2005-01-22 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / gcc / c-typeck.c
blob237afadedf49559c95212d34dad11fc94ef42d00
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
67 /* Nonzero if we've already printed a "missing braces around initializer"
68 message within this initializer. */
69 static int missing_braces_mentioned;
71 static int require_constant_value;
72 static int require_constant_elements;
74 static tree qualify_type (tree, tree);
75 static int tagged_types_tu_compatible_p (tree, tree);
76 static int comp_target_types (tree, tree, int);
77 static int function_types_compatible_p (tree, tree);
78 static int type_lists_compatible_p (tree, tree);
79 static tree decl_constant_value_for_broken_optimization (tree);
80 static tree default_function_array_conversion (tree);
81 static tree lookup_field (tree, tree);
82 static tree convert_arguments (tree, tree, tree, tree);
83 static tree pointer_diff (tree, tree);
84 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
85 int);
86 static tree valid_compound_expr_initializer (tree, tree);
87 static void push_string (const char *);
88 static void push_member_name (tree);
89 static void push_array_bounds (int);
90 static int spelling_length (void);
91 static char *print_spelling (char *);
92 static void warning_init (const char *);
93 static tree digest_init (tree, tree, bool, int);
94 static void output_init_element (tree, bool, tree, tree, int);
95 static void output_pending_init_elements (int);
96 static int set_designator (int);
97 static void push_range_stack (tree);
98 static void add_pending_init (tree, tree);
99 static void set_nonincremental_init (void);
100 static void set_nonincremental_init_from_string (tree);
101 static tree find_init_member (tree);
102 static void readonly_error (tree, enum lvalue_use);
103 static void record_maybe_used_decl (tree);
105 /* Do `exp = require_complete_type (exp);' to make sure exp
106 does not have an incomplete type. (That includes void types.) */
108 tree
109 require_complete_type (tree value)
111 tree type = TREE_TYPE (value);
113 if (value == error_mark_node || type == error_mark_node)
114 return error_mark_node;
116 /* First, detect a valid value with a complete type. */
117 if (COMPLETE_TYPE_P (type))
118 return value;
120 c_incomplete_type_error (value, type);
121 return error_mark_node;
124 /* Print an error message for invalid use of an incomplete type.
125 VALUE is the expression that was used (or 0 if that isn't known)
126 and TYPE is the type that was invalid. */
128 void
129 c_incomplete_type_error (tree value, tree type)
131 const char *type_code_string;
133 /* Avoid duplicate error message. */
134 if (TREE_CODE (type) == ERROR_MARK)
135 return;
137 if (value != 0 && (TREE_CODE (value) == VAR_DECL
138 || TREE_CODE (value) == PARM_DECL))
139 error ("%qs has an incomplete type",
140 IDENTIFIER_POINTER (DECL_NAME (value)));
141 else
143 retry:
144 /* We must print an error message. Be clever about what it says. */
146 switch (TREE_CODE (type))
148 case RECORD_TYPE:
149 type_code_string = "struct";
150 break;
152 case UNION_TYPE:
153 type_code_string = "union";
154 break;
156 case ENUMERAL_TYPE:
157 type_code_string = "enum";
158 break;
160 case VOID_TYPE:
161 error ("invalid use of void expression");
162 return;
164 case ARRAY_TYPE:
165 if (TYPE_DOMAIN (type))
167 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
169 error ("invalid use of flexible array member");
170 return;
172 type = TREE_TYPE (type);
173 goto retry;
175 error ("invalid use of array with unspecified bounds");
176 return;
178 default:
179 gcc_unreachable ();
182 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
183 error ("invalid use of undefined type %<%s %s%>",
184 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
185 else
186 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
187 error ("invalid use of incomplete typedef %qs",
188 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
192 /* Given a type, apply default promotions wrt unnamed function
193 arguments and return the new type. */
195 tree
196 c_type_promotes_to (tree type)
198 if (TYPE_MAIN_VARIANT (type) == float_type_node)
199 return double_type_node;
201 if (c_promoting_integer_type_p (type))
203 /* Preserve unsignedness if not really getting any wider. */
204 if (TYPE_UNSIGNED (type)
205 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
206 return unsigned_type_node;
207 return integer_type_node;
210 return type;
213 /* Return a variant of TYPE which has all the type qualifiers of LIKE
214 as well as those of TYPE. */
216 static tree
217 qualify_type (tree type, tree like)
219 return c_build_qualified_type (type,
220 TYPE_QUALS (type) | TYPE_QUALS (like));
223 /* Return the composite type of two compatible types.
225 We assume that comptypes has already been done and returned
226 nonzero; if that isn't so, this may crash. In particular, we
227 assume that qualifiers match. */
229 tree
230 composite_type (tree t1, tree t2)
232 enum tree_code code1;
233 enum tree_code code2;
234 tree attributes;
236 /* Save time if the two types are the same. */
238 if (t1 == t2) return t1;
240 /* If one type is nonsense, use the other. */
241 if (t1 == error_mark_node)
242 return t2;
243 if (t2 == error_mark_node)
244 return t1;
246 code1 = TREE_CODE (t1);
247 code2 = TREE_CODE (t2);
249 /* Merge the attributes. */
250 attributes = targetm.merge_type_attributes (t1, t2);
252 /* If one is an enumerated type and the other is the compatible
253 integer type, the composite type might be either of the two
254 (DR#013 question 3). For consistency, use the enumerated type as
255 the composite type. */
257 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
258 return t1;
259 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
260 return t2;
262 gcc_assert (code1 == code2);
264 switch (code1)
266 case POINTER_TYPE:
267 /* For two pointers, do this recursively on the target type. */
269 tree pointed_to_1 = TREE_TYPE (t1);
270 tree pointed_to_2 = TREE_TYPE (t2);
271 tree target = composite_type (pointed_to_1, pointed_to_2);
272 t1 = build_pointer_type (target);
273 t1 = build_type_attribute_variant (t1, attributes);
274 return qualify_type (t1, t2);
277 case ARRAY_TYPE:
279 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
281 /* We should not have any type quals on arrays at all. */
282 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
284 /* Save space: see if the result is identical to one of the args. */
285 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
286 return build_type_attribute_variant (t1, attributes);
287 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
288 return build_type_attribute_variant (t2, attributes);
290 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
291 return build_type_attribute_variant (t1, attributes);
292 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
293 return build_type_attribute_variant (t2, attributes);
295 /* Merge the element types, and have a size if either arg has one. */
296 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
297 return build_type_attribute_variant (t1, attributes);
300 case FUNCTION_TYPE:
301 /* Function types: prefer the one that specified arg types.
302 If both do, merge the arg types. Also merge the return types. */
304 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
305 tree p1 = TYPE_ARG_TYPES (t1);
306 tree p2 = TYPE_ARG_TYPES (t2);
307 int len;
308 tree newargs, n;
309 int i;
311 /* Save space: see if the result is identical to one of the args. */
312 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
313 return build_type_attribute_variant (t1, attributes);
314 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
315 return build_type_attribute_variant (t2, attributes);
317 /* Simple way if one arg fails to specify argument types. */
318 if (TYPE_ARG_TYPES (t1) == 0)
320 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
321 t1 = build_type_attribute_variant (t1, attributes);
322 return qualify_type (t1, t2);
324 if (TYPE_ARG_TYPES (t2) == 0)
326 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
327 t1 = build_type_attribute_variant (t1, attributes);
328 return qualify_type (t1, t2);
331 /* If both args specify argument types, we must merge the two
332 lists, argument by argument. */
333 /* Tell global_bindings_p to return false so that variable_size
334 doesn't abort on VLAs in parameter types. */
335 c_override_global_bindings_to_false = true;
337 len = list_length (p1);
338 newargs = 0;
340 for (i = 0; i < len; i++)
341 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
343 n = newargs;
345 for (; p1;
346 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
348 /* A null type means arg type is not specified.
349 Take whatever the other function type has. */
350 if (TREE_VALUE (p1) == 0)
352 TREE_VALUE (n) = TREE_VALUE (p2);
353 goto parm_done;
355 if (TREE_VALUE (p2) == 0)
357 TREE_VALUE (n) = TREE_VALUE (p1);
358 goto parm_done;
361 /* Given wait (union {union wait *u; int *i} *)
362 and wait (union wait *),
363 prefer union wait * as type of parm. */
364 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
365 && TREE_VALUE (p1) != TREE_VALUE (p2))
367 tree memb;
368 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
369 memb; memb = TREE_CHAIN (memb))
370 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
372 TREE_VALUE (n) = TREE_VALUE (p2);
373 if (pedantic)
374 pedwarn ("function types not truly compatible in ISO C");
375 goto parm_done;
378 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
379 && TREE_VALUE (p2) != TREE_VALUE (p1))
381 tree memb;
382 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
383 memb; memb = TREE_CHAIN (memb))
384 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
386 TREE_VALUE (n) = TREE_VALUE (p1);
387 if (pedantic)
388 pedwarn ("function types not truly compatible in ISO C");
389 goto parm_done;
392 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
393 parm_done: ;
396 c_override_global_bindings_to_false = false;
397 t1 = build_function_type (valtype, newargs);
398 t1 = qualify_type (t1, t2);
399 /* ... falls through ... */
402 default:
403 return build_type_attribute_variant (t1, attributes);
408 /* Return the type of a conditional expression between pointers to
409 possibly differently qualified versions of compatible types.
411 We assume that comp_target_types has already been done and returned
412 nonzero; if that isn't so, this may crash. */
414 static tree
415 common_pointer_type (tree t1, tree t2)
417 tree attributes;
418 tree pointed_to_1;
419 tree pointed_to_2;
420 tree target;
422 /* Save time if the two types are the same. */
424 if (t1 == t2) return t1;
426 /* If one type is nonsense, use the other. */
427 if (t1 == error_mark_node)
428 return t2;
429 if (t2 == error_mark_node)
430 return t1;
432 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
433 && TREE_CODE (t2) == POINTER_TYPE);
435 /* Merge the attributes. */
436 attributes = targetm.merge_type_attributes (t1, t2);
438 /* Find the composite type of the target types, and combine the
439 qualifiers of the two types' targets. */
440 pointed_to_1 = TREE_TYPE (t1);
441 pointed_to_2 = TREE_TYPE (t2);
442 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
443 TYPE_MAIN_VARIANT (pointed_to_2));
444 t1 = build_pointer_type (c_build_qualified_type
445 (target,
446 TYPE_QUALS (pointed_to_1) |
447 TYPE_QUALS (pointed_to_2)));
448 return build_type_attribute_variant (t1, attributes);
451 /* Return the common type for two arithmetic types under the usual
452 arithmetic conversions. The default conversions have already been
453 applied, and enumerated types converted to their compatible integer
454 types. The resulting type is unqualified and has no attributes.
456 This is the type for the result of most arithmetic operations
457 if the operands have the given two types. */
459 tree
460 common_type (tree t1, tree t2)
462 enum tree_code code1;
463 enum tree_code code2;
465 /* If one type is nonsense, use the other. */
466 if (t1 == error_mark_node)
467 return t2;
468 if (t2 == error_mark_node)
469 return t1;
471 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
472 t1 = TYPE_MAIN_VARIANT (t1);
474 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
475 t2 = TYPE_MAIN_VARIANT (t2);
477 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
478 t1 = build_type_attribute_variant (t1, NULL_TREE);
480 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
481 t2 = build_type_attribute_variant (t2, NULL_TREE);
483 /* Save time if the two types are the same. */
485 if (t1 == t2) return t1;
487 code1 = TREE_CODE (t1);
488 code2 = TREE_CODE (t2);
490 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
491 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
492 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
493 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
495 /* If one type is a vector type, return that type. (How the usual
496 arithmetic conversions apply to the vector types extension is not
497 precisely specified.) */
498 if (code1 == VECTOR_TYPE)
499 return t1;
501 if (code2 == VECTOR_TYPE)
502 return t2;
504 /* If one type is complex, form the common type of the non-complex
505 components, then make that complex. Use T1 or T2 if it is the
506 required type. */
507 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
509 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
510 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
511 tree subtype = common_type (subtype1, subtype2);
513 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
514 return t1;
515 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
516 return t2;
517 else
518 return build_complex_type (subtype);
521 /* If only one is real, use it as the result. */
523 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
524 return t1;
526 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
527 return t2;
529 /* Both real or both integers; use the one with greater precision. */
531 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
532 return t1;
533 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
534 return t2;
536 /* Same precision. Prefer long longs to longs to ints when the
537 same precision, following the C99 rules on integer type rank
538 (which are equivalent to the C90 rules for C90 types). */
540 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
541 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
542 return long_long_unsigned_type_node;
544 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
545 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
547 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
548 return long_long_unsigned_type_node;
549 else
550 return long_long_integer_type_node;
553 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
554 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
555 return long_unsigned_type_node;
557 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
558 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
560 /* But preserve unsignedness from the other type,
561 since long cannot hold all the values of an unsigned int. */
562 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
563 return long_unsigned_type_node;
564 else
565 return long_integer_type_node;
568 /* Likewise, prefer long double to double even if same size. */
569 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
570 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
571 return long_double_type_node;
573 /* Otherwise prefer the unsigned one. */
575 if (TYPE_UNSIGNED (t1))
576 return t1;
577 else
578 return t2;
581 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
582 or various other operations. Return 2 if they are compatible
583 but a warning may be needed if you use them together. */
586 comptypes (tree type1, tree type2)
588 tree t1 = type1;
589 tree t2 = type2;
590 int attrval, val;
592 /* Suppress errors caused by previously reported errors. */
594 if (t1 == t2 || !t1 || !t2
595 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
596 return 1;
598 /* If either type is the internal version of sizetype, return the
599 language version. */
600 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
601 && TYPE_ORIG_SIZE_TYPE (t1))
602 t1 = TYPE_ORIG_SIZE_TYPE (t1);
604 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
605 && TYPE_ORIG_SIZE_TYPE (t2))
606 t2 = TYPE_ORIG_SIZE_TYPE (t2);
609 /* Enumerated types are compatible with integer types, but this is
610 not transitive: two enumerated types in the same translation unit
611 are compatible with each other only if they are the same type. */
613 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
614 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
615 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
616 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
618 if (t1 == t2)
619 return 1;
621 /* Different classes of types can't be compatible. */
623 if (TREE_CODE (t1) != TREE_CODE (t2))
624 return 0;
626 /* Qualifiers must match. C99 6.7.3p9 */
628 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
629 return 0;
631 /* Allow for two different type nodes which have essentially the same
632 definition. Note that we already checked for equality of the type
633 qualifiers (just above). */
635 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
636 return 1;
638 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
639 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
640 return 0;
642 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
643 val = 0;
645 switch (TREE_CODE (t1))
647 case POINTER_TYPE:
648 /* We must give ObjC the first crack at comparing pointers, since
649 protocol qualifiers may be involved. */
650 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
651 break;
652 /* Do not remove mode or aliasing information. */
653 if (TYPE_MODE (t1) != TYPE_MODE (t2)
654 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
655 break;
656 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
657 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
658 break;
660 case FUNCTION_TYPE:
661 val = function_types_compatible_p (t1, t2);
662 break;
664 case ARRAY_TYPE:
666 tree d1 = TYPE_DOMAIN (t1);
667 tree d2 = TYPE_DOMAIN (t2);
668 bool d1_variable, d2_variable;
669 bool d1_zero, d2_zero;
670 val = 1;
672 /* Target types must match incl. qualifiers. */
673 if (TREE_TYPE (t1) != TREE_TYPE (t2)
674 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
675 return 0;
677 /* Sizes must match unless one is missing or variable. */
678 if (d1 == 0 || d2 == 0 || d1 == d2)
679 break;
681 d1_zero = !TYPE_MAX_VALUE (d1);
682 d2_zero = !TYPE_MAX_VALUE (d2);
684 d1_variable = (!d1_zero
685 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
686 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
687 d2_variable = (!d2_zero
688 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
689 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
691 if (d1_variable || d2_variable)
692 break;
693 if (d1_zero && d2_zero)
694 break;
695 if (d1_zero || d2_zero
696 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
697 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
698 val = 0;
700 break;
703 case RECORD_TYPE:
704 /* We are dealing with two distinct structs. In assorted Objective-C
705 corner cases, however, these can still be deemed equivalent. */
706 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
707 val = 1;
709 case ENUMERAL_TYPE:
710 case UNION_TYPE:
711 if (val != 1 && !same_translation_unit_p (t1, t2))
712 val = tagged_types_tu_compatible_p (t1, t2);
713 break;
715 case VECTOR_TYPE:
716 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
717 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
718 break;
720 default:
721 break;
723 return attrval == 2 && val == 1 ? 2 : val;
726 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
727 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
728 to 1 or 0 depending if the check of the pointer types is meant to
729 be reflexive or not (typically, assignments are not reflexive,
730 while comparisons are reflexive).
733 static int
734 comp_target_types (tree ttl, tree ttr, int reflexive)
736 int val;
738 /* Give objc_comptypes a crack at letting these types through. */
739 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
740 return val;
742 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
743 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
745 if (val == 2 && pedantic)
746 pedwarn ("types are not quite compatible");
747 return val;
750 /* Subroutines of `comptypes'. */
752 /* Determine whether two trees derive from the same translation unit.
753 If the CONTEXT chain ends in a null, that tree's context is still
754 being parsed, so if two trees have context chains ending in null,
755 they're in the same translation unit. */
757 same_translation_unit_p (tree t1, tree t2)
759 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
760 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
762 case tcc_declaration:
763 t1 = DECL_CONTEXT (t1); break;
764 case tcc_type:
765 t1 = TYPE_CONTEXT (t1); break;
766 case tcc_exceptional:
767 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
768 default: gcc_unreachable ();
771 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
772 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
774 case tcc_declaration:
775 t2 = DECL_CONTEXT (t2); break;
776 case tcc_type:
777 t2 = TYPE_CONTEXT (t2); break;
778 case tcc_exceptional:
779 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
780 default: gcc_unreachable ();
783 return t1 == t2;
786 /* The C standard says that two structures in different translation
787 units are compatible with each other only if the types of their
788 fields are compatible (among other things). So, consider two copies
789 of this structure: */
791 struct tagged_tu_seen {
792 const struct tagged_tu_seen * next;
793 tree t1;
794 tree t2;
797 /* Can they be compatible with each other? We choose to break the
798 recursion by allowing those types to be compatible. */
800 static const struct tagged_tu_seen * tagged_tu_seen_base;
802 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
803 compatible. If the two types are not the same (which has been
804 checked earlier), this can only happen when multiple translation
805 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
806 rules. */
808 static int
809 tagged_types_tu_compatible_p (tree t1, tree t2)
811 tree s1, s2;
812 bool needs_warning = false;
814 /* We have to verify that the tags of the types are the same. This
815 is harder than it looks because this may be a typedef, so we have
816 to go look at the original type. It may even be a typedef of a
817 typedef...
818 In the case of compiler-created builtin structs the TYPE_DECL
819 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
820 while (TYPE_NAME (t1)
821 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
822 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
823 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
825 while (TYPE_NAME (t2)
826 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
827 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
828 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
830 /* C90 didn't have the requirement that the two tags be the same. */
831 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
832 return 0;
834 /* C90 didn't say what happened if one or both of the types were
835 incomplete; we choose to follow C99 rules here, which is that they
836 are compatible. */
837 if (TYPE_SIZE (t1) == NULL
838 || TYPE_SIZE (t2) == NULL)
839 return 1;
842 const struct tagged_tu_seen * tts_i;
843 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
844 if (tts_i->t1 == t1 && tts_i->t2 == t2)
845 return 1;
848 switch (TREE_CODE (t1))
850 case ENUMERAL_TYPE:
853 /* Speed up the case where the type values are in the same order. */
854 tree tv1 = TYPE_VALUES (t1);
855 tree tv2 = TYPE_VALUES (t2);
857 if (tv1 == tv2)
858 return 1;
860 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
862 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
863 break;
864 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
865 return 0;
868 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
869 return 1;
870 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
871 return 0;
873 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
874 return 0;
876 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
878 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
879 if (s2 == NULL
880 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
881 return 0;
883 return 1;
886 case UNION_TYPE:
888 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
889 return 0;
891 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
893 bool ok = false;
894 struct tagged_tu_seen tts;
896 tts.next = tagged_tu_seen_base;
897 tts.t1 = t1;
898 tts.t2 = t2;
899 tagged_tu_seen_base = &tts;
901 if (DECL_NAME (s1) != NULL)
902 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
903 if (DECL_NAME (s1) == DECL_NAME (s2))
905 int result;
906 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
907 if (result == 0)
908 break;
909 if (result == 2)
910 needs_warning = true;
912 if (TREE_CODE (s1) == FIELD_DECL
913 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
914 DECL_FIELD_BIT_OFFSET (s2)) != 1)
915 break;
917 ok = true;
918 break;
920 tagged_tu_seen_base = tts.next;
921 if (!ok)
922 return 0;
924 return needs_warning ? 2 : 1;
927 case RECORD_TYPE:
929 struct tagged_tu_seen tts;
931 tts.next = tagged_tu_seen_base;
932 tts.t1 = t1;
933 tts.t2 = t2;
934 tagged_tu_seen_base = &tts;
936 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
937 s1 && s2;
938 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
940 int result;
941 if (TREE_CODE (s1) != TREE_CODE (s2)
942 || DECL_NAME (s1) != DECL_NAME (s2))
943 break;
944 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
945 if (result == 0)
946 break;
947 if (result == 2)
948 needs_warning = true;
950 if (TREE_CODE (s1) == FIELD_DECL
951 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
952 DECL_FIELD_BIT_OFFSET (s2)) != 1)
953 break;
955 tagged_tu_seen_base = tts.next;
956 if (s1 && s2)
957 return 0;
958 return needs_warning ? 2 : 1;
961 default:
962 gcc_unreachable ();
966 /* Return 1 if two function types F1 and F2 are compatible.
967 If either type specifies no argument types,
968 the other must specify a fixed number of self-promoting arg types.
969 Otherwise, if one type specifies only the number of arguments,
970 the other must specify that number of self-promoting arg types.
971 Otherwise, the argument types must match. */
973 static int
974 function_types_compatible_p (tree f1, tree f2)
976 tree args1, args2;
977 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
978 int val = 1;
979 int val1;
980 tree ret1, ret2;
982 ret1 = TREE_TYPE (f1);
983 ret2 = TREE_TYPE (f2);
985 /* 'volatile' qualifiers on a function's return type used to mean
986 the function is noreturn. */
987 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
988 pedwarn ("function return types not compatible due to %<volatile%>");
989 if (TYPE_VOLATILE (ret1))
990 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
991 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
992 if (TYPE_VOLATILE (ret2))
993 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
994 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
995 val = comptypes (ret1, ret2);
996 if (val == 0)
997 return 0;
999 args1 = TYPE_ARG_TYPES (f1);
1000 args2 = TYPE_ARG_TYPES (f2);
1002 /* An unspecified parmlist matches any specified parmlist
1003 whose argument types don't need default promotions. */
1005 if (args1 == 0)
1007 if (!self_promoting_args_p (args2))
1008 return 0;
1009 /* If one of these types comes from a non-prototype fn definition,
1010 compare that with the other type's arglist.
1011 If they don't match, ask for a warning (but no error). */
1012 if (TYPE_ACTUAL_ARG_TYPES (f1)
1013 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1014 val = 2;
1015 return val;
1017 if (args2 == 0)
1019 if (!self_promoting_args_p (args1))
1020 return 0;
1021 if (TYPE_ACTUAL_ARG_TYPES (f2)
1022 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1023 val = 2;
1024 return val;
1027 /* Both types have argument lists: compare them and propagate results. */
1028 val1 = type_lists_compatible_p (args1, args2);
1029 return val1 != 1 ? val1 : val;
1032 /* Check two lists of types for compatibility,
1033 returning 0 for incompatible, 1 for compatible,
1034 or 2 for compatible with warning. */
1036 static int
1037 type_lists_compatible_p (tree args1, tree args2)
1039 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1040 int val = 1;
1041 int newval = 0;
1043 while (1)
1045 if (args1 == 0 && args2 == 0)
1046 return val;
1047 /* If one list is shorter than the other,
1048 they fail to match. */
1049 if (args1 == 0 || args2 == 0)
1050 return 0;
1051 /* A null pointer instead of a type
1052 means there is supposed to be an argument
1053 but nothing is specified about what type it has.
1054 So match anything that self-promotes. */
1055 if (TREE_VALUE (args1) == 0)
1057 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1058 return 0;
1060 else if (TREE_VALUE (args2) == 0)
1062 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1063 return 0;
1065 /* If one of the lists has an error marker, ignore this arg. */
1066 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1067 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1069 else if (!(newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1070 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1072 /* Allow wait (union {union wait *u; int *i} *)
1073 and wait (union wait *) to be compatible. */
1074 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1075 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1076 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1077 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1078 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1079 TYPE_SIZE (TREE_VALUE (args2))))
1081 tree memb;
1082 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1083 memb; memb = TREE_CHAIN (memb))
1084 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1085 break;
1086 if (memb == 0)
1087 return 0;
1089 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1090 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1091 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1092 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1093 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1094 TYPE_SIZE (TREE_VALUE (args1))))
1096 tree memb;
1097 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1098 memb; memb = TREE_CHAIN (memb))
1099 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1100 break;
1101 if (memb == 0)
1102 return 0;
1104 else
1105 return 0;
1108 /* comptypes said ok, but record if it said to warn. */
1109 if (newval > val)
1110 val = newval;
1112 args1 = TREE_CHAIN (args1);
1113 args2 = TREE_CHAIN (args2);
1117 /* Compute the size to increment a pointer by. */
1119 static tree
1120 c_size_in_bytes (tree type)
1122 enum tree_code code = TREE_CODE (type);
1124 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1125 return size_one_node;
1127 if (!COMPLETE_OR_VOID_TYPE_P (type))
1129 error ("arithmetic on pointer to an incomplete type");
1130 return size_one_node;
1133 /* Convert in case a char is more than one unit. */
1134 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1135 size_int (TYPE_PRECISION (char_type_node)
1136 / BITS_PER_UNIT));
1139 /* Return either DECL or its known constant value (if it has one). */
1141 tree
1142 decl_constant_value (tree decl)
1144 if (/* Don't change a variable array bound or initial value to a constant
1145 in a place where a variable is invalid. Note that DECL_INITIAL
1146 isn't valid for a PARM_DECL. */
1147 current_function_decl != 0
1148 && TREE_CODE (decl) != PARM_DECL
1149 && !TREE_THIS_VOLATILE (decl)
1150 && TREE_READONLY (decl)
1151 && DECL_INITIAL (decl) != 0
1152 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1153 /* This is invalid if initial value is not constant.
1154 If it has either a function call, a memory reference,
1155 or a variable, then re-evaluating it could give different results. */
1156 && TREE_CONSTANT (DECL_INITIAL (decl))
1157 /* Check for cases where this is sub-optimal, even though valid. */
1158 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1159 return DECL_INITIAL (decl);
1160 return decl;
1163 /* Return either DECL or its known constant value (if it has one), but
1164 return DECL if pedantic or DECL has mode BLKmode. This is for
1165 bug-compatibility with the old behavior of decl_constant_value
1166 (before GCC 3.0); every use of this function is a bug and it should
1167 be removed before GCC 3.1. It is not appropriate to use pedantic
1168 in a way that affects optimization, and BLKmode is probably not the
1169 right test for avoiding misoptimizations either. */
1171 static tree
1172 decl_constant_value_for_broken_optimization (tree decl)
1174 if (pedantic || DECL_MODE (decl) == BLKmode)
1175 return decl;
1176 else
1177 return decl_constant_value (decl);
1181 /* Perform the default conversion of arrays and functions to pointers.
1182 Return the result of converting EXP. For any other expression, just
1183 return EXP. */
1185 static tree
1186 default_function_array_conversion (tree exp)
1188 tree orig_exp;
1189 tree type = TREE_TYPE (exp);
1190 enum tree_code code = TREE_CODE (type);
1191 int not_lvalue = 0;
1193 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1194 an lvalue.
1196 Do not use STRIP_NOPS here! It will remove conversions from pointer
1197 to integer and cause infinite recursion. */
1198 orig_exp = exp;
1199 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1200 || (TREE_CODE (exp) == NOP_EXPR
1201 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1203 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1204 not_lvalue = 1;
1205 exp = TREE_OPERAND (exp, 0);
1208 if (TREE_NO_WARNING (orig_exp))
1209 TREE_NO_WARNING (exp) = 1;
1211 if (code == FUNCTION_TYPE)
1213 return build_unary_op (ADDR_EXPR, exp, 0);
1215 if (code == ARRAY_TYPE)
1217 tree adr;
1218 tree restype = TREE_TYPE (type);
1219 tree ptrtype;
1220 int constp = 0;
1221 int volatilep = 0;
1222 int lvalue_array_p;
1224 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1226 constp = TREE_READONLY (exp);
1227 volatilep = TREE_THIS_VOLATILE (exp);
1230 if (TYPE_QUALS (type) || constp || volatilep)
1231 restype
1232 = c_build_qualified_type (restype,
1233 TYPE_QUALS (type)
1234 | (constp * TYPE_QUAL_CONST)
1235 | (volatilep * TYPE_QUAL_VOLATILE));
1237 if (TREE_CODE (exp) == INDIRECT_REF)
1238 return convert (build_pointer_type (restype),
1239 TREE_OPERAND (exp, 0));
1241 if (TREE_CODE (exp) == COMPOUND_EXPR)
1243 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1244 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1245 TREE_OPERAND (exp, 0), op1);
1248 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1249 if (!flag_isoc99 && !lvalue_array_p)
1251 /* Before C99, non-lvalue arrays do not decay to pointers.
1252 Normally, using such an array would be invalid; but it can
1253 be used correctly inside sizeof or as a statement expression.
1254 Thus, do not give an error here; an error will result later. */
1255 return exp;
1258 ptrtype = build_pointer_type (restype);
1260 if (TREE_CODE (exp) == VAR_DECL)
1262 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1263 ADDR_EXPR because it's the best way of representing what
1264 happens in C when we take the address of an array and place
1265 it in a pointer to the element type. */
1266 adr = build1 (ADDR_EXPR, ptrtype, exp);
1267 if (!c_mark_addressable (exp))
1268 return error_mark_node;
1269 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1270 return adr;
1272 /* This way is better for a COMPONENT_REF since it can
1273 simplify the offset for a component. */
1274 adr = build_unary_op (ADDR_EXPR, exp, 1);
1275 return convert (ptrtype, adr);
1277 return exp;
1280 /* Perform default promotions for C data used in expressions.
1281 Arrays and functions are converted to pointers;
1282 enumeral types or short or char, to int.
1283 In addition, manifest constants symbols are replaced by their values. */
1285 tree
1286 default_conversion (tree exp)
1288 tree orig_exp;
1289 tree type = TREE_TYPE (exp);
1290 enum tree_code code = TREE_CODE (type);
1292 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1293 return default_function_array_conversion (exp);
1295 /* Constants can be used directly unless they're not loadable. */
1296 if (TREE_CODE (exp) == CONST_DECL)
1297 exp = DECL_INITIAL (exp);
1299 /* Replace a nonvolatile const static variable with its value unless
1300 it is an array, in which case we must be sure that taking the
1301 address of the array produces consistent results. */
1302 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1304 exp = decl_constant_value_for_broken_optimization (exp);
1305 type = TREE_TYPE (exp);
1308 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1309 an lvalue.
1311 Do not use STRIP_NOPS here! It will remove conversions from pointer
1312 to integer and cause infinite recursion. */
1313 orig_exp = exp;
1314 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1315 || (TREE_CODE (exp) == NOP_EXPR
1316 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1317 exp = TREE_OPERAND (exp, 0);
1319 if (TREE_NO_WARNING (orig_exp))
1320 TREE_NO_WARNING (exp) = 1;
1322 /* Normally convert enums to int,
1323 but convert wide enums to something wider. */
1324 if (code == ENUMERAL_TYPE)
1326 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1327 TYPE_PRECISION (integer_type_node)),
1328 ((TYPE_PRECISION (type)
1329 >= TYPE_PRECISION (integer_type_node))
1330 && TYPE_UNSIGNED (type)));
1332 return convert (type, exp);
1335 if (TREE_CODE (exp) == COMPONENT_REF
1336 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1337 /* If it's thinner than an int, promote it like a
1338 c_promoting_integer_type_p, otherwise leave it alone. */
1339 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1340 TYPE_PRECISION (integer_type_node)))
1341 return convert (integer_type_node, exp);
1343 if (c_promoting_integer_type_p (type))
1345 /* Preserve unsignedness if not really getting any wider. */
1346 if (TYPE_UNSIGNED (type)
1347 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1348 return convert (unsigned_type_node, exp);
1350 return convert (integer_type_node, exp);
1353 if (code == VOID_TYPE)
1355 error ("void value not ignored as it ought to be");
1356 return error_mark_node;
1358 return exp;
1361 /* Look up COMPONENT in a structure or union DECL.
1363 If the component name is not found, returns NULL_TREE. Otherwise,
1364 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1365 stepping down the chain to the component, which is in the last
1366 TREE_VALUE of the list. Normally the list is of length one, but if
1367 the component is embedded within (nested) anonymous structures or
1368 unions, the list steps down the chain to the component. */
1370 static tree
1371 lookup_field (tree decl, tree component)
1373 tree type = TREE_TYPE (decl);
1374 tree field;
1376 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1377 to the field elements. Use a binary search on this array to quickly
1378 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1379 will always be set for structures which have many elements. */
1381 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1383 int bot, top, half;
1384 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1386 field = TYPE_FIELDS (type);
1387 bot = 0;
1388 top = TYPE_LANG_SPECIFIC (type)->s->len;
1389 while (top - bot > 1)
1391 half = (top - bot + 1) >> 1;
1392 field = field_array[bot+half];
1394 if (DECL_NAME (field) == NULL_TREE)
1396 /* Step through all anon unions in linear fashion. */
1397 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1399 field = field_array[bot++];
1400 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1401 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1403 tree anon = lookup_field (field, component);
1405 if (anon)
1406 return tree_cons (NULL_TREE, field, anon);
1410 /* Entire record is only anon unions. */
1411 if (bot > top)
1412 return NULL_TREE;
1414 /* Restart the binary search, with new lower bound. */
1415 continue;
1418 if (DECL_NAME (field) == component)
1419 break;
1420 if (DECL_NAME (field) < component)
1421 bot += half;
1422 else
1423 top = bot + half;
1426 if (DECL_NAME (field_array[bot]) == component)
1427 field = field_array[bot];
1428 else if (DECL_NAME (field) != component)
1429 return NULL_TREE;
1431 else
1433 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1435 if (DECL_NAME (field) == NULL_TREE
1436 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1437 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1439 tree anon = lookup_field (field, component);
1441 if (anon)
1442 return tree_cons (NULL_TREE, field, anon);
1445 if (DECL_NAME (field) == component)
1446 break;
1449 if (field == NULL_TREE)
1450 return NULL_TREE;
1453 return tree_cons (NULL_TREE, field, NULL_TREE);
1456 /* Make an expression to refer to the COMPONENT field of
1457 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1459 tree
1460 build_component_ref (tree datum, tree component)
1462 tree type = TREE_TYPE (datum);
1463 enum tree_code code = TREE_CODE (type);
1464 tree field = NULL;
1465 tree ref;
1467 if (!objc_is_public (datum, component))
1468 return error_mark_node;
1470 /* See if there is a field or component with name COMPONENT. */
1472 if (code == RECORD_TYPE || code == UNION_TYPE)
1474 if (!COMPLETE_TYPE_P (type))
1476 c_incomplete_type_error (NULL_TREE, type);
1477 return error_mark_node;
1480 field = lookup_field (datum, component);
1482 if (!field)
1484 error ("%qT has no member named %qs", type,
1485 IDENTIFIER_POINTER (component));
1486 return error_mark_node;
1489 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1490 This might be better solved in future the way the C++ front
1491 end does it - by giving the anonymous entities each a
1492 separate name and type, and then have build_component_ref
1493 recursively call itself. We can't do that here. */
1496 tree subdatum = TREE_VALUE (field);
1498 if (TREE_TYPE (subdatum) == error_mark_node)
1499 return error_mark_node;
1501 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1502 NULL_TREE);
1503 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1504 TREE_READONLY (ref) = 1;
1505 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1506 TREE_THIS_VOLATILE (ref) = 1;
1508 if (TREE_DEPRECATED (subdatum))
1509 warn_deprecated_use (subdatum);
1511 datum = ref;
1513 field = TREE_CHAIN (field);
1515 while (field);
1517 return ref;
1519 else if (code != ERROR_MARK)
1520 error ("request for member %qs in something not a structure or union",
1521 IDENTIFIER_POINTER (component));
1523 return error_mark_node;
1526 /* Given an expression PTR for a pointer, return an expression
1527 for the value pointed to.
1528 ERRORSTRING is the name of the operator to appear in error messages. */
1530 tree
1531 build_indirect_ref (tree ptr, const char *errorstring)
1533 tree pointer = default_conversion (ptr);
1534 tree type = TREE_TYPE (pointer);
1536 if (TREE_CODE (type) == POINTER_TYPE)
1538 if (TREE_CODE (pointer) == ADDR_EXPR
1539 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1540 == TREE_TYPE (type)))
1541 return TREE_OPERAND (pointer, 0);
1542 else
1544 tree t = TREE_TYPE (type);
1545 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1547 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1549 error ("dereferencing pointer to incomplete type");
1550 return error_mark_node;
1552 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1553 warning ("dereferencing %<void *%> pointer");
1555 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1556 so that we get the proper error message if the result is used
1557 to assign to. Also, &* is supposed to be a no-op.
1558 And ANSI C seems to specify that the type of the result
1559 should be the const type. */
1560 /* A de-reference of a pointer to const is not a const. It is valid
1561 to change it via some other pointer. */
1562 TREE_READONLY (ref) = TYPE_READONLY (t);
1563 TREE_SIDE_EFFECTS (ref)
1564 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1565 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1566 return ref;
1569 else if (TREE_CODE (pointer) != ERROR_MARK)
1570 error ("invalid type argument of %qs", errorstring);
1571 return error_mark_node;
1574 /* This handles expressions of the form "a[i]", which denotes
1575 an array reference.
1577 This is logically equivalent in C to *(a+i), but we may do it differently.
1578 If A is a variable or a member, we generate a primitive ARRAY_REF.
1579 This avoids forcing the array out of registers, and can work on
1580 arrays that are not lvalues (for example, members of structures returned
1581 by functions). */
1583 tree
1584 build_array_ref (tree array, tree index)
1586 bool swapped = false;
1587 if (TREE_TYPE (array) == error_mark_node
1588 || TREE_TYPE (index) == error_mark_node)
1589 return error_mark_node;
1591 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1592 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1594 tree temp;
1595 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1596 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1598 error ("subscripted value is neither array nor pointer");
1599 return error_mark_node;
1601 temp = array;
1602 array = index;
1603 index = temp;
1604 swapped = true;
1607 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1609 error ("array subscript is not an integer");
1610 return error_mark_node;
1613 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1615 error ("subscripted value is pointer to function");
1616 return error_mark_node;
1619 /* Subscripting with type char is likely to lose on a machine where
1620 chars are signed. So warn on any machine, but optionally. Don't
1621 warn for unsigned char since that type is safe. Don't warn for
1622 signed char because anyone who uses that must have done so
1623 deliberately. ??? Existing practice has also been to warn only
1624 when the char index is syntactically the index, not for
1625 char[array]. */
1626 if (warn_char_subscripts && !swapped
1627 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1628 warning ("array subscript has type %<char%>");
1630 /* Apply default promotions *after* noticing character types. */
1631 index = default_conversion (index);
1633 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1635 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1637 tree rval, type;
1639 /* An array that is indexed by a non-constant
1640 cannot be stored in a register; we must be able to do
1641 address arithmetic on its address.
1642 Likewise an array of elements of variable size. */
1643 if (TREE_CODE (index) != INTEGER_CST
1644 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1645 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1647 if (!c_mark_addressable (array))
1648 return error_mark_node;
1650 /* An array that is indexed by a constant value which is not within
1651 the array bounds cannot be stored in a register either; because we
1652 would get a crash in store_bit_field/extract_bit_field when trying
1653 to access a non-existent part of the register. */
1654 if (TREE_CODE (index) == INTEGER_CST
1655 && TYPE_DOMAIN (TREE_TYPE (array))
1656 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1658 if (!c_mark_addressable (array))
1659 return error_mark_node;
1662 if (pedantic)
1664 tree foo = array;
1665 while (TREE_CODE (foo) == COMPONENT_REF)
1666 foo = TREE_OPERAND (foo, 0);
1667 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1668 pedwarn ("ISO C forbids subscripting %<register%> array");
1669 else if (!flag_isoc99 && !lvalue_p (foo))
1670 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1673 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1674 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1675 /* Array ref is const/volatile if the array elements are
1676 or if the array is. */
1677 TREE_READONLY (rval)
1678 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1679 | TREE_READONLY (array));
1680 TREE_SIDE_EFFECTS (rval)
1681 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1682 | TREE_SIDE_EFFECTS (array));
1683 TREE_THIS_VOLATILE (rval)
1684 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1685 /* This was added by rms on 16 Nov 91.
1686 It fixes vol struct foo *a; a->elts[1]
1687 in an inline function.
1688 Hope it doesn't break something else. */
1689 | TREE_THIS_VOLATILE (array));
1690 return require_complete_type (fold (rval));
1692 else
1694 tree ar = default_conversion (array);
1696 if (ar == error_mark_node)
1697 return ar;
1699 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1700 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1702 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1703 "array indexing");
1707 /* Build an external reference to identifier ID. FUN indicates
1708 whether this will be used for a function call. */
1709 tree
1710 build_external_ref (tree id, int fun)
1712 tree ref;
1713 tree decl = lookup_name (id);
1715 /* In Objective-C, an instance variable (ivar) may be preferred to
1716 whatever lookup_name() found. */
1717 decl = objc_lookup_ivar (decl, id);
1719 if (decl && decl != error_mark_node)
1720 ref = decl;
1721 else if (fun)
1722 /* Implicit function declaration. */
1723 ref = implicitly_declare (id);
1724 else if (decl == error_mark_node)
1725 /* Don't complain about something that's already been
1726 complained about. */
1727 return error_mark_node;
1728 else
1730 undeclared_variable (id);
1731 return error_mark_node;
1734 if (TREE_TYPE (ref) == error_mark_node)
1735 return error_mark_node;
1737 if (TREE_DEPRECATED (ref))
1738 warn_deprecated_use (ref);
1740 if (!skip_evaluation)
1741 assemble_external (ref);
1742 TREE_USED (ref) = 1;
1744 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1746 if (!in_sizeof && !in_typeof)
1747 C_DECL_USED (ref) = 1;
1748 else if (DECL_INITIAL (ref) == 0
1749 && DECL_EXTERNAL (ref)
1750 && !TREE_PUBLIC (ref))
1751 record_maybe_used_decl (ref);
1754 if (TREE_CODE (ref) == CONST_DECL)
1756 ref = DECL_INITIAL (ref);
1757 TREE_CONSTANT (ref) = 1;
1758 TREE_INVARIANT (ref) = 1;
1760 else if (current_function_decl != 0
1761 && !DECL_FILE_SCOPE_P (current_function_decl)
1762 && (TREE_CODE (ref) == VAR_DECL
1763 || TREE_CODE (ref) == PARM_DECL
1764 || TREE_CODE (ref) == FUNCTION_DECL))
1766 tree context = decl_function_context (ref);
1768 if (context != 0 && context != current_function_decl)
1769 DECL_NONLOCAL (ref) = 1;
1772 return ref;
1775 /* Record details of decls possibly used inside sizeof or typeof. */
1776 struct maybe_used_decl
1778 /* The decl. */
1779 tree decl;
1780 /* The level seen at (in_sizeof + in_typeof). */
1781 int level;
1782 /* The next one at this level or above, or NULL. */
1783 struct maybe_used_decl *next;
1786 static struct maybe_used_decl *maybe_used_decls;
1788 /* Record that DECL, an undefined static function reference seen
1789 inside sizeof or typeof, might be used if the operand of sizeof is
1790 a VLA type or the operand of typeof is a variably modified
1791 type. */
1793 static void
1794 record_maybe_used_decl (tree decl)
1796 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1797 t->decl = decl;
1798 t->level = in_sizeof + in_typeof;
1799 t->next = maybe_used_decls;
1800 maybe_used_decls = t;
1803 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1804 USED is false, just discard them. If it is true, mark them used
1805 (if no longer inside sizeof or typeof) or move them to the next
1806 level up (if still inside sizeof or typeof). */
1808 void
1809 pop_maybe_used (bool used)
1811 struct maybe_used_decl *p = maybe_used_decls;
1812 int cur_level = in_sizeof + in_typeof;
1813 while (p && p->level > cur_level)
1815 if (used)
1817 if (cur_level == 0)
1818 C_DECL_USED (p->decl) = 1;
1819 else
1820 p->level = cur_level;
1822 p = p->next;
1824 if (!used || cur_level == 0)
1825 maybe_used_decls = p;
1828 /* Return the result of sizeof applied to EXPR. */
1830 struct c_expr
1831 c_expr_sizeof_expr (struct c_expr expr)
1833 struct c_expr ret;
1834 if (expr.value == error_mark_node)
1836 ret.value = error_mark_node;
1837 ret.original_code = ERROR_MARK;
1838 pop_maybe_used (false);
1840 else
1842 ret.value = c_sizeof (TREE_TYPE (expr.value));
1843 ret.original_code = ERROR_MARK;
1844 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1846 return ret;
1849 /* Return the result of sizeof applied to T, a structure for the type
1850 name passed to sizeof (rather than the type itself). */
1852 struct c_expr
1853 c_expr_sizeof_type (struct c_type_name *t)
1855 tree type;
1856 struct c_expr ret;
1857 type = groktypename (t);
1858 ret.value = c_sizeof (type);
1859 ret.original_code = ERROR_MARK;
1860 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1861 return ret;
1864 /* Build a function call to function FUNCTION with parameters PARAMS.
1865 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1866 TREE_VALUE of each node is a parameter-expression.
1867 FUNCTION's data type may be a function type or a pointer-to-function. */
1869 tree
1870 build_function_call (tree function, tree params)
1872 tree fntype, fundecl = 0;
1873 tree coerced_params;
1874 tree name = NULL_TREE, result;
1875 tree tem;
1877 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1878 STRIP_TYPE_NOPS (function);
1880 /* Convert anything with function type to a pointer-to-function. */
1881 if (TREE_CODE (function) == FUNCTION_DECL)
1883 name = DECL_NAME (function);
1885 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1886 (because calling an inline function does not mean the function
1887 needs to be separately compiled). */
1888 fntype = build_type_variant (TREE_TYPE (function),
1889 TREE_READONLY (function),
1890 TREE_THIS_VOLATILE (function));
1891 fundecl = function;
1892 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1894 else
1895 function = default_conversion (function);
1897 fntype = TREE_TYPE (function);
1899 if (TREE_CODE (fntype) == ERROR_MARK)
1900 return error_mark_node;
1902 if (!(TREE_CODE (fntype) == POINTER_TYPE
1903 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1905 error ("called object %qE is not a function", function);
1906 return error_mark_node;
1909 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1910 current_function_returns_abnormally = 1;
1912 /* fntype now gets the type of function pointed to. */
1913 fntype = TREE_TYPE (fntype);
1915 /* Check that the function is called through a compatible prototype.
1916 If it is not, replace the call by a trap, wrapped up in a compound
1917 expression if necessary. This has the nice side-effect to prevent
1918 the tree-inliner from generating invalid assignment trees which may
1919 blow up in the RTL expander later.
1921 ??? This doesn't work for Objective-C because objc_comptypes
1922 refuses to compare function prototypes, yet the compiler appears
1923 to build calls that are flagged as invalid by C's comptypes. */
1924 if (!c_dialect_objc ()
1925 && TREE_CODE (function) == NOP_EXPR
1926 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1927 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1928 && !comptypes (fntype, TREE_TYPE (tem)))
1930 tree return_type = TREE_TYPE (fntype);
1931 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1932 NULL_TREE);
1934 /* This situation leads to run-time undefined behavior. We can't,
1935 therefore, simply error unless we can prove that all possible
1936 executions of the program must execute the code. */
1937 warning ("function called through a non-compatible type");
1939 /* We can, however, treat "undefined" any way we please.
1940 Call abort to encourage the user to fix the program. */
1941 inform ("if this code is reached, the program will abort");
1943 if (VOID_TYPE_P (return_type))
1944 return trap;
1945 else
1947 tree rhs;
1949 if (AGGREGATE_TYPE_P (return_type))
1950 rhs = build_compound_literal (return_type,
1951 build_constructor (return_type,
1952 NULL_TREE));
1953 else
1954 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1956 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
1960 /* Convert the parameters to the types declared in the
1961 function prototype, or apply default promotions. */
1963 coerced_params
1964 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
1966 if (coerced_params == error_mark_node)
1967 return error_mark_node;
1969 /* Check that the arguments to the function are valid. */
1971 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1973 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
1974 function, coerced_params, NULL_TREE);
1975 TREE_SIDE_EFFECTS (result) = 1;
1977 if (require_constant_value)
1979 result = fold_initializer (result);
1981 if (TREE_CONSTANT (result)
1982 && (name == NULL_TREE
1983 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1984 pedwarn_init ("initializer element is not constant");
1986 else
1987 result = fold (result);
1989 if (VOID_TYPE_P (TREE_TYPE (result)))
1990 return result;
1991 return require_complete_type (result);
1994 /* Convert the argument expressions in the list VALUES
1995 to the types in the list TYPELIST. The result is a list of converted
1996 argument expressions, unless there are too few arguments in which
1997 case it is error_mark_node.
1999 If TYPELIST is exhausted, or when an element has NULL as its type,
2000 perform the default conversions.
2002 PARMLIST is the chain of parm decls for the function being called.
2003 It may be 0, if that info is not available.
2004 It is used only for generating error messages.
2006 FUNCTION is a tree for the called function. It is used only for
2007 error messages, where it is formatted with %qE.
2009 This is also where warnings about wrong number of args are generated.
2011 Both VALUES and the returned value are chains of TREE_LIST nodes
2012 with the elements of the list in the TREE_VALUE slots of those nodes. */
2014 static tree
2015 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2017 tree typetail, valtail;
2018 tree result = NULL;
2019 int parmnum;
2020 tree selector;
2022 /* Change pointer to function to the function itself for
2023 diagnostics. */
2024 if (TREE_CODE (function) == ADDR_EXPR
2025 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2026 function = TREE_OPERAND (function, 0);
2028 /* Handle an ObjC selector specially for diagnostics. */
2029 selector = objc_message_selector ();
2031 /* Scan the given expressions and types, producing individual
2032 converted arguments and pushing them on RESULT in reverse order. */
2034 for (valtail = values, typetail = typelist, parmnum = 0;
2035 valtail;
2036 valtail = TREE_CHAIN (valtail), parmnum++)
2038 tree type = typetail ? TREE_VALUE (typetail) : 0;
2039 tree val = TREE_VALUE (valtail);
2040 tree rname = function;
2041 int argnum = parmnum + 1;
2043 if (type == void_type_node)
2045 error ("too many arguments to function %qE", function);
2046 break;
2049 if (selector && argnum > 2)
2051 rname = selector;
2052 argnum -= 2;
2055 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2056 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2057 to convert automatically to a pointer. */
2058 if (TREE_CODE (val) == NON_LVALUE_EXPR)
2059 val = TREE_OPERAND (val, 0);
2061 val = default_function_array_conversion (val);
2063 val = require_complete_type (val);
2065 if (type != 0)
2067 /* Formal parm type is specified by a function prototype. */
2068 tree parmval;
2070 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2072 error ("type of formal parameter %d is incomplete", parmnum + 1);
2073 parmval = val;
2075 else
2077 /* Optionally warn about conversions that
2078 differ from the default conversions. */
2079 if (warn_conversion || warn_traditional)
2081 unsigned int formal_prec = TYPE_PRECISION (type);
2083 if (INTEGRAL_TYPE_P (type)
2084 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2085 warning ("passing argument %d of %qE as integer "
2086 "rather than floating due to prototype",
2087 argnum, rname);
2088 if (INTEGRAL_TYPE_P (type)
2089 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2090 warning ("passing argument %d of %qE as integer "
2091 "rather than complex due to prototype",
2092 argnum, rname);
2093 else if (TREE_CODE (type) == COMPLEX_TYPE
2094 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2095 warning ("passing argument %d of %qE as complex "
2096 "rather than floating due to prototype",
2097 argnum, rname);
2098 else if (TREE_CODE (type) == REAL_TYPE
2099 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2100 warning ("passing argument %d of %qE as floating "
2101 "rather than integer due to prototype",
2102 argnum, rname);
2103 else if (TREE_CODE (type) == COMPLEX_TYPE
2104 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2105 warning ("passing argument %d of %qE as complex "
2106 "rather than integer due to prototype",
2107 argnum, rname);
2108 else if (TREE_CODE (type) == REAL_TYPE
2109 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2110 warning ("passing argument %d of %qE as floating "
2111 "rather than complex due to prototype",
2112 argnum, rname);
2113 /* ??? At some point, messages should be written about
2114 conversions between complex types, but that's too messy
2115 to do now. */
2116 else if (TREE_CODE (type) == REAL_TYPE
2117 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2119 /* Warn if any argument is passed as `float',
2120 since without a prototype it would be `double'. */
2121 if (formal_prec == TYPE_PRECISION (float_type_node))
2122 warning ("passing argument %d of %qE as %<float%> "
2123 "rather than %<double%> due to prototype",
2124 argnum, rname);
2126 /* Detect integer changing in width or signedness.
2127 These warnings are only activated with
2128 -Wconversion, not with -Wtraditional. */
2129 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2130 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2132 tree would_have_been = default_conversion (val);
2133 tree type1 = TREE_TYPE (would_have_been);
2135 if (TREE_CODE (type) == ENUMERAL_TYPE
2136 && (TYPE_MAIN_VARIANT (type)
2137 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2138 /* No warning if function asks for enum
2139 and the actual arg is that enum type. */
2141 else if (formal_prec != TYPE_PRECISION (type1))
2142 warning ("passing argument %d of %qE with different "
2143 "width due to prototype", argnum, rname);
2144 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2146 /* Don't complain if the formal parameter type
2147 is an enum, because we can't tell now whether
2148 the value was an enum--even the same enum. */
2149 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2151 else if (TREE_CODE (val) == INTEGER_CST
2152 && int_fits_type_p (val, type))
2153 /* Change in signedness doesn't matter
2154 if a constant value is unaffected. */
2156 /* Likewise for a constant in a NOP_EXPR. */
2157 else if (TREE_CODE (val) == NOP_EXPR
2158 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2159 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2161 /* If the value is extended from a narrower
2162 unsigned type, it doesn't matter whether we
2163 pass it as signed or unsigned; the value
2164 certainly is the same either way. */
2165 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2166 && TYPE_UNSIGNED (TREE_TYPE (val)))
2168 else if (TYPE_UNSIGNED (type))
2169 warning ("passing argument %d of %qE as unsigned "
2170 "due to prototype", argnum, rname);
2171 else
2172 warning ("passing argument %d of %qE as signed "
2173 "due to prototype", argnum, rname);
2177 parmval = convert_for_assignment (type, val, ic_argpass,
2178 fundecl, function,
2179 parmnum + 1);
2181 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2182 && INTEGRAL_TYPE_P (type)
2183 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2184 parmval = default_conversion (parmval);
2186 result = tree_cons (NULL_TREE, parmval, result);
2188 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2189 && (TYPE_PRECISION (TREE_TYPE (val))
2190 < TYPE_PRECISION (double_type_node)))
2191 /* Convert `float' to `double'. */
2192 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2193 else
2194 /* Convert `short' and `char' to full-size `int'. */
2195 result = tree_cons (NULL_TREE, default_conversion (val), result);
2197 if (typetail)
2198 typetail = TREE_CHAIN (typetail);
2201 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2203 error ("too few arguments to function %qE", function);
2204 return error_mark_node;
2207 return nreverse (result);
2210 /* This is the entry point used by the parser
2211 for binary operators in the input.
2212 In addition to constructing the expression,
2213 we check for operands that were written with other binary operators
2214 in a way that is likely to confuse the user. */
2216 struct c_expr
2217 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2218 struct c_expr arg2)
2220 struct c_expr result;
2222 enum tree_code code1 = arg1.original_code;
2223 enum tree_code code2 = arg2.original_code;
2225 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2226 result.original_code = code;
2228 if (TREE_CODE (result.value) == ERROR_MARK)
2229 return result;
2231 /* Check for cases such as x+y<<z which users are likely
2232 to misinterpret. */
2233 if (warn_parentheses)
2235 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2237 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2238 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2239 warning ("suggest parentheses around + or - inside shift");
2242 if (code == TRUTH_ORIF_EXPR)
2244 if (code1 == TRUTH_ANDIF_EXPR
2245 || code2 == TRUTH_ANDIF_EXPR)
2246 warning ("suggest parentheses around && within ||");
2249 if (code == BIT_IOR_EXPR)
2251 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2252 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2253 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2254 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2255 warning ("suggest parentheses around arithmetic in operand of |");
2256 /* Check cases like x|y==z */
2257 if (TREE_CODE_CLASS (code1) == tcc_comparison
2258 || TREE_CODE_CLASS (code2) == tcc_comparison)
2259 warning ("suggest parentheses around comparison in operand of |");
2262 if (code == BIT_XOR_EXPR)
2264 if (code1 == BIT_AND_EXPR
2265 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2266 || code2 == BIT_AND_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_AND_EXPR)
2277 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2278 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2279 warning ("suggest parentheses around + or - in operand of &");
2280 /* Check cases like x&y==z */
2281 if (TREE_CODE_CLASS (code1) == tcc_comparison
2282 || TREE_CODE_CLASS (code2) == tcc_comparison)
2283 warning ("suggest parentheses around comparison in operand of &");
2285 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2286 if (TREE_CODE_CLASS (code) == tcc_comparison
2287 && (TREE_CODE_CLASS (code1) == tcc_comparison
2288 || TREE_CODE_CLASS (code2) == tcc_comparison))
2289 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2293 unsigned_conversion_warning (result.value, arg1.value);
2294 unsigned_conversion_warning (result.value, arg2.value);
2295 overflow_warning (result.value);
2297 return result;
2300 /* Return a tree for the difference of pointers OP0 and OP1.
2301 The resulting tree has type int. */
2303 static tree
2304 pointer_diff (tree op0, tree op1)
2306 tree restype = ptrdiff_type_node;
2308 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2309 tree con0, con1, lit0, lit1;
2310 tree orig_op1 = op1;
2312 if (pedantic || warn_pointer_arith)
2314 if (TREE_CODE (target_type) == VOID_TYPE)
2315 pedwarn ("pointer of type %<void *%> used in subtraction");
2316 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2317 pedwarn ("pointer to a function used in subtraction");
2320 /* If the conversion to ptrdiff_type does anything like widening or
2321 converting a partial to an integral mode, we get a convert_expression
2322 that is in the way to do any simplifications.
2323 (fold-const.c doesn't know that the extra bits won't be needed.
2324 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2325 different mode in place.)
2326 So first try to find a common term here 'by hand'; we want to cover
2327 at least the cases that occur in legal static initializers. */
2328 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2329 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2331 if (TREE_CODE (con0) == PLUS_EXPR)
2333 lit0 = TREE_OPERAND (con0, 1);
2334 con0 = TREE_OPERAND (con0, 0);
2336 else
2337 lit0 = integer_zero_node;
2339 if (TREE_CODE (con1) == PLUS_EXPR)
2341 lit1 = TREE_OPERAND (con1, 1);
2342 con1 = TREE_OPERAND (con1, 0);
2344 else
2345 lit1 = integer_zero_node;
2347 if (operand_equal_p (con0, con1, 0))
2349 op0 = lit0;
2350 op1 = lit1;
2354 /* First do the subtraction as integers;
2355 then drop through to build the divide operator.
2356 Do not do default conversions on the minus operator
2357 in case restype is a short type. */
2359 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2360 convert (restype, op1), 0);
2361 /* This generates an error if op1 is pointer to incomplete type. */
2362 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2363 error ("arithmetic on pointer to an incomplete type");
2365 /* This generates an error if op0 is pointer to incomplete type. */
2366 op1 = c_size_in_bytes (target_type);
2368 /* Divide by the size, in easiest possible way. */
2369 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2372 /* Construct and perhaps optimize a tree representation
2373 for a unary operation. CODE, a tree_code, specifies the operation
2374 and XARG is the operand.
2375 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2376 the default promotions (such as from short to int).
2377 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2378 allows non-lvalues; this is only used to handle conversion of non-lvalue
2379 arrays to pointers in C99. */
2381 tree
2382 build_unary_op (enum tree_code code, tree xarg, int flag)
2384 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2385 tree arg = xarg;
2386 tree argtype = 0;
2387 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2388 tree val;
2389 int noconvert = flag;
2391 if (typecode == ERROR_MARK)
2392 return error_mark_node;
2393 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2394 typecode = INTEGER_TYPE;
2396 switch (code)
2398 case CONVERT_EXPR:
2399 /* This is used for unary plus, because a CONVERT_EXPR
2400 is enough to prevent anybody from looking inside for
2401 associativity, but won't generate any code. */
2402 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2403 || typecode == COMPLEX_TYPE
2404 || typecode == VECTOR_TYPE))
2406 error ("wrong type argument to unary plus");
2407 return error_mark_node;
2409 else if (!noconvert)
2410 arg = default_conversion (arg);
2411 arg = non_lvalue (arg);
2412 break;
2414 case NEGATE_EXPR:
2415 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2416 || typecode == COMPLEX_TYPE
2417 || typecode == VECTOR_TYPE))
2419 error ("wrong type argument to unary minus");
2420 return error_mark_node;
2422 else if (!noconvert)
2423 arg = default_conversion (arg);
2424 break;
2426 case BIT_NOT_EXPR:
2427 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2429 if (!noconvert)
2430 arg = default_conversion (arg);
2432 else if (typecode == COMPLEX_TYPE)
2434 code = CONJ_EXPR;
2435 if (pedantic)
2436 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2437 if (!noconvert)
2438 arg = default_conversion (arg);
2440 else
2442 error ("wrong type argument to bit-complement");
2443 return error_mark_node;
2445 break;
2447 case ABS_EXPR:
2448 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2450 error ("wrong type argument to abs");
2451 return error_mark_node;
2453 else if (!noconvert)
2454 arg = default_conversion (arg);
2455 break;
2457 case CONJ_EXPR:
2458 /* Conjugating a real value is a no-op, but allow it anyway. */
2459 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2460 || typecode == COMPLEX_TYPE))
2462 error ("wrong type argument to conjugation");
2463 return error_mark_node;
2465 else if (!noconvert)
2466 arg = default_conversion (arg);
2467 break;
2469 case TRUTH_NOT_EXPR:
2470 if (typecode != INTEGER_TYPE
2471 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2472 && typecode != COMPLEX_TYPE
2473 /* These will convert to a pointer. */
2474 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2476 error ("wrong type argument to unary exclamation mark");
2477 return error_mark_node;
2479 arg = lang_hooks.truthvalue_conversion (arg);
2480 return invert_truthvalue (arg);
2482 case NOP_EXPR:
2483 break;
2485 case REALPART_EXPR:
2486 if (TREE_CODE (arg) == COMPLEX_CST)
2487 return TREE_REALPART (arg);
2488 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2489 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2490 else
2491 return arg;
2493 case IMAGPART_EXPR:
2494 if (TREE_CODE (arg) == COMPLEX_CST)
2495 return TREE_IMAGPART (arg);
2496 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2497 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2498 else
2499 return convert (TREE_TYPE (arg), integer_zero_node);
2501 case PREINCREMENT_EXPR:
2502 case POSTINCREMENT_EXPR:
2503 case PREDECREMENT_EXPR:
2504 case POSTDECREMENT_EXPR:
2506 /* Increment or decrement the real part of the value,
2507 and don't change the imaginary part. */
2508 if (typecode == COMPLEX_TYPE)
2510 tree real, imag;
2512 if (pedantic)
2513 pedwarn ("ISO C does not support %<++%> and %<--%>"
2514 " on complex types");
2516 arg = stabilize_reference (arg);
2517 real = build_unary_op (REALPART_EXPR, arg, 1);
2518 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2519 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2520 build_unary_op (code, real, 1), imag);
2523 /* Report invalid types. */
2525 if (typecode != POINTER_TYPE
2526 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2528 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2529 error ("wrong type argument to increment");
2530 else
2531 error ("wrong type argument to decrement");
2533 return error_mark_node;
2537 tree inc;
2538 tree result_type = TREE_TYPE (arg);
2540 arg = get_unwidened (arg, 0);
2541 argtype = TREE_TYPE (arg);
2543 /* Compute the increment. */
2545 if (typecode == POINTER_TYPE)
2547 /* If pointer target is an undefined struct,
2548 we just cannot know how to do the arithmetic. */
2549 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2551 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2552 error ("increment of pointer to unknown structure");
2553 else
2554 error ("decrement of pointer to unknown structure");
2556 else if ((pedantic || warn_pointer_arith)
2557 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2558 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2560 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2561 pedwarn ("wrong type argument to increment");
2562 else
2563 pedwarn ("wrong type argument to decrement");
2566 inc = c_size_in_bytes (TREE_TYPE (result_type));
2568 else
2569 inc = integer_one_node;
2571 inc = convert (argtype, inc);
2573 /* Complain about anything else that is not a true lvalue. */
2574 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2575 || code == POSTINCREMENT_EXPR)
2576 ? lv_increment
2577 : lv_decrement)))
2578 return error_mark_node;
2580 /* Report a read-only lvalue. */
2581 if (TREE_READONLY (arg))
2582 readonly_error (arg,
2583 ((code == PREINCREMENT_EXPR
2584 || code == POSTINCREMENT_EXPR)
2585 ? lv_increment : lv_decrement));
2587 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2588 val = boolean_increment (code, arg);
2589 else
2590 val = build2 (code, TREE_TYPE (arg), arg, inc);
2591 TREE_SIDE_EFFECTS (val) = 1;
2592 val = convert (result_type, val);
2593 if (TREE_CODE (val) != code)
2594 TREE_NO_WARNING (val) = 1;
2595 return val;
2598 case ADDR_EXPR:
2599 /* Note that this operation never does default_conversion. */
2601 /* Let &* cancel out to simplify resulting code. */
2602 if (TREE_CODE (arg) == INDIRECT_REF)
2604 /* Don't let this be an lvalue. */
2605 if (lvalue_p (TREE_OPERAND (arg, 0)))
2606 return non_lvalue (TREE_OPERAND (arg, 0));
2607 return TREE_OPERAND (arg, 0);
2610 /* For &x[y], return x+y */
2611 if (TREE_CODE (arg) == ARRAY_REF)
2613 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2614 return error_mark_node;
2615 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2616 TREE_OPERAND (arg, 1), 1);
2619 /* Anything not already handled and not a true memory reference
2620 or a non-lvalue array is an error. */
2621 else if (typecode != FUNCTION_TYPE && !flag
2622 && !lvalue_or_else (arg, lv_addressof))
2623 return error_mark_node;
2625 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2626 argtype = TREE_TYPE (arg);
2628 /* If the lvalue is const or volatile, merge that into the type
2629 to which the address will point. Note that you can't get a
2630 restricted pointer by taking the address of something, so we
2631 only have to deal with `const' and `volatile' here. */
2632 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2633 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2634 argtype = c_build_type_variant (argtype,
2635 TREE_READONLY (arg),
2636 TREE_THIS_VOLATILE (arg));
2638 if (!c_mark_addressable (arg))
2639 return error_mark_node;
2641 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2642 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2644 argtype = build_pointer_type (argtype);
2646 /* ??? Cope with user tricks that amount to offsetof. Delete this
2647 when we have proper support for integer constant expressions. */
2648 val = get_base_address (arg);
2649 if (val && TREE_CODE (val) == INDIRECT_REF
2650 && integer_zerop (TREE_OPERAND (val, 0)))
2651 return fold_convert (argtype, fold_offsetof (arg));
2653 val = build1 (ADDR_EXPR, argtype, arg);
2655 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2656 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2658 return val;
2660 default:
2661 break;
2664 if (argtype == 0)
2665 argtype = TREE_TYPE (arg);
2666 val = build1 (code, argtype, arg);
2667 return require_constant_value ? fold_initializer (val) : fold (val);
2670 /* Return nonzero if REF is an lvalue valid for this language.
2671 Lvalues can be assigned, unless their type has TYPE_READONLY.
2672 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2675 lvalue_p (tree ref)
2677 enum tree_code code = TREE_CODE (ref);
2679 switch (code)
2681 case REALPART_EXPR:
2682 case IMAGPART_EXPR:
2683 case COMPONENT_REF:
2684 return lvalue_p (TREE_OPERAND (ref, 0));
2686 case COMPOUND_LITERAL_EXPR:
2687 case STRING_CST:
2688 return 1;
2690 case INDIRECT_REF:
2691 case ARRAY_REF:
2692 case VAR_DECL:
2693 case PARM_DECL:
2694 case RESULT_DECL:
2695 case ERROR_MARK:
2696 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2697 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2699 case BIND_EXPR:
2700 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2702 default:
2703 return 0;
2707 /* Give an error for storing in something that is 'const'. */
2709 static void
2710 readonly_error (tree arg, enum lvalue_use use)
2712 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2713 /* Using this macro rather than (for example) arrays of messages
2714 ensures that all the format strings are checked at compile
2715 time. */
2716 #define READONLY_MSG(A, I, D) (use == lv_assign \
2717 ? (A) \
2718 : (use == lv_increment ? (I) : (D)))
2719 if (TREE_CODE (arg) == COMPONENT_REF)
2721 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2722 readonly_error (TREE_OPERAND (arg, 0), use);
2723 else
2724 error (READONLY_MSG (N_("assignment of read-only member %qs"),
2725 N_("increment of read-only member %qs"),
2726 N_("decrement of read-only member %qs")),
2727 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2729 else if (TREE_CODE (arg) == VAR_DECL)
2730 error (READONLY_MSG (N_("assignment of read-only variable %qs"),
2731 N_("increment of read-only variable %qs"),
2732 N_("decrement of read-only variable %qs")),
2733 IDENTIFIER_POINTER (DECL_NAME (arg)));
2734 else
2735 error (READONLY_MSG (N_("assignment of read-only location"),
2736 N_("increment of read-only location"),
2737 N_("decrement of read-only location")));
2740 /* Mark EXP saying that we need to be able to take the
2741 address of it; it should not be allocated in a register.
2742 Returns true if successful. */
2744 bool
2745 c_mark_addressable (tree exp)
2747 tree x = exp;
2749 while (1)
2750 switch (TREE_CODE (x))
2752 case COMPONENT_REF:
2753 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2755 error
2756 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2757 return false;
2760 /* ... fall through ... */
2762 case ADDR_EXPR:
2763 case ARRAY_REF:
2764 case REALPART_EXPR:
2765 case IMAGPART_EXPR:
2766 x = TREE_OPERAND (x, 0);
2767 break;
2769 case COMPOUND_LITERAL_EXPR:
2770 case CONSTRUCTOR:
2771 TREE_ADDRESSABLE (x) = 1;
2772 return true;
2774 case VAR_DECL:
2775 case CONST_DECL:
2776 case PARM_DECL:
2777 case RESULT_DECL:
2778 if (C_DECL_REGISTER (x)
2779 && DECL_NONLOCAL (x))
2781 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2783 error
2784 ("global register variable %qD used in nested function", x);
2785 return false;
2787 pedwarn ("register variable %qD used in nested function", x);
2789 else if (C_DECL_REGISTER (x))
2791 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2792 error ("address of global register variable %qD requested", x);
2793 else
2794 error ("address of register variable %qD requested", x);
2795 return false;
2798 /* drops in */
2799 case FUNCTION_DECL:
2800 TREE_ADDRESSABLE (x) = 1;
2801 /* drops out */
2802 default:
2803 return true;
2807 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2809 tree
2810 build_conditional_expr (tree ifexp, tree op1, tree op2)
2812 tree type1;
2813 tree type2;
2814 enum tree_code code1;
2815 enum tree_code code2;
2816 tree result_type = NULL;
2817 tree orig_op1 = op1, orig_op2 = op2;
2819 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2821 /* Promote both alternatives. */
2823 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2824 op1 = default_conversion (op1);
2825 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2826 op2 = default_conversion (op2);
2828 if (TREE_CODE (ifexp) == ERROR_MARK
2829 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2830 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2831 return error_mark_node;
2833 type1 = TREE_TYPE (op1);
2834 code1 = TREE_CODE (type1);
2835 type2 = TREE_TYPE (op2);
2836 code2 = TREE_CODE (type2);
2838 /* C90 does not permit non-lvalue arrays in conditional expressions.
2839 In C99 they will be pointers by now. */
2840 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2842 error ("non-lvalue array in conditional expression");
2843 return error_mark_node;
2846 /* Quickly detect the usual case where op1 and op2 have the same type
2847 after promotion. */
2848 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2850 if (type1 == type2)
2851 result_type = type1;
2852 else
2853 result_type = TYPE_MAIN_VARIANT (type1);
2855 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2856 || code1 == COMPLEX_TYPE)
2857 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2858 || code2 == COMPLEX_TYPE))
2860 result_type = common_type (type1, type2);
2862 /* If -Wsign-compare, warn here if type1 and type2 have
2863 different signedness. We'll promote the signed to unsigned
2864 and later code won't know it used to be different.
2865 Do this check on the original types, so that explicit casts
2866 will be considered, but default promotions won't. */
2867 if (warn_sign_compare && !skip_evaluation)
2869 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2870 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2872 if (unsigned_op1 ^ unsigned_op2)
2874 /* Do not warn if the result type is signed, since the
2875 signed type will only be chosen if it can represent
2876 all the values of the unsigned type. */
2877 if (!TYPE_UNSIGNED (result_type))
2878 /* OK */;
2879 /* Do not warn if the signed quantity is an unsuffixed
2880 integer literal (or some static constant expression
2881 involving such literals) and it is non-negative. */
2882 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2883 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2884 /* OK */;
2885 else
2886 warning ("signed and unsigned type in conditional expression");
2890 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2892 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2893 pedwarn ("ISO C forbids conditional expr with only one void side");
2894 result_type = void_type_node;
2896 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2898 if (comp_target_types (type1, type2, 1))
2899 result_type = common_pointer_type (type1, type2);
2900 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2901 && TREE_CODE (orig_op1) != NOP_EXPR)
2902 result_type = qualify_type (type2, type1);
2903 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2904 && TREE_CODE (orig_op2) != NOP_EXPR)
2905 result_type = qualify_type (type1, type2);
2906 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2908 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2909 pedwarn ("ISO C forbids conditional expr between "
2910 "%<void *%> and function pointer");
2911 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2912 TREE_TYPE (type2)));
2914 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2916 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2917 pedwarn ("ISO C forbids conditional expr between "
2918 "%<void *%> and function pointer");
2919 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2920 TREE_TYPE (type1)));
2922 else
2924 pedwarn ("pointer type mismatch in conditional expression");
2925 result_type = build_pointer_type (void_type_node);
2928 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2930 if (!integer_zerop (op2))
2931 pedwarn ("pointer/integer type mismatch in conditional expression");
2932 else
2934 op2 = null_pointer_node;
2936 result_type = type1;
2938 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2940 if (!integer_zerop (op1))
2941 pedwarn ("pointer/integer type mismatch in conditional expression");
2942 else
2944 op1 = null_pointer_node;
2946 result_type = type2;
2949 if (!result_type)
2951 if (flag_cond_mismatch)
2952 result_type = void_type_node;
2953 else
2955 error ("type mismatch in conditional expression");
2956 return error_mark_node;
2960 /* Merge const and volatile flags of the incoming types. */
2961 result_type
2962 = build_type_variant (result_type,
2963 TREE_READONLY (op1) || TREE_READONLY (op2),
2964 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2966 if (result_type != TREE_TYPE (op1))
2967 op1 = convert_and_check (result_type, op1);
2968 if (result_type != TREE_TYPE (op2))
2969 op2 = convert_and_check (result_type, op2);
2971 if (TREE_CODE (ifexp) == INTEGER_CST)
2972 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2974 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
2977 /* Return a compound expression that performs two expressions and
2978 returns the value of the second of them. */
2980 tree
2981 build_compound_expr (tree expr1, tree expr2)
2983 /* Convert arrays and functions to pointers. */
2984 expr2 = default_function_array_conversion (expr2);
2986 if (!TREE_SIDE_EFFECTS (expr1))
2988 /* The left-hand operand of a comma expression is like an expression
2989 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2990 any side-effects, unless it was explicitly cast to (void). */
2991 if (warn_unused_value
2992 && !(TREE_CODE (expr1) == CONVERT_EXPR
2993 && VOID_TYPE_P (TREE_TYPE (expr1))))
2994 warning ("left-hand operand of comma expression has no effect");
2997 /* With -Wunused, we should also warn if the left-hand operand does have
2998 side-effects, but computes a value which is not used. For example, in
2999 `foo() + bar(), baz()' the result of the `+' operator is not used,
3000 so we should issue a warning. */
3001 else if (warn_unused_value)
3002 warn_if_unused_value (expr1, input_location);
3004 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3007 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3009 tree
3010 build_c_cast (tree type, tree expr)
3012 tree value = expr;
3014 if (type == error_mark_node || expr == error_mark_node)
3015 return error_mark_node;
3017 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3018 only in <protocol> qualifications. But when constructing cast expressions,
3019 the protocols do matter and must be kept around. */
3020 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3021 return build1 (NOP_EXPR, type, expr);
3023 type = TYPE_MAIN_VARIANT (type);
3025 if (TREE_CODE (type) == ARRAY_TYPE)
3027 error ("cast specifies array type");
3028 return error_mark_node;
3031 if (TREE_CODE (type) == FUNCTION_TYPE)
3033 error ("cast specifies function type");
3034 return error_mark_node;
3037 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3039 if (pedantic)
3041 if (TREE_CODE (type) == RECORD_TYPE
3042 || TREE_CODE (type) == UNION_TYPE)
3043 pedwarn ("ISO C forbids casting nonscalar to the same type");
3046 else if (TREE_CODE (type) == UNION_TYPE)
3048 tree field;
3049 value = default_function_array_conversion (value);
3051 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3052 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3053 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3054 break;
3056 if (field)
3058 tree t;
3060 if (pedantic)
3061 pedwarn ("ISO C forbids casts to union type");
3062 t = digest_init (type,
3063 build_constructor (type,
3064 build_tree_list (field, value)),
3065 true, 0);
3066 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3067 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3068 return t;
3070 error ("cast to union type from type not present in union");
3071 return error_mark_node;
3073 else
3075 tree otype, ovalue;
3077 /* If casting to void, avoid the error that would come
3078 from default_conversion in the case of a non-lvalue array. */
3079 if (type == void_type_node)
3080 return build1 (CONVERT_EXPR, type, value);
3082 /* Convert functions and arrays to pointers,
3083 but don't convert any other types. */
3084 value = default_function_array_conversion (value);
3085 otype = TREE_TYPE (value);
3087 /* Optionally warn about potentially worrisome casts. */
3089 if (warn_cast_qual
3090 && TREE_CODE (type) == POINTER_TYPE
3091 && TREE_CODE (otype) == POINTER_TYPE)
3093 tree in_type = type;
3094 tree in_otype = otype;
3095 int added = 0;
3096 int discarded = 0;
3098 /* Check that the qualifiers on IN_TYPE are a superset of
3099 the qualifiers of IN_OTYPE. The outermost level of
3100 POINTER_TYPE nodes is uninteresting and we stop as soon
3101 as we hit a non-POINTER_TYPE node on either type. */
3104 in_otype = TREE_TYPE (in_otype);
3105 in_type = TREE_TYPE (in_type);
3107 /* GNU C allows cv-qualified function types. 'const'
3108 means the function is very pure, 'volatile' means it
3109 can't return. We need to warn when such qualifiers
3110 are added, not when they're taken away. */
3111 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3112 && TREE_CODE (in_type) == FUNCTION_TYPE)
3113 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3114 else
3115 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3117 while (TREE_CODE (in_type) == POINTER_TYPE
3118 && TREE_CODE (in_otype) == POINTER_TYPE);
3120 if (added)
3121 warning ("cast adds new qualifiers to function type");
3123 if (discarded)
3124 /* There are qualifiers present in IN_OTYPE that are not
3125 present in IN_TYPE. */
3126 warning ("cast discards qualifiers from pointer target type");
3129 /* Warn about possible alignment problems. */
3130 if (STRICT_ALIGNMENT && warn_cast_align
3131 && TREE_CODE (type) == POINTER_TYPE
3132 && TREE_CODE (otype) == POINTER_TYPE
3133 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3134 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3135 /* Don't warn about opaque types, where the actual alignment
3136 restriction is unknown. */
3137 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3138 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3139 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3140 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3141 warning ("cast increases required alignment of target type");
3143 if (TREE_CODE (type) == INTEGER_TYPE
3144 && TREE_CODE (otype) == POINTER_TYPE
3145 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3146 && !TREE_CONSTANT (value))
3147 warning ("cast from pointer to integer of different size");
3149 if (warn_bad_function_cast
3150 && TREE_CODE (value) == CALL_EXPR
3151 && TREE_CODE (type) != TREE_CODE (otype))
3152 warning ("cast from function call of type %qT to non-matching "
3153 "type %qT", otype, type);
3155 if (TREE_CODE (type) == POINTER_TYPE
3156 && TREE_CODE (otype) == INTEGER_TYPE
3157 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3158 /* Don't warn about converting any constant. */
3159 && !TREE_CONSTANT (value))
3160 warning ("cast to pointer from integer of different size");
3162 if (TREE_CODE (type) == POINTER_TYPE
3163 && TREE_CODE (otype) == POINTER_TYPE
3164 && TREE_CODE (expr) == ADDR_EXPR
3165 && DECL_P (TREE_OPERAND (expr, 0))
3166 && flag_strict_aliasing && warn_strict_aliasing
3167 && !VOID_TYPE_P (TREE_TYPE (type)))
3169 /* Casting the address of a decl to non void pointer. Warn
3170 if the cast breaks type based aliasing. */
3171 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3172 warning ("type-punning to incomplete type might break strict-aliasing rules");
3173 else
3175 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3176 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3178 if (!alias_sets_conflict_p (set1, set2))
3179 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3180 else if (warn_strict_aliasing > 1
3181 && !alias_sets_might_conflict_p (set1, set2))
3182 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3186 /* If pedantic, warn for conversions between function and object
3187 pointer types, except for converting a null pointer constant
3188 to function pointer type. */
3189 if (pedantic
3190 && TREE_CODE (type) == POINTER_TYPE
3191 && TREE_CODE (otype) == POINTER_TYPE
3192 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3193 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3194 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3196 if (pedantic
3197 && TREE_CODE (type) == POINTER_TYPE
3198 && TREE_CODE (otype) == POINTER_TYPE
3199 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3200 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3201 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3202 && TREE_CODE (expr) != NOP_EXPR))
3203 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3205 ovalue = value;
3206 value = convert (type, value);
3208 /* Ignore any integer overflow caused by the cast. */
3209 if (TREE_CODE (value) == INTEGER_CST)
3211 if (EXPR_P (ovalue))
3212 /* If OVALUE had overflow set, then so will VALUE, so it
3213 is safe to overwrite. */
3214 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3215 else
3216 TREE_OVERFLOW (value) = 0;
3218 if (CONSTANT_CLASS_P (ovalue))
3219 /* Similarly, constant_overflow cannot have become
3220 cleared. */
3221 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3225 /* Don't let a cast be an lvalue. */
3226 if (value == expr)
3227 value = non_lvalue (value);
3229 return value;
3232 /* Interpret a cast of expression EXPR to type TYPE. */
3233 tree
3234 c_cast_expr (struct c_type_name *type_name, tree expr)
3236 tree type;
3237 int saved_wsp = warn_strict_prototypes;
3239 /* This avoids warnings about unprototyped casts on
3240 integers. E.g. "#define SIG_DFL (void(*)())0". */
3241 if (TREE_CODE (expr) == INTEGER_CST)
3242 warn_strict_prototypes = 0;
3243 type = groktypename (type_name);
3244 warn_strict_prototypes = saved_wsp;
3246 return build_c_cast (type, expr);
3250 /* Build an assignment expression of lvalue LHS from value RHS.
3251 MODIFYCODE is the code for a binary operator that we use
3252 to combine the old value of LHS with RHS to get the new value.
3253 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3255 tree
3256 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3258 tree result;
3259 tree newrhs;
3260 tree lhstype = TREE_TYPE (lhs);
3261 tree olhstype = lhstype;
3263 /* Types that aren't fully specified cannot be used in assignments. */
3264 lhs = require_complete_type (lhs);
3266 /* Avoid duplicate error messages from operands that had errors. */
3267 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3268 return error_mark_node;
3270 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3271 /* Do not use STRIP_NOPS here. We do not want an enumerator
3272 whose value is 0 to count as a null pointer constant. */
3273 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3274 rhs = TREE_OPERAND (rhs, 0);
3276 newrhs = rhs;
3278 /* If a binary op has been requested, combine the old LHS value with the RHS
3279 producing the value we should actually store into the LHS. */
3281 if (modifycode != NOP_EXPR)
3283 lhs = stabilize_reference (lhs);
3284 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3287 if (!lvalue_or_else (lhs, lv_assign))
3288 return error_mark_node;
3290 /* Give an error for storing in something that is 'const'. */
3292 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3293 || ((TREE_CODE (lhstype) == RECORD_TYPE
3294 || TREE_CODE (lhstype) == UNION_TYPE)
3295 && C_TYPE_FIELDS_READONLY (lhstype)))
3296 readonly_error (lhs, lv_assign);
3298 /* If storing into a structure or union member,
3299 it has probably been given type `int'.
3300 Compute the type that would go with
3301 the actual amount of storage the member occupies. */
3303 if (TREE_CODE (lhs) == COMPONENT_REF
3304 && (TREE_CODE (lhstype) == INTEGER_TYPE
3305 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3306 || TREE_CODE (lhstype) == REAL_TYPE
3307 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3308 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3310 /* If storing in a field that is in actuality a short or narrower than one,
3311 we must store in the field in its actual type. */
3313 if (lhstype != TREE_TYPE (lhs))
3315 lhs = copy_node (lhs);
3316 TREE_TYPE (lhs) = lhstype;
3319 /* Convert new value to destination type. */
3321 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3322 NULL_TREE, NULL_TREE, 0);
3323 if (TREE_CODE (newrhs) == ERROR_MARK)
3324 return error_mark_node;
3326 /* Scan operands. */
3328 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3329 TREE_SIDE_EFFECTS (result) = 1;
3331 /* If we got the LHS in a different type for storing in,
3332 convert the result back to the nominal type of LHS
3333 so that the value we return always has the same type
3334 as the LHS argument. */
3336 if (olhstype == TREE_TYPE (result))
3337 return result;
3338 return convert_for_assignment (olhstype, result, ic_assign,
3339 NULL_TREE, NULL_TREE, 0);
3342 /* Convert value RHS to type TYPE as preparation for an assignment
3343 to an lvalue of type TYPE.
3344 The real work of conversion is done by `convert'.
3345 The purpose of this function is to generate error messages
3346 for assignments that are not allowed in C.
3347 ERRTYPE says whether it is argument passing, assignment,
3348 initialization or return.
3350 FUNCTION is a tree for the function being called.
3351 PARMNUM is the number of the argument, for printing in error messages. */
3353 static tree
3354 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3355 tree fundecl, tree function, int parmnum)
3357 enum tree_code codel = TREE_CODE (type);
3358 tree rhstype;
3359 enum tree_code coder;
3360 tree rname = NULL_TREE;
3362 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3364 tree selector;
3365 /* Change pointer to function to the function itself for
3366 diagnostics. */
3367 if (TREE_CODE (function) == ADDR_EXPR
3368 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3369 function = TREE_OPERAND (function, 0);
3371 /* Handle an ObjC selector specially for diagnostics. */
3372 selector = objc_message_selector ();
3373 rname = function;
3374 if (selector && parmnum > 2)
3376 rname = selector;
3377 parmnum -= 2;
3381 /* This macro is used to emit diagnostics to ensure that all format
3382 strings are complete sentences, visible to gettext and checked at
3383 compile time. */
3384 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3385 do { \
3386 switch (errtype) \
3388 case ic_argpass: \
3389 pedwarn (AR, parmnum, rname); \
3390 break; \
3391 case ic_argpass_nonproto: \
3392 warning (AR, parmnum, rname); \
3393 break; \
3394 case ic_assign: \
3395 pedwarn (AS); \
3396 break; \
3397 case ic_init: \
3398 pedwarn (IN); \
3399 break; \
3400 case ic_return: \
3401 pedwarn (RE); \
3402 break; \
3403 default: \
3404 gcc_unreachable (); \
3406 } while (0)
3408 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3409 /* Do not use STRIP_NOPS here. We do not want an enumerator
3410 whose value is 0 to count as a null pointer constant. */
3411 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3412 rhs = TREE_OPERAND (rhs, 0);
3414 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3415 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3416 rhs = default_conversion (rhs);
3417 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3418 rhs = decl_constant_value_for_broken_optimization (rhs);
3420 rhstype = TREE_TYPE (rhs);
3421 coder = TREE_CODE (rhstype);
3423 if (coder == ERROR_MARK)
3424 return error_mark_node;
3426 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3428 overflow_warning (rhs);
3429 /* Check for Objective-C protocols. This will automatically
3430 issue a warning if there are protocol violations. No need to
3431 use the return value. */
3432 if (c_dialect_objc ())
3433 objc_comptypes (type, rhstype, 0);
3434 return rhs;
3437 if (coder == VOID_TYPE)
3439 /* Except for passing an argument to an unprototyped function,
3440 this is a constraint violation. When passing an argument to
3441 an unprototyped function, it is compile-time undefined;
3442 making it a constraint in that case was rejected in
3443 DR#252. */
3444 error ("void value not ignored as it ought to be");
3445 return error_mark_node;
3447 /* A type converts to a reference to it.
3448 This code doesn't fully support references, it's just for the
3449 special case of va_start and va_copy. */
3450 if (codel == REFERENCE_TYPE
3451 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3453 if (!lvalue_p (rhs))
3455 error ("cannot pass rvalue to reference parameter");
3456 return error_mark_node;
3458 if (!c_mark_addressable (rhs))
3459 return error_mark_node;
3460 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3462 /* We already know that these two types are compatible, but they
3463 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3464 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3465 likely to be va_list, a typedef to __builtin_va_list, which
3466 is different enough that it will cause problems later. */
3467 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3468 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3470 rhs = build1 (NOP_EXPR, type, rhs);
3471 return rhs;
3473 /* Some types can interconvert without explicit casts. */
3474 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3475 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3476 return convert (type, rhs);
3477 /* Arithmetic types all interconvert, and enum is treated like int. */
3478 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3479 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3480 || codel == BOOLEAN_TYPE)
3481 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3482 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3483 || coder == BOOLEAN_TYPE))
3484 return convert_and_check (type, rhs);
3486 /* Conversion to a transparent union from its member types.
3487 This applies only to function arguments. */
3488 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3489 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3491 tree memb_types;
3492 tree marginal_memb_type = 0;
3494 for (memb_types = TYPE_FIELDS (type); memb_types;
3495 memb_types = TREE_CHAIN (memb_types))
3497 tree memb_type = TREE_TYPE (memb_types);
3499 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3500 TYPE_MAIN_VARIANT (rhstype)))
3501 break;
3503 if (TREE_CODE (memb_type) != POINTER_TYPE)
3504 continue;
3506 if (coder == POINTER_TYPE)
3508 tree ttl = TREE_TYPE (memb_type);
3509 tree ttr = TREE_TYPE (rhstype);
3511 /* Any non-function converts to a [const][volatile] void *
3512 and vice versa; otherwise, targets must be the same.
3513 Meanwhile, the lhs target must have all the qualifiers of
3514 the rhs. */
3515 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3516 || comp_target_types (memb_type, rhstype, 0))
3518 /* If this type won't generate any warnings, use it. */
3519 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3520 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3521 && TREE_CODE (ttl) == FUNCTION_TYPE)
3522 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3523 == TYPE_QUALS (ttr))
3524 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3525 == TYPE_QUALS (ttl))))
3526 break;
3528 /* Keep looking for a better type, but remember this one. */
3529 if (!marginal_memb_type)
3530 marginal_memb_type = memb_type;
3534 /* Can convert integer zero to any pointer type. */
3535 if (integer_zerop (rhs)
3536 || (TREE_CODE (rhs) == NOP_EXPR
3537 && integer_zerop (TREE_OPERAND (rhs, 0))))
3539 rhs = null_pointer_node;
3540 break;
3544 if (memb_types || marginal_memb_type)
3546 if (!memb_types)
3548 /* We have only a marginally acceptable member type;
3549 it needs a warning. */
3550 tree ttl = TREE_TYPE (marginal_memb_type);
3551 tree ttr = TREE_TYPE (rhstype);
3553 /* Const and volatile mean something different for function
3554 types, so the usual warnings are not appropriate. */
3555 if (TREE_CODE (ttr) == FUNCTION_TYPE
3556 && TREE_CODE (ttl) == FUNCTION_TYPE)
3558 /* Because const and volatile on functions are
3559 restrictions that say the function will not do
3560 certain things, it is okay to use a const or volatile
3561 function where an ordinary one is wanted, but not
3562 vice-versa. */
3563 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3564 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3565 "makes qualified function "
3566 "pointer from unqualified"),
3567 N_("assignment makes qualified "
3568 "function pointer from "
3569 "unqualified"),
3570 N_("initialization makes qualified "
3571 "function pointer from "
3572 "unqualified"),
3573 N_("return makes qualified function "
3574 "pointer from unqualified"));
3576 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3577 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3578 "qualifiers from pointer target type"),
3579 N_("assignment discards qualifiers "
3580 "from pointer target type"),
3581 N_("initialization discards qualifiers "
3582 "from pointer target type"),
3583 N_("return discards qualifiers from "
3584 "pointer target type"));
3587 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3588 pedwarn ("ISO C prohibits argument conversion to union type");
3590 return build1 (NOP_EXPR, type, rhs);
3594 /* Conversions among pointers */
3595 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3596 && (coder == codel))
3598 tree ttl = TREE_TYPE (type);
3599 tree ttr = TREE_TYPE (rhstype);
3600 bool is_opaque_pointer;
3601 int target_cmp = 0; /* Cache comp_target_types () result. */
3603 /* Opaque pointers are treated like void pointers. */
3604 is_opaque_pointer = (targetm.vector_opaque_p (type)
3605 || targetm.vector_opaque_p (rhstype))
3606 && TREE_CODE (ttl) == VECTOR_TYPE
3607 && TREE_CODE (ttr) == VECTOR_TYPE;
3609 /* Any non-function converts to a [const][volatile] void *
3610 and vice versa; otherwise, targets must be the same.
3611 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3612 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3613 || (target_cmp = comp_target_types (type, rhstype, 0))
3614 || is_opaque_pointer
3615 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3616 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3618 if (pedantic
3619 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3621 (VOID_TYPE_P (ttr)
3622 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3623 which are not ANSI null ptr constants. */
3624 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3625 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3626 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3627 "%qE between function pointer "
3628 "and %<void *%>"),
3629 N_("ISO C forbids assignment between "
3630 "function pointer and %<void *%>"),
3631 N_("ISO C forbids initialization between "
3632 "function pointer and %<void *%>"),
3633 N_("ISO C forbids return between function "
3634 "pointer and %<void *%>"));
3635 /* Const and volatile mean something different for function types,
3636 so the usual warnings are not appropriate. */
3637 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3638 && TREE_CODE (ttl) != FUNCTION_TYPE)
3640 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3641 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3642 "qualifiers from pointer target type"),
3643 N_("assignment discards qualifiers "
3644 "from pointer target type"),
3645 N_("initialization discards qualifiers "
3646 "from pointer target type"),
3647 N_("return discards qualifiers from "
3648 "pointer target type"));
3649 /* If this is not a case of ignoring a mismatch in signedness,
3650 no warning. */
3651 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3652 || target_cmp)
3654 /* If there is a mismatch, do warn. */
3655 else if (warn_pointer_sign)
3656 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3657 "%d of %qE differ in signedness"),
3658 N_("pointer targets in assignment "
3659 "differ in signedness"),
3660 N_("pointer targets in initialization "
3661 "differ in signedness"),
3662 N_("pointer targets in return differ "
3663 "in signedness"));
3665 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3666 && TREE_CODE (ttr) == FUNCTION_TYPE)
3668 /* Because const and volatile on functions are restrictions
3669 that say the function will not do certain things,
3670 it is okay to use a const or volatile function
3671 where an ordinary one is wanted, but not vice-versa. */
3672 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3673 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3674 "qualified function pointer "
3675 "from unqualified"),
3676 N_("assignment makes qualified function "
3677 "pointer from unqualified"),
3678 N_("initialization makes qualified "
3679 "function pointer from unqualified"),
3680 N_("return makes qualified function "
3681 "pointer from unqualified"));
3684 else
3685 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3686 "incompatible pointer type"),
3687 N_("assignment from incompatible pointer type"),
3688 N_("initialization from incompatible "
3689 "pointer type"),
3690 N_("return from incompatible pointer type"));
3691 return convert (type, rhs);
3693 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3695 /* ??? This should not be an error when inlining calls to
3696 unprototyped functions. */
3697 error ("invalid use of non-lvalue array");
3698 return error_mark_node;
3700 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3702 /* An explicit constant 0 can convert to a pointer,
3703 or one that results from arithmetic, even including
3704 a cast to integer type. */
3705 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3707 !(TREE_CODE (rhs) == NOP_EXPR
3708 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3709 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3710 && integer_zerop (TREE_OPERAND (rhs, 0))))
3711 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3712 "pointer from integer without a cast"),
3713 N_("assignment makes pointer from integer "
3714 "without a cast"),
3715 N_("initialization makes pointer from "
3716 "integer without a cast"),
3717 N_("return makes pointer from integer "
3718 "without a cast"));
3720 return convert (type, rhs);
3722 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3724 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3725 "from pointer without a cast"),
3726 N_("assignment makes integer from pointer "
3727 "without a cast"),
3728 N_("initialization makes integer from pointer "
3729 "without a cast"),
3730 N_("return makes integer from pointer "
3731 "without a cast"));
3732 return convert (type, rhs);
3734 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3735 return convert (type, rhs);
3737 switch (errtype)
3739 case ic_argpass:
3740 case ic_argpass_nonproto:
3741 /* ??? This should not be an error when inlining calls to
3742 unprototyped functions. */
3743 error ("incompatible type for argument %d of %qE", parmnum, rname);
3744 break;
3745 case ic_assign:
3746 error ("incompatible types in assignment");
3747 break;
3748 case ic_init:
3749 error ("incompatible types in initialization");
3750 break;
3751 case ic_return:
3752 error ("incompatible types in return");
3753 break;
3754 default:
3755 gcc_unreachable ();
3758 return error_mark_node;
3761 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3762 is used for error and waring reporting and indicates which argument
3763 is being processed. */
3765 tree
3766 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3768 tree ret, type;
3770 /* If FN was prototyped, the value has been converted already
3771 in convert_arguments. */
3772 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3773 return value;
3775 type = TREE_TYPE (parm);
3776 ret = convert_for_assignment (type, value,
3777 ic_argpass_nonproto, fn,
3778 fn, argnum);
3779 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3780 && INTEGRAL_TYPE_P (type)
3781 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3782 ret = default_conversion (ret);
3783 return ret;
3786 /* If VALUE is a compound expr all of whose expressions are constant, then
3787 return its value. Otherwise, return error_mark_node.
3789 This is for handling COMPOUND_EXPRs as initializer elements
3790 which is allowed with a warning when -pedantic is specified. */
3792 static tree
3793 valid_compound_expr_initializer (tree value, tree endtype)
3795 if (TREE_CODE (value) == COMPOUND_EXPR)
3797 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3798 == error_mark_node)
3799 return error_mark_node;
3800 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3801 endtype);
3803 else if (!initializer_constant_valid_p (value, endtype))
3804 return error_mark_node;
3805 else
3806 return value;
3809 /* Perform appropriate conversions on the initial value of a variable,
3810 store it in the declaration DECL,
3811 and print any error messages that are appropriate.
3812 If the init is invalid, store an ERROR_MARK. */
3814 void
3815 store_init_value (tree decl, tree init)
3817 tree value, type;
3819 /* If variable's type was invalidly declared, just ignore it. */
3821 type = TREE_TYPE (decl);
3822 if (TREE_CODE (type) == ERROR_MARK)
3823 return;
3825 /* Digest the specified initializer into an expression. */
3827 value = digest_init (type, init, true, TREE_STATIC (decl));
3829 /* Store the expression if valid; else report error. */
3831 if (warn_traditional && !in_system_header
3832 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3833 warning ("traditional C rejects automatic aggregate initialization");
3835 DECL_INITIAL (decl) = value;
3837 /* ANSI wants warnings about out-of-range constant initializers. */
3838 STRIP_TYPE_NOPS (value);
3839 constant_expression_warning (value);
3841 /* Check if we need to set array size from compound literal size. */
3842 if (TREE_CODE (type) == ARRAY_TYPE
3843 && TYPE_DOMAIN (type) == 0
3844 && value != error_mark_node)
3846 tree inside_init = init;
3848 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3849 inside_init = TREE_OPERAND (init, 0);
3850 inside_init = fold (inside_init);
3852 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3854 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3856 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3858 /* For int foo[] = (int [3]){1}; we need to set array size
3859 now since later on array initializer will be just the
3860 brace enclosed list of the compound literal. */
3861 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3862 layout_type (type);
3863 layout_decl (decl, 0);
3869 /* Methods for storing and printing names for error messages. */
3871 /* Implement a spelling stack that allows components of a name to be pushed
3872 and popped. Each element on the stack is this structure. */
3874 struct spelling
3876 int kind;
3877 union
3879 int i;
3880 const char *s;
3881 } u;
3884 #define SPELLING_STRING 1
3885 #define SPELLING_MEMBER 2
3886 #define SPELLING_BOUNDS 3
3888 static struct spelling *spelling; /* Next stack element (unused). */
3889 static struct spelling *spelling_base; /* Spelling stack base. */
3890 static int spelling_size; /* Size of the spelling stack. */
3892 /* Macros to save and restore the spelling stack around push_... functions.
3893 Alternative to SAVE_SPELLING_STACK. */
3895 #define SPELLING_DEPTH() (spelling - spelling_base)
3896 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3898 /* Push an element on the spelling stack with type KIND and assign VALUE
3899 to MEMBER. */
3901 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3903 int depth = SPELLING_DEPTH (); \
3905 if (depth >= spelling_size) \
3907 spelling_size += 10; \
3908 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3909 spelling_size); \
3910 RESTORE_SPELLING_DEPTH (depth); \
3913 spelling->kind = (KIND); \
3914 spelling->MEMBER = (VALUE); \
3915 spelling++; \
3918 /* Push STRING on the stack. Printed literally. */
3920 static void
3921 push_string (const char *string)
3923 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3926 /* Push a member name on the stack. Printed as '.' STRING. */
3928 static void
3929 push_member_name (tree decl)
3931 const char *const string
3932 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3933 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3936 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3938 static void
3939 push_array_bounds (int bounds)
3941 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3944 /* Compute the maximum size in bytes of the printed spelling. */
3946 static int
3947 spelling_length (void)
3949 int size = 0;
3950 struct spelling *p;
3952 for (p = spelling_base; p < spelling; p++)
3954 if (p->kind == SPELLING_BOUNDS)
3955 size += 25;
3956 else
3957 size += strlen (p->u.s) + 1;
3960 return size;
3963 /* Print the spelling to BUFFER and return it. */
3965 static char *
3966 print_spelling (char *buffer)
3968 char *d = buffer;
3969 struct spelling *p;
3971 for (p = spelling_base; p < spelling; p++)
3972 if (p->kind == SPELLING_BOUNDS)
3974 sprintf (d, "[%d]", p->u.i);
3975 d += strlen (d);
3977 else
3979 const char *s;
3980 if (p->kind == SPELLING_MEMBER)
3981 *d++ = '.';
3982 for (s = p->u.s; (*d = *s++); d++)
3985 *d++ = '\0';
3986 return buffer;
3989 /* Issue an error message for a bad initializer component.
3990 MSGID identifies the message.
3991 The component name is taken from the spelling stack. */
3993 void
3994 error_init (const char *msgid)
3996 char *ofwhat;
3998 error ("%s", _(msgid));
3999 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4000 if (*ofwhat)
4001 error ("(near initialization for %qs)", ofwhat);
4004 /* Issue a pedantic warning for a bad initializer component.
4005 MSGID identifies the message.
4006 The component name is taken from the spelling stack. */
4008 void
4009 pedwarn_init (const char *msgid)
4011 char *ofwhat;
4013 pedwarn ("%s", _(msgid));
4014 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4015 if (*ofwhat)
4016 pedwarn ("(near initialization for %qs)", ofwhat);
4019 /* Issue a warning for a bad initializer component.
4020 MSGID identifies the message.
4021 The component name is taken from the spelling stack. */
4023 static void
4024 warning_init (const char *msgid)
4026 char *ofwhat;
4028 warning ("%s", _(msgid));
4029 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4030 if (*ofwhat)
4031 warning ("(near initialization for %qs)", ofwhat);
4034 /* If TYPE is an array type and EXPR is a parenthesized string
4035 constant, warn if pedantic that EXPR is being used to initialize an
4036 object of type TYPE. */
4038 void
4039 maybe_warn_string_init (tree type, struct c_expr expr)
4041 if (pedantic
4042 && TREE_CODE (type) == ARRAY_TYPE
4043 && TREE_CODE (expr.value) == STRING_CST
4044 && expr.original_code != STRING_CST)
4045 pedwarn_init ("array initialized from parenthesized string constant");
4048 /* Digest the parser output INIT as an initializer for type TYPE.
4049 Return a C expression of type TYPE to represent the initial value.
4051 If INIT is a string constant, STRICT_STRING is true if it is
4052 unparenthesized or we should not warn here for it being parenthesized.
4053 For other types of INIT, STRICT_STRING is not used.
4055 REQUIRE_CONSTANT requests an error if non-constant initializers or
4056 elements are seen. */
4058 static tree
4059 digest_init (tree type, tree init, bool strict_string, int require_constant)
4061 enum tree_code code = TREE_CODE (type);
4062 tree inside_init = init;
4064 if (type == error_mark_node
4065 || init == error_mark_node
4066 || TREE_TYPE (init) == error_mark_node)
4067 return error_mark_node;
4069 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4070 /* Do not use STRIP_NOPS here. We do not want an enumerator
4071 whose value is 0 to count as a null pointer constant. */
4072 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4073 inside_init = TREE_OPERAND (init, 0);
4075 inside_init = fold (inside_init);
4077 /* Initialization of an array of chars from a string constant
4078 optionally enclosed in braces. */
4080 if (code == ARRAY_TYPE && inside_init
4081 && TREE_CODE (inside_init) == STRING_CST)
4083 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4084 /* Note that an array could be both an array of character type
4085 and an array of wchar_t if wchar_t is signed char or unsigned
4086 char. */
4087 bool char_array = (typ1 == char_type_node
4088 || typ1 == signed_char_type_node
4089 || typ1 == unsigned_char_type_node);
4090 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4091 if (char_array || wchar_array)
4093 struct c_expr expr;
4094 bool char_string;
4095 expr.value = inside_init;
4096 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4097 maybe_warn_string_init (type, expr);
4099 char_string
4100 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4101 == char_type_node);
4103 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4104 TYPE_MAIN_VARIANT (type)))
4105 return inside_init;
4107 if (!wchar_array && !char_string)
4109 error_init ("char-array initialized from wide string");
4110 return error_mark_node;
4112 if (char_string && !char_array)
4114 error_init ("wchar_t-array initialized from non-wide string");
4115 return error_mark_node;
4118 TREE_TYPE (inside_init) = type;
4119 if (TYPE_DOMAIN (type) != 0
4120 && TYPE_SIZE (type) != 0
4121 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4122 /* Subtract 1 (or sizeof (wchar_t))
4123 because it's ok to ignore the terminating null char
4124 that is counted in the length of the constant. */
4125 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4126 TREE_STRING_LENGTH (inside_init)
4127 - ((TYPE_PRECISION (typ1)
4128 != TYPE_PRECISION (char_type_node))
4129 ? (TYPE_PRECISION (wchar_type_node)
4130 / BITS_PER_UNIT)
4131 : 1)))
4132 pedwarn_init ("initializer-string for array of chars is too long");
4134 return inside_init;
4136 else if (INTEGRAL_TYPE_P (typ1))
4138 error_init ("array of inappropriate type initialized "
4139 "from string constant");
4140 return error_mark_node;
4144 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4145 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4146 below and handle as a constructor. */
4147 if (code == VECTOR_TYPE
4148 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4149 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4150 && TREE_CONSTANT (inside_init))
4152 if (TREE_CODE (inside_init) == VECTOR_CST
4153 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4154 TYPE_MAIN_VARIANT (type)))
4155 return inside_init;
4157 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4159 tree link;
4161 /* Iterate through elements and check if all constructor
4162 elements are *_CSTs. */
4163 for (link = CONSTRUCTOR_ELTS (inside_init);
4164 link;
4165 link = TREE_CHAIN (link))
4166 if (! CONSTANT_CLASS_P (TREE_VALUE (link)))
4167 break;
4169 if (link == NULL)
4170 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4174 /* Any type can be initialized
4175 from an expression of the same type, optionally with braces. */
4177 if (inside_init && TREE_TYPE (inside_init) != 0
4178 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4179 TYPE_MAIN_VARIANT (type))
4180 || (code == ARRAY_TYPE
4181 && comptypes (TREE_TYPE (inside_init), type))
4182 || (code == VECTOR_TYPE
4183 && comptypes (TREE_TYPE (inside_init), type))
4184 || (code == POINTER_TYPE
4185 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4186 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4187 TREE_TYPE (type)))
4188 || (code == POINTER_TYPE
4189 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4190 && comptypes (TREE_TYPE (inside_init),
4191 TREE_TYPE (type)))))
4193 if (code == POINTER_TYPE)
4195 inside_init = default_function_array_conversion (inside_init);
4197 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4199 error_init ("invalid use of non-lvalue array");
4200 return error_mark_node;
4204 if (code == VECTOR_TYPE)
4205 /* Although the types are compatible, we may require a
4206 conversion. */
4207 inside_init = convert (type, inside_init);
4209 if (require_constant && !flag_isoc99
4210 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4212 /* As an extension, allow initializing objects with static storage
4213 duration with compound literals (which are then treated just as
4214 the brace enclosed list they contain). */
4215 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4216 inside_init = DECL_INITIAL (decl);
4219 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4220 && TREE_CODE (inside_init) != CONSTRUCTOR)
4222 error_init ("array initialized from non-constant array expression");
4223 return error_mark_node;
4226 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4227 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4229 /* Compound expressions can only occur here if -pedantic or
4230 -pedantic-errors is specified. In the later case, we always want
4231 an error. In the former case, we simply want a warning. */
4232 if (require_constant && pedantic
4233 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4235 inside_init
4236 = valid_compound_expr_initializer (inside_init,
4237 TREE_TYPE (inside_init));
4238 if (inside_init == error_mark_node)
4239 error_init ("initializer element is not constant");
4240 else
4241 pedwarn_init ("initializer element is not constant");
4242 if (flag_pedantic_errors)
4243 inside_init = error_mark_node;
4245 else if (require_constant
4246 && !initializer_constant_valid_p (inside_init,
4247 TREE_TYPE (inside_init)))
4249 error_init ("initializer element is not constant");
4250 inside_init = error_mark_node;
4253 return inside_init;
4256 /* Handle scalar types, including conversions. */
4258 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4259 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4260 || code == VECTOR_TYPE)
4262 /* Note that convert_for_assignment calls default_conversion
4263 for arrays and functions. We must not call it in the
4264 case where inside_init is a null pointer constant. */
4265 inside_init
4266 = convert_for_assignment (type, init, ic_init,
4267 NULL_TREE, NULL_TREE, 0);
4269 /* Check to see if we have already given an error message. */
4270 if (inside_init == error_mark_node)
4272 else if (require_constant && !TREE_CONSTANT (inside_init))
4274 error_init ("initializer element is not constant");
4275 inside_init = error_mark_node;
4277 else if (require_constant
4278 && !initializer_constant_valid_p (inside_init,
4279 TREE_TYPE (inside_init)))
4281 error_init ("initializer element is not computable at load time");
4282 inside_init = error_mark_node;
4285 return inside_init;
4288 /* Come here only for records and arrays. */
4290 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4292 error_init ("variable-sized object may not be initialized");
4293 return error_mark_node;
4296 error_init ("invalid initializer");
4297 return error_mark_node;
4300 /* Handle initializers that use braces. */
4302 /* Type of object we are accumulating a constructor for.
4303 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4304 static tree constructor_type;
4306 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4307 left to fill. */
4308 static tree constructor_fields;
4310 /* For an ARRAY_TYPE, this is the specified index
4311 at which to store the next element we get. */
4312 static tree constructor_index;
4314 /* For an ARRAY_TYPE, this is the maximum index. */
4315 static tree constructor_max_index;
4317 /* For a RECORD_TYPE, this is the first field not yet written out. */
4318 static tree constructor_unfilled_fields;
4320 /* For an ARRAY_TYPE, this is the index of the first element
4321 not yet written out. */
4322 static tree constructor_unfilled_index;
4324 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4325 This is so we can generate gaps between fields, when appropriate. */
4326 static tree constructor_bit_index;
4328 /* If we are saving up the elements rather than allocating them,
4329 this is the list of elements so far (in reverse order,
4330 most recent first). */
4331 static tree constructor_elements;
4333 /* 1 if constructor should be incrementally stored into a constructor chain,
4334 0 if all the elements should be kept in AVL tree. */
4335 static int constructor_incremental;
4337 /* 1 if so far this constructor's elements are all compile-time constants. */
4338 static int constructor_constant;
4340 /* 1 if so far this constructor's elements are all valid address constants. */
4341 static int constructor_simple;
4343 /* 1 if this constructor is erroneous so far. */
4344 static int constructor_erroneous;
4346 /* Structure for managing pending initializer elements, organized as an
4347 AVL tree. */
4349 struct init_node
4351 struct init_node *left, *right;
4352 struct init_node *parent;
4353 int balance;
4354 tree purpose;
4355 tree value;
4358 /* Tree of pending elements at this constructor level.
4359 These are elements encountered out of order
4360 which belong at places we haven't reached yet in actually
4361 writing the output.
4362 Will never hold tree nodes across GC runs. */
4363 static struct init_node *constructor_pending_elts;
4365 /* The SPELLING_DEPTH of this constructor. */
4366 static int constructor_depth;
4368 /* DECL node for which an initializer is being read.
4369 0 means we are reading a constructor expression
4370 such as (struct foo) {...}. */
4371 static tree constructor_decl;
4373 /* Nonzero if this is an initializer for a top-level decl. */
4374 static int constructor_top_level;
4376 /* Nonzero if there were any member designators in this initializer. */
4377 static int constructor_designated;
4379 /* Nesting depth of designator list. */
4380 static int designator_depth;
4382 /* Nonzero if there were diagnosed errors in this designator list. */
4383 static int designator_errorneous;
4386 /* This stack has a level for each implicit or explicit level of
4387 structuring in the initializer, including the outermost one. It
4388 saves the values of most of the variables above. */
4390 struct constructor_range_stack;
4392 struct constructor_stack
4394 struct constructor_stack *next;
4395 tree type;
4396 tree fields;
4397 tree index;
4398 tree max_index;
4399 tree unfilled_index;
4400 tree unfilled_fields;
4401 tree bit_index;
4402 tree elements;
4403 struct init_node *pending_elts;
4404 int offset;
4405 int depth;
4406 /* If value nonzero, this value should replace the entire
4407 constructor at this level. */
4408 struct c_expr replacement_value;
4409 struct constructor_range_stack *range_stack;
4410 char constant;
4411 char simple;
4412 char implicit;
4413 char erroneous;
4414 char outer;
4415 char incremental;
4416 char designated;
4419 struct constructor_stack *constructor_stack;
4421 /* This stack represents designators from some range designator up to
4422 the last designator in the list. */
4424 struct constructor_range_stack
4426 struct constructor_range_stack *next, *prev;
4427 struct constructor_stack *stack;
4428 tree range_start;
4429 tree index;
4430 tree range_end;
4431 tree fields;
4434 struct constructor_range_stack *constructor_range_stack;
4436 /* This stack records separate initializers that are nested.
4437 Nested initializers can't happen in ANSI C, but GNU C allows them
4438 in cases like { ... (struct foo) { ... } ... }. */
4440 struct initializer_stack
4442 struct initializer_stack *next;
4443 tree decl;
4444 struct constructor_stack *constructor_stack;
4445 struct constructor_range_stack *constructor_range_stack;
4446 tree elements;
4447 struct spelling *spelling;
4448 struct spelling *spelling_base;
4449 int spelling_size;
4450 char top_level;
4451 char require_constant_value;
4452 char require_constant_elements;
4455 struct initializer_stack *initializer_stack;
4457 /* Prepare to parse and output the initializer for variable DECL. */
4459 void
4460 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4462 const char *locus;
4463 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4465 p->decl = constructor_decl;
4466 p->require_constant_value = require_constant_value;
4467 p->require_constant_elements = require_constant_elements;
4468 p->constructor_stack = constructor_stack;
4469 p->constructor_range_stack = constructor_range_stack;
4470 p->elements = constructor_elements;
4471 p->spelling = spelling;
4472 p->spelling_base = spelling_base;
4473 p->spelling_size = spelling_size;
4474 p->top_level = constructor_top_level;
4475 p->next = initializer_stack;
4476 initializer_stack = p;
4478 constructor_decl = decl;
4479 constructor_designated = 0;
4480 constructor_top_level = top_level;
4482 if (decl != 0 && decl != error_mark_node)
4484 require_constant_value = TREE_STATIC (decl);
4485 require_constant_elements
4486 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4487 /* For a scalar, you can always use any value to initialize,
4488 even within braces. */
4489 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4490 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4491 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4492 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4493 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4495 else
4497 require_constant_value = 0;
4498 require_constant_elements = 0;
4499 locus = "(anonymous)";
4502 constructor_stack = 0;
4503 constructor_range_stack = 0;
4505 missing_braces_mentioned = 0;
4507 spelling_base = 0;
4508 spelling_size = 0;
4509 RESTORE_SPELLING_DEPTH (0);
4511 if (locus)
4512 push_string (locus);
4515 void
4516 finish_init (void)
4518 struct initializer_stack *p = initializer_stack;
4520 /* Free the whole constructor stack of this initializer. */
4521 while (constructor_stack)
4523 struct constructor_stack *q = constructor_stack;
4524 constructor_stack = q->next;
4525 free (q);
4528 gcc_assert (!constructor_range_stack);
4530 /* Pop back to the data of the outer initializer (if any). */
4531 free (spelling_base);
4533 constructor_decl = p->decl;
4534 require_constant_value = p->require_constant_value;
4535 require_constant_elements = p->require_constant_elements;
4536 constructor_stack = p->constructor_stack;
4537 constructor_range_stack = p->constructor_range_stack;
4538 constructor_elements = p->elements;
4539 spelling = p->spelling;
4540 spelling_base = p->spelling_base;
4541 spelling_size = p->spelling_size;
4542 constructor_top_level = p->top_level;
4543 initializer_stack = p->next;
4544 free (p);
4547 /* Call here when we see the initializer is surrounded by braces.
4548 This is instead of a call to push_init_level;
4549 it is matched by a call to pop_init_level.
4551 TYPE is the type to initialize, for a constructor expression.
4552 For an initializer for a decl, TYPE is zero. */
4554 void
4555 really_start_incremental_init (tree type)
4557 struct constructor_stack *p = XNEW (struct constructor_stack);
4559 if (type == 0)
4560 type = TREE_TYPE (constructor_decl);
4562 if (targetm.vector_opaque_p (type))
4563 error ("opaque vector types cannot be initialized");
4565 p->type = constructor_type;
4566 p->fields = constructor_fields;
4567 p->index = constructor_index;
4568 p->max_index = constructor_max_index;
4569 p->unfilled_index = constructor_unfilled_index;
4570 p->unfilled_fields = constructor_unfilled_fields;
4571 p->bit_index = constructor_bit_index;
4572 p->elements = constructor_elements;
4573 p->constant = constructor_constant;
4574 p->simple = constructor_simple;
4575 p->erroneous = constructor_erroneous;
4576 p->pending_elts = constructor_pending_elts;
4577 p->depth = constructor_depth;
4578 p->replacement_value.value = 0;
4579 p->replacement_value.original_code = ERROR_MARK;
4580 p->implicit = 0;
4581 p->range_stack = 0;
4582 p->outer = 0;
4583 p->incremental = constructor_incremental;
4584 p->designated = constructor_designated;
4585 p->next = 0;
4586 constructor_stack = p;
4588 constructor_constant = 1;
4589 constructor_simple = 1;
4590 constructor_depth = SPELLING_DEPTH ();
4591 constructor_elements = 0;
4592 constructor_pending_elts = 0;
4593 constructor_type = type;
4594 constructor_incremental = 1;
4595 constructor_designated = 0;
4596 designator_depth = 0;
4597 designator_errorneous = 0;
4599 if (TREE_CODE (constructor_type) == RECORD_TYPE
4600 || TREE_CODE (constructor_type) == UNION_TYPE)
4602 constructor_fields = TYPE_FIELDS (constructor_type);
4603 /* Skip any nameless bit fields at the beginning. */
4604 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4605 && DECL_NAME (constructor_fields) == 0)
4606 constructor_fields = TREE_CHAIN (constructor_fields);
4608 constructor_unfilled_fields = constructor_fields;
4609 constructor_bit_index = bitsize_zero_node;
4611 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4613 if (TYPE_DOMAIN (constructor_type))
4615 constructor_max_index
4616 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4618 /* Detect non-empty initializations of zero-length arrays. */
4619 if (constructor_max_index == NULL_TREE
4620 && TYPE_SIZE (constructor_type))
4621 constructor_max_index = build_int_cst (NULL_TREE, -1);
4623 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4624 to initialize VLAs will cause a proper error; avoid tree
4625 checking errors as well by setting a safe value. */
4626 if (constructor_max_index
4627 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4628 constructor_max_index = build_int_cst (NULL_TREE, -1);
4630 constructor_index
4631 = convert (bitsizetype,
4632 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4634 else
4635 constructor_index = bitsize_zero_node;
4637 constructor_unfilled_index = constructor_index;
4639 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4641 /* Vectors are like simple fixed-size arrays. */
4642 constructor_max_index =
4643 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4644 constructor_index = convert (bitsizetype, bitsize_zero_node);
4645 constructor_unfilled_index = constructor_index;
4647 else
4649 /* Handle the case of int x = {5}; */
4650 constructor_fields = constructor_type;
4651 constructor_unfilled_fields = constructor_type;
4655 /* Push down into a subobject, for initialization.
4656 If this is for an explicit set of braces, IMPLICIT is 0.
4657 If it is because the next element belongs at a lower level,
4658 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4660 void
4661 push_init_level (int implicit)
4663 struct constructor_stack *p;
4664 tree value = NULL_TREE;
4666 /* If we've exhausted any levels that didn't have braces,
4667 pop them now. */
4668 while (constructor_stack->implicit)
4670 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4671 || TREE_CODE (constructor_type) == UNION_TYPE)
4672 && constructor_fields == 0)
4673 process_init_element (pop_init_level (1));
4674 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4675 && constructor_max_index
4676 && tree_int_cst_lt (constructor_max_index, constructor_index))
4677 process_init_element (pop_init_level (1));
4678 else
4679 break;
4682 /* Unless this is an explicit brace, we need to preserve previous
4683 content if any. */
4684 if (implicit)
4686 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4687 || TREE_CODE (constructor_type) == UNION_TYPE)
4688 && constructor_fields)
4689 value = find_init_member (constructor_fields);
4690 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4691 value = find_init_member (constructor_index);
4694 p = XNEW (struct constructor_stack);
4695 p->type = constructor_type;
4696 p->fields = constructor_fields;
4697 p->index = constructor_index;
4698 p->max_index = constructor_max_index;
4699 p->unfilled_index = constructor_unfilled_index;
4700 p->unfilled_fields = constructor_unfilled_fields;
4701 p->bit_index = constructor_bit_index;
4702 p->elements = constructor_elements;
4703 p->constant = constructor_constant;
4704 p->simple = constructor_simple;
4705 p->erroneous = constructor_erroneous;
4706 p->pending_elts = constructor_pending_elts;
4707 p->depth = constructor_depth;
4708 p->replacement_value.value = 0;
4709 p->replacement_value.original_code = ERROR_MARK;
4710 p->implicit = implicit;
4711 p->outer = 0;
4712 p->incremental = constructor_incremental;
4713 p->designated = constructor_designated;
4714 p->next = constructor_stack;
4715 p->range_stack = 0;
4716 constructor_stack = p;
4718 constructor_constant = 1;
4719 constructor_simple = 1;
4720 constructor_depth = SPELLING_DEPTH ();
4721 constructor_elements = 0;
4722 constructor_incremental = 1;
4723 constructor_designated = 0;
4724 constructor_pending_elts = 0;
4725 if (!implicit)
4727 p->range_stack = constructor_range_stack;
4728 constructor_range_stack = 0;
4729 designator_depth = 0;
4730 designator_errorneous = 0;
4733 /* Don't die if an entire brace-pair level is superfluous
4734 in the containing level. */
4735 if (constructor_type == 0)
4737 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4738 || TREE_CODE (constructor_type) == UNION_TYPE)
4740 /* Don't die if there are extra init elts at the end. */
4741 if (constructor_fields == 0)
4742 constructor_type = 0;
4743 else
4745 constructor_type = TREE_TYPE (constructor_fields);
4746 push_member_name (constructor_fields);
4747 constructor_depth++;
4750 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4752 constructor_type = TREE_TYPE (constructor_type);
4753 push_array_bounds (tree_low_cst (constructor_index, 0));
4754 constructor_depth++;
4757 if (constructor_type == 0)
4759 error_init ("extra brace group at end of initializer");
4760 constructor_fields = 0;
4761 constructor_unfilled_fields = 0;
4762 return;
4765 if (value && TREE_CODE (value) == CONSTRUCTOR)
4767 constructor_constant = TREE_CONSTANT (value);
4768 constructor_simple = TREE_STATIC (value);
4769 constructor_elements = CONSTRUCTOR_ELTS (value);
4770 if (constructor_elements
4771 && (TREE_CODE (constructor_type) == RECORD_TYPE
4772 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4773 set_nonincremental_init ();
4776 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4778 missing_braces_mentioned = 1;
4779 warning_init ("missing braces around initializer");
4782 if (TREE_CODE (constructor_type) == RECORD_TYPE
4783 || TREE_CODE (constructor_type) == UNION_TYPE)
4785 constructor_fields = TYPE_FIELDS (constructor_type);
4786 /* Skip any nameless bit fields at the beginning. */
4787 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4788 && DECL_NAME (constructor_fields) == 0)
4789 constructor_fields = TREE_CHAIN (constructor_fields);
4791 constructor_unfilled_fields = constructor_fields;
4792 constructor_bit_index = bitsize_zero_node;
4794 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4796 /* Vectors are like simple fixed-size arrays. */
4797 constructor_max_index =
4798 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4799 constructor_index = convert (bitsizetype, integer_zero_node);
4800 constructor_unfilled_index = constructor_index;
4802 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4804 if (TYPE_DOMAIN (constructor_type))
4806 constructor_max_index
4807 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4809 /* Detect non-empty initializations of zero-length arrays. */
4810 if (constructor_max_index == NULL_TREE
4811 && TYPE_SIZE (constructor_type))
4812 constructor_max_index = build_int_cst (NULL_TREE, -1);
4814 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4815 to initialize VLAs will cause a proper error; avoid tree
4816 checking errors as well by setting a safe value. */
4817 if (constructor_max_index
4818 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4819 constructor_max_index = build_int_cst (NULL_TREE, -1);
4821 constructor_index
4822 = convert (bitsizetype,
4823 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4825 else
4826 constructor_index = bitsize_zero_node;
4828 constructor_unfilled_index = constructor_index;
4829 if (value && TREE_CODE (value) == STRING_CST)
4831 /* We need to split the char/wchar array into individual
4832 characters, so that we don't have to special case it
4833 everywhere. */
4834 set_nonincremental_init_from_string (value);
4837 else
4839 warning_init ("braces around scalar initializer");
4840 constructor_fields = constructor_type;
4841 constructor_unfilled_fields = constructor_type;
4845 /* At the end of an implicit or explicit brace level,
4846 finish up that level of constructor. If a single expression
4847 with redundant braces initialized that level, return the
4848 c_expr structure for that expression. Otherwise, the original_code
4849 element is set to ERROR_MARK.
4850 If we were outputting the elements as they are read, return 0 as the value
4851 from inner levels (process_init_element ignores that),
4852 but return error_mark_node as the value from the outermost level
4853 (that's what we want to put in DECL_INITIAL).
4854 Otherwise, return a CONSTRUCTOR expression as the value. */
4856 struct c_expr
4857 pop_init_level (int implicit)
4859 struct constructor_stack *p;
4860 struct c_expr ret;
4861 ret.value = 0;
4862 ret.original_code = ERROR_MARK;
4864 if (implicit == 0)
4866 /* When we come to an explicit close brace,
4867 pop any inner levels that didn't have explicit braces. */
4868 while (constructor_stack->implicit)
4869 process_init_element (pop_init_level (1));
4871 gcc_assert (!constructor_range_stack);
4874 /* Now output all pending elements. */
4875 constructor_incremental = 1;
4876 output_pending_init_elements (1);
4878 p = constructor_stack;
4880 /* Error for initializing a flexible array member, or a zero-length
4881 array member in an inappropriate context. */
4882 if (constructor_type && constructor_fields
4883 && TREE_CODE (constructor_type) == ARRAY_TYPE
4884 && TYPE_DOMAIN (constructor_type)
4885 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4887 /* Silently discard empty initializations. The parser will
4888 already have pedwarned for empty brackets. */
4889 if (integer_zerop (constructor_unfilled_index))
4890 constructor_type = NULL_TREE;
4891 else
4893 gcc_assert (!TYPE_SIZE (constructor_type));
4895 if (constructor_depth > 2)
4896 error_init ("initialization of flexible array member in a nested context");
4897 else if (pedantic)
4898 pedwarn_init ("initialization of a flexible array member");
4900 /* We have already issued an error message for the existence
4901 of a flexible array member not at the end of the structure.
4902 Discard the initializer so that we do not abort later. */
4903 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4904 constructor_type = NULL_TREE;
4908 /* Warn when some struct elements are implicitly initialized to zero. */
4909 if (warn_missing_field_initializers
4910 && constructor_type
4911 && TREE_CODE (constructor_type) == RECORD_TYPE
4912 && constructor_unfilled_fields)
4914 /* Do not warn for flexible array members or zero-length arrays. */
4915 while (constructor_unfilled_fields
4916 && (!DECL_SIZE (constructor_unfilled_fields)
4917 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4918 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4920 /* Do not warn if this level of the initializer uses member
4921 designators; it is likely to be deliberate. */
4922 if (constructor_unfilled_fields && !constructor_designated)
4924 push_member_name (constructor_unfilled_fields);
4925 warning_init ("missing initializer");
4926 RESTORE_SPELLING_DEPTH (constructor_depth);
4930 /* Pad out the end of the structure. */
4931 if (p->replacement_value.value)
4932 /* If this closes a superfluous brace pair,
4933 just pass out the element between them. */
4934 ret = p->replacement_value;
4935 else if (constructor_type == 0)
4937 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4938 && TREE_CODE (constructor_type) != UNION_TYPE
4939 && TREE_CODE (constructor_type) != ARRAY_TYPE
4940 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4942 /* A nonincremental scalar initializer--just return
4943 the element, after verifying there is just one. */
4944 if (constructor_elements == 0)
4946 if (!constructor_erroneous)
4947 error_init ("empty scalar initializer");
4948 ret.value = error_mark_node;
4950 else if (TREE_CHAIN (constructor_elements) != 0)
4952 error_init ("extra elements in scalar initializer");
4953 ret.value = TREE_VALUE (constructor_elements);
4955 else
4956 ret.value = TREE_VALUE (constructor_elements);
4958 else
4960 if (constructor_erroneous)
4961 ret.value = error_mark_node;
4962 else
4964 ret.value = build_constructor (constructor_type,
4965 nreverse (constructor_elements));
4966 if (constructor_constant)
4967 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
4968 if (constructor_constant && constructor_simple)
4969 TREE_STATIC (ret.value) = 1;
4973 constructor_type = p->type;
4974 constructor_fields = p->fields;
4975 constructor_index = p->index;
4976 constructor_max_index = p->max_index;
4977 constructor_unfilled_index = p->unfilled_index;
4978 constructor_unfilled_fields = p->unfilled_fields;
4979 constructor_bit_index = p->bit_index;
4980 constructor_elements = p->elements;
4981 constructor_constant = p->constant;
4982 constructor_simple = p->simple;
4983 constructor_erroneous = p->erroneous;
4984 constructor_incremental = p->incremental;
4985 constructor_designated = p->designated;
4986 constructor_pending_elts = p->pending_elts;
4987 constructor_depth = p->depth;
4988 if (!p->implicit)
4989 constructor_range_stack = p->range_stack;
4990 RESTORE_SPELLING_DEPTH (constructor_depth);
4992 constructor_stack = p->next;
4993 free (p);
4995 if (ret.value == 0)
4997 if (constructor_stack == 0)
4999 ret.value = error_mark_node;
5000 return ret;
5002 return ret;
5004 return ret;
5007 /* Common handling for both array range and field name designators.
5008 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5010 static int
5011 set_designator (int array)
5013 tree subtype;
5014 enum tree_code subcode;
5016 /* Don't die if an entire brace-pair level is superfluous
5017 in the containing level. */
5018 if (constructor_type == 0)
5019 return 1;
5021 /* If there were errors in this designator list already, bail out
5022 silently. */
5023 if (designator_errorneous)
5024 return 1;
5026 if (!designator_depth)
5028 gcc_assert (!constructor_range_stack);
5030 /* Designator list starts at the level of closest explicit
5031 braces. */
5032 while (constructor_stack->implicit)
5033 process_init_element (pop_init_level (1));
5034 constructor_designated = 1;
5035 return 0;
5038 switch (TREE_CODE (constructor_type))
5040 case RECORD_TYPE:
5041 case UNION_TYPE:
5042 subtype = TREE_TYPE (constructor_fields);
5043 if (subtype != error_mark_node)
5044 subtype = TYPE_MAIN_VARIANT (subtype);
5045 break;
5046 case ARRAY_TYPE:
5047 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5048 break;
5049 default:
5050 gcc_unreachable ();
5053 subcode = TREE_CODE (subtype);
5054 if (array && subcode != ARRAY_TYPE)
5056 error_init ("array index in non-array initializer");
5057 return 1;
5059 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5061 error_init ("field name not in record or union initializer");
5062 return 1;
5065 constructor_designated = 1;
5066 push_init_level (2);
5067 return 0;
5070 /* If there are range designators in designator list, push a new designator
5071 to constructor_range_stack. RANGE_END is end of such stack range or
5072 NULL_TREE if there is no range designator at this level. */
5074 static void
5075 push_range_stack (tree range_end)
5077 struct constructor_range_stack *p;
5079 p = GGC_NEW (struct constructor_range_stack);
5080 p->prev = constructor_range_stack;
5081 p->next = 0;
5082 p->fields = constructor_fields;
5083 p->range_start = constructor_index;
5084 p->index = constructor_index;
5085 p->stack = constructor_stack;
5086 p->range_end = range_end;
5087 if (constructor_range_stack)
5088 constructor_range_stack->next = p;
5089 constructor_range_stack = p;
5092 /* Within an array initializer, specify the next index to be initialized.
5093 FIRST is that index. If LAST is nonzero, then initialize a range
5094 of indices, running from FIRST through LAST. */
5096 void
5097 set_init_index (tree first, tree last)
5099 if (set_designator (1))
5100 return;
5102 designator_errorneous = 1;
5104 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5105 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5107 error_init ("array index in initializer not of integer type");
5108 return;
5111 while ((TREE_CODE (first) == NOP_EXPR
5112 || TREE_CODE (first) == CONVERT_EXPR
5113 || TREE_CODE (first) == NON_LVALUE_EXPR)
5114 && (TYPE_MODE (TREE_TYPE (first))
5115 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5116 first = TREE_OPERAND (first, 0);
5118 if (last)
5119 while ((TREE_CODE (last) == NOP_EXPR
5120 || TREE_CODE (last) == CONVERT_EXPR
5121 || TREE_CODE (last) == NON_LVALUE_EXPR)
5122 && (TYPE_MODE (TREE_TYPE (last))
5123 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5124 last = TREE_OPERAND (last, 0);
5126 if (TREE_CODE (first) != INTEGER_CST)
5127 error_init ("nonconstant array index in initializer");
5128 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5129 error_init ("nonconstant array index in initializer");
5130 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5131 error_init ("array index in non-array initializer");
5132 else if (tree_int_cst_sgn (first) == -1)
5133 error_init ("array index in initializer exceeds array bounds");
5134 else if (constructor_max_index
5135 && tree_int_cst_lt (constructor_max_index, first))
5136 error_init ("array index in initializer exceeds array bounds");
5137 else
5139 constructor_index = convert (bitsizetype, first);
5141 if (last)
5143 if (tree_int_cst_equal (first, last))
5144 last = 0;
5145 else if (tree_int_cst_lt (last, first))
5147 error_init ("empty index range in initializer");
5148 last = 0;
5150 else
5152 last = convert (bitsizetype, last);
5153 if (constructor_max_index != 0
5154 && tree_int_cst_lt (constructor_max_index, last))
5156 error_init ("array index range in initializer exceeds array bounds");
5157 last = 0;
5162 designator_depth++;
5163 designator_errorneous = 0;
5164 if (constructor_range_stack || last)
5165 push_range_stack (last);
5169 /* Within a struct initializer, specify the next field to be initialized. */
5171 void
5172 set_init_label (tree fieldname)
5174 tree tail;
5176 if (set_designator (0))
5177 return;
5179 designator_errorneous = 1;
5181 if (TREE_CODE (constructor_type) != RECORD_TYPE
5182 && TREE_CODE (constructor_type) != UNION_TYPE)
5184 error_init ("field name not in record or union initializer");
5185 return;
5188 for (tail = TYPE_FIELDS (constructor_type); tail;
5189 tail = TREE_CHAIN (tail))
5191 if (DECL_NAME (tail) == fieldname)
5192 break;
5195 if (tail == 0)
5196 error ("unknown field %qs specified in initializer",
5197 IDENTIFIER_POINTER (fieldname));
5198 else
5200 constructor_fields = tail;
5201 designator_depth++;
5202 designator_errorneous = 0;
5203 if (constructor_range_stack)
5204 push_range_stack (NULL_TREE);
5208 /* Add a new initializer to the tree of pending initializers. PURPOSE
5209 identifies the initializer, either array index or field in a structure.
5210 VALUE is the value of that index or field. */
5212 static void
5213 add_pending_init (tree purpose, tree value)
5215 struct init_node *p, **q, *r;
5217 q = &constructor_pending_elts;
5218 p = 0;
5220 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5222 while (*q != 0)
5224 p = *q;
5225 if (tree_int_cst_lt (purpose, p->purpose))
5226 q = &p->left;
5227 else if (tree_int_cst_lt (p->purpose, purpose))
5228 q = &p->right;
5229 else
5231 if (TREE_SIDE_EFFECTS (p->value))
5232 warning_init ("initialized field with side-effects overwritten");
5233 p->value = value;
5234 return;
5238 else
5240 tree bitpos;
5242 bitpos = bit_position (purpose);
5243 while (*q != NULL)
5245 p = *q;
5246 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5247 q = &p->left;
5248 else if (p->purpose != purpose)
5249 q = &p->right;
5250 else
5252 if (TREE_SIDE_EFFECTS (p->value))
5253 warning_init ("initialized field with side-effects overwritten");
5254 p->value = value;
5255 return;
5260 r = GGC_NEW (struct init_node);
5261 r->purpose = purpose;
5262 r->value = value;
5264 *q = r;
5265 r->parent = p;
5266 r->left = 0;
5267 r->right = 0;
5268 r->balance = 0;
5270 while (p)
5272 struct init_node *s;
5274 if (r == p->left)
5276 if (p->balance == 0)
5277 p->balance = -1;
5278 else if (p->balance < 0)
5280 if (r->balance < 0)
5282 /* L rotation. */
5283 p->left = r->right;
5284 if (p->left)
5285 p->left->parent = p;
5286 r->right = p;
5288 p->balance = 0;
5289 r->balance = 0;
5291 s = p->parent;
5292 p->parent = r;
5293 r->parent = s;
5294 if (s)
5296 if (s->left == p)
5297 s->left = r;
5298 else
5299 s->right = r;
5301 else
5302 constructor_pending_elts = r;
5304 else
5306 /* LR rotation. */
5307 struct init_node *t = r->right;
5309 r->right = t->left;
5310 if (r->right)
5311 r->right->parent = r;
5312 t->left = r;
5314 p->left = t->right;
5315 if (p->left)
5316 p->left->parent = p;
5317 t->right = p;
5319 p->balance = t->balance < 0;
5320 r->balance = -(t->balance > 0);
5321 t->balance = 0;
5323 s = p->parent;
5324 p->parent = t;
5325 r->parent = t;
5326 t->parent = s;
5327 if (s)
5329 if (s->left == p)
5330 s->left = t;
5331 else
5332 s->right = t;
5334 else
5335 constructor_pending_elts = t;
5337 break;
5339 else
5341 /* p->balance == +1; growth of left side balances the node. */
5342 p->balance = 0;
5343 break;
5346 else /* r == p->right */
5348 if (p->balance == 0)
5349 /* Growth propagation from right side. */
5350 p->balance++;
5351 else if (p->balance > 0)
5353 if (r->balance > 0)
5355 /* R rotation. */
5356 p->right = r->left;
5357 if (p->right)
5358 p->right->parent = p;
5359 r->left = p;
5361 p->balance = 0;
5362 r->balance = 0;
5364 s = p->parent;
5365 p->parent = r;
5366 r->parent = s;
5367 if (s)
5369 if (s->left == p)
5370 s->left = r;
5371 else
5372 s->right = r;
5374 else
5375 constructor_pending_elts = r;
5377 else /* r->balance == -1 */
5379 /* RL rotation */
5380 struct init_node *t = r->left;
5382 r->left = t->right;
5383 if (r->left)
5384 r->left->parent = r;
5385 t->right = r;
5387 p->right = t->left;
5388 if (p->right)
5389 p->right->parent = p;
5390 t->left = p;
5392 r->balance = (t->balance < 0);
5393 p->balance = -(t->balance > 0);
5394 t->balance = 0;
5396 s = p->parent;
5397 p->parent = t;
5398 r->parent = t;
5399 t->parent = s;
5400 if (s)
5402 if (s->left == p)
5403 s->left = t;
5404 else
5405 s->right = t;
5407 else
5408 constructor_pending_elts = t;
5410 break;
5412 else
5414 /* p->balance == -1; growth of right side balances the node. */
5415 p->balance = 0;
5416 break;
5420 r = p;
5421 p = p->parent;
5425 /* Build AVL tree from a sorted chain. */
5427 static void
5428 set_nonincremental_init (void)
5430 tree chain;
5432 if (TREE_CODE (constructor_type) != RECORD_TYPE
5433 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5434 return;
5436 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5437 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5438 constructor_elements = 0;
5439 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5441 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5442 /* Skip any nameless bit fields at the beginning. */
5443 while (constructor_unfilled_fields != 0
5444 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5445 && DECL_NAME (constructor_unfilled_fields) == 0)
5446 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5449 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5451 if (TYPE_DOMAIN (constructor_type))
5452 constructor_unfilled_index
5453 = convert (bitsizetype,
5454 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5455 else
5456 constructor_unfilled_index = bitsize_zero_node;
5458 constructor_incremental = 0;
5461 /* Build AVL tree from a string constant. */
5463 static void
5464 set_nonincremental_init_from_string (tree str)
5466 tree value, purpose, type;
5467 HOST_WIDE_INT val[2];
5468 const char *p, *end;
5469 int byte, wchar_bytes, charwidth, bitpos;
5471 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5473 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5474 == TYPE_PRECISION (char_type_node))
5475 wchar_bytes = 1;
5476 else
5478 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5479 == TYPE_PRECISION (wchar_type_node));
5480 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5482 charwidth = TYPE_PRECISION (char_type_node);
5483 type = TREE_TYPE (constructor_type);
5484 p = TREE_STRING_POINTER (str);
5485 end = p + TREE_STRING_LENGTH (str);
5487 for (purpose = bitsize_zero_node;
5488 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5489 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5491 if (wchar_bytes == 1)
5493 val[1] = (unsigned char) *p++;
5494 val[0] = 0;
5496 else
5498 val[0] = 0;
5499 val[1] = 0;
5500 for (byte = 0; byte < wchar_bytes; byte++)
5502 if (BYTES_BIG_ENDIAN)
5503 bitpos = (wchar_bytes - byte - 1) * charwidth;
5504 else
5505 bitpos = byte * charwidth;
5506 val[bitpos < HOST_BITS_PER_WIDE_INT]
5507 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5508 << (bitpos % HOST_BITS_PER_WIDE_INT);
5512 if (!TYPE_UNSIGNED (type))
5514 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5515 if (bitpos < HOST_BITS_PER_WIDE_INT)
5517 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5519 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5520 val[0] = -1;
5523 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5525 if (val[1] < 0)
5526 val[0] = -1;
5528 else if (val[0] & (((HOST_WIDE_INT) 1)
5529 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5530 val[0] |= ((HOST_WIDE_INT) -1)
5531 << (bitpos - HOST_BITS_PER_WIDE_INT);
5534 value = build_int_cst_wide (type, val[1], val[0]);
5535 add_pending_init (purpose, value);
5538 constructor_incremental = 0;
5541 /* Return value of FIELD in pending initializer or zero if the field was
5542 not initialized yet. */
5544 static tree
5545 find_init_member (tree field)
5547 struct init_node *p;
5549 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5551 if (constructor_incremental
5552 && tree_int_cst_lt (field, constructor_unfilled_index))
5553 set_nonincremental_init ();
5555 p = constructor_pending_elts;
5556 while (p)
5558 if (tree_int_cst_lt (field, p->purpose))
5559 p = p->left;
5560 else if (tree_int_cst_lt (p->purpose, field))
5561 p = p->right;
5562 else
5563 return p->value;
5566 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5568 tree bitpos = bit_position (field);
5570 if (constructor_incremental
5571 && (!constructor_unfilled_fields
5572 || tree_int_cst_lt (bitpos,
5573 bit_position (constructor_unfilled_fields))))
5574 set_nonincremental_init ();
5576 p = constructor_pending_elts;
5577 while (p)
5579 if (field == p->purpose)
5580 return p->value;
5581 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5582 p = p->left;
5583 else
5584 p = p->right;
5587 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5589 if (constructor_elements
5590 && TREE_PURPOSE (constructor_elements) == field)
5591 return TREE_VALUE (constructor_elements);
5593 return 0;
5596 /* "Output" the next constructor element.
5597 At top level, really output it to assembler code now.
5598 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5599 TYPE is the data type that the containing data type wants here.
5600 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5601 If VALUE is a string constant, STRICT_STRING is true if it is
5602 unparenthesized or we should not warn here for it being parenthesized.
5603 For other types of VALUE, STRICT_STRING is not used.
5605 PENDING if non-nil means output pending elements that belong
5606 right after this element. (PENDING is normally 1;
5607 it is 0 while outputting pending elements, to avoid recursion.) */
5609 static void
5610 output_init_element (tree value, bool strict_string, tree type, tree field,
5611 int pending)
5613 if (type == error_mark_node || value == error_mark_node)
5615 constructor_erroneous = 1;
5616 return;
5618 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5619 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5620 && !(TREE_CODE (value) == STRING_CST
5621 && TREE_CODE (type) == ARRAY_TYPE
5622 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5623 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5624 TYPE_MAIN_VARIANT (type))))
5625 value = default_conversion (value);
5627 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5628 && require_constant_value && !flag_isoc99 && pending)
5630 /* As an extension, allow initializing objects with static storage
5631 duration with compound literals (which are then treated just as
5632 the brace enclosed list they contain). */
5633 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5634 value = DECL_INITIAL (decl);
5637 if (value == error_mark_node)
5638 constructor_erroneous = 1;
5639 else if (!TREE_CONSTANT (value))
5640 constructor_constant = 0;
5641 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5642 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5643 || TREE_CODE (constructor_type) == UNION_TYPE)
5644 && DECL_C_BIT_FIELD (field)
5645 && TREE_CODE (value) != INTEGER_CST))
5646 constructor_simple = 0;
5648 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5650 if (require_constant_value)
5652 error_init ("initializer element is not constant");
5653 value = error_mark_node;
5655 else if (require_constant_elements)
5656 pedwarn ("initializer element is not computable at load time");
5659 /* If this field is empty (and not at the end of structure),
5660 don't do anything other than checking the initializer. */
5661 if (field
5662 && (TREE_TYPE (field) == error_mark_node
5663 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5664 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5665 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5666 || TREE_CHAIN (field)))))
5667 return;
5669 value = digest_init (type, value, strict_string, require_constant_value);
5670 if (value == error_mark_node)
5672 constructor_erroneous = 1;
5673 return;
5676 /* If this element doesn't come next in sequence,
5677 put it on constructor_pending_elts. */
5678 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5679 && (!constructor_incremental
5680 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5682 if (constructor_incremental
5683 && tree_int_cst_lt (field, constructor_unfilled_index))
5684 set_nonincremental_init ();
5686 add_pending_init (field, value);
5687 return;
5689 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5690 && (!constructor_incremental
5691 || field != constructor_unfilled_fields))
5693 /* We do this for records but not for unions. In a union,
5694 no matter which field is specified, it can be initialized
5695 right away since it starts at the beginning of the union. */
5696 if (constructor_incremental)
5698 if (!constructor_unfilled_fields)
5699 set_nonincremental_init ();
5700 else
5702 tree bitpos, unfillpos;
5704 bitpos = bit_position (field);
5705 unfillpos = bit_position (constructor_unfilled_fields);
5707 if (tree_int_cst_lt (bitpos, unfillpos))
5708 set_nonincremental_init ();
5712 add_pending_init (field, value);
5713 return;
5715 else if (TREE_CODE (constructor_type) == UNION_TYPE
5716 && constructor_elements)
5718 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5719 warning_init ("initialized field with side-effects overwritten");
5721 /* We can have just one union field set. */
5722 constructor_elements = 0;
5725 /* Otherwise, output this element either to
5726 constructor_elements or to the assembler file. */
5728 if (field && TREE_CODE (field) == INTEGER_CST)
5729 field = copy_node (field);
5730 constructor_elements
5731 = tree_cons (field, value, constructor_elements);
5733 /* Advance the variable that indicates sequential elements output. */
5734 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5735 constructor_unfilled_index
5736 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5737 bitsize_one_node);
5738 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5740 constructor_unfilled_fields
5741 = TREE_CHAIN (constructor_unfilled_fields);
5743 /* Skip any nameless bit fields. */
5744 while (constructor_unfilled_fields != 0
5745 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5746 && DECL_NAME (constructor_unfilled_fields) == 0)
5747 constructor_unfilled_fields =
5748 TREE_CHAIN (constructor_unfilled_fields);
5750 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5751 constructor_unfilled_fields = 0;
5753 /* Now output any pending elements which have become next. */
5754 if (pending)
5755 output_pending_init_elements (0);
5758 /* Output any pending elements which have become next.
5759 As we output elements, constructor_unfilled_{fields,index}
5760 advances, which may cause other elements to become next;
5761 if so, they too are output.
5763 If ALL is 0, we return when there are
5764 no more pending elements to output now.
5766 If ALL is 1, we output space as necessary so that
5767 we can output all the pending elements. */
5769 static void
5770 output_pending_init_elements (int all)
5772 struct init_node *elt = constructor_pending_elts;
5773 tree next;
5775 retry:
5777 /* Look through the whole pending tree.
5778 If we find an element that should be output now,
5779 output it. Otherwise, set NEXT to the element
5780 that comes first among those still pending. */
5782 next = 0;
5783 while (elt)
5785 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5787 if (tree_int_cst_equal (elt->purpose,
5788 constructor_unfilled_index))
5789 output_init_element (elt->value, true,
5790 TREE_TYPE (constructor_type),
5791 constructor_unfilled_index, 0);
5792 else if (tree_int_cst_lt (constructor_unfilled_index,
5793 elt->purpose))
5795 /* Advance to the next smaller node. */
5796 if (elt->left)
5797 elt = elt->left;
5798 else
5800 /* We have reached the smallest node bigger than the
5801 current unfilled index. Fill the space first. */
5802 next = elt->purpose;
5803 break;
5806 else
5808 /* Advance to the next bigger node. */
5809 if (elt->right)
5810 elt = elt->right;
5811 else
5813 /* We have reached the biggest node in a subtree. Find
5814 the parent of it, which is the next bigger node. */
5815 while (elt->parent && elt->parent->right == elt)
5816 elt = elt->parent;
5817 elt = elt->parent;
5818 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5819 elt->purpose))
5821 next = elt->purpose;
5822 break;
5827 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5828 || TREE_CODE (constructor_type) == UNION_TYPE)
5830 tree ctor_unfilled_bitpos, elt_bitpos;
5832 /* If the current record is complete we are done. */
5833 if (constructor_unfilled_fields == 0)
5834 break;
5836 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5837 elt_bitpos = bit_position (elt->purpose);
5838 /* We can't compare fields here because there might be empty
5839 fields in between. */
5840 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5842 constructor_unfilled_fields = elt->purpose;
5843 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5844 elt->purpose, 0);
5846 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
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 field. 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
5872 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5873 bit_position (elt->purpose))))
5875 next = elt->purpose;
5876 break;
5883 /* Ordinarily return, but not if we want to output all
5884 and there are elements left. */
5885 if (!(all && next != 0))
5886 return;
5888 /* If it's not incremental, just skip over the gap, so that after
5889 jumping to retry we will output the next successive element. */
5890 if (TREE_CODE (constructor_type) == RECORD_TYPE
5891 || TREE_CODE (constructor_type) == UNION_TYPE)
5892 constructor_unfilled_fields = next;
5893 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5894 constructor_unfilled_index = next;
5896 /* ELT now points to the node in the pending tree with the next
5897 initializer to output. */
5898 goto retry;
5901 /* Add one non-braced element to the current constructor level.
5902 This adjusts the current position within the constructor's type.
5903 This may also start or terminate implicit levels
5904 to handle a partly-braced initializer.
5906 Once this has found the correct level for the new element,
5907 it calls output_init_element. */
5909 void
5910 process_init_element (struct c_expr value)
5912 tree orig_value = value.value;
5913 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5914 bool strict_string = value.original_code == STRING_CST;
5916 designator_depth = 0;
5917 designator_errorneous = 0;
5919 /* Handle superfluous braces around string cst as in
5920 char x[] = {"foo"}; */
5921 if (string_flag
5922 && constructor_type
5923 && TREE_CODE (constructor_type) == ARRAY_TYPE
5924 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5925 && integer_zerop (constructor_unfilled_index))
5927 if (constructor_stack->replacement_value.value)
5928 error_init ("excess elements in char array initializer");
5929 constructor_stack->replacement_value = value;
5930 return;
5933 if (constructor_stack->replacement_value.value != 0)
5935 error_init ("excess elements in struct initializer");
5936 return;
5939 /* Ignore elements of a brace group if it is entirely superfluous
5940 and has already been diagnosed. */
5941 if (constructor_type == 0)
5942 return;
5944 /* If we've exhausted any levels that didn't have braces,
5945 pop them now. */
5946 while (constructor_stack->implicit)
5948 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5949 || TREE_CODE (constructor_type) == UNION_TYPE)
5950 && constructor_fields == 0)
5951 process_init_element (pop_init_level (1));
5952 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5953 && (constructor_max_index == 0
5954 || tree_int_cst_lt (constructor_max_index,
5955 constructor_index)))
5956 process_init_element (pop_init_level (1));
5957 else
5958 break;
5961 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5962 if (constructor_range_stack)
5964 /* If value is a compound literal and we'll be just using its
5965 content, don't put it into a SAVE_EXPR. */
5966 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
5967 || !require_constant_value
5968 || flag_isoc99)
5969 value.value = save_expr (value.value);
5972 while (1)
5974 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5976 tree fieldtype;
5977 enum tree_code fieldcode;
5979 if (constructor_fields == 0)
5981 pedwarn_init ("excess elements in struct initializer");
5982 break;
5985 fieldtype = TREE_TYPE (constructor_fields);
5986 if (fieldtype != error_mark_node)
5987 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5988 fieldcode = TREE_CODE (fieldtype);
5990 /* Error for non-static initialization of a flexible array member. */
5991 if (fieldcode == ARRAY_TYPE
5992 && !require_constant_value
5993 && TYPE_SIZE (fieldtype) == NULL_TREE
5994 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5996 error_init ("non-static initialization of a flexible array member");
5997 break;
6000 /* Accept a string constant to initialize a subarray. */
6001 if (value.value != 0
6002 && fieldcode == ARRAY_TYPE
6003 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6004 && string_flag)
6005 value.value = orig_value;
6006 /* Otherwise, if we have come to a subaggregate,
6007 and we don't have an element of its type, push into it. */
6008 else if (value.value != 0
6009 && value.value != error_mark_node
6010 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6011 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6012 || fieldcode == UNION_TYPE))
6014 push_init_level (1);
6015 continue;
6018 if (value.value)
6020 push_member_name (constructor_fields);
6021 output_init_element (value.value, strict_string,
6022 fieldtype, constructor_fields, 1);
6023 RESTORE_SPELLING_DEPTH (constructor_depth);
6025 else
6026 /* Do the bookkeeping for an element that was
6027 directly output as a constructor. */
6029 /* For a record, keep track of end position of last field. */
6030 if (DECL_SIZE (constructor_fields))
6031 constructor_bit_index
6032 = size_binop (PLUS_EXPR,
6033 bit_position (constructor_fields),
6034 DECL_SIZE (constructor_fields));
6036 /* If the current field was the first one not yet written out,
6037 it isn't now, so update. */
6038 if (constructor_unfilled_fields == constructor_fields)
6040 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6041 /* Skip any nameless bit fields. */
6042 while (constructor_unfilled_fields != 0
6043 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6044 && DECL_NAME (constructor_unfilled_fields) == 0)
6045 constructor_unfilled_fields =
6046 TREE_CHAIN (constructor_unfilled_fields);
6050 constructor_fields = TREE_CHAIN (constructor_fields);
6051 /* Skip any nameless bit fields at the beginning. */
6052 while (constructor_fields != 0
6053 && DECL_C_BIT_FIELD (constructor_fields)
6054 && DECL_NAME (constructor_fields) == 0)
6055 constructor_fields = TREE_CHAIN (constructor_fields);
6057 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6059 tree fieldtype;
6060 enum tree_code fieldcode;
6062 if (constructor_fields == 0)
6064 pedwarn_init ("excess elements in union initializer");
6065 break;
6068 fieldtype = TREE_TYPE (constructor_fields);
6069 if (fieldtype != error_mark_node)
6070 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6071 fieldcode = TREE_CODE (fieldtype);
6073 /* Warn that traditional C rejects initialization of unions.
6074 We skip the warning if the value is zero. This is done
6075 under the assumption that the zero initializer in user
6076 code appears conditioned on e.g. __STDC__ to avoid
6077 "missing initializer" warnings and relies on default
6078 initialization to zero in the traditional C case.
6079 We also skip the warning if the initializer is designated,
6080 again on the assumption that this must be conditional on
6081 __STDC__ anyway (and we've already complained about the
6082 member-designator already). */
6083 if (warn_traditional && !in_system_header && !constructor_designated
6084 && !(value.value && (integer_zerop (value.value)
6085 || real_zerop (value.value))))
6086 warning ("traditional C rejects initialization of unions");
6088 /* Accept a string constant to initialize a subarray. */
6089 if (value.value != 0
6090 && fieldcode == ARRAY_TYPE
6091 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6092 && string_flag)
6093 value.value = orig_value;
6094 /* Otherwise, if we have come to a subaggregate,
6095 and we don't have an element of its type, push into it. */
6096 else if (value.value != 0
6097 && value.value != error_mark_node
6098 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6099 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6100 || fieldcode == UNION_TYPE))
6102 push_init_level (1);
6103 continue;
6106 if (value.value)
6108 push_member_name (constructor_fields);
6109 output_init_element (value.value, strict_string,
6110 fieldtype, constructor_fields, 1);
6111 RESTORE_SPELLING_DEPTH (constructor_depth);
6113 else
6114 /* Do the bookkeeping for an element that was
6115 directly output as a constructor. */
6117 constructor_bit_index = DECL_SIZE (constructor_fields);
6118 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6121 constructor_fields = 0;
6123 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6125 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6126 enum tree_code eltcode = TREE_CODE (elttype);
6128 /* Accept a string constant to initialize a subarray. */
6129 if (value.value != 0
6130 && eltcode == ARRAY_TYPE
6131 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6132 && string_flag)
6133 value.value = orig_value;
6134 /* Otherwise, if we have come to a subaggregate,
6135 and we don't have an element of its type, push into it. */
6136 else if (value.value != 0
6137 && value.value != error_mark_node
6138 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6139 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6140 || eltcode == UNION_TYPE))
6142 push_init_level (1);
6143 continue;
6146 if (constructor_max_index != 0
6147 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6148 || integer_all_onesp (constructor_max_index)))
6150 pedwarn_init ("excess elements in array initializer");
6151 break;
6154 /* Now output the actual element. */
6155 if (value.value)
6157 push_array_bounds (tree_low_cst (constructor_index, 0));
6158 output_init_element (value.value, strict_string,
6159 elttype, constructor_index, 1);
6160 RESTORE_SPELLING_DEPTH (constructor_depth);
6163 constructor_index
6164 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6166 if (!value.value)
6167 /* If we are doing the bookkeeping for an element that was
6168 directly output as a constructor, we must update
6169 constructor_unfilled_index. */
6170 constructor_unfilled_index = constructor_index;
6172 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6174 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6176 /* Do a basic check of initializer size. Note that vectors
6177 always have a fixed size derived from their type. */
6178 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6180 pedwarn_init ("excess elements in vector initializer");
6181 break;
6184 /* Now output the actual element. */
6185 if (value.value)
6186 output_init_element (value.value, strict_string,
6187 elttype, constructor_index, 1);
6189 constructor_index
6190 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6192 if (!value.value)
6193 /* If we are doing the bookkeeping for an element that was
6194 directly output as a constructor, we must update
6195 constructor_unfilled_index. */
6196 constructor_unfilled_index = constructor_index;
6199 /* Handle the sole element allowed in a braced initializer
6200 for a scalar variable. */
6201 else if (constructor_fields == 0)
6203 pedwarn_init ("excess elements in scalar initializer");
6204 break;
6206 else
6208 if (value.value)
6209 output_init_element (value.value, strict_string,
6210 constructor_type, NULL_TREE, 1);
6211 constructor_fields = 0;
6214 /* Handle range initializers either at this level or anywhere higher
6215 in the designator stack. */
6216 if (constructor_range_stack)
6218 struct constructor_range_stack *p, *range_stack;
6219 int finish = 0;
6221 range_stack = constructor_range_stack;
6222 constructor_range_stack = 0;
6223 while (constructor_stack != range_stack->stack)
6225 gcc_assert (constructor_stack->implicit);
6226 process_init_element (pop_init_level (1));
6228 for (p = range_stack;
6229 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6230 p = p->prev)
6232 gcc_assert (constructor_stack->implicit);
6233 process_init_element (pop_init_level (1));
6236 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6237 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6238 finish = 1;
6240 while (1)
6242 constructor_index = p->index;
6243 constructor_fields = p->fields;
6244 if (finish && p->range_end && p->index == p->range_start)
6246 finish = 0;
6247 p->prev = 0;
6249 p = p->next;
6250 if (!p)
6251 break;
6252 push_init_level (2);
6253 p->stack = constructor_stack;
6254 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6255 p->index = p->range_start;
6258 if (!finish)
6259 constructor_range_stack = range_stack;
6260 continue;
6263 break;
6266 constructor_range_stack = 0;
6269 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6270 (guaranteed to be 'volatile' or null) and ARGS (represented using
6271 an ASM_EXPR node). */
6272 tree
6273 build_asm_stmt (tree cv_qualifier, tree args)
6275 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6276 ASM_VOLATILE_P (args) = 1;
6277 return add_stmt (args);
6280 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6281 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6282 SIMPLE indicates whether there was anything at all after the
6283 string in the asm expression -- asm("blah") and asm("blah" : )
6284 are subtly different. We use a ASM_EXPR node to represent this. */
6285 tree
6286 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6287 bool simple)
6289 tree tail;
6290 tree args;
6291 int i;
6292 const char *constraint;
6293 const char **oconstraints;
6294 bool allows_mem, allows_reg, is_inout;
6295 int ninputs, noutputs;
6297 ninputs = list_length (inputs);
6298 noutputs = list_length (outputs);
6299 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6301 string = resolve_asm_operand_names (string, outputs, inputs);
6303 /* Remove output conversions that change the type but not the mode. */
6304 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6306 tree output = TREE_VALUE (tail);
6308 /* ??? Really, this should not be here. Users should be using a
6309 proper lvalue, dammit. But there's a long history of using casts
6310 in the output operands. In cases like longlong.h, this becomes a
6311 primitive form of typechecking -- if the cast can be removed, then
6312 the output operand had a type of the proper width; otherwise we'll
6313 get an error. Gross, but ... */
6314 STRIP_NOPS (output);
6316 if (!lvalue_or_else (output, lv_asm))
6317 output = error_mark_node;
6319 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6320 oconstraints[i] = constraint;
6322 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6323 &allows_mem, &allows_reg, &is_inout))
6325 /* If the operand is going to end up in memory,
6326 mark it addressable. */
6327 if (!allows_reg && !c_mark_addressable (output))
6328 output = error_mark_node;
6330 else
6331 output = error_mark_node;
6333 TREE_VALUE (tail) = output;
6336 /* Perform default conversions on array and function inputs.
6337 Don't do this for other types as it would screw up operands
6338 expected to be in memory. */
6339 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6341 tree input;
6343 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6344 input = TREE_VALUE (tail);
6346 input = default_function_array_conversion (input);
6348 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6349 oconstraints, &allows_mem, &allows_reg))
6351 /* If the operand is going to end up in memory,
6352 mark it addressable. */
6353 if (!allows_reg && allows_mem)
6355 /* Strip the nops as we allow this case. FIXME, this really
6356 should be rejected or made deprecated. */
6357 STRIP_NOPS (input);
6358 if (!c_mark_addressable (input))
6359 input = error_mark_node;
6362 else
6363 input = error_mark_node;
6365 TREE_VALUE (tail) = input;
6368 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6370 /* Simple asm statements are treated as volatile. */
6371 if (simple)
6373 ASM_VOLATILE_P (args) = 1;
6374 ASM_INPUT_P (args) = 1;
6377 return args;
6380 /* Generate a goto statement to LABEL. */
6382 tree
6383 c_finish_goto_label (tree label)
6385 tree decl = lookup_label (label);
6386 if (!decl)
6387 return NULL_TREE;
6389 TREE_USED (decl) = 1;
6390 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6393 /* Generate a computed goto statement to EXPR. */
6395 tree
6396 c_finish_goto_ptr (tree expr)
6398 if (pedantic)
6399 pedwarn ("ISO C forbids %<goto *expr;%>");
6400 expr = convert (ptr_type_node, expr);
6401 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6404 /* Generate a C `return' statement. RETVAL is the expression for what
6405 to return, or a null pointer for `return;' with no value. */
6407 tree
6408 c_finish_return (tree retval)
6410 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6412 if (TREE_THIS_VOLATILE (current_function_decl))
6413 warning ("function declared %<noreturn%> has a %<return%> statement");
6415 if (!retval)
6417 current_function_returns_null = 1;
6418 if ((warn_return_type || flag_isoc99)
6419 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6420 pedwarn_c99 ("%<return%> with no value, in "
6421 "function returning non-void");
6423 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6425 current_function_returns_null = 1;
6426 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6427 pedwarn ("%<return%> with a value, in function returning void");
6429 else
6431 tree t = convert_for_assignment (valtype, retval, ic_return,
6432 NULL_TREE, NULL_TREE, 0);
6433 tree res = DECL_RESULT (current_function_decl);
6434 tree inner;
6436 current_function_returns_value = 1;
6437 if (t == error_mark_node)
6438 return NULL_TREE;
6440 inner = t = convert (TREE_TYPE (res), t);
6442 /* Strip any conversions, additions, and subtractions, and see if
6443 we are returning the address of a local variable. Warn if so. */
6444 while (1)
6446 switch (TREE_CODE (inner))
6448 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6449 case PLUS_EXPR:
6450 inner = TREE_OPERAND (inner, 0);
6451 continue;
6453 case MINUS_EXPR:
6454 /* If the second operand of the MINUS_EXPR has a pointer
6455 type (or is converted from it), this may be valid, so
6456 don't give a warning. */
6458 tree op1 = TREE_OPERAND (inner, 1);
6460 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6461 && (TREE_CODE (op1) == NOP_EXPR
6462 || TREE_CODE (op1) == NON_LVALUE_EXPR
6463 || TREE_CODE (op1) == CONVERT_EXPR))
6464 op1 = TREE_OPERAND (op1, 0);
6466 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6467 break;
6469 inner = TREE_OPERAND (inner, 0);
6470 continue;
6473 case ADDR_EXPR:
6474 inner = TREE_OPERAND (inner, 0);
6476 while (REFERENCE_CLASS_P (inner)
6477 && TREE_CODE (inner) != INDIRECT_REF)
6478 inner = TREE_OPERAND (inner, 0);
6480 if (DECL_P (inner)
6481 && !DECL_EXTERNAL (inner)
6482 && !TREE_STATIC (inner)
6483 && DECL_CONTEXT (inner) == current_function_decl)
6484 warning ("function returns address of local variable");
6485 break;
6487 default:
6488 break;
6491 break;
6494 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6497 return add_stmt (build_stmt (RETURN_EXPR, retval));
6500 struct c_switch {
6501 /* The SWITCH_STMT being built. */
6502 tree switch_stmt;
6504 /* The original type of the testing expression, i.e. before the
6505 default conversion is applied. */
6506 tree orig_type;
6508 /* A splay-tree mapping the low element of a case range to the high
6509 element, or NULL_TREE if there is no high element. Used to
6510 determine whether or not a new case label duplicates an old case
6511 label. We need a tree, rather than simply a hash table, because
6512 of the GNU case range extension. */
6513 splay_tree cases;
6515 /* The next node on the stack. */
6516 struct c_switch *next;
6519 /* A stack of the currently active switch statements. The innermost
6520 switch statement is on the top of the stack. There is no need to
6521 mark the stack for garbage collection because it is only active
6522 during the processing of the body of a function, and we never
6523 collect at that point. */
6525 struct c_switch *c_switch_stack;
6527 /* Start a C switch statement, testing expression EXP. Return the new
6528 SWITCH_STMT. */
6530 tree
6531 c_start_case (tree exp)
6533 enum tree_code code;
6534 tree type, orig_type = error_mark_node;
6535 struct c_switch *cs;
6537 if (exp != error_mark_node)
6539 code = TREE_CODE (TREE_TYPE (exp));
6540 orig_type = TREE_TYPE (exp);
6542 if (!INTEGRAL_TYPE_P (orig_type)
6543 && code != ERROR_MARK)
6545 error ("switch quantity not an integer");
6546 exp = integer_zero_node;
6547 orig_type = error_mark_node;
6549 else
6551 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6553 if (warn_traditional && !in_system_header
6554 && (type == long_integer_type_node
6555 || type == long_unsigned_type_node))
6556 warning ("%<long%> switch expression not converted to "
6557 "%<int%> in ISO C");
6559 exp = default_conversion (exp);
6560 type = TREE_TYPE (exp);
6564 /* Add this new SWITCH_STMT to the stack. */
6565 cs = XNEW (struct c_switch);
6566 cs->switch_stmt = build_stmt ((enum tree_code) SWITCH_STMT, exp, NULL_TREE,
6567 orig_type);
6568 cs->orig_type = orig_type;
6569 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6570 cs->next = c_switch_stack;
6571 c_switch_stack = cs;
6573 return add_stmt (cs->switch_stmt);
6576 /* Process a case label. */
6578 tree
6579 do_case (tree low_value, tree high_value)
6581 tree label = NULL_TREE;
6583 if (c_switch_stack)
6585 label = c_add_case_label (c_switch_stack->cases,
6586 SWITCH_COND (c_switch_stack->switch_stmt),
6587 c_switch_stack->orig_type,
6588 low_value, high_value);
6589 if (label == error_mark_node)
6590 label = NULL_TREE;
6592 else if (low_value)
6593 error ("case label not within a switch statement");
6594 else
6595 error ("%<default%> label not within a switch statement");
6597 return label;
6600 /* Finish the switch statement. */
6602 void
6603 c_finish_case (tree body)
6605 struct c_switch *cs = c_switch_stack;
6607 SWITCH_BODY (cs->switch_stmt) = body;
6609 /* Emit warnings as needed. */
6610 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6612 /* Pop the stack. */
6613 c_switch_stack = cs->next;
6614 splay_tree_delete (cs->cases);
6615 XDELETE (cs);
6618 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6619 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6620 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6621 statement, and was not surrounded with parenthesis. */
6623 void
6624 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6625 tree else_block, bool nested_if)
6627 tree stmt;
6629 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6630 if (warn_parentheses && nested_if && else_block == NULL)
6632 tree inner_if = then_block;
6634 /* We know from the grammar productions that there is an IF nested
6635 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6636 it might not be exactly THEN_BLOCK, but should be the last
6637 non-container statement within. */
6638 while (1)
6639 switch (TREE_CODE (inner_if))
6641 case COND_EXPR:
6642 goto found;
6643 case BIND_EXPR:
6644 inner_if = BIND_EXPR_BODY (inner_if);
6645 break;
6646 case STATEMENT_LIST:
6647 inner_if = expr_last (then_block);
6648 break;
6649 case TRY_FINALLY_EXPR:
6650 case TRY_CATCH_EXPR:
6651 inner_if = TREE_OPERAND (inner_if, 0);
6652 break;
6653 default:
6654 gcc_unreachable ();
6656 found:
6658 if (COND_EXPR_ELSE (inner_if))
6659 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6660 &if_locus);
6663 /* Diagnose ";" via the special empty statement node that we create. */
6664 if (extra_warnings)
6666 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6668 if (!else_block)
6669 warning ("%Hempty body in an if-statement",
6670 EXPR_LOCUS (then_block));
6671 then_block = alloc_stmt_list ();
6673 if (else_block
6674 && TREE_CODE (else_block) == NOP_EXPR
6675 && !TREE_TYPE (else_block))
6677 warning ("%Hempty body in an else-statement",
6678 EXPR_LOCUS (else_block));
6679 else_block = alloc_stmt_list ();
6683 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6684 SET_EXPR_LOCATION (stmt, if_locus);
6685 add_stmt (stmt);
6688 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6689 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6690 is false for DO loops. INCR is the FOR increment expression. BODY is
6691 the statement controlled by the loop. BLAB is the break label. CLAB is
6692 the continue label. Everything is allowed to be NULL. */
6694 void
6695 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6696 tree blab, tree clab, bool cond_is_first)
6698 tree entry = NULL, exit = NULL, t;
6700 /* If the condition is zero don't generate a loop construct. */
6701 if (cond && integer_zerop (cond))
6703 if (cond_is_first)
6705 t = build_and_jump (&blab);
6706 SET_EXPR_LOCATION (t, start_locus);
6707 add_stmt (t);
6710 else
6712 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6714 /* If we have an exit condition, then we build an IF with gotos either
6715 out of the loop, or to the top of it. If there's no exit condition,
6716 then we just build a jump back to the top. */
6717 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6719 if (cond && !integer_nonzerop (cond))
6721 /* Canonicalize the loop condition to the end. This means
6722 generating a branch to the loop condition. Reuse the
6723 continue label, if possible. */
6724 if (cond_is_first)
6726 if (incr || !clab)
6728 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6729 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6731 else
6732 t = build1 (GOTO_EXPR, void_type_node, clab);
6733 SET_EXPR_LOCATION (t, start_locus);
6734 add_stmt (t);
6737 t = build_and_jump (&blab);
6738 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6739 exit = fold (exit);
6740 if (cond_is_first)
6741 SET_EXPR_LOCATION (exit, start_locus);
6742 else
6743 SET_EXPR_LOCATION (exit, input_location);
6746 add_stmt (top);
6749 if (body)
6750 add_stmt (body);
6751 if (clab)
6752 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6753 if (incr)
6754 add_stmt (incr);
6755 if (entry)
6756 add_stmt (entry);
6757 if (exit)
6758 add_stmt (exit);
6759 if (blab)
6760 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6763 tree
6764 c_finish_bc_stmt (tree *label_p, bool is_break)
6766 bool skip;
6767 tree label = *label_p;
6769 /* In switch statements break is sometimes stylistically used after
6770 a return statement. This can lead to spurious warnings about
6771 control reaching the end of a non-void function when it is
6772 inlined. Note that we are calling block_may_fallthru with
6773 language specific tree nodes; this works because
6774 block_may_fallthru returns true when given something it does not
6775 understand. */
6776 skip = !block_may_fallthru (cur_stmt_list);
6778 if (!label)
6780 if (!skip)
6781 *label_p = label = create_artificial_label ();
6783 else if (TREE_CODE (label) != LABEL_DECL)
6785 if (is_break)
6786 error ("break statement not within loop or switch");
6787 else
6788 error ("continue statement not within a loop");
6789 return NULL_TREE;
6792 if (skip)
6793 return NULL_TREE;
6795 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6798 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6800 static void
6801 emit_side_effect_warnings (tree expr)
6803 if (expr == error_mark_node)
6805 else if (!TREE_SIDE_EFFECTS (expr))
6807 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6808 warning ("%Hstatement with no effect",
6809 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6811 else if (warn_unused_value)
6812 warn_if_unused_value (expr, input_location);
6815 /* Process an expression as if it were a complete statement. Emit
6816 diagnostics, but do not call ADD_STMT. */
6818 tree
6819 c_process_expr_stmt (tree expr)
6821 if (!expr)
6822 return NULL_TREE;
6824 /* Do default conversion if safe and possibly important,
6825 in case within ({...}). */
6826 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6827 && (flag_isoc99 || lvalue_p (expr)))
6828 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6829 expr = default_conversion (expr);
6831 if (warn_sequence_point)
6832 verify_sequence_points (expr);
6834 if (TREE_TYPE (expr) != error_mark_node
6835 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6836 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6837 error ("expression statement has incomplete type");
6839 /* If we're not processing a statement expression, warn about unused values.
6840 Warnings for statement expressions will be emitted later, once we figure
6841 out which is the result. */
6842 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6843 && (extra_warnings || warn_unused_value))
6844 emit_side_effect_warnings (expr);
6846 /* If the expression is not of a type to which we cannot assign a line
6847 number, wrap the thing in a no-op NOP_EXPR. */
6848 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6849 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6851 if (EXPR_P (expr))
6852 SET_EXPR_LOCATION (expr, input_location);
6854 return expr;
6857 /* Emit an expression as a statement. */
6859 tree
6860 c_finish_expr_stmt (tree expr)
6862 if (expr)
6863 return add_stmt (c_process_expr_stmt (expr));
6864 else
6865 return NULL;
6868 /* Do the opposite and emit a statement as an expression. To begin,
6869 create a new binding level and return it. */
6871 tree
6872 c_begin_stmt_expr (void)
6874 tree ret;
6876 /* We must force a BLOCK for this level so that, if it is not expanded
6877 later, there is a way to turn off the entire subtree of blocks that
6878 are contained in it. */
6879 keep_next_level ();
6880 ret = c_begin_compound_stmt (true);
6882 /* Mark the current statement list as belonging to a statement list. */
6883 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6885 return ret;
6888 tree
6889 c_finish_stmt_expr (tree body)
6891 tree last, type, tmp, val;
6892 tree *last_p;
6894 body = c_end_compound_stmt (body, true);
6896 /* Locate the last statement in BODY. See c_end_compound_stmt
6897 about always returning a BIND_EXPR. */
6898 last_p = &BIND_EXPR_BODY (body);
6899 last = BIND_EXPR_BODY (body);
6901 continue_searching:
6902 if (TREE_CODE (last) == STATEMENT_LIST)
6904 tree_stmt_iterator i;
6906 /* This can happen with degenerate cases like ({ }). No value. */
6907 if (!TREE_SIDE_EFFECTS (last))
6908 return body;
6910 /* If we're supposed to generate side effects warnings, process
6911 all of the statements except the last. */
6912 if (extra_warnings || warn_unused_value)
6914 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6915 emit_side_effect_warnings (tsi_stmt (i));
6917 else
6918 i = tsi_last (last);
6919 last_p = tsi_stmt_ptr (i);
6920 last = *last_p;
6923 /* If the end of the list is exception related, then the list was split
6924 by a call to push_cleanup. Continue searching. */
6925 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6926 || TREE_CODE (last) == TRY_CATCH_EXPR)
6928 last_p = &TREE_OPERAND (last, 0);
6929 last = *last_p;
6930 goto continue_searching;
6933 /* In the case that the BIND_EXPR is not necessary, return the
6934 expression out from inside it. */
6935 if (last == error_mark_node
6936 || (last == BIND_EXPR_BODY (body)
6937 && BIND_EXPR_VARS (body) == NULL))
6938 return last;
6940 /* Extract the type of said expression. */
6941 type = TREE_TYPE (last);
6943 /* If we're not returning a value at all, then the BIND_EXPR that
6944 we already have is a fine expression to return. */
6945 if (!type || VOID_TYPE_P (type))
6946 return body;
6948 /* Now that we've located the expression containing the value, it seems
6949 silly to make voidify_wrapper_expr repeat the process. Create a
6950 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6951 tmp = create_tmp_var_raw (type, NULL);
6953 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6954 tree_expr_nonnegative_p giving up immediately. */
6955 val = last;
6956 if (TREE_CODE (val) == NOP_EXPR
6957 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
6958 val = TREE_OPERAND (val, 0);
6960 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
6961 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
6963 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
6966 /* Begin and end compound statements. This is as simple as pushing
6967 and popping new statement lists from the tree. */
6969 tree
6970 c_begin_compound_stmt (bool do_scope)
6972 tree stmt = push_stmt_list ();
6973 if (do_scope)
6974 push_scope ();
6975 return stmt;
6978 tree
6979 c_end_compound_stmt (tree stmt, bool do_scope)
6981 tree block = NULL;
6983 if (do_scope)
6985 if (c_dialect_objc ())
6986 objc_clear_super_receiver ();
6987 block = pop_scope ();
6990 stmt = pop_stmt_list (stmt);
6991 stmt = c_build_bind_expr (block, stmt);
6993 /* If this compound statement is nested immediately inside a statement
6994 expression, then force a BIND_EXPR to be created. Otherwise we'll
6995 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6996 STATEMENT_LISTs merge, and thus we can lose track of what statement
6997 was really last. */
6998 if (cur_stmt_list
6999 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7000 && TREE_CODE (stmt) != BIND_EXPR)
7002 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7003 TREE_SIDE_EFFECTS (stmt) = 1;
7006 return stmt;
7009 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7010 when the current scope is exited. EH_ONLY is true when this is not
7011 meant to apply to normal control flow transfer. */
7013 void
7014 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7016 enum tree_code code;
7017 tree stmt, list;
7018 bool stmt_expr;
7020 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7021 stmt = build_stmt (code, NULL, cleanup);
7022 add_stmt (stmt);
7023 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7024 list = push_stmt_list ();
7025 TREE_OPERAND (stmt, 0) = list;
7026 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7029 /* Build a binary-operation expression without default conversions.
7030 CODE is the kind of expression to build.
7031 This function differs from `build' in several ways:
7032 the data type of the result is computed and recorded in it,
7033 warnings are generated if arg data types are invalid,
7034 special handling for addition and subtraction of pointers is known,
7035 and some optimization is done (operations on narrow ints
7036 are done in the narrower type when that gives the same result).
7037 Constant folding is also done before the result is returned.
7039 Note that the operands will never have enumeral types, or function
7040 or array types, because either they will have the default conversions
7041 performed or they have both just been converted to some other type in which
7042 the arithmetic is to be done. */
7044 tree
7045 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7046 int convert_p)
7048 tree type0, type1;
7049 enum tree_code code0, code1;
7050 tree op0, op1;
7052 /* Expression code to give to the expression when it is built.
7053 Normally this is CODE, which is what the caller asked for,
7054 but in some special cases we change it. */
7055 enum tree_code resultcode = code;
7057 /* Data type in which the computation is to be performed.
7058 In the simplest cases this is the common type of the arguments. */
7059 tree result_type = NULL;
7061 /* Nonzero means operands have already been type-converted
7062 in whatever way is necessary.
7063 Zero means they need to be converted to RESULT_TYPE. */
7064 int converted = 0;
7066 /* Nonzero means create the expression with this type, rather than
7067 RESULT_TYPE. */
7068 tree build_type = 0;
7070 /* Nonzero means after finally constructing the expression
7071 convert it to this type. */
7072 tree final_type = 0;
7074 /* Nonzero if this is an operation like MIN or MAX which can
7075 safely be computed in short if both args are promoted shorts.
7076 Also implies COMMON.
7077 -1 indicates a bitwise operation; this makes a difference
7078 in the exact conditions for when it is safe to do the operation
7079 in a narrower mode. */
7080 int shorten = 0;
7082 /* Nonzero if this is a comparison operation;
7083 if both args are promoted shorts, compare the original shorts.
7084 Also implies COMMON. */
7085 int short_compare = 0;
7087 /* Nonzero if this is a right-shift operation, which can be computed on the
7088 original short and then promoted if the operand is a promoted short. */
7089 int short_shift = 0;
7091 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7092 int common = 0;
7094 if (convert_p)
7096 op0 = default_conversion (orig_op0);
7097 op1 = default_conversion (orig_op1);
7099 else
7101 op0 = orig_op0;
7102 op1 = orig_op1;
7105 type0 = TREE_TYPE (op0);
7106 type1 = TREE_TYPE (op1);
7108 /* The expression codes of the data types of the arguments tell us
7109 whether the arguments are integers, floating, pointers, etc. */
7110 code0 = TREE_CODE (type0);
7111 code1 = TREE_CODE (type1);
7113 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7114 STRIP_TYPE_NOPS (op0);
7115 STRIP_TYPE_NOPS (op1);
7117 /* If an error was already reported for one of the arguments,
7118 avoid reporting another error. */
7120 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7121 return error_mark_node;
7123 switch (code)
7125 case PLUS_EXPR:
7126 /* Handle the pointer + int case. */
7127 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7128 return pointer_int_sum (PLUS_EXPR, op0, op1);
7129 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7130 return pointer_int_sum (PLUS_EXPR, op1, op0);
7131 else
7132 common = 1;
7133 break;
7135 case MINUS_EXPR:
7136 /* Subtraction of two similar pointers.
7137 We must subtract them as integers, then divide by object size. */
7138 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7139 && comp_target_types (type0, type1, 1))
7140 return pointer_diff (op0, op1);
7141 /* Handle pointer minus int. Just like pointer plus int. */
7142 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7143 return pointer_int_sum (MINUS_EXPR, op0, op1);
7144 else
7145 common = 1;
7146 break;
7148 case MULT_EXPR:
7149 common = 1;
7150 break;
7152 case TRUNC_DIV_EXPR:
7153 case CEIL_DIV_EXPR:
7154 case FLOOR_DIV_EXPR:
7155 case ROUND_DIV_EXPR:
7156 case EXACT_DIV_EXPR:
7157 /* Floating point division by zero is a legitimate way to obtain
7158 infinities and NaNs. */
7159 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7160 warning ("division by zero");
7162 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7163 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7164 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7165 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7167 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7168 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7169 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7170 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7172 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7173 resultcode = RDIV_EXPR;
7174 else
7175 /* Although it would be tempting to shorten always here, that
7176 loses on some targets, since the modulo instruction is
7177 undefined if the quotient can't be represented in the
7178 computation mode. We shorten only if unsigned or if
7179 dividing by something we know != -1. */
7180 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7181 || (TREE_CODE (op1) == INTEGER_CST
7182 && !integer_all_onesp (op1)));
7183 common = 1;
7185 break;
7187 case BIT_AND_EXPR:
7188 case BIT_IOR_EXPR:
7189 case BIT_XOR_EXPR:
7190 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7191 shorten = -1;
7192 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7193 common = 1;
7194 break;
7196 case TRUNC_MOD_EXPR:
7197 case FLOOR_MOD_EXPR:
7198 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7199 warning ("division by zero");
7201 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7203 /* Although it would be tempting to shorten always here, that loses
7204 on some targets, since the modulo instruction is undefined if the
7205 quotient can't be represented in the computation mode. We shorten
7206 only if unsigned or if dividing by something we know != -1. */
7207 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7208 || (TREE_CODE (op1) == INTEGER_CST
7209 && !integer_all_onesp (op1)));
7210 common = 1;
7212 break;
7214 case TRUTH_ANDIF_EXPR:
7215 case TRUTH_ORIF_EXPR:
7216 case TRUTH_AND_EXPR:
7217 case TRUTH_OR_EXPR:
7218 case TRUTH_XOR_EXPR:
7219 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7220 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7221 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7222 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7224 /* Result of these operations is always an int,
7225 but that does not mean the operands should be
7226 converted to ints! */
7227 result_type = integer_type_node;
7228 op0 = lang_hooks.truthvalue_conversion (op0);
7229 op1 = lang_hooks.truthvalue_conversion (op1);
7230 converted = 1;
7232 break;
7234 /* Shift operations: result has same type as first operand;
7235 always convert second operand to int.
7236 Also set SHORT_SHIFT if shifting rightward. */
7238 case RSHIFT_EXPR:
7239 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7241 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7243 if (tree_int_cst_sgn (op1) < 0)
7244 warning ("right shift count is negative");
7245 else
7247 if (!integer_zerop (op1))
7248 short_shift = 1;
7250 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7251 warning ("right shift count >= width of type");
7255 /* Use the type of the value to be shifted. */
7256 result_type = type0;
7257 /* Convert the shift-count to an integer, regardless of size
7258 of value being shifted. */
7259 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7260 op1 = convert (integer_type_node, op1);
7261 /* Avoid converting op1 to result_type later. */
7262 converted = 1;
7264 break;
7266 case LSHIFT_EXPR:
7267 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7269 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7271 if (tree_int_cst_sgn (op1) < 0)
7272 warning ("left shift count is negative");
7274 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7275 warning ("left shift count >= width of type");
7278 /* Use the type of the value to be shifted. */
7279 result_type = type0;
7280 /* Convert the shift-count to an integer, regardless of size
7281 of value being shifted. */
7282 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7283 op1 = convert (integer_type_node, op1);
7284 /* Avoid converting op1 to result_type later. */
7285 converted = 1;
7287 break;
7289 case EQ_EXPR:
7290 case NE_EXPR:
7291 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7292 warning ("comparing floating point with == or != is unsafe");
7293 /* Result of comparison is always int,
7294 but don't convert the args to int! */
7295 build_type = integer_type_node;
7296 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7297 || code0 == COMPLEX_TYPE)
7298 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7299 || code1 == COMPLEX_TYPE))
7300 short_compare = 1;
7301 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7303 tree tt0 = TREE_TYPE (type0);
7304 tree tt1 = TREE_TYPE (type1);
7305 /* Anything compares with void *. void * compares with anything.
7306 Otherwise, the targets must be compatible
7307 and both must be object or both incomplete. */
7308 if (comp_target_types (type0, type1, 1))
7309 result_type = common_pointer_type (type0, type1);
7310 else if (VOID_TYPE_P (tt0))
7312 /* op0 != orig_op0 detects the case of something
7313 whose value is 0 but which isn't a valid null ptr const. */
7314 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7315 && TREE_CODE (tt1) == FUNCTION_TYPE)
7316 pedwarn ("ISO C forbids comparison of %<void *%>"
7317 " with function pointer");
7319 else if (VOID_TYPE_P (tt1))
7321 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7322 && TREE_CODE (tt0) == FUNCTION_TYPE)
7323 pedwarn ("ISO C forbids comparison of %<void *%>"
7324 " with function pointer");
7326 else
7327 pedwarn ("comparison of distinct pointer types lacks a cast");
7329 if (result_type == NULL_TREE)
7330 result_type = ptr_type_node;
7332 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7333 && integer_zerop (op1))
7334 result_type = type0;
7335 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7336 && integer_zerop (op0))
7337 result_type = type1;
7338 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7340 result_type = type0;
7341 pedwarn ("comparison between pointer and integer");
7343 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7345 result_type = type1;
7346 pedwarn ("comparison between pointer and integer");
7348 break;
7350 case LE_EXPR:
7351 case GE_EXPR:
7352 case LT_EXPR:
7353 case GT_EXPR:
7354 build_type = integer_type_node;
7355 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7356 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7357 short_compare = 1;
7358 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7360 if (comp_target_types (type0, type1, 1))
7362 result_type = common_pointer_type (type0, type1);
7363 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7364 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7365 pedwarn ("comparison of complete and incomplete pointers");
7366 else if (pedantic
7367 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7368 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7370 else
7372 result_type = ptr_type_node;
7373 pedwarn ("comparison of distinct pointer types lacks a cast");
7376 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7377 && integer_zerop (op1))
7379 result_type = type0;
7380 if (pedantic || extra_warnings)
7381 pedwarn ("ordered comparison of pointer with integer zero");
7383 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7384 && integer_zerop (op0))
7386 result_type = type1;
7387 if (pedantic)
7388 pedwarn ("ordered comparison of pointer with integer zero");
7390 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7392 result_type = type0;
7393 pedwarn ("comparison between pointer and integer");
7395 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7397 result_type = type1;
7398 pedwarn ("comparison between pointer and integer");
7400 break;
7402 default:
7403 gcc_unreachable ();
7406 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7407 return error_mark_node;
7409 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7410 || code0 == VECTOR_TYPE)
7412 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7413 || code1 == VECTOR_TYPE))
7415 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7417 if (shorten || common || short_compare)
7418 result_type = common_type (type0, type1);
7420 /* For certain operations (which identify themselves by shorten != 0)
7421 if both args were extended from the same smaller type,
7422 do the arithmetic in that type and then extend.
7424 shorten !=0 and !=1 indicates a bitwise operation.
7425 For them, this optimization is safe only if
7426 both args are zero-extended or both are sign-extended.
7427 Otherwise, we might change the result.
7428 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7429 but calculated in (unsigned short) it would be (unsigned short)-1. */
7431 if (shorten && none_complex)
7433 int unsigned0, unsigned1;
7434 tree arg0 = get_narrower (op0, &unsigned0);
7435 tree arg1 = get_narrower (op1, &unsigned1);
7436 /* UNS is 1 if the operation to be done is an unsigned one. */
7437 int uns = TYPE_UNSIGNED (result_type);
7438 tree type;
7440 final_type = result_type;
7442 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7443 but it *requires* conversion to FINAL_TYPE. */
7445 if ((TYPE_PRECISION (TREE_TYPE (op0))
7446 == TYPE_PRECISION (TREE_TYPE (arg0)))
7447 && TREE_TYPE (op0) != final_type)
7448 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7449 if ((TYPE_PRECISION (TREE_TYPE (op1))
7450 == TYPE_PRECISION (TREE_TYPE (arg1)))
7451 && TREE_TYPE (op1) != final_type)
7452 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7454 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7456 /* For bitwise operations, signedness of nominal type
7457 does not matter. Consider only how operands were extended. */
7458 if (shorten == -1)
7459 uns = unsigned0;
7461 /* Note that in all three cases below we refrain from optimizing
7462 an unsigned operation on sign-extended args.
7463 That would not be valid. */
7465 /* Both args variable: if both extended in same way
7466 from same width, do it in that width.
7467 Do it unsigned if args were zero-extended. */
7468 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7469 < TYPE_PRECISION (result_type))
7470 && (TYPE_PRECISION (TREE_TYPE (arg1))
7471 == TYPE_PRECISION (TREE_TYPE (arg0)))
7472 && unsigned0 == unsigned1
7473 && (unsigned0 || !uns))
7474 result_type
7475 = c_common_signed_or_unsigned_type
7476 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7477 else if (TREE_CODE (arg0) == INTEGER_CST
7478 && (unsigned1 || !uns)
7479 && (TYPE_PRECISION (TREE_TYPE (arg1))
7480 < TYPE_PRECISION (result_type))
7481 && (type
7482 = c_common_signed_or_unsigned_type (unsigned1,
7483 TREE_TYPE (arg1)),
7484 int_fits_type_p (arg0, type)))
7485 result_type = type;
7486 else if (TREE_CODE (arg1) == INTEGER_CST
7487 && (unsigned0 || !uns)
7488 && (TYPE_PRECISION (TREE_TYPE (arg0))
7489 < TYPE_PRECISION (result_type))
7490 && (type
7491 = c_common_signed_or_unsigned_type (unsigned0,
7492 TREE_TYPE (arg0)),
7493 int_fits_type_p (arg1, type)))
7494 result_type = type;
7497 /* Shifts can be shortened if shifting right. */
7499 if (short_shift)
7501 int unsigned_arg;
7502 tree arg0 = get_narrower (op0, &unsigned_arg);
7504 final_type = result_type;
7506 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7507 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7509 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7510 /* We can shorten only if the shift count is less than the
7511 number of bits in the smaller type size. */
7512 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7513 /* We cannot drop an unsigned shift after sign-extension. */
7514 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7516 /* Do an unsigned shift if the operand was zero-extended. */
7517 result_type
7518 = c_common_signed_or_unsigned_type (unsigned_arg,
7519 TREE_TYPE (arg0));
7520 /* Convert value-to-be-shifted to that type. */
7521 if (TREE_TYPE (op0) != result_type)
7522 op0 = convert (result_type, op0);
7523 converted = 1;
7527 /* Comparison operations are shortened too but differently.
7528 They identify themselves by setting short_compare = 1. */
7530 if (short_compare)
7532 /* Don't write &op0, etc., because that would prevent op0
7533 from being kept in a register.
7534 Instead, make copies of the our local variables and
7535 pass the copies by reference, then copy them back afterward. */
7536 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7537 enum tree_code xresultcode = resultcode;
7538 tree val
7539 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7541 if (val != 0)
7542 return val;
7544 op0 = xop0, op1 = xop1;
7545 converted = 1;
7546 resultcode = xresultcode;
7548 if (warn_sign_compare && skip_evaluation == 0)
7550 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7551 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7552 int unsignedp0, unsignedp1;
7553 tree primop0 = get_narrower (op0, &unsignedp0);
7554 tree primop1 = get_narrower (op1, &unsignedp1);
7556 xop0 = orig_op0;
7557 xop1 = orig_op1;
7558 STRIP_TYPE_NOPS (xop0);
7559 STRIP_TYPE_NOPS (xop1);
7561 /* Give warnings for comparisons between signed and unsigned
7562 quantities that may fail.
7564 Do the checking based on the original operand trees, so that
7565 casts will be considered, but default promotions won't be.
7567 Do not warn if the comparison is being done in a signed type,
7568 since the signed type will only be chosen if it can represent
7569 all the values of the unsigned type. */
7570 if (!TYPE_UNSIGNED (result_type))
7571 /* OK */;
7572 /* Do not warn if both operands are the same signedness. */
7573 else if (op0_signed == op1_signed)
7574 /* OK */;
7575 else
7577 tree sop, uop;
7579 if (op0_signed)
7580 sop = xop0, uop = xop1;
7581 else
7582 sop = xop1, uop = xop0;
7584 /* Do not warn if the signed quantity is an
7585 unsuffixed integer literal (or some static
7586 constant expression involving such literals or a
7587 conditional expression involving such literals)
7588 and it is non-negative. */
7589 if (tree_expr_nonnegative_p (sop))
7590 /* OK */;
7591 /* Do not warn if the comparison is an equality operation,
7592 the unsigned quantity is an integral constant, and it
7593 would fit in the result if the result were signed. */
7594 else if (TREE_CODE (uop) == INTEGER_CST
7595 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7596 && int_fits_type_p
7597 (uop, c_common_signed_type (result_type)))
7598 /* OK */;
7599 /* Do not warn if the unsigned quantity is an enumeration
7600 constant and its maximum value would fit in the result
7601 if the result were signed. */
7602 else if (TREE_CODE (uop) == INTEGER_CST
7603 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7604 && int_fits_type_p
7605 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7606 c_common_signed_type (result_type)))
7607 /* OK */;
7608 else
7609 warning ("comparison between signed and unsigned");
7612 /* Warn if two unsigned values are being compared in a size
7613 larger than their original size, and one (and only one) is the
7614 result of a `~' operator. This comparison will always fail.
7616 Also warn if one operand is a constant, and the constant
7617 does not have all bits set that are set in the ~ operand
7618 when it is extended. */
7620 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7621 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7623 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7624 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7625 &unsignedp0);
7626 else
7627 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7628 &unsignedp1);
7630 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7632 tree primop;
7633 HOST_WIDE_INT constant, mask;
7634 int unsignedp, bits;
7636 if (host_integerp (primop0, 0))
7638 primop = primop1;
7639 unsignedp = unsignedp1;
7640 constant = tree_low_cst (primop0, 0);
7642 else
7644 primop = primop0;
7645 unsignedp = unsignedp0;
7646 constant = tree_low_cst (primop1, 0);
7649 bits = TYPE_PRECISION (TREE_TYPE (primop));
7650 if (bits < TYPE_PRECISION (result_type)
7651 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7653 mask = (~(HOST_WIDE_INT) 0) << bits;
7654 if ((mask & constant) != mask)
7655 warning ("comparison of promoted ~unsigned with constant");
7658 else if (unsignedp0 && unsignedp1
7659 && (TYPE_PRECISION (TREE_TYPE (primop0))
7660 < TYPE_PRECISION (result_type))
7661 && (TYPE_PRECISION (TREE_TYPE (primop1))
7662 < TYPE_PRECISION (result_type)))
7663 warning ("comparison of promoted ~unsigned with unsigned");
7669 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7670 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7671 Then the expression will be built.
7672 It will be given type FINAL_TYPE if that is nonzero;
7673 otherwise, it will be given type RESULT_TYPE. */
7675 if (!result_type)
7677 binary_op_error (code);
7678 return error_mark_node;
7681 if (!converted)
7683 if (TREE_TYPE (op0) != result_type)
7684 op0 = convert (result_type, op0);
7685 if (TREE_TYPE (op1) != result_type)
7686 op1 = convert (result_type, op1);
7688 /* This can happen if one operand has a vector type, and the other
7689 has a different type. */
7690 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7691 return error_mark_node;
7694 if (build_type == NULL_TREE)
7695 build_type = result_type;
7698 tree result = build2 (resultcode, build_type, op0, op1);
7700 /* Treat expressions in initializers specially as they can't trap. */
7701 result = require_constant_value ? fold_initializer (result)
7702 : fold (result);
7704 if (final_type != 0)
7705 result = convert (final_type, result);
7706 return result;