* tree-ssa-threadupdate.c (copy_phis_to_block): Install PHI
[official-gcc.git] / gcc / c-typeck.c
blob728e768a7f63ee23218a60b055353c6d830af7e5
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
47 /* Places where an lvalue, or modifiable lvalue, may be required.
48 Used to select diagnostic messages in lvalue_or_else and
49 readonly_error. */
50 enum lvalue_use {
51 lv_assign,
52 lv_increment,
53 lv_decrement,
54 lv_addressof,
55 lv_asm
58 /* Possible cases of implicit bad conversions. Used to select
59 diagnostic messages in convert_for_assignment. */
60 enum impl_conv {
61 ic_argpass,
62 ic_argpass_nonproto,
63 ic_assign,
64 ic_init,
65 ic_return
68 /* The level of nesting inside "__alignof__". */
69 int in_alignof;
71 /* The level of nesting inside "sizeof". */
72 int in_sizeof;
74 /* The level of nesting inside "typeof". */
75 int in_typeof;
77 /* Nonzero if we've already printed a "missing braces around initializer"
78 message within this initializer. */
79 static int missing_braces_mentioned;
81 static int require_constant_value;
82 static int require_constant_elements;
84 static tree qualify_type (tree, tree);
85 static int tagged_types_tu_compatible_p (tree, tree);
86 static int comp_target_types (tree, tree, int);
87 static int function_types_compatible_p (tree, tree);
88 static int type_lists_compatible_p (tree, tree);
89 static tree decl_constant_value_for_broken_optimization (tree);
90 static tree default_function_array_conversion (tree);
91 static tree lookup_field (tree, tree);
92 static tree convert_arguments (tree, tree, tree, tree);
93 static tree pointer_diff (tree, tree);
94 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
95 int);
96 static tree valid_compound_expr_initializer (tree, tree);
97 static void push_string (const char *);
98 static void push_member_name (tree);
99 static void push_array_bounds (int);
100 static int spelling_length (void);
101 static char *print_spelling (char *);
102 static void warning_init (const char *);
103 static tree digest_init (tree, tree, bool, int);
104 static void output_init_element (tree, bool, tree, tree, int);
105 static void output_pending_init_elements (int);
106 static int set_designator (int);
107 static void push_range_stack (tree);
108 static void add_pending_init (tree, tree);
109 static void set_nonincremental_init (void);
110 static void set_nonincremental_init_from_string (tree);
111 static tree find_init_member (tree);
112 static int lvalue_or_else (tree, enum lvalue_use);
113 static void readonly_error (tree, enum lvalue_use);
115 /* Do `exp = require_complete_type (exp);' to make sure exp
116 does not have an incomplete type. (That includes void types.) */
118 tree
119 require_complete_type (tree value)
121 tree type = TREE_TYPE (value);
123 if (value == error_mark_node || type == error_mark_node)
124 return error_mark_node;
126 /* First, detect a valid value with a complete type. */
127 if (COMPLETE_TYPE_P (type))
128 return value;
130 c_incomplete_type_error (value, type);
131 return error_mark_node;
134 /* Print an error message for invalid use of an incomplete type.
135 VALUE is the expression that was used (or 0 if that isn't known)
136 and TYPE is the type that was invalid. */
138 void
139 c_incomplete_type_error (tree value, tree type)
141 const char *type_code_string;
143 /* Avoid duplicate error message. */
144 if (TREE_CODE (type) == ERROR_MARK)
145 return;
147 if (value != 0 && (TREE_CODE (value) == VAR_DECL
148 || TREE_CODE (value) == PARM_DECL))
149 error ("%qs has an incomplete type",
150 IDENTIFIER_POINTER (DECL_NAME (value)));
151 else
153 retry:
154 /* We must print an error message. Be clever about what it says. */
156 switch (TREE_CODE (type))
158 case RECORD_TYPE:
159 type_code_string = "struct";
160 break;
162 case UNION_TYPE:
163 type_code_string = "union";
164 break;
166 case ENUMERAL_TYPE:
167 type_code_string = "enum";
168 break;
170 case VOID_TYPE:
171 error ("invalid use of void expression");
172 return;
174 case ARRAY_TYPE:
175 if (TYPE_DOMAIN (type))
177 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
179 error ("invalid use of flexible array member");
180 return;
182 type = TREE_TYPE (type);
183 goto retry;
185 error ("invalid use of array with unspecified bounds");
186 return;
188 default:
189 gcc_unreachable ();
192 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
193 error ("invalid use of undefined type %<%s %s%>",
194 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
195 else
196 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
197 error ("invalid use of incomplete typedef %qs",
198 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
202 /* Given a type, apply default promotions wrt unnamed function
203 arguments and return the new type. */
205 tree
206 c_type_promotes_to (tree type)
208 if (TYPE_MAIN_VARIANT (type) == float_type_node)
209 return double_type_node;
211 if (c_promoting_integer_type_p (type))
213 /* Preserve unsignedness if not really getting any wider. */
214 if (TYPE_UNSIGNED (type)
215 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
216 return unsigned_type_node;
217 return integer_type_node;
220 return type;
223 /* Return a variant of TYPE which has all the type qualifiers of LIKE
224 as well as those of TYPE. */
226 static tree
227 qualify_type (tree type, tree like)
229 return c_build_qualified_type (type,
230 TYPE_QUALS (type) | TYPE_QUALS (like));
233 /* Return the composite type of two compatible types.
235 We assume that comptypes has already been done and returned
236 nonzero; if that isn't so, this may crash. In particular, we
237 assume that qualifiers match. */
239 tree
240 composite_type (tree t1, tree t2)
242 enum tree_code code1;
243 enum tree_code code2;
244 tree attributes;
246 /* Save time if the two types are the same. */
248 if (t1 == t2) return t1;
250 /* If one type is nonsense, use the other. */
251 if (t1 == error_mark_node)
252 return t2;
253 if (t2 == error_mark_node)
254 return t1;
256 code1 = TREE_CODE (t1);
257 code2 = TREE_CODE (t2);
259 /* Merge the attributes. */
260 attributes = targetm.merge_type_attributes (t1, t2);
262 /* If one is an enumerated type and the other is the compatible
263 integer type, the composite type might be either of the two
264 (DR#013 question 3). For consistency, use the enumerated type as
265 the composite type. */
267 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
268 return t1;
269 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
270 return t2;
272 gcc_assert (code1 == code2);
274 switch (code1)
276 case POINTER_TYPE:
277 /* For two pointers, do this recursively on the target type. */
279 tree pointed_to_1 = TREE_TYPE (t1);
280 tree pointed_to_2 = TREE_TYPE (t2);
281 tree target = composite_type (pointed_to_1, pointed_to_2);
282 t1 = build_pointer_type (target);
283 t1 = build_type_attribute_variant (t1, attributes);
284 return qualify_type (t1, t2);
287 case ARRAY_TYPE:
289 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
291 /* We should not have any type quals on arrays at all. */
292 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
294 /* Save space: see if the result is identical to one of the args. */
295 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
296 return build_type_attribute_variant (t1, attributes);
297 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
298 return build_type_attribute_variant (t2, attributes);
300 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
301 return build_type_attribute_variant (t1, attributes);
302 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
303 return build_type_attribute_variant (t2, attributes);
305 /* Merge the element types, and have a size if either arg has one. */
306 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
307 return build_type_attribute_variant (t1, attributes);
310 case FUNCTION_TYPE:
311 /* Function types: prefer the one that specified arg types.
312 If both do, merge the arg types. Also merge the return types. */
314 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
315 tree p1 = TYPE_ARG_TYPES (t1);
316 tree p2 = TYPE_ARG_TYPES (t2);
317 int len;
318 tree newargs, n;
319 int i;
321 /* Save space: see if the result is identical to one of the args. */
322 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
323 return build_type_attribute_variant (t1, attributes);
324 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
325 return build_type_attribute_variant (t2, attributes);
327 /* Simple way if one arg fails to specify argument types. */
328 if (TYPE_ARG_TYPES (t1) == 0)
330 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
331 t1 = build_type_attribute_variant (t1, attributes);
332 return qualify_type (t1, t2);
334 if (TYPE_ARG_TYPES (t2) == 0)
336 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
337 t1 = build_type_attribute_variant (t1, attributes);
338 return qualify_type (t1, t2);
341 /* If both args specify argument types, we must merge the two
342 lists, argument by argument. */
343 /* Tell global_bindings_p to return false so that variable_size
344 doesn't abort on VLAs in parameter types. */
345 c_override_global_bindings_to_false = true;
347 len = list_length (p1);
348 newargs = 0;
350 for (i = 0; i < len; i++)
351 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
353 n = newargs;
355 for (; p1;
356 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
358 /* A null type means arg type is not specified.
359 Take whatever the other function type has. */
360 if (TREE_VALUE (p1) == 0)
362 TREE_VALUE (n) = TREE_VALUE (p2);
363 goto parm_done;
365 if (TREE_VALUE (p2) == 0)
367 TREE_VALUE (n) = TREE_VALUE (p1);
368 goto parm_done;
371 /* Given wait (union {union wait *u; int *i} *)
372 and wait (union wait *),
373 prefer union wait * as type of parm. */
374 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
375 && TREE_VALUE (p1) != TREE_VALUE (p2))
377 tree memb;
378 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
379 memb; memb = TREE_CHAIN (memb))
380 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
382 TREE_VALUE (n) = TREE_VALUE (p2);
383 if (pedantic)
384 pedwarn ("function types not truly compatible in ISO C");
385 goto parm_done;
388 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
389 && TREE_VALUE (p2) != TREE_VALUE (p1))
391 tree memb;
392 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
393 memb; memb = TREE_CHAIN (memb))
394 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
396 TREE_VALUE (n) = TREE_VALUE (p1);
397 if (pedantic)
398 pedwarn ("function types not truly compatible in ISO C");
399 goto parm_done;
402 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
403 parm_done: ;
406 c_override_global_bindings_to_false = false;
407 t1 = build_function_type (valtype, newargs);
408 t1 = qualify_type (t1, t2);
409 /* ... falls through ... */
412 default:
413 return build_type_attribute_variant (t1, attributes);
418 /* Return the type of a conditional expression between pointers to
419 possibly differently qualified versions of compatible types.
421 We assume that comp_target_types has already been done and returned
422 nonzero; if that isn't so, this may crash. */
424 static tree
425 common_pointer_type (tree t1, tree t2)
427 tree attributes;
428 tree pointed_to_1;
429 tree pointed_to_2;
430 tree target;
432 /* Save time if the two types are the same. */
434 if (t1 == t2) return t1;
436 /* If one type is nonsense, use the other. */
437 if (t1 == error_mark_node)
438 return t2;
439 if (t2 == error_mark_node)
440 return t1;
442 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
443 && TREE_CODE (t2) == POINTER_TYPE);
445 /* Merge the attributes. */
446 attributes = targetm.merge_type_attributes (t1, t2);
448 /* Find the composite type of the target types, and combine the
449 qualifiers of the two types' targets. */
450 pointed_to_1 = TREE_TYPE (t1);
451 pointed_to_2 = TREE_TYPE (t2);
452 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
453 TYPE_MAIN_VARIANT (pointed_to_2));
454 t1 = build_pointer_type (c_build_qualified_type
455 (target,
456 TYPE_QUALS (pointed_to_1) |
457 TYPE_QUALS (pointed_to_2)));
458 return build_type_attribute_variant (t1, attributes);
461 /* Return the common type for two arithmetic types under the usual
462 arithmetic conversions. The default conversions have already been
463 applied, and enumerated types converted to their compatible integer
464 types. The resulting type is unqualified and has no attributes.
466 This is the type for the result of most arithmetic operations
467 if the operands have the given two types. */
469 tree
470 common_type (tree t1, tree t2)
472 enum tree_code code1;
473 enum tree_code code2;
475 /* If one type is nonsense, use the other. */
476 if (t1 == error_mark_node)
477 return t2;
478 if (t2 == error_mark_node)
479 return t1;
481 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
482 t1 = TYPE_MAIN_VARIANT (t1);
484 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
485 t2 = TYPE_MAIN_VARIANT (t2);
487 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
488 t1 = build_type_attribute_variant (t1, NULL_TREE);
490 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
491 t2 = build_type_attribute_variant (t2, NULL_TREE);
493 /* Save time if the two types are the same. */
495 if (t1 == t2) return t1;
497 code1 = TREE_CODE (t1);
498 code2 = TREE_CODE (t2);
500 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
501 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
502 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
503 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
505 /* If one type is a vector type, return that type. (How the usual
506 arithmetic conversions apply to the vector types extension is not
507 precisely specified.) */
508 if (code1 == VECTOR_TYPE)
509 return t1;
511 if (code2 == VECTOR_TYPE)
512 return t2;
514 /* If one type is complex, form the common type of the non-complex
515 components, then make that complex. Use T1 or T2 if it is the
516 required type. */
517 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
519 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
520 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
521 tree subtype = common_type (subtype1, subtype2);
523 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
524 return t1;
525 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
526 return t2;
527 else
528 return build_complex_type (subtype);
531 /* If only one is real, use it as the result. */
533 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
534 return t1;
536 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
537 return t2;
539 /* Both real or both integers; use the one with greater precision. */
541 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
542 return t1;
543 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
544 return t2;
546 /* Same precision. Prefer long longs to longs to ints when the
547 same precision, following the C99 rules on integer type rank
548 (which are equivalent to the C90 rules for C90 types). */
550 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
551 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
552 return long_long_unsigned_type_node;
554 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
555 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
557 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
558 return long_long_unsigned_type_node;
559 else
560 return long_long_integer_type_node;
563 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
564 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
565 return long_unsigned_type_node;
567 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
568 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
570 /* But preserve unsignedness from the other type,
571 since long cannot hold all the values of an unsigned int. */
572 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
573 return long_unsigned_type_node;
574 else
575 return long_integer_type_node;
578 /* Likewise, prefer long double to double even if same size. */
579 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
580 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
581 return long_double_type_node;
583 /* Otherwise prefer the unsigned one. */
585 if (TYPE_UNSIGNED (t1))
586 return t1;
587 else
588 return t2;
591 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
592 or various other operations. Return 2 if they are compatible
593 but a warning may be needed if you use them together. */
596 comptypes (tree type1, tree type2)
598 tree t1 = type1;
599 tree t2 = type2;
600 int attrval, val;
602 /* Suppress errors caused by previously reported errors. */
604 if (t1 == t2 || !t1 || !t2
605 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
606 return 1;
608 /* If either type is the internal version of sizetype, return the
609 language version. */
610 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
611 && TYPE_ORIG_SIZE_TYPE (t1))
612 t1 = TYPE_ORIG_SIZE_TYPE (t1);
614 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
615 && TYPE_ORIG_SIZE_TYPE (t2))
616 t2 = TYPE_ORIG_SIZE_TYPE (t2);
619 /* Enumerated types are compatible with integer types, but this is
620 not transitive: two enumerated types in the same translation unit
621 are compatible with each other only if they are the same type. */
623 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
624 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
625 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
626 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
628 if (t1 == t2)
629 return 1;
631 /* Different classes of types can't be compatible. */
633 if (TREE_CODE (t1) != TREE_CODE (t2))
634 return 0;
636 /* Qualifiers must match. C99 6.7.3p9 */
638 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
639 return 0;
641 /* Allow for two different type nodes which have essentially the same
642 definition. Note that we already checked for equality of the type
643 qualifiers (just above). */
645 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
646 return 1;
648 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
649 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
650 return 0;
652 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
653 val = 0;
655 switch (TREE_CODE (t1))
657 case POINTER_TYPE:
658 /* We must give ObjC the first crack at comparing pointers, since
659 protocol qualifiers may be involved. */
660 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
661 break;
662 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
663 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
664 break;
666 case FUNCTION_TYPE:
667 val = function_types_compatible_p (t1, t2);
668 break;
670 case ARRAY_TYPE:
672 tree d1 = TYPE_DOMAIN (t1);
673 tree d2 = TYPE_DOMAIN (t2);
674 bool d1_variable, d2_variable;
675 bool d1_zero, d2_zero;
676 val = 1;
678 /* Target types must match incl. qualifiers. */
679 if (TREE_TYPE (t1) != TREE_TYPE (t2)
680 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
681 return 0;
683 /* Sizes must match unless one is missing or variable. */
684 if (d1 == 0 || d2 == 0 || d1 == d2)
685 break;
687 d1_zero = !TYPE_MAX_VALUE (d1);
688 d2_zero = !TYPE_MAX_VALUE (d2);
690 d1_variable = (!d1_zero
691 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
692 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
693 d2_variable = (!d2_zero
694 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
695 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
697 if (d1_variable || d2_variable)
698 break;
699 if (d1_zero && d2_zero)
700 break;
701 if (d1_zero || d2_zero
702 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
703 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
704 val = 0;
706 break;
709 case RECORD_TYPE:
710 /* We are dealing with two distinct structs. In assorted Objective-C
711 corner cases, however, these can still be deemed equivalent. */
712 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
713 val = 1;
715 case ENUMERAL_TYPE:
716 case UNION_TYPE:
717 if (val != 1 && !same_translation_unit_p (t1, t2))
718 val = tagged_types_tu_compatible_p (t1, t2);
719 break;
721 case VECTOR_TYPE:
722 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
723 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
724 break;
726 default:
727 break;
729 return attrval == 2 && val == 1 ? 2 : val;
732 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
733 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
734 to 1 or 0 depending if the check of the pointer types is meant to
735 be reflexive or not (typically, assignments are not reflexive,
736 while comparisons are reflexive).
739 static int
740 comp_target_types (tree ttl, tree ttr, int reflexive)
742 int val;
744 /* Give objc_comptypes a crack at letting these types through. */
745 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
746 return val;
748 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
749 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
751 if (val == 2 && pedantic)
752 pedwarn ("types are not quite compatible");
753 return val;
756 /* Subroutines of `comptypes'. */
758 /* Determine whether two trees derive from the same translation unit.
759 If the CONTEXT chain ends in a null, that tree's context is still
760 being parsed, so if two trees have context chains ending in null,
761 they're in the same translation unit. */
763 same_translation_unit_p (tree t1, tree t2)
765 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
766 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
768 case tcc_declaration:
769 t1 = DECL_CONTEXT (t1); break;
770 case tcc_type:
771 t1 = TYPE_CONTEXT (t1); break;
772 case tcc_exceptional:
773 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
774 default: gcc_unreachable ();
777 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
778 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
780 case tcc_declaration:
781 t2 = DECL_CONTEXT (t2); break;
782 case tcc_type:
783 t2 = TYPE_CONTEXT (t2); break;
784 case tcc_exceptional:
785 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
786 default: gcc_unreachable ();
789 return t1 == t2;
792 /* The C standard says that two structures in different translation
793 units are compatible with each other only if the types of their
794 fields are compatible (among other things). So, consider two copies
795 of this structure: */
797 struct tagged_tu_seen {
798 const struct tagged_tu_seen * next;
799 tree t1;
800 tree t2;
803 /* Can they be compatible with each other? We choose to break the
804 recursion by allowing those types to be compatible. */
806 static const struct tagged_tu_seen * tagged_tu_seen_base;
808 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
809 compatible. If the two types are not the same (which has been
810 checked earlier), this can only happen when multiple translation
811 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
812 rules. */
814 static int
815 tagged_types_tu_compatible_p (tree t1, tree t2)
817 tree s1, s2;
818 bool needs_warning = false;
820 /* We have to verify that the tags of the types are the same. This
821 is harder than it looks because this may be a typedef, so we have
822 to go look at the original type. It may even be a typedef of a
823 typedef...
824 In the case of compiler-created builtin structs the TYPE_DECL
825 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
826 while (TYPE_NAME (t1)
827 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
828 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
829 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
831 while (TYPE_NAME (t2)
832 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
833 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
834 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
836 /* C90 didn't have the requirement that the two tags be the same. */
837 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
838 return 0;
840 /* C90 didn't say what happened if one or both of the types were
841 incomplete; we choose to follow C99 rules here, which is that they
842 are compatible. */
843 if (TYPE_SIZE (t1) == NULL
844 || TYPE_SIZE (t2) == NULL)
845 return 1;
848 const struct tagged_tu_seen * tts_i;
849 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
850 if (tts_i->t1 == t1 && tts_i->t2 == t2)
851 return 1;
854 switch (TREE_CODE (t1))
856 case ENUMERAL_TYPE:
859 /* Speed up the case where the type values are in the same order. */
860 tree tv1 = TYPE_VALUES (t1);
861 tree tv2 = TYPE_VALUES (t2);
863 if (tv1 == tv2)
864 return 1;
866 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
868 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
869 break;
870 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
871 return 0;
874 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
875 return 1;
876 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
877 return 0;
879 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
880 return 0;
882 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
884 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
885 if (s2 == NULL
886 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
887 return 0;
889 return 1;
892 case UNION_TYPE:
894 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
895 return 0;
897 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
899 bool ok = false;
900 struct tagged_tu_seen tts;
902 tts.next = tagged_tu_seen_base;
903 tts.t1 = t1;
904 tts.t2 = t2;
905 tagged_tu_seen_base = &tts;
907 if (DECL_NAME (s1) != NULL)
908 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
909 if (DECL_NAME (s1) == DECL_NAME (s2))
911 int result;
912 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
913 if (result == 0)
914 break;
915 if (result == 2)
916 needs_warning = true;
918 if (TREE_CODE (s1) == FIELD_DECL
919 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
920 DECL_FIELD_BIT_OFFSET (s2)) != 1)
921 break;
923 ok = true;
924 break;
926 tagged_tu_seen_base = tts.next;
927 if (!ok)
928 return 0;
930 return needs_warning ? 2 : 1;
933 case RECORD_TYPE:
935 struct tagged_tu_seen tts;
937 tts.next = tagged_tu_seen_base;
938 tts.t1 = t1;
939 tts.t2 = t2;
940 tagged_tu_seen_base = &tts;
942 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
943 s1 && s2;
944 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
946 int result;
947 if (TREE_CODE (s1) != TREE_CODE (s2)
948 || DECL_NAME (s1) != DECL_NAME (s2))
949 break;
950 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
951 if (result == 0)
952 break;
953 if (result == 2)
954 needs_warning = true;
956 if (TREE_CODE (s1) == FIELD_DECL
957 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
958 DECL_FIELD_BIT_OFFSET (s2)) != 1)
959 break;
961 tagged_tu_seen_base = tts.next;
962 if (s1 && s2)
963 return 0;
964 return needs_warning ? 2 : 1;
967 default:
968 gcc_unreachable ();
972 /* Return 1 if two function types F1 and F2 are compatible.
973 If either type specifies no argument types,
974 the other must specify a fixed number of self-promoting arg types.
975 Otherwise, if one type specifies only the number of arguments,
976 the other must specify that number of self-promoting arg types.
977 Otherwise, the argument types must match. */
979 static int
980 function_types_compatible_p (tree f1, tree f2)
982 tree args1, args2;
983 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
984 int val = 1;
985 int val1;
986 tree ret1, ret2;
988 ret1 = TREE_TYPE (f1);
989 ret2 = TREE_TYPE (f2);
991 /* 'volatile' qualifiers on a function's return type used to mean
992 the function is noreturn. */
993 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
994 pedwarn ("function return types not compatible due to %<volatile%>");
995 if (TYPE_VOLATILE (ret1))
996 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
997 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
998 if (TYPE_VOLATILE (ret2))
999 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1000 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1001 val = comptypes (ret1, ret2);
1002 if (val == 0)
1003 return 0;
1005 args1 = TYPE_ARG_TYPES (f1);
1006 args2 = TYPE_ARG_TYPES (f2);
1008 /* An unspecified parmlist matches any specified parmlist
1009 whose argument types don't need default promotions. */
1011 if (args1 == 0)
1013 if (!self_promoting_args_p (args2))
1014 return 0;
1015 /* If one of these types comes from a non-prototype fn definition,
1016 compare that with the other type's arglist.
1017 If they don't match, ask for a warning (but no error). */
1018 if (TYPE_ACTUAL_ARG_TYPES (f1)
1019 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1020 val = 2;
1021 return val;
1023 if (args2 == 0)
1025 if (!self_promoting_args_p (args1))
1026 return 0;
1027 if (TYPE_ACTUAL_ARG_TYPES (f2)
1028 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1029 val = 2;
1030 return val;
1033 /* Both types have argument lists: compare them and propagate results. */
1034 val1 = type_lists_compatible_p (args1, args2);
1035 return val1 != 1 ? val1 : val;
1038 /* Check two lists of types for compatibility,
1039 returning 0 for incompatible, 1 for compatible,
1040 or 2 for compatible with warning. */
1042 static int
1043 type_lists_compatible_p (tree args1, tree args2)
1045 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1046 int val = 1;
1047 int newval = 0;
1049 while (1)
1051 if (args1 == 0 && args2 == 0)
1052 return val;
1053 /* If one list is shorter than the other,
1054 they fail to match. */
1055 if (args1 == 0 || args2 == 0)
1056 return 0;
1057 /* A null pointer instead of a type
1058 means there is supposed to be an argument
1059 but nothing is specified about what type it has.
1060 So match anything that self-promotes. */
1061 if (TREE_VALUE (args1) == 0)
1063 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1064 return 0;
1066 else if (TREE_VALUE (args2) == 0)
1068 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1069 return 0;
1071 /* If one of the lists has an error marker, ignore this arg. */
1072 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1073 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1075 else if (!(newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1076 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1078 /* Allow wait (union {union wait *u; int *i} *)
1079 and wait (union wait *) to be compatible. */
1080 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1081 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1082 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1083 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1084 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1085 TYPE_SIZE (TREE_VALUE (args2))))
1087 tree memb;
1088 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1089 memb; memb = TREE_CHAIN (memb))
1090 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1091 break;
1092 if (memb == 0)
1093 return 0;
1095 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1096 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1097 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1098 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1099 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1100 TYPE_SIZE (TREE_VALUE (args1))))
1102 tree memb;
1103 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1104 memb; memb = TREE_CHAIN (memb))
1105 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1106 break;
1107 if (memb == 0)
1108 return 0;
1110 else
1111 return 0;
1114 /* comptypes said ok, but record if it said to warn. */
1115 if (newval > val)
1116 val = newval;
1118 args1 = TREE_CHAIN (args1);
1119 args2 = TREE_CHAIN (args2);
1123 /* Compute the size to increment a pointer by. */
1125 tree
1126 c_size_in_bytes (tree type)
1128 enum tree_code code = TREE_CODE (type);
1130 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1131 return size_one_node;
1133 if (!COMPLETE_OR_VOID_TYPE_P (type))
1135 error ("arithmetic on pointer to an incomplete type");
1136 return size_one_node;
1139 /* Convert in case a char is more than one unit. */
1140 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1141 size_int (TYPE_PRECISION (char_type_node)
1142 / BITS_PER_UNIT));
1145 /* Return either DECL or its known constant value (if it has one). */
1147 tree
1148 decl_constant_value (tree decl)
1150 if (/* Don't change a variable array bound or initial value to a constant
1151 in a place where a variable is invalid. Note that DECL_INITIAL
1152 isn't valid for a PARM_DECL. */
1153 current_function_decl != 0
1154 && TREE_CODE (decl) != PARM_DECL
1155 && !TREE_THIS_VOLATILE (decl)
1156 && TREE_READONLY (decl)
1157 && DECL_INITIAL (decl) != 0
1158 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1159 /* This is invalid if initial value is not constant.
1160 If it has either a function call, a memory reference,
1161 or a variable, then re-evaluating it could give different results. */
1162 && TREE_CONSTANT (DECL_INITIAL (decl))
1163 /* Check for cases where this is sub-optimal, even though valid. */
1164 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1165 return DECL_INITIAL (decl);
1166 return decl;
1169 /* Return either DECL or its known constant value (if it has one), but
1170 return DECL if pedantic or DECL has mode BLKmode. This is for
1171 bug-compatibility with the old behavior of decl_constant_value
1172 (before GCC 3.0); every use of this function is a bug and it should
1173 be removed before GCC 3.1. It is not appropriate to use pedantic
1174 in a way that affects optimization, and BLKmode is probably not the
1175 right test for avoiding misoptimizations either. */
1177 static tree
1178 decl_constant_value_for_broken_optimization (tree decl)
1180 if (pedantic || DECL_MODE (decl) == BLKmode)
1181 return decl;
1182 else
1183 return decl_constant_value (decl);
1187 /* Perform the default conversion of arrays and functions to pointers.
1188 Return the result of converting EXP. For any other expression, just
1189 return EXP. */
1191 static tree
1192 default_function_array_conversion (tree exp)
1194 tree orig_exp;
1195 tree type = TREE_TYPE (exp);
1196 enum tree_code code = TREE_CODE (type);
1197 int not_lvalue = 0;
1199 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1200 an lvalue.
1202 Do not use STRIP_NOPS here! It will remove conversions from pointer
1203 to integer and cause infinite recursion. */
1204 orig_exp = exp;
1205 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1206 || (TREE_CODE (exp) == NOP_EXPR
1207 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1209 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1210 not_lvalue = 1;
1211 exp = TREE_OPERAND (exp, 0);
1214 if (TREE_NO_WARNING (orig_exp))
1215 TREE_NO_WARNING (exp) = 1;
1217 if (code == FUNCTION_TYPE)
1219 return build_unary_op (ADDR_EXPR, exp, 0);
1221 if (code == ARRAY_TYPE)
1223 tree adr;
1224 tree restype = TREE_TYPE (type);
1225 tree ptrtype;
1226 int constp = 0;
1227 int volatilep = 0;
1228 int lvalue_array_p;
1230 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1232 constp = TREE_READONLY (exp);
1233 volatilep = TREE_THIS_VOLATILE (exp);
1236 if (TYPE_QUALS (type) || constp || volatilep)
1237 restype
1238 = c_build_qualified_type (restype,
1239 TYPE_QUALS (type)
1240 | (constp * TYPE_QUAL_CONST)
1241 | (volatilep * TYPE_QUAL_VOLATILE));
1243 if (TREE_CODE (exp) == INDIRECT_REF)
1244 return convert (build_pointer_type (restype),
1245 TREE_OPERAND (exp, 0));
1247 if (TREE_CODE (exp) == COMPOUND_EXPR)
1249 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1250 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1251 TREE_OPERAND (exp, 0), op1);
1254 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1255 if (!flag_isoc99 && !lvalue_array_p)
1257 /* Before C99, non-lvalue arrays do not decay to pointers.
1258 Normally, using such an array would be invalid; but it can
1259 be used correctly inside sizeof or as a statement expression.
1260 Thus, do not give an error here; an error will result later. */
1261 return exp;
1264 ptrtype = build_pointer_type (restype);
1266 if (TREE_CODE (exp) == VAR_DECL)
1268 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1269 ADDR_EXPR because it's the best way of representing what
1270 happens in C when we take the address of an array and place
1271 it in a pointer to the element type. */
1272 adr = build1 (ADDR_EXPR, ptrtype, exp);
1273 if (!c_mark_addressable (exp))
1274 return error_mark_node;
1275 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1276 return adr;
1278 /* This way is better for a COMPONENT_REF since it can
1279 simplify the offset for a component. */
1280 adr = build_unary_op (ADDR_EXPR, exp, 1);
1281 return convert (ptrtype, adr);
1283 return exp;
1286 /* Perform default promotions for C data used in expressions.
1287 Arrays and functions are converted to pointers;
1288 enumeral types or short or char, to int.
1289 In addition, manifest constants symbols are replaced by their values. */
1291 tree
1292 default_conversion (tree exp)
1294 tree orig_exp;
1295 tree type = TREE_TYPE (exp);
1296 enum tree_code code = TREE_CODE (type);
1298 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1299 return default_function_array_conversion (exp);
1301 /* Constants can be used directly unless they're not loadable. */
1302 if (TREE_CODE (exp) == CONST_DECL)
1303 exp = DECL_INITIAL (exp);
1305 /* Replace a nonvolatile const static variable with its value unless
1306 it is an array, in which case we must be sure that taking the
1307 address of the array produces consistent results. */
1308 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1310 exp = decl_constant_value_for_broken_optimization (exp);
1311 type = TREE_TYPE (exp);
1314 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1315 an lvalue.
1317 Do not use STRIP_NOPS here! It will remove conversions from pointer
1318 to integer and cause infinite recursion. */
1319 orig_exp = exp;
1320 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1321 || (TREE_CODE (exp) == NOP_EXPR
1322 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1323 exp = TREE_OPERAND (exp, 0);
1325 if (TREE_NO_WARNING (orig_exp))
1326 TREE_NO_WARNING (exp) = 1;
1328 /* Normally convert enums to int,
1329 but convert wide enums to something wider. */
1330 if (code == ENUMERAL_TYPE)
1332 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1333 TYPE_PRECISION (integer_type_node)),
1334 ((TYPE_PRECISION (type)
1335 >= TYPE_PRECISION (integer_type_node))
1336 && TYPE_UNSIGNED (type)));
1338 return convert (type, exp);
1341 if (TREE_CODE (exp) == COMPONENT_REF
1342 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1343 /* If it's thinner than an int, promote it like a
1344 c_promoting_integer_type_p, otherwise leave it alone. */
1345 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1346 TYPE_PRECISION (integer_type_node)))
1347 return convert (integer_type_node, exp);
1349 if (c_promoting_integer_type_p (type))
1351 /* Preserve unsignedness if not really getting any wider. */
1352 if (TYPE_UNSIGNED (type)
1353 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1354 return convert (unsigned_type_node, exp);
1356 return convert (integer_type_node, exp);
1359 if (code == VOID_TYPE)
1361 error ("void value not ignored as it ought to be");
1362 return error_mark_node;
1364 return exp;
1367 /* Look up COMPONENT in a structure or union DECL.
1369 If the component name is not found, returns NULL_TREE. Otherwise,
1370 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1371 stepping down the chain to the component, which is in the last
1372 TREE_VALUE of the list. Normally the list is of length one, but if
1373 the component is embedded within (nested) anonymous structures or
1374 unions, the list steps down the chain to the component. */
1376 static tree
1377 lookup_field (tree decl, tree component)
1379 tree type = TREE_TYPE (decl);
1380 tree field;
1382 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1383 to the field elements. Use a binary search on this array to quickly
1384 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1385 will always be set for structures which have many elements. */
1387 if (TYPE_LANG_SPECIFIC (type))
1389 int bot, top, half;
1390 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1392 field = TYPE_FIELDS (type);
1393 bot = 0;
1394 top = TYPE_LANG_SPECIFIC (type)->s->len;
1395 while (top - bot > 1)
1397 half = (top - bot + 1) >> 1;
1398 field = field_array[bot+half];
1400 if (DECL_NAME (field) == NULL_TREE)
1402 /* Step through all anon unions in linear fashion. */
1403 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1405 field = field_array[bot++];
1406 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1407 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1409 tree anon = lookup_field (field, component);
1411 if (anon)
1412 return tree_cons (NULL_TREE, field, anon);
1416 /* Entire record is only anon unions. */
1417 if (bot > top)
1418 return NULL_TREE;
1420 /* Restart the binary search, with new lower bound. */
1421 continue;
1424 if (DECL_NAME (field) == component)
1425 break;
1426 if (DECL_NAME (field) < component)
1427 bot += half;
1428 else
1429 top = bot + half;
1432 if (DECL_NAME (field_array[bot]) == component)
1433 field = field_array[bot];
1434 else if (DECL_NAME (field) != component)
1435 return NULL_TREE;
1437 else
1439 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1441 if (DECL_NAME (field) == NULL_TREE
1442 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1443 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1445 tree anon = lookup_field (field, component);
1447 if (anon)
1448 return tree_cons (NULL_TREE, field, anon);
1451 if (DECL_NAME (field) == component)
1452 break;
1455 if (field == NULL_TREE)
1456 return NULL_TREE;
1459 return tree_cons (NULL_TREE, field, NULL_TREE);
1462 /* Make an expression to refer to the COMPONENT field of
1463 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1465 tree
1466 build_component_ref (tree datum, tree component)
1468 tree type = TREE_TYPE (datum);
1469 enum tree_code code = TREE_CODE (type);
1470 tree field = NULL;
1471 tree ref;
1473 if (!objc_is_public (datum, component))
1474 return error_mark_node;
1476 /* See if there is a field or component with name COMPONENT. */
1478 if (code == RECORD_TYPE || code == UNION_TYPE)
1480 if (!COMPLETE_TYPE_P (type))
1482 c_incomplete_type_error (NULL_TREE, type);
1483 return error_mark_node;
1486 field = lookup_field (datum, component);
1488 if (!field)
1490 error ("%qT has no member named %qs", type,
1491 IDENTIFIER_POINTER (component));
1492 return error_mark_node;
1495 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1496 This might be better solved in future the way the C++ front
1497 end does it - by giving the anonymous entities each a
1498 separate name and type, and then have build_component_ref
1499 recursively call itself. We can't do that here. */
1502 tree subdatum = TREE_VALUE (field);
1504 if (TREE_TYPE (subdatum) == error_mark_node)
1505 return error_mark_node;
1507 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1508 NULL_TREE);
1509 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1510 TREE_READONLY (ref) = 1;
1511 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1512 TREE_THIS_VOLATILE (ref) = 1;
1514 if (TREE_DEPRECATED (subdatum))
1515 warn_deprecated_use (subdatum);
1517 datum = ref;
1519 field = TREE_CHAIN (field);
1521 while (field);
1523 return ref;
1525 else if (code != ERROR_MARK)
1526 error ("request for member %qs in something not a structure or union",
1527 IDENTIFIER_POINTER (component));
1529 return error_mark_node;
1532 /* Given an expression PTR for a pointer, return an expression
1533 for the value pointed to.
1534 ERRORSTRING is the name of the operator to appear in error messages. */
1536 tree
1537 build_indirect_ref (tree ptr, const char *errorstring)
1539 tree pointer = default_conversion (ptr);
1540 tree type = TREE_TYPE (pointer);
1542 if (TREE_CODE (type) == POINTER_TYPE)
1544 if (TREE_CODE (pointer) == ADDR_EXPR
1545 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1546 == TREE_TYPE (type)))
1547 return TREE_OPERAND (pointer, 0);
1548 else
1550 tree t = TREE_TYPE (type);
1551 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1553 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1555 error ("dereferencing pointer to incomplete type");
1556 return error_mark_node;
1558 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1559 warning ("dereferencing %<void *%> pointer");
1561 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1562 so that we get the proper error message if the result is used
1563 to assign to. Also, &* is supposed to be a no-op.
1564 And ANSI C seems to specify that the type of the result
1565 should be the const type. */
1566 /* A de-reference of a pointer to const is not a const. It is valid
1567 to change it via some other pointer. */
1568 TREE_READONLY (ref) = TYPE_READONLY (t);
1569 TREE_SIDE_EFFECTS (ref)
1570 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1571 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1572 return ref;
1575 else if (TREE_CODE (pointer) != ERROR_MARK)
1576 error ("invalid type argument of %qs", errorstring);
1577 return error_mark_node;
1580 /* This handles expressions of the form "a[i]", which denotes
1581 an array reference.
1583 This is logically equivalent in C to *(a+i), but we may do it differently.
1584 If A is a variable or a member, we generate a primitive ARRAY_REF.
1585 This avoids forcing the array out of registers, and can work on
1586 arrays that are not lvalues (for example, members of structures returned
1587 by functions). */
1589 tree
1590 build_array_ref (tree array, tree index)
1592 if (index == 0)
1594 error ("subscript missing in array reference");
1595 return error_mark_node;
1598 if (TREE_TYPE (array) == error_mark_node
1599 || TREE_TYPE (index) == error_mark_node)
1600 return error_mark_node;
1602 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1604 tree rval, type;
1606 /* Subscripting with type char is likely to lose
1607 on a machine where chars are signed.
1608 So warn on any machine, but optionally.
1609 Don't warn for unsigned char since that type is safe.
1610 Don't warn for signed char because anyone who uses that
1611 must have done so deliberately. */
1612 if (warn_char_subscripts
1613 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1614 warning ("array subscript has type %<char%>");
1616 /* Apply default promotions *after* noticing character types. */
1617 index = default_conversion (index);
1619 /* Require integer *after* promotion, for sake of enums. */
1620 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1622 error ("array subscript is not an integer");
1623 return error_mark_node;
1626 /* An array that is indexed by a non-constant
1627 cannot be stored in a register; we must be able to do
1628 address arithmetic on its address.
1629 Likewise an array of elements of variable size. */
1630 if (TREE_CODE (index) != INTEGER_CST
1631 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1632 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1634 if (!c_mark_addressable (array))
1635 return error_mark_node;
1637 /* An array that is indexed by a constant value which is not within
1638 the array bounds cannot be stored in a register either; because we
1639 would get a crash in store_bit_field/extract_bit_field when trying
1640 to access a non-existent part of the register. */
1641 if (TREE_CODE (index) == INTEGER_CST
1642 && TYPE_DOMAIN (TREE_TYPE (array))
1643 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1645 if (!c_mark_addressable (array))
1646 return error_mark_node;
1649 if (pedantic)
1651 tree foo = array;
1652 while (TREE_CODE (foo) == COMPONENT_REF)
1653 foo = TREE_OPERAND (foo, 0);
1654 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1655 pedwarn ("ISO C forbids subscripting %<register%> array");
1656 else if (!flag_isoc99 && !lvalue_p (foo))
1657 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1660 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1661 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1662 /* Array ref is const/volatile if the array elements are
1663 or if the array is. */
1664 TREE_READONLY (rval)
1665 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1666 | TREE_READONLY (array));
1667 TREE_SIDE_EFFECTS (rval)
1668 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1669 | TREE_SIDE_EFFECTS (array));
1670 TREE_THIS_VOLATILE (rval)
1671 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1672 /* This was added by rms on 16 Nov 91.
1673 It fixes vol struct foo *a; a->elts[1]
1674 in an inline function.
1675 Hope it doesn't break something else. */
1676 | TREE_THIS_VOLATILE (array));
1677 return require_complete_type (fold (rval));
1681 tree ar = default_conversion (array);
1682 tree ind = default_conversion (index);
1684 /* Do the same warning check as above, but only on the part that's
1685 syntactically the index and only if it is also semantically
1686 the index. */
1687 if (warn_char_subscripts
1688 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1689 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1690 warning ("subscript has type %<char%>");
1692 /* Put the integer in IND to simplify error checking. */
1693 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1695 tree temp = ar;
1696 ar = ind;
1697 ind = temp;
1700 if (ar == error_mark_node)
1701 return ar;
1703 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1704 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1706 error ("subscripted value is neither array nor pointer");
1707 return error_mark_node;
1709 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1711 error ("array subscript is not an integer");
1712 return error_mark_node;
1715 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1716 "array indexing");
1720 /* Build an external reference to identifier ID. FUN indicates
1721 whether this will be used for a function call. */
1722 tree
1723 build_external_ref (tree id, int fun)
1725 tree ref;
1726 tree decl = lookup_name (id);
1728 /* In Objective-C, an instance variable (ivar) may be preferred to
1729 whatever lookup_name() found. */
1730 decl = objc_lookup_ivar (decl, id);
1732 if (decl && decl != error_mark_node)
1733 ref = decl;
1734 else if (fun)
1735 /* Implicit function declaration. */
1736 ref = implicitly_declare (id);
1737 else if (decl == error_mark_node)
1738 /* Don't complain about something that's already been
1739 complained about. */
1740 return error_mark_node;
1741 else
1743 undeclared_variable (id);
1744 return error_mark_node;
1747 if (TREE_TYPE (ref) == error_mark_node)
1748 return error_mark_node;
1750 if (TREE_DEPRECATED (ref))
1751 warn_deprecated_use (ref);
1753 if (!skip_evaluation)
1754 assemble_external (ref);
1755 TREE_USED (ref) = 1;
1757 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1759 if (!in_sizeof && !in_typeof)
1760 C_DECL_USED (ref) = 1;
1761 else if (DECL_INITIAL (ref) == 0
1762 && DECL_EXTERNAL (ref)
1763 && !TREE_PUBLIC (ref))
1764 record_maybe_used_decl (ref);
1767 if (TREE_CODE (ref) == CONST_DECL)
1769 ref = DECL_INITIAL (ref);
1770 TREE_CONSTANT (ref) = 1;
1771 TREE_INVARIANT (ref) = 1;
1773 else if (current_function_decl != 0
1774 && !DECL_FILE_SCOPE_P (current_function_decl)
1775 && (TREE_CODE (ref) == VAR_DECL
1776 || TREE_CODE (ref) == PARM_DECL
1777 || TREE_CODE (ref) == FUNCTION_DECL))
1779 tree context = decl_function_context (ref);
1781 if (context != 0 && context != current_function_decl)
1782 DECL_NONLOCAL (ref) = 1;
1785 return ref;
1788 /* Record details of decls possibly used inside sizeof or typeof. */
1789 struct maybe_used_decl
1791 /* The decl. */
1792 tree decl;
1793 /* The level seen at (in_sizeof + in_typeof). */
1794 int level;
1795 /* The next one at this level or above, or NULL. */
1796 struct maybe_used_decl *next;
1799 static struct maybe_used_decl *maybe_used_decls;
1801 /* Record that DECL, an undefined static function reference seen
1802 inside sizeof or typeof, might be used if the operand of sizeof is
1803 a VLA type or the operand of typeof is a variably modified
1804 type. */
1806 void
1807 record_maybe_used_decl (tree decl)
1809 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1810 t->decl = decl;
1811 t->level = in_sizeof + in_typeof;
1812 t->next = maybe_used_decls;
1813 maybe_used_decls = t;
1816 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1817 USED is false, just discard them. If it is true, mark them used
1818 (if no longer inside sizeof or typeof) or move them to the next
1819 level up (if still inside sizeof or typeof). */
1821 void
1822 pop_maybe_used (bool used)
1824 struct maybe_used_decl *p = maybe_used_decls;
1825 int cur_level = in_sizeof + in_typeof;
1826 while (p && p->level > cur_level)
1828 if (used)
1830 if (cur_level == 0)
1831 C_DECL_USED (p->decl) = 1;
1832 else
1833 p->level = cur_level;
1835 p = p->next;
1837 if (!used || cur_level == 0)
1838 maybe_used_decls = p;
1841 /* Return the result of sizeof applied to EXPR. */
1843 struct c_expr
1844 c_expr_sizeof_expr (struct c_expr expr)
1846 struct c_expr ret;
1847 if (expr.value == error_mark_node)
1849 ret.value = error_mark_node;
1850 ret.original_code = ERROR_MARK;
1851 pop_maybe_used (false);
1853 else
1855 ret.value = c_sizeof (TREE_TYPE (expr.value));
1856 ret.original_code = ERROR_MARK;
1857 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1859 return ret;
1862 /* Return the result of sizeof applied to T, a structure for the type
1863 name passed to sizeof (rather than the type itself). */
1865 struct c_expr
1866 c_expr_sizeof_type (struct c_type_name *t)
1868 tree type;
1869 struct c_expr ret;
1870 type = groktypename (t);
1871 ret.value = c_sizeof (type);
1872 ret.original_code = ERROR_MARK;
1873 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1874 return ret;
1877 /* Build a function call to function FUNCTION with parameters PARAMS.
1878 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1879 TREE_VALUE of each node is a parameter-expression.
1880 FUNCTION's data type may be a function type or a pointer-to-function. */
1882 tree
1883 build_function_call (tree function, tree params)
1885 tree fntype, fundecl = 0;
1886 tree coerced_params;
1887 tree name = NULL_TREE, result;
1888 tree tem;
1890 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1891 STRIP_TYPE_NOPS (function);
1893 /* Convert anything with function type to a pointer-to-function. */
1894 if (TREE_CODE (function) == FUNCTION_DECL)
1896 name = DECL_NAME (function);
1898 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1899 (because calling an inline function does not mean the function
1900 needs to be separately compiled). */
1901 fntype = build_type_variant (TREE_TYPE (function),
1902 TREE_READONLY (function),
1903 TREE_THIS_VOLATILE (function));
1904 fundecl = function;
1905 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1907 else
1908 function = default_conversion (function);
1910 fntype = TREE_TYPE (function);
1912 if (TREE_CODE (fntype) == ERROR_MARK)
1913 return error_mark_node;
1915 if (!(TREE_CODE (fntype) == POINTER_TYPE
1916 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1918 error ("called object %qE is not a function", function);
1919 return error_mark_node;
1922 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1923 current_function_returns_abnormally = 1;
1925 /* fntype now gets the type of function pointed to. */
1926 fntype = TREE_TYPE (fntype);
1928 /* Check that the function is called through a compatible prototype.
1929 If it is not, replace the call by a trap, wrapped up in a compound
1930 expression if necessary. This has the nice side-effect to prevent
1931 the tree-inliner from generating invalid assignment trees which may
1932 blow up in the RTL expander later.
1934 ??? This doesn't work for Objective-C because objc_comptypes
1935 refuses to compare function prototypes, yet the compiler appears
1936 to build calls that are flagged as invalid by C's comptypes. */
1937 if (!c_dialect_objc ()
1938 && TREE_CODE (function) == NOP_EXPR
1939 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1940 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1941 && !comptypes (fntype, TREE_TYPE (tem)))
1943 tree return_type = TREE_TYPE (fntype);
1944 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1945 NULL_TREE);
1947 /* This situation leads to run-time undefined behavior. We can't,
1948 therefore, simply error unless we can prove that all possible
1949 executions of the program must execute the code. */
1950 warning ("function called through a non-compatible type");
1952 /* We can, however, treat "undefined" any way we please.
1953 Call abort to encourage the user to fix the program. */
1954 inform ("if this code is reached, the program will abort");
1956 if (VOID_TYPE_P (return_type))
1957 return trap;
1958 else
1960 tree rhs;
1962 if (AGGREGATE_TYPE_P (return_type))
1963 rhs = build_compound_literal (return_type,
1964 build_constructor (return_type,
1965 NULL_TREE));
1966 else
1967 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1969 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
1973 /* Convert the parameters to the types declared in the
1974 function prototype, or apply default promotions. */
1976 coerced_params
1977 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
1979 if (coerced_params == error_mark_node)
1980 return error_mark_node;
1982 /* Check that the arguments to the function are valid. */
1984 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1986 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
1987 function, coerced_params, NULL_TREE);
1988 TREE_SIDE_EFFECTS (result) = 1;
1990 if (require_constant_value)
1992 result = fold_initializer (result);
1994 if (TREE_CONSTANT (result)
1995 && (name == NULL_TREE
1996 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1997 pedwarn_init ("initializer element is not constant");
1999 else
2000 result = fold (result);
2002 if (VOID_TYPE_P (TREE_TYPE (result)))
2003 return result;
2004 return require_complete_type (result);
2007 /* Convert the argument expressions in the list VALUES
2008 to the types in the list TYPELIST. The result is a list of converted
2009 argument expressions, unless there are too few arguments in which
2010 case it is error_mark_node.
2012 If TYPELIST is exhausted, or when an element has NULL as its type,
2013 perform the default conversions.
2015 PARMLIST is the chain of parm decls for the function being called.
2016 It may be 0, if that info is not available.
2017 It is used only for generating error messages.
2019 FUNCTION is a tree for the called function. It is used only for
2020 error messages, where it is formatted with %qE.
2022 This is also where warnings about wrong number of args are generated.
2024 Both VALUES and the returned value are chains of TREE_LIST nodes
2025 with the elements of the list in the TREE_VALUE slots of those nodes. */
2027 static tree
2028 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2030 tree typetail, valtail;
2031 tree result = NULL;
2032 int parmnum;
2033 tree selector;
2035 /* Change pointer to function to the function itself for
2036 diagnostics. */
2037 if (TREE_CODE (function) == ADDR_EXPR
2038 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2039 function = TREE_OPERAND (function, 0);
2041 /* Handle an ObjC selector specially for diagnostics. */
2042 selector = objc_message_selector ();
2044 /* Scan the given expressions and types, producing individual
2045 converted arguments and pushing them on RESULT in reverse order. */
2047 for (valtail = values, typetail = typelist, parmnum = 0;
2048 valtail;
2049 valtail = TREE_CHAIN (valtail), parmnum++)
2051 tree type = typetail ? TREE_VALUE (typetail) : 0;
2052 tree val = TREE_VALUE (valtail);
2053 tree rname = function;
2054 int argnum = parmnum + 1;
2056 if (type == void_type_node)
2058 error ("too many arguments to function %qE", function);
2059 break;
2062 if (selector && argnum > 2)
2064 rname = selector;
2065 argnum -= 2;
2068 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2069 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2070 to convert automatically to a pointer. */
2071 if (TREE_CODE (val) == NON_LVALUE_EXPR)
2072 val = TREE_OPERAND (val, 0);
2074 val = default_function_array_conversion (val);
2076 val = require_complete_type (val);
2078 if (type != 0)
2080 /* Formal parm type is specified by a function prototype. */
2081 tree parmval;
2083 if (!COMPLETE_TYPE_P (type))
2085 error ("type of formal parameter %d is incomplete", parmnum + 1);
2086 parmval = val;
2088 else
2090 /* Optionally warn about conversions that
2091 differ from the default conversions. */
2092 if (warn_conversion || warn_traditional)
2094 unsigned int formal_prec = TYPE_PRECISION (type);
2096 if (INTEGRAL_TYPE_P (type)
2097 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2098 warning ("passing argument %d of %qE as integer "
2099 "rather than floating due to prototype",
2100 argnum, rname);
2101 if (INTEGRAL_TYPE_P (type)
2102 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2103 warning ("passing argument %d of %qE as integer "
2104 "rather than complex due to prototype",
2105 argnum, rname);
2106 else if (TREE_CODE (type) == COMPLEX_TYPE
2107 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2108 warning ("passing argument %d of %qE as complex "
2109 "rather than floating due to prototype",
2110 argnum, rname);
2111 else if (TREE_CODE (type) == REAL_TYPE
2112 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2113 warning ("passing argument %d of %qE as floating "
2114 "rather than integer due to prototype",
2115 argnum, rname);
2116 else if (TREE_CODE (type) == COMPLEX_TYPE
2117 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2118 warning ("passing argument %d of %qE as complex "
2119 "rather than integer due to prototype",
2120 argnum, rname);
2121 else if (TREE_CODE (type) == REAL_TYPE
2122 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2123 warning ("passing argument %d of %qE as floating "
2124 "rather than complex due to prototype",
2125 argnum, rname);
2126 /* ??? At some point, messages should be written about
2127 conversions between complex types, but that's too messy
2128 to do now. */
2129 else if (TREE_CODE (type) == REAL_TYPE
2130 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2132 /* Warn if any argument is passed as `float',
2133 since without a prototype it would be `double'. */
2134 if (formal_prec == TYPE_PRECISION (float_type_node))
2135 warning ("passing argument %d of %qE as %<float%> "
2136 "rather than %<double%> due to prototype",
2137 argnum, rname);
2139 /* Detect integer changing in width or signedness.
2140 These warnings are only activated with
2141 -Wconversion, not with -Wtraditional. */
2142 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2143 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2145 tree would_have_been = default_conversion (val);
2146 tree type1 = TREE_TYPE (would_have_been);
2148 if (TREE_CODE (type) == ENUMERAL_TYPE
2149 && (TYPE_MAIN_VARIANT (type)
2150 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2151 /* No warning if function asks for enum
2152 and the actual arg is that enum type. */
2154 else if (formal_prec != TYPE_PRECISION (type1))
2155 warning ("passing argument %d of %qE with different "
2156 "width due to prototype", argnum, rname);
2157 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2159 /* Don't complain if the formal parameter type
2160 is an enum, because we can't tell now whether
2161 the value was an enum--even the same enum. */
2162 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2164 else if (TREE_CODE (val) == INTEGER_CST
2165 && int_fits_type_p (val, type))
2166 /* Change in signedness doesn't matter
2167 if a constant value is unaffected. */
2169 /* Likewise for a constant in a NOP_EXPR. */
2170 else if (TREE_CODE (val) == NOP_EXPR
2171 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2172 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2174 /* If the value is extended from a narrower
2175 unsigned type, it doesn't matter whether we
2176 pass it as signed or unsigned; the value
2177 certainly is the same either way. */
2178 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2179 && TYPE_UNSIGNED (TREE_TYPE (val)))
2181 else if (TYPE_UNSIGNED (type))
2182 warning ("passing argument %d of %qE as unsigned "
2183 "due to prototype", argnum, rname);
2184 else
2185 warning ("passing argument %d of %qE as signed "
2186 "due to prototype", argnum, rname);
2190 parmval = convert_for_assignment (type, val, ic_argpass,
2191 fundecl, function,
2192 parmnum + 1);
2194 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2195 && INTEGRAL_TYPE_P (type)
2196 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2197 parmval = default_conversion (parmval);
2199 result = tree_cons (NULL_TREE, parmval, result);
2201 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2202 && (TYPE_PRECISION (TREE_TYPE (val))
2203 < TYPE_PRECISION (double_type_node)))
2204 /* Convert `float' to `double'. */
2205 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2206 else
2207 /* Convert `short' and `char' to full-size `int'. */
2208 result = tree_cons (NULL_TREE, default_conversion (val), result);
2210 if (typetail)
2211 typetail = TREE_CHAIN (typetail);
2214 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2216 error ("too few arguments to function %qE", function);
2217 return error_mark_node;
2220 return nreverse (result);
2223 /* This is the entry point used by the parser
2224 for binary operators in the input.
2225 In addition to constructing the expression,
2226 we check for operands that were written with other binary operators
2227 in a way that is likely to confuse the user. */
2229 struct c_expr
2230 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2231 struct c_expr arg2)
2233 struct c_expr result;
2235 enum tree_code code1 = arg1.original_code;
2236 enum tree_code code2 = arg2.original_code;
2238 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2239 result.original_code = code;
2241 if (TREE_CODE (result.value) == ERROR_MARK)
2242 return result;
2244 /* Check for cases such as x+y<<z which users are likely
2245 to misinterpret. */
2246 if (warn_parentheses)
2248 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2250 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2251 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2252 warning ("suggest parentheses around + or - inside shift");
2255 if (code == TRUTH_ORIF_EXPR)
2257 if (code1 == TRUTH_ANDIF_EXPR
2258 || code2 == TRUTH_ANDIF_EXPR)
2259 warning ("suggest parentheses around && within ||");
2262 if (code == BIT_IOR_EXPR)
2264 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2265 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2266 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2267 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2268 warning ("suggest parentheses around arithmetic in operand of |");
2269 /* Check cases like x|y==z */
2270 if (TREE_CODE_CLASS (code1) == tcc_comparison
2271 || TREE_CODE_CLASS (code2) == tcc_comparison)
2272 warning ("suggest parentheses around comparison in operand of |");
2275 if (code == BIT_XOR_EXPR)
2277 if (code1 == BIT_AND_EXPR
2278 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2279 || code2 == BIT_AND_EXPR
2280 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2281 warning ("suggest parentheses around arithmetic in operand of ^");
2282 /* Check cases like x^y==z */
2283 if (TREE_CODE_CLASS (code1) == tcc_comparison
2284 || TREE_CODE_CLASS (code2) == tcc_comparison)
2285 warning ("suggest parentheses around comparison in operand of ^");
2288 if (code == BIT_AND_EXPR)
2290 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2291 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2292 warning ("suggest parentheses around + or - in operand of &");
2293 /* Check cases like x&y==z */
2294 if (TREE_CODE_CLASS (code1) == tcc_comparison
2295 || TREE_CODE_CLASS (code2) == tcc_comparison)
2296 warning ("suggest parentheses around comparison in operand of &");
2298 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2299 if (TREE_CODE_CLASS (code) == tcc_comparison
2300 && (TREE_CODE_CLASS (code1) == tcc_comparison
2301 || TREE_CODE_CLASS (code2) == tcc_comparison))
2302 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2306 unsigned_conversion_warning (result.value, arg1.value);
2307 unsigned_conversion_warning (result.value, arg2.value);
2308 overflow_warning (result.value);
2310 return result;
2313 /* Return a tree for the difference of pointers OP0 and OP1.
2314 The resulting tree has type int. */
2316 static tree
2317 pointer_diff (tree op0, tree op1)
2319 tree restype = ptrdiff_type_node;
2321 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2322 tree con0, con1, lit0, lit1;
2323 tree orig_op1 = op1;
2325 if (pedantic || warn_pointer_arith)
2327 if (TREE_CODE (target_type) == VOID_TYPE)
2328 pedwarn ("pointer of type %<void *%> used in subtraction");
2329 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2330 pedwarn ("pointer to a function used in subtraction");
2333 /* If the conversion to ptrdiff_type does anything like widening or
2334 converting a partial to an integral mode, we get a convert_expression
2335 that is in the way to do any simplifications.
2336 (fold-const.c doesn't know that the extra bits won't be needed.
2337 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2338 different mode in place.)
2339 So first try to find a common term here 'by hand'; we want to cover
2340 at least the cases that occur in legal static initializers. */
2341 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2342 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2344 if (TREE_CODE (con0) == PLUS_EXPR)
2346 lit0 = TREE_OPERAND (con0, 1);
2347 con0 = TREE_OPERAND (con0, 0);
2349 else
2350 lit0 = integer_zero_node;
2352 if (TREE_CODE (con1) == PLUS_EXPR)
2354 lit1 = TREE_OPERAND (con1, 1);
2355 con1 = TREE_OPERAND (con1, 0);
2357 else
2358 lit1 = integer_zero_node;
2360 if (operand_equal_p (con0, con1, 0))
2362 op0 = lit0;
2363 op1 = lit1;
2367 /* First do the subtraction as integers;
2368 then drop through to build the divide operator.
2369 Do not do default conversions on the minus operator
2370 in case restype is a short type. */
2372 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2373 convert (restype, op1), 0);
2374 /* This generates an error if op1 is pointer to incomplete type. */
2375 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2376 error ("arithmetic on pointer to an incomplete type");
2378 /* This generates an error if op0 is pointer to incomplete type. */
2379 op1 = c_size_in_bytes (target_type);
2381 /* Divide by the size, in easiest possible way. */
2382 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2385 /* Construct and perhaps optimize a tree representation
2386 for a unary operation. CODE, a tree_code, specifies the operation
2387 and XARG is the operand.
2388 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2389 the default promotions (such as from short to int).
2390 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2391 allows non-lvalues; this is only used to handle conversion of non-lvalue
2392 arrays to pointers in C99. */
2394 tree
2395 build_unary_op (enum tree_code code, tree xarg, int flag)
2397 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2398 tree arg = xarg;
2399 tree argtype = 0;
2400 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2401 tree val;
2402 int noconvert = flag;
2404 if (typecode == ERROR_MARK)
2405 return error_mark_node;
2406 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2407 typecode = INTEGER_TYPE;
2409 switch (code)
2411 case CONVERT_EXPR:
2412 /* This is used for unary plus, because a CONVERT_EXPR
2413 is enough to prevent anybody from looking inside for
2414 associativity, but won't generate any code. */
2415 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2416 || typecode == COMPLEX_TYPE
2417 || typecode == VECTOR_TYPE))
2419 error ("wrong type argument to unary plus");
2420 return error_mark_node;
2422 else if (!noconvert)
2423 arg = default_conversion (arg);
2424 arg = non_lvalue (arg);
2425 break;
2427 case NEGATE_EXPR:
2428 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2429 || typecode == COMPLEX_TYPE
2430 || typecode == VECTOR_TYPE))
2432 error ("wrong type argument to unary minus");
2433 return error_mark_node;
2435 else if (!noconvert)
2436 arg = default_conversion (arg);
2437 break;
2439 case BIT_NOT_EXPR:
2440 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2442 if (!noconvert)
2443 arg = default_conversion (arg);
2445 else if (typecode == COMPLEX_TYPE)
2447 code = CONJ_EXPR;
2448 if (pedantic)
2449 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2450 if (!noconvert)
2451 arg = default_conversion (arg);
2453 else
2455 error ("wrong type argument to bit-complement");
2456 return error_mark_node;
2458 break;
2460 case ABS_EXPR:
2461 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2463 error ("wrong type argument to abs");
2464 return error_mark_node;
2466 else if (!noconvert)
2467 arg = default_conversion (arg);
2468 break;
2470 case CONJ_EXPR:
2471 /* Conjugating a real value is a no-op, but allow it anyway. */
2472 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2473 || typecode == COMPLEX_TYPE))
2475 error ("wrong type argument to conjugation");
2476 return error_mark_node;
2478 else if (!noconvert)
2479 arg = default_conversion (arg);
2480 break;
2482 case TRUTH_NOT_EXPR:
2483 if (typecode != INTEGER_TYPE
2484 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2485 && typecode != COMPLEX_TYPE
2486 /* These will convert to a pointer. */
2487 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2489 error ("wrong type argument to unary exclamation mark");
2490 return error_mark_node;
2492 arg = lang_hooks.truthvalue_conversion (arg);
2493 return invert_truthvalue (arg);
2495 case NOP_EXPR:
2496 break;
2498 case REALPART_EXPR:
2499 if (TREE_CODE (arg) == COMPLEX_CST)
2500 return TREE_REALPART (arg);
2501 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2502 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2503 else
2504 return arg;
2506 case IMAGPART_EXPR:
2507 if (TREE_CODE (arg) == COMPLEX_CST)
2508 return TREE_IMAGPART (arg);
2509 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2510 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2511 else
2512 return convert (TREE_TYPE (arg), integer_zero_node);
2514 case PREINCREMENT_EXPR:
2515 case POSTINCREMENT_EXPR:
2516 case PREDECREMENT_EXPR:
2517 case POSTDECREMENT_EXPR:
2519 /* Increment or decrement the real part of the value,
2520 and don't change the imaginary part. */
2521 if (typecode == COMPLEX_TYPE)
2523 tree real, imag;
2525 if (pedantic)
2526 pedwarn ("ISO C does not support %<++%> and %<--%>"
2527 " on complex types");
2529 arg = stabilize_reference (arg);
2530 real = build_unary_op (REALPART_EXPR, arg, 1);
2531 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2532 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2533 build_unary_op (code, real, 1), imag);
2536 /* Report invalid types. */
2538 if (typecode != POINTER_TYPE
2539 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2541 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2542 error ("wrong type argument to increment");
2543 else
2544 error ("wrong type argument to decrement");
2546 return error_mark_node;
2550 tree inc;
2551 tree result_type = TREE_TYPE (arg);
2553 arg = get_unwidened (arg, 0);
2554 argtype = TREE_TYPE (arg);
2556 /* Compute the increment. */
2558 if (typecode == POINTER_TYPE)
2560 /* If pointer target is an undefined struct,
2561 we just cannot know how to do the arithmetic. */
2562 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2564 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2565 error ("increment of pointer to unknown structure");
2566 else
2567 error ("decrement of pointer to unknown structure");
2569 else if ((pedantic || warn_pointer_arith)
2570 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2571 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2573 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2574 pedwarn ("wrong type argument to increment");
2575 else
2576 pedwarn ("wrong type argument to decrement");
2579 inc = c_size_in_bytes (TREE_TYPE (result_type));
2581 else
2582 inc = integer_one_node;
2584 inc = convert (argtype, inc);
2586 /* Complain about anything else that is not a true lvalue. */
2587 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2588 || code == POSTINCREMENT_EXPR)
2589 ? lv_increment
2590 : lv_decrement)))
2591 return error_mark_node;
2593 /* Report a read-only lvalue. */
2594 if (TREE_READONLY (arg))
2595 readonly_error (arg,
2596 ((code == PREINCREMENT_EXPR
2597 || code == POSTINCREMENT_EXPR)
2598 ? lv_increment : lv_decrement));
2600 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2601 val = boolean_increment (code, arg);
2602 else
2603 val = build2 (code, TREE_TYPE (arg), arg, inc);
2604 TREE_SIDE_EFFECTS (val) = 1;
2605 val = convert (result_type, val);
2606 if (TREE_CODE (val) != code)
2607 TREE_NO_WARNING (val) = 1;
2608 return val;
2611 case ADDR_EXPR:
2612 /* Note that this operation never does default_conversion. */
2614 /* Let &* cancel out to simplify resulting code. */
2615 if (TREE_CODE (arg) == INDIRECT_REF)
2617 /* Don't let this be an lvalue. */
2618 if (lvalue_p (TREE_OPERAND (arg, 0)))
2619 return non_lvalue (TREE_OPERAND (arg, 0));
2620 return TREE_OPERAND (arg, 0);
2623 /* For &x[y], return x+y */
2624 if (TREE_CODE (arg) == ARRAY_REF)
2626 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2627 return error_mark_node;
2628 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2629 TREE_OPERAND (arg, 1), 1);
2632 /* Anything not already handled and not a true memory reference
2633 or a non-lvalue array is an error. */
2634 else if (typecode != FUNCTION_TYPE && !flag
2635 && !lvalue_or_else (arg, lv_addressof))
2636 return error_mark_node;
2638 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2639 argtype = TREE_TYPE (arg);
2641 /* If the lvalue is const or volatile, merge that into the type
2642 to which the address will point. Note that you can't get a
2643 restricted pointer by taking the address of something, so we
2644 only have to deal with `const' and `volatile' here. */
2645 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2646 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2647 argtype = c_build_type_variant (argtype,
2648 TREE_READONLY (arg),
2649 TREE_THIS_VOLATILE (arg));
2651 if (!c_mark_addressable (arg))
2652 return error_mark_node;
2654 if (TREE_CODE (arg) == COMPONENT_REF
2655 && DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
2657 error ("attempt to take address of bit-field structure member %qD",
2658 TREE_OPERAND (arg, 1));
2659 return error_mark_node;
2662 argtype = build_pointer_type (argtype);
2664 /* ??? Cope with user tricks that amount to offsetof. Delete this
2665 when we have proper support for integer constant expressions. */
2666 val = get_base_address (arg);
2667 if (val && TREE_CODE (val) == INDIRECT_REF
2668 && integer_zerop (TREE_OPERAND (val, 0)))
2669 return fold_convert (argtype, fold_offsetof (arg));
2671 val = build1 (ADDR_EXPR, argtype, arg);
2673 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2674 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2676 return val;
2678 default:
2679 break;
2682 if (argtype == 0)
2683 argtype = TREE_TYPE (arg);
2684 val = build1 (code, argtype, arg);
2685 return require_constant_value ? fold_initializer (val) : fold (val);
2688 /* Return nonzero if REF is an lvalue valid for this language.
2689 Lvalues can be assigned, unless their type has TYPE_READONLY.
2690 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2693 lvalue_p (tree ref)
2695 enum tree_code code = TREE_CODE (ref);
2697 switch (code)
2699 case REALPART_EXPR:
2700 case IMAGPART_EXPR:
2701 case COMPONENT_REF:
2702 return lvalue_p (TREE_OPERAND (ref, 0));
2704 case COMPOUND_LITERAL_EXPR:
2705 case STRING_CST:
2706 return 1;
2708 case INDIRECT_REF:
2709 case ARRAY_REF:
2710 case VAR_DECL:
2711 case PARM_DECL:
2712 case RESULT_DECL:
2713 case ERROR_MARK:
2714 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2715 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2717 case BIND_EXPR:
2718 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2720 default:
2721 return 0;
2725 /* Return nonzero if REF is an lvalue valid for this language;
2726 otherwise, print an error message and return zero. USE says
2727 how the lvalue is being used and so selects the error message. */
2729 static int
2730 lvalue_or_else (tree ref, enum lvalue_use use)
2732 int win = lvalue_p (ref);
2734 if (!win)
2736 switch (use)
2738 case lv_assign:
2739 error ("invalid lvalue in assignment");
2740 break;
2741 case lv_increment:
2742 error ("invalid lvalue in increment");
2743 break;
2744 case lv_decrement:
2745 error ("invalid lvalue in decrement");
2746 break;
2747 case lv_addressof:
2748 error ("invalid lvalue in unary %<&%>");
2749 break;
2750 case lv_asm:
2751 error ("invalid lvalue in asm statement");
2752 break;
2753 default:
2754 gcc_unreachable ();
2758 return win;
2762 /* Give an error for storing in something that is 'const'. */
2764 static void
2765 readonly_error (tree arg, enum lvalue_use use)
2767 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2768 /* Using this macro rather than (for example) arrays of messages
2769 ensures that all the format strings are checked at compile
2770 time. */
2771 #define READONLY_MSG(A, I, D) (use == lv_assign \
2772 ? (A) \
2773 : (use == lv_increment ? (I) : (D)))
2774 if (TREE_CODE (arg) == COMPONENT_REF)
2776 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2777 readonly_error (TREE_OPERAND (arg, 0), use);
2778 else
2779 error (READONLY_MSG (N_("assignment of read-only member %qs"),
2780 N_("increment of read-only member %qs"),
2781 N_("decrement of read-only member %qs")),
2782 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2784 else if (TREE_CODE (arg) == VAR_DECL)
2785 error (READONLY_MSG (N_("assignment of read-only variable %qs"),
2786 N_("increment of read-only variable %qs"),
2787 N_("decrement of read-only variable %qs")),
2788 IDENTIFIER_POINTER (DECL_NAME (arg)));
2789 else
2790 error (READONLY_MSG (N_("assignment of read-only location"),
2791 N_("increment of read-only location"),
2792 N_("decrement of read-only location")));
2795 /* Mark EXP saying that we need to be able to take the
2796 address of it; it should not be allocated in a register.
2797 Returns true if successful. */
2799 bool
2800 c_mark_addressable (tree exp)
2802 tree x = exp;
2804 while (1)
2805 switch (TREE_CODE (x))
2807 case COMPONENT_REF:
2808 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2810 error
2811 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2812 return false;
2815 /* ... fall through ... */
2817 case ADDR_EXPR:
2818 case ARRAY_REF:
2819 case REALPART_EXPR:
2820 case IMAGPART_EXPR:
2821 x = TREE_OPERAND (x, 0);
2822 break;
2824 case COMPOUND_LITERAL_EXPR:
2825 case CONSTRUCTOR:
2826 TREE_ADDRESSABLE (x) = 1;
2827 return true;
2829 case VAR_DECL:
2830 case CONST_DECL:
2831 case PARM_DECL:
2832 case RESULT_DECL:
2833 if (C_DECL_REGISTER (x)
2834 && DECL_NONLOCAL (x))
2836 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2838 error
2839 ("global register variable %qD used in nested function", x);
2840 return false;
2842 pedwarn ("register variable %qD used in nested function", x);
2844 else if (C_DECL_REGISTER (x))
2846 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2847 error ("address of global register variable %qD requested", x);
2848 else
2849 error ("address of register variable %qD requested", x);
2850 return false;
2853 /* drops in */
2854 case FUNCTION_DECL:
2855 TREE_ADDRESSABLE (x) = 1;
2856 /* drops out */
2857 default:
2858 return true;
2862 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2864 tree
2865 build_conditional_expr (tree ifexp, tree op1, tree op2)
2867 tree type1;
2868 tree type2;
2869 enum tree_code code1;
2870 enum tree_code code2;
2871 tree result_type = NULL;
2872 tree orig_op1 = op1, orig_op2 = op2;
2874 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2876 /* Promote both alternatives. */
2878 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2879 op1 = default_conversion (op1);
2880 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2881 op2 = default_conversion (op2);
2883 if (TREE_CODE (ifexp) == ERROR_MARK
2884 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2885 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2886 return error_mark_node;
2888 type1 = TREE_TYPE (op1);
2889 code1 = TREE_CODE (type1);
2890 type2 = TREE_TYPE (op2);
2891 code2 = TREE_CODE (type2);
2893 /* C90 does not permit non-lvalue arrays in conditional expressions.
2894 In C99 they will be pointers by now. */
2895 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2897 error ("non-lvalue array in conditional expression");
2898 return error_mark_node;
2901 /* Quickly detect the usual case where op1 and op2 have the same type
2902 after promotion. */
2903 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2905 if (type1 == type2)
2906 result_type = type1;
2907 else
2908 result_type = TYPE_MAIN_VARIANT (type1);
2910 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2911 || code1 == COMPLEX_TYPE)
2912 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2913 || code2 == COMPLEX_TYPE))
2915 result_type = common_type (type1, type2);
2917 /* If -Wsign-compare, warn here if type1 and type2 have
2918 different signedness. We'll promote the signed to unsigned
2919 and later code won't know it used to be different.
2920 Do this check on the original types, so that explicit casts
2921 will be considered, but default promotions won't. */
2922 if (warn_sign_compare && !skip_evaluation)
2924 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2925 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2927 if (unsigned_op1 ^ unsigned_op2)
2929 /* Do not warn if the result type is signed, since the
2930 signed type will only be chosen if it can represent
2931 all the values of the unsigned type. */
2932 if (!TYPE_UNSIGNED (result_type))
2933 /* OK */;
2934 /* Do not warn if the signed quantity is an unsuffixed
2935 integer literal (or some static constant expression
2936 involving such literals) and it is non-negative. */
2937 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2938 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2939 /* OK */;
2940 else
2941 warning ("signed and unsigned type in conditional expression");
2945 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2947 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2948 pedwarn ("ISO C forbids conditional expr with only one void side");
2949 result_type = void_type_node;
2951 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2953 if (comp_target_types (type1, type2, 1))
2954 result_type = common_pointer_type (type1, type2);
2955 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2956 && TREE_CODE (orig_op1) != NOP_EXPR)
2957 result_type = qualify_type (type2, type1);
2958 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2959 && TREE_CODE (orig_op2) != NOP_EXPR)
2960 result_type = qualify_type (type1, type2);
2961 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2963 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2964 pedwarn ("ISO C forbids conditional expr between "
2965 "%<void *%> and function pointer");
2966 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2967 TREE_TYPE (type2)));
2969 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2971 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2972 pedwarn ("ISO C forbids conditional expr between "
2973 "%<void *%> and function pointer");
2974 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2975 TREE_TYPE (type1)));
2977 else
2979 pedwarn ("pointer type mismatch in conditional expression");
2980 result_type = build_pointer_type (void_type_node);
2983 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2985 if (!integer_zerop (op2))
2986 pedwarn ("pointer/integer type mismatch in conditional expression");
2987 else
2989 op2 = null_pointer_node;
2991 result_type = type1;
2993 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2995 if (!integer_zerop (op1))
2996 pedwarn ("pointer/integer type mismatch in conditional expression");
2997 else
2999 op1 = null_pointer_node;
3001 result_type = type2;
3004 if (!result_type)
3006 if (flag_cond_mismatch)
3007 result_type = void_type_node;
3008 else
3010 error ("type mismatch in conditional expression");
3011 return error_mark_node;
3015 /* Merge const and volatile flags of the incoming types. */
3016 result_type
3017 = build_type_variant (result_type,
3018 TREE_READONLY (op1) || TREE_READONLY (op2),
3019 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3021 if (result_type != TREE_TYPE (op1))
3022 op1 = convert_and_check (result_type, op1);
3023 if (result_type != TREE_TYPE (op2))
3024 op2 = convert_and_check (result_type, op2);
3026 if (TREE_CODE (ifexp) == INTEGER_CST)
3027 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3029 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3032 /* Return a compound expression that performs two expressions and
3033 returns the value of the second of them. */
3035 tree
3036 build_compound_expr (tree expr1, tree expr2)
3038 /* Convert arrays and functions to pointers. */
3039 expr2 = default_function_array_conversion (expr2);
3041 if (!TREE_SIDE_EFFECTS (expr1))
3043 /* The left-hand operand of a comma expression is like an expression
3044 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3045 any side-effects, unless it was explicitly cast to (void). */
3046 if (warn_unused_value
3047 && !(TREE_CODE (expr1) == CONVERT_EXPR
3048 && VOID_TYPE_P (TREE_TYPE (expr1))))
3049 warning ("left-hand operand of comma expression has no effect");
3052 /* With -Wunused, we should also warn if the left-hand operand does have
3053 side-effects, but computes a value which is not used. For example, in
3054 `foo() + bar(), baz()' the result of the `+' operator is not used,
3055 so we should issue a warning. */
3056 else if (warn_unused_value)
3057 warn_if_unused_value (expr1, input_location);
3059 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3062 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3064 tree
3065 build_c_cast (tree type, tree expr)
3067 tree value = expr;
3069 if (type == error_mark_node || expr == error_mark_node)
3070 return error_mark_node;
3072 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3073 only in <protocol> qualifications. But when constructing cast expressions,
3074 the protocols do matter and must be kept around. */
3075 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3076 return build1 (NOP_EXPR, type, expr);
3078 type = TYPE_MAIN_VARIANT (type);
3080 if (TREE_CODE (type) == ARRAY_TYPE)
3082 error ("cast specifies array type");
3083 return error_mark_node;
3086 if (TREE_CODE (type) == FUNCTION_TYPE)
3088 error ("cast specifies function type");
3089 return error_mark_node;
3092 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3094 if (pedantic)
3096 if (TREE_CODE (type) == RECORD_TYPE
3097 || TREE_CODE (type) == UNION_TYPE)
3098 pedwarn ("ISO C forbids casting nonscalar to the same type");
3101 else if (TREE_CODE (type) == UNION_TYPE)
3103 tree field;
3104 value = default_function_array_conversion (value);
3106 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3107 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3108 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3109 break;
3111 if (field)
3113 tree t;
3115 if (pedantic)
3116 pedwarn ("ISO C forbids casts to union type");
3117 t = digest_init (type,
3118 build_constructor (type,
3119 build_tree_list (field, value)),
3120 true, 0);
3121 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3122 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3123 return t;
3125 error ("cast to union type from type not present in union");
3126 return error_mark_node;
3128 else
3130 tree otype, ovalue;
3132 /* If casting to void, avoid the error that would come
3133 from default_conversion in the case of a non-lvalue array. */
3134 if (type == void_type_node)
3135 return build1 (CONVERT_EXPR, type, value);
3137 /* Convert functions and arrays to pointers,
3138 but don't convert any other types. */
3139 value = default_function_array_conversion (value);
3140 otype = TREE_TYPE (value);
3142 /* Optionally warn about potentially worrisome casts. */
3144 if (warn_cast_qual
3145 && TREE_CODE (type) == POINTER_TYPE
3146 && TREE_CODE (otype) == POINTER_TYPE)
3148 tree in_type = type;
3149 tree in_otype = otype;
3150 int added = 0;
3151 int discarded = 0;
3153 /* Check that the qualifiers on IN_TYPE are a superset of
3154 the qualifiers of IN_OTYPE. The outermost level of
3155 POINTER_TYPE nodes is uninteresting and we stop as soon
3156 as we hit a non-POINTER_TYPE node on either type. */
3159 in_otype = TREE_TYPE (in_otype);
3160 in_type = TREE_TYPE (in_type);
3162 /* GNU C allows cv-qualified function types. 'const'
3163 means the function is very pure, 'volatile' means it
3164 can't return. We need to warn when such qualifiers
3165 are added, not when they're taken away. */
3166 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3167 && TREE_CODE (in_type) == FUNCTION_TYPE)
3168 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3169 else
3170 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3172 while (TREE_CODE (in_type) == POINTER_TYPE
3173 && TREE_CODE (in_otype) == POINTER_TYPE);
3175 if (added)
3176 warning ("cast adds new qualifiers to function type");
3178 if (discarded)
3179 /* There are qualifiers present in IN_OTYPE that are not
3180 present in IN_TYPE. */
3181 warning ("cast discards qualifiers from pointer target type");
3184 /* Warn about possible alignment problems. */
3185 if (STRICT_ALIGNMENT && warn_cast_align
3186 && TREE_CODE (type) == POINTER_TYPE
3187 && TREE_CODE (otype) == POINTER_TYPE
3188 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3189 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3190 /* Don't warn about opaque types, where the actual alignment
3191 restriction is unknown. */
3192 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3193 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3194 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3195 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3196 warning ("cast increases required alignment of target type");
3198 if (TREE_CODE (type) == INTEGER_TYPE
3199 && TREE_CODE (otype) == POINTER_TYPE
3200 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3201 && !TREE_CONSTANT (value))
3202 warning ("cast from pointer to integer of different size");
3204 if (warn_bad_function_cast
3205 && TREE_CODE (value) == CALL_EXPR
3206 && TREE_CODE (type) != TREE_CODE (otype))
3207 warning ("cast from function call of type %qT to non-matching "
3208 "type %qT", otype, type);
3210 if (TREE_CODE (type) == POINTER_TYPE
3211 && TREE_CODE (otype) == INTEGER_TYPE
3212 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3213 /* Don't warn about converting any constant. */
3214 && !TREE_CONSTANT (value))
3215 warning ("cast to pointer from integer of different size");
3217 if (TREE_CODE (type) == POINTER_TYPE
3218 && TREE_CODE (otype) == POINTER_TYPE
3219 && TREE_CODE (expr) == ADDR_EXPR
3220 && DECL_P (TREE_OPERAND (expr, 0))
3221 && flag_strict_aliasing && warn_strict_aliasing
3222 && !VOID_TYPE_P (TREE_TYPE (type)))
3224 /* Casting the address of a decl to non void pointer. Warn
3225 if the cast breaks type based aliasing. */
3226 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3227 warning ("type-punning to incomplete type might break strict-aliasing rules");
3228 else
3230 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3231 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3233 if (!alias_sets_conflict_p (set1, set2))
3234 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3235 else if (warn_strict_aliasing > 1
3236 && !alias_sets_might_conflict_p (set1, set2))
3237 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3241 /* If pedantic, warn for conversions between function and object
3242 pointer types, except for converting a null pointer constant
3243 to function pointer type. */
3244 if (pedantic
3245 && TREE_CODE (type) == POINTER_TYPE
3246 && TREE_CODE (otype) == POINTER_TYPE
3247 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3248 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3249 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3251 if (pedantic
3252 && TREE_CODE (type) == POINTER_TYPE
3253 && TREE_CODE (otype) == POINTER_TYPE
3254 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3255 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3256 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3257 && TREE_CODE (expr) != NOP_EXPR))
3258 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3260 ovalue = value;
3261 /* Replace a nonvolatile const static variable with its value. */
3262 if (optimize && TREE_CODE (value) == VAR_DECL)
3263 value = decl_constant_value (value);
3264 value = convert (type, value);
3266 /* Ignore any integer overflow caused by the cast. */
3267 if (TREE_CODE (value) == INTEGER_CST)
3269 if (EXPR_P (ovalue))
3270 /* If OVALUE had overflow set, then so will VALUE, so it
3271 is safe to overwrite. */
3272 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3273 else
3274 TREE_OVERFLOW (value) = 0;
3276 if (CONSTANT_CLASS_P (ovalue))
3277 /* Similarly, constant_overflow cannot have become
3278 cleared. */
3279 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3283 /* Don't let a cast be an lvalue. */
3284 if (value == expr)
3285 value = non_lvalue (value);
3287 return value;
3290 /* Interpret a cast of expression EXPR to type TYPE. */
3291 tree
3292 c_cast_expr (struct c_type_name *type_name, tree expr)
3294 tree type;
3295 int saved_wsp = warn_strict_prototypes;
3297 /* This avoids warnings about unprototyped casts on
3298 integers. E.g. "#define SIG_DFL (void(*)())0". */
3299 if (TREE_CODE (expr) == INTEGER_CST)
3300 warn_strict_prototypes = 0;
3301 type = groktypename (type_name);
3302 warn_strict_prototypes = saved_wsp;
3304 return build_c_cast (type, expr);
3308 /* Build an assignment expression of lvalue LHS from value RHS.
3309 MODIFYCODE is the code for a binary operator that we use
3310 to combine the old value of LHS with RHS to get the new value.
3311 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3313 tree
3314 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3316 tree result;
3317 tree newrhs;
3318 tree lhstype = TREE_TYPE (lhs);
3319 tree olhstype = lhstype;
3321 /* Types that aren't fully specified cannot be used in assignments. */
3322 lhs = require_complete_type (lhs);
3324 /* Avoid duplicate error messages from operands that had errors. */
3325 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3326 return error_mark_node;
3328 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3329 /* Do not use STRIP_NOPS here. We do not want an enumerator
3330 whose value is 0 to count as a null pointer constant. */
3331 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3332 rhs = TREE_OPERAND (rhs, 0);
3334 newrhs = rhs;
3336 /* If a binary op has been requested, combine the old LHS value with the RHS
3337 producing the value we should actually store into the LHS. */
3339 if (modifycode != NOP_EXPR)
3341 lhs = stabilize_reference (lhs);
3342 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3345 if (!lvalue_or_else (lhs, lv_assign))
3346 return error_mark_node;
3348 /* Give an error for storing in something that is 'const'. */
3350 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3351 || ((TREE_CODE (lhstype) == RECORD_TYPE
3352 || TREE_CODE (lhstype) == UNION_TYPE)
3353 && C_TYPE_FIELDS_READONLY (lhstype)))
3354 readonly_error (lhs, lv_assign);
3356 /* If storing into a structure or union member,
3357 it has probably been given type `int'.
3358 Compute the type that would go with
3359 the actual amount of storage the member occupies. */
3361 if (TREE_CODE (lhs) == COMPONENT_REF
3362 && (TREE_CODE (lhstype) == INTEGER_TYPE
3363 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3364 || TREE_CODE (lhstype) == REAL_TYPE
3365 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3366 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3368 /* If storing in a field that is in actuality a short or narrower than one,
3369 we must store in the field in its actual type. */
3371 if (lhstype != TREE_TYPE (lhs))
3373 lhs = copy_node (lhs);
3374 TREE_TYPE (lhs) = lhstype;
3377 /* Convert new value to destination type. */
3379 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3380 NULL_TREE, NULL_TREE, 0);
3381 if (TREE_CODE (newrhs) == ERROR_MARK)
3382 return error_mark_node;
3384 /* Scan operands. */
3386 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3387 TREE_SIDE_EFFECTS (result) = 1;
3389 /* If we got the LHS in a different type for storing in,
3390 convert the result back to the nominal type of LHS
3391 so that the value we return always has the same type
3392 as the LHS argument. */
3394 if (olhstype == TREE_TYPE (result))
3395 return result;
3396 return convert_for_assignment (olhstype, result, ic_assign,
3397 NULL_TREE, NULL_TREE, 0);
3400 /* Convert value RHS to type TYPE as preparation for an assignment
3401 to an lvalue of type TYPE.
3402 The real work of conversion is done by `convert'.
3403 The purpose of this function is to generate error messages
3404 for assignments that are not allowed in C.
3405 ERRTYPE says whether it is argument passing, assignment,
3406 initialization or return.
3408 FUNCTION is a tree for the function being called.
3409 PARMNUM is the number of the argument, for printing in error messages. */
3411 static tree
3412 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3413 tree fundecl, tree function, int parmnum)
3415 enum tree_code codel = TREE_CODE (type);
3416 tree rhstype;
3417 enum tree_code coder;
3418 tree rname = NULL_TREE;
3420 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3422 tree selector;
3423 /* Change pointer to function to the function itself for
3424 diagnostics. */
3425 if (TREE_CODE (function) == ADDR_EXPR
3426 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3427 function = TREE_OPERAND (function, 0);
3429 /* Handle an ObjC selector specially for diagnostics. */
3430 selector = objc_message_selector ();
3431 rname = function;
3432 if (selector && parmnum > 2)
3434 rname = selector;
3435 parmnum -= 2;
3439 /* This macro is used to emit diagnostics to ensure that all format
3440 strings are complete sentences, visible to gettext and checked at
3441 compile time. */
3442 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3443 do { \
3444 switch (errtype) \
3446 case ic_argpass: \
3447 pedwarn (AR, parmnum, rname); \
3448 break; \
3449 case ic_argpass_nonproto: \
3450 warning (AR, parmnum, rname); \
3451 break; \
3452 case ic_assign: \
3453 pedwarn (AS); \
3454 break; \
3455 case ic_init: \
3456 pedwarn (IN); \
3457 break; \
3458 case ic_return: \
3459 pedwarn (RE); \
3460 break; \
3461 default: \
3462 gcc_unreachable (); \
3464 } while (0)
3466 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3467 /* Do not use STRIP_NOPS here. We do not want an enumerator
3468 whose value is 0 to count as a null pointer constant. */
3469 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3470 rhs = TREE_OPERAND (rhs, 0);
3472 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3473 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3474 rhs = default_conversion (rhs);
3475 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3476 rhs = decl_constant_value_for_broken_optimization (rhs);
3478 rhstype = TREE_TYPE (rhs);
3479 coder = TREE_CODE (rhstype);
3481 if (coder == ERROR_MARK)
3482 return error_mark_node;
3484 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3486 overflow_warning (rhs);
3487 /* Check for Objective-C protocols. This will automatically
3488 issue a warning if there are protocol violations. No need to
3489 use the return value. */
3490 if (c_dialect_objc ())
3491 objc_comptypes (type, rhstype, 0);
3492 return rhs;
3495 if (coder == VOID_TYPE)
3497 /* Except for passing an argument to an unprototyped function,
3498 this is a constraint violation. When passing an argument to
3499 an unprototyped function, it is compile-time undefined;
3500 making it a constraint in that case was rejected in
3501 DR#252. */
3502 error ("void value not ignored as it ought to be");
3503 return error_mark_node;
3505 /* A type converts to a reference to it.
3506 This code doesn't fully support references, it's just for the
3507 special case of va_start and va_copy. */
3508 if (codel == REFERENCE_TYPE
3509 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3511 if (!lvalue_p (rhs))
3513 error ("cannot pass rvalue to reference parameter");
3514 return error_mark_node;
3516 if (!c_mark_addressable (rhs))
3517 return error_mark_node;
3518 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3520 /* We already know that these two types are compatible, but they
3521 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3522 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3523 likely to be va_list, a typedef to __builtin_va_list, which
3524 is different enough that it will cause problems later. */
3525 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3526 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3528 rhs = build1 (NOP_EXPR, type, rhs);
3529 return rhs;
3531 /* Some types can interconvert without explicit casts. */
3532 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3533 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3534 return convert (type, rhs);
3535 /* Arithmetic types all interconvert, and enum is treated like int. */
3536 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3537 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3538 || codel == BOOLEAN_TYPE)
3539 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3540 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3541 || coder == BOOLEAN_TYPE))
3542 return convert_and_check (type, rhs);
3544 /* Conversion to a transparent union from its member types.
3545 This applies only to function arguments. */
3546 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3547 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3549 tree memb_types;
3550 tree marginal_memb_type = 0;
3552 for (memb_types = TYPE_FIELDS (type); memb_types;
3553 memb_types = TREE_CHAIN (memb_types))
3555 tree memb_type = TREE_TYPE (memb_types);
3557 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3558 TYPE_MAIN_VARIANT (rhstype)))
3559 break;
3561 if (TREE_CODE (memb_type) != POINTER_TYPE)
3562 continue;
3564 if (coder == POINTER_TYPE)
3566 tree ttl = TREE_TYPE (memb_type);
3567 tree ttr = TREE_TYPE (rhstype);
3569 /* Any non-function converts to a [const][volatile] void *
3570 and vice versa; otherwise, targets must be the same.
3571 Meanwhile, the lhs target must have all the qualifiers of
3572 the rhs. */
3573 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3574 || comp_target_types (memb_type, rhstype, 0))
3576 /* If this type won't generate any warnings, use it. */
3577 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3578 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3579 && TREE_CODE (ttl) == FUNCTION_TYPE)
3580 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3581 == TYPE_QUALS (ttr))
3582 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3583 == TYPE_QUALS (ttl))))
3584 break;
3586 /* Keep looking for a better type, but remember this one. */
3587 if (!marginal_memb_type)
3588 marginal_memb_type = memb_type;
3592 /* Can convert integer zero to any pointer type. */
3593 if (integer_zerop (rhs)
3594 || (TREE_CODE (rhs) == NOP_EXPR
3595 && integer_zerop (TREE_OPERAND (rhs, 0))))
3597 rhs = null_pointer_node;
3598 break;
3602 if (memb_types || marginal_memb_type)
3604 if (!memb_types)
3606 /* We have only a marginally acceptable member type;
3607 it needs a warning. */
3608 tree ttl = TREE_TYPE (marginal_memb_type);
3609 tree ttr = TREE_TYPE (rhstype);
3611 /* Const and volatile mean something different for function
3612 types, so the usual warnings are not appropriate. */
3613 if (TREE_CODE (ttr) == FUNCTION_TYPE
3614 && TREE_CODE (ttl) == FUNCTION_TYPE)
3616 /* Because const and volatile on functions are
3617 restrictions that say the function will not do
3618 certain things, it is okay to use a const or volatile
3619 function where an ordinary one is wanted, but not
3620 vice-versa. */
3621 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3622 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3623 "makes qualified function "
3624 "pointer from unqualified"),
3625 N_("assignment makes qualified "
3626 "function pointer from "
3627 "unqualified"),
3628 N_("initialization makes qualified "
3629 "function pointer from "
3630 "unqualified"),
3631 N_("return makes qualified function "
3632 "pointer from unqualified"));
3634 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3635 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3636 "qualifiers from pointer target type"),
3637 N_("assignment discards qualifiers "
3638 "from pointer target type"),
3639 N_("initialization discards qualifiers "
3640 "from pointer target type"),
3641 N_("return discards qualifiers from "
3642 "pointer target type"));
3645 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3646 pedwarn ("ISO C prohibits argument conversion to union type");
3648 return build1 (NOP_EXPR, type, rhs);
3652 /* Conversions among pointers */
3653 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3654 && (coder == codel))
3656 tree ttl = TREE_TYPE (type);
3657 tree ttr = TREE_TYPE (rhstype);
3658 bool is_opaque_pointer;
3659 int target_cmp = 0; /* Cache comp_target_types () result. */
3661 /* Opaque pointers are treated like void pointers. */
3662 is_opaque_pointer = (targetm.vector_opaque_p (type)
3663 || targetm.vector_opaque_p (rhstype))
3664 && TREE_CODE (ttl) == VECTOR_TYPE
3665 && TREE_CODE (ttr) == VECTOR_TYPE;
3667 /* Any non-function converts to a [const][volatile] void *
3668 and vice versa; otherwise, targets must be the same.
3669 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3670 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3671 || (target_cmp = comp_target_types (type, rhstype, 0))
3672 || is_opaque_pointer
3673 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3674 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3676 if (pedantic
3677 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3679 (VOID_TYPE_P (ttr)
3680 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3681 which are not ANSI null ptr constants. */
3682 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3683 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3684 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3685 "%qE between function pointer "
3686 "and %<void *%>"),
3687 N_("ISO C forbids assignment between "
3688 "function pointer and %<void *%>"),
3689 N_("ISO C forbids initialization between "
3690 "function pointer and %<void *%>"),
3691 N_("ISO C forbids return between function "
3692 "pointer and %<void *%>"));
3693 /* Const and volatile mean something different for function types,
3694 so the usual warnings are not appropriate. */
3695 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3696 && TREE_CODE (ttl) != FUNCTION_TYPE)
3698 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3699 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3700 "qualifiers from pointer target type"),
3701 N_("assignment discards qualifiers "
3702 "from pointer target type"),
3703 N_("initialization discards qualifiers "
3704 "from pointer target type"),
3705 N_("return discards qualifiers from "
3706 "pointer target type"));
3707 /* If this is not a case of ignoring a mismatch in signedness,
3708 no warning. */
3709 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3710 || target_cmp)
3712 /* If there is a mismatch, do warn. */
3713 else
3714 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3715 "%d of %qE differ in signedness"),
3716 N_("pointer targets in assignment "
3717 "differ in signedness"),
3718 N_("pointer targets in initialization "
3719 "differ in signedness"),
3720 N_("pointer targets in return differ "
3721 "in signedness"));
3723 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3724 && TREE_CODE (ttr) == FUNCTION_TYPE)
3726 /* Because const and volatile on functions are restrictions
3727 that say the function will not do certain things,
3728 it is okay to use a const or volatile function
3729 where an ordinary one is wanted, but not vice-versa. */
3730 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3731 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3732 "qualified function pointer "
3733 "from unqualified"),
3734 N_("assignment makes qualified function "
3735 "pointer from unqualified"),
3736 N_("initialization makes qualified "
3737 "function pointer from unqualified"),
3738 N_("return makes qualified function "
3739 "pointer from unqualified"));
3742 else
3743 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3744 "incompatible pointer type"),
3745 N_("assignment from incompatible pointer type"),
3746 N_("initialization from incompatible "
3747 "pointer type"),
3748 N_("return from incompatible pointer type"));
3749 return convert (type, rhs);
3751 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3753 /* ??? This should not be an error when inlining calls to
3754 unprototyped functions. */
3755 error ("invalid use of non-lvalue array");
3756 return error_mark_node;
3758 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3760 /* An explicit constant 0 can convert to a pointer,
3761 or one that results from arithmetic, even including
3762 a cast to integer type. */
3763 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3765 !(TREE_CODE (rhs) == NOP_EXPR
3766 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3767 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3768 && integer_zerop (TREE_OPERAND (rhs, 0))))
3769 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3770 "pointer from integer without a cast"),
3771 N_("assignment makes pointer from integer "
3772 "without a cast"),
3773 N_("initialization makes pointer from "
3774 "integer without a cast"),
3775 N_("return makes pointer from integer "
3776 "without a cast"));
3778 return convert (type, rhs);
3780 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3782 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3783 "from pointer without a cast"),
3784 N_("assignment makes integer from pointer "
3785 "without a cast"),
3786 N_("initialization makes integer from pointer "
3787 "without a cast"),
3788 N_("return makes integer from pointer "
3789 "without a cast"));
3790 return convert (type, rhs);
3792 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3793 return convert (type, rhs);
3795 switch (errtype)
3797 case ic_argpass:
3798 case ic_argpass_nonproto:
3799 /* ??? This should not be an error when inlining calls to
3800 unprototyped functions. */
3801 error ("incompatible type for argument %d of %qE", parmnum, rname);
3802 break;
3803 case ic_assign:
3804 error ("incompatible types in assignment");
3805 break;
3806 case ic_init:
3807 error ("incompatible types in initialization");
3808 break;
3809 case ic_return:
3810 error ("incompatible types in return");
3811 break;
3812 default:
3813 gcc_unreachable ();
3816 return error_mark_node;
3819 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3820 is used for error and waring reporting and indicates which argument
3821 is being processed. */
3823 tree
3824 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3826 tree ret, type;
3828 /* If FN was prototyped, the value has been converted already
3829 in convert_arguments. */
3830 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3831 return value;
3833 type = TREE_TYPE (parm);
3834 ret = convert_for_assignment (type, value,
3835 ic_argpass_nonproto, fn,
3836 fn, argnum);
3837 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3838 && INTEGRAL_TYPE_P (type)
3839 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3840 ret = default_conversion (ret);
3841 return ret;
3844 /* If VALUE is a compound expr all of whose expressions are constant, then
3845 return its value. Otherwise, return error_mark_node.
3847 This is for handling COMPOUND_EXPRs as initializer elements
3848 which is allowed with a warning when -pedantic is specified. */
3850 static tree
3851 valid_compound_expr_initializer (tree value, tree endtype)
3853 if (TREE_CODE (value) == COMPOUND_EXPR)
3855 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3856 == error_mark_node)
3857 return error_mark_node;
3858 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3859 endtype);
3861 else if (!initializer_constant_valid_p (value, endtype))
3862 return error_mark_node;
3863 else
3864 return value;
3867 /* Perform appropriate conversions on the initial value of a variable,
3868 store it in the declaration DECL,
3869 and print any error messages that are appropriate.
3870 If the init is invalid, store an ERROR_MARK. */
3872 void
3873 store_init_value (tree decl, tree init)
3875 tree value, type;
3877 /* If variable's type was invalidly declared, just ignore it. */
3879 type = TREE_TYPE (decl);
3880 if (TREE_CODE (type) == ERROR_MARK)
3881 return;
3883 /* Digest the specified initializer into an expression. */
3885 value = digest_init (type, init, true, TREE_STATIC (decl));
3887 /* Store the expression if valid; else report error. */
3889 if (warn_traditional && !in_system_header
3890 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3891 warning ("traditional C rejects automatic aggregate initialization");
3893 DECL_INITIAL (decl) = value;
3895 /* ANSI wants warnings about out-of-range constant initializers. */
3896 STRIP_TYPE_NOPS (value);
3897 constant_expression_warning (value);
3899 /* Check if we need to set array size from compound literal size. */
3900 if (TREE_CODE (type) == ARRAY_TYPE
3901 && TYPE_DOMAIN (type) == 0
3902 && value != error_mark_node)
3904 tree inside_init = init;
3906 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3907 inside_init = TREE_OPERAND (init, 0);
3908 inside_init = fold (inside_init);
3910 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3912 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3914 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3916 /* For int foo[] = (int [3]){1}; we need to set array size
3917 now since later on array initializer will be just the
3918 brace enclosed list of the compound literal. */
3919 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3920 layout_type (type);
3921 layout_decl (decl, 0);
3927 /* Methods for storing and printing names for error messages. */
3929 /* Implement a spelling stack that allows components of a name to be pushed
3930 and popped. Each element on the stack is this structure. */
3932 struct spelling
3934 int kind;
3935 union
3937 int i;
3938 const char *s;
3939 } u;
3942 #define SPELLING_STRING 1
3943 #define SPELLING_MEMBER 2
3944 #define SPELLING_BOUNDS 3
3946 static struct spelling *spelling; /* Next stack element (unused). */
3947 static struct spelling *spelling_base; /* Spelling stack base. */
3948 static int spelling_size; /* Size of the spelling stack. */
3950 /* Macros to save and restore the spelling stack around push_... functions.
3951 Alternative to SAVE_SPELLING_STACK. */
3953 #define SPELLING_DEPTH() (spelling - spelling_base)
3954 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3956 /* Push an element on the spelling stack with type KIND and assign VALUE
3957 to MEMBER. */
3959 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3961 int depth = SPELLING_DEPTH (); \
3963 if (depth >= spelling_size) \
3965 spelling_size += 10; \
3966 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3967 spelling_size); \
3968 RESTORE_SPELLING_DEPTH (depth); \
3971 spelling->kind = (KIND); \
3972 spelling->MEMBER = (VALUE); \
3973 spelling++; \
3976 /* Push STRING on the stack. Printed literally. */
3978 static void
3979 push_string (const char *string)
3981 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3984 /* Push a member name on the stack. Printed as '.' STRING. */
3986 static void
3987 push_member_name (tree decl)
3989 const char *const string
3990 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3991 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3994 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3996 static void
3997 push_array_bounds (int bounds)
3999 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4002 /* Compute the maximum size in bytes of the printed spelling. */
4004 static int
4005 spelling_length (void)
4007 int size = 0;
4008 struct spelling *p;
4010 for (p = spelling_base; p < spelling; p++)
4012 if (p->kind == SPELLING_BOUNDS)
4013 size += 25;
4014 else
4015 size += strlen (p->u.s) + 1;
4018 return size;
4021 /* Print the spelling to BUFFER and return it. */
4023 static char *
4024 print_spelling (char *buffer)
4026 char *d = buffer;
4027 struct spelling *p;
4029 for (p = spelling_base; p < spelling; p++)
4030 if (p->kind == SPELLING_BOUNDS)
4032 sprintf (d, "[%d]", p->u.i);
4033 d += strlen (d);
4035 else
4037 const char *s;
4038 if (p->kind == SPELLING_MEMBER)
4039 *d++ = '.';
4040 for (s = p->u.s; (*d = *s++); d++)
4043 *d++ = '\0';
4044 return buffer;
4047 /* Issue an error message for a bad initializer component.
4048 MSGID identifies the message.
4049 The component name is taken from the spelling stack. */
4051 void
4052 error_init (const char *msgid)
4054 char *ofwhat;
4056 error ("%s", _(msgid));
4057 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4058 if (*ofwhat)
4059 error ("(near initialization for %qs)", ofwhat);
4062 /* Issue a pedantic warning for a bad initializer component.
4063 MSGID identifies the message.
4064 The component name is taken from the spelling stack. */
4066 void
4067 pedwarn_init (const char *msgid)
4069 char *ofwhat;
4071 pedwarn ("%s", _(msgid));
4072 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4073 if (*ofwhat)
4074 pedwarn ("(near initialization for %qs)", ofwhat);
4077 /* Issue a warning for a bad initializer component.
4078 MSGID identifies the message.
4079 The component name is taken from the spelling stack. */
4081 static void
4082 warning_init (const char *msgid)
4084 char *ofwhat;
4086 warning ("%s", _(msgid));
4087 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4088 if (*ofwhat)
4089 warning ("(near initialization for %qs)", ofwhat);
4092 /* If TYPE is an array type and EXPR is a parenthesized string
4093 constant, warn if pedantic that EXPR is being used to initialize an
4094 object of type TYPE. */
4096 void
4097 maybe_warn_string_init (tree type, struct c_expr expr)
4099 if (pedantic
4100 && TREE_CODE (type) == ARRAY_TYPE
4101 && TREE_CODE (expr.value) == STRING_CST
4102 && expr.original_code != STRING_CST)
4103 pedwarn_init ("array initialized from parenthesized string constant");
4106 /* Digest the parser output INIT as an initializer for type TYPE.
4107 Return a C expression of type TYPE to represent the initial value.
4109 If INIT is a string constant, STRICT_STRING is true if it is
4110 unparenthesized or we should not warn here for it being parenthesized.
4111 For other types of INIT, STRICT_STRING is not used.
4113 REQUIRE_CONSTANT requests an error if non-constant initializers or
4114 elements are seen. */
4116 static tree
4117 digest_init (tree type, tree init, bool strict_string, int require_constant)
4119 enum tree_code code = TREE_CODE (type);
4120 tree inside_init = init;
4122 if (type == error_mark_node
4123 || init == error_mark_node
4124 || TREE_TYPE (init) == error_mark_node)
4125 return error_mark_node;
4127 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4128 /* Do not use STRIP_NOPS here. We do not want an enumerator
4129 whose value is 0 to count as a null pointer constant. */
4130 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4131 inside_init = TREE_OPERAND (init, 0);
4133 inside_init = fold (inside_init);
4135 /* Initialization of an array of chars from a string constant
4136 optionally enclosed in braces. */
4138 if (code == ARRAY_TYPE && inside_init
4139 && TREE_CODE (inside_init) == STRING_CST)
4141 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4142 /* Note that an array could be both an array of character type
4143 and an array of wchar_t if wchar_t is signed char or unsigned
4144 char. */
4145 bool char_array = (typ1 == char_type_node
4146 || typ1 == signed_char_type_node
4147 || typ1 == unsigned_char_type_node);
4148 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4149 if (char_array || wchar_array)
4151 struct c_expr expr;
4152 bool char_string;
4153 expr.value = inside_init;
4154 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4155 maybe_warn_string_init (type, expr);
4157 char_string
4158 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4159 == char_type_node);
4161 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4162 TYPE_MAIN_VARIANT (type)))
4163 return inside_init;
4165 if (!wchar_array && !char_string)
4167 error_init ("char-array initialized from wide string");
4168 return error_mark_node;
4170 if (char_string && !char_array)
4172 error_init ("wchar_t-array initialized from non-wide string");
4173 return error_mark_node;
4176 TREE_TYPE (inside_init) = type;
4177 if (TYPE_DOMAIN (type) != 0
4178 && TYPE_SIZE (type) != 0
4179 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4180 /* Subtract 1 (or sizeof (wchar_t))
4181 because it's ok to ignore the terminating null char
4182 that is counted in the length of the constant. */
4183 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4184 TREE_STRING_LENGTH (inside_init)
4185 - ((TYPE_PRECISION (typ1)
4186 != TYPE_PRECISION (char_type_node))
4187 ? (TYPE_PRECISION (wchar_type_node)
4188 / BITS_PER_UNIT)
4189 : 1)))
4190 pedwarn_init ("initializer-string for array of chars is too long");
4192 return inside_init;
4194 else if (INTEGRAL_TYPE_P (typ1))
4196 error_init ("array of inappropriate type initialized "
4197 "from string constant");
4198 return error_mark_node;
4202 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4203 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4204 below and handle as a constructor. */
4205 if (code == VECTOR_TYPE
4206 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4207 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4208 && TREE_CONSTANT (inside_init))
4210 if (TREE_CODE (inside_init) == VECTOR_CST
4211 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4212 TYPE_MAIN_VARIANT (type)))
4213 return inside_init;
4214 else
4215 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4218 /* Any type can be initialized
4219 from an expression of the same type, optionally with braces. */
4221 if (inside_init && TREE_TYPE (inside_init) != 0
4222 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4223 TYPE_MAIN_VARIANT (type))
4224 || (code == ARRAY_TYPE
4225 && comptypes (TREE_TYPE (inside_init), type))
4226 || (code == VECTOR_TYPE
4227 && comptypes (TREE_TYPE (inside_init), type))
4228 || (code == POINTER_TYPE
4229 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4230 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4231 TREE_TYPE (type)))
4232 || (code == POINTER_TYPE
4233 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4234 && comptypes (TREE_TYPE (inside_init),
4235 TREE_TYPE (type)))))
4237 if (code == POINTER_TYPE)
4239 inside_init = default_function_array_conversion (inside_init);
4241 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4243 error_init ("invalid use of non-lvalue array");
4244 return error_mark_node;
4248 if (code == VECTOR_TYPE)
4249 /* Although the types are compatible, we may require a
4250 conversion. */
4251 inside_init = convert (type, inside_init);
4253 if (require_constant && !flag_isoc99
4254 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4256 /* As an extension, allow initializing objects with static storage
4257 duration with compound literals (which are then treated just as
4258 the brace enclosed list they contain). */
4259 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4260 inside_init = DECL_INITIAL (decl);
4263 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4264 && TREE_CODE (inside_init) != CONSTRUCTOR)
4266 error_init ("array initialized from non-constant array expression");
4267 return error_mark_node;
4270 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4271 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4273 /* Compound expressions can only occur here if -pedantic or
4274 -pedantic-errors is specified. In the later case, we always want
4275 an error. In the former case, we simply want a warning. */
4276 if (require_constant && pedantic
4277 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4279 inside_init
4280 = valid_compound_expr_initializer (inside_init,
4281 TREE_TYPE (inside_init));
4282 if (inside_init == error_mark_node)
4283 error_init ("initializer element is not constant");
4284 else
4285 pedwarn_init ("initializer element is not constant");
4286 if (flag_pedantic_errors)
4287 inside_init = error_mark_node;
4289 else if (require_constant
4290 && !initializer_constant_valid_p (inside_init,
4291 TREE_TYPE (inside_init)))
4293 error_init ("initializer element is not constant");
4294 inside_init = error_mark_node;
4297 return inside_init;
4300 /* Handle scalar types, including conversions. */
4302 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4303 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4304 || code == VECTOR_TYPE)
4306 /* Note that convert_for_assignment calls default_conversion
4307 for arrays and functions. We must not call it in the
4308 case where inside_init is a null pointer constant. */
4309 inside_init
4310 = convert_for_assignment (type, init, ic_init,
4311 NULL_TREE, NULL_TREE, 0);
4313 /* Check to see if we have already given an error message. */
4314 if (inside_init == error_mark_node)
4316 else if (require_constant && !TREE_CONSTANT (inside_init))
4318 error_init ("initializer element is not constant");
4319 inside_init = error_mark_node;
4321 else if (require_constant
4322 && !initializer_constant_valid_p (inside_init,
4323 TREE_TYPE (inside_init)))
4325 error_init ("initializer element is not computable at load time");
4326 inside_init = error_mark_node;
4329 return inside_init;
4332 /* Come here only for records and arrays. */
4334 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4336 error_init ("variable-sized object may not be initialized");
4337 return error_mark_node;
4340 error_init ("invalid initializer");
4341 return error_mark_node;
4344 /* Handle initializers that use braces. */
4346 /* Type of object we are accumulating a constructor for.
4347 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4348 static tree constructor_type;
4350 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4351 left to fill. */
4352 static tree constructor_fields;
4354 /* For an ARRAY_TYPE, this is the specified index
4355 at which to store the next element we get. */
4356 static tree constructor_index;
4358 /* For an ARRAY_TYPE, this is the maximum index. */
4359 static tree constructor_max_index;
4361 /* For a RECORD_TYPE, this is the first field not yet written out. */
4362 static tree constructor_unfilled_fields;
4364 /* For an ARRAY_TYPE, this is the index of the first element
4365 not yet written out. */
4366 static tree constructor_unfilled_index;
4368 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4369 This is so we can generate gaps between fields, when appropriate. */
4370 static tree constructor_bit_index;
4372 /* If we are saving up the elements rather than allocating them,
4373 this is the list of elements so far (in reverse order,
4374 most recent first). */
4375 static tree constructor_elements;
4377 /* 1 if constructor should be incrementally stored into a constructor chain,
4378 0 if all the elements should be kept in AVL tree. */
4379 static int constructor_incremental;
4381 /* 1 if so far this constructor's elements are all compile-time constants. */
4382 static int constructor_constant;
4384 /* 1 if so far this constructor's elements are all valid address constants. */
4385 static int constructor_simple;
4387 /* 1 if this constructor is erroneous so far. */
4388 static int constructor_erroneous;
4390 /* Structure for managing pending initializer elements, organized as an
4391 AVL tree. */
4393 struct init_node
4395 struct init_node *left, *right;
4396 struct init_node *parent;
4397 int balance;
4398 tree purpose;
4399 tree value;
4402 /* Tree of pending elements at this constructor level.
4403 These are elements encountered out of order
4404 which belong at places we haven't reached yet in actually
4405 writing the output.
4406 Will never hold tree nodes across GC runs. */
4407 static struct init_node *constructor_pending_elts;
4409 /* The SPELLING_DEPTH of this constructor. */
4410 static int constructor_depth;
4412 /* 0 if implicitly pushing constructor levels is allowed. */
4413 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4415 /* DECL node for which an initializer is being read.
4416 0 means we are reading a constructor expression
4417 such as (struct foo) {...}. */
4418 static tree constructor_decl;
4420 /* Nonzero if this is an initializer for a top-level decl. */
4421 static int constructor_top_level;
4423 /* Nonzero if there were any member designators in this initializer. */
4424 static int constructor_designated;
4426 /* Nesting depth of designator list. */
4427 static int designator_depth;
4429 /* Nonzero if there were diagnosed errors in this designator list. */
4430 static int designator_errorneous;
4433 /* This stack has a level for each implicit or explicit level of
4434 structuring in the initializer, including the outermost one. It
4435 saves the values of most of the variables above. */
4437 struct constructor_range_stack;
4439 struct constructor_stack
4441 struct constructor_stack *next;
4442 tree type;
4443 tree fields;
4444 tree index;
4445 tree max_index;
4446 tree unfilled_index;
4447 tree unfilled_fields;
4448 tree bit_index;
4449 tree elements;
4450 struct init_node *pending_elts;
4451 int offset;
4452 int depth;
4453 /* If value nonzero, this value should replace the entire
4454 constructor at this level. */
4455 struct c_expr replacement_value;
4456 struct constructor_range_stack *range_stack;
4457 char constant;
4458 char simple;
4459 char implicit;
4460 char erroneous;
4461 char outer;
4462 char incremental;
4463 char designated;
4466 struct constructor_stack *constructor_stack;
4468 /* This stack represents designators from some range designator up to
4469 the last designator in the list. */
4471 struct constructor_range_stack
4473 struct constructor_range_stack *next, *prev;
4474 struct constructor_stack *stack;
4475 tree range_start;
4476 tree index;
4477 tree range_end;
4478 tree fields;
4481 struct constructor_range_stack *constructor_range_stack;
4483 /* This stack records separate initializers that are nested.
4484 Nested initializers can't happen in ANSI C, but GNU C allows them
4485 in cases like { ... (struct foo) { ... } ... }. */
4487 struct initializer_stack
4489 struct initializer_stack *next;
4490 tree decl;
4491 struct constructor_stack *constructor_stack;
4492 struct constructor_range_stack *constructor_range_stack;
4493 tree elements;
4494 struct spelling *spelling;
4495 struct spelling *spelling_base;
4496 int spelling_size;
4497 char top_level;
4498 char require_constant_value;
4499 char require_constant_elements;
4502 struct initializer_stack *initializer_stack;
4504 /* Prepare to parse and output the initializer for variable DECL. */
4506 void
4507 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4509 const char *locus;
4510 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4512 p->decl = constructor_decl;
4513 p->require_constant_value = require_constant_value;
4514 p->require_constant_elements = require_constant_elements;
4515 p->constructor_stack = constructor_stack;
4516 p->constructor_range_stack = constructor_range_stack;
4517 p->elements = constructor_elements;
4518 p->spelling = spelling;
4519 p->spelling_base = spelling_base;
4520 p->spelling_size = spelling_size;
4521 p->top_level = constructor_top_level;
4522 p->next = initializer_stack;
4523 initializer_stack = p;
4525 constructor_decl = decl;
4526 constructor_designated = 0;
4527 constructor_top_level = top_level;
4529 if (decl != 0)
4531 require_constant_value = TREE_STATIC (decl);
4532 require_constant_elements
4533 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4534 /* For a scalar, you can always use any value to initialize,
4535 even within braces. */
4536 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4537 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4538 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4539 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4540 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4542 else
4544 require_constant_value = 0;
4545 require_constant_elements = 0;
4546 locus = "(anonymous)";
4549 constructor_stack = 0;
4550 constructor_range_stack = 0;
4552 missing_braces_mentioned = 0;
4554 spelling_base = 0;
4555 spelling_size = 0;
4556 RESTORE_SPELLING_DEPTH (0);
4558 if (locus)
4559 push_string (locus);
4562 void
4563 finish_init (void)
4565 struct initializer_stack *p = initializer_stack;
4567 /* Free the whole constructor stack of this initializer. */
4568 while (constructor_stack)
4570 struct constructor_stack *q = constructor_stack;
4571 constructor_stack = q->next;
4572 free (q);
4575 gcc_assert (!constructor_range_stack);
4577 /* Pop back to the data of the outer initializer (if any). */
4578 free (spelling_base);
4580 constructor_decl = p->decl;
4581 require_constant_value = p->require_constant_value;
4582 require_constant_elements = p->require_constant_elements;
4583 constructor_stack = p->constructor_stack;
4584 constructor_range_stack = p->constructor_range_stack;
4585 constructor_elements = p->elements;
4586 spelling = p->spelling;
4587 spelling_base = p->spelling_base;
4588 spelling_size = p->spelling_size;
4589 constructor_top_level = p->top_level;
4590 initializer_stack = p->next;
4591 free (p);
4594 /* Call here when we see the initializer is surrounded by braces.
4595 This is instead of a call to push_init_level;
4596 it is matched by a call to pop_init_level.
4598 TYPE is the type to initialize, for a constructor expression.
4599 For an initializer for a decl, TYPE is zero. */
4601 void
4602 really_start_incremental_init (tree type)
4604 struct constructor_stack *p = XNEW (struct constructor_stack);
4606 if (type == 0)
4607 type = TREE_TYPE (constructor_decl);
4609 if (targetm.vector_opaque_p (type))
4610 error ("opaque vector types cannot be initialized");
4612 p->type = constructor_type;
4613 p->fields = constructor_fields;
4614 p->index = constructor_index;
4615 p->max_index = constructor_max_index;
4616 p->unfilled_index = constructor_unfilled_index;
4617 p->unfilled_fields = constructor_unfilled_fields;
4618 p->bit_index = constructor_bit_index;
4619 p->elements = constructor_elements;
4620 p->constant = constructor_constant;
4621 p->simple = constructor_simple;
4622 p->erroneous = constructor_erroneous;
4623 p->pending_elts = constructor_pending_elts;
4624 p->depth = constructor_depth;
4625 p->replacement_value.value = 0;
4626 p->replacement_value.original_code = ERROR_MARK;
4627 p->implicit = 0;
4628 p->range_stack = 0;
4629 p->outer = 0;
4630 p->incremental = constructor_incremental;
4631 p->designated = constructor_designated;
4632 p->next = 0;
4633 constructor_stack = p;
4635 constructor_constant = 1;
4636 constructor_simple = 1;
4637 constructor_depth = SPELLING_DEPTH ();
4638 constructor_elements = 0;
4639 constructor_pending_elts = 0;
4640 constructor_type = type;
4641 constructor_incremental = 1;
4642 constructor_designated = 0;
4643 designator_depth = 0;
4644 designator_errorneous = 0;
4646 if (TREE_CODE (constructor_type) == RECORD_TYPE
4647 || TREE_CODE (constructor_type) == UNION_TYPE)
4649 constructor_fields = TYPE_FIELDS (constructor_type);
4650 /* Skip any nameless bit fields at the beginning. */
4651 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4652 && DECL_NAME (constructor_fields) == 0)
4653 constructor_fields = TREE_CHAIN (constructor_fields);
4655 constructor_unfilled_fields = constructor_fields;
4656 constructor_bit_index = bitsize_zero_node;
4658 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4660 if (TYPE_DOMAIN (constructor_type))
4662 constructor_max_index
4663 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4665 /* Detect non-empty initializations of zero-length arrays. */
4666 if (constructor_max_index == NULL_TREE
4667 && TYPE_SIZE (constructor_type))
4668 constructor_max_index = build_int_cst (NULL_TREE, -1);
4670 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4671 to initialize VLAs will cause a proper error; avoid tree
4672 checking errors as well by setting a safe value. */
4673 if (constructor_max_index
4674 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4675 constructor_max_index = build_int_cst (NULL_TREE, -1);
4677 constructor_index
4678 = convert (bitsizetype,
4679 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4681 else
4682 constructor_index = bitsize_zero_node;
4684 constructor_unfilled_index = constructor_index;
4686 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4688 /* Vectors are like simple fixed-size arrays. */
4689 constructor_max_index =
4690 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4691 constructor_index = convert (bitsizetype, bitsize_zero_node);
4692 constructor_unfilled_index = constructor_index;
4694 else
4696 /* Handle the case of int x = {5}; */
4697 constructor_fields = constructor_type;
4698 constructor_unfilled_fields = constructor_type;
4702 /* Push down into a subobject, for initialization.
4703 If this is for an explicit set of braces, IMPLICIT is 0.
4704 If it is because the next element belongs at a lower level,
4705 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4707 void
4708 push_init_level (int implicit)
4710 struct constructor_stack *p;
4711 tree value = NULL_TREE;
4713 /* If we've exhausted any levels that didn't have braces,
4714 pop them now. */
4715 while (constructor_stack->implicit)
4717 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4718 || TREE_CODE (constructor_type) == UNION_TYPE)
4719 && constructor_fields == 0)
4720 process_init_element (pop_init_level (1));
4721 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4722 && constructor_max_index
4723 && tree_int_cst_lt (constructor_max_index, constructor_index))
4724 process_init_element (pop_init_level (1));
4725 else
4726 break;
4729 /* Unless this is an explicit brace, we need to preserve previous
4730 content if any. */
4731 if (implicit)
4733 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4734 || TREE_CODE (constructor_type) == UNION_TYPE)
4735 && constructor_fields)
4736 value = find_init_member (constructor_fields);
4737 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4738 value = find_init_member (constructor_index);
4741 p = XNEW (struct constructor_stack);
4742 p->type = constructor_type;
4743 p->fields = constructor_fields;
4744 p->index = constructor_index;
4745 p->max_index = constructor_max_index;
4746 p->unfilled_index = constructor_unfilled_index;
4747 p->unfilled_fields = constructor_unfilled_fields;
4748 p->bit_index = constructor_bit_index;
4749 p->elements = constructor_elements;
4750 p->constant = constructor_constant;
4751 p->simple = constructor_simple;
4752 p->erroneous = constructor_erroneous;
4753 p->pending_elts = constructor_pending_elts;
4754 p->depth = constructor_depth;
4755 p->replacement_value.value = 0;
4756 p->replacement_value.original_code = ERROR_MARK;
4757 p->implicit = implicit;
4758 p->outer = 0;
4759 p->incremental = constructor_incremental;
4760 p->designated = constructor_designated;
4761 p->next = constructor_stack;
4762 p->range_stack = 0;
4763 constructor_stack = p;
4765 constructor_constant = 1;
4766 constructor_simple = 1;
4767 constructor_depth = SPELLING_DEPTH ();
4768 constructor_elements = 0;
4769 constructor_incremental = 1;
4770 constructor_designated = 0;
4771 constructor_pending_elts = 0;
4772 if (!implicit)
4774 p->range_stack = constructor_range_stack;
4775 constructor_range_stack = 0;
4776 designator_depth = 0;
4777 designator_errorneous = 0;
4780 /* Don't die if an entire brace-pair level is superfluous
4781 in the containing level. */
4782 if (constructor_type == 0)
4784 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4785 || TREE_CODE (constructor_type) == UNION_TYPE)
4787 /* Don't die if there are extra init elts at the end. */
4788 if (constructor_fields == 0)
4789 constructor_type = 0;
4790 else
4792 constructor_type = TREE_TYPE (constructor_fields);
4793 push_member_name (constructor_fields);
4794 constructor_depth++;
4797 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4799 constructor_type = TREE_TYPE (constructor_type);
4800 push_array_bounds (tree_low_cst (constructor_index, 0));
4801 constructor_depth++;
4804 if (constructor_type == 0)
4806 error_init ("extra brace group at end of initializer");
4807 constructor_fields = 0;
4808 constructor_unfilled_fields = 0;
4809 return;
4812 if (value && TREE_CODE (value) == CONSTRUCTOR)
4814 constructor_constant = TREE_CONSTANT (value);
4815 constructor_simple = TREE_STATIC (value);
4816 constructor_elements = CONSTRUCTOR_ELTS (value);
4817 if (constructor_elements
4818 && (TREE_CODE (constructor_type) == RECORD_TYPE
4819 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4820 set_nonincremental_init ();
4823 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4825 missing_braces_mentioned = 1;
4826 warning_init ("missing braces around initializer");
4829 if (TREE_CODE (constructor_type) == RECORD_TYPE
4830 || TREE_CODE (constructor_type) == UNION_TYPE)
4832 constructor_fields = TYPE_FIELDS (constructor_type);
4833 /* Skip any nameless bit fields at the beginning. */
4834 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4835 && DECL_NAME (constructor_fields) == 0)
4836 constructor_fields = TREE_CHAIN (constructor_fields);
4838 constructor_unfilled_fields = constructor_fields;
4839 constructor_bit_index = bitsize_zero_node;
4841 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4843 /* Vectors are like simple fixed-size arrays. */
4844 constructor_max_index =
4845 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4846 constructor_index = convert (bitsizetype, integer_zero_node);
4847 constructor_unfilled_index = constructor_index;
4849 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4851 if (TYPE_DOMAIN (constructor_type))
4853 constructor_max_index
4854 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4856 /* Detect non-empty initializations of zero-length arrays. */
4857 if (constructor_max_index == NULL_TREE
4858 && TYPE_SIZE (constructor_type))
4859 constructor_max_index = build_int_cst (NULL_TREE, -1);
4861 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4862 to initialize VLAs will cause a proper error; avoid tree
4863 checking errors as well by setting a safe value. */
4864 if (constructor_max_index
4865 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4866 constructor_max_index = build_int_cst (NULL_TREE, -1);
4868 constructor_index
4869 = convert (bitsizetype,
4870 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4872 else
4873 constructor_index = bitsize_zero_node;
4875 constructor_unfilled_index = constructor_index;
4876 if (value && TREE_CODE (value) == STRING_CST)
4878 /* We need to split the char/wchar array into individual
4879 characters, so that we don't have to special case it
4880 everywhere. */
4881 set_nonincremental_init_from_string (value);
4884 else
4886 warning_init ("braces around scalar initializer");
4887 constructor_fields = constructor_type;
4888 constructor_unfilled_fields = constructor_type;
4892 /* At the end of an implicit or explicit brace level,
4893 finish up that level of constructor. If a single expression
4894 with redundant braces initialized that level, return the
4895 c_expr structure for that expression. Otherwise, the original_code
4896 element is set to ERROR_MARK.
4897 If we were outputting the elements as they are read, return 0 as the value
4898 from inner levels (process_init_element ignores that),
4899 but return error_mark_node as the value from the outermost level
4900 (that's what we want to put in DECL_INITIAL).
4901 Otherwise, return a CONSTRUCTOR expression as the value. */
4903 struct c_expr
4904 pop_init_level (int implicit)
4906 struct constructor_stack *p;
4907 struct c_expr ret;
4908 ret.value = 0;
4909 ret.original_code = ERROR_MARK;
4911 if (implicit == 0)
4913 /* When we come to an explicit close brace,
4914 pop any inner levels that didn't have explicit braces. */
4915 while (constructor_stack->implicit)
4916 process_init_element (pop_init_level (1));
4918 gcc_assert (!constructor_range_stack);
4921 /* Now output all pending elements. */
4922 constructor_incremental = 1;
4923 output_pending_init_elements (1);
4925 p = constructor_stack;
4927 /* Error for initializing a flexible array member, or a zero-length
4928 array member in an inappropriate context. */
4929 if (constructor_type && constructor_fields
4930 && TREE_CODE (constructor_type) == ARRAY_TYPE
4931 && TYPE_DOMAIN (constructor_type)
4932 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4934 /* Silently discard empty initializations. The parser will
4935 already have pedwarned for empty brackets. */
4936 if (integer_zerop (constructor_unfilled_index))
4937 constructor_type = NULL_TREE;
4938 else
4940 gcc_assert (!TYPE_SIZE (constructor_type));
4942 if (constructor_depth > 2)
4943 error_init ("initialization of flexible array member in a nested context");
4944 else if (pedantic)
4945 pedwarn_init ("initialization of a flexible array member");
4947 /* We have already issued an error message for the existence
4948 of a flexible array member not at the end of the structure.
4949 Discard the initializer so that we do not abort later. */
4950 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4951 constructor_type = NULL_TREE;
4955 /* Warn when some struct elements are implicitly initialized to zero. */
4956 if (warn_missing_field_initializers
4957 && constructor_type
4958 && TREE_CODE (constructor_type) == RECORD_TYPE
4959 && constructor_unfilled_fields)
4961 /* Do not warn for flexible array members or zero-length arrays. */
4962 while (constructor_unfilled_fields
4963 && (!DECL_SIZE (constructor_unfilled_fields)
4964 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4965 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4967 /* Do not warn if this level of the initializer uses member
4968 designators; it is likely to be deliberate. */
4969 if (constructor_unfilled_fields && !constructor_designated)
4971 push_member_name (constructor_unfilled_fields);
4972 warning_init ("missing initializer");
4973 RESTORE_SPELLING_DEPTH (constructor_depth);
4977 /* Pad out the end of the structure. */
4978 if (p->replacement_value.value)
4979 /* If this closes a superfluous brace pair,
4980 just pass out the element between them. */
4981 ret = p->replacement_value;
4982 else if (constructor_type == 0)
4984 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4985 && TREE_CODE (constructor_type) != UNION_TYPE
4986 && TREE_CODE (constructor_type) != ARRAY_TYPE
4987 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4989 /* A nonincremental scalar initializer--just return
4990 the element, after verifying there is just one. */
4991 if (constructor_elements == 0)
4993 if (!constructor_erroneous)
4994 error_init ("empty scalar initializer");
4995 ret.value = error_mark_node;
4997 else if (TREE_CHAIN (constructor_elements) != 0)
4999 error_init ("extra elements in scalar initializer");
5000 ret.value = TREE_VALUE (constructor_elements);
5002 else
5003 ret.value = TREE_VALUE (constructor_elements);
5005 else
5007 if (constructor_erroneous)
5008 ret.value = error_mark_node;
5009 else
5011 ret.value = build_constructor (constructor_type,
5012 nreverse (constructor_elements));
5013 if (constructor_constant)
5014 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5015 if (constructor_constant && constructor_simple)
5016 TREE_STATIC (ret.value) = 1;
5020 constructor_type = p->type;
5021 constructor_fields = p->fields;
5022 constructor_index = p->index;
5023 constructor_max_index = p->max_index;
5024 constructor_unfilled_index = p->unfilled_index;
5025 constructor_unfilled_fields = p->unfilled_fields;
5026 constructor_bit_index = p->bit_index;
5027 constructor_elements = p->elements;
5028 constructor_constant = p->constant;
5029 constructor_simple = p->simple;
5030 constructor_erroneous = p->erroneous;
5031 constructor_incremental = p->incremental;
5032 constructor_designated = p->designated;
5033 constructor_pending_elts = p->pending_elts;
5034 constructor_depth = p->depth;
5035 if (!p->implicit)
5036 constructor_range_stack = p->range_stack;
5037 RESTORE_SPELLING_DEPTH (constructor_depth);
5039 constructor_stack = p->next;
5040 free (p);
5042 if (ret.value == 0)
5044 if (constructor_stack == 0)
5046 ret.value = error_mark_node;
5047 return ret;
5049 return ret;
5051 return ret;
5054 /* Common handling for both array range and field name designators.
5055 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5057 static int
5058 set_designator (int array)
5060 tree subtype;
5061 enum tree_code subcode;
5063 /* Don't die if an entire brace-pair level is superfluous
5064 in the containing level. */
5065 if (constructor_type == 0)
5066 return 1;
5068 /* If there were errors in this designator list already, bail out
5069 silently. */
5070 if (designator_errorneous)
5071 return 1;
5073 if (!designator_depth)
5075 gcc_assert (!constructor_range_stack);
5077 /* Designator list starts at the level of closest explicit
5078 braces. */
5079 while (constructor_stack->implicit)
5080 process_init_element (pop_init_level (1));
5081 constructor_designated = 1;
5082 return 0;
5085 if (constructor_no_implicit)
5087 error_init ("initialization designators may not nest");
5088 return 1;
5091 switch (TREE_CODE (constructor_type))
5093 case RECORD_TYPE:
5094 case UNION_TYPE:
5095 subtype = TREE_TYPE (constructor_fields);
5096 if (subtype != error_mark_node)
5097 subtype = TYPE_MAIN_VARIANT (subtype);
5098 break;
5099 case ARRAY_TYPE:
5100 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5101 break;
5102 default:
5103 gcc_unreachable ();
5106 subcode = TREE_CODE (subtype);
5107 if (array && subcode != ARRAY_TYPE)
5109 error_init ("array index in non-array initializer");
5110 return 1;
5112 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5114 error_init ("field name not in record or union initializer");
5115 return 1;
5118 constructor_designated = 1;
5119 push_init_level (2);
5120 return 0;
5123 /* If there are range designators in designator list, push a new designator
5124 to constructor_range_stack. RANGE_END is end of such stack range or
5125 NULL_TREE if there is no range designator at this level. */
5127 static void
5128 push_range_stack (tree range_end)
5130 struct constructor_range_stack *p;
5132 p = GGC_NEW (struct constructor_range_stack);
5133 p->prev = constructor_range_stack;
5134 p->next = 0;
5135 p->fields = constructor_fields;
5136 p->range_start = constructor_index;
5137 p->index = constructor_index;
5138 p->stack = constructor_stack;
5139 p->range_end = range_end;
5140 if (constructor_range_stack)
5141 constructor_range_stack->next = p;
5142 constructor_range_stack = p;
5145 /* Within an array initializer, specify the next index to be initialized.
5146 FIRST is that index. If LAST is nonzero, then initialize a range
5147 of indices, running from FIRST through LAST. */
5149 void
5150 set_init_index (tree first, tree last)
5152 if (set_designator (1))
5153 return;
5155 designator_errorneous = 1;
5157 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5158 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5160 error_init ("array index in initializer not of integer type");
5161 return;
5164 while ((TREE_CODE (first) == NOP_EXPR
5165 || TREE_CODE (first) == CONVERT_EXPR
5166 || TREE_CODE (first) == NON_LVALUE_EXPR)
5167 && (TYPE_MODE (TREE_TYPE (first))
5168 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5169 first = TREE_OPERAND (first, 0);
5171 if (last)
5172 while ((TREE_CODE (last) == NOP_EXPR
5173 || TREE_CODE (last) == CONVERT_EXPR
5174 || TREE_CODE (last) == NON_LVALUE_EXPR)
5175 && (TYPE_MODE (TREE_TYPE (last))
5176 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5177 last = TREE_OPERAND (last, 0);
5179 if (TREE_CODE (first) != INTEGER_CST)
5180 error_init ("nonconstant array index in initializer");
5181 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5182 error_init ("nonconstant array index in initializer");
5183 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5184 error_init ("array index in non-array initializer");
5185 else if (tree_int_cst_sgn (first) == -1)
5186 error_init ("array index in initializer exceeds array bounds");
5187 else if (constructor_max_index
5188 && tree_int_cst_lt (constructor_max_index, first))
5189 error_init ("array index in initializer exceeds array bounds");
5190 else
5192 constructor_index = convert (bitsizetype, first);
5194 if (last)
5196 if (tree_int_cst_equal (first, last))
5197 last = 0;
5198 else if (tree_int_cst_lt (last, first))
5200 error_init ("empty index range in initializer");
5201 last = 0;
5203 else
5205 last = convert (bitsizetype, last);
5206 if (constructor_max_index != 0
5207 && tree_int_cst_lt (constructor_max_index, last))
5209 error_init ("array index range in initializer exceeds array bounds");
5210 last = 0;
5215 designator_depth++;
5216 designator_errorneous = 0;
5217 if (constructor_range_stack || last)
5218 push_range_stack (last);
5222 /* Within a struct initializer, specify the next field to be initialized. */
5224 void
5225 set_init_label (tree fieldname)
5227 tree tail;
5229 if (set_designator (0))
5230 return;
5232 designator_errorneous = 1;
5234 if (TREE_CODE (constructor_type) != RECORD_TYPE
5235 && TREE_CODE (constructor_type) != UNION_TYPE)
5237 error_init ("field name not in record or union initializer");
5238 return;
5241 for (tail = TYPE_FIELDS (constructor_type); tail;
5242 tail = TREE_CHAIN (tail))
5244 if (DECL_NAME (tail) == fieldname)
5245 break;
5248 if (tail == 0)
5249 error ("unknown field %qs specified in initializer",
5250 IDENTIFIER_POINTER (fieldname));
5251 else
5253 constructor_fields = tail;
5254 designator_depth++;
5255 designator_errorneous = 0;
5256 if (constructor_range_stack)
5257 push_range_stack (NULL_TREE);
5261 /* Add a new initializer to the tree of pending initializers. PURPOSE
5262 identifies the initializer, either array index or field in a structure.
5263 VALUE is the value of that index or field. */
5265 static void
5266 add_pending_init (tree purpose, tree value)
5268 struct init_node *p, **q, *r;
5270 q = &constructor_pending_elts;
5271 p = 0;
5273 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5275 while (*q != 0)
5277 p = *q;
5278 if (tree_int_cst_lt (purpose, p->purpose))
5279 q = &p->left;
5280 else if (tree_int_cst_lt (p->purpose, purpose))
5281 q = &p->right;
5282 else
5284 if (TREE_SIDE_EFFECTS (p->value))
5285 warning_init ("initialized field with side-effects overwritten");
5286 p->value = value;
5287 return;
5291 else
5293 tree bitpos;
5295 bitpos = bit_position (purpose);
5296 while (*q != NULL)
5298 p = *q;
5299 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5300 q = &p->left;
5301 else if (p->purpose != purpose)
5302 q = &p->right;
5303 else
5305 if (TREE_SIDE_EFFECTS (p->value))
5306 warning_init ("initialized field with side-effects overwritten");
5307 p->value = value;
5308 return;
5313 r = GGC_NEW (struct init_node);
5314 r->purpose = purpose;
5315 r->value = value;
5317 *q = r;
5318 r->parent = p;
5319 r->left = 0;
5320 r->right = 0;
5321 r->balance = 0;
5323 while (p)
5325 struct init_node *s;
5327 if (r == p->left)
5329 if (p->balance == 0)
5330 p->balance = -1;
5331 else if (p->balance < 0)
5333 if (r->balance < 0)
5335 /* L rotation. */
5336 p->left = r->right;
5337 if (p->left)
5338 p->left->parent = p;
5339 r->right = p;
5341 p->balance = 0;
5342 r->balance = 0;
5344 s = p->parent;
5345 p->parent = r;
5346 r->parent = s;
5347 if (s)
5349 if (s->left == p)
5350 s->left = r;
5351 else
5352 s->right = r;
5354 else
5355 constructor_pending_elts = r;
5357 else
5359 /* LR rotation. */
5360 struct init_node *t = r->right;
5362 r->right = t->left;
5363 if (r->right)
5364 r->right->parent = r;
5365 t->left = r;
5367 p->left = t->right;
5368 if (p->left)
5369 p->left->parent = p;
5370 t->right = p;
5372 p->balance = t->balance < 0;
5373 r->balance = -(t->balance > 0);
5374 t->balance = 0;
5376 s = p->parent;
5377 p->parent = t;
5378 r->parent = t;
5379 t->parent = s;
5380 if (s)
5382 if (s->left == p)
5383 s->left = t;
5384 else
5385 s->right = t;
5387 else
5388 constructor_pending_elts = t;
5390 break;
5392 else
5394 /* p->balance == +1; growth of left side balances the node. */
5395 p->balance = 0;
5396 break;
5399 else /* r == p->right */
5401 if (p->balance == 0)
5402 /* Growth propagation from right side. */
5403 p->balance++;
5404 else if (p->balance > 0)
5406 if (r->balance > 0)
5408 /* R rotation. */
5409 p->right = r->left;
5410 if (p->right)
5411 p->right->parent = p;
5412 r->left = p;
5414 p->balance = 0;
5415 r->balance = 0;
5417 s = p->parent;
5418 p->parent = r;
5419 r->parent = s;
5420 if (s)
5422 if (s->left == p)
5423 s->left = r;
5424 else
5425 s->right = r;
5427 else
5428 constructor_pending_elts = r;
5430 else /* r->balance == -1 */
5432 /* RL rotation */
5433 struct init_node *t = r->left;
5435 r->left = t->right;
5436 if (r->left)
5437 r->left->parent = r;
5438 t->right = r;
5440 p->right = t->left;
5441 if (p->right)
5442 p->right->parent = p;
5443 t->left = p;
5445 r->balance = (t->balance < 0);
5446 p->balance = -(t->balance > 0);
5447 t->balance = 0;
5449 s = p->parent;
5450 p->parent = t;
5451 r->parent = t;
5452 t->parent = s;
5453 if (s)
5455 if (s->left == p)
5456 s->left = t;
5457 else
5458 s->right = t;
5460 else
5461 constructor_pending_elts = t;
5463 break;
5465 else
5467 /* p->balance == -1; growth of right side balances the node. */
5468 p->balance = 0;
5469 break;
5473 r = p;
5474 p = p->parent;
5478 /* Build AVL tree from a sorted chain. */
5480 static void
5481 set_nonincremental_init (void)
5483 tree chain;
5485 if (TREE_CODE (constructor_type) != RECORD_TYPE
5486 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5487 return;
5489 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5490 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5491 constructor_elements = 0;
5492 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5494 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5495 /* Skip any nameless bit fields at the beginning. */
5496 while (constructor_unfilled_fields != 0
5497 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5498 && DECL_NAME (constructor_unfilled_fields) == 0)
5499 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5502 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5504 if (TYPE_DOMAIN (constructor_type))
5505 constructor_unfilled_index
5506 = convert (bitsizetype,
5507 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5508 else
5509 constructor_unfilled_index = bitsize_zero_node;
5511 constructor_incremental = 0;
5514 /* Build AVL tree from a string constant. */
5516 static void
5517 set_nonincremental_init_from_string (tree str)
5519 tree value, purpose, type;
5520 HOST_WIDE_INT val[2];
5521 const char *p, *end;
5522 int byte, wchar_bytes, charwidth, bitpos;
5524 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5526 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5527 == TYPE_PRECISION (char_type_node))
5528 wchar_bytes = 1;
5529 else
5531 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5532 == TYPE_PRECISION (wchar_type_node));
5533 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5535 charwidth = TYPE_PRECISION (char_type_node);
5536 type = TREE_TYPE (constructor_type);
5537 p = TREE_STRING_POINTER (str);
5538 end = p + TREE_STRING_LENGTH (str);
5540 for (purpose = bitsize_zero_node;
5541 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5542 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5544 if (wchar_bytes == 1)
5546 val[1] = (unsigned char) *p++;
5547 val[0] = 0;
5549 else
5551 val[0] = 0;
5552 val[1] = 0;
5553 for (byte = 0; byte < wchar_bytes; byte++)
5555 if (BYTES_BIG_ENDIAN)
5556 bitpos = (wchar_bytes - byte - 1) * charwidth;
5557 else
5558 bitpos = byte * charwidth;
5559 val[bitpos < HOST_BITS_PER_WIDE_INT]
5560 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5561 << (bitpos % HOST_BITS_PER_WIDE_INT);
5565 if (!TYPE_UNSIGNED (type))
5567 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5568 if (bitpos < HOST_BITS_PER_WIDE_INT)
5570 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5572 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5573 val[0] = -1;
5576 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5578 if (val[1] < 0)
5579 val[0] = -1;
5581 else if (val[0] & (((HOST_WIDE_INT) 1)
5582 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5583 val[0] |= ((HOST_WIDE_INT) -1)
5584 << (bitpos - HOST_BITS_PER_WIDE_INT);
5587 value = build_int_cst_wide (type, val[1], val[0]);
5588 add_pending_init (purpose, value);
5591 constructor_incremental = 0;
5594 /* Return value of FIELD in pending initializer or zero if the field was
5595 not initialized yet. */
5597 static tree
5598 find_init_member (tree field)
5600 struct init_node *p;
5602 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5604 if (constructor_incremental
5605 && tree_int_cst_lt (field, constructor_unfilled_index))
5606 set_nonincremental_init ();
5608 p = constructor_pending_elts;
5609 while (p)
5611 if (tree_int_cst_lt (field, p->purpose))
5612 p = p->left;
5613 else if (tree_int_cst_lt (p->purpose, field))
5614 p = p->right;
5615 else
5616 return p->value;
5619 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5621 tree bitpos = bit_position (field);
5623 if (constructor_incremental
5624 && (!constructor_unfilled_fields
5625 || tree_int_cst_lt (bitpos,
5626 bit_position (constructor_unfilled_fields))))
5627 set_nonincremental_init ();
5629 p = constructor_pending_elts;
5630 while (p)
5632 if (field == p->purpose)
5633 return p->value;
5634 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5635 p = p->left;
5636 else
5637 p = p->right;
5640 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5642 if (constructor_elements
5643 && TREE_PURPOSE (constructor_elements) == field)
5644 return TREE_VALUE (constructor_elements);
5646 return 0;
5649 /* "Output" the next constructor element.
5650 At top level, really output it to assembler code now.
5651 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5652 TYPE is the data type that the containing data type wants here.
5653 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5654 If VALUE is a string constant, STRICT_STRING is true if it is
5655 unparenthesized or we should not warn here for it being parenthesized.
5656 For other types of VALUE, STRICT_STRING is not used.
5658 PENDING if non-nil means output pending elements that belong
5659 right after this element. (PENDING is normally 1;
5660 it is 0 while outputting pending elements, to avoid recursion.) */
5662 static void
5663 output_init_element (tree value, bool strict_string, tree type, tree field,
5664 int pending)
5666 if (type == error_mark_node)
5668 constructor_erroneous = 1;
5669 return;
5671 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5672 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5673 && !(TREE_CODE (value) == STRING_CST
5674 && TREE_CODE (type) == ARRAY_TYPE
5675 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5676 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5677 TYPE_MAIN_VARIANT (type))))
5678 value = default_conversion (value);
5680 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5681 && require_constant_value && !flag_isoc99 && pending)
5683 /* As an extension, allow initializing objects with static storage
5684 duration with compound literals (which are then treated just as
5685 the brace enclosed list they contain). */
5686 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5687 value = DECL_INITIAL (decl);
5690 if (value == error_mark_node)
5691 constructor_erroneous = 1;
5692 else if (!TREE_CONSTANT (value))
5693 constructor_constant = 0;
5694 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5695 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5696 || TREE_CODE (constructor_type) == UNION_TYPE)
5697 && DECL_C_BIT_FIELD (field)
5698 && TREE_CODE (value) != INTEGER_CST))
5699 constructor_simple = 0;
5701 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5703 if (require_constant_value)
5705 error_init ("initializer element is not constant");
5706 value = error_mark_node;
5708 else if (require_constant_elements)
5709 pedwarn ("initializer element is not computable at load time");
5712 /* If this field is empty (and not at the end of structure),
5713 don't do anything other than checking the initializer. */
5714 if (field
5715 && (TREE_TYPE (field) == error_mark_node
5716 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5717 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5718 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5719 || TREE_CHAIN (field)))))
5720 return;
5722 value = digest_init (type, value, strict_string, require_constant_value);
5723 if (value == error_mark_node)
5725 constructor_erroneous = 1;
5726 return;
5729 /* If this element doesn't come next in sequence,
5730 put it on constructor_pending_elts. */
5731 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5732 && (!constructor_incremental
5733 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5735 if (constructor_incremental
5736 && tree_int_cst_lt (field, constructor_unfilled_index))
5737 set_nonincremental_init ();
5739 add_pending_init (field, value);
5740 return;
5742 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5743 && (!constructor_incremental
5744 || field != constructor_unfilled_fields))
5746 /* We do this for records but not for unions. In a union,
5747 no matter which field is specified, it can be initialized
5748 right away since it starts at the beginning of the union. */
5749 if (constructor_incremental)
5751 if (!constructor_unfilled_fields)
5752 set_nonincremental_init ();
5753 else
5755 tree bitpos, unfillpos;
5757 bitpos = bit_position (field);
5758 unfillpos = bit_position (constructor_unfilled_fields);
5760 if (tree_int_cst_lt (bitpos, unfillpos))
5761 set_nonincremental_init ();
5765 add_pending_init (field, value);
5766 return;
5768 else if (TREE_CODE (constructor_type) == UNION_TYPE
5769 && constructor_elements)
5771 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5772 warning_init ("initialized field with side-effects overwritten");
5774 /* We can have just one union field set. */
5775 constructor_elements = 0;
5778 /* Otherwise, output this element either to
5779 constructor_elements or to the assembler file. */
5781 if (field && TREE_CODE (field) == INTEGER_CST)
5782 field = copy_node (field);
5783 constructor_elements
5784 = tree_cons (field, value, constructor_elements);
5786 /* Advance the variable that indicates sequential elements output. */
5787 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5788 constructor_unfilled_index
5789 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5790 bitsize_one_node);
5791 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5793 constructor_unfilled_fields
5794 = TREE_CHAIN (constructor_unfilled_fields);
5796 /* Skip any nameless bit fields. */
5797 while (constructor_unfilled_fields != 0
5798 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5799 && DECL_NAME (constructor_unfilled_fields) == 0)
5800 constructor_unfilled_fields =
5801 TREE_CHAIN (constructor_unfilled_fields);
5803 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5804 constructor_unfilled_fields = 0;
5806 /* Now output any pending elements which have become next. */
5807 if (pending)
5808 output_pending_init_elements (0);
5811 /* Output any pending elements which have become next.
5812 As we output elements, constructor_unfilled_{fields,index}
5813 advances, which may cause other elements to become next;
5814 if so, they too are output.
5816 If ALL is 0, we return when there are
5817 no more pending elements to output now.
5819 If ALL is 1, we output space as necessary so that
5820 we can output all the pending elements. */
5822 static void
5823 output_pending_init_elements (int all)
5825 struct init_node *elt = constructor_pending_elts;
5826 tree next;
5828 retry:
5830 /* Look through the whole pending tree.
5831 If we find an element that should be output now,
5832 output it. Otherwise, set NEXT to the element
5833 that comes first among those still pending. */
5835 next = 0;
5836 while (elt)
5838 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5840 if (tree_int_cst_equal (elt->purpose,
5841 constructor_unfilled_index))
5842 output_init_element (elt->value, true,
5843 TREE_TYPE (constructor_type),
5844 constructor_unfilled_index, 0);
5845 else if (tree_int_cst_lt (constructor_unfilled_index,
5846 elt->purpose))
5848 /* Advance to the next smaller node. */
5849 if (elt->left)
5850 elt = elt->left;
5851 else
5853 /* We have reached the smallest node bigger than the
5854 current unfilled index. Fill the space first. */
5855 next = elt->purpose;
5856 break;
5859 else
5861 /* Advance to the next bigger node. */
5862 if (elt->right)
5863 elt = elt->right;
5864 else
5866 /* We have reached the biggest node in a subtree. Find
5867 the parent of it, which is the next bigger node. */
5868 while (elt->parent && elt->parent->right == elt)
5869 elt = elt->parent;
5870 elt = elt->parent;
5871 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5872 elt->purpose))
5874 next = elt->purpose;
5875 break;
5880 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5881 || TREE_CODE (constructor_type) == UNION_TYPE)
5883 tree ctor_unfilled_bitpos, elt_bitpos;
5885 /* If the current record is complete we are done. */
5886 if (constructor_unfilled_fields == 0)
5887 break;
5889 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5890 elt_bitpos = bit_position (elt->purpose);
5891 /* We can't compare fields here because there might be empty
5892 fields in between. */
5893 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5895 constructor_unfilled_fields = elt->purpose;
5896 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5897 elt->purpose, 0);
5899 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5901 /* Advance to the next smaller node. */
5902 if (elt->left)
5903 elt = elt->left;
5904 else
5906 /* We have reached the smallest node bigger than the
5907 current unfilled field. Fill the space first. */
5908 next = elt->purpose;
5909 break;
5912 else
5914 /* Advance to the next bigger node. */
5915 if (elt->right)
5916 elt = elt->right;
5917 else
5919 /* We have reached the biggest node in a subtree. Find
5920 the parent of it, which is the next bigger node. */
5921 while (elt->parent && elt->parent->right == elt)
5922 elt = elt->parent;
5923 elt = elt->parent;
5924 if (elt
5925 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5926 bit_position (elt->purpose))))
5928 next = elt->purpose;
5929 break;
5936 /* Ordinarily return, but not if we want to output all
5937 and there are elements left. */
5938 if (!(all && next != 0))
5939 return;
5941 /* If it's not incremental, just skip over the gap, so that after
5942 jumping to retry we will output the next successive element. */
5943 if (TREE_CODE (constructor_type) == RECORD_TYPE
5944 || TREE_CODE (constructor_type) == UNION_TYPE)
5945 constructor_unfilled_fields = next;
5946 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5947 constructor_unfilled_index = next;
5949 /* ELT now points to the node in the pending tree with the next
5950 initializer to output. */
5951 goto retry;
5954 /* Add one non-braced element to the current constructor level.
5955 This adjusts the current position within the constructor's type.
5956 This may also start or terminate implicit levels
5957 to handle a partly-braced initializer.
5959 Once this has found the correct level for the new element,
5960 it calls output_init_element. */
5962 void
5963 process_init_element (struct c_expr value)
5965 tree orig_value = value.value;
5966 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5967 bool strict_string = value.original_code == STRING_CST;
5969 designator_depth = 0;
5970 designator_errorneous = 0;
5972 /* Handle superfluous braces around string cst as in
5973 char x[] = {"foo"}; */
5974 if (string_flag
5975 && constructor_type
5976 && TREE_CODE (constructor_type) == ARRAY_TYPE
5977 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5978 && integer_zerop (constructor_unfilled_index))
5980 if (constructor_stack->replacement_value.value)
5981 error_init ("excess elements in char array initializer");
5982 constructor_stack->replacement_value = value;
5983 return;
5986 if (constructor_stack->replacement_value.value != 0)
5988 error_init ("excess elements in struct initializer");
5989 return;
5992 /* Ignore elements of a brace group if it is entirely superfluous
5993 and has already been diagnosed. */
5994 if (constructor_type == 0)
5995 return;
5997 /* If we've exhausted any levels that didn't have braces,
5998 pop them now. */
5999 while (constructor_stack->implicit)
6001 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6002 || TREE_CODE (constructor_type) == UNION_TYPE)
6003 && constructor_fields == 0)
6004 process_init_element (pop_init_level (1));
6005 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6006 && (constructor_max_index == 0
6007 || tree_int_cst_lt (constructor_max_index,
6008 constructor_index)))
6009 process_init_element (pop_init_level (1));
6010 else
6011 break;
6014 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6015 if (constructor_range_stack)
6017 /* If value is a compound literal and we'll be just using its
6018 content, don't put it into a SAVE_EXPR. */
6019 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6020 || !require_constant_value
6021 || flag_isoc99)
6022 value.value = save_expr (value.value);
6025 while (1)
6027 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6029 tree fieldtype;
6030 enum tree_code fieldcode;
6032 if (constructor_fields == 0)
6034 pedwarn_init ("excess elements in struct initializer");
6035 break;
6038 fieldtype = TREE_TYPE (constructor_fields);
6039 if (fieldtype != error_mark_node)
6040 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6041 fieldcode = TREE_CODE (fieldtype);
6043 /* Error for non-static initialization of a flexible array member. */
6044 if (fieldcode == ARRAY_TYPE
6045 && !require_constant_value
6046 && TYPE_SIZE (fieldtype) == NULL_TREE
6047 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6049 error_init ("non-static initialization of a flexible array member");
6050 break;
6053 /* Accept a string constant to initialize a subarray. */
6054 if (value.value != 0
6055 && fieldcode == ARRAY_TYPE
6056 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6057 && string_flag)
6058 value.value = orig_value;
6059 /* Otherwise, if we have come to a subaggregate,
6060 and we don't have an element of its type, push into it. */
6061 else if (value.value != 0 && !constructor_no_implicit
6062 && value.value != error_mark_node
6063 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6064 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6065 || fieldcode == UNION_TYPE))
6067 push_init_level (1);
6068 continue;
6071 if (value.value)
6073 push_member_name (constructor_fields);
6074 output_init_element (value.value, strict_string,
6075 fieldtype, constructor_fields, 1);
6076 RESTORE_SPELLING_DEPTH (constructor_depth);
6078 else
6079 /* Do the bookkeeping for an element that was
6080 directly output as a constructor. */
6082 /* For a record, keep track of end position of last field. */
6083 if (DECL_SIZE (constructor_fields))
6084 constructor_bit_index
6085 = size_binop (PLUS_EXPR,
6086 bit_position (constructor_fields),
6087 DECL_SIZE (constructor_fields));
6089 /* If the current field was the first one not yet written out,
6090 it isn't now, so update. */
6091 if (constructor_unfilled_fields == constructor_fields)
6093 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6094 /* Skip any nameless bit fields. */
6095 while (constructor_unfilled_fields != 0
6096 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6097 && DECL_NAME (constructor_unfilled_fields) == 0)
6098 constructor_unfilled_fields =
6099 TREE_CHAIN (constructor_unfilled_fields);
6103 constructor_fields = TREE_CHAIN (constructor_fields);
6104 /* Skip any nameless bit fields at the beginning. */
6105 while (constructor_fields != 0
6106 && DECL_C_BIT_FIELD (constructor_fields)
6107 && DECL_NAME (constructor_fields) == 0)
6108 constructor_fields = TREE_CHAIN (constructor_fields);
6110 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6112 tree fieldtype;
6113 enum tree_code fieldcode;
6115 if (constructor_fields == 0)
6117 pedwarn_init ("excess elements in union initializer");
6118 break;
6121 fieldtype = TREE_TYPE (constructor_fields);
6122 if (fieldtype != error_mark_node)
6123 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6124 fieldcode = TREE_CODE (fieldtype);
6126 /* Warn that traditional C rejects initialization of unions.
6127 We skip the warning if the value is zero. This is done
6128 under the assumption that the zero initializer in user
6129 code appears conditioned on e.g. __STDC__ to avoid
6130 "missing initializer" warnings and relies on default
6131 initialization to zero in the traditional C case.
6132 We also skip the warning if the initializer is designated,
6133 again on the assumption that this must be conditional on
6134 __STDC__ anyway (and we've already complained about the
6135 member-designator already). */
6136 if (warn_traditional && !in_system_header && !constructor_designated
6137 && !(value.value && (integer_zerop (value.value)
6138 || real_zerop (value.value))))
6139 warning ("traditional C rejects initialization of unions");
6141 /* Accept a string constant to initialize a subarray. */
6142 if (value.value != 0
6143 && fieldcode == ARRAY_TYPE
6144 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6145 && string_flag)
6146 value.value = orig_value;
6147 /* Otherwise, if we have come to a subaggregate,
6148 and we don't have an element of its type, push into it. */
6149 else if (value.value != 0 && !constructor_no_implicit
6150 && value.value != error_mark_node
6151 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6152 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6153 || fieldcode == UNION_TYPE))
6155 push_init_level (1);
6156 continue;
6159 if (value.value)
6161 push_member_name (constructor_fields);
6162 output_init_element (value.value, strict_string,
6163 fieldtype, constructor_fields, 1);
6164 RESTORE_SPELLING_DEPTH (constructor_depth);
6166 else
6167 /* Do the bookkeeping for an element that was
6168 directly output as a constructor. */
6170 constructor_bit_index = DECL_SIZE (constructor_fields);
6171 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6174 constructor_fields = 0;
6176 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6178 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6179 enum tree_code eltcode = TREE_CODE (elttype);
6181 /* Accept a string constant to initialize a subarray. */
6182 if (value.value != 0
6183 && eltcode == ARRAY_TYPE
6184 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6185 && string_flag)
6186 value.value = orig_value;
6187 /* Otherwise, if we have come to a subaggregate,
6188 and we don't have an element of its type, push into it. */
6189 else if (value.value != 0 && !constructor_no_implicit
6190 && value.value != error_mark_node
6191 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6192 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6193 || eltcode == UNION_TYPE))
6195 push_init_level (1);
6196 continue;
6199 if (constructor_max_index != 0
6200 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6201 || integer_all_onesp (constructor_max_index)))
6203 pedwarn_init ("excess elements in array initializer");
6204 break;
6207 /* Now output the actual element. */
6208 if (value.value)
6210 push_array_bounds (tree_low_cst (constructor_index, 0));
6211 output_init_element (value.value, strict_string,
6212 elttype, constructor_index, 1);
6213 RESTORE_SPELLING_DEPTH (constructor_depth);
6216 constructor_index
6217 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6219 if (!value.value)
6220 /* If we are doing the bookkeeping for an element that was
6221 directly output as a constructor, we must update
6222 constructor_unfilled_index. */
6223 constructor_unfilled_index = constructor_index;
6225 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6227 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6229 /* Do a basic check of initializer size. Note that vectors
6230 always have a fixed size derived from their type. */
6231 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6233 pedwarn_init ("excess elements in vector initializer");
6234 break;
6237 /* Now output the actual element. */
6238 if (value.value)
6239 output_init_element (value.value, strict_string,
6240 elttype, constructor_index, 1);
6242 constructor_index
6243 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6245 if (!value.value)
6246 /* If we are doing the bookkeeping for an element that was
6247 directly output as a constructor, we must update
6248 constructor_unfilled_index. */
6249 constructor_unfilled_index = constructor_index;
6252 /* Handle the sole element allowed in a braced initializer
6253 for a scalar variable. */
6254 else if (constructor_fields == 0)
6256 pedwarn_init ("excess elements in scalar initializer");
6257 break;
6259 else
6261 if (value.value)
6262 output_init_element (value.value, strict_string,
6263 constructor_type, NULL_TREE, 1);
6264 constructor_fields = 0;
6267 /* Handle range initializers either at this level or anywhere higher
6268 in the designator stack. */
6269 if (constructor_range_stack)
6271 struct constructor_range_stack *p, *range_stack;
6272 int finish = 0;
6274 range_stack = constructor_range_stack;
6275 constructor_range_stack = 0;
6276 while (constructor_stack != range_stack->stack)
6278 gcc_assert (constructor_stack->implicit);
6279 process_init_element (pop_init_level (1));
6281 for (p = range_stack;
6282 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6283 p = p->prev)
6285 gcc_assert (constructor_stack->implicit);
6286 process_init_element (pop_init_level (1));
6289 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6290 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6291 finish = 1;
6293 while (1)
6295 constructor_index = p->index;
6296 constructor_fields = p->fields;
6297 if (finish && p->range_end && p->index == p->range_start)
6299 finish = 0;
6300 p->prev = 0;
6302 p = p->next;
6303 if (!p)
6304 break;
6305 push_init_level (2);
6306 p->stack = constructor_stack;
6307 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6308 p->index = p->range_start;
6311 if (!finish)
6312 constructor_range_stack = range_stack;
6313 continue;
6316 break;
6319 constructor_range_stack = 0;
6322 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6323 (guaranteed to be 'volatile' or null) and ARGS (represented using
6324 an ASM_EXPR node). */
6325 tree
6326 build_asm_stmt (tree cv_qualifier, tree args)
6328 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6329 ASM_VOLATILE_P (args) = 1;
6330 return add_stmt (args);
6333 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6334 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6335 SIMPLE indicates whether there was anything at all after the
6336 string in the asm expression -- asm("blah") and asm("blah" : )
6337 are subtly different. We use a ASM_EXPR node to represent this. */
6338 tree
6339 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6340 bool simple)
6342 tree tail;
6343 tree args;
6344 int i;
6345 const char *constraint;
6346 bool allows_mem, allows_reg, is_inout;
6347 int ninputs;
6348 int noutputs;
6350 ninputs = list_length (inputs);
6351 noutputs = list_length (outputs);
6353 /* Remove output conversions that change the type but not the mode. */
6354 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6356 tree output = TREE_VALUE (tail);
6357 STRIP_NOPS (output);
6358 TREE_VALUE (tail) = output;
6359 lvalue_or_else (output, lv_asm);
6361 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6363 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
6364 &allows_mem, &allows_reg, &is_inout))
6366 /* By marking this operand as erroneous, we will not try
6367 to process this operand again in expand_asm_operands. */
6368 TREE_VALUE (tail) = error_mark_node;
6369 continue;
6372 /* If the operand is a DECL that is going to end up in
6373 memory, assume it is addressable. This is a bit more
6374 conservative than it would ideally be; the exact test is
6375 buried deep in expand_asm_operands and depends on the
6376 DECL_RTL for the OPERAND -- which we don't have at this
6377 point. */
6378 if (!allows_reg && DECL_P (output))
6379 c_mark_addressable (output);
6382 /* Perform default conversions on array and function inputs.
6383 Don't do this for other types as it would screw up operands
6384 expected to be in memory. */
6385 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6386 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6388 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6390 /* Simple asm statements are treated as volatile. */
6391 if (simple)
6393 ASM_VOLATILE_P (args) = 1;
6394 ASM_INPUT_P (args) = 1;
6396 return args;
6399 /* Generate a goto statement to LABEL. */
6401 tree
6402 c_finish_goto_label (tree label)
6404 tree decl = lookup_label (label);
6405 if (!decl)
6406 return NULL_TREE;
6408 TREE_USED (decl) = 1;
6409 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6412 /* Generate a computed goto statement to EXPR. */
6414 tree
6415 c_finish_goto_ptr (tree expr)
6417 if (pedantic)
6418 pedwarn ("ISO C forbids %<goto *expr;%>");
6419 expr = convert (ptr_type_node, expr);
6420 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6423 /* Generate a C `return' statement. RETVAL is the expression for what
6424 to return, or a null pointer for `return;' with no value. */
6426 tree
6427 c_finish_return (tree retval)
6429 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6431 if (TREE_THIS_VOLATILE (current_function_decl))
6432 warning ("function declared %<noreturn%> has a %<return%> statement");
6434 if (!retval)
6436 current_function_returns_null = 1;
6437 if ((warn_return_type || flag_isoc99)
6438 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6439 pedwarn_c99 ("%<return%> with no value, in "
6440 "function returning non-void");
6442 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6444 current_function_returns_null = 1;
6445 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6446 pedwarn ("%<return%> with a value, in function returning void");
6448 else
6450 tree t = convert_for_assignment (valtype, retval, ic_return,
6451 NULL_TREE, NULL_TREE, 0);
6452 tree res = DECL_RESULT (current_function_decl);
6453 tree inner;
6455 current_function_returns_value = 1;
6456 if (t == error_mark_node)
6457 return NULL_TREE;
6459 inner = t = convert (TREE_TYPE (res), t);
6461 /* Strip any conversions, additions, and subtractions, and see if
6462 we are returning the address of a local variable. Warn if so. */
6463 while (1)
6465 switch (TREE_CODE (inner))
6467 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6468 case PLUS_EXPR:
6469 inner = TREE_OPERAND (inner, 0);
6470 continue;
6472 case MINUS_EXPR:
6473 /* If the second operand of the MINUS_EXPR has a pointer
6474 type (or is converted from it), this may be valid, so
6475 don't give a warning. */
6477 tree op1 = TREE_OPERAND (inner, 1);
6479 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6480 && (TREE_CODE (op1) == NOP_EXPR
6481 || TREE_CODE (op1) == NON_LVALUE_EXPR
6482 || TREE_CODE (op1) == CONVERT_EXPR))
6483 op1 = TREE_OPERAND (op1, 0);
6485 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6486 break;
6488 inner = TREE_OPERAND (inner, 0);
6489 continue;
6492 case ADDR_EXPR:
6493 inner = TREE_OPERAND (inner, 0);
6495 while (REFERENCE_CLASS_P (inner)
6496 && TREE_CODE (inner) != INDIRECT_REF)
6497 inner = TREE_OPERAND (inner, 0);
6499 if (DECL_P (inner)
6500 && !DECL_EXTERNAL (inner)
6501 && !TREE_STATIC (inner)
6502 && DECL_CONTEXT (inner) == current_function_decl)
6503 warning ("function returns address of local variable");
6504 break;
6506 default:
6507 break;
6510 break;
6513 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6516 return add_stmt (build_stmt (RETURN_EXPR, retval));
6519 struct c_switch {
6520 /* The SWITCH_STMT being built. */
6521 tree switch_stmt;
6523 /* The original type of the testing expression, i.e. before the
6524 default conversion is applied. */
6525 tree orig_type;
6527 /* A splay-tree mapping the low element of a case range to the high
6528 element, or NULL_TREE if there is no high element. Used to
6529 determine whether or not a new case label duplicates an old case
6530 label. We need a tree, rather than simply a hash table, because
6531 of the GNU case range extension. */
6532 splay_tree cases;
6534 /* The next node on the stack. */
6535 struct c_switch *next;
6538 /* A stack of the currently active switch statements. The innermost
6539 switch statement is on the top of the stack. There is no need to
6540 mark the stack for garbage collection because it is only active
6541 during the processing of the body of a function, and we never
6542 collect at that point. */
6544 struct c_switch *c_switch_stack;
6546 /* Start a C switch statement, testing expression EXP. Return the new
6547 SWITCH_STMT. */
6549 tree
6550 c_start_case (tree exp)
6552 enum tree_code code;
6553 tree type, orig_type = error_mark_node;
6554 struct c_switch *cs;
6556 if (exp != error_mark_node)
6558 code = TREE_CODE (TREE_TYPE (exp));
6559 orig_type = TREE_TYPE (exp);
6561 if (!INTEGRAL_TYPE_P (orig_type)
6562 && code != ERROR_MARK)
6564 error ("switch quantity not an integer");
6565 exp = integer_zero_node;
6567 else
6569 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6571 if (warn_traditional && !in_system_header
6572 && (type == long_integer_type_node
6573 || type == long_unsigned_type_node))
6574 warning ("%<long%> switch expression not converted to "
6575 "%<int%> in ISO C");
6577 exp = default_conversion (exp);
6578 type = TREE_TYPE (exp);
6582 /* Add this new SWITCH_STMT to the stack. */
6583 cs = XNEW (struct c_switch);
6584 cs->switch_stmt = build_stmt ((enum tree_code) SWITCH_STMT, exp, NULL_TREE,
6585 orig_type);
6586 cs->orig_type = orig_type;
6587 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6588 cs->next = c_switch_stack;
6589 c_switch_stack = cs;
6591 return add_stmt (cs->switch_stmt);
6594 /* Process a case label. */
6596 tree
6597 do_case (tree low_value, tree high_value)
6599 tree label = NULL_TREE;
6601 if (c_switch_stack)
6603 label = c_add_case_label (c_switch_stack->cases,
6604 SWITCH_COND (c_switch_stack->switch_stmt),
6605 c_switch_stack->orig_type,
6606 low_value, high_value);
6607 if (label == error_mark_node)
6608 label = NULL_TREE;
6610 else if (low_value)
6611 error ("case label not within a switch statement");
6612 else
6613 error ("%<default%> label not within a switch statement");
6615 return label;
6618 /* Finish the switch statement. */
6620 void
6621 c_finish_case (tree body)
6623 struct c_switch *cs = c_switch_stack;
6625 SWITCH_BODY (cs->switch_stmt) = body;
6627 /* Emit warnings as needed. */
6628 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6630 /* Pop the stack. */
6631 c_switch_stack = cs->next;
6632 splay_tree_delete (cs->cases);
6633 XDELETE (cs);
6636 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6637 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6638 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6639 statement, and was not surrounded with parenthesis. */
6641 void
6642 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6643 tree else_block, bool nested_if)
6645 tree stmt;
6647 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6648 if (warn_parentheses && nested_if && else_block == NULL)
6650 tree inner_if = then_block;
6652 /* We know from the grammar productions that there is an IF nested
6653 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6654 it might not be exactly THEN_BLOCK, but should be the last
6655 non-container statement within. */
6656 while (1)
6657 switch (TREE_CODE (inner_if))
6659 case COND_EXPR:
6660 goto found;
6661 case BIND_EXPR:
6662 inner_if = BIND_EXPR_BODY (inner_if);
6663 break;
6664 case STATEMENT_LIST:
6665 inner_if = expr_last (then_block);
6666 break;
6667 case TRY_FINALLY_EXPR:
6668 case TRY_CATCH_EXPR:
6669 inner_if = TREE_OPERAND (inner_if, 0);
6670 break;
6671 default:
6672 gcc_unreachable ();
6674 found:
6676 if (COND_EXPR_ELSE (inner_if))
6677 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6678 &if_locus);
6681 /* Diagnose ";" via the special empty statement node that we create. */
6682 if (extra_warnings)
6684 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6686 if (!else_block)
6687 warning ("%Hempty body in an if-statement",
6688 EXPR_LOCUS (then_block));
6689 then_block = alloc_stmt_list ();
6691 if (else_block
6692 && TREE_CODE (else_block) == NOP_EXPR
6693 && !TREE_TYPE (else_block))
6695 warning ("%Hempty body in an else-statement",
6696 EXPR_LOCUS (else_block));
6697 else_block = alloc_stmt_list ();
6701 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6702 SET_EXPR_LOCATION (stmt, if_locus);
6703 add_stmt (stmt);
6706 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6707 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6708 is false for DO loops. INCR is the FOR increment expression. BODY is
6709 the statement controlled by the loop. BLAB is the break label. CLAB is
6710 the continue label. Everything is allowed to be NULL. */
6712 void
6713 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6714 tree blab, tree clab, bool cond_is_first)
6716 tree entry = NULL, exit = NULL, t;
6718 /* Detect do { ... } while (0) and don't generate loop construct. */
6719 if (cond && !cond_is_first && integer_zerop (cond))
6720 cond = NULL;
6721 if (cond_is_first || cond)
6723 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6725 /* If we have an exit condition, then we build an IF with gotos either
6726 out of the loop, or to the top of it. If there's no exit condition,
6727 then we just build a jump back to the top. */
6728 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6730 if (cond)
6732 /* Canonicalize the loop condition to the end. This means
6733 generating a branch to the loop condition. Reuse the
6734 continue label, if possible. */
6735 if (cond_is_first)
6737 if (incr || !clab)
6739 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6740 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6742 else
6743 t = build1 (GOTO_EXPR, void_type_node, clab);
6744 SET_EXPR_LOCATION (t, start_locus);
6745 add_stmt (t);
6748 t = build_and_jump (&blab);
6749 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6750 exit = fold (exit);
6751 if (cond_is_first)
6752 SET_EXPR_LOCATION (exit, start_locus);
6753 else
6754 SET_EXPR_LOCATION (exit, input_location);
6757 add_stmt (top);
6760 if (body)
6761 add_stmt (body);
6762 if (clab)
6763 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6764 if (incr)
6765 add_stmt (incr);
6766 if (entry)
6767 add_stmt (entry);
6768 if (exit)
6769 add_stmt (exit);
6770 if (blab)
6771 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6774 tree
6775 c_finish_bc_stmt (tree *label_p, bool is_break)
6777 tree label = *label_p;
6779 if (!label)
6780 *label_p = label = create_artificial_label ();
6781 else if (TREE_CODE (label) != LABEL_DECL)
6783 if (is_break)
6784 error ("break statement not within loop or switch");
6785 else
6786 error ("continue statement not within a loop");
6787 return NULL_TREE;
6790 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6793 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6795 static void
6796 emit_side_effect_warnings (tree expr)
6798 if (expr == error_mark_node)
6800 else if (!TREE_SIDE_EFFECTS (expr))
6802 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6803 warning ("%Hstatement with no effect",
6804 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6806 else if (warn_unused_value)
6807 warn_if_unused_value (expr, input_location);
6810 /* Process an expression as if it were a complete statement. Emit
6811 diagnostics, but do not call ADD_STMT. */
6813 tree
6814 c_process_expr_stmt (tree expr)
6816 if (!expr)
6817 return NULL_TREE;
6819 /* Do default conversion if safe and possibly important,
6820 in case within ({...}). */
6821 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6822 && (flag_isoc99 || lvalue_p (expr)))
6823 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6824 expr = default_conversion (expr);
6826 if (warn_sequence_point)
6827 verify_sequence_points (expr);
6829 if (TREE_TYPE (expr) != error_mark_node
6830 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6831 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6832 error ("expression statement has incomplete type");
6834 /* If we're not processing a statement expression, warn about unused values.
6835 Warnings for statement expressions will be emitted later, once we figure
6836 out which is the result. */
6837 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6838 && (extra_warnings || warn_unused_value))
6839 emit_side_effect_warnings (expr);
6841 /* If the expression is not of a type to which we cannot assign a line
6842 number, wrap the thing in a no-op NOP_EXPR. */
6843 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6844 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6846 if (EXPR_P (expr))
6847 SET_EXPR_LOCATION (expr, input_location);
6849 return expr;
6852 /* Emit an expression as a statement. */
6854 tree
6855 c_finish_expr_stmt (tree expr)
6857 if (expr)
6858 return add_stmt (c_process_expr_stmt (expr));
6859 else
6860 return NULL;
6863 /* Do the opposite and emit a statement as an expression. To begin,
6864 create a new binding level and return it. */
6866 tree
6867 c_begin_stmt_expr (void)
6869 tree ret;
6871 /* We must force a BLOCK for this level so that, if it is not expanded
6872 later, there is a way to turn off the entire subtree of blocks that
6873 are contained in it. */
6874 keep_next_level ();
6875 ret = c_begin_compound_stmt (true);
6877 /* Mark the current statement list as belonging to a statement list. */
6878 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6880 return ret;
6883 tree
6884 c_finish_stmt_expr (tree body)
6886 tree last, type, tmp, val;
6887 tree *last_p;
6889 body = c_end_compound_stmt (body, true);
6891 /* Locate the last statement in BODY. See c_end_compound_stmt
6892 about always returning a BIND_EXPR. */
6893 last_p = &BIND_EXPR_BODY (body);
6894 last = BIND_EXPR_BODY (body);
6896 continue_searching:
6897 if (TREE_CODE (last) == STATEMENT_LIST)
6899 tree_stmt_iterator i;
6901 /* This can happen with degenerate cases like ({ }). No value. */
6902 if (!TREE_SIDE_EFFECTS (last))
6903 return body;
6905 /* If we're supposed to generate side effects warnings, process
6906 all of the statements except the last. */
6907 if (extra_warnings || warn_unused_value)
6909 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6910 emit_side_effect_warnings (tsi_stmt (i));
6912 else
6913 i = tsi_last (last);
6914 last_p = tsi_stmt_ptr (i);
6915 last = *last_p;
6918 /* If the end of the list is exception related, then the list was split
6919 by a call to push_cleanup. Continue searching. */
6920 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6921 || TREE_CODE (last) == TRY_CATCH_EXPR)
6923 last_p = &TREE_OPERAND (last, 0);
6924 last = *last_p;
6925 goto continue_searching;
6928 /* In the case that the BIND_EXPR is not necessary, return the
6929 expression out from inside it. */
6930 if (last == error_mark_node
6931 || (last == BIND_EXPR_BODY (body)
6932 && BIND_EXPR_VARS (body) == NULL))
6933 return last;
6935 /* Extract the type of said expression. */
6936 type = TREE_TYPE (last);
6938 /* If we're not returning a value at all, then the BIND_EXPR that
6939 we already have is a fine expression to return. */
6940 if (!type || VOID_TYPE_P (type))
6941 return body;
6943 /* Now that we've located the expression containing the value, it seems
6944 silly to make voidify_wrapper_expr repeat the process. Create a
6945 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6946 tmp = create_tmp_var_raw (type, NULL);
6948 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6949 tree_expr_nonnegative_p giving up immediately. */
6950 val = last;
6951 if (TREE_CODE (val) == NOP_EXPR
6952 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
6953 val = TREE_OPERAND (val, 0);
6955 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
6956 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
6958 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
6961 /* Begin and end compound statements. This is as simple as pushing
6962 and popping new statement lists from the tree. */
6964 tree
6965 c_begin_compound_stmt (bool do_scope)
6967 tree stmt = push_stmt_list ();
6968 if (do_scope)
6969 push_scope ();
6970 return stmt;
6973 tree
6974 c_end_compound_stmt (tree stmt, bool do_scope)
6976 tree block = NULL;
6978 if (do_scope)
6980 if (c_dialect_objc ())
6981 objc_clear_super_receiver ();
6982 block = pop_scope ();
6985 stmt = pop_stmt_list (stmt);
6986 stmt = c_build_bind_expr (block, stmt);
6988 /* If this compound statement is nested immediately inside a statement
6989 expression, then force a BIND_EXPR to be created. Otherwise we'll
6990 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6991 STATEMENT_LISTs merge, and thus we can lose track of what statement
6992 was really last. */
6993 if (cur_stmt_list
6994 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6995 && TREE_CODE (stmt) != BIND_EXPR)
6997 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
6998 TREE_SIDE_EFFECTS (stmt) = 1;
7001 return stmt;
7004 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7005 when the current scope is exited. EH_ONLY is true when this is not
7006 meant to apply to normal control flow transfer. */
7008 void
7009 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7011 enum tree_code code;
7012 tree stmt, list;
7013 bool stmt_expr;
7015 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7016 stmt = build_stmt (code, NULL, cleanup);
7017 add_stmt (stmt);
7018 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7019 list = push_stmt_list ();
7020 TREE_OPERAND (stmt, 0) = list;
7021 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7024 /* Build a binary-operation expression without default conversions.
7025 CODE is the kind of expression to build.
7026 This function differs from `build' in several ways:
7027 the data type of the result is computed and recorded in it,
7028 warnings are generated if arg data types are invalid,
7029 special handling for addition and subtraction of pointers is known,
7030 and some optimization is done (operations on narrow ints
7031 are done in the narrower type when that gives the same result).
7032 Constant folding is also done before the result is returned.
7034 Note that the operands will never have enumeral types, or function
7035 or array types, because either they will have the default conversions
7036 performed or they have both just been converted to some other type in which
7037 the arithmetic is to be done. */
7039 tree
7040 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7041 int convert_p)
7043 tree type0, type1;
7044 enum tree_code code0, code1;
7045 tree op0, op1;
7047 /* Expression code to give to the expression when it is built.
7048 Normally this is CODE, which is what the caller asked for,
7049 but in some special cases we change it. */
7050 enum tree_code resultcode = code;
7052 /* Data type in which the computation is to be performed.
7053 In the simplest cases this is the common type of the arguments. */
7054 tree result_type = NULL;
7056 /* Nonzero means operands have already been type-converted
7057 in whatever way is necessary.
7058 Zero means they need to be converted to RESULT_TYPE. */
7059 int converted = 0;
7061 /* Nonzero means create the expression with this type, rather than
7062 RESULT_TYPE. */
7063 tree build_type = 0;
7065 /* Nonzero means after finally constructing the expression
7066 convert it to this type. */
7067 tree final_type = 0;
7069 /* Nonzero if this is an operation like MIN or MAX which can
7070 safely be computed in short if both args are promoted shorts.
7071 Also implies COMMON.
7072 -1 indicates a bitwise operation; this makes a difference
7073 in the exact conditions for when it is safe to do the operation
7074 in a narrower mode. */
7075 int shorten = 0;
7077 /* Nonzero if this is a comparison operation;
7078 if both args are promoted shorts, compare the original shorts.
7079 Also implies COMMON. */
7080 int short_compare = 0;
7082 /* Nonzero if this is a right-shift operation, which can be computed on the
7083 original short and then promoted if the operand is a promoted short. */
7084 int short_shift = 0;
7086 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7087 int common = 0;
7089 if (convert_p)
7091 op0 = default_conversion (orig_op0);
7092 op1 = default_conversion (orig_op1);
7094 else
7096 op0 = orig_op0;
7097 op1 = orig_op1;
7100 type0 = TREE_TYPE (op0);
7101 type1 = TREE_TYPE (op1);
7103 /* The expression codes of the data types of the arguments tell us
7104 whether the arguments are integers, floating, pointers, etc. */
7105 code0 = TREE_CODE (type0);
7106 code1 = TREE_CODE (type1);
7108 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7109 STRIP_TYPE_NOPS (op0);
7110 STRIP_TYPE_NOPS (op1);
7112 /* If an error was already reported for one of the arguments,
7113 avoid reporting another error. */
7115 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7116 return error_mark_node;
7118 switch (code)
7120 case PLUS_EXPR:
7121 /* Handle the pointer + int case. */
7122 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7123 return pointer_int_sum (PLUS_EXPR, op0, op1);
7124 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7125 return pointer_int_sum (PLUS_EXPR, op1, op0);
7126 else
7127 common = 1;
7128 break;
7130 case MINUS_EXPR:
7131 /* Subtraction of two similar pointers.
7132 We must subtract them as integers, then divide by object size. */
7133 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7134 && comp_target_types (type0, type1, 1))
7135 return pointer_diff (op0, op1);
7136 /* Handle pointer minus int. Just like pointer plus int. */
7137 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7138 return pointer_int_sum (MINUS_EXPR, op0, op1);
7139 else
7140 common = 1;
7141 break;
7143 case MULT_EXPR:
7144 common = 1;
7145 break;
7147 case TRUNC_DIV_EXPR:
7148 case CEIL_DIV_EXPR:
7149 case FLOOR_DIV_EXPR:
7150 case ROUND_DIV_EXPR:
7151 case EXACT_DIV_EXPR:
7152 /* Floating point division by zero is a legitimate way to obtain
7153 infinities and NaNs. */
7154 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7155 warning ("division by zero");
7157 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7158 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7159 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7160 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7162 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7163 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7164 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7165 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7167 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7168 resultcode = RDIV_EXPR;
7169 else
7170 /* Although it would be tempting to shorten always here, that
7171 loses on some targets, since the modulo instruction is
7172 undefined if the quotient can't be represented in the
7173 computation mode. We shorten only if unsigned or if
7174 dividing by something we know != -1. */
7175 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7176 || (TREE_CODE (op1) == INTEGER_CST
7177 && !integer_all_onesp (op1)));
7178 common = 1;
7180 break;
7182 case BIT_AND_EXPR:
7183 case BIT_IOR_EXPR:
7184 case BIT_XOR_EXPR:
7185 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7186 shorten = -1;
7187 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7188 common = 1;
7189 break;
7191 case TRUNC_MOD_EXPR:
7192 case FLOOR_MOD_EXPR:
7193 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7194 warning ("division by zero");
7196 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7198 /* Although it would be tempting to shorten always here, that loses
7199 on some targets, since the modulo instruction is undefined if the
7200 quotient can't be represented in the computation mode. We shorten
7201 only if unsigned or if dividing by something we know != -1. */
7202 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7203 || (TREE_CODE (op1) == INTEGER_CST
7204 && !integer_all_onesp (op1)));
7205 common = 1;
7207 break;
7209 case TRUTH_ANDIF_EXPR:
7210 case TRUTH_ORIF_EXPR:
7211 case TRUTH_AND_EXPR:
7212 case TRUTH_OR_EXPR:
7213 case TRUTH_XOR_EXPR:
7214 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7215 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7216 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7217 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7219 /* Result of these operations is always an int,
7220 but that does not mean the operands should be
7221 converted to ints! */
7222 result_type = integer_type_node;
7223 op0 = lang_hooks.truthvalue_conversion (op0);
7224 op1 = lang_hooks.truthvalue_conversion (op1);
7225 converted = 1;
7227 break;
7229 /* Shift operations: result has same type as first operand;
7230 always convert second operand to int.
7231 Also set SHORT_SHIFT if shifting rightward. */
7233 case RSHIFT_EXPR:
7234 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7236 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7238 if (tree_int_cst_sgn (op1) < 0)
7239 warning ("right shift count is negative");
7240 else
7242 if (!integer_zerop (op1))
7243 short_shift = 1;
7245 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7246 warning ("right shift count >= width of type");
7250 /* Use the type of the value to be shifted. */
7251 result_type = type0;
7252 /* Convert the shift-count to an integer, regardless of size
7253 of value being shifted. */
7254 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7255 op1 = convert (integer_type_node, op1);
7256 /* Avoid converting op1 to result_type later. */
7257 converted = 1;
7259 break;
7261 case LSHIFT_EXPR:
7262 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7264 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7266 if (tree_int_cst_sgn (op1) < 0)
7267 warning ("left shift count is negative");
7269 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7270 warning ("left shift count >= width of type");
7273 /* Use the type of the value to be shifted. */
7274 result_type = type0;
7275 /* Convert the shift-count to an integer, regardless of size
7276 of value being shifted. */
7277 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7278 op1 = convert (integer_type_node, op1);
7279 /* Avoid converting op1 to result_type later. */
7280 converted = 1;
7282 break;
7284 case RROTATE_EXPR:
7285 case LROTATE_EXPR:
7286 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7288 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7290 if (tree_int_cst_sgn (op1) < 0)
7291 warning ("shift count is negative");
7292 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7293 warning ("shift count >= width of type");
7296 /* Use the type of the value to be shifted. */
7297 result_type = type0;
7298 /* Convert the shift-count to an integer, regardless of size
7299 of value being shifted. */
7300 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7301 op1 = convert (integer_type_node, op1);
7302 /* Avoid converting op1 to result_type later. */
7303 converted = 1;
7305 break;
7307 case EQ_EXPR:
7308 case NE_EXPR:
7309 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7310 warning ("comparing floating point with == or != is unsafe");
7311 /* Result of comparison is always int,
7312 but don't convert the args to int! */
7313 build_type = integer_type_node;
7314 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7315 || code0 == COMPLEX_TYPE)
7316 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7317 || code1 == COMPLEX_TYPE))
7318 short_compare = 1;
7319 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7321 tree tt0 = TREE_TYPE (type0);
7322 tree tt1 = TREE_TYPE (type1);
7323 /* Anything compares with void *. void * compares with anything.
7324 Otherwise, the targets must be compatible
7325 and both must be object or both incomplete. */
7326 if (comp_target_types (type0, type1, 1))
7327 result_type = common_pointer_type (type0, type1);
7328 else if (VOID_TYPE_P (tt0))
7330 /* op0 != orig_op0 detects the case of something
7331 whose value is 0 but which isn't a valid null ptr const. */
7332 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7333 && TREE_CODE (tt1) == FUNCTION_TYPE)
7334 pedwarn ("ISO C forbids comparison of %<void *%>"
7335 " with function pointer");
7337 else if (VOID_TYPE_P (tt1))
7339 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7340 && TREE_CODE (tt0) == FUNCTION_TYPE)
7341 pedwarn ("ISO C forbids comparison of %<void *%>"
7342 " with function pointer");
7344 else
7345 pedwarn ("comparison of distinct pointer types lacks a cast");
7347 if (result_type == NULL_TREE)
7348 result_type = ptr_type_node;
7350 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7351 && integer_zerop (op1))
7352 result_type = type0;
7353 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7354 && integer_zerop (op0))
7355 result_type = type1;
7356 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7358 result_type = type0;
7359 pedwarn ("comparison between pointer and integer");
7361 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7363 result_type = type1;
7364 pedwarn ("comparison between pointer and integer");
7366 break;
7368 case MAX_EXPR:
7369 case MIN_EXPR:
7370 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7371 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7372 shorten = 1;
7373 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7375 if (comp_target_types (type0, type1, 1))
7377 result_type = common_pointer_type (type0, type1);
7378 if (pedantic
7379 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7380 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7382 else
7384 result_type = ptr_type_node;
7385 pedwarn ("comparison of distinct pointer types lacks a cast");
7388 break;
7390 case LE_EXPR:
7391 case GE_EXPR:
7392 case LT_EXPR:
7393 case GT_EXPR:
7394 build_type = integer_type_node;
7395 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7396 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7397 short_compare = 1;
7398 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7400 if (comp_target_types (type0, type1, 1))
7402 result_type = common_pointer_type (type0, type1);
7403 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7404 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7405 pedwarn ("comparison of complete and incomplete pointers");
7406 else if (pedantic
7407 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7408 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7410 else
7412 result_type = ptr_type_node;
7413 pedwarn ("comparison of distinct pointer types lacks a cast");
7416 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7417 && integer_zerop (op1))
7419 result_type = type0;
7420 if (pedantic || extra_warnings)
7421 pedwarn ("ordered comparison of pointer with integer zero");
7423 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7424 && integer_zerop (op0))
7426 result_type = type1;
7427 if (pedantic)
7428 pedwarn ("ordered comparison of pointer with integer zero");
7430 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7432 result_type = type0;
7433 pedwarn ("comparison between pointer and integer");
7435 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7437 result_type = type1;
7438 pedwarn ("comparison between pointer and integer");
7440 break;
7442 case UNORDERED_EXPR:
7443 case ORDERED_EXPR:
7444 case UNLT_EXPR:
7445 case UNLE_EXPR:
7446 case UNGT_EXPR:
7447 case UNGE_EXPR:
7448 case UNEQ_EXPR:
7449 case LTGT_EXPR:
7450 build_type = integer_type_node;
7451 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
7453 error ("unordered comparison on non-floating point argument");
7454 return error_mark_node;
7456 common = 1;
7457 break;
7459 default:
7460 break;
7463 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7464 return error_mark_node;
7466 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7467 || code0 == VECTOR_TYPE)
7469 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7470 || code1 == VECTOR_TYPE))
7472 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7474 if (shorten || common || short_compare)
7475 result_type = common_type (type0, type1);
7477 /* For certain operations (which identify themselves by shorten != 0)
7478 if both args were extended from the same smaller type,
7479 do the arithmetic in that type and then extend.
7481 shorten !=0 and !=1 indicates a bitwise operation.
7482 For them, this optimization is safe only if
7483 both args are zero-extended or both are sign-extended.
7484 Otherwise, we might change the result.
7485 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7486 but calculated in (unsigned short) it would be (unsigned short)-1. */
7488 if (shorten && none_complex)
7490 int unsigned0, unsigned1;
7491 tree arg0 = get_narrower (op0, &unsigned0);
7492 tree arg1 = get_narrower (op1, &unsigned1);
7493 /* UNS is 1 if the operation to be done is an unsigned one. */
7494 int uns = TYPE_UNSIGNED (result_type);
7495 tree type;
7497 final_type = result_type;
7499 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7500 but it *requires* conversion to FINAL_TYPE. */
7502 if ((TYPE_PRECISION (TREE_TYPE (op0))
7503 == TYPE_PRECISION (TREE_TYPE (arg0)))
7504 && TREE_TYPE (op0) != final_type)
7505 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7506 if ((TYPE_PRECISION (TREE_TYPE (op1))
7507 == TYPE_PRECISION (TREE_TYPE (arg1)))
7508 && TREE_TYPE (op1) != final_type)
7509 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7511 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7513 /* For bitwise operations, signedness of nominal type
7514 does not matter. Consider only how operands were extended. */
7515 if (shorten == -1)
7516 uns = unsigned0;
7518 /* Note that in all three cases below we refrain from optimizing
7519 an unsigned operation on sign-extended args.
7520 That would not be valid. */
7522 /* Both args variable: if both extended in same way
7523 from same width, do it in that width.
7524 Do it unsigned if args were zero-extended. */
7525 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7526 < TYPE_PRECISION (result_type))
7527 && (TYPE_PRECISION (TREE_TYPE (arg1))
7528 == TYPE_PRECISION (TREE_TYPE (arg0)))
7529 && unsigned0 == unsigned1
7530 && (unsigned0 || !uns))
7531 result_type
7532 = c_common_signed_or_unsigned_type
7533 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7534 else if (TREE_CODE (arg0) == INTEGER_CST
7535 && (unsigned1 || !uns)
7536 && (TYPE_PRECISION (TREE_TYPE (arg1))
7537 < TYPE_PRECISION (result_type))
7538 && (type
7539 = c_common_signed_or_unsigned_type (unsigned1,
7540 TREE_TYPE (arg1)),
7541 int_fits_type_p (arg0, type)))
7542 result_type = type;
7543 else if (TREE_CODE (arg1) == INTEGER_CST
7544 && (unsigned0 || !uns)
7545 && (TYPE_PRECISION (TREE_TYPE (arg0))
7546 < TYPE_PRECISION (result_type))
7547 && (type
7548 = c_common_signed_or_unsigned_type (unsigned0,
7549 TREE_TYPE (arg0)),
7550 int_fits_type_p (arg1, type)))
7551 result_type = type;
7554 /* Shifts can be shortened if shifting right. */
7556 if (short_shift)
7558 int unsigned_arg;
7559 tree arg0 = get_narrower (op0, &unsigned_arg);
7561 final_type = result_type;
7563 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7564 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7566 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7567 /* We can shorten only if the shift count is less than the
7568 number of bits in the smaller type size. */
7569 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7570 /* We cannot drop an unsigned shift after sign-extension. */
7571 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7573 /* Do an unsigned shift if the operand was zero-extended. */
7574 result_type
7575 = c_common_signed_or_unsigned_type (unsigned_arg,
7576 TREE_TYPE (arg0));
7577 /* Convert value-to-be-shifted to that type. */
7578 if (TREE_TYPE (op0) != result_type)
7579 op0 = convert (result_type, op0);
7580 converted = 1;
7584 /* Comparison operations are shortened too but differently.
7585 They identify themselves by setting short_compare = 1. */
7587 if (short_compare)
7589 /* Don't write &op0, etc., because that would prevent op0
7590 from being kept in a register.
7591 Instead, make copies of the our local variables and
7592 pass the copies by reference, then copy them back afterward. */
7593 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7594 enum tree_code xresultcode = resultcode;
7595 tree val
7596 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7598 if (val != 0)
7599 return val;
7601 op0 = xop0, op1 = xop1;
7602 converted = 1;
7603 resultcode = xresultcode;
7605 if (warn_sign_compare && skip_evaluation == 0)
7607 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7608 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7609 int unsignedp0, unsignedp1;
7610 tree primop0 = get_narrower (op0, &unsignedp0);
7611 tree primop1 = get_narrower (op1, &unsignedp1);
7613 xop0 = orig_op0;
7614 xop1 = orig_op1;
7615 STRIP_TYPE_NOPS (xop0);
7616 STRIP_TYPE_NOPS (xop1);
7618 /* Give warnings for comparisons between signed and unsigned
7619 quantities that may fail.
7621 Do the checking based on the original operand trees, so that
7622 casts will be considered, but default promotions won't be.
7624 Do not warn if the comparison is being done in a signed type,
7625 since the signed type will only be chosen if it can represent
7626 all the values of the unsigned type. */
7627 if (!TYPE_UNSIGNED (result_type))
7628 /* OK */;
7629 /* Do not warn if both operands are the same signedness. */
7630 else if (op0_signed == op1_signed)
7631 /* OK */;
7632 else
7634 tree sop, uop;
7636 if (op0_signed)
7637 sop = xop0, uop = xop1;
7638 else
7639 sop = xop1, uop = xop0;
7641 /* Do not warn if the signed quantity is an
7642 unsuffixed integer literal (or some static
7643 constant expression involving such literals or a
7644 conditional expression involving such literals)
7645 and it is non-negative. */
7646 if (tree_expr_nonnegative_p (sop))
7647 /* OK */;
7648 /* Do not warn if the comparison is an equality operation,
7649 the unsigned quantity is an integral constant, and it
7650 would fit in the result if the result were signed. */
7651 else if (TREE_CODE (uop) == INTEGER_CST
7652 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7653 && int_fits_type_p
7654 (uop, c_common_signed_type (result_type)))
7655 /* OK */;
7656 /* Do not warn if the unsigned quantity is an enumeration
7657 constant and its maximum value would fit in the result
7658 if the result were signed. */
7659 else if (TREE_CODE (uop) == INTEGER_CST
7660 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7661 && int_fits_type_p
7662 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7663 c_common_signed_type (result_type)))
7664 /* OK */;
7665 else
7666 warning ("comparison between signed and unsigned");
7669 /* Warn if two unsigned values are being compared in a size
7670 larger than their original size, and one (and only one) is the
7671 result of a `~' operator. This comparison will always fail.
7673 Also warn if one operand is a constant, and the constant
7674 does not have all bits set that are set in the ~ operand
7675 when it is extended. */
7677 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7678 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7680 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7681 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7682 &unsignedp0);
7683 else
7684 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7685 &unsignedp1);
7687 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7689 tree primop;
7690 HOST_WIDE_INT constant, mask;
7691 int unsignedp, bits;
7693 if (host_integerp (primop0, 0))
7695 primop = primop1;
7696 unsignedp = unsignedp1;
7697 constant = tree_low_cst (primop0, 0);
7699 else
7701 primop = primop0;
7702 unsignedp = unsignedp0;
7703 constant = tree_low_cst (primop1, 0);
7706 bits = TYPE_PRECISION (TREE_TYPE (primop));
7707 if (bits < TYPE_PRECISION (result_type)
7708 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7710 mask = (~(HOST_WIDE_INT) 0) << bits;
7711 if ((mask & constant) != mask)
7712 warning ("comparison of promoted ~unsigned with constant");
7715 else if (unsignedp0 && unsignedp1
7716 && (TYPE_PRECISION (TREE_TYPE (primop0))
7717 < TYPE_PRECISION (result_type))
7718 && (TYPE_PRECISION (TREE_TYPE (primop1))
7719 < TYPE_PRECISION (result_type)))
7720 warning ("comparison of promoted ~unsigned with unsigned");
7726 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7727 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7728 Then the expression will be built.
7729 It will be given type FINAL_TYPE if that is nonzero;
7730 otherwise, it will be given type RESULT_TYPE. */
7732 if (!result_type)
7734 binary_op_error (code);
7735 return error_mark_node;
7738 if (!converted)
7740 if (TREE_TYPE (op0) != result_type)
7741 op0 = convert (result_type, op0);
7742 if (TREE_TYPE (op1) != result_type)
7743 op1 = convert (result_type, op1);
7745 /* This can happen if one operand has a vector type, and the other
7746 has a different type. */
7747 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7748 return error_mark_node;
7751 if (build_type == NULL_TREE)
7752 build_type = result_type;
7755 tree result = build2 (resultcode, build_type, op0, op1);
7757 /* Treat expressions in initializers specially as they can't trap. */
7758 result = require_constant_value ? fold_initializer (result)
7759 : fold (result);
7761 if (final_type != 0)
7762 result = convert (final_type, result);
7763 return result;