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 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"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned;
52 static tree qualify_type (tree, tree);
53 static int tagged_types_tu_compatible_p (tree, tree, int);
54 static int comp_target_types (tree, tree, int);
55 static int function_types_compatible_p (tree, tree, int);
56 static int type_lists_compatible_p (tree, tree, int);
57 static tree decl_constant_value_for_broken_optimization (tree);
58 static tree default_function_array_conversion (tree);
59 static tree lookup_field (tree, tree);
60 static tree convert_arguments (tree, tree, tree, tree);
61 static tree pointer_diff (tree, tree);
62 static tree unary_complex_lvalue (enum tree_code, tree, int);
63 static void pedantic_lvalue_warning (enum tree_code);
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 /* Treat an enum type as the integer type of the same width and
479 if (TREE_CODE (t1) == ENUMERAL_TYPE)
480 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
481 if (TREE_CODE (t2) == ENUMERAL_TYPE)
482 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
487 /* Different classes of types can't be compatible. */
489 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
491 /* Qualifiers must match. */
493 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
496 /* Allow for two different type nodes which have essentially the same
497 definition. Note that we already checked for equality of the type
498 qualifiers (just above). */
500 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
504 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
507 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
510 switch (TREE_CODE (t1))
513 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
514 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
518 val = function_types_compatible_p (t1, t2, flags);
523 tree d1 = TYPE_DOMAIN (t1);
524 tree d2 = TYPE_DOMAIN (t2);
525 bool d1_variable, d2_variable;
526 bool d1_zero, d2_zero;
529 /* Target types must match incl. qualifiers. */
530 if (TREE_TYPE (t1) != TREE_TYPE (t2)
531 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
535 /* Sizes must match unless one is missing or variable. */
536 if (d1 == 0 || d2 == 0 || d1 == d2)
539 d1_zero = ! TYPE_MAX_VALUE (d1);
540 d2_zero = ! TYPE_MAX_VALUE (d2);
542 d1_variable = (! d1_zero
543 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
544 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
545 d2_variable = (! d2_zero
546 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
547 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
549 if (d1_variable || d2_variable)
551 if (d1_zero && d2_zero)
553 if (d1_zero || d2_zero
554 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
555 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
562 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
567 if (val != 1 && (flags & COMPARE_DIFFERENT_TU))
568 val = tagged_types_tu_compatible_p (t1, t2, flags);
572 /* The target might allow certain vector types to be compatible. */
573 val = (*targetm.vector_opaque_p) (t1)
574 || (*targetm.vector_opaque_p) (t2);
580 return attrval == 2 && val == 1 ? 2 : val;
583 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
584 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
585 to 1 or 0 depending if the check of the pointer types is meant to
586 be reflexive or not (typically, assignments are not reflexive,
587 while comparisons are reflexive).
591 comp_target_types (tree ttl, tree ttr, int reflexive)
595 /* Give objc_comptypes a crack at letting these types through. */
596 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
599 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
600 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
602 if (val == 2 && pedantic)
603 pedwarn ("types are not quite compatible");
607 /* Subroutines of `comptypes'. */
609 /* The C standard says that two structures in different translation
610 units are compatible with each other only if the types of their
611 fields are compatible (among other things). So, consider two copies
612 of this structure: */
614 struct tagged_tu_seen {
615 const struct tagged_tu_seen * next;
620 /* Can they be compatible with each other? We choose to break the
621 recursion by allowing those types to be compatible. */
623 static const struct tagged_tu_seen * tagged_tu_seen_base;
625 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
626 compatible. If the two types are not the same (which has been
627 checked earlier), this can only happen when multiple translation
628 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
632 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
635 bool needs_warning = false;
637 /* We have to verify that the tags of the types are the same. This
638 is harder than it looks because this may be a typedef, so we have
639 to go look at the original type. It may even be a typedef of a
641 while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
642 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
644 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
645 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
647 /* C90 didn't have the requirement that the two tags be the same. */
648 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
651 /* C90 didn't say what happened if one or both of the types were
652 incomplete; we choose to follow C99 rules here, which is that they
654 if (TYPE_SIZE (t1) == NULL
655 || TYPE_SIZE (t2) == NULL)
659 const struct tagged_tu_seen * tts_i;
660 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
661 if (tts_i->t1 == t1 && tts_i->t2 == t2)
665 switch (TREE_CODE (t1))
669 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
672 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
674 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
676 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
684 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
687 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
690 struct tagged_tu_seen tts;
692 tts.next = tagged_tu_seen_base;
695 tagged_tu_seen_base = &tts;
697 if (DECL_NAME (s1) != NULL)
698 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
699 if (DECL_NAME (s1) == DECL_NAME (s2))
702 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
706 needs_warning = true;
708 if (TREE_CODE (s1) == FIELD_DECL
709 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
710 DECL_FIELD_BIT_OFFSET (s2)) != 1)
716 tagged_tu_seen_base = tts.next;
720 return needs_warning ? 2 : 1;
725 struct tagged_tu_seen tts;
727 tts.next = tagged_tu_seen_base;
730 tagged_tu_seen_base = &tts;
732 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
734 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
737 if (TREE_CODE (s1) != TREE_CODE (s2)
738 || DECL_NAME (s1) != DECL_NAME (s2))
740 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
744 needs_warning = true;
746 if (TREE_CODE (s1) == FIELD_DECL
747 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
748 DECL_FIELD_BIT_OFFSET (s2)) != 1)
751 tagged_tu_seen_base = tts.next;
754 return needs_warning ? 2 : 1;
762 /* Return 1 if two function types F1 and F2 are compatible.
763 If either type specifies no argument types,
764 the other must specify a fixed number of self-promoting arg types.
765 Otherwise, if one type specifies only the number of arguments,
766 the other must specify that number of self-promoting arg types.
767 Otherwise, the argument types must match. */
770 function_types_compatible_p (tree f1, tree f2, int flags)
773 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
778 ret1 = TREE_TYPE (f1);
779 ret2 = TREE_TYPE (f2);
781 /* 'volatile' qualifiers on a function's return type mean the function
783 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
784 pedwarn ("function return types not compatible due to `volatile'");
785 if (TYPE_VOLATILE (ret1))
786 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
787 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
788 if (TYPE_VOLATILE (ret2))
789 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
790 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
791 val = comptypes (ret1, ret2, flags);
795 args1 = TYPE_ARG_TYPES (f1);
796 args2 = TYPE_ARG_TYPES (f2);
798 /* An unspecified parmlist matches any specified parmlist
799 whose argument types don't need default promotions. */
803 if (!self_promoting_args_p (args2))
805 /* If one of these types comes from a non-prototype fn definition,
806 compare that with the other type's arglist.
807 If they don't match, ask for a warning (but no error). */
808 if (TYPE_ACTUAL_ARG_TYPES (f1)
809 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
816 if (!self_promoting_args_p (args1))
818 if (TYPE_ACTUAL_ARG_TYPES (f2)
819 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
825 /* Both types have argument lists: compare them and propagate results. */
826 val1 = type_lists_compatible_p (args1, args2, flags);
827 return val1 != 1 ? val1 : val;
830 /* Check two lists of types for compatibility,
831 returning 0 for incompatible, 1 for compatible,
832 or 2 for compatible with warning. */
835 type_lists_compatible_p (tree args1, tree args2, int flags)
837 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
843 if (args1 == 0 && args2 == 0)
845 /* If one list is shorter than the other,
846 they fail to match. */
847 if (args1 == 0 || args2 == 0)
849 /* A null pointer instead of a type
850 means there is supposed to be an argument
851 but nothing is specified about what type it has.
852 So match anything that self-promotes. */
853 if (TREE_VALUE (args1) == 0)
855 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
858 else if (TREE_VALUE (args2) == 0)
860 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
863 /* If one of the lists has an error marker, ignore this arg. */
864 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
865 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
867 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
868 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
871 /* Allow wait (union {union wait *u; int *i} *)
872 and wait (union wait *) to be compatible. */
873 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
874 && (TYPE_NAME (TREE_VALUE (args1)) == 0
875 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
876 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
877 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
878 TYPE_SIZE (TREE_VALUE (args2))))
881 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
882 memb; memb = TREE_CHAIN (memb))
883 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
889 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
890 && (TYPE_NAME (TREE_VALUE (args2)) == 0
891 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
892 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
893 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
894 TYPE_SIZE (TREE_VALUE (args1))))
897 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
898 memb; memb = TREE_CHAIN (memb))
899 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
909 /* comptypes said ok, but record if it said to warn. */
913 args1 = TREE_CHAIN (args1);
914 args2 = TREE_CHAIN (args2);
918 /* Compute the size to increment a pointer by. */
921 c_size_in_bytes (tree type)
923 enum tree_code code = TREE_CODE (type);
925 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
926 return size_one_node;
928 if (!COMPLETE_OR_VOID_TYPE_P (type))
930 error ("arithmetic on pointer to an incomplete type");
931 return size_one_node;
934 /* Convert in case a char is more than one unit. */
935 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
936 size_int (TYPE_PRECISION (char_type_node)
940 /* Return either DECL or its known constant value (if it has one). */
943 decl_constant_value (tree decl)
945 if (/* Don't change a variable array bound or initial value to a constant
946 in a place where a variable is invalid. */
947 current_function_decl != 0
948 && ! TREE_THIS_VOLATILE (decl)
949 && TREE_READONLY (decl)
950 && DECL_INITIAL (decl) != 0
951 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
952 /* This is invalid if initial value is not constant.
953 If it has either a function call, a memory reference,
954 or a variable, then re-evaluating it could give different results. */
955 && TREE_CONSTANT (DECL_INITIAL (decl))
956 /* Check for cases where this is sub-optimal, even though valid. */
957 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
958 return DECL_INITIAL (decl);
962 /* Return either DECL or its known constant value (if it has one), but
963 return DECL if pedantic or DECL has mode BLKmode. This is for
964 bug-compatibility with the old behavior of decl_constant_value
965 (before GCC 3.0); every use of this function is a bug and it should
966 be removed before GCC 3.1. It is not appropriate to use pedantic
967 in a way that affects optimization, and BLKmode is probably not the
968 right test for avoiding misoptimizations either. */
971 decl_constant_value_for_broken_optimization (tree decl)
973 if (pedantic || DECL_MODE (decl) == BLKmode)
976 return decl_constant_value (decl);
980 /* Perform the default conversion of arrays and functions to pointers.
981 Return the result of converting EXP. For any other expression, just
985 default_function_array_conversion (tree exp)
988 tree type = TREE_TYPE (exp);
989 enum tree_code code = TREE_CODE (type);
992 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
995 Do not use STRIP_NOPS here! It will remove conversions from pointer
996 to integer and cause infinite recursion. */
998 while (TREE_CODE (exp) == NON_LVALUE_EXPR
999 || (TREE_CODE (exp) == NOP_EXPR
1000 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1002 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1004 exp = TREE_OPERAND (exp, 0);
1007 /* Preserve the original expression code. */
1008 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1009 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1011 if (code == FUNCTION_TYPE)
1013 return build_unary_op (ADDR_EXPR, exp, 0);
1015 if (code == ARRAY_TYPE)
1018 tree restype = TREE_TYPE (type);
1024 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1026 constp = TREE_READONLY (exp);
1027 volatilep = TREE_THIS_VOLATILE (exp);
1030 if (TYPE_QUALS (type) || constp || volatilep)
1032 = c_build_qualified_type (restype,
1034 | (constp * TYPE_QUAL_CONST)
1035 | (volatilep * TYPE_QUAL_VOLATILE));
1037 if (TREE_CODE (exp) == INDIRECT_REF)
1038 return convert (TYPE_POINTER_TO (restype),
1039 TREE_OPERAND (exp, 0));
1041 if (TREE_CODE (exp) == COMPOUND_EXPR)
1043 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1044 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1045 TREE_OPERAND (exp, 0), op1);
1048 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1049 if (!flag_isoc99 && !lvalue_array_p)
1051 /* Before C99, non-lvalue arrays do not decay to pointers.
1052 Normally, using such an array would be invalid; but it can
1053 be used correctly inside sizeof or as a statement expression.
1054 Thus, do not give an error here; an error will result later. */
1058 ptrtype = build_pointer_type (restype);
1060 if (TREE_CODE (exp) == VAR_DECL)
1062 /* ??? This is not really quite correct
1063 in that the type of the operand of ADDR_EXPR
1064 is not the target type of the type of the ADDR_EXPR itself.
1065 Question is, can this lossage be avoided? */
1066 adr = build1 (ADDR_EXPR, ptrtype, exp);
1067 if (!c_mark_addressable (exp))
1068 return error_mark_node;
1069 TREE_CONSTANT (adr) = staticp (exp);
1070 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1073 /* This way is better for a COMPONENT_REF since it can
1074 simplify the offset for a component. */
1075 adr = build_unary_op (ADDR_EXPR, exp, 1);
1076 return convert (ptrtype, adr);
1081 /* Perform default promotions for C data used in expressions.
1082 Arrays and functions are converted to pointers;
1083 enumeral types or short or char, to int.
1084 In addition, manifest constants symbols are replaced by their values. */
1087 default_conversion (tree exp)
1090 tree type = TREE_TYPE (exp);
1091 enum tree_code code = TREE_CODE (type);
1093 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1094 return default_function_array_conversion (exp);
1096 /* Constants can be used directly unless they're not loadable. */
1097 if (TREE_CODE (exp) == CONST_DECL)
1098 exp = DECL_INITIAL (exp);
1100 /* Replace a nonvolatile const static variable with its value unless
1101 it is an array, in which case we must be sure that taking the
1102 address of the array produces consistent results. */
1103 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1105 exp = decl_constant_value_for_broken_optimization (exp);
1106 type = TREE_TYPE (exp);
1109 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1112 Do not use STRIP_NOPS here! It will remove conversions from pointer
1113 to integer and cause infinite recursion. */
1115 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1116 || (TREE_CODE (exp) == NOP_EXPR
1117 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1118 exp = TREE_OPERAND (exp, 0);
1120 /* Preserve the original expression code. */
1121 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1122 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1124 /* Normally convert enums to int,
1125 but convert wide enums to something wider. */
1126 if (code == ENUMERAL_TYPE)
1128 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1129 TYPE_PRECISION (integer_type_node)),
1130 ((TYPE_PRECISION (type)
1131 >= TYPE_PRECISION (integer_type_node))
1132 && TREE_UNSIGNED (type)));
1134 return convert (type, exp);
1137 if (TREE_CODE (exp) == COMPONENT_REF
1138 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1139 /* If it's thinner than an int, promote it like a
1140 c_promoting_integer_type_p, otherwise leave it alone. */
1141 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1142 TYPE_PRECISION (integer_type_node)))
1143 return convert (integer_type_node, exp);
1145 if (c_promoting_integer_type_p (type))
1147 /* Preserve unsignedness if not really getting any wider. */
1148 if (TREE_UNSIGNED (type)
1149 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1150 return convert (unsigned_type_node, exp);
1152 return convert (integer_type_node, exp);
1155 if (code == VOID_TYPE)
1157 error ("void value not ignored as it ought to be");
1158 return error_mark_node;
1163 /* Look up COMPONENT in a structure or union DECL.
1165 If the component name is not found, returns NULL_TREE. Otherwise,
1166 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1167 stepping down the chain to the component, which is in the last
1168 TREE_VALUE of the list. Normally the list is of length one, but if
1169 the component is embedded within (nested) anonymous structures or
1170 unions, the list steps down the chain to the component. */
1173 lookup_field (tree decl, tree component)
1175 tree type = TREE_TYPE (decl);
1178 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1179 to the field elements. Use a binary search on this array to quickly
1180 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1181 will always be set for structures which have many elements. */
1183 if (TYPE_LANG_SPECIFIC (type))
1186 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1188 field = TYPE_FIELDS (type);
1190 top = TYPE_LANG_SPECIFIC (type)->s->len;
1191 while (top - bot > 1)
1193 half = (top - bot + 1) >> 1;
1194 field = field_array[bot+half];
1196 if (DECL_NAME (field) == NULL_TREE)
1198 /* Step through all anon unions in linear fashion. */
1199 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1201 field = field_array[bot++];
1202 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1203 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1205 tree anon = lookup_field (field, component);
1208 return tree_cons (NULL_TREE, field, anon);
1212 /* Entire record is only anon unions. */
1216 /* Restart the binary search, with new lower bound. */
1220 if (DECL_NAME (field) == component)
1222 if (DECL_NAME (field) < component)
1228 if (DECL_NAME (field_array[bot]) == component)
1229 field = field_array[bot];
1230 else if (DECL_NAME (field) != component)
1235 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1237 if (DECL_NAME (field) == NULL_TREE
1238 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1239 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1241 tree anon = lookup_field (field, component);
1244 return tree_cons (NULL_TREE, field, anon);
1247 if (DECL_NAME (field) == component)
1251 if (field == NULL_TREE)
1255 return tree_cons (NULL_TREE, field, NULL_TREE);
1258 /* Make an expression to refer to the COMPONENT field of
1259 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1262 build_component_ref (tree datum, tree component)
1264 tree type = TREE_TYPE (datum);
1265 enum tree_code code = TREE_CODE (type);
1269 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1270 If pedantic ensure that the arguments are not lvalues; otherwise,
1271 if the component is an array, it would wrongly decay to a pointer in
1273 We cannot do this with a COND_EXPR, because in a conditional expression
1274 the default promotions are applied to both sides, and this would yield
1275 the wrong type of the result; for example, if the components have
1277 switch (TREE_CODE (datum))
1281 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1282 return build (COMPOUND_EXPR, TREE_TYPE (value),
1283 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1289 /* See if there is a field or component with name COMPONENT. */
1291 if (code == RECORD_TYPE || code == UNION_TYPE)
1293 if (!COMPLETE_TYPE_P (type))
1295 c_incomplete_type_error (NULL_TREE, type);
1296 return error_mark_node;
1299 field = lookup_field (datum, component);
1303 error ("%s has no member named `%s'",
1304 code == RECORD_TYPE ? "structure" : "union",
1305 IDENTIFIER_POINTER (component));
1306 return error_mark_node;
1309 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1310 This might be better solved in future the way the C++ front
1311 end does it - by giving the anonymous entities each a
1312 separate name and type, and then have build_component_ref
1313 recursively call itself. We can't do that here. */
1316 tree subdatum = TREE_VALUE (field);
1318 if (TREE_TYPE (subdatum) == error_mark_node)
1319 return error_mark_node;
1321 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1322 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1323 TREE_READONLY (ref) = 1;
1324 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1325 TREE_THIS_VOLATILE (ref) = 1;
1327 if (TREE_DEPRECATED (subdatum))
1328 warn_deprecated_use (subdatum);
1332 field = TREE_CHAIN (field);
1338 else if (code != ERROR_MARK)
1339 error ("request for member `%s' in something not a structure or union",
1340 IDENTIFIER_POINTER (component));
1342 return error_mark_node;
1345 /* Given an expression PTR for a pointer, return an expression
1346 for the value pointed to.
1347 ERRORSTRING is the name of the operator to appear in error messages. */
1350 build_indirect_ref (tree ptr, const char *errorstring)
1352 tree pointer = default_conversion (ptr);
1353 tree type = TREE_TYPE (pointer);
1355 if (TREE_CODE (type) == POINTER_TYPE)
1357 if (TREE_CODE (pointer) == ADDR_EXPR
1358 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1359 == TREE_TYPE (type)))
1360 return TREE_OPERAND (pointer, 0);
1363 tree t = TREE_TYPE (type);
1364 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1366 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1368 error ("dereferencing pointer to incomplete type");
1369 return error_mark_node;
1371 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1372 warning ("dereferencing `void *' pointer");
1374 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1375 so that we get the proper error message if the result is used
1376 to assign to. Also, &* is supposed to be a no-op.
1377 And ANSI C seems to specify that the type of the result
1378 should be the const type. */
1379 /* A de-reference of a pointer to const is not a const. It is valid
1380 to change it via some other pointer. */
1381 TREE_READONLY (ref) = TYPE_READONLY (t);
1382 TREE_SIDE_EFFECTS (ref)
1383 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1384 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1388 else if (TREE_CODE (pointer) != ERROR_MARK)
1389 error ("invalid type argument of `%s'", errorstring);
1390 return error_mark_node;
1393 /* This handles expressions of the form "a[i]", which denotes
1396 This is logically equivalent in C to *(a+i), but we may do it differently.
1397 If A is a variable or a member, we generate a primitive ARRAY_REF.
1398 This avoids forcing the array out of registers, and can work on
1399 arrays that are not lvalues (for example, members of structures returned
1403 build_array_ref (tree array, tree index)
1407 error ("subscript missing in array reference");
1408 return error_mark_node;
1411 if (TREE_TYPE (array) == error_mark_node
1412 || TREE_TYPE (index) == error_mark_node)
1413 return error_mark_node;
1415 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1416 && TREE_CODE (array) != INDIRECT_REF)
1420 /* Subscripting with type char is likely to lose
1421 on a machine where chars are signed.
1422 So warn on any machine, but optionally.
1423 Don't warn for unsigned char since that type is safe.
1424 Don't warn for signed char because anyone who uses that
1425 must have done so deliberately. */
1426 if (warn_char_subscripts
1427 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1428 warning ("array subscript has type `char'");
1430 /* Apply default promotions *after* noticing character types. */
1431 index = default_conversion (index);
1433 /* Require integer *after* promotion, for sake of enums. */
1434 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1436 error ("array subscript is not an integer");
1437 return error_mark_node;
1440 /* An array that is indexed by a non-constant
1441 cannot be stored in a register; we must be able to do
1442 address arithmetic on its address.
1443 Likewise an array of elements of variable size. */
1444 if (TREE_CODE (index) != INTEGER_CST
1445 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1446 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1448 if (!c_mark_addressable (array))
1449 return error_mark_node;
1451 /* An array that is indexed by a constant value which is not within
1452 the array bounds cannot be stored in a register either; because we
1453 would get a crash in store_bit_field/extract_bit_field when trying
1454 to access a non-existent part of the register. */
1455 if (TREE_CODE (index) == INTEGER_CST
1456 && TYPE_VALUES (TREE_TYPE (array))
1457 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1459 if (!c_mark_addressable (array))
1460 return error_mark_node;
1466 while (TREE_CODE (foo) == COMPONENT_REF)
1467 foo = TREE_OPERAND (foo, 0);
1468 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1469 pedwarn ("ISO C forbids subscripting `register' array");
1470 else if (! flag_isoc99 && ! lvalue_p (foo))
1471 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1474 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1475 rval = build (ARRAY_REF, type, array, index);
1476 /* Array ref is const/volatile if the array elements are
1477 or if the array is. */
1478 TREE_READONLY (rval)
1479 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1480 | TREE_READONLY (array));
1481 TREE_SIDE_EFFECTS (rval)
1482 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1483 | TREE_SIDE_EFFECTS (array));
1484 TREE_THIS_VOLATILE (rval)
1485 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1486 /* This was added by rms on 16 Nov 91.
1487 It fixes vol struct foo *a; a->elts[1]
1488 in an inline function.
1489 Hope it doesn't break something else. */
1490 | TREE_THIS_VOLATILE (array));
1491 return require_complete_type (fold (rval));
1495 tree ar = default_conversion (array);
1496 tree ind = default_conversion (index);
1498 /* Do the same warning check as above, but only on the part that's
1499 syntactically the index and only if it is also semantically
1501 if (warn_char_subscripts
1502 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1503 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1504 warning ("subscript has type `char'");
1506 /* Put the integer in IND to simplify error checking. */
1507 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1514 if (ar == error_mark_node)
1517 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1518 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1520 error ("subscripted value is neither array nor pointer");
1521 return error_mark_node;
1523 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1525 error ("array subscript is not an integer");
1526 return error_mark_node;
1529 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1534 /* Build an external reference to identifier ID. FUN indicates
1535 whether this will be used for a function call. */
1537 build_external_ref (tree id, int fun)
1540 tree decl = lookup_name (id);
1541 tree objc_ivar = lookup_objc_ivar (id);
1543 if (decl && decl != error_mark_node)
1545 /* Properly declared variable or function reference. */
1548 else if (decl != objc_ivar && !C_DECL_FILE_SCOPE (decl))
1550 warning ("local declaration of `%s' hides instance variable",
1551 IDENTIFIER_POINTER (id));
1560 /* Implicit function declaration. */
1561 ref = implicitly_declare (id);
1562 else if (decl == error_mark_node)
1563 /* Don't complain about something that's already been
1564 complained about. */
1565 return error_mark_node;
1568 undeclared_variable (id);
1569 return error_mark_node;
1572 if (TREE_TYPE (ref) == error_mark_node)
1573 return error_mark_node;
1575 if (TREE_DEPRECATED (ref))
1576 warn_deprecated_use (ref);
1578 if (!skip_evaluation)
1579 assemble_external (ref);
1580 TREE_USED (ref) = 1;
1582 if (TREE_CODE (ref) == CONST_DECL)
1584 ref = DECL_INITIAL (ref);
1585 TREE_CONSTANT (ref) = 1;
1587 else if (current_function_decl != 0
1588 && !C_DECL_FILE_SCOPE (current_function_decl)
1589 && (TREE_CODE (ref) == VAR_DECL
1590 || TREE_CODE (ref) == PARM_DECL
1591 || TREE_CODE (ref) == FUNCTION_DECL))
1593 tree context = decl_function_context (ref);
1595 if (context != 0 && context != current_function_decl)
1596 DECL_NONLOCAL (ref) = 1;
1602 /* Build a function call to function FUNCTION with parameters PARAMS.
1603 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1604 TREE_VALUE of each node is a parameter-expression.
1605 FUNCTION's data type may be a function type or a pointer-to-function. */
1608 build_function_call (tree function, tree params)
1610 tree fntype, fundecl = 0;
1611 tree coerced_params;
1612 tree name = NULL_TREE, result;
1614 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1615 STRIP_TYPE_NOPS (function);
1617 /* Convert anything with function type to a pointer-to-function. */
1618 if (TREE_CODE (function) == FUNCTION_DECL)
1620 name = DECL_NAME (function);
1622 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1623 (because calling an inline function does not mean the function
1624 needs to be separately compiled). */
1625 fntype = build_type_variant (TREE_TYPE (function),
1626 TREE_READONLY (function),
1627 TREE_THIS_VOLATILE (function));
1629 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1632 function = default_conversion (function);
1634 fntype = TREE_TYPE (function);
1636 if (TREE_CODE (fntype) == ERROR_MARK)
1637 return error_mark_node;
1639 if (!(TREE_CODE (fntype) == POINTER_TYPE
1640 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1642 error ("called object is not a function");
1643 return error_mark_node;
1646 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1647 current_function_returns_abnormally = 1;
1649 /* fntype now gets the type of function pointed to. */
1650 fntype = TREE_TYPE (fntype);
1652 /* Convert the parameters to the types declared in the
1653 function prototype, or apply default promotions. */
1656 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1658 /* Check that the arguments to the function are valid. */
1660 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1662 /* Recognize certain built-in functions so we can make tree-codes
1663 other than CALL_EXPR. We do this when it enables fold-const.c
1664 to do something useful. */
1666 if (TREE_CODE (function) == ADDR_EXPR
1667 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1668 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1670 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1671 params, coerced_params);
1676 result = build (CALL_EXPR, TREE_TYPE (fntype),
1677 function, coerced_params, NULL_TREE);
1678 TREE_SIDE_EFFECTS (result) = 1;
1679 result = fold (result);
1681 if (VOID_TYPE_P (TREE_TYPE (result)))
1683 return require_complete_type (result);
1686 /* Convert the argument expressions in the list VALUES
1687 to the types in the list TYPELIST. The result is a list of converted
1688 argument expressions.
1690 If TYPELIST is exhausted, or when an element has NULL as its type,
1691 perform the default conversions.
1693 PARMLIST is the chain of parm decls for the function being called.
1694 It may be 0, if that info is not available.
1695 It is used only for generating error messages.
1697 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1699 This is also where warnings about wrong number of args are generated.
1701 Both VALUES and the returned value are chains of TREE_LIST nodes
1702 with the elements of the list in the TREE_VALUE slots of those nodes. */
1705 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1707 tree typetail, valtail;
1711 /* Scan the given expressions and types, producing individual
1712 converted arguments and pushing them on RESULT in reverse order. */
1714 for (valtail = values, typetail = typelist, parmnum = 0;
1716 valtail = TREE_CHAIN (valtail), parmnum++)
1718 tree type = typetail ? TREE_VALUE (typetail) : 0;
1719 tree val = TREE_VALUE (valtail);
1721 if (type == void_type_node)
1724 error ("too many arguments to function `%s'",
1725 IDENTIFIER_POINTER (name));
1727 error ("too many arguments to function");
1731 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1732 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1733 to convert automatically to a pointer. */
1734 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1735 val = TREE_OPERAND (val, 0);
1737 val = default_function_array_conversion (val);
1739 val = require_complete_type (val);
1743 /* Formal parm type is specified by a function prototype. */
1746 if (!COMPLETE_TYPE_P (type))
1748 error ("type of formal parameter %d is incomplete", parmnum + 1);
1753 /* Optionally warn about conversions that
1754 differ from the default conversions. */
1755 if (warn_conversion || warn_traditional)
1757 int formal_prec = TYPE_PRECISION (type);
1759 if (INTEGRAL_TYPE_P (type)
1760 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1761 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1762 if (INTEGRAL_TYPE_P (type)
1763 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1764 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1765 else if (TREE_CODE (type) == COMPLEX_TYPE
1766 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1767 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1768 else if (TREE_CODE (type) == REAL_TYPE
1769 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1770 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1771 else if (TREE_CODE (type) == COMPLEX_TYPE
1772 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1773 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1774 else if (TREE_CODE (type) == REAL_TYPE
1775 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1776 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1777 /* ??? At some point, messages should be written about
1778 conversions between complex types, but that's too messy
1780 else if (TREE_CODE (type) == REAL_TYPE
1781 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1783 /* Warn if any argument is passed as `float',
1784 since without a prototype it would be `double'. */
1785 if (formal_prec == TYPE_PRECISION (float_type_node))
1786 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1788 /* Detect integer changing in width or signedness.
1789 These warnings are only activated with
1790 -Wconversion, not with -Wtraditional. */
1791 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1792 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1794 tree would_have_been = default_conversion (val);
1795 tree type1 = TREE_TYPE (would_have_been);
1797 if (TREE_CODE (type) == ENUMERAL_TYPE
1798 && (TYPE_MAIN_VARIANT (type)
1799 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1800 /* No warning if function asks for enum
1801 and the actual arg is that enum type. */
1803 else if (formal_prec != TYPE_PRECISION (type1))
1804 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1805 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1807 /* Don't complain if the formal parameter type
1808 is an enum, because we can't tell now whether
1809 the value was an enum--even the same enum. */
1810 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1812 else if (TREE_CODE (val) == INTEGER_CST
1813 && int_fits_type_p (val, type))
1814 /* Change in signedness doesn't matter
1815 if a constant value is unaffected. */
1817 /* Likewise for a constant in a NOP_EXPR. */
1818 else if (TREE_CODE (val) == NOP_EXPR
1819 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1820 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1822 /* If the value is extended from a narrower
1823 unsigned type, it doesn't matter whether we
1824 pass it as signed or unsigned; the value
1825 certainly is the same either way. */
1826 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1827 && TREE_UNSIGNED (TREE_TYPE (val)))
1829 else if (TREE_UNSIGNED (type))
1830 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1832 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1836 parmval = convert_for_assignment (type, val,
1837 (char *) 0, /* arg passing */
1838 fundecl, name, parmnum + 1);
1840 if (PROMOTE_PROTOTYPES
1841 && INTEGRAL_TYPE_P (type)
1842 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1843 parmval = default_conversion (parmval);
1845 result = tree_cons (NULL_TREE, parmval, result);
1847 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1848 && (TYPE_PRECISION (TREE_TYPE (val))
1849 < TYPE_PRECISION (double_type_node)))
1850 /* Convert `float' to `double'. */
1851 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1853 /* Convert `short' and `char' to full-size `int'. */
1854 result = tree_cons (NULL_TREE, default_conversion (val), result);
1857 typetail = TREE_CHAIN (typetail);
1860 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1863 error ("too few arguments to function `%s'",
1864 IDENTIFIER_POINTER (name));
1866 error ("too few arguments to function");
1869 return nreverse (result);
1872 /* This is the entry point used by the parser
1873 for binary operators in the input.
1874 In addition to constructing the expression,
1875 we check for operands that were written with other binary operators
1876 in a way that is likely to confuse the user. */
1879 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1881 tree result = build_binary_op (code, arg1, arg2, 1);
1884 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1885 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1886 enum tree_code code1 = ERROR_MARK;
1887 enum tree_code code2 = ERROR_MARK;
1889 if (TREE_CODE (result) == ERROR_MARK)
1890 return error_mark_node;
1892 if (IS_EXPR_CODE_CLASS (class1))
1893 code1 = C_EXP_ORIGINAL_CODE (arg1);
1894 if (IS_EXPR_CODE_CLASS (class2))
1895 code2 = C_EXP_ORIGINAL_CODE (arg2);
1897 /* Check for cases such as x+y<<z which users are likely
1898 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1899 is cleared to prevent these warnings. */
1900 if (warn_parentheses)
1902 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1904 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1905 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1906 warning ("suggest parentheses around + or - inside shift");
1909 if (code == TRUTH_ORIF_EXPR)
1911 if (code1 == TRUTH_ANDIF_EXPR
1912 || code2 == TRUTH_ANDIF_EXPR)
1913 warning ("suggest parentheses around && within ||");
1916 if (code == BIT_IOR_EXPR)
1918 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1919 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1920 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1921 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1922 warning ("suggest parentheses around arithmetic in operand of |");
1923 /* Check cases like x|y==z */
1924 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1925 warning ("suggest parentheses around comparison in operand of |");
1928 if (code == BIT_XOR_EXPR)
1930 if (code1 == BIT_AND_EXPR
1931 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1932 || code2 == BIT_AND_EXPR
1933 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1934 warning ("suggest parentheses around arithmetic in operand of ^");
1935 /* Check cases like x^y==z */
1936 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1937 warning ("suggest parentheses around comparison in operand of ^");
1940 if (code == BIT_AND_EXPR)
1942 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1943 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1944 warning ("suggest parentheses around + or - in operand of &");
1945 /* Check cases like x&y==z */
1946 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1947 warning ("suggest parentheses around comparison in operand of &");
1951 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1952 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1953 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1954 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1956 unsigned_conversion_warning (result, arg1);
1957 unsigned_conversion_warning (result, arg2);
1958 overflow_warning (result);
1960 class = TREE_CODE_CLASS (TREE_CODE (result));
1962 /* Record the code that was specified in the source,
1963 for the sake of warnings about confusing nesting. */
1964 if (IS_EXPR_CODE_CLASS (class))
1965 C_SET_EXP_ORIGINAL_CODE (result, code);
1968 int flag = TREE_CONSTANT (result);
1969 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1970 so that convert_for_assignment wouldn't strip it.
1971 That way, we got warnings for things like p = (1 - 1).
1972 But it turns out we should not get those warnings. */
1973 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1974 C_SET_EXP_ORIGINAL_CODE (result, code);
1975 TREE_CONSTANT (result) = flag;
1982 /* Return true if `t' is known to be non-negative. */
1985 c_tree_expr_nonnegative_p (tree t)
1987 if (TREE_CODE (t) == STMT_EXPR)
1989 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
1991 /* Find the last statement in the chain, ignoring the final
1992 * scope statement */
1993 while (TREE_CHAIN (t) != NULL_TREE
1994 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
1996 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
1998 return tree_expr_nonnegative_p (t);
2001 /* Return a tree for the difference of pointers OP0 and OP1.
2002 The resulting tree has type int. */
2005 pointer_diff (tree op0, tree op1)
2007 tree result, folded;
2008 tree restype = ptrdiff_type_node;
2010 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2011 tree con0, con1, lit0, lit1;
2012 tree orig_op1 = op1;
2014 if (pedantic || warn_pointer_arith)
2016 if (TREE_CODE (target_type) == VOID_TYPE)
2017 pedwarn ("pointer of type `void *' used in subtraction");
2018 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2019 pedwarn ("pointer to a function used in subtraction");
2022 /* If the conversion to ptrdiff_type does anything like widening or
2023 converting a partial to an integral mode, we get a convert_expression
2024 that is in the way to do any simplifications.
2025 (fold-const.c doesn't know that the extra bits won't be needed.
2026 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2027 different mode in place.)
2028 So first try to find a common term here 'by hand'; we want to cover
2029 at least the cases that occur in legal static initializers. */
2030 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2031 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2033 if (TREE_CODE (con0) == PLUS_EXPR)
2035 lit0 = TREE_OPERAND (con0, 1);
2036 con0 = TREE_OPERAND (con0, 0);
2039 lit0 = integer_zero_node;
2041 if (TREE_CODE (con1) == PLUS_EXPR)
2043 lit1 = TREE_OPERAND (con1, 1);
2044 con1 = TREE_OPERAND (con1, 0);
2047 lit1 = integer_zero_node;
2049 if (operand_equal_p (con0, con1, 0))
2056 /* First do the subtraction as integers;
2057 then drop through to build the divide operator.
2058 Do not do default conversions on the minus operator
2059 in case restype is a short type. */
2061 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2062 convert (restype, op1), 0);
2063 /* This generates an error if op1 is pointer to incomplete type. */
2064 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2065 error ("arithmetic on pointer to an incomplete type");
2067 /* This generates an error if op0 is pointer to incomplete type. */
2068 op1 = c_size_in_bytes (target_type);
2070 /* Divide by the size, in easiest possible way. */
2072 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2074 folded = fold (result);
2075 if (folded == result)
2076 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2080 /* Construct and perhaps optimize a tree representation
2081 for a unary operation. CODE, a tree_code, specifies the operation
2082 and XARG is the operand.
2083 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2084 the default promotions (such as from short to int).
2085 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2086 allows non-lvalues; this is only used to handle conversion of non-lvalue
2087 arrays to pointers in C99. */
2090 build_unary_op (enum tree_code code, tree xarg, int flag)
2092 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2095 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2097 int noconvert = flag;
2099 if (typecode == ERROR_MARK)
2100 return error_mark_node;
2101 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2102 typecode = INTEGER_TYPE;
2107 /* This is used for unary plus, because a CONVERT_EXPR
2108 is enough to prevent anybody from looking inside for
2109 associativity, but won't generate any code. */
2110 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2111 || typecode == COMPLEX_TYPE))
2113 error ("wrong type argument to unary plus");
2114 return error_mark_node;
2116 else if (!noconvert)
2117 arg = default_conversion (arg);
2118 arg = non_lvalue (arg);
2122 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2123 || typecode == COMPLEX_TYPE
2124 || typecode == VECTOR_TYPE))
2126 error ("wrong type argument to unary minus");
2127 return error_mark_node;
2129 else if (!noconvert)
2130 arg = default_conversion (arg);
2134 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2137 arg = default_conversion (arg);
2139 else if (typecode == COMPLEX_TYPE)
2143 pedwarn ("ISO C does not support `~' for complex conjugation");
2145 arg = default_conversion (arg);
2149 error ("wrong type argument to bit-complement");
2150 return error_mark_node;
2155 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2157 error ("wrong type argument to abs");
2158 return error_mark_node;
2160 else if (!noconvert)
2161 arg = default_conversion (arg);
2165 /* Conjugating a real value is a no-op, but allow it anyway. */
2166 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2167 || typecode == COMPLEX_TYPE))
2169 error ("wrong type argument to conjugation");
2170 return error_mark_node;
2172 else if (!noconvert)
2173 arg = default_conversion (arg);
2176 case TRUTH_NOT_EXPR:
2177 if (typecode != INTEGER_TYPE
2178 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2179 && typecode != COMPLEX_TYPE
2180 /* These will convert to a pointer. */
2181 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2183 error ("wrong type argument to unary exclamation mark");
2184 return error_mark_node;
2186 arg = c_common_truthvalue_conversion (arg);
2187 return invert_truthvalue (arg);
2193 if (TREE_CODE (arg) == COMPLEX_CST)
2194 return TREE_REALPART (arg);
2195 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2196 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2201 if (TREE_CODE (arg) == COMPLEX_CST)
2202 return TREE_IMAGPART (arg);
2203 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2204 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2206 return convert (TREE_TYPE (arg), integer_zero_node);
2208 case PREINCREMENT_EXPR:
2209 case POSTINCREMENT_EXPR:
2210 case PREDECREMENT_EXPR:
2211 case POSTDECREMENT_EXPR:
2212 /* Handle complex lvalues (when permitted)
2213 by reduction to simpler cases. */
2215 val = unary_complex_lvalue (code, arg, 0);
2219 /* Increment or decrement the real part of the value,
2220 and don't change the imaginary part. */
2221 if (typecode == COMPLEX_TYPE)
2226 pedwarn ("ISO C does not support `++' and `--' on complex types");
2228 arg = stabilize_reference (arg);
2229 real = build_unary_op (REALPART_EXPR, arg, 1);
2230 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2231 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2232 build_unary_op (code, real, 1), imag);
2235 /* Report invalid types. */
2237 if (typecode != POINTER_TYPE
2238 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2240 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2241 error ("wrong type argument to increment");
2243 error ("wrong type argument to decrement");
2245 return error_mark_node;
2250 tree result_type = TREE_TYPE (arg);
2252 arg = get_unwidened (arg, 0);
2253 argtype = TREE_TYPE (arg);
2255 /* Compute the increment. */
2257 if (typecode == POINTER_TYPE)
2259 /* If pointer target is an undefined struct,
2260 we just cannot know how to do the arithmetic. */
2261 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2263 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2264 error ("increment of pointer to unknown structure");
2266 error ("decrement of pointer to unknown structure");
2268 else if ((pedantic || warn_pointer_arith)
2269 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2270 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2272 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2273 pedwarn ("wrong type argument to increment");
2275 pedwarn ("wrong type argument to decrement");
2278 inc = c_size_in_bytes (TREE_TYPE (result_type));
2281 inc = integer_one_node;
2283 inc = convert (argtype, inc);
2285 /* Handle incrementing a cast-expression. */
2288 switch (TREE_CODE (arg))
2293 case FIX_TRUNC_EXPR:
2294 case FIX_FLOOR_EXPR:
2295 case FIX_ROUND_EXPR:
2297 pedantic_lvalue_warning (CONVERT_EXPR);
2298 /* If the real type has the same machine representation
2299 as the type it is cast to, we can make better output
2300 by adding directly to the inside of the cast. */
2301 if ((TREE_CODE (TREE_TYPE (arg))
2302 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2303 && (TYPE_MODE (TREE_TYPE (arg))
2304 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2305 arg = TREE_OPERAND (arg, 0);
2308 tree incremented, modify, value;
2309 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2310 value = boolean_increment (code, arg);
2313 arg = stabilize_reference (arg);
2314 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2317 value = save_expr (arg);
2318 incremented = build (((code == PREINCREMENT_EXPR
2319 || code == POSTINCREMENT_EXPR)
2320 ? PLUS_EXPR : MINUS_EXPR),
2321 argtype, value, inc);
2322 TREE_SIDE_EFFECTS (incremented) = 1;
2323 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2324 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2326 TREE_USED (value) = 1;
2336 /* Complain about anything else that is not a true lvalue. */
2337 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2338 || code == POSTINCREMENT_EXPR)
2339 ? "invalid lvalue in increment"
2340 : "invalid lvalue in decrement")))
2341 return error_mark_node;
2343 /* Report a read-only lvalue. */
2344 if (TREE_READONLY (arg))
2345 readonly_warning (arg,
2346 ((code == PREINCREMENT_EXPR
2347 || code == POSTINCREMENT_EXPR)
2348 ? "increment" : "decrement"));
2350 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2351 val = boolean_increment (code, arg);
2353 val = build (code, TREE_TYPE (arg), arg, inc);
2354 TREE_SIDE_EFFECTS (val) = 1;
2355 val = convert (result_type, val);
2356 if (TREE_CODE (val) != code)
2357 TREE_NO_UNUSED_WARNING (val) = 1;
2362 /* Note that this operation never does default_conversion. */
2364 /* Let &* cancel out to simplify resulting code. */
2365 if (TREE_CODE (arg) == INDIRECT_REF)
2367 /* Don't let this be an lvalue. */
2368 if (lvalue_p (TREE_OPERAND (arg, 0)))
2369 return non_lvalue (TREE_OPERAND (arg, 0));
2370 return TREE_OPERAND (arg, 0);
2373 /* For &x[y], return x+y */
2374 if (TREE_CODE (arg) == ARRAY_REF)
2376 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2377 return error_mark_node;
2378 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2379 TREE_OPERAND (arg, 1), 1);
2382 /* Handle complex lvalues (when permitted)
2383 by reduction to simpler cases. */
2384 val = unary_complex_lvalue (code, arg, flag);
2388 /* Anything not already handled and not a true memory reference
2389 or a non-lvalue array is an error. */
2390 else if (typecode != FUNCTION_TYPE && !flag
2391 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2392 return error_mark_node;
2394 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2395 argtype = TREE_TYPE (arg);
2397 /* If the lvalue is const or volatile, merge that into the type
2398 to which the address will point. Note that you can't get a
2399 restricted pointer by taking the address of something, so we
2400 only have to deal with `const' and `volatile' here. */
2401 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2402 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2403 argtype = c_build_type_variant (argtype,
2404 TREE_READONLY (arg),
2405 TREE_THIS_VOLATILE (arg));
2407 argtype = build_pointer_type (argtype);
2409 if (!c_mark_addressable (arg))
2410 return error_mark_node;
2415 if (TREE_CODE (arg) == COMPONENT_REF)
2417 tree field = TREE_OPERAND (arg, 1);
2419 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2421 if (DECL_C_BIT_FIELD (field))
2423 error ("attempt to take address of bit-field structure member `%s'",
2424 IDENTIFIER_POINTER (DECL_NAME (field)));
2425 return error_mark_node;
2428 addr = fold (build (PLUS_EXPR, argtype,
2429 convert (argtype, addr),
2430 convert (argtype, byte_position (field))));
2433 addr = build1 (code, argtype, arg);
2435 /* Address of a static or external variable or
2436 file-scope function counts as a constant. */
2438 && ! (TREE_CODE (arg) == FUNCTION_DECL
2439 && !C_DECL_FILE_SCOPE (arg)))
2440 TREE_CONSTANT (addr) = 1;
2449 argtype = TREE_TYPE (arg);
2450 return fold (build1 (code, argtype, arg));
2453 /* Return nonzero if REF is an lvalue valid for this language.
2454 Lvalues can be assigned, unless their type has TYPE_READONLY.
2455 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2460 enum tree_code code = TREE_CODE (ref);
2467 return lvalue_p (TREE_OPERAND (ref, 0));
2469 case COMPOUND_LITERAL_EXPR:
2479 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2480 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2484 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2491 /* Return nonzero if REF is an lvalue valid for this language;
2492 otherwise, print an error message and return zero. */
2495 lvalue_or_else (tree ref, const char *msgid)
2497 int win = lvalue_p (ref);
2500 error ("%s", msgid);
2505 /* Apply unary lvalue-demanding operator CODE to the expression ARG
2506 for certain kinds of expressions which are not really lvalues
2507 but which we can accept as lvalues. If FLAG is nonzero, then
2508 non-lvalues are OK since we may be converting a non-lvalue array to
2511 If ARG is not a kind of expression we can handle, return zero. */
2514 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
2516 /* Handle (a, b) used as an "lvalue". */
2517 if (TREE_CODE (arg) == COMPOUND_EXPR)
2519 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
2521 /* If this returns a function type, it isn't really being used as
2522 an lvalue, so don't issue a warning about it. */
2523 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2524 pedantic_lvalue_warning (COMPOUND_EXPR);
2526 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
2527 TREE_OPERAND (arg, 0), real_result);
2530 /* Handle (a ? b : c) used as an "lvalue". */
2531 if (TREE_CODE (arg) == COND_EXPR)
2534 pedantic_lvalue_warning (COND_EXPR);
2535 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2536 pedantic_lvalue_warning (COMPOUND_EXPR);
2538 return (build_conditional_expr
2539 (TREE_OPERAND (arg, 0),
2540 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
2541 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
2547 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
2548 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
2551 pedantic_lvalue_warning (enum tree_code code)
2557 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
2560 pedwarn ("ISO C forbids use of compound expressions as lvalues");
2563 pedwarn ("ISO C forbids use of cast expressions as lvalues");
2568 /* Warn about storing in something that is `const'. */
2571 readonly_warning (tree arg, const char *msgid)
2573 if (TREE_CODE (arg) == COMPONENT_REF)
2575 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2576 readonly_warning (TREE_OPERAND (arg, 0), msgid);
2578 pedwarn ("%s of read-only member `%s'", _(msgid),
2579 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2581 else if (TREE_CODE (arg) == VAR_DECL)
2582 pedwarn ("%s of read-only variable `%s'", _(msgid),
2583 IDENTIFIER_POINTER (DECL_NAME (arg)));
2585 pedwarn ("%s of read-only location", _(msgid));
2588 /* Mark EXP saying that we need to be able to take the
2589 address of it; it should not be allocated in a register.
2590 Returns true if successful. */
2593 c_mark_addressable (tree exp)
2598 switch (TREE_CODE (x))
2601 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2603 error ("cannot take address of bit-field `%s'",
2604 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2608 /* ... fall through ... */
2614 x = TREE_OPERAND (x, 0);
2617 case COMPOUND_LITERAL_EXPR:
2619 TREE_ADDRESSABLE (x) = 1;
2626 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2627 && DECL_NONLOCAL (x))
2629 if (TREE_PUBLIC (x))
2631 error ("global register variable `%s' used in nested function",
2632 IDENTIFIER_POINTER (DECL_NAME (x)));
2635 pedwarn ("register variable `%s' used in nested function",
2636 IDENTIFIER_POINTER (DECL_NAME (x)));
2638 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2640 if (TREE_PUBLIC (x))
2642 error ("address of global register variable `%s' requested",
2643 IDENTIFIER_POINTER (DECL_NAME (x)));
2647 /* If we are making this addressable due to its having
2648 volatile components, give a different error message. Also
2649 handle the case of an unnamed parameter by not trying
2650 to give the name. */
2652 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2654 error ("cannot put object with volatile field into register");
2658 pedwarn ("address of register variable `%s' requested",
2659 IDENTIFIER_POINTER (DECL_NAME (x)));
2661 put_var_into_stack (x, /*rescan=*/true);
2665 TREE_ADDRESSABLE (x) = 1;
2672 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2675 build_conditional_expr (tree ifexp, tree op1, tree op2)
2679 enum tree_code code1;
2680 enum tree_code code2;
2681 tree result_type = NULL;
2682 tree orig_op1 = op1, orig_op2 = op2;
2684 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
2686 /* Promote both alternatives. */
2688 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2689 op1 = default_conversion (op1);
2690 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2691 op2 = default_conversion (op2);
2693 if (TREE_CODE (ifexp) == ERROR_MARK
2694 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2695 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2696 return error_mark_node;
2698 type1 = TREE_TYPE (op1);
2699 code1 = TREE_CODE (type1);
2700 type2 = TREE_TYPE (op2);
2701 code2 = TREE_CODE (type2);
2703 /* Quickly detect the usual case where op1 and op2 have the same type
2705 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2708 result_type = type1;
2710 result_type = TYPE_MAIN_VARIANT (type1);
2712 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2713 || code1 == COMPLEX_TYPE)
2714 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2715 || code2 == COMPLEX_TYPE))
2717 result_type = common_type (type1, type2);
2719 /* If -Wsign-compare, warn here if type1 and type2 have
2720 different signedness. We'll promote the signed to unsigned
2721 and later code won't know it used to be different.
2722 Do this check on the original types, so that explicit casts
2723 will be considered, but default promotions won't. */
2724 if (warn_sign_compare && !skip_evaluation)
2726 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2727 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2729 if (unsigned_op1 ^ unsigned_op2)
2731 /* Do not warn if the result type is signed, since the
2732 signed type will only be chosen if it can represent
2733 all the values of the unsigned type. */
2734 if (! TREE_UNSIGNED (result_type))
2736 /* Do not warn if the signed quantity is an unsuffixed
2737 integer literal (or some static constant expression
2738 involving such literals) and it is non-negative. */
2739 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2740 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2743 warning ("signed and unsigned type in conditional expression");
2747 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2749 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2750 pedwarn ("ISO C forbids conditional expr with only one void side");
2751 result_type = void_type_node;
2753 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2755 if (comp_target_types (type1, type2, 1))
2756 result_type = common_type (type1, type2);
2757 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2758 && TREE_CODE (orig_op1) != NOP_EXPR)
2759 result_type = qualify_type (type2, type1);
2760 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2761 && TREE_CODE (orig_op2) != NOP_EXPR)
2762 result_type = qualify_type (type1, type2);
2763 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2765 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2766 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2767 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2768 TREE_TYPE (type2)));
2770 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2772 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2773 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2774 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2775 TREE_TYPE (type1)));
2779 pedwarn ("pointer type mismatch in conditional expression");
2780 result_type = build_pointer_type (void_type_node);
2783 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2785 if (! integer_zerop (op2))
2786 pedwarn ("pointer/integer type mismatch in conditional expression");
2789 op2 = null_pointer_node;
2791 result_type = type1;
2793 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2795 if (!integer_zerop (op1))
2796 pedwarn ("pointer/integer type mismatch in conditional expression");
2799 op1 = null_pointer_node;
2801 result_type = type2;
2806 if (flag_cond_mismatch)
2807 result_type = void_type_node;
2810 error ("type mismatch in conditional expression");
2811 return error_mark_node;
2815 /* Merge const and volatile flags of the incoming types. */
2817 = build_type_variant (result_type,
2818 TREE_READONLY (op1) || TREE_READONLY (op2),
2819 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2821 if (result_type != TREE_TYPE (op1))
2822 op1 = convert_and_check (result_type, op1);
2823 if (result_type != TREE_TYPE (op2))
2824 op2 = convert_and_check (result_type, op2);
2826 if (TREE_CODE (ifexp) == INTEGER_CST)
2827 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2829 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2832 /* Given a list of expressions, return a compound expression
2833 that performs them all and returns the value of the last of them. */
2836 build_compound_expr (tree list)
2838 return internal_build_compound_expr (list, TRUE);
2842 internal_build_compound_expr (tree list, int first_p)
2846 if (TREE_CHAIN (list) == 0)
2848 /* Convert arrays and functions to pointers when there
2849 really is a comma operator. */
2852 = default_function_array_conversion (TREE_VALUE (list));
2854 /* Don't let (0, 0) be null pointer constant. */
2855 if (!first_p && integer_zerop (TREE_VALUE (list)))
2856 return non_lvalue (TREE_VALUE (list));
2857 return TREE_VALUE (list);
2860 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2862 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2864 /* The left-hand operand of a comma expression is like an expression
2865 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2866 any side-effects, unless it was explicitly cast to (void). */
2867 if (warn_unused_value
2868 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2869 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2870 warning ("left-hand operand of comma expression has no effect");
2872 /* When pedantic, a compound expression can be neither an lvalue
2873 nor an integer constant expression. */
2878 /* With -Wunused, we should also warn if the left-hand operand does have
2879 side-effects, but computes a value which is not used. For example, in
2880 `foo() + bar(), baz()' the result of the `+' operator is not used,
2881 so we should issue a warning. */
2882 else if (warn_unused_value)
2883 warn_if_unused_value (TREE_VALUE (list));
2885 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2888 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2891 build_c_cast (tree type, tree expr)
2895 if (type == error_mark_node || expr == error_mark_node)
2896 return error_mark_node;
2898 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2899 only in <protocol> qualifications. But when constructing cast expressions,
2900 the protocols do matter and must be kept around. */
2901 if (!c_dialect_objc () || !objc_is_id (type))
2902 type = TYPE_MAIN_VARIANT (type);
2904 if (TREE_CODE (type) == ARRAY_TYPE)
2906 error ("cast specifies array type");
2907 return error_mark_node;
2910 if (TREE_CODE (type) == FUNCTION_TYPE)
2912 error ("cast specifies function type");
2913 return error_mark_node;
2916 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2920 if (TREE_CODE (type) == RECORD_TYPE
2921 || TREE_CODE (type) == UNION_TYPE)
2922 pedwarn ("ISO C forbids casting nonscalar to the same type");
2925 else if (TREE_CODE (type) == UNION_TYPE)
2928 value = default_function_array_conversion (value);
2930 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2931 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2932 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2940 pedwarn ("ISO C forbids casts to union type");
2941 t = digest_init (type,
2942 build_constructor (type,
2943 build_tree_list (field, value)),
2945 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2948 error ("cast to union type from type not present in union");
2949 return error_mark_node;
2955 /* If casting to void, avoid the error that would come
2956 from default_conversion in the case of a non-lvalue array. */
2957 if (type == void_type_node)
2958 return build1 (CONVERT_EXPR, type, value);
2960 /* Convert functions and arrays to pointers,
2961 but don't convert any other types. */
2962 value = default_function_array_conversion (value);
2963 otype = TREE_TYPE (value);
2965 /* Optionally warn about potentially worrisome casts. */
2968 && TREE_CODE (type) == POINTER_TYPE
2969 && TREE_CODE (otype) == POINTER_TYPE)
2971 tree in_type = type;
2972 tree in_otype = otype;
2976 /* Check that the qualifiers on IN_TYPE are a superset of
2977 the qualifiers of IN_OTYPE. The outermost level of
2978 POINTER_TYPE nodes is uninteresting and we stop as soon
2979 as we hit a non-POINTER_TYPE node on either type. */
2982 in_otype = TREE_TYPE (in_otype);
2983 in_type = TREE_TYPE (in_type);
2985 /* GNU C allows cv-qualified function types. 'const'
2986 means the function is very pure, 'volatile' means it
2987 can't return. We need to warn when such qualifiers
2988 are added, not when they're taken away. */
2989 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2990 && TREE_CODE (in_type) == FUNCTION_TYPE)
2991 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2993 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2995 while (TREE_CODE (in_type) == POINTER_TYPE
2996 && TREE_CODE (in_otype) == POINTER_TYPE);
2999 warning ("cast adds new qualifiers to function type");
3002 /* There are qualifiers present in IN_OTYPE that are not
3003 present in IN_TYPE. */
3004 warning ("cast discards qualifiers from pointer target type");
3007 /* Warn about possible alignment problems. */
3008 if (STRICT_ALIGNMENT && warn_cast_align
3009 && TREE_CODE (type) == POINTER_TYPE
3010 && TREE_CODE (otype) == POINTER_TYPE
3011 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3012 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3013 /* Don't warn about opaque types, where the actual alignment
3014 restriction is unknown. */
3015 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3016 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3017 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3018 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3019 warning ("cast increases required alignment of target type");
3021 if (TREE_CODE (type) == INTEGER_TYPE
3022 && TREE_CODE (otype) == POINTER_TYPE
3023 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3024 && !TREE_CONSTANT (value))
3025 warning ("cast from pointer to integer of different size");
3027 if (warn_bad_function_cast
3028 && TREE_CODE (value) == CALL_EXPR
3029 && TREE_CODE (type) != TREE_CODE (otype))
3030 warning ("cast does not match function type");
3032 if (TREE_CODE (type) == POINTER_TYPE
3033 && TREE_CODE (otype) == INTEGER_TYPE
3034 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3035 /* Don't warn about converting any constant. */
3036 && !TREE_CONSTANT (value))
3037 warning ("cast to pointer from integer of different size");
3039 if (TREE_CODE (type) == POINTER_TYPE
3040 && TREE_CODE (otype) == POINTER_TYPE
3041 && TREE_CODE (expr) == ADDR_EXPR
3042 && DECL_P (TREE_OPERAND (expr, 0))
3043 && flag_strict_aliasing && warn_strict_aliasing
3044 && !VOID_TYPE_P (TREE_TYPE (type)))
3046 /* Casting the address of a decl to non void pointer. Warn
3047 if the cast breaks type based aliasing. */
3048 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3049 warning ("type-punning to incomplete type might break strict-aliasing rules");
3050 else if (!alias_sets_conflict_p
3051 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3052 get_alias_set (TREE_TYPE (type))))
3053 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3057 /* Replace a nonvolatile const static variable with its value. */
3058 if (optimize && TREE_CODE (value) == VAR_DECL)
3059 value = decl_constant_value (value);
3060 value = convert (type, value);
3062 /* Ignore any integer overflow caused by the cast. */
3063 if (TREE_CODE (value) == INTEGER_CST)
3065 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3066 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3070 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3071 if (pedantic && TREE_CODE (value) == INTEGER_CST
3072 && TREE_CODE (expr) == INTEGER_CST
3073 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3074 value = non_lvalue (value);
3076 /* If pedantic, don't let a cast be an lvalue. */
3077 if (value == expr && pedantic)
3078 value = non_lvalue (value);
3083 /* Interpret a cast of expression EXPR to type TYPE. */
3085 c_cast_expr (tree type, tree expr)
3087 int saved_wsp = warn_strict_prototypes;
3089 /* This avoids warnings about unprototyped casts on
3090 integers. E.g. "#define SIG_DFL (void(*)())0". */
3091 if (TREE_CODE (expr) == INTEGER_CST)
3092 warn_strict_prototypes = 0;
3093 type = groktypename (type);
3094 warn_strict_prototypes = saved_wsp;
3096 return build_c_cast (type, expr);
3100 /* Build an assignment expression of lvalue LHS from value RHS.
3101 MODIFYCODE is the code for a binary operator that we use
3102 to combine the old value of LHS with RHS to get the new value.
3103 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3106 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3110 tree lhstype = TREE_TYPE (lhs);
3111 tree olhstype = lhstype;
3113 /* Types that aren't fully specified cannot be used in assignments. */
3114 lhs = require_complete_type (lhs);
3116 /* Avoid duplicate error messages from operands that had errors. */
3117 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3118 return error_mark_node;
3120 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3121 /* Do not use STRIP_NOPS here. We do not want an enumerator
3122 whose value is 0 to count as a null pointer constant. */
3123 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3124 rhs = TREE_OPERAND (rhs, 0);
3128 /* Handle control structure constructs used as "lvalues". */
3130 switch (TREE_CODE (lhs))
3132 /* Handle (a, b) used as an "lvalue". */
3134 pedantic_lvalue_warning (COMPOUND_EXPR);
3135 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3136 if (TREE_CODE (newrhs) == ERROR_MARK)
3137 return error_mark_node;
3138 return build (COMPOUND_EXPR, lhstype,
3139 TREE_OPERAND (lhs, 0), newrhs);
3141 /* Handle (a ? b : c) used as an "lvalue". */
3143 pedantic_lvalue_warning (COND_EXPR);
3144 rhs = save_expr (rhs);
3146 /* Produce (a ? (b = rhs) : (c = rhs))
3147 except that the RHS goes through a save-expr
3148 so the code to compute it is only emitted once. */
3150 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3151 build_modify_expr (TREE_OPERAND (lhs, 1),
3153 build_modify_expr (TREE_OPERAND (lhs, 2),
3155 if (TREE_CODE (cond) == ERROR_MARK)
3157 /* Make sure the code to compute the rhs comes out
3158 before the split. */
3159 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3160 /* But cast it to void to avoid an "unused" error. */
3161 convert (void_type_node, rhs), cond);
3167 /* If a binary op has been requested, combine the old LHS value with the RHS
3168 producing the value we should actually store into the LHS. */
3170 if (modifycode != NOP_EXPR)
3172 lhs = stabilize_reference (lhs);
3173 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3176 /* Handle a cast used as an "lvalue".
3177 We have already performed any binary operator using the value as cast.
3178 Now convert the result to the cast type of the lhs,
3179 and then true type of the lhs and store it there;
3180 then convert result back to the cast type to be the value
3181 of the assignment. */
3183 switch (TREE_CODE (lhs))
3188 case FIX_TRUNC_EXPR:
3189 case FIX_FLOOR_EXPR:
3190 case FIX_ROUND_EXPR:
3192 newrhs = default_function_array_conversion (newrhs);
3194 tree inner_lhs = TREE_OPERAND (lhs, 0);
3196 result = build_modify_expr (inner_lhs, NOP_EXPR,
3197 convert (TREE_TYPE (inner_lhs),
3198 convert (lhstype, newrhs)));
3199 if (TREE_CODE (result) == ERROR_MARK)
3201 pedantic_lvalue_warning (CONVERT_EXPR);
3202 return convert (TREE_TYPE (lhs), result);
3209 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3210 Reject anything strange now. */
3212 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3213 return error_mark_node;
3215 /* Warn about storing in something that is `const'. */
3217 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3218 || ((TREE_CODE (lhstype) == RECORD_TYPE
3219 || TREE_CODE (lhstype) == UNION_TYPE)
3220 && C_TYPE_FIELDS_READONLY (lhstype)))
3221 readonly_warning (lhs, "assignment");
3223 /* If storing into a structure or union member,
3224 it has probably been given type `int'.
3225 Compute the type that would go with
3226 the actual amount of storage the member occupies. */
3228 if (TREE_CODE (lhs) == COMPONENT_REF
3229 && (TREE_CODE (lhstype) == INTEGER_TYPE
3230 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3231 || TREE_CODE (lhstype) == REAL_TYPE
3232 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3233 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3235 /* If storing in a field that is in actuality a short or narrower than one,
3236 we must store in the field in its actual type. */
3238 if (lhstype != TREE_TYPE (lhs))
3240 lhs = copy_node (lhs);
3241 TREE_TYPE (lhs) = lhstype;
3244 /* Convert new value to destination type. */
3246 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3247 NULL_TREE, NULL_TREE, 0);
3248 if (TREE_CODE (newrhs) == ERROR_MARK)
3249 return error_mark_node;
3253 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3254 TREE_SIDE_EFFECTS (result) = 1;
3256 /* If we got the LHS in a different type for storing in,
3257 convert the result back to the nominal type of LHS
3258 so that the value we return always has the same type
3259 as the LHS argument. */
3261 if (olhstype == TREE_TYPE (result))
3263 return convert_for_assignment (olhstype, result, _("assignment"),
3264 NULL_TREE, NULL_TREE, 0);
3267 /* Convert value RHS to type TYPE as preparation for an assignment
3268 to an lvalue of type TYPE.
3269 The real work of conversion is done by `convert'.
3270 The purpose of this function is to generate error messages
3271 for assignments that are not allowed in C.
3272 ERRTYPE is a string to use in error messages:
3273 "assignment", "return", etc. If it is null, this is parameter passing
3274 for a function call (and different error messages are output).
3276 FUNNAME is the name of the function being called,
3277 as an IDENTIFIER_NODE, or null.
3278 PARMNUM is the number of the argument, for printing in error messages. */
3281 convert_for_assignment (tree type, tree rhs, const char *errtype,
3282 tree fundecl, tree funname, int parmnum)
3284 enum tree_code codel = TREE_CODE (type);
3286 enum tree_code coder;
3288 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3289 /* Do not use STRIP_NOPS here. We do not want an enumerator
3290 whose value is 0 to count as a null pointer constant. */
3291 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3292 rhs = TREE_OPERAND (rhs, 0);
3294 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3295 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3296 rhs = default_conversion (rhs);
3297 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3298 rhs = decl_constant_value_for_broken_optimization (rhs);
3300 rhstype = TREE_TYPE (rhs);
3301 coder = TREE_CODE (rhstype);
3303 if (coder == ERROR_MARK)
3304 return error_mark_node;
3306 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3308 overflow_warning (rhs);
3309 /* Check for Objective-C protocols. This will automatically
3310 issue a warning if there are protocol violations. No need to
3311 use the return value. */
3312 if (c_dialect_objc ())
3313 objc_comptypes (type, rhstype, 0);
3317 if (coder == VOID_TYPE)
3319 error ("void value not ignored as it ought to be");
3320 return error_mark_node;
3322 /* A type converts to a reference to it.
3323 This code doesn't fully support references, it's just for the
3324 special case of va_start and va_copy. */
3325 if (codel == REFERENCE_TYPE
3326 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3328 if (!lvalue_p (rhs))
3330 error ("cannot pass rvalue to reference parameter");
3331 return error_mark_node;
3333 if (!c_mark_addressable (rhs))
3334 return error_mark_node;
3335 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3337 /* We already know that these two types are compatible, but they
3338 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3339 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3340 likely to be va_list, a typedef to __builtin_va_list, which
3341 is different enough that it will cause problems later. */
3342 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3343 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3345 rhs = build1 (NOP_EXPR, type, rhs);
3348 /* Some types can interconvert without explicit casts. */
3349 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3350 && ((*targetm.vector_opaque_p) (type)
3351 || (*targetm.vector_opaque_p) (rhstype)))
3352 return convert (type, rhs);
3353 /* Arithmetic types all interconvert, and enum is treated like int. */
3354 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3355 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3356 || codel == BOOLEAN_TYPE)
3357 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3358 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3359 || coder == BOOLEAN_TYPE))
3360 return convert_and_check (type, rhs);
3362 /* Conversion to a transparent union from its member types.
3363 This applies only to function arguments. */
3364 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3367 tree marginal_memb_type = 0;
3369 for (memb_types = TYPE_FIELDS (type); memb_types;
3370 memb_types = TREE_CHAIN (memb_types))
3372 tree memb_type = TREE_TYPE (memb_types);
3374 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3375 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3378 if (TREE_CODE (memb_type) != POINTER_TYPE)
3381 if (coder == POINTER_TYPE)
3383 tree ttl = TREE_TYPE (memb_type);
3384 tree ttr = TREE_TYPE (rhstype);
3386 /* Any non-function converts to a [const][volatile] void *
3387 and vice versa; otherwise, targets must be the same.
3388 Meanwhile, the lhs target must have all the qualifiers of
3390 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3391 || comp_target_types (memb_type, rhstype, 0))
3393 /* If this type won't generate any warnings, use it. */
3394 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3395 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3396 && TREE_CODE (ttl) == FUNCTION_TYPE)
3397 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3398 == TYPE_QUALS (ttr))
3399 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3400 == TYPE_QUALS (ttl))))
3403 /* Keep looking for a better type, but remember this one. */
3404 if (! marginal_memb_type)
3405 marginal_memb_type = memb_type;
3409 /* Can convert integer zero to any pointer type. */
3410 if (integer_zerop (rhs)
3411 || (TREE_CODE (rhs) == NOP_EXPR
3412 && integer_zerop (TREE_OPERAND (rhs, 0))))
3414 rhs = null_pointer_node;
3419 if (memb_types || marginal_memb_type)
3423 /* We have only a marginally acceptable member type;
3424 it needs a warning. */
3425 tree ttl = TREE_TYPE (marginal_memb_type);
3426 tree ttr = TREE_TYPE (rhstype);
3428 /* Const and volatile mean something different for function
3429 types, so the usual warnings are not appropriate. */
3430 if (TREE_CODE (ttr) == FUNCTION_TYPE
3431 && TREE_CODE (ttl) == FUNCTION_TYPE)
3433 /* Because const and volatile on functions are
3434 restrictions that say the function will not do
3435 certain things, it is okay to use a const or volatile
3436 function where an ordinary one is wanted, but not
3438 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3439 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3440 errtype, funname, parmnum);
3442 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3443 warn_for_assignment ("%s discards qualifiers from pointer target type",
3448 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3449 pedwarn ("ISO C prohibits argument conversion to union type");
3451 return build1 (NOP_EXPR, type, rhs);
3455 /* Conversions among pointers */
3456 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3457 && (coder == codel))
3459 tree ttl = TREE_TYPE (type);
3460 tree ttr = TREE_TYPE (rhstype);
3461 bool is_opaque_pointer;
3463 /* Opaque pointers are treated like void pointers. */
3464 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
3465 || (*targetm.vector_opaque_p) (rhstype))
3466 && TREE_CODE (ttl) == VECTOR_TYPE
3467 && TREE_CODE (ttr) == VECTOR_TYPE;
3469 /* Any non-function converts to a [const][volatile] void *
3470 and vice versa; otherwise, targets must be the same.
3471 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3472 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3473 || comp_target_types (type, rhstype, 0)
3474 || is_opaque_pointer
3475 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3476 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3479 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3482 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3483 which are not ANSI null ptr constants. */
3484 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3485 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3486 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3487 errtype, funname, parmnum);
3488 /* Const and volatile mean something different for function types,
3489 so the usual warnings are not appropriate. */
3490 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3491 && TREE_CODE (ttl) != FUNCTION_TYPE)
3493 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3494 warn_for_assignment ("%s discards qualifiers from pointer target type",
3495 errtype, funname, parmnum);
3496 /* If this is not a case of ignoring a mismatch in signedness,
3498 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3499 || comp_target_types (type, rhstype, 0))
3501 /* If there is a mismatch, do warn. */
3503 warn_for_assignment ("pointer targets in %s differ in signedness",
3504 errtype, funname, parmnum);
3506 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3507 && TREE_CODE (ttr) == FUNCTION_TYPE)
3509 /* Because const and volatile on functions are restrictions
3510 that say the function will not do certain things,
3511 it is okay to use a const or volatile function
3512 where an ordinary one is wanted, but not vice-versa. */
3513 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3514 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3515 errtype, funname, parmnum);
3519 warn_for_assignment ("%s from incompatible pointer type",
3520 errtype, funname, parmnum);
3521 return convert (type, rhs);
3523 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3525 /* An explicit constant 0 can convert to a pointer,
3526 or one that results from arithmetic, even including
3527 a cast to integer type. */
3528 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3530 ! (TREE_CODE (rhs) == NOP_EXPR
3531 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3532 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3533 && integer_zerop (TREE_OPERAND (rhs, 0))))
3535 warn_for_assignment ("%s makes pointer from integer without a cast",
3536 errtype, funname, parmnum);
3537 return convert (type, rhs);
3539 return null_pointer_node;
3541 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3543 warn_for_assignment ("%s makes integer from pointer without a cast",
3544 errtype, funname, parmnum);
3545 return convert (type, rhs);
3547 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3548 return convert (type, rhs);
3554 tree selector = objc_message_selector ();
3556 if (selector && parmnum > 2)
3557 error ("incompatible type for argument %d of `%s'",
3558 parmnum - 2, IDENTIFIER_POINTER (selector));
3560 error ("incompatible type for argument %d of `%s'",
3561 parmnum, IDENTIFIER_POINTER (funname));
3564 error ("incompatible type for argument %d of indirect function call",
3568 error ("incompatible types in %s", errtype);
3570 return error_mark_node;
3573 /* Convert VALUE for assignment into inlined parameter PARM. */
3576 c_convert_parm_for_inlining (tree parm, tree value, tree fn)
3580 /* If FN was prototyped, the value has been converted already
3581 in convert_arguments. */
3582 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3585 type = TREE_TYPE (parm);
3586 ret = convert_for_assignment (type, value,
3587 (char *) 0 /* arg passing */, fn,
3589 if (PROMOTE_PROTOTYPES
3590 && INTEGRAL_TYPE_P (type)
3591 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3592 ret = default_conversion (ret);
3596 /* Print a warning using MSGID.
3597 It gets OPNAME as its one parameter.
3598 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3599 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3600 FUNCTION and ARGNUM are handled specially if we are building an
3601 Objective-C selector. */
3604 warn_for_assignment (const char *msgid, const char *opname, tree function,
3609 tree selector = objc_message_selector ();
3612 if (selector && argnum > 2)
3614 function = selector;
3621 /* Function name is known; supply it. */
3622 const char *const argstring = _("passing arg of `%s'");
3623 new_opname = alloca (IDENTIFIER_LENGTH (function)
3624 + strlen (argstring) + 1 + 1);
3625 sprintf (new_opname, argstring,
3626 IDENTIFIER_POINTER (function));
3630 /* Function name unknown (call through ptr). */
3631 const char *const argnofun = _("passing arg of pointer to function");
3632 new_opname = alloca (strlen (argnofun) + 1 + 1);
3633 sprintf (new_opname, argnofun);
3638 /* Function name is known; supply it. */
3639 const char *const argstring = _("passing arg %d of `%s'");
3640 new_opname = alloca (IDENTIFIER_LENGTH (function)
3641 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3642 sprintf (new_opname, argstring, argnum,
3643 IDENTIFIER_POINTER (function));
3647 /* Function name unknown (call through ptr); just give arg number. */
3648 const char *const argnofun = _("passing arg %d of pointer to function");
3649 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3650 sprintf (new_opname, argnofun, argnum);
3652 opname = new_opname;
3654 pedwarn (msgid, opname);
3657 /* If VALUE is a compound expr all of whose expressions are constant, then
3658 return its value. Otherwise, return error_mark_node.
3660 This is for handling COMPOUND_EXPRs as initializer elements
3661 which is allowed with a warning when -pedantic is specified. */
3664 valid_compound_expr_initializer (tree value, tree endtype)
3666 if (TREE_CODE (value) == COMPOUND_EXPR)
3668 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3670 return error_mark_node;
3671 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3674 else if (! TREE_CONSTANT (value)
3675 && ! initializer_constant_valid_p (value, endtype))
3676 return error_mark_node;
3681 /* Perform appropriate conversions on the initial value of a variable,
3682 store it in the declaration DECL,
3683 and print any error messages that are appropriate.
3684 If the init is invalid, store an ERROR_MARK. */
3687 store_init_value (tree decl, tree init)
3691 /* If variable's type was invalidly declared, just ignore it. */
3693 type = TREE_TYPE (decl);
3694 if (TREE_CODE (type) == ERROR_MARK)
3697 /* Digest the specified initializer into an expression. */
3699 value = digest_init (type, init, TREE_STATIC (decl));
3701 /* Store the expression if valid; else report error. */
3703 if (warn_traditional && !in_system_header
3704 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3705 warning ("traditional C rejects automatic aggregate initialization");
3707 DECL_INITIAL (decl) = value;
3709 /* ANSI wants warnings about out-of-range constant initializers. */
3710 STRIP_TYPE_NOPS (value);
3711 constant_expression_warning (value);
3713 /* Check if we need to set array size from compound literal size. */
3714 if (TREE_CODE (type) == ARRAY_TYPE
3715 && TYPE_DOMAIN (type) == 0
3716 && value != error_mark_node)
3718 tree inside_init = init;
3720 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3721 inside_init = TREE_OPERAND (init, 0);
3722 inside_init = fold (inside_init);
3724 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3726 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3728 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3730 /* For int foo[] = (int [3]){1}; we need to set array size
3731 now since later on array initializer will be just the
3732 brace enclosed list of the compound literal. */
3733 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3735 layout_decl (decl, 0);
3741 /* Methods for storing and printing names for error messages. */
3743 /* Implement a spelling stack that allows components of a name to be pushed
3744 and popped. Each element on the stack is this structure. */
3756 #define SPELLING_STRING 1
3757 #define SPELLING_MEMBER 2
3758 #define SPELLING_BOUNDS 3
3760 static struct spelling *spelling; /* Next stack element (unused). */
3761 static struct spelling *spelling_base; /* Spelling stack base. */
3762 static int spelling_size; /* Size of the spelling stack. */
3764 /* Macros to save and restore the spelling stack around push_... functions.
3765 Alternative to SAVE_SPELLING_STACK. */
3767 #define SPELLING_DEPTH() (spelling - spelling_base)
3768 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3770 /* Push an element on the spelling stack with type KIND and assign VALUE
3773 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3775 int depth = SPELLING_DEPTH (); \
3777 if (depth >= spelling_size) \
3779 spelling_size += 10; \
3780 if (spelling_base == 0) \
3781 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3783 spelling_base = xrealloc (spelling_base, \
3784 spelling_size * sizeof (struct spelling)); \
3785 RESTORE_SPELLING_DEPTH (depth); \
3788 spelling->kind = (KIND); \
3789 spelling->MEMBER = (VALUE); \
3793 /* Push STRING on the stack. Printed literally. */
3796 push_string (const char *string)
3798 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3801 /* Push a member name on the stack. Printed as '.' STRING. */
3804 push_member_name (tree decl)
3806 const char *const string
3807 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3808 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3811 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3814 push_array_bounds (int bounds)
3816 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3819 /* Compute the maximum size in bytes of the printed spelling. */
3822 spelling_length (void)
3827 for (p = spelling_base; p < spelling; p++)
3829 if (p->kind == SPELLING_BOUNDS)
3832 size += strlen (p->u.s) + 1;
3838 /* Print the spelling to BUFFER and return it. */
3841 print_spelling (char *buffer)
3846 for (p = spelling_base; p < spelling; p++)
3847 if (p->kind == SPELLING_BOUNDS)
3849 sprintf (d, "[%d]", p->u.i);
3855 if (p->kind == SPELLING_MEMBER)
3857 for (s = p->u.s; (*d = *s++); d++)
3864 /* Issue an error message for a bad initializer component.
3865 MSGID identifies the message.
3866 The component name is taken from the spelling stack. */
3869 error_init (const char *msgid)
3873 error ("%s", _(msgid));
3874 ofwhat = print_spelling (alloca (spelling_length () + 1));
3876 error ("(near initialization for `%s')", ofwhat);
3879 /* Issue a pedantic warning for a bad initializer component.
3880 MSGID identifies the message.
3881 The component name is taken from the spelling stack. */
3884 pedwarn_init (const char *msgid)
3888 pedwarn ("%s", _(msgid));
3889 ofwhat = print_spelling (alloca (spelling_length () + 1));
3891 pedwarn ("(near initialization for `%s')", ofwhat);
3894 /* Issue a warning for a bad initializer component.
3895 MSGID identifies the message.
3896 The component name is taken from the spelling stack. */
3899 warning_init (const char *msgid)
3903 warning ("%s", _(msgid));
3904 ofwhat = print_spelling (alloca (spelling_length () + 1));
3906 warning ("(near initialization for `%s')", ofwhat);
3909 /* Digest the parser output INIT as an initializer for type TYPE.
3910 Return a C expression of type TYPE to represent the initial value.
3912 REQUIRE_CONSTANT requests an error if non-constant initializers or
3913 elements are seen. */
3916 digest_init (tree type, tree init, int require_constant)
3918 enum tree_code code = TREE_CODE (type);
3919 tree inside_init = init;
3921 if (type == error_mark_node
3922 || init == error_mark_node
3923 || TREE_TYPE (init) == error_mark_node)
3924 return error_mark_node;
3926 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3927 /* Do not use STRIP_NOPS here. We do not want an enumerator
3928 whose value is 0 to count as a null pointer constant. */
3929 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3930 inside_init = TREE_OPERAND (init, 0);
3932 inside_init = fold (inside_init);
3934 /* Initialization of an array of chars from a string constant
3935 optionally enclosed in braces. */
3937 if (code == ARRAY_TYPE)
3939 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3940 if ((typ1 == char_type_node
3941 || typ1 == signed_char_type_node
3942 || typ1 == unsigned_char_type_node
3943 || typ1 == unsigned_wchar_type_node
3944 || typ1 == signed_wchar_type_node)
3945 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3947 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3948 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3951 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3953 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3955 error_init ("char-array initialized from wide string");
3956 return error_mark_node;
3958 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3960 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3962 error_init ("int-array initialized from non-wide string");
3963 return error_mark_node;
3966 TREE_TYPE (inside_init) = type;
3967 if (TYPE_DOMAIN (type) != 0
3968 && TYPE_SIZE (type) != 0
3969 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3970 /* Subtract 1 (or sizeof (wchar_t))
3971 because it's ok to ignore the terminating null char
3972 that is counted in the length of the constant. */
3973 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3974 TREE_STRING_LENGTH (inside_init)
3975 - ((TYPE_PRECISION (typ1)
3976 != TYPE_PRECISION (char_type_node))
3977 ? (TYPE_PRECISION (wchar_type_node)
3980 pedwarn_init ("initializer-string for array of chars is too long");
3986 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3987 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3988 below and handle as a constructor. */
3989 if (code == VECTOR_TYPE
3990 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
3991 && TREE_CONSTANT (inside_init))
3993 if (TREE_CODE (inside_init) == VECTOR_CST
3994 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3995 TYPE_MAIN_VARIANT (type),
3999 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4002 /* Any type can be initialized
4003 from an expression of the same type, optionally with braces. */
4005 if (inside_init && TREE_TYPE (inside_init) != 0
4006 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4007 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
4008 || (code == ARRAY_TYPE
4009 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4010 || (code == VECTOR_TYPE
4011 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4012 || (code == POINTER_TYPE
4013 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4014 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4015 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4016 TREE_TYPE (type), COMPARE_STRICT))))
4018 if (code == POINTER_TYPE)
4019 inside_init = default_function_array_conversion (inside_init);
4021 if (code == VECTOR_TYPE)
4022 /* Although the types are compatible, we may require a
4024 inside_init = convert (type, inside_init);
4026 if (require_constant && !flag_isoc99
4027 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4029 /* As an extension, allow initializing objects with static storage
4030 duration with compound literals (which are then treated just as
4031 the brace enclosed list they contain). */
4032 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4033 inside_init = DECL_INITIAL (decl);
4036 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4037 && TREE_CODE (inside_init) != CONSTRUCTOR)
4039 error_init ("array initialized from non-constant array expression");
4040 return error_mark_node;
4043 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4044 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4046 /* Compound expressions can only occur here if -pedantic or
4047 -pedantic-errors is specified. In the later case, we always want
4048 an error. In the former case, we simply want a warning. */
4049 if (require_constant && pedantic
4050 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4053 = valid_compound_expr_initializer (inside_init,
4054 TREE_TYPE (inside_init));
4055 if (inside_init == error_mark_node)
4056 error_init ("initializer element is not constant");
4058 pedwarn_init ("initializer element is not constant");
4059 if (flag_pedantic_errors)
4060 inside_init = error_mark_node;
4062 else if (require_constant
4063 && (!TREE_CONSTANT (inside_init)
4064 /* This test catches things like `7 / 0' which
4065 result in an expression for which TREE_CONSTANT
4066 is true, but which is not actually something
4067 that is a legal constant. We really should not
4068 be using this function, because it is a part of
4069 the back-end. Instead, the expression should
4070 already have been turned into ERROR_MARK_NODE. */
4071 || !initializer_constant_valid_p (inside_init,
4072 TREE_TYPE (inside_init))))
4074 error_init ("initializer element is not constant");
4075 inside_init = error_mark_node;
4081 /* Handle scalar types, including conversions. */
4083 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4084 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4086 /* Note that convert_for_assignment calls default_conversion
4087 for arrays and functions. We must not call it in the
4088 case where inside_init is a null pointer constant. */
4090 = convert_for_assignment (type, init, _("initialization"),
4091 NULL_TREE, NULL_TREE, 0);
4093 if (require_constant && ! TREE_CONSTANT (inside_init))
4095 error_init ("initializer element is not constant");
4096 inside_init = error_mark_node;
4098 else if (require_constant
4099 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4101 error_init ("initializer element is not computable at load time");
4102 inside_init = error_mark_node;
4108 /* Come here only for records and arrays. */
4110 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4112 error_init ("variable-sized object may not be initialized");
4113 return error_mark_node;
4116 error_init ("invalid initializer");
4117 return error_mark_node;
4120 /* Handle initializers that use braces. */
4122 /* Type of object we are accumulating a constructor for.
4123 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4124 static tree constructor_type;
4126 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4128 static tree constructor_fields;
4130 /* For an ARRAY_TYPE, this is the specified index
4131 at which to store the next element we get. */
4132 static tree constructor_index;
4134 /* For an ARRAY_TYPE, this is the maximum index. */
4135 static tree constructor_max_index;
4137 /* For a RECORD_TYPE, this is the first field not yet written out. */
4138 static tree constructor_unfilled_fields;
4140 /* For an ARRAY_TYPE, this is the index of the first element
4141 not yet written out. */
4142 static tree constructor_unfilled_index;
4144 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4145 This is so we can generate gaps between fields, when appropriate. */
4146 static tree constructor_bit_index;
4148 /* If we are saving up the elements rather than allocating them,
4149 this is the list of elements so far (in reverse order,
4150 most recent first). */
4151 static tree constructor_elements;
4153 /* 1 if constructor should be incrementally stored into a constructor chain,
4154 0 if all the elements should be kept in AVL tree. */
4155 static int constructor_incremental;
4157 /* 1 if so far this constructor's elements are all compile-time constants. */
4158 static int constructor_constant;
4160 /* 1 if so far this constructor's elements are all valid address constants. */
4161 static int constructor_simple;
4163 /* 1 if this constructor is erroneous so far. */
4164 static int constructor_erroneous;
4166 /* Structure for managing pending initializer elements, organized as an
4171 struct init_node *left, *right;
4172 struct init_node *parent;
4178 /* Tree of pending elements at this constructor level.
4179 These are elements encountered out of order
4180 which belong at places we haven't reached yet in actually
4182 Will never hold tree nodes across GC runs. */
4183 static struct init_node *constructor_pending_elts;
4185 /* The SPELLING_DEPTH of this constructor. */
4186 static int constructor_depth;
4188 /* 0 if implicitly pushing constructor levels is allowed. */
4189 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4191 static int require_constant_value;
4192 static int require_constant_elements;
4194 /* DECL node for which an initializer is being read.
4195 0 means we are reading a constructor expression
4196 such as (struct foo) {...}. */
4197 static tree constructor_decl;
4199 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4200 static const char *constructor_asmspec;
4202 /* Nonzero if this is an initializer for a top-level decl. */
4203 static int constructor_top_level;
4205 /* Nonzero if there were any member designators in this initializer. */
4206 static int constructor_designated;
4208 /* Nesting depth of designator list. */
4209 static int designator_depth;
4211 /* Nonzero if there were diagnosed errors in this designator list. */
4212 static int designator_errorneous;
4215 /* This stack has a level for each implicit or explicit level of
4216 structuring in the initializer, including the outermost one. It
4217 saves the values of most of the variables above. */
4219 struct constructor_range_stack;
4221 struct constructor_stack
4223 struct constructor_stack *next;
4228 tree unfilled_index;
4229 tree unfilled_fields;
4232 struct init_node *pending_elts;
4235 /* If nonzero, this value should replace the entire
4236 constructor at this level. */
4237 tree replacement_value;
4238 struct constructor_range_stack *range_stack;
4248 struct constructor_stack *constructor_stack;
4250 /* This stack represents designators from some range designator up to
4251 the last designator in the list. */
4253 struct constructor_range_stack
4255 struct constructor_range_stack *next, *prev;
4256 struct constructor_stack *stack;
4263 struct constructor_range_stack *constructor_range_stack;
4265 /* This stack records separate initializers that are nested.
4266 Nested initializers can't happen in ANSI C, but GNU C allows them
4267 in cases like { ... (struct foo) { ... } ... }. */
4269 struct initializer_stack
4271 struct initializer_stack *next;
4273 const char *asmspec;
4274 struct constructor_stack *constructor_stack;
4275 struct constructor_range_stack *constructor_range_stack;
4277 struct spelling *spelling;
4278 struct spelling *spelling_base;
4281 char require_constant_value;
4282 char require_constant_elements;
4285 struct initializer_stack *initializer_stack;
4287 /* Prepare to parse and output the initializer for variable DECL. */
4290 start_init (tree decl, tree asmspec_tree, int top_level)
4293 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4294 const char *asmspec = 0;
4297 asmspec = TREE_STRING_POINTER (asmspec_tree);
4299 p->decl = constructor_decl;
4300 p->asmspec = constructor_asmspec;
4301 p->require_constant_value = require_constant_value;
4302 p->require_constant_elements = require_constant_elements;
4303 p->constructor_stack = constructor_stack;
4304 p->constructor_range_stack = constructor_range_stack;
4305 p->elements = constructor_elements;
4306 p->spelling = spelling;
4307 p->spelling_base = spelling_base;
4308 p->spelling_size = spelling_size;
4309 p->top_level = constructor_top_level;
4310 p->next = initializer_stack;
4311 initializer_stack = p;
4313 constructor_decl = decl;
4314 constructor_asmspec = asmspec;
4315 constructor_designated = 0;
4316 constructor_top_level = top_level;
4320 require_constant_value = TREE_STATIC (decl);
4321 require_constant_elements
4322 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4323 /* For a scalar, you can always use any value to initialize,
4324 even within braces. */
4325 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4326 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4327 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4328 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4329 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4333 require_constant_value = 0;
4334 require_constant_elements = 0;
4335 locus = "(anonymous)";
4338 constructor_stack = 0;
4339 constructor_range_stack = 0;
4341 missing_braces_mentioned = 0;
4345 RESTORE_SPELLING_DEPTH (0);
4348 push_string (locus);
4354 struct initializer_stack *p = initializer_stack;
4356 /* Free the whole constructor stack of this initializer. */
4357 while (constructor_stack)
4359 struct constructor_stack *q = constructor_stack;
4360 constructor_stack = q->next;
4364 if (constructor_range_stack)
4367 /* Pop back to the data of the outer initializer (if any). */
4368 constructor_decl = p->decl;
4369 constructor_asmspec = p->asmspec;
4370 require_constant_value = p->require_constant_value;
4371 require_constant_elements = p->require_constant_elements;
4372 constructor_stack = p->constructor_stack;
4373 constructor_range_stack = p->constructor_range_stack;
4374 constructor_elements = p->elements;
4375 spelling = p->spelling;
4376 spelling_base = p->spelling_base;
4377 spelling_size = p->spelling_size;
4378 constructor_top_level = p->top_level;
4379 initializer_stack = p->next;
4383 /* Call here when we see the initializer is surrounded by braces.
4384 This is instead of a call to push_init_level;
4385 it is matched by a call to pop_init_level.
4387 TYPE is the type to initialize, for a constructor expression.
4388 For an initializer for a decl, TYPE is zero. */
4391 really_start_incremental_init (tree type)
4393 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4396 type = TREE_TYPE (constructor_decl);
4398 if ((*targetm.vector_opaque_p) (type))
4399 error ("opaque vector types cannot be initialized");
4401 p->type = constructor_type;
4402 p->fields = constructor_fields;
4403 p->index = constructor_index;
4404 p->max_index = constructor_max_index;
4405 p->unfilled_index = constructor_unfilled_index;
4406 p->unfilled_fields = constructor_unfilled_fields;
4407 p->bit_index = constructor_bit_index;
4408 p->elements = constructor_elements;
4409 p->constant = constructor_constant;
4410 p->simple = constructor_simple;
4411 p->erroneous = constructor_erroneous;
4412 p->pending_elts = constructor_pending_elts;
4413 p->depth = constructor_depth;
4414 p->replacement_value = 0;
4418 p->incremental = constructor_incremental;
4419 p->designated = constructor_designated;
4421 constructor_stack = p;
4423 constructor_constant = 1;
4424 constructor_simple = 1;
4425 constructor_depth = SPELLING_DEPTH ();
4426 constructor_elements = 0;
4427 constructor_pending_elts = 0;
4428 constructor_type = type;
4429 constructor_incremental = 1;
4430 constructor_designated = 0;
4431 designator_depth = 0;
4432 designator_errorneous = 0;
4434 if (TREE_CODE (constructor_type) == RECORD_TYPE
4435 || TREE_CODE (constructor_type) == UNION_TYPE)
4437 constructor_fields = TYPE_FIELDS (constructor_type);
4438 /* Skip any nameless bit fields at the beginning. */
4439 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4440 && DECL_NAME (constructor_fields) == 0)
4441 constructor_fields = TREE_CHAIN (constructor_fields);
4443 constructor_unfilled_fields = constructor_fields;
4444 constructor_bit_index = bitsize_zero_node;
4446 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4448 if (TYPE_DOMAIN (constructor_type))
4450 constructor_max_index
4451 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4453 /* Detect non-empty initializations of zero-length arrays. */
4454 if (constructor_max_index == NULL_TREE
4455 && TYPE_SIZE (constructor_type))
4456 constructor_max_index = build_int_2 (-1, -1);
4458 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4459 to initialize VLAs will cause a proper error; avoid tree
4460 checking errors as well by setting a safe value. */
4461 if (constructor_max_index
4462 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4463 constructor_max_index = build_int_2 (-1, -1);
4466 = convert (bitsizetype,
4467 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4470 constructor_index = bitsize_zero_node;
4472 constructor_unfilled_index = constructor_index;
4474 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4476 /* Vectors are like simple fixed-size arrays. */
4477 constructor_max_index =
4478 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4479 constructor_index = convert (bitsizetype, bitsize_zero_node);
4480 constructor_unfilled_index = constructor_index;
4484 /* Handle the case of int x = {5}; */
4485 constructor_fields = constructor_type;
4486 constructor_unfilled_fields = constructor_type;
4490 /* Push down into a subobject, for initialization.
4491 If this is for an explicit set of braces, IMPLICIT is 0.
4492 If it is because the next element belongs at a lower level,
4493 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4496 push_init_level (int implicit)
4498 struct constructor_stack *p;
4499 tree value = NULL_TREE;
4501 /* If we've exhausted any levels that didn't have braces,
4503 while (constructor_stack->implicit)
4505 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4506 || TREE_CODE (constructor_type) == UNION_TYPE)
4507 && constructor_fields == 0)
4508 process_init_element (pop_init_level (1));
4509 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4510 && constructor_max_index
4511 && tree_int_cst_lt (constructor_max_index, constructor_index))
4512 process_init_element (pop_init_level (1));
4517 /* Unless this is an explicit brace, we need to preserve previous
4521 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4522 || TREE_CODE (constructor_type) == UNION_TYPE)
4523 && constructor_fields)
4524 value = find_init_member (constructor_fields);
4525 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4526 value = find_init_member (constructor_index);
4529 p = xmalloc (sizeof (struct constructor_stack));
4530 p->type = constructor_type;
4531 p->fields = constructor_fields;
4532 p->index = constructor_index;
4533 p->max_index = constructor_max_index;
4534 p->unfilled_index = constructor_unfilled_index;
4535 p->unfilled_fields = constructor_unfilled_fields;
4536 p->bit_index = constructor_bit_index;
4537 p->elements = constructor_elements;
4538 p->constant = constructor_constant;
4539 p->simple = constructor_simple;
4540 p->erroneous = constructor_erroneous;
4541 p->pending_elts = constructor_pending_elts;
4542 p->depth = constructor_depth;
4543 p->replacement_value = 0;
4544 p->implicit = implicit;
4546 p->incremental = constructor_incremental;
4547 p->designated = constructor_designated;
4548 p->next = constructor_stack;
4550 constructor_stack = p;
4552 constructor_constant = 1;
4553 constructor_simple = 1;
4554 constructor_depth = SPELLING_DEPTH ();
4555 constructor_elements = 0;
4556 constructor_incremental = 1;
4557 constructor_designated = 0;
4558 constructor_pending_elts = 0;
4561 p->range_stack = constructor_range_stack;
4562 constructor_range_stack = 0;
4563 designator_depth = 0;
4564 designator_errorneous = 0;
4567 /* Don't die if an entire brace-pair level is superfluous
4568 in the containing level. */
4569 if (constructor_type == 0)
4571 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4572 || TREE_CODE (constructor_type) == UNION_TYPE)
4574 /* Don't die if there are extra init elts at the end. */
4575 if (constructor_fields == 0)
4576 constructor_type = 0;
4579 constructor_type = TREE_TYPE (constructor_fields);
4580 push_member_name (constructor_fields);
4581 constructor_depth++;
4584 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4586 constructor_type = TREE_TYPE (constructor_type);
4587 push_array_bounds (tree_low_cst (constructor_index, 0));
4588 constructor_depth++;
4591 if (constructor_type == 0)
4593 error_init ("extra brace group at end of initializer");
4594 constructor_fields = 0;
4595 constructor_unfilled_fields = 0;
4599 if (value && TREE_CODE (value) == CONSTRUCTOR)
4601 constructor_constant = TREE_CONSTANT (value);
4602 constructor_simple = TREE_STATIC (value);
4603 constructor_elements = CONSTRUCTOR_ELTS (value);
4604 if (constructor_elements
4605 && (TREE_CODE (constructor_type) == RECORD_TYPE
4606 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4607 set_nonincremental_init ();
4610 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4612 missing_braces_mentioned = 1;
4613 warning_init ("missing braces around initializer");
4616 if (TREE_CODE (constructor_type) == RECORD_TYPE
4617 || TREE_CODE (constructor_type) == UNION_TYPE)
4619 constructor_fields = TYPE_FIELDS (constructor_type);
4620 /* Skip any nameless bit fields at the beginning. */
4621 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4622 && DECL_NAME (constructor_fields) == 0)
4623 constructor_fields = TREE_CHAIN (constructor_fields);
4625 constructor_unfilled_fields = constructor_fields;
4626 constructor_bit_index = bitsize_zero_node;
4628 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4630 /* Vectors are like simple fixed-size arrays. */
4631 constructor_max_index =
4632 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4633 constructor_index = convert (bitsizetype, integer_zero_node);
4634 constructor_unfilled_index = constructor_index;
4636 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4638 if (TYPE_DOMAIN (constructor_type))
4640 constructor_max_index
4641 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4643 /* Detect non-empty initializations of zero-length arrays. */
4644 if (constructor_max_index == NULL_TREE
4645 && TYPE_SIZE (constructor_type))
4646 constructor_max_index = build_int_2 (-1, -1);
4648 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4649 to initialize VLAs will cause a proper error; avoid tree
4650 checking errors as well by setting a safe value. */
4651 if (constructor_max_index
4652 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4653 constructor_max_index = build_int_2 (-1, -1);
4656 = convert (bitsizetype,
4657 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4660 constructor_index = bitsize_zero_node;
4662 constructor_unfilled_index = constructor_index;
4663 if (value && TREE_CODE (value) == STRING_CST)
4665 /* We need to split the char/wchar array into individual
4666 characters, so that we don't have to special case it
4668 set_nonincremental_init_from_string (value);
4673 warning_init ("braces around scalar initializer");
4674 constructor_fields = constructor_type;
4675 constructor_unfilled_fields = constructor_type;
4679 /* At the end of an implicit or explicit brace level,
4680 finish up that level of constructor.
4681 If we were outputting the elements as they are read, return 0
4682 from inner levels (process_init_element ignores that),
4683 but return error_mark_node from the outermost level
4684 (that's what we want to put in DECL_INITIAL).
4685 Otherwise, return a CONSTRUCTOR expression. */
4688 pop_init_level (int implicit)
4690 struct constructor_stack *p;
4691 tree constructor = 0;
4695 /* When we come to an explicit close brace,
4696 pop any inner levels that didn't have explicit braces. */
4697 while (constructor_stack->implicit)
4698 process_init_element (pop_init_level (1));
4700 if (constructor_range_stack)
4704 p = constructor_stack;
4706 /* Error for initializing a flexible array member, or a zero-length
4707 array member in an inappropriate context. */
4708 if (constructor_type && constructor_fields
4709 && TREE_CODE (constructor_type) == ARRAY_TYPE
4710 && TYPE_DOMAIN (constructor_type)
4711 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4713 /* Silently discard empty initializations. The parser will
4714 already have pedwarned for empty brackets. */
4715 if (integer_zerop (constructor_unfilled_index))
4716 constructor_type = NULL_TREE;
4717 else if (! TYPE_SIZE (constructor_type))
4719 if (constructor_depth > 2)
4720 error_init ("initialization of flexible array member in a nested context");
4722 pedwarn_init ("initialization of a flexible array member");
4724 /* We have already issued an error message for the existence
4725 of a flexible array member not at the end of the structure.
4726 Discard the initializer so that we do not abort later. */
4727 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4728 constructor_type = NULL_TREE;
4731 /* Zero-length arrays are no longer special, so we should no longer
4736 /* Warn when some struct elements are implicitly initialized to zero. */
4739 && TREE_CODE (constructor_type) == RECORD_TYPE
4740 && constructor_unfilled_fields)
4742 /* Do not warn for flexible array members or zero-length arrays. */
4743 while (constructor_unfilled_fields
4744 && (! DECL_SIZE (constructor_unfilled_fields)
4745 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4746 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4748 /* Do not warn if this level of the initializer uses member
4749 designators; it is likely to be deliberate. */
4750 if (constructor_unfilled_fields && !constructor_designated)
4752 push_member_name (constructor_unfilled_fields);
4753 warning_init ("missing initializer");
4754 RESTORE_SPELLING_DEPTH (constructor_depth);
4758 /* Now output all pending elements. */
4759 constructor_incremental = 1;
4760 output_pending_init_elements (1);
4762 /* Pad out the end of the structure. */
4763 if (p->replacement_value)
4764 /* If this closes a superfluous brace pair,
4765 just pass out the element between them. */
4766 constructor = p->replacement_value;
4767 else if (constructor_type == 0)
4769 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4770 && TREE_CODE (constructor_type) != UNION_TYPE
4771 && TREE_CODE (constructor_type) != ARRAY_TYPE
4772 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4774 /* A nonincremental scalar initializer--just return
4775 the element, after verifying there is just one. */
4776 if (constructor_elements == 0)
4778 if (!constructor_erroneous)
4779 error_init ("empty scalar initializer");
4780 constructor = error_mark_node;
4782 else if (TREE_CHAIN (constructor_elements) != 0)
4784 error_init ("extra elements in scalar initializer");
4785 constructor = TREE_VALUE (constructor_elements);
4788 constructor = TREE_VALUE (constructor_elements);
4792 if (constructor_erroneous)
4793 constructor = error_mark_node;
4796 constructor = build_constructor (constructor_type,
4797 nreverse (constructor_elements));
4798 if (constructor_constant)
4799 TREE_CONSTANT (constructor) = 1;
4800 if (constructor_constant && constructor_simple)
4801 TREE_STATIC (constructor) = 1;
4805 constructor_type = p->type;
4806 constructor_fields = p->fields;
4807 constructor_index = p->index;
4808 constructor_max_index = p->max_index;
4809 constructor_unfilled_index = p->unfilled_index;
4810 constructor_unfilled_fields = p->unfilled_fields;
4811 constructor_bit_index = p->bit_index;
4812 constructor_elements = p->elements;
4813 constructor_constant = p->constant;
4814 constructor_simple = p->simple;
4815 constructor_erroneous = p->erroneous;
4816 constructor_incremental = p->incremental;
4817 constructor_designated = p->designated;
4818 constructor_pending_elts = p->pending_elts;
4819 constructor_depth = p->depth;
4821 constructor_range_stack = p->range_stack;
4822 RESTORE_SPELLING_DEPTH (constructor_depth);
4824 constructor_stack = p->next;
4827 if (constructor == 0)
4829 if (constructor_stack == 0)
4830 return error_mark_node;
4836 /* Common handling for both array range and field name designators.
4837 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4840 set_designator (int array)
4843 enum tree_code subcode;
4845 /* Don't die if an entire brace-pair level is superfluous
4846 in the containing level. */
4847 if (constructor_type == 0)
4850 /* If there were errors in this designator list already, bail out silently. */
4851 if (designator_errorneous)
4854 if (!designator_depth)
4856 if (constructor_range_stack)
4859 /* Designator list starts at the level of closest explicit
4861 while (constructor_stack->implicit)
4862 process_init_element (pop_init_level (1));
4863 constructor_designated = 1;
4867 if (constructor_no_implicit)
4869 error_init ("initialization designators may not nest");
4873 if (TREE_CODE (constructor_type) == RECORD_TYPE
4874 || TREE_CODE (constructor_type) == UNION_TYPE)
4876 subtype = TREE_TYPE (constructor_fields);
4877 if (subtype != error_mark_node)
4878 subtype = TYPE_MAIN_VARIANT (subtype);
4880 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4882 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4887 subcode = TREE_CODE (subtype);
4888 if (array && subcode != ARRAY_TYPE)
4890 error_init ("array index in non-array initializer");
4893 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4895 error_init ("field name not in record or union initializer");
4899 constructor_designated = 1;
4900 push_init_level (2);
4904 /* If there are range designators in designator list, push a new designator
4905 to constructor_range_stack. RANGE_END is end of such stack range or
4906 NULL_TREE if there is no range designator at this level. */
4909 push_range_stack (tree range_end)
4911 struct constructor_range_stack *p;
4913 p = ggc_alloc (sizeof (struct constructor_range_stack));
4914 p->prev = constructor_range_stack;
4916 p->fields = constructor_fields;
4917 p->range_start = constructor_index;
4918 p->index = constructor_index;
4919 p->stack = constructor_stack;
4920 p->range_end = range_end;
4921 if (constructor_range_stack)
4922 constructor_range_stack->next = p;
4923 constructor_range_stack = p;
4926 /* Within an array initializer, specify the next index to be initialized.
4927 FIRST is that index. If LAST is nonzero, then initialize a range
4928 of indices, running from FIRST through LAST. */
4931 set_init_index (tree first, tree last)
4933 if (set_designator (1))
4936 designator_errorneous = 1;
4938 while ((TREE_CODE (first) == NOP_EXPR
4939 || TREE_CODE (first) == CONVERT_EXPR
4940 || TREE_CODE (first) == NON_LVALUE_EXPR)
4941 && (TYPE_MODE (TREE_TYPE (first))
4942 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4943 first = TREE_OPERAND (first, 0);
4946 while ((TREE_CODE (last) == NOP_EXPR
4947 || TREE_CODE (last) == CONVERT_EXPR
4948 || TREE_CODE (last) == NON_LVALUE_EXPR)
4949 && (TYPE_MODE (TREE_TYPE (last))
4950 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4951 last = TREE_OPERAND (last, 0);
4953 if (TREE_CODE (first) != INTEGER_CST)
4954 error_init ("nonconstant array index in initializer");
4955 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4956 error_init ("nonconstant array index in initializer");
4957 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4958 error_init ("array index in non-array initializer");
4959 else if (constructor_max_index
4960 && tree_int_cst_lt (constructor_max_index, first))
4961 error_init ("array index in initializer exceeds array bounds");
4964 constructor_index = convert (bitsizetype, first);
4968 if (tree_int_cst_equal (first, last))
4970 else if (tree_int_cst_lt (last, first))
4972 error_init ("empty index range in initializer");
4977 last = convert (bitsizetype, last);
4978 if (constructor_max_index != 0
4979 && tree_int_cst_lt (constructor_max_index, last))
4981 error_init ("array index range in initializer exceeds array bounds");
4988 designator_errorneous = 0;
4989 if (constructor_range_stack || last)
4990 push_range_stack (last);
4994 /* Within a struct initializer, specify the next field to be initialized. */
4997 set_init_label (tree fieldname)
5001 if (set_designator (0))
5004 designator_errorneous = 1;
5006 if (TREE_CODE (constructor_type) != RECORD_TYPE
5007 && TREE_CODE (constructor_type) != UNION_TYPE)
5009 error_init ("field name not in record or union initializer");
5013 for (tail = TYPE_FIELDS (constructor_type); tail;
5014 tail = TREE_CHAIN (tail))
5016 if (DECL_NAME (tail) == fieldname)
5021 error ("unknown field `%s' specified in initializer",
5022 IDENTIFIER_POINTER (fieldname));
5025 constructor_fields = tail;
5027 designator_errorneous = 0;
5028 if (constructor_range_stack)
5029 push_range_stack (NULL_TREE);
5033 /* Add a new initializer to the tree of pending initializers. PURPOSE
5034 identifies the initializer, either array index or field in a structure.
5035 VALUE is the value of that index or field. */
5038 add_pending_init (tree purpose, tree value)
5040 struct init_node *p, **q, *r;
5042 q = &constructor_pending_elts;
5045 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5050 if (tree_int_cst_lt (purpose, p->purpose))
5052 else if (tree_int_cst_lt (p->purpose, purpose))
5056 if (TREE_SIDE_EFFECTS (p->value))
5057 warning_init ("initialized field with side-effects overwritten");
5067 bitpos = bit_position (purpose);
5071 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5073 else if (p->purpose != purpose)
5077 if (TREE_SIDE_EFFECTS (p->value))
5078 warning_init ("initialized field with side-effects overwritten");
5085 r = ggc_alloc (sizeof (struct init_node));
5086 r->purpose = purpose;
5097 struct init_node *s;
5101 if (p->balance == 0)
5103 else if (p->balance < 0)
5110 p->left->parent = p;
5127 constructor_pending_elts = r;
5132 struct init_node *t = r->right;
5136 r->right->parent = r;
5141 p->left->parent = p;
5144 p->balance = t->balance < 0;
5145 r->balance = -(t->balance > 0);
5160 constructor_pending_elts = t;
5166 /* p->balance == +1; growth of left side balances the node. */
5171 else /* r == p->right */
5173 if (p->balance == 0)
5174 /* Growth propagation from right side. */
5176 else if (p->balance > 0)
5183 p->right->parent = p;
5200 constructor_pending_elts = r;
5202 else /* r->balance == -1 */
5205 struct init_node *t = r->left;
5209 r->left->parent = r;
5214 p->right->parent = p;
5217 r->balance = (t->balance < 0);
5218 p->balance = -(t->balance > 0);
5233 constructor_pending_elts = t;
5239 /* p->balance == -1; growth of right side balances the node. */
5250 /* Build AVL tree from a sorted chain. */
5253 set_nonincremental_init (void)
5257 if (TREE_CODE (constructor_type) != RECORD_TYPE
5258 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5261 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5262 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5263 constructor_elements = 0;
5264 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5266 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5267 /* Skip any nameless bit fields at the beginning. */
5268 while (constructor_unfilled_fields != 0
5269 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5270 && DECL_NAME (constructor_unfilled_fields) == 0)
5271 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5274 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5276 if (TYPE_DOMAIN (constructor_type))
5277 constructor_unfilled_index
5278 = convert (bitsizetype,
5279 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5281 constructor_unfilled_index = bitsize_zero_node;
5283 constructor_incremental = 0;
5286 /* Build AVL tree from a string constant. */
5289 set_nonincremental_init_from_string (tree str)
5291 tree value, purpose, type;
5292 HOST_WIDE_INT val[2];
5293 const char *p, *end;
5294 int byte, wchar_bytes, charwidth, bitpos;
5296 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5299 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5300 == TYPE_PRECISION (char_type_node))
5302 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5303 == TYPE_PRECISION (wchar_type_node))
5304 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5308 charwidth = TYPE_PRECISION (char_type_node);
5309 type = TREE_TYPE (constructor_type);
5310 p = TREE_STRING_POINTER (str);
5311 end = p + TREE_STRING_LENGTH (str);
5313 for (purpose = bitsize_zero_node;
5314 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5315 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5317 if (wchar_bytes == 1)
5319 val[1] = (unsigned char) *p++;
5326 for (byte = 0; byte < wchar_bytes; byte++)
5328 if (BYTES_BIG_ENDIAN)
5329 bitpos = (wchar_bytes - byte - 1) * charwidth;
5331 bitpos = byte * charwidth;
5332 val[bitpos < HOST_BITS_PER_WIDE_INT]
5333 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5334 << (bitpos % HOST_BITS_PER_WIDE_INT);
5338 if (!TREE_UNSIGNED (type))
5340 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5341 if (bitpos < HOST_BITS_PER_WIDE_INT)
5343 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5345 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5349 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5354 else if (val[0] & (((HOST_WIDE_INT) 1)
5355 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5356 val[0] |= ((HOST_WIDE_INT) -1)
5357 << (bitpos - HOST_BITS_PER_WIDE_INT);
5360 value = build_int_2 (val[1], val[0]);
5361 TREE_TYPE (value) = type;
5362 add_pending_init (purpose, value);
5365 constructor_incremental = 0;
5368 /* Return value of FIELD in pending initializer or zero if the field was
5369 not initialized yet. */
5372 find_init_member (tree field)
5374 struct init_node *p;
5376 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5378 if (constructor_incremental
5379 && tree_int_cst_lt (field, constructor_unfilled_index))
5380 set_nonincremental_init ();
5382 p = constructor_pending_elts;
5385 if (tree_int_cst_lt (field, p->purpose))
5387 else if (tree_int_cst_lt (p->purpose, field))
5393 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5395 tree bitpos = bit_position (field);
5397 if (constructor_incremental
5398 && (!constructor_unfilled_fields
5399 || tree_int_cst_lt (bitpos,
5400 bit_position (constructor_unfilled_fields))))
5401 set_nonincremental_init ();
5403 p = constructor_pending_elts;
5406 if (field == p->purpose)
5408 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5414 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5416 if (constructor_elements
5417 && TREE_PURPOSE (constructor_elements) == field)
5418 return TREE_VALUE (constructor_elements);
5423 /* "Output" the next constructor element.
5424 At top level, really output it to assembler code now.
5425 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5426 TYPE is the data type that the containing data type wants here.
5427 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5429 PENDING if non-nil means output pending elements that belong
5430 right after this element. (PENDING is normally 1;
5431 it is 0 while outputting pending elements, to avoid recursion.) */
5434 output_init_element (tree value, tree type, tree field, int pending)
5436 if (type == error_mark_node)
5438 constructor_erroneous = 1;
5441 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5442 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5443 && !(TREE_CODE (value) == STRING_CST
5444 && TREE_CODE (type) == ARRAY_TYPE
5445 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5446 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5447 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5448 value = default_conversion (value);
5450 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5451 && require_constant_value && !flag_isoc99 && pending)
5453 /* As an extension, allow initializing objects with static storage
5454 duration with compound literals (which are then treated just as
5455 the brace enclosed list they contain). */
5456 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5457 value = DECL_INITIAL (decl);
5460 if (value == error_mark_node)
5461 constructor_erroneous = 1;
5462 else if (!TREE_CONSTANT (value))
5463 constructor_constant = 0;
5464 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5465 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5466 || TREE_CODE (constructor_type) == UNION_TYPE)
5467 && DECL_C_BIT_FIELD (field)
5468 && TREE_CODE (value) != INTEGER_CST))
5469 constructor_simple = 0;
5471 if (require_constant_value && ! TREE_CONSTANT (value))
5473 error_init ("initializer element is not constant");
5474 value = error_mark_node;
5476 else if (require_constant_elements
5477 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5478 pedwarn ("initializer element is not computable at load time");
5480 /* If this field is empty (and not at the end of structure),
5481 don't do anything other than checking the initializer. */
5483 && (TREE_TYPE (field) == error_mark_node
5484 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5485 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5486 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5487 || TREE_CHAIN (field)))))
5490 value = digest_init (type, value, require_constant_value);
5491 if (value == error_mark_node)
5493 constructor_erroneous = 1;
5497 /* If this element doesn't come next in sequence,
5498 put it on constructor_pending_elts. */
5499 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5500 && (!constructor_incremental
5501 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5503 if (constructor_incremental
5504 && tree_int_cst_lt (field, constructor_unfilled_index))
5505 set_nonincremental_init ();
5507 add_pending_init (field, value);
5510 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5511 && (!constructor_incremental
5512 || field != constructor_unfilled_fields))
5514 /* We do this for records but not for unions. In a union,
5515 no matter which field is specified, it can be initialized
5516 right away since it starts at the beginning of the union. */
5517 if (constructor_incremental)
5519 if (!constructor_unfilled_fields)
5520 set_nonincremental_init ();
5523 tree bitpos, unfillpos;
5525 bitpos = bit_position (field);
5526 unfillpos = bit_position (constructor_unfilled_fields);
5528 if (tree_int_cst_lt (bitpos, unfillpos))
5529 set_nonincremental_init ();
5533 add_pending_init (field, value);
5536 else if (TREE_CODE (constructor_type) == UNION_TYPE
5537 && constructor_elements)
5539 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5540 warning_init ("initialized field with side-effects overwritten");
5542 /* We can have just one union field set. */
5543 constructor_elements = 0;
5546 /* Otherwise, output this element either to
5547 constructor_elements or to the assembler file. */
5549 if (field && TREE_CODE (field) == INTEGER_CST)
5550 field = copy_node (field);
5551 constructor_elements
5552 = tree_cons (field, value, constructor_elements);
5554 /* Advance the variable that indicates sequential elements output. */
5555 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5556 constructor_unfilled_index
5557 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5559 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5561 constructor_unfilled_fields
5562 = TREE_CHAIN (constructor_unfilled_fields);
5564 /* Skip any nameless bit fields. */
5565 while (constructor_unfilled_fields != 0
5566 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5567 && DECL_NAME (constructor_unfilled_fields) == 0)
5568 constructor_unfilled_fields =
5569 TREE_CHAIN (constructor_unfilled_fields);
5571 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5572 constructor_unfilled_fields = 0;
5574 /* Now output any pending elements which have become next. */
5576 output_pending_init_elements (0);
5579 /* Output any pending elements which have become next.
5580 As we output elements, constructor_unfilled_{fields,index}
5581 advances, which may cause other elements to become next;
5582 if so, they too are output.
5584 If ALL is 0, we return when there are
5585 no more pending elements to output now.
5587 If ALL is 1, we output space as necessary so that
5588 we can output all the pending elements. */
5591 output_pending_init_elements (int all)
5593 struct init_node *elt = constructor_pending_elts;
5598 /* Look thru the whole pending tree.
5599 If we find an element that should be output now,
5600 output it. Otherwise, set NEXT to the element
5601 that comes first among those still pending. */
5606 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5608 if (tree_int_cst_equal (elt->purpose,
5609 constructor_unfilled_index))
5610 output_init_element (elt->value,
5611 TREE_TYPE (constructor_type),
5612 constructor_unfilled_index, 0);
5613 else if (tree_int_cst_lt (constructor_unfilled_index,
5616 /* Advance to the next smaller node. */
5621 /* We have reached the smallest node bigger than the
5622 current unfilled index. Fill the space first. */
5623 next = elt->purpose;
5629 /* Advance to the next bigger node. */
5634 /* We have reached the biggest node in a subtree. Find
5635 the parent of it, which is the next bigger node. */
5636 while (elt->parent && elt->parent->right == elt)
5639 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5642 next = elt->purpose;
5648 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5649 || TREE_CODE (constructor_type) == UNION_TYPE)
5651 tree ctor_unfilled_bitpos, elt_bitpos;
5653 /* If the current record is complete we are done. */
5654 if (constructor_unfilled_fields == 0)
5657 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5658 elt_bitpos = bit_position (elt->purpose);
5659 /* We can't compare fields here because there might be empty
5660 fields in between. */
5661 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5663 constructor_unfilled_fields = elt->purpose;
5664 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5667 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5669 /* Advance to the next smaller node. */
5674 /* We have reached the smallest node bigger than the
5675 current unfilled field. Fill the space first. */
5676 next = elt->purpose;
5682 /* Advance to the next bigger node. */
5687 /* We have reached the biggest node in a subtree. Find
5688 the parent of it, which is the next bigger node. */
5689 while (elt->parent && elt->parent->right == elt)
5693 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5694 bit_position (elt->purpose))))
5696 next = elt->purpose;
5704 /* Ordinarily return, but not if we want to output all
5705 and there are elements left. */
5706 if (! (all && next != 0))
5709 /* If it's not incremental, just skip over the gap, so that after
5710 jumping to retry we will output the next successive element. */
5711 if (TREE_CODE (constructor_type) == RECORD_TYPE
5712 || TREE_CODE (constructor_type) == UNION_TYPE)
5713 constructor_unfilled_fields = next;
5714 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5715 constructor_unfilled_index = next;
5717 /* ELT now points to the node in the pending tree with the next
5718 initializer to output. */
5722 /* Add one non-braced element to the current constructor level.
5723 This adjusts the current position within the constructor's type.
5724 This may also start or terminate implicit levels
5725 to handle a partly-braced initializer.
5727 Once this has found the correct level for the new element,
5728 it calls output_init_element. */
5731 process_init_element (tree value)
5733 tree orig_value = value;
5734 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5736 designator_depth = 0;
5737 designator_errorneous = 0;
5739 /* Handle superfluous braces around string cst as in
5740 char x[] = {"foo"}; */
5743 && TREE_CODE (constructor_type) == ARRAY_TYPE
5744 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5745 && integer_zerop (constructor_unfilled_index))
5747 if (constructor_stack->replacement_value)
5748 error_init ("excess elements in char array initializer");
5749 constructor_stack->replacement_value = value;
5753 if (constructor_stack->replacement_value != 0)
5755 error_init ("excess elements in struct initializer");
5759 /* Ignore elements of a brace group if it is entirely superfluous
5760 and has already been diagnosed. */
5761 if (constructor_type == 0)
5764 /* If we've exhausted any levels that didn't have braces,
5766 while (constructor_stack->implicit)
5768 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5769 || TREE_CODE (constructor_type) == UNION_TYPE)
5770 && constructor_fields == 0)
5771 process_init_element (pop_init_level (1));
5772 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5773 && (constructor_max_index == 0
5774 || tree_int_cst_lt (constructor_max_index,
5775 constructor_index)))
5776 process_init_element (pop_init_level (1));
5781 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5782 if (constructor_range_stack)
5784 /* If value is a compound literal and we'll be just using its
5785 content, don't put it into a SAVE_EXPR. */
5786 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5787 || !require_constant_value
5789 value = save_expr (value);
5794 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5797 enum tree_code fieldcode;
5799 if (constructor_fields == 0)
5801 pedwarn_init ("excess elements in struct initializer");
5805 fieldtype = TREE_TYPE (constructor_fields);
5806 if (fieldtype != error_mark_node)
5807 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5808 fieldcode = TREE_CODE (fieldtype);
5810 /* Error for non-static initialization of a flexible array member. */
5811 if (fieldcode == ARRAY_TYPE
5812 && !require_constant_value
5813 && TYPE_SIZE (fieldtype) == NULL_TREE
5814 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5816 error_init ("non-static initialization of a flexible array member");
5820 /* Accept a string constant to initialize a subarray. */
5822 && fieldcode == ARRAY_TYPE
5823 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5826 /* Otherwise, if we have come to a subaggregate,
5827 and we don't have an element of its type, push into it. */
5828 else if (value != 0 && !constructor_no_implicit
5829 && value != error_mark_node
5830 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5831 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5832 || fieldcode == UNION_TYPE))
5834 push_init_level (1);
5840 push_member_name (constructor_fields);
5841 output_init_element (value, fieldtype, constructor_fields, 1);
5842 RESTORE_SPELLING_DEPTH (constructor_depth);
5845 /* Do the bookkeeping for an element that was
5846 directly output as a constructor. */
5848 /* For a record, keep track of end position of last field. */
5849 if (DECL_SIZE (constructor_fields))
5850 constructor_bit_index
5851 = size_binop (PLUS_EXPR,
5852 bit_position (constructor_fields),
5853 DECL_SIZE (constructor_fields));
5855 /* If the current field was the first one not yet written out,
5856 it isn't now, so update. */
5857 if (constructor_unfilled_fields == constructor_fields)
5859 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5860 /* Skip any nameless bit fields. */
5861 while (constructor_unfilled_fields != 0
5862 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5863 && DECL_NAME (constructor_unfilled_fields) == 0)
5864 constructor_unfilled_fields =
5865 TREE_CHAIN (constructor_unfilled_fields);
5869 constructor_fields = TREE_CHAIN (constructor_fields);
5870 /* Skip any nameless bit fields at the beginning. */
5871 while (constructor_fields != 0
5872 && DECL_C_BIT_FIELD (constructor_fields)
5873 && DECL_NAME (constructor_fields) == 0)
5874 constructor_fields = TREE_CHAIN (constructor_fields);
5876 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5879 enum tree_code fieldcode;
5881 if (constructor_fields == 0)
5883 pedwarn_init ("excess elements in union initializer");
5887 fieldtype = TREE_TYPE (constructor_fields);
5888 if (fieldtype != error_mark_node)
5889 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5890 fieldcode = TREE_CODE (fieldtype);
5892 /* Warn that traditional C rejects initialization of unions.
5893 We skip the warning if the value is zero. This is done
5894 under the assumption that the zero initializer in user
5895 code appears conditioned on e.g. __STDC__ to avoid
5896 "missing initializer" warnings and relies on default
5897 initialization to zero in the traditional C case.
5898 We also skip the warning if the initializer is designated,
5899 again on the assumption that this must be conditional on
5900 __STDC__ anyway (and we've already complained about the
5901 member-designator already). */
5902 if (warn_traditional && !in_system_header && !constructor_designated
5903 && !(value && (integer_zerop (value) || real_zerop (value))))
5904 warning ("traditional C rejects initialization of unions");
5906 /* Accept a string constant to initialize a subarray. */
5908 && fieldcode == ARRAY_TYPE
5909 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5912 /* Otherwise, if we have come to a subaggregate,
5913 and we don't have an element of its type, push into it. */
5914 else if (value != 0 && !constructor_no_implicit
5915 && value != error_mark_node
5916 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5917 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5918 || fieldcode == UNION_TYPE))
5920 push_init_level (1);
5926 push_member_name (constructor_fields);
5927 output_init_element (value, fieldtype, constructor_fields, 1);
5928 RESTORE_SPELLING_DEPTH (constructor_depth);
5931 /* Do the bookkeeping for an element that was
5932 directly output as a constructor. */
5934 constructor_bit_index = DECL_SIZE (constructor_fields);
5935 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5938 constructor_fields = 0;
5940 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5942 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5943 enum tree_code eltcode = TREE_CODE (elttype);
5945 /* Accept a string constant to initialize a subarray. */
5947 && eltcode == ARRAY_TYPE
5948 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5951 /* Otherwise, if we have come to a subaggregate,
5952 and we don't have an element of its type, push into it. */
5953 else if (value != 0 && !constructor_no_implicit
5954 && value != error_mark_node
5955 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5956 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5957 || eltcode == UNION_TYPE))
5959 push_init_level (1);
5963 if (constructor_max_index != 0
5964 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5965 || integer_all_onesp (constructor_max_index)))
5967 pedwarn_init ("excess elements in array initializer");
5971 /* Now output the actual element. */
5974 push_array_bounds (tree_low_cst (constructor_index, 0));
5975 output_init_element (value, elttype, constructor_index, 1);
5976 RESTORE_SPELLING_DEPTH (constructor_depth);
5980 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5983 /* If we are doing the bookkeeping for an element that was
5984 directly output as a constructor, we must update
5985 constructor_unfilled_index. */
5986 constructor_unfilled_index = constructor_index;
5988 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5990 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5992 /* Do a basic check of initializer size. Note that vectors
5993 always have a fixed size derived from their type. */
5994 if (tree_int_cst_lt (constructor_max_index, constructor_index))
5996 pedwarn_init ("excess elements in vector initializer");
6000 /* Now output the actual element. */
6002 output_init_element (value, elttype, constructor_index, 1);
6005 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6008 /* If we are doing the bookkeeping for an element that was
6009 directly output as a constructor, we must update
6010 constructor_unfilled_index. */
6011 constructor_unfilled_index = constructor_index;
6014 /* Handle the sole element allowed in a braced initializer
6015 for a scalar variable. */
6016 else if (constructor_fields == 0)
6018 pedwarn_init ("excess elements in scalar initializer");
6024 output_init_element (value, constructor_type, NULL_TREE, 1);
6025 constructor_fields = 0;
6028 /* Handle range initializers either at this level or anywhere higher
6029 in the designator stack. */
6030 if (constructor_range_stack)
6032 struct constructor_range_stack *p, *range_stack;
6035 range_stack = constructor_range_stack;
6036 constructor_range_stack = 0;
6037 while (constructor_stack != range_stack->stack)
6039 if (!constructor_stack->implicit)
6041 process_init_element (pop_init_level (1));
6043 for (p = range_stack;
6044 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6047 if (!constructor_stack->implicit)
6049 process_init_element (pop_init_level (1));
6052 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6053 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6058 constructor_index = p->index;
6059 constructor_fields = p->fields;
6060 if (finish && p->range_end && p->index == p->range_start)
6068 push_init_level (2);
6069 p->stack = constructor_stack;
6070 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6071 p->index = p->range_start;
6075 constructor_range_stack = range_stack;
6082 constructor_range_stack = 0;
6085 /* Build a simple asm-statement, from one string literal. */
6087 simple_asm_stmt (tree expr)
6091 if (TREE_CODE (expr) == ADDR_EXPR)
6092 expr = TREE_OPERAND (expr, 0);
6094 if (TREE_CODE (expr) == STRING_CST)
6098 /* Simple asm statements are treated as volatile. */
6099 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6100 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6101 ASM_INPUT_P (stmt) = 1;
6105 error ("argument of `asm' is not a constant string");
6109 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6110 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6113 build_asm_stmt (tree cv_qualifier, tree string, tree outputs, tree inputs,
6118 if (TREE_CODE (string) != STRING_CST)
6120 error ("asm template is not a string constant");
6124 if (cv_qualifier != NULL_TREE
6125 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6127 warning ("%s qualifier ignored on asm",
6128 IDENTIFIER_POINTER (cv_qualifier));
6129 cv_qualifier = NULL_TREE;
6132 /* We can remove output conversions that change the type,
6133 but not the mode. */
6134 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6136 tree output = TREE_VALUE (tail);
6138 STRIP_NOPS (output);
6139 TREE_VALUE (tail) = output;
6141 /* Allow conversions as LHS here. build_modify_expr as called below
6142 will do the right thing with them. */
6143 while (TREE_CODE (output) == NOP_EXPR
6144 || TREE_CODE (output) == CONVERT_EXPR
6145 || TREE_CODE (output) == FLOAT_EXPR
6146 || TREE_CODE (output) == FIX_TRUNC_EXPR
6147 || TREE_CODE (output) == FIX_FLOOR_EXPR
6148 || TREE_CODE (output) == FIX_ROUND_EXPR
6149 || TREE_CODE (output) == FIX_CEIL_EXPR)
6150 output = TREE_OPERAND (output, 0);
6152 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6155 /* Remove output conversions that change the type but not the mode. */
6156 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6158 tree output = TREE_VALUE (tail);
6159 STRIP_NOPS (output);
6160 TREE_VALUE (tail) = output;
6163 /* Perform default conversions on array and function inputs.
6164 Don't do this for other types as it would screw up operands
6165 expected to be in memory. */
6166 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6167 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6169 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6170 outputs, inputs, clobbers));
6173 /* Expand an ASM statement with operands, handling output operands
6174 that are not variables or INDIRECT_REFS by transforming such
6175 cases into cases that expand_asm_operands can handle.
6177 Arguments are same as for expand_asm_operands. */
6180 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6181 tree clobbers, int vol, const char *filename,
6184 int noutputs = list_length (outputs);
6186 /* o[I] is the place that output number I should be written. */
6187 tree *o = alloca (noutputs * sizeof (tree));
6190 /* Record the contents of OUTPUTS before it is modified. */
6191 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6193 o[i] = TREE_VALUE (tail);
6194 if (o[i] == error_mark_node)
6198 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6199 OUTPUTS some trees for where the values were actually stored. */
6200 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6202 /* Copy all the intermediate outputs into the specified outputs. */
6203 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6205 if (o[i] != TREE_VALUE (tail))
6207 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6208 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6211 /* Restore the original value so that it's correct the next
6212 time we expand this function. */
6213 TREE_VALUE (tail) = o[i];
6215 /* Detect modification of read-only values.
6216 (Otherwise done by build_modify_expr.) */
6219 tree type = TREE_TYPE (o[i]);
6220 if (TREE_READONLY (o[i])
6221 || TYPE_READONLY (type)
6222 || ((TREE_CODE (type) == RECORD_TYPE
6223 || TREE_CODE (type) == UNION_TYPE)
6224 && C_TYPE_FIELDS_READONLY (type)))
6225 readonly_warning (o[i], "modification by `asm'");
6229 /* Those MODIFY_EXPRs could do autoincrements. */
6233 /* Expand a C `return' statement.
6234 RETVAL is the expression for what to return,
6235 or a null pointer for `return;' with no value. */
6238 c_expand_return (tree retval)
6240 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6242 if (TREE_THIS_VOLATILE (current_function_decl))
6243 warning ("function declared `noreturn' has a `return' statement");
6247 current_function_returns_null = 1;
6248 if ((warn_return_type || flag_isoc99)
6249 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6250 pedwarn_c99 ("`return' with no value, in function returning non-void");
6252 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6254 current_function_returns_null = 1;
6255 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6256 pedwarn ("`return' with a value, in function returning void");
6260 tree t = convert_for_assignment (valtype, retval, _("return"),
6261 NULL_TREE, NULL_TREE, 0);
6262 tree res = DECL_RESULT (current_function_decl);
6265 current_function_returns_value = 1;
6266 if (t == error_mark_node)
6269 inner = t = convert (TREE_TYPE (res), t);
6271 /* Strip any conversions, additions, and subtractions, and see if
6272 we are returning the address of a local variable. Warn if so. */
6275 switch (TREE_CODE (inner))
6277 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6279 inner = TREE_OPERAND (inner, 0);
6283 /* If the second operand of the MINUS_EXPR has a pointer
6284 type (or is converted from it), this may be valid, so
6285 don't give a warning. */
6287 tree op1 = TREE_OPERAND (inner, 1);
6289 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6290 && (TREE_CODE (op1) == NOP_EXPR
6291 || TREE_CODE (op1) == NON_LVALUE_EXPR
6292 || TREE_CODE (op1) == CONVERT_EXPR))
6293 op1 = TREE_OPERAND (op1, 0);
6295 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6298 inner = TREE_OPERAND (inner, 0);
6303 inner = TREE_OPERAND (inner, 0);
6305 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6306 inner = TREE_OPERAND (inner, 0);
6308 if (TREE_CODE (inner) == VAR_DECL
6309 && ! DECL_EXTERNAL (inner)
6310 && ! TREE_STATIC (inner)
6311 && DECL_CONTEXT (inner) == current_function_decl)
6312 warning ("function returns address of local variable");
6322 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6325 return add_stmt (build_return_stmt (retval));
6329 /* The SWITCH_STMT being built. */
6331 /* A splay-tree mapping the low element of a case range to the high
6332 element, or NULL_TREE if there is no high element. Used to
6333 determine whether or not a new case label duplicates an old case
6334 label. We need a tree, rather than simply a hash table, because
6335 of the GNU case range extension. */
6337 /* The next node on the stack. */
6338 struct c_switch *next;
6341 /* A stack of the currently active switch statements. The innermost
6342 switch statement is on the top of the stack. There is no need to
6343 mark the stack for garbage collection because it is only active
6344 during the processing of the body of a function, and we never
6345 collect at that point. */
6347 static struct c_switch *switch_stack;
6349 /* Start a C switch statement, testing expression EXP. Return the new
6353 c_start_case (tree exp)
6355 enum tree_code code;
6356 tree type, orig_type = error_mark_node;
6357 struct c_switch *cs;
6359 if (exp != error_mark_node)
6361 code = TREE_CODE (TREE_TYPE (exp));
6362 orig_type = TREE_TYPE (exp);
6364 if (! INTEGRAL_TYPE_P (orig_type)
6365 && code != ERROR_MARK)
6367 error ("switch quantity not an integer");
6368 exp = integer_zero_node;
6372 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6374 if (warn_traditional && !in_system_header
6375 && (type == long_integer_type_node
6376 || type == long_unsigned_type_node))
6377 warning ("`long' switch expression not converted to `int' in ISO C");
6379 exp = default_conversion (exp);
6380 type = TREE_TYPE (exp);
6384 /* Add this new SWITCH_STMT to the stack. */
6385 cs = xmalloc (sizeof (*cs));
6386 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6387 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6388 cs->next = switch_stack;
6391 return add_stmt (switch_stack->switch_stmt);
6394 /* Process a case label. */
6397 do_case (tree low_value, tree high_value)
6399 tree label = NULL_TREE;
6403 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6405 label = c_add_case_label (switch_stack->cases,
6406 SWITCH_COND (switch_stack->switch_stmt),
6407 low_value, high_value);
6408 if (label == error_mark_node)
6410 else if (switch_was_empty_p)
6412 /* Attach the first case label to the SWITCH_BODY. */
6413 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6414 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6418 error ("case label not within a switch statement");
6420 error ("`default' label not within a switch statement");
6425 /* Finish the switch statement. */
6428 c_finish_case (void)
6430 struct c_switch *cs = switch_stack;
6432 /* Rechain the next statements to the SWITCH_STMT. */
6433 last_tree = cs->switch_stmt;
6435 /* Pop the stack. */
6436 switch_stack = switch_stack->next;
6437 splay_tree_delete (cs->cases);
6441 /* Build a binary-operation expression without default conversions.
6442 CODE is the kind of expression to build.
6443 This function differs from `build' in several ways:
6444 the data type of the result is computed and recorded in it,
6445 warnings are generated if arg data types are invalid,
6446 special handling for addition and subtraction of pointers is known,
6447 and some optimization is done (operations on narrow ints
6448 are done in the narrower type when that gives the same result).
6449 Constant folding is also done before the result is returned.
6451 Note that the operands will never have enumeral types, or function
6452 or array types, because either they will have the default conversions
6453 performed or they have both just been converted to some other type in which
6454 the arithmetic is to be done. */
6457 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6461 enum tree_code code0, code1;
6464 /* Expression code to give to the expression when it is built.
6465 Normally this is CODE, which is what the caller asked for,
6466 but in some special cases we change it. */
6467 enum tree_code resultcode = code;
6469 /* Data type in which the computation is to be performed.
6470 In the simplest cases this is the common type of the arguments. */
6471 tree result_type = NULL;
6473 /* Nonzero means operands have already been type-converted
6474 in whatever way is necessary.
6475 Zero means they need to be converted to RESULT_TYPE. */
6478 /* Nonzero means create the expression with this type, rather than
6480 tree build_type = 0;
6482 /* Nonzero means after finally constructing the expression
6483 convert it to this type. */
6484 tree final_type = 0;
6486 /* Nonzero if this is an operation like MIN or MAX which can
6487 safely be computed in short if both args are promoted shorts.
6488 Also implies COMMON.
6489 -1 indicates a bitwise operation; this makes a difference
6490 in the exact conditions for when it is safe to do the operation
6491 in a narrower mode. */
6494 /* Nonzero if this is a comparison operation;
6495 if both args are promoted shorts, compare the original shorts.
6496 Also implies COMMON. */
6497 int short_compare = 0;
6499 /* Nonzero if this is a right-shift operation, which can be computed on the
6500 original short and then promoted if the operand is a promoted short. */
6501 int short_shift = 0;
6503 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6508 op0 = default_conversion (orig_op0);
6509 op1 = default_conversion (orig_op1);
6517 type0 = TREE_TYPE (op0);
6518 type1 = TREE_TYPE (op1);
6520 /* The expression codes of the data types of the arguments tell us
6521 whether the arguments are integers, floating, pointers, etc. */
6522 code0 = TREE_CODE (type0);
6523 code1 = TREE_CODE (type1);
6525 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6526 STRIP_TYPE_NOPS (op0);
6527 STRIP_TYPE_NOPS (op1);
6529 /* If an error was already reported for one of the arguments,
6530 avoid reporting another error. */
6532 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6533 return error_mark_node;
6538 /* Handle the pointer + int case. */
6539 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6540 return pointer_int_sum (PLUS_EXPR, op0, op1);
6541 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6542 return pointer_int_sum (PLUS_EXPR, op1, op0);
6548 /* Subtraction of two similar pointers.
6549 We must subtract them as integers, then divide by object size. */
6550 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6551 && comp_target_types (type0, type1, 1))
6552 return pointer_diff (op0, op1);
6553 /* Handle pointer minus int. Just like pointer plus int. */
6554 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6555 return pointer_int_sum (MINUS_EXPR, op0, op1);
6564 case TRUNC_DIV_EXPR:
6566 case FLOOR_DIV_EXPR:
6567 case ROUND_DIV_EXPR:
6568 case EXACT_DIV_EXPR:
6569 /* Floating point division by zero is a legitimate way to obtain
6570 infinities and NaNs. */
6571 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6572 warning ("division by zero");
6574 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6575 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6576 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6577 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6579 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6580 resultcode = RDIV_EXPR;
6582 /* Although it would be tempting to shorten always here, that
6583 loses on some targets, since the modulo instruction is
6584 undefined if the quotient can't be represented in the
6585 computation mode. We shorten only if unsigned or if
6586 dividing by something we know != -1. */
6587 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6588 || (TREE_CODE (op1) == INTEGER_CST
6589 && ! integer_all_onesp (op1)));
6595 case BIT_ANDTC_EXPR:
6598 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6600 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6604 case TRUNC_MOD_EXPR:
6605 case FLOOR_MOD_EXPR:
6606 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6607 warning ("division by zero");
6609 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6611 /* Although it would be tempting to shorten always here, that loses
6612 on some targets, since the modulo instruction is undefined if the
6613 quotient can't be represented in the computation mode. We shorten
6614 only if unsigned or if dividing by something we know != -1. */
6615 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6616 || (TREE_CODE (op1) == INTEGER_CST
6617 && ! integer_all_onesp (op1)));
6622 case TRUTH_ANDIF_EXPR:
6623 case TRUTH_ORIF_EXPR:
6624 case TRUTH_AND_EXPR:
6626 case TRUTH_XOR_EXPR:
6627 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6628 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6629 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6630 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6632 /* Result of these operations is always an int,
6633 but that does not mean the operands should be
6634 converted to ints! */
6635 result_type = integer_type_node;
6636 op0 = c_common_truthvalue_conversion (op0);
6637 op1 = c_common_truthvalue_conversion (op1);
6642 /* Shift operations: result has same type as first operand;
6643 always convert second operand to int.
6644 Also set SHORT_SHIFT if shifting rightward. */
6647 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6649 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6651 if (tree_int_cst_sgn (op1) < 0)
6652 warning ("right shift count is negative");
6655 if (! integer_zerop (op1))
6658 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6659 warning ("right shift count >= width of type");
6663 /* Use the type of the value to be shifted. */
6664 result_type = type0;
6665 /* Convert the shift-count to an integer, regardless of size
6666 of value being shifted. */
6667 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6668 op1 = convert (integer_type_node, op1);
6669 /* Avoid converting op1 to result_type later. */
6675 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6677 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6679 if (tree_int_cst_sgn (op1) < 0)
6680 warning ("left shift count is negative");
6682 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6683 warning ("left shift count >= width of type");
6686 /* Use the type of the value to be shifted. */
6687 result_type = type0;
6688 /* Convert the shift-count to an integer, regardless of size
6689 of value being shifted. */
6690 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6691 op1 = convert (integer_type_node, op1);
6692 /* Avoid converting op1 to result_type later. */
6699 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6701 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6703 if (tree_int_cst_sgn (op1) < 0)
6704 warning ("shift count is negative");
6705 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6706 warning ("shift count >= width of type");
6709 /* Use the type of the value to be shifted. */
6710 result_type = type0;
6711 /* Convert the shift-count to an integer, regardless of size
6712 of value being shifted. */
6713 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6714 op1 = convert (integer_type_node, op1);
6715 /* Avoid converting op1 to result_type later. */
6722 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6723 warning ("comparing floating point with == or != is unsafe");
6724 /* Result of comparison is always int,
6725 but don't convert the args to int! */
6726 build_type = integer_type_node;
6727 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6728 || code0 == COMPLEX_TYPE
6729 || code0 == VECTOR_TYPE)
6730 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6731 || code1 == COMPLEX_TYPE
6732 || code1 == VECTOR_TYPE))
6734 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6736 tree tt0 = TREE_TYPE (type0);
6737 tree tt1 = TREE_TYPE (type1);
6738 /* Anything compares with void *. void * compares with anything.
6739 Otherwise, the targets must be compatible
6740 and both must be object or both incomplete. */
6741 if (comp_target_types (type0, type1, 1))
6742 result_type = common_type (type0, type1);
6743 else if (VOID_TYPE_P (tt0))
6745 /* op0 != orig_op0 detects the case of something
6746 whose value is 0 but which isn't a valid null ptr const. */
6747 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6748 && TREE_CODE (tt1) == FUNCTION_TYPE)
6749 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6751 else if (VOID_TYPE_P (tt1))
6753 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6754 && TREE_CODE (tt0) == FUNCTION_TYPE)
6755 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6758 pedwarn ("comparison of distinct pointer types lacks a cast");
6760 if (result_type == NULL_TREE)
6761 result_type = ptr_type_node;
6763 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6764 && integer_zerop (op1))
6765 result_type = type0;
6766 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6767 && integer_zerop (op0))
6768 result_type = type1;
6769 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6771 result_type = type0;
6772 pedwarn ("comparison between pointer and integer");
6774 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6776 result_type = type1;
6777 pedwarn ("comparison between pointer and integer");
6783 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6784 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6786 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6788 if (comp_target_types (type0, type1, 1))
6790 result_type = common_type (type0, type1);
6792 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6793 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6797 result_type = ptr_type_node;
6798 pedwarn ("comparison of distinct pointer types lacks a cast");
6807 build_type = integer_type_node;
6808 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6809 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6811 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6813 if (comp_target_types (type0, type1, 1))
6815 result_type = common_type (type0, type1);
6816 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6817 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6818 pedwarn ("comparison of complete and incomplete pointers");
6820 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6821 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6825 result_type = ptr_type_node;
6826 pedwarn ("comparison of distinct pointer types lacks a cast");
6829 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6830 && integer_zerop (op1))
6832 result_type = type0;
6833 if (pedantic || extra_warnings)
6834 pedwarn ("ordered comparison of pointer with integer zero");
6836 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6837 && integer_zerop (op0))
6839 result_type = type1;
6841 pedwarn ("ordered comparison of pointer with integer zero");
6843 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6845 result_type = type0;
6846 pedwarn ("comparison between pointer and integer");
6848 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6850 result_type = type1;
6851 pedwarn ("comparison between pointer and integer");
6855 case UNORDERED_EXPR:
6862 build_type = integer_type_node;
6863 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6865 error ("unordered comparison on non-floating point argument");
6866 return error_mark_node;
6875 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6876 || code0 == VECTOR_TYPE)
6878 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6879 || code1 == VECTOR_TYPE))
6881 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6883 if (shorten || common || short_compare)
6884 result_type = common_type (type0, type1);
6886 /* For certain operations (which identify themselves by shorten != 0)
6887 if both args were extended from the same smaller type,
6888 do the arithmetic in that type and then extend.
6890 shorten !=0 and !=1 indicates a bitwise operation.
6891 For them, this optimization is safe only if
6892 both args are zero-extended or both are sign-extended.
6893 Otherwise, we might change the result.
6894 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6895 but calculated in (unsigned short) it would be (unsigned short)-1. */
6897 if (shorten && none_complex)
6899 int unsigned0, unsigned1;
6900 tree arg0 = get_narrower (op0, &unsigned0);
6901 tree arg1 = get_narrower (op1, &unsigned1);
6902 /* UNS is 1 if the operation to be done is an unsigned one. */
6903 int uns = TREE_UNSIGNED (result_type);
6906 final_type = result_type;
6908 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6909 but it *requires* conversion to FINAL_TYPE. */
6911 if ((TYPE_PRECISION (TREE_TYPE (op0))
6912 == TYPE_PRECISION (TREE_TYPE (arg0)))
6913 && TREE_TYPE (op0) != final_type)
6914 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
6915 if ((TYPE_PRECISION (TREE_TYPE (op1))
6916 == TYPE_PRECISION (TREE_TYPE (arg1)))
6917 && TREE_TYPE (op1) != final_type)
6918 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
6920 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6922 /* For bitwise operations, signedness of nominal type
6923 does not matter. Consider only how operands were extended. */
6927 /* Note that in all three cases below we refrain from optimizing
6928 an unsigned operation on sign-extended args.
6929 That would not be valid. */
6931 /* Both args variable: if both extended in same way
6932 from same width, do it in that width.
6933 Do it unsigned if args were zero-extended. */
6934 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6935 < TYPE_PRECISION (result_type))
6936 && (TYPE_PRECISION (TREE_TYPE (arg1))
6937 == TYPE_PRECISION (TREE_TYPE (arg0)))
6938 && unsigned0 == unsigned1
6939 && (unsigned0 || !uns))
6941 = c_common_signed_or_unsigned_type
6942 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6943 else if (TREE_CODE (arg0) == INTEGER_CST
6944 && (unsigned1 || !uns)
6945 && (TYPE_PRECISION (TREE_TYPE (arg1))
6946 < TYPE_PRECISION (result_type))
6948 = c_common_signed_or_unsigned_type (unsigned1,
6950 int_fits_type_p (arg0, type)))
6952 else if (TREE_CODE (arg1) == INTEGER_CST
6953 && (unsigned0 || !uns)
6954 && (TYPE_PRECISION (TREE_TYPE (arg0))
6955 < TYPE_PRECISION (result_type))
6957 = c_common_signed_or_unsigned_type (unsigned0,
6959 int_fits_type_p (arg1, type)))
6963 /* Shifts can be shortened if shifting right. */
6968 tree arg0 = get_narrower (op0, &unsigned_arg);
6970 final_type = result_type;
6972 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6973 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
6975 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6976 /* We can shorten only if the shift count is less than the
6977 number of bits in the smaller type size. */
6978 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6979 /* We cannot drop an unsigned shift after sign-extension. */
6980 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
6982 /* Do an unsigned shift if the operand was zero-extended. */
6984 = c_common_signed_or_unsigned_type (unsigned_arg,
6986 /* Convert value-to-be-shifted to that type. */
6987 if (TREE_TYPE (op0) != result_type)
6988 op0 = convert (result_type, op0);
6993 /* Comparison operations are shortened too but differently.
6994 They identify themselves by setting short_compare = 1. */
6998 /* Don't write &op0, etc., because that would prevent op0
6999 from being kept in a register.
7000 Instead, make copies of the our local variables and
7001 pass the copies by reference, then copy them back afterward. */
7002 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7003 enum tree_code xresultcode = resultcode;
7005 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7010 op0 = xop0, op1 = xop1;
7012 resultcode = xresultcode;
7014 if (warn_sign_compare && skip_evaluation == 0)
7016 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
7017 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
7018 int unsignedp0, unsignedp1;
7019 tree primop0 = get_narrower (op0, &unsignedp0);
7020 tree primop1 = get_narrower (op1, &unsignedp1);
7024 STRIP_TYPE_NOPS (xop0);
7025 STRIP_TYPE_NOPS (xop1);
7027 /* Give warnings for comparisons between signed and unsigned
7028 quantities that may fail.
7030 Do the checking based on the original operand trees, so that
7031 casts will be considered, but default promotions won't be.
7033 Do not warn if the comparison is being done in a signed type,
7034 since the signed type will only be chosen if it can represent
7035 all the values of the unsigned type. */
7036 if (! TREE_UNSIGNED (result_type))
7038 /* Do not warn if both operands are the same signedness. */
7039 else if (op0_signed == op1_signed)
7046 sop = xop0, uop = xop1;
7048 sop = xop1, uop = xop0;
7050 /* Do not warn if the signed quantity is an
7051 unsuffixed integer literal (or some static
7052 constant expression involving such literals or a
7053 conditional expression involving such literals)
7054 and it is non-negative. */
7055 if (c_tree_expr_nonnegative_p (sop))
7057 /* Do not warn if the comparison is an equality operation,
7058 the unsigned quantity is an integral constant, and it
7059 would fit in the result if the result were signed. */
7060 else if (TREE_CODE (uop) == INTEGER_CST
7061 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7063 (uop, c_common_signed_type (result_type)))
7065 /* Do not warn if the unsigned quantity is an enumeration
7066 constant and its maximum value would fit in the result
7067 if the result were signed. */
7068 else if (TREE_CODE (uop) == INTEGER_CST
7069 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7071 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7072 c_common_signed_type (result_type)))
7075 warning ("comparison between signed and unsigned");
7078 /* Warn if two unsigned values are being compared in a size
7079 larger than their original size, and one (and only one) is the
7080 result of a `~' operator. This comparison will always fail.
7082 Also warn if one operand is a constant, and the constant
7083 does not have all bits set that are set in the ~ operand
7084 when it is extended. */
7086 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7087 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7089 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7090 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7093 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7096 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7099 HOST_WIDE_INT constant, mask;
7100 int unsignedp, bits;
7102 if (host_integerp (primop0, 0))
7105 unsignedp = unsignedp1;
7106 constant = tree_low_cst (primop0, 0);
7111 unsignedp = unsignedp0;
7112 constant = tree_low_cst (primop1, 0);
7115 bits = TYPE_PRECISION (TREE_TYPE (primop));
7116 if (bits < TYPE_PRECISION (result_type)
7117 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7119 mask = (~ (HOST_WIDE_INT) 0) << bits;
7120 if ((mask & constant) != mask)
7121 warning ("comparison of promoted ~unsigned with constant");
7124 else if (unsignedp0 && unsignedp1
7125 && (TYPE_PRECISION (TREE_TYPE (primop0))
7126 < TYPE_PRECISION (result_type))
7127 && (TYPE_PRECISION (TREE_TYPE (primop1))
7128 < TYPE_PRECISION (result_type)))
7129 warning ("comparison of promoted ~unsigned with unsigned");
7135 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7136 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7137 Then the expression will be built.
7138 It will be given type FINAL_TYPE if that is nonzero;
7139 otherwise, it will be given type RESULT_TYPE. */
7143 binary_op_error (code);
7144 return error_mark_node;
7149 if (TREE_TYPE (op0) != result_type)
7150 op0 = convert (result_type, op0);
7151 if (TREE_TYPE (op1) != result_type)
7152 op1 = convert (result_type, op1);
7155 if (build_type == NULL_TREE)
7156 build_type = result_type;
7159 tree result = build (resultcode, build_type, op0, op1);
7162 /* Treat expressions in initializers specially as they can't trap. */
7163 folded = initializer_stack ? fold_initializer (result)
7165 if (folded == result)
7166 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7167 if (final_type != 0)
7168 return convert (final_type, folded);