config/
[official-gcc.git] / gcc / c-typeck.c
blob35678e04f311f92a967f6b52faaabc3e8ef9ac07
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
47 /* The level of nesting inside "__alignof__". */
48 int in_alignof;
50 /* The level of nesting inside "sizeof". */
51 int in_sizeof;
53 /* The level of nesting inside "typeof". */
54 int in_typeof;
56 /* Nonzero if we've already printed a "missing braces around initializer"
57 message within this initializer. */
58 static int missing_braces_mentioned;
60 static int require_constant_value;
61 static int require_constant_elements;
63 static tree qualify_type (tree, tree);
64 static int tagged_types_tu_compatible_p (tree, tree);
65 static int comp_target_types (tree, tree, int);
66 static int function_types_compatible_p (tree, tree);
67 static int type_lists_compatible_p (tree, tree);
68 static tree decl_constant_value_for_broken_optimization (tree);
69 static tree default_function_array_conversion (tree);
70 static tree lookup_field (tree, tree);
71 static tree convert_arguments (tree, tree, tree, tree);
72 static tree pointer_diff (tree, tree);
73 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
74 int);
75 static void warn_for_assignment (const char *, const char *, tree, int);
76 static tree valid_compound_expr_initializer (tree, tree);
77 static void push_string (const char *);
78 static void push_member_name (tree);
79 static void push_array_bounds (int);
80 static int spelling_length (void);
81 static char *print_spelling (char *);
82 static void warning_init (const char *);
83 static tree digest_init (tree, tree, bool, int);
84 static void output_init_element (tree, bool, tree, tree, int);
85 static void output_pending_init_elements (int);
86 static int set_designator (int);
87 static void push_range_stack (tree);
88 static void add_pending_init (tree, tree);
89 static void set_nonincremental_init (void);
90 static void set_nonincremental_init_from_string (tree);
91 static tree find_init_member (tree);
92 static int lvalue_or_else (tree, const char *);
94 /* Do `exp = require_complete_type (exp);' to make sure exp
95 does not have an incomplete type. (That includes void types.) */
97 tree
98 require_complete_type (tree value)
100 tree type = TREE_TYPE (value);
102 if (value == error_mark_node || type == error_mark_node)
103 return error_mark_node;
105 /* First, detect a valid value with a complete type. */
106 if (COMPLETE_TYPE_P (type))
107 return value;
109 c_incomplete_type_error (value, type);
110 return error_mark_node;
113 /* Print an error message for invalid use of an incomplete type.
114 VALUE is the expression that was used (or 0 if that isn't known)
115 and TYPE is the type that was invalid. */
117 void
118 c_incomplete_type_error (tree value, tree type)
120 const char *type_code_string;
122 /* Avoid duplicate error message. */
123 if (TREE_CODE (type) == ERROR_MARK)
124 return;
126 if (value != 0 && (TREE_CODE (value) == VAR_DECL
127 || TREE_CODE (value) == PARM_DECL))
128 error ("%qs has an incomplete type",
129 IDENTIFIER_POINTER (DECL_NAME (value)));
130 else
132 retry:
133 /* We must print an error message. Be clever about what it says. */
135 switch (TREE_CODE (type))
137 case RECORD_TYPE:
138 type_code_string = "struct";
139 break;
141 case UNION_TYPE:
142 type_code_string = "union";
143 break;
145 case ENUMERAL_TYPE:
146 type_code_string = "enum";
147 break;
149 case VOID_TYPE:
150 error ("invalid use of void expression");
151 return;
153 case ARRAY_TYPE:
154 if (TYPE_DOMAIN (type))
156 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
158 error ("invalid use of flexible array member");
159 return;
161 type = TREE_TYPE (type);
162 goto retry;
164 error ("invalid use of array with unspecified bounds");
165 return;
167 default:
168 gcc_unreachable ();
171 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
172 error ("invalid use of undefined type %<%s %s%>",
173 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
174 else
175 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
176 error ("invalid use of incomplete typedef %qs",
177 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
181 /* Given a type, apply default promotions wrt unnamed function
182 arguments and return the new type. */
184 tree
185 c_type_promotes_to (tree type)
187 if (TYPE_MAIN_VARIANT (type) == float_type_node)
188 return double_type_node;
190 if (c_promoting_integer_type_p (type))
192 /* Preserve unsignedness if not really getting any wider. */
193 if (TYPE_UNSIGNED (type)
194 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
195 return unsigned_type_node;
196 return integer_type_node;
199 return type;
202 /* Return a variant of TYPE which has all the type qualifiers of LIKE
203 as well as those of TYPE. */
205 static tree
206 qualify_type (tree type, tree like)
208 return c_build_qualified_type (type,
209 TYPE_QUALS (type) | TYPE_QUALS (like));
212 /* Return the composite type of two compatible types.
214 We assume that comptypes has already been done and returned
215 nonzero; if that isn't so, this may crash. In particular, we
216 assume that qualifiers match. */
218 tree
219 composite_type (tree t1, tree t2)
221 enum tree_code code1;
222 enum tree_code code2;
223 tree attributes;
225 /* Save time if the two types are the same. */
227 if (t1 == t2) return t1;
229 /* If one type is nonsense, use the other. */
230 if (t1 == error_mark_node)
231 return t2;
232 if (t2 == error_mark_node)
233 return t1;
235 code1 = TREE_CODE (t1);
236 code2 = TREE_CODE (t2);
238 /* Merge the attributes. */
239 attributes = targetm.merge_type_attributes (t1, t2);
241 /* If one is an enumerated type and the other is the compatible
242 integer type, the composite type might be either of the two
243 (DR#013 question 3). For consistency, use the enumerated type as
244 the composite type. */
246 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
247 return t1;
248 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
249 return t2;
251 gcc_assert (code1 == code2);
253 switch (code1)
255 case POINTER_TYPE:
256 /* For two pointers, do this recursively on the target type. */
258 tree pointed_to_1 = TREE_TYPE (t1);
259 tree pointed_to_2 = TREE_TYPE (t2);
260 tree target = composite_type (pointed_to_1, pointed_to_2);
261 t1 = build_pointer_type (target);
262 t1 = build_type_attribute_variant (t1, attributes);
263 return qualify_type (t1, t2);
266 case ARRAY_TYPE:
268 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
270 /* We should not have any type quals on arrays at all. */
271 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
273 /* Save space: see if the result is identical to one of the args. */
274 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
275 return build_type_attribute_variant (t1, attributes);
276 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
277 return build_type_attribute_variant (t2, attributes);
279 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
280 return build_type_attribute_variant (t1, attributes);
281 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
282 return build_type_attribute_variant (t2, attributes);
284 /* Merge the element types, and have a size if either arg has one. */
285 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
286 return build_type_attribute_variant (t1, attributes);
289 case FUNCTION_TYPE:
290 /* Function types: prefer the one that specified arg types.
291 If both do, merge the arg types. Also merge the return types. */
293 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
294 tree p1 = TYPE_ARG_TYPES (t1);
295 tree p2 = TYPE_ARG_TYPES (t2);
296 int len;
297 tree newargs, n;
298 int i;
300 /* Save space: see if the result is identical to one of the args. */
301 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
302 return build_type_attribute_variant (t1, attributes);
303 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
304 return build_type_attribute_variant (t2, attributes);
306 /* Simple way if one arg fails to specify argument types. */
307 if (TYPE_ARG_TYPES (t1) == 0)
309 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
310 t1 = build_type_attribute_variant (t1, attributes);
311 return qualify_type (t1, t2);
313 if (TYPE_ARG_TYPES (t2) == 0)
315 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
316 t1 = build_type_attribute_variant (t1, attributes);
317 return qualify_type (t1, t2);
320 /* If both args specify argument types, we must merge the two
321 lists, argument by argument. */
322 /* Tell global_bindings_p to return false so that variable_size
323 doesn't abort on VLAs in parameter types. */
324 c_override_global_bindings_to_false = true;
326 len = list_length (p1);
327 newargs = 0;
329 for (i = 0; i < len; i++)
330 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
332 n = newargs;
334 for (; p1;
335 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
337 /* A null type means arg type is not specified.
338 Take whatever the other function type has. */
339 if (TREE_VALUE (p1) == 0)
341 TREE_VALUE (n) = TREE_VALUE (p2);
342 goto parm_done;
344 if (TREE_VALUE (p2) == 0)
346 TREE_VALUE (n) = TREE_VALUE (p1);
347 goto parm_done;
350 /* Given wait (union {union wait *u; int *i} *)
351 and wait (union wait *),
352 prefer union wait * as type of parm. */
353 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
354 && TREE_VALUE (p1) != TREE_VALUE (p2))
356 tree memb;
357 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
358 memb; memb = TREE_CHAIN (memb))
359 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
361 TREE_VALUE (n) = TREE_VALUE (p2);
362 if (pedantic)
363 pedwarn ("function types not truly compatible in ISO C");
364 goto parm_done;
367 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
368 && TREE_VALUE (p2) != TREE_VALUE (p1))
370 tree memb;
371 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
372 memb; memb = TREE_CHAIN (memb))
373 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
375 TREE_VALUE (n) = TREE_VALUE (p1);
376 if (pedantic)
377 pedwarn ("function types not truly compatible in ISO C");
378 goto parm_done;
381 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
382 parm_done: ;
385 c_override_global_bindings_to_false = false;
386 t1 = build_function_type (valtype, newargs);
387 t1 = qualify_type (t1, t2);
388 /* ... falls through ... */
391 default:
392 return build_type_attribute_variant (t1, attributes);
397 /* Return the type of a conditional expression between pointers to
398 possibly differently qualified versions of compatible types.
400 We assume that comp_target_types has already been done and returned
401 nonzero; if that isn't so, this may crash. */
403 static tree
404 common_pointer_type (tree t1, tree t2)
406 tree attributes;
407 tree pointed_to_1;
408 tree pointed_to_2;
409 tree target;
411 /* Save time if the two types are the same. */
413 if (t1 == t2) return t1;
415 /* If one type is nonsense, use the other. */
416 if (t1 == error_mark_node)
417 return t2;
418 if (t2 == error_mark_node)
419 return t1;
421 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
422 && TREE_CODE (t2) == POINTER_TYPE);
424 /* Merge the attributes. */
425 attributes = targetm.merge_type_attributes (t1, t2);
427 /* Find the composite type of the target types, and combine the
428 qualifiers of the two types' targets. */
429 pointed_to_1 = TREE_TYPE (t1);
430 pointed_to_2 = TREE_TYPE (t2);
431 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
432 TYPE_MAIN_VARIANT (pointed_to_2));
433 t1 = build_pointer_type (c_build_qualified_type
434 (target,
435 TYPE_QUALS (pointed_to_1) |
436 TYPE_QUALS (pointed_to_2)));
437 return build_type_attribute_variant (t1, attributes);
440 /* Return the common type for two arithmetic types under the usual
441 arithmetic conversions. The default conversions have already been
442 applied, and enumerated types converted to their compatible integer
443 types. The resulting type is unqualified and has no attributes.
445 This is the type for the result of most arithmetic operations
446 if the operands have the given two types. */
448 tree
449 common_type (tree t1, tree t2)
451 enum tree_code code1;
452 enum tree_code code2;
454 /* If one type is nonsense, use the other. */
455 if (t1 == error_mark_node)
456 return t2;
457 if (t2 == error_mark_node)
458 return t1;
460 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
461 t1 = TYPE_MAIN_VARIANT (t1);
463 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
464 t2 = TYPE_MAIN_VARIANT (t2);
466 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
467 t1 = build_type_attribute_variant (t1, NULL_TREE);
469 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
470 t2 = build_type_attribute_variant (t2, NULL_TREE);
472 /* Save time if the two types are the same. */
474 if (t1 == t2) return t1;
476 code1 = TREE_CODE (t1);
477 code2 = TREE_CODE (t2);
479 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
480 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
481 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
482 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
484 /* If one type is a vector type, return that type. (How the usual
485 arithmetic conversions apply to the vector types extension is not
486 precisely specified.) */
487 if (code1 == VECTOR_TYPE)
488 return t1;
490 if (code2 == VECTOR_TYPE)
491 return t2;
493 /* If one type is complex, form the common type of the non-complex
494 components, then make that complex. Use T1 or T2 if it is the
495 required type. */
496 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
498 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
499 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
500 tree subtype = common_type (subtype1, subtype2);
502 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
503 return t1;
504 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
505 return t2;
506 else
507 return build_complex_type (subtype);
510 /* If only one is real, use it as the result. */
512 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
513 return t1;
515 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
516 return t2;
518 /* Both real or both integers; use the one with greater precision. */
520 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
521 return t1;
522 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
523 return t2;
525 /* Same precision. Prefer long longs to longs to ints when the
526 same precision, following the C99 rules on integer type rank
527 (which are equivalent to the C90 rules for C90 types). */
529 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
530 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
531 return long_long_unsigned_type_node;
533 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
534 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
536 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
537 return long_long_unsigned_type_node;
538 else
539 return long_long_integer_type_node;
542 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
543 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
544 return long_unsigned_type_node;
546 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
547 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
549 /* But preserve unsignedness from the other type,
550 since long cannot hold all the values of an unsigned int. */
551 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
552 return long_unsigned_type_node;
553 else
554 return long_integer_type_node;
557 /* Likewise, prefer long double to double even if same size. */
558 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
559 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
560 return long_double_type_node;
562 /* Otherwise prefer the unsigned one. */
564 if (TYPE_UNSIGNED (t1))
565 return t1;
566 else
567 return t2;
570 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
571 or various other operations. Return 2 if they are compatible
572 but a warning may be needed if you use them together. */
575 comptypes (tree type1, tree type2)
577 tree t1 = type1;
578 tree t2 = type2;
579 int attrval, val;
581 /* Suppress errors caused by previously reported errors. */
583 if (t1 == t2 || !t1 || !t2
584 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
585 return 1;
587 /* If either type is the internal version of sizetype, return the
588 language version. */
589 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
590 && TYPE_ORIG_SIZE_TYPE (t1))
591 t1 = TYPE_ORIG_SIZE_TYPE (t1);
593 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
594 && TYPE_ORIG_SIZE_TYPE (t2))
595 t2 = TYPE_ORIG_SIZE_TYPE (t2);
598 /* Enumerated types are compatible with integer types, but this is
599 not transitive: two enumerated types in the same translation unit
600 are compatible with each other only if they are the same type. */
602 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
603 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
604 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
605 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
607 if (t1 == t2)
608 return 1;
610 /* Different classes of types can't be compatible. */
612 if (TREE_CODE (t1) != TREE_CODE (t2))
613 return 0;
615 /* Qualifiers must match. C99 6.7.3p9 */
617 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
618 return 0;
620 /* Allow for two different type nodes which have essentially the same
621 definition. Note that we already checked for equality of the type
622 qualifiers (just above). */
624 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
625 return 1;
627 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
628 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
629 return 0;
631 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
632 val = 0;
634 switch (TREE_CODE (t1))
636 case POINTER_TYPE:
637 /* We must give ObjC the first crack at comparing pointers, since
638 protocol qualifiers may be involved. */
639 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
640 break;
641 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
642 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
643 break;
645 case FUNCTION_TYPE:
646 val = function_types_compatible_p (t1, t2);
647 break;
649 case ARRAY_TYPE:
651 tree d1 = TYPE_DOMAIN (t1);
652 tree d2 = TYPE_DOMAIN (t2);
653 bool d1_variable, d2_variable;
654 bool d1_zero, d2_zero;
655 val = 1;
657 /* Target types must match incl. qualifiers. */
658 if (TREE_TYPE (t1) != TREE_TYPE (t2)
659 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
660 return 0;
662 /* Sizes must match unless one is missing or variable. */
663 if (d1 == 0 || d2 == 0 || d1 == d2)
664 break;
666 d1_zero = ! TYPE_MAX_VALUE (d1);
667 d2_zero = ! TYPE_MAX_VALUE (d2);
669 d1_variable = (! d1_zero
670 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
671 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
672 d2_variable = (! d2_zero
673 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
674 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
676 if (d1_variable || d2_variable)
677 break;
678 if (d1_zero && d2_zero)
679 break;
680 if (d1_zero || d2_zero
681 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
682 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
683 val = 0;
685 break;
688 case RECORD_TYPE:
689 /* We are dealing with two distinct structs. In assorted Objective-C
690 corner cases, however, these can still be deemed equivalent. */
691 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
692 val = 1;
694 case ENUMERAL_TYPE:
695 case UNION_TYPE:
696 if (val != 1 && !same_translation_unit_p (t1, t2))
697 val = tagged_types_tu_compatible_p (t1, t2);
698 break;
700 case VECTOR_TYPE:
701 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
702 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
703 break;
705 default:
706 break;
708 return attrval == 2 && val == 1 ? 2 : val;
711 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
712 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
713 to 1 or 0 depending if the check of the pointer types is meant to
714 be reflexive or not (typically, assignments are not reflexive,
715 while comparisons are reflexive).
718 static int
719 comp_target_types (tree ttl, tree ttr, int reflexive)
721 int val;
723 /* Give objc_comptypes a crack at letting these types through. */
724 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
725 return val;
727 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
728 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
730 if (val == 2 && pedantic)
731 pedwarn ("types are not quite compatible");
732 return val;
735 /* Subroutines of `comptypes'. */
737 /* Determine whether two trees derive from the same translation unit.
738 If the CONTEXT chain ends in a null, that tree's context is still
739 being parsed, so if two trees have context chains ending in null,
740 they're in the same translation unit. */
742 same_translation_unit_p (tree t1, tree t2)
744 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
745 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
747 case tcc_declaration:
748 t1 = DECL_CONTEXT (t1); break;
749 case tcc_type:
750 t1 = TYPE_CONTEXT (t1); break;
751 case tcc_exceptional:
752 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
753 default: gcc_unreachable ();
756 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
757 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
759 case tcc_declaration:
760 t2 = DECL_CONTEXT (t2); break;
761 case tcc_type:
762 t2 = TYPE_CONTEXT (t2); break;
763 case tcc_exceptional:
764 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
765 default: gcc_unreachable ();
768 return t1 == t2;
771 /* The C standard says that two structures in different translation
772 units are compatible with each other only if the types of their
773 fields are compatible (among other things). So, consider two copies
774 of this structure: */
776 struct tagged_tu_seen {
777 const struct tagged_tu_seen * next;
778 tree t1;
779 tree t2;
782 /* Can they be compatible with each other? We choose to break the
783 recursion by allowing those types to be compatible. */
785 static const struct tagged_tu_seen * tagged_tu_seen_base;
787 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
788 compatible. If the two types are not the same (which has been
789 checked earlier), this can only happen when multiple translation
790 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
791 rules. */
793 static int
794 tagged_types_tu_compatible_p (tree t1, tree t2)
796 tree s1, s2;
797 bool needs_warning = false;
799 /* We have to verify that the tags of the types are the same. This
800 is harder than it looks because this may be a typedef, so we have
801 to go look at the original type. It may even be a typedef of a
802 typedef...
803 In the case of compiler-created builtin structs the TYPE_DECL
804 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
805 while (TYPE_NAME (t1)
806 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
807 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
808 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
810 while (TYPE_NAME (t2)
811 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
812 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
813 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
815 /* C90 didn't have the requirement that the two tags be the same. */
816 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
817 return 0;
819 /* C90 didn't say what happened if one or both of the types were
820 incomplete; we choose to follow C99 rules here, which is that they
821 are compatible. */
822 if (TYPE_SIZE (t1) == NULL
823 || TYPE_SIZE (t2) == NULL)
824 return 1;
827 const struct tagged_tu_seen * tts_i;
828 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
829 if (tts_i->t1 == t1 && tts_i->t2 == t2)
830 return 1;
833 switch (TREE_CODE (t1))
835 case ENUMERAL_TYPE:
838 /* Speed up the case where the type values are in the same order. */
839 tree tv1 = TYPE_VALUES (t1);
840 tree tv2 = TYPE_VALUES (t2);
842 if (tv1 == tv2)
843 return 1;
845 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
847 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
848 break;
849 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
850 return 0;
853 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
854 return 1;
855 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
856 return 0;
858 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
859 return 0;
861 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
863 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
864 if (s2 == NULL
865 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
866 return 0;
868 return 1;
871 case UNION_TYPE:
873 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
874 return 0;
876 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
878 bool ok = false;
879 struct tagged_tu_seen tts;
881 tts.next = tagged_tu_seen_base;
882 tts.t1 = t1;
883 tts.t2 = t2;
884 tagged_tu_seen_base = &tts;
886 if (DECL_NAME (s1) != NULL)
887 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
888 if (DECL_NAME (s1) == DECL_NAME (s2))
890 int result;
891 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
892 if (result == 0)
893 break;
894 if (result == 2)
895 needs_warning = true;
897 if (TREE_CODE (s1) == FIELD_DECL
898 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
899 DECL_FIELD_BIT_OFFSET (s2)) != 1)
900 break;
902 ok = true;
903 break;
905 tagged_tu_seen_base = tts.next;
906 if (! ok)
907 return 0;
909 return needs_warning ? 2 : 1;
912 case RECORD_TYPE:
914 struct tagged_tu_seen tts;
916 tts.next = tagged_tu_seen_base;
917 tts.t1 = t1;
918 tts.t2 = t2;
919 tagged_tu_seen_base = &tts;
921 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
922 s1 && s2;
923 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
925 int result;
926 if (TREE_CODE (s1) != TREE_CODE (s2)
927 || DECL_NAME (s1) != DECL_NAME (s2))
928 break;
929 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
930 if (result == 0)
931 break;
932 if (result == 2)
933 needs_warning = true;
935 if (TREE_CODE (s1) == FIELD_DECL
936 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
937 DECL_FIELD_BIT_OFFSET (s2)) != 1)
938 break;
940 tagged_tu_seen_base = tts.next;
941 if (s1 && s2)
942 return 0;
943 return needs_warning ? 2 : 1;
946 default:
947 gcc_unreachable ();
951 /* Return 1 if two function types F1 and F2 are compatible.
952 If either type specifies no argument types,
953 the other must specify a fixed number of self-promoting arg types.
954 Otherwise, if one type specifies only the number of arguments,
955 the other must specify that number of self-promoting arg types.
956 Otherwise, the argument types must match. */
958 static int
959 function_types_compatible_p (tree f1, tree f2)
961 tree args1, args2;
962 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
963 int val = 1;
964 int val1;
965 tree ret1, ret2;
967 ret1 = TREE_TYPE (f1);
968 ret2 = TREE_TYPE (f2);
970 /* 'volatile' qualifiers on a function's return type used to mean
971 the function is noreturn. */
972 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
973 pedwarn ("function return types not compatible due to %<volatile%>");
974 if (TYPE_VOLATILE (ret1))
975 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
976 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
977 if (TYPE_VOLATILE (ret2))
978 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
979 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
980 val = comptypes (ret1, ret2);
981 if (val == 0)
982 return 0;
984 args1 = TYPE_ARG_TYPES (f1);
985 args2 = TYPE_ARG_TYPES (f2);
987 /* An unspecified parmlist matches any specified parmlist
988 whose argument types don't need default promotions. */
990 if (args1 == 0)
992 if (!self_promoting_args_p (args2))
993 return 0;
994 /* If one of these types comes from a non-prototype fn definition,
995 compare that with the other type's arglist.
996 If they don't match, ask for a warning (but no error). */
997 if (TYPE_ACTUAL_ARG_TYPES (f1)
998 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
999 val = 2;
1000 return val;
1002 if (args2 == 0)
1004 if (!self_promoting_args_p (args1))
1005 return 0;
1006 if (TYPE_ACTUAL_ARG_TYPES (f2)
1007 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1008 val = 2;
1009 return val;
1012 /* Both types have argument lists: compare them and propagate results. */
1013 val1 = type_lists_compatible_p (args1, args2);
1014 return val1 != 1 ? val1 : val;
1017 /* Check two lists of types for compatibility,
1018 returning 0 for incompatible, 1 for compatible,
1019 or 2 for compatible with warning. */
1021 static int
1022 type_lists_compatible_p (tree args1, tree args2)
1024 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1025 int val = 1;
1026 int newval = 0;
1028 while (1)
1030 if (args1 == 0 && args2 == 0)
1031 return val;
1032 /* If one list is shorter than the other,
1033 they fail to match. */
1034 if (args1 == 0 || args2 == 0)
1035 return 0;
1036 /* A null pointer instead of a type
1037 means there is supposed to be an argument
1038 but nothing is specified about what type it has.
1039 So match anything that self-promotes. */
1040 if (TREE_VALUE (args1) == 0)
1042 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1043 return 0;
1045 else if (TREE_VALUE (args2) == 0)
1047 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1048 return 0;
1050 /* If one of the lists has an error marker, ignore this arg. */
1051 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1052 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1054 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1055 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1057 /* Allow wait (union {union wait *u; int *i} *)
1058 and wait (union wait *) to be compatible. */
1059 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1060 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1061 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1062 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1063 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1064 TYPE_SIZE (TREE_VALUE (args2))))
1066 tree memb;
1067 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1068 memb; memb = TREE_CHAIN (memb))
1069 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1070 break;
1071 if (memb == 0)
1072 return 0;
1074 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1075 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1076 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1077 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1078 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1079 TYPE_SIZE (TREE_VALUE (args1))))
1081 tree memb;
1082 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1083 memb; memb = TREE_CHAIN (memb))
1084 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1085 break;
1086 if (memb == 0)
1087 return 0;
1089 else
1090 return 0;
1093 /* comptypes said ok, but record if it said to warn. */
1094 if (newval > val)
1095 val = newval;
1097 args1 = TREE_CHAIN (args1);
1098 args2 = TREE_CHAIN (args2);
1102 /* Compute the size to increment a pointer by. */
1104 tree
1105 c_size_in_bytes (tree type)
1107 enum tree_code code = TREE_CODE (type);
1109 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1110 return size_one_node;
1112 if (!COMPLETE_OR_VOID_TYPE_P (type))
1114 error ("arithmetic on pointer to an incomplete type");
1115 return size_one_node;
1118 /* Convert in case a char is more than one unit. */
1119 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1120 size_int (TYPE_PRECISION (char_type_node)
1121 / BITS_PER_UNIT));
1124 /* Return either DECL or its known constant value (if it has one). */
1126 tree
1127 decl_constant_value (tree decl)
1129 if (/* Don't change a variable array bound or initial value to a constant
1130 in a place where a variable is invalid. Note that DECL_INITIAL
1131 isn't valid for a PARM_DECL. */
1132 current_function_decl != 0
1133 && TREE_CODE (decl) != PARM_DECL
1134 && ! TREE_THIS_VOLATILE (decl)
1135 && TREE_READONLY (decl)
1136 && DECL_INITIAL (decl) != 0
1137 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1138 /* This is invalid if initial value is not constant.
1139 If it has either a function call, a memory reference,
1140 or a variable, then re-evaluating it could give different results. */
1141 && TREE_CONSTANT (DECL_INITIAL (decl))
1142 /* Check for cases where this is sub-optimal, even though valid. */
1143 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1144 return DECL_INITIAL (decl);
1145 return decl;
1148 /* Return either DECL or its known constant value (if it has one), but
1149 return DECL if pedantic or DECL has mode BLKmode. This is for
1150 bug-compatibility with the old behavior of decl_constant_value
1151 (before GCC 3.0); every use of this function is a bug and it should
1152 be removed before GCC 3.1. It is not appropriate to use pedantic
1153 in a way that affects optimization, and BLKmode is probably not the
1154 right test for avoiding misoptimizations either. */
1156 static tree
1157 decl_constant_value_for_broken_optimization (tree decl)
1159 if (pedantic || DECL_MODE (decl) == BLKmode)
1160 return decl;
1161 else
1162 return decl_constant_value (decl);
1166 /* Perform the default conversion of arrays and functions to pointers.
1167 Return the result of converting EXP. For any other expression, just
1168 return EXP. */
1170 static tree
1171 default_function_array_conversion (tree exp)
1173 tree orig_exp;
1174 tree type = TREE_TYPE (exp);
1175 enum tree_code code = TREE_CODE (type);
1176 int not_lvalue = 0;
1178 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1179 an lvalue.
1181 Do not use STRIP_NOPS here! It will remove conversions from pointer
1182 to integer and cause infinite recursion. */
1183 orig_exp = exp;
1184 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1185 || (TREE_CODE (exp) == NOP_EXPR
1186 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1188 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1189 not_lvalue = 1;
1190 exp = TREE_OPERAND (exp, 0);
1193 if (TREE_NO_WARNING (orig_exp))
1194 TREE_NO_WARNING (exp) = 1;
1196 if (code == FUNCTION_TYPE)
1198 return build_unary_op (ADDR_EXPR, exp, 0);
1200 if (code == ARRAY_TYPE)
1202 tree adr;
1203 tree restype = TREE_TYPE (type);
1204 tree ptrtype;
1205 int constp = 0;
1206 int volatilep = 0;
1207 int lvalue_array_p;
1209 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1211 constp = TREE_READONLY (exp);
1212 volatilep = TREE_THIS_VOLATILE (exp);
1215 if (TYPE_QUALS (type) || constp || volatilep)
1216 restype
1217 = c_build_qualified_type (restype,
1218 TYPE_QUALS (type)
1219 | (constp * TYPE_QUAL_CONST)
1220 | (volatilep * TYPE_QUAL_VOLATILE));
1222 if (TREE_CODE (exp) == INDIRECT_REF)
1223 return convert (build_pointer_type (restype),
1224 TREE_OPERAND (exp, 0));
1226 if (TREE_CODE (exp) == COMPOUND_EXPR)
1228 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1229 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1230 TREE_OPERAND (exp, 0), op1);
1233 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1234 if (!flag_isoc99 && !lvalue_array_p)
1236 /* Before C99, non-lvalue arrays do not decay to pointers.
1237 Normally, using such an array would be invalid; but it can
1238 be used correctly inside sizeof or as a statement expression.
1239 Thus, do not give an error here; an error will result later. */
1240 return exp;
1243 ptrtype = build_pointer_type (restype);
1245 if (TREE_CODE (exp) == VAR_DECL)
1247 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1248 ADDR_EXPR because it's the best way of representing what
1249 happens in C when we take the address of an array and place
1250 it in a pointer to the element type. */
1251 adr = build1 (ADDR_EXPR, ptrtype, exp);
1252 if (!c_mark_addressable (exp))
1253 return error_mark_node;
1254 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1255 return adr;
1257 /* This way is better for a COMPONENT_REF since it can
1258 simplify the offset for a component. */
1259 adr = build_unary_op (ADDR_EXPR, exp, 1);
1260 return convert (ptrtype, adr);
1262 return exp;
1265 /* Perform default promotions for C data used in expressions.
1266 Arrays and functions are converted to pointers;
1267 enumeral types or short or char, to int.
1268 In addition, manifest constants symbols are replaced by their values. */
1270 tree
1271 default_conversion (tree exp)
1273 tree orig_exp;
1274 tree type = TREE_TYPE (exp);
1275 enum tree_code code = TREE_CODE (type);
1277 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1278 return default_function_array_conversion (exp);
1280 /* Constants can be used directly unless they're not loadable. */
1281 if (TREE_CODE (exp) == CONST_DECL)
1282 exp = DECL_INITIAL (exp);
1284 /* Replace a nonvolatile const static variable with its value unless
1285 it is an array, in which case we must be sure that taking the
1286 address of the array produces consistent results. */
1287 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1289 exp = decl_constant_value_for_broken_optimization (exp);
1290 type = TREE_TYPE (exp);
1293 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1294 an lvalue.
1296 Do not use STRIP_NOPS here! It will remove conversions from pointer
1297 to integer and cause infinite recursion. */
1298 orig_exp = exp;
1299 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1300 || (TREE_CODE (exp) == NOP_EXPR
1301 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1302 exp = TREE_OPERAND (exp, 0);
1304 if (TREE_NO_WARNING (orig_exp))
1305 TREE_NO_WARNING (exp) = 1;
1307 /* Normally convert enums to int,
1308 but convert wide enums to something wider. */
1309 if (code == ENUMERAL_TYPE)
1311 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1312 TYPE_PRECISION (integer_type_node)),
1313 ((TYPE_PRECISION (type)
1314 >= TYPE_PRECISION (integer_type_node))
1315 && TYPE_UNSIGNED (type)));
1317 return convert (type, exp);
1320 if (TREE_CODE (exp) == COMPONENT_REF
1321 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1322 /* If it's thinner than an int, promote it like a
1323 c_promoting_integer_type_p, otherwise leave it alone. */
1324 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1325 TYPE_PRECISION (integer_type_node)))
1326 return convert (integer_type_node, exp);
1328 if (c_promoting_integer_type_p (type))
1330 /* Preserve unsignedness if not really getting any wider. */
1331 if (TYPE_UNSIGNED (type)
1332 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1333 return convert (unsigned_type_node, exp);
1335 return convert (integer_type_node, exp);
1338 if (code == VOID_TYPE)
1340 error ("void value not ignored as it ought to be");
1341 return error_mark_node;
1343 return exp;
1346 /* Look up COMPONENT in a structure or union DECL.
1348 If the component name is not found, returns NULL_TREE. Otherwise,
1349 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1350 stepping down the chain to the component, which is in the last
1351 TREE_VALUE of the list. Normally the list is of length one, but if
1352 the component is embedded within (nested) anonymous structures or
1353 unions, the list steps down the chain to the component. */
1355 static tree
1356 lookup_field (tree decl, tree component)
1358 tree type = TREE_TYPE (decl);
1359 tree field;
1361 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1362 to the field elements. Use a binary search on this array to quickly
1363 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1364 will always be set for structures which have many elements. */
1366 if (TYPE_LANG_SPECIFIC (type))
1368 int bot, top, half;
1369 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1371 field = TYPE_FIELDS (type);
1372 bot = 0;
1373 top = TYPE_LANG_SPECIFIC (type)->s->len;
1374 while (top - bot > 1)
1376 half = (top - bot + 1) >> 1;
1377 field = field_array[bot+half];
1379 if (DECL_NAME (field) == NULL_TREE)
1381 /* Step through all anon unions in linear fashion. */
1382 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1384 field = field_array[bot++];
1385 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1386 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1388 tree anon = lookup_field (field, component);
1390 if (anon)
1391 return tree_cons (NULL_TREE, field, anon);
1395 /* Entire record is only anon unions. */
1396 if (bot > top)
1397 return NULL_TREE;
1399 /* Restart the binary search, with new lower bound. */
1400 continue;
1403 if (DECL_NAME (field) == component)
1404 break;
1405 if (DECL_NAME (field) < component)
1406 bot += half;
1407 else
1408 top = bot + half;
1411 if (DECL_NAME (field_array[bot]) == component)
1412 field = field_array[bot];
1413 else if (DECL_NAME (field) != component)
1414 return NULL_TREE;
1416 else
1418 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1420 if (DECL_NAME (field) == NULL_TREE
1421 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1422 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1424 tree anon = lookup_field (field, component);
1426 if (anon)
1427 return tree_cons (NULL_TREE, field, anon);
1430 if (DECL_NAME (field) == component)
1431 break;
1434 if (field == NULL_TREE)
1435 return NULL_TREE;
1438 return tree_cons (NULL_TREE, field, NULL_TREE);
1441 /* Make an expression to refer to the COMPONENT field of
1442 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1444 tree
1445 build_component_ref (tree datum, tree component)
1447 tree type = TREE_TYPE (datum);
1448 enum tree_code code = TREE_CODE (type);
1449 tree field = NULL;
1450 tree ref;
1452 if (!objc_is_public (datum, component))
1453 return error_mark_node;
1455 /* See if there is a field or component with name COMPONENT. */
1457 if (code == RECORD_TYPE || code == UNION_TYPE)
1459 if (!COMPLETE_TYPE_P (type))
1461 c_incomplete_type_error (NULL_TREE, type);
1462 return error_mark_node;
1465 field = lookup_field (datum, component);
1467 if (!field)
1469 error ("%s has no member named %qs",
1470 code == RECORD_TYPE ? "structure" : "union",
1471 IDENTIFIER_POINTER (component));
1472 return error_mark_node;
1475 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1476 This might be better solved in future the way the C++ front
1477 end does it - by giving the anonymous entities each a
1478 separate name and type, and then have build_component_ref
1479 recursively call itself. We can't do that here. */
1482 tree subdatum = TREE_VALUE (field);
1484 if (TREE_TYPE (subdatum) == error_mark_node)
1485 return error_mark_node;
1487 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1488 NULL_TREE);
1489 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1490 TREE_READONLY (ref) = 1;
1491 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1492 TREE_THIS_VOLATILE (ref) = 1;
1494 if (TREE_DEPRECATED (subdatum))
1495 warn_deprecated_use (subdatum);
1497 datum = ref;
1499 field = TREE_CHAIN (field);
1501 while (field);
1503 return ref;
1505 else if (code != ERROR_MARK)
1506 error ("request for member %qs in something not a structure or union",
1507 IDENTIFIER_POINTER (component));
1509 return error_mark_node;
1512 /* Given an expression PTR for a pointer, return an expression
1513 for the value pointed to.
1514 ERRORSTRING is the name of the operator to appear in error messages. */
1516 tree
1517 build_indirect_ref (tree ptr, const char *errorstring)
1519 tree pointer = default_conversion (ptr);
1520 tree type = TREE_TYPE (pointer);
1522 if (TREE_CODE (type) == POINTER_TYPE)
1524 if (TREE_CODE (pointer) == ADDR_EXPR
1525 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1526 == TREE_TYPE (type)))
1527 return TREE_OPERAND (pointer, 0);
1528 else
1530 tree t = TREE_TYPE (type);
1531 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1533 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1535 error ("dereferencing pointer to incomplete type");
1536 return error_mark_node;
1538 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1539 warning ("dereferencing %<void *%> pointer");
1541 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1542 so that we get the proper error message if the result is used
1543 to assign to. Also, &* is supposed to be a no-op.
1544 And ANSI C seems to specify that the type of the result
1545 should be the const type. */
1546 /* A de-reference of a pointer to const is not a const. It is valid
1547 to change it via some other pointer. */
1548 TREE_READONLY (ref) = TYPE_READONLY (t);
1549 TREE_SIDE_EFFECTS (ref)
1550 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1551 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1552 return ref;
1555 else if (TREE_CODE (pointer) != ERROR_MARK)
1556 error ("invalid type argument of %qs", errorstring);
1557 return error_mark_node;
1560 /* This handles expressions of the form "a[i]", which denotes
1561 an array reference.
1563 This is logically equivalent in C to *(a+i), but we may do it differently.
1564 If A is a variable or a member, we generate a primitive ARRAY_REF.
1565 This avoids forcing the array out of registers, and can work on
1566 arrays that are not lvalues (for example, members of structures returned
1567 by functions). */
1569 tree
1570 build_array_ref (tree array, tree index)
1572 if (index == 0)
1574 error ("subscript missing in array reference");
1575 return error_mark_node;
1578 if (TREE_TYPE (array) == error_mark_node
1579 || TREE_TYPE (index) == error_mark_node)
1580 return error_mark_node;
1582 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1584 tree rval, type;
1586 /* Subscripting with type char is likely to lose
1587 on a machine where chars are signed.
1588 So warn on any machine, but optionally.
1589 Don't warn for unsigned char since that type is safe.
1590 Don't warn for signed char because anyone who uses that
1591 must have done so deliberately. */
1592 if (warn_char_subscripts
1593 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1594 warning ("array subscript has type %<char%>");
1596 /* Apply default promotions *after* noticing character types. */
1597 index = default_conversion (index);
1599 /* Require integer *after* promotion, for sake of enums. */
1600 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1602 error ("array subscript is not an integer");
1603 return error_mark_node;
1606 /* An array that is indexed by a non-constant
1607 cannot be stored in a register; we must be able to do
1608 address arithmetic on its address.
1609 Likewise an array of elements of variable size. */
1610 if (TREE_CODE (index) != INTEGER_CST
1611 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1612 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1614 if (!c_mark_addressable (array))
1615 return error_mark_node;
1617 /* An array that is indexed by a constant value which is not within
1618 the array bounds cannot be stored in a register either; because we
1619 would get a crash in store_bit_field/extract_bit_field when trying
1620 to access a non-existent part of the register. */
1621 if (TREE_CODE (index) == INTEGER_CST
1622 && TYPE_DOMAIN (TREE_TYPE (array))
1623 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1625 if (!c_mark_addressable (array))
1626 return error_mark_node;
1629 if (pedantic)
1631 tree foo = array;
1632 while (TREE_CODE (foo) == COMPONENT_REF)
1633 foo = TREE_OPERAND (foo, 0);
1634 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1635 pedwarn ("ISO C forbids subscripting %<register%> array");
1636 else if (! flag_isoc99 && ! lvalue_p (foo))
1637 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1640 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1641 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1642 /* Array ref is const/volatile if the array elements are
1643 or if the array is. */
1644 TREE_READONLY (rval)
1645 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1646 | TREE_READONLY (array));
1647 TREE_SIDE_EFFECTS (rval)
1648 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1649 | TREE_SIDE_EFFECTS (array));
1650 TREE_THIS_VOLATILE (rval)
1651 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1652 /* This was added by rms on 16 Nov 91.
1653 It fixes vol struct foo *a; a->elts[1]
1654 in an inline function.
1655 Hope it doesn't break something else. */
1656 | TREE_THIS_VOLATILE (array));
1657 return require_complete_type (fold (rval));
1661 tree ar = default_conversion (array);
1662 tree ind = default_conversion (index);
1664 /* Do the same warning check as above, but only on the part that's
1665 syntactically the index and only if it is also semantically
1666 the index. */
1667 if (warn_char_subscripts
1668 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1669 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1670 warning ("subscript has type %<char%>");
1672 /* Put the integer in IND to simplify error checking. */
1673 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1675 tree temp = ar;
1676 ar = ind;
1677 ind = temp;
1680 if (ar == error_mark_node)
1681 return ar;
1683 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1684 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1686 error ("subscripted value is neither array nor pointer");
1687 return error_mark_node;
1689 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1691 error ("array subscript is not an integer");
1692 return error_mark_node;
1695 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1696 "array indexing");
1700 /* Build an external reference to identifier ID. FUN indicates
1701 whether this will be used for a function call. */
1702 tree
1703 build_external_ref (tree id, int fun)
1705 tree ref;
1706 tree decl = lookup_name (id);
1707 tree objc_ivar = objc_lookup_ivar (id);
1709 if (decl && decl != error_mark_node)
1711 /* Properly declared variable or function reference. */
1712 if (!objc_ivar)
1713 ref = decl;
1714 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1716 warning ("local declaration of %qs hides instance variable",
1717 IDENTIFIER_POINTER (id));
1718 ref = decl;
1720 else
1721 ref = objc_ivar;
1723 else if (objc_ivar)
1724 ref = objc_ivar;
1725 else if (fun)
1726 /* Implicit function declaration. */
1727 ref = implicitly_declare (id);
1728 else if (decl == error_mark_node)
1729 /* Don't complain about something that's already been
1730 complained about. */
1731 return error_mark_node;
1732 else
1734 undeclared_variable (id);
1735 return error_mark_node;
1738 if (TREE_TYPE (ref) == error_mark_node)
1739 return error_mark_node;
1741 if (TREE_DEPRECATED (ref))
1742 warn_deprecated_use (ref);
1744 if (!skip_evaluation)
1745 assemble_external (ref);
1746 TREE_USED (ref) = 1;
1748 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1750 if (!in_sizeof && !in_typeof)
1751 C_DECL_USED (ref) = 1;
1752 else if (DECL_INITIAL (ref) == 0
1753 && DECL_EXTERNAL (ref)
1754 && !TREE_PUBLIC (ref))
1755 record_maybe_used_decl (ref);
1758 if (TREE_CODE (ref) == CONST_DECL)
1760 ref = DECL_INITIAL (ref);
1761 TREE_CONSTANT (ref) = 1;
1762 TREE_INVARIANT (ref) = 1;
1764 else if (current_function_decl != 0
1765 && !DECL_FILE_SCOPE_P (current_function_decl)
1766 && (TREE_CODE (ref) == VAR_DECL
1767 || TREE_CODE (ref) == PARM_DECL
1768 || TREE_CODE (ref) == FUNCTION_DECL))
1770 tree context = decl_function_context (ref);
1772 if (context != 0 && context != current_function_decl)
1773 DECL_NONLOCAL (ref) = 1;
1776 return ref;
1779 /* Record details of decls possibly used inside sizeof or typeof. */
1780 struct maybe_used_decl
1782 /* The decl. */
1783 tree decl;
1784 /* The level seen at (in_sizeof + in_typeof). */
1785 int level;
1786 /* The next one at this level or above, or NULL. */
1787 struct maybe_used_decl *next;
1790 static struct maybe_used_decl *maybe_used_decls;
1792 /* Record that DECL, an undefined static function reference seen
1793 inside sizeof or typeof, might be used if the operand of sizeof is
1794 a VLA type or the operand of typeof is a variably modified
1795 type. */
1797 void
1798 record_maybe_used_decl (tree decl)
1800 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1801 t->decl = decl;
1802 t->level = in_sizeof + in_typeof;
1803 t->next = maybe_used_decls;
1804 maybe_used_decls = t;
1807 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1808 USED is false, just discard them. If it is true, mark them used
1809 (if no longer inside sizeof or typeof) or move them to the next
1810 level up (if still inside sizeof or typeof). */
1812 void
1813 pop_maybe_used (bool used)
1815 struct maybe_used_decl *p = maybe_used_decls;
1816 int cur_level = in_sizeof + in_typeof;
1817 while (p && p->level > cur_level)
1819 if (used)
1821 if (cur_level == 0)
1822 C_DECL_USED (p->decl) = 1;
1823 else
1824 p->level = cur_level;
1826 p = p->next;
1828 if (!used || cur_level == 0)
1829 maybe_used_decls = p;
1832 /* Return the result of sizeof applied to EXPR. */
1834 struct c_expr
1835 c_expr_sizeof_expr (struct c_expr expr)
1837 struct c_expr ret;
1838 if (expr.value == error_mark_node)
1840 ret.value = error_mark_node;
1841 ret.original_code = ERROR_MARK;
1842 pop_maybe_used (false);
1844 else
1846 ret.value = c_sizeof (TREE_TYPE (expr.value));
1847 ret.original_code = ERROR_MARK;
1848 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1850 return ret;
1853 /* Return the result of sizeof applied to T, a structure for the type
1854 name passed to sizeof (rather than the type itself). */
1856 struct c_expr
1857 c_expr_sizeof_type (struct c_type_name *t)
1859 tree type;
1860 struct c_expr ret;
1861 type = groktypename (t);
1862 ret.value = c_sizeof (type);
1863 ret.original_code = ERROR_MARK;
1864 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
1865 return ret;
1868 /* Build a function call to function FUNCTION with parameters PARAMS.
1869 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1870 TREE_VALUE of each node is a parameter-expression.
1871 FUNCTION's data type may be a function type or a pointer-to-function. */
1873 tree
1874 build_function_call (tree function, tree params)
1876 tree fntype, fundecl = 0;
1877 tree coerced_params;
1878 tree name = NULL_TREE, result;
1879 tree tem;
1881 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1882 STRIP_TYPE_NOPS (function);
1884 /* Convert anything with function type to a pointer-to-function. */
1885 if (TREE_CODE (function) == FUNCTION_DECL)
1887 name = DECL_NAME (function);
1889 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1890 (because calling an inline function does not mean the function
1891 needs to be separately compiled). */
1892 fntype = build_type_variant (TREE_TYPE (function),
1893 TREE_READONLY (function),
1894 TREE_THIS_VOLATILE (function));
1895 fundecl = function;
1896 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1898 else
1899 function = default_conversion (function);
1901 fntype = TREE_TYPE (function);
1903 if (TREE_CODE (fntype) == ERROR_MARK)
1904 return error_mark_node;
1906 if (!(TREE_CODE (fntype) == POINTER_TYPE
1907 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1909 error ("called object %qE is not a function", function);
1910 return error_mark_node;
1913 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1914 current_function_returns_abnormally = 1;
1916 /* fntype now gets the type of function pointed to. */
1917 fntype = TREE_TYPE (fntype);
1919 /* Check that the function is called through a compatible prototype.
1920 If it is not, replace the call by a trap, wrapped up in a compound
1921 expression if necessary. This has the nice side-effect to prevent
1922 the tree-inliner from generating invalid assignment trees which may
1923 blow up in the RTL expander later.
1925 ??? This doesn't work for Objective-C because objc_comptypes
1926 refuses to compare function prototypes, yet the compiler appears
1927 to build calls that are flagged as invalid by C's comptypes. */
1928 if (! c_dialect_objc ()
1929 && TREE_CODE (function) == NOP_EXPR
1930 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1931 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1932 && ! comptypes (fntype, TREE_TYPE (tem)))
1934 tree return_type = TREE_TYPE (fntype);
1935 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1936 NULL_TREE);
1938 /* This situation leads to run-time undefined behavior. We can't,
1939 therefore, simply error unless we can prove that all possible
1940 executions of the program must execute the code. */
1941 warning ("function called through a non-compatible type");
1943 /* We can, however, treat "undefined" any way we please.
1944 Call abort to encourage the user to fix the program. */
1945 inform ("if this code is reached, the program will abort");
1947 if (VOID_TYPE_P (return_type))
1948 return trap;
1949 else
1951 tree rhs;
1953 if (AGGREGATE_TYPE_P (return_type))
1954 rhs = build_compound_literal (return_type,
1955 build_constructor (return_type,
1956 NULL_TREE));
1957 else
1958 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1960 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
1964 /* Convert the parameters to the types declared in the
1965 function prototype, or apply default promotions. */
1967 coerced_params
1968 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1970 /* Check that the arguments to the function are valid. */
1972 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1974 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
1975 function, coerced_params, NULL_TREE);
1976 TREE_SIDE_EFFECTS (result) = 1;
1978 if (require_constant_value)
1980 result = fold_initializer (result);
1982 if (TREE_CONSTANT (result)
1983 && (name == NULL_TREE
1984 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1985 pedwarn_init ("initializer element is not constant");
1987 else
1988 result = fold (result);
1990 if (VOID_TYPE_P (TREE_TYPE (result)))
1991 return result;
1992 return require_complete_type (result);
1995 /* Convert the argument expressions in the list VALUES
1996 to the types in the list TYPELIST. The result is a list of converted
1997 argument expressions.
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 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
2008 This is also where warnings about wrong number of args are generated.
2010 Both VALUES and the returned value are chains of TREE_LIST nodes
2011 with the elements of the list in the TREE_VALUE slots of those nodes. */
2013 static tree
2014 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
2016 tree typetail, valtail;
2017 tree result = NULL;
2018 int parmnum;
2020 /* Scan the given expressions and types, producing individual
2021 converted arguments and pushing them on RESULT in reverse order. */
2023 for (valtail = values, typetail = typelist, parmnum = 0;
2024 valtail;
2025 valtail = TREE_CHAIN (valtail), parmnum++)
2027 tree type = typetail ? TREE_VALUE (typetail) : 0;
2028 tree val = TREE_VALUE (valtail);
2030 if (type == void_type_node)
2032 if (name)
2033 error ("too many arguments to function %qs",
2034 IDENTIFIER_POINTER (name));
2035 else
2036 error ("too many arguments to function");
2037 break;
2040 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2041 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2042 to convert automatically to a pointer. */
2043 if (TREE_CODE (val) == NON_LVALUE_EXPR)
2044 val = TREE_OPERAND (val, 0);
2046 val = default_function_array_conversion (val);
2048 val = require_complete_type (val);
2050 if (type != 0)
2052 /* Formal parm type is specified by a function prototype. */
2053 tree parmval;
2055 if (!COMPLETE_TYPE_P (type))
2057 error ("type of formal parameter %d is incomplete", parmnum + 1);
2058 parmval = val;
2060 else
2062 /* Optionally warn about conversions that
2063 differ from the default conversions. */
2064 if (warn_conversion || warn_traditional)
2066 unsigned int formal_prec = TYPE_PRECISION (type);
2068 if (INTEGRAL_TYPE_P (type)
2069 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2070 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
2071 if (INTEGRAL_TYPE_P (type)
2072 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2073 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
2074 else if (TREE_CODE (type) == COMPLEX_TYPE
2075 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2076 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
2077 else if (TREE_CODE (type) == REAL_TYPE
2078 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2079 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
2080 else if (TREE_CODE (type) == COMPLEX_TYPE
2081 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2082 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
2083 else if (TREE_CODE (type) == REAL_TYPE
2084 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2085 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
2086 /* ??? At some point, messages should be written about
2087 conversions between complex types, but that's too messy
2088 to do now. */
2089 else if (TREE_CODE (type) == REAL_TYPE
2090 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2092 /* Warn if any argument is passed as `float',
2093 since without a prototype it would be `double'. */
2094 if (formal_prec == TYPE_PRECISION (float_type_node))
2095 warn_for_assignment ("%s as %<float%> rather than "
2096 "%<double%> due to prototype",
2097 (char *) 0, name, parmnum + 1);
2099 /* Detect integer changing in width or signedness.
2100 These warnings are only activated with
2101 -Wconversion, not with -Wtraditional. */
2102 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2103 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2105 tree would_have_been = default_conversion (val);
2106 tree type1 = TREE_TYPE (would_have_been);
2108 if (TREE_CODE (type) == ENUMERAL_TYPE
2109 && (TYPE_MAIN_VARIANT (type)
2110 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2111 /* No warning if function asks for enum
2112 and the actual arg is that enum type. */
2114 else if (formal_prec != TYPE_PRECISION (type1))
2115 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2116 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2118 /* Don't complain if the formal parameter type
2119 is an enum, because we can't tell now whether
2120 the value was an enum--even the same enum. */
2121 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2123 else if (TREE_CODE (val) == INTEGER_CST
2124 && int_fits_type_p (val, type))
2125 /* Change in signedness doesn't matter
2126 if a constant value is unaffected. */
2128 /* Likewise for a constant in a NOP_EXPR. */
2129 else if (TREE_CODE (val) == NOP_EXPR
2130 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2131 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2133 /* If the value is extended from a narrower
2134 unsigned type, it doesn't matter whether we
2135 pass it as signed or unsigned; the value
2136 certainly is the same either way. */
2137 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2138 && TYPE_UNSIGNED (TREE_TYPE (val)))
2140 else if (TYPE_UNSIGNED (type))
2141 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2142 else
2143 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2147 parmval = convert_for_assignment (type, val,
2148 (char *) 0, /* arg passing */
2149 fundecl, name, parmnum + 1);
2151 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2152 && INTEGRAL_TYPE_P (type)
2153 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2154 parmval = default_conversion (parmval);
2156 result = tree_cons (NULL_TREE, parmval, result);
2158 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2159 && (TYPE_PRECISION (TREE_TYPE (val))
2160 < TYPE_PRECISION (double_type_node)))
2161 /* Convert `float' to `double'. */
2162 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2163 else
2164 /* Convert `short' and `char' to full-size `int'. */
2165 result = tree_cons (NULL_TREE, default_conversion (val), result);
2167 if (typetail)
2168 typetail = TREE_CHAIN (typetail);
2171 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2173 if (name)
2174 error ("too few arguments to function %qs",
2175 IDENTIFIER_POINTER (name));
2176 else
2177 error ("too few arguments to function");
2180 return nreverse (result);
2183 /* This is the entry point used by the parser
2184 for binary operators in the input.
2185 In addition to constructing the expression,
2186 we check for operands that were written with other binary operators
2187 in a way that is likely to confuse the user. */
2189 struct c_expr
2190 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2191 struct c_expr arg2)
2193 struct c_expr result;
2195 enum tree_code code1 = arg1.original_code;
2196 enum tree_code code2 = arg2.original_code;
2198 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2199 result.original_code = code;
2201 if (TREE_CODE (result.value) == ERROR_MARK)
2202 return result;
2204 /* Check for cases such as x+y<<z which users are likely
2205 to misinterpret. */
2206 if (warn_parentheses)
2208 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2210 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2211 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2212 warning ("suggest parentheses around + or - inside shift");
2215 if (code == TRUTH_ORIF_EXPR)
2217 if (code1 == TRUTH_ANDIF_EXPR
2218 || code2 == TRUTH_ANDIF_EXPR)
2219 warning ("suggest parentheses around && within ||");
2222 if (code == BIT_IOR_EXPR)
2224 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2225 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2226 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2227 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2228 warning ("suggest parentheses around arithmetic in operand of |");
2229 /* Check cases like x|y==z */
2230 if (TREE_CODE_CLASS (code1) == tcc_comparison
2231 || TREE_CODE_CLASS (code2) == tcc_comparison)
2232 warning ("suggest parentheses around comparison in operand of |");
2235 if (code == BIT_XOR_EXPR)
2237 if (code1 == BIT_AND_EXPR
2238 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2239 || code2 == BIT_AND_EXPR
2240 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2241 warning ("suggest parentheses around arithmetic in operand of ^");
2242 /* Check cases like x^y==z */
2243 if (TREE_CODE_CLASS (code1) == tcc_comparison
2244 || TREE_CODE_CLASS (code2) == tcc_comparison)
2245 warning ("suggest parentheses around comparison in operand of ^");
2248 if (code == BIT_AND_EXPR)
2250 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2251 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2252 warning ("suggest parentheses around + or - in operand of &");
2253 /* Check cases like x&y==z */
2254 if (TREE_CODE_CLASS (code1) == tcc_comparison
2255 || TREE_CODE_CLASS (code2) == tcc_comparison)
2256 warning ("suggest parentheses around comparison in operand of &");
2258 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2259 if (TREE_CODE_CLASS (code) == tcc_comparison
2260 && (TREE_CODE_CLASS (code1) == tcc_comparison
2261 || TREE_CODE_CLASS (code2) == tcc_comparison))
2262 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2266 unsigned_conversion_warning (result.value, arg1.value);
2267 unsigned_conversion_warning (result.value, arg2.value);
2268 overflow_warning (result.value);
2270 return result;
2273 /* Return a tree for the difference of pointers OP0 and OP1.
2274 The resulting tree has type int. */
2276 static tree
2277 pointer_diff (tree op0, tree op1)
2279 tree restype = ptrdiff_type_node;
2281 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2282 tree con0, con1, lit0, lit1;
2283 tree orig_op1 = op1;
2285 if (pedantic || warn_pointer_arith)
2287 if (TREE_CODE (target_type) == VOID_TYPE)
2288 pedwarn ("pointer of type %<void *%> used in subtraction");
2289 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2290 pedwarn ("pointer to a function used in subtraction");
2293 /* If the conversion to ptrdiff_type does anything like widening or
2294 converting a partial to an integral mode, we get a convert_expression
2295 that is in the way to do any simplifications.
2296 (fold-const.c doesn't know that the extra bits won't be needed.
2297 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2298 different mode in place.)
2299 So first try to find a common term here 'by hand'; we want to cover
2300 at least the cases that occur in legal static initializers. */
2301 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2302 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2304 if (TREE_CODE (con0) == PLUS_EXPR)
2306 lit0 = TREE_OPERAND (con0, 1);
2307 con0 = TREE_OPERAND (con0, 0);
2309 else
2310 lit0 = integer_zero_node;
2312 if (TREE_CODE (con1) == PLUS_EXPR)
2314 lit1 = TREE_OPERAND (con1, 1);
2315 con1 = TREE_OPERAND (con1, 0);
2317 else
2318 lit1 = integer_zero_node;
2320 if (operand_equal_p (con0, con1, 0))
2322 op0 = lit0;
2323 op1 = lit1;
2327 /* First do the subtraction as integers;
2328 then drop through to build the divide operator.
2329 Do not do default conversions on the minus operator
2330 in case restype is a short type. */
2332 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2333 convert (restype, op1), 0);
2334 /* This generates an error if op1 is pointer to incomplete type. */
2335 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2336 error ("arithmetic on pointer to an incomplete type");
2338 /* This generates an error if op0 is pointer to incomplete type. */
2339 op1 = c_size_in_bytes (target_type);
2341 /* Divide by the size, in easiest possible way. */
2342 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2345 /* Construct and perhaps optimize a tree representation
2346 for a unary operation. CODE, a tree_code, specifies the operation
2347 and XARG is the operand.
2348 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2349 the default promotions (such as from short to int).
2350 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2351 allows non-lvalues; this is only used to handle conversion of non-lvalue
2352 arrays to pointers in C99. */
2354 tree
2355 build_unary_op (enum tree_code code, tree xarg, int flag)
2357 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2358 tree arg = xarg;
2359 tree argtype = 0;
2360 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2361 tree val;
2362 int noconvert = flag;
2364 if (typecode == ERROR_MARK)
2365 return error_mark_node;
2366 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2367 typecode = INTEGER_TYPE;
2369 switch (code)
2371 case CONVERT_EXPR:
2372 /* This is used for unary plus, because a CONVERT_EXPR
2373 is enough to prevent anybody from looking inside for
2374 associativity, but won't generate any code. */
2375 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2376 || typecode == COMPLEX_TYPE
2377 || typecode == VECTOR_TYPE))
2379 error ("wrong type argument to unary plus");
2380 return error_mark_node;
2382 else if (!noconvert)
2383 arg = default_conversion (arg);
2384 arg = non_lvalue (arg);
2385 break;
2387 case NEGATE_EXPR:
2388 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2389 || typecode == COMPLEX_TYPE
2390 || typecode == VECTOR_TYPE))
2392 error ("wrong type argument to unary minus");
2393 return error_mark_node;
2395 else if (!noconvert)
2396 arg = default_conversion (arg);
2397 break;
2399 case BIT_NOT_EXPR:
2400 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2402 if (!noconvert)
2403 arg = default_conversion (arg);
2405 else if (typecode == COMPLEX_TYPE)
2407 code = CONJ_EXPR;
2408 if (pedantic)
2409 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2410 if (!noconvert)
2411 arg = default_conversion (arg);
2413 else
2415 error ("wrong type argument to bit-complement");
2416 return error_mark_node;
2418 break;
2420 case ABS_EXPR:
2421 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2423 error ("wrong type argument to abs");
2424 return error_mark_node;
2426 else if (!noconvert)
2427 arg = default_conversion (arg);
2428 break;
2430 case CONJ_EXPR:
2431 /* Conjugating a real value is a no-op, but allow it anyway. */
2432 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2433 || typecode == COMPLEX_TYPE))
2435 error ("wrong type argument to conjugation");
2436 return error_mark_node;
2438 else if (!noconvert)
2439 arg = default_conversion (arg);
2440 break;
2442 case TRUTH_NOT_EXPR:
2443 if (typecode != INTEGER_TYPE
2444 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2445 && typecode != COMPLEX_TYPE
2446 /* These will convert to a pointer. */
2447 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2449 error ("wrong type argument to unary exclamation mark");
2450 return error_mark_node;
2452 arg = lang_hooks.truthvalue_conversion (arg);
2453 return invert_truthvalue (arg);
2455 case NOP_EXPR:
2456 break;
2458 case REALPART_EXPR:
2459 if (TREE_CODE (arg) == COMPLEX_CST)
2460 return TREE_REALPART (arg);
2461 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2462 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2463 else
2464 return arg;
2466 case IMAGPART_EXPR:
2467 if (TREE_CODE (arg) == COMPLEX_CST)
2468 return TREE_IMAGPART (arg);
2469 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2470 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2471 else
2472 return convert (TREE_TYPE (arg), integer_zero_node);
2474 case PREINCREMENT_EXPR:
2475 case POSTINCREMENT_EXPR:
2476 case PREDECREMENT_EXPR:
2477 case POSTDECREMENT_EXPR:
2479 /* Increment or decrement the real part of the value,
2480 and don't change the imaginary part. */
2481 if (typecode == COMPLEX_TYPE)
2483 tree real, imag;
2485 if (pedantic)
2486 pedwarn ("ISO C does not support %<++%> and %<--%>"
2487 " on complex types");
2489 arg = stabilize_reference (arg);
2490 real = build_unary_op (REALPART_EXPR, arg, 1);
2491 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2492 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2493 build_unary_op (code, real, 1), imag);
2496 /* Report invalid types. */
2498 if (typecode != POINTER_TYPE
2499 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2501 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2502 error ("wrong type argument to increment");
2503 else
2504 error ("wrong type argument to decrement");
2506 return error_mark_node;
2510 tree inc;
2511 tree result_type = TREE_TYPE (arg);
2513 arg = get_unwidened (arg, 0);
2514 argtype = TREE_TYPE (arg);
2516 /* Compute the increment. */
2518 if (typecode == POINTER_TYPE)
2520 /* If pointer target is an undefined struct,
2521 we just cannot know how to do the arithmetic. */
2522 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2524 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2525 error ("increment of pointer to unknown structure");
2526 else
2527 error ("decrement of pointer to unknown structure");
2529 else if ((pedantic || warn_pointer_arith)
2530 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2531 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2533 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2534 pedwarn ("wrong type argument to increment");
2535 else
2536 pedwarn ("wrong type argument to decrement");
2539 inc = c_size_in_bytes (TREE_TYPE (result_type));
2541 else
2542 inc = integer_one_node;
2544 inc = convert (argtype, inc);
2546 /* Complain about anything else that is not a true lvalue. */
2547 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2548 || code == POSTINCREMENT_EXPR)
2549 ? "invalid lvalue in increment"
2550 : "invalid lvalue in decrement")))
2551 return error_mark_node;
2553 /* Report a read-only lvalue. */
2554 if (TREE_READONLY (arg))
2555 readonly_error (arg,
2556 ((code == PREINCREMENT_EXPR
2557 || code == POSTINCREMENT_EXPR)
2558 ? "increment" : "decrement"));
2560 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2561 val = boolean_increment (code, arg);
2562 else
2563 val = build2 (code, TREE_TYPE (arg), arg, inc);
2564 TREE_SIDE_EFFECTS (val) = 1;
2565 val = convert (result_type, val);
2566 if (TREE_CODE (val) != code)
2567 TREE_NO_WARNING (val) = 1;
2568 return val;
2571 case ADDR_EXPR:
2572 /* Note that this operation never does default_conversion. */
2574 /* Let &* cancel out to simplify resulting code. */
2575 if (TREE_CODE (arg) == INDIRECT_REF)
2577 /* Don't let this be an lvalue. */
2578 if (lvalue_p (TREE_OPERAND (arg, 0)))
2579 return non_lvalue (TREE_OPERAND (arg, 0));
2580 return TREE_OPERAND (arg, 0);
2583 /* For &x[y], return x+y */
2584 if (TREE_CODE (arg) == ARRAY_REF)
2586 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2587 return error_mark_node;
2588 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2589 TREE_OPERAND (arg, 1), 1);
2592 /* Anything not already handled and not a true memory reference
2593 or a non-lvalue array is an error. */
2594 else if (typecode != FUNCTION_TYPE && !flag
2595 && !lvalue_or_else (arg, "invalid lvalue in unary %<&%>"))
2596 return error_mark_node;
2598 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2599 argtype = TREE_TYPE (arg);
2601 /* If the lvalue is const or volatile, merge that into the type
2602 to which the address will point. Note that you can't get a
2603 restricted pointer by taking the address of something, so we
2604 only have to deal with `const' and `volatile' here. */
2605 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2606 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2607 argtype = c_build_type_variant (argtype,
2608 TREE_READONLY (arg),
2609 TREE_THIS_VOLATILE (arg));
2611 if (!c_mark_addressable (arg))
2612 return error_mark_node;
2614 if (TREE_CODE (arg) == COMPONENT_REF
2615 && DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
2617 error ("attempt to take address of bit-field structure member %qD",
2618 TREE_OPERAND (arg, 1));
2619 return error_mark_node;
2622 argtype = build_pointer_type (argtype);
2624 /* ??? Cope with user tricks that amount to offsetof. Delete this
2625 when we have proper support for integer constant expressions. */
2626 val = get_base_address (arg);
2627 if (val && TREE_CODE (val) == INDIRECT_REF
2628 && integer_zerop (TREE_OPERAND (val, 0)))
2629 return fold_convert (argtype, fold_offsetof (arg));
2631 val = build1 (ADDR_EXPR, argtype, arg);
2633 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2634 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2636 return val;
2638 default:
2639 break;
2642 if (argtype == 0)
2643 argtype = TREE_TYPE (arg);
2644 val = build1 (code, argtype, arg);
2645 return require_constant_value ? fold_initializer (val) : fold (val);
2648 /* Return nonzero if REF is an lvalue valid for this language.
2649 Lvalues can be assigned, unless their type has TYPE_READONLY.
2650 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2653 lvalue_p (tree ref)
2655 enum tree_code code = TREE_CODE (ref);
2657 switch (code)
2659 case REALPART_EXPR:
2660 case IMAGPART_EXPR:
2661 case COMPONENT_REF:
2662 return lvalue_p (TREE_OPERAND (ref, 0));
2664 case COMPOUND_LITERAL_EXPR:
2665 case STRING_CST:
2666 return 1;
2668 case INDIRECT_REF:
2669 case ARRAY_REF:
2670 case VAR_DECL:
2671 case PARM_DECL:
2672 case RESULT_DECL:
2673 case ERROR_MARK:
2674 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2675 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2677 case BIND_EXPR:
2678 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2680 default:
2681 return 0;
2685 /* Return nonzero if REF is an lvalue valid for this language;
2686 otherwise, print an error message and return zero. */
2688 static int
2689 lvalue_or_else (tree ref, const char *msgid)
2691 int win = lvalue_p (ref);
2693 if (! win)
2694 error ("%s", msgid);
2696 return win;
2700 /* Warn about storing in something that is `const'. */
2702 void
2703 readonly_error (tree arg, const char *msgid)
2705 if (TREE_CODE (arg) == COMPONENT_REF)
2707 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2708 readonly_error (TREE_OPERAND (arg, 0), msgid);
2709 else
2710 error ("%s of read-only member %qs", _(msgid),
2711 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2713 else if (TREE_CODE (arg) == VAR_DECL)
2714 error ("%s of read-only variable %qs", _(msgid),
2715 IDENTIFIER_POINTER (DECL_NAME (arg)));
2716 else
2717 error ("%s of read-only location", _(msgid));
2720 /* Mark EXP saying that we need to be able to take the
2721 address of it; it should not be allocated in a register.
2722 Returns true if successful. */
2724 bool
2725 c_mark_addressable (tree exp)
2727 tree x = exp;
2729 while (1)
2730 switch (TREE_CODE (x))
2732 case COMPONENT_REF:
2733 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2735 error ("cannot take address of bit-field %qs",
2736 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2737 return false;
2740 /* ... fall through ... */
2742 case ADDR_EXPR:
2743 case ARRAY_REF:
2744 case REALPART_EXPR:
2745 case IMAGPART_EXPR:
2746 x = TREE_OPERAND (x, 0);
2747 break;
2749 case COMPOUND_LITERAL_EXPR:
2750 case CONSTRUCTOR:
2751 TREE_ADDRESSABLE (x) = 1;
2752 return true;
2754 case VAR_DECL:
2755 case CONST_DECL:
2756 case PARM_DECL:
2757 case RESULT_DECL:
2758 if (C_DECL_REGISTER (x)
2759 && DECL_NONLOCAL (x))
2761 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2763 error ("global register variable %qs used in nested function",
2764 IDENTIFIER_POINTER (DECL_NAME (x)));
2765 return false;
2767 pedwarn ("register variable %qs used in nested function",
2768 IDENTIFIER_POINTER (DECL_NAME (x)));
2770 else if (C_DECL_REGISTER (x))
2772 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2774 error ("address of global register variable %qs requested",
2775 IDENTIFIER_POINTER (DECL_NAME (x)));
2776 return false;
2779 pedwarn ("address of register variable %qs requested",
2780 IDENTIFIER_POINTER (DECL_NAME (x)));
2783 /* drops in */
2784 case FUNCTION_DECL:
2785 TREE_ADDRESSABLE (x) = 1;
2786 /* drops out */
2787 default:
2788 return true;
2792 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2794 tree
2795 build_conditional_expr (tree ifexp, tree op1, tree op2)
2797 tree type1;
2798 tree type2;
2799 enum tree_code code1;
2800 enum tree_code code2;
2801 tree result_type = NULL;
2802 tree orig_op1 = op1, orig_op2 = op2;
2804 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2806 /* Promote both alternatives. */
2808 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2809 op1 = default_conversion (op1);
2810 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2811 op2 = default_conversion (op2);
2813 if (TREE_CODE (ifexp) == ERROR_MARK
2814 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2815 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2816 return error_mark_node;
2818 type1 = TREE_TYPE (op1);
2819 code1 = TREE_CODE (type1);
2820 type2 = TREE_TYPE (op2);
2821 code2 = TREE_CODE (type2);
2823 /* C90 does not permit non-lvalue arrays in conditional expressions.
2824 In C99 they will be pointers by now. */
2825 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2827 error ("non-lvalue array in conditional expression");
2828 return error_mark_node;
2831 /* Quickly detect the usual case where op1 and op2 have the same type
2832 after promotion. */
2833 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2835 if (type1 == type2)
2836 result_type = type1;
2837 else
2838 result_type = TYPE_MAIN_VARIANT (type1);
2840 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2841 || code1 == COMPLEX_TYPE)
2842 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2843 || code2 == COMPLEX_TYPE))
2845 result_type = common_type (type1, type2);
2847 /* If -Wsign-compare, warn here if type1 and type2 have
2848 different signedness. We'll promote the signed to unsigned
2849 and later code won't know it used to be different.
2850 Do this check on the original types, so that explicit casts
2851 will be considered, but default promotions won't. */
2852 if (warn_sign_compare && !skip_evaluation)
2854 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2855 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2857 if (unsigned_op1 ^ unsigned_op2)
2859 /* Do not warn if the result type is signed, since the
2860 signed type will only be chosen if it can represent
2861 all the values of the unsigned type. */
2862 if (! TYPE_UNSIGNED (result_type))
2863 /* OK */;
2864 /* Do not warn if the signed quantity is an unsuffixed
2865 integer literal (or some static constant expression
2866 involving such literals) and it is non-negative. */
2867 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2868 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2869 /* OK */;
2870 else
2871 warning ("signed and unsigned type in conditional expression");
2875 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2877 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2878 pedwarn ("ISO C forbids conditional expr with only one void side");
2879 result_type = void_type_node;
2881 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2883 if (comp_target_types (type1, type2, 1))
2884 result_type = common_pointer_type (type1, type2);
2885 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2886 && TREE_CODE (orig_op1) != NOP_EXPR)
2887 result_type = qualify_type (type2, type1);
2888 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2889 && TREE_CODE (orig_op2) != NOP_EXPR)
2890 result_type = qualify_type (type1, type2);
2891 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2893 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2894 pedwarn ("ISO C forbids conditional expr between "
2895 "%<void *%> and function pointer");
2896 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2897 TREE_TYPE (type2)));
2899 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2901 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2902 pedwarn ("ISO C forbids conditional expr between "
2903 "%<void *%> and function pointer");
2904 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2905 TREE_TYPE (type1)));
2907 else
2909 pedwarn ("pointer type mismatch in conditional expression");
2910 result_type = build_pointer_type (void_type_node);
2913 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2915 if (! integer_zerop (op2))
2916 pedwarn ("pointer/integer type mismatch in conditional expression");
2917 else
2919 op2 = null_pointer_node;
2921 result_type = type1;
2923 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2925 if (!integer_zerop (op1))
2926 pedwarn ("pointer/integer type mismatch in conditional expression");
2927 else
2929 op1 = null_pointer_node;
2931 result_type = type2;
2934 if (!result_type)
2936 if (flag_cond_mismatch)
2937 result_type = void_type_node;
2938 else
2940 error ("type mismatch in conditional expression");
2941 return error_mark_node;
2945 /* Merge const and volatile flags of the incoming types. */
2946 result_type
2947 = build_type_variant (result_type,
2948 TREE_READONLY (op1) || TREE_READONLY (op2),
2949 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2951 if (result_type != TREE_TYPE (op1))
2952 op1 = convert_and_check (result_type, op1);
2953 if (result_type != TREE_TYPE (op2))
2954 op2 = convert_and_check (result_type, op2);
2956 if (TREE_CODE (ifexp) == INTEGER_CST)
2957 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2959 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
2962 /* Return a compound expression that performs two expressions and
2963 returns the value of the second of them. */
2965 tree
2966 build_compound_expr (tree expr1, tree expr2)
2968 /* Convert arrays and functions to pointers. */
2969 expr2 = default_function_array_conversion (expr2);
2971 /* Don't let (0, 0) be null pointer constant. */
2972 if (integer_zerop (expr2))
2973 expr2 = non_lvalue (expr2);
2975 if (! TREE_SIDE_EFFECTS (expr1))
2977 /* The left-hand operand of a comma expression is like an expression
2978 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2979 any side-effects, unless it was explicitly cast to (void). */
2980 if (warn_unused_value
2981 && ! (TREE_CODE (expr1) == CONVERT_EXPR
2982 && VOID_TYPE_P (TREE_TYPE (expr1))))
2983 warning ("left-hand operand of comma expression has no effect");
2986 /* With -Wunused, we should also warn if the left-hand operand does have
2987 side-effects, but computes a value which is not used. For example, in
2988 `foo() + bar(), baz()' the result of the `+' operator is not used,
2989 so we should issue a warning. */
2990 else if (warn_unused_value)
2991 warn_if_unused_value (expr1, input_location);
2993 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
2996 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2998 tree
2999 build_c_cast (tree type, tree expr)
3001 tree value = expr;
3003 if (type == error_mark_node || expr == error_mark_node)
3004 return error_mark_node;
3006 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3007 only in <protocol> qualifications. But when constructing cast expressions,
3008 the protocols do matter and must be kept around. */
3009 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3010 return build1 (NOP_EXPR, type, expr);
3012 type = TYPE_MAIN_VARIANT (type);
3014 if (TREE_CODE (type) == ARRAY_TYPE)
3016 error ("cast specifies array type");
3017 return error_mark_node;
3020 if (TREE_CODE (type) == FUNCTION_TYPE)
3022 error ("cast specifies function type");
3023 return error_mark_node;
3026 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3028 if (pedantic)
3030 if (TREE_CODE (type) == RECORD_TYPE
3031 || TREE_CODE (type) == UNION_TYPE)
3032 pedwarn ("ISO C forbids casting nonscalar to the same type");
3035 else if (TREE_CODE (type) == UNION_TYPE)
3037 tree field;
3038 value = default_function_array_conversion (value);
3040 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3041 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3042 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3043 break;
3045 if (field)
3047 tree t;
3049 if (pedantic)
3050 pedwarn ("ISO C forbids casts to union type");
3051 t = digest_init (type,
3052 build_constructor (type,
3053 build_tree_list (field, value)),
3054 true, 0);
3055 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3056 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3057 return t;
3059 error ("cast to union type from type not present in union");
3060 return error_mark_node;
3062 else
3064 tree otype, ovalue;
3066 /* If casting to void, avoid the error that would come
3067 from default_conversion in the case of a non-lvalue array. */
3068 if (type == void_type_node)
3069 return build1 (CONVERT_EXPR, type, value);
3071 /* Convert functions and arrays to pointers,
3072 but don't convert any other types. */
3073 value = default_function_array_conversion (value);
3074 otype = TREE_TYPE (value);
3076 /* Optionally warn about potentially worrisome casts. */
3078 if (warn_cast_qual
3079 && TREE_CODE (type) == POINTER_TYPE
3080 && TREE_CODE (otype) == POINTER_TYPE)
3082 tree in_type = type;
3083 tree in_otype = otype;
3084 int added = 0;
3085 int discarded = 0;
3087 /* Check that the qualifiers on IN_TYPE are a superset of
3088 the qualifiers of IN_OTYPE. The outermost level of
3089 POINTER_TYPE nodes is uninteresting and we stop as soon
3090 as we hit a non-POINTER_TYPE node on either type. */
3093 in_otype = TREE_TYPE (in_otype);
3094 in_type = TREE_TYPE (in_type);
3096 /* GNU C allows cv-qualified function types. 'const'
3097 means the function is very pure, 'volatile' means it
3098 can't return. We need to warn when such qualifiers
3099 are added, not when they're taken away. */
3100 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3101 && TREE_CODE (in_type) == FUNCTION_TYPE)
3102 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3103 else
3104 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3106 while (TREE_CODE (in_type) == POINTER_TYPE
3107 && TREE_CODE (in_otype) == POINTER_TYPE);
3109 if (added)
3110 warning ("cast adds new qualifiers to function type");
3112 if (discarded)
3113 /* There are qualifiers present in IN_OTYPE that are not
3114 present in IN_TYPE. */
3115 warning ("cast discards qualifiers from pointer target type");
3118 /* Warn about possible alignment problems. */
3119 if (STRICT_ALIGNMENT && warn_cast_align
3120 && TREE_CODE (type) == POINTER_TYPE
3121 && TREE_CODE (otype) == POINTER_TYPE
3122 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3123 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3124 /* Don't warn about opaque types, where the actual alignment
3125 restriction is unknown. */
3126 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3127 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3128 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3129 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3130 warning ("cast increases required alignment of target type");
3132 if (TREE_CODE (type) == INTEGER_TYPE
3133 && TREE_CODE (otype) == POINTER_TYPE
3134 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3135 && !TREE_CONSTANT (value))
3136 warning ("cast from pointer to integer of different size");
3138 if (warn_bad_function_cast
3139 && TREE_CODE (value) == CALL_EXPR
3140 && TREE_CODE (type) != TREE_CODE (otype))
3141 warning ("cast does not match function type");
3143 if (TREE_CODE (type) == POINTER_TYPE
3144 && TREE_CODE (otype) == INTEGER_TYPE
3145 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3146 /* Don't warn about converting any constant. */
3147 && !TREE_CONSTANT (value))
3148 warning ("cast to pointer from integer of different size");
3150 if (TREE_CODE (type) == POINTER_TYPE
3151 && TREE_CODE (otype) == POINTER_TYPE
3152 && TREE_CODE (expr) == ADDR_EXPR
3153 && DECL_P (TREE_OPERAND (expr, 0))
3154 && flag_strict_aliasing && warn_strict_aliasing
3155 && !VOID_TYPE_P (TREE_TYPE (type)))
3157 /* Casting the address of a decl to non void pointer. Warn
3158 if the cast breaks type based aliasing. */
3159 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3160 warning ("type-punning to incomplete type might break strict-aliasing rules");
3161 else
3163 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3164 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3166 if (!alias_sets_conflict_p (set1, set2))
3167 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3168 else if (warn_strict_aliasing > 1
3169 && !alias_sets_might_conflict_p (set1, set2))
3170 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3174 /* If pedantic, warn for conversions between function and object
3175 pointer types, except for converting a null pointer constant
3176 to function pointer type. */
3177 if (pedantic
3178 && TREE_CODE (type) == POINTER_TYPE
3179 && TREE_CODE (otype) == POINTER_TYPE
3180 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3181 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3182 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3184 if (pedantic
3185 && TREE_CODE (type) == POINTER_TYPE
3186 && TREE_CODE (otype) == POINTER_TYPE
3187 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3188 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3189 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3190 && TREE_CODE (expr) != NOP_EXPR))
3191 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3193 ovalue = value;
3194 /* Replace a nonvolatile const static variable with its value. */
3195 if (optimize && TREE_CODE (value) == VAR_DECL)
3196 value = decl_constant_value (value);
3197 value = convert (type, value);
3199 /* Ignore any integer overflow caused by the cast. */
3200 if (TREE_CODE (value) == INTEGER_CST)
3202 if (EXPR_P (ovalue))
3203 /* If OVALUE had overflow set, then so will VALUE, so it
3204 is safe to overwrite. */
3205 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3206 else
3207 TREE_OVERFLOW (value) = 0;
3209 if (CONSTANT_CLASS_P (ovalue))
3210 /* Similarly, constant_overflow cannot have become
3211 cleared. */
3212 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3216 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3217 if (TREE_CODE (value) == INTEGER_CST
3218 && TREE_CODE (expr) == INTEGER_CST
3219 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3220 value = non_lvalue (value);
3222 /* Don't let a cast be an lvalue. */
3223 if (value == expr)
3224 value = non_lvalue (value);
3226 return value;
3229 /* Interpret a cast of expression EXPR to type TYPE. */
3230 tree
3231 c_cast_expr (struct c_type_name *type_name, tree expr)
3233 tree type;
3234 int saved_wsp = warn_strict_prototypes;
3236 /* This avoids warnings about unprototyped casts on
3237 integers. E.g. "#define SIG_DFL (void(*)())0". */
3238 if (TREE_CODE (expr) == INTEGER_CST)
3239 warn_strict_prototypes = 0;
3240 type = groktypename (type_name);
3241 warn_strict_prototypes = saved_wsp;
3243 return build_c_cast (type, expr);
3247 /* Build an assignment expression of lvalue LHS from value RHS.
3248 MODIFYCODE is the code for a binary operator that we use
3249 to combine the old value of LHS with RHS to get the new value.
3250 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3252 tree
3253 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3255 tree result;
3256 tree newrhs;
3257 tree lhstype = TREE_TYPE (lhs);
3258 tree olhstype = lhstype;
3260 /* Types that aren't fully specified cannot be used in assignments. */
3261 lhs = require_complete_type (lhs);
3263 /* Avoid duplicate error messages from operands that had errors. */
3264 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3265 return error_mark_node;
3267 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3268 /* Do not use STRIP_NOPS here. We do not want an enumerator
3269 whose value is 0 to count as a null pointer constant. */
3270 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3271 rhs = TREE_OPERAND (rhs, 0);
3273 newrhs = rhs;
3275 /* If a binary op has been requested, combine the old LHS value with the RHS
3276 producing the value we should actually store into the LHS. */
3278 if (modifycode != NOP_EXPR)
3280 lhs = stabilize_reference (lhs);
3281 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3284 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3285 return error_mark_node;
3287 /* Warn about storing in something that is `const'. */
3289 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3290 || ((TREE_CODE (lhstype) == RECORD_TYPE
3291 || TREE_CODE (lhstype) == UNION_TYPE)
3292 && C_TYPE_FIELDS_READONLY (lhstype)))
3293 readonly_error (lhs, "assignment");
3295 /* If storing into a structure or union member,
3296 it has probably been given type `int'.
3297 Compute the type that would go with
3298 the actual amount of storage the member occupies. */
3300 if (TREE_CODE (lhs) == COMPONENT_REF
3301 && (TREE_CODE (lhstype) == INTEGER_TYPE
3302 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3303 || TREE_CODE (lhstype) == REAL_TYPE
3304 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3305 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3307 /* If storing in a field that is in actuality a short or narrower than one,
3308 we must store in the field in its actual type. */
3310 if (lhstype != TREE_TYPE (lhs))
3312 lhs = copy_node (lhs);
3313 TREE_TYPE (lhs) = lhstype;
3316 /* Convert new value to destination type. */
3318 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3319 NULL_TREE, NULL_TREE, 0);
3320 if (TREE_CODE (newrhs) == ERROR_MARK)
3321 return error_mark_node;
3323 /* Scan operands */
3325 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3326 TREE_SIDE_EFFECTS (result) = 1;
3328 /* If we got the LHS in a different type for storing in,
3329 convert the result back to the nominal type of LHS
3330 so that the value we return always has the same type
3331 as the LHS argument. */
3333 if (olhstype == TREE_TYPE (result))
3334 return result;
3335 return convert_for_assignment (olhstype, result, _("assignment"),
3336 NULL_TREE, NULL_TREE, 0);
3339 /* Convert value RHS to type TYPE as preparation for an assignment
3340 to an lvalue of type TYPE.
3341 The real work of conversion is done by `convert'.
3342 The purpose of this function is to generate error messages
3343 for assignments that are not allowed in C.
3344 ERRTYPE is a string to use in error messages:
3345 "assignment", "return", etc. If it is null, this is parameter passing
3346 for a function call (and different error messages are output).
3348 FUNNAME is the name of the function being called,
3349 as an IDENTIFIER_NODE, or null.
3350 PARMNUM is the number of the argument, for printing in error messages. */
3352 static tree
3353 convert_for_assignment (tree type, tree rhs, const char *errtype,
3354 tree fundecl, tree funname, int parmnum)
3356 enum tree_code codel = TREE_CODE (type);
3357 tree rhstype;
3358 enum tree_code coder;
3360 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3361 /* Do not use STRIP_NOPS here. We do not want an enumerator
3362 whose value is 0 to count as a null pointer constant. */
3363 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3364 rhs = TREE_OPERAND (rhs, 0);
3366 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3367 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3368 rhs = default_conversion (rhs);
3369 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3370 rhs = decl_constant_value_for_broken_optimization (rhs);
3372 rhstype = TREE_TYPE (rhs);
3373 coder = TREE_CODE (rhstype);
3375 if (coder == ERROR_MARK)
3376 return error_mark_node;
3378 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3380 overflow_warning (rhs);
3381 /* Check for Objective-C protocols. This will automatically
3382 issue a warning if there are protocol violations. No need to
3383 use the return value. */
3384 if (c_dialect_objc ())
3385 objc_comptypes (type, rhstype, 0);
3386 return rhs;
3389 if (coder == VOID_TYPE)
3391 error ("void value not ignored as it ought to be");
3392 return error_mark_node;
3394 /* A type converts to a reference to it.
3395 This code doesn't fully support references, it's just for the
3396 special case of va_start and va_copy. */
3397 if (codel == REFERENCE_TYPE
3398 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3400 if (!lvalue_p (rhs))
3402 error ("cannot pass rvalue to reference parameter");
3403 return error_mark_node;
3405 if (!c_mark_addressable (rhs))
3406 return error_mark_node;
3407 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3409 /* We already know that these two types are compatible, but they
3410 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3411 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3412 likely to be va_list, a typedef to __builtin_va_list, which
3413 is different enough that it will cause problems later. */
3414 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3415 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3417 rhs = build1 (NOP_EXPR, type, rhs);
3418 return rhs;
3420 /* Some types can interconvert without explicit casts. */
3421 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3422 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3423 return convert (type, rhs);
3424 /* Arithmetic types all interconvert, and enum is treated like int. */
3425 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3426 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3427 || codel == BOOLEAN_TYPE)
3428 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3429 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3430 || coder == BOOLEAN_TYPE))
3431 return convert_and_check (type, rhs);
3433 /* Conversion to a transparent union from its member types.
3434 This applies only to function arguments. */
3435 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3437 tree memb_types;
3438 tree marginal_memb_type = 0;
3440 for (memb_types = TYPE_FIELDS (type); memb_types;
3441 memb_types = TREE_CHAIN (memb_types))
3443 tree memb_type = TREE_TYPE (memb_types);
3445 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3446 TYPE_MAIN_VARIANT (rhstype)))
3447 break;
3449 if (TREE_CODE (memb_type) != POINTER_TYPE)
3450 continue;
3452 if (coder == POINTER_TYPE)
3454 tree ttl = TREE_TYPE (memb_type);
3455 tree ttr = TREE_TYPE (rhstype);
3457 /* Any non-function converts to a [const][volatile] void *
3458 and vice versa; otherwise, targets must be the same.
3459 Meanwhile, the lhs target must have all the qualifiers of
3460 the rhs. */
3461 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3462 || comp_target_types (memb_type, rhstype, 0))
3464 /* If this type won't generate any warnings, use it. */
3465 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3466 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3467 && TREE_CODE (ttl) == FUNCTION_TYPE)
3468 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3469 == TYPE_QUALS (ttr))
3470 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3471 == TYPE_QUALS (ttl))))
3472 break;
3474 /* Keep looking for a better type, but remember this one. */
3475 if (! marginal_memb_type)
3476 marginal_memb_type = memb_type;
3480 /* Can convert integer zero to any pointer type. */
3481 if (integer_zerop (rhs)
3482 || (TREE_CODE (rhs) == NOP_EXPR
3483 && integer_zerop (TREE_OPERAND (rhs, 0))))
3485 rhs = null_pointer_node;
3486 break;
3490 if (memb_types || marginal_memb_type)
3492 if (! memb_types)
3494 /* We have only a marginally acceptable member type;
3495 it needs a warning. */
3496 tree ttl = TREE_TYPE (marginal_memb_type);
3497 tree ttr = TREE_TYPE (rhstype);
3499 /* Const and volatile mean something different for function
3500 types, so the usual warnings are not appropriate. */
3501 if (TREE_CODE (ttr) == FUNCTION_TYPE
3502 && TREE_CODE (ttl) == FUNCTION_TYPE)
3504 /* Because const and volatile on functions are
3505 restrictions that say the function will not do
3506 certain things, it is okay to use a const or volatile
3507 function where an ordinary one is wanted, but not
3508 vice-versa. */
3509 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3510 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3511 errtype, funname, parmnum);
3513 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3514 warn_for_assignment ("%s discards qualifiers from pointer target type",
3515 errtype, funname,
3516 parmnum);
3519 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3520 pedwarn ("ISO C prohibits argument conversion to union type");
3522 return build1 (NOP_EXPR, type, rhs);
3526 /* Conversions among pointers */
3527 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3528 && (coder == codel))
3530 tree ttl = TREE_TYPE (type);
3531 tree ttr = TREE_TYPE (rhstype);
3532 bool is_opaque_pointer;
3533 int target_cmp = 0; /* Cache comp_target_types () result. */
3535 /* Opaque pointers are treated like void pointers. */
3536 is_opaque_pointer = (targetm.vector_opaque_p (type)
3537 || targetm.vector_opaque_p (rhstype))
3538 && TREE_CODE (ttl) == VECTOR_TYPE
3539 && TREE_CODE (ttr) == VECTOR_TYPE;
3541 /* Any non-function converts to a [const][volatile] void *
3542 and vice versa; otherwise, targets must be the same.
3543 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3544 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3545 || (target_cmp = comp_target_types (type, rhstype, 0))
3546 || is_opaque_pointer
3547 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3548 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3550 if (pedantic
3551 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3553 (VOID_TYPE_P (ttr)
3554 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3555 which are not ANSI null ptr constants. */
3556 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3557 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3558 warn_for_assignment ("ISO C forbids %s between function "
3559 "pointer and %<void *%>",
3560 errtype, funname, parmnum);
3561 /* Const and volatile mean something different for function types,
3562 so the usual warnings are not appropriate. */
3563 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3564 && TREE_CODE (ttl) != FUNCTION_TYPE)
3566 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3567 warn_for_assignment ("%s discards qualifiers from pointer target type",
3568 errtype, funname, parmnum);
3569 /* If this is not a case of ignoring a mismatch in signedness,
3570 no warning. */
3571 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3572 || target_cmp)
3574 /* If there is a mismatch, do warn. */
3575 else
3576 warn_for_assignment ("pointer targets in %s differ in signedness",
3577 errtype, funname, parmnum);
3579 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3580 && TREE_CODE (ttr) == FUNCTION_TYPE)
3582 /* Because const and volatile on functions are restrictions
3583 that say the function will not do certain things,
3584 it is okay to use a const or volatile function
3585 where an ordinary one is wanted, but not vice-versa. */
3586 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3587 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3588 errtype, funname, parmnum);
3591 else
3592 warn_for_assignment ("%s from incompatible pointer type",
3593 errtype, funname, parmnum);
3594 return convert (type, rhs);
3596 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3598 error ("invalid use of non-lvalue array");
3599 return error_mark_node;
3601 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3603 /* An explicit constant 0 can convert to a pointer,
3604 or one that results from arithmetic, even including
3605 a cast to integer type. */
3606 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3608 ! (TREE_CODE (rhs) == NOP_EXPR
3609 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3610 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3611 && integer_zerop (TREE_OPERAND (rhs, 0))))
3612 warn_for_assignment ("%s makes pointer from integer without a cast",
3613 errtype, funname, parmnum);
3615 return convert (type, rhs);
3617 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3619 warn_for_assignment ("%s makes integer from pointer without a cast",
3620 errtype, funname, parmnum);
3621 return convert (type, rhs);
3623 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3624 return convert (type, rhs);
3626 if (!errtype)
3628 if (funname)
3630 tree selector = objc_message_selector ();
3632 if (selector && parmnum > 2)
3633 error ("incompatible type for argument %d of %qs",
3634 parmnum - 2, IDENTIFIER_POINTER (selector));
3635 else
3636 error ("incompatible type for argument %d of %qs",
3637 parmnum, IDENTIFIER_POINTER (funname));
3639 else
3640 error ("incompatible type for argument %d of indirect function call",
3641 parmnum);
3643 else
3644 error ("incompatible types in %s", errtype);
3646 return error_mark_node;
3649 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3650 is used for error and waring reporting and indicates which argument
3651 is being processed. */
3653 tree
3654 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3656 tree ret, type;
3658 /* If FN was prototyped, the value has been converted already
3659 in convert_arguments. */
3660 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3661 return value;
3663 type = TREE_TYPE (parm);
3664 ret = convert_for_assignment (type, value,
3665 (char *) 0 /* arg passing */, fn,
3666 DECL_NAME (fn), argnum);
3667 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3668 && INTEGRAL_TYPE_P (type)
3669 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3670 ret = default_conversion (ret);
3671 return ret;
3674 /* Print a warning using MSGID.
3675 It gets OPNAME as its one parameter.
3676 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3677 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3678 FUNCTION and ARGNUM are handled specially if we are building an
3679 Objective-C selector. */
3681 static void
3682 warn_for_assignment (const char *msgid, const char *opname, tree function,
3683 int argnum)
3685 if (opname == 0)
3687 tree selector = objc_message_selector ();
3688 char * new_opname;
3690 if (selector && argnum > 2)
3692 function = selector;
3693 argnum -= 2;
3695 if (argnum == 0)
3697 if (function)
3699 /* Function name is known; supply it. */
3700 const char *const argstring = _("passing arg of '%s'");
3701 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
3702 + strlen (argstring) + 1 + 1);
3703 sprintf (new_opname, argstring,
3704 IDENTIFIER_POINTER (function));
3706 else
3708 /* Function name unknown (call through ptr). */
3709 const char *const argnofun = _("passing arg of pointer to function");
3710 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
3711 sprintf (new_opname, argnofun);
3714 else if (function)
3716 /* Function name is known; supply it. */
3717 const char *const argstring = _("passing arg %d of '%s'");
3718 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
3719 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3720 sprintf (new_opname, argstring, argnum,
3721 IDENTIFIER_POINTER (function));
3723 else
3725 /* Function name unknown (call through ptr); just give arg number. */
3726 const char *const argnofun = _("passing arg %d of pointer to function");
3727 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3728 sprintf (new_opname, argnofun, argnum);
3730 opname = new_opname;
3732 pedwarn (msgid, opname);
3735 /* If VALUE is a compound expr all of whose expressions are constant, then
3736 return its value. Otherwise, return error_mark_node.
3738 This is for handling COMPOUND_EXPRs as initializer elements
3739 which is allowed with a warning when -pedantic is specified. */
3741 static tree
3742 valid_compound_expr_initializer (tree value, tree endtype)
3744 if (TREE_CODE (value) == COMPOUND_EXPR)
3746 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3747 == error_mark_node)
3748 return error_mark_node;
3749 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3750 endtype);
3752 else if (!initializer_constant_valid_p (value, endtype))
3753 return error_mark_node;
3754 else
3755 return value;
3758 /* Perform appropriate conversions on the initial value of a variable,
3759 store it in the declaration DECL,
3760 and print any error messages that are appropriate.
3761 If the init is invalid, store an ERROR_MARK. */
3763 void
3764 store_init_value (tree decl, tree init)
3766 tree value, type;
3768 /* If variable's type was invalidly declared, just ignore it. */
3770 type = TREE_TYPE (decl);
3771 if (TREE_CODE (type) == ERROR_MARK)
3772 return;
3774 /* Digest the specified initializer into an expression. */
3776 value = digest_init (type, init, true, TREE_STATIC (decl));
3778 /* Store the expression if valid; else report error. */
3780 if (warn_traditional && !in_system_header
3781 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3782 warning ("traditional C rejects automatic aggregate initialization");
3784 DECL_INITIAL (decl) = value;
3786 /* ANSI wants warnings about out-of-range constant initializers. */
3787 STRIP_TYPE_NOPS (value);
3788 constant_expression_warning (value);
3790 /* Check if we need to set array size from compound literal size. */
3791 if (TREE_CODE (type) == ARRAY_TYPE
3792 && TYPE_DOMAIN (type) == 0
3793 && value != error_mark_node)
3795 tree inside_init = init;
3797 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3798 inside_init = TREE_OPERAND (init, 0);
3799 inside_init = fold (inside_init);
3801 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3803 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3805 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3807 /* For int foo[] = (int [3]){1}; we need to set array size
3808 now since later on array initializer will be just the
3809 brace enclosed list of the compound literal. */
3810 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3811 layout_type (type);
3812 layout_decl (decl, 0);
3818 /* Methods for storing and printing names for error messages. */
3820 /* Implement a spelling stack that allows components of a name to be pushed
3821 and popped. Each element on the stack is this structure. */
3823 struct spelling
3825 int kind;
3826 union
3828 int i;
3829 const char *s;
3830 } u;
3833 #define SPELLING_STRING 1
3834 #define SPELLING_MEMBER 2
3835 #define SPELLING_BOUNDS 3
3837 static struct spelling *spelling; /* Next stack element (unused). */
3838 static struct spelling *spelling_base; /* Spelling stack base. */
3839 static int spelling_size; /* Size of the spelling stack. */
3841 /* Macros to save and restore the spelling stack around push_... functions.
3842 Alternative to SAVE_SPELLING_STACK. */
3844 #define SPELLING_DEPTH() (spelling - spelling_base)
3845 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3847 /* Push an element on the spelling stack with type KIND and assign VALUE
3848 to MEMBER. */
3850 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3852 int depth = SPELLING_DEPTH (); \
3854 if (depth >= spelling_size) \
3856 spelling_size += 10; \
3857 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
3858 spelling_size); \
3859 RESTORE_SPELLING_DEPTH (depth); \
3862 spelling->kind = (KIND); \
3863 spelling->MEMBER = (VALUE); \
3864 spelling++; \
3867 /* Push STRING on the stack. Printed literally. */
3869 static void
3870 push_string (const char *string)
3872 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3875 /* Push a member name on the stack. Printed as '.' STRING. */
3877 static void
3878 push_member_name (tree decl)
3880 const char *const string
3881 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3882 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3885 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3887 static void
3888 push_array_bounds (int bounds)
3890 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3893 /* Compute the maximum size in bytes of the printed spelling. */
3895 static int
3896 spelling_length (void)
3898 int size = 0;
3899 struct spelling *p;
3901 for (p = spelling_base; p < spelling; p++)
3903 if (p->kind == SPELLING_BOUNDS)
3904 size += 25;
3905 else
3906 size += strlen (p->u.s) + 1;
3909 return size;
3912 /* Print the spelling to BUFFER and return it. */
3914 static char *
3915 print_spelling (char *buffer)
3917 char *d = buffer;
3918 struct spelling *p;
3920 for (p = spelling_base; p < spelling; p++)
3921 if (p->kind == SPELLING_BOUNDS)
3923 sprintf (d, "[%d]", p->u.i);
3924 d += strlen (d);
3926 else
3928 const char *s;
3929 if (p->kind == SPELLING_MEMBER)
3930 *d++ = '.';
3931 for (s = p->u.s; (*d = *s++); d++)
3934 *d++ = '\0';
3935 return buffer;
3938 /* Issue an error message for a bad initializer component.
3939 MSGID identifies the message.
3940 The component name is taken from the spelling stack. */
3942 void
3943 error_init (const char *msgid)
3945 char *ofwhat;
3947 error ("%s", _(msgid));
3948 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3949 if (*ofwhat)
3950 error ("(near initialization for %qs)", ofwhat);
3953 /* Issue a pedantic warning for a bad initializer component.
3954 MSGID identifies the message.
3955 The component name is taken from the spelling stack. */
3957 void
3958 pedwarn_init (const char *msgid)
3960 char *ofwhat;
3962 pedwarn ("%s", _(msgid));
3963 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3964 if (*ofwhat)
3965 pedwarn ("(near initialization for %qs)", ofwhat);
3968 /* Issue a warning for a bad initializer component.
3969 MSGID identifies the message.
3970 The component name is taken from the spelling stack. */
3972 static void
3973 warning_init (const char *msgid)
3975 char *ofwhat;
3977 warning ("%s", _(msgid));
3978 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3979 if (*ofwhat)
3980 warning ("(near initialization for %qs)", ofwhat);
3983 /* If TYPE is an array type and EXPR is a parenthesized string
3984 constant, warn if pedantic that EXPR is being used to initialize an
3985 object of type TYPE. */
3987 void
3988 maybe_warn_string_init (tree type, struct c_expr expr)
3990 if (pedantic
3991 && TREE_CODE (type) == ARRAY_TYPE
3992 && TREE_CODE (expr.value) == STRING_CST
3993 && expr.original_code != STRING_CST)
3994 pedwarn_init ("array initialized from parenthesized string constant");
3997 /* Digest the parser output INIT as an initializer for type TYPE.
3998 Return a C expression of type TYPE to represent the initial value.
4000 If INIT is a string constant, STRICT_STRING is true if it is
4001 unparenthesized or we should not warn here for it being parenthesized.
4002 For other types of INIT, STRICT_STRING is not used.
4004 REQUIRE_CONSTANT requests an error if non-constant initializers or
4005 elements are seen. */
4007 static tree
4008 digest_init (tree type, tree init, bool strict_string, int require_constant)
4010 enum tree_code code = TREE_CODE (type);
4011 tree inside_init = init;
4013 if (type == error_mark_node
4014 || init == error_mark_node
4015 || TREE_TYPE (init) == error_mark_node)
4016 return error_mark_node;
4018 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4019 /* Do not use STRIP_NOPS here. We do not want an enumerator
4020 whose value is 0 to count as a null pointer constant. */
4021 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4022 inside_init = TREE_OPERAND (init, 0);
4024 inside_init = fold (inside_init);
4026 /* Initialization of an array of chars from a string constant
4027 optionally enclosed in braces. */
4029 if (code == ARRAY_TYPE && inside_init
4030 && TREE_CODE (inside_init) == STRING_CST)
4032 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4033 /* Note that an array could be both an array of character type
4034 and an array of wchar_t if wchar_t is signed char or unsigned
4035 char. */
4036 bool char_array = (typ1 == char_type_node
4037 || typ1 == signed_char_type_node
4038 || typ1 == unsigned_char_type_node);
4039 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4040 if (char_array || wchar_array)
4042 struct c_expr expr;
4043 bool char_string;
4044 expr.value = inside_init;
4045 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4046 maybe_warn_string_init (type, expr);
4048 char_string
4049 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4050 == char_type_node);
4052 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4053 TYPE_MAIN_VARIANT (type)))
4054 return inside_init;
4056 if (!wchar_array && !char_string)
4058 error_init ("char-array initialized from wide string");
4059 return error_mark_node;
4061 if (char_string && !char_array)
4063 error_init ("wchar_t-array initialized from non-wide string");
4064 return error_mark_node;
4067 TREE_TYPE (inside_init) = type;
4068 if (TYPE_DOMAIN (type) != 0
4069 && TYPE_SIZE (type) != 0
4070 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4071 /* Subtract 1 (or sizeof (wchar_t))
4072 because it's ok to ignore the terminating null char
4073 that is counted in the length of the constant. */
4074 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4075 TREE_STRING_LENGTH (inside_init)
4076 - ((TYPE_PRECISION (typ1)
4077 != TYPE_PRECISION (char_type_node))
4078 ? (TYPE_PRECISION (wchar_type_node)
4079 / BITS_PER_UNIT)
4080 : 1)))
4081 pedwarn_init ("initializer-string for array of chars is too long");
4083 return inside_init;
4085 else if (INTEGRAL_TYPE_P (typ1))
4087 error_init ("array of inappropriate type initialized "
4088 "from string constant");
4089 return error_mark_node;
4093 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4094 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4095 below and handle as a constructor. */
4096 if (code == VECTOR_TYPE
4097 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4098 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4099 && TREE_CONSTANT (inside_init))
4101 if (TREE_CODE (inside_init) == VECTOR_CST
4102 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4103 TYPE_MAIN_VARIANT (type)))
4104 return inside_init;
4105 else
4106 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4109 /* Any type can be initialized
4110 from an expression of the same type, optionally with braces. */
4112 if (inside_init && TREE_TYPE (inside_init) != 0
4113 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4114 TYPE_MAIN_VARIANT (type))
4115 || (code == ARRAY_TYPE
4116 && comptypes (TREE_TYPE (inside_init), type))
4117 || (code == VECTOR_TYPE
4118 && comptypes (TREE_TYPE (inside_init), type))
4119 || (code == POINTER_TYPE
4120 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4121 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4122 TREE_TYPE (type)))
4123 || (code == POINTER_TYPE
4124 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4125 && comptypes (TREE_TYPE (inside_init),
4126 TREE_TYPE (type)))))
4128 if (code == POINTER_TYPE)
4130 inside_init = default_function_array_conversion (inside_init);
4132 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4134 error_init ("invalid use of non-lvalue array");
4135 return error_mark_node;
4139 if (code == VECTOR_TYPE)
4140 /* Although the types are compatible, we may require a
4141 conversion. */
4142 inside_init = convert (type, inside_init);
4144 if (require_constant && !flag_isoc99
4145 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4147 /* As an extension, allow initializing objects with static storage
4148 duration with compound literals (which are then treated just as
4149 the brace enclosed list they contain). */
4150 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4151 inside_init = DECL_INITIAL (decl);
4154 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4155 && TREE_CODE (inside_init) != CONSTRUCTOR)
4157 error_init ("array initialized from non-constant array expression");
4158 return error_mark_node;
4161 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4162 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4164 /* Compound expressions can only occur here if -pedantic or
4165 -pedantic-errors is specified. In the later case, we always want
4166 an error. In the former case, we simply want a warning. */
4167 if (require_constant && pedantic
4168 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4170 inside_init
4171 = valid_compound_expr_initializer (inside_init,
4172 TREE_TYPE (inside_init));
4173 if (inside_init == error_mark_node)
4174 error_init ("initializer element is not constant");
4175 else
4176 pedwarn_init ("initializer element is not constant");
4177 if (flag_pedantic_errors)
4178 inside_init = error_mark_node;
4180 else if (require_constant
4181 && !initializer_constant_valid_p (inside_init,
4182 TREE_TYPE (inside_init)))
4184 error_init ("initializer element is not constant");
4185 inside_init = error_mark_node;
4188 return inside_init;
4191 /* Handle scalar types, including conversions. */
4193 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4194 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4195 || code == VECTOR_TYPE)
4197 /* Note that convert_for_assignment calls default_conversion
4198 for arrays and functions. We must not call it in the
4199 case where inside_init is a null pointer constant. */
4200 inside_init
4201 = convert_for_assignment (type, init, _("initialization"),
4202 NULL_TREE, NULL_TREE, 0);
4204 /* Check to see if we have already given an error message. */
4205 if (inside_init == error_mark_node)
4207 else if (require_constant && ! TREE_CONSTANT (inside_init))
4209 error_init ("initializer element is not constant");
4210 inside_init = error_mark_node;
4212 else if (require_constant
4213 && !initializer_constant_valid_p (inside_init,
4214 TREE_TYPE (inside_init)))
4216 error_init ("initializer element is not computable at load time");
4217 inside_init = error_mark_node;
4220 return inside_init;
4223 /* Come here only for records and arrays. */
4225 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4227 error_init ("variable-sized object may not be initialized");
4228 return error_mark_node;
4231 error_init ("invalid initializer");
4232 return error_mark_node;
4235 /* Handle initializers that use braces. */
4237 /* Type of object we are accumulating a constructor for.
4238 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4239 static tree constructor_type;
4241 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4242 left to fill. */
4243 static tree constructor_fields;
4245 /* For an ARRAY_TYPE, this is the specified index
4246 at which to store the next element we get. */
4247 static tree constructor_index;
4249 /* For an ARRAY_TYPE, this is the maximum index. */
4250 static tree constructor_max_index;
4252 /* For a RECORD_TYPE, this is the first field not yet written out. */
4253 static tree constructor_unfilled_fields;
4255 /* For an ARRAY_TYPE, this is the index of the first element
4256 not yet written out. */
4257 static tree constructor_unfilled_index;
4259 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4260 This is so we can generate gaps between fields, when appropriate. */
4261 static tree constructor_bit_index;
4263 /* If we are saving up the elements rather than allocating them,
4264 this is the list of elements so far (in reverse order,
4265 most recent first). */
4266 static tree constructor_elements;
4268 /* 1 if constructor should be incrementally stored into a constructor chain,
4269 0 if all the elements should be kept in AVL tree. */
4270 static int constructor_incremental;
4272 /* 1 if so far this constructor's elements are all compile-time constants. */
4273 static int constructor_constant;
4275 /* 1 if so far this constructor's elements are all valid address constants. */
4276 static int constructor_simple;
4278 /* 1 if this constructor is erroneous so far. */
4279 static int constructor_erroneous;
4281 /* Structure for managing pending initializer elements, organized as an
4282 AVL tree. */
4284 struct init_node
4286 struct init_node *left, *right;
4287 struct init_node *parent;
4288 int balance;
4289 tree purpose;
4290 tree value;
4293 /* Tree of pending elements at this constructor level.
4294 These are elements encountered out of order
4295 which belong at places we haven't reached yet in actually
4296 writing the output.
4297 Will never hold tree nodes across GC runs. */
4298 static struct init_node *constructor_pending_elts;
4300 /* The SPELLING_DEPTH of this constructor. */
4301 static int constructor_depth;
4303 /* 0 if implicitly pushing constructor levels is allowed. */
4304 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4306 /* DECL node for which an initializer is being read.
4307 0 means we are reading a constructor expression
4308 such as (struct foo) {...}. */
4309 static tree constructor_decl;
4311 /* Nonzero if this is an initializer for a top-level decl. */
4312 static int constructor_top_level;
4314 /* Nonzero if there were any member designators in this initializer. */
4315 static int constructor_designated;
4317 /* Nesting depth of designator list. */
4318 static int designator_depth;
4320 /* Nonzero if there were diagnosed errors in this designator list. */
4321 static int designator_errorneous;
4324 /* This stack has a level for each implicit or explicit level of
4325 structuring in the initializer, including the outermost one. It
4326 saves the values of most of the variables above. */
4328 struct constructor_range_stack;
4330 struct constructor_stack
4332 struct constructor_stack *next;
4333 tree type;
4334 tree fields;
4335 tree index;
4336 tree max_index;
4337 tree unfilled_index;
4338 tree unfilled_fields;
4339 tree bit_index;
4340 tree elements;
4341 struct init_node *pending_elts;
4342 int offset;
4343 int depth;
4344 /* If value nonzero, this value should replace the entire
4345 constructor at this level. */
4346 struct c_expr replacement_value;
4347 struct constructor_range_stack *range_stack;
4348 char constant;
4349 char simple;
4350 char implicit;
4351 char erroneous;
4352 char outer;
4353 char incremental;
4354 char designated;
4357 struct constructor_stack *constructor_stack;
4359 /* This stack represents designators from some range designator up to
4360 the last designator in the list. */
4362 struct constructor_range_stack
4364 struct constructor_range_stack *next, *prev;
4365 struct constructor_stack *stack;
4366 tree range_start;
4367 tree index;
4368 tree range_end;
4369 tree fields;
4372 struct constructor_range_stack *constructor_range_stack;
4374 /* This stack records separate initializers that are nested.
4375 Nested initializers can't happen in ANSI C, but GNU C allows them
4376 in cases like { ... (struct foo) { ... } ... }. */
4378 struct initializer_stack
4380 struct initializer_stack *next;
4381 tree decl;
4382 struct constructor_stack *constructor_stack;
4383 struct constructor_range_stack *constructor_range_stack;
4384 tree elements;
4385 struct spelling *spelling;
4386 struct spelling *spelling_base;
4387 int spelling_size;
4388 char top_level;
4389 char require_constant_value;
4390 char require_constant_elements;
4393 struct initializer_stack *initializer_stack;
4395 /* Prepare to parse and output the initializer for variable DECL. */
4397 void
4398 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4400 const char *locus;
4401 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4403 p->decl = constructor_decl;
4404 p->require_constant_value = require_constant_value;
4405 p->require_constant_elements = require_constant_elements;
4406 p->constructor_stack = constructor_stack;
4407 p->constructor_range_stack = constructor_range_stack;
4408 p->elements = constructor_elements;
4409 p->spelling = spelling;
4410 p->spelling_base = spelling_base;
4411 p->spelling_size = spelling_size;
4412 p->top_level = constructor_top_level;
4413 p->next = initializer_stack;
4414 initializer_stack = p;
4416 constructor_decl = decl;
4417 constructor_designated = 0;
4418 constructor_top_level = top_level;
4420 if (decl != 0)
4422 require_constant_value = TREE_STATIC (decl);
4423 require_constant_elements
4424 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4425 /* For a scalar, you can always use any value to initialize,
4426 even within braces. */
4427 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4428 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4429 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4430 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4431 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4433 else
4435 require_constant_value = 0;
4436 require_constant_elements = 0;
4437 locus = "(anonymous)";
4440 constructor_stack = 0;
4441 constructor_range_stack = 0;
4443 missing_braces_mentioned = 0;
4445 spelling_base = 0;
4446 spelling_size = 0;
4447 RESTORE_SPELLING_DEPTH (0);
4449 if (locus)
4450 push_string (locus);
4453 void
4454 finish_init (void)
4456 struct initializer_stack *p = initializer_stack;
4458 /* Free the whole constructor stack of this initializer. */
4459 while (constructor_stack)
4461 struct constructor_stack *q = constructor_stack;
4462 constructor_stack = q->next;
4463 free (q);
4466 gcc_assert (!constructor_range_stack);
4468 /* Pop back to the data of the outer initializer (if any). */
4469 free (spelling_base);
4471 constructor_decl = p->decl;
4472 require_constant_value = p->require_constant_value;
4473 require_constant_elements = p->require_constant_elements;
4474 constructor_stack = p->constructor_stack;
4475 constructor_range_stack = p->constructor_range_stack;
4476 constructor_elements = p->elements;
4477 spelling = p->spelling;
4478 spelling_base = p->spelling_base;
4479 spelling_size = p->spelling_size;
4480 constructor_top_level = p->top_level;
4481 initializer_stack = p->next;
4482 free (p);
4485 /* Call here when we see the initializer is surrounded by braces.
4486 This is instead of a call to push_init_level;
4487 it is matched by a call to pop_init_level.
4489 TYPE is the type to initialize, for a constructor expression.
4490 For an initializer for a decl, TYPE is zero. */
4492 void
4493 really_start_incremental_init (tree type)
4495 struct constructor_stack *p = XNEW (struct constructor_stack);
4497 if (type == 0)
4498 type = TREE_TYPE (constructor_decl);
4500 if (targetm.vector_opaque_p (type))
4501 error ("opaque vector types cannot be initialized");
4503 p->type = constructor_type;
4504 p->fields = constructor_fields;
4505 p->index = constructor_index;
4506 p->max_index = constructor_max_index;
4507 p->unfilled_index = constructor_unfilled_index;
4508 p->unfilled_fields = constructor_unfilled_fields;
4509 p->bit_index = constructor_bit_index;
4510 p->elements = constructor_elements;
4511 p->constant = constructor_constant;
4512 p->simple = constructor_simple;
4513 p->erroneous = constructor_erroneous;
4514 p->pending_elts = constructor_pending_elts;
4515 p->depth = constructor_depth;
4516 p->replacement_value.value = 0;
4517 p->replacement_value.original_code = ERROR_MARK;
4518 p->implicit = 0;
4519 p->range_stack = 0;
4520 p->outer = 0;
4521 p->incremental = constructor_incremental;
4522 p->designated = constructor_designated;
4523 p->next = 0;
4524 constructor_stack = p;
4526 constructor_constant = 1;
4527 constructor_simple = 1;
4528 constructor_depth = SPELLING_DEPTH ();
4529 constructor_elements = 0;
4530 constructor_pending_elts = 0;
4531 constructor_type = type;
4532 constructor_incremental = 1;
4533 constructor_designated = 0;
4534 designator_depth = 0;
4535 designator_errorneous = 0;
4537 if (TREE_CODE (constructor_type) == RECORD_TYPE
4538 || TREE_CODE (constructor_type) == UNION_TYPE)
4540 constructor_fields = TYPE_FIELDS (constructor_type);
4541 /* Skip any nameless bit fields at the beginning. */
4542 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4543 && DECL_NAME (constructor_fields) == 0)
4544 constructor_fields = TREE_CHAIN (constructor_fields);
4546 constructor_unfilled_fields = constructor_fields;
4547 constructor_bit_index = bitsize_zero_node;
4549 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4551 if (TYPE_DOMAIN (constructor_type))
4553 constructor_max_index
4554 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4556 /* Detect non-empty initializations of zero-length arrays. */
4557 if (constructor_max_index == NULL_TREE
4558 && TYPE_SIZE (constructor_type))
4559 constructor_max_index = build_int_cst (NULL_TREE, -1);
4561 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4562 to initialize VLAs will cause a proper error; avoid tree
4563 checking errors as well by setting a safe value. */
4564 if (constructor_max_index
4565 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4566 constructor_max_index = build_int_cst (NULL_TREE, -1);
4568 constructor_index
4569 = convert (bitsizetype,
4570 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4572 else
4573 constructor_index = bitsize_zero_node;
4575 constructor_unfilled_index = constructor_index;
4577 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4579 /* Vectors are like simple fixed-size arrays. */
4580 constructor_max_index =
4581 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4582 constructor_index = convert (bitsizetype, bitsize_zero_node);
4583 constructor_unfilled_index = constructor_index;
4585 else
4587 /* Handle the case of int x = {5}; */
4588 constructor_fields = constructor_type;
4589 constructor_unfilled_fields = constructor_type;
4593 /* Push down into a subobject, for initialization.
4594 If this is for an explicit set of braces, IMPLICIT is 0.
4595 If it is because the next element belongs at a lower level,
4596 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4598 void
4599 push_init_level (int implicit)
4601 struct constructor_stack *p;
4602 tree value = NULL_TREE;
4604 /* If we've exhausted any levels that didn't have braces,
4605 pop them now. */
4606 while (constructor_stack->implicit)
4608 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4609 || TREE_CODE (constructor_type) == UNION_TYPE)
4610 && constructor_fields == 0)
4611 process_init_element (pop_init_level (1));
4612 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4613 && constructor_max_index
4614 && tree_int_cst_lt (constructor_max_index, constructor_index))
4615 process_init_element (pop_init_level (1));
4616 else
4617 break;
4620 /* Unless this is an explicit brace, we need to preserve previous
4621 content if any. */
4622 if (implicit)
4624 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4625 || TREE_CODE (constructor_type) == UNION_TYPE)
4626 && constructor_fields)
4627 value = find_init_member (constructor_fields);
4628 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4629 value = find_init_member (constructor_index);
4632 p = XNEW (struct constructor_stack);
4633 p->type = constructor_type;
4634 p->fields = constructor_fields;
4635 p->index = constructor_index;
4636 p->max_index = constructor_max_index;
4637 p->unfilled_index = constructor_unfilled_index;
4638 p->unfilled_fields = constructor_unfilled_fields;
4639 p->bit_index = constructor_bit_index;
4640 p->elements = constructor_elements;
4641 p->constant = constructor_constant;
4642 p->simple = constructor_simple;
4643 p->erroneous = constructor_erroneous;
4644 p->pending_elts = constructor_pending_elts;
4645 p->depth = constructor_depth;
4646 p->replacement_value.value = 0;
4647 p->replacement_value.original_code = ERROR_MARK;
4648 p->implicit = implicit;
4649 p->outer = 0;
4650 p->incremental = constructor_incremental;
4651 p->designated = constructor_designated;
4652 p->next = constructor_stack;
4653 p->range_stack = 0;
4654 constructor_stack = p;
4656 constructor_constant = 1;
4657 constructor_simple = 1;
4658 constructor_depth = SPELLING_DEPTH ();
4659 constructor_elements = 0;
4660 constructor_incremental = 1;
4661 constructor_designated = 0;
4662 constructor_pending_elts = 0;
4663 if (!implicit)
4665 p->range_stack = constructor_range_stack;
4666 constructor_range_stack = 0;
4667 designator_depth = 0;
4668 designator_errorneous = 0;
4671 /* Don't die if an entire brace-pair level is superfluous
4672 in the containing level. */
4673 if (constructor_type == 0)
4675 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4676 || TREE_CODE (constructor_type) == UNION_TYPE)
4678 /* Don't die if there are extra init elts at the end. */
4679 if (constructor_fields == 0)
4680 constructor_type = 0;
4681 else
4683 constructor_type = TREE_TYPE (constructor_fields);
4684 push_member_name (constructor_fields);
4685 constructor_depth++;
4688 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4690 constructor_type = TREE_TYPE (constructor_type);
4691 push_array_bounds (tree_low_cst (constructor_index, 0));
4692 constructor_depth++;
4695 if (constructor_type == 0)
4697 error_init ("extra brace group at end of initializer");
4698 constructor_fields = 0;
4699 constructor_unfilled_fields = 0;
4700 return;
4703 if (value && TREE_CODE (value) == CONSTRUCTOR)
4705 constructor_constant = TREE_CONSTANT (value);
4706 constructor_simple = TREE_STATIC (value);
4707 constructor_elements = CONSTRUCTOR_ELTS (value);
4708 if (constructor_elements
4709 && (TREE_CODE (constructor_type) == RECORD_TYPE
4710 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4711 set_nonincremental_init ();
4714 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4716 missing_braces_mentioned = 1;
4717 warning_init ("missing braces around initializer");
4720 if (TREE_CODE (constructor_type) == RECORD_TYPE
4721 || TREE_CODE (constructor_type) == UNION_TYPE)
4723 constructor_fields = TYPE_FIELDS (constructor_type);
4724 /* Skip any nameless bit fields at the beginning. */
4725 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4726 && DECL_NAME (constructor_fields) == 0)
4727 constructor_fields = TREE_CHAIN (constructor_fields);
4729 constructor_unfilled_fields = constructor_fields;
4730 constructor_bit_index = bitsize_zero_node;
4732 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4734 /* Vectors are like simple fixed-size arrays. */
4735 constructor_max_index =
4736 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4737 constructor_index = convert (bitsizetype, integer_zero_node);
4738 constructor_unfilled_index = constructor_index;
4740 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4742 if (TYPE_DOMAIN (constructor_type))
4744 constructor_max_index
4745 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4747 /* Detect non-empty initializations of zero-length arrays. */
4748 if (constructor_max_index == NULL_TREE
4749 && TYPE_SIZE (constructor_type))
4750 constructor_max_index = build_int_cst (NULL_TREE, -1);
4752 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4753 to initialize VLAs will cause a proper error; avoid tree
4754 checking errors as well by setting a safe value. */
4755 if (constructor_max_index
4756 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4757 constructor_max_index = build_int_cst (NULL_TREE, -1);
4759 constructor_index
4760 = convert (bitsizetype,
4761 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4763 else
4764 constructor_index = bitsize_zero_node;
4766 constructor_unfilled_index = constructor_index;
4767 if (value && TREE_CODE (value) == STRING_CST)
4769 /* We need to split the char/wchar array into individual
4770 characters, so that we don't have to special case it
4771 everywhere. */
4772 set_nonincremental_init_from_string (value);
4775 else
4777 warning_init ("braces around scalar initializer");
4778 constructor_fields = constructor_type;
4779 constructor_unfilled_fields = constructor_type;
4783 /* At the end of an implicit or explicit brace level,
4784 finish up that level of constructor. If a single expression
4785 with redundant braces initialized that level, return the
4786 c_expr structure for that expression. Otherwise, the original_code
4787 element is set to ERROR_MARK.
4788 If we were outputting the elements as they are read, return 0 as the value
4789 from inner levels (process_init_element ignores that),
4790 but return error_mark_node as the value from the outermost level
4791 (that's what we want to put in DECL_INITIAL).
4792 Otherwise, return a CONSTRUCTOR expression as the value. */
4794 struct c_expr
4795 pop_init_level (int implicit)
4797 struct constructor_stack *p;
4798 struct c_expr ret;
4799 ret.value = 0;
4800 ret.original_code = ERROR_MARK;
4802 if (implicit == 0)
4804 /* When we come to an explicit close brace,
4805 pop any inner levels that didn't have explicit braces. */
4806 while (constructor_stack->implicit)
4807 process_init_element (pop_init_level (1));
4809 gcc_assert (!constructor_range_stack);
4812 /* Now output all pending elements. */
4813 constructor_incremental = 1;
4814 output_pending_init_elements (1);
4816 p = constructor_stack;
4818 /* Error for initializing a flexible array member, or a zero-length
4819 array member in an inappropriate context. */
4820 if (constructor_type && constructor_fields
4821 && TREE_CODE (constructor_type) == ARRAY_TYPE
4822 && TYPE_DOMAIN (constructor_type)
4823 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4825 /* Silently discard empty initializations. The parser will
4826 already have pedwarned for empty brackets. */
4827 if (integer_zerop (constructor_unfilled_index))
4828 constructor_type = NULL_TREE;
4829 else
4831 gcc_assert (!TYPE_SIZE (constructor_type));
4833 if (constructor_depth > 2)
4834 error_init ("initialization of flexible array member in a nested context");
4835 else if (pedantic)
4836 pedwarn_init ("initialization of a flexible array member");
4838 /* We have already issued an error message for the existence
4839 of a flexible array member not at the end of the structure.
4840 Discard the initializer so that we do not abort later. */
4841 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4842 constructor_type = NULL_TREE;
4846 /* Warn when some struct elements are implicitly initialized to zero. */
4847 if (warn_missing_field_initializers
4848 && constructor_type
4849 && TREE_CODE (constructor_type) == RECORD_TYPE
4850 && constructor_unfilled_fields)
4852 /* Do not warn for flexible array members or zero-length arrays. */
4853 while (constructor_unfilled_fields
4854 && (! DECL_SIZE (constructor_unfilled_fields)
4855 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4856 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4858 /* Do not warn if this level of the initializer uses member
4859 designators; it is likely to be deliberate. */
4860 if (constructor_unfilled_fields && !constructor_designated)
4862 push_member_name (constructor_unfilled_fields);
4863 warning_init ("missing initializer");
4864 RESTORE_SPELLING_DEPTH (constructor_depth);
4868 /* Pad out the end of the structure. */
4869 if (p->replacement_value.value)
4870 /* If this closes a superfluous brace pair,
4871 just pass out the element between them. */
4872 ret = p->replacement_value;
4873 else if (constructor_type == 0)
4875 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4876 && TREE_CODE (constructor_type) != UNION_TYPE
4877 && TREE_CODE (constructor_type) != ARRAY_TYPE
4878 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4880 /* A nonincremental scalar initializer--just return
4881 the element, after verifying there is just one. */
4882 if (constructor_elements == 0)
4884 if (!constructor_erroneous)
4885 error_init ("empty scalar initializer");
4886 ret.value = error_mark_node;
4888 else if (TREE_CHAIN (constructor_elements) != 0)
4890 error_init ("extra elements in scalar initializer");
4891 ret.value = TREE_VALUE (constructor_elements);
4893 else
4894 ret.value = TREE_VALUE (constructor_elements);
4896 else
4898 if (constructor_erroneous)
4899 ret.value = error_mark_node;
4900 else
4902 ret.value = build_constructor (constructor_type,
4903 nreverse (constructor_elements));
4904 if (constructor_constant)
4905 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
4906 if (constructor_constant && constructor_simple)
4907 TREE_STATIC (ret.value) = 1;
4911 constructor_type = p->type;
4912 constructor_fields = p->fields;
4913 constructor_index = p->index;
4914 constructor_max_index = p->max_index;
4915 constructor_unfilled_index = p->unfilled_index;
4916 constructor_unfilled_fields = p->unfilled_fields;
4917 constructor_bit_index = p->bit_index;
4918 constructor_elements = p->elements;
4919 constructor_constant = p->constant;
4920 constructor_simple = p->simple;
4921 constructor_erroneous = p->erroneous;
4922 constructor_incremental = p->incremental;
4923 constructor_designated = p->designated;
4924 constructor_pending_elts = p->pending_elts;
4925 constructor_depth = p->depth;
4926 if (!p->implicit)
4927 constructor_range_stack = p->range_stack;
4928 RESTORE_SPELLING_DEPTH (constructor_depth);
4930 constructor_stack = p->next;
4931 free (p);
4933 if (ret.value == 0)
4935 if (constructor_stack == 0)
4937 ret.value = error_mark_node;
4938 return ret;
4940 return ret;
4942 return ret;
4945 /* Common handling for both array range and field name designators.
4946 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4948 static int
4949 set_designator (int array)
4951 tree subtype;
4952 enum tree_code subcode;
4954 /* Don't die if an entire brace-pair level is superfluous
4955 in the containing level. */
4956 if (constructor_type == 0)
4957 return 1;
4959 /* If there were errors in this designator list already, bail out
4960 silently. */
4961 if (designator_errorneous)
4962 return 1;
4964 if (!designator_depth)
4966 gcc_assert (!constructor_range_stack);
4968 /* Designator list starts at the level of closest explicit
4969 braces. */
4970 while (constructor_stack->implicit)
4971 process_init_element (pop_init_level (1));
4972 constructor_designated = 1;
4973 return 0;
4976 if (constructor_no_implicit)
4978 error_init ("initialization designators may not nest");
4979 return 1;
4982 switch (TREE_CODE (constructor_type))
4984 case RECORD_TYPE:
4985 case UNION_TYPE:
4986 subtype = TREE_TYPE (constructor_fields);
4987 if (subtype != error_mark_node)
4988 subtype = TYPE_MAIN_VARIANT (subtype);
4989 break;
4990 case ARRAY_TYPE:
4991 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4992 break;
4993 default:
4994 gcc_unreachable ();
4997 subcode = TREE_CODE (subtype);
4998 if (array && subcode != ARRAY_TYPE)
5000 error_init ("array index in non-array initializer");
5001 return 1;
5003 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5005 error_init ("field name not in record or union initializer");
5006 return 1;
5009 constructor_designated = 1;
5010 push_init_level (2);
5011 return 0;
5014 /* If there are range designators in designator list, push a new designator
5015 to constructor_range_stack. RANGE_END is end of such stack range or
5016 NULL_TREE if there is no range designator at this level. */
5018 static void
5019 push_range_stack (tree range_end)
5021 struct constructor_range_stack *p;
5023 p = GGC_NEW (struct constructor_range_stack);
5024 p->prev = constructor_range_stack;
5025 p->next = 0;
5026 p->fields = constructor_fields;
5027 p->range_start = constructor_index;
5028 p->index = constructor_index;
5029 p->stack = constructor_stack;
5030 p->range_end = range_end;
5031 if (constructor_range_stack)
5032 constructor_range_stack->next = p;
5033 constructor_range_stack = p;
5036 /* Within an array initializer, specify the next index to be initialized.
5037 FIRST is that index. If LAST is nonzero, then initialize a range
5038 of indices, running from FIRST through LAST. */
5040 void
5041 set_init_index (tree first, tree last)
5043 if (set_designator (1))
5044 return;
5046 designator_errorneous = 1;
5048 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5049 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5051 error_init ("array index in initializer not of integer type");
5052 return;
5055 while ((TREE_CODE (first) == NOP_EXPR
5056 || TREE_CODE (first) == CONVERT_EXPR
5057 || TREE_CODE (first) == NON_LVALUE_EXPR)
5058 && (TYPE_MODE (TREE_TYPE (first))
5059 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5060 first = TREE_OPERAND (first, 0);
5062 if (last)
5063 while ((TREE_CODE (last) == NOP_EXPR
5064 || TREE_CODE (last) == CONVERT_EXPR
5065 || TREE_CODE (last) == NON_LVALUE_EXPR)
5066 && (TYPE_MODE (TREE_TYPE (last))
5067 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5068 last = TREE_OPERAND (last, 0);
5070 if (TREE_CODE (first) != INTEGER_CST)
5071 error_init ("nonconstant array index in initializer");
5072 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5073 error_init ("nonconstant array index in initializer");
5074 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5075 error_init ("array index in non-array initializer");
5076 else if (tree_int_cst_sgn (first) == -1)
5077 error_init ("array index in initializer exceeds array bounds");
5078 else if (constructor_max_index
5079 && tree_int_cst_lt (constructor_max_index, first))
5080 error_init ("array index in initializer exceeds array bounds");
5081 else
5083 constructor_index = convert (bitsizetype, first);
5085 if (last)
5087 if (tree_int_cst_equal (first, last))
5088 last = 0;
5089 else if (tree_int_cst_lt (last, first))
5091 error_init ("empty index range in initializer");
5092 last = 0;
5094 else
5096 last = convert (bitsizetype, last);
5097 if (constructor_max_index != 0
5098 && tree_int_cst_lt (constructor_max_index, last))
5100 error_init ("array index range in initializer exceeds array bounds");
5101 last = 0;
5106 designator_depth++;
5107 designator_errorneous = 0;
5108 if (constructor_range_stack || last)
5109 push_range_stack (last);
5113 /* Within a struct initializer, specify the next field to be initialized. */
5115 void
5116 set_init_label (tree fieldname)
5118 tree tail;
5120 if (set_designator (0))
5121 return;
5123 designator_errorneous = 1;
5125 if (TREE_CODE (constructor_type) != RECORD_TYPE
5126 && TREE_CODE (constructor_type) != UNION_TYPE)
5128 error_init ("field name not in record or union initializer");
5129 return;
5132 for (tail = TYPE_FIELDS (constructor_type); tail;
5133 tail = TREE_CHAIN (tail))
5135 if (DECL_NAME (tail) == fieldname)
5136 break;
5139 if (tail == 0)
5140 error ("unknown field %qs specified in initializer",
5141 IDENTIFIER_POINTER (fieldname));
5142 else
5144 constructor_fields = tail;
5145 designator_depth++;
5146 designator_errorneous = 0;
5147 if (constructor_range_stack)
5148 push_range_stack (NULL_TREE);
5152 /* Add a new initializer to the tree of pending initializers. PURPOSE
5153 identifies the initializer, either array index or field in a structure.
5154 VALUE is the value of that index or field. */
5156 static void
5157 add_pending_init (tree purpose, tree value)
5159 struct init_node *p, **q, *r;
5161 q = &constructor_pending_elts;
5162 p = 0;
5164 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5166 while (*q != 0)
5168 p = *q;
5169 if (tree_int_cst_lt (purpose, p->purpose))
5170 q = &p->left;
5171 else if (tree_int_cst_lt (p->purpose, purpose))
5172 q = &p->right;
5173 else
5175 if (TREE_SIDE_EFFECTS (p->value))
5176 warning_init ("initialized field with side-effects overwritten");
5177 p->value = value;
5178 return;
5182 else
5184 tree bitpos;
5186 bitpos = bit_position (purpose);
5187 while (*q != NULL)
5189 p = *q;
5190 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5191 q = &p->left;
5192 else if (p->purpose != purpose)
5193 q = &p->right;
5194 else
5196 if (TREE_SIDE_EFFECTS (p->value))
5197 warning_init ("initialized field with side-effects overwritten");
5198 p->value = value;
5199 return;
5204 r = GGC_NEW (struct init_node);
5205 r->purpose = purpose;
5206 r->value = value;
5208 *q = r;
5209 r->parent = p;
5210 r->left = 0;
5211 r->right = 0;
5212 r->balance = 0;
5214 while (p)
5216 struct init_node *s;
5218 if (r == p->left)
5220 if (p->balance == 0)
5221 p->balance = -1;
5222 else if (p->balance < 0)
5224 if (r->balance < 0)
5226 /* L rotation. */
5227 p->left = r->right;
5228 if (p->left)
5229 p->left->parent = p;
5230 r->right = p;
5232 p->balance = 0;
5233 r->balance = 0;
5235 s = p->parent;
5236 p->parent = r;
5237 r->parent = s;
5238 if (s)
5240 if (s->left == p)
5241 s->left = r;
5242 else
5243 s->right = r;
5245 else
5246 constructor_pending_elts = r;
5248 else
5250 /* LR rotation. */
5251 struct init_node *t = r->right;
5253 r->right = t->left;
5254 if (r->right)
5255 r->right->parent = r;
5256 t->left = r;
5258 p->left = t->right;
5259 if (p->left)
5260 p->left->parent = p;
5261 t->right = p;
5263 p->balance = t->balance < 0;
5264 r->balance = -(t->balance > 0);
5265 t->balance = 0;
5267 s = p->parent;
5268 p->parent = t;
5269 r->parent = t;
5270 t->parent = s;
5271 if (s)
5273 if (s->left == p)
5274 s->left = t;
5275 else
5276 s->right = t;
5278 else
5279 constructor_pending_elts = t;
5281 break;
5283 else
5285 /* p->balance == +1; growth of left side balances the node. */
5286 p->balance = 0;
5287 break;
5290 else /* r == p->right */
5292 if (p->balance == 0)
5293 /* Growth propagation from right side. */
5294 p->balance++;
5295 else if (p->balance > 0)
5297 if (r->balance > 0)
5299 /* R rotation. */
5300 p->right = r->left;
5301 if (p->right)
5302 p->right->parent = p;
5303 r->left = p;
5305 p->balance = 0;
5306 r->balance = 0;
5308 s = p->parent;
5309 p->parent = r;
5310 r->parent = s;
5311 if (s)
5313 if (s->left == p)
5314 s->left = r;
5315 else
5316 s->right = r;
5318 else
5319 constructor_pending_elts = r;
5321 else /* r->balance == -1 */
5323 /* RL rotation */
5324 struct init_node *t = r->left;
5326 r->left = t->right;
5327 if (r->left)
5328 r->left->parent = r;
5329 t->right = r;
5331 p->right = t->left;
5332 if (p->right)
5333 p->right->parent = p;
5334 t->left = p;
5336 r->balance = (t->balance < 0);
5337 p->balance = -(t->balance > 0);
5338 t->balance = 0;
5340 s = p->parent;
5341 p->parent = t;
5342 r->parent = t;
5343 t->parent = s;
5344 if (s)
5346 if (s->left == p)
5347 s->left = t;
5348 else
5349 s->right = t;
5351 else
5352 constructor_pending_elts = t;
5354 break;
5356 else
5358 /* p->balance == -1; growth of right side balances the node. */
5359 p->balance = 0;
5360 break;
5364 r = p;
5365 p = p->parent;
5369 /* Build AVL tree from a sorted chain. */
5371 static void
5372 set_nonincremental_init (void)
5374 tree chain;
5376 if (TREE_CODE (constructor_type) != RECORD_TYPE
5377 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5378 return;
5380 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5381 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5382 constructor_elements = 0;
5383 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5385 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5386 /* Skip any nameless bit fields at the beginning. */
5387 while (constructor_unfilled_fields != 0
5388 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5389 && DECL_NAME (constructor_unfilled_fields) == 0)
5390 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5393 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5395 if (TYPE_DOMAIN (constructor_type))
5396 constructor_unfilled_index
5397 = convert (bitsizetype,
5398 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5399 else
5400 constructor_unfilled_index = bitsize_zero_node;
5402 constructor_incremental = 0;
5405 /* Build AVL tree from a string constant. */
5407 static void
5408 set_nonincremental_init_from_string (tree str)
5410 tree value, purpose, type;
5411 HOST_WIDE_INT val[2];
5412 const char *p, *end;
5413 int byte, wchar_bytes, charwidth, bitpos;
5415 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5417 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5418 == TYPE_PRECISION (char_type_node))
5419 wchar_bytes = 1;
5420 else
5422 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5423 == TYPE_PRECISION (wchar_type_node));
5424 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5426 charwidth = TYPE_PRECISION (char_type_node);
5427 type = TREE_TYPE (constructor_type);
5428 p = TREE_STRING_POINTER (str);
5429 end = p + TREE_STRING_LENGTH (str);
5431 for (purpose = bitsize_zero_node;
5432 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5433 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5435 if (wchar_bytes == 1)
5437 val[1] = (unsigned char) *p++;
5438 val[0] = 0;
5440 else
5442 val[0] = 0;
5443 val[1] = 0;
5444 for (byte = 0; byte < wchar_bytes; byte++)
5446 if (BYTES_BIG_ENDIAN)
5447 bitpos = (wchar_bytes - byte - 1) * charwidth;
5448 else
5449 bitpos = byte * charwidth;
5450 val[bitpos < HOST_BITS_PER_WIDE_INT]
5451 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5452 << (bitpos % HOST_BITS_PER_WIDE_INT);
5456 if (!TYPE_UNSIGNED (type))
5458 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5459 if (bitpos < HOST_BITS_PER_WIDE_INT)
5461 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5463 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5464 val[0] = -1;
5467 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5469 if (val[1] < 0)
5470 val[0] = -1;
5472 else if (val[0] & (((HOST_WIDE_INT) 1)
5473 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5474 val[0] |= ((HOST_WIDE_INT) -1)
5475 << (bitpos - HOST_BITS_PER_WIDE_INT);
5478 value = build_int_cst_wide (type, val[1], val[0]);
5479 add_pending_init (purpose, value);
5482 constructor_incremental = 0;
5485 /* Return value of FIELD in pending initializer or zero if the field was
5486 not initialized yet. */
5488 static tree
5489 find_init_member (tree field)
5491 struct init_node *p;
5493 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5495 if (constructor_incremental
5496 && tree_int_cst_lt (field, constructor_unfilled_index))
5497 set_nonincremental_init ();
5499 p = constructor_pending_elts;
5500 while (p)
5502 if (tree_int_cst_lt (field, p->purpose))
5503 p = p->left;
5504 else if (tree_int_cst_lt (p->purpose, field))
5505 p = p->right;
5506 else
5507 return p->value;
5510 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5512 tree bitpos = bit_position (field);
5514 if (constructor_incremental
5515 && (!constructor_unfilled_fields
5516 || tree_int_cst_lt (bitpos,
5517 bit_position (constructor_unfilled_fields))))
5518 set_nonincremental_init ();
5520 p = constructor_pending_elts;
5521 while (p)
5523 if (field == p->purpose)
5524 return p->value;
5525 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5526 p = p->left;
5527 else
5528 p = p->right;
5531 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5533 if (constructor_elements
5534 && TREE_PURPOSE (constructor_elements) == field)
5535 return TREE_VALUE (constructor_elements);
5537 return 0;
5540 /* "Output" the next constructor element.
5541 At top level, really output it to assembler code now.
5542 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5543 TYPE is the data type that the containing data type wants here.
5544 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5545 If VALUE is a string constant, STRICT_STRING is true if it is
5546 unparenthesized or we should not warn here for it being parenthesized.
5547 For other types of VALUE, STRICT_STRING is not used.
5549 PENDING if non-nil means output pending elements that belong
5550 right after this element. (PENDING is normally 1;
5551 it is 0 while outputting pending elements, to avoid recursion.) */
5553 static void
5554 output_init_element (tree value, bool strict_string, tree type, tree field,
5555 int pending)
5557 if (type == error_mark_node)
5559 constructor_erroneous = 1;
5560 return;
5562 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5563 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5564 && !(TREE_CODE (value) == STRING_CST
5565 && TREE_CODE (type) == ARRAY_TYPE
5566 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5567 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5568 TYPE_MAIN_VARIANT (type))))
5569 value = default_conversion (value);
5571 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5572 && require_constant_value && !flag_isoc99 && pending)
5574 /* As an extension, allow initializing objects with static storage
5575 duration with compound literals (which are then treated just as
5576 the brace enclosed list they contain). */
5577 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5578 value = DECL_INITIAL (decl);
5581 if (value == error_mark_node)
5582 constructor_erroneous = 1;
5583 else if (!TREE_CONSTANT (value))
5584 constructor_constant = 0;
5585 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5586 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5587 || TREE_CODE (constructor_type) == UNION_TYPE)
5588 && DECL_C_BIT_FIELD (field)
5589 && TREE_CODE (value) != INTEGER_CST))
5590 constructor_simple = 0;
5592 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5594 if (require_constant_value)
5596 error_init ("initializer element is not constant");
5597 value = error_mark_node;
5599 else if (require_constant_elements)
5600 pedwarn ("initializer element is not computable at load time");
5603 /* If this field is empty (and not at the end of structure),
5604 don't do anything other than checking the initializer. */
5605 if (field
5606 && (TREE_TYPE (field) == error_mark_node
5607 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5608 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5609 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5610 || TREE_CHAIN (field)))))
5611 return;
5613 value = digest_init (type, value, strict_string, require_constant_value);
5614 if (value == error_mark_node)
5616 constructor_erroneous = 1;
5617 return;
5620 /* If this element doesn't come next in sequence,
5621 put it on constructor_pending_elts. */
5622 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5623 && (!constructor_incremental
5624 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5626 if (constructor_incremental
5627 && tree_int_cst_lt (field, constructor_unfilled_index))
5628 set_nonincremental_init ();
5630 add_pending_init (field, value);
5631 return;
5633 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5634 && (!constructor_incremental
5635 || field != constructor_unfilled_fields))
5637 /* We do this for records but not for unions. In a union,
5638 no matter which field is specified, it can be initialized
5639 right away since it starts at the beginning of the union. */
5640 if (constructor_incremental)
5642 if (!constructor_unfilled_fields)
5643 set_nonincremental_init ();
5644 else
5646 tree bitpos, unfillpos;
5648 bitpos = bit_position (field);
5649 unfillpos = bit_position (constructor_unfilled_fields);
5651 if (tree_int_cst_lt (bitpos, unfillpos))
5652 set_nonincremental_init ();
5656 add_pending_init (field, value);
5657 return;
5659 else if (TREE_CODE (constructor_type) == UNION_TYPE
5660 && constructor_elements)
5662 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5663 warning_init ("initialized field with side-effects overwritten");
5665 /* We can have just one union field set. */
5666 constructor_elements = 0;
5669 /* Otherwise, output this element either to
5670 constructor_elements or to the assembler file. */
5672 if (field && TREE_CODE (field) == INTEGER_CST)
5673 field = copy_node (field);
5674 constructor_elements
5675 = tree_cons (field, value, constructor_elements);
5677 /* Advance the variable that indicates sequential elements output. */
5678 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5679 constructor_unfilled_index
5680 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5681 bitsize_one_node);
5682 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5684 constructor_unfilled_fields
5685 = TREE_CHAIN (constructor_unfilled_fields);
5687 /* Skip any nameless bit fields. */
5688 while (constructor_unfilled_fields != 0
5689 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5690 && DECL_NAME (constructor_unfilled_fields) == 0)
5691 constructor_unfilled_fields =
5692 TREE_CHAIN (constructor_unfilled_fields);
5694 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5695 constructor_unfilled_fields = 0;
5697 /* Now output any pending elements which have become next. */
5698 if (pending)
5699 output_pending_init_elements (0);
5702 /* Output any pending elements which have become next.
5703 As we output elements, constructor_unfilled_{fields,index}
5704 advances, which may cause other elements to become next;
5705 if so, they too are output.
5707 If ALL is 0, we return when there are
5708 no more pending elements to output now.
5710 If ALL is 1, we output space as necessary so that
5711 we can output all the pending elements. */
5713 static void
5714 output_pending_init_elements (int all)
5716 struct init_node *elt = constructor_pending_elts;
5717 tree next;
5719 retry:
5721 /* Look through the whole pending tree.
5722 If we find an element that should be output now,
5723 output it. Otherwise, set NEXT to the element
5724 that comes first among those still pending. */
5726 next = 0;
5727 while (elt)
5729 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5731 if (tree_int_cst_equal (elt->purpose,
5732 constructor_unfilled_index))
5733 output_init_element (elt->value, true,
5734 TREE_TYPE (constructor_type),
5735 constructor_unfilled_index, 0);
5736 else if (tree_int_cst_lt (constructor_unfilled_index,
5737 elt->purpose))
5739 /* Advance to the next smaller node. */
5740 if (elt->left)
5741 elt = elt->left;
5742 else
5744 /* We have reached the smallest node bigger than the
5745 current unfilled index. Fill the space first. */
5746 next = elt->purpose;
5747 break;
5750 else
5752 /* Advance to the next bigger node. */
5753 if (elt->right)
5754 elt = elt->right;
5755 else
5757 /* We have reached the biggest node in a subtree. Find
5758 the parent of it, which is the next bigger node. */
5759 while (elt->parent && elt->parent->right == elt)
5760 elt = elt->parent;
5761 elt = elt->parent;
5762 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5763 elt->purpose))
5765 next = elt->purpose;
5766 break;
5771 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5772 || TREE_CODE (constructor_type) == UNION_TYPE)
5774 tree ctor_unfilled_bitpos, elt_bitpos;
5776 /* If the current record is complete we are done. */
5777 if (constructor_unfilled_fields == 0)
5778 break;
5780 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5781 elt_bitpos = bit_position (elt->purpose);
5782 /* We can't compare fields here because there might be empty
5783 fields in between. */
5784 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5786 constructor_unfilled_fields = elt->purpose;
5787 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5788 elt->purpose, 0);
5790 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5792 /* Advance to the next smaller node. */
5793 if (elt->left)
5794 elt = elt->left;
5795 else
5797 /* We have reached the smallest node bigger than the
5798 current unfilled field. Fill the space first. */
5799 next = elt->purpose;
5800 break;
5803 else
5805 /* Advance to the next bigger node. */
5806 if (elt->right)
5807 elt = elt->right;
5808 else
5810 /* We have reached the biggest node in a subtree. Find
5811 the parent of it, which is the next bigger node. */
5812 while (elt->parent && elt->parent->right == elt)
5813 elt = elt->parent;
5814 elt = elt->parent;
5815 if (elt
5816 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5817 bit_position (elt->purpose))))
5819 next = elt->purpose;
5820 break;
5827 /* Ordinarily return, but not if we want to output all
5828 and there are elements left. */
5829 if (! (all && next != 0))
5830 return;
5832 /* If it's not incremental, just skip over the gap, so that after
5833 jumping to retry we will output the next successive element. */
5834 if (TREE_CODE (constructor_type) == RECORD_TYPE
5835 || TREE_CODE (constructor_type) == UNION_TYPE)
5836 constructor_unfilled_fields = next;
5837 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5838 constructor_unfilled_index = next;
5840 /* ELT now points to the node in the pending tree with the next
5841 initializer to output. */
5842 goto retry;
5845 /* Add one non-braced element to the current constructor level.
5846 This adjusts the current position within the constructor's type.
5847 This may also start or terminate implicit levels
5848 to handle a partly-braced initializer.
5850 Once this has found the correct level for the new element,
5851 it calls output_init_element. */
5853 void
5854 process_init_element (struct c_expr value)
5856 tree orig_value = value.value;
5857 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
5858 bool strict_string = value.original_code == STRING_CST;
5860 designator_depth = 0;
5861 designator_errorneous = 0;
5863 /* Handle superfluous braces around string cst as in
5864 char x[] = {"foo"}; */
5865 if (string_flag
5866 && constructor_type
5867 && TREE_CODE (constructor_type) == ARRAY_TYPE
5868 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
5869 && integer_zerop (constructor_unfilled_index))
5871 if (constructor_stack->replacement_value.value)
5872 error_init ("excess elements in char array initializer");
5873 constructor_stack->replacement_value = value;
5874 return;
5877 if (constructor_stack->replacement_value.value != 0)
5879 error_init ("excess elements in struct initializer");
5880 return;
5883 /* Ignore elements of a brace group if it is entirely superfluous
5884 and has already been diagnosed. */
5885 if (constructor_type == 0)
5886 return;
5888 /* If we've exhausted any levels that didn't have braces,
5889 pop them now. */
5890 while (constructor_stack->implicit)
5892 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5893 || TREE_CODE (constructor_type) == UNION_TYPE)
5894 && constructor_fields == 0)
5895 process_init_element (pop_init_level (1));
5896 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5897 && (constructor_max_index == 0
5898 || tree_int_cst_lt (constructor_max_index,
5899 constructor_index)))
5900 process_init_element (pop_init_level (1));
5901 else
5902 break;
5905 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5906 if (constructor_range_stack)
5908 /* If value is a compound literal and we'll be just using its
5909 content, don't put it into a SAVE_EXPR. */
5910 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
5911 || !require_constant_value
5912 || flag_isoc99)
5913 value.value = save_expr (value.value);
5916 while (1)
5918 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5920 tree fieldtype;
5921 enum tree_code fieldcode;
5923 if (constructor_fields == 0)
5925 pedwarn_init ("excess elements in struct initializer");
5926 break;
5929 fieldtype = TREE_TYPE (constructor_fields);
5930 if (fieldtype != error_mark_node)
5931 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5932 fieldcode = TREE_CODE (fieldtype);
5934 /* Error for non-static initialization of a flexible array member. */
5935 if (fieldcode == ARRAY_TYPE
5936 && !require_constant_value
5937 && TYPE_SIZE (fieldtype) == NULL_TREE
5938 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5940 error_init ("non-static initialization of a flexible array member");
5941 break;
5944 /* Accept a string constant to initialize a subarray. */
5945 if (value.value != 0
5946 && fieldcode == ARRAY_TYPE
5947 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
5948 && string_flag)
5949 value.value = orig_value;
5950 /* Otherwise, if we have come to a subaggregate,
5951 and we don't have an element of its type, push into it. */
5952 else if (value.value != 0 && !constructor_no_implicit
5953 && value.value != error_mark_node
5954 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
5955 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5956 || fieldcode == UNION_TYPE))
5958 push_init_level (1);
5959 continue;
5962 if (value.value)
5964 push_member_name (constructor_fields);
5965 output_init_element (value.value, strict_string,
5966 fieldtype, constructor_fields, 1);
5967 RESTORE_SPELLING_DEPTH (constructor_depth);
5969 else
5970 /* Do the bookkeeping for an element that was
5971 directly output as a constructor. */
5973 /* For a record, keep track of end position of last field. */
5974 if (DECL_SIZE (constructor_fields))
5975 constructor_bit_index
5976 = size_binop (PLUS_EXPR,
5977 bit_position (constructor_fields),
5978 DECL_SIZE (constructor_fields));
5980 /* If the current field was the first one not yet written out,
5981 it isn't now, so update. */
5982 if (constructor_unfilled_fields == constructor_fields)
5984 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5985 /* Skip any nameless bit fields. */
5986 while (constructor_unfilled_fields != 0
5987 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5988 && DECL_NAME (constructor_unfilled_fields) == 0)
5989 constructor_unfilled_fields =
5990 TREE_CHAIN (constructor_unfilled_fields);
5994 constructor_fields = TREE_CHAIN (constructor_fields);
5995 /* Skip any nameless bit fields at the beginning. */
5996 while (constructor_fields != 0
5997 && DECL_C_BIT_FIELD (constructor_fields)
5998 && DECL_NAME (constructor_fields) == 0)
5999 constructor_fields = TREE_CHAIN (constructor_fields);
6001 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6003 tree fieldtype;
6004 enum tree_code fieldcode;
6006 if (constructor_fields == 0)
6008 pedwarn_init ("excess elements in union initializer");
6009 break;
6012 fieldtype = TREE_TYPE (constructor_fields);
6013 if (fieldtype != error_mark_node)
6014 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6015 fieldcode = TREE_CODE (fieldtype);
6017 /* Warn that traditional C rejects initialization of unions.
6018 We skip the warning if the value is zero. This is done
6019 under the assumption that the zero initializer in user
6020 code appears conditioned on e.g. __STDC__ to avoid
6021 "missing initializer" warnings and relies on default
6022 initialization to zero in the traditional C case.
6023 We also skip the warning if the initializer is designated,
6024 again on the assumption that this must be conditional on
6025 __STDC__ anyway (and we've already complained about the
6026 member-designator already). */
6027 if (warn_traditional && !in_system_header && !constructor_designated
6028 && !(value.value && (integer_zerop (value.value)
6029 || real_zerop (value.value))))
6030 warning ("traditional C rejects initialization of unions");
6032 /* Accept a string constant to initialize a subarray. */
6033 if (value.value != 0
6034 && fieldcode == ARRAY_TYPE
6035 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6036 && string_flag)
6037 value.value = orig_value;
6038 /* Otherwise, if we have come to a subaggregate,
6039 and we don't have an element of its type, push into it. */
6040 else if (value.value != 0 && !constructor_no_implicit
6041 && value.value != error_mark_node
6042 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6043 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6044 || fieldcode == UNION_TYPE))
6046 push_init_level (1);
6047 continue;
6050 if (value.value)
6052 push_member_name (constructor_fields);
6053 output_init_element (value.value, strict_string,
6054 fieldtype, constructor_fields, 1);
6055 RESTORE_SPELLING_DEPTH (constructor_depth);
6057 else
6058 /* Do the bookkeeping for an element that was
6059 directly output as a constructor. */
6061 constructor_bit_index = DECL_SIZE (constructor_fields);
6062 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6065 constructor_fields = 0;
6067 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6069 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6070 enum tree_code eltcode = TREE_CODE (elttype);
6072 /* Accept a string constant to initialize a subarray. */
6073 if (value.value != 0
6074 && eltcode == ARRAY_TYPE
6075 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6076 && string_flag)
6077 value.value = orig_value;
6078 /* Otherwise, if we have come to a subaggregate,
6079 and we don't have an element of its type, push into it. */
6080 else if (value.value != 0 && !constructor_no_implicit
6081 && value.value != error_mark_node
6082 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6083 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6084 || eltcode == UNION_TYPE))
6086 push_init_level (1);
6087 continue;
6090 if (constructor_max_index != 0
6091 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6092 || integer_all_onesp (constructor_max_index)))
6094 pedwarn_init ("excess elements in array initializer");
6095 break;
6098 /* Now output the actual element. */
6099 if (value.value)
6101 push_array_bounds (tree_low_cst (constructor_index, 0));
6102 output_init_element (value.value, strict_string,
6103 elttype, constructor_index, 1);
6104 RESTORE_SPELLING_DEPTH (constructor_depth);
6107 constructor_index
6108 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6110 if (!value.value)
6111 /* If we are doing the bookkeeping for an element that was
6112 directly output as a constructor, we must update
6113 constructor_unfilled_index. */
6114 constructor_unfilled_index = constructor_index;
6116 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6118 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6120 /* Do a basic check of initializer size. Note that vectors
6121 always have a fixed size derived from their type. */
6122 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6124 pedwarn_init ("excess elements in vector initializer");
6125 break;
6128 /* Now output the actual element. */
6129 if (value.value)
6130 output_init_element (value.value, strict_string,
6131 elttype, constructor_index, 1);
6133 constructor_index
6134 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6136 if (!value.value)
6137 /* If we are doing the bookkeeping for an element that was
6138 directly output as a constructor, we must update
6139 constructor_unfilled_index. */
6140 constructor_unfilled_index = constructor_index;
6143 /* Handle the sole element allowed in a braced initializer
6144 for a scalar variable. */
6145 else if (constructor_fields == 0)
6147 pedwarn_init ("excess elements in scalar initializer");
6148 break;
6150 else
6152 if (value.value)
6153 output_init_element (value.value, strict_string,
6154 constructor_type, NULL_TREE, 1);
6155 constructor_fields = 0;
6158 /* Handle range initializers either at this level or anywhere higher
6159 in the designator stack. */
6160 if (constructor_range_stack)
6162 struct constructor_range_stack *p, *range_stack;
6163 int finish = 0;
6165 range_stack = constructor_range_stack;
6166 constructor_range_stack = 0;
6167 while (constructor_stack != range_stack->stack)
6169 gcc_assert (constructor_stack->implicit);
6170 process_init_element (pop_init_level (1));
6172 for (p = range_stack;
6173 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6174 p = p->prev)
6176 gcc_assert (constructor_stack->implicit);
6177 process_init_element (pop_init_level (1));
6180 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6181 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6182 finish = 1;
6184 while (1)
6186 constructor_index = p->index;
6187 constructor_fields = p->fields;
6188 if (finish && p->range_end && p->index == p->range_start)
6190 finish = 0;
6191 p->prev = 0;
6193 p = p->next;
6194 if (!p)
6195 break;
6196 push_init_level (2);
6197 p->stack = constructor_stack;
6198 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6199 p->index = p->range_start;
6202 if (!finish)
6203 constructor_range_stack = range_stack;
6204 continue;
6207 break;
6210 constructor_range_stack = 0;
6213 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6214 (guaranteed to be 'volatile' or null) and ARGS (represented using
6215 an ASM_EXPR node). */
6216 tree
6217 build_asm_stmt (tree cv_qualifier, tree args)
6219 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6220 ASM_VOLATILE_P (args) = 1;
6221 return add_stmt (args);
6224 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6225 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6226 SIMPLE indicates whether there was anything at all after the
6227 string in the asm expression -- asm("blah") and asm("blah" : )
6228 are subtly different. We use a ASM_EXPR node to represent this. */
6229 tree
6230 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6231 bool simple)
6233 tree tail;
6234 tree args;
6235 int i;
6236 const char *constraint;
6237 bool allows_mem, allows_reg, is_inout;
6238 int ninputs;
6239 int noutputs;
6241 ninputs = list_length (inputs);
6242 noutputs = list_length (outputs);
6244 /* Remove output conversions that change the type but not the mode. */
6245 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6247 tree output = TREE_VALUE (tail);
6248 STRIP_NOPS (output);
6249 TREE_VALUE (tail) = output;
6250 lvalue_or_else (output, "invalid lvalue in asm statement");
6252 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6254 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
6255 &allows_mem, &allows_reg, &is_inout))
6257 /* By marking this operand as erroneous, we will not try
6258 to process this operand again in expand_asm_operands. */
6259 TREE_VALUE (tail) = error_mark_node;
6260 continue;
6263 /* If the operand is a DECL that is going to end up in
6264 memory, assume it is addressable. This is a bit more
6265 conservative than it would ideally be; the exact test is
6266 buried deep in expand_asm_operands and depends on the
6267 DECL_RTL for the OPERAND -- which we don't have at this
6268 point. */
6269 if (!allows_reg && DECL_P (output))
6270 c_mark_addressable (output);
6273 /* Perform default conversions on array and function inputs.
6274 Don't do this for other types as it would screw up operands
6275 expected to be in memory. */
6276 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6277 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6279 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6281 /* Simple asm statements are treated as volatile. */
6282 if (simple)
6284 ASM_VOLATILE_P (args) = 1;
6285 ASM_INPUT_P (args) = 1;
6287 return args;
6290 /* Generate a goto statement to LABEL. */
6292 tree
6293 c_finish_goto_label (tree label)
6295 tree decl = lookup_label (label);
6296 if (!decl)
6297 return NULL_TREE;
6299 TREE_USED (decl) = 1;
6300 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6303 /* Generate a computed goto statement to EXPR. */
6305 tree
6306 c_finish_goto_ptr (tree expr)
6308 if (pedantic)
6309 pedwarn ("ISO C forbids %<goto *expr;%>");
6310 expr = convert (ptr_type_node, expr);
6311 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6314 /* Generate a C `return' statement. RETVAL is the expression for what
6315 to return, or a null pointer for `return;' with no value. */
6317 tree
6318 c_finish_return (tree retval)
6320 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6322 if (TREE_THIS_VOLATILE (current_function_decl))
6323 warning ("function declared %<noreturn%> has a %<return%> statement");
6325 if (!retval)
6327 current_function_returns_null = 1;
6328 if ((warn_return_type || flag_isoc99)
6329 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6330 pedwarn_c99 ("%<return%> with no value, in "
6331 "function returning non-void");
6333 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6335 current_function_returns_null = 1;
6336 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6337 pedwarn ("%<return%> with a value, in function returning void");
6339 else
6341 tree t = convert_for_assignment (valtype, retval, _("return"),
6342 NULL_TREE, NULL_TREE, 0);
6343 tree res = DECL_RESULT (current_function_decl);
6344 tree inner;
6346 current_function_returns_value = 1;
6347 if (t == error_mark_node)
6348 return NULL_TREE;
6350 inner = t = convert (TREE_TYPE (res), t);
6352 /* Strip any conversions, additions, and subtractions, and see if
6353 we are returning the address of a local variable. Warn if so. */
6354 while (1)
6356 switch (TREE_CODE (inner))
6358 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6359 case PLUS_EXPR:
6360 inner = TREE_OPERAND (inner, 0);
6361 continue;
6363 case MINUS_EXPR:
6364 /* If the second operand of the MINUS_EXPR has a pointer
6365 type (or is converted from it), this may be valid, so
6366 don't give a warning. */
6368 tree op1 = TREE_OPERAND (inner, 1);
6370 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6371 && (TREE_CODE (op1) == NOP_EXPR
6372 || TREE_CODE (op1) == NON_LVALUE_EXPR
6373 || TREE_CODE (op1) == CONVERT_EXPR))
6374 op1 = TREE_OPERAND (op1, 0);
6376 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6377 break;
6379 inner = TREE_OPERAND (inner, 0);
6380 continue;
6383 case ADDR_EXPR:
6384 inner = TREE_OPERAND (inner, 0);
6386 while (REFERENCE_CLASS_P (inner)
6387 && TREE_CODE (inner) != INDIRECT_REF)
6388 inner = TREE_OPERAND (inner, 0);
6390 if (DECL_P (inner)
6391 && ! DECL_EXTERNAL (inner)
6392 && ! TREE_STATIC (inner)
6393 && DECL_CONTEXT (inner) == current_function_decl)
6394 warning ("function returns address of local variable");
6395 break;
6397 default:
6398 break;
6401 break;
6404 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6407 return add_stmt (build_stmt (RETURN_EXPR, retval));
6410 struct c_switch {
6411 /* The SWITCH_STMT being built. */
6412 tree switch_stmt;
6414 /* The original type of the testing expression, i.e. before the
6415 default conversion is applied. */
6416 tree orig_type;
6418 /* A splay-tree mapping the low element of a case range to the high
6419 element, or NULL_TREE if there is no high element. Used to
6420 determine whether or not a new case label duplicates an old case
6421 label. We need a tree, rather than simply a hash table, because
6422 of the GNU case range extension. */
6423 splay_tree cases;
6425 /* The next node on the stack. */
6426 struct c_switch *next;
6429 /* A stack of the currently active switch statements. The innermost
6430 switch statement is on the top of the stack. There is no need to
6431 mark the stack for garbage collection because it is only active
6432 during the processing of the body of a function, and we never
6433 collect at that point. */
6435 struct c_switch *c_switch_stack;
6437 /* Start a C switch statement, testing expression EXP. Return the new
6438 SWITCH_STMT. */
6440 tree
6441 c_start_case (tree exp)
6443 enum tree_code code;
6444 tree type, orig_type = error_mark_node;
6445 struct c_switch *cs;
6447 if (exp != error_mark_node)
6449 code = TREE_CODE (TREE_TYPE (exp));
6450 orig_type = TREE_TYPE (exp);
6452 if (! INTEGRAL_TYPE_P (orig_type)
6453 && code != ERROR_MARK)
6455 error ("switch quantity not an integer");
6456 exp = integer_zero_node;
6458 else
6460 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6462 if (warn_traditional && !in_system_header
6463 && (type == long_integer_type_node
6464 || type == long_unsigned_type_node))
6465 warning ("%<long%> switch expression not converted to "
6466 "%<int%> in ISO C");
6468 exp = default_conversion (exp);
6469 type = TREE_TYPE (exp);
6473 /* Add this new SWITCH_STMT to the stack. */
6474 cs = XNEW (struct c_switch);
6475 cs->switch_stmt = build_stmt ((enum tree_code) SWITCH_STMT, exp, NULL_TREE,
6476 orig_type);
6477 cs->orig_type = orig_type;
6478 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6479 cs->next = c_switch_stack;
6480 c_switch_stack = cs;
6482 return add_stmt (cs->switch_stmt);
6485 /* Process a case label. */
6487 tree
6488 do_case (tree low_value, tree high_value)
6490 tree label = NULL_TREE;
6492 if (c_switch_stack)
6494 label = c_add_case_label (c_switch_stack->cases,
6495 SWITCH_COND (c_switch_stack->switch_stmt),
6496 c_switch_stack->orig_type,
6497 low_value, high_value);
6498 if (label == error_mark_node)
6499 label = NULL_TREE;
6501 else if (low_value)
6502 error ("case label not within a switch statement");
6503 else
6504 error ("%<default%> label not within a switch statement");
6506 return label;
6509 /* Finish the switch statement. */
6511 void
6512 c_finish_case (tree body)
6514 struct c_switch *cs = c_switch_stack;
6516 SWITCH_BODY (cs->switch_stmt) = body;
6518 /* Emit warnings as needed. */
6519 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6521 /* Pop the stack. */
6522 c_switch_stack = cs->next;
6523 splay_tree_delete (cs->cases);
6524 XDELETE (cs);
6527 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6528 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6529 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6530 statement, and was not surrounded with parenthesis. */
6532 void
6533 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6534 tree else_block, bool nested_if)
6536 tree stmt;
6538 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6539 if (warn_parentheses && nested_if && else_block == NULL)
6541 tree inner_if = then_block;
6543 /* We know from the grammar productions that there is an IF nested
6544 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6545 it might not be exactly THEN_BLOCK, but should be the last
6546 non-container statement within. */
6547 while (1)
6548 switch (TREE_CODE (inner_if))
6550 case COND_EXPR:
6551 goto found;
6552 case BIND_EXPR:
6553 inner_if = BIND_EXPR_BODY (inner_if);
6554 break;
6555 case STATEMENT_LIST:
6556 inner_if = expr_last (then_block);
6557 break;
6558 case TRY_FINALLY_EXPR:
6559 case TRY_CATCH_EXPR:
6560 inner_if = TREE_OPERAND (inner_if, 0);
6561 break;
6562 default:
6563 gcc_unreachable ();
6565 found:
6567 if (COND_EXPR_ELSE (inner_if))
6568 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6569 &if_locus);
6572 /* Diagnose ";" via the special empty statement node that we create. */
6573 if (extra_warnings)
6575 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6577 if (!else_block)
6578 warning ("%Hempty body in an if-statement",
6579 EXPR_LOCUS (then_block));
6580 then_block = alloc_stmt_list ();
6582 if (else_block
6583 && TREE_CODE (else_block) == NOP_EXPR
6584 && !TREE_TYPE (else_block))
6586 warning ("%Hempty body in an else-statement",
6587 EXPR_LOCUS (else_block));
6588 else_block = alloc_stmt_list ();
6592 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6593 SET_EXPR_LOCATION (stmt, if_locus);
6594 add_stmt (stmt);
6597 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6598 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6599 is false for DO loops. INCR is the FOR increment expression. BODY is
6600 the statement controlled by the loop. BLAB is the break label. CLAB is
6601 the continue label. Everything is allowed to be NULL. */
6603 void
6604 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6605 tree blab, tree clab, bool cond_is_first)
6607 tree entry = NULL, exit = NULL, t;
6609 /* Detect do { ... } while (0) and don't generate loop construct. */
6610 if (cond && !cond_is_first && integer_zerop (cond))
6611 cond = NULL;
6612 if (cond_is_first || cond)
6614 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6616 /* If we have an exit condition, then we build an IF with gotos either
6617 out of the loop, or to the top of it. If there's no exit condition,
6618 then we just build a jump back to the top. */
6619 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6621 if (cond)
6623 /* Canonicalize the loop condition to the end. This means
6624 generating a branch to the loop condition. Reuse the
6625 continue label, if possible. */
6626 if (cond_is_first)
6628 if (incr || !clab)
6630 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6631 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6633 else
6634 t = build1 (GOTO_EXPR, void_type_node, clab);
6635 SET_EXPR_LOCATION (t, start_locus);
6636 add_stmt (t);
6639 t = build_and_jump (&blab);
6640 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6641 exit = fold (exit);
6642 if (cond_is_first)
6643 SET_EXPR_LOCATION (exit, start_locus);
6644 else
6645 SET_EXPR_LOCATION (exit, input_location);
6648 add_stmt (top);
6651 if (body)
6652 add_stmt (body);
6653 if (clab)
6654 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6655 if (incr)
6656 add_stmt (incr);
6657 if (entry)
6658 add_stmt (entry);
6659 if (exit)
6660 add_stmt (exit);
6661 if (blab)
6662 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6665 tree
6666 c_finish_bc_stmt (tree *label_p, bool is_break)
6668 tree label = *label_p;
6670 if (!label)
6671 *label_p = label = create_artificial_label ();
6672 else if (TREE_CODE (label) != LABEL_DECL)
6674 if (is_break)
6675 error ("break statement not within loop or switch");
6676 else
6677 error ("continue statement not within a loop");
6678 return NULL_TREE;
6681 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6684 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6686 static void
6687 emit_side_effect_warnings (tree expr)
6689 if (expr == error_mark_node)
6691 else if (!TREE_SIDE_EFFECTS (expr))
6693 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6694 warning ("%Hstatement with no effect",
6695 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
6697 else if (warn_unused_value)
6698 warn_if_unused_value (expr, input_location);
6701 /* Process an expression as if it were a complete statement. Emit
6702 diagnostics, but do not call ADD_STMT. */
6704 tree
6705 c_process_expr_stmt (tree expr)
6707 if (!expr)
6708 return NULL_TREE;
6710 /* Do default conversion if safe and possibly important,
6711 in case within ({...}). */
6712 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
6713 && (flag_isoc99 || lvalue_p (expr)))
6714 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
6715 expr = default_conversion (expr);
6717 if (warn_sequence_point)
6718 verify_sequence_points (expr);
6720 if (TREE_TYPE (expr) != error_mark_node
6721 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
6722 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
6723 error ("expression statement has incomplete type");
6725 /* If we're not processing a statement expression, warn about unused values.
6726 Warnings for statement expressions will be emitted later, once we figure
6727 out which is the result. */
6728 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6729 && (extra_warnings || warn_unused_value))
6730 emit_side_effect_warnings (expr);
6732 /* If the expression is not of a type to which we cannot assign a line
6733 number, wrap the thing in a no-op NOP_EXPR. */
6734 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
6735 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
6737 if (EXPR_P (expr))
6738 SET_EXPR_LOCATION (expr, input_location);
6740 return expr;
6743 /* Emit an expression as a statement. */
6745 tree
6746 c_finish_expr_stmt (tree expr)
6748 if (expr)
6749 return add_stmt (c_process_expr_stmt (expr));
6750 else
6751 return NULL;
6754 /* Do the opposite and emit a statement as an expression. To begin,
6755 create a new binding level and return it. */
6757 tree
6758 c_begin_stmt_expr (void)
6760 tree ret;
6762 /* We must force a BLOCK for this level so that, if it is not expanded
6763 later, there is a way to turn off the entire subtree of blocks that
6764 are contained in it. */
6765 keep_next_level ();
6766 ret = c_begin_compound_stmt (true);
6768 /* Mark the current statement list as belonging to a statement list. */
6769 STATEMENT_LIST_STMT_EXPR (ret) = 1;
6771 return ret;
6774 tree
6775 c_finish_stmt_expr (tree body)
6777 tree last, type, tmp, val;
6778 tree *last_p;
6780 body = c_end_compound_stmt (body, true);
6782 /* Locate the last statement in BODY. See c_end_compound_stmt
6783 about always returning a BIND_EXPR. */
6784 last_p = &BIND_EXPR_BODY (body);
6785 last = BIND_EXPR_BODY (body);
6787 continue_searching:
6788 if (TREE_CODE (last) == STATEMENT_LIST)
6790 tree_stmt_iterator i;
6792 /* This can happen with degenerate cases like ({ }). No value. */
6793 if (!TREE_SIDE_EFFECTS (last))
6794 return body;
6796 /* If we're supposed to generate side effects warnings, process
6797 all of the statements except the last. */
6798 if (extra_warnings || warn_unused_value)
6800 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
6801 emit_side_effect_warnings (tsi_stmt (i));
6803 else
6804 i = tsi_last (last);
6805 last_p = tsi_stmt_ptr (i);
6806 last = *last_p;
6809 /* If the end of the list is exception related, then the list was split
6810 by a call to push_cleanup. Continue searching. */
6811 if (TREE_CODE (last) == TRY_FINALLY_EXPR
6812 || TREE_CODE (last) == TRY_CATCH_EXPR)
6814 last_p = &TREE_OPERAND (last, 0);
6815 last = *last_p;
6816 goto continue_searching;
6819 /* In the case that the BIND_EXPR is not necessary, return the
6820 expression out from inside it. */
6821 if (last == error_mark_node
6822 || (last == BIND_EXPR_BODY (body)
6823 && BIND_EXPR_VARS (body) == NULL))
6824 return last;
6826 /* Extract the type of said expression. */
6827 type = TREE_TYPE (last);
6829 /* If we're not returning a value at all, then the BIND_EXPR that
6830 we already have is a fine expression to return. */
6831 if (!type || VOID_TYPE_P (type))
6832 return body;
6834 /* Now that we've located the expression containing the value, it seems
6835 silly to make voidify_wrapper_expr repeat the process. Create a
6836 temporary of the appropriate type and stick it in a TARGET_EXPR. */
6837 tmp = create_tmp_var_raw (type, NULL);
6839 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
6840 tree_expr_nonnegative_p giving up immediately. */
6841 val = last;
6842 if (TREE_CODE (val) == NOP_EXPR
6843 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
6844 val = TREE_OPERAND (val, 0);
6846 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
6847 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
6849 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
6852 /* Begin and end compound statements. This is as simple as pushing
6853 and popping new statement lists from the tree. */
6855 tree
6856 c_begin_compound_stmt (bool do_scope)
6858 tree stmt = push_stmt_list ();
6859 if (do_scope)
6860 push_scope ();
6861 return stmt;
6864 tree
6865 c_end_compound_stmt (tree stmt, bool do_scope)
6867 tree block = NULL;
6869 if (do_scope)
6871 if (c_dialect_objc ())
6872 objc_clear_super_receiver ();
6873 block = pop_scope ();
6876 stmt = pop_stmt_list (stmt);
6877 stmt = c_build_bind_expr (block, stmt);
6879 /* If this compound statement is nested immediately inside a statement
6880 expression, then force a BIND_EXPR to be created. Otherwise we'll
6881 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
6882 STATEMENT_LISTs merge, and thus we can lose track of what statement
6883 was really last. */
6884 if (cur_stmt_list
6885 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
6886 && TREE_CODE (stmt) != BIND_EXPR)
6888 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
6889 TREE_SIDE_EFFECTS (stmt) = 1;
6892 return stmt;
6895 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
6896 when the current scope is exited. EH_ONLY is true when this is not
6897 meant to apply to normal control flow transfer. */
6899 void
6900 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
6902 enum tree_code code;
6903 tree stmt, list;
6904 bool stmt_expr;
6906 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
6907 stmt = build_stmt (code, NULL, cleanup);
6908 add_stmt (stmt);
6909 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
6910 list = push_stmt_list ();
6911 TREE_OPERAND (stmt, 0) = list;
6912 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
6915 /* Build a binary-operation expression without default conversions.
6916 CODE is the kind of expression to build.
6917 This function differs from `build' in several ways:
6918 the data type of the result is computed and recorded in it,
6919 warnings are generated if arg data types are invalid,
6920 special handling for addition and subtraction of pointers is known,
6921 and some optimization is done (operations on narrow ints
6922 are done in the narrower type when that gives the same result).
6923 Constant folding is also done before the result is returned.
6925 Note that the operands will never have enumeral types, or function
6926 or array types, because either they will have the default conversions
6927 performed or they have both just been converted to some other type in which
6928 the arithmetic is to be done. */
6930 tree
6931 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6932 int convert_p)
6934 tree type0, type1;
6935 enum tree_code code0, code1;
6936 tree op0, op1;
6938 /* Expression code to give to the expression when it is built.
6939 Normally this is CODE, which is what the caller asked for,
6940 but in some special cases we change it. */
6941 enum tree_code resultcode = code;
6943 /* Data type in which the computation is to be performed.
6944 In the simplest cases this is the common type of the arguments. */
6945 tree result_type = NULL;
6947 /* Nonzero means operands have already been type-converted
6948 in whatever way is necessary.
6949 Zero means they need to be converted to RESULT_TYPE. */
6950 int converted = 0;
6952 /* Nonzero means create the expression with this type, rather than
6953 RESULT_TYPE. */
6954 tree build_type = 0;
6956 /* Nonzero means after finally constructing the expression
6957 convert it to this type. */
6958 tree final_type = 0;
6960 /* Nonzero if this is an operation like MIN or MAX which can
6961 safely be computed in short if both args are promoted shorts.
6962 Also implies COMMON.
6963 -1 indicates a bitwise operation; this makes a difference
6964 in the exact conditions for when it is safe to do the operation
6965 in a narrower mode. */
6966 int shorten = 0;
6968 /* Nonzero if this is a comparison operation;
6969 if both args are promoted shorts, compare the original shorts.
6970 Also implies COMMON. */
6971 int short_compare = 0;
6973 /* Nonzero if this is a right-shift operation, which can be computed on the
6974 original short and then promoted if the operand is a promoted short. */
6975 int short_shift = 0;
6977 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6978 int common = 0;
6980 if (convert_p)
6982 op0 = default_conversion (orig_op0);
6983 op1 = default_conversion (orig_op1);
6985 else
6987 op0 = orig_op0;
6988 op1 = orig_op1;
6991 type0 = TREE_TYPE (op0);
6992 type1 = TREE_TYPE (op1);
6994 /* The expression codes of the data types of the arguments tell us
6995 whether the arguments are integers, floating, pointers, etc. */
6996 code0 = TREE_CODE (type0);
6997 code1 = TREE_CODE (type1);
6999 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7000 STRIP_TYPE_NOPS (op0);
7001 STRIP_TYPE_NOPS (op1);
7003 /* If an error was already reported for one of the arguments,
7004 avoid reporting another error. */
7006 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7007 return error_mark_node;
7009 switch (code)
7011 case PLUS_EXPR:
7012 /* Handle the pointer + int case. */
7013 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7014 return pointer_int_sum (PLUS_EXPR, op0, op1);
7015 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7016 return pointer_int_sum (PLUS_EXPR, op1, op0);
7017 else
7018 common = 1;
7019 break;
7021 case MINUS_EXPR:
7022 /* Subtraction of two similar pointers.
7023 We must subtract them as integers, then divide by object size. */
7024 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7025 && comp_target_types (type0, type1, 1))
7026 return pointer_diff (op0, op1);
7027 /* Handle pointer minus int. Just like pointer plus int. */
7028 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7029 return pointer_int_sum (MINUS_EXPR, op0, op1);
7030 else
7031 common = 1;
7032 break;
7034 case MULT_EXPR:
7035 common = 1;
7036 break;
7038 case TRUNC_DIV_EXPR:
7039 case CEIL_DIV_EXPR:
7040 case FLOOR_DIV_EXPR:
7041 case ROUND_DIV_EXPR:
7042 case EXACT_DIV_EXPR:
7043 /* Floating point division by zero is a legitimate way to obtain
7044 infinities and NaNs. */
7045 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7046 warning ("division by zero");
7048 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7049 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7050 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7051 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7053 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7054 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7055 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7056 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7058 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7059 resultcode = RDIV_EXPR;
7060 else
7061 /* Although it would be tempting to shorten always here, that
7062 loses on some targets, since the modulo instruction is
7063 undefined if the quotient can't be represented in the
7064 computation mode. We shorten only if unsigned or if
7065 dividing by something we know != -1. */
7066 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7067 || (TREE_CODE (op1) == INTEGER_CST
7068 && ! integer_all_onesp (op1)));
7069 common = 1;
7071 break;
7073 case BIT_AND_EXPR:
7074 case BIT_IOR_EXPR:
7075 case BIT_XOR_EXPR:
7076 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7077 shorten = -1;
7078 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7079 common = 1;
7080 break;
7082 case TRUNC_MOD_EXPR:
7083 case FLOOR_MOD_EXPR:
7084 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7085 warning ("division by zero");
7087 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7089 /* Although it would be tempting to shorten always here, that loses
7090 on some targets, since the modulo instruction is undefined if the
7091 quotient can't be represented in the computation mode. We shorten
7092 only if unsigned or if dividing by something we know != -1. */
7093 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7094 || (TREE_CODE (op1) == INTEGER_CST
7095 && ! integer_all_onesp (op1)));
7096 common = 1;
7098 break;
7100 case TRUTH_ANDIF_EXPR:
7101 case TRUTH_ORIF_EXPR:
7102 case TRUTH_AND_EXPR:
7103 case TRUTH_OR_EXPR:
7104 case TRUTH_XOR_EXPR:
7105 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7106 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7107 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7108 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7110 /* Result of these operations is always an int,
7111 but that does not mean the operands should be
7112 converted to ints! */
7113 result_type = integer_type_node;
7114 op0 = lang_hooks.truthvalue_conversion (op0);
7115 op1 = lang_hooks.truthvalue_conversion (op1);
7116 converted = 1;
7118 break;
7120 /* Shift operations: result has same type as first operand;
7121 always convert second operand to int.
7122 Also set SHORT_SHIFT if shifting rightward. */
7124 case RSHIFT_EXPR:
7125 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7127 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7129 if (tree_int_cst_sgn (op1) < 0)
7130 warning ("right shift count is negative");
7131 else
7133 if (! integer_zerop (op1))
7134 short_shift = 1;
7136 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7137 warning ("right shift count >= width of type");
7141 /* Use the type of the value to be shifted. */
7142 result_type = type0;
7143 /* Convert the shift-count to an integer, regardless of size
7144 of value being shifted. */
7145 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7146 op1 = convert (integer_type_node, op1);
7147 /* Avoid converting op1 to result_type later. */
7148 converted = 1;
7150 break;
7152 case LSHIFT_EXPR:
7153 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7155 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7157 if (tree_int_cst_sgn (op1) < 0)
7158 warning ("left shift count is negative");
7160 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7161 warning ("left shift count >= width of type");
7164 /* Use the type of the value to be shifted. */
7165 result_type = type0;
7166 /* Convert the shift-count to an integer, regardless of size
7167 of value being shifted. */
7168 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7169 op1 = convert (integer_type_node, op1);
7170 /* Avoid converting op1 to result_type later. */
7171 converted = 1;
7173 break;
7175 case RROTATE_EXPR:
7176 case LROTATE_EXPR:
7177 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7179 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7181 if (tree_int_cst_sgn (op1) < 0)
7182 warning ("shift count is negative");
7183 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7184 warning ("shift count >= width of type");
7187 /* Use the type of the value to be shifted. */
7188 result_type = type0;
7189 /* Convert the shift-count to an integer, regardless of size
7190 of value being shifted. */
7191 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7192 op1 = convert (integer_type_node, op1);
7193 /* Avoid converting op1 to result_type later. */
7194 converted = 1;
7196 break;
7198 case EQ_EXPR:
7199 case NE_EXPR:
7200 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7201 warning ("comparing floating point with == or != is unsafe");
7202 /* Result of comparison is always int,
7203 but don't convert the args to int! */
7204 build_type = integer_type_node;
7205 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7206 || code0 == COMPLEX_TYPE)
7207 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7208 || code1 == COMPLEX_TYPE))
7209 short_compare = 1;
7210 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7212 tree tt0 = TREE_TYPE (type0);
7213 tree tt1 = TREE_TYPE (type1);
7214 /* Anything compares with void *. void * compares with anything.
7215 Otherwise, the targets must be compatible
7216 and both must be object or both incomplete. */
7217 if (comp_target_types (type0, type1, 1))
7218 result_type = common_pointer_type (type0, type1);
7219 else if (VOID_TYPE_P (tt0))
7221 /* op0 != orig_op0 detects the case of something
7222 whose value is 0 but which isn't a valid null ptr const. */
7223 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7224 && TREE_CODE (tt1) == FUNCTION_TYPE)
7225 pedwarn ("ISO C forbids comparison of %<void *%>"
7226 " with function pointer");
7228 else if (VOID_TYPE_P (tt1))
7230 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7231 && TREE_CODE (tt0) == FUNCTION_TYPE)
7232 pedwarn ("ISO C forbids comparison of %<void *%>"
7233 " with function pointer");
7235 else
7236 pedwarn ("comparison of distinct pointer types lacks a cast");
7238 if (result_type == NULL_TREE)
7239 result_type = ptr_type_node;
7241 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7242 && integer_zerop (op1))
7243 result_type = type0;
7244 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7245 && integer_zerop (op0))
7246 result_type = type1;
7247 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7249 result_type = type0;
7250 pedwarn ("comparison between pointer and integer");
7252 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7254 result_type = type1;
7255 pedwarn ("comparison between pointer and integer");
7257 break;
7259 case MAX_EXPR:
7260 case MIN_EXPR:
7261 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7262 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7263 shorten = 1;
7264 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7266 if (comp_target_types (type0, type1, 1))
7268 result_type = common_pointer_type (type0, type1);
7269 if (pedantic
7270 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7271 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7273 else
7275 result_type = ptr_type_node;
7276 pedwarn ("comparison of distinct pointer types lacks a cast");
7279 break;
7281 case LE_EXPR:
7282 case GE_EXPR:
7283 case LT_EXPR:
7284 case GT_EXPR:
7285 build_type = integer_type_node;
7286 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7287 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7288 short_compare = 1;
7289 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7291 if (comp_target_types (type0, type1, 1))
7293 result_type = common_pointer_type (type0, type1);
7294 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7295 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7296 pedwarn ("comparison of complete and incomplete pointers");
7297 else if (pedantic
7298 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7299 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7301 else
7303 result_type = ptr_type_node;
7304 pedwarn ("comparison of distinct pointer types lacks a cast");
7307 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7308 && integer_zerop (op1))
7310 result_type = type0;
7311 if (pedantic || extra_warnings)
7312 pedwarn ("ordered comparison of pointer with integer zero");
7314 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7315 && integer_zerop (op0))
7317 result_type = type1;
7318 if (pedantic)
7319 pedwarn ("ordered comparison of pointer with integer zero");
7321 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7323 result_type = type0;
7324 pedwarn ("comparison between pointer and integer");
7326 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7328 result_type = type1;
7329 pedwarn ("comparison between pointer and integer");
7331 break;
7333 case UNORDERED_EXPR:
7334 case ORDERED_EXPR:
7335 case UNLT_EXPR:
7336 case UNLE_EXPR:
7337 case UNGT_EXPR:
7338 case UNGE_EXPR:
7339 case UNEQ_EXPR:
7340 case LTGT_EXPR:
7341 build_type = integer_type_node;
7342 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
7344 error ("unordered comparison on non-floating point argument");
7345 return error_mark_node;
7347 common = 1;
7348 break;
7350 default:
7351 break;
7354 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7355 return error_mark_node;
7357 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7358 || code0 == VECTOR_TYPE)
7360 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7361 || code1 == VECTOR_TYPE))
7363 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7365 if (shorten || common || short_compare)
7366 result_type = common_type (type0, type1);
7368 /* For certain operations (which identify themselves by shorten != 0)
7369 if both args were extended from the same smaller type,
7370 do the arithmetic in that type and then extend.
7372 shorten !=0 and !=1 indicates a bitwise operation.
7373 For them, this optimization is safe only if
7374 both args are zero-extended or both are sign-extended.
7375 Otherwise, we might change the result.
7376 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7377 but calculated in (unsigned short) it would be (unsigned short)-1. */
7379 if (shorten && none_complex)
7381 int unsigned0, unsigned1;
7382 tree arg0 = get_narrower (op0, &unsigned0);
7383 tree arg1 = get_narrower (op1, &unsigned1);
7384 /* UNS is 1 if the operation to be done is an unsigned one. */
7385 int uns = TYPE_UNSIGNED (result_type);
7386 tree type;
7388 final_type = result_type;
7390 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7391 but it *requires* conversion to FINAL_TYPE. */
7393 if ((TYPE_PRECISION (TREE_TYPE (op0))
7394 == TYPE_PRECISION (TREE_TYPE (arg0)))
7395 && TREE_TYPE (op0) != final_type)
7396 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7397 if ((TYPE_PRECISION (TREE_TYPE (op1))
7398 == TYPE_PRECISION (TREE_TYPE (arg1)))
7399 && TREE_TYPE (op1) != final_type)
7400 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7402 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7404 /* For bitwise operations, signedness of nominal type
7405 does not matter. Consider only how operands were extended. */
7406 if (shorten == -1)
7407 uns = unsigned0;
7409 /* Note that in all three cases below we refrain from optimizing
7410 an unsigned operation on sign-extended args.
7411 That would not be valid. */
7413 /* Both args variable: if both extended in same way
7414 from same width, do it in that width.
7415 Do it unsigned if args were zero-extended. */
7416 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7417 < TYPE_PRECISION (result_type))
7418 && (TYPE_PRECISION (TREE_TYPE (arg1))
7419 == TYPE_PRECISION (TREE_TYPE (arg0)))
7420 && unsigned0 == unsigned1
7421 && (unsigned0 || !uns))
7422 result_type
7423 = c_common_signed_or_unsigned_type
7424 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7425 else if (TREE_CODE (arg0) == INTEGER_CST
7426 && (unsigned1 || !uns)
7427 && (TYPE_PRECISION (TREE_TYPE (arg1))
7428 < TYPE_PRECISION (result_type))
7429 && (type
7430 = c_common_signed_or_unsigned_type (unsigned1,
7431 TREE_TYPE (arg1)),
7432 int_fits_type_p (arg0, type)))
7433 result_type = type;
7434 else if (TREE_CODE (arg1) == INTEGER_CST
7435 && (unsigned0 || !uns)
7436 && (TYPE_PRECISION (TREE_TYPE (arg0))
7437 < TYPE_PRECISION (result_type))
7438 && (type
7439 = c_common_signed_or_unsigned_type (unsigned0,
7440 TREE_TYPE (arg0)),
7441 int_fits_type_p (arg1, type)))
7442 result_type = type;
7445 /* Shifts can be shortened if shifting right. */
7447 if (short_shift)
7449 int unsigned_arg;
7450 tree arg0 = get_narrower (op0, &unsigned_arg);
7452 final_type = result_type;
7454 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7455 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7457 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7458 /* We can shorten only if the shift count is less than the
7459 number of bits in the smaller type size. */
7460 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7461 /* We cannot drop an unsigned shift after sign-extension. */
7462 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7464 /* Do an unsigned shift if the operand was zero-extended. */
7465 result_type
7466 = c_common_signed_or_unsigned_type (unsigned_arg,
7467 TREE_TYPE (arg0));
7468 /* Convert value-to-be-shifted to that type. */
7469 if (TREE_TYPE (op0) != result_type)
7470 op0 = convert (result_type, op0);
7471 converted = 1;
7475 /* Comparison operations are shortened too but differently.
7476 They identify themselves by setting short_compare = 1. */
7478 if (short_compare)
7480 /* Don't write &op0, etc., because that would prevent op0
7481 from being kept in a register.
7482 Instead, make copies of the our local variables and
7483 pass the copies by reference, then copy them back afterward. */
7484 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7485 enum tree_code xresultcode = resultcode;
7486 tree val
7487 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7489 if (val != 0)
7490 return val;
7492 op0 = xop0, op1 = xop1;
7493 converted = 1;
7494 resultcode = xresultcode;
7496 if (warn_sign_compare && skip_evaluation == 0)
7498 int op0_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7499 int op1_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7500 int unsignedp0, unsignedp1;
7501 tree primop0 = get_narrower (op0, &unsignedp0);
7502 tree primop1 = get_narrower (op1, &unsignedp1);
7504 xop0 = orig_op0;
7505 xop1 = orig_op1;
7506 STRIP_TYPE_NOPS (xop0);
7507 STRIP_TYPE_NOPS (xop1);
7509 /* Give warnings for comparisons between signed and unsigned
7510 quantities that may fail.
7512 Do the checking based on the original operand trees, so that
7513 casts will be considered, but default promotions won't be.
7515 Do not warn if the comparison is being done in a signed type,
7516 since the signed type will only be chosen if it can represent
7517 all the values of the unsigned type. */
7518 if (! TYPE_UNSIGNED (result_type))
7519 /* OK */;
7520 /* Do not warn if both operands are the same signedness. */
7521 else if (op0_signed == op1_signed)
7522 /* OK */;
7523 else
7525 tree sop, uop;
7527 if (op0_signed)
7528 sop = xop0, uop = xop1;
7529 else
7530 sop = xop1, uop = xop0;
7532 /* Do not warn if the signed quantity is an
7533 unsuffixed integer literal (or some static
7534 constant expression involving such literals or a
7535 conditional expression involving such literals)
7536 and it is non-negative. */
7537 if (tree_expr_nonnegative_p (sop))
7538 /* OK */;
7539 /* Do not warn if the comparison is an equality operation,
7540 the unsigned quantity is an integral constant, and it
7541 would fit in the result if the result were signed. */
7542 else if (TREE_CODE (uop) == INTEGER_CST
7543 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7544 && int_fits_type_p
7545 (uop, c_common_signed_type (result_type)))
7546 /* OK */;
7547 /* Do not warn if the unsigned quantity is an enumeration
7548 constant and its maximum value would fit in the result
7549 if the result were signed. */
7550 else if (TREE_CODE (uop) == INTEGER_CST
7551 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7552 && int_fits_type_p
7553 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7554 c_common_signed_type (result_type)))
7555 /* OK */;
7556 else
7557 warning ("comparison between signed and unsigned");
7560 /* Warn if two unsigned values are being compared in a size
7561 larger than their original size, and one (and only one) is the
7562 result of a `~' operator. This comparison will always fail.
7564 Also warn if one operand is a constant, and the constant
7565 does not have all bits set that are set in the ~ operand
7566 when it is extended. */
7568 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7569 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7571 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7572 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7573 &unsignedp0);
7574 else
7575 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7576 &unsignedp1);
7578 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7580 tree primop;
7581 HOST_WIDE_INT constant, mask;
7582 int unsignedp, bits;
7584 if (host_integerp (primop0, 0))
7586 primop = primop1;
7587 unsignedp = unsignedp1;
7588 constant = tree_low_cst (primop0, 0);
7590 else
7592 primop = primop0;
7593 unsignedp = unsignedp0;
7594 constant = tree_low_cst (primop1, 0);
7597 bits = TYPE_PRECISION (TREE_TYPE (primop));
7598 if (bits < TYPE_PRECISION (result_type)
7599 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7601 mask = (~ (HOST_WIDE_INT) 0) << bits;
7602 if ((mask & constant) != mask)
7603 warning ("comparison of promoted ~unsigned with constant");
7606 else if (unsignedp0 && unsignedp1
7607 && (TYPE_PRECISION (TREE_TYPE (primop0))
7608 < TYPE_PRECISION (result_type))
7609 && (TYPE_PRECISION (TREE_TYPE (primop1))
7610 < TYPE_PRECISION (result_type)))
7611 warning ("comparison of promoted ~unsigned with unsigned");
7617 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7618 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7619 Then the expression will be built.
7620 It will be given type FINAL_TYPE if that is nonzero;
7621 otherwise, it will be given type RESULT_TYPE. */
7623 if (!result_type)
7625 binary_op_error (code);
7626 return error_mark_node;
7629 if (! converted)
7631 if (TREE_TYPE (op0) != result_type)
7632 op0 = convert (result_type, op0);
7633 if (TREE_TYPE (op1) != result_type)
7634 op1 = convert (result_type, op1);
7636 /* This can happen if one operand has a vector type, and the other
7637 has a different type. */
7638 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7639 return error_mark_node;
7642 if (build_type == NULL_TREE)
7643 build_type = result_type;
7646 tree result = build2 (resultcode, build_type, op0, op1);
7648 /* Treat expressions in initializers specially as they can't trap. */
7649 result = require_constant_value ? fold_initializer (result)
7650 : fold (result);
7652 if (final_type != 0)
7653 result = convert (final_type, result);
7654 return result;