2003-03-11 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / c-typeck.c
blob52105ac84ecef8a6a705bc43bfeec65c5278e51e
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. */
1155 for (; field; field = TREE_CHAIN (field))
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;
1174 return ref;
1176 else if (code != ERROR_MARK)
1177 error ("request for member `%s' in something not a structure or union",
1178 IDENTIFIER_POINTER (component));
1180 return error_mark_node;
1183 /* Given an expression PTR for a pointer, return an expression
1184 for the value pointed to.
1185 ERRORSTRING is the name of the operator to appear in error messages. */
1187 tree
1188 build_indirect_ref (ptr, errorstring)
1189 tree ptr;
1190 const char *errorstring;
1192 tree pointer = default_conversion (ptr);
1193 tree type = TREE_TYPE (pointer);
1195 if (TREE_CODE (type) == POINTER_TYPE)
1197 if (TREE_CODE (pointer) == ADDR_EXPR
1198 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1199 == TREE_TYPE (type)))
1200 return TREE_OPERAND (pointer, 0);
1201 else
1203 tree t = TREE_TYPE (type);
1204 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1206 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1208 error ("dereferencing pointer to incomplete type");
1209 return error_mark_node;
1211 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1212 warning ("dereferencing `void *' pointer");
1214 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1215 so that we get the proper error message if the result is used
1216 to assign to. Also, &* is supposed to be a no-op.
1217 And ANSI C seems to specify that the type of the result
1218 should be the const type. */
1219 /* A de-reference of a pointer to const is not a const. It is valid
1220 to change it via some other pointer. */
1221 TREE_READONLY (ref) = TYPE_READONLY (t);
1222 TREE_SIDE_EFFECTS (ref)
1223 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1224 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1225 return ref;
1228 else if (TREE_CODE (pointer) != ERROR_MARK)
1229 error ("invalid type argument of `%s'", errorstring);
1230 return error_mark_node;
1233 /* This handles expressions of the form "a[i]", which denotes
1234 an array reference.
1236 This is logically equivalent in C to *(a+i), but we may do it differently.
1237 If A is a variable or a member, we generate a primitive ARRAY_REF.
1238 This avoids forcing the array out of registers, and can work on
1239 arrays that are not lvalues (for example, members of structures returned
1240 by functions). */
1242 tree
1243 build_array_ref (array, index)
1244 tree array, index;
1246 if (index == 0)
1248 error ("subscript missing in array reference");
1249 return error_mark_node;
1252 if (TREE_TYPE (array) == error_mark_node
1253 || TREE_TYPE (index) == error_mark_node)
1254 return error_mark_node;
1256 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1257 && TREE_CODE (array) != INDIRECT_REF)
1259 tree rval, type;
1261 /* Subscripting with type char is likely to lose
1262 on a machine where chars are signed.
1263 So warn on any machine, but optionally.
1264 Don't warn for unsigned char since that type is safe.
1265 Don't warn for signed char because anyone who uses that
1266 must have done so deliberately. */
1267 if (warn_char_subscripts
1268 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1269 warning ("array subscript has type `char'");
1271 /* Apply default promotions *after* noticing character types. */
1272 index = default_conversion (index);
1274 /* Require integer *after* promotion, for sake of enums. */
1275 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1277 error ("array subscript is not an integer");
1278 return error_mark_node;
1281 /* An array that is indexed by a non-constant
1282 cannot be stored in a register; we must be able to do
1283 address arithmetic on its address.
1284 Likewise an array of elements of variable size. */
1285 if (TREE_CODE (index) != INTEGER_CST
1286 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1287 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1289 if (!c_mark_addressable (array))
1290 return error_mark_node;
1292 /* An array that is indexed by a constant value which is not within
1293 the array bounds cannot be stored in a register either; because we
1294 would get a crash in store_bit_field/extract_bit_field when trying
1295 to access a non-existent part of the register. */
1296 if (TREE_CODE (index) == INTEGER_CST
1297 && TYPE_VALUES (TREE_TYPE (array))
1298 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1300 if (!c_mark_addressable (array))
1301 return error_mark_node;
1304 if (pedantic)
1306 tree foo = array;
1307 while (TREE_CODE (foo) == COMPONENT_REF)
1308 foo = TREE_OPERAND (foo, 0);
1309 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1310 pedwarn ("ISO C forbids subscripting `register' array");
1311 else if (! flag_isoc99 && ! lvalue_p (foo))
1312 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1315 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1316 rval = build (ARRAY_REF, type, array, index);
1317 /* Array ref is const/volatile if the array elements are
1318 or if the array is. */
1319 TREE_READONLY (rval)
1320 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1321 | TREE_READONLY (array));
1322 TREE_SIDE_EFFECTS (rval)
1323 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1324 | TREE_SIDE_EFFECTS (array));
1325 TREE_THIS_VOLATILE (rval)
1326 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1327 /* This was added by rms on 16 Nov 91.
1328 It fixes vol struct foo *a; a->elts[1]
1329 in an inline function.
1330 Hope it doesn't break something else. */
1331 | TREE_THIS_VOLATILE (array));
1332 return require_complete_type (fold (rval));
1336 tree ar = default_conversion (array);
1337 tree ind = default_conversion (index);
1339 /* Do the same warning check as above, but only on the part that's
1340 syntactically the index and only if it is also semantically
1341 the index. */
1342 if (warn_char_subscripts
1343 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1344 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1345 warning ("subscript has type `char'");
1347 /* Put the integer in IND to simplify error checking. */
1348 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1350 tree temp = ar;
1351 ar = ind;
1352 ind = temp;
1355 if (ar == error_mark_node)
1356 return ar;
1358 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1359 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1361 error ("subscripted value is neither array nor pointer");
1362 return error_mark_node;
1364 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1366 error ("array subscript is not an integer");
1367 return error_mark_node;
1370 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1371 "array indexing");
1375 /* Build an external reference to identifier ID. FUN indicates
1376 whether this will be used for a function call. */
1377 tree
1378 build_external_ref (id, fun)
1379 tree id;
1380 int fun;
1382 tree ref;
1383 tree decl = lookup_name (id);
1384 tree objc_ivar = lookup_objc_ivar (id);
1386 if (decl && TREE_DEPRECATED (decl))
1387 warn_deprecated_use (decl);
1389 if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
1391 if (objc_ivar)
1392 ref = objc_ivar;
1393 else if (fun)
1395 if (!decl || decl == error_mark_node)
1396 /* Ordinary implicit function declaration. */
1397 ref = implicitly_declare (id);
1398 else
1400 /* Implicit declaration of built-in function. Don't
1401 change the built-in declaration, but don't let this
1402 go by silently, either. */
1403 implicit_decl_warning (id);
1405 /* only issue this warning once */
1406 C_DECL_ANTICIPATED (decl) = 0;
1407 ref = decl;
1410 else
1412 /* Reference to undeclared variable, including reference to
1413 builtin outside of function-call context. */
1414 if (current_function_decl == 0)
1415 error ("`%s' undeclared here (not in a function)",
1416 IDENTIFIER_POINTER (id));
1417 else
1419 if (IDENTIFIER_GLOBAL_VALUE (id) != error_mark_node
1420 || IDENTIFIER_ERROR_LOCUS (id) != current_function_decl)
1422 error ("`%s' undeclared (first use in this function)",
1423 IDENTIFIER_POINTER (id));
1425 if (! undeclared_variable_notice)
1427 error ("(Each undeclared identifier is reported only once");
1428 error ("for each function it appears in.)");
1429 undeclared_variable_notice = 1;
1432 IDENTIFIER_GLOBAL_VALUE (id) = error_mark_node;
1433 IDENTIFIER_ERROR_LOCUS (id) = current_function_decl;
1435 return error_mark_node;
1438 else
1440 /* Properly declared variable or function reference. */
1441 if (!objc_ivar)
1442 ref = decl;
1443 else if (decl != objc_ivar && IDENTIFIER_LOCAL_VALUE (id))
1445 warning ("local declaration of `%s' hides instance variable",
1446 IDENTIFIER_POINTER (id));
1447 ref = decl;
1449 else
1450 ref = objc_ivar;
1453 if (TREE_TYPE (ref) == error_mark_node)
1454 return error_mark_node;
1456 if (!skip_evaluation)
1457 assemble_external (ref);
1458 TREE_USED (ref) = 1;
1460 if (TREE_CODE (ref) == CONST_DECL)
1462 ref = DECL_INITIAL (ref);
1463 TREE_CONSTANT (ref) = 1;
1466 return ref;
1469 /* Build a function call to function FUNCTION with parameters PARAMS.
1470 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1471 TREE_VALUE of each node is a parameter-expression.
1472 FUNCTION's data type may be a function type or a pointer-to-function. */
1474 tree
1475 build_function_call (function, params)
1476 tree function, params;
1478 tree fntype, fundecl = 0;
1479 tree coerced_params;
1480 tree name = NULL_TREE, result;
1482 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1483 STRIP_TYPE_NOPS (function);
1485 /* Convert anything with function type to a pointer-to-function. */
1486 if (TREE_CODE (function) == FUNCTION_DECL)
1488 name = DECL_NAME (function);
1490 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1491 (because calling an inline function does not mean the function
1492 needs to be separately compiled). */
1493 fntype = build_type_variant (TREE_TYPE (function),
1494 TREE_READONLY (function),
1495 TREE_THIS_VOLATILE (function));
1496 fundecl = function;
1497 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1499 else
1500 function = default_conversion (function);
1502 fntype = TREE_TYPE (function);
1504 if (TREE_CODE (fntype) == ERROR_MARK)
1505 return error_mark_node;
1507 if (!(TREE_CODE (fntype) == POINTER_TYPE
1508 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1510 error ("called object is not a function");
1511 return error_mark_node;
1514 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1515 current_function_returns_abnormally = 1;
1517 /* fntype now gets the type of function pointed to. */
1518 fntype = TREE_TYPE (fntype);
1520 /* Convert the parameters to the types declared in the
1521 function prototype, or apply default promotions. */
1523 coerced_params
1524 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1526 /* Check that the arguments to the function are valid. */
1528 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1530 /* Recognize certain built-in functions so we can make tree-codes
1531 other than CALL_EXPR. We do this when it enables fold-const.c
1532 to do something useful. */
1534 if (TREE_CODE (function) == ADDR_EXPR
1535 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1536 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1538 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1539 params, coerced_params);
1540 if (result)
1541 return result;
1544 result = build (CALL_EXPR, TREE_TYPE (fntype),
1545 function, coerced_params, NULL_TREE);
1546 TREE_SIDE_EFFECTS (result) = 1;
1547 result = fold (result);
1549 if (VOID_TYPE_P (TREE_TYPE (result)))
1550 return result;
1551 return require_complete_type (result);
1554 /* Convert the argument expressions in the list VALUES
1555 to the types in the list TYPELIST. The result is a list of converted
1556 argument expressions.
1558 If TYPELIST is exhausted, or when an element has NULL as its type,
1559 perform the default conversions.
1561 PARMLIST is the chain of parm decls for the function being called.
1562 It may be 0, if that info is not available.
1563 It is used only for generating error messages.
1565 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1567 This is also where warnings about wrong number of args are generated.
1569 Both VALUES and the returned value are chains of TREE_LIST nodes
1570 with the elements of the list in the TREE_VALUE slots of those nodes. */
1572 static tree
1573 convert_arguments (typelist, values, name, fundecl)
1574 tree typelist, values, name, fundecl;
1576 tree typetail, valtail;
1577 tree result = NULL;
1578 int parmnum;
1580 /* Scan the given expressions and types, producing individual
1581 converted arguments and pushing them on RESULT in reverse order. */
1583 for (valtail = values, typetail = typelist, parmnum = 0;
1584 valtail;
1585 valtail = TREE_CHAIN (valtail), parmnum++)
1587 tree type = typetail ? TREE_VALUE (typetail) : 0;
1588 tree val = TREE_VALUE (valtail);
1590 if (type == void_type_node)
1592 if (name)
1593 error ("too many arguments to function `%s'",
1594 IDENTIFIER_POINTER (name));
1595 else
1596 error ("too many arguments to function");
1597 break;
1600 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1601 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1602 to convert automatically to a pointer. */
1603 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1604 val = TREE_OPERAND (val, 0);
1606 val = default_function_array_conversion (val);
1608 val = require_complete_type (val);
1610 if (type != 0)
1612 /* Formal parm type is specified by a function prototype. */
1613 tree parmval;
1615 if (!COMPLETE_TYPE_P (type))
1617 error ("type of formal parameter %d is incomplete", parmnum + 1);
1618 parmval = val;
1620 else
1622 /* Optionally warn about conversions that
1623 differ from the default conversions. */
1624 if (warn_conversion || warn_traditional)
1626 int formal_prec = TYPE_PRECISION (type);
1628 if (INTEGRAL_TYPE_P (type)
1629 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1630 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1631 if (INTEGRAL_TYPE_P (type)
1632 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1633 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1634 else if (TREE_CODE (type) == COMPLEX_TYPE
1635 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1636 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1637 else if (TREE_CODE (type) == REAL_TYPE
1638 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1639 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1640 else if (TREE_CODE (type) == COMPLEX_TYPE
1641 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1642 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1643 else if (TREE_CODE (type) == REAL_TYPE
1644 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1645 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1646 /* ??? At some point, messages should be written about
1647 conversions between complex types, but that's too messy
1648 to do now. */
1649 else if (TREE_CODE (type) == REAL_TYPE
1650 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1652 /* Warn if any argument is passed as `float',
1653 since without a prototype it would be `double'. */
1654 if (formal_prec == TYPE_PRECISION (float_type_node))
1655 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1657 /* Detect integer changing in width or signedness.
1658 These warnings are only activated with
1659 -Wconversion, not with -Wtraditional. */
1660 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1661 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1663 tree would_have_been = default_conversion (val);
1664 tree type1 = TREE_TYPE (would_have_been);
1666 if (TREE_CODE (type) == ENUMERAL_TYPE
1667 && (TYPE_MAIN_VARIANT (type)
1668 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1669 /* No warning if function asks for enum
1670 and the actual arg is that enum type. */
1672 else if (formal_prec != TYPE_PRECISION (type1))
1673 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1674 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1676 /* Don't complain if the formal parameter type
1677 is an enum, because we can't tell now whether
1678 the value was an enum--even the same enum. */
1679 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1681 else if (TREE_CODE (val) == INTEGER_CST
1682 && int_fits_type_p (val, type))
1683 /* Change in signedness doesn't matter
1684 if a constant value is unaffected. */
1686 /* Likewise for a constant in a NOP_EXPR. */
1687 else if (TREE_CODE (val) == NOP_EXPR
1688 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1689 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1691 #if 0 /* We never get such tree structure here. */
1692 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1693 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1694 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1695 /* Change in signedness doesn't matter
1696 if an enum value is unaffected. */
1698 #endif
1699 /* If the value is extended from a narrower
1700 unsigned type, it doesn't matter whether we
1701 pass it as signed or unsigned; the value
1702 certainly is the same either way. */
1703 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1704 && TREE_UNSIGNED (TREE_TYPE (val)))
1706 else if (TREE_UNSIGNED (type))
1707 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1708 else
1709 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1713 parmval = convert_for_assignment (type, val,
1714 (char *) 0, /* arg passing */
1715 fundecl, name, parmnum + 1);
1717 if (PROMOTE_PROTOTYPES
1718 && INTEGRAL_TYPE_P (type)
1719 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1720 parmval = default_conversion (parmval);
1722 result = tree_cons (NULL_TREE, parmval, result);
1724 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1725 && (TYPE_PRECISION (TREE_TYPE (val))
1726 < TYPE_PRECISION (double_type_node)))
1727 /* Convert `float' to `double'. */
1728 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1729 else
1730 /* Convert `short' and `char' to full-size `int'. */
1731 result = tree_cons (NULL_TREE, default_conversion (val), result);
1733 if (typetail)
1734 typetail = TREE_CHAIN (typetail);
1737 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1739 if (name)
1740 error ("too few arguments to function `%s'",
1741 IDENTIFIER_POINTER (name));
1742 else
1743 error ("too few arguments to function");
1746 return nreverse (result);
1749 /* This is the entry point used by the parser
1750 for binary operators in the input.
1751 In addition to constructing the expression,
1752 we check for operands that were written with other binary operators
1753 in a way that is likely to confuse the user. */
1755 tree
1756 parser_build_binary_op (code, arg1, arg2)
1757 enum tree_code code;
1758 tree arg1, arg2;
1760 tree result = build_binary_op (code, arg1, arg2, 1);
1762 char class;
1763 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1764 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1765 enum tree_code code1 = ERROR_MARK;
1766 enum tree_code code2 = ERROR_MARK;
1768 if (TREE_CODE (result) == ERROR_MARK)
1769 return error_mark_node;
1771 if (IS_EXPR_CODE_CLASS (class1))
1772 code1 = C_EXP_ORIGINAL_CODE (arg1);
1773 if (IS_EXPR_CODE_CLASS (class2))
1774 code2 = C_EXP_ORIGINAL_CODE (arg2);
1776 /* Check for cases such as x+y<<z which users are likely
1777 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1778 is cleared to prevent these warnings. */
1779 if (warn_parentheses)
1781 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1783 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1784 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1785 warning ("suggest parentheses around + or - inside shift");
1788 if (code == TRUTH_ORIF_EXPR)
1790 if (code1 == TRUTH_ANDIF_EXPR
1791 || code2 == TRUTH_ANDIF_EXPR)
1792 warning ("suggest parentheses around && within ||");
1795 if (code == BIT_IOR_EXPR)
1797 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1798 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1799 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1800 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1801 warning ("suggest parentheses around arithmetic in operand of |");
1802 /* Check cases like x|y==z */
1803 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1804 warning ("suggest parentheses around comparison in operand of |");
1807 if (code == BIT_XOR_EXPR)
1809 if (code1 == BIT_AND_EXPR
1810 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1811 || code2 == BIT_AND_EXPR
1812 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1813 warning ("suggest parentheses around arithmetic in operand of ^");
1814 /* Check cases like x^y==z */
1815 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1816 warning ("suggest parentheses around comparison in operand of ^");
1819 if (code == BIT_AND_EXPR)
1821 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1822 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1823 warning ("suggest parentheses around + or - in operand of &");
1824 /* Check cases like x&y==z */
1825 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1826 warning ("suggest parentheses around comparison in operand of &");
1830 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1831 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1832 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1833 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1835 unsigned_conversion_warning (result, arg1);
1836 unsigned_conversion_warning (result, arg2);
1837 overflow_warning (result);
1839 class = TREE_CODE_CLASS (TREE_CODE (result));
1841 /* Record the code that was specified in the source,
1842 for the sake of warnings about confusing nesting. */
1843 if (IS_EXPR_CODE_CLASS (class))
1844 C_SET_EXP_ORIGINAL_CODE (result, code);
1845 else
1847 int flag = TREE_CONSTANT (result);
1848 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1849 so that convert_for_assignment wouldn't strip it.
1850 That way, we got warnings for things like p = (1 - 1).
1851 But it turns out we should not get those warnings. */
1852 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1853 C_SET_EXP_ORIGINAL_CODE (result, code);
1854 TREE_CONSTANT (result) = flag;
1857 return result;
1860 /* Build a binary-operation expression without default conversions.
1861 CODE is the kind of expression to build.
1862 This function differs from `build' in several ways:
1863 the data type of the result is computed and recorded in it,
1864 warnings are generated if arg data types are invalid,
1865 special handling for addition and subtraction of pointers is known,
1866 and some optimization is done (operations on narrow ints
1867 are done in the narrower type when that gives the same result).
1868 Constant folding is also done before the result is returned.
1870 Note that the operands will never have enumeral types, or function
1871 or array types, because either they will have the default conversions
1872 performed or they have both just been converted to some other type in which
1873 the arithmetic is to be done. */
1875 tree
1876 build_binary_op (code, orig_op0, orig_op1, convert_p)
1877 enum tree_code code;
1878 tree orig_op0, orig_op1;
1879 int convert_p;
1881 tree type0, type1;
1882 enum tree_code code0, code1;
1883 tree op0, op1;
1885 /* Expression code to give to the expression when it is built.
1886 Normally this is CODE, which is what the caller asked for,
1887 but in some special cases we change it. */
1888 enum tree_code resultcode = code;
1890 /* Data type in which the computation is to be performed.
1891 In the simplest cases this is the common type of the arguments. */
1892 tree result_type = NULL;
1894 /* Nonzero means operands have already been type-converted
1895 in whatever way is necessary.
1896 Zero means they need to be converted to RESULT_TYPE. */
1897 int converted = 0;
1899 /* Nonzero means create the expression with this type, rather than
1900 RESULT_TYPE. */
1901 tree build_type = 0;
1903 /* Nonzero means after finally constructing the expression
1904 convert it to this type. */
1905 tree final_type = 0;
1907 /* Nonzero if this is an operation like MIN or MAX which can
1908 safely be computed in short if both args are promoted shorts.
1909 Also implies COMMON.
1910 -1 indicates a bitwise operation; this makes a difference
1911 in the exact conditions for when it is safe to do the operation
1912 in a narrower mode. */
1913 int shorten = 0;
1915 /* Nonzero if this is a comparison operation;
1916 if both args are promoted shorts, compare the original shorts.
1917 Also implies COMMON. */
1918 int short_compare = 0;
1920 /* Nonzero if this is a right-shift operation, which can be computed on the
1921 original short and then promoted if the operand is a promoted short. */
1922 int short_shift = 0;
1924 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1925 int common = 0;
1927 if (convert_p)
1929 op0 = default_conversion (orig_op0);
1930 op1 = default_conversion (orig_op1);
1932 else
1934 op0 = orig_op0;
1935 op1 = orig_op1;
1938 type0 = TREE_TYPE (op0);
1939 type1 = TREE_TYPE (op1);
1941 /* The expression codes of the data types of the arguments tell us
1942 whether the arguments are integers, floating, pointers, etc. */
1943 code0 = TREE_CODE (type0);
1944 code1 = TREE_CODE (type1);
1946 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1947 STRIP_TYPE_NOPS (op0);
1948 STRIP_TYPE_NOPS (op1);
1950 /* If an error was already reported for one of the arguments,
1951 avoid reporting another error. */
1953 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1954 return error_mark_node;
1956 switch (code)
1958 case PLUS_EXPR:
1959 /* Handle the pointer + int case. */
1960 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1961 return pointer_int_sum (PLUS_EXPR, op0, op1);
1962 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1963 return pointer_int_sum (PLUS_EXPR, op1, op0);
1964 else
1965 common = 1;
1966 break;
1968 case MINUS_EXPR:
1969 /* Subtraction of two similar pointers.
1970 We must subtract them as integers, then divide by object size. */
1971 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1972 && comp_target_types (type0, type1, 1))
1973 return pointer_diff (op0, op1);
1974 /* Handle pointer minus int. Just like pointer plus int. */
1975 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1976 return pointer_int_sum (MINUS_EXPR, op0, op1);
1977 else
1978 common = 1;
1979 break;
1981 case MULT_EXPR:
1982 common = 1;
1983 break;
1985 case TRUNC_DIV_EXPR:
1986 case CEIL_DIV_EXPR:
1987 case FLOOR_DIV_EXPR:
1988 case ROUND_DIV_EXPR:
1989 case EXACT_DIV_EXPR:
1990 /* Floating point division by zero is a legitimate way to obtain
1991 infinities and NaNs. */
1992 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
1993 warning ("division by zero");
1995 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1996 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
1997 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1998 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
2000 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2001 resultcode = RDIV_EXPR;
2002 else
2003 /* Although it would be tempting to shorten always here, that
2004 loses on some targets, since the modulo instruction is
2005 undefined if the quotient can't be represented in the
2006 computation mode. We shorten only if unsigned or if
2007 dividing by something we know != -1. */
2008 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2009 || (TREE_CODE (op1) == INTEGER_CST
2010 && ! integer_all_onesp (op1)));
2011 common = 1;
2013 break;
2015 case BIT_AND_EXPR:
2016 case BIT_ANDTC_EXPR:
2017 case BIT_IOR_EXPR:
2018 case BIT_XOR_EXPR:
2019 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2020 shorten = -1;
2021 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
2022 common = 1;
2023 break;
2025 case TRUNC_MOD_EXPR:
2026 case FLOOR_MOD_EXPR:
2027 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2028 warning ("division by zero");
2030 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2032 /* Although it would be tempting to shorten always here, that loses
2033 on some targets, since the modulo instruction is undefined if the
2034 quotient can't be represented in the computation mode. We shorten
2035 only if unsigned or if dividing by something we know != -1. */
2036 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2037 || (TREE_CODE (op1) == INTEGER_CST
2038 && ! integer_all_onesp (op1)));
2039 common = 1;
2041 break;
2043 case TRUTH_ANDIF_EXPR:
2044 case TRUTH_ORIF_EXPR:
2045 case TRUTH_AND_EXPR:
2046 case TRUTH_OR_EXPR:
2047 case TRUTH_XOR_EXPR:
2048 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2049 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2050 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2051 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2053 /* Result of these operations is always an int,
2054 but that does not mean the operands should be
2055 converted to ints! */
2056 result_type = integer_type_node;
2057 op0 = c_common_truthvalue_conversion (op0);
2058 op1 = c_common_truthvalue_conversion (op1);
2059 converted = 1;
2061 break;
2063 /* Shift operations: result has same type as first operand;
2064 always convert second operand to int.
2065 Also set SHORT_SHIFT if shifting rightward. */
2067 case RSHIFT_EXPR:
2068 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2070 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2072 if (tree_int_cst_sgn (op1) < 0)
2073 warning ("right shift count is negative");
2074 else
2076 if (! integer_zerop (op1))
2077 short_shift = 1;
2079 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2080 warning ("right shift count >= width of type");
2084 /* Use the type of the value to be shifted. */
2085 result_type = type0;
2086 /* Convert the shift-count to an integer, regardless of size
2087 of value being shifted. */
2088 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2089 op1 = convert (integer_type_node, op1);
2090 /* Avoid converting op1 to result_type later. */
2091 converted = 1;
2093 break;
2095 case LSHIFT_EXPR:
2096 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2098 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2100 if (tree_int_cst_sgn (op1) < 0)
2101 warning ("left shift count is negative");
2103 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2104 warning ("left shift count >= width of type");
2107 /* Use the type of the value to be shifted. */
2108 result_type = type0;
2109 /* Convert the shift-count to an integer, regardless of size
2110 of value being shifted. */
2111 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2112 op1 = convert (integer_type_node, op1);
2113 /* Avoid converting op1 to result_type later. */
2114 converted = 1;
2116 break;
2118 case RROTATE_EXPR:
2119 case LROTATE_EXPR:
2120 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2122 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2124 if (tree_int_cst_sgn (op1) < 0)
2125 warning ("shift count is negative");
2126 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2127 warning ("shift count >= width of type");
2130 /* Use the type of the value to be shifted. */
2131 result_type = type0;
2132 /* Convert the shift-count to an integer, regardless of size
2133 of value being shifted. */
2134 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2135 op1 = convert (integer_type_node, op1);
2136 /* Avoid converting op1 to result_type later. */
2137 converted = 1;
2139 break;
2141 case EQ_EXPR:
2142 case NE_EXPR:
2143 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2144 warning ("comparing floating point with == or != is unsafe");
2145 /* Result of comparison is always int,
2146 but don't convert the args to int! */
2147 build_type = integer_type_node;
2148 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2149 || code0 == COMPLEX_TYPE
2150 || code0 == VECTOR_TYPE)
2151 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2152 || code1 == COMPLEX_TYPE
2153 || code1 == VECTOR_TYPE))
2154 short_compare = 1;
2155 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2157 tree tt0 = TREE_TYPE (type0);
2158 tree tt1 = TREE_TYPE (type1);
2159 /* Anything compares with void *. void * compares with anything.
2160 Otherwise, the targets must be compatible
2161 and both must be object or both incomplete. */
2162 if (comp_target_types (type0, type1, 1))
2163 result_type = common_type (type0, type1);
2164 else if (VOID_TYPE_P (tt0))
2166 /* op0 != orig_op0 detects the case of something
2167 whose value is 0 but which isn't a valid null ptr const. */
2168 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2169 && TREE_CODE (tt1) == FUNCTION_TYPE)
2170 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2172 else if (VOID_TYPE_P (tt1))
2174 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2175 && TREE_CODE (tt0) == FUNCTION_TYPE)
2176 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2178 else
2179 pedwarn ("comparison of distinct pointer types lacks a cast");
2181 if (result_type == NULL_TREE)
2182 result_type = ptr_type_node;
2184 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2185 && integer_zerop (op1))
2186 result_type = type0;
2187 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2188 && integer_zerop (op0))
2189 result_type = type1;
2190 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2192 result_type = type0;
2193 pedwarn ("comparison between pointer and integer");
2195 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2197 result_type = type1;
2198 pedwarn ("comparison between pointer and integer");
2200 break;
2202 case MAX_EXPR:
2203 case MIN_EXPR:
2204 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2205 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2206 shorten = 1;
2207 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2209 if (comp_target_types (type0, type1, 1))
2211 result_type = common_type (type0, type1);
2212 if (pedantic
2213 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2214 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2216 else
2218 result_type = ptr_type_node;
2219 pedwarn ("comparison of distinct pointer types lacks a cast");
2222 break;
2224 case LE_EXPR:
2225 case GE_EXPR:
2226 case LT_EXPR:
2227 case GT_EXPR:
2228 build_type = integer_type_node;
2229 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2230 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2231 short_compare = 1;
2232 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2234 if (comp_target_types (type0, type1, 1))
2236 result_type = common_type (type0, type1);
2237 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2238 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2239 pedwarn ("comparison of complete and incomplete pointers");
2240 else if (pedantic
2241 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2242 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2244 else
2246 result_type = ptr_type_node;
2247 pedwarn ("comparison of distinct pointer types lacks a cast");
2250 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2251 && integer_zerop (op1))
2253 result_type = type0;
2254 if (pedantic || extra_warnings)
2255 pedwarn ("ordered comparison of pointer with integer zero");
2257 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2258 && integer_zerop (op0))
2260 result_type = type1;
2261 if (pedantic)
2262 pedwarn ("ordered comparison of pointer with integer zero");
2264 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2266 result_type = type0;
2267 pedwarn ("comparison between pointer and integer");
2269 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2271 result_type = type1;
2272 pedwarn ("comparison between pointer and integer");
2274 break;
2276 case UNORDERED_EXPR:
2277 case ORDERED_EXPR:
2278 case UNLT_EXPR:
2279 case UNLE_EXPR:
2280 case UNGT_EXPR:
2281 case UNGE_EXPR:
2282 case UNEQ_EXPR:
2283 build_type = integer_type_node;
2284 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2286 error ("unordered comparison on non-floating point argument");
2287 return error_mark_node;
2289 common = 1;
2290 break;
2292 default:
2293 break;
2296 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
2297 || code0 == VECTOR_TYPE)
2299 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
2300 || code1 == VECTOR_TYPE))
2302 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2304 if (shorten || common || short_compare)
2305 result_type = common_type (type0, type1);
2307 /* For certain operations (which identify themselves by shorten != 0)
2308 if both args were extended from the same smaller type,
2309 do the arithmetic in that type and then extend.
2311 shorten !=0 and !=1 indicates a bitwise operation.
2312 For them, this optimization is safe only if
2313 both args are zero-extended or both are sign-extended.
2314 Otherwise, we might change the result.
2315 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2316 but calculated in (unsigned short) it would be (unsigned short)-1. */
2318 if (shorten && none_complex)
2320 int unsigned0, unsigned1;
2321 tree arg0 = get_narrower (op0, &unsigned0);
2322 tree arg1 = get_narrower (op1, &unsigned1);
2323 /* UNS is 1 if the operation to be done is an unsigned one. */
2324 int uns = TREE_UNSIGNED (result_type);
2325 tree type;
2327 final_type = result_type;
2329 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2330 but it *requires* conversion to FINAL_TYPE. */
2332 if ((TYPE_PRECISION (TREE_TYPE (op0))
2333 == TYPE_PRECISION (TREE_TYPE (arg0)))
2334 && TREE_TYPE (op0) != final_type)
2335 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2336 if ((TYPE_PRECISION (TREE_TYPE (op1))
2337 == TYPE_PRECISION (TREE_TYPE (arg1)))
2338 && TREE_TYPE (op1) != final_type)
2339 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2341 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2343 /* For bitwise operations, signedness of nominal type
2344 does not matter. Consider only how operands were extended. */
2345 if (shorten == -1)
2346 uns = unsigned0;
2348 /* Note that in all three cases below we refrain from optimizing
2349 an unsigned operation on sign-extended args.
2350 That would not be valid. */
2352 /* Both args variable: if both extended in same way
2353 from same width, do it in that width.
2354 Do it unsigned if args were zero-extended. */
2355 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2356 < TYPE_PRECISION (result_type))
2357 && (TYPE_PRECISION (TREE_TYPE (arg1))
2358 == TYPE_PRECISION (TREE_TYPE (arg0)))
2359 && unsigned0 == unsigned1
2360 && (unsigned0 || !uns))
2361 result_type
2362 = c_common_signed_or_unsigned_type
2363 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2364 else if (TREE_CODE (arg0) == INTEGER_CST
2365 && (unsigned1 || !uns)
2366 && (TYPE_PRECISION (TREE_TYPE (arg1))
2367 < TYPE_PRECISION (result_type))
2368 && (type
2369 = c_common_signed_or_unsigned_type (unsigned1,
2370 TREE_TYPE (arg1)),
2371 int_fits_type_p (arg0, type)))
2372 result_type = type;
2373 else if (TREE_CODE (arg1) == INTEGER_CST
2374 && (unsigned0 || !uns)
2375 && (TYPE_PRECISION (TREE_TYPE (arg0))
2376 < TYPE_PRECISION (result_type))
2377 && (type
2378 = c_common_signed_or_unsigned_type (unsigned0,
2379 TREE_TYPE (arg0)),
2380 int_fits_type_p (arg1, type)))
2381 result_type = type;
2384 /* Shifts can be shortened if shifting right. */
2386 if (short_shift)
2388 int unsigned_arg;
2389 tree arg0 = get_narrower (op0, &unsigned_arg);
2391 final_type = result_type;
2393 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2394 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2396 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2397 /* We can shorten only if the shift count is less than the
2398 number of bits in the smaller type size. */
2399 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2400 /* We cannot drop an unsigned shift after sign-extension. */
2401 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2403 /* Do an unsigned shift if the operand was zero-extended. */
2404 result_type
2405 = c_common_signed_or_unsigned_type (unsigned_arg,
2406 TREE_TYPE (arg0));
2407 /* Convert value-to-be-shifted to that type. */
2408 if (TREE_TYPE (op0) != result_type)
2409 op0 = convert (result_type, op0);
2410 converted = 1;
2414 /* Comparison operations are shortened too but differently.
2415 They identify themselves by setting short_compare = 1. */
2417 if (short_compare)
2419 /* Don't write &op0, etc., because that would prevent op0
2420 from being kept in a register.
2421 Instead, make copies of the our local variables and
2422 pass the copies by reference, then copy them back afterward. */
2423 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2424 enum tree_code xresultcode = resultcode;
2425 tree val
2426 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2428 if (val != 0)
2429 return val;
2431 op0 = xop0, op1 = xop1;
2432 converted = 1;
2433 resultcode = xresultcode;
2435 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2436 && skip_evaluation == 0)
2438 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2439 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2440 int unsignedp0, unsignedp1;
2441 tree primop0 = get_narrower (op0, &unsignedp0);
2442 tree primop1 = get_narrower (op1, &unsignedp1);
2444 xop0 = orig_op0;
2445 xop1 = orig_op1;
2446 STRIP_TYPE_NOPS (xop0);
2447 STRIP_TYPE_NOPS (xop1);
2449 /* Give warnings for comparisons between signed and unsigned
2450 quantities that may fail.
2452 Do the checking based on the original operand trees, so that
2453 casts will be considered, but default promotions won't be.
2455 Do not warn if the comparison is being done in a signed type,
2456 since the signed type will only be chosen if it can represent
2457 all the values of the unsigned type. */
2458 if (! TREE_UNSIGNED (result_type))
2459 /* OK */;
2460 /* Do not warn if both operands are the same signedness. */
2461 else if (op0_signed == op1_signed)
2462 /* OK */;
2463 else
2465 tree sop, uop;
2467 if (op0_signed)
2468 sop = xop0, uop = xop1;
2469 else
2470 sop = xop1, uop = xop0;
2472 /* Do not warn if the signed quantity is an
2473 unsuffixed integer literal (or some static
2474 constant expression involving such literals or a
2475 conditional expression involving such literals)
2476 and it is non-negative. */
2477 if (c_tree_expr_nonnegative_p (sop))
2478 /* OK */;
2479 /* Do not warn if the comparison is an equality operation,
2480 the unsigned quantity is an integral constant, and it
2481 would fit in the result if the result were signed. */
2482 else if (TREE_CODE (uop) == INTEGER_CST
2483 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2484 && int_fits_type_p
2485 (uop, c_common_signed_type (result_type)))
2486 /* OK */;
2487 /* Do not warn if the unsigned quantity is an enumeration
2488 constant and its maximum value would fit in the result
2489 if the result were signed. */
2490 else if (TREE_CODE (uop) == INTEGER_CST
2491 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2492 && int_fits_type_p
2493 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2494 c_common_signed_type (result_type)))
2495 /* OK */;
2496 else
2497 warning ("comparison between signed and unsigned");
2500 /* Warn if two unsigned values are being compared in a size
2501 larger than their original size, and one (and only one) is the
2502 result of a `~' operator. This comparison will always fail.
2504 Also warn if one operand is a constant, and the constant
2505 does not have all bits set that are set in the ~ operand
2506 when it is extended. */
2508 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2509 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2511 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2512 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2513 &unsignedp0);
2514 else
2515 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2516 &unsignedp1);
2518 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2520 tree primop;
2521 HOST_WIDE_INT constant, mask;
2522 int unsignedp, bits;
2524 if (host_integerp (primop0, 0))
2526 primop = primop1;
2527 unsignedp = unsignedp1;
2528 constant = tree_low_cst (primop0, 0);
2530 else
2532 primop = primop0;
2533 unsignedp = unsignedp0;
2534 constant = tree_low_cst (primop1, 0);
2537 bits = TYPE_PRECISION (TREE_TYPE (primop));
2538 if (bits < TYPE_PRECISION (result_type)
2539 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2541 mask = (~ (HOST_WIDE_INT) 0) << bits;
2542 if ((mask & constant) != mask)
2543 warning ("comparison of promoted ~unsigned with constant");
2546 else if (unsignedp0 && unsignedp1
2547 && (TYPE_PRECISION (TREE_TYPE (primop0))
2548 < TYPE_PRECISION (result_type))
2549 && (TYPE_PRECISION (TREE_TYPE (primop1))
2550 < TYPE_PRECISION (result_type)))
2551 warning ("comparison of promoted ~unsigned with unsigned");
2557 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2558 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2559 Then the expression will be built.
2560 It will be given type FINAL_TYPE if that is nonzero;
2561 otherwise, it will be given type RESULT_TYPE. */
2563 if (!result_type)
2565 binary_op_error (code);
2566 return error_mark_node;
2569 if (! converted)
2571 if (TREE_TYPE (op0) != result_type)
2572 op0 = convert (result_type, op0);
2573 if (TREE_TYPE (op1) != result_type)
2574 op1 = convert (result_type, op1);
2577 if (build_type == NULL_TREE)
2578 build_type = result_type;
2581 tree result = build (resultcode, build_type, op0, op1);
2582 tree folded;
2584 folded = fold (result);
2585 if (folded == result)
2586 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2587 if (final_type != 0)
2588 return convert (final_type, folded);
2589 return folded;
2594 /* Return true if `t' is known to be non-negative. */
2597 c_tree_expr_nonnegative_p (t)
2598 tree t;
2600 if (TREE_CODE (t) == STMT_EXPR)
2602 t=COMPOUND_BODY (STMT_EXPR_STMT (t));
2604 /* Find the last statement in the chain, ignoring the final
2605 * scope statement */
2606 while (TREE_CHAIN (t) != NULL_TREE
2607 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2608 t=TREE_CHAIN (t);
2609 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2611 return tree_expr_nonnegative_p (t);
2614 /* Return a tree for the difference of pointers OP0 and OP1.
2615 The resulting tree has type int. */
2617 static tree
2618 pointer_diff (op0, op1)
2619 tree op0, op1;
2621 tree result, folded;
2622 tree restype = ptrdiff_type_node;
2624 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2625 tree con0, con1, lit0, lit1;
2626 tree orig_op1 = op1;
2628 if (pedantic || warn_pointer_arith)
2630 if (TREE_CODE (target_type) == VOID_TYPE)
2631 pedwarn ("pointer of type `void *' used in subtraction");
2632 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2633 pedwarn ("pointer to a function used in subtraction");
2636 /* If the conversion to ptrdiff_type does anything like widening or
2637 converting a partial to an integral mode, we get a convert_expression
2638 that is in the way to do any simplifications.
2639 (fold-const.c doesn't know that the extra bits won't be needed.
2640 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2641 different mode in place.)
2642 So first try to find a common term here 'by hand'; we want to cover
2643 at least the cases that occur in legal static initializers. */
2644 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2645 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2647 if (TREE_CODE (con0) == PLUS_EXPR)
2649 lit0 = TREE_OPERAND (con0, 1);
2650 con0 = TREE_OPERAND (con0, 0);
2652 else
2653 lit0 = integer_zero_node;
2655 if (TREE_CODE (con1) == PLUS_EXPR)
2657 lit1 = TREE_OPERAND (con1, 1);
2658 con1 = TREE_OPERAND (con1, 0);
2660 else
2661 lit1 = integer_zero_node;
2663 if (operand_equal_p (con0, con1, 0))
2665 op0 = lit0;
2666 op1 = lit1;
2670 /* First do the subtraction as integers;
2671 then drop through to build the divide operator.
2672 Do not do default conversions on the minus operator
2673 in case restype is a short type. */
2675 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2676 convert (restype, op1), 0);
2677 /* This generates an error if op1 is pointer to incomplete type. */
2678 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2679 error ("arithmetic on pointer to an incomplete type");
2681 /* This generates an error if op0 is pointer to incomplete type. */
2682 op1 = c_size_in_bytes (target_type);
2684 /* Divide by the size, in easiest possible way. */
2686 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2688 folded = fold (result);
2689 if (folded == result)
2690 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2691 return folded;
2694 /* Construct and perhaps optimize a tree representation
2695 for a unary operation. CODE, a tree_code, specifies the operation
2696 and XARG is the operand.
2697 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2698 the default promotions (such as from short to int).
2699 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2700 allows non-lvalues; this is only used to handle conversion of non-lvalue
2701 arrays to pointers in C99. */
2703 tree
2704 build_unary_op (code, xarg, flag)
2705 enum tree_code code;
2706 tree xarg;
2707 int flag;
2709 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2710 tree arg = xarg;
2711 tree argtype = 0;
2712 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2713 tree val;
2714 int noconvert = flag;
2716 if (typecode == ERROR_MARK)
2717 return error_mark_node;
2718 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2719 typecode = INTEGER_TYPE;
2721 switch (code)
2723 case CONVERT_EXPR:
2724 /* This is used for unary plus, because a CONVERT_EXPR
2725 is enough to prevent anybody from looking inside for
2726 associativity, but won't generate any code. */
2727 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2728 || typecode == COMPLEX_TYPE))
2730 error ("wrong type argument to unary plus");
2731 return error_mark_node;
2733 else if (!noconvert)
2734 arg = default_conversion (arg);
2735 arg = non_lvalue (arg);
2736 break;
2738 case NEGATE_EXPR:
2739 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2740 || typecode == COMPLEX_TYPE
2741 || typecode == VECTOR_TYPE))
2743 error ("wrong type argument to unary minus");
2744 return error_mark_node;
2746 else if (!noconvert)
2747 arg = default_conversion (arg);
2748 break;
2750 case BIT_NOT_EXPR:
2751 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2753 if (!noconvert)
2754 arg = default_conversion (arg);
2756 else if (typecode == COMPLEX_TYPE)
2758 code = CONJ_EXPR;
2759 if (pedantic)
2760 pedwarn ("ISO C does not support `~' for complex conjugation");
2761 if (!noconvert)
2762 arg = default_conversion (arg);
2764 else
2766 error ("wrong type argument to bit-complement");
2767 return error_mark_node;
2769 break;
2771 case ABS_EXPR:
2772 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2773 || typecode == COMPLEX_TYPE))
2775 error ("wrong type argument to abs");
2776 return error_mark_node;
2778 else if (!noconvert)
2779 arg = default_conversion (arg);
2780 break;
2782 case CONJ_EXPR:
2783 /* Conjugating a real value is a no-op, but allow it anyway. */
2784 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2785 || typecode == COMPLEX_TYPE))
2787 error ("wrong type argument to conjugation");
2788 return error_mark_node;
2790 else if (!noconvert)
2791 arg = default_conversion (arg);
2792 break;
2794 case TRUTH_NOT_EXPR:
2795 if (typecode != INTEGER_TYPE
2796 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2797 && typecode != COMPLEX_TYPE
2798 /* These will convert to a pointer. */
2799 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2801 error ("wrong type argument to unary exclamation mark");
2802 return error_mark_node;
2804 arg = c_common_truthvalue_conversion (arg);
2805 return invert_truthvalue (arg);
2807 case NOP_EXPR:
2808 break;
2810 case REALPART_EXPR:
2811 if (TREE_CODE (arg) == COMPLEX_CST)
2812 return TREE_REALPART (arg);
2813 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2814 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2815 else
2816 return arg;
2818 case IMAGPART_EXPR:
2819 if (TREE_CODE (arg) == COMPLEX_CST)
2820 return TREE_IMAGPART (arg);
2821 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2822 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2823 else
2824 return convert (TREE_TYPE (arg), integer_zero_node);
2826 case PREINCREMENT_EXPR:
2827 case POSTINCREMENT_EXPR:
2828 case PREDECREMENT_EXPR:
2829 case POSTDECREMENT_EXPR:
2830 /* Handle complex lvalues (when permitted)
2831 by reduction to simpler cases. */
2833 val = unary_complex_lvalue (code, arg, 0);
2834 if (val != 0)
2835 return val;
2837 /* Increment or decrement the real part of the value,
2838 and don't change the imaginary part. */
2839 if (typecode == COMPLEX_TYPE)
2841 tree real, imag;
2843 if (pedantic)
2844 pedwarn ("ISO C does not support `++' and `--' on complex types");
2846 arg = stabilize_reference (arg);
2847 real = build_unary_op (REALPART_EXPR, arg, 1);
2848 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2849 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2850 build_unary_op (code, real, 1), imag);
2853 /* Report invalid types. */
2855 if (typecode != POINTER_TYPE
2856 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2858 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2859 error ("wrong type argument to increment");
2860 else
2861 error ("wrong type argument to decrement");
2863 return error_mark_node;
2867 tree inc;
2868 tree result_type = TREE_TYPE (arg);
2870 arg = get_unwidened (arg, 0);
2871 argtype = TREE_TYPE (arg);
2873 /* Compute the increment. */
2875 if (typecode == POINTER_TYPE)
2877 /* If pointer target is an undefined struct,
2878 we just cannot know how to do the arithmetic. */
2879 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2881 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2882 error ("increment of pointer to unknown structure");
2883 else
2884 error ("decrement of pointer to unknown structure");
2886 else if ((pedantic || warn_pointer_arith)
2887 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2888 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2890 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2891 pedwarn ("wrong type argument to increment");
2892 else
2893 pedwarn ("wrong type argument to decrement");
2896 inc = c_size_in_bytes (TREE_TYPE (result_type));
2898 else
2899 inc = integer_one_node;
2901 inc = convert (argtype, inc);
2903 /* Handle incrementing a cast-expression. */
2905 while (1)
2906 switch (TREE_CODE (arg))
2908 case NOP_EXPR:
2909 case CONVERT_EXPR:
2910 case FLOAT_EXPR:
2911 case FIX_TRUNC_EXPR:
2912 case FIX_FLOOR_EXPR:
2913 case FIX_ROUND_EXPR:
2914 case FIX_CEIL_EXPR:
2915 pedantic_lvalue_warning (CONVERT_EXPR);
2916 /* If the real type has the same machine representation
2917 as the type it is cast to, we can make better output
2918 by adding directly to the inside of the cast. */
2919 if ((TREE_CODE (TREE_TYPE (arg))
2920 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2921 && (TYPE_MODE (TREE_TYPE (arg))
2922 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2923 arg = TREE_OPERAND (arg, 0);
2924 else
2926 tree incremented, modify, value;
2927 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2928 value = boolean_increment (code, arg);
2929 else
2931 arg = stabilize_reference (arg);
2932 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2933 value = arg;
2934 else
2935 value = save_expr (arg);
2936 incremented = build (((code == PREINCREMENT_EXPR
2937 || code == POSTINCREMENT_EXPR)
2938 ? PLUS_EXPR : MINUS_EXPR),
2939 argtype, value, inc);
2940 TREE_SIDE_EFFECTS (incremented) = 1;
2941 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2942 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2944 TREE_USED (value) = 1;
2945 return value;
2947 break;
2949 default:
2950 goto give_up;
2952 give_up:
2954 /* Complain about anything else that is not a true lvalue. */
2955 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2956 || code == POSTINCREMENT_EXPR)
2957 ? "invalid lvalue in increment"
2958 : "invalid lvalue in decrement")))
2959 return error_mark_node;
2961 /* Report a read-only lvalue. */
2962 if (TREE_READONLY (arg))
2963 readonly_warning (arg,
2964 ((code == PREINCREMENT_EXPR
2965 || code == POSTINCREMENT_EXPR)
2966 ? "increment" : "decrement"));
2968 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2969 val = boolean_increment (code, arg);
2970 else
2971 val = build (code, TREE_TYPE (arg), arg, inc);
2972 TREE_SIDE_EFFECTS (val) = 1;
2973 val = convert (result_type, val);
2974 if (TREE_CODE (val) != code)
2975 TREE_NO_UNUSED_WARNING (val) = 1;
2976 return val;
2979 case ADDR_EXPR:
2980 /* Note that this operation never does default_conversion. */
2982 /* Let &* cancel out to simplify resulting code. */
2983 if (TREE_CODE (arg) == INDIRECT_REF)
2985 /* Don't let this be an lvalue. */
2986 if (lvalue_p (TREE_OPERAND (arg, 0)))
2987 return non_lvalue (TREE_OPERAND (arg, 0));
2988 return TREE_OPERAND (arg, 0);
2991 /* For &x[y], return x+y */
2992 if (TREE_CODE (arg) == ARRAY_REF)
2994 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2995 return error_mark_node;
2996 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2997 TREE_OPERAND (arg, 1), 1);
3000 /* Handle complex lvalues (when permitted)
3001 by reduction to simpler cases. */
3002 val = unary_complex_lvalue (code, arg, flag);
3003 if (val != 0)
3004 return val;
3006 #if 0 /* Turned off because inconsistent;
3007 float f; *&(int)f = 3.4 stores in int format
3008 whereas (int)f = 3.4 stores in float format. */
3009 /* Address of a cast is just a cast of the address
3010 of the operand of the cast. */
3011 switch (TREE_CODE (arg))
3013 case NOP_EXPR:
3014 case CONVERT_EXPR:
3015 case FLOAT_EXPR:
3016 case FIX_TRUNC_EXPR:
3017 case FIX_FLOOR_EXPR:
3018 case FIX_ROUND_EXPR:
3019 case FIX_CEIL_EXPR:
3020 if (pedantic)
3021 pedwarn ("ISO C forbids the address of a cast expression");
3022 return convert (build_pointer_type (TREE_TYPE (arg)),
3023 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3024 0));
3026 #endif
3028 /* Anything not already handled and not a true memory reference
3029 or a non-lvalue array is an error. */
3030 else if (typecode != FUNCTION_TYPE && !flag
3031 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3032 return error_mark_node;
3034 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3035 argtype = TREE_TYPE (arg);
3037 /* If the lvalue is const or volatile, merge that into the type
3038 to which the address will point. Note that you can't get a
3039 restricted pointer by taking the address of something, so we
3040 only have to deal with `const' and `volatile' here. */
3041 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3042 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3043 argtype = c_build_type_variant (argtype,
3044 TREE_READONLY (arg),
3045 TREE_THIS_VOLATILE (arg));
3047 argtype = build_pointer_type (argtype);
3049 if (!c_mark_addressable (arg))
3050 return error_mark_node;
3053 tree addr;
3055 if (TREE_CODE (arg) == COMPONENT_REF)
3057 tree field = TREE_OPERAND (arg, 1);
3059 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
3061 if (DECL_C_BIT_FIELD (field))
3063 error ("attempt to take address of bit-field structure member `%s'",
3064 IDENTIFIER_POINTER (DECL_NAME (field)));
3065 return error_mark_node;
3068 addr = fold (build (PLUS_EXPR, argtype,
3069 convert (argtype, addr),
3070 convert (argtype, byte_position (field))));
3072 else
3073 addr = build1 (code, argtype, arg);
3075 /* Address of a static or external variable or
3076 file-scope function counts as a constant. */
3077 if (staticp (arg)
3078 && ! (TREE_CODE (arg) == FUNCTION_DECL
3079 && DECL_CONTEXT (arg) != 0))
3080 TREE_CONSTANT (addr) = 1;
3081 return addr;
3084 default:
3085 break;
3088 if (argtype == 0)
3089 argtype = TREE_TYPE (arg);
3090 return fold (build1 (code, argtype, arg));
3093 #if 0
3094 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3095 convert ARG with the same conversions in the same order
3096 and return the result. */
3098 static tree
3099 convert_sequence (conversions, arg)
3100 tree conversions;
3101 tree arg;
3103 switch (TREE_CODE (conversions))
3105 case NOP_EXPR:
3106 case CONVERT_EXPR:
3107 case FLOAT_EXPR:
3108 case FIX_TRUNC_EXPR:
3109 case FIX_FLOOR_EXPR:
3110 case FIX_ROUND_EXPR:
3111 case FIX_CEIL_EXPR:
3112 return convert (TREE_TYPE (conversions),
3113 convert_sequence (TREE_OPERAND (conversions, 0),
3114 arg));
3116 default:
3117 return arg;
3120 #endif /* 0 */
3122 /* Return nonzero if REF is an lvalue valid for this language.
3123 Lvalues can be assigned, unless their type has TYPE_READONLY.
3124 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3127 lvalue_p (ref)
3128 tree ref;
3130 enum tree_code code = TREE_CODE (ref);
3132 switch (code)
3134 case REALPART_EXPR:
3135 case IMAGPART_EXPR:
3136 case COMPONENT_REF:
3137 return lvalue_p (TREE_OPERAND (ref, 0));
3139 case COMPOUND_LITERAL_EXPR:
3140 case STRING_CST:
3141 return 1;
3143 case INDIRECT_REF:
3144 case ARRAY_REF:
3145 case VAR_DECL:
3146 case PARM_DECL:
3147 case RESULT_DECL:
3148 case ERROR_MARK:
3149 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3150 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3152 case BIND_EXPR:
3153 case RTL_EXPR:
3154 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3156 default:
3157 return 0;
3161 /* Return nonzero if REF is an lvalue valid for this language;
3162 otherwise, print an error message and return zero. */
3165 lvalue_or_else (ref, msgid)
3166 tree ref;
3167 const char *msgid;
3169 int win = lvalue_p (ref);
3171 if (! win)
3172 error ("%s", msgid);
3174 return win;
3177 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3178 for certain kinds of expressions which are not really lvalues
3179 but which we can accept as lvalues. If FLAG is nonzero, then
3180 non-lvalues are OK since we may be converting a non-lvalue array to
3181 a pointer in C99.
3183 If ARG is not a kind of expression we can handle, return zero. */
3185 static tree
3186 unary_complex_lvalue (code, arg, flag)
3187 enum tree_code code;
3188 tree arg;
3189 int flag;
3191 /* Handle (a, b) used as an "lvalue". */
3192 if (TREE_CODE (arg) == COMPOUND_EXPR)
3194 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3196 /* If this returns a function type, it isn't really being used as
3197 an lvalue, so don't issue a warning about it. */
3198 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3199 pedantic_lvalue_warning (COMPOUND_EXPR);
3201 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3202 TREE_OPERAND (arg, 0), real_result);
3205 /* Handle (a ? b : c) used as an "lvalue". */
3206 if (TREE_CODE (arg) == COND_EXPR)
3208 if (!flag)
3209 pedantic_lvalue_warning (COND_EXPR);
3210 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3211 pedantic_lvalue_warning (COMPOUND_EXPR);
3213 return (build_conditional_expr
3214 (TREE_OPERAND (arg, 0),
3215 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3216 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3219 return 0;
3222 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3223 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3225 static void
3226 pedantic_lvalue_warning (code)
3227 enum tree_code code;
3229 if (pedantic)
3230 switch (code)
3232 case COND_EXPR:
3233 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3234 break;
3235 case COMPOUND_EXPR:
3236 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3237 break;
3238 default:
3239 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3240 break;
3244 /* Warn about storing in something that is `const'. */
3246 void
3247 readonly_warning (arg, msgid)
3248 tree arg;
3249 const char *msgid;
3251 if (TREE_CODE (arg) == COMPONENT_REF)
3253 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3254 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3255 else
3256 pedwarn ("%s of read-only member `%s'", _(msgid),
3257 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3259 else if (TREE_CODE (arg) == VAR_DECL)
3260 pedwarn ("%s of read-only variable `%s'", _(msgid),
3261 IDENTIFIER_POINTER (DECL_NAME (arg)));
3262 else
3263 pedwarn ("%s of read-only location", _(msgid));
3266 /* Mark EXP saying that we need to be able to take the
3267 address of it; it should not be allocated in a register.
3268 Returns true if successful. */
3270 bool
3271 c_mark_addressable (exp)
3272 tree exp;
3274 tree x = exp;
3276 while (1)
3277 switch (TREE_CODE (x))
3279 case COMPONENT_REF:
3280 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3282 error ("cannot take address of bit-field `%s'",
3283 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3284 return false;
3287 /* ... fall through ... */
3289 case ADDR_EXPR:
3290 case ARRAY_REF:
3291 case REALPART_EXPR:
3292 case IMAGPART_EXPR:
3293 x = TREE_OPERAND (x, 0);
3294 break;
3296 case COMPOUND_LITERAL_EXPR:
3297 case CONSTRUCTOR:
3298 TREE_ADDRESSABLE (x) = 1;
3299 return true;
3301 case VAR_DECL:
3302 case CONST_DECL:
3303 case PARM_DECL:
3304 case RESULT_DECL:
3305 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3306 && DECL_NONLOCAL (x))
3308 if (TREE_PUBLIC (x))
3310 error ("global register variable `%s' used in nested function",
3311 IDENTIFIER_POINTER (DECL_NAME (x)));
3312 return false;
3314 pedwarn ("register variable `%s' used in nested function",
3315 IDENTIFIER_POINTER (DECL_NAME (x)));
3317 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3319 if (TREE_PUBLIC (x))
3321 error ("address of global register variable `%s' requested",
3322 IDENTIFIER_POINTER (DECL_NAME (x)));
3323 return false;
3326 /* If we are making this addressable due to its having
3327 volatile components, give a different error message. Also
3328 handle the case of an unnamed parameter by not trying
3329 to give the name. */
3331 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3333 error ("cannot put object with volatile field into register");
3334 return false;
3337 pedwarn ("address of register variable `%s' requested",
3338 IDENTIFIER_POINTER (DECL_NAME (x)));
3340 put_var_into_stack (x);
3342 /* drops in */
3343 case FUNCTION_DECL:
3344 TREE_ADDRESSABLE (x) = 1;
3345 #if 0 /* poplevel deals with this now. */
3346 if (DECL_CONTEXT (x) == 0)
3347 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3348 #endif
3350 default:
3351 return true;
3355 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3357 tree
3358 build_conditional_expr (ifexp, op1, op2)
3359 tree ifexp, op1, op2;
3361 tree type1;
3362 tree type2;
3363 enum tree_code code1;
3364 enum tree_code code2;
3365 tree result_type = NULL;
3366 tree orig_op1 = op1, orig_op2 = op2;
3368 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
3370 #if 0 /* Produces wrong result if within sizeof. */
3371 /* Don't promote the operands separately if they promote
3372 the same way. Return the unpromoted type and let the combined
3373 value get promoted if necessary. */
3375 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3376 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3377 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3378 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3380 if (TREE_CODE (ifexp) == INTEGER_CST)
3381 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3383 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3385 #endif
3387 /* Promote both alternatives. */
3389 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3390 op1 = default_conversion (op1);
3391 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3392 op2 = default_conversion (op2);
3394 if (TREE_CODE (ifexp) == ERROR_MARK
3395 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3396 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3397 return error_mark_node;
3399 type1 = TREE_TYPE (op1);
3400 code1 = TREE_CODE (type1);
3401 type2 = TREE_TYPE (op2);
3402 code2 = TREE_CODE (type2);
3404 /* Quickly detect the usual case where op1 and op2 have the same type
3405 after promotion. */
3406 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3408 if (type1 == type2)
3409 result_type = type1;
3410 else
3411 result_type = TYPE_MAIN_VARIANT (type1);
3413 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3414 || code1 == COMPLEX_TYPE)
3415 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3416 || code2 == COMPLEX_TYPE))
3418 result_type = common_type (type1, type2);
3420 /* If -Wsign-compare, warn here if type1 and type2 have
3421 different signedness. We'll promote the signed to unsigned
3422 and later code won't know it used to be different.
3423 Do this check on the original types, so that explicit casts
3424 will be considered, but default promotions won't. */
3425 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3426 && !skip_evaluation)
3428 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3429 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3431 if (unsigned_op1 ^ unsigned_op2)
3433 /* Do not warn if the result type is signed, since the
3434 signed type will only be chosen if it can represent
3435 all the values of the unsigned type. */
3436 if (! TREE_UNSIGNED (result_type))
3437 /* OK */;
3438 /* Do not warn if the signed quantity is an unsuffixed
3439 integer literal (or some static constant expression
3440 involving such literals) and it is non-negative. */
3441 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
3442 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
3443 /* OK */;
3444 else
3445 warning ("signed and unsigned type in conditional expression");
3449 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3451 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3452 pedwarn ("ISO C forbids conditional expr with only one void side");
3453 result_type = void_type_node;
3455 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3457 if (comp_target_types (type1, type2, 1))
3458 result_type = common_type (type1, type2);
3459 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3460 && TREE_CODE (orig_op1) != NOP_EXPR)
3461 result_type = qualify_type (type2, type1);
3462 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3463 && TREE_CODE (orig_op2) != NOP_EXPR)
3464 result_type = qualify_type (type1, type2);
3465 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3467 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3468 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3469 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3470 TREE_TYPE (type2)));
3472 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3474 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3475 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3476 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3477 TREE_TYPE (type1)));
3479 else
3481 pedwarn ("pointer type mismatch in conditional expression");
3482 result_type = build_pointer_type (void_type_node);
3485 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3487 if (! integer_zerop (op2))
3488 pedwarn ("pointer/integer type mismatch in conditional expression");
3489 else
3491 op2 = null_pointer_node;
3493 result_type = type1;
3495 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3497 if (!integer_zerop (op1))
3498 pedwarn ("pointer/integer type mismatch in conditional expression");
3499 else
3501 op1 = null_pointer_node;
3503 result_type = type2;
3506 if (!result_type)
3508 if (flag_cond_mismatch)
3509 result_type = void_type_node;
3510 else
3512 error ("type mismatch in conditional expression");
3513 return error_mark_node;
3517 /* Merge const and volatile flags of the incoming types. */
3518 result_type
3519 = build_type_variant (result_type,
3520 TREE_READONLY (op1) || TREE_READONLY (op2),
3521 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3523 if (result_type != TREE_TYPE (op1))
3524 op1 = convert_and_check (result_type, op1);
3525 if (result_type != TREE_TYPE (op2))
3526 op2 = convert_and_check (result_type, op2);
3528 if (TREE_CODE (ifexp) == INTEGER_CST)
3529 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3531 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3534 /* Given a list of expressions, return a compound expression
3535 that performs them all and returns the value of the last of them. */
3537 tree
3538 build_compound_expr (list)
3539 tree list;
3541 return internal_build_compound_expr (list, TRUE);
3544 static tree
3545 internal_build_compound_expr (list, first_p)
3546 tree list;
3547 int first_p;
3549 tree rest;
3551 if (TREE_CHAIN (list) == 0)
3553 /* Convert arrays and functions to pointers when there
3554 really is a comma operator. */
3555 if (!first_p)
3556 TREE_VALUE (list)
3557 = default_function_array_conversion (TREE_VALUE (list));
3559 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3560 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3562 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3563 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3564 list = TREE_OPERAND (list, 0);
3565 #endif
3567 /* Don't let (0, 0) be null pointer constant. */
3568 if (!first_p && integer_zerop (TREE_VALUE (list)))
3569 return non_lvalue (TREE_VALUE (list));
3570 return TREE_VALUE (list);
3573 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3575 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3577 /* The left-hand operand of a comma expression is like an expression
3578 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3579 any side-effects, unless it was explicitly cast to (void). */
3580 if ((extra_warnings || warn_unused_value)
3581 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3582 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3583 warning ("left-hand operand of comma expression has no effect");
3585 /* When pedantic, a compound expression can be neither an lvalue
3586 nor an integer constant expression. */
3587 if (! pedantic)
3588 return rest;
3591 /* With -Wunused, we should also warn if the left-hand operand does have
3592 side-effects, but computes a value which is not used. For example, in
3593 `foo() + bar(), baz()' the result of the `+' operator is not used,
3594 so we should issue a warning. */
3595 else if (warn_unused_value)
3596 warn_if_unused_value (TREE_VALUE (list));
3598 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3601 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3603 tree
3604 build_c_cast (type, expr)
3605 tree type;
3606 tree expr;
3608 tree value = expr;
3610 if (type == error_mark_node || expr == error_mark_node)
3611 return error_mark_node;
3613 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3614 only in <protocol> qualifications. But when constructing cast expressions,
3615 the protocols do matter and must be kept around. */
3616 if (!flag_objc || !objc_is_id (type))
3617 type = TYPE_MAIN_VARIANT (type);
3619 #if 0
3620 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3621 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3622 value = TREE_OPERAND (value, 0);
3623 #endif
3625 if (TREE_CODE (type) == ARRAY_TYPE)
3627 error ("cast specifies array type");
3628 return error_mark_node;
3631 if (TREE_CODE (type) == FUNCTION_TYPE)
3633 error ("cast specifies function type");
3634 return error_mark_node;
3637 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3639 if (pedantic)
3641 if (TREE_CODE (type) == RECORD_TYPE
3642 || TREE_CODE (type) == UNION_TYPE)
3643 pedwarn ("ISO C forbids casting nonscalar to the same type");
3646 else if (TREE_CODE (type) == UNION_TYPE)
3648 tree field;
3649 value = default_function_array_conversion (value);
3651 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3652 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3653 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3654 break;
3656 if (field)
3658 tree t;
3660 if (pedantic)
3661 pedwarn ("ISO C forbids casts to union type");
3662 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3663 build_tree_list (field, value)), 0);
3664 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3665 return t;
3667 error ("cast to union type from type not present in union");
3668 return error_mark_node;
3670 else
3672 tree otype, ovalue;
3674 /* If casting to void, avoid the error that would come
3675 from default_conversion in the case of a non-lvalue array. */
3676 if (type == void_type_node)
3677 return build1 (CONVERT_EXPR, type, value);
3679 /* Convert functions and arrays to pointers,
3680 but don't convert any other types. */
3681 value = default_function_array_conversion (value);
3682 otype = TREE_TYPE (value);
3684 /* Optionally warn about potentially worrisome casts. */
3686 if (warn_cast_qual
3687 && TREE_CODE (type) == POINTER_TYPE
3688 && TREE_CODE (otype) == POINTER_TYPE)
3690 tree in_type = type;
3691 tree in_otype = otype;
3692 int added = 0;
3693 int discarded = 0;
3695 /* Check that the qualifiers on IN_TYPE are a superset of
3696 the qualifiers of IN_OTYPE. The outermost level of
3697 POINTER_TYPE nodes is uninteresting and we stop as soon
3698 as we hit a non-POINTER_TYPE node on either type. */
3701 in_otype = TREE_TYPE (in_otype);
3702 in_type = TREE_TYPE (in_type);
3704 /* GNU C allows cv-qualified function types. 'const'
3705 means the function is very pure, 'volatile' means it
3706 can't return. We need to warn when such qualifiers
3707 are added, not when they're taken away. */
3708 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3709 && TREE_CODE (in_type) == FUNCTION_TYPE)
3710 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3711 else
3712 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3714 while (TREE_CODE (in_type) == POINTER_TYPE
3715 && TREE_CODE (in_otype) == POINTER_TYPE);
3717 if (added)
3718 warning ("cast adds new qualifiers to function type");
3720 if (discarded)
3721 /* There are qualifiers present in IN_OTYPE that are not
3722 present in IN_TYPE. */
3723 warning ("cast discards qualifiers from pointer target type");
3726 /* Warn about possible alignment problems. */
3727 if (STRICT_ALIGNMENT && warn_cast_align
3728 && TREE_CODE (type) == POINTER_TYPE
3729 && TREE_CODE (otype) == POINTER_TYPE
3730 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3731 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3732 /* Don't warn about opaque types, where the actual alignment
3733 restriction is unknown. */
3734 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3735 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3736 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3737 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3738 warning ("cast increases required alignment of target type");
3740 if (TREE_CODE (type) == INTEGER_TYPE
3741 && TREE_CODE (otype) == POINTER_TYPE
3742 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3743 && !TREE_CONSTANT (value))
3744 warning ("cast from pointer to integer of different size");
3746 if (warn_bad_function_cast
3747 && TREE_CODE (value) == CALL_EXPR
3748 && TREE_CODE (type) != TREE_CODE (otype))
3749 warning ("cast does not match function type");
3751 if (TREE_CODE (type) == POINTER_TYPE
3752 && TREE_CODE (otype) == INTEGER_TYPE
3753 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3754 /* Don't warn about converting any constant. */
3755 && !TREE_CONSTANT (value))
3756 warning ("cast to pointer from integer of different size");
3758 if (TREE_CODE (type) == POINTER_TYPE
3759 && TREE_CODE (otype) == POINTER_TYPE
3760 && TREE_CODE (expr) == ADDR_EXPR
3761 && DECL_P (TREE_OPERAND (expr, 0))
3762 && flag_strict_aliasing && warn_strict_aliasing
3763 && !VOID_TYPE_P (TREE_TYPE (type)))
3765 /* Casting the address of a decl to non void pointer. Warn
3766 if the cast breaks type based aliasing. */
3767 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3768 warning ("type-punning to incomplete type might break strict-aliasing rules");
3769 else if (!alias_sets_conflict_p
3770 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3771 get_alias_set (TREE_TYPE (type))))
3772 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3775 ovalue = value;
3776 /* Replace a nonvolatile const static variable with its value. */
3777 if (optimize && TREE_CODE (value) == VAR_DECL)
3778 value = decl_constant_value (value);
3779 value = convert (type, value);
3781 /* Ignore any integer overflow caused by the cast. */
3782 if (TREE_CODE (value) == INTEGER_CST)
3784 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3785 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3789 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3790 if (pedantic && TREE_CODE (value) == INTEGER_CST
3791 && TREE_CODE (expr) == INTEGER_CST
3792 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3793 value = non_lvalue (value);
3795 /* If pedantic, don't let a cast be an lvalue. */
3796 if (value == expr && pedantic)
3797 value = non_lvalue (value);
3799 return value;
3802 /* Interpret a cast of expression EXPR to type TYPE. */
3803 tree
3804 c_cast_expr (type, expr)
3805 tree type, expr;
3807 int saved_wsp = warn_strict_prototypes;
3809 /* This avoids warnings about unprototyped casts on
3810 integers. E.g. "#define SIG_DFL (void(*)())0". */
3811 if (TREE_CODE (expr) == INTEGER_CST)
3812 warn_strict_prototypes = 0;
3813 type = groktypename (type);
3814 warn_strict_prototypes = saved_wsp;
3816 return build_c_cast (type, expr);
3820 /* Build an assignment expression of lvalue LHS from value RHS.
3821 MODIFYCODE is the code for a binary operator that we use
3822 to combine the old value of LHS with RHS to get the new value.
3823 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3825 tree
3826 build_modify_expr (lhs, modifycode, rhs)
3827 tree lhs, rhs;
3828 enum tree_code modifycode;
3830 tree result;
3831 tree newrhs;
3832 tree lhstype = TREE_TYPE (lhs);
3833 tree olhstype = lhstype;
3835 /* Types that aren't fully specified cannot be used in assignments. */
3836 lhs = require_complete_type (lhs);
3838 /* Avoid duplicate error messages from operands that had errors. */
3839 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3840 return error_mark_node;
3842 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3843 /* Do not use STRIP_NOPS here. We do not want an enumerator
3844 whose value is 0 to count as a null pointer constant. */
3845 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3846 rhs = TREE_OPERAND (rhs, 0);
3848 newrhs = rhs;
3850 /* Handle control structure constructs used as "lvalues". */
3852 switch (TREE_CODE (lhs))
3854 /* Handle (a, b) used as an "lvalue". */
3855 case COMPOUND_EXPR:
3856 pedantic_lvalue_warning (COMPOUND_EXPR);
3857 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3858 if (TREE_CODE (newrhs) == ERROR_MARK)
3859 return error_mark_node;
3860 return build (COMPOUND_EXPR, lhstype,
3861 TREE_OPERAND (lhs, 0), newrhs);
3863 /* Handle (a ? b : c) used as an "lvalue". */
3864 case COND_EXPR:
3865 pedantic_lvalue_warning (COND_EXPR);
3866 rhs = save_expr (rhs);
3868 /* Produce (a ? (b = rhs) : (c = rhs))
3869 except that the RHS goes through a save-expr
3870 so the code to compute it is only emitted once. */
3871 tree cond
3872 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3873 build_modify_expr (TREE_OPERAND (lhs, 1),
3874 modifycode, rhs),
3875 build_modify_expr (TREE_OPERAND (lhs, 2),
3876 modifycode, rhs));
3877 if (TREE_CODE (cond) == ERROR_MARK)
3878 return cond;
3879 /* Make sure the code to compute the rhs comes out
3880 before the split. */
3881 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3882 /* But cast it to void to avoid an "unused" error. */
3883 convert (void_type_node, rhs), cond);
3885 default:
3886 break;
3889 /* If a binary op has been requested, combine the old LHS value with the RHS
3890 producing the value we should actually store into the LHS. */
3892 if (modifycode != NOP_EXPR)
3894 lhs = stabilize_reference (lhs);
3895 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3898 /* Handle a cast used as an "lvalue".
3899 We have already performed any binary operator using the value as cast.
3900 Now convert the result to the cast type of the lhs,
3901 and then true type of the lhs and store it there;
3902 then convert result back to the cast type to be the value
3903 of the assignment. */
3905 switch (TREE_CODE (lhs))
3907 case NOP_EXPR:
3908 case CONVERT_EXPR:
3909 case FLOAT_EXPR:
3910 case FIX_TRUNC_EXPR:
3911 case FIX_FLOOR_EXPR:
3912 case FIX_ROUND_EXPR:
3913 case FIX_CEIL_EXPR:
3914 newrhs = default_function_array_conversion (newrhs);
3916 tree inner_lhs = TREE_OPERAND (lhs, 0);
3917 tree result;
3918 result = build_modify_expr (inner_lhs, NOP_EXPR,
3919 convert (TREE_TYPE (inner_lhs),
3920 convert (lhstype, newrhs)));
3921 if (TREE_CODE (result) == ERROR_MARK)
3922 return result;
3923 pedantic_lvalue_warning (CONVERT_EXPR);
3924 return convert (TREE_TYPE (lhs), result);
3927 default:
3928 break;
3931 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3932 Reject anything strange now. */
3934 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3935 return error_mark_node;
3937 /* Warn about storing in something that is `const'. */
3939 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3940 || ((TREE_CODE (lhstype) == RECORD_TYPE
3941 || TREE_CODE (lhstype) == UNION_TYPE)
3942 && C_TYPE_FIELDS_READONLY (lhstype)))
3943 readonly_warning (lhs, "assignment");
3945 /* If storing into a structure or union member,
3946 it has probably been given type `int'.
3947 Compute the type that would go with
3948 the actual amount of storage the member occupies. */
3950 if (TREE_CODE (lhs) == COMPONENT_REF
3951 && (TREE_CODE (lhstype) == INTEGER_TYPE
3952 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3953 || TREE_CODE (lhstype) == REAL_TYPE
3954 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3955 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3957 /* If storing in a field that is in actuality a short or narrower than one,
3958 we must store in the field in its actual type. */
3960 if (lhstype != TREE_TYPE (lhs))
3962 lhs = copy_node (lhs);
3963 TREE_TYPE (lhs) = lhstype;
3966 /* Convert new value to destination type. */
3968 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3969 NULL_TREE, NULL_TREE, 0);
3970 if (TREE_CODE (newrhs) == ERROR_MARK)
3971 return error_mark_node;
3973 /* Scan operands */
3975 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3976 TREE_SIDE_EFFECTS (result) = 1;
3978 /* If we got the LHS in a different type for storing in,
3979 convert the result back to the nominal type of LHS
3980 so that the value we return always has the same type
3981 as the LHS argument. */
3983 if (olhstype == TREE_TYPE (result))
3984 return result;
3985 return convert_for_assignment (olhstype, result, _("assignment"),
3986 NULL_TREE, NULL_TREE, 0);
3989 /* Convert value RHS to type TYPE as preparation for an assignment
3990 to an lvalue of type TYPE.
3991 The real work of conversion is done by `convert'.
3992 The purpose of this function is to generate error messages
3993 for assignments that are not allowed in C.
3994 ERRTYPE is a string to use in error messages:
3995 "assignment", "return", etc. If it is null, this is parameter passing
3996 for a function call (and different error messages are output).
3998 FUNNAME is the name of the function being called,
3999 as an IDENTIFIER_NODE, or null.
4000 PARMNUM is the number of the argument, for printing in error messages. */
4002 static tree
4003 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4004 tree type, rhs;
4005 const char *errtype;
4006 tree fundecl, funname;
4007 int parmnum;
4009 enum tree_code codel = TREE_CODE (type);
4010 tree rhstype;
4011 enum tree_code coder;
4013 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4014 /* Do not use STRIP_NOPS here. We do not want an enumerator
4015 whose value is 0 to count as a null pointer constant. */
4016 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4017 rhs = TREE_OPERAND (rhs, 0);
4019 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4020 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4021 rhs = default_conversion (rhs);
4022 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4023 rhs = decl_constant_value_for_broken_optimization (rhs);
4025 rhstype = TREE_TYPE (rhs);
4026 coder = TREE_CODE (rhstype);
4028 if (coder == ERROR_MARK)
4029 return error_mark_node;
4031 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4033 overflow_warning (rhs);
4034 /* Check for Objective-C protocols. This will automatically
4035 issue a warning if there are protocol violations. No need to
4036 use the return value. */
4037 if (flag_objc)
4038 objc_comptypes (type, rhstype, 0);
4039 return rhs;
4042 if (coder == VOID_TYPE)
4044 error ("void value not ignored as it ought to be");
4045 return error_mark_node;
4047 /* A type converts to a reference to it.
4048 This code doesn't fully support references, it's just for the
4049 special case of va_start and va_copy. */
4050 if (codel == REFERENCE_TYPE
4051 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4053 if (!lvalue_p (rhs))
4055 error ("cannot pass rvalue to reference parameter");
4056 return error_mark_node;
4058 if (!c_mark_addressable (rhs))
4059 return error_mark_node;
4060 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4062 /* We already know that these two types are compatible, but they
4063 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4064 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4065 likely to be va_list, a typedef to __builtin_va_list, which
4066 is different enough that it will cause problems later. */
4067 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4068 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4070 rhs = build1 (NOP_EXPR, type, rhs);
4071 return rhs;
4073 /* Some types can interconvert without explicit casts. */
4074 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4075 && ((*targetm.vector_opaque_p) (type)
4076 || (*targetm.vector_opaque_p) (rhstype)))
4077 return convert (type, rhs);
4078 /* Arithmetic types all interconvert, and enum is treated like int. */
4079 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4080 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4081 || codel == BOOLEAN_TYPE)
4082 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4083 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4084 || coder == BOOLEAN_TYPE))
4085 return convert_and_check (type, rhs);
4087 /* Conversion to a transparent union from its member types.
4088 This applies only to function arguments. */
4089 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4091 tree memb_types;
4092 tree marginal_memb_type = 0;
4094 for (memb_types = TYPE_FIELDS (type); memb_types;
4095 memb_types = TREE_CHAIN (memb_types))
4097 tree memb_type = TREE_TYPE (memb_types);
4099 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4100 TYPE_MAIN_VARIANT (rhstype)))
4101 break;
4103 if (TREE_CODE (memb_type) != POINTER_TYPE)
4104 continue;
4106 if (coder == POINTER_TYPE)
4108 tree ttl = TREE_TYPE (memb_type);
4109 tree ttr = TREE_TYPE (rhstype);
4111 /* Any non-function converts to a [const][volatile] void *
4112 and vice versa; otherwise, targets must be the same.
4113 Meanwhile, the lhs target must have all the qualifiers of
4114 the rhs. */
4115 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4116 || comp_target_types (memb_type, rhstype, 0))
4118 /* If this type won't generate any warnings, use it. */
4119 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4120 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4121 && TREE_CODE (ttl) == FUNCTION_TYPE)
4122 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4123 == TYPE_QUALS (ttr))
4124 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4125 == TYPE_QUALS (ttl))))
4126 break;
4128 /* Keep looking for a better type, but remember this one. */
4129 if (! marginal_memb_type)
4130 marginal_memb_type = memb_type;
4134 /* Can convert integer zero to any pointer type. */
4135 if (integer_zerop (rhs)
4136 || (TREE_CODE (rhs) == NOP_EXPR
4137 && integer_zerop (TREE_OPERAND (rhs, 0))))
4139 rhs = null_pointer_node;
4140 break;
4144 if (memb_types || marginal_memb_type)
4146 if (! memb_types)
4148 /* We have only a marginally acceptable member type;
4149 it needs a warning. */
4150 tree ttl = TREE_TYPE (marginal_memb_type);
4151 tree ttr = TREE_TYPE (rhstype);
4153 /* Const and volatile mean something different for function
4154 types, so the usual warnings are not appropriate. */
4155 if (TREE_CODE (ttr) == FUNCTION_TYPE
4156 && TREE_CODE (ttl) == FUNCTION_TYPE)
4158 /* Because const and volatile on functions are
4159 restrictions that say the function will not do
4160 certain things, it is okay to use a const or volatile
4161 function where an ordinary one is wanted, but not
4162 vice-versa. */
4163 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4164 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4165 errtype, funname, parmnum);
4167 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4168 warn_for_assignment ("%s discards qualifiers from pointer target type",
4169 errtype, funname,
4170 parmnum);
4173 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4174 pedwarn ("ISO C prohibits argument conversion to union type");
4176 return build1 (NOP_EXPR, type, rhs);
4180 /* Conversions among pointers */
4181 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4182 && (coder == codel))
4184 tree ttl = TREE_TYPE (type);
4185 tree ttr = TREE_TYPE (rhstype);
4187 /* Any non-function converts to a [const][volatile] void *
4188 and vice versa; otherwise, targets must be the same.
4189 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4190 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4191 || comp_target_types (type, rhstype, 0)
4192 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
4193 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4195 if (pedantic
4196 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4198 (VOID_TYPE_P (ttr)
4199 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4200 which are not ANSI null ptr constants. */
4201 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4202 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4203 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4204 errtype, funname, parmnum);
4205 /* Const and volatile mean something different for function types,
4206 so the usual warnings are not appropriate. */
4207 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4208 && TREE_CODE (ttl) != FUNCTION_TYPE)
4210 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4211 warn_for_assignment ("%s discards qualifiers from pointer target type",
4212 errtype, funname, parmnum);
4213 /* If this is not a case of ignoring a mismatch in signedness,
4214 no warning. */
4215 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4216 || comp_target_types (type, rhstype, 0))
4218 /* If there is a mismatch, do warn. */
4219 else if (pedantic)
4220 warn_for_assignment ("pointer targets in %s differ in signedness",
4221 errtype, funname, parmnum);
4223 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4224 && TREE_CODE (ttr) == FUNCTION_TYPE)
4226 /* Because const and volatile on functions are restrictions
4227 that say the function will not do certain things,
4228 it is okay to use a const or volatile function
4229 where an ordinary one is wanted, but not vice-versa. */
4230 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4231 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4232 errtype, funname, parmnum);
4235 else
4236 warn_for_assignment ("%s from incompatible pointer type",
4237 errtype, funname, parmnum);
4238 return convert (type, rhs);
4240 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4242 /* An explicit constant 0 can convert to a pointer,
4243 or one that results from arithmetic, even including
4244 a cast to integer type. */
4245 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4247 ! (TREE_CODE (rhs) == NOP_EXPR
4248 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4249 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4250 && integer_zerop (TREE_OPERAND (rhs, 0))))
4252 warn_for_assignment ("%s makes pointer from integer without a cast",
4253 errtype, funname, parmnum);
4254 return convert (type, rhs);
4256 return null_pointer_node;
4258 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4260 warn_for_assignment ("%s makes integer from pointer without a cast",
4261 errtype, funname, parmnum);
4262 return convert (type, rhs);
4264 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4265 return convert (type, rhs);
4267 if (!errtype)
4269 if (funname)
4271 tree selector = objc_message_selector ();
4273 if (selector && parmnum > 2)
4274 error ("incompatible type for argument %d of `%s'",
4275 parmnum - 2, IDENTIFIER_POINTER (selector));
4276 else
4277 error ("incompatible type for argument %d of `%s'",
4278 parmnum, IDENTIFIER_POINTER (funname));
4280 else
4281 error ("incompatible type for argument %d of indirect function call",
4282 parmnum);
4284 else
4285 error ("incompatible types in %s", errtype);
4287 return error_mark_node;
4290 /* Convert VALUE for assignment into inlined parameter PARM. */
4292 tree
4293 c_convert_parm_for_inlining (parm, value, fn)
4294 tree parm, value, fn;
4296 tree ret, type;
4298 /* If FN was prototyped, the value has been converted already
4299 in convert_arguments. */
4300 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4301 return value;
4303 type = TREE_TYPE (parm);
4304 ret = convert_for_assignment (type, value,
4305 (char *) 0 /* arg passing */, fn,
4306 DECL_NAME (fn), 0);
4307 if (PROMOTE_PROTOTYPES
4308 && INTEGRAL_TYPE_P (type)
4309 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4310 ret = default_conversion (ret);
4311 return ret;
4314 /* Print a warning using MSGID.
4315 It gets OPNAME as its one parameter.
4316 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
4317 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4318 FUNCTION and ARGNUM are handled specially if we are building an
4319 Objective-C selector. */
4321 static void
4322 warn_for_assignment (msgid, opname, function, argnum)
4323 const char *msgid;
4324 const char *opname;
4325 tree function;
4326 int argnum;
4328 if (opname == 0)
4330 tree selector = objc_message_selector ();
4331 char * new_opname;
4333 if (selector && argnum > 2)
4335 function = selector;
4336 argnum -= 2;
4338 if (argnum == 0)
4340 if (function)
4342 /* Function name is known; supply it. */
4343 const char *const argstring = _("passing arg of `%s'");
4344 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4345 + strlen (argstring) + 1
4346 + 1);
4347 sprintf (new_opname, argstring,
4348 IDENTIFIER_POINTER (function));
4350 else
4352 /* Function name unknown (call through ptr). */
4353 const char *const argnofun = _("passing arg of pointer to function");
4354 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
4355 sprintf (new_opname, argnofun);
4358 else if (function)
4360 /* Function name is known; supply it. */
4361 const char *const argstring = _("passing arg %d of `%s'");
4362 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4363 + strlen (argstring) + 1 + 25
4364 /*%d*/ + 1);
4365 sprintf (new_opname, argstring, argnum,
4366 IDENTIFIER_POINTER (function));
4368 else
4370 /* Function name unknown (call through ptr); just give arg number. */
4371 const char *const argnofun = _("passing arg %d of pointer to function");
4372 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4373 sprintf (new_opname, argnofun, argnum);
4375 opname = new_opname;
4377 pedwarn (msgid, opname);
4380 /* If VALUE is a compound expr all of whose expressions are constant, then
4381 return its value. Otherwise, return error_mark_node.
4383 This is for handling COMPOUND_EXPRs as initializer elements
4384 which is allowed with a warning when -pedantic is specified. */
4386 static tree
4387 valid_compound_expr_initializer (value, endtype)
4388 tree value;
4389 tree endtype;
4391 if (TREE_CODE (value) == COMPOUND_EXPR)
4393 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4394 == error_mark_node)
4395 return error_mark_node;
4396 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4397 endtype);
4399 else if (! TREE_CONSTANT (value)
4400 && ! initializer_constant_valid_p (value, endtype))
4401 return error_mark_node;
4402 else
4403 return value;
4406 /* Perform appropriate conversions on the initial value of a variable,
4407 store it in the declaration DECL,
4408 and print any error messages that are appropriate.
4409 If the init is invalid, store an ERROR_MARK. */
4411 void
4412 store_init_value (decl, init)
4413 tree decl, init;
4415 tree value, type;
4417 /* If variable's type was invalidly declared, just ignore it. */
4419 type = TREE_TYPE (decl);
4420 if (TREE_CODE (type) == ERROR_MARK)
4421 return;
4423 /* Digest the specified initializer into an expression. */
4425 value = digest_init (type, init, TREE_STATIC (decl));
4427 /* Store the expression if valid; else report error. */
4429 #if 0
4430 /* Note that this is the only place we can detect the error
4431 in a case such as struct foo bar = (struct foo) { x, y };
4432 where there is one initial value which is a constructor expression. */
4433 if (value == error_mark_node)
4435 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4437 error ("initializer for static variable is not constant");
4438 value = error_mark_node;
4440 else if (TREE_STATIC (decl)
4441 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4443 error ("initializer for static variable uses complicated arithmetic");
4444 value = error_mark_node;
4446 else
4448 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4450 if (! TREE_CONSTANT (value))
4451 pedwarn ("aggregate initializer is not constant");
4452 else if (! TREE_STATIC (value))
4453 pedwarn ("aggregate initializer uses complicated arithmetic");
4456 #endif
4458 if (warn_traditional && !in_system_header
4459 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4460 warning ("traditional C rejects automatic aggregate initialization");
4462 DECL_INITIAL (decl) = value;
4464 /* ANSI wants warnings about out-of-range constant initializers. */
4465 STRIP_TYPE_NOPS (value);
4466 constant_expression_warning (value);
4468 /* Check if we need to set array size from compound literal size. */
4469 if (TREE_CODE (type) == ARRAY_TYPE
4470 && TYPE_DOMAIN (type) == 0
4471 && value != error_mark_node)
4473 tree inside_init = init;
4475 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4476 inside_init = TREE_OPERAND (init, 0);
4477 inside_init = fold (inside_init);
4479 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4481 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4483 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4485 /* For int foo[] = (int [3]){1}; we need to set array size
4486 now since later on array initializer will be just the
4487 brace enclosed list of the compound literal. */
4488 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4489 layout_type (type);
4490 layout_decl (decl, 0);
4496 /* Methods for storing and printing names for error messages. */
4498 /* Implement a spelling stack that allows components of a name to be pushed
4499 and popped. Each element on the stack is this structure. */
4501 struct spelling
4503 int kind;
4504 union
4506 int i;
4507 const char *s;
4508 } u;
4511 #define SPELLING_STRING 1
4512 #define SPELLING_MEMBER 2
4513 #define SPELLING_BOUNDS 3
4515 static struct spelling *spelling; /* Next stack element (unused). */
4516 static struct spelling *spelling_base; /* Spelling stack base. */
4517 static int spelling_size; /* Size of the spelling stack. */
4519 /* Macros to save and restore the spelling stack around push_... functions.
4520 Alternative to SAVE_SPELLING_STACK. */
4522 #define SPELLING_DEPTH() (spelling - spelling_base)
4523 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4525 /* Push an element on the spelling stack with type KIND and assign VALUE
4526 to MEMBER. */
4528 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4530 int depth = SPELLING_DEPTH (); \
4532 if (depth >= spelling_size) \
4534 spelling_size += 10; \
4535 if (spelling_base == 0) \
4536 spelling_base \
4537 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4538 else \
4539 spelling_base \
4540 = (struct spelling *) xrealloc (spelling_base, \
4541 spelling_size * sizeof (struct spelling)); \
4542 RESTORE_SPELLING_DEPTH (depth); \
4545 spelling->kind = (KIND); \
4546 spelling->MEMBER = (VALUE); \
4547 spelling++; \
4550 /* Push STRING on the stack. Printed literally. */
4552 static void
4553 push_string (string)
4554 const char *string;
4556 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4559 /* Push a member name on the stack. Printed as '.' STRING. */
4561 static void
4562 push_member_name (decl)
4563 tree decl;
4566 const char *const string
4567 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4568 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4571 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4573 static void
4574 push_array_bounds (bounds)
4575 int bounds;
4577 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4580 /* Compute the maximum size in bytes of the printed spelling. */
4582 static int
4583 spelling_length ()
4585 int size = 0;
4586 struct spelling *p;
4588 for (p = spelling_base; p < spelling; p++)
4590 if (p->kind == SPELLING_BOUNDS)
4591 size += 25;
4592 else
4593 size += strlen (p->u.s) + 1;
4596 return size;
4599 /* Print the spelling to BUFFER and return it. */
4601 static char *
4602 print_spelling (buffer)
4603 char *buffer;
4605 char *d = buffer;
4606 struct spelling *p;
4608 for (p = spelling_base; p < spelling; p++)
4609 if (p->kind == SPELLING_BOUNDS)
4611 sprintf (d, "[%d]", p->u.i);
4612 d += strlen (d);
4614 else
4616 const char *s;
4617 if (p->kind == SPELLING_MEMBER)
4618 *d++ = '.';
4619 for (s = p->u.s; (*d = *s++); d++)
4622 *d++ = '\0';
4623 return buffer;
4626 /* Issue an error message for a bad initializer component.
4627 MSGID identifies the message.
4628 The component name is taken from the spelling stack. */
4630 void
4631 error_init (msgid)
4632 const char *msgid;
4634 char *ofwhat;
4636 error ("%s", _(msgid));
4637 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4638 if (*ofwhat)
4639 error ("(near initialization for `%s')", ofwhat);
4642 /* Issue a pedantic warning for a bad initializer component.
4643 MSGID identifies the message.
4644 The component name is taken from the spelling stack. */
4646 void
4647 pedwarn_init (msgid)
4648 const char *msgid;
4650 char *ofwhat;
4652 pedwarn ("%s", _(msgid));
4653 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4654 if (*ofwhat)
4655 pedwarn ("(near initialization for `%s')", ofwhat);
4658 /* Issue a warning for a bad initializer component.
4659 MSGID identifies the message.
4660 The component name is taken from the spelling stack. */
4662 static void
4663 warning_init (msgid)
4664 const char *msgid;
4666 char *ofwhat;
4668 warning ("%s", _(msgid));
4669 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4670 if (*ofwhat)
4671 warning ("(near initialization for `%s')", ofwhat);
4674 /* Digest the parser output INIT as an initializer for type TYPE.
4675 Return a C expression of type TYPE to represent the initial value.
4677 REQUIRE_CONSTANT requests an error if non-constant initializers or
4678 elements are seen. */
4680 static tree
4681 digest_init (type, init, require_constant)
4682 tree type, init;
4683 int require_constant;
4685 enum tree_code code = TREE_CODE (type);
4686 tree inside_init = init;
4688 if (type == error_mark_node
4689 || init == error_mark_node
4690 || TREE_TYPE (init) == error_mark_node)
4691 return error_mark_node;
4693 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4694 /* Do not use STRIP_NOPS here. We do not want an enumerator
4695 whose value is 0 to count as a null pointer constant. */
4696 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4697 inside_init = TREE_OPERAND (init, 0);
4699 inside_init = fold (inside_init);
4701 /* Initialization of an array of chars from a string constant
4702 optionally enclosed in braces. */
4704 if (code == ARRAY_TYPE)
4706 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4707 if ((typ1 == char_type_node
4708 || typ1 == signed_char_type_node
4709 || typ1 == unsigned_char_type_node
4710 || typ1 == unsigned_wchar_type_node
4711 || typ1 == signed_wchar_type_node)
4712 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4714 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4715 TYPE_MAIN_VARIANT (type)))
4716 return inside_init;
4718 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4719 != char_type_node)
4720 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4722 error_init ("char-array initialized from wide string");
4723 return error_mark_node;
4725 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4726 == char_type_node)
4727 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4729 error_init ("int-array initialized from non-wide string");
4730 return error_mark_node;
4733 TREE_TYPE (inside_init) = type;
4734 if (TYPE_DOMAIN (type) != 0
4735 && TYPE_SIZE (type) != 0
4736 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4737 /* Subtract 1 (or sizeof (wchar_t))
4738 because it's ok to ignore the terminating null char
4739 that is counted in the length of the constant. */
4740 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4741 TREE_STRING_LENGTH (inside_init)
4742 - ((TYPE_PRECISION (typ1)
4743 != TYPE_PRECISION (char_type_node))
4744 ? (TYPE_PRECISION (wchar_type_node)
4745 / BITS_PER_UNIT)
4746 : 1)))
4747 pedwarn_init ("initializer-string for array of chars is too long");
4749 return inside_init;
4753 /* Any type can be initialized
4754 from an expression of the same type, optionally with braces. */
4756 if (inside_init && TREE_TYPE (inside_init) != 0
4757 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4758 TYPE_MAIN_VARIANT (type))
4759 || (code == ARRAY_TYPE
4760 && comptypes (TREE_TYPE (inside_init), type))
4761 || (code == VECTOR_TYPE
4762 && comptypes (TREE_TYPE (inside_init), type))
4763 || (code == POINTER_TYPE
4764 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4765 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4766 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4767 TREE_TYPE (type)))))
4769 if (code == POINTER_TYPE)
4770 inside_init = default_function_array_conversion (inside_init);
4772 if (require_constant && !flag_isoc99
4773 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4775 /* As an extension, allow initializing objects with static storage
4776 duration with compound literals (which are then treated just as
4777 the brace enclosed list they contain). */
4778 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4779 inside_init = DECL_INITIAL (decl);
4782 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4783 && TREE_CODE (inside_init) != CONSTRUCTOR)
4785 error_init ("array initialized from non-constant array expression");
4786 return error_mark_node;
4789 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4790 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4792 /* Compound expressions can only occur here if -pedantic or
4793 -pedantic-errors is specified. In the later case, we always want
4794 an error. In the former case, we simply want a warning. */
4795 if (require_constant && pedantic
4796 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4798 inside_init
4799 = valid_compound_expr_initializer (inside_init,
4800 TREE_TYPE (inside_init));
4801 if (inside_init == error_mark_node)
4802 error_init ("initializer element is not constant");
4803 else
4804 pedwarn_init ("initializer element is not constant");
4805 if (flag_pedantic_errors)
4806 inside_init = error_mark_node;
4808 else if (require_constant
4809 && (!TREE_CONSTANT (inside_init)
4810 /* This test catches things like `7 / 0' which
4811 result in an expression for which TREE_CONSTANT
4812 is true, but which is not actually something
4813 that is a legal constant. We really should not
4814 be using this function, because it is a part of
4815 the back-end. Instead, the expression should
4816 already have been turned into ERROR_MARK_NODE. */
4817 || !initializer_constant_valid_p (inside_init,
4818 TREE_TYPE (inside_init))))
4820 error_init ("initializer element is not constant");
4821 inside_init = error_mark_node;
4824 return inside_init;
4827 /* Handle scalar types, including conversions. */
4829 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4830 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4832 /* Note that convert_for_assignment calls default_conversion
4833 for arrays and functions. We must not call it in the
4834 case where inside_init is a null pointer constant. */
4835 inside_init
4836 = convert_for_assignment (type, init, _("initialization"),
4837 NULL_TREE, NULL_TREE, 0);
4839 if (require_constant && ! TREE_CONSTANT (inside_init))
4841 error_init ("initializer element is not constant");
4842 inside_init = error_mark_node;
4844 else if (require_constant
4845 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4847 error_init ("initializer element is not computable at load time");
4848 inside_init = error_mark_node;
4851 return inside_init;
4854 /* Come here only for records and arrays. */
4856 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4858 error_init ("variable-sized object may not be initialized");
4859 return error_mark_node;
4862 error_init ("invalid initializer");
4863 return error_mark_node;
4866 /* Handle initializers that use braces. */
4868 /* Type of object we are accumulating a constructor for.
4869 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4870 static tree constructor_type;
4872 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4873 left to fill. */
4874 static tree constructor_fields;
4876 /* For an ARRAY_TYPE, this is the specified index
4877 at which to store the next element we get. */
4878 static tree constructor_index;
4880 /* For an ARRAY_TYPE, this is the maximum index. */
4881 static tree constructor_max_index;
4883 /* For a RECORD_TYPE, this is the first field not yet written out. */
4884 static tree constructor_unfilled_fields;
4886 /* For an ARRAY_TYPE, this is the index of the first element
4887 not yet written out. */
4888 static tree constructor_unfilled_index;
4890 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4891 This is so we can generate gaps between fields, when appropriate. */
4892 static tree constructor_bit_index;
4894 /* If we are saving up the elements rather than allocating them,
4895 this is the list of elements so far (in reverse order,
4896 most recent first). */
4897 static tree constructor_elements;
4899 /* 1 if constructor should be incrementally stored into a constructor chain,
4900 0 if all the elements should be kept in AVL tree. */
4901 static int constructor_incremental;
4903 /* 1 if so far this constructor's elements are all compile-time constants. */
4904 static int constructor_constant;
4906 /* 1 if so far this constructor's elements are all valid address constants. */
4907 static int constructor_simple;
4909 /* 1 if this constructor is erroneous so far. */
4910 static int constructor_erroneous;
4912 /* 1 if have called defer_addressed_constants. */
4913 static int constructor_subconstants_deferred;
4915 /* Structure for managing pending initializer elements, organized as an
4916 AVL tree. */
4918 struct init_node
4920 struct init_node *left, *right;
4921 struct init_node *parent;
4922 int balance;
4923 tree purpose;
4924 tree value;
4927 /* Tree of pending elements at this constructor level.
4928 These are elements encountered out of order
4929 which belong at places we haven't reached yet in actually
4930 writing the output.
4931 Will never hold tree nodes across GC runs. */
4932 static struct init_node *constructor_pending_elts;
4934 /* The SPELLING_DEPTH of this constructor. */
4935 static int constructor_depth;
4937 /* 0 if implicitly pushing constructor levels is allowed. */
4938 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4940 static int require_constant_value;
4941 static int require_constant_elements;
4943 /* DECL node for which an initializer is being read.
4944 0 means we are reading a constructor expression
4945 such as (struct foo) {...}. */
4946 static tree constructor_decl;
4948 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4949 static const char *constructor_asmspec;
4951 /* Nonzero if this is an initializer for a top-level decl. */
4952 static int constructor_top_level;
4954 /* Nonzero if there were any member designators in this initializer. */
4955 static int constructor_designated;
4957 /* Nesting depth of designator list. */
4958 static int designator_depth;
4960 /* Nonzero if there were diagnosed errors in this designator list. */
4961 static int designator_errorneous;
4964 /* This stack has a level for each implicit or explicit level of
4965 structuring in the initializer, including the outermost one. It
4966 saves the values of most of the variables above. */
4968 struct constructor_range_stack;
4970 struct constructor_stack
4972 struct constructor_stack *next;
4973 tree type;
4974 tree fields;
4975 tree index;
4976 tree max_index;
4977 tree unfilled_index;
4978 tree unfilled_fields;
4979 tree bit_index;
4980 tree elements;
4981 struct init_node *pending_elts;
4982 int offset;
4983 int depth;
4984 /* If nonzero, this value should replace the entire
4985 constructor at this level. */
4986 tree replacement_value;
4987 struct constructor_range_stack *range_stack;
4988 char constant;
4989 char simple;
4990 char implicit;
4991 char erroneous;
4992 char outer;
4993 char incremental;
4994 char designated;
4997 struct constructor_stack *constructor_stack;
4999 /* This stack represents designators from some range designator up to
5000 the last designator in the list. */
5002 struct constructor_range_stack
5004 struct constructor_range_stack *next, *prev;
5005 struct constructor_stack *stack;
5006 tree range_start;
5007 tree index;
5008 tree range_end;
5009 tree fields;
5012 struct constructor_range_stack *constructor_range_stack;
5014 /* This stack records separate initializers that are nested.
5015 Nested initializers can't happen in ANSI C, but GNU C allows them
5016 in cases like { ... (struct foo) { ... } ... }. */
5018 struct initializer_stack
5020 struct initializer_stack *next;
5021 tree decl;
5022 const char *asmspec;
5023 struct constructor_stack *constructor_stack;
5024 struct constructor_range_stack *constructor_range_stack;
5025 tree elements;
5026 struct spelling *spelling;
5027 struct spelling *spelling_base;
5028 int spelling_size;
5029 char top_level;
5030 char require_constant_value;
5031 char require_constant_elements;
5032 char deferred;
5035 struct initializer_stack *initializer_stack;
5037 /* Prepare to parse and output the initializer for variable DECL. */
5039 void
5040 start_init (decl, asmspec_tree, top_level)
5041 tree decl;
5042 tree asmspec_tree;
5043 int top_level;
5045 const char *locus;
5046 struct initializer_stack *p
5047 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5048 const char *asmspec = 0;
5050 if (asmspec_tree)
5051 asmspec = TREE_STRING_POINTER (asmspec_tree);
5053 p->decl = constructor_decl;
5054 p->asmspec = constructor_asmspec;
5055 p->require_constant_value = require_constant_value;
5056 p->require_constant_elements = require_constant_elements;
5057 p->constructor_stack = constructor_stack;
5058 p->constructor_range_stack = constructor_range_stack;
5059 p->elements = constructor_elements;
5060 p->spelling = spelling;
5061 p->spelling_base = spelling_base;
5062 p->spelling_size = spelling_size;
5063 p->deferred = constructor_subconstants_deferred;
5064 p->top_level = constructor_top_level;
5065 p->next = initializer_stack;
5066 initializer_stack = p;
5068 constructor_decl = decl;
5069 constructor_asmspec = asmspec;
5070 constructor_subconstants_deferred = 0;
5071 constructor_designated = 0;
5072 constructor_top_level = top_level;
5074 if (decl != 0)
5076 require_constant_value = TREE_STATIC (decl);
5077 require_constant_elements
5078 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5079 /* For a scalar, you can always use any value to initialize,
5080 even within braces. */
5081 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5082 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5083 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5084 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5085 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5087 else
5089 require_constant_value = 0;
5090 require_constant_elements = 0;
5091 locus = "(anonymous)";
5094 constructor_stack = 0;
5095 constructor_range_stack = 0;
5097 missing_braces_mentioned = 0;
5099 spelling_base = 0;
5100 spelling_size = 0;
5101 RESTORE_SPELLING_DEPTH (0);
5103 if (locus)
5104 push_string (locus);
5107 void
5108 finish_init ()
5110 struct initializer_stack *p = initializer_stack;
5112 /* Output subconstants (string constants, usually)
5113 that were referenced within this initializer and saved up.
5114 Must do this if and only if we called defer_addressed_constants. */
5115 if (constructor_subconstants_deferred)
5116 output_deferred_addressed_constants ();
5118 /* Free the whole constructor stack of this initializer. */
5119 while (constructor_stack)
5121 struct constructor_stack *q = constructor_stack;
5122 constructor_stack = q->next;
5123 free (q);
5126 if (constructor_range_stack)
5127 abort ();
5129 /* Pop back to the data of the outer initializer (if any). */
5130 constructor_decl = p->decl;
5131 constructor_asmspec = p->asmspec;
5132 require_constant_value = p->require_constant_value;
5133 require_constant_elements = p->require_constant_elements;
5134 constructor_stack = p->constructor_stack;
5135 constructor_range_stack = p->constructor_range_stack;
5136 constructor_elements = p->elements;
5137 spelling = p->spelling;
5138 spelling_base = p->spelling_base;
5139 spelling_size = p->spelling_size;
5140 constructor_subconstants_deferred = p->deferred;
5141 constructor_top_level = p->top_level;
5142 initializer_stack = p->next;
5143 free (p);
5146 /* Call here when we see the initializer is surrounded by braces.
5147 This is instead of a call to push_init_level;
5148 it is matched by a call to pop_init_level.
5150 TYPE is the type to initialize, for a constructor expression.
5151 For an initializer for a decl, TYPE is zero. */
5153 void
5154 really_start_incremental_init (type)
5155 tree type;
5157 struct constructor_stack *p
5158 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5160 if (type == 0)
5161 type = TREE_TYPE (constructor_decl);
5163 if ((*targetm.vector_opaque_p) (type))
5164 error ("opaque vector types cannot be initialized");
5166 p->type = constructor_type;
5167 p->fields = constructor_fields;
5168 p->index = constructor_index;
5169 p->max_index = constructor_max_index;
5170 p->unfilled_index = constructor_unfilled_index;
5171 p->unfilled_fields = constructor_unfilled_fields;
5172 p->bit_index = constructor_bit_index;
5173 p->elements = constructor_elements;
5174 p->constant = constructor_constant;
5175 p->simple = constructor_simple;
5176 p->erroneous = constructor_erroneous;
5177 p->pending_elts = constructor_pending_elts;
5178 p->depth = constructor_depth;
5179 p->replacement_value = 0;
5180 p->implicit = 0;
5181 p->range_stack = 0;
5182 p->outer = 0;
5183 p->incremental = constructor_incremental;
5184 p->designated = constructor_designated;
5185 p->next = 0;
5186 constructor_stack = p;
5188 constructor_constant = 1;
5189 constructor_simple = 1;
5190 constructor_depth = SPELLING_DEPTH ();
5191 constructor_elements = 0;
5192 constructor_pending_elts = 0;
5193 constructor_type = type;
5194 constructor_incremental = 1;
5195 constructor_designated = 0;
5196 designator_depth = 0;
5197 designator_errorneous = 0;
5199 if (TREE_CODE (constructor_type) == RECORD_TYPE
5200 || TREE_CODE (constructor_type) == UNION_TYPE)
5202 constructor_fields = TYPE_FIELDS (constructor_type);
5203 /* Skip any nameless bit fields at the beginning. */
5204 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5205 && DECL_NAME (constructor_fields) == 0)
5206 constructor_fields = TREE_CHAIN (constructor_fields);
5208 constructor_unfilled_fields = constructor_fields;
5209 constructor_bit_index = bitsize_zero_node;
5211 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5213 if (TYPE_DOMAIN (constructor_type))
5215 constructor_max_index
5216 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5218 /* Detect non-empty initializations of zero-length arrays. */
5219 if (constructor_max_index == NULL_TREE
5220 && TYPE_SIZE (constructor_type))
5221 constructor_max_index = build_int_2 (-1, -1);
5223 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5224 to initialize VLAs will cause a proper error; avoid tree
5225 checking errors as well by setting a safe value. */
5226 if (constructor_max_index
5227 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5228 constructor_max_index = build_int_2 (-1, -1);
5230 constructor_index
5231 = convert (bitsizetype,
5232 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5234 else
5235 constructor_index = bitsize_zero_node;
5237 constructor_unfilled_index = constructor_index;
5239 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5241 /* Vectors are like simple fixed-size arrays. */
5242 constructor_max_index =
5243 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5244 constructor_index = convert (bitsizetype, bitsize_zero_node);
5245 constructor_unfilled_index = constructor_index;
5247 else
5249 /* Handle the case of int x = {5}; */
5250 constructor_fields = constructor_type;
5251 constructor_unfilled_fields = constructor_type;
5255 /* Push down into a subobject, for initialization.
5256 If this is for an explicit set of braces, IMPLICIT is 0.
5257 If it is because the next element belongs at a lower level,
5258 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5260 void
5261 push_init_level (implicit)
5262 int implicit;
5264 struct constructor_stack *p;
5265 tree value = NULL_TREE;
5267 /* If we've exhausted any levels that didn't have braces,
5268 pop them now. */
5269 while (constructor_stack->implicit)
5271 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5272 || TREE_CODE (constructor_type) == UNION_TYPE)
5273 && constructor_fields == 0)
5274 process_init_element (pop_init_level (1));
5275 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5276 && constructor_max_index
5277 && tree_int_cst_lt (constructor_max_index, constructor_index))
5278 process_init_element (pop_init_level (1));
5279 else
5280 break;
5283 /* Unless this is an explicit brace, we need to preserve previous
5284 content if any. */
5285 if (implicit)
5287 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5288 || TREE_CODE (constructor_type) == UNION_TYPE)
5289 && constructor_fields)
5290 value = find_init_member (constructor_fields);
5291 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5292 value = find_init_member (constructor_index);
5295 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5296 p->type = constructor_type;
5297 p->fields = constructor_fields;
5298 p->index = constructor_index;
5299 p->max_index = constructor_max_index;
5300 p->unfilled_index = constructor_unfilled_index;
5301 p->unfilled_fields = constructor_unfilled_fields;
5302 p->bit_index = constructor_bit_index;
5303 p->elements = constructor_elements;
5304 p->constant = constructor_constant;
5305 p->simple = constructor_simple;
5306 p->erroneous = constructor_erroneous;
5307 p->pending_elts = constructor_pending_elts;
5308 p->depth = constructor_depth;
5309 p->replacement_value = 0;
5310 p->implicit = implicit;
5311 p->outer = 0;
5312 p->incremental = constructor_incremental;
5313 p->designated = constructor_designated;
5314 p->next = constructor_stack;
5315 p->range_stack = 0;
5316 constructor_stack = p;
5318 constructor_constant = 1;
5319 constructor_simple = 1;
5320 constructor_depth = SPELLING_DEPTH ();
5321 constructor_elements = 0;
5322 constructor_incremental = 1;
5323 constructor_designated = 0;
5324 constructor_pending_elts = 0;
5325 if (!implicit)
5327 p->range_stack = constructor_range_stack;
5328 constructor_range_stack = 0;
5329 designator_depth = 0;
5330 designator_errorneous = 0;
5333 /* Don't die if an entire brace-pair level is superfluous
5334 in the containing level. */
5335 if (constructor_type == 0)
5337 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5338 || TREE_CODE (constructor_type) == UNION_TYPE)
5340 /* Don't die if there are extra init elts at the end. */
5341 if (constructor_fields == 0)
5342 constructor_type = 0;
5343 else
5345 constructor_type = TREE_TYPE (constructor_fields);
5346 push_member_name (constructor_fields);
5347 constructor_depth++;
5350 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5352 constructor_type = TREE_TYPE (constructor_type);
5353 push_array_bounds (tree_low_cst (constructor_index, 0));
5354 constructor_depth++;
5357 if (constructor_type == 0)
5359 error_init ("extra brace group at end of initializer");
5360 constructor_fields = 0;
5361 constructor_unfilled_fields = 0;
5362 return;
5365 if (value && TREE_CODE (value) == CONSTRUCTOR)
5367 constructor_constant = TREE_CONSTANT (value);
5368 constructor_simple = TREE_STATIC (value);
5369 constructor_elements = TREE_OPERAND (value, 1);
5370 if (constructor_elements
5371 && (TREE_CODE (constructor_type) == RECORD_TYPE
5372 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5373 set_nonincremental_init ();
5376 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5378 missing_braces_mentioned = 1;
5379 warning_init ("missing braces around initializer");
5382 if (TREE_CODE (constructor_type) == RECORD_TYPE
5383 || TREE_CODE (constructor_type) == UNION_TYPE)
5385 constructor_fields = TYPE_FIELDS (constructor_type);
5386 /* Skip any nameless bit fields at the beginning. */
5387 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5388 && DECL_NAME (constructor_fields) == 0)
5389 constructor_fields = TREE_CHAIN (constructor_fields);
5391 constructor_unfilled_fields = constructor_fields;
5392 constructor_bit_index = bitsize_zero_node;
5394 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5396 /* Vectors are like simple fixed-size arrays. */
5397 constructor_max_index =
5398 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5399 constructor_index = convert (bitsizetype, integer_zero_node);
5400 constructor_unfilled_index = constructor_index;
5402 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5404 if (TYPE_DOMAIN (constructor_type))
5406 constructor_max_index
5407 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5409 /* Detect non-empty initializations of zero-length arrays. */
5410 if (constructor_max_index == NULL_TREE
5411 && TYPE_SIZE (constructor_type))
5412 constructor_max_index = build_int_2 (-1, -1);
5414 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5415 to initialize VLAs will cause a proper error; avoid tree
5416 checking errors as well by setting a safe value. */
5417 if (constructor_max_index
5418 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5419 constructor_max_index = build_int_2 (-1, -1);
5421 constructor_index
5422 = convert (bitsizetype,
5423 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5425 else
5426 constructor_index = bitsize_zero_node;
5428 constructor_unfilled_index = constructor_index;
5429 if (value && TREE_CODE (value) == STRING_CST)
5431 /* We need to split the char/wchar array into individual
5432 characters, so that we don't have to special case it
5433 everywhere. */
5434 set_nonincremental_init_from_string (value);
5437 else
5439 warning_init ("braces around scalar initializer");
5440 constructor_fields = constructor_type;
5441 constructor_unfilled_fields = constructor_type;
5445 /* At the end of an implicit or explicit brace level,
5446 finish up that level of constructor.
5447 If we were outputting the elements as they are read, return 0
5448 from inner levels (process_init_element ignores that),
5449 but return error_mark_node from the outermost level
5450 (that's what we want to put in DECL_INITIAL).
5451 Otherwise, return a CONSTRUCTOR expression. */
5453 tree
5454 pop_init_level (implicit)
5455 int implicit;
5457 struct constructor_stack *p;
5458 tree constructor = 0;
5460 if (implicit == 0)
5462 /* When we come to an explicit close brace,
5463 pop any inner levels that didn't have explicit braces. */
5464 while (constructor_stack->implicit)
5465 process_init_element (pop_init_level (1));
5467 if (constructor_range_stack)
5468 abort ();
5471 p = constructor_stack;
5473 /* Error for initializing a flexible array member, or a zero-length
5474 array member in an inappropriate context. */
5475 if (constructor_type && constructor_fields
5476 && TREE_CODE (constructor_type) == ARRAY_TYPE
5477 && TYPE_DOMAIN (constructor_type)
5478 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5480 /* Silently discard empty initializations. The parser will
5481 already have pedwarned for empty brackets. */
5482 if (integer_zerop (constructor_unfilled_index))
5483 constructor_type = NULL_TREE;
5484 else if (! TYPE_SIZE (constructor_type))
5486 if (constructor_depth > 2)
5487 error_init ("initialization of flexible array member in a nested context");
5488 else if (pedantic)
5489 pedwarn_init ("initialization of a flexible array member");
5491 /* We have already issued an error message for the existence
5492 of a flexible array member not at the end of the structure.
5493 Discard the initializer so that we do not abort later. */
5494 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5495 constructor_type = NULL_TREE;
5497 else
5498 /* Zero-length arrays are no longer special, so we should no longer
5499 get here. */
5500 abort ();
5503 /* Warn when some struct elements are implicitly initialized to zero. */
5504 if (extra_warnings
5505 && constructor_type
5506 && TREE_CODE (constructor_type) == RECORD_TYPE
5507 && constructor_unfilled_fields)
5509 /* Do not warn for flexible array members or zero-length arrays. */
5510 while (constructor_unfilled_fields
5511 && (! DECL_SIZE (constructor_unfilled_fields)
5512 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5513 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5515 /* Do not warn if this level of the initializer uses member
5516 designators; it is likely to be deliberate. */
5517 if (constructor_unfilled_fields && !constructor_designated)
5519 push_member_name (constructor_unfilled_fields);
5520 warning_init ("missing initializer");
5521 RESTORE_SPELLING_DEPTH (constructor_depth);
5525 /* Now output all pending elements. */
5526 constructor_incremental = 1;
5527 output_pending_init_elements (1);
5529 /* Pad out the end of the structure. */
5530 if (p->replacement_value)
5531 /* If this closes a superfluous brace pair,
5532 just pass out the element between them. */
5533 constructor = p->replacement_value;
5534 else if (constructor_type == 0)
5536 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5537 && TREE_CODE (constructor_type) != UNION_TYPE
5538 && TREE_CODE (constructor_type) != ARRAY_TYPE
5539 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5541 /* A nonincremental scalar initializer--just return
5542 the element, after verifying there is just one. */
5543 if (constructor_elements == 0)
5545 if (!constructor_erroneous)
5546 error_init ("empty scalar initializer");
5547 constructor = error_mark_node;
5549 else if (TREE_CHAIN (constructor_elements) != 0)
5551 error_init ("extra elements in scalar initializer");
5552 constructor = TREE_VALUE (constructor_elements);
5554 else
5555 constructor = TREE_VALUE (constructor_elements);
5557 else
5559 if (constructor_erroneous)
5560 constructor = error_mark_node;
5561 else
5563 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5564 nreverse (constructor_elements));
5565 if (constructor_constant)
5566 TREE_CONSTANT (constructor) = 1;
5567 if (constructor_constant && constructor_simple)
5568 TREE_STATIC (constructor) = 1;
5572 constructor_type = p->type;
5573 constructor_fields = p->fields;
5574 constructor_index = p->index;
5575 constructor_max_index = p->max_index;
5576 constructor_unfilled_index = p->unfilled_index;
5577 constructor_unfilled_fields = p->unfilled_fields;
5578 constructor_bit_index = p->bit_index;
5579 constructor_elements = p->elements;
5580 constructor_constant = p->constant;
5581 constructor_simple = p->simple;
5582 constructor_erroneous = p->erroneous;
5583 constructor_incremental = p->incremental;
5584 constructor_designated = p->designated;
5585 constructor_pending_elts = p->pending_elts;
5586 constructor_depth = p->depth;
5587 if (!p->implicit)
5588 constructor_range_stack = p->range_stack;
5589 RESTORE_SPELLING_DEPTH (constructor_depth);
5591 constructor_stack = p->next;
5592 free (p);
5594 if (constructor == 0)
5596 if (constructor_stack == 0)
5597 return error_mark_node;
5598 return NULL_TREE;
5600 return constructor;
5603 /* Common handling for both array range and field name designators.
5604 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5606 static int
5607 set_designator (array)
5608 int array;
5610 tree subtype;
5611 enum tree_code subcode;
5613 /* Don't die if an entire brace-pair level is superfluous
5614 in the containing level. */
5615 if (constructor_type == 0)
5616 return 1;
5618 /* If there were errors in this designator list already, bail out silently. */
5619 if (designator_errorneous)
5620 return 1;
5622 if (!designator_depth)
5624 if (constructor_range_stack)
5625 abort ();
5627 /* Designator list starts at the level of closest explicit
5628 braces. */
5629 while (constructor_stack->implicit)
5630 process_init_element (pop_init_level (1));
5631 constructor_designated = 1;
5632 return 0;
5635 if (constructor_no_implicit)
5637 error_init ("initialization designators may not nest");
5638 return 1;
5641 if (TREE_CODE (constructor_type) == RECORD_TYPE
5642 || TREE_CODE (constructor_type) == UNION_TYPE)
5644 subtype = TREE_TYPE (constructor_fields);
5645 if (subtype != error_mark_node)
5646 subtype = TYPE_MAIN_VARIANT (subtype);
5648 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5650 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5652 else
5653 abort ();
5655 subcode = TREE_CODE (subtype);
5656 if (array && subcode != ARRAY_TYPE)
5658 error_init ("array index in non-array initializer");
5659 return 1;
5661 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5663 error_init ("field name not in record or union initializer");
5664 return 1;
5667 constructor_designated = 1;
5668 push_init_level (2);
5669 return 0;
5672 /* If there are range designators in designator list, push a new designator
5673 to constructor_range_stack. RANGE_END is end of such stack range or
5674 NULL_TREE if there is no range designator at this level. */
5676 static void
5677 push_range_stack (range_end)
5678 tree range_end;
5680 struct constructor_range_stack *p;
5682 p = (struct constructor_range_stack *)
5683 ggc_alloc (sizeof (struct constructor_range_stack));
5684 p->prev = constructor_range_stack;
5685 p->next = 0;
5686 p->fields = constructor_fields;
5687 p->range_start = constructor_index;
5688 p->index = constructor_index;
5689 p->stack = constructor_stack;
5690 p->range_end = range_end;
5691 if (constructor_range_stack)
5692 constructor_range_stack->next = p;
5693 constructor_range_stack = p;
5696 /* Within an array initializer, specify the next index to be initialized.
5697 FIRST is that index. If LAST is nonzero, then initialize a range
5698 of indices, running from FIRST through LAST. */
5700 void
5701 set_init_index (first, last)
5702 tree first, last;
5704 if (set_designator (1))
5705 return;
5707 designator_errorneous = 1;
5709 while ((TREE_CODE (first) == NOP_EXPR
5710 || TREE_CODE (first) == CONVERT_EXPR
5711 || TREE_CODE (first) == NON_LVALUE_EXPR)
5712 && (TYPE_MODE (TREE_TYPE (first))
5713 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5714 first = TREE_OPERAND (first, 0);
5716 if (last)
5717 while ((TREE_CODE (last) == NOP_EXPR
5718 || TREE_CODE (last) == CONVERT_EXPR
5719 || TREE_CODE (last) == NON_LVALUE_EXPR)
5720 && (TYPE_MODE (TREE_TYPE (last))
5721 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5722 last = TREE_OPERAND (last, 0);
5724 if (TREE_CODE (first) != INTEGER_CST)
5725 error_init ("nonconstant array index in initializer");
5726 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5727 error_init ("nonconstant array index in initializer");
5728 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5729 error_init ("array index in non-array initializer");
5730 else if (constructor_max_index
5731 && tree_int_cst_lt (constructor_max_index, first))
5732 error_init ("array index in initializer exceeds array bounds");
5733 else
5735 constructor_index = convert (bitsizetype, first);
5737 if (last)
5739 if (tree_int_cst_equal (first, last))
5740 last = 0;
5741 else if (tree_int_cst_lt (last, first))
5743 error_init ("empty index range in initializer");
5744 last = 0;
5746 else
5748 last = convert (bitsizetype, last);
5749 if (constructor_max_index != 0
5750 && tree_int_cst_lt (constructor_max_index, last))
5752 error_init ("array index range in initializer exceeds array bounds");
5753 last = 0;
5758 designator_depth++;
5759 designator_errorneous = 0;
5760 if (constructor_range_stack || last)
5761 push_range_stack (last);
5765 /* Within a struct initializer, specify the next field to be initialized. */
5767 void
5768 set_init_label (fieldname)
5769 tree fieldname;
5771 tree tail;
5773 if (set_designator (0))
5774 return;
5776 designator_errorneous = 1;
5778 if (TREE_CODE (constructor_type) != RECORD_TYPE
5779 && TREE_CODE (constructor_type) != UNION_TYPE)
5781 error_init ("field name not in record or union initializer");
5782 return;
5785 for (tail = TYPE_FIELDS (constructor_type); tail;
5786 tail = TREE_CHAIN (tail))
5788 if (DECL_NAME (tail) == fieldname)
5789 break;
5792 if (tail == 0)
5793 error ("unknown field `%s' specified in initializer",
5794 IDENTIFIER_POINTER (fieldname));
5795 else
5797 constructor_fields = tail;
5798 designator_depth++;
5799 designator_errorneous = 0;
5800 if (constructor_range_stack)
5801 push_range_stack (NULL_TREE);
5805 /* Add a new initializer to the tree of pending initializers. PURPOSE
5806 identifies the initializer, either array index or field in a structure.
5807 VALUE is the value of that index or field. */
5809 static void
5810 add_pending_init (purpose, value)
5811 tree purpose, value;
5813 struct init_node *p, **q, *r;
5815 q = &constructor_pending_elts;
5816 p = 0;
5818 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5820 while (*q != 0)
5822 p = *q;
5823 if (tree_int_cst_lt (purpose, p->purpose))
5824 q = &p->left;
5825 else if (tree_int_cst_lt (p->purpose, purpose))
5826 q = &p->right;
5827 else
5829 if (TREE_SIDE_EFFECTS (p->value))
5830 warning_init ("initialized field with side-effects overwritten");
5831 p->value = value;
5832 return;
5836 else
5838 tree bitpos;
5840 bitpos = bit_position (purpose);
5841 while (*q != NULL)
5843 p = *q;
5844 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5845 q = &p->left;
5846 else if (p->purpose != purpose)
5847 q = &p->right;
5848 else
5850 if (TREE_SIDE_EFFECTS (p->value))
5851 warning_init ("initialized field with side-effects overwritten");
5852 p->value = value;
5853 return;
5858 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5859 r->purpose = purpose;
5860 r->value = value;
5862 *q = r;
5863 r->parent = p;
5864 r->left = 0;
5865 r->right = 0;
5866 r->balance = 0;
5868 while (p)
5870 struct init_node *s;
5872 if (r == p->left)
5874 if (p->balance == 0)
5875 p->balance = -1;
5876 else if (p->balance < 0)
5878 if (r->balance < 0)
5880 /* L rotation. */
5881 p->left = r->right;
5882 if (p->left)
5883 p->left->parent = p;
5884 r->right = p;
5886 p->balance = 0;
5887 r->balance = 0;
5889 s = p->parent;
5890 p->parent = r;
5891 r->parent = s;
5892 if (s)
5894 if (s->left == p)
5895 s->left = r;
5896 else
5897 s->right = r;
5899 else
5900 constructor_pending_elts = r;
5902 else
5904 /* LR rotation. */
5905 struct init_node *t = r->right;
5907 r->right = t->left;
5908 if (r->right)
5909 r->right->parent = r;
5910 t->left = r;
5912 p->left = t->right;
5913 if (p->left)
5914 p->left->parent = p;
5915 t->right = p;
5917 p->balance = t->balance < 0;
5918 r->balance = -(t->balance > 0);
5919 t->balance = 0;
5921 s = p->parent;
5922 p->parent = t;
5923 r->parent = t;
5924 t->parent = s;
5925 if (s)
5927 if (s->left == p)
5928 s->left = t;
5929 else
5930 s->right = t;
5932 else
5933 constructor_pending_elts = t;
5935 break;
5937 else
5939 /* p->balance == +1; growth of left side balances the node. */
5940 p->balance = 0;
5941 break;
5944 else /* r == p->right */
5946 if (p->balance == 0)
5947 /* Growth propagation from right side. */
5948 p->balance++;
5949 else if (p->balance > 0)
5951 if (r->balance > 0)
5953 /* R rotation. */
5954 p->right = r->left;
5955 if (p->right)
5956 p->right->parent = p;
5957 r->left = p;
5959 p->balance = 0;
5960 r->balance = 0;
5962 s = p->parent;
5963 p->parent = r;
5964 r->parent = s;
5965 if (s)
5967 if (s->left == p)
5968 s->left = r;
5969 else
5970 s->right = r;
5972 else
5973 constructor_pending_elts = r;
5975 else /* r->balance == -1 */
5977 /* RL rotation */
5978 struct init_node *t = r->left;
5980 r->left = t->right;
5981 if (r->left)
5982 r->left->parent = r;
5983 t->right = r;
5985 p->right = t->left;
5986 if (p->right)
5987 p->right->parent = p;
5988 t->left = p;
5990 r->balance = (t->balance < 0);
5991 p->balance = -(t->balance > 0);
5992 t->balance = 0;
5994 s = p->parent;
5995 p->parent = t;
5996 r->parent = t;
5997 t->parent = s;
5998 if (s)
6000 if (s->left == p)
6001 s->left = t;
6002 else
6003 s->right = t;
6005 else
6006 constructor_pending_elts = t;
6008 break;
6010 else
6012 /* p->balance == -1; growth of right side balances the node. */
6013 p->balance = 0;
6014 break;
6018 r = p;
6019 p = p->parent;
6023 /* Build AVL tree from a sorted chain. */
6025 static void
6026 set_nonincremental_init ()
6028 tree chain;
6030 if (TREE_CODE (constructor_type) != RECORD_TYPE
6031 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6032 return;
6034 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6035 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6036 constructor_elements = 0;
6037 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6039 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6040 /* Skip any nameless bit fields at the beginning. */
6041 while (constructor_unfilled_fields != 0
6042 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6043 && DECL_NAME (constructor_unfilled_fields) == 0)
6044 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6047 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6049 if (TYPE_DOMAIN (constructor_type))
6050 constructor_unfilled_index
6051 = convert (bitsizetype,
6052 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6053 else
6054 constructor_unfilled_index = bitsize_zero_node;
6056 constructor_incremental = 0;
6059 /* Build AVL tree from a string constant. */
6061 static void
6062 set_nonincremental_init_from_string (str)
6063 tree str;
6065 tree value, purpose, type;
6066 HOST_WIDE_INT val[2];
6067 const char *p, *end;
6068 int byte, wchar_bytes, charwidth, bitpos;
6070 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6071 abort ();
6073 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6074 == TYPE_PRECISION (char_type_node))
6075 wchar_bytes = 1;
6076 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6077 == TYPE_PRECISION (wchar_type_node))
6078 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6079 else
6080 abort ();
6082 charwidth = TYPE_PRECISION (char_type_node);
6083 type = TREE_TYPE (constructor_type);
6084 p = TREE_STRING_POINTER (str);
6085 end = p + TREE_STRING_LENGTH (str);
6087 for (purpose = bitsize_zero_node;
6088 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6089 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6091 if (wchar_bytes == 1)
6093 val[1] = (unsigned char) *p++;
6094 val[0] = 0;
6096 else
6098 val[0] = 0;
6099 val[1] = 0;
6100 for (byte = 0; byte < wchar_bytes; byte++)
6102 if (BYTES_BIG_ENDIAN)
6103 bitpos = (wchar_bytes - byte - 1) * charwidth;
6104 else
6105 bitpos = byte * charwidth;
6106 val[bitpos < HOST_BITS_PER_WIDE_INT]
6107 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6108 << (bitpos % HOST_BITS_PER_WIDE_INT);
6112 if (!TREE_UNSIGNED (type))
6114 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6115 if (bitpos < HOST_BITS_PER_WIDE_INT)
6117 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6119 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6120 val[0] = -1;
6123 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6125 if (val[1] < 0)
6126 val[0] = -1;
6128 else if (val[0] & (((HOST_WIDE_INT) 1)
6129 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6130 val[0] |= ((HOST_WIDE_INT) -1)
6131 << (bitpos - HOST_BITS_PER_WIDE_INT);
6134 value = build_int_2 (val[1], val[0]);
6135 TREE_TYPE (value) = type;
6136 add_pending_init (purpose, value);
6139 constructor_incremental = 0;
6142 /* Return value of FIELD in pending initializer or zero if the field was
6143 not initialized yet. */
6145 static tree
6146 find_init_member (field)
6147 tree field;
6149 struct init_node *p;
6151 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6153 if (constructor_incremental
6154 && tree_int_cst_lt (field, constructor_unfilled_index))
6155 set_nonincremental_init ();
6157 p = constructor_pending_elts;
6158 while (p)
6160 if (tree_int_cst_lt (field, p->purpose))
6161 p = p->left;
6162 else if (tree_int_cst_lt (p->purpose, field))
6163 p = p->right;
6164 else
6165 return p->value;
6168 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6170 tree bitpos = bit_position (field);
6172 if (constructor_incremental
6173 && (!constructor_unfilled_fields
6174 || tree_int_cst_lt (bitpos,
6175 bit_position (constructor_unfilled_fields))))
6176 set_nonincremental_init ();
6178 p = constructor_pending_elts;
6179 while (p)
6181 if (field == p->purpose)
6182 return p->value;
6183 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6184 p = p->left;
6185 else
6186 p = p->right;
6189 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6191 if (constructor_elements
6192 && TREE_PURPOSE (constructor_elements) == field)
6193 return TREE_VALUE (constructor_elements);
6195 return 0;
6198 /* "Output" the next constructor element.
6199 At top level, really output it to assembler code now.
6200 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6201 TYPE is the data type that the containing data type wants here.
6202 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6204 PENDING if non-nil means output pending elements that belong
6205 right after this element. (PENDING is normally 1;
6206 it is 0 while outputting pending elements, to avoid recursion.) */
6208 static void
6209 output_init_element (value, type, field, pending)
6210 tree value, type, field;
6211 int pending;
6213 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6214 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6215 && !(TREE_CODE (value) == STRING_CST
6216 && TREE_CODE (type) == ARRAY_TYPE
6217 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6218 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6219 TYPE_MAIN_VARIANT (type))))
6220 value = default_conversion (value);
6222 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6223 && require_constant_value && !flag_isoc99 && pending)
6225 /* As an extension, allow initializing objects with static storage
6226 duration with compound literals (which are then treated just as
6227 the brace enclosed list they contain). */
6228 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6229 value = DECL_INITIAL (decl);
6232 if (value == error_mark_node)
6233 constructor_erroneous = 1;
6234 else if (!TREE_CONSTANT (value))
6235 constructor_constant = 0;
6236 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6237 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6238 || TREE_CODE (constructor_type) == UNION_TYPE)
6239 && DECL_C_BIT_FIELD (field)
6240 && TREE_CODE (value) != INTEGER_CST))
6241 constructor_simple = 0;
6243 if (require_constant_value && ! TREE_CONSTANT (value))
6245 error_init ("initializer element is not constant");
6246 value = error_mark_node;
6248 else if (require_constant_elements
6249 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6250 pedwarn ("initializer element is not computable at load time");
6252 /* If this field is empty (and not at the end of structure),
6253 don't do anything other than checking the initializer. */
6254 if (field
6255 && (TREE_TYPE (field) == error_mark_node
6256 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6257 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6258 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6259 || TREE_CHAIN (field)))))
6260 return;
6262 value = digest_init (type, value, require_constant_value);
6263 if (value == error_mark_node)
6265 constructor_erroneous = 1;
6266 return;
6269 /* If this element doesn't come next in sequence,
6270 put it on constructor_pending_elts. */
6271 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6272 && (!constructor_incremental
6273 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6275 if (constructor_incremental
6276 && tree_int_cst_lt (field, constructor_unfilled_index))
6277 set_nonincremental_init ();
6279 add_pending_init (field, value);
6280 return;
6282 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6283 && (!constructor_incremental
6284 || field != constructor_unfilled_fields))
6286 /* We do this for records but not for unions. In a union,
6287 no matter which field is specified, it can be initialized
6288 right away since it starts at the beginning of the union. */
6289 if (constructor_incremental)
6291 if (!constructor_unfilled_fields)
6292 set_nonincremental_init ();
6293 else
6295 tree bitpos, unfillpos;
6297 bitpos = bit_position (field);
6298 unfillpos = bit_position (constructor_unfilled_fields);
6300 if (tree_int_cst_lt (bitpos, unfillpos))
6301 set_nonincremental_init ();
6305 add_pending_init (field, value);
6306 return;
6308 else if (TREE_CODE (constructor_type) == UNION_TYPE
6309 && constructor_elements)
6311 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6312 warning_init ("initialized field with side-effects overwritten");
6314 /* We can have just one union field set. */
6315 constructor_elements = 0;
6318 /* Otherwise, output this element either to
6319 constructor_elements or to the assembler file. */
6321 if (field && TREE_CODE (field) == INTEGER_CST)
6322 field = copy_node (field);
6323 constructor_elements
6324 = tree_cons (field, value, constructor_elements);
6326 /* Advance the variable that indicates sequential elements output. */
6327 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6328 constructor_unfilled_index
6329 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6330 bitsize_one_node);
6331 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6333 constructor_unfilled_fields
6334 = TREE_CHAIN (constructor_unfilled_fields);
6336 /* Skip any nameless bit fields. */
6337 while (constructor_unfilled_fields != 0
6338 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6339 && DECL_NAME (constructor_unfilled_fields) == 0)
6340 constructor_unfilled_fields =
6341 TREE_CHAIN (constructor_unfilled_fields);
6343 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6344 constructor_unfilled_fields = 0;
6346 /* Now output any pending elements which have become next. */
6347 if (pending)
6348 output_pending_init_elements (0);
6351 /* Output any pending elements which have become next.
6352 As we output elements, constructor_unfilled_{fields,index}
6353 advances, which may cause other elements to become next;
6354 if so, they too are output.
6356 If ALL is 0, we return when there are
6357 no more pending elements to output now.
6359 If ALL is 1, we output space as necessary so that
6360 we can output all the pending elements. */
6362 static void
6363 output_pending_init_elements (all)
6364 int all;
6366 struct init_node *elt = constructor_pending_elts;
6367 tree next;
6369 retry:
6371 /* Look thru the whole pending tree.
6372 If we find an element that should be output now,
6373 output it. Otherwise, set NEXT to the element
6374 that comes first among those still pending. */
6376 next = 0;
6377 while (elt)
6379 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6381 if (tree_int_cst_equal (elt->purpose,
6382 constructor_unfilled_index))
6383 output_init_element (elt->value,
6384 TREE_TYPE (constructor_type),
6385 constructor_unfilled_index, 0);
6386 else if (tree_int_cst_lt (constructor_unfilled_index,
6387 elt->purpose))
6389 /* Advance to the next smaller node. */
6390 if (elt->left)
6391 elt = elt->left;
6392 else
6394 /* We have reached the smallest node bigger than the
6395 current unfilled index. Fill the space first. */
6396 next = elt->purpose;
6397 break;
6400 else
6402 /* Advance to the next bigger node. */
6403 if (elt->right)
6404 elt = elt->right;
6405 else
6407 /* We have reached the biggest node in a subtree. Find
6408 the parent of it, which is the next bigger node. */
6409 while (elt->parent && elt->parent->right == elt)
6410 elt = elt->parent;
6411 elt = elt->parent;
6412 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6413 elt->purpose))
6415 next = elt->purpose;
6416 break;
6421 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6422 || TREE_CODE (constructor_type) == UNION_TYPE)
6424 tree ctor_unfilled_bitpos, elt_bitpos;
6426 /* If the current record is complete we are done. */
6427 if (constructor_unfilled_fields == 0)
6428 break;
6430 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6431 elt_bitpos = bit_position (elt->purpose);
6432 /* We can't compare fields here because there might be empty
6433 fields in between. */
6434 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6436 constructor_unfilled_fields = elt->purpose;
6437 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6438 elt->purpose, 0);
6440 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6442 /* Advance to the next smaller node. */
6443 if (elt->left)
6444 elt = elt->left;
6445 else
6447 /* We have reached the smallest node bigger than the
6448 current unfilled field. Fill the space first. */
6449 next = elt->purpose;
6450 break;
6453 else
6455 /* Advance to the next bigger node. */
6456 if (elt->right)
6457 elt = elt->right;
6458 else
6460 /* We have reached the biggest node in a subtree. Find
6461 the parent of it, which is the next bigger node. */
6462 while (elt->parent && elt->parent->right == elt)
6463 elt = elt->parent;
6464 elt = elt->parent;
6465 if (elt
6466 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6467 bit_position (elt->purpose))))
6469 next = elt->purpose;
6470 break;
6477 /* Ordinarily return, but not if we want to output all
6478 and there are elements left. */
6479 if (! (all && next != 0))
6480 return;
6482 /* If it's not incremental, just skip over the gap, so that after
6483 jumping to retry we will output the next successive element. */
6484 if (TREE_CODE (constructor_type) == RECORD_TYPE
6485 || TREE_CODE (constructor_type) == UNION_TYPE)
6486 constructor_unfilled_fields = next;
6487 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6488 constructor_unfilled_index = next;
6490 /* ELT now points to the node in the pending tree with the next
6491 initializer to output. */
6492 goto retry;
6495 /* Add one non-braced element to the current constructor level.
6496 This adjusts the current position within the constructor's type.
6497 This may also start or terminate implicit levels
6498 to handle a partly-braced initializer.
6500 Once this has found the correct level for the new element,
6501 it calls output_init_element. */
6503 void
6504 process_init_element (value)
6505 tree value;
6507 tree orig_value = value;
6508 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6510 designator_depth = 0;
6511 designator_errorneous = 0;
6513 /* Handle superfluous braces around string cst as in
6514 char x[] = {"foo"}; */
6515 if (string_flag
6516 && constructor_type
6517 && TREE_CODE (constructor_type) == ARRAY_TYPE
6518 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6519 && integer_zerop (constructor_unfilled_index))
6521 if (constructor_stack->replacement_value)
6522 error_init ("excess elements in char array initializer");
6523 constructor_stack->replacement_value = value;
6524 return;
6527 if (constructor_stack->replacement_value != 0)
6529 error_init ("excess elements in struct initializer");
6530 return;
6533 /* Ignore elements of a brace group if it is entirely superfluous
6534 and has already been diagnosed. */
6535 if (constructor_type == 0)
6536 return;
6538 /* If we've exhausted any levels that didn't have braces,
6539 pop them now. */
6540 while (constructor_stack->implicit)
6542 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6543 || TREE_CODE (constructor_type) == UNION_TYPE)
6544 && constructor_fields == 0)
6545 process_init_element (pop_init_level (1));
6546 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6547 && (constructor_max_index == 0
6548 || tree_int_cst_lt (constructor_max_index,
6549 constructor_index)))
6550 process_init_element (pop_init_level (1));
6551 else
6552 break;
6555 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6556 if (constructor_range_stack)
6558 /* If value is a compound literal and we'll be just using its
6559 content, don't put it into a SAVE_EXPR. */
6560 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
6561 || !require_constant_value
6562 || flag_isoc99)
6563 value = save_expr (value);
6566 while (1)
6568 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6570 tree fieldtype;
6571 enum tree_code fieldcode;
6573 if (constructor_fields == 0)
6575 pedwarn_init ("excess elements in struct initializer");
6576 break;
6579 fieldtype = TREE_TYPE (constructor_fields);
6580 if (fieldtype != error_mark_node)
6581 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6582 fieldcode = TREE_CODE (fieldtype);
6584 /* Error for non-static initialization of a flexible array member. */
6585 if (fieldcode == ARRAY_TYPE
6586 && !require_constant_value
6587 && TYPE_SIZE (fieldtype) == NULL_TREE
6588 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6590 error_init ("non-static initialization of a flexible array member");
6591 break;
6594 /* Accept a string constant to initialize a subarray. */
6595 if (value != 0
6596 && fieldcode == ARRAY_TYPE
6597 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6598 && string_flag)
6599 value = orig_value;
6600 /* Otherwise, if we have come to a subaggregate,
6601 and we don't have an element of its type, push into it. */
6602 else if (value != 0 && !constructor_no_implicit
6603 && value != error_mark_node
6604 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6605 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6606 || fieldcode == UNION_TYPE))
6608 push_init_level (1);
6609 continue;
6612 if (value)
6614 push_member_name (constructor_fields);
6615 output_init_element (value, fieldtype, constructor_fields, 1);
6616 RESTORE_SPELLING_DEPTH (constructor_depth);
6618 else
6619 /* Do the bookkeeping for an element that was
6620 directly output as a constructor. */
6622 /* For a record, keep track of end position of last field. */
6623 if (DECL_SIZE (constructor_fields))
6624 constructor_bit_index
6625 = size_binop (PLUS_EXPR,
6626 bit_position (constructor_fields),
6627 DECL_SIZE (constructor_fields));
6629 /* If the current field was the first one not yet written out,
6630 it isn't now, so update. */
6631 if (constructor_unfilled_fields == constructor_fields)
6633 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6634 /* Skip any nameless bit fields. */
6635 while (constructor_unfilled_fields != 0
6636 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6637 && DECL_NAME (constructor_unfilled_fields) == 0)
6638 constructor_unfilled_fields =
6639 TREE_CHAIN (constructor_unfilled_fields);
6643 constructor_fields = TREE_CHAIN (constructor_fields);
6644 /* Skip any nameless bit fields at the beginning. */
6645 while (constructor_fields != 0
6646 && DECL_C_BIT_FIELD (constructor_fields)
6647 && DECL_NAME (constructor_fields) == 0)
6648 constructor_fields = TREE_CHAIN (constructor_fields);
6650 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6652 tree fieldtype;
6653 enum tree_code fieldcode;
6655 if (constructor_fields == 0)
6657 pedwarn_init ("excess elements in union initializer");
6658 break;
6661 fieldtype = TREE_TYPE (constructor_fields);
6662 if (fieldtype != error_mark_node)
6663 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6664 fieldcode = TREE_CODE (fieldtype);
6666 /* Warn that traditional C rejects initialization of unions.
6667 We skip the warning if the value is zero. This is done
6668 under the assumption that the zero initializer in user
6669 code appears conditioned on e.g. __STDC__ to avoid
6670 "missing initializer" warnings and relies on default
6671 initialization to zero in the traditional C case.
6672 We also skip the warning if the initializer is designated,
6673 again on the assumption that this must be conditional on
6674 __STDC__ anyway (and we've already complained about the
6675 member-designator already). */
6676 if (warn_traditional && !in_system_header && !constructor_designated
6677 && !(value && (integer_zerop (value) || real_zerop (value))))
6678 warning ("traditional C rejects initialization of unions");
6680 /* Accept a string constant to initialize a subarray. */
6681 if (value != 0
6682 && fieldcode == ARRAY_TYPE
6683 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6684 && string_flag)
6685 value = orig_value;
6686 /* Otherwise, if we have come to a subaggregate,
6687 and we don't have an element of its type, push into it. */
6688 else if (value != 0 && !constructor_no_implicit
6689 && value != error_mark_node
6690 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6691 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6692 || fieldcode == UNION_TYPE))
6694 push_init_level (1);
6695 continue;
6698 if (value)
6700 push_member_name (constructor_fields);
6701 output_init_element (value, fieldtype, constructor_fields, 1);
6702 RESTORE_SPELLING_DEPTH (constructor_depth);
6704 else
6705 /* Do the bookkeeping for an element that was
6706 directly output as a constructor. */
6708 constructor_bit_index = DECL_SIZE (constructor_fields);
6709 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6712 constructor_fields = 0;
6714 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6716 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6717 enum tree_code eltcode = TREE_CODE (elttype);
6719 /* Accept a string constant to initialize a subarray. */
6720 if (value != 0
6721 && eltcode == ARRAY_TYPE
6722 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6723 && string_flag)
6724 value = orig_value;
6725 /* Otherwise, if we have come to a subaggregate,
6726 and we don't have an element of its type, push into it. */
6727 else if (value != 0 && !constructor_no_implicit
6728 && value != error_mark_node
6729 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6730 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6731 || eltcode == UNION_TYPE))
6733 push_init_level (1);
6734 continue;
6737 if (constructor_max_index != 0
6738 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6739 || integer_all_onesp (constructor_max_index)))
6741 pedwarn_init ("excess elements in array initializer");
6742 break;
6745 /* Now output the actual element. */
6746 if (value)
6748 push_array_bounds (tree_low_cst (constructor_index, 0));
6749 output_init_element (value, elttype, constructor_index, 1);
6750 RESTORE_SPELLING_DEPTH (constructor_depth);
6753 constructor_index
6754 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6756 if (! value)
6757 /* If we are doing the bookkeeping for an element that was
6758 directly output as a constructor, we must update
6759 constructor_unfilled_index. */
6760 constructor_unfilled_index = constructor_index;
6762 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6764 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6766 /* Do a basic check of initializer size. Note that vectors
6767 always have a fixed size derived from their type. */
6768 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6770 pedwarn_init ("excess elements in vector initializer");
6771 break;
6774 /* Now output the actual element. */
6775 if (value)
6776 output_init_element (value, elttype, constructor_index, 1);
6778 constructor_index
6779 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6781 if (! value)
6782 /* If we are doing the bookkeeping for an element that was
6783 directly output as a constructor, we must update
6784 constructor_unfilled_index. */
6785 constructor_unfilled_index = constructor_index;
6788 /* Handle the sole element allowed in a braced initializer
6789 for a scalar variable. */
6790 else if (constructor_fields == 0)
6792 pedwarn_init ("excess elements in scalar initializer");
6793 break;
6795 else
6797 if (value)
6798 output_init_element (value, constructor_type, NULL_TREE, 1);
6799 constructor_fields = 0;
6802 /* Handle range initializers either at this level or anywhere higher
6803 in the designator stack. */
6804 if (constructor_range_stack)
6806 struct constructor_range_stack *p, *range_stack;
6807 int finish = 0;
6809 range_stack = constructor_range_stack;
6810 constructor_range_stack = 0;
6811 while (constructor_stack != range_stack->stack)
6813 if (!constructor_stack->implicit)
6814 abort ();
6815 process_init_element (pop_init_level (1));
6817 for (p = range_stack;
6818 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6819 p = p->prev)
6821 if (!constructor_stack->implicit)
6822 abort ();
6823 process_init_element (pop_init_level (1));
6826 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6827 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6828 finish = 1;
6830 while (1)
6832 constructor_index = p->index;
6833 constructor_fields = p->fields;
6834 if (finish && p->range_end && p->index == p->range_start)
6836 finish = 0;
6837 p->prev = 0;
6839 p = p->next;
6840 if (!p)
6841 break;
6842 push_init_level (2);
6843 p->stack = constructor_stack;
6844 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6845 p->index = p->range_start;
6848 if (!finish)
6849 constructor_range_stack = range_stack;
6850 continue;
6853 break;
6856 constructor_range_stack = 0;
6859 /* Build a simple asm-statement, from one string literal. */
6860 tree
6861 simple_asm_stmt (expr)
6862 tree expr;
6864 STRIP_NOPS (expr);
6866 if (TREE_CODE (expr) == ADDR_EXPR)
6867 expr = TREE_OPERAND (expr, 0);
6869 if (TREE_CODE (expr) == STRING_CST)
6871 tree stmt;
6873 /* Simple asm statements are treated as volatile. */
6874 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6875 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6876 ASM_INPUT_P (stmt) = 1;
6877 return stmt;
6880 error ("argument of `asm' is not a constant string");
6881 return NULL_TREE;
6884 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6885 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6887 tree
6888 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6889 tree cv_qualifier;
6890 tree string;
6891 tree outputs;
6892 tree inputs;
6893 tree clobbers;
6895 tree tail;
6897 if (TREE_CODE (string) != STRING_CST)
6899 error ("asm template is not a string constant");
6900 return NULL_TREE;
6903 if (cv_qualifier != NULL_TREE
6904 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6906 warning ("%s qualifier ignored on asm",
6907 IDENTIFIER_POINTER (cv_qualifier));
6908 cv_qualifier = NULL_TREE;
6911 /* We can remove output conversions that change the type,
6912 but not the mode. */
6913 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6915 tree output = TREE_VALUE (tail);
6917 STRIP_NOPS (output);
6918 TREE_VALUE (tail) = output;
6920 /* Allow conversions as LHS here. build_modify_expr as called below
6921 will do the right thing with them. */
6922 while (TREE_CODE (output) == NOP_EXPR
6923 || TREE_CODE (output) == CONVERT_EXPR
6924 || TREE_CODE (output) == FLOAT_EXPR
6925 || TREE_CODE (output) == FIX_TRUNC_EXPR
6926 || TREE_CODE (output) == FIX_FLOOR_EXPR
6927 || TREE_CODE (output) == FIX_ROUND_EXPR
6928 || TREE_CODE (output) == FIX_CEIL_EXPR)
6929 output = TREE_OPERAND (output, 0);
6931 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6934 /* Remove output conversions that change the type but not the mode. */
6935 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6937 tree output = TREE_VALUE (tail);
6938 STRIP_NOPS (output);
6939 TREE_VALUE (tail) = output;
6942 /* Perform default conversions on array and function inputs.
6943 Don't do this for other types as it would screw up operands
6944 expected to be in memory. */
6945 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6946 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6948 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6949 outputs, inputs, clobbers));
6952 /* Expand an ASM statement with operands, handling output operands
6953 that are not variables or INDIRECT_REFS by transforming such
6954 cases into cases that expand_asm_operands can handle.
6956 Arguments are same as for expand_asm_operands. */
6958 void
6959 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6960 tree string, outputs, inputs, clobbers;
6961 int vol;
6962 const char *filename;
6963 int line;
6965 int noutputs = list_length (outputs);
6966 int i;
6967 /* o[I] is the place that output number I should be written. */
6968 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6969 tree tail;
6971 /* Record the contents of OUTPUTS before it is modified. */
6972 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6974 o[i] = TREE_VALUE (tail);
6975 if (o[i] == error_mark_node)
6976 return;
6979 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6980 OUTPUTS some trees for where the values were actually stored. */
6981 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6983 /* Copy all the intermediate outputs into the specified outputs. */
6984 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6986 if (o[i] != TREE_VALUE (tail))
6988 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6989 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6990 free_temp_slots ();
6992 /* Restore the original value so that it's correct the next
6993 time we expand this function. */
6994 TREE_VALUE (tail) = o[i];
6996 /* Detect modification of read-only values.
6997 (Otherwise done by build_modify_expr.) */
6998 else
7000 tree type = TREE_TYPE (o[i]);
7001 if (TREE_READONLY (o[i])
7002 || TYPE_READONLY (type)
7003 || ((TREE_CODE (type) == RECORD_TYPE
7004 || TREE_CODE (type) == UNION_TYPE)
7005 && C_TYPE_FIELDS_READONLY (type)))
7006 readonly_warning (o[i], "modification by `asm'");
7010 /* Those MODIFY_EXPRs could do autoincrements. */
7011 emit_queue ();
7014 /* Expand a C `return' statement.
7015 RETVAL is the expression for what to return,
7016 or a null pointer for `return;' with no value. */
7018 tree
7019 c_expand_return (retval)
7020 tree retval;
7022 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
7024 if (TREE_THIS_VOLATILE (current_function_decl))
7025 warning ("function declared `noreturn' has a `return' statement");
7027 if (!retval)
7029 current_function_returns_null = 1;
7030 if ((warn_return_type || flag_isoc99)
7031 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7032 pedwarn_c99 ("`return' with no value, in function returning non-void");
7034 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7036 current_function_returns_null = 1;
7037 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7038 pedwarn ("`return' with a value, in function returning void");
7040 else
7042 tree t = convert_for_assignment (valtype, retval, _("return"),
7043 NULL_TREE, NULL_TREE, 0);
7044 tree res = DECL_RESULT (current_function_decl);
7045 tree inner;
7047 current_function_returns_value = 1;
7048 if (t == error_mark_node)
7049 return NULL_TREE;
7051 inner = t = convert (TREE_TYPE (res), t);
7053 /* Strip any conversions, additions, and subtractions, and see if
7054 we are returning the address of a local variable. Warn if so. */
7055 while (1)
7057 switch (TREE_CODE (inner))
7059 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7060 case PLUS_EXPR:
7061 inner = TREE_OPERAND (inner, 0);
7062 continue;
7064 case MINUS_EXPR:
7065 /* If the second operand of the MINUS_EXPR has a pointer
7066 type (or is converted from it), this may be valid, so
7067 don't give a warning. */
7069 tree op1 = TREE_OPERAND (inner, 1);
7071 while (! POINTER_TYPE_P (TREE_TYPE (op1))
7072 && (TREE_CODE (op1) == NOP_EXPR
7073 || TREE_CODE (op1) == NON_LVALUE_EXPR
7074 || TREE_CODE (op1) == CONVERT_EXPR))
7075 op1 = TREE_OPERAND (op1, 0);
7077 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7078 break;
7080 inner = TREE_OPERAND (inner, 0);
7081 continue;
7084 case ADDR_EXPR:
7085 inner = TREE_OPERAND (inner, 0);
7087 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7088 inner = TREE_OPERAND (inner, 0);
7090 if (TREE_CODE (inner) == VAR_DECL
7091 && ! DECL_EXTERNAL (inner)
7092 && ! TREE_STATIC (inner)
7093 && DECL_CONTEXT (inner) == current_function_decl)
7094 warning ("function returns address of local variable");
7095 break;
7097 default:
7098 break;
7101 break;
7104 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7107 return add_stmt (build_return_stmt (retval));
7110 struct c_switch {
7111 /* The SWITCH_STMT being built. */
7112 tree switch_stmt;
7113 /* A splay-tree mapping the low element of a case range to the high
7114 element, or NULL_TREE if there is no high element. Used to
7115 determine whether or not a new case label duplicates an old case
7116 label. We need a tree, rather than simply a hash table, because
7117 of the GNU case range extension. */
7118 splay_tree cases;
7119 /* The next node on the stack. */
7120 struct c_switch *next;
7123 /* A stack of the currently active switch statements. The innermost
7124 switch statement is on the top of the stack. There is no need to
7125 mark the stack for garbage collection because it is only active
7126 during the processing of the body of a function, and we never
7127 collect at that point. */
7129 static struct c_switch *switch_stack;
7131 /* Start a C switch statement, testing expression EXP. Return the new
7132 SWITCH_STMT. */
7134 tree
7135 c_start_case (exp)
7136 tree exp;
7138 enum tree_code code;
7139 tree type, orig_type = error_mark_node;
7140 struct c_switch *cs;
7142 if (exp != error_mark_node)
7144 code = TREE_CODE (TREE_TYPE (exp));
7145 orig_type = TREE_TYPE (exp);
7147 if (! INTEGRAL_TYPE_P (orig_type)
7148 && code != ERROR_MARK)
7150 error ("switch quantity not an integer");
7151 exp = integer_zero_node;
7153 else
7155 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7157 if (warn_traditional && !in_system_header
7158 && (type == long_integer_type_node
7159 || type == long_unsigned_type_node))
7160 warning ("`long' switch expression not converted to `int' in ISO C");
7162 exp = default_conversion (exp);
7163 type = TREE_TYPE (exp);
7167 /* Add this new SWITCH_STMT to the stack. */
7168 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7169 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
7170 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7171 cs->next = switch_stack;
7172 switch_stack = cs;
7174 return add_stmt (switch_stack->switch_stmt);
7177 /* Process a case label. */
7179 tree
7180 do_case (low_value, high_value)
7181 tree low_value;
7182 tree high_value;
7184 tree label = NULL_TREE;
7186 if (switch_stack)
7188 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
7190 label = c_add_case_label (switch_stack->cases,
7191 SWITCH_COND (switch_stack->switch_stmt),
7192 low_value, high_value);
7193 if (label == error_mark_node)
7194 label = NULL_TREE;
7195 else if (switch_was_empty_p)
7197 /* Attach the first case label to the SWITCH_BODY. */
7198 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
7199 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
7202 else if (low_value)
7203 error ("case label not within a switch statement");
7204 else
7205 error ("`default' label not within a switch statement");
7207 return label;
7210 /* Finish the switch statement. */
7212 void
7213 c_finish_case ()
7215 struct c_switch *cs = switch_stack;
7217 /* Rechain the next statements to the SWITCH_STMT. */
7218 last_tree = cs->switch_stmt;
7220 /* Pop the stack. */
7221 switch_stack = switch_stack->next;
7222 splay_tree_delete (cs->cases);
7223 free (cs);