gcc/
[official-gcc.git] / gcc / c-typeck.c
blob54f9714f514a5fc2c3b5ea9391aae306fb08105e
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
4677 constructor_index = bitsize_zero_node;
4679 constructor_unfilled_index = constructor_index;
4681 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4683 /* Vectors are like simple fixed-size arrays. */
4684 constructor_max_index =
4685 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4686 constructor_index = convert (bitsizetype, bitsize_zero_node);
4687 constructor_unfilled_index = constructor_index;
4689 else
4691 /* Handle the case of int x = {5}; */
4692 constructor_fields = constructor_type;
4693 constructor_unfilled_fields = constructor_type;
4697 /* Push down into a subobject, for initialization.
4698 If this is for an explicit set of braces, IMPLICIT is 0.
4699 If it is because the next element belongs at a lower level,
4700 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4702 void
4703 push_init_level (int implicit)
4705 struct constructor_stack *p;
4706 tree value = NULL_TREE;
4708 /* If we've exhausted any levels that didn't have braces,
4709 pop them now. */
4710 while (constructor_stack->implicit)
4712 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4713 || TREE_CODE (constructor_type) == UNION_TYPE)
4714 && constructor_fields == 0)
4715 process_init_element (pop_init_level (1));
4716 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4717 && constructor_max_index
4718 && tree_int_cst_lt (constructor_max_index, constructor_index))
4719 process_init_element (pop_init_level (1));
4720 else
4721 break;
4724 /* Unless this is an explicit brace, we need to preserve previous
4725 content if any. */
4726 if (implicit)
4728 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4729 || TREE_CODE (constructor_type) == UNION_TYPE)
4730 && constructor_fields)
4731 value = find_init_member (constructor_fields);
4732 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4733 value = find_init_member (constructor_index);
4736 p = XNEW (struct constructor_stack);
4737 p->type = constructor_type;
4738 p->fields = constructor_fields;
4739 p->index = constructor_index;
4740 p->max_index = constructor_max_index;
4741 p->unfilled_index = constructor_unfilled_index;
4742 p->unfilled_fields = constructor_unfilled_fields;
4743 p->bit_index = constructor_bit_index;
4744 p->elements = constructor_elements;
4745 p->constant = constructor_constant;
4746 p->simple = constructor_simple;
4747 p->erroneous = constructor_erroneous;
4748 p->pending_elts = constructor_pending_elts;
4749 p->depth = constructor_depth;
4750 p->replacement_value.value = 0;
4751 p->replacement_value.original_code = ERROR_MARK;
4752 p->implicit = implicit;
4753 p->outer = 0;
4754 p->incremental = constructor_incremental;
4755 p->designated = constructor_designated;
4756 p->next = constructor_stack;
4757 p->range_stack = 0;
4758 constructor_stack = p;
4760 constructor_constant = 1;
4761 constructor_simple = 1;
4762 constructor_depth = SPELLING_DEPTH ();
4763 constructor_elements = 0;
4764 constructor_incremental = 1;
4765 constructor_designated = 0;
4766 constructor_pending_elts = 0;
4767 if (!implicit)
4769 p->range_stack = constructor_range_stack;
4770 constructor_range_stack = 0;
4771 designator_depth = 0;
4772 designator_errorneous = 0;
4775 /* Don't die if an entire brace-pair level is superfluous
4776 in the containing level. */
4777 if (constructor_type == 0)
4779 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4780 || TREE_CODE (constructor_type) == UNION_TYPE)
4782 /* Don't die if there are extra init elts at the end. */
4783 if (constructor_fields == 0)
4784 constructor_type = 0;
4785 else
4787 constructor_type = TREE_TYPE (constructor_fields);
4788 push_member_name (constructor_fields);
4789 constructor_depth++;
4792 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4794 constructor_type = TREE_TYPE (constructor_type);
4795 push_array_bounds (tree_low_cst (constructor_index, 0));
4796 constructor_depth++;
4799 if (constructor_type == 0)
4801 error_init ("extra brace group at end of initializer");
4802 constructor_fields = 0;
4803 constructor_unfilled_fields = 0;
4804 return;
4807 if (value && TREE_CODE (value) == CONSTRUCTOR)
4809 constructor_constant = TREE_CONSTANT (value);
4810 constructor_simple = TREE_STATIC (value);
4811 constructor_elements = CONSTRUCTOR_ELTS (value);
4812 if (constructor_elements
4813 && (TREE_CODE (constructor_type) == RECORD_TYPE
4814 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4815 set_nonincremental_init ();
4818 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4820 missing_braces_mentioned = 1;
4821 warning_init ("missing braces around initializer");
4824 if (TREE_CODE (constructor_type) == RECORD_TYPE
4825 || TREE_CODE (constructor_type) == UNION_TYPE)
4827 constructor_fields = TYPE_FIELDS (constructor_type);
4828 /* Skip any nameless bit fields at the beginning. */
4829 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4830 && DECL_NAME (constructor_fields) == 0)
4831 constructor_fields = TREE_CHAIN (constructor_fields);
4833 constructor_unfilled_fields = constructor_fields;
4834 constructor_bit_index = bitsize_zero_node;
4836 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4838 /* Vectors are like simple fixed-size arrays. */
4839 constructor_max_index =
4840 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4841 constructor_index = convert (bitsizetype, integer_zero_node);
4842 constructor_unfilled_index = constructor_index;
4844 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4846 if (TYPE_DOMAIN (constructor_type))
4848 constructor_max_index
4849 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4851 /* Detect non-empty initializations of zero-length arrays. */
4852 if (constructor_max_index == NULL_TREE
4853 && TYPE_SIZE (constructor_type))
4854 constructor_max_index = build_int_cst (NULL_TREE, -1);
4856 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4857 to initialize VLAs will cause a proper error; avoid tree
4858 checking errors as well by setting a safe value. */
4859 if (constructor_max_index
4860 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4861 constructor_max_index = build_int_cst (NULL_TREE, -1);
4863 constructor_index
4864 = convert (bitsizetype,
4865 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4867 else
4868 constructor_index = bitsize_zero_node;
4870 constructor_unfilled_index = constructor_index;
4871 if (value && TREE_CODE (value) == STRING_CST)
4873 /* We need to split the char/wchar array into individual
4874 characters, so that we don't have to special case it
4875 everywhere. */
4876 set_nonincremental_init_from_string (value);
4879 else
4881 if (constructor_type != error_mark_node)
4882 warning_init ("braces around scalar initializer");
4883 constructor_fields = constructor_type;
4884 constructor_unfilled_fields = constructor_type;
4888 /* At the end of an implicit or explicit brace level,
4889 finish up that level of constructor. If a single expression
4890 with redundant braces initialized that level, return the
4891 c_expr structure for that expression. Otherwise, the original_code
4892 element is set to ERROR_MARK.
4893 If we were outputting the elements as they are read, return 0 as the value
4894 from inner levels (process_init_element ignores that),
4895 but return error_mark_node as the value from the outermost level
4896 (that's what we want to put in DECL_INITIAL).
4897 Otherwise, return a CONSTRUCTOR expression as the value. */
4899 struct c_expr
4900 pop_init_level (int implicit)
4902 struct constructor_stack *p;
4903 struct c_expr ret;
4904 ret.value = 0;
4905 ret.original_code = ERROR_MARK;
4907 if (implicit == 0)
4909 /* When we come to an explicit close brace,
4910 pop any inner levels that didn't have explicit braces. */
4911 while (constructor_stack->implicit)
4912 process_init_element (pop_init_level (1));
4914 gcc_assert (!constructor_range_stack);
4917 /* Now output all pending elements. */
4918 constructor_incremental = 1;
4919 output_pending_init_elements (1);
4921 p = constructor_stack;
4923 /* Error for initializing a flexible array member, or a zero-length
4924 array member in an inappropriate context. */
4925 if (constructor_type && constructor_fields
4926 && TREE_CODE (constructor_type) == ARRAY_TYPE
4927 && TYPE_DOMAIN (constructor_type)
4928 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4930 /* Silently discard empty initializations. The parser will
4931 already have pedwarned for empty brackets. */
4932 if (integer_zerop (constructor_unfilled_index))
4933 constructor_type = NULL_TREE;
4934 else
4936 gcc_assert (!TYPE_SIZE (constructor_type));
4938 if (constructor_depth > 2)
4939 error_init ("initialization of flexible array member in a nested context");
4940 else if (pedantic)
4941 pedwarn_init ("initialization of a flexible array member");
4943 /* We have already issued an error message for the existence
4944 of a flexible array member not at the end of the structure.
4945 Discard the initializer so that we do not abort later. */
4946 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4947 constructor_type = NULL_TREE;
4951 /* Warn when some struct elements are implicitly initialized to zero. */
4952 if (warn_missing_field_initializers
4953 && constructor_type
4954 && TREE_CODE (constructor_type) == RECORD_TYPE
4955 && constructor_unfilled_fields)
4957 /* Do not warn for flexible array members or zero-length arrays. */
4958 while (constructor_unfilled_fields
4959 && (!DECL_SIZE (constructor_unfilled_fields)
4960 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4961 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4963 /* Do not warn if this level of the initializer uses member
4964 designators; it is likely to be deliberate. */
4965 if (constructor_unfilled_fields && !constructor_designated)
4967 push_member_name (constructor_unfilled_fields);
4968 warning_init ("missing initializer");
4969 RESTORE_SPELLING_DEPTH (constructor_depth);
4973 /* Pad out the end of the structure. */
4974 if (p->replacement_value.value)
4975 /* If this closes a superfluous brace pair,
4976 just pass out the element between them. */
4977 ret = p->replacement_value;
4978 else if (constructor_type == 0)
4980 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4981 && TREE_CODE (constructor_type) != UNION_TYPE
4982 && TREE_CODE (constructor_type) != ARRAY_TYPE
4983 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4985 /* A nonincremental scalar initializer--just return
4986 the element, after verifying there is just one. */
4987 if (constructor_elements == 0)
4989 if (!constructor_erroneous)
4990 error_init ("empty scalar initializer");
4991 ret.value = error_mark_node;
4993 else if (TREE_CHAIN (constructor_elements) != 0)
4995 error_init ("extra elements in scalar initializer");
4996 ret.value = TREE_VALUE (constructor_elements);
4998 else
4999 ret.value = TREE_VALUE (constructor_elements);
5001 else
5003 if (constructor_erroneous)
5004 ret.value = error_mark_node;
5005 else
5007 ret.value = build_constructor (constructor_type,
5008 nreverse (constructor_elements));
5009 if (constructor_constant)
5010 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5011 if (constructor_constant && constructor_simple)
5012 TREE_STATIC (ret.value) = 1;
5016 constructor_type = p->type;
5017 constructor_fields = p->fields;
5018 constructor_index = p->index;
5019 constructor_max_index = p->max_index;
5020 constructor_unfilled_index = p->unfilled_index;
5021 constructor_unfilled_fields = p->unfilled_fields;
5022 constructor_bit_index = p->bit_index;
5023 constructor_elements = p->elements;
5024 constructor_constant = p->constant;
5025 constructor_simple = p->simple;
5026 constructor_erroneous = p->erroneous;
5027 constructor_incremental = p->incremental;
5028 constructor_designated = p->designated;
5029 constructor_pending_elts = p->pending_elts;
5030 constructor_depth = p->depth;
5031 if (!p->implicit)
5032 constructor_range_stack = p->range_stack;
5033 RESTORE_SPELLING_DEPTH (constructor_depth);
5035 constructor_stack = p->next;
5036 free (p);
5038 if (ret.value == 0)
5040 if (constructor_stack == 0)
5042 ret.value = error_mark_node;
5043 return ret;
5045 return ret;
5047 return ret;
5050 /* Common handling for both array range and field name designators.
5051 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5053 static int
5054 set_designator (int array)
5056 tree subtype;
5057 enum tree_code subcode;
5059 /* Don't die if an entire brace-pair level is superfluous
5060 in the containing level. */
5061 if (constructor_type == 0)
5062 return 1;
5064 /* If there were errors in this designator list already, bail out
5065 silently. */
5066 if (designator_errorneous)
5067 return 1;
5069 if (!designator_depth)
5071 gcc_assert (!constructor_range_stack);
5073 /* Designator list starts at the level of closest explicit
5074 braces. */
5075 while (constructor_stack->implicit)
5076 process_init_element (pop_init_level (1));
5077 constructor_designated = 1;
5078 return 0;
5081 switch (TREE_CODE (constructor_type))
5083 case RECORD_TYPE:
5084 case UNION_TYPE:
5085 subtype = TREE_TYPE (constructor_fields);
5086 if (subtype != error_mark_node)
5087 subtype = TYPE_MAIN_VARIANT (subtype);
5088 break;
5089 case ARRAY_TYPE:
5090 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5091 break;
5092 default:
5093 gcc_unreachable ();
5096 subcode = TREE_CODE (subtype);
5097 if (array && subcode != ARRAY_TYPE)
5099 error_init ("array index in non-array initializer");
5100 return 1;
5102 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5104 error_init ("field name not in record or union initializer");
5105 return 1;
5108 constructor_designated = 1;
5109 push_init_level (2);
5110 return 0;
5113 /* If there are range designators in designator list, push a new designator
5114 to constructor_range_stack. RANGE_END is end of such stack range or
5115 NULL_TREE if there is no range designator at this level. */
5117 static void
5118 push_range_stack (tree range_end)
5120 struct constructor_range_stack *p;
5122 p = GGC_NEW (struct constructor_range_stack);
5123 p->prev = constructor_range_stack;
5124 p->next = 0;
5125 p->fields = constructor_fields;
5126 p->range_start = constructor_index;
5127 p->index = constructor_index;
5128 p->stack = constructor_stack;
5129 p->range_end = range_end;
5130 if (constructor_range_stack)
5131 constructor_range_stack->next = p;
5132 constructor_range_stack = p;
5135 /* Within an array initializer, specify the next index to be initialized.
5136 FIRST is that index. If LAST is nonzero, then initialize a range
5137 of indices, running from FIRST through LAST. */
5139 void
5140 set_init_index (tree first, tree last)
5142 if (set_designator (1))
5143 return;
5145 designator_errorneous = 1;
5147 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5148 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5150 error_init ("array index in initializer not of integer type");
5151 return;
5154 while ((TREE_CODE (first) == NOP_EXPR
5155 || TREE_CODE (first) == CONVERT_EXPR
5156 || TREE_CODE (first) == NON_LVALUE_EXPR)
5157 && (TYPE_MODE (TREE_TYPE (first))
5158 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5159 first = TREE_OPERAND (first, 0);
5161 if (last)
5162 while ((TREE_CODE (last) == NOP_EXPR
5163 || TREE_CODE (last) == CONVERT_EXPR
5164 || TREE_CODE (last) == NON_LVALUE_EXPR)
5165 && (TYPE_MODE (TREE_TYPE (last))
5166 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5167 last = TREE_OPERAND (last, 0);
5169 if (TREE_CODE (first) != INTEGER_CST)
5170 error_init ("nonconstant array index in initializer");
5171 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5172 error_init ("nonconstant array index in initializer");
5173 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5174 error_init ("array index in non-array initializer");
5175 else if (tree_int_cst_sgn (first) == -1)
5176 error_init ("array index in initializer exceeds array bounds");
5177 else if (constructor_max_index
5178 && tree_int_cst_lt (constructor_max_index, first))
5179 error_init ("array index in initializer exceeds array bounds");
5180 else
5182 constructor_index = convert (bitsizetype, first);
5184 if (last)
5186 if (tree_int_cst_equal (first, last))
5187 last = 0;
5188 else if (tree_int_cst_lt (last, first))
5190 error_init ("empty index range in initializer");
5191 last = 0;
5193 else
5195 last = convert (bitsizetype, last);
5196 if (constructor_max_index != 0
5197 && tree_int_cst_lt (constructor_max_index, last))
5199 error_init ("array index range in initializer exceeds array bounds");
5200 last = 0;
5205 designator_depth++;
5206 designator_errorneous = 0;
5207 if (constructor_range_stack || last)
5208 push_range_stack (last);
5212 /* Within a struct initializer, specify the next field to be initialized. */
5214 void
5215 set_init_label (tree fieldname)
5217 tree tail;
5219 if (set_designator (0))
5220 return;
5222 designator_errorneous = 1;
5224 if (TREE_CODE (constructor_type) != RECORD_TYPE
5225 && TREE_CODE (constructor_type) != UNION_TYPE)
5227 error_init ("field name not in record or union initializer");
5228 return;
5231 for (tail = TYPE_FIELDS (constructor_type); tail;
5232 tail = TREE_CHAIN (tail))
5234 if (DECL_NAME (tail) == fieldname)
5235 break;
5238 if (tail == 0)
5239 error ("unknown field %qs specified in initializer",
5240 IDENTIFIER_POINTER (fieldname));
5241 else
5243 constructor_fields = tail;
5244 designator_depth++;
5245 designator_errorneous = 0;
5246 if (constructor_range_stack)
5247 push_range_stack (NULL_TREE);
5251 /* Add a new initializer to the tree of pending initializers. PURPOSE
5252 identifies the initializer, either array index or field in a structure.
5253 VALUE is the value of that index or field. */
5255 static void
5256 add_pending_init (tree purpose, tree value)
5258 struct init_node *p, **q, *r;
5260 q = &constructor_pending_elts;
5261 p = 0;
5263 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5265 while (*q != 0)
5267 p = *q;
5268 if (tree_int_cst_lt (purpose, p->purpose))
5269 q = &p->left;
5270 else if (tree_int_cst_lt (p->purpose, purpose))
5271 q = &p->right;
5272 else
5274 if (TREE_SIDE_EFFECTS (p->value))
5275 warning_init ("initialized field with side-effects overwritten");
5276 p->value = value;
5277 return;
5281 else
5283 tree bitpos;
5285 bitpos = bit_position (purpose);
5286 while (*q != NULL)
5288 p = *q;
5289 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5290 q = &p->left;
5291 else if (p->purpose != purpose)
5292 q = &p->right;
5293 else
5295 if (TREE_SIDE_EFFECTS (p->value))
5296 warning_init ("initialized field with side-effects overwritten");
5297 p->value = value;
5298 return;
5303 r = GGC_NEW (struct init_node);
5304 r->purpose = purpose;
5305 r->value = value;
5307 *q = r;
5308 r->parent = p;
5309 r->left = 0;
5310 r->right = 0;
5311 r->balance = 0;
5313 while (p)
5315 struct init_node *s;
5317 if (r == p->left)
5319 if (p->balance == 0)
5320 p->balance = -1;
5321 else if (p->balance < 0)
5323 if (r->balance < 0)
5325 /* L rotation. */
5326 p->left = r->right;
5327 if (p->left)
5328 p->left->parent = p;
5329 r->right = p;
5331 p->balance = 0;
5332 r->balance = 0;
5334 s = p->parent;
5335 p->parent = r;
5336 r->parent = s;
5337 if (s)
5339 if (s->left == p)
5340 s->left = r;
5341 else
5342 s->right = r;
5344 else
5345 constructor_pending_elts = r;
5347 else
5349 /* LR rotation. */
5350 struct init_node *t = r->right;
5352 r->right = t->left;
5353 if (r->right)
5354 r->right->parent = r;
5355 t->left = r;
5357 p->left = t->right;
5358 if (p->left)
5359 p->left->parent = p;
5360 t->right = p;
5362 p->balance = t->balance < 0;
5363 r->balance = -(t->balance > 0);
5364 t->balance = 0;
5366 s = p->parent;
5367 p->parent = t;
5368 r->parent = t;
5369 t->parent = s;
5370 if (s)
5372 if (s->left == p)
5373 s->left = t;
5374 else
5375 s->right = t;
5377 else
5378 constructor_pending_elts = t;
5380 break;
5382 else
5384 /* p->balance == +1; growth of left side balances the node. */
5385 p->balance = 0;
5386 break;
5389 else /* r == p->right */
5391 if (p->balance == 0)
5392 /* Growth propagation from right side. */
5393 p->balance++;
5394 else if (p->balance > 0)
5396 if (r->balance > 0)
5398 /* R rotation. */
5399 p->right = r->left;
5400 if (p->right)
5401 p->right->parent = p;
5402 r->left = p;
5404 p->balance = 0;
5405 r->balance = 0;
5407 s = p->parent;
5408 p->parent = r;
5409 r->parent = s;
5410 if (s)
5412 if (s->left == p)
5413 s->left = r;
5414 else
5415 s->right = r;
5417 else
5418 constructor_pending_elts = r;
5420 else /* r->balance == -1 */
5422 /* RL rotation */
5423 struct init_node *t = r->left;
5425 r->left = t->right;
5426 if (r->left)
5427 r->left->parent = r;
5428 t->right = r;
5430 p->right = t->left;
5431 if (p->right)
5432 p->right->parent = p;
5433 t->left = p;
5435 r->balance = (t->balance < 0);
5436 p->balance = -(t->balance > 0);
5437 t->balance = 0;
5439 s = p->parent;
5440 p->parent = t;
5441 r->parent = t;
5442 t->parent = s;
5443 if (s)
5445 if (s->left == p)
5446 s->left = t;
5447 else
5448 s->right = t;
5450 else
5451 constructor_pending_elts = t;
5453 break;
5455 else
5457 /* p->balance == -1; growth of right side balances the node. */
5458 p->balance = 0;
5459 break;
5463 r = p;
5464 p = p->parent;
5468 /* Build AVL tree from a sorted chain. */
5470 static void
5471 set_nonincremental_init (void)
5473 tree chain;
5475 if (TREE_CODE (constructor_type) != RECORD_TYPE
5476 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5477 return;
5479 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5480 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5481 constructor_elements = 0;
5482 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5484 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5485 /* Skip any nameless bit fields at the beginning. */
5486 while (constructor_unfilled_fields != 0
5487 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5488 && DECL_NAME (constructor_unfilled_fields) == 0)
5489 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5492 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5494 if (TYPE_DOMAIN (constructor_type))
5495 constructor_unfilled_index
5496 = convert (bitsizetype,
5497 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5498 else
5499 constructor_unfilled_index = bitsize_zero_node;
5501 constructor_incremental = 0;
5504 /* Build AVL tree from a string constant. */
5506 static void
5507 set_nonincremental_init_from_string (tree str)
5509 tree value, purpose, type;
5510 HOST_WIDE_INT val[2];
5511 const char *p, *end;
5512 int byte, wchar_bytes, charwidth, bitpos;
5514 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5516 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5517 == TYPE_PRECISION (char_type_node))
5518 wchar_bytes = 1;
5519 else
5521 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5522 == TYPE_PRECISION (wchar_type_node));
5523 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5525 charwidth = TYPE_PRECISION (char_type_node);
5526 type = TREE_TYPE (constructor_type);
5527 p = TREE_STRING_POINTER (str);
5528 end = p + TREE_STRING_LENGTH (str);
5530 for (purpose = bitsize_zero_node;
5531 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5532 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5534 if (wchar_bytes == 1)
5536 val[1] = (unsigned char) *p++;
5537 val[0] = 0;
5539 else
5541 val[0] = 0;
5542 val[1] = 0;
5543 for (byte = 0; byte < wchar_bytes; byte++)
5545 if (BYTES_BIG_ENDIAN)
5546 bitpos = (wchar_bytes - byte - 1) * charwidth;
5547 else
5548 bitpos = byte * charwidth;
5549 val[bitpos < HOST_BITS_PER_WIDE_INT]
5550 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5551 << (bitpos % HOST_BITS_PER_WIDE_INT);
5555 if (!TYPE_UNSIGNED (type))
5557 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5558 if (bitpos < HOST_BITS_PER_WIDE_INT)
5560 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5562 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5563 val[0] = -1;
5566 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5568 if (val[1] < 0)
5569 val[0] = -1;
5571 else if (val[0] & (((HOST_WIDE_INT) 1)
5572 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5573 val[0] |= ((HOST_WIDE_INT) -1)
5574 << (bitpos - HOST_BITS_PER_WIDE_INT);
5577 value = build_int_cst_wide (type, val[1], val[0]);
5578 add_pending_init (purpose, value);
5581 constructor_incremental = 0;
5584 /* Return value of FIELD in pending initializer or zero if the field was
5585 not initialized yet. */
5587 static tree
5588 find_init_member (tree field)
5590 struct init_node *p;
5592 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5594 if (constructor_incremental
5595 && tree_int_cst_lt (field, constructor_unfilled_index))
5596 set_nonincremental_init ();
5598 p = constructor_pending_elts;
5599 while (p)
5601 if (tree_int_cst_lt (field, p->purpose))
5602 p = p->left;
5603 else if (tree_int_cst_lt (p->purpose, field))
5604 p = p->right;
5605 else
5606 return p->value;
5609 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5611 tree bitpos = bit_position (field);
5613 if (constructor_incremental
5614 && (!constructor_unfilled_fields
5615 || tree_int_cst_lt (bitpos,
5616 bit_position (constructor_unfilled_fields))))
5617 set_nonincremental_init ();
5619 p = constructor_pending_elts;
5620 while (p)
5622 if (field == p->purpose)
5623 return p->value;
5624 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5625 p = p->left;
5626 else
5627 p = p->right;
5630 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5632 if (constructor_elements
5633 && TREE_PURPOSE (constructor_elements) == field)
5634 return TREE_VALUE (constructor_elements);
5636 return 0;
5639 /* "Output" the next constructor element.
5640 At top level, really output it to assembler code now.
5641 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5642 TYPE is the data type that the containing data type wants here.
5643 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5644 If VALUE is a string constant, STRICT_STRING is true if it is
5645 unparenthesized or we should not warn here for it being parenthesized.
5646 For other types of VALUE, STRICT_STRING is not used.
5648 PENDING if non-nil means output pending elements that belong
5649 right after this element. (PENDING is normally 1;
5650 it is 0 while outputting pending elements, to avoid recursion.) */
5652 static void
5653 output_init_element (tree value, bool strict_string, tree type, tree field,
5654 int pending)
5656 if (type == error_mark_node || value == error_mark_node)
5658 constructor_erroneous = 1;
5659 return;
5661 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5662 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5663 && !(TREE_CODE (value) == STRING_CST
5664 && TREE_CODE (type) == ARRAY_TYPE
5665 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5666 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5667 TYPE_MAIN_VARIANT (type))))
5668 value = default_conversion (value);
5670 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5671 && require_constant_value && !flag_isoc99 && pending)
5673 /* As an extension, allow initializing objects with static storage
5674 duration with compound literals (which are then treated just as
5675 the brace enclosed list they contain). */
5676 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5677 value = DECL_INITIAL (decl);
5680 if (value == error_mark_node)
5681 constructor_erroneous = 1;
5682 else if (!TREE_CONSTANT (value))
5683 constructor_constant = 0;
5684 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5685 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5686 || TREE_CODE (constructor_type) == UNION_TYPE)
5687 && DECL_C_BIT_FIELD (field)
5688 && TREE_CODE (value) != INTEGER_CST))
5689 constructor_simple = 0;
5691 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5693 if (require_constant_value)
5695 error_init ("initializer element is not constant");
5696 value = error_mark_node;
5698 else if (require_constant_elements)
5699 pedwarn ("initializer element is not computable at load time");
5702 /* If this field is empty (and not at the end of structure),
5703 don't do anything other than checking the initializer. */
5704 if (field
5705 && (TREE_TYPE (field) == error_mark_node
5706 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5707 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5708 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5709 || TREE_CHAIN (field)))))
5710 return;
5712 value = digest_init (type, value, strict_string, require_constant_value);
5713 if (value == error_mark_node)
5715 constructor_erroneous = 1;
5716 return;
5719 /* If this element doesn't come next in sequence,
5720 put it on constructor_pending_elts. */
5721 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5722 && (!constructor_incremental
5723 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5725 if (constructor_incremental
5726 && tree_int_cst_lt (field, constructor_unfilled_index))
5727 set_nonincremental_init ();
5729 add_pending_init (field, value);
5730 return;
5732 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5733 && (!constructor_incremental
5734 || field != constructor_unfilled_fields))
5736 /* We do this for records but not for unions. In a union,
5737 no matter which field is specified, it can be initialized
5738 right away since it starts at the beginning of the union. */
5739 if (constructor_incremental)
5741 if (!constructor_unfilled_fields)
5742 set_nonincremental_init ();
5743 else
5745 tree bitpos, unfillpos;
5747 bitpos = bit_position (field);
5748 unfillpos = bit_position (constructor_unfilled_fields);
5750 if (tree_int_cst_lt (bitpos, unfillpos))
5751 set_nonincremental_init ();
5755 add_pending_init (field, value);
5756 return;
5758 else if (TREE_CODE (constructor_type) == UNION_TYPE
5759 && constructor_elements)
5761 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5762 warning_init ("initialized field with side-effects overwritten");
5764 /* We can have just one union field set. */
5765 constructor_elements = 0;
5768 /* Otherwise, output this element either to
5769 constructor_elements or to the assembler file. */
5771 if (field && TREE_CODE (field) == INTEGER_CST)
5772 field = copy_node (field);
5773 constructor_elements
5774 = tree_cons (field, value, constructor_elements);
5776 /* Advance the variable that indicates sequential elements output. */
5777 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5778 constructor_unfilled_index
5779 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5780 bitsize_one_node);
5781 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5783 constructor_unfilled_fields
5784 = TREE_CHAIN (constructor_unfilled_fields);
5786 /* Skip any nameless bit fields. */
5787 while (constructor_unfilled_fields != 0
5788 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5789 && DECL_NAME (constructor_unfilled_fields) == 0)
5790 constructor_unfilled_fields =
5791 TREE_CHAIN (constructor_unfilled_fields);
5793 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5794 constructor_unfilled_fields = 0;
5796 /* Now output any pending elements which have become next. */
5797 if (pending)
5798 output_pending_init_elements (0);
5801 /* Output any pending elements which have become next.
5802 As we output elements, constructor_unfilled_{fields,index}
5803 advances, which may cause other elements to become next;
5804 if so, they too are output.
5806 If ALL is 0, we return when there are
5807 no more pending elements to output now.
5809 If ALL is 1, we output space as necessary so that
5810 we can output all the pending elements. */
5812 static void
5813 output_pending_init_elements (int all)
5815 struct init_node *elt = constructor_pending_elts;
5816 tree next;
5818 retry:
5820 /* Look through the whole pending tree.
5821 If we find an element that should be output now,
5822 output it. Otherwise, set NEXT to the element
5823 that comes first among those still pending. */
5825 next = 0;
5826 while (elt)
5828 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5830 if (tree_int_cst_equal (elt->purpose,
5831 constructor_unfilled_index))
5832 output_init_element (elt->value, true,
5833 TREE_TYPE (constructor_type),
5834 constructor_unfilled_index, 0);
5835 else if (tree_int_cst_lt (constructor_unfilled_index,
5836 elt->purpose))
5838 /* Advance to the next smaller node. */
5839 if (elt->left)
5840 elt = elt->left;
5841 else
5843 /* We have reached the smallest node bigger than the
5844 current unfilled index. Fill the space first. */
5845 next = elt->purpose;
5846 break;
5849 else
5851 /* Advance to the next bigger node. */
5852 if (elt->right)
5853 elt = elt->right;
5854 else
5856 /* We have reached the biggest node in a subtree. Find
5857 the parent of it, which is the next bigger node. */
5858 while (elt->parent && elt->parent->right == elt)
5859 elt = elt->parent;
5860 elt = elt->parent;
5861 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5862 elt->purpose))
5864 next = elt->purpose;
5865 break;
5870 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5871 || TREE_CODE (constructor_type) == UNION_TYPE)
5873 tree ctor_unfilled_bitpos, elt_bitpos;
5875 /* If the current record is complete we are done. */
5876 if (constructor_unfilled_fields == 0)
5877 break;
5879 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5880 elt_bitpos = bit_position (elt->purpose);
5881 /* We can't compare fields here because there might be empty
5882 fields in between. */
5883 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5885 constructor_unfilled_fields = elt->purpose;
5886 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5887 elt->purpose, 0);
5889 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5891 /* Advance to the next smaller node. */
5892 if (elt->left)
5893 elt = elt->left;
5894 else
5896 /* We have reached the smallest node bigger than the
5897 current unfilled field. Fill the space first. */
5898 next = elt->purpose;
5899 break;
5902 else
5904 /* Advance to the next bigger node. */
5905 if (elt->right)
5906 elt = elt->right;
5907 else
5909 /* We have reached the biggest node in a subtree. Find
5910 the parent of it, which is the next bigger node. */
5911 while (elt->parent && elt->parent->right == elt)
5912 elt = elt->parent;
5913 elt = elt->parent;
5914 if (elt
5915 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5916 bit_position (elt->purpose))))
5918 next = elt->purpose;
5919 break;
5926 /* Ordinarily return, but not if we want to output all
5927 and there are elements left. */
5928 if (!(all && next != 0))
5929 return;
5931 /* If it's not incremental, just skip over the gap, so that after
5932 jumping to retry we will output the next successive element. */
5933 if (TREE_CODE (constructor_type) == RECORD_TYPE
5934 || TREE_CODE (constructor_type) == UNION_TYPE)
5935 constructor_unfilled_fields = next;
5936 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5937 constructor_unfilled_index = next;
5939 /* ELT now points to the node in the pending tree with the next
5940 initializer to output. */
5941 goto retry;
5944 /* Add one non-braced element to the current constructor level.
5945 This adjusts the current position within the constructor's type.
5946 This may also start or terminate implicit levels
5947 to handle a partly-braced initializer.
5949 Once this has found the correct level for the new element,
5950 it calls output_init_element. */
5952 void
5953 process_init_element (struct c_expr value)
5955 tree orig_value = value.value;
5956 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5957 bool strict_string = value.original_code == STRING_CST;
5959 designator_depth = 0;
5960 designator_errorneous = 0;
5962 /* Handle superfluous braces around string cst as in
5963 char x[] = {"foo"}; */
5964 if (string_flag
5965 && constructor_type
5966 && TREE_CODE (constructor_type) == ARRAY_TYPE
5967 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5968 && integer_zerop (constructor_unfilled_index))
5970 if (constructor_stack->replacement_value.value)
5971 error_init ("excess elements in char array initializer");
5972 constructor_stack->replacement_value = value;
5973 return;
5976 if (constructor_stack->replacement_value.value != 0)
5978 error_init ("excess elements in struct initializer");
5979 return;
5982 /* Ignore elements of a brace group if it is entirely superfluous
5983 and has already been diagnosed. */
5984 if (constructor_type == 0)
5985 return;
5987 /* If we've exhausted any levels that didn't have braces,
5988 pop them now. */
5989 while (constructor_stack->implicit)
5991 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5992 || TREE_CODE (constructor_type) == UNION_TYPE)
5993 && constructor_fields == 0)
5994 process_init_element (pop_init_level (1));
5995 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5996 && (constructor_max_index == 0
5997 || tree_int_cst_lt (constructor_max_index,
5998 constructor_index)))
5999 process_init_element (pop_init_level (1));
6000 else
6001 break;
6004 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6005 if (constructor_range_stack)
6007 /* If value is a compound literal and we'll be just using its
6008 content, don't put it into a SAVE_EXPR. */
6009 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6010 || !require_constant_value
6011 || flag_isoc99)
6012 value.value = save_expr (value.value);
6015 while (1)
6017 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6019 tree fieldtype;
6020 enum tree_code fieldcode;
6022 if (constructor_fields == 0)
6024 pedwarn_init ("excess elements in struct initializer");
6025 break;
6028 fieldtype = TREE_TYPE (constructor_fields);
6029 if (fieldtype != error_mark_node)
6030 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6031 fieldcode = TREE_CODE (fieldtype);
6033 /* Error for non-static initialization of a flexible array member. */
6034 if (fieldcode == ARRAY_TYPE
6035 && !require_constant_value
6036 && TYPE_SIZE (fieldtype) == NULL_TREE
6037 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6039 error_init ("non-static initialization of a flexible array member");
6040 break;
6043 /* Accept a string constant to initialize a subarray. */
6044 if (value.value != 0
6045 && fieldcode == ARRAY_TYPE
6046 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6047 && string_flag)
6048 value.value = orig_value;
6049 /* Otherwise, if we have come to a subaggregate,
6050 and we don't have an element of its type, push into it. */
6051 else if (value.value != 0
6052 && value.value != error_mark_node
6053 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6054 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6055 || fieldcode == UNION_TYPE))
6057 push_init_level (1);
6058 continue;
6061 if (value.value)
6063 push_member_name (constructor_fields);
6064 output_init_element (value.value, strict_string,
6065 fieldtype, constructor_fields, 1);
6066 RESTORE_SPELLING_DEPTH (constructor_depth);
6068 else
6069 /* Do the bookkeeping for an element that was
6070 directly output as a constructor. */
6072 /* For a record, keep track of end position of last field. */
6073 if (DECL_SIZE (constructor_fields))
6074 constructor_bit_index
6075 = size_binop (PLUS_EXPR,
6076 bit_position (constructor_fields),
6077 DECL_SIZE (constructor_fields));
6079 /* If the current field was the first one not yet written out,
6080 it isn't now, so update. */
6081 if (constructor_unfilled_fields == constructor_fields)
6083 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6084 /* Skip any nameless bit fields. */
6085 while (constructor_unfilled_fields != 0
6086 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6087 && DECL_NAME (constructor_unfilled_fields) == 0)
6088 constructor_unfilled_fields =
6089 TREE_CHAIN (constructor_unfilled_fields);
6093 constructor_fields = TREE_CHAIN (constructor_fields);
6094 /* Skip any nameless bit fields at the beginning. */
6095 while (constructor_fields != 0
6096 && DECL_C_BIT_FIELD (constructor_fields)
6097 && DECL_NAME (constructor_fields) == 0)
6098 constructor_fields = TREE_CHAIN (constructor_fields);
6100 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6102 tree fieldtype;
6103 enum tree_code fieldcode;
6105 if (constructor_fields == 0)
6107 pedwarn_init ("excess elements in union initializer");
6108 break;
6111 fieldtype = TREE_TYPE (constructor_fields);
6112 if (fieldtype != error_mark_node)
6113 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6114 fieldcode = TREE_CODE (fieldtype);
6116 /* Warn that traditional C rejects initialization of unions.
6117 We skip the warning if the value is zero. This is done
6118 under the assumption that the zero initializer in user
6119 code appears conditioned on e.g. __STDC__ to avoid
6120 "missing initializer" warnings and relies on default
6121 initialization to zero in the traditional C case.
6122 We also skip the warning if the initializer is designated,
6123 again on the assumption that this must be conditional on
6124 __STDC__ anyway (and we've already complained about the
6125 member-designator already). */
6126 if (warn_traditional && !in_system_header && !constructor_designated
6127 && !(value.value && (integer_zerop (value.value)
6128 || real_zerop (value.value))))
6129 warning ("traditional C rejects initialization of unions");
6131 /* Accept a string constant to initialize a subarray. */
6132 if (value.value != 0
6133 && fieldcode == ARRAY_TYPE
6134 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6135 && string_flag)
6136 value.value = orig_value;
6137 /* Otherwise, if we have come to a subaggregate,
6138 and we don't have an element of its type, push into it. */
6139 else if (value.value != 0
6140 && value.value != error_mark_node
6141 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6142 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6143 || fieldcode == UNION_TYPE))
6145 push_init_level (1);
6146 continue;
6149 if (value.value)
6151 push_member_name (constructor_fields);
6152 output_init_element (value.value, strict_string,
6153 fieldtype, constructor_fields, 1);
6154 RESTORE_SPELLING_DEPTH (constructor_depth);
6156 else
6157 /* Do the bookkeeping for an element that was
6158 directly output as a constructor. */
6160 constructor_bit_index = DECL_SIZE (constructor_fields);
6161 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6164 constructor_fields = 0;
6166 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6168 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6169 enum tree_code eltcode = TREE_CODE (elttype);
6171 /* Accept a string constant to initialize a subarray. */
6172 if (value.value != 0
6173 && eltcode == ARRAY_TYPE
6174 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6175 && string_flag)
6176 value.value = orig_value;
6177 /* Otherwise, if we have come to a subaggregate,
6178 and we don't have an element of its type, push into it. */
6179 else if (value.value != 0
6180 && value.value != error_mark_node
6181 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6182 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6183 || eltcode == UNION_TYPE))
6185 push_init_level (1);
6186 continue;
6189 if (constructor_max_index != 0
6190 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6191 || integer_all_onesp (constructor_max_index)))
6193 pedwarn_init ("excess elements in array initializer");
6194 break;
6197 /* Now output the actual element. */
6198 if (value.value)
6200 push_array_bounds (tree_low_cst (constructor_index, 0));
6201 output_init_element (value.value, strict_string,
6202 elttype, constructor_index, 1);
6203 RESTORE_SPELLING_DEPTH (constructor_depth);
6206 constructor_index
6207 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6209 if (!value.value)
6210 /* If we are doing the bookkeeping for an element that was
6211 directly output as a constructor, we must update
6212 constructor_unfilled_index. */
6213 constructor_unfilled_index = constructor_index;
6215 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6217 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6219 /* Do a basic check of initializer size. Note that vectors
6220 always have a fixed size derived from their type. */
6221 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6223 pedwarn_init ("excess elements in vector initializer");
6224 break;
6227 /* Now output the actual element. */
6228 if (value.value)
6229 output_init_element (value.value, strict_string,
6230 elttype, constructor_index, 1);
6232 constructor_index
6233 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6235 if (!value.value)
6236 /* If we are doing the bookkeeping for an element that was
6237 directly output as a constructor, we must update
6238 constructor_unfilled_index. */
6239 constructor_unfilled_index = constructor_index;
6242 /* Handle the sole element allowed in a braced initializer
6243 for a scalar variable. */
6244 else if (constructor_type != error_mark_node
6245 && constructor_fields == 0)
6247 pedwarn_init ("excess elements in scalar initializer");
6248 break;
6250 else
6252 if (value.value)
6253 output_init_element (value.value, strict_string,
6254 constructor_type, NULL_TREE, 1);
6255 constructor_fields = 0;
6258 /* Handle range initializers either at this level or anywhere higher
6259 in the designator stack. */
6260 if (constructor_range_stack)
6262 struct constructor_range_stack *p, *range_stack;
6263 int finish = 0;
6265 range_stack = constructor_range_stack;
6266 constructor_range_stack = 0;
6267 while (constructor_stack != range_stack->stack)
6269 gcc_assert (constructor_stack->implicit);
6270 process_init_element (pop_init_level (1));
6272 for (p = range_stack;
6273 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6274 p = p->prev)
6276 gcc_assert (constructor_stack->implicit);
6277 process_init_element (pop_init_level (1));
6280 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6281 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6282 finish = 1;
6284 while (1)
6286 constructor_index = p->index;
6287 constructor_fields = p->fields;
6288 if (finish && p->range_end && p->index == p->range_start)
6290 finish = 0;
6291 p->prev = 0;
6293 p = p->next;
6294 if (!p)
6295 break;
6296 push_init_level (2);
6297 p->stack = constructor_stack;
6298 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6299 p->index = p->range_start;
6302 if (!finish)
6303 constructor_range_stack = range_stack;
6304 continue;
6307 break;
6310 constructor_range_stack = 0;
6313 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6314 (guaranteed to be 'volatile' or null) and ARGS (represented using
6315 an ASM_EXPR node). */
6316 tree
6317 build_asm_stmt (tree cv_qualifier, tree args)
6319 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6320 ASM_VOLATILE_P (args) = 1;
6321 return add_stmt (args);
6324 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6325 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6326 SIMPLE indicates whether there was anything at all after the
6327 string in the asm expression -- asm("blah") and asm("blah" : )
6328 are subtly different. We use a ASM_EXPR node to represent this. */
6329 tree
6330 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6331 bool simple)
6333 tree tail;
6334 tree args;
6335 int i;
6336 const char *constraint;
6337 const char **oconstraints;
6338 bool allows_mem, allows_reg, is_inout;
6339 int ninputs, noutputs;
6341 ninputs = list_length (inputs);
6342 noutputs = list_length (outputs);
6343 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6345 string = resolve_asm_operand_names (string, outputs, inputs);
6347 /* Remove output conversions that change the type but not the mode. */
6348 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6350 tree output = TREE_VALUE (tail);
6352 /* ??? Really, this should not be here. Users should be using a
6353 proper lvalue, dammit. But there's a long history of using casts
6354 in the output operands. In cases like longlong.h, this becomes a
6355 primitive form of typechecking -- if the cast can be removed, then
6356 the output operand had a type of the proper width; otherwise we'll
6357 get an error. Gross, but ... */
6358 STRIP_NOPS (output);
6360 if (!lvalue_or_else (output, lv_asm))
6361 output = error_mark_node;
6363 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6364 oconstraints[i] = constraint;
6366 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6367 &allows_mem, &allows_reg, &is_inout))
6369 /* If the operand is going to end up in memory,
6370 mark it addressable. */
6371 if (!allows_reg && !c_mark_addressable (output))
6372 output = error_mark_node;
6374 else
6375 output = error_mark_node;
6377 TREE_VALUE (tail) = output;
6380 /* Perform default conversions on array and function inputs.
6381 Don't do this for other types as it would screw up operands
6382 expected to be in memory. */
6383 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6385 tree input;
6387 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6388 input = TREE_VALUE (tail);
6390 input = default_function_array_conversion (input);
6392 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6393 oconstraints, &allows_mem, &allows_reg))
6395 /* If the operand is going to end up in memory,
6396 mark it addressable. */
6397 if (!allows_reg && allows_mem)
6399 /* Strip the nops as we allow this case. FIXME, this really
6400 should be rejected or made deprecated. */
6401 STRIP_NOPS (input);
6402 if (!c_mark_addressable (input))
6403 input = error_mark_node;
6406 else
6407 input = error_mark_node;
6409 TREE_VALUE (tail) = input;
6412 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6414 /* Simple asm statements are treated as volatile. */
6415 if (simple)
6417 ASM_VOLATILE_P (args) = 1;
6418 ASM_INPUT_P (args) = 1;
6421 return args;
6424 /* Generate a goto statement to LABEL. */
6426 tree
6427 c_finish_goto_label (tree label)
6429 tree decl = lookup_label (label);
6430 if (!decl)
6431 return NULL_TREE;
6433 TREE_USED (decl) = 1;
6434 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6437 /* Generate a computed goto statement to EXPR. */
6439 tree
6440 c_finish_goto_ptr (tree expr)
6442 if (pedantic)
6443 pedwarn ("ISO C forbids %<goto *expr;%>");
6444 expr = convert (ptr_type_node, expr);
6445 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6448 /* Generate a C `return' statement. RETVAL is the expression for what
6449 to return, or a null pointer for `return;' with no value. */
6451 tree
6452 c_finish_return (tree retval)
6454 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6456 if (TREE_THIS_VOLATILE (current_function_decl))
6457 warning ("function declared %<noreturn%> has a %<return%> statement");
6459 if (!retval)
6461 current_function_returns_null = 1;
6462 if ((warn_return_type || flag_isoc99)
6463 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6464 pedwarn_c99 ("%<return%> with no value, in "
6465 "function returning non-void");
6467 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6469 current_function_returns_null = 1;
6470 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6471 pedwarn ("%<return%> with a value, in function returning void");
6473 else
6475 tree t = convert_for_assignment (valtype, retval, ic_return,
6476 NULL_TREE, NULL_TREE, 0);
6477 tree res = DECL_RESULT (current_function_decl);
6478 tree inner;
6480 current_function_returns_value = 1;
6481 if (t == error_mark_node)
6482 return NULL_TREE;
6484 inner = t = convert (TREE_TYPE (res), t);
6486 /* Strip any conversions, additions, and subtractions, and see if
6487 we are returning the address of a local variable. Warn if so. */
6488 while (1)
6490 switch (TREE_CODE (inner))
6492 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6493 case PLUS_EXPR:
6494 inner = TREE_OPERAND (inner, 0);
6495 continue;
6497 case MINUS_EXPR:
6498 /* If the second operand of the MINUS_EXPR has a pointer
6499 type (or is converted from it), this may be valid, so
6500 don't give a warning. */
6502 tree op1 = TREE_OPERAND (inner, 1);
6504 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6505 && (TREE_CODE (op1) == NOP_EXPR
6506 || TREE_CODE (op1) == NON_LVALUE_EXPR
6507 || TREE_CODE (op1) == CONVERT_EXPR))
6508 op1 = TREE_OPERAND (op1, 0);
6510 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6511 break;
6513 inner = TREE_OPERAND (inner, 0);
6514 continue;
6517 case ADDR_EXPR:
6518 inner = TREE_OPERAND (inner, 0);
6520 while (REFERENCE_CLASS_P (inner)
6521 && TREE_CODE (inner) != INDIRECT_REF)
6522 inner = TREE_OPERAND (inner, 0);
6524 if (DECL_P (inner)
6525 && !DECL_EXTERNAL (inner)
6526 && !TREE_STATIC (inner)
6527 && DECL_CONTEXT (inner) == current_function_decl)
6528 warning ("function returns address of local variable");
6529 break;
6531 default:
6532 break;
6535 break;
6538 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6541 return add_stmt (build_stmt (RETURN_EXPR, retval));
6544 struct c_switch {
6545 /* The SWITCH_STMT being built. */
6546 tree switch_stmt;
6548 /* The original type of the testing expression, i.e. before the
6549 default conversion is applied. */
6550 tree orig_type;
6552 /* A splay-tree mapping the low element of a case range to the high
6553 element, or NULL_TREE if there is no high element. Used to
6554 determine whether or not a new case label duplicates an old case
6555 label. We need a tree, rather than simply a hash table, because
6556 of the GNU case range extension. */
6557 splay_tree cases;
6559 /* The next node on the stack. */
6560 struct c_switch *next;
6563 /* A stack of the currently active switch statements. The innermost
6564 switch statement is on the top of the stack. There is no need to
6565 mark the stack for garbage collection because it is only active
6566 during the processing of the body of a function, and we never
6567 collect at that point. */
6569 struct c_switch *c_switch_stack;
6571 /* Start a C switch statement, testing expression EXP. Return the new
6572 SWITCH_STMT. */
6574 tree
6575 c_start_case (tree exp)
6577 enum tree_code code;
6578 tree type, orig_type = error_mark_node;
6579 struct c_switch *cs;
6581 if (exp != error_mark_node)
6583 code = TREE_CODE (TREE_TYPE (exp));
6584 orig_type = TREE_TYPE (exp);
6586 if (!INTEGRAL_TYPE_P (orig_type)
6587 && code != ERROR_MARK)
6589 error ("switch quantity not an integer");
6590 exp = integer_zero_node;
6591 orig_type = error_mark_node;
6593 else
6595 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6597 if (warn_traditional && !in_system_header
6598 && (type == long_integer_type_node
6599 || type == long_unsigned_type_node))
6600 warning ("%<long%> switch expression not converted to "
6601 "%<int%> in ISO C");
6603 exp = default_conversion (exp);
6604 type = TREE_TYPE (exp);
6608 /* Add this new SWITCH_STMT to the stack. */
6609 cs = XNEW (struct c_switch);
6610 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6611 cs->orig_type = orig_type;
6612 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6613 cs->next = c_switch_stack;
6614 c_switch_stack = cs;
6616 return add_stmt (cs->switch_stmt);
6619 /* Process a case label. */
6621 tree
6622 do_case (tree low_value, tree high_value)
6624 tree label = NULL_TREE;
6626 if (c_switch_stack)
6628 label = c_add_case_label (c_switch_stack->cases,
6629 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
6630 c_switch_stack->orig_type,
6631 low_value, high_value);
6632 if (label == error_mark_node)
6633 label = NULL_TREE;
6635 else if (low_value)
6636 error ("case label not within a switch statement");
6637 else
6638 error ("%<default%> label not within a switch statement");
6640 return label;
6643 /* Finish the switch statement. */
6645 void
6646 c_finish_case (tree body)
6648 struct c_switch *cs = c_switch_stack;
6650 SWITCH_STMT_BODY (cs->switch_stmt) = body;
6652 /* Emit warnings as needed. */
6653 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6655 /* Pop the stack. */
6656 c_switch_stack = cs->next;
6657 splay_tree_delete (cs->cases);
6658 XDELETE (cs);
6661 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6662 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6663 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6664 statement, and was not surrounded with parenthesis. */
6666 void
6667 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6668 tree else_block, bool nested_if)
6670 tree stmt;
6672 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6673 if (warn_parentheses && nested_if && else_block == NULL)
6675 tree inner_if = then_block;
6677 /* We know from the grammar productions that there is an IF nested
6678 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6679 it might not be exactly THEN_BLOCK, but should be the last
6680 non-container statement within. */
6681 while (1)
6682 switch (TREE_CODE (inner_if))
6684 case COND_EXPR:
6685 goto found;
6686 case BIND_EXPR:
6687 inner_if = BIND_EXPR_BODY (inner_if);
6688 break;
6689 case STATEMENT_LIST:
6690 inner_if = expr_last (then_block);
6691 break;
6692 case TRY_FINALLY_EXPR:
6693 case TRY_CATCH_EXPR:
6694 inner_if = TREE_OPERAND (inner_if, 0);
6695 break;
6696 default:
6697 gcc_unreachable ();
6699 found:
6701 if (COND_EXPR_ELSE (inner_if))
6702 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6703 &if_locus);
6706 /* Diagnose ";" via the special empty statement node that we create. */
6707 if (extra_warnings)
6709 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6711 if (!else_block)
6712 warning ("%Hempty body in an if-statement",
6713 EXPR_LOCUS (then_block));
6714 then_block = alloc_stmt_list ();
6716 if (else_block
6717 && TREE_CODE (else_block) == NOP_EXPR
6718 && !TREE_TYPE (else_block))
6720 warning ("%Hempty body in an else-statement",
6721 EXPR_LOCUS (else_block));
6722 else_block = alloc_stmt_list ();
6726 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6727 SET_EXPR_LOCATION (stmt, if_locus);
6728 add_stmt (stmt);
6731 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6732 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6733 is false for DO loops. INCR is the FOR increment expression. BODY is
6734 the statement controlled by the loop. BLAB is the break label. CLAB is
6735 the continue label. Everything is allowed to be NULL. */
6737 void
6738 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6739 tree blab, tree clab, bool cond_is_first)
6741 tree entry = NULL, exit = NULL, t;
6743 /* If the condition is zero don't generate a loop construct. */
6744 if (cond && integer_zerop (cond))
6746 if (cond_is_first)
6748 t = build_and_jump (&blab);
6749 SET_EXPR_LOCATION (t, start_locus);
6750 add_stmt (t);
6753 else
6755 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6757 /* If we have an exit condition, then we build an IF with gotos either
6758 out of the loop, or to the top of it. If there's no exit condition,
6759 then we just build a jump back to the top. */
6760 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6762 if (cond && !integer_nonzerop (cond))
6764 /* Canonicalize the loop condition to the end. This means
6765 generating a branch to the loop condition. Reuse the
6766 continue label, if possible. */
6767 if (cond_is_first)
6769 if (incr || !clab)
6771 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6772 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6774 else
6775 t = build1 (GOTO_EXPR, void_type_node, clab);
6776 SET_EXPR_LOCATION (t, start_locus);
6777 add_stmt (t);
6780 t = build_and_jump (&blab);
6781 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6782 exit = fold (exit);
6783 if (cond_is_first)
6784 SET_EXPR_LOCATION (exit, start_locus);
6785 else
6786 SET_EXPR_LOCATION (exit, input_location);
6789 add_stmt (top);
6792 if (body)
6793 add_stmt (body);
6794 if (clab)
6795 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6796 if (incr)
6797 add_stmt (incr);
6798 if (entry)
6799 add_stmt (entry);
6800 if (exit)
6801 add_stmt (exit);
6802 if (blab)
6803 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6806 tree
6807 c_finish_bc_stmt (tree *label_p, bool is_break)
6809 bool skip;
6810 tree label = *label_p;
6812 /* In switch statements break is sometimes stylistically used after
6813 a return statement. This can lead to spurious warnings about
6814 control reaching the end of a non-void function when it is
6815 inlined. Note that we are calling block_may_fallthru with
6816 language specific tree nodes; this works because
6817 block_may_fallthru returns true when given something it does not
6818 understand. */
6819 skip = !block_may_fallthru (cur_stmt_list);
6821 if (!label)
6823 if (!skip)
6824 *label_p = label = create_artificial_label ();
6826 else if (TREE_CODE (label) != LABEL_DECL)
6828 if (is_break)
6829 error ("break statement not within loop or switch");
6830 else
6831 error ("continue statement not within a loop");
6832 return NULL_TREE;
6835 if (skip)
6836 return NULL_TREE;
6838 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6841 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6843 static void
6844 emit_side_effect_warnings (tree expr)
6846 if (expr == error_mark_node)
6848 else if (!TREE_SIDE_EFFECTS (expr))
6850 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6851 warning ("%Hstatement with no effect",
6852 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6854 else if (warn_unused_value)
6855 warn_if_unused_value (expr, input_location);
6858 /* Process an expression as if it were a complete statement. Emit
6859 diagnostics, but do not call ADD_STMT. */
6861 tree
6862 c_process_expr_stmt (tree expr)
6864 if (!expr)
6865 return NULL_TREE;
6867 /* Do default conversion if safe and possibly important,
6868 in case within ({...}). */
6869 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6870 && (flag_isoc99 || lvalue_p (expr)))
6871 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6872 expr = default_conversion (expr);
6874 if (warn_sequence_point)
6875 verify_sequence_points (expr);
6877 if (TREE_TYPE (expr) != error_mark_node
6878 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6879 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6880 error ("expression statement has incomplete type");
6882 /* If we're not processing a statement expression, warn about unused values.
6883 Warnings for statement expressions will be emitted later, once we figure
6884 out which is the result. */
6885 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6886 && (extra_warnings || warn_unused_value))
6887 emit_side_effect_warnings (expr);
6889 /* If the expression is not of a type to which we cannot assign a line
6890 number, wrap the thing in a no-op NOP_EXPR. */
6891 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6892 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6894 if (EXPR_P (expr))
6895 SET_EXPR_LOCATION (expr, input_location);
6897 return expr;
6900 /* Emit an expression as a statement. */
6902 tree
6903 c_finish_expr_stmt (tree expr)
6905 if (expr)
6906 return add_stmt (c_process_expr_stmt (expr));
6907 else
6908 return NULL;
6911 /* Do the opposite and emit a statement as an expression. To begin,
6912 create a new binding level and return it. */
6914 tree
6915 c_begin_stmt_expr (void)
6917 tree ret;
6919 /* We must force a BLOCK for this level so that, if it is not expanded
6920 later, there is a way to turn off the entire subtree of blocks that
6921 are contained in it. */
6922 keep_next_level ();
6923 ret = c_begin_compound_stmt (true);
6925 /* Mark the current statement list as belonging to a statement list. */
6926 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6928 return ret;
6931 tree
6932 c_finish_stmt_expr (tree body)
6934 tree last, type, tmp, val;
6935 tree *last_p;
6937 body = c_end_compound_stmt (body, true);
6939 /* Locate the last statement in BODY. See c_end_compound_stmt
6940 about always returning a BIND_EXPR. */
6941 last_p = &BIND_EXPR_BODY (body);
6942 last = BIND_EXPR_BODY (body);
6944 continue_searching:
6945 if (TREE_CODE (last) == STATEMENT_LIST)
6947 tree_stmt_iterator i;
6949 /* This can happen with degenerate cases like ({ }). No value. */
6950 if (!TREE_SIDE_EFFECTS (last))
6951 return body;
6953 /* If we're supposed to generate side effects warnings, process
6954 all of the statements except the last. */
6955 if (extra_warnings || warn_unused_value)
6957 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6958 emit_side_effect_warnings (tsi_stmt (i));
6960 else
6961 i = tsi_last (last);
6962 last_p = tsi_stmt_ptr (i);
6963 last = *last_p;
6966 /* If the end of the list is exception related, then the list was split
6967 by a call to push_cleanup. Continue searching. */
6968 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6969 || TREE_CODE (last) == TRY_CATCH_EXPR)
6971 last_p = &TREE_OPERAND (last, 0);
6972 last = *last_p;
6973 goto continue_searching;
6976 /* In the case that the BIND_EXPR is not necessary, return the
6977 expression out from inside it. */
6978 if (last == error_mark_node
6979 || (last == BIND_EXPR_BODY (body)
6980 && BIND_EXPR_VARS (body) == NULL))
6981 return last;
6983 /* Extract the type of said expression. */
6984 type = TREE_TYPE (last);
6986 /* If we're not returning a value at all, then the BIND_EXPR that
6987 we already have is a fine expression to return. */
6988 if (!type || VOID_TYPE_P (type))
6989 return body;
6991 /* Now that we've located the expression containing the value, it seems
6992 silly to make voidify_wrapper_expr repeat the process. Create a
6993 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6994 tmp = create_tmp_var_raw (type, NULL);
6996 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6997 tree_expr_nonnegative_p giving up immediately. */
6998 val = last;
6999 if (TREE_CODE (val) == NOP_EXPR
7000 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7001 val = TREE_OPERAND (val, 0);
7003 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7004 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7006 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7009 /* Begin and end compound statements. This is as simple as pushing
7010 and popping new statement lists from the tree. */
7012 tree
7013 c_begin_compound_stmt (bool do_scope)
7015 tree stmt = push_stmt_list ();
7016 if (do_scope)
7017 push_scope ();
7018 return stmt;
7021 tree
7022 c_end_compound_stmt (tree stmt, bool do_scope)
7024 tree block = NULL;
7026 if (do_scope)
7028 if (c_dialect_objc ())
7029 objc_clear_super_receiver ();
7030 block = pop_scope ();
7033 stmt = pop_stmt_list (stmt);
7034 stmt = c_build_bind_expr (block, stmt);
7036 /* If this compound statement is nested immediately inside a statement
7037 expression, then force a BIND_EXPR to be created. Otherwise we'll
7038 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7039 STATEMENT_LISTs merge, and thus we can lose track of what statement
7040 was really last. */
7041 if (cur_stmt_list
7042 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7043 && TREE_CODE (stmt) != BIND_EXPR)
7045 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7046 TREE_SIDE_EFFECTS (stmt) = 1;
7049 return stmt;
7052 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7053 when the current scope is exited. EH_ONLY is true when this is not
7054 meant to apply to normal control flow transfer. */
7056 void
7057 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7059 enum tree_code code;
7060 tree stmt, list;
7061 bool stmt_expr;
7063 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7064 stmt = build_stmt (code, NULL, cleanup);
7065 add_stmt (stmt);
7066 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7067 list = push_stmt_list ();
7068 TREE_OPERAND (stmt, 0) = list;
7069 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7072 /* Build a binary-operation expression without default conversions.
7073 CODE is the kind of expression to build.
7074 This function differs from `build' in several ways:
7075 the data type of the result is computed and recorded in it,
7076 warnings are generated if arg data types are invalid,
7077 special handling for addition and subtraction of pointers is known,
7078 and some optimization is done (operations on narrow ints
7079 are done in the narrower type when that gives the same result).
7080 Constant folding is also done before the result is returned.
7082 Note that the operands will never have enumeral types, or function
7083 or array types, because either they will have the default conversions
7084 performed or they have both just been converted to some other type in which
7085 the arithmetic is to be done. */
7087 tree
7088 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7089 int convert_p)
7091 tree type0, type1;
7092 enum tree_code code0, code1;
7093 tree op0, op1;
7095 /* Expression code to give to the expression when it is built.
7096 Normally this is CODE, which is what the caller asked for,
7097 but in some special cases we change it. */
7098 enum tree_code resultcode = code;
7100 /* Data type in which the computation is to be performed.
7101 In the simplest cases this is the common type of the arguments. */
7102 tree result_type = NULL;
7104 /* Nonzero means operands have already been type-converted
7105 in whatever way is necessary.
7106 Zero means they need to be converted to RESULT_TYPE. */
7107 int converted = 0;
7109 /* Nonzero means create the expression with this type, rather than
7110 RESULT_TYPE. */
7111 tree build_type = 0;
7113 /* Nonzero means after finally constructing the expression
7114 convert it to this type. */
7115 tree final_type = 0;
7117 /* Nonzero if this is an operation like MIN or MAX which can
7118 safely be computed in short if both args are promoted shorts.
7119 Also implies COMMON.
7120 -1 indicates a bitwise operation; this makes a difference
7121 in the exact conditions for when it is safe to do the operation
7122 in a narrower mode. */
7123 int shorten = 0;
7125 /* Nonzero if this is a comparison operation;
7126 if both args are promoted shorts, compare the original shorts.
7127 Also implies COMMON. */
7128 int short_compare = 0;
7130 /* Nonzero if this is a right-shift operation, which can be computed on the
7131 original short and then promoted if the operand is a promoted short. */
7132 int short_shift = 0;
7134 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7135 int common = 0;
7137 if (convert_p)
7139 op0 = default_conversion (orig_op0);
7140 op1 = default_conversion (orig_op1);
7142 else
7144 op0 = orig_op0;
7145 op1 = orig_op1;
7148 type0 = TREE_TYPE (op0);
7149 type1 = TREE_TYPE (op1);
7151 /* The expression codes of the data types of the arguments tell us
7152 whether the arguments are integers, floating, pointers, etc. */
7153 code0 = TREE_CODE (type0);
7154 code1 = TREE_CODE (type1);
7156 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7157 STRIP_TYPE_NOPS (op0);
7158 STRIP_TYPE_NOPS (op1);
7160 /* If an error was already reported for one of the arguments,
7161 avoid reporting another error. */
7163 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7164 return error_mark_node;
7166 switch (code)
7168 case PLUS_EXPR:
7169 /* Handle the pointer + int case. */
7170 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7171 return pointer_int_sum (PLUS_EXPR, op0, op1);
7172 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7173 return pointer_int_sum (PLUS_EXPR, op1, op0);
7174 else
7175 common = 1;
7176 break;
7178 case MINUS_EXPR:
7179 /* Subtraction of two similar pointers.
7180 We must subtract them as integers, then divide by object size. */
7181 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7182 && comp_target_types (type0, type1, 1))
7183 return pointer_diff (op0, op1);
7184 /* Handle pointer minus int. Just like pointer plus int. */
7185 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7186 return pointer_int_sum (MINUS_EXPR, op0, op1);
7187 else
7188 common = 1;
7189 break;
7191 case MULT_EXPR:
7192 common = 1;
7193 break;
7195 case TRUNC_DIV_EXPR:
7196 case CEIL_DIV_EXPR:
7197 case FLOOR_DIV_EXPR:
7198 case ROUND_DIV_EXPR:
7199 case EXACT_DIV_EXPR:
7200 /* Floating point division by zero is a legitimate way to obtain
7201 infinities and NaNs. */
7202 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7203 warning ("division by zero");
7205 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7206 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7207 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7208 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7210 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7211 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7212 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7213 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7215 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7216 resultcode = RDIV_EXPR;
7217 else
7218 /* Although it would be tempting to shorten always here, that
7219 loses on some targets, since the modulo instruction is
7220 undefined if the quotient can't be represented in the
7221 computation mode. We shorten only if unsigned or if
7222 dividing by something we know != -1. */
7223 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7224 || (TREE_CODE (op1) == INTEGER_CST
7225 && !integer_all_onesp (op1)));
7226 common = 1;
7228 break;
7230 case BIT_AND_EXPR:
7231 case BIT_IOR_EXPR:
7232 case BIT_XOR_EXPR:
7233 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7234 shorten = -1;
7235 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7236 common = 1;
7237 break;
7239 case TRUNC_MOD_EXPR:
7240 case FLOOR_MOD_EXPR:
7241 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7242 warning ("division by zero");
7244 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7246 /* Although it would be tempting to shorten always here, that loses
7247 on some targets, since the modulo instruction is undefined if the
7248 quotient can't be represented in the computation mode. We shorten
7249 only if unsigned or if dividing by something we know != -1. */
7250 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7251 || (TREE_CODE (op1) == INTEGER_CST
7252 && !integer_all_onesp (op1)));
7253 common = 1;
7255 break;
7257 case TRUTH_ANDIF_EXPR:
7258 case TRUTH_ORIF_EXPR:
7259 case TRUTH_AND_EXPR:
7260 case TRUTH_OR_EXPR:
7261 case TRUTH_XOR_EXPR:
7262 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7263 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7264 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7265 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7267 /* Result of these operations is always an int,
7268 but that does not mean the operands should be
7269 converted to ints! */
7270 result_type = integer_type_node;
7271 op0 = lang_hooks.truthvalue_conversion (op0);
7272 op1 = lang_hooks.truthvalue_conversion (op1);
7273 converted = 1;
7275 break;
7277 /* Shift operations: result has same type as first operand;
7278 always convert second operand to int.
7279 Also set SHORT_SHIFT if shifting rightward. */
7281 case RSHIFT_EXPR:
7282 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7284 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7286 if (tree_int_cst_sgn (op1) < 0)
7287 warning ("right shift count is negative");
7288 else
7290 if (!integer_zerop (op1))
7291 short_shift = 1;
7293 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7294 warning ("right shift count >= width of type");
7298 /* Use the type of the value to be shifted. */
7299 result_type = type0;
7300 /* Convert the shift-count to an integer, regardless of size
7301 of value being shifted. */
7302 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7303 op1 = convert (integer_type_node, op1);
7304 /* Avoid converting op1 to result_type later. */
7305 converted = 1;
7307 break;
7309 case LSHIFT_EXPR:
7310 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7312 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7314 if (tree_int_cst_sgn (op1) < 0)
7315 warning ("left shift count is negative");
7317 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7318 warning ("left shift count >= width of type");
7321 /* Use the type of the value to be shifted. */
7322 result_type = type0;
7323 /* Convert the shift-count to an integer, regardless of size
7324 of value being shifted. */
7325 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7326 op1 = convert (integer_type_node, op1);
7327 /* Avoid converting op1 to result_type later. */
7328 converted = 1;
7330 break;
7332 case EQ_EXPR:
7333 case NE_EXPR:
7334 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7335 warning ("comparing floating point with == or != is unsafe");
7336 /* Result of comparison is always int,
7337 but don't convert the args to int! */
7338 build_type = integer_type_node;
7339 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7340 || code0 == COMPLEX_TYPE)
7341 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7342 || code1 == COMPLEX_TYPE))
7343 short_compare = 1;
7344 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7346 tree tt0 = TREE_TYPE (type0);
7347 tree tt1 = TREE_TYPE (type1);
7348 /* Anything compares with void *. void * compares with anything.
7349 Otherwise, the targets must be compatible
7350 and both must be object or both incomplete. */
7351 if (comp_target_types (type0, type1, 1))
7352 result_type = common_pointer_type (type0, type1);
7353 else if (VOID_TYPE_P (tt0))
7355 /* op0 != orig_op0 detects the case of something
7356 whose value is 0 but which isn't a valid null ptr const. */
7357 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7358 && TREE_CODE (tt1) == FUNCTION_TYPE)
7359 pedwarn ("ISO C forbids comparison of %<void *%>"
7360 " with function pointer");
7362 else if (VOID_TYPE_P (tt1))
7364 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7365 && TREE_CODE (tt0) == FUNCTION_TYPE)
7366 pedwarn ("ISO C forbids comparison of %<void *%>"
7367 " with function pointer");
7369 else
7370 pedwarn ("comparison of distinct pointer types lacks a cast");
7372 if (result_type == NULL_TREE)
7373 result_type = ptr_type_node;
7375 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7376 && integer_zerop (op1))
7377 result_type = type0;
7378 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7379 && integer_zerop (op0))
7380 result_type = type1;
7381 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7383 result_type = type0;
7384 pedwarn ("comparison between pointer and integer");
7386 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7388 result_type = type1;
7389 pedwarn ("comparison between pointer and integer");
7391 break;
7393 case LE_EXPR:
7394 case GE_EXPR:
7395 case LT_EXPR:
7396 case GT_EXPR:
7397 build_type = integer_type_node;
7398 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7399 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7400 short_compare = 1;
7401 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7403 if (comp_target_types (type0, type1, 1))
7405 result_type = common_pointer_type (type0, type1);
7406 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7407 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7408 pedwarn ("comparison of complete and incomplete pointers");
7409 else if (pedantic
7410 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7411 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7413 else
7415 result_type = ptr_type_node;
7416 pedwarn ("comparison of distinct pointer types lacks a cast");
7419 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7420 && integer_zerop (op1))
7422 result_type = type0;
7423 if (pedantic || extra_warnings)
7424 pedwarn ("ordered comparison of pointer with integer zero");
7426 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7427 && integer_zerop (op0))
7429 result_type = type1;
7430 if (pedantic)
7431 pedwarn ("ordered comparison of pointer with integer zero");
7433 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7435 result_type = type0;
7436 pedwarn ("comparison between pointer and integer");
7438 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7440 result_type = type1;
7441 pedwarn ("comparison between pointer and integer");
7443 break;
7445 default:
7446 gcc_unreachable ();
7449 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7450 return error_mark_node;
7452 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7453 || code0 == VECTOR_TYPE)
7455 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7456 || code1 == VECTOR_TYPE))
7458 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7460 if (shorten || common || short_compare)
7461 result_type = common_type (type0, type1);
7463 /* For certain operations (which identify themselves by shorten != 0)
7464 if both args were extended from the same smaller type,
7465 do the arithmetic in that type and then extend.
7467 shorten !=0 and !=1 indicates a bitwise operation.
7468 For them, this optimization is safe only if
7469 both args are zero-extended or both are sign-extended.
7470 Otherwise, we might change the result.
7471 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7472 but calculated in (unsigned short) it would be (unsigned short)-1. */
7474 if (shorten && none_complex)
7476 int unsigned0, unsigned1;
7477 tree arg0 = get_narrower (op0, &unsigned0);
7478 tree arg1 = get_narrower (op1, &unsigned1);
7479 /* UNS is 1 if the operation to be done is an unsigned one. */
7480 int uns = TYPE_UNSIGNED (result_type);
7481 tree type;
7483 final_type = result_type;
7485 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7486 but it *requires* conversion to FINAL_TYPE. */
7488 if ((TYPE_PRECISION (TREE_TYPE (op0))
7489 == TYPE_PRECISION (TREE_TYPE (arg0)))
7490 && TREE_TYPE (op0) != final_type)
7491 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7492 if ((TYPE_PRECISION (TREE_TYPE (op1))
7493 == TYPE_PRECISION (TREE_TYPE (arg1)))
7494 && TREE_TYPE (op1) != final_type)
7495 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7497 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7499 /* For bitwise operations, signedness of nominal type
7500 does not matter. Consider only how operands were extended. */
7501 if (shorten == -1)
7502 uns = unsigned0;
7504 /* Note that in all three cases below we refrain from optimizing
7505 an unsigned operation on sign-extended args.
7506 That would not be valid. */
7508 /* Both args variable: if both extended in same way
7509 from same width, do it in that width.
7510 Do it unsigned if args were zero-extended. */
7511 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7512 < TYPE_PRECISION (result_type))
7513 && (TYPE_PRECISION (TREE_TYPE (arg1))
7514 == TYPE_PRECISION (TREE_TYPE (arg0)))
7515 && unsigned0 == unsigned1
7516 && (unsigned0 || !uns))
7517 result_type
7518 = c_common_signed_or_unsigned_type
7519 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7520 else if (TREE_CODE (arg0) == INTEGER_CST
7521 && (unsigned1 || !uns)
7522 && (TYPE_PRECISION (TREE_TYPE (arg1))
7523 < TYPE_PRECISION (result_type))
7524 && (type
7525 = c_common_signed_or_unsigned_type (unsigned1,
7526 TREE_TYPE (arg1)),
7527 int_fits_type_p (arg0, type)))
7528 result_type = type;
7529 else if (TREE_CODE (arg1) == INTEGER_CST
7530 && (unsigned0 || !uns)
7531 && (TYPE_PRECISION (TREE_TYPE (arg0))
7532 < TYPE_PRECISION (result_type))
7533 && (type
7534 = c_common_signed_or_unsigned_type (unsigned0,
7535 TREE_TYPE (arg0)),
7536 int_fits_type_p (arg1, type)))
7537 result_type = type;
7540 /* Shifts can be shortened if shifting right. */
7542 if (short_shift)
7544 int unsigned_arg;
7545 tree arg0 = get_narrower (op0, &unsigned_arg);
7547 final_type = result_type;
7549 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7550 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7552 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7553 /* We can shorten only if the shift count is less than the
7554 number of bits in the smaller type size. */
7555 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7556 /* We cannot drop an unsigned shift after sign-extension. */
7557 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7559 /* Do an unsigned shift if the operand was zero-extended. */
7560 result_type
7561 = c_common_signed_or_unsigned_type (unsigned_arg,
7562 TREE_TYPE (arg0));
7563 /* Convert value-to-be-shifted to that type. */
7564 if (TREE_TYPE (op0) != result_type)
7565 op0 = convert (result_type, op0);
7566 converted = 1;
7570 /* Comparison operations are shortened too but differently.
7571 They identify themselves by setting short_compare = 1. */
7573 if (short_compare)
7575 /* Don't write &op0, etc., because that would prevent op0
7576 from being kept in a register.
7577 Instead, make copies of the our local variables and
7578 pass the copies by reference, then copy them back afterward. */
7579 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7580 enum tree_code xresultcode = resultcode;
7581 tree val
7582 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7584 if (val != 0)
7585 return val;
7587 op0 = xop0, op1 = xop1;
7588 converted = 1;
7589 resultcode = xresultcode;
7591 if (warn_sign_compare && skip_evaluation == 0)
7593 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7594 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7595 int unsignedp0, unsignedp1;
7596 tree primop0 = get_narrower (op0, &unsignedp0);
7597 tree primop1 = get_narrower (op1, &unsignedp1);
7599 xop0 = orig_op0;
7600 xop1 = orig_op1;
7601 STRIP_TYPE_NOPS (xop0);
7602 STRIP_TYPE_NOPS (xop1);
7604 /* Give warnings for comparisons between signed and unsigned
7605 quantities that may fail.
7607 Do the checking based on the original operand trees, so that
7608 casts will be considered, but default promotions won't be.
7610 Do not warn if the comparison is being done in a signed type,
7611 since the signed type will only be chosen if it can represent
7612 all the values of the unsigned type. */
7613 if (!TYPE_UNSIGNED (result_type))
7614 /* OK */;
7615 /* Do not warn if both operands are the same signedness. */
7616 else if (op0_signed == op1_signed)
7617 /* OK */;
7618 else
7620 tree sop, uop;
7622 if (op0_signed)
7623 sop = xop0, uop = xop1;
7624 else
7625 sop = xop1, uop = xop0;
7627 /* Do not warn if the signed quantity is an
7628 unsuffixed integer literal (or some static
7629 constant expression involving such literals or a
7630 conditional expression involving such literals)
7631 and it is non-negative. */
7632 if (tree_expr_nonnegative_p (sop))
7633 /* OK */;
7634 /* Do not warn if the comparison is an equality operation,
7635 the unsigned quantity is an integral constant, and it
7636 would fit in the result if the result were signed. */
7637 else if (TREE_CODE (uop) == INTEGER_CST
7638 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7639 && int_fits_type_p
7640 (uop, c_common_signed_type (result_type)))
7641 /* OK */;
7642 /* Do not warn if the unsigned quantity is an enumeration
7643 constant and its maximum value would fit in the result
7644 if the result were signed. */
7645 else if (TREE_CODE (uop) == INTEGER_CST
7646 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7647 && int_fits_type_p
7648 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7649 c_common_signed_type (result_type)))
7650 /* OK */;
7651 else
7652 warning ("comparison between signed and unsigned");
7655 /* Warn if two unsigned values are being compared in a size
7656 larger than their original size, and one (and only one) is the
7657 result of a `~' operator. This comparison will always fail.
7659 Also warn if one operand is a constant, and the constant
7660 does not have all bits set that are set in the ~ operand
7661 when it is extended. */
7663 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7664 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7666 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7667 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7668 &unsignedp0);
7669 else
7670 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7671 &unsignedp1);
7673 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7675 tree primop;
7676 HOST_WIDE_INT constant, mask;
7677 int unsignedp, bits;
7679 if (host_integerp (primop0, 0))
7681 primop = primop1;
7682 unsignedp = unsignedp1;
7683 constant = tree_low_cst (primop0, 0);
7685 else
7687 primop = primop0;
7688 unsignedp = unsignedp0;
7689 constant = tree_low_cst (primop1, 0);
7692 bits = TYPE_PRECISION (TREE_TYPE (primop));
7693 if (bits < TYPE_PRECISION (result_type)
7694 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7696 mask = (~(HOST_WIDE_INT) 0) << bits;
7697 if ((mask & constant) != mask)
7698 warning ("comparison of promoted ~unsigned with constant");
7701 else if (unsignedp0 && unsignedp1
7702 && (TYPE_PRECISION (TREE_TYPE (primop0))
7703 < TYPE_PRECISION (result_type))
7704 && (TYPE_PRECISION (TREE_TYPE (primop1))
7705 < TYPE_PRECISION (result_type)))
7706 warning ("comparison of promoted ~unsigned with unsigned");
7712 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7713 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7714 Then the expression will be built.
7715 It will be given type FINAL_TYPE if that is nonzero;
7716 otherwise, it will be given type RESULT_TYPE. */
7718 if (!result_type)
7720 binary_op_error (code);
7721 return error_mark_node;
7724 if (!converted)
7726 if (TREE_TYPE (op0) != result_type)
7727 op0 = convert (result_type, op0);
7728 if (TREE_TYPE (op1) != result_type)
7729 op1 = convert (result_type, op1);
7731 /* This can happen if one operand has a vector type, and the other
7732 has a different type. */
7733 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7734 return error_mark_node;
7737 if (build_type == NULL_TREE)
7738 build_type = result_type;
7741 tree result = build2 (resultcode, build_type, op0, op1);
7743 /* Treat expressions in initializers specially as they can't trap. */
7744 result = require_constant_value ? fold_initializer (result)
7745 : fold (result);
7747 if (final_type != 0)
7748 result = convert (final_type, result);
7749 return result;