PR target/16286
[official-gcc.git] / gcc / c-typeck.c
blobcf883b7d545040a0745948115232d27994eed1bc
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 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 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
47 /* Places where an lvalue, or modifiable lvalue, may be required.
48 Used to select diagnostic messages in lvalue_or_else and
49 readonly_error. */
50 enum lvalue_use {
51 lv_assign,
52 lv_increment,
53 lv_decrement,
54 lv_addressof,
55 lv_asm
58 /* Possible cases of implicit bad conversions. Used to select
59 diagnostic messages in convert_for_assignment. */
60 enum impl_conv {
61 ic_argpass,
62 ic_argpass_nonproto,
63 ic_assign,
64 ic_init,
65 ic_return
68 /* The level of nesting inside "__alignof__". */
69 int in_alignof;
71 /* The level of nesting inside "sizeof". */
72 int in_sizeof;
74 /* The level of nesting inside "typeof". */
75 int in_typeof;
77 /* Nonzero if we've already printed a "missing braces around initializer"
78 message within this initializer. */
79 static int missing_braces_mentioned;
81 static int require_constant_value;
82 static int require_constant_elements;
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (tree, tree);
86 static int comp_target_types (tree, tree, int);
87 static int function_types_compatible_p (tree, tree);
88 static int type_lists_compatible_p (tree, tree);
89 static tree decl_constant_value_for_broken_optimization (tree);
90 static tree default_function_array_conversion (tree);
91 static tree lookup_field (tree, tree);
92 static tree convert_arguments (tree, tree, tree, tree);
93 static tree pointer_diff (tree, tree);
94 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
95 int);
96 static tree valid_compound_expr_initializer (tree, tree);
97 static void push_string (const char *);
98 static void push_member_name (tree);
99 static void push_array_bounds (int);
100 static int spelling_length (void);
101 static char *print_spelling (char *);
102 static void warning_init (const char *);
103 static tree digest_init (tree, tree, bool, int);
104 static void output_init_element (tree, bool, tree, tree, int);
105 static void output_pending_init_elements (int);
106 static int set_designator (int);
107 static void push_range_stack (tree);
108 static void add_pending_init (tree, tree);
109 static void set_nonincremental_init (void);
110 static void set_nonincremental_init_from_string (tree);
111 static tree find_init_member (tree);
112 static int lvalue_or_else (tree, enum lvalue_use);
113 static void readonly_error (tree, enum lvalue_use);
115 /* Do `exp = require_complete_type (exp);' to make sure exp
116 does not have an incomplete type. (That includes void types.) */
118 tree
119 require_complete_type (tree value)
121 tree type = TREE_TYPE (value);
123 if (value == error_mark_node || type == error_mark_node)
124 return error_mark_node;
126 /* First, detect a valid value with a complete type. */
127 if (COMPLETE_TYPE_P (type))
128 return value;
130 c_incomplete_type_error (value, type);
131 return error_mark_node;
134 /* Print an error message for invalid use of an incomplete type.
135 VALUE is the expression that was used (or 0 if that isn't known)
136 and TYPE is the type that was invalid. */
138 void
139 c_incomplete_type_error (tree value, tree type)
141 const char *type_code_string;
143 /* Avoid duplicate error message. */
144 if (TREE_CODE (type) == ERROR_MARK)
145 return;
147 if (value != 0 && (TREE_CODE (value) == VAR_DECL
148 || TREE_CODE (value) == PARM_DECL))
149 error ("%qs has an incomplete type",
150 IDENTIFIER_POINTER (DECL_NAME (value)));
151 else
153 retry:
154 /* We must print an error message. Be clever about what it says. */
156 switch (TREE_CODE (type))
158 case RECORD_TYPE:
159 type_code_string = "struct";
160 break;
162 case UNION_TYPE:
163 type_code_string = "union";
164 break;
166 case ENUMERAL_TYPE:
167 type_code_string = "enum";
168 break;
170 case VOID_TYPE:
171 error ("invalid use of void expression");
172 return;
174 case ARRAY_TYPE:
175 if (TYPE_DOMAIN (type))
177 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
179 error ("invalid use of flexible array member");
180 return;
182 type = TREE_TYPE (type);
183 goto retry;
185 error ("invalid use of array with unspecified bounds");
186 return;
188 default:
189 gcc_unreachable ();
192 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
193 error ("invalid use of undefined type %<%s %s%>",
194 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
195 else
196 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
197 error ("invalid use of incomplete typedef %qs",
198 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
202 /* Given a type, apply default promotions wrt unnamed function
203 arguments and return the new type. */
205 tree
206 c_type_promotes_to (tree type)
208 if (TYPE_MAIN_VARIANT (type) == float_type_node)
209 return double_type_node;
211 if (c_promoting_integer_type_p (type))
213 /* Preserve unsignedness if not really getting any wider. */
214 if (TYPE_UNSIGNED (type)
215 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
216 return unsigned_type_node;
217 return integer_type_node;
220 return type;
223 /* Return a variant of TYPE which has all the type qualifiers of LIKE
224 as well as those of TYPE. */
226 static tree
227 qualify_type (tree type, tree like)
229 return c_build_qualified_type (type,
230 TYPE_QUALS (type) | TYPE_QUALS (like));
233 /* Return the composite type of two compatible types.
235 We assume that comptypes has already been done and returned
236 nonzero; if that isn't so, this may crash. In particular, we
237 assume that qualifiers match. */
239 tree
240 composite_type (tree t1, tree t2)
242 enum tree_code code1;
243 enum tree_code code2;
244 tree attributes;
246 /* Save time if the two types are the same. */
248 if (t1 == t2) return t1;
250 /* If one type is nonsense, use the other. */
251 if (t1 == error_mark_node)
252 return t2;
253 if (t2 == error_mark_node)
254 return t1;
256 code1 = TREE_CODE (t1);
257 code2 = TREE_CODE (t2);
259 /* Merge the attributes. */
260 attributes = targetm.merge_type_attributes (t1, t2);
262 /* If one is an enumerated type and the other is the compatible
263 integer type, the composite type might be either of the two
264 (DR#013 question 3). For consistency, use the enumerated type as
265 the composite type. */
267 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
268 return t1;
269 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
270 return t2;
272 gcc_assert (code1 == code2);
274 switch (code1)
276 case POINTER_TYPE:
277 /* For two pointers, do this recursively on the target type. */
279 tree pointed_to_1 = TREE_TYPE (t1);
280 tree pointed_to_2 = TREE_TYPE (t2);
281 tree target = composite_type (pointed_to_1, pointed_to_2);
282 t1 = build_pointer_type (target);
283 t1 = build_type_attribute_variant (t1, attributes);
284 return qualify_type (t1, t2);
287 case ARRAY_TYPE:
289 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
291 /* We should not have any type quals on arrays at all. */
292 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
294 /* Save space: see if the result is identical to one of the args. */
295 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
296 return build_type_attribute_variant (t1, attributes);
297 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
298 return build_type_attribute_variant (t2, attributes);
300 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
301 return build_type_attribute_variant (t1, attributes);
302 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
303 return build_type_attribute_variant (t2, attributes);
305 /* Merge the element types, and have a size if either arg has one. */
306 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
307 return build_type_attribute_variant (t1, attributes);
310 case FUNCTION_TYPE:
311 /* Function types: prefer the one that specified arg types.
312 If both do, merge the arg types. Also merge the return types. */
314 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
315 tree p1 = TYPE_ARG_TYPES (t1);
316 tree p2 = TYPE_ARG_TYPES (t2);
317 int len;
318 tree newargs, n;
319 int i;
321 /* Save space: see if the result is identical to one of the args. */
322 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
323 return build_type_attribute_variant (t1, attributes);
324 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
325 return build_type_attribute_variant (t2, attributes);
327 /* Simple way if one arg fails to specify argument types. */
328 if (TYPE_ARG_TYPES (t1) == 0)
330 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
331 t1 = build_type_attribute_variant (t1, attributes);
332 return qualify_type (t1, t2);
334 if (TYPE_ARG_TYPES (t2) == 0)
336 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
337 t1 = build_type_attribute_variant (t1, attributes);
338 return qualify_type (t1, t2);
341 /* If both args specify argument types, we must merge the two
342 lists, argument by argument. */
343 /* Tell global_bindings_p to return false so that variable_size
344 doesn't abort on VLAs in parameter types. */
345 c_override_global_bindings_to_false = true;
347 len = list_length (p1);
348 newargs = 0;
350 for (i = 0; i < len; i++)
351 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
353 n = newargs;
355 for (; p1;
356 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
358 /* A null type means arg type is not specified.
359 Take whatever the other function type has. */
360 if (TREE_VALUE (p1) == 0)
362 TREE_VALUE (n) = TREE_VALUE (p2);
363 goto parm_done;
365 if (TREE_VALUE (p2) == 0)
367 TREE_VALUE (n) = TREE_VALUE (p1);
368 goto parm_done;
371 /* Given wait (union {union wait *u; int *i} *)
372 and wait (union wait *),
373 prefer union wait * as type of parm. */
374 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
375 && TREE_VALUE (p1) != TREE_VALUE (p2))
377 tree memb;
378 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
379 memb; memb = TREE_CHAIN (memb))
380 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
382 TREE_VALUE (n) = TREE_VALUE (p2);
383 if (pedantic)
384 pedwarn ("function types not truly compatible in ISO C");
385 goto parm_done;
388 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
389 && TREE_VALUE (p2) != TREE_VALUE (p1))
391 tree memb;
392 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
393 memb; memb = TREE_CHAIN (memb))
394 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
396 TREE_VALUE (n) = TREE_VALUE (p1);
397 if (pedantic)
398 pedwarn ("function types not truly compatible in ISO C");
399 goto parm_done;
402 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
403 parm_done: ;
406 c_override_global_bindings_to_false = false;
407 t1 = build_function_type (valtype, newargs);
408 t1 = qualify_type (t1, t2);
409 /* ... falls through ... */
412 default:
413 return build_type_attribute_variant (t1, attributes);
418 /* Return the type of a conditional expression between pointers to
419 possibly differently qualified versions of compatible types.
421 We assume that comp_target_types has already been done and returned
422 nonzero; if that isn't so, this may crash. */
424 static tree
425 common_pointer_type (tree t1, tree t2)
427 tree attributes;
428 tree pointed_to_1;
429 tree pointed_to_2;
430 tree target;
432 /* Save time if the two types are the same. */
434 if (t1 == t2) return t1;
436 /* If one type is nonsense, use the other. */
437 if (t1 == error_mark_node)
438 return t2;
439 if (t2 == error_mark_node)
440 return t1;
442 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
443 && TREE_CODE (t2) == POINTER_TYPE);
445 /* Merge the attributes. */
446 attributes = targetm.merge_type_attributes (t1, t2);
448 /* Find the composite type of the target types, and combine the
449 qualifiers of the two types' targets. */
450 pointed_to_1 = TREE_TYPE (t1);
451 pointed_to_2 = TREE_TYPE (t2);
452 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
453 TYPE_MAIN_VARIANT (pointed_to_2));
454 t1 = build_pointer_type (c_build_qualified_type
455 (target,
456 TYPE_QUALS (pointed_to_1) |
457 TYPE_QUALS (pointed_to_2)));
458 return build_type_attribute_variant (t1, attributes);
461 /* Return the common type for two arithmetic types under the usual
462 arithmetic conversions. The default conversions have already been
463 applied, and enumerated types converted to their compatible integer
464 types. The resulting type is unqualified and has no attributes.
466 This is the type for the result of most arithmetic operations
467 if the operands have the given two types. */
469 tree
470 common_type (tree t1, tree t2)
472 enum tree_code code1;
473 enum tree_code code2;
475 /* If one type is nonsense, use the other. */
476 if (t1 == error_mark_node)
477 return t2;
478 if (t2 == error_mark_node)
479 return t1;
481 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
482 t1 = TYPE_MAIN_VARIANT (t1);
484 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
485 t2 = TYPE_MAIN_VARIANT (t2);
487 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
488 t1 = build_type_attribute_variant (t1, NULL_TREE);
490 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
491 t2 = build_type_attribute_variant (t2, NULL_TREE);
493 /* Save time if the two types are the same. */
495 if (t1 == t2) return t1;
497 code1 = TREE_CODE (t1);
498 code2 = TREE_CODE (t2);
500 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
501 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
502 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
503 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
505 /* If one type is a vector type, return that type. (How the usual
506 arithmetic conversions apply to the vector types extension is not
507 precisely specified.) */
508 if (code1 == VECTOR_TYPE)
509 return t1;
511 if (code2 == VECTOR_TYPE)
512 return t2;
514 /* If one type is complex, form the common type of the non-complex
515 components, then make that complex. Use T1 or T2 if it is the
516 required type. */
517 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
519 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
520 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
521 tree subtype = common_type (subtype1, subtype2);
523 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
524 return t1;
525 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
526 return t2;
527 else
528 return build_complex_type (subtype);
531 /* If only one is real, use it as the result. */
533 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
534 return t1;
536 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
537 return t2;
539 /* Both real or both integers; use the one with greater precision. */
541 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
542 return t1;
543 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
544 return t2;
546 /* Same precision. Prefer long longs to longs to ints when the
547 same precision, following the C99 rules on integer type rank
548 (which are equivalent to the C90 rules for C90 types). */
550 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
551 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
552 return long_long_unsigned_type_node;
554 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
555 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
557 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
558 return long_long_unsigned_type_node;
559 else
560 return long_long_integer_type_node;
563 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
564 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
565 return long_unsigned_type_node;
567 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
568 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
570 /* But preserve unsignedness from the other type,
571 since long cannot hold all the values of an unsigned int. */
572 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
573 return long_unsigned_type_node;
574 else
575 return long_integer_type_node;
578 /* Likewise, prefer long double to double even if same size. */
579 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
580 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
581 return long_double_type_node;
583 /* Otherwise prefer the unsigned one. */
585 if (TYPE_UNSIGNED (t1))
586 return t1;
587 else
588 return t2;
591 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
592 or various other operations. Return 2 if they are compatible
593 but a warning may be needed if you use them together. */
596 comptypes (tree type1, tree type2)
598 tree t1 = type1;
599 tree t2 = type2;
600 int attrval, val;
602 /* Suppress errors caused by previously reported errors. */
604 if (t1 == t2 || !t1 || !t2
605 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
606 return 1;
608 /* If either type is the internal version of sizetype, return the
609 language version. */
610 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
611 && TYPE_ORIG_SIZE_TYPE (t1))
612 t1 = TYPE_ORIG_SIZE_TYPE (t1);
614 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
615 && TYPE_ORIG_SIZE_TYPE (t2))
616 t2 = TYPE_ORIG_SIZE_TYPE (t2);
619 /* Enumerated types are compatible with integer types, but this is
620 not transitive: two enumerated types in the same translation unit
621 are compatible with each other only if they are the same type. */
623 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
624 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
625 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
626 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
628 if (t1 == t2)
629 return 1;
631 /* Different classes of types can't be compatible. */
633 if (TREE_CODE (t1) != TREE_CODE (t2))
634 return 0;
636 /* Qualifiers must match. C99 6.7.3p9 */
638 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
639 return 0;
641 /* Allow for two different type nodes which have essentially the same
642 definition. Note that we already checked for equality of the type
643 qualifiers (just above). */
645 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
646 return 1;
648 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
649 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
650 return 0;
652 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
653 val = 0;
655 switch (TREE_CODE (t1))
657 case POINTER_TYPE:
658 /* We must give ObjC the first crack at comparing pointers, since
659 protocol qualifiers may be involved. */
660 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
661 break;
662 /* Do not remove mode or aliasing information. */
663 if (TYPE_MODE (t1) != TYPE_MODE (t2)
664 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
665 break;
666 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
667 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
668 break;
670 case FUNCTION_TYPE:
671 val = function_types_compatible_p (t1, t2);
672 break;
674 case ARRAY_TYPE:
676 tree d1 = TYPE_DOMAIN (t1);
677 tree d2 = TYPE_DOMAIN (t2);
678 bool d1_variable, d2_variable;
679 bool d1_zero, d2_zero;
680 val = 1;
682 /* Target types must match incl. qualifiers. */
683 if (TREE_TYPE (t1) != TREE_TYPE (t2)
684 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
685 return 0;
687 /* Sizes must match unless one is missing or variable. */
688 if (d1 == 0 || d2 == 0 || d1 == d2)
689 break;
691 d1_zero = !TYPE_MAX_VALUE (d1);
692 d2_zero = !TYPE_MAX_VALUE (d2);
694 d1_variable = (!d1_zero
695 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
696 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
697 d2_variable = (!d2_zero
698 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
699 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
701 if (d1_variable || d2_variable)
702 break;
703 if (d1_zero && d2_zero)
704 break;
705 if (d1_zero || d2_zero
706 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
707 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
708 val = 0;
710 break;
713 case RECORD_TYPE:
714 /* We are dealing with two distinct structs. In assorted Objective-C
715 corner cases, however, these can still be deemed equivalent. */
716 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
717 val = 1;
719 case ENUMERAL_TYPE:
720 case UNION_TYPE:
721 if (val != 1 && !same_translation_unit_p (t1, t2))
722 val = tagged_types_tu_compatible_p (t1, t2);
723 break;
725 case VECTOR_TYPE:
726 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
727 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
728 break;
730 default:
731 break;
733 return attrval == 2 && val == 1 ? 2 : val;
736 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
737 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
738 to 1 or 0 depending if the check of the pointer types is meant to
739 be reflexive or not (typically, assignments are not reflexive,
740 while comparisons are reflexive).
743 static int
744 comp_target_types (tree ttl, tree ttr, int reflexive)
746 int val;
748 /* Give objc_comptypes a crack at letting these types through. */
749 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
750 return val;
752 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
753 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
755 if (val == 2 && pedantic)
756 pedwarn ("types are not quite compatible");
757 return val;
760 /* Subroutines of `comptypes'. */
762 /* Determine whether two trees derive from the same translation unit.
763 If the CONTEXT chain ends in a null, that tree's context is still
764 being parsed, so if two trees have context chains ending in null,
765 they're in the same translation unit. */
767 same_translation_unit_p (tree t1, tree t2)
769 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
770 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
772 case tcc_declaration:
773 t1 = DECL_CONTEXT (t1); break;
774 case tcc_type:
775 t1 = TYPE_CONTEXT (t1); break;
776 case tcc_exceptional:
777 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
778 default: gcc_unreachable ();
781 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
782 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
784 case tcc_declaration:
785 t2 = DECL_CONTEXT (t2); break;
786 case tcc_type:
787 t2 = TYPE_CONTEXT (t2); break;
788 case tcc_exceptional:
789 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
790 default: gcc_unreachable ();
793 return t1 == t2;
796 /* The C standard says that two structures in different translation
797 units are compatible with each other only if the types of their
798 fields are compatible (among other things). So, consider two copies
799 of this structure: */
801 struct tagged_tu_seen {
802 const struct tagged_tu_seen * next;
803 tree t1;
804 tree t2;
807 /* Can they be compatible with each other? We choose to break the
808 recursion by allowing those types to be compatible. */
810 static const struct tagged_tu_seen * tagged_tu_seen_base;
812 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
813 compatible. If the two types are not the same (which has been
814 checked earlier), this can only happen when multiple translation
815 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
816 rules. */
818 static int
819 tagged_types_tu_compatible_p (tree t1, tree t2)
821 tree s1, s2;
822 bool needs_warning = false;
824 /* We have to verify that the tags of the types are the same. This
825 is harder than it looks because this may be a typedef, so we have
826 to go look at the original type. It may even be a typedef of a
827 typedef...
828 In the case of compiler-created builtin structs the TYPE_DECL
829 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
830 while (TYPE_NAME (t1)
831 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
832 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
833 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
835 while (TYPE_NAME (t2)
836 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
837 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
838 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
840 /* C90 didn't have the requirement that the two tags be the same. */
841 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
842 return 0;
844 /* C90 didn't say what happened if one or both of the types were
845 incomplete; we choose to follow C99 rules here, which is that they
846 are compatible. */
847 if (TYPE_SIZE (t1) == NULL
848 || TYPE_SIZE (t2) == NULL)
849 return 1;
852 const struct tagged_tu_seen * tts_i;
853 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
854 if (tts_i->t1 == t1 && tts_i->t2 == t2)
855 return 1;
858 switch (TREE_CODE (t1))
860 case ENUMERAL_TYPE:
863 /* Speed up the case where the type values are in the same order. */
864 tree tv1 = TYPE_VALUES (t1);
865 tree tv2 = TYPE_VALUES (t2);
867 if (tv1 == tv2)
868 return 1;
870 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
872 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
873 break;
874 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
875 return 0;
878 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
879 return 1;
880 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
881 return 0;
883 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
884 return 0;
886 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
888 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
889 if (s2 == NULL
890 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
891 return 0;
893 return 1;
896 case UNION_TYPE:
898 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
899 return 0;
901 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
903 bool ok = false;
904 struct tagged_tu_seen tts;
906 tts.next = tagged_tu_seen_base;
907 tts.t1 = t1;
908 tts.t2 = t2;
909 tagged_tu_seen_base = &tts;
911 if (DECL_NAME (s1) != NULL)
912 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
913 if (DECL_NAME (s1) == DECL_NAME (s2))
915 int result;
916 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
917 if (result == 0)
918 break;
919 if (result == 2)
920 needs_warning = true;
922 if (TREE_CODE (s1) == FIELD_DECL
923 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
924 DECL_FIELD_BIT_OFFSET (s2)) != 1)
925 break;
927 ok = true;
928 break;
930 tagged_tu_seen_base = tts.next;
931 if (!ok)
932 return 0;
934 return needs_warning ? 2 : 1;
937 case RECORD_TYPE:
939 struct tagged_tu_seen tts;
941 tts.next = tagged_tu_seen_base;
942 tts.t1 = t1;
943 tts.t2 = t2;
944 tagged_tu_seen_base = &tts;
946 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
947 s1 && s2;
948 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
950 int result;
951 if (TREE_CODE (s1) != TREE_CODE (s2)
952 || DECL_NAME (s1) != DECL_NAME (s2))
953 break;
954 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
955 if (result == 0)
956 break;
957 if (result == 2)
958 needs_warning = true;
960 if (TREE_CODE (s1) == FIELD_DECL
961 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
962 DECL_FIELD_BIT_OFFSET (s2)) != 1)
963 break;
965 tagged_tu_seen_base = tts.next;
966 if (s1 && s2)
967 return 0;
968 return needs_warning ? 2 : 1;
971 default:
972 gcc_unreachable ();
976 /* Return 1 if two function types F1 and F2 are compatible.
977 If either type specifies no argument types,
978 the other must specify a fixed number of self-promoting arg types.
979 Otherwise, if one type specifies only the number of arguments,
980 the other must specify that number of self-promoting arg types.
981 Otherwise, the argument types must match. */
983 static int
984 function_types_compatible_p (tree f1, tree f2)
986 tree args1, args2;
987 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
988 int val = 1;
989 int val1;
990 tree ret1, ret2;
992 ret1 = TREE_TYPE (f1);
993 ret2 = TREE_TYPE (f2);
995 /* 'volatile' qualifiers on a function's return type used to mean
996 the function is noreturn. */
997 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
998 pedwarn ("function return types not compatible due to %<volatile%>");
999 if (TYPE_VOLATILE (ret1))
1000 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1001 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1002 if (TYPE_VOLATILE (ret2))
1003 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1004 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1005 val = comptypes (ret1, ret2);
1006 if (val == 0)
1007 return 0;
1009 args1 = TYPE_ARG_TYPES (f1);
1010 args2 = TYPE_ARG_TYPES (f2);
1012 /* An unspecified parmlist matches any specified parmlist
1013 whose argument types don't need default promotions. */
1015 if (args1 == 0)
1017 if (!self_promoting_args_p (args2))
1018 return 0;
1019 /* If one of these types comes from a non-prototype fn definition,
1020 compare that with the other type's arglist.
1021 If they don't match, ask for a warning (but no error). */
1022 if (TYPE_ACTUAL_ARG_TYPES (f1)
1023 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1024 val = 2;
1025 return val;
1027 if (args2 == 0)
1029 if (!self_promoting_args_p (args1))
1030 return 0;
1031 if (TYPE_ACTUAL_ARG_TYPES (f2)
1032 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1033 val = 2;
1034 return val;
1037 /* Both types have argument lists: compare them and propagate results. */
1038 val1 = type_lists_compatible_p (args1, args2);
1039 return val1 != 1 ? val1 : val;
1042 /* Check two lists of types for compatibility,
1043 returning 0 for incompatible, 1 for compatible,
1044 or 2 for compatible with warning. */
1046 static int
1047 type_lists_compatible_p (tree args1, tree args2)
1049 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1050 int val = 1;
1051 int newval = 0;
1053 while (1)
1055 if (args1 == 0 && args2 == 0)
1056 return val;
1057 /* If one list is shorter than the other,
1058 they fail to match. */
1059 if (args1 == 0 || args2 == 0)
1060 return 0;
1061 /* A null pointer instead of a type
1062 means there is supposed to be an argument
1063 but nothing is specified about what type it has.
1064 So match anything that self-promotes. */
1065 if (TREE_VALUE (args1) == 0)
1067 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1068 return 0;
1070 else if (TREE_VALUE (args2) == 0)
1072 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1073 return 0;
1075 /* If one of the lists has an error marker, ignore this arg. */
1076 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1077 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1079 else if (!(newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1080 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1082 /* Allow wait (union {union wait *u; int *i} *)
1083 and wait (union wait *) to be compatible. */
1084 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1085 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1086 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1087 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1088 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1089 TYPE_SIZE (TREE_VALUE (args2))))
1091 tree memb;
1092 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1093 memb; memb = TREE_CHAIN (memb))
1094 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1095 break;
1096 if (memb == 0)
1097 return 0;
1099 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1100 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1101 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1102 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1103 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1104 TYPE_SIZE (TREE_VALUE (args1))))
1106 tree memb;
1107 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1108 memb; memb = TREE_CHAIN (memb))
1109 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1110 break;
1111 if (memb == 0)
1112 return 0;
1114 else
1115 return 0;
1118 /* comptypes said ok, but record if it said to warn. */
1119 if (newval > val)
1120 val = newval;
1122 args1 = TREE_CHAIN (args1);
1123 args2 = TREE_CHAIN (args2);
1127 /* Compute the size to increment a pointer by. */
1129 tree
1130 c_size_in_bytes (tree type)
1132 enum tree_code code = TREE_CODE (type);
1134 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1135 return size_one_node;
1137 if (!COMPLETE_OR_VOID_TYPE_P (type))
1139 error ("arithmetic on pointer to an incomplete type");
1140 return size_one_node;
1143 /* Convert in case a char is more than one unit. */
1144 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1145 size_int (TYPE_PRECISION (char_type_node)
1146 / BITS_PER_UNIT));
1149 /* Return either DECL or its known constant value (if it has one). */
1151 tree
1152 decl_constant_value (tree decl)
1154 if (/* Don't change a variable array bound or initial value to a constant
1155 in a place where a variable is invalid. Note that DECL_INITIAL
1156 isn't valid for a PARM_DECL. */
1157 current_function_decl != 0
1158 && TREE_CODE (decl) != PARM_DECL
1159 && !TREE_THIS_VOLATILE (decl)
1160 && TREE_READONLY (decl)
1161 && DECL_INITIAL (decl) != 0
1162 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1163 /* This is invalid if initial value is not constant.
1164 If it has either a function call, a memory reference,
1165 or a variable, then re-evaluating it could give different results. */
1166 && TREE_CONSTANT (DECL_INITIAL (decl))
1167 /* Check for cases where this is sub-optimal, even though valid. */
1168 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1169 return DECL_INITIAL (decl);
1170 return decl;
1173 /* Return either DECL or its known constant value (if it has one), but
1174 return DECL if pedantic or DECL has mode BLKmode. This is for
1175 bug-compatibility with the old behavior of decl_constant_value
1176 (before GCC 3.0); every use of this function is a bug and it should
1177 be removed before GCC 3.1. It is not appropriate to use pedantic
1178 in a way that affects optimization, and BLKmode is probably not the
1179 right test for avoiding misoptimizations either. */
1181 static tree
1182 decl_constant_value_for_broken_optimization (tree decl)
1184 if (pedantic || DECL_MODE (decl) == BLKmode)
1185 return decl;
1186 else
1187 return decl_constant_value (decl);
1191 /* Perform the default conversion of arrays and functions to pointers.
1192 Return the result of converting EXP. For any other expression, just
1193 return EXP. */
1195 static tree
1196 default_function_array_conversion (tree exp)
1198 tree orig_exp;
1199 tree type = TREE_TYPE (exp);
1200 enum tree_code code = TREE_CODE (type);
1201 int not_lvalue = 0;
1203 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1204 an lvalue.
1206 Do not use STRIP_NOPS here! It will remove conversions from pointer
1207 to integer and cause infinite recursion. */
1208 orig_exp = exp;
1209 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1210 || (TREE_CODE (exp) == NOP_EXPR
1211 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1213 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1214 not_lvalue = 1;
1215 exp = TREE_OPERAND (exp, 0);
1218 if (TREE_NO_WARNING (orig_exp))
1219 TREE_NO_WARNING (exp) = 1;
1221 if (code == FUNCTION_TYPE)
1223 return build_unary_op (ADDR_EXPR, exp, 0);
1225 if (code == ARRAY_TYPE)
1227 tree adr;
1228 tree restype = TREE_TYPE (type);
1229 tree ptrtype;
1230 int constp = 0;
1231 int volatilep = 0;
1232 int lvalue_array_p;
1234 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1236 constp = TREE_READONLY (exp);
1237 volatilep = TREE_THIS_VOLATILE (exp);
1240 if (TYPE_QUALS (type) || constp || volatilep)
1241 restype
1242 = c_build_qualified_type (restype,
1243 TYPE_QUALS (type)
1244 | (constp * TYPE_QUAL_CONST)
1245 | (volatilep * TYPE_QUAL_VOLATILE));
1247 if (TREE_CODE (exp) == INDIRECT_REF)
1248 return convert (build_pointer_type (restype),
1249 TREE_OPERAND (exp, 0));
1251 if (TREE_CODE (exp) == COMPOUND_EXPR)
1253 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1254 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1255 TREE_OPERAND (exp, 0), op1);
1258 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1259 if (!flag_isoc99 && !lvalue_array_p)
1261 /* Before C99, non-lvalue arrays do not decay to pointers.
1262 Normally, using such an array would be invalid; but it can
1263 be used correctly inside sizeof or as a statement expression.
1264 Thus, do not give an error here; an error will result later. */
1265 return exp;
1268 ptrtype = build_pointer_type (restype);
1270 if (TREE_CODE (exp) == VAR_DECL)
1272 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1273 ADDR_EXPR because it's the best way of representing what
1274 happens in C when we take the address of an array and place
1275 it in a pointer to the element type. */
1276 adr = build1 (ADDR_EXPR, ptrtype, exp);
1277 if (!c_mark_addressable (exp))
1278 return error_mark_node;
1279 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1280 return adr;
1282 /* This way is better for a COMPONENT_REF since it can
1283 simplify the offset for a component. */
1284 adr = build_unary_op (ADDR_EXPR, exp, 1);
1285 return convert (ptrtype, adr);
1287 return exp;
1290 /* Perform default promotions for C data used in expressions.
1291 Arrays and functions are converted to pointers;
1292 enumeral types or short or char, to int.
1293 In addition, manifest constants symbols are replaced by their values. */
1295 tree
1296 default_conversion (tree exp)
1298 tree orig_exp;
1299 tree type = TREE_TYPE (exp);
1300 enum tree_code code = TREE_CODE (type);
1302 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1303 return default_function_array_conversion (exp);
1305 /* Constants can be used directly unless they're not loadable. */
1306 if (TREE_CODE (exp) == CONST_DECL)
1307 exp = DECL_INITIAL (exp);
1309 /* Replace a nonvolatile const static variable with its value unless
1310 it is an array, in which case we must be sure that taking the
1311 address of the array produces consistent results. */
1312 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1314 exp = decl_constant_value_for_broken_optimization (exp);
1315 type = TREE_TYPE (exp);
1318 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1319 an lvalue.
1321 Do not use STRIP_NOPS here! It will remove conversions from pointer
1322 to integer and cause infinite recursion. */
1323 orig_exp = exp;
1324 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1325 || (TREE_CODE (exp) == NOP_EXPR
1326 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1327 exp = TREE_OPERAND (exp, 0);
1329 if (TREE_NO_WARNING (orig_exp))
1330 TREE_NO_WARNING (exp) = 1;
1332 /* Normally convert enums to int,
1333 but convert wide enums to something wider. */
1334 if (code == ENUMERAL_TYPE)
1336 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1337 TYPE_PRECISION (integer_type_node)),
1338 ((TYPE_PRECISION (type)
1339 >= TYPE_PRECISION (integer_type_node))
1340 && TYPE_UNSIGNED (type)));
1342 return convert (type, exp);
1345 if (TREE_CODE (exp) == COMPONENT_REF
1346 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1347 /* If it's thinner than an int, promote it like a
1348 c_promoting_integer_type_p, otherwise leave it alone. */
1349 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1350 TYPE_PRECISION (integer_type_node)))
1351 return convert (integer_type_node, exp);
1353 if (c_promoting_integer_type_p (type))
1355 /* Preserve unsignedness if not really getting any wider. */
1356 if (TYPE_UNSIGNED (type)
1357 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1358 return convert (unsigned_type_node, exp);
1360 return convert (integer_type_node, exp);
1363 if (code == VOID_TYPE)
1365 error ("void value not ignored as it ought to be");
1366 return error_mark_node;
1368 return exp;
1371 /* Look up COMPONENT in a structure or union DECL.
1373 If the component name is not found, returns NULL_TREE. Otherwise,
1374 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1375 stepping down the chain to the component, which is in the last
1376 TREE_VALUE of the list. Normally the list is of length one, but if
1377 the component is embedded within (nested) anonymous structures or
1378 unions, the list steps down the chain to the component. */
1380 static tree
1381 lookup_field (tree decl, tree component)
1383 tree type = TREE_TYPE (decl);
1384 tree field;
1386 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1387 to the field elements. Use a binary search on this array to quickly
1388 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1389 will always be set for structures which have many elements. */
1391 if (TYPE_LANG_SPECIFIC (type))
1393 int bot, top, half;
1394 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1396 field = TYPE_FIELDS (type);
1397 bot = 0;
1398 top = TYPE_LANG_SPECIFIC (type)->s->len;
1399 while (top - bot > 1)
1401 half = (top - bot + 1) >> 1;
1402 field = field_array[bot+half];
1404 if (DECL_NAME (field) == NULL_TREE)
1406 /* Step through all anon unions in linear fashion. */
1407 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1409 field = field_array[bot++];
1410 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1411 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1413 tree anon = lookup_field (field, component);
1415 if (anon)
1416 return tree_cons (NULL_TREE, field, anon);
1420 /* Entire record is only anon unions. */
1421 if (bot > top)
1422 return NULL_TREE;
1424 /* Restart the binary search, with new lower bound. */
1425 continue;
1428 if (DECL_NAME (field) == component)
1429 break;
1430 if (DECL_NAME (field) < component)
1431 bot += half;
1432 else
1433 top = bot + half;
1436 if (DECL_NAME (field_array[bot]) == component)
1437 field = field_array[bot];
1438 else if (DECL_NAME (field) != component)
1439 return NULL_TREE;
1441 else
1443 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1445 if (DECL_NAME (field) == NULL_TREE
1446 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1447 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1449 tree anon = lookup_field (field, component);
1451 if (anon)
1452 return tree_cons (NULL_TREE, field, anon);
1455 if (DECL_NAME (field) == component)
1456 break;
1459 if (field == NULL_TREE)
1460 return NULL_TREE;
1463 return tree_cons (NULL_TREE, field, NULL_TREE);
1466 /* Make an expression to refer to the COMPONENT field of
1467 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1469 tree
1470 build_component_ref (tree datum, tree component)
1472 tree type = TREE_TYPE (datum);
1473 enum tree_code code = TREE_CODE (type);
1474 tree field = NULL;
1475 tree ref;
1477 if (!objc_is_public (datum, component))
1478 return error_mark_node;
1480 /* See if there is a field or component with name COMPONENT. */
1482 if (code == RECORD_TYPE || code == UNION_TYPE)
1484 if (!COMPLETE_TYPE_P (type))
1486 c_incomplete_type_error (NULL_TREE, type);
1487 return error_mark_node;
1490 field = lookup_field (datum, component);
1492 if (!field)
1494 error ("%qT has no member named %qs", type,
1495 IDENTIFIER_POINTER (component));
1496 return error_mark_node;
1499 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1500 This might be better solved in future the way the C++ front
1501 end does it - by giving the anonymous entities each a
1502 separate name and type, and then have build_component_ref
1503 recursively call itself. We can't do that here. */
1506 tree subdatum = TREE_VALUE (field);
1508 if (TREE_TYPE (subdatum) == error_mark_node)
1509 return error_mark_node;
1511 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1512 NULL_TREE);
1513 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1514 TREE_READONLY (ref) = 1;
1515 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1516 TREE_THIS_VOLATILE (ref) = 1;
1518 if (TREE_DEPRECATED (subdatum))
1519 warn_deprecated_use (subdatum);
1521 datum = ref;
1523 field = TREE_CHAIN (field);
1525 while (field);
1527 return ref;
1529 else if (code != ERROR_MARK)
1530 error ("request for member %qs in something not a structure or union",
1531 IDENTIFIER_POINTER (component));
1533 return error_mark_node;
1536 /* Given an expression PTR for a pointer, return an expression
1537 for the value pointed to.
1538 ERRORSTRING is the name of the operator to appear in error messages. */
1540 tree
1541 build_indirect_ref (tree ptr, const char *errorstring)
1543 tree pointer = default_conversion (ptr);
1544 tree type = TREE_TYPE (pointer);
1546 if (TREE_CODE (type) == POINTER_TYPE)
1548 if (TREE_CODE (pointer) == ADDR_EXPR
1549 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1550 == TREE_TYPE (type)))
1551 return TREE_OPERAND (pointer, 0);
1552 else
1554 tree t = TREE_TYPE (type);
1555 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1557 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1559 error ("dereferencing pointer to incomplete type");
1560 return error_mark_node;
1562 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1563 warning ("dereferencing %<void *%> pointer");
1565 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1566 so that we get the proper error message if the result is used
1567 to assign to. Also, &* is supposed to be a no-op.
1568 And ANSI C seems to specify that the type of the result
1569 should be the const type. */
1570 /* A de-reference of a pointer to const is not a const. It is valid
1571 to change it via some other pointer. */
1572 TREE_READONLY (ref) = TYPE_READONLY (t);
1573 TREE_SIDE_EFFECTS (ref)
1574 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1575 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1576 return ref;
1579 else if (TREE_CODE (pointer) != ERROR_MARK)
1580 error ("invalid type argument of %qs", errorstring);
1581 return error_mark_node;
1584 /* This handles expressions of the form "a[i]", which denotes
1585 an array reference.
1587 This is logically equivalent in C to *(a+i), but we may do it differently.
1588 If A is a variable or a member, we generate a primitive ARRAY_REF.
1589 This avoids forcing the array out of registers, and can work on
1590 arrays that are not lvalues (for example, members of structures returned
1591 by functions). */
1593 tree
1594 build_array_ref (tree array, tree index)
1596 if (index == 0)
1598 error ("subscript missing in array reference");
1599 return error_mark_node;
1602 if (TREE_TYPE (array) == error_mark_node
1603 || TREE_TYPE (index) == error_mark_node)
1604 return error_mark_node;
1606 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1608 tree rval, type;
1610 /* Subscripting with type char is likely to lose
1611 on a machine where chars are signed.
1612 So warn on any machine, but optionally.
1613 Don't warn for unsigned char since that type is safe.
1614 Don't warn for signed char because anyone who uses that
1615 must have done so deliberately. */
1616 if (warn_char_subscripts
1617 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1618 warning ("array subscript has type %<char%>");
1620 /* Apply default promotions *after* noticing character types. */
1621 index = default_conversion (index);
1623 /* Require integer *after* promotion, for sake of enums. */
1624 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1626 error ("array subscript is not an integer");
1627 return error_mark_node;
1630 /* An array that is indexed by a non-constant
1631 cannot be stored in a register; we must be able to do
1632 address arithmetic on its address.
1633 Likewise an array of elements of variable size. */
1634 if (TREE_CODE (index) != INTEGER_CST
1635 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1636 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1638 if (!c_mark_addressable (array))
1639 return error_mark_node;
1641 /* An array that is indexed by a constant value which is not within
1642 the array bounds cannot be stored in a register either; because we
1643 would get a crash in store_bit_field/extract_bit_field when trying
1644 to access a non-existent part of the register. */
1645 if (TREE_CODE (index) == INTEGER_CST
1646 && TYPE_DOMAIN (TREE_TYPE (array))
1647 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1649 if (!c_mark_addressable (array))
1650 return error_mark_node;
1653 if (pedantic)
1655 tree foo = array;
1656 while (TREE_CODE (foo) == COMPONENT_REF)
1657 foo = TREE_OPERAND (foo, 0);
1658 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1659 pedwarn ("ISO C forbids subscripting %<register%> array");
1660 else if (!flag_isoc99 && !lvalue_p (foo))
1661 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1664 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1665 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1666 /* Array ref is const/volatile if the array elements are
1667 or if the array is. */
1668 TREE_READONLY (rval)
1669 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1670 | TREE_READONLY (array));
1671 TREE_SIDE_EFFECTS (rval)
1672 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1673 | TREE_SIDE_EFFECTS (array));
1674 TREE_THIS_VOLATILE (rval)
1675 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1676 /* This was added by rms on 16 Nov 91.
1677 It fixes vol struct foo *a; a->elts[1]
1678 in an inline function.
1679 Hope it doesn't break something else. */
1680 | TREE_THIS_VOLATILE (array));
1681 return require_complete_type (fold (rval));
1685 tree ar = default_conversion (array);
1686 tree ind = default_conversion (index);
1688 /* Do the same warning check as above, but only on the part that's
1689 syntactically the index and only if it is also semantically
1690 the index. */
1691 if (warn_char_subscripts
1692 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1693 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1694 warning ("subscript has type %<char%>");
1696 /* Put the integer in IND to simplify error checking. */
1697 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1699 tree temp = ar;
1700 ar = ind;
1701 ind = temp;
1704 if (ar == error_mark_node)
1705 return ar;
1707 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1708 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1710 error ("subscripted value is neither array nor pointer");
1711 return error_mark_node;
1713 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1715 error ("array subscript is not an integer");
1716 return error_mark_node;
1719 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1720 "array indexing");
1724 /* Build an external reference to identifier ID. FUN indicates
1725 whether this will be used for a function call. */
1726 tree
1727 build_external_ref (tree id, int fun)
1729 tree ref;
1730 tree decl = lookup_name (id);
1732 /* In Objective-C, an instance variable (ivar) may be preferred to
1733 whatever lookup_name() found. */
1734 decl = objc_lookup_ivar (decl, id);
1736 if (decl && decl != error_mark_node)
1737 ref = decl;
1738 else if (fun)
1739 /* Implicit function declaration. */
1740 ref = implicitly_declare (id);
1741 else if (decl == error_mark_node)
1742 /* Don't complain about something that's already been
1743 complained about. */
1744 return error_mark_node;
1745 else
1747 undeclared_variable (id);
1748 return error_mark_node;
1751 if (TREE_TYPE (ref) == error_mark_node)
1752 return error_mark_node;
1754 if (TREE_DEPRECATED (ref))
1755 warn_deprecated_use (ref);
1757 if (!skip_evaluation)
1758 assemble_external (ref);
1759 TREE_USED (ref) = 1;
1761 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1763 if (!in_sizeof && !in_typeof)
1764 C_DECL_USED (ref) = 1;
1765 else if (DECL_INITIAL (ref) == 0
1766 && DECL_EXTERNAL (ref)
1767 && !TREE_PUBLIC (ref))
1768 record_maybe_used_decl (ref);
1771 if (TREE_CODE (ref) == CONST_DECL)
1773 ref = DECL_INITIAL (ref);
1774 TREE_CONSTANT (ref) = 1;
1775 TREE_INVARIANT (ref) = 1;
1777 else if (current_function_decl != 0
1778 && !DECL_FILE_SCOPE_P (current_function_decl)
1779 && (TREE_CODE (ref) == VAR_DECL
1780 || TREE_CODE (ref) == PARM_DECL
1781 || TREE_CODE (ref) == FUNCTION_DECL))
1783 tree context = decl_function_context (ref);
1785 if (context != 0 && context != current_function_decl)
1786 DECL_NONLOCAL (ref) = 1;
1789 return ref;
1792 /* Record details of decls possibly used inside sizeof or typeof. */
1793 struct maybe_used_decl
1795 /* The decl. */
1796 tree decl;
1797 /* The level seen at (in_sizeof + in_typeof). */
1798 int level;
1799 /* The next one at this level or above, or NULL. */
1800 struct maybe_used_decl *next;
1803 static struct maybe_used_decl *maybe_used_decls;
1805 /* Record that DECL, an undefined static function reference seen
1806 inside sizeof or typeof, might be used if the operand of sizeof is
1807 a VLA type or the operand of typeof is a variably modified
1808 type. */
1810 void
1811 record_maybe_used_decl (tree decl)
1813 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1814 t->decl = decl;
1815 t->level = in_sizeof + in_typeof;
1816 t->next = maybe_used_decls;
1817 maybe_used_decls = t;
1820 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1821 USED is false, just discard them. If it is true, mark them used
1822 (if no longer inside sizeof or typeof) or move them to the next
1823 level up (if still inside sizeof or typeof). */
1825 void
1826 pop_maybe_used (bool used)
1828 struct maybe_used_decl *p = maybe_used_decls;
1829 int cur_level = in_sizeof + in_typeof;
1830 while (p && p->level > cur_level)
1832 if (used)
1834 if (cur_level == 0)
1835 C_DECL_USED (p->decl) = 1;
1836 else
1837 p->level = cur_level;
1839 p = p->next;
1841 if (!used || cur_level == 0)
1842 maybe_used_decls = p;
1845 /* Return the result of sizeof applied to EXPR. */
1847 struct c_expr
1848 c_expr_sizeof_expr (struct c_expr expr)
1850 struct c_expr ret;
1851 if (expr.value == error_mark_node)
1853 ret.value = error_mark_node;
1854 ret.original_code = ERROR_MARK;
1855 pop_maybe_used (false);
1857 else
1859 ret.value = c_sizeof (TREE_TYPE (expr.value));
1860 ret.original_code = ERROR_MARK;
1861 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1863 return ret;
1866 /* Return the result of sizeof applied to T, a structure for the type
1867 name passed to sizeof (rather than the type itself). */
1869 struct c_expr
1870 c_expr_sizeof_type (struct c_type_name *t)
1872 tree type;
1873 struct c_expr ret;
1874 type = groktypename (t);
1875 ret.value = c_sizeof (type);
1876 ret.original_code = ERROR_MARK;
1877 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1878 return ret;
1881 /* Build a function call to function FUNCTION with parameters PARAMS.
1882 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1883 TREE_VALUE of each node is a parameter-expression.
1884 FUNCTION's data type may be a function type or a pointer-to-function. */
1886 tree
1887 build_function_call (tree function, tree params)
1889 tree fntype, fundecl = 0;
1890 tree coerced_params;
1891 tree name = NULL_TREE, result;
1892 tree tem;
1894 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1895 STRIP_TYPE_NOPS (function);
1897 /* Convert anything with function type to a pointer-to-function. */
1898 if (TREE_CODE (function) == FUNCTION_DECL)
1900 name = DECL_NAME (function);
1902 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1903 (because calling an inline function does not mean the function
1904 needs to be separately compiled). */
1905 fntype = build_type_variant (TREE_TYPE (function),
1906 TREE_READONLY (function),
1907 TREE_THIS_VOLATILE (function));
1908 fundecl = function;
1909 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1911 else
1912 function = default_conversion (function);
1914 fntype = TREE_TYPE (function);
1916 if (TREE_CODE (fntype) == ERROR_MARK)
1917 return error_mark_node;
1919 if (!(TREE_CODE (fntype) == POINTER_TYPE
1920 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1922 error ("called object %qE is not a function", function);
1923 return error_mark_node;
1926 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1927 current_function_returns_abnormally = 1;
1929 /* fntype now gets the type of function pointed to. */
1930 fntype = TREE_TYPE (fntype);
1932 /* Check that the function is called through a compatible prototype.
1933 If it is not, replace the call by a trap, wrapped up in a compound
1934 expression if necessary. This has the nice side-effect to prevent
1935 the tree-inliner from generating invalid assignment trees which may
1936 blow up in the RTL expander later.
1938 ??? This doesn't work for Objective-C because objc_comptypes
1939 refuses to compare function prototypes, yet the compiler appears
1940 to build calls that are flagged as invalid by C's comptypes. */
1941 if (!c_dialect_objc ()
1942 && TREE_CODE (function) == NOP_EXPR
1943 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1944 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1945 && !comptypes (fntype, TREE_TYPE (tem)))
1947 tree return_type = TREE_TYPE (fntype);
1948 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1949 NULL_TREE);
1951 /* This situation leads to run-time undefined behavior. We can't,
1952 therefore, simply error unless we can prove that all possible
1953 executions of the program must execute the code. */
1954 warning ("function called through a non-compatible type");
1956 /* We can, however, treat "undefined" any way we please.
1957 Call abort to encourage the user to fix the program. */
1958 inform ("if this code is reached, the program will abort");
1960 if (VOID_TYPE_P (return_type))
1961 return trap;
1962 else
1964 tree rhs;
1966 if (AGGREGATE_TYPE_P (return_type))
1967 rhs = build_compound_literal (return_type,
1968 build_constructor (return_type,
1969 NULL_TREE));
1970 else
1971 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1973 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
1977 /* Convert the parameters to the types declared in the
1978 function prototype, or apply default promotions. */
1980 coerced_params
1981 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
1983 if (coerced_params == error_mark_node)
1984 return error_mark_node;
1986 /* Check that the arguments to the function are valid. */
1988 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1990 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
1991 function, coerced_params, NULL_TREE);
1992 TREE_SIDE_EFFECTS (result) = 1;
1994 if (require_constant_value)
1996 result = fold_initializer (result);
1998 if (TREE_CONSTANT (result)
1999 && (name == NULL_TREE
2000 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2001 pedwarn_init ("initializer element is not constant");
2003 else
2004 result = fold (result);
2006 if (VOID_TYPE_P (TREE_TYPE (result)))
2007 return result;
2008 return require_complete_type (result);
2011 /* Convert the argument expressions in the list VALUES
2012 to the types in the list TYPELIST. The result is a list of converted
2013 argument expressions, unless there are too few arguments in which
2014 case it is error_mark_node.
2016 If TYPELIST is exhausted, or when an element has NULL as its type,
2017 perform the default conversions.
2019 PARMLIST is the chain of parm decls for the function being called.
2020 It may be 0, if that info is not available.
2021 It is used only for generating error messages.
2023 FUNCTION is a tree for the called function. It is used only for
2024 error messages, where it is formatted with %qE.
2026 This is also where warnings about wrong number of args are generated.
2028 Both VALUES and the returned value are chains of TREE_LIST nodes
2029 with the elements of the list in the TREE_VALUE slots of those nodes. */
2031 static tree
2032 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2034 tree typetail, valtail;
2035 tree result = NULL;
2036 int parmnum;
2037 tree selector;
2039 /* Change pointer to function to the function itself for
2040 diagnostics. */
2041 if (TREE_CODE (function) == ADDR_EXPR
2042 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2043 function = TREE_OPERAND (function, 0);
2045 /* Handle an ObjC selector specially for diagnostics. */
2046 selector = objc_message_selector ();
2048 /* Scan the given expressions and types, producing individual
2049 converted arguments and pushing them on RESULT in reverse order. */
2051 for (valtail = values, typetail = typelist, parmnum = 0;
2052 valtail;
2053 valtail = TREE_CHAIN (valtail), parmnum++)
2055 tree type = typetail ? TREE_VALUE (typetail) : 0;
2056 tree val = TREE_VALUE (valtail);
2057 tree rname = function;
2058 int argnum = parmnum + 1;
2060 if (type == void_type_node)
2062 error ("too many arguments to function %qE", function);
2063 break;
2066 if (selector && argnum > 2)
2068 rname = selector;
2069 argnum -= 2;
2072 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2073 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2074 to convert automatically to a pointer. */
2075 if (TREE_CODE (val) == NON_LVALUE_EXPR)
2076 val = TREE_OPERAND (val, 0);
2078 val = default_function_array_conversion (val);
2080 val = require_complete_type (val);
2082 if (type != 0)
2084 /* Formal parm type is specified by a function prototype. */
2085 tree parmval;
2087 if (!COMPLETE_TYPE_P (type))
2089 error ("type of formal parameter %d is incomplete", parmnum + 1);
2090 parmval = val;
2092 else
2094 /* Optionally warn about conversions that
2095 differ from the default conversions. */
2096 if (warn_conversion || warn_traditional)
2098 unsigned int formal_prec = TYPE_PRECISION (type);
2100 if (INTEGRAL_TYPE_P (type)
2101 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2102 warning ("passing argument %d of %qE as integer "
2103 "rather than floating due to prototype",
2104 argnum, rname);
2105 if (INTEGRAL_TYPE_P (type)
2106 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2107 warning ("passing argument %d of %qE as integer "
2108 "rather than complex due to prototype",
2109 argnum, rname);
2110 else if (TREE_CODE (type) == COMPLEX_TYPE
2111 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2112 warning ("passing argument %d of %qE as complex "
2113 "rather than floating due to prototype",
2114 argnum, rname);
2115 else if (TREE_CODE (type) == REAL_TYPE
2116 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2117 warning ("passing argument %d of %qE as floating "
2118 "rather than integer due to prototype",
2119 argnum, rname);
2120 else if (TREE_CODE (type) == COMPLEX_TYPE
2121 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2122 warning ("passing argument %d of %qE as complex "
2123 "rather than integer due to prototype",
2124 argnum, rname);
2125 else if (TREE_CODE (type) == REAL_TYPE
2126 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2127 warning ("passing argument %d of %qE as floating "
2128 "rather than complex due to prototype",
2129 argnum, rname);
2130 /* ??? At some point, messages should be written about
2131 conversions between complex types, but that's too messy
2132 to do now. */
2133 else if (TREE_CODE (type) == REAL_TYPE
2134 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2136 /* Warn if any argument is passed as `float',
2137 since without a prototype it would be `double'. */
2138 if (formal_prec == TYPE_PRECISION (float_type_node))
2139 warning ("passing argument %d of %qE as %<float%> "
2140 "rather than %<double%> due to prototype",
2141 argnum, rname);
2143 /* Detect integer changing in width or signedness.
2144 These warnings are only activated with
2145 -Wconversion, not with -Wtraditional. */
2146 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2147 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2149 tree would_have_been = default_conversion (val);
2150 tree type1 = TREE_TYPE (would_have_been);
2152 if (TREE_CODE (type) == ENUMERAL_TYPE
2153 && (TYPE_MAIN_VARIANT (type)
2154 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2155 /* No warning if function asks for enum
2156 and the actual arg is that enum type. */
2158 else if (formal_prec != TYPE_PRECISION (type1))
2159 warning ("passing argument %d of %qE with different "
2160 "width due to prototype", argnum, rname);
2161 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2163 /* Don't complain if the formal parameter type
2164 is an enum, because we can't tell now whether
2165 the value was an enum--even the same enum. */
2166 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2168 else if (TREE_CODE (val) == INTEGER_CST
2169 && int_fits_type_p (val, type))
2170 /* Change in signedness doesn't matter
2171 if a constant value is unaffected. */
2173 /* Likewise for a constant in a NOP_EXPR. */
2174 else if (TREE_CODE (val) == NOP_EXPR
2175 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2176 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2178 /* If the value is extended from a narrower
2179 unsigned type, it doesn't matter whether we
2180 pass it as signed or unsigned; the value
2181 certainly is the same either way. */
2182 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2183 && TYPE_UNSIGNED (TREE_TYPE (val)))
2185 else if (TYPE_UNSIGNED (type))
2186 warning ("passing argument %d of %qE as unsigned "
2187 "due to prototype", argnum, rname);
2188 else
2189 warning ("passing argument %d of %qE as signed "
2190 "due to prototype", argnum, rname);
2194 parmval = convert_for_assignment (type, val, ic_argpass,
2195 fundecl, function,
2196 parmnum + 1);
2198 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2199 && INTEGRAL_TYPE_P (type)
2200 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2201 parmval = default_conversion (parmval);
2203 result = tree_cons (NULL_TREE, parmval, result);
2205 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2206 && (TYPE_PRECISION (TREE_TYPE (val))
2207 < TYPE_PRECISION (double_type_node)))
2208 /* Convert `float' to `double'. */
2209 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2210 else
2211 /* Convert `short' and `char' to full-size `int'. */
2212 result = tree_cons (NULL_TREE, default_conversion (val), result);
2214 if (typetail)
2215 typetail = TREE_CHAIN (typetail);
2218 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2220 error ("too few arguments to function %qE", function);
2221 return error_mark_node;
2224 return nreverse (result);
2227 /* This is the entry point used by the parser
2228 for binary operators in the input.
2229 In addition to constructing the expression,
2230 we check for operands that were written with other binary operators
2231 in a way that is likely to confuse the user. */
2233 struct c_expr
2234 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2235 struct c_expr arg2)
2237 struct c_expr result;
2239 enum tree_code code1 = arg1.original_code;
2240 enum tree_code code2 = arg2.original_code;
2242 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2243 result.original_code = code;
2245 if (TREE_CODE (result.value) == ERROR_MARK)
2246 return result;
2248 /* Check for cases such as x+y<<z which users are likely
2249 to misinterpret. */
2250 if (warn_parentheses)
2252 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2254 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2255 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2256 warning ("suggest parentheses around + or - inside shift");
2259 if (code == TRUTH_ORIF_EXPR)
2261 if (code1 == TRUTH_ANDIF_EXPR
2262 || code2 == TRUTH_ANDIF_EXPR)
2263 warning ("suggest parentheses around && within ||");
2266 if (code == BIT_IOR_EXPR)
2268 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2269 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2270 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2271 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2272 warning ("suggest parentheses around arithmetic in operand of |");
2273 /* Check cases like x|y==z */
2274 if (TREE_CODE_CLASS (code1) == tcc_comparison
2275 || TREE_CODE_CLASS (code2) == tcc_comparison)
2276 warning ("suggest parentheses around comparison in operand of |");
2279 if (code == BIT_XOR_EXPR)
2281 if (code1 == BIT_AND_EXPR
2282 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2283 || code2 == BIT_AND_EXPR
2284 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2285 warning ("suggest parentheses around arithmetic in operand of ^");
2286 /* Check cases like x^y==z */
2287 if (TREE_CODE_CLASS (code1) == tcc_comparison
2288 || TREE_CODE_CLASS (code2) == tcc_comparison)
2289 warning ("suggest parentheses around comparison in operand of ^");
2292 if (code == BIT_AND_EXPR)
2294 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2295 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2296 warning ("suggest parentheses around + or - in operand of &");
2297 /* Check cases like x&y==z */
2298 if (TREE_CODE_CLASS (code1) == tcc_comparison
2299 || TREE_CODE_CLASS (code2) == tcc_comparison)
2300 warning ("suggest parentheses around comparison in operand of &");
2302 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2303 if (TREE_CODE_CLASS (code) == tcc_comparison
2304 && (TREE_CODE_CLASS (code1) == tcc_comparison
2305 || TREE_CODE_CLASS (code2) == tcc_comparison))
2306 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2310 unsigned_conversion_warning (result.value, arg1.value);
2311 unsigned_conversion_warning (result.value, arg2.value);
2312 overflow_warning (result.value);
2314 return result;
2317 /* Return a tree for the difference of pointers OP0 and OP1.
2318 The resulting tree has type int. */
2320 static tree
2321 pointer_diff (tree op0, tree op1)
2323 tree restype = ptrdiff_type_node;
2325 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2326 tree con0, con1, lit0, lit1;
2327 tree orig_op1 = op1;
2329 if (pedantic || warn_pointer_arith)
2331 if (TREE_CODE (target_type) == VOID_TYPE)
2332 pedwarn ("pointer of type %<void *%> used in subtraction");
2333 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2334 pedwarn ("pointer to a function used in subtraction");
2337 /* If the conversion to ptrdiff_type does anything like widening or
2338 converting a partial to an integral mode, we get a convert_expression
2339 that is in the way to do any simplifications.
2340 (fold-const.c doesn't know that the extra bits won't be needed.
2341 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2342 different mode in place.)
2343 So first try to find a common term here 'by hand'; we want to cover
2344 at least the cases that occur in legal static initializers. */
2345 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2346 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2348 if (TREE_CODE (con0) == PLUS_EXPR)
2350 lit0 = TREE_OPERAND (con0, 1);
2351 con0 = TREE_OPERAND (con0, 0);
2353 else
2354 lit0 = integer_zero_node;
2356 if (TREE_CODE (con1) == PLUS_EXPR)
2358 lit1 = TREE_OPERAND (con1, 1);
2359 con1 = TREE_OPERAND (con1, 0);
2361 else
2362 lit1 = integer_zero_node;
2364 if (operand_equal_p (con0, con1, 0))
2366 op0 = lit0;
2367 op1 = lit1;
2371 /* First do the subtraction as integers;
2372 then drop through to build the divide operator.
2373 Do not do default conversions on the minus operator
2374 in case restype is a short type. */
2376 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2377 convert (restype, op1), 0);
2378 /* This generates an error if op1 is pointer to incomplete type. */
2379 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2380 error ("arithmetic on pointer to an incomplete type");
2382 /* This generates an error if op0 is pointer to incomplete type. */
2383 op1 = c_size_in_bytes (target_type);
2385 /* Divide by the size, in easiest possible way. */
2386 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2389 /* Construct and perhaps optimize a tree representation
2390 for a unary operation. CODE, a tree_code, specifies the operation
2391 and XARG is the operand.
2392 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2393 the default promotions (such as from short to int).
2394 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2395 allows non-lvalues; this is only used to handle conversion of non-lvalue
2396 arrays to pointers in C99. */
2398 tree
2399 build_unary_op (enum tree_code code, tree xarg, int flag)
2401 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2402 tree arg = xarg;
2403 tree argtype = 0;
2404 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2405 tree val;
2406 int noconvert = flag;
2408 if (typecode == ERROR_MARK)
2409 return error_mark_node;
2410 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2411 typecode = INTEGER_TYPE;
2413 switch (code)
2415 case CONVERT_EXPR:
2416 /* This is used for unary plus, because a CONVERT_EXPR
2417 is enough to prevent anybody from looking inside for
2418 associativity, but won't generate any code. */
2419 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2420 || typecode == COMPLEX_TYPE
2421 || typecode == VECTOR_TYPE))
2423 error ("wrong type argument to unary plus");
2424 return error_mark_node;
2426 else if (!noconvert)
2427 arg = default_conversion (arg);
2428 arg = non_lvalue (arg);
2429 break;
2431 case NEGATE_EXPR:
2432 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2433 || typecode == COMPLEX_TYPE
2434 || typecode == VECTOR_TYPE))
2436 error ("wrong type argument to unary minus");
2437 return error_mark_node;
2439 else if (!noconvert)
2440 arg = default_conversion (arg);
2441 break;
2443 case BIT_NOT_EXPR:
2444 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2446 if (!noconvert)
2447 arg = default_conversion (arg);
2449 else if (typecode == COMPLEX_TYPE)
2451 code = CONJ_EXPR;
2452 if (pedantic)
2453 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2454 if (!noconvert)
2455 arg = default_conversion (arg);
2457 else
2459 error ("wrong type argument to bit-complement");
2460 return error_mark_node;
2462 break;
2464 case ABS_EXPR:
2465 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2467 error ("wrong type argument to abs");
2468 return error_mark_node;
2470 else if (!noconvert)
2471 arg = default_conversion (arg);
2472 break;
2474 case CONJ_EXPR:
2475 /* Conjugating a real value is a no-op, but allow it anyway. */
2476 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2477 || typecode == COMPLEX_TYPE))
2479 error ("wrong type argument to conjugation");
2480 return error_mark_node;
2482 else if (!noconvert)
2483 arg = default_conversion (arg);
2484 break;
2486 case TRUTH_NOT_EXPR:
2487 if (typecode != INTEGER_TYPE
2488 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2489 && typecode != COMPLEX_TYPE
2490 /* These will convert to a pointer. */
2491 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2493 error ("wrong type argument to unary exclamation mark");
2494 return error_mark_node;
2496 arg = lang_hooks.truthvalue_conversion (arg);
2497 return invert_truthvalue (arg);
2499 case NOP_EXPR:
2500 break;
2502 case REALPART_EXPR:
2503 if (TREE_CODE (arg) == COMPLEX_CST)
2504 return TREE_REALPART (arg);
2505 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2506 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2507 else
2508 return arg;
2510 case IMAGPART_EXPR:
2511 if (TREE_CODE (arg) == COMPLEX_CST)
2512 return TREE_IMAGPART (arg);
2513 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2514 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2515 else
2516 return convert (TREE_TYPE (arg), integer_zero_node);
2518 case PREINCREMENT_EXPR:
2519 case POSTINCREMENT_EXPR:
2520 case PREDECREMENT_EXPR:
2521 case POSTDECREMENT_EXPR:
2523 /* Increment or decrement the real part of the value,
2524 and don't change the imaginary part. */
2525 if (typecode == COMPLEX_TYPE)
2527 tree real, imag;
2529 if (pedantic)
2530 pedwarn ("ISO C does not support %<++%> and %<--%>"
2531 " on complex types");
2533 arg = stabilize_reference (arg);
2534 real = build_unary_op (REALPART_EXPR, arg, 1);
2535 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2536 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2537 build_unary_op (code, real, 1), imag);
2540 /* Report invalid types. */
2542 if (typecode != POINTER_TYPE
2543 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2545 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2546 error ("wrong type argument to increment");
2547 else
2548 error ("wrong type argument to decrement");
2550 return error_mark_node;
2554 tree inc;
2555 tree result_type = TREE_TYPE (arg);
2557 arg = get_unwidened (arg, 0);
2558 argtype = TREE_TYPE (arg);
2560 /* Compute the increment. */
2562 if (typecode == POINTER_TYPE)
2564 /* If pointer target is an undefined struct,
2565 we just cannot know how to do the arithmetic. */
2566 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2568 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2569 error ("increment of pointer to unknown structure");
2570 else
2571 error ("decrement of pointer to unknown structure");
2573 else if ((pedantic || warn_pointer_arith)
2574 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2575 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2577 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2578 pedwarn ("wrong type argument to increment");
2579 else
2580 pedwarn ("wrong type argument to decrement");
2583 inc = c_size_in_bytes (TREE_TYPE (result_type));
2585 else
2586 inc = integer_one_node;
2588 inc = convert (argtype, inc);
2590 /* Complain about anything else that is not a true lvalue. */
2591 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2592 || code == POSTINCREMENT_EXPR)
2593 ? lv_increment
2594 : lv_decrement)))
2595 return error_mark_node;
2597 /* Report a read-only lvalue. */
2598 if (TREE_READONLY (arg))
2599 readonly_error (arg,
2600 ((code == PREINCREMENT_EXPR
2601 || code == POSTINCREMENT_EXPR)
2602 ? lv_increment : lv_decrement));
2604 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2605 val = boolean_increment (code, arg);
2606 else
2607 val = build2 (code, TREE_TYPE (arg), arg, inc);
2608 TREE_SIDE_EFFECTS (val) = 1;
2609 val = convert (result_type, val);
2610 if (TREE_CODE (val) != code)
2611 TREE_NO_WARNING (val) = 1;
2612 return val;
2615 case ADDR_EXPR:
2616 /* Note that this operation never does default_conversion. */
2618 /* Let &* cancel out to simplify resulting code. */
2619 if (TREE_CODE (arg) == INDIRECT_REF)
2621 /* Don't let this be an lvalue. */
2622 if (lvalue_p (TREE_OPERAND (arg, 0)))
2623 return non_lvalue (TREE_OPERAND (arg, 0));
2624 return TREE_OPERAND (arg, 0);
2627 /* For &x[y], return x+y */
2628 if (TREE_CODE (arg) == ARRAY_REF)
2630 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2631 return error_mark_node;
2632 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2633 TREE_OPERAND (arg, 1), 1);
2636 /* Anything not already handled and not a true memory reference
2637 or a non-lvalue array is an error. */
2638 else if (typecode != FUNCTION_TYPE && !flag
2639 && !lvalue_or_else (arg, lv_addressof))
2640 return error_mark_node;
2642 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2643 argtype = TREE_TYPE (arg);
2645 /* If the lvalue is const or volatile, merge that into the type
2646 to which the address will point. Note that you can't get a
2647 restricted pointer by taking the address of something, so we
2648 only have to deal with `const' and `volatile' here. */
2649 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2650 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2651 argtype = c_build_type_variant (argtype,
2652 TREE_READONLY (arg),
2653 TREE_THIS_VOLATILE (arg));
2655 if (!c_mark_addressable (arg))
2656 return error_mark_node;
2658 if (TREE_CODE (arg) == COMPONENT_REF
2659 && DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
2661 error ("attempt to take address of bit-field structure member %qD",
2662 TREE_OPERAND (arg, 1));
2663 return error_mark_node;
2666 argtype = build_pointer_type (argtype);
2668 /* ??? Cope with user tricks that amount to offsetof. Delete this
2669 when we have proper support for integer constant expressions. */
2670 val = get_base_address (arg);
2671 if (val && TREE_CODE (val) == INDIRECT_REF
2672 && integer_zerop (TREE_OPERAND (val, 0)))
2673 return fold_convert (argtype, fold_offsetof (arg));
2675 val = build1 (ADDR_EXPR, argtype, arg);
2677 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2678 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2680 return val;
2682 default:
2683 break;
2686 if (argtype == 0)
2687 argtype = TREE_TYPE (arg);
2688 val = build1 (code, argtype, arg);
2689 return require_constant_value ? fold_initializer (val) : fold (val);
2692 /* Return nonzero if REF is an lvalue valid for this language.
2693 Lvalues can be assigned, unless their type has TYPE_READONLY.
2694 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2697 lvalue_p (tree ref)
2699 enum tree_code code = TREE_CODE (ref);
2701 switch (code)
2703 case REALPART_EXPR:
2704 case IMAGPART_EXPR:
2705 case COMPONENT_REF:
2706 return lvalue_p (TREE_OPERAND (ref, 0));
2708 case COMPOUND_LITERAL_EXPR:
2709 case STRING_CST:
2710 return 1;
2712 case INDIRECT_REF:
2713 case ARRAY_REF:
2714 case VAR_DECL:
2715 case PARM_DECL:
2716 case RESULT_DECL:
2717 case ERROR_MARK:
2718 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2719 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2721 case BIND_EXPR:
2722 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2724 default:
2725 return 0;
2729 /* Return nonzero if REF is an lvalue valid for this language;
2730 otherwise, print an error message and return zero. USE says
2731 how the lvalue is being used and so selects the error message. */
2733 static int
2734 lvalue_or_else (tree ref, enum lvalue_use use)
2736 int win = lvalue_p (ref);
2738 if (!win)
2740 switch (use)
2742 case lv_assign:
2743 error ("invalid lvalue in assignment");
2744 break;
2745 case lv_increment:
2746 error ("invalid lvalue in increment");
2747 break;
2748 case lv_decrement:
2749 error ("invalid lvalue in decrement");
2750 break;
2751 case lv_addressof:
2752 error ("invalid lvalue in unary %<&%>");
2753 break;
2754 case lv_asm:
2755 error ("invalid lvalue in asm statement");
2756 break;
2757 default:
2758 gcc_unreachable ();
2762 return win;
2766 /* Give an error for storing in something that is 'const'. */
2768 static void
2769 readonly_error (tree arg, enum lvalue_use use)
2771 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2772 /* Using this macro rather than (for example) arrays of messages
2773 ensures that all the format strings are checked at compile
2774 time. */
2775 #define READONLY_MSG(A, I, D) (use == lv_assign \
2776 ? (A) \
2777 : (use == lv_increment ? (I) : (D)))
2778 if (TREE_CODE (arg) == COMPONENT_REF)
2780 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2781 readonly_error (TREE_OPERAND (arg, 0), use);
2782 else
2783 error (READONLY_MSG (N_("assignment of read-only member %qs"),
2784 N_("increment of read-only member %qs"),
2785 N_("decrement of read-only member %qs")),
2786 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2788 else if (TREE_CODE (arg) == VAR_DECL)
2789 error (READONLY_MSG (N_("assignment of read-only variable %qs"),
2790 N_("increment of read-only variable %qs"),
2791 N_("decrement of read-only variable %qs")),
2792 IDENTIFIER_POINTER (DECL_NAME (arg)));
2793 else
2794 error (READONLY_MSG (N_("assignment of read-only location"),
2795 N_("increment of read-only location"),
2796 N_("decrement of read-only location")));
2799 /* Mark EXP saying that we need to be able to take the
2800 address of it; it should not be allocated in a register.
2801 Returns true if successful. */
2803 bool
2804 c_mark_addressable (tree exp)
2806 tree x = exp;
2808 while (1)
2809 switch (TREE_CODE (x))
2811 case COMPONENT_REF:
2812 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2814 error
2815 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2816 return false;
2819 /* ... fall through ... */
2821 case ADDR_EXPR:
2822 case ARRAY_REF:
2823 case REALPART_EXPR:
2824 case IMAGPART_EXPR:
2825 x = TREE_OPERAND (x, 0);
2826 break;
2828 case COMPOUND_LITERAL_EXPR:
2829 case CONSTRUCTOR:
2830 TREE_ADDRESSABLE (x) = 1;
2831 return true;
2833 case VAR_DECL:
2834 case CONST_DECL:
2835 case PARM_DECL:
2836 case RESULT_DECL:
2837 if (C_DECL_REGISTER (x)
2838 && DECL_NONLOCAL (x))
2840 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2842 error
2843 ("global register variable %qD used in nested function", x);
2844 return false;
2846 pedwarn ("register variable %qD used in nested function", x);
2848 else if (C_DECL_REGISTER (x))
2850 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2851 error ("address of global register variable %qD requested", x);
2852 else
2853 error ("address of register variable %qD requested", x);
2854 return false;
2857 /* drops in */
2858 case FUNCTION_DECL:
2859 TREE_ADDRESSABLE (x) = 1;
2860 /* drops out */
2861 default:
2862 return true;
2866 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2868 tree
2869 build_conditional_expr (tree ifexp, tree op1, tree op2)
2871 tree type1;
2872 tree type2;
2873 enum tree_code code1;
2874 enum tree_code code2;
2875 tree result_type = NULL;
2876 tree orig_op1 = op1, orig_op2 = op2;
2878 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2880 /* Promote both alternatives. */
2882 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2883 op1 = default_conversion (op1);
2884 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2885 op2 = default_conversion (op2);
2887 if (TREE_CODE (ifexp) == ERROR_MARK
2888 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2889 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2890 return error_mark_node;
2892 type1 = TREE_TYPE (op1);
2893 code1 = TREE_CODE (type1);
2894 type2 = TREE_TYPE (op2);
2895 code2 = TREE_CODE (type2);
2897 /* C90 does not permit non-lvalue arrays in conditional expressions.
2898 In C99 they will be pointers by now. */
2899 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2901 error ("non-lvalue array in conditional expression");
2902 return error_mark_node;
2905 /* Quickly detect the usual case where op1 and op2 have the same type
2906 after promotion. */
2907 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2909 if (type1 == type2)
2910 result_type = type1;
2911 else
2912 result_type = TYPE_MAIN_VARIANT (type1);
2914 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2915 || code1 == COMPLEX_TYPE)
2916 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2917 || code2 == COMPLEX_TYPE))
2919 result_type = common_type (type1, type2);
2921 /* If -Wsign-compare, warn here if type1 and type2 have
2922 different signedness. We'll promote the signed to unsigned
2923 and later code won't know it used to be different.
2924 Do this check on the original types, so that explicit casts
2925 will be considered, but default promotions won't. */
2926 if (warn_sign_compare && !skip_evaluation)
2928 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2929 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2931 if (unsigned_op1 ^ unsigned_op2)
2933 /* Do not warn if the result type is signed, since the
2934 signed type will only be chosen if it can represent
2935 all the values of the unsigned type. */
2936 if (!TYPE_UNSIGNED (result_type))
2937 /* OK */;
2938 /* Do not warn if the signed quantity is an unsuffixed
2939 integer literal (or some static constant expression
2940 involving such literals) and it is non-negative. */
2941 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2942 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2943 /* OK */;
2944 else
2945 warning ("signed and unsigned type in conditional expression");
2949 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2951 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2952 pedwarn ("ISO C forbids conditional expr with only one void side");
2953 result_type = void_type_node;
2955 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2957 if (comp_target_types (type1, type2, 1))
2958 result_type = common_pointer_type (type1, type2);
2959 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2960 && TREE_CODE (orig_op1) != NOP_EXPR)
2961 result_type = qualify_type (type2, type1);
2962 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2963 && TREE_CODE (orig_op2) != NOP_EXPR)
2964 result_type = qualify_type (type1, type2);
2965 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2967 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2968 pedwarn ("ISO C forbids conditional expr between "
2969 "%<void *%> and function pointer");
2970 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2971 TREE_TYPE (type2)));
2973 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2975 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2976 pedwarn ("ISO C forbids conditional expr between "
2977 "%<void *%> and function pointer");
2978 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2979 TREE_TYPE (type1)));
2981 else
2983 pedwarn ("pointer type mismatch in conditional expression");
2984 result_type = build_pointer_type (void_type_node);
2987 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2989 if (!integer_zerop (op2))
2990 pedwarn ("pointer/integer type mismatch in conditional expression");
2991 else
2993 op2 = null_pointer_node;
2995 result_type = type1;
2997 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2999 if (!integer_zerop (op1))
3000 pedwarn ("pointer/integer type mismatch in conditional expression");
3001 else
3003 op1 = null_pointer_node;
3005 result_type = type2;
3008 if (!result_type)
3010 if (flag_cond_mismatch)
3011 result_type = void_type_node;
3012 else
3014 error ("type mismatch in conditional expression");
3015 return error_mark_node;
3019 /* Merge const and volatile flags of the incoming types. */
3020 result_type
3021 = build_type_variant (result_type,
3022 TREE_READONLY (op1) || TREE_READONLY (op2),
3023 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3025 if (result_type != TREE_TYPE (op1))
3026 op1 = convert_and_check (result_type, op1);
3027 if (result_type != TREE_TYPE (op2))
3028 op2 = convert_and_check (result_type, op2);
3030 if (TREE_CODE (ifexp) == INTEGER_CST)
3031 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3033 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3036 /* Return a compound expression that performs two expressions and
3037 returns the value of the second of them. */
3039 tree
3040 build_compound_expr (tree expr1, tree expr2)
3042 /* Convert arrays and functions to pointers. */
3043 expr2 = default_function_array_conversion (expr2);
3045 if (!TREE_SIDE_EFFECTS (expr1))
3047 /* The left-hand operand of a comma expression is like an expression
3048 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3049 any side-effects, unless it was explicitly cast to (void). */
3050 if (warn_unused_value
3051 && !(TREE_CODE (expr1) == CONVERT_EXPR
3052 && VOID_TYPE_P (TREE_TYPE (expr1))))
3053 warning ("left-hand operand of comma expression has no effect");
3056 /* With -Wunused, we should also warn if the left-hand operand does have
3057 side-effects, but computes a value which is not used. For example, in
3058 `foo() + bar(), baz()' the result of the `+' operator is not used,
3059 so we should issue a warning. */
3060 else if (warn_unused_value)
3061 warn_if_unused_value (expr1, input_location);
3063 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3066 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3068 tree
3069 build_c_cast (tree type, tree expr)
3071 tree value = expr;
3073 if (type == error_mark_node || expr == error_mark_node)
3074 return error_mark_node;
3076 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3077 only in <protocol> qualifications. But when constructing cast expressions,
3078 the protocols do matter and must be kept around. */
3079 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3080 return build1 (NOP_EXPR, type, expr);
3082 type = TYPE_MAIN_VARIANT (type);
3084 if (TREE_CODE (type) == ARRAY_TYPE)
3086 error ("cast specifies array type");
3087 return error_mark_node;
3090 if (TREE_CODE (type) == FUNCTION_TYPE)
3092 error ("cast specifies function type");
3093 return error_mark_node;
3096 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3098 if (pedantic)
3100 if (TREE_CODE (type) == RECORD_TYPE
3101 || TREE_CODE (type) == UNION_TYPE)
3102 pedwarn ("ISO C forbids casting nonscalar to the same type");
3105 else if (TREE_CODE (type) == UNION_TYPE)
3107 tree field;
3108 value = default_function_array_conversion (value);
3110 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3111 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3112 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3113 break;
3115 if (field)
3117 tree t;
3119 if (pedantic)
3120 pedwarn ("ISO C forbids casts to union type");
3121 t = digest_init (type,
3122 build_constructor (type,
3123 build_tree_list (field, value)),
3124 true, 0);
3125 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3126 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3127 return t;
3129 error ("cast to union type from type not present in union");
3130 return error_mark_node;
3132 else
3134 tree otype, ovalue;
3136 /* If casting to void, avoid the error that would come
3137 from default_conversion in the case of a non-lvalue array. */
3138 if (type == void_type_node)
3139 return build1 (CONVERT_EXPR, type, value);
3141 /* Convert functions and arrays to pointers,
3142 but don't convert any other types. */
3143 value = default_function_array_conversion (value);
3144 otype = TREE_TYPE (value);
3146 /* Optionally warn about potentially worrisome casts. */
3148 if (warn_cast_qual
3149 && TREE_CODE (type) == POINTER_TYPE
3150 && TREE_CODE (otype) == POINTER_TYPE)
3152 tree in_type = type;
3153 tree in_otype = otype;
3154 int added = 0;
3155 int discarded = 0;
3157 /* Check that the qualifiers on IN_TYPE are a superset of
3158 the qualifiers of IN_OTYPE. The outermost level of
3159 POINTER_TYPE nodes is uninteresting and we stop as soon
3160 as we hit a non-POINTER_TYPE node on either type. */
3163 in_otype = TREE_TYPE (in_otype);
3164 in_type = TREE_TYPE (in_type);
3166 /* GNU C allows cv-qualified function types. 'const'
3167 means the function is very pure, 'volatile' means it
3168 can't return. We need to warn when such qualifiers
3169 are added, not when they're taken away. */
3170 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3171 && TREE_CODE (in_type) == FUNCTION_TYPE)
3172 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3173 else
3174 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3176 while (TREE_CODE (in_type) == POINTER_TYPE
3177 && TREE_CODE (in_otype) == POINTER_TYPE);
3179 if (added)
3180 warning ("cast adds new qualifiers to function type");
3182 if (discarded)
3183 /* There are qualifiers present in IN_OTYPE that are not
3184 present in IN_TYPE. */
3185 warning ("cast discards qualifiers from pointer target type");
3188 /* Warn about possible alignment problems. */
3189 if (STRICT_ALIGNMENT && warn_cast_align
3190 && TREE_CODE (type) == POINTER_TYPE
3191 && TREE_CODE (otype) == POINTER_TYPE
3192 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3193 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3194 /* Don't warn about opaque types, where the actual alignment
3195 restriction is unknown. */
3196 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3197 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3198 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3199 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3200 warning ("cast increases required alignment of target type");
3202 if (TREE_CODE (type) == INTEGER_TYPE
3203 && TREE_CODE (otype) == POINTER_TYPE
3204 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3205 && !TREE_CONSTANT (value))
3206 warning ("cast from pointer to integer of different size");
3208 if (warn_bad_function_cast
3209 && TREE_CODE (value) == CALL_EXPR
3210 && TREE_CODE (type) != TREE_CODE (otype))
3211 warning ("cast from function call of type %qT to non-matching "
3212 "type %qT", otype, type);
3214 if (TREE_CODE (type) == POINTER_TYPE
3215 && TREE_CODE (otype) == INTEGER_TYPE
3216 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3217 /* Don't warn about converting any constant. */
3218 && !TREE_CONSTANT (value))
3219 warning ("cast to pointer from integer of different size");
3221 if (TREE_CODE (type) == POINTER_TYPE
3222 && TREE_CODE (otype) == POINTER_TYPE
3223 && TREE_CODE (expr) == ADDR_EXPR
3224 && DECL_P (TREE_OPERAND (expr, 0))
3225 && flag_strict_aliasing && warn_strict_aliasing
3226 && !VOID_TYPE_P (TREE_TYPE (type)))
3228 /* Casting the address of a decl to non void pointer. Warn
3229 if the cast breaks type based aliasing. */
3230 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3231 warning ("type-punning to incomplete type might break strict-aliasing rules");
3232 else
3234 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3235 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3237 if (!alias_sets_conflict_p (set1, set2))
3238 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3239 else if (warn_strict_aliasing > 1
3240 && !alias_sets_might_conflict_p (set1, set2))
3241 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3245 /* If pedantic, warn for conversions between function and object
3246 pointer types, except for converting a null pointer constant
3247 to function pointer type. */
3248 if (pedantic
3249 && TREE_CODE (type) == POINTER_TYPE
3250 && TREE_CODE (otype) == POINTER_TYPE
3251 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3252 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3253 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3255 if (pedantic
3256 && TREE_CODE (type) == POINTER_TYPE
3257 && TREE_CODE (otype) == POINTER_TYPE
3258 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3259 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3260 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3261 && TREE_CODE (expr) != NOP_EXPR))
3262 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3264 ovalue = value;
3265 /* Replace a nonvolatile const static variable with its value. */
3266 if (optimize && TREE_CODE (value) == VAR_DECL)
3267 value = decl_constant_value (value);
3268 value = convert (type, value);
3270 /* Ignore any integer overflow caused by the cast. */
3271 if (TREE_CODE (value) == INTEGER_CST)
3273 if (EXPR_P (ovalue))
3274 /* If OVALUE had overflow set, then so will VALUE, so it
3275 is safe to overwrite. */
3276 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3277 else
3278 TREE_OVERFLOW (value) = 0;
3280 if (CONSTANT_CLASS_P (ovalue))
3281 /* Similarly, constant_overflow cannot have become
3282 cleared. */
3283 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3287 /* Don't let a cast be an lvalue. */
3288 if (value == expr)
3289 value = non_lvalue (value);
3291 return value;
3294 /* Interpret a cast of expression EXPR to type TYPE. */
3295 tree
3296 c_cast_expr (struct c_type_name *type_name, tree expr)
3298 tree type;
3299 int saved_wsp = warn_strict_prototypes;
3301 /* This avoids warnings about unprototyped casts on
3302 integers. E.g. "#define SIG_DFL (void(*)())0". */
3303 if (TREE_CODE (expr) == INTEGER_CST)
3304 warn_strict_prototypes = 0;
3305 type = groktypename (type_name);
3306 warn_strict_prototypes = saved_wsp;
3308 return build_c_cast (type, expr);
3312 /* Build an assignment expression of lvalue LHS from value RHS.
3313 MODIFYCODE is the code for a binary operator that we use
3314 to combine the old value of LHS with RHS to get the new value.
3315 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3317 tree
3318 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3320 tree result;
3321 tree newrhs;
3322 tree lhstype = TREE_TYPE (lhs);
3323 tree olhstype = lhstype;
3325 /* Types that aren't fully specified cannot be used in assignments. */
3326 lhs = require_complete_type (lhs);
3328 /* Avoid duplicate error messages from operands that had errors. */
3329 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3330 return error_mark_node;
3332 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3333 /* Do not use STRIP_NOPS here. We do not want an enumerator
3334 whose value is 0 to count as a null pointer constant. */
3335 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3336 rhs = TREE_OPERAND (rhs, 0);
3338 newrhs = rhs;
3340 /* If a binary op has been requested, combine the old LHS value with the RHS
3341 producing the value we should actually store into the LHS. */
3343 if (modifycode != NOP_EXPR)
3345 lhs = stabilize_reference (lhs);
3346 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3349 if (!lvalue_or_else (lhs, lv_assign))
3350 return error_mark_node;
3352 /* Give an error for storing in something that is 'const'. */
3354 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3355 || ((TREE_CODE (lhstype) == RECORD_TYPE
3356 || TREE_CODE (lhstype) == UNION_TYPE)
3357 && C_TYPE_FIELDS_READONLY (lhstype)))
3358 readonly_error (lhs, lv_assign);
3360 /* If storing into a structure or union member,
3361 it has probably been given type `int'.
3362 Compute the type that would go with
3363 the actual amount of storage the member occupies. */
3365 if (TREE_CODE (lhs) == COMPONENT_REF
3366 && (TREE_CODE (lhstype) == INTEGER_TYPE
3367 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3368 || TREE_CODE (lhstype) == REAL_TYPE
3369 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3370 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3372 /* If storing in a field that is in actuality a short or narrower than one,
3373 we must store in the field in its actual type. */
3375 if (lhstype != TREE_TYPE (lhs))
3377 lhs = copy_node (lhs);
3378 TREE_TYPE (lhs) = lhstype;
3381 /* Convert new value to destination type. */
3383 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3384 NULL_TREE, NULL_TREE, 0);
3385 if (TREE_CODE (newrhs) == ERROR_MARK)
3386 return error_mark_node;
3388 /* Scan operands. */
3390 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3391 TREE_SIDE_EFFECTS (result) = 1;
3393 /* If we got the LHS in a different type for storing in,
3394 convert the result back to the nominal type of LHS
3395 so that the value we return always has the same type
3396 as the LHS argument. */
3398 if (olhstype == TREE_TYPE (result))
3399 return result;
3400 return convert_for_assignment (olhstype, result, ic_assign,
3401 NULL_TREE, NULL_TREE, 0);
3404 /* Convert value RHS to type TYPE as preparation for an assignment
3405 to an lvalue of type TYPE.
3406 The real work of conversion is done by `convert'.
3407 The purpose of this function is to generate error messages
3408 for assignments that are not allowed in C.
3409 ERRTYPE says whether it is argument passing, assignment,
3410 initialization or return.
3412 FUNCTION is a tree for the function being called.
3413 PARMNUM is the number of the argument, for printing in error messages. */
3415 static tree
3416 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3417 tree fundecl, tree function, int parmnum)
3419 enum tree_code codel = TREE_CODE (type);
3420 tree rhstype;
3421 enum tree_code coder;
3422 tree rname = NULL_TREE;
3424 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3426 tree selector;
3427 /* Change pointer to function to the function itself for
3428 diagnostics. */
3429 if (TREE_CODE (function) == ADDR_EXPR
3430 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3431 function = TREE_OPERAND (function, 0);
3433 /* Handle an ObjC selector specially for diagnostics. */
3434 selector = objc_message_selector ();
3435 rname = function;
3436 if (selector && parmnum > 2)
3438 rname = selector;
3439 parmnum -= 2;
3443 /* This macro is used to emit diagnostics to ensure that all format
3444 strings are complete sentences, visible to gettext and checked at
3445 compile time. */
3446 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3447 do { \
3448 switch (errtype) \
3450 case ic_argpass: \
3451 pedwarn (AR, parmnum, rname); \
3452 break; \
3453 case ic_argpass_nonproto: \
3454 warning (AR, parmnum, rname); \
3455 break; \
3456 case ic_assign: \
3457 pedwarn (AS); \
3458 break; \
3459 case ic_init: \
3460 pedwarn (IN); \
3461 break; \
3462 case ic_return: \
3463 pedwarn (RE); \
3464 break; \
3465 default: \
3466 gcc_unreachable (); \
3468 } while (0)
3470 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3471 /* Do not use STRIP_NOPS here. We do not want an enumerator
3472 whose value is 0 to count as a null pointer constant. */
3473 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3474 rhs = TREE_OPERAND (rhs, 0);
3476 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3477 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3478 rhs = default_conversion (rhs);
3479 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3480 rhs = decl_constant_value_for_broken_optimization (rhs);
3482 rhstype = TREE_TYPE (rhs);
3483 coder = TREE_CODE (rhstype);
3485 if (coder == ERROR_MARK)
3486 return error_mark_node;
3488 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3490 overflow_warning (rhs);
3491 /* Check for Objective-C protocols. This will automatically
3492 issue a warning if there are protocol violations. No need to
3493 use the return value. */
3494 if (c_dialect_objc ())
3495 objc_comptypes (type, rhstype, 0);
3496 return rhs;
3499 if (coder == VOID_TYPE)
3501 /* Except for passing an argument to an unprototyped function,
3502 this is a constraint violation. When passing an argument to
3503 an unprototyped function, it is compile-time undefined;
3504 making it a constraint in that case was rejected in
3505 DR#252. */
3506 error ("void value not ignored as it ought to be");
3507 return error_mark_node;
3509 /* A type converts to a reference to it.
3510 This code doesn't fully support references, it's just for the
3511 special case of va_start and va_copy. */
3512 if (codel == REFERENCE_TYPE
3513 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3515 if (!lvalue_p (rhs))
3517 error ("cannot pass rvalue to reference parameter");
3518 return error_mark_node;
3520 if (!c_mark_addressable (rhs))
3521 return error_mark_node;
3522 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3524 /* We already know that these two types are compatible, but they
3525 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3526 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3527 likely to be va_list, a typedef to __builtin_va_list, which
3528 is different enough that it will cause problems later. */
3529 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3530 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3532 rhs = build1 (NOP_EXPR, type, rhs);
3533 return rhs;
3535 /* Some types can interconvert without explicit casts. */
3536 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3537 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3538 return convert (type, rhs);
3539 /* Arithmetic types all interconvert, and enum is treated like int. */
3540 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3541 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3542 || codel == BOOLEAN_TYPE)
3543 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3544 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3545 || coder == BOOLEAN_TYPE))
3546 return convert_and_check (type, rhs);
3548 /* Conversion to a transparent union from its member types.
3549 This applies only to function arguments. */
3550 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3551 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3553 tree memb_types;
3554 tree marginal_memb_type = 0;
3556 for (memb_types = TYPE_FIELDS (type); memb_types;
3557 memb_types = TREE_CHAIN (memb_types))
3559 tree memb_type = TREE_TYPE (memb_types);
3561 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3562 TYPE_MAIN_VARIANT (rhstype)))
3563 break;
3565 if (TREE_CODE (memb_type) != POINTER_TYPE)
3566 continue;
3568 if (coder == POINTER_TYPE)
3570 tree ttl = TREE_TYPE (memb_type);
3571 tree ttr = TREE_TYPE (rhstype);
3573 /* Any non-function converts to a [const][volatile] void *
3574 and vice versa; otherwise, targets must be the same.
3575 Meanwhile, the lhs target must have all the qualifiers of
3576 the rhs. */
3577 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3578 || comp_target_types (memb_type, rhstype, 0))
3580 /* If this type won't generate any warnings, use it. */
3581 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3582 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3583 && TREE_CODE (ttl) == FUNCTION_TYPE)
3584 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3585 == TYPE_QUALS (ttr))
3586 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3587 == TYPE_QUALS (ttl))))
3588 break;
3590 /* Keep looking for a better type, but remember this one. */
3591 if (!marginal_memb_type)
3592 marginal_memb_type = memb_type;
3596 /* Can convert integer zero to any pointer type. */
3597 if (integer_zerop (rhs)
3598 || (TREE_CODE (rhs) == NOP_EXPR
3599 && integer_zerop (TREE_OPERAND (rhs, 0))))
3601 rhs = null_pointer_node;
3602 break;
3606 if (memb_types || marginal_memb_type)
3608 if (!memb_types)
3610 /* We have only a marginally acceptable member type;
3611 it needs a warning. */
3612 tree ttl = TREE_TYPE (marginal_memb_type);
3613 tree ttr = TREE_TYPE (rhstype);
3615 /* Const and volatile mean something different for function
3616 types, so the usual warnings are not appropriate. */
3617 if (TREE_CODE (ttr) == FUNCTION_TYPE
3618 && TREE_CODE (ttl) == FUNCTION_TYPE)
3620 /* Because const and volatile on functions are
3621 restrictions that say the function will not do
3622 certain things, it is okay to use a const or volatile
3623 function where an ordinary one is wanted, but not
3624 vice-versa. */
3625 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3626 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3627 "makes qualified function "
3628 "pointer from unqualified"),
3629 N_("assignment makes qualified "
3630 "function pointer from "
3631 "unqualified"),
3632 N_("initialization makes qualified "
3633 "function pointer from "
3634 "unqualified"),
3635 N_("return makes qualified function "
3636 "pointer from unqualified"));
3638 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3639 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3640 "qualifiers from pointer target type"),
3641 N_("assignment discards qualifiers "
3642 "from pointer target type"),
3643 N_("initialization discards qualifiers "
3644 "from pointer target type"),
3645 N_("return discards qualifiers from "
3646 "pointer target type"));
3649 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3650 pedwarn ("ISO C prohibits argument conversion to union type");
3652 return build1 (NOP_EXPR, type, rhs);
3656 /* Conversions among pointers */
3657 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3658 && (coder == codel))
3660 tree ttl = TREE_TYPE (type);
3661 tree ttr = TREE_TYPE (rhstype);
3662 bool is_opaque_pointer;
3663 int target_cmp = 0; /* Cache comp_target_types () result. */
3665 /* Opaque pointers are treated like void pointers. */
3666 is_opaque_pointer = (targetm.vector_opaque_p (type)
3667 || targetm.vector_opaque_p (rhstype))
3668 && TREE_CODE (ttl) == VECTOR_TYPE
3669 && TREE_CODE (ttr) == VECTOR_TYPE;
3671 /* Any non-function converts to a [const][volatile] void *
3672 and vice versa; otherwise, targets must be the same.
3673 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3674 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3675 || (target_cmp = comp_target_types (type, rhstype, 0))
3676 || is_opaque_pointer
3677 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3678 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3680 if (pedantic
3681 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3683 (VOID_TYPE_P (ttr)
3684 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3685 which are not ANSI null ptr constants. */
3686 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3687 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3688 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3689 "%qE between function pointer "
3690 "and %<void *%>"),
3691 N_("ISO C forbids assignment between "
3692 "function pointer and %<void *%>"),
3693 N_("ISO C forbids initialization between "
3694 "function pointer and %<void *%>"),
3695 N_("ISO C forbids return between function "
3696 "pointer and %<void *%>"));
3697 /* Const and volatile mean something different for function types,
3698 so the usual warnings are not appropriate. */
3699 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3700 && TREE_CODE (ttl) != FUNCTION_TYPE)
3702 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3703 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3704 "qualifiers from pointer target type"),
3705 N_("assignment discards qualifiers "
3706 "from pointer target type"),
3707 N_("initialization discards qualifiers "
3708 "from pointer target type"),
3709 N_("return discards qualifiers from "
3710 "pointer target type"));
3711 /* If this is not a case of ignoring a mismatch in signedness,
3712 no warning. */
3713 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3714 || target_cmp)
3716 /* If there is a mismatch, do warn. */
3717 else
3718 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3719 "%d of %qE differ in signedness"),
3720 N_("pointer targets in assignment "
3721 "differ in signedness"),
3722 N_("pointer targets in initialization "
3723 "differ in signedness"),
3724 N_("pointer targets in return differ "
3725 "in signedness"));
3727 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3728 && TREE_CODE (ttr) == FUNCTION_TYPE)
3730 /* Because const and volatile on functions are restrictions
3731 that say the function will not do certain things,
3732 it is okay to use a const or volatile function
3733 where an ordinary one is wanted, but not vice-versa. */
3734 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3735 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3736 "qualified function pointer "
3737 "from unqualified"),
3738 N_("assignment makes qualified function "
3739 "pointer from unqualified"),
3740 N_("initialization makes qualified "
3741 "function pointer from unqualified"),
3742 N_("return makes qualified function "
3743 "pointer from unqualified"));
3746 else
3747 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3748 "incompatible pointer type"),
3749 N_("assignment from incompatible pointer type"),
3750 N_("initialization from incompatible "
3751 "pointer type"),
3752 N_("return from incompatible pointer type"));
3753 return convert (type, rhs);
3755 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3757 /* ??? This should not be an error when inlining calls to
3758 unprototyped functions. */
3759 error ("invalid use of non-lvalue array");
3760 return error_mark_node;
3762 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3764 /* An explicit constant 0 can convert to a pointer,
3765 or one that results from arithmetic, even including
3766 a cast to integer type. */
3767 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3769 !(TREE_CODE (rhs) == NOP_EXPR
3770 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3771 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3772 && integer_zerop (TREE_OPERAND (rhs, 0))))
3773 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3774 "pointer from integer without a cast"),
3775 N_("assignment makes pointer from integer "
3776 "without a cast"),
3777 N_("initialization makes pointer from "
3778 "integer without a cast"),
3779 N_("return makes pointer from integer "
3780 "without a cast"));
3782 return convert (type, rhs);
3784 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3786 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3787 "from pointer without a cast"),
3788 N_("assignment makes integer from pointer "
3789 "without a cast"),
3790 N_("initialization makes integer from pointer "
3791 "without a cast"),
3792 N_("return makes integer from pointer "
3793 "without a cast"));
3794 return convert (type, rhs);
3796 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3797 return convert (type, rhs);
3799 switch (errtype)
3801 case ic_argpass:
3802 case ic_argpass_nonproto:
3803 /* ??? This should not be an error when inlining calls to
3804 unprototyped functions. */
3805 error ("incompatible type for argument %d of %qE", parmnum, rname);
3806 break;
3807 case ic_assign:
3808 error ("incompatible types in assignment");
3809 break;
3810 case ic_init:
3811 error ("incompatible types in initialization");
3812 break;
3813 case ic_return:
3814 error ("incompatible types in return");
3815 break;
3816 default:
3817 gcc_unreachable ();
3820 return error_mark_node;
3823 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3824 is used for error and waring reporting and indicates which argument
3825 is being processed. */
3827 tree
3828 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3830 tree ret, type;
3832 /* If FN was prototyped, the value has been converted already
3833 in convert_arguments. */
3834 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3835 return value;
3837 type = TREE_TYPE (parm);
3838 ret = convert_for_assignment (type, value,
3839 ic_argpass_nonproto, fn,
3840 fn, argnum);
3841 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3842 && INTEGRAL_TYPE_P (type)
3843 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3844 ret = default_conversion (ret);
3845 return ret;
3848 /* If VALUE is a compound expr all of whose expressions are constant, then
3849 return its value. Otherwise, return error_mark_node.
3851 This is for handling COMPOUND_EXPRs as initializer elements
3852 which is allowed with a warning when -pedantic is specified. */
3854 static tree
3855 valid_compound_expr_initializer (tree value, tree endtype)
3857 if (TREE_CODE (value) == COMPOUND_EXPR)
3859 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3860 == error_mark_node)
3861 return error_mark_node;
3862 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3863 endtype);
3865 else if (!initializer_constant_valid_p (value, endtype))
3866 return error_mark_node;
3867 else
3868 return value;
3871 /* Perform appropriate conversions on the initial value of a variable,
3872 store it in the declaration DECL,
3873 and print any error messages that are appropriate.
3874 If the init is invalid, store an ERROR_MARK. */
3876 void
3877 store_init_value (tree decl, tree init)
3879 tree value, type;
3881 /* If variable's type was invalidly declared, just ignore it. */
3883 type = TREE_TYPE (decl);
3884 if (TREE_CODE (type) == ERROR_MARK)
3885 return;
3887 /* Digest the specified initializer into an expression. */
3889 value = digest_init (type, init, true, TREE_STATIC (decl));
3891 /* Store the expression if valid; else report error. */
3893 if (warn_traditional && !in_system_header
3894 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3895 warning ("traditional C rejects automatic aggregate initialization");
3897 DECL_INITIAL (decl) = value;
3899 /* ANSI wants warnings about out-of-range constant initializers. */
3900 STRIP_TYPE_NOPS (value);
3901 constant_expression_warning (value);
3903 /* Check if we need to set array size from compound literal size. */
3904 if (TREE_CODE (type) == ARRAY_TYPE
3905 && TYPE_DOMAIN (type) == 0
3906 && value != error_mark_node)
3908 tree inside_init = init;
3910 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3911 inside_init = TREE_OPERAND (init, 0);
3912 inside_init = fold (inside_init);
3914 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3916 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3918 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3920 /* For int foo[] = (int [3]){1}; we need to set array size
3921 now since later on array initializer will be just the
3922 brace enclosed list of the compound literal. */
3923 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3924 layout_type (type);
3925 layout_decl (decl, 0);
3931 /* Methods for storing and printing names for error messages. */
3933 /* Implement a spelling stack that allows components of a name to be pushed
3934 and popped. Each element on the stack is this structure. */
3936 struct spelling
3938 int kind;
3939 union
3941 int i;
3942 const char *s;
3943 } u;
3946 #define SPELLING_STRING 1
3947 #define SPELLING_MEMBER 2
3948 #define SPELLING_BOUNDS 3
3950 static struct spelling *spelling; /* Next stack element (unused). */
3951 static struct spelling *spelling_base; /* Spelling stack base. */
3952 static int spelling_size; /* Size of the spelling stack. */
3954 /* Macros to save and restore the spelling stack around push_... functions.
3955 Alternative to SAVE_SPELLING_STACK. */
3957 #define SPELLING_DEPTH() (spelling - spelling_base)
3958 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3960 /* Push an element on the spelling stack with type KIND and assign VALUE
3961 to MEMBER. */
3963 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3965 int depth = SPELLING_DEPTH (); \
3967 if (depth >= spelling_size) \
3969 spelling_size += 10; \
3970 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3971 spelling_size); \
3972 RESTORE_SPELLING_DEPTH (depth); \
3975 spelling->kind = (KIND); \
3976 spelling->MEMBER = (VALUE); \
3977 spelling++; \
3980 /* Push STRING on the stack. Printed literally. */
3982 static void
3983 push_string (const char *string)
3985 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3988 /* Push a member name on the stack. Printed as '.' STRING. */
3990 static void
3991 push_member_name (tree decl)
3993 const char *const string
3994 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3995 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3998 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4000 static void
4001 push_array_bounds (int bounds)
4003 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4006 /* Compute the maximum size in bytes of the printed spelling. */
4008 static int
4009 spelling_length (void)
4011 int size = 0;
4012 struct spelling *p;
4014 for (p = spelling_base; p < spelling; p++)
4016 if (p->kind == SPELLING_BOUNDS)
4017 size += 25;
4018 else
4019 size += strlen (p->u.s) + 1;
4022 return size;
4025 /* Print the spelling to BUFFER and return it. */
4027 static char *
4028 print_spelling (char *buffer)
4030 char *d = buffer;
4031 struct spelling *p;
4033 for (p = spelling_base; p < spelling; p++)
4034 if (p->kind == SPELLING_BOUNDS)
4036 sprintf (d, "[%d]", p->u.i);
4037 d += strlen (d);
4039 else
4041 const char *s;
4042 if (p->kind == SPELLING_MEMBER)
4043 *d++ = '.';
4044 for (s = p->u.s; (*d = *s++); d++)
4047 *d++ = '\0';
4048 return buffer;
4051 /* Issue an error message for a bad initializer component.
4052 MSGID identifies the message.
4053 The component name is taken from the spelling stack. */
4055 void
4056 error_init (const char *msgid)
4058 char *ofwhat;
4060 error ("%s", _(msgid));
4061 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4062 if (*ofwhat)
4063 error ("(near initialization for %qs)", ofwhat);
4066 /* Issue a pedantic warning for a bad initializer component.
4067 MSGID identifies the message.
4068 The component name is taken from the spelling stack. */
4070 void
4071 pedwarn_init (const char *msgid)
4073 char *ofwhat;
4075 pedwarn ("%s", _(msgid));
4076 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4077 if (*ofwhat)
4078 pedwarn ("(near initialization for %qs)", ofwhat);
4081 /* Issue a warning for a bad initializer component.
4082 MSGID identifies the message.
4083 The component name is taken from the spelling stack. */
4085 static void
4086 warning_init (const char *msgid)
4088 char *ofwhat;
4090 warning ("%s", _(msgid));
4091 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4092 if (*ofwhat)
4093 warning ("(near initialization for %qs)", ofwhat);
4096 /* If TYPE is an array type and EXPR is a parenthesized string
4097 constant, warn if pedantic that EXPR is being used to initialize an
4098 object of type TYPE. */
4100 void
4101 maybe_warn_string_init (tree type, struct c_expr expr)
4103 if (pedantic
4104 && TREE_CODE (type) == ARRAY_TYPE
4105 && TREE_CODE (expr.value) == STRING_CST
4106 && expr.original_code != STRING_CST)
4107 pedwarn_init ("array initialized from parenthesized string constant");
4110 /* Digest the parser output INIT as an initializer for type TYPE.
4111 Return a C expression of type TYPE to represent the initial value.
4113 If INIT is a string constant, STRICT_STRING is true if it is
4114 unparenthesized or we should not warn here for it being parenthesized.
4115 For other types of INIT, STRICT_STRING is not used.
4117 REQUIRE_CONSTANT requests an error if non-constant initializers or
4118 elements are seen. */
4120 static tree
4121 digest_init (tree type, tree init, bool strict_string, int require_constant)
4123 enum tree_code code = TREE_CODE (type);
4124 tree inside_init = init;
4126 if (type == error_mark_node
4127 || init == error_mark_node
4128 || TREE_TYPE (init) == error_mark_node)
4129 return error_mark_node;
4131 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4132 /* Do not use STRIP_NOPS here. We do not want an enumerator
4133 whose value is 0 to count as a null pointer constant. */
4134 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4135 inside_init = TREE_OPERAND (init, 0);
4137 inside_init = fold (inside_init);
4139 /* Initialization of an array of chars from a string constant
4140 optionally enclosed in braces. */
4142 if (code == ARRAY_TYPE && inside_init
4143 && TREE_CODE (inside_init) == STRING_CST)
4145 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4146 /* Note that an array could be both an array of character type
4147 and an array of wchar_t if wchar_t is signed char or unsigned
4148 char. */
4149 bool char_array = (typ1 == char_type_node
4150 || typ1 == signed_char_type_node
4151 || typ1 == unsigned_char_type_node);
4152 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4153 if (char_array || wchar_array)
4155 struct c_expr expr;
4156 bool char_string;
4157 expr.value = inside_init;
4158 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4159 maybe_warn_string_init (type, expr);
4161 char_string
4162 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4163 == char_type_node);
4165 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4166 TYPE_MAIN_VARIANT (type)))
4167 return inside_init;
4169 if (!wchar_array && !char_string)
4171 error_init ("char-array initialized from wide string");
4172 return error_mark_node;
4174 if (char_string && !char_array)
4176 error_init ("wchar_t-array initialized from non-wide string");
4177 return error_mark_node;
4180 TREE_TYPE (inside_init) = type;
4181 if (TYPE_DOMAIN (type) != 0
4182 && TYPE_SIZE (type) != 0
4183 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4184 /* Subtract 1 (or sizeof (wchar_t))
4185 because it's ok to ignore the terminating null char
4186 that is counted in the length of the constant. */
4187 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4188 TREE_STRING_LENGTH (inside_init)
4189 - ((TYPE_PRECISION (typ1)
4190 != TYPE_PRECISION (char_type_node))
4191 ? (TYPE_PRECISION (wchar_type_node)
4192 / BITS_PER_UNIT)
4193 : 1)))
4194 pedwarn_init ("initializer-string for array of chars is too long");
4196 return inside_init;
4198 else if (INTEGRAL_TYPE_P (typ1))
4200 error_init ("array of inappropriate type initialized "
4201 "from string constant");
4202 return error_mark_node;
4206 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4207 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4208 below and handle as a constructor. */
4209 if (code == VECTOR_TYPE
4210 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4211 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4212 && TREE_CONSTANT (inside_init))
4214 if (TREE_CODE (inside_init) == VECTOR_CST
4215 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4216 TYPE_MAIN_VARIANT (type)))
4217 return inside_init;
4218 else
4219 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4222 /* Any type can be initialized
4223 from an expression of the same type, optionally with braces. */
4225 if (inside_init && TREE_TYPE (inside_init) != 0
4226 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4227 TYPE_MAIN_VARIANT (type))
4228 || (code == ARRAY_TYPE
4229 && comptypes (TREE_TYPE (inside_init), type))
4230 || (code == VECTOR_TYPE
4231 && comptypes (TREE_TYPE (inside_init), type))
4232 || (code == POINTER_TYPE
4233 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4234 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4235 TREE_TYPE (type)))
4236 || (code == POINTER_TYPE
4237 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4238 && comptypes (TREE_TYPE (inside_init),
4239 TREE_TYPE (type)))))
4241 if (code == POINTER_TYPE)
4243 inside_init = default_function_array_conversion (inside_init);
4245 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4247 error_init ("invalid use of non-lvalue array");
4248 return error_mark_node;
4252 if (code == VECTOR_TYPE)
4253 /* Although the types are compatible, we may require a
4254 conversion. */
4255 inside_init = convert (type, inside_init);
4257 if (require_constant && !flag_isoc99
4258 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4260 /* As an extension, allow initializing objects with static storage
4261 duration with compound literals (which are then treated just as
4262 the brace enclosed list they contain). */
4263 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4264 inside_init = DECL_INITIAL (decl);
4267 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4268 && TREE_CODE (inside_init) != CONSTRUCTOR)
4270 error_init ("array initialized from non-constant array expression");
4271 return error_mark_node;
4274 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4275 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4277 /* Compound expressions can only occur here if -pedantic or
4278 -pedantic-errors is specified. In the later case, we always want
4279 an error. In the former case, we simply want a warning. */
4280 if (require_constant && pedantic
4281 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4283 inside_init
4284 = valid_compound_expr_initializer (inside_init,
4285 TREE_TYPE (inside_init));
4286 if (inside_init == error_mark_node)
4287 error_init ("initializer element is not constant");
4288 else
4289 pedwarn_init ("initializer element is not constant");
4290 if (flag_pedantic_errors)
4291 inside_init = error_mark_node;
4293 else if (require_constant
4294 && !initializer_constant_valid_p (inside_init,
4295 TREE_TYPE (inside_init)))
4297 error_init ("initializer element is not constant");
4298 inside_init = error_mark_node;
4301 return inside_init;
4304 /* Handle scalar types, including conversions. */
4306 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4307 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4308 || code == VECTOR_TYPE)
4310 /* Note that convert_for_assignment calls default_conversion
4311 for arrays and functions. We must not call it in the
4312 case where inside_init is a null pointer constant. */
4313 inside_init
4314 = convert_for_assignment (type, init, ic_init,
4315 NULL_TREE, NULL_TREE, 0);
4317 /* Check to see if we have already given an error message. */
4318 if (inside_init == error_mark_node)
4320 else if (require_constant && !TREE_CONSTANT (inside_init))
4322 error_init ("initializer element is not constant");
4323 inside_init = error_mark_node;
4325 else if (require_constant
4326 && !initializer_constant_valid_p (inside_init,
4327 TREE_TYPE (inside_init)))
4329 error_init ("initializer element is not computable at load time");
4330 inside_init = error_mark_node;
4333 return inside_init;
4336 /* Come here only for records and arrays. */
4338 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4340 error_init ("variable-sized object may not be initialized");
4341 return error_mark_node;
4344 error_init ("invalid initializer");
4345 return error_mark_node;
4348 /* Handle initializers that use braces. */
4350 /* Type of object we are accumulating a constructor for.
4351 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4352 static tree constructor_type;
4354 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4355 left to fill. */
4356 static tree constructor_fields;
4358 /* For an ARRAY_TYPE, this is the specified index
4359 at which to store the next element we get. */
4360 static tree constructor_index;
4362 /* For an ARRAY_TYPE, this is the maximum index. */
4363 static tree constructor_max_index;
4365 /* For a RECORD_TYPE, this is the first field not yet written out. */
4366 static tree constructor_unfilled_fields;
4368 /* For an ARRAY_TYPE, this is the index of the first element
4369 not yet written out. */
4370 static tree constructor_unfilled_index;
4372 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4373 This is so we can generate gaps between fields, when appropriate. */
4374 static tree constructor_bit_index;
4376 /* If we are saving up the elements rather than allocating them,
4377 this is the list of elements so far (in reverse order,
4378 most recent first). */
4379 static tree constructor_elements;
4381 /* 1 if constructor should be incrementally stored into a constructor chain,
4382 0 if all the elements should be kept in AVL tree. */
4383 static int constructor_incremental;
4385 /* 1 if so far this constructor's elements are all compile-time constants. */
4386 static int constructor_constant;
4388 /* 1 if so far this constructor's elements are all valid address constants. */
4389 static int constructor_simple;
4391 /* 1 if this constructor is erroneous so far. */
4392 static int constructor_erroneous;
4394 /* Structure for managing pending initializer elements, organized as an
4395 AVL tree. */
4397 struct init_node
4399 struct init_node *left, *right;
4400 struct init_node *parent;
4401 int balance;
4402 tree purpose;
4403 tree value;
4406 /* Tree of pending elements at this constructor level.
4407 These are elements encountered out of order
4408 which belong at places we haven't reached yet in actually
4409 writing the output.
4410 Will never hold tree nodes across GC runs. */
4411 static struct init_node *constructor_pending_elts;
4413 /* The SPELLING_DEPTH of this constructor. */
4414 static int constructor_depth;
4416 /* 0 if implicitly pushing constructor levels is allowed. */
4417 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4419 /* DECL node for which an initializer is being read.
4420 0 means we are reading a constructor expression
4421 such as (struct foo) {...}. */
4422 static tree constructor_decl;
4424 /* Nonzero if this is an initializer for a top-level decl. */
4425 static int constructor_top_level;
4427 /* Nonzero if there were any member designators in this initializer. */
4428 static int constructor_designated;
4430 /* Nesting depth of designator list. */
4431 static int designator_depth;
4433 /* Nonzero if there were diagnosed errors in this designator list. */
4434 static int designator_errorneous;
4437 /* This stack has a level for each implicit or explicit level of
4438 structuring in the initializer, including the outermost one. It
4439 saves the values of most of the variables above. */
4441 struct constructor_range_stack;
4443 struct constructor_stack
4445 struct constructor_stack *next;
4446 tree type;
4447 tree fields;
4448 tree index;
4449 tree max_index;
4450 tree unfilled_index;
4451 tree unfilled_fields;
4452 tree bit_index;
4453 tree elements;
4454 struct init_node *pending_elts;
4455 int offset;
4456 int depth;
4457 /* If value nonzero, this value should replace the entire
4458 constructor at this level. */
4459 struct c_expr replacement_value;
4460 struct constructor_range_stack *range_stack;
4461 char constant;
4462 char simple;
4463 char implicit;
4464 char erroneous;
4465 char outer;
4466 char incremental;
4467 char designated;
4470 struct constructor_stack *constructor_stack;
4472 /* This stack represents designators from some range designator up to
4473 the last designator in the list. */
4475 struct constructor_range_stack
4477 struct constructor_range_stack *next, *prev;
4478 struct constructor_stack *stack;
4479 tree range_start;
4480 tree index;
4481 tree range_end;
4482 tree fields;
4485 struct constructor_range_stack *constructor_range_stack;
4487 /* This stack records separate initializers that are nested.
4488 Nested initializers can't happen in ANSI C, but GNU C allows them
4489 in cases like { ... (struct foo) { ... } ... }. */
4491 struct initializer_stack
4493 struct initializer_stack *next;
4494 tree decl;
4495 struct constructor_stack *constructor_stack;
4496 struct constructor_range_stack *constructor_range_stack;
4497 tree elements;
4498 struct spelling *spelling;
4499 struct spelling *spelling_base;
4500 int spelling_size;
4501 char top_level;
4502 char require_constant_value;
4503 char require_constant_elements;
4506 struct initializer_stack *initializer_stack;
4508 /* Prepare to parse and output the initializer for variable DECL. */
4510 void
4511 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4513 const char *locus;
4514 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4516 p->decl = constructor_decl;
4517 p->require_constant_value = require_constant_value;
4518 p->require_constant_elements = require_constant_elements;
4519 p->constructor_stack = constructor_stack;
4520 p->constructor_range_stack = constructor_range_stack;
4521 p->elements = constructor_elements;
4522 p->spelling = spelling;
4523 p->spelling_base = spelling_base;
4524 p->spelling_size = spelling_size;
4525 p->top_level = constructor_top_level;
4526 p->next = initializer_stack;
4527 initializer_stack = p;
4529 constructor_decl = decl;
4530 constructor_designated = 0;
4531 constructor_top_level = top_level;
4533 if (decl != 0)
4535 require_constant_value = TREE_STATIC (decl);
4536 require_constant_elements
4537 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4538 /* For a scalar, you can always use any value to initialize,
4539 even within braces. */
4540 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4541 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4542 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4543 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4544 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4546 else
4548 require_constant_value = 0;
4549 require_constant_elements = 0;
4550 locus = "(anonymous)";
4553 constructor_stack = 0;
4554 constructor_range_stack = 0;
4556 missing_braces_mentioned = 0;
4558 spelling_base = 0;
4559 spelling_size = 0;
4560 RESTORE_SPELLING_DEPTH (0);
4562 if (locus)
4563 push_string (locus);
4566 void
4567 finish_init (void)
4569 struct initializer_stack *p = initializer_stack;
4571 /* Free the whole constructor stack of this initializer. */
4572 while (constructor_stack)
4574 struct constructor_stack *q = constructor_stack;
4575 constructor_stack = q->next;
4576 free (q);
4579 gcc_assert (!constructor_range_stack);
4581 /* Pop back to the data of the outer initializer (if any). */
4582 free (spelling_base);
4584 constructor_decl = p->decl;
4585 require_constant_value = p->require_constant_value;
4586 require_constant_elements = p->require_constant_elements;
4587 constructor_stack = p->constructor_stack;
4588 constructor_range_stack = p->constructor_range_stack;
4589 constructor_elements = p->elements;
4590 spelling = p->spelling;
4591 spelling_base = p->spelling_base;
4592 spelling_size = p->spelling_size;
4593 constructor_top_level = p->top_level;
4594 initializer_stack = p->next;
4595 free (p);
4598 /* Call here when we see the initializer is surrounded by braces.
4599 This is instead of a call to push_init_level;
4600 it is matched by a call to pop_init_level.
4602 TYPE is the type to initialize, for a constructor expression.
4603 For an initializer for a decl, TYPE is zero. */
4605 void
4606 really_start_incremental_init (tree type)
4608 struct constructor_stack *p = XNEW (struct constructor_stack);
4610 if (type == 0)
4611 type = TREE_TYPE (constructor_decl);
4613 if (targetm.vector_opaque_p (type))
4614 error ("opaque vector types cannot be initialized");
4616 p->type = constructor_type;
4617 p->fields = constructor_fields;
4618 p->index = constructor_index;
4619 p->max_index = constructor_max_index;
4620 p->unfilled_index = constructor_unfilled_index;
4621 p->unfilled_fields = constructor_unfilled_fields;
4622 p->bit_index = constructor_bit_index;
4623 p->elements = constructor_elements;
4624 p->constant = constructor_constant;
4625 p->simple = constructor_simple;
4626 p->erroneous = constructor_erroneous;
4627 p->pending_elts = constructor_pending_elts;
4628 p->depth = constructor_depth;
4629 p->replacement_value.value = 0;
4630 p->replacement_value.original_code = ERROR_MARK;
4631 p->implicit = 0;
4632 p->range_stack = 0;
4633 p->outer = 0;
4634 p->incremental = constructor_incremental;
4635 p->designated = constructor_designated;
4636 p->next = 0;
4637 constructor_stack = p;
4639 constructor_constant = 1;
4640 constructor_simple = 1;
4641 constructor_depth = SPELLING_DEPTH ();
4642 constructor_elements = 0;
4643 constructor_pending_elts = 0;
4644 constructor_type = type;
4645 constructor_incremental = 1;
4646 constructor_designated = 0;
4647 designator_depth = 0;
4648 designator_errorneous = 0;
4650 if (TREE_CODE (constructor_type) == RECORD_TYPE
4651 || TREE_CODE (constructor_type) == UNION_TYPE)
4653 constructor_fields = TYPE_FIELDS (constructor_type);
4654 /* Skip any nameless bit fields at the beginning. */
4655 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4656 && DECL_NAME (constructor_fields) == 0)
4657 constructor_fields = TREE_CHAIN (constructor_fields);
4659 constructor_unfilled_fields = constructor_fields;
4660 constructor_bit_index = bitsize_zero_node;
4662 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4664 if (TYPE_DOMAIN (constructor_type))
4666 constructor_max_index
4667 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4669 /* Detect non-empty initializations of zero-length arrays. */
4670 if (constructor_max_index == NULL_TREE
4671 && TYPE_SIZE (constructor_type))
4672 constructor_max_index = build_int_cst (NULL_TREE, -1);
4674 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4675 to initialize VLAs will cause a proper error; avoid tree
4676 checking errors as well by setting a safe value. */
4677 if (constructor_max_index
4678 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4679 constructor_max_index = build_int_cst (NULL_TREE, -1);
4681 constructor_index
4682 = convert (bitsizetype,
4683 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4685 else
4686 constructor_index = bitsize_zero_node;
4688 constructor_unfilled_index = constructor_index;
4690 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4692 /* Vectors are like simple fixed-size arrays. */
4693 constructor_max_index =
4694 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4695 constructor_index = convert (bitsizetype, bitsize_zero_node);
4696 constructor_unfilled_index = constructor_index;
4698 else
4700 /* Handle the case of int x = {5}; */
4701 constructor_fields = constructor_type;
4702 constructor_unfilled_fields = constructor_type;
4706 /* Push down into a subobject, for initialization.
4707 If this is for an explicit set of braces, IMPLICIT is 0.
4708 If it is because the next element belongs at a lower level,
4709 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4711 void
4712 push_init_level (int implicit)
4714 struct constructor_stack *p;
4715 tree value = NULL_TREE;
4717 /* If we've exhausted any levels that didn't have braces,
4718 pop them now. */
4719 while (constructor_stack->implicit)
4721 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4722 || TREE_CODE (constructor_type) == UNION_TYPE)
4723 && constructor_fields == 0)
4724 process_init_element (pop_init_level (1));
4725 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4726 && constructor_max_index
4727 && tree_int_cst_lt (constructor_max_index, constructor_index))
4728 process_init_element (pop_init_level (1));
4729 else
4730 break;
4733 /* Unless this is an explicit brace, we need to preserve previous
4734 content if any. */
4735 if (implicit)
4737 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4738 || TREE_CODE (constructor_type) == UNION_TYPE)
4739 && constructor_fields)
4740 value = find_init_member (constructor_fields);
4741 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4742 value = find_init_member (constructor_index);
4745 p = XNEW (struct constructor_stack);
4746 p->type = constructor_type;
4747 p->fields = constructor_fields;
4748 p->index = constructor_index;
4749 p->max_index = constructor_max_index;
4750 p->unfilled_index = constructor_unfilled_index;
4751 p->unfilled_fields = constructor_unfilled_fields;
4752 p->bit_index = constructor_bit_index;
4753 p->elements = constructor_elements;
4754 p->constant = constructor_constant;
4755 p->simple = constructor_simple;
4756 p->erroneous = constructor_erroneous;
4757 p->pending_elts = constructor_pending_elts;
4758 p->depth = constructor_depth;
4759 p->replacement_value.value = 0;
4760 p->replacement_value.original_code = ERROR_MARK;
4761 p->implicit = implicit;
4762 p->outer = 0;
4763 p->incremental = constructor_incremental;
4764 p->designated = constructor_designated;
4765 p->next = constructor_stack;
4766 p->range_stack = 0;
4767 constructor_stack = p;
4769 constructor_constant = 1;
4770 constructor_simple = 1;
4771 constructor_depth = SPELLING_DEPTH ();
4772 constructor_elements = 0;
4773 constructor_incremental = 1;
4774 constructor_designated = 0;
4775 constructor_pending_elts = 0;
4776 if (!implicit)
4778 p->range_stack = constructor_range_stack;
4779 constructor_range_stack = 0;
4780 designator_depth = 0;
4781 designator_errorneous = 0;
4784 /* Don't die if an entire brace-pair level is superfluous
4785 in the containing level. */
4786 if (constructor_type == 0)
4788 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4789 || TREE_CODE (constructor_type) == UNION_TYPE)
4791 /* Don't die if there are extra init elts at the end. */
4792 if (constructor_fields == 0)
4793 constructor_type = 0;
4794 else
4796 constructor_type = TREE_TYPE (constructor_fields);
4797 push_member_name (constructor_fields);
4798 constructor_depth++;
4801 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4803 constructor_type = TREE_TYPE (constructor_type);
4804 push_array_bounds (tree_low_cst (constructor_index, 0));
4805 constructor_depth++;
4808 if (constructor_type == 0)
4810 error_init ("extra brace group at end of initializer");
4811 constructor_fields = 0;
4812 constructor_unfilled_fields = 0;
4813 return;
4816 if (value && TREE_CODE (value) == CONSTRUCTOR)
4818 constructor_constant = TREE_CONSTANT (value);
4819 constructor_simple = TREE_STATIC (value);
4820 constructor_elements = CONSTRUCTOR_ELTS (value);
4821 if (constructor_elements
4822 && (TREE_CODE (constructor_type) == RECORD_TYPE
4823 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4824 set_nonincremental_init ();
4827 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4829 missing_braces_mentioned = 1;
4830 warning_init ("missing braces around initializer");
4833 if (TREE_CODE (constructor_type) == RECORD_TYPE
4834 || TREE_CODE (constructor_type) == UNION_TYPE)
4836 constructor_fields = TYPE_FIELDS (constructor_type);
4837 /* Skip any nameless bit fields at the beginning. */
4838 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4839 && DECL_NAME (constructor_fields) == 0)
4840 constructor_fields = TREE_CHAIN (constructor_fields);
4842 constructor_unfilled_fields = constructor_fields;
4843 constructor_bit_index = bitsize_zero_node;
4845 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4847 /* Vectors are like simple fixed-size arrays. */
4848 constructor_max_index =
4849 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4850 constructor_index = convert (bitsizetype, integer_zero_node);
4851 constructor_unfilled_index = constructor_index;
4853 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4855 if (TYPE_DOMAIN (constructor_type))
4857 constructor_max_index
4858 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4860 /* Detect non-empty initializations of zero-length arrays. */
4861 if (constructor_max_index == NULL_TREE
4862 && TYPE_SIZE (constructor_type))
4863 constructor_max_index = build_int_cst (NULL_TREE, -1);
4865 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4866 to initialize VLAs will cause a proper error; avoid tree
4867 checking errors as well by setting a safe value. */
4868 if (constructor_max_index
4869 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4870 constructor_max_index = build_int_cst (NULL_TREE, -1);
4872 constructor_index
4873 = convert (bitsizetype,
4874 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4876 else
4877 constructor_index = bitsize_zero_node;
4879 constructor_unfilled_index = constructor_index;
4880 if (value && TREE_CODE (value) == STRING_CST)
4882 /* We need to split the char/wchar array into individual
4883 characters, so that we don't have to special case it
4884 everywhere. */
4885 set_nonincremental_init_from_string (value);
4888 else
4890 warning_init ("braces around scalar initializer");
4891 constructor_fields = constructor_type;
4892 constructor_unfilled_fields = constructor_type;
4896 /* At the end of an implicit or explicit brace level,
4897 finish up that level of constructor. If a single expression
4898 with redundant braces initialized that level, return the
4899 c_expr structure for that expression. Otherwise, the original_code
4900 element is set to ERROR_MARK.
4901 If we were outputting the elements as they are read, return 0 as the value
4902 from inner levels (process_init_element ignores that),
4903 but return error_mark_node as the value from the outermost level
4904 (that's what we want to put in DECL_INITIAL).
4905 Otherwise, return a CONSTRUCTOR expression as the value. */
4907 struct c_expr
4908 pop_init_level (int implicit)
4910 struct constructor_stack *p;
4911 struct c_expr ret;
4912 ret.value = 0;
4913 ret.original_code = ERROR_MARK;
4915 if (implicit == 0)
4917 /* When we come to an explicit close brace,
4918 pop any inner levels that didn't have explicit braces. */
4919 while (constructor_stack->implicit)
4920 process_init_element (pop_init_level (1));
4922 gcc_assert (!constructor_range_stack);
4925 /* Now output all pending elements. */
4926 constructor_incremental = 1;
4927 output_pending_init_elements (1);
4929 p = constructor_stack;
4931 /* Error for initializing a flexible array member, or a zero-length
4932 array member in an inappropriate context. */
4933 if (constructor_type && constructor_fields
4934 && TREE_CODE (constructor_type) == ARRAY_TYPE
4935 && TYPE_DOMAIN (constructor_type)
4936 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4938 /* Silently discard empty initializations. The parser will
4939 already have pedwarned for empty brackets. */
4940 if (integer_zerop (constructor_unfilled_index))
4941 constructor_type = NULL_TREE;
4942 else
4944 gcc_assert (!TYPE_SIZE (constructor_type));
4946 if (constructor_depth > 2)
4947 error_init ("initialization of flexible array member in a nested context");
4948 else if (pedantic)
4949 pedwarn_init ("initialization of a flexible array member");
4951 /* We have already issued an error message for the existence
4952 of a flexible array member not at the end of the structure.
4953 Discard the initializer so that we do not abort later. */
4954 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4955 constructor_type = NULL_TREE;
4959 /* Warn when some struct elements are implicitly initialized to zero. */
4960 if (warn_missing_field_initializers
4961 && constructor_type
4962 && TREE_CODE (constructor_type) == RECORD_TYPE
4963 && constructor_unfilled_fields)
4965 /* Do not warn for flexible array members or zero-length arrays. */
4966 while (constructor_unfilled_fields
4967 && (!DECL_SIZE (constructor_unfilled_fields)
4968 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4969 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4971 /* Do not warn if this level of the initializer uses member
4972 designators; it is likely to be deliberate. */
4973 if (constructor_unfilled_fields && !constructor_designated)
4975 push_member_name (constructor_unfilled_fields);
4976 warning_init ("missing initializer");
4977 RESTORE_SPELLING_DEPTH (constructor_depth);
4981 /* Pad out the end of the structure. */
4982 if (p->replacement_value.value)
4983 /* If this closes a superfluous brace pair,
4984 just pass out the element between them. */
4985 ret = p->replacement_value;
4986 else if (constructor_type == 0)
4988 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4989 && TREE_CODE (constructor_type) != UNION_TYPE
4990 && TREE_CODE (constructor_type) != ARRAY_TYPE
4991 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4993 /* A nonincremental scalar initializer--just return
4994 the element, after verifying there is just one. */
4995 if (constructor_elements == 0)
4997 if (!constructor_erroneous)
4998 error_init ("empty scalar initializer");
4999 ret.value = error_mark_node;
5001 else if (TREE_CHAIN (constructor_elements) != 0)
5003 error_init ("extra elements in scalar initializer");
5004 ret.value = TREE_VALUE (constructor_elements);
5006 else
5007 ret.value = TREE_VALUE (constructor_elements);
5009 else
5011 if (constructor_erroneous)
5012 ret.value = error_mark_node;
5013 else
5015 ret.value = build_constructor (constructor_type,
5016 nreverse (constructor_elements));
5017 if (constructor_constant)
5018 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5019 if (constructor_constant && constructor_simple)
5020 TREE_STATIC (ret.value) = 1;
5024 constructor_type = p->type;
5025 constructor_fields = p->fields;
5026 constructor_index = p->index;
5027 constructor_max_index = p->max_index;
5028 constructor_unfilled_index = p->unfilled_index;
5029 constructor_unfilled_fields = p->unfilled_fields;
5030 constructor_bit_index = p->bit_index;
5031 constructor_elements = p->elements;
5032 constructor_constant = p->constant;
5033 constructor_simple = p->simple;
5034 constructor_erroneous = p->erroneous;
5035 constructor_incremental = p->incremental;
5036 constructor_designated = p->designated;
5037 constructor_pending_elts = p->pending_elts;
5038 constructor_depth = p->depth;
5039 if (!p->implicit)
5040 constructor_range_stack = p->range_stack;
5041 RESTORE_SPELLING_DEPTH (constructor_depth);
5043 constructor_stack = p->next;
5044 free (p);
5046 if (ret.value == 0)
5048 if (constructor_stack == 0)
5050 ret.value = error_mark_node;
5051 return ret;
5053 return ret;
5055 return ret;
5058 /* Common handling for both array range and field name designators.
5059 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5061 static int
5062 set_designator (int array)
5064 tree subtype;
5065 enum tree_code subcode;
5067 /* Don't die if an entire brace-pair level is superfluous
5068 in the containing level. */
5069 if (constructor_type == 0)
5070 return 1;
5072 /* If there were errors in this designator list already, bail out
5073 silently. */
5074 if (designator_errorneous)
5075 return 1;
5077 if (!designator_depth)
5079 gcc_assert (!constructor_range_stack);
5081 /* Designator list starts at the level of closest explicit
5082 braces. */
5083 while (constructor_stack->implicit)
5084 process_init_element (pop_init_level (1));
5085 constructor_designated = 1;
5086 return 0;
5089 if (constructor_no_implicit)
5091 error_init ("initialization designators may not nest");
5092 return 1;
5095 switch (TREE_CODE (constructor_type))
5097 case RECORD_TYPE:
5098 case UNION_TYPE:
5099 subtype = TREE_TYPE (constructor_fields);
5100 if (subtype != error_mark_node)
5101 subtype = TYPE_MAIN_VARIANT (subtype);
5102 break;
5103 case ARRAY_TYPE:
5104 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5105 break;
5106 default:
5107 gcc_unreachable ();
5110 subcode = TREE_CODE (subtype);
5111 if (array && subcode != ARRAY_TYPE)
5113 error_init ("array index in non-array initializer");
5114 return 1;
5116 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5118 error_init ("field name not in record or union initializer");
5119 return 1;
5122 constructor_designated = 1;
5123 push_init_level (2);
5124 return 0;
5127 /* If there are range designators in designator list, push a new designator
5128 to constructor_range_stack. RANGE_END is end of such stack range or
5129 NULL_TREE if there is no range designator at this level. */
5131 static void
5132 push_range_stack (tree range_end)
5134 struct constructor_range_stack *p;
5136 p = GGC_NEW (struct constructor_range_stack);
5137 p->prev = constructor_range_stack;
5138 p->next = 0;
5139 p->fields = constructor_fields;
5140 p->range_start = constructor_index;
5141 p->index = constructor_index;
5142 p->stack = constructor_stack;
5143 p->range_end = range_end;
5144 if (constructor_range_stack)
5145 constructor_range_stack->next = p;
5146 constructor_range_stack = p;
5149 /* Within an array initializer, specify the next index to be initialized.
5150 FIRST is that index. If LAST is nonzero, then initialize a range
5151 of indices, running from FIRST through LAST. */
5153 void
5154 set_init_index (tree first, tree last)
5156 if (set_designator (1))
5157 return;
5159 designator_errorneous = 1;
5161 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5162 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5164 error_init ("array index in initializer not of integer type");
5165 return;
5168 while ((TREE_CODE (first) == NOP_EXPR
5169 || TREE_CODE (first) == CONVERT_EXPR
5170 || TREE_CODE (first) == NON_LVALUE_EXPR)
5171 && (TYPE_MODE (TREE_TYPE (first))
5172 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5173 first = TREE_OPERAND (first, 0);
5175 if (last)
5176 while ((TREE_CODE (last) == NOP_EXPR
5177 || TREE_CODE (last) == CONVERT_EXPR
5178 || TREE_CODE (last) == NON_LVALUE_EXPR)
5179 && (TYPE_MODE (TREE_TYPE (last))
5180 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5181 last = TREE_OPERAND (last, 0);
5183 if (TREE_CODE (first) != INTEGER_CST)
5184 error_init ("nonconstant array index in initializer");
5185 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5186 error_init ("nonconstant array index in initializer");
5187 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5188 error_init ("array index in non-array initializer");
5189 else if (tree_int_cst_sgn (first) == -1)
5190 error_init ("array index in initializer exceeds array bounds");
5191 else if (constructor_max_index
5192 && tree_int_cst_lt (constructor_max_index, first))
5193 error_init ("array index in initializer exceeds array bounds");
5194 else
5196 constructor_index = convert (bitsizetype, first);
5198 if (last)
5200 if (tree_int_cst_equal (first, last))
5201 last = 0;
5202 else if (tree_int_cst_lt (last, first))
5204 error_init ("empty index range in initializer");
5205 last = 0;
5207 else
5209 last = convert (bitsizetype, last);
5210 if (constructor_max_index != 0
5211 && tree_int_cst_lt (constructor_max_index, last))
5213 error_init ("array index range in initializer exceeds array bounds");
5214 last = 0;
5219 designator_depth++;
5220 designator_errorneous = 0;
5221 if (constructor_range_stack || last)
5222 push_range_stack (last);
5226 /* Within a struct initializer, specify the next field to be initialized. */
5228 void
5229 set_init_label (tree fieldname)
5231 tree tail;
5233 if (set_designator (0))
5234 return;
5236 designator_errorneous = 1;
5238 if (TREE_CODE (constructor_type) != RECORD_TYPE
5239 && TREE_CODE (constructor_type) != UNION_TYPE)
5241 error_init ("field name not in record or union initializer");
5242 return;
5245 for (tail = TYPE_FIELDS (constructor_type); tail;
5246 tail = TREE_CHAIN (tail))
5248 if (DECL_NAME (tail) == fieldname)
5249 break;
5252 if (tail == 0)
5253 error ("unknown field %qs specified in initializer",
5254 IDENTIFIER_POINTER (fieldname));
5255 else
5257 constructor_fields = tail;
5258 designator_depth++;
5259 designator_errorneous = 0;
5260 if (constructor_range_stack)
5261 push_range_stack (NULL_TREE);
5265 /* Add a new initializer to the tree of pending initializers. PURPOSE
5266 identifies the initializer, either array index or field in a structure.
5267 VALUE is the value of that index or field. */
5269 static void
5270 add_pending_init (tree purpose, tree value)
5272 struct init_node *p, **q, *r;
5274 q = &constructor_pending_elts;
5275 p = 0;
5277 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5279 while (*q != 0)
5281 p = *q;
5282 if (tree_int_cst_lt (purpose, p->purpose))
5283 q = &p->left;
5284 else if (tree_int_cst_lt (p->purpose, purpose))
5285 q = &p->right;
5286 else
5288 if (TREE_SIDE_EFFECTS (p->value))
5289 warning_init ("initialized field with side-effects overwritten");
5290 p->value = value;
5291 return;
5295 else
5297 tree bitpos;
5299 bitpos = bit_position (purpose);
5300 while (*q != NULL)
5302 p = *q;
5303 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5304 q = &p->left;
5305 else if (p->purpose != purpose)
5306 q = &p->right;
5307 else
5309 if (TREE_SIDE_EFFECTS (p->value))
5310 warning_init ("initialized field with side-effects overwritten");
5311 p->value = value;
5312 return;
5317 r = GGC_NEW (struct init_node);
5318 r->purpose = purpose;
5319 r->value = value;
5321 *q = r;
5322 r->parent = p;
5323 r->left = 0;
5324 r->right = 0;
5325 r->balance = 0;
5327 while (p)
5329 struct init_node *s;
5331 if (r == p->left)
5333 if (p->balance == 0)
5334 p->balance = -1;
5335 else if (p->balance < 0)
5337 if (r->balance < 0)
5339 /* L rotation. */
5340 p->left = r->right;
5341 if (p->left)
5342 p->left->parent = p;
5343 r->right = p;
5345 p->balance = 0;
5346 r->balance = 0;
5348 s = p->parent;
5349 p->parent = r;
5350 r->parent = s;
5351 if (s)
5353 if (s->left == p)
5354 s->left = r;
5355 else
5356 s->right = r;
5358 else
5359 constructor_pending_elts = r;
5361 else
5363 /* LR rotation. */
5364 struct init_node *t = r->right;
5366 r->right = t->left;
5367 if (r->right)
5368 r->right->parent = r;
5369 t->left = r;
5371 p->left = t->right;
5372 if (p->left)
5373 p->left->parent = p;
5374 t->right = p;
5376 p->balance = t->balance < 0;
5377 r->balance = -(t->balance > 0);
5378 t->balance = 0;
5380 s = p->parent;
5381 p->parent = t;
5382 r->parent = t;
5383 t->parent = s;
5384 if (s)
5386 if (s->left == p)
5387 s->left = t;
5388 else
5389 s->right = t;
5391 else
5392 constructor_pending_elts = t;
5394 break;
5396 else
5398 /* p->balance == +1; growth of left side balances the node. */
5399 p->balance = 0;
5400 break;
5403 else /* r == p->right */
5405 if (p->balance == 0)
5406 /* Growth propagation from right side. */
5407 p->balance++;
5408 else if (p->balance > 0)
5410 if (r->balance > 0)
5412 /* R rotation. */
5413 p->right = r->left;
5414 if (p->right)
5415 p->right->parent = p;
5416 r->left = p;
5418 p->balance = 0;
5419 r->balance = 0;
5421 s = p->parent;
5422 p->parent = r;
5423 r->parent = s;
5424 if (s)
5426 if (s->left == p)
5427 s->left = r;
5428 else
5429 s->right = r;
5431 else
5432 constructor_pending_elts = r;
5434 else /* r->balance == -1 */
5436 /* RL rotation */
5437 struct init_node *t = r->left;
5439 r->left = t->right;
5440 if (r->left)
5441 r->left->parent = r;
5442 t->right = r;
5444 p->right = t->left;
5445 if (p->right)
5446 p->right->parent = p;
5447 t->left = p;
5449 r->balance = (t->balance < 0);
5450 p->balance = -(t->balance > 0);
5451 t->balance = 0;
5453 s = p->parent;
5454 p->parent = t;
5455 r->parent = t;
5456 t->parent = s;
5457 if (s)
5459 if (s->left == p)
5460 s->left = t;
5461 else
5462 s->right = t;
5464 else
5465 constructor_pending_elts = t;
5467 break;
5469 else
5471 /* p->balance == -1; growth of right side balances the node. */
5472 p->balance = 0;
5473 break;
5477 r = p;
5478 p = p->parent;
5482 /* Build AVL tree from a sorted chain. */
5484 static void
5485 set_nonincremental_init (void)
5487 tree chain;
5489 if (TREE_CODE (constructor_type) != RECORD_TYPE
5490 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5491 return;
5493 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5494 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5495 constructor_elements = 0;
5496 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5498 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5499 /* Skip any nameless bit fields at the beginning. */
5500 while (constructor_unfilled_fields != 0
5501 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5502 && DECL_NAME (constructor_unfilled_fields) == 0)
5503 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5506 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5508 if (TYPE_DOMAIN (constructor_type))
5509 constructor_unfilled_index
5510 = convert (bitsizetype,
5511 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5512 else
5513 constructor_unfilled_index = bitsize_zero_node;
5515 constructor_incremental = 0;
5518 /* Build AVL tree from a string constant. */
5520 static void
5521 set_nonincremental_init_from_string (tree str)
5523 tree value, purpose, type;
5524 HOST_WIDE_INT val[2];
5525 const char *p, *end;
5526 int byte, wchar_bytes, charwidth, bitpos;
5528 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5530 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5531 == TYPE_PRECISION (char_type_node))
5532 wchar_bytes = 1;
5533 else
5535 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5536 == TYPE_PRECISION (wchar_type_node));
5537 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5539 charwidth = TYPE_PRECISION (char_type_node);
5540 type = TREE_TYPE (constructor_type);
5541 p = TREE_STRING_POINTER (str);
5542 end = p + TREE_STRING_LENGTH (str);
5544 for (purpose = bitsize_zero_node;
5545 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5546 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5548 if (wchar_bytes == 1)
5550 val[1] = (unsigned char) *p++;
5551 val[0] = 0;
5553 else
5555 val[0] = 0;
5556 val[1] = 0;
5557 for (byte = 0; byte < wchar_bytes; byte++)
5559 if (BYTES_BIG_ENDIAN)
5560 bitpos = (wchar_bytes - byte - 1) * charwidth;
5561 else
5562 bitpos = byte * charwidth;
5563 val[bitpos < HOST_BITS_PER_WIDE_INT]
5564 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5565 << (bitpos % HOST_BITS_PER_WIDE_INT);
5569 if (!TYPE_UNSIGNED (type))
5571 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5572 if (bitpos < HOST_BITS_PER_WIDE_INT)
5574 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5576 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5577 val[0] = -1;
5580 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5582 if (val[1] < 0)
5583 val[0] = -1;
5585 else if (val[0] & (((HOST_WIDE_INT) 1)
5586 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5587 val[0] |= ((HOST_WIDE_INT) -1)
5588 << (bitpos - HOST_BITS_PER_WIDE_INT);
5591 value = build_int_cst_wide (type, val[1], val[0]);
5592 add_pending_init (purpose, value);
5595 constructor_incremental = 0;
5598 /* Return value of FIELD in pending initializer or zero if the field was
5599 not initialized yet. */
5601 static tree
5602 find_init_member (tree field)
5604 struct init_node *p;
5606 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5608 if (constructor_incremental
5609 && tree_int_cst_lt (field, constructor_unfilled_index))
5610 set_nonincremental_init ();
5612 p = constructor_pending_elts;
5613 while (p)
5615 if (tree_int_cst_lt (field, p->purpose))
5616 p = p->left;
5617 else if (tree_int_cst_lt (p->purpose, field))
5618 p = p->right;
5619 else
5620 return p->value;
5623 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5625 tree bitpos = bit_position (field);
5627 if (constructor_incremental
5628 && (!constructor_unfilled_fields
5629 || tree_int_cst_lt (bitpos,
5630 bit_position (constructor_unfilled_fields))))
5631 set_nonincremental_init ();
5633 p = constructor_pending_elts;
5634 while (p)
5636 if (field == p->purpose)
5637 return p->value;
5638 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5639 p = p->left;
5640 else
5641 p = p->right;
5644 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5646 if (constructor_elements
5647 && TREE_PURPOSE (constructor_elements) == field)
5648 return TREE_VALUE (constructor_elements);
5650 return 0;
5653 /* "Output" the next constructor element.
5654 At top level, really output it to assembler code now.
5655 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5656 TYPE is the data type that the containing data type wants here.
5657 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5658 If VALUE is a string constant, STRICT_STRING is true if it is
5659 unparenthesized or we should not warn here for it being parenthesized.
5660 For other types of VALUE, STRICT_STRING is not used.
5662 PENDING if non-nil means output pending elements that belong
5663 right after this element. (PENDING is normally 1;
5664 it is 0 while outputting pending elements, to avoid recursion.) */
5666 static void
5667 output_init_element (tree value, bool strict_string, tree type, tree field,
5668 int pending)
5670 if (type == error_mark_node || value == error_mark_node)
5672 constructor_erroneous = 1;
5673 return;
5675 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5676 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5677 && !(TREE_CODE (value) == STRING_CST
5678 && TREE_CODE (type) == ARRAY_TYPE
5679 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5680 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5681 TYPE_MAIN_VARIANT (type))))
5682 value = default_conversion (value);
5684 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5685 && require_constant_value && !flag_isoc99 && pending)
5687 /* As an extension, allow initializing objects with static storage
5688 duration with compound literals (which are then treated just as
5689 the brace enclosed list they contain). */
5690 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5691 value = DECL_INITIAL (decl);
5694 if (value == error_mark_node)
5695 constructor_erroneous = 1;
5696 else if (!TREE_CONSTANT (value))
5697 constructor_constant = 0;
5698 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5699 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5700 || TREE_CODE (constructor_type) == UNION_TYPE)
5701 && DECL_C_BIT_FIELD (field)
5702 && TREE_CODE (value) != INTEGER_CST))
5703 constructor_simple = 0;
5705 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5707 if (require_constant_value)
5709 error_init ("initializer element is not constant");
5710 value = error_mark_node;
5712 else if (require_constant_elements)
5713 pedwarn ("initializer element is not computable at load time");
5716 /* If this field is empty (and not at the end of structure),
5717 don't do anything other than checking the initializer. */
5718 if (field
5719 && (TREE_TYPE (field) == error_mark_node
5720 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5721 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5722 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5723 || TREE_CHAIN (field)))))
5724 return;
5726 value = digest_init (type, value, strict_string, require_constant_value);
5727 if (value == error_mark_node)
5729 constructor_erroneous = 1;
5730 return;
5733 /* If this element doesn't come next in sequence,
5734 put it on constructor_pending_elts. */
5735 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5736 && (!constructor_incremental
5737 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5739 if (constructor_incremental
5740 && tree_int_cst_lt (field, constructor_unfilled_index))
5741 set_nonincremental_init ();
5743 add_pending_init (field, value);
5744 return;
5746 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5747 && (!constructor_incremental
5748 || field != constructor_unfilled_fields))
5750 /* We do this for records but not for unions. In a union,
5751 no matter which field is specified, it can be initialized
5752 right away since it starts at the beginning of the union. */
5753 if (constructor_incremental)
5755 if (!constructor_unfilled_fields)
5756 set_nonincremental_init ();
5757 else
5759 tree bitpos, unfillpos;
5761 bitpos = bit_position (field);
5762 unfillpos = bit_position (constructor_unfilled_fields);
5764 if (tree_int_cst_lt (bitpos, unfillpos))
5765 set_nonincremental_init ();
5769 add_pending_init (field, value);
5770 return;
5772 else if (TREE_CODE (constructor_type) == UNION_TYPE
5773 && constructor_elements)
5775 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5776 warning_init ("initialized field with side-effects overwritten");
5778 /* We can have just one union field set. */
5779 constructor_elements = 0;
5782 /* Otherwise, output this element either to
5783 constructor_elements or to the assembler file. */
5785 if (field && TREE_CODE (field) == INTEGER_CST)
5786 field = copy_node (field);
5787 constructor_elements
5788 = tree_cons (field, value, constructor_elements);
5790 /* Advance the variable that indicates sequential elements output. */
5791 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5792 constructor_unfilled_index
5793 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5794 bitsize_one_node);
5795 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5797 constructor_unfilled_fields
5798 = TREE_CHAIN (constructor_unfilled_fields);
5800 /* Skip any nameless bit fields. */
5801 while (constructor_unfilled_fields != 0
5802 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5803 && DECL_NAME (constructor_unfilled_fields) == 0)
5804 constructor_unfilled_fields =
5805 TREE_CHAIN (constructor_unfilled_fields);
5807 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5808 constructor_unfilled_fields = 0;
5810 /* Now output any pending elements which have become next. */
5811 if (pending)
5812 output_pending_init_elements (0);
5815 /* Output any pending elements which have become next.
5816 As we output elements, constructor_unfilled_{fields,index}
5817 advances, which may cause other elements to become next;
5818 if so, they too are output.
5820 If ALL is 0, we return when there are
5821 no more pending elements to output now.
5823 If ALL is 1, we output space as necessary so that
5824 we can output all the pending elements. */
5826 static void
5827 output_pending_init_elements (int all)
5829 struct init_node *elt = constructor_pending_elts;
5830 tree next;
5832 retry:
5834 /* Look through the whole pending tree.
5835 If we find an element that should be output now,
5836 output it. Otherwise, set NEXT to the element
5837 that comes first among those still pending. */
5839 next = 0;
5840 while (elt)
5842 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5844 if (tree_int_cst_equal (elt->purpose,
5845 constructor_unfilled_index))
5846 output_init_element (elt->value, true,
5847 TREE_TYPE (constructor_type),
5848 constructor_unfilled_index, 0);
5849 else if (tree_int_cst_lt (constructor_unfilled_index,
5850 elt->purpose))
5852 /* Advance to the next smaller node. */
5853 if (elt->left)
5854 elt = elt->left;
5855 else
5857 /* We have reached the smallest node bigger than the
5858 current unfilled index. Fill the space first. */
5859 next = elt->purpose;
5860 break;
5863 else
5865 /* Advance to the next bigger node. */
5866 if (elt->right)
5867 elt = elt->right;
5868 else
5870 /* We have reached the biggest node in a subtree. Find
5871 the parent of it, which is the next bigger node. */
5872 while (elt->parent && elt->parent->right == elt)
5873 elt = elt->parent;
5874 elt = elt->parent;
5875 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5876 elt->purpose))
5878 next = elt->purpose;
5879 break;
5884 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5885 || TREE_CODE (constructor_type) == UNION_TYPE)
5887 tree ctor_unfilled_bitpos, elt_bitpos;
5889 /* If the current record is complete we are done. */
5890 if (constructor_unfilled_fields == 0)
5891 break;
5893 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5894 elt_bitpos = bit_position (elt->purpose);
5895 /* We can't compare fields here because there might be empty
5896 fields in between. */
5897 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5899 constructor_unfilled_fields = elt->purpose;
5900 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5901 elt->purpose, 0);
5903 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5905 /* Advance to the next smaller node. */
5906 if (elt->left)
5907 elt = elt->left;
5908 else
5910 /* We have reached the smallest node bigger than the
5911 current unfilled field. Fill the space first. */
5912 next = elt->purpose;
5913 break;
5916 else
5918 /* Advance to the next bigger node. */
5919 if (elt->right)
5920 elt = elt->right;
5921 else
5923 /* We have reached the biggest node in a subtree. Find
5924 the parent of it, which is the next bigger node. */
5925 while (elt->parent && elt->parent->right == elt)
5926 elt = elt->parent;
5927 elt = elt->parent;
5928 if (elt
5929 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5930 bit_position (elt->purpose))))
5932 next = elt->purpose;
5933 break;
5940 /* Ordinarily return, but not if we want to output all
5941 and there are elements left. */
5942 if (!(all && next != 0))
5943 return;
5945 /* If it's not incremental, just skip over the gap, so that after
5946 jumping to retry we will output the next successive element. */
5947 if (TREE_CODE (constructor_type) == RECORD_TYPE
5948 || TREE_CODE (constructor_type) == UNION_TYPE)
5949 constructor_unfilled_fields = next;
5950 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5951 constructor_unfilled_index = next;
5953 /* ELT now points to the node in the pending tree with the next
5954 initializer to output. */
5955 goto retry;
5958 /* Add one non-braced element to the current constructor level.
5959 This adjusts the current position within the constructor's type.
5960 This may also start or terminate implicit levels
5961 to handle a partly-braced initializer.
5963 Once this has found the correct level for the new element,
5964 it calls output_init_element. */
5966 void
5967 process_init_element (struct c_expr value)
5969 tree orig_value = value.value;
5970 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5971 bool strict_string = value.original_code == STRING_CST;
5973 designator_depth = 0;
5974 designator_errorneous = 0;
5976 /* Handle superfluous braces around string cst as in
5977 char x[] = {"foo"}; */
5978 if (string_flag
5979 && constructor_type
5980 && TREE_CODE (constructor_type) == ARRAY_TYPE
5981 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5982 && integer_zerop (constructor_unfilled_index))
5984 if (constructor_stack->replacement_value.value)
5985 error_init ("excess elements in char array initializer");
5986 constructor_stack->replacement_value = value;
5987 return;
5990 if (constructor_stack->replacement_value.value != 0)
5992 error_init ("excess elements in struct initializer");
5993 return;
5996 /* Ignore elements of a brace group if it is entirely superfluous
5997 and has already been diagnosed. */
5998 if (constructor_type == 0)
5999 return;
6001 /* If we've exhausted any levels that didn't have braces,
6002 pop them now. */
6003 while (constructor_stack->implicit)
6005 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6006 || TREE_CODE (constructor_type) == UNION_TYPE)
6007 && constructor_fields == 0)
6008 process_init_element (pop_init_level (1));
6009 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6010 && (constructor_max_index == 0
6011 || tree_int_cst_lt (constructor_max_index,
6012 constructor_index)))
6013 process_init_element (pop_init_level (1));
6014 else
6015 break;
6018 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6019 if (constructor_range_stack)
6021 /* If value is a compound literal and we'll be just using its
6022 content, don't put it into a SAVE_EXPR. */
6023 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6024 || !require_constant_value
6025 || flag_isoc99)
6026 value.value = save_expr (value.value);
6029 while (1)
6031 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6033 tree fieldtype;
6034 enum tree_code fieldcode;
6036 if (constructor_fields == 0)
6038 pedwarn_init ("excess elements in struct initializer");
6039 break;
6042 fieldtype = TREE_TYPE (constructor_fields);
6043 if (fieldtype != error_mark_node)
6044 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6045 fieldcode = TREE_CODE (fieldtype);
6047 /* Error for non-static initialization of a flexible array member. */
6048 if (fieldcode == ARRAY_TYPE
6049 && !require_constant_value
6050 && TYPE_SIZE (fieldtype) == NULL_TREE
6051 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6053 error_init ("non-static initialization of a flexible array member");
6054 break;
6057 /* Accept a string constant to initialize a subarray. */
6058 if (value.value != 0
6059 && fieldcode == ARRAY_TYPE
6060 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6061 && string_flag)
6062 value.value = orig_value;
6063 /* Otherwise, if we have come to a subaggregate,
6064 and we don't have an element of its type, push into it. */
6065 else if (value.value != 0 && !constructor_no_implicit
6066 && value.value != error_mark_node
6067 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6068 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6069 || fieldcode == UNION_TYPE))
6071 push_init_level (1);
6072 continue;
6075 if (value.value)
6077 push_member_name (constructor_fields);
6078 output_init_element (value.value, strict_string,
6079 fieldtype, constructor_fields, 1);
6080 RESTORE_SPELLING_DEPTH (constructor_depth);
6082 else
6083 /* Do the bookkeeping for an element that was
6084 directly output as a constructor. */
6086 /* For a record, keep track of end position of last field. */
6087 if (DECL_SIZE (constructor_fields))
6088 constructor_bit_index
6089 = size_binop (PLUS_EXPR,
6090 bit_position (constructor_fields),
6091 DECL_SIZE (constructor_fields));
6093 /* If the current field was the first one not yet written out,
6094 it isn't now, so update. */
6095 if (constructor_unfilled_fields == constructor_fields)
6097 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6098 /* Skip any nameless bit fields. */
6099 while (constructor_unfilled_fields != 0
6100 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6101 && DECL_NAME (constructor_unfilled_fields) == 0)
6102 constructor_unfilled_fields =
6103 TREE_CHAIN (constructor_unfilled_fields);
6107 constructor_fields = TREE_CHAIN (constructor_fields);
6108 /* Skip any nameless bit fields at the beginning. */
6109 while (constructor_fields != 0
6110 && DECL_C_BIT_FIELD (constructor_fields)
6111 && DECL_NAME (constructor_fields) == 0)
6112 constructor_fields = TREE_CHAIN (constructor_fields);
6114 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6116 tree fieldtype;
6117 enum tree_code fieldcode;
6119 if (constructor_fields == 0)
6121 pedwarn_init ("excess elements in union initializer");
6122 break;
6125 fieldtype = TREE_TYPE (constructor_fields);
6126 if (fieldtype != error_mark_node)
6127 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6128 fieldcode = TREE_CODE (fieldtype);
6130 /* Warn that traditional C rejects initialization of unions.
6131 We skip the warning if the value is zero. This is done
6132 under the assumption that the zero initializer in user
6133 code appears conditioned on e.g. __STDC__ to avoid
6134 "missing initializer" warnings and relies on default
6135 initialization to zero in the traditional C case.
6136 We also skip the warning if the initializer is designated,
6137 again on the assumption that this must be conditional on
6138 __STDC__ anyway (and we've already complained about the
6139 member-designator already). */
6140 if (warn_traditional && !in_system_header && !constructor_designated
6141 && !(value.value && (integer_zerop (value.value)
6142 || real_zerop (value.value))))
6143 warning ("traditional C rejects initialization of unions");
6145 /* Accept a string constant to initialize a subarray. */
6146 if (value.value != 0
6147 && fieldcode == ARRAY_TYPE
6148 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6149 && string_flag)
6150 value.value = orig_value;
6151 /* Otherwise, if we have come to a subaggregate,
6152 and we don't have an element of its type, push into it. */
6153 else if (value.value != 0 && !constructor_no_implicit
6154 && value.value != error_mark_node
6155 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6156 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6157 || fieldcode == UNION_TYPE))
6159 push_init_level (1);
6160 continue;
6163 if (value.value)
6165 push_member_name (constructor_fields);
6166 output_init_element (value.value, strict_string,
6167 fieldtype, constructor_fields, 1);
6168 RESTORE_SPELLING_DEPTH (constructor_depth);
6170 else
6171 /* Do the bookkeeping for an element that was
6172 directly output as a constructor. */
6174 constructor_bit_index = DECL_SIZE (constructor_fields);
6175 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6178 constructor_fields = 0;
6180 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6182 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6183 enum tree_code eltcode = TREE_CODE (elttype);
6185 /* Accept a string constant to initialize a subarray. */
6186 if (value.value != 0
6187 && eltcode == ARRAY_TYPE
6188 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6189 && string_flag)
6190 value.value = orig_value;
6191 /* Otherwise, if we have come to a subaggregate,
6192 and we don't have an element of its type, push into it. */
6193 else if (value.value != 0 && !constructor_no_implicit
6194 && value.value != error_mark_node
6195 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6196 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6197 || eltcode == UNION_TYPE))
6199 push_init_level (1);
6200 continue;
6203 if (constructor_max_index != 0
6204 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6205 || integer_all_onesp (constructor_max_index)))
6207 pedwarn_init ("excess elements in array initializer");
6208 break;
6211 /* Now output the actual element. */
6212 if (value.value)
6214 push_array_bounds (tree_low_cst (constructor_index, 0));
6215 output_init_element (value.value, strict_string,
6216 elttype, constructor_index, 1);
6217 RESTORE_SPELLING_DEPTH (constructor_depth);
6220 constructor_index
6221 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6223 if (!value.value)
6224 /* If we are doing the bookkeeping for an element that was
6225 directly output as a constructor, we must update
6226 constructor_unfilled_index. */
6227 constructor_unfilled_index = constructor_index;
6229 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6231 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6233 /* Do a basic check of initializer size. Note that vectors
6234 always have a fixed size derived from their type. */
6235 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6237 pedwarn_init ("excess elements in vector initializer");
6238 break;
6241 /* Now output the actual element. */
6242 if (value.value)
6243 output_init_element (value.value, strict_string,
6244 elttype, constructor_index, 1);
6246 constructor_index
6247 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6249 if (!value.value)
6250 /* If we are doing the bookkeeping for an element that was
6251 directly output as a constructor, we must update
6252 constructor_unfilled_index. */
6253 constructor_unfilled_index = constructor_index;
6256 /* Handle the sole element allowed in a braced initializer
6257 for a scalar variable. */
6258 else if (constructor_fields == 0)
6260 pedwarn_init ("excess elements in scalar initializer");
6261 break;
6263 else
6265 if (value.value)
6266 output_init_element (value.value, strict_string,
6267 constructor_type, NULL_TREE, 1);
6268 constructor_fields = 0;
6271 /* Handle range initializers either at this level or anywhere higher
6272 in the designator stack. */
6273 if (constructor_range_stack)
6275 struct constructor_range_stack *p, *range_stack;
6276 int finish = 0;
6278 range_stack = constructor_range_stack;
6279 constructor_range_stack = 0;
6280 while (constructor_stack != range_stack->stack)
6282 gcc_assert (constructor_stack->implicit);
6283 process_init_element (pop_init_level (1));
6285 for (p = range_stack;
6286 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6287 p = p->prev)
6289 gcc_assert (constructor_stack->implicit);
6290 process_init_element (pop_init_level (1));
6293 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6294 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6295 finish = 1;
6297 while (1)
6299 constructor_index = p->index;
6300 constructor_fields = p->fields;
6301 if (finish && p->range_end && p->index == p->range_start)
6303 finish = 0;
6304 p->prev = 0;
6306 p = p->next;
6307 if (!p)
6308 break;
6309 push_init_level (2);
6310 p->stack = constructor_stack;
6311 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6312 p->index = p->range_start;
6315 if (!finish)
6316 constructor_range_stack = range_stack;
6317 continue;
6320 break;
6323 constructor_range_stack = 0;
6326 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6327 (guaranteed to be 'volatile' or null) and ARGS (represented using
6328 an ASM_EXPR node). */
6329 tree
6330 build_asm_stmt (tree cv_qualifier, tree args)
6332 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6333 ASM_VOLATILE_P (args) = 1;
6334 return add_stmt (args);
6337 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6338 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6339 SIMPLE indicates whether there was anything at all after the
6340 string in the asm expression -- asm("blah") and asm("blah" : )
6341 are subtly different. We use a ASM_EXPR node to represent this. */
6342 tree
6343 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6344 bool simple)
6346 tree tail;
6347 tree args;
6348 int i;
6349 const char *constraint;
6350 bool allows_mem, allows_reg, is_inout;
6351 int ninputs;
6352 int noutputs;
6354 ninputs = list_length (inputs);
6355 noutputs = list_length (outputs);
6357 /* Remove output conversions that change the type but not the mode. */
6358 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6360 tree output = TREE_VALUE (tail);
6361 STRIP_NOPS (output);
6362 TREE_VALUE (tail) = output;
6363 lvalue_or_else (output, lv_asm);
6365 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6367 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
6368 &allows_mem, &allows_reg, &is_inout))
6370 /* By marking this operand as erroneous, we will not try
6371 to process this operand again in expand_asm_operands. */
6372 TREE_VALUE (tail) = error_mark_node;
6373 continue;
6376 /* If the operand is a DECL that is going to end up in
6377 memory, assume it is addressable. This is a bit more
6378 conservative than it would ideally be; the exact test is
6379 buried deep in expand_asm_operands and depends on the
6380 DECL_RTL for the OPERAND -- which we don't have at this
6381 point. */
6382 if (!allows_reg && DECL_P (output))
6383 c_mark_addressable (output);
6386 /* Perform default conversions on array and function inputs.
6387 Don't do this for other types as it would screw up operands
6388 expected to be in memory. */
6389 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6390 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6392 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6394 /* Simple asm statements are treated as volatile. */
6395 if (simple)
6397 ASM_VOLATILE_P (args) = 1;
6398 ASM_INPUT_P (args) = 1;
6400 return args;
6403 /* Generate a goto statement to LABEL. */
6405 tree
6406 c_finish_goto_label (tree label)
6408 tree decl = lookup_label (label);
6409 if (!decl)
6410 return NULL_TREE;
6412 TREE_USED (decl) = 1;
6413 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6416 /* Generate a computed goto statement to EXPR. */
6418 tree
6419 c_finish_goto_ptr (tree expr)
6421 if (pedantic)
6422 pedwarn ("ISO C forbids %<goto *expr;%>");
6423 expr = convert (ptr_type_node, expr);
6424 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6427 /* Generate a C `return' statement. RETVAL is the expression for what
6428 to return, or a null pointer for `return;' with no value. */
6430 tree
6431 c_finish_return (tree retval)
6433 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6435 if (TREE_THIS_VOLATILE (current_function_decl))
6436 warning ("function declared %<noreturn%> has a %<return%> statement");
6438 if (!retval)
6440 current_function_returns_null = 1;
6441 if ((warn_return_type || flag_isoc99)
6442 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6443 pedwarn_c99 ("%<return%> with no value, in "
6444 "function returning non-void");
6446 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6448 current_function_returns_null = 1;
6449 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6450 pedwarn ("%<return%> with a value, in function returning void");
6452 else
6454 tree t = convert_for_assignment (valtype, retval, ic_return,
6455 NULL_TREE, NULL_TREE, 0);
6456 tree res = DECL_RESULT (current_function_decl);
6457 tree inner;
6459 current_function_returns_value = 1;
6460 if (t == error_mark_node)
6461 return NULL_TREE;
6463 inner = t = convert (TREE_TYPE (res), t);
6465 /* Strip any conversions, additions, and subtractions, and see if
6466 we are returning the address of a local variable. Warn if so. */
6467 while (1)
6469 switch (TREE_CODE (inner))
6471 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6472 case PLUS_EXPR:
6473 inner = TREE_OPERAND (inner, 0);
6474 continue;
6476 case MINUS_EXPR:
6477 /* If the second operand of the MINUS_EXPR has a pointer
6478 type (or is converted from it), this may be valid, so
6479 don't give a warning. */
6481 tree op1 = TREE_OPERAND (inner, 1);
6483 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6484 && (TREE_CODE (op1) == NOP_EXPR
6485 || TREE_CODE (op1) == NON_LVALUE_EXPR
6486 || TREE_CODE (op1) == CONVERT_EXPR))
6487 op1 = TREE_OPERAND (op1, 0);
6489 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6490 break;
6492 inner = TREE_OPERAND (inner, 0);
6493 continue;
6496 case ADDR_EXPR:
6497 inner = TREE_OPERAND (inner, 0);
6499 while (REFERENCE_CLASS_P (inner)
6500 && TREE_CODE (inner) != INDIRECT_REF)
6501 inner = TREE_OPERAND (inner, 0);
6503 if (DECL_P (inner)
6504 && !DECL_EXTERNAL (inner)
6505 && !TREE_STATIC (inner)
6506 && DECL_CONTEXT (inner) == current_function_decl)
6507 warning ("function returns address of local variable");
6508 break;
6510 default:
6511 break;
6514 break;
6517 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6520 return add_stmt (build_stmt (RETURN_EXPR, retval));
6523 struct c_switch {
6524 /* The SWITCH_STMT being built. */
6525 tree switch_stmt;
6527 /* The original type of the testing expression, i.e. before the
6528 default conversion is applied. */
6529 tree orig_type;
6531 /* A splay-tree mapping the low element of a case range to the high
6532 element, or NULL_TREE if there is no high element. Used to
6533 determine whether or not a new case label duplicates an old case
6534 label. We need a tree, rather than simply a hash table, because
6535 of the GNU case range extension. */
6536 splay_tree cases;
6538 /* The next node on the stack. */
6539 struct c_switch *next;
6542 /* A stack of the currently active switch statements. The innermost
6543 switch statement is on the top of the stack. There is no need to
6544 mark the stack for garbage collection because it is only active
6545 during the processing of the body of a function, and we never
6546 collect at that point. */
6548 struct c_switch *c_switch_stack;
6550 /* Start a C switch statement, testing expression EXP. Return the new
6551 SWITCH_STMT. */
6553 tree
6554 c_start_case (tree exp)
6556 enum tree_code code;
6557 tree type, orig_type = error_mark_node;
6558 struct c_switch *cs;
6560 if (exp != error_mark_node)
6562 code = TREE_CODE (TREE_TYPE (exp));
6563 orig_type = TREE_TYPE (exp);
6565 if (!INTEGRAL_TYPE_P (orig_type)
6566 && code != ERROR_MARK)
6568 error ("switch quantity not an integer");
6569 exp = integer_zero_node;
6571 else
6573 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6575 if (warn_traditional && !in_system_header
6576 && (type == long_integer_type_node
6577 || type == long_unsigned_type_node))
6578 warning ("%<long%> switch expression not converted to "
6579 "%<int%> in ISO C");
6581 exp = default_conversion (exp);
6582 type = TREE_TYPE (exp);
6586 /* Add this new SWITCH_STMT to the stack. */
6587 cs = XNEW (struct c_switch);
6588 cs->switch_stmt = build_stmt ((enum tree_code) SWITCH_STMT, exp, NULL_TREE,
6589 orig_type);
6590 cs->orig_type = orig_type;
6591 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6592 cs->next = c_switch_stack;
6593 c_switch_stack = cs;
6595 return add_stmt (cs->switch_stmt);
6598 /* Process a case label. */
6600 tree
6601 do_case (tree low_value, tree high_value)
6603 tree label = NULL_TREE;
6605 if (c_switch_stack)
6607 label = c_add_case_label (c_switch_stack->cases,
6608 SWITCH_COND (c_switch_stack->switch_stmt),
6609 c_switch_stack->orig_type,
6610 low_value, high_value);
6611 if (label == error_mark_node)
6612 label = NULL_TREE;
6614 else if (low_value)
6615 error ("case label not within a switch statement");
6616 else
6617 error ("%<default%> label not within a switch statement");
6619 return label;
6622 /* Finish the switch statement. */
6624 void
6625 c_finish_case (tree body)
6627 struct c_switch *cs = c_switch_stack;
6629 SWITCH_BODY (cs->switch_stmt) = body;
6631 /* Emit warnings as needed. */
6632 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6634 /* Pop the stack. */
6635 c_switch_stack = cs->next;
6636 splay_tree_delete (cs->cases);
6637 XDELETE (cs);
6640 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6641 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6642 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6643 statement, and was not surrounded with parenthesis. */
6645 void
6646 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6647 tree else_block, bool nested_if)
6649 tree stmt;
6651 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6652 if (warn_parentheses && nested_if && else_block == NULL)
6654 tree inner_if = then_block;
6656 /* We know from the grammar productions that there is an IF nested
6657 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6658 it might not be exactly THEN_BLOCK, but should be the last
6659 non-container statement within. */
6660 while (1)
6661 switch (TREE_CODE (inner_if))
6663 case COND_EXPR:
6664 goto found;
6665 case BIND_EXPR:
6666 inner_if = BIND_EXPR_BODY (inner_if);
6667 break;
6668 case STATEMENT_LIST:
6669 inner_if = expr_last (then_block);
6670 break;
6671 case TRY_FINALLY_EXPR:
6672 case TRY_CATCH_EXPR:
6673 inner_if = TREE_OPERAND (inner_if, 0);
6674 break;
6675 default:
6676 gcc_unreachable ();
6678 found:
6680 if (COND_EXPR_ELSE (inner_if))
6681 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6682 &if_locus);
6685 /* Diagnose ";" via the special empty statement node that we create. */
6686 if (extra_warnings)
6688 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6690 if (!else_block)
6691 warning ("%Hempty body in an if-statement",
6692 EXPR_LOCUS (then_block));
6693 then_block = alloc_stmt_list ();
6695 if (else_block
6696 && TREE_CODE (else_block) == NOP_EXPR
6697 && !TREE_TYPE (else_block))
6699 warning ("%Hempty body in an else-statement",
6700 EXPR_LOCUS (else_block));
6701 else_block = alloc_stmt_list ();
6705 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6706 SET_EXPR_LOCATION (stmt, if_locus);
6707 add_stmt (stmt);
6710 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6711 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6712 is false for DO loops. INCR is the FOR increment expression. BODY is
6713 the statement controlled by the loop. BLAB is the break label. CLAB is
6714 the continue label. Everything is allowed to be NULL. */
6716 void
6717 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6718 tree blab, tree clab, bool cond_is_first)
6720 tree entry = NULL, exit = NULL, t;
6722 /* Detect do { ... } while (0) and don't generate loop construct. */
6723 if (cond && !cond_is_first && integer_zerop (cond))
6724 cond = NULL;
6725 if (cond_is_first || cond)
6727 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6729 /* If we have an exit condition, then we build an IF with gotos either
6730 out of the loop, or to the top of it. If there's no exit condition,
6731 then we just build a jump back to the top. */
6732 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6734 if (cond)
6736 /* Canonicalize the loop condition to the end. This means
6737 generating a branch to the loop condition. Reuse the
6738 continue label, if possible. */
6739 if (cond_is_first)
6741 if (incr || !clab)
6743 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6744 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6746 else
6747 t = build1 (GOTO_EXPR, void_type_node, clab);
6748 SET_EXPR_LOCATION (t, start_locus);
6749 add_stmt (t);
6752 t = build_and_jump (&blab);
6753 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6754 exit = fold (exit);
6755 if (cond_is_first)
6756 SET_EXPR_LOCATION (exit, start_locus);
6757 else
6758 SET_EXPR_LOCATION (exit, input_location);
6761 add_stmt (top);
6764 if (body)
6765 add_stmt (body);
6766 if (clab)
6767 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6768 if (incr)
6769 add_stmt (incr);
6770 if (entry)
6771 add_stmt (entry);
6772 if (exit)
6773 add_stmt (exit);
6774 if (blab)
6775 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6778 tree
6779 c_finish_bc_stmt (tree *label_p, bool is_break)
6781 tree label = *label_p;
6783 if (!label)
6784 *label_p = label = create_artificial_label ();
6785 else if (TREE_CODE (label) != LABEL_DECL)
6787 if (is_break)
6788 error ("break statement not within loop or switch");
6789 else
6790 error ("continue statement not within a loop");
6791 return NULL_TREE;
6794 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6797 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6799 static void
6800 emit_side_effect_warnings (tree expr)
6802 if (expr == error_mark_node)
6804 else if (!TREE_SIDE_EFFECTS (expr))
6806 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6807 warning ("%Hstatement with no effect",
6808 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6810 else if (warn_unused_value)
6811 warn_if_unused_value (expr, input_location);
6814 /* Process an expression as if it were a complete statement. Emit
6815 diagnostics, but do not call ADD_STMT. */
6817 tree
6818 c_process_expr_stmt (tree expr)
6820 if (!expr)
6821 return NULL_TREE;
6823 /* Do default conversion if safe and possibly important,
6824 in case within ({...}). */
6825 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6826 && (flag_isoc99 || lvalue_p (expr)))
6827 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6828 expr = default_conversion (expr);
6830 if (warn_sequence_point)
6831 verify_sequence_points (expr);
6833 if (TREE_TYPE (expr) != error_mark_node
6834 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6835 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6836 error ("expression statement has incomplete type");
6838 /* If we're not processing a statement expression, warn about unused values.
6839 Warnings for statement expressions will be emitted later, once we figure
6840 out which is the result. */
6841 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6842 && (extra_warnings || warn_unused_value))
6843 emit_side_effect_warnings (expr);
6845 /* If the expression is not of a type to which we cannot assign a line
6846 number, wrap the thing in a no-op NOP_EXPR. */
6847 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6848 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6850 if (EXPR_P (expr))
6851 SET_EXPR_LOCATION (expr, input_location);
6853 return expr;
6856 /* Emit an expression as a statement. */
6858 tree
6859 c_finish_expr_stmt (tree expr)
6861 if (expr)
6862 return add_stmt (c_process_expr_stmt (expr));
6863 else
6864 return NULL;
6867 /* Do the opposite and emit a statement as an expression. To begin,
6868 create a new binding level and return it. */
6870 tree
6871 c_begin_stmt_expr (void)
6873 tree ret;
6875 /* We must force a BLOCK for this level so that, if it is not expanded
6876 later, there is a way to turn off the entire subtree of blocks that
6877 are contained in it. */
6878 keep_next_level ();
6879 ret = c_begin_compound_stmt (true);
6881 /* Mark the current statement list as belonging to a statement list. */
6882 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6884 return ret;
6887 tree
6888 c_finish_stmt_expr (tree body)
6890 tree last, type, tmp, val;
6891 tree *last_p;
6893 body = c_end_compound_stmt (body, true);
6895 /* Locate the last statement in BODY. See c_end_compound_stmt
6896 about always returning a BIND_EXPR. */
6897 last_p = &BIND_EXPR_BODY (body);
6898 last = BIND_EXPR_BODY (body);
6900 continue_searching:
6901 if (TREE_CODE (last) == STATEMENT_LIST)
6903 tree_stmt_iterator i;
6905 /* This can happen with degenerate cases like ({ }). No value. */
6906 if (!TREE_SIDE_EFFECTS (last))
6907 return body;
6909 /* If we're supposed to generate side effects warnings, process
6910 all of the statements except the last. */
6911 if (extra_warnings || warn_unused_value)
6913 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6914 emit_side_effect_warnings (tsi_stmt (i));
6916 else
6917 i = tsi_last (last);
6918 last_p = tsi_stmt_ptr (i);
6919 last = *last_p;
6922 /* If the end of the list is exception related, then the list was split
6923 by a call to push_cleanup. Continue searching. */
6924 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6925 || TREE_CODE (last) == TRY_CATCH_EXPR)
6927 last_p = &TREE_OPERAND (last, 0);
6928 last = *last_p;
6929 goto continue_searching;
6932 /* In the case that the BIND_EXPR is not necessary, return the
6933 expression out from inside it. */
6934 if (last == error_mark_node
6935 || (last == BIND_EXPR_BODY (body)
6936 && BIND_EXPR_VARS (body) == NULL))
6937 return last;
6939 /* Extract the type of said expression. */
6940 type = TREE_TYPE (last);
6942 /* If we're not returning a value at all, then the BIND_EXPR that
6943 we already have is a fine expression to return. */
6944 if (!type || VOID_TYPE_P (type))
6945 return body;
6947 /* Now that we've located the expression containing the value, it seems
6948 silly to make voidify_wrapper_expr repeat the process. Create a
6949 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6950 tmp = create_tmp_var_raw (type, NULL);
6952 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6953 tree_expr_nonnegative_p giving up immediately. */
6954 val = last;
6955 if (TREE_CODE (val) == NOP_EXPR
6956 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
6957 val = TREE_OPERAND (val, 0);
6959 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
6960 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
6962 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
6965 /* Begin and end compound statements. This is as simple as pushing
6966 and popping new statement lists from the tree. */
6968 tree
6969 c_begin_compound_stmt (bool do_scope)
6971 tree stmt = push_stmt_list ();
6972 if (do_scope)
6973 push_scope ();
6974 return stmt;
6977 tree
6978 c_end_compound_stmt (tree stmt, bool do_scope)
6980 tree block = NULL;
6982 if (do_scope)
6984 if (c_dialect_objc ())
6985 objc_clear_super_receiver ();
6986 block = pop_scope ();
6989 stmt = pop_stmt_list (stmt);
6990 stmt = c_build_bind_expr (block, stmt);
6992 /* If this compound statement is nested immediately inside a statement
6993 expression, then force a BIND_EXPR to be created. Otherwise we'll
6994 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6995 STATEMENT_LISTs merge, and thus we can lose track of what statement
6996 was really last. */
6997 if (cur_stmt_list
6998 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6999 && TREE_CODE (stmt) != BIND_EXPR)
7001 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7002 TREE_SIDE_EFFECTS (stmt) = 1;
7005 return stmt;
7008 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7009 when the current scope is exited. EH_ONLY is true when this is not
7010 meant to apply to normal control flow transfer. */
7012 void
7013 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7015 enum tree_code code;
7016 tree stmt, list;
7017 bool stmt_expr;
7019 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7020 stmt = build_stmt (code, NULL, cleanup);
7021 add_stmt (stmt);
7022 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7023 list = push_stmt_list ();
7024 TREE_OPERAND (stmt, 0) = list;
7025 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7028 /* Build a binary-operation expression without default conversions.
7029 CODE is the kind of expression to build.
7030 This function differs from `build' in several ways:
7031 the data type of the result is computed and recorded in it,
7032 warnings are generated if arg data types are invalid,
7033 special handling for addition and subtraction of pointers is known,
7034 and some optimization is done (operations on narrow ints
7035 are done in the narrower type when that gives the same result).
7036 Constant folding is also done before the result is returned.
7038 Note that the operands will never have enumeral types, or function
7039 or array types, because either they will have the default conversions
7040 performed or they have both just been converted to some other type in which
7041 the arithmetic is to be done. */
7043 tree
7044 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7045 int convert_p)
7047 tree type0, type1;
7048 enum tree_code code0, code1;
7049 tree op0, op1;
7051 /* Expression code to give to the expression when it is built.
7052 Normally this is CODE, which is what the caller asked for,
7053 but in some special cases we change it. */
7054 enum tree_code resultcode = code;
7056 /* Data type in which the computation is to be performed.
7057 In the simplest cases this is the common type of the arguments. */
7058 tree result_type = NULL;
7060 /* Nonzero means operands have already been type-converted
7061 in whatever way is necessary.
7062 Zero means they need to be converted to RESULT_TYPE. */
7063 int converted = 0;
7065 /* Nonzero means create the expression with this type, rather than
7066 RESULT_TYPE. */
7067 tree build_type = 0;
7069 /* Nonzero means after finally constructing the expression
7070 convert it to this type. */
7071 tree final_type = 0;
7073 /* Nonzero if this is an operation like MIN or MAX which can
7074 safely be computed in short if both args are promoted shorts.
7075 Also implies COMMON.
7076 -1 indicates a bitwise operation; this makes a difference
7077 in the exact conditions for when it is safe to do the operation
7078 in a narrower mode. */
7079 int shorten = 0;
7081 /* Nonzero if this is a comparison operation;
7082 if both args are promoted shorts, compare the original shorts.
7083 Also implies COMMON. */
7084 int short_compare = 0;
7086 /* Nonzero if this is a right-shift operation, which can be computed on the
7087 original short and then promoted if the operand is a promoted short. */
7088 int short_shift = 0;
7090 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7091 int common = 0;
7093 if (convert_p)
7095 op0 = default_conversion (orig_op0);
7096 op1 = default_conversion (orig_op1);
7098 else
7100 op0 = orig_op0;
7101 op1 = orig_op1;
7104 type0 = TREE_TYPE (op0);
7105 type1 = TREE_TYPE (op1);
7107 /* The expression codes of the data types of the arguments tell us
7108 whether the arguments are integers, floating, pointers, etc. */
7109 code0 = TREE_CODE (type0);
7110 code1 = TREE_CODE (type1);
7112 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7113 STRIP_TYPE_NOPS (op0);
7114 STRIP_TYPE_NOPS (op1);
7116 /* If an error was already reported for one of the arguments,
7117 avoid reporting another error. */
7119 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7120 return error_mark_node;
7122 switch (code)
7124 case PLUS_EXPR:
7125 /* Handle the pointer + int case. */
7126 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7127 return pointer_int_sum (PLUS_EXPR, op0, op1);
7128 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7129 return pointer_int_sum (PLUS_EXPR, op1, op0);
7130 else
7131 common = 1;
7132 break;
7134 case MINUS_EXPR:
7135 /* Subtraction of two similar pointers.
7136 We must subtract them as integers, then divide by object size. */
7137 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7138 && comp_target_types (type0, type1, 1))
7139 return pointer_diff (op0, op1);
7140 /* Handle pointer minus int. Just like pointer plus int. */
7141 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7142 return pointer_int_sum (MINUS_EXPR, op0, op1);
7143 else
7144 common = 1;
7145 break;
7147 case MULT_EXPR:
7148 common = 1;
7149 break;
7151 case TRUNC_DIV_EXPR:
7152 case CEIL_DIV_EXPR:
7153 case FLOOR_DIV_EXPR:
7154 case ROUND_DIV_EXPR:
7155 case EXACT_DIV_EXPR:
7156 /* Floating point division by zero is a legitimate way to obtain
7157 infinities and NaNs. */
7158 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7159 warning ("division by zero");
7161 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7162 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7163 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7164 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7166 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7167 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7168 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7169 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7171 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7172 resultcode = RDIV_EXPR;
7173 else
7174 /* Although it would be tempting to shorten always here, that
7175 loses on some targets, since the modulo instruction is
7176 undefined if the quotient can't be represented in the
7177 computation mode. We shorten only if unsigned or if
7178 dividing by something we know != -1. */
7179 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7180 || (TREE_CODE (op1) == INTEGER_CST
7181 && !integer_all_onesp (op1)));
7182 common = 1;
7184 break;
7186 case BIT_AND_EXPR:
7187 case BIT_IOR_EXPR:
7188 case BIT_XOR_EXPR:
7189 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7190 shorten = -1;
7191 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7192 common = 1;
7193 break;
7195 case TRUNC_MOD_EXPR:
7196 case FLOOR_MOD_EXPR:
7197 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7198 warning ("division by zero");
7200 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7202 /* Although it would be tempting to shorten always here, that loses
7203 on some targets, since the modulo instruction is undefined if the
7204 quotient can't be represented in the computation mode. We shorten
7205 only if unsigned or if dividing by something we know != -1. */
7206 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7207 || (TREE_CODE (op1) == INTEGER_CST
7208 && !integer_all_onesp (op1)));
7209 common = 1;
7211 break;
7213 case TRUTH_ANDIF_EXPR:
7214 case TRUTH_ORIF_EXPR:
7215 case TRUTH_AND_EXPR:
7216 case TRUTH_OR_EXPR:
7217 case TRUTH_XOR_EXPR:
7218 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7219 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7220 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7221 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7223 /* Result of these operations is always an int,
7224 but that does not mean the operands should be
7225 converted to ints! */
7226 result_type = integer_type_node;
7227 op0 = lang_hooks.truthvalue_conversion (op0);
7228 op1 = lang_hooks.truthvalue_conversion (op1);
7229 converted = 1;
7231 break;
7233 /* Shift operations: result has same type as first operand;
7234 always convert second operand to int.
7235 Also set SHORT_SHIFT if shifting rightward. */
7237 case RSHIFT_EXPR:
7238 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7240 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7242 if (tree_int_cst_sgn (op1) < 0)
7243 warning ("right shift count is negative");
7244 else
7246 if (!integer_zerop (op1))
7247 short_shift = 1;
7249 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7250 warning ("right shift count >= width of type");
7254 /* Use the type of the value to be shifted. */
7255 result_type = type0;
7256 /* Convert the shift-count to an integer, regardless of size
7257 of value being shifted. */
7258 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7259 op1 = convert (integer_type_node, op1);
7260 /* Avoid converting op1 to result_type later. */
7261 converted = 1;
7263 break;
7265 case LSHIFT_EXPR:
7266 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7268 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7270 if (tree_int_cst_sgn (op1) < 0)
7271 warning ("left shift count is negative");
7273 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7274 warning ("left shift count >= width of type");
7277 /* Use the type of the value to be shifted. */
7278 result_type = type0;
7279 /* Convert the shift-count to an integer, regardless of size
7280 of value being shifted. */
7281 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7282 op1 = convert (integer_type_node, op1);
7283 /* Avoid converting op1 to result_type later. */
7284 converted = 1;
7286 break;
7288 case RROTATE_EXPR:
7289 case LROTATE_EXPR:
7290 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7292 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7294 if (tree_int_cst_sgn (op1) < 0)
7295 warning ("shift count is negative");
7296 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7297 warning ("shift count >= width of type");
7300 /* Use the type of the value to be shifted. */
7301 result_type = type0;
7302 /* Convert the shift-count to an integer, regardless of size
7303 of value being shifted. */
7304 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7305 op1 = convert (integer_type_node, op1);
7306 /* Avoid converting op1 to result_type later. */
7307 converted = 1;
7309 break;
7311 case EQ_EXPR:
7312 case NE_EXPR:
7313 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7314 warning ("comparing floating point with == or != is unsafe");
7315 /* Result of comparison is always int,
7316 but don't convert the args to int! */
7317 build_type = integer_type_node;
7318 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7319 || code0 == COMPLEX_TYPE)
7320 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7321 || code1 == COMPLEX_TYPE))
7322 short_compare = 1;
7323 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7325 tree tt0 = TREE_TYPE (type0);
7326 tree tt1 = TREE_TYPE (type1);
7327 /* Anything compares with void *. void * compares with anything.
7328 Otherwise, the targets must be compatible
7329 and both must be object or both incomplete. */
7330 if (comp_target_types (type0, type1, 1))
7331 result_type = common_pointer_type (type0, type1);
7332 else if (VOID_TYPE_P (tt0))
7334 /* op0 != orig_op0 detects the case of something
7335 whose value is 0 but which isn't a valid null ptr const. */
7336 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7337 && TREE_CODE (tt1) == FUNCTION_TYPE)
7338 pedwarn ("ISO C forbids comparison of %<void *%>"
7339 " with function pointer");
7341 else if (VOID_TYPE_P (tt1))
7343 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7344 && TREE_CODE (tt0) == FUNCTION_TYPE)
7345 pedwarn ("ISO C forbids comparison of %<void *%>"
7346 " with function pointer");
7348 else
7349 pedwarn ("comparison of distinct pointer types lacks a cast");
7351 if (result_type == NULL_TREE)
7352 result_type = ptr_type_node;
7354 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7355 && integer_zerop (op1))
7356 result_type = type0;
7357 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7358 && integer_zerop (op0))
7359 result_type = type1;
7360 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7362 result_type = type0;
7363 pedwarn ("comparison between pointer and integer");
7365 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7367 result_type = type1;
7368 pedwarn ("comparison between pointer and integer");
7370 break;
7372 case MAX_EXPR:
7373 case MIN_EXPR:
7374 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7375 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7376 shorten = 1;
7377 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7379 if (comp_target_types (type0, type1, 1))
7381 result_type = common_pointer_type (type0, type1);
7382 if (pedantic
7383 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7384 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7386 else
7388 result_type = ptr_type_node;
7389 pedwarn ("comparison of distinct pointer types lacks a cast");
7392 break;
7394 case LE_EXPR:
7395 case GE_EXPR:
7396 case LT_EXPR:
7397 case GT_EXPR:
7398 build_type = integer_type_node;
7399 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7400 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7401 short_compare = 1;
7402 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7404 if (comp_target_types (type0, type1, 1))
7406 result_type = common_pointer_type (type0, type1);
7407 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7408 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7409 pedwarn ("comparison of complete and incomplete pointers");
7410 else if (pedantic
7411 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7412 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7414 else
7416 result_type = ptr_type_node;
7417 pedwarn ("comparison of distinct pointer types lacks a cast");
7420 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7421 && integer_zerop (op1))
7423 result_type = type0;
7424 if (pedantic || extra_warnings)
7425 pedwarn ("ordered comparison of pointer with integer zero");
7427 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7428 && integer_zerop (op0))
7430 result_type = type1;
7431 if (pedantic)
7432 pedwarn ("ordered comparison of pointer with integer zero");
7434 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7436 result_type = type0;
7437 pedwarn ("comparison between pointer and integer");
7439 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7441 result_type = type1;
7442 pedwarn ("comparison between pointer and integer");
7444 break;
7446 case UNORDERED_EXPR:
7447 case ORDERED_EXPR:
7448 case UNLT_EXPR:
7449 case UNLE_EXPR:
7450 case UNGT_EXPR:
7451 case UNGE_EXPR:
7452 case UNEQ_EXPR:
7453 case LTGT_EXPR:
7454 build_type = integer_type_node;
7455 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
7457 error ("unordered comparison on non-floating point argument");
7458 return error_mark_node;
7460 common = 1;
7461 break;
7463 default:
7464 break;
7467 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7468 return error_mark_node;
7470 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7471 || code0 == VECTOR_TYPE)
7473 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7474 || code1 == VECTOR_TYPE))
7476 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7478 if (shorten || common || short_compare)
7479 result_type = common_type (type0, type1);
7481 /* For certain operations (which identify themselves by shorten != 0)
7482 if both args were extended from the same smaller type,
7483 do the arithmetic in that type and then extend.
7485 shorten !=0 and !=1 indicates a bitwise operation.
7486 For them, this optimization is safe only if
7487 both args are zero-extended or both are sign-extended.
7488 Otherwise, we might change the result.
7489 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7490 but calculated in (unsigned short) it would be (unsigned short)-1. */
7492 if (shorten && none_complex)
7494 int unsigned0, unsigned1;
7495 tree arg0 = get_narrower (op0, &unsigned0);
7496 tree arg1 = get_narrower (op1, &unsigned1);
7497 /* UNS is 1 if the operation to be done is an unsigned one. */
7498 int uns = TYPE_UNSIGNED (result_type);
7499 tree type;
7501 final_type = result_type;
7503 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7504 but it *requires* conversion to FINAL_TYPE. */
7506 if ((TYPE_PRECISION (TREE_TYPE (op0))
7507 == TYPE_PRECISION (TREE_TYPE (arg0)))
7508 && TREE_TYPE (op0) != final_type)
7509 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7510 if ((TYPE_PRECISION (TREE_TYPE (op1))
7511 == TYPE_PRECISION (TREE_TYPE (arg1)))
7512 && TREE_TYPE (op1) != final_type)
7513 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7515 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7517 /* For bitwise operations, signedness of nominal type
7518 does not matter. Consider only how operands were extended. */
7519 if (shorten == -1)
7520 uns = unsigned0;
7522 /* Note that in all three cases below we refrain from optimizing
7523 an unsigned operation on sign-extended args.
7524 That would not be valid. */
7526 /* Both args variable: if both extended in same way
7527 from same width, do it in that width.
7528 Do it unsigned if args were zero-extended. */
7529 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7530 < TYPE_PRECISION (result_type))
7531 && (TYPE_PRECISION (TREE_TYPE (arg1))
7532 == TYPE_PRECISION (TREE_TYPE (arg0)))
7533 && unsigned0 == unsigned1
7534 && (unsigned0 || !uns))
7535 result_type
7536 = c_common_signed_or_unsigned_type
7537 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7538 else if (TREE_CODE (arg0) == INTEGER_CST
7539 && (unsigned1 || !uns)
7540 && (TYPE_PRECISION (TREE_TYPE (arg1))
7541 < TYPE_PRECISION (result_type))
7542 && (type
7543 = c_common_signed_or_unsigned_type (unsigned1,
7544 TREE_TYPE (arg1)),
7545 int_fits_type_p (arg0, type)))
7546 result_type = type;
7547 else if (TREE_CODE (arg1) == INTEGER_CST
7548 && (unsigned0 || !uns)
7549 && (TYPE_PRECISION (TREE_TYPE (arg0))
7550 < TYPE_PRECISION (result_type))
7551 && (type
7552 = c_common_signed_or_unsigned_type (unsigned0,
7553 TREE_TYPE (arg0)),
7554 int_fits_type_p (arg1, type)))
7555 result_type = type;
7558 /* Shifts can be shortened if shifting right. */
7560 if (short_shift)
7562 int unsigned_arg;
7563 tree arg0 = get_narrower (op0, &unsigned_arg);
7565 final_type = result_type;
7567 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7568 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7570 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7571 /* We can shorten only if the shift count is less than the
7572 number of bits in the smaller type size. */
7573 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7574 /* We cannot drop an unsigned shift after sign-extension. */
7575 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7577 /* Do an unsigned shift if the operand was zero-extended. */
7578 result_type
7579 = c_common_signed_or_unsigned_type (unsigned_arg,
7580 TREE_TYPE (arg0));
7581 /* Convert value-to-be-shifted to that type. */
7582 if (TREE_TYPE (op0) != result_type)
7583 op0 = convert (result_type, op0);
7584 converted = 1;
7588 /* Comparison operations are shortened too but differently.
7589 They identify themselves by setting short_compare = 1. */
7591 if (short_compare)
7593 /* Don't write &op0, etc., because that would prevent op0
7594 from being kept in a register.
7595 Instead, make copies of the our local variables and
7596 pass the copies by reference, then copy them back afterward. */
7597 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7598 enum tree_code xresultcode = resultcode;
7599 tree val
7600 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7602 if (val != 0)
7603 return val;
7605 op0 = xop0, op1 = xop1;
7606 converted = 1;
7607 resultcode = xresultcode;
7609 if (warn_sign_compare && skip_evaluation == 0)
7611 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7612 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7613 int unsignedp0, unsignedp1;
7614 tree primop0 = get_narrower (op0, &unsignedp0);
7615 tree primop1 = get_narrower (op1, &unsignedp1);
7617 xop0 = orig_op0;
7618 xop1 = orig_op1;
7619 STRIP_TYPE_NOPS (xop0);
7620 STRIP_TYPE_NOPS (xop1);
7622 /* Give warnings for comparisons between signed and unsigned
7623 quantities that may fail.
7625 Do the checking based on the original operand trees, so that
7626 casts will be considered, but default promotions won't be.
7628 Do not warn if the comparison is being done in a signed type,
7629 since the signed type will only be chosen if it can represent
7630 all the values of the unsigned type. */
7631 if (!TYPE_UNSIGNED (result_type))
7632 /* OK */;
7633 /* Do not warn if both operands are the same signedness. */
7634 else if (op0_signed == op1_signed)
7635 /* OK */;
7636 else
7638 tree sop, uop;
7640 if (op0_signed)
7641 sop = xop0, uop = xop1;
7642 else
7643 sop = xop1, uop = xop0;
7645 /* Do not warn if the signed quantity is an
7646 unsuffixed integer literal (or some static
7647 constant expression involving such literals or a
7648 conditional expression involving such literals)
7649 and it is non-negative. */
7650 if (tree_expr_nonnegative_p (sop))
7651 /* OK */;
7652 /* Do not warn if the comparison is an equality operation,
7653 the unsigned quantity is an integral constant, and it
7654 would fit in the result if the result were signed. */
7655 else if (TREE_CODE (uop) == INTEGER_CST
7656 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7657 && int_fits_type_p
7658 (uop, c_common_signed_type (result_type)))
7659 /* OK */;
7660 /* Do not warn if the unsigned quantity is an enumeration
7661 constant and its maximum value would fit in the result
7662 if the result were signed. */
7663 else if (TREE_CODE (uop) == INTEGER_CST
7664 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7665 && int_fits_type_p
7666 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7667 c_common_signed_type (result_type)))
7668 /* OK */;
7669 else
7670 warning ("comparison between signed and unsigned");
7673 /* Warn if two unsigned values are being compared in a size
7674 larger than their original size, and one (and only one) is the
7675 result of a `~' operator. This comparison will always fail.
7677 Also warn if one operand is a constant, and the constant
7678 does not have all bits set that are set in the ~ operand
7679 when it is extended. */
7681 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7682 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7684 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7685 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7686 &unsignedp0);
7687 else
7688 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7689 &unsignedp1);
7691 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7693 tree primop;
7694 HOST_WIDE_INT constant, mask;
7695 int unsignedp, bits;
7697 if (host_integerp (primop0, 0))
7699 primop = primop1;
7700 unsignedp = unsignedp1;
7701 constant = tree_low_cst (primop0, 0);
7703 else
7705 primop = primop0;
7706 unsignedp = unsignedp0;
7707 constant = tree_low_cst (primop1, 0);
7710 bits = TYPE_PRECISION (TREE_TYPE (primop));
7711 if (bits < TYPE_PRECISION (result_type)
7712 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7714 mask = (~(HOST_WIDE_INT) 0) << bits;
7715 if ((mask & constant) != mask)
7716 warning ("comparison of promoted ~unsigned with constant");
7719 else if (unsignedp0 && unsignedp1
7720 && (TYPE_PRECISION (TREE_TYPE (primop0))
7721 < TYPE_PRECISION (result_type))
7722 && (TYPE_PRECISION (TREE_TYPE (primop1))
7723 < TYPE_PRECISION (result_type)))
7724 warning ("comparison of promoted ~unsigned with unsigned");
7730 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7731 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7732 Then the expression will be built.
7733 It will be given type FINAL_TYPE if that is nonzero;
7734 otherwise, it will be given type RESULT_TYPE. */
7736 if (!result_type)
7738 binary_op_error (code);
7739 return error_mark_node;
7742 if (!converted)
7744 if (TREE_TYPE (op0) != result_type)
7745 op0 = convert (result_type, op0);
7746 if (TREE_TYPE (op1) != result_type)
7747 op1 = convert (result_type, op1);
7749 /* This can happen if one operand has a vector type, and the other
7750 has a different type. */
7751 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7752 return error_mark_node;
7755 if (build_type == NULL_TREE)
7756 build_type = result_type;
7759 tree result = build2 (resultcode, build_type, op0, op1);
7761 /* Treat expressions in initializers specially as they can't trap. */
7762 result = require_constant_value ? fold_initializer (result)
7763 : fold (result);
7765 if (final_type != 0)
7766 result = convert (final_type, result);
7767 return result;