* recog.c (asm_operand_ok): Allow float CONST_VECTORs for 'F'.
[official-gcc.git] / gcc / c-typeck.c
blobf6e8d538d7531a5229bda9e011d47a91c3e56a40
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_diff PARAMS ((tree, tree));
62 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree, int));
63 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
64 static tree internal_build_compound_expr PARAMS ((tree, int));
65 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
66 tree, tree, int));
67 static void warn_for_assignment PARAMS ((const char *, const char *,
68 tree, int));
69 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
70 static void push_string PARAMS ((const char *));
71 static void push_member_name PARAMS ((tree));
72 static void push_array_bounds PARAMS ((int));
73 static int spelling_length PARAMS ((void));
74 static char *print_spelling PARAMS ((char *));
75 static void warning_init PARAMS ((const char *));
76 static tree digest_init PARAMS ((tree, tree, int));
77 static void output_init_element PARAMS ((tree, tree, tree, int));
78 static void output_pending_init_elements PARAMS ((int));
79 static int set_designator PARAMS ((int));
80 static void push_range_stack PARAMS ((tree));
81 static void add_pending_init PARAMS ((tree, tree));
82 static void set_nonincremental_init PARAMS ((void));
83 static void set_nonincremental_init_from_string PARAMS ((tree));
84 static tree find_init_member PARAMS ((tree));
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.) */
89 tree
90 require_complete_type (value)
91 tree value;
93 tree type = TREE_TYPE (value);
95 if (value == error_mark_node || type == error_mark_node)
96 return error_mark_node;
98 /* First, detect a valid value with a complete type. */
99 if (COMPLETE_TYPE_P (type))
100 return value;
102 c_incomplete_type_error (value, type);
103 return error_mark_node;
106 /* Print an error message for invalid use of an incomplete type.
107 VALUE is the expression that was used (or 0 if that isn't known)
108 and TYPE is the type that was invalid. */
110 void
111 c_incomplete_type_error (value, type)
112 tree value;
113 tree type;
115 const char *type_code_string;
117 /* Avoid duplicate error message. */
118 if (TREE_CODE (type) == ERROR_MARK)
119 return;
121 if (value != 0 && (TREE_CODE (value) == VAR_DECL
122 || TREE_CODE (value) == PARM_DECL))
123 error ("`%s' has an incomplete type",
124 IDENTIFIER_POINTER (DECL_NAME (value)));
125 else
127 retry:
128 /* We must print an error message. Be clever about what it says. */
130 switch (TREE_CODE (type))
132 case RECORD_TYPE:
133 type_code_string = "struct";
134 break;
136 case UNION_TYPE:
137 type_code_string = "union";
138 break;
140 case ENUMERAL_TYPE:
141 type_code_string = "enum";
142 break;
144 case VOID_TYPE:
145 error ("invalid use of void expression");
146 return;
148 case ARRAY_TYPE:
149 if (TYPE_DOMAIN (type))
151 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
153 error ("invalid use of flexible array member");
154 return;
156 type = TREE_TYPE (type);
157 goto retry;
159 error ("invalid use of array with unspecified bounds");
160 return;
162 default:
163 abort ();
166 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
167 error ("invalid use of undefined type `%s %s'",
168 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
169 else
170 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
171 error ("invalid use of incomplete typedef `%s'",
172 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
176 /* Given a type, apply default promotions wrt unnamed function
177 arguments and return the new type. */
179 tree
180 c_type_promotes_to (type)
181 tree type;
183 if (TYPE_MAIN_VARIANT (type) == float_type_node)
184 return double_type_node;
186 if (c_promoting_integer_type_p (type))
188 /* Preserve unsignedness if not really getting any wider. */
189 if (TREE_UNSIGNED (type)
190 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
191 return unsigned_type_node;
192 return integer_type_node;
195 return type;
198 /* Return a variant of TYPE which has all the type qualifiers of LIKE
199 as well as those of TYPE. */
201 static tree
202 qualify_type (type, like)
203 tree type, like;
205 return c_build_qualified_type (type,
206 TYPE_QUALS (type) | TYPE_QUALS (like));
209 /* Return the common type of two types.
210 We assume that comptypes has already been done and returned 1;
211 if that isn't so, this may crash. In particular, we assume that qualifiers
212 match.
214 This is the type for the result of most arithmetic operations
215 if the operands have the given two types. */
217 tree
218 common_type (t1, t2)
219 tree t1, t2;
221 enum tree_code code1;
222 enum tree_code code2;
223 tree attributes;
225 /* Save time if the two types are the same. */
227 if (t1 == t2) return t1;
229 /* If one type is nonsense, use the other. */
230 if (t1 == error_mark_node)
231 return t2;
232 if (t2 == error_mark_node)
233 return t1;
235 /* Merge the attributes. */
236 attributes = (*targetm.merge_type_attributes) (t1, t2);
238 /* Treat an enum type as the unsigned integer type of the same width. */
240 if (TREE_CODE (t1) == ENUMERAL_TYPE)
241 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
242 if (TREE_CODE (t2) == ENUMERAL_TYPE)
243 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
245 code1 = TREE_CODE (t1);
246 code2 = TREE_CODE (t2);
248 /* If one type is complex, form the common type of the non-complex
249 components, then make that complex. Use T1 or T2 if it is the
250 required type. */
251 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
253 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
254 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
255 tree subtype = common_type (subtype1, subtype2);
257 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
258 return build_type_attribute_variant (t1, attributes);
259 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
260 return build_type_attribute_variant (t2, attributes);
261 else
262 return build_type_attribute_variant (build_complex_type (subtype),
263 attributes);
266 switch (code1)
268 case INTEGER_TYPE:
269 case REAL_TYPE:
270 /* If only one is real, use it as the result. */
272 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
273 return build_type_attribute_variant (t1, attributes);
275 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
276 return build_type_attribute_variant (t2, attributes);
278 /* Both real or both integers; use the one with greater precision. */
280 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
281 return build_type_attribute_variant (t1, attributes);
282 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
283 return build_type_attribute_variant (t2, attributes);
285 /* Same precision. Prefer longs to ints even when same size. */
287 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
288 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
289 return build_type_attribute_variant (long_unsigned_type_node,
290 attributes);
292 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
293 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
295 /* But preserve unsignedness from the other type,
296 since long cannot hold all the values of an unsigned int. */
297 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
298 t1 = long_unsigned_type_node;
299 else
300 t1 = long_integer_type_node;
301 return build_type_attribute_variant (t1, attributes);
304 /* Likewise, prefer long double to double even if same size. */
305 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
306 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
307 return build_type_attribute_variant (long_double_type_node,
308 attributes);
310 /* Otherwise prefer the unsigned one. */
312 if (TREE_UNSIGNED (t1))
313 return build_type_attribute_variant (t1, attributes);
314 else
315 return build_type_attribute_variant (t2, attributes);
317 case POINTER_TYPE:
318 /* For two pointers, do this recursively on the target type,
319 and combine the qualifiers of the two types' targets. */
320 /* This code was turned off; I don't know why.
321 But ANSI C specifies doing this with the qualifiers.
322 So I turned it on again. */
324 tree pointed_to_1 = TREE_TYPE (t1);
325 tree pointed_to_2 = TREE_TYPE (t2);
326 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
327 TYPE_MAIN_VARIANT (pointed_to_2));
328 t1 = build_pointer_type (c_build_qualified_type
329 (target,
330 TYPE_QUALS (pointed_to_1) |
331 TYPE_QUALS (pointed_to_2)));
332 return build_type_attribute_variant (t1, attributes);
334 #if 0
335 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
336 return build_type_attribute_variant (t1, attributes);
337 #endif
339 case ARRAY_TYPE:
341 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
342 /* Save space: see if the result is identical to one of the args. */
343 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
344 return build_type_attribute_variant (t1, attributes);
345 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
346 return build_type_attribute_variant (t2, attributes);
347 /* Merge the element types, and have a size if either arg has one. */
348 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
349 return build_type_attribute_variant (t1, attributes);
352 case FUNCTION_TYPE:
353 /* Function types: prefer the one that specified arg types.
354 If both do, merge the arg types. Also merge the return types. */
356 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
357 tree p1 = TYPE_ARG_TYPES (t1);
358 tree p2 = TYPE_ARG_TYPES (t2);
359 int len;
360 tree newargs, n;
361 int i;
363 /* Save space: see if the result is identical to one of the args. */
364 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
365 return build_type_attribute_variant (t1, attributes);
366 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
367 return build_type_attribute_variant (t2, attributes);
369 /* Simple way if one arg fails to specify argument types. */
370 if (TYPE_ARG_TYPES (t1) == 0)
372 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
373 return build_type_attribute_variant (t1, attributes);
375 if (TYPE_ARG_TYPES (t2) == 0)
377 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
378 return build_type_attribute_variant (t1, attributes);
381 /* If both args specify argument types, we must merge the two
382 lists, argument by argument. */
384 pushlevel (0);
385 declare_parm_level (1);
387 len = list_length (p1);
388 newargs = 0;
390 for (i = 0; i < len; i++)
391 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
393 n = newargs;
395 for (; p1;
396 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
398 /* A null type means arg type is not specified.
399 Take whatever the other function type has. */
400 if (TREE_VALUE (p1) == 0)
402 TREE_VALUE (n) = TREE_VALUE (p2);
403 goto parm_done;
405 if (TREE_VALUE (p2) == 0)
407 TREE_VALUE (n) = TREE_VALUE (p1);
408 goto parm_done;
411 /* Given wait (union {union wait *u; int *i} *)
412 and wait (union wait *),
413 prefer union wait * as type of parm. */
414 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
415 && TREE_VALUE (p1) != TREE_VALUE (p2))
417 tree memb;
418 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
419 memb; memb = TREE_CHAIN (memb))
420 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
422 TREE_VALUE (n) = TREE_VALUE (p2);
423 if (pedantic)
424 pedwarn ("function types not truly compatible in ISO C");
425 goto parm_done;
428 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
429 && TREE_VALUE (p2) != TREE_VALUE (p1))
431 tree memb;
432 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
433 memb; memb = TREE_CHAIN (memb))
434 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
436 TREE_VALUE (n) = TREE_VALUE (p1);
437 if (pedantic)
438 pedwarn ("function types not truly compatible in ISO C");
439 goto parm_done;
442 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
443 parm_done: ;
446 poplevel (0, 0, 0);
448 t1 = build_function_type (valtype, newargs);
449 /* ... falls through ... */
452 default:
453 return build_type_attribute_variant (t1, attributes);
458 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
459 or various other operations. Return 2 if they are compatible
460 but a warning may be needed if you use them together. */
463 comptypes (type1, type2)
464 tree type1, type2;
466 tree t1 = type1;
467 tree t2 = type2;
468 int attrval, val;
470 /* Suppress errors caused by previously reported errors. */
472 if (t1 == t2 || !t1 || !t2
473 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
474 return 1;
476 /* If either type is the internal version of sizetype, return the
477 language version. */
478 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
479 && TYPE_DOMAIN (t1) != 0)
480 t1 = TYPE_DOMAIN (t1);
482 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
483 && TYPE_DOMAIN (t2) != 0)
484 t2 = TYPE_DOMAIN (t2);
486 /* Treat an enum type as the integer type of the same width and
487 signedness. */
489 if (TREE_CODE (t1) == ENUMERAL_TYPE)
490 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
491 if (TREE_CODE (t2) == ENUMERAL_TYPE)
492 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
494 if (t1 == t2)
495 return 1;
497 /* Different classes of types can't be compatible. */
499 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
501 /* Qualifiers must match. */
503 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
504 return 0;
506 /* Allow for two different type nodes which have essentially the same
507 definition. Note that we already checked for equality of the type
508 qualifiers (just above). */
510 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
511 return 1;
513 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
514 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
515 return 0;
517 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
518 val = 0;
520 switch (TREE_CODE (t1))
522 case POINTER_TYPE:
523 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
524 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
525 break;
527 case FUNCTION_TYPE:
528 val = function_types_compatible_p (t1, t2);
529 break;
531 case ARRAY_TYPE:
533 tree d1 = TYPE_DOMAIN (t1);
534 tree d2 = TYPE_DOMAIN (t2);
535 bool d1_variable, d2_variable;
536 bool d1_zero, d2_zero;
537 val = 1;
539 /* Target types must match incl. qualifiers. */
540 if (TREE_TYPE (t1) != TREE_TYPE (t2)
541 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
542 return 0;
544 /* Sizes must match unless one is missing or variable. */
545 if (d1 == 0 || d2 == 0 || d1 == d2)
546 break;
548 d1_zero = ! TYPE_MAX_VALUE (d1);
549 d2_zero = ! TYPE_MAX_VALUE (d2);
551 d1_variable = (! d1_zero
552 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
553 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
554 d2_variable = (! d2_zero
555 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
556 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
558 if (d1_variable || d2_variable)
559 break;
560 if (d1_zero && d2_zero)
561 break;
562 if (d1_zero || d2_zero
563 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
564 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
565 val = 0;
567 break;
570 case RECORD_TYPE:
571 if (maybe_objc_comptypes (t1, t2, 0) == 1)
572 val = 1;
573 break;
575 default:
576 break;
578 return attrval == 2 && val == 1 ? 2 : val;
581 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
582 ignoring their qualifiers. */
584 static int
585 comp_target_types (ttl, ttr)
586 tree ttl, ttr;
588 int val;
590 /* Give maybe_objc_comptypes a crack at letting these types through. */
591 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
592 return val;
594 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
595 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
597 if (val == 2 && pedantic)
598 pedwarn ("types are not quite compatible");
599 return val;
602 /* Subroutines of `comptypes'. */
604 /* Return 1 if two function types F1 and F2 are compatible.
605 If either type specifies no argument types,
606 the other must specify a fixed number of self-promoting arg types.
607 Otherwise, if one type specifies only the number of arguments,
608 the other must specify that number of self-promoting arg types.
609 Otherwise, the argument types must match. */
611 static int
612 function_types_compatible_p (f1, f2)
613 tree f1, f2;
615 tree args1, args2;
616 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
617 int val = 1;
618 int val1;
620 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
621 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
622 return 0;
624 args1 = TYPE_ARG_TYPES (f1);
625 args2 = TYPE_ARG_TYPES (f2);
627 /* An unspecified parmlist matches any specified parmlist
628 whose argument types don't need default promotions. */
630 if (args1 == 0)
632 if (!self_promoting_args_p (args2))
633 return 0;
634 /* If one of these types comes from a non-prototype fn definition,
635 compare that with the other type's arglist.
636 If they don't match, ask for a warning (but no error). */
637 if (TYPE_ACTUAL_ARG_TYPES (f1)
638 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
639 val = 2;
640 return val;
642 if (args2 == 0)
644 if (!self_promoting_args_p (args1))
645 return 0;
646 if (TYPE_ACTUAL_ARG_TYPES (f2)
647 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
648 val = 2;
649 return val;
652 /* Both types have argument lists: compare them and propagate results. */
653 val1 = type_lists_compatible_p (args1, args2);
654 return val1 != 1 ? val1 : val;
657 /* Check two lists of types for compatibility,
658 returning 0 for incompatible, 1 for compatible,
659 or 2 for compatible with warning. */
661 static int
662 type_lists_compatible_p (args1, args2)
663 tree args1, args2;
665 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
666 int val = 1;
667 int newval = 0;
669 while (1)
671 if (args1 == 0 && args2 == 0)
672 return val;
673 /* If one list is shorter than the other,
674 they fail to match. */
675 if (args1 == 0 || args2 == 0)
676 return 0;
677 /* A null pointer instead of a type
678 means there is supposed to be an argument
679 but nothing is specified about what type it has.
680 So match anything that self-promotes. */
681 if (TREE_VALUE (args1) == 0)
683 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
684 return 0;
686 else if (TREE_VALUE (args2) == 0)
688 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
689 return 0;
691 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
692 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
694 /* Allow wait (union {union wait *u; int *i} *)
695 and wait (union wait *) to be compatible. */
696 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
697 && (TYPE_NAME (TREE_VALUE (args1)) == 0
698 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
699 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
700 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
701 TYPE_SIZE (TREE_VALUE (args2))))
703 tree memb;
704 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
705 memb; memb = TREE_CHAIN (memb))
706 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
707 break;
708 if (memb == 0)
709 return 0;
711 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
712 && (TYPE_NAME (TREE_VALUE (args2)) == 0
713 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
714 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
715 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
716 TYPE_SIZE (TREE_VALUE (args1))))
718 tree memb;
719 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
720 memb; memb = TREE_CHAIN (memb))
721 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
722 break;
723 if (memb == 0)
724 return 0;
726 else
727 return 0;
730 /* comptypes said ok, but record if it said to warn. */
731 if (newval > val)
732 val = newval;
734 args1 = TREE_CHAIN (args1);
735 args2 = TREE_CHAIN (args2);
739 tree
740 c_sizeof_nowarn (type)
741 tree type;
743 enum tree_code code = TREE_CODE (type);
744 tree size;
746 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
747 size = size_one_node;
748 else if (!COMPLETE_TYPE_P (type))
749 size = size_zero_node;
750 else
751 /* Convert in case a char is more than one unit. */
752 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
753 size_int (TYPE_PRECISION (char_type_node)
754 / BITS_PER_UNIT));
756 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
757 TYPE_IS_SIZETYPE means that certain things (like overflow) will
758 never happen. However, this node should really have type
759 `size_t', which is just a typedef for an ordinary integer type. */
760 return fold (build1 (NOP_EXPR, c_size_type_node, size));
763 /* Compute the size to increment a pointer by. */
765 tree
766 c_size_in_bytes (type)
767 tree type;
769 enum tree_code code = TREE_CODE (type);
771 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
772 return size_one_node;
774 if (!COMPLETE_OR_VOID_TYPE_P (type))
776 error ("arithmetic on pointer to an incomplete type");
777 return size_one_node;
780 /* Convert in case a char is more than one unit. */
781 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
782 size_int (TYPE_PRECISION (char_type_node)
783 / BITS_PER_UNIT));
786 /* Return either DECL or its known constant value (if it has one). */
788 tree
789 decl_constant_value (decl)
790 tree decl;
792 if (/* Don't change a variable array bound or initial value to a constant
793 in a place where a variable is invalid. */
794 current_function_decl != 0
795 && ! TREE_THIS_VOLATILE (decl)
796 && TREE_READONLY (decl)
797 && DECL_INITIAL (decl) != 0
798 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
799 /* This is invalid if initial value is not constant.
800 If it has either a function call, a memory reference,
801 or a variable, then re-evaluating it could give different results. */
802 && TREE_CONSTANT (DECL_INITIAL (decl))
803 /* Check for cases where this is sub-optimal, even though valid. */
804 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
805 return DECL_INITIAL (decl);
806 return decl;
809 /* Return either DECL or its known constant value (if it has one), but
810 return DECL if pedantic or DECL has mode BLKmode. This is for
811 bug-compatibility with the old behavior of decl_constant_value
812 (before GCC 3.0); every use of this function is a bug and it should
813 be removed before GCC 3.1. It is not appropriate to use pedantic
814 in a way that affects optimization, and BLKmode is probably not the
815 right test for avoiding misoptimizations either. */
817 static tree
818 decl_constant_value_for_broken_optimization (decl)
819 tree decl;
821 if (pedantic || DECL_MODE (decl) == BLKmode)
822 return decl;
823 else
824 return decl_constant_value (decl);
828 /* Perform the default conversion of arrays and functions to pointers.
829 Return the result of converting EXP. For any other expression, just
830 return EXP. */
832 static tree
833 default_function_array_conversion (exp)
834 tree exp;
836 tree orig_exp;
837 tree type = TREE_TYPE (exp);
838 enum tree_code code = TREE_CODE (type);
839 int not_lvalue = 0;
841 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
842 an lvalue.
844 Do not use STRIP_NOPS here! It will remove conversions from pointer
845 to integer and cause infinite recursion. */
846 orig_exp = exp;
847 while (TREE_CODE (exp) == NON_LVALUE_EXPR
848 || (TREE_CODE (exp) == NOP_EXPR
849 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
851 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
852 not_lvalue = 1;
853 exp = TREE_OPERAND (exp, 0);
856 /* Preserve the original expression code. */
857 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
858 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
860 if (code == FUNCTION_TYPE)
862 return build_unary_op (ADDR_EXPR, exp, 0);
864 if (code == ARRAY_TYPE)
866 tree adr;
867 tree restype = TREE_TYPE (type);
868 tree ptrtype;
869 int constp = 0;
870 int volatilep = 0;
871 int lvalue_array_p;
873 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
875 constp = TREE_READONLY (exp);
876 volatilep = TREE_THIS_VOLATILE (exp);
879 if (TYPE_QUALS (type) || constp || volatilep)
880 restype
881 = c_build_qualified_type (restype,
882 TYPE_QUALS (type)
883 | (constp * TYPE_QUAL_CONST)
884 | (volatilep * TYPE_QUAL_VOLATILE));
886 if (TREE_CODE (exp) == INDIRECT_REF)
887 return convert (TYPE_POINTER_TO (restype),
888 TREE_OPERAND (exp, 0));
890 if (TREE_CODE (exp) == COMPOUND_EXPR)
892 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
893 return build (COMPOUND_EXPR, TREE_TYPE (op1),
894 TREE_OPERAND (exp, 0), op1);
897 lvalue_array_p = !not_lvalue && lvalue_p (exp);
898 if (!flag_isoc99 && !lvalue_array_p)
900 /* Before C99, non-lvalue arrays do not decay to pointers.
901 Normally, using such an array would be invalid; but it can
902 be used correctly inside sizeof or as a statement expression.
903 Thus, do not give an error here; an error will result later. */
904 return exp;
907 ptrtype = build_pointer_type (restype);
909 if (TREE_CODE (exp) == VAR_DECL)
911 /* ??? This is not really quite correct
912 in that the type of the operand of ADDR_EXPR
913 is not the target type of the type of the ADDR_EXPR itself.
914 Question is, can this lossage be avoided? */
915 adr = build1 (ADDR_EXPR, ptrtype, exp);
916 if (!c_mark_addressable (exp))
917 return error_mark_node;
918 TREE_CONSTANT (adr) = staticp (exp);
919 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
920 return adr;
922 /* This way is better for a COMPONENT_REF since it can
923 simplify the offset for a component. */
924 adr = build_unary_op (ADDR_EXPR, exp, 1);
925 return convert (ptrtype, adr);
927 return exp;
930 /* Perform default promotions for C data used in expressions.
931 Arrays and functions are converted to pointers;
932 enumeral types or short or char, to int.
933 In addition, manifest constants symbols are replaced by their values. */
935 tree
936 default_conversion (exp)
937 tree exp;
939 tree orig_exp;
940 tree type = TREE_TYPE (exp);
941 enum tree_code code = TREE_CODE (type);
943 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
944 return default_function_array_conversion (exp);
946 /* Constants can be used directly unless they're not loadable. */
947 if (TREE_CODE (exp) == CONST_DECL)
948 exp = DECL_INITIAL (exp);
950 /* Replace a nonvolatile const static variable with its value unless
951 it is an array, in which case we must be sure that taking the
952 address of the array produces consistent results. */
953 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
955 exp = decl_constant_value_for_broken_optimization (exp);
956 type = TREE_TYPE (exp);
959 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
960 an lvalue.
962 Do not use STRIP_NOPS here! It will remove conversions from pointer
963 to integer and cause infinite recursion. */
964 orig_exp = exp;
965 while (TREE_CODE (exp) == NON_LVALUE_EXPR
966 || (TREE_CODE (exp) == NOP_EXPR
967 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
968 exp = TREE_OPERAND (exp, 0);
970 /* Preserve the original expression code. */
971 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
972 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
974 /* Normally convert enums to int,
975 but convert wide enums to something wider. */
976 if (code == ENUMERAL_TYPE)
978 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
979 TYPE_PRECISION (integer_type_node)),
980 ((TYPE_PRECISION (type)
981 >= TYPE_PRECISION (integer_type_node))
982 && TREE_UNSIGNED (type)));
984 return convert (type, exp);
987 if (TREE_CODE (exp) == COMPONENT_REF
988 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
989 /* If it's thinner than an int, promote it like a
990 c_promoting_integer_type_p, otherwise leave it alone. */
991 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
992 TYPE_PRECISION (integer_type_node)))
993 return convert (integer_type_node, exp);
995 if (c_promoting_integer_type_p (type))
997 /* Preserve unsignedness if not really getting any wider. */
998 if (TREE_UNSIGNED (type)
999 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1000 return convert (unsigned_type_node, exp);
1002 return convert (integer_type_node, exp);
1005 if (code == VOID_TYPE)
1007 error ("void value not ignored as it ought to be");
1008 return error_mark_node;
1010 return exp;
1013 /* Look up COMPONENT in a structure or union DECL.
1015 If the component name is not found, returns NULL_TREE. Otherwise,
1016 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1017 stepping down the chain to the component, which is in the last
1018 TREE_VALUE of the list. Normally the list is of length one, but if
1019 the component is embedded within (nested) anonymous structures or
1020 unions, the list steps down the chain to the component. */
1022 static tree
1023 lookup_field (decl, component)
1024 tree decl, component;
1026 tree type = TREE_TYPE (decl);
1027 tree field;
1029 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1030 to the field elements. Use a binary search on this array to quickly
1031 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1032 will always be set for structures which have many elements. */
1034 if (TYPE_LANG_SPECIFIC (type))
1036 int bot, top, half;
1037 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1039 field = TYPE_FIELDS (type);
1040 bot = 0;
1041 top = TYPE_LANG_SPECIFIC (type)->len;
1042 while (top - bot > 1)
1044 half = (top - bot + 1) >> 1;
1045 field = field_array[bot+half];
1047 if (DECL_NAME (field) == NULL_TREE)
1049 /* Step through all anon unions in linear fashion. */
1050 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1052 field = field_array[bot++];
1053 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1054 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1056 tree anon = lookup_field (field, component);
1058 if (anon)
1059 return tree_cons (NULL_TREE, field, anon);
1063 /* Entire record is only anon unions. */
1064 if (bot > top)
1065 return NULL_TREE;
1067 /* Restart the binary search, with new lower bound. */
1068 continue;
1071 if (DECL_NAME (field) == component)
1072 break;
1073 if (DECL_NAME (field) < component)
1074 bot += half;
1075 else
1076 top = bot + half;
1079 if (DECL_NAME (field_array[bot]) == component)
1080 field = field_array[bot];
1081 else if (DECL_NAME (field) != component)
1082 return NULL_TREE;
1084 else
1086 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1088 if (DECL_NAME (field) == NULL_TREE
1089 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1090 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1092 tree anon = lookup_field (field, component);
1094 if (anon)
1095 return tree_cons (NULL_TREE, field, anon);
1098 if (DECL_NAME (field) == component)
1099 break;
1102 if (field == NULL_TREE)
1103 return NULL_TREE;
1106 return tree_cons (NULL_TREE, field, NULL_TREE);
1109 /* Make an expression to refer to the COMPONENT field of
1110 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1112 tree
1113 build_component_ref (datum, component)
1114 tree datum, component;
1116 tree type = TREE_TYPE (datum);
1117 enum tree_code code = TREE_CODE (type);
1118 tree field = NULL;
1119 tree ref;
1121 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1122 If pedantic ensure that the arguments are not lvalues; otherwise,
1123 if the component is an array, it would wrongly decay to a pointer in
1124 C89 mode.
1125 We cannot do this with a COND_EXPR, because in a conditional expression
1126 the default promotions are applied to both sides, and this would yield
1127 the wrong type of the result; for example, if the components have
1128 type "char". */
1129 switch (TREE_CODE (datum))
1131 case COMPOUND_EXPR:
1133 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1134 return build (COMPOUND_EXPR, TREE_TYPE (value),
1135 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1137 default:
1138 break;
1141 /* See if there is a field or component with name COMPONENT. */
1143 if (code == RECORD_TYPE || code == UNION_TYPE)
1145 if (!COMPLETE_TYPE_P (type))
1147 c_incomplete_type_error (NULL_TREE, type);
1148 return error_mark_node;
1151 field = lookup_field (datum, component);
1153 if (!field)
1155 error ("%s has no member named `%s'",
1156 code == RECORD_TYPE ? "structure" : "union",
1157 IDENTIFIER_POINTER (component));
1158 return error_mark_node;
1161 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1162 This might be better solved in future the way the C++ front
1163 end does it - by giving the anonymous entities each a
1164 separate name and type, and then have build_component_ref
1165 recursively call itself. We can't do that here. */
1166 for (; field; field = TREE_CHAIN (field))
1168 tree subdatum = TREE_VALUE (field);
1170 if (TREE_TYPE (subdatum) == error_mark_node)
1171 return error_mark_node;
1173 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1174 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1175 TREE_READONLY (ref) = 1;
1176 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1177 TREE_THIS_VOLATILE (ref) = 1;
1179 if (TREE_DEPRECATED (subdatum))
1180 warn_deprecated_use (subdatum);
1182 datum = ref;
1185 return ref;
1187 else if (code != ERROR_MARK)
1188 error ("request for member `%s' in something not a structure or union",
1189 IDENTIFIER_POINTER (component));
1191 return error_mark_node;
1194 /* Given an expression PTR for a pointer, return an expression
1195 for the value pointed to.
1196 ERRORSTRING is the name of the operator to appear in error messages. */
1198 tree
1199 build_indirect_ref (ptr, errorstring)
1200 tree ptr;
1201 const char *errorstring;
1203 tree pointer = default_conversion (ptr);
1204 tree type = TREE_TYPE (pointer);
1206 if (TREE_CODE (type) == POINTER_TYPE)
1208 if (TREE_CODE (pointer) == ADDR_EXPR
1209 && !flag_volatile
1210 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1211 == TREE_TYPE (type)))
1212 return TREE_OPERAND (pointer, 0);
1213 else
1215 tree t = TREE_TYPE (type);
1216 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1218 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1220 error ("dereferencing pointer to incomplete type");
1221 return error_mark_node;
1223 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1224 warning ("dereferencing `void *' pointer");
1226 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1227 so that we get the proper error message if the result is used
1228 to assign to. Also, &* is supposed to be a no-op.
1229 And ANSI C seems to specify that the type of the result
1230 should be the const type. */
1231 /* A de-reference of a pointer to const is not a const. It is valid
1232 to change it via some other pointer. */
1233 TREE_READONLY (ref) = TYPE_READONLY (t);
1234 TREE_SIDE_EFFECTS (ref)
1235 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1236 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1237 return ref;
1240 else if (TREE_CODE (pointer) != ERROR_MARK)
1241 error ("invalid type argument of `%s'", errorstring);
1242 return error_mark_node;
1245 /* This handles expressions of the form "a[i]", which denotes
1246 an array reference.
1248 This is logically equivalent in C to *(a+i), but we may do it differently.
1249 If A is a variable or a member, we generate a primitive ARRAY_REF.
1250 This avoids forcing the array out of registers, and can work on
1251 arrays that are not lvalues (for example, members of structures returned
1252 by functions). */
1254 tree
1255 build_array_ref (array, index)
1256 tree array, index;
1258 if (index == 0)
1260 error ("subscript missing in array reference");
1261 return error_mark_node;
1264 if (TREE_TYPE (array) == error_mark_node
1265 || TREE_TYPE (index) == error_mark_node)
1266 return error_mark_node;
1268 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1269 && TREE_CODE (array) != INDIRECT_REF)
1271 tree rval, type;
1273 /* Subscripting with type char is likely to lose
1274 on a machine where chars are signed.
1275 So warn on any machine, but optionally.
1276 Don't warn for unsigned char since that type is safe.
1277 Don't warn for signed char because anyone who uses that
1278 must have done so deliberately. */
1279 if (warn_char_subscripts
1280 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1281 warning ("array subscript has type `char'");
1283 /* Apply default promotions *after* noticing character types. */
1284 index = default_conversion (index);
1286 /* Require integer *after* promotion, for sake of enums. */
1287 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1289 error ("array subscript is not an integer");
1290 return error_mark_node;
1293 /* An array that is indexed by a non-constant
1294 cannot be stored in a register; we must be able to do
1295 address arithmetic on its address.
1296 Likewise an array of elements of variable size. */
1297 if (TREE_CODE (index) != INTEGER_CST
1298 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1299 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1301 if (!c_mark_addressable (array))
1302 return error_mark_node;
1304 /* An array that is indexed by a constant value which is not within
1305 the array bounds cannot be stored in a register either; because we
1306 would get a crash in store_bit_field/extract_bit_field when trying
1307 to access a non-existent part of the register. */
1308 if (TREE_CODE (index) == INTEGER_CST
1309 && TYPE_VALUES (TREE_TYPE (array))
1310 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1312 if (!c_mark_addressable (array))
1313 return error_mark_node;
1316 if (pedantic)
1318 tree foo = array;
1319 while (TREE_CODE (foo) == COMPONENT_REF)
1320 foo = TREE_OPERAND (foo, 0);
1321 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1322 pedwarn ("ISO C forbids subscripting `register' array");
1323 else if (! flag_isoc99 && ! lvalue_p (foo))
1324 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1327 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1328 rval = build (ARRAY_REF, type, array, index);
1329 /* Array ref is const/volatile if the array elements are
1330 or if the array is. */
1331 TREE_READONLY (rval)
1332 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1333 | TREE_READONLY (array));
1334 TREE_SIDE_EFFECTS (rval)
1335 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1336 | TREE_SIDE_EFFECTS (array));
1337 TREE_THIS_VOLATILE (rval)
1338 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1339 /* This was added by rms on 16 Nov 91.
1340 It fixes vol struct foo *a; a->elts[1]
1341 in an inline function.
1342 Hope it doesn't break something else. */
1343 | TREE_THIS_VOLATILE (array));
1344 return require_complete_type (fold (rval));
1348 tree ar = default_conversion (array);
1349 tree ind = default_conversion (index);
1351 /* Do the same warning check as above, but only on the part that's
1352 syntactically the index and only if it is also semantically
1353 the index. */
1354 if (warn_char_subscripts
1355 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1356 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1357 warning ("subscript has type `char'");
1359 /* Put the integer in IND to simplify error checking. */
1360 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1362 tree temp = ar;
1363 ar = ind;
1364 ind = temp;
1367 if (ar == error_mark_node)
1368 return ar;
1370 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1371 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1373 error ("subscripted value is neither array nor pointer");
1374 return error_mark_node;
1376 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1378 error ("array subscript is not an integer");
1379 return error_mark_node;
1382 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1383 "array indexing");
1387 /* Build an external reference to identifier ID. FUN indicates
1388 whether this will be used for a function call. */
1389 tree
1390 build_external_ref (id, fun)
1391 tree id;
1392 int fun;
1394 tree ref;
1395 tree decl = lookup_name (id);
1396 tree objc_ivar = lookup_objc_ivar (id);
1398 if (decl && TREE_DEPRECATED (decl))
1399 warn_deprecated_use (decl);
1401 if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
1403 if (objc_ivar)
1404 ref = objc_ivar;
1405 else if (fun)
1407 if (!decl || decl == error_mark_node)
1408 /* Ordinary implicit function declaration. */
1409 ref = implicitly_declare (id);
1410 else
1412 /* Implicit declaration of built-in function. Don't
1413 change the built-in declaration, but don't let this
1414 go by silently, either. */
1415 implicit_decl_warning (id);
1417 /* only issue this warning once */
1418 C_DECL_ANTICIPATED (decl) = 0;
1419 ref = decl;
1422 else
1424 /* Reference to undeclared variable, including reference to
1425 builtin outside of function-call context. */
1426 if (current_function_decl == 0)
1427 error ("`%s' undeclared here (not in a function)",
1428 IDENTIFIER_POINTER (id));
1429 else
1431 if (IDENTIFIER_GLOBAL_VALUE (id) != error_mark_node
1432 || IDENTIFIER_ERROR_LOCUS (id) != current_function_decl)
1434 error ("`%s' undeclared (first use in this function)",
1435 IDENTIFIER_POINTER (id));
1437 if (! undeclared_variable_notice)
1439 error ("(Each undeclared identifier is reported only once");
1440 error ("for each function it appears in.)");
1441 undeclared_variable_notice = 1;
1444 IDENTIFIER_GLOBAL_VALUE (id) = error_mark_node;
1445 IDENTIFIER_ERROR_LOCUS (id) = current_function_decl;
1447 return error_mark_node;
1450 else
1452 /* Properly declared variable or function reference. */
1453 if (!objc_ivar)
1454 ref = decl;
1455 else if (decl != objc_ivar && IDENTIFIER_LOCAL_VALUE (id))
1457 warning ("local declaration of `%s' hides instance variable",
1458 IDENTIFIER_POINTER (id));
1459 ref = decl;
1461 else
1462 ref = objc_ivar;
1465 if (TREE_TYPE (ref) == error_mark_node)
1466 return error_mark_node;
1468 assemble_external (ref);
1469 TREE_USED (ref) = 1;
1471 if (TREE_CODE (ref) == CONST_DECL)
1473 ref = DECL_INITIAL (ref);
1474 TREE_CONSTANT (ref) = 1;
1477 return ref;
1480 /* Build a function call to function FUNCTION with parameters PARAMS.
1481 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1482 TREE_VALUE of each node is a parameter-expression.
1483 FUNCTION's data type may be a function type or a pointer-to-function. */
1485 tree
1486 build_function_call (function, params)
1487 tree function, params;
1489 tree fntype, fundecl = 0;
1490 tree coerced_params;
1491 tree name = NULL_TREE, assembler_name = NULL_TREE, result;
1493 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1494 STRIP_TYPE_NOPS (function);
1496 /* Convert anything with function type to a pointer-to-function. */
1497 if (TREE_CODE (function) == FUNCTION_DECL)
1499 name = DECL_NAME (function);
1500 assembler_name = DECL_ASSEMBLER_NAME (function);
1502 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1503 (because calling an inline function does not mean the function
1504 needs to be separately compiled). */
1505 fntype = build_type_variant (TREE_TYPE (function),
1506 TREE_READONLY (function),
1507 TREE_THIS_VOLATILE (function));
1508 fundecl = function;
1509 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1511 else
1512 function = default_conversion (function);
1514 fntype = TREE_TYPE (function);
1516 if (TREE_CODE (fntype) == ERROR_MARK)
1517 return error_mark_node;
1519 if (!(TREE_CODE (fntype) == POINTER_TYPE
1520 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1522 error ("called object is not a function");
1523 return error_mark_node;
1526 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1527 current_function_returns_abnormally = 1;
1529 /* fntype now gets the type of function pointed to. */
1530 fntype = TREE_TYPE (fntype);
1532 /* Convert the parameters to the types declared in the
1533 function prototype, or apply default promotions. */
1535 coerced_params
1536 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1538 /* Check that the arguments to the function are valid. */
1540 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1542 /* Recognize certain built-in functions so we can make tree-codes
1543 other than CALL_EXPR. We do this when it enables fold-const.c
1544 to do something useful. */
1546 if (TREE_CODE (function) == ADDR_EXPR
1547 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1548 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1550 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1551 params, coerced_params);
1552 if (result)
1553 return result;
1556 result = build (CALL_EXPR, TREE_TYPE (fntype),
1557 function, coerced_params, NULL_TREE);
1558 TREE_SIDE_EFFECTS (result) = 1;
1559 result = fold (result);
1561 if (VOID_TYPE_P (TREE_TYPE (result)))
1562 return result;
1563 return require_complete_type (result);
1566 /* Convert the argument expressions in the list VALUES
1567 to the types in the list TYPELIST. The result is a list of converted
1568 argument expressions.
1570 If TYPELIST is exhausted, or when an element has NULL as its type,
1571 perform the default conversions.
1573 PARMLIST is the chain of parm decls for the function being called.
1574 It may be 0, if that info is not available.
1575 It is used only for generating error messages.
1577 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1579 This is also where warnings about wrong number of args are generated.
1581 Both VALUES and the returned value are chains of TREE_LIST nodes
1582 with the elements of the list in the TREE_VALUE slots of those nodes. */
1584 static tree
1585 convert_arguments (typelist, values, name, fundecl)
1586 tree typelist, values, name, fundecl;
1588 tree typetail, valtail;
1589 tree result = NULL;
1590 int parmnum;
1592 /* Scan the given expressions and types, producing individual
1593 converted arguments and pushing them on RESULT in reverse order. */
1595 for (valtail = values, typetail = typelist, parmnum = 0;
1596 valtail;
1597 valtail = TREE_CHAIN (valtail), parmnum++)
1599 tree type = typetail ? TREE_VALUE (typetail) : 0;
1600 tree val = TREE_VALUE (valtail);
1602 if (type == void_type_node)
1604 if (name)
1605 error ("too many arguments to function `%s'",
1606 IDENTIFIER_POINTER (name));
1607 else
1608 error ("too many arguments to function");
1609 break;
1612 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1613 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1614 to convert automatically to a pointer. */
1615 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1616 val = TREE_OPERAND (val, 0);
1618 val = default_function_array_conversion (val);
1620 val = require_complete_type (val);
1622 if (type != 0)
1624 /* Formal parm type is specified by a function prototype. */
1625 tree parmval;
1627 if (!COMPLETE_TYPE_P (type))
1629 error ("type of formal parameter %d is incomplete", parmnum + 1);
1630 parmval = val;
1632 else
1634 /* Optionally warn about conversions that
1635 differ from the default conversions. */
1636 if (warn_conversion || warn_traditional)
1638 int formal_prec = TYPE_PRECISION (type);
1640 if (INTEGRAL_TYPE_P (type)
1641 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1642 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1643 if (INTEGRAL_TYPE_P (type)
1644 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1645 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1646 else if (TREE_CODE (type) == COMPLEX_TYPE
1647 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1648 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1649 else if (TREE_CODE (type) == REAL_TYPE
1650 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1651 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1652 else if (TREE_CODE (type) == COMPLEX_TYPE
1653 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1654 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1655 else if (TREE_CODE (type) == REAL_TYPE
1656 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1657 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1658 /* ??? At some point, messages should be written about
1659 conversions between complex types, but that's too messy
1660 to do now. */
1661 else if (TREE_CODE (type) == REAL_TYPE
1662 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1664 /* Warn if any argument is passed as `float',
1665 since without a prototype it would be `double'. */
1666 if (formal_prec == TYPE_PRECISION (float_type_node))
1667 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1669 /* Detect integer changing in width or signedness.
1670 These warnings are only activated with
1671 -Wconversion, not with -Wtraditional. */
1672 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1673 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1675 tree would_have_been = default_conversion (val);
1676 tree type1 = TREE_TYPE (would_have_been);
1678 if (TREE_CODE (type) == ENUMERAL_TYPE
1679 && (TYPE_MAIN_VARIANT (type)
1680 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1681 /* No warning if function asks for enum
1682 and the actual arg is that enum type. */
1684 else if (formal_prec != TYPE_PRECISION (type1))
1685 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1686 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1688 /* Don't complain if the formal parameter type
1689 is an enum, because we can't tell now whether
1690 the value was an enum--even the same enum. */
1691 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1693 else if (TREE_CODE (val) == INTEGER_CST
1694 && int_fits_type_p (val, type))
1695 /* Change in signedness doesn't matter
1696 if a constant value is unaffected. */
1698 /* Likewise for a constant in a NOP_EXPR. */
1699 else if (TREE_CODE (val) == NOP_EXPR
1700 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1701 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1703 #if 0 /* We never get such tree structure here. */
1704 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1705 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1706 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1707 /* Change in signedness doesn't matter
1708 if an enum value is unaffected. */
1710 #endif
1711 /* If the value is extended from a narrower
1712 unsigned type, it doesn't matter whether we
1713 pass it as signed or unsigned; the value
1714 certainly is the same either way. */
1715 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1716 && TREE_UNSIGNED (TREE_TYPE (val)))
1718 else if (TREE_UNSIGNED (type))
1719 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1720 else
1721 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1725 parmval = convert_for_assignment (type, val,
1726 (char *) 0, /* arg passing */
1727 fundecl, name, parmnum + 1);
1729 if (PROMOTE_PROTOTYPES
1730 && INTEGRAL_TYPE_P (type)
1731 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1732 parmval = default_conversion (parmval);
1734 result = tree_cons (NULL_TREE, parmval, result);
1736 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1737 && (TYPE_PRECISION (TREE_TYPE (val))
1738 < TYPE_PRECISION (double_type_node)))
1739 /* Convert `float' to `double'. */
1740 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1741 else
1742 /* Convert `short' and `char' to full-size `int'. */
1743 result = tree_cons (NULL_TREE, default_conversion (val), result);
1745 if (typetail)
1746 typetail = TREE_CHAIN (typetail);
1749 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1751 if (name)
1752 error ("too few arguments to function `%s'",
1753 IDENTIFIER_POINTER (name));
1754 else
1755 error ("too few arguments to function");
1758 return nreverse (result);
1761 /* This is the entry point used by the parser
1762 for binary operators in the input.
1763 In addition to constructing the expression,
1764 we check for operands that were written with other binary operators
1765 in a way that is likely to confuse the user. */
1767 tree
1768 parser_build_binary_op (code, arg1, arg2)
1769 enum tree_code code;
1770 tree arg1, arg2;
1772 tree result = build_binary_op (code, arg1, arg2, 1);
1774 char class;
1775 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1776 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1777 enum tree_code code1 = ERROR_MARK;
1778 enum tree_code code2 = ERROR_MARK;
1780 if (TREE_CODE (result) == ERROR_MARK)
1781 return error_mark_node;
1783 if (IS_EXPR_CODE_CLASS (class1))
1784 code1 = C_EXP_ORIGINAL_CODE (arg1);
1785 if (IS_EXPR_CODE_CLASS (class2))
1786 code2 = C_EXP_ORIGINAL_CODE (arg2);
1788 /* Check for cases such as x+y<<z which users are likely
1789 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1790 is cleared to prevent these warnings. */
1791 if (warn_parentheses)
1793 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1795 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1796 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1797 warning ("suggest parentheses around + or - inside shift");
1800 if (code == TRUTH_ORIF_EXPR)
1802 if (code1 == TRUTH_ANDIF_EXPR
1803 || code2 == TRUTH_ANDIF_EXPR)
1804 warning ("suggest parentheses around && within ||");
1807 if (code == BIT_IOR_EXPR)
1809 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1810 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1811 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1812 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1813 warning ("suggest parentheses around arithmetic in operand of |");
1814 /* Check cases like x|y==z */
1815 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1816 warning ("suggest parentheses around comparison in operand of |");
1819 if (code == BIT_XOR_EXPR)
1821 if (code1 == BIT_AND_EXPR
1822 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1823 || code2 == BIT_AND_EXPR
1824 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1825 warning ("suggest parentheses around arithmetic in operand of ^");
1826 /* Check cases like x^y==z */
1827 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1828 warning ("suggest parentheses around comparison in operand of ^");
1831 if (code == BIT_AND_EXPR)
1833 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1834 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1835 warning ("suggest parentheses around + or - in operand of &");
1836 /* Check cases like x&y==z */
1837 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1838 warning ("suggest parentheses around comparison in operand of &");
1842 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1843 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1844 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1845 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1847 unsigned_conversion_warning (result, arg1);
1848 unsigned_conversion_warning (result, arg2);
1849 overflow_warning (result);
1851 class = TREE_CODE_CLASS (TREE_CODE (result));
1853 /* Record the code that was specified in the source,
1854 for the sake of warnings about confusing nesting. */
1855 if (IS_EXPR_CODE_CLASS (class))
1856 C_SET_EXP_ORIGINAL_CODE (result, code);
1857 else
1859 int flag = TREE_CONSTANT (result);
1860 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1861 so that convert_for_assignment wouldn't strip it.
1862 That way, we got warnings for things like p = (1 - 1).
1863 But it turns out we should not get those warnings. */
1864 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1865 C_SET_EXP_ORIGINAL_CODE (result, code);
1866 TREE_CONSTANT (result) = flag;
1869 return result;
1872 /* Build a binary-operation expression without default conversions.
1873 CODE is the kind of expression to build.
1874 This function differs from `build' in several ways:
1875 the data type of the result is computed and recorded in it,
1876 warnings are generated if arg data types are invalid,
1877 special handling for addition and subtraction of pointers is known,
1878 and some optimization is done (operations on narrow ints
1879 are done in the narrower type when that gives the same result).
1880 Constant folding is also done before the result is returned.
1882 Note that the operands will never have enumeral types, or function
1883 or array types, because either they will have the default conversions
1884 performed or they have both just been converted to some other type in which
1885 the arithmetic is to be done. */
1887 tree
1888 build_binary_op (code, orig_op0, orig_op1, convert_p)
1889 enum tree_code code;
1890 tree orig_op0, orig_op1;
1891 int convert_p;
1893 tree type0, type1;
1894 enum tree_code code0, code1;
1895 tree op0, op1;
1897 /* Expression code to give to the expression when it is built.
1898 Normally this is CODE, which is what the caller asked for,
1899 but in some special cases we change it. */
1900 enum tree_code resultcode = code;
1902 /* Data type in which the computation is to be performed.
1903 In the simplest cases this is the common type of the arguments. */
1904 tree result_type = NULL;
1906 /* Nonzero means operands have already been type-converted
1907 in whatever way is necessary.
1908 Zero means they need to be converted to RESULT_TYPE. */
1909 int converted = 0;
1911 /* Nonzero means create the expression with this type, rather than
1912 RESULT_TYPE. */
1913 tree build_type = 0;
1915 /* Nonzero means after finally constructing the expression
1916 convert it to this type. */
1917 tree final_type = 0;
1919 /* Nonzero if this is an operation like MIN or MAX which can
1920 safely be computed in short if both args are promoted shorts.
1921 Also implies COMMON.
1922 -1 indicates a bitwise operation; this makes a difference
1923 in the exact conditions for when it is safe to do the operation
1924 in a narrower mode. */
1925 int shorten = 0;
1927 /* Nonzero if this is a comparison operation;
1928 if both args are promoted shorts, compare the original shorts.
1929 Also implies COMMON. */
1930 int short_compare = 0;
1932 /* Nonzero if this is a right-shift operation, which can be computed on the
1933 original short and then promoted if the operand is a promoted short. */
1934 int short_shift = 0;
1936 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1937 int common = 0;
1939 if (convert_p)
1941 op0 = default_conversion (orig_op0);
1942 op1 = default_conversion (orig_op1);
1944 else
1946 op0 = orig_op0;
1947 op1 = orig_op1;
1950 type0 = TREE_TYPE (op0);
1951 type1 = TREE_TYPE (op1);
1953 /* The expression codes of the data types of the arguments tell us
1954 whether the arguments are integers, floating, pointers, etc. */
1955 code0 = TREE_CODE (type0);
1956 code1 = TREE_CODE (type1);
1958 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1959 STRIP_TYPE_NOPS (op0);
1960 STRIP_TYPE_NOPS (op1);
1962 /* If an error was already reported for one of the arguments,
1963 avoid reporting another error. */
1965 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1966 return error_mark_node;
1968 switch (code)
1970 case PLUS_EXPR:
1971 /* Handle the pointer + int case. */
1972 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1973 return pointer_int_sum (PLUS_EXPR, op0, op1);
1974 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1975 return pointer_int_sum (PLUS_EXPR, op1, op0);
1976 else
1977 common = 1;
1978 break;
1980 case MINUS_EXPR:
1981 /* Subtraction of two similar pointers.
1982 We must subtract them as integers, then divide by object size. */
1983 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1984 && comp_target_types (type0, type1))
1985 return pointer_diff (op0, op1);
1986 /* Handle pointer minus int. Just like pointer plus int. */
1987 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1988 return pointer_int_sum (MINUS_EXPR, op0, op1);
1989 else
1990 common = 1;
1991 break;
1993 case MULT_EXPR:
1994 common = 1;
1995 break;
1997 case TRUNC_DIV_EXPR:
1998 case CEIL_DIV_EXPR:
1999 case FLOOR_DIV_EXPR:
2000 case ROUND_DIV_EXPR:
2001 case EXACT_DIV_EXPR:
2002 /* Floating point division by zero is a legitimate way to obtain
2003 infinities and NaNs. */
2004 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2005 warning ("division by zero");
2007 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2008 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
2009 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2010 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
2012 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2013 resultcode = RDIV_EXPR;
2014 else
2015 /* Although it would be tempting to shorten always here, that
2016 loses on some targets, since the modulo instruction is
2017 undefined if the quotient can't be represented in the
2018 computation mode. We shorten only if unsigned or if
2019 dividing by something we know != -1. */
2020 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2021 || (TREE_CODE (op1) == INTEGER_CST
2022 && ! integer_all_onesp (op1)));
2023 common = 1;
2025 break;
2027 case BIT_AND_EXPR:
2028 case BIT_ANDTC_EXPR:
2029 case BIT_IOR_EXPR:
2030 case BIT_XOR_EXPR:
2031 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2032 shorten = -1;
2033 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
2034 common = 1;
2035 break;
2037 case TRUNC_MOD_EXPR:
2038 case FLOOR_MOD_EXPR:
2039 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2040 warning ("division by zero");
2042 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2044 /* Although it would be tempting to shorten always here, that loses
2045 on some targets, since the modulo instruction is undefined if the
2046 quotient can't be represented in the computation mode. We shorten
2047 only if unsigned or if dividing by something we know != -1. */
2048 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2049 || (TREE_CODE (op1) == INTEGER_CST
2050 && ! integer_all_onesp (op1)));
2051 common = 1;
2053 break;
2055 case TRUTH_ANDIF_EXPR:
2056 case TRUTH_ORIF_EXPR:
2057 case TRUTH_AND_EXPR:
2058 case TRUTH_OR_EXPR:
2059 case TRUTH_XOR_EXPR:
2060 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2061 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2062 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2063 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2065 /* Result of these operations is always an int,
2066 but that does not mean the operands should be
2067 converted to ints! */
2068 result_type = integer_type_node;
2069 op0 = c_common_truthvalue_conversion (op0);
2070 op1 = c_common_truthvalue_conversion (op1);
2071 converted = 1;
2073 break;
2075 /* Shift operations: result has same type as first operand;
2076 always convert second operand to int.
2077 Also set SHORT_SHIFT if shifting rightward. */
2079 case RSHIFT_EXPR:
2080 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2082 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2084 if (tree_int_cst_sgn (op1) < 0)
2085 warning ("right shift count is negative");
2086 else
2088 if (! integer_zerop (op1))
2089 short_shift = 1;
2091 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2092 warning ("right shift count >= width of type");
2096 /* Use the type of the value to be shifted. */
2097 result_type = type0;
2098 /* Convert the shift-count to an integer, regardless of size
2099 of value being shifted. */
2100 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2101 op1 = convert (integer_type_node, op1);
2102 /* Avoid converting op1 to result_type later. */
2103 converted = 1;
2105 break;
2107 case LSHIFT_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 ("left shift count is negative");
2115 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2116 warning ("left shift count >= width of type");
2119 /* Use the type of the value to be shifted. */
2120 result_type = type0;
2121 /* Convert the shift-count to an integer, regardless of size
2122 of value being shifted. */
2123 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2124 op1 = convert (integer_type_node, op1);
2125 /* Avoid converting op1 to result_type later. */
2126 converted = 1;
2128 break;
2130 case RROTATE_EXPR:
2131 case LROTATE_EXPR:
2132 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2134 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2136 if (tree_int_cst_sgn (op1) < 0)
2137 warning ("shift count is negative");
2138 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2139 warning ("shift count >= width of type");
2142 /* Use the type of the value to be shifted. */
2143 result_type = type0;
2144 /* Convert the shift-count to an integer, regardless of size
2145 of value being shifted. */
2146 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2147 op1 = convert (integer_type_node, op1);
2148 /* Avoid converting op1 to result_type later. */
2149 converted = 1;
2151 break;
2153 case EQ_EXPR:
2154 case NE_EXPR:
2155 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2156 warning ("comparing floating point with == or != is unsafe");
2157 /* Result of comparison is always int,
2158 but don't convert the args to int! */
2159 build_type = integer_type_node;
2160 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2161 || code0 == COMPLEX_TYPE
2162 || code0 == VECTOR_TYPE)
2163 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2164 || code1 == COMPLEX_TYPE
2165 || code1 == VECTOR_TYPE))
2166 short_compare = 1;
2167 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2169 tree tt0 = TREE_TYPE (type0);
2170 tree tt1 = TREE_TYPE (type1);
2171 /* Anything compares with void *. void * compares with anything.
2172 Otherwise, the targets must be compatible
2173 and both must be object or both incomplete. */
2174 if (comp_target_types (type0, type1))
2175 result_type = common_type (type0, type1);
2176 else if (VOID_TYPE_P (tt0))
2178 /* op0 != orig_op0 detects the case of something
2179 whose value is 0 but which isn't a valid null ptr const. */
2180 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2181 && TREE_CODE (tt1) == FUNCTION_TYPE)
2182 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2184 else if (VOID_TYPE_P (tt1))
2186 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2187 && TREE_CODE (tt0) == FUNCTION_TYPE)
2188 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2190 else
2191 pedwarn ("comparison of distinct pointer types lacks a cast");
2193 if (result_type == NULL_TREE)
2194 result_type = ptr_type_node;
2196 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2197 && integer_zerop (op1))
2198 result_type = type0;
2199 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2200 && integer_zerop (op0))
2201 result_type = type1;
2202 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2204 result_type = type0;
2205 pedwarn ("comparison between pointer and integer");
2207 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2209 result_type = type1;
2210 pedwarn ("comparison between pointer and integer");
2212 break;
2214 case MAX_EXPR:
2215 case MIN_EXPR:
2216 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2217 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2218 shorten = 1;
2219 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2221 if (comp_target_types (type0, type1))
2223 result_type = common_type (type0, type1);
2224 if (pedantic
2225 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2226 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2228 else
2230 result_type = ptr_type_node;
2231 pedwarn ("comparison of distinct pointer types lacks a cast");
2234 break;
2236 case LE_EXPR:
2237 case GE_EXPR:
2238 case LT_EXPR:
2239 case GT_EXPR:
2240 build_type = integer_type_node;
2241 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2242 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2243 short_compare = 1;
2244 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2246 if (comp_target_types (type0, type1))
2248 result_type = common_type (type0, type1);
2249 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2250 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2251 pedwarn ("comparison of complete and incomplete pointers");
2252 else if (pedantic
2253 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2254 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2256 else
2258 result_type = ptr_type_node;
2259 pedwarn ("comparison of distinct pointer types lacks a cast");
2262 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2263 && integer_zerop (op1))
2265 result_type = type0;
2266 if (pedantic || extra_warnings)
2267 pedwarn ("ordered comparison of pointer with integer zero");
2269 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2270 && integer_zerop (op0))
2272 result_type = type1;
2273 if (pedantic)
2274 pedwarn ("ordered comparison of pointer with integer zero");
2276 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2278 result_type = type0;
2279 pedwarn ("comparison between pointer and integer");
2281 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2283 result_type = type1;
2284 pedwarn ("comparison between pointer and integer");
2286 break;
2288 case UNORDERED_EXPR:
2289 case ORDERED_EXPR:
2290 case UNLT_EXPR:
2291 case UNLE_EXPR:
2292 case UNGT_EXPR:
2293 case UNGE_EXPR:
2294 case UNEQ_EXPR:
2295 build_type = integer_type_node;
2296 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2298 error ("unordered comparison on non-floating point argument");
2299 return error_mark_node;
2301 common = 1;
2302 break;
2304 default:
2305 break;
2308 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
2309 || code0 == VECTOR_TYPE)
2311 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
2312 || code1 == VECTOR_TYPE))
2314 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2316 if (shorten || common || short_compare)
2317 result_type = common_type (type0, type1);
2319 /* For certain operations (which identify themselves by shorten != 0)
2320 if both args were extended from the same smaller type,
2321 do the arithmetic in that type and then extend.
2323 shorten !=0 and !=1 indicates a bitwise operation.
2324 For them, this optimization is safe only if
2325 both args are zero-extended or both are sign-extended.
2326 Otherwise, we might change the result.
2327 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2328 but calculated in (unsigned short) it would be (unsigned short)-1. */
2330 if (shorten && none_complex)
2332 int unsigned0, unsigned1;
2333 tree arg0 = get_narrower (op0, &unsigned0);
2334 tree arg1 = get_narrower (op1, &unsigned1);
2335 /* UNS is 1 if the operation to be done is an unsigned one. */
2336 int uns = TREE_UNSIGNED (result_type);
2337 tree type;
2339 final_type = result_type;
2341 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2342 but it *requires* conversion to FINAL_TYPE. */
2344 if ((TYPE_PRECISION (TREE_TYPE (op0))
2345 == TYPE_PRECISION (TREE_TYPE (arg0)))
2346 && TREE_TYPE (op0) != final_type)
2347 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2348 if ((TYPE_PRECISION (TREE_TYPE (op1))
2349 == TYPE_PRECISION (TREE_TYPE (arg1)))
2350 && TREE_TYPE (op1) != final_type)
2351 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2353 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2355 /* For bitwise operations, signedness of nominal type
2356 does not matter. Consider only how operands were extended. */
2357 if (shorten == -1)
2358 uns = unsigned0;
2360 /* Note that in all three cases below we refrain from optimizing
2361 an unsigned operation on sign-extended args.
2362 That would not be valid. */
2364 /* Both args variable: if both extended in same way
2365 from same width, do it in that width.
2366 Do it unsigned if args were zero-extended. */
2367 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2368 < TYPE_PRECISION (result_type))
2369 && (TYPE_PRECISION (TREE_TYPE (arg1))
2370 == TYPE_PRECISION (TREE_TYPE (arg0)))
2371 && unsigned0 == unsigned1
2372 && (unsigned0 || !uns))
2373 result_type
2374 = c_common_signed_or_unsigned_type
2375 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2376 else if (TREE_CODE (arg0) == INTEGER_CST
2377 && (unsigned1 || !uns)
2378 && (TYPE_PRECISION (TREE_TYPE (arg1))
2379 < TYPE_PRECISION (result_type))
2380 && (type
2381 = c_common_signed_or_unsigned_type (unsigned1,
2382 TREE_TYPE (arg1)),
2383 int_fits_type_p (arg0, type)))
2384 result_type = type;
2385 else if (TREE_CODE (arg1) == INTEGER_CST
2386 && (unsigned0 || !uns)
2387 && (TYPE_PRECISION (TREE_TYPE (arg0))
2388 < TYPE_PRECISION (result_type))
2389 && (type
2390 = c_common_signed_or_unsigned_type (unsigned0,
2391 TREE_TYPE (arg0)),
2392 int_fits_type_p (arg1, type)))
2393 result_type = type;
2396 /* Shifts can be shortened if shifting right. */
2398 if (short_shift)
2400 int unsigned_arg;
2401 tree arg0 = get_narrower (op0, &unsigned_arg);
2403 final_type = result_type;
2405 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2406 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2408 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2409 /* We can shorten only if the shift count is less than the
2410 number of bits in the smaller type size. */
2411 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2412 /* We cannot drop an unsigned shift after sign-extension. */
2413 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2415 /* Do an unsigned shift if the operand was zero-extended. */
2416 result_type
2417 = c_common_signed_or_unsigned_type (unsigned_arg,
2418 TREE_TYPE (arg0));
2419 /* Convert value-to-be-shifted to that type. */
2420 if (TREE_TYPE (op0) != result_type)
2421 op0 = convert (result_type, op0);
2422 converted = 1;
2426 /* Comparison operations are shortened too but differently.
2427 They identify themselves by setting short_compare = 1. */
2429 if (short_compare)
2431 /* Don't write &op0, etc., because that would prevent op0
2432 from being kept in a register.
2433 Instead, make copies of the our local variables and
2434 pass the copies by reference, then copy them back afterward. */
2435 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2436 enum tree_code xresultcode = resultcode;
2437 tree val
2438 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2440 if (val != 0)
2441 return val;
2443 op0 = xop0, op1 = xop1;
2444 converted = 1;
2445 resultcode = xresultcode;
2447 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2448 && skip_evaluation == 0)
2450 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2451 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2452 int unsignedp0, unsignedp1;
2453 tree primop0 = get_narrower (op0, &unsignedp0);
2454 tree primop1 = get_narrower (op1, &unsignedp1);
2456 xop0 = orig_op0;
2457 xop1 = orig_op1;
2458 STRIP_TYPE_NOPS (xop0);
2459 STRIP_TYPE_NOPS (xop1);
2461 /* Give warnings for comparisons between signed and unsigned
2462 quantities that may fail.
2464 Do the checking based on the original operand trees, so that
2465 casts will be considered, but default promotions won't be.
2467 Do not warn if the comparison is being done in a signed type,
2468 since the signed type will only be chosen if it can represent
2469 all the values of the unsigned type. */
2470 if (! TREE_UNSIGNED (result_type))
2471 /* OK */;
2472 /* Do not warn if both operands are the same signedness. */
2473 else if (op0_signed == op1_signed)
2474 /* OK */;
2475 else
2477 tree sop, uop;
2479 if (op0_signed)
2480 sop = xop0, uop = xop1;
2481 else
2482 sop = xop1, uop = xop0;
2484 /* Do not warn if the signed quantity is an
2485 unsuffixed integer literal (or some static
2486 constant expression involving such literals or a
2487 conditional expression involving such literals)
2488 and it is non-negative. */
2489 if (tree_expr_nonnegative_p (sop))
2490 /* OK */;
2491 /* Do not warn if the comparison is an equality operation,
2492 the unsigned quantity is an integral constant, and it
2493 would fit in the result if the result were signed. */
2494 else if (TREE_CODE (uop) == INTEGER_CST
2495 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2496 && int_fits_type_p
2497 (uop, c_common_signed_type (result_type)))
2498 /* OK */;
2499 /* Do not warn if the unsigned quantity is an enumeration
2500 constant and its maximum value would fit in the result
2501 if the result were signed. */
2502 else if (TREE_CODE (uop) == INTEGER_CST
2503 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2504 && int_fits_type_p
2505 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2506 c_common_signed_type (result_type)))
2507 /* OK */;
2508 else
2509 warning ("comparison between signed and unsigned");
2512 /* Warn if two unsigned values are being compared in a size
2513 larger than their original size, and one (and only one) is the
2514 result of a `~' operator. This comparison will always fail.
2516 Also warn if one operand is a constant, and the constant
2517 does not have all bits set that are set in the ~ operand
2518 when it is extended. */
2520 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2521 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2523 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2524 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2525 &unsignedp0);
2526 else
2527 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2528 &unsignedp1);
2530 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2532 tree primop;
2533 HOST_WIDE_INT constant, mask;
2534 int unsignedp, bits;
2536 if (host_integerp (primop0, 0))
2538 primop = primop1;
2539 unsignedp = unsignedp1;
2540 constant = tree_low_cst (primop0, 0);
2542 else
2544 primop = primop0;
2545 unsignedp = unsignedp0;
2546 constant = tree_low_cst (primop1, 0);
2549 bits = TYPE_PRECISION (TREE_TYPE (primop));
2550 if (bits < TYPE_PRECISION (result_type)
2551 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2553 mask = (~ (HOST_WIDE_INT) 0) << bits;
2554 if ((mask & constant) != mask)
2555 warning ("comparison of promoted ~unsigned with constant");
2558 else if (unsignedp0 && unsignedp1
2559 && (TYPE_PRECISION (TREE_TYPE (primop0))
2560 < TYPE_PRECISION (result_type))
2561 && (TYPE_PRECISION (TREE_TYPE (primop1))
2562 < TYPE_PRECISION (result_type)))
2563 warning ("comparison of promoted ~unsigned with unsigned");
2569 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2570 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2571 Then the expression will be built.
2572 It will be given type FINAL_TYPE if that is nonzero;
2573 otherwise, it will be given type RESULT_TYPE. */
2575 if (!result_type)
2577 binary_op_error (code);
2578 return error_mark_node;
2581 if (! converted)
2583 if (TREE_TYPE (op0) != result_type)
2584 op0 = convert (result_type, op0);
2585 if (TREE_TYPE (op1) != result_type)
2586 op1 = convert (result_type, op1);
2589 if (build_type == NULL_TREE)
2590 build_type = result_type;
2593 tree result = build (resultcode, build_type, op0, op1);
2594 tree folded;
2596 folded = fold (result);
2597 if (folded == result)
2598 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2599 if (final_type != 0)
2600 return convert (final_type, folded);
2601 return folded;
2605 /* Return a tree for the difference of pointers OP0 and OP1.
2606 The resulting tree has type int. */
2608 static tree
2609 pointer_diff (op0, op1)
2610 tree op0, op1;
2612 tree result, folded;
2613 tree restype = ptrdiff_type_node;
2615 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2616 tree con0, con1, lit0, lit1;
2617 tree orig_op1 = op1;
2619 if (pedantic || warn_pointer_arith)
2621 if (TREE_CODE (target_type) == VOID_TYPE)
2622 pedwarn ("pointer of type `void *' used in subtraction");
2623 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2624 pedwarn ("pointer to a function used in subtraction");
2627 /* If the conversion to ptrdiff_type does anything like widening or
2628 converting a partial to an integral mode, we get a convert_expression
2629 that is in the way to do any simplifications.
2630 (fold-const.c doesn't know that the extra bits won't be needed.
2631 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2632 different mode in place.)
2633 So first try to find a common term here 'by hand'; we want to cover
2634 at least the cases that occur in legal static initializers. */
2635 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2636 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2638 if (TREE_CODE (con0) == PLUS_EXPR)
2640 lit0 = TREE_OPERAND (con0, 1);
2641 con0 = TREE_OPERAND (con0, 0);
2643 else
2644 lit0 = integer_zero_node;
2646 if (TREE_CODE (con1) == PLUS_EXPR)
2648 lit1 = TREE_OPERAND (con1, 1);
2649 con1 = TREE_OPERAND (con1, 0);
2651 else
2652 lit1 = integer_zero_node;
2654 if (operand_equal_p (con0, con1, 0))
2656 op0 = lit0;
2657 op1 = lit1;
2661 /* First do the subtraction as integers;
2662 then drop through to build the divide operator.
2663 Do not do default conversions on the minus operator
2664 in case restype is a short type. */
2666 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2667 convert (restype, op1), 0);
2668 /* This generates an error if op1 is pointer to incomplete type. */
2669 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2670 error ("arithmetic on pointer to an incomplete type");
2672 /* This generates an error if op0 is pointer to incomplete type. */
2673 op1 = c_size_in_bytes (target_type);
2675 /* Divide by the size, in easiest possible way. */
2677 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2679 folded = fold (result);
2680 if (folded == result)
2681 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2682 return folded;
2685 /* Construct and perhaps optimize a tree representation
2686 for a unary operation. CODE, a tree_code, specifies the operation
2687 and XARG is the operand.
2688 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2689 the default promotions (such as from short to int).
2690 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2691 allows non-lvalues; this is only used to handle conversion of non-lvalue
2692 arrays to pointers in C99. */
2694 tree
2695 build_unary_op (code, xarg, flag)
2696 enum tree_code code;
2697 tree xarg;
2698 int flag;
2700 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2701 tree arg = xarg;
2702 tree argtype = 0;
2703 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2704 tree val;
2705 int noconvert = flag;
2707 if (typecode == ERROR_MARK)
2708 return error_mark_node;
2709 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2710 typecode = INTEGER_TYPE;
2712 switch (code)
2714 case CONVERT_EXPR:
2715 /* This is used for unary plus, because a CONVERT_EXPR
2716 is enough to prevent anybody from looking inside for
2717 associativity, but won't generate any code. */
2718 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2719 || typecode == COMPLEX_TYPE))
2721 error ("wrong type argument to unary plus");
2722 return error_mark_node;
2724 else if (!noconvert)
2725 arg = default_conversion (arg);
2726 arg = non_lvalue (arg);
2727 break;
2729 case NEGATE_EXPR:
2730 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2731 || typecode == COMPLEX_TYPE
2732 || typecode == VECTOR_TYPE))
2734 error ("wrong type argument to unary minus");
2735 return error_mark_node;
2737 else if (!noconvert)
2738 arg = default_conversion (arg);
2739 break;
2741 case BIT_NOT_EXPR:
2742 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2744 if (!noconvert)
2745 arg = default_conversion (arg);
2747 else if (typecode == COMPLEX_TYPE)
2749 code = CONJ_EXPR;
2750 if (pedantic)
2751 pedwarn ("ISO C does not support `~' for complex conjugation");
2752 if (!noconvert)
2753 arg = default_conversion (arg);
2755 else
2757 error ("wrong type argument to bit-complement");
2758 return error_mark_node;
2760 break;
2762 case ABS_EXPR:
2763 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2764 || typecode == COMPLEX_TYPE))
2766 error ("wrong type argument to abs");
2767 return error_mark_node;
2769 else if (!noconvert)
2770 arg = default_conversion (arg);
2771 break;
2773 case CONJ_EXPR:
2774 /* Conjugating a real value is a no-op, but allow it anyway. */
2775 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2776 || typecode == COMPLEX_TYPE))
2778 error ("wrong type argument to conjugation");
2779 return error_mark_node;
2781 else if (!noconvert)
2782 arg = default_conversion (arg);
2783 break;
2785 case TRUTH_NOT_EXPR:
2786 if (typecode != INTEGER_TYPE
2787 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2788 && typecode != COMPLEX_TYPE
2789 /* These will convert to a pointer. */
2790 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2792 error ("wrong type argument to unary exclamation mark");
2793 return error_mark_node;
2795 arg = c_common_truthvalue_conversion (arg);
2796 return invert_truthvalue (arg);
2798 case NOP_EXPR:
2799 break;
2801 case REALPART_EXPR:
2802 if (TREE_CODE (arg) == COMPLEX_CST)
2803 return TREE_REALPART (arg);
2804 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2805 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2806 else
2807 return arg;
2809 case IMAGPART_EXPR:
2810 if (TREE_CODE (arg) == COMPLEX_CST)
2811 return TREE_IMAGPART (arg);
2812 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2813 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2814 else
2815 return convert (TREE_TYPE (arg), integer_zero_node);
2817 case PREINCREMENT_EXPR:
2818 case POSTINCREMENT_EXPR:
2819 case PREDECREMENT_EXPR:
2820 case POSTDECREMENT_EXPR:
2821 /* Handle complex lvalues (when permitted)
2822 by reduction to simpler cases. */
2824 val = unary_complex_lvalue (code, arg, 0);
2825 if (val != 0)
2826 return val;
2828 /* Increment or decrement the real part of the value,
2829 and don't change the imaginary part. */
2830 if (typecode == COMPLEX_TYPE)
2832 tree real, imag;
2834 if (pedantic)
2835 pedwarn ("ISO C does not support `++' and `--' on complex types");
2837 arg = stabilize_reference (arg);
2838 real = build_unary_op (REALPART_EXPR, arg, 1);
2839 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2840 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2841 build_unary_op (code, real, 1), imag);
2844 /* Report invalid types. */
2846 if (typecode != POINTER_TYPE
2847 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2849 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2850 error ("wrong type argument to increment");
2851 else
2852 error ("wrong type argument to decrement");
2854 return error_mark_node;
2858 tree inc;
2859 tree result_type = TREE_TYPE (arg);
2861 arg = get_unwidened (arg, 0);
2862 argtype = TREE_TYPE (arg);
2864 /* Compute the increment. */
2866 if (typecode == POINTER_TYPE)
2868 /* If pointer target is an undefined struct,
2869 we just cannot know how to do the arithmetic. */
2870 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2872 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2873 error ("increment of pointer to unknown structure");
2874 else
2875 error ("decrement of pointer to unknown structure");
2877 else if ((pedantic || warn_pointer_arith)
2878 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2879 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2881 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2882 pedwarn ("wrong type argument to increment");
2883 else
2884 pedwarn ("wrong type argument to decrement");
2887 inc = c_size_in_bytes (TREE_TYPE (result_type));
2889 else
2890 inc = integer_one_node;
2892 inc = convert (argtype, inc);
2894 /* Handle incrementing a cast-expression. */
2896 while (1)
2897 switch (TREE_CODE (arg))
2899 case NOP_EXPR:
2900 case CONVERT_EXPR:
2901 case FLOAT_EXPR:
2902 case FIX_TRUNC_EXPR:
2903 case FIX_FLOOR_EXPR:
2904 case FIX_ROUND_EXPR:
2905 case FIX_CEIL_EXPR:
2906 pedantic_lvalue_warning (CONVERT_EXPR);
2907 /* If the real type has the same machine representation
2908 as the type it is cast to, we can make better output
2909 by adding directly to the inside of the cast. */
2910 if ((TREE_CODE (TREE_TYPE (arg))
2911 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2912 && (TYPE_MODE (TREE_TYPE (arg))
2913 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2914 arg = TREE_OPERAND (arg, 0);
2915 else
2917 tree incremented, modify, value;
2918 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2919 value = boolean_increment (code, arg);
2920 else
2922 arg = stabilize_reference (arg);
2923 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2924 value = arg;
2925 else
2926 value = save_expr (arg);
2927 incremented = build (((code == PREINCREMENT_EXPR
2928 || code == POSTINCREMENT_EXPR)
2929 ? PLUS_EXPR : MINUS_EXPR),
2930 argtype, value, inc);
2931 TREE_SIDE_EFFECTS (incremented) = 1;
2932 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2933 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2935 TREE_USED (value) = 1;
2936 return value;
2938 break;
2940 default:
2941 goto give_up;
2943 give_up:
2945 /* Complain about anything else that is not a true lvalue. */
2946 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2947 || code == POSTINCREMENT_EXPR)
2948 ? "invalid lvalue in increment"
2949 : "invalid lvalue in decrement")))
2950 return error_mark_node;
2952 /* Report a read-only lvalue. */
2953 if (TREE_READONLY (arg))
2954 readonly_warning (arg,
2955 ((code == PREINCREMENT_EXPR
2956 || code == POSTINCREMENT_EXPR)
2957 ? "increment" : "decrement"));
2959 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2960 val = boolean_increment (code, arg);
2961 else
2962 val = build (code, TREE_TYPE (arg), arg, inc);
2963 TREE_SIDE_EFFECTS (val) = 1;
2964 val = convert (result_type, val);
2965 if (TREE_CODE (val) != code)
2966 TREE_NO_UNUSED_WARNING (val) = 1;
2967 return val;
2970 case ADDR_EXPR:
2971 /* Note that this operation never does default_conversion. */
2973 /* Let &* cancel out to simplify resulting code. */
2974 if (TREE_CODE (arg) == INDIRECT_REF)
2976 /* Don't let this be an lvalue. */
2977 if (lvalue_p (TREE_OPERAND (arg, 0)))
2978 return non_lvalue (TREE_OPERAND (arg, 0));
2979 return TREE_OPERAND (arg, 0);
2982 /* For &x[y], return x+y */
2983 if (TREE_CODE (arg) == ARRAY_REF)
2985 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2986 return error_mark_node;
2987 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2988 TREE_OPERAND (arg, 1), 1);
2991 /* Handle complex lvalues (when permitted)
2992 by reduction to simpler cases. */
2993 val = unary_complex_lvalue (code, arg, flag);
2994 if (val != 0)
2995 return val;
2997 #if 0 /* Turned off because inconsistent;
2998 float f; *&(int)f = 3.4 stores in int format
2999 whereas (int)f = 3.4 stores in float format. */
3000 /* Address of a cast is just a cast of the address
3001 of the operand of the cast. */
3002 switch (TREE_CODE (arg))
3004 case NOP_EXPR:
3005 case CONVERT_EXPR:
3006 case FLOAT_EXPR:
3007 case FIX_TRUNC_EXPR:
3008 case FIX_FLOOR_EXPR:
3009 case FIX_ROUND_EXPR:
3010 case FIX_CEIL_EXPR:
3011 if (pedantic)
3012 pedwarn ("ISO C forbids the address of a cast expression");
3013 return convert (build_pointer_type (TREE_TYPE (arg)),
3014 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3015 0));
3017 #endif
3019 /* Anything not already handled and not a true memory reference
3020 or a non-lvalue array is an error. */
3021 else if (typecode != FUNCTION_TYPE && !flag
3022 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3023 return error_mark_node;
3025 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3026 argtype = TREE_TYPE (arg);
3028 /* If the lvalue is const or volatile, merge that into the type
3029 to which the address will point. Note that you can't get a
3030 restricted pointer by taking the address of something, so we
3031 only have to deal with `const' and `volatile' here. */
3032 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3033 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3034 argtype = c_build_type_variant (argtype,
3035 TREE_READONLY (arg),
3036 TREE_THIS_VOLATILE (arg));
3038 argtype = build_pointer_type (argtype);
3040 if (!c_mark_addressable (arg))
3041 return error_mark_node;
3044 tree addr;
3046 if (TREE_CODE (arg) == COMPONENT_REF)
3048 tree field = TREE_OPERAND (arg, 1);
3050 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
3052 if (DECL_C_BIT_FIELD (field))
3054 error ("attempt to take address of bit-field structure member `%s'",
3055 IDENTIFIER_POINTER (DECL_NAME (field)));
3056 return error_mark_node;
3059 addr = fold (build (PLUS_EXPR, argtype,
3060 convert (argtype, addr),
3061 convert (argtype, byte_position (field))));
3063 else
3064 addr = build1 (code, argtype, arg);
3066 /* Address of a static or external variable or
3067 file-scope function counts as a constant. */
3068 if (staticp (arg)
3069 && ! (TREE_CODE (arg) == FUNCTION_DECL
3070 && DECL_CONTEXT (arg) != 0))
3071 TREE_CONSTANT (addr) = 1;
3072 return addr;
3075 default:
3076 break;
3079 if (argtype == 0)
3080 argtype = TREE_TYPE (arg);
3081 return fold (build1 (code, argtype, arg));
3084 #if 0
3085 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3086 convert ARG with the same conversions in the same order
3087 and return the result. */
3089 static tree
3090 convert_sequence (conversions, arg)
3091 tree conversions;
3092 tree arg;
3094 switch (TREE_CODE (conversions))
3096 case NOP_EXPR:
3097 case CONVERT_EXPR:
3098 case FLOAT_EXPR:
3099 case FIX_TRUNC_EXPR:
3100 case FIX_FLOOR_EXPR:
3101 case FIX_ROUND_EXPR:
3102 case FIX_CEIL_EXPR:
3103 return convert (TREE_TYPE (conversions),
3104 convert_sequence (TREE_OPERAND (conversions, 0),
3105 arg));
3107 default:
3108 return arg;
3111 #endif /* 0 */
3113 /* Return nonzero if REF is an lvalue valid for this language.
3114 Lvalues can be assigned, unless their type has TYPE_READONLY.
3115 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3118 lvalue_p (ref)
3119 tree ref;
3121 enum tree_code code = TREE_CODE (ref);
3123 switch (code)
3125 case REALPART_EXPR:
3126 case IMAGPART_EXPR:
3127 case COMPONENT_REF:
3128 return lvalue_p (TREE_OPERAND (ref, 0));
3130 case COMPOUND_LITERAL_EXPR:
3131 case STRING_CST:
3132 return 1;
3134 case INDIRECT_REF:
3135 case ARRAY_REF:
3136 case VAR_DECL:
3137 case PARM_DECL:
3138 case RESULT_DECL:
3139 case ERROR_MARK:
3140 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3141 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3143 case BIND_EXPR:
3144 case RTL_EXPR:
3145 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3147 default:
3148 return 0;
3152 /* Return nonzero if REF is an lvalue valid for this language;
3153 otherwise, print an error message and return zero. */
3156 lvalue_or_else (ref, msgid)
3157 tree ref;
3158 const char *msgid;
3160 int win = lvalue_p (ref);
3162 if (! win)
3163 error ("%s", msgid);
3165 return win;
3168 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3169 for certain kinds of expressions which are not really lvalues
3170 but which we can accept as lvalues. If FLAG is nonzero, then
3171 non-lvalues are OK since we may be converting a non-lvalue array to
3172 a pointer in C99.
3174 If ARG is not a kind of expression we can handle, return zero. */
3176 static tree
3177 unary_complex_lvalue (code, arg, flag)
3178 enum tree_code code;
3179 tree arg;
3180 int flag;
3182 /* Handle (a, b) used as an "lvalue". */
3183 if (TREE_CODE (arg) == COMPOUND_EXPR)
3185 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3187 /* If this returns a function type, it isn't really being used as
3188 an lvalue, so don't issue a warning about it. */
3189 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3190 pedantic_lvalue_warning (COMPOUND_EXPR);
3192 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3193 TREE_OPERAND (arg, 0), real_result);
3196 /* Handle (a ? b : c) used as an "lvalue". */
3197 if (TREE_CODE (arg) == COND_EXPR)
3199 if (!flag)
3200 pedantic_lvalue_warning (COND_EXPR);
3201 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
3202 pedantic_lvalue_warning (COMPOUND_EXPR);
3204 return (build_conditional_expr
3205 (TREE_OPERAND (arg, 0),
3206 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3207 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
3210 return 0;
3213 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3214 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3216 static void
3217 pedantic_lvalue_warning (code)
3218 enum tree_code code;
3220 if (pedantic)
3221 switch (code)
3223 case COND_EXPR:
3224 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3225 break;
3226 case COMPOUND_EXPR:
3227 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3228 break;
3229 default:
3230 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3231 break;
3235 /* Warn about storing in something that is `const'. */
3237 void
3238 readonly_warning (arg, msgid)
3239 tree arg;
3240 const char *msgid;
3242 if (TREE_CODE (arg) == COMPONENT_REF)
3244 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3245 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3246 else
3247 pedwarn ("%s of read-only member `%s'", _(msgid),
3248 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3250 else if (TREE_CODE (arg) == VAR_DECL)
3251 pedwarn ("%s of read-only variable `%s'", _(msgid),
3252 IDENTIFIER_POINTER (DECL_NAME (arg)));
3253 else
3254 pedwarn ("%s of read-only location", _(msgid));
3257 /* Mark EXP saying that we need to be able to take the
3258 address of it; it should not be allocated in a register.
3259 Returns true if successful. */
3261 bool
3262 c_mark_addressable (exp)
3263 tree exp;
3265 tree x = exp;
3267 while (1)
3268 switch (TREE_CODE (x))
3270 case COMPONENT_REF:
3271 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3273 error ("cannot take address of bit-field `%s'",
3274 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3275 return false;
3278 /* ... fall through ... */
3280 case ADDR_EXPR:
3281 case ARRAY_REF:
3282 case REALPART_EXPR:
3283 case IMAGPART_EXPR:
3284 x = TREE_OPERAND (x, 0);
3285 break;
3287 case COMPOUND_LITERAL_EXPR:
3288 case CONSTRUCTOR:
3289 TREE_ADDRESSABLE (x) = 1;
3290 return true;
3292 case VAR_DECL:
3293 case CONST_DECL:
3294 case PARM_DECL:
3295 case RESULT_DECL:
3296 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3297 && DECL_NONLOCAL (x))
3299 if (TREE_PUBLIC (x))
3301 error ("global register variable `%s' used in nested function",
3302 IDENTIFIER_POINTER (DECL_NAME (x)));
3303 return false;
3305 pedwarn ("register variable `%s' used in nested function",
3306 IDENTIFIER_POINTER (DECL_NAME (x)));
3308 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3310 if (TREE_PUBLIC (x))
3312 error ("address of global register variable `%s' requested",
3313 IDENTIFIER_POINTER (DECL_NAME (x)));
3314 return false;
3317 /* If we are making this addressable due to its having
3318 volatile components, give a different error message. Also
3319 handle the case of an unnamed parameter by not trying
3320 to give the name. */
3322 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3324 error ("cannot put object with volatile field into register");
3325 return false;
3328 pedwarn ("address of register variable `%s' requested",
3329 IDENTIFIER_POINTER (DECL_NAME (x)));
3331 put_var_into_stack (x);
3333 /* drops in */
3334 case FUNCTION_DECL:
3335 TREE_ADDRESSABLE (x) = 1;
3336 #if 0 /* poplevel deals with this now. */
3337 if (DECL_CONTEXT (x) == 0)
3338 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3339 #endif
3341 default:
3342 return true;
3346 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3348 tree
3349 build_conditional_expr (ifexp, op1, op2)
3350 tree ifexp, op1, op2;
3352 tree type1;
3353 tree type2;
3354 enum tree_code code1;
3355 enum tree_code code2;
3356 tree result_type = NULL;
3357 tree orig_op1 = op1, orig_op2 = op2;
3359 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
3361 #if 0 /* Produces wrong result if within sizeof. */
3362 /* Don't promote the operands separately if they promote
3363 the same way. Return the unpromoted type and let the combined
3364 value get promoted if necessary. */
3366 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3367 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3368 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3369 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3371 if (TREE_CODE (ifexp) == INTEGER_CST)
3372 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3374 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3376 #endif
3378 /* Promote both alternatives. */
3380 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3381 op1 = default_conversion (op1);
3382 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3383 op2 = default_conversion (op2);
3385 if (TREE_CODE (ifexp) == ERROR_MARK
3386 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3387 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3388 return error_mark_node;
3390 type1 = TREE_TYPE (op1);
3391 code1 = TREE_CODE (type1);
3392 type2 = TREE_TYPE (op2);
3393 code2 = TREE_CODE (type2);
3395 /* Quickly detect the usual case where op1 and op2 have the same type
3396 after promotion. */
3397 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3399 if (type1 == type2)
3400 result_type = type1;
3401 else
3402 result_type = TYPE_MAIN_VARIANT (type1);
3404 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3405 || code1 == COMPLEX_TYPE)
3406 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3407 || code2 == COMPLEX_TYPE))
3409 result_type = common_type (type1, type2);
3411 /* If -Wsign-compare, warn here if type1 and type2 have
3412 different signedness. We'll promote the signed to unsigned
3413 and later code won't know it used to be different.
3414 Do this check on the original types, so that explicit casts
3415 will be considered, but default promotions won't. */
3416 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3417 && !skip_evaluation)
3419 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3420 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3422 if (unsigned_op1 ^ unsigned_op2)
3424 /* Do not warn if the result type is signed, since the
3425 signed type will only be chosen if it can represent
3426 all the values of the unsigned type. */
3427 if (! TREE_UNSIGNED (result_type))
3428 /* OK */;
3429 /* Do not warn if the signed quantity is an unsuffixed
3430 integer literal (or some static constant expression
3431 involving such literals) and it is non-negative. */
3432 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3433 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3434 /* OK */;
3435 else
3436 warning ("signed and unsigned type in conditional expression");
3440 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3442 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3443 pedwarn ("ISO C forbids conditional expr with only one void side");
3444 result_type = void_type_node;
3446 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3448 if (comp_target_types (type1, type2))
3449 result_type = common_type (type1, type2);
3450 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3451 && TREE_CODE (orig_op1) != NOP_EXPR)
3452 result_type = qualify_type (type2, type1);
3453 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3454 && TREE_CODE (orig_op2) != NOP_EXPR)
3455 result_type = qualify_type (type1, type2);
3456 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3458 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3459 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3460 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3461 TREE_TYPE (type2)));
3463 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3465 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3466 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3467 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3468 TREE_TYPE (type1)));
3470 else
3472 pedwarn ("pointer type mismatch in conditional expression");
3473 result_type = build_pointer_type (void_type_node);
3476 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3478 if (! integer_zerop (op2))
3479 pedwarn ("pointer/integer type mismatch in conditional expression");
3480 else
3482 op2 = null_pointer_node;
3484 result_type = type1;
3486 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3488 if (!integer_zerop (op1))
3489 pedwarn ("pointer/integer type mismatch in conditional expression");
3490 else
3492 op1 = null_pointer_node;
3494 result_type = type2;
3497 if (!result_type)
3499 if (flag_cond_mismatch)
3500 result_type = void_type_node;
3501 else
3503 error ("type mismatch in conditional expression");
3504 return error_mark_node;
3508 /* Merge const and volatile flags of the incoming types. */
3509 result_type
3510 = build_type_variant (result_type,
3511 TREE_READONLY (op1) || TREE_READONLY (op2),
3512 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3514 if (result_type != TREE_TYPE (op1))
3515 op1 = convert_and_check (result_type, op1);
3516 if (result_type != TREE_TYPE (op2))
3517 op2 = convert_and_check (result_type, op2);
3519 if (TREE_CODE (ifexp) == INTEGER_CST)
3520 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3522 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3525 /* Given a list of expressions, return a compound expression
3526 that performs them all and returns the value of the last of them. */
3528 tree
3529 build_compound_expr (list)
3530 tree list;
3532 return internal_build_compound_expr (list, TRUE);
3535 static tree
3536 internal_build_compound_expr (list, first_p)
3537 tree list;
3538 int first_p;
3540 tree rest;
3542 if (TREE_CHAIN (list) == 0)
3544 /* Convert arrays and functions to pointers when there
3545 really is a comma operator. */
3546 if (!first_p)
3547 TREE_VALUE (list)
3548 = default_function_array_conversion (TREE_VALUE (list));
3550 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3551 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3553 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3554 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3555 list = TREE_OPERAND (list, 0);
3556 #endif
3558 /* Don't let (0, 0) be null pointer constant. */
3559 if (!first_p && integer_zerop (TREE_VALUE (list)))
3560 return non_lvalue (TREE_VALUE (list));
3561 return TREE_VALUE (list);
3564 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3566 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3568 /* The left-hand operand of a comma expression is like an expression
3569 statement: with -W or -Wunused, we should warn if it doesn't have
3570 any side-effects, unless it was explicitly cast to (void). */
3571 if ((extra_warnings || warn_unused_value)
3572 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3573 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3574 warning ("left-hand operand of comma expression has no effect");
3576 /* When pedantic, a compound expression can be neither an lvalue
3577 nor an integer constant expression. */
3578 if (! pedantic)
3579 return rest;
3582 /* With -Wunused, we should also warn if the left-hand operand does have
3583 side-effects, but computes a value which is not used. For example, in
3584 `foo() + bar(), baz()' the result of the `+' operator is not used,
3585 so we should issue a warning. */
3586 else if (warn_unused_value)
3587 warn_if_unused_value (TREE_VALUE (list));
3589 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3592 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3594 tree
3595 build_c_cast (type, expr)
3596 tree type;
3597 tree expr;
3599 tree value = expr;
3601 if (type == error_mark_node || expr == error_mark_node)
3602 return error_mark_node;
3603 type = TYPE_MAIN_VARIANT (type);
3605 #if 0
3606 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3607 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3608 value = TREE_OPERAND (value, 0);
3609 #endif
3611 if (TREE_CODE (type) == ARRAY_TYPE)
3613 error ("cast specifies array type");
3614 return error_mark_node;
3617 if (TREE_CODE (type) == FUNCTION_TYPE)
3619 error ("cast specifies function type");
3620 return error_mark_node;
3623 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3625 if (pedantic)
3627 if (TREE_CODE (type) == RECORD_TYPE
3628 || TREE_CODE (type) == UNION_TYPE)
3629 pedwarn ("ISO C forbids casting nonscalar to the same type");
3632 else if (TREE_CODE (type) == UNION_TYPE)
3634 tree field;
3635 value = default_function_array_conversion (value);
3637 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3638 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3639 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3640 break;
3642 if (field)
3644 const char *name;
3645 tree t;
3647 if (pedantic)
3648 pedwarn ("ISO C forbids casts to union type");
3649 if (TYPE_NAME (type) != 0)
3651 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3652 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3653 else
3654 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3656 else
3657 name = "";
3658 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3659 build_tree_list (field, value)), 0);
3660 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3661 return t;
3663 error ("cast to union type from type not present in union");
3664 return error_mark_node;
3666 else
3668 tree otype, ovalue;
3670 /* If casting to void, avoid the error that would come
3671 from default_conversion in the case of a non-lvalue array. */
3672 if (type == void_type_node)
3673 return build1 (CONVERT_EXPR, type, value);
3675 /* Convert functions and arrays to pointers,
3676 but don't convert any other types. */
3677 value = default_function_array_conversion (value);
3678 otype = TREE_TYPE (value);
3680 /* Optionally warn about potentially worrisome casts. */
3682 if (warn_cast_qual
3683 && TREE_CODE (type) == POINTER_TYPE
3684 && TREE_CODE (otype) == POINTER_TYPE)
3686 tree in_type = type;
3687 tree in_otype = otype;
3688 int added = 0;
3689 int discarded = 0;
3691 /* Check that the qualifiers on IN_TYPE are a superset of
3692 the qualifiers of IN_OTYPE. The outermost level of
3693 POINTER_TYPE nodes is uninteresting and we stop as soon
3694 as we hit a non-POINTER_TYPE node on either type. */
3697 in_otype = TREE_TYPE (in_otype);
3698 in_type = TREE_TYPE (in_type);
3700 /* GNU C allows cv-qualified function types. 'const'
3701 means the function is very pure, 'volatile' means it
3702 can't return. We need to warn when such qualifiers
3703 are added, not when they're taken away. */
3704 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3705 && TREE_CODE (in_type) == FUNCTION_TYPE)
3706 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3707 else
3708 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3710 while (TREE_CODE (in_type) == POINTER_TYPE
3711 && TREE_CODE (in_otype) == POINTER_TYPE);
3713 if (added)
3714 warning ("cast adds new qualifiers to function type");
3716 if (discarded)
3717 /* There are qualifiers present in IN_OTYPE that are not
3718 present in IN_TYPE. */
3719 warning ("cast discards qualifiers from pointer target type");
3722 /* Warn about possible alignment problems. */
3723 if (STRICT_ALIGNMENT && warn_cast_align
3724 && TREE_CODE (type) == POINTER_TYPE
3725 && TREE_CODE (otype) == POINTER_TYPE
3726 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3727 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3728 /* Don't warn about opaque types, where the actual alignment
3729 restriction is unknown. */
3730 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3731 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3732 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3733 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3734 warning ("cast increases required alignment of target type");
3736 if (TREE_CODE (type) == INTEGER_TYPE
3737 && TREE_CODE (otype) == POINTER_TYPE
3738 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3739 && !TREE_CONSTANT (value))
3740 warning ("cast from pointer to integer of different size");
3742 if (warn_bad_function_cast
3743 && TREE_CODE (value) == CALL_EXPR
3744 && TREE_CODE (type) != TREE_CODE (otype))
3745 warning ("cast does not match function type");
3747 if (TREE_CODE (type) == POINTER_TYPE
3748 && TREE_CODE (otype) == INTEGER_TYPE
3749 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3750 /* Don't warn about converting any constant. */
3751 && !TREE_CONSTANT (value))
3752 warning ("cast to pointer from integer of different size");
3754 ovalue = value;
3755 value = convert (type, value);
3757 /* Ignore any integer overflow caused by the cast. */
3758 if (TREE_CODE (value) == INTEGER_CST)
3760 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3761 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3765 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3766 if (pedantic && TREE_CODE (value) == INTEGER_CST
3767 && TREE_CODE (expr) == INTEGER_CST
3768 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3769 value = non_lvalue (value);
3771 /* If pedantic, don't let a cast be an lvalue. */
3772 if (value == expr && pedantic)
3773 value = non_lvalue (value);
3775 return value;
3778 /* Interpret a cast of expression EXPR to type TYPE. */
3779 tree
3780 c_cast_expr (type, expr)
3781 tree type, expr;
3783 int saved_wsp = warn_strict_prototypes;
3785 /* This avoids warnings about unprototyped casts on
3786 integers. E.g. "#define SIG_DFL (void(*)())0". */
3787 if (TREE_CODE (expr) == INTEGER_CST)
3788 warn_strict_prototypes = 0;
3789 type = groktypename (type);
3790 warn_strict_prototypes = saved_wsp;
3792 return build_c_cast (type, expr);
3796 /* Build an assignment expression of lvalue LHS from value RHS.
3797 MODIFYCODE is the code for a binary operator that we use
3798 to combine the old value of LHS with RHS to get the new value.
3799 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3801 tree
3802 build_modify_expr (lhs, modifycode, rhs)
3803 tree lhs, rhs;
3804 enum tree_code modifycode;
3806 tree result;
3807 tree newrhs;
3808 tree lhstype = TREE_TYPE (lhs);
3809 tree olhstype = lhstype;
3811 /* Types that aren't fully specified cannot be used in assignments. */
3812 lhs = require_complete_type (lhs);
3814 /* Avoid duplicate error messages from operands that had errors. */
3815 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3816 return error_mark_node;
3818 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3819 /* Do not use STRIP_NOPS here. We do not want an enumerator
3820 whose value is 0 to count as a null pointer constant. */
3821 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3822 rhs = TREE_OPERAND (rhs, 0);
3824 newrhs = rhs;
3826 /* Handle control structure constructs used as "lvalues". */
3828 switch (TREE_CODE (lhs))
3830 /* Handle (a, b) used as an "lvalue". */
3831 case COMPOUND_EXPR:
3832 pedantic_lvalue_warning (COMPOUND_EXPR);
3833 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3834 if (TREE_CODE (newrhs) == ERROR_MARK)
3835 return error_mark_node;
3836 return build (COMPOUND_EXPR, lhstype,
3837 TREE_OPERAND (lhs, 0), newrhs);
3839 /* Handle (a ? b : c) used as an "lvalue". */
3840 case COND_EXPR:
3841 pedantic_lvalue_warning (COND_EXPR);
3842 rhs = save_expr (rhs);
3844 /* Produce (a ? (b = rhs) : (c = rhs))
3845 except that the RHS goes through a save-expr
3846 so the code to compute it is only emitted once. */
3847 tree cond
3848 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3849 build_modify_expr (TREE_OPERAND (lhs, 1),
3850 modifycode, rhs),
3851 build_modify_expr (TREE_OPERAND (lhs, 2),
3852 modifycode, rhs));
3853 if (TREE_CODE (cond) == ERROR_MARK)
3854 return cond;
3855 /* Make sure the code to compute the rhs comes out
3856 before the split. */
3857 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3858 /* But cast it to void to avoid an "unused" error. */
3859 convert (void_type_node, rhs), cond);
3861 default:
3862 break;
3865 /* If a binary op has been requested, combine the old LHS value with the RHS
3866 producing the value we should actually store into the LHS. */
3868 if (modifycode != NOP_EXPR)
3870 lhs = stabilize_reference (lhs);
3871 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3874 /* Handle a cast used as an "lvalue".
3875 We have already performed any binary operator using the value as cast.
3876 Now convert the result to the cast type of the lhs,
3877 and then true type of the lhs and store it there;
3878 then convert result back to the cast type to be the value
3879 of the assignment. */
3881 switch (TREE_CODE (lhs))
3883 case NOP_EXPR:
3884 case CONVERT_EXPR:
3885 case FLOAT_EXPR:
3886 case FIX_TRUNC_EXPR:
3887 case FIX_FLOOR_EXPR:
3888 case FIX_ROUND_EXPR:
3889 case FIX_CEIL_EXPR:
3890 newrhs = default_function_array_conversion (newrhs);
3892 tree inner_lhs = TREE_OPERAND (lhs, 0);
3893 tree result;
3894 result = build_modify_expr (inner_lhs, NOP_EXPR,
3895 convert (TREE_TYPE (inner_lhs),
3896 convert (lhstype, newrhs)));
3897 if (TREE_CODE (result) == ERROR_MARK)
3898 return result;
3899 pedantic_lvalue_warning (CONVERT_EXPR);
3900 return convert (TREE_TYPE (lhs), result);
3903 default:
3904 break;
3907 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3908 Reject anything strange now. */
3910 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3911 return error_mark_node;
3913 /* Warn about storing in something that is `const'. */
3915 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3916 || ((TREE_CODE (lhstype) == RECORD_TYPE
3917 || TREE_CODE (lhstype) == UNION_TYPE)
3918 && C_TYPE_FIELDS_READONLY (lhstype)))
3919 readonly_warning (lhs, "assignment");
3921 /* If storing into a structure or union member,
3922 it has probably been given type `int'.
3923 Compute the type that would go with
3924 the actual amount of storage the member occupies. */
3926 if (TREE_CODE (lhs) == COMPONENT_REF
3927 && (TREE_CODE (lhstype) == INTEGER_TYPE
3928 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3929 || TREE_CODE (lhstype) == REAL_TYPE
3930 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3931 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3933 /* If storing in a field that is in actuality a short or narrower than one,
3934 we must store in the field in its actual type. */
3936 if (lhstype != TREE_TYPE (lhs))
3938 lhs = copy_node (lhs);
3939 TREE_TYPE (lhs) = lhstype;
3942 /* Convert new value to destination type. */
3944 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3945 NULL_TREE, NULL_TREE, 0);
3946 if (TREE_CODE (newrhs) == ERROR_MARK)
3947 return error_mark_node;
3949 /* Scan operands */
3951 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3952 TREE_SIDE_EFFECTS (result) = 1;
3954 /* If we got the LHS in a different type for storing in,
3955 convert the result back to the nominal type of LHS
3956 so that the value we return always has the same type
3957 as the LHS argument. */
3959 if (olhstype == TREE_TYPE (result))
3960 return result;
3961 return convert_for_assignment (olhstype, result, _("assignment"),
3962 NULL_TREE, NULL_TREE, 0);
3965 /* Convert value RHS to type TYPE as preparation for an assignment
3966 to an lvalue of type TYPE.
3967 The real work of conversion is done by `convert'.
3968 The purpose of this function is to generate error messages
3969 for assignments that are not allowed in C.
3970 ERRTYPE is a string to use in error messages:
3971 "assignment", "return", etc. If it is null, this is parameter passing
3972 for a function call (and different error messages are output).
3974 FUNNAME is the name of the function being called,
3975 as an IDENTIFIER_NODE, or null.
3976 PARMNUM is the number of the argument, for printing in error messages. */
3978 static tree
3979 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
3980 tree type, rhs;
3981 const char *errtype;
3982 tree fundecl, funname;
3983 int parmnum;
3985 enum tree_code codel = TREE_CODE (type);
3986 tree rhstype;
3987 enum tree_code coder;
3989 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3990 /* Do not use STRIP_NOPS here. We do not want an enumerator
3991 whose value is 0 to count as a null pointer constant. */
3992 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3993 rhs = TREE_OPERAND (rhs, 0);
3995 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3996 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3997 rhs = default_conversion (rhs);
3998 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3999 rhs = decl_constant_value_for_broken_optimization (rhs);
4001 rhstype = TREE_TYPE (rhs);
4002 coder = TREE_CODE (rhstype);
4004 if (coder == ERROR_MARK)
4005 return error_mark_node;
4007 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4009 overflow_warning (rhs);
4010 /* Check for Objective-C protocols. This will issue a warning if
4011 there are protocol violations. No need to use the return value. */
4012 maybe_objc_comptypes (type, rhstype, 0);
4013 return rhs;
4016 if (coder == VOID_TYPE)
4018 error ("void value not ignored as it ought to be");
4019 return error_mark_node;
4021 /* A type converts to a reference to it.
4022 This code doesn't fully support references, it's just for the
4023 special case of va_start and va_copy. */
4024 if (codel == REFERENCE_TYPE
4025 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4027 if (!lvalue_p (rhs))
4029 error ("cannot pass rvalue to reference parameter");
4030 return error_mark_node;
4032 if (!c_mark_addressable (rhs))
4033 return error_mark_node;
4034 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4036 /* We already know that these two types are compatible, but they
4037 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4038 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4039 likely to be va_list, a typedef to __builtin_va_list, which
4040 is different enough that it will cause problems later. */
4041 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4042 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4044 rhs = build1 (NOP_EXPR, type, rhs);
4045 return rhs;
4047 /* Arithmetic types all interconvert, and enum is treated like int. */
4048 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4049 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4050 || codel == BOOLEAN_TYPE)
4051 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4052 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4053 || coder == BOOLEAN_TYPE))
4054 return convert_and_check (type, rhs);
4056 /* Conversion to a transparent union from its member types.
4057 This applies only to function arguments. */
4058 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4060 tree memb_types;
4061 tree marginal_memb_type = 0;
4063 for (memb_types = TYPE_FIELDS (type); memb_types;
4064 memb_types = TREE_CHAIN (memb_types))
4066 tree memb_type = TREE_TYPE (memb_types);
4068 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4069 TYPE_MAIN_VARIANT (rhstype)))
4070 break;
4072 if (TREE_CODE (memb_type) != POINTER_TYPE)
4073 continue;
4075 if (coder == POINTER_TYPE)
4077 tree ttl = TREE_TYPE (memb_type);
4078 tree ttr = TREE_TYPE (rhstype);
4080 /* Any non-function converts to a [const][volatile] void *
4081 and vice versa; otherwise, targets must be the same.
4082 Meanwhile, the lhs target must have all the qualifiers of
4083 the rhs. */
4084 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4085 || comp_target_types (memb_type, rhstype))
4087 /* If this type won't generate any warnings, use it. */
4088 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4089 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4090 && TREE_CODE (ttl) == FUNCTION_TYPE)
4091 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4092 == TYPE_QUALS (ttr))
4093 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4094 == TYPE_QUALS (ttl))))
4095 break;
4097 /* Keep looking for a better type, but remember this one. */
4098 if (! marginal_memb_type)
4099 marginal_memb_type = memb_type;
4103 /* Can convert integer zero to any pointer type. */
4104 if (integer_zerop (rhs)
4105 || (TREE_CODE (rhs) == NOP_EXPR
4106 && integer_zerop (TREE_OPERAND (rhs, 0))))
4108 rhs = null_pointer_node;
4109 break;
4113 if (memb_types || marginal_memb_type)
4115 if (! memb_types)
4117 /* We have only a marginally acceptable member type;
4118 it needs a warning. */
4119 tree ttl = TREE_TYPE (marginal_memb_type);
4120 tree ttr = TREE_TYPE (rhstype);
4122 /* Const and volatile mean something different for function
4123 types, so the usual warnings are not appropriate. */
4124 if (TREE_CODE (ttr) == FUNCTION_TYPE
4125 && TREE_CODE (ttl) == FUNCTION_TYPE)
4127 /* Because const and volatile on functions are
4128 restrictions that say the function will not do
4129 certain things, it is okay to use a const or volatile
4130 function where an ordinary one is wanted, but not
4131 vice-versa. */
4132 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4133 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4134 errtype, funname, parmnum);
4136 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4137 warn_for_assignment ("%s discards qualifiers from pointer target type",
4138 errtype, funname,
4139 parmnum);
4142 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4143 pedwarn ("ISO C prohibits argument conversion to union type");
4145 return build1 (NOP_EXPR, type, rhs);
4149 /* Conversions among pointers */
4150 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4151 && (coder == codel))
4153 tree ttl = TREE_TYPE (type);
4154 tree ttr = TREE_TYPE (rhstype);
4156 /* Any non-function converts to a [const][volatile] void *
4157 and vice versa; otherwise, targets must be the same.
4158 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4159 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4160 || comp_target_types (type, rhstype)
4161 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
4162 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4164 if (pedantic
4165 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4167 (VOID_TYPE_P (ttr)
4168 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4169 which are not ANSI null ptr constants. */
4170 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4171 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4172 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4173 errtype, funname, parmnum);
4174 /* Const and volatile mean something different for function types,
4175 so the usual warnings are not appropriate. */
4176 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4177 && TREE_CODE (ttl) != FUNCTION_TYPE)
4179 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4180 warn_for_assignment ("%s discards qualifiers from pointer target type",
4181 errtype, funname, parmnum);
4182 /* If this is not a case of ignoring a mismatch in signedness,
4183 no warning. */
4184 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4185 || comp_target_types (type, rhstype))
4187 /* If there is a mismatch, do warn. */
4188 else if (pedantic)
4189 warn_for_assignment ("pointer targets in %s differ in signedness",
4190 errtype, funname, parmnum);
4192 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4193 && TREE_CODE (ttr) == FUNCTION_TYPE)
4195 /* Because const and volatile on functions are restrictions
4196 that say the function will not do certain things,
4197 it is okay to use a const or volatile function
4198 where an ordinary one is wanted, but not vice-versa. */
4199 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4200 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4201 errtype, funname, parmnum);
4204 else
4205 warn_for_assignment ("%s from incompatible pointer type",
4206 errtype, funname, parmnum);
4207 return convert (type, rhs);
4209 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4211 /* An explicit constant 0 can convert to a pointer,
4212 or one that results from arithmetic, even including
4213 a cast to integer type. */
4214 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4216 ! (TREE_CODE (rhs) == NOP_EXPR
4217 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4218 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4219 && integer_zerop (TREE_OPERAND (rhs, 0))))
4221 warn_for_assignment ("%s makes pointer from integer without a cast",
4222 errtype, funname, parmnum);
4223 return convert (type, rhs);
4225 return null_pointer_node;
4227 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4229 warn_for_assignment ("%s makes integer from pointer without a cast",
4230 errtype, funname, parmnum);
4231 return convert (type, rhs);
4233 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4234 return convert (type, rhs);
4236 if (!errtype)
4238 if (funname)
4240 tree selector = maybe_building_objc_message_expr ();
4242 if (selector && parmnum > 2)
4243 error ("incompatible type for argument %d of `%s'",
4244 parmnum - 2, IDENTIFIER_POINTER (selector));
4245 else
4246 error ("incompatible type for argument %d of `%s'",
4247 parmnum, IDENTIFIER_POINTER (funname));
4249 else
4250 error ("incompatible type for argument %d of indirect function call",
4251 parmnum);
4253 else
4254 error ("incompatible types in %s", errtype);
4256 return error_mark_node;
4259 /* Convert VALUE for assignment into inlined parameter PARM. */
4261 tree
4262 c_convert_parm_for_inlining (parm, value, fn)
4263 tree parm, value, fn;
4265 tree ret, type;
4267 /* If FN was prototyped, the value has been converted already
4268 in convert_arguments. */
4269 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4270 return value;
4272 type = TREE_TYPE (parm);
4273 ret = convert_for_assignment (type, value,
4274 (char *) 0 /* arg passing */, fn,
4275 DECL_NAME (fn), 0);
4276 if (PROMOTE_PROTOTYPES
4277 && INTEGRAL_TYPE_P (type)
4278 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4279 ret = default_conversion (ret);
4280 return ret;
4283 /* Print a warning using MSGID.
4284 It gets OPNAME as its one parameter.
4285 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4286 FUNCTION and ARGNUM are handled specially if we are building an
4287 Objective-C selector. */
4289 static void
4290 warn_for_assignment (msgid, opname, function, argnum)
4291 const char *msgid;
4292 const char *opname;
4293 tree function;
4294 int argnum;
4296 if (opname == 0)
4298 tree selector = maybe_building_objc_message_expr ();
4299 char * new_opname;
4301 if (selector && argnum > 2)
4303 function = selector;
4304 argnum -= 2;
4306 if (function)
4308 /* Function name is known; supply it. */
4309 const char *const argstring = _("passing arg %d of `%s'");
4310 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4311 + strlen (argstring) + 1 + 25
4312 /*%d*/ + 1);
4313 sprintf (new_opname, argstring, argnum,
4314 IDENTIFIER_POINTER (function));
4316 else
4318 /* Function name unknown (call through ptr); just give arg number. */
4319 const char *const argnofun = _("passing arg %d of pointer to function");
4320 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4321 sprintf (new_opname, argnofun, argnum);
4323 opname = new_opname;
4325 pedwarn (msgid, opname);
4328 /* If VALUE is a compound expr all of whose expressions are constant, then
4329 return its value. Otherwise, return error_mark_node.
4331 This is for handling COMPOUND_EXPRs as initializer elements
4332 which is allowed with a warning when -pedantic is specified. */
4334 static tree
4335 valid_compound_expr_initializer (value, endtype)
4336 tree value;
4337 tree endtype;
4339 if (TREE_CODE (value) == COMPOUND_EXPR)
4341 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4342 == error_mark_node)
4343 return error_mark_node;
4344 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4345 endtype);
4347 else if (! TREE_CONSTANT (value)
4348 && ! initializer_constant_valid_p (value, endtype))
4349 return error_mark_node;
4350 else
4351 return value;
4354 /* Perform appropriate conversions on the initial value of a variable,
4355 store it in the declaration DECL,
4356 and print any error messages that are appropriate.
4357 If the init is invalid, store an ERROR_MARK. */
4359 void
4360 store_init_value (decl, init)
4361 tree decl, init;
4363 tree value, type;
4365 /* If variable's type was invalidly declared, just ignore it. */
4367 type = TREE_TYPE (decl);
4368 if (TREE_CODE (type) == ERROR_MARK)
4369 return;
4371 /* Digest the specified initializer into an expression. */
4373 value = digest_init (type, init, TREE_STATIC (decl));
4375 /* Store the expression if valid; else report error. */
4377 #if 0
4378 /* Note that this is the only place we can detect the error
4379 in a case such as struct foo bar = (struct foo) { x, y };
4380 where there is one initial value which is a constructor expression. */
4381 if (value == error_mark_node)
4383 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4385 error ("initializer for static variable is not constant");
4386 value = error_mark_node;
4388 else if (TREE_STATIC (decl)
4389 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4391 error ("initializer for static variable uses complicated arithmetic");
4392 value = error_mark_node;
4394 else
4396 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4398 if (! TREE_CONSTANT (value))
4399 pedwarn ("aggregate initializer is not constant");
4400 else if (! TREE_STATIC (value))
4401 pedwarn ("aggregate initializer uses complicated arithmetic");
4404 #endif
4406 if (warn_traditional && !in_system_header
4407 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4408 warning ("traditional C rejects automatic aggregate initialization");
4410 DECL_INITIAL (decl) = value;
4412 /* ANSI wants warnings about out-of-range constant initializers. */
4413 STRIP_TYPE_NOPS (value);
4414 constant_expression_warning (value);
4416 /* Check if we need to set array size from compound literal size. */
4417 if (TREE_CODE (type) == ARRAY_TYPE
4418 && TYPE_DOMAIN (type) == 0
4419 && value != error_mark_node)
4421 tree inside_init = init;
4423 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4424 inside_init = TREE_OPERAND (init, 0);
4425 inside_init = fold (inside_init);
4427 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4429 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4431 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4433 /* For int foo[] = (int [3]){1}; we need to set array size
4434 now since later on array initializer will be just the
4435 brace enclosed list of the compound literal. */
4436 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4437 layout_type (type);
4438 layout_decl (decl, 0);
4444 /* Methods for storing and printing names for error messages. */
4446 /* Implement a spelling stack that allows components of a name to be pushed
4447 and popped. Each element on the stack is this structure. */
4449 struct spelling
4451 int kind;
4452 union
4454 int i;
4455 const char *s;
4456 } u;
4459 #define SPELLING_STRING 1
4460 #define SPELLING_MEMBER 2
4461 #define SPELLING_BOUNDS 3
4463 static struct spelling *spelling; /* Next stack element (unused). */
4464 static struct spelling *spelling_base; /* Spelling stack base. */
4465 static int spelling_size; /* Size of the spelling stack. */
4467 /* Macros to save and restore the spelling stack around push_... functions.
4468 Alternative to SAVE_SPELLING_STACK. */
4470 #define SPELLING_DEPTH() (spelling - spelling_base)
4471 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4473 /* Push an element on the spelling stack with type KIND and assign VALUE
4474 to MEMBER. */
4476 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4478 int depth = SPELLING_DEPTH (); \
4480 if (depth >= spelling_size) \
4482 spelling_size += 10; \
4483 if (spelling_base == 0) \
4484 spelling_base \
4485 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4486 else \
4487 spelling_base \
4488 = (struct spelling *) xrealloc (spelling_base, \
4489 spelling_size * sizeof (struct spelling)); \
4490 RESTORE_SPELLING_DEPTH (depth); \
4493 spelling->kind = (KIND); \
4494 spelling->MEMBER = (VALUE); \
4495 spelling++; \
4498 /* Push STRING on the stack. Printed literally. */
4500 static void
4501 push_string (string)
4502 const char *string;
4504 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4507 /* Push a member name on the stack. Printed as '.' STRING. */
4509 static void
4510 push_member_name (decl)
4511 tree decl;
4514 const char *const string
4515 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4516 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4519 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4521 static void
4522 push_array_bounds (bounds)
4523 int bounds;
4525 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4528 /* Compute the maximum size in bytes of the printed spelling. */
4530 static int
4531 spelling_length ()
4533 int size = 0;
4534 struct spelling *p;
4536 for (p = spelling_base; p < spelling; p++)
4538 if (p->kind == SPELLING_BOUNDS)
4539 size += 25;
4540 else
4541 size += strlen (p->u.s) + 1;
4544 return size;
4547 /* Print the spelling to BUFFER and return it. */
4549 static char *
4550 print_spelling (buffer)
4551 char *buffer;
4553 char *d = buffer;
4554 struct spelling *p;
4556 for (p = spelling_base; p < spelling; p++)
4557 if (p->kind == SPELLING_BOUNDS)
4559 sprintf (d, "[%d]", p->u.i);
4560 d += strlen (d);
4562 else
4564 const char *s;
4565 if (p->kind == SPELLING_MEMBER)
4566 *d++ = '.';
4567 for (s = p->u.s; (*d = *s++); d++)
4570 *d++ = '\0';
4571 return buffer;
4574 /* Issue an error message for a bad initializer component.
4575 MSGID identifies the message.
4576 The component name is taken from the spelling stack. */
4578 void
4579 error_init (msgid)
4580 const char *msgid;
4582 char *ofwhat;
4584 error ("%s", _(msgid));
4585 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4586 if (*ofwhat)
4587 error ("(near initialization for `%s')", ofwhat);
4590 /* Issue a pedantic warning for a bad initializer component.
4591 MSGID identifies the message.
4592 The component name is taken from the spelling stack. */
4594 void
4595 pedwarn_init (msgid)
4596 const char *msgid;
4598 char *ofwhat;
4600 pedwarn ("%s", _(msgid));
4601 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4602 if (*ofwhat)
4603 pedwarn ("(near initialization for `%s')", ofwhat);
4606 /* Issue a warning for a bad initializer component.
4607 MSGID identifies the message.
4608 The component name is taken from the spelling stack. */
4610 static void
4611 warning_init (msgid)
4612 const char *msgid;
4614 char *ofwhat;
4616 warning ("%s", _(msgid));
4617 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4618 if (*ofwhat)
4619 warning ("(near initialization for `%s')", ofwhat);
4622 /* Digest the parser output INIT as an initializer for type TYPE.
4623 Return a C expression of type TYPE to represent the initial value.
4625 REQUIRE_CONSTANT requests an error if non-constant initializers or
4626 elements are seen. */
4628 static tree
4629 digest_init (type, init, require_constant)
4630 tree type, init;
4631 int require_constant;
4633 enum tree_code code = TREE_CODE (type);
4634 tree inside_init = init;
4636 if (type == error_mark_node
4637 || init == error_mark_node
4638 || TREE_TYPE (init) == error_mark_node)
4639 return error_mark_node;
4641 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4642 /* Do not use STRIP_NOPS here. We do not want an enumerator
4643 whose value is 0 to count as a null pointer constant. */
4644 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4645 inside_init = TREE_OPERAND (init, 0);
4647 inside_init = fold (inside_init);
4649 /* Initialization of an array of chars from a string constant
4650 optionally enclosed in braces. */
4652 if (code == ARRAY_TYPE)
4654 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4655 if ((typ1 == char_type_node
4656 || typ1 == signed_char_type_node
4657 || typ1 == unsigned_char_type_node
4658 || typ1 == unsigned_wchar_type_node
4659 || typ1 == signed_wchar_type_node)
4660 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4662 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4663 TYPE_MAIN_VARIANT (type)))
4664 return inside_init;
4666 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4667 != char_type_node)
4668 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4670 error_init ("char-array initialized from wide string");
4671 return error_mark_node;
4673 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4674 == char_type_node)
4675 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4677 error_init ("int-array initialized from non-wide string");
4678 return error_mark_node;
4681 TREE_TYPE (inside_init) = type;
4682 if (TYPE_DOMAIN (type) != 0
4683 && TYPE_SIZE (type) != 0
4684 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4685 /* Subtract 1 (or sizeof (wchar_t))
4686 because it's ok to ignore the terminating null char
4687 that is counted in the length of the constant. */
4688 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4689 TREE_STRING_LENGTH (inside_init)
4690 - ((TYPE_PRECISION (typ1)
4691 != TYPE_PRECISION (char_type_node))
4692 ? (TYPE_PRECISION (wchar_type_node)
4693 / BITS_PER_UNIT)
4694 : 1)))
4695 pedwarn_init ("initializer-string for array of chars is too long");
4697 return inside_init;
4701 /* Any type can be initialized
4702 from an expression of the same type, optionally with braces. */
4704 if (inside_init && TREE_TYPE (inside_init) != 0
4705 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4706 TYPE_MAIN_VARIANT (type))
4707 || (code == ARRAY_TYPE
4708 && comptypes (TREE_TYPE (inside_init), type))
4709 || (code == VECTOR_TYPE
4710 && comptypes (TREE_TYPE (inside_init), type))
4711 || (code == POINTER_TYPE
4712 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4713 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4714 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4715 TREE_TYPE (type)))))
4717 if (code == POINTER_TYPE)
4718 inside_init = default_function_array_conversion (inside_init);
4720 if (require_constant && !flag_isoc99
4721 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4723 /* As an extension, allow initializing objects with static storage
4724 duration with compound literals (which are then treated just as
4725 the brace enclosed list they contain). */
4726 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4727 inside_init = DECL_INITIAL (decl);
4730 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4731 && TREE_CODE (inside_init) != CONSTRUCTOR)
4733 error_init ("array initialized from non-constant array expression");
4734 return error_mark_node;
4737 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4738 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4740 /* Compound expressions can only occur here if -pedantic or
4741 -pedantic-errors is specified. In the later case, we always want
4742 an error. In the former case, we simply want a warning. */
4743 if (require_constant && pedantic
4744 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4746 inside_init
4747 = valid_compound_expr_initializer (inside_init,
4748 TREE_TYPE (inside_init));
4749 if (inside_init == error_mark_node)
4750 error_init ("initializer element is not constant");
4751 else
4752 pedwarn_init ("initializer element is not constant");
4753 if (flag_pedantic_errors)
4754 inside_init = error_mark_node;
4756 else if (require_constant
4757 && (!TREE_CONSTANT (inside_init)
4758 /* This test catches things like `7 / 0' which
4759 result in an expression for which TREE_CONSTANT
4760 is true, but which is not actually something
4761 that is a legal constant. We really should not
4762 be using this function, because it is a part of
4763 the back-end. Instead, the expression should
4764 already have been turned into ERROR_MARK_NODE. */
4765 || !initializer_constant_valid_p (inside_init,
4766 TREE_TYPE (inside_init))))
4768 error_init ("initializer element is not constant");
4769 inside_init = error_mark_node;
4772 return inside_init;
4775 /* Handle scalar types, including conversions. */
4777 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4778 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4780 /* Note that convert_for_assignment calls default_conversion
4781 for arrays and functions. We must not call it in the
4782 case where inside_init is a null pointer constant. */
4783 inside_init
4784 = convert_for_assignment (type, init, _("initialization"),
4785 NULL_TREE, NULL_TREE, 0);
4787 if (require_constant && ! TREE_CONSTANT (inside_init))
4789 error_init ("initializer element is not constant");
4790 inside_init = error_mark_node;
4792 else if (require_constant
4793 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4795 error_init ("initializer element is not computable at load time");
4796 inside_init = error_mark_node;
4799 return inside_init;
4802 /* Come here only for records and arrays. */
4804 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4806 error_init ("variable-sized object may not be initialized");
4807 return error_mark_node;
4810 error_init ("invalid initializer");
4811 return error_mark_node;
4814 /* Handle initializers that use braces. */
4816 /* Type of object we are accumulating a constructor for.
4817 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4818 static tree constructor_type;
4820 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4821 left to fill. */
4822 static tree constructor_fields;
4824 /* For an ARRAY_TYPE, this is the specified index
4825 at which to store the next element we get. */
4826 static tree constructor_index;
4828 /* For an ARRAY_TYPE, this is the maximum index. */
4829 static tree constructor_max_index;
4831 /* For a RECORD_TYPE, this is the first field not yet written out. */
4832 static tree constructor_unfilled_fields;
4834 /* For an ARRAY_TYPE, this is the index of the first element
4835 not yet written out. */
4836 static tree constructor_unfilled_index;
4838 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4839 This is so we can generate gaps between fields, when appropriate. */
4840 static tree constructor_bit_index;
4842 /* If we are saving up the elements rather than allocating them,
4843 this is the list of elements so far (in reverse order,
4844 most recent first). */
4845 static tree constructor_elements;
4847 /* 1 if constructor should be incrementally stored into a constructor chain,
4848 0 if all the elements should be kept in AVL tree. */
4849 static int constructor_incremental;
4851 /* 1 if so far this constructor's elements are all compile-time constants. */
4852 static int constructor_constant;
4854 /* 1 if so far this constructor's elements are all valid address constants. */
4855 static int constructor_simple;
4857 /* 1 if this constructor is erroneous so far. */
4858 static int constructor_erroneous;
4860 /* 1 if have called defer_addressed_constants. */
4861 static int constructor_subconstants_deferred;
4863 /* Structure for managing pending initializer elements, organized as an
4864 AVL tree. */
4866 struct init_node
4868 struct init_node *left, *right;
4869 struct init_node *parent;
4870 int balance;
4871 tree purpose;
4872 tree value;
4875 /* Tree of pending elements at this constructor level.
4876 These are elements encountered out of order
4877 which belong at places we haven't reached yet in actually
4878 writing the output.
4879 Will never hold tree nodes across GC runs. */
4880 static struct init_node *constructor_pending_elts;
4882 /* The SPELLING_DEPTH of this constructor. */
4883 static int constructor_depth;
4885 /* 0 if implicitly pushing constructor levels is allowed. */
4886 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4888 static int require_constant_value;
4889 static int require_constant_elements;
4891 /* DECL node for which an initializer is being read.
4892 0 means we are reading a constructor expression
4893 such as (struct foo) {...}. */
4894 static tree constructor_decl;
4896 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4897 static const char *constructor_asmspec;
4899 /* Nonzero if this is an initializer for a top-level decl. */
4900 static int constructor_top_level;
4902 /* Nonzero if there were any member designators in this initializer. */
4903 static int constructor_designated;
4905 /* Nesting depth of designator list. */
4906 static int designator_depth;
4908 /* Nonzero if there were diagnosed errors in this designator list. */
4909 static int designator_errorneous;
4912 /* This stack has a level for each implicit or explicit level of
4913 structuring in the initializer, including the outermost one. It
4914 saves the values of most of the variables above. */
4916 struct constructor_range_stack;
4918 struct constructor_stack
4920 struct constructor_stack *next;
4921 tree type;
4922 tree fields;
4923 tree index;
4924 tree max_index;
4925 tree unfilled_index;
4926 tree unfilled_fields;
4927 tree bit_index;
4928 tree elements;
4929 struct init_node *pending_elts;
4930 int offset;
4931 int depth;
4932 /* If nonzero, this value should replace the entire
4933 constructor at this level. */
4934 tree replacement_value;
4935 struct constructor_range_stack *range_stack;
4936 char constant;
4937 char simple;
4938 char implicit;
4939 char erroneous;
4940 char outer;
4941 char incremental;
4942 char designated;
4945 struct constructor_stack *constructor_stack;
4947 /* This stack represents designators from some range designator up to
4948 the last designator in the list. */
4950 struct constructor_range_stack
4952 struct constructor_range_stack *next, *prev;
4953 struct constructor_stack *stack;
4954 tree range_start;
4955 tree index;
4956 tree range_end;
4957 tree fields;
4960 struct constructor_range_stack *constructor_range_stack;
4962 /* This stack records separate initializers that are nested.
4963 Nested initializers can't happen in ANSI C, but GNU C allows them
4964 in cases like { ... (struct foo) { ... } ... }. */
4966 struct initializer_stack
4968 struct initializer_stack *next;
4969 tree decl;
4970 const char *asmspec;
4971 struct constructor_stack *constructor_stack;
4972 struct constructor_range_stack *constructor_range_stack;
4973 tree elements;
4974 struct spelling *spelling;
4975 struct spelling *spelling_base;
4976 int spelling_size;
4977 char top_level;
4978 char require_constant_value;
4979 char require_constant_elements;
4980 char deferred;
4983 struct initializer_stack *initializer_stack;
4985 /* Prepare to parse and output the initializer for variable DECL. */
4987 void
4988 start_init (decl, asmspec_tree, top_level)
4989 tree decl;
4990 tree asmspec_tree;
4991 int top_level;
4993 const char *locus;
4994 struct initializer_stack *p
4995 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
4996 const char *asmspec = 0;
4998 if (asmspec_tree)
4999 asmspec = TREE_STRING_POINTER (asmspec_tree);
5001 p->decl = constructor_decl;
5002 p->asmspec = constructor_asmspec;
5003 p->require_constant_value = require_constant_value;
5004 p->require_constant_elements = require_constant_elements;
5005 p->constructor_stack = constructor_stack;
5006 p->constructor_range_stack = constructor_range_stack;
5007 p->elements = constructor_elements;
5008 p->spelling = spelling;
5009 p->spelling_base = spelling_base;
5010 p->spelling_size = spelling_size;
5011 p->deferred = constructor_subconstants_deferred;
5012 p->top_level = constructor_top_level;
5013 p->next = initializer_stack;
5014 initializer_stack = p;
5016 constructor_decl = decl;
5017 constructor_asmspec = asmspec;
5018 constructor_subconstants_deferred = 0;
5019 constructor_designated = 0;
5020 constructor_top_level = top_level;
5022 if (decl != 0)
5024 require_constant_value = TREE_STATIC (decl);
5025 require_constant_elements
5026 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5027 /* For a scalar, you can always use any value to initialize,
5028 even within braces. */
5029 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5030 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5031 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5032 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5033 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5035 else
5037 require_constant_value = 0;
5038 require_constant_elements = 0;
5039 locus = "(anonymous)";
5042 constructor_stack = 0;
5043 constructor_range_stack = 0;
5045 missing_braces_mentioned = 0;
5047 spelling_base = 0;
5048 spelling_size = 0;
5049 RESTORE_SPELLING_DEPTH (0);
5051 if (locus)
5052 push_string (locus);
5055 void
5056 finish_init ()
5058 struct initializer_stack *p = initializer_stack;
5060 /* Output subconstants (string constants, usually)
5061 that were referenced within this initializer and saved up.
5062 Must do this if and only if we called defer_addressed_constants. */
5063 if (constructor_subconstants_deferred)
5064 output_deferred_addressed_constants ();
5066 /* Free the whole constructor stack of this initializer. */
5067 while (constructor_stack)
5069 struct constructor_stack *q = constructor_stack;
5070 constructor_stack = q->next;
5071 free (q);
5074 if (constructor_range_stack)
5075 abort ();
5077 /* Pop back to the data of the outer initializer (if any). */
5078 constructor_decl = p->decl;
5079 constructor_asmspec = p->asmspec;
5080 require_constant_value = p->require_constant_value;
5081 require_constant_elements = p->require_constant_elements;
5082 constructor_stack = p->constructor_stack;
5083 constructor_range_stack = p->constructor_range_stack;
5084 constructor_elements = p->elements;
5085 spelling = p->spelling;
5086 spelling_base = p->spelling_base;
5087 spelling_size = p->spelling_size;
5088 constructor_subconstants_deferred = p->deferred;
5089 constructor_top_level = p->top_level;
5090 initializer_stack = p->next;
5091 free (p);
5094 /* Call here when we see the initializer is surrounded by braces.
5095 This is instead of a call to push_init_level;
5096 it is matched by a call to pop_init_level.
5098 TYPE is the type to initialize, for a constructor expression.
5099 For an initializer for a decl, TYPE is zero. */
5101 void
5102 really_start_incremental_init (type)
5103 tree type;
5105 struct constructor_stack *p
5106 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5108 if (type == 0)
5109 type = TREE_TYPE (constructor_decl);
5111 p->type = constructor_type;
5112 p->fields = constructor_fields;
5113 p->index = constructor_index;
5114 p->max_index = constructor_max_index;
5115 p->unfilled_index = constructor_unfilled_index;
5116 p->unfilled_fields = constructor_unfilled_fields;
5117 p->bit_index = constructor_bit_index;
5118 p->elements = constructor_elements;
5119 p->constant = constructor_constant;
5120 p->simple = constructor_simple;
5121 p->erroneous = constructor_erroneous;
5122 p->pending_elts = constructor_pending_elts;
5123 p->depth = constructor_depth;
5124 p->replacement_value = 0;
5125 p->implicit = 0;
5126 p->range_stack = 0;
5127 p->outer = 0;
5128 p->incremental = constructor_incremental;
5129 p->designated = constructor_designated;
5130 p->next = 0;
5131 constructor_stack = p;
5133 constructor_constant = 1;
5134 constructor_simple = 1;
5135 constructor_depth = SPELLING_DEPTH ();
5136 constructor_elements = 0;
5137 constructor_pending_elts = 0;
5138 constructor_type = type;
5139 constructor_incremental = 1;
5140 constructor_designated = 0;
5141 designator_depth = 0;
5142 designator_errorneous = 0;
5144 if (TREE_CODE (constructor_type) == RECORD_TYPE
5145 || TREE_CODE (constructor_type) == UNION_TYPE)
5147 constructor_fields = TYPE_FIELDS (constructor_type);
5148 /* Skip any nameless bit fields at the beginning. */
5149 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5150 && DECL_NAME (constructor_fields) == 0)
5151 constructor_fields = TREE_CHAIN (constructor_fields);
5153 constructor_unfilled_fields = constructor_fields;
5154 constructor_bit_index = bitsize_zero_node;
5156 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5158 if (TYPE_DOMAIN (constructor_type))
5160 constructor_max_index
5161 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5163 /* Detect non-empty initializations of zero-length arrays. */
5164 if (constructor_max_index == NULL_TREE
5165 && TYPE_SIZE (constructor_type))
5166 constructor_max_index = build_int_2 (-1, -1);
5168 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5169 to initialize VLAs will cause an proper error; avoid tree
5170 checking errors as well by setting a safe value. */
5171 if (constructor_max_index
5172 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5173 constructor_max_index = build_int_2 (-1, -1);
5175 constructor_index
5176 = convert (bitsizetype,
5177 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5179 else
5180 constructor_index = bitsize_zero_node;
5182 constructor_unfilled_index = constructor_index;
5184 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5186 /* Vectors are like simple fixed-size arrays. */
5187 constructor_max_index =
5188 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5189 constructor_index = convert (bitsizetype, bitsize_zero_node);
5190 constructor_unfilled_index = constructor_index;
5192 else
5194 /* Handle the case of int x = {5}; */
5195 constructor_fields = constructor_type;
5196 constructor_unfilled_fields = constructor_type;
5200 /* Push down into a subobject, for initialization.
5201 If this is for an explicit set of braces, IMPLICIT is 0.
5202 If it is because the next element belongs at a lower level,
5203 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5205 void
5206 push_init_level (implicit)
5207 int implicit;
5209 struct constructor_stack *p;
5210 tree value = NULL_TREE;
5212 /* If we've exhausted any levels that didn't have braces,
5213 pop them now. */
5214 while (constructor_stack->implicit)
5216 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5217 || TREE_CODE (constructor_type) == UNION_TYPE)
5218 && constructor_fields == 0)
5219 process_init_element (pop_init_level (1));
5220 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5221 && tree_int_cst_lt (constructor_max_index, constructor_index))
5222 process_init_element (pop_init_level (1));
5223 else
5224 break;
5227 /* Unless this is an explicit brace, we need to preserve previous
5228 content if any. */
5229 if (implicit)
5231 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5232 || TREE_CODE (constructor_type) == UNION_TYPE)
5233 && constructor_fields)
5234 value = find_init_member (constructor_fields);
5235 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5236 value = find_init_member (constructor_index);
5239 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5240 p->type = constructor_type;
5241 p->fields = constructor_fields;
5242 p->index = constructor_index;
5243 p->max_index = constructor_max_index;
5244 p->unfilled_index = constructor_unfilled_index;
5245 p->unfilled_fields = constructor_unfilled_fields;
5246 p->bit_index = constructor_bit_index;
5247 p->elements = constructor_elements;
5248 p->constant = constructor_constant;
5249 p->simple = constructor_simple;
5250 p->erroneous = constructor_erroneous;
5251 p->pending_elts = constructor_pending_elts;
5252 p->depth = constructor_depth;
5253 p->replacement_value = 0;
5254 p->implicit = implicit;
5255 p->outer = 0;
5256 p->incremental = constructor_incremental;
5257 p->designated = constructor_designated;
5258 p->next = constructor_stack;
5259 p->range_stack = 0;
5260 constructor_stack = p;
5262 constructor_constant = 1;
5263 constructor_simple = 1;
5264 constructor_depth = SPELLING_DEPTH ();
5265 constructor_elements = 0;
5266 constructor_incremental = 1;
5267 constructor_designated = 0;
5268 constructor_pending_elts = 0;
5269 if (!implicit)
5271 p->range_stack = constructor_range_stack;
5272 constructor_range_stack = 0;
5273 designator_depth = 0;
5274 designator_errorneous = 0;
5277 /* Don't die if an entire brace-pair level is superfluous
5278 in the containing level. */
5279 if (constructor_type == 0)
5281 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5282 || TREE_CODE (constructor_type) == UNION_TYPE)
5284 /* Don't die if there are extra init elts at the end. */
5285 if (constructor_fields == 0)
5286 constructor_type = 0;
5287 else
5289 constructor_type = TREE_TYPE (constructor_fields);
5290 push_member_name (constructor_fields);
5291 constructor_depth++;
5294 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5296 constructor_type = TREE_TYPE (constructor_type);
5297 push_array_bounds (tree_low_cst (constructor_index, 0));
5298 constructor_depth++;
5301 if (constructor_type == 0)
5303 error_init ("extra brace group at end of initializer");
5304 constructor_fields = 0;
5305 constructor_unfilled_fields = 0;
5306 return;
5309 if (value && TREE_CODE (value) == CONSTRUCTOR)
5311 constructor_constant = TREE_CONSTANT (value);
5312 constructor_simple = TREE_STATIC (value);
5313 constructor_elements = TREE_OPERAND (value, 1);
5314 if (constructor_elements
5315 && (TREE_CODE (constructor_type) == RECORD_TYPE
5316 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5317 set_nonincremental_init ();
5320 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5322 missing_braces_mentioned = 1;
5323 warning_init ("missing braces around initializer");
5326 if (TREE_CODE (constructor_type) == RECORD_TYPE
5327 || TREE_CODE (constructor_type) == UNION_TYPE)
5329 constructor_fields = TYPE_FIELDS (constructor_type);
5330 /* Skip any nameless bit fields at the beginning. */
5331 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5332 && DECL_NAME (constructor_fields) == 0)
5333 constructor_fields = TREE_CHAIN (constructor_fields);
5335 constructor_unfilled_fields = constructor_fields;
5336 constructor_bit_index = bitsize_zero_node;
5338 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5340 /* Vectors are like simple fixed-size arrays. */
5341 constructor_max_index =
5342 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5343 constructor_index = convert (bitsizetype, integer_zero_node);
5344 constructor_unfilled_index = constructor_index;
5346 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5348 if (TYPE_DOMAIN (constructor_type))
5350 constructor_max_index
5351 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5353 /* Detect non-empty initializations of zero-length arrays. */
5354 if (constructor_max_index == NULL_TREE
5355 && TYPE_SIZE (constructor_type))
5356 constructor_max_index = build_int_2 (-1, -1);
5358 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5359 to initialize VLAs will cause an proper error; avoid tree
5360 checking errors as well by setting a safe value. */
5361 if (constructor_max_index
5362 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5363 constructor_max_index = build_int_2 (-1, -1);
5365 constructor_index
5366 = convert (bitsizetype,
5367 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5369 else
5370 constructor_index = bitsize_zero_node;
5372 constructor_unfilled_index = constructor_index;
5373 if (value && TREE_CODE (value) == STRING_CST)
5375 /* We need to split the char/wchar array into individual
5376 characters, so that we don't have to special case it
5377 everywhere. */
5378 set_nonincremental_init_from_string (value);
5381 else
5383 warning_init ("braces around scalar initializer");
5384 constructor_fields = constructor_type;
5385 constructor_unfilled_fields = constructor_type;
5389 /* At the end of an implicit or explicit brace level,
5390 finish up that level of constructor.
5391 If we were outputting the elements as they are read, return 0
5392 from inner levels (process_init_element ignores that),
5393 but return error_mark_node from the outermost level
5394 (that's what we want to put in DECL_INITIAL).
5395 Otherwise, return a CONSTRUCTOR expression. */
5397 tree
5398 pop_init_level (implicit)
5399 int implicit;
5401 struct constructor_stack *p;
5402 tree constructor = 0;
5404 if (implicit == 0)
5406 /* When we come to an explicit close brace,
5407 pop any inner levels that didn't have explicit braces. */
5408 while (constructor_stack->implicit)
5409 process_init_element (pop_init_level (1));
5411 if (constructor_range_stack)
5412 abort ();
5415 p = constructor_stack;
5417 /* Error for initializing a flexible array member, or a zero-length
5418 array member in an inappropriate context. */
5419 if (constructor_type && constructor_fields
5420 && TREE_CODE (constructor_type) == ARRAY_TYPE
5421 && TYPE_DOMAIN (constructor_type)
5422 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5424 /* Silently discard empty initializations. The parser will
5425 already have pedwarned for empty brackets. */
5426 if (integer_zerop (constructor_unfilled_index))
5427 constructor_type = NULL_TREE;
5428 else if (! TYPE_SIZE (constructor_type))
5430 if (constructor_depth > 2)
5431 error_init ("initialization of flexible array member in a nested context");
5432 else if (pedantic)
5433 pedwarn_init ("initialization of a flexible array member");
5435 /* We have already issued an error message for the existence
5436 of a flexible array member not at the end of the structure.
5437 Discard the initializer so that we do not abort later. */
5438 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5439 constructor_type = NULL_TREE;
5441 else
5442 /* Zero-length arrays are no longer special, so we should no longer
5443 get here. */
5444 abort ();
5447 /* Warn when some struct elements are implicitly initialized to zero. */
5448 if (extra_warnings
5449 && constructor_type
5450 && TREE_CODE (constructor_type) == RECORD_TYPE
5451 && constructor_unfilled_fields)
5453 /* Do not warn for flexible array members or zero-length arrays. */
5454 while (constructor_unfilled_fields
5455 && (! DECL_SIZE (constructor_unfilled_fields)
5456 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5457 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5459 /* Do not warn if this level of the initializer uses member
5460 designators; it is likely to be deliberate. */
5461 if (constructor_unfilled_fields && !constructor_designated)
5463 push_member_name (constructor_unfilled_fields);
5464 warning_init ("missing initializer");
5465 RESTORE_SPELLING_DEPTH (constructor_depth);
5469 /* Now output all pending elements. */
5470 constructor_incremental = 1;
5471 output_pending_init_elements (1);
5473 /* Pad out the end of the structure. */
5474 if (p->replacement_value)
5475 /* If this closes a superfluous brace pair,
5476 just pass out the element between them. */
5477 constructor = p->replacement_value;
5478 else if (constructor_type == 0)
5480 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5481 && TREE_CODE (constructor_type) != UNION_TYPE
5482 && TREE_CODE (constructor_type) != ARRAY_TYPE
5483 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5485 /* A nonincremental scalar initializer--just return
5486 the element, after verifying there is just one. */
5487 if (constructor_elements == 0)
5489 if (!constructor_erroneous)
5490 error_init ("empty scalar initializer");
5491 constructor = error_mark_node;
5493 else if (TREE_CHAIN (constructor_elements) != 0)
5495 error_init ("extra elements in scalar initializer");
5496 constructor = TREE_VALUE (constructor_elements);
5498 else
5499 constructor = TREE_VALUE (constructor_elements);
5501 else
5503 if (constructor_erroneous)
5504 constructor = error_mark_node;
5505 else
5507 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5508 nreverse (constructor_elements));
5509 if (constructor_constant)
5510 TREE_CONSTANT (constructor) = 1;
5511 if (constructor_constant && constructor_simple)
5512 TREE_STATIC (constructor) = 1;
5516 constructor_type = p->type;
5517 constructor_fields = p->fields;
5518 constructor_index = p->index;
5519 constructor_max_index = p->max_index;
5520 constructor_unfilled_index = p->unfilled_index;
5521 constructor_unfilled_fields = p->unfilled_fields;
5522 constructor_bit_index = p->bit_index;
5523 constructor_elements = p->elements;
5524 constructor_constant = p->constant;
5525 constructor_simple = p->simple;
5526 constructor_erroneous = p->erroneous;
5527 constructor_incremental = p->incremental;
5528 constructor_designated = p->designated;
5529 constructor_pending_elts = p->pending_elts;
5530 constructor_depth = p->depth;
5531 if (!p->implicit)
5532 constructor_range_stack = p->range_stack;
5533 RESTORE_SPELLING_DEPTH (constructor_depth);
5535 constructor_stack = p->next;
5536 free (p);
5538 if (constructor == 0)
5540 if (constructor_stack == 0)
5541 return error_mark_node;
5542 return NULL_TREE;
5544 return constructor;
5547 /* Common handling for both array range and field name designators.
5548 ARRAY argument is non-zero for array ranges. Returns zero for success. */
5550 static int
5551 set_designator (array)
5552 int array;
5554 tree subtype;
5555 enum tree_code subcode;
5557 /* Don't die if an entire brace-pair level is superfluous
5558 in the containing level. */
5559 if (constructor_type == 0)
5560 return 1;
5562 /* If there were errors in this designator list already, bail out silently. */
5563 if (designator_errorneous)
5564 return 1;
5566 if (!designator_depth)
5568 if (constructor_range_stack)
5569 abort ();
5571 /* Designator list starts at the level of closest explicit
5572 braces. */
5573 while (constructor_stack->implicit)
5574 process_init_element (pop_init_level (1));
5575 constructor_designated = 1;
5576 return 0;
5579 if (constructor_no_implicit)
5581 error_init ("initialization designators may not nest");
5582 return 1;
5585 if (TREE_CODE (constructor_type) == RECORD_TYPE
5586 || TREE_CODE (constructor_type) == UNION_TYPE)
5588 subtype = TREE_TYPE (constructor_fields);
5589 if (subtype != error_mark_node)
5590 subtype = TYPE_MAIN_VARIANT (subtype);
5592 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5594 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5596 else
5597 abort ();
5599 subcode = TREE_CODE (subtype);
5600 if (array && subcode != ARRAY_TYPE)
5602 error_init ("array index in non-array initializer");
5603 return 1;
5605 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5607 error_init ("field name not in record or union initializer");
5608 return 1;
5611 constructor_designated = 1;
5612 push_init_level (2);
5613 return 0;
5616 /* If there are range designators in designator list, push a new designator
5617 to constructor_range_stack. RANGE_END is end of such stack range or
5618 NULL_TREE if there is no range designator at this level. */
5620 static void
5621 push_range_stack (range_end)
5622 tree range_end;
5624 struct constructor_range_stack *p;
5626 p = (struct constructor_range_stack *)
5627 ggc_alloc (sizeof (struct constructor_range_stack));
5628 p->prev = constructor_range_stack;
5629 p->next = 0;
5630 p->fields = constructor_fields;
5631 p->range_start = constructor_index;
5632 p->index = constructor_index;
5633 p->stack = constructor_stack;
5634 p->range_end = range_end;
5635 if (constructor_range_stack)
5636 constructor_range_stack->next = p;
5637 constructor_range_stack = p;
5640 /* Within an array initializer, specify the next index to be initialized.
5641 FIRST is that index. If LAST is nonzero, then initialize a range
5642 of indices, running from FIRST through LAST. */
5644 void
5645 set_init_index (first, last)
5646 tree first, last;
5648 if (set_designator (1))
5649 return;
5651 designator_errorneous = 1;
5653 while ((TREE_CODE (first) == NOP_EXPR
5654 || TREE_CODE (first) == CONVERT_EXPR
5655 || TREE_CODE (first) == NON_LVALUE_EXPR)
5656 && (TYPE_MODE (TREE_TYPE (first))
5657 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5658 first = TREE_OPERAND (first, 0);
5660 if (last)
5661 while ((TREE_CODE (last) == NOP_EXPR
5662 || TREE_CODE (last) == CONVERT_EXPR
5663 || TREE_CODE (last) == NON_LVALUE_EXPR)
5664 && (TYPE_MODE (TREE_TYPE (last))
5665 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5666 last = TREE_OPERAND (last, 0);
5668 if (TREE_CODE (first) != INTEGER_CST)
5669 error_init ("nonconstant array index in initializer");
5670 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5671 error_init ("nonconstant array index in initializer");
5672 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5673 error_init ("array index in non-array initializer");
5674 else if (constructor_max_index
5675 && tree_int_cst_lt (constructor_max_index, first))
5676 error_init ("array index in initializer exceeds array bounds");
5677 else
5679 constructor_index = convert (bitsizetype, first);
5681 if (last)
5683 if (tree_int_cst_equal (first, last))
5684 last = 0;
5685 else if (tree_int_cst_lt (last, first))
5687 error_init ("empty index range in initializer");
5688 last = 0;
5690 else
5692 last = convert (bitsizetype, last);
5693 if (constructor_max_index != 0
5694 && tree_int_cst_lt (constructor_max_index, last))
5696 error_init ("array index range in initializer exceeds array bounds");
5697 last = 0;
5702 designator_depth++;
5703 designator_errorneous = 0;
5704 if (constructor_range_stack || last)
5705 push_range_stack (last);
5709 /* Within a struct initializer, specify the next field to be initialized. */
5711 void
5712 set_init_label (fieldname)
5713 tree fieldname;
5715 tree tail;
5717 if (set_designator (0))
5718 return;
5720 designator_errorneous = 1;
5722 if (TREE_CODE (constructor_type) != RECORD_TYPE
5723 && TREE_CODE (constructor_type) != UNION_TYPE)
5725 error_init ("field name not in record or union initializer");
5726 return;
5729 for (tail = TYPE_FIELDS (constructor_type); tail;
5730 tail = TREE_CHAIN (tail))
5732 if (DECL_NAME (tail) == fieldname)
5733 break;
5736 if (tail == 0)
5737 error ("unknown field `%s' specified in initializer",
5738 IDENTIFIER_POINTER (fieldname));
5739 else
5741 constructor_fields = tail;
5742 designator_depth++;
5743 designator_errorneous = 0;
5744 if (constructor_range_stack)
5745 push_range_stack (NULL_TREE);
5749 /* Add a new initializer to the tree of pending initializers. PURPOSE
5750 identifies the initializer, either array index or field in a structure.
5751 VALUE is the value of that index or field. */
5753 static void
5754 add_pending_init (purpose, value)
5755 tree purpose, value;
5757 struct init_node *p, **q, *r;
5759 q = &constructor_pending_elts;
5760 p = 0;
5762 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5764 while (*q != 0)
5766 p = *q;
5767 if (tree_int_cst_lt (purpose, p->purpose))
5768 q = &p->left;
5769 else if (tree_int_cst_lt (p->purpose, purpose))
5770 q = &p->right;
5771 else
5773 if (TREE_SIDE_EFFECTS (p->value))
5774 warning_init ("initialized field with side-effects overwritten");
5775 p->value = value;
5776 return;
5780 else
5782 tree bitpos;
5784 bitpos = bit_position (purpose);
5785 while (*q != NULL)
5787 p = *q;
5788 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5789 q = &p->left;
5790 else if (p->purpose != purpose)
5791 q = &p->right;
5792 else
5794 if (TREE_SIDE_EFFECTS (p->value))
5795 warning_init ("initialized field with side-effects overwritten");
5796 p->value = value;
5797 return;
5802 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5803 r->purpose = purpose;
5804 r->value = value;
5806 *q = r;
5807 r->parent = p;
5808 r->left = 0;
5809 r->right = 0;
5810 r->balance = 0;
5812 while (p)
5814 struct init_node *s;
5816 if (r == p->left)
5818 if (p->balance == 0)
5819 p->balance = -1;
5820 else if (p->balance < 0)
5822 if (r->balance < 0)
5824 /* L rotation. */
5825 p->left = r->right;
5826 if (p->left)
5827 p->left->parent = p;
5828 r->right = p;
5830 p->balance = 0;
5831 r->balance = 0;
5833 s = p->parent;
5834 p->parent = r;
5835 r->parent = s;
5836 if (s)
5838 if (s->left == p)
5839 s->left = r;
5840 else
5841 s->right = r;
5843 else
5844 constructor_pending_elts = r;
5846 else
5848 /* LR rotation. */
5849 struct init_node *t = r->right;
5851 r->right = t->left;
5852 if (r->right)
5853 r->right->parent = r;
5854 t->left = r;
5856 p->left = t->right;
5857 if (p->left)
5858 p->left->parent = p;
5859 t->right = p;
5861 p->balance = t->balance < 0;
5862 r->balance = -(t->balance > 0);
5863 t->balance = 0;
5865 s = p->parent;
5866 p->parent = t;
5867 r->parent = t;
5868 t->parent = s;
5869 if (s)
5871 if (s->left == p)
5872 s->left = t;
5873 else
5874 s->right = t;
5876 else
5877 constructor_pending_elts = t;
5879 break;
5881 else
5883 /* p->balance == +1; growth of left side balances the node. */
5884 p->balance = 0;
5885 break;
5888 else /* r == p->right */
5890 if (p->balance == 0)
5891 /* Growth propagation from right side. */
5892 p->balance++;
5893 else if (p->balance > 0)
5895 if (r->balance > 0)
5897 /* R rotation. */
5898 p->right = r->left;
5899 if (p->right)
5900 p->right->parent = p;
5901 r->left = p;
5903 p->balance = 0;
5904 r->balance = 0;
5906 s = p->parent;
5907 p->parent = r;
5908 r->parent = s;
5909 if (s)
5911 if (s->left == p)
5912 s->left = r;
5913 else
5914 s->right = r;
5916 else
5917 constructor_pending_elts = r;
5919 else /* r->balance == -1 */
5921 /* RL rotation */
5922 struct init_node *t = r->left;
5924 r->left = t->right;
5925 if (r->left)
5926 r->left->parent = r;
5927 t->right = r;
5929 p->right = t->left;
5930 if (p->right)
5931 p->right->parent = p;
5932 t->left = p;
5934 r->balance = (t->balance < 0);
5935 p->balance = -(t->balance > 0);
5936 t->balance = 0;
5938 s = p->parent;
5939 p->parent = t;
5940 r->parent = t;
5941 t->parent = s;
5942 if (s)
5944 if (s->left == p)
5945 s->left = t;
5946 else
5947 s->right = t;
5949 else
5950 constructor_pending_elts = t;
5952 break;
5954 else
5956 /* p->balance == -1; growth of right side balances the node. */
5957 p->balance = 0;
5958 break;
5962 r = p;
5963 p = p->parent;
5967 /* Build AVL tree from a sorted chain. */
5969 static void
5970 set_nonincremental_init ()
5972 tree chain;
5974 if (TREE_CODE (constructor_type) != RECORD_TYPE
5975 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5976 return;
5978 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5979 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5980 constructor_elements = 0;
5981 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5983 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5984 /* Skip any nameless bit fields at the beginning. */
5985 while (constructor_unfilled_fields != 0
5986 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5987 && DECL_NAME (constructor_unfilled_fields) == 0)
5988 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5991 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5993 if (TYPE_DOMAIN (constructor_type))
5994 constructor_unfilled_index
5995 = convert (bitsizetype,
5996 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5997 else
5998 constructor_unfilled_index = bitsize_zero_node;
6000 constructor_incremental = 0;
6003 /* Build AVL tree from a string constant. */
6005 static void
6006 set_nonincremental_init_from_string (str)
6007 tree str;
6009 tree value, purpose, type;
6010 HOST_WIDE_INT val[2];
6011 const char *p, *end;
6012 int byte, wchar_bytes, charwidth, bitpos;
6014 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6015 abort ();
6017 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6018 == TYPE_PRECISION (char_type_node))
6019 wchar_bytes = 1;
6020 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6021 == TYPE_PRECISION (wchar_type_node))
6022 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6023 else
6024 abort ();
6026 charwidth = TYPE_PRECISION (char_type_node);
6027 type = TREE_TYPE (constructor_type);
6028 p = TREE_STRING_POINTER (str);
6029 end = p + TREE_STRING_LENGTH (str);
6031 for (purpose = bitsize_zero_node;
6032 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6033 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6035 if (wchar_bytes == 1)
6037 val[1] = (unsigned char) *p++;
6038 val[0] = 0;
6040 else
6042 val[0] = 0;
6043 val[1] = 0;
6044 for (byte = 0; byte < wchar_bytes; byte++)
6046 if (BYTES_BIG_ENDIAN)
6047 bitpos = (wchar_bytes - byte - 1) * charwidth;
6048 else
6049 bitpos = byte * charwidth;
6050 val[bitpos < HOST_BITS_PER_WIDE_INT]
6051 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6052 << (bitpos % HOST_BITS_PER_WIDE_INT);
6056 if (!TREE_UNSIGNED (type))
6058 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6059 if (bitpos < HOST_BITS_PER_WIDE_INT)
6061 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6063 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6064 val[0] = -1;
6067 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6069 if (val[1] < 0)
6070 val[0] = -1;
6072 else if (val[0] & (((HOST_WIDE_INT) 1)
6073 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6074 val[0] |= ((HOST_WIDE_INT) -1)
6075 << (bitpos - HOST_BITS_PER_WIDE_INT);
6078 value = build_int_2 (val[1], val[0]);
6079 TREE_TYPE (value) = type;
6080 add_pending_init (purpose, value);
6083 constructor_incremental = 0;
6086 /* Return value of FIELD in pending initializer or zero if the field was
6087 not initialized yet. */
6089 static tree
6090 find_init_member (field)
6091 tree field;
6093 struct init_node *p;
6095 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6097 if (constructor_incremental
6098 && tree_int_cst_lt (field, constructor_unfilled_index))
6099 set_nonincremental_init ();
6101 p = constructor_pending_elts;
6102 while (p)
6104 if (tree_int_cst_lt (field, p->purpose))
6105 p = p->left;
6106 else if (tree_int_cst_lt (p->purpose, field))
6107 p = p->right;
6108 else
6109 return p->value;
6112 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6114 tree bitpos = bit_position (field);
6116 if (constructor_incremental
6117 && (!constructor_unfilled_fields
6118 || tree_int_cst_lt (bitpos,
6119 bit_position (constructor_unfilled_fields))))
6120 set_nonincremental_init ();
6122 p = constructor_pending_elts;
6123 while (p)
6125 if (field == p->purpose)
6126 return p->value;
6127 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6128 p = p->left;
6129 else
6130 p = p->right;
6133 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6135 if (constructor_elements
6136 && TREE_PURPOSE (constructor_elements) == field)
6137 return TREE_VALUE (constructor_elements);
6139 return 0;
6142 /* "Output" the next constructor element.
6143 At top level, really output it to assembler code now.
6144 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6145 TYPE is the data type that the containing data type wants here.
6146 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6148 PENDING if non-nil means output pending elements that belong
6149 right after this element. (PENDING is normally 1;
6150 it is 0 while outputting pending elements, to avoid recursion.) */
6152 static void
6153 output_init_element (value, type, field, pending)
6154 tree value, type, field;
6155 int pending;
6157 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6158 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6159 && !(TREE_CODE (value) == STRING_CST
6160 && TREE_CODE (type) == ARRAY_TYPE
6161 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6162 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6163 TYPE_MAIN_VARIANT (type))))
6164 value = default_conversion (value);
6166 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6167 && require_constant_value && !flag_isoc99 && pending)
6169 /* As an extension, allow initializing objects with static storage
6170 duration with compound literals (which are then treated just as
6171 the brace enclosed list they contain). */
6172 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6173 value = DECL_INITIAL (decl);
6176 if (value == error_mark_node)
6177 constructor_erroneous = 1;
6178 else if (!TREE_CONSTANT (value))
6179 constructor_constant = 0;
6180 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6181 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6182 || TREE_CODE (constructor_type) == UNION_TYPE)
6183 && DECL_C_BIT_FIELD (field)
6184 && TREE_CODE (value) != INTEGER_CST))
6185 constructor_simple = 0;
6187 if (require_constant_value && ! TREE_CONSTANT (value))
6189 error_init ("initializer element is not constant");
6190 value = error_mark_node;
6192 else if (require_constant_elements
6193 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6194 pedwarn ("initializer element is not computable at load time");
6196 /* If this field is empty (and not at the end of structure),
6197 don't do anything other than checking the initializer. */
6198 if (field
6199 && (TREE_TYPE (field) == error_mark_node
6200 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6201 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6202 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6203 || TREE_CHAIN (field)))))
6204 return;
6206 value = digest_init (type, value, require_constant_value);
6207 if (value == error_mark_node)
6209 constructor_erroneous = 1;
6210 return;
6213 /* If this element doesn't come next in sequence,
6214 put it on constructor_pending_elts. */
6215 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6216 && (!constructor_incremental
6217 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6219 if (constructor_incremental
6220 && tree_int_cst_lt (field, constructor_unfilled_index))
6221 set_nonincremental_init ();
6223 add_pending_init (field, value);
6224 return;
6226 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6227 && (!constructor_incremental
6228 || field != constructor_unfilled_fields))
6230 /* We do this for records but not for unions. In a union,
6231 no matter which field is specified, it can be initialized
6232 right away since it starts at the beginning of the union. */
6233 if (constructor_incremental)
6235 if (!constructor_unfilled_fields)
6236 set_nonincremental_init ();
6237 else
6239 tree bitpos, unfillpos;
6241 bitpos = bit_position (field);
6242 unfillpos = bit_position (constructor_unfilled_fields);
6244 if (tree_int_cst_lt (bitpos, unfillpos))
6245 set_nonincremental_init ();
6249 add_pending_init (field, value);
6250 return;
6252 else if (TREE_CODE (constructor_type) == UNION_TYPE
6253 && constructor_elements)
6255 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6256 warning_init ("initialized field with side-effects overwritten");
6258 /* We can have just one union field set. */
6259 constructor_elements = 0;
6262 /* Otherwise, output this element either to
6263 constructor_elements or to the assembler file. */
6265 if (field && TREE_CODE (field) == INTEGER_CST)
6266 field = copy_node (field);
6267 constructor_elements
6268 = tree_cons (field, value, constructor_elements);
6270 /* Advance the variable that indicates sequential elements output. */
6271 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6272 constructor_unfilled_index
6273 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6274 bitsize_one_node);
6275 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6277 constructor_unfilled_fields
6278 = TREE_CHAIN (constructor_unfilled_fields);
6280 /* Skip any nameless bit fields. */
6281 while (constructor_unfilled_fields != 0
6282 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6283 && DECL_NAME (constructor_unfilled_fields) == 0)
6284 constructor_unfilled_fields =
6285 TREE_CHAIN (constructor_unfilled_fields);
6287 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6288 constructor_unfilled_fields = 0;
6290 /* Now output any pending elements which have become next. */
6291 if (pending)
6292 output_pending_init_elements (0);
6295 /* Output any pending elements which have become next.
6296 As we output elements, constructor_unfilled_{fields,index}
6297 advances, which may cause other elements to become next;
6298 if so, they too are output.
6300 If ALL is 0, we return when there are
6301 no more pending elements to output now.
6303 If ALL is 1, we output space as necessary so that
6304 we can output all the pending elements. */
6306 static void
6307 output_pending_init_elements (all)
6308 int all;
6310 struct init_node *elt = constructor_pending_elts;
6311 tree next;
6313 retry:
6315 /* Look thru the whole pending tree.
6316 If we find an element that should be output now,
6317 output it. Otherwise, set NEXT to the element
6318 that comes first among those still pending. */
6320 next = 0;
6321 while (elt)
6323 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6325 if (tree_int_cst_equal (elt->purpose,
6326 constructor_unfilled_index))
6327 output_init_element (elt->value,
6328 TREE_TYPE (constructor_type),
6329 constructor_unfilled_index, 0);
6330 else if (tree_int_cst_lt (constructor_unfilled_index,
6331 elt->purpose))
6333 /* Advance to the next smaller node. */
6334 if (elt->left)
6335 elt = elt->left;
6336 else
6338 /* We have reached the smallest node bigger than the
6339 current unfilled index. Fill the space first. */
6340 next = elt->purpose;
6341 break;
6344 else
6346 /* Advance to the next bigger node. */
6347 if (elt->right)
6348 elt = elt->right;
6349 else
6351 /* We have reached the biggest node in a subtree. Find
6352 the parent of it, which is the next bigger node. */
6353 while (elt->parent && elt->parent->right == elt)
6354 elt = elt->parent;
6355 elt = elt->parent;
6356 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6357 elt->purpose))
6359 next = elt->purpose;
6360 break;
6365 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6366 || TREE_CODE (constructor_type) == UNION_TYPE)
6368 tree ctor_unfilled_bitpos, elt_bitpos;
6370 /* If the current record is complete we are done. */
6371 if (constructor_unfilled_fields == 0)
6372 break;
6374 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6375 elt_bitpos = bit_position (elt->purpose);
6376 /* We can't compare fields here because there might be empty
6377 fields in between. */
6378 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6380 constructor_unfilled_fields = elt->purpose;
6381 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6382 elt->purpose, 0);
6384 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6386 /* Advance to the next smaller node. */
6387 if (elt->left)
6388 elt = elt->left;
6389 else
6391 /* We have reached the smallest node bigger than the
6392 current unfilled field. Fill the space first. */
6393 next = elt->purpose;
6394 break;
6397 else
6399 /* Advance to the next bigger node. */
6400 if (elt->right)
6401 elt = elt->right;
6402 else
6404 /* We have reached the biggest node in a subtree. Find
6405 the parent of it, which is the next bigger node. */
6406 while (elt->parent && elt->parent->right == elt)
6407 elt = elt->parent;
6408 elt = elt->parent;
6409 if (elt
6410 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6411 bit_position (elt->purpose))))
6413 next = elt->purpose;
6414 break;
6421 /* Ordinarily return, but not if we want to output all
6422 and there are elements left. */
6423 if (! (all && next != 0))
6424 return;
6426 /* If it's not incremental, just skip over the gap, so that after
6427 jumping to retry we will output the next successive element. */
6428 if (TREE_CODE (constructor_type) == RECORD_TYPE
6429 || TREE_CODE (constructor_type) == UNION_TYPE)
6430 constructor_unfilled_fields = next;
6431 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6432 constructor_unfilled_index = next;
6434 /* ELT now points to the node in the pending tree with the next
6435 initializer to output. */
6436 goto retry;
6439 /* Add one non-braced element to the current constructor level.
6440 This adjusts the current position within the constructor's type.
6441 This may also start or terminate implicit levels
6442 to handle a partly-braced initializer.
6444 Once this has found the correct level for the new element,
6445 it calls output_init_element. */
6447 void
6448 process_init_element (value)
6449 tree value;
6451 tree orig_value = value;
6452 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6454 designator_depth = 0;
6455 designator_errorneous = 0;
6457 /* Handle superfluous braces around string cst as in
6458 char x[] = {"foo"}; */
6459 if (string_flag
6460 && constructor_type
6461 && TREE_CODE (constructor_type) == ARRAY_TYPE
6462 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6463 && integer_zerop (constructor_unfilled_index))
6465 if (constructor_stack->replacement_value)
6466 error_init ("excess elements in char array initializer");
6467 constructor_stack->replacement_value = value;
6468 return;
6471 if (constructor_stack->replacement_value != 0)
6473 error_init ("excess elements in struct initializer");
6474 return;
6477 /* Ignore elements of a brace group if it is entirely superfluous
6478 and has already been diagnosed. */
6479 if (constructor_type == 0)
6480 return;
6482 /* If we've exhausted any levels that didn't have braces,
6483 pop them now. */
6484 while (constructor_stack->implicit)
6486 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6487 || TREE_CODE (constructor_type) == UNION_TYPE)
6488 && constructor_fields == 0)
6489 process_init_element (pop_init_level (1));
6490 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6491 && (constructor_max_index == 0
6492 || tree_int_cst_lt (constructor_max_index,
6493 constructor_index)))
6494 process_init_element (pop_init_level (1));
6495 else
6496 break;
6499 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6500 if (constructor_range_stack)
6502 /* If value is a compound literal and we'll be just using its
6503 content, don't put it into a SAVE_EXPR. */
6504 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
6505 || !require_constant_value
6506 || flag_isoc99)
6507 value = save_expr (value);
6510 while (1)
6512 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6514 tree fieldtype;
6515 enum tree_code fieldcode;
6517 if (constructor_fields == 0)
6519 pedwarn_init ("excess elements in struct initializer");
6520 break;
6523 fieldtype = TREE_TYPE (constructor_fields);
6524 if (fieldtype != error_mark_node)
6525 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6526 fieldcode = TREE_CODE (fieldtype);
6528 /* Error for non-static initialization of a flexible array member. */
6529 if (fieldcode == ARRAY_TYPE
6530 && !require_constant_value
6531 && TYPE_SIZE (fieldtype) == NULL_TREE
6532 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6534 error_init ("non-static initialization of a flexible array member");
6535 break;
6538 /* Accept a string constant to initialize a subarray. */
6539 if (value != 0
6540 && fieldcode == ARRAY_TYPE
6541 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6542 && string_flag)
6543 value = orig_value;
6544 /* Otherwise, if we have come to a subaggregate,
6545 and we don't have an element of its type, push into it. */
6546 else if (value != 0 && !constructor_no_implicit
6547 && value != error_mark_node
6548 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6549 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6550 || fieldcode == UNION_TYPE))
6552 push_init_level (1);
6553 continue;
6556 if (value)
6558 push_member_name (constructor_fields);
6559 output_init_element (value, fieldtype, constructor_fields, 1);
6560 RESTORE_SPELLING_DEPTH (constructor_depth);
6562 else
6563 /* Do the bookkeeping for an element that was
6564 directly output as a constructor. */
6566 /* For a record, keep track of end position of last field. */
6567 if (DECL_SIZE (constructor_fields))
6568 constructor_bit_index
6569 = size_binop (PLUS_EXPR,
6570 bit_position (constructor_fields),
6571 DECL_SIZE (constructor_fields));
6573 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6574 /* Skip any nameless bit fields. */
6575 while (constructor_unfilled_fields != 0
6576 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6577 && DECL_NAME (constructor_unfilled_fields) == 0)
6578 constructor_unfilled_fields =
6579 TREE_CHAIN (constructor_unfilled_fields);
6582 constructor_fields = TREE_CHAIN (constructor_fields);
6583 /* Skip any nameless bit fields at the beginning. */
6584 while (constructor_fields != 0
6585 && DECL_C_BIT_FIELD (constructor_fields)
6586 && DECL_NAME (constructor_fields) == 0)
6587 constructor_fields = TREE_CHAIN (constructor_fields);
6589 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6591 tree fieldtype;
6592 enum tree_code fieldcode;
6594 if (constructor_fields == 0)
6596 pedwarn_init ("excess elements in union initializer");
6597 break;
6600 fieldtype = TREE_TYPE (constructor_fields);
6601 if (fieldtype != error_mark_node)
6602 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6603 fieldcode = TREE_CODE (fieldtype);
6605 /* Warn that traditional C rejects initialization of unions.
6606 We skip the warning if the value is zero. This is done
6607 under the assumption that the zero initializer in user
6608 code appears conditioned on e.g. __STDC__ to avoid
6609 "missing initializer" warnings and relies on default
6610 initialization to zero in the traditional C case.
6611 We also skip the warning if the initializer is designated,
6612 again on the assumption that this must be conditional on
6613 __STDC__ anyway (and we've already complained about the
6614 member-designator already). */
6615 if (warn_traditional && !in_system_header && !constructor_designated
6616 && !(value && (integer_zerop (value) || real_zerop (value))))
6617 warning ("traditional C rejects initialization of unions");
6619 /* Accept a string constant to initialize a subarray. */
6620 if (value != 0
6621 && fieldcode == ARRAY_TYPE
6622 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6623 && string_flag)
6624 value = orig_value;
6625 /* Otherwise, if we have come to a subaggregate,
6626 and we don't have an element of its type, push into it. */
6627 else if (value != 0 && !constructor_no_implicit
6628 && value != error_mark_node
6629 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6630 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6631 || fieldcode == UNION_TYPE))
6633 push_init_level (1);
6634 continue;
6637 if (value)
6639 push_member_name (constructor_fields);
6640 output_init_element (value, fieldtype, constructor_fields, 1);
6641 RESTORE_SPELLING_DEPTH (constructor_depth);
6643 else
6644 /* Do the bookkeeping for an element that was
6645 directly output as a constructor. */
6647 constructor_bit_index = DECL_SIZE (constructor_fields);
6648 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6651 constructor_fields = 0;
6653 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6655 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6656 enum tree_code eltcode = TREE_CODE (elttype);
6658 /* Accept a string constant to initialize a subarray. */
6659 if (value != 0
6660 && eltcode == ARRAY_TYPE
6661 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6662 && string_flag)
6663 value = orig_value;
6664 /* Otherwise, if we have come to a subaggregate,
6665 and we don't have an element of its type, push into it. */
6666 else if (value != 0 && !constructor_no_implicit
6667 && value != error_mark_node
6668 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6669 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6670 || eltcode == UNION_TYPE))
6672 push_init_level (1);
6673 continue;
6676 if (constructor_max_index != 0
6677 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6678 || integer_all_onesp (constructor_max_index)))
6680 pedwarn_init ("excess elements in array initializer");
6681 break;
6684 /* Now output the actual element. */
6685 if (value)
6687 push_array_bounds (tree_low_cst (constructor_index, 0));
6688 output_init_element (value, elttype, constructor_index, 1);
6689 RESTORE_SPELLING_DEPTH (constructor_depth);
6692 constructor_index
6693 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6695 if (! value)
6696 /* If we are doing the bookkeeping for an element that was
6697 directly output as a constructor, we must update
6698 constructor_unfilled_index. */
6699 constructor_unfilled_index = constructor_index;
6701 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6703 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6705 /* Do a basic check of initializer size. Note that vectors
6706 always have a fixed size derived from their type. */
6707 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6709 pedwarn_init ("excess elements in vector initializer");
6710 break;
6713 /* Now output the actual element. */
6714 if (value)
6715 output_init_element (value, elttype, constructor_index, 1);
6717 constructor_index
6718 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6720 if (! value)
6721 /* If we are doing the bookkeeping for an element that was
6722 directly output as a constructor, we must update
6723 constructor_unfilled_index. */
6724 constructor_unfilled_index = constructor_index;
6727 /* Handle the sole element allowed in a braced initializer
6728 for a scalar variable. */
6729 else if (constructor_fields == 0)
6731 pedwarn_init ("excess elements in scalar initializer");
6732 break;
6734 else
6736 if (value)
6737 output_init_element (value, constructor_type, NULL_TREE, 1);
6738 constructor_fields = 0;
6741 /* Handle range initializers either at this level or anywhere higher
6742 in the designator stack. */
6743 if (constructor_range_stack)
6745 struct constructor_range_stack *p, *range_stack;
6746 int finish = 0;
6748 range_stack = constructor_range_stack;
6749 constructor_range_stack = 0;
6750 while (constructor_stack != range_stack->stack)
6752 if (!constructor_stack->implicit)
6753 abort ();
6754 process_init_element (pop_init_level (1));
6756 for (p = range_stack;
6757 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6758 p = p->prev)
6760 if (!constructor_stack->implicit)
6761 abort ();
6762 process_init_element (pop_init_level (1));
6765 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6766 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6767 finish = 1;
6769 while (1)
6771 constructor_index = p->index;
6772 constructor_fields = p->fields;
6773 if (finish && p->range_end && p->index == p->range_start)
6775 finish = 0;
6776 p->prev = 0;
6778 p = p->next;
6779 if (!p)
6780 break;
6781 push_init_level (2);
6782 p->stack = constructor_stack;
6783 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6784 p->index = p->range_start;
6787 if (!finish)
6788 constructor_range_stack = range_stack;
6789 continue;
6792 break;
6795 constructor_range_stack = 0;
6798 /* Build a simple asm-statement, from one string literal. */
6799 tree
6800 simple_asm_stmt (expr)
6801 tree expr;
6803 STRIP_NOPS (expr);
6805 if (TREE_CODE (expr) == ADDR_EXPR)
6806 expr = TREE_OPERAND (expr, 0);
6808 if (TREE_CODE (expr) == STRING_CST)
6810 tree stmt;
6812 stmt = add_stmt (build_stmt (ASM_STMT, NULL_TREE, expr,
6813 NULL_TREE, NULL_TREE,
6814 NULL_TREE));
6815 ASM_INPUT_P (stmt) = 1;
6816 return stmt;
6819 error ("argument of `asm' is not a constant string");
6820 return NULL_TREE;
6823 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6824 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6826 tree
6827 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6828 tree cv_qualifier;
6829 tree string;
6830 tree outputs;
6831 tree inputs;
6832 tree clobbers;
6834 tree tail;
6836 if (TREE_CODE (string) != STRING_CST)
6838 error ("asm template is not a string constant");
6839 return NULL_TREE;
6842 if (cv_qualifier != NULL_TREE
6843 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6845 warning ("%s qualifier ignored on asm",
6846 IDENTIFIER_POINTER (cv_qualifier));
6847 cv_qualifier = NULL_TREE;
6850 /* We can remove output conversions that change the type,
6851 but not the mode. */
6852 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6854 tree output = TREE_VALUE (tail);
6856 STRIP_NOPS (output);
6857 TREE_VALUE (tail) = output;
6859 /* Allow conversions as LHS here. build_modify_expr as called below
6860 will do the right thing with them. */
6861 while (TREE_CODE (output) == NOP_EXPR
6862 || TREE_CODE (output) == CONVERT_EXPR
6863 || TREE_CODE (output) == FLOAT_EXPR
6864 || TREE_CODE (output) == FIX_TRUNC_EXPR
6865 || TREE_CODE (output) == FIX_FLOOR_EXPR
6866 || TREE_CODE (output) == FIX_ROUND_EXPR
6867 || TREE_CODE (output) == FIX_CEIL_EXPR)
6868 output = TREE_OPERAND (output, 0);
6870 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6873 /* Remove output conversions that change the type but not the mode. */
6874 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6876 tree output = TREE_VALUE (tail);
6877 STRIP_NOPS (output);
6878 TREE_VALUE (tail) = output;
6881 /* Perform default conversions on array and function inputs.
6882 Don't do this for other types as it would screw up operands
6883 expected to be in memory. */
6884 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6885 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6887 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6888 outputs, inputs, clobbers));
6891 /* Expand an ASM statement with operands, handling output operands
6892 that are not variables or INDIRECT_REFS by transforming such
6893 cases into cases that expand_asm_operands can handle.
6895 Arguments are same as for expand_asm_operands. */
6897 void
6898 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6899 tree string, outputs, inputs, clobbers;
6900 int vol;
6901 const char *filename;
6902 int line;
6904 int noutputs = list_length (outputs);
6905 int i;
6906 /* o[I] is the place that output number I should be written. */
6907 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6908 tree tail;
6910 /* Record the contents of OUTPUTS before it is modified. */
6911 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6912 o[i] = TREE_VALUE (tail);
6914 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6915 OUTPUTS some trees for where the values were actually stored. */
6916 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6918 /* Copy all the intermediate outputs into the specified outputs. */
6919 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6921 if (o[i] != TREE_VALUE (tail))
6923 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6924 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6925 free_temp_slots ();
6927 /* Restore the original value so that it's correct the next
6928 time we expand this function. */
6929 TREE_VALUE (tail) = o[i];
6931 /* Detect modification of read-only values.
6932 (Otherwise done by build_modify_expr.) */
6933 else
6935 tree type = TREE_TYPE (o[i]);
6936 if (TREE_READONLY (o[i])
6937 || TYPE_READONLY (type)
6938 || ((TREE_CODE (type) == RECORD_TYPE
6939 || TREE_CODE (type) == UNION_TYPE)
6940 && C_TYPE_FIELDS_READONLY (type)))
6941 readonly_warning (o[i], "modification by `asm'");
6945 /* Those MODIFY_EXPRs could do autoincrements. */
6946 emit_queue ();
6949 /* Expand a C `return' statement.
6950 RETVAL is the expression for what to return,
6951 or a null pointer for `return;' with no value. */
6953 tree
6954 c_expand_return (retval)
6955 tree retval;
6957 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6959 if (TREE_THIS_VOLATILE (current_function_decl))
6960 warning ("function declared `noreturn' has a `return' statement");
6962 if (!retval)
6964 current_function_returns_null = 1;
6965 if ((warn_return_type || flag_isoc99)
6966 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6967 pedwarn_c99 ("`return' with no value, in function returning non-void");
6969 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6971 current_function_returns_null = 1;
6972 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6973 pedwarn ("`return' with a value, in function returning void");
6975 else
6977 tree t = convert_for_assignment (valtype, retval, _("return"),
6978 NULL_TREE, NULL_TREE, 0);
6979 tree res = DECL_RESULT (current_function_decl);
6980 tree inner;
6982 current_function_returns_value = 1;
6983 if (t == error_mark_node)
6984 return NULL_TREE;
6986 inner = t = convert (TREE_TYPE (res), t);
6988 /* Strip any conversions, additions, and subtractions, and see if
6989 we are returning the address of a local variable. Warn if so. */
6990 while (1)
6992 switch (TREE_CODE (inner))
6994 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6995 case PLUS_EXPR:
6996 inner = TREE_OPERAND (inner, 0);
6997 continue;
6999 case MINUS_EXPR:
7000 /* If the second operand of the MINUS_EXPR has a pointer
7001 type (or is converted from it), this may be valid, so
7002 don't give a warning. */
7004 tree op1 = TREE_OPERAND (inner, 1);
7006 while (! POINTER_TYPE_P (TREE_TYPE (op1))
7007 && (TREE_CODE (op1) == NOP_EXPR
7008 || TREE_CODE (op1) == NON_LVALUE_EXPR
7009 || TREE_CODE (op1) == CONVERT_EXPR))
7010 op1 = TREE_OPERAND (op1, 0);
7012 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7013 break;
7015 inner = TREE_OPERAND (inner, 0);
7016 continue;
7019 case ADDR_EXPR:
7020 inner = TREE_OPERAND (inner, 0);
7022 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7023 inner = TREE_OPERAND (inner, 0);
7025 if (TREE_CODE (inner) == VAR_DECL
7026 && ! DECL_EXTERNAL (inner)
7027 && ! TREE_STATIC (inner)
7028 && DECL_CONTEXT (inner) == current_function_decl)
7029 warning ("function returns address of local variable");
7030 break;
7032 default:
7033 break;
7036 break;
7039 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7042 return add_stmt (build_return_stmt (retval));
7045 struct c_switch {
7046 /* The SWITCH_STMT being built. */
7047 tree switch_stmt;
7048 /* A splay-tree mapping the low element of a case range to the high
7049 element, or NULL_TREE if there is no high element. Used to
7050 determine whether or not a new case label duplicates an old case
7051 label. We need a tree, rather than simply a hash table, because
7052 of the GNU case range extension. */
7053 splay_tree cases;
7054 /* The next node on the stack. */
7055 struct c_switch *next;
7058 /* A stack of the currently active switch statements. The innermost
7059 switch statement is on the top of the stack. There is no need to
7060 mark the stack for garbage collection because it is only active
7061 during the processing of the body of a function, and we never
7062 collect at that point. */
7064 static struct c_switch *switch_stack;
7066 /* Start a C switch statement, testing expression EXP. Return the new
7067 SWITCH_STMT. */
7069 tree
7070 c_start_case (exp)
7071 tree exp;
7073 enum tree_code code;
7074 tree type, orig_type = error_mark_node;
7075 struct c_switch *cs;
7077 if (exp != error_mark_node)
7079 code = TREE_CODE (TREE_TYPE (exp));
7080 orig_type = TREE_TYPE (exp);
7082 if (! INTEGRAL_TYPE_P (orig_type)
7083 && code != ERROR_MARK)
7085 error ("switch quantity not an integer");
7086 exp = integer_zero_node;
7088 else
7090 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7092 if (warn_traditional && !in_system_header
7093 && (type == long_integer_type_node
7094 || type == long_unsigned_type_node))
7095 warning ("`long' switch expression not converted to `int' in ISO C");
7097 exp = default_conversion (exp);
7098 type = TREE_TYPE (exp);
7102 /* Add this new SWITCH_STMT to the stack. */
7103 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7104 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
7105 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7106 cs->next = switch_stack;
7107 switch_stack = cs;
7109 return add_stmt (switch_stack->switch_stmt);
7112 /* Process a case label. */
7114 tree
7115 do_case (low_value, high_value)
7116 tree low_value;
7117 tree high_value;
7119 tree label = NULL_TREE;
7121 if (switch_stack)
7123 label = c_add_case_label (switch_stack->cases,
7124 SWITCH_COND (switch_stack->switch_stmt),
7125 low_value, high_value);
7126 if (label == error_mark_node)
7127 label = NULL_TREE;
7129 else if (low_value)
7130 error ("case label not within a switch statement");
7131 else
7132 error ("`default' label not within a switch statement");
7134 return label;
7137 /* Finish the switch statement. */
7139 void
7140 c_finish_case ()
7142 struct c_switch *cs = switch_stack;
7144 RECHAIN_STMTS (cs->switch_stmt, SWITCH_BODY (cs->switch_stmt));
7146 /* Pop the stack. */
7147 switch_stack = switch_stack->next;
7148 splay_tree_delete (cs->cases);
7149 free (cs);