2004-11-24 Kelley Cook <kcook@gcc.gnu.org>
[official-gcc.git] / gcc / c-typeck.c
blobc5401042b66b61bf983bb3e2dc4ed2bcf50d0b7e
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);
114 static void record_maybe_used_decl (tree);
116 /* Do `exp = require_complete_type (exp);' to make sure exp
117 does not have an incomplete type. (That includes void types.) */
119 tree
120 require_complete_type (tree value)
122 tree type = TREE_TYPE (value);
124 if (value == error_mark_node || type == error_mark_node)
125 return error_mark_node;
127 /* First, detect a valid value with a complete type. */
128 if (COMPLETE_TYPE_P (type))
129 return value;
131 c_incomplete_type_error (value, type);
132 return error_mark_node;
135 /* Print an error message for invalid use of an incomplete type.
136 VALUE is the expression that was used (or 0 if that isn't known)
137 and TYPE is the type that was invalid. */
139 void
140 c_incomplete_type_error (tree value, tree type)
142 const char *type_code_string;
144 /* Avoid duplicate error message. */
145 if (TREE_CODE (type) == ERROR_MARK)
146 return;
148 if (value != 0 && (TREE_CODE (value) == VAR_DECL
149 || TREE_CODE (value) == PARM_DECL))
150 error ("%qs has an incomplete type",
151 IDENTIFIER_POINTER (DECL_NAME (value)));
152 else
154 retry:
155 /* We must print an error message. Be clever about what it says. */
157 switch (TREE_CODE (type))
159 case RECORD_TYPE:
160 type_code_string = "struct";
161 break;
163 case UNION_TYPE:
164 type_code_string = "union";
165 break;
167 case ENUMERAL_TYPE:
168 type_code_string = "enum";
169 break;
171 case VOID_TYPE:
172 error ("invalid use of void expression");
173 return;
175 case ARRAY_TYPE:
176 if (TYPE_DOMAIN (type))
178 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
180 error ("invalid use of flexible array member");
181 return;
183 type = TREE_TYPE (type);
184 goto retry;
186 error ("invalid use of array with unspecified bounds");
187 return;
189 default:
190 gcc_unreachable ();
193 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
194 error ("invalid use of undefined type %<%s %s%>",
195 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
196 else
197 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
198 error ("invalid use of incomplete typedef %qs",
199 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
203 /* Given a type, apply default promotions wrt unnamed function
204 arguments and return the new type. */
206 tree
207 c_type_promotes_to (tree type)
209 if (TYPE_MAIN_VARIANT (type) == float_type_node)
210 return double_type_node;
212 if (c_promoting_integer_type_p (type))
214 /* Preserve unsignedness if not really getting any wider. */
215 if (TYPE_UNSIGNED (type)
216 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
217 return unsigned_type_node;
218 return integer_type_node;
221 return type;
224 /* Return a variant of TYPE which has all the type qualifiers of LIKE
225 as well as those of TYPE. */
227 static tree
228 qualify_type (tree type, tree like)
230 return c_build_qualified_type (type,
231 TYPE_QUALS (type) | TYPE_QUALS (like));
234 /* Return the composite type of two compatible types.
236 We assume that comptypes has already been done and returned
237 nonzero; if that isn't so, this may crash. In particular, we
238 assume that qualifiers match. */
240 tree
241 composite_type (tree t1, tree t2)
243 enum tree_code code1;
244 enum tree_code code2;
245 tree attributes;
247 /* Save time if the two types are the same. */
249 if (t1 == t2) return t1;
251 /* If one type is nonsense, use the other. */
252 if (t1 == error_mark_node)
253 return t2;
254 if (t2 == error_mark_node)
255 return t1;
257 code1 = TREE_CODE (t1);
258 code2 = TREE_CODE (t2);
260 /* Merge the attributes. */
261 attributes = targetm.merge_type_attributes (t1, t2);
263 /* If one is an enumerated type and the other is the compatible
264 integer type, the composite type might be either of the two
265 (DR#013 question 3). For consistency, use the enumerated type as
266 the composite type. */
268 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
269 return t1;
270 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
271 return t2;
273 gcc_assert (code1 == code2);
275 switch (code1)
277 case POINTER_TYPE:
278 /* For two pointers, do this recursively on the target type. */
280 tree pointed_to_1 = TREE_TYPE (t1);
281 tree pointed_to_2 = TREE_TYPE (t2);
282 tree target = composite_type (pointed_to_1, pointed_to_2);
283 t1 = build_pointer_type (target);
284 t1 = build_type_attribute_variant (t1, attributes);
285 return qualify_type (t1, t2);
288 case ARRAY_TYPE:
290 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
292 /* We should not have any type quals on arrays at all. */
293 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
295 /* Save space: see if the result is identical to one of the args. */
296 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
297 return build_type_attribute_variant (t1, attributes);
298 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
299 return build_type_attribute_variant (t2, attributes);
301 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
302 return build_type_attribute_variant (t1, attributes);
303 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
304 return build_type_attribute_variant (t2, attributes);
306 /* Merge the element types, and have a size if either arg has one. */
307 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
308 return build_type_attribute_variant (t1, attributes);
311 case FUNCTION_TYPE:
312 /* Function types: prefer the one that specified arg types.
313 If both do, merge the arg types. Also merge the return types. */
315 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
316 tree p1 = TYPE_ARG_TYPES (t1);
317 tree p2 = TYPE_ARG_TYPES (t2);
318 int len;
319 tree newargs, n;
320 int i;
322 /* Save space: see if the result is identical to one of the args. */
323 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
324 return build_type_attribute_variant (t1, attributes);
325 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
326 return build_type_attribute_variant (t2, attributes);
328 /* Simple way if one arg fails to specify argument types. */
329 if (TYPE_ARG_TYPES (t1) == 0)
331 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
332 t1 = build_type_attribute_variant (t1, attributes);
333 return qualify_type (t1, t2);
335 if (TYPE_ARG_TYPES (t2) == 0)
337 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
338 t1 = build_type_attribute_variant (t1, attributes);
339 return qualify_type (t1, t2);
342 /* If both args specify argument types, we must merge the two
343 lists, argument by argument. */
344 /* Tell global_bindings_p to return false so that variable_size
345 doesn't abort on VLAs in parameter types. */
346 c_override_global_bindings_to_false = true;
348 len = list_length (p1);
349 newargs = 0;
351 for (i = 0; i < len; i++)
352 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
354 n = newargs;
356 for (; p1;
357 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
359 /* A null type means arg type is not specified.
360 Take whatever the other function type has. */
361 if (TREE_VALUE (p1) == 0)
363 TREE_VALUE (n) = TREE_VALUE (p2);
364 goto parm_done;
366 if (TREE_VALUE (p2) == 0)
368 TREE_VALUE (n) = TREE_VALUE (p1);
369 goto parm_done;
372 /* Given wait (union {union wait *u; int *i} *)
373 and wait (union wait *),
374 prefer union wait * as type of parm. */
375 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
376 && TREE_VALUE (p1) != TREE_VALUE (p2))
378 tree memb;
379 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
380 memb; memb = TREE_CHAIN (memb))
381 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
383 TREE_VALUE (n) = TREE_VALUE (p2);
384 if (pedantic)
385 pedwarn ("function types not truly compatible in ISO C");
386 goto parm_done;
389 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
390 && TREE_VALUE (p2) != TREE_VALUE (p1))
392 tree memb;
393 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
394 memb; memb = TREE_CHAIN (memb))
395 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
397 TREE_VALUE (n) = TREE_VALUE (p1);
398 if (pedantic)
399 pedwarn ("function types not truly compatible in ISO C");
400 goto parm_done;
403 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
404 parm_done: ;
407 c_override_global_bindings_to_false = false;
408 t1 = build_function_type (valtype, newargs);
409 t1 = qualify_type (t1, t2);
410 /* ... falls through ... */
413 default:
414 return build_type_attribute_variant (t1, attributes);
419 /* Return the type of a conditional expression between pointers to
420 possibly differently qualified versions of compatible types.
422 We assume that comp_target_types has already been done and returned
423 nonzero; if that isn't so, this may crash. */
425 static tree
426 common_pointer_type (tree t1, tree t2)
428 tree attributes;
429 tree pointed_to_1;
430 tree pointed_to_2;
431 tree target;
433 /* Save time if the two types are the same. */
435 if (t1 == t2) return t1;
437 /* If one type is nonsense, use the other. */
438 if (t1 == error_mark_node)
439 return t2;
440 if (t2 == error_mark_node)
441 return t1;
443 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
444 && TREE_CODE (t2) == POINTER_TYPE);
446 /* Merge the attributes. */
447 attributes = targetm.merge_type_attributes (t1, t2);
449 /* Find the composite type of the target types, and combine the
450 qualifiers of the two types' targets. */
451 pointed_to_1 = TREE_TYPE (t1);
452 pointed_to_2 = TREE_TYPE (t2);
453 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
454 TYPE_MAIN_VARIANT (pointed_to_2));
455 t1 = build_pointer_type (c_build_qualified_type
456 (target,
457 TYPE_QUALS (pointed_to_1) |
458 TYPE_QUALS (pointed_to_2)));
459 return build_type_attribute_variant (t1, attributes);
462 /* Return the common type for two arithmetic types under the usual
463 arithmetic conversions. The default conversions have already been
464 applied, and enumerated types converted to their compatible integer
465 types. The resulting type is unqualified and has no attributes.
467 This is the type for the result of most arithmetic operations
468 if the operands have the given two types. */
470 tree
471 common_type (tree t1, tree t2)
473 enum tree_code code1;
474 enum tree_code code2;
476 /* If one type is nonsense, use the other. */
477 if (t1 == error_mark_node)
478 return t2;
479 if (t2 == error_mark_node)
480 return t1;
482 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
483 t1 = TYPE_MAIN_VARIANT (t1);
485 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
486 t2 = TYPE_MAIN_VARIANT (t2);
488 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
489 t1 = build_type_attribute_variant (t1, NULL_TREE);
491 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
492 t2 = build_type_attribute_variant (t2, NULL_TREE);
494 /* Save time if the two types are the same. */
496 if (t1 == t2) return t1;
498 code1 = TREE_CODE (t1);
499 code2 = TREE_CODE (t2);
501 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
502 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
503 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
504 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
506 /* If one type is a vector type, return that type. (How the usual
507 arithmetic conversions apply to the vector types extension is not
508 precisely specified.) */
509 if (code1 == VECTOR_TYPE)
510 return t1;
512 if (code2 == VECTOR_TYPE)
513 return t2;
515 /* If one type is complex, form the common type of the non-complex
516 components, then make that complex. Use T1 or T2 if it is the
517 required type. */
518 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
520 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
521 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
522 tree subtype = common_type (subtype1, subtype2);
524 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
525 return t1;
526 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
527 return t2;
528 else
529 return build_complex_type (subtype);
532 /* If only one is real, use it as the result. */
534 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
535 return t1;
537 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
538 return t2;
540 /* Both real or both integers; use the one with greater precision. */
542 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
543 return t1;
544 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
545 return t2;
547 /* Same precision. Prefer long longs to longs to ints when the
548 same precision, following the C99 rules on integer type rank
549 (which are equivalent to the C90 rules for C90 types). */
551 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
552 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
553 return long_long_unsigned_type_node;
555 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
556 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
558 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
559 return long_long_unsigned_type_node;
560 else
561 return long_long_integer_type_node;
564 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
565 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
566 return long_unsigned_type_node;
568 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
569 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
571 /* But preserve unsignedness from the other type,
572 since long cannot hold all the values of an unsigned int. */
573 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
574 return long_unsigned_type_node;
575 else
576 return long_integer_type_node;
579 /* Likewise, prefer long double to double even if same size. */
580 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
581 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
582 return long_double_type_node;
584 /* Otherwise prefer the unsigned one. */
586 if (TYPE_UNSIGNED (t1))
587 return t1;
588 else
589 return t2;
592 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
593 or various other operations. Return 2 if they are compatible
594 but a warning may be needed if you use them together. */
597 comptypes (tree type1, tree type2)
599 tree t1 = type1;
600 tree t2 = type2;
601 int attrval, val;
603 /* Suppress errors caused by previously reported errors. */
605 if (t1 == t2 || !t1 || !t2
606 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
607 return 1;
609 /* If either type is the internal version of sizetype, return the
610 language version. */
611 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
612 && TYPE_ORIG_SIZE_TYPE (t1))
613 t1 = TYPE_ORIG_SIZE_TYPE (t1);
615 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
616 && TYPE_ORIG_SIZE_TYPE (t2))
617 t2 = TYPE_ORIG_SIZE_TYPE (t2);
620 /* Enumerated types are compatible with integer types, but this is
621 not transitive: two enumerated types in the same translation unit
622 are compatible with each other only if they are the same type. */
624 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
625 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
626 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
627 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
629 if (t1 == t2)
630 return 1;
632 /* Different classes of types can't be compatible. */
634 if (TREE_CODE (t1) != TREE_CODE (t2))
635 return 0;
637 /* Qualifiers must match. C99 6.7.3p9 */
639 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
640 return 0;
642 /* Allow for two different type nodes which have essentially the same
643 definition. Note that we already checked for equality of the type
644 qualifiers (just above). */
646 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
647 return 1;
649 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
650 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
651 return 0;
653 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
654 val = 0;
656 switch (TREE_CODE (t1))
658 case POINTER_TYPE:
659 /* We must give ObjC the first crack at comparing pointers, since
660 protocol qualifiers may be involved. */
661 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
662 break;
663 /* Do not remove mode or aliasing information. */
664 if (TYPE_MODE (t1) != TYPE_MODE (t2)
665 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
666 break;
667 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
668 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
669 break;
671 case FUNCTION_TYPE:
672 val = function_types_compatible_p (t1, t2);
673 break;
675 case ARRAY_TYPE:
677 tree d1 = TYPE_DOMAIN (t1);
678 tree d2 = TYPE_DOMAIN (t2);
679 bool d1_variable, d2_variable;
680 bool d1_zero, d2_zero;
681 val = 1;
683 /* Target types must match incl. qualifiers. */
684 if (TREE_TYPE (t1) != TREE_TYPE (t2)
685 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
686 return 0;
688 /* Sizes must match unless one is missing or variable. */
689 if (d1 == 0 || d2 == 0 || d1 == d2)
690 break;
692 d1_zero = !TYPE_MAX_VALUE (d1);
693 d2_zero = !TYPE_MAX_VALUE (d2);
695 d1_variable = (!d1_zero
696 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
697 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
698 d2_variable = (!d2_zero
699 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
700 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
702 if (d1_variable || d2_variable)
703 break;
704 if (d1_zero && d2_zero)
705 break;
706 if (d1_zero || d2_zero
707 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
708 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
709 val = 0;
711 break;
714 case RECORD_TYPE:
715 /* We are dealing with two distinct structs. In assorted Objective-C
716 corner cases, however, these can still be deemed equivalent. */
717 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
718 val = 1;
720 case ENUMERAL_TYPE:
721 case UNION_TYPE:
722 if (val != 1 && !same_translation_unit_p (t1, t2))
723 val = tagged_types_tu_compatible_p (t1, t2);
724 break;
726 case VECTOR_TYPE:
727 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
728 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
729 break;
731 default:
732 break;
734 return attrval == 2 && val == 1 ? 2 : val;
737 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
738 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
739 to 1 or 0 depending if the check of the pointer types is meant to
740 be reflexive or not (typically, assignments are not reflexive,
741 while comparisons are reflexive).
744 static int
745 comp_target_types (tree ttl, tree ttr, int reflexive)
747 int val;
749 /* Give objc_comptypes a crack at letting these types through. */
750 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
751 return val;
753 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
754 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
756 if (val == 2 && pedantic)
757 pedwarn ("types are not quite compatible");
758 return val;
761 /* Subroutines of `comptypes'. */
763 /* Determine whether two trees derive from the same translation unit.
764 If the CONTEXT chain ends in a null, that tree's context is still
765 being parsed, so if two trees have context chains ending in null,
766 they're in the same translation unit. */
768 same_translation_unit_p (tree t1, tree t2)
770 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
771 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
773 case tcc_declaration:
774 t1 = DECL_CONTEXT (t1); break;
775 case tcc_type:
776 t1 = TYPE_CONTEXT (t1); break;
777 case tcc_exceptional:
778 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
779 default: gcc_unreachable ();
782 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
783 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
785 case tcc_declaration:
786 t2 = DECL_CONTEXT (t2); break;
787 case tcc_type:
788 t2 = TYPE_CONTEXT (t2); break;
789 case tcc_exceptional:
790 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
791 default: gcc_unreachable ();
794 return t1 == t2;
797 /* The C standard says that two structures in different translation
798 units are compatible with each other only if the types of their
799 fields are compatible (among other things). So, consider two copies
800 of this structure: */
802 struct tagged_tu_seen {
803 const struct tagged_tu_seen * next;
804 tree t1;
805 tree t2;
808 /* Can they be compatible with each other? We choose to break the
809 recursion by allowing those types to be compatible. */
811 static const struct tagged_tu_seen * tagged_tu_seen_base;
813 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
814 compatible. If the two types are not the same (which has been
815 checked earlier), this can only happen when multiple translation
816 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
817 rules. */
819 static int
820 tagged_types_tu_compatible_p (tree t1, tree t2)
822 tree s1, s2;
823 bool needs_warning = false;
825 /* We have to verify that the tags of the types are the same. This
826 is harder than it looks because this may be a typedef, so we have
827 to go look at the original type. It may even be a typedef of a
828 typedef...
829 In the case of compiler-created builtin structs the TYPE_DECL
830 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
831 while (TYPE_NAME (t1)
832 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
833 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
834 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
836 while (TYPE_NAME (t2)
837 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
838 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
839 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
841 /* C90 didn't have the requirement that the two tags be the same. */
842 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
843 return 0;
845 /* C90 didn't say what happened if one or both of the types were
846 incomplete; we choose to follow C99 rules here, which is that they
847 are compatible. */
848 if (TYPE_SIZE (t1) == NULL
849 || TYPE_SIZE (t2) == NULL)
850 return 1;
853 const struct tagged_tu_seen * tts_i;
854 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
855 if (tts_i->t1 == t1 && tts_i->t2 == t2)
856 return 1;
859 switch (TREE_CODE (t1))
861 case ENUMERAL_TYPE:
864 /* Speed up the case where the type values are in the same order. */
865 tree tv1 = TYPE_VALUES (t1);
866 tree tv2 = TYPE_VALUES (t2);
868 if (tv1 == tv2)
869 return 1;
871 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
873 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
874 break;
875 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
876 return 0;
879 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
880 return 1;
881 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
882 return 0;
884 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
885 return 0;
887 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
889 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
890 if (s2 == NULL
891 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
892 return 0;
894 return 1;
897 case UNION_TYPE:
899 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
900 return 0;
902 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
904 bool ok = false;
905 struct tagged_tu_seen tts;
907 tts.next = tagged_tu_seen_base;
908 tts.t1 = t1;
909 tts.t2 = t2;
910 tagged_tu_seen_base = &tts;
912 if (DECL_NAME (s1) != NULL)
913 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
914 if (DECL_NAME (s1) == DECL_NAME (s2))
916 int result;
917 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
918 if (result == 0)
919 break;
920 if (result == 2)
921 needs_warning = true;
923 if (TREE_CODE (s1) == FIELD_DECL
924 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
925 DECL_FIELD_BIT_OFFSET (s2)) != 1)
926 break;
928 ok = true;
929 break;
931 tagged_tu_seen_base = tts.next;
932 if (!ok)
933 return 0;
935 return needs_warning ? 2 : 1;
938 case RECORD_TYPE:
940 struct tagged_tu_seen tts;
942 tts.next = tagged_tu_seen_base;
943 tts.t1 = t1;
944 tts.t2 = t2;
945 tagged_tu_seen_base = &tts;
947 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
948 s1 && s2;
949 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
951 int result;
952 if (TREE_CODE (s1) != TREE_CODE (s2)
953 || DECL_NAME (s1) != DECL_NAME (s2))
954 break;
955 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
956 if (result == 0)
957 break;
958 if (result == 2)
959 needs_warning = true;
961 if (TREE_CODE (s1) == FIELD_DECL
962 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
963 DECL_FIELD_BIT_OFFSET (s2)) != 1)
964 break;
966 tagged_tu_seen_base = tts.next;
967 if (s1 && s2)
968 return 0;
969 return needs_warning ? 2 : 1;
972 default:
973 gcc_unreachable ();
977 /* Return 1 if two function types F1 and F2 are compatible.
978 If either type specifies no argument types,
979 the other must specify a fixed number of self-promoting arg types.
980 Otherwise, if one type specifies only the number of arguments,
981 the other must specify that number of self-promoting arg types.
982 Otherwise, the argument types must match. */
984 static int
985 function_types_compatible_p (tree f1, tree f2)
987 tree args1, args2;
988 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
989 int val = 1;
990 int val1;
991 tree ret1, ret2;
993 ret1 = TREE_TYPE (f1);
994 ret2 = TREE_TYPE (f2);
996 /* 'volatile' qualifiers on a function's return type used to mean
997 the function is noreturn. */
998 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
999 pedwarn ("function return types not compatible due to %<volatile%>");
1000 if (TYPE_VOLATILE (ret1))
1001 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1002 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1003 if (TYPE_VOLATILE (ret2))
1004 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1005 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1006 val = comptypes (ret1, ret2);
1007 if (val == 0)
1008 return 0;
1010 args1 = TYPE_ARG_TYPES (f1);
1011 args2 = TYPE_ARG_TYPES (f2);
1013 /* An unspecified parmlist matches any specified parmlist
1014 whose argument types don't need default promotions. */
1016 if (args1 == 0)
1018 if (!self_promoting_args_p (args2))
1019 return 0;
1020 /* If one of these types comes from a non-prototype fn definition,
1021 compare that with the other type's arglist.
1022 If they don't match, ask for a warning (but no error). */
1023 if (TYPE_ACTUAL_ARG_TYPES (f1)
1024 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1025 val = 2;
1026 return val;
1028 if (args2 == 0)
1030 if (!self_promoting_args_p (args1))
1031 return 0;
1032 if (TYPE_ACTUAL_ARG_TYPES (f2)
1033 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1034 val = 2;
1035 return val;
1038 /* Both types have argument lists: compare them and propagate results. */
1039 val1 = type_lists_compatible_p (args1, args2);
1040 return val1 != 1 ? val1 : val;
1043 /* Check two lists of types for compatibility,
1044 returning 0 for incompatible, 1 for compatible,
1045 or 2 for compatible with warning. */
1047 static int
1048 type_lists_compatible_p (tree args1, tree args2)
1050 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1051 int val = 1;
1052 int newval = 0;
1054 while (1)
1056 if (args1 == 0 && args2 == 0)
1057 return val;
1058 /* If one list is shorter than the other,
1059 they fail to match. */
1060 if (args1 == 0 || args2 == 0)
1061 return 0;
1062 /* A null pointer instead of a type
1063 means there is supposed to be an argument
1064 but nothing is specified about what type it has.
1065 So match anything that self-promotes. */
1066 if (TREE_VALUE (args1) == 0)
1068 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1069 return 0;
1071 else if (TREE_VALUE (args2) == 0)
1073 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1074 return 0;
1076 /* If one of the lists has an error marker, ignore this arg. */
1077 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1078 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1080 else if (!(newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1081 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1083 /* Allow wait (union {union wait *u; int *i} *)
1084 and wait (union wait *) to be compatible. */
1085 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1086 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1087 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1088 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1089 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1090 TYPE_SIZE (TREE_VALUE (args2))))
1092 tree memb;
1093 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1094 memb; memb = TREE_CHAIN (memb))
1095 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1096 break;
1097 if (memb == 0)
1098 return 0;
1100 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1101 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1102 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1103 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1104 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1105 TYPE_SIZE (TREE_VALUE (args1))))
1107 tree memb;
1108 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1109 memb; memb = TREE_CHAIN (memb))
1110 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1111 break;
1112 if (memb == 0)
1113 return 0;
1115 else
1116 return 0;
1119 /* comptypes said ok, but record if it said to warn. */
1120 if (newval > val)
1121 val = newval;
1123 args1 = TREE_CHAIN (args1);
1124 args2 = TREE_CHAIN (args2);
1128 /* Compute the size to increment a pointer by. */
1130 static tree
1131 c_size_in_bytes (tree type)
1133 enum tree_code code = TREE_CODE (type);
1135 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1136 return size_one_node;
1138 if (!COMPLETE_OR_VOID_TYPE_P (type))
1140 error ("arithmetic on pointer to an incomplete type");
1141 return size_one_node;
1144 /* Convert in case a char is more than one unit. */
1145 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1146 size_int (TYPE_PRECISION (char_type_node)
1147 / BITS_PER_UNIT));
1150 /* Return either DECL or its known constant value (if it has one). */
1152 tree
1153 decl_constant_value (tree decl)
1155 if (/* Don't change a variable array bound or initial value to a constant
1156 in a place where a variable is invalid. Note that DECL_INITIAL
1157 isn't valid for a PARM_DECL. */
1158 current_function_decl != 0
1159 && TREE_CODE (decl) != PARM_DECL
1160 && !TREE_THIS_VOLATILE (decl)
1161 && TREE_READONLY (decl)
1162 && DECL_INITIAL (decl) != 0
1163 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1164 /* This is invalid if initial value is not constant.
1165 If it has either a function call, a memory reference,
1166 or a variable, then re-evaluating it could give different results. */
1167 && TREE_CONSTANT (DECL_INITIAL (decl))
1168 /* Check for cases where this is sub-optimal, even though valid. */
1169 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1170 return DECL_INITIAL (decl);
1171 return decl;
1174 /* Return either DECL or its known constant value (if it has one), but
1175 return DECL if pedantic or DECL has mode BLKmode. This is for
1176 bug-compatibility with the old behavior of decl_constant_value
1177 (before GCC 3.0); every use of this function is a bug and it should
1178 be removed before GCC 3.1. It is not appropriate to use pedantic
1179 in a way that affects optimization, and BLKmode is probably not the
1180 right test for avoiding misoptimizations either. */
1182 static tree
1183 decl_constant_value_for_broken_optimization (tree decl)
1185 if (pedantic || DECL_MODE (decl) == BLKmode)
1186 return decl;
1187 else
1188 return decl_constant_value (decl);
1192 /* Perform the default conversion of arrays and functions to pointers.
1193 Return the result of converting EXP. For any other expression, just
1194 return EXP. */
1196 static tree
1197 default_function_array_conversion (tree exp)
1199 tree orig_exp;
1200 tree type = TREE_TYPE (exp);
1201 enum tree_code code = TREE_CODE (type);
1202 int not_lvalue = 0;
1204 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1205 an lvalue.
1207 Do not use STRIP_NOPS here! It will remove conversions from pointer
1208 to integer and cause infinite recursion. */
1209 orig_exp = exp;
1210 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1211 || (TREE_CODE (exp) == NOP_EXPR
1212 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1214 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1215 not_lvalue = 1;
1216 exp = TREE_OPERAND (exp, 0);
1219 if (TREE_NO_WARNING (orig_exp))
1220 TREE_NO_WARNING (exp) = 1;
1222 if (code == FUNCTION_TYPE)
1224 return build_unary_op (ADDR_EXPR, exp, 0);
1226 if (code == ARRAY_TYPE)
1228 tree adr;
1229 tree restype = TREE_TYPE (type);
1230 tree ptrtype;
1231 int constp = 0;
1232 int volatilep = 0;
1233 int lvalue_array_p;
1235 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1237 constp = TREE_READONLY (exp);
1238 volatilep = TREE_THIS_VOLATILE (exp);
1241 if (TYPE_QUALS (type) || constp || volatilep)
1242 restype
1243 = c_build_qualified_type (restype,
1244 TYPE_QUALS (type)
1245 | (constp * TYPE_QUAL_CONST)
1246 | (volatilep * TYPE_QUAL_VOLATILE));
1248 if (TREE_CODE (exp) == INDIRECT_REF)
1249 return convert (build_pointer_type (restype),
1250 TREE_OPERAND (exp, 0));
1252 if (TREE_CODE (exp) == COMPOUND_EXPR)
1254 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1255 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1256 TREE_OPERAND (exp, 0), op1);
1259 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1260 if (!flag_isoc99 && !lvalue_array_p)
1262 /* Before C99, non-lvalue arrays do not decay to pointers.
1263 Normally, using such an array would be invalid; but it can
1264 be used correctly inside sizeof or as a statement expression.
1265 Thus, do not give an error here; an error will result later. */
1266 return exp;
1269 ptrtype = build_pointer_type (restype);
1271 if (TREE_CODE (exp) == VAR_DECL)
1273 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1274 ADDR_EXPR because it's the best way of representing what
1275 happens in C when we take the address of an array and place
1276 it in a pointer to the element type. */
1277 adr = build1 (ADDR_EXPR, ptrtype, exp);
1278 if (!c_mark_addressable (exp))
1279 return error_mark_node;
1280 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1281 return adr;
1283 /* This way is better for a COMPONENT_REF since it can
1284 simplify the offset for a component. */
1285 adr = build_unary_op (ADDR_EXPR, exp, 1);
1286 return convert (ptrtype, adr);
1288 return exp;
1291 /* Perform default promotions for C data used in expressions.
1292 Arrays and functions are converted to pointers;
1293 enumeral types or short or char, to int.
1294 In addition, manifest constants symbols are replaced by their values. */
1296 tree
1297 default_conversion (tree exp)
1299 tree orig_exp;
1300 tree type = TREE_TYPE (exp);
1301 enum tree_code code = TREE_CODE (type);
1303 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1304 return default_function_array_conversion (exp);
1306 /* Constants can be used directly unless they're not loadable. */
1307 if (TREE_CODE (exp) == CONST_DECL)
1308 exp = DECL_INITIAL (exp);
1310 /* Replace a nonvolatile const static variable with its value unless
1311 it is an array, in which case we must be sure that taking the
1312 address of the array produces consistent results. */
1313 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1315 exp = decl_constant_value_for_broken_optimization (exp);
1316 type = TREE_TYPE (exp);
1319 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1320 an lvalue.
1322 Do not use STRIP_NOPS here! It will remove conversions from pointer
1323 to integer and cause infinite recursion. */
1324 orig_exp = exp;
1325 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1326 || (TREE_CODE (exp) == NOP_EXPR
1327 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1328 exp = TREE_OPERAND (exp, 0);
1330 if (TREE_NO_WARNING (orig_exp))
1331 TREE_NO_WARNING (exp) = 1;
1333 /* Normally convert enums to int,
1334 but convert wide enums to something wider. */
1335 if (code == ENUMERAL_TYPE)
1337 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1338 TYPE_PRECISION (integer_type_node)),
1339 ((TYPE_PRECISION (type)
1340 >= TYPE_PRECISION (integer_type_node))
1341 && TYPE_UNSIGNED (type)));
1343 return convert (type, exp);
1346 if (TREE_CODE (exp) == COMPONENT_REF
1347 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1348 /* If it's thinner than an int, promote it like a
1349 c_promoting_integer_type_p, otherwise leave it alone. */
1350 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1351 TYPE_PRECISION (integer_type_node)))
1352 return convert (integer_type_node, exp);
1354 if (c_promoting_integer_type_p (type))
1356 /* Preserve unsignedness if not really getting any wider. */
1357 if (TYPE_UNSIGNED (type)
1358 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1359 return convert (unsigned_type_node, exp);
1361 return convert (integer_type_node, exp);
1364 if (code == VOID_TYPE)
1366 error ("void value not ignored as it ought to be");
1367 return error_mark_node;
1369 return exp;
1372 /* Look up COMPONENT in a structure or union DECL.
1374 If the component name is not found, returns NULL_TREE. Otherwise,
1375 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1376 stepping down the chain to the component, which is in the last
1377 TREE_VALUE of the list. Normally the list is of length one, but if
1378 the component is embedded within (nested) anonymous structures or
1379 unions, the list steps down the chain to the component. */
1381 static tree
1382 lookup_field (tree decl, tree component)
1384 tree type = TREE_TYPE (decl);
1385 tree field;
1387 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1388 to the field elements. Use a binary search on this array to quickly
1389 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1390 will always be set for structures which have many elements. */
1392 if (TYPE_LANG_SPECIFIC (type))
1394 int bot, top, half;
1395 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1397 field = TYPE_FIELDS (type);
1398 bot = 0;
1399 top = TYPE_LANG_SPECIFIC (type)->s->len;
1400 while (top - bot > 1)
1402 half = (top - bot + 1) >> 1;
1403 field = field_array[bot+half];
1405 if (DECL_NAME (field) == NULL_TREE)
1407 /* Step through all anon unions in linear fashion. */
1408 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1410 field = field_array[bot++];
1411 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1412 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1414 tree anon = lookup_field (field, component);
1416 if (anon)
1417 return tree_cons (NULL_TREE, field, anon);
1421 /* Entire record is only anon unions. */
1422 if (bot > top)
1423 return NULL_TREE;
1425 /* Restart the binary search, with new lower bound. */
1426 continue;
1429 if (DECL_NAME (field) == component)
1430 break;
1431 if (DECL_NAME (field) < component)
1432 bot += half;
1433 else
1434 top = bot + half;
1437 if (DECL_NAME (field_array[bot]) == component)
1438 field = field_array[bot];
1439 else if (DECL_NAME (field) != component)
1440 return NULL_TREE;
1442 else
1444 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1446 if (DECL_NAME (field) == NULL_TREE
1447 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1448 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1450 tree anon = lookup_field (field, component);
1452 if (anon)
1453 return tree_cons (NULL_TREE, field, anon);
1456 if (DECL_NAME (field) == component)
1457 break;
1460 if (field == NULL_TREE)
1461 return NULL_TREE;
1464 return tree_cons (NULL_TREE, field, NULL_TREE);
1467 /* Make an expression to refer to the COMPONENT field of
1468 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1470 tree
1471 build_component_ref (tree datum, tree component)
1473 tree type = TREE_TYPE (datum);
1474 enum tree_code code = TREE_CODE (type);
1475 tree field = NULL;
1476 tree ref;
1478 if (!objc_is_public (datum, component))
1479 return error_mark_node;
1481 /* See if there is a field or component with name COMPONENT. */
1483 if (code == RECORD_TYPE || code == UNION_TYPE)
1485 if (!COMPLETE_TYPE_P (type))
1487 c_incomplete_type_error (NULL_TREE, type);
1488 return error_mark_node;
1491 field = lookup_field (datum, component);
1493 if (!field)
1495 error ("%qT has no member named %qs", type,
1496 IDENTIFIER_POINTER (component));
1497 return error_mark_node;
1500 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1501 This might be better solved in future the way the C++ front
1502 end does it - by giving the anonymous entities each a
1503 separate name and type, and then have build_component_ref
1504 recursively call itself. We can't do that here. */
1507 tree subdatum = TREE_VALUE (field);
1509 if (TREE_TYPE (subdatum) == error_mark_node)
1510 return error_mark_node;
1512 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1513 NULL_TREE);
1514 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1515 TREE_READONLY (ref) = 1;
1516 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1517 TREE_THIS_VOLATILE (ref) = 1;
1519 if (TREE_DEPRECATED (subdatum))
1520 warn_deprecated_use (subdatum);
1522 datum = ref;
1524 field = TREE_CHAIN (field);
1526 while (field);
1528 return ref;
1530 else if (code != ERROR_MARK)
1531 error ("request for member %qs in something not a structure or union",
1532 IDENTIFIER_POINTER (component));
1534 return error_mark_node;
1537 /* Given an expression PTR for a pointer, return an expression
1538 for the value pointed to.
1539 ERRORSTRING is the name of the operator to appear in error messages. */
1541 tree
1542 build_indirect_ref (tree ptr, const char *errorstring)
1544 tree pointer = default_conversion (ptr);
1545 tree type = TREE_TYPE (pointer);
1547 if (TREE_CODE (type) == POINTER_TYPE)
1549 if (TREE_CODE (pointer) == ADDR_EXPR
1550 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1551 == TREE_TYPE (type)))
1552 return TREE_OPERAND (pointer, 0);
1553 else
1555 tree t = TREE_TYPE (type);
1556 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1558 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1560 error ("dereferencing pointer to incomplete type");
1561 return error_mark_node;
1563 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1564 warning ("dereferencing %<void *%> pointer");
1566 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1567 so that we get the proper error message if the result is used
1568 to assign to. Also, &* is supposed to be a no-op.
1569 And ANSI C seems to specify that the type of the result
1570 should be the const type. */
1571 /* A de-reference of a pointer to const is not a const. It is valid
1572 to change it via some other pointer. */
1573 TREE_READONLY (ref) = TYPE_READONLY (t);
1574 TREE_SIDE_EFFECTS (ref)
1575 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1576 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1577 return ref;
1580 else if (TREE_CODE (pointer) != ERROR_MARK)
1581 error ("invalid type argument of %qs", errorstring);
1582 return error_mark_node;
1585 /* This handles expressions of the form "a[i]", which denotes
1586 an array reference.
1588 This is logically equivalent in C to *(a+i), but we may do it differently.
1589 If A is a variable or a member, we generate a primitive ARRAY_REF.
1590 This avoids forcing the array out of registers, and can work on
1591 arrays that are not lvalues (for example, members of structures returned
1592 by functions). */
1594 tree
1595 build_array_ref (tree array, tree index)
1597 bool swapped = false;
1598 if (TREE_TYPE (array) == error_mark_node
1599 || TREE_TYPE (index) == error_mark_node)
1600 return error_mark_node;
1602 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1603 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1605 tree temp;
1606 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1607 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1609 error ("subscripted value is neither array nor pointer");
1610 return error_mark_node;
1612 temp = array;
1613 array = index;
1614 index = temp;
1615 swapped = true;
1618 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1620 error ("array subscript is not an integer");
1621 return error_mark_node;
1624 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1626 error ("subscripted value is pointer to function");
1627 return error_mark_node;
1630 /* Subscripting with type char is likely to lose on a machine where
1631 chars are signed. So warn on any machine, but optionally. Don't
1632 warn for unsigned char since that type is safe. Don't warn for
1633 signed char because anyone who uses that must have done so
1634 deliberately. ??? Existing practice has also been to warn only
1635 when the char index is syntactically the index, not for
1636 char[array]. */
1637 if (warn_char_subscripts && !swapped
1638 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1639 warning ("array subscript has type %<char%>");
1641 /* Apply default promotions *after* noticing character types. */
1642 index = default_conversion (index);
1644 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1646 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1648 tree rval, type;
1650 /* An array that is indexed by a non-constant
1651 cannot be stored in a register; we must be able to do
1652 address arithmetic on its address.
1653 Likewise an array of elements of variable size. */
1654 if (TREE_CODE (index) != INTEGER_CST
1655 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1656 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1658 if (!c_mark_addressable (array))
1659 return error_mark_node;
1661 /* An array that is indexed by a constant value which is not within
1662 the array bounds cannot be stored in a register either; because we
1663 would get a crash in store_bit_field/extract_bit_field when trying
1664 to access a non-existent part of the register. */
1665 if (TREE_CODE (index) == INTEGER_CST
1666 && TYPE_DOMAIN (TREE_TYPE (array))
1667 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1669 if (!c_mark_addressable (array))
1670 return error_mark_node;
1673 if (pedantic)
1675 tree foo = array;
1676 while (TREE_CODE (foo) == COMPONENT_REF)
1677 foo = TREE_OPERAND (foo, 0);
1678 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1679 pedwarn ("ISO C forbids subscripting %<register%> array");
1680 else if (!flag_isoc99 && !lvalue_p (foo))
1681 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1684 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1685 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1686 /* Array ref is const/volatile if the array elements are
1687 or if the array is. */
1688 TREE_READONLY (rval)
1689 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1690 | TREE_READONLY (array));
1691 TREE_SIDE_EFFECTS (rval)
1692 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1693 | TREE_SIDE_EFFECTS (array));
1694 TREE_THIS_VOLATILE (rval)
1695 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1696 /* This was added by rms on 16 Nov 91.
1697 It fixes vol struct foo *a; a->elts[1]
1698 in an inline function.
1699 Hope it doesn't break something else. */
1700 | TREE_THIS_VOLATILE (array));
1701 return require_complete_type (fold (rval));
1703 else
1705 tree ar = default_conversion (array);
1707 if (ar == error_mark_node)
1708 return ar;
1710 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1711 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1713 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1714 "array indexing");
1718 /* Build an external reference to identifier ID. FUN indicates
1719 whether this will be used for a function call. */
1720 tree
1721 build_external_ref (tree id, int fun)
1723 tree ref;
1724 tree decl = lookup_name (id);
1726 /* In Objective-C, an instance variable (ivar) may be preferred to
1727 whatever lookup_name() found. */
1728 decl = objc_lookup_ivar (decl, id);
1730 if (decl && decl != error_mark_node)
1731 ref = decl;
1732 else if (fun)
1733 /* Implicit function declaration. */
1734 ref = implicitly_declare (id);
1735 else if (decl == error_mark_node)
1736 /* Don't complain about something that's already been
1737 complained about. */
1738 return error_mark_node;
1739 else
1741 undeclared_variable (id);
1742 return error_mark_node;
1745 if (TREE_TYPE (ref) == error_mark_node)
1746 return error_mark_node;
1748 if (TREE_DEPRECATED (ref))
1749 warn_deprecated_use (ref);
1751 if (!skip_evaluation)
1752 assemble_external (ref);
1753 TREE_USED (ref) = 1;
1755 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1757 if (!in_sizeof && !in_typeof)
1758 C_DECL_USED (ref) = 1;
1759 else if (DECL_INITIAL (ref) == 0
1760 && DECL_EXTERNAL (ref)
1761 && !TREE_PUBLIC (ref))
1762 record_maybe_used_decl (ref);
1765 if (TREE_CODE (ref) == CONST_DECL)
1767 ref = DECL_INITIAL (ref);
1768 TREE_CONSTANT (ref) = 1;
1769 TREE_INVARIANT (ref) = 1;
1771 else if (current_function_decl != 0
1772 && !DECL_FILE_SCOPE_P (current_function_decl)
1773 && (TREE_CODE (ref) == VAR_DECL
1774 || TREE_CODE (ref) == PARM_DECL
1775 || TREE_CODE (ref) == FUNCTION_DECL))
1777 tree context = decl_function_context (ref);
1779 if (context != 0 && context != current_function_decl)
1780 DECL_NONLOCAL (ref) = 1;
1783 return ref;
1786 /* Record details of decls possibly used inside sizeof or typeof. */
1787 struct maybe_used_decl
1789 /* The decl. */
1790 tree decl;
1791 /* The level seen at (in_sizeof + in_typeof). */
1792 int level;
1793 /* The next one at this level or above, or NULL. */
1794 struct maybe_used_decl *next;
1797 static struct maybe_used_decl *maybe_used_decls;
1799 /* Record that DECL, an undefined static function reference seen
1800 inside sizeof or typeof, might be used if the operand of sizeof is
1801 a VLA type or the operand of typeof is a variably modified
1802 type. */
1804 static void
1805 record_maybe_used_decl (tree decl)
1807 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1808 t->decl = decl;
1809 t->level = in_sizeof + in_typeof;
1810 t->next = maybe_used_decls;
1811 maybe_used_decls = t;
1814 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1815 USED is false, just discard them. If it is true, mark them used
1816 (if no longer inside sizeof or typeof) or move them to the next
1817 level up (if still inside sizeof or typeof). */
1819 void
1820 pop_maybe_used (bool used)
1822 struct maybe_used_decl *p = maybe_used_decls;
1823 int cur_level = in_sizeof + in_typeof;
1824 while (p && p->level > cur_level)
1826 if (used)
1828 if (cur_level == 0)
1829 C_DECL_USED (p->decl) = 1;
1830 else
1831 p->level = cur_level;
1833 p = p->next;
1835 if (!used || cur_level == 0)
1836 maybe_used_decls = p;
1839 /* Return the result of sizeof applied to EXPR. */
1841 struct c_expr
1842 c_expr_sizeof_expr (struct c_expr expr)
1844 struct c_expr ret;
1845 if (expr.value == error_mark_node)
1847 ret.value = error_mark_node;
1848 ret.original_code = ERROR_MARK;
1849 pop_maybe_used (false);
1851 else
1853 ret.value = c_sizeof (TREE_TYPE (expr.value));
1854 ret.original_code = ERROR_MARK;
1855 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1857 return ret;
1860 /* Return the result of sizeof applied to T, a structure for the type
1861 name passed to sizeof (rather than the type itself). */
1863 struct c_expr
1864 c_expr_sizeof_type (struct c_type_name *t)
1866 tree type;
1867 struct c_expr ret;
1868 type = groktypename (t);
1869 ret.value = c_sizeof (type);
1870 ret.original_code = ERROR_MARK;
1871 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1872 return ret;
1875 /* Build a function call to function FUNCTION with parameters PARAMS.
1876 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1877 TREE_VALUE of each node is a parameter-expression.
1878 FUNCTION's data type may be a function type or a pointer-to-function. */
1880 tree
1881 build_function_call (tree function, tree params)
1883 tree fntype, fundecl = 0;
1884 tree coerced_params;
1885 tree name = NULL_TREE, result;
1886 tree tem;
1888 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1889 STRIP_TYPE_NOPS (function);
1891 /* Convert anything with function type to a pointer-to-function. */
1892 if (TREE_CODE (function) == FUNCTION_DECL)
1894 name = DECL_NAME (function);
1896 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1897 (because calling an inline function does not mean the function
1898 needs to be separately compiled). */
1899 fntype = build_type_variant (TREE_TYPE (function),
1900 TREE_READONLY (function),
1901 TREE_THIS_VOLATILE (function));
1902 fundecl = function;
1903 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1905 else
1906 function = default_conversion (function);
1908 fntype = TREE_TYPE (function);
1910 if (TREE_CODE (fntype) == ERROR_MARK)
1911 return error_mark_node;
1913 if (!(TREE_CODE (fntype) == POINTER_TYPE
1914 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1916 error ("called object %qE is not a function", function);
1917 return error_mark_node;
1920 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1921 current_function_returns_abnormally = 1;
1923 /* fntype now gets the type of function pointed to. */
1924 fntype = TREE_TYPE (fntype);
1926 /* Check that the function is called through a compatible prototype.
1927 If it is not, replace the call by a trap, wrapped up in a compound
1928 expression if necessary. This has the nice side-effect to prevent
1929 the tree-inliner from generating invalid assignment trees which may
1930 blow up in the RTL expander later.
1932 ??? This doesn't work for Objective-C because objc_comptypes
1933 refuses to compare function prototypes, yet the compiler appears
1934 to build calls that are flagged as invalid by C's comptypes. */
1935 if (!c_dialect_objc ()
1936 && TREE_CODE (function) == NOP_EXPR
1937 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1938 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1939 && !comptypes (fntype, TREE_TYPE (tem)))
1941 tree return_type = TREE_TYPE (fntype);
1942 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1943 NULL_TREE);
1945 /* This situation leads to run-time undefined behavior. We can't,
1946 therefore, simply error unless we can prove that all possible
1947 executions of the program must execute the code. */
1948 warning ("function called through a non-compatible type");
1950 /* We can, however, treat "undefined" any way we please.
1951 Call abort to encourage the user to fix the program. */
1952 inform ("if this code is reached, the program will abort");
1954 if (VOID_TYPE_P (return_type))
1955 return trap;
1956 else
1958 tree rhs;
1960 if (AGGREGATE_TYPE_P (return_type))
1961 rhs = build_compound_literal (return_type,
1962 build_constructor (return_type,
1963 NULL_TREE));
1964 else
1965 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1967 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
1971 /* Convert the parameters to the types declared in the
1972 function prototype, or apply default promotions. */
1974 coerced_params
1975 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
1977 if (coerced_params == error_mark_node)
1978 return error_mark_node;
1980 /* Check that the arguments to the function are valid. */
1982 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1984 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
1985 function, coerced_params, NULL_TREE);
1986 TREE_SIDE_EFFECTS (result) = 1;
1988 if (require_constant_value)
1990 result = fold_initializer (result);
1992 if (TREE_CONSTANT (result)
1993 && (name == NULL_TREE
1994 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1995 pedwarn_init ("initializer element is not constant");
1997 else
1998 result = fold (result);
2000 if (VOID_TYPE_P (TREE_TYPE (result)))
2001 return result;
2002 return require_complete_type (result);
2005 /* Convert the argument expressions in the list VALUES
2006 to the types in the list TYPELIST. The result is a list of converted
2007 argument expressions, unless there are too few arguments in which
2008 case it is error_mark_node.
2010 If TYPELIST is exhausted, or when an element has NULL as its type,
2011 perform the default conversions.
2013 PARMLIST is the chain of parm decls for the function being called.
2014 It may be 0, if that info is not available.
2015 It is used only for generating error messages.
2017 FUNCTION is a tree for the called function. It is used only for
2018 error messages, where it is formatted with %qE.
2020 This is also where warnings about wrong number of args are generated.
2022 Both VALUES and the returned value are chains of TREE_LIST nodes
2023 with the elements of the list in the TREE_VALUE slots of those nodes. */
2025 static tree
2026 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2028 tree typetail, valtail;
2029 tree result = NULL;
2030 int parmnum;
2031 tree selector;
2033 /* Change pointer to function to the function itself for
2034 diagnostics. */
2035 if (TREE_CODE (function) == ADDR_EXPR
2036 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2037 function = TREE_OPERAND (function, 0);
2039 /* Handle an ObjC selector specially for diagnostics. */
2040 selector = objc_message_selector ();
2042 /* Scan the given expressions and types, producing individual
2043 converted arguments and pushing them on RESULT in reverse order. */
2045 for (valtail = values, typetail = typelist, parmnum = 0;
2046 valtail;
2047 valtail = TREE_CHAIN (valtail), parmnum++)
2049 tree type = typetail ? TREE_VALUE (typetail) : 0;
2050 tree val = TREE_VALUE (valtail);
2051 tree rname = function;
2052 int argnum = parmnum + 1;
2054 if (type == void_type_node)
2056 error ("too many arguments to function %qE", function);
2057 break;
2060 if (selector && argnum > 2)
2062 rname = selector;
2063 argnum -= 2;
2066 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2067 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2068 to convert automatically to a pointer. */
2069 if (TREE_CODE (val) == NON_LVALUE_EXPR)
2070 val = TREE_OPERAND (val, 0);
2072 val = default_function_array_conversion (val);
2074 val = require_complete_type (val);
2076 if (type != 0)
2078 /* Formal parm type is specified by a function prototype. */
2079 tree parmval;
2081 if (!COMPLETE_TYPE_P (type))
2083 error ("type of formal parameter %d is incomplete", parmnum + 1);
2084 parmval = val;
2086 else
2088 /* Optionally warn about conversions that
2089 differ from the default conversions. */
2090 if (warn_conversion || warn_traditional)
2092 unsigned int formal_prec = TYPE_PRECISION (type);
2094 if (INTEGRAL_TYPE_P (type)
2095 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2096 warning ("passing argument %d of %qE as integer "
2097 "rather than floating due to prototype",
2098 argnum, rname);
2099 if (INTEGRAL_TYPE_P (type)
2100 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2101 warning ("passing argument %d of %qE as integer "
2102 "rather than complex due to prototype",
2103 argnum, rname);
2104 else if (TREE_CODE (type) == COMPLEX_TYPE
2105 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2106 warning ("passing argument %d of %qE as complex "
2107 "rather than floating due to prototype",
2108 argnum, rname);
2109 else if (TREE_CODE (type) == REAL_TYPE
2110 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2111 warning ("passing argument %d of %qE as floating "
2112 "rather than integer due to prototype",
2113 argnum, rname);
2114 else if (TREE_CODE (type) == COMPLEX_TYPE
2115 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2116 warning ("passing argument %d of %qE as complex "
2117 "rather than integer due to prototype",
2118 argnum, rname);
2119 else if (TREE_CODE (type) == REAL_TYPE
2120 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2121 warning ("passing argument %d of %qE as floating "
2122 "rather than complex due to prototype",
2123 argnum, rname);
2124 /* ??? At some point, messages should be written about
2125 conversions between complex types, but that's too messy
2126 to do now. */
2127 else if (TREE_CODE (type) == REAL_TYPE
2128 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2130 /* Warn if any argument is passed as `float',
2131 since without a prototype it would be `double'. */
2132 if (formal_prec == TYPE_PRECISION (float_type_node))
2133 warning ("passing argument %d of %qE as %<float%> "
2134 "rather than %<double%> due to prototype",
2135 argnum, rname);
2137 /* Detect integer changing in width or signedness.
2138 These warnings are only activated with
2139 -Wconversion, not with -Wtraditional. */
2140 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2141 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2143 tree would_have_been = default_conversion (val);
2144 tree type1 = TREE_TYPE (would_have_been);
2146 if (TREE_CODE (type) == ENUMERAL_TYPE
2147 && (TYPE_MAIN_VARIANT (type)
2148 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2149 /* No warning if function asks for enum
2150 and the actual arg is that enum type. */
2152 else if (formal_prec != TYPE_PRECISION (type1))
2153 warning ("passing argument %d of %qE with different "
2154 "width due to prototype", argnum, rname);
2155 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2157 /* Don't complain if the formal parameter type
2158 is an enum, because we can't tell now whether
2159 the value was an enum--even the same enum. */
2160 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2162 else if (TREE_CODE (val) == INTEGER_CST
2163 && int_fits_type_p (val, type))
2164 /* Change in signedness doesn't matter
2165 if a constant value is unaffected. */
2167 /* Likewise for a constant in a NOP_EXPR. */
2168 else if (TREE_CODE (val) == NOP_EXPR
2169 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2170 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2172 /* If the value is extended from a narrower
2173 unsigned type, it doesn't matter whether we
2174 pass it as signed or unsigned; the value
2175 certainly is the same either way. */
2176 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2177 && TYPE_UNSIGNED (TREE_TYPE (val)))
2179 else if (TYPE_UNSIGNED (type))
2180 warning ("passing argument %d of %qE as unsigned "
2181 "due to prototype", argnum, rname);
2182 else
2183 warning ("passing argument %d of %qE as signed "
2184 "due to prototype", argnum, rname);
2188 parmval = convert_for_assignment (type, val, ic_argpass,
2189 fundecl, function,
2190 parmnum + 1);
2192 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2193 && INTEGRAL_TYPE_P (type)
2194 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2195 parmval = default_conversion (parmval);
2197 result = tree_cons (NULL_TREE, parmval, result);
2199 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2200 && (TYPE_PRECISION (TREE_TYPE (val))
2201 < TYPE_PRECISION (double_type_node)))
2202 /* Convert `float' to `double'. */
2203 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2204 else
2205 /* Convert `short' and `char' to full-size `int'. */
2206 result = tree_cons (NULL_TREE, default_conversion (val), result);
2208 if (typetail)
2209 typetail = TREE_CHAIN (typetail);
2212 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2214 error ("too few arguments to function %qE", function);
2215 return error_mark_node;
2218 return nreverse (result);
2221 /* This is the entry point used by the parser
2222 for binary operators in the input.
2223 In addition to constructing the expression,
2224 we check for operands that were written with other binary operators
2225 in a way that is likely to confuse the user. */
2227 struct c_expr
2228 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2229 struct c_expr arg2)
2231 struct c_expr result;
2233 enum tree_code code1 = arg1.original_code;
2234 enum tree_code code2 = arg2.original_code;
2236 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2237 result.original_code = code;
2239 if (TREE_CODE (result.value) == ERROR_MARK)
2240 return result;
2242 /* Check for cases such as x+y<<z which users are likely
2243 to misinterpret. */
2244 if (warn_parentheses)
2246 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2248 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2249 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2250 warning ("suggest parentheses around + or - inside shift");
2253 if (code == TRUTH_ORIF_EXPR)
2255 if (code1 == TRUTH_ANDIF_EXPR
2256 || code2 == TRUTH_ANDIF_EXPR)
2257 warning ("suggest parentheses around && within ||");
2260 if (code == BIT_IOR_EXPR)
2262 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2263 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2264 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2265 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2266 warning ("suggest parentheses around arithmetic in operand of |");
2267 /* Check cases like x|y==z */
2268 if (TREE_CODE_CLASS (code1) == tcc_comparison
2269 || TREE_CODE_CLASS (code2) == tcc_comparison)
2270 warning ("suggest parentheses around comparison in operand of |");
2273 if (code == BIT_XOR_EXPR)
2275 if (code1 == BIT_AND_EXPR
2276 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2277 || code2 == BIT_AND_EXPR
2278 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2279 warning ("suggest parentheses around arithmetic in operand of ^");
2280 /* Check cases like x^y==z */
2281 if (TREE_CODE_CLASS (code1) == tcc_comparison
2282 || TREE_CODE_CLASS (code2) == tcc_comparison)
2283 warning ("suggest parentheses around comparison in operand of ^");
2286 if (code == BIT_AND_EXPR)
2288 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2289 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2290 warning ("suggest parentheses around + or - in operand of &");
2291 /* Check cases like x&y==z */
2292 if (TREE_CODE_CLASS (code1) == tcc_comparison
2293 || TREE_CODE_CLASS (code2) == tcc_comparison)
2294 warning ("suggest parentheses around comparison in operand of &");
2296 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2297 if (TREE_CODE_CLASS (code) == tcc_comparison
2298 && (TREE_CODE_CLASS (code1) == tcc_comparison
2299 || TREE_CODE_CLASS (code2) == tcc_comparison))
2300 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2304 unsigned_conversion_warning (result.value, arg1.value);
2305 unsigned_conversion_warning (result.value, arg2.value);
2306 overflow_warning (result.value);
2308 return result;
2311 /* Return a tree for the difference of pointers OP0 and OP1.
2312 The resulting tree has type int. */
2314 static tree
2315 pointer_diff (tree op0, tree op1)
2317 tree restype = ptrdiff_type_node;
2319 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2320 tree con0, con1, lit0, lit1;
2321 tree orig_op1 = op1;
2323 if (pedantic || warn_pointer_arith)
2325 if (TREE_CODE (target_type) == VOID_TYPE)
2326 pedwarn ("pointer of type %<void *%> used in subtraction");
2327 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2328 pedwarn ("pointer to a function used in subtraction");
2331 /* If the conversion to ptrdiff_type does anything like widening or
2332 converting a partial to an integral mode, we get a convert_expression
2333 that is in the way to do any simplifications.
2334 (fold-const.c doesn't know that the extra bits won't be needed.
2335 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2336 different mode in place.)
2337 So first try to find a common term here 'by hand'; we want to cover
2338 at least the cases that occur in legal static initializers. */
2339 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2340 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2342 if (TREE_CODE (con0) == PLUS_EXPR)
2344 lit0 = TREE_OPERAND (con0, 1);
2345 con0 = TREE_OPERAND (con0, 0);
2347 else
2348 lit0 = integer_zero_node;
2350 if (TREE_CODE (con1) == PLUS_EXPR)
2352 lit1 = TREE_OPERAND (con1, 1);
2353 con1 = TREE_OPERAND (con1, 0);
2355 else
2356 lit1 = integer_zero_node;
2358 if (operand_equal_p (con0, con1, 0))
2360 op0 = lit0;
2361 op1 = lit1;
2365 /* First do the subtraction as integers;
2366 then drop through to build the divide operator.
2367 Do not do default conversions on the minus operator
2368 in case restype is a short type. */
2370 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2371 convert (restype, op1), 0);
2372 /* This generates an error if op1 is pointer to incomplete type. */
2373 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2374 error ("arithmetic on pointer to an incomplete type");
2376 /* This generates an error if op0 is pointer to incomplete type. */
2377 op1 = c_size_in_bytes (target_type);
2379 /* Divide by the size, in easiest possible way. */
2380 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2383 /* Construct and perhaps optimize a tree representation
2384 for a unary operation. CODE, a tree_code, specifies the operation
2385 and XARG is the operand.
2386 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2387 the default promotions (such as from short to int).
2388 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2389 allows non-lvalues; this is only used to handle conversion of non-lvalue
2390 arrays to pointers in C99. */
2392 tree
2393 build_unary_op (enum tree_code code, tree xarg, int flag)
2395 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2396 tree arg = xarg;
2397 tree argtype = 0;
2398 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2399 tree val;
2400 int noconvert = flag;
2402 if (typecode == ERROR_MARK)
2403 return error_mark_node;
2404 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2405 typecode = INTEGER_TYPE;
2407 switch (code)
2409 case CONVERT_EXPR:
2410 /* This is used for unary plus, because a CONVERT_EXPR
2411 is enough to prevent anybody from looking inside for
2412 associativity, but won't generate any code. */
2413 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2414 || typecode == COMPLEX_TYPE
2415 || typecode == VECTOR_TYPE))
2417 error ("wrong type argument to unary plus");
2418 return error_mark_node;
2420 else if (!noconvert)
2421 arg = default_conversion (arg);
2422 arg = non_lvalue (arg);
2423 break;
2425 case NEGATE_EXPR:
2426 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2427 || typecode == COMPLEX_TYPE
2428 || typecode == VECTOR_TYPE))
2430 error ("wrong type argument to unary minus");
2431 return error_mark_node;
2433 else if (!noconvert)
2434 arg = default_conversion (arg);
2435 break;
2437 case BIT_NOT_EXPR:
2438 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2440 if (!noconvert)
2441 arg = default_conversion (arg);
2443 else if (typecode == COMPLEX_TYPE)
2445 code = CONJ_EXPR;
2446 if (pedantic)
2447 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2448 if (!noconvert)
2449 arg = default_conversion (arg);
2451 else
2453 error ("wrong type argument to bit-complement");
2454 return error_mark_node;
2456 break;
2458 case ABS_EXPR:
2459 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2461 error ("wrong type argument to abs");
2462 return error_mark_node;
2464 else if (!noconvert)
2465 arg = default_conversion (arg);
2466 break;
2468 case CONJ_EXPR:
2469 /* Conjugating a real value is a no-op, but allow it anyway. */
2470 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2471 || typecode == COMPLEX_TYPE))
2473 error ("wrong type argument to conjugation");
2474 return error_mark_node;
2476 else if (!noconvert)
2477 arg = default_conversion (arg);
2478 break;
2480 case TRUTH_NOT_EXPR:
2481 if (typecode != INTEGER_TYPE
2482 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2483 && typecode != COMPLEX_TYPE
2484 /* These will convert to a pointer. */
2485 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2487 error ("wrong type argument to unary exclamation mark");
2488 return error_mark_node;
2490 arg = lang_hooks.truthvalue_conversion (arg);
2491 return invert_truthvalue (arg);
2493 case NOP_EXPR:
2494 break;
2496 case REALPART_EXPR:
2497 if (TREE_CODE (arg) == COMPLEX_CST)
2498 return TREE_REALPART (arg);
2499 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2500 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2501 else
2502 return arg;
2504 case IMAGPART_EXPR:
2505 if (TREE_CODE (arg) == COMPLEX_CST)
2506 return TREE_IMAGPART (arg);
2507 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2508 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2509 else
2510 return convert (TREE_TYPE (arg), integer_zero_node);
2512 case PREINCREMENT_EXPR:
2513 case POSTINCREMENT_EXPR:
2514 case PREDECREMENT_EXPR:
2515 case POSTDECREMENT_EXPR:
2517 /* Increment or decrement the real part of the value,
2518 and don't change the imaginary part. */
2519 if (typecode == COMPLEX_TYPE)
2521 tree real, imag;
2523 if (pedantic)
2524 pedwarn ("ISO C does not support %<++%> and %<--%>"
2525 " on complex types");
2527 arg = stabilize_reference (arg);
2528 real = build_unary_op (REALPART_EXPR, arg, 1);
2529 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2530 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2531 build_unary_op (code, real, 1), imag);
2534 /* Report invalid types. */
2536 if (typecode != POINTER_TYPE
2537 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2539 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2540 error ("wrong type argument to increment");
2541 else
2542 error ("wrong type argument to decrement");
2544 return error_mark_node;
2548 tree inc;
2549 tree result_type = TREE_TYPE (arg);
2551 arg = get_unwidened (arg, 0);
2552 argtype = TREE_TYPE (arg);
2554 /* Compute the increment. */
2556 if (typecode == POINTER_TYPE)
2558 /* If pointer target is an undefined struct,
2559 we just cannot know how to do the arithmetic. */
2560 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2562 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2563 error ("increment of pointer to unknown structure");
2564 else
2565 error ("decrement of pointer to unknown structure");
2567 else if ((pedantic || warn_pointer_arith)
2568 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2569 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2571 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2572 pedwarn ("wrong type argument to increment");
2573 else
2574 pedwarn ("wrong type argument to decrement");
2577 inc = c_size_in_bytes (TREE_TYPE (result_type));
2579 else
2580 inc = integer_one_node;
2582 inc = convert (argtype, inc);
2584 /* Complain about anything else that is not a true lvalue. */
2585 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2586 || code == POSTINCREMENT_EXPR)
2587 ? lv_increment
2588 : lv_decrement)))
2589 return error_mark_node;
2591 /* Report a read-only lvalue. */
2592 if (TREE_READONLY (arg))
2593 readonly_error (arg,
2594 ((code == PREINCREMENT_EXPR
2595 || code == POSTINCREMENT_EXPR)
2596 ? lv_increment : lv_decrement));
2598 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2599 val = boolean_increment (code, arg);
2600 else
2601 val = build2 (code, TREE_TYPE (arg), arg, inc);
2602 TREE_SIDE_EFFECTS (val) = 1;
2603 val = convert (result_type, val);
2604 if (TREE_CODE (val) != code)
2605 TREE_NO_WARNING (val) = 1;
2606 return val;
2609 case ADDR_EXPR:
2610 /* Note that this operation never does default_conversion. */
2612 /* Let &* cancel out to simplify resulting code. */
2613 if (TREE_CODE (arg) == INDIRECT_REF)
2615 /* Don't let this be an lvalue. */
2616 if (lvalue_p (TREE_OPERAND (arg, 0)))
2617 return non_lvalue (TREE_OPERAND (arg, 0));
2618 return TREE_OPERAND (arg, 0);
2621 /* For &x[y], return x+y */
2622 if (TREE_CODE (arg) == ARRAY_REF)
2624 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2625 return error_mark_node;
2626 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2627 TREE_OPERAND (arg, 1), 1);
2630 /* Anything not already handled and not a true memory reference
2631 or a non-lvalue array is an error. */
2632 else if (typecode != FUNCTION_TYPE && !flag
2633 && !lvalue_or_else (arg, lv_addressof))
2634 return error_mark_node;
2636 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2637 argtype = TREE_TYPE (arg);
2639 /* If the lvalue is const or volatile, merge that into the type
2640 to which the address will point. Note that you can't get a
2641 restricted pointer by taking the address of something, so we
2642 only have to deal with `const' and `volatile' here. */
2643 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2644 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2645 argtype = c_build_type_variant (argtype,
2646 TREE_READONLY (arg),
2647 TREE_THIS_VOLATILE (arg));
2649 if (!c_mark_addressable (arg))
2650 return error_mark_node;
2652 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2653 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2655 argtype = build_pointer_type (argtype);
2657 /* ??? Cope with user tricks that amount to offsetof. Delete this
2658 when we have proper support for integer constant expressions. */
2659 val = get_base_address (arg);
2660 if (val && TREE_CODE (val) == INDIRECT_REF
2661 && integer_zerop (TREE_OPERAND (val, 0)))
2662 return fold_convert (argtype, fold_offsetof (arg));
2664 val = build1 (ADDR_EXPR, argtype, arg);
2666 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2667 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2669 return val;
2671 default:
2672 break;
2675 if (argtype == 0)
2676 argtype = TREE_TYPE (arg);
2677 val = build1 (code, argtype, arg);
2678 return require_constant_value ? fold_initializer (val) : fold (val);
2681 /* Return nonzero if REF is an lvalue valid for this language.
2682 Lvalues can be assigned, unless their type has TYPE_READONLY.
2683 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2686 lvalue_p (tree ref)
2688 enum tree_code code = TREE_CODE (ref);
2690 switch (code)
2692 case REALPART_EXPR:
2693 case IMAGPART_EXPR:
2694 case COMPONENT_REF:
2695 return lvalue_p (TREE_OPERAND (ref, 0));
2697 case COMPOUND_LITERAL_EXPR:
2698 case STRING_CST:
2699 return 1;
2701 case INDIRECT_REF:
2702 case ARRAY_REF:
2703 case VAR_DECL:
2704 case PARM_DECL:
2705 case RESULT_DECL:
2706 case ERROR_MARK:
2707 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2708 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2710 case BIND_EXPR:
2711 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2713 default:
2714 return 0;
2718 /* Return nonzero if REF is an lvalue valid for this language;
2719 otherwise, print an error message and return zero. USE says
2720 how the lvalue is being used and so selects the error message. */
2722 static int
2723 lvalue_or_else (tree ref, enum lvalue_use use)
2725 int win = lvalue_p (ref);
2727 if (!win)
2729 switch (use)
2731 case lv_assign:
2732 error ("invalid lvalue in assignment");
2733 break;
2734 case lv_increment:
2735 error ("invalid lvalue in increment");
2736 break;
2737 case lv_decrement:
2738 error ("invalid lvalue in decrement");
2739 break;
2740 case lv_addressof:
2741 error ("invalid lvalue in unary %<&%>");
2742 break;
2743 case lv_asm:
2744 error ("invalid lvalue in asm statement");
2745 break;
2746 default:
2747 gcc_unreachable ();
2751 return win;
2755 /* Give an error for storing in something that is 'const'. */
2757 static void
2758 readonly_error (tree arg, enum lvalue_use use)
2760 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2761 /* Using this macro rather than (for example) arrays of messages
2762 ensures that all the format strings are checked at compile
2763 time. */
2764 #define READONLY_MSG(A, I, D) (use == lv_assign \
2765 ? (A) \
2766 : (use == lv_increment ? (I) : (D)))
2767 if (TREE_CODE (arg) == COMPONENT_REF)
2769 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2770 readonly_error (TREE_OPERAND (arg, 0), use);
2771 else
2772 error (READONLY_MSG (N_("assignment of read-only member %qs"),
2773 N_("increment of read-only member %qs"),
2774 N_("decrement of read-only member %qs")),
2775 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2777 else if (TREE_CODE (arg) == VAR_DECL)
2778 error (READONLY_MSG (N_("assignment of read-only variable %qs"),
2779 N_("increment of read-only variable %qs"),
2780 N_("decrement of read-only variable %qs")),
2781 IDENTIFIER_POINTER (DECL_NAME (arg)));
2782 else
2783 error (READONLY_MSG (N_("assignment of read-only location"),
2784 N_("increment of read-only location"),
2785 N_("decrement of read-only location")));
2788 /* Mark EXP saying that we need to be able to take the
2789 address of it; it should not be allocated in a register.
2790 Returns true if successful. */
2792 bool
2793 c_mark_addressable (tree exp)
2795 tree x = exp;
2797 while (1)
2798 switch (TREE_CODE (x))
2800 case COMPONENT_REF:
2801 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2803 error
2804 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2805 return false;
2808 /* ... fall through ... */
2810 case ADDR_EXPR:
2811 case ARRAY_REF:
2812 case REALPART_EXPR:
2813 case IMAGPART_EXPR:
2814 x = TREE_OPERAND (x, 0);
2815 break;
2817 case COMPOUND_LITERAL_EXPR:
2818 case CONSTRUCTOR:
2819 TREE_ADDRESSABLE (x) = 1;
2820 return true;
2822 case VAR_DECL:
2823 case CONST_DECL:
2824 case PARM_DECL:
2825 case RESULT_DECL:
2826 if (C_DECL_REGISTER (x)
2827 && DECL_NONLOCAL (x))
2829 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2831 error
2832 ("global register variable %qD used in nested function", x);
2833 return false;
2835 pedwarn ("register variable %qD used in nested function", x);
2837 else if (C_DECL_REGISTER (x))
2839 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2840 error ("address of global register variable %qD requested", x);
2841 else
2842 error ("address of register variable %qD requested", x);
2843 return false;
2846 /* drops in */
2847 case FUNCTION_DECL:
2848 TREE_ADDRESSABLE (x) = 1;
2849 /* drops out */
2850 default:
2851 return true;
2855 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2857 tree
2858 build_conditional_expr (tree ifexp, tree op1, tree op2)
2860 tree type1;
2861 tree type2;
2862 enum tree_code code1;
2863 enum tree_code code2;
2864 tree result_type = NULL;
2865 tree orig_op1 = op1, orig_op2 = op2;
2867 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2869 /* Promote both alternatives. */
2871 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2872 op1 = default_conversion (op1);
2873 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2874 op2 = default_conversion (op2);
2876 if (TREE_CODE (ifexp) == ERROR_MARK
2877 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2878 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2879 return error_mark_node;
2881 type1 = TREE_TYPE (op1);
2882 code1 = TREE_CODE (type1);
2883 type2 = TREE_TYPE (op2);
2884 code2 = TREE_CODE (type2);
2886 /* C90 does not permit non-lvalue arrays in conditional expressions.
2887 In C99 they will be pointers by now. */
2888 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2890 error ("non-lvalue array in conditional expression");
2891 return error_mark_node;
2894 /* Quickly detect the usual case where op1 and op2 have the same type
2895 after promotion. */
2896 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2898 if (type1 == type2)
2899 result_type = type1;
2900 else
2901 result_type = TYPE_MAIN_VARIANT (type1);
2903 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2904 || code1 == COMPLEX_TYPE)
2905 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2906 || code2 == COMPLEX_TYPE))
2908 result_type = common_type (type1, type2);
2910 /* If -Wsign-compare, warn here if type1 and type2 have
2911 different signedness. We'll promote the signed to unsigned
2912 and later code won't know it used to be different.
2913 Do this check on the original types, so that explicit casts
2914 will be considered, but default promotions won't. */
2915 if (warn_sign_compare && !skip_evaluation)
2917 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2918 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2920 if (unsigned_op1 ^ unsigned_op2)
2922 /* Do not warn if the result type is signed, since the
2923 signed type will only be chosen if it can represent
2924 all the values of the unsigned type. */
2925 if (!TYPE_UNSIGNED (result_type))
2926 /* OK */;
2927 /* Do not warn if the signed quantity is an unsuffixed
2928 integer literal (or some static constant expression
2929 involving such literals) and it is non-negative. */
2930 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2931 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2932 /* OK */;
2933 else
2934 warning ("signed and unsigned type in conditional expression");
2938 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2940 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2941 pedwarn ("ISO C forbids conditional expr with only one void side");
2942 result_type = void_type_node;
2944 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2946 if (comp_target_types (type1, type2, 1))
2947 result_type = common_pointer_type (type1, type2);
2948 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2949 && TREE_CODE (orig_op1) != NOP_EXPR)
2950 result_type = qualify_type (type2, type1);
2951 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2952 && TREE_CODE (orig_op2) != NOP_EXPR)
2953 result_type = qualify_type (type1, type2);
2954 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2956 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2957 pedwarn ("ISO C forbids conditional expr between "
2958 "%<void *%> and function pointer");
2959 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2960 TREE_TYPE (type2)));
2962 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2964 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2965 pedwarn ("ISO C forbids conditional expr between "
2966 "%<void *%> and function pointer");
2967 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2968 TREE_TYPE (type1)));
2970 else
2972 pedwarn ("pointer type mismatch in conditional expression");
2973 result_type = build_pointer_type (void_type_node);
2976 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2978 if (!integer_zerop (op2))
2979 pedwarn ("pointer/integer type mismatch in conditional expression");
2980 else
2982 op2 = null_pointer_node;
2984 result_type = type1;
2986 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2988 if (!integer_zerop (op1))
2989 pedwarn ("pointer/integer type mismatch in conditional expression");
2990 else
2992 op1 = null_pointer_node;
2994 result_type = type2;
2997 if (!result_type)
2999 if (flag_cond_mismatch)
3000 result_type = void_type_node;
3001 else
3003 error ("type mismatch in conditional expression");
3004 return error_mark_node;
3008 /* Merge const and volatile flags of the incoming types. */
3009 result_type
3010 = build_type_variant (result_type,
3011 TREE_READONLY (op1) || TREE_READONLY (op2),
3012 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3014 if (result_type != TREE_TYPE (op1))
3015 op1 = convert_and_check (result_type, op1);
3016 if (result_type != TREE_TYPE (op2))
3017 op2 = convert_and_check (result_type, op2);
3019 if (TREE_CODE (ifexp) == INTEGER_CST)
3020 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3022 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3025 /* Return a compound expression that performs two expressions and
3026 returns the value of the second of them. */
3028 tree
3029 build_compound_expr (tree expr1, tree expr2)
3031 /* Convert arrays and functions to pointers. */
3032 expr2 = default_function_array_conversion (expr2);
3034 if (!TREE_SIDE_EFFECTS (expr1))
3036 /* The left-hand operand of a comma expression is like an expression
3037 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3038 any side-effects, unless it was explicitly cast to (void). */
3039 if (warn_unused_value
3040 && !(TREE_CODE (expr1) == CONVERT_EXPR
3041 && VOID_TYPE_P (TREE_TYPE (expr1))))
3042 warning ("left-hand operand of comma expression has no effect");
3045 /* With -Wunused, we should also warn if the left-hand operand does have
3046 side-effects, but computes a value which is not used. For example, in
3047 `foo() + bar(), baz()' the result of the `+' operator is not used,
3048 so we should issue a warning. */
3049 else if (warn_unused_value)
3050 warn_if_unused_value (expr1, input_location);
3052 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3055 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3057 tree
3058 build_c_cast (tree type, tree expr)
3060 tree value = expr;
3062 if (type == error_mark_node || expr == error_mark_node)
3063 return error_mark_node;
3065 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3066 only in <protocol> qualifications. But when constructing cast expressions,
3067 the protocols do matter and must be kept around. */
3068 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3069 return build1 (NOP_EXPR, type, expr);
3071 type = TYPE_MAIN_VARIANT (type);
3073 if (TREE_CODE (type) == ARRAY_TYPE)
3075 error ("cast specifies array type");
3076 return error_mark_node;
3079 if (TREE_CODE (type) == FUNCTION_TYPE)
3081 error ("cast specifies function type");
3082 return error_mark_node;
3085 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3087 if (pedantic)
3089 if (TREE_CODE (type) == RECORD_TYPE
3090 || TREE_CODE (type) == UNION_TYPE)
3091 pedwarn ("ISO C forbids casting nonscalar to the same type");
3094 else if (TREE_CODE (type) == UNION_TYPE)
3096 tree field;
3097 value = default_function_array_conversion (value);
3099 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3100 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3101 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3102 break;
3104 if (field)
3106 tree t;
3108 if (pedantic)
3109 pedwarn ("ISO C forbids casts to union type");
3110 t = digest_init (type,
3111 build_constructor (type,
3112 build_tree_list (field, value)),
3113 true, 0);
3114 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3115 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3116 return t;
3118 error ("cast to union type from type not present in union");
3119 return error_mark_node;
3121 else
3123 tree otype, ovalue;
3125 /* If casting to void, avoid the error that would come
3126 from default_conversion in the case of a non-lvalue array. */
3127 if (type == void_type_node)
3128 return build1 (CONVERT_EXPR, type, value);
3130 /* Convert functions and arrays to pointers,
3131 but don't convert any other types. */
3132 value = default_function_array_conversion (value);
3133 otype = TREE_TYPE (value);
3135 /* Optionally warn about potentially worrisome casts. */
3137 if (warn_cast_qual
3138 && TREE_CODE (type) == POINTER_TYPE
3139 && TREE_CODE (otype) == POINTER_TYPE)
3141 tree in_type = type;
3142 tree in_otype = otype;
3143 int added = 0;
3144 int discarded = 0;
3146 /* Check that the qualifiers on IN_TYPE are a superset of
3147 the qualifiers of IN_OTYPE. The outermost level of
3148 POINTER_TYPE nodes is uninteresting and we stop as soon
3149 as we hit a non-POINTER_TYPE node on either type. */
3152 in_otype = TREE_TYPE (in_otype);
3153 in_type = TREE_TYPE (in_type);
3155 /* GNU C allows cv-qualified function types. 'const'
3156 means the function is very pure, 'volatile' means it
3157 can't return. We need to warn when such qualifiers
3158 are added, not when they're taken away. */
3159 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3160 && TREE_CODE (in_type) == FUNCTION_TYPE)
3161 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3162 else
3163 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3165 while (TREE_CODE (in_type) == POINTER_TYPE
3166 && TREE_CODE (in_otype) == POINTER_TYPE);
3168 if (added)
3169 warning ("cast adds new qualifiers to function type");
3171 if (discarded)
3172 /* There are qualifiers present in IN_OTYPE that are not
3173 present in IN_TYPE. */
3174 warning ("cast discards qualifiers from pointer target type");
3177 /* Warn about possible alignment problems. */
3178 if (STRICT_ALIGNMENT && warn_cast_align
3179 && TREE_CODE (type) == POINTER_TYPE
3180 && TREE_CODE (otype) == POINTER_TYPE
3181 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3182 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3183 /* Don't warn about opaque types, where the actual alignment
3184 restriction is unknown. */
3185 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3186 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3187 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3188 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3189 warning ("cast increases required alignment of target type");
3191 if (TREE_CODE (type) == INTEGER_TYPE
3192 && TREE_CODE (otype) == POINTER_TYPE
3193 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3194 && !TREE_CONSTANT (value))
3195 warning ("cast from pointer to integer of different size");
3197 if (warn_bad_function_cast
3198 && TREE_CODE (value) == CALL_EXPR
3199 && TREE_CODE (type) != TREE_CODE (otype))
3200 warning ("cast from function call of type %qT to non-matching "
3201 "type %qT", otype, type);
3203 if (TREE_CODE (type) == POINTER_TYPE
3204 && TREE_CODE (otype) == INTEGER_TYPE
3205 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3206 /* Don't warn about converting any constant. */
3207 && !TREE_CONSTANT (value))
3208 warning ("cast to pointer from integer of different size");
3210 if (TREE_CODE (type) == POINTER_TYPE
3211 && TREE_CODE (otype) == POINTER_TYPE
3212 && TREE_CODE (expr) == ADDR_EXPR
3213 && DECL_P (TREE_OPERAND (expr, 0))
3214 && flag_strict_aliasing && warn_strict_aliasing
3215 && !VOID_TYPE_P (TREE_TYPE (type)))
3217 /* Casting the address of a decl to non void pointer. Warn
3218 if the cast breaks type based aliasing. */
3219 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3220 warning ("type-punning to incomplete type might break strict-aliasing rules");
3221 else
3223 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3224 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3226 if (!alias_sets_conflict_p (set1, set2))
3227 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3228 else if (warn_strict_aliasing > 1
3229 && !alias_sets_might_conflict_p (set1, set2))
3230 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3234 /* If pedantic, warn for conversions between function and object
3235 pointer types, except for converting a null pointer constant
3236 to function pointer type. */
3237 if (pedantic
3238 && TREE_CODE (type) == POINTER_TYPE
3239 && TREE_CODE (otype) == POINTER_TYPE
3240 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3241 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3242 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3244 if (pedantic
3245 && TREE_CODE (type) == POINTER_TYPE
3246 && TREE_CODE (otype) == POINTER_TYPE
3247 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3248 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3249 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3250 && TREE_CODE (expr) != NOP_EXPR))
3251 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3253 ovalue = value;
3254 /* Replace a nonvolatile const static variable with its value. */
3255 if (optimize && TREE_CODE (value) == VAR_DECL)
3256 value = decl_constant_value (value);
3257 value = convert (type, value);
3259 /* Ignore any integer overflow caused by the cast. */
3260 if (TREE_CODE (value) == INTEGER_CST)
3262 if (EXPR_P (ovalue))
3263 /* If OVALUE had overflow set, then so will VALUE, so it
3264 is safe to overwrite. */
3265 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3266 else
3267 TREE_OVERFLOW (value) = 0;
3269 if (CONSTANT_CLASS_P (ovalue))
3270 /* Similarly, constant_overflow cannot have become
3271 cleared. */
3272 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3276 /* Don't let a cast be an lvalue. */
3277 if (value == expr)
3278 value = non_lvalue (value);
3280 return value;
3283 /* Interpret a cast of expression EXPR to type TYPE. */
3284 tree
3285 c_cast_expr (struct c_type_name *type_name, tree expr)
3287 tree type;
3288 int saved_wsp = warn_strict_prototypes;
3290 /* This avoids warnings about unprototyped casts on
3291 integers. E.g. "#define SIG_DFL (void(*)())0". */
3292 if (TREE_CODE (expr) == INTEGER_CST)
3293 warn_strict_prototypes = 0;
3294 type = groktypename (type_name);
3295 warn_strict_prototypes = saved_wsp;
3297 return build_c_cast (type, expr);
3301 /* Build an assignment expression of lvalue LHS from value RHS.
3302 MODIFYCODE is the code for a binary operator that we use
3303 to combine the old value of LHS with RHS to get the new value.
3304 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3306 tree
3307 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3309 tree result;
3310 tree newrhs;
3311 tree lhstype = TREE_TYPE (lhs);
3312 tree olhstype = lhstype;
3314 /* Types that aren't fully specified cannot be used in assignments. */
3315 lhs = require_complete_type (lhs);
3317 /* Avoid duplicate error messages from operands that had errors. */
3318 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3319 return error_mark_node;
3321 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3322 /* Do not use STRIP_NOPS here. We do not want an enumerator
3323 whose value is 0 to count as a null pointer constant. */
3324 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3325 rhs = TREE_OPERAND (rhs, 0);
3327 newrhs = rhs;
3329 /* If a binary op has been requested, combine the old LHS value with the RHS
3330 producing the value we should actually store into the LHS. */
3332 if (modifycode != NOP_EXPR)
3334 lhs = stabilize_reference (lhs);
3335 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3338 if (!lvalue_or_else (lhs, lv_assign))
3339 return error_mark_node;
3341 /* Give an error for storing in something that is 'const'. */
3343 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3344 || ((TREE_CODE (lhstype) == RECORD_TYPE
3345 || TREE_CODE (lhstype) == UNION_TYPE)
3346 && C_TYPE_FIELDS_READONLY (lhstype)))
3347 readonly_error (lhs, lv_assign);
3349 /* If storing into a structure or union member,
3350 it has probably been given type `int'.
3351 Compute the type that would go with
3352 the actual amount of storage the member occupies. */
3354 if (TREE_CODE (lhs) == COMPONENT_REF
3355 && (TREE_CODE (lhstype) == INTEGER_TYPE
3356 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3357 || TREE_CODE (lhstype) == REAL_TYPE
3358 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3359 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3361 /* If storing in a field that is in actuality a short or narrower than one,
3362 we must store in the field in its actual type. */
3364 if (lhstype != TREE_TYPE (lhs))
3366 lhs = copy_node (lhs);
3367 TREE_TYPE (lhs) = lhstype;
3370 /* Convert new value to destination type. */
3372 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3373 NULL_TREE, NULL_TREE, 0);
3374 if (TREE_CODE (newrhs) == ERROR_MARK)
3375 return error_mark_node;
3377 /* Scan operands. */
3379 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3380 TREE_SIDE_EFFECTS (result) = 1;
3382 /* If we got the LHS in a different type for storing in,
3383 convert the result back to the nominal type of LHS
3384 so that the value we return always has the same type
3385 as the LHS argument. */
3387 if (olhstype == TREE_TYPE (result))
3388 return result;
3389 return convert_for_assignment (olhstype, result, ic_assign,
3390 NULL_TREE, NULL_TREE, 0);
3393 /* Convert value RHS to type TYPE as preparation for an assignment
3394 to an lvalue of type TYPE.
3395 The real work of conversion is done by `convert'.
3396 The purpose of this function is to generate error messages
3397 for assignments that are not allowed in C.
3398 ERRTYPE says whether it is argument passing, assignment,
3399 initialization or return.
3401 FUNCTION is a tree for the function being called.
3402 PARMNUM is the number of the argument, for printing in error messages. */
3404 static tree
3405 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3406 tree fundecl, tree function, int parmnum)
3408 enum tree_code codel = TREE_CODE (type);
3409 tree rhstype;
3410 enum tree_code coder;
3411 tree rname = NULL_TREE;
3413 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3415 tree selector;
3416 /* Change pointer to function to the function itself for
3417 diagnostics. */
3418 if (TREE_CODE (function) == ADDR_EXPR
3419 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3420 function = TREE_OPERAND (function, 0);
3422 /* Handle an ObjC selector specially for diagnostics. */
3423 selector = objc_message_selector ();
3424 rname = function;
3425 if (selector && parmnum > 2)
3427 rname = selector;
3428 parmnum -= 2;
3432 /* This macro is used to emit diagnostics to ensure that all format
3433 strings are complete sentences, visible to gettext and checked at
3434 compile time. */
3435 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3436 do { \
3437 switch (errtype) \
3439 case ic_argpass: \
3440 pedwarn (AR, parmnum, rname); \
3441 break; \
3442 case ic_argpass_nonproto: \
3443 warning (AR, parmnum, rname); \
3444 break; \
3445 case ic_assign: \
3446 pedwarn (AS); \
3447 break; \
3448 case ic_init: \
3449 pedwarn (IN); \
3450 break; \
3451 case ic_return: \
3452 pedwarn (RE); \
3453 break; \
3454 default: \
3455 gcc_unreachable (); \
3457 } while (0)
3459 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3460 /* Do not use STRIP_NOPS here. We do not want an enumerator
3461 whose value is 0 to count as a null pointer constant. */
3462 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3463 rhs = TREE_OPERAND (rhs, 0);
3465 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3466 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3467 rhs = default_conversion (rhs);
3468 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3469 rhs = decl_constant_value_for_broken_optimization (rhs);
3471 rhstype = TREE_TYPE (rhs);
3472 coder = TREE_CODE (rhstype);
3474 if (coder == ERROR_MARK)
3475 return error_mark_node;
3477 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3479 overflow_warning (rhs);
3480 /* Check for Objective-C protocols. This will automatically
3481 issue a warning if there are protocol violations. No need to
3482 use the return value. */
3483 if (c_dialect_objc ())
3484 objc_comptypes (type, rhstype, 0);
3485 return rhs;
3488 if (coder == VOID_TYPE)
3490 /* Except for passing an argument to an unprototyped function,
3491 this is a constraint violation. When passing an argument to
3492 an unprototyped function, it is compile-time undefined;
3493 making it a constraint in that case was rejected in
3494 DR#252. */
3495 error ("void value not ignored as it ought to be");
3496 return error_mark_node;
3498 /* A type converts to a reference to it.
3499 This code doesn't fully support references, it's just for the
3500 special case of va_start and va_copy. */
3501 if (codel == REFERENCE_TYPE
3502 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3504 if (!lvalue_p (rhs))
3506 error ("cannot pass rvalue to reference parameter");
3507 return error_mark_node;
3509 if (!c_mark_addressable (rhs))
3510 return error_mark_node;
3511 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3513 /* We already know that these two types are compatible, but they
3514 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3515 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3516 likely to be va_list, a typedef to __builtin_va_list, which
3517 is different enough that it will cause problems later. */
3518 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3519 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3521 rhs = build1 (NOP_EXPR, type, rhs);
3522 return rhs;
3524 /* Some types can interconvert without explicit casts. */
3525 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3526 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3527 return convert (type, rhs);
3528 /* Arithmetic types all interconvert, and enum is treated like int. */
3529 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3530 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3531 || codel == BOOLEAN_TYPE)
3532 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3533 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3534 || coder == BOOLEAN_TYPE))
3535 return convert_and_check (type, rhs);
3537 /* Conversion to a transparent union from its member types.
3538 This applies only to function arguments. */
3539 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3540 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3542 tree memb_types;
3543 tree marginal_memb_type = 0;
3545 for (memb_types = TYPE_FIELDS (type); memb_types;
3546 memb_types = TREE_CHAIN (memb_types))
3548 tree memb_type = TREE_TYPE (memb_types);
3550 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3551 TYPE_MAIN_VARIANT (rhstype)))
3552 break;
3554 if (TREE_CODE (memb_type) != POINTER_TYPE)
3555 continue;
3557 if (coder == POINTER_TYPE)
3559 tree ttl = TREE_TYPE (memb_type);
3560 tree ttr = TREE_TYPE (rhstype);
3562 /* Any non-function converts to a [const][volatile] void *
3563 and vice versa; otherwise, targets must be the same.
3564 Meanwhile, the lhs target must have all the qualifiers of
3565 the rhs. */
3566 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3567 || comp_target_types (memb_type, rhstype, 0))
3569 /* If this type won't generate any warnings, use it. */
3570 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3571 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3572 && TREE_CODE (ttl) == FUNCTION_TYPE)
3573 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3574 == TYPE_QUALS (ttr))
3575 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3576 == TYPE_QUALS (ttl))))
3577 break;
3579 /* Keep looking for a better type, but remember this one. */
3580 if (!marginal_memb_type)
3581 marginal_memb_type = memb_type;
3585 /* Can convert integer zero to any pointer type. */
3586 if (integer_zerop (rhs)
3587 || (TREE_CODE (rhs) == NOP_EXPR
3588 && integer_zerop (TREE_OPERAND (rhs, 0))))
3590 rhs = null_pointer_node;
3591 break;
3595 if (memb_types || marginal_memb_type)
3597 if (!memb_types)
3599 /* We have only a marginally acceptable member type;
3600 it needs a warning. */
3601 tree ttl = TREE_TYPE (marginal_memb_type);
3602 tree ttr = TREE_TYPE (rhstype);
3604 /* Const and volatile mean something different for function
3605 types, so the usual warnings are not appropriate. */
3606 if (TREE_CODE (ttr) == FUNCTION_TYPE
3607 && TREE_CODE (ttl) == FUNCTION_TYPE)
3609 /* Because const and volatile on functions are
3610 restrictions that say the function will not do
3611 certain things, it is okay to use a const or volatile
3612 function where an ordinary one is wanted, but not
3613 vice-versa. */
3614 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3615 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3616 "makes qualified function "
3617 "pointer from unqualified"),
3618 N_("assignment makes qualified "
3619 "function pointer from "
3620 "unqualified"),
3621 N_("initialization makes qualified "
3622 "function pointer from "
3623 "unqualified"),
3624 N_("return makes qualified function "
3625 "pointer from unqualified"));
3627 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3628 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3629 "qualifiers from pointer target type"),
3630 N_("assignment discards qualifiers "
3631 "from pointer target type"),
3632 N_("initialization discards qualifiers "
3633 "from pointer target type"),
3634 N_("return discards qualifiers from "
3635 "pointer target type"));
3638 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3639 pedwarn ("ISO C prohibits argument conversion to union type");
3641 return build1 (NOP_EXPR, type, rhs);
3645 /* Conversions among pointers */
3646 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3647 && (coder == codel))
3649 tree ttl = TREE_TYPE (type);
3650 tree ttr = TREE_TYPE (rhstype);
3651 bool is_opaque_pointer;
3652 int target_cmp = 0; /* Cache comp_target_types () result. */
3654 /* Opaque pointers are treated like void pointers. */
3655 is_opaque_pointer = (targetm.vector_opaque_p (type)
3656 || targetm.vector_opaque_p (rhstype))
3657 && TREE_CODE (ttl) == VECTOR_TYPE
3658 && TREE_CODE (ttr) == VECTOR_TYPE;
3660 /* Any non-function converts to a [const][volatile] void *
3661 and vice versa; otherwise, targets must be the same.
3662 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3663 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3664 || (target_cmp = comp_target_types (type, rhstype, 0))
3665 || is_opaque_pointer
3666 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3667 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3669 if (pedantic
3670 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3672 (VOID_TYPE_P (ttr)
3673 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3674 which are not ANSI null ptr constants. */
3675 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3676 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3677 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3678 "%qE between function pointer "
3679 "and %<void *%>"),
3680 N_("ISO C forbids assignment between "
3681 "function pointer and %<void *%>"),
3682 N_("ISO C forbids initialization between "
3683 "function pointer and %<void *%>"),
3684 N_("ISO C forbids return between function "
3685 "pointer and %<void *%>"));
3686 /* Const and volatile mean something different for function types,
3687 so the usual warnings are not appropriate. */
3688 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3689 && TREE_CODE (ttl) != FUNCTION_TYPE)
3691 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3692 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3693 "qualifiers from pointer target type"),
3694 N_("assignment discards qualifiers "
3695 "from pointer target type"),
3696 N_("initialization discards qualifiers "
3697 "from pointer target type"),
3698 N_("return discards qualifiers from "
3699 "pointer target type"));
3700 /* If this is not a case of ignoring a mismatch in signedness,
3701 no warning. */
3702 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3703 || target_cmp)
3705 /* If there is a mismatch, do warn. */
3706 else
3707 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3708 "%d of %qE differ in signedness"),
3709 N_("pointer targets in assignment "
3710 "differ in signedness"),
3711 N_("pointer targets in initialization "
3712 "differ in signedness"),
3713 N_("pointer targets in return differ "
3714 "in signedness"));
3716 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3717 && TREE_CODE (ttr) == FUNCTION_TYPE)
3719 /* Because const and volatile on functions are restrictions
3720 that say the function will not do certain things,
3721 it is okay to use a const or volatile function
3722 where an ordinary one is wanted, but not vice-versa. */
3723 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3724 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3725 "qualified function pointer "
3726 "from unqualified"),
3727 N_("assignment makes qualified function "
3728 "pointer from unqualified"),
3729 N_("initialization makes qualified "
3730 "function pointer from unqualified"),
3731 N_("return makes qualified function "
3732 "pointer from unqualified"));
3735 else
3736 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3737 "incompatible pointer type"),
3738 N_("assignment from incompatible pointer type"),
3739 N_("initialization from incompatible "
3740 "pointer type"),
3741 N_("return from incompatible pointer type"));
3742 return convert (type, rhs);
3744 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3746 /* ??? This should not be an error when inlining calls to
3747 unprototyped functions. */
3748 error ("invalid use of non-lvalue array");
3749 return error_mark_node;
3751 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3753 /* An explicit constant 0 can convert to a pointer,
3754 or one that results from arithmetic, even including
3755 a cast to integer type. */
3756 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3758 !(TREE_CODE (rhs) == NOP_EXPR
3759 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3760 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3761 && integer_zerop (TREE_OPERAND (rhs, 0))))
3762 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3763 "pointer from integer without a cast"),
3764 N_("assignment makes pointer from integer "
3765 "without a cast"),
3766 N_("initialization makes pointer from "
3767 "integer without a cast"),
3768 N_("return makes pointer from integer "
3769 "without a cast"));
3771 return convert (type, rhs);
3773 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3775 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3776 "from pointer without a cast"),
3777 N_("assignment makes integer from pointer "
3778 "without a cast"),
3779 N_("initialization makes integer from pointer "
3780 "without a cast"),
3781 N_("return makes integer from pointer "
3782 "without a cast"));
3783 return convert (type, rhs);
3785 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3786 return convert (type, rhs);
3788 switch (errtype)
3790 case ic_argpass:
3791 case ic_argpass_nonproto:
3792 /* ??? This should not be an error when inlining calls to
3793 unprototyped functions. */
3794 error ("incompatible type for argument %d of %qE", parmnum, rname);
3795 break;
3796 case ic_assign:
3797 error ("incompatible types in assignment");
3798 break;
3799 case ic_init:
3800 error ("incompatible types in initialization");
3801 break;
3802 case ic_return:
3803 error ("incompatible types in return");
3804 break;
3805 default:
3806 gcc_unreachable ();
3809 return error_mark_node;
3812 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3813 is used for error and waring reporting and indicates which argument
3814 is being processed. */
3816 tree
3817 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3819 tree ret, type;
3821 /* If FN was prototyped, the value has been converted already
3822 in convert_arguments. */
3823 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3824 return value;
3826 type = TREE_TYPE (parm);
3827 ret = convert_for_assignment (type, value,
3828 ic_argpass_nonproto, fn,
3829 fn, argnum);
3830 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3831 && INTEGRAL_TYPE_P (type)
3832 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3833 ret = default_conversion (ret);
3834 return ret;
3837 /* If VALUE is a compound expr all of whose expressions are constant, then
3838 return its value. Otherwise, return error_mark_node.
3840 This is for handling COMPOUND_EXPRs as initializer elements
3841 which is allowed with a warning when -pedantic is specified. */
3843 static tree
3844 valid_compound_expr_initializer (tree value, tree endtype)
3846 if (TREE_CODE (value) == COMPOUND_EXPR)
3848 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3849 == error_mark_node)
3850 return error_mark_node;
3851 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3852 endtype);
3854 else if (!initializer_constant_valid_p (value, endtype))
3855 return error_mark_node;
3856 else
3857 return value;
3860 /* Perform appropriate conversions on the initial value of a variable,
3861 store it in the declaration DECL,
3862 and print any error messages that are appropriate.
3863 If the init is invalid, store an ERROR_MARK. */
3865 void
3866 store_init_value (tree decl, tree init)
3868 tree value, type;
3870 /* If variable's type was invalidly declared, just ignore it. */
3872 type = TREE_TYPE (decl);
3873 if (TREE_CODE (type) == ERROR_MARK)
3874 return;
3876 /* Digest the specified initializer into an expression. */
3878 value = digest_init (type, init, true, TREE_STATIC (decl));
3880 /* Store the expression if valid; else report error. */
3882 if (warn_traditional && !in_system_header
3883 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3884 warning ("traditional C rejects automatic aggregate initialization");
3886 DECL_INITIAL (decl) = value;
3888 /* ANSI wants warnings about out-of-range constant initializers. */
3889 STRIP_TYPE_NOPS (value);
3890 constant_expression_warning (value);
3892 /* Check if we need to set array size from compound literal size. */
3893 if (TREE_CODE (type) == ARRAY_TYPE
3894 && TYPE_DOMAIN (type) == 0
3895 && value != error_mark_node)
3897 tree inside_init = init;
3899 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3900 inside_init = TREE_OPERAND (init, 0);
3901 inside_init = fold (inside_init);
3903 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3905 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3907 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3909 /* For int foo[] = (int [3]){1}; we need to set array size
3910 now since later on array initializer will be just the
3911 brace enclosed list of the compound literal. */
3912 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3913 layout_type (type);
3914 layout_decl (decl, 0);
3920 /* Methods for storing and printing names for error messages. */
3922 /* Implement a spelling stack that allows components of a name to be pushed
3923 and popped. Each element on the stack is this structure. */
3925 struct spelling
3927 int kind;
3928 union
3930 int i;
3931 const char *s;
3932 } u;
3935 #define SPELLING_STRING 1
3936 #define SPELLING_MEMBER 2
3937 #define SPELLING_BOUNDS 3
3939 static struct spelling *spelling; /* Next stack element (unused). */
3940 static struct spelling *spelling_base; /* Spelling stack base. */
3941 static int spelling_size; /* Size of the spelling stack. */
3943 /* Macros to save and restore the spelling stack around push_... functions.
3944 Alternative to SAVE_SPELLING_STACK. */
3946 #define SPELLING_DEPTH() (spelling - spelling_base)
3947 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3949 /* Push an element on the spelling stack with type KIND and assign VALUE
3950 to MEMBER. */
3952 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3954 int depth = SPELLING_DEPTH (); \
3956 if (depth >= spelling_size) \
3958 spelling_size += 10; \
3959 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3960 spelling_size); \
3961 RESTORE_SPELLING_DEPTH (depth); \
3964 spelling->kind = (KIND); \
3965 spelling->MEMBER = (VALUE); \
3966 spelling++; \
3969 /* Push STRING on the stack. Printed literally. */
3971 static void
3972 push_string (const char *string)
3974 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3977 /* Push a member name on the stack. Printed as '.' STRING. */
3979 static void
3980 push_member_name (tree decl)
3982 const char *const string
3983 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3984 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3987 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3989 static void
3990 push_array_bounds (int bounds)
3992 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3995 /* Compute the maximum size in bytes of the printed spelling. */
3997 static int
3998 spelling_length (void)
4000 int size = 0;
4001 struct spelling *p;
4003 for (p = spelling_base; p < spelling; p++)
4005 if (p->kind == SPELLING_BOUNDS)
4006 size += 25;
4007 else
4008 size += strlen (p->u.s) + 1;
4011 return size;
4014 /* Print the spelling to BUFFER and return it. */
4016 static char *
4017 print_spelling (char *buffer)
4019 char *d = buffer;
4020 struct spelling *p;
4022 for (p = spelling_base; p < spelling; p++)
4023 if (p->kind == SPELLING_BOUNDS)
4025 sprintf (d, "[%d]", p->u.i);
4026 d += strlen (d);
4028 else
4030 const char *s;
4031 if (p->kind == SPELLING_MEMBER)
4032 *d++ = '.';
4033 for (s = p->u.s; (*d = *s++); d++)
4036 *d++ = '\0';
4037 return buffer;
4040 /* Issue an error message for a bad initializer component.
4041 MSGID identifies the message.
4042 The component name is taken from the spelling stack. */
4044 void
4045 error_init (const char *msgid)
4047 char *ofwhat;
4049 error ("%s", _(msgid));
4050 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4051 if (*ofwhat)
4052 error ("(near initialization for %qs)", ofwhat);
4055 /* Issue a pedantic warning for a bad initializer component.
4056 MSGID identifies the message.
4057 The component name is taken from the spelling stack. */
4059 void
4060 pedwarn_init (const char *msgid)
4062 char *ofwhat;
4064 pedwarn ("%s", _(msgid));
4065 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4066 if (*ofwhat)
4067 pedwarn ("(near initialization for %qs)", ofwhat);
4070 /* Issue a warning for a bad initializer component.
4071 MSGID identifies the message.
4072 The component name is taken from the spelling stack. */
4074 static void
4075 warning_init (const char *msgid)
4077 char *ofwhat;
4079 warning ("%s", _(msgid));
4080 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4081 if (*ofwhat)
4082 warning ("(near initialization for %qs)", ofwhat);
4085 /* If TYPE is an array type and EXPR is a parenthesized string
4086 constant, warn if pedantic that EXPR is being used to initialize an
4087 object of type TYPE. */
4089 void
4090 maybe_warn_string_init (tree type, struct c_expr expr)
4092 if (pedantic
4093 && TREE_CODE (type) == ARRAY_TYPE
4094 && TREE_CODE (expr.value) == STRING_CST
4095 && expr.original_code != STRING_CST)
4096 pedwarn_init ("array initialized from parenthesized string constant");
4099 /* Digest the parser output INIT as an initializer for type TYPE.
4100 Return a C expression of type TYPE to represent the initial value.
4102 If INIT is a string constant, STRICT_STRING is true if it is
4103 unparenthesized or we should not warn here for it being parenthesized.
4104 For other types of INIT, STRICT_STRING is not used.
4106 REQUIRE_CONSTANT requests an error if non-constant initializers or
4107 elements are seen. */
4109 static tree
4110 digest_init (tree type, tree init, bool strict_string, int require_constant)
4112 enum tree_code code = TREE_CODE (type);
4113 tree inside_init = init;
4115 if (type == error_mark_node
4116 || init == error_mark_node
4117 || TREE_TYPE (init) == error_mark_node)
4118 return error_mark_node;
4120 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4121 /* Do not use STRIP_NOPS here. We do not want an enumerator
4122 whose value is 0 to count as a null pointer constant. */
4123 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4124 inside_init = TREE_OPERAND (init, 0);
4126 inside_init = fold (inside_init);
4128 /* Initialization of an array of chars from a string constant
4129 optionally enclosed in braces. */
4131 if (code == ARRAY_TYPE && inside_init
4132 && TREE_CODE (inside_init) == STRING_CST)
4134 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4135 /* Note that an array could be both an array of character type
4136 and an array of wchar_t if wchar_t is signed char or unsigned
4137 char. */
4138 bool char_array = (typ1 == char_type_node
4139 || typ1 == signed_char_type_node
4140 || typ1 == unsigned_char_type_node);
4141 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4142 if (char_array || wchar_array)
4144 struct c_expr expr;
4145 bool char_string;
4146 expr.value = inside_init;
4147 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4148 maybe_warn_string_init (type, expr);
4150 char_string
4151 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4152 == char_type_node);
4154 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4155 TYPE_MAIN_VARIANT (type)))
4156 return inside_init;
4158 if (!wchar_array && !char_string)
4160 error_init ("char-array initialized from wide string");
4161 return error_mark_node;
4163 if (char_string && !char_array)
4165 error_init ("wchar_t-array initialized from non-wide string");
4166 return error_mark_node;
4169 TREE_TYPE (inside_init) = type;
4170 if (TYPE_DOMAIN (type) != 0
4171 && TYPE_SIZE (type) != 0
4172 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4173 /* Subtract 1 (or sizeof (wchar_t))
4174 because it's ok to ignore the terminating null char
4175 that is counted in the length of the constant. */
4176 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4177 TREE_STRING_LENGTH (inside_init)
4178 - ((TYPE_PRECISION (typ1)
4179 != TYPE_PRECISION (char_type_node))
4180 ? (TYPE_PRECISION (wchar_type_node)
4181 / BITS_PER_UNIT)
4182 : 1)))
4183 pedwarn_init ("initializer-string for array of chars is too long");
4185 return inside_init;
4187 else if (INTEGRAL_TYPE_P (typ1))
4189 error_init ("array of inappropriate type initialized "
4190 "from string constant");
4191 return error_mark_node;
4195 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4196 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4197 below and handle as a constructor. */
4198 if (code == VECTOR_TYPE
4199 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4200 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4201 && TREE_CONSTANT (inside_init))
4203 if (TREE_CODE (inside_init) == VECTOR_CST
4204 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4205 TYPE_MAIN_VARIANT (type)))
4206 return inside_init;
4207 else
4208 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4211 /* Any type can be initialized
4212 from an expression of the same type, optionally with braces. */
4214 if (inside_init && TREE_TYPE (inside_init) != 0
4215 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4216 TYPE_MAIN_VARIANT (type))
4217 || (code == ARRAY_TYPE
4218 && comptypes (TREE_TYPE (inside_init), type))
4219 || (code == VECTOR_TYPE
4220 && comptypes (TREE_TYPE (inside_init), type))
4221 || (code == POINTER_TYPE
4222 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4223 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4224 TREE_TYPE (type)))
4225 || (code == POINTER_TYPE
4226 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4227 && comptypes (TREE_TYPE (inside_init),
4228 TREE_TYPE (type)))))
4230 if (code == POINTER_TYPE)
4232 inside_init = default_function_array_conversion (inside_init);
4234 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4236 error_init ("invalid use of non-lvalue array");
4237 return error_mark_node;
4241 if (code == VECTOR_TYPE)
4242 /* Although the types are compatible, we may require a
4243 conversion. */
4244 inside_init = convert (type, inside_init);
4246 if (require_constant && !flag_isoc99
4247 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4249 /* As an extension, allow initializing objects with static storage
4250 duration with compound literals (which are then treated just as
4251 the brace enclosed list they contain). */
4252 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4253 inside_init = DECL_INITIAL (decl);
4256 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4257 && TREE_CODE (inside_init) != CONSTRUCTOR)
4259 error_init ("array initialized from non-constant array expression");
4260 return error_mark_node;
4263 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4264 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4266 /* Compound expressions can only occur here if -pedantic or
4267 -pedantic-errors is specified. In the later case, we always want
4268 an error. In the former case, we simply want a warning. */
4269 if (require_constant && pedantic
4270 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4272 inside_init
4273 = valid_compound_expr_initializer (inside_init,
4274 TREE_TYPE (inside_init));
4275 if (inside_init == error_mark_node)
4276 error_init ("initializer element is not constant");
4277 else
4278 pedwarn_init ("initializer element is not constant");
4279 if (flag_pedantic_errors)
4280 inside_init = error_mark_node;
4282 else if (require_constant
4283 && !initializer_constant_valid_p (inside_init,
4284 TREE_TYPE (inside_init)))
4286 error_init ("initializer element is not constant");
4287 inside_init = error_mark_node;
4290 return inside_init;
4293 /* Handle scalar types, including conversions. */
4295 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4296 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4297 || code == VECTOR_TYPE)
4299 /* Note that convert_for_assignment calls default_conversion
4300 for arrays and functions. We must not call it in the
4301 case where inside_init is a null pointer constant. */
4302 inside_init
4303 = convert_for_assignment (type, init, ic_init,
4304 NULL_TREE, NULL_TREE, 0);
4306 /* Check to see if we have already given an error message. */
4307 if (inside_init == error_mark_node)
4309 else if (require_constant && !TREE_CONSTANT (inside_init))
4311 error_init ("initializer element is not constant");
4312 inside_init = error_mark_node;
4314 else if (require_constant
4315 && !initializer_constant_valid_p (inside_init,
4316 TREE_TYPE (inside_init)))
4318 error_init ("initializer element is not computable at load time");
4319 inside_init = error_mark_node;
4322 return inside_init;
4325 /* Come here only for records and arrays. */
4327 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4329 error_init ("variable-sized object may not be initialized");
4330 return error_mark_node;
4333 error_init ("invalid initializer");
4334 return error_mark_node;
4337 /* Handle initializers that use braces. */
4339 /* Type of object we are accumulating a constructor for.
4340 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4341 static tree constructor_type;
4343 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4344 left to fill. */
4345 static tree constructor_fields;
4347 /* For an ARRAY_TYPE, this is the specified index
4348 at which to store the next element we get. */
4349 static tree constructor_index;
4351 /* For an ARRAY_TYPE, this is the maximum index. */
4352 static tree constructor_max_index;
4354 /* For a RECORD_TYPE, this is the first field not yet written out. */
4355 static tree constructor_unfilled_fields;
4357 /* For an ARRAY_TYPE, this is the index of the first element
4358 not yet written out. */
4359 static tree constructor_unfilled_index;
4361 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4362 This is so we can generate gaps between fields, when appropriate. */
4363 static tree constructor_bit_index;
4365 /* If we are saving up the elements rather than allocating them,
4366 this is the list of elements so far (in reverse order,
4367 most recent first). */
4368 static tree constructor_elements;
4370 /* 1 if constructor should be incrementally stored into a constructor chain,
4371 0 if all the elements should be kept in AVL tree. */
4372 static int constructor_incremental;
4374 /* 1 if so far this constructor's elements are all compile-time constants. */
4375 static int constructor_constant;
4377 /* 1 if so far this constructor's elements are all valid address constants. */
4378 static int constructor_simple;
4380 /* 1 if this constructor is erroneous so far. */
4381 static int constructor_erroneous;
4383 /* Structure for managing pending initializer elements, organized as an
4384 AVL tree. */
4386 struct init_node
4388 struct init_node *left, *right;
4389 struct init_node *parent;
4390 int balance;
4391 tree purpose;
4392 tree value;
4395 /* Tree of pending elements at this constructor level.
4396 These are elements encountered out of order
4397 which belong at places we haven't reached yet in actually
4398 writing the output.
4399 Will never hold tree nodes across GC runs. */
4400 static struct init_node *constructor_pending_elts;
4402 /* The SPELLING_DEPTH of this constructor. */
4403 static int constructor_depth;
4405 /* 0 if implicitly pushing constructor levels is allowed. */
4406 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4408 /* DECL node for which an initializer is being read.
4409 0 means we are reading a constructor expression
4410 such as (struct foo) {...}. */
4411 static tree constructor_decl;
4413 /* Nonzero if this is an initializer for a top-level decl. */
4414 static int constructor_top_level;
4416 /* Nonzero if there were any member designators in this initializer. */
4417 static int constructor_designated;
4419 /* Nesting depth of designator list. */
4420 static int designator_depth;
4422 /* Nonzero if there were diagnosed errors in this designator list. */
4423 static int designator_errorneous;
4426 /* This stack has a level for each implicit or explicit level of
4427 structuring in the initializer, including the outermost one. It
4428 saves the values of most of the variables above. */
4430 struct constructor_range_stack;
4432 struct constructor_stack
4434 struct constructor_stack *next;
4435 tree type;
4436 tree fields;
4437 tree index;
4438 tree max_index;
4439 tree unfilled_index;
4440 tree unfilled_fields;
4441 tree bit_index;
4442 tree elements;
4443 struct init_node *pending_elts;
4444 int offset;
4445 int depth;
4446 /* If value nonzero, this value should replace the entire
4447 constructor at this level. */
4448 struct c_expr replacement_value;
4449 struct constructor_range_stack *range_stack;
4450 char constant;
4451 char simple;
4452 char implicit;
4453 char erroneous;
4454 char outer;
4455 char incremental;
4456 char designated;
4459 struct constructor_stack *constructor_stack;
4461 /* This stack represents designators from some range designator up to
4462 the last designator in the list. */
4464 struct constructor_range_stack
4466 struct constructor_range_stack *next, *prev;
4467 struct constructor_stack *stack;
4468 tree range_start;
4469 tree index;
4470 tree range_end;
4471 tree fields;
4474 struct constructor_range_stack *constructor_range_stack;
4476 /* This stack records separate initializers that are nested.
4477 Nested initializers can't happen in ANSI C, but GNU C allows them
4478 in cases like { ... (struct foo) { ... } ... }. */
4480 struct initializer_stack
4482 struct initializer_stack *next;
4483 tree decl;
4484 struct constructor_stack *constructor_stack;
4485 struct constructor_range_stack *constructor_range_stack;
4486 tree elements;
4487 struct spelling *spelling;
4488 struct spelling *spelling_base;
4489 int spelling_size;
4490 char top_level;
4491 char require_constant_value;
4492 char require_constant_elements;
4495 struct initializer_stack *initializer_stack;
4497 /* Prepare to parse and output the initializer for variable DECL. */
4499 void
4500 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4502 const char *locus;
4503 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4505 p->decl = constructor_decl;
4506 p->require_constant_value = require_constant_value;
4507 p->require_constant_elements = require_constant_elements;
4508 p->constructor_stack = constructor_stack;
4509 p->constructor_range_stack = constructor_range_stack;
4510 p->elements = constructor_elements;
4511 p->spelling = spelling;
4512 p->spelling_base = spelling_base;
4513 p->spelling_size = spelling_size;
4514 p->top_level = constructor_top_level;
4515 p->next = initializer_stack;
4516 initializer_stack = p;
4518 constructor_decl = decl;
4519 constructor_designated = 0;
4520 constructor_top_level = top_level;
4522 if (decl != 0)
4524 require_constant_value = TREE_STATIC (decl);
4525 require_constant_elements
4526 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4527 /* For a scalar, you can always use any value to initialize,
4528 even within braces. */
4529 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4530 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4531 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4532 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4533 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4535 else
4537 require_constant_value = 0;
4538 require_constant_elements = 0;
4539 locus = "(anonymous)";
4542 constructor_stack = 0;
4543 constructor_range_stack = 0;
4545 missing_braces_mentioned = 0;
4547 spelling_base = 0;
4548 spelling_size = 0;
4549 RESTORE_SPELLING_DEPTH (0);
4551 if (locus)
4552 push_string (locus);
4555 void
4556 finish_init (void)
4558 struct initializer_stack *p = initializer_stack;
4560 /* Free the whole constructor stack of this initializer. */
4561 while (constructor_stack)
4563 struct constructor_stack *q = constructor_stack;
4564 constructor_stack = q->next;
4565 free (q);
4568 gcc_assert (!constructor_range_stack);
4570 /* Pop back to the data of the outer initializer (if any). */
4571 free (spelling_base);
4573 constructor_decl = p->decl;
4574 require_constant_value = p->require_constant_value;
4575 require_constant_elements = p->require_constant_elements;
4576 constructor_stack = p->constructor_stack;
4577 constructor_range_stack = p->constructor_range_stack;
4578 constructor_elements = p->elements;
4579 spelling = p->spelling;
4580 spelling_base = p->spelling_base;
4581 spelling_size = p->spelling_size;
4582 constructor_top_level = p->top_level;
4583 initializer_stack = p->next;
4584 free (p);
4587 /* Call here when we see the initializer is surrounded by braces.
4588 This is instead of a call to push_init_level;
4589 it is matched by a call to pop_init_level.
4591 TYPE is the type to initialize, for a constructor expression.
4592 For an initializer for a decl, TYPE is zero. */
4594 void
4595 really_start_incremental_init (tree type)
4597 struct constructor_stack *p = XNEW (struct constructor_stack);
4599 if (type == 0)
4600 type = TREE_TYPE (constructor_decl);
4602 if (targetm.vector_opaque_p (type))
4603 error ("opaque vector types cannot be initialized");
4605 p->type = constructor_type;
4606 p->fields = constructor_fields;
4607 p->index = constructor_index;
4608 p->max_index = constructor_max_index;
4609 p->unfilled_index = constructor_unfilled_index;
4610 p->unfilled_fields = constructor_unfilled_fields;
4611 p->bit_index = constructor_bit_index;
4612 p->elements = constructor_elements;
4613 p->constant = constructor_constant;
4614 p->simple = constructor_simple;
4615 p->erroneous = constructor_erroneous;
4616 p->pending_elts = constructor_pending_elts;
4617 p->depth = constructor_depth;
4618 p->replacement_value.value = 0;
4619 p->replacement_value.original_code = ERROR_MARK;
4620 p->implicit = 0;
4621 p->range_stack = 0;
4622 p->outer = 0;
4623 p->incremental = constructor_incremental;
4624 p->designated = constructor_designated;
4625 p->next = 0;
4626 constructor_stack = p;
4628 constructor_constant = 1;
4629 constructor_simple = 1;
4630 constructor_depth = SPELLING_DEPTH ();
4631 constructor_elements = 0;
4632 constructor_pending_elts = 0;
4633 constructor_type = type;
4634 constructor_incremental = 1;
4635 constructor_designated = 0;
4636 designator_depth = 0;
4637 designator_errorneous = 0;
4639 if (TREE_CODE (constructor_type) == RECORD_TYPE
4640 || TREE_CODE (constructor_type) == UNION_TYPE)
4642 constructor_fields = TYPE_FIELDS (constructor_type);
4643 /* Skip any nameless bit fields at the beginning. */
4644 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4645 && DECL_NAME (constructor_fields) == 0)
4646 constructor_fields = TREE_CHAIN (constructor_fields);
4648 constructor_unfilled_fields = constructor_fields;
4649 constructor_bit_index = bitsize_zero_node;
4651 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4653 if (TYPE_DOMAIN (constructor_type))
4655 constructor_max_index
4656 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4658 /* Detect non-empty initializations of zero-length arrays. */
4659 if (constructor_max_index == NULL_TREE
4660 && TYPE_SIZE (constructor_type))
4661 constructor_max_index = build_int_cst (NULL_TREE, -1);
4663 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4664 to initialize VLAs will cause a proper error; avoid tree
4665 checking errors as well by setting a safe value. */
4666 if (constructor_max_index
4667 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4668 constructor_max_index = build_int_cst (NULL_TREE, -1);
4670 constructor_index
4671 = convert (bitsizetype,
4672 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4674 else
4675 constructor_index = bitsize_zero_node;
4677 constructor_unfilled_index = constructor_index;
4679 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4681 /* Vectors are like simple fixed-size arrays. */
4682 constructor_max_index =
4683 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4684 constructor_index = convert (bitsizetype, bitsize_zero_node);
4685 constructor_unfilled_index = constructor_index;
4687 else
4689 /* Handle the case of int x = {5}; */
4690 constructor_fields = constructor_type;
4691 constructor_unfilled_fields = constructor_type;
4695 /* Push down into a subobject, for initialization.
4696 If this is for an explicit set of braces, IMPLICIT is 0.
4697 If it is because the next element belongs at a lower level,
4698 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4700 void
4701 push_init_level (int implicit)
4703 struct constructor_stack *p;
4704 tree value = NULL_TREE;
4706 /* If we've exhausted any levels that didn't have braces,
4707 pop them now. */
4708 while (constructor_stack->implicit)
4710 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4711 || TREE_CODE (constructor_type) == UNION_TYPE)
4712 && constructor_fields == 0)
4713 process_init_element (pop_init_level (1));
4714 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4715 && constructor_max_index
4716 && tree_int_cst_lt (constructor_max_index, constructor_index))
4717 process_init_element (pop_init_level (1));
4718 else
4719 break;
4722 /* Unless this is an explicit brace, we need to preserve previous
4723 content if any. */
4724 if (implicit)
4726 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4727 || TREE_CODE (constructor_type) == UNION_TYPE)
4728 && constructor_fields)
4729 value = find_init_member (constructor_fields);
4730 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4731 value = find_init_member (constructor_index);
4734 p = XNEW (struct constructor_stack);
4735 p->type = constructor_type;
4736 p->fields = constructor_fields;
4737 p->index = constructor_index;
4738 p->max_index = constructor_max_index;
4739 p->unfilled_index = constructor_unfilled_index;
4740 p->unfilled_fields = constructor_unfilled_fields;
4741 p->bit_index = constructor_bit_index;
4742 p->elements = constructor_elements;
4743 p->constant = constructor_constant;
4744 p->simple = constructor_simple;
4745 p->erroneous = constructor_erroneous;
4746 p->pending_elts = constructor_pending_elts;
4747 p->depth = constructor_depth;
4748 p->replacement_value.value = 0;
4749 p->replacement_value.original_code = ERROR_MARK;
4750 p->implicit = implicit;
4751 p->outer = 0;
4752 p->incremental = constructor_incremental;
4753 p->designated = constructor_designated;
4754 p->next = constructor_stack;
4755 p->range_stack = 0;
4756 constructor_stack = p;
4758 constructor_constant = 1;
4759 constructor_simple = 1;
4760 constructor_depth = SPELLING_DEPTH ();
4761 constructor_elements = 0;
4762 constructor_incremental = 1;
4763 constructor_designated = 0;
4764 constructor_pending_elts = 0;
4765 if (!implicit)
4767 p->range_stack = constructor_range_stack;
4768 constructor_range_stack = 0;
4769 designator_depth = 0;
4770 designator_errorneous = 0;
4773 /* Don't die if an entire brace-pair level is superfluous
4774 in the containing level. */
4775 if (constructor_type == 0)
4777 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4778 || TREE_CODE (constructor_type) == UNION_TYPE)
4780 /* Don't die if there are extra init elts at the end. */
4781 if (constructor_fields == 0)
4782 constructor_type = 0;
4783 else
4785 constructor_type = TREE_TYPE (constructor_fields);
4786 push_member_name (constructor_fields);
4787 constructor_depth++;
4790 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4792 constructor_type = TREE_TYPE (constructor_type);
4793 push_array_bounds (tree_low_cst (constructor_index, 0));
4794 constructor_depth++;
4797 if (constructor_type == 0)
4799 error_init ("extra brace group at end of initializer");
4800 constructor_fields = 0;
4801 constructor_unfilled_fields = 0;
4802 return;
4805 if (value && TREE_CODE (value) == CONSTRUCTOR)
4807 constructor_constant = TREE_CONSTANT (value);
4808 constructor_simple = TREE_STATIC (value);
4809 constructor_elements = CONSTRUCTOR_ELTS (value);
4810 if (constructor_elements
4811 && (TREE_CODE (constructor_type) == RECORD_TYPE
4812 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4813 set_nonincremental_init ();
4816 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4818 missing_braces_mentioned = 1;
4819 warning_init ("missing braces around initializer");
4822 if (TREE_CODE (constructor_type) == RECORD_TYPE
4823 || TREE_CODE (constructor_type) == UNION_TYPE)
4825 constructor_fields = TYPE_FIELDS (constructor_type);
4826 /* Skip any nameless bit fields at the beginning. */
4827 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4828 && DECL_NAME (constructor_fields) == 0)
4829 constructor_fields = TREE_CHAIN (constructor_fields);
4831 constructor_unfilled_fields = constructor_fields;
4832 constructor_bit_index = bitsize_zero_node;
4834 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4836 /* Vectors are like simple fixed-size arrays. */
4837 constructor_max_index =
4838 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4839 constructor_index = convert (bitsizetype, integer_zero_node);
4840 constructor_unfilled_index = constructor_index;
4842 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4844 if (TYPE_DOMAIN (constructor_type))
4846 constructor_max_index
4847 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4849 /* Detect non-empty initializations of zero-length arrays. */
4850 if (constructor_max_index == NULL_TREE
4851 && TYPE_SIZE (constructor_type))
4852 constructor_max_index = build_int_cst (NULL_TREE, -1);
4854 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4855 to initialize VLAs will cause a proper error; avoid tree
4856 checking errors as well by setting a safe value. */
4857 if (constructor_max_index
4858 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4859 constructor_max_index = build_int_cst (NULL_TREE, -1);
4861 constructor_index
4862 = convert (bitsizetype,
4863 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4865 else
4866 constructor_index = bitsize_zero_node;
4868 constructor_unfilled_index = constructor_index;
4869 if (value && TREE_CODE (value) == STRING_CST)
4871 /* We need to split the char/wchar array into individual
4872 characters, so that we don't have to special case it
4873 everywhere. */
4874 set_nonincremental_init_from_string (value);
4877 else
4879 warning_init ("braces around scalar initializer");
4880 constructor_fields = constructor_type;
4881 constructor_unfilled_fields = constructor_type;
4885 /* At the end of an implicit or explicit brace level,
4886 finish up that level of constructor. If a single expression
4887 with redundant braces initialized that level, return the
4888 c_expr structure for that expression. Otherwise, the original_code
4889 element is set to ERROR_MARK.
4890 If we were outputting the elements as they are read, return 0 as the value
4891 from inner levels (process_init_element ignores that),
4892 but return error_mark_node as the value from the outermost level
4893 (that's what we want to put in DECL_INITIAL).
4894 Otherwise, return a CONSTRUCTOR expression as the value. */
4896 struct c_expr
4897 pop_init_level (int implicit)
4899 struct constructor_stack *p;
4900 struct c_expr ret;
4901 ret.value = 0;
4902 ret.original_code = ERROR_MARK;
4904 if (implicit == 0)
4906 /* When we come to an explicit close brace,
4907 pop any inner levels that didn't have explicit braces. */
4908 while (constructor_stack->implicit)
4909 process_init_element (pop_init_level (1));
4911 gcc_assert (!constructor_range_stack);
4914 /* Now output all pending elements. */
4915 constructor_incremental = 1;
4916 output_pending_init_elements (1);
4918 p = constructor_stack;
4920 /* Error for initializing a flexible array member, or a zero-length
4921 array member in an inappropriate context. */
4922 if (constructor_type && constructor_fields
4923 && TREE_CODE (constructor_type) == ARRAY_TYPE
4924 && TYPE_DOMAIN (constructor_type)
4925 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4927 /* Silently discard empty initializations. The parser will
4928 already have pedwarned for empty brackets. */
4929 if (integer_zerop (constructor_unfilled_index))
4930 constructor_type = NULL_TREE;
4931 else
4933 gcc_assert (!TYPE_SIZE (constructor_type));
4935 if (constructor_depth > 2)
4936 error_init ("initialization of flexible array member in a nested context");
4937 else if (pedantic)
4938 pedwarn_init ("initialization of a flexible array member");
4940 /* We have already issued an error message for the existence
4941 of a flexible array member not at the end of the structure.
4942 Discard the initializer so that we do not abort later. */
4943 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4944 constructor_type = NULL_TREE;
4948 /* Warn when some struct elements are implicitly initialized to zero. */
4949 if (warn_missing_field_initializers
4950 && constructor_type
4951 && TREE_CODE (constructor_type) == RECORD_TYPE
4952 && constructor_unfilled_fields)
4954 /* Do not warn for flexible array members or zero-length arrays. */
4955 while (constructor_unfilled_fields
4956 && (!DECL_SIZE (constructor_unfilled_fields)
4957 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4958 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4960 /* Do not warn if this level of the initializer uses member
4961 designators; it is likely to be deliberate. */
4962 if (constructor_unfilled_fields && !constructor_designated)
4964 push_member_name (constructor_unfilled_fields);
4965 warning_init ("missing initializer");
4966 RESTORE_SPELLING_DEPTH (constructor_depth);
4970 /* Pad out the end of the structure. */
4971 if (p->replacement_value.value)
4972 /* If this closes a superfluous brace pair,
4973 just pass out the element between them. */
4974 ret = p->replacement_value;
4975 else if (constructor_type == 0)
4977 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4978 && TREE_CODE (constructor_type) != UNION_TYPE
4979 && TREE_CODE (constructor_type) != ARRAY_TYPE
4980 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4982 /* A nonincremental scalar initializer--just return
4983 the element, after verifying there is just one. */
4984 if (constructor_elements == 0)
4986 if (!constructor_erroneous)
4987 error_init ("empty scalar initializer");
4988 ret.value = error_mark_node;
4990 else if (TREE_CHAIN (constructor_elements) != 0)
4992 error_init ("extra elements in scalar initializer");
4993 ret.value = TREE_VALUE (constructor_elements);
4995 else
4996 ret.value = TREE_VALUE (constructor_elements);
4998 else
5000 if (constructor_erroneous)
5001 ret.value = error_mark_node;
5002 else
5004 ret.value = build_constructor (constructor_type,
5005 nreverse (constructor_elements));
5006 if (constructor_constant)
5007 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5008 if (constructor_constant && constructor_simple)
5009 TREE_STATIC (ret.value) = 1;
5013 constructor_type = p->type;
5014 constructor_fields = p->fields;
5015 constructor_index = p->index;
5016 constructor_max_index = p->max_index;
5017 constructor_unfilled_index = p->unfilled_index;
5018 constructor_unfilled_fields = p->unfilled_fields;
5019 constructor_bit_index = p->bit_index;
5020 constructor_elements = p->elements;
5021 constructor_constant = p->constant;
5022 constructor_simple = p->simple;
5023 constructor_erroneous = p->erroneous;
5024 constructor_incremental = p->incremental;
5025 constructor_designated = p->designated;
5026 constructor_pending_elts = p->pending_elts;
5027 constructor_depth = p->depth;
5028 if (!p->implicit)
5029 constructor_range_stack = p->range_stack;
5030 RESTORE_SPELLING_DEPTH (constructor_depth);
5032 constructor_stack = p->next;
5033 free (p);
5035 if (ret.value == 0)
5037 if (constructor_stack == 0)
5039 ret.value = error_mark_node;
5040 return ret;
5042 return ret;
5044 return ret;
5047 /* Common handling for both array range and field name designators.
5048 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5050 static int
5051 set_designator (int array)
5053 tree subtype;
5054 enum tree_code subcode;
5056 /* Don't die if an entire brace-pair level is superfluous
5057 in the containing level. */
5058 if (constructor_type == 0)
5059 return 1;
5061 /* If there were errors in this designator list already, bail out
5062 silently. */
5063 if (designator_errorneous)
5064 return 1;
5066 if (!designator_depth)
5068 gcc_assert (!constructor_range_stack);
5070 /* Designator list starts at the level of closest explicit
5071 braces. */
5072 while (constructor_stack->implicit)
5073 process_init_element (pop_init_level (1));
5074 constructor_designated = 1;
5075 return 0;
5078 if (constructor_no_implicit)
5080 error_init ("initialization designators may not nest");
5081 return 1;
5084 switch (TREE_CODE (constructor_type))
5086 case RECORD_TYPE:
5087 case UNION_TYPE:
5088 subtype = TREE_TYPE (constructor_fields);
5089 if (subtype != error_mark_node)
5090 subtype = TYPE_MAIN_VARIANT (subtype);
5091 break;
5092 case ARRAY_TYPE:
5093 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5094 break;
5095 default:
5096 gcc_unreachable ();
5099 subcode = TREE_CODE (subtype);
5100 if (array && subcode != ARRAY_TYPE)
5102 error_init ("array index in non-array initializer");
5103 return 1;
5105 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5107 error_init ("field name not in record or union initializer");
5108 return 1;
5111 constructor_designated = 1;
5112 push_init_level (2);
5113 return 0;
5116 /* If there are range designators in designator list, push a new designator
5117 to constructor_range_stack. RANGE_END is end of such stack range or
5118 NULL_TREE if there is no range designator at this level. */
5120 static void
5121 push_range_stack (tree range_end)
5123 struct constructor_range_stack *p;
5125 p = GGC_NEW (struct constructor_range_stack);
5126 p->prev = constructor_range_stack;
5127 p->next = 0;
5128 p->fields = constructor_fields;
5129 p->range_start = constructor_index;
5130 p->index = constructor_index;
5131 p->stack = constructor_stack;
5132 p->range_end = range_end;
5133 if (constructor_range_stack)
5134 constructor_range_stack->next = p;
5135 constructor_range_stack = p;
5138 /* Within an array initializer, specify the next index to be initialized.
5139 FIRST is that index. If LAST is nonzero, then initialize a range
5140 of indices, running from FIRST through LAST. */
5142 void
5143 set_init_index (tree first, tree last)
5145 if (set_designator (1))
5146 return;
5148 designator_errorneous = 1;
5150 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5151 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5153 error_init ("array index in initializer not of integer type");
5154 return;
5157 while ((TREE_CODE (first) == NOP_EXPR
5158 || TREE_CODE (first) == CONVERT_EXPR
5159 || TREE_CODE (first) == NON_LVALUE_EXPR)
5160 && (TYPE_MODE (TREE_TYPE (first))
5161 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5162 first = TREE_OPERAND (first, 0);
5164 if (last)
5165 while ((TREE_CODE (last) == NOP_EXPR
5166 || TREE_CODE (last) == CONVERT_EXPR
5167 || TREE_CODE (last) == NON_LVALUE_EXPR)
5168 && (TYPE_MODE (TREE_TYPE (last))
5169 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5170 last = TREE_OPERAND (last, 0);
5172 if (TREE_CODE (first) != INTEGER_CST)
5173 error_init ("nonconstant array index in initializer");
5174 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5175 error_init ("nonconstant array index in initializer");
5176 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5177 error_init ("array index in non-array initializer");
5178 else if (tree_int_cst_sgn (first) == -1)
5179 error_init ("array index in initializer exceeds array bounds");
5180 else if (constructor_max_index
5181 && tree_int_cst_lt (constructor_max_index, first))
5182 error_init ("array index in initializer exceeds array bounds");
5183 else
5185 constructor_index = convert (bitsizetype, first);
5187 if (last)
5189 if (tree_int_cst_equal (first, last))
5190 last = 0;
5191 else if (tree_int_cst_lt (last, first))
5193 error_init ("empty index range in initializer");
5194 last = 0;
5196 else
5198 last = convert (bitsizetype, last);
5199 if (constructor_max_index != 0
5200 && tree_int_cst_lt (constructor_max_index, last))
5202 error_init ("array index range in initializer exceeds array bounds");
5203 last = 0;
5208 designator_depth++;
5209 designator_errorneous = 0;
5210 if (constructor_range_stack || last)
5211 push_range_stack (last);
5215 /* Within a struct initializer, specify the next field to be initialized. */
5217 void
5218 set_init_label (tree fieldname)
5220 tree tail;
5222 if (set_designator (0))
5223 return;
5225 designator_errorneous = 1;
5227 if (TREE_CODE (constructor_type) != RECORD_TYPE
5228 && TREE_CODE (constructor_type) != UNION_TYPE)
5230 error_init ("field name not in record or union initializer");
5231 return;
5234 for (tail = TYPE_FIELDS (constructor_type); tail;
5235 tail = TREE_CHAIN (tail))
5237 if (DECL_NAME (tail) == fieldname)
5238 break;
5241 if (tail == 0)
5242 error ("unknown field %qs specified in initializer",
5243 IDENTIFIER_POINTER (fieldname));
5244 else
5246 constructor_fields = tail;
5247 designator_depth++;
5248 designator_errorneous = 0;
5249 if (constructor_range_stack)
5250 push_range_stack (NULL_TREE);
5254 /* Add a new initializer to the tree of pending initializers. PURPOSE
5255 identifies the initializer, either array index or field in a structure.
5256 VALUE is the value of that index or field. */
5258 static void
5259 add_pending_init (tree purpose, tree value)
5261 struct init_node *p, **q, *r;
5263 q = &constructor_pending_elts;
5264 p = 0;
5266 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5268 while (*q != 0)
5270 p = *q;
5271 if (tree_int_cst_lt (purpose, p->purpose))
5272 q = &p->left;
5273 else if (tree_int_cst_lt (p->purpose, purpose))
5274 q = &p->right;
5275 else
5277 if (TREE_SIDE_EFFECTS (p->value))
5278 warning_init ("initialized field with side-effects overwritten");
5279 p->value = value;
5280 return;
5284 else
5286 tree bitpos;
5288 bitpos = bit_position (purpose);
5289 while (*q != NULL)
5291 p = *q;
5292 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5293 q = &p->left;
5294 else if (p->purpose != purpose)
5295 q = &p->right;
5296 else
5298 if (TREE_SIDE_EFFECTS (p->value))
5299 warning_init ("initialized field with side-effects overwritten");
5300 p->value = value;
5301 return;
5306 r = GGC_NEW (struct init_node);
5307 r->purpose = purpose;
5308 r->value = value;
5310 *q = r;
5311 r->parent = p;
5312 r->left = 0;
5313 r->right = 0;
5314 r->balance = 0;
5316 while (p)
5318 struct init_node *s;
5320 if (r == p->left)
5322 if (p->balance == 0)
5323 p->balance = -1;
5324 else if (p->balance < 0)
5326 if (r->balance < 0)
5328 /* L rotation. */
5329 p->left = r->right;
5330 if (p->left)
5331 p->left->parent = p;
5332 r->right = p;
5334 p->balance = 0;
5335 r->balance = 0;
5337 s = p->parent;
5338 p->parent = r;
5339 r->parent = s;
5340 if (s)
5342 if (s->left == p)
5343 s->left = r;
5344 else
5345 s->right = r;
5347 else
5348 constructor_pending_elts = r;
5350 else
5352 /* LR rotation. */
5353 struct init_node *t = r->right;
5355 r->right = t->left;
5356 if (r->right)
5357 r->right->parent = r;
5358 t->left = r;
5360 p->left = t->right;
5361 if (p->left)
5362 p->left->parent = p;
5363 t->right = p;
5365 p->balance = t->balance < 0;
5366 r->balance = -(t->balance > 0);
5367 t->balance = 0;
5369 s = p->parent;
5370 p->parent = t;
5371 r->parent = t;
5372 t->parent = s;
5373 if (s)
5375 if (s->left == p)
5376 s->left = t;
5377 else
5378 s->right = t;
5380 else
5381 constructor_pending_elts = t;
5383 break;
5385 else
5387 /* p->balance == +1; growth of left side balances the node. */
5388 p->balance = 0;
5389 break;
5392 else /* r == p->right */
5394 if (p->balance == 0)
5395 /* Growth propagation from right side. */
5396 p->balance++;
5397 else if (p->balance > 0)
5399 if (r->balance > 0)
5401 /* R rotation. */
5402 p->right = r->left;
5403 if (p->right)
5404 p->right->parent = p;
5405 r->left = p;
5407 p->balance = 0;
5408 r->balance = 0;
5410 s = p->parent;
5411 p->parent = r;
5412 r->parent = s;
5413 if (s)
5415 if (s->left == p)
5416 s->left = r;
5417 else
5418 s->right = r;
5420 else
5421 constructor_pending_elts = r;
5423 else /* r->balance == -1 */
5425 /* RL rotation */
5426 struct init_node *t = r->left;
5428 r->left = t->right;
5429 if (r->left)
5430 r->left->parent = r;
5431 t->right = r;
5433 p->right = t->left;
5434 if (p->right)
5435 p->right->parent = p;
5436 t->left = p;
5438 r->balance = (t->balance < 0);
5439 p->balance = -(t->balance > 0);
5440 t->balance = 0;
5442 s = p->parent;
5443 p->parent = t;
5444 r->parent = t;
5445 t->parent = s;
5446 if (s)
5448 if (s->left == p)
5449 s->left = t;
5450 else
5451 s->right = t;
5453 else
5454 constructor_pending_elts = t;
5456 break;
5458 else
5460 /* p->balance == -1; growth of right side balances the node. */
5461 p->balance = 0;
5462 break;
5466 r = p;
5467 p = p->parent;
5471 /* Build AVL tree from a sorted chain. */
5473 static void
5474 set_nonincremental_init (void)
5476 tree chain;
5478 if (TREE_CODE (constructor_type) != RECORD_TYPE
5479 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5480 return;
5482 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5483 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5484 constructor_elements = 0;
5485 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5487 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5488 /* Skip any nameless bit fields at the beginning. */
5489 while (constructor_unfilled_fields != 0
5490 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5491 && DECL_NAME (constructor_unfilled_fields) == 0)
5492 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5495 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5497 if (TYPE_DOMAIN (constructor_type))
5498 constructor_unfilled_index
5499 = convert (bitsizetype,
5500 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5501 else
5502 constructor_unfilled_index = bitsize_zero_node;
5504 constructor_incremental = 0;
5507 /* Build AVL tree from a string constant. */
5509 static void
5510 set_nonincremental_init_from_string (tree str)
5512 tree value, purpose, type;
5513 HOST_WIDE_INT val[2];
5514 const char *p, *end;
5515 int byte, wchar_bytes, charwidth, bitpos;
5517 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5519 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5520 == TYPE_PRECISION (char_type_node))
5521 wchar_bytes = 1;
5522 else
5524 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5525 == TYPE_PRECISION (wchar_type_node));
5526 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5528 charwidth = TYPE_PRECISION (char_type_node);
5529 type = TREE_TYPE (constructor_type);
5530 p = TREE_STRING_POINTER (str);
5531 end = p + TREE_STRING_LENGTH (str);
5533 for (purpose = bitsize_zero_node;
5534 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5535 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5537 if (wchar_bytes == 1)
5539 val[1] = (unsigned char) *p++;
5540 val[0] = 0;
5542 else
5544 val[0] = 0;
5545 val[1] = 0;
5546 for (byte = 0; byte < wchar_bytes; byte++)
5548 if (BYTES_BIG_ENDIAN)
5549 bitpos = (wchar_bytes - byte - 1) * charwidth;
5550 else
5551 bitpos = byte * charwidth;
5552 val[bitpos < HOST_BITS_PER_WIDE_INT]
5553 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5554 << (bitpos % HOST_BITS_PER_WIDE_INT);
5558 if (!TYPE_UNSIGNED (type))
5560 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5561 if (bitpos < HOST_BITS_PER_WIDE_INT)
5563 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5565 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5566 val[0] = -1;
5569 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5571 if (val[1] < 0)
5572 val[0] = -1;
5574 else if (val[0] & (((HOST_WIDE_INT) 1)
5575 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5576 val[0] |= ((HOST_WIDE_INT) -1)
5577 << (bitpos - HOST_BITS_PER_WIDE_INT);
5580 value = build_int_cst_wide (type, val[1], val[0]);
5581 add_pending_init (purpose, value);
5584 constructor_incremental = 0;
5587 /* Return value of FIELD in pending initializer or zero if the field was
5588 not initialized yet. */
5590 static tree
5591 find_init_member (tree field)
5593 struct init_node *p;
5595 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5597 if (constructor_incremental
5598 && tree_int_cst_lt (field, constructor_unfilled_index))
5599 set_nonincremental_init ();
5601 p = constructor_pending_elts;
5602 while (p)
5604 if (tree_int_cst_lt (field, p->purpose))
5605 p = p->left;
5606 else if (tree_int_cst_lt (p->purpose, field))
5607 p = p->right;
5608 else
5609 return p->value;
5612 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5614 tree bitpos = bit_position (field);
5616 if (constructor_incremental
5617 && (!constructor_unfilled_fields
5618 || tree_int_cst_lt (bitpos,
5619 bit_position (constructor_unfilled_fields))))
5620 set_nonincremental_init ();
5622 p = constructor_pending_elts;
5623 while (p)
5625 if (field == p->purpose)
5626 return p->value;
5627 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5628 p = p->left;
5629 else
5630 p = p->right;
5633 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5635 if (constructor_elements
5636 && TREE_PURPOSE (constructor_elements) == field)
5637 return TREE_VALUE (constructor_elements);
5639 return 0;
5642 /* "Output" the next constructor element.
5643 At top level, really output it to assembler code now.
5644 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5645 TYPE is the data type that the containing data type wants here.
5646 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5647 If VALUE is a string constant, STRICT_STRING is true if it is
5648 unparenthesized or we should not warn here for it being parenthesized.
5649 For other types of VALUE, STRICT_STRING is not used.
5651 PENDING if non-nil means output pending elements that belong
5652 right after this element. (PENDING is normally 1;
5653 it is 0 while outputting pending elements, to avoid recursion.) */
5655 static void
5656 output_init_element (tree value, bool strict_string, tree type, tree field,
5657 int pending)
5659 if (type == error_mark_node || value == error_mark_node)
5661 constructor_erroneous = 1;
5662 return;
5664 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5665 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5666 && !(TREE_CODE (value) == STRING_CST
5667 && TREE_CODE (type) == ARRAY_TYPE
5668 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5669 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5670 TYPE_MAIN_VARIANT (type))))
5671 value = default_conversion (value);
5673 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5674 && require_constant_value && !flag_isoc99 && pending)
5676 /* As an extension, allow initializing objects with static storage
5677 duration with compound literals (which are then treated just as
5678 the brace enclosed list they contain). */
5679 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5680 value = DECL_INITIAL (decl);
5683 if (value == error_mark_node)
5684 constructor_erroneous = 1;
5685 else if (!TREE_CONSTANT (value))
5686 constructor_constant = 0;
5687 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5688 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5689 || TREE_CODE (constructor_type) == UNION_TYPE)
5690 && DECL_C_BIT_FIELD (field)
5691 && TREE_CODE (value) != INTEGER_CST))
5692 constructor_simple = 0;
5694 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5696 if (require_constant_value)
5698 error_init ("initializer element is not constant");
5699 value = error_mark_node;
5701 else if (require_constant_elements)
5702 pedwarn ("initializer element is not computable at load time");
5705 /* If this field is empty (and not at the end of structure),
5706 don't do anything other than checking the initializer. */
5707 if (field
5708 && (TREE_TYPE (field) == error_mark_node
5709 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5710 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5711 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5712 || TREE_CHAIN (field)))))
5713 return;
5715 value = digest_init (type, value, strict_string, require_constant_value);
5716 if (value == error_mark_node)
5718 constructor_erroneous = 1;
5719 return;
5722 /* If this element doesn't come next in sequence,
5723 put it on constructor_pending_elts. */
5724 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5725 && (!constructor_incremental
5726 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5728 if (constructor_incremental
5729 && tree_int_cst_lt (field, constructor_unfilled_index))
5730 set_nonincremental_init ();
5732 add_pending_init (field, value);
5733 return;
5735 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5736 && (!constructor_incremental
5737 || field != constructor_unfilled_fields))
5739 /* We do this for records but not for unions. In a union,
5740 no matter which field is specified, it can be initialized
5741 right away since it starts at the beginning of the union. */
5742 if (constructor_incremental)
5744 if (!constructor_unfilled_fields)
5745 set_nonincremental_init ();
5746 else
5748 tree bitpos, unfillpos;
5750 bitpos = bit_position (field);
5751 unfillpos = bit_position (constructor_unfilled_fields);
5753 if (tree_int_cst_lt (bitpos, unfillpos))
5754 set_nonincremental_init ();
5758 add_pending_init (field, value);
5759 return;
5761 else if (TREE_CODE (constructor_type) == UNION_TYPE
5762 && constructor_elements)
5764 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5765 warning_init ("initialized field with side-effects overwritten");
5767 /* We can have just one union field set. */
5768 constructor_elements = 0;
5771 /* Otherwise, output this element either to
5772 constructor_elements or to the assembler file. */
5774 if (field && TREE_CODE (field) == INTEGER_CST)
5775 field = copy_node (field);
5776 constructor_elements
5777 = tree_cons (field, value, constructor_elements);
5779 /* Advance the variable that indicates sequential elements output. */
5780 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5781 constructor_unfilled_index
5782 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5783 bitsize_one_node);
5784 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5786 constructor_unfilled_fields
5787 = TREE_CHAIN (constructor_unfilled_fields);
5789 /* Skip any nameless bit fields. */
5790 while (constructor_unfilled_fields != 0
5791 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5792 && DECL_NAME (constructor_unfilled_fields) == 0)
5793 constructor_unfilled_fields =
5794 TREE_CHAIN (constructor_unfilled_fields);
5796 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5797 constructor_unfilled_fields = 0;
5799 /* Now output any pending elements which have become next. */
5800 if (pending)
5801 output_pending_init_elements (0);
5804 /* Output any pending elements which have become next.
5805 As we output elements, constructor_unfilled_{fields,index}
5806 advances, which may cause other elements to become next;
5807 if so, they too are output.
5809 If ALL is 0, we return when there are
5810 no more pending elements to output now.
5812 If ALL is 1, we output space as necessary so that
5813 we can output all the pending elements. */
5815 static void
5816 output_pending_init_elements (int all)
5818 struct init_node *elt = constructor_pending_elts;
5819 tree next;
5821 retry:
5823 /* Look through the whole pending tree.
5824 If we find an element that should be output now,
5825 output it. Otherwise, set NEXT to the element
5826 that comes first among those still pending. */
5828 next = 0;
5829 while (elt)
5831 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5833 if (tree_int_cst_equal (elt->purpose,
5834 constructor_unfilled_index))
5835 output_init_element (elt->value, true,
5836 TREE_TYPE (constructor_type),
5837 constructor_unfilled_index, 0);
5838 else if (tree_int_cst_lt (constructor_unfilled_index,
5839 elt->purpose))
5841 /* Advance to the next smaller node. */
5842 if (elt->left)
5843 elt = elt->left;
5844 else
5846 /* We have reached the smallest node bigger than the
5847 current unfilled index. Fill the space first. */
5848 next = elt->purpose;
5849 break;
5852 else
5854 /* Advance to the next bigger node. */
5855 if (elt->right)
5856 elt = elt->right;
5857 else
5859 /* We have reached the biggest node in a subtree. Find
5860 the parent of it, which is the next bigger node. */
5861 while (elt->parent && elt->parent->right == elt)
5862 elt = elt->parent;
5863 elt = elt->parent;
5864 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5865 elt->purpose))
5867 next = elt->purpose;
5868 break;
5873 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5874 || TREE_CODE (constructor_type) == UNION_TYPE)
5876 tree ctor_unfilled_bitpos, elt_bitpos;
5878 /* If the current record is complete we are done. */
5879 if (constructor_unfilled_fields == 0)
5880 break;
5882 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5883 elt_bitpos = bit_position (elt->purpose);
5884 /* We can't compare fields here because there might be empty
5885 fields in between. */
5886 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5888 constructor_unfilled_fields = elt->purpose;
5889 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5890 elt->purpose, 0);
5892 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5894 /* Advance to the next smaller node. */
5895 if (elt->left)
5896 elt = elt->left;
5897 else
5899 /* We have reached the smallest node bigger than the
5900 current unfilled field. Fill the space first. */
5901 next = elt->purpose;
5902 break;
5905 else
5907 /* Advance to the next bigger node. */
5908 if (elt->right)
5909 elt = elt->right;
5910 else
5912 /* We have reached the biggest node in a subtree. Find
5913 the parent of it, which is the next bigger node. */
5914 while (elt->parent && elt->parent->right == elt)
5915 elt = elt->parent;
5916 elt = elt->parent;
5917 if (elt
5918 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5919 bit_position (elt->purpose))))
5921 next = elt->purpose;
5922 break;
5929 /* Ordinarily return, but not if we want to output all
5930 and there are elements left. */
5931 if (!(all && next != 0))
5932 return;
5934 /* If it's not incremental, just skip over the gap, so that after
5935 jumping to retry we will output the next successive element. */
5936 if (TREE_CODE (constructor_type) == RECORD_TYPE
5937 || TREE_CODE (constructor_type) == UNION_TYPE)
5938 constructor_unfilled_fields = next;
5939 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5940 constructor_unfilled_index = next;
5942 /* ELT now points to the node in the pending tree with the next
5943 initializer to output. */
5944 goto retry;
5947 /* Add one non-braced element to the current constructor level.
5948 This adjusts the current position within the constructor's type.
5949 This may also start or terminate implicit levels
5950 to handle a partly-braced initializer.
5952 Once this has found the correct level for the new element,
5953 it calls output_init_element. */
5955 void
5956 process_init_element (struct c_expr value)
5958 tree orig_value = value.value;
5959 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5960 bool strict_string = value.original_code == STRING_CST;
5962 designator_depth = 0;
5963 designator_errorneous = 0;
5965 /* Handle superfluous braces around string cst as in
5966 char x[] = {"foo"}; */
5967 if (string_flag
5968 && constructor_type
5969 && TREE_CODE (constructor_type) == ARRAY_TYPE
5970 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5971 && integer_zerop (constructor_unfilled_index))
5973 if (constructor_stack->replacement_value.value)
5974 error_init ("excess elements in char array initializer");
5975 constructor_stack->replacement_value = value;
5976 return;
5979 if (constructor_stack->replacement_value.value != 0)
5981 error_init ("excess elements in struct initializer");
5982 return;
5985 /* Ignore elements of a brace group if it is entirely superfluous
5986 and has already been diagnosed. */
5987 if (constructor_type == 0)
5988 return;
5990 /* If we've exhausted any levels that didn't have braces,
5991 pop them now. */
5992 while (constructor_stack->implicit)
5994 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5995 || TREE_CODE (constructor_type) == UNION_TYPE)
5996 && constructor_fields == 0)
5997 process_init_element (pop_init_level (1));
5998 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5999 && (constructor_max_index == 0
6000 || tree_int_cst_lt (constructor_max_index,
6001 constructor_index)))
6002 process_init_element (pop_init_level (1));
6003 else
6004 break;
6007 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6008 if (constructor_range_stack)
6010 /* If value is a compound literal and we'll be just using its
6011 content, don't put it into a SAVE_EXPR. */
6012 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6013 || !require_constant_value
6014 || flag_isoc99)
6015 value.value = save_expr (value.value);
6018 while (1)
6020 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6022 tree fieldtype;
6023 enum tree_code fieldcode;
6025 if (constructor_fields == 0)
6027 pedwarn_init ("excess elements in struct initializer");
6028 break;
6031 fieldtype = TREE_TYPE (constructor_fields);
6032 if (fieldtype != error_mark_node)
6033 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6034 fieldcode = TREE_CODE (fieldtype);
6036 /* Error for non-static initialization of a flexible array member. */
6037 if (fieldcode == ARRAY_TYPE
6038 && !require_constant_value
6039 && TYPE_SIZE (fieldtype) == NULL_TREE
6040 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6042 error_init ("non-static initialization of a flexible array member");
6043 break;
6046 /* Accept a string constant to initialize a subarray. */
6047 if (value.value != 0
6048 && fieldcode == ARRAY_TYPE
6049 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6050 && string_flag)
6051 value.value = orig_value;
6052 /* Otherwise, if we have come to a subaggregate,
6053 and we don't have an element of its type, push into it. */
6054 else if (value.value != 0 && !constructor_no_implicit
6055 && value.value != error_mark_node
6056 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6057 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6058 || fieldcode == UNION_TYPE))
6060 push_init_level (1);
6061 continue;
6064 if (value.value)
6066 push_member_name (constructor_fields);
6067 output_init_element (value.value, strict_string,
6068 fieldtype, constructor_fields, 1);
6069 RESTORE_SPELLING_DEPTH (constructor_depth);
6071 else
6072 /* Do the bookkeeping for an element that was
6073 directly output as a constructor. */
6075 /* For a record, keep track of end position of last field. */
6076 if (DECL_SIZE (constructor_fields))
6077 constructor_bit_index
6078 = size_binop (PLUS_EXPR,
6079 bit_position (constructor_fields),
6080 DECL_SIZE (constructor_fields));
6082 /* If the current field was the first one not yet written out,
6083 it isn't now, so update. */
6084 if (constructor_unfilled_fields == constructor_fields)
6086 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6087 /* Skip any nameless bit fields. */
6088 while (constructor_unfilled_fields != 0
6089 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6090 && DECL_NAME (constructor_unfilled_fields) == 0)
6091 constructor_unfilled_fields =
6092 TREE_CHAIN (constructor_unfilled_fields);
6096 constructor_fields = TREE_CHAIN (constructor_fields);
6097 /* Skip any nameless bit fields at the beginning. */
6098 while (constructor_fields != 0
6099 && DECL_C_BIT_FIELD (constructor_fields)
6100 && DECL_NAME (constructor_fields) == 0)
6101 constructor_fields = TREE_CHAIN (constructor_fields);
6103 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6105 tree fieldtype;
6106 enum tree_code fieldcode;
6108 if (constructor_fields == 0)
6110 pedwarn_init ("excess elements in union initializer");
6111 break;
6114 fieldtype = TREE_TYPE (constructor_fields);
6115 if (fieldtype != error_mark_node)
6116 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6117 fieldcode = TREE_CODE (fieldtype);
6119 /* Warn that traditional C rejects initialization of unions.
6120 We skip the warning if the value is zero. This is done
6121 under the assumption that the zero initializer in user
6122 code appears conditioned on e.g. __STDC__ to avoid
6123 "missing initializer" warnings and relies on default
6124 initialization to zero in the traditional C case.
6125 We also skip the warning if the initializer is designated,
6126 again on the assumption that this must be conditional on
6127 __STDC__ anyway (and we've already complained about the
6128 member-designator already). */
6129 if (warn_traditional && !in_system_header && !constructor_designated
6130 && !(value.value && (integer_zerop (value.value)
6131 || real_zerop (value.value))))
6132 warning ("traditional C rejects initialization of unions");
6134 /* Accept a string constant to initialize a subarray. */
6135 if (value.value != 0
6136 && fieldcode == ARRAY_TYPE
6137 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6138 && string_flag)
6139 value.value = orig_value;
6140 /* Otherwise, if we have come to a subaggregate,
6141 and we don't have an element of its type, push into it. */
6142 else if (value.value != 0 && !constructor_no_implicit
6143 && value.value != error_mark_node
6144 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6145 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6146 || fieldcode == UNION_TYPE))
6148 push_init_level (1);
6149 continue;
6152 if (value.value)
6154 push_member_name (constructor_fields);
6155 output_init_element (value.value, strict_string,
6156 fieldtype, constructor_fields, 1);
6157 RESTORE_SPELLING_DEPTH (constructor_depth);
6159 else
6160 /* Do the bookkeeping for an element that was
6161 directly output as a constructor. */
6163 constructor_bit_index = DECL_SIZE (constructor_fields);
6164 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6167 constructor_fields = 0;
6169 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6171 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6172 enum tree_code eltcode = TREE_CODE (elttype);
6174 /* Accept a string constant to initialize a subarray. */
6175 if (value.value != 0
6176 && eltcode == ARRAY_TYPE
6177 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6178 && string_flag)
6179 value.value = orig_value;
6180 /* Otherwise, if we have come to a subaggregate,
6181 and we don't have an element of its type, push into it. */
6182 else if (value.value != 0 && !constructor_no_implicit
6183 && value.value != error_mark_node
6184 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6185 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6186 || eltcode == UNION_TYPE))
6188 push_init_level (1);
6189 continue;
6192 if (constructor_max_index != 0
6193 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6194 || integer_all_onesp (constructor_max_index)))
6196 pedwarn_init ("excess elements in array initializer");
6197 break;
6200 /* Now output the actual element. */
6201 if (value.value)
6203 push_array_bounds (tree_low_cst (constructor_index, 0));
6204 output_init_element (value.value, strict_string,
6205 elttype, constructor_index, 1);
6206 RESTORE_SPELLING_DEPTH (constructor_depth);
6209 constructor_index
6210 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6212 if (!value.value)
6213 /* If we are doing the bookkeeping for an element that was
6214 directly output as a constructor, we must update
6215 constructor_unfilled_index. */
6216 constructor_unfilled_index = constructor_index;
6218 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6220 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6222 /* Do a basic check of initializer size. Note that vectors
6223 always have a fixed size derived from their type. */
6224 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6226 pedwarn_init ("excess elements in vector initializer");
6227 break;
6230 /* Now output the actual element. */
6231 if (value.value)
6232 output_init_element (value.value, strict_string,
6233 elttype, constructor_index, 1);
6235 constructor_index
6236 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6238 if (!value.value)
6239 /* If we are doing the bookkeeping for an element that was
6240 directly output as a constructor, we must update
6241 constructor_unfilled_index. */
6242 constructor_unfilled_index = constructor_index;
6245 /* Handle the sole element allowed in a braced initializer
6246 for a scalar variable. */
6247 else if (constructor_fields == 0)
6249 pedwarn_init ("excess elements in scalar initializer");
6250 break;
6252 else
6254 if (value.value)
6255 output_init_element (value.value, strict_string,
6256 constructor_type, NULL_TREE, 1);
6257 constructor_fields = 0;
6260 /* Handle range initializers either at this level or anywhere higher
6261 in the designator stack. */
6262 if (constructor_range_stack)
6264 struct constructor_range_stack *p, *range_stack;
6265 int finish = 0;
6267 range_stack = constructor_range_stack;
6268 constructor_range_stack = 0;
6269 while (constructor_stack != range_stack->stack)
6271 gcc_assert (constructor_stack->implicit);
6272 process_init_element (pop_init_level (1));
6274 for (p = range_stack;
6275 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6276 p = p->prev)
6278 gcc_assert (constructor_stack->implicit);
6279 process_init_element (pop_init_level (1));
6282 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6283 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6284 finish = 1;
6286 while (1)
6288 constructor_index = p->index;
6289 constructor_fields = p->fields;
6290 if (finish && p->range_end && p->index == p->range_start)
6292 finish = 0;
6293 p->prev = 0;
6295 p = p->next;
6296 if (!p)
6297 break;
6298 push_init_level (2);
6299 p->stack = constructor_stack;
6300 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6301 p->index = p->range_start;
6304 if (!finish)
6305 constructor_range_stack = range_stack;
6306 continue;
6309 break;
6312 constructor_range_stack = 0;
6315 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6316 (guaranteed to be 'volatile' or null) and ARGS (represented using
6317 an ASM_EXPR node). */
6318 tree
6319 build_asm_stmt (tree cv_qualifier, tree args)
6321 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6322 ASM_VOLATILE_P (args) = 1;
6323 return add_stmt (args);
6326 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6327 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6328 SIMPLE indicates whether there was anything at all after the
6329 string in the asm expression -- asm("blah") and asm("blah" : )
6330 are subtly different. We use a ASM_EXPR node to represent this. */
6331 tree
6332 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6333 bool simple)
6335 tree tail;
6336 tree args;
6337 int i;
6338 const char *constraint;
6339 bool allows_mem, allows_reg, is_inout;
6340 int ninputs;
6341 int noutputs;
6343 ninputs = list_length (inputs);
6344 noutputs = list_length (outputs);
6346 /* Remove output conversions that change the type but not the mode. */
6347 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6349 tree output = TREE_VALUE (tail);
6350 STRIP_NOPS (output);
6351 TREE_VALUE (tail) = output;
6352 lvalue_or_else (output, lv_asm);
6354 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6356 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
6357 &allows_mem, &allows_reg, &is_inout))
6359 /* By marking this operand as erroneous, we will not try
6360 to process this operand again in expand_asm_operands. */
6361 TREE_VALUE (tail) = error_mark_node;
6362 continue;
6365 /* If the operand is a DECL that is going to end up in
6366 memory, assume it is addressable. This is a bit more
6367 conservative than it would ideally be; the exact test is
6368 buried deep in expand_asm_operands and depends on the
6369 DECL_RTL for the OPERAND -- which we don't have at this
6370 point. */
6371 if (!allows_reg && DECL_P (output))
6372 c_mark_addressable (output);
6375 /* Perform default conversions on array and function inputs.
6376 Don't do this for other types as it would screw up operands
6377 expected to be in memory. */
6378 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6379 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6381 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6383 /* Simple asm statements are treated as volatile. */
6384 if (simple)
6386 ASM_VOLATILE_P (args) = 1;
6387 ASM_INPUT_P (args) = 1;
6389 return args;
6392 /* Generate a goto statement to LABEL. */
6394 tree
6395 c_finish_goto_label (tree label)
6397 tree decl = lookup_label (label);
6398 if (!decl)
6399 return NULL_TREE;
6401 TREE_USED (decl) = 1;
6402 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6405 /* Generate a computed goto statement to EXPR. */
6407 tree
6408 c_finish_goto_ptr (tree expr)
6410 if (pedantic)
6411 pedwarn ("ISO C forbids %<goto *expr;%>");
6412 expr = convert (ptr_type_node, expr);
6413 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6416 /* Generate a C `return' statement. RETVAL is the expression for what
6417 to return, or a null pointer for `return;' with no value. */
6419 tree
6420 c_finish_return (tree retval)
6422 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6424 if (TREE_THIS_VOLATILE (current_function_decl))
6425 warning ("function declared %<noreturn%> has a %<return%> statement");
6427 if (!retval)
6429 current_function_returns_null = 1;
6430 if ((warn_return_type || flag_isoc99)
6431 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6432 pedwarn_c99 ("%<return%> with no value, in "
6433 "function returning non-void");
6435 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6437 current_function_returns_null = 1;
6438 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6439 pedwarn ("%<return%> with a value, in function returning void");
6441 else
6443 tree t = convert_for_assignment (valtype, retval, ic_return,
6444 NULL_TREE, NULL_TREE, 0);
6445 tree res = DECL_RESULT (current_function_decl);
6446 tree inner;
6448 current_function_returns_value = 1;
6449 if (t == error_mark_node)
6450 return NULL_TREE;
6452 inner = t = convert (TREE_TYPE (res), t);
6454 /* Strip any conversions, additions, and subtractions, and see if
6455 we are returning the address of a local variable. Warn if so. */
6456 while (1)
6458 switch (TREE_CODE (inner))
6460 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6461 case PLUS_EXPR:
6462 inner = TREE_OPERAND (inner, 0);
6463 continue;
6465 case MINUS_EXPR:
6466 /* If the second operand of the MINUS_EXPR has a pointer
6467 type (or is converted from it), this may be valid, so
6468 don't give a warning. */
6470 tree op1 = TREE_OPERAND (inner, 1);
6472 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6473 && (TREE_CODE (op1) == NOP_EXPR
6474 || TREE_CODE (op1) == NON_LVALUE_EXPR
6475 || TREE_CODE (op1) == CONVERT_EXPR))
6476 op1 = TREE_OPERAND (op1, 0);
6478 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6479 break;
6481 inner = TREE_OPERAND (inner, 0);
6482 continue;
6485 case ADDR_EXPR:
6486 inner = TREE_OPERAND (inner, 0);
6488 while (REFERENCE_CLASS_P (inner)
6489 && TREE_CODE (inner) != INDIRECT_REF)
6490 inner = TREE_OPERAND (inner, 0);
6492 if (DECL_P (inner)
6493 && !DECL_EXTERNAL (inner)
6494 && !TREE_STATIC (inner)
6495 && DECL_CONTEXT (inner) == current_function_decl)
6496 warning ("function returns address of local variable");
6497 break;
6499 default:
6500 break;
6503 break;
6506 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6509 return add_stmt (build_stmt (RETURN_EXPR, retval));
6512 struct c_switch {
6513 /* The SWITCH_STMT being built. */
6514 tree switch_stmt;
6516 /* The original type of the testing expression, i.e. before the
6517 default conversion is applied. */
6518 tree orig_type;
6520 /* A splay-tree mapping the low element of a case range to the high
6521 element, or NULL_TREE if there is no high element. Used to
6522 determine whether or not a new case label duplicates an old case
6523 label. We need a tree, rather than simply a hash table, because
6524 of the GNU case range extension. */
6525 splay_tree cases;
6527 /* The next node on the stack. */
6528 struct c_switch *next;
6531 /* A stack of the currently active switch statements. The innermost
6532 switch statement is on the top of the stack. There is no need to
6533 mark the stack for garbage collection because it is only active
6534 during the processing of the body of a function, and we never
6535 collect at that point. */
6537 struct c_switch *c_switch_stack;
6539 /* Start a C switch statement, testing expression EXP. Return the new
6540 SWITCH_STMT. */
6542 tree
6543 c_start_case (tree exp)
6545 enum tree_code code;
6546 tree type, orig_type = error_mark_node;
6547 struct c_switch *cs;
6549 if (exp != error_mark_node)
6551 code = TREE_CODE (TREE_TYPE (exp));
6552 orig_type = TREE_TYPE (exp);
6554 if (!INTEGRAL_TYPE_P (orig_type)
6555 && code != ERROR_MARK)
6557 error ("switch quantity not an integer");
6558 exp = integer_zero_node;
6560 else
6562 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6564 if (warn_traditional && !in_system_header
6565 && (type == long_integer_type_node
6566 || type == long_unsigned_type_node))
6567 warning ("%<long%> switch expression not converted to "
6568 "%<int%> in ISO C");
6570 exp = default_conversion (exp);
6571 type = TREE_TYPE (exp);
6575 /* Add this new SWITCH_STMT to the stack. */
6576 cs = XNEW (struct c_switch);
6577 cs->switch_stmt = build_stmt ((enum tree_code) SWITCH_STMT, exp, NULL_TREE,
6578 orig_type);
6579 cs->orig_type = orig_type;
6580 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6581 cs->next = c_switch_stack;
6582 c_switch_stack = cs;
6584 return add_stmt (cs->switch_stmt);
6587 /* Process a case label. */
6589 tree
6590 do_case (tree low_value, tree high_value)
6592 tree label = NULL_TREE;
6594 if (c_switch_stack)
6596 label = c_add_case_label (c_switch_stack->cases,
6597 SWITCH_COND (c_switch_stack->switch_stmt),
6598 c_switch_stack->orig_type,
6599 low_value, high_value);
6600 if (label == error_mark_node)
6601 label = NULL_TREE;
6603 else if (low_value)
6604 error ("case label not within a switch statement");
6605 else
6606 error ("%<default%> label not within a switch statement");
6608 return label;
6611 /* Finish the switch statement. */
6613 void
6614 c_finish_case (tree body)
6616 struct c_switch *cs = c_switch_stack;
6618 SWITCH_BODY (cs->switch_stmt) = body;
6620 /* Emit warnings as needed. */
6621 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6623 /* Pop the stack. */
6624 c_switch_stack = cs->next;
6625 splay_tree_delete (cs->cases);
6626 XDELETE (cs);
6629 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6630 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6631 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6632 statement, and was not surrounded with parenthesis. */
6634 void
6635 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6636 tree else_block, bool nested_if)
6638 tree stmt;
6640 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6641 if (warn_parentheses && nested_if && else_block == NULL)
6643 tree inner_if = then_block;
6645 /* We know from the grammar productions that there is an IF nested
6646 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6647 it might not be exactly THEN_BLOCK, but should be the last
6648 non-container statement within. */
6649 while (1)
6650 switch (TREE_CODE (inner_if))
6652 case COND_EXPR:
6653 goto found;
6654 case BIND_EXPR:
6655 inner_if = BIND_EXPR_BODY (inner_if);
6656 break;
6657 case STATEMENT_LIST:
6658 inner_if = expr_last (then_block);
6659 break;
6660 case TRY_FINALLY_EXPR:
6661 case TRY_CATCH_EXPR:
6662 inner_if = TREE_OPERAND (inner_if, 0);
6663 break;
6664 default:
6665 gcc_unreachable ();
6667 found:
6669 if (COND_EXPR_ELSE (inner_if))
6670 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6671 &if_locus);
6674 /* Diagnose ";" via the special empty statement node that we create. */
6675 if (extra_warnings)
6677 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6679 if (!else_block)
6680 warning ("%Hempty body in an if-statement",
6681 EXPR_LOCUS (then_block));
6682 then_block = alloc_stmt_list ();
6684 if (else_block
6685 && TREE_CODE (else_block) == NOP_EXPR
6686 && !TREE_TYPE (else_block))
6688 warning ("%Hempty body in an else-statement",
6689 EXPR_LOCUS (else_block));
6690 else_block = alloc_stmt_list ();
6694 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6695 SET_EXPR_LOCATION (stmt, if_locus);
6696 add_stmt (stmt);
6699 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6700 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6701 is false for DO loops. INCR is the FOR increment expression. BODY is
6702 the statement controlled by the loop. BLAB is the break label. CLAB is
6703 the continue label. Everything is allowed to be NULL. */
6705 void
6706 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6707 tree blab, tree clab, bool cond_is_first)
6709 tree entry = NULL, exit = NULL, t;
6711 /* Detect do { ... } while (0) and don't generate loop construct. */
6712 if (cond && !cond_is_first && integer_zerop (cond))
6713 cond = NULL;
6714 if (cond_is_first || cond)
6716 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6718 /* If we have an exit condition, then we build an IF with gotos either
6719 out of the loop, or to the top of it. If there's no exit condition,
6720 then we just build a jump back to the top. */
6721 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6723 if (cond)
6725 /* Canonicalize the loop condition to the end. This means
6726 generating a branch to the loop condition. Reuse the
6727 continue label, if possible. */
6728 if (cond_is_first)
6730 if (incr || !clab)
6732 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6733 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6735 else
6736 t = build1 (GOTO_EXPR, void_type_node, clab);
6737 SET_EXPR_LOCATION (t, start_locus);
6738 add_stmt (t);
6741 t = build_and_jump (&blab);
6742 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6743 exit = fold (exit);
6744 if (cond_is_first)
6745 SET_EXPR_LOCATION (exit, start_locus);
6746 else
6747 SET_EXPR_LOCATION (exit, input_location);
6750 add_stmt (top);
6753 if (body)
6754 add_stmt (body);
6755 if (clab)
6756 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6757 if (incr)
6758 add_stmt (incr);
6759 if (entry)
6760 add_stmt (entry);
6761 if (exit)
6762 add_stmt (exit);
6763 if (blab)
6764 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6767 tree
6768 c_finish_bc_stmt (tree *label_p, bool is_break)
6770 tree label = *label_p;
6772 if (!label)
6773 *label_p = label = create_artificial_label ();
6774 else if (TREE_CODE (label) != LABEL_DECL)
6776 if (is_break)
6777 error ("break statement not within loop or switch");
6778 else
6779 error ("continue statement not within a loop");
6780 return NULL_TREE;
6783 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6786 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6788 static void
6789 emit_side_effect_warnings (tree expr)
6791 if (expr == error_mark_node)
6793 else if (!TREE_SIDE_EFFECTS (expr))
6795 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6796 warning ("%Hstatement with no effect",
6797 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6799 else if (warn_unused_value)
6800 warn_if_unused_value (expr, input_location);
6803 /* Process an expression as if it were a complete statement. Emit
6804 diagnostics, but do not call ADD_STMT. */
6806 tree
6807 c_process_expr_stmt (tree expr)
6809 if (!expr)
6810 return NULL_TREE;
6812 /* Do default conversion if safe and possibly important,
6813 in case within ({...}). */
6814 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6815 && (flag_isoc99 || lvalue_p (expr)))
6816 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6817 expr = default_conversion (expr);
6819 if (warn_sequence_point)
6820 verify_sequence_points (expr);
6822 if (TREE_TYPE (expr) != error_mark_node
6823 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6824 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6825 error ("expression statement has incomplete type");
6827 /* If we're not processing a statement expression, warn about unused values.
6828 Warnings for statement expressions will be emitted later, once we figure
6829 out which is the result. */
6830 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6831 && (extra_warnings || warn_unused_value))
6832 emit_side_effect_warnings (expr);
6834 /* If the expression is not of a type to which we cannot assign a line
6835 number, wrap the thing in a no-op NOP_EXPR. */
6836 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6837 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6839 if (EXPR_P (expr))
6840 SET_EXPR_LOCATION (expr, input_location);
6842 return expr;
6845 /* Emit an expression as a statement. */
6847 tree
6848 c_finish_expr_stmt (tree expr)
6850 if (expr)
6851 return add_stmt (c_process_expr_stmt (expr));
6852 else
6853 return NULL;
6856 /* Do the opposite and emit a statement as an expression. To begin,
6857 create a new binding level and return it. */
6859 tree
6860 c_begin_stmt_expr (void)
6862 tree ret;
6864 /* We must force a BLOCK for this level so that, if it is not expanded
6865 later, there is a way to turn off the entire subtree of blocks that
6866 are contained in it. */
6867 keep_next_level ();
6868 ret = c_begin_compound_stmt (true);
6870 /* Mark the current statement list as belonging to a statement list. */
6871 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6873 return ret;
6876 tree
6877 c_finish_stmt_expr (tree body)
6879 tree last, type, tmp, val;
6880 tree *last_p;
6882 body = c_end_compound_stmt (body, true);
6884 /* Locate the last statement in BODY. See c_end_compound_stmt
6885 about always returning a BIND_EXPR. */
6886 last_p = &BIND_EXPR_BODY (body);
6887 last = BIND_EXPR_BODY (body);
6889 continue_searching:
6890 if (TREE_CODE (last) == STATEMENT_LIST)
6892 tree_stmt_iterator i;
6894 /* This can happen with degenerate cases like ({ }). No value. */
6895 if (!TREE_SIDE_EFFECTS (last))
6896 return body;
6898 /* If we're supposed to generate side effects warnings, process
6899 all of the statements except the last. */
6900 if (extra_warnings || warn_unused_value)
6902 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6903 emit_side_effect_warnings (tsi_stmt (i));
6905 else
6906 i = tsi_last (last);
6907 last_p = tsi_stmt_ptr (i);
6908 last = *last_p;
6911 /* If the end of the list is exception related, then the list was split
6912 by a call to push_cleanup. Continue searching. */
6913 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6914 || TREE_CODE (last) == TRY_CATCH_EXPR)
6916 last_p = &TREE_OPERAND (last, 0);
6917 last = *last_p;
6918 goto continue_searching;
6921 /* In the case that the BIND_EXPR is not necessary, return the
6922 expression out from inside it. */
6923 if (last == error_mark_node
6924 || (last == BIND_EXPR_BODY (body)
6925 && BIND_EXPR_VARS (body) == NULL))
6926 return last;
6928 /* Extract the type of said expression. */
6929 type = TREE_TYPE (last);
6931 /* If we're not returning a value at all, then the BIND_EXPR that
6932 we already have is a fine expression to return. */
6933 if (!type || VOID_TYPE_P (type))
6934 return body;
6936 /* Now that we've located the expression containing the value, it seems
6937 silly to make voidify_wrapper_expr repeat the process. Create a
6938 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6939 tmp = create_tmp_var_raw (type, NULL);
6941 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6942 tree_expr_nonnegative_p giving up immediately. */
6943 val = last;
6944 if (TREE_CODE (val) == NOP_EXPR
6945 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
6946 val = TREE_OPERAND (val, 0);
6948 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
6949 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
6951 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
6954 /* Begin and end compound statements. This is as simple as pushing
6955 and popping new statement lists from the tree. */
6957 tree
6958 c_begin_compound_stmt (bool do_scope)
6960 tree stmt = push_stmt_list ();
6961 if (do_scope)
6962 push_scope ();
6963 return stmt;
6966 tree
6967 c_end_compound_stmt (tree stmt, bool do_scope)
6969 tree block = NULL;
6971 if (do_scope)
6973 if (c_dialect_objc ())
6974 objc_clear_super_receiver ();
6975 block = pop_scope ();
6978 stmt = pop_stmt_list (stmt);
6979 stmt = c_build_bind_expr (block, stmt);
6981 /* If this compound statement is nested immediately inside a statement
6982 expression, then force a BIND_EXPR to be created. Otherwise we'll
6983 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6984 STATEMENT_LISTs merge, and thus we can lose track of what statement
6985 was really last. */
6986 if (cur_stmt_list
6987 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6988 && TREE_CODE (stmt) != BIND_EXPR)
6990 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
6991 TREE_SIDE_EFFECTS (stmt) = 1;
6994 return stmt;
6997 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
6998 when the current scope is exited. EH_ONLY is true when this is not
6999 meant to apply to normal control flow transfer. */
7001 void
7002 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7004 enum tree_code code;
7005 tree stmt, list;
7006 bool stmt_expr;
7008 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7009 stmt = build_stmt (code, NULL, cleanup);
7010 add_stmt (stmt);
7011 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7012 list = push_stmt_list ();
7013 TREE_OPERAND (stmt, 0) = list;
7014 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7017 /* Build a binary-operation expression without default conversions.
7018 CODE is the kind of expression to build.
7019 This function differs from `build' in several ways:
7020 the data type of the result is computed and recorded in it,
7021 warnings are generated if arg data types are invalid,
7022 special handling for addition and subtraction of pointers is known,
7023 and some optimization is done (operations on narrow ints
7024 are done in the narrower type when that gives the same result).
7025 Constant folding is also done before the result is returned.
7027 Note that the operands will never have enumeral types, or function
7028 or array types, because either they will have the default conversions
7029 performed or they have both just been converted to some other type in which
7030 the arithmetic is to be done. */
7032 tree
7033 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7034 int convert_p)
7036 tree type0, type1;
7037 enum tree_code code0, code1;
7038 tree op0, op1;
7040 /* Expression code to give to the expression when it is built.
7041 Normally this is CODE, which is what the caller asked for,
7042 but in some special cases we change it. */
7043 enum tree_code resultcode = code;
7045 /* Data type in which the computation is to be performed.
7046 In the simplest cases this is the common type of the arguments. */
7047 tree result_type = NULL;
7049 /* Nonzero means operands have already been type-converted
7050 in whatever way is necessary.
7051 Zero means they need to be converted to RESULT_TYPE. */
7052 int converted = 0;
7054 /* Nonzero means create the expression with this type, rather than
7055 RESULT_TYPE. */
7056 tree build_type = 0;
7058 /* Nonzero means after finally constructing the expression
7059 convert it to this type. */
7060 tree final_type = 0;
7062 /* Nonzero if this is an operation like MIN or MAX which can
7063 safely be computed in short if both args are promoted shorts.
7064 Also implies COMMON.
7065 -1 indicates a bitwise operation; this makes a difference
7066 in the exact conditions for when it is safe to do the operation
7067 in a narrower mode. */
7068 int shorten = 0;
7070 /* Nonzero if this is a comparison operation;
7071 if both args are promoted shorts, compare the original shorts.
7072 Also implies COMMON. */
7073 int short_compare = 0;
7075 /* Nonzero if this is a right-shift operation, which can be computed on the
7076 original short and then promoted if the operand is a promoted short. */
7077 int short_shift = 0;
7079 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7080 int common = 0;
7082 if (convert_p)
7084 op0 = default_conversion (orig_op0);
7085 op1 = default_conversion (orig_op1);
7087 else
7089 op0 = orig_op0;
7090 op1 = orig_op1;
7093 type0 = TREE_TYPE (op0);
7094 type1 = TREE_TYPE (op1);
7096 /* The expression codes of the data types of the arguments tell us
7097 whether the arguments are integers, floating, pointers, etc. */
7098 code0 = TREE_CODE (type0);
7099 code1 = TREE_CODE (type1);
7101 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7102 STRIP_TYPE_NOPS (op0);
7103 STRIP_TYPE_NOPS (op1);
7105 /* If an error was already reported for one of the arguments,
7106 avoid reporting another error. */
7108 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7109 return error_mark_node;
7111 switch (code)
7113 case PLUS_EXPR:
7114 /* Handle the pointer + int case. */
7115 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7116 return pointer_int_sum (PLUS_EXPR, op0, op1);
7117 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7118 return pointer_int_sum (PLUS_EXPR, op1, op0);
7119 else
7120 common = 1;
7121 break;
7123 case MINUS_EXPR:
7124 /* Subtraction of two similar pointers.
7125 We must subtract them as integers, then divide by object size. */
7126 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7127 && comp_target_types (type0, type1, 1))
7128 return pointer_diff (op0, op1);
7129 /* Handle pointer minus int. Just like pointer plus int. */
7130 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7131 return pointer_int_sum (MINUS_EXPR, op0, op1);
7132 else
7133 common = 1;
7134 break;
7136 case MULT_EXPR:
7137 common = 1;
7138 break;
7140 case TRUNC_DIV_EXPR:
7141 case CEIL_DIV_EXPR:
7142 case FLOOR_DIV_EXPR:
7143 case ROUND_DIV_EXPR:
7144 case EXACT_DIV_EXPR:
7145 /* Floating point division by zero is a legitimate way to obtain
7146 infinities and NaNs. */
7147 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7148 warning ("division by zero");
7150 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7151 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7152 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7153 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7155 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7156 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7157 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7158 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7160 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7161 resultcode = RDIV_EXPR;
7162 else
7163 /* Although it would be tempting to shorten always here, that
7164 loses on some targets, since the modulo instruction is
7165 undefined if the quotient can't be represented in the
7166 computation mode. We shorten only if unsigned or if
7167 dividing by something we know != -1. */
7168 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7169 || (TREE_CODE (op1) == INTEGER_CST
7170 && !integer_all_onesp (op1)));
7171 common = 1;
7173 break;
7175 case BIT_AND_EXPR:
7176 case BIT_IOR_EXPR:
7177 case BIT_XOR_EXPR:
7178 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7179 shorten = -1;
7180 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7181 common = 1;
7182 break;
7184 case TRUNC_MOD_EXPR:
7185 case FLOOR_MOD_EXPR:
7186 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7187 warning ("division by zero");
7189 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7191 /* Although it would be tempting to shorten always here, that loses
7192 on some targets, since the modulo instruction is undefined if the
7193 quotient can't be represented in the computation mode. We shorten
7194 only if unsigned or if dividing by something we know != -1. */
7195 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7196 || (TREE_CODE (op1) == INTEGER_CST
7197 && !integer_all_onesp (op1)));
7198 common = 1;
7200 break;
7202 case TRUTH_ANDIF_EXPR:
7203 case TRUTH_ORIF_EXPR:
7204 case TRUTH_AND_EXPR:
7205 case TRUTH_OR_EXPR:
7206 case TRUTH_XOR_EXPR:
7207 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7208 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7209 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7210 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7212 /* Result of these operations is always an int,
7213 but that does not mean the operands should be
7214 converted to ints! */
7215 result_type = integer_type_node;
7216 op0 = lang_hooks.truthvalue_conversion (op0);
7217 op1 = lang_hooks.truthvalue_conversion (op1);
7218 converted = 1;
7220 break;
7222 /* Shift operations: result has same type as first operand;
7223 always convert second operand to int.
7224 Also set SHORT_SHIFT if shifting rightward. */
7226 case RSHIFT_EXPR:
7227 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7229 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7231 if (tree_int_cst_sgn (op1) < 0)
7232 warning ("right shift count is negative");
7233 else
7235 if (!integer_zerop (op1))
7236 short_shift = 1;
7238 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7239 warning ("right shift count >= width of type");
7243 /* Use the type of the value to be shifted. */
7244 result_type = type0;
7245 /* Convert the shift-count to an integer, regardless of size
7246 of value being shifted. */
7247 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7248 op1 = convert (integer_type_node, op1);
7249 /* Avoid converting op1 to result_type later. */
7250 converted = 1;
7252 break;
7254 case LSHIFT_EXPR:
7255 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7257 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7259 if (tree_int_cst_sgn (op1) < 0)
7260 warning ("left shift count is negative");
7262 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7263 warning ("left shift count >= width of type");
7266 /* Use the type of the value to be shifted. */
7267 result_type = type0;
7268 /* Convert the shift-count to an integer, regardless of size
7269 of value being shifted. */
7270 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7271 op1 = convert (integer_type_node, op1);
7272 /* Avoid converting op1 to result_type later. */
7273 converted = 1;
7275 break;
7277 case EQ_EXPR:
7278 case NE_EXPR:
7279 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7280 warning ("comparing floating point with == or != is unsafe");
7281 /* Result of comparison is always int,
7282 but don't convert the args to int! */
7283 build_type = integer_type_node;
7284 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7285 || code0 == COMPLEX_TYPE)
7286 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7287 || code1 == COMPLEX_TYPE))
7288 short_compare = 1;
7289 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7291 tree tt0 = TREE_TYPE (type0);
7292 tree tt1 = TREE_TYPE (type1);
7293 /* Anything compares with void *. void * compares with anything.
7294 Otherwise, the targets must be compatible
7295 and both must be object or both incomplete. */
7296 if (comp_target_types (type0, type1, 1))
7297 result_type = common_pointer_type (type0, type1);
7298 else if (VOID_TYPE_P (tt0))
7300 /* op0 != orig_op0 detects the case of something
7301 whose value is 0 but which isn't a valid null ptr const. */
7302 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7303 && TREE_CODE (tt1) == FUNCTION_TYPE)
7304 pedwarn ("ISO C forbids comparison of %<void *%>"
7305 " with function pointer");
7307 else if (VOID_TYPE_P (tt1))
7309 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7310 && TREE_CODE (tt0) == FUNCTION_TYPE)
7311 pedwarn ("ISO C forbids comparison of %<void *%>"
7312 " with function pointer");
7314 else
7315 pedwarn ("comparison of distinct pointer types lacks a cast");
7317 if (result_type == NULL_TREE)
7318 result_type = ptr_type_node;
7320 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7321 && integer_zerop (op1))
7322 result_type = type0;
7323 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7324 && integer_zerop (op0))
7325 result_type = type1;
7326 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7328 result_type = type0;
7329 pedwarn ("comparison between pointer and integer");
7331 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7333 result_type = type1;
7334 pedwarn ("comparison between pointer and integer");
7336 break;
7338 case LE_EXPR:
7339 case GE_EXPR:
7340 case LT_EXPR:
7341 case GT_EXPR:
7342 build_type = integer_type_node;
7343 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7344 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7345 short_compare = 1;
7346 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7348 if (comp_target_types (type0, type1, 1))
7350 result_type = common_pointer_type (type0, type1);
7351 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7352 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7353 pedwarn ("comparison of complete and incomplete pointers");
7354 else if (pedantic
7355 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7356 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7358 else
7360 result_type = ptr_type_node;
7361 pedwarn ("comparison of distinct pointer types lacks a cast");
7364 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7365 && integer_zerop (op1))
7367 result_type = type0;
7368 if (pedantic || extra_warnings)
7369 pedwarn ("ordered comparison of pointer with integer zero");
7371 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7372 && integer_zerop (op0))
7374 result_type = type1;
7375 if (pedantic)
7376 pedwarn ("ordered comparison of pointer with integer zero");
7378 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7380 result_type = type0;
7381 pedwarn ("comparison between pointer and integer");
7383 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7385 result_type = type1;
7386 pedwarn ("comparison between pointer and integer");
7388 break;
7390 default:
7391 gcc_unreachable ();
7394 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7395 return error_mark_node;
7397 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7398 || code0 == VECTOR_TYPE)
7400 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7401 || code1 == VECTOR_TYPE))
7403 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7405 if (shorten || common || short_compare)
7406 result_type = common_type (type0, type1);
7408 /* For certain operations (which identify themselves by shorten != 0)
7409 if both args were extended from the same smaller type,
7410 do the arithmetic in that type and then extend.
7412 shorten !=0 and !=1 indicates a bitwise operation.
7413 For them, this optimization is safe only if
7414 both args are zero-extended or both are sign-extended.
7415 Otherwise, we might change the result.
7416 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7417 but calculated in (unsigned short) it would be (unsigned short)-1. */
7419 if (shorten && none_complex)
7421 int unsigned0, unsigned1;
7422 tree arg0 = get_narrower (op0, &unsigned0);
7423 tree arg1 = get_narrower (op1, &unsigned1);
7424 /* UNS is 1 if the operation to be done is an unsigned one. */
7425 int uns = TYPE_UNSIGNED (result_type);
7426 tree type;
7428 final_type = result_type;
7430 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7431 but it *requires* conversion to FINAL_TYPE. */
7433 if ((TYPE_PRECISION (TREE_TYPE (op0))
7434 == TYPE_PRECISION (TREE_TYPE (arg0)))
7435 && TREE_TYPE (op0) != final_type)
7436 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7437 if ((TYPE_PRECISION (TREE_TYPE (op1))
7438 == TYPE_PRECISION (TREE_TYPE (arg1)))
7439 && TREE_TYPE (op1) != final_type)
7440 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7442 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7444 /* For bitwise operations, signedness of nominal type
7445 does not matter. Consider only how operands were extended. */
7446 if (shorten == -1)
7447 uns = unsigned0;
7449 /* Note that in all three cases below we refrain from optimizing
7450 an unsigned operation on sign-extended args.
7451 That would not be valid. */
7453 /* Both args variable: if both extended in same way
7454 from same width, do it in that width.
7455 Do it unsigned if args were zero-extended. */
7456 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7457 < TYPE_PRECISION (result_type))
7458 && (TYPE_PRECISION (TREE_TYPE (arg1))
7459 == TYPE_PRECISION (TREE_TYPE (arg0)))
7460 && unsigned0 == unsigned1
7461 && (unsigned0 || !uns))
7462 result_type
7463 = c_common_signed_or_unsigned_type
7464 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7465 else if (TREE_CODE (arg0) == INTEGER_CST
7466 && (unsigned1 || !uns)
7467 && (TYPE_PRECISION (TREE_TYPE (arg1))
7468 < TYPE_PRECISION (result_type))
7469 && (type
7470 = c_common_signed_or_unsigned_type (unsigned1,
7471 TREE_TYPE (arg1)),
7472 int_fits_type_p (arg0, type)))
7473 result_type = type;
7474 else if (TREE_CODE (arg1) == INTEGER_CST
7475 && (unsigned0 || !uns)
7476 && (TYPE_PRECISION (TREE_TYPE (arg0))
7477 < TYPE_PRECISION (result_type))
7478 && (type
7479 = c_common_signed_or_unsigned_type (unsigned0,
7480 TREE_TYPE (arg0)),
7481 int_fits_type_p (arg1, type)))
7482 result_type = type;
7485 /* Shifts can be shortened if shifting right. */
7487 if (short_shift)
7489 int unsigned_arg;
7490 tree arg0 = get_narrower (op0, &unsigned_arg);
7492 final_type = result_type;
7494 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7495 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7497 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7498 /* We can shorten only if the shift count is less than the
7499 number of bits in the smaller type size. */
7500 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7501 /* We cannot drop an unsigned shift after sign-extension. */
7502 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7504 /* Do an unsigned shift if the operand was zero-extended. */
7505 result_type
7506 = c_common_signed_or_unsigned_type (unsigned_arg,
7507 TREE_TYPE (arg0));
7508 /* Convert value-to-be-shifted to that type. */
7509 if (TREE_TYPE (op0) != result_type)
7510 op0 = convert (result_type, op0);
7511 converted = 1;
7515 /* Comparison operations are shortened too but differently.
7516 They identify themselves by setting short_compare = 1. */
7518 if (short_compare)
7520 /* Don't write &op0, etc., because that would prevent op0
7521 from being kept in a register.
7522 Instead, make copies of the our local variables and
7523 pass the copies by reference, then copy them back afterward. */
7524 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7525 enum tree_code xresultcode = resultcode;
7526 tree val
7527 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7529 if (val != 0)
7530 return val;
7532 op0 = xop0, op1 = xop1;
7533 converted = 1;
7534 resultcode = xresultcode;
7536 if (warn_sign_compare && skip_evaluation == 0)
7538 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7539 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7540 int unsignedp0, unsignedp1;
7541 tree primop0 = get_narrower (op0, &unsignedp0);
7542 tree primop1 = get_narrower (op1, &unsignedp1);
7544 xop0 = orig_op0;
7545 xop1 = orig_op1;
7546 STRIP_TYPE_NOPS (xop0);
7547 STRIP_TYPE_NOPS (xop1);
7549 /* Give warnings for comparisons between signed and unsigned
7550 quantities that may fail.
7552 Do the checking based on the original operand trees, so that
7553 casts will be considered, but default promotions won't be.
7555 Do not warn if the comparison is being done in a signed type,
7556 since the signed type will only be chosen if it can represent
7557 all the values of the unsigned type. */
7558 if (!TYPE_UNSIGNED (result_type))
7559 /* OK */;
7560 /* Do not warn if both operands are the same signedness. */
7561 else if (op0_signed == op1_signed)
7562 /* OK */;
7563 else
7565 tree sop, uop;
7567 if (op0_signed)
7568 sop = xop0, uop = xop1;
7569 else
7570 sop = xop1, uop = xop0;
7572 /* Do not warn if the signed quantity is an
7573 unsuffixed integer literal (or some static
7574 constant expression involving such literals or a
7575 conditional expression involving such literals)
7576 and it is non-negative. */
7577 if (tree_expr_nonnegative_p (sop))
7578 /* OK */;
7579 /* Do not warn if the comparison is an equality operation,
7580 the unsigned quantity is an integral constant, and it
7581 would fit in the result if the result were signed. */
7582 else if (TREE_CODE (uop) == INTEGER_CST
7583 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7584 && int_fits_type_p
7585 (uop, c_common_signed_type (result_type)))
7586 /* OK */;
7587 /* Do not warn if the unsigned quantity is an enumeration
7588 constant and its maximum value would fit in the result
7589 if the result were signed. */
7590 else if (TREE_CODE (uop) == INTEGER_CST
7591 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7592 && int_fits_type_p
7593 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7594 c_common_signed_type (result_type)))
7595 /* OK */;
7596 else
7597 warning ("comparison between signed and unsigned");
7600 /* Warn if two unsigned values are being compared in a size
7601 larger than their original size, and one (and only one) is the
7602 result of a `~' operator. This comparison will always fail.
7604 Also warn if one operand is a constant, and the constant
7605 does not have all bits set that are set in the ~ operand
7606 when it is extended. */
7608 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7609 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7611 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7612 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7613 &unsignedp0);
7614 else
7615 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7616 &unsignedp1);
7618 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7620 tree primop;
7621 HOST_WIDE_INT constant, mask;
7622 int unsignedp, bits;
7624 if (host_integerp (primop0, 0))
7626 primop = primop1;
7627 unsignedp = unsignedp1;
7628 constant = tree_low_cst (primop0, 0);
7630 else
7632 primop = primop0;
7633 unsignedp = unsignedp0;
7634 constant = tree_low_cst (primop1, 0);
7637 bits = TYPE_PRECISION (TREE_TYPE (primop));
7638 if (bits < TYPE_PRECISION (result_type)
7639 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7641 mask = (~(HOST_WIDE_INT) 0) << bits;
7642 if ((mask & constant) != mask)
7643 warning ("comparison of promoted ~unsigned with constant");
7646 else if (unsignedp0 && unsignedp1
7647 && (TYPE_PRECISION (TREE_TYPE (primop0))
7648 < TYPE_PRECISION (result_type))
7649 && (TYPE_PRECISION (TREE_TYPE (primop1))
7650 < TYPE_PRECISION (result_type)))
7651 warning ("comparison of promoted ~unsigned with unsigned");
7657 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7658 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7659 Then the expression will be built.
7660 It will be given type FINAL_TYPE if that is nonzero;
7661 otherwise, it will be given type RESULT_TYPE. */
7663 if (!result_type)
7665 binary_op_error (code);
7666 return error_mark_node;
7669 if (!converted)
7671 if (TREE_TYPE (op0) != result_type)
7672 op0 = convert (result_type, op0);
7673 if (TREE_TYPE (op1) != result_type)
7674 op1 = convert (result_type, op1);
7676 /* This can happen if one operand has a vector type, and the other
7677 has a different type. */
7678 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7679 return error_mark_node;
7682 if (build_type == NULL_TREE)
7683 build_type = result_type;
7686 tree result = build2 (resultcode, build_type, op0, op1);
7688 /* Treat expressions in initializers specially as they can't trap. */
7689 result = require_constant_value ? fold_initializer (result)
7690 : fold (result);
7692 if (final_type != 0)
7693 result = convert (final_type, result);
7694 return result;