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 same_translation_unit_p (tree, tree);
55 static int tagged_types_tu_compatible_p (tree, tree, int);
56 static int comp_target_types (tree, tree, int);
57 static int function_types_compatible_p (tree, tree, int);
58 static int type_lists_compatible_p (tree, tree, int);
59 static tree decl_constant_value_for_broken_optimization (tree);
60 static tree default_function_array_conversion (tree);
61 static tree lookup_field (tree, tree);
62 static tree convert_arguments (tree, tree, tree, tree);
63 static tree pointer_diff (tree, tree);
64 static tree internal_build_compound_expr (tree, int);
65 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
67 static void warn_for_assignment (const char *, const char *, tree, int);
68 static tree valid_compound_expr_initializer (tree, tree);
69 static void push_string (const char *);
70 static void push_member_name (tree);
71 static void push_array_bounds (int);
72 static int spelling_length (void);
73 static char *print_spelling (char *);
74 static void warning_init (const char *);
75 static tree digest_init (tree, tree, int);
76 static void output_init_element (tree, tree, tree, int);
77 static void output_pending_init_elements (int);
78 static int set_designator (int);
79 static void push_range_stack (tree);
80 static void add_pending_init (tree, tree);
81 static void set_nonincremental_init (void);
82 static void set_nonincremental_init_from_string (tree);
83 static tree find_init_member (tree);
85 /* Do `exp = require_complete_type (exp);' to make sure exp
86 does not have an incomplete type. (That includes void types.) */
89 require_complete_type (tree value)
91 tree type = TREE_TYPE (value);
93 if (value == error_mark_node || type == error_mark_node)
94 return error_mark_node;
96 /* First, detect a valid value with a complete type. */
97 if (COMPLETE_TYPE_P (type))
100 c_incomplete_type_error (value, type);
101 return error_mark_node;
104 /* Print an error message for invalid use of an incomplete type.
105 VALUE is the expression that was used (or 0 if that isn't known)
106 and TYPE is the type that was invalid. */
109 c_incomplete_type_error (tree value, tree type)
111 const char *type_code_string;
113 /* Avoid duplicate error message. */
114 if (TREE_CODE (type) == ERROR_MARK)
117 if (value != 0 && (TREE_CODE (value) == VAR_DECL
118 || TREE_CODE (value) == PARM_DECL))
119 error ("`%s' has an incomplete type",
120 IDENTIFIER_POINTER (DECL_NAME (value)));
124 /* We must print an error message. Be clever about what it says. */
126 switch (TREE_CODE (type))
129 type_code_string = "struct";
133 type_code_string = "union";
137 type_code_string = "enum";
141 error ("invalid use of void expression");
145 if (TYPE_DOMAIN (type))
147 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
149 error ("invalid use of flexible array member");
152 type = TREE_TYPE (type);
155 error ("invalid use of array with unspecified bounds");
162 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
163 error ("invalid use of undefined type `%s %s'",
164 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
166 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
167 error ("invalid use of incomplete typedef `%s'",
168 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
172 /* Given a type, apply default promotions wrt unnamed function
173 arguments and return the new type. */
176 c_type_promotes_to (tree type)
178 if (TYPE_MAIN_VARIANT (type) == float_type_node)
179 return double_type_node;
181 if (c_promoting_integer_type_p (type))
183 /* Preserve unsignedness if not really getting any wider. */
184 if (TREE_UNSIGNED (type)
185 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
186 return unsigned_type_node;
187 return integer_type_node;
193 /* Return a variant of TYPE which has all the type qualifiers of LIKE
194 as well as those of TYPE. */
197 qualify_type (tree type, tree like)
199 return c_build_qualified_type (type,
200 TYPE_QUALS (type) | TYPE_QUALS (like));
203 /* Return the common type of two types.
204 We assume that comptypes has already been done and returned 1;
205 if that isn't so, this may crash. In particular, we assume that qualifiers
208 This is the type for the result of most arithmetic operations
209 if the operands have the given two types. */
212 common_type (tree t1, tree t2)
214 enum tree_code code1;
215 enum tree_code code2;
218 /* Save time if the two types are the same. */
220 if (t1 == t2) return t1;
222 /* If one type is nonsense, use the other. */
223 if (t1 == error_mark_node)
225 if (t2 == error_mark_node)
228 /* Merge the attributes. */
229 attributes = (*targetm.merge_type_attributes) (t1, t2);
231 /* Treat an enum type as the unsigned integer type of the same width. */
233 if (TREE_CODE (t1) == ENUMERAL_TYPE)
234 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
235 if (TREE_CODE (t2) == ENUMERAL_TYPE)
236 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
238 code1 = TREE_CODE (t1);
239 code2 = TREE_CODE (t2);
241 /* If one type is complex, form the common type of the non-complex
242 components, then make that complex. Use T1 or T2 if it is the
244 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
246 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
247 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
248 tree subtype = common_type (subtype1, subtype2);
250 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
251 return build_type_attribute_variant (t1, attributes);
252 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
253 return build_type_attribute_variant (t2, attributes);
255 return build_type_attribute_variant (build_complex_type (subtype),
263 /* If only one is real, use it as the result. */
265 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
266 return build_type_attribute_variant (t1, attributes);
268 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
269 return build_type_attribute_variant (t2, attributes);
271 /* Both real or both integers; use the one with greater precision. */
273 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
274 return build_type_attribute_variant (t1, attributes);
275 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
276 return build_type_attribute_variant (t2, attributes);
278 /* Same precision. Prefer longs to ints even when same size. */
280 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
281 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
282 return build_type_attribute_variant (long_unsigned_type_node,
285 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
286 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
288 /* But preserve unsignedness from the other type,
289 since long cannot hold all the values of an unsigned int. */
290 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
291 t1 = long_unsigned_type_node;
293 t1 = long_integer_type_node;
294 return build_type_attribute_variant (t1, attributes);
297 /* Likewise, prefer long double to double even if same size. */
298 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
299 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
300 return build_type_attribute_variant (long_double_type_node,
303 /* Otherwise prefer the unsigned one. */
305 if (TREE_UNSIGNED (t1))
306 return build_type_attribute_variant (t1, attributes);
308 return build_type_attribute_variant (t2, attributes);
311 /* For two pointers, do this recursively on the target type,
312 and combine the qualifiers of the two types' targets. */
313 /* This code was turned off; I don't know why.
314 But ANSI C specifies doing this with the qualifiers.
315 So I turned it on again. */
317 tree pointed_to_1 = TREE_TYPE (t1);
318 tree pointed_to_2 = TREE_TYPE (t2);
319 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
320 TYPE_MAIN_VARIANT (pointed_to_2));
321 t1 = build_pointer_type (c_build_qualified_type
323 TYPE_QUALS (pointed_to_1) |
324 TYPE_QUALS (pointed_to_2)));
325 return build_type_attribute_variant (t1, attributes);
330 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
331 /* Save space: see if the result is identical to one of the args. */
332 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
333 return build_type_attribute_variant (t1, attributes);
334 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
335 return build_type_attribute_variant (t2, attributes);
336 /* Merge the element types, and have a size if either arg has one. */
337 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
338 return build_type_attribute_variant (t1, attributes);
342 /* Function types: prefer the one that specified arg types.
343 If both do, merge the arg types. Also merge the return types. */
345 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
346 tree p1 = TYPE_ARG_TYPES (t1);
347 tree p2 = TYPE_ARG_TYPES (t2);
352 /* Save space: see if the result is identical to one of the args. */
353 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
354 return build_type_attribute_variant (t1, attributes);
355 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
356 return build_type_attribute_variant (t2, attributes);
358 /* Simple way if one arg fails to specify argument types. */
359 if (TYPE_ARG_TYPES (t1) == 0)
361 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
362 return build_type_attribute_variant (t1, attributes);
364 if (TYPE_ARG_TYPES (t2) == 0)
366 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
367 return build_type_attribute_variant (t1, attributes);
370 /* If both args specify argument types, we must merge the two
371 lists, argument by argument. */
374 declare_parm_level ();
376 len = list_length (p1);
379 for (i = 0; i < len; i++)
380 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
385 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
387 /* A null type means arg type is not specified.
388 Take whatever the other function type has. */
389 if (TREE_VALUE (p1) == 0)
391 TREE_VALUE (n) = TREE_VALUE (p2);
394 if (TREE_VALUE (p2) == 0)
396 TREE_VALUE (n) = TREE_VALUE (p1);
400 /* Given wait (union {union wait *u; int *i} *)
401 and wait (union wait *),
402 prefer union wait * as type of parm. */
403 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
404 && TREE_VALUE (p1) != TREE_VALUE (p2))
407 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
408 memb; memb = TREE_CHAIN (memb))
409 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
412 TREE_VALUE (n) = TREE_VALUE (p2);
414 pedwarn ("function types not truly compatible in ISO C");
418 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
419 && TREE_VALUE (p2) != TREE_VALUE (p1))
422 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
423 memb; memb = TREE_CHAIN (memb))
424 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
427 TREE_VALUE (n) = TREE_VALUE (p1);
429 pedwarn ("function types not truly compatible in ISO C");
433 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
439 t1 = build_function_type (valtype, newargs);
440 /* ... falls through ... */
444 return build_type_attribute_variant (t1, attributes);
449 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
450 or various other operations. Return 2 if they are compatible
451 but a warning may be needed if you use them together. */
454 comptypes (tree type1, tree type2, int flags)
460 /* Suppress errors caused by previously reported errors. */
462 if (t1 == t2 || !t1 || !t2
463 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
466 /* If either type is the internal version of sizetype, return the
468 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
469 && TYPE_DOMAIN (t1) != 0)
470 t1 = TYPE_DOMAIN (t1);
472 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
473 && TYPE_DOMAIN (t2) != 0)
474 t2 = TYPE_DOMAIN (t2);
476 /* Enumerated types are compatible with integer types, but this is
477 not transitive: two enumerated types in the same translation unit
478 are compatible with each other only if they are the same type. */
480 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
481 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
482 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
483 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
488 /* Different classes of types can't be compatible. */
490 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
492 /* Qualifiers must match. */
494 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
497 /* Allow for two different type nodes which have essentially the same
498 definition. Note that we already checked for equality of the type
499 qualifiers (just above). */
501 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
504 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
505 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
508 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
511 switch (TREE_CODE (t1))
514 /* We must give ObjC the first crack at comparing pointers, since
515 protocol qualifiers may be involved. */
516 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
518 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
519 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
523 val = function_types_compatible_p (t1, t2, flags);
528 tree d1 = TYPE_DOMAIN (t1);
529 tree d2 = TYPE_DOMAIN (t2);
530 bool d1_variable, d2_variable;
531 bool d1_zero, d2_zero;
534 /* Target types must match incl. qualifiers. */
535 if (TREE_TYPE (t1) != TREE_TYPE (t2)
536 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
540 /* Sizes must match unless one is missing or variable. */
541 if (d1 == 0 || d2 == 0 || d1 == d2)
544 d1_zero = ! TYPE_MAX_VALUE (d1);
545 d2_zero = ! TYPE_MAX_VALUE (d2);
547 d1_variable = (! d1_zero
548 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
549 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
550 d2_variable = (! d2_zero
551 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
552 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
554 if (d1_variable || d2_variable)
556 if (d1_zero && d2_zero)
558 if (d1_zero || d2_zero
559 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
560 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
567 /* We are dealing with two distinct structs. In assorted Objective-C
568 corner cases, however, these can still be deemed equivalent. */
569 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
574 if (val != 1 && !same_translation_unit_p (t1, t2))
575 val = tagged_types_tu_compatible_p (t1, t2, flags);
579 /* The target might allow certain vector types to be compatible. */
580 val = (*targetm.vector_opaque_p) (t1)
581 || (*targetm.vector_opaque_p) (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 types derive from the same translation unit.
617 If the CONTEXT chain ends in a null, that type's context is still
618 being parsed, so if two types 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 (t1); 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) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
677 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
679 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
680 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
682 /* C90 didn't have the requirement that the two tags be the same. */
683 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
686 /* C90 didn't say what happened if one or both of the types were
687 incomplete; we choose to follow C99 rules here, which is that they
689 if (TYPE_SIZE (t1) == NULL
690 || TYPE_SIZE (t2) == NULL)
694 const struct tagged_tu_seen * tts_i;
695 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
696 if (tts_i->t1 == t1 && tts_i->t2 == t2)
700 switch (TREE_CODE (t1))
705 /* Speed up the case where the type values are in the same order. */
706 tree tv1 = TYPE_VALUES (t1);
707 tree tv2 = TYPE_VALUES (t2);
712 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
714 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
716 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
720 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
722 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
725 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
728 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
730 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
732 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
740 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
743 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
746 struct tagged_tu_seen tts;
748 tts.next = tagged_tu_seen_base;
751 tagged_tu_seen_base = &tts;
753 if (DECL_NAME (s1) != NULL)
754 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
755 if (DECL_NAME (s1) == DECL_NAME (s2))
758 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
762 needs_warning = true;
764 if (TREE_CODE (s1) == FIELD_DECL
765 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
766 DECL_FIELD_BIT_OFFSET (s2)) != 1)
772 tagged_tu_seen_base = tts.next;
776 return needs_warning ? 2 : 1;
781 struct tagged_tu_seen tts;
783 tts.next = tagged_tu_seen_base;
786 tagged_tu_seen_base = &tts;
788 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
790 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
793 if (TREE_CODE (s1) != TREE_CODE (s2)
794 || DECL_NAME (s1) != DECL_NAME (s2))
796 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
800 needs_warning = true;
802 if (TREE_CODE (s1) == FIELD_DECL
803 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
804 DECL_FIELD_BIT_OFFSET (s2)) != 1)
807 tagged_tu_seen_base = tts.next;
810 return needs_warning ? 2 : 1;
818 /* Return 1 if two function types F1 and F2 are compatible.
819 If either type specifies no argument types,
820 the other must specify a fixed number of self-promoting arg types.
821 Otherwise, if one type specifies only the number of arguments,
822 the other must specify that number of self-promoting arg types.
823 Otherwise, the argument types must match. */
826 function_types_compatible_p (tree f1, tree f2, int flags)
829 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
834 ret1 = TREE_TYPE (f1);
835 ret2 = TREE_TYPE (f2);
837 /* 'volatile' qualifiers on a function's return type mean the function
839 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
840 pedwarn ("function return types not compatible due to `volatile'");
841 if (TYPE_VOLATILE (ret1))
842 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
843 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
844 if (TYPE_VOLATILE (ret2))
845 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
846 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
847 val = comptypes (ret1, ret2, flags);
851 args1 = TYPE_ARG_TYPES (f1);
852 args2 = TYPE_ARG_TYPES (f2);
854 /* An unspecified parmlist matches any specified parmlist
855 whose argument types don't need default promotions. */
859 if (!self_promoting_args_p (args2))
861 /* If one of these types comes from a non-prototype fn definition,
862 compare that with the other type's arglist.
863 If they don't match, ask for a warning (but no error). */
864 if (TYPE_ACTUAL_ARG_TYPES (f1)
865 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
872 if (!self_promoting_args_p (args1))
874 if (TYPE_ACTUAL_ARG_TYPES (f2)
875 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
881 /* Both types have argument lists: compare them and propagate results. */
882 val1 = type_lists_compatible_p (args1, args2, flags);
883 return val1 != 1 ? val1 : val;
886 /* Check two lists of types for compatibility,
887 returning 0 for incompatible, 1 for compatible,
888 or 2 for compatible with warning. */
891 type_lists_compatible_p (tree args1, tree args2, int flags)
893 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
899 if (args1 == 0 && args2 == 0)
901 /* If one list is shorter than the other,
902 they fail to match. */
903 if (args1 == 0 || args2 == 0)
905 /* A null pointer instead of a type
906 means there is supposed to be an argument
907 but nothing is specified about what type it has.
908 So match anything that self-promotes. */
909 if (TREE_VALUE (args1) == 0)
911 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
914 else if (TREE_VALUE (args2) == 0)
916 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
919 /* If one of the lists has an error marker, ignore this arg. */
920 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
921 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
923 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
924 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
927 /* Allow wait (union {union wait *u; int *i} *)
928 and wait (union wait *) to be compatible. */
929 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
930 && (TYPE_NAME (TREE_VALUE (args1)) == 0
931 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
932 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
933 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
934 TYPE_SIZE (TREE_VALUE (args2))))
937 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
938 memb; memb = TREE_CHAIN (memb))
939 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
945 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
946 && (TYPE_NAME (TREE_VALUE (args2)) == 0
947 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
948 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
949 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
950 TYPE_SIZE (TREE_VALUE (args1))))
953 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
954 memb; memb = TREE_CHAIN (memb))
955 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
965 /* comptypes said ok, but record if it said to warn. */
969 args1 = TREE_CHAIN (args1);
970 args2 = TREE_CHAIN (args2);
974 /* Compute the size to increment a pointer by. */
977 c_size_in_bytes (tree type)
979 enum tree_code code = TREE_CODE (type);
981 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
982 return size_one_node;
984 if (!COMPLETE_OR_VOID_TYPE_P (type))
986 error ("arithmetic on pointer to an incomplete type");
987 return size_one_node;
990 /* Convert in case a char is more than one unit. */
991 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
992 size_int (TYPE_PRECISION (char_type_node)
996 /* Return either DECL or its known constant value (if it has one). */
999 decl_constant_value (tree decl)
1001 if (/* Don't change a variable array bound or initial value to a constant
1002 in a place where a variable is invalid. */
1003 current_function_decl != 0
1004 && ! TREE_THIS_VOLATILE (decl)
1005 && TREE_READONLY (decl)
1006 && DECL_INITIAL (decl) != 0
1007 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1008 /* This is invalid if initial value is not constant.
1009 If it has either a function call, a memory reference,
1010 or a variable, then re-evaluating it could give different results. */
1011 && TREE_CONSTANT (DECL_INITIAL (decl))
1012 /* Check for cases where this is sub-optimal, even though valid. */
1013 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1014 return DECL_INITIAL (decl);
1018 /* Return either DECL or its known constant value (if it has one), but
1019 return DECL if pedantic or DECL has mode BLKmode. This is for
1020 bug-compatibility with the old behavior of decl_constant_value
1021 (before GCC 3.0); every use of this function is a bug and it should
1022 be removed before GCC 3.1. It is not appropriate to use pedantic
1023 in a way that affects optimization, and BLKmode is probably not the
1024 right test for avoiding misoptimizations either. */
1027 decl_constant_value_for_broken_optimization (tree decl)
1029 if (pedantic || DECL_MODE (decl) == BLKmode)
1032 return decl_constant_value (decl);
1036 /* Perform the default conversion of arrays and functions to pointers.
1037 Return the result of converting EXP. For any other expression, just
1041 default_function_array_conversion (tree exp)
1044 tree type = TREE_TYPE (exp);
1045 enum tree_code code = TREE_CODE (type);
1048 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1051 Do not use STRIP_NOPS here! It will remove conversions from pointer
1052 to integer and cause infinite recursion. */
1054 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1055 || (TREE_CODE (exp) == NOP_EXPR
1056 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1058 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1060 exp = TREE_OPERAND (exp, 0);
1063 /* Preserve the original expression code. */
1064 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1065 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1067 if (code == FUNCTION_TYPE)
1069 return build_unary_op (ADDR_EXPR, exp, 0);
1071 if (code == ARRAY_TYPE)
1074 tree restype = TREE_TYPE (type);
1080 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1082 constp = TREE_READONLY (exp);
1083 volatilep = TREE_THIS_VOLATILE (exp);
1086 if (TYPE_QUALS (type) || constp || volatilep)
1088 = c_build_qualified_type (restype,
1090 | (constp * TYPE_QUAL_CONST)
1091 | (volatilep * TYPE_QUAL_VOLATILE));
1093 if (TREE_CODE (exp) == INDIRECT_REF)
1094 return convert (TYPE_POINTER_TO (restype),
1095 TREE_OPERAND (exp, 0));
1097 if (TREE_CODE (exp) == COMPOUND_EXPR)
1099 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1100 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1101 TREE_OPERAND (exp, 0), op1);
1104 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1105 if (!flag_isoc99 && !lvalue_array_p)
1107 /* Before C99, non-lvalue arrays do not decay to pointers.
1108 Normally, using such an array would be invalid; but it can
1109 be used correctly inside sizeof or as a statement expression.
1110 Thus, do not give an error here; an error will result later. */
1114 ptrtype = build_pointer_type (restype);
1116 if (TREE_CODE (exp) == VAR_DECL)
1118 /* ??? This is not really quite correct
1119 in that the type of the operand of ADDR_EXPR
1120 is not the target type of the type of the ADDR_EXPR itself.
1121 Question is, can this lossage be avoided? */
1122 adr = build1 (ADDR_EXPR, ptrtype, exp);
1123 if (!c_mark_addressable (exp))
1124 return error_mark_node;
1125 TREE_CONSTANT (adr) = staticp (exp);
1126 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1129 /* This way is better for a COMPONENT_REF since it can
1130 simplify the offset for a component. */
1131 adr = build_unary_op (ADDR_EXPR, exp, 1);
1132 return convert (ptrtype, adr);
1137 /* Perform default promotions for C data used in expressions.
1138 Arrays and functions are converted to pointers;
1139 enumeral types or short or char, to int.
1140 In addition, manifest constants symbols are replaced by their values. */
1143 default_conversion (tree exp)
1146 tree type = TREE_TYPE (exp);
1147 enum tree_code code = TREE_CODE (type);
1149 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1150 return default_function_array_conversion (exp);
1152 /* Constants can be used directly unless they're not loadable. */
1153 if (TREE_CODE (exp) == CONST_DECL)
1154 exp = DECL_INITIAL (exp);
1156 /* Replace a nonvolatile const static variable with its value unless
1157 it is an array, in which case we must be sure that taking the
1158 address of the array produces consistent results. */
1159 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1161 exp = decl_constant_value_for_broken_optimization (exp);
1162 type = TREE_TYPE (exp);
1165 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1168 Do not use STRIP_NOPS here! It will remove conversions from pointer
1169 to integer and cause infinite recursion. */
1171 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1172 || (TREE_CODE (exp) == NOP_EXPR
1173 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1174 exp = TREE_OPERAND (exp, 0);
1176 /* Preserve the original expression code. */
1177 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1178 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1180 /* Normally convert enums to int,
1181 but convert wide enums to something wider. */
1182 if (code == ENUMERAL_TYPE)
1184 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1185 TYPE_PRECISION (integer_type_node)),
1186 ((TYPE_PRECISION (type)
1187 >= TYPE_PRECISION (integer_type_node))
1188 && TREE_UNSIGNED (type)));
1190 return convert (type, exp);
1193 if (TREE_CODE (exp) == COMPONENT_REF
1194 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1195 /* If it's thinner than an int, promote it like a
1196 c_promoting_integer_type_p, otherwise leave it alone. */
1197 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1198 TYPE_PRECISION (integer_type_node)))
1199 return convert (integer_type_node, exp);
1201 if (c_promoting_integer_type_p (type))
1203 /* Preserve unsignedness if not really getting any wider. */
1204 if (TREE_UNSIGNED (type)
1205 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1206 return convert (unsigned_type_node, exp);
1208 return convert (integer_type_node, exp);
1211 if (code == VOID_TYPE)
1213 error ("void value not ignored as it ought to be");
1214 return error_mark_node;
1219 /* Look up COMPONENT in a structure or union DECL.
1221 If the component name is not found, returns NULL_TREE. Otherwise,
1222 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1223 stepping down the chain to the component, which is in the last
1224 TREE_VALUE of the list. Normally the list is of length one, but if
1225 the component is embedded within (nested) anonymous structures or
1226 unions, the list steps down the chain to the component. */
1229 lookup_field (tree decl, tree component)
1231 tree type = TREE_TYPE (decl);
1234 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1235 to the field elements. Use a binary search on this array to quickly
1236 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1237 will always be set for structures which have many elements. */
1239 if (TYPE_LANG_SPECIFIC (type))
1242 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1244 field = TYPE_FIELDS (type);
1246 top = TYPE_LANG_SPECIFIC (type)->s->len;
1247 while (top - bot > 1)
1249 half = (top - bot + 1) >> 1;
1250 field = field_array[bot+half];
1252 if (DECL_NAME (field) == NULL_TREE)
1254 /* Step through all anon unions in linear fashion. */
1255 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1257 field = field_array[bot++];
1258 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1259 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1261 tree anon = lookup_field (field, component);
1264 return tree_cons (NULL_TREE, field, anon);
1268 /* Entire record is only anon unions. */
1272 /* Restart the binary search, with new lower bound. */
1276 if (DECL_NAME (field) == component)
1278 if (DECL_NAME (field) < component)
1284 if (DECL_NAME (field_array[bot]) == component)
1285 field = field_array[bot];
1286 else if (DECL_NAME (field) != component)
1291 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1293 if (DECL_NAME (field) == NULL_TREE
1294 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1295 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1297 tree anon = lookup_field (field, component);
1300 return tree_cons (NULL_TREE, field, anon);
1303 if (DECL_NAME (field) == component)
1307 if (field == NULL_TREE)
1311 return tree_cons (NULL_TREE, field, NULL_TREE);
1314 /* Make an expression to refer to the COMPONENT field of
1315 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1318 build_component_ref (tree datum, tree component)
1320 tree type = TREE_TYPE (datum);
1321 enum tree_code code = TREE_CODE (type);
1325 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1326 Ensure that the arguments are not lvalues; otherwise,
1327 if the component is an array, it would wrongly decay to a pointer in
1329 We cannot do this with a COND_EXPR, because in a conditional expression
1330 the default promotions are applied to both sides, and this would yield
1331 the wrong type of the result; for example, if the components have
1333 switch (TREE_CODE (datum))
1337 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1338 return build (COMPOUND_EXPR, TREE_TYPE (value),
1339 TREE_OPERAND (datum, 0), non_lvalue (value));
1345 /* See if there is a field or component with name COMPONENT. */
1347 if (code == RECORD_TYPE || code == UNION_TYPE)
1349 if (!COMPLETE_TYPE_P (type))
1351 c_incomplete_type_error (NULL_TREE, type);
1352 return error_mark_node;
1355 field = lookup_field (datum, component);
1359 error ("%s has no member named `%s'",
1360 code == RECORD_TYPE ? "structure" : "union",
1361 IDENTIFIER_POINTER (component));
1362 return error_mark_node;
1365 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1366 This might be better solved in future the way the C++ front
1367 end does it - by giving the anonymous entities each a
1368 separate name and type, and then have build_component_ref
1369 recursively call itself. We can't do that here. */
1372 tree subdatum = TREE_VALUE (field);
1374 if (TREE_TYPE (subdatum) == error_mark_node)
1375 return error_mark_node;
1377 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1378 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1379 TREE_READONLY (ref) = 1;
1380 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1381 TREE_THIS_VOLATILE (ref) = 1;
1383 if (TREE_DEPRECATED (subdatum))
1384 warn_deprecated_use (subdatum);
1388 field = TREE_CHAIN (field);
1394 else if (code != ERROR_MARK)
1395 error ("request for member `%s' in something not a structure or union",
1396 IDENTIFIER_POINTER (component));
1398 return error_mark_node;
1401 /* Given an expression PTR for a pointer, return an expression
1402 for the value pointed to.
1403 ERRORSTRING is the name of the operator to appear in error messages. */
1406 build_indirect_ref (tree ptr, const char *errorstring)
1408 tree pointer = default_conversion (ptr);
1409 tree type = TREE_TYPE (pointer);
1411 if (TREE_CODE (type) == POINTER_TYPE)
1413 if (TREE_CODE (pointer) == ADDR_EXPR
1414 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1415 == TREE_TYPE (type)))
1416 return TREE_OPERAND (pointer, 0);
1419 tree t = TREE_TYPE (type);
1420 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1422 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1424 error ("dereferencing pointer to incomplete type");
1425 return error_mark_node;
1427 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1428 warning ("dereferencing `void *' pointer");
1430 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1431 so that we get the proper error message if the result is used
1432 to assign to. Also, &* is supposed to be a no-op.
1433 And ANSI C seems to specify that the type of the result
1434 should be the const type. */
1435 /* A de-reference of a pointer to const is not a const. It is valid
1436 to change it via some other pointer. */
1437 TREE_READONLY (ref) = TYPE_READONLY (t);
1438 TREE_SIDE_EFFECTS (ref)
1439 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1440 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1444 else if (TREE_CODE (pointer) != ERROR_MARK)
1445 error ("invalid type argument of `%s'", errorstring);
1446 return error_mark_node;
1449 /* This handles expressions of the form "a[i]", which denotes
1452 This is logically equivalent in C to *(a+i), but we may do it differently.
1453 If A is a variable or a member, we generate a primitive ARRAY_REF.
1454 This avoids forcing the array out of registers, and can work on
1455 arrays that are not lvalues (for example, members of structures returned
1459 build_array_ref (tree array, tree index)
1463 error ("subscript missing in array reference");
1464 return error_mark_node;
1467 if (TREE_TYPE (array) == error_mark_node
1468 || TREE_TYPE (index) == error_mark_node)
1469 return error_mark_node;
1471 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1472 && TREE_CODE (array) != INDIRECT_REF)
1476 /* Subscripting with type char is likely to lose
1477 on a machine where chars are signed.
1478 So warn on any machine, but optionally.
1479 Don't warn for unsigned char since that type is safe.
1480 Don't warn for signed char because anyone who uses that
1481 must have done so deliberately. */
1482 if (warn_char_subscripts
1483 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1484 warning ("array subscript has type `char'");
1486 /* Apply default promotions *after* noticing character types. */
1487 index = default_conversion (index);
1489 /* Require integer *after* promotion, for sake of enums. */
1490 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1492 error ("array subscript is not an integer");
1493 return error_mark_node;
1496 /* An array that is indexed by a non-constant
1497 cannot be stored in a register; we must be able to do
1498 address arithmetic on its address.
1499 Likewise an array of elements of variable size. */
1500 if (TREE_CODE (index) != INTEGER_CST
1501 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1502 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1504 if (!c_mark_addressable (array))
1505 return error_mark_node;
1507 /* An array that is indexed by a constant value which is not within
1508 the array bounds cannot be stored in a register either; because we
1509 would get a crash in store_bit_field/extract_bit_field when trying
1510 to access a non-existent part of the register. */
1511 if (TREE_CODE (index) == INTEGER_CST
1512 && TYPE_VALUES (TREE_TYPE (array))
1513 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1515 if (!c_mark_addressable (array))
1516 return error_mark_node;
1522 while (TREE_CODE (foo) == COMPONENT_REF)
1523 foo = TREE_OPERAND (foo, 0);
1524 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1525 pedwarn ("ISO C forbids subscripting `register' array");
1526 else if (! flag_isoc99 && ! lvalue_p (foo))
1527 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1530 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1531 rval = build (ARRAY_REF, type, array, index);
1532 /* Array ref is const/volatile if the array elements are
1533 or if the array is. */
1534 TREE_READONLY (rval)
1535 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1536 | TREE_READONLY (array));
1537 TREE_SIDE_EFFECTS (rval)
1538 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1539 | TREE_SIDE_EFFECTS (array));
1540 TREE_THIS_VOLATILE (rval)
1541 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1542 /* This was added by rms on 16 Nov 91.
1543 It fixes vol struct foo *a; a->elts[1]
1544 in an inline function.
1545 Hope it doesn't break something else. */
1546 | TREE_THIS_VOLATILE (array));
1547 return require_complete_type (fold (rval));
1551 tree ar = default_conversion (array);
1552 tree ind = default_conversion (index);
1554 /* Do the same warning check as above, but only on the part that's
1555 syntactically the index and only if it is also semantically
1557 if (warn_char_subscripts
1558 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1559 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1560 warning ("subscript has type `char'");
1562 /* Put the integer in IND to simplify error checking. */
1563 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1570 if (ar == error_mark_node)
1573 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1574 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1576 error ("subscripted value is neither array nor pointer");
1577 return error_mark_node;
1579 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1581 error ("array subscript is not an integer");
1582 return error_mark_node;
1585 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1590 /* Build an external reference to identifier ID. FUN indicates
1591 whether this will be used for a function call. */
1593 build_external_ref (tree id, int fun)
1596 tree decl = lookup_name (id);
1597 tree objc_ivar = lookup_objc_ivar (id);
1599 if (decl && decl != error_mark_node)
1601 /* Properly declared variable or function reference. */
1604 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1606 warning ("local declaration of `%s' hides instance variable",
1607 IDENTIFIER_POINTER (id));
1616 /* Implicit function declaration. */
1617 ref = implicitly_declare (id);
1618 else if (decl == error_mark_node)
1619 /* Don't complain about something that's already been
1620 complained about. */
1621 return error_mark_node;
1624 undeclared_variable (id);
1625 return error_mark_node;
1628 if (TREE_TYPE (ref) == error_mark_node)
1629 return error_mark_node;
1631 if (TREE_DEPRECATED (ref))
1632 warn_deprecated_use (ref);
1634 if (!skip_evaluation)
1635 assemble_external (ref);
1636 TREE_USED (ref) = 1;
1638 if (TREE_CODE (ref) == CONST_DECL)
1640 ref = DECL_INITIAL (ref);
1641 TREE_CONSTANT (ref) = 1;
1643 else if (current_function_decl != 0
1644 && !DECL_FILE_SCOPE_P (current_function_decl)
1645 && (TREE_CODE (ref) == VAR_DECL
1646 || TREE_CODE (ref) == PARM_DECL
1647 || TREE_CODE (ref) == FUNCTION_DECL))
1649 tree context = decl_function_context (ref);
1651 if (context != 0 && context != current_function_decl)
1652 DECL_NONLOCAL (ref) = 1;
1658 /* Build a function call to function FUNCTION with parameters PARAMS.
1659 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1660 TREE_VALUE of each node is a parameter-expression.
1661 FUNCTION's data type may be a function type or a pointer-to-function. */
1664 build_function_call (tree function, tree params)
1666 tree fntype, fundecl = 0;
1667 tree coerced_params;
1668 tree name = NULL_TREE, result;
1671 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1672 STRIP_TYPE_NOPS (function);
1674 /* Convert anything with function type to a pointer-to-function. */
1675 if (TREE_CODE (function) == FUNCTION_DECL)
1677 name = DECL_NAME (function);
1679 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1680 (because calling an inline function does not mean the function
1681 needs to be separately compiled). */
1682 fntype = build_type_variant (TREE_TYPE (function),
1683 TREE_READONLY (function),
1684 TREE_THIS_VOLATILE (function));
1686 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1689 function = default_conversion (function);
1691 fntype = TREE_TYPE (function);
1693 if (TREE_CODE (fntype) == ERROR_MARK)
1694 return error_mark_node;
1696 if (!(TREE_CODE (fntype) == POINTER_TYPE
1697 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1699 error ("called object is not a function");
1700 return error_mark_node;
1703 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1704 current_function_returns_abnormally = 1;
1706 /* fntype now gets the type of function pointed to. */
1707 fntype = TREE_TYPE (fntype);
1709 /* Check that the function is called through a compatible prototype.
1710 If it is not, replace the call by a trap, wrapped up in a compound
1711 expression if necessary. This has the nice side-effect to prevent
1712 the tree-inliner from generating invalid assignment trees which may
1713 blow up in the RTL expander later.
1715 ??? This doesn't work for Objective-C because objc_comptypes
1716 refuses to compare function prototypes, yet the compiler appears
1717 to build calls that are flagged as invalid by C's comptypes. */
1718 if (! c_dialect_objc ()
1719 && TREE_CODE (function) == NOP_EXPR
1720 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1721 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1722 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1724 tree return_type = TREE_TYPE (fntype);
1725 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1728 /* This situation leads to run-time undefined behavior. We can't,
1729 therefore, simply error unless we can prove that all possible
1730 executions of the program must execute the code. */
1731 warning ("function called through a non-compatible type");
1733 /* We can, however, treat "undefined" any way we please.
1734 Call abort to encourage the user to fix the program. */
1735 inform ("if this code is reached, the program will abort");
1737 if (VOID_TYPE_P (return_type))
1743 if (AGGREGATE_TYPE_P (return_type))
1744 rhs = build_compound_literal (return_type,
1745 build_constructor (return_type,
1748 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1750 return build (COMPOUND_EXPR, return_type, trap, rhs);
1754 /* Convert the parameters to the types declared in the
1755 function prototype, or apply default promotions. */
1758 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1760 /* Check that the arguments to the function are valid. */
1762 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1764 /* Recognize certain built-in functions so we can make tree-codes
1765 other than CALL_EXPR. We do this when it enables fold-const.c
1766 to do something useful. */
1768 if (TREE_CODE (function) == ADDR_EXPR
1769 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1770 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1772 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1773 params, coerced_params);
1778 result = build (CALL_EXPR, TREE_TYPE (fntype),
1779 function, coerced_params, NULL_TREE);
1780 TREE_SIDE_EFFECTS (result) = 1;
1781 result = fold (result);
1783 if (VOID_TYPE_P (TREE_TYPE (result)))
1785 return require_complete_type (result);
1788 /* Convert the argument expressions in the list VALUES
1789 to the types in the list TYPELIST. The result is a list of converted
1790 argument expressions.
1792 If TYPELIST is exhausted, or when an element has NULL as its type,
1793 perform the default conversions.
1795 PARMLIST is the chain of parm decls for the function being called.
1796 It may be 0, if that info is not available.
1797 It is used only for generating error messages.
1799 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1801 This is also where warnings about wrong number of args are generated.
1803 Both VALUES and the returned value are chains of TREE_LIST nodes
1804 with the elements of the list in the TREE_VALUE slots of those nodes. */
1807 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1809 tree typetail, valtail;
1813 /* Scan the given expressions and types, producing individual
1814 converted arguments and pushing them on RESULT in reverse order. */
1816 for (valtail = values, typetail = typelist, parmnum = 0;
1818 valtail = TREE_CHAIN (valtail), parmnum++)
1820 tree type = typetail ? TREE_VALUE (typetail) : 0;
1821 tree val = TREE_VALUE (valtail);
1823 if (type == void_type_node)
1826 error ("too many arguments to function `%s'",
1827 IDENTIFIER_POINTER (name));
1829 error ("too many arguments to function");
1833 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1834 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1835 to convert automatically to a pointer. */
1836 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1837 val = TREE_OPERAND (val, 0);
1839 val = default_function_array_conversion (val);
1841 val = require_complete_type (val);
1845 /* Formal parm type is specified by a function prototype. */
1848 if (!COMPLETE_TYPE_P (type))
1850 error ("type of formal parameter %d is incomplete", parmnum + 1);
1855 /* Optionally warn about conversions that
1856 differ from the default conversions. */
1857 if (warn_conversion || warn_traditional)
1859 int formal_prec = TYPE_PRECISION (type);
1861 if (INTEGRAL_TYPE_P (type)
1862 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1863 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1864 if (INTEGRAL_TYPE_P (type)
1865 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1866 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1867 else if (TREE_CODE (type) == COMPLEX_TYPE
1868 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1869 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1870 else if (TREE_CODE (type) == REAL_TYPE
1871 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1872 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1873 else if (TREE_CODE (type) == COMPLEX_TYPE
1874 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1875 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1876 else if (TREE_CODE (type) == REAL_TYPE
1877 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1878 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1879 /* ??? At some point, messages should be written about
1880 conversions between complex types, but that's too messy
1882 else if (TREE_CODE (type) == REAL_TYPE
1883 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1885 /* Warn if any argument is passed as `float',
1886 since without a prototype it would be `double'. */
1887 if (formal_prec == TYPE_PRECISION (float_type_node))
1888 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1890 /* Detect integer changing in width or signedness.
1891 These warnings are only activated with
1892 -Wconversion, not with -Wtraditional. */
1893 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1894 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1896 tree would_have_been = default_conversion (val);
1897 tree type1 = TREE_TYPE (would_have_been);
1899 if (TREE_CODE (type) == ENUMERAL_TYPE
1900 && (TYPE_MAIN_VARIANT (type)
1901 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1902 /* No warning if function asks for enum
1903 and the actual arg is that enum type. */
1905 else if (formal_prec != TYPE_PRECISION (type1))
1906 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1907 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1909 /* Don't complain if the formal parameter type
1910 is an enum, because we can't tell now whether
1911 the value was an enum--even the same enum. */
1912 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1914 else if (TREE_CODE (val) == INTEGER_CST
1915 && int_fits_type_p (val, type))
1916 /* Change in signedness doesn't matter
1917 if a constant value is unaffected. */
1919 /* Likewise for a constant in a NOP_EXPR. */
1920 else if (TREE_CODE (val) == NOP_EXPR
1921 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1922 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1924 /* If the value is extended from a narrower
1925 unsigned type, it doesn't matter whether we
1926 pass it as signed or unsigned; the value
1927 certainly is the same either way. */
1928 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1929 && TREE_UNSIGNED (TREE_TYPE (val)))
1931 else if (TREE_UNSIGNED (type))
1932 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1934 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1938 parmval = convert_for_assignment (type, val,
1939 (char *) 0, /* arg passing */
1940 fundecl, name, parmnum + 1);
1942 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1943 && INTEGRAL_TYPE_P (type)
1944 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1945 parmval = default_conversion (parmval);
1947 result = tree_cons (NULL_TREE, parmval, result);
1949 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1950 && (TYPE_PRECISION (TREE_TYPE (val))
1951 < TYPE_PRECISION (double_type_node)))
1952 /* Convert `float' to `double'. */
1953 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1955 /* Convert `short' and `char' to full-size `int'. */
1956 result = tree_cons (NULL_TREE, default_conversion (val), result);
1959 typetail = TREE_CHAIN (typetail);
1962 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1965 error ("too few arguments to function `%s'",
1966 IDENTIFIER_POINTER (name));
1968 error ("too few arguments to function");
1971 return nreverse (result);
1974 /* This is the entry point used by the parser
1975 for binary operators in the input.
1976 In addition to constructing the expression,
1977 we check for operands that were written with other binary operators
1978 in a way that is likely to confuse the user. */
1981 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1983 tree result = build_binary_op (code, arg1, arg2, 1);
1986 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1987 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1988 enum tree_code code1 = ERROR_MARK;
1989 enum tree_code code2 = ERROR_MARK;
1991 if (TREE_CODE (result) == ERROR_MARK)
1992 return error_mark_node;
1994 if (IS_EXPR_CODE_CLASS (class1))
1995 code1 = C_EXP_ORIGINAL_CODE (arg1);
1996 if (IS_EXPR_CODE_CLASS (class2))
1997 code2 = C_EXP_ORIGINAL_CODE (arg2);
1999 /* Check for cases such as x+y<<z which users are likely
2000 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2001 is cleared to prevent these warnings. */
2002 if (warn_parentheses)
2004 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2006 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2007 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2008 warning ("suggest parentheses around + or - inside shift");
2011 if (code == TRUTH_ORIF_EXPR)
2013 if (code1 == TRUTH_ANDIF_EXPR
2014 || code2 == TRUTH_ANDIF_EXPR)
2015 warning ("suggest parentheses around && within ||");
2018 if (code == BIT_IOR_EXPR)
2020 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2021 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2022 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2023 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2024 warning ("suggest parentheses around arithmetic in operand of |");
2025 /* Check cases like x|y==z */
2026 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2027 warning ("suggest parentheses around comparison in operand of |");
2030 if (code == BIT_XOR_EXPR)
2032 if (code1 == BIT_AND_EXPR
2033 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2034 || code2 == BIT_AND_EXPR
2035 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2036 warning ("suggest parentheses around arithmetic in operand of ^");
2037 /* Check cases like x^y==z */
2038 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2039 warning ("suggest parentheses around comparison in operand of ^");
2042 if (code == BIT_AND_EXPR)
2044 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2045 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2046 warning ("suggest parentheses around + or - in operand of &");
2047 /* Check cases like x&y==z */
2048 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2049 warning ("suggest parentheses around comparison in operand of &");
2053 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2054 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2055 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2056 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2058 unsigned_conversion_warning (result, arg1);
2059 unsigned_conversion_warning (result, arg2);
2060 overflow_warning (result);
2062 class = TREE_CODE_CLASS (TREE_CODE (result));
2064 /* Record the code that was specified in the source,
2065 for the sake of warnings about confusing nesting. */
2066 if (IS_EXPR_CODE_CLASS (class))
2067 C_SET_EXP_ORIGINAL_CODE (result, code);
2070 int flag = TREE_CONSTANT (result);
2071 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2072 so that convert_for_assignment wouldn't strip it.
2073 That way, we got warnings for things like p = (1 - 1).
2074 But it turns out we should not get those warnings. */
2075 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2076 C_SET_EXP_ORIGINAL_CODE (result, code);
2077 TREE_CONSTANT (result) = flag;
2084 /* Return true if `t' is known to be non-negative. */
2087 c_tree_expr_nonnegative_p (tree t)
2089 if (TREE_CODE (t) == STMT_EXPR)
2091 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2093 /* Find the last statement in the chain, ignoring the final
2094 * scope statement */
2095 while (TREE_CHAIN (t) != NULL_TREE
2096 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2098 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2100 return tree_expr_nonnegative_p (t);
2103 /* Return a tree for the difference of pointers OP0 and OP1.
2104 The resulting tree has type int. */
2107 pointer_diff (tree op0, tree op1)
2109 tree result, folded;
2110 tree restype = ptrdiff_type_node;
2112 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2113 tree con0, con1, lit0, lit1;
2114 tree orig_op1 = op1;
2116 if (pedantic || warn_pointer_arith)
2118 if (TREE_CODE (target_type) == VOID_TYPE)
2119 pedwarn ("pointer of type `void *' used in subtraction");
2120 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2121 pedwarn ("pointer to a function used in subtraction");
2124 /* If the conversion to ptrdiff_type does anything like widening or
2125 converting a partial to an integral mode, we get a convert_expression
2126 that is in the way to do any simplifications.
2127 (fold-const.c doesn't know that the extra bits won't be needed.
2128 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2129 different mode in place.)
2130 So first try to find a common term here 'by hand'; we want to cover
2131 at least the cases that occur in legal static initializers. */
2132 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2133 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2135 if (TREE_CODE (con0) == PLUS_EXPR)
2137 lit0 = TREE_OPERAND (con0, 1);
2138 con0 = TREE_OPERAND (con0, 0);
2141 lit0 = integer_zero_node;
2143 if (TREE_CODE (con1) == PLUS_EXPR)
2145 lit1 = TREE_OPERAND (con1, 1);
2146 con1 = TREE_OPERAND (con1, 0);
2149 lit1 = integer_zero_node;
2151 if (operand_equal_p (con0, con1, 0))
2158 /* First do the subtraction as integers;
2159 then drop through to build the divide operator.
2160 Do not do default conversions on the minus operator
2161 in case restype is a short type. */
2163 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2164 convert (restype, op1), 0);
2165 /* This generates an error if op1 is pointer to incomplete type. */
2166 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2167 error ("arithmetic on pointer to an incomplete type");
2169 /* This generates an error if op0 is pointer to incomplete type. */
2170 op1 = c_size_in_bytes (target_type);
2172 /* Divide by the size, in easiest possible way. */
2174 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2176 folded = fold (result);
2177 if (folded == result)
2178 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2182 /* Construct and perhaps optimize a tree representation
2183 for a unary operation. CODE, a tree_code, specifies the operation
2184 and XARG is the operand.
2185 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2186 the default promotions (such as from short to int).
2187 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2188 allows non-lvalues; this is only used to handle conversion of non-lvalue
2189 arrays to pointers in C99. */
2192 build_unary_op (enum tree_code code, tree xarg, int flag)
2194 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2197 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2199 int noconvert = flag;
2201 if (typecode == ERROR_MARK)
2202 return error_mark_node;
2203 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2204 typecode = INTEGER_TYPE;
2209 /* This is used for unary plus, because a CONVERT_EXPR
2210 is enough to prevent anybody from looking inside for
2211 associativity, but won't generate any code. */
2212 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2213 || typecode == COMPLEX_TYPE))
2215 error ("wrong type argument to unary plus");
2216 return error_mark_node;
2218 else if (!noconvert)
2219 arg = default_conversion (arg);
2220 arg = non_lvalue (arg);
2224 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2225 || typecode == COMPLEX_TYPE
2226 || typecode == VECTOR_TYPE))
2228 error ("wrong type argument to unary minus");
2229 return error_mark_node;
2231 else if (!noconvert)
2232 arg = default_conversion (arg);
2236 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2239 arg = default_conversion (arg);
2241 else if (typecode == COMPLEX_TYPE)
2245 pedwarn ("ISO C does not support `~' for complex conjugation");
2247 arg = default_conversion (arg);
2251 error ("wrong type argument to bit-complement");
2252 return error_mark_node;
2257 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2259 error ("wrong type argument to abs");
2260 return error_mark_node;
2262 else if (!noconvert)
2263 arg = default_conversion (arg);
2267 /* Conjugating a real value is a no-op, but allow it anyway. */
2268 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2269 || typecode == COMPLEX_TYPE))
2271 error ("wrong type argument to conjugation");
2272 return error_mark_node;
2274 else if (!noconvert)
2275 arg = default_conversion (arg);
2278 case TRUTH_NOT_EXPR:
2279 if (typecode != INTEGER_TYPE
2280 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2281 && typecode != COMPLEX_TYPE
2282 /* These will convert to a pointer. */
2283 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2285 error ("wrong type argument to unary exclamation mark");
2286 return error_mark_node;
2288 arg = (*lang_hooks.truthvalue_conversion) (arg);
2289 return invert_truthvalue (arg);
2295 if (TREE_CODE (arg) == COMPLEX_CST)
2296 return TREE_REALPART (arg);
2297 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2298 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2303 if (TREE_CODE (arg) == COMPLEX_CST)
2304 return TREE_IMAGPART (arg);
2305 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2306 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2308 return convert (TREE_TYPE (arg), integer_zero_node);
2310 case PREINCREMENT_EXPR:
2311 case POSTINCREMENT_EXPR:
2312 case PREDECREMENT_EXPR:
2313 case POSTDECREMENT_EXPR:
2315 /* Increment or decrement the real part of the value,
2316 and don't change the imaginary part. */
2317 if (typecode == COMPLEX_TYPE)
2322 pedwarn ("ISO C does not support `++' and `--' on complex types");
2324 arg = stabilize_reference (arg);
2325 real = build_unary_op (REALPART_EXPR, arg, 1);
2326 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2327 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2328 build_unary_op (code, real, 1), imag);
2331 /* Report invalid types. */
2333 if (typecode != POINTER_TYPE
2334 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2336 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2337 error ("wrong type argument to increment");
2339 error ("wrong type argument to decrement");
2341 return error_mark_node;
2346 tree result_type = TREE_TYPE (arg);
2348 arg = get_unwidened (arg, 0);
2349 argtype = TREE_TYPE (arg);
2351 /* Compute the increment. */
2353 if (typecode == POINTER_TYPE)
2355 /* If pointer target is an undefined struct,
2356 we just cannot know how to do the arithmetic. */
2357 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2359 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2360 error ("increment of pointer to unknown structure");
2362 error ("decrement of pointer to unknown structure");
2364 else if ((pedantic || warn_pointer_arith)
2365 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2366 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2368 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2369 pedwarn ("wrong type argument to increment");
2371 pedwarn ("wrong type argument to decrement");
2374 inc = c_size_in_bytes (TREE_TYPE (result_type));
2377 inc = integer_one_node;
2379 inc = convert (argtype, inc);
2381 /* Complain about anything else that is not a true lvalue. */
2382 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2383 || code == POSTINCREMENT_EXPR)
2384 ? "invalid lvalue in increment"
2385 : "invalid lvalue in decrement")))
2386 return error_mark_node;
2388 /* Report a read-only lvalue. */
2389 if (TREE_READONLY (arg))
2390 readonly_error (arg,
2391 ((code == PREINCREMENT_EXPR
2392 || code == POSTINCREMENT_EXPR)
2393 ? "increment" : "decrement"));
2395 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2396 val = boolean_increment (code, arg);
2398 val = build (code, TREE_TYPE (arg), arg, inc);
2399 TREE_SIDE_EFFECTS (val) = 1;
2400 val = convert (result_type, val);
2401 if (TREE_CODE (val) != code)
2402 TREE_NO_UNUSED_WARNING (val) = 1;
2407 /* Note that this operation never does default_conversion. */
2409 /* Let &* cancel out to simplify resulting code. */
2410 if (TREE_CODE (arg) == INDIRECT_REF)
2412 /* Don't let this be an lvalue. */
2413 if (lvalue_p (TREE_OPERAND (arg, 0)))
2414 return non_lvalue (TREE_OPERAND (arg, 0));
2415 return TREE_OPERAND (arg, 0);
2418 /* For &x[y], return x+y */
2419 if (TREE_CODE (arg) == ARRAY_REF)
2421 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2422 return error_mark_node;
2423 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2424 TREE_OPERAND (arg, 1), 1);
2427 /* Anything not already handled and not a true memory reference
2428 or a non-lvalue array is an error. */
2429 else if (typecode != FUNCTION_TYPE && !flag
2430 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2431 return error_mark_node;
2433 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2434 argtype = TREE_TYPE (arg);
2436 /* If the lvalue is const or volatile, merge that into the type
2437 to which the address will point. Note that you can't get a
2438 restricted pointer by taking the address of something, so we
2439 only have to deal with `const' and `volatile' here. */
2440 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2441 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2442 argtype = c_build_type_variant (argtype,
2443 TREE_READONLY (arg),
2444 TREE_THIS_VOLATILE (arg));
2446 argtype = build_pointer_type (argtype);
2448 if (!c_mark_addressable (arg))
2449 return error_mark_node;
2454 if (TREE_CODE (arg) == COMPONENT_REF)
2456 tree field = TREE_OPERAND (arg, 1);
2458 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2460 if (DECL_C_BIT_FIELD (field))
2462 error ("attempt to take address of bit-field structure member `%s'",
2463 IDENTIFIER_POINTER (DECL_NAME (field)));
2464 return error_mark_node;
2467 addr = fold (build (PLUS_EXPR, argtype,
2468 convert (argtype, addr),
2469 convert (argtype, byte_position (field))));
2472 addr = build1 (code, argtype, arg);
2474 /* Address of a static or external variable or
2475 file-scope function counts as a constant. */
2477 && ! (TREE_CODE (arg) == FUNCTION_DECL
2478 && !DECL_FILE_SCOPE_P (arg)))
2479 TREE_CONSTANT (addr) = 1;
2488 argtype = TREE_TYPE (arg);
2489 return fold (build1 (code, argtype, arg));
2492 /* Return nonzero if REF is an lvalue valid for this language.
2493 Lvalues can be assigned, unless their type has TYPE_READONLY.
2494 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2499 enum tree_code code = TREE_CODE (ref);
2506 return lvalue_p (TREE_OPERAND (ref, 0));
2508 case COMPOUND_LITERAL_EXPR:
2518 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2519 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2523 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2530 /* Return nonzero if REF is an lvalue valid for this language;
2531 otherwise, print an error message and return zero. */
2534 lvalue_or_else (tree ref, const char *msgid)
2536 int win = lvalue_p (ref);
2539 error ("%s", msgid);
2545 /* Warn about storing in something that is `const'. */
2548 readonly_error (tree arg, const char *msgid)
2550 if (TREE_CODE (arg) == COMPONENT_REF)
2552 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2553 readonly_error (TREE_OPERAND (arg, 0), msgid);
2555 error ("%s of read-only member `%s'", _(msgid),
2556 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2558 else if (TREE_CODE (arg) == VAR_DECL)
2559 error ("%s of read-only variable `%s'", _(msgid),
2560 IDENTIFIER_POINTER (DECL_NAME (arg)));
2562 error ("%s of read-only location", _(msgid));
2565 /* Mark EXP saying that we need to be able to take the
2566 address of it; it should not be allocated in a register.
2567 Returns true if successful. */
2570 c_mark_addressable (tree exp)
2575 switch (TREE_CODE (x))
2578 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2580 error ("cannot take address of bit-field `%s'",
2581 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2585 /* ... fall through ... */
2591 x = TREE_OPERAND (x, 0);
2594 case COMPOUND_LITERAL_EXPR:
2596 TREE_ADDRESSABLE (x) = 1;
2603 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2604 && DECL_NONLOCAL (x))
2606 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2608 error ("global register variable `%s' used in nested function",
2609 IDENTIFIER_POINTER (DECL_NAME (x)));
2612 pedwarn ("register variable `%s' used in nested function",
2613 IDENTIFIER_POINTER (DECL_NAME (x)));
2615 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2617 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2619 error ("address of global register variable `%s' requested",
2620 IDENTIFIER_POINTER (DECL_NAME (x)));
2624 /* If we are making this addressable due to its having
2625 volatile components, give a different error message. Also
2626 handle the case of an unnamed parameter by not trying
2627 to give the name. */
2629 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2631 error ("cannot put object with volatile field into register");
2635 pedwarn ("address of register variable `%s' requested",
2636 IDENTIFIER_POINTER (DECL_NAME (x)));
2638 put_var_into_stack (x, /*rescan=*/true);
2642 TREE_ADDRESSABLE (x) = 1;
2649 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2652 build_conditional_expr (tree ifexp, tree op1, tree op2)
2656 enum tree_code code1;
2657 enum tree_code code2;
2658 tree result_type = NULL;
2659 tree orig_op1 = op1, orig_op2 = op2;
2661 ifexp = (*lang_hooks.truthvalue_conversion) (default_conversion (ifexp));
2663 /* Promote both alternatives. */
2665 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2666 op1 = default_conversion (op1);
2667 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2668 op2 = default_conversion (op2);
2670 if (TREE_CODE (ifexp) == ERROR_MARK
2671 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2672 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2673 return error_mark_node;
2675 type1 = TREE_TYPE (op1);
2676 code1 = TREE_CODE (type1);
2677 type2 = TREE_TYPE (op2);
2678 code2 = TREE_CODE (type2);
2680 /* C90 does not permit non-lvalue arrays in conditional expressions.
2681 In C99 they will be pointers by now. */
2682 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2684 error ("non-lvalue array in conditional expression");
2685 return error_mark_node;
2688 /* Quickly detect the usual case where op1 and op2 have the same type
2690 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2693 result_type = type1;
2695 result_type = TYPE_MAIN_VARIANT (type1);
2697 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2698 || code1 == COMPLEX_TYPE)
2699 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2700 || code2 == COMPLEX_TYPE))
2702 result_type = common_type (type1, type2);
2704 /* If -Wsign-compare, warn here if type1 and type2 have
2705 different signedness. We'll promote the signed to unsigned
2706 and later code won't know it used to be different.
2707 Do this check on the original types, so that explicit casts
2708 will be considered, but default promotions won't. */
2709 if (warn_sign_compare && !skip_evaluation)
2711 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2712 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2714 if (unsigned_op1 ^ unsigned_op2)
2716 /* Do not warn if the result type is signed, since the
2717 signed type will only be chosen if it can represent
2718 all the values of the unsigned type. */
2719 if (! TREE_UNSIGNED (result_type))
2721 /* Do not warn if the signed quantity is an unsuffixed
2722 integer literal (or some static constant expression
2723 involving such literals) and it is non-negative. */
2724 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2725 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2728 warning ("signed and unsigned type in conditional expression");
2732 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2734 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2735 pedwarn ("ISO C forbids conditional expr with only one void side");
2736 result_type = void_type_node;
2738 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2740 if (comp_target_types (type1, type2, 1))
2741 result_type = common_type (type1, type2);
2742 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2743 && TREE_CODE (orig_op1) != NOP_EXPR)
2744 result_type = qualify_type (type2, type1);
2745 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2746 && TREE_CODE (orig_op2) != NOP_EXPR)
2747 result_type = qualify_type (type1, type2);
2748 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2750 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2751 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2752 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2753 TREE_TYPE (type2)));
2755 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2757 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2758 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2759 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2760 TREE_TYPE (type1)));
2764 pedwarn ("pointer type mismatch in conditional expression");
2765 result_type = build_pointer_type (void_type_node);
2768 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2770 if (! integer_zerop (op2))
2771 pedwarn ("pointer/integer type mismatch in conditional expression");
2774 op2 = null_pointer_node;
2776 result_type = type1;
2778 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2780 if (!integer_zerop (op1))
2781 pedwarn ("pointer/integer type mismatch in conditional expression");
2784 op1 = null_pointer_node;
2786 result_type = type2;
2791 if (flag_cond_mismatch)
2792 result_type = void_type_node;
2795 error ("type mismatch in conditional expression");
2796 return error_mark_node;
2800 /* Merge const and volatile flags of the incoming types. */
2802 = build_type_variant (result_type,
2803 TREE_READONLY (op1) || TREE_READONLY (op2),
2804 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2806 if (result_type != TREE_TYPE (op1))
2807 op1 = convert_and_check (result_type, op1);
2808 if (result_type != TREE_TYPE (op2))
2809 op2 = convert_and_check (result_type, op2);
2811 if (TREE_CODE (ifexp) == INTEGER_CST)
2812 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2814 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2817 /* Given a list of expressions, return a compound expression
2818 that performs them all and returns the value of the last of them. */
2821 build_compound_expr (tree list)
2823 return internal_build_compound_expr (list, TRUE);
2827 internal_build_compound_expr (tree list, int first_p)
2831 if (TREE_CHAIN (list) == 0)
2833 /* Convert arrays and functions to pointers when there
2834 really is a comma operator. */
2837 = default_function_array_conversion (TREE_VALUE (list));
2839 /* Don't let (0, 0) be null pointer constant. */
2840 if (!first_p && integer_zerop (TREE_VALUE (list)))
2841 return non_lvalue (TREE_VALUE (list));
2842 return TREE_VALUE (list);
2845 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2847 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2849 /* The left-hand operand of a comma expression is like an expression
2850 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2851 any side-effects, unless it was explicitly cast to (void). */
2852 if (warn_unused_value
2853 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2854 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2855 warning ("left-hand operand of comma expression has no effect");
2858 /* With -Wunused, we should also warn if the left-hand operand does have
2859 side-effects, but computes a value which is not used. For example, in
2860 `foo() + bar(), baz()' the result of the `+' operator is not used,
2861 so we should issue a warning. */
2862 else if (warn_unused_value)
2863 warn_if_unused_value (TREE_VALUE (list));
2865 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2868 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2871 build_c_cast (tree type, tree expr)
2875 if (type == error_mark_node || expr == error_mark_node)
2876 return error_mark_node;
2878 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2879 only in <protocol> qualifications. But when constructing cast expressions,
2880 the protocols do matter and must be kept around. */
2881 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2882 type = TYPE_MAIN_VARIANT (type);
2884 if (TREE_CODE (type) == ARRAY_TYPE)
2886 error ("cast specifies array type");
2887 return error_mark_node;
2890 if (TREE_CODE (type) == FUNCTION_TYPE)
2892 error ("cast specifies function type");
2893 return error_mark_node;
2896 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2900 if (TREE_CODE (type) == RECORD_TYPE
2901 || TREE_CODE (type) == UNION_TYPE)
2902 pedwarn ("ISO C forbids casting nonscalar to the same type");
2905 else if (TREE_CODE (type) == UNION_TYPE)
2908 value = default_function_array_conversion (value);
2910 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2911 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2912 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2920 pedwarn ("ISO C forbids casts to union type");
2921 t = digest_init (type,
2922 build_constructor (type,
2923 build_tree_list (field, value)),
2925 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2928 error ("cast to union type from type not present in union");
2929 return error_mark_node;
2935 /* If casting to void, avoid the error that would come
2936 from default_conversion in the case of a non-lvalue array. */
2937 if (type == void_type_node)
2938 return build1 (CONVERT_EXPR, type, value);
2940 /* Convert functions and arrays to pointers,
2941 but don't convert any other types. */
2942 value = default_function_array_conversion (value);
2943 otype = TREE_TYPE (value);
2945 /* Optionally warn about potentially worrisome casts. */
2948 && TREE_CODE (type) == POINTER_TYPE
2949 && TREE_CODE (otype) == POINTER_TYPE)
2951 tree in_type = type;
2952 tree in_otype = otype;
2956 /* Check that the qualifiers on IN_TYPE are a superset of
2957 the qualifiers of IN_OTYPE. The outermost level of
2958 POINTER_TYPE nodes is uninteresting and we stop as soon
2959 as we hit a non-POINTER_TYPE node on either type. */
2962 in_otype = TREE_TYPE (in_otype);
2963 in_type = TREE_TYPE (in_type);
2965 /* GNU C allows cv-qualified function types. 'const'
2966 means the function is very pure, 'volatile' means it
2967 can't return. We need to warn when such qualifiers
2968 are added, not when they're taken away. */
2969 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2970 && TREE_CODE (in_type) == FUNCTION_TYPE)
2971 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2973 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2975 while (TREE_CODE (in_type) == POINTER_TYPE
2976 && TREE_CODE (in_otype) == POINTER_TYPE);
2979 warning ("cast adds new qualifiers to function type");
2982 /* There are qualifiers present in IN_OTYPE that are not
2983 present in IN_TYPE. */
2984 warning ("cast discards qualifiers from pointer target type");
2987 /* Warn about possible alignment problems. */
2988 if (STRICT_ALIGNMENT && warn_cast_align
2989 && TREE_CODE (type) == POINTER_TYPE
2990 && TREE_CODE (otype) == POINTER_TYPE
2991 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
2992 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
2993 /* Don't warn about opaque types, where the actual alignment
2994 restriction is unknown. */
2995 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
2996 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
2997 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
2998 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
2999 warning ("cast increases required alignment of target type");
3001 if (TREE_CODE (type) == INTEGER_TYPE
3002 && TREE_CODE (otype) == POINTER_TYPE
3003 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3004 && !TREE_CONSTANT (value))
3005 warning ("cast from pointer to integer of different size");
3007 if (warn_bad_function_cast
3008 && TREE_CODE (value) == CALL_EXPR
3009 && TREE_CODE (type) != TREE_CODE (otype))
3010 warning ("cast does not match function type");
3012 if (TREE_CODE (type) == POINTER_TYPE
3013 && TREE_CODE (otype) == INTEGER_TYPE
3014 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3015 /* Don't warn about converting any constant. */
3016 && !TREE_CONSTANT (value))
3017 warning ("cast to pointer from integer of different size");
3019 if (TREE_CODE (type) == POINTER_TYPE
3020 && TREE_CODE (otype) == POINTER_TYPE
3021 && TREE_CODE (expr) == ADDR_EXPR
3022 && DECL_P (TREE_OPERAND (expr, 0))
3023 && flag_strict_aliasing && warn_strict_aliasing
3024 && !VOID_TYPE_P (TREE_TYPE (type)))
3026 /* Casting the address of a decl to non void pointer. Warn
3027 if the cast breaks type based aliasing. */
3028 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3029 warning ("type-punning to incomplete type might break strict-aliasing rules");
3032 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3033 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3035 if (!alias_sets_conflict_p (set1, set2))
3036 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3037 else if (warn_strict_aliasing > 1
3038 && !alias_sets_might_conflict_p (set1, set2))
3039 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3043 /* If pedantic, warn for conversions between function and object
3044 pointer types, except for converting a null pointer constant
3045 to function pointer type. */
3047 && TREE_CODE (type) == POINTER_TYPE
3048 && TREE_CODE (otype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3050 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3051 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3054 && TREE_CODE (type) == POINTER_TYPE
3055 && TREE_CODE (otype) == POINTER_TYPE
3056 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3057 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3058 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3059 && TREE_CODE (expr) != NOP_EXPR))
3060 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3063 /* Replace a nonvolatile const static variable with its value. */
3064 if (optimize && TREE_CODE (value) == VAR_DECL)
3065 value = decl_constant_value (value);
3066 value = convert (type, value);
3068 /* Ignore any integer overflow caused by the cast. */
3069 if (TREE_CODE (value) == INTEGER_CST)
3071 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3072 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3076 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3077 if (TREE_CODE (value) == INTEGER_CST
3078 && TREE_CODE (expr) == INTEGER_CST
3079 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3080 value = non_lvalue (value);
3082 /* Don't let a cast be an lvalue. */
3084 value = non_lvalue (value);
3089 /* Interpret a cast of expression EXPR to type TYPE. */
3091 c_cast_expr (tree type, tree expr)
3093 int saved_wsp = warn_strict_prototypes;
3095 /* This avoids warnings about unprototyped casts on
3096 integers. E.g. "#define SIG_DFL (void(*)())0". */
3097 if (TREE_CODE (expr) == INTEGER_CST)
3098 warn_strict_prototypes = 0;
3099 type = groktypename (type);
3100 warn_strict_prototypes = saved_wsp;
3102 return build_c_cast (type, expr);
3106 /* Build an assignment expression of lvalue LHS from value RHS.
3107 MODIFYCODE is the code for a binary operator that we use
3108 to combine the old value of LHS with RHS to get the new value.
3109 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3112 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3116 tree lhstype = TREE_TYPE (lhs);
3117 tree olhstype = lhstype;
3119 /* Types that aren't fully specified cannot be used in assignments. */
3120 lhs = require_complete_type (lhs);
3122 /* Avoid duplicate error messages from operands that had errors. */
3123 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3124 return error_mark_node;
3126 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3127 /* Do not use STRIP_NOPS here. We do not want an enumerator
3128 whose value is 0 to count as a null pointer constant. */
3129 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3130 rhs = TREE_OPERAND (rhs, 0);
3134 /* If a binary op has been requested, combine the old LHS value with the RHS
3135 producing the value we should actually store into the LHS. */
3137 if (modifycode != NOP_EXPR)
3139 lhs = stabilize_reference (lhs);
3140 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3143 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3144 return error_mark_node;
3146 /* Warn about storing in something that is `const'. */
3148 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3149 || ((TREE_CODE (lhstype) == RECORD_TYPE
3150 || TREE_CODE (lhstype) == UNION_TYPE)
3151 && C_TYPE_FIELDS_READONLY (lhstype)))
3152 readonly_error (lhs, "assignment");
3154 /* If storing into a structure or union member,
3155 it has probably been given type `int'.
3156 Compute the type that would go with
3157 the actual amount of storage the member occupies. */
3159 if (TREE_CODE (lhs) == COMPONENT_REF
3160 && (TREE_CODE (lhstype) == INTEGER_TYPE
3161 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3162 || TREE_CODE (lhstype) == REAL_TYPE
3163 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3164 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3166 /* If storing in a field that is in actuality a short or narrower than one,
3167 we must store in the field in its actual type. */
3169 if (lhstype != TREE_TYPE (lhs))
3171 lhs = copy_node (lhs);
3172 TREE_TYPE (lhs) = lhstype;
3175 /* Convert new value to destination type. */
3177 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3178 NULL_TREE, NULL_TREE, 0);
3179 if (TREE_CODE (newrhs) == ERROR_MARK)
3180 return error_mark_node;
3184 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3185 TREE_SIDE_EFFECTS (result) = 1;
3187 /* If we got the LHS in a different type for storing in,
3188 convert the result back to the nominal type of LHS
3189 so that the value we return always has the same type
3190 as the LHS argument. */
3192 if (olhstype == TREE_TYPE (result))
3194 return convert_for_assignment (olhstype, result, _("assignment"),
3195 NULL_TREE, NULL_TREE, 0);
3198 /* Convert value RHS to type TYPE as preparation for an assignment
3199 to an lvalue of type TYPE.
3200 The real work of conversion is done by `convert'.
3201 The purpose of this function is to generate error messages
3202 for assignments that are not allowed in C.
3203 ERRTYPE is a string to use in error messages:
3204 "assignment", "return", etc. If it is null, this is parameter passing
3205 for a function call (and different error messages are output).
3207 FUNNAME is the name of the function being called,
3208 as an IDENTIFIER_NODE, or null.
3209 PARMNUM is the number of the argument, for printing in error messages. */
3212 convert_for_assignment (tree type, tree rhs, const char *errtype,
3213 tree fundecl, tree funname, int parmnum)
3215 enum tree_code codel = TREE_CODE (type);
3217 enum tree_code coder;
3219 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3220 /* Do not use STRIP_NOPS here. We do not want an enumerator
3221 whose value is 0 to count as a null pointer constant. */
3222 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3223 rhs = TREE_OPERAND (rhs, 0);
3225 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3226 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3227 rhs = default_conversion (rhs);
3228 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3229 rhs = decl_constant_value_for_broken_optimization (rhs);
3231 rhstype = TREE_TYPE (rhs);
3232 coder = TREE_CODE (rhstype);
3234 if (coder == ERROR_MARK)
3235 return error_mark_node;
3237 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3239 overflow_warning (rhs);
3240 /* Check for Objective-C protocols. This will automatically
3241 issue a warning if there are protocol violations. No need to
3242 use the return value. */
3243 if (c_dialect_objc ())
3244 objc_comptypes (type, rhstype, 0);
3248 if (coder == VOID_TYPE)
3250 error ("void value not ignored as it ought to be");
3251 return error_mark_node;
3253 /* A type converts to a reference to it.
3254 This code doesn't fully support references, it's just for the
3255 special case of va_start and va_copy. */
3256 if (codel == REFERENCE_TYPE
3257 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3259 if (!lvalue_p (rhs))
3261 error ("cannot pass rvalue to reference parameter");
3262 return error_mark_node;
3264 if (!c_mark_addressable (rhs))
3265 return error_mark_node;
3266 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3268 /* We already know that these two types are compatible, but they
3269 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3270 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3271 likely to be va_list, a typedef to __builtin_va_list, which
3272 is different enough that it will cause problems later. */
3273 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3274 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3276 rhs = build1 (NOP_EXPR, type, rhs);
3279 /* Some types can interconvert without explicit casts. */
3280 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3281 && ((*targetm.vector_opaque_p) (type)
3282 || (*targetm.vector_opaque_p) (rhstype)))
3283 return convert (type, rhs);
3284 /* Arithmetic types all interconvert, and enum is treated like int. */
3285 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3286 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3287 || codel == BOOLEAN_TYPE)
3288 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3289 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3290 || coder == BOOLEAN_TYPE))
3291 return convert_and_check (type, rhs);
3293 /* Conversion to a transparent union from its member types.
3294 This applies only to function arguments. */
3295 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3298 tree marginal_memb_type = 0;
3300 for (memb_types = TYPE_FIELDS (type); memb_types;
3301 memb_types = TREE_CHAIN (memb_types))
3303 tree memb_type = TREE_TYPE (memb_types);
3305 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3306 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3309 if (TREE_CODE (memb_type) != POINTER_TYPE)
3312 if (coder == POINTER_TYPE)
3314 tree ttl = TREE_TYPE (memb_type);
3315 tree ttr = TREE_TYPE (rhstype);
3317 /* Any non-function converts to a [const][volatile] void *
3318 and vice versa; otherwise, targets must be the same.
3319 Meanwhile, the lhs target must have all the qualifiers of
3321 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3322 || comp_target_types (memb_type, rhstype, 0))
3324 /* If this type won't generate any warnings, use it. */
3325 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3326 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3327 && TREE_CODE (ttl) == FUNCTION_TYPE)
3328 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3329 == TYPE_QUALS (ttr))
3330 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3331 == TYPE_QUALS (ttl))))
3334 /* Keep looking for a better type, but remember this one. */
3335 if (! marginal_memb_type)
3336 marginal_memb_type = memb_type;
3340 /* Can convert integer zero to any pointer type. */
3341 if (integer_zerop (rhs)
3342 || (TREE_CODE (rhs) == NOP_EXPR
3343 && integer_zerop (TREE_OPERAND (rhs, 0))))
3345 rhs = null_pointer_node;
3350 if (memb_types || marginal_memb_type)
3354 /* We have only a marginally acceptable member type;
3355 it needs a warning. */
3356 tree ttl = TREE_TYPE (marginal_memb_type);
3357 tree ttr = TREE_TYPE (rhstype);
3359 /* Const and volatile mean something different for function
3360 types, so the usual warnings are not appropriate. */
3361 if (TREE_CODE (ttr) == FUNCTION_TYPE
3362 && TREE_CODE (ttl) == FUNCTION_TYPE)
3364 /* Because const and volatile on functions are
3365 restrictions that say the function will not do
3366 certain things, it is okay to use a const or volatile
3367 function where an ordinary one is wanted, but not
3369 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3370 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3371 errtype, funname, parmnum);
3373 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3374 warn_for_assignment ("%s discards qualifiers from pointer target type",
3379 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3380 pedwarn ("ISO C prohibits argument conversion to union type");
3382 return build1 (NOP_EXPR, type, rhs);
3386 /* Conversions among pointers */
3387 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3388 && (coder == codel))
3390 tree ttl = TREE_TYPE (type);
3391 tree ttr = TREE_TYPE (rhstype);
3392 bool is_opaque_pointer;
3393 int target_cmp = 0; /* Cache comp_target_types () result. */
3395 /* Opaque pointers are treated like void pointers. */
3396 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
3397 || (*targetm.vector_opaque_p) (rhstype))
3398 && TREE_CODE (ttl) == VECTOR_TYPE
3399 && TREE_CODE (ttr) == VECTOR_TYPE;
3401 /* Any non-function converts to a [const][volatile] void *
3402 and vice versa; otherwise, targets must be the same.
3403 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3404 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3405 || (target_cmp = comp_target_types (type, rhstype, 0))
3406 || is_opaque_pointer
3407 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3408 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3411 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3414 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3415 which are not ANSI null ptr constants. */
3416 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3417 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3418 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3419 errtype, funname, parmnum);
3420 /* Const and volatile mean something different for function types,
3421 so the usual warnings are not appropriate. */
3422 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3423 && TREE_CODE (ttl) != FUNCTION_TYPE)
3425 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3426 warn_for_assignment ("%s discards qualifiers from pointer target type",
3427 errtype, funname, parmnum);
3428 /* If this is not a case of ignoring a mismatch in signedness,
3430 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3433 /* If there is a mismatch, do warn. */
3435 warn_for_assignment ("pointer targets in %s differ in signedness",
3436 errtype, funname, parmnum);
3438 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3439 && TREE_CODE (ttr) == FUNCTION_TYPE)
3441 /* Because const and volatile on functions are restrictions
3442 that say the function will not do certain things,
3443 it is okay to use a const or volatile function
3444 where an ordinary one is wanted, but not vice-versa. */
3445 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3446 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3447 errtype, funname, parmnum);
3451 warn_for_assignment ("%s from incompatible pointer type",
3452 errtype, funname, parmnum);
3453 return convert (type, rhs);
3455 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3457 error ("invalid use of non-lvalue array");
3458 return error_mark_node;
3460 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3462 /* An explicit constant 0 can convert to a pointer,
3463 or one that results from arithmetic, even including
3464 a cast to integer type. */
3465 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3467 ! (TREE_CODE (rhs) == NOP_EXPR
3468 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3469 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3470 && integer_zerop (TREE_OPERAND (rhs, 0))))
3471 warn_for_assignment ("%s makes pointer from integer without a cast",
3472 errtype, funname, parmnum);
3474 return convert (type, rhs);
3476 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3478 warn_for_assignment ("%s makes integer from pointer without a cast",
3479 errtype, funname, parmnum);
3480 return convert (type, rhs);
3482 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3483 return convert (type, rhs);
3489 tree selector = objc_message_selector ();
3491 if (selector && parmnum > 2)
3492 error ("incompatible type for argument %d of `%s'",
3493 parmnum - 2, IDENTIFIER_POINTER (selector));
3495 error ("incompatible type for argument %d of `%s'",
3496 parmnum, IDENTIFIER_POINTER (funname));
3499 error ("incompatible type for argument %d of indirect function call",
3503 error ("incompatible types in %s", errtype);
3505 return error_mark_node;
3508 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3509 is used for error and waring reporting and indicates which argument
3510 is being processed. */
3513 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3517 /* If FN was prototyped, the value has been converted already
3518 in convert_arguments. */
3519 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3522 type = TREE_TYPE (parm);
3523 ret = convert_for_assignment (type, value,
3524 (char *) 0 /* arg passing */, fn,
3525 DECL_NAME (fn), argnum);
3526 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3527 && INTEGRAL_TYPE_P (type)
3528 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3529 ret = default_conversion (ret);
3533 /* Print a warning using MSGID.
3534 It gets OPNAME as its one parameter.
3535 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3536 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3537 FUNCTION and ARGNUM are handled specially if we are building an
3538 Objective-C selector. */
3541 warn_for_assignment (const char *msgid, const char *opname, tree function,
3546 tree selector = objc_message_selector ();
3549 if (selector && argnum > 2)
3551 function = selector;
3558 /* Function name is known; supply it. */
3559 const char *const argstring = _("passing arg of `%s'");
3560 new_opname = alloca (IDENTIFIER_LENGTH (function)
3561 + strlen (argstring) + 1 + 1);
3562 sprintf (new_opname, argstring,
3563 IDENTIFIER_POINTER (function));
3567 /* Function name unknown (call through ptr). */
3568 const char *const argnofun = _("passing arg of pointer to function");
3569 new_opname = alloca (strlen (argnofun) + 1 + 1);
3570 sprintf (new_opname, argnofun);
3575 /* Function name is known; supply it. */
3576 const char *const argstring = _("passing arg %d of `%s'");
3577 new_opname = alloca (IDENTIFIER_LENGTH (function)
3578 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3579 sprintf (new_opname, argstring, argnum,
3580 IDENTIFIER_POINTER (function));
3584 /* Function name unknown (call through ptr); just give arg number. */
3585 const char *const argnofun = _("passing arg %d of pointer to function");
3586 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3587 sprintf (new_opname, argnofun, argnum);
3589 opname = new_opname;
3591 pedwarn (msgid, opname);
3594 /* If VALUE is a compound expr all of whose expressions are constant, then
3595 return its value. Otherwise, return error_mark_node.
3597 This is for handling COMPOUND_EXPRs as initializer elements
3598 which is allowed with a warning when -pedantic is specified. */
3601 valid_compound_expr_initializer (tree value, tree endtype)
3603 if (TREE_CODE (value) == COMPOUND_EXPR)
3605 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3607 return error_mark_node;
3608 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3611 else if (! TREE_CONSTANT (value)
3612 && ! initializer_constant_valid_p (value, endtype))
3613 return error_mark_node;
3618 /* Perform appropriate conversions on the initial value of a variable,
3619 store it in the declaration DECL,
3620 and print any error messages that are appropriate.
3621 If the init is invalid, store an ERROR_MARK. */
3624 store_init_value (tree decl, tree init)
3628 /* If variable's type was invalidly declared, just ignore it. */
3630 type = TREE_TYPE (decl);
3631 if (TREE_CODE (type) == ERROR_MARK)
3634 /* Digest the specified initializer into an expression. */
3636 value = digest_init (type, init, TREE_STATIC (decl));
3638 /* Store the expression if valid; else report error. */
3640 if (warn_traditional && !in_system_header
3641 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3642 warning ("traditional C rejects automatic aggregate initialization");
3644 DECL_INITIAL (decl) = value;
3646 /* ANSI wants warnings about out-of-range constant initializers. */
3647 STRIP_TYPE_NOPS (value);
3648 constant_expression_warning (value);
3650 /* Check if we need to set array size from compound literal size. */
3651 if (TREE_CODE (type) == ARRAY_TYPE
3652 && TYPE_DOMAIN (type) == 0
3653 && value != error_mark_node)
3655 tree inside_init = init;
3657 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3658 inside_init = TREE_OPERAND (init, 0);
3659 inside_init = fold (inside_init);
3661 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3663 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3665 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3667 /* For int foo[] = (int [3]){1}; we need to set array size
3668 now since later on array initializer will be just the
3669 brace enclosed list of the compound literal. */
3670 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3672 layout_decl (decl, 0);
3678 /* Methods for storing and printing names for error messages. */
3680 /* Implement a spelling stack that allows components of a name to be pushed
3681 and popped. Each element on the stack is this structure. */
3693 #define SPELLING_STRING 1
3694 #define SPELLING_MEMBER 2
3695 #define SPELLING_BOUNDS 3
3697 static struct spelling *spelling; /* Next stack element (unused). */
3698 static struct spelling *spelling_base; /* Spelling stack base. */
3699 static int spelling_size; /* Size of the spelling stack. */
3701 /* Macros to save and restore the spelling stack around push_... functions.
3702 Alternative to SAVE_SPELLING_STACK. */
3704 #define SPELLING_DEPTH() (spelling - spelling_base)
3705 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3707 /* Push an element on the spelling stack with type KIND and assign VALUE
3710 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3712 int depth = SPELLING_DEPTH (); \
3714 if (depth >= spelling_size) \
3716 spelling_size += 10; \
3717 if (spelling_base == 0) \
3718 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3720 spelling_base = xrealloc (spelling_base, \
3721 spelling_size * sizeof (struct spelling)); \
3722 RESTORE_SPELLING_DEPTH (depth); \
3725 spelling->kind = (KIND); \
3726 spelling->MEMBER = (VALUE); \
3730 /* Push STRING on the stack. Printed literally. */
3733 push_string (const char *string)
3735 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3738 /* Push a member name on the stack. Printed as '.' STRING. */
3741 push_member_name (tree decl)
3743 const char *const string
3744 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3745 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3748 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3751 push_array_bounds (int bounds)
3753 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3756 /* Compute the maximum size in bytes of the printed spelling. */
3759 spelling_length (void)
3764 for (p = spelling_base; p < spelling; p++)
3766 if (p->kind == SPELLING_BOUNDS)
3769 size += strlen (p->u.s) + 1;
3775 /* Print the spelling to BUFFER and return it. */
3778 print_spelling (char *buffer)
3783 for (p = spelling_base; p < spelling; p++)
3784 if (p->kind == SPELLING_BOUNDS)
3786 sprintf (d, "[%d]", p->u.i);
3792 if (p->kind == SPELLING_MEMBER)
3794 for (s = p->u.s; (*d = *s++); d++)
3801 /* Issue an error message for a bad initializer component.
3802 MSGID identifies the message.
3803 The component name is taken from the spelling stack. */
3806 error_init (const char *msgid)
3810 error ("%s", _(msgid));
3811 ofwhat = print_spelling (alloca (spelling_length () + 1));
3813 error ("(near initialization for `%s')", ofwhat);
3816 /* Issue a pedantic warning for a bad initializer component.
3817 MSGID identifies the message.
3818 The component name is taken from the spelling stack. */
3821 pedwarn_init (const char *msgid)
3825 pedwarn ("%s", _(msgid));
3826 ofwhat = print_spelling (alloca (spelling_length () + 1));
3828 pedwarn ("(near initialization for `%s')", ofwhat);
3831 /* Issue a warning for a bad initializer component.
3832 MSGID identifies the message.
3833 The component name is taken from the spelling stack. */
3836 warning_init (const char *msgid)
3840 warning ("%s", _(msgid));
3841 ofwhat = print_spelling (alloca (spelling_length () + 1));
3843 warning ("(near initialization for `%s')", ofwhat);
3846 /* Digest the parser output INIT as an initializer for type TYPE.
3847 Return a C expression of type TYPE to represent the initial value.
3849 REQUIRE_CONSTANT requests an error if non-constant initializers or
3850 elements are seen. */
3853 digest_init (tree type, tree init, int require_constant)
3855 enum tree_code code = TREE_CODE (type);
3856 tree inside_init = init;
3858 if (type == error_mark_node
3859 || init == error_mark_node
3860 || TREE_TYPE (init) == error_mark_node)
3861 return error_mark_node;
3863 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3864 /* Do not use STRIP_NOPS here. We do not want an enumerator
3865 whose value is 0 to count as a null pointer constant. */
3866 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3867 inside_init = TREE_OPERAND (init, 0);
3869 inside_init = fold (inside_init);
3871 /* Initialization of an array of chars from a string constant
3872 optionally enclosed in braces. */
3874 if (code == ARRAY_TYPE)
3876 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3877 if ((typ1 == char_type_node
3878 || typ1 == signed_char_type_node
3879 || typ1 == unsigned_char_type_node
3880 || typ1 == unsigned_wchar_type_node
3881 || typ1 == signed_wchar_type_node)
3882 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3884 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3885 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3888 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3890 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3892 error_init ("char-array initialized from wide string");
3893 return error_mark_node;
3895 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3897 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3899 error_init ("int-array initialized from non-wide string");
3900 return error_mark_node;
3903 TREE_TYPE (inside_init) = type;
3904 if (TYPE_DOMAIN (type) != 0
3905 && TYPE_SIZE (type) != 0
3906 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3907 /* Subtract 1 (or sizeof (wchar_t))
3908 because it's ok to ignore the terminating null char
3909 that is counted in the length of the constant. */
3910 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3911 TREE_STRING_LENGTH (inside_init)
3912 - ((TYPE_PRECISION (typ1)
3913 != TYPE_PRECISION (char_type_node))
3914 ? (TYPE_PRECISION (wchar_type_node)
3917 pedwarn_init ("initializer-string for array of chars is too long");
3923 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3924 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3925 below and handle as a constructor. */
3926 if (code == VECTOR_TYPE
3927 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
3928 && TREE_CONSTANT (inside_init))
3930 if (TREE_CODE (inside_init) == VECTOR_CST
3931 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3932 TYPE_MAIN_VARIANT (type),
3936 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
3939 /* Any type can be initialized
3940 from an expression of the same type, optionally with braces. */
3942 if (inside_init && TREE_TYPE (inside_init) != 0
3943 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3944 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
3945 || (code == ARRAY_TYPE
3946 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3947 || (code == VECTOR_TYPE
3948 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3949 || (code == POINTER_TYPE
3950 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3951 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3952 TREE_TYPE (type), COMPARE_STRICT))
3953 || (code == POINTER_TYPE
3954 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
3955 && comptypes (TREE_TYPE (inside_init),
3956 TREE_TYPE (type), COMPARE_STRICT))))
3958 if (code == POINTER_TYPE)
3960 inside_init = default_function_array_conversion (inside_init);
3962 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
3964 error_init ("invalid use of non-lvalue array");
3965 return error_mark_node;
3969 if (code == VECTOR_TYPE)
3970 /* Although the types are compatible, we may require a
3972 inside_init = convert (type, inside_init);
3974 if (require_constant && !flag_isoc99
3975 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3977 /* As an extension, allow initializing objects with static storage
3978 duration with compound literals (which are then treated just as
3979 the brace enclosed list they contain). */
3980 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3981 inside_init = DECL_INITIAL (decl);
3984 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
3985 && TREE_CODE (inside_init) != CONSTRUCTOR)
3987 error_init ("array initialized from non-constant array expression");
3988 return error_mark_node;
3991 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
3992 inside_init = decl_constant_value_for_broken_optimization (inside_init);
3994 /* Compound expressions can only occur here if -pedantic or
3995 -pedantic-errors is specified. In the later case, we always want
3996 an error. In the former case, we simply want a warning. */
3997 if (require_constant && pedantic
3998 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4001 = valid_compound_expr_initializer (inside_init,
4002 TREE_TYPE (inside_init));
4003 if (inside_init == error_mark_node)
4004 error_init ("initializer element is not constant");
4006 pedwarn_init ("initializer element is not constant");
4007 if (flag_pedantic_errors)
4008 inside_init = error_mark_node;
4010 else if (require_constant
4011 && (!TREE_CONSTANT (inside_init)
4012 /* This test catches things like `7 / 0' which
4013 result in an expression for which TREE_CONSTANT
4014 is true, but which is not actually something
4015 that is a legal constant. We really should not
4016 be using this function, because it is a part of
4017 the back-end. Instead, the expression should
4018 already have been turned into ERROR_MARK_NODE. */
4019 || !initializer_constant_valid_p (inside_init,
4020 TREE_TYPE (inside_init))))
4022 error_init ("initializer element is not constant");
4023 inside_init = error_mark_node;
4029 /* Handle scalar types, including conversions. */
4031 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4032 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4034 /* Note that convert_for_assignment calls default_conversion
4035 for arrays and functions. We must not call it in the
4036 case where inside_init is a null pointer constant. */
4038 = convert_for_assignment (type, init, _("initialization"),
4039 NULL_TREE, NULL_TREE, 0);
4041 if (require_constant && ! TREE_CONSTANT (inside_init))
4043 error_init ("initializer element is not constant");
4044 inside_init = error_mark_node;
4046 else if (require_constant
4047 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4049 error_init ("initializer element is not computable at load time");
4050 inside_init = error_mark_node;
4056 /* Come here only for records and arrays. */
4058 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4060 error_init ("variable-sized object may not be initialized");
4061 return error_mark_node;
4064 error_init ("invalid initializer");
4065 return error_mark_node;
4068 /* Handle initializers that use braces. */
4070 /* Type of object we are accumulating a constructor for.
4071 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4072 static tree constructor_type;
4074 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4076 static tree constructor_fields;
4078 /* For an ARRAY_TYPE, this is the specified index
4079 at which to store the next element we get. */
4080 static tree constructor_index;
4082 /* For an ARRAY_TYPE, this is the maximum index. */
4083 static tree constructor_max_index;
4085 /* For a RECORD_TYPE, this is the first field not yet written out. */
4086 static tree constructor_unfilled_fields;
4088 /* For an ARRAY_TYPE, this is the index of the first element
4089 not yet written out. */
4090 static tree constructor_unfilled_index;
4092 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4093 This is so we can generate gaps between fields, when appropriate. */
4094 static tree constructor_bit_index;
4096 /* If we are saving up the elements rather than allocating them,
4097 this is the list of elements so far (in reverse order,
4098 most recent first). */
4099 static tree constructor_elements;
4101 /* 1 if constructor should be incrementally stored into a constructor chain,
4102 0 if all the elements should be kept in AVL tree. */
4103 static int constructor_incremental;
4105 /* 1 if so far this constructor's elements are all compile-time constants. */
4106 static int constructor_constant;
4108 /* 1 if so far this constructor's elements are all valid address constants. */
4109 static int constructor_simple;
4111 /* 1 if this constructor is erroneous so far. */
4112 static int constructor_erroneous;
4114 /* Structure for managing pending initializer elements, organized as an
4119 struct init_node *left, *right;
4120 struct init_node *parent;
4126 /* Tree of pending elements at this constructor level.
4127 These are elements encountered out of order
4128 which belong at places we haven't reached yet in actually
4130 Will never hold tree nodes across GC runs. */
4131 static struct init_node *constructor_pending_elts;
4133 /* The SPELLING_DEPTH of this constructor. */
4134 static int constructor_depth;
4136 /* 0 if implicitly pushing constructor levels is allowed. */
4137 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4139 static int require_constant_value;
4140 static int require_constant_elements;
4142 /* DECL node for which an initializer is being read.
4143 0 means we are reading a constructor expression
4144 such as (struct foo) {...}. */
4145 static tree constructor_decl;
4147 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4148 static const char *constructor_asmspec;
4150 /* Nonzero if this is an initializer for a top-level decl. */
4151 static int constructor_top_level;
4153 /* Nonzero if there were any member designators in this initializer. */
4154 static int constructor_designated;
4156 /* Nesting depth of designator list. */
4157 static int designator_depth;
4159 /* Nonzero if there were diagnosed errors in this designator list. */
4160 static int designator_errorneous;
4163 /* This stack has a level for each implicit or explicit level of
4164 structuring in the initializer, including the outermost one. It
4165 saves the values of most of the variables above. */
4167 struct constructor_range_stack;
4169 struct constructor_stack
4171 struct constructor_stack *next;
4176 tree unfilled_index;
4177 tree unfilled_fields;
4180 struct init_node *pending_elts;
4183 /* If nonzero, this value should replace the entire
4184 constructor at this level. */
4185 tree replacement_value;
4186 struct constructor_range_stack *range_stack;
4196 struct constructor_stack *constructor_stack;
4198 /* This stack represents designators from some range designator up to
4199 the last designator in the list. */
4201 struct constructor_range_stack
4203 struct constructor_range_stack *next, *prev;
4204 struct constructor_stack *stack;
4211 struct constructor_range_stack *constructor_range_stack;
4213 /* This stack records separate initializers that are nested.
4214 Nested initializers can't happen in ANSI C, but GNU C allows them
4215 in cases like { ... (struct foo) { ... } ... }. */
4217 struct initializer_stack
4219 struct initializer_stack *next;
4221 const char *asmspec;
4222 struct constructor_stack *constructor_stack;
4223 struct constructor_range_stack *constructor_range_stack;
4225 struct spelling *spelling;
4226 struct spelling *spelling_base;
4229 char require_constant_value;
4230 char require_constant_elements;
4233 struct initializer_stack *initializer_stack;
4235 /* Prepare to parse and output the initializer for variable DECL. */
4238 start_init (tree decl, tree asmspec_tree, int top_level)
4241 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4242 const char *asmspec = 0;
4245 asmspec = TREE_STRING_POINTER (asmspec_tree);
4247 p->decl = constructor_decl;
4248 p->asmspec = constructor_asmspec;
4249 p->require_constant_value = require_constant_value;
4250 p->require_constant_elements = require_constant_elements;
4251 p->constructor_stack = constructor_stack;
4252 p->constructor_range_stack = constructor_range_stack;
4253 p->elements = constructor_elements;
4254 p->spelling = spelling;
4255 p->spelling_base = spelling_base;
4256 p->spelling_size = spelling_size;
4257 p->top_level = constructor_top_level;
4258 p->next = initializer_stack;
4259 initializer_stack = p;
4261 constructor_decl = decl;
4262 constructor_asmspec = asmspec;
4263 constructor_designated = 0;
4264 constructor_top_level = top_level;
4268 require_constant_value = TREE_STATIC (decl);
4269 require_constant_elements
4270 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4271 /* For a scalar, you can always use any value to initialize,
4272 even within braces. */
4273 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4274 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4275 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4276 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4277 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4281 require_constant_value = 0;
4282 require_constant_elements = 0;
4283 locus = "(anonymous)";
4286 constructor_stack = 0;
4287 constructor_range_stack = 0;
4289 missing_braces_mentioned = 0;
4293 RESTORE_SPELLING_DEPTH (0);
4296 push_string (locus);
4302 struct initializer_stack *p = initializer_stack;
4304 /* Free the whole constructor stack of this initializer. */
4305 while (constructor_stack)
4307 struct constructor_stack *q = constructor_stack;
4308 constructor_stack = q->next;
4312 if (constructor_range_stack)
4315 /* Pop back to the data of the outer initializer (if any). */
4316 free (spelling_base);
4318 constructor_decl = p->decl;
4319 constructor_asmspec = p->asmspec;
4320 require_constant_value = p->require_constant_value;
4321 require_constant_elements = p->require_constant_elements;
4322 constructor_stack = p->constructor_stack;
4323 constructor_range_stack = p->constructor_range_stack;
4324 constructor_elements = p->elements;
4325 spelling = p->spelling;
4326 spelling_base = p->spelling_base;
4327 spelling_size = p->spelling_size;
4328 constructor_top_level = p->top_level;
4329 initializer_stack = p->next;
4333 /* Call here when we see the initializer is surrounded by braces.
4334 This is instead of a call to push_init_level;
4335 it is matched by a call to pop_init_level.
4337 TYPE is the type to initialize, for a constructor expression.
4338 For an initializer for a decl, TYPE is zero. */
4341 really_start_incremental_init (tree type)
4343 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4346 type = TREE_TYPE (constructor_decl);
4348 if ((*targetm.vector_opaque_p) (type))
4349 error ("opaque vector types cannot be initialized");
4351 p->type = constructor_type;
4352 p->fields = constructor_fields;
4353 p->index = constructor_index;
4354 p->max_index = constructor_max_index;
4355 p->unfilled_index = constructor_unfilled_index;
4356 p->unfilled_fields = constructor_unfilled_fields;
4357 p->bit_index = constructor_bit_index;
4358 p->elements = constructor_elements;
4359 p->constant = constructor_constant;
4360 p->simple = constructor_simple;
4361 p->erroneous = constructor_erroneous;
4362 p->pending_elts = constructor_pending_elts;
4363 p->depth = constructor_depth;
4364 p->replacement_value = 0;
4368 p->incremental = constructor_incremental;
4369 p->designated = constructor_designated;
4371 constructor_stack = p;
4373 constructor_constant = 1;
4374 constructor_simple = 1;
4375 constructor_depth = SPELLING_DEPTH ();
4376 constructor_elements = 0;
4377 constructor_pending_elts = 0;
4378 constructor_type = type;
4379 constructor_incremental = 1;
4380 constructor_designated = 0;
4381 designator_depth = 0;
4382 designator_errorneous = 0;
4384 if (TREE_CODE (constructor_type) == RECORD_TYPE
4385 || TREE_CODE (constructor_type) == UNION_TYPE)
4387 constructor_fields = TYPE_FIELDS (constructor_type);
4388 /* Skip any nameless bit fields at the beginning. */
4389 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4390 && DECL_NAME (constructor_fields) == 0)
4391 constructor_fields = TREE_CHAIN (constructor_fields);
4393 constructor_unfilled_fields = constructor_fields;
4394 constructor_bit_index = bitsize_zero_node;
4396 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4398 if (TYPE_DOMAIN (constructor_type))
4400 constructor_max_index
4401 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4403 /* Detect non-empty initializations of zero-length arrays. */
4404 if (constructor_max_index == NULL_TREE
4405 && TYPE_SIZE (constructor_type))
4406 constructor_max_index = build_int_2 (-1, -1);
4408 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4409 to initialize VLAs will cause a proper error; avoid tree
4410 checking errors as well by setting a safe value. */
4411 if (constructor_max_index
4412 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4413 constructor_max_index = build_int_2 (-1, -1);
4416 = convert (bitsizetype,
4417 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4420 constructor_index = bitsize_zero_node;
4422 constructor_unfilled_index = constructor_index;
4424 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4426 /* Vectors are like simple fixed-size arrays. */
4427 constructor_max_index =
4428 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4429 constructor_index = convert (bitsizetype, bitsize_zero_node);
4430 constructor_unfilled_index = constructor_index;
4434 /* Handle the case of int x = {5}; */
4435 constructor_fields = constructor_type;
4436 constructor_unfilled_fields = constructor_type;
4440 /* Push down into a subobject, for initialization.
4441 If this is for an explicit set of braces, IMPLICIT is 0.
4442 If it is because the next element belongs at a lower level,
4443 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4446 push_init_level (int implicit)
4448 struct constructor_stack *p;
4449 tree value = NULL_TREE;
4451 /* If we've exhausted any levels that didn't have braces,
4453 while (constructor_stack->implicit)
4455 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4456 || TREE_CODE (constructor_type) == UNION_TYPE)
4457 && constructor_fields == 0)
4458 process_init_element (pop_init_level (1));
4459 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4460 && constructor_max_index
4461 && tree_int_cst_lt (constructor_max_index, constructor_index))
4462 process_init_element (pop_init_level (1));
4467 /* Unless this is an explicit brace, we need to preserve previous
4471 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4472 || TREE_CODE (constructor_type) == UNION_TYPE)
4473 && constructor_fields)
4474 value = find_init_member (constructor_fields);
4475 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4476 value = find_init_member (constructor_index);
4479 p = xmalloc (sizeof (struct constructor_stack));
4480 p->type = constructor_type;
4481 p->fields = constructor_fields;
4482 p->index = constructor_index;
4483 p->max_index = constructor_max_index;
4484 p->unfilled_index = constructor_unfilled_index;
4485 p->unfilled_fields = constructor_unfilled_fields;
4486 p->bit_index = constructor_bit_index;
4487 p->elements = constructor_elements;
4488 p->constant = constructor_constant;
4489 p->simple = constructor_simple;
4490 p->erroneous = constructor_erroneous;
4491 p->pending_elts = constructor_pending_elts;
4492 p->depth = constructor_depth;
4493 p->replacement_value = 0;
4494 p->implicit = implicit;
4496 p->incremental = constructor_incremental;
4497 p->designated = constructor_designated;
4498 p->next = constructor_stack;
4500 constructor_stack = p;
4502 constructor_constant = 1;
4503 constructor_simple = 1;
4504 constructor_depth = SPELLING_DEPTH ();
4505 constructor_elements = 0;
4506 constructor_incremental = 1;
4507 constructor_designated = 0;
4508 constructor_pending_elts = 0;
4511 p->range_stack = constructor_range_stack;
4512 constructor_range_stack = 0;
4513 designator_depth = 0;
4514 designator_errorneous = 0;
4517 /* Don't die if an entire brace-pair level is superfluous
4518 in the containing level. */
4519 if (constructor_type == 0)
4521 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4522 || TREE_CODE (constructor_type) == UNION_TYPE)
4524 /* Don't die if there are extra init elts at the end. */
4525 if (constructor_fields == 0)
4526 constructor_type = 0;
4529 constructor_type = TREE_TYPE (constructor_fields);
4530 push_member_name (constructor_fields);
4531 constructor_depth++;
4534 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4536 constructor_type = TREE_TYPE (constructor_type);
4537 push_array_bounds (tree_low_cst (constructor_index, 0));
4538 constructor_depth++;
4541 if (constructor_type == 0)
4543 error_init ("extra brace group at end of initializer");
4544 constructor_fields = 0;
4545 constructor_unfilled_fields = 0;
4549 if (value && TREE_CODE (value) == CONSTRUCTOR)
4551 constructor_constant = TREE_CONSTANT (value);
4552 constructor_simple = TREE_STATIC (value);
4553 constructor_elements = CONSTRUCTOR_ELTS (value);
4554 if (constructor_elements
4555 && (TREE_CODE (constructor_type) == RECORD_TYPE
4556 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4557 set_nonincremental_init ();
4560 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4562 missing_braces_mentioned = 1;
4563 warning_init ("missing braces around initializer");
4566 if (TREE_CODE (constructor_type) == RECORD_TYPE
4567 || TREE_CODE (constructor_type) == UNION_TYPE)
4569 constructor_fields = TYPE_FIELDS (constructor_type);
4570 /* Skip any nameless bit fields at the beginning. */
4571 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4572 && DECL_NAME (constructor_fields) == 0)
4573 constructor_fields = TREE_CHAIN (constructor_fields);
4575 constructor_unfilled_fields = constructor_fields;
4576 constructor_bit_index = bitsize_zero_node;
4578 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4580 /* Vectors are like simple fixed-size arrays. */
4581 constructor_max_index =
4582 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4583 constructor_index = convert (bitsizetype, integer_zero_node);
4584 constructor_unfilled_index = constructor_index;
4586 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4588 if (TYPE_DOMAIN (constructor_type))
4590 constructor_max_index
4591 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4593 /* Detect non-empty initializations of zero-length arrays. */
4594 if (constructor_max_index == NULL_TREE
4595 && TYPE_SIZE (constructor_type))
4596 constructor_max_index = build_int_2 (-1, -1);
4598 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4599 to initialize VLAs will cause a proper error; avoid tree
4600 checking errors as well by setting a safe value. */
4601 if (constructor_max_index
4602 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4603 constructor_max_index = build_int_2 (-1, -1);
4606 = convert (bitsizetype,
4607 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4610 constructor_index = bitsize_zero_node;
4612 constructor_unfilled_index = constructor_index;
4613 if (value && TREE_CODE (value) == STRING_CST)
4615 /* We need to split the char/wchar array into individual
4616 characters, so that we don't have to special case it
4618 set_nonincremental_init_from_string (value);
4623 warning_init ("braces around scalar initializer");
4624 constructor_fields = constructor_type;
4625 constructor_unfilled_fields = constructor_type;
4629 /* At the end of an implicit or explicit brace level,
4630 finish up that level of constructor.
4631 If we were outputting the elements as they are read, return 0
4632 from inner levels (process_init_element ignores that),
4633 but return error_mark_node from the outermost level
4634 (that's what we want to put in DECL_INITIAL).
4635 Otherwise, return a CONSTRUCTOR expression. */
4638 pop_init_level (int implicit)
4640 struct constructor_stack *p;
4641 tree constructor = 0;
4645 /* When we come to an explicit close brace,
4646 pop any inner levels that didn't have explicit braces. */
4647 while (constructor_stack->implicit)
4648 process_init_element (pop_init_level (1));
4650 if (constructor_range_stack)
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 /* Now output all pending elements. */
4709 constructor_incremental = 1;
4710 output_pending_init_elements (1);
4712 /* Pad out the end of the structure. */
4713 if (p->replacement_value)
4714 /* If this closes a superfluous brace pair,
4715 just pass out the element between them. */
4716 constructor = p->replacement_value;
4717 else if (constructor_type == 0)
4719 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4720 && TREE_CODE (constructor_type) != UNION_TYPE
4721 && TREE_CODE (constructor_type) != ARRAY_TYPE
4722 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4724 /* A nonincremental scalar initializer--just return
4725 the element, after verifying there is just one. */
4726 if (constructor_elements == 0)
4728 if (!constructor_erroneous)
4729 error_init ("empty scalar initializer");
4730 constructor = error_mark_node;
4732 else if (TREE_CHAIN (constructor_elements) != 0)
4734 error_init ("extra elements in scalar initializer");
4735 constructor = TREE_VALUE (constructor_elements);
4738 constructor = TREE_VALUE (constructor_elements);
4742 if (constructor_erroneous)
4743 constructor = error_mark_node;
4746 constructor = build_constructor (constructor_type,
4747 nreverse (constructor_elements));
4748 if (constructor_constant)
4749 TREE_CONSTANT (constructor) = 1;
4750 if (constructor_constant && constructor_simple)
4751 TREE_STATIC (constructor) = 1;
4755 constructor_type = p->type;
4756 constructor_fields = p->fields;
4757 constructor_index = p->index;
4758 constructor_max_index = p->max_index;
4759 constructor_unfilled_index = p->unfilled_index;
4760 constructor_unfilled_fields = p->unfilled_fields;
4761 constructor_bit_index = p->bit_index;
4762 constructor_elements = p->elements;
4763 constructor_constant = p->constant;
4764 constructor_simple = p->simple;
4765 constructor_erroneous = p->erroneous;
4766 constructor_incremental = p->incremental;
4767 constructor_designated = p->designated;
4768 constructor_pending_elts = p->pending_elts;
4769 constructor_depth = p->depth;
4771 constructor_range_stack = p->range_stack;
4772 RESTORE_SPELLING_DEPTH (constructor_depth);
4774 constructor_stack = p->next;
4777 if (constructor == 0)
4779 if (constructor_stack == 0)
4780 return error_mark_node;
4786 /* Common handling for both array range and field name designators.
4787 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4790 set_designator (int array)
4793 enum tree_code subcode;
4795 /* Don't die if an entire brace-pair level is superfluous
4796 in the containing level. */
4797 if (constructor_type == 0)
4800 /* If there were errors in this designator list already, bail out silently. */
4801 if (designator_errorneous)
4804 if (!designator_depth)
4806 if (constructor_range_stack)
4809 /* Designator list starts at the level of closest explicit
4811 while (constructor_stack->implicit)
4812 process_init_element (pop_init_level (1));
4813 constructor_designated = 1;
4817 if (constructor_no_implicit)
4819 error_init ("initialization designators may not nest");
4823 if (TREE_CODE (constructor_type) == RECORD_TYPE
4824 || TREE_CODE (constructor_type) == UNION_TYPE)
4826 subtype = TREE_TYPE (constructor_fields);
4827 if (subtype != error_mark_node)
4828 subtype = TYPE_MAIN_VARIANT (subtype);
4830 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4832 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4837 subcode = TREE_CODE (subtype);
4838 if (array && subcode != ARRAY_TYPE)
4840 error_init ("array index in non-array initializer");
4843 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4845 error_init ("field name not in record or union initializer");
4849 constructor_designated = 1;
4850 push_init_level (2);
4854 /* If there are range designators in designator list, push a new designator
4855 to constructor_range_stack. RANGE_END is end of such stack range or
4856 NULL_TREE if there is no range designator at this level. */
4859 push_range_stack (tree range_end)
4861 struct constructor_range_stack *p;
4863 p = ggc_alloc (sizeof (struct constructor_range_stack));
4864 p->prev = constructor_range_stack;
4866 p->fields = constructor_fields;
4867 p->range_start = constructor_index;
4868 p->index = constructor_index;
4869 p->stack = constructor_stack;
4870 p->range_end = range_end;
4871 if (constructor_range_stack)
4872 constructor_range_stack->next = p;
4873 constructor_range_stack = p;
4876 /* Within an array initializer, specify the next index to be initialized.
4877 FIRST is that index. If LAST is nonzero, then initialize a range
4878 of indices, running from FIRST through LAST. */
4881 set_init_index (tree first, tree last)
4883 if (set_designator (1))
4886 designator_errorneous = 1;
4888 while ((TREE_CODE (first) == NOP_EXPR
4889 || TREE_CODE (first) == CONVERT_EXPR
4890 || TREE_CODE (first) == NON_LVALUE_EXPR)
4891 && (TYPE_MODE (TREE_TYPE (first))
4892 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4893 first = TREE_OPERAND (first, 0);
4896 while ((TREE_CODE (last) == NOP_EXPR
4897 || TREE_CODE (last) == CONVERT_EXPR
4898 || TREE_CODE (last) == NON_LVALUE_EXPR)
4899 && (TYPE_MODE (TREE_TYPE (last))
4900 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4901 last = TREE_OPERAND (last, 0);
4903 if (TREE_CODE (first) != INTEGER_CST)
4904 error_init ("nonconstant array index in initializer");
4905 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4906 error_init ("nonconstant array index in initializer");
4907 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4908 error_init ("array index in non-array initializer");
4909 else if (tree_int_cst_sgn (first) == -1)
4910 error_init ("array index in initializer exceeds array bounds");
4911 else if (constructor_max_index
4912 && tree_int_cst_lt (constructor_max_index, first))
4913 error_init ("array index in initializer exceeds array bounds");
4916 constructor_index = convert (bitsizetype, first);
4920 if (tree_int_cst_equal (first, last))
4922 else if (tree_int_cst_lt (last, first))
4924 error_init ("empty index range in initializer");
4929 last = convert (bitsizetype, last);
4930 if (constructor_max_index != 0
4931 && tree_int_cst_lt (constructor_max_index, last))
4933 error_init ("array index range in initializer exceeds array bounds");
4940 designator_errorneous = 0;
4941 if (constructor_range_stack || last)
4942 push_range_stack (last);
4946 /* Within a struct initializer, specify the next field to be initialized. */
4949 set_init_label (tree fieldname)
4953 if (set_designator (0))
4956 designator_errorneous = 1;
4958 if (TREE_CODE (constructor_type) != RECORD_TYPE
4959 && TREE_CODE (constructor_type) != UNION_TYPE)
4961 error_init ("field name not in record or union initializer");
4965 for (tail = TYPE_FIELDS (constructor_type); tail;
4966 tail = TREE_CHAIN (tail))
4968 if (DECL_NAME (tail) == fieldname)
4973 error ("unknown field `%s' specified in initializer",
4974 IDENTIFIER_POINTER (fieldname));
4977 constructor_fields = tail;
4979 designator_errorneous = 0;
4980 if (constructor_range_stack)
4981 push_range_stack (NULL_TREE);
4985 /* Add a new initializer to the tree of pending initializers. PURPOSE
4986 identifies the initializer, either array index or field in a structure.
4987 VALUE is the value of that index or field. */
4990 add_pending_init (tree purpose, tree value)
4992 struct init_node *p, **q, *r;
4994 q = &constructor_pending_elts;
4997 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5002 if (tree_int_cst_lt (purpose, p->purpose))
5004 else if (tree_int_cst_lt (p->purpose, purpose))
5008 if (TREE_SIDE_EFFECTS (p->value))
5009 warning_init ("initialized field with side-effects overwritten");
5019 bitpos = bit_position (purpose);
5023 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5025 else if (p->purpose != purpose)
5029 if (TREE_SIDE_EFFECTS (p->value))
5030 warning_init ("initialized field with side-effects overwritten");
5037 r = ggc_alloc (sizeof (struct init_node));
5038 r->purpose = purpose;
5049 struct init_node *s;
5053 if (p->balance == 0)
5055 else if (p->balance < 0)
5062 p->left->parent = p;
5079 constructor_pending_elts = r;
5084 struct init_node *t = r->right;
5088 r->right->parent = r;
5093 p->left->parent = p;
5096 p->balance = t->balance < 0;
5097 r->balance = -(t->balance > 0);
5112 constructor_pending_elts = t;
5118 /* p->balance == +1; growth of left side balances the node. */
5123 else /* r == p->right */
5125 if (p->balance == 0)
5126 /* Growth propagation from right side. */
5128 else if (p->balance > 0)
5135 p->right->parent = p;
5152 constructor_pending_elts = r;
5154 else /* r->balance == -1 */
5157 struct init_node *t = r->left;
5161 r->left->parent = r;
5166 p->right->parent = p;
5169 r->balance = (t->balance < 0);
5170 p->balance = -(t->balance > 0);
5185 constructor_pending_elts = t;
5191 /* p->balance == -1; growth of right side balances the node. */
5202 /* Build AVL tree from a sorted chain. */
5205 set_nonincremental_init (void)
5209 if (TREE_CODE (constructor_type) != RECORD_TYPE
5210 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5213 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5214 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5215 constructor_elements = 0;
5216 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5218 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5219 /* Skip any nameless bit fields at the beginning. */
5220 while (constructor_unfilled_fields != 0
5221 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5222 && DECL_NAME (constructor_unfilled_fields) == 0)
5223 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5226 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5228 if (TYPE_DOMAIN (constructor_type))
5229 constructor_unfilled_index
5230 = convert (bitsizetype,
5231 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5233 constructor_unfilled_index = bitsize_zero_node;
5235 constructor_incremental = 0;
5238 /* Build AVL tree from a string constant. */
5241 set_nonincremental_init_from_string (tree str)
5243 tree value, purpose, type;
5244 HOST_WIDE_INT val[2];
5245 const char *p, *end;
5246 int byte, wchar_bytes, charwidth, bitpos;
5248 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5251 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5252 == TYPE_PRECISION (char_type_node))
5254 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5255 == TYPE_PRECISION (wchar_type_node))
5256 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5260 charwidth = TYPE_PRECISION (char_type_node);
5261 type = TREE_TYPE (constructor_type);
5262 p = TREE_STRING_POINTER (str);
5263 end = p + TREE_STRING_LENGTH (str);
5265 for (purpose = bitsize_zero_node;
5266 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5267 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5269 if (wchar_bytes == 1)
5271 val[1] = (unsigned char) *p++;
5278 for (byte = 0; byte < wchar_bytes; byte++)
5280 if (BYTES_BIG_ENDIAN)
5281 bitpos = (wchar_bytes - byte - 1) * charwidth;
5283 bitpos = byte * charwidth;
5284 val[bitpos < HOST_BITS_PER_WIDE_INT]
5285 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5286 << (bitpos % HOST_BITS_PER_WIDE_INT);
5290 if (!TREE_UNSIGNED (type))
5292 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5293 if (bitpos < HOST_BITS_PER_WIDE_INT)
5295 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5297 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5301 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5306 else if (val[0] & (((HOST_WIDE_INT) 1)
5307 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5308 val[0] |= ((HOST_WIDE_INT) -1)
5309 << (bitpos - HOST_BITS_PER_WIDE_INT);
5312 value = build_int_2 (val[1], val[0]);
5313 TREE_TYPE (value) = type;
5314 add_pending_init (purpose, value);
5317 constructor_incremental = 0;
5320 /* Return value of FIELD in pending initializer or zero if the field was
5321 not initialized yet. */
5324 find_init_member (tree field)
5326 struct init_node *p;
5328 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5330 if (constructor_incremental
5331 && tree_int_cst_lt (field, constructor_unfilled_index))
5332 set_nonincremental_init ();
5334 p = constructor_pending_elts;
5337 if (tree_int_cst_lt (field, p->purpose))
5339 else if (tree_int_cst_lt (p->purpose, field))
5345 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5347 tree bitpos = bit_position (field);
5349 if (constructor_incremental
5350 && (!constructor_unfilled_fields
5351 || tree_int_cst_lt (bitpos,
5352 bit_position (constructor_unfilled_fields))))
5353 set_nonincremental_init ();
5355 p = constructor_pending_elts;
5358 if (field == p->purpose)
5360 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5366 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5368 if (constructor_elements
5369 && TREE_PURPOSE (constructor_elements) == field)
5370 return TREE_VALUE (constructor_elements);
5375 /* "Output" the next constructor element.
5376 At top level, really output it to assembler code now.
5377 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5378 TYPE is the data type that the containing data type wants here.
5379 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5381 PENDING if non-nil means output pending elements that belong
5382 right after this element. (PENDING is normally 1;
5383 it is 0 while outputting pending elements, to avoid recursion.) */
5386 output_init_element (tree value, tree type, tree field, int pending)
5388 if (type == error_mark_node)
5390 constructor_erroneous = 1;
5393 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5394 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5395 && !(TREE_CODE (value) == STRING_CST
5396 && TREE_CODE (type) == ARRAY_TYPE
5397 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5398 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5399 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5400 value = default_conversion (value);
5402 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5403 && require_constant_value && !flag_isoc99 && pending)
5405 /* As an extension, allow initializing objects with static storage
5406 duration with compound literals (which are then treated just as
5407 the brace enclosed list they contain). */
5408 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5409 value = DECL_INITIAL (decl);
5412 if (value == error_mark_node)
5413 constructor_erroneous = 1;
5414 else if (!TREE_CONSTANT (value))
5415 constructor_constant = 0;
5416 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5417 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5418 || TREE_CODE (constructor_type) == UNION_TYPE)
5419 && DECL_C_BIT_FIELD (field)
5420 && TREE_CODE (value) != INTEGER_CST))
5421 constructor_simple = 0;
5423 if (require_constant_value && ! TREE_CONSTANT (value))
5425 error_init ("initializer element is not constant");
5426 value = error_mark_node;
5428 else if (require_constant_elements
5429 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5430 pedwarn ("initializer element is not computable at load time");
5432 /* If this field is empty (and not at the end of structure),
5433 don't do anything other than checking the initializer. */
5435 && (TREE_TYPE (field) == error_mark_node
5436 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5437 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5438 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5439 || TREE_CHAIN (field)))))
5442 value = digest_init (type, value, require_constant_value);
5443 if (value == error_mark_node)
5445 constructor_erroneous = 1;
5449 /* If this element doesn't come next in sequence,
5450 put it on constructor_pending_elts. */
5451 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5452 && (!constructor_incremental
5453 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5455 if (constructor_incremental
5456 && tree_int_cst_lt (field, constructor_unfilled_index))
5457 set_nonincremental_init ();
5459 add_pending_init (field, value);
5462 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5463 && (!constructor_incremental
5464 || field != constructor_unfilled_fields))
5466 /* We do this for records but not for unions. In a union,
5467 no matter which field is specified, it can be initialized
5468 right away since it starts at the beginning of the union. */
5469 if (constructor_incremental)
5471 if (!constructor_unfilled_fields)
5472 set_nonincremental_init ();
5475 tree bitpos, unfillpos;
5477 bitpos = bit_position (field);
5478 unfillpos = bit_position (constructor_unfilled_fields);
5480 if (tree_int_cst_lt (bitpos, unfillpos))
5481 set_nonincremental_init ();
5485 add_pending_init (field, value);
5488 else if (TREE_CODE (constructor_type) == UNION_TYPE
5489 && constructor_elements)
5491 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5492 warning_init ("initialized field with side-effects overwritten");
5494 /* We can have just one union field set. */
5495 constructor_elements = 0;
5498 /* Otherwise, output this element either to
5499 constructor_elements or to the assembler file. */
5501 if (field && TREE_CODE (field) == INTEGER_CST)
5502 field = copy_node (field);
5503 constructor_elements
5504 = tree_cons (field, value, constructor_elements);
5506 /* Advance the variable that indicates sequential elements output. */
5507 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5508 constructor_unfilled_index
5509 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5511 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5513 constructor_unfilled_fields
5514 = TREE_CHAIN (constructor_unfilled_fields);
5516 /* Skip any nameless bit fields. */
5517 while (constructor_unfilled_fields != 0
5518 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5519 && DECL_NAME (constructor_unfilled_fields) == 0)
5520 constructor_unfilled_fields =
5521 TREE_CHAIN (constructor_unfilled_fields);
5523 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5524 constructor_unfilled_fields = 0;
5526 /* Now output any pending elements which have become next. */
5528 output_pending_init_elements (0);
5531 /* Output any pending elements which have become next.
5532 As we output elements, constructor_unfilled_{fields,index}
5533 advances, which may cause other elements to become next;
5534 if so, they too are output.
5536 If ALL is 0, we return when there are
5537 no more pending elements to output now.
5539 If ALL is 1, we output space as necessary so that
5540 we can output all the pending elements. */
5543 output_pending_init_elements (int all)
5545 struct init_node *elt = constructor_pending_elts;
5550 /* Look through the whole pending tree.
5551 If we find an element that should be output now,
5552 output it. Otherwise, set NEXT to the element
5553 that comes first among those still pending. */
5558 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5560 if (tree_int_cst_equal (elt->purpose,
5561 constructor_unfilled_index))
5562 output_init_element (elt->value,
5563 TREE_TYPE (constructor_type),
5564 constructor_unfilled_index, 0);
5565 else if (tree_int_cst_lt (constructor_unfilled_index,
5568 /* Advance to the next smaller node. */
5573 /* We have reached the smallest node bigger than the
5574 current unfilled index. Fill the space first. */
5575 next = elt->purpose;
5581 /* Advance to the next bigger node. */
5586 /* We have reached the biggest node in a subtree. Find
5587 the parent of it, which is the next bigger node. */
5588 while (elt->parent && elt->parent->right == elt)
5591 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5594 next = elt->purpose;
5600 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5601 || TREE_CODE (constructor_type) == UNION_TYPE)
5603 tree ctor_unfilled_bitpos, elt_bitpos;
5605 /* If the current record is complete we are done. */
5606 if (constructor_unfilled_fields == 0)
5609 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5610 elt_bitpos = bit_position (elt->purpose);
5611 /* We can't compare fields here because there might be empty
5612 fields in between. */
5613 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5615 constructor_unfilled_fields = elt->purpose;
5616 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5619 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5621 /* Advance to the next smaller node. */
5626 /* We have reached the smallest node bigger than the
5627 current unfilled field. Fill the space first. */
5628 next = elt->purpose;
5634 /* Advance to the next bigger node. */
5639 /* We have reached the biggest node in a subtree. Find
5640 the parent of it, which is the next bigger node. */
5641 while (elt->parent && elt->parent->right == elt)
5645 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5646 bit_position (elt->purpose))))
5648 next = elt->purpose;
5656 /* Ordinarily return, but not if we want to output all
5657 and there are elements left. */
5658 if (! (all && next != 0))
5661 /* If it's not incremental, just skip over the gap, so that after
5662 jumping to retry we will output the next successive element. */
5663 if (TREE_CODE (constructor_type) == RECORD_TYPE
5664 || TREE_CODE (constructor_type) == UNION_TYPE)
5665 constructor_unfilled_fields = next;
5666 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5667 constructor_unfilled_index = next;
5669 /* ELT now points to the node in the pending tree with the next
5670 initializer to output. */
5674 /* Add one non-braced element to the current constructor level.
5675 This adjusts the current position within the constructor's type.
5676 This may also start or terminate implicit levels
5677 to handle a partly-braced initializer.
5679 Once this has found the correct level for the new element,
5680 it calls output_init_element. */
5683 process_init_element (tree value)
5685 tree orig_value = value;
5686 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5688 designator_depth = 0;
5689 designator_errorneous = 0;
5691 /* Handle superfluous braces around string cst as in
5692 char x[] = {"foo"}; */
5695 && TREE_CODE (constructor_type) == ARRAY_TYPE
5696 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5697 && integer_zerop (constructor_unfilled_index))
5699 if (constructor_stack->replacement_value)
5700 error_init ("excess elements in char array initializer");
5701 constructor_stack->replacement_value = value;
5705 if (constructor_stack->replacement_value != 0)
5707 error_init ("excess elements in struct initializer");
5711 /* Ignore elements of a brace group if it is entirely superfluous
5712 and has already been diagnosed. */
5713 if (constructor_type == 0)
5716 /* If we've exhausted any levels that didn't have braces,
5718 while (constructor_stack->implicit)
5720 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5721 || TREE_CODE (constructor_type) == UNION_TYPE)
5722 && constructor_fields == 0)
5723 process_init_element (pop_init_level (1));
5724 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5725 && (constructor_max_index == 0
5726 || tree_int_cst_lt (constructor_max_index,
5727 constructor_index)))
5728 process_init_element (pop_init_level (1));
5733 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5734 if (constructor_range_stack)
5736 /* If value is a compound literal and we'll be just using its
5737 content, don't put it into a SAVE_EXPR. */
5738 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5739 || !require_constant_value
5741 value = save_expr (value);
5746 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5749 enum tree_code fieldcode;
5751 if (constructor_fields == 0)
5753 pedwarn_init ("excess elements in struct initializer");
5757 fieldtype = TREE_TYPE (constructor_fields);
5758 if (fieldtype != error_mark_node)
5759 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5760 fieldcode = TREE_CODE (fieldtype);
5762 /* Error for non-static initialization of a flexible array member. */
5763 if (fieldcode == ARRAY_TYPE
5764 && !require_constant_value
5765 && TYPE_SIZE (fieldtype) == NULL_TREE
5766 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5768 error_init ("non-static initialization of a flexible array member");
5772 /* Accept a string constant to initialize a subarray. */
5774 && fieldcode == ARRAY_TYPE
5775 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5778 /* Otherwise, if we have come to a subaggregate,
5779 and we don't have an element of its type, push into it. */
5780 else if (value != 0 && !constructor_no_implicit
5781 && value != error_mark_node
5782 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5783 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5784 || fieldcode == UNION_TYPE))
5786 push_init_level (1);
5792 push_member_name (constructor_fields);
5793 output_init_element (value, fieldtype, constructor_fields, 1);
5794 RESTORE_SPELLING_DEPTH (constructor_depth);
5797 /* Do the bookkeeping for an element that was
5798 directly output as a constructor. */
5800 /* For a record, keep track of end position of last field. */
5801 if (DECL_SIZE (constructor_fields))
5802 constructor_bit_index
5803 = size_binop (PLUS_EXPR,
5804 bit_position (constructor_fields),
5805 DECL_SIZE (constructor_fields));
5807 /* If the current field was the first one not yet written out,
5808 it isn't now, so update. */
5809 if (constructor_unfilled_fields == constructor_fields)
5811 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5812 /* Skip any nameless bit fields. */
5813 while (constructor_unfilled_fields != 0
5814 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5815 && DECL_NAME (constructor_unfilled_fields) == 0)
5816 constructor_unfilled_fields =
5817 TREE_CHAIN (constructor_unfilled_fields);
5821 constructor_fields = TREE_CHAIN (constructor_fields);
5822 /* Skip any nameless bit fields at the beginning. */
5823 while (constructor_fields != 0
5824 && DECL_C_BIT_FIELD (constructor_fields)
5825 && DECL_NAME (constructor_fields) == 0)
5826 constructor_fields = TREE_CHAIN (constructor_fields);
5828 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5831 enum tree_code fieldcode;
5833 if (constructor_fields == 0)
5835 pedwarn_init ("excess elements in union initializer");
5839 fieldtype = TREE_TYPE (constructor_fields);
5840 if (fieldtype != error_mark_node)
5841 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5842 fieldcode = TREE_CODE (fieldtype);
5844 /* Warn that traditional C rejects initialization of unions.
5845 We skip the warning if the value is zero. This is done
5846 under the assumption that the zero initializer in user
5847 code appears conditioned on e.g. __STDC__ to avoid
5848 "missing initializer" warnings and relies on default
5849 initialization to zero in the traditional C case.
5850 We also skip the warning if the initializer is designated,
5851 again on the assumption that this must be conditional on
5852 __STDC__ anyway (and we've already complained about the
5853 member-designator already). */
5854 if (warn_traditional && !in_system_header && !constructor_designated
5855 && !(value && (integer_zerop (value) || real_zerop (value))))
5856 warning ("traditional C rejects initialization of unions");
5858 /* Accept a string constant to initialize a subarray. */
5860 && fieldcode == ARRAY_TYPE
5861 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5864 /* Otherwise, if we have come to a subaggregate,
5865 and we don't have an element of its type, push into it. */
5866 else if (value != 0 && !constructor_no_implicit
5867 && value != error_mark_node
5868 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5869 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5870 || fieldcode == UNION_TYPE))
5872 push_init_level (1);
5878 push_member_name (constructor_fields);
5879 output_init_element (value, fieldtype, constructor_fields, 1);
5880 RESTORE_SPELLING_DEPTH (constructor_depth);
5883 /* Do the bookkeeping for an element that was
5884 directly output as a constructor. */
5886 constructor_bit_index = DECL_SIZE (constructor_fields);
5887 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5890 constructor_fields = 0;
5892 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5894 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5895 enum tree_code eltcode = TREE_CODE (elttype);
5897 /* Accept a string constant to initialize a subarray. */
5899 && eltcode == ARRAY_TYPE
5900 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5903 /* Otherwise, if we have come to a subaggregate,
5904 and we don't have an element of its type, push into it. */
5905 else if (value != 0 && !constructor_no_implicit
5906 && value != error_mark_node
5907 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5908 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5909 || eltcode == UNION_TYPE))
5911 push_init_level (1);
5915 if (constructor_max_index != 0
5916 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5917 || integer_all_onesp (constructor_max_index)))
5919 pedwarn_init ("excess elements in array initializer");
5923 /* Now output the actual element. */
5926 push_array_bounds (tree_low_cst (constructor_index, 0));
5927 output_init_element (value, elttype, constructor_index, 1);
5928 RESTORE_SPELLING_DEPTH (constructor_depth);
5932 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5935 /* If we are doing the bookkeeping for an element that was
5936 directly output as a constructor, we must update
5937 constructor_unfilled_index. */
5938 constructor_unfilled_index = constructor_index;
5940 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5942 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5944 /* Do a basic check of initializer size. Note that vectors
5945 always have a fixed size derived from their type. */
5946 if (tree_int_cst_lt (constructor_max_index, constructor_index))
5948 pedwarn_init ("excess elements in vector initializer");
5952 /* Now output the actual element. */
5954 output_init_element (value, elttype, constructor_index, 1);
5957 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5960 /* If we are doing the bookkeeping for an element that was
5961 directly output as a constructor, we must update
5962 constructor_unfilled_index. */
5963 constructor_unfilled_index = constructor_index;
5966 /* Handle the sole element allowed in a braced initializer
5967 for a scalar variable. */
5968 else if (constructor_fields == 0)
5970 pedwarn_init ("excess elements in scalar initializer");
5976 output_init_element (value, constructor_type, NULL_TREE, 1);
5977 constructor_fields = 0;
5980 /* Handle range initializers either at this level or anywhere higher
5981 in the designator stack. */
5982 if (constructor_range_stack)
5984 struct constructor_range_stack *p, *range_stack;
5987 range_stack = constructor_range_stack;
5988 constructor_range_stack = 0;
5989 while (constructor_stack != range_stack->stack)
5991 if (!constructor_stack->implicit)
5993 process_init_element (pop_init_level (1));
5995 for (p = range_stack;
5996 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
5999 if (!constructor_stack->implicit)
6001 process_init_element (pop_init_level (1));
6004 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6005 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6010 constructor_index = p->index;
6011 constructor_fields = p->fields;
6012 if (finish && p->range_end && p->index == p->range_start)
6020 push_init_level (2);
6021 p->stack = constructor_stack;
6022 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6023 p->index = p->range_start;
6027 constructor_range_stack = range_stack;
6034 constructor_range_stack = 0;
6037 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6038 (guaranteed to be 'volatile' or null) and ARGS (represented using
6039 an ASM_STMT node). */
6041 build_asm_stmt (tree cv_qualifier, tree args)
6043 if (!TREE_OPERAND (args, 0))
6044 TREE_OPERAND (args, 0) = cv_qualifier;
6045 return add_stmt (args);
6048 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6049 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6050 SIMPLE indicates whether there was anything at all after the
6051 string in the asm expression -- asm("blah") and asm("blah" : )
6052 are subtly different. We use a ASM_STMT node to represent this. */
6054 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6060 /* We can remove output conversions that change the type,
6061 but not the mode. */
6062 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6064 tree output = TREE_VALUE (tail);
6066 STRIP_NOPS (output);
6067 TREE_VALUE (tail) = output;
6069 /* Allow conversions as LHS here. build_modify_expr as called below
6070 will do the right thing with them. */
6071 while (TREE_CODE (output) == NOP_EXPR
6072 || TREE_CODE (output) == CONVERT_EXPR
6073 || TREE_CODE (output) == FLOAT_EXPR
6074 || TREE_CODE (output) == FIX_TRUNC_EXPR
6075 || TREE_CODE (output) == FIX_FLOOR_EXPR
6076 || TREE_CODE (output) == FIX_ROUND_EXPR
6077 || TREE_CODE (output) == FIX_CEIL_EXPR)
6078 output = TREE_OPERAND (output, 0);
6080 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6083 /* Remove output conversions that change the type but not the mode. */
6084 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6086 tree output = TREE_VALUE (tail);
6087 STRIP_NOPS (output);
6088 TREE_VALUE (tail) = output;
6091 /* Perform default conversions on array and function inputs.
6092 Don't do this for other types as it would screw up operands
6093 expected to be in memory. */
6094 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6095 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6097 args = build_stmt (ASM_STMT, 0, string, outputs, inputs, clobbers);
6099 /* Simple asm statements are treated as volatile. */
6102 TREE_OPERAND (args, 0) = ridpointers[RID_VOLATILE];
6103 ASM_INPUT_P (args) = 1;
6108 /* Expand an ASM statement with operands, handling output operands
6109 that are not variables or INDIRECT_REFS by transforming such
6110 cases into cases that expand_asm_operands can handle.
6112 Arguments are same as for expand_asm_operands. */
6115 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6116 tree clobbers, int vol, location_t locus)
6118 int noutputs = list_length (outputs);
6120 /* o[I] is the place that output number I should be written. */
6121 tree *o = alloca (noutputs * sizeof (tree));
6124 /* Record the contents of OUTPUTS before it is modified. */
6125 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6127 o[i] = TREE_VALUE (tail);
6128 if (o[i] == error_mark_node)
6132 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6133 OUTPUTS some trees for where the values were actually stored. */
6134 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
6136 /* Copy all the intermediate outputs into the specified outputs. */
6137 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6139 if (o[i] != TREE_VALUE (tail))
6141 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6142 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6145 /* Restore the original value so that it's correct the next
6146 time we expand this function. */
6147 TREE_VALUE (tail) = o[i];
6149 /* Detect modification of read-only values.
6150 (Otherwise done by build_modify_expr.) */
6153 tree type = TREE_TYPE (o[i]);
6154 if (TREE_READONLY (o[i])
6155 || TYPE_READONLY (type)
6156 || ((TREE_CODE (type) == RECORD_TYPE
6157 || TREE_CODE (type) == UNION_TYPE)
6158 && C_TYPE_FIELDS_READONLY (type)))
6159 readonly_error (o[i], "modification by `asm'");
6163 /* Those MODIFY_EXPRs could do autoincrements. */
6167 /* Expand a C `return' statement.
6168 RETVAL is the expression for what to return,
6169 or a null pointer for `return;' with no value. */
6172 c_expand_return (tree retval)
6174 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6176 if (TREE_THIS_VOLATILE (current_function_decl))
6177 warning ("function declared `noreturn' has a `return' statement");
6181 current_function_returns_null = 1;
6182 if ((warn_return_type || flag_isoc99)
6183 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6184 pedwarn_c99 ("`return' with no value, in function returning non-void");
6186 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6188 current_function_returns_null = 1;
6189 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6190 pedwarn ("`return' with a value, in function returning void");
6194 tree t = convert_for_assignment (valtype, retval, _("return"),
6195 NULL_TREE, NULL_TREE, 0);
6196 tree res = DECL_RESULT (current_function_decl);
6199 current_function_returns_value = 1;
6200 if (t == error_mark_node)
6203 inner = t = convert (TREE_TYPE (res), t);
6205 /* Strip any conversions, additions, and subtractions, and see if
6206 we are returning the address of a local variable. Warn if so. */
6209 switch (TREE_CODE (inner))
6211 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6213 inner = TREE_OPERAND (inner, 0);
6217 /* If the second operand of the MINUS_EXPR has a pointer
6218 type (or is converted from it), this may be valid, so
6219 don't give a warning. */
6221 tree op1 = TREE_OPERAND (inner, 1);
6223 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6224 && (TREE_CODE (op1) == NOP_EXPR
6225 || TREE_CODE (op1) == NON_LVALUE_EXPR
6226 || TREE_CODE (op1) == CONVERT_EXPR))
6227 op1 = TREE_OPERAND (op1, 0);
6229 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6232 inner = TREE_OPERAND (inner, 0);
6237 inner = TREE_OPERAND (inner, 0);
6239 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6240 inner = TREE_OPERAND (inner, 0);
6243 && ! DECL_EXTERNAL (inner)
6244 && ! TREE_STATIC (inner)
6245 && DECL_CONTEXT (inner) == current_function_decl)
6246 warning ("function returns address of local variable");
6256 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6259 return add_stmt (build_return_stmt (retval));
6263 /* The SWITCH_STMT being built. */
6265 /* A splay-tree mapping the low element of a case range to the high
6266 element, or NULL_TREE if there is no high element. Used to
6267 determine whether or not a new case label duplicates an old case
6268 label. We need a tree, rather than simply a hash table, because
6269 of the GNU case range extension. */
6271 /* The next node on the stack. */
6272 struct c_switch *next;
6275 /* A stack of the currently active switch statements. The innermost
6276 switch statement is on the top of the stack. There is no need to
6277 mark the stack for garbage collection because it is only active
6278 during the processing of the body of a function, and we never
6279 collect at that point. */
6281 static struct c_switch *switch_stack;
6283 /* Start a C switch statement, testing expression EXP. Return the new
6287 c_start_case (tree exp)
6289 enum tree_code code;
6290 tree type, orig_type = error_mark_node;
6291 struct c_switch *cs;
6293 if (exp != error_mark_node)
6295 code = TREE_CODE (TREE_TYPE (exp));
6296 orig_type = TREE_TYPE (exp);
6298 if (! INTEGRAL_TYPE_P (orig_type)
6299 && code != ERROR_MARK)
6301 error ("switch quantity not an integer");
6302 exp = integer_zero_node;
6306 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6308 if (warn_traditional && !in_system_header
6309 && (type == long_integer_type_node
6310 || type == long_unsigned_type_node))
6311 warning ("`long' switch expression not converted to `int' in ISO C");
6313 exp = default_conversion (exp);
6314 type = TREE_TYPE (exp);
6318 /* Add this new SWITCH_STMT to the stack. */
6319 cs = xmalloc (sizeof (*cs));
6320 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6321 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6322 cs->next = switch_stack;
6325 return add_stmt (switch_stack->switch_stmt);
6328 /* Process a case label. */
6331 do_case (tree low_value, tree high_value)
6333 tree label = NULL_TREE;
6337 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6339 label = c_add_case_label (switch_stack->cases,
6340 SWITCH_COND (switch_stack->switch_stmt),
6341 low_value, high_value);
6342 if (label == error_mark_node)
6344 else if (switch_was_empty_p)
6346 /* Attach the first case label to the SWITCH_BODY. */
6347 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6348 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6352 error ("case label not within a switch statement");
6354 error ("`default' label not within a switch statement");
6359 /* Finish the switch statement. */
6362 c_finish_case (void)
6364 struct c_switch *cs = switch_stack;
6366 /* Rechain the next statements to the SWITCH_STMT. */
6367 last_tree = cs->switch_stmt;
6369 /* Pop the stack. */
6370 switch_stack = switch_stack->next;
6371 splay_tree_delete (cs->cases);
6375 /* Build a binary-operation expression without default conversions.
6376 CODE is the kind of expression to build.
6377 This function differs from `build' in several ways:
6378 the data type of the result is computed and recorded in it,
6379 warnings are generated if arg data types are invalid,
6380 special handling for addition and subtraction of pointers is known,
6381 and some optimization is done (operations on narrow ints
6382 are done in the narrower type when that gives the same result).
6383 Constant folding is also done before the result is returned.
6385 Note that the operands will never have enumeral types, or function
6386 or array types, because either they will have the default conversions
6387 performed or they have both just been converted to some other type in which
6388 the arithmetic is to be done. */
6391 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6395 enum tree_code code0, code1;
6398 /* Expression code to give to the expression when it is built.
6399 Normally this is CODE, which is what the caller asked for,
6400 but in some special cases we change it. */
6401 enum tree_code resultcode = code;
6403 /* Data type in which the computation is to be performed.
6404 In the simplest cases this is the common type of the arguments. */
6405 tree result_type = NULL;
6407 /* Nonzero means operands have already been type-converted
6408 in whatever way is necessary.
6409 Zero means they need to be converted to RESULT_TYPE. */
6412 /* Nonzero means create the expression with this type, rather than
6414 tree build_type = 0;
6416 /* Nonzero means after finally constructing the expression
6417 convert it to this type. */
6418 tree final_type = 0;
6420 /* Nonzero if this is an operation like MIN or MAX which can
6421 safely be computed in short if both args are promoted shorts.
6422 Also implies COMMON.
6423 -1 indicates a bitwise operation; this makes a difference
6424 in the exact conditions for when it is safe to do the operation
6425 in a narrower mode. */
6428 /* Nonzero if this is a comparison operation;
6429 if both args are promoted shorts, compare the original shorts.
6430 Also implies COMMON. */
6431 int short_compare = 0;
6433 /* Nonzero if this is a right-shift operation, which can be computed on the
6434 original short and then promoted if the operand is a promoted short. */
6435 int short_shift = 0;
6437 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6442 op0 = default_conversion (orig_op0);
6443 op1 = default_conversion (orig_op1);
6451 type0 = TREE_TYPE (op0);
6452 type1 = TREE_TYPE (op1);
6454 /* The expression codes of the data types of the arguments tell us
6455 whether the arguments are integers, floating, pointers, etc. */
6456 code0 = TREE_CODE (type0);
6457 code1 = TREE_CODE (type1);
6459 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6460 STRIP_TYPE_NOPS (op0);
6461 STRIP_TYPE_NOPS (op1);
6463 /* If an error was already reported for one of the arguments,
6464 avoid reporting another error. */
6466 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6467 return error_mark_node;
6472 /* Handle the pointer + int case. */
6473 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6474 return pointer_int_sum (PLUS_EXPR, op0, op1);
6475 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6476 return pointer_int_sum (PLUS_EXPR, op1, op0);
6482 /* Subtraction of two similar pointers.
6483 We must subtract them as integers, then divide by object size. */
6484 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6485 && comp_target_types (type0, type1, 1))
6486 return pointer_diff (op0, op1);
6487 /* Handle pointer minus int. Just like pointer plus int. */
6488 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6489 return pointer_int_sum (MINUS_EXPR, op0, op1);
6498 case TRUNC_DIV_EXPR:
6500 case FLOOR_DIV_EXPR:
6501 case ROUND_DIV_EXPR:
6502 case EXACT_DIV_EXPR:
6503 /* Floating point division by zero is a legitimate way to obtain
6504 infinities and NaNs. */
6505 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6506 warning ("division by zero");
6508 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6509 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6510 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6511 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6513 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6514 resultcode = RDIV_EXPR;
6516 /* Although it would be tempting to shorten always here, that
6517 loses on some targets, since the modulo instruction is
6518 undefined if the quotient can't be represented in the
6519 computation mode. We shorten only if unsigned or if
6520 dividing by something we know != -1. */
6521 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6522 || (TREE_CODE (op1) == INTEGER_CST
6523 && ! integer_all_onesp (op1)));
6531 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6533 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6537 case TRUNC_MOD_EXPR:
6538 case FLOOR_MOD_EXPR:
6539 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6540 warning ("division by zero");
6542 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6544 /* Although it would be tempting to shorten always here, that loses
6545 on some targets, since the modulo instruction is undefined if the
6546 quotient can't be represented in the computation mode. We shorten
6547 only if unsigned or if dividing by something we know != -1. */
6548 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6549 || (TREE_CODE (op1) == INTEGER_CST
6550 && ! integer_all_onesp (op1)));
6555 case TRUTH_ANDIF_EXPR:
6556 case TRUTH_ORIF_EXPR:
6557 case TRUTH_AND_EXPR:
6559 case TRUTH_XOR_EXPR:
6560 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6561 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6562 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6563 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6565 /* Result of these operations is always an int,
6566 but that does not mean the operands should be
6567 converted to ints! */
6568 result_type = integer_type_node;
6569 op0 = (*lang_hooks.truthvalue_conversion) (op0);
6570 op1 = (*lang_hooks.truthvalue_conversion) (op1);
6575 /* Shift operations: result has same type as first operand;
6576 always convert second operand to int.
6577 Also set SHORT_SHIFT if shifting rightward. */
6580 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6582 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6584 if (tree_int_cst_sgn (op1) < 0)
6585 warning ("right shift count is negative");
6588 if (! integer_zerop (op1))
6591 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6592 warning ("right shift count >= width of type");
6596 /* Use the type of the value to be shifted. */
6597 result_type = type0;
6598 /* Convert the shift-count to an integer, regardless of size
6599 of value being shifted. */
6600 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6601 op1 = convert (integer_type_node, op1);
6602 /* Avoid converting op1 to result_type later. */
6608 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6610 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6612 if (tree_int_cst_sgn (op1) < 0)
6613 warning ("left shift count is negative");
6615 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6616 warning ("left shift count >= width of type");
6619 /* Use the type of the value to be shifted. */
6620 result_type = type0;
6621 /* Convert the shift-count to an integer, regardless of size
6622 of value being shifted. */
6623 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6624 op1 = convert (integer_type_node, op1);
6625 /* Avoid converting op1 to result_type later. */
6632 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6634 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6636 if (tree_int_cst_sgn (op1) < 0)
6637 warning ("shift count is negative");
6638 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6639 warning ("shift count >= width of type");
6642 /* Use the type of the value to be shifted. */
6643 result_type = type0;
6644 /* Convert the shift-count to an integer, regardless of size
6645 of value being shifted. */
6646 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6647 op1 = convert (integer_type_node, op1);
6648 /* Avoid converting op1 to result_type later. */
6655 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6656 warning ("comparing floating point with == or != is unsafe");
6657 /* Result of comparison is always int,
6658 but don't convert the args to int! */
6659 build_type = integer_type_node;
6660 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6661 || code0 == COMPLEX_TYPE
6662 || code0 == VECTOR_TYPE)
6663 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6664 || code1 == COMPLEX_TYPE
6665 || code1 == VECTOR_TYPE))
6667 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6669 tree tt0 = TREE_TYPE (type0);
6670 tree tt1 = TREE_TYPE (type1);
6671 /* Anything compares with void *. void * compares with anything.
6672 Otherwise, the targets must be compatible
6673 and both must be object or both incomplete. */
6674 if (comp_target_types (type0, type1, 1))
6675 result_type = common_type (type0, type1);
6676 else if (VOID_TYPE_P (tt0))
6678 /* op0 != orig_op0 detects the case of something
6679 whose value is 0 but which isn't a valid null ptr const. */
6680 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6681 && TREE_CODE (tt1) == FUNCTION_TYPE)
6682 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6684 else if (VOID_TYPE_P (tt1))
6686 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6687 && TREE_CODE (tt0) == FUNCTION_TYPE)
6688 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6691 pedwarn ("comparison of distinct pointer types lacks a cast");
6693 if (result_type == NULL_TREE)
6694 result_type = ptr_type_node;
6696 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6697 && integer_zerop (op1))
6698 result_type = type0;
6699 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6700 && integer_zerop (op0))
6701 result_type = type1;
6702 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6704 result_type = type0;
6705 pedwarn ("comparison between pointer and integer");
6707 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6709 result_type = type1;
6710 pedwarn ("comparison between pointer and integer");
6716 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6717 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6719 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6721 if (comp_target_types (type0, type1, 1))
6723 result_type = common_type (type0, type1);
6725 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6726 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6730 result_type = ptr_type_node;
6731 pedwarn ("comparison of distinct pointer types lacks a cast");
6740 build_type = integer_type_node;
6741 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6742 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6744 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6746 if (comp_target_types (type0, type1, 1))
6748 result_type = common_type (type0, type1);
6749 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6750 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6751 pedwarn ("comparison of complete and incomplete pointers");
6753 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6754 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6758 result_type = ptr_type_node;
6759 pedwarn ("comparison of distinct pointer types lacks a cast");
6762 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6763 && integer_zerop (op1))
6765 result_type = type0;
6766 if (pedantic || extra_warnings)
6767 pedwarn ("ordered comparison of pointer with integer zero");
6769 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6770 && integer_zerop (op0))
6772 result_type = type1;
6774 pedwarn ("ordered comparison of pointer with integer zero");
6776 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6778 result_type = type0;
6779 pedwarn ("comparison between pointer and integer");
6781 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6783 result_type = type1;
6784 pedwarn ("comparison between pointer and integer");
6788 case UNORDERED_EXPR:
6795 build_type = integer_type_node;
6796 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6798 error ("unordered comparison on non-floating point argument");
6799 return error_mark_node;
6808 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6809 return error_mark_node;
6811 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6812 || code0 == VECTOR_TYPE)
6814 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6815 || code1 == VECTOR_TYPE))
6817 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6819 if (shorten || common || short_compare)
6820 result_type = common_type (type0, type1);
6822 /* For certain operations (which identify themselves by shorten != 0)
6823 if both args were extended from the same smaller type,
6824 do the arithmetic in that type and then extend.
6826 shorten !=0 and !=1 indicates a bitwise operation.
6827 For them, this optimization is safe only if
6828 both args are zero-extended or both are sign-extended.
6829 Otherwise, we might change the result.
6830 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6831 but calculated in (unsigned short) it would be (unsigned short)-1. */
6833 if (shorten && none_complex)
6835 int unsigned0, unsigned1;
6836 tree arg0 = get_narrower (op0, &unsigned0);
6837 tree arg1 = get_narrower (op1, &unsigned1);
6838 /* UNS is 1 if the operation to be done is an unsigned one. */
6839 int uns = TREE_UNSIGNED (result_type);
6842 final_type = result_type;
6844 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6845 but it *requires* conversion to FINAL_TYPE. */
6847 if ((TYPE_PRECISION (TREE_TYPE (op0))
6848 == TYPE_PRECISION (TREE_TYPE (arg0)))
6849 && TREE_TYPE (op0) != final_type)
6850 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
6851 if ((TYPE_PRECISION (TREE_TYPE (op1))
6852 == TYPE_PRECISION (TREE_TYPE (arg1)))
6853 && TREE_TYPE (op1) != final_type)
6854 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
6856 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6858 /* For bitwise operations, signedness of nominal type
6859 does not matter. Consider only how operands were extended. */
6863 /* Note that in all three cases below we refrain from optimizing
6864 an unsigned operation on sign-extended args.
6865 That would not be valid. */
6867 /* Both args variable: if both extended in same way
6868 from same width, do it in that width.
6869 Do it unsigned if args were zero-extended. */
6870 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6871 < TYPE_PRECISION (result_type))
6872 && (TYPE_PRECISION (TREE_TYPE (arg1))
6873 == TYPE_PRECISION (TREE_TYPE (arg0)))
6874 && unsigned0 == unsigned1
6875 && (unsigned0 || !uns))
6877 = c_common_signed_or_unsigned_type
6878 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6879 else if (TREE_CODE (arg0) == INTEGER_CST
6880 && (unsigned1 || !uns)
6881 && (TYPE_PRECISION (TREE_TYPE (arg1))
6882 < TYPE_PRECISION (result_type))
6884 = c_common_signed_or_unsigned_type (unsigned1,
6886 int_fits_type_p (arg0, type)))
6888 else if (TREE_CODE (arg1) == INTEGER_CST
6889 && (unsigned0 || !uns)
6890 && (TYPE_PRECISION (TREE_TYPE (arg0))
6891 < TYPE_PRECISION (result_type))
6893 = c_common_signed_or_unsigned_type (unsigned0,
6895 int_fits_type_p (arg1, type)))
6899 /* Shifts can be shortened if shifting right. */
6904 tree arg0 = get_narrower (op0, &unsigned_arg);
6906 final_type = result_type;
6908 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6909 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
6911 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6912 /* We can shorten only if the shift count is less than the
6913 number of bits in the smaller type size. */
6914 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6915 /* We cannot drop an unsigned shift after sign-extension. */
6916 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
6918 /* Do an unsigned shift if the operand was zero-extended. */
6920 = c_common_signed_or_unsigned_type (unsigned_arg,
6922 /* Convert value-to-be-shifted to that type. */
6923 if (TREE_TYPE (op0) != result_type)
6924 op0 = convert (result_type, op0);
6929 /* Comparison operations are shortened too but differently.
6930 They identify themselves by setting short_compare = 1. */
6934 /* Don't write &op0, etc., because that would prevent op0
6935 from being kept in a register.
6936 Instead, make copies of the our local variables and
6937 pass the copies by reference, then copy them back afterward. */
6938 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
6939 enum tree_code xresultcode = resultcode;
6941 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
6946 op0 = xop0, op1 = xop1;
6948 resultcode = xresultcode;
6950 if (warn_sign_compare && skip_evaluation == 0)
6952 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
6953 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
6954 int unsignedp0, unsignedp1;
6955 tree primop0 = get_narrower (op0, &unsignedp0);
6956 tree primop1 = get_narrower (op1, &unsignedp1);
6960 STRIP_TYPE_NOPS (xop0);
6961 STRIP_TYPE_NOPS (xop1);
6963 /* Give warnings for comparisons between signed and unsigned
6964 quantities that may fail.
6966 Do the checking based on the original operand trees, so that
6967 casts will be considered, but default promotions won't be.
6969 Do not warn if the comparison is being done in a signed type,
6970 since the signed type will only be chosen if it can represent
6971 all the values of the unsigned type. */
6972 if (! TREE_UNSIGNED (result_type))
6974 /* Do not warn if both operands are the same signedness. */
6975 else if (op0_signed == op1_signed)
6982 sop = xop0, uop = xop1;
6984 sop = xop1, uop = xop0;
6986 /* Do not warn if the signed quantity is an
6987 unsuffixed integer literal (or some static
6988 constant expression involving such literals or a
6989 conditional expression involving such literals)
6990 and it is non-negative. */
6991 if (c_tree_expr_nonnegative_p (sop))
6993 /* Do not warn if the comparison is an equality operation,
6994 the unsigned quantity is an integral constant, and it
6995 would fit in the result if the result were signed. */
6996 else if (TREE_CODE (uop) == INTEGER_CST
6997 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
6999 (uop, c_common_signed_type (result_type)))
7001 /* Do not warn if the unsigned quantity is an enumeration
7002 constant and its maximum value would fit in the result
7003 if the result were signed. */
7004 else if (TREE_CODE (uop) == INTEGER_CST
7005 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7007 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7008 c_common_signed_type (result_type)))
7011 warning ("comparison between signed and unsigned");
7014 /* Warn if two unsigned values are being compared in a size
7015 larger than their original size, and one (and only one) is the
7016 result of a `~' operator. This comparison will always fail.
7018 Also warn if one operand is a constant, and the constant
7019 does not have all bits set that are set in the ~ operand
7020 when it is extended. */
7022 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7023 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7025 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7026 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7029 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7032 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7035 HOST_WIDE_INT constant, mask;
7036 int unsignedp, bits;
7038 if (host_integerp (primop0, 0))
7041 unsignedp = unsignedp1;
7042 constant = tree_low_cst (primop0, 0);
7047 unsignedp = unsignedp0;
7048 constant = tree_low_cst (primop1, 0);
7051 bits = TYPE_PRECISION (TREE_TYPE (primop));
7052 if (bits < TYPE_PRECISION (result_type)
7053 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7055 mask = (~ (HOST_WIDE_INT) 0) << bits;
7056 if ((mask & constant) != mask)
7057 warning ("comparison of promoted ~unsigned with constant");
7060 else if (unsignedp0 && unsignedp1
7061 && (TYPE_PRECISION (TREE_TYPE (primop0))
7062 < TYPE_PRECISION (result_type))
7063 && (TYPE_PRECISION (TREE_TYPE (primop1))
7064 < TYPE_PRECISION (result_type)))
7065 warning ("comparison of promoted ~unsigned with unsigned");
7071 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7072 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7073 Then the expression will be built.
7074 It will be given type FINAL_TYPE if that is nonzero;
7075 otherwise, it will be given type RESULT_TYPE. */
7079 binary_op_error (code);
7080 return error_mark_node;
7085 if (TREE_TYPE (op0) != result_type)
7086 op0 = convert (result_type, op0);
7087 if (TREE_TYPE (op1) != result_type)
7088 op1 = convert (result_type, op1);
7091 if (build_type == NULL_TREE)
7092 build_type = result_type;
7095 tree result = build (resultcode, build_type, op0, op1);
7098 /* Treat expressions in initializers specially as they can't trap. */
7099 folded = initializer_stack ? fold_initializer (result)
7101 if (folded == result)
7102 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7103 if (final_type != 0)
7104 return convert (final_type, folded);