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
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
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
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 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
34 #include "coretypes.h"
38 #include "langhooks.h"
49 /* Nonzero if we've already printed a "missing braces around initializer"
50 message within this initializer. */
51 static int missing_braces_mentioned;
53 static tree qualify_type (tree, tree);
54 static int tagged_types_tu_compatible_p (tree, tree, int);
55 static int comp_target_types (tree, tree, int);
56 static int function_types_compatible_p (tree, tree, int);
57 static int type_lists_compatible_p (tree, tree, int);
58 static tree decl_constant_value_for_broken_optimization (tree);
59 static tree default_function_array_conversion (tree);
60 static tree lookup_field (tree, tree);
61 static tree convert_arguments (tree, tree, tree, tree);
62 static tree pointer_diff (tree, tree);
63 static tree internal_build_compound_expr (tree, int);
64 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
66 static void warn_for_assignment (const char *, const char *, tree, int);
67 static tree valid_compound_expr_initializer (tree, tree);
68 static void push_string (const char *);
69 static void push_member_name (tree);
70 static void push_array_bounds (int);
71 static int spelling_length (void);
72 static char *print_spelling (char *);
73 static void warning_init (const char *);
74 static tree digest_init (tree, tree, int);
75 static void output_init_element (tree, tree, tree, int);
76 static void output_pending_init_elements (int);
77 static int set_designator (int);
78 static void push_range_stack (tree);
79 static void add_pending_init (tree, tree);
80 static void set_nonincremental_init (void);
81 static void set_nonincremental_init_from_string (tree);
82 static tree find_init_member (tree);
84 /* Do `exp = require_complete_type (exp);' to make sure exp
85 does not have an incomplete type. (That includes void types.) */
88 require_complete_type (tree value)
90 tree type = TREE_TYPE (value);
92 if (value == error_mark_node || type == error_mark_node)
93 return error_mark_node;
95 /* First, detect a valid value with a complete type. */
96 if (COMPLETE_TYPE_P (type))
99 c_incomplete_type_error (value, type);
100 return error_mark_node;
103 /* Print an error message for invalid use of an incomplete type.
104 VALUE is the expression that was used (or 0 if that isn't known)
105 and TYPE is the type that was invalid. */
108 c_incomplete_type_error (tree value, tree type)
110 const char *type_code_string;
112 /* Avoid duplicate error message. */
113 if (TREE_CODE (type) == ERROR_MARK)
116 if (value != 0 && (TREE_CODE (value) == VAR_DECL
117 || TREE_CODE (value) == PARM_DECL))
118 error ("`%s' has an incomplete type",
119 IDENTIFIER_POINTER (DECL_NAME (value)));
123 /* We must print an error message. Be clever about what it says. */
125 switch (TREE_CODE (type))
128 type_code_string = "struct";
132 type_code_string = "union";
136 type_code_string = "enum";
140 error ("invalid use of void expression");
144 if (TYPE_DOMAIN (type))
146 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
148 error ("invalid use of flexible array member");
151 type = TREE_TYPE (type);
154 error ("invalid use of array with unspecified bounds");
161 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
162 error ("invalid use of undefined type `%s %s'",
163 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
165 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
166 error ("invalid use of incomplete typedef `%s'",
167 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
171 /* Given a type, apply default promotions wrt unnamed function
172 arguments and return the new type. */
175 c_type_promotes_to (tree type)
177 if (TYPE_MAIN_VARIANT (type) == float_type_node)
178 return double_type_node;
180 if (c_promoting_integer_type_p (type))
182 /* Preserve unsignedness if not really getting any wider. */
183 if (TYPE_UNSIGNED (type)
184 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
185 return unsigned_type_node;
186 return integer_type_node;
192 /* Return a variant of TYPE which has all the type qualifiers of LIKE
193 as well as those of TYPE. */
196 qualify_type (tree type, tree like)
198 return c_build_qualified_type (type,
199 TYPE_QUALS (type) | TYPE_QUALS (like));
202 /* Return the common type of two types.
203 We assume that comptypes has already been done and returned 1;
204 if that isn't so, this may crash. In particular, we assume that qualifiers
207 This is the type for the result of most arithmetic operations
208 if the operands have the given two types. */
211 common_type (tree t1, tree t2)
213 enum tree_code code1;
214 enum tree_code code2;
217 /* Save time if the two types are the same. */
219 if (t1 == t2) return t1;
221 /* If one type is nonsense, use the other. */
222 if (t1 == error_mark_node)
224 if (t2 == error_mark_node)
227 /* Merge the attributes. */
228 attributes = targetm.merge_type_attributes (t1, t2);
230 /* Treat an enum type as the unsigned integer type of the same width. */
232 if (TREE_CODE (t1) == ENUMERAL_TYPE)
233 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
234 if (TREE_CODE (t2) == ENUMERAL_TYPE)
235 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
237 code1 = TREE_CODE (t1);
238 code2 = TREE_CODE (t2);
240 /* If one type is complex, form the common type of the non-complex
241 components, then make that complex. Use T1 or T2 if it is the
243 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
245 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
246 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
247 tree subtype = common_type (subtype1, subtype2);
249 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
250 return build_type_attribute_variant (t1, attributes);
251 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
252 return build_type_attribute_variant (t2, attributes);
254 return build_type_attribute_variant (build_complex_type (subtype),
262 /* If only one is real, use it as the result. */
264 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
265 return build_type_attribute_variant (t1, attributes);
267 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
268 return build_type_attribute_variant (t2, attributes);
270 /* Both real or both integers; use the one with greater precision. */
272 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
273 return build_type_attribute_variant (t1, attributes);
274 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
275 return build_type_attribute_variant (t2, attributes);
277 /* Same precision. Prefer longs to ints even when same size. */
279 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
280 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
281 return build_type_attribute_variant (long_unsigned_type_node,
284 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
285 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
287 /* But preserve unsignedness from the other type,
288 since long cannot hold all the values of an unsigned int. */
289 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
290 t1 = long_unsigned_type_node;
292 t1 = long_integer_type_node;
293 return build_type_attribute_variant (t1, attributes);
296 /* Likewise, prefer long double to double even if same size. */
297 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
298 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
299 return build_type_attribute_variant (long_double_type_node,
302 /* Otherwise prefer the unsigned one. */
304 if (TYPE_UNSIGNED (t1))
305 return build_type_attribute_variant (t1, attributes);
307 return build_type_attribute_variant (t2, attributes);
310 /* For two pointers, do this recursively on the target type,
311 and combine the qualifiers of the two types' targets. */
312 /* This code was turned off; I don't know why.
313 But ANSI C specifies doing this with the qualifiers.
314 So I turned it on again. */
316 tree pointed_to_1 = TREE_TYPE (t1);
317 tree pointed_to_2 = TREE_TYPE (t2);
318 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
319 TYPE_MAIN_VARIANT (pointed_to_2));
320 t1 = build_pointer_type (c_build_qualified_type
322 TYPE_QUALS (pointed_to_1) |
323 TYPE_QUALS (pointed_to_2)));
324 return build_type_attribute_variant (t1, attributes);
329 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
330 /* Save space: see if the result is identical to one of the args. */
331 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
332 return build_type_attribute_variant (t1, attributes);
333 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
334 return build_type_attribute_variant (t2, attributes);
335 /* Merge the element types, and have a size if either arg has one. */
336 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
337 return build_type_attribute_variant (t1, attributes);
341 /* Function types: prefer the one that specified arg types.
342 If both do, merge the arg types. Also merge the return types. */
344 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
345 tree p1 = TYPE_ARG_TYPES (t1);
346 tree p2 = TYPE_ARG_TYPES (t2);
351 /* Save space: see if the result is identical to one of the args. */
352 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
353 return build_type_attribute_variant (t1, attributes);
354 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
355 return build_type_attribute_variant (t2, attributes);
357 /* Simple way if one arg fails to specify argument types. */
358 if (TYPE_ARG_TYPES (t1) == 0)
360 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
361 return build_type_attribute_variant (t1, attributes);
363 if (TYPE_ARG_TYPES (t2) == 0)
365 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
366 return build_type_attribute_variant (t1, attributes);
369 /* If both args specify argument types, we must merge the two
370 lists, argument by argument. */
371 /* Tell global_bindings_p to return false so that variable_size
372 doesn't abort on VLAs in parameter types. */
373 c_override_global_bindings_to_false = true;
375 len = list_length (p1);
378 for (i = 0; i < len; i++)
379 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
384 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
386 /* A null type means arg type is not specified.
387 Take whatever the other function type has. */
388 if (TREE_VALUE (p1) == 0)
390 TREE_VALUE (n) = TREE_VALUE (p2);
393 if (TREE_VALUE (p2) == 0)
395 TREE_VALUE (n) = TREE_VALUE (p1);
399 /* Given wait (union {union wait *u; int *i} *)
400 and wait (union wait *),
401 prefer union wait * as type of parm. */
402 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
403 && TREE_VALUE (p1) != TREE_VALUE (p2))
406 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
407 memb; memb = TREE_CHAIN (memb))
408 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
411 TREE_VALUE (n) = TREE_VALUE (p2);
413 pedwarn ("function types not truly compatible in ISO C");
417 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
418 && TREE_VALUE (p2) != TREE_VALUE (p1))
421 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
422 memb; memb = TREE_CHAIN (memb))
423 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
426 TREE_VALUE (n) = TREE_VALUE (p1);
428 pedwarn ("function types not truly compatible in ISO C");
432 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
436 c_override_global_bindings_to_false = false;
437 t1 = build_function_type (valtype, newargs);
438 /* ... falls through ... */
442 return build_type_attribute_variant (t1, attributes);
447 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
448 or various other operations. Return 2 if they are compatible
449 but a warning may be needed if you use them together. */
452 comptypes (tree type1, tree type2, int flags)
458 /* Suppress errors caused by previously reported errors. */
460 if (t1 == t2 || !t1 || !t2
461 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
464 /* If either type is the internal version of sizetype, return the
466 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
467 && TYPE_ORIG_SIZE_TYPE (t1))
468 t1 = TYPE_ORIG_SIZE_TYPE (t1);
470 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
471 && TYPE_ORIG_SIZE_TYPE (t2))
472 t2 = TYPE_ORIG_SIZE_TYPE (t2);
475 /* Enumerated types are compatible with integer types, but this is
476 not transitive: two enumerated types in the same translation unit
477 are compatible with each other only if they are the same type. */
479 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
480 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
481 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
482 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
487 /* Different classes of types can't be compatible. */
489 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
491 /* Qualifiers must match. */
493 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
496 /* Allow for two different type nodes which have essentially the same
497 definition. Note that we already checked for equality of the type
498 qualifiers (just above). */
500 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
504 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
507 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
510 switch (TREE_CODE (t1))
513 /* We must give ObjC the first crack at comparing pointers, since
514 protocol qualifiers may be involved. */
515 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
517 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
518 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
522 val = function_types_compatible_p (t1, t2, flags);
527 tree d1 = TYPE_DOMAIN (t1);
528 tree d2 = TYPE_DOMAIN (t2);
529 bool d1_variable, d2_variable;
530 bool d1_zero, d2_zero;
533 /* Target types must match incl. qualifiers. */
534 if (TREE_TYPE (t1) != TREE_TYPE (t2)
535 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
539 /* Sizes must match unless one is missing or variable. */
540 if (d1 == 0 || d2 == 0 || d1 == d2)
543 d1_zero = ! TYPE_MAX_VALUE (d1);
544 d2_zero = ! TYPE_MAX_VALUE (d2);
546 d1_variable = (! d1_zero
547 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
548 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
549 d2_variable = (! d2_zero
550 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
551 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
553 if (d1_variable || d2_variable)
555 if (d1_zero && d2_zero)
557 if (d1_zero || d2_zero
558 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
559 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
566 /* We are dealing with two distinct structs. In assorted Objective-C
567 corner cases, however, these can still be deemed equivalent. */
568 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
573 if (val != 1 && !same_translation_unit_p (t1, t2))
574 val = tagged_types_tu_compatible_p (t1, t2, flags);
578 /* The target might allow certain vector types to be compatible. */
579 val = targetm.vector_opaque_p (t1)
580 || targetm.vector_opaque_p (t2)
581 || TYPE_MODE (t1) == TYPE_MODE (t2);
587 return attrval == 2 && val == 1 ? 2 : val;
590 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
591 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
592 to 1 or 0 depending if the check of the pointer types is meant to
593 be reflexive or not (typically, assignments are not reflexive,
594 while comparisons are reflexive).
598 comp_target_types (tree ttl, tree ttr, int reflexive)
602 /* Give objc_comptypes a crack at letting these types through. */
603 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
606 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
607 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
609 if (val == 2 && pedantic)
610 pedwarn ("types are not quite compatible");
614 /* Subroutines of `comptypes'. */
616 /* Determine whether two trees derive from the same translation unit.
617 If the CONTEXT chain ends in a null, that tree's context is still
618 being parsed, so if two trees have context chains ending in null,
619 they're in the same translation unit. */
621 same_translation_unit_p (tree t1, tree t2)
623 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
624 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
626 case 'd': t1 = DECL_CONTEXT (t1); break;
627 case 't': t1 = TYPE_CONTEXT (t1); break;
628 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
632 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
633 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
635 case 'd': t2 = DECL_CONTEXT (t2); break;
636 case 't': t2 = TYPE_CONTEXT (t2); break;
637 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
644 /* The C standard says that two structures in different translation
645 units are compatible with each other only if the types of their
646 fields are compatible (among other things). So, consider two copies
647 of this structure: */
649 struct tagged_tu_seen {
650 const struct tagged_tu_seen * next;
655 /* Can they be compatible with each other? We choose to break the
656 recursion by allowing those types to be compatible. */
658 static const struct tagged_tu_seen * tagged_tu_seen_base;
660 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
661 compatible. If the two types are not the same (which has been
662 checked earlier), this can only happen when multiple translation
663 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
667 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
670 bool needs_warning = false;
672 /* We have to verify that the tags of the types are the same. This
673 is harder than it looks because this may be a typedef, so we have
674 to go look at the original type. It may even be a typedef of a
676 while (TYPE_NAME (t1)
677 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
678 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
679 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
681 while (TYPE_NAME (t2)
682 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
683 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
684 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
686 /* C90 didn't have the requirement that the two tags be the same. */
687 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
690 /* C90 didn't say what happened if one or both of the types were
691 incomplete; we choose to follow C99 rules here, which is that they
693 if (TYPE_SIZE (t1) == NULL
694 || TYPE_SIZE (t2) == NULL)
698 const struct tagged_tu_seen * tts_i;
699 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
700 if (tts_i->t1 == t1 && tts_i->t2 == t2)
704 switch (TREE_CODE (t1))
709 /* Speed up the case where the type values are in the same order. */
710 tree tv1 = TYPE_VALUES (t1);
711 tree tv2 = TYPE_VALUES (t2);
716 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
718 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
720 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
724 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
726 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
729 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
732 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
734 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
736 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
744 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
747 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
750 struct tagged_tu_seen tts;
752 tts.next = tagged_tu_seen_base;
755 tagged_tu_seen_base = &tts;
757 if (DECL_NAME (s1) != NULL)
758 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
759 if (DECL_NAME (s1) == DECL_NAME (s2))
762 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
766 needs_warning = true;
768 if (TREE_CODE (s1) == FIELD_DECL
769 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
770 DECL_FIELD_BIT_OFFSET (s2)) != 1)
776 tagged_tu_seen_base = tts.next;
780 return needs_warning ? 2 : 1;
785 struct tagged_tu_seen tts;
787 tts.next = tagged_tu_seen_base;
790 tagged_tu_seen_base = &tts;
792 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
794 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
797 if (TREE_CODE (s1) != TREE_CODE (s2)
798 || DECL_NAME (s1) != DECL_NAME (s2))
800 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
804 needs_warning = true;
806 if (TREE_CODE (s1) == FIELD_DECL
807 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
808 DECL_FIELD_BIT_OFFSET (s2)) != 1)
811 tagged_tu_seen_base = tts.next;
814 return needs_warning ? 2 : 1;
822 /* Return 1 if two function types F1 and F2 are compatible.
823 If either type specifies no argument types,
824 the other must specify a fixed number of self-promoting arg types.
825 Otherwise, if one type specifies only the number of arguments,
826 the other must specify that number of self-promoting arg types.
827 Otherwise, the argument types must match. */
830 function_types_compatible_p (tree f1, tree f2, int flags)
833 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
838 ret1 = TREE_TYPE (f1);
839 ret2 = TREE_TYPE (f2);
841 /* 'volatile' qualifiers on a function's return type mean the function
843 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
844 pedwarn ("function return types not compatible due to `volatile'");
845 if (TYPE_VOLATILE (ret1))
846 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
847 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
848 if (TYPE_VOLATILE (ret2))
849 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
850 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
851 val = comptypes (ret1, ret2, flags);
855 args1 = TYPE_ARG_TYPES (f1);
856 args2 = TYPE_ARG_TYPES (f2);
858 /* An unspecified parmlist matches any specified parmlist
859 whose argument types don't need default promotions. */
863 if (!self_promoting_args_p (args2))
865 /* If one of these types comes from a non-prototype fn definition,
866 compare that with the other type's arglist.
867 If they don't match, ask for a warning (but no error). */
868 if (TYPE_ACTUAL_ARG_TYPES (f1)
869 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
876 if (!self_promoting_args_p (args1))
878 if (TYPE_ACTUAL_ARG_TYPES (f2)
879 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
885 /* Both types have argument lists: compare them and propagate results. */
886 val1 = type_lists_compatible_p (args1, args2, flags);
887 return val1 != 1 ? val1 : val;
890 /* Check two lists of types for compatibility,
891 returning 0 for incompatible, 1 for compatible,
892 or 2 for compatible with warning. */
895 type_lists_compatible_p (tree args1, tree args2, int flags)
897 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
903 if (args1 == 0 && args2 == 0)
905 /* If one list is shorter than the other,
906 they fail to match. */
907 if (args1 == 0 || args2 == 0)
909 /* A null pointer instead of a type
910 means there is supposed to be an argument
911 but nothing is specified about what type it has.
912 So match anything that self-promotes. */
913 if (TREE_VALUE (args1) == 0)
915 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
918 else if (TREE_VALUE (args2) == 0)
920 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
923 /* If one of the lists has an error marker, ignore this arg. */
924 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
925 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
927 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
928 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
931 /* Allow wait (union {union wait *u; int *i} *)
932 and wait (union wait *) to be compatible. */
933 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
934 && (TYPE_NAME (TREE_VALUE (args1)) == 0
935 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
936 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
937 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
938 TYPE_SIZE (TREE_VALUE (args2))))
941 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
942 memb; memb = TREE_CHAIN (memb))
943 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
949 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
950 && (TYPE_NAME (TREE_VALUE (args2)) == 0
951 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
952 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
953 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
954 TYPE_SIZE (TREE_VALUE (args1))))
957 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
958 memb; memb = TREE_CHAIN (memb))
959 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
969 /* comptypes said ok, but record if it said to warn. */
973 args1 = TREE_CHAIN (args1);
974 args2 = TREE_CHAIN (args2);
978 /* Compute the size to increment a pointer by. */
981 c_size_in_bytes (tree type)
983 enum tree_code code = TREE_CODE (type);
985 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
986 return size_one_node;
988 if (!COMPLETE_OR_VOID_TYPE_P (type))
990 error ("arithmetic on pointer to an incomplete type");
991 return size_one_node;
994 /* Convert in case a char is more than one unit. */
995 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
996 size_int (TYPE_PRECISION (char_type_node)
1000 /* Return either DECL or its known constant value (if it has one). */
1003 decl_constant_value (tree decl)
1005 if (/* Don't change a variable array bound or initial value to a constant
1006 in a place where a variable is invalid. Note that DECL_INITIAL
1007 isn't valid for a PARM_DECL. */
1008 current_function_decl != 0
1009 && TREE_CODE (decl) != PARM_DECL
1010 && ! TREE_THIS_VOLATILE (decl)
1011 && TREE_READONLY (decl)
1012 && DECL_INITIAL (decl) != 0
1013 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1014 /* This is invalid if initial value is not constant.
1015 If it has either a function call, a memory reference,
1016 or a variable, then re-evaluating it could give different results. */
1017 && TREE_CONSTANT (DECL_INITIAL (decl))
1018 /* Check for cases where this is sub-optimal, even though valid. */
1019 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1020 return DECL_INITIAL (decl);
1024 /* Return either DECL or its known constant value (if it has one), but
1025 return DECL if pedantic or DECL has mode BLKmode. This is for
1026 bug-compatibility with the old behavior of decl_constant_value
1027 (before GCC 3.0); every use of this function is a bug and it should
1028 be removed before GCC 3.1. It is not appropriate to use pedantic
1029 in a way that affects optimization, and BLKmode is probably not the
1030 right test for avoiding misoptimizations either. */
1033 decl_constant_value_for_broken_optimization (tree decl)
1035 if (pedantic || DECL_MODE (decl) == BLKmode)
1038 return decl_constant_value (decl);
1042 /* Perform the default conversion of arrays and functions to pointers.
1043 Return the result of converting EXP. For any other expression, just
1047 default_function_array_conversion (tree exp)
1050 tree type = TREE_TYPE (exp);
1051 enum tree_code code = TREE_CODE (type);
1054 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1057 Do not use STRIP_NOPS here! It will remove conversions from pointer
1058 to integer and cause infinite recursion. */
1060 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1061 || (TREE_CODE (exp) == NOP_EXPR
1062 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1064 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1066 exp = TREE_OPERAND (exp, 0);
1069 /* Preserve the original expression code. */
1070 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1071 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1073 if (code == FUNCTION_TYPE)
1075 return build_unary_op (ADDR_EXPR, exp, 0);
1077 if (code == ARRAY_TYPE)
1080 tree restype = TREE_TYPE (type);
1086 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1088 constp = TREE_READONLY (exp);
1089 volatilep = TREE_THIS_VOLATILE (exp);
1092 if (TYPE_QUALS (type) || constp || volatilep)
1094 = c_build_qualified_type (restype,
1096 | (constp * TYPE_QUAL_CONST)
1097 | (volatilep * TYPE_QUAL_VOLATILE));
1099 if (TREE_CODE (exp) == INDIRECT_REF)
1100 return convert (TYPE_POINTER_TO (restype),
1101 TREE_OPERAND (exp, 0));
1103 if (TREE_CODE (exp) == COMPOUND_EXPR)
1105 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1106 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1107 TREE_OPERAND (exp, 0), op1);
1110 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1111 if (!flag_isoc99 && !lvalue_array_p)
1113 /* Before C99, non-lvalue arrays do not decay to pointers.
1114 Normally, using such an array would be invalid; but it can
1115 be used correctly inside sizeof or as a statement expression.
1116 Thus, do not give an error here; an error will result later. */
1120 ptrtype = build_pointer_type (restype);
1122 if (TREE_CODE (exp) == VAR_DECL)
1124 /* ??? This is not really quite correct
1125 in that the type of the operand of ADDR_EXPR
1126 is not the target type of the type of the ADDR_EXPR itself.
1127 Question is, can this lossage be avoided? */
1128 adr = build1 (ADDR_EXPR, ptrtype, exp);
1129 if (!c_mark_addressable (exp))
1130 return error_mark_node;
1131 TREE_CONSTANT (adr) = staticp (exp);
1132 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1135 /* This way is better for a COMPONENT_REF since it can
1136 simplify the offset for a component. */
1137 adr = build_unary_op (ADDR_EXPR, exp, 1);
1138 return convert (ptrtype, adr);
1143 /* Perform default promotions for C data used in expressions.
1144 Arrays and functions are converted to pointers;
1145 enumeral types or short or char, to int.
1146 In addition, manifest constants symbols are replaced by their values. */
1149 default_conversion (tree exp)
1152 tree type = TREE_TYPE (exp);
1153 enum tree_code code = TREE_CODE (type);
1155 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1156 return default_function_array_conversion (exp);
1158 /* Constants can be used directly unless they're not loadable. */
1159 if (TREE_CODE (exp) == CONST_DECL)
1160 exp = DECL_INITIAL (exp);
1162 /* Replace a nonvolatile const static variable with its value unless
1163 it is an array, in which case we must be sure that taking the
1164 address of the array produces consistent results. */
1165 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1167 exp = decl_constant_value_for_broken_optimization (exp);
1168 type = TREE_TYPE (exp);
1171 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1174 Do not use STRIP_NOPS here! It will remove conversions from pointer
1175 to integer and cause infinite recursion. */
1177 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1178 || (TREE_CODE (exp) == NOP_EXPR
1179 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1180 exp = TREE_OPERAND (exp, 0);
1182 /* Preserve the original expression code. */
1183 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1184 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1186 /* Normally convert enums to int,
1187 but convert wide enums to something wider. */
1188 if (code == ENUMERAL_TYPE)
1190 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1191 TYPE_PRECISION (integer_type_node)),
1192 ((TYPE_PRECISION (type)
1193 >= TYPE_PRECISION (integer_type_node))
1194 && TYPE_UNSIGNED (type)));
1196 return convert (type, exp);
1199 if (TREE_CODE (exp) == COMPONENT_REF
1200 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1201 /* If it's thinner than an int, promote it like a
1202 c_promoting_integer_type_p, otherwise leave it alone. */
1203 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1204 TYPE_PRECISION (integer_type_node)))
1205 return convert (integer_type_node, exp);
1207 if (c_promoting_integer_type_p (type))
1209 /* Preserve unsignedness if not really getting any wider. */
1210 if (TYPE_UNSIGNED (type)
1211 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1212 return convert (unsigned_type_node, exp);
1214 return convert (integer_type_node, exp);
1217 if (code == VOID_TYPE)
1219 error ("void value not ignored as it ought to be");
1220 return error_mark_node;
1225 /* Look up COMPONENT in a structure or union DECL.
1227 If the component name is not found, returns NULL_TREE. Otherwise,
1228 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1229 stepping down the chain to the component, which is in the last
1230 TREE_VALUE of the list. Normally the list is of length one, but if
1231 the component is embedded within (nested) anonymous structures or
1232 unions, the list steps down the chain to the component. */
1235 lookup_field (tree decl, tree component)
1237 tree type = TREE_TYPE (decl);
1240 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1241 to the field elements. Use a binary search on this array to quickly
1242 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1243 will always be set for structures which have many elements. */
1245 if (TYPE_LANG_SPECIFIC (type))
1248 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1250 field = TYPE_FIELDS (type);
1252 top = TYPE_LANG_SPECIFIC (type)->s->len;
1253 while (top - bot > 1)
1255 half = (top - bot + 1) >> 1;
1256 field = field_array[bot+half];
1258 if (DECL_NAME (field) == NULL_TREE)
1260 /* Step through all anon unions in linear fashion. */
1261 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1263 field = field_array[bot++];
1264 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1265 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1267 tree anon = lookup_field (field, component);
1270 return tree_cons (NULL_TREE, field, anon);
1274 /* Entire record is only anon unions. */
1278 /* Restart the binary search, with new lower bound. */
1282 if (DECL_NAME (field) == component)
1284 if (DECL_NAME (field) < component)
1290 if (DECL_NAME (field_array[bot]) == component)
1291 field = field_array[bot];
1292 else if (DECL_NAME (field) != component)
1297 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1299 if (DECL_NAME (field) == NULL_TREE
1300 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1301 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1303 tree anon = lookup_field (field, component);
1306 return tree_cons (NULL_TREE, field, anon);
1309 if (DECL_NAME (field) == component)
1313 if (field == NULL_TREE)
1317 return tree_cons (NULL_TREE, field, NULL_TREE);
1320 /* Make an expression to refer to the COMPONENT field of
1321 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1324 build_component_ref (tree datum, tree component)
1326 tree type = TREE_TYPE (datum);
1327 enum tree_code code = TREE_CODE (type);
1331 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1332 Ensure that the arguments are not lvalues; otherwise,
1333 if the component is an array, it would wrongly decay to a pointer in
1335 We cannot do this with a COND_EXPR, because in a conditional expression
1336 the default promotions are applied to both sides, and this would yield
1337 the wrong type of the result; for example, if the components have
1339 switch (TREE_CODE (datum))
1343 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1344 return build (COMPOUND_EXPR, TREE_TYPE (value),
1345 TREE_OPERAND (datum, 0), non_lvalue (value));
1351 /* See if there is a field or component with name COMPONENT. */
1353 if (code == RECORD_TYPE || code == UNION_TYPE)
1355 if (!COMPLETE_TYPE_P (type))
1357 c_incomplete_type_error (NULL_TREE, type);
1358 return error_mark_node;
1361 field = lookup_field (datum, component);
1365 error ("%s has no member named `%s'",
1366 code == RECORD_TYPE ? "structure" : "union",
1367 IDENTIFIER_POINTER (component));
1368 return error_mark_node;
1371 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1372 This might be better solved in future the way the C++ front
1373 end does it - by giving the anonymous entities each a
1374 separate name and type, and then have build_component_ref
1375 recursively call itself. We can't do that here. */
1378 tree subdatum = TREE_VALUE (field);
1380 if (TREE_TYPE (subdatum) == error_mark_node)
1381 return error_mark_node;
1383 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1384 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1385 TREE_READONLY (ref) = 1;
1386 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1387 TREE_THIS_VOLATILE (ref) = 1;
1389 if (TREE_DEPRECATED (subdatum))
1390 warn_deprecated_use (subdatum);
1394 field = TREE_CHAIN (field);
1400 else if (code != ERROR_MARK)
1401 error ("request for member `%s' in something not a structure or union",
1402 IDENTIFIER_POINTER (component));
1404 return error_mark_node;
1407 /* Given an expression PTR for a pointer, return an expression
1408 for the value pointed to.
1409 ERRORSTRING is the name of the operator to appear in error messages. */
1412 build_indirect_ref (tree ptr, const char *errorstring)
1414 tree pointer = default_conversion (ptr);
1415 tree type = TREE_TYPE (pointer);
1417 if (TREE_CODE (type) == POINTER_TYPE)
1419 if (TREE_CODE (pointer) == ADDR_EXPR
1420 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1421 == TREE_TYPE (type)))
1422 return TREE_OPERAND (pointer, 0);
1425 tree t = TREE_TYPE (type);
1426 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1428 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1430 error ("dereferencing pointer to incomplete type");
1431 return error_mark_node;
1433 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1434 warning ("dereferencing `void *' pointer");
1436 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1437 so that we get the proper error message if the result is used
1438 to assign to. Also, &* is supposed to be a no-op.
1439 And ANSI C seems to specify that the type of the result
1440 should be the const type. */
1441 /* A de-reference of a pointer to const is not a const. It is valid
1442 to change it via some other pointer. */
1443 TREE_READONLY (ref) = TYPE_READONLY (t);
1444 TREE_SIDE_EFFECTS (ref)
1445 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1446 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1450 else if (TREE_CODE (pointer) != ERROR_MARK)
1451 error ("invalid type argument of `%s'", errorstring);
1452 return error_mark_node;
1455 /* This handles expressions of the form "a[i]", which denotes
1458 This is logically equivalent in C to *(a+i), but we may do it differently.
1459 If A is a variable or a member, we generate a primitive ARRAY_REF.
1460 This avoids forcing the array out of registers, and can work on
1461 arrays that are not lvalues (for example, members of structures returned
1465 build_array_ref (tree array, tree index)
1469 error ("subscript missing in array reference");
1470 return error_mark_node;
1473 if (TREE_TYPE (array) == error_mark_node
1474 || TREE_TYPE (index) == error_mark_node)
1475 return error_mark_node;
1477 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1478 && TREE_CODE (array) != INDIRECT_REF)
1482 /* Subscripting with type char is likely to lose
1483 on a machine where chars are signed.
1484 So warn on any machine, but optionally.
1485 Don't warn for unsigned char since that type is safe.
1486 Don't warn for signed char because anyone who uses that
1487 must have done so deliberately. */
1488 if (warn_char_subscripts
1489 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1490 warning ("array subscript has type `char'");
1492 /* Apply default promotions *after* noticing character types. */
1493 index = default_conversion (index);
1495 /* Require integer *after* promotion, for sake of enums. */
1496 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1498 error ("array subscript is not an integer");
1499 return error_mark_node;
1502 /* An array that is indexed by a non-constant
1503 cannot be stored in a register; we must be able to do
1504 address arithmetic on its address.
1505 Likewise an array of elements of variable size. */
1506 if (TREE_CODE (index) != INTEGER_CST
1507 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1508 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1510 if (!c_mark_addressable (array))
1511 return error_mark_node;
1513 /* An array that is indexed by a constant value which is not within
1514 the array bounds cannot be stored in a register either; because we
1515 would get a crash in store_bit_field/extract_bit_field when trying
1516 to access a non-existent part of the register. */
1517 if (TREE_CODE (index) == INTEGER_CST
1518 && TYPE_DOMAIN (TREE_TYPE (array))
1519 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1521 if (!c_mark_addressable (array))
1522 return error_mark_node;
1528 while (TREE_CODE (foo) == COMPONENT_REF)
1529 foo = TREE_OPERAND (foo, 0);
1530 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1531 pedwarn ("ISO C forbids subscripting `register' array");
1532 else if (! flag_isoc99 && ! lvalue_p (foo))
1533 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1536 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1537 rval = build (ARRAY_REF, type, array, index);
1538 /* Array ref is const/volatile if the array elements are
1539 or if the array is. */
1540 TREE_READONLY (rval)
1541 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1542 | TREE_READONLY (array));
1543 TREE_SIDE_EFFECTS (rval)
1544 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1545 | TREE_SIDE_EFFECTS (array));
1546 TREE_THIS_VOLATILE (rval)
1547 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1548 /* This was added by rms on 16 Nov 91.
1549 It fixes vol struct foo *a; a->elts[1]
1550 in an inline function.
1551 Hope it doesn't break something else. */
1552 | TREE_THIS_VOLATILE (array));
1553 return require_complete_type (fold (rval));
1557 tree ar = default_conversion (array);
1558 tree ind = default_conversion (index);
1560 /* Do the same warning check as above, but only on the part that's
1561 syntactically the index and only if it is also semantically
1563 if (warn_char_subscripts
1564 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1565 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1566 warning ("subscript has type `char'");
1568 /* Put the integer in IND to simplify error checking. */
1569 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1576 if (ar == error_mark_node)
1579 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1580 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1582 error ("subscripted value is neither array nor pointer");
1583 return error_mark_node;
1585 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1587 error ("array subscript is not an integer");
1588 return error_mark_node;
1591 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1596 /* Build an external reference to identifier ID. FUN indicates
1597 whether this will be used for a function call. */
1599 build_external_ref (tree id, int fun)
1602 tree decl = lookup_name (id);
1603 tree objc_ivar = lookup_objc_ivar (id);
1605 if (decl && decl != error_mark_node)
1607 /* Properly declared variable or function reference. */
1610 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1612 warning ("local declaration of `%s' hides instance variable",
1613 IDENTIFIER_POINTER (id));
1622 /* Implicit function declaration. */
1623 ref = implicitly_declare (id);
1624 else if (decl == error_mark_node)
1625 /* Don't complain about something that's already been
1626 complained about. */
1627 return error_mark_node;
1630 undeclared_variable (id);
1631 return error_mark_node;
1634 if (TREE_TYPE (ref) == error_mark_node)
1635 return error_mark_node;
1637 if (TREE_DEPRECATED (ref))
1638 warn_deprecated_use (ref);
1640 if (!skip_evaluation)
1641 assemble_external (ref);
1642 TREE_USED (ref) = 1;
1644 if (TREE_CODE (ref) == CONST_DECL)
1646 ref = DECL_INITIAL (ref);
1647 TREE_CONSTANT (ref) = 1;
1649 else if (current_function_decl != 0
1650 && !DECL_FILE_SCOPE_P (current_function_decl)
1651 && (TREE_CODE (ref) == VAR_DECL
1652 || TREE_CODE (ref) == PARM_DECL
1653 || TREE_CODE (ref) == FUNCTION_DECL))
1655 tree context = decl_function_context (ref);
1657 if (context != 0 && context != current_function_decl)
1658 DECL_NONLOCAL (ref) = 1;
1664 /* Build a function call to function FUNCTION with parameters PARAMS.
1665 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1666 TREE_VALUE of each node is a parameter-expression.
1667 FUNCTION's data type may be a function type or a pointer-to-function. */
1670 build_function_call (tree function, tree params)
1672 tree fntype, fundecl = 0;
1673 tree coerced_params;
1674 tree name = NULL_TREE, result;
1677 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1678 STRIP_TYPE_NOPS (function);
1680 /* Convert anything with function type to a pointer-to-function. */
1681 if (TREE_CODE (function) == FUNCTION_DECL)
1683 name = DECL_NAME (function);
1685 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1686 (because calling an inline function does not mean the function
1687 needs to be separately compiled). */
1688 fntype = build_type_variant (TREE_TYPE (function),
1689 TREE_READONLY (function),
1690 TREE_THIS_VOLATILE (function));
1692 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1695 function = default_conversion (function);
1697 fntype = TREE_TYPE (function);
1699 if (TREE_CODE (fntype) == ERROR_MARK)
1700 return error_mark_node;
1702 if (!(TREE_CODE (fntype) == POINTER_TYPE
1703 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1705 error ("called object is not a function");
1706 return error_mark_node;
1709 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1710 current_function_returns_abnormally = 1;
1712 /* fntype now gets the type of function pointed to. */
1713 fntype = TREE_TYPE (fntype);
1715 /* Check that the function is called through a compatible prototype.
1716 If it is not, replace the call by a trap, wrapped up in a compound
1717 expression if necessary. This has the nice side-effect to prevent
1718 the tree-inliner from generating invalid assignment trees which may
1719 blow up in the RTL expander later.
1721 ??? This doesn't work for Objective-C because objc_comptypes
1722 refuses to compare function prototypes, yet the compiler appears
1723 to build calls that are flagged as invalid by C's comptypes. */
1724 if (! c_dialect_objc ()
1725 && TREE_CODE (function) == NOP_EXPR
1726 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1727 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1728 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1730 tree return_type = TREE_TYPE (fntype);
1731 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1734 /* This situation leads to run-time undefined behavior. We can't,
1735 therefore, simply error unless we can prove that all possible
1736 executions of the program must execute the code. */
1737 warning ("function called through a non-compatible type");
1739 /* We can, however, treat "undefined" any way we please.
1740 Call abort to encourage the user to fix the program. */
1741 inform ("if this code is reached, the program will abort");
1743 if (VOID_TYPE_P (return_type))
1749 if (AGGREGATE_TYPE_P (return_type))
1750 rhs = build_compound_literal (return_type,
1751 build_constructor (return_type,
1754 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1756 return build (COMPOUND_EXPR, return_type, trap, rhs);
1760 /* Convert the parameters to the types declared in the
1761 function prototype, or apply default promotions. */
1764 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1766 /* Check that the arguments to the function are valid. */
1768 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1770 /* Recognize certain built-in functions so we can make tree-codes
1771 other than CALL_EXPR. We do this when it enables fold-const.c
1772 to do something useful. */
1774 if (TREE_CODE (function) == ADDR_EXPR
1775 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1776 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1778 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1779 params, coerced_params);
1784 result = build (CALL_EXPR, TREE_TYPE (fntype),
1785 function, coerced_params, NULL_TREE);
1786 TREE_SIDE_EFFECTS (result) = 1;
1787 result = fold (result);
1789 if (VOID_TYPE_P (TREE_TYPE (result)))
1791 return require_complete_type (result);
1794 /* Convert the argument expressions in the list VALUES
1795 to the types in the list TYPELIST. The result is a list of converted
1796 argument expressions.
1798 If TYPELIST is exhausted, or when an element has NULL as its type,
1799 perform the default conversions.
1801 PARMLIST is the chain of parm decls for the function being called.
1802 It may be 0, if that info is not available.
1803 It is used only for generating error messages.
1805 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1807 This is also where warnings about wrong number of args are generated.
1809 Both VALUES and the returned value are chains of TREE_LIST nodes
1810 with the elements of the list in the TREE_VALUE slots of those nodes. */
1813 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1815 tree typetail, valtail;
1819 /* Scan the given expressions and types, producing individual
1820 converted arguments and pushing them on RESULT in reverse order. */
1822 for (valtail = values, typetail = typelist, parmnum = 0;
1824 valtail = TREE_CHAIN (valtail), parmnum++)
1826 tree type = typetail ? TREE_VALUE (typetail) : 0;
1827 tree val = TREE_VALUE (valtail);
1829 if (type == void_type_node)
1832 error ("too many arguments to function `%s'",
1833 IDENTIFIER_POINTER (name));
1835 error ("too many arguments to function");
1839 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1840 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1841 to convert automatically to a pointer. */
1842 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1843 val = TREE_OPERAND (val, 0);
1845 val = default_function_array_conversion (val);
1847 val = require_complete_type (val);
1851 /* Formal parm type is specified by a function prototype. */
1854 if (!COMPLETE_TYPE_P (type))
1856 error ("type of formal parameter %d is incomplete", parmnum + 1);
1861 /* Optionally warn about conversions that
1862 differ from the default conversions. */
1863 if (warn_conversion || warn_traditional)
1865 int formal_prec = TYPE_PRECISION (type);
1867 if (INTEGRAL_TYPE_P (type)
1868 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1869 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1870 if (INTEGRAL_TYPE_P (type)
1871 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1872 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1873 else if (TREE_CODE (type) == COMPLEX_TYPE
1874 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1875 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1876 else if (TREE_CODE (type) == REAL_TYPE
1877 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1878 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1879 else if (TREE_CODE (type) == COMPLEX_TYPE
1880 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1881 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1882 else if (TREE_CODE (type) == REAL_TYPE
1883 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1884 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1885 /* ??? At some point, messages should be written about
1886 conversions between complex types, but that's too messy
1888 else if (TREE_CODE (type) == REAL_TYPE
1889 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1891 /* Warn if any argument is passed as `float',
1892 since without a prototype it would be `double'. */
1893 if (formal_prec == TYPE_PRECISION (float_type_node))
1894 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1896 /* Detect integer changing in width or signedness.
1897 These warnings are only activated with
1898 -Wconversion, not with -Wtraditional. */
1899 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1900 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1902 tree would_have_been = default_conversion (val);
1903 tree type1 = TREE_TYPE (would_have_been);
1905 if (TREE_CODE (type) == ENUMERAL_TYPE
1906 && (TYPE_MAIN_VARIANT (type)
1907 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1908 /* No warning if function asks for enum
1909 and the actual arg is that enum type. */
1911 else if (formal_prec != TYPE_PRECISION (type1))
1912 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1913 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
1915 /* Don't complain if the formal parameter type
1916 is an enum, because we can't tell now whether
1917 the value was an enum--even the same enum. */
1918 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1920 else if (TREE_CODE (val) == INTEGER_CST
1921 && int_fits_type_p (val, type))
1922 /* Change in signedness doesn't matter
1923 if a constant value is unaffected. */
1925 /* Likewise for a constant in a NOP_EXPR. */
1926 else if (TREE_CODE (val) == NOP_EXPR
1927 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1928 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1930 /* If the value is extended from a narrower
1931 unsigned type, it doesn't matter whether we
1932 pass it as signed or unsigned; the value
1933 certainly is the same either way. */
1934 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1935 && TYPE_UNSIGNED (TREE_TYPE (val)))
1937 else if (TYPE_UNSIGNED (type))
1938 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1940 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1944 parmval = convert_for_assignment (type, val,
1945 (char *) 0, /* arg passing */
1946 fundecl, name, parmnum + 1);
1948 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1949 && INTEGRAL_TYPE_P (type)
1950 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1951 parmval = default_conversion (parmval);
1953 result = tree_cons (NULL_TREE, parmval, result);
1955 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1956 && (TYPE_PRECISION (TREE_TYPE (val))
1957 < TYPE_PRECISION (double_type_node)))
1958 /* Convert `float' to `double'. */
1959 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1961 /* Convert `short' and `char' to full-size `int'. */
1962 result = tree_cons (NULL_TREE, default_conversion (val), result);
1965 typetail = TREE_CHAIN (typetail);
1968 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1971 error ("too few arguments to function `%s'",
1972 IDENTIFIER_POINTER (name));
1974 error ("too few arguments to function");
1977 return nreverse (result);
1980 /* This is the entry point used by the parser
1981 for binary operators in the input.
1982 In addition to constructing the expression,
1983 we check for operands that were written with other binary operators
1984 in a way that is likely to confuse the user. */
1987 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1989 tree result = build_binary_op (code, arg1, arg2, 1);
1992 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1993 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1994 enum tree_code code1 = ERROR_MARK;
1995 enum tree_code code2 = ERROR_MARK;
1997 if (TREE_CODE (result) == ERROR_MARK)
1998 return error_mark_node;
2000 if (IS_EXPR_CODE_CLASS (class1))
2001 code1 = C_EXP_ORIGINAL_CODE (arg1);
2002 if (IS_EXPR_CODE_CLASS (class2))
2003 code2 = C_EXP_ORIGINAL_CODE (arg2);
2005 /* Check for cases such as x+y<<z which users are likely
2006 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2007 is cleared to prevent these warnings. */
2008 if (warn_parentheses)
2010 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2012 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2013 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2014 warning ("suggest parentheses around + or - inside shift");
2017 if (code == TRUTH_ORIF_EXPR)
2019 if (code1 == TRUTH_ANDIF_EXPR
2020 || code2 == TRUTH_ANDIF_EXPR)
2021 warning ("suggest parentheses around && within ||");
2024 if (code == BIT_IOR_EXPR)
2026 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2027 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2028 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2029 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2030 warning ("suggest parentheses around arithmetic in operand of |");
2031 /* Check cases like x|y==z */
2032 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2033 warning ("suggest parentheses around comparison in operand of |");
2036 if (code == BIT_XOR_EXPR)
2038 if (code1 == BIT_AND_EXPR
2039 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2040 || code2 == BIT_AND_EXPR
2041 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2042 warning ("suggest parentheses around arithmetic in operand of ^");
2043 /* Check cases like x^y==z */
2044 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2045 warning ("suggest parentheses around comparison in operand of ^");
2048 if (code == BIT_AND_EXPR)
2050 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2051 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2052 warning ("suggest parentheses around + or - in operand of &");
2053 /* Check cases like x&y==z */
2054 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2055 warning ("suggest parentheses around comparison in operand of &");
2059 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2060 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2061 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2062 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2064 unsigned_conversion_warning (result, arg1);
2065 unsigned_conversion_warning (result, arg2);
2066 overflow_warning (result);
2068 class = TREE_CODE_CLASS (TREE_CODE (result));
2070 /* Record the code that was specified in the source,
2071 for the sake of warnings about confusing nesting. */
2072 if (IS_EXPR_CODE_CLASS (class))
2073 C_SET_EXP_ORIGINAL_CODE (result, code);
2076 int flag = TREE_CONSTANT (result);
2077 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2078 so that convert_for_assignment wouldn't strip it.
2079 That way, we got warnings for things like p = (1 - 1).
2080 But it turns out we should not get those warnings. */
2081 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2082 C_SET_EXP_ORIGINAL_CODE (result, code);
2083 TREE_CONSTANT (result) = flag;
2090 /* Return true if `t' is known to be non-negative. */
2093 c_tree_expr_nonnegative_p (tree t)
2095 if (TREE_CODE (t) == STMT_EXPR)
2097 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2099 /* Find the last statement in the chain, ignoring the final
2100 * scope statement */
2101 while (TREE_CHAIN (t) != NULL_TREE
2102 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2104 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2106 return tree_expr_nonnegative_p (t);
2109 /* Return a tree for the difference of pointers OP0 and OP1.
2110 The resulting tree has type int. */
2113 pointer_diff (tree op0, tree op1)
2115 tree result, folded;
2116 tree restype = ptrdiff_type_node;
2118 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2119 tree con0, con1, lit0, lit1;
2120 tree orig_op1 = op1;
2122 if (pedantic || warn_pointer_arith)
2124 if (TREE_CODE (target_type) == VOID_TYPE)
2125 pedwarn ("pointer of type `void *' used in subtraction");
2126 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2127 pedwarn ("pointer to a function used in subtraction");
2130 /* If the conversion to ptrdiff_type does anything like widening or
2131 converting a partial to an integral mode, we get a convert_expression
2132 that is in the way to do any simplifications.
2133 (fold-const.c doesn't know that the extra bits won't be needed.
2134 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2135 different mode in place.)
2136 So first try to find a common term here 'by hand'; we want to cover
2137 at least the cases that occur in legal static initializers. */
2138 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2139 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2141 if (TREE_CODE (con0) == PLUS_EXPR)
2143 lit0 = TREE_OPERAND (con0, 1);
2144 con0 = TREE_OPERAND (con0, 0);
2147 lit0 = integer_zero_node;
2149 if (TREE_CODE (con1) == PLUS_EXPR)
2151 lit1 = TREE_OPERAND (con1, 1);
2152 con1 = TREE_OPERAND (con1, 0);
2155 lit1 = integer_zero_node;
2157 if (operand_equal_p (con0, con1, 0))
2164 /* First do the subtraction as integers;
2165 then drop through to build the divide operator.
2166 Do not do default conversions on the minus operator
2167 in case restype is a short type. */
2169 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2170 convert (restype, op1), 0);
2171 /* This generates an error if op1 is pointer to incomplete type. */
2172 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2173 error ("arithmetic on pointer to an incomplete type");
2175 /* This generates an error if op0 is pointer to incomplete type. */
2176 op1 = c_size_in_bytes (target_type);
2178 /* Divide by the size, in easiest possible way. */
2180 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2182 folded = fold (result);
2183 if (folded == result)
2184 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2188 /* Construct and perhaps optimize a tree representation
2189 for a unary operation. CODE, a tree_code, specifies the operation
2190 and XARG is the operand.
2191 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2192 the default promotions (such as from short to int).
2193 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2194 allows non-lvalues; this is only used to handle conversion of non-lvalue
2195 arrays to pointers in C99. */
2198 build_unary_op (enum tree_code code, tree xarg, int flag)
2200 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2203 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2205 int noconvert = flag;
2207 if (typecode == ERROR_MARK)
2208 return error_mark_node;
2209 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2210 typecode = INTEGER_TYPE;
2215 /* This is used for unary plus, because a CONVERT_EXPR
2216 is enough to prevent anybody from looking inside for
2217 associativity, but won't generate any code. */
2218 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2219 || typecode == COMPLEX_TYPE))
2221 error ("wrong type argument to unary plus");
2222 return error_mark_node;
2224 else if (!noconvert)
2225 arg = default_conversion (arg);
2226 arg = non_lvalue (arg);
2230 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2231 || typecode == COMPLEX_TYPE
2232 || typecode == VECTOR_TYPE))
2234 error ("wrong type argument to unary minus");
2235 return error_mark_node;
2237 else if (!noconvert)
2238 arg = default_conversion (arg);
2242 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2245 arg = default_conversion (arg);
2247 else if (typecode == COMPLEX_TYPE)
2251 pedwarn ("ISO C does not support `~' for complex conjugation");
2253 arg = default_conversion (arg);
2257 error ("wrong type argument to bit-complement");
2258 return error_mark_node;
2263 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2265 error ("wrong type argument to abs");
2266 return error_mark_node;
2268 else if (!noconvert)
2269 arg = default_conversion (arg);
2273 /* Conjugating a real value is a no-op, but allow it anyway. */
2274 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2275 || typecode == COMPLEX_TYPE))
2277 error ("wrong type argument to conjugation");
2278 return error_mark_node;
2280 else if (!noconvert)
2281 arg = default_conversion (arg);
2284 case TRUTH_NOT_EXPR:
2285 if (typecode != INTEGER_TYPE
2286 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2287 && typecode != COMPLEX_TYPE
2288 /* These will convert to a pointer. */
2289 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2291 error ("wrong type argument to unary exclamation mark");
2292 return error_mark_node;
2294 arg = lang_hooks.truthvalue_conversion (arg);
2295 return invert_truthvalue (arg);
2301 if (TREE_CODE (arg) == COMPLEX_CST)
2302 return TREE_REALPART (arg);
2303 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2304 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2309 if (TREE_CODE (arg) == COMPLEX_CST)
2310 return TREE_IMAGPART (arg);
2311 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2312 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2314 return convert (TREE_TYPE (arg), integer_zero_node);
2316 case PREINCREMENT_EXPR:
2317 case POSTINCREMENT_EXPR:
2318 case PREDECREMENT_EXPR:
2319 case POSTDECREMENT_EXPR:
2321 /* Increment or decrement the real part of the value,
2322 and don't change the imaginary part. */
2323 if (typecode == COMPLEX_TYPE)
2328 pedwarn ("ISO C does not support `++' and `--' on complex types");
2330 arg = stabilize_reference (arg);
2331 real = build_unary_op (REALPART_EXPR, arg, 1);
2332 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2333 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2334 build_unary_op (code, real, 1), imag);
2337 /* Report invalid types. */
2339 if (typecode != POINTER_TYPE
2340 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2342 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2343 error ("wrong type argument to increment");
2345 error ("wrong type argument to decrement");
2347 return error_mark_node;
2352 tree result_type = TREE_TYPE (arg);
2354 arg = get_unwidened (arg, 0);
2355 argtype = TREE_TYPE (arg);
2357 /* Compute the increment. */
2359 if (typecode == POINTER_TYPE)
2361 /* If pointer target is an undefined struct,
2362 we just cannot know how to do the arithmetic. */
2363 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2365 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2366 error ("increment of pointer to unknown structure");
2368 error ("decrement of pointer to unknown structure");
2370 else if ((pedantic || warn_pointer_arith)
2371 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2372 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2374 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2375 pedwarn ("wrong type argument to increment");
2377 pedwarn ("wrong type argument to decrement");
2380 inc = c_size_in_bytes (TREE_TYPE (result_type));
2383 inc = integer_one_node;
2385 inc = convert (argtype, inc);
2387 /* Complain about anything else that is not a true lvalue. */
2388 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2389 || code == POSTINCREMENT_EXPR)
2390 ? "invalid lvalue in increment"
2391 : "invalid lvalue in decrement")))
2392 return error_mark_node;
2394 /* Report a read-only lvalue. */
2395 if (TREE_READONLY (arg))
2396 readonly_error (arg,
2397 ((code == PREINCREMENT_EXPR
2398 || code == POSTINCREMENT_EXPR)
2399 ? "increment" : "decrement"));
2401 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2402 val = boolean_increment (code, arg);
2404 val = build (code, TREE_TYPE (arg), arg, inc);
2405 TREE_SIDE_EFFECTS (val) = 1;
2406 val = convert (result_type, val);
2407 if (TREE_CODE (val) != code)
2408 TREE_NO_UNUSED_WARNING (val) = 1;
2413 /* Note that this operation never does default_conversion. */
2415 /* Let &* cancel out to simplify resulting code. */
2416 if (TREE_CODE (arg) == INDIRECT_REF)
2418 /* Don't let this be an lvalue. */
2419 if (lvalue_p (TREE_OPERAND (arg, 0)))
2420 return non_lvalue (TREE_OPERAND (arg, 0));
2421 return TREE_OPERAND (arg, 0);
2424 /* For &x[y], return x+y */
2425 if (TREE_CODE (arg) == ARRAY_REF)
2427 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2428 return error_mark_node;
2429 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2430 TREE_OPERAND (arg, 1), 1);
2433 /* Anything not already handled and not a true memory reference
2434 or a non-lvalue array is an error. */
2435 else if (typecode != FUNCTION_TYPE && !flag
2436 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2437 return error_mark_node;
2439 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2440 argtype = TREE_TYPE (arg);
2442 /* If the lvalue is const or volatile, merge that into the type
2443 to which the address will point. Note that you can't get a
2444 restricted pointer by taking the address of something, so we
2445 only have to deal with `const' and `volatile' here. */
2446 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2447 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2448 argtype = c_build_type_variant (argtype,
2449 TREE_READONLY (arg),
2450 TREE_THIS_VOLATILE (arg));
2452 argtype = build_pointer_type (argtype);
2454 if (!c_mark_addressable (arg))
2455 return error_mark_node;
2460 if (TREE_CODE (arg) == COMPONENT_REF)
2462 tree field = TREE_OPERAND (arg, 1);
2464 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2466 if (DECL_C_BIT_FIELD (field))
2468 error ("attempt to take address of bit-field structure member `%s'",
2469 IDENTIFIER_POINTER (DECL_NAME (field)));
2470 return error_mark_node;
2473 addr = fold (build (PLUS_EXPR, argtype,
2474 convert (argtype, addr),
2475 convert (argtype, byte_position (field))));
2478 addr = build1 (code, argtype, arg);
2480 /* Address of a static or external variable or
2481 file-scope function counts as a constant. */
2483 && ! (TREE_CODE (arg) == FUNCTION_DECL
2484 && !DECL_FILE_SCOPE_P (arg)))
2485 TREE_CONSTANT (addr) = 1;
2494 argtype = TREE_TYPE (arg);
2495 return fold (build1 (code, argtype, arg));
2498 /* Return nonzero if REF is an lvalue valid for this language.
2499 Lvalues can be assigned, unless their type has TYPE_READONLY.
2500 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2505 enum tree_code code = TREE_CODE (ref);
2512 return lvalue_p (TREE_OPERAND (ref, 0));
2514 case COMPOUND_LITERAL_EXPR:
2524 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2525 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2529 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2536 /* Return nonzero if REF is an lvalue valid for this language;
2537 otherwise, print an error message and return zero. */
2540 lvalue_or_else (tree ref, const char *msgid)
2542 int win = lvalue_p (ref);
2545 error ("%s", msgid);
2551 /* Warn about storing in something that is `const'. */
2554 readonly_error (tree arg, const char *msgid)
2556 if (TREE_CODE (arg) == COMPONENT_REF)
2558 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2559 readonly_error (TREE_OPERAND (arg, 0), msgid);
2561 error ("%s of read-only member `%s'", _(msgid),
2562 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2564 else if (TREE_CODE (arg) == VAR_DECL)
2565 error ("%s of read-only variable `%s'", _(msgid),
2566 IDENTIFIER_POINTER (DECL_NAME (arg)));
2568 error ("%s of read-only location", _(msgid));
2571 /* Mark EXP saying that we need to be able to take the
2572 address of it; it should not be allocated in a register.
2573 Returns true if successful. */
2576 c_mark_addressable (tree exp)
2581 switch (TREE_CODE (x))
2584 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2586 error ("cannot take address of bit-field `%s'",
2587 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2591 /* ... fall through ... */
2597 x = TREE_OPERAND (x, 0);
2600 case COMPOUND_LITERAL_EXPR:
2602 TREE_ADDRESSABLE (x) = 1;
2609 if (C_DECL_REGISTER (x)
2610 && DECL_NONLOCAL (x))
2612 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2614 error ("global register variable `%s' used in nested function",
2615 IDENTIFIER_POINTER (DECL_NAME (x)));
2618 pedwarn ("register variable `%s' used in nested function",
2619 IDENTIFIER_POINTER (DECL_NAME (x)));
2621 else if (C_DECL_REGISTER (x))
2623 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2625 error ("address of global register variable `%s' requested",
2626 IDENTIFIER_POINTER (DECL_NAME (x)));
2630 pedwarn ("address of register variable `%s' requested",
2631 IDENTIFIER_POINTER (DECL_NAME (x)));
2633 put_var_into_stack (x, /*rescan=*/true);
2637 TREE_ADDRESSABLE (x) = 1;
2644 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2647 build_conditional_expr (tree ifexp, tree op1, tree op2)
2651 enum tree_code code1;
2652 enum tree_code code2;
2653 tree result_type = NULL;
2654 tree orig_op1 = op1, orig_op2 = op2;
2656 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2658 /* Promote both alternatives. */
2660 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2661 op1 = default_conversion (op1);
2662 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2663 op2 = default_conversion (op2);
2665 if (TREE_CODE (ifexp) == ERROR_MARK
2666 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2667 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2668 return error_mark_node;
2670 type1 = TREE_TYPE (op1);
2671 code1 = TREE_CODE (type1);
2672 type2 = TREE_TYPE (op2);
2673 code2 = TREE_CODE (type2);
2675 /* C90 does not permit non-lvalue arrays in conditional expressions.
2676 In C99 they will be pointers by now. */
2677 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2679 error ("non-lvalue array in conditional expression");
2680 return error_mark_node;
2683 /* Quickly detect the usual case where op1 and op2 have the same type
2685 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2688 result_type = type1;
2690 result_type = TYPE_MAIN_VARIANT (type1);
2692 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2693 || code1 == COMPLEX_TYPE)
2694 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2695 || code2 == COMPLEX_TYPE))
2697 result_type = common_type (type1, type2);
2699 /* If -Wsign-compare, warn here if type1 and type2 have
2700 different signedness. We'll promote the signed to unsigned
2701 and later code won't know it used to be different.
2702 Do this check on the original types, so that explicit casts
2703 will be considered, but default promotions won't. */
2704 if (warn_sign_compare && !skip_evaluation)
2706 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2707 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2709 if (unsigned_op1 ^ unsigned_op2)
2711 /* Do not warn if the result type is signed, since the
2712 signed type will only be chosen if it can represent
2713 all the values of the unsigned type. */
2714 if (! TYPE_UNSIGNED (result_type))
2716 /* Do not warn if the signed quantity is an unsuffixed
2717 integer literal (or some static constant expression
2718 involving such literals) and it is non-negative. */
2719 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2720 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2723 warning ("signed and unsigned type in conditional expression");
2727 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2729 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2730 pedwarn ("ISO C forbids conditional expr with only one void side");
2731 result_type = void_type_node;
2733 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2735 if (comp_target_types (type1, type2, 1))
2736 result_type = common_type (type1, type2);
2737 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2738 && TREE_CODE (orig_op1) != NOP_EXPR)
2739 result_type = qualify_type (type2, type1);
2740 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2741 && TREE_CODE (orig_op2) != NOP_EXPR)
2742 result_type = qualify_type (type1, type2);
2743 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2745 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2746 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2747 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2748 TREE_TYPE (type2)));
2750 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2752 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2753 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2754 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2755 TREE_TYPE (type1)));
2759 pedwarn ("pointer type mismatch in conditional expression");
2760 result_type = build_pointer_type (void_type_node);
2763 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2765 if (! integer_zerop (op2))
2766 pedwarn ("pointer/integer type mismatch in conditional expression");
2769 op2 = null_pointer_node;
2771 result_type = type1;
2773 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2775 if (!integer_zerop (op1))
2776 pedwarn ("pointer/integer type mismatch in conditional expression");
2779 op1 = null_pointer_node;
2781 result_type = type2;
2786 if (flag_cond_mismatch)
2787 result_type = void_type_node;
2790 error ("type mismatch in conditional expression");
2791 return error_mark_node;
2795 /* Merge const and volatile flags of the incoming types. */
2797 = build_type_variant (result_type,
2798 TREE_READONLY (op1) || TREE_READONLY (op2),
2799 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2801 if (result_type != TREE_TYPE (op1))
2802 op1 = convert_and_check (result_type, op1);
2803 if (result_type != TREE_TYPE (op2))
2804 op2 = convert_and_check (result_type, op2);
2806 if (TREE_CODE (ifexp) == INTEGER_CST)
2807 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2809 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2812 /* Given a list of expressions, return a compound expression
2813 that performs them all and returns the value of the last of them. */
2816 build_compound_expr (tree list)
2818 return internal_build_compound_expr (list, TRUE);
2822 internal_build_compound_expr (tree list, int first_p)
2826 if (TREE_CHAIN (list) == 0)
2828 /* Convert arrays and functions to pointers when there
2829 really is a comma operator. */
2832 = default_function_array_conversion (TREE_VALUE (list));
2834 /* Don't let (0, 0) be null pointer constant. */
2835 if (!first_p && integer_zerop (TREE_VALUE (list)))
2836 return non_lvalue (TREE_VALUE (list));
2837 return TREE_VALUE (list);
2840 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2842 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2844 /* The left-hand operand of a comma expression is like an expression
2845 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2846 any side-effects, unless it was explicitly cast to (void). */
2847 if (warn_unused_value
2848 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2849 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2850 warning ("left-hand operand of comma expression has no effect");
2853 /* With -Wunused, we should also warn if the left-hand operand does have
2854 side-effects, but computes a value which is not used. For example, in
2855 `foo() + bar(), baz()' the result of the `+' operator is not used,
2856 so we should issue a warning. */
2857 else if (warn_unused_value)
2858 warn_if_unused_value (TREE_VALUE (list));
2860 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2863 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2866 build_c_cast (tree type, tree expr)
2870 if (type == error_mark_node || expr == error_mark_node)
2871 return error_mark_node;
2873 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2874 only in <protocol> qualifications. But when constructing cast expressions,
2875 the protocols do matter and must be kept around. */
2876 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2877 type = TYPE_MAIN_VARIANT (type);
2879 if (TREE_CODE (type) == ARRAY_TYPE)
2881 error ("cast specifies array type");
2882 return error_mark_node;
2885 if (TREE_CODE (type) == FUNCTION_TYPE)
2887 error ("cast specifies function type");
2888 return error_mark_node;
2891 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2895 if (TREE_CODE (type) == RECORD_TYPE
2896 || TREE_CODE (type) == UNION_TYPE)
2897 pedwarn ("ISO C forbids casting nonscalar to the same type");
2900 else if (TREE_CODE (type) == UNION_TYPE)
2903 value = default_function_array_conversion (value);
2905 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2906 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2907 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2915 pedwarn ("ISO C forbids casts to union type");
2916 t = digest_init (type,
2917 build_constructor (type,
2918 build_tree_list (field, value)),
2920 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2923 error ("cast to union type from type not present in union");
2924 return error_mark_node;
2930 /* If casting to void, avoid the error that would come
2931 from default_conversion in the case of a non-lvalue array. */
2932 if (type == void_type_node)
2933 return build1 (CONVERT_EXPR, type, value);
2935 /* Convert functions and arrays to pointers,
2936 but don't convert any other types. */
2937 value = default_function_array_conversion (value);
2938 otype = TREE_TYPE (value);
2940 /* Optionally warn about potentially worrisome casts. */
2943 && TREE_CODE (type) == POINTER_TYPE
2944 && TREE_CODE (otype) == POINTER_TYPE)
2946 tree in_type = type;
2947 tree in_otype = otype;
2951 /* Check that the qualifiers on IN_TYPE are a superset of
2952 the qualifiers of IN_OTYPE. The outermost level of
2953 POINTER_TYPE nodes is uninteresting and we stop as soon
2954 as we hit a non-POINTER_TYPE node on either type. */
2957 in_otype = TREE_TYPE (in_otype);
2958 in_type = TREE_TYPE (in_type);
2960 /* GNU C allows cv-qualified function types. 'const'
2961 means the function is very pure, 'volatile' means it
2962 can't return. We need to warn when such qualifiers
2963 are added, not when they're taken away. */
2964 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2965 && TREE_CODE (in_type) == FUNCTION_TYPE)
2966 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2968 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2970 while (TREE_CODE (in_type) == POINTER_TYPE
2971 && TREE_CODE (in_otype) == POINTER_TYPE);
2974 warning ("cast adds new qualifiers to function type");
2977 /* There are qualifiers present in IN_OTYPE that are not
2978 present in IN_TYPE. */
2979 warning ("cast discards qualifiers from pointer target type");
2982 /* Warn about possible alignment problems. */
2983 if (STRICT_ALIGNMENT && warn_cast_align
2984 && TREE_CODE (type) == POINTER_TYPE
2985 && TREE_CODE (otype) == POINTER_TYPE
2986 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
2987 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
2988 /* Don't warn about opaque types, where the actual alignment
2989 restriction is unknown. */
2990 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
2991 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
2992 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
2993 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
2994 warning ("cast increases required alignment of target type");
2996 if (TREE_CODE (type) == INTEGER_TYPE
2997 && TREE_CODE (otype) == POINTER_TYPE
2998 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
2999 && !TREE_CONSTANT (value))
3000 warning ("cast from pointer to integer of different size");
3002 if (warn_bad_function_cast
3003 && TREE_CODE (value) == CALL_EXPR
3004 && TREE_CODE (type) != TREE_CODE (otype))
3005 warning ("cast does not match function type");
3007 if (TREE_CODE (type) == POINTER_TYPE
3008 && TREE_CODE (otype) == INTEGER_TYPE
3009 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3010 /* Don't warn about converting any constant. */
3011 && !TREE_CONSTANT (value))
3012 warning ("cast to pointer from integer of different size");
3014 if (TREE_CODE (type) == POINTER_TYPE
3015 && TREE_CODE (otype) == POINTER_TYPE
3016 && TREE_CODE (expr) == ADDR_EXPR
3017 && DECL_P (TREE_OPERAND (expr, 0))
3018 && flag_strict_aliasing && warn_strict_aliasing
3019 && !VOID_TYPE_P (TREE_TYPE (type)))
3021 /* Casting the address of a decl to non void pointer. Warn
3022 if the cast breaks type based aliasing. */
3023 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3024 warning ("type-punning to incomplete type might break strict-aliasing rules");
3027 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3028 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3030 if (!alias_sets_conflict_p (set1, set2))
3031 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3032 else if (warn_strict_aliasing > 1
3033 && !alias_sets_might_conflict_p (set1, set2))
3034 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3038 /* If pedantic, warn for conversions between function and object
3039 pointer types, except for converting a null pointer constant
3040 to function pointer type. */
3042 && TREE_CODE (type) == POINTER_TYPE
3043 && TREE_CODE (otype) == POINTER_TYPE
3044 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3045 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3046 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3049 && TREE_CODE (type) == POINTER_TYPE
3050 && TREE_CODE (otype) == POINTER_TYPE
3051 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3052 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3053 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3054 && TREE_CODE (expr) != NOP_EXPR))
3055 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3058 /* Replace a nonvolatile const static variable with its value. */
3059 if (optimize && TREE_CODE (value) == VAR_DECL)
3060 value = decl_constant_value (value);
3061 value = convert (type, value);
3063 /* Ignore any integer overflow caused by the cast. */
3064 if (TREE_CODE (value) == INTEGER_CST)
3066 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3068 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3069 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3073 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3074 if (TREE_CODE (value) == INTEGER_CST
3075 && TREE_CODE (expr) == INTEGER_CST
3076 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3077 value = non_lvalue (value);
3079 /* Don't let a cast be an lvalue. */
3081 value = non_lvalue (value);
3086 /* Interpret a cast of expression EXPR to type TYPE. */
3088 c_cast_expr (tree type, tree expr)
3090 int saved_wsp = warn_strict_prototypes;
3092 /* This avoids warnings about unprototyped casts on
3093 integers. E.g. "#define SIG_DFL (void(*)())0". */
3094 if (TREE_CODE (expr) == INTEGER_CST)
3095 warn_strict_prototypes = 0;
3096 type = groktypename (type);
3097 warn_strict_prototypes = saved_wsp;
3099 return build_c_cast (type, expr);
3103 /* Build an assignment expression of lvalue LHS from value RHS.
3104 MODIFYCODE is the code for a binary operator that we use
3105 to combine the old value of LHS with RHS to get the new value.
3106 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3109 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3113 tree lhstype = TREE_TYPE (lhs);
3114 tree olhstype = lhstype;
3116 /* Types that aren't fully specified cannot be used in assignments. */
3117 lhs = require_complete_type (lhs);
3119 /* Avoid duplicate error messages from operands that had errors. */
3120 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3121 return error_mark_node;
3123 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3124 /* Do not use STRIP_NOPS here. We do not want an enumerator
3125 whose value is 0 to count as a null pointer constant. */
3126 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3127 rhs = TREE_OPERAND (rhs, 0);
3131 /* If a binary op has been requested, combine the old LHS value with the RHS
3132 producing the value we should actually store into the LHS. */
3134 if (modifycode != NOP_EXPR)
3136 lhs = stabilize_reference (lhs);
3137 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3140 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3141 return error_mark_node;
3143 /* Warn about storing in something that is `const'. */
3145 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3146 || ((TREE_CODE (lhstype) == RECORD_TYPE
3147 || TREE_CODE (lhstype) == UNION_TYPE)
3148 && C_TYPE_FIELDS_READONLY (lhstype)))
3149 readonly_error (lhs, "assignment");
3151 /* If storing into a structure or union member,
3152 it has probably been given type `int'.
3153 Compute the type that would go with
3154 the actual amount of storage the member occupies. */
3156 if (TREE_CODE (lhs) == COMPONENT_REF
3157 && (TREE_CODE (lhstype) == INTEGER_TYPE
3158 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3159 || TREE_CODE (lhstype) == REAL_TYPE
3160 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3161 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3163 /* If storing in a field that is in actuality a short or narrower than one,
3164 we must store in the field in its actual type. */
3166 if (lhstype != TREE_TYPE (lhs))
3168 lhs = copy_node (lhs);
3169 TREE_TYPE (lhs) = lhstype;
3172 /* Convert new value to destination type. */
3174 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3175 NULL_TREE, NULL_TREE, 0);
3176 if (TREE_CODE (newrhs) == ERROR_MARK)
3177 return error_mark_node;
3181 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3182 TREE_SIDE_EFFECTS (result) = 1;
3184 /* If we got the LHS in a different type for storing in,
3185 convert the result back to the nominal type of LHS
3186 so that the value we return always has the same type
3187 as the LHS argument. */
3189 if (olhstype == TREE_TYPE (result))
3191 return convert_for_assignment (olhstype, result, _("assignment"),
3192 NULL_TREE, NULL_TREE, 0);
3195 /* Convert value RHS to type TYPE as preparation for an assignment
3196 to an lvalue of type TYPE.
3197 The real work of conversion is done by `convert'.
3198 The purpose of this function is to generate error messages
3199 for assignments that are not allowed in C.
3200 ERRTYPE is a string to use in error messages:
3201 "assignment", "return", etc. If it is null, this is parameter passing
3202 for a function call (and different error messages are output).
3204 FUNNAME is the name of the function being called,
3205 as an IDENTIFIER_NODE, or null.
3206 PARMNUM is the number of the argument, for printing in error messages. */
3209 convert_for_assignment (tree type, tree rhs, const char *errtype,
3210 tree fundecl, tree funname, int parmnum)
3212 enum tree_code codel = TREE_CODE (type);
3214 enum tree_code coder;
3216 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3217 /* Do not use STRIP_NOPS here. We do not want an enumerator
3218 whose value is 0 to count as a null pointer constant. */
3219 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3220 rhs = TREE_OPERAND (rhs, 0);
3222 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3223 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3224 rhs = default_conversion (rhs);
3225 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3226 rhs = decl_constant_value_for_broken_optimization (rhs);
3228 rhstype = TREE_TYPE (rhs);
3229 coder = TREE_CODE (rhstype);
3231 if (coder == ERROR_MARK)
3232 return error_mark_node;
3234 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3236 overflow_warning (rhs);
3237 /* Check for Objective-C protocols. This will automatically
3238 issue a warning if there are protocol violations. No need to
3239 use the return value. */
3240 if (c_dialect_objc ())
3241 objc_comptypes (type, rhstype, 0);
3245 if (coder == VOID_TYPE)
3247 error ("void value not ignored as it ought to be");
3248 return error_mark_node;
3250 /* A type converts to a reference to it.
3251 This code doesn't fully support references, it's just for the
3252 special case of va_start and va_copy. */
3253 if (codel == REFERENCE_TYPE
3254 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3256 if (!lvalue_p (rhs))
3258 error ("cannot pass rvalue to reference parameter");
3259 return error_mark_node;
3261 if (!c_mark_addressable (rhs))
3262 return error_mark_node;
3263 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3265 /* We already know that these two types are compatible, but they
3266 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3267 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3268 likely to be va_list, a typedef to __builtin_va_list, which
3269 is different enough that it will cause problems later. */
3270 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3271 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3273 rhs = build1 (NOP_EXPR, type, rhs);
3276 /* Some types can interconvert without explicit casts. */
3277 else if (codel == VECTOR_TYPE
3278 && comptypes (type, TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3279 return convert (type, rhs);
3280 /* Arithmetic types all interconvert, and enum is treated like int. */
3281 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3282 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3283 || codel == BOOLEAN_TYPE)
3284 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3285 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3286 || coder == BOOLEAN_TYPE))
3287 return convert_and_check (type, rhs);
3289 /* Conversion to a transparent union from its member types.
3290 This applies only to function arguments. */
3291 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3294 tree marginal_memb_type = 0;
3296 for (memb_types = TYPE_FIELDS (type); memb_types;
3297 memb_types = TREE_CHAIN (memb_types))
3299 tree memb_type = TREE_TYPE (memb_types);
3301 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3302 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3305 if (TREE_CODE (memb_type) != POINTER_TYPE)
3308 if (coder == POINTER_TYPE)
3310 tree ttl = TREE_TYPE (memb_type);
3311 tree ttr = TREE_TYPE (rhstype);
3313 /* Any non-function converts to a [const][volatile] void *
3314 and vice versa; otherwise, targets must be the same.
3315 Meanwhile, the lhs target must have all the qualifiers of
3317 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3318 || comp_target_types (memb_type, rhstype, 0))
3320 /* If this type won't generate any warnings, use it. */
3321 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3322 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3323 && TREE_CODE (ttl) == FUNCTION_TYPE)
3324 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3325 == TYPE_QUALS (ttr))
3326 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3327 == TYPE_QUALS (ttl))))
3330 /* Keep looking for a better type, but remember this one. */
3331 if (! marginal_memb_type)
3332 marginal_memb_type = memb_type;
3336 /* Can convert integer zero to any pointer type. */
3337 if (integer_zerop (rhs)
3338 || (TREE_CODE (rhs) == NOP_EXPR
3339 && integer_zerop (TREE_OPERAND (rhs, 0))))
3341 rhs = null_pointer_node;
3346 if (memb_types || marginal_memb_type)
3350 /* We have only a marginally acceptable member type;
3351 it needs a warning. */
3352 tree ttl = TREE_TYPE (marginal_memb_type);
3353 tree ttr = TREE_TYPE (rhstype);
3355 /* Const and volatile mean something different for function
3356 types, so the usual warnings are not appropriate. */
3357 if (TREE_CODE (ttr) == FUNCTION_TYPE
3358 && TREE_CODE (ttl) == FUNCTION_TYPE)
3360 /* Because const and volatile on functions are
3361 restrictions that say the function will not do
3362 certain things, it is okay to use a const or volatile
3363 function where an ordinary one is wanted, but not
3365 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3366 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3367 errtype, funname, parmnum);
3369 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3370 warn_for_assignment ("%s discards qualifiers from pointer target type",
3375 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3376 pedwarn ("ISO C prohibits argument conversion to union type");
3378 return build1 (NOP_EXPR, type, rhs);
3382 /* Conversions among pointers */
3383 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3384 && (coder == codel))
3386 tree ttl = TREE_TYPE (type);
3387 tree ttr = TREE_TYPE (rhstype);
3388 bool is_opaque_pointer;
3389 int target_cmp = 0; /* Cache comp_target_types () result. */
3391 /* Opaque pointers are treated like void pointers. */
3392 is_opaque_pointer = (targetm.vector_opaque_p (type)
3393 || targetm.vector_opaque_p (rhstype))
3394 && TREE_CODE (ttl) == VECTOR_TYPE
3395 && TREE_CODE (ttr) == VECTOR_TYPE;
3397 /* Any non-function converts to a [const][volatile] void *
3398 and vice versa; otherwise, targets must be the same.
3399 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3400 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3401 || (target_cmp = comp_target_types (type, rhstype, 0))
3402 || is_opaque_pointer
3403 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3404 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3407 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3410 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3411 which are not ANSI null ptr constants. */
3412 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3413 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3414 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3415 errtype, funname, parmnum);
3416 /* Const and volatile mean something different for function types,
3417 so the usual warnings are not appropriate. */
3418 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3419 && TREE_CODE (ttl) != FUNCTION_TYPE)
3421 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3422 warn_for_assignment ("%s discards qualifiers from pointer target type",
3423 errtype, funname, parmnum);
3424 /* If this is not a case of ignoring a mismatch in signedness,
3426 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3429 /* If there is a mismatch, do warn. */
3431 warn_for_assignment ("pointer targets in %s differ in signedness",
3432 errtype, funname, parmnum);
3434 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3435 && TREE_CODE (ttr) == FUNCTION_TYPE)
3437 /* Because const and volatile on functions are restrictions
3438 that say the function will not do certain things,
3439 it is okay to use a const or volatile function
3440 where an ordinary one is wanted, but not vice-versa. */
3441 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3442 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3443 errtype, funname, parmnum);
3447 warn_for_assignment ("%s from incompatible pointer type",
3448 errtype, funname, parmnum);
3449 return convert (type, rhs);
3451 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3453 error ("invalid use of non-lvalue array");
3454 return error_mark_node;
3456 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3458 /* An explicit constant 0 can convert to a pointer,
3459 or one that results from arithmetic, even including
3460 a cast to integer type. */
3461 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3463 ! (TREE_CODE (rhs) == NOP_EXPR
3464 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3465 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3466 && integer_zerop (TREE_OPERAND (rhs, 0))))
3467 warn_for_assignment ("%s makes pointer from integer without a cast",
3468 errtype, funname, parmnum);
3470 return convert (type, rhs);
3472 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3474 warn_for_assignment ("%s makes integer from pointer without a cast",
3475 errtype, funname, parmnum);
3476 return convert (type, rhs);
3478 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3479 return convert (type, rhs);
3485 tree selector = objc_message_selector ();
3487 if (selector && parmnum > 2)
3488 error ("incompatible type for argument %d of `%s'",
3489 parmnum - 2, IDENTIFIER_POINTER (selector));
3491 error ("incompatible type for argument %d of `%s'",
3492 parmnum, IDENTIFIER_POINTER (funname));
3495 error ("incompatible type for argument %d of indirect function call",
3499 error ("incompatible types in %s", errtype);
3501 return error_mark_node;
3504 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3505 is used for error and waring reporting and indicates which argument
3506 is being processed. */
3509 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3513 /* If FN was prototyped, the value has been converted already
3514 in convert_arguments. */
3515 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3518 type = TREE_TYPE (parm);
3519 ret = convert_for_assignment (type, value,
3520 (char *) 0 /* arg passing */, fn,
3521 DECL_NAME (fn), argnum);
3522 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3523 && INTEGRAL_TYPE_P (type)
3524 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3525 ret = default_conversion (ret);
3529 /* Print a warning using MSGID.
3530 It gets OPNAME as its one parameter.
3531 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3532 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3533 FUNCTION and ARGNUM are handled specially if we are building an
3534 Objective-C selector. */
3537 warn_for_assignment (const char *msgid, const char *opname, tree function,
3542 tree selector = objc_message_selector ();
3545 if (selector && argnum > 2)
3547 function = selector;
3554 /* Function name is known; supply it. */
3555 const char *const argstring = _("passing arg of `%s'");
3556 new_opname = alloca (IDENTIFIER_LENGTH (function)
3557 + strlen (argstring) + 1 + 1);
3558 sprintf (new_opname, argstring,
3559 IDENTIFIER_POINTER (function));
3563 /* Function name unknown (call through ptr). */
3564 const char *const argnofun = _("passing arg of pointer to function");
3565 new_opname = alloca (strlen (argnofun) + 1 + 1);
3566 sprintf (new_opname, argnofun);
3571 /* Function name is known; supply it. */
3572 const char *const argstring = _("passing arg %d of `%s'");
3573 new_opname = alloca (IDENTIFIER_LENGTH (function)
3574 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3575 sprintf (new_opname, argstring, argnum,
3576 IDENTIFIER_POINTER (function));
3580 /* Function name unknown (call through ptr); just give arg number. */
3581 const char *const argnofun = _("passing arg %d of pointer to function");
3582 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3583 sprintf (new_opname, argnofun, argnum);
3585 opname = new_opname;
3587 pedwarn (msgid, opname);
3590 /* If VALUE is a compound expr all of whose expressions are constant, then
3591 return its value. Otherwise, return error_mark_node.
3593 This is for handling COMPOUND_EXPRs as initializer elements
3594 which is allowed with a warning when -pedantic is specified. */
3597 valid_compound_expr_initializer (tree value, tree endtype)
3599 if (TREE_CODE (value) == COMPOUND_EXPR)
3601 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3603 return error_mark_node;
3604 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3607 else if (! TREE_CONSTANT (value)
3608 && ! initializer_constant_valid_p (value, endtype))
3609 return error_mark_node;
3614 /* Perform appropriate conversions on the initial value of a variable,
3615 store it in the declaration DECL,
3616 and print any error messages that are appropriate.
3617 If the init is invalid, store an ERROR_MARK. */
3620 store_init_value (tree decl, tree init)
3624 /* If variable's type was invalidly declared, just ignore it. */
3626 type = TREE_TYPE (decl);
3627 if (TREE_CODE (type) == ERROR_MARK)
3630 /* Digest the specified initializer into an expression. */
3632 value = digest_init (type, init, TREE_STATIC (decl));
3634 /* Store the expression if valid; else report error. */
3636 if (warn_traditional && !in_system_header
3637 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3638 warning ("traditional C rejects automatic aggregate initialization");
3640 DECL_INITIAL (decl) = value;
3642 /* ANSI wants warnings about out-of-range constant initializers. */
3643 STRIP_TYPE_NOPS (value);
3644 constant_expression_warning (value);
3646 /* Check if we need to set array size from compound literal size. */
3647 if (TREE_CODE (type) == ARRAY_TYPE
3648 && TYPE_DOMAIN (type) == 0
3649 && value != error_mark_node)
3651 tree inside_init = init;
3653 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3654 inside_init = TREE_OPERAND (init, 0);
3655 inside_init = fold (inside_init);
3657 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3659 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3661 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3663 /* For int foo[] = (int [3]){1}; we need to set array size
3664 now since later on array initializer will be just the
3665 brace enclosed list of the compound literal. */
3666 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3668 layout_decl (decl, 0);
3674 /* Methods for storing and printing names for error messages. */
3676 /* Implement a spelling stack that allows components of a name to be pushed
3677 and popped. Each element on the stack is this structure. */
3689 #define SPELLING_STRING 1
3690 #define SPELLING_MEMBER 2
3691 #define SPELLING_BOUNDS 3
3693 static struct spelling *spelling; /* Next stack element (unused). */
3694 static struct spelling *spelling_base; /* Spelling stack base. */
3695 static int spelling_size; /* Size of the spelling stack. */
3697 /* Macros to save and restore the spelling stack around push_... functions.
3698 Alternative to SAVE_SPELLING_STACK. */
3700 #define SPELLING_DEPTH() (spelling - spelling_base)
3701 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3703 /* Push an element on the spelling stack with type KIND and assign VALUE
3706 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3708 int depth = SPELLING_DEPTH (); \
3710 if (depth >= spelling_size) \
3712 spelling_size += 10; \
3713 if (spelling_base == 0) \
3714 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3716 spelling_base = xrealloc (spelling_base, \
3717 spelling_size * sizeof (struct spelling)); \
3718 RESTORE_SPELLING_DEPTH (depth); \
3721 spelling->kind = (KIND); \
3722 spelling->MEMBER = (VALUE); \
3726 /* Push STRING on the stack. Printed literally. */
3729 push_string (const char *string)
3731 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3734 /* Push a member name on the stack. Printed as '.' STRING. */
3737 push_member_name (tree decl)
3739 const char *const string
3740 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3741 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3744 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3747 push_array_bounds (int bounds)
3749 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3752 /* Compute the maximum size in bytes of the printed spelling. */
3755 spelling_length (void)
3760 for (p = spelling_base; p < spelling; p++)
3762 if (p->kind == SPELLING_BOUNDS)
3765 size += strlen (p->u.s) + 1;
3771 /* Print the spelling to BUFFER and return it. */
3774 print_spelling (char *buffer)
3779 for (p = spelling_base; p < spelling; p++)
3780 if (p->kind == SPELLING_BOUNDS)
3782 sprintf (d, "[%d]", p->u.i);
3788 if (p->kind == SPELLING_MEMBER)
3790 for (s = p->u.s; (*d = *s++); d++)
3797 /* Issue an error message for a bad initializer component.
3798 MSGID identifies the message.
3799 The component name is taken from the spelling stack. */
3802 error_init (const char *msgid)
3806 error ("%s", _(msgid));
3807 ofwhat = print_spelling (alloca (spelling_length () + 1));
3809 error ("(near initialization for `%s')", ofwhat);
3812 /* Issue a pedantic warning for a bad initializer component.
3813 MSGID identifies the message.
3814 The component name is taken from the spelling stack. */
3817 pedwarn_init (const char *msgid)
3821 pedwarn ("%s", _(msgid));
3822 ofwhat = print_spelling (alloca (spelling_length () + 1));
3824 pedwarn ("(near initialization for `%s')", ofwhat);
3827 /* Issue a warning for a bad initializer component.
3828 MSGID identifies the message.
3829 The component name is taken from the spelling stack. */
3832 warning_init (const char *msgid)
3836 warning ("%s", _(msgid));
3837 ofwhat = print_spelling (alloca (spelling_length () + 1));
3839 warning ("(near initialization for `%s')", ofwhat);
3842 /* Digest the parser output INIT as an initializer for type TYPE.
3843 Return a C expression of type TYPE to represent the initial value.
3845 REQUIRE_CONSTANT requests an error if non-constant initializers or
3846 elements are seen. */
3849 digest_init (tree type, tree init, int require_constant)
3851 enum tree_code code = TREE_CODE (type);
3852 tree inside_init = init;
3854 if (type == error_mark_node
3855 || init == error_mark_node
3856 || TREE_TYPE (init) == error_mark_node)
3857 return error_mark_node;
3859 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3860 /* Do not use STRIP_NOPS here. We do not want an enumerator
3861 whose value is 0 to count as a null pointer constant. */
3862 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3863 inside_init = TREE_OPERAND (init, 0);
3865 inside_init = fold (inside_init);
3867 /* Initialization of an array of chars from a string constant
3868 optionally enclosed in braces. */
3870 if (code == ARRAY_TYPE)
3872 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3873 if ((typ1 == char_type_node
3874 || typ1 == signed_char_type_node
3875 || typ1 == unsigned_char_type_node
3876 || typ1 == unsigned_wchar_type_node
3877 || typ1 == signed_wchar_type_node)
3878 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3880 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3881 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3884 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3886 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3888 error_init ("char-array initialized from wide string");
3889 return error_mark_node;
3891 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3893 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3895 error_init ("int-array initialized from non-wide string");
3896 return error_mark_node;
3899 TREE_TYPE (inside_init) = type;
3900 if (TYPE_DOMAIN (type) != 0
3901 && TYPE_SIZE (type) != 0
3902 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3903 /* Subtract 1 (or sizeof (wchar_t))
3904 because it's ok to ignore the terminating null char
3905 that is counted in the length of the constant. */
3906 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3907 TREE_STRING_LENGTH (inside_init)
3908 - ((TYPE_PRECISION (typ1)
3909 != TYPE_PRECISION (char_type_node))
3910 ? (TYPE_PRECISION (wchar_type_node)
3913 pedwarn_init ("initializer-string for array of chars is too long");
3919 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3920 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3921 below and handle as a constructor. */
3922 if (code == VECTOR_TYPE
3923 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
3924 && TREE_CONSTANT (inside_init))
3926 if (TREE_CODE (inside_init) == VECTOR_CST
3927 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3928 TYPE_MAIN_VARIANT (type),
3932 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
3935 /* Any type can be initialized
3936 from an expression of the same type, optionally with braces. */
3938 if (inside_init && TREE_TYPE (inside_init) != 0
3939 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3940 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
3941 || (code == ARRAY_TYPE
3942 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3943 || (code == VECTOR_TYPE
3944 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3945 || (code == POINTER_TYPE
3946 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3947 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3948 TREE_TYPE (type), COMPARE_STRICT))
3949 || (code == POINTER_TYPE
3950 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
3951 && comptypes (TREE_TYPE (inside_init),
3952 TREE_TYPE (type), COMPARE_STRICT))))
3954 if (code == POINTER_TYPE)
3956 inside_init = default_function_array_conversion (inside_init);
3958 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
3960 error_init ("invalid use of non-lvalue array");
3961 return error_mark_node;
3965 if (code == VECTOR_TYPE)
3966 /* Although the types are compatible, we may require a
3968 inside_init = convert (type, inside_init);
3970 if (require_constant && !flag_isoc99
3971 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3973 /* As an extension, allow initializing objects with static storage
3974 duration with compound literals (which are then treated just as
3975 the brace enclosed list they contain). */
3976 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3977 inside_init = DECL_INITIAL (decl);
3980 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
3981 && TREE_CODE (inside_init) != CONSTRUCTOR)
3983 error_init ("array initialized from non-constant array expression");
3984 return error_mark_node;
3987 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
3988 inside_init = decl_constant_value_for_broken_optimization (inside_init);
3990 /* Compound expressions can only occur here if -pedantic or
3991 -pedantic-errors is specified. In the later case, we always want
3992 an error. In the former case, we simply want a warning. */
3993 if (require_constant && pedantic
3994 && TREE_CODE (inside_init) == COMPOUND_EXPR)
3997 = valid_compound_expr_initializer (inside_init,
3998 TREE_TYPE (inside_init));
3999 if (inside_init == error_mark_node)
4000 error_init ("initializer element is not constant");
4002 pedwarn_init ("initializer element is not constant");
4003 if (flag_pedantic_errors)
4004 inside_init = error_mark_node;
4006 else if (require_constant
4007 && (!TREE_CONSTANT (inside_init)
4008 /* This test catches things like `7 / 0' which
4009 result in an expression for which TREE_CONSTANT
4010 is true, but which is not actually something
4011 that is a legal constant. We really should not
4012 be using this function, because it is a part of
4013 the back-end. Instead, the expression should
4014 already have been turned into ERROR_MARK_NODE. */
4015 || !initializer_constant_valid_p (inside_init,
4016 TREE_TYPE (inside_init))))
4018 error_init ("initializer element is not constant");
4019 inside_init = error_mark_node;
4025 /* Handle scalar types, including conversions. */
4027 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4028 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4030 /* Note that convert_for_assignment calls default_conversion
4031 for arrays and functions. We must not call it in the
4032 case where inside_init is a null pointer constant. */
4034 = convert_for_assignment (type, init, _("initialization"),
4035 NULL_TREE, NULL_TREE, 0);
4037 if (require_constant && ! TREE_CONSTANT (inside_init))
4039 error_init ("initializer element is not constant");
4040 inside_init = error_mark_node;
4042 else if (require_constant
4043 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4045 error_init ("initializer element is not computable at load time");
4046 inside_init = error_mark_node;
4052 /* Come here only for records and arrays. */
4054 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4056 error_init ("variable-sized object may not be initialized");
4057 return error_mark_node;
4060 error_init ("invalid initializer");
4061 return error_mark_node;
4064 /* Handle initializers that use braces. */
4066 /* Type of object we are accumulating a constructor for.
4067 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4068 static tree constructor_type;
4070 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4072 static tree constructor_fields;
4074 /* For an ARRAY_TYPE, this is the specified index
4075 at which to store the next element we get. */
4076 static tree constructor_index;
4078 /* For an ARRAY_TYPE, this is the maximum index. */
4079 static tree constructor_max_index;
4081 /* For a RECORD_TYPE, this is the first field not yet written out. */
4082 static tree constructor_unfilled_fields;
4084 /* For an ARRAY_TYPE, this is the index of the first element
4085 not yet written out. */
4086 static tree constructor_unfilled_index;
4088 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4089 This is so we can generate gaps between fields, when appropriate. */
4090 static tree constructor_bit_index;
4092 /* If we are saving up the elements rather than allocating them,
4093 this is the list of elements so far (in reverse order,
4094 most recent first). */
4095 static tree constructor_elements;
4097 /* 1 if constructor should be incrementally stored into a constructor chain,
4098 0 if all the elements should be kept in AVL tree. */
4099 static int constructor_incremental;
4101 /* 1 if so far this constructor's elements are all compile-time constants. */
4102 static int constructor_constant;
4104 /* 1 if so far this constructor's elements are all valid address constants. */
4105 static int constructor_simple;
4107 /* 1 if this constructor is erroneous so far. */
4108 static int constructor_erroneous;
4110 /* Structure for managing pending initializer elements, organized as an
4115 struct init_node *left, *right;
4116 struct init_node *parent;
4122 /* Tree of pending elements at this constructor level.
4123 These are elements encountered out of order
4124 which belong at places we haven't reached yet in actually
4126 Will never hold tree nodes across GC runs. */
4127 static struct init_node *constructor_pending_elts;
4129 /* The SPELLING_DEPTH of this constructor. */
4130 static int constructor_depth;
4132 /* 0 if implicitly pushing constructor levels is allowed. */
4133 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4135 static int require_constant_value;
4136 static int require_constant_elements;
4138 /* DECL node for which an initializer is being read.
4139 0 means we are reading a constructor expression
4140 such as (struct foo) {...}. */
4141 static tree constructor_decl;
4143 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4144 static const char *constructor_asmspec;
4146 /* Nonzero if this is an initializer for a top-level decl. */
4147 static int constructor_top_level;
4149 /* Nonzero if there were any member designators in this initializer. */
4150 static int constructor_designated;
4152 /* Nesting depth of designator list. */
4153 static int designator_depth;
4155 /* Nonzero if there were diagnosed errors in this designator list. */
4156 static int designator_errorneous;
4159 /* This stack has a level for each implicit or explicit level of
4160 structuring in the initializer, including the outermost one. It
4161 saves the values of most of the variables above. */
4163 struct constructor_range_stack;
4165 struct constructor_stack
4167 struct constructor_stack *next;
4172 tree unfilled_index;
4173 tree unfilled_fields;
4176 struct init_node *pending_elts;
4179 /* If nonzero, this value should replace the entire
4180 constructor at this level. */
4181 tree replacement_value;
4182 struct constructor_range_stack *range_stack;
4192 struct constructor_stack *constructor_stack;
4194 /* This stack represents designators from some range designator up to
4195 the last designator in the list. */
4197 struct constructor_range_stack
4199 struct constructor_range_stack *next, *prev;
4200 struct constructor_stack *stack;
4207 struct constructor_range_stack *constructor_range_stack;
4209 /* This stack records separate initializers that are nested.
4210 Nested initializers can't happen in ANSI C, but GNU C allows them
4211 in cases like { ... (struct foo) { ... } ... }. */
4213 struct initializer_stack
4215 struct initializer_stack *next;
4217 const char *asmspec;
4218 struct constructor_stack *constructor_stack;
4219 struct constructor_range_stack *constructor_range_stack;
4221 struct spelling *spelling;
4222 struct spelling *spelling_base;
4225 char require_constant_value;
4226 char require_constant_elements;
4229 struct initializer_stack *initializer_stack;
4231 /* Prepare to parse and output the initializer for variable DECL. */
4234 start_init (tree decl, tree asmspec_tree, int top_level)
4237 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4238 const char *asmspec = 0;
4241 asmspec = TREE_STRING_POINTER (asmspec_tree);
4243 p->decl = constructor_decl;
4244 p->asmspec = constructor_asmspec;
4245 p->require_constant_value = require_constant_value;
4246 p->require_constant_elements = require_constant_elements;
4247 p->constructor_stack = constructor_stack;
4248 p->constructor_range_stack = constructor_range_stack;
4249 p->elements = constructor_elements;
4250 p->spelling = spelling;
4251 p->spelling_base = spelling_base;
4252 p->spelling_size = spelling_size;
4253 p->top_level = constructor_top_level;
4254 p->next = initializer_stack;
4255 initializer_stack = p;
4257 constructor_decl = decl;
4258 constructor_asmspec = asmspec;
4259 constructor_designated = 0;
4260 constructor_top_level = top_level;
4264 require_constant_value = TREE_STATIC (decl);
4265 require_constant_elements
4266 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4267 /* For a scalar, you can always use any value to initialize,
4268 even within braces. */
4269 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4270 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4271 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4272 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4273 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4277 require_constant_value = 0;
4278 require_constant_elements = 0;
4279 locus = "(anonymous)";
4282 constructor_stack = 0;
4283 constructor_range_stack = 0;
4285 missing_braces_mentioned = 0;
4289 RESTORE_SPELLING_DEPTH (0);
4292 push_string (locus);
4298 struct initializer_stack *p = initializer_stack;
4300 /* Free the whole constructor stack of this initializer. */
4301 while (constructor_stack)
4303 struct constructor_stack *q = constructor_stack;
4304 constructor_stack = q->next;
4308 if (constructor_range_stack)
4311 /* Pop back to the data of the outer initializer (if any). */
4312 free (spelling_base);
4314 constructor_decl = p->decl;
4315 constructor_asmspec = p->asmspec;
4316 require_constant_value = p->require_constant_value;
4317 require_constant_elements = p->require_constant_elements;
4318 constructor_stack = p->constructor_stack;
4319 constructor_range_stack = p->constructor_range_stack;
4320 constructor_elements = p->elements;
4321 spelling = p->spelling;
4322 spelling_base = p->spelling_base;
4323 spelling_size = p->spelling_size;
4324 constructor_top_level = p->top_level;
4325 initializer_stack = p->next;
4329 /* Call here when we see the initializer is surrounded by braces.
4330 This is instead of a call to push_init_level;
4331 it is matched by a call to pop_init_level.
4333 TYPE is the type to initialize, for a constructor expression.
4334 For an initializer for a decl, TYPE is zero. */
4337 really_start_incremental_init (tree type)
4339 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4342 type = TREE_TYPE (constructor_decl);
4344 if (targetm.vector_opaque_p (type))
4345 error ("opaque vector types cannot be initialized");
4347 p->type = constructor_type;
4348 p->fields = constructor_fields;
4349 p->index = constructor_index;
4350 p->max_index = constructor_max_index;
4351 p->unfilled_index = constructor_unfilled_index;
4352 p->unfilled_fields = constructor_unfilled_fields;
4353 p->bit_index = constructor_bit_index;
4354 p->elements = constructor_elements;
4355 p->constant = constructor_constant;
4356 p->simple = constructor_simple;
4357 p->erroneous = constructor_erroneous;
4358 p->pending_elts = constructor_pending_elts;
4359 p->depth = constructor_depth;
4360 p->replacement_value = 0;
4364 p->incremental = constructor_incremental;
4365 p->designated = constructor_designated;
4367 constructor_stack = p;
4369 constructor_constant = 1;
4370 constructor_simple = 1;
4371 constructor_depth = SPELLING_DEPTH ();
4372 constructor_elements = 0;
4373 constructor_pending_elts = 0;
4374 constructor_type = type;
4375 constructor_incremental = 1;
4376 constructor_designated = 0;
4377 designator_depth = 0;
4378 designator_errorneous = 0;
4380 if (TREE_CODE (constructor_type) == RECORD_TYPE
4381 || TREE_CODE (constructor_type) == UNION_TYPE)
4383 constructor_fields = TYPE_FIELDS (constructor_type);
4384 /* Skip any nameless bit fields at the beginning. */
4385 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4386 && DECL_NAME (constructor_fields) == 0)
4387 constructor_fields = TREE_CHAIN (constructor_fields);
4389 constructor_unfilled_fields = constructor_fields;
4390 constructor_bit_index = bitsize_zero_node;
4392 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4394 if (TYPE_DOMAIN (constructor_type))
4396 constructor_max_index
4397 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4399 /* Detect non-empty initializations of zero-length arrays. */
4400 if (constructor_max_index == NULL_TREE
4401 && TYPE_SIZE (constructor_type))
4402 constructor_max_index = build_int_2 (-1, -1);
4404 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4405 to initialize VLAs will cause a proper error; avoid tree
4406 checking errors as well by setting a safe value. */
4407 if (constructor_max_index
4408 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4409 constructor_max_index = build_int_2 (-1, -1);
4412 = convert (bitsizetype,
4413 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4416 constructor_index = bitsize_zero_node;
4418 constructor_unfilled_index = constructor_index;
4420 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4422 /* Vectors are like simple fixed-size arrays. */
4423 constructor_max_index =
4424 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4425 constructor_index = convert (bitsizetype, bitsize_zero_node);
4426 constructor_unfilled_index = constructor_index;
4430 /* Handle the case of int x = {5}; */
4431 constructor_fields = constructor_type;
4432 constructor_unfilled_fields = constructor_type;
4436 /* Push down into a subobject, for initialization.
4437 If this is for an explicit set of braces, IMPLICIT is 0.
4438 If it is because the next element belongs at a lower level,
4439 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4442 push_init_level (int implicit)
4444 struct constructor_stack *p;
4445 tree value = NULL_TREE;
4447 /* If we've exhausted any levels that didn't have braces,
4449 while (constructor_stack->implicit)
4451 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4452 || TREE_CODE (constructor_type) == UNION_TYPE)
4453 && constructor_fields == 0)
4454 process_init_element (pop_init_level (1));
4455 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4456 && constructor_max_index
4457 && tree_int_cst_lt (constructor_max_index, constructor_index))
4458 process_init_element (pop_init_level (1));
4463 /* Unless this is an explicit brace, we need to preserve previous
4467 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4468 || TREE_CODE (constructor_type) == UNION_TYPE)
4469 && constructor_fields)
4470 value = find_init_member (constructor_fields);
4471 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4472 value = find_init_member (constructor_index);
4475 p = xmalloc (sizeof (struct constructor_stack));
4476 p->type = constructor_type;
4477 p->fields = constructor_fields;
4478 p->index = constructor_index;
4479 p->max_index = constructor_max_index;
4480 p->unfilled_index = constructor_unfilled_index;
4481 p->unfilled_fields = constructor_unfilled_fields;
4482 p->bit_index = constructor_bit_index;
4483 p->elements = constructor_elements;
4484 p->constant = constructor_constant;
4485 p->simple = constructor_simple;
4486 p->erroneous = constructor_erroneous;
4487 p->pending_elts = constructor_pending_elts;
4488 p->depth = constructor_depth;
4489 p->replacement_value = 0;
4490 p->implicit = implicit;
4492 p->incremental = constructor_incremental;
4493 p->designated = constructor_designated;
4494 p->next = constructor_stack;
4496 constructor_stack = p;
4498 constructor_constant = 1;
4499 constructor_simple = 1;
4500 constructor_depth = SPELLING_DEPTH ();
4501 constructor_elements = 0;
4502 constructor_incremental = 1;
4503 constructor_designated = 0;
4504 constructor_pending_elts = 0;
4507 p->range_stack = constructor_range_stack;
4508 constructor_range_stack = 0;
4509 designator_depth = 0;
4510 designator_errorneous = 0;
4513 /* Don't die if an entire brace-pair level is superfluous
4514 in the containing level. */
4515 if (constructor_type == 0)
4517 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4518 || TREE_CODE (constructor_type) == UNION_TYPE)
4520 /* Don't die if there are extra init elts at the end. */
4521 if (constructor_fields == 0)
4522 constructor_type = 0;
4525 constructor_type = TREE_TYPE (constructor_fields);
4526 push_member_name (constructor_fields);
4527 constructor_depth++;
4530 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4532 constructor_type = TREE_TYPE (constructor_type);
4533 push_array_bounds (tree_low_cst (constructor_index, 0));
4534 constructor_depth++;
4537 if (constructor_type == 0)
4539 error_init ("extra brace group at end of initializer");
4540 constructor_fields = 0;
4541 constructor_unfilled_fields = 0;
4545 if (value && TREE_CODE (value) == CONSTRUCTOR)
4547 constructor_constant = TREE_CONSTANT (value);
4548 constructor_simple = TREE_STATIC (value);
4549 constructor_elements = CONSTRUCTOR_ELTS (value);
4550 if (constructor_elements
4551 && (TREE_CODE (constructor_type) == RECORD_TYPE
4552 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4553 set_nonincremental_init ();
4556 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4558 missing_braces_mentioned = 1;
4559 warning_init ("missing braces around initializer");
4562 if (TREE_CODE (constructor_type) == RECORD_TYPE
4563 || TREE_CODE (constructor_type) == UNION_TYPE)
4565 constructor_fields = TYPE_FIELDS (constructor_type);
4566 /* Skip any nameless bit fields at the beginning. */
4567 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4568 && DECL_NAME (constructor_fields) == 0)
4569 constructor_fields = TREE_CHAIN (constructor_fields);
4571 constructor_unfilled_fields = constructor_fields;
4572 constructor_bit_index = bitsize_zero_node;
4574 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4576 /* Vectors are like simple fixed-size arrays. */
4577 constructor_max_index =
4578 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4579 constructor_index = convert (bitsizetype, integer_zero_node);
4580 constructor_unfilled_index = constructor_index;
4582 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4584 if (TYPE_DOMAIN (constructor_type))
4586 constructor_max_index
4587 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4589 /* Detect non-empty initializations of zero-length arrays. */
4590 if (constructor_max_index == NULL_TREE
4591 && TYPE_SIZE (constructor_type))
4592 constructor_max_index = build_int_2 (-1, -1);
4594 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4595 to initialize VLAs will cause a proper error; avoid tree
4596 checking errors as well by setting a safe value. */
4597 if (constructor_max_index
4598 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4599 constructor_max_index = build_int_2 (-1, -1);
4602 = convert (bitsizetype,
4603 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4606 constructor_index = bitsize_zero_node;
4608 constructor_unfilled_index = constructor_index;
4609 if (value && TREE_CODE (value) == STRING_CST)
4611 /* We need to split the char/wchar array into individual
4612 characters, so that we don't have to special case it
4614 set_nonincremental_init_from_string (value);
4619 warning_init ("braces around scalar initializer");
4620 constructor_fields = constructor_type;
4621 constructor_unfilled_fields = constructor_type;
4625 /* At the end of an implicit or explicit brace level,
4626 finish up that level of constructor.
4627 If we were outputting the elements as they are read, return 0
4628 from inner levels (process_init_element ignores that),
4629 but return error_mark_node from the outermost level
4630 (that's what we want to put in DECL_INITIAL).
4631 Otherwise, return a CONSTRUCTOR expression. */
4634 pop_init_level (int implicit)
4636 struct constructor_stack *p;
4637 tree constructor = 0;
4641 /* When we come to an explicit close brace,
4642 pop any inner levels that didn't have explicit braces. */
4643 while (constructor_stack->implicit)
4644 process_init_element (pop_init_level (1));
4646 if (constructor_range_stack)
4650 /* Now output all pending elements. */
4651 constructor_incremental = 1;
4652 output_pending_init_elements (1);
4654 p = constructor_stack;
4656 /* Error for initializing a flexible array member, or a zero-length
4657 array member in an inappropriate context. */
4658 if (constructor_type && constructor_fields
4659 && TREE_CODE (constructor_type) == ARRAY_TYPE
4660 && TYPE_DOMAIN (constructor_type)
4661 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4663 /* Silently discard empty initializations. The parser will
4664 already have pedwarned for empty brackets. */
4665 if (integer_zerop (constructor_unfilled_index))
4666 constructor_type = NULL_TREE;
4667 else if (! TYPE_SIZE (constructor_type))
4669 if (constructor_depth > 2)
4670 error_init ("initialization of flexible array member in a nested context");
4672 pedwarn_init ("initialization of a flexible array member");
4674 /* We have already issued an error message for the existence
4675 of a flexible array member not at the end of the structure.
4676 Discard the initializer so that we do not abort later. */
4677 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4678 constructor_type = NULL_TREE;
4681 /* Zero-length arrays are no longer special, so we should no longer
4686 /* Warn when some struct elements are implicitly initialized to zero. */
4689 && TREE_CODE (constructor_type) == RECORD_TYPE
4690 && constructor_unfilled_fields)
4692 /* Do not warn for flexible array members or zero-length arrays. */
4693 while (constructor_unfilled_fields
4694 && (! DECL_SIZE (constructor_unfilled_fields)
4695 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4696 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4698 /* Do not warn if this level of the initializer uses member
4699 designators; it is likely to be deliberate. */
4700 if (constructor_unfilled_fields && !constructor_designated)
4702 push_member_name (constructor_unfilled_fields);
4703 warning_init ("missing initializer");
4704 RESTORE_SPELLING_DEPTH (constructor_depth);
4708 /* Pad out the end of the structure. */
4709 if (p->replacement_value)
4710 /* If this closes a superfluous brace pair,
4711 just pass out the element between them. */
4712 constructor = p->replacement_value;
4713 else if (constructor_type == 0)
4715 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4716 && TREE_CODE (constructor_type) != UNION_TYPE
4717 && TREE_CODE (constructor_type) != ARRAY_TYPE
4718 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4720 /* A nonincremental scalar initializer--just return
4721 the element, after verifying there is just one. */
4722 if (constructor_elements == 0)
4724 if (!constructor_erroneous)
4725 error_init ("empty scalar initializer");
4726 constructor = error_mark_node;
4728 else if (TREE_CHAIN (constructor_elements) != 0)
4730 error_init ("extra elements in scalar initializer");
4731 constructor = TREE_VALUE (constructor_elements);
4734 constructor = TREE_VALUE (constructor_elements);
4738 if (constructor_erroneous)
4739 constructor = error_mark_node;
4742 constructor = build_constructor (constructor_type,
4743 nreverse (constructor_elements));
4744 if (constructor_constant)
4745 TREE_CONSTANT (constructor) = 1;
4746 if (constructor_constant && constructor_simple)
4747 TREE_STATIC (constructor) = 1;
4751 constructor_type = p->type;
4752 constructor_fields = p->fields;
4753 constructor_index = p->index;
4754 constructor_max_index = p->max_index;
4755 constructor_unfilled_index = p->unfilled_index;
4756 constructor_unfilled_fields = p->unfilled_fields;
4757 constructor_bit_index = p->bit_index;
4758 constructor_elements = p->elements;
4759 constructor_constant = p->constant;
4760 constructor_simple = p->simple;
4761 constructor_erroneous = p->erroneous;
4762 constructor_incremental = p->incremental;
4763 constructor_designated = p->designated;
4764 constructor_pending_elts = p->pending_elts;
4765 constructor_depth = p->depth;
4767 constructor_range_stack = p->range_stack;
4768 RESTORE_SPELLING_DEPTH (constructor_depth);
4770 constructor_stack = p->next;
4773 if (constructor == 0)
4775 if (constructor_stack == 0)
4776 return error_mark_node;
4782 /* Common handling for both array range and field name designators.
4783 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4786 set_designator (int array)
4789 enum tree_code subcode;
4791 /* Don't die if an entire brace-pair level is superfluous
4792 in the containing level. */
4793 if (constructor_type == 0)
4796 /* If there were errors in this designator list already, bail out silently. */
4797 if (designator_errorneous)
4800 if (!designator_depth)
4802 if (constructor_range_stack)
4805 /* Designator list starts at the level of closest explicit
4807 while (constructor_stack->implicit)
4808 process_init_element (pop_init_level (1));
4809 constructor_designated = 1;
4813 if (constructor_no_implicit)
4815 error_init ("initialization designators may not nest");
4819 if (TREE_CODE (constructor_type) == RECORD_TYPE
4820 || TREE_CODE (constructor_type) == UNION_TYPE)
4822 subtype = TREE_TYPE (constructor_fields);
4823 if (subtype != error_mark_node)
4824 subtype = TYPE_MAIN_VARIANT (subtype);
4826 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4828 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4833 subcode = TREE_CODE (subtype);
4834 if (array && subcode != ARRAY_TYPE)
4836 error_init ("array index in non-array initializer");
4839 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4841 error_init ("field name not in record or union initializer");
4845 constructor_designated = 1;
4846 push_init_level (2);
4850 /* If there are range designators in designator list, push a new designator
4851 to constructor_range_stack. RANGE_END is end of such stack range or
4852 NULL_TREE if there is no range designator at this level. */
4855 push_range_stack (tree range_end)
4857 struct constructor_range_stack *p;
4859 p = ggc_alloc (sizeof (struct constructor_range_stack));
4860 p->prev = constructor_range_stack;
4862 p->fields = constructor_fields;
4863 p->range_start = constructor_index;
4864 p->index = constructor_index;
4865 p->stack = constructor_stack;
4866 p->range_end = range_end;
4867 if (constructor_range_stack)
4868 constructor_range_stack->next = p;
4869 constructor_range_stack = p;
4872 /* Within an array initializer, specify the next index to be initialized.
4873 FIRST is that index. If LAST is nonzero, then initialize a range
4874 of indices, running from FIRST through LAST. */
4877 set_init_index (tree first, tree last)
4879 if (set_designator (1))
4882 designator_errorneous = 1;
4884 while ((TREE_CODE (first) == NOP_EXPR
4885 || TREE_CODE (first) == CONVERT_EXPR
4886 || TREE_CODE (first) == NON_LVALUE_EXPR)
4887 && (TYPE_MODE (TREE_TYPE (first))
4888 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4889 first = TREE_OPERAND (first, 0);
4892 while ((TREE_CODE (last) == NOP_EXPR
4893 || TREE_CODE (last) == CONVERT_EXPR
4894 || TREE_CODE (last) == NON_LVALUE_EXPR)
4895 && (TYPE_MODE (TREE_TYPE (last))
4896 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4897 last = TREE_OPERAND (last, 0);
4899 if (TREE_CODE (first) != INTEGER_CST)
4900 error_init ("nonconstant array index in initializer");
4901 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4902 error_init ("nonconstant array index in initializer");
4903 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4904 error_init ("array index in non-array initializer");
4905 else if (tree_int_cst_sgn (first) == -1)
4906 error_init ("array index in initializer exceeds array bounds");
4907 else if (constructor_max_index
4908 && tree_int_cst_lt (constructor_max_index, first))
4909 error_init ("array index in initializer exceeds array bounds");
4912 constructor_index = convert (bitsizetype, first);
4916 if (tree_int_cst_equal (first, last))
4918 else if (tree_int_cst_lt (last, first))
4920 error_init ("empty index range in initializer");
4925 last = convert (bitsizetype, last);
4926 if (constructor_max_index != 0
4927 && tree_int_cst_lt (constructor_max_index, last))
4929 error_init ("array index range in initializer exceeds array bounds");
4936 designator_errorneous = 0;
4937 if (constructor_range_stack || last)
4938 push_range_stack (last);
4942 /* Within a struct initializer, specify the next field to be initialized. */
4945 set_init_label (tree fieldname)
4949 if (set_designator (0))
4952 designator_errorneous = 1;
4954 if (TREE_CODE (constructor_type) != RECORD_TYPE
4955 && TREE_CODE (constructor_type) != UNION_TYPE)
4957 error_init ("field name not in record or union initializer");
4961 for (tail = TYPE_FIELDS (constructor_type); tail;
4962 tail = TREE_CHAIN (tail))
4964 if (DECL_NAME (tail) == fieldname)
4969 error ("unknown field `%s' specified in initializer",
4970 IDENTIFIER_POINTER (fieldname));
4973 constructor_fields = tail;
4975 designator_errorneous = 0;
4976 if (constructor_range_stack)
4977 push_range_stack (NULL_TREE);
4981 /* Add a new initializer to the tree of pending initializers. PURPOSE
4982 identifies the initializer, either array index or field in a structure.
4983 VALUE is the value of that index or field. */
4986 add_pending_init (tree purpose, tree value)
4988 struct init_node *p, **q, *r;
4990 q = &constructor_pending_elts;
4993 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4998 if (tree_int_cst_lt (purpose, p->purpose))
5000 else if (tree_int_cst_lt (p->purpose, purpose))
5004 if (TREE_SIDE_EFFECTS (p->value))
5005 warning_init ("initialized field with side-effects overwritten");
5015 bitpos = bit_position (purpose);
5019 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5021 else if (p->purpose != purpose)
5025 if (TREE_SIDE_EFFECTS (p->value))
5026 warning_init ("initialized field with side-effects overwritten");
5033 r = ggc_alloc (sizeof (struct init_node));
5034 r->purpose = purpose;
5045 struct init_node *s;
5049 if (p->balance == 0)
5051 else if (p->balance < 0)
5058 p->left->parent = p;
5075 constructor_pending_elts = r;
5080 struct init_node *t = r->right;
5084 r->right->parent = r;
5089 p->left->parent = p;
5092 p->balance = t->balance < 0;
5093 r->balance = -(t->balance > 0);
5108 constructor_pending_elts = t;
5114 /* p->balance == +1; growth of left side balances the node. */
5119 else /* r == p->right */
5121 if (p->balance == 0)
5122 /* Growth propagation from right side. */
5124 else if (p->balance > 0)
5131 p->right->parent = p;
5148 constructor_pending_elts = r;
5150 else /* r->balance == -1 */
5153 struct init_node *t = r->left;
5157 r->left->parent = r;
5162 p->right->parent = p;
5165 r->balance = (t->balance < 0);
5166 p->balance = -(t->balance > 0);
5181 constructor_pending_elts = t;
5187 /* p->balance == -1; growth of right side balances the node. */
5198 /* Build AVL tree from a sorted chain. */
5201 set_nonincremental_init (void)
5205 if (TREE_CODE (constructor_type) != RECORD_TYPE
5206 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5209 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5210 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5211 constructor_elements = 0;
5212 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5214 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5215 /* Skip any nameless bit fields at the beginning. */
5216 while (constructor_unfilled_fields != 0
5217 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5218 && DECL_NAME (constructor_unfilled_fields) == 0)
5219 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5222 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5224 if (TYPE_DOMAIN (constructor_type))
5225 constructor_unfilled_index
5226 = convert (bitsizetype,
5227 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5229 constructor_unfilled_index = bitsize_zero_node;
5231 constructor_incremental = 0;
5234 /* Build AVL tree from a string constant. */
5237 set_nonincremental_init_from_string (tree str)
5239 tree value, purpose, type;
5240 HOST_WIDE_INT val[2];
5241 const char *p, *end;
5242 int byte, wchar_bytes, charwidth, bitpos;
5244 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5247 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5248 == TYPE_PRECISION (char_type_node))
5250 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5251 == TYPE_PRECISION (wchar_type_node))
5252 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5256 charwidth = TYPE_PRECISION (char_type_node);
5257 type = TREE_TYPE (constructor_type);
5258 p = TREE_STRING_POINTER (str);
5259 end = p + TREE_STRING_LENGTH (str);
5261 for (purpose = bitsize_zero_node;
5262 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5263 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5265 if (wchar_bytes == 1)
5267 val[1] = (unsigned char) *p++;
5274 for (byte = 0; byte < wchar_bytes; byte++)
5276 if (BYTES_BIG_ENDIAN)
5277 bitpos = (wchar_bytes - byte - 1) * charwidth;
5279 bitpos = byte * charwidth;
5280 val[bitpos < HOST_BITS_PER_WIDE_INT]
5281 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5282 << (bitpos % HOST_BITS_PER_WIDE_INT);
5286 if (!TYPE_UNSIGNED (type))
5288 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5289 if (bitpos < HOST_BITS_PER_WIDE_INT)
5291 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5293 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5297 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5302 else if (val[0] & (((HOST_WIDE_INT) 1)
5303 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5304 val[0] |= ((HOST_WIDE_INT) -1)
5305 << (bitpos - HOST_BITS_PER_WIDE_INT);
5308 value = build_int_2 (val[1], val[0]);
5309 TREE_TYPE (value) = type;
5310 add_pending_init (purpose, value);
5313 constructor_incremental = 0;
5316 /* Return value of FIELD in pending initializer or zero if the field was
5317 not initialized yet. */
5320 find_init_member (tree field)
5322 struct init_node *p;
5324 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5326 if (constructor_incremental
5327 && tree_int_cst_lt (field, constructor_unfilled_index))
5328 set_nonincremental_init ();
5330 p = constructor_pending_elts;
5333 if (tree_int_cst_lt (field, p->purpose))
5335 else if (tree_int_cst_lt (p->purpose, field))
5341 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5343 tree bitpos = bit_position (field);
5345 if (constructor_incremental
5346 && (!constructor_unfilled_fields
5347 || tree_int_cst_lt (bitpos,
5348 bit_position (constructor_unfilled_fields))))
5349 set_nonincremental_init ();
5351 p = constructor_pending_elts;
5354 if (field == p->purpose)
5356 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5362 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5364 if (constructor_elements
5365 && TREE_PURPOSE (constructor_elements) == field)
5366 return TREE_VALUE (constructor_elements);
5371 /* "Output" the next constructor element.
5372 At top level, really output it to assembler code now.
5373 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5374 TYPE is the data type that the containing data type wants here.
5375 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5377 PENDING if non-nil means output pending elements that belong
5378 right after this element. (PENDING is normally 1;
5379 it is 0 while outputting pending elements, to avoid recursion.) */
5382 output_init_element (tree value, tree type, tree field, int pending)
5384 if (type == error_mark_node)
5386 constructor_erroneous = 1;
5389 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5390 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5391 && !(TREE_CODE (value) == STRING_CST
5392 && TREE_CODE (type) == ARRAY_TYPE
5393 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5394 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5395 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5396 value = default_conversion (value);
5398 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5399 && require_constant_value && !flag_isoc99 && pending)
5401 /* As an extension, allow initializing objects with static storage
5402 duration with compound literals (which are then treated just as
5403 the brace enclosed list they contain). */
5404 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5405 value = DECL_INITIAL (decl);
5408 if (value == error_mark_node)
5409 constructor_erroneous = 1;
5410 else if (!TREE_CONSTANT (value))
5411 constructor_constant = 0;
5412 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5413 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5414 || TREE_CODE (constructor_type) == UNION_TYPE)
5415 && DECL_C_BIT_FIELD (field)
5416 && TREE_CODE (value) != INTEGER_CST))
5417 constructor_simple = 0;
5419 if (require_constant_value && ! TREE_CONSTANT (value))
5421 error_init ("initializer element is not constant");
5422 value = error_mark_node;
5424 else if (require_constant_elements
5425 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5426 pedwarn ("initializer element is not computable at load time");
5428 /* If this field is empty (and not at the end of structure),
5429 don't do anything other than checking the initializer. */
5431 && (TREE_TYPE (field) == error_mark_node
5432 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5433 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5434 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5435 || TREE_CHAIN (field)))))
5438 value = digest_init (type, value, require_constant_value);
5439 if (value == error_mark_node)
5441 constructor_erroneous = 1;
5445 /* If this element doesn't come next in sequence,
5446 put it on constructor_pending_elts. */
5447 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5448 && (!constructor_incremental
5449 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5451 if (constructor_incremental
5452 && tree_int_cst_lt (field, constructor_unfilled_index))
5453 set_nonincremental_init ();
5455 add_pending_init (field, value);
5458 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5459 && (!constructor_incremental
5460 || field != constructor_unfilled_fields))
5462 /* We do this for records but not for unions. In a union,
5463 no matter which field is specified, it can be initialized
5464 right away since it starts at the beginning of the union. */
5465 if (constructor_incremental)
5467 if (!constructor_unfilled_fields)
5468 set_nonincremental_init ();
5471 tree bitpos, unfillpos;
5473 bitpos = bit_position (field);
5474 unfillpos = bit_position (constructor_unfilled_fields);
5476 if (tree_int_cst_lt (bitpos, unfillpos))
5477 set_nonincremental_init ();
5481 add_pending_init (field, value);
5484 else if (TREE_CODE (constructor_type) == UNION_TYPE
5485 && constructor_elements)
5487 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5488 warning_init ("initialized field with side-effects overwritten");
5490 /* We can have just one union field set. */
5491 constructor_elements = 0;
5494 /* Otherwise, output this element either to
5495 constructor_elements or to the assembler file. */
5497 if (field && TREE_CODE (field) == INTEGER_CST)
5498 field = copy_node (field);
5499 constructor_elements
5500 = tree_cons (field, value, constructor_elements);
5502 /* Advance the variable that indicates sequential elements output. */
5503 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5504 constructor_unfilled_index
5505 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5507 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5509 constructor_unfilled_fields
5510 = TREE_CHAIN (constructor_unfilled_fields);
5512 /* Skip any nameless bit fields. */
5513 while (constructor_unfilled_fields != 0
5514 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5515 && DECL_NAME (constructor_unfilled_fields) == 0)
5516 constructor_unfilled_fields =
5517 TREE_CHAIN (constructor_unfilled_fields);
5519 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5520 constructor_unfilled_fields = 0;
5522 /* Now output any pending elements which have become next. */
5524 output_pending_init_elements (0);
5527 /* Output any pending elements which have become next.
5528 As we output elements, constructor_unfilled_{fields,index}
5529 advances, which may cause other elements to become next;
5530 if so, they too are output.
5532 If ALL is 0, we return when there are
5533 no more pending elements to output now.
5535 If ALL is 1, we output space as necessary so that
5536 we can output all the pending elements. */
5539 output_pending_init_elements (int all)
5541 struct init_node *elt = constructor_pending_elts;
5546 /* Look through the whole pending tree.
5547 If we find an element that should be output now,
5548 output it. Otherwise, set NEXT to the element
5549 that comes first among those still pending. */
5554 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5556 if (tree_int_cst_equal (elt->purpose,
5557 constructor_unfilled_index))
5558 output_init_element (elt->value,
5559 TREE_TYPE (constructor_type),
5560 constructor_unfilled_index, 0);
5561 else if (tree_int_cst_lt (constructor_unfilled_index,
5564 /* Advance to the next smaller node. */
5569 /* We have reached the smallest node bigger than the
5570 current unfilled index. Fill the space first. */
5571 next = elt->purpose;
5577 /* Advance to the next bigger node. */
5582 /* We have reached the biggest node in a subtree. Find
5583 the parent of it, which is the next bigger node. */
5584 while (elt->parent && elt->parent->right == elt)
5587 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5590 next = elt->purpose;
5596 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5597 || TREE_CODE (constructor_type) == UNION_TYPE)
5599 tree ctor_unfilled_bitpos, elt_bitpos;
5601 /* If the current record is complete we are done. */
5602 if (constructor_unfilled_fields == 0)
5605 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5606 elt_bitpos = bit_position (elt->purpose);
5607 /* We can't compare fields here because there might be empty
5608 fields in between. */
5609 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5611 constructor_unfilled_fields = elt->purpose;
5612 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5615 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5617 /* Advance to the next smaller node. */
5622 /* We have reached the smallest node bigger than the
5623 current unfilled field. Fill the space first. */
5624 next = elt->purpose;
5630 /* Advance to the next bigger node. */
5635 /* We have reached the biggest node in a subtree. Find
5636 the parent of it, which is the next bigger node. */
5637 while (elt->parent && elt->parent->right == elt)
5641 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5642 bit_position (elt->purpose))))
5644 next = elt->purpose;
5652 /* Ordinarily return, but not if we want to output all
5653 and there are elements left. */
5654 if (! (all && next != 0))
5657 /* If it's not incremental, just skip over the gap, so that after
5658 jumping to retry we will output the next successive element. */
5659 if (TREE_CODE (constructor_type) == RECORD_TYPE
5660 || TREE_CODE (constructor_type) == UNION_TYPE)
5661 constructor_unfilled_fields = next;
5662 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5663 constructor_unfilled_index = next;
5665 /* ELT now points to the node in the pending tree with the next
5666 initializer to output. */
5670 /* Add one non-braced element to the current constructor level.
5671 This adjusts the current position within the constructor's type.
5672 This may also start or terminate implicit levels
5673 to handle a partly-braced initializer.
5675 Once this has found the correct level for the new element,
5676 it calls output_init_element. */
5679 process_init_element (tree value)
5681 tree orig_value = value;
5682 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5684 designator_depth = 0;
5685 designator_errorneous = 0;
5687 /* Handle superfluous braces around string cst as in
5688 char x[] = {"foo"}; */
5691 && TREE_CODE (constructor_type) == ARRAY_TYPE
5692 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5693 && integer_zerop (constructor_unfilled_index))
5695 if (constructor_stack->replacement_value)
5696 error_init ("excess elements in char array initializer");
5697 constructor_stack->replacement_value = value;
5701 if (constructor_stack->replacement_value != 0)
5703 error_init ("excess elements in struct initializer");
5707 /* Ignore elements of a brace group if it is entirely superfluous
5708 and has already been diagnosed. */
5709 if (constructor_type == 0)
5712 /* If we've exhausted any levels that didn't have braces,
5714 while (constructor_stack->implicit)
5716 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5717 || TREE_CODE (constructor_type) == UNION_TYPE)
5718 && constructor_fields == 0)
5719 process_init_element (pop_init_level (1));
5720 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5721 && (constructor_max_index == 0
5722 || tree_int_cst_lt (constructor_max_index,
5723 constructor_index)))
5724 process_init_element (pop_init_level (1));
5729 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5730 if (constructor_range_stack)
5732 /* If value is a compound literal and we'll be just using its
5733 content, don't put it into a SAVE_EXPR. */
5734 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5735 || !require_constant_value
5737 value = save_expr (value);
5742 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5745 enum tree_code fieldcode;
5747 if (constructor_fields == 0)
5749 pedwarn_init ("excess elements in struct initializer");
5753 fieldtype = TREE_TYPE (constructor_fields);
5754 if (fieldtype != error_mark_node)
5755 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5756 fieldcode = TREE_CODE (fieldtype);
5758 /* Error for non-static initialization of a flexible array member. */
5759 if (fieldcode == ARRAY_TYPE
5760 && !require_constant_value
5761 && TYPE_SIZE (fieldtype) == NULL_TREE
5762 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5764 error_init ("non-static initialization of a flexible array member");
5768 /* Accept a string constant to initialize a subarray. */
5770 && fieldcode == ARRAY_TYPE
5771 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5774 /* Otherwise, if we have come to a subaggregate,
5775 and we don't have an element of its type, push into it. */
5776 else if (value != 0 && !constructor_no_implicit
5777 && value != error_mark_node
5778 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5779 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5780 || fieldcode == UNION_TYPE))
5782 push_init_level (1);
5788 push_member_name (constructor_fields);
5789 output_init_element (value, fieldtype, constructor_fields, 1);
5790 RESTORE_SPELLING_DEPTH (constructor_depth);
5793 /* Do the bookkeeping for an element that was
5794 directly output as a constructor. */
5796 /* For a record, keep track of end position of last field. */
5797 if (DECL_SIZE (constructor_fields))
5798 constructor_bit_index
5799 = size_binop (PLUS_EXPR,
5800 bit_position (constructor_fields),
5801 DECL_SIZE (constructor_fields));
5803 /* If the current field was the first one not yet written out,
5804 it isn't now, so update. */
5805 if (constructor_unfilled_fields == constructor_fields)
5807 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5808 /* Skip any nameless bit fields. */
5809 while (constructor_unfilled_fields != 0
5810 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5811 && DECL_NAME (constructor_unfilled_fields) == 0)
5812 constructor_unfilled_fields =
5813 TREE_CHAIN (constructor_unfilled_fields);
5817 constructor_fields = TREE_CHAIN (constructor_fields);
5818 /* Skip any nameless bit fields at the beginning. */
5819 while (constructor_fields != 0
5820 && DECL_C_BIT_FIELD (constructor_fields)
5821 && DECL_NAME (constructor_fields) == 0)
5822 constructor_fields = TREE_CHAIN (constructor_fields);
5824 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5827 enum tree_code fieldcode;
5829 if (constructor_fields == 0)
5831 pedwarn_init ("excess elements in union initializer");
5835 fieldtype = TREE_TYPE (constructor_fields);
5836 if (fieldtype != error_mark_node)
5837 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5838 fieldcode = TREE_CODE (fieldtype);
5840 /* Warn that traditional C rejects initialization of unions.
5841 We skip the warning if the value is zero. This is done
5842 under the assumption that the zero initializer in user
5843 code appears conditioned on e.g. __STDC__ to avoid
5844 "missing initializer" warnings and relies on default
5845 initialization to zero in the traditional C case.
5846 We also skip the warning if the initializer is designated,
5847 again on the assumption that this must be conditional on
5848 __STDC__ anyway (and we've already complained about the
5849 member-designator already). */
5850 if (warn_traditional && !in_system_header && !constructor_designated
5851 && !(value && (integer_zerop (value) || real_zerop (value))))
5852 warning ("traditional C rejects initialization of unions");
5854 /* Accept a string constant to initialize a subarray. */
5856 && fieldcode == ARRAY_TYPE
5857 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5860 /* Otherwise, if we have come to a subaggregate,
5861 and we don't have an element of its type, push into it. */
5862 else if (value != 0 && !constructor_no_implicit
5863 && value != error_mark_node
5864 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5865 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5866 || fieldcode == UNION_TYPE))
5868 push_init_level (1);
5874 push_member_name (constructor_fields);
5875 output_init_element (value, fieldtype, constructor_fields, 1);
5876 RESTORE_SPELLING_DEPTH (constructor_depth);
5879 /* Do the bookkeeping for an element that was
5880 directly output as a constructor. */
5882 constructor_bit_index = DECL_SIZE (constructor_fields);
5883 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5886 constructor_fields = 0;
5888 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5890 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5891 enum tree_code eltcode = TREE_CODE (elttype);
5893 /* Accept a string constant to initialize a subarray. */
5895 && eltcode == ARRAY_TYPE
5896 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5899 /* Otherwise, if we have come to a subaggregate,
5900 and we don't have an element of its type, push into it. */
5901 else if (value != 0 && !constructor_no_implicit
5902 && value != error_mark_node
5903 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5904 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5905 || eltcode == UNION_TYPE))
5907 push_init_level (1);
5911 if (constructor_max_index != 0
5912 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5913 || integer_all_onesp (constructor_max_index)))
5915 pedwarn_init ("excess elements in array initializer");
5919 /* Now output the actual element. */
5922 push_array_bounds (tree_low_cst (constructor_index, 0));
5923 output_init_element (value, elttype, constructor_index, 1);
5924 RESTORE_SPELLING_DEPTH (constructor_depth);
5928 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5931 /* If we are doing the bookkeeping for an element that was
5932 directly output as a constructor, we must update
5933 constructor_unfilled_index. */
5934 constructor_unfilled_index = constructor_index;
5936 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5938 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5940 /* Do a basic check of initializer size. Note that vectors
5941 always have a fixed size derived from their type. */
5942 if (tree_int_cst_lt (constructor_max_index, constructor_index))
5944 pedwarn_init ("excess elements in vector initializer");
5948 /* Now output the actual element. */
5950 output_init_element (value, elttype, constructor_index, 1);
5953 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5956 /* If we are doing the bookkeeping for an element that was
5957 directly output as a constructor, we must update
5958 constructor_unfilled_index. */
5959 constructor_unfilled_index = constructor_index;
5962 /* Handle the sole element allowed in a braced initializer
5963 for a scalar variable. */
5964 else if (constructor_fields == 0)
5966 pedwarn_init ("excess elements in scalar initializer");
5972 output_init_element (value, constructor_type, NULL_TREE, 1);
5973 constructor_fields = 0;
5976 /* Handle range initializers either at this level or anywhere higher
5977 in the designator stack. */
5978 if (constructor_range_stack)
5980 struct constructor_range_stack *p, *range_stack;
5983 range_stack = constructor_range_stack;
5984 constructor_range_stack = 0;
5985 while (constructor_stack != range_stack->stack)
5987 if (!constructor_stack->implicit)
5989 process_init_element (pop_init_level (1));
5991 for (p = range_stack;
5992 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
5995 if (!constructor_stack->implicit)
5997 process_init_element (pop_init_level (1));
6000 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6001 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6006 constructor_index = p->index;
6007 constructor_fields = p->fields;
6008 if (finish && p->range_end && p->index == p->range_start)
6016 push_init_level (2);
6017 p->stack = constructor_stack;
6018 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6019 p->index = p->range_start;
6023 constructor_range_stack = range_stack;
6030 constructor_range_stack = 0;
6033 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6034 (guaranteed to be 'volatile' or null) and ARGS (represented using
6035 an ASM_STMT node). */
6037 build_asm_stmt (tree cv_qualifier, tree args)
6039 if (!TREE_OPERAND (args, 0))
6040 TREE_OPERAND (args, 0) = cv_qualifier;
6041 return add_stmt (args);
6044 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6045 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6046 SIMPLE indicates whether there was anything at all after the
6047 string in the asm expression -- asm("blah") and asm("blah" : )
6048 are subtly different. We use a ASM_STMT node to represent this. */
6050 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6056 /* We can remove output conversions that change the type,
6057 but not the mode. */
6058 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6060 tree output = TREE_VALUE (tail);
6062 STRIP_NOPS (output);
6063 TREE_VALUE (tail) = output;
6065 /* Allow conversions as LHS here. build_modify_expr as called below
6066 will do the right thing with them. */
6067 while (TREE_CODE (output) == NOP_EXPR
6068 || TREE_CODE (output) == CONVERT_EXPR
6069 || TREE_CODE (output) == FLOAT_EXPR
6070 || TREE_CODE (output) == FIX_TRUNC_EXPR
6071 || TREE_CODE (output) == FIX_FLOOR_EXPR
6072 || TREE_CODE (output) == FIX_ROUND_EXPR
6073 || TREE_CODE (output) == FIX_CEIL_EXPR)
6074 output = TREE_OPERAND (output, 0);
6076 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6079 /* Remove output conversions that change the type but not the mode. */
6080 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6082 tree output = TREE_VALUE (tail);
6083 STRIP_NOPS (output);
6084 TREE_VALUE (tail) = output;
6087 /* Perform default conversions on array and function inputs.
6088 Don't do this for other types as it would screw up operands
6089 expected to be in memory. */
6090 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6091 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6093 args = build_stmt (ASM_STMT, 0, string, outputs, inputs, clobbers);
6095 /* Simple asm statements are treated as volatile. */
6098 TREE_OPERAND (args, 0) = ridpointers[RID_VOLATILE];
6099 ASM_INPUT_P (args) = 1;
6104 /* Expand an ASM statement with operands, handling output operands
6105 that are not variables or INDIRECT_REFS by transforming such
6106 cases into cases that expand_asm_operands can handle.
6108 Arguments are same as for expand_asm_operands. */
6111 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6112 tree clobbers, int vol, location_t locus)
6114 int noutputs = list_length (outputs);
6116 /* o[I] is the place that output number I should be written. */
6117 tree *o = alloca (noutputs * sizeof (tree));
6120 /* Record the contents of OUTPUTS before it is modified. */
6121 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6123 o[i] = TREE_VALUE (tail);
6124 if (o[i] == error_mark_node)
6128 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6129 OUTPUTS some trees for where the values were actually stored. */
6130 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
6132 /* Copy all the intermediate outputs into the specified outputs. */
6133 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6135 if (o[i] != TREE_VALUE (tail))
6137 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6138 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6141 /* Restore the original value so that it's correct the next
6142 time we expand this function. */
6143 TREE_VALUE (tail) = o[i];
6145 /* Detect modification of read-only values.
6146 (Otherwise done by build_modify_expr.) */
6149 tree type = TREE_TYPE (o[i]);
6150 if (TREE_READONLY (o[i])
6151 || TYPE_READONLY (type)
6152 || ((TREE_CODE (type) == RECORD_TYPE
6153 || TREE_CODE (type) == UNION_TYPE)
6154 && C_TYPE_FIELDS_READONLY (type)))
6155 readonly_error (o[i], "modification by `asm'");
6159 /* Those MODIFY_EXPRs could do autoincrements. */
6163 /* Expand a C `return' statement.
6164 RETVAL is the expression for what to return,
6165 or a null pointer for `return;' with no value. */
6168 c_expand_return (tree retval)
6170 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6172 if (TREE_THIS_VOLATILE (current_function_decl))
6173 warning ("function declared `noreturn' has a `return' statement");
6177 current_function_returns_null = 1;
6178 if ((warn_return_type || flag_isoc99)
6179 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6180 pedwarn_c99 ("`return' with no value, in function returning non-void");
6182 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6184 current_function_returns_null = 1;
6185 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6186 pedwarn ("`return' with a value, in function returning void");
6190 tree t = convert_for_assignment (valtype, retval, _("return"),
6191 NULL_TREE, NULL_TREE, 0);
6192 tree res = DECL_RESULT (current_function_decl);
6195 current_function_returns_value = 1;
6196 if (t == error_mark_node)
6199 inner = t = convert (TREE_TYPE (res), t);
6201 /* Strip any conversions, additions, and subtractions, and see if
6202 we are returning the address of a local variable. Warn if so. */
6205 switch (TREE_CODE (inner))
6207 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6209 inner = TREE_OPERAND (inner, 0);
6213 /* If the second operand of the MINUS_EXPR has a pointer
6214 type (or is converted from it), this may be valid, so
6215 don't give a warning. */
6217 tree op1 = TREE_OPERAND (inner, 1);
6219 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6220 && (TREE_CODE (op1) == NOP_EXPR
6221 || TREE_CODE (op1) == NON_LVALUE_EXPR
6222 || TREE_CODE (op1) == CONVERT_EXPR))
6223 op1 = TREE_OPERAND (op1, 0);
6225 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6228 inner = TREE_OPERAND (inner, 0);
6233 inner = TREE_OPERAND (inner, 0);
6235 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6236 inner = TREE_OPERAND (inner, 0);
6239 && ! DECL_EXTERNAL (inner)
6240 && ! TREE_STATIC (inner)
6241 && DECL_CONTEXT (inner) == current_function_decl)
6242 warning ("function returns address of local variable");
6252 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6255 return add_stmt (build_return_stmt (retval));
6259 /* The SWITCH_STMT being built. */
6261 /* A splay-tree mapping the low element of a case range to the high
6262 element, or NULL_TREE if there is no high element. Used to
6263 determine whether or not a new case label duplicates an old case
6264 label. We need a tree, rather than simply a hash table, because
6265 of the GNU case range extension. */
6267 /* The next node on the stack. */
6268 struct c_switch *next;
6271 /* A stack of the currently active switch statements. The innermost
6272 switch statement is on the top of the stack. There is no need to
6273 mark the stack for garbage collection because it is only active
6274 during the processing of the body of a function, and we never
6275 collect at that point. */
6277 static struct c_switch *switch_stack;
6279 /* Start a C switch statement, testing expression EXP. Return the new
6283 c_start_case (tree exp)
6285 enum tree_code code;
6286 tree type, orig_type = error_mark_node;
6287 struct c_switch *cs;
6289 if (exp != error_mark_node)
6291 code = TREE_CODE (TREE_TYPE (exp));
6292 orig_type = TREE_TYPE (exp);
6294 if (! INTEGRAL_TYPE_P (orig_type)
6295 && code != ERROR_MARK)
6297 error ("switch quantity not an integer");
6298 exp = integer_zero_node;
6302 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6304 if (warn_traditional && !in_system_header
6305 && (type == long_integer_type_node
6306 || type == long_unsigned_type_node))
6307 warning ("`long' switch expression not converted to `int' in ISO C");
6309 exp = default_conversion (exp);
6310 type = TREE_TYPE (exp);
6314 /* Add this new SWITCH_STMT to the stack. */
6315 cs = xmalloc (sizeof (*cs));
6316 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6317 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6318 cs->next = switch_stack;
6321 return add_stmt (switch_stack->switch_stmt);
6324 /* Process a case label. */
6327 do_case (tree low_value, tree high_value)
6329 tree label = NULL_TREE;
6333 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6335 label = c_add_case_label (switch_stack->cases,
6336 SWITCH_COND (switch_stack->switch_stmt),
6337 low_value, high_value);
6338 if (label == error_mark_node)
6340 else if (switch_was_empty_p)
6342 /* Attach the first case label to the SWITCH_BODY. */
6343 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6344 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6348 error ("case label not within a switch statement");
6350 error ("`default' label not within a switch statement");
6355 /* Finish the switch statement. */
6358 c_finish_case (void)
6360 struct c_switch *cs = switch_stack;
6362 /* Rechain the next statements to the SWITCH_STMT. */
6363 last_tree = cs->switch_stmt;
6365 /* Pop the stack. */
6366 switch_stack = switch_stack->next;
6367 splay_tree_delete (cs->cases);
6371 /* Build a binary-operation expression without default conversions.
6372 CODE is the kind of expression to build.
6373 This function differs from `build' in several ways:
6374 the data type of the result is computed and recorded in it,
6375 warnings are generated if arg data types are invalid,
6376 special handling for addition and subtraction of pointers is known,
6377 and some optimization is done (operations on narrow ints
6378 are done in the narrower type when that gives the same result).
6379 Constant folding is also done before the result is returned.
6381 Note that the operands will never have enumeral types, or function
6382 or array types, because either they will have the default conversions
6383 performed or they have both just been converted to some other type in which
6384 the arithmetic is to be done. */
6387 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6391 enum tree_code code0, code1;
6394 /* Expression code to give to the expression when it is built.
6395 Normally this is CODE, which is what the caller asked for,
6396 but in some special cases we change it. */
6397 enum tree_code resultcode = code;
6399 /* Data type in which the computation is to be performed.
6400 In the simplest cases this is the common type of the arguments. */
6401 tree result_type = NULL;
6403 /* Nonzero means operands have already been type-converted
6404 in whatever way is necessary.
6405 Zero means they need to be converted to RESULT_TYPE. */
6408 /* Nonzero means create the expression with this type, rather than
6410 tree build_type = 0;
6412 /* Nonzero means after finally constructing the expression
6413 convert it to this type. */
6414 tree final_type = 0;
6416 /* Nonzero if this is an operation like MIN or MAX which can
6417 safely be computed in short if both args are promoted shorts.
6418 Also implies COMMON.
6419 -1 indicates a bitwise operation; this makes a difference
6420 in the exact conditions for when it is safe to do the operation
6421 in a narrower mode. */
6424 /* Nonzero if this is a comparison operation;
6425 if both args are promoted shorts, compare the original shorts.
6426 Also implies COMMON. */
6427 int short_compare = 0;
6429 /* Nonzero if this is a right-shift operation, which can be computed on the
6430 original short and then promoted if the operand is a promoted short. */
6431 int short_shift = 0;
6433 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6438 op0 = default_conversion (orig_op0);
6439 op1 = default_conversion (orig_op1);
6447 type0 = TREE_TYPE (op0);
6448 type1 = TREE_TYPE (op1);
6450 /* The expression codes of the data types of the arguments tell us
6451 whether the arguments are integers, floating, pointers, etc. */
6452 code0 = TREE_CODE (type0);
6453 code1 = TREE_CODE (type1);
6455 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6456 STRIP_TYPE_NOPS (op0);
6457 STRIP_TYPE_NOPS (op1);
6459 /* If an error was already reported for one of the arguments,
6460 avoid reporting another error. */
6462 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6463 return error_mark_node;
6468 /* Handle the pointer + int case. */
6469 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6470 return pointer_int_sum (PLUS_EXPR, op0, op1);
6471 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6472 return pointer_int_sum (PLUS_EXPR, op1, op0);
6478 /* Subtraction of two similar pointers.
6479 We must subtract them as integers, then divide by object size. */
6480 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6481 && comp_target_types (type0, type1, 1))
6482 return pointer_diff (op0, op1);
6483 /* Handle pointer minus int. Just like pointer plus int. */
6484 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6485 return pointer_int_sum (MINUS_EXPR, op0, op1);
6494 case TRUNC_DIV_EXPR:
6496 case FLOOR_DIV_EXPR:
6497 case ROUND_DIV_EXPR:
6498 case EXACT_DIV_EXPR:
6499 /* Floating point division by zero is a legitimate way to obtain
6500 infinities and NaNs. */
6501 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6502 warning ("division by zero");
6504 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6505 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6506 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6507 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6509 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6510 resultcode = RDIV_EXPR;
6512 /* Although it would be tempting to shorten always here, that
6513 loses on some targets, since the modulo instruction is
6514 undefined if the quotient can't be represented in the
6515 computation mode. We shorten only if unsigned or if
6516 dividing by something we know != -1. */
6517 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6518 || (TREE_CODE (op1) == INTEGER_CST
6519 && ! integer_all_onesp (op1)));
6527 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6529 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6533 case TRUNC_MOD_EXPR:
6534 case FLOOR_MOD_EXPR:
6535 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6536 warning ("division by zero");
6538 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6540 /* Although it would be tempting to shorten always here, that loses
6541 on some targets, since the modulo instruction is undefined if the
6542 quotient can't be represented in the computation mode. We shorten
6543 only if unsigned or if dividing by something we know != -1. */
6544 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
6545 || (TREE_CODE (op1) == INTEGER_CST
6546 && ! integer_all_onesp (op1)));
6551 case TRUTH_ANDIF_EXPR:
6552 case TRUTH_ORIF_EXPR:
6553 case TRUTH_AND_EXPR:
6555 case TRUTH_XOR_EXPR:
6556 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6557 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6558 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6559 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6561 /* Result of these operations is always an int,
6562 but that does not mean the operands should be
6563 converted to ints! */
6564 result_type = integer_type_node;
6565 op0 = lang_hooks.truthvalue_conversion (op0);
6566 op1 = lang_hooks.truthvalue_conversion (op1);
6571 /* Shift operations: result has same type as first operand;
6572 always convert second operand to int.
6573 Also set SHORT_SHIFT if shifting rightward. */
6576 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6578 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6580 if (tree_int_cst_sgn (op1) < 0)
6581 warning ("right shift count is negative");
6584 if (! integer_zerop (op1))
6587 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6588 warning ("right shift count >= width of type");
6592 /* Use the type of the value to be shifted. */
6593 result_type = type0;
6594 /* Convert the shift-count to an integer, regardless of size
6595 of value being shifted. */
6596 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6597 op1 = convert (integer_type_node, op1);
6598 /* Avoid converting op1 to result_type later. */
6604 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6606 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6608 if (tree_int_cst_sgn (op1) < 0)
6609 warning ("left shift count is negative");
6611 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6612 warning ("left shift count >= width of type");
6615 /* Use the type of the value to be shifted. */
6616 result_type = type0;
6617 /* Convert the shift-count to an integer, regardless of size
6618 of value being shifted. */
6619 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6620 op1 = convert (integer_type_node, op1);
6621 /* Avoid converting op1 to result_type later. */
6628 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6630 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6632 if (tree_int_cst_sgn (op1) < 0)
6633 warning ("shift count is negative");
6634 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6635 warning ("shift count >= width of type");
6638 /* Use the type of the value to be shifted. */
6639 result_type = type0;
6640 /* Convert the shift-count to an integer, regardless of size
6641 of value being shifted. */
6642 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6643 op1 = convert (integer_type_node, op1);
6644 /* Avoid converting op1 to result_type later. */
6651 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6652 warning ("comparing floating point with == or != is unsafe");
6653 /* Result of comparison is always int,
6654 but don't convert the args to int! */
6655 build_type = integer_type_node;
6656 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6657 || code0 == COMPLEX_TYPE)
6658 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6659 || code1 == COMPLEX_TYPE))
6661 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6663 tree tt0 = TREE_TYPE (type0);
6664 tree tt1 = TREE_TYPE (type1);
6665 /* Anything compares with void *. void * compares with anything.
6666 Otherwise, the targets must be compatible
6667 and both must be object or both incomplete. */
6668 if (comp_target_types (type0, type1, 1))
6669 result_type = common_type (type0, type1);
6670 else if (VOID_TYPE_P (tt0))
6672 /* op0 != orig_op0 detects the case of something
6673 whose value is 0 but which isn't a valid null ptr const. */
6674 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6675 && TREE_CODE (tt1) == FUNCTION_TYPE)
6676 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6678 else if (VOID_TYPE_P (tt1))
6680 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6681 && TREE_CODE (tt0) == FUNCTION_TYPE)
6682 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6685 pedwarn ("comparison of distinct pointer types lacks a cast");
6687 if (result_type == NULL_TREE)
6688 result_type = ptr_type_node;
6690 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6691 && integer_zerop (op1))
6692 result_type = type0;
6693 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6694 && integer_zerop (op0))
6695 result_type = type1;
6696 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6698 result_type = type0;
6699 pedwarn ("comparison between pointer and integer");
6701 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6703 result_type = type1;
6704 pedwarn ("comparison between pointer and integer");
6710 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6711 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6713 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6715 if (comp_target_types (type0, type1, 1))
6717 result_type = common_type (type0, type1);
6719 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6720 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6724 result_type = ptr_type_node;
6725 pedwarn ("comparison of distinct pointer types lacks a cast");
6734 build_type = integer_type_node;
6735 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6736 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6738 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6740 if (comp_target_types (type0, type1, 1))
6742 result_type = common_type (type0, type1);
6743 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6744 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6745 pedwarn ("comparison of complete and incomplete pointers");
6747 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6748 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6752 result_type = ptr_type_node;
6753 pedwarn ("comparison of distinct pointer types lacks a cast");
6756 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6757 && integer_zerop (op1))
6759 result_type = type0;
6760 if (pedantic || extra_warnings)
6761 pedwarn ("ordered comparison of pointer with integer zero");
6763 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6764 && integer_zerop (op0))
6766 result_type = type1;
6768 pedwarn ("ordered comparison of pointer with integer zero");
6770 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6772 result_type = type0;
6773 pedwarn ("comparison between pointer and integer");
6775 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6777 result_type = type1;
6778 pedwarn ("comparison between pointer and integer");
6782 case UNORDERED_EXPR:
6789 build_type = integer_type_node;
6790 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6792 error ("unordered comparison on non-floating point argument");
6793 return error_mark_node;
6802 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6803 return error_mark_node;
6805 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6806 || code0 == VECTOR_TYPE)
6808 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6809 || code1 == VECTOR_TYPE))
6811 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6813 if (shorten || common || short_compare)
6814 result_type = common_type (type0, type1);
6816 /* For certain operations (which identify themselves by shorten != 0)
6817 if both args were extended from the same smaller type,
6818 do the arithmetic in that type and then extend.
6820 shorten !=0 and !=1 indicates a bitwise operation.
6821 For them, this optimization is safe only if
6822 both args are zero-extended or both are sign-extended.
6823 Otherwise, we might change the result.
6824 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6825 but calculated in (unsigned short) it would be (unsigned short)-1. */
6827 if (shorten && none_complex)
6829 int unsigned0, unsigned1;
6830 tree arg0 = get_narrower (op0, &unsigned0);
6831 tree arg1 = get_narrower (op1, &unsigned1);
6832 /* UNS is 1 if the operation to be done is an unsigned one. */
6833 int uns = TYPE_UNSIGNED (result_type);
6836 final_type = result_type;
6838 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6839 but it *requires* conversion to FINAL_TYPE. */
6841 if ((TYPE_PRECISION (TREE_TYPE (op0))
6842 == TYPE_PRECISION (TREE_TYPE (arg0)))
6843 && TREE_TYPE (op0) != final_type)
6844 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
6845 if ((TYPE_PRECISION (TREE_TYPE (op1))
6846 == TYPE_PRECISION (TREE_TYPE (arg1)))
6847 && TREE_TYPE (op1) != final_type)
6848 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
6850 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6852 /* For bitwise operations, signedness of nominal type
6853 does not matter. Consider only how operands were extended. */
6857 /* Note that in all three cases below we refrain from optimizing
6858 an unsigned operation on sign-extended args.
6859 That would not be valid. */
6861 /* Both args variable: if both extended in same way
6862 from same width, do it in that width.
6863 Do it unsigned if args were zero-extended. */
6864 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6865 < TYPE_PRECISION (result_type))
6866 && (TYPE_PRECISION (TREE_TYPE (arg1))
6867 == TYPE_PRECISION (TREE_TYPE (arg0)))
6868 && unsigned0 == unsigned1
6869 && (unsigned0 || !uns))
6871 = c_common_signed_or_unsigned_type
6872 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6873 else if (TREE_CODE (arg0) == INTEGER_CST
6874 && (unsigned1 || !uns)
6875 && (TYPE_PRECISION (TREE_TYPE (arg1))
6876 < TYPE_PRECISION (result_type))
6878 = c_common_signed_or_unsigned_type (unsigned1,
6880 int_fits_type_p (arg0, type)))
6882 else if (TREE_CODE (arg1) == INTEGER_CST
6883 && (unsigned0 || !uns)
6884 && (TYPE_PRECISION (TREE_TYPE (arg0))
6885 < TYPE_PRECISION (result_type))
6887 = c_common_signed_or_unsigned_type (unsigned0,
6889 int_fits_type_p (arg1, type)))
6893 /* Shifts can be shortened if shifting right. */
6898 tree arg0 = get_narrower (op0, &unsigned_arg);
6900 final_type = result_type;
6902 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6903 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6905 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6906 /* We can shorten only if the shift count is less than the
6907 number of bits in the smaller type size. */
6908 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6909 /* We cannot drop an unsigned shift after sign-extension. */
6910 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6912 /* Do an unsigned shift if the operand was zero-extended. */
6914 = c_common_signed_or_unsigned_type (unsigned_arg,
6916 /* Convert value-to-be-shifted to that type. */
6917 if (TREE_TYPE (op0) != result_type)
6918 op0 = convert (result_type, op0);
6923 /* Comparison operations are shortened too but differently.
6924 They identify themselves by setting short_compare = 1. */
6928 /* Don't write &op0, etc., because that would prevent op0
6929 from being kept in a register.
6930 Instead, make copies of the our local variables and
6931 pass the copies by reference, then copy them back afterward. */
6932 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
6933 enum tree_code xresultcode = resultcode;
6935 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
6940 op0 = xop0, op1 = xop1;
6942 resultcode = xresultcode;
6944 if (warn_sign_compare && skip_evaluation == 0)
6946 int op0_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op0));
6947 int op1_signed = ! TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6948 int unsignedp0, unsignedp1;
6949 tree primop0 = get_narrower (op0, &unsignedp0);
6950 tree primop1 = get_narrower (op1, &unsignedp1);
6954 STRIP_TYPE_NOPS (xop0);
6955 STRIP_TYPE_NOPS (xop1);
6957 /* Give warnings for comparisons between signed and unsigned
6958 quantities that may fail.
6960 Do the checking based on the original operand trees, so that
6961 casts will be considered, but default promotions won't be.
6963 Do not warn if the comparison is being done in a signed type,
6964 since the signed type will only be chosen if it can represent
6965 all the values of the unsigned type. */
6966 if (! TYPE_UNSIGNED (result_type))
6968 /* Do not warn if both operands are the same signedness. */
6969 else if (op0_signed == op1_signed)
6976 sop = xop0, uop = xop1;
6978 sop = xop1, uop = xop0;
6980 /* Do not warn if the signed quantity is an
6981 unsuffixed integer literal (or some static
6982 constant expression involving such literals or a
6983 conditional expression involving such literals)
6984 and it is non-negative. */
6985 if (c_tree_expr_nonnegative_p (sop))
6987 /* Do not warn if the comparison is an equality operation,
6988 the unsigned quantity is an integral constant, and it
6989 would fit in the result if the result were signed. */
6990 else if (TREE_CODE (uop) == INTEGER_CST
6991 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
6993 (uop, c_common_signed_type (result_type)))
6995 /* Do not warn if the unsigned quantity is an enumeration
6996 constant and its maximum value would fit in the result
6997 if the result were signed. */
6998 else if (TREE_CODE (uop) == INTEGER_CST
6999 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7001 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7002 c_common_signed_type (result_type)))
7005 warning ("comparison between signed and unsigned");
7008 /* Warn if two unsigned values are being compared in a size
7009 larger than their original size, and one (and only one) is the
7010 result of a `~' operator. This comparison will always fail.
7012 Also warn if one operand is a constant, and the constant
7013 does not have all bits set that are set in the ~ operand
7014 when it is extended. */
7016 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7017 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7019 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7020 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7023 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7026 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7029 HOST_WIDE_INT constant, mask;
7030 int unsignedp, bits;
7032 if (host_integerp (primop0, 0))
7035 unsignedp = unsignedp1;
7036 constant = tree_low_cst (primop0, 0);
7041 unsignedp = unsignedp0;
7042 constant = tree_low_cst (primop1, 0);
7045 bits = TYPE_PRECISION (TREE_TYPE (primop));
7046 if (bits < TYPE_PRECISION (result_type)
7047 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7049 mask = (~ (HOST_WIDE_INT) 0) << bits;
7050 if ((mask & constant) != mask)
7051 warning ("comparison of promoted ~unsigned with constant");
7054 else if (unsignedp0 && unsignedp1
7055 && (TYPE_PRECISION (TREE_TYPE (primop0))
7056 < TYPE_PRECISION (result_type))
7057 && (TYPE_PRECISION (TREE_TYPE (primop1))
7058 < TYPE_PRECISION (result_type)))
7059 warning ("comparison of promoted ~unsigned with unsigned");
7065 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7066 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7067 Then the expression will be built.
7068 It will be given type FINAL_TYPE if that is nonzero;
7069 otherwise, it will be given type RESULT_TYPE. */
7073 binary_op_error (code);
7074 return error_mark_node;
7079 if (TREE_TYPE (op0) != result_type)
7080 op0 = convert (result_type, op0);
7081 if (TREE_TYPE (op1) != result_type)
7082 op1 = convert (result_type, op1);
7085 if (build_type == NULL_TREE)
7086 build_type = result_type;
7089 tree result = build (resultcode, build_type, op0, op1);
7092 /* Treat expressions in initializers specially as they can't trap. */
7093 folded = initializer_stack ? fold_initializer (result)
7095 if (folded == result)
7096 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7097 if (final_type != 0)
7098 return convert (final_type, folded);