1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
34 #include "coretypes.h"
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 same_translation_unit_p (tree, tree);
54 static int tagged_types_tu_compatible_p (tree, tree, int);
55 static int comp_target_types (tree, tree, int);
56 static int function_types_compatible_p (tree, tree, int);
57 static int type_lists_compatible_p (tree, tree, int);
58 static tree decl_constant_value_for_broken_optimization (tree);
59 static tree default_function_array_conversion (tree);
60 static tree lookup_field (tree, tree);
61 static tree convert_arguments (tree, tree, tree, tree);
62 static tree pointer_diff (tree, tree);
63 static tree unary_complex_lvalue (enum tree_code, tree, int);
64 static void pedantic_lvalue_warning (enum tree_code);
65 static tree internal_build_compound_expr (tree, int);
66 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
68 static void warn_for_assignment (const char *, const char *, tree, int);
69 static tree valid_compound_expr_initializer (tree, tree);
70 static void push_string (const char *);
71 static void push_member_name (tree);
72 static void push_array_bounds (int);
73 static int spelling_length (void);
74 static char *print_spelling (char *);
75 static void warning_init (const char *);
76 static tree digest_init (tree, tree, int);
77 static void output_init_element (tree, tree, tree, int);
78 static void output_pending_init_elements (int);
79 static int set_designator (int);
80 static void push_range_stack (tree);
81 static void add_pending_init (tree, tree);
82 static void set_nonincremental_init (void);
83 static void set_nonincremental_init_from_string (tree);
84 static tree find_init_member (tree);
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.) */
90 require_complete_type (tree value)
92 tree type = TREE_TYPE (value);
94 if (value == error_mark_node || type == error_mark_node)
95 return error_mark_node;
97 /* First, detect a valid value with a complete type. */
98 if (COMPLETE_TYPE_P (type))
101 c_incomplete_type_error (value, type);
102 return error_mark_node;
105 /* Print an error message for invalid use of an incomplete type.
106 VALUE is the expression that was used (or 0 if that isn't known)
107 and TYPE is the type that was invalid. */
110 c_incomplete_type_error (tree value, tree type)
112 const char *type_code_string;
114 /* Avoid duplicate error message. */
115 if (TREE_CODE (type) == ERROR_MARK)
118 if (value != 0 && (TREE_CODE (value) == VAR_DECL
119 || TREE_CODE (value) == PARM_DECL))
120 error ("`%s' has an incomplete type",
121 IDENTIFIER_POINTER (DECL_NAME (value)));
125 /* We must print an error message. Be clever about what it says. */
127 switch (TREE_CODE (type))
130 type_code_string = "struct";
134 type_code_string = "union";
138 type_code_string = "enum";
142 error ("invalid use of void expression");
146 if (TYPE_DOMAIN (type))
148 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
150 error ("invalid use of flexible array member");
153 type = TREE_TYPE (type);
156 error ("invalid use of array with unspecified bounds");
163 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
164 error ("invalid use of undefined type `%s %s'",
165 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
167 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
168 error ("invalid use of incomplete typedef `%s'",
169 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
173 /* Given a type, apply default promotions wrt unnamed function
174 arguments and return the new type. */
177 c_type_promotes_to (tree type)
179 if (TYPE_MAIN_VARIANT (type) == float_type_node)
180 return double_type_node;
182 if (c_promoting_integer_type_p (type))
184 /* Preserve unsignedness if not really getting any wider. */
185 if (TREE_UNSIGNED (type)
186 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
187 return unsigned_type_node;
188 return integer_type_node;
194 /* Return a variant of TYPE which has all the type qualifiers of LIKE
195 as well as those of TYPE. */
198 qualify_type (tree type, tree like)
200 return c_build_qualified_type (type,
201 TYPE_QUALS (type) | TYPE_QUALS (like));
204 /* Return the common type of two types.
205 We assume that comptypes has already been done and returned 1;
206 if that isn't so, this may crash. In particular, we assume that qualifiers
209 This is the type for the result of most arithmetic operations
210 if the operands have the given two types. */
213 common_type (tree t1, tree t2)
215 enum tree_code code1;
216 enum tree_code code2;
219 /* Save time if the two types are the same. */
221 if (t1 == t2) return t1;
223 /* If one type is nonsense, use the other. */
224 if (t1 == error_mark_node)
226 if (t2 == error_mark_node)
229 /* Merge the attributes. */
230 attributes = (*targetm.merge_type_attributes) (t1, t2);
232 /* Treat an enum type as the unsigned integer type of the same width. */
234 if (TREE_CODE (t1) == ENUMERAL_TYPE)
235 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
236 if (TREE_CODE (t2) == ENUMERAL_TYPE)
237 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
239 code1 = TREE_CODE (t1);
240 code2 = TREE_CODE (t2);
242 /* If one type is complex, form the common type of the non-complex
243 components, then make that complex. Use T1 or T2 if it is the
245 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
247 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
248 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
249 tree subtype = common_type (subtype1, subtype2);
251 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
252 return build_type_attribute_variant (t1, attributes);
253 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
254 return build_type_attribute_variant (t2, attributes);
256 return build_type_attribute_variant (build_complex_type (subtype),
264 /* If only one is real, use it as the result. */
266 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
267 return build_type_attribute_variant (t1, attributes);
269 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
270 return build_type_attribute_variant (t2, attributes);
272 /* Both real or both integers; use the one with greater precision. */
274 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
275 return build_type_attribute_variant (t1, attributes);
276 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
277 return build_type_attribute_variant (t2, attributes);
279 /* Same precision. Prefer longs to ints even when same size. */
281 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
282 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
283 return build_type_attribute_variant (long_unsigned_type_node,
286 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
287 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
289 /* But preserve unsignedness from the other type,
290 since long cannot hold all the values of an unsigned int. */
291 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
292 t1 = long_unsigned_type_node;
294 t1 = long_integer_type_node;
295 return build_type_attribute_variant (t1, attributes);
298 /* Likewise, prefer long double to double even if same size. */
299 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
300 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
301 return build_type_attribute_variant (long_double_type_node,
304 /* Otherwise prefer the unsigned one. */
306 if (TREE_UNSIGNED (t1))
307 return build_type_attribute_variant (t1, attributes);
309 return build_type_attribute_variant (t2, attributes);
312 /* For two pointers, do this recursively on the target type,
313 and combine the qualifiers of the two types' targets. */
314 /* This code was turned off; I don't know why.
315 But ANSI C specifies doing this with the qualifiers.
316 So I turned it on again. */
318 tree pointed_to_1 = TREE_TYPE (t1);
319 tree pointed_to_2 = TREE_TYPE (t2);
320 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
321 TYPE_MAIN_VARIANT (pointed_to_2));
322 t1 = build_pointer_type (c_build_qualified_type
324 TYPE_QUALS (pointed_to_1) |
325 TYPE_QUALS (pointed_to_2)));
326 return build_type_attribute_variant (t1, attributes);
331 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
332 /* Save space: see if the result is identical to one of the args. */
333 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
334 return build_type_attribute_variant (t1, attributes);
335 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
336 return build_type_attribute_variant (t2, attributes);
337 /* Merge the element types, and have a size if either arg has one. */
338 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
339 return build_type_attribute_variant (t1, attributes);
343 /* Function types: prefer the one that specified arg types.
344 If both do, merge the arg types. Also merge the return types. */
346 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
347 tree p1 = TYPE_ARG_TYPES (t1);
348 tree p2 = TYPE_ARG_TYPES (t2);
353 /* Save space: see if the result is identical to one of the args. */
354 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
355 return build_type_attribute_variant (t1, attributes);
356 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
357 return build_type_attribute_variant (t2, attributes);
359 /* Simple way if one arg fails to specify argument types. */
360 if (TYPE_ARG_TYPES (t1) == 0)
362 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
363 return build_type_attribute_variant (t1, attributes);
365 if (TYPE_ARG_TYPES (t2) == 0)
367 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
368 return build_type_attribute_variant (t1, attributes);
371 /* If both args specify argument types, we must merge the two
372 lists, argument by argument. */
375 declare_parm_level ();
377 len = list_length (p1);
380 for (i = 0; i < len; i++)
381 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
386 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
388 /* A null type means arg type is not specified.
389 Take whatever the other function type has. */
390 if (TREE_VALUE (p1) == 0)
392 TREE_VALUE (n) = TREE_VALUE (p2);
395 if (TREE_VALUE (p2) == 0)
397 TREE_VALUE (n) = TREE_VALUE (p1);
401 /* Given wait (union {union wait *u; int *i} *)
402 and wait (union wait *),
403 prefer union wait * as type of parm. */
404 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
405 && TREE_VALUE (p1) != TREE_VALUE (p2))
408 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
409 memb; memb = TREE_CHAIN (memb))
410 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
413 TREE_VALUE (n) = TREE_VALUE (p2);
415 pedwarn ("function types not truly compatible in ISO C");
419 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
420 && TREE_VALUE (p2) != TREE_VALUE (p1))
423 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
424 memb; memb = TREE_CHAIN (memb))
425 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
428 TREE_VALUE (n) = TREE_VALUE (p1);
430 pedwarn ("function types not truly compatible in ISO C");
434 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
440 t1 = build_function_type (valtype, newargs);
441 /* ... falls through ... */
445 return build_type_attribute_variant (t1, attributes);
450 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
451 or various other operations. Return 2 if they are compatible
452 but a warning may be needed if you use them together. */
455 comptypes (tree type1, tree type2, int flags)
461 /* Suppress errors caused by previously reported errors. */
463 if (t1 == t2 || !t1 || !t2
464 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
467 /* If either type is the internal version of sizetype, return the
469 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
470 && TYPE_DOMAIN (t1) != 0)
471 t1 = TYPE_DOMAIN (t1);
473 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
474 && TYPE_DOMAIN (t2) != 0)
475 t2 = TYPE_DOMAIN (t2);
477 /* Enumerated types are compatible with integer types, but this is
478 not transitive: two enumerated types in the same translation unit
479 are compatible with each other only if they are the same type. */
481 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
482 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
483 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
484 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
489 /* Different classes of types can't be compatible. */
491 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
493 /* Qualifiers must match. */
495 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
498 /* Allow for two different type nodes which have essentially the same
499 definition. Note that we already checked for equality of the type
500 qualifiers (just above). */
502 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
505 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
506 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
509 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
512 switch (TREE_CODE (t1))
515 /* We must give ObjC the first crack at comparing pointers, since
516 protocol qualifiers may be involved. */
517 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
519 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
520 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
524 val = function_types_compatible_p (t1, t2, flags);
529 tree d1 = TYPE_DOMAIN (t1);
530 tree d2 = TYPE_DOMAIN (t2);
531 bool d1_variable, d2_variable;
532 bool d1_zero, d2_zero;
535 /* Target types must match incl. qualifiers. */
536 if (TREE_TYPE (t1) != TREE_TYPE (t2)
537 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
541 /* Sizes must match unless one is missing or variable. */
542 if (d1 == 0 || d2 == 0 || d1 == d2)
545 d1_zero = ! TYPE_MAX_VALUE (d1);
546 d2_zero = ! TYPE_MAX_VALUE (d2);
548 d1_variable = (! d1_zero
549 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
550 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
551 d2_variable = (! d2_zero
552 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
553 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
555 if (d1_variable || d2_variable)
557 if (d1_zero && d2_zero)
559 if (d1_zero || d2_zero
560 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
561 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
568 /* We are dealing with two distinct structs. In assorted Objective-C
569 corner cases, however, these can still be deemed equivalent. */
570 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
575 if (val != 1 && !same_translation_unit_p (t1, t2))
576 val = tagged_types_tu_compatible_p (t1, t2, flags);
580 /* The target might allow certain vector types to be compatible. */
581 val = (*targetm.vector_opaque_p) (t1)
582 || (*targetm.vector_opaque_p) (t2);
588 return attrval == 2 && val == 1 ? 2 : val;
591 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
592 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
593 to 1 or 0 depending if the check of the pointer types is meant to
594 be reflexive or not (typically, assignments are not reflexive,
595 while comparisons are reflexive).
599 comp_target_types (tree ttl, tree ttr, int reflexive)
603 /* Give objc_comptypes a crack at letting these types through. */
604 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
607 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
608 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
610 if (val == 2 && pedantic)
611 pedwarn ("types are not quite compatible");
615 /* Subroutines of `comptypes'. */
617 /* Determine whether two types derive from the same translation unit.
618 If the CONTEXT chain ends in a null, that type's context is still
619 being parsed, so if two types have context chains ending in null,
620 they're in the same translation unit. */
622 same_translation_unit_p (tree t1, tree t2)
624 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
625 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
627 case 'd': t1 = DECL_CONTEXT (t1); break;
628 case 't': t1 = TYPE_CONTEXT (t1); break;
629 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
633 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
634 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
636 case 'd': t2 = DECL_CONTEXT (t1); break;
637 case 't': t2 = TYPE_CONTEXT (t2); break;
638 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
645 /* The C standard says that two structures in different translation
646 units are compatible with each other only if the types of their
647 fields are compatible (among other things). So, consider two copies
648 of this structure: */
650 struct tagged_tu_seen {
651 const struct tagged_tu_seen * next;
656 /* Can they be compatible with each other? We choose to break the
657 recursion by allowing those types to be compatible. */
659 static const struct tagged_tu_seen * tagged_tu_seen_base;
661 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
662 compatible. If the two types are not the same (which has been
663 checked earlier), this can only happen when multiple translation
664 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
668 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
671 bool needs_warning = false;
673 /* We have to verify that the tags of the types are the same. This
674 is harder than it looks because this may be a typedef, so we have
675 to go look at the original type. It may even be a typedef of a
677 while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
678 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
680 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
681 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
683 /* C90 didn't have the requirement that the two tags be the same. */
684 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
687 /* C90 didn't say what happened if one or both of the types were
688 incomplete; we choose to follow C99 rules here, which is that they
690 if (TYPE_SIZE (t1) == NULL
691 || TYPE_SIZE (t2) == NULL)
695 const struct tagged_tu_seen * tts_i;
696 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
697 if (tts_i->t1 == t1 && tts_i->t2 == t2)
701 switch (TREE_CODE (t1))
705 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
708 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
710 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
712 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
720 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
723 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
726 struct tagged_tu_seen tts;
728 tts.next = tagged_tu_seen_base;
731 tagged_tu_seen_base = &tts;
733 if (DECL_NAME (s1) != NULL)
734 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
735 if (DECL_NAME (s1) == DECL_NAME (s2))
738 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
742 needs_warning = true;
744 if (TREE_CODE (s1) == FIELD_DECL
745 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
746 DECL_FIELD_BIT_OFFSET (s2)) != 1)
752 tagged_tu_seen_base = tts.next;
756 return needs_warning ? 2 : 1;
761 struct tagged_tu_seen tts;
763 tts.next = tagged_tu_seen_base;
766 tagged_tu_seen_base = &tts;
768 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
770 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
773 if (TREE_CODE (s1) != TREE_CODE (s2)
774 || DECL_NAME (s1) != DECL_NAME (s2))
776 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
780 needs_warning = true;
782 if (TREE_CODE (s1) == FIELD_DECL
783 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
784 DECL_FIELD_BIT_OFFSET (s2)) != 1)
787 tagged_tu_seen_base = tts.next;
790 return needs_warning ? 2 : 1;
798 /* Return 1 if two function types F1 and F2 are compatible.
799 If either type specifies no argument types,
800 the other must specify a fixed number of self-promoting arg types.
801 Otherwise, if one type specifies only the number of arguments,
802 the other must specify that number of self-promoting arg types.
803 Otherwise, the argument types must match. */
806 function_types_compatible_p (tree f1, tree f2, int flags)
809 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
814 ret1 = TREE_TYPE (f1);
815 ret2 = TREE_TYPE (f2);
817 /* 'volatile' qualifiers on a function's return type mean the function
819 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
820 pedwarn ("function return types not compatible due to `volatile'");
821 if (TYPE_VOLATILE (ret1))
822 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
823 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
824 if (TYPE_VOLATILE (ret2))
825 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
826 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
827 val = comptypes (ret1, ret2, flags);
831 args1 = TYPE_ARG_TYPES (f1);
832 args2 = TYPE_ARG_TYPES (f2);
834 /* An unspecified parmlist matches any specified parmlist
835 whose argument types don't need default promotions. */
839 if (!self_promoting_args_p (args2))
841 /* If one of these types comes from a non-prototype fn definition,
842 compare that with the other type's arglist.
843 If they don't match, ask for a warning (but no error). */
844 if (TYPE_ACTUAL_ARG_TYPES (f1)
845 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
852 if (!self_promoting_args_p (args1))
854 if (TYPE_ACTUAL_ARG_TYPES (f2)
855 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
861 /* Both types have argument lists: compare them and propagate results. */
862 val1 = type_lists_compatible_p (args1, args2, flags);
863 return val1 != 1 ? val1 : val;
866 /* Check two lists of types for compatibility,
867 returning 0 for incompatible, 1 for compatible,
868 or 2 for compatible with warning. */
871 type_lists_compatible_p (tree args1, tree args2, int flags)
873 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
879 if (args1 == 0 && args2 == 0)
881 /* If one list is shorter than the other,
882 they fail to match. */
883 if (args1 == 0 || args2 == 0)
885 /* A null pointer instead of a type
886 means there is supposed to be an argument
887 but nothing is specified about what type it has.
888 So match anything that self-promotes. */
889 if (TREE_VALUE (args1) == 0)
891 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
894 else if (TREE_VALUE (args2) == 0)
896 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
899 /* If one of the lists has an error marker, ignore this arg. */
900 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
901 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
903 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
904 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
907 /* Allow wait (union {union wait *u; int *i} *)
908 and wait (union wait *) to be compatible. */
909 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
910 && (TYPE_NAME (TREE_VALUE (args1)) == 0
911 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
912 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
913 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
914 TYPE_SIZE (TREE_VALUE (args2))))
917 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
918 memb; memb = TREE_CHAIN (memb))
919 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
925 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
926 && (TYPE_NAME (TREE_VALUE (args2)) == 0
927 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
928 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
929 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
930 TYPE_SIZE (TREE_VALUE (args1))))
933 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
934 memb; memb = TREE_CHAIN (memb))
935 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
945 /* comptypes said ok, but record if it said to warn. */
949 args1 = TREE_CHAIN (args1);
950 args2 = TREE_CHAIN (args2);
954 /* Compute the size to increment a pointer by. */
957 c_size_in_bytes (tree type)
959 enum tree_code code = TREE_CODE (type);
961 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
962 return size_one_node;
964 if (!COMPLETE_OR_VOID_TYPE_P (type))
966 error ("arithmetic on pointer to an incomplete type");
967 return size_one_node;
970 /* Convert in case a char is more than one unit. */
971 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
972 size_int (TYPE_PRECISION (char_type_node)
976 /* Return either DECL or its known constant value (if it has one). */
979 decl_constant_value (tree decl)
981 if (/* Don't change a variable array bound or initial value to a constant
982 in a place where a variable is invalid. */
983 current_function_decl != 0
984 && ! TREE_THIS_VOLATILE (decl)
985 && TREE_READONLY (decl)
986 && DECL_INITIAL (decl) != 0
987 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
988 /* This is invalid if initial value is not constant.
989 If it has either a function call, a memory reference,
990 or a variable, then re-evaluating it could give different results. */
991 && TREE_CONSTANT (DECL_INITIAL (decl))
992 /* Check for cases where this is sub-optimal, even though valid. */
993 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
994 return DECL_INITIAL (decl);
998 /* Return either DECL or its known constant value (if it has one), but
999 return DECL if pedantic or DECL has mode BLKmode. This is for
1000 bug-compatibility with the old behavior of decl_constant_value
1001 (before GCC 3.0); every use of this function is a bug and it should
1002 be removed before GCC 3.1. It is not appropriate to use pedantic
1003 in a way that affects optimization, and BLKmode is probably not the
1004 right test for avoiding misoptimizations either. */
1007 decl_constant_value_for_broken_optimization (tree decl)
1009 if (pedantic || DECL_MODE (decl) == BLKmode)
1012 return decl_constant_value (decl);
1016 /* Perform the default conversion of arrays and functions to pointers.
1017 Return the result of converting EXP. For any other expression, just
1021 default_function_array_conversion (tree exp)
1024 tree type = TREE_TYPE (exp);
1025 enum tree_code code = TREE_CODE (type);
1028 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1031 Do not use STRIP_NOPS here! It will remove conversions from pointer
1032 to integer and cause infinite recursion. */
1034 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1035 || (TREE_CODE (exp) == NOP_EXPR
1036 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1038 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1040 exp = TREE_OPERAND (exp, 0);
1043 /* Preserve the original expression code. */
1044 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1045 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1047 if (code == FUNCTION_TYPE)
1049 return build_unary_op (ADDR_EXPR, exp, 0);
1051 if (code == ARRAY_TYPE)
1054 tree restype = TREE_TYPE (type);
1060 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1062 constp = TREE_READONLY (exp);
1063 volatilep = TREE_THIS_VOLATILE (exp);
1066 if (TYPE_QUALS (type) || constp || volatilep)
1068 = c_build_qualified_type (restype,
1070 | (constp * TYPE_QUAL_CONST)
1071 | (volatilep * TYPE_QUAL_VOLATILE));
1073 if (TREE_CODE (exp) == INDIRECT_REF)
1074 return convert (TYPE_POINTER_TO (restype),
1075 TREE_OPERAND (exp, 0));
1077 if (TREE_CODE (exp) == COMPOUND_EXPR)
1079 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1080 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1081 TREE_OPERAND (exp, 0), op1);
1084 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1085 if (!flag_isoc99 && !lvalue_array_p)
1087 /* Before C99, non-lvalue arrays do not decay to pointers.
1088 Normally, using such an array would be invalid; but it can
1089 be used correctly inside sizeof or as a statement expression.
1090 Thus, do not give an error here; an error will result later. */
1094 ptrtype = build_pointer_type (restype);
1096 if (TREE_CODE (exp) == VAR_DECL)
1098 /* ??? This is not really quite correct
1099 in that the type of the operand of ADDR_EXPR
1100 is not the target type of the type of the ADDR_EXPR itself.
1101 Question is, can this lossage be avoided? */
1102 adr = build1 (ADDR_EXPR, ptrtype, exp);
1103 if (!c_mark_addressable (exp))
1104 return error_mark_node;
1105 TREE_CONSTANT (adr) = staticp (exp);
1106 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1109 /* This way is better for a COMPONENT_REF since it can
1110 simplify the offset for a component. */
1111 adr = build_unary_op (ADDR_EXPR, exp, 1);
1112 return convert (ptrtype, adr);
1117 /* Perform default promotions for C data used in expressions.
1118 Arrays and functions are converted to pointers;
1119 enumeral types or short or char, to int.
1120 In addition, manifest constants symbols are replaced by their values. */
1123 default_conversion (tree exp)
1126 tree type = TREE_TYPE (exp);
1127 enum tree_code code = TREE_CODE (type);
1129 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1130 return default_function_array_conversion (exp);
1132 /* Constants can be used directly unless they're not loadable. */
1133 if (TREE_CODE (exp) == CONST_DECL)
1134 exp = DECL_INITIAL (exp);
1136 /* Replace a nonvolatile const static variable with its value unless
1137 it is an array, in which case we must be sure that taking the
1138 address of the array produces consistent results. */
1139 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1141 exp = decl_constant_value_for_broken_optimization (exp);
1142 type = TREE_TYPE (exp);
1145 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1148 Do not use STRIP_NOPS here! It will remove conversions from pointer
1149 to integer and cause infinite recursion. */
1151 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1152 || (TREE_CODE (exp) == NOP_EXPR
1153 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1154 exp = TREE_OPERAND (exp, 0);
1156 /* Preserve the original expression code. */
1157 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1158 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1160 /* Normally convert enums to int,
1161 but convert wide enums to something wider. */
1162 if (code == ENUMERAL_TYPE)
1164 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1165 TYPE_PRECISION (integer_type_node)),
1166 ((TYPE_PRECISION (type)
1167 >= TYPE_PRECISION (integer_type_node))
1168 && TREE_UNSIGNED (type)));
1170 return convert (type, exp);
1173 if (TREE_CODE (exp) == COMPONENT_REF
1174 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1175 /* If it's thinner than an int, promote it like a
1176 c_promoting_integer_type_p, otherwise leave it alone. */
1177 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1178 TYPE_PRECISION (integer_type_node)))
1179 return convert (integer_type_node, exp);
1181 if (c_promoting_integer_type_p (type))
1183 /* Preserve unsignedness if not really getting any wider. */
1184 if (TREE_UNSIGNED (type)
1185 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1186 return convert (unsigned_type_node, exp);
1188 return convert (integer_type_node, exp);
1191 if (code == VOID_TYPE)
1193 error ("void value not ignored as it ought to be");
1194 return error_mark_node;
1199 /* Look up COMPONENT in a structure or union DECL.
1201 If the component name is not found, returns NULL_TREE. Otherwise,
1202 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1203 stepping down the chain to the component, which is in the last
1204 TREE_VALUE of the list. Normally the list is of length one, but if
1205 the component is embedded within (nested) anonymous structures or
1206 unions, the list steps down the chain to the component. */
1209 lookup_field (tree decl, tree component)
1211 tree type = TREE_TYPE (decl);
1214 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1215 to the field elements. Use a binary search on this array to quickly
1216 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1217 will always be set for structures which have many elements. */
1219 if (TYPE_LANG_SPECIFIC (type))
1222 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1224 field = TYPE_FIELDS (type);
1226 top = TYPE_LANG_SPECIFIC (type)->s->len;
1227 while (top - bot > 1)
1229 half = (top - bot + 1) >> 1;
1230 field = field_array[bot+half];
1232 if (DECL_NAME (field) == NULL_TREE)
1234 /* Step through all anon unions in linear fashion. */
1235 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1237 field = field_array[bot++];
1238 if (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);
1248 /* Entire record is only anon unions. */
1252 /* Restart the binary search, with new lower bound. */
1256 if (DECL_NAME (field) == component)
1258 if (DECL_NAME (field) < component)
1264 if (DECL_NAME (field_array[bot]) == component)
1265 field = field_array[bot];
1266 else if (DECL_NAME (field) != component)
1271 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1273 if (DECL_NAME (field) == NULL_TREE
1274 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1275 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1277 tree anon = lookup_field (field, component);
1280 return tree_cons (NULL_TREE, field, anon);
1283 if (DECL_NAME (field) == component)
1287 if (field == NULL_TREE)
1291 return tree_cons (NULL_TREE, field, NULL_TREE);
1294 /* Make an expression to refer to the COMPONENT field of
1295 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1298 build_component_ref (tree datum, tree component)
1300 tree type = TREE_TYPE (datum);
1301 enum tree_code code = TREE_CODE (type);
1305 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1306 If pedantic ensure that the arguments are not lvalues; otherwise,
1307 if the component is an array, it would wrongly decay to a pointer in
1309 We cannot do this with a COND_EXPR, because in a conditional expression
1310 the default promotions are applied to both sides, and this would yield
1311 the wrong type of the result; for example, if the components have
1313 switch (TREE_CODE (datum))
1317 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1318 return build (COMPOUND_EXPR, TREE_TYPE (value),
1319 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1325 /* See if there is a field or component with name COMPONENT. */
1327 if (code == RECORD_TYPE || code == UNION_TYPE)
1329 if (!COMPLETE_TYPE_P (type))
1331 c_incomplete_type_error (NULL_TREE, type);
1332 return error_mark_node;
1335 field = lookup_field (datum, component);
1339 error ("%s has no member named `%s'",
1340 code == RECORD_TYPE ? "structure" : "union",
1341 IDENTIFIER_POINTER (component));
1342 return error_mark_node;
1345 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1346 This might be better solved in future the way the C++ front
1347 end does it - by giving the anonymous entities each a
1348 separate name and type, and then have build_component_ref
1349 recursively call itself. We can't do that here. */
1352 tree subdatum = TREE_VALUE (field);
1354 if (TREE_TYPE (subdatum) == error_mark_node)
1355 return error_mark_node;
1357 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1358 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1359 TREE_READONLY (ref) = 1;
1360 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1361 TREE_THIS_VOLATILE (ref) = 1;
1363 if (TREE_DEPRECATED (subdatum))
1364 warn_deprecated_use (subdatum);
1368 field = TREE_CHAIN (field);
1374 else if (code != ERROR_MARK)
1375 error ("request for member `%s' in something not a structure or union",
1376 IDENTIFIER_POINTER (component));
1378 return error_mark_node;
1381 /* Given an expression PTR for a pointer, return an expression
1382 for the value pointed to.
1383 ERRORSTRING is the name of the operator to appear in error messages. */
1386 build_indirect_ref (tree ptr, const char *errorstring)
1388 tree pointer = default_conversion (ptr);
1389 tree type = TREE_TYPE (pointer);
1391 if (TREE_CODE (type) == POINTER_TYPE)
1393 if (TREE_CODE (pointer) == ADDR_EXPR
1394 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1395 == TREE_TYPE (type)))
1396 return TREE_OPERAND (pointer, 0);
1399 tree t = TREE_TYPE (type);
1400 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1402 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1404 error ("dereferencing pointer to incomplete type");
1405 return error_mark_node;
1407 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1408 warning ("dereferencing `void *' pointer");
1410 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1411 so that we get the proper error message if the result is used
1412 to assign to. Also, &* is supposed to be a no-op.
1413 And ANSI C seems to specify that the type of the result
1414 should be the const type. */
1415 /* A de-reference of a pointer to const is not a const. It is valid
1416 to change it via some other pointer. */
1417 TREE_READONLY (ref) = TYPE_READONLY (t);
1418 TREE_SIDE_EFFECTS (ref)
1419 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1420 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1424 else if (TREE_CODE (pointer) != ERROR_MARK)
1425 error ("invalid type argument of `%s'", errorstring);
1426 return error_mark_node;
1429 /* This handles expressions of the form "a[i]", which denotes
1432 This is logically equivalent in C to *(a+i), but we may do it differently.
1433 If A is a variable or a member, we generate a primitive ARRAY_REF.
1434 This avoids forcing the array out of registers, and can work on
1435 arrays that are not lvalues (for example, members of structures returned
1439 build_array_ref (tree array, tree index)
1443 error ("subscript missing in array reference");
1444 return error_mark_node;
1447 if (TREE_TYPE (array) == error_mark_node
1448 || TREE_TYPE (index) == error_mark_node)
1449 return error_mark_node;
1451 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1452 && TREE_CODE (array) != INDIRECT_REF)
1456 /* Subscripting with type char is likely to lose
1457 on a machine where chars are signed.
1458 So warn on any machine, but optionally.
1459 Don't warn for unsigned char since that type is safe.
1460 Don't warn for signed char because anyone who uses that
1461 must have done so deliberately. */
1462 if (warn_char_subscripts
1463 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1464 warning ("array subscript has type `char'");
1466 /* Apply default promotions *after* noticing character types. */
1467 index = default_conversion (index);
1469 /* Require integer *after* promotion, for sake of enums. */
1470 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1472 error ("array subscript is not an integer");
1473 return error_mark_node;
1476 /* An array that is indexed by a non-constant
1477 cannot be stored in a register; we must be able to do
1478 address arithmetic on its address.
1479 Likewise an array of elements of variable size. */
1480 if (TREE_CODE (index) != INTEGER_CST
1481 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1482 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1484 if (!c_mark_addressable (array))
1485 return error_mark_node;
1487 /* An array that is indexed by a constant value which is not within
1488 the array bounds cannot be stored in a register either; because we
1489 would get a crash in store_bit_field/extract_bit_field when trying
1490 to access a non-existent part of the register. */
1491 if (TREE_CODE (index) == INTEGER_CST
1492 && TYPE_VALUES (TREE_TYPE (array))
1493 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1495 if (!c_mark_addressable (array))
1496 return error_mark_node;
1502 while (TREE_CODE (foo) == COMPONENT_REF)
1503 foo = TREE_OPERAND (foo, 0);
1504 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1505 pedwarn ("ISO C forbids subscripting `register' array");
1506 else if (! flag_isoc99 && ! lvalue_p (foo))
1507 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1510 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1511 rval = build (ARRAY_REF, type, array, index);
1512 /* Array ref is const/volatile if the array elements are
1513 or if the array is. */
1514 TREE_READONLY (rval)
1515 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1516 | TREE_READONLY (array));
1517 TREE_SIDE_EFFECTS (rval)
1518 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1519 | TREE_SIDE_EFFECTS (array));
1520 TREE_THIS_VOLATILE (rval)
1521 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1522 /* This was added by rms on 16 Nov 91.
1523 It fixes vol struct foo *a; a->elts[1]
1524 in an inline function.
1525 Hope it doesn't break something else. */
1526 | TREE_THIS_VOLATILE (array));
1527 return require_complete_type (fold (rval));
1531 tree ar = default_conversion (array);
1532 tree ind = default_conversion (index);
1534 /* Do the same warning check as above, but only on the part that's
1535 syntactically the index and only if it is also semantically
1537 if (warn_char_subscripts
1538 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1539 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1540 warning ("subscript has type `char'");
1542 /* Put the integer in IND to simplify error checking. */
1543 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1550 if (ar == error_mark_node)
1553 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1554 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1556 error ("subscripted value is neither array nor pointer");
1557 return error_mark_node;
1559 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1561 error ("array subscript is not an integer");
1562 return error_mark_node;
1565 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1570 /* Build an external reference to identifier ID. FUN indicates
1571 whether this will be used for a function call. */
1573 build_external_ref (tree id, int fun)
1576 tree decl = lookup_name (id);
1577 tree objc_ivar = lookup_objc_ivar (id);
1579 if (decl && decl != error_mark_node)
1581 /* Properly declared variable or function reference. */
1584 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1586 warning ("local declaration of `%s' hides instance variable",
1587 IDENTIFIER_POINTER (id));
1596 /* Implicit function declaration. */
1597 ref = implicitly_declare (id);
1598 else if (decl == error_mark_node)
1599 /* Don't complain about something that's already been
1600 complained about. */
1601 return error_mark_node;
1604 undeclared_variable (id);
1605 return error_mark_node;
1608 if (TREE_TYPE (ref) == error_mark_node)
1609 return error_mark_node;
1611 if (TREE_DEPRECATED (ref))
1612 warn_deprecated_use (ref);
1614 if (!skip_evaluation)
1615 assemble_external (ref);
1616 TREE_USED (ref) = 1;
1618 if (TREE_CODE (ref) == CONST_DECL)
1620 ref = DECL_INITIAL (ref);
1621 TREE_CONSTANT (ref) = 1;
1623 else if (current_function_decl != 0
1624 && !DECL_FILE_SCOPE_P (current_function_decl)
1625 && (TREE_CODE (ref) == VAR_DECL
1626 || TREE_CODE (ref) == PARM_DECL
1627 || TREE_CODE (ref) == FUNCTION_DECL))
1629 tree context = decl_function_context (ref);
1631 if (context != 0 && context != current_function_decl)
1632 DECL_NONLOCAL (ref) = 1;
1638 /* Build a function call to function FUNCTION with parameters PARAMS.
1639 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1640 TREE_VALUE of each node is a parameter-expression.
1641 FUNCTION's data type may be a function type or a pointer-to-function. */
1644 build_function_call (tree function, tree params)
1646 tree fntype, fundecl = 0;
1647 tree coerced_params;
1648 tree name = NULL_TREE, result;
1651 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1652 STRIP_TYPE_NOPS (function);
1654 /* Convert anything with function type to a pointer-to-function. */
1655 if (TREE_CODE (function) == FUNCTION_DECL)
1657 name = DECL_NAME (function);
1659 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1660 (because calling an inline function does not mean the function
1661 needs to be separately compiled). */
1662 fntype = build_type_variant (TREE_TYPE (function),
1663 TREE_READONLY (function),
1664 TREE_THIS_VOLATILE (function));
1666 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1669 function = default_conversion (function);
1671 fntype = TREE_TYPE (function);
1673 if (TREE_CODE (fntype) == ERROR_MARK)
1674 return error_mark_node;
1676 if (!(TREE_CODE (fntype) == POINTER_TYPE
1677 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1679 error ("called object is not a function");
1680 return error_mark_node;
1683 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1684 current_function_returns_abnormally = 1;
1686 /* fntype now gets the type of function pointed to. */
1687 fntype = TREE_TYPE (fntype);
1689 /* Check that the function is called through a compatible prototype.
1690 If it is not, replace the call by a trap, wrapped up in a compound
1691 expression if necessary. This has the nice side-effect to prevent
1692 the tree-inliner from generating invalid assignment trees which may
1693 blow up in the RTL expander later.
1695 ??? This doesn't work for Objective-C because objc_comptypes
1696 refuses to compare function prototypes, yet the compiler appears
1697 to build calls that are flagged as invalid by C's comptypes. */
1698 if (! c_dialect_objc ()
1699 && TREE_CODE (function) == NOP_EXPR
1700 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1701 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1702 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1704 tree return_type = TREE_TYPE (fntype);
1705 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1708 /* This situation leads to run-time undefined behavior. We can't,
1709 therefore, simply error unless we can prove that all possible
1710 executions of the program must execute the code. */
1711 warning ("function called through a non-compatible type");
1713 if (VOID_TYPE_P (return_type))
1719 if (AGGREGATE_TYPE_P (return_type))
1720 rhs = build_compound_literal (return_type,
1721 build_constructor (return_type,
1724 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1726 return build (COMPOUND_EXPR, return_type, trap, rhs);
1730 /* Convert the parameters to the types declared in the
1731 function prototype, or apply default promotions. */
1734 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1736 /* Check that the arguments to the function are valid. */
1738 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1740 /* Recognize certain built-in functions so we can make tree-codes
1741 other than CALL_EXPR. We do this when it enables fold-const.c
1742 to do something useful. */
1744 if (TREE_CODE (function) == ADDR_EXPR
1745 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1746 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1748 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1749 params, coerced_params);
1754 result = build (CALL_EXPR, TREE_TYPE (fntype),
1755 function, coerced_params, NULL_TREE);
1756 TREE_SIDE_EFFECTS (result) = 1;
1757 result = fold (result);
1759 if (VOID_TYPE_P (TREE_TYPE (result)))
1761 return require_complete_type (result);
1764 /* Convert the argument expressions in the list VALUES
1765 to the types in the list TYPELIST. The result is a list of converted
1766 argument expressions.
1768 If TYPELIST is exhausted, or when an element has NULL as its type,
1769 perform the default conversions.
1771 PARMLIST is the chain of parm decls for the function being called.
1772 It may be 0, if that info is not available.
1773 It is used only for generating error messages.
1775 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1777 This is also where warnings about wrong number of args are generated.
1779 Both VALUES and the returned value are chains of TREE_LIST nodes
1780 with the elements of the list in the TREE_VALUE slots of those nodes. */
1783 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1785 tree typetail, valtail;
1789 /* Scan the given expressions and types, producing individual
1790 converted arguments and pushing them on RESULT in reverse order. */
1792 for (valtail = values, typetail = typelist, parmnum = 0;
1794 valtail = TREE_CHAIN (valtail), parmnum++)
1796 tree type = typetail ? TREE_VALUE (typetail) : 0;
1797 tree val = TREE_VALUE (valtail);
1799 if (type == void_type_node)
1802 error ("too many arguments to function `%s'",
1803 IDENTIFIER_POINTER (name));
1805 error ("too many arguments to function");
1809 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1810 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1811 to convert automatically to a pointer. */
1812 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1813 val = TREE_OPERAND (val, 0);
1815 val = default_function_array_conversion (val);
1817 val = require_complete_type (val);
1821 /* Formal parm type is specified by a function prototype. */
1824 if (!COMPLETE_TYPE_P (type))
1826 error ("type of formal parameter %d is incomplete", parmnum + 1);
1831 /* Optionally warn about conversions that
1832 differ from the default conversions. */
1833 if (warn_conversion || warn_traditional)
1835 int formal_prec = TYPE_PRECISION (type);
1837 if (INTEGRAL_TYPE_P (type)
1838 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1839 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1840 if (INTEGRAL_TYPE_P (type)
1841 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1842 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1843 else if (TREE_CODE (type) == COMPLEX_TYPE
1844 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1845 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1846 else if (TREE_CODE (type) == REAL_TYPE
1847 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1848 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1849 else if (TREE_CODE (type) == COMPLEX_TYPE
1850 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1851 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1852 else if (TREE_CODE (type) == REAL_TYPE
1853 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1854 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1855 /* ??? At some point, messages should be written about
1856 conversions between complex types, but that's too messy
1858 else if (TREE_CODE (type) == REAL_TYPE
1859 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1861 /* Warn if any argument is passed as `float',
1862 since without a prototype it would be `double'. */
1863 if (formal_prec == TYPE_PRECISION (float_type_node))
1864 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1866 /* Detect integer changing in width or signedness.
1867 These warnings are only activated with
1868 -Wconversion, not with -Wtraditional. */
1869 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1870 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1872 tree would_have_been = default_conversion (val);
1873 tree type1 = TREE_TYPE (would_have_been);
1875 if (TREE_CODE (type) == ENUMERAL_TYPE
1876 && (TYPE_MAIN_VARIANT (type)
1877 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1878 /* No warning if function asks for enum
1879 and the actual arg is that enum type. */
1881 else if (formal_prec != TYPE_PRECISION (type1))
1882 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1883 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1885 /* Don't complain if the formal parameter type
1886 is an enum, because we can't tell now whether
1887 the value was an enum--even the same enum. */
1888 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1890 else if (TREE_CODE (val) == INTEGER_CST
1891 && int_fits_type_p (val, type))
1892 /* Change in signedness doesn't matter
1893 if a constant value is unaffected. */
1895 /* Likewise for a constant in a NOP_EXPR. */
1896 else if (TREE_CODE (val) == NOP_EXPR
1897 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1898 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1900 /* If the value is extended from a narrower
1901 unsigned type, it doesn't matter whether we
1902 pass it as signed or unsigned; the value
1903 certainly is the same either way. */
1904 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1905 && TREE_UNSIGNED (TREE_TYPE (val)))
1907 else if (TREE_UNSIGNED (type))
1908 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1910 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1914 parmval = convert_for_assignment (type, val,
1915 (char *) 0, /* arg passing */
1916 fundecl, name, parmnum + 1);
1918 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1919 && INTEGRAL_TYPE_P (type)
1920 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1921 parmval = default_conversion (parmval);
1923 result = tree_cons (NULL_TREE, parmval, result);
1925 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1926 && (TYPE_PRECISION (TREE_TYPE (val))
1927 < TYPE_PRECISION (double_type_node)))
1928 /* Convert `float' to `double'. */
1929 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1931 /* Convert `short' and `char' to full-size `int'. */
1932 result = tree_cons (NULL_TREE, default_conversion (val), result);
1935 typetail = TREE_CHAIN (typetail);
1938 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1941 error ("too few arguments to function `%s'",
1942 IDENTIFIER_POINTER (name));
1944 error ("too few arguments to function");
1947 return nreverse (result);
1950 /* This is the entry point used by the parser
1951 for binary operators in the input.
1952 In addition to constructing the expression,
1953 we check for operands that were written with other binary operators
1954 in a way that is likely to confuse the user. */
1957 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1959 tree result = build_binary_op (code, arg1, arg2, 1);
1962 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1963 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1964 enum tree_code code1 = ERROR_MARK;
1965 enum tree_code code2 = ERROR_MARK;
1967 if (TREE_CODE (result) == ERROR_MARK)
1968 return error_mark_node;
1970 if (IS_EXPR_CODE_CLASS (class1))
1971 code1 = C_EXP_ORIGINAL_CODE (arg1);
1972 if (IS_EXPR_CODE_CLASS (class2))
1973 code2 = C_EXP_ORIGINAL_CODE (arg2);
1975 /* Check for cases such as x+y<<z which users are likely
1976 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1977 is cleared to prevent these warnings. */
1978 if (warn_parentheses)
1980 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1982 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1983 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1984 warning ("suggest parentheses around + or - inside shift");
1987 if (code == TRUTH_ORIF_EXPR)
1989 if (code1 == TRUTH_ANDIF_EXPR
1990 || code2 == TRUTH_ANDIF_EXPR)
1991 warning ("suggest parentheses around && within ||");
1994 if (code == BIT_IOR_EXPR)
1996 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1997 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1998 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1999 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2000 warning ("suggest parentheses around arithmetic in operand of |");
2001 /* Check cases like x|y==z */
2002 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2003 warning ("suggest parentheses around comparison in operand of |");
2006 if (code == BIT_XOR_EXPR)
2008 if (code1 == BIT_AND_EXPR
2009 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2010 || code2 == BIT_AND_EXPR
2011 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2012 warning ("suggest parentheses around arithmetic in operand of ^");
2013 /* Check cases like x^y==z */
2014 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2015 warning ("suggest parentheses around comparison in operand of ^");
2018 if (code == BIT_AND_EXPR)
2020 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2021 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2022 warning ("suggest parentheses around + or - in operand of &");
2023 /* Check cases like x&y==z */
2024 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2025 warning ("suggest parentheses around comparison in operand of &");
2029 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2030 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2031 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2032 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2034 unsigned_conversion_warning (result, arg1);
2035 unsigned_conversion_warning (result, arg2);
2036 overflow_warning (result);
2038 class = TREE_CODE_CLASS (TREE_CODE (result));
2040 /* Record the code that was specified in the source,
2041 for the sake of warnings about confusing nesting. */
2042 if (IS_EXPR_CODE_CLASS (class))
2043 C_SET_EXP_ORIGINAL_CODE (result, code);
2046 int flag = TREE_CONSTANT (result);
2047 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2048 so that convert_for_assignment wouldn't strip it.
2049 That way, we got warnings for things like p = (1 - 1).
2050 But it turns out we should not get those warnings. */
2051 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2052 C_SET_EXP_ORIGINAL_CODE (result, code);
2053 TREE_CONSTANT (result) = flag;
2060 /* Return true if `t' is known to be non-negative. */
2063 c_tree_expr_nonnegative_p (tree t)
2065 if (TREE_CODE (t) == STMT_EXPR)
2067 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2069 /* Find the last statement in the chain, ignoring the final
2070 * scope statement */
2071 while (TREE_CHAIN (t) != NULL_TREE
2072 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2074 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2076 return tree_expr_nonnegative_p (t);
2079 /* Return a tree for the difference of pointers OP0 and OP1.
2080 The resulting tree has type int. */
2083 pointer_diff (tree op0, tree op1)
2085 tree result, folded;
2086 tree restype = ptrdiff_type_node;
2088 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2089 tree con0, con1, lit0, lit1;
2090 tree orig_op1 = op1;
2092 if (pedantic || warn_pointer_arith)
2094 if (TREE_CODE (target_type) == VOID_TYPE)
2095 pedwarn ("pointer of type `void *' used in subtraction");
2096 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2097 pedwarn ("pointer to a function used in subtraction");
2100 /* If the conversion to ptrdiff_type does anything like widening or
2101 converting a partial to an integral mode, we get a convert_expression
2102 that is in the way to do any simplifications.
2103 (fold-const.c doesn't know that the extra bits won't be needed.
2104 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2105 different mode in place.)
2106 So first try to find a common term here 'by hand'; we want to cover
2107 at least the cases that occur in legal static initializers. */
2108 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2109 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2111 if (TREE_CODE (con0) == PLUS_EXPR)
2113 lit0 = TREE_OPERAND (con0, 1);
2114 con0 = TREE_OPERAND (con0, 0);
2117 lit0 = integer_zero_node;
2119 if (TREE_CODE (con1) == PLUS_EXPR)
2121 lit1 = TREE_OPERAND (con1, 1);
2122 con1 = TREE_OPERAND (con1, 0);
2125 lit1 = integer_zero_node;
2127 if (operand_equal_p (con0, con1, 0))
2134 /* First do the subtraction as integers;
2135 then drop through to build the divide operator.
2136 Do not do default conversions on the minus operator
2137 in case restype is a short type. */
2139 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2140 convert (restype, op1), 0);
2141 /* This generates an error if op1 is pointer to incomplete type. */
2142 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2143 error ("arithmetic on pointer to an incomplete type");
2145 /* This generates an error if op0 is pointer to incomplete type. */
2146 op1 = c_size_in_bytes (target_type);
2148 /* Divide by the size, in easiest possible way. */
2150 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2152 folded = fold (result);
2153 if (folded == result)
2154 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2158 /* Construct and perhaps optimize a tree representation
2159 for a unary operation. CODE, a tree_code, specifies the operation
2160 and XARG is the operand.
2161 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2162 the default promotions (such as from short to int).
2163 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2164 allows non-lvalues; this is only used to handle conversion of non-lvalue
2165 arrays to pointers in C99. */
2168 build_unary_op (enum tree_code code, tree xarg, int flag)
2170 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2173 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2175 int noconvert = flag;
2177 if (typecode == ERROR_MARK)
2178 return error_mark_node;
2179 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2180 typecode = INTEGER_TYPE;
2185 /* This is used for unary plus, because a CONVERT_EXPR
2186 is enough to prevent anybody from looking inside for
2187 associativity, but won't generate any code. */
2188 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2189 || typecode == COMPLEX_TYPE))
2191 error ("wrong type argument to unary plus");
2192 return error_mark_node;
2194 else if (!noconvert)
2195 arg = default_conversion (arg);
2196 arg = non_lvalue (arg);
2200 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2201 || typecode == COMPLEX_TYPE
2202 || typecode == VECTOR_TYPE))
2204 error ("wrong type argument to unary minus");
2205 return error_mark_node;
2207 else if (!noconvert)
2208 arg = default_conversion (arg);
2212 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2215 arg = default_conversion (arg);
2217 else if (typecode == COMPLEX_TYPE)
2221 pedwarn ("ISO C does not support `~' for complex conjugation");
2223 arg = default_conversion (arg);
2227 error ("wrong type argument to bit-complement");
2228 return error_mark_node;
2233 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2235 error ("wrong type argument to abs");
2236 return error_mark_node;
2238 else if (!noconvert)
2239 arg = default_conversion (arg);
2243 /* Conjugating a real value is a no-op, but allow it anyway. */
2244 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2245 || typecode == COMPLEX_TYPE))
2247 error ("wrong type argument to conjugation");
2248 return error_mark_node;
2250 else if (!noconvert)
2251 arg = default_conversion (arg);
2254 case TRUTH_NOT_EXPR:
2255 if (typecode != INTEGER_TYPE
2256 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2257 && typecode != COMPLEX_TYPE
2258 /* These will convert to a pointer. */
2259 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2261 error ("wrong type argument to unary exclamation mark");
2262 return error_mark_node;
2264 arg = c_common_truthvalue_conversion (arg);
2265 return invert_truthvalue (arg);
2271 if (TREE_CODE (arg) == COMPLEX_CST)
2272 return TREE_REALPART (arg);
2273 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2274 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2279 if (TREE_CODE (arg) == COMPLEX_CST)
2280 return TREE_IMAGPART (arg);
2281 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2282 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2284 return convert (TREE_TYPE (arg), integer_zero_node);
2286 case PREINCREMENT_EXPR:
2287 case POSTINCREMENT_EXPR:
2288 case PREDECREMENT_EXPR:
2289 case POSTDECREMENT_EXPR:
2290 /* Handle complex lvalues (when permitted)
2291 by reduction to simpler cases. */
2293 val = unary_complex_lvalue (code, arg, 0);
2297 /* Increment or decrement the real part of the value,
2298 and don't change the imaginary part. */
2299 if (typecode == COMPLEX_TYPE)
2304 pedwarn ("ISO C does not support `++' and `--' on complex types");
2306 arg = stabilize_reference (arg);
2307 real = build_unary_op (REALPART_EXPR, arg, 1);
2308 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2309 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2310 build_unary_op (code, real, 1), imag);
2313 /* Report invalid types. */
2315 if (typecode != POINTER_TYPE
2316 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2318 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2319 error ("wrong type argument to increment");
2321 error ("wrong type argument to decrement");
2323 return error_mark_node;
2328 tree result_type = TREE_TYPE (arg);
2330 arg = get_unwidened (arg, 0);
2331 argtype = TREE_TYPE (arg);
2333 /* Compute the increment. */
2335 if (typecode == POINTER_TYPE)
2337 /* If pointer target is an undefined struct,
2338 we just cannot know how to do the arithmetic. */
2339 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2341 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2342 error ("increment of pointer to unknown structure");
2344 error ("decrement of pointer to unknown structure");
2346 else if ((pedantic || warn_pointer_arith)
2347 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2348 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2350 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2351 pedwarn ("wrong type argument to increment");
2353 pedwarn ("wrong type argument to decrement");
2356 inc = c_size_in_bytes (TREE_TYPE (result_type));
2359 inc = integer_one_node;
2361 inc = convert (argtype, inc);
2363 /* Handle incrementing a cast-expression. */
2366 switch (TREE_CODE (arg))
2371 case FIX_TRUNC_EXPR:
2372 case FIX_FLOOR_EXPR:
2373 case FIX_ROUND_EXPR:
2375 pedantic_lvalue_warning (CONVERT_EXPR);
2376 /* If the real type has the same machine representation
2377 as the type it is cast to, we can make better output
2378 by adding directly to the inside of the cast. */
2379 if ((TREE_CODE (TREE_TYPE (arg))
2380 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2381 && (TYPE_MODE (TREE_TYPE (arg))
2382 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2383 arg = TREE_OPERAND (arg, 0);
2386 tree incremented, modify, value;
2387 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2388 value = boolean_increment (code, arg);
2391 arg = stabilize_reference (arg);
2392 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2395 value = save_expr (arg);
2396 incremented = build (((code == PREINCREMENT_EXPR
2397 || code == POSTINCREMENT_EXPR)
2398 ? PLUS_EXPR : MINUS_EXPR),
2399 argtype, value, inc);
2400 TREE_SIDE_EFFECTS (incremented) = 1;
2401 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2402 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2404 TREE_USED (value) = 1;
2414 /* Complain about anything else that is not a true lvalue. */
2415 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2416 || code == POSTINCREMENT_EXPR)
2417 ? "invalid lvalue in increment"
2418 : "invalid lvalue in decrement")))
2419 return error_mark_node;
2421 /* Report a read-only lvalue. */
2422 if (TREE_READONLY (arg))
2423 readonly_warning (arg,
2424 ((code == PREINCREMENT_EXPR
2425 || code == POSTINCREMENT_EXPR)
2426 ? "increment" : "decrement"));
2428 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2429 val = boolean_increment (code, arg);
2431 val = build (code, TREE_TYPE (arg), arg, inc);
2432 TREE_SIDE_EFFECTS (val) = 1;
2433 val = convert (result_type, val);
2434 if (TREE_CODE (val) != code)
2435 TREE_NO_UNUSED_WARNING (val) = 1;
2440 /* Note that this operation never does default_conversion. */
2442 /* Let &* cancel out to simplify resulting code. */
2443 if (TREE_CODE (arg) == INDIRECT_REF)
2445 /* Don't let this be an lvalue. */
2446 if (lvalue_p (TREE_OPERAND (arg, 0)))
2447 return non_lvalue (TREE_OPERAND (arg, 0));
2448 return TREE_OPERAND (arg, 0);
2451 /* For &x[y], return x+y */
2452 if (TREE_CODE (arg) == ARRAY_REF)
2454 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2455 return error_mark_node;
2456 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2457 TREE_OPERAND (arg, 1), 1);
2460 /* Handle complex lvalues (when permitted)
2461 by reduction to simpler cases. */
2462 val = unary_complex_lvalue (code, arg, flag);
2466 /* Anything not already handled and not a true memory reference
2467 or a non-lvalue array is an error. */
2468 else if (typecode != FUNCTION_TYPE && !flag
2469 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2470 return error_mark_node;
2472 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2473 argtype = TREE_TYPE (arg);
2475 /* If the lvalue is const or volatile, merge that into the type
2476 to which the address will point. Note that you can't get a
2477 restricted pointer by taking the address of something, so we
2478 only have to deal with `const' and `volatile' here. */
2479 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2480 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2481 argtype = c_build_type_variant (argtype,
2482 TREE_READONLY (arg),
2483 TREE_THIS_VOLATILE (arg));
2485 argtype = build_pointer_type (argtype);
2487 if (!c_mark_addressable (arg))
2488 return error_mark_node;
2493 if (TREE_CODE (arg) == COMPONENT_REF)
2495 tree field = TREE_OPERAND (arg, 1);
2497 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2499 if (DECL_C_BIT_FIELD (field))
2501 error ("attempt to take address of bit-field structure member `%s'",
2502 IDENTIFIER_POINTER (DECL_NAME (field)));
2503 return error_mark_node;
2506 addr = fold (build (PLUS_EXPR, argtype,
2507 convert (argtype, addr),
2508 convert (argtype, byte_position (field))));
2511 addr = build1 (code, argtype, arg);
2513 /* Address of a static or external variable or
2514 file-scope function counts as a constant. */
2516 && ! (TREE_CODE (arg) == FUNCTION_DECL
2517 && !DECL_FILE_SCOPE_P (arg)))
2518 TREE_CONSTANT (addr) = 1;
2527 argtype = TREE_TYPE (arg);
2528 return fold (build1 (code, argtype, arg));
2531 /* Return nonzero if REF is an lvalue valid for this language.
2532 Lvalues can be assigned, unless their type has TYPE_READONLY.
2533 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2538 enum tree_code code = TREE_CODE (ref);
2545 return lvalue_p (TREE_OPERAND (ref, 0));
2547 case COMPOUND_LITERAL_EXPR:
2557 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2558 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2562 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2569 /* Return nonzero if REF is an lvalue valid for this language;
2570 otherwise, print an error message and return zero. */
2573 lvalue_or_else (tree ref, const char *msgid)
2575 int win = lvalue_p (ref);
2578 error ("%s", msgid);
2583 /* Apply unary lvalue-demanding operator CODE to the expression ARG
2584 for certain kinds of expressions which are not really lvalues
2585 but which we can accept as lvalues. If FLAG is nonzero, then
2586 non-lvalues are OK since we may be converting a non-lvalue array to
2589 If ARG is not a kind of expression we can handle, return zero. */
2592 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
2594 /* Handle (a, b) used as an "lvalue". */
2595 if (TREE_CODE (arg) == COMPOUND_EXPR)
2597 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
2599 /* If this returns a function type, it isn't really being used as
2600 an lvalue, so don't issue a warning about it. */
2601 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2602 pedantic_lvalue_warning (COMPOUND_EXPR);
2604 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
2605 TREE_OPERAND (arg, 0), real_result);
2608 /* Handle (a ? b : c) used as an "lvalue". */
2609 if (TREE_CODE (arg) == COND_EXPR)
2612 pedantic_lvalue_warning (COND_EXPR);
2613 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2614 pedantic_lvalue_warning (COMPOUND_EXPR);
2616 return (build_conditional_expr
2617 (TREE_OPERAND (arg, 0),
2618 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
2619 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
2625 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
2626 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
2629 pedantic_lvalue_warning (enum tree_code code)
2634 pedwarn ("use of conditional expressions as lvalues is deprecated");
2637 pedwarn ("use of compound expressions as lvalues is deprecated");
2640 pedwarn ("use of cast expressions as lvalues is deprecated");
2645 /* Warn about storing in something that is `const'. */
2648 readonly_warning (tree arg, const char *msgid)
2650 if (TREE_CODE (arg) == COMPONENT_REF)
2652 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2653 readonly_warning (TREE_OPERAND (arg, 0), msgid);
2655 pedwarn ("%s of read-only member `%s'", _(msgid),
2656 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2658 else if (TREE_CODE (arg) == VAR_DECL)
2659 pedwarn ("%s of read-only variable `%s'", _(msgid),
2660 IDENTIFIER_POINTER (DECL_NAME (arg)));
2662 pedwarn ("%s of read-only location", _(msgid));
2665 /* Mark EXP saying that we need to be able to take the
2666 address of it; it should not be allocated in a register.
2667 Returns true if successful. */
2670 c_mark_addressable (tree exp)
2675 switch (TREE_CODE (x))
2678 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2680 error ("cannot take address of bit-field `%s'",
2681 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2685 /* ... fall through ... */
2691 x = TREE_OPERAND (x, 0);
2694 case COMPOUND_LITERAL_EXPR:
2696 TREE_ADDRESSABLE (x) = 1;
2703 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2704 && DECL_NONLOCAL (x))
2706 if (TREE_PUBLIC (x))
2708 error ("global register variable `%s' used in nested function",
2709 IDENTIFIER_POINTER (DECL_NAME (x)));
2712 pedwarn ("register variable `%s' used in nested function",
2713 IDENTIFIER_POINTER (DECL_NAME (x)));
2715 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2717 if (TREE_PUBLIC (x))
2719 error ("address of global register variable `%s' requested",
2720 IDENTIFIER_POINTER (DECL_NAME (x)));
2724 /* If we are making this addressable due to its having
2725 volatile components, give a different error message. Also
2726 handle the case of an unnamed parameter by not trying
2727 to give the name. */
2729 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2731 error ("cannot put object with volatile field into register");
2735 pedwarn ("address of register variable `%s' requested",
2736 IDENTIFIER_POINTER (DECL_NAME (x)));
2738 put_var_into_stack (x, /*rescan=*/true);
2742 TREE_ADDRESSABLE (x) = 1;
2749 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2752 build_conditional_expr (tree ifexp, tree op1, tree op2)
2756 enum tree_code code1;
2757 enum tree_code code2;
2758 tree result_type = NULL;
2759 tree orig_op1 = op1, orig_op2 = op2;
2761 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
2763 /* Promote both alternatives. */
2765 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2766 op1 = default_conversion (op1);
2767 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2768 op2 = default_conversion (op2);
2770 if (TREE_CODE (ifexp) == ERROR_MARK
2771 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2772 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2773 return error_mark_node;
2775 type1 = TREE_TYPE (op1);
2776 code1 = TREE_CODE (type1);
2777 type2 = TREE_TYPE (op2);
2778 code2 = TREE_CODE (type2);
2780 /* Quickly detect the usual case where op1 and op2 have the same type
2782 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2785 result_type = type1;
2787 result_type = TYPE_MAIN_VARIANT (type1);
2789 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2790 || code1 == COMPLEX_TYPE)
2791 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2792 || code2 == COMPLEX_TYPE))
2794 result_type = common_type (type1, type2);
2796 /* If -Wsign-compare, warn here if type1 and type2 have
2797 different signedness. We'll promote the signed to unsigned
2798 and later code won't know it used to be different.
2799 Do this check on the original types, so that explicit casts
2800 will be considered, but default promotions won't. */
2801 if (warn_sign_compare && !skip_evaluation)
2803 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2804 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2806 if (unsigned_op1 ^ unsigned_op2)
2808 /* Do not warn if the result type is signed, since the
2809 signed type will only be chosen if it can represent
2810 all the values of the unsigned type. */
2811 if (! TREE_UNSIGNED (result_type))
2813 /* Do not warn if the signed quantity is an unsuffixed
2814 integer literal (or some static constant expression
2815 involving such literals) and it is non-negative. */
2816 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2817 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2820 warning ("signed and unsigned type in conditional expression");
2824 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2826 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2827 pedwarn ("ISO C forbids conditional expr with only one void side");
2828 result_type = void_type_node;
2830 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2832 if (comp_target_types (type1, type2, 1))
2833 result_type = common_type (type1, type2);
2834 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2835 && TREE_CODE (orig_op1) != NOP_EXPR)
2836 result_type = qualify_type (type2, type1);
2837 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2838 && TREE_CODE (orig_op2) != NOP_EXPR)
2839 result_type = qualify_type (type1, type2);
2840 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2842 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2843 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2844 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2845 TREE_TYPE (type2)));
2847 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2849 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2850 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2851 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2852 TREE_TYPE (type1)));
2856 pedwarn ("pointer type mismatch in conditional expression");
2857 result_type = build_pointer_type (void_type_node);
2860 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2862 if (! integer_zerop (op2))
2863 pedwarn ("pointer/integer type mismatch in conditional expression");
2866 op2 = null_pointer_node;
2868 result_type = type1;
2870 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2872 if (!integer_zerop (op1))
2873 pedwarn ("pointer/integer type mismatch in conditional expression");
2876 op1 = null_pointer_node;
2878 result_type = type2;
2883 if (flag_cond_mismatch)
2884 result_type = void_type_node;
2887 error ("type mismatch in conditional expression");
2888 return error_mark_node;
2892 /* Merge const and volatile flags of the incoming types. */
2894 = build_type_variant (result_type,
2895 TREE_READONLY (op1) || TREE_READONLY (op2),
2896 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2898 if (result_type != TREE_TYPE (op1))
2899 op1 = convert_and_check (result_type, op1);
2900 if (result_type != TREE_TYPE (op2))
2901 op2 = convert_and_check (result_type, op2);
2903 if (TREE_CODE (ifexp) == INTEGER_CST)
2904 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2906 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2909 /* Given a list of expressions, return a compound expression
2910 that performs them all and returns the value of the last of them. */
2913 build_compound_expr (tree list)
2915 return internal_build_compound_expr (list, TRUE);
2919 internal_build_compound_expr (tree list, int first_p)
2923 if (TREE_CHAIN (list) == 0)
2925 /* Convert arrays and functions to pointers when there
2926 really is a comma operator. */
2929 = default_function_array_conversion (TREE_VALUE (list));
2931 /* Don't let (0, 0) be null pointer constant. */
2932 if (!first_p && integer_zerop (TREE_VALUE (list)))
2933 return non_lvalue (TREE_VALUE (list));
2934 return TREE_VALUE (list);
2937 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2939 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2941 /* The left-hand operand of a comma expression is like an expression
2942 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2943 any side-effects, unless it was explicitly cast to (void). */
2944 if (warn_unused_value
2945 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2946 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2947 warning ("left-hand operand of comma expression has no effect");
2950 /* With -Wunused, we should also warn if the left-hand operand does have
2951 side-effects, but computes a value which is not used. For example, in
2952 `foo() + bar(), baz()' the result of the `+' operator is not used,
2953 so we should issue a warning. */
2954 else if (warn_unused_value)
2955 warn_if_unused_value (TREE_VALUE (list));
2957 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2960 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2963 build_c_cast (tree type, tree expr)
2967 if (type == error_mark_node || expr == error_mark_node)
2968 return error_mark_node;
2970 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2971 only in <protocol> qualifications. But when constructing cast expressions,
2972 the protocols do matter and must be kept around. */
2973 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2974 type = TYPE_MAIN_VARIANT (type);
2976 if (TREE_CODE (type) == ARRAY_TYPE)
2978 error ("cast specifies array type");
2979 return error_mark_node;
2982 if (TREE_CODE (type) == FUNCTION_TYPE)
2984 error ("cast specifies function type");
2985 return error_mark_node;
2988 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2992 if (TREE_CODE (type) == RECORD_TYPE
2993 || TREE_CODE (type) == UNION_TYPE)
2994 pedwarn ("ISO C forbids casting nonscalar to the same type");
2997 else if (TREE_CODE (type) == UNION_TYPE)
3000 value = default_function_array_conversion (value);
3002 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3003 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3004 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
3012 pedwarn ("ISO C forbids casts to union type");
3013 t = digest_init (type,
3014 build_constructor (type,
3015 build_tree_list (field, value)),
3017 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3020 error ("cast to union type from type not present in union");
3021 return error_mark_node;
3027 /* If casting to void, avoid the error that would come
3028 from default_conversion in the case of a non-lvalue array. */
3029 if (type == void_type_node)
3030 return build1 (CONVERT_EXPR, type, value);
3032 /* Convert functions and arrays to pointers,
3033 but don't convert any other types. */
3034 value = default_function_array_conversion (value);
3035 otype = TREE_TYPE (value);
3037 /* Optionally warn about potentially worrisome casts. */
3040 && TREE_CODE (type) == POINTER_TYPE
3041 && TREE_CODE (otype) == POINTER_TYPE)
3043 tree in_type = type;
3044 tree in_otype = otype;
3048 /* Check that the qualifiers on IN_TYPE are a superset of
3049 the qualifiers of IN_OTYPE. The outermost level of
3050 POINTER_TYPE nodes is uninteresting and we stop as soon
3051 as we hit a non-POINTER_TYPE node on either type. */
3054 in_otype = TREE_TYPE (in_otype);
3055 in_type = TREE_TYPE (in_type);
3057 /* GNU C allows cv-qualified function types. 'const'
3058 means the function is very pure, 'volatile' means it
3059 can't return. We need to warn when such qualifiers
3060 are added, not when they're taken away. */
3061 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3062 && TREE_CODE (in_type) == FUNCTION_TYPE)
3063 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3065 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3067 while (TREE_CODE (in_type) == POINTER_TYPE
3068 && TREE_CODE (in_otype) == POINTER_TYPE);
3071 warning ("cast adds new qualifiers to function type");
3074 /* There are qualifiers present in IN_OTYPE that are not
3075 present in IN_TYPE. */
3076 warning ("cast discards qualifiers from pointer target type");
3079 /* Warn about possible alignment problems. */
3080 if (STRICT_ALIGNMENT && warn_cast_align
3081 && TREE_CODE (type) == POINTER_TYPE
3082 && TREE_CODE (otype) == POINTER_TYPE
3083 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3084 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3085 /* Don't warn about opaque types, where the actual alignment
3086 restriction is unknown. */
3087 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3088 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3089 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3090 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3091 warning ("cast increases required alignment of target type");
3093 if (TREE_CODE (type) == INTEGER_TYPE
3094 && TREE_CODE (otype) == POINTER_TYPE
3095 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3096 && !TREE_CONSTANT (value))
3097 warning ("cast from pointer to integer of different size");
3099 if (warn_bad_function_cast
3100 && TREE_CODE (value) == CALL_EXPR
3101 && TREE_CODE (type) != TREE_CODE (otype))
3102 warning ("cast does not match function type");
3104 if (TREE_CODE (type) == POINTER_TYPE
3105 && TREE_CODE (otype) == INTEGER_TYPE
3106 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3107 /* Don't warn about converting any constant. */
3108 && !TREE_CONSTANT (value))
3109 warning ("cast to pointer from integer of different size");
3111 if (TREE_CODE (type) == POINTER_TYPE
3112 && TREE_CODE (otype) == POINTER_TYPE
3113 && TREE_CODE (expr) == ADDR_EXPR
3114 && DECL_P (TREE_OPERAND (expr, 0))
3115 && flag_strict_aliasing && warn_strict_aliasing
3116 && !VOID_TYPE_P (TREE_TYPE (type)))
3118 /* Casting the address of a decl to non void pointer. Warn
3119 if the cast breaks type based aliasing. */
3120 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3121 warning ("type-punning to incomplete type might break strict-aliasing rules");
3122 else if (!alias_sets_conflict_p
3123 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3124 get_alias_set (TREE_TYPE (type))))
3125 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3128 /* If pedantic, warn for conversions between function and object
3129 pointer types, except for converting a null pointer constant
3130 to function pointer type. */
3132 && TREE_CODE (type) == POINTER_TYPE
3133 && TREE_CODE (otype) == POINTER_TYPE
3134 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3135 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3136 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3139 && TREE_CODE (type) == POINTER_TYPE
3140 && TREE_CODE (otype) == POINTER_TYPE
3141 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3142 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3143 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3144 && TREE_CODE (expr) != NOP_EXPR))
3145 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3148 /* Replace a nonvolatile const static variable with its value. */
3149 if (optimize && TREE_CODE (value) == VAR_DECL)
3150 value = decl_constant_value (value);
3151 value = convert (type, value);
3153 /* Ignore any integer overflow caused by the cast. */
3154 if (TREE_CODE (value) == INTEGER_CST)
3156 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3157 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3161 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3162 if (pedantic && TREE_CODE (value) == INTEGER_CST
3163 && TREE_CODE (expr) == INTEGER_CST
3164 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3165 value = non_lvalue (value);
3167 /* If pedantic, don't let a cast be an lvalue. */
3168 if (value == expr && pedantic)
3169 value = non_lvalue (value);
3174 /* Interpret a cast of expression EXPR to type TYPE. */
3176 c_cast_expr (tree type, tree expr)
3178 int saved_wsp = warn_strict_prototypes;
3180 /* This avoids warnings about unprototyped casts on
3181 integers. E.g. "#define SIG_DFL (void(*)())0". */
3182 if (TREE_CODE (expr) == INTEGER_CST)
3183 warn_strict_prototypes = 0;
3184 type = groktypename (type);
3185 warn_strict_prototypes = saved_wsp;
3187 return build_c_cast (type, expr);
3191 /* Build an assignment expression of lvalue LHS from value RHS.
3192 MODIFYCODE is the code for a binary operator that we use
3193 to combine the old value of LHS with RHS to get the new value.
3194 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3197 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3201 tree lhstype = TREE_TYPE (lhs);
3202 tree olhstype = lhstype;
3204 /* Types that aren't fully specified cannot be used in assignments. */
3205 lhs = require_complete_type (lhs);
3207 /* Avoid duplicate error messages from operands that had errors. */
3208 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3209 return error_mark_node;
3211 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3212 /* Do not use STRIP_NOPS here. We do not want an enumerator
3213 whose value is 0 to count as a null pointer constant. */
3214 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3215 rhs = TREE_OPERAND (rhs, 0);
3219 /* Handle control structure constructs used as "lvalues". */
3221 switch (TREE_CODE (lhs))
3223 /* Handle (a, b) used as an "lvalue". */
3225 pedantic_lvalue_warning (COMPOUND_EXPR);
3226 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3227 if (TREE_CODE (newrhs) == ERROR_MARK)
3228 return error_mark_node;
3229 return build (COMPOUND_EXPR, lhstype,
3230 TREE_OPERAND (lhs, 0), newrhs);
3232 /* Handle (a ? b : c) used as an "lvalue". */
3234 pedantic_lvalue_warning (COND_EXPR);
3235 rhs = save_expr (rhs);
3237 /* Produce (a ? (b = rhs) : (c = rhs))
3238 except that the RHS goes through a save-expr
3239 so the code to compute it is only emitted once. */
3241 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3242 build_modify_expr (TREE_OPERAND (lhs, 1),
3244 build_modify_expr (TREE_OPERAND (lhs, 2),
3246 if (TREE_CODE (cond) == ERROR_MARK)
3248 /* Make sure the code to compute the rhs comes out
3249 before the split. */
3250 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3251 /* But cast it to void to avoid an "unused" error. */
3252 convert (void_type_node, rhs), cond);
3258 /* If a binary op has been requested, combine the old LHS value with the RHS
3259 producing the value we should actually store into the LHS. */
3261 if (modifycode != NOP_EXPR)
3263 lhs = stabilize_reference (lhs);
3264 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3267 /* Handle a cast used as an "lvalue".
3268 We have already performed any binary operator using the value as cast.
3269 Now convert the result to the cast type of the lhs,
3270 and then true type of the lhs and store it there;
3271 then convert result back to the cast type to be the value
3272 of the assignment. */
3274 switch (TREE_CODE (lhs))
3279 case FIX_TRUNC_EXPR:
3280 case FIX_FLOOR_EXPR:
3281 case FIX_ROUND_EXPR:
3283 newrhs = default_function_array_conversion (newrhs);
3285 tree inner_lhs = TREE_OPERAND (lhs, 0);
3287 result = build_modify_expr (inner_lhs, NOP_EXPR,
3288 convert (TREE_TYPE (inner_lhs),
3289 convert (lhstype, newrhs)));
3290 if (TREE_CODE (result) == ERROR_MARK)
3292 pedantic_lvalue_warning (CONVERT_EXPR);
3293 return convert (TREE_TYPE (lhs), result);
3300 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3301 Reject anything strange now. */
3303 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3304 return error_mark_node;
3306 /* Warn about storing in something that is `const'. */
3308 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3309 || ((TREE_CODE (lhstype) == RECORD_TYPE
3310 || TREE_CODE (lhstype) == UNION_TYPE)
3311 && C_TYPE_FIELDS_READONLY (lhstype)))
3312 readonly_warning (lhs, "assignment");
3314 /* If storing into a structure or union member,
3315 it has probably been given type `int'.
3316 Compute the type that would go with
3317 the actual amount of storage the member occupies. */
3319 if (TREE_CODE (lhs) == COMPONENT_REF
3320 && (TREE_CODE (lhstype) == INTEGER_TYPE
3321 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3322 || TREE_CODE (lhstype) == REAL_TYPE
3323 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3324 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3326 /* If storing in a field that is in actuality a short or narrower than one,
3327 we must store in the field in its actual type. */
3329 if (lhstype != TREE_TYPE (lhs))
3331 lhs = copy_node (lhs);
3332 TREE_TYPE (lhs) = lhstype;
3335 /* Convert new value to destination type. */
3337 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3338 NULL_TREE, NULL_TREE, 0);
3339 if (TREE_CODE (newrhs) == ERROR_MARK)
3340 return error_mark_node;
3344 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3345 TREE_SIDE_EFFECTS (result) = 1;
3347 /* If we got the LHS in a different type for storing in,
3348 convert the result back to the nominal type of LHS
3349 so that the value we return always has the same type
3350 as the LHS argument. */
3352 if (olhstype == TREE_TYPE (result))
3354 return convert_for_assignment (olhstype, result, _("assignment"),
3355 NULL_TREE, NULL_TREE, 0);
3358 /* Convert value RHS to type TYPE as preparation for an assignment
3359 to an lvalue of type TYPE.
3360 The real work of conversion is done by `convert'.
3361 The purpose of this function is to generate error messages
3362 for assignments that are not allowed in C.
3363 ERRTYPE is a string to use in error messages:
3364 "assignment", "return", etc. If it is null, this is parameter passing
3365 for a function call (and different error messages are output).
3367 FUNNAME is the name of the function being called,
3368 as an IDENTIFIER_NODE, or null.
3369 PARMNUM is the number of the argument, for printing in error messages. */
3372 convert_for_assignment (tree type, tree rhs, const char *errtype,
3373 tree fundecl, tree funname, int parmnum)
3375 enum tree_code codel = TREE_CODE (type);
3377 enum tree_code coder;
3379 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3380 /* Do not use STRIP_NOPS here. We do not want an enumerator
3381 whose value is 0 to count as a null pointer constant. */
3382 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3383 rhs = TREE_OPERAND (rhs, 0);
3385 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3386 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3387 rhs = default_conversion (rhs);
3388 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3389 rhs = decl_constant_value_for_broken_optimization (rhs);
3391 rhstype = TREE_TYPE (rhs);
3392 coder = TREE_CODE (rhstype);
3394 if (coder == ERROR_MARK)
3395 return error_mark_node;
3397 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3399 overflow_warning (rhs);
3400 /* Check for Objective-C protocols. This will automatically
3401 issue a warning if there are protocol violations. No need to
3402 use the return value. */
3403 if (c_dialect_objc ())
3404 objc_comptypes (type, rhstype, 0);
3408 if (coder == VOID_TYPE)
3410 error ("void value not ignored as it ought to be");
3411 return error_mark_node;
3413 /* A type converts to a reference to it.
3414 This code doesn't fully support references, it's just for the
3415 special case of va_start and va_copy. */
3416 if (codel == REFERENCE_TYPE
3417 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3419 if (!lvalue_p (rhs))
3421 error ("cannot pass rvalue to reference parameter");
3422 return error_mark_node;
3424 if (!c_mark_addressable (rhs))
3425 return error_mark_node;
3426 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3428 /* We already know that these two types are compatible, but they
3429 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3430 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3431 likely to be va_list, a typedef to __builtin_va_list, which
3432 is different enough that it will cause problems later. */
3433 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3434 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3436 rhs = build1 (NOP_EXPR, type, rhs);
3439 /* Some types can interconvert without explicit casts. */
3440 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3441 && ((*targetm.vector_opaque_p) (type)
3442 || (*targetm.vector_opaque_p) (rhstype)))
3443 return convert (type, rhs);
3444 /* Arithmetic types all interconvert, and enum is treated like int. */
3445 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3446 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3447 || codel == BOOLEAN_TYPE)
3448 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3449 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3450 || coder == BOOLEAN_TYPE))
3451 return convert_and_check (type, rhs);
3453 /* Conversion to a transparent union from its member types.
3454 This applies only to function arguments. */
3455 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3458 tree marginal_memb_type = 0;
3460 for (memb_types = TYPE_FIELDS (type); memb_types;
3461 memb_types = TREE_CHAIN (memb_types))
3463 tree memb_type = TREE_TYPE (memb_types);
3465 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3466 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3469 if (TREE_CODE (memb_type) != POINTER_TYPE)
3472 if (coder == POINTER_TYPE)
3474 tree ttl = TREE_TYPE (memb_type);
3475 tree ttr = TREE_TYPE (rhstype);
3477 /* Any non-function converts to a [const][volatile] void *
3478 and vice versa; otherwise, targets must be the same.
3479 Meanwhile, the lhs target must have all the qualifiers of
3481 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3482 || comp_target_types (memb_type, rhstype, 0))
3484 /* If this type won't generate any warnings, use it. */
3485 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3486 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3487 && TREE_CODE (ttl) == FUNCTION_TYPE)
3488 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3489 == TYPE_QUALS (ttr))
3490 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3491 == TYPE_QUALS (ttl))))
3494 /* Keep looking for a better type, but remember this one. */
3495 if (! marginal_memb_type)
3496 marginal_memb_type = memb_type;
3500 /* Can convert integer zero to any pointer type. */
3501 if (integer_zerop (rhs)
3502 || (TREE_CODE (rhs) == NOP_EXPR
3503 && integer_zerop (TREE_OPERAND (rhs, 0))))
3505 rhs = null_pointer_node;
3510 if (memb_types || marginal_memb_type)
3514 /* We have only a marginally acceptable member type;
3515 it needs a warning. */
3516 tree ttl = TREE_TYPE (marginal_memb_type);
3517 tree ttr = TREE_TYPE (rhstype);
3519 /* Const and volatile mean something different for function
3520 types, so the usual warnings are not appropriate. */
3521 if (TREE_CODE (ttr) == FUNCTION_TYPE
3522 && TREE_CODE (ttl) == FUNCTION_TYPE)
3524 /* Because const and volatile on functions are
3525 restrictions that say the function will not do
3526 certain things, it is okay to use a const or volatile
3527 function where an ordinary one is wanted, but not
3529 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3530 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3531 errtype, funname, parmnum);
3533 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3534 warn_for_assignment ("%s discards qualifiers from pointer target type",
3539 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3540 pedwarn ("ISO C prohibits argument conversion to union type");
3542 return build1 (NOP_EXPR, type, rhs);
3546 /* Conversions among pointers */
3547 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3548 && (coder == codel))
3550 tree ttl = TREE_TYPE (type);
3551 tree ttr = TREE_TYPE (rhstype);
3552 bool is_opaque_pointer;
3553 int target_cmp = 0; /* Cache comp_target_types () result. */
3555 /* Opaque pointers are treated like void pointers. */
3556 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
3557 || (*targetm.vector_opaque_p) (rhstype))
3558 && TREE_CODE (ttl) == VECTOR_TYPE
3559 && TREE_CODE (ttr) == VECTOR_TYPE;
3561 /* Any non-function converts to a [const][volatile] void *
3562 and vice versa; otherwise, targets must be the same.
3563 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3564 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3565 || (target_cmp = comp_target_types (type, rhstype, 0))
3566 || is_opaque_pointer
3567 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3568 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3571 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3574 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3575 which are not ANSI null ptr constants. */
3576 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3577 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3578 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3579 errtype, funname, parmnum);
3580 /* Const and volatile mean something different for function types,
3581 so the usual warnings are not appropriate. */
3582 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3583 && TREE_CODE (ttl) != FUNCTION_TYPE)
3585 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3586 warn_for_assignment ("%s discards qualifiers from pointer target type",
3587 errtype, funname, parmnum);
3588 /* If this is not a case of ignoring a mismatch in signedness,
3590 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3593 /* If there is a mismatch, do warn. */
3595 warn_for_assignment ("pointer targets in %s differ in signedness",
3596 errtype, funname, parmnum);
3598 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3599 && TREE_CODE (ttr) == FUNCTION_TYPE)
3601 /* Because const and volatile on functions are restrictions
3602 that say the function will not do certain things,
3603 it is okay to use a const or volatile function
3604 where an ordinary one is wanted, but not vice-versa. */
3605 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3606 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3607 errtype, funname, parmnum);
3611 warn_for_assignment ("%s from incompatible pointer type",
3612 errtype, funname, parmnum);
3613 return convert (type, rhs);
3615 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3617 error ("invalid use of non-lvalue array");
3618 return error_mark_node;
3620 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3622 /* An explicit constant 0 can convert to a pointer,
3623 or one that results from arithmetic, even including
3624 a cast to integer type. */
3625 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3627 ! (TREE_CODE (rhs) == NOP_EXPR
3628 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3629 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3630 && integer_zerop (TREE_OPERAND (rhs, 0))))
3631 warn_for_assignment ("%s makes pointer from integer without a cast",
3632 errtype, funname, parmnum);
3634 return convert (type, rhs);
3636 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3638 warn_for_assignment ("%s makes integer from pointer without a cast",
3639 errtype, funname, parmnum);
3640 return convert (type, rhs);
3642 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3643 return convert (type, rhs);
3649 tree selector = objc_message_selector ();
3651 if (selector && parmnum > 2)
3652 error ("incompatible type for argument %d of `%s'",
3653 parmnum - 2, IDENTIFIER_POINTER (selector));
3655 error ("incompatible type for argument %d of `%s'",
3656 parmnum, IDENTIFIER_POINTER (funname));
3659 error ("incompatible type for argument %d of indirect function call",
3663 error ("incompatible types in %s", errtype);
3665 return error_mark_node;
3668 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3669 is used for error and waring reporting and indicates which argument
3670 is being processed. */
3673 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3677 /* If FN was prototyped, the value has been converted already
3678 in convert_arguments. */
3679 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3682 type = TREE_TYPE (parm);
3683 ret = convert_for_assignment (type, value,
3684 (char *) 0 /* arg passing */, fn,
3685 DECL_NAME (fn), argnum);
3686 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3687 && INTEGRAL_TYPE_P (type)
3688 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3689 ret = default_conversion (ret);
3693 /* Print a warning using MSGID.
3694 It gets OPNAME as its one parameter.
3695 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3696 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3697 FUNCTION and ARGNUM are handled specially if we are building an
3698 Objective-C selector. */
3701 warn_for_assignment (const char *msgid, const char *opname, tree function,
3706 tree selector = objc_message_selector ();
3709 if (selector && argnum > 2)
3711 function = selector;
3718 /* Function name is known; supply it. */
3719 const char *const argstring = _("passing arg of `%s'");
3720 new_opname = alloca (IDENTIFIER_LENGTH (function)
3721 + strlen (argstring) + 1 + 1);
3722 sprintf (new_opname, argstring,
3723 IDENTIFIER_POINTER (function));
3727 /* Function name unknown (call through ptr). */
3728 const char *const argnofun = _("passing arg of pointer to function");
3729 new_opname = alloca (strlen (argnofun) + 1 + 1);
3730 sprintf (new_opname, argnofun);
3735 /* Function name is known; supply it. */
3736 const char *const argstring = _("passing arg %d of `%s'");
3737 new_opname = alloca (IDENTIFIER_LENGTH (function)
3738 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3739 sprintf (new_opname, argstring, argnum,
3740 IDENTIFIER_POINTER (function));
3744 /* Function name unknown (call through ptr); just give arg number. */
3745 const char *const argnofun = _("passing arg %d of pointer to function");
3746 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3747 sprintf (new_opname, argnofun, argnum);
3749 opname = new_opname;
3751 pedwarn (msgid, opname);
3754 /* If VALUE is a compound expr all of whose expressions are constant, then
3755 return its value. Otherwise, return error_mark_node.
3757 This is for handling COMPOUND_EXPRs as initializer elements
3758 which is allowed with a warning when -pedantic is specified. */
3761 valid_compound_expr_initializer (tree value, tree endtype)
3763 if (TREE_CODE (value) == COMPOUND_EXPR)
3765 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3767 return error_mark_node;
3768 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3771 else if (! TREE_CONSTANT (value)
3772 && ! initializer_constant_valid_p (value, endtype))
3773 return error_mark_node;
3778 /* Perform appropriate conversions on the initial value of a variable,
3779 store it in the declaration DECL,
3780 and print any error messages that are appropriate.
3781 If the init is invalid, store an ERROR_MARK. */
3784 store_init_value (tree decl, tree init)
3788 /* If variable's type was invalidly declared, just ignore it. */
3790 type = TREE_TYPE (decl);
3791 if (TREE_CODE (type) == ERROR_MARK)
3794 /* Digest the specified initializer into an expression. */
3796 value = digest_init (type, init, TREE_STATIC (decl));
3798 /* Store the expression if valid; else report error. */
3800 if (warn_traditional && !in_system_header
3801 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3802 warning ("traditional C rejects automatic aggregate initialization");
3804 DECL_INITIAL (decl) = value;
3806 /* ANSI wants warnings about out-of-range constant initializers. */
3807 STRIP_TYPE_NOPS (value);
3808 constant_expression_warning (value);
3810 /* Check if we need to set array size from compound literal size. */
3811 if (TREE_CODE (type) == ARRAY_TYPE
3812 && TYPE_DOMAIN (type) == 0
3813 && value != error_mark_node)
3815 tree inside_init = init;
3817 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3818 inside_init = TREE_OPERAND (init, 0);
3819 inside_init = fold (inside_init);
3821 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3823 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3825 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3827 /* For int foo[] = (int [3]){1}; we need to set array size
3828 now since later on array initializer will be just the
3829 brace enclosed list of the compound literal. */
3830 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3832 layout_decl (decl, 0);
3838 /* Methods for storing and printing names for error messages. */
3840 /* Implement a spelling stack that allows components of a name to be pushed
3841 and popped. Each element on the stack is this structure. */
3853 #define SPELLING_STRING 1
3854 #define SPELLING_MEMBER 2
3855 #define SPELLING_BOUNDS 3
3857 static struct spelling *spelling; /* Next stack element (unused). */
3858 static struct spelling *spelling_base; /* Spelling stack base. */
3859 static int spelling_size; /* Size of the spelling stack. */
3861 /* Macros to save and restore the spelling stack around push_... functions.
3862 Alternative to SAVE_SPELLING_STACK. */
3864 #define SPELLING_DEPTH() (spelling - spelling_base)
3865 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3867 /* Push an element on the spelling stack with type KIND and assign VALUE
3870 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3872 int depth = SPELLING_DEPTH (); \
3874 if (depth >= spelling_size) \
3876 spelling_size += 10; \
3877 if (spelling_base == 0) \
3878 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3880 spelling_base = xrealloc (spelling_base, \
3881 spelling_size * sizeof (struct spelling)); \
3882 RESTORE_SPELLING_DEPTH (depth); \
3885 spelling->kind = (KIND); \
3886 spelling->MEMBER = (VALUE); \
3890 /* Push STRING on the stack. Printed literally. */
3893 push_string (const char *string)
3895 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3898 /* Push a member name on the stack. Printed as '.' STRING. */
3901 push_member_name (tree decl)
3903 const char *const string
3904 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3905 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3908 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3911 push_array_bounds (int bounds)
3913 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3916 /* Compute the maximum size in bytes of the printed spelling. */
3919 spelling_length (void)
3924 for (p = spelling_base; p < spelling; p++)
3926 if (p->kind == SPELLING_BOUNDS)
3929 size += strlen (p->u.s) + 1;
3935 /* Print the spelling to BUFFER and return it. */
3938 print_spelling (char *buffer)
3943 for (p = spelling_base; p < spelling; p++)
3944 if (p->kind == SPELLING_BOUNDS)
3946 sprintf (d, "[%d]", p->u.i);
3952 if (p->kind == SPELLING_MEMBER)
3954 for (s = p->u.s; (*d = *s++); d++)
3961 /* Issue an error message for a bad initializer component.
3962 MSGID identifies the message.
3963 The component name is taken from the spelling stack. */
3966 error_init (const char *msgid)