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. */
30 #include "coretypes.h"
34 #include "langhooks.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.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 int require_constant_value;
53 static int require_constant_elements;
55 static tree qualify_type (tree, tree);
56 static int tagged_types_tu_compatible_p (tree, tree);
57 static int comp_target_types (tree, tree, int);
58 static int function_types_compatible_p (tree, tree);
59 static int type_lists_compatible_p (tree, tree);
60 static tree decl_constant_value_for_broken_optimization (tree);
61 static tree default_function_array_conversion (tree);
62 static tree lookup_field (tree, tree);
63 static tree convert_arguments (tree, tree, tree, tree);
64 static tree pointer_diff (tree, tree);
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);
85 static int lvalue_or_else (tree, const char *);
87 /* Do `exp = require_complete_type (exp);' to make sure exp
88 does not have an incomplete type. (That includes void types.) */
91 require_complete_type (tree value)
93 tree type = TREE_TYPE (value);
95 if (value == error_mark_node || type == error_mark_node)
96 return error_mark_node;
98 /* First, detect a valid value with a complete type. */
99 if (COMPLETE_TYPE_P (type))
102 c_incomplete_type_error (value, type);
103 return error_mark_node;
106 /* Print an error message for invalid use of an incomplete type.
107 VALUE is the expression that was used (or 0 if that isn't known)
108 and TYPE is the type that was invalid. */
111 c_incomplete_type_error (tree value, tree type)
113 const char *type_code_string;
115 /* Avoid duplicate error message. */
116 if (TREE_CODE (type) == ERROR_MARK)
119 if (value != 0 && (TREE_CODE (value) == VAR_DECL
120 || TREE_CODE (value) == PARM_DECL))
121 error ("`%s' has an incomplete type",
122 IDENTIFIER_POINTER (DECL_NAME (value)));
126 /* We must print an error message. Be clever about what it says. */
128 switch (TREE_CODE (type))
131 type_code_string = "struct";
135 type_code_string = "union";
139 type_code_string = "enum";
143 error ("invalid use of void expression");
147 if (TYPE_DOMAIN (type))
149 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
151 error ("invalid use of flexible array member");
154 type = TREE_TYPE (type);
157 error ("invalid use of array with unspecified bounds");
164 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
165 error ("invalid use of undefined type `%s %s'",
166 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
168 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
169 error ("invalid use of incomplete typedef `%s'",
170 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
174 /* Given a type, apply default promotions wrt unnamed function
175 arguments and return the new type. */
178 c_type_promotes_to (tree type)
180 if (TYPE_MAIN_VARIANT (type) == float_type_node)
181 return double_type_node;
183 if (c_promoting_integer_type_p (type))
185 /* Preserve unsignedness if not really getting any wider. */
186 if (TYPE_UNSIGNED (type)
187 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
188 return unsigned_type_node;
189 return integer_type_node;
195 /* Return a variant of TYPE which has all the type qualifiers of LIKE
196 as well as those of TYPE. */
199 qualify_type (tree type, tree like)
201 return c_build_qualified_type (type,
202 TYPE_QUALS (type) | TYPE_QUALS (like));
205 /* Return the composite type of two compatible types.
207 We assume that comptypes has already been done and returned
208 nonzero; if that isn't so, this may crash. In particular, we
209 assume that qualifiers match. */
212 composite_type (tree t1, tree t2)
214 enum tree_code code1;
215 enum tree_code code2;
218 /* Save time if the two types are the same. */
220 if (t1 == t2) return t1;
222 /* If one type is nonsense, use the other. */
223 if (t1 == error_mark_node)
225 if (t2 == error_mark_node)
228 code1 = TREE_CODE (t1);
229 code2 = TREE_CODE (t2);
231 /* Merge the attributes. */
232 attributes = targetm.merge_type_attributes (t1, t2);
234 /* If one is an enumerated type and the other is the compatible
235 integer type, the composite type might be either of the two
236 (DR#013 question 3). For consistency, use the enumerated type as
237 the composite type. */
239 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
241 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
250 /* For two pointers, do this recursively on the target type. */
252 tree pointed_to_1 = TREE_TYPE (t1);
253 tree pointed_to_2 = TREE_TYPE (t2);
254 tree target = composite_type (pointed_to_1, pointed_to_2);
255 t1 = build_pointer_type (target);
256 t1 = build_type_attribute_variant (t1, attributes);
257 return qualify_type (t1, t2);
262 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
264 /* We should not have any type quals on arrays at all. */
265 if (TYPE_QUALS (t1) || TYPE_QUALS (t2))
268 /* Save space: see if the result is identical to one of the args. */
269 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
270 return build_type_attribute_variant (t1, attributes);
271 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
272 return build_type_attribute_variant (t2, attributes);
274 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
275 return build_type_attribute_variant (t1, attributes);
276 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
277 return build_type_attribute_variant (t2, attributes);
279 /* Merge the element types, and have a size if either arg has one. */
280 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
281 return build_type_attribute_variant (t1, attributes);
285 /* Function types: prefer the one that specified arg types.
286 If both do, merge the arg types. Also merge the return types. */
288 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
289 tree p1 = TYPE_ARG_TYPES (t1);
290 tree p2 = TYPE_ARG_TYPES (t2);
295 /* Save space: see if the result is identical to one of the args. */
296 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
297 return build_type_attribute_variant (t1, attributes);
298 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
299 return build_type_attribute_variant (t2, attributes);
301 /* Simple way if one arg fails to specify argument types. */
302 if (TYPE_ARG_TYPES (t1) == 0)
304 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
305 t1 = build_type_attribute_variant (t1, attributes);
306 return qualify_type (t1, t2);
308 if (TYPE_ARG_TYPES (t2) == 0)
310 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
311 t1 = build_type_attribute_variant (t1, attributes);
312 return qualify_type (t1, t2);
315 /* If both args specify argument types, we must merge the two
316 lists, argument by argument. */
317 /* Tell global_bindings_p to return false so that variable_size
318 doesn't abort on VLAs in parameter types. */
319 c_override_global_bindings_to_false = true;
321 len = list_length (p1);
324 for (i = 0; i < len; i++)
325 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
330 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
332 /* A null type means arg type is not specified.
333 Take whatever the other function type has. */
334 if (TREE_VALUE (p1) == 0)
336 TREE_VALUE (n) = TREE_VALUE (p2);
339 if (TREE_VALUE (p2) == 0)
341 TREE_VALUE (n) = TREE_VALUE (p1);
345 /* Given wait (union {union wait *u; int *i} *)
346 and wait (union wait *),
347 prefer union wait * as type of parm. */
348 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
349 && TREE_VALUE (p1) != TREE_VALUE (p2))
352 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
353 memb; memb = TREE_CHAIN (memb))
354 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
356 TREE_VALUE (n) = TREE_VALUE (p2);
358 pedwarn ("function types not truly compatible in ISO C");
362 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
363 && TREE_VALUE (p2) != TREE_VALUE (p1))
366 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
367 memb; memb = TREE_CHAIN (memb))
368 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
370 TREE_VALUE (n) = TREE_VALUE (p1);
372 pedwarn ("function types not truly compatible in ISO C");
376 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
380 c_override_global_bindings_to_false = false;
381 t1 = build_function_type (valtype, newargs);
382 t1 = qualify_type (t1, t2);
383 /* ... falls through ... */
387 return build_type_attribute_variant (t1, attributes);
392 /* Return the type of a conditional expression between pointers to
393 possibly differently qualified versions of compatible types.
395 We assume that comp_target_types has already been done and returned
396 nonzero; if that isn't so, this may crash. */
399 common_pointer_type (tree t1, tree t2)
406 /* Save time if the two types are the same. */
408 if (t1 == t2) return t1;
410 /* If one type is nonsense, use the other. */
411 if (t1 == error_mark_node)
413 if (t2 == error_mark_node)
416 if (TREE_CODE (t1) != POINTER_TYPE || TREE_CODE (t2) != POINTER_TYPE)
419 /* Merge the attributes. */
420 attributes = targetm.merge_type_attributes (t1, t2);
422 /* Find the composite type of the target types, and combine the
423 qualifiers of the two types' targets. */
424 pointed_to_1 = TREE_TYPE (t1);
425 pointed_to_2 = TREE_TYPE (t2);
426 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
427 TYPE_MAIN_VARIANT (pointed_to_2));
428 t1 = build_pointer_type (c_build_qualified_type
430 TYPE_QUALS (pointed_to_1) |
431 TYPE_QUALS (pointed_to_2)));
432 return build_type_attribute_variant (t1, attributes);
435 /* Return the common type for two arithmetic types under the usual
436 arithmetic conversions. The default conversions have already been
437 applied, and enumerated types converted to their compatible integer
438 types. The resulting type is unqualified and has no attributes.
440 This is the type for the result of most arithmetic operations
441 if the operands have the given two types. */
444 common_type (tree t1, tree t2)
446 enum tree_code code1;
447 enum tree_code code2;
449 /* If one type is nonsense, use the other. */
450 if (t1 == error_mark_node)
452 if (t2 == error_mark_node)
455 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
456 t1 = TYPE_MAIN_VARIANT (t1);
458 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
459 t2 = TYPE_MAIN_VARIANT (t2);
461 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
462 t1 = build_type_attribute_variant (t1, NULL_TREE);
464 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
465 t2 = build_type_attribute_variant (t2, NULL_TREE);
467 /* Save time if the two types are the same. */
469 if (t1 == t2) return t1;
471 code1 = TREE_CODE (t1);
472 code2 = TREE_CODE (t2);
474 if (code1 != VECTOR_TYPE && code1 != COMPLEX_TYPE
475 && code1 != REAL_TYPE && code1 != INTEGER_TYPE)
478 if (code2 != VECTOR_TYPE && code2 != COMPLEX_TYPE
479 && code2 != REAL_TYPE && code2 != INTEGER_TYPE)
482 /* If one type is a vector type, return that type. (How the usual
483 arithmetic conversions apply to the vector types extension is not
484 precisely specified.) */
485 if (code1 == VECTOR_TYPE)
488 if (code2 == VECTOR_TYPE)
491 /* If one type is complex, form the common type of the non-complex
492 components, then make that complex. Use T1 or T2 if it is the
494 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
496 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
497 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
498 tree subtype = common_type (subtype1, subtype2);
500 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
502 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
505 return build_complex_type (subtype);
508 /* If only one is real, use it as the result. */
510 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
513 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
516 /* Both real or both integers; use the one with greater precision. */
518 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
520 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
523 /* Same precision. Prefer long longs to longs to ints when the
524 same precision, following the C99 rules on integer type rank
525 (which are equivalent to the C90 rules for C90 types). */
527 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
528 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
529 return long_long_unsigned_type_node;
531 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
532 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
534 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
535 return long_long_unsigned_type_node;
537 return long_long_integer_type_node;
540 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
541 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
542 return long_unsigned_type_node;
544 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
545 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
547 /* But preserve unsignedness from the other type,
548 since long cannot hold all the values of an unsigned int. */
549 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
550 return long_unsigned_type_node;
552 return long_integer_type_node;
555 /* Likewise, prefer long double to double even if same size. */
556 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
557 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
558 return long_double_type_node;
560 /* Otherwise prefer the unsigned one. */
562 if (TYPE_UNSIGNED (t1))
568 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
569 or various other operations. Return 2 if they are compatible
570 but a warning may be needed if you use them together. */
573 comptypes (tree type1, tree type2)
579 /* Suppress errors caused by previously reported errors. */
581 if (t1 == t2 || !t1 || !t2
582 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
585 /* If either type is the internal version of sizetype, return the
587 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
588 && TYPE_ORIG_SIZE_TYPE (t1))
589 t1 = TYPE_ORIG_SIZE_TYPE (t1);
591 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
592 && TYPE_ORIG_SIZE_TYPE (t2))
593 t2 = TYPE_ORIG_SIZE_TYPE (t2);
596 /* Enumerated types are compatible with integer types, but this is
597 not transitive: two enumerated types in the same translation unit
598 are compatible with each other only if they are the same type. */
600 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
601 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
602 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
603 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
608 /* Different classes of types can't be compatible. */
610 if (TREE_CODE (t1) != TREE_CODE (t2))
613 /* Qualifiers must match. C99 6.7.3p9 */
615 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
618 /* Allow for two different type nodes which have essentially the same
619 definition. Note that we already checked for equality of the type
620 qualifiers (just above). */
622 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
625 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
626 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
629 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
632 switch (TREE_CODE (t1))
635 /* We must give ObjC the first crack at comparing pointers, since
636 protocol qualifiers may be involved. */
637 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
639 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
640 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
644 val = function_types_compatible_p (t1, t2);
649 tree d1 = TYPE_DOMAIN (t1);
650 tree d2 = TYPE_DOMAIN (t2);
651 bool d1_variable, d2_variable;
652 bool d1_zero, d2_zero;
655 /* Target types must match incl. qualifiers. */
656 if (TREE_TYPE (t1) != TREE_TYPE (t2)
657 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
660 /* Sizes must match unless one is missing or variable. */
661 if (d1 == 0 || d2 == 0 || d1 == d2)
664 d1_zero = ! TYPE_MAX_VALUE (d1);
665 d2_zero = ! TYPE_MAX_VALUE (d2);
667 d1_variable = (! d1_zero
668 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
669 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
670 d2_variable = (! d2_zero
671 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
672 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
674 if (d1_variable || d2_variable)
676 if (d1_zero && d2_zero)
678 if (d1_zero || d2_zero
679 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
680 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
687 /* We are dealing with two distinct structs. In assorted Objective-C
688 corner cases, however, these can still be deemed equivalent. */
689 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
694 if (val != 1 && !same_translation_unit_p (t1, t2))
695 val = tagged_types_tu_compatible_p (t1, t2);
699 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
700 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
706 return attrval == 2 && val == 1 ? 2 : val;
709 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
710 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
711 to 1 or 0 depending if the check of the pointer types is meant to
712 be reflexive or not (typically, assignments are not reflexive,
713 while comparisons are reflexive).
717 comp_target_types (tree ttl, tree ttr, int reflexive)
721 /* Give objc_comptypes a crack at letting these types through. */
722 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
725 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
726 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
728 if (val == 2 && pedantic)
729 pedwarn ("types are not quite compatible");
733 /* Subroutines of `comptypes'. */
735 /* Determine whether two trees derive from the same translation unit.
736 If the CONTEXT chain ends in a null, that tree's context is still
737 being parsed, so if two trees have context chains ending in null,
738 they're in the same translation unit. */
740 same_translation_unit_p (tree t1, tree t2)
742 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
743 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
745 case 'd': t1 = DECL_CONTEXT (t1); break;
746 case 't': t1 = TYPE_CONTEXT (t1); break;
747 case 'x': t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
751 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
752 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
754 case 'd': t2 = DECL_CONTEXT (t2); break;
755 case 't': t2 = TYPE_CONTEXT (t2); break;
756 case 'x': t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
763 /* The C standard says that two structures in different translation
764 units are compatible with each other only if the types of their
765 fields are compatible (among other things). So, consider two copies
766 of this structure: */
768 struct tagged_tu_seen {
769 const struct tagged_tu_seen * next;
774 /* Can they be compatible with each other? We choose to break the
775 recursion by allowing those types to be compatible. */
777 static const struct tagged_tu_seen * tagged_tu_seen_base;
779 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
780 compatible. If the two types are not the same (which has been
781 checked earlier), this can only happen when multiple translation
782 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
786 tagged_types_tu_compatible_p (tree t1, tree t2)
789 bool needs_warning = false;
791 /* We have to verify that the tags of the types are the same. This
792 is harder than it looks because this may be a typedef, so we have
793 to go look at the original type. It may even be a typedef of a
795 In the case of compiler-created builtin structs the TYPE_DECL
796 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
797 while (TYPE_NAME (t1)
798 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
799 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
800 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
802 while (TYPE_NAME (t2)
803 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
804 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
805 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
807 /* C90 didn't have the requirement that the two tags be the same. */
808 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
811 /* C90 didn't say what happened if one or both of the types were
812 incomplete; we choose to follow C99 rules here, which is that they
814 if (TYPE_SIZE (t1) == NULL
815 || TYPE_SIZE (t2) == NULL)
819 const struct tagged_tu_seen * tts_i;
820 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
821 if (tts_i->t1 == t1 && tts_i->t2 == t2)
825 switch (TREE_CODE (t1))
830 /* Speed up the case where the type values are in the same order. */
831 tree tv1 = TYPE_VALUES (t1);
832 tree tv2 = TYPE_VALUES (t2);
837 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
839 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
841 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
845 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
847 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
850 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
853 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
855 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
857 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
865 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
868 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
871 struct tagged_tu_seen tts;
873 tts.next = tagged_tu_seen_base;
876 tagged_tu_seen_base = &tts;
878 if (DECL_NAME (s1) != NULL)
879 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
880 if (DECL_NAME (s1) == DECL_NAME (s2))
883 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
887 needs_warning = true;
889 if (TREE_CODE (s1) == FIELD_DECL
890 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
891 DECL_FIELD_BIT_OFFSET (s2)) != 1)
897 tagged_tu_seen_base = tts.next;
901 return needs_warning ? 2 : 1;
906 struct tagged_tu_seen tts;
908 tts.next = tagged_tu_seen_base;
911 tagged_tu_seen_base = &tts;
913 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
915 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
918 if (TREE_CODE (s1) != TREE_CODE (s2)
919 || DECL_NAME (s1) != DECL_NAME (s2))
921 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
925 needs_warning = true;
927 if (TREE_CODE (s1) == FIELD_DECL
928 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
929 DECL_FIELD_BIT_OFFSET (s2)) != 1)
932 tagged_tu_seen_base = tts.next;
935 return needs_warning ? 2 : 1;
943 /* Return 1 if two function types F1 and F2 are compatible.
944 If either type specifies no argument types,
945 the other must specify a fixed number of self-promoting arg types.
946 Otherwise, if one type specifies only the number of arguments,
947 the other must specify that number of self-promoting arg types.
948 Otherwise, the argument types must match. */
951 function_types_compatible_p (tree f1, tree f2)
954 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
959 ret1 = TREE_TYPE (f1);
960 ret2 = TREE_TYPE (f2);
962 /* 'volatile' qualifiers on a function's return type mean the function
964 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
965 pedwarn ("function return types not compatible due to `volatile'");
966 if (TYPE_VOLATILE (ret1))
967 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
968 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
969 if (TYPE_VOLATILE (ret2))
970 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
971 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
972 val = comptypes (ret1, ret2);
976 args1 = TYPE_ARG_TYPES (f1);
977 args2 = TYPE_ARG_TYPES (f2);
979 /* An unspecified parmlist matches any specified parmlist
980 whose argument types don't need default promotions. */
984 if (!self_promoting_args_p (args2))
986 /* If one of these types comes from a non-prototype fn definition,
987 compare that with the other type's arglist.
988 If they don't match, ask for a warning (but no error). */
989 if (TYPE_ACTUAL_ARG_TYPES (f1)
990 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
996 if (!self_promoting_args_p (args1))
998 if (TYPE_ACTUAL_ARG_TYPES (f2)
999 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1004 /* Both types have argument lists: compare them and propagate results. */
1005 val1 = type_lists_compatible_p (args1, args2);
1006 return val1 != 1 ? val1 : val;
1009 /* Check two lists of types for compatibility,
1010 returning 0 for incompatible, 1 for compatible,
1011 or 2 for compatible with warning. */
1014 type_lists_compatible_p (tree args1, tree args2)
1016 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1022 if (args1 == 0 && args2 == 0)
1024 /* If one list is shorter than the other,
1025 they fail to match. */
1026 if (args1 == 0 || args2 == 0)
1028 /* A null pointer instead of a type
1029 means there is supposed to be an argument
1030 but nothing is specified about what type it has.
1031 So match anything that self-promotes. */
1032 if (TREE_VALUE (args1) == 0)
1034 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1037 else if (TREE_VALUE (args2) == 0)
1039 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1042 /* If one of the lists has an error marker, ignore this arg. */
1043 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1044 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1046 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1047 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
1049 /* Allow wait (union {union wait *u; int *i} *)
1050 and wait (union wait *) to be compatible. */
1051 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1052 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1053 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1054 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1055 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1056 TYPE_SIZE (TREE_VALUE (args2))))
1059 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1060 memb; memb = TREE_CHAIN (memb))
1061 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
1066 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1067 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1068 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1069 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1070 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1071 TYPE_SIZE (TREE_VALUE (args1))))
1074 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1075 memb; memb = TREE_CHAIN (memb))
1076 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
1085 /* comptypes said ok, but record if it said to warn. */
1089 args1 = TREE_CHAIN (args1);
1090 args2 = TREE_CHAIN (args2);
1094 /* Compute the size to increment a pointer by. */
1097 c_size_in_bytes (tree type)
1099 enum tree_code code = TREE_CODE (type);
1101 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1102 return size_one_node;
1104 if (!COMPLETE_OR_VOID_TYPE_P (type))
1106 error ("arithmetic on pointer to an incomplete type");
1107 return size_one_node;
1110 /* Convert in case a char is more than one unit. */
1111 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1112 size_int (TYPE_PRECISION (char_type_node)
1116 /* Return either DECL or its known constant value (if it has one). */
1119 decl_constant_value (tree decl)
1121 if (/* Don't change a variable array bound or initial value to a constant
1122 in a place where a variable is invalid. Note that DECL_INITIAL
1123 isn't valid for a PARM_DECL. */
1124 current_function_decl != 0
1125 && TREE_CODE (decl) != PARM_DECL
1126 && ! TREE_THIS_VOLATILE (decl)
1127 && TREE_READONLY (decl)
1128 && DECL_INITIAL (decl) != 0
1129 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1130 /* This is invalid if initial value is not constant.
1131 If it has either a function call, a memory reference,
1132 or a variable, then re-evaluating it could give different results. */
1133 && TREE_CONSTANT (DECL_INITIAL (decl))
1134 /* Check for cases where this is sub-optimal, even though valid. */
1135 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1136 return DECL_INITIAL (decl);
1140 /* Return either DECL or its known constant value (if it has one), but
1141 return DECL if pedantic or DECL has mode BLKmode. This is for
1142 bug-compatibility with the old behavior of decl_constant_value
1143 (before GCC 3.0); every use of this function is a bug and it should
1144 be removed before GCC 3.1. It is not appropriate to use pedantic
1145 in a way that affects optimization, and BLKmode is probably not the
1146 right test for avoiding misoptimizations either. */
1149 decl_constant_value_for_broken_optimization (tree decl)
1151 if (pedantic || DECL_MODE (decl) == BLKmode)
1154 return decl_constant_value (decl);
1158 /* Perform the default conversion of arrays and functions to pointers.
1159 Return the result of converting EXP. For any other expression, just
1163 default_function_array_conversion (tree exp)
1166 tree type = TREE_TYPE (exp);
1167 enum tree_code code = TREE_CODE (type);
1170 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1173 Do not use STRIP_NOPS here! It will remove conversions from pointer
1174 to integer and cause infinite recursion. */
1176 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1177 || (TREE_CODE (exp) == NOP_EXPR
1178 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1180 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1182 exp = TREE_OPERAND (exp, 0);
1185 /* Preserve the original expression code. */
1186 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1187 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1189 if (code == FUNCTION_TYPE)
1191 return build_unary_op (ADDR_EXPR, exp, 0);
1193 if (code == ARRAY_TYPE)
1196 tree restype = TREE_TYPE (type);
1202 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1204 constp = TREE_READONLY (exp);
1205 volatilep = TREE_THIS_VOLATILE (exp);
1208 if (TYPE_QUALS (type) || constp || volatilep)
1210 = c_build_qualified_type (restype,
1212 | (constp * TYPE_QUAL_CONST)
1213 | (volatilep * TYPE_QUAL_VOLATILE));
1215 if (TREE_CODE (exp) == INDIRECT_REF)
1216 return convert (build_pointer_type (restype),
1217 TREE_OPERAND (exp, 0));
1219 if (TREE_CODE (exp) == COMPOUND_EXPR)
1221 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1222 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1223 TREE_OPERAND (exp, 0), op1);
1226 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1227 if (!flag_isoc99 && !lvalue_array_p)
1229 /* Before C99, non-lvalue arrays do not decay to pointers.
1230 Normally, using such an array would be invalid; but it can
1231 be used correctly inside sizeof or as a statement expression.
1232 Thus, do not give an error here; an error will result later. */
1236 ptrtype = build_pointer_type (restype);
1238 if (TREE_CODE (exp) == VAR_DECL)
1240 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1241 ADDR_EXPR because it's the best way of representing what
1242 happens in C when we take the address of an array and place
1243 it in a pointer to the element type. */
1244 adr = build1 (ADDR_EXPR, ptrtype, exp);
1245 if (!c_mark_addressable (exp))
1246 return error_mark_node;
1247 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1250 /* This way is better for a COMPONENT_REF since it can
1251 simplify the offset for a component. */
1252 adr = build_unary_op (ADDR_EXPR, exp, 1);
1253 return convert (ptrtype, adr);
1258 /* Perform default promotions for C data used in expressions.
1259 Arrays and functions are converted to pointers;
1260 enumeral types or short or char, to int.
1261 In addition, manifest constants symbols are replaced by their values. */
1264 default_conversion (tree exp)
1267 tree type = TREE_TYPE (exp);
1268 enum tree_code code = TREE_CODE (type);
1270 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1271 return default_function_array_conversion (exp);
1273 /* Constants can be used directly unless they're not loadable. */
1274 if (TREE_CODE (exp) == CONST_DECL)
1275 exp = DECL_INITIAL (exp);
1277 /* Replace a nonvolatile const static variable with its value unless
1278 it is an array, in which case we must be sure that taking the
1279 address of the array produces consistent results. */
1280 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1282 exp = decl_constant_value_for_broken_optimization (exp);
1283 type = TREE_TYPE (exp);
1286 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1289 Do not use STRIP_NOPS here! It will remove conversions from pointer
1290 to integer and cause infinite recursion. */
1292 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1293 || (TREE_CODE (exp) == NOP_EXPR
1294 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1295 exp = TREE_OPERAND (exp, 0);
1297 /* Preserve the original expression code. */
1298 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1299 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1301 /* Normally convert enums to int,
1302 but convert wide enums to something wider. */
1303 if (code == ENUMERAL_TYPE)
1305 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1306 TYPE_PRECISION (integer_type_node)),
1307 ((TYPE_PRECISION (type)
1308 >= TYPE_PRECISION (integer_type_node))
1309 && TYPE_UNSIGNED (type)));
1311 return convert (type, exp);
1314 if (TREE_CODE (exp) == COMPONENT_REF
1315 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1316 /* If it's thinner than an int, promote it like a
1317 c_promoting_integer_type_p, otherwise leave it alone. */
1318 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1319 TYPE_PRECISION (integer_type_node)))
1320 return convert (integer_type_node, exp);
1322 if (c_promoting_integer_type_p (type))
1324 /* Preserve unsignedness if not really getting any wider. */
1325 if (TYPE_UNSIGNED (type)
1326 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1327 return convert (unsigned_type_node, exp);
1329 return convert (integer_type_node, exp);
1332 if (code == VOID_TYPE)
1334 error ("void value not ignored as it ought to be");
1335 return error_mark_node;
1340 /* Look up COMPONENT in a structure or union DECL.
1342 If the component name is not found, returns NULL_TREE. Otherwise,
1343 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1344 stepping down the chain to the component, which is in the last
1345 TREE_VALUE of the list. Normally the list is of length one, but if
1346 the component is embedded within (nested) anonymous structures or
1347 unions, the list steps down the chain to the component. */
1350 lookup_field (tree decl, tree component)
1352 tree type = TREE_TYPE (decl);
1355 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1356 to the field elements. Use a binary search on this array to quickly
1357 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1358 will always be set for structures which have many elements. */
1360 if (TYPE_LANG_SPECIFIC (type))
1363 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1365 field = TYPE_FIELDS (type);
1367 top = TYPE_LANG_SPECIFIC (type)->s->len;
1368 while (top - bot > 1)
1370 half = (top - bot + 1) >> 1;
1371 field = field_array[bot+half];
1373 if (DECL_NAME (field) == NULL_TREE)
1375 /* Step through all anon unions in linear fashion. */
1376 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1378 field = field_array[bot++];
1379 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1380 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1382 tree anon = lookup_field (field, component);
1385 return tree_cons (NULL_TREE, field, anon);
1389 /* Entire record is only anon unions. */
1393 /* Restart the binary search, with new lower bound. */
1397 if (DECL_NAME (field) == component)
1399 if (DECL_NAME (field) < component)
1405 if (DECL_NAME (field_array[bot]) == component)
1406 field = field_array[bot];
1407 else if (DECL_NAME (field) != component)
1412 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1414 if (DECL_NAME (field) == NULL_TREE
1415 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1416 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1418 tree anon = lookup_field (field, component);
1421 return tree_cons (NULL_TREE, field, anon);
1424 if (DECL_NAME (field) == component)
1428 if (field == NULL_TREE)
1432 return tree_cons (NULL_TREE, field, NULL_TREE);
1435 /* Make an expression to refer to the COMPONENT field of
1436 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1439 build_component_ref (tree datum, tree component)
1441 tree type = TREE_TYPE (datum);
1442 enum tree_code code = TREE_CODE (type);
1446 if (!objc_is_public (datum, component))
1447 return error_mark_node;
1449 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1450 Ensure that the arguments are not lvalues; otherwise,
1451 if the component is an array, it would wrongly decay to a pointer in
1453 We cannot do this with a COND_EXPR, because in a conditional expression
1454 the default promotions are applied to both sides, and this would yield
1455 the wrong type of the result; for example, if the components have
1457 switch (TREE_CODE (datum))
1461 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1462 return build (COMPOUND_EXPR, TREE_TYPE (value),
1463 TREE_OPERAND (datum, 0), non_lvalue (value));
1469 /* See if there is a field or component with name COMPONENT. */
1471 if (code == RECORD_TYPE || code == UNION_TYPE)
1473 if (!COMPLETE_TYPE_P (type))
1475 c_incomplete_type_error (NULL_TREE, type);
1476 return error_mark_node;
1479 field = lookup_field (datum, component);
1483 error ("%s has no member named `%s'",
1484 code == RECORD_TYPE ? "structure" : "union",
1485 IDENTIFIER_POINTER (component));
1486 return error_mark_node;
1489 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1490 This might be better solved in future the way the C++ front
1491 end does it - by giving the anonymous entities each a
1492 separate name and type, and then have build_component_ref
1493 recursively call itself. We can't do that here. */
1496 tree subdatum = TREE_VALUE (field);
1498 if (TREE_TYPE (subdatum) == error_mark_node)
1499 return error_mark_node;
1501 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1503 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1504 TREE_READONLY (ref) = 1;
1505 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1506 TREE_THIS_VOLATILE (ref) = 1;
1508 if (TREE_DEPRECATED (subdatum))
1509 warn_deprecated_use (subdatum);
1513 field = TREE_CHAIN (field);
1519 else if (code != ERROR_MARK)
1520 error ("request for member `%s' in something not a structure or union",
1521 IDENTIFIER_POINTER (component));
1523 return error_mark_node;
1526 /* Given an expression PTR for a pointer, return an expression
1527 for the value pointed to.
1528 ERRORSTRING is the name of the operator to appear in error messages. */
1531 build_indirect_ref (tree ptr, const char *errorstring)
1533 tree pointer = default_conversion (ptr);
1534 tree type = TREE_TYPE (pointer);
1536 if (TREE_CODE (type) == POINTER_TYPE)
1538 if (TREE_CODE (pointer) == ADDR_EXPR
1539 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1540 == TREE_TYPE (type)))
1541 return TREE_OPERAND (pointer, 0);
1544 tree t = TREE_TYPE (type);
1545 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1547 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1549 error ("dereferencing pointer to incomplete type");
1550 return error_mark_node;
1552 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1553 warning ("dereferencing `void *' pointer");
1555 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1556 so that we get the proper error message if the result is used
1557 to assign to. Also, &* is supposed to be a no-op.
1558 And ANSI C seems to specify that the type of the result
1559 should be the const type. */
1560 /* A de-reference of a pointer to const is not a const. It is valid
1561 to change it via some other pointer. */
1562 TREE_READONLY (ref) = TYPE_READONLY (t);
1563 TREE_SIDE_EFFECTS (ref)
1564 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1565 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1569 else if (TREE_CODE (pointer) != ERROR_MARK)
1570 error ("invalid type argument of `%s'", errorstring);
1571 return error_mark_node;
1574 /* This handles expressions of the form "a[i]", which denotes
1577 This is logically equivalent in C to *(a+i), but we may do it differently.
1578 If A is a variable or a member, we generate a primitive ARRAY_REF.
1579 This avoids forcing the array out of registers, and can work on
1580 arrays that are not lvalues (for example, members of structures returned
1584 build_array_ref (tree array, tree index)
1588 error ("subscript missing in array reference");
1589 return error_mark_node;
1592 if (TREE_TYPE (array) == error_mark_node
1593 || TREE_TYPE (index) == error_mark_node)
1594 return error_mark_node;
1596 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1600 /* Subscripting with type char is likely to lose
1601 on a machine where chars are signed.
1602 So warn on any machine, but optionally.
1603 Don't warn for unsigned char since that type is safe.
1604 Don't warn for signed char because anyone who uses that
1605 must have done so deliberately. */
1606 if (warn_char_subscripts
1607 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1608 warning ("array subscript has type `char'");
1610 /* Apply default promotions *after* noticing character types. */
1611 index = default_conversion (index);
1613 /* Require integer *after* promotion, for sake of enums. */
1614 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1616 error ("array subscript is not an integer");
1617 return error_mark_node;
1620 /* An array that is indexed by a non-constant
1621 cannot be stored in a register; we must be able to do
1622 address arithmetic on its address.
1623 Likewise an array of elements of variable size. */
1624 if (TREE_CODE (index) != INTEGER_CST
1625 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1626 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1628 if (!c_mark_addressable (array))
1629 return error_mark_node;
1631 /* An array that is indexed by a constant value which is not within
1632 the array bounds cannot be stored in a register either; because we
1633 would get a crash in store_bit_field/extract_bit_field when trying
1634 to access a non-existent part of the register. */
1635 if (TREE_CODE (index) == INTEGER_CST
1636 && TYPE_DOMAIN (TREE_TYPE (array))
1637 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1639 if (!c_mark_addressable (array))
1640 return error_mark_node;
1646 while (TREE_CODE (foo) == COMPONENT_REF)
1647 foo = TREE_OPERAND (foo, 0);
1648 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1649 pedwarn ("ISO C forbids subscripting `register' array");
1650 else if (! flag_isoc99 && ! lvalue_p (foo))
1651 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1654 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1655 rval = build (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1656 /* Array ref is const/volatile if the array elements are
1657 or if the array is. */
1658 TREE_READONLY (rval)
1659 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1660 | TREE_READONLY (array));
1661 TREE_SIDE_EFFECTS (rval)
1662 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1663 | TREE_SIDE_EFFECTS (array));
1664 TREE_THIS_VOLATILE (rval)
1665 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1666 /* This was added by rms on 16 Nov 91.
1667 It fixes vol struct foo *a; a->elts[1]
1668 in an inline function.
1669 Hope it doesn't break something else. */
1670 | TREE_THIS_VOLATILE (array));
1671 return require_complete_type (fold (rval));
1675 tree ar = default_conversion (array);
1676 tree ind = default_conversion (index);
1678 /* Do the same warning check as above, but only on the part that's
1679 syntactically the index and only if it is also semantically
1681 if (warn_char_subscripts
1682 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1683 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1684 warning ("subscript has type `char'");
1686 /* Put the integer in IND to simplify error checking. */
1687 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1694 if (ar == error_mark_node)
1697 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1698 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1700 error ("subscripted value is neither array nor pointer");
1701 return error_mark_node;
1703 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1705 error ("array subscript is not an integer");
1706 return error_mark_node;
1709 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1714 /* Build an external reference to identifier ID. FUN indicates
1715 whether this will be used for a function call. */
1717 build_external_ref (tree id, int fun)
1720 tree decl = lookup_name (id);
1721 tree objc_ivar = lookup_objc_ivar (id);
1723 if (decl && decl != error_mark_node)
1725 /* Properly declared variable or function reference. */
1728 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1730 warning ("local declaration of `%s' hides instance variable",
1731 IDENTIFIER_POINTER (id));
1740 /* Implicit function declaration. */
1741 ref = implicitly_declare (id);
1742 else if (decl == error_mark_node)
1743 /* Don't complain about something that's already been
1744 complained about. */
1745 return error_mark_node;
1748 undeclared_variable (id);
1749 return error_mark_node;
1752 if (TREE_TYPE (ref) == error_mark_node)
1753 return error_mark_node;
1755 if (TREE_DEPRECATED (ref))
1756 warn_deprecated_use (ref);
1758 if (!skip_evaluation)
1759 assemble_external (ref);
1760 TREE_USED (ref) = 1;
1762 if (TREE_CODE (ref) == CONST_DECL)
1764 ref = DECL_INITIAL (ref);
1765 TREE_CONSTANT (ref) = 1;
1766 TREE_INVARIANT (ref) = 1;
1768 else if (current_function_decl != 0
1769 && !DECL_FILE_SCOPE_P (current_function_decl)
1770 && (TREE_CODE (ref) == VAR_DECL
1771 || TREE_CODE (ref) == PARM_DECL
1772 || TREE_CODE (ref) == FUNCTION_DECL))
1774 tree context = decl_function_context (ref);
1776 if (context != 0 && context != current_function_decl)
1777 DECL_NONLOCAL (ref) = 1;
1783 /* Build a function call to function FUNCTION with parameters PARAMS.
1784 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1785 TREE_VALUE of each node is a parameter-expression.
1786 FUNCTION's data type may be a function type or a pointer-to-function. */
1789 build_function_call (tree function, tree params)
1791 tree fntype, fundecl = 0;
1792 tree coerced_params;
1793 tree name = NULL_TREE, result;
1796 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1797 STRIP_TYPE_NOPS (function);
1799 /* Convert anything with function type to a pointer-to-function. */
1800 if (TREE_CODE (function) == FUNCTION_DECL)
1802 name = DECL_NAME (function);
1804 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1805 (because calling an inline function does not mean the function
1806 needs to be separately compiled). */
1807 fntype = build_type_variant (TREE_TYPE (function),
1808 TREE_READONLY (function),
1809 TREE_THIS_VOLATILE (function));
1811 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1814 function = default_conversion (function);
1816 fntype = TREE_TYPE (function);
1818 if (TREE_CODE (fntype) == ERROR_MARK)
1819 return error_mark_node;
1821 if (!(TREE_CODE (fntype) == POINTER_TYPE
1822 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1824 error ("called object is not a function");
1825 return error_mark_node;
1828 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1829 current_function_returns_abnormally = 1;
1831 /* fntype now gets the type of function pointed to. */
1832 fntype = TREE_TYPE (fntype);
1834 /* Check that the function is called through a compatible prototype.
1835 If it is not, replace the call by a trap, wrapped up in a compound
1836 expression if necessary. This has the nice side-effect to prevent
1837 the tree-inliner from generating invalid assignment trees which may
1838 blow up in the RTL expander later.
1840 ??? This doesn't work for Objective-C because objc_comptypes
1841 refuses to compare function prototypes, yet the compiler appears
1842 to build calls that are flagged as invalid by C's comptypes. */
1843 if (! c_dialect_objc ()
1844 && TREE_CODE (function) == NOP_EXPR
1845 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1846 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1847 && ! comptypes (fntype, TREE_TYPE (tem)))
1849 tree return_type = TREE_TYPE (fntype);
1850 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1853 /* This situation leads to run-time undefined behavior. We can't,
1854 therefore, simply error unless we can prove that all possible
1855 executions of the program must execute the code. */
1856 warning ("function called through a non-compatible type");
1858 /* We can, however, treat "undefined" any way we please.
1859 Call abort to encourage the user to fix the program. */
1860 inform ("if this code is reached, the program will abort");
1862 if (VOID_TYPE_P (return_type))
1868 if (AGGREGATE_TYPE_P (return_type))
1869 rhs = build_compound_literal (return_type,
1870 build_constructor (return_type,
1873 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1875 return build (COMPOUND_EXPR, return_type, trap, rhs);
1879 /* Convert the parameters to the types declared in the
1880 function prototype, or apply default promotions. */
1883 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1885 /* Check that the arguments to the function are valid. */
1887 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1889 result = build (CALL_EXPR, TREE_TYPE (fntype),
1890 function, coerced_params, NULL_TREE);
1891 TREE_SIDE_EFFECTS (result) = 1;
1893 if (require_constant_value)
1895 result = fold_initializer (result);
1897 if (TREE_CONSTANT (result)
1898 && (name == NULL_TREE
1899 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1900 pedwarn_init ("initializer element is not constant");
1903 result = fold (result);
1905 if (VOID_TYPE_P (TREE_TYPE (result)))
1907 return require_complete_type (result);
1910 /* Convert the argument expressions in the list VALUES
1911 to the types in the list TYPELIST. The result is a list of converted
1912 argument expressions.
1914 If TYPELIST is exhausted, or when an element has NULL as its type,
1915 perform the default conversions.
1917 PARMLIST is the chain of parm decls for the function being called.
1918 It may be 0, if that info is not available.
1919 It is used only for generating error messages.
1921 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1923 This is also where warnings about wrong number of args are generated.
1925 Both VALUES and the returned value are chains of TREE_LIST nodes
1926 with the elements of the list in the TREE_VALUE slots of those nodes. */
1929 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1931 tree typetail, valtail;
1935 /* Scan the given expressions and types, producing individual
1936 converted arguments and pushing them on RESULT in reverse order. */
1938 for (valtail = values, typetail = typelist, parmnum = 0;
1940 valtail = TREE_CHAIN (valtail), parmnum++)
1942 tree type = typetail ? TREE_VALUE (typetail) : 0;
1943 tree val = TREE_VALUE (valtail);
1945 if (type == void_type_node)
1948 error ("too many arguments to function `%s'",
1949 IDENTIFIER_POINTER (name));
1951 error ("too many arguments to function");
1955 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1956 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1957 to convert automatically to a pointer. */
1958 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1959 val = TREE_OPERAND (val, 0);
1961 val = default_function_array_conversion (val);
1963 val = require_complete_type (val);
1967 /* Formal parm type is specified by a function prototype. */
1970 if (!COMPLETE_TYPE_P (type))
1972 error ("type of formal parameter %d is incomplete", parmnum + 1);
1977 /* Optionally warn about conversions that
1978 differ from the default conversions. */
1979 if (warn_conversion || warn_traditional)
1981 int formal_prec = TYPE_PRECISION (type);
1983 if (INTEGRAL_TYPE_P (type)
1984 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1985 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1986 if (INTEGRAL_TYPE_P (type)
1987 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1988 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1989 else if (TREE_CODE (type) == COMPLEX_TYPE
1990 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1991 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1992 else if (TREE_CODE (type) == REAL_TYPE
1993 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1994 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1995 else if (TREE_CODE (type) == COMPLEX_TYPE
1996 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1997 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1998 else if (TREE_CODE (type) == REAL_TYPE
1999 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2000 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
2001 /* ??? At some point, messages should be written about
2002 conversions between complex types, but that's too messy
2004 else if (TREE_CODE (type) == REAL_TYPE
2005 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2007 /* Warn if any argument is passed as `float',
2008 since without a prototype it would be `double'. */
2009 if (formal_prec == TYPE_PRECISION (float_type_node))
2010 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
2012 /* Detect integer changing in width or signedness.
2013 These warnings are only activated with
2014 -Wconversion, not with -Wtraditional. */
2015 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2016 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2018 tree would_have_been = default_conversion (val);
2019 tree type1 = TREE_TYPE (would_have_been);
2021 if (TREE_CODE (type) == ENUMERAL_TYPE
2022 && (TYPE_MAIN_VARIANT (type)
2023 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2024 /* No warning if function asks for enum
2025 and the actual arg is that enum type. */
2027 else if (formal_prec != TYPE_PRECISION (type1))
2028 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2029 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2031 /* Don't complain if the formal parameter type
2032 is an enum, because we can't tell now whether
2033 the value was an enum--even the same enum. */
2034 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2036 else if (TREE_CODE (val) == INTEGER_CST
2037 && int_fits_type_p (val, type))
2038 /* Change in signedness doesn't matter
2039 if a constant value is unaffected. */
2041 /* Likewise for a constant in a NOP_EXPR. */
2042 else if (TREE_CODE (val) == NOP_EXPR
2043 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2044 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2046 /* If the value is extended from a narrower
2047 unsigned type, it doesn't matter whether we
2048 pass it as signed or unsigned; the value
2049 certainly is the same either way. */
2050 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2051 && TYPE_UNSIGNED (TREE_TYPE (val)))
2053 else if (TYPE_UNSIGNED (type))
2054 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2056 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2060 parmval = convert_for_assignment (type, val,
2061 (char *) 0, /* arg passing */
2062 fundecl, name, parmnum + 1);
2064 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2065 && INTEGRAL_TYPE_P (type)
2066 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2067 parmval = default_conversion (parmval);
2069 result = tree_cons (NULL_TREE, parmval, result);
2071 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2072 && (TYPE_PRECISION (TREE_TYPE (val))
2073 < TYPE_PRECISION (double_type_node)))
2074 /* Convert `float' to `double'. */
2075 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2077 /* Convert `short' and `char' to full-size `int'. */
2078 result = tree_cons (NULL_TREE, default_conversion (val), result);
2081 typetail = TREE_CHAIN (typetail);
2084 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2087 error ("too few arguments to function `%s'",
2088 IDENTIFIER_POINTER (name));
2090 error ("too few arguments to function");
2093 return nreverse (result);
2096 /* This is the entry point used by the parser
2097 for binary operators in the input.
2098 In addition to constructing the expression,
2099 we check for operands that were written with other binary operators
2100 in a way that is likely to confuse the user. */
2103 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
2105 tree result = build_binary_op (code, arg1, arg2, 1);
2108 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2109 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2110 enum tree_code code1 = ERROR_MARK;
2111 enum tree_code code2 = ERROR_MARK;
2113 if (TREE_CODE (result) == ERROR_MARK)
2114 return error_mark_node;
2116 if (IS_EXPR_CODE_CLASS (class1))
2117 code1 = C_EXP_ORIGINAL_CODE (arg1);
2118 if (IS_EXPR_CODE_CLASS (class2))
2119 code2 = C_EXP_ORIGINAL_CODE (arg2);
2121 /* Check for cases such as x+y<<z which users are likely
2122 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2123 is cleared to prevent these warnings. */
2124 if (warn_parentheses)
2126 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2128 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2129 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2130 warning ("suggest parentheses around + or - inside shift");
2133 if (code == TRUTH_ORIF_EXPR)
2135 if (code1 == TRUTH_ANDIF_EXPR
2136 || code2 == TRUTH_ANDIF_EXPR)
2137 warning ("suggest parentheses around && within ||");
2140 if (code == BIT_IOR_EXPR)
2142 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2143 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2144 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2145 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2146 warning ("suggest parentheses around arithmetic in operand of |");
2147 /* Check cases like x|y==z */
2148 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2149 warning ("suggest parentheses around comparison in operand of |");
2152 if (code == BIT_XOR_EXPR)
2154 if (code1 == BIT_AND_EXPR
2155 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2156 || code2 == BIT_AND_EXPR
2157 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2158 warning ("suggest parentheses around arithmetic in operand of ^");
2159 /* Check cases like x^y==z */
2160 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2161 warning ("suggest parentheses around comparison in operand of ^");
2164 if (code == BIT_AND_EXPR)
2166 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2167 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2168 warning ("suggest parentheses around + or - in operand of &");
2169 /* Check cases like x&y==z */
2170 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2171 warning ("suggest parentheses around comparison in operand of &");
2173 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2174 if (TREE_CODE_CLASS (code) == '<'
2175 && (TREE_CODE_CLASS (code1) == '<'
2176 || TREE_CODE_CLASS (code2) == '<'))
2177 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2181 unsigned_conversion_warning (result, arg1);
2182 unsigned_conversion_warning (result, arg2);
2183 overflow_warning (result);
2185 class = TREE_CODE_CLASS (TREE_CODE (result));
2187 /* Record the code that was specified in the source,
2188 for the sake of warnings about confusing nesting. */
2189 if (IS_EXPR_CODE_CLASS (class))
2190 C_SET_EXP_ORIGINAL_CODE (result, code);
2193 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2194 so that convert_for_assignment wouldn't strip it.
2195 That way, we got warnings for things like p = (1 - 1).
2196 But it turns out we should not get those warnings. */
2197 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2198 C_SET_EXP_ORIGINAL_CODE (result, code);
2204 /* Return a tree for the difference of pointers OP0 and OP1.
2205 The resulting tree has type int. */
2208 pointer_diff (tree op0, tree op1)
2210 tree restype = ptrdiff_type_node;
2212 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2213 tree con0, con1, lit0, lit1;
2214 tree orig_op1 = op1;
2216 if (pedantic || warn_pointer_arith)
2218 if (TREE_CODE (target_type) == VOID_TYPE)
2219 pedwarn ("pointer of type `void *' used in subtraction");
2220 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2221 pedwarn ("pointer to a function used in subtraction");
2224 /* If the conversion to ptrdiff_type does anything like widening or
2225 converting a partial to an integral mode, we get a convert_expression
2226 that is in the way to do any simplifications.
2227 (fold-const.c doesn't know that the extra bits won't be needed.
2228 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2229 different mode in place.)
2230 So first try to find a common term here 'by hand'; we want to cover
2231 at least the cases that occur in legal static initializers. */
2232 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2233 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2235 if (TREE_CODE (con0) == PLUS_EXPR)
2237 lit0 = TREE_OPERAND (con0, 1);
2238 con0 = TREE_OPERAND (con0, 0);
2241 lit0 = integer_zero_node;
2243 if (TREE_CODE (con1) == PLUS_EXPR)
2245 lit1 = TREE_OPERAND (con1, 1);
2246 con1 = TREE_OPERAND (con1, 0);
2249 lit1 = integer_zero_node;
2251 if (operand_equal_p (con0, con1, 0))
2258 /* First do the subtraction as integers;
2259 then drop through to build the divide operator.
2260 Do not do default conversions on the minus operator
2261 in case restype is a short type. */
2263 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2264 convert (restype, op1), 0);
2265 /* This generates an error if op1 is pointer to incomplete type. */
2266 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2267 error ("arithmetic on pointer to an incomplete type");
2269 /* This generates an error if op0 is pointer to incomplete type. */
2270 op1 = c_size_in_bytes (target_type);
2272 /* Divide by the size, in easiest possible way. */
2273 return fold (build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2276 /* Construct and perhaps optimize a tree representation
2277 for a unary operation. CODE, a tree_code, specifies the operation
2278 and XARG is the operand.
2279 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2280 the default promotions (such as from short to int).
2281 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2282 allows non-lvalues; this is only used to handle conversion of non-lvalue
2283 arrays to pointers in C99. */
2286 build_unary_op (enum tree_code code, tree xarg, int flag)
2288 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2291 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2293 int noconvert = flag;
2295 if (typecode == ERROR_MARK)
2296 return error_mark_node;
2297 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2298 typecode = INTEGER_TYPE;
2303 /* This is used for unary plus, because a CONVERT_EXPR
2304 is enough to prevent anybody from looking inside for
2305 associativity, but won't generate any code. */
2306 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2307 || typecode == COMPLEX_TYPE
2308 || typecode == VECTOR_TYPE))
2310 error ("wrong type argument to unary plus");
2311 return error_mark_node;
2313 else if (!noconvert)
2314 arg = default_conversion (arg);
2315 arg = non_lvalue (arg);
2319 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2320 || typecode == COMPLEX_TYPE
2321 || typecode == VECTOR_TYPE))
2323 error ("wrong type argument to unary minus");
2324 return error_mark_node;
2326 else if (!noconvert)
2327 arg = default_conversion (arg);
2331 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2334 arg = default_conversion (arg);
2336 else if (typecode == COMPLEX_TYPE)
2340 pedwarn ("ISO C does not support `~' for complex conjugation");
2342 arg = default_conversion (arg);
2346 error ("wrong type argument to bit-complement");
2347 return error_mark_node;
2352 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2354 error ("wrong type argument to abs");
2355 return error_mark_node;
2357 else if (!noconvert)
2358 arg = default_conversion (arg);
2362 /* Conjugating a real value is a no-op, but allow it anyway. */
2363 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2364 || typecode == COMPLEX_TYPE))
2366 error ("wrong type argument to conjugation");
2367 return error_mark_node;
2369 else if (!noconvert)
2370 arg = default_conversion (arg);
2373 case TRUTH_NOT_EXPR:
2374 if (typecode != INTEGER_TYPE
2375 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2376 && typecode != COMPLEX_TYPE
2377 /* These will convert to a pointer. */
2378 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2380 error ("wrong type argument to unary exclamation mark");
2381 return error_mark_node;
2383 arg = lang_hooks.truthvalue_conversion (arg);
2384 return invert_truthvalue (arg);
2390 if (TREE_CODE (arg) == COMPLEX_CST)
2391 return TREE_REALPART (arg);
2392 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2393 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2398 if (TREE_CODE (arg) == COMPLEX_CST)
2399 return TREE_IMAGPART (arg);
2400 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2401 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2403 return convert (TREE_TYPE (arg), integer_zero_node);
2405 case PREINCREMENT_EXPR:
2406 case POSTINCREMENT_EXPR:
2407 case PREDECREMENT_EXPR:
2408 case POSTDECREMENT_EXPR:
2410 /* Increment or decrement the real part of the value,
2411 and don't change the imaginary part. */
2412 if (typecode == COMPLEX_TYPE)
2417 pedwarn ("ISO C does not support `++' and `--' on complex types");
2419 arg = stabilize_reference (arg);
2420 real = build_unary_op (REALPART_EXPR, arg, 1);
2421 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2422 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2423 build_unary_op (code, real, 1), imag);
2426 /* Report invalid types. */
2428 if (typecode != POINTER_TYPE
2429 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2431 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2432 error ("wrong type argument to increment");
2434 error ("wrong type argument to decrement");
2436 return error_mark_node;
2441 tree result_type = TREE_TYPE (arg);
2443 arg = get_unwidened (arg, 0);
2444 argtype = TREE_TYPE (arg);
2446 /* Compute the increment. */
2448 if (typecode == POINTER_TYPE)
2450 /* If pointer target is an undefined struct,
2451 we just cannot know how to do the arithmetic. */
2452 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2454 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2455 error ("increment of pointer to unknown structure");
2457 error ("decrement of pointer to unknown structure");
2459 else if ((pedantic || warn_pointer_arith)
2460 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2461 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2463 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2464 pedwarn ("wrong type argument to increment");
2466 pedwarn ("wrong type argument to decrement");
2469 inc = c_size_in_bytes (TREE_TYPE (result_type));
2472 inc = integer_one_node;
2474 inc = convert (argtype, inc);
2476 /* Complain about anything else that is not a true lvalue. */
2477 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2478 || code == POSTINCREMENT_EXPR)
2479 ? "invalid lvalue in increment"
2480 : "invalid lvalue in decrement")))
2481 return error_mark_node;
2483 /* Report a read-only lvalue. */
2484 if (TREE_READONLY (arg))
2485 readonly_error (arg,
2486 ((code == PREINCREMENT_EXPR
2487 || code == POSTINCREMENT_EXPR)
2488 ? "increment" : "decrement"));
2490 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2491 val = boolean_increment (code, arg);
2493 val = build (code, TREE_TYPE (arg), arg, inc);
2494 TREE_SIDE_EFFECTS (val) = 1;
2495 val = convert (result_type, val);
2496 if (TREE_CODE (val) != code)
2497 TREE_NO_WARNING (val) = 1;
2502 /* Note that this operation never does default_conversion. */
2504 /* Let &* cancel out to simplify resulting code. */
2505 if (TREE_CODE (arg) == INDIRECT_REF)
2507 /* Don't let this be an lvalue. */
2508 if (lvalue_p (TREE_OPERAND (arg, 0)))
2509 return non_lvalue (TREE_OPERAND (arg, 0));
2510 return TREE_OPERAND (arg, 0);
2513 /* For &x[y], return x+y */
2514 if (TREE_CODE (arg) == ARRAY_REF)
2516 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2517 return error_mark_node;
2518 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2519 TREE_OPERAND (arg, 1), 1);
2522 /* Anything not already handled and not a true memory reference
2523 or a non-lvalue array is an error. */
2524 else if (typecode != FUNCTION_TYPE && !flag
2525 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2526 return error_mark_node;
2528 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2529 argtype = TREE_TYPE (arg);
2531 /* If the lvalue is const or volatile, merge that into the type
2532 to which the address will point. Note that you can't get a
2533 restricted pointer by taking the address of something, so we
2534 only have to deal with `const' and `volatile' here. */
2535 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2536 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2537 argtype = c_build_type_variant (argtype,
2538 TREE_READONLY (arg),
2539 TREE_THIS_VOLATILE (arg));
2541 argtype = build_pointer_type (argtype);
2543 if (!c_mark_addressable (arg))
2544 return error_mark_node;
2549 if (TREE_CODE (arg) == COMPONENT_REF)
2551 tree field = TREE_OPERAND (arg, 1);
2553 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2555 if (DECL_C_BIT_FIELD (field))
2557 error ("attempt to take address of bit-field structure member `%s'",
2558 IDENTIFIER_POINTER (DECL_NAME (field)));
2559 return error_mark_node;
2562 addr = fold (build (PLUS_EXPR, argtype,
2563 convert (argtype, addr),
2564 convert (argtype, byte_position (field))));
2567 addr = build1 (code, argtype, arg);
2569 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2570 TREE_INVARIANT (addr) = TREE_CONSTANT (addr) = 1;
2580 argtype = TREE_TYPE (arg);
2581 val = build1 (code, argtype, arg);
2582 return require_constant_value ? fold_initializer (val) : fold (val);
2585 /* Return nonzero if REF is an lvalue valid for this language.
2586 Lvalues can be assigned, unless their type has TYPE_READONLY.
2587 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2592 enum tree_code code = TREE_CODE (ref);
2599 return lvalue_p (TREE_OPERAND (ref, 0));
2601 case COMPOUND_LITERAL_EXPR:
2611 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2612 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2615 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2622 /* Return nonzero if REF is an lvalue valid for this language;
2623 otherwise, print an error message and return zero. */
2626 lvalue_or_else (tree ref, const char *msgid)
2628 int win = lvalue_p (ref);
2631 error ("%s", msgid);
2637 /* Warn about storing in something that is `const'. */
2640 readonly_error (tree arg, const char *msgid)
2642 if (TREE_CODE (arg) == COMPONENT_REF)
2644 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2645 readonly_error (TREE_OPERAND (arg, 0), msgid);
2647 error ("%s of read-only member `%s'", _(msgid),
2648 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2650 else if (TREE_CODE (arg) == VAR_DECL)
2651 error ("%s of read-only variable `%s'", _(msgid),
2652 IDENTIFIER_POINTER (DECL_NAME (arg)));
2654 error ("%s of read-only location", _(msgid));
2657 /* Mark EXP saying that we need to be able to take the
2658 address of it; it should not be allocated in a register.
2659 Returns true if successful. */
2662 c_mark_addressable (tree exp)
2667 switch (TREE_CODE (x))
2670 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2672 error ("cannot take address of bit-field `%s'",
2673 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2677 /* ... fall through ... */
2683 x = TREE_OPERAND (x, 0);
2686 case COMPOUND_LITERAL_EXPR:
2688 TREE_ADDRESSABLE (x) = 1;
2695 if (C_DECL_REGISTER (x)
2696 && DECL_NONLOCAL (x))
2698 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2700 error ("global register variable `%s' used in nested function",
2701 IDENTIFIER_POINTER (DECL_NAME (x)));
2704 pedwarn ("register variable `%s' used in nested function",
2705 IDENTIFIER_POINTER (DECL_NAME (x)));
2707 else if (C_DECL_REGISTER (x))
2709 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2711 error ("address of global register variable `%s' requested",
2712 IDENTIFIER_POINTER (DECL_NAME (x)));
2716 pedwarn ("address of register variable `%s' requested",
2717 IDENTIFIER_POINTER (DECL_NAME (x)));
2722 TREE_ADDRESSABLE (x) = 1;
2729 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2732 build_conditional_expr (tree ifexp, tree op1, tree op2)
2736 enum tree_code code1;
2737 enum tree_code code2;
2738 tree result_type = NULL;
2739 tree orig_op1 = op1, orig_op2 = op2;
2741 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2743 /* Promote both alternatives. */
2745 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2746 op1 = default_conversion (op1);
2747 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2748 op2 = default_conversion (op2);
2750 if (TREE_CODE (ifexp) == ERROR_MARK
2751 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2752 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2753 return error_mark_node;
2755 type1 = TREE_TYPE (op1);
2756 code1 = TREE_CODE (type1);
2757 type2 = TREE_TYPE (op2);
2758 code2 = TREE_CODE (type2);
2760 /* C90 does not permit non-lvalue arrays in conditional expressions.
2761 In C99 they will be pointers by now. */
2762 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2764 error ("non-lvalue array in conditional expression");
2765 return error_mark_node;
2768 /* Quickly detect the usual case where op1 and op2 have the same type
2770 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2773 result_type = type1;
2775 result_type = TYPE_MAIN_VARIANT (type1);
2777 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2778 || code1 == COMPLEX_TYPE)
2779 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2780 || code2 == COMPLEX_TYPE))
2782 result_type = common_type (type1, type2);
2784 /* If -Wsign-compare, warn here if type1 and type2 have
2785 different signedness. We'll promote the signed to unsigned
2786 and later code won't know it used to be different.
2787 Do this check on the original types, so that explicit casts
2788 will be considered, but default promotions won't. */
2789 if (warn_sign_compare && !skip_evaluation)
2791 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2792 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2794 if (unsigned_op1 ^ unsigned_op2)
2796 /* Do not warn if the result type is signed, since the
2797 signed type will only be chosen if it can represent
2798 all the values of the unsigned type. */
2799 if (! TYPE_UNSIGNED (result_type))
2801 /* Do not warn if the signed quantity is an unsuffixed
2802 integer literal (or some static constant expression
2803 involving such literals) and it is non-negative. */
2804 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2805 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2808 warning ("signed and unsigned type in conditional expression");
2812 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2814 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2815 pedwarn ("ISO C forbids conditional expr with only one void side");
2816 result_type = void_type_node;
2818 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2820 if (comp_target_types (type1, type2, 1))
2821 result_type = common_pointer_type (type1, type2);
2822 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2823 && TREE_CODE (orig_op1) != NOP_EXPR)
2824 result_type = qualify_type (type2, type1);
2825 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2826 && TREE_CODE (orig_op2) != NOP_EXPR)
2827 result_type = qualify_type (type1, type2);
2828 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2830 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2831 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2832 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2833 TREE_TYPE (type2)));
2835 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2837 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2838 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2839 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2840 TREE_TYPE (type1)));
2844 pedwarn ("pointer type mismatch in conditional expression");
2845 result_type = build_pointer_type (void_type_node);
2848 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2850 if (! integer_zerop (op2))
2851 pedwarn ("pointer/integer type mismatch in conditional expression");
2854 op2 = null_pointer_node;
2856 result_type = type1;
2858 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2860 if (!integer_zerop (op1))
2861 pedwarn ("pointer/integer type mismatch in conditional expression");
2864 op1 = null_pointer_node;
2866 result_type = type2;
2871 if (flag_cond_mismatch)
2872 result_type = void_type_node;
2875 error ("type mismatch in conditional expression");
2876 return error_mark_node;
2880 /* Merge const and volatile flags of the incoming types. */
2882 = build_type_variant (result_type,
2883 TREE_READONLY (op1) || TREE_READONLY (op2),
2884 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2886 if (result_type != TREE_TYPE (op1))
2887 op1 = convert_and_check (result_type, op1);
2888 if (result_type != TREE_TYPE (op2))
2889 op2 = convert_and_check (result_type, op2);
2891 if (TREE_CODE (ifexp) == INTEGER_CST)
2892 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2894 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2897 /* Given a list of expressions, return a compound expression
2898 that performs them all and returns the value of the last of them. */
2901 build_compound_expr (tree list)
2903 return internal_build_compound_expr (list, TRUE);
2907 internal_build_compound_expr (tree list, int first_p)
2911 if (TREE_CHAIN (list) == 0)
2913 /* Convert arrays and functions to pointers when there
2914 really is a comma operator. */
2917 = default_function_array_conversion (TREE_VALUE (list));
2919 /* Don't let (0, 0) be null pointer constant. */
2920 if (!first_p && integer_zerop (TREE_VALUE (list)))
2921 return non_lvalue (TREE_VALUE (list));
2922 return TREE_VALUE (list);
2925 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2927 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2929 /* The left-hand operand of a comma expression is like an expression
2930 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2931 any side-effects, unless it was explicitly cast to (void). */
2932 if (warn_unused_value
2933 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2934 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2935 warning ("left-hand operand of comma expression has no effect");
2938 /* With -Wunused, we should also warn if the left-hand operand does have
2939 side-effects, but computes a value which is not used. For example, in
2940 `foo() + bar(), baz()' the result of the `+' operator is not used,
2941 so we should issue a warning. */
2942 else if (warn_unused_value)
2943 warn_if_unused_value (TREE_VALUE (list), input_location);
2945 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2948 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2951 build_c_cast (tree type, tree expr)
2955 if (type == error_mark_node || expr == error_mark_node)
2956 return error_mark_node;
2958 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2959 only in <protocol> qualifications. But when constructing cast expressions,
2960 the protocols do matter and must be kept around. */
2961 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2962 type = TYPE_MAIN_VARIANT (type);
2964 if (TREE_CODE (type) == ARRAY_TYPE)
2966 error ("cast specifies array type");
2967 return error_mark_node;
2970 if (TREE_CODE (type) == FUNCTION_TYPE)
2972 error ("cast specifies function type");
2973 return error_mark_node;
2976 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2980 if (TREE_CODE (type) == RECORD_TYPE
2981 || TREE_CODE (type) == UNION_TYPE)
2982 pedwarn ("ISO C forbids casting nonscalar to the same type");
2985 else if (TREE_CODE (type) == UNION_TYPE)
2988 value = default_function_array_conversion (value);
2990 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2991 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2992 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3000 pedwarn ("ISO C forbids casts to union type");
3001 t = digest_init (type,
3002 build_constructor (type,
3003 build_tree_list (field, value)),
3005 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3006 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3009 error ("cast to union type from type not present in union");
3010 return error_mark_node;
3016 /* If casting to void, avoid the error that would come
3017 from default_conversion in the case of a non-lvalue array. */
3018 if (type == void_type_node)
3019 return build1 (CONVERT_EXPR, type, value);
3021 /* Convert functions and arrays to pointers,
3022 but don't convert any other types. */
3023 value = default_function_array_conversion (value);
3024 otype = TREE_TYPE (value);
3026 /* Optionally warn about potentially worrisome casts. */
3029 && TREE_CODE (type) == POINTER_TYPE
3030 && TREE_CODE (otype) == POINTER_TYPE)
3032 tree in_type = type;
3033 tree in_otype = otype;
3037 /* Check that the qualifiers on IN_TYPE are a superset of
3038 the qualifiers of IN_OTYPE. The outermost level of
3039 POINTER_TYPE nodes is uninteresting and we stop as soon
3040 as we hit a non-POINTER_TYPE node on either type. */
3043 in_otype = TREE_TYPE (in_otype);
3044 in_type = TREE_TYPE (in_type);
3046 /* GNU C allows cv-qualified function types. 'const'
3047 means the function is very pure, 'volatile' means it
3048 can't return. We need to warn when such qualifiers
3049 are added, not when they're taken away. */
3050 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3051 && TREE_CODE (in_type) == FUNCTION_TYPE)
3052 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3054 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3056 while (TREE_CODE (in_type) == POINTER_TYPE
3057 && TREE_CODE (in_otype) == POINTER_TYPE);
3060 warning ("cast adds new qualifiers to function type");
3063 /* There are qualifiers present in IN_OTYPE that are not
3064 present in IN_TYPE. */
3065 warning ("cast discards qualifiers from pointer target type");
3068 /* Warn about possible alignment problems. */
3069 if (STRICT_ALIGNMENT && warn_cast_align
3070 && TREE_CODE (type) == POINTER_TYPE
3071 && TREE_CODE (otype) == POINTER_TYPE
3072 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3073 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3074 /* Don't warn about opaque types, where the actual alignment
3075 restriction is unknown. */
3076 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3077 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3078 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3079 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3080 warning ("cast increases required alignment of target type");
3082 if (TREE_CODE (type) == INTEGER_TYPE
3083 && TREE_CODE (otype) == POINTER_TYPE
3084 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3085 && !TREE_CONSTANT (value))
3086 warning ("cast from pointer to integer of different size");
3088 if (warn_bad_function_cast
3089 && TREE_CODE (value) == CALL_EXPR
3090 && TREE_CODE (type) != TREE_CODE (otype))
3091 warning ("cast does not match function type");
3093 if (TREE_CODE (type) == POINTER_TYPE
3094 && TREE_CODE (otype) == INTEGER_TYPE
3095 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3096 /* Don't warn about converting any constant. */
3097 && !TREE_CONSTANT (value))
3098 warning ("cast to pointer from integer of different size");
3100 if (TREE_CODE (type) == POINTER_TYPE
3101 && TREE_CODE (otype) == POINTER_TYPE
3102 && TREE_CODE (expr) == ADDR_EXPR
3103 && DECL_P (TREE_OPERAND (expr, 0))
3104 && flag_strict_aliasing && warn_strict_aliasing
3105 && !VOID_TYPE_P (TREE_TYPE (type)))
3107 /* Casting the address of a decl to non void pointer. Warn
3108 if the cast breaks type based aliasing. */
3109 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3110 warning ("type-punning to incomplete type might break strict-aliasing rules");
3113 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3114 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3116 if (!alias_sets_conflict_p (set1, set2))
3117 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3118 else if (warn_strict_aliasing > 1
3119 && !alias_sets_might_conflict_p (set1, set2))
3120 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3124 /* If pedantic, warn for conversions between function and object
3125 pointer types, except for converting a null pointer constant
3126 to function pointer type. */
3128 && TREE_CODE (type) == POINTER_TYPE
3129 && TREE_CODE (otype) == POINTER_TYPE
3130 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3131 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3132 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3135 && TREE_CODE (type) == POINTER_TYPE
3136 && TREE_CODE (otype) == POINTER_TYPE
3137 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3138 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3139 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3140 && TREE_CODE (expr) != NOP_EXPR))
3141 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3144 /* Replace a nonvolatile const static variable with its value. */
3145 if (optimize && TREE_CODE (value) == VAR_DECL)
3146 value = decl_constant_value (value);
3147 value = convert (type, value);
3149 /* Ignore any integer overflow caused by the cast. */
3150 if (TREE_CODE (value) == INTEGER_CST)
3152 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3154 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3155 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3159 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3160 if (TREE_CODE (value) == INTEGER_CST
3161 && TREE_CODE (expr) == INTEGER_CST
3162 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3163 value = non_lvalue (value);
3165 /* Don't let a cast be an lvalue. */
3167 value = non_lvalue (value);
3172 /* Interpret a cast of expression EXPR to type TYPE. */
3174 c_cast_expr (tree type, tree expr)
3176 int saved_wsp = warn_strict_prototypes;
3178 /* This avoids warnings about unprototyped casts on
3179 integers. E.g. "#define SIG_DFL (void(*)())0". */
3180 if (TREE_CODE (expr) == INTEGER_CST)
3181 warn_strict_prototypes = 0;
3182 type = groktypename (type);
3183 warn_strict_prototypes = saved_wsp;
3185 return build_c_cast (type, expr);
3189 /* Build an assignment expression of lvalue LHS from value RHS.
3190 MODIFYCODE is the code for a binary operator that we use
3191 to combine the old value of LHS with RHS to get the new value.
3192 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3195 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3199 tree lhstype = TREE_TYPE (lhs);
3200 tree olhstype = lhstype;
3202 /* Types that aren't fully specified cannot be used in assignments. */
3203 lhs = require_complete_type (lhs);
3205 /* Avoid duplicate error messages from operands that had errors. */
3206 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3207 return error_mark_node;
3209 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3210 /* Do not use STRIP_NOPS here. We do not want an enumerator
3211 whose value is 0 to count as a null pointer constant. */
3212 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3213 rhs = TREE_OPERAND (rhs, 0);
3217 /* If a binary op has been requested, combine the old LHS value with the RHS
3218 producing the value we should actually store into the LHS. */
3220 if (modifycode != NOP_EXPR)
3222 lhs = stabilize_reference (lhs);
3223 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3226 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3227 return error_mark_node;
3229 /* Warn about storing in something that is `const'. */
3231 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3232 || ((TREE_CODE (lhstype) == RECORD_TYPE
3233 || TREE_CODE (lhstype) == UNION_TYPE)
3234 && C_TYPE_FIELDS_READONLY (lhstype)))
3235 readonly_error (lhs, "assignment");
3237 /* If storing into a structure or union member,
3238 it has probably been given type `int'.
3239 Compute the type that would go with
3240 the actual amount of storage the member occupies. */
3242 if (TREE_CODE (lhs) == COMPONENT_REF
3243 && (TREE_CODE (lhstype) == INTEGER_TYPE
3244 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3245 || TREE_CODE (lhstype) == REAL_TYPE
3246 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3247 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3249 /* If storing in a field that is in actuality a short or narrower than one,
3250 we must store in the field in its actual type. */
3252 if (lhstype != TREE_TYPE (lhs))
3254 lhs = copy_node (lhs);
3255 TREE_TYPE (lhs) = lhstype;
3258 /* Convert new value to destination type. */
3260 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3261 NULL_TREE, NULL_TREE, 0);
3262 if (TREE_CODE (newrhs) == ERROR_MARK)
3263 return error_mark_node;
3267 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3268 TREE_SIDE_EFFECTS (result) = 1;
3270 /* If we got the LHS in a different type for storing in,
3271 convert the result back to the nominal type of LHS
3272 so that the value we return always has the same type
3273 as the LHS argument. */
3275 if (olhstype == TREE_TYPE (result))
3277 return convert_for_assignment (olhstype, result, _("assignment"),
3278 NULL_TREE, NULL_TREE, 0);
3281 /* Convert value RHS to type TYPE as preparation for an assignment
3282 to an lvalue of type TYPE.
3283 The real work of conversion is done by `convert'.
3284 The purpose of this function is to generate error messages
3285 for assignments that are not allowed in C.
3286 ERRTYPE is a string to use in error messages:
3287 "assignment", "return", etc. If it is null, this is parameter passing
3288 for a function call (and different error messages are output).
3290 FUNNAME is the name of the function being called,
3291 as an IDENTIFIER_NODE, or null.
3292 PARMNUM is the number of the argument, for printing in error messages. */
3295 convert_for_assignment (tree type, tree rhs, const char *errtype,
3296 tree fundecl, tree funname, int parmnum)
3298 enum tree_code codel = TREE_CODE (type);
3300 enum tree_code coder;
3302 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3303 /* Do not use STRIP_NOPS here. We do not want an enumerator
3304 whose value is 0 to count as a null pointer constant. */
3305 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3306 rhs = TREE_OPERAND (rhs, 0);
3308 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3309 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3310 rhs = default_conversion (rhs);
3311 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3312 rhs = decl_constant_value_for_broken_optimization (rhs);
3314 rhstype = TREE_TYPE (rhs);
3315 coder = TREE_CODE (rhstype);
3317 if (coder == ERROR_MARK)
3318 return error_mark_node;
3320 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3322 overflow_warning (rhs);
3323 /* Check for Objective-C protocols. This will automatically
3324 issue a warning if there are protocol violations. No need to
3325 use the return value. */
3326 if (c_dialect_objc ())
3327 objc_comptypes (type, rhstype, 0);
3331 if (coder == VOID_TYPE)
3333 error ("void value not ignored as it ought to be");
3334 return error_mark_node;
3336 /* A type converts to a reference to it.
3337 This code doesn't fully support references, it's just for the
3338 special case of va_start and va_copy. */
3339 if (codel == REFERENCE_TYPE
3340 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3342 if (!lvalue_p (rhs))
3344 error ("cannot pass rvalue to reference parameter");
3345 return error_mark_node;
3347 if (!c_mark_addressable (rhs))
3348 return error_mark_node;
3349 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3351 /* We already know that these two types are compatible, but they
3352 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3353 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3354 likely to be va_list, a typedef to __builtin_va_list, which
3355 is different enough that it will cause problems later. */
3356 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3357 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3359 rhs = build1 (NOP_EXPR, type, rhs);
3362 /* Some types can interconvert without explicit casts. */
3363 else if (codel == VECTOR_TYPE
3364 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3365 return convert (type, rhs);
3366 /* Arithmetic types all interconvert, and enum is treated like int. */
3367 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3368 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3369 || codel == BOOLEAN_TYPE)
3370 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3371 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3372 || coder == BOOLEAN_TYPE))
3373 return convert_and_check (type, rhs);
3375 /* Conversion to a transparent union from its member types.
3376 This applies only to function arguments. */
3377 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3380 tree marginal_memb_type = 0;
3382 for (memb_types = TYPE_FIELDS (type); memb_types;
3383 memb_types = TREE_CHAIN (memb_types))
3385 tree memb_type = TREE_TYPE (memb_types);
3387 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3388 TYPE_MAIN_VARIANT (rhstype)))
3391 if (TREE_CODE (memb_type) != POINTER_TYPE)
3394 if (coder == POINTER_TYPE)
3396 tree ttl = TREE_TYPE (memb_type);
3397 tree ttr = TREE_TYPE (rhstype);
3399 /* Any non-function converts to a [const][volatile] void *
3400 and vice versa; otherwise, targets must be the same.
3401 Meanwhile, the lhs target must have all the qualifiers of
3403 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3404 || comp_target_types (memb_type, rhstype, 0))
3406 /* If this type won't generate any warnings, use it. */
3407 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3408 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3409 && TREE_CODE (ttl) == FUNCTION_TYPE)
3410 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3411 == TYPE_QUALS (ttr))
3412 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3413 == TYPE_QUALS (ttl))))
3416 /* Keep looking for a better type, but remember this one. */
3417 if (! marginal_memb_type)
3418 marginal_memb_type = memb_type;
3422 /* Can convert integer zero to any pointer type. */
3423 if (integer_zerop (rhs)
3424 || (TREE_CODE (rhs) == NOP_EXPR
3425 && integer_zerop (TREE_OPERAND (rhs, 0))))
3427 rhs = null_pointer_node;
3432 if (memb_types || marginal_memb_type)
3436 /* We have only a marginally acceptable member type;
3437 it needs a warning. */
3438 tree ttl = TREE_TYPE (marginal_memb_type);
3439 tree ttr = TREE_TYPE (rhstype);
3441 /* Const and volatile mean something different for function
3442 types, so the usual warnings are not appropriate. */
3443 if (TREE_CODE (ttr) == FUNCTION_TYPE
3444 && TREE_CODE (ttl) == FUNCTION_TYPE)
3446 /* Because const and volatile on functions are
3447 restrictions that say the function will not do
3448 certain things, it is okay to use a const or volatile
3449 function where an ordinary one is wanted, but not
3451 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3452 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3453 errtype, funname, parmnum);
3455 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3456 warn_for_assignment ("%s discards qualifiers from pointer target type",
3461 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3462 pedwarn ("ISO C prohibits argument conversion to union type");
3464 return build1 (NOP_EXPR, type, rhs);
3468 /* Conversions among pointers */
3469 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3470 && (coder == codel))
3472 tree ttl = TREE_TYPE (type);
3473 tree ttr = TREE_TYPE (rhstype);
3474 bool is_opaque_pointer;
3475 int target_cmp = 0; /* Cache comp_target_types () result. */
3477 /* Opaque pointers are treated like void pointers. */
3478 is_opaque_pointer = (targetm.vector_opaque_p (type)
3479 || targetm.vector_opaque_p (rhstype))
3480 && TREE_CODE (ttl) == VECTOR_TYPE
3481 && TREE_CODE (ttr) == VECTOR_TYPE;
3483 /* Any non-function converts to a [const][volatile] void *
3484 and vice versa; otherwise, targets must be the same.
3485 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3486 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3487 || (target_cmp = comp_target_types (type, rhstype, 0))
3488 || is_opaque_pointer
3489 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3490 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3493 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3496 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3497 which are not ANSI null ptr constants. */
3498 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3499 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3500 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3501 errtype, funname, parmnum);
3502 /* Const and volatile mean something different for function types,
3503 so the usual warnings are not appropriate. */
3504 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3505 && TREE_CODE (ttl) != FUNCTION_TYPE)
3507 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3508 warn_for_assignment ("%s discards qualifiers from pointer target type",
3509 errtype, funname, parmnum);
3510 /* If this is not a case of ignoring a mismatch in signedness,
3512 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3515 /* If there is a mismatch, do warn. */
3517 warn_for_assignment ("pointer targets in %s differ in signedness",
3518 errtype, funname, parmnum);
3520 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3521 && TREE_CODE (ttr) == FUNCTION_TYPE)
3523 /* Because const and volatile on functions are restrictions
3524 that say the function will not do certain things,
3525 it is okay to use a const or volatile function
3526 where an ordinary one is wanted, but not vice-versa. */
3527 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3528 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3529 errtype, funname, parmnum);
3533 warn_for_assignment ("%s from incompatible pointer type",
3534 errtype, funname, parmnum);
3535 return convert (type, rhs);
3537 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3539 error ("invalid use of non-lvalue array");
3540 return error_mark_node;
3542 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3544 /* An explicit constant 0 can convert to a pointer,
3545 or one that results from arithmetic, even including
3546 a cast to integer type. */
3547 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3549 ! (TREE_CODE (rhs) == NOP_EXPR
3550 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3551 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3552 && integer_zerop (TREE_OPERAND (rhs, 0))))
3553 warn_for_assignment ("%s makes pointer from integer without a cast",
3554 errtype, funname, parmnum);
3556 return convert (type, rhs);
3558 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3560 warn_for_assignment ("%s makes integer from pointer without a cast",
3561 errtype, funname, parmnum);
3562 return convert (type, rhs);
3564 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3565 return convert (type, rhs);
3571 tree selector = objc_message_selector ();
3573 if (selector && parmnum > 2)
3574 error ("incompatible type for argument %d of `%s'",
3575 parmnum - 2, IDENTIFIER_POINTER (selector));
3577 error ("incompatible type for argument %d of `%s'",
3578 parmnum, IDENTIFIER_POINTER (funname));
3581 error ("incompatible type for argument %d of indirect function call",
3585 error ("incompatible types in %s", errtype);
3587 return error_mark_node;
3590 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3591 is used for error and waring reporting and indicates which argument
3592 is being processed. */
3595 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3599 /* If FN was prototyped, the value has been converted already
3600 in convert_arguments. */
3601 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3604 type = TREE_TYPE (parm);
3605 ret = convert_for_assignment (type, value,
3606 (char *) 0 /* arg passing */, fn,
3607 DECL_NAME (fn), argnum);
3608 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3609 && INTEGRAL_TYPE_P (type)
3610 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3611 ret = default_conversion (ret);
3615 /* Print a warning using MSGID.
3616 It gets OPNAME as its one parameter.
3617 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3618 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3619 FUNCTION and ARGNUM are handled specially if we are building an
3620 Objective-C selector. */
3623 warn_for_assignment (const char *msgid, const char *opname, tree function,
3628 tree selector = objc_message_selector ();
3631 if (selector && argnum > 2)
3633 function = selector;
3640 /* Function name is known; supply it. */
3641 const char *const argstring = _("passing arg of `%s'");
3642 new_opname = alloca (IDENTIFIER_LENGTH (function)
3643 + strlen (argstring) + 1 + 1);
3644 sprintf (new_opname, argstring,
3645 IDENTIFIER_POINTER (function));
3649 /* Function name unknown (call through ptr). */
3650 const char *const argnofun = _("passing arg of pointer to function");
3651 new_opname = alloca (strlen (argnofun) + 1 + 1);
3652 sprintf (new_opname, argnofun);
3657 /* Function name is known; supply it. */
3658 const char *const argstring = _("passing arg %d of `%s'");
3659 new_opname = alloca (IDENTIFIER_LENGTH (function)
3660 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3661 sprintf (new_opname, argstring, argnum,
3662 IDENTIFIER_POINTER (function));
3666 /* Function name unknown (call through ptr); just give arg number. */
3667 const char *const argnofun = _("passing arg %d of pointer to function");
3668 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3669 sprintf (new_opname, argnofun, argnum);
3671 opname = new_opname;
3673 pedwarn (msgid, opname);
3676 /* If VALUE is a compound expr all of whose expressions are constant, then
3677 return its value. Otherwise, return error_mark_node.
3679 This is for handling COMPOUND_EXPRs as initializer elements
3680 which is allowed with a warning when -pedantic is specified. */
3683 valid_compound_expr_initializer (tree value, tree endtype)
3685 if (TREE_CODE (value) == COMPOUND_EXPR)
3687 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3689 return error_mark_node;
3690 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3693 else if (! TREE_CONSTANT (value)
3694 && ! initializer_constant_valid_p (value, endtype))
3695 return error_mark_node;
3700 /* Perform appropriate conversions on the initial value of a variable,
3701 store it in the declaration DECL,
3702 and print any error messages that are appropriate.
3703 If the init is invalid, store an ERROR_MARK. */
3706 store_init_value (tree decl, tree init)
3710 /* If variable's type was invalidly declared, just ignore it. */
3712 type = TREE_TYPE (decl);
3713 if (TREE_CODE (type) == ERROR_MARK)
3716 /* Digest the specified initializer into an expression. */
3718 value = digest_init (type, init, TREE_STATIC (decl));
3720 /* Store the expression if valid; else report error. */
3722 if (warn_traditional && !in_system_header
3723 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3724 warning ("traditional C rejects automatic aggregate initialization");
3726 DECL_INITIAL (decl) = value;
3728 /* ANSI wants warnings about out-of-range constant initializers. */
3729 STRIP_TYPE_NOPS (value);
3730 constant_expression_warning (value);
3732 /* Check if we need to set array size from compound literal size. */
3733 if (TREE_CODE (type) == ARRAY_TYPE
3734 && TYPE_DOMAIN (type) == 0
3735 && value != error_mark_node)
3737 tree inside_init = init;
3739 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3740 inside_init = TREE_OPERAND (init, 0);
3741 inside_init = fold (inside_init);
3743 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3745 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3747 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3749 /* For int foo[] = (int [3]){1}; we need to set array size
3750 now since later on array initializer will be just the
3751 brace enclosed list of the compound literal. */
3752 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3754 layout_decl (decl, 0);
3760 /* Methods for storing and printing names for error messages. */
3762 /* Implement a spelling stack that allows components of a name to be pushed
3763 and popped. Each element on the stack is this structure. */
3775 #define SPELLING_STRING 1
3776 #define SPELLING_MEMBER 2
3777 #define SPELLING_BOUNDS 3
3779 static struct spelling *spelling; /* Next stack element (unused). */
3780 static struct spelling *spelling_base; /* Spelling stack base. */
3781 static int spelling_size; /* Size of the spelling stack. */
3783 /* Macros to save and restore the spelling stack around push_... functions.
3784 Alternative to SAVE_SPELLING_STACK. */
3786 #define SPELLING_DEPTH() (spelling - spelling_base)
3787 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3789 /* Push an element on the spelling stack with type KIND and assign VALUE
3792 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3794 int depth = SPELLING_DEPTH (); \
3796 if (depth >= spelling_size) \
3798 spelling_size += 10; \
3799 if (spelling_base == 0) \
3800 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3802 spelling_base = xrealloc (spelling_base, \
3803 spelling_size * sizeof (struct spelling)); \
3804 RESTORE_SPELLING_DEPTH (depth); \
3807 spelling->kind = (KIND); \
3808 spelling->MEMBER = (VALUE); \
3812 /* Push STRING on the stack. Printed literally. */
3815 push_string (const char *string)
3817 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3820 /* Push a member name on the stack. Printed as '.' STRING. */
3823 push_member_name (tree decl)
3825 const char *const string
3826 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3827 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3830 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3833 push_array_bounds (int bounds)
3835 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3838 /* Compute the maximum size in bytes of the printed spelling. */
3841 spelling_length (void)
3846 for (p = spelling_base; p < spelling; p++)
3848 if (p->kind == SPELLING_BOUNDS)
3851 size += strlen (p->u.s) + 1;
3857 /* Print the spelling to BUFFER and return it. */
3860 print_spelling (char *buffer)
3865 for (p = spelling_base; p < spelling; p++)
3866 if (p->kind == SPELLING_BOUNDS)
3868 sprintf (d, "[%d]", p->u.i);
3874 if (p->kind == SPELLING_MEMBER)
3876 for (s = p->u.s; (*d = *s++); d++)
3883 /* Issue an error message for a bad initializer component.
3884 MSGID identifies the message.
3885 The component name is taken from the spelling stack. */
3888 error_init (const char *msgid)
3892 error ("%s", _(msgid));
3893 ofwhat = print_spelling (alloca (spelling_length () + 1));
3895 error ("(near initialization for `%s')", ofwhat);
3898 /* Issue a pedantic warning for a bad initializer component.
3899 MSGID identifies the message.
3900 The component name is taken from the spelling stack. */
3903 pedwarn_init (const char *msgid)
3907 pedwarn ("%s", _(msgid));
3908 ofwhat = print_spelling (alloca (spelling_length () + 1));
3910 pedwarn ("(near initialization for `%s')", ofwhat);
3913 /* Issue a warning for a bad initializer component.
3914 MSGID identifies the message.
3915 The component name is taken from the spelling stack. */
3918 warning_init (const char *msgid)
3922 warning ("%s", _(msgid));
3923 ofwhat = print_spelling (alloca (spelling_length () + 1));
3925 warning ("(near initialization for `%s')", ofwhat);
3928 /* Digest the parser output INIT as an initializer for type TYPE.
3929 Return a C expression of type TYPE to represent the initial value.
3931 REQUIRE_CONSTANT requests an error if non-constant initializers or
3932 elements are seen. */
3935 digest_init (tree type, tree init, int require_constant)
3937 enum tree_code code = TREE_CODE (type);
3938 tree inside_init = init;
3940 if (type == error_mark_node
3941 || init == error_mark_node
3942 || TREE_TYPE (init) == error_mark_node)
3943 return error_mark_node;
3945 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3946 /* Do not use STRIP_NOPS here. We do not want an enumerator
3947 whose value is 0 to count as a null pointer constant. */
3948 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3949 inside_init = TREE_OPERAND (init, 0);
3951 inside_init = fold (inside_init);
3953 /* Initialization of an array of chars from a string constant
3954 optionally enclosed in braces. */
3956 if (code == ARRAY_TYPE)
3958 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3959 if ((typ1 == char_type_node
3960 || typ1 == signed_char_type_node
3961 || typ1 == unsigned_char_type_node
3962 || typ1 == unsigned_wchar_type_node
3963 || typ1 == signed_wchar_type_node)
3964 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3966 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3967 TYPE_MAIN_VARIANT (type)))
3970 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3972 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3974 error_init ("char-array initialized from wide string");
3975 return error_mark_node;
3977 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3979 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3981 error_init ("int-array initialized from non-wide string");
3982 return error_mark_node;
3985 TREE_TYPE (inside_init) = type;
3986 if (TYPE_DOMAIN (type) != 0
3987 && TYPE_SIZE (type) != 0
3988 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3989 /* Subtract 1 (or sizeof (wchar_t))
3990 because it's ok to ignore the terminating null char
3991 that is counted in the length of the constant. */
3992 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3993 TREE_STRING_LENGTH (inside_init)
3994 - ((TYPE_PRECISION (typ1)
3995 != TYPE_PRECISION (char_type_node))
3996 ? (TYPE_PRECISION (wchar_type_node)
3999 pedwarn_init ("initializer-string for array of chars is too long");