2002-02-19 Philip Blundell <philb@gnu.org>
[official-gcc.git] / gcc / c-typeck.c
blobdd8abdd11f75181e787a39176daa99e72dc901b5
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 "rtl.h"
35 #include "tree.h"
36 #include "c-tree.h"
37 #include "tm_p.h"
38 #include "flags.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "toplev.h"
42 #include "intl.h"
43 #include "ggc.h"
44 #include "target.h"
46 /* Nonzero if we've already printed a "missing braces around initializer"
47 message within this initializer. */
48 static int missing_braces_mentioned;
50 /* 1 if we explained undeclared var errors. */
51 static int undeclared_variable_notice;
53 static tree qualify_type PARAMS ((tree, tree));
54 static int comp_target_types PARAMS ((tree, tree));
55 static int function_types_compatible_p PARAMS ((tree, tree));
56 static int type_lists_compatible_p PARAMS ((tree, tree));
57 static tree decl_constant_value_for_broken_optimization PARAMS ((tree));
58 static tree default_function_array_conversion PARAMS ((tree));
59 static tree lookup_field PARAMS ((tree, tree));
60 static tree convert_arguments PARAMS ((tree, tree, tree, tree));
61 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
62 static tree pointer_diff PARAMS ((tree, tree));
63 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree, int));
64 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
65 static tree internal_build_compound_expr PARAMS ((tree, int));
66 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
67 tree, tree, int));
68 static void warn_for_assignment PARAMS ((const char *, const char *,
69 tree, int));
70 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
71 static void push_string PARAMS ((const char *));
72 static void push_member_name PARAMS ((tree));
73 static void push_array_bounds PARAMS ((int));
74 static int spelling_length PARAMS ((void));
75 static char *print_spelling PARAMS ((char *));
76 static void warning_init PARAMS ((const char *));
77 static tree digest_init PARAMS ((tree, tree, int, int));
78 static void output_init_element PARAMS ((tree, tree, tree, int));
79 static void output_pending_init_elements PARAMS ((int));
80 static int set_designator PARAMS ((int));
81 static void push_range_stack PARAMS ((tree));
82 static void add_pending_init PARAMS ((tree, tree));
83 static void set_nonincremental_init PARAMS ((void));
84 static void set_nonincremental_init_from_string PARAMS ((tree));
85 static tree find_init_member PARAMS ((tree));
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.) */
90 tree
91 require_complete_type (value)
92 tree value;
94 tree type = TREE_TYPE (value);
96 if (value == error_mark_node || type == error_mark_node)
97 return error_mark_node;
99 /* First, detect a valid value with a complete type. */
100 if (COMPLETE_TYPE_P (type))
101 return value;
103 incomplete_type_error (value, type);
104 return error_mark_node;
107 /* Print an error message for invalid use of an incomplete type.
108 VALUE is the expression that was used (or 0 if that isn't known)
109 and TYPE is the type that was invalid. */
111 void
112 incomplete_type_error (value, type)
113 tree value;
114 tree type;
116 const char *type_code_string;
118 /* Avoid duplicate error message. */
119 if (TREE_CODE (type) == ERROR_MARK)
120 return;
122 if (value != 0 && (TREE_CODE (value) == VAR_DECL
123 || TREE_CODE (value) == PARM_DECL))
124 error ("`%s' has an incomplete type",
125 IDENTIFIER_POINTER (DECL_NAME (value)));
126 else
128 retry:
129 /* We must print an error message. Be clever about what it says. */
131 switch (TREE_CODE (type))
133 case RECORD_TYPE:
134 type_code_string = "struct";
135 break;
137 case UNION_TYPE:
138 type_code_string = "union";
139 break;
141 case ENUMERAL_TYPE:
142 type_code_string = "enum";
143 break;
145 case VOID_TYPE:
146 error ("invalid use of void expression");
147 return;
149 case ARRAY_TYPE:
150 if (TYPE_DOMAIN (type))
152 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
154 error ("invalid use of flexible array member");
155 return;
157 type = TREE_TYPE (type);
158 goto retry;
160 error ("invalid use of array with unspecified bounds");
161 return;
163 default:
164 abort ();
167 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
168 error ("invalid use of undefined type `%s %s'",
169 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
170 else
171 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
172 error ("invalid use of incomplete typedef `%s'",
173 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
177 /* Return a variant of TYPE which has all the type qualifiers of LIKE
178 as well as those of TYPE. */
180 static tree
181 qualify_type (type, like)
182 tree type, like;
184 return c_build_qualified_type (type,
185 TYPE_QUALS (type) | TYPE_QUALS (like));
188 /* Return the common type of two types.
189 We assume that comptypes has already been done and returned 1;
190 if that isn't so, this may crash. In particular, we assume that qualifiers
191 match.
193 This is the type for the result of most arithmetic operations
194 if the operands have the given two types. */
196 tree
197 common_type (t1, t2)
198 tree t1, t2;
200 enum tree_code code1;
201 enum tree_code code2;
202 tree attributes;
204 /* Save time if the two types are the same. */
206 if (t1 == t2) return t1;
208 /* If one type is nonsense, use the other. */
209 if (t1 == error_mark_node)
210 return t2;
211 if (t2 == error_mark_node)
212 return t1;
214 /* Merge the attributes. */
215 attributes = (*targetm.merge_type_attributes) (t1, t2);
217 /* Treat an enum type as the unsigned integer type of the same width. */
219 if (TREE_CODE (t1) == ENUMERAL_TYPE)
220 t1 = type_for_size (TYPE_PRECISION (t1), 1);
221 if (TREE_CODE (t2) == ENUMERAL_TYPE)
222 t2 = type_for_size (TYPE_PRECISION (t2), 1);
224 code1 = TREE_CODE (t1);
225 code2 = TREE_CODE (t2);
227 /* If one type is complex, form the common type of the non-complex
228 components, then make that complex. Use T1 or T2 if it is the
229 required type. */
230 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
232 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
233 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
234 tree subtype = common_type (subtype1, subtype2);
236 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
237 return build_type_attribute_variant (t1, attributes);
238 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
239 return build_type_attribute_variant (t2, attributes);
240 else
241 return build_type_attribute_variant (build_complex_type (subtype),
242 attributes);
245 switch (code1)
247 case INTEGER_TYPE:
248 case REAL_TYPE:
249 /* If only one is real, use it as the result. */
251 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
252 return build_type_attribute_variant (t1, attributes);
254 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
255 return build_type_attribute_variant (t2, attributes);
257 /* Both real or both integers; use the one with greater precision. */
259 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
260 return build_type_attribute_variant (t1, attributes);
261 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
262 return build_type_attribute_variant (t2, attributes);
264 /* Same precision. Prefer longs to ints even when same size. */
266 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
267 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
268 return build_type_attribute_variant (long_unsigned_type_node,
269 attributes);
271 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
272 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
274 /* But preserve unsignedness from the other type,
275 since long cannot hold all the values of an unsigned int. */
276 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
277 t1 = long_unsigned_type_node;
278 else
279 t1 = long_integer_type_node;
280 return build_type_attribute_variant (t1, attributes);
283 /* Likewise, prefer long double to double even if same size. */
284 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
285 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
286 return build_type_attribute_variant (long_double_type_node,
287 attributes);
289 /* Otherwise prefer the unsigned one. */
291 if (TREE_UNSIGNED (t1))
292 return build_type_attribute_variant (t1, attributes);
293 else
294 return build_type_attribute_variant (t2, attributes);
296 case POINTER_TYPE:
297 /* For two pointers, do this recursively on the target type,
298 and combine the qualifiers of the two types' targets. */
299 /* This code was turned off; I don't know why.
300 But ANSI C specifies doing this with the qualifiers.
301 So I turned it on again. */
303 tree pointed_to_1 = TREE_TYPE (t1);
304 tree pointed_to_2 = TREE_TYPE (t2);
305 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
306 TYPE_MAIN_VARIANT (pointed_to_2));
307 t1 = build_pointer_type (c_build_qualified_type
308 (target,
309 TYPE_QUALS (pointed_to_1) |
310 TYPE_QUALS (pointed_to_2)));
311 return build_type_attribute_variant (t1, attributes);
313 #if 0
314 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
315 return build_type_attribute_variant (t1, attributes);
316 #endif
318 case ARRAY_TYPE:
320 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
321 /* Save space: see if the result is identical to one of the args. */
322 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
323 return build_type_attribute_variant (t1, attributes);
324 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
325 return build_type_attribute_variant (t2, attributes);
326 /* Merge the element types, and have a size if either arg has one. */
327 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
328 return build_type_attribute_variant (t1, attributes);
331 case FUNCTION_TYPE:
332 /* Function types: prefer the one that specified arg types.
333 If both do, merge the arg types. Also merge the return types. */
335 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
336 tree p1 = TYPE_ARG_TYPES (t1);
337 tree p2 = TYPE_ARG_TYPES (t2);
338 int len;
339 tree newargs, n;
340 int i;
342 /* Save space: see if the result is identical to one of the args. */
343 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
344 return build_type_attribute_variant (t1, attributes);
345 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
346 return build_type_attribute_variant (t2, attributes);
348 /* Simple way if one arg fails to specify argument types. */
349 if (TYPE_ARG_TYPES (t1) == 0)
351 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
352 return build_type_attribute_variant (t1, attributes);
354 if (TYPE_ARG_TYPES (t2) == 0)
356 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
357 return build_type_attribute_variant (t1, attributes);
360 /* If both args specify argument types, we must merge the two
361 lists, argument by argument. */
363 pushlevel (0);
364 declare_parm_level (1);
366 len = list_length (p1);
367 newargs = 0;
369 for (i = 0; i < len; i++)
370 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
372 n = newargs;
374 for (; p1;
375 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
377 /* A null type means arg type is not specified.
378 Take whatever the other function type has. */
379 if (TREE_VALUE (p1) == 0)
381 TREE_VALUE (n) = TREE_VALUE (p2);
382 goto parm_done;
384 if (TREE_VALUE (p2) == 0)
386 TREE_VALUE (n) = TREE_VALUE (p1);
387 goto parm_done;
390 /* Given wait (union {union wait *u; int *i} *)
391 and wait (union wait *),
392 prefer union wait * as type of parm. */
393 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
394 && TREE_VALUE (p1) != TREE_VALUE (p2))
396 tree memb;
397 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
398 memb; memb = TREE_CHAIN (memb))
399 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
401 TREE_VALUE (n) = TREE_VALUE (p2);
402 if (pedantic)
403 pedwarn ("function types not truly compatible in ISO C");
404 goto parm_done;
407 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
408 && TREE_VALUE (p2) != TREE_VALUE (p1))
410 tree memb;
411 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
412 memb; memb = TREE_CHAIN (memb))
413 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
415 TREE_VALUE (n) = TREE_VALUE (p1);
416 if (pedantic)
417 pedwarn ("function types not truly compatible in ISO C");
418 goto parm_done;
421 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
422 parm_done: ;
425 poplevel (0, 0, 0);
427 t1 = build_function_type (valtype, newargs);
428 /* ... falls through ... */
431 default:
432 return build_type_attribute_variant (t1, attributes);
437 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
438 or various other operations. Return 2 if they are compatible
439 but a warning may be needed if you use them together. */
442 comptypes (type1, type2)
443 tree type1, type2;
445 tree t1 = type1;
446 tree t2 = type2;
447 int attrval, val;
449 /* Suppress errors caused by previously reported errors. */
451 if (t1 == t2 || !t1 || !t2
452 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
453 return 1;
455 /* If either type is the internal version of sizetype, return the
456 language version. */
457 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
458 && TYPE_DOMAIN (t1) != 0)
459 t1 = TYPE_DOMAIN (t1);
461 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
462 && TYPE_DOMAIN (t2) != 0)
463 t2 = TYPE_DOMAIN (t2);
465 /* Treat an enum type as the integer type of the same width and
466 signedness. */
468 if (TREE_CODE (t1) == ENUMERAL_TYPE)
469 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
470 if (TREE_CODE (t2) == ENUMERAL_TYPE)
471 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
473 if (t1 == t2)
474 return 1;
476 /* Different classes of types can't be compatible. */
478 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
480 /* Qualifiers must match. */
482 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
483 return 0;
485 /* Allow for two different type nodes which have essentially the same
486 definition. Note that we already checked for equality of the type
487 qualifiers (just above). */
489 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
490 return 1;
492 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
493 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
494 return 0;
496 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
497 val = 0;
499 switch (TREE_CODE (t1))
501 case POINTER_TYPE:
502 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
503 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
504 break;
506 case FUNCTION_TYPE:
507 val = function_types_compatible_p (t1, t2);
508 break;
510 case ARRAY_TYPE:
512 tree d1 = TYPE_DOMAIN (t1);
513 tree d2 = TYPE_DOMAIN (t2);
514 bool d1_variable, d2_variable;
515 bool d1_zero, d2_zero;
516 val = 1;
518 /* Target types must match incl. qualifiers. */
519 if (TREE_TYPE (t1) != TREE_TYPE (t2)
520 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
521 return 0;
523 /* Sizes must match unless one is missing or variable. */
524 if (d1 == 0 || d2 == 0 || d1 == d2)
525 break;
527 d1_zero = ! TYPE_MAX_VALUE (d1);
528 d2_zero = ! TYPE_MAX_VALUE (d2);
530 d1_variable = (! d1_zero
531 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
532 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
533 d2_variable = (! d2_zero
534 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
535 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
537 if (d1_variable || d2_variable)
538 break;
539 if (d1_zero && d2_zero)
540 break;
541 if (d1_zero || d2_zero
542 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
543 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
544 val = 0;
546 break;
549 case RECORD_TYPE:
550 if (maybe_objc_comptypes (t1, t2, 0) == 1)
551 val = 1;
552 break;
554 default:
555 break;
557 return attrval == 2 && val == 1 ? 2 : val;
560 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
561 ignoring their qualifiers. */
563 static int
564 comp_target_types (ttl, ttr)
565 tree ttl, ttr;
567 int val;
569 /* Give maybe_objc_comptypes a crack at letting these types through. */
570 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
571 return val;
573 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
574 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
576 if (val == 2 && pedantic)
577 pedwarn ("types are not quite compatible");
578 return val;
581 /* Subroutines of `comptypes'. */
583 /* Return 1 if two function types F1 and F2 are compatible.
584 If either type specifies no argument types,
585 the other must specify a fixed number of self-promoting arg types.
586 Otherwise, if one type specifies only the number of arguments,
587 the other must specify that number of self-promoting arg types.
588 Otherwise, the argument types must match. */
590 static int
591 function_types_compatible_p (f1, f2)
592 tree f1, f2;
594 tree args1, args2;
595 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
596 int val = 1;
597 int val1;
599 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
600 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
601 return 0;
603 args1 = TYPE_ARG_TYPES (f1);
604 args2 = TYPE_ARG_TYPES (f2);
606 /* An unspecified parmlist matches any specified parmlist
607 whose argument types don't need default promotions. */
609 if (args1 == 0)
611 if (!self_promoting_args_p (args2))
612 return 0;
613 /* If one of these types comes from a non-prototype fn definition,
614 compare that with the other type's arglist.
615 If they don't match, ask for a warning (but no error). */
616 if (TYPE_ACTUAL_ARG_TYPES (f1)
617 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
618 val = 2;
619 return val;
621 if (args2 == 0)
623 if (!self_promoting_args_p (args1))
624 return 0;
625 if (TYPE_ACTUAL_ARG_TYPES (f2)
626 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
627 val = 2;
628 return val;
631 /* Both types have argument lists: compare them and propagate results. */
632 val1 = type_lists_compatible_p (args1, args2);
633 return val1 != 1 ? val1 : val;
636 /* Check two lists of types for compatibility,
637 returning 0 for incompatible, 1 for compatible,
638 or 2 for compatible with warning. */
640 static int
641 type_lists_compatible_p (args1, args2)
642 tree args1, args2;
644 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
645 int val = 1;
646 int newval = 0;
648 while (1)
650 if (args1 == 0 && args2 == 0)
651 return val;
652 /* If one list is shorter than the other,
653 they fail to match. */
654 if (args1 == 0 || args2 == 0)
655 return 0;
656 /* A null pointer instead of a type
657 means there is supposed to be an argument
658 but nothing is specified about what type it has.
659 So match anything that self-promotes. */
660 if (TREE_VALUE (args1) == 0)
662 if (simple_type_promotes_to (TREE_VALUE (args2)) != NULL_TREE)
663 return 0;
665 else if (TREE_VALUE (args2) == 0)
667 if (simple_type_promotes_to (TREE_VALUE (args1)) != NULL_TREE)
668 return 0;
670 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
671 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
673 /* Allow wait (union {union wait *u; int *i} *)
674 and wait (union wait *) to be compatible. */
675 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
676 && (TYPE_NAME (TREE_VALUE (args1)) == 0
677 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
678 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
679 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
680 TYPE_SIZE (TREE_VALUE (args2))))
682 tree memb;
683 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
684 memb; memb = TREE_CHAIN (memb))
685 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
686 break;
687 if (memb == 0)
688 return 0;
690 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
691 && (TYPE_NAME (TREE_VALUE (args2)) == 0
692 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
693 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
694 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
695 TYPE_SIZE (TREE_VALUE (args1))))
697 tree memb;
698 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
699 memb; memb = TREE_CHAIN (memb))
700 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
701 break;
702 if (memb == 0)
703 return 0;
705 else
706 return 0;
709 /* comptypes said ok, but record if it said to warn. */
710 if (newval > val)
711 val = newval;
713 args1 = TREE_CHAIN (args1);
714 args2 = TREE_CHAIN (args2);
718 /* Compute the value of the `sizeof' operator. */
720 tree
721 c_sizeof (type)
722 tree type;
724 enum tree_code code = TREE_CODE (type);
725 tree size;
727 if (code == FUNCTION_TYPE)
729 if (pedantic || warn_pointer_arith)
730 pedwarn ("sizeof applied to a function type");
731 size = size_one_node;
733 else if (code == VOID_TYPE)
735 if (pedantic || warn_pointer_arith)
736 pedwarn ("sizeof applied to a void type");
737 size = size_one_node;
739 else if (code == ERROR_MARK)
740 size = size_one_node;
741 else if (!COMPLETE_TYPE_P (type))
743 error ("sizeof applied to an incomplete type");
744 size = size_zero_node;
746 else
747 /* Convert in case a char is more than one unit. */
748 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
749 size_int (TYPE_PRECISION (char_type_node)
750 / BITS_PER_UNIT));
752 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
753 TYPE_IS_SIZETYPE means that certain things (like overflow) will
754 never happen. However, this node should really have type
755 `size_t', which is just a typedef for an ordinary integer type. */
756 return fold (build1 (NOP_EXPR, c_size_type_node, size));
759 tree
760 c_sizeof_nowarn (type)
761 tree type;
763 enum tree_code code = TREE_CODE (type);
764 tree size;
766 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
767 size = size_one_node;
768 else if (!COMPLETE_TYPE_P (type))
769 size = size_zero_node;
770 else
771 /* Convert in case a char is more than one unit. */
772 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
773 size_int (TYPE_PRECISION (char_type_node)
774 / BITS_PER_UNIT));
776 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
777 TYPE_IS_SIZETYPE means that certain things (like overflow) will
778 never happen. However, this node should really have type
779 `size_t', which is just a typedef for an ordinary integer type. */
780 return fold (build1 (NOP_EXPR, c_size_type_node, size));
783 /* Compute the size to increment a pointer by. */
785 tree
786 c_size_in_bytes (type)
787 tree type;
789 enum tree_code code = TREE_CODE (type);
791 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
792 return size_one_node;
794 if (!COMPLETE_OR_VOID_TYPE_P (type))
796 error ("arithmetic on pointer to an incomplete type");
797 return size_one_node;
800 /* Convert in case a char is more than one unit. */
801 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
802 size_int (TYPE_PRECISION (char_type_node)
803 / BITS_PER_UNIT));
806 /* Return either DECL or its known constant value (if it has one). */
808 tree
809 decl_constant_value (decl)
810 tree decl;
812 if (/* Don't change a variable array bound or initial value to a constant
813 in a place where a variable is invalid. */
814 current_function_decl != 0
815 && ! TREE_THIS_VOLATILE (decl)
816 && TREE_READONLY (decl)
817 && DECL_INITIAL (decl) != 0
818 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
819 /* This is invalid if initial value is not constant.
820 If it has either a function call, a memory reference,
821 or a variable, then re-evaluating it could give different results. */
822 && TREE_CONSTANT (DECL_INITIAL (decl))
823 /* Check for cases where this is sub-optimal, even though valid. */
824 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
825 return DECL_INITIAL (decl);
826 return decl;
829 /* Return either DECL or its known constant value (if it has one), but
830 return DECL if pedantic or DECL has mode BLKmode. This is for
831 bug-compatibility with the old behavior of decl_constant_value
832 (before GCC 3.0); every use of this function is a bug and it should
833 be removed before GCC 3.1. It is not appropriate to use pedantic
834 in a way that affects optimization, and BLKmode is probably not the
835 right test for avoiding misoptimizations either. */
837 static tree
838 decl_constant_value_for_broken_optimization (decl)
839 tree decl;
841 if (pedantic || DECL_MODE (decl) == BLKmode)
842 return decl;
843 else
844 return decl_constant_value (decl);
848 /* Perform the default conversion of arrays and functions to pointers.
849 Return the result of converting EXP. For any other expression, just
850 return EXP. */
852 static tree
853 default_function_array_conversion (exp)
854 tree exp;
856 tree orig_exp;
857 tree type = TREE_TYPE (exp);
858 enum tree_code code = TREE_CODE (type);
859 int not_lvalue = 0;
861 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
862 an lvalue.
864 Do not use STRIP_NOPS here! It will remove conversions from pointer
865 to integer and cause infinite recursion. */
866 orig_exp = exp;
867 while (TREE_CODE (exp) == NON_LVALUE_EXPR
868 || (TREE_CODE (exp) == NOP_EXPR
869 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
871 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
872 not_lvalue = 1;
873 exp = TREE_OPERAND (exp, 0);
876 /* Preserve the original expression code. */
877 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
878 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
880 if (code == FUNCTION_TYPE)
882 return build_unary_op (ADDR_EXPR, exp, 0);
884 if (code == ARRAY_TYPE)
886 tree adr;
887 tree restype = TREE_TYPE (type);
888 tree ptrtype;
889 int constp = 0;
890 int volatilep = 0;
891 int lvalue_array_p;
893 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
895 constp = TREE_READONLY (exp);
896 volatilep = TREE_THIS_VOLATILE (exp);
899 if (TYPE_QUALS (type) || constp || volatilep)
900 restype
901 = c_build_qualified_type (restype,
902 TYPE_QUALS (type)
903 | (constp * TYPE_QUAL_CONST)
904 | (volatilep * TYPE_QUAL_VOLATILE));
906 if (TREE_CODE (exp) == INDIRECT_REF)
907 return convert (TYPE_POINTER_TO (restype),
908 TREE_OPERAND (exp, 0));
910 if (TREE_CODE (exp) == COMPOUND_EXPR)
912 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
913 return build (COMPOUND_EXPR, TREE_TYPE (op1),
914 TREE_OPERAND (exp, 0), op1);
917 lvalue_array_p = !not_lvalue && lvalue_p (exp);
918 if (!flag_isoc99 && !lvalue_array_p)
920 /* Before C99, non-lvalue arrays do not decay to pointers.
921 Normally, using such an array would be invalid; but it can
922 be used correctly inside sizeof or as a statement expression.
923 Thus, do not give an error here; an error will result later. */
924 return exp;
927 ptrtype = build_pointer_type (restype);
929 if (TREE_CODE (exp) == VAR_DECL)
931 /* ??? This is not really quite correct
932 in that the type of the operand of ADDR_EXPR
933 is not the target type of the type of the ADDR_EXPR itself.
934 Question is, can this lossage be avoided? */
935 adr = build1 (ADDR_EXPR, ptrtype, exp);
936 if (mark_addressable (exp) == 0)
937 return error_mark_node;
938 TREE_CONSTANT (adr) = staticp (exp);
939 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
940 return adr;
942 /* This way is better for a COMPONENT_REF since it can
943 simplify the offset for a component. */
944 adr = build_unary_op (ADDR_EXPR, exp, 1);
945 return convert (ptrtype, adr);
947 return exp;
950 /* Perform default promotions for C data used in expressions.
951 Arrays and functions are converted to pointers;
952 enumeral types or short or char, to int.
953 In addition, manifest constants symbols are replaced by their values. */
955 tree
956 default_conversion (exp)
957 tree exp;
959 tree orig_exp;
960 tree type = TREE_TYPE (exp);
961 enum tree_code code = TREE_CODE (type);
963 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
964 return default_function_array_conversion (exp);
966 /* Constants can be used directly unless they're not loadable. */
967 if (TREE_CODE (exp) == CONST_DECL)
968 exp = DECL_INITIAL (exp);
970 /* Replace a nonvolatile const static variable with its value unless
971 it is an array, in which case we must be sure that taking the
972 address of the array produces consistent results. */
973 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
975 exp = decl_constant_value_for_broken_optimization (exp);
976 type = TREE_TYPE (exp);
979 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
980 an lvalue.
982 Do not use STRIP_NOPS here! It will remove conversions from pointer
983 to integer and cause infinite recursion. */
984 orig_exp = exp;
985 while (TREE_CODE (exp) == NON_LVALUE_EXPR
986 || (TREE_CODE (exp) == NOP_EXPR
987 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
988 exp = TREE_OPERAND (exp, 0);
990 /* Preserve the original expression code. */
991 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
992 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
994 /* Normally convert enums to int,
995 but convert wide enums to something wider. */
996 if (code == ENUMERAL_TYPE)
998 type = type_for_size (MAX (TYPE_PRECISION (type),
999 TYPE_PRECISION (integer_type_node)),
1000 ((flag_traditional
1001 || (TYPE_PRECISION (type)
1002 >= TYPE_PRECISION (integer_type_node)))
1003 && TREE_UNSIGNED (type)));
1005 return convert (type, exp);
1008 if (TREE_CODE (exp) == COMPONENT_REF
1009 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1010 /* If it's thinner than an int, promote it like a
1011 c_promoting_integer_type_p, otherwise leave it alone. */
1012 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1013 TYPE_PRECISION (integer_type_node)))
1014 return convert (flag_traditional && TREE_UNSIGNED (type)
1015 ? unsigned_type_node : integer_type_node,
1016 exp);
1018 if (c_promoting_integer_type_p (type))
1020 /* Traditionally, unsignedness is preserved in default promotions.
1021 Also preserve unsignedness if not really getting any wider. */
1022 if (TREE_UNSIGNED (type)
1023 && (flag_traditional
1024 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1025 return convert (unsigned_type_node, exp);
1027 return convert (integer_type_node, exp);
1030 if (flag_traditional && !flag_allow_single_precision
1031 && TYPE_MAIN_VARIANT (type) == float_type_node)
1032 return convert (double_type_node, exp);
1034 if (code == VOID_TYPE)
1036 error ("void value not ignored as it ought to be");
1037 return error_mark_node;
1039 return exp;
1042 /* Look up COMPONENT in a structure or union DECL.
1044 If the component name is not found, returns NULL_TREE. Otherwise,
1045 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1046 stepping down the chain to the component, which is in the last
1047 TREE_VALUE of the list. Normally the list is of length one, but if
1048 the component is embedded within (nested) anonymous structures or
1049 unions, the list steps down the chain to the component. */
1051 static tree
1052 lookup_field (decl, component)
1053 tree decl, component;
1055 tree type = TREE_TYPE (decl);
1056 tree field;
1058 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1059 to the field elements. Use a binary search on this array to quickly
1060 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1061 will always be set for structures which have many elements. */
1063 if (TYPE_LANG_SPECIFIC (type))
1065 int bot, top, half;
1066 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1068 field = TYPE_FIELDS (type);
1069 bot = 0;
1070 top = TYPE_LANG_SPECIFIC (type)->len;
1071 while (top - bot > 1)
1073 half = (top - bot + 1) >> 1;
1074 field = field_array[bot+half];
1076 if (DECL_NAME (field) == NULL_TREE)
1078 /* Step through all anon unions in linear fashion. */
1079 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1081 field = field_array[bot++];
1082 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1083 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1085 tree anon = lookup_field (field, component);
1087 if (anon)
1088 return tree_cons (NULL_TREE, field, anon);
1092 /* Entire record is only anon unions. */
1093 if (bot > top)
1094 return NULL_TREE;
1096 /* Restart the binary search, with new lower bound. */
1097 continue;
1100 if (DECL_NAME (field) == component)
1101 break;
1102 if (DECL_NAME (field) < component)
1103 bot += half;
1104 else
1105 top = bot + half;
1108 if (DECL_NAME (field_array[bot]) == component)
1109 field = field_array[bot];
1110 else if (DECL_NAME (field) != component)
1111 return NULL_TREE;
1113 else
1115 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1117 if (DECL_NAME (field) == NULL_TREE
1118 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1119 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1121 tree anon = lookup_field (field, component);
1123 if (anon)
1124 return tree_cons (NULL_TREE, field, anon);
1127 if (DECL_NAME (field) == component)
1128 break;
1131 if (field == NULL_TREE)
1132 return NULL_TREE;
1135 return tree_cons (NULL_TREE, field, NULL_TREE);
1138 /* Make an expression to refer to the COMPONENT field of
1139 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1141 tree
1142 build_component_ref (datum, component)
1143 tree datum, component;
1145 tree type = TREE_TYPE (datum);
1146 enum tree_code code = TREE_CODE (type);
1147 tree field = NULL;
1148 tree ref;
1150 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1151 If pedantic ensure that the arguments are not lvalues; otherwise,
1152 if the component is an array, it would wrongly decay to a pointer in
1153 C89 mode.
1154 We cannot do this with a COND_EXPR, because in a conditional expression
1155 the default promotions are applied to both sides, and this would yield
1156 the wrong type of the result; for example, if the components have
1157 type "char". */
1158 switch (TREE_CODE (datum))
1160 case COMPOUND_EXPR:
1162 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1163 return build (COMPOUND_EXPR, TREE_TYPE (value),
1164 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1166 default:
1167 break;
1170 /* See if there is a field or component with name COMPONENT. */
1172 if (code == RECORD_TYPE || code == UNION_TYPE)
1174 if (!COMPLETE_TYPE_P (type))
1176 incomplete_type_error (NULL_TREE, type);
1177 return error_mark_node;
1180 field = lookup_field (datum, component);
1182 if (!field)
1184 error ("%s has no member named `%s'",
1185 code == RECORD_TYPE ? "structure" : "union",
1186 IDENTIFIER_POINTER (component));
1187 return error_mark_node;
1190 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1191 This might be better solved in future the way the C++ front
1192 end does it - by giving the anonymous entities each a
1193 separate name and type, and then have build_component_ref
1194 recursively call itself. We can't do that here. */
1195 for (; field; field = TREE_CHAIN (field))
1197 tree subdatum = TREE_VALUE (field);
1199 if (TREE_TYPE (subdatum) == error_mark_node)
1200 return error_mark_node;
1202 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1203 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1204 TREE_READONLY (ref) = 1;
1205 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1206 TREE_THIS_VOLATILE (ref) = 1;
1208 if (TREE_DEPRECATED (subdatum))
1209 warn_deprecated_use (subdatum);
1211 datum = ref;
1214 return ref;
1216 else if (code != ERROR_MARK)
1217 error ("request for member `%s' in something not a structure or union",
1218 IDENTIFIER_POINTER (component));
1220 return error_mark_node;
1223 /* Given an expression PTR for a pointer, return an expression
1224 for the value pointed to.
1225 ERRORSTRING is the name of the operator to appear in error messages. */
1227 tree
1228 build_indirect_ref (ptr, errorstring)
1229 tree ptr;
1230 const char *errorstring;
1232 tree pointer = default_conversion (ptr);
1233 tree type = TREE_TYPE (pointer);
1235 if (TREE_CODE (type) == POINTER_TYPE)
1237 if (TREE_CODE (pointer) == ADDR_EXPR
1238 && !flag_volatile
1239 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1240 == TREE_TYPE (type)))
1241 return TREE_OPERAND (pointer, 0);
1242 else
1244 tree t = TREE_TYPE (type);
1245 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1247 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1249 error ("dereferencing pointer to incomplete type");
1250 return error_mark_node;
1252 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1253 warning ("dereferencing `void *' pointer");
1255 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1256 so that we get the proper error message if the result is used
1257 to assign to. Also, &* is supposed to be a no-op.
1258 And ANSI C seems to specify that the type of the result
1259 should be the const type. */
1260 /* A de-reference of a pointer to const is not a const. It is valid
1261 to change it via some other pointer. */
1262 TREE_READONLY (ref) = TYPE_READONLY (t);
1263 TREE_SIDE_EFFECTS (ref)
1264 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1265 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1266 return ref;
1269 else if (TREE_CODE (pointer) != ERROR_MARK)
1270 error ("invalid type argument of `%s'", errorstring);
1271 return error_mark_node;
1274 /* This handles expressions of the form "a[i]", which denotes
1275 an array reference.
1277 This is logically equivalent in C to *(a+i), but we may do it differently.
1278 If A is a variable or a member, we generate a primitive ARRAY_REF.
1279 This avoids forcing the array out of registers, and can work on
1280 arrays that are not lvalues (for example, members of structures returned
1281 by functions). */
1283 tree
1284 build_array_ref (array, index)
1285 tree array, index;
1287 if (index == 0)
1289 error ("subscript missing in array reference");
1290 return error_mark_node;
1293 if (TREE_TYPE (array) == error_mark_node
1294 || TREE_TYPE (index) == error_mark_node)
1295 return error_mark_node;
1297 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1298 && TREE_CODE (array) != INDIRECT_REF)
1300 tree rval, type;
1302 /* Subscripting with type char is likely to lose
1303 on a machine where chars are signed.
1304 So warn on any machine, but optionally.
1305 Don't warn for unsigned char since that type is safe.
1306 Don't warn for signed char because anyone who uses that
1307 must have done so deliberately. */
1308 if (warn_char_subscripts
1309 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1310 warning ("array subscript has type `char'");
1312 /* Apply default promotions *after* noticing character types. */
1313 index = default_conversion (index);
1315 /* Require integer *after* promotion, for sake of enums. */
1316 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1318 error ("array subscript is not an integer");
1319 return error_mark_node;
1322 /* An array that is indexed by a non-constant
1323 cannot be stored in a register; we must be able to do
1324 address arithmetic on its address.
1325 Likewise an array of elements of variable size. */
1326 if (TREE_CODE (index) != INTEGER_CST
1327 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1328 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1330 if (mark_addressable (array) == 0)
1331 return error_mark_node;
1333 /* An array that is indexed by a constant value which is not within
1334 the array bounds cannot be stored in a register either; because we
1335 would get a crash in store_bit_field/extract_bit_field when trying
1336 to access a non-existent part of the register. */
1337 if (TREE_CODE (index) == INTEGER_CST
1338 && TYPE_VALUES (TREE_TYPE (array))
1339 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1341 if (mark_addressable (array) == 0)
1342 return error_mark_node;
1345 if (pedantic)
1347 tree foo = array;
1348 while (TREE_CODE (foo) == COMPONENT_REF)
1349 foo = TREE_OPERAND (foo, 0);
1350 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1351 pedwarn ("ISO C forbids subscripting `register' array");
1352 else if (! flag_isoc99 && ! lvalue_p (foo))
1353 pedwarn ("ISO C89 forbids subscripting non-lvalue array");
1356 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1357 rval = build (ARRAY_REF, type, array, index);
1358 /* Array ref is const/volatile if the array elements are
1359 or if the array is. */
1360 TREE_READONLY (rval)
1361 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1362 | TREE_READONLY (array));
1363 TREE_SIDE_EFFECTS (rval)
1364 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1365 | TREE_SIDE_EFFECTS (array));
1366 TREE_THIS_VOLATILE (rval)
1367 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1368 /* This was added by rms on 16 Nov 91.
1369 It fixes vol struct foo *a; a->elts[1]
1370 in an inline function.
1371 Hope it doesn't break something else. */
1372 | TREE_THIS_VOLATILE (array));
1373 return require_complete_type (fold (rval));
1377 tree ar = default_conversion (array);
1378 tree ind = default_conversion (index);
1380 /* Do the same warning check as above, but only on the part that's
1381 syntactically the index and only if it is also semantically
1382 the index. */
1383 if (warn_char_subscripts
1384 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1385 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1386 warning ("subscript has type `char'");
1388 /* Put the integer in IND to simplify error checking. */
1389 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1391 tree temp = ar;
1392 ar = ind;
1393 ind = temp;
1396 if (ar == error_mark_node)
1397 return ar;
1399 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1400 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1402 error ("subscripted value is neither array nor pointer");
1403 return error_mark_node;
1405 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1407 error ("array subscript is not an integer");
1408 return error_mark_node;
1411 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1412 "array indexing");
1416 /* Build an external reference to identifier ID. FUN indicates
1417 whether this will be used for a function call. */
1418 tree
1419 build_external_ref (id, fun)
1420 tree id;
1421 int fun;
1423 tree ref;
1424 tree decl = lookup_name (id);
1425 tree objc_ivar = lookup_objc_ivar (id);
1427 if (decl && TREE_DEPRECATED (decl))
1428 warn_deprecated_use (decl);
1430 if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
1432 if (objc_ivar)
1433 ref = objc_ivar;
1434 else if (fun)
1436 if (!decl || decl == error_mark_node)
1437 /* Ordinary implicit function declaration. */
1438 ref = implicitly_declare (id);
1439 else
1441 /* Implicit declaration of built-in function. Don't
1442 change the built-in declaration, but don't let this
1443 go by silently, either. */
1444 implicit_decl_warning (id);
1446 /* only issue this warning once */
1447 C_DECL_ANTICIPATED (decl) = 0;
1448 ref = decl;
1451 else
1453 /* Reference to undeclared variable, including reference to
1454 builtin outside of function-call context. */
1455 if (current_function_decl == 0)
1456 error ("`%s' undeclared here (not in a function)",
1457 IDENTIFIER_POINTER (id));
1458 else
1460 if (IDENTIFIER_GLOBAL_VALUE (id) != error_mark_node
1461 || IDENTIFIER_ERROR_LOCUS (id) != current_function_decl)
1463 error ("`%s' undeclared (first use in this function)",
1464 IDENTIFIER_POINTER (id));
1466 if (! undeclared_variable_notice)
1468 error ("(Each undeclared identifier is reported only once");
1469 error ("for each function it appears in.)");
1470 undeclared_variable_notice = 1;
1473 IDENTIFIER_GLOBAL_VALUE (id) = error_mark_node;
1474 IDENTIFIER_ERROR_LOCUS (id) = current_function_decl;
1476 return error_mark_node;
1479 else
1481 /* Properly declared variable or function reference. */
1482 if (!objc_ivar)
1483 ref = decl;
1484 else if (decl != objc_ivar && IDENTIFIER_LOCAL_VALUE (id))
1486 warning ("local declaration of `%s' hides instance variable",
1487 IDENTIFIER_POINTER (id));
1488 ref = decl;
1490 else
1491 ref = objc_ivar;
1494 if (TREE_TYPE (ref) == error_mark_node)
1495 return error_mark_node;
1497 assemble_external (ref);
1498 TREE_USED (ref) = 1;
1500 if (TREE_CODE (ref) == CONST_DECL)
1502 ref = DECL_INITIAL (ref);
1503 TREE_CONSTANT (ref) = 1;
1506 return ref;
1509 /* Build a function call to function FUNCTION with parameters PARAMS.
1510 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1511 TREE_VALUE of each node is a parameter-expression.
1512 FUNCTION's data type may be a function type or a pointer-to-function. */
1514 tree
1515 build_function_call (function, params)
1516 tree function, params;
1518 tree fntype, fundecl = 0;
1519 tree coerced_params;
1520 tree name = NULL_TREE, assembler_name = NULL_TREE, result;
1522 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1523 STRIP_TYPE_NOPS (function);
1525 /* Convert anything with function type to a pointer-to-function. */
1526 if (TREE_CODE (function) == FUNCTION_DECL)
1528 name = DECL_NAME (function);
1529 assembler_name = DECL_ASSEMBLER_NAME (function);
1531 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1532 (because calling an inline function does not mean the function
1533 needs to be separately compiled). */
1534 fntype = build_type_variant (TREE_TYPE (function),
1535 TREE_READONLY (function),
1536 TREE_THIS_VOLATILE (function));
1537 fundecl = function;
1538 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1540 else
1541 function = default_conversion (function);
1543 fntype = TREE_TYPE (function);
1545 if (TREE_CODE (fntype) == ERROR_MARK)
1546 return error_mark_node;
1548 if (!(TREE_CODE (fntype) == POINTER_TYPE
1549 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1551 error ("called object is not a function");
1552 return error_mark_node;
1555 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1556 current_function_returns_abnormally = 1;
1558 /* fntype now gets the type of function pointed to. */
1559 fntype = TREE_TYPE (fntype);
1561 /* Convert the parameters to the types declared in the
1562 function prototype, or apply default promotions. */
1564 coerced_params
1565 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1567 /* Check for errors in format strings. */
1569 if (warn_format)
1570 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
1572 /* Recognize certain built-in functions so we can make tree-codes
1573 other than CALL_EXPR. We do this when it enables fold-const.c
1574 to do something useful. */
1576 if (TREE_CODE (function) == ADDR_EXPR
1577 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1578 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1580 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1581 params, coerced_params);
1582 if (result)
1583 return result;
1586 result = build (CALL_EXPR, TREE_TYPE (fntype),
1587 function, coerced_params, NULL_TREE);
1588 TREE_SIDE_EFFECTS (result) = 1;
1589 result = fold (result);
1591 if (VOID_TYPE_P (TREE_TYPE (result)))
1592 return result;
1593 return require_complete_type (result);
1596 /* Convert the argument expressions in the list VALUES
1597 to the types in the list TYPELIST. The result is a list of converted
1598 argument expressions.
1600 If TYPELIST is exhausted, or when an element has NULL as its type,
1601 perform the default conversions.
1603 PARMLIST is the chain of parm decls for the function being called.
1604 It may be 0, if that info is not available.
1605 It is used only for generating error messages.
1607 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1609 This is also where warnings about wrong number of args are generated.
1611 Both VALUES and the returned value are chains of TREE_LIST nodes
1612 with the elements of the list in the TREE_VALUE slots of those nodes. */
1614 static tree
1615 convert_arguments (typelist, values, name, fundecl)
1616 tree typelist, values, name, fundecl;
1618 tree typetail, valtail;
1619 tree result = NULL;
1620 int parmnum;
1622 /* Scan the given expressions and types, producing individual
1623 converted arguments and pushing them on RESULT in reverse order. */
1625 for (valtail = values, typetail = typelist, parmnum = 0;
1626 valtail;
1627 valtail = TREE_CHAIN (valtail), parmnum++)
1629 tree type = typetail ? TREE_VALUE (typetail) : 0;
1630 tree val = TREE_VALUE (valtail);
1632 if (type == void_type_node)
1634 if (name)
1635 error ("too many arguments to function `%s'",
1636 IDENTIFIER_POINTER (name));
1637 else
1638 error ("too many arguments to function");
1639 break;
1642 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1643 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1644 to convert automatically to a pointer. */
1645 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1646 val = TREE_OPERAND (val, 0);
1648 val = default_function_array_conversion (val);
1650 val = require_complete_type (val);
1652 if (type != 0)
1654 /* Formal parm type is specified by a function prototype. */
1655 tree parmval;
1657 if (!COMPLETE_TYPE_P (type))
1659 error ("type of formal parameter %d is incomplete", parmnum + 1);
1660 parmval = val;
1662 else
1664 /* Optionally warn about conversions that
1665 differ from the default conversions. */
1666 if (warn_conversion || warn_traditional)
1668 int formal_prec = TYPE_PRECISION (type);
1670 if (INTEGRAL_TYPE_P (type)
1671 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1672 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1673 if (INTEGRAL_TYPE_P (type)
1674 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1675 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1676 else if (TREE_CODE (type) == COMPLEX_TYPE
1677 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1678 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1679 else if (TREE_CODE (type) == REAL_TYPE
1680 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1681 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1682 else if (TREE_CODE (type) == COMPLEX_TYPE
1683 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1684 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1685 else if (TREE_CODE (type) == REAL_TYPE
1686 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1687 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1688 /* ??? At some point, messages should be written about
1689 conversions between complex types, but that's too messy
1690 to do now. */
1691 else if (TREE_CODE (type) == REAL_TYPE
1692 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1694 /* Warn if any argument is passed as `float',
1695 since without a prototype it would be `double'. */
1696 if (formal_prec == TYPE_PRECISION (float_type_node))
1697 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1699 /* Detect integer changing in width or signedness.
1700 These warnings are only activated with
1701 -Wconversion, not with -Wtraditional. */
1702 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1703 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1705 tree would_have_been = default_conversion (val);
1706 tree type1 = TREE_TYPE (would_have_been);
1708 if (TREE_CODE (type) == ENUMERAL_TYPE
1709 && (TYPE_MAIN_VARIANT (type)
1710 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1711 /* No warning if function asks for enum
1712 and the actual arg is that enum type. */
1714 else if (formal_prec != TYPE_PRECISION (type1))
1715 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1716 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1718 /* Don't complain if the formal parameter type
1719 is an enum, because we can't tell now whether
1720 the value was an enum--even the same enum. */
1721 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1723 else if (TREE_CODE (val) == INTEGER_CST
1724 && int_fits_type_p (val, type))
1725 /* Change in signedness doesn't matter
1726 if a constant value is unaffected. */
1728 /* Likewise for a constant in a NOP_EXPR. */
1729 else if (TREE_CODE (val) == NOP_EXPR
1730 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1731 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1733 #if 0 /* We never get such tree structure here. */
1734 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1735 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1736 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1737 /* Change in signedness doesn't matter
1738 if an enum value is unaffected. */
1740 #endif
1741 /* If the value is extended from a narrower
1742 unsigned type, it doesn't matter whether we
1743 pass it as signed or unsigned; the value
1744 certainly is the same either way. */
1745 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1746 && TREE_UNSIGNED (TREE_TYPE (val)))
1748 else if (TREE_UNSIGNED (type))
1749 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1750 else
1751 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1755 parmval = convert_for_assignment (type, val,
1756 (char *) 0, /* arg passing */
1757 fundecl, name, parmnum + 1);
1759 if (PROMOTE_PROTOTYPES
1760 && INTEGRAL_TYPE_P (type)
1761 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1762 parmval = default_conversion (parmval);
1764 result = tree_cons (NULL_TREE, parmval, result);
1766 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1767 && (TYPE_PRECISION (TREE_TYPE (val))
1768 < TYPE_PRECISION (double_type_node)))
1769 /* Convert `float' to `double'. */
1770 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1771 else
1772 /* Convert `short' and `char' to full-size `int'. */
1773 result = tree_cons (NULL_TREE, default_conversion (val), result);
1775 if (typetail)
1776 typetail = TREE_CHAIN (typetail);
1779 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1781 if (name)
1782 error ("too few arguments to function `%s'",
1783 IDENTIFIER_POINTER (name));
1784 else
1785 error ("too few arguments to function");
1788 return nreverse (result);
1791 /* This is the entry point used by the parser
1792 for binary operators in the input.
1793 In addition to constructing the expression,
1794 we check for operands that were written with other binary operators
1795 in a way that is likely to confuse the user. */
1797 tree
1798 parser_build_binary_op (code, arg1, arg2)
1799 enum tree_code code;
1800 tree arg1, arg2;
1802 tree result = build_binary_op (code, arg1, arg2, 1);
1804 char class;
1805 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1806 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1807 enum tree_code code1 = ERROR_MARK;
1808 enum tree_code code2 = ERROR_MARK;
1810 if (TREE_CODE (result) == ERROR_MARK)
1811 return error_mark_node;
1813 if (IS_EXPR_CODE_CLASS (class1))
1814 code1 = C_EXP_ORIGINAL_CODE (arg1);
1815 if (IS_EXPR_CODE_CLASS (class2))
1816 code2 = C_EXP_ORIGINAL_CODE (arg2);
1818 /* Check for cases such as x+y<<z which users are likely
1819 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1820 is cleared to prevent these warnings. */
1821 if (warn_parentheses)
1823 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1825 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1826 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1827 warning ("suggest parentheses around + or - inside shift");
1830 if (code == TRUTH_ORIF_EXPR)
1832 if (code1 == TRUTH_ANDIF_EXPR
1833 || code2 == TRUTH_ANDIF_EXPR)
1834 warning ("suggest parentheses around && within ||");
1837 if (code == BIT_IOR_EXPR)
1839 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1840 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1841 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1842 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1843 warning ("suggest parentheses around arithmetic in operand of |");
1844 /* Check cases like x|y==z */
1845 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1846 warning ("suggest parentheses around comparison in operand of |");
1849 if (code == BIT_XOR_EXPR)
1851 if (code1 == BIT_AND_EXPR
1852 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1853 || code2 == BIT_AND_EXPR
1854 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1855 warning ("suggest parentheses around arithmetic in operand of ^");
1856 /* Check cases like x^y==z */
1857 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1858 warning ("suggest parentheses around comparison in operand of ^");
1861 if (code == BIT_AND_EXPR)
1863 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1864 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1865 warning ("suggest parentheses around + or - in operand of &");
1866 /* Check cases like x&y==z */
1867 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1868 warning ("suggest parentheses around comparison in operand of &");
1872 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1873 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1874 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1875 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1877 unsigned_conversion_warning (result, arg1);
1878 unsigned_conversion_warning (result, arg2);
1879 overflow_warning (result);
1881 class = TREE_CODE_CLASS (TREE_CODE (result));
1883 /* Record the code that was specified in the source,
1884 for the sake of warnings about confusing nesting. */
1885 if (IS_EXPR_CODE_CLASS (class))
1886 C_SET_EXP_ORIGINAL_CODE (result, code);
1887 else
1889 int flag = TREE_CONSTANT (result);
1890 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1891 so that convert_for_assignment wouldn't strip it.
1892 That way, we got warnings for things like p = (1 - 1).
1893 But it turns out we should not get those warnings. */
1894 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1895 C_SET_EXP_ORIGINAL_CODE (result, code);
1896 TREE_CONSTANT (result) = flag;
1899 return result;
1902 /* Build a binary-operation expression without default conversions.
1903 CODE is the kind of expression to build.
1904 This function differs from `build' in several ways:
1905 the data type of the result is computed and recorded in it,
1906 warnings are generated if arg data types are invalid,
1907 special handling for addition and subtraction of pointers is known,
1908 and some optimization is done (operations on narrow ints
1909 are done in the narrower type when that gives the same result).
1910 Constant folding is also done before the result is returned.
1912 Note that the operands will never have enumeral types, or function
1913 or array types, because either they will have the default conversions
1914 performed or they have both just been converted to some other type in which
1915 the arithmetic is to be done. */
1917 tree
1918 build_binary_op (code, orig_op0, orig_op1, convert_p)
1919 enum tree_code code;
1920 tree orig_op0, orig_op1;
1921 int convert_p;
1923 tree type0, type1;
1924 enum tree_code code0, code1;
1925 tree op0, op1;
1927 /* Expression code to give to the expression when it is built.
1928 Normally this is CODE, which is what the caller asked for,
1929 but in some special cases we change it. */
1930 enum tree_code resultcode = code;
1932 /* Data type in which the computation is to be performed.
1933 In the simplest cases this is the common type of the arguments. */
1934 tree result_type = NULL;
1936 /* Nonzero means operands have already been type-converted
1937 in whatever way is necessary.
1938 Zero means they need to be converted to RESULT_TYPE. */
1939 int converted = 0;
1941 /* Nonzero means create the expression with this type, rather than
1942 RESULT_TYPE. */
1943 tree build_type = 0;
1945 /* Nonzero means after finally constructing the expression
1946 convert it to this type. */
1947 tree final_type = 0;
1949 /* Nonzero if this is an operation like MIN or MAX which can
1950 safely be computed in short if both args are promoted shorts.
1951 Also implies COMMON.
1952 -1 indicates a bitwise operation; this makes a difference
1953 in the exact conditions for when it is safe to do the operation
1954 in a narrower mode. */
1955 int shorten = 0;
1957 /* Nonzero if this is a comparison operation;
1958 if both args are promoted shorts, compare the original shorts.
1959 Also implies COMMON. */
1960 int short_compare = 0;
1962 /* Nonzero if this is a right-shift operation, which can be computed on the
1963 original short and then promoted if the operand is a promoted short. */
1964 int short_shift = 0;
1966 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1967 int common = 0;
1969 if (convert_p)
1971 op0 = default_conversion (orig_op0);
1972 op1 = default_conversion (orig_op1);
1974 else
1976 op0 = orig_op0;
1977 op1 = orig_op1;
1980 type0 = TREE_TYPE (op0);
1981 type1 = TREE_TYPE (op1);
1983 /* The expression codes of the data types of the arguments tell us
1984 whether the arguments are integers, floating, pointers, etc. */
1985 code0 = TREE_CODE (type0);
1986 code1 = TREE_CODE (type1);
1988 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1989 STRIP_TYPE_NOPS (op0);
1990 STRIP_TYPE_NOPS (op1);
1992 /* If an error was already reported for one of the arguments,
1993 avoid reporting another error. */
1995 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1996 return error_mark_node;
1998 switch (code)
2000 case PLUS_EXPR:
2001 /* Handle the pointer + int case. */
2002 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2003 return pointer_int_sum (PLUS_EXPR, op0, op1);
2004 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2005 return pointer_int_sum (PLUS_EXPR, op1, op0);
2006 else
2007 common = 1;
2008 break;
2010 case MINUS_EXPR:
2011 /* Subtraction of two similar pointers.
2012 We must subtract them as integers, then divide by object size. */
2013 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2014 && comp_target_types (type0, type1))
2015 return pointer_diff (op0, op1);
2016 /* Handle pointer minus int. Just like pointer plus int. */
2017 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2018 return pointer_int_sum (MINUS_EXPR, op0, op1);
2019 else
2020 common = 1;
2021 break;
2023 case MULT_EXPR:
2024 common = 1;
2025 break;
2027 case TRUNC_DIV_EXPR:
2028 case CEIL_DIV_EXPR:
2029 case FLOOR_DIV_EXPR:
2030 case ROUND_DIV_EXPR:
2031 case EXACT_DIV_EXPR:
2032 /* Floating point division by zero is a legitimate way to obtain
2033 infinities and NaNs. */
2034 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2035 warning ("division by zero");
2037 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2038 || code0 == COMPLEX_TYPE)
2039 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2040 || code1 == COMPLEX_TYPE))
2042 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2043 resultcode = RDIV_EXPR;
2044 else
2045 /* Although it would be tempting to shorten always here, that
2046 loses on some targets, since the modulo instruction is
2047 undefined if the quotient can't be represented in the
2048 computation mode. We shorten only if unsigned or if
2049 dividing by something we know != -1. */
2050 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2051 || (TREE_CODE (op1) == INTEGER_CST
2052 && ! integer_all_onesp (op1)));
2053 common = 1;
2055 break;
2057 case BIT_AND_EXPR:
2058 case BIT_ANDTC_EXPR:
2059 case BIT_IOR_EXPR:
2060 case BIT_XOR_EXPR:
2061 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2062 shorten = -1;
2063 break;
2065 case TRUNC_MOD_EXPR:
2066 case FLOOR_MOD_EXPR:
2067 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2068 warning ("division by zero");
2070 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2072 /* Although it would be tempting to shorten always here, that loses
2073 on some targets, since the modulo instruction is undefined if the
2074 quotient can't be represented in the computation mode. We shorten
2075 only if unsigned or if dividing by something we know != -1. */
2076 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2077 || (TREE_CODE (op1) == INTEGER_CST
2078 && ! integer_all_onesp (op1)));
2079 common = 1;
2081 break;
2083 case TRUTH_ANDIF_EXPR:
2084 case TRUTH_ORIF_EXPR:
2085 case TRUTH_AND_EXPR:
2086 case TRUTH_OR_EXPR:
2087 case TRUTH_XOR_EXPR:
2088 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2089 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2090 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2091 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2093 /* Result of these operations is always an int,
2094 but that does not mean the operands should be
2095 converted to ints! */
2096 result_type = integer_type_node;
2097 op0 = truthvalue_conversion (op0);
2098 op1 = truthvalue_conversion (op1);
2099 converted = 1;
2101 break;
2103 /* Shift operations: result has same type as first operand;
2104 always convert second operand to int.
2105 Also set SHORT_SHIFT if shifting rightward. */
2107 case RSHIFT_EXPR:
2108 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2110 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2112 if (tree_int_cst_sgn (op1) < 0)
2113 warning ("right shift count is negative");
2114 else
2116 if (! integer_zerop (op1))
2117 short_shift = 1;
2119 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2120 warning ("right shift count >= width of type");
2124 /* Use the type of the value to be shifted.
2125 This is what most traditional C compilers do. */
2126 result_type = type0;
2127 /* Unless traditional, convert the shift-count to an integer,
2128 regardless of size of value being shifted. */
2129 if (! flag_traditional)
2131 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2132 op1 = convert (integer_type_node, op1);
2133 /* Avoid converting op1 to result_type later. */
2134 converted = 1;
2137 break;
2139 case LSHIFT_EXPR:
2140 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2142 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2144 if (tree_int_cst_sgn (op1) < 0)
2145 warning ("left shift count is negative");
2147 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2148 warning ("left shift count >= width of type");
2151 /* Use the type of the value to be shifted.
2152 This is what most traditional C compilers do. */
2153 result_type = type0;
2154 /* Unless traditional, convert the shift-count to an integer,
2155 regardless of size of value being shifted. */
2156 if (! flag_traditional)
2158 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2159 op1 = convert (integer_type_node, op1);
2160 /* Avoid converting op1 to result_type later. */
2161 converted = 1;
2164 break;
2166 case RROTATE_EXPR:
2167 case LROTATE_EXPR:
2168 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2170 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2172 if (tree_int_cst_sgn (op1) < 0)
2173 warning ("shift count is negative");
2174 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2175 warning ("shift count >= width of type");
2178 /* Use the type of the value to be shifted.
2179 This is what most traditional C compilers do. */
2180 result_type = type0;
2181 /* Unless traditional, convert the shift-count to an integer,
2182 regardless of size of value being shifted. */
2183 if (! flag_traditional)
2185 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2186 op1 = convert (integer_type_node, op1);
2187 /* Avoid converting op1 to result_type later. */
2188 converted = 1;
2191 break;
2193 case EQ_EXPR:
2194 case NE_EXPR:
2195 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2196 warning ("comparing floating point with == or != is unsafe");
2197 /* Result of comparison is always int,
2198 but don't convert the args to int! */
2199 build_type = integer_type_node;
2200 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2201 || code0 == COMPLEX_TYPE)
2202 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2203 || code1 == COMPLEX_TYPE))
2204 short_compare = 1;
2205 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2207 tree tt0 = TREE_TYPE (type0);
2208 tree tt1 = TREE_TYPE (type1);
2209 /* Anything compares with void *. void * compares with anything.
2210 Otherwise, the targets must be compatible
2211 and both must be object or both incomplete. */
2212 if (comp_target_types (type0, type1))
2213 result_type = common_type (type0, type1);
2214 else if (VOID_TYPE_P (tt0))
2216 /* op0 != orig_op0 detects the case of something
2217 whose value is 0 but which isn't a valid null ptr const. */
2218 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2219 && TREE_CODE (tt1) == FUNCTION_TYPE)
2220 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2222 else if (VOID_TYPE_P (tt1))
2224 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2225 && TREE_CODE (tt0) == FUNCTION_TYPE)
2226 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2228 else
2229 pedwarn ("comparison of distinct pointer types lacks a cast");
2231 if (result_type == NULL_TREE)
2232 result_type = ptr_type_node;
2234 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2235 && integer_zerop (op1))
2236 result_type = type0;
2237 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2238 && integer_zerop (op0))
2239 result_type = type1;
2240 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2242 result_type = type0;
2243 if (! flag_traditional)
2244 pedwarn ("comparison between pointer and integer");
2246 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2248 result_type = type1;
2249 if (! flag_traditional)
2250 pedwarn ("comparison between pointer and integer");
2252 break;
2254 case MAX_EXPR:
2255 case MIN_EXPR:
2256 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2257 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2258 shorten = 1;
2259 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2261 if (comp_target_types (type0, type1))
2263 result_type = common_type (type0, type1);
2264 if (pedantic
2265 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2266 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2268 else
2270 result_type = ptr_type_node;
2271 pedwarn ("comparison of distinct pointer types lacks a cast");
2274 break;
2276 case LE_EXPR:
2277 case GE_EXPR:
2278 case LT_EXPR:
2279 case GT_EXPR:
2280 build_type = integer_type_node;
2281 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2282 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2283 short_compare = 1;
2284 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2286 if (comp_target_types (type0, type1))
2288 result_type = common_type (type0, type1);
2289 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2290 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2291 pedwarn ("comparison of complete and incomplete pointers");
2292 else if (pedantic
2293 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2294 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2296 else
2298 result_type = ptr_type_node;
2299 pedwarn ("comparison of distinct pointer types lacks a cast");
2302 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2303 && integer_zerop (op1))
2305 result_type = type0;
2306 if (pedantic || extra_warnings)
2307 pedwarn ("ordered comparison of pointer with integer zero");
2309 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2310 && integer_zerop (op0))
2312 result_type = type1;
2313 if (pedantic)
2314 pedwarn ("ordered comparison of pointer with integer zero");
2316 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2318 result_type = type0;
2319 if (! flag_traditional)
2320 pedwarn ("comparison between pointer and integer");
2322 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2324 result_type = type1;
2325 if (! flag_traditional)
2326 pedwarn ("comparison between pointer and integer");
2328 break;
2330 case UNORDERED_EXPR:
2331 case ORDERED_EXPR:
2332 case UNLT_EXPR:
2333 case UNLE_EXPR:
2334 case UNGT_EXPR:
2335 case UNGE_EXPR:
2336 case UNEQ_EXPR:
2337 build_type = integer_type_node;
2338 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2340 error ("unordered comparison on non-floating point argument");
2341 return error_mark_node;
2343 common = 1;
2344 break;
2346 default:
2347 break;
2350 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2352 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2354 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2356 if (shorten || common || short_compare)
2357 result_type = common_type (type0, type1);
2359 /* For certain operations (which identify themselves by shorten != 0)
2360 if both args were extended from the same smaller type,
2361 do the arithmetic in that type and then extend.
2363 shorten !=0 and !=1 indicates a bitwise operation.
2364 For them, this optimization is safe only if
2365 both args are zero-extended or both are sign-extended.
2366 Otherwise, we might change the result.
2367 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2368 but calculated in (unsigned short) it would be (unsigned short)-1. */
2370 if (shorten && none_complex)
2372 int unsigned0, unsigned1;
2373 tree arg0 = get_narrower (op0, &unsigned0);
2374 tree arg1 = get_narrower (op1, &unsigned1);
2375 /* UNS is 1 if the operation to be done is an unsigned one. */
2376 int uns = TREE_UNSIGNED (result_type);
2377 tree type;
2379 final_type = result_type;
2381 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2382 but it *requires* conversion to FINAL_TYPE. */
2384 if ((TYPE_PRECISION (TREE_TYPE (op0))
2385 == TYPE_PRECISION (TREE_TYPE (arg0)))
2386 && TREE_TYPE (op0) != final_type)
2387 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2388 if ((TYPE_PRECISION (TREE_TYPE (op1))
2389 == TYPE_PRECISION (TREE_TYPE (arg1)))
2390 && TREE_TYPE (op1) != final_type)
2391 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2393 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2395 /* For bitwise operations, signedness of nominal type
2396 does not matter. Consider only how operands were extended. */
2397 if (shorten == -1)
2398 uns = unsigned0;
2400 /* Note that in all three cases below we refrain from optimizing
2401 an unsigned operation on sign-extended args.
2402 That would not be valid. */
2404 /* Both args variable: if both extended in same way
2405 from same width, do it in that width.
2406 Do it unsigned if args were zero-extended. */
2407 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2408 < TYPE_PRECISION (result_type))
2409 && (TYPE_PRECISION (TREE_TYPE (arg1))
2410 == TYPE_PRECISION (TREE_TYPE (arg0)))
2411 && unsigned0 == unsigned1
2412 && (unsigned0 || !uns))
2413 result_type
2414 = signed_or_unsigned_type (unsigned0,
2415 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2416 else if (TREE_CODE (arg0) == INTEGER_CST
2417 && (unsigned1 || !uns)
2418 && (TYPE_PRECISION (TREE_TYPE (arg1))
2419 < TYPE_PRECISION (result_type))
2420 && (type = signed_or_unsigned_type (unsigned1,
2421 TREE_TYPE (arg1)),
2422 int_fits_type_p (arg0, type)))
2423 result_type = type;
2424 else if (TREE_CODE (arg1) == INTEGER_CST
2425 && (unsigned0 || !uns)
2426 && (TYPE_PRECISION (TREE_TYPE (arg0))
2427 < TYPE_PRECISION (result_type))
2428 && (type = signed_or_unsigned_type (unsigned0,
2429 TREE_TYPE (arg0)),
2430 int_fits_type_p (arg1, type)))
2431 result_type = type;
2434 /* Shifts can be shortened if shifting right. */
2436 if (short_shift)
2438 int unsigned_arg;
2439 tree arg0 = get_narrower (op0, &unsigned_arg);
2441 final_type = result_type;
2443 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2444 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2446 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2447 /* We can shorten only if the shift count is less than the
2448 number of bits in the smaller type size. */
2449 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2450 /* We cannot drop an unsigned shift after sign-extension. */
2451 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2453 /* Do an unsigned shift if the operand was zero-extended. */
2454 result_type
2455 = signed_or_unsigned_type (unsigned_arg, TREE_TYPE (arg0));
2456 /* Convert value-to-be-shifted to that type. */
2457 if (TREE_TYPE (op0) != result_type)
2458 op0 = convert (result_type, op0);
2459 converted = 1;
2463 /* Comparison operations are shortened too but differently.
2464 They identify themselves by setting short_compare = 1. */
2466 if (short_compare)
2468 /* Don't write &op0, etc., because that would prevent op0
2469 from being kept in a register.
2470 Instead, make copies of the our local variables and
2471 pass the copies by reference, then copy them back afterward. */
2472 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2473 enum tree_code xresultcode = resultcode;
2474 tree val
2475 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2477 if (val != 0)
2478 return val;
2480 op0 = xop0, op1 = xop1;
2481 converted = 1;
2482 resultcode = xresultcode;
2484 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2485 && skip_evaluation == 0)
2487 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2488 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2489 int unsignedp0, unsignedp1;
2490 tree primop0 = get_narrower (op0, &unsignedp0);
2491 tree primop1 = get_narrower (op1, &unsignedp1);
2493 xop0 = orig_op0;
2494 xop1 = orig_op1;
2495 STRIP_TYPE_NOPS (xop0);
2496 STRIP_TYPE_NOPS (xop1);
2498 /* Give warnings for comparisons between signed and unsigned
2499 quantities that may fail.
2501 Do the checking based on the original operand trees, so that
2502 casts will be considered, but default promotions won't be.
2504 Do not warn if the comparison is being done in a signed type,
2505 since the signed type will only be chosen if it can represent
2506 all the values of the unsigned type. */
2507 if (! TREE_UNSIGNED (result_type))
2508 /* OK */;
2509 /* Do not warn if both operands are the same signedness. */
2510 else if (op0_signed == op1_signed)
2511 /* OK */;
2512 else
2514 tree sop, uop;
2516 if (op0_signed)
2517 sop = xop0, uop = xop1;
2518 else
2519 sop = xop1, uop = xop0;
2521 /* Do not warn if the signed quantity is an
2522 unsuffixed integer literal (or some static
2523 constant expression involving such literals or a
2524 conditional expression involving such literals)
2525 and it is non-negative. */
2526 if (tree_expr_nonnegative_p (sop))
2527 /* OK */;
2528 /* Do not warn if the comparison is an equality operation,
2529 the unsigned quantity is an integral constant, and it
2530 would fit in the result if the result were signed. */
2531 else if (TREE_CODE (uop) == INTEGER_CST
2532 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2533 && int_fits_type_p (uop, signed_type (result_type)))
2534 /* OK */;
2535 /* Do not warn if the unsigned quantity is an enumeration
2536 constant and its maximum value would fit in the result
2537 if the result were signed. */
2538 else if (TREE_CODE (uop) == INTEGER_CST
2539 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2540 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2541 signed_type (result_type)))
2542 /* OK */;
2543 else
2544 warning ("comparison between signed and unsigned");
2547 /* Warn if two unsigned values are being compared in a size
2548 larger than their original size, and one (and only one) is the
2549 result of a `~' operator. This comparison will always fail.
2551 Also warn if one operand is a constant, and the constant
2552 does not have all bits set that are set in the ~ operand
2553 when it is extended. */
2555 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2556 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2558 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2559 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2560 &unsignedp0);
2561 else
2562 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2563 &unsignedp1);
2565 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2567 tree primop;
2568 HOST_WIDE_INT constant, mask;
2569 int unsignedp, bits;
2571 if (host_integerp (primop0, 0))
2573 primop = primop1;
2574 unsignedp = unsignedp1;
2575 constant = tree_low_cst (primop0, 0);
2577 else
2579 primop = primop0;
2580 unsignedp = unsignedp0;
2581 constant = tree_low_cst (primop1, 0);
2584 bits = TYPE_PRECISION (TREE_TYPE (primop));
2585 if (bits < TYPE_PRECISION (result_type)
2586 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2588 mask = (~ (HOST_WIDE_INT) 0) << bits;
2589 if ((mask & constant) != mask)
2590 warning ("comparison of promoted ~unsigned with constant");
2593 else if (unsignedp0 && unsignedp1
2594 && (TYPE_PRECISION (TREE_TYPE (primop0))
2595 < TYPE_PRECISION (result_type))
2596 && (TYPE_PRECISION (TREE_TYPE (primop1))
2597 < TYPE_PRECISION (result_type)))
2598 warning ("comparison of promoted ~unsigned with unsigned");
2604 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2605 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2606 Then the expression will be built.
2607 It will be given type FINAL_TYPE if that is nonzero;
2608 otherwise, it will be given type RESULT_TYPE. */
2610 if (!result_type)
2612 binary_op_error (code);
2613 return error_mark_node;
2616 if (! converted)
2618 if (TREE_TYPE (op0) != result_type)
2619 op0 = convert (result_type, op0);
2620 if (TREE_TYPE (op1) != result_type)
2621 op1 = convert (result_type, op1);
2624 if (build_type == NULL_TREE)
2625 build_type = result_type;
2628 tree result = build (resultcode, build_type, op0, op1);
2629 tree folded;
2631 folded = fold (result);
2632 if (folded == result)
2633 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2634 if (final_type != 0)
2635 return convert (final_type, folded);
2636 return folded;
2640 /* Return a tree for the sum or difference (RESULTCODE says which)
2641 of pointer PTROP and integer INTOP. */
2643 static tree
2644 pointer_int_sum (resultcode, ptrop, intop)
2645 enum tree_code resultcode;
2646 tree ptrop, intop;
2648 tree size_exp;
2650 tree result;
2651 tree folded;
2653 /* The result is a pointer of the same type that is being added. */
2655 tree result_type = TREE_TYPE (ptrop);
2657 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2659 if (pedantic || warn_pointer_arith)
2660 pedwarn ("pointer of type `void *' used in arithmetic");
2661 size_exp = integer_one_node;
2663 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2665 if (pedantic || warn_pointer_arith)
2666 pedwarn ("pointer to a function used in arithmetic");
2667 size_exp = integer_one_node;
2669 else
2670 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2672 /* If what we are about to multiply by the size of the elements
2673 contains a constant term, apply distributive law
2674 and multiply that constant term separately.
2675 This helps produce common subexpressions. */
2677 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2678 && ! TREE_CONSTANT (intop)
2679 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2680 && TREE_CONSTANT (size_exp)
2681 /* If the constant comes from pointer subtraction,
2682 skip this optimization--it would cause an error. */
2683 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2684 /* If the constant is unsigned, and smaller than the pointer size,
2685 then we must skip this optimization. This is because it could cause
2686 an overflow error if the constant is negative but INTOP is not. */
2687 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2688 || (TYPE_PRECISION (TREE_TYPE (intop))
2689 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2691 enum tree_code subcode = resultcode;
2692 tree int_type = TREE_TYPE (intop);
2693 if (TREE_CODE (intop) == MINUS_EXPR)
2694 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2695 /* Convert both subexpression types to the type of intop,
2696 because weird cases involving pointer arithmetic
2697 can result in a sum or difference with different type args. */
2698 ptrop = build_binary_op (subcode, ptrop,
2699 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2700 intop = convert (int_type, TREE_OPERAND (intop, 0));
2703 /* Convert the integer argument to a type the same size as sizetype
2704 so the multiply won't overflow spuriously. */
2706 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2707 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2708 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2709 TREE_UNSIGNED (sizetype)), intop);
2711 /* Replace the integer argument with a suitable product by the object size.
2712 Do this multiplication as signed, then convert to the appropriate
2713 pointer type (actually unsigned integral). */
2715 intop = convert (result_type,
2716 build_binary_op (MULT_EXPR, intop,
2717 convert (TREE_TYPE (intop), size_exp), 1));
2719 /* Create the sum or difference. */
2721 result = build (resultcode, result_type, ptrop, intop);
2723 folded = fold (result);
2724 if (folded == result)
2725 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2726 return folded;
2729 /* Return a tree for the difference of pointers OP0 and OP1.
2730 The resulting tree has type int. */
2732 static tree
2733 pointer_diff (op0, op1)
2734 tree op0, op1;
2736 tree result, folded;
2737 tree restype = ptrdiff_type_node;
2739 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2740 tree con0, con1, lit0, lit1;
2741 tree orig_op1 = op1;
2743 if (pedantic || warn_pointer_arith)
2745 if (TREE_CODE (target_type) == VOID_TYPE)
2746 pedwarn ("pointer of type `void *' used in subtraction");
2747 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2748 pedwarn ("pointer to a function used in subtraction");
2751 /* If the conversion to ptrdiff_type does anything like widening or
2752 converting a partial to an integral mode, we get a convert_expression
2753 that is in the way to do any simplifications.
2754 (fold-const.c doesn't know that the extra bits won't be needed.
2755 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2756 different mode in place.)
2757 So first try to find a common term here 'by hand'; we want to cover
2758 at least the cases that occur in legal static initializers. */
2759 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2760 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2762 if (TREE_CODE (con0) == PLUS_EXPR)
2764 lit0 = TREE_OPERAND (con0, 1);
2765 con0 = TREE_OPERAND (con0, 0);
2767 else
2768 lit0 = integer_zero_node;
2770 if (TREE_CODE (con1) == PLUS_EXPR)
2772 lit1 = TREE_OPERAND (con1, 1);
2773 con1 = TREE_OPERAND (con1, 0);
2775 else
2776 lit1 = integer_zero_node;
2778 if (operand_equal_p (con0, con1, 0))
2780 op0 = lit0;
2781 op1 = lit1;
2785 /* First do the subtraction as integers;
2786 then drop through to build the divide operator.
2787 Do not do default conversions on the minus operator
2788 in case restype is a short type. */
2790 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2791 convert (restype, op1), 0);
2792 /* This generates an error if op1 is pointer to incomplete type. */
2793 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2794 error ("arithmetic on pointer to an incomplete type");
2796 /* This generates an error if op0 is pointer to incomplete type. */
2797 op1 = c_size_in_bytes (target_type);
2799 /* Divide by the size, in easiest possible way. */
2801 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2803 folded = fold (result);
2804 if (folded == result)
2805 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2806 return folded;
2809 /* Construct and perhaps optimize a tree representation
2810 for a unary operation. CODE, a tree_code, specifies the operation
2811 and XARG is the operand.
2812 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2813 the default promotions (such as from short to int).
2814 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2815 allows non-lvalues; this is only used to handle conversion of non-lvalue
2816 arrays to pointers in C99. */
2818 tree
2819 build_unary_op (code, xarg, flag)
2820 enum tree_code code;
2821 tree xarg;
2822 int flag;
2824 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2825 tree arg = xarg;
2826 tree argtype = 0;
2827 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2828 tree val;
2829 int noconvert = flag;
2831 if (typecode == ERROR_MARK)
2832 return error_mark_node;
2833 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2834 typecode = INTEGER_TYPE;
2836 switch (code)
2838 case CONVERT_EXPR:
2839 /* This is used for unary plus, because a CONVERT_EXPR
2840 is enough to prevent anybody from looking inside for
2841 associativity, but won't generate any code. */
2842 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2843 || typecode == COMPLEX_TYPE))
2845 error ("wrong type argument to unary plus");
2846 return error_mark_node;
2848 else if (!noconvert)
2849 arg = default_conversion (arg);
2850 break;
2852 case NEGATE_EXPR:
2853 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2854 || typecode == COMPLEX_TYPE))
2856 error ("wrong type argument to unary minus");
2857 return error_mark_node;
2859 else if (!noconvert)
2860 arg = default_conversion (arg);
2861 break;
2863 case BIT_NOT_EXPR:
2864 if (typecode == COMPLEX_TYPE)
2866 code = CONJ_EXPR;
2867 if (pedantic)
2868 pedwarn ("ISO C does not support `~' for complex conjugation");
2869 if (!noconvert)
2870 arg = default_conversion (arg);
2872 else if (typecode != INTEGER_TYPE)
2874 error ("wrong type argument to bit-complement");
2875 return error_mark_node;
2877 else if (!noconvert)
2878 arg = default_conversion (arg);
2879 break;
2881 case ABS_EXPR:
2882 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2883 || typecode == COMPLEX_TYPE))
2885 error ("wrong type argument to abs");
2886 return error_mark_node;
2888 else if (!noconvert)
2889 arg = default_conversion (arg);
2890 break;
2892 case CONJ_EXPR:
2893 /* Conjugating a real value is a no-op, but allow it anyway. */
2894 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2895 || typecode == COMPLEX_TYPE))
2897 error ("wrong type argument to conjugation");
2898 return error_mark_node;
2900 else if (!noconvert)
2901 arg = default_conversion (arg);
2902 break;
2904 case TRUTH_NOT_EXPR:
2905 if (typecode != INTEGER_TYPE
2906 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2907 && typecode != COMPLEX_TYPE
2908 /* These will convert to a pointer. */
2909 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2911 error ("wrong type argument to unary exclamation mark");
2912 return error_mark_node;
2914 arg = truthvalue_conversion (arg);
2915 return invert_truthvalue (arg);
2917 case NOP_EXPR:
2918 break;
2920 case REALPART_EXPR:
2921 if (TREE_CODE (arg) == COMPLEX_CST)
2922 return TREE_REALPART (arg);
2923 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2924 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2925 else
2926 return arg;
2928 case IMAGPART_EXPR:
2929 if (TREE_CODE (arg) == COMPLEX_CST)
2930 return TREE_IMAGPART (arg);
2931 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2932 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2933 else
2934 return convert (TREE_TYPE (arg), integer_zero_node);
2936 case PREINCREMENT_EXPR:
2937 case POSTINCREMENT_EXPR:
2938 case PREDECREMENT_EXPR:
2939 case POSTDECREMENT_EXPR:
2940 /* Handle complex lvalues (when permitted)
2941 by reduction to simpler cases. */
2943 val = unary_complex_lvalue (code, arg, 0);
2944 if (val != 0)
2945 return val;
2947 /* Increment or decrement the real part of the value,
2948 and don't change the imaginary part. */
2949 if (typecode == COMPLEX_TYPE)
2951 tree real, imag;
2953 if (pedantic)
2954 pedwarn ("ISO C does not support `++' and `--' on complex types");
2956 arg = stabilize_reference (arg);
2957 real = build_unary_op (REALPART_EXPR, arg, 1);
2958 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2959 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2960 build_unary_op (code, real, 1), imag);
2963 /* Report invalid types. */
2965 if (typecode != POINTER_TYPE
2966 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2968 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2969 error ("wrong type argument to increment");
2970 else
2971 error ("wrong type argument to decrement");
2973 return error_mark_node;
2977 tree inc;
2978 tree result_type = TREE_TYPE (arg);
2980 arg = get_unwidened (arg, 0);
2981 argtype = TREE_TYPE (arg);
2983 /* Compute the increment. */
2985 if (typecode == POINTER_TYPE)
2987 /* If pointer target is an undefined struct,
2988 we just cannot know how to do the arithmetic. */
2989 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2991 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2992 error ("increment of pointer to unknown structure");
2993 else
2994 error ("decrement of pointer to unknown structure");
2996 else if ((pedantic || warn_pointer_arith)
2997 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2998 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
3000 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3001 pedwarn ("wrong type argument to increment");
3002 else
3003 pedwarn ("wrong type argument to decrement");
3006 inc = c_size_in_bytes (TREE_TYPE (result_type));
3008 else
3009 inc = integer_one_node;
3011 inc = convert (argtype, inc);
3013 /* Handle incrementing a cast-expression. */
3015 while (1)
3016 switch (TREE_CODE (arg))
3018 case NOP_EXPR:
3019 case CONVERT_EXPR:
3020 case FLOAT_EXPR:
3021 case FIX_TRUNC_EXPR:
3022 case FIX_FLOOR_EXPR:
3023 case FIX_ROUND_EXPR:
3024 case FIX_CEIL_EXPR:
3025 pedantic_lvalue_warning (CONVERT_EXPR);
3026 /* If the real type has the same machine representation
3027 as the type it is cast to, we can make better output
3028 by adding directly to the inside of the cast. */
3029 if ((TREE_CODE (TREE_TYPE (arg))
3030 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
3031 && (TYPE_MODE (TREE_TYPE (arg))
3032 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
3033 arg = TREE_OPERAND (arg, 0);
3034 else
3036 tree incremented, modify, value;
3037 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3038 value = boolean_increment (code, arg);
3039 else
3041 arg = stabilize_reference (arg);
3042 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3043 value = arg;
3044 else
3045 value = save_expr (arg);
3046 incremented = build (((code == PREINCREMENT_EXPR
3047 || code == POSTINCREMENT_EXPR)
3048 ? PLUS_EXPR : MINUS_EXPR),
3049 argtype, value, inc);
3050 TREE_SIDE_EFFECTS (incremented) = 1;
3051 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3052 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3054 TREE_USED (value) = 1;
3055 return value;
3057 break;
3059 default:
3060 goto give_up;
3062 give_up:
3064 /* Complain about anything else that is not a true lvalue. */
3065 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3066 || code == POSTINCREMENT_EXPR)
3067 ? "invalid lvalue in increment"
3068 : "invalid lvalue in decrement")))
3069 return error_mark_node;
3071 /* Report a read-only lvalue. */
3072 if (TREE_READONLY (arg))
3073 readonly_warning (arg,
3074 ((code == PREINCREMENT_EXPR
3075 || code == POSTINCREMENT_EXPR)
3076 ? "increment" : "decrement"));
3078 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3079 val = boolean_increment (code, arg);
3080 else
3081 val = build (code, TREE_TYPE (arg), arg, inc);
3082 TREE_SIDE_EFFECTS (val) = 1;
3083 val = convert (result_type, val);
3084 if (TREE_CODE (val) != code)
3085 TREE_NO_UNUSED_WARNING (val) = 1;
3086 return val;
3089 case ADDR_EXPR:
3090 /* Note that this operation never does default_conversion. */
3092 /* Let &* cancel out to simplify resulting code. */
3093 if (TREE_CODE (arg) == INDIRECT_REF)
3095 /* Don't let this be an lvalue. */
3096 if (lvalue_p (TREE_OPERAND (arg, 0)))
3097 return non_lvalue (TREE_OPERAND (arg, 0));
3098 return TREE_OPERAND (arg, 0);
3101 /* For &x[y], return x+y */
3102 if (TREE_CODE (arg) == ARRAY_REF)
3104 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3105 return error_mark_node;
3106 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3107 TREE_OPERAND (arg, 1), 1);
3110 /* Handle complex lvalues (when permitted)
3111 by reduction to simpler cases. */
3112 val = unary_complex_lvalue (code, arg, flag);
3113 if (val != 0)
3114 return val;
3116 #if 0 /* Turned off because inconsistent;
3117 float f; *&(int)f = 3.4 stores in int format
3118 whereas (int)f = 3.4 stores in float format. */
3119 /* Address of a cast is just a cast of the address
3120 of the operand of the cast. */
3121 switch (TREE_CODE (arg))
3123 case NOP_EXPR:
3124 case CONVERT_EXPR:
3125 case FLOAT_EXPR:
3126 case FIX_TRUNC_EXPR:
3127 case FIX_FLOOR_EXPR:
3128 case FIX_ROUND_EXPR:
3129 case FIX_CEIL_EXPR:
3130 if (pedantic)
3131 pedwarn ("ISO C forbids the address of a cast expression");
3132 return convert (build_pointer_type (TREE_TYPE (arg)),
3133 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3134 0));
3136 #endif
3138 /* Anything not already handled and not a true memory reference
3139 or a non-lvalue array is an error. */
3140 else if (typecode != FUNCTION_TYPE && !flag
3141 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3142 return error_mark_node;
3144 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3145 argtype = TREE_TYPE (arg);
3147 /* If the lvalue is const or volatile, merge that into the type
3148 to which the address will point. Note that you can't get a
3149 restricted pointer by taking the address of something, so we
3150 only have to deal with `const' and `volatile' here. */
3151 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3152 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3153 argtype = c_build_type_variant (argtype,
3154 TREE_READONLY (arg),
3155 TREE_THIS_VOLATILE (arg));
3157 argtype = build_pointer_type (argtype);
3159 if (mark_addressable (arg) == 0)
3160 return error_mark_node;
3163 tree addr;
3165 if (TREE_CODE (arg) == COMPONENT_REF)
3167 tree field = TREE_OPERAND (arg, 1);
3169 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
3171 if (DECL_C_BIT_FIELD (field))
3173 error ("attempt to take address of bit-field structure member `%s'",
3174 IDENTIFIER_POINTER (DECL_NAME (field)));
3175 return error_mark_node;
3178 addr = fold (build (PLUS_EXPR, argtype,
3179 convert (argtype, addr),
3180 convert (argtype, byte_position (field))));
3182 else
3183 addr = build1 (code, argtype, arg);
3185 /* Address of a static or external variable or
3186 file-scope function counts as a constant. */
3187 if (staticp (arg)
3188 && ! (TREE_CODE (arg) == FUNCTION_DECL
3189 && DECL_CONTEXT (arg) != 0))
3190 TREE_CONSTANT (addr) = 1;
3191 return addr;
3194 default:
3195 break;
3198 if (argtype == 0)
3199 argtype = TREE_TYPE (arg);
3200 return fold (build1 (code, argtype, arg));
3203 #if 0
3204 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3205 convert ARG with the same conversions in the same order
3206 and return the result. */
3208 static tree
3209 convert_sequence (conversions, arg)
3210 tree conversions;
3211 tree arg;
3213 switch (TREE_CODE (conversions))
3215 case NOP_EXPR:
3216 case CONVERT_EXPR:
3217 case FLOAT_EXPR:
3218 case FIX_TRUNC_EXPR:
3219 case FIX_FLOOR_EXPR:
3220 case FIX_ROUND_EXPR:
3221 case FIX_CEIL_EXPR:
3222 return convert (TREE_TYPE (conversions),
3223 convert_sequence (TREE_OPERAND (conversions, 0),
3224 arg));
3226 default:
3227 return arg;
3230 #endif /* 0 */
3232 /* Return nonzero if REF is an lvalue valid for this language.
3233 Lvalues can be assigned, unless their type has TYPE_READONLY.
3234 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3237 lvalue_p (ref)
3238 tree ref;
3240 enum tree_code code = TREE_CODE (ref);
3242 switch (code)
3244 case REALPART_EXPR:
3245 case IMAGPART_EXPR:
3246 case COMPONENT_REF:
3247 return lvalue_p (TREE_OPERAND (ref, 0));
3249 case COMPOUND_LITERAL_EXPR:
3250 case STRING_CST:
3251 return 1;
3253 case INDIRECT_REF:
3254 case ARRAY_REF:
3255 case VAR_DECL:
3256 case PARM_DECL:
3257 case RESULT_DECL:
3258 case ERROR_MARK:
3259 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3260 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3262 case BIND_EXPR:
3263 case RTL_EXPR:
3264 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3266 default:
3267 return 0;
3271 /* Return nonzero if REF is an lvalue valid for this language;
3272 otherwise, print an error message and return zero. */
3275 lvalue_or_else (ref, msgid)
3276 tree ref;
3277 const char *msgid;
3279 int win = lvalue_p (ref);
3281 if (! win)
3282 error ("%s", msgid);
3284 return win;
3287 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3288 for certain kinds of expressions which are not really lvalues
3289 but which we can accept as lvalues. If FLAG is nonzero, then
3290 non-lvalues are OK since we may be converting a non-lvalue array to
3291 a pointer in C99.
3293 If ARG is not a kind of expression we can handle, return zero. */
3295 static tree
3296 unary_complex_lvalue (code, arg, flag)
3297 enum tree_code code;
3298 tree arg;
3299 int flag;
3301 /* Handle (a, b) used as an "lvalue". */
3302 if (TREE_CODE (arg) == COMPOUND_EXPR)
3304 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3306 /* If this returns a function type, it isn't really being used as
3307 an lvalue, so don't issue a warning about it. */
3308 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3309 pedantic_lvalue_warning (COMPOUND_EXPR);
3311 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3312 TREE_OPERAND (arg, 0), real_result);
3315 /* Handle (a ? b : c) used as an "lvalue". */
3316 if (TREE_CODE (arg) == COND_EXPR)
3318 if (!flag)
3319 pedantic_lvalue_warning (COND_EXPR);
3320 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3321 pedantic_lvalue_warning (COMPOUND_EXPR);
3323 return (build_conditional_expr
3324 (TREE_OPERAND (arg, 0),
3325 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3326 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3329 return 0;
3332 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3333 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3335 static void
3336 pedantic_lvalue_warning (code)
3337 enum tree_code code;
3339 if (pedantic)
3340 switch (code)
3342 case COND_EXPR:
3343 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3344 break;
3345 case COMPOUND_EXPR:
3346 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3347 break;
3348 default:
3349 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3350 break;
3354 /* Warn about storing in something that is `const'. */
3356 void
3357 readonly_warning (arg, msgid)
3358 tree arg;
3359 const char *msgid;
3361 if (TREE_CODE (arg) == COMPONENT_REF)
3363 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3364 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3365 else
3366 pedwarn ("%s of read-only member `%s'", _(msgid),
3367 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3369 else if (TREE_CODE (arg) == VAR_DECL)
3370 pedwarn ("%s of read-only variable `%s'", _(msgid),
3371 IDENTIFIER_POINTER (DECL_NAME (arg)));
3372 else
3373 pedwarn ("%s of read-only location", _(msgid));
3376 /* Mark EXP saying that we need to be able to take the
3377 address of it; it should not be allocated in a register.
3378 Value is 1 if successful. */
3381 mark_addressable (exp)
3382 tree exp;
3384 tree x = exp;
3385 while (1)
3386 switch (TREE_CODE (x))
3388 case COMPONENT_REF:
3389 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3391 error ("cannot take address of bit-field `%s'",
3392 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3393 return 0;
3396 /* ... fall through ... */
3398 case ADDR_EXPR:
3399 case ARRAY_REF:
3400 case REALPART_EXPR:
3401 case IMAGPART_EXPR:
3402 x = TREE_OPERAND (x, 0);
3403 break;
3405 case COMPOUND_LITERAL_EXPR:
3406 case CONSTRUCTOR:
3407 TREE_ADDRESSABLE (x) = 1;
3408 return 1;
3410 case VAR_DECL:
3411 case CONST_DECL:
3412 case PARM_DECL:
3413 case RESULT_DECL:
3414 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3415 && DECL_NONLOCAL (x))
3417 if (TREE_PUBLIC (x))
3419 error ("global register variable `%s' used in nested function",
3420 IDENTIFIER_POINTER (DECL_NAME (x)));
3421 return 0;
3423 pedwarn ("register variable `%s' used in nested function",
3424 IDENTIFIER_POINTER (DECL_NAME (x)));
3426 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3428 if (TREE_PUBLIC (x))
3430 error ("address of global register variable `%s' requested",
3431 IDENTIFIER_POINTER (DECL_NAME (x)));
3432 return 0;
3435 /* If we are making this addressable due to its having
3436 volatile components, give a different error message. Also
3437 handle the case of an unnamed parameter by not trying
3438 to give the name. */
3440 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3442 error ("cannot put object with volatile field into register");
3443 return 0;
3446 pedwarn ("address of register variable `%s' requested",
3447 IDENTIFIER_POINTER (DECL_NAME (x)));
3449 put_var_into_stack (x);
3451 /* drops in */
3452 case FUNCTION_DECL:
3453 TREE_ADDRESSABLE (x) = 1;
3454 #if 0 /* poplevel deals with this now. */
3455 if (DECL_CONTEXT (x) == 0)
3456 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3457 #endif
3459 default:
3460 return 1;
3464 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3466 tree
3467 build_conditional_expr (ifexp, op1, op2)
3468 tree ifexp, op1, op2;
3470 tree type1;
3471 tree type2;
3472 enum tree_code code1;
3473 enum tree_code code2;
3474 tree result_type = NULL;
3475 tree orig_op1 = op1, orig_op2 = op2;
3477 ifexp = truthvalue_conversion (default_conversion (ifexp));
3479 #if 0 /* Produces wrong result if within sizeof. */
3480 /* Don't promote the operands separately if they promote
3481 the same way. Return the unpromoted type and let the combined
3482 value get promoted if necessary. */
3484 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3485 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3486 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3487 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3489 if (TREE_CODE (ifexp) == INTEGER_CST)
3490 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3492 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3494 #endif
3496 /* Promote both alternatives. */
3498 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3499 op1 = default_conversion (op1);
3500 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3501 op2 = default_conversion (op2);
3503 if (TREE_CODE (ifexp) == ERROR_MARK
3504 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3505 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3506 return error_mark_node;
3508 type1 = TREE_TYPE (op1);
3509 code1 = TREE_CODE (type1);
3510 type2 = TREE_TYPE (op2);
3511 code2 = TREE_CODE (type2);
3513 /* Quickly detect the usual case where op1 and op2 have the same type
3514 after promotion. */
3515 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3517 if (type1 == type2)
3518 result_type = type1;
3519 else
3520 result_type = TYPE_MAIN_VARIANT (type1);
3522 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3523 || code1 == COMPLEX_TYPE)
3524 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3525 || code2 == COMPLEX_TYPE))
3527 result_type = common_type (type1, type2);
3529 /* If -Wsign-compare, warn here if type1 and type2 have
3530 different signedness. We'll promote the signed to unsigned
3531 and later code won't know it used to be different.
3532 Do this check on the original types, so that explicit casts
3533 will be considered, but default promotions won't. */
3534 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3535 && !skip_evaluation)
3537 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3538 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3540 if (unsigned_op1 ^ unsigned_op2)
3542 /* Do not warn if the result type is signed, since the
3543 signed type will only be chosen if it can represent
3544 all the values of the unsigned type. */
3545 if (! TREE_UNSIGNED (result_type))
3546 /* OK */;
3547 /* Do not warn if the signed quantity is an unsuffixed
3548 integer literal (or some static constant expression
3549 involving such literals) and it is non-negative. */
3550 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3551 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3552 /* OK */;
3553 else
3554 warning ("signed and unsigned type in conditional expression");
3558 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3560 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3561 pedwarn ("ISO C forbids conditional expr with only one void side");
3562 result_type = void_type_node;
3564 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3566 if (comp_target_types (type1, type2))
3567 result_type = common_type (type1, type2);
3568 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3569 && TREE_CODE (orig_op1) != NOP_EXPR)
3570 result_type = qualify_type (type2, type1);
3571 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3572 && TREE_CODE (orig_op2) != NOP_EXPR)
3573 result_type = qualify_type (type1, type2);
3574 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3576 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3577 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3578 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3579 TREE_TYPE (type2)));
3581 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3583 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3584 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3585 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3586 TREE_TYPE (type1)));
3588 else
3590 pedwarn ("pointer type mismatch in conditional expression");
3591 result_type = build_pointer_type (void_type_node);
3594 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3596 if (! integer_zerop (op2))
3597 pedwarn ("pointer/integer type mismatch in conditional expression");
3598 else
3600 op2 = null_pointer_node;
3602 result_type = type1;
3604 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3606 if (!integer_zerop (op1))
3607 pedwarn ("pointer/integer type mismatch in conditional expression");
3608 else
3610 op1 = null_pointer_node;
3612 result_type = type2;
3615 if (!result_type)
3617 if (flag_cond_mismatch)
3618 result_type = void_type_node;
3619 else
3621 error ("type mismatch in conditional expression");
3622 return error_mark_node;
3626 /* Merge const and volatile flags of the incoming types. */
3627 result_type
3628 = build_type_variant (result_type,
3629 TREE_READONLY (op1) || TREE_READONLY (op2),
3630 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3632 if (result_type != TREE_TYPE (op1))
3633 op1 = convert_and_check (result_type, op1);
3634 if (result_type != TREE_TYPE (op2))
3635 op2 = convert_and_check (result_type, op2);
3637 if (TREE_CODE (ifexp) == INTEGER_CST)
3638 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3640 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3643 /* Given a list of expressions, return a compound expression
3644 that performs them all and returns the value of the last of them. */
3646 tree
3647 build_compound_expr (list)
3648 tree list;
3650 return internal_build_compound_expr (list, TRUE);
3653 static tree
3654 internal_build_compound_expr (list, first_p)
3655 tree list;
3656 int first_p;
3658 tree rest;
3660 if (TREE_CHAIN (list) == 0)
3662 /* Convert arrays and functions to pointers when there
3663 really is a comma operator. */
3664 if (!first_p)
3665 TREE_VALUE (list)
3666 = default_function_array_conversion (TREE_VALUE (list));
3668 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3669 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3671 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3672 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3673 list = TREE_OPERAND (list, 0);
3674 #endif
3676 /* Don't let (0, 0) be null pointer constant. */
3677 if (!first_p && integer_zerop (TREE_VALUE (list)))
3678 return non_lvalue (TREE_VALUE (list));
3679 return TREE_VALUE (list);
3682 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3684 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3686 /* The left-hand operand of a comma expression is like an expression
3687 statement: with -W or -Wunused, we should warn if it doesn't have
3688 any side-effects, unless it was explicitly cast to (void). */
3689 if ((extra_warnings || warn_unused_value)
3690 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3691 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3692 warning ("left-hand operand of comma expression has no effect");
3694 /* When pedantic, a compound expression can be neither an lvalue
3695 nor an integer constant expression. */
3696 if (! pedantic)
3697 return rest;
3700 /* With -Wunused, we should also warn if the left-hand operand does have
3701 side-effects, but computes a value which is not used. For example, in
3702 `foo() + bar(), baz()' the result of the `+' operator is not used,
3703 so we should issue a warning. */
3704 else if (warn_unused_value)
3705 warn_if_unused_value (TREE_VALUE (list));
3707 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3710 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3712 tree
3713 build_c_cast (type, expr)
3714 tree type;
3715 tree expr;
3717 tree value = expr;
3719 if (type == error_mark_node || expr == error_mark_node)
3720 return error_mark_node;
3721 type = TYPE_MAIN_VARIANT (type);
3723 #if 0
3724 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3725 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3726 value = TREE_OPERAND (value, 0);
3727 #endif
3729 if (TREE_CODE (type) == ARRAY_TYPE)
3731 error ("cast specifies array type");
3732 return error_mark_node;
3735 if (TREE_CODE (type) == FUNCTION_TYPE)
3737 error ("cast specifies function type");
3738 return error_mark_node;
3741 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3743 if (pedantic)
3745 if (TREE_CODE (type) == RECORD_TYPE
3746 || TREE_CODE (type) == UNION_TYPE)
3747 pedwarn ("ISO C forbids casting nonscalar to the same type");
3750 else if (TREE_CODE (type) == UNION_TYPE)
3752 tree field;
3753 value = default_function_array_conversion (value);
3755 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3756 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3757 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3758 break;
3760 if (field)
3762 const char *name;
3763 tree t;
3765 if (pedantic)
3766 pedwarn ("ISO C forbids casts to union type");
3767 if (TYPE_NAME (type) != 0)
3769 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3770 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3771 else
3772 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3774 else
3775 name = "";
3776 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3777 build_tree_list (field, value)),
3778 0, 0);
3779 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3780 return t;
3782 error ("cast to union type from type not present in union");
3783 return error_mark_node;
3785 else
3787 tree otype, ovalue;
3789 /* If casting to void, avoid the error that would come
3790 from default_conversion in the case of a non-lvalue array. */
3791 if (type == void_type_node)
3792 return build1 (CONVERT_EXPR, type, value);
3794 /* Convert functions and arrays to pointers,
3795 but don't convert any other types. */
3796 value = default_function_array_conversion (value);
3797 otype = TREE_TYPE (value);
3799 /* Optionally warn about potentially worrisome casts. */
3801 if (warn_cast_qual
3802 && TREE_CODE (type) == POINTER_TYPE
3803 && TREE_CODE (otype) == POINTER_TYPE)
3805 tree in_type = type;
3806 tree in_otype = otype;
3807 int added = 0;
3808 int discarded = 0;
3810 /* Check that the qualifiers on IN_TYPE are a superset of
3811 the qualifiers of IN_OTYPE. The outermost level of
3812 POINTER_TYPE nodes is uninteresting and we stop as soon
3813 as we hit a non-POINTER_TYPE node on either type. */
3816 in_otype = TREE_TYPE (in_otype);
3817 in_type = TREE_TYPE (in_type);
3819 /* GNU C allows cv-qualified function types. 'const'
3820 means the function is very pure, 'volatile' means it
3821 can't return. We need to warn when such qualifiers
3822 are added, not when they're taken away. */
3823 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3824 && TREE_CODE (in_type) == FUNCTION_TYPE)
3825 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3826 else
3827 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3829 while (TREE_CODE (in_type) == POINTER_TYPE
3830 && TREE_CODE (in_otype) == POINTER_TYPE);
3832 if (added)
3833 warning ("cast adds new qualifiers to function type");
3835 if (discarded)
3836 /* There are qualifiers present in IN_OTYPE that are not
3837 present in IN_TYPE. */
3838 warning ("cast discards qualifiers from pointer target type");
3841 /* Warn about possible alignment problems. */
3842 if (STRICT_ALIGNMENT && warn_cast_align
3843 && TREE_CODE (type) == POINTER_TYPE
3844 && TREE_CODE (otype) == POINTER_TYPE
3845 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3846 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3847 /* Don't warn about opaque types, where the actual alignment
3848 restriction is unknown. */
3849 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3850 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3851 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3852 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3853 warning ("cast increases required alignment of target type");
3855 if (TREE_CODE (type) == INTEGER_TYPE
3856 && TREE_CODE (otype) == POINTER_TYPE
3857 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3858 && !TREE_CONSTANT (value))
3859 warning ("cast from pointer to integer of different size");
3861 if (warn_bad_function_cast
3862 && TREE_CODE (value) == CALL_EXPR
3863 && TREE_CODE (type) != TREE_CODE (otype))
3864 warning ("cast does not match function type");
3866 if (TREE_CODE (type) == POINTER_TYPE
3867 && TREE_CODE (otype) == INTEGER_TYPE
3868 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3869 /* Don't warn about converting any constant. */
3870 && !TREE_CONSTANT (value))
3871 warning ("cast to pointer from integer of different size");
3873 ovalue = value;
3874 value = convert (type, value);
3876 /* Ignore any integer overflow caused by the cast. */
3877 if (TREE_CODE (value) == INTEGER_CST)
3879 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3880 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3884 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3885 if (pedantic && TREE_CODE (value) == INTEGER_CST
3886 && TREE_CODE (expr) == INTEGER_CST
3887 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3888 value = non_lvalue (value);
3890 /* If pedantic, don't let a cast be an lvalue. */
3891 if (value == expr && pedantic)
3892 value = non_lvalue (value);
3894 return value;
3897 /* Interpret a cast of expression EXPR to type TYPE. */
3898 tree
3899 c_cast_expr (type, expr)
3900 tree type, expr;
3902 int saved_wsp = warn_strict_prototypes;
3904 /* This avoids warnings about unprototyped casts on
3905 integers. E.g. "#define SIG_DFL (void(*)())0". */
3906 if (TREE_CODE (expr) == INTEGER_CST)
3907 warn_strict_prototypes = 0;
3908 type = groktypename (type);
3909 warn_strict_prototypes = saved_wsp;
3911 return build_c_cast (type, expr);
3915 /* Build an assignment expression of lvalue LHS from value RHS.
3916 MODIFYCODE is the code for a binary operator that we use
3917 to combine the old value of LHS with RHS to get the new value.
3918 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3920 tree
3921 build_modify_expr (lhs, modifycode, rhs)
3922 tree lhs, rhs;
3923 enum tree_code modifycode;
3925 tree result;
3926 tree newrhs;
3927 tree lhstype = TREE_TYPE (lhs);
3928 tree olhstype = lhstype;
3930 /* Types that aren't fully specified cannot be used in assignments. */
3931 lhs = require_complete_type (lhs);
3933 /* Avoid duplicate error messages from operands that had errors. */
3934 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3935 return error_mark_node;
3937 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3938 /* Do not use STRIP_NOPS here. We do not want an enumerator
3939 whose value is 0 to count as a null pointer constant. */
3940 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3941 rhs = TREE_OPERAND (rhs, 0);
3943 newrhs = rhs;
3945 /* Handle control structure constructs used as "lvalues". */
3947 switch (TREE_CODE (lhs))
3949 /* Handle (a, b) used as an "lvalue". */
3950 case COMPOUND_EXPR:
3951 pedantic_lvalue_warning (COMPOUND_EXPR);
3952 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3953 if (TREE_CODE (newrhs) == ERROR_MARK)
3954 return error_mark_node;
3955 return build (COMPOUND_EXPR, lhstype,
3956 TREE_OPERAND (lhs, 0), newrhs);
3958 /* Handle (a ? b : c) used as an "lvalue". */
3959 case COND_EXPR:
3960 pedantic_lvalue_warning (COND_EXPR);
3961 rhs = save_expr (rhs);
3963 /* Produce (a ? (b = rhs) : (c = rhs))
3964 except that the RHS goes through a save-expr
3965 so the code to compute it is only emitted once. */
3966 tree cond
3967 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3968 build_modify_expr (TREE_OPERAND (lhs, 1),
3969 modifycode, rhs),
3970 build_modify_expr (TREE_OPERAND (lhs, 2),
3971 modifycode, rhs));
3972 if (TREE_CODE (cond) == ERROR_MARK)
3973 return cond;
3974 /* Make sure the code to compute the rhs comes out
3975 before the split. */
3976 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3977 /* But cast it to void to avoid an "unused" error. */
3978 convert (void_type_node, rhs), cond);
3980 default:
3981 break;
3984 /* If a binary op has been requested, combine the old LHS value with the RHS
3985 producing the value we should actually store into the LHS. */
3987 if (modifycode != NOP_EXPR)
3989 lhs = stabilize_reference (lhs);
3990 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3993 /* Handle a cast used as an "lvalue".
3994 We have already performed any binary operator using the value as cast.
3995 Now convert the result to the cast type of the lhs,
3996 and then true type of the lhs and store it there;
3997 then convert result back to the cast type to be the value
3998 of the assignment. */
4000 switch (TREE_CODE (lhs))
4002 case NOP_EXPR:
4003 case CONVERT_EXPR:
4004 case FLOAT_EXPR:
4005 case FIX_TRUNC_EXPR:
4006 case FIX_FLOOR_EXPR:
4007 case FIX_ROUND_EXPR:
4008 case FIX_CEIL_EXPR:
4009 newrhs = default_function_array_conversion (newrhs);
4011 tree inner_lhs = TREE_OPERAND (lhs, 0);
4012 tree result;
4013 result = build_modify_expr (inner_lhs, NOP_EXPR,
4014 convert (TREE_TYPE (inner_lhs),
4015 convert (lhstype, newrhs)));
4016 if (TREE_CODE (result) == ERROR_MARK)
4017 return result;
4018 pedantic_lvalue_warning (CONVERT_EXPR);
4019 return convert (TREE_TYPE (lhs), result);
4022 default:
4023 break;
4026 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
4027 Reject anything strange now. */
4029 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
4030 return error_mark_node;
4032 /* Warn about storing in something that is `const'. */
4034 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
4035 || ((TREE_CODE (lhstype) == RECORD_TYPE
4036 || TREE_CODE (lhstype) == UNION_TYPE)
4037 && C_TYPE_FIELDS_READONLY (lhstype)))
4038 readonly_warning (lhs, "assignment");
4040 /* If storing into a structure or union member,
4041 it has probably been given type `int'.
4042 Compute the type that would go with
4043 the actual amount of storage the member occupies. */
4045 if (TREE_CODE (lhs) == COMPONENT_REF
4046 && (TREE_CODE (lhstype) == INTEGER_TYPE
4047 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4048 || TREE_CODE (lhstype) == REAL_TYPE
4049 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4050 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4052 /* If storing in a field that is in actuality a short or narrower than one,
4053 we must store in the field in its actual type. */
4055 if (lhstype != TREE_TYPE (lhs))
4057 lhs = copy_node (lhs);
4058 TREE_TYPE (lhs) = lhstype;
4061 /* Convert new value to destination type. */
4063 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
4064 NULL_TREE, NULL_TREE, 0);
4065 if (TREE_CODE (newrhs) == ERROR_MARK)
4066 return error_mark_node;
4068 /* Scan operands */
4070 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4071 TREE_SIDE_EFFECTS (result) = 1;
4073 /* If we got the LHS in a different type for storing in,
4074 convert the result back to the nominal type of LHS
4075 so that the value we return always has the same type
4076 as the LHS argument. */
4078 if (olhstype == TREE_TYPE (result))
4079 return result;
4080 return convert_for_assignment (olhstype, result, _("assignment"),
4081 NULL_TREE, NULL_TREE, 0);
4084 /* Convert value RHS to type TYPE as preparation for an assignment
4085 to an lvalue of type TYPE.
4086 The real work of conversion is done by `convert'.
4087 The purpose of this function is to generate error messages
4088 for assignments that are not allowed in C.
4089 ERRTYPE is a string to use in error messages:
4090 "assignment", "return", etc. If it is null, this is parameter passing
4091 for a function call (and different error messages are output).
4093 FUNNAME is the name of the function being called,
4094 as an IDENTIFIER_NODE, or null.
4095 PARMNUM is the number of the argument, for printing in error messages. */
4097 static tree
4098 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4099 tree type, rhs;
4100 const char *errtype;
4101 tree fundecl, funname;
4102 int parmnum;
4104 enum tree_code codel = TREE_CODE (type);
4105 tree rhstype;
4106 enum tree_code coder;
4108 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4109 /* Do not use STRIP_NOPS here. We do not want an enumerator
4110 whose value is 0 to count as a null pointer constant. */
4111 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4112 rhs = TREE_OPERAND (rhs, 0);
4114 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4115 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4116 rhs = default_conversion (rhs);
4117 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4118 rhs = decl_constant_value_for_broken_optimization (rhs);
4120 rhstype = TREE_TYPE (rhs);
4121 coder = TREE_CODE (rhstype);
4123 if (coder == ERROR_MARK)
4124 return error_mark_node;
4126 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4128 overflow_warning (rhs);
4129 /* Check for Objective-C protocols. This will issue a warning if
4130 there are protocol violations. No need to use the return value. */
4131 maybe_objc_comptypes (type, rhstype, 0);
4132 return rhs;
4135 if (coder == VOID_TYPE)
4137 error ("void value not ignored as it ought to be");
4138 return error_mark_node;
4140 /* A type converts to a reference to it.
4141 This code doesn't fully support references, it's just for the
4142 special case of va_start and va_copy. */
4143 if (codel == REFERENCE_TYPE
4144 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4146 if (mark_addressable (rhs) == 0)
4147 return error_mark_node;
4148 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4150 /* We already know that these two types are compatible, but they
4151 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4152 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4153 likely to be va_list, a typedef to __builtin_va_list, which
4154 is different enough that it will cause problems later. */
4155 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4156 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4158 rhs = build1 (NOP_EXPR, type, rhs);
4159 return rhs;
4161 /* Arithmetic types all interconvert, and enum is treated like int. */
4162 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4163 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4164 || codel == BOOLEAN_TYPE)
4165 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4166 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4167 || coder == BOOLEAN_TYPE))
4168 return convert_and_check (type, rhs);
4170 /* Conversion to a transparent union from its member types.
4171 This applies only to function arguments. */
4172 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4174 tree memb_types;
4175 tree marginal_memb_type = 0;
4177 for (memb_types = TYPE_FIELDS (type); memb_types;
4178 memb_types = TREE_CHAIN (memb_types))
4180 tree memb_type = TREE_TYPE (memb_types);
4182 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4183 TYPE_MAIN_VARIANT (rhstype)))
4184 break;
4186 if (TREE_CODE (memb_type) != POINTER_TYPE)
4187 continue;
4189 if (coder == POINTER_TYPE)
4191 tree ttl = TREE_TYPE (memb_type);
4192 tree ttr = TREE_TYPE (rhstype);
4194 /* Any non-function converts to a [const][volatile] void *
4195 and vice versa; otherwise, targets must be the same.
4196 Meanwhile, the lhs target must have all the qualifiers of
4197 the rhs. */
4198 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4199 || comp_target_types (memb_type, rhstype))
4201 /* If this type won't generate any warnings, use it. */
4202 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4203 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4204 && TREE_CODE (ttl) == FUNCTION_TYPE)
4205 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4206 == TYPE_QUALS (ttr))
4207 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4208 == TYPE_QUALS (ttl))))
4209 break;
4211 /* Keep looking for a better type, but remember this one. */
4212 if (! marginal_memb_type)
4213 marginal_memb_type = memb_type;
4217 /* Can convert integer zero to any pointer type. */
4218 if (integer_zerop (rhs)
4219 || (TREE_CODE (rhs) == NOP_EXPR
4220 && integer_zerop (TREE_OPERAND (rhs, 0))))
4222 rhs = null_pointer_node;
4223 break;
4227 if (memb_types || marginal_memb_type)
4229 if (! memb_types)
4231 /* We have only a marginally acceptable member type;
4232 it needs a warning. */
4233 tree ttl = TREE_TYPE (marginal_memb_type);
4234 tree ttr = TREE_TYPE (rhstype);
4236 /* Const and volatile mean something different for function
4237 types, so the usual warnings are not appropriate. */
4238 if (TREE_CODE (ttr) == FUNCTION_TYPE
4239 && TREE_CODE (ttl) == FUNCTION_TYPE)
4241 /* Because const and volatile on functions are
4242 restrictions that say the function will not do
4243 certain things, it is okay to use a const or volatile
4244 function where an ordinary one is wanted, but not
4245 vice-versa. */
4246 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4247 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4248 errtype, funname, parmnum);
4250 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4251 warn_for_assignment ("%s discards qualifiers from pointer target type",
4252 errtype, funname,
4253 parmnum);
4256 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4257 pedwarn ("ISO C prohibits argument conversion to union type");
4259 return build1 (NOP_EXPR, type, rhs);
4263 /* Conversions among pointers */
4264 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4265 && (coder == POINTER_TYPE || coder == REFERENCE_TYPE))
4267 tree ttl = TREE_TYPE (type);
4268 tree ttr = TREE_TYPE (rhstype);
4270 /* Any non-function converts to a [const][volatile] void *
4271 and vice versa; otherwise, targets must be the same.
4272 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4273 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4274 || comp_target_types (type, rhstype)
4275 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4276 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4278 if (pedantic
4279 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4281 (VOID_TYPE_P (ttr)
4282 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4283 which are not ANSI null ptr constants. */
4284 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4285 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4286 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4287 errtype, funname, parmnum);
4288 /* Const and volatile mean something different for function types,
4289 so the usual warnings are not appropriate. */
4290 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4291 && TREE_CODE (ttl) != FUNCTION_TYPE)
4293 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4294 warn_for_assignment ("%s discards qualifiers from pointer target type",
4295 errtype, funname, parmnum);
4296 /* If this is not a case of ignoring a mismatch in signedness,
4297 no warning. */
4298 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4299 || comp_target_types (type, rhstype))
4301 /* If there is a mismatch, do warn. */
4302 else if (pedantic)
4303 warn_for_assignment ("pointer targets in %s differ in signedness",
4304 errtype, funname, parmnum);
4306 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4307 && TREE_CODE (ttr) == FUNCTION_TYPE)
4309 /* Because const and volatile on functions are restrictions
4310 that say the function will not do certain things,
4311 it is okay to use a const or volatile function
4312 where an ordinary one is wanted, but not vice-versa. */
4313 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4314 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4315 errtype, funname, parmnum);
4318 else
4319 warn_for_assignment ("%s from incompatible pointer type",
4320 errtype, funname, parmnum);
4321 return convert (type, rhs);
4323 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4325 /* An explicit constant 0 can convert to a pointer,
4326 or one that results from arithmetic, even including
4327 a cast to integer type. */
4328 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4330 ! (TREE_CODE (rhs) == NOP_EXPR
4331 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4332 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4333 && integer_zerop (TREE_OPERAND (rhs, 0))))
4335 warn_for_assignment ("%s makes pointer from integer without a cast",
4336 errtype, funname, parmnum);
4337 return convert (type, rhs);
4339 return null_pointer_node;
4341 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4343 warn_for_assignment ("%s makes integer from pointer without a cast",
4344 errtype, funname, parmnum);
4345 return convert (type, rhs);
4347 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4348 return convert (type, rhs);
4350 if (!errtype)
4352 if (funname)
4354 tree selector = maybe_building_objc_message_expr ();
4356 if (selector && parmnum > 2)
4357 error ("incompatible type for argument %d of `%s'",
4358 parmnum - 2, IDENTIFIER_POINTER (selector));
4359 else
4360 error ("incompatible type for argument %d of `%s'",
4361 parmnum, IDENTIFIER_POINTER (funname));
4363 else
4364 error ("incompatible type for argument %d of indirect function call",
4365 parmnum);
4367 else
4368 error ("incompatible types in %s", errtype);
4370 return error_mark_node;
4373 /* Print a warning using MSGID.
4374 It gets OPNAME as its one parameter.
4375 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4376 FUNCTION and ARGNUM are handled specially if we are building an
4377 Objective-C selector. */
4379 static void
4380 warn_for_assignment (msgid, opname, function, argnum)
4381 const char *msgid;
4382 const char *opname;
4383 tree function;
4384 int argnum;
4386 if (opname == 0)
4388 tree selector = maybe_building_objc_message_expr ();
4389 char * new_opname;
4391 if (selector && argnum > 2)
4393 function = selector;
4394 argnum -= 2;
4396 if (function)
4398 /* Function name is known; supply it. */
4399 const char *const argstring = _("passing arg %d of `%s'");
4400 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4401 + strlen (argstring) + 1 + 25
4402 /*%d*/ + 1);
4403 sprintf (new_opname, argstring, argnum,
4404 IDENTIFIER_POINTER (function));
4406 else
4408 /* Function name unknown (call through ptr); just give arg number. */
4409 const char *const argnofun = _("passing arg %d of pointer to function");
4410 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4411 sprintf (new_opname, argnofun, argnum);
4413 opname = new_opname;
4415 pedwarn (msgid, opname);
4418 /* If VALUE is a compound expr all of whose expressions are constant, then
4419 return its value. Otherwise, return error_mark_node.
4421 This is for handling COMPOUND_EXPRs as initializer elements
4422 which is allowed with a warning when -pedantic is specified. */
4424 static tree
4425 valid_compound_expr_initializer (value, endtype)
4426 tree value;
4427 tree endtype;
4429 if (TREE_CODE (value) == COMPOUND_EXPR)
4431 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4432 == error_mark_node)
4433 return error_mark_node;
4434 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4435 endtype);
4437 else if (! TREE_CONSTANT (value)
4438 && ! initializer_constant_valid_p (value, endtype))
4439 return error_mark_node;
4440 else
4441 return value;
4444 /* Perform appropriate conversions on the initial value of a variable,
4445 store it in the declaration DECL,
4446 and print any error messages that are appropriate.
4447 If the init is invalid, store an ERROR_MARK. */
4449 void
4450 store_init_value (decl, init)
4451 tree decl, init;
4453 tree value, type;
4455 /* If variable's type was invalidly declared, just ignore it. */
4457 type = TREE_TYPE (decl);
4458 if (TREE_CODE (type) == ERROR_MARK)
4459 return;
4461 /* Digest the specified initializer into an expression. */
4463 value = digest_init (type, init, TREE_STATIC (decl),
4464 TREE_STATIC (decl) || (pedantic && !flag_isoc99));
4466 /* Store the expression if valid; else report error. */
4468 #if 0
4469 /* Note that this is the only place we can detect the error
4470 in a case such as struct foo bar = (struct foo) { x, y };
4471 where there is one initial value which is a constructor expression. */
4472 if (value == error_mark_node)
4474 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4476 error ("initializer for static variable is not constant");
4477 value = error_mark_node;
4479 else if (TREE_STATIC (decl)
4480 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4482 error ("initializer for static variable uses complicated arithmetic");
4483 value = error_mark_node;
4485 else
4487 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4489 if (! TREE_CONSTANT (value))
4490 pedwarn ("aggregate initializer is not constant");
4491 else if (! TREE_STATIC (value))
4492 pedwarn ("aggregate initializer uses complicated arithmetic");
4495 #endif
4497 if (warn_traditional && !in_system_header
4498 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4499 warning ("traditional C rejects automatic aggregate initialization");
4501 DECL_INITIAL (decl) = value;
4503 /* ANSI wants warnings about out-of-range constant initializers. */
4504 STRIP_TYPE_NOPS (value);
4505 constant_expression_warning (value);
4507 /* Check if we need to set array size from compound literal size. */
4508 if (TREE_CODE (type) == ARRAY_TYPE
4509 && TYPE_DOMAIN (type) == 0
4510 && value != error_mark_node)
4512 tree inside_init = init;
4514 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4515 inside_init = TREE_OPERAND (init, 0);
4516 inside_init = fold (inside_init);
4518 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4520 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4522 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4524 /* For int foo[] = (int [3]){1}; we need to set array size
4525 now since later on array initializer will be just the
4526 brace enclosed list of the compound literal. */
4527 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4528 layout_type (type);
4529 layout_decl (decl, 0);
4535 /* Methods for storing and printing names for error messages. */
4537 /* Implement a spelling stack that allows components of a name to be pushed
4538 and popped. Each element on the stack is this structure. */
4540 struct spelling
4542 int kind;
4543 union
4545 int i;
4546 const char *s;
4547 } u;
4550 #define SPELLING_STRING 1
4551 #define SPELLING_MEMBER 2
4552 #define SPELLING_BOUNDS 3
4554 static struct spelling *spelling; /* Next stack element (unused). */
4555 static struct spelling *spelling_base; /* Spelling stack base. */
4556 static int spelling_size; /* Size of the spelling stack. */
4558 /* Macros to save and restore the spelling stack around push_... functions.
4559 Alternative to SAVE_SPELLING_STACK. */
4561 #define SPELLING_DEPTH() (spelling - spelling_base)
4562 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4564 /* Save and restore the spelling stack around arbitrary C code. */
4566 #define SAVE_SPELLING_DEPTH(code) \
4568 int __depth = SPELLING_DEPTH (); \
4569 code; \
4570 RESTORE_SPELLING_DEPTH (__depth); \
4573 /* Push an element on the spelling stack with type KIND and assign VALUE
4574 to MEMBER. */
4576 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4578 int depth = SPELLING_DEPTH (); \
4580 if (depth >= spelling_size) \
4582 spelling_size += 10; \
4583 if (spelling_base == 0) \
4584 spelling_base \
4585 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4586 else \
4587 spelling_base \
4588 = (struct spelling *) xrealloc (spelling_base, \
4589 spelling_size * sizeof (struct spelling)); \
4590 RESTORE_SPELLING_DEPTH (depth); \
4593 spelling->kind = (KIND); \
4594 spelling->MEMBER = (VALUE); \
4595 spelling++; \
4598 /* Push STRING on the stack. Printed literally. */
4600 static void
4601 push_string (string)
4602 const char *string;
4604 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4607 /* Push a member name on the stack. Printed as '.' STRING. */
4609 static void
4610 push_member_name (decl)
4611 tree decl;
4614 const char *const string
4615 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4616 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4619 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4621 static void
4622 push_array_bounds (bounds)
4623 int bounds;
4625 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4628 /* Compute the maximum size in bytes of the printed spelling. */
4630 static int
4631 spelling_length ()
4633 int size = 0;
4634 struct spelling *p;
4636 for (p = spelling_base; p < spelling; p++)
4638 if (p->kind == SPELLING_BOUNDS)
4639 size += 25;
4640 else
4641 size += strlen (p->u.s) + 1;
4644 return size;
4647 /* Print the spelling to BUFFER and return it. */
4649 static char *
4650 print_spelling (buffer)
4651 char *buffer;
4653 char *d = buffer;
4654 struct spelling *p;
4656 for (p = spelling_base; p < spelling; p++)
4657 if (p->kind == SPELLING_BOUNDS)
4659 sprintf (d, "[%d]", p->u.i);
4660 d += strlen (d);
4662 else
4664 const char *s;
4665 if (p->kind == SPELLING_MEMBER)
4666 *d++ = '.';
4667 for (s = p->u.s; (*d = *s++); d++)
4670 *d++ = '\0';
4671 return buffer;
4674 /* Issue an error message for a bad initializer component.
4675 MSGID identifies the message.
4676 The component name is taken from the spelling stack. */
4678 void
4679 error_init (msgid)
4680 const char *msgid;
4682 char *ofwhat;
4684 error ("%s", _(msgid));
4685 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4686 if (*ofwhat)
4687 error ("(near initialization for `%s')", ofwhat);
4690 /* Issue a pedantic warning for a bad initializer component.
4691 MSGID identifies the message.
4692 The component name is taken from the spelling stack. */
4694 void
4695 pedwarn_init (msgid)
4696 const char *msgid;
4698 char *ofwhat;
4700 pedwarn ("%s", _(msgid));
4701 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4702 if (*ofwhat)
4703 pedwarn ("(near initialization for `%s')", ofwhat);
4706 /* Issue a warning for a bad initializer component.
4707 MSGID identifies the message.
4708 The component name is taken from the spelling stack. */
4710 static void
4711 warning_init (msgid)
4712 const char *msgid;
4714 char *ofwhat;
4716 warning ("%s", _(msgid));
4717 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4718 if (*ofwhat)
4719 warning ("(near initialization for `%s')", ofwhat);
4722 /* Digest the parser output INIT as an initializer for type TYPE.
4723 Return a C expression of type TYPE to represent the initial value.
4725 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4726 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4727 applies only to elements of constructors. */
4729 static tree
4730 digest_init (type, init, require_constant, constructor_constant)
4731 tree type, init;
4732 int require_constant, constructor_constant;
4734 enum tree_code code = TREE_CODE (type);
4735 tree inside_init = init;
4737 if (type == error_mark_node
4738 || init == error_mark_node
4739 || TREE_TYPE (init) == error_mark_node)
4740 return error_mark_node;
4742 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4743 /* Do not use STRIP_NOPS here. We do not want an enumerator
4744 whose value is 0 to count as a null pointer constant. */
4745 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4746 inside_init = TREE_OPERAND (init, 0);
4748 inside_init = fold (inside_init);
4750 /* Initialization of an array of chars from a string constant
4751 optionally enclosed in braces. */
4753 if (code == ARRAY_TYPE)
4755 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4756 if ((typ1 == char_type_node
4757 || typ1 == signed_char_type_node
4758 || typ1 == unsigned_char_type_node
4759 || typ1 == unsigned_wchar_type_node
4760 || typ1 == signed_wchar_type_node)
4761 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4763 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4764 TYPE_MAIN_VARIANT (type)))
4765 return inside_init;
4767 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4768 != char_type_node)
4769 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4771 error_init ("char-array initialized from wide string");
4772 return error_mark_node;
4774 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4775 == char_type_node)
4776 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4778 error_init ("int-array initialized from non-wide string");
4779 return error_mark_node;
4782 TREE_TYPE (inside_init) = type;
4783 if (TYPE_DOMAIN (type) != 0
4784 && TYPE_SIZE (type) != 0
4785 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4786 /* Subtract 1 (or sizeof (wchar_t))
4787 because it's ok to ignore the terminating null char
4788 that is counted in the length of the constant. */
4789 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4790 TREE_STRING_LENGTH (inside_init)
4791 - ((TYPE_PRECISION (typ1)
4792 != TYPE_PRECISION (char_type_node))
4793 ? (TYPE_PRECISION (wchar_type_node)
4794 / BITS_PER_UNIT)
4795 : 1)))
4796 pedwarn_init ("initializer-string for array of chars is too long");
4798 return inside_init;
4802 /* Any type can be initialized
4803 from an expression of the same type, optionally with braces. */
4805 if (inside_init && TREE_TYPE (inside_init) != 0
4806 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4807 TYPE_MAIN_VARIANT (type))
4808 || (code == ARRAY_TYPE
4809 && comptypes (TREE_TYPE (inside_init), type))
4810 || (code == VECTOR_TYPE
4811 && comptypes (TREE_TYPE (inside_init), type))
4812 || (code == POINTER_TYPE
4813 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4814 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4815 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4816 TREE_TYPE (type)))))
4818 if (code == POINTER_TYPE)
4819 inside_init = default_function_array_conversion (inside_init);
4821 if (require_constant && !flag_isoc99
4822 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4824 /* As an extension, allow initializing objects with static storage
4825 duration with compound literals (which are then treated just as
4826 the brace enclosed list they contain). */
4827 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4828 inside_init = DECL_INITIAL (decl);
4831 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4832 && TREE_CODE (inside_init) != CONSTRUCTOR)
4834 error_init ("array initialized from non-constant array expression");
4835 return error_mark_node;
4838 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4839 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4841 /* Compound expressions can only occur here if -pedantic or
4842 -pedantic-errors is specified. In the later case, we always want
4843 an error. In the former case, we simply want a warning. */
4844 if (require_constant && pedantic
4845 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4847 inside_init
4848 = valid_compound_expr_initializer (inside_init,
4849 TREE_TYPE (inside_init));
4850 if (inside_init == error_mark_node)
4851 error_init ("initializer element is not constant");
4852 else
4853 pedwarn_init ("initializer element is not constant");
4854 if (flag_pedantic_errors)
4855 inside_init = error_mark_node;
4857 else if (require_constant
4858 && (!TREE_CONSTANT (inside_init)
4859 /* This test catches things like `7 / 0' which
4860 result in an expression for which TREE_CONSTANT
4861 is true, but which is not actually something
4862 that is a legal constant. We really should not
4863 be using this function, because it is a part of
4864 the back-end. Instead, the expression should
4865 already have been turned into ERROR_MARK_NODE. */
4866 || !initializer_constant_valid_p (inside_init,
4867 TREE_TYPE (inside_init))))
4869 error_init ("initializer element is not constant");
4870 inside_init = error_mark_node;
4873 return inside_init;
4876 /* Handle scalar types, including conversions. */
4878 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4879 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4881 /* Note that convert_for_assignment calls default_conversion
4882 for arrays and functions. We must not call it in the
4883 case where inside_init is a null pointer constant. */
4884 inside_init
4885 = convert_for_assignment (type, init, _("initialization"),
4886 NULL_TREE, NULL_TREE, 0);
4888 if (require_constant && ! TREE_CONSTANT (inside_init))
4890 error_init ("initializer element is not constant");
4891 inside_init = error_mark_node;
4893 else if (require_constant
4894 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4896 error_init ("initializer element is not computable at load time");
4897 inside_init = error_mark_node;
4900 return inside_init;
4903 /* Come here only for records and arrays. */
4905 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4907 error_init ("variable-sized object may not be initialized");
4908 return error_mark_node;
4911 /* Traditionally, you can write struct foo x = 0;
4912 and it initializes the first element of x to 0. */
4913 if (flag_traditional)
4915 tree top = 0, prev = 0, otype = type;
4916 while (TREE_CODE (type) == RECORD_TYPE
4917 || TREE_CODE (type) == ARRAY_TYPE
4918 || TREE_CODE (type) == QUAL_UNION_TYPE
4919 || TREE_CODE (type) == UNION_TYPE)
4921 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4922 if (prev == 0)
4923 top = temp;
4924 else
4925 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4926 prev = temp;
4927 if (TREE_CODE (type) == ARRAY_TYPE)
4928 type = TREE_TYPE (type);
4929 else if (TYPE_FIELDS (type))
4930 type = TREE_TYPE (TYPE_FIELDS (type));
4931 else
4933 error_init ("invalid initializer");
4934 return error_mark_node;
4938 if (otype != type)
4940 TREE_OPERAND (prev, 1)
4941 = build_tree_list (NULL_TREE,
4942 digest_init (type, init, require_constant,
4943 constructor_constant));
4944 return top;
4946 else
4947 return error_mark_node;
4949 error_init ("invalid initializer");
4950 return error_mark_node;
4953 /* Handle initializers that use braces. */
4955 /* Type of object we are accumulating a constructor for.
4956 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4957 static tree constructor_type;
4959 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4960 left to fill. */
4961 static tree constructor_fields;
4963 /* For an ARRAY_TYPE, this is the specified index
4964 at which to store the next element we get. */
4965 static tree constructor_index;
4967 /* For an ARRAY_TYPE, this is the maximum index. */
4968 static tree constructor_max_index;
4970 /* For a RECORD_TYPE, this is the first field not yet written out. */
4971 static tree constructor_unfilled_fields;
4973 /* For an ARRAY_TYPE, this is the index of the first element
4974 not yet written out. */
4975 static tree constructor_unfilled_index;
4977 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4978 This is so we can generate gaps between fields, when appropriate. */
4979 static tree constructor_bit_index;
4981 /* If we are saving up the elements rather than allocating them,
4982 this is the list of elements so far (in reverse order,
4983 most recent first). */
4984 static tree constructor_elements;
4986 /* 1 if constructor should be incrementally stored into a constructor chain,
4987 0 if all the elements should be kept in AVL tree. */
4988 static int constructor_incremental;
4990 /* 1 if so far this constructor's elements are all compile-time constants. */
4991 static int constructor_constant;
4993 /* 1 if so far this constructor's elements are all valid address constants. */
4994 static int constructor_simple;
4996 /* 1 if this constructor is erroneous so far. */
4997 static int constructor_erroneous;
4999 /* 1 if have called defer_addressed_constants. */
5000 static int constructor_subconstants_deferred;
5002 /* Structure for managing pending initializer elements, organized as an
5003 AVL tree. */
5005 struct init_node
5007 struct init_node *left, *right;
5008 struct init_node *parent;
5009 int balance;
5010 tree purpose;
5011 tree value;
5014 /* Tree of pending elements at this constructor level.
5015 These are elements encountered out of order
5016 which belong at places we haven't reached yet in actually
5017 writing the output.
5018 Will never hold tree nodes across GC runs. */
5019 static struct init_node *constructor_pending_elts;
5021 /* The SPELLING_DEPTH of this constructor. */
5022 static int constructor_depth;
5024 /* 0 if implicitly pushing constructor levels is allowed. */
5025 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
5027 static int require_constant_value;
5028 static int require_constant_elements;
5030 /* DECL node for which an initializer is being read.
5031 0 means we are reading a constructor expression
5032 such as (struct foo) {...}. */
5033 static tree constructor_decl;
5035 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5036 static const char *constructor_asmspec;
5038 /* Nonzero if this is an initializer for a top-level decl. */
5039 static int constructor_top_level;
5041 /* Nonzero if there were any member designators in this initializer. */
5042 static int constructor_designated;
5044 /* Nesting depth of designator list. */
5045 static int designator_depth;
5047 /* Nonzero if there were diagnosed errors in this designator list. */
5048 static int designator_errorneous;
5051 /* This stack has a level for each implicit or explicit level of
5052 structuring in the initializer, including the outermost one. It
5053 saves the values of most of the variables above. */
5055 struct constructor_range_stack;
5057 struct constructor_stack
5059 struct constructor_stack *next;
5060 tree type;
5061 tree fields;
5062 tree index;
5063 tree max_index;
5064 tree unfilled_index;
5065 tree unfilled_fields;
5066 tree bit_index;
5067 tree elements;
5068 struct init_node *pending_elts;
5069 int offset;
5070 int depth;
5071 /* If nonzero, this value should replace the entire
5072 constructor at this level. */
5073 tree replacement_value;
5074 struct constructor_range_stack *range_stack;
5075 char constant;
5076 char simple;
5077 char implicit;
5078 char erroneous;
5079 char outer;
5080 char incremental;
5081 char designated;
5084 struct constructor_stack *constructor_stack;
5086 /* This stack represents designators from some range designator up to
5087 the last designator in the list. */
5089 struct constructor_range_stack
5091 struct constructor_range_stack *next, *prev;
5092 struct constructor_stack *stack;
5093 tree range_start;
5094 tree index;
5095 tree range_end;
5096 tree fields;
5099 struct constructor_range_stack *constructor_range_stack;
5101 /* This stack records separate initializers that are nested.
5102 Nested initializers can't happen in ANSI C, but GNU C allows them
5103 in cases like { ... (struct foo) { ... } ... }. */
5105 struct initializer_stack
5107 struct initializer_stack *next;
5108 tree decl;
5109 const char *asmspec;
5110 struct constructor_stack *constructor_stack;
5111 struct constructor_range_stack *constructor_range_stack;
5112 tree elements;
5113 struct spelling *spelling;
5114 struct spelling *spelling_base;
5115 int spelling_size;
5116 char top_level;
5117 char require_constant_value;
5118 char require_constant_elements;
5119 char deferred;
5122 struct initializer_stack *initializer_stack;
5124 /* Prepare to parse and output the initializer for variable DECL. */
5126 void
5127 start_init (decl, asmspec_tree, top_level)
5128 tree decl;
5129 tree asmspec_tree;
5130 int top_level;
5132 const char *locus;
5133 struct initializer_stack *p
5134 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5135 const char *asmspec = 0;
5137 if (asmspec_tree)
5138 asmspec = TREE_STRING_POINTER (asmspec_tree);
5140 p->decl = constructor_decl;
5141 p->asmspec = constructor_asmspec;
5142 p->require_constant_value = require_constant_value;
5143 p->require_constant_elements = require_constant_elements;
5144 p->constructor_stack = constructor_stack;
5145 p->constructor_range_stack = constructor_range_stack;
5146 p->elements = constructor_elements;
5147 p->spelling = spelling;
5148 p->spelling_base = spelling_base;
5149 p->spelling_size = spelling_size;
5150 p->deferred = constructor_subconstants_deferred;
5151 p->top_level = constructor_top_level;
5152 p->next = initializer_stack;
5153 initializer_stack = p;
5155 constructor_decl = decl;
5156 constructor_asmspec = asmspec;
5157 constructor_subconstants_deferred = 0;
5158 constructor_designated = 0;
5159 constructor_top_level = top_level;
5161 if (decl != 0)
5163 require_constant_value = TREE_STATIC (decl);
5164 require_constant_elements
5165 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5166 /* For a scalar, you can always use any value to initialize,
5167 even within braces. */
5168 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5169 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5170 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5171 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5172 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5174 else
5176 require_constant_value = 0;
5177 require_constant_elements = 0;
5178 locus = "(anonymous)";
5181 constructor_stack = 0;
5182 constructor_range_stack = 0;
5184 missing_braces_mentioned = 0;
5186 spelling_base = 0;
5187 spelling_size = 0;
5188 RESTORE_SPELLING_DEPTH (0);
5190 if (locus)
5191 push_string (locus);
5194 void
5195 finish_init ()
5197 struct initializer_stack *p = initializer_stack;
5199 /* Output subconstants (string constants, usually)
5200 that were referenced within this initializer and saved up.
5201 Must do this if and only if we called defer_addressed_constants. */
5202 if (constructor_subconstants_deferred)
5203 output_deferred_addressed_constants ();
5205 /* Free the whole constructor stack of this initializer. */
5206 while (constructor_stack)
5208 struct constructor_stack *q = constructor_stack;
5209 constructor_stack = q->next;
5210 free (q);
5213 if (constructor_range_stack)
5214 abort ();
5216 /* Pop back to the data of the outer initializer (if any). */
5217 constructor_decl = p->decl;
5218 constructor_asmspec = p->asmspec;
5219 require_constant_value = p->require_constant_value;
5220 require_constant_elements = p->require_constant_elements;
5221 constructor_stack = p->constructor_stack;
5222 constructor_range_stack = p->constructor_range_stack;
5223 constructor_elements = p->elements;
5224 spelling = p->spelling;
5225 spelling_base = p->spelling_base;
5226 spelling_size = p->spelling_size;
5227 constructor_subconstants_deferred = p->deferred;
5228 constructor_top_level = p->top_level;
5229 initializer_stack = p->next;
5230 free (p);
5233 /* Call here when we see the initializer is surrounded by braces.
5234 This is instead of a call to push_init_level;
5235 it is matched by a call to pop_init_level.
5237 TYPE is the type to initialize, for a constructor expression.
5238 For an initializer for a decl, TYPE is zero. */
5240 void
5241 really_start_incremental_init (type)
5242 tree type;
5244 struct constructor_stack *p
5245 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5247 if (type == 0)
5248 type = TREE_TYPE (constructor_decl);
5250 p->type = constructor_type;
5251 p->fields = constructor_fields;
5252 p->index = constructor_index;
5253 p->max_index = constructor_max_index;
5254 p->unfilled_index = constructor_unfilled_index;
5255 p->unfilled_fields = constructor_unfilled_fields;
5256 p->bit_index = constructor_bit_index;
5257 p->elements = constructor_elements;
5258 p->constant = constructor_constant;
5259 p->simple = constructor_simple;
5260 p->erroneous = constructor_erroneous;
5261 p->pending_elts = constructor_pending_elts;
5262 p->depth = constructor_depth;
5263 p->replacement_value = 0;
5264 p->implicit = 0;
5265 p->range_stack = 0;
5266 p->outer = 0;
5267 p->incremental = constructor_incremental;
5268 p->designated = constructor_designated;
5269 p->next = 0;
5270 constructor_stack = p;
5272 constructor_constant = 1;
5273 constructor_simple = 1;
5274 constructor_depth = SPELLING_DEPTH ();
5275 constructor_elements = 0;
5276 constructor_pending_elts = 0;
5277 constructor_type = type;
5278 constructor_incremental = 1;
5279 constructor_designated = 0;
5280 designator_depth = 0;
5281 designator_errorneous = 0;
5283 if (TREE_CODE (constructor_type) == RECORD_TYPE
5284 || TREE_CODE (constructor_type) == UNION_TYPE)
5286 constructor_fields = TYPE_FIELDS (constructor_type);
5287 /* Skip any nameless bit fields at the beginning. */
5288 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5289 && DECL_NAME (constructor_fields) == 0)
5290 constructor_fields = TREE_CHAIN (constructor_fields);
5292 constructor_unfilled_fields = constructor_fields;
5293 constructor_bit_index = bitsize_zero_node;
5295 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5297 if (TYPE_DOMAIN (constructor_type))
5299 constructor_max_index
5300 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5302 /* Detect non-empty initializations of zero-length arrays. */
5303 if (constructor_max_index == NULL_TREE
5304 && TYPE_SIZE (constructor_type))
5305 constructor_max_index = build_int_2 (-1, -1);
5307 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5308 to initialize VLAs will cause an proper error; avoid tree
5309 checking errors as well by setting a safe value. */
5310 if (constructor_max_index
5311 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5312 constructor_max_index = build_int_2 (-1, -1);
5314 constructor_index
5315 = convert (bitsizetype,
5316 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5318 else
5319 constructor_index = bitsize_zero_node;
5321 constructor_unfilled_index = constructor_index;
5323 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5325 /* Vectors are like simple fixed-size arrays. */
5326 constructor_max_index =
5327 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5328 constructor_index = convert (bitsizetype, integer_zero_node);
5329 constructor_unfilled_index = constructor_index;
5331 else
5333 /* Handle the case of int x = {5}; */
5334 constructor_fields = constructor_type;
5335 constructor_unfilled_fields = constructor_type;
5339 /* Push down into a subobject, for initialization.
5340 If this is for an explicit set of braces, IMPLICIT is 0.
5341 If it is because the next element belongs at a lower level,
5342 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5344 void
5345 push_init_level (implicit)
5346 int implicit;
5348 struct constructor_stack *p;
5349 tree value = NULL_TREE;
5351 /* If we've exhausted any levels that didn't have braces,
5352 pop them now. */
5353 while (constructor_stack->implicit)
5355 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5356 || TREE_CODE (constructor_type) == UNION_TYPE)
5357 && constructor_fields == 0)
5358 process_init_element (pop_init_level (1));
5359 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5360 && tree_int_cst_lt (constructor_max_index, constructor_index))
5361 process_init_element (pop_init_level (1));
5362 else
5363 break;
5366 /* Unless this is an explicit brace, we need to preserve previous
5367 content if any. */
5368 if (implicit)
5370 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5371 || TREE_CODE (constructor_type) == UNION_TYPE)
5372 && constructor_fields)
5373 value = find_init_member (constructor_fields);
5374 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5375 value = find_init_member (constructor_index);
5378 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5379 p->type = constructor_type;
5380 p->fields = constructor_fields;
5381 p->index = constructor_index;
5382 p->max_index = constructor_max_index;
5383 p->unfilled_index = constructor_unfilled_index;
5384 p->unfilled_fields = constructor_unfilled_fields;
5385 p->bit_index = constructor_bit_index;
5386 p->elements = constructor_elements;
5387 p->constant = constructor_constant;
5388 p->simple = constructor_simple;
5389 p->erroneous = constructor_erroneous;
5390 p->pending_elts = constructor_pending_elts;
5391 p->depth = constructor_depth;
5392 p->replacement_value = 0;
5393 p->implicit = implicit;
5394 p->outer = 0;
5395 p->incremental = constructor_incremental;
5396 p->designated = constructor_designated;
5397 p->next = constructor_stack;
5398 p->range_stack = 0;
5399 constructor_stack = p;
5401 constructor_constant = 1;
5402 constructor_simple = 1;
5403 constructor_depth = SPELLING_DEPTH ();
5404 constructor_elements = 0;
5405 constructor_incremental = 1;
5406 constructor_designated = 0;
5407 constructor_pending_elts = 0;
5408 if (!implicit)
5410 p->range_stack = constructor_range_stack;
5411 constructor_range_stack = 0;
5412 designator_depth = 0;
5413 designator_errorneous = 0;
5416 /* Don't die if an entire brace-pair level is superfluous
5417 in the containing level. */
5418 if (constructor_type == 0)
5420 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5421 || TREE_CODE (constructor_type) == UNION_TYPE)
5423 /* Don't die if there are extra init elts at the end. */
5424 if (constructor_fields == 0)
5425 constructor_type = 0;
5426 else
5428 constructor_type = TREE_TYPE (constructor_fields);
5429 push_member_name (constructor_fields);
5430 constructor_depth++;
5433 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5435 constructor_type = TREE_TYPE (constructor_type);
5436 push_array_bounds (tree_low_cst (constructor_index, 0));
5437 constructor_depth++;
5440 if (constructor_type == 0)
5442 error_init ("extra brace group at end of initializer");
5443 constructor_fields = 0;
5444 constructor_unfilled_fields = 0;
5445 return;
5448 if (value && TREE_CODE (value) == CONSTRUCTOR)
5450 constructor_constant = TREE_CONSTANT (value);
5451 constructor_simple = TREE_STATIC (value);
5452 constructor_elements = TREE_OPERAND (value, 1);
5453 if (constructor_elements
5454 && (TREE_CODE (constructor_type) == RECORD_TYPE
5455 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5456 set_nonincremental_init ();
5459 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5461 missing_braces_mentioned = 1;
5462 warning_init ("missing braces around initializer");
5465 if (TREE_CODE (constructor_type) == RECORD_TYPE
5466 || TREE_CODE (constructor_type) == UNION_TYPE)
5468 constructor_fields = TYPE_FIELDS (constructor_type);
5469 /* Skip any nameless bit fields at the beginning. */
5470 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5471 && DECL_NAME (constructor_fields) == 0)
5472 constructor_fields = TREE_CHAIN (constructor_fields);
5474 constructor_unfilled_fields = constructor_fields;
5475 constructor_bit_index = bitsize_zero_node;
5477 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5479 if (TYPE_DOMAIN (constructor_type))
5481 constructor_max_index
5482 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5484 /* Detect non-empty initializations of zero-length arrays. */
5485 if (constructor_max_index == NULL_TREE
5486 && TYPE_SIZE (constructor_type))
5487 constructor_max_index = build_int_2 (-1, -1);
5489 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5490 to initialize VLAs will cause an proper error; avoid tree
5491 checking errors as well by setting a safe value. */
5492 if (constructor_max_index
5493 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5494 constructor_max_index = build_int_2 (-1, -1);
5496 constructor_index
5497 = convert (bitsizetype,
5498 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5500 else
5501 constructor_index = bitsize_zero_node;
5503 constructor_unfilled_index = constructor_index;
5504 if (value && TREE_CODE (value) == STRING_CST)
5506 /* We need to split the char/wchar array into individual
5507 characters, so that we don't have to special case it
5508 everywhere. */
5509 set_nonincremental_init_from_string (value);
5512 else
5514 warning_init ("braces around scalar initializer");
5515 constructor_fields = constructor_type;
5516 constructor_unfilled_fields = constructor_type;
5520 /* At the end of an implicit or explicit brace level,
5521 finish up that level of constructor.
5522 If we were outputting the elements as they are read, return 0
5523 from inner levels (process_init_element ignores that),
5524 but return error_mark_node from the outermost level
5525 (that's what we want to put in DECL_INITIAL).
5526 Otherwise, return a CONSTRUCTOR expression. */
5528 tree
5529 pop_init_level (implicit)
5530 int implicit;
5532 struct constructor_stack *p;
5533 tree constructor = 0;
5535 if (implicit == 0)
5537 /* When we come to an explicit close brace,
5538 pop any inner levels that didn't have explicit braces. */
5539 while (constructor_stack->implicit)
5540 process_init_element (pop_init_level (1));
5542 if (constructor_range_stack)
5543 abort ();
5546 p = constructor_stack;
5548 /* Error for initializing a flexible array member, or a zero-length
5549 array member in an inappropriate context. */
5550 if (constructor_type && constructor_fields
5551 && TREE_CODE (constructor_type) == ARRAY_TYPE
5552 && TYPE_DOMAIN (constructor_type)
5553 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5555 /* Silently discard empty initializations. The parser will
5556 already have pedwarned for empty brackets. */
5557 if (integer_zerop (constructor_unfilled_index))
5558 constructor_type = NULL_TREE;
5559 else if (! TYPE_SIZE (constructor_type))
5561 if (constructor_depth > 2)
5562 error_init ("initialization of flexible array member in a nested context");
5563 else if (pedantic)
5564 pedwarn_init ("initialization of a flexible array member");
5566 /* We have already issued an error message for the existence
5567 of a flexible array member not at the end of the structure.
5568 Discard the initializer so that we do not abort later. */
5569 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5570 constructor_type = NULL_TREE;
5572 else
5573 /* Zero-length arrays are no longer special, so we should no longer
5574 get here. */
5575 abort ();
5578 /* Warn when some struct elements are implicitly initialized to zero. */
5579 if (extra_warnings
5580 && constructor_type
5581 && TREE_CODE (constructor_type) == RECORD_TYPE
5582 && constructor_unfilled_fields)
5584 /* Do not warn for flexible array members or zero-length arrays. */
5585 while (constructor_unfilled_fields
5586 && (! DECL_SIZE (constructor_unfilled_fields)
5587 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5588 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5590 /* Do not warn if this level of the initializer uses member
5591 designators; it is likely to be deliberate. */
5592 if (constructor_unfilled_fields && !constructor_designated)
5594 push_member_name (constructor_unfilled_fields);
5595 warning_init ("missing initializer");
5596 RESTORE_SPELLING_DEPTH (constructor_depth);
5600 /* Now output all pending elements. */
5601 constructor_incremental = 1;
5602 output_pending_init_elements (1);
5604 /* Pad out the end of the structure. */
5605 if (p->replacement_value)
5606 /* If this closes a superfluous brace pair,
5607 just pass out the element between them. */
5608 constructor = p->replacement_value;
5609 else if (constructor_type == 0)
5611 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5612 && TREE_CODE (constructor_type) != UNION_TYPE
5613 && TREE_CODE (constructor_type) != ARRAY_TYPE
5614 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5616 /* A nonincremental scalar initializer--just return
5617 the element, after verifying there is just one. */
5618 if (constructor_elements == 0)
5620 if (!constructor_erroneous)
5621 error_init ("empty scalar initializer");
5622 constructor = error_mark_node;
5624 else if (TREE_CHAIN (constructor_elements) != 0)
5626 error_init ("extra elements in scalar initializer");
5627 constructor = TREE_VALUE (constructor_elements);
5629 else
5630 constructor = TREE_VALUE (constructor_elements);
5632 else
5634 if (constructor_erroneous)
5635 constructor = error_mark_node;
5636 else
5638 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5639 nreverse (constructor_elements));
5640 if (constructor_constant)
5641 TREE_CONSTANT (constructor) = 1;
5642 if (constructor_constant && constructor_simple)
5643 TREE_STATIC (constructor) = 1;
5647 constructor_type = p->type;
5648 constructor_fields = p->fields;
5649 constructor_index = p->index;
5650 constructor_max_index = p->max_index;
5651 constructor_unfilled_index = p->unfilled_index;
5652 constructor_unfilled_fields = p->unfilled_fields;
5653 constructor_bit_index = p->bit_index;
5654 constructor_elements = p->elements;
5655 constructor_constant = p->constant;
5656 constructor_simple = p->simple;
5657 constructor_erroneous = p->erroneous;
5658 constructor_incremental = p->incremental;
5659 constructor_designated = p->designated;
5660 constructor_pending_elts = p->pending_elts;
5661 constructor_depth = p->depth;
5662 if (!p->implicit)
5663 constructor_range_stack = p->range_stack;
5664 RESTORE_SPELLING_DEPTH (constructor_depth);
5666 constructor_stack = p->next;
5667 free (p);
5669 if (constructor == 0)
5671 if (constructor_stack == 0)
5672 return error_mark_node;
5673 return NULL_TREE;
5675 return constructor;
5678 /* Common handling for both array range and field name designators.
5679 ARRAY argument is non-zero for array ranges. Returns zero for success. */
5681 static int
5682 set_designator (array)
5683 int array;
5685 tree subtype;
5686 enum tree_code subcode;
5688 /* Don't die if an entire brace-pair level is superfluous
5689 in the containing level. */
5690 if (constructor_type == 0)
5691 return 1;
5693 /* If there were errors in this designator list already, bail out silently. */
5694 if (designator_errorneous)
5695 return 1;
5697 if (!designator_depth)
5699 if (constructor_range_stack)
5700 abort ();
5702 /* Designator list starts at the level of closest explicit
5703 braces. */
5704 while (constructor_stack->implicit)
5705 process_init_element (pop_init_level (1));
5706 constructor_designated = 1;
5707 return 0;
5710 if (constructor_no_implicit)
5712 error_init ("initialization designators may not nest");
5713 return 1;
5716 if (TREE_CODE (constructor_type) == RECORD_TYPE
5717 || TREE_CODE (constructor_type) == UNION_TYPE)
5719 subtype = TREE_TYPE (constructor_fields);
5720 if (subtype != error_mark_node)
5721 subtype = TYPE_MAIN_VARIANT (subtype);
5723 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5725 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5727 else
5728 abort ();
5730 subcode = TREE_CODE (subtype);
5731 if (array && subcode != ARRAY_TYPE)
5733 error_init ("array index in non-array initializer");
5734 return 1;
5736 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5738 error_init ("field name not in record or union initializer");
5739 return 1;
5742 constructor_designated = 1;
5743 push_init_level (2);
5744 return 0;
5747 /* If there are range designators in designator list, push a new designator
5748 to constructor_range_stack. RANGE_END is end of such stack range or
5749 NULL_TREE if there is no range designator at this level. */
5751 static void
5752 push_range_stack (range_end)
5753 tree range_end;
5755 struct constructor_range_stack *p;
5757 p = (struct constructor_range_stack *)
5758 ggc_alloc (sizeof (struct constructor_range_stack));
5759 p->prev = constructor_range_stack;
5760 p->next = 0;
5761 p->fields = constructor_fields;
5762 p->range_start = constructor_index;
5763 p->index = constructor_index;
5764 p->stack = constructor_stack;
5765 p->range_end = range_end;
5766 if (constructor_range_stack)
5767 constructor_range_stack->next = p;
5768 constructor_range_stack = p;
5771 /* Within an array initializer, specify the next index to be initialized.
5772 FIRST is that index. If LAST is nonzero, then initialize a range
5773 of indices, running from FIRST through LAST. */
5775 void
5776 set_init_index (first, last)
5777 tree first, last;
5779 if (set_designator (1))
5780 return;
5782 designator_errorneous = 1;
5784 while ((TREE_CODE (first) == NOP_EXPR
5785 || TREE_CODE (first) == CONVERT_EXPR
5786 || TREE_CODE (first) == NON_LVALUE_EXPR)
5787 && (TYPE_MODE (TREE_TYPE (first))
5788 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5789 first = TREE_OPERAND (first, 0);
5791 if (last)
5792 while ((TREE_CODE (last) == NOP_EXPR
5793 || TREE_CODE (last) == CONVERT_EXPR
5794 || TREE_CODE (last) == NON_LVALUE_EXPR)
5795 && (TYPE_MODE (TREE_TYPE (last))
5796 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5797 last = TREE_OPERAND (last, 0);
5799 if (TREE_CODE (first) != INTEGER_CST)
5800 error_init ("nonconstant array index in initializer");
5801 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5802 error_init ("nonconstant array index in initializer");
5803 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5804 error_init ("array index in non-array initializer");
5805 else if (constructor_max_index
5806 && tree_int_cst_lt (constructor_max_index, first))
5807 error_init ("array index in initializer exceeds array bounds");
5808 else
5810 constructor_index = convert (bitsizetype, first);
5812 if (last)
5814 if (tree_int_cst_equal (first, last))
5815 last = 0;
5816 else if (tree_int_cst_lt (last, first))
5818 error_init ("empty index range in initializer");
5819 last = 0;
5821 else
5823 last = convert (bitsizetype, last);
5824 if (constructor_max_index != 0
5825 && tree_int_cst_lt (constructor_max_index, last))
5827 error_init ("array index range in initializer exceeds array bounds");
5828 last = 0;
5833 designator_depth++;
5834 designator_errorneous = 0;
5835 if (constructor_range_stack || last)
5836 push_range_stack (last);
5840 /* Within a struct initializer, specify the next field to be initialized. */
5842 void
5843 set_init_label (fieldname)
5844 tree fieldname;
5846 tree tail;
5848 if (set_designator (0))
5849 return;
5851 designator_errorneous = 1;
5853 if (TREE_CODE (constructor_type) != RECORD_TYPE
5854 && TREE_CODE (constructor_type) != UNION_TYPE)
5856 error_init ("field name not in record or union initializer");
5857 return;
5860 for (tail = TYPE_FIELDS (constructor_type); tail;
5861 tail = TREE_CHAIN (tail))
5863 if (DECL_NAME (tail) == fieldname)
5864 break;
5867 if (tail == 0)
5868 error ("unknown field `%s' specified in initializer",
5869 IDENTIFIER_POINTER (fieldname));
5870 else
5872 constructor_fields = tail;
5873 designator_depth++;
5874 designator_errorneous = 0;
5875 if (constructor_range_stack)
5876 push_range_stack (NULL_TREE);
5880 /* Add a new initializer to the tree of pending initializers. PURPOSE
5881 identifies the initializer, either array index or field in a structure.
5882 VALUE is the value of that index or field. */
5884 static void
5885 add_pending_init (purpose, value)
5886 tree purpose, value;
5888 struct init_node *p, **q, *r;
5890 q = &constructor_pending_elts;
5891 p = 0;
5893 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5895 while (*q != 0)
5897 p = *q;
5898 if (tree_int_cst_lt (purpose, p->purpose))
5899 q = &p->left;
5900 else if (tree_int_cst_lt (p->purpose, purpose))
5901 q = &p->right;
5902 else
5904 if (TREE_SIDE_EFFECTS (p->value))
5905 warning_init ("initialized field with side-effects overwritten");
5906 p->value = value;
5907 return;
5911 else
5913 tree bitpos;
5915 bitpos = bit_position (purpose);
5916 while (*q != NULL)
5918 p = *q;
5919 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5920 q = &p->left;
5921 else if (p->purpose != purpose)
5922 q = &p->right;
5923 else
5925 if (TREE_SIDE_EFFECTS (p->value))
5926 warning_init ("initialized field with side-effects overwritten");
5927 p->value = value;
5928 return;
5933 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5934 r->purpose = purpose;
5935 r->value = value;
5937 *q = r;
5938 r->parent = p;
5939 r->left = 0;
5940 r->right = 0;
5941 r->balance = 0;
5943 while (p)
5945 struct init_node *s;
5947 if (r == p->left)
5949 if (p->balance == 0)
5950 p->balance = -1;
5951 else if (p->balance < 0)
5953 if (r->balance < 0)
5955 /* L rotation. */
5956 p->left = r->right;
5957 if (p->left)
5958 p->left->parent = p;
5959 r->right = p;
5961 p->balance = 0;
5962 r->balance = 0;
5964 s = p->parent;
5965 p->parent = r;
5966 r->parent = s;
5967 if (s)
5969 if (s->left == p)
5970 s->left = r;
5971 else
5972 s->right = r;
5974 else
5975 constructor_pending_elts = r;
5977 else
5979 /* LR rotation. */
5980 struct init_node *t = r->right;
5982 r->right = t->left;
5983 if (r->right)
5984 r->right->parent = r;
5985 t->left = r;
5987 p->left = t->right;
5988 if (p->left)
5989 p->left->parent = p;
5990 t->right = p;
5992 p->balance = t->balance < 0;
5993 r->balance = -(t->balance > 0);
5994 t->balance = 0;
5996 s = p->parent;
5997 p->parent = t;
5998 r->parent = t;
5999 t->parent = s;
6000 if (s)
6002 if (s->left == p)
6003 s->left = t;
6004 else
6005 s->right = t;
6007 else
6008 constructor_pending_elts = t;
6010 break;
6012 else
6014 /* p->balance == +1; growth of left side balances the node. */
6015 p->balance = 0;
6016 break;
6019 else /* r == p->right */
6021 if (p->balance == 0)
6022 /* Growth propagation from right side. */
6023 p->balance++;
6024 else if (p->balance > 0)
6026 if (r->balance > 0)
6028 /* R rotation. */
6029 p->right = r->left;
6030 if (p->right)
6031 p->right->parent = p;
6032 r->left = p;
6034 p->balance = 0;
6035 r->balance = 0;
6037 s = p->parent;
6038 p->parent = r;
6039 r->parent = s;
6040 if (s)
6042 if (s->left == p)
6043 s->left = r;
6044 else
6045 s->right = r;
6047 else
6048 constructor_pending_elts = r;
6050 else /* r->balance == -1 */
6052 /* RL rotation */
6053 struct init_node *t = r->left;
6055 r->left = t->right;
6056 if (r->left)
6057 r->left->parent = r;
6058 t->right = r;
6060 p->right = t->left;
6061 if (p->right)
6062 p->right->parent = p;
6063 t->left = p;
6065 r->balance = (t->balance < 0);
6066 p->balance = -(t->balance > 0);
6067 t->balance = 0;
6069 s = p->parent;
6070 p->parent = t;
6071 r->parent = t;
6072 t->parent = s;
6073 if (s)
6075 if (s->left == p)
6076 s->left = t;
6077 else
6078 s->right = t;
6080 else
6081 constructor_pending_elts = t;
6083 break;
6085 else
6087 /* p->balance == -1; growth of right side balances the node. */
6088 p->balance = 0;
6089 break;
6093 r = p;
6094 p = p->parent;
6098 /* Build AVL tree from a sorted chain. */
6100 static void
6101 set_nonincremental_init ()
6103 tree chain;
6105 if (TREE_CODE (constructor_type) != RECORD_TYPE
6106 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6107 return;
6109 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6110 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6111 constructor_elements = 0;
6112 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6114 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6115 /* Skip any nameless bit fields at the beginning. */
6116 while (constructor_unfilled_fields != 0
6117 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6118 && DECL_NAME (constructor_unfilled_fields) == 0)
6119 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6122 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6124 if (TYPE_DOMAIN (constructor_type))
6125 constructor_unfilled_index
6126 = convert (bitsizetype,
6127 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6128 else
6129 constructor_unfilled_index = bitsize_zero_node;
6131 constructor_incremental = 0;
6134 /* Build AVL tree from a string constant. */
6136 static void
6137 set_nonincremental_init_from_string (str)
6138 tree str;
6140 tree value, purpose, type;
6141 HOST_WIDE_INT val[2];
6142 const char *p, *end;
6143 int byte, wchar_bytes, charwidth, bitpos;
6145 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6146 abort ();
6148 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6149 == TYPE_PRECISION (char_type_node))
6150 wchar_bytes = 1;
6151 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6152 == TYPE_PRECISION (wchar_type_node))
6153 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6154 else
6155 abort ();
6157 charwidth = TYPE_PRECISION (char_type_node);
6158 type = TREE_TYPE (constructor_type);
6159 p = TREE_STRING_POINTER (str);
6160 end = p + TREE_STRING_LENGTH (str);
6162 for (purpose = bitsize_zero_node;
6163 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6164 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6166 if (wchar_bytes == 1)
6168 val[1] = (unsigned char) *p++;
6169 val[0] = 0;
6171 else
6173 val[0] = 0;
6174 val[1] = 0;
6175 for (byte = 0; byte < wchar_bytes; byte++)
6177 if (BYTES_BIG_ENDIAN)
6178 bitpos = (wchar_bytes - byte - 1) * charwidth;
6179 else
6180 bitpos = byte * charwidth;
6181 val[bitpos < HOST_BITS_PER_WIDE_INT]
6182 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6183 << (bitpos % HOST_BITS_PER_WIDE_INT);
6187 if (!TREE_UNSIGNED (type))
6189 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6190 if (bitpos < HOST_BITS_PER_WIDE_INT)
6192 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6194 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6195 val[0] = -1;
6198 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6200 if (val[1] < 0)
6201 val[0] = -1;
6203 else if (val[0] & (((HOST_WIDE_INT) 1)
6204 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6205 val[0] |= ((HOST_WIDE_INT) -1)
6206 << (bitpos - HOST_BITS_PER_WIDE_INT);
6209 value = build_int_2 (val[1], val[0]);
6210 TREE_TYPE (value) = type;
6211 add_pending_init (purpose, value);
6214 constructor_incremental = 0;
6217 /* Return value of FIELD in pending initializer or zero if the field was
6218 not initialized yet. */
6220 static tree
6221 find_init_member (field)
6222 tree field;
6224 struct init_node *p;
6226 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6228 if (constructor_incremental
6229 && tree_int_cst_lt (field, constructor_unfilled_index))
6230 set_nonincremental_init ();
6232 p = constructor_pending_elts;
6233 while (p)
6235 if (tree_int_cst_lt (field, p->purpose))
6236 p = p->left;
6237 else if (tree_int_cst_lt (p->purpose, field))
6238 p = p->right;
6239 else
6240 return p->value;
6243 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6245 tree bitpos = bit_position (field);
6247 if (constructor_incremental
6248 && (!constructor_unfilled_fields
6249 || tree_int_cst_lt (bitpos,
6250 bit_position (constructor_unfilled_fields))))
6251 set_nonincremental_init ();
6253 p = constructor_pending_elts;
6254 while (p)
6256 if (field == p->purpose)
6257 return p->value;
6258 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6259 p = p->left;
6260 else
6261 p = p->right;
6264 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6266 if (constructor_elements
6267 && TREE_PURPOSE (constructor_elements) == field)
6268 return TREE_VALUE (constructor_elements);
6270 return 0;
6273 /* "Output" the next constructor element.
6274 At top level, really output it to assembler code now.
6275 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6276 TYPE is the data type that the containing data type wants here.
6277 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6279 PENDING if non-nil means output pending elements that belong
6280 right after this element. (PENDING is normally 1;
6281 it is 0 while outputting pending elements, to avoid recursion.) */
6283 static void
6284 output_init_element (value, type, field, pending)
6285 tree value, type, field;
6286 int pending;
6288 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6289 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6290 && !(TREE_CODE (value) == STRING_CST
6291 && TREE_CODE (type) == ARRAY_TYPE
6292 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6293 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6294 TYPE_MAIN_VARIANT (type))))
6295 value = default_conversion (value);
6297 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6298 && require_constant_value && !flag_isoc99 && pending)
6300 /* As an extension, allow initializing objects with static storage
6301 duration with compound literals (which are then treated just as
6302 the brace enclosed list they contain). */
6303 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6304 value = DECL_INITIAL (decl);
6307 if (value == error_mark_node)
6308 constructor_erroneous = 1;
6309 else if (!TREE_CONSTANT (value))
6310 constructor_constant = 0;
6311 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6312 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6313 || TREE_CODE (constructor_type) == UNION_TYPE)
6314 && DECL_C_BIT_FIELD (field)
6315 && TREE_CODE (value) != INTEGER_CST))
6316 constructor_simple = 0;
6318 if (require_constant_value && ! TREE_CONSTANT (value))
6320 error_init ("initializer element is not constant");
6321 value = error_mark_node;
6323 else if (require_constant_elements
6324 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6325 pedwarn ("initializer element is not computable at load time");
6327 /* If this field is empty (and not at the end of structure),
6328 don't do anything other than checking the initializer. */
6329 if (field
6330 && (TREE_TYPE (field) == error_mark_node
6331 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6332 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6333 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6334 || TREE_CHAIN (field)))))
6335 return;
6337 value = digest_init (type, value, require_constant_value,
6338 require_constant_elements);
6339 if (value == error_mark_node)
6341 constructor_erroneous = 1;
6342 return;
6345 /* If this element doesn't come next in sequence,
6346 put it on constructor_pending_elts. */
6347 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6348 && (!constructor_incremental
6349 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6351 if (constructor_incremental
6352 && tree_int_cst_lt (field, constructor_unfilled_index))
6353 set_nonincremental_init ();
6355 add_pending_init (field, value);
6356 return;
6358 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6359 && (!constructor_incremental
6360 || field != constructor_unfilled_fields))
6362 /* We do this for records but not for unions. In a union,
6363 no matter which field is specified, it can be initialized
6364 right away since it starts at the beginning of the union. */
6365 if (constructor_incremental)
6367 if (!constructor_unfilled_fields)
6368 set_nonincremental_init ();
6369 else
6371 tree bitpos, unfillpos;
6373 bitpos = bit_position (field);
6374 unfillpos = bit_position (constructor_unfilled_fields);
6376 if (tree_int_cst_lt (bitpos, unfillpos))
6377 set_nonincremental_init ();
6381 add_pending_init (field, value);
6382 return;
6384 else if (TREE_CODE (constructor_type) == UNION_TYPE
6385 && constructor_elements)
6387 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6388 warning_init ("initialized field with side-effects overwritten");
6390 /* We can have just one union field set. */
6391 constructor_elements = 0;
6394 /* Otherwise, output this element either to
6395 constructor_elements or to the assembler file. */
6397 if (field && TREE_CODE (field) == INTEGER_CST)
6398 field = copy_node (field);
6399 constructor_elements
6400 = tree_cons (field, value, constructor_elements);
6402 /* Advance the variable that indicates sequential elements output. */
6403 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6404 constructor_unfilled_index
6405 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6406 bitsize_one_node);
6407 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6409 constructor_unfilled_fields
6410 = TREE_CHAIN (constructor_unfilled_fields);
6412 /* Skip any nameless bit fields. */
6413 while (constructor_unfilled_fields != 0
6414 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6415 && DECL_NAME (constructor_unfilled_fields) == 0)
6416 constructor_unfilled_fields =
6417 TREE_CHAIN (constructor_unfilled_fields);
6419 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6420 constructor_unfilled_fields = 0;
6422 /* Now output any pending elements which have become next. */
6423 if (pending)
6424 output_pending_init_elements (0);
6427 /* Output any pending elements which have become next.
6428 As we output elements, constructor_unfilled_{fields,index}
6429 advances, which may cause other elements to become next;
6430 if so, they too are output.
6432 If ALL is 0, we return when there are
6433 no more pending elements to output now.
6435 If ALL is 1, we output space as necessary so that
6436 we can output all the pending elements. */
6438 static void
6439 output_pending_init_elements (all)
6440 int all;
6442 struct init_node *elt = constructor_pending_elts;
6443 tree next;
6445 retry:
6447 /* Look thru the whole pending tree.
6448 If we find an element that should be output now,
6449 output it. Otherwise, set NEXT to the element
6450 that comes first among those still pending. */
6452 next = 0;
6453 while (elt)
6455 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6457 if (tree_int_cst_equal (elt->purpose,
6458 constructor_unfilled_index))
6459 output_init_element (elt->value,
6460 TREE_TYPE (constructor_type),
6461 constructor_unfilled_index, 0);
6462 else if (tree_int_cst_lt (constructor_unfilled_index,
6463 elt->purpose))
6465 /* Advance to the next smaller node. */
6466 if (elt->left)
6467 elt = elt->left;
6468 else
6470 /* We have reached the smallest node bigger than the
6471 current unfilled index. Fill the space first. */
6472 next = elt->purpose;
6473 break;
6476 else
6478 /* Advance to the next bigger node. */
6479 if (elt->right)
6480 elt = elt->right;
6481 else
6483 /* We have reached the biggest node in a subtree. Find
6484 the parent of it, which is the next bigger node. */
6485 while (elt->parent && elt->parent->right == elt)
6486 elt = elt->parent;
6487 elt = elt->parent;
6488 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6489 elt->purpose))
6491 next = elt->purpose;
6492 break;
6497 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6498 || TREE_CODE (constructor_type) == UNION_TYPE)
6500 tree ctor_unfilled_bitpos, elt_bitpos;
6502 /* If the current record is complete we are done. */
6503 if (constructor_unfilled_fields == 0)
6504 break;
6506 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6507 elt_bitpos = bit_position (elt->purpose);
6508 /* We can't compare fields here because there might be empty
6509 fields in between. */
6510 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6512 constructor_unfilled_fields = elt->purpose;
6513 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6514 elt->purpose, 0);
6516 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6518 /* Advance to the next smaller node. */
6519 if (elt->left)
6520 elt = elt->left;
6521 else
6523 /* We have reached the smallest node bigger than the
6524 current unfilled field. Fill the space first. */
6525 next = elt->purpose;
6526 break;
6529 else
6531 /* Advance to the next bigger node. */
6532 if (elt->right)
6533 elt = elt->right;
6534 else
6536 /* We have reached the biggest node in a subtree. Find
6537 the parent of it, which is the next bigger node. */
6538 while (elt->parent && elt->parent->right == elt)
6539 elt = elt->parent;
6540 elt = elt->parent;
6541 if (elt
6542 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6543 bit_position (elt->purpose))))
6545 next = elt->purpose;
6546 break;
6553 /* Ordinarily return, but not if we want to output all
6554 and there are elements left. */
6555 if (! (all && next != 0))
6556 return;
6558 /* If it's not incremental, just skip over the gap, so that after
6559 jumping to retry we will output the next successive element. */
6560 if (TREE_CODE (constructor_type) == RECORD_TYPE
6561 || TREE_CODE (constructor_type) == UNION_TYPE)
6562 constructor_unfilled_fields = next;
6563 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6564 constructor_unfilled_index = next;
6566 /* ELT now points to the node in the pending tree with the next
6567 initializer to output. */
6568 goto retry;
6571 /* Add one non-braced element to the current constructor level.
6572 This adjusts the current position within the constructor's type.
6573 This may also start or terminate implicit levels
6574 to handle a partly-braced initializer.
6576 Once this has found the correct level for the new element,
6577 it calls output_init_element. */
6579 void
6580 process_init_element (value)
6581 tree value;
6583 tree orig_value = value;
6584 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6586 designator_depth = 0;
6587 designator_errorneous = 0;
6589 /* Handle superfluous braces around string cst as in
6590 char x[] = {"foo"}; */
6591 if (string_flag
6592 && constructor_type
6593 && TREE_CODE (constructor_type) == ARRAY_TYPE
6594 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6595 && integer_zerop (constructor_unfilled_index))
6597 if (constructor_stack->replacement_value)
6598 error_init ("excess elements in char array initializer");
6599 constructor_stack->replacement_value = value;
6600 return;
6603 if (constructor_stack->replacement_value != 0)
6605 error_init ("excess elements in struct initializer");
6606 return;
6609 /* Ignore elements of a brace group if it is entirely superfluous
6610 and has already been diagnosed. */
6611 if (constructor_type == 0)
6612 return;
6614 /* If we've exhausted any levels that didn't have braces,
6615 pop them now. */
6616 while (constructor_stack->implicit)
6618 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6619 || TREE_CODE (constructor_type) == UNION_TYPE)
6620 && constructor_fields == 0)
6621 process_init_element (pop_init_level (1));
6622 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6623 && (constructor_max_index == 0
6624 || tree_int_cst_lt (constructor_max_index,
6625 constructor_index)))
6626 process_init_element (pop_init_level (1));
6627 else
6628 break;
6631 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6632 if (constructor_range_stack)
6634 /* If value is a compound literal and we'll be just using its
6635 content, don't put it into a SAVE_EXPR. */
6636 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
6637 || !require_constant_value
6638 || flag_isoc99)
6639 value = save_expr (value);
6642 while (1)
6644 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6646 tree fieldtype;
6647 enum tree_code fieldcode;
6649 if (constructor_fields == 0)
6651 pedwarn_init ("excess elements in struct initializer");
6652 break;
6655 fieldtype = TREE_TYPE (constructor_fields);
6656 if (fieldtype != error_mark_node)
6657 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6658 fieldcode = TREE_CODE (fieldtype);
6660 /* Accept a string constant to initialize a subarray. */
6661 if (value != 0
6662 && fieldcode == ARRAY_TYPE
6663 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6664 && string_flag)
6665 value = orig_value;
6666 /* Otherwise, if we have come to a subaggregate,
6667 and we don't have an element of its type, push into it. */
6668 else if (value != 0 && !constructor_no_implicit
6669 && value != error_mark_node
6670 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6671 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6672 || fieldcode == UNION_TYPE))
6674 push_init_level (1);
6675 continue;
6678 if (value)
6680 push_member_name (constructor_fields);
6681 output_init_element (value, fieldtype, constructor_fields, 1);
6682 RESTORE_SPELLING_DEPTH (constructor_depth);
6684 else
6685 /* Do the bookkeeping for an element that was
6686 directly output as a constructor. */
6688 /* For a record, keep track of end position of last field. */
6689 if (DECL_SIZE (constructor_fields))
6690 constructor_bit_index
6691 = size_binop (PLUS_EXPR,
6692 bit_position (constructor_fields),
6693 DECL_SIZE (constructor_fields));
6695 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6696 /* Skip any nameless bit fields. */
6697 while (constructor_unfilled_fields != 0
6698 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6699 && DECL_NAME (constructor_unfilled_fields) == 0)
6700 constructor_unfilled_fields =
6701 TREE_CHAIN (constructor_unfilled_fields);
6704 constructor_fields = TREE_CHAIN (constructor_fields);
6705 /* Skip any nameless bit fields at the beginning. */
6706 while (constructor_fields != 0
6707 && DECL_C_BIT_FIELD (constructor_fields)
6708 && DECL_NAME (constructor_fields) == 0)
6709 constructor_fields = TREE_CHAIN (constructor_fields);
6711 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6713 tree fieldtype;
6714 enum tree_code fieldcode;
6716 if (constructor_fields == 0)
6718 pedwarn_init ("excess elements in union initializer");
6719 break;
6722 fieldtype = TREE_TYPE (constructor_fields);
6723 if (fieldtype != error_mark_node)
6724 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6725 fieldcode = TREE_CODE (fieldtype);
6727 /* Warn that traditional C rejects initialization of unions.
6728 We skip the warning if the value is zero. This is done
6729 under the assumption that the zero initializer in user
6730 code appears conditioned on e.g. __STDC__ to avoid
6731 "missing initializer" warnings and relies on default
6732 initialization to zero in the traditional C case.
6733 We also skip the warning if the initializer is designated,
6734 again on the assumption that this must be conditional on
6735 __STDC__ anyway (and we've already complained about the
6736 member-designator already). */
6737 if (warn_traditional && !in_system_header && !constructor_designated
6738 && !(value && (integer_zerop (value) || real_zerop (value))))
6739 warning ("traditional C rejects initialization of unions");
6741 /* Accept a string constant to initialize a subarray. */
6742 if (value != 0
6743 && fieldcode == ARRAY_TYPE
6744 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6745 && string_flag)
6746 value = orig_value;
6747 /* Otherwise, if we have come to a subaggregate,
6748 and we don't have an element of its type, push into it. */
6749 else if (value != 0 && !constructor_no_implicit
6750 && value != error_mark_node
6751 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6752 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6753 || fieldcode == UNION_TYPE))
6755 push_init_level (1);
6756 continue;
6759 if (value)
6761 push_member_name (constructor_fields);
6762 output_init_element (value, fieldtype, constructor_fields, 1);
6763 RESTORE_SPELLING_DEPTH (constructor_depth);
6765 else
6766 /* Do the bookkeeping for an element that was
6767 directly output as a constructor. */
6769 constructor_bit_index = DECL_SIZE (constructor_fields);
6770 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6773 constructor_fields = 0;
6775 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6777 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6778 enum tree_code eltcode = TREE_CODE (elttype);
6780 /* Accept a string constant to initialize a subarray. */
6781 if (value != 0
6782 && eltcode == ARRAY_TYPE
6783 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6784 && string_flag)
6785 value = orig_value;
6786 /* Otherwise, if we have come to a subaggregate,
6787 and we don't have an element of its type, push into it. */
6788 else if (value != 0 && !constructor_no_implicit
6789 && value != error_mark_node
6790 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6791 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6792 || eltcode == UNION_TYPE))
6794 push_init_level (1);
6795 continue;
6798 if (constructor_max_index != 0
6799 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6800 || integer_all_onesp (constructor_max_index)))
6802 pedwarn_init ("excess elements in array initializer");
6803 break;
6806 /* Now output the actual element. */
6807 if (value)
6809 push_array_bounds (tree_low_cst (constructor_index, 0));
6810 output_init_element (value, elttype, constructor_index, 1);
6811 RESTORE_SPELLING_DEPTH (constructor_depth);
6814 constructor_index
6815 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6817 if (! value)
6818 /* If we are doing the bookkeeping for an element that was
6819 directly output as a constructor, we must update
6820 constructor_unfilled_index. */
6821 constructor_unfilled_index = constructor_index;
6823 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6825 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6827 /* Do a basic check of initializer size. Note that vectors
6828 always have a fixed size derived from their type. */
6829 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6831 pedwarn_init ("excess elements in vector initializer");
6832 break;
6835 /* Now output the actual element. */
6836 if (value)
6837 output_init_element (value, elttype, constructor_index, 1);
6839 constructor_index
6840 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6842 if (! value)
6843 /* If we are doing the bookkeeping for an element that was
6844 directly output as a constructor, we must update
6845 constructor_unfilled_index. */
6846 constructor_unfilled_index = constructor_index;
6849 /* Handle the sole element allowed in a braced initializer
6850 for a scalar variable. */
6851 else if (constructor_fields == 0)
6853 pedwarn_init ("excess elements in scalar initializer");
6854 break;
6856 else
6858 if (value)
6859 output_init_element (value, constructor_type, NULL_TREE, 1);
6860 constructor_fields = 0;
6863 /* Handle range initializers either at this level or anywhere higher
6864 in the designator stack. */
6865 if (constructor_range_stack)
6867 struct constructor_range_stack *p, *range_stack;
6868 int finish = 0;
6870 range_stack = constructor_range_stack;
6871 constructor_range_stack = 0;
6872 while (constructor_stack != range_stack->stack)
6874 if (!constructor_stack->implicit)
6875 abort ();
6876 process_init_element (pop_init_level (1));
6878 for (p = range_stack;
6879 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6880 p = p->prev)
6882 if (!constructor_stack->implicit)
6883 abort ();
6884 process_init_element (pop_init_level (1));
6887 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6888 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6889 finish = 1;
6891 while (1)
6893 constructor_index = p->index;
6894 constructor_fields = p->fields;
6895 if (finish && p->range_end && p->index == p->range_start)
6897 finish = 0;
6898 p->prev = 0;
6900 p = p->next;
6901 if (!p)
6902 break;
6903 push_init_level (2);
6904 p->stack = constructor_stack;
6905 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6906 p->index = p->range_start;
6909 if (!finish)
6910 constructor_range_stack = range_stack;
6911 continue;
6914 break;
6917 constructor_range_stack = 0;
6920 /* Build a simple asm-statement, from one string literal. */
6921 tree
6922 simple_asm_stmt (expr)
6923 tree expr;
6925 STRIP_NOPS (expr);
6927 if (TREE_CODE (expr) == ADDR_EXPR)
6928 expr = TREE_OPERAND (expr, 0);
6930 if (TREE_CODE (expr) == STRING_CST)
6932 tree stmt;
6934 if (TREE_CHAIN (expr))
6935 expr = combine_strings (expr);
6936 stmt = add_stmt (build_stmt (ASM_STMT, NULL_TREE, expr,
6937 NULL_TREE, NULL_TREE,
6938 NULL_TREE));
6939 ASM_INPUT_P (stmt) = 1;
6940 return stmt;
6943 error ("argument of `asm' is not a constant string");
6944 return NULL_TREE;
6947 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6948 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6950 tree
6951 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6952 tree cv_qualifier;
6953 tree string;
6954 tree outputs;
6955 tree inputs;
6956 tree clobbers;
6958 tree tail;
6960 if (TREE_CHAIN (string))
6961 string = combine_strings (string);
6962 if (TREE_CODE (string) != STRING_CST)
6964 error ("asm template is not a string constant");
6965 return NULL_TREE;
6968 if (cv_qualifier != NULL_TREE
6969 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6971 warning ("%s qualifier ignored on asm",
6972 IDENTIFIER_POINTER (cv_qualifier));
6973 cv_qualifier = NULL_TREE;
6976 /* We can remove output conversions that change the type,
6977 but not the mode. */
6978 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6980 tree output = TREE_VALUE (tail);
6982 STRIP_NOPS (output);
6983 TREE_VALUE (tail) = output;
6985 /* Allow conversions as LHS here. build_modify_expr as called below
6986 will do the right thing with them. */
6987 while (TREE_CODE (output) == NOP_EXPR
6988 || TREE_CODE (output) == CONVERT_EXPR
6989 || TREE_CODE (output) == FLOAT_EXPR
6990 || TREE_CODE (output) == FIX_TRUNC_EXPR
6991 || TREE_CODE (output) == FIX_FLOOR_EXPR
6992 || TREE_CODE (output) == FIX_ROUND_EXPR
6993 || TREE_CODE (output) == FIX_CEIL_EXPR)
6994 output = TREE_OPERAND (output, 0);
6996 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6999 /* Remove output conversions that change the type but not the mode. */
7000 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
7002 tree output = TREE_VALUE (tail);
7003 STRIP_NOPS (output);
7004 TREE_VALUE (tail) = output;
7007 /* Perform default conversions on array and function inputs.
7008 Don't do this for other types as it would screw up operands
7009 expected to be in memory. */
7010 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
7011 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
7013 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
7014 outputs, inputs, clobbers));
7017 /* Expand an ASM statement with operands, handling output operands
7018 that are not variables or INDIRECT_REFS by transforming such
7019 cases into cases that expand_asm_operands can handle.
7021 Arguments are same as for expand_asm_operands. */
7023 void
7024 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
7025 tree string, outputs, inputs, clobbers;
7026 int vol;
7027 const char *filename;
7028 int line;
7030 int noutputs = list_length (outputs);
7031 int i;
7032 /* o[I] is the place that output number I should be written. */
7033 tree *o = (tree *) alloca (noutputs * sizeof (tree));
7034 tree tail;
7036 /* Record the contents of OUTPUTS before it is modified. */
7037 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7038 o[i] = TREE_VALUE (tail);
7040 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
7041 OUTPUTS some trees for where the values were actually stored. */
7042 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
7044 /* Copy all the intermediate outputs into the specified outputs. */
7045 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7047 if (o[i] != TREE_VALUE (tail))
7049 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
7050 NULL_RTX, VOIDmode, EXPAND_NORMAL);
7051 free_temp_slots ();
7053 /* Restore the original value so that it's correct the next
7054 time we expand this function. */
7055 TREE_VALUE (tail) = o[i];
7057 /* Detect modification of read-only values.
7058 (Otherwise done by build_modify_expr.) */
7059 else
7061 tree type = TREE_TYPE (o[i]);
7062 if (TREE_READONLY (o[i])
7063 || TYPE_READONLY (type)
7064 || ((TREE_CODE (type) == RECORD_TYPE
7065 || TREE_CODE (type) == UNION_TYPE)
7066 && C_TYPE_FIELDS_READONLY (type)))
7067 readonly_warning (o[i], "modification by `asm'");
7071 /* Those MODIFY_EXPRs could do autoincrements. */
7072 emit_queue ();
7075 /* Expand a C `return' statement.
7076 RETVAL is the expression for what to return,
7077 or a null pointer for `return;' with no value. */
7079 tree
7080 c_expand_return (retval)
7081 tree retval;
7083 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
7085 if (TREE_THIS_VOLATILE (current_function_decl))
7086 warning ("function declared `noreturn' has a `return' statement");
7088 if (!retval)
7090 current_function_returns_null = 1;
7091 if ((warn_return_type || flag_isoc99)
7092 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7093 pedwarn_c99 ("`return' with no value, in function returning non-void");
7095 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7097 current_function_returns_null = 1;
7098 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7099 pedwarn ("`return' with a value, in function returning void");
7101 else
7103 tree t = convert_for_assignment (valtype, retval, _("return"),
7104 NULL_TREE, NULL_TREE, 0);
7105 tree res = DECL_RESULT (current_function_decl);
7106 tree inner;
7108 current_function_returns_value = 1;
7109 if (t == error_mark_node)
7110 return NULL_TREE;
7112 inner = t = convert (TREE_TYPE (res), t);
7114 /* Strip any conversions, additions, and subtractions, and see if
7115 we are returning the address of a local variable. Warn if so. */
7116 while (1)
7118 switch (TREE_CODE (inner))
7120 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7121 case PLUS_EXPR:
7122 inner = TREE_OPERAND (inner, 0);
7123 continue;
7125 case MINUS_EXPR:
7126 /* If the second operand of the MINUS_EXPR has a pointer
7127 type (or is converted from it), this may be valid, so
7128 don't give a warning. */
7130 tree op1 = TREE_OPERAND (inner, 1);
7132 while (! POINTER_TYPE_P (TREE_TYPE (op1))
7133 && (TREE_CODE (op1) == NOP_EXPR
7134 || TREE_CODE (op1) == NON_LVALUE_EXPR
7135 || TREE_CODE (op1) == CONVERT_EXPR))
7136 op1 = TREE_OPERAND (op1, 0);
7138 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7139 break;
7141 inner = TREE_OPERAND (inner, 0);
7142 continue;
7145 case ADDR_EXPR:
7146 inner = TREE_OPERAND (inner, 0);
7148 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7149 inner = TREE_OPERAND (inner, 0);
7151 if (TREE_CODE (inner) == VAR_DECL
7152 && ! DECL_EXTERNAL (inner)
7153 && ! TREE_STATIC (inner)
7154 && DECL_CONTEXT (inner) == current_function_decl)
7155 warning ("function returns address of local variable");
7156 break;
7158 default:
7159 break;
7162 break;
7165 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7168 return add_stmt (build_return_stmt (retval));
7171 struct c_switch {
7172 /* The SWITCH_STMT being built. */
7173 tree switch_stmt;
7174 /* A splay-tree mapping the low element of a case range to the high
7175 element, or NULL_TREE if there is no high element. Used to
7176 determine whether or not a new case label duplicates an old case
7177 label. We need a tree, rather than simply a hash table, because
7178 of the GNU case range extension. */
7179 splay_tree cases;
7180 /* The next node on the stack. */
7181 struct c_switch *next;
7184 /* A stack of the currently active switch statements. The innermost
7185 switch statement is on the top of the stack. There is no need to
7186 mark the stack for garbage collection because it is only active
7187 during the processing of the body of a function, and we never
7188 collect at that point. */
7190 static struct c_switch *switch_stack;
7192 /* Start a C switch statement, testing expression EXP. Return the new
7193 SWITCH_STMT. */
7195 tree
7196 c_start_case (exp)
7197 tree exp;
7199 enum tree_code code;
7200 tree type, orig_type = error_mark_node;
7201 struct c_switch *cs;
7203 if (exp != error_mark_node)
7205 code = TREE_CODE (TREE_TYPE (exp));
7206 orig_type = TREE_TYPE (exp);
7208 if (! INTEGRAL_TYPE_P (orig_type)
7209 && code != ERROR_MARK)
7211 error ("switch quantity not an integer");
7212 exp = integer_zero_node;
7214 else
7216 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7218 if (warn_traditional && !in_system_header
7219 && (type == long_integer_type_node
7220 || type == long_unsigned_type_node))
7221 warning ("`long' switch expression not converted to `int' in ISO C");
7223 exp = default_conversion (exp);
7224 type = TREE_TYPE (exp);
7228 /* Add this new SWITCH_STMT to the stack. */
7229 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7230 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
7231 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7232 cs->next = switch_stack;
7233 switch_stack = cs;
7235 return add_stmt (switch_stack->switch_stmt);
7238 /* Process a case label. */
7240 tree
7241 do_case (low_value, high_value)
7242 tree low_value;
7243 tree high_value;
7245 tree label = NULL_TREE;
7247 if (switch_stack)
7249 label = c_add_case_label (switch_stack->cases,
7250 SWITCH_COND (switch_stack->switch_stmt),
7251 low_value, high_value);
7252 if (label == error_mark_node)
7253 label = NULL_TREE;
7255 else if (low_value)
7256 error ("case label not within a switch statement");
7257 else
7258 error ("`default' label not within a switch statement");
7260 return label;
7263 /* Finish the switch statement. */
7265 void
7266 c_finish_case ()
7268 struct c_switch *cs = switch_stack;
7270 RECHAIN_STMTS (cs->switch_stmt, SWITCH_BODY (cs->switch_stmt));
7272 /* Pop the stack. */
7273 switch_stack = switch_stack->next;
7274 splay_tree_delete (cs->cases);
7275 free (cs);