PR middle-end/19583
[official-gcc.git] / gcc / c-typeck.c
blobf04ba73ad75fe5fa772bea03cc44eb8e423067e8
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 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"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
67 /* Nonzero if we've already printed a "missing braces around initializer"
68 message within this initializer. */
69 static int missing_braces_mentioned;
71 static int require_constant_value;
72 static int require_constant_elements;
74 static tree qualify_type (tree, tree);
75 static int tagged_types_tu_compatible_p (tree, tree);
76 static int comp_target_types (tree, tree, int);
77 static int function_types_compatible_p (tree, tree);
78 static int type_lists_compatible_p (tree, tree);
79 static tree decl_constant_value_for_broken_optimization (tree);
80 static tree default_function_array_conversion (tree);
81 static tree lookup_field (tree, tree);
82 static tree convert_arguments (tree, tree, tree, tree);
83 static tree pointer_diff (tree, tree);
84 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
85 int);
86 static tree valid_compound_expr_initializer (tree, tree);
87 static void push_string (const char *);
88 static void push_member_name (tree);
89 static void push_array_bounds (int);
90 static int spelling_length (void);
91 static char *print_spelling (char *);
92 static void warning_init (const char *);
93 static tree digest_init (tree, tree, bool, int);
94 static void output_init_element (tree, bool, tree, tree, int);
95 static void output_pending_init_elements (int);
96 static int set_designator (int);
97 static void push_range_stack (tree);
98 static void add_pending_init (tree, tree);
99 static void set_nonincremental_init (void);
100 static void set_nonincremental_init_from_string (tree);
101 static tree find_init_member (tree);
102 static void readonly_error (tree, enum lvalue_use);
103 static void record_maybe_used_decl (tree);
105 /* Do `exp = require_complete_type (exp);' to make sure exp
106 does not have an incomplete type. (That includes void types.) */
108 tree
109 require_complete_type (tree value)
111 tree type = TREE_TYPE (value);
113 if (value == error_mark_node || type == error_mark_node)
114 return error_mark_node;
116 /* First, detect a valid value with a complete type. */
117 if (COMPLETE_TYPE_P (type))
118 return value;
120 c_incomplete_type_error (value, type);
121 return error_mark_node;
124 /* Print an error message for invalid use of an incomplete type.
125 VALUE is the expression that was used (or 0 if that isn't known)
126 and TYPE is the type that was invalid. */
128 void
129 c_incomplete_type_error (tree value, tree type)
131 const char *type_code_string;
133 /* Avoid duplicate error message. */
134 if (TREE_CODE (type) == ERROR_MARK)
135 return;
137 if (value != 0 && (TREE_CODE (value) == VAR_DECL
138 || TREE_CODE (value) == PARM_DECL))
139 error ("%qs has an incomplete type",
140 IDENTIFIER_POINTER (DECL_NAME (value)));
141 else
143 retry:
144 /* We must print an error message. Be clever about what it says. */
146 switch (TREE_CODE (type))
148 case RECORD_TYPE:
149 type_code_string = "struct";
150 break;
152 case UNION_TYPE:
153 type_code_string = "union";
154 break;
156 case ENUMERAL_TYPE:
157 type_code_string = "enum";
158 break;
160 case VOID_TYPE:
161 error ("invalid use of void expression");
162 return;
164 case ARRAY_TYPE:
165 if (TYPE_DOMAIN (type))
167 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
169 error ("invalid use of flexible array member");
170 return;
172 type = TREE_TYPE (type);
173 goto retry;
175 error ("invalid use of array with unspecified bounds");
176 return;
178 default:
179 gcc_unreachable ();
182 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
183 error ("invalid use of undefined type %<%s %s%>",
184 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
185 else
186 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
187 error ("invalid use of incomplete typedef %qs",
188 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
192 /* Given a type, apply default promotions wrt unnamed function
193 arguments and return the new type. */
195 tree
196 c_type_promotes_to (tree type)
198 if (TYPE_MAIN_VARIANT (type) == float_type_node)
199 return double_type_node;
201 if (c_promoting_integer_type_p (type))
203 /* Preserve unsignedness if not really getting any wider. */
204 if (TYPE_UNSIGNED (type)
205 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
206 return unsigned_type_node;
207 return integer_type_node;
210 return type;
213 /* Return a variant of TYPE which has all the type qualifiers of LIKE
214 as well as those of TYPE. */
216 static tree
217 qualify_type (tree type, tree like)
219 return c_build_qualified_type (type,
220 TYPE_QUALS (type) | TYPE_QUALS (like));
223 /* Return the composite type of two compatible types.
225 We assume that comptypes has already been done and returned
226 nonzero; if that isn't so, this may crash. In particular, we
227 assume that qualifiers match. */
229 tree
230 composite_type (tree t1, tree t2)
232 enum tree_code code1;
233 enum tree_code code2;
234 tree attributes;
236 /* Save time if the two types are the same. */
238 if (t1 == t2) return t1;
240 /* If one type is nonsense, use the other. */
241 if (t1 == error_mark_node)
242 return t2;
243 if (t2 == error_mark_node)
244 return t1;
246 code1 = TREE_CODE (t1);
247 code2 = TREE_CODE (t2);
249 /* Merge the attributes. */
250 attributes = targetm.merge_type_attributes (t1, t2);
252 /* If one is an enumerated type and the other is the compatible
253 integer type, the composite type might be either of the two
254 (DR#013 question 3). For consistency, use the enumerated type as
255 the composite type. */
257 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
258 return t1;
259 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
260 return t2;
262 gcc_assert (code1 == code2);
264 switch (code1)
266 case POINTER_TYPE:
267 /* For two pointers, do this recursively on the target type. */
269 tree pointed_to_1 = TREE_TYPE (t1);
270 tree pointed_to_2 = TREE_TYPE (t2);
271 tree target = composite_type (pointed_to_1, pointed_to_2);
272 t1 = build_pointer_type (target);
273 t1 = build_type_attribute_variant (t1, attributes);
274 return qualify_type (t1, t2);
277 case ARRAY_TYPE:
279 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
280 int quals;
281 tree unqual_elt;
283 /* We should not have any type quals on arrays at all. */
284 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
286 /* Save space: see if the result is identical to one of the args. */
287 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
288 return build_type_attribute_variant (t1, attributes);
289 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
290 return build_type_attribute_variant (t2, attributes);
292 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
293 return build_type_attribute_variant (t1, attributes);
294 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
295 return build_type_attribute_variant (t2, attributes);
297 /* Merge the element types, and have a size if either arg has
298 one. We may have qualifiers on the element types. To set
299 up TYPE_MAIN_VARIANT correctly, we need to form the
300 composite of the unqualified types and add the qualifiers
301 back at the end. */
302 quals = TYPE_QUALS (strip_array_types (elt));
303 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
304 t1 = build_array_type (unqual_elt,
305 TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
306 t1 = c_build_qualified_type (t1, quals);
307 return build_type_attribute_variant (t1, attributes);
310 case FUNCTION_TYPE:
311 /* Function types: prefer the one that specified arg types.
312 If both do, merge the arg types. Also merge the return types. */
314 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
315 tree p1 = TYPE_ARG_TYPES (t1);
316 tree p2 = TYPE_ARG_TYPES (t2);
317 int len;
318 tree newargs, n;
319 int i;
321 /* Save space: see if the result is identical to one of the args. */
322 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
323 return build_type_attribute_variant (t1, attributes);
324 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
325 return build_type_attribute_variant (t2, attributes);
327 /* Simple way if one arg fails to specify argument types. */
328 if (TYPE_ARG_TYPES (t1) == 0)
330 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
331 t1 = build_type_attribute_variant (t1, attributes);
332 return qualify_type (t1, t2);
334 if (TYPE_ARG_TYPES (t2) == 0)
336 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
337 t1 = build_type_attribute_variant (t1, attributes);
338 return qualify_type (t1, t2);
341 /* If both args specify argument types, we must merge the two
342 lists, argument by argument. */
343 /* Tell global_bindings_p to return false so that variable_size
344 doesn't abort on VLAs in parameter types. */
345 c_override_global_bindings_to_false = true;
347 len = list_length (p1);
348 newargs = 0;
350 for (i = 0; i < len; i++)
351 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
353 n = newargs;
355 for (; p1;
356 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
358 /* A null type means arg type is not specified.
359 Take whatever the other function type has. */
360 if (TREE_VALUE (p1) == 0)
362 TREE_VALUE (n) = TREE_VALUE (p2);
363 goto parm_done;
365 if (TREE_VALUE (p2) == 0)
367 TREE_VALUE (n) = TREE_VALUE (p1);
368 goto parm_done;
371 /* Given wait (union {union wait *u; int *i} *)
372 and wait (union wait *),
373 prefer union wait * as type of parm. */
374 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
375 && TREE_VALUE (p1) != TREE_VALUE (p2))
377 tree memb;
378 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
379 memb; memb = TREE_CHAIN (memb))
380 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
382 TREE_VALUE (n) = TREE_VALUE (p2);
383 if (pedantic)
384 pedwarn ("function types not truly compatible in ISO C");
385 goto parm_done;
388 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
389 && TREE_VALUE (p2) != TREE_VALUE (p1))
391 tree memb;
392 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
393 memb; memb = TREE_CHAIN (memb))
394 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
396 TREE_VALUE (n) = TREE_VALUE (p1);
397 if (pedantic)
398 pedwarn ("function types not truly compatible in ISO C");
399 goto parm_done;
402 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
403 parm_done: ;
406 c_override_global_bindings_to_false = false;
407 t1 = build_function_type (valtype, newargs);
408 t1 = qualify_type (t1, t2);
409 /* ... falls through ... */
412 default:
413 return build_type_attribute_variant (t1, attributes);
418 /* Return the type of a conditional expression between pointers to
419 possibly differently qualified versions of compatible types.
421 We assume that comp_target_types has already been done and returned
422 nonzero; if that isn't so, this may crash. */
424 static tree
425 common_pointer_type (tree t1, tree t2)
427 tree attributes;
428 tree pointed_to_1, mv1;
429 tree pointed_to_2, mv2;
430 tree target;
432 /* Save time if the two types are the same. */
434 if (t1 == t2) return t1;
436 /* If one type is nonsense, use the other. */
437 if (t1 == error_mark_node)
438 return t2;
439 if (t2 == error_mark_node)
440 return t1;
442 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
443 && TREE_CODE (t2) == POINTER_TYPE);
445 /* Merge the attributes. */
446 attributes = targetm.merge_type_attributes (t1, t2);
448 /* Find the composite type of the target types, and combine the
449 qualifiers of the two types' targets. Do not lose qualifiers on
450 array element types by taking the TYPE_MAIN_VARIANT. */
451 mv1 = pointed_to_1 = TREE_TYPE (t1);
452 mv2 = pointed_to_2 = TREE_TYPE (t2);
453 if (TREE_CODE (mv1) != ARRAY_TYPE)
454 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
455 if (TREE_CODE (mv2) != ARRAY_TYPE)
456 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
457 target = composite_type (mv1, mv2);
458 t1 = build_pointer_type (c_build_qualified_type
459 (target,
460 TYPE_QUALS (pointed_to_1) |
461 TYPE_QUALS (pointed_to_2)));
462 return build_type_attribute_variant (t1, attributes);
465 /* Return the common type for two arithmetic types under the usual
466 arithmetic conversions. The default conversions have already been
467 applied, and enumerated types converted to their compatible integer
468 types. The resulting type is unqualified and has no attributes.
470 This is the type for the result of most arithmetic operations
471 if the operands have the given two types. */
473 tree
474 common_type (tree t1, tree t2)
476 enum tree_code code1;
477 enum tree_code code2;
479 /* If one type is nonsense, use the other. */
480 if (t1 == error_mark_node)
481 return t2;
482 if (t2 == error_mark_node)
483 return t1;
485 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
486 t1 = TYPE_MAIN_VARIANT (t1);
488 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
489 t2 = TYPE_MAIN_VARIANT (t2);
491 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
492 t1 = build_type_attribute_variant (t1, NULL_TREE);
494 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
495 t2 = build_type_attribute_variant (t2, NULL_TREE);
497 /* Save time if the two types are the same. */
499 if (t1 == t2) return t1;
501 code1 = TREE_CODE (t1);
502 code2 = TREE_CODE (t2);
504 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
505 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
506 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
507 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
509 /* If one type is a vector type, return that type. (How the usual
510 arithmetic conversions apply to the vector types extension is not
511 precisely specified.) */
512 if (code1 == VECTOR_TYPE)
513 return t1;
515 if (code2 == VECTOR_TYPE)
516 return t2;
518 /* If one type is complex, form the common type of the non-complex
519 components, then make that complex. Use T1 or T2 if it is the
520 required type. */
521 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
523 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
524 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
525 tree subtype = common_type (subtype1, subtype2);
527 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
528 return t1;
529 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
530 return t2;
531 else
532 return build_complex_type (subtype);
535 /* If only one is real, use it as the result. */
537 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
538 return t1;
540 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
541 return t2;
543 /* Both real or both integers; use the one with greater precision. */
545 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
546 return t1;
547 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
548 return t2;
550 /* Same precision. Prefer long longs to longs to ints when the
551 same precision, following the C99 rules on integer type rank
552 (which are equivalent to the C90 rules for C90 types). */
554 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
555 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
556 return long_long_unsigned_type_node;
558 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
559 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
561 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
562 return long_long_unsigned_type_node;
563 else
564 return long_long_integer_type_node;
567 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
568 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
569 return long_unsigned_type_node;
571 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
572 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
574 /* But preserve unsignedness from the other type,
575 since long cannot hold all the values of an unsigned int. */
576 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
577 return long_unsigned_type_node;
578 else
579 return long_integer_type_node;
582 /* Likewise, prefer long double to double even if same size. */
583 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
584 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
585 return long_double_type_node;
587 /* Otherwise prefer the unsigned one. */
589 if (TYPE_UNSIGNED (t1))
590 return t1;
591 else
592 return t2;
595 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
596 or various other operations. Return 2 if they are compatible
597 but a warning may be needed if you use them together. */
600 comptypes (tree type1, tree type2)
602 tree t1 = type1;
603 tree t2 = type2;
604 int attrval, val;
606 /* Suppress errors caused by previously reported errors. */
608 if (t1 == t2 || !t1 || !t2
609 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
610 return 1;
612 /* If either type is the internal version of sizetype, return the
613 language version. */
614 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
615 && TYPE_ORIG_SIZE_TYPE (t1))
616 t1 = TYPE_ORIG_SIZE_TYPE (t1);
618 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
619 && TYPE_ORIG_SIZE_TYPE (t2))
620 t2 = TYPE_ORIG_SIZE_TYPE (t2);
623 /* Enumerated types are compatible with integer types, but this is
624 not transitive: two enumerated types in the same translation unit
625 are compatible with each other only if they are the same type. */
627 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
628 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
629 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
630 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
632 if (t1 == t2)
633 return 1;
635 /* Different classes of types can't be compatible. */
637 if (TREE_CODE (t1) != TREE_CODE (t2))
638 return 0;
640 /* Qualifiers must match. C99 6.7.3p9 */
642 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
643 return 0;
645 /* Allow for two different type nodes which have essentially the same
646 definition. Note that we already checked for equality of the type
647 qualifiers (just above). */
649 if (TREE_CODE (t1) != ARRAY_TYPE
650 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
651 return 1;
653 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
654 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
655 return 0;
657 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
658 val = 0;
660 switch (TREE_CODE (t1))
662 case POINTER_TYPE:
663 /* We must give ObjC the first crack at comparing pointers, since
664 protocol qualifiers may be involved. */
665 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
666 break;
667 /* Do not remove mode or aliasing information. */
668 if (TYPE_MODE (t1) != TYPE_MODE (t2)
669 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
670 break;
671 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
672 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
673 break;
675 case FUNCTION_TYPE:
676 val = function_types_compatible_p (t1, t2);
677 break;
679 case ARRAY_TYPE:
681 tree d1 = TYPE_DOMAIN (t1);
682 tree d2 = TYPE_DOMAIN (t2);
683 bool d1_variable, d2_variable;
684 bool d1_zero, d2_zero;
685 val = 1;
687 /* Target types must match incl. qualifiers. */
688 if (TREE_TYPE (t1) != TREE_TYPE (t2)
689 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
690 return 0;
692 /* Sizes must match unless one is missing or variable. */
693 if (d1 == 0 || d2 == 0 || d1 == d2)
694 break;
696 d1_zero = !TYPE_MAX_VALUE (d1);
697 d2_zero = !TYPE_MAX_VALUE (d2);
699 d1_variable = (!d1_zero
700 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
701 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
702 d2_variable = (!d2_zero
703 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
704 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
706 if (d1_variable || d2_variable)
707 break;
708 if (d1_zero && d2_zero)
709 break;
710 if (d1_zero || d2_zero
711 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
712 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
713 val = 0;
715 break;
718 case RECORD_TYPE:
719 /* We are dealing with two distinct structs. In assorted Objective-C
720 corner cases, however, these can still be deemed equivalent. */
721 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
722 val = 1;
724 case ENUMERAL_TYPE:
725 case UNION_TYPE:
726 if (val != 1 && !same_translation_unit_p (t1, t2))
727 val = tagged_types_tu_compatible_p (t1, t2);
728 break;
730 case VECTOR_TYPE:
731 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
732 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
733 break;
735 default:
736 break;
738 return attrval == 2 && val == 1 ? 2 : val;
741 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
742 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
743 to 1 or 0 depending if the check of the pointer types is meant to
744 be reflexive or not (typically, assignments are not reflexive,
745 while comparisons are reflexive).
748 static int
749 comp_target_types (tree ttl, tree ttr, int reflexive)
751 int val;
752 tree mvl, mvr;
754 /* Give objc_comptypes a crack at letting these types through. */
755 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
756 return val;
758 /* Do not lose qualifiers on element types of array types that are
759 pointer targets by taking their TYPE_MAIN_VARIANT. */
760 mvl = TREE_TYPE (ttl);
761 mvr = TREE_TYPE (ttr);
762 if (TREE_CODE (mvl) != ARRAY_TYPE)
763 mvl = TYPE_MAIN_VARIANT (mvl);
764 if (TREE_CODE (mvr) != ARRAY_TYPE)
765 mvr = TYPE_MAIN_VARIANT (mvr);
766 val = comptypes (mvl, mvr);
768 if (val == 2 && pedantic)
769 pedwarn ("types are not quite compatible");
770 return val;
773 /* Subroutines of `comptypes'. */
775 /* Determine whether two trees derive from the same translation unit.
776 If the CONTEXT chain ends in a null, that tree's context is still
777 being parsed, so if two trees have context chains ending in null,
778 they're in the same translation unit. */
780 same_translation_unit_p (tree t1, tree t2)
782 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
783 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
785 case tcc_declaration:
786 t1 = DECL_CONTEXT (t1); break;
787 case tcc_type:
788 t1 = TYPE_CONTEXT (t1); break;
789 case tcc_exceptional:
790 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
791 default: gcc_unreachable ();
794 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
795 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
797 case tcc_declaration:
798 t2 = DECL_CONTEXT (t2); break;
799 case tcc_type:
800 t2 = TYPE_CONTEXT (t2); break;
801 case tcc_exceptional:
802 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
803 default: gcc_unreachable ();
806 return t1 == t2;
809 /* The C standard says that two structures in different translation
810 units are compatible with each other only if the types of their
811 fields are compatible (among other things). So, consider two copies
812 of this structure: */
814 struct tagged_tu_seen {
815 const struct tagged_tu_seen * next;
816 tree t1;
817 tree t2;
820 /* Can they be compatible with each other? We choose to break the
821 recursion by allowing those types to be compatible. */
823 static const struct tagged_tu_seen * tagged_tu_seen_base;
825 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
826 compatible. If the two types are not the same (which has been
827 checked earlier), this can only happen when multiple translation
828 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
829 rules. */
831 static int
832 tagged_types_tu_compatible_p (tree t1, tree t2)
834 tree s1, s2;
835 bool needs_warning = false;
837 /* We have to verify that the tags of the types are the same. This
838 is harder than it looks because this may be a typedef, so we have
839 to go look at the original type. It may even be a typedef of a
840 typedef...
841 In the case of compiler-created builtin structs the TYPE_DECL
842 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
843 while (TYPE_NAME (t1)
844 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
845 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
846 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
848 while (TYPE_NAME (t2)
849 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
850 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
851 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
853 /* C90 didn't have the requirement that the two tags be the same. */
854 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
855 return 0;
857 /* C90 didn't say what happened if one or both of the types were
858 incomplete; we choose to follow C99 rules here, which is that they
859 are compatible. */
860 if (TYPE_SIZE (t1) == NULL
861 || TYPE_SIZE (t2) == NULL)
862 return 1;
865 const struct tagged_tu_seen * tts_i;
866 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
867 if (tts_i->t1 == t1 && tts_i->t2 == t2)
868 return 1;
871 switch (TREE_CODE (t1))
873 case ENUMERAL_TYPE:
876 /* Speed up the case where the type values are in the same order. */
877 tree tv1 = TYPE_VALUES (t1);
878 tree tv2 = TYPE_VALUES (t2);
880 if (tv1 == tv2)
881 return 1;
883 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
885 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
886 break;
887 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
888 return 0;
891 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
892 return 1;
893 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
894 return 0;
896 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
897 return 0;
899 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
901 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
902 if (s2 == NULL
903 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
904 return 0;
906 return 1;
909 case UNION_TYPE:
911 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
912 return 0;
914 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
916 bool ok = false;
917 struct tagged_tu_seen tts;
919 tts.next = tagged_tu_seen_base;
920 tts.t1 = t1;
921 tts.t2 = t2;
922 tagged_tu_seen_base = &tts;
924 if (DECL_NAME (s1) != NULL)
925 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
926 if (DECL_NAME (s1) == DECL_NAME (s2))
928 int result;
929 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
930 if (result == 0)
931 break;
932 if (result == 2)
933 needs_warning = true;
935 if (TREE_CODE (s1) == FIELD_DECL
936 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
937 DECL_FIELD_BIT_OFFSET (s2)) != 1)
938 break;
940 ok = true;
941 break;
943 tagged_tu_seen_base = tts.next;
944 if (!ok)
945 return 0;
947 return needs_warning ? 2 : 1;
950 case RECORD_TYPE:
952 struct tagged_tu_seen tts;
954 tts.next = tagged_tu_seen_base;
955 tts.t1 = t1;
956 tts.t2 = t2;
957 tagged_tu_seen_base = &tts;
959 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
960 s1 && s2;
961 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
963 int result;
964 if (TREE_CODE (s1) != TREE_CODE (s2)
965 || DECL_NAME (s1) != DECL_NAME (s2))
966 break;
967 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
968 if (result == 0)
969 break;
970 if (result == 2)
971 needs_warning = true;
973 if (TREE_CODE (s1) == FIELD_DECL
974 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
975 DECL_FIELD_BIT_OFFSET (s2)) != 1)
976 break;
978 tagged_tu_seen_base = tts.next;
979 if (s1 && s2)
980 return 0;
981 return needs_warning ? 2 : 1;
984 default:
985 gcc_unreachable ();
989 /* Return 1 if two function types F1 and F2 are compatible.
990 If either type specifies no argument types,
991 the other must specify a fixed number of self-promoting arg types.
992 Otherwise, if one type specifies only the number of arguments,
993 the other must specify that number of self-promoting arg types.
994 Otherwise, the argument types must match. */
996 static int
997 function_types_compatible_p (tree f1, tree f2)
999 tree args1, args2;
1000 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1001 int val = 1;
1002 int val1;
1003 tree ret1, ret2;
1005 ret1 = TREE_TYPE (f1);
1006 ret2 = TREE_TYPE (f2);
1008 /* 'volatile' qualifiers on a function's return type used to mean
1009 the function is noreturn. */
1010 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1011 pedwarn ("function return types not compatible due to %<volatile%>");
1012 if (TYPE_VOLATILE (ret1))
1013 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1014 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1015 if (TYPE_VOLATILE (ret2))
1016 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1017 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1018 val = comptypes (ret1, ret2);
1019 if (val == 0)
1020 return 0;
1022 args1 = TYPE_ARG_TYPES (f1);
1023 args2 = TYPE_ARG_TYPES (f2);
1025 /* An unspecified parmlist matches any specified parmlist
1026 whose argument types don't need default promotions. */
1028 if (args1 == 0)
1030 if (!self_promoting_args_p (args2))
1031 return 0;
1032 /* If one of these types comes from a non-prototype fn definition,
1033 compare that with the other type's arglist.
1034 If they don't match, ask for a warning (but no error). */
1035 if (TYPE_ACTUAL_ARG_TYPES (f1)
1036 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1037 val = 2;
1038 return val;
1040 if (args2 == 0)
1042 if (!self_promoting_args_p (args1))
1043 return 0;
1044 if (TYPE_ACTUAL_ARG_TYPES (f2)
1045 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1046 val = 2;
1047 return val;
1050 /* Both types have argument lists: compare them and propagate results. */
1051 val1 = type_lists_compatible_p (args1, args2);
1052 return val1 != 1 ? val1 : val;
1055 /* Check two lists of types for compatibility,
1056 returning 0 for incompatible, 1 for compatible,
1057 or 2 for compatible with warning. */
1059 static int
1060 type_lists_compatible_p (tree args1, tree args2)
1062 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1063 int val = 1;
1064 int newval = 0;
1066 while (1)
1068 tree a1, mv1, a2, mv2;
1069 if (args1 == 0 && args2 == 0)
1070 return val;
1071 /* If one list is shorter than the other,
1072 they fail to match. */
1073 if (args1 == 0 || args2 == 0)
1074 return 0;
1075 mv1 = a1 = TREE_VALUE (args1);
1076 mv2 = a2 = TREE_VALUE (args2);
1077 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1078 mv1 = TYPE_MAIN_VARIANT (mv1);
1079 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1080 mv2 = TYPE_MAIN_VARIANT (mv2);
1081 /* A null pointer instead of a type
1082 means there is supposed to be an argument
1083 but nothing is specified about what type it has.
1084 So match anything that self-promotes. */
1085 if (a1 == 0)
1087 if (c_type_promotes_to (a2) != a2)
1088 return 0;
1090 else if (a2 == 0)
1092 if (c_type_promotes_to (a1) != a1)
1093 return 0;
1095 /* If one of the lists has an error marker, ignore this arg. */
1096 else if (TREE_CODE (a1) == ERROR_MARK
1097 || TREE_CODE (a2) == ERROR_MARK)
1099 else if (!(newval = comptypes (mv1, mv2)))
1101 /* Allow wait (union {union wait *u; int *i} *)
1102 and wait (union wait *) to be compatible. */
1103 if (TREE_CODE (a1) == UNION_TYPE
1104 && (TYPE_NAME (a1) == 0
1105 || TYPE_TRANSPARENT_UNION (a1))
1106 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1107 && tree_int_cst_equal (TYPE_SIZE (a1),
1108 TYPE_SIZE (a2)))
1110 tree memb;
1111 for (memb = TYPE_FIELDS (a1);
1112 memb; memb = TREE_CHAIN (memb))
1113 if (comptypes (TREE_TYPE (memb), a2))
1114 break;
1115 if (memb == 0)
1116 return 0;
1118 else if (TREE_CODE (a2) == UNION_TYPE
1119 && (TYPE_NAME (a2) == 0
1120 || TYPE_TRANSPARENT_UNION (a2))
1121 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1122 && tree_int_cst_equal (TYPE_SIZE (a2),
1123 TYPE_SIZE (a1)))
1125 tree memb;
1126 for (memb = TYPE_FIELDS (a2);
1127 memb; memb = TREE_CHAIN (memb))
1128 if (comptypes (TREE_TYPE (memb), a1))
1129 break;
1130 if (memb == 0)
1131 return 0;
1133 else
1134 return 0;
1137 /* comptypes said ok, but record if it said to warn. */
1138 if (newval > val)
1139 val = newval;
1141 args1 = TREE_CHAIN (args1);
1142 args2 = TREE_CHAIN (args2);
1146 /* Compute the size to increment a pointer by. */
1148 static tree
1149 c_size_in_bytes (tree type)
1151 enum tree_code code = TREE_CODE (type);
1153 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1154 return size_one_node;
1156 if (!COMPLETE_OR_VOID_TYPE_P (type))
1158 error ("arithmetic on pointer to an incomplete type");
1159 return size_one_node;
1162 /* Convert in case a char is more than one unit. */
1163 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1164 size_int (TYPE_PRECISION (char_type_node)
1165 / BITS_PER_UNIT));
1168 /* Return either DECL or its known constant value (if it has one). */
1170 tree
1171 decl_constant_value (tree decl)
1173 if (/* Don't change a variable array bound or initial value to a constant
1174 in a place where a variable is invalid. Note that DECL_INITIAL
1175 isn't valid for a PARM_DECL. */
1176 current_function_decl != 0
1177 && TREE_CODE (decl) != PARM_DECL
1178 && !TREE_THIS_VOLATILE (decl)
1179 && TREE_READONLY (decl)
1180 && DECL_INITIAL (decl) != 0
1181 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1182 /* This is invalid if initial value is not constant.
1183 If it has either a function call, a memory reference,
1184 or a variable, then re-evaluating it could give different results. */
1185 && TREE_CONSTANT (DECL_INITIAL (decl))
1186 /* Check for cases where this is sub-optimal, even though valid. */
1187 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1188 return DECL_INITIAL (decl);
1189 return decl;
1192 /* Return either DECL or its known constant value (if it has one), but
1193 return DECL if pedantic or DECL has mode BLKmode. This is for
1194 bug-compatibility with the old behavior of decl_constant_value
1195 (before GCC 3.0); every use of this function is a bug and it should
1196 be removed before GCC 3.1. It is not appropriate to use pedantic
1197 in a way that affects optimization, and BLKmode is probably not the
1198 right test for avoiding misoptimizations either. */
1200 static tree
1201 decl_constant_value_for_broken_optimization (tree decl)
1203 if (pedantic || DECL_MODE (decl) == BLKmode)
1204 return decl;
1205 else
1206 return decl_constant_value (decl);
1210 /* Perform the default conversion of arrays and functions to pointers.
1211 Return the result of converting EXP. For any other expression, just
1212 return EXP. */
1214 static tree
1215 default_function_array_conversion (tree exp)
1217 tree orig_exp;
1218 tree type = TREE_TYPE (exp);
1219 enum tree_code code = TREE_CODE (type);
1220 int not_lvalue = 0;
1222 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1223 an lvalue.
1225 Do not use STRIP_NOPS here! It will remove conversions from pointer
1226 to integer and cause infinite recursion. */
1227 orig_exp = exp;
1228 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1229 || (TREE_CODE (exp) == NOP_EXPR
1230 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1232 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1233 not_lvalue = 1;
1234 exp = TREE_OPERAND (exp, 0);
1237 if (TREE_NO_WARNING (orig_exp))
1238 TREE_NO_WARNING (exp) = 1;
1240 if (code == FUNCTION_TYPE)
1242 return build_unary_op (ADDR_EXPR, exp, 0);
1244 if (code == ARRAY_TYPE)
1246 tree adr;
1247 tree restype = TREE_TYPE (type);
1248 tree ptrtype;
1249 int constp = 0;
1250 int volatilep = 0;
1251 int lvalue_array_p;
1253 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1255 constp = TREE_READONLY (exp);
1256 volatilep = TREE_THIS_VOLATILE (exp);
1259 if (TYPE_QUALS (type) || constp || volatilep)
1260 restype
1261 = c_build_qualified_type (restype,
1262 TYPE_QUALS (type)
1263 | (constp * TYPE_QUAL_CONST)
1264 | (volatilep * TYPE_QUAL_VOLATILE));
1266 if (TREE_CODE (exp) == INDIRECT_REF)
1267 return convert (build_pointer_type (restype),
1268 TREE_OPERAND (exp, 0));
1270 if (TREE_CODE (exp) == COMPOUND_EXPR)
1272 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1273 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1274 TREE_OPERAND (exp, 0), op1);
1277 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1278 if (!flag_isoc99 && !lvalue_array_p)
1280 /* Before C99, non-lvalue arrays do not decay to pointers.
1281 Normally, using such an array would be invalid; but it can
1282 be used correctly inside sizeof or as a statement expression.
1283 Thus, do not give an error here; an error will result later. */
1284 return exp;
1287 ptrtype = build_pointer_type (restype);
1289 if (TREE_CODE (exp) == VAR_DECL)
1291 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1292 ADDR_EXPR because it's the best way of representing what
1293 happens in C when we take the address of an array and place
1294 it in a pointer to the element type. */
1295 adr = build1 (ADDR_EXPR, ptrtype, exp);
1296 if (!c_mark_addressable (exp))
1297 return error_mark_node;
1298 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1299 return adr;
1301 /* This way is better for a COMPONENT_REF since it can
1302 simplify the offset for a component. */
1303 adr = build_unary_op (ADDR_EXPR, exp, 1);
1304 return convert (ptrtype, adr);
1306 return exp;
1309 /* Perform default promotions for C data used in expressions.
1310 Arrays and functions are converted to pointers;
1311 enumeral types or short or char, to int.
1312 In addition, manifest constants symbols are replaced by their values. */
1314 tree
1315 default_conversion (tree exp)
1317 tree orig_exp;
1318 tree type = TREE_TYPE (exp);
1319 enum tree_code code = TREE_CODE (type);
1321 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1322 return default_function_array_conversion (exp);
1324 /* Constants can be used directly unless they're not loadable. */
1325 if (TREE_CODE (exp) == CONST_DECL)
1326 exp = DECL_INITIAL (exp);
1328 /* Replace a nonvolatile const static variable with its value unless
1329 it is an array, in which case we must be sure that taking the
1330 address of the array produces consistent results. */
1331 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1333 exp = decl_constant_value_for_broken_optimization (exp);
1334 type = TREE_TYPE (exp);
1337 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1338 an lvalue.
1340 Do not use STRIP_NOPS here! It will remove conversions from pointer
1341 to integer and cause infinite recursion. */
1342 orig_exp = exp;
1343 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1344 || (TREE_CODE (exp) == NOP_EXPR
1345 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1346 exp = TREE_OPERAND (exp, 0);
1348 if (TREE_NO_WARNING (orig_exp))
1349 TREE_NO_WARNING (exp) = 1;
1351 /* Normally convert enums to int,
1352 but convert wide enums to something wider. */
1353 if (code == ENUMERAL_TYPE)
1355 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1356 TYPE_PRECISION (integer_type_node)),
1357 ((TYPE_PRECISION (type)
1358 >= TYPE_PRECISION (integer_type_node))
1359 && TYPE_UNSIGNED (type)));
1361 return convert (type, exp);
1364 if (TREE_CODE (exp) == COMPONENT_REF
1365 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1366 /* If it's thinner than an int, promote it like a
1367 c_promoting_integer_type_p, otherwise leave it alone. */
1368 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1369 TYPE_PRECISION (integer_type_node)))
1370 return convert (integer_type_node, exp);
1372 if (c_promoting_integer_type_p (type))
1374 /* Preserve unsignedness if not really getting any wider. */
1375 if (TYPE_UNSIGNED (type)
1376 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1377 return convert (unsigned_type_node, exp);
1379 return convert (integer_type_node, exp);
1382 if (code == VOID_TYPE)
1384 error ("void value not ignored as it ought to be");
1385 return error_mark_node;
1387 return exp;
1390 /* Look up COMPONENT in a structure or union DECL.
1392 If the component name is not found, returns NULL_TREE. Otherwise,
1393 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1394 stepping down the chain to the component, which is in the last
1395 TREE_VALUE of the list. Normally the list is of length one, but if
1396 the component is embedded within (nested) anonymous structures or
1397 unions, the list steps down the chain to the component. */
1399 static tree
1400 lookup_field (tree decl, tree component)
1402 tree type = TREE_TYPE (decl);
1403 tree field;
1405 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1406 to the field elements. Use a binary search on this array to quickly
1407 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1408 will always be set for structures which have many elements. */
1410 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1412 int bot, top, half;
1413 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1415 field = TYPE_FIELDS (type);
1416 bot = 0;
1417 top = TYPE_LANG_SPECIFIC (type)->s->len;
1418 while (top - bot > 1)
1420 half = (top - bot + 1) >> 1;
1421 field = field_array[bot+half];
1423 if (DECL_NAME (field) == NULL_TREE)
1425 /* Step through all anon unions in linear fashion. */
1426 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1428 field = field_array[bot++];
1429 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1430 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1432 tree anon = lookup_field (field, component);
1434 if (anon)
1435 return tree_cons (NULL_TREE, field, anon);
1439 /* Entire record is only anon unions. */
1440 if (bot > top)
1441 return NULL_TREE;
1443 /* Restart the binary search, with new lower bound. */
1444 continue;
1447 if (DECL_NAME (field) == component)
1448 break;
1449 if (DECL_NAME (field) < component)
1450 bot += half;
1451 else
1452 top = bot + half;
1455 if (DECL_NAME (field_array[bot]) == component)
1456 field = field_array[bot];
1457 else if (DECL_NAME (field) != component)
1458 return NULL_TREE;
1460 else
1462 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1464 if (DECL_NAME (field) == NULL_TREE
1465 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1466 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1468 tree anon = lookup_field (field, component);
1470 if (anon)
1471 return tree_cons (NULL_TREE, field, anon);
1474 if (DECL_NAME (field) == component)
1475 break;
1478 if (field == NULL_TREE)
1479 return NULL_TREE;
1482 return tree_cons (NULL_TREE, field, NULL_TREE);
1485 /* Make an expression to refer to the COMPONENT field of
1486 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1488 tree
1489 build_component_ref (tree datum, tree component)
1491 tree type = TREE_TYPE (datum);
1492 enum tree_code code = TREE_CODE (type);
1493 tree field = NULL;
1494 tree ref;
1496 if (!objc_is_public (datum, component))
1497 return error_mark_node;
1499 /* See if there is a field or component with name COMPONENT. */
1501 if (code == RECORD_TYPE || code == UNION_TYPE)
1503 if (!COMPLETE_TYPE_P (type))
1505 c_incomplete_type_error (NULL_TREE, type);
1506 return error_mark_node;
1509 field = lookup_field (datum, component);
1511 if (!field)
1513 error ("%qT has no member named %qs", type,
1514 IDENTIFIER_POINTER (component));
1515 return error_mark_node;
1518 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1519 This might be better solved in future the way the C++ front
1520 end does it - by giving the anonymous entities each a
1521 separate name and type, and then have build_component_ref
1522 recursively call itself. We can't do that here. */
1525 tree subdatum = TREE_VALUE (field);
1527 if (TREE_TYPE (subdatum) == error_mark_node)
1528 return error_mark_node;
1530 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1531 NULL_TREE);
1532 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1533 TREE_READONLY (ref) = 1;
1534 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1535 TREE_THIS_VOLATILE (ref) = 1;
1537 if (TREE_DEPRECATED (subdatum))
1538 warn_deprecated_use (subdatum);
1540 datum = ref;
1542 field = TREE_CHAIN (field);
1544 while (field);
1546 return ref;
1548 else if (code != ERROR_MARK)
1549 error ("request for member %qs in something not a structure or union",
1550 IDENTIFIER_POINTER (component));
1552 return error_mark_node;
1555 /* Given an expression PTR for a pointer, return an expression
1556 for the value pointed to.
1557 ERRORSTRING is the name of the operator to appear in error messages. */
1559 tree
1560 build_indirect_ref (tree ptr, const char *errorstring)
1562 tree pointer = default_conversion (ptr);
1563 tree type = TREE_TYPE (pointer);
1565 if (TREE_CODE (type) == POINTER_TYPE)
1567 if (TREE_CODE (pointer) == ADDR_EXPR
1568 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1569 == TREE_TYPE (type)))
1570 return TREE_OPERAND (pointer, 0);
1571 else
1573 tree t = TREE_TYPE (type);
1574 tree mvt = t;
1575 tree ref;
1577 if (TREE_CODE (mvt) != ARRAY_TYPE)
1578 mvt = TYPE_MAIN_VARIANT (mvt);
1579 ref = build1 (INDIRECT_REF, mvt, pointer);
1581 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1583 error ("dereferencing pointer to incomplete type");
1584 return error_mark_node;
1586 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1587 warning ("dereferencing %<void *%> pointer");
1589 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1590 so that we get the proper error message if the result is used
1591 to assign to. Also, &* is supposed to be a no-op.
1592 And ANSI C seems to specify that the type of the result
1593 should be the const type. */
1594 /* A de-reference of a pointer to const is not a const. It is valid
1595 to change it via some other pointer. */
1596 TREE_READONLY (ref) = TYPE_READONLY (t);
1597 TREE_SIDE_EFFECTS (ref)
1598 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1599 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1600 return ref;
1603 else if (TREE_CODE (pointer) != ERROR_MARK)
1604 error ("invalid type argument of %qs", errorstring);
1605 return error_mark_node;
1608 /* This handles expressions of the form "a[i]", which denotes
1609 an array reference.
1611 This is logically equivalent in C to *(a+i), but we may do it differently.
1612 If A is a variable or a member, we generate a primitive ARRAY_REF.
1613 This avoids forcing the array out of registers, and can work on
1614 arrays that are not lvalues (for example, members of structures returned
1615 by functions). */
1617 tree
1618 build_array_ref (tree array, tree index)
1620 bool swapped = false;
1621 if (TREE_TYPE (array) == error_mark_node
1622 || TREE_TYPE (index) == error_mark_node)
1623 return error_mark_node;
1625 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1626 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1628 tree temp;
1629 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1630 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1632 error ("subscripted value is neither array nor pointer");
1633 return error_mark_node;
1635 temp = array;
1636 array = index;
1637 index = temp;
1638 swapped = true;
1641 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1643 error ("array subscript is not an integer");
1644 return error_mark_node;
1647 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1649 error ("subscripted value is pointer to function");
1650 return error_mark_node;
1653 /* Subscripting with type char is likely to lose on a machine where
1654 chars are signed. So warn on any machine, but optionally. Don't
1655 warn for unsigned char since that type is safe. Don't warn for
1656 signed char because anyone who uses that must have done so
1657 deliberately. ??? Existing practice has also been to warn only
1658 when the char index is syntactically the index, not for
1659 char[array]. */
1660 if (warn_char_subscripts && !swapped
1661 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1662 warning ("array subscript has type %<char%>");
1664 /* Apply default promotions *after* noticing character types. */
1665 index = default_conversion (index);
1667 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1669 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1671 tree rval, type;
1673 /* An array that is indexed by a non-constant
1674 cannot be stored in a register; we must be able to do
1675 address arithmetic on its address.
1676 Likewise an array of elements of variable size. */
1677 if (TREE_CODE (index) != INTEGER_CST
1678 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1679 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1681 if (!c_mark_addressable (array))
1682 return error_mark_node;
1684 /* An array that is indexed by a constant value which is not within
1685 the array bounds cannot be stored in a register either; because we
1686 would get a crash in store_bit_field/extract_bit_field when trying
1687 to access a non-existent part of the register. */
1688 if (TREE_CODE (index) == INTEGER_CST
1689 && TYPE_DOMAIN (TREE_TYPE (array))
1690 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1692 if (!c_mark_addressable (array))
1693 return error_mark_node;
1696 if (pedantic)
1698 tree foo = array;
1699 while (TREE_CODE (foo) == COMPONENT_REF)
1700 foo = TREE_OPERAND (foo, 0);
1701 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1702 pedwarn ("ISO C forbids subscripting %<register%> array");
1703 else if (!flag_isoc99 && !lvalue_p (foo))
1704 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1707 type = TREE_TYPE (TREE_TYPE (array));
1708 if (TREE_CODE (type) != ARRAY_TYPE)
1709 type = TYPE_MAIN_VARIANT (type);
1710 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1711 /* Array ref is const/volatile if the array elements are
1712 or if the array is. */
1713 TREE_READONLY (rval)
1714 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1715 | TREE_READONLY (array));
1716 TREE_SIDE_EFFECTS (rval)
1717 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1718 | TREE_SIDE_EFFECTS (array));
1719 TREE_THIS_VOLATILE (rval)
1720 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1721 /* This was added by rms on 16 Nov 91.
1722 It fixes vol struct foo *a; a->elts[1]
1723 in an inline function.
1724 Hope it doesn't break something else. */
1725 | TREE_THIS_VOLATILE (array));
1726 return require_complete_type (fold (rval));
1728 else
1730 tree ar = default_conversion (array);
1732 if (ar == error_mark_node)
1733 return ar;
1735 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1736 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1738 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1739 "array indexing");
1743 /* Build an external reference to identifier ID. FUN indicates
1744 whether this will be used for a function call. */
1745 tree
1746 build_external_ref (tree id, int fun)
1748 tree ref;
1749 tree decl = lookup_name (id);
1751 /* In Objective-C, an instance variable (ivar) may be preferred to
1752 whatever lookup_name() found. */
1753 decl = objc_lookup_ivar (decl, id);
1755 if (decl && decl != error_mark_node)
1756 ref = decl;
1757 else if (fun)
1758 /* Implicit function declaration. */
1759 ref = implicitly_declare (id);
1760 else if (decl == error_mark_node)
1761 /* Don't complain about something that's already been
1762 complained about. */
1763 return error_mark_node;
1764 else
1766 undeclared_variable (id);
1767 return error_mark_node;
1770 if (TREE_TYPE (ref) == error_mark_node)
1771 return error_mark_node;
1773 if (TREE_DEPRECATED (ref))
1774 warn_deprecated_use (ref);
1776 if (!skip_evaluation)
1777 assemble_external (ref);
1778 TREE_USED (ref) = 1;
1780 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1782 if (!in_sizeof && !in_typeof)
1783 C_DECL_USED (ref) = 1;
1784 else if (DECL_INITIAL (ref) == 0
1785 && DECL_EXTERNAL (ref)
1786 && !TREE_PUBLIC (ref))
1787 record_maybe_used_decl (ref);
1790 if (TREE_CODE (ref) == CONST_DECL)
1792 ref = DECL_INITIAL (ref);
1793 TREE_CONSTANT (ref) = 1;
1794 TREE_INVARIANT (ref) = 1;
1796 else if (current_function_decl != 0
1797 && !DECL_FILE_SCOPE_P (current_function_decl)
1798 && (TREE_CODE (ref) == VAR_DECL
1799 || TREE_CODE (ref) == PARM_DECL
1800 || TREE_CODE (ref) == FUNCTION_DECL))
1802 tree context = decl_function_context (ref);
1804 if (context != 0 && context != current_function_decl)
1805 DECL_NONLOCAL (ref) = 1;
1808 return ref;
1811 /* Record details of decls possibly used inside sizeof or typeof. */
1812 struct maybe_used_decl
1814 /* The decl. */
1815 tree decl;
1816 /* The level seen at (in_sizeof + in_typeof). */
1817 int level;
1818 /* The next one at this level or above, or NULL. */
1819 struct maybe_used_decl *next;
1822 static struct maybe_used_decl *maybe_used_decls;
1824 /* Record that DECL, an undefined static function reference seen
1825 inside sizeof or typeof, might be used if the operand of sizeof is
1826 a VLA type or the operand of typeof is a variably modified
1827 type. */
1829 static void
1830 record_maybe_used_decl (tree decl)
1832 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1833 t->decl = decl;
1834 t->level = in_sizeof + in_typeof;
1835 t->next = maybe_used_decls;
1836 maybe_used_decls = t;
1839 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1840 USED is false, just discard them. If it is true, mark them used
1841 (if no longer inside sizeof or typeof) or move them to the next
1842 level up (if still inside sizeof or typeof). */
1844 void
1845 pop_maybe_used (bool used)
1847 struct maybe_used_decl *p = maybe_used_decls;
1848 int cur_level = in_sizeof + in_typeof;
1849 while (p && p->level > cur_level)
1851 if (used)
1853 if (cur_level == 0)
1854 C_DECL_USED (p->decl) = 1;
1855 else
1856 p->level = cur_level;
1858 p = p->next;
1860 if (!used || cur_level == 0)
1861 maybe_used_decls = p;
1864 /* Return the result of sizeof applied to EXPR. */
1866 struct c_expr
1867 c_expr_sizeof_expr (struct c_expr expr)
1869 struct c_expr ret;
1870 if (expr.value == error_mark_node)
1872 ret.value = error_mark_node;
1873 ret.original_code = ERROR_MARK;
1874 pop_maybe_used (false);
1876 else
1878 ret.value = c_sizeof (TREE_TYPE (expr.value));
1879 ret.original_code = ERROR_MARK;
1880 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1882 return ret;
1885 /* Return the result of sizeof applied to T, a structure for the type
1886 name passed to sizeof (rather than the type itself). */
1888 struct c_expr
1889 c_expr_sizeof_type (struct c_type_name *t)
1891 tree type;
1892 struct c_expr ret;
1893 type = groktypename (t);
1894 ret.value = c_sizeof (type);
1895 ret.original_code = ERROR_MARK;
1896 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1897 return ret;
1900 /* Build a function call to function FUNCTION with parameters PARAMS.
1901 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1902 TREE_VALUE of each node is a parameter-expression.
1903 FUNCTION's data type may be a function type or a pointer-to-function. */
1905 tree
1906 build_function_call (tree function, tree params)
1908 tree fntype, fundecl = 0;
1909 tree coerced_params;
1910 tree name = NULL_TREE, result;
1911 tree tem;
1913 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1914 STRIP_TYPE_NOPS (function);
1916 /* Convert anything with function type to a pointer-to-function. */
1917 if (TREE_CODE (function) == FUNCTION_DECL)
1919 name = DECL_NAME (function);
1921 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1922 (because calling an inline function does not mean the function
1923 needs to be separately compiled). */
1924 fntype = build_type_variant (TREE_TYPE (function),
1925 TREE_READONLY (function),
1926 TREE_THIS_VOLATILE (function));
1927 fundecl = function;
1928 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1930 else
1931 function = default_conversion (function);
1933 fntype = TREE_TYPE (function);
1935 if (TREE_CODE (fntype) == ERROR_MARK)
1936 return error_mark_node;
1938 if (!(TREE_CODE (fntype) == POINTER_TYPE
1939 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1941 error ("called object %qE is not a function", function);
1942 return error_mark_node;
1945 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1946 current_function_returns_abnormally = 1;
1948 /* fntype now gets the type of function pointed to. */
1949 fntype = TREE_TYPE (fntype);
1951 /* Check that the function is called through a compatible prototype.
1952 If it is not, replace the call by a trap, wrapped up in a compound
1953 expression if necessary. This has the nice side-effect to prevent
1954 the tree-inliner from generating invalid assignment trees which may
1955 blow up in the RTL expander later.
1957 ??? This doesn't work for Objective-C because objc_comptypes
1958 refuses to compare function prototypes, yet the compiler appears
1959 to build calls that are flagged as invalid by C's comptypes. */
1960 if (!c_dialect_objc ()
1961 && TREE_CODE (function) == NOP_EXPR
1962 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1963 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1964 && !comptypes (fntype, TREE_TYPE (tem)))
1966 tree return_type = TREE_TYPE (fntype);
1967 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1968 NULL_TREE);
1970 /* This situation leads to run-time undefined behavior. We can't,
1971 therefore, simply error unless we can prove that all possible
1972 executions of the program must execute the code. */
1973 warning ("function called through a non-compatible type");
1975 /* We can, however, treat "undefined" any way we please.
1976 Call abort to encourage the user to fix the program. */
1977 inform ("if this code is reached, the program will abort");
1979 if (VOID_TYPE_P (return_type))
1980 return trap;
1981 else
1983 tree rhs;
1985 if (AGGREGATE_TYPE_P (return_type))
1986 rhs = build_compound_literal (return_type,
1987 build_constructor (return_type,
1988 NULL_TREE));
1989 else
1990 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1992 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
1996 /* Convert the parameters to the types declared in the
1997 function prototype, or apply default promotions. */
1999 coerced_params
2000 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2002 if (coerced_params == error_mark_node)
2003 return error_mark_node;
2005 /* Check that the arguments to the function are valid. */
2007 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
2009 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2010 function, coerced_params, NULL_TREE);
2011 TREE_SIDE_EFFECTS (result) = 1;
2013 if (require_constant_value)
2015 result = fold_initializer (result);
2017 if (TREE_CONSTANT (result)
2018 && (name == NULL_TREE
2019 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2020 pedwarn_init ("initializer element is not constant");
2022 else
2023 result = fold (result);
2025 if (VOID_TYPE_P (TREE_TYPE (result)))
2026 return result;
2027 return require_complete_type (result);
2030 /* Convert the argument expressions in the list VALUES
2031 to the types in the list TYPELIST. The result is a list of converted
2032 argument expressions, unless there are too few arguments in which
2033 case it is error_mark_node.
2035 If TYPELIST is exhausted, or when an element has NULL as its type,
2036 perform the default conversions.
2038 PARMLIST is the chain of parm decls for the function being called.
2039 It may be 0, if that info is not available.
2040 It is used only for generating error messages.
2042 FUNCTION is a tree for the called function. It is used only for
2043 error messages, where it is formatted with %qE.
2045 This is also where warnings about wrong number of args are generated.
2047 Both VALUES and the returned value are chains of TREE_LIST nodes
2048 with the elements of the list in the TREE_VALUE slots of those nodes. */
2050 static tree
2051 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2053 tree typetail, valtail;
2054 tree result = NULL;
2055 int parmnum;
2056 tree selector;
2058 /* Change pointer to function to the function itself for
2059 diagnostics. */
2060 if (TREE_CODE (function) == ADDR_EXPR
2061 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2062 function = TREE_OPERAND (function, 0);
2064 /* Handle an ObjC selector specially for diagnostics. */
2065 selector = objc_message_selector ();
2067 /* Scan the given expressions and types, producing individual
2068 converted arguments and pushing them on RESULT in reverse order. */
2070 for (valtail = values, typetail = typelist, parmnum = 0;
2071 valtail;
2072 valtail = TREE_CHAIN (valtail), parmnum++)
2074 tree type = typetail ? TREE_VALUE (typetail) : 0;
2075 tree val = TREE_VALUE (valtail);
2076 tree rname = function;
2077 int argnum = parmnum + 1;
2079 if (type == void_type_node)
2081 error ("too many arguments to function %qE", function);
2082 break;
2085 if (selector && argnum > 2)
2087 rname = selector;
2088 argnum -= 2;
2091 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2092 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2093 to convert automatically to a pointer. */
2094 if (TREE_CODE (val) == NON_LVALUE_EXPR)
2095 val = TREE_OPERAND (val, 0);
2097 val = default_function_array_conversion (val);
2099 val = require_complete_type (val);
2101 if (type != 0)
2103 /* Formal parm type is specified by a function prototype. */
2104 tree parmval;
2106 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2108 error ("type of formal parameter %d is incomplete", parmnum + 1);
2109 parmval = val;
2111 else
2113 /* Optionally warn about conversions that
2114 differ from the default conversions. */
2115 if (warn_conversion || warn_traditional)
2117 unsigned int formal_prec = TYPE_PRECISION (type);
2119 if (INTEGRAL_TYPE_P (type)
2120 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2121 warning ("passing argument %d of %qE as integer "
2122 "rather than floating due to prototype",
2123 argnum, rname);
2124 if (INTEGRAL_TYPE_P (type)
2125 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2126 warning ("passing argument %d of %qE as integer "
2127 "rather than complex due to prototype",
2128 argnum, rname);
2129 else if (TREE_CODE (type) == COMPLEX_TYPE
2130 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2131 warning ("passing argument %d of %qE as complex "
2132 "rather than floating due to prototype",
2133 argnum, rname);
2134 else if (TREE_CODE (type) == REAL_TYPE
2135 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2136 warning ("passing argument %d of %qE as floating "
2137 "rather than integer due to prototype",
2138 argnum, rname);
2139 else if (TREE_CODE (type) == COMPLEX_TYPE
2140 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2141 warning ("passing argument %d of %qE as complex "
2142 "rather than integer due to prototype",
2143 argnum, rname);
2144 else if (TREE_CODE (type) == REAL_TYPE
2145 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2146 warning ("passing argument %d of %qE as floating "
2147 "rather than complex due to prototype",
2148 argnum, rname);
2149 /* ??? At some point, messages should be written about
2150 conversions between complex types, but that's too messy
2151 to do now. */
2152 else if (TREE_CODE (type) == REAL_TYPE
2153 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2155 /* Warn if any argument is passed as `float',
2156 since without a prototype it would be `double'. */
2157 if (formal_prec == TYPE_PRECISION (float_type_node))
2158 warning ("passing argument %d of %qE as %<float%> "
2159 "rather than %<double%> due to prototype",
2160 argnum, rname);
2162 /* Detect integer changing in width or signedness.
2163 These warnings are only activated with
2164 -Wconversion, not with -Wtraditional. */
2165 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2166 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2168 tree would_have_been = default_conversion (val);
2169 tree type1 = TREE_TYPE (would_have_been);
2171 if (TREE_CODE (type) == ENUMERAL_TYPE
2172 && (TYPE_MAIN_VARIANT (type)
2173 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2174 /* No warning if function asks for enum
2175 and the actual arg is that enum type. */
2177 else if (formal_prec != TYPE_PRECISION (type1))
2178 warning ("passing argument %d of %qE with different "
2179 "width due to prototype", argnum, rname);
2180 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2182 /* Don't complain if the formal parameter type
2183 is an enum, because we can't tell now whether
2184 the value was an enum--even the same enum. */
2185 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2187 else if (TREE_CODE (val) == INTEGER_CST
2188 && int_fits_type_p (val, type))
2189 /* Change in signedness doesn't matter
2190 if a constant value is unaffected. */
2192 /* Likewise for a constant in a NOP_EXPR. */
2193 else if (TREE_CODE (val) == NOP_EXPR
2194 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2195 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2197 /* If the value is extended from a narrower
2198 unsigned type, it doesn't matter whether we
2199 pass it as signed or unsigned; the value
2200 certainly is the same either way. */
2201 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2202 && TYPE_UNSIGNED (TREE_TYPE (val)))
2204 else if (TYPE_UNSIGNED (type))
2205 warning ("passing argument %d of %qE as unsigned "
2206 "due to prototype", argnum, rname);
2207 else
2208 warning ("passing argument %d of %qE as signed "
2209 "due to prototype", argnum, rname);
2213 parmval = convert_for_assignment (type, val, ic_argpass,
2214 fundecl, function,
2215 parmnum + 1);
2217 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2218 && INTEGRAL_TYPE_P (type)
2219 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2220 parmval = default_conversion (parmval);
2222 result = tree_cons (NULL_TREE, parmval, result);
2224 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2225 && (TYPE_PRECISION (TREE_TYPE (val))
2226 < TYPE_PRECISION (double_type_node)))
2227 /* Convert `float' to `double'. */
2228 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2229 else
2230 /* Convert `short' and `char' to full-size `int'. */
2231 result = tree_cons (NULL_TREE, default_conversion (val), result);
2233 if (typetail)
2234 typetail = TREE_CHAIN (typetail);
2237 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2239 error ("too few arguments to function %qE", function);
2240 return error_mark_node;
2243 return nreverse (result);
2246 /* This is the entry point used by the parser
2247 for binary operators in the input.
2248 In addition to constructing the expression,
2249 we check for operands that were written with other binary operators
2250 in a way that is likely to confuse the user. */
2252 struct c_expr
2253 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2254 struct c_expr arg2)
2256 struct c_expr result;
2258 enum tree_code code1 = arg1.original_code;
2259 enum tree_code code2 = arg2.original_code;
2261 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2262 result.original_code = code;
2264 if (TREE_CODE (result.value) == ERROR_MARK)
2265 return result;
2267 /* Check for cases such as x+y<<z which users are likely
2268 to misinterpret. */
2269 if (warn_parentheses)
2271 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2273 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2274 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2275 warning ("suggest parentheses around + or - inside shift");
2278 if (code == TRUTH_ORIF_EXPR)
2280 if (code1 == TRUTH_ANDIF_EXPR
2281 || code2 == TRUTH_ANDIF_EXPR)
2282 warning ("suggest parentheses around && within ||");
2285 if (code == BIT_IOR_EXPR)
2287 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2288 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2289 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2290 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2291 warning ("suggest parentheses around arithmetic in operand of |");
2292 /* Check cases like x|y==z */
2293 if (TREE_CODE_CLASS (code1) == tcc_comparison
2294 || TREE_CODE_CLASS (code2) == tcc_comparison)
2295 warning ("suggest parentheses around comparison in operand of |");
2298 if (code == BIT_XOR_EXPR)
2300 if (code1 == BIT_AND_EXPR
2301 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2302 || code2 == BIT_AND_EXPR
2303 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2304 warning ("suggest parentheses around arithmetic in operand of ^");
2305 /* Check cases like x^y==z */
2306 if (TREE_CODE_CLASS (code1) == tcc_comparison
2307 || TREE_CODE_CLASS (code2) == tcc_comparison)
2308 warning ("suggest parentheses around comparison in operand of ^");
2311 if (code == BIT_AND_EXPR)
2313 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2314 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2315 warning ("suggest parentheses around + or - in operand of &");
2316 /* Check cases like x&y==z */
2317 if (TREE_CODE_CLASS (code1) == tcc_comparison
2318 || TREE_CODE_CLASS (code2) == tcc_comparison)
2319 warning ("suggest parentheses around comparison in operand of &");
2321 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2322 if (TREE_CODE_CLASS (code) == tcc_comparison
2323 && (TREE_CODE_CLASS (code1) == tcc_comparison
2324 || TREE_CODE_CLASS (code2) == tcc_comparison))
2325 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2329 unsigned_conversion_warning (result.value, arg1.value);
2330 unsigned_conversion_warning (result.value, arg2.value);
2331 overflow_warning (result.value);
2333 return result;
2336 /* Return a tree for the difference of pointers OP0 and OP1.
2337 The resulting tree has type int. */
2339 static tree
2340 pointer_diff (tree op0, tree op1)
2342 tree restype = ptrdiff_type_node;
2344 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2345 tree con0, con1, lit0, lit1;
2346 tree orig_op1 = op1;
2348 if (pedantic || warn_pointer_arith)
2350 if (TREE_CODE (target_type) == VOID_TYPE)
2351 pedwarn ("pointer of type %<void *%> used in subtraction");
2352 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2353 pedwarn ("pointer to a function used in subtraction");
2356 /* If the conversion to ptrdiff_type does anything like widening or
2357 converting a partial to an integral mode, we get a convert_expression
2358 that is in the way to do any simplifications.
2359 (fold-const.c doesn't know that the extra bits won't be needed.
2360 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2361 different mode in place.)
2362 So first try to find a common term here 'by hand'; we want to cover
2363 at least the cases that occur in legal static initializers. */
2364 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2365 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2367 if (TREE_CODE (con0) == PLUS_EXPR)
2369 lit0 = TREE_OPERAND (con0, 1);
2370 con0 = TREE_OPERAND (con0, 0);
2372 else
2373 lit0 = integer_zero_node;
2375 if (TREE_CODE (con1) == PLUS_EXPR)
2377 lit1 = TREE_OPERAND (con1, 1);
2378 con1 = TREE_OPERAND (con1, 0);
2380 else
2381 lit1 = integer_zero_node;
2383 if (operand_equal_p (con0, con1, 0))
2385 op0 = lit0;
2386 op1 = lit1;
2390 /* First do the subtraction as integers;
2391 then drop through to build the divide operator.
2392 Do not do default conversions on the minus operator
2393 in case restype is a short type. */
2395 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2396 convert (restype, op1), 0);
2397 /* This generates an error if op1 is pointer to incomplete type. */
2398 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2399 error ("arithmetic on pointer to an incomplete type");
2401 /* This generates an error if op0 is pointer to incomplete type. */
2402 op1 = c_size_in_bytes (target_type);
2404 /* Divide by the size, in easiest possible way. */
2405 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2408 /* Construct and perhaps optimize a tree representation
2409 for a unary operation. CODE, a tree_code, specifies the operation
2410 and XARG is the operand.
2411 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2412 the default promotions (such as from short to int).
2413 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2414 allows non-lvalues; this is only used to handle conversion of non-lvalue
2415 arrays to pointers in C99. */
2417 tree
2418 build_unary_op (enum tree_code code, tree xarg, int flag)
2420 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2421 tree arg = xarg;
2422 tree argtype = 0;
2423 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2424 tree val;
2425 int noconvert = flag;
2427 if (typecode == ERROR_MARK)
2428 return error_mark_node;
2429 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2430 typecode = INTEGER_TYPE;
2432 switch (code)
2434 case CONVERT_EXPR:
2435 /* This is used for unary plus, because a CONVERT_EXPR
2436 is enough to prevent anybody from looking inside for
2437 associativity, but won't generate any code. */
2438 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2439 || typecode == COMPLEX_TYPE
2440 || typecode == VECTOR_TYPE))
2442 error ("wrong type argument to unary plus");
2443 return error_mark_node;
2445 else if (!noconvert)
2446 arg = default_conversion (arg);
2447 arg = non_lvalue (arg);
2448 break;
2450 case NEGATE_EXPR:
2451 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2452 || typecode == COMPLEX_TYPE
2453 || typecode == VECTOR_TYPE))
2455 error ("wrong type argument to unary minus");
2456 return error_mark_node;
2458 else if (!noconvert)
2459 arg = default_conversion (arg);
2460 break;
2462 case BIT_NOT_EXPR:
2463 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2465 if (!noconvert)
2466 arg = default_conversion (arg);
2468 else if (typecode == COMPLEX_TYPE)
2470 code = CONJ_EXPR;
2471 if (pedantic)
2472 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2473 if (!noconvert)
2474 arg = default_conversion (arg);
2476 else
2478 error ("wrong type argument to bit-complement");
2479 return error_mark_node;
2481 break;
2483 case ABS_EXPR:
2484 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2486 error ("wrong type argument to abs");
2487 return error_mark_node;
2489 else if (!noconvert)
2490 arg = default_conversion (arg);
2491 break;
2493 case CONJ_EXPR:
2494 /* Conjugating a real value is a no-op, but allow it anyway. */
2495 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2496 || typecode == COMPLEX_TYPE))
2498 error ("wrong type argument to conjugation");
2499 return error_mark_node;
2501 else if (!noconvert)
2502 arg = default_conversion (arg);
2503 break;
2505 case TRUTH_NOT_EXPR:
2506 if (typecode != INTEGER_TYPE
2507 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2508 && typecode != COMPLEX_TYPE
2509 /* These will convert to a pointer. */
2510 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2512 error ("wrong type argument to unary exclamation mark");
2513 return error_mark_node;
2515 arg = lang_hooks.truthvalue_conversion (arg);
2516 return invert_truthvalue (arg);
2518 case NOP_EXPR:
2519 break;
2521 case REALPART_EXPR:
2522 if (TREE_CODE (arg) == COMPLEX_CST)
2523 return TREE_REALPART (arg);
2524 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2525 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2526 else
2527 return arg;
2529 case IMAGPART_EXPR:
2530 if (TREE_CODE (arg) == COMPLEX_CST)
2531 return TREE_IMAGPART (arg);
2532 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2533 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2534 else
2535 return convert (TREE_TYPE (arg), integer_zero_node);
2537 case PREINCREMENT_EXPR:
2538 case POSTINCREMENT_EXPR:
2539 case PREDECREMENT_EXPR:
2540 case POSTDECREMENT_EXPR:
2542 /* Increment or decrement the real part of the value,
2543 and don't change the imaginary part. */
2544 if (typecode == COMPLEX_TYPE)
2546 tree real, imag;
2548 if (pedantic)
2549 pedwarn ("ISO C does not support %<++%> and %<--%>"
2550 " on complex types");
2552 arg = stabilize_reference (arg);
2553 real = build_unary_op (REALPART_EXPR, arg, 1);
2554 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2555 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2556 build_unary_op (code, real, 1), imag);
2559 /* Report invalid types. */
2561 if (typecode != POINTER_TYPE
2562 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2564 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2565 error ("wrong type argument to increment");
2566 else
2567 error ("wrong type argument to decrement");
2569 return error_mark_node;
2573 tree inc;
2574 tree result_type = TREE_TYPE (arg);
2576 arg = get_unwidened (arg, 0);
2577 argtype = TREE_TYPE (arg);
2579 /* Compute the increment. */
2581 if (typecode == POINTER_TYPE)
2583 /* If pointer target is an undefined struct,
2584 we just cannot know how to do the arithmetic. */
2585 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2587 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2588 error ("increment of pointer to unknown structure");
2589 else
2590 error ("decrement of pointer to unknown structure");
2592 else if ((pedantic || warn_pointer_arith)
2593 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2594 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2596 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2597 pedwarn ("wrong type argument to increment");
2598 else
2599 pedwarn ("wrong type argument to decrement");
2602 inc = c_size_in_bytes (TREE_TYPE (result_type));
2604 else
2605 inc = integer_one_node;
2607 inc = convert (argtype, inc);
2609 /* Complain about anything else that is not a true lvalue. */
2610 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2611 || code == POSTINCREMENT_EXPR)
2612 ? lv_increment
2613 : lv_decrement)))
2614 return error_mark_node;
2616 /* Report a read-only lvalue. */
2617 if (TREE_READONLY (arg))
2618 readonly_error (arg,
2619 ((code == PREINCREMENT_EXPR
2620 || code == POSTINCREMENT_EXPR)
2621 ? lv_increment : lv_decrement));
2623 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2624 val = boolean_increment (code, arg);
2625 else
2626 val = build2 (code, TREE_TYPE (arg), arg, inc);
2627 TREE_SIDE_EFFECTS (val) = 1;
2628 val = convert (result_type, val);
2629 if (TREE_CODE (val) != code)
2630 TREE_NO_WARNING (val) = 1;
2631 return val;
2634 case ADDR_EXPR:
2635 /* Note that this operation never does default_conversion. */
2637 /* Let &* cancel out to simplify resulting code. */
2638 if (TREE_CODE (arg) == INDIRECT_REF)
2640 /* Don't let this be an lvalue. */
2641 if (lvalue_p (TREE_OPERAND (arg, 0)))
2642 return non_lvalue (TREE_OPERAND (arg, 0));
2643 return TREE_OPERAND (arg, 0);
2646 /* For &x[y], return x+y */
2647 if (TREE_CODE (arg) == ARRAY_REF)
2649 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2650 return error_mark_node;
2651 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2652 TREE_OPERAND (arg, 1), 1);
2655 /* Anything not already handled and not a true memory reference
2656 or a non-lvalue array is an error. */
2657 else if (typecode != FUNCTION_TYPE && !flag
2658 && !lvalue_or_else (arg, lv_addressof))
2659 return error_mark_node;
2661 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2662 argtype = TREE_TYPE (arg);
2664 /* If the lvalue is const or volatile, merge that into the type
2665 to which the address will point. Note that you can't get a
2666 restricted pointer by taking the address of something, so we
2667 only have to deal with `const' and `volatile' here. */
2668 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2669 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2670 argtype = c_build_type_variant (argtype,
2671 TREE_READONLY (arg),
2672 TREE_THIS_VOLATILE (arg));
2674 if (!c_mark_addressable (arg))
2675 return error_mark_node;
2677 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2678 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2680 argtype = build_pointer_type (argtype);
2682 /* ??? Cope with user tricks that amount to offsetof. Delete this
2683 when we have proper support for integer constant expressions. */
2684 val = get_base_address (arg);
2685 if (val && TREE_CODE (val) == INDIRECT_REF
2686 && integer_zerop (TREE_OPERAND (val, 0)))
2687 return fold_convert (argtype, fold_offsetof (arg));
2689 val = build1 (ADDR_EXPR, argtype, arg);
2691 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2692 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2694 return val;
2696 default:
2697 break;
2700 if (argtype == 0)
2701 argtype = TREE_TYPE (arg);
2702 val = build1 (code, argtype, arg);
2703 return require_constant_value ? fold_initializer (val) : fold (val);
2706 /* Return nonzero if REF is an lvalue valid for this language.
2707 Lvalues can be assigned, unless their type has TYPE_READONLY.
2708 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2711 lvalue_p (tree ref)
2713 enum tree_code code = TREE_CODE (ref);
2715 switch (code)
2717 case REALPART_EXPR:
2718 case IMAGPART_EXPR:
2719 case COMPONENT_REF:
2720 return lvalue_p (TREE_OPERAND (ref, 0));
2722 case COMPOUND_LITERAL_EXPR:
2723 case STRING_CST:
2724 return 1;
2726 case INDIRECT_REF:
2727 case ARRAY_REF:
2728 case VAR_DECL:
2729 case PARM_DECL:
2730 case RESULT_DECL:
2731 case ERROR_MARK:
2732 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2733 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2735 case BIND_EXPR:
2736 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2738 default:
2739 return 0;
2743 /* Give an error for storing in something that is 'const'. */
2745 static void
2746 readonly_error (tree arg, enum lvalue_use use)
2748 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2749 /* Using this macro rather than (for example) arrays of messages
2750 ensures that all the format strings are checked at compile
2751 time. */
2752 #define READONLY_MSG(A, I, D) (use == lv_assign \
2753 ? (A) \
2754 : (use == lv_increment ? (I) : (D)))
2755 if (TREE_CODE (arg) == COMPONENT_REF)
2757 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2758 readonly_error (TREE_OPERAND (arg, 0), use);
2759 else
2760 error (READONLY_MSG (N_("assignment of read-only member %qs"),
2761 N_("increment of read-only member %qs"),
2762 N_("decrement of read-only member %qs")),
2763 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2765 else if (TREE_CODE (arg) == VAR_DECL)
2766 error (READONLY_MSG (N_("assignment of read-only variable %qs"),
2767 N_("increment of read-only variable %qs"),
2768 N_("decrement of read-only variable %qs")),
2769 IDENTIFIER_POINTER (DECL_NAME (arg)));
2770 else
2771 error (READONLY_MSG (N_("assignment of read-only location"),
2772 N_("increment of read-only location"),
2773 N_("decrement of read-only location")));
2776 /* Mark EXP saying that we need to be able to take the
2777 address of it; it should not be allocated in a register.
2778 Returns true if successful. */
2780 bool
2781 c_mark_addressable (tree exp)
2783 tree x = exp;
2785 while (1)
2786 switch (TREE_CODE (x))
2788 case COMPONENT_REF:
2789 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2791 error
2792 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2793 return false;
2796 /* ... fall through ... */
2798 case ADDR_EXPR:
2799 case ARRAY_REF:
2800 case REALPART_EXPR:
2801 case IMAGPART_EXPR:
2802 x = TREE_OPERAND (x, 0);
2803 break;
2805 case COMPOUND_LITERAL_EXPR:
2806 case CONSTRUCTOR:
2807 TREE_ADDRESSABLE (x) = 1;
2808 return true;
2810 case VAR_DECL:
2811 case CONST_DECL:
2812 case PARM_DECL:
2813 case RESULT_DECL:
2814 if (C_DECL_REGISTER (x)
2815 && DECL_NONLOCAL (x))
2817 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2819 error
2820 ("global register variable %qD used in nested function", x);
2821 return false;
2823 pedwarn ("register variable %qD used in nested function", x);
2825 else if (C_DECL_REGISTER (x))
2827 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2828 error ("address of global register variable %qD requested", x);
2829 else
2830 error ("address of register variable %qD requested", x);
2831 return false;
2834 /* drops in */
2835 case FUNCTION_DECL:
2836 TREE_ADDRESSABLE (x) = 1;
2837 /* drops out */
2838 default:
2839 return true;
2843 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2845 tree
2846 build_conditional_expr (tree ifexp, tree op1, tree op2)
2848 tree type1;
2849 tree type2;
2850 enum tree_code code1;
2851 enum tree_code code2;
2852 tree result_type = NULL;
2853 tree orig_op1 = op1, orig_op2 = op2;
2855 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2857 /* Promote both alternatives. */
2859 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2860 op1 = default_conversion (op1);
2861 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2862 op2 = default_conversion (op2);
2864 if (TREE_CODE (ifexp) == ERROR_MARK
2865 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2866 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2867 return error_mark_node;
2869 type1 = TREE_TYPE (op1);
2870 code1 = TREE_CODE (type1);
2871 type2 = TREE_TYPE (op2);
2872 code2 = TREE_CODE (type2);
2874 /* C90 does not permit non-lvalue arrays in conditional expressions.
2875 In C99 they will be pointers by now. */
2876 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2878 error ("non-lvalue array in conditional expression");
2879 return error_mark_node;
2882 /* Quickly detect the usual case where op1 and op2 have the same type
2883 after promotion. */
2884 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2886 if (type1 == type2)
2887 result_type = type1;
2888 else
2889 result_type = TYPE_MAIN_VARIANT (type1);
2891 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2892 || code1 == COMPLEX_TYPE)
2893 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2894 || code2 == COMPLEX_TYPE))
2896 result_type = common_type (type1, type2);
2898 /* If -Wsign-compare, warn here if type1 and type2 have
2899 different signedness. We'll promote the signed to unsigned
2900 and later code won't know it used to be different.
2901 Do this check on the original types, so that explicit casts
2902 will be considered, but default promotions won't. */
2903 if (warn_sign_compare && !skip_evaluation)
2905 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2906 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2908 if (unsigned_op1 ^ unsigned_op2)
2910 /* Do not warn if the result type is signed, since the
2911 signed type will only be chosen if it can represent
2912 all the values of the unsigned type. */
2913 if (!TYPE_UNSIGNED (result_type))
2914 /* OK */;
2915 /* Do not warn if the signed quantity is an unsuffixed
2916 integer literal (or some static constant expression
2917 involving such literals) and it is non-negative. */
2918 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2919 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2920 /* OK */;
2921 else
2922 warning ("signed and unsigned type in conditional expression");
2926 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2928 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2929 pedwarn ("ISO C forbids conditional expr with only one void side");
2930 result_type = void_type_node;
2932 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2934 if (comp_target_types (type1, type2, 1))
2935 result_type = common_pointer_type (type1, type2);
2936 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2937 && TREE_CODE (orig_op1) != NOP_EXPR)
2938 result_type = qualify_type (type2, type1);
2939 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2940 && TREE_CODE (orig_op2) != NOP_EXPR)
2941 result_type = qualify_type (type1, type2);
2942 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2944 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2945 pedwarn ("ISO C forbids conditional expr between "
2946 "%<void *%> and function pointer");
2947 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2948 TREE_TYPE (type2)));
2950 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2952 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2953 pedwarn ("ISO C forbids conditional expr between "
2954 "%<void *%> and function pointer");
2955 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2956 TREE_TYPE (type1)));
2958 else
2960 pedwarn ("pointer type mismatch in conditional expression");
2961 result_type = build_pointer_type (void_type_node);
2964 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2966 if (!integer_zerop (op2))
2967 pedwarn ("pointer/integer type mismatch in conditional expression");
2968 else
2970 op2 = null_pointer_node;
2972 result_type = type1;
2974 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2976 if (!integer_zerop (op1))
2977 pedwarn ("pointer/integer type mismatch in conditional expression");
2978 else
2980 op1 = null_pointer_node;
2982 result_type = type2;
2985 if (!result_type)
2987 if (flag_cond_mismatch)
2988 result_type = void_type_node;
2989 else
2991 error ("type mismatch in conditional expression");
2992 return error_mark_node;
2996 /* Merge const and volatile flags of the incoming types. */
2997 result_type
2998 = build_type_variant (result_type,
2999 TREE_READONLY (op1) || TREE_READONLY (op2),
3000 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3002 if (result_type != TREE_TYPE (op1))
3003 op1 = convert_and_check (result_type, op1);
3004 if (result_type != TREE_TYPE (op2))
3005 op2 = convert_and_check (result_type, op2);
3007 if (TREE_CODE (ifexp) == INTEGER_CST)
3008 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3010 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3013 /* Return a compound expression that performs two expressions and
3014 returns the value of the second of them. */
3016 tree
3017 build_compound_expr (tree expr1, tree expr2)
3019 /* Convert arrays and functions to pointers. */
3020 expr2 = default_function_array_conversion (expr2);
3022 if (!TREE_SIDE_EFFECTS (expr1))
3024 /* The left-hand operand of a comma expression is like an expression
3025 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3026 any side-effects, unless it was explicitly cast to (void). */
3027 if (warn_unused_value
3028 && !(TREE_CODE (expr1) == CONVERT_EXPR
3029 && VOID_TYPE_P (TREE_TYPE (expr1))))
3030 warning ("left-hand operand of comma expression has no effect");
3033 /* With -Wunused, we should also warn if the left-hand operand does have
3034 side-effects, but computes a value which is not used. For example, in
3035 `foo() + bar(), baz()' the result of the `+' operator is not used,
3036 so we should issue a warning. */
3037 else if (warn_unused_value)
3038 warn_if_unused_value (expr1, input_location);
3040 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3043 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3045 tree
3046 build_c_cast (tree type, tree expr)
3048 tree value = expr;
3050 if (type == error_mark_node || expr == error_mark_node)
3051 return error_mark_node;
3053 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3054 only in <protocol> qualifications. But when constructing cast expressions,
3055 the protocols do matter and must be kept around. */
3056 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3057 return build1 (NOP_EXPR, type, expr);
3059 type = TYPE_MAIN_VARIANT (type);
3061 if (TREE_CODE (type) == ARRAY_TYPE)
3063 error ("cast specifies array type");
3064 return error_mark_node;
3067 if (TREE_CODE (type) == FUNCTION_TYPE)
3069 error ("cast specifies function type");
3070 return error_mark_node;
3073 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3075 if (pedantic)
3077 if (TREE_CODE (type) == RECORD_TYPE
3078 || TREE_CODE (type) == UNION_TYPE)
3079 pedwarn ("ISO C forbids casting nonscalar to the same type");
3082 else if (TREE_CODE (type) == UNION_TYPE)
3084 tree field;
3085 value = default_function_array_conversion (value);
3087 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3088 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3089 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3090 break;
3092 if (field)
3094 tree t;
3096 if (pedantic)
3097 pedwarn ("ISO C forbids casts to union type");
3098 t = digest_init (type,
3099 build_constructor (type,
3100 build_tree_list (field, value)),
3101 true, 0);
3102 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3103 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3104 return t;
3106 error ("cast to union type from type not present in union");
3107 return error_mark_node;
3109 else
3111 tree otype, ovalue;
3113 /* If casting to void, avoid the error that would come
3114 from default_conversion in the case of a non-lvalue array. */
3115 if (type == void_type_node)
3116 return build1 (CONVERT_EXPR, type, value);
3118 /* Convert functions and arrays to pointers,
3119 but don't convert any other types. */
3120 value = default_function_array_conversion (value);
3121 otype = TREE_TYPE (value);
3123 /* Optionally warn about potentially worrisome casts. */
3125 if (warn_cast_qual
3126 && TREE_CODE (type) == POINTER_TYPE
3127 && TREE_CODE (otype) == POINTER_TYPE)
3129 tree in_type = type;
3130 tree in_otype = otype;
3131 int added = 0;
3132 int discarded = 0;
3134 /* Check that the qualifiers on IN_TYPE are a superset of
3135 the qualifiers of IN_OTYPE. The outermost level of
3136 POINTER_TYPE nodes is uninteresting and we stop as soon
3137 as we hit a non-POINTER_TYPE node on either type. */
3140 in_otype = TREE_TYPE (in_otype);
3141 in_type = TREE_TYPE (in_type);
3143 /* GNU C allows cv-qualified function types. 'const'
3144 means the function is very pure, 'volatile' means it
3145 can't return. We need to warn when such qualifiers
3146 are added, not when they're taken away. */
3147 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3148 && TREE_CODE (in_type) == FUNCTION_TYPE)
3149 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3150 else
3151 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3153 while (TREE_CODE (in_type) == POINTER_TYPE
3154 && TREE_CODE (in_otype) == POINTER_TYPE);
3156 if (added)
3157 warning ("cast adds new qualifiers to function type");
3159 if (discarded)
3160 /* There are qualifiers present in IN_OTYPE that are not
3161 present in IN_TYPE. */
3162 warning ("cast discards qualifiers from pointer target type");
3165 /* Warn about possible alignment problems. */
3166 if (STRICT_ALIGNMENT && warn_cast_align
3167 && TREE_CODE (type) == POINTER_TYPE
3168 && TREE_CODE (otype) == POINTER_TYPE
3169 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3170 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3171 /* Don't warn about opaque types, where the actual alignment
3172 restriction is unknown. */
3173 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3174 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3175 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3176 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3177 warning ("cast increases required alignment of target type");
3179 if (TREE_CODE (type) == INTEGER_TYPE
3180 && TREE_CODE (otype) == POINTER_TYPE
3181 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3182 && !TREE_CONSTANT (value))
3183 warning ("cast from pointer to integer of different size");
3185 if (warn_bad_function_cast
3186 && TREE_CODE (value) == CALL_EXPR
3187 && TREE_CODE (type) != TREE_CODE (otype))
3188 warning ("cast from function call of type %qT to non-matching "
3189 "type %qT", otype, type);
3191 if (TREE_CODE (type) == POINTER_TYPE
3192 && TREE_CODE (otype) == INTEGER_TYPE
3193 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3194 /* Don't warn about converting any constant. */
3195 && !TREE_CONSTANT (value))
3196 warning ("cast to pointer from integer of different size");
3198 if (TREE_CODE (type) == POINTER_TYPE
3199 && TREE_CODE (otype) == POINTER_TYPE
3200 && TREE_CODE (expr) == ADDR_EXPR
3201 && DECL_P (TREE_OPERAND (expr, 0))
3202 && flag_strict_aliasing && warn_strict_aliasing
3203 && !VOID_TYPE_P (TREE_TYPE (type)))
3205 /* Casting the address of a decl to non void pointer. Warn
3206 if the cast breaks type based aliasing. */
3207 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3208 warning ("type-punning to incomplete type might break strict-aliasing rules");
3209 else
3211 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3212 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3214 if (!alias_sets_conflict_p (set1, set2))
3215 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3216 else if (warn_strict_aliasing > 1
3217 && !alias_sets_might_conflict_p (set1, set2))
3218 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3222 /* If pedantic, warn for conversions between function and object
3223 pointer types, except for converting a null pointer constant
3224 to function pointer type. */
3225 if (pedantic
3226 && TREE_CODE (type) == POINTER_TYPE
3227 && TREE_CODE (otype) == POINTER_TYPE
3228 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3229 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3230 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3232 if (pedantic
3233 && TREE_CODE (type) == POINTER_TYPE
3234 && TREE_CODE (otype) == POINTER_TYPE
3235 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3236 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3237 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3238 && TREE_CODE (expr) != NOP_EXPR))
3239 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3241 ovalue = value;
3242 value = convert (type, value);
3244 /* Ignore any integer overflow caused by the cast. */
3245 if (TREE_CODE (value) == INTEGER_CST)
3247 if (EXPR_P (ovalue))
3248 /* If OVALUE had overflow set, then so will VALUE, so it
3249 is safe to overwrite. */
3250 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3251 else
3252 TREE_OVERFLOW (value) = 0;
3254 if (CONSTANT_CLASS_P (ovalue))
3255 /* Similarly, constant_overflow cannot have become
3256 cleared. */
3257 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3261 /* Don't let a cast be an lvalue. */
3262 if (value == expr)
3263 value = non_lvalue (value);
3265 return value;
3268 /* Interpret a cast of expression EXPR to type TYPE. */
3269 tree
3270 c_cast_expr (struct c_type_name *type_name, tree expr)
3272 tree type;
3273 int saved_wsp = warn_strict_prototypes;
3275 /* This avoids warnings about unprototyped casts on
3276 integers. E.g. "#define SIG_DFL (void(*)())0". */
3277 if (TREE_CODE (expr) == INTEGER_CST)
3278 warn_strict_prototypes = 0;
3279 type = groktypename (type_name);
3280 warn_strict_prototypes = saved_wsp;
3282 return build_c_cast (type, expr);
3286 /* Build an assignment expression of lvalue LHS from value RHS.
3287 MODIFYCODE is the code for a binary operator that we use
3288 to combine the old value of LHS with RHS to get the new value.
3289 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3291 tree
3292 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3294 tree result;
3295 tree newrhs;
3296 tree lhstype = TREE_TYPE (lhs);
3297 tree olhstype = lhstype;
3299 /* Types that aren't fully specified cannot be used in assignments. */
3300 lhs = require_complete_type (lhs);
3302 /* Avoid duplicate error messages from operands that had errors. */
3303 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3304 return error_mark_node;
3306 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3307 /* Do not use STRIP_NOPS here. We do not want an enumerator
3308 whose value is 0 to count as a null pointer constant. */
3309 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3310 rhs = TREE_OPERAND (rhs, 0);
3312 newrhs = rhs;
3314 /* If a binary op has been requested, combine the old LHS value with the RHS
3315 producing the value we should actually store into the LHS. */
3317 if (modifycode != NOP_EXPR)
3319 lhs = stabilize_reference (lhs);
3320 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3323 if (!lvalue_or_else (lhs, lv_assign))
3324 return error_mark_node;
3326 /* Give an error for storing in something that is 'const'. */
3328 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3329 || ((TREE_CODE (lhstype) == RECORD_TYPE
3330 || TREE_CODE (lhstype) == UNION_TYPE)
3331 && C_TYPE_FIELDS_READONLY (lhstype)))
3332 readonly_error (lhs, lv_assign);
3334 /* If storing into a structure or union member,
3335 it has probably been given type `int'.
3336 Compute the type that would go with
3337 the actual amount of storage the member occupies. */
3339 if (TREE_CODE (lhs) == COMPONENT_REF
3340 && (TREE_CODE (lhstype) == INTEGER_TYPE
3341 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3342 || TREE_CODE (lhstype) == REAL_TYPE
3343 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3344 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3346 /* If storing in a field that is in actuality a short or narrower than one,
3347 we must store in the field in its actual type. */
3349 if (lhstype != TREE_TYPE (lhs))
3351 lhs = copy_node (lhs);
3352 TREE_TYPE (lhs) = lhstype;
3355 /* Convert new value to destination type. */
3357 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3358 NULL_TREE, NULL_TREE, 0);
3359 if (TREE_CODE (newrhs) == ERROR_MARK)
3360 return error_mark_node;
3362 /* Scan operands. */
3364 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3365 TREE_SIDE_EFFECTS (result) = 1;
3367 /* If we got the LHS in a different type for storing in,
3368 convert the result back to the nominal type of LHS
3369 so that the value we return always has the same type
3370 as the LHS argument. */
3372 if (olhstype == TREE_TYPE (result))
3373 return result;
3374 return convert_for_assignment (olhstype, result, ic_assign,
3375 NULL_TREE, NULL_TREE, 0);
3378 /* Convert value RHS to type TYPE as preparation for an assignment
3379 to an lvalue of type TYPE.
3380 The real work of conversion is done by `convert'.
3381 The purpose of this function is to generate error messages
3382 for assignments that are not allowed in C.
3383 ERRTYPE says whether it is argument passing, assignment,
3384 initialization or return.
3386 FUNCTION is a tree for the function being called.
3387 PARMNUM is the number of the argument, for printing in error messages. */
3389 static tree
3390 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3391 tree fundecl, tree function, int parmnum)
3393 enum tree_code codel = TREE_CODE (type);
3394 tree rhstype;
3395 enum tree_code coder;
3396 tree rname = NULL_TREE;
3398 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3400 tree selector;
3401 /* Change pointer to function to the function itself for
3402 diagnostics. */
3403 if (TREE_CODE (function) == ADDR_EXPR
3404 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3405 function = TREE_OPERAND (function, 0);
3407 /* Handle an ObjC selector specially for diagnostics. */
3408 selector = objc_message_selector ();
3409 rname = function;
3410 if (selector && parmnum > 2)
3412 rname = selector;
3413 parmnum -= 2;
3417 /* This macro is used to emit diagnostics to ensure that all format
3418 strings are complete sentences, visible to gettext and checked at
3419 compile time. */
3420 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3421 do { \
3422 switch (errtype) \
3424 case ic_argpass: \
3425 pedwarn (AR, parmnum, rname); \
3426 break; \
3427 case ic_argpass_nonproto: \
3428 warning (AR, parmnum, rname); \
3429 break; \
3430 case ic_assign: \
3431 pedwarn (AS); \
3432 break; \
3433 case ic_init: \
3434 pedwarn (IN); \
3435 break; \
3436 case ic_return: \
3437 pedwarn (RE); \
3438 break; \
3439 default: \
3440 gcc_unreachable (); \
3442 } while (0)
3444 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3445 /* Do not use STRIP_NOPS here. We do not want an enumerator
3446 whose value is 0 to count as a null pointer constant. */
3447 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3448 rhs = TREE_OPERAND (rhs, 0);
3450 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3451 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3452 rhs = default_conversion (rhs);
3453 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3454 rhs = decl_constant_value_for_broken_optimization (rhs);
3456 rhstype = TREE_TYPE (rhs);
3457 coder = TREE_CODE (rhstype);
3459 if (coder == ERROR_MARK)
3460 return error_mark_node;
3462 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3464 overflow_warning (rhs);
3465 /* Check for Objective-C protocols. This will automatically
3466 issue a warning if there are protocol violations. No need to
3467 use the return value. */
3468 if (c_dialect_objc ())
3469 objc_comptypes (type, rhstype, 0);
3470 return rhs;
3473 if (coder == VOID_TYPE)
3475 /* Except for passing an argument to an unprototyped function,
3476 this is a constraint violation. When passing an argument to
3477 an unprototyped function, it is compile-time undefined;
3478 making it a constraint in that case was rejected in
3479 DR#252. */
3480 error ("void value not ignored as it ought to be");
3481 return error_mark_node;
3483 /* A type converts to a reference to it.
3484 This code doesn't fully support references, it's just for the
3485 special case of va_start and va_copy. */
3486 if (codel == REFERENCE_TYPE
3487 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3489 if (!lvalue_p (rhs))
3491 error ("cannot pass rvalue to reference parameter");
3492 return error_mark_node;
3494 if (!c_mark_addressable (rhs))
3495 return error_mark_node;
3496 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3498 /* We already know that these two types are compatible, but they
3499 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3500 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3501 likely to be va_list, a typedef to __builtin_va_list, which
3502 is different enough that it will cause problems later. */
3503 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3504 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3506 rhs = build1 (NOP_EXPR, type, rhs);
3507 return rhs;
3509 /* Some types can interconvert without explicit casts. */
3510 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3511 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3512 return convert (type, rhs);
3513 /* Arithmetic types all interconvert, and enum is treated like int. */
3514 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3515 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3516 || codel == BOOLEAN_TYPE)
3517 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3518 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3519 || coder == BOOLEAN_TYPE))
3520 return convert_and_check (type, rhs);
3522 /* Conversion to a transparent union from its member types.
3523 This applies only to function arguments. */
3524 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3525 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3527 tree memb_types;
3528 tree marginal_memb_type = 0;
3530 for (memb_types = TYPE_FIELDS (type); memb_types;
3531 memb_types = TREE_CHAIN (memb_types))
3533 tree memb_type = TREE_TYPE (memb_types);
3535 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3536 TYPE_MAIN_VARIANT (rhstype)))
3537 break;
3539 if (TREE_CODE (memb_type) != POINTER_TYPE)
3540 continue;
3542 if (coder == POINTER_TYPE)
3544 tree ttl = TREE_TYPE (memb_type);
3545 tree ttr = TREE_TYPE (rhstype);
3547 /* Any non-function converts to a [const][volatile] void *
3548 and vice versa; otherwise, targets must be the same.
3549 Meanwhile, the lhs target must have all the qualifiers of
3550 the rhs. */
3551 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3552 || comp_target_types (memb_type, rhstype, 0))
3554 /* If this type won't generate any warnings, use it. */
3555 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3556 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3557 && TREE_CODE (ttl) == FUNCTION_TYPE)
3558 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3559 == TYPE_QUALS (ttr))
3560 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3561 == TYPE_QUALS (ttl))))
3562 break;
3564 /* Keep looking for a better type, but remember this one. */
3565 if (!marginal_memb_type)
3566 marginal_memb_type = memb_type;
3570 /* Can convert integer zero to any pointer type. */
3571 if (integer_zerop (rhs)
3572 || (TREE_CODE (rhs) == NOP_EXPR
3573 && integer_zerop (TREE_OPERAND (rhs, 0))))
3575 rhs = null_pointer_node;
3576 break;
3580 if (memb_types || marginal_memb_type)
3582 if (!memb_types)
3584 /* We have only a marginally acceptable member type;
3585 it needs a warning. */
3586 tree ttl = TREE_TYPE (marginal_memb_type);
3587 tree ttr = TREE_TYPE (rhstype);
3589 /* Const and volatile mean something different for function
3590 types, so the usual warnings are not appropriate. */
3591 if (TREE_CODE (ttr) == FUNCTION_TYPE
3592 && TREE_CODE (ttl) == FUNCTION_TYPE)
3594 /* Because const and volatile on functions are
3595 restrictions that say the function will not do
3596 certain things, it is okay to use a const or volatile
3597 function where an ordinary one is wanted, but not
3598 vice-versa. */
3599 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3600 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3601 "makes qualified function "
3602 "pointer from unqualified"),
3603 N_("assignment makes qualified "
3604 "function pointer from "
3605 "unqualified"),
3606 N_("initialization makes qualified "
3607 "function pointer from "
3608 "unqualified"),
3609 N_("return makes qualified function "
3610 "pointer from unqualified"));
3612 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3613 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3614 "qualifiers from pointer target type"),
3615 N_("assignment discards qualifiers "
3616 "from pointer target type"),
3617 N_("initialization discards qualifiers "
3618 "from pointer target type"),
3619 N_("return discards qualifiers from "
3620 "pointer target type"));
3623 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3624 pedwarn ("ISO C prohibits argument conversion to union type");
3626 return build1 (NOP_EXPR, type, rhs);
3630 /* Conversions among pointers */
3631 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3632 && (coder == codel))
3634 tree ttl = TREE_TYPE (type);
3635 tree ttr = TREE_TYPE (rhstype);
3636 tree mvl = ttl;
3637 tree mvr = ttr;
3638 bool is_opaque_pointer;
3639 int target_cmp = 0; /* Cache comp_target_types () result. */
3641 if (TREE_CODE (mvl) != ARRAY_TYPE)
3642 mvl = TYPE_MAIN_VARIANT (mvl);
3643 if (TREE_CODE (mvr) != ARRAY_TYPE)
3644 mvr = TYPE_MAIN_VARIANT (mvr);
3645 /* Opaque pointers are treated like void pointers. */
3646 is_opaque_pointer = (targetm.vector_opaque_p (type)
3647 || targetm.vector_opaque_p (rhstype))
3648 && TREE_CODE (ttl) == VECTOR_TYPE
3649 && TREE_CODE (ttr) == VECTOR_TYPE;
3651 /* Any non-function converts to a [const][volatile] void *
3652 and vice versa; otherwise, targets must be the same.
3653 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3654 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3655 || (target_cmp = comp_target_types (type, rhstype, 0))
3656 || is_opaque_pointer
3657 || (c_common_unsigned_type (mvl)
3658 == c_common_unsigned_type (mvr)))
3660 if (pedantic
3661 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3663 (VOID_TYPE_P (ttr)
3664 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3665 which are not ANSI null ptr constants. */
3666 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3667 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3668 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3669 "%qE between function pointer "
3670 "and %<void *%>"),
3671 N_("ISO C forbids assignment between "
3672 "function pointer and %<void *%>"),
3673 N_("ISO C forbids initialization between "
3674 "function pointer and %<void *%>"),
3675 N_("ISO C forbids return between function "
3676 "pointer and %<void *%>"));
3677 /* Const and volatile mean something different for function types,
3678 so the usual warnings are not appropriate. */
3679 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3680 && TREE_CODE (ttl) != FUNCTION_TYPE)
3682 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3683 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3684 "qualifiers from pointer target type"),
3685 N_("assignment discards qualifiers "
3686 "from pointer target type"),
3687 N_("initialization discards qualifiers "
3688 "from pointer target type"),
3689 N_("return discards qualifiers from "
3690 "pointer target type"));
3691 /* If this is not a case of ignoring a mismatch in signedness,
3692 no warning. */
3693 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3694 || target_cmp)
3696 /* If there is a mismatch, do warn. */
3697 else if (warn_pointer_sign)
3698 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3699 "%d of %qE differ in signedness"),
3700 N_("pointer targets in assignment "
3701 "differ in signedness"),
3702 N_("pointer targets in initialization "
3703 "differ in signedness"),
3704 N_("pointer targets in return differ "
3705 "in signedness"));
3707 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3708 && TREE_CODE (ttr) == FUNCTION_TYPE)
3710 /* Because const and volatile on functions are restrictions
3711 that say the function will not do certain things,
3712 it is okay to use a const or volatile function
3713 where an ordinary one is wanted, but not vice-versa. */
3714 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3715 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3716 "qualified function pointer "
3717 "from unqualified"),
3718 N_("assignment makes qualified function "
3719 "pointer from unqualified"),
3720 N_("initialization makes qualified "
3721 "function pointer from unqualified"),
3722 N_("return makes qualified function "
3723 "pointer from unqualified"));
3726 else
3727 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3728 "incompatible pointer type"),
3729 N_("assignment from incompatible pointer type"),
3730 N_("initialization from incompatible "
3731 "pointer type"),
3732 N_("return from incompatible pointer type"));
3733 return convert (type, rhs);
3735 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3737 /* ??? This should not be an error when inlining calls to
3738 unprototyped functions. */
3739 error ("invalid use of non-lvalue array");
3740 return error_mark_node;
3742 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3744 /* An explicit constant 0 can convert to a pointer,
3745 or one that results from arithmetic, even including
3746 a cast to integer type. */
3747 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3749 !(TREE_CODE (rhs) == NOP_EXPR
3750 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3751 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3752 && integer_zerop (TREE_OPERAND (rhs, 0))))
3753 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3754 "pointer from integer without a cast"),
3755 N_("assignment makes pointer from integer "
3756 "without a cast"),
3757 N_("initialization makes pointer from "
3758 "integer without a cast"),
3759 N_("return makes pointer from integer "
3760 "without a cast"));
3762 return convert (type, rhs);
3764 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3766 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3767 "from pointer without a cast"),
3768 N_("assignment makes integer from pointer "
3769 "without a cast"),
3770 N_("initialization makes integer from pointer "
3771 "without a cast"),
3772 N_("return makes integer from pointer "
3773 "without a cast"));
3774 return convert (type, rhs);
3776 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3777 return convert (type, rhs);
3779 switch (errtype)
3781 case ic_argpass:
3782 case ic_argpass_nonproto:
3783 /* ??? This should not be an error when inlining calls to
3784 unprototyped functions. */
3785 error ("incompatible type for argument %d of %qE", parmnum, rname);
3786 break;
3787 case ic_assign:
3788 error ("incompatible types in assignment");
3789 break;
3790 case ic_init:
3791 error ("incompatible types in initialization");
3792 break;
3793 case ic_return:
3794 error ("incompatible types in return");
3795 break;
3796 default:
3797 gcc_unreachable ();
3800 return error_mark_node;
3803 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3804 is used for error and waring reporting and indicates which argument
3805 is being processed. */
3807 tree
3808 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3810 tree ret, type;
3812 /* If FN was prototyped, the value has been converted already
3813 in convert_arguments. */
3814 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3815 return value;
3817 type = TREE_TYPE (parm);
3818 ret = convert_for_assignment (type, value,
3819 ic_argpass_nonproto, fn,
3820 fn, argnum);
3821 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3822 && INTEGRAL_TYPE_P (type)
3823 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3824 ret = default_conversion (ret);
3825 return ret;
3828 /* If VALUE is a compound expr all of whose expressions are constant, then
3829 return its value. Otherwise, return error_mark_node.
3831 This is for handling COMPOUND_EXPRs as initializer elements
3832 which is allowed with a warning when -pedantic is specified. */
3834 static tree
3835 valid_compound_expr_initializer (tree value, tree endtype)
3837 if (TREE_CODE (value) == COMPOUND_EXPR)
3839 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3840 == error_mark_node)
3841 return error_mark_node;
3842 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3843 endtype);
3845 else if (!initializer_constant_valid_p (value, endtype))
3846 return error_mark_node;
3847 else
3848 return value;
3851 /* Perform appropriate conversions on the initial value of a variable,
3852 store it in the declaration DECL,
3853 and print any error messages that are appropriate.
3854 If the init is invalid, store an ERROR_MARK. */
3856 void
3857 store_init_value (tree decl, tree init)
3859 tree value, type;
3861 /* If variable's type was invalidly declared, just ignore it. */
3863 type = TREE_TYPE (decl);
3864 if (TREE_CODE (type) == ERROR_MARK)
3865 return;
3867 /* Digest the specified initializer into an expression. */
3869 value = digest_init (type, init, true, TREE_STATIC (decl));
3871 /* Store the expression if valid; else report error. */
3873 if (warn_traditional && !in_system_header
3874 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3875 warning ("traditional C rejects automatic aggregate initialization");
3877 DECL_INITIAL (decl) = value;
3879 /* ANSI wants warnings about out-of-range constant initializers. */
3880 STRIP_TYPE_NOPS (value);
3881 constant_expression_warning (value);
3883 /* Check if we need to set array size from compound literal size. */
3884 if (TREE_CODE (type) == ARRAY_TYPE
3885 && TYPE_DOMAIN (type) == 0
3886 && value != error_mark_node)
3888 tree inside_init = init;
3890 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3891 inside_init = TREE_OPERAND (init, 0);
3892 inside_init = fold (inside_init);
3894 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3896 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3898 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3900 /* For int foo[] = (int [3]){1}; we need to set array size
3901 now since later on array initializer will be just the
3902 brace enclosed list of the compound literal. */
3903 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3904 layout_type (type);
3905 layout_decl (decl, 0);
3911 /* Methods for storing and printing names for error messages. */
3913 /* Implement a spelling stack that allows components of a name to be pushed
3914 and popped. Each element on the stack is this structure. */
3916 struct spelling
3918 int kind;
3919 union
3921 int i;
3922 const char *s;
3923 } u;
3926 #define SPELLING_STRING 1
3927 #define SPELLING_MEMBER 2
3928 #define SPELLING_BOUNDS 3
3930 static struct spelling *spelling; /* Next stack element (unused). */
3931 static struct spelling *spelling_base; /* Spelling stack base. */
3932 static int spelling_size; /* Size of the spelling stack. */
3934 /* Macros to save and restore the spelling stack around push_... functions.
3935 Alternative to SAVE_SPELLING_STACK. */
3937 #define SPELLING_DEPTH() (spelling - spelling_base)
3938 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3940 /* Push an element on the spelling stack with type KIND and assign VALUE
3941 to MEMBER. */
3943 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3945 int depth = SPELLING_DEPTH (); \
3947 if (depth >= spelling_size) \
3949 spelling_size += 10; \
3950 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3951 spelling_size); \
3952 RESTORE_SPELLING_DEPTH (depth); \
3955 spelling->kind = (KIND); \
3956 spelling->MEMBER = (VALUE); \
3957 spelling++; \
3960 /* Push STRING on the stack. Printed literally. */
3962 static void
3963 push_string (const char *string)
3965 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3968 /* Push a member name on the stack. Printed as '.' STRING. */
3970 static void
3971 push_member_name (tree decl)
3973 const char *const string
3974 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3975 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3978 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3980 static void
3981 push_array_bounds (int bounds)
3983 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3986 /* Compute the maximum size in bytes of the printed spelling. */
3988 static int
3989 spelling_length (void)
3991 int size = 0;
3992 struct spelling *p;
3994 for (p = spelling_base; p < spelling; p++)
3996 if (p->kind == SPELLING_BOUNDS)
3997 size += 25;
3998 else
3999 size += strlen (p->u.s) + 1;
4002 return size;
4005 /* Print the spelling to BUFFER and return it. */
4007 static char *
4008 print_spelling (char *buffer)
4010 char *d = buffer;
4011 struct spelling *p;
4013 for (p = spelling_base; p < spelling; p++)
4014 if (p->kind == SPELLING_BOUNDS)
4016 sprintf (d, "[%d]", p->u.i);
4017 d += strlen (d);
4019 else
4021 const char *s;
4022 if (p->kind == SPELLING_MEMBER)
4023 *d++ = '.';
4024 for (s = p->u.s; (*d = *s++); d++)
4027 *d++ = '\0';
4028 return buffer;
4031 /* Issue an error message for a bad initializer component.
4032 MSGID identifies the message.
4033 The component name is taken from the spelling stack. */
4035 void
4036 error_init (const char *msgid)
4038 char *ofwhat;
4040 error ("%s", _(msgid));
4041 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4042 if (*ofwhat)
4043 error ("(near initialization for %qs)", ofwhat);
4046 /* Issue a pedantic warning for a bad initializer component.
4047 MSGID identifies the message.
4048 The component name is taken from the spelling stack. */
4050 void
4051 pedwarn_init (const char *msgid)
4053 char *ofwhat;
4055 pedwarn ("%s", _(msgid));
4056 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4057 if (*ofwhat)
4058 pedwarn ("(near initialization for %qs)", ofwhat);
4061 /* Issue a warning for a bad initializer component.
4062 MSGID identifies the message.
4063 The component name is taken from the spelling stack. */
4065 static void
4066 warning_init (const char *msgid)
4068 char *ofwhat;
4070 warning ("%s", _(msgid));
4071 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4072 if (*ofwhat)
4073 warning ("(near initialization for %qs)", ofwhat);
4076 /* If TYPE is an array type and EXPR is a parenthesized string
4077 constant, warn if pedantic that EXPR is being used to initialize an
4078 object of type TYPE. */
4080 void
4081 maybe_warn_string_init (tree type, struct c_expr expr)
4083 if (pedantic
4084 && TREE_CODE (type) == ARRAY_TYPE
4085 && TREE_CODE (expr.value) == STRING_CST
4086 && expr.original_code != STRING_CST)
4087 pedwarn_init ("array initialized from parenthesized string constant");
4090 /* Digest the parser output INIT as an initializer for type TYPE.
4091 Return a C expression of type TYPE to represent the initial value.
4093 If INIT is a string constant, STRICT_STRING is true if it is
4094 unparenthesized or we should not warn here for it being parenthesized.
4095 For other types of INIT, STRICT_STRING is not used.
4097 REQUIRE_CONSTANT requests an error if non-constant initializers or
4098 elements are seen. */
4100 static tree
4101 digest_init (tree type, tree init, bool strict_string, int require_constant)
4103 enum tree_code code = TREE_CODE (type);
4104 tree inside_init = init;
4106 if (type == error_mark_node
4107 || init == error_mark_node
4108 || TREE_TYPE (init) == error_mark_node)
4109 return error_mark_node;
4111 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4112 /* Do not use STRIP_NOPS here. We do not want an enumerator
4113 whose value is 0 to count as a null pointer constant. */
4114 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4115 inside_init = TREE_OPERAND (init, 0);
4117 inside_init = fold (inside_init);
4119 /* Initialization of an array of chars from a string constant
4120 optionally enclosed in braces. */
4122 if (code == ARRAY_TYPE && inside_init
4123 && TREE_CODE (inside_init) == STRING_CST)
4125 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4126 /* Note that an array could be both an array of character type
4127 and an array of wchar_t if wchar_t is signed char or unsigned
4128 char. */
4129 bool char_array = (typ1 == char_type_node
4130 || typ1 == signed_char_type_node
4131 || typ1 == unsigned_char_type_node);
4132 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4133 if (char_array || wchar_array)
4135 struct c_expr expr;
4136 bool char_string;
4137 expr.value = inside_init;
4138 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4139 maybe_warn_string_init (type, expr);
4141 char_string
4142 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4143 == char_type_node);
4145 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4146 TYPE_MAIN_VARIANT (type)))
4147 return inside_init;
4149 if (!wchar_array && !char_string)
4151 error_init ("char-array initialized from wide string");
4152 return error_mark_node;
4154 if (char_string && !char_array)
4156 error_init ("wchar_t-array initialized from non-wide string");
4157 return error_mark_node;
4160 TREE_TYPE (inside_init) = type;
4161 if (TYPE_DOMAIN (type) != 0
4162 && TYPE_SIZE (type) != 0
4163 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4164 /* Subtract 1 (or sizeof (wchar_t))
4165 because it's ok to ignore the terminating null char
4166 that is counted in the length of the constant. */
4167 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4168 TREE_STRING_LENGTH (inside_init)
4169 - ((TYPE_PRECISION (typ1)
4170 != TYPE_PRECISION (char_type_node))
4171 ? (TYPE_PRECISION (wchar_type_node)
4172 / BITS_PER_UNIT)
4173 : 1)))
4174 pedwarn_init ("initializer-string for array of chars is too long");
4176 return inside_init;
4178 else if (INTEGRAL_TYPE_P (typ1))
4180 error_init ("array of inappropriate type initialized "
4181 "from string constant");
4182 return error_mark_node;
4186 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4187 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4188 below and handle as a constructor. */
4189 if (code == VECTOR_TYPE
4190 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4191 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4192 && TREE_CONSTANT (inside_init))
4194 if (TREE_CODE (inside_init) == VECTOR_CST
4195 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4196 TYPE_MAIN_VARIANT (type)))
4197 return inside_init;
4199 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4201 tree link;
4203 /* Iterate through elements and check if all constructor
4204 elements are *_CSTs. */
4205 for (link = CONSTRUCTOR_ELTS (inside_init);
4206 link;
4207 link = TREE_CHAIN (link))
4208 if (! CONSTANT_CLASS_P (TREE_VALUE (link)))
4209 break;
4211 if (link == NULL)
4212 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4216 /* Any type can be initialized
4217 from an expression of the same type, optionally with braces. */
4219 if (inside_init && TREE_TYPE (inside_init) != 0
4220 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4221 TYPE_MAIN_VARIANT (type))
4222 || (code == ARRAY_TYPE
4223 && comptypes (TREE_TYPE (inside_init), type))
4224 || (code == VECTOR_TYPE
4225 && comptypes (TREE_TYPE (inside_init), type))
4226 || (code == POINTER_TYPE
4227 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4228 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4229 TREE_TYPE (type)))
4230 || (code == POINTER_TYPE
4231 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4232 && comptypes (TREE_TYPE (inside_init),
4233 TREE_TYPE (type)))))
4235 if (code == POINTER_TYPE)
4237 inside_init = default_function_array_conversion (inside_init);
4239 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4241 error_init ("invalid use of non-lvalue array");
4242 return error_mark_node;
4246 if (code == VECTOR_TYPE)
4247 /* Although the types are compatible, we may require a
4248 conversion. */
4249 inside_init = convert (type, inside_init);
4251 if (require_constant && !flag_isoc99
4252 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4254 /* As an extension, allow initializing objects with static storage
4255 duration with compound literals (which are then treated just as
4256 the brace enclosed list they contain). */
4257 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4258 inside_init = DECL_INITIAL (decl);
4261 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4262 && TREE_CODE (inside_init) != CONSTRUCTOR)
4264 error_init ("array initialized from non-constant array expression");
4265 return error_mark_node;
4268 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4269 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4271 /* Compound expressions can only occur here if -pedantic or
4272 -pedantic-errors is specified. In the later case, we always want
4273 an error. In the former case, we simply want a warning. */
4274 if (require_constant && pedantic
4275 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4277 inside_init
4278 = valid_compound_expr_initializer (inside_init,
4279 TREE_TYPE (inside_init));
4280 if (inside_init == error_mark_node)
4281 error_init ("initializer element is not constant");
4282 else
4283 pedwarn_init ("initializer element is not constant");
4284 if (flag_pedantic_errors)
4285 inside_init = error_mark_node;
4287 else if (require_constant
4288 && !initializer_constant_valid_p (inside_init,
4289 TREE_TYPE (inside_init)))
4291 error_init ("initializer element is not constant");
4292 inside_init = error_mark_node;
4295 return inside_init;
4298 /* Handle scalar types, including conversions. */
4300 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4301 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4302 || code == VECTOR_TYPE)
4304 /* Note that convert_for_assignment calls default_conversion
4305 for arrays and functions. We must not call it in the
4306 case where inside_init is a null pointer constant. */
4307 inside_init
4308 = convert_for_assignment (type, init, ic_init,
4309 NULL_TREE, NULL_TREE, 0);
4311 /* Check to see if we have already given an error message. */
4312 if (inside_init == error_mark_node)
4314 else if (require_constant && !TREE_CONSTANT (inside_init))
4316 error_init ("initializer element is not constant");
4317 inside_init = error_mark_node;
4319 else if (require_constant
4320 && !initializer_constant_valid_p (inside_init,
4321 TREE_TYPE (inside_init)))
4323 error_init ("initializer element is not computable at load time");
4324 inside_init = error_mark_node;
4327 return inside_init;
4330 /* Come here only for records and arrays. */
4332 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4334 error_init ("variable-sized object may not be initialized");
4335 return error_mark_node;
4338 error_init ("invalid initializer");
4339 return error_mark_node;
4342 /* Handle initializers that use braces. */
4344 /* Type of object we are accumulating a constructor for.
4345 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4346 static tree constructor_type;
4348 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4349 left to fill. */
4350 static tree constructor_fields;
4352 /* For an ARRAY_TYPE, this is the specified index
4353 at which to store the next element we get. */
4354 static tree constructor_index;
4356 /* For an ARRAY_TYPE, this is the maximum index. */
4357 static tree constructor_max_index;
4359 /* For a RECORD_TYPE, this is the first field not yet written out. */
4360 static tree constructor_unfilled_fields;
4362 /* For an ARRAY_TYPE, this is the index of the first element
4363 not yet written out. */
4364 static tree constructor_unfilled_index;
4366 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4367 This is so we can generate gaps between fields, when appropriate. */
4368 static tree constructor_bit_index;
4370 /* If we are saving up the elements rather than allocating them,
4371 this is the list of elements so far (in reverse order,
4372 most recent first). */
4373 static tree constructor_elements;
4375 /* 1 if constructor should be incrementally stored into a constructor chain,
4376 0 if all the elements should be kept in AVL tree. */
4377 static int constructor_incremental;
4379 /* 1 if so far this constructor's elements are all compile-time constants. */
4380 static int constructor_constant;
4382 /* 1 if so far this constructor's elements are all valid address constants. */
4383 static int constructor_simple;
4385 /* 1 if this constructor is erroneous so far. */
4386 static int constructor_erroneous;
4388 /* Structure for managing pending initializer elements, organized as an
4389 AVL tree. */
4391 struct init_node
4393 struct init_node *left, *right;
4394 struct init_node *parent;
4395 int balance;
4396 tree purpose;
4397 tree value;
4400 /* Tree of pending elements at this constructor level.
4401 These are elements encountered out of order
4402 which belong at places we haven't reached yet in actually
4403 writing the output.
4404 Will never hold tree nodes across GC runs. */
4405 static struct init_node *constructor_pending_elts;
4407 /* The SPELLING_DEPTH of this constructor. */
4408 static int constructor_depth;
4410 /* DECL node for which an initializer is being read.
4411 0 means we are reading a constructor expression
4412 such as (struct foo) {...}. */
4413 static tree constructor_decl;
4415 /* Nonzero if this is an initializer for a top-level decl. */
4416 static int constructor_top_level;
4418 /* Nonzero if there were any member designators in this initializer. */
4419 static int constructor_designated;
4421 /* Nesting depth of designator list. */
4422 static int designator_depth;
4424 /* Nonzero if there were diagnosed errors in this designator list. */
4425 static int designator_errorneous;
4428 /* This stack has a level for each implicit or explicit level of
4429 structuring in the initializer, including the outermost one. It
4430 saves the values of most of the variables above. */
4432 struct constructor_range_stack;
4434 struct constructor_stack
4436 struct constructor_stack *next;
4437 tree type;
4438 tree fields;
4439 tree index;
4440 tree max_index;
4441 tree unfilled_index;
4442 tree unfilled_fields;
4443 tree bit_index;
4444 tree elements;
4445 struct init_node *pending_elts;
4446 int offset;
4447 int depth;
4448 /* If value nonzero, this value should replace the entire
4449 constructor at this level. */
4450 struct c_expr replacement_value;
4451 struct constructor_range_stack *range_stack;
4452 char constant;
4453 char simple;
4454 char implicit;
4455 char erroneous;
4456 char outer;
4457 char incremental;
4458 char designated;
4461 struct constructor_stack *constructor_stack;
4463 /* This stack represents designators from some range designator up to
4464 the last designator in the list. */
4466 struct constructor_range_stack
4468 struct constructor_range_stack *next, *prev;
4469 struct constructor_stack *stack;
4470 tree range_start;
4471 tree index;
4472 tree range_end;
4473 tree fields;
4476 struct constructor_range_stack *constructor_range_stack;
4478 /* This stack records separate initializers that are nested.
4479 Nested initializers can't happen in ANSI C, but GNU C allows them
4480 in cases like { ... (struct foo) { ... } ... }. */
4482 struct initializer_stack
4484 struct initializer_stack *next;
4485 tree decl;
4486 struct constructor_stack *constructor_stack;
4487 struct constructor_range_stack *constructor_range_stack;
4488 tree elements;
4489 struct spelling *spelling;
4490 struct spelling *spelling_base;
4491 int spelling_size;
4492 char top_level;
4493 char require_constant_value;
4494 char require_constant_elements;
4497 struct initializer_stack *initializer_stack;
4499 /* Prepare to parse and output the initializer for variable DECL. */
4501 void
4502 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4504 const char *locus;
4505 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4507 p->decl = constructor_decl;
4508 p->require_constant_value = require_constant_value;
4509 p->require_constant_elements = require_constant_elements;
4510 p->constructor_stack = constructor_stack;
4511 p->constructor_range_stack = constructor_range_stack;
4512 p->elements = constructor_elements;
4513 p->spelling = spelling;
4514 p->spelling_base = spelling_base;
4515 p->spelling_size = spelling_size;
4516 p->top_level = constructor_top_level;
4517 p->next = initializer_stack;
4518 initializer_stack = p;
4520 constructor_decl = decl;
4521 constructor_designated = 0;
4522 constructor_top_level = top_level;
4524 if (decl != 0 && decl != error_mark_node)
4526 require_constant_value = TREE_STATIC (decl);
4527 require_constant_elements
4528 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4529 /* For a scalar, you can always use any value to initialize,
4530 even within braces. */
4531 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4532 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4533 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4534 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4535 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4537 else
4539 require_constant_value = 0;
4540 require_constant_elements = 0;
4541 locus = "(anonymous)";
4544 constructor_stack = 0;
4545 constructor_range_stack = 0;
4547 missing_braces_mentioned = 0;
4549 spelling_base = 0;
4550 spelling_size = 0;
4551 RESTORE_SPELLING_DEPTH (0);
4553 if (locus)
4554 push_string (locus);
4557 void
4558 finish_init (void)
4560 struct initializer_stack *p = initializer_stack;
4562 /* Free the whole constructor stack of this initializer. */
4563 while (constructor_stack)
4565 struct constructor_stack *q = constructor_stack;
4566 constructor_stack = q->next;
4567 free (q);
4570 gcc_assert (!constructor_range_stack);
4572 /* Pop back to the data of the outer initializer (if any). */
4573 free (spelling_base);
4575 constructor_decl = p->decl;
4576 require_constant_value = p->require_constant_value;
4577 require_constant_elements = p->require_constant_elements;
4578 constructor_stack = p->constructor_stack;
4579 constructor_range_stack = p->constructor_range_stack;
4580 constructor_elements = p->elements;
4581 spelling = p->spelling;
4582 spelling_base = p->spelling_base;
4583 spelling_size = p->spelling_size;
4584 constructor_top_level = p->top_level;
4585 initializer_stack = p->next;
4586 free (p);
4589 /* Call here when we see the initializer is surrounded by braces.
4590 This is instead of a call to push_init_level;
4591 it is matched by a call to pop_init_level.
4593 TYPE is the type to initialize, for a constructor expression.
4594 For an initializer for a decl, TYPE is zero. */
4596 void
4597 really_start_incremental_init (tree type)
4599 struct constructor_stack *p = XNEW (struct constructor_stack);
4601 if (type == 0)
4602 type = TREE_TYPE (constructor_decl);
4604 if (targetm.vector_opaque_p (type))
4605 error ("opaque vector types cannot be initialized");
4607 p->type = constructor_type;
4608 p->fields = constructor_fields;
4609 p->index = constructor_index;
4610 p->max_index = constructor_max_index;
4611 p->unfilled_index = constructor_unfilled_index;
4612 p->unfilled_fields = constructor_unfilled_fields;
4613 p->bit_index = constructor_bit_index;
4614 p->elements = constructor_elements;
4615 p->constant = constructor_constant;
4616 p->simple = constructor_simple;
4617 p->erroneous = constructor_erroneous;
4618 p->pending_elts = constructor_pending_elts;
4619 p->depth = constructor_depth;
4620 p->replacement_value.value = 0;
4621 p->replacement_value.original_code = ERROR_MARK;
4622 p->implicit = 0;
4623 p->range_stack = 0;
4624 p->outer = 0;
4625 p->incremental = constructor_incremental;
4626 p->designated = constructor_designated;
4627 p->next = 0;
4628 constructor_stack = p;
4630 constructor_constant = 1;
4631 constructor_simple = 1;
4632 constructor_depth = SPELLING_DEPTH ();
4633 constructor_elements = 0;
4634 constructor_pending_elts = 0;
4635 constructor_type = type;
4636 constructor_incremental = 1;
4637 constructor_designated = 0;
4638 designator_depth = 0;
4639 designator_errorneous = 0;
4641 if (TREE_CODE (constructor_type) == RECORD_TYPE
4642 || TREE_CODE (constructor_type) == UNION_TYPE)
4644 constructor_fields = TYPE_FIELDS (constructor_type);
4645 /* Skip any nameless bit fields at the beginning. */
4646 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4647 && DECL_NAME (constructor_fields) == 0)
4648 constructor_fields = TREE_CHAIN (constructor_fields);
4650 constructor_unfilled_fields = constructor_fields;
4651 constructor_bit_index = bitsize_zero_node;
4653 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4655 if (TYPE_DOMAIN (constructor_type))
4657 constructor_max_index
4658 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4660 /* Detect non-empty initializations of zero-length arrays. */
4661 if (constructor_max_index == NULL_TREE
4662 && TYPE_SIZE (constructor_type))
4663 constructor_max_index = build_int_cst (NULL_TREE, -1);
4665 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4666 to initialize VLAs will cause a proper error; avoid tree
4667 checking errors as well by setting a safe value. */
4668 if (constructor_max_index
4669 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4670 constructor_max_index = build_int_cst (NULL_TREE, -1);
4672 constructor_index
4673 = convert (bitsizetype,
4674 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4676 else
4678 constructor_index = bitsize_zero_node;
4679 constructor_max_index = NULL_TREE;
4682 constructor_unfilled_index = constructor_index;
4684 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4686 /* Vectors are like simple fixed-size arrays. */
4687 constructor_max_index =
4688 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4689 constructor_index = convert (bitsizetype, bitsize_zero_node);
4690 constructor_unfilled_index = constructor_index;
4692 else
4694 /* Handle the case of int x = {5}; */
4695 constructor_fields = constructor_type;
4696 constructor_unfilled_fields = constructor_type;
4700 /* Push down into a subobject, for initialization.
4701 If this is for an explicit set of braces, IMPLICIT is 0.
4702 If it is because the next element belongs at a lower level,
4703 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4705 void
4706 push_init_level (int implicit)
4708 struct constructor_stack *p;
4709 tree value = NULL_TREE;
4711 /* If we've exhausted any levels that didn't have braces,
4712 pop them now. */
4713 while (constructor_stack->implicit)
4715 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4716 || TREE_CODE (constructor_type) == UNION_TYPE)
4717 && constructor_fields == 0)
4718 process_init_element (pop_init_level (1));
4719 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4720 && constructor_max_index
4721 && tree_int_cst_lt (constructor_max_index, constructor_index))
4722 process_init_element (pop_init_level (1));
4723 else
4724 break;
4727 /* Unless this is an explicit brace, we need to preserve previous
4728 content if any. */
4729 if (implicit)
4731 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4732 || TREE_CODE (constructor_type) == UNION_TYPE)
4733 && constructor_fields)
4734 value = find_init_member (constructor_fields);
4735 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4736 value = find_init_member (constructor_index);
4739 p = XNEW (struct constructor_stack);
4740 p->type = constructor_type;
4741 p->fields = constructor_fields;
4742 p->index = constructor_index;
4743 p->max_index = constructor_max_index;
4744 p->unfilled_index = constructor_unfilled_index;
4745 p->unfilled_fields = constructor_unfilled_fields;
4746 p->bit_index = constructor_bit_index;
4747 p->elements = constructor_elements;
4748 p->constant = constructor_constant;
4749 p->simple = constructor_simple;
4750 p->erroneous = constructor_erroneous;
4751 p->pending_elts = constructor_pending_elts;
4752 p->depth = constructor_depth;
4753 p->replacement_value.value = 0;
4754 p->replacement_value.original_code = ERROR_MARK;
4755 p->implicit = implicit;
4756 p->outer = 0;
4757 p->incremental = constructor_incremental;
4758 p->designated = constructor_designated;
4759 p->next = constructor_stack;
4760 p->range_stack = 0;
4761 constructor_stack = p;
4763 constructor_constant = 1;
4764 constructor_simple = 1;
4765 constructor_depth = SPELLING_DEPTH ();
4766 constructor_elements = 0;
4767 constructor_incremental = 1;
4768 constructor_designated = 0;
4769 constructor_pending_elts = 0;
4770 if (!implicit)
4772 p->range_stack = constructor_range_stack;
4773 constructor_range_stack = 0;
4774 designator_depth = 0;
4775 designator_errorneous = 0;
4778 /* Don't die if an entire brace-pair level is superfluous
4779 in the containing level. */
4780 if (constructor_type == 0)
4782 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4783 || TREE_CODE (constructor_type) == UNION_TYPE)
4785 /* Don't die if there are extra init elts at the end. */
4786 if (constructor_fields == 0)
4787 constructor_type = 0;
4788 else
4790 constructor_type = TREE_TYPE (constructor_fields);
4791 push_member_name (constructor_fields);
4792 constructor_depth++;
4795 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4797 constructor_type = TREE_TYPE (constructor_type);
4798 push_array_bounds (tree_low_cst (constructor_index, 0));
4799 constructor_depth++;
4802 if (constructor_type == 0)
4804 error_init ("extra brace group at end of initializer");
4805 constructor_fields = 0;
4806 constructor_unfilled_fields = 0;
4807 return;
4810 if (value && TREE_CODE (value) == CONSTRUCTOR)
4812 constructor_constant = TREE_CONSTANT (value);
4813 constructor_simple = TREE_STATIC (value);
4814 constructor_elements = CONSTRUCTOR_ELTS (value);
4815 if (constructor_elements
4816 && (TREE_CODE (constructor_type) == RECORD_TYPE
4817 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4818 set_nonincremental_init ();
4821 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4823 missing_braces_mentioned = 1;
4824 warning_init ("missing braces around initializer");
4827 if (TREE_CODE (constructor_type) == RECORD_TYPE
4828 || TREE_CODE (constructor_type) == UNION_TYPE)
4830 constructor_fields = TYPE_FIELDS (constructor_type);
4831 /* Skip any nameless bit fields at the beginning. */
4832 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4833 && DECL_NAME (constructor_fields) == 0)
4834 constructor_fields = TREE_CHAIN (constructor_fields);
4836 constructor_unfilled_fields = constructor_fields;
4837 constructor_bit_index = bitsize_zero_node;
4839 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4841 /* Vectors are like simple fixed-size arrays. */
4842 constructor_max_index =
4843 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4844 constructor_index = convert (bitsizetype, integer_zero_node);
4845 constructor_unfilled_index = constructor_index;
4847 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4849 if (TYPE_DOMAIN (constructor_type))
4851 constructor_max_index
4852 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4854 /* Detect non-empty initializations of zero-length arrays. */
4855 if (constructor_max_index == NULL_TREE
4856 && TYPE_SIZE (constructor_type))
4857 constructor_max_index = build_int_cst (NULL_TREE, -1);
4859 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4860 to initialize VLAs will cause a proper error; avoid tree
4861 checking errors as well by setting a safe value. */
4862 if (constructor_max_index
4863 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4864 constructor_max_index = build_int_cst (NULL_TREE, -1);
4866 constructor_index
4867 = convert (bitsizetype,
4868 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4870 else
4871 constructor_index = bitsize_zero_node;
4873 constructor_unfilled_index = constructor_index;
4874 if (value && TREE_CODE (value) == STRING_CST)
4876 /* We need to split the char/wchar array into individual
4877 characters, so that we don't have to special case it
4878 everywhere. */
4879 set_nonincremental_init_from_string (value);
4882 else
4884 if (constructor_type != error_mark_node)
4885 warning_init ("braces around scalar initializer");
4886 constructor_fields = constructor_type;
4887 constructor_unfilled_fields = constructor_type;
4891 /* At the end of an implicit or explicit brace level,
4892 finish up that level of constructor. If a single expression
4893 with redundant braces initialized that level, return the
4894 c_expr structure for that expression. Otherwise, the original_code
4895 element is set to ERROR_MARK.
4896 If we were outputting the elements as they are read, return 0 as the value
4897 from inner levels (process_init_element ignores that),
4898 but return error_mark_node as the value from the outermost level
4899 (that's what we want to put in DECL_INITIAL).
4900 Otherwise, return a CONSTRUCTOR expression as the value. */
4902 struct c_expr
4903 pop_init_level (int implicit)
4905 struct constructor_stack *p;
4906 struct c_expr ret;
4907 ret.value = 0;
4908 ret.original_code = ERROR_MARK;
4910 if (implicit == 0)
4912 /* When we come to an explicit close brace,
4913 pop any inner levels that didn't have explicit braces. */
4914 while (constructor_stack->implicit)
4915 process_init_element (pop_init_level (1));
4917 gcc_assert (!constructor_range_stack);
4920 /* Now output all pending elements. */
4921 constructor_incremental = 1;
4922 output_pending_init_elements (1);
4924 p = constructor_stack;
4926 /* Error for initializing a flexible array member, or a zero-length
4927 array member in an inappropriate context. */
4928 if (constructor_type && constructor_fields
4929 && TREE_CODE (constructor_type) == ARRAY_TYPE
4930 && TYPE_DOMAIN (constructor_type)
4931 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4933 /* Silently discard empty initializations. The parser will
4934 already have pedwarned for empty brackets. */
4935 if (integer_zerop (constructor_unfilled_index))
4936 constructor_type = NULL_TREE;
4937 else
4939 gcc_assert (!TYPE_SIZE (constructor_type));
4941 if (constructor_depth > 2)
4942 error_init ("initialization of flexible array member in a nested context");
4943 else if (pedantic)
4944 pedwarn_init ("initialization of a flexible array member");
4946 /* We have already issued an error message for the existence
4947 of a flexible array member not at the end of the structure.
4948 Discard the initializer so that we do not abort later. */
4949 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4950 constructor_type = NULL_TREE;
4954 /* Warn when some struct elements are implicitly initialized to zero. */
4955 if (warn_missing_field_initializers
4956 && constructor_type
4957 && TREE_CODE (constructor_type) == RECORD_TYPE
4958 && constructor_unfilled_fields)
4960 /* Do not warn for flexible array members or zero-length arrays. */
4961 while (constructor_unfilled_fields
4962 && (!DECL_SIZE (constructor_unfilled_fields)
4963 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4964 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4966 /* Do not warn if this level of the initializer uses member
4967 designators; it is likely to be deliberate. */
4968 if (constructor_unfilled_fields && !constructor_designated)
4970 push_member_name (constructor_unfilled_fields);
4971 warning_init ("missing initializer");
4972 RESTORE_SPELLING_DEPTH (constructor_depth);
4976 /* Pad out the end of the structure. */
4977 if (p->replacement_value.value)
4978 /* If this closes a superfluous brace pair,
4979 just pass out the element between them. */
4980 ret = p->replacement_value;
4981 else if (constructor_type == 0)
4983 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4984 && TREE_CODE (constructor_type) != UNION_TYPE
4985 && TREE_CODE (constructor_type) != ARRAY_TYPE
4986 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4988 /* A nonincremental scalar initializer--just return
4989 the element, after verifying there is just one. */
4990 if (constructor_elements == 0)
4992 if (!constructor_erroneous)
4993 error_init ("empty scalar initializer");
4994 ret.value = error_mark_node;
4996 else if (TREE_CHAIN (constructor_elements) != 0)
4998 error_init ("extra elements in scalar initializer");
4999 ret.value = TREE_VALUE (constructor_elements);
5001 else
5002 ret.value = TREE_VALUE (constructor_elements);
5004 else
5006 if (constructor_erroneous)
5007 ret.value = error_mark_node;
5008 else
5010 ret.value = build_constructor (constructor_type,
5011 nreverse (constructor_elements));
5012 if (constructor_constant)
5013 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5014 if (constructor_constant && constructor_simple)
5015 TREE_STATIC (ret.value) = 1;
5019 constructor_type = p->type;
5020 constructor_fields = p->fields;
5021 constructor_index = p->index;
5022 constructor_max_index = p->max_index;
5023 constructor_unfilled_index = p->unfilled_index;
5024 constructor_unfilled_fields = p->unfilled_fields;
5025 constructor_bit_index = p->bit_index;
5026 constructor_elements = p->elements;
5027 constructor_constant = p->constant;
5028 constructor_simple = p->simple;
5029 constructor_erroneous = p->erroneous;
5030 constructor_incremental = p->incremental;
5031 constructor_designated = p->designated;
5032 constructor_pending_elts = p->pending_elts;
5033 constructor_depth = p->depth;
5034 if (!p->implicit)
5035 constructor_range_stack = p->range_stack;
5036 RESTORE_SPELLING_DEPTH (constructor_depth);
5038 constructor_stack = p->next;
5039 free (p);
5041 if (ret.value == 0)
5043 if (constructor_stack == 0)
5045 ret.value = error_mark_node;
5046 return ret;
5048 return ret;
5050 return ret;
5053 /* Common handling for both array range and field name designators.
5054 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5056 static int
5057 set_designator (int array)
5059 tree subtype;
5060 enum tree_code subcode;
5062 /* Don't die if an entire brace-pair level is superfluous
5063 in the containing level. */
5064 if (constructor_type == 0)
5065 return 1;
5067 /* If there were errors in this designator list already, bail out
5068 silently. */
5069 if (designator_errorneous)
5070 return 1;
5072 if (!designator_depth)
5074 gcc_assert (!constructor_range_stack);
5076 /* Designator list starts at the level of closest explicit
5077 braces. */
5078 while (constructor_stack->implicit)
5079 process_init_element (pop_init_level (1));
5080 constructor_designated = 1;
5081 return 0;
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
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
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
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_type != error_mark_node
6248 && constructor_fields == 0)
6250 pedwarn_init ("excess elements in scalar initializer");
6251 break;
6253 else
6255 if (value.value)
6256 output_init_element (value.value, strict_string,
6257 constructor_type, NULL_TREE, 1);
6258 constructor_fields = 0;
6261 /* Handle range initializers either at this level or anywhere higher
6262 in the designator stack. */
6263 if (constructor_range_stack)
6265 struct constructor_range_stack *p, *range_stack;
6266 int finish = 0;
6268 range_stack = constructor_range_stack;
6269 constructor_range_stack = 0;
6270 while (constructor_stack != range_stack->stack)
6272 gcc_assert (constructor_stack->implicit);
6273 process_init_element (pop_init_level (1));
6275 for (p = range_stack;
6276 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6277 p = p->prev)
6279 gcc_assert (constructor_stack->implicit);
6280 process_init_element (pop_init_level (1));
6283 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6284 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6285 finish = 1;
6287 while (1)
6289 constructor_index = p->index;
6290 constructor_fields = p->fields;
6291 if (finish && p->range_end && p->index == p->range_start)
6293 finish = 0;
6294 p->prev = 0;
6296 p = p->next;
6297 if (!p)
6298 break;
6299 push_init_level (2);
6300 p->stack = constructor_stack;
6301 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6302 p->index = p->range_start;
6305 if (!finish)
6306 constructor_range_stack = range_stack;
6307 continue;
6310 break;
6313 constructor_range_stack = 0;
6316 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6317 (guaranteed to be 'volatile' or null) and ARGS (represented using
6318 an ASM_EXPR node). */
6319 tree
6320 build_asm_stmt (tree cv_qualifier, tree args)
6322 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6323 ASM_VOLATILE_P (args) = 1;
6324 return add_stmt (args);
6327 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6328 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6329 SIMPLE indicates whether there was anything at all after the
6330 string in the asm expression -- asm("blah") and asm("blah" : )
6331 are subtly different. We use a ASM_EXPR node to represent this. */
6332 tree
6333 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6334 bool simple)
6336 tree tail;
6337 tree args;
6338 int i;
6339 const char *constraint;
6340 const char **oconstraints;
6341 bool allows_mem, allows_reg, is_inout;
6342 int ninputs, noutputs;
6344 ninputs = list_length (inputs);
6345 noutputs = list_length (outputs);
6346 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6348 string = resolve_asm_operand_names (string, outputs, inputs);
6350 /* Remove output conversions that change the type but not the mode. */
6351 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6353 tree output = TREE_VALUE (tail);
6355 /* ??? Really, this should not be here. Users should be using a
6356 proper lvalue, dammit. But there's a long history of using casts
6357 in the output operands. In cases like longlong.h, this becomes a
6358 primitive form of typechecking -- if the cast can be removed, then
6359 the output operand had a type of the proper width; otherwise we'll
6360 get an error. Gross, but ... */
6361 STRIP_NOPS (output);
6363 if (!lvalue_or_else (output, lv_asm))
6364 output = error_mark_node;
6366 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6367 oconstraints[i] = constraint;
6369 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6370 &allows_mem, &allows_reg, &is_inout))
6372 /* If the operand is going to end up in memory,
6373 mark it addressable. */
6374 if (!allows_reg && !c_mark_addressable (output))
6375 output = error_mark_node;
6377 else
6378 output = error_mark_node;
6380 TREE_VALUE (tail) = output;
6383 /* Perform default conversions on array and function inputs.
6384 Don't do this for other types as it would screw up operands
6385 expected to be in memory. */
6386 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6388 tree input;
6390 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6391 input = TREE_VALUE (tail);
6393 input = default_function_array_conversion (input);
6395 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6396 oconstraints, &allows_mem, &allows_reg))
6398 /* If the operand is going to end up in memory,
6399 mark it addressable. */
6400 if (!allows_reg && allows_mem)
6402 /* Strip the nops as we allow this case. FIXME, this really
6403 should be rejected or made deprecated. */
6404 STRIP_NOPS (input);
6405 if (!c_mark_addressable (input))
6406 input = error_mark_node;
6409 else
6410 input = error_mark_node;
6412 TREE_VALUE (tail) = input;
6415 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6417 /* Simple asm statements are treated as volatile. */
6418 if (simple)
6420 ASM_VOLATILE_P (args) = 1;
6421 ASM_INPUT_P (args) = 1;
6424 return args;
6427 /* Generate a goto statement to LABEL. */
6429 tree
6430 c_finish_goto_label (tree label)
6432 tree decl = lookup_label (label);
6433 if (!decl)
6434 return NULL_TREE;
6436 TREE_USED (decl) = 1;
6437 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6440 /* Generate a computed goto statement to EXPR. */
6442 tree
6443 c_finish_goto_ptr (tree expr)
6445 if (pedantic)
6446 pedwarn ("ISO C forbids %<goto *expr;%>");
6447 expr = convert (ptr_type_node, expr);
6448 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6451 /* Generate a C `return' statement. RETVAL is the expression for what
6452 to return, or a null pointer for `return;' with no value. */
6454 tree
6455 c_finish_return (tree retval)
6457 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6459 if (TREE_THIS_VOLATILE (current_function_decl))
6460 warning ("function declared %<noreturn%> has a %<return%> statement");
6462 if (!retval)
6464 current_function_returns_null = 1;
6465 if ((warn_return_type || flag_isoc99)
6466 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6467 pedwarn_c99 ("%<return%> with no value, in "
6468 "function returning non-void");
6470 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6472 current_function_returns_null = 1;
6473 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6474 pedwarn ("%<return%> with a value, in function returning void");
6476 else
6478 tree t = convert_for_assignment (valtype, retval, ic_return,
6479 NULL_TREE, NULL_TREE, 0);
6480 tree res = DECL_RESULT (current_function_decl);
6481 tree inner;
6483 current_function_returns_value = 1;
6484 if (t == error_mark_node)
6485 return NULL_TREE;
6487 inner = t = convert (TREE_TYPE (res), t);
6489 /* Strip any conversions, additions, and subtractions, and see if
6490 we are returning the address of a local variable. Warn if so. */
6491 while (1)
6493 switch (TREE_CODE (inner))
6495 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6496 case PLUS_EXPR:
6497 inner = TREE_OPERAND (inner, 0);
6498 continue;
6500 case MINUS_EXPR:
6501 /* If the second operand of the MINUS_EXPR has a pointer
6502 type (or is converted from it), this may be valid, so
6503 don't give a warning. */
6505 tree op1 = TREE_OPERAND (inner, 1);
6507 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6508 && (TREE_CODE (op1) == NOP_EXPR
6509 || TREE_CODE (op1) == NON_LVALUE_EXPR
6510 || TREE_CODE (op1) == CONVERT_EXPR))
6511 op1 = TREE_OPERAND (op1, 0);
6513 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6514 break;
6516 inner = TREE_OPERAND (inner, 0);
6517 continue;
6520 case ADDR_EXPR:
6521 inner = TREE_OPERAND (inner, 0);
6523 while (REFERENCE_CLASS_P (inner)
6524 && TREE_CODE (inner) != INDIRECT_REF)
6525 inner = TREE_OPERAND (inner, 0);
6527 if (DECL_P (inner)
6528 && !DECL_EXTERNAL (inner)
6529 && !TREE_STATIC (inner)
6530 && DECL_CONTEXT (inner) == current_function_decl)
6531 warning ("function returns address of local variable");
6532 break;
6534 default:
6535 break;
6538 break;
6541 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6544 return add_stmt (build_stmt (RETURN_EXPR, retval));
6547 struct c_switch {
6548 /* The SWITCH_STMT being built. */
6549 tree switch_stmt;
6551 /* The original type of the testing expression, i.e. before the
6552 default conversion is applied. */
6553 tree orig_type;
6555 /* A splay-tree mapping the low element of a case range to the high
6556 element, or NULL_TREE if there is no high element. Used to
6557 determine whether or not a new case label duplicates an old case
6558 label. We need a tree, rather than simply a hash table, because
6559 of the GNU case range extension. */
6560 splay_tree cases;
6562 /* The next node on the stack. */
6563 struct c_switch *next;
6566 /* A stack of the currently active switch statements. The innermost
6567 switch statement is on the top of the stack. There is no need to
6568 mark the stack for garbage collection because it is only active
6569 during the processing of the body of a function, and we never
6570 collect at that point. */
6572 struct c_switch *c_switch_stack;
6574 /* Start a C switch statement, testing expression EXP. Return the new
6575 SWITCH_STMT. */
6577 tree
6578 c_start_case (tree exp)
6580 enum tree_code code;
6581 tree type, orig_type = error_mark_node;
6582 struct c_switch *cs;
6584 if (exp != error_mark_node)
6586 code = TREE_CODE (TREE_TYPE (exp));
6587 orig_type = TREE_TYPE (exp);
6589 if (!INTEGRAL_TYPE_P (orig_type)
6590 && code != ERROR_MARK)
6592 error ("switch quantity not an integer");
6593 exp = integer_zero_node;
6594 orig_type = error_mark_node;
6596 else
6598 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6600 if (warn_traditional && !in_system_header
6601 && (type == long_integer_type_node
6602 || type == long_unsigned_type_node))
6603 warning ("%<long%> switch expression not converted to "
6604 "%<int%> in ISO C");
6606 exp = default_conversion (exp);
6607 type = TREE_TYPE (exp);
6611 /* Add this new SWITCH_STMT to the stack. */
6612 cs = XNEW (struct c_switch);
6613 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6614 cs->orig_type = orig_type;
6615 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6616 cs->next = c_switch_stack;
6617 c_switch_stack = cs;
6619 return add_stmt (cs->switch_stmt);
6622 /* Process a case label. */
6624 tree
6625 do_case (tree low_value, tree high_value)
6627 tree label = NULL_TREE;
6629 if (c_switch_stack)
6631 label = c_add_case_label (c_switch_stack->cases,
6632 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
6633 c_switch_stack->orig_type,
6634 low_value, high_value);
6635 if (label == error_mark_node)
6636 label = NULL_TREE;
6638 else if (low_value)
6639 error ("case label not within a switch statement");
6640 else
6641 error ("%<default%> label not within a switch statement");
6643 return label;
6646 /* Finish the switch statement. */
6648 void
6649 c_finish_case (tree body)
6651 struct c_switch *cs = c_switch_stack;
6653 SWITCH_STMT_BODY (cs->switch_stmt) = body;
6655 /* Emit warnings as needed. */
6656 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6658 /* Pop the stack. */
6659 c_switch_stack = cs->next;
6660 splay_tree_delete (cs->cases);
6661 XDELETE (cs);
6664 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6665 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6666 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6667 statement, and was not surrounded with parenthesis. */
6669 void
6670 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6671 tree else_block, bool nested_if)
6673 tree stmt;
6675 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6676 if (warn_parentheses && nested_if && else_block == NULL)
6678 tree inner_if = then_block;
6680 /* We know from the grammar productions that there is an IF nested
6681 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6682 it might not be exactly THEN_BLOCK, but should be the last
6683 non-container statement within. */
6684 while (1)
6685 switch (TREE_CODE (inner_if))
6687 case COND_EXPR:
6688 goto found;
6689 case BIND_EXPR:
6690 inner_if = BIND_EXPR_BODY (inner_if);
6691 break;
6692 case STATEMENT_LIST:
6693 inner_if = expr_last (then_block);
6694 break;
6695 case TRY_FINALLY_EXPR:
6696 case TRY_CATCH_EXPR:
6697 inner_if = TREE_OPERAND (inner_if, 0);
6698 break;
6699 default:
6700 gcc_unreachable ();
6702 found:
6704 if (COND_EXPR_ELSE (inner_if))
6705 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6706 &if_locus);
6709 /* Diagnose ";" via the special empty statement node that we create. */
6710 if (extra_warnings)
6712 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6714 if (!else_block)
6715 warning ("%Hempty body in an if-statement",
6716 EXPR_LOCUS (then_block));
6717 then_block = alloc_stmt_list ();
6719 if (else_block
6720 && TREE_CODE (else_block) == NOP_EXPR
6721 && !TREE_TYPE (else_block))
6723 warning ("%Hempty body in an else-statement",
6724 EXPR_LOCUS (else_block));
6725 else_block = alloc_stmt_list ();
6729 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6730 SET_EXPR_LOCATION (stmt, if_locus);
6731 add_stmt (stmt);
6734 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6735 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6736 is false for DO loops. INCR is the FOR increment expression. BODY is
6737 the statement controlled by the loop. BLAB is the break label. CLAB is
6738 the continue label. Everything is allowed to be NULL. */
6740 void
6741 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6742 tree blab, tree clab, bool cond_is_first)
6744 tree entry = NULL, exit = NULL, t;
6746 /* If the condition is zero don't generate a loop construct. */
6747 if (cond && integer_zerop (cond))
6749 if (cond_is_first)
6751 t = build_and_jump (&blab);
6752 SET_EXPR_LOCATION (t, start_locus);
6753 add_stmt (t);
6756 else
6758 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6760 /* If we have an exit condition, then we build an IF with gotos either
6761 out of the loop, or to the top of it. If there's no exit condition,
6762 then we just build a jump back to the top. */
6763 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6765 if (cond && !integer_nonzerop (cond))
6767 /* Canonicalize the loop condition to the end. This means
6768 generating a branch to the loop condition. Reuse the
6769 continue label, if possible. */
6770 if (cond_is_first)
6772 if (incr || !clab)
6774 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6775 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6777 else
6778 t = build1 (GOTO_EXPR, void_type_node, clab);
6779 SET_EXPR_LOCATION (t, start_locus);
6780 add_stmt (t);
6783 t = build_and_jump (&blab);
6784 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6785 exit = fold (exit);
6786 if (cond_is_first)
6787 SET_EXPR_LOCATION (exit, start_locus);
6788 else
6789 SET_EXPR_LOCATION (exit, input_location);
6792 add_stmt (top);
6795 if (body)
6796 add_stmt (body);
6797 if (clab)
6798 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6799 if (incr)
6800 add_stmt (incr);
6801 if (entry)
6802 add_stmt (entry);
6803 if (exit)
6804 add_stmt (exit);
6805 if (blab)
6806 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6809 tree
6810 c_finish_bc_stmt (tree *label_p, bool is_break)
6812 bool skip;
6813 tree label = *label_p;
6815 /* In switch statements break is sometimes stylistically used after
6816 a return statement. This can lead to spurious warnings about
6817 control reaching the end of a non-void function when it is
6818 inlined. Note that we are calling block_may_fallthru with
6819 language specific tree nodes; this works because
6820 block_may_fallthru returns true when given something it does not
6821 understand. */
6822 skip = !block_may_fallthru (cur_stmt_list);
6824 if (!label)
6826 if (!skip)
6827 *label_p = label = create_artificial_label ();
6829 else if (TREE_CODE (label) != LABEL_DECL)
6831 if (is_break)
6832 error ("break statement not within loop or switch");
6833 else
6834 error ("continue statement not within a loop");
6835 return NULL_TREE;
6838 if (skip)
6839 return NULL_TREE;
6841 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6844 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6846 static void
6847 emit_side_effect_warnings (tree expr)
6849 if (expr == error_mark_node)
6851 else if (!TREE_SIDE_EFFECTS (expr))
6853 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6854 warning ("%Hstatement with no effect",
6855 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6857 else if (warn_unused_value)
6858 warn_if_unused_value (expr, input_location);
6861 /* Process an expression as if it were a complete statement. Emit
6862 diagnostics, but do not call ADD_STMT. */
6864 tree
6865 c_process_expr_stmt (tree expr)
6867 if (!expr)
6868 return NULL_TREE;
6870 /* Do default conversion if safe and possibly important,
6871 in case within ({...}). */
6872 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6873 && (flag_isoc99 || lvalue_p (expr)))
6874 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6875 expr = default_conversion (expr);
6877 if (warn_sequence_point)
6878 verify_sequence_points (expr);
6880 if (TREE_TYPE (expr) != error_mark_node
6881 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6882 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6883 error ("expression statement has incomplete type");
6885 /* If we're not processing a statement expression, warn about unused values.
6886 Warnings for statement expressions will be emitted later, once we figure
6887 out which is the result. */
6888 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6889 && (extra_warnings || warn_unused_value))
6890 emit_side_effect_warnings (expr);
6892 /* If the expression is not of a type to which we cannot assign a line
6893 number, wrap the thing in a no-op NOP_EXPR. */
6894 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6895 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6897 if (EXPR_P (expr))
6898 SET_EXPR_LOCATION (expr, input_location);
6900 return expr;
6903 /* Emit an expression as a statement. */
6905 tree
6906 c_finish_expr_stmt (tree expr)
6908 if (expr)
6909 return add_stmt (c_process_expr_stmt (expr));
6910 else
6911 return NULL;
6914 /* Do the opposite and emit a statement as an expression. To begin,
6915 create a new binding level and return it. */
6917 tree
6918 c_begin_stmt_expr (void)
6920 tree ret;
6922 /* We must force a BLOCK for this level so that, if it is not expanded
6923 later, there is a way to turn off the entire subtree of blocks that
6924 are contained in it. */
6925 keep_next_level ();
6926 ret = c_begin_compound_stmt (true);
6928 /* Mark the current statement list as belonging to a statement list. */
6929 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6931 return ret;
6934 tree
6935 c_finish_stmt_expr (tree body)
6937 tree last, type, tmp, val;
6938 tree *last_p;
6940 body = c_end_compound_stmt (body, true);
6942 /* Locate the last statement in BODY. See c_end_compound_stmt
6943 about always returning a BIND_EXPR. */
6944 last_p = &BIND_EXPR_BODY (body);
6945 last = BIND_EXPR_BODY (body);
6947 continue_searching:
6948 if (TREE_CODE (last) == STATEMENT_LIST)
6950 tree_stmt_iterator i;
6952 /* This can happen with degenerate cases like ({ }). No value. */
6953 if (!TREE_SIDE_EFFECTS (last))
6954 return body;
6956 /* If we're supposed to generate side effects warnings, process
6957 all of the statements except the last. */
6958 if (extra_warnings || warn_unused_value)
6960 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6961 emit_side_effect_warnings (tsi_stmt (i));
6963 else
6964 i = tsi_last (last);
6965 last_p = tsi_stmt_ptr (i);
6966 last = *last_p;
6969 /* If the end of the list is exception related, then the list was split
6970 by a call to push_cleanup. Continue searching. */
6971 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6972 || TREE_CODE (last) == TRY_CATCH_EXPR)
6974 last_p = &TREE_OPERAND (last, 0);
6975 last = *last_p;
6976 goto continue_searching;
6979 /* In the case that the BIND_EXPR is not necessary, return the
6980 expression out from inside it. */
6981 if (last == error_mark_node
6982 || (last == BIND_EXPR_BODY (body)
6983 && BIND_EXPR_VARS (body) == NULL))
6984 return last;
6986 /* Extract the type of said expression. */
6987 type = TREE_TYPE (last);
6989 /* If we're not returning a value at all, then the BIND_EXPR that
6990 we already have is a fine expression to return. */
6991 if (!type || VOID_TYPE_P (type))
6992 return body;
6994 /* Now that we've located the expression containing the value, it seems
6995 silly to make voidify_wrapper_expr repeat the process. Create a
6996 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6997 tmp = create_tmp_var_raw (type, NULL);
6999 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7000 tree_expr_nonnegative_p giving up immediately. */
7001 val = last;
7002 if (TREE_CODE (val) == NOP_EXPR
7003 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7004 val = TREE_OPERAND (val, 0);
7006 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7007 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7009 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7012 /* Begin and end compound statements. This is as simple as pushing
7013 and popping new statement lists from the tree. */
7015 tree
7016 c_begin_compound_stmt (bool do_scope)
7018 tree stmt = push_stmt_list ();
7019 if (do_scope)
7020 push_scope ();
7021 return stmt;
7024 tree
7025 c_end_compound_stmt (tree stmt, bool do_scope)
7027 tree block = NULL;
7029 if (do_scope)
7031 if (c_dialect_objc ())
7032 objc_clear_super_receiver ();
7033 block = pop_scope ();
7036 stmt = pop_stmt_list (stmt);
7037 stmt = c_build_bind_expr (block, stmt);
7039 /* If this compound statement is nested immediately inside a statement
7040 expression, then force a BIND_EXPR to be created. Otherwise we'll
7041 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7042 STATEMENT_LISTs merge, and thus we can lose track of what statement
7043 was really last. */
7044 if (cur_stmt_list
7045 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7046 && TREE_CODE (stmt) != BIND_EXPR)
7048 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7049 TREE_SIDE_EFFECTS (stmt) = 1;
7052 return stmt;
7055 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7056 when the current scope is exited. EH_ONLY is true when this is not
7057 meant to apply to normal control flow transfer. */
7059 void
7060 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7062 enum tree_code code;
7063 tree stmt, list;
7064 bool stmt_expr;
7066 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7067 stmt = build_stmt (code, NULL, cleanup);
7068 add_stmt (stmt);
7069 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7070 list = push_stmt_list ();
7071 TREE_OPERAND (stmt, 0) = list;
7072 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7075 /* Build a binary-operation expression without default conversions.
7076 CODE is the kind of expression to build.
7077 This function differs from `build' in several ways:
7078 the data type of the result is computed and recorded in it,
7079 warnings are generated if arg data types are invalid,
7080 special handling for addition and subtraction of pointers is known,
7081 and some optimization is done (operations on narrow ints
7082 are done in the narrower type when that gives the same result).
7083 Constant folding is also done before the result is returned.
7085 Note that the operands will never have enumeral types, or function
7086 or array types, because either they will have the default conversions
7087 performed or they have both just been converted to some other type in which
7088 the arithmetic is to be done. */
7090 tree
7091 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7092 int convert_p)
7094 tree type0, type1;
7095 enum tree_code code0, code1;
7096 tree op0, op1;
7098 /* Expression code to give to the expression when it is built.
7099 Normally this is CODE, which is what the caller asked for,
7100 but in some special cases we change it. */
7101 enum tree_code resultcode = code;
7103 /* Data type in which the computation is to be performed.
7104 In the simplest cases this is the common type of the arguments. */
7105 tree result_type = NULL;
7107 /* Nonzero means operands have already been type-converted
7108 in whatever way is necessary.
7109 Zero means they need to be converted to RESULT_TYPE. */
7110 int converted = 0;
7112 /* Nonzero means create the expression with this type, rather than
7113 RESULT_TYPE. */
7114 tree build_type = 0;
7116 /* Nonzero means after finally constructing the expression
7117 convert it to this type. */
7118 tree final_type = 0;
7120 /* Nonzero if this is an operation like MIN or MAX which can
7121 safely be computed in short if both args are promoted shorts.
7122 Also implies COMMON.
7123 -1 indicates a bitwise operation; this makes a difference
7124 in the exact conditions for when it is safe to do the operation
7125 in a narrower mode. */
7126 int shorten = 0;
7128 /* Nonzero if this is a comparison operation;
7129 if both args are promoted shorts, compare the original shorts.
7130 Also implies COMMON. */
7131 int short_compare = 0;
7133 /* Nonzero if this is a right-shift operation, which can be computed on the
7134 original short and then promoted if the operand is a promoted short. */
7135 int short_shift = 0;
7137 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7138 int common = 0;
7140 if (convert_p)
7142 op0 = default_conversion (orig_op0);
7143 op1 = default_conversion (orig_op1);
7145 else
7147 op0 = orig_op0;
7148 op1 = orig_op1;
7151 type0 = TREE_TYPE (op0);
7152 type1 = TREE_TYPE (op1);
7154 /* The expression codes of the data types of the arguments tell us
7155 whether the arguments are integers, floating, pointers, etc. */
7156 code0 = TREE_CODE (type0);
7157 code1 = TREE_CODE (type1);
7159 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7160 STRIP_TYPE_NOPS (op0);
7161 STRIP_TYPE_NOPS (op1);
7163 /* If an error was already reported for one of the arguments,
7164 avoid reporting another error. */
7166 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7167 return error_mark_node;
7169 switch (code)
7171 case PLUS_EXPR:
7172 /* Handle the pointer + int case. */
7173 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7174 return pointer_int_sum (PLUS_EXPR, op0, op1);
7175 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7176 return pointer_int_sum (PLUS_EXPR, op1, op0);
7177 else
7178 common = 1;
7179 break;
7181 case MINUS_EXPR:
7182 /* Subtraction of two similar pointers.
7183 We must subtract them as integers, then divide by object size. */
7184 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7185 && comp_target_types (type0, type1, 1))
7186 return pointer_diff (op0, op1);
7187 /* Handle pointer minus int. Just like pointer plus int. */
7188 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7189 return pointer_int_sum (MINUS_EXPR, op0, op1);
7190 else
7191 common = 1;
7192 break;
7194 case MULT_EXPR:
7195 common = 1;
7196 break;
7198 case TRUNC_DIV_EXPR:
7199 case CEIL_DIV_EXPR:
7200 case FLOOR_DIV_EXPR:
7201 case ROUND_DIV_EXPR:
7202 case EXACT_DIV_EXPR:
7203 /* Floating point division by zero is a legitimate way to obtain
7204 infinities and NaNs. */
7205 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7206 warning ("division by zero");
7208 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7209 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7210 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7211 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7213 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7214 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7215 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7216 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7218 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7219 resultcode = RDIV_EXPR;
7220 else
7221 /* Although it would be tempting to shorten always here, that
7222 loses on some targets, since the modulo instruction is
7223 undefined if the quotient can't be represented in the
7224 computation mode. We shorten only if unsigned or if
7225 dividing by something we know != -1. */
7226 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7227 || (TREE_CODE (op1) == INTEGER_CST
7228 && !integer_all_onesp (op1)));
7229 common = 1;
7231 break;
7233 case BIT_AND_EXPR:
7234 case BIT_IOR_EXPR:
7235 case BIT_XOR_EXPR:
7236 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7237 shorten = -1;
7238 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7239 common = 1;
7240 break;
7242 case TRUNC_MOD_EXPR:
7243 case FLOOR_MOD_EXPR:
7244 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7245 warning ("division by zero");
7247 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7249 /* Although it would be tempting to shorten always here, that loses
7250 on some targets, since the modulo instruction is undefined if the
7251 quotient can't be represented in the computation mode. We shorten
7252 only if unsigned or if dividing by something we know != -1. */
7253 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7254 || (TREE_CODE (op1) == INTEGER_CST
7255 && !integer_all_onesp (op1)));
7256 common = 1;
7258 break;
7260 case TRUTH_ANDIF_EXPR:
7261 case TRUTH_ORIF_EXPR:
7262 case TRUTH_AND_EXPR:
7263 case TRUTH_OR_EXPR:
7264 case TRUTH_XOR_EXPR:
7265 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7266 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7267 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7268 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7270 /* Result of these operations is always an int,
7271 but that does not mean the operands should be
7272 converted to ints! */
7273 result_type = integer_type_node;
7274 op0 = lang_hooks.truthvalue_conversion (op0);
7275 op1 = lang_hooks.truthvalue_conversion (op1);
7276 converted = 1;
7278 break;
7280 /* Shift operations: result has same type as first operand;
7281 always convert second operand to int.
7282 Also set SHORT_SHIFT if shifting rightward. */
7284 case RSHIFT_EXPR:
7285 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7287 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7289 if (tree_int_cst_sgn (op1) < 0)
7290 warning ("right shift count is negative");
7291 else
7293 if (!integer_zerop (op1))
7294 short_shift = 1;
7296 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7297 warning ("right shift count >= width of type");
7301 /* Use the type of the value to be shifted. */
7302 result_type = type0;
7303 /* Convert the shift-count to an integer, regardless of size
7304 of value being shifted. */
7305 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7306 op1 = convert (integer_type_node, op1);
7307 /* Avoid converting op1 to result_type later. */
7308 converted = 1;
7310 break;
7312 case LSHIFT_EXPR:
7313 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7315 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7317 if (tree_int_cst_sgn (op1) < 0)
7318 warning ("left shift count is negative");
7320 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7321 warning ("left shift count >= width of type");
7324 /* Use the type of the value to be shifted. */
7325 result_type = type0;
7326 /* Convert the shift-count to an integer, regardless of size
7327 of value being shifted. */
7328 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7329 op1 = convert (integer_type_node, op1);
7330 /* Avoid converting op1 to result_type later. */
7331 converted = 1;
7333 break;
7335 case EQ_EXPR:
7336 case NE_EXPR:
7337 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7338 warning ("comparing floating point with == or != is unsafe");
7339 /* Result of comparison is always int,
7340 but don't convert the args to int! */
7341 build_type = integer_type_node;
7342 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7343 || code0 == COMPLEX_TYPE)
7344 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7345 || code1 == COMPLEX_TYPE))
7346 short_compare = 1;
7347 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7349 tree tt0 = TREE_TYPE (type0);
7350 tree tt1 = TREE_TYPE (type1);
7351 /* Anything compares with void *. void * compares with anything.
7352 Otherwise, the targets must be compatible
7353 and both must be object or both incomplete. */
7354 if (comp_target_types (type0, type1, 1))
7355 result_type = common_pointer_type (type0, type1);
7356 else if (VOID_TYPE_P (tt0))
7358 /* op0 != orig_op0 detects the case of something
7359 whose value is 0 but which isn't a valid null ptr const. */
7360 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7361 && TREE_CODE (tt1) == FUNCTION_TYPE)
7362 pedwarn ("ISO C forbids comparison of %<void *%>"
7363 " with function pointer");
7365 else if (VOID_TYPE_P (tt1))
7367 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7368 && TREE_CODE (tt0) == FUNCTION_TYPE)
7369 pedwarn ("ISO C forbids comparison of %<void *%>"
7370 " with function pointer");
7372 else
7373 pedwarn ("comparison of distinct pointer types lacks a cast");
7375 if (result_type == NULL_TREE)
7376 result_type = ptr_type_node;
7378 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7379 && integer_zerop (op1))
7380 result_type = type0;
7381 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7382 && integer_zerop (op0))
7383 result_type = type1;
7384 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7386 result_type = type0;
7387 pedwarn ("comparison between pointer and integer");
7389 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7391 result_type = type1;
7392 pedwarn ("comparison between pointer and integer");
7394 break;
7396 case LE_EXPR:
7397 case GE_EXPR:
7398 case LT_EXPR:
7399 case GT_EXPR:
7400 build_type = integer_type_node;
7401 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7402 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7403 short_compare = 1;
7404 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7406 if (comp_target_types (type0, type1, 1))
7408 result_type = common_pointer_type (type0, type1);
7409 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7410 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7411 pedwarn ("comparison of complete and incomplete pointers");
7412 else if (pedantic
7413 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7414 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7416 else
7418 result_type = ptr_type_node;
7419 pedwarn ("comparison of distinct pointer types lacks a cast");
7422 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7423 && integer_zerop (op1))
7425 result_type = type0;
7426 if (pedantic || extra_warnings)
7427 pedwarn ("ordered comparison of pointer with integer zero");
7429 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7430 && integer_zerop (op0))
7432 result_type = type1;
7433 if (pedantic)
7434 pedwarn ("ordered comparison of pointer with integer zero");
7436 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7438 result_type = type0;
7439 pedwarn ("comparison between pointer and integer");
7441 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7443 result_type = type1;
7444 pedwarn ("comparison between pointer and integer");
7446 break;
7448 default:
7449 gcc_unreachable ();
7452 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7453 return error_mark_node;
7455 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7456 || code0 == VECTOR_TYPE)
7458 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7459 || code1 == VECTOR_TYPE))
7461 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7463 if (shorten || common || short_compare)
7464 result_type = common_type (type0, type1);
7466 /* For certain operations (which identify themselves by shorten != 0)
7467 if both args were extended from the same smaller type,
7468 do the arithmetic in that type and then extend.
7470 shorten !=0 and !=1 indicates a bitwise operation.
7471 For them, this optimization is safe only if
7472 both args are zero-extended or both are sign-extended.
7473 Otherwise, we might change the result.
7474 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7475 but calculated in (unsigned short) it would be (unsigned short)-1. */
7477 if (shorten && none_complex)
7479 int unsigned0, unsigned1;
7480 tree arg0 = get_narrower (op0, &unsigned0);
7481 tree arg1 = get_narrower (op1, &unsigned1);
7482 /* UNS is 1 if the operation to be done is an unsigned one. */
7483 int uns = TYPE_UNSIGNED (result_type);
7484 tree type;
7486 final_type = result_type;
7488 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7489 but it *requires* conversion to FINAL_TYPE. */
7491 if ((TYPE_PRECISION (TREE_TYPE (op0))
7492 == TYPE_PRECISION (TREE_TYPE (arg0)))
7493 && TREE_TYPE (op0) != final_type)
7494 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7495 if ((TYPE_PRECISION (TREE_TYPE (op1))
7496 == TYPE_PRECISION (TREE_TYPE (arg1)))
7497 && TREE_TYPE (op1) != final_type)
7498 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7500 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7502 /* For bitwise operations, signedness of nominal type
7503 does not matter. Consider only how operands were extended. */
7504 if (shorten == -1)
7505 uns = unsigned0;
7507 /* Note that in all three cases below we refrain from optimizing
7508 an unsigned operation on sign-extended args.
7509 That would not be valid. */
7511 /* Both args variable: if both extended in same way
7512 from same width, do it in that width.
7513 Do it unsigned if args were zero-extended. */
7514 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7515 < TYPE_PRECISION (result_type))
7516 && (TYPE_PRECISION (TREE_TYPE (arg1))
7517 == TYPE_PRECISION (TREE_TYPE (arg0)))
7518 && unsigned0 == unsigned1
7519 && (unsigned0 || !uns))
7520 result_type
7521 = c_common_signed_or_unsigned_type
7522 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7523 else if (TREE_CODE (arg0) == INTEGER_CST
7524 && (unsigned1 || !uns)
7525 && (TYPE_PRECISION (TREE_TYPE (arg1))
7526 < TYPE_PRECISION (result_type))
7527 && (type
7528 = c_common_signed_or_unsigned_type (unsigned1,
7529 TREE_TYPE (arg1)),
7530 int_fits_type_p (arg0, type)))
7531 result_type = type;
7532 else if (TREE_CODE (arg1) == INTEGER_CST
7533 && (unsigned0 || !uns)
7534 && (TYPE_PRECISION (TREE_TYPE (arg0))
7535 < TYPE_PRECISION (result_type))
7536 && (type
7537 = c_common_signed_or_unsigned_type (unsigned0,
7538 TREE_TYPE (arg0)),
7539 int_fits_type_p (arg1, type)))
7540 result_type = type;
7543 /* Shifts can be shortened if shifting right. */
7545 if (short_shift)
7547 int unsigned_arg;
7548 tree arg0 = get_narrower (op0, &unsigned_arg);
7550 final_type = result_type;
7552 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7553 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7555 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7556 /* We can shorten only if the shift count is less than the
7557 number of bits in the smaller type size. */
7558 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7559 /* We cannot drop an unsigned shift after sign-extension. */
7560 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7562 /* Do an unsigned shift if the operand was zero-extended. */
7563 result_type
7564 = c_common_signed_or_unsigned_type (unsigned_arg,
7565 TREE_TYPE (arg0));
7566 /* Convert value-to-be-shifted to that type. */
7567 if (TREE_TYPE (op0) != result_type)
7568 op0 = convert (result_type, op0);
7569 converted = 1;
7573 /* Comparison operations are shortened too but differently.
7574 They identify themselves by setting short_compare = 1. */
7576 if (short_compare)
7578 /* Don't write &op0, etc., because that would prevent op0
7579 from being kept in a register.
7580 Instead, make copies of the our local variables and
7581 pass the copies by reference, then copy them back afterward. */
7582 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7583 enum tree_code xresultcode = resultcode;
7584 tree val
7585 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7587 if (val != 0)
7588 return val;
7590 op0 = xop0, op1 = xop1;
7591 converted = 1;
7592 resultcode = xresultcode;
7594 if (warn_sign_compare && skip_evaluation == 0)
7596 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7597 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7598 int unsignedp0, unsignedp1;
7599 tree primop0 = get_narrower (op0, &unsignedp0);
7600 tree primop1 = get_narrower (op1, &unsignedp1);
7602 xop0 = orig_op0;
7603 xop1 = orig_op1;
7604 STRIP_TYPE_NOPS (xop0);
7605 STRIP_TYPE_NOPS (xop1);
7607 /* Give warnings for comparisons between signed and unsigned
7608 quantities that may fail.
7610 Do the checking based on the original operand trees, so that
7611 casts will be considered, but default promotions won't be.
7613 Do not warn if the comparison is being done in a signed type,
7614 since the signed type will only be chosen if it can represent
7615 all the values of the unsigned type. */
7616 if (!TYPE_UNSIGNED (result_type))
7617 /* OK */;
7618 /* Do not warn if both operands are the same signedness. */
7619 else if (op0_signed == op1_signed)
7620 /* OK */;
7621 else
7623 tree sop, uop;
7625 if (op0_signed)
7626 sop = xop0, uop = xop1;
7627 else
7628 sop = xop1, uop = xop0;
7630 /* Do not warn if the signed quantity is an
7631 unsuffixed integer literal (or some static
7632 constant expression involving such literals or a
7633 conditional expression involving such literals)
7634 and it is non-negative. */
7635 if (tree_expr_nonnegative_p (sop))
7636 /* OK */;
7637 /* Do not warn if the comparison is an equality operation,
7638 the unsigned quantity is an integral constant, and it
7639 would fit in the result if the result were signed. */
7640 else if (TREE_CODE (uop) == INTEGER_CST
7641 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7642 && int_fits_type_p
7643 (uop, c_common_signed_type (result_type)))
7644 /* OK */;
7645 /* Do not warn if the unsigned quantity is an enumeration
7646 constant and its maximum value would fit in the result
7647 if the result were signed. */
7648 else if (TREE_CODE (uop) == INTEGER_CST
7649 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7650 && int_fits_type_p
7651 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7652 c_common_signed_type (result_type)))
7653 /* OK */;
7654 else
7655 warning ("comparison between signed and unsigned");
7658 /* Warn if two unsigned values are being compared in a size
7659 larger than their original size, and one (and only one) is the
7660 result of a `~' operator. This comparison will always fail.
7662 Also warn if one operand is a constant, and the constant
7663 does not have all bits set that are set in the ~ operand
7664 when it is extended. */
7666 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7667 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7669 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7670 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7671 &unsignedp0);
7672 else
7673 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7674 &unsignedp1);
7676 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7678 tree primop;
7679 HOST_WIDE_INT constant, mask;
7680 int unsignedp, bits;
7682 if (host_integerp (primop0, 0))
7684 primop = primop1;
7685 unsignedp = unsignedp1;
7686 constant = tree_low_cst (primop0, 0);
7688 else
7690 primop = primop0;
7691 unsignedp = unsignedp0;
7692 constant = tree_low_cst (primop1, 0);
7695 bits = TYPE_PRECISION (TREE_TYPE (primop));
7696 if (bits < TYPE_PRECISION (result_type)
7697 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7699 mask = (~(HOST_WIDE_INT) 0) << bits;
7700 if ((mask & constant) != mask)
7701 warning ("comparison of promoted ~unsigned with constant");
7704 else if (unsignedp0 && unsignedp1
7705 && (TYPE_PRECISION (TREE_TYPE (primop0))
7706 < TYPE_PRECISION (result_type))
7707 && (TYPE_PRECISION (TREE_TYPE (primop1))
7708 < TYPE_PRECISION (result_type)))
7709 warning ("comparison of promoted ~unsigned with unsigned");
7715 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7716 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7717 Then the expression will be built.
7718 It will be given type FINAL_TYPE if that is nonzero;
7719 otherwise, it will be given type RESULT_TYPE. */
7721 if (!result_type)
7723 binary_op_error (code);
7724 return error_mark_node;
7727 if (!converted)
7729 if (TREE_TYPE (op0) != result_type)
7730 op0 = convert (result_type, op0);
7731 if (TREE_TYPE (op1) != result_type)
7732 op1 = convert (result_type, op1);
7734 /* This can happen if one operand has a vector type, and the other
7735 has a different type. */
7736 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7737 return error_mark_node;
7740 if (build_type == NULL_TREE)
7741 build_type = result_type;
7744 tree result = build2 (resultcode, build_type, op0, op1);
7746 /* Treat expressions in initializers specially as they can't trap. */
7747 result = require_constant_value ? fold_initializer (result)
7748 : fold (result);
7750 if (final_type != 0)
7751 result = convert (final_type, result);
7752 return result;