1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
34 #include "coretypes.h"
38 #include "langhooks.h"
49 /* Nonzero if we've already printed a "missing braces around initializer"
50 message within this initializer. */
51 static int missing_braces_mentioned;
53 static int require_constant_value;
54 static int require_constant_elements;
56 static tree qualify_type (tree, tree);
57 static int tagged_types_tu_compatible_p (tree, tree, int);
58 static int comp_target_types (tree, tree, int);
59 static int function_types_compatible_p (tree, tree, int);
60 static int type_lists_compatible_p (tree, tree, int);
61 static tree decl_constant_value_for_broken_optimization (tree);
62 static tree default_function_array_conversion (tree);
63 static tree lookup_field (tree, tree);
64 static tree convert_arguments (tree, tree, tree, tree);
65 static tree pointer_diff (tree, tree);
66 static tree internal_build_compound_expr (tree, int);
67 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
69 static void warn_for_assignment (const char *, const char *, tree, int);
70 static tree valid_compound_expr_initializer (tree, tree);
71 static void push_string (const char *);
72 static void push_member_name (tree);
73 static void push_array_bounds (int);
74 static int spelling_length (void);
75 static char *print_spelling (char *);
76 static void warning_init (const char *);
77 static tree digest_init (tree, tree, int);
78 static void output_init_element (tree, tree, tree, int);
79 static void output_pending_init_elements (int);
80 static int set_designator (int);
81 static void push_range_stack (tree);
82 static void add_pending_init (tree, tree);
83 static void set_nonincremental_init (void);
84 static void set_nonincremental_init_from_string (tree);
85 static tree find_init_member (tree);
86 static int lvalue_or_else (tree, const char *);
88 /* Do `exp = require_complete_type (exp);' to make sure exp
89 does not have an incomplete type. (That includes void types.) */
92 require_complete_type (tree value)
94 tree type = TREE_TYPE (value);
96 if (value == error_mark_node || type == error_mark_node)
97 return error_mark_node;
99 /* First, detect a valid value with a complete type. */
100 if (COMPLETE_TYPE_P (type))
103 c_incomplete_type_error (value, type);
104 return error_mark_node;
107 /* Print an error message for invalid use of an incomplete type.
108 VALUE is the expression that was used (or 0 if that isn't known)
109 and TYPE is the type that was invalid. */
112 c_incomplete_type_error (tree value, tree type)
114 const char *type_code_string;
116 /* Avoid duplicate error message. */
117 if (TREE_CODE (type) == ERROR_MARK)
120 if (value != 0 && (TREE_CODE (value) == VAR_DECL
121 || TREE_CODE (value) == PARM_DECL))
122 error ("`%s' has an incomplete type",
123 IDENTIFIER_POINTER (DECL_NAME (value)));
127 /* We must print an error message. Be clever about what it says. */
129 switch (TREE_CODE (type))
132 type_code_string = "struct";
136 type_code_string = "union";
140 type_code_string = "enum";
144 error ("invalid use of void expression");
148 if (TYPE_DOMAIN (type))
150 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
152 error ("invalid use of flexible array member");
155 type = TREE_TYPE (type);
158 error ("invalid use of array with unspecified bounds");
165 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
166 error ("invalid use of undefined type `%s %s'",
167 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
169 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
170 error ("invalid use of incomplete typedef `%s'",
171 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
175 /* Given a type, apply default promotions wrt unnamed function
176 arguments and return the new type. */
179 c_type_promotes_to (tree type)
181 if (TYPE_MAIN_VARIANT (type) == float_type_node)
182 return double_type_node;
184 if (c_promoting_integer_type_p (type))
186 /* Preserve unsignedness if not really getting any wider. */
187 if (TYPE_UNSIGNED (type)
188 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
189 return unsigned_type_node;
190 return integer_type_node;
196 /* Return a variant of TYPE which has all the type qualifiers of LIKE
197 as well as those of TYPE. */
200 qualify_type (tree type, tree like)
202 return c_build_qualified_type (type,
203 TYPE_QUALS (type) | TYPE_QUALS (like));
206 /* Return the composite type of two compatible types.
208 We assume that comptypes has already been done and returned
209 nonzero; if that isn't so, this may crash. In particular, we
210 assume that qualifiers match. */
213 composite_type (tree t1, tree t2)
215 enum tree_code code1;
216 enum tree_code code2;
219 /* Save time if the two types are the same. */
221 if (t1 == t2) return t1;
223 /* If one type is nonsense, use the other. */
224 if (t1 == error_mark_node)
226 if (t2 == error_mark_node)
229 code1 = TREE_CODE (t1);
230 code2 = TREE_CODE (t2);
232 /* Merge the attributes. */
233 attributes = targetm.merge_type_attributes (t1, t2);
235 /* If one is an enumerated type and the other is the compatible
236 integer type, the composite type might be either of the two
237 (DR#013 question 3). For consistency, use the enumerated type as
238 the composite type. */
240 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
242 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
251 /* For two pointers, do this recursively on the target type. */
253 tree pointed_to_1 = TREE_TYPE (t1);
254 tree pointed_to_2 = TREE_TYPE (t2);
255 tree target = composite_type (pointed_to_1, pointed_to_2);
256 t1 = build_pointer_type (target);
257 return build_type_attribute_variant (t1, attributes);
262 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
263 /* Save space: see if the result is identical to one of the args. */
264 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
265 return build_type_attribute_variant (t1, attributes);
266 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
267 return build_type_attribute_variant (t2, attributes);
268 /* Merge the element types, and have a size if either arg has one. */
269 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
270 return build_type_attribute_variant (t1, attributes);
274 /* Function types: prefer the one that specified arg types.
275 If both do, merge the arg types. Also merge the return types. */
277 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
278 tree p1 = TYPE_ARG_TYPES (t1);
279 tree p2 = TYPE_ARG_TYPES (t2);
284 /* Save space: see if the result is identical to one of the args. */
285 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
286 return build_type_attribute_variant (t1, attributes);
287 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
288 return build_type_attribute_variant (t2, attributes);
290 /* Simple way if one arg fails to specify argument types. */
291 if (TYPE_ARG_TYPES (t1) == 0)
293 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
294 return build_type_attribute_variant (t1, attributes);
296 if (TYPE_ARG_TYPES (t2) == 0)
298 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
299 return build_type_attribute_variant (t1, attributes);
302 /* If both args specify argument types, we must merge the two
303 lists, argument by argument. */
304 /* Tell global_bindings_p to return false so that variable_size
305 doesn't abort on VLAs in parameter types. */
306 c_override_global_bindings_to_false = true;
308 len = list_length (p1);
311 for (i = 0; i < len; i++)
312 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
317 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
319 /* A null type means arg type is not specified.
320 Take whatever the other function type has. */
321 if (TREE_VALUE (p1) == 0)
323 TREE_VALUE (n) = TREE_VALUE (p2);
326 if (TREE_VALUE (p2) == 0)
328 TREE_VALUE (n) = TREE_VALUE (p1);
332 /* Given wait (union {union wait *u; int *i} *)
333 and wait (union wait *),
334 prefer union wait * as type of parm. */
335 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
336 && TREE_VALUE (p1) != TREE_VALUE (p2))
339 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
340 memb; memb = TREE_CHAIN (memb))
341 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
344 TREE_VALUE (n) = TREE_VALUE (p2);
346 pedwarn ("function types not truly compatible in ISO C");
350 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
351 && TREE_VALUE (p2) != TREE_VALUE (p1))
354 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
355 memb; memb = TREE_CHAIN (memb))
356 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
359 TREE_VALUE (n) = TREE_VALUE (p1);
361 pedwarn ("function types not truly compatible in ISO C");
365 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
369 c_override_global_bindings_to_false = false;
370 t1 = build_function_type (valtype, newargs);
371 /* ... falls through ... */
375 return build_type_attribute_variant (t1, attributes);
380 /* Return the type of a conditional expression between pointers to
381 possibly differently qualified versions of compatible types.
383 We assume that comp_target_types has already been done and returned
384 nonzero; if that isn't so, this may crash. */
387 common_pointer_type (tree t1, tree t2)
394 /* Save time if the two types are the same. */
396 if (t1 == t2) return t1;
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
401 if (t2 == error_mark_node)
404 if (TREE_CODE (t1) != POINTER_TYPE || TREE_CODE (t2) != POINTER_TYPE)
407 /* Merge the attributes. */
408 attributes = targetm.merge_type_attributes (t1, t2);
410 /* Find the composite type of the target types, and combine the
411 qualifiers of the two types' targets. */
412 pointed_to_1 = TREE_TYPE (t1);
413 pointed_to_2 = TREE_TYPE (t2);
414 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
415 TYPE_MAIN_VARIANT (pointed_to_2));
416 t1 = build_pointer_type (c_build_qualified_type
418 TYPE_QUALS (pointed_to_1) |
419 TYPE_QUALS (pointed_to_2)));
420 return build_type_attribute_variant (t1, attributes);
423 /* Return the common type for two arithmetic types under the usual
424 arithmetic conversions. The default conversions have already been
425 applied, and enumerated types converted to their compatible integer
426 types. The resulting type is unqualified and has no attributes.
428 This is the type for the result of most arithmetic operations
429 if the operands have the given two types. */
432 common_type (tree t1, tree t2)
434 enum tree_code code1;
435 enum tree_code code2;
437 /* If one type is nonsense, use the other. */
438 if (t1 == error_mark_node)
440 if (t2 == error_mark_node)
443 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
444 t1 = TYPE_MAIN_VARIANT (t1);
446 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
447 t2 = TYPE_MAIN_VARIANT (t2);
449 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
450 t1 = build_type_attribute_variant (t1, NULL_TREE);
452 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
453 t2 = build_type_attribute_variant (t2, NULL_TREE);
455 /* Save time if the two types are the same. */
457 if (t1 == t2) return t1;
459 code1 = TREE_CODE (t1);
460 code2 = TREE_CODE (t2);
462 if (code1 != VECTOR_TYPE && code1 != COMPLEX_TYPE
463 && code1 != REAL_TYPE && code1 != INTEGER_TYPE)
466 if (code2 != VECTOR_TYPE && code2 != COMPLEX_TYPE
467 && code2 != REAL_TYPE && code2 != INTEGER_TYPE)
470 /* If one type is a vector type, return that type. (How the usual
471 arithmetic conversions apply to the vector types extension is not
472 precisely specified.) */
473 if (code1 == VECTOR_TYPE)
476 if (code2 == VECTOR_TYPE)
479 /* If one type is complex, form the common type of the non-complex
480 components, then make that complex. Use T1 or T2 if it is the
482 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
484 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
485 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
486 tree subtype = common_type (subtype1, subtype2);
488 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
490 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
493 return build_complex_type (subtype);
496 /* If only one is real, use it as the result. */
498 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
501 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
504 /* Both real or both integers; use the one with greater precision. */
506 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
508 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
511 /* Same precision. Prefer long longs to longs to ints when the
512 same precision, following the C99 rules on integer type rank
513 (which are equivalent to the C90 rules for C90 types). */
515 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
516 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
517 return long_long_unsigned_type_node;
519 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
520 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
522 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
523 return long_long_unsigned_type_node;
525 return long_long_integer_type_node;
528 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
529 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
530 return long_unsigned_type_node;
532 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
533 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
535 /* But preserve unsignedness from the other type,
536 since long cannot hold all the values of an unsigned int. */
537 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
538 return long_unsigned_type_node;
540 return long_integer_type_node;
543 /* Likewise, prefer long double to double even if same size. */
544 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
545 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
546 return long_double_type_node;
548 /* Otherwise prefer the unsigned one. */
550 if (TYPE_UNSIGNED (t1))
556 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
557 or various other operations. Return 2 if they are compatible
558 but a warning may be needed if you use them together. */
561 comptypes (tree type1, tree type2, int flags)
567 /* Suppress errors caused by previously reported errors. */
569 if (t1 == t2 || !t1 || !t2
570 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
573 /* If either type is the internal version of sizetype, return the
575 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
576 && TYPE_ORIG_SIZE_TYPE (t1))
577 t1 = TYPE_ORIG_SIZE_TYPE (t1);
579 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
580 && TYPE_ORIG_SIZE_TYPE (t2))
581 t2 = TYPE_ORIG_SIZE_TYPE (t2);
584 /* Enumerated types are compatible with integer types, but this is
585 not transitive: two enumerated types in the same translation unit
586 are compatible with each other only if they are the same type. */
588 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
589 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
590 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
591 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
596 /* Different classes of types can't be compatible. */
598 if (TREE_CODE (t1) != TREE_CODE (t2))
601 /* Qualifiers must match. C99 6.7.3p9 */
603 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
606 /* Allow for two different type nodes which have essentially the same
607 definition. Note that we already checked for equality of the type
608 qualifiers (just above). */
610 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
613 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
614 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
617 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
620 switch (TREE_CODE (t1))
623 /* We must give ObjC the first crack at comparing pointers, since
624 protocol qualifiers may be involved. */
625 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
627 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
628 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
632 val = function_types_compatible_p (t1, t2, flags);
637 tree d1 = TYPE_DOMAIN (t1);
638 tree d2 = TYPE_DOMAIN (t2);
639 bool d1_variable, d2_variable;
640 bool d1_zero, d2_zero;
643 /* Target types must match incl. qualifiers. */
644 if (TREE_TYPE (t1) != TREE_TYPE (t2)
645 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
649 /* Sizes must match unless one is missing or variable. */
650 if (d1 == 0 || d2 == 0 || d1 == d2)
653 d1_zero = ! TYPE_MAX_VALUE (d1);
654 d2_zero = ! TYPE_MAX_VALUE (d2);
656 d1_variable = (! d1_zero
657 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
658 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
659 d2_variable = (! d2_zero
660 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
661 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
663 if (d1_variable || d2_variable)
665 if (d1_zero && d2_zero)
667 if (d1_zero || d2_zero
668 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
669 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
676 /* We are dealing with two distinct structs. In assorted Objective-C
677 corner cases, however, these can still be deemed equivalent. */
678 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
683 if (val != 1 && !same_translation_unit_p (t1, t2))
684 val = tagged_types_tu_compatible_p (t1, t2, flags);
688 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
689 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), 0);
695 return attrval == 2 && val == 1 ? 2 : val;
698 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
699 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
700 to 1 or 0 depending if the check of the pointer types is meant to
701 be reflexive or not (typically, assignments are not reflexive,
702 while comparisons are reflexive).
706 comp_target_types (tree ttl, tree ttr, int reflexive)
710 /* Give objc_comptypes a crack at letting these types through. */
711 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
714 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
715 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
717 if (val == 2 && pedantic)
718 pedwarn ("types are not quite compatible");
722 /* Subroutines of `comptypes'. */
724 /* Determine whether two trees derive from the same translation unit.
725 If the CONTEXT chain ends in a null, that tree's context is still
726 being parsed, so if two trees have context chains ending in null,
727 they're in the same translation unit. */
729 same_translation_unit_p (tree t1, tree t2)
731 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
732 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
734 case 'd': t1 = DECL_CONTEXT (t1); break;
735 case 't': t1 = TYPE_CONTEXT (t1); break;
736 case 'x': t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
740 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
741 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
743 case 'd': t2 = DECL_CONTEXT (t2); break;
744 case 't': t2 = TYPE_CONTEXT (t2); break;
745 case 'x': t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
752 /* The C standard says that two structures in different translation
753 units are compatible with each other only if the types of their
754 fields are compatible (among other things). So, consider two copies
755 of this structure: */
757 struct tagged_tu_seen {
758 const struct tagged_tu_seen * next;
763 /* Can they be compatible with each other? We choose to break the
764 recursion by allowing those types to be compatible. */
766 static const struct tagged_tu_seen * tagged_tu_seen_base;
768 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
769 compatible. If the two types are not the same (which has been
770 checked earlier), this can only happen when multiple translation
771 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
775 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
778 bool needs_warning = false;
780 /* We have to verify that the tags of the types are the same. This
781 is harder than it looks because this may be a typedef, so we have
782 to go look at the original type. It may even be a typedef of a
784 In the case of compiler-created builtin structs the TYPE_DECL
785 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
786 while (TYPE_NAME (t1)
787 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
788 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
789 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
791 while (TYPE_NAME (t2)
792 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
793 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
794 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
796 /* C90 didn't have the requirement that the two tags be the same. */
797 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
800 /* C90 didn't say what happened if one or both of the types were
801 incomplete; we choose to follow C99 rules here, which is that they
803 if (TYPE_SIZE (t1) == NULL
804 || TYPE_SIZE (t2) == NULL)
808 const struct tagged_tu_seen * tts_i;
809 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
810 if (tts_i->t1 == t1 && tts_i->t2 == t2)
814 switch (TREE_CODE (t1))
819 /* Speed up the case where the type values are in the same order. */
820 tree tv1 = TYPE_VALUES (t1);
821 tree tv2 = TYPE_VALUES (t2);
826 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
828 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
830 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
834 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
836 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
839 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
842 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
844 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
846 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
854 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
857 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
860 struct tagged_tu_seen tts;
862 tts.next = tagged_tu_seen_base;
865 tagged_tu_seen_base = &tts;
867 if (DECL_NAME (s1) != NULL)
868 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
869 if (DECL_NAME (s1) == DECL_NAME (s2))
872 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
876 needs_warning = true;
878 if (TREE_CODE (s1) == FIELD_DECL
879 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
880 DECL_FIELD_BIT_OFFSET (s2)) != 1)
886 tagged_tu_seen_base = tts.next;
890 return needs_warning ? 2 : 1;
895 struct tagged_tu_seen tts;
897 tts.next = tagged_tu_seen_base;
900 tagged_tu_seen_base = &tts;
902 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
904 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
907 if (TREE_CODE (s1) != TREE_CODE (s2)
908 || DECL_NAME (s1) != DECL_NAME (s2))
910 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
914 needs_warning = true;
916 if (TREE_CODE (s1) == FIELD_DECL
917 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
918 DECL_FIELD_BIT_OFFSET (s2)) != 1)
921 tagged_tu_seen_base = tts.next;
924 return needs_warning ? 2 : 1;
932 /* Return 1 if two function types F1 and F2 are compatible.
933 If either type specifies no argument types,
934 the other must specify a fixed number of self-promoting arg types.
935 Otherwise, if one type specifies only the number of arguments,
936 the other must specify that number of self-promoting arg types.
937 Otherwise, the argument types must match. */
940 function_types_compatible_p (tree f1, tree f2, int flags)
943 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
948 ret1 = TREE_TYPE (f1);
949 ret2 = TREE_TYPE (f2);
951 /* 'volatile' qualifiers on a function's return type mean the function
953 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
954 pedwarn ("function return types not compatible due to `volatile'");
955 if (TYPE_VOLATILE (ret1))
956 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
957 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
958 if (TYPE_VOLATILE (ret2))
959 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
960 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
961 val = comptypes (ret1, ret2, flags);
965 args1 = TYPE_ARG_TYPES (f1);
966 args2 = TYPE_ARG_TYPES (f2);
968 /* An unspecified parmlist matches any specified parmlist
969 whose argument types don't need default promotions. */
973 if (!self_promoting_args_p (args2))
975 /* If one of these types comes from a non-prototype fn definition,
976 compare that with the other type's arglist.
977 If they don't match, ask for a warning (but no error). */
978 if (TYPE_ACTUAL_ARG_TYPES (f1)
979 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
986 if (!self_promoting_args_p (args1))
988 if (TYPE_ACTUAL_ARG_TYPES (f2)
989 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
995 /* Both types have argument lists: compare them and propagate results. */
996 val1 = type_lists_compatible_p (args1, args2, flags);
997 return val1 != 1 ? val1 : val;
1000 /* Check two lists of types for compatibility,
1001 returning 0 for incompatible, 1 for compatible,
1002 or 2 for compatible with warning. */
1005 type_lists_compatible_p (tree args1, tree args2, int flags)
1007 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1013 if (args1 == 0 && args2 == 0)
1015 /* If one list is shorter than the other,
1016 they fail to match. */
1017 if (args1 == 0 || args2 == 0)
1019 /* A null pointer instead of a type
1020 means there is supposed to be an argument
1021 but nothing is specified about what type it has.
1022 So match anything that self-promotes. */
1023 if (TREE_VALUE (args1) == 0)
1025 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1028 else if (TREE_VALUE (args2) == 0)
1030 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1033 /* If one of the lists has an error marker, ignore this arg. */
1034 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1035 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1037 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1038 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
1041 /* Allow wait (union {union wait *u; int *i} *)
1042 and wait (union wait *) to be compatible. */
1043 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1044 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1045 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1046 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1047 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1048 TYPE_SIZE (TREE_VALUE (args2))))
1051 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1052 memb; memb = TREE_CHAIN (memb))
1053 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
1059 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1060 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1061 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1062 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1063 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1064 TYPE_SIZE (TREE_VALUE (args1))))
1067 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1068 memb; memb = TREE_CHAIN (memb))
1069 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
1079 /* comptypes said ok, but record if it said to warn. */
1083 args1 = TREE_CHAIN (args1);
1084 args2 = TREE_CHAIN (args2);
1088 /* Compute the size to increment a pointer by. */
1091 c_size_in_bytes (tree type)
1093 enum tree_code code = TREE_CODE (type);
1095 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1096 return size_one_node;
1098 if (!COMPLETE_OR_VOID_TYPE_P (type))
1100 error ("arithmetic on pointer to an incomplete type");
1101 return size_one_node;
1104 /* Convert in case a char is more than one unit. */
1105 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1106 size_int (TYPE_PRECISION (char_type_node)
1110 /* Return either DECL or its known constant value (if it has one). */
1113 decl_constant_value (tree decl)
1115 if (/* Don't change a variable array bound or initial value to a constant
1116 in a place where a variable is invalid. Note that DECL_INITIAL
1117 isn't valid for a PARM_DECL. */
1118 current_function_decl != 0
1119 && TREE_CODE (decl) != PARM_DECL
1120 && ! TREE_THIS_VOLATILE (decl)
1121 && TREE_READONLY (decl)
1122 && DECL_INITIAL (decl) != 0
1123 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1124 /* This is invalid if initial value is not constant.
1125 If it has either a function call, a memory reference,
1126 or a variable, then re-evaluating it could give different results. */
1127 && TREE_CONSTANT (DECL_INITIAL (decl))
1128 /* Check for cases where this is sub-optimal, even though valid. */
1129 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1130 return DECL_INITIAL (decl);
1134 /* Return either DECL or its known constant value (if it has one), but
1135 return DECL if pedantic or DECL has mode BLKmode. This is for
1136 bug-compatibility with the old behavior of decl_constant_value
1137 (before GCC 3.0); every use of this function is a bug and it should
1138 be removed before GCC 3.1. It is not appropriate to use pedantic
1139 in a way that affects optimization, and BLKmode is probably not the
1140 right test for avoiding misoptimizations either. */
1143 decl_constant_value_for_broken_optimization (tree decl)
1145 if (pedantic || DECL_MODE (decl) == BLKmode)
1148 return decl_constant_value (decl);
1152 /* Perform the default conversion of arrays and functions to pointers.
1153 Return the result of converting EXP. For any other expression, just
1157 default_function_array_conversion (tree exp)
1160 tree type = TREE_TYPE (exp);
1161 enum tree_code code = TREE_CODE (type);
1164 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1167 Do not use STRIP_NOPS here! It will remove conversions from pointer
1168 to integer and cause infinite recursion. */
1170 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1171 || (TREE_CODE (exp) == NOP_EXPR
1172 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1174 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1176 exp = TREE_OPERAND (exp, 0);
1179 /* Preserve the original expression code. */
1180 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1181 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1183 if (code == FUNCTION_TYPE)
1185 return build_unary_op (ADDR_EXPR, exp, 0);
1187 if (code == ARRAY_TYPE)
1190 tree restype = TREE_TYPE (type);
1196 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1198 constp = TREE_READONLY (exp);
1199 volatilep = TREE_THIS_VOLATILE (exp);
1202 if (TYPE_QUALS (type) || constp || volatilep)
1204 = c_build_qualified_type (restype,
1206 | (constp * TYPE_QUAL_CONST)
1207 | (volatilep * TYPE_QUAL_VOLATILE));
1209 if (TREE_CODE (exp) == INDIRECT_REF)
1210 return convert (build_pointer_type (restype),
1211 TREE_OPERAND (exp, 0));
1213 if (TREE_CODE (exp) == COMPOUND_EXPR)
1215 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1216 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1217 TREE_OPERAND (exp, 0), op1);
1220 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1221 if (!flag_isoc99 && !lvalue_array_p)
1223 /* Before C99, non-lvalue arrays do not decay to pointers.
1224 Normally, using such an array would be invalid; but it can
1225 be used correctly inside sizeof or as a statement expression.
1226 Thus, do not give an error here; an error will result later. */
1230 ptrtype = build_pointer_type (restype);
1232 if (TREE_CODE (exp) == VAR_DECL)
1234 /* ??? This is not really quite correct
1235 in that the type of the operand of ADDR_EXPR
1236 is not the target type of the type of the ADDR_EXPR itself.
1237 Question is, can this lossage be avoided? */
1238 adr = build1 (ADDR_EXPR, ptrtype, exp);
1239 if (!c_mark_addressable (exp))
1240 return error_mark_node;
1241 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1244 /* This way is better for a COMPONENT_REF since it can
1245 simplify the offset for a component. */
1246 adr = build_unary_op (ADDR_EXPR, exp, 1);
1247 return convert (ptrtype, adr);
1252 /* Perform default promotions for C data used in expressions.
1253 Arrays and functions are converted to pointers;
1254 enumeral types or short or char, to int.
1255 In addition, manifest constants symbols are replaced by their values. */
1258 default_conversion (tree exp)
1261 tree type = TREE_TYPE (exp);
1262 enum tree_code code = TREE_CODE (type);
1264 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1265 return default_function_array_conversion (exp);
1267 /* Constants can be used directly unless they're not loadable. */
1268 if (TREE_CODE (exp) == CONST_DECL)
1269 exp = DECL_INITIAL (exp);
1271 /* Replace a nonvolatile const static variable with its value unless
1272 it is an array, in which case we must be sure that taking the
1273 address of the array produces consistent results. */
1274 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1276 exp = decl_constant_value_for_broken_optimization (exp);
1277 type = TREE_TYPE (exp);
1280 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1283 Do not use STRIP_NOPS here! It will remove conversions from pointer
1284 to integer and cause infinite recursion. */
1286 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1287 || (TREE_CODE (exp) == NOP_EXPR
1288 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1289 exp = TREE_OPERAND (exp, 0);
1291 /* Preserve the original expression code. */
1292 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1293 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1295 /* Normally convert enums to int,
1296 but convert wide enums to something wider. */
1297 if (code == ENUMERAL_TYPE)
1299 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1300 TYPE_PRECISION (integer_type_node)),
1301 ((TYPE_PRECISION (type)
1302 >= TYPE_PRECISION (integer_type_node))
1303 && TYPE_UNSIGNED (type)));
1305 return convert (type, exp);
1308 if (TREE_CODE (exp) == COMPONENT_REF
1309 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1310 /* If it's thinner than an int, promote it like a
1311 c_promoting_integer_type_p, otherwise leave it alone. */
1312 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1313 TYPE_PRECISION (integer_type_node)))
1314 return convert (integer_type_node, exp);
1316 if (c_promoting_integer_type_p (type))
1318 /* Preserve unsignedness if not really getting any wider. */
1319 if (TYPE_UNSIGNED (type)
1320 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1321 return convert (unsigned_type_node, exp);
1323 return convert (integer_type_node, exp);
1326 if (code == VOID_TYPE)
1328 error ("void value not ignored as it ought to be");
1329 return error_mark_node;
1334 /* Look up COMPONENT in a structure or union DECL.
1336 If the component name is not found, returns NULL_TREE. Otherwise,
1337 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1338 stepping down the chain to the component, which is in the last
1339 TREE_VALUE of the list. Normally the list is of length one, but if
1340 the component is embedded within (nested) anonymous structures or
1341 unions, the list steps down the chain to the component. */
1344 lookup_field (tree decl, tree component)
1346 tree type = TREE_TYPE (decl);
1349 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1350 to the field elements. Use a binary search on this array to quickly
1351 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1352 will always be set for structures which have many elements. */
1354 if (TYPE_LANG_SPECIFIC (type))
1357 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1359 field = TYPE_FIELDS (type);
1361 top = TYPE_LANG_SPECIFIC (type)->s->len;
1362 while (top - bot > 1)
1364 half = (top - bot + 1) >> 1;
1365 field = field_array[bot+half];
1367 if (DECL_NAME (field) == NULL_TREE)
1369 /* Step through all anon unions in linear fashion. */
1370 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1372 field = field_array[bot++];
1373 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1374 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1376 tree anon = lookup_field (field, component);
1379 return tree_cons (NULL_TREE, field, anon);
1383 /* Entire record is only anon unions. */
1387 /* Restart the binary search, with new lower bound. */
1391 if (DECL_NAME (field) == component)
1393 if (DECL_NAME (field) < component)
1399 if (DECL_NAME (field_array[bot]) == component)
1400 field = field_array[bot];
1401 else if (DECL_NAME (field) != component)
1406 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1408 if (DECL_NAME (field) == NULL_TREE
1409 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1410 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1412 tree anon = lookup_field (field, component);
1415 return tree_cons (NULL_TREE, field, anon);
1418 if (DECL_NAME (field) == component)
1422 if (field == NULL_TREE)
1426 return tree_cons (NULL_TREE, field, NULL_TREE);
1429 /* Make an expression to refer to the COMPONENT field of
1430 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1433 build_component_ref (tree datum, tree component)
1435 tree type = TREE_TYPE (datum);
1436 enum tree_code code = TREE_CODE (type);
1440 if (!objc_is_public (datum, component))
1441 return error_mark_node;
1443 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1444 Ensure that the arguments are not lvalues; otherwise,
1445 if the component is an array, it would wrongly decay to a pointer in
1447 We cannot do this with a COND_EXPR, because in a conditional expression
1448 the default promotions are applied to both sides, and this would yield
1449 the wrong type of the result; for example, if the components have
1451 switch (TREE_CODE (datum))
1455 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1456 return build (COMPOUND_EXPR, TREE_TYPE (value),
1457 TREE_OPERAND (datum, 0), non_lvalue (value));
1463 /* See if there is a field or component with name COMPONENT. */
1465 if (code == RECORD_TYPE || code == UNION_TYPE)
1467 if (!COMPLETE_TYPE_P (type))
1469 c_incomplete_type_error (NULL_TREE, type);
1470 return error_mark_node;
1473 field = lookup_field (datum, component);
1477 error ("%s has no member named `%s'",
1478 code == RECORD_TYPE ? "structure" : "union",
1479 IDENTIFIER_POINTER (component));
1480 return error_mark_node;
1483 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1484 This might be better solved in future the way the C++ front
1485 end does it - by giving the anonymous entities each a
1486 separate name and type, and then have build_component_ref
1487 recursively call itself. We can't do that here. */
1490 tree subdatum = TREE_VALUE (field);
1492 if (TREE_TYPE (subdatum) == error_mark_node)
1493 return error_mark_node;
1495 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1496 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1497 TREE_READONLY (ref) = 1;
1498 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1499 TREE_THIS_VOLATILE (ref) = 1;
1501 if (TREE_DEPRECATED (subdatum))
1502 warn_deprecated_use (subdatum);
1506 field = TREE_CHAIN (field);
1512 else if (code != ERROR_MARK)
1513 error ("request for member `%s' in something not a structure or union",
1514 IDENTIFIER_POINTER (component));
1516 return error_mark_node;
1519 /* Given an expression PTR for a pointer, return an expression
1520 for the value pointed to.
1521 ERRORSTRING is the name of the operator to appear in error messages. */
1524 build_indirect_ref (tree ptr, const char *errorstring)
1526 tree pointer = default_conversion (ptr);
1527 tree type = TREE_TYPE (pointer);
1529 if (TREE_CODE (type) == POINTER_TYPE)
1531 if (TREE_CODE (pointer) == ADDR_EXPR
1532 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1533 == TREE_TYPE (type)))
1534 return TREE_OPERAND (pointer, 0);
1537 tree t = TREE_TYPE (type);
1538 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1540 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1542 error ("dereferencing pointer to incomplete type");
1543 return error_mark_node;
1545 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1546 warning ("dereferencing `void *' pointer");
1548 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1549 so that we get the proper error message if the result is used
1550 to assign to. Also, &* is supposed to be a no-op.
1551 And ANSI C seems to specify that the type of the result
1552 should be the const type. */
1553 /* A de-reference of a pointer to const is not a const. It is valid
1554 to change it via some other pointer. */
1555 TREE_READONLY (ref) = TYPE_READONLY (t);
1556 TREE_SIDE_EFFECTS (ref)
1557 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1558 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1562 else if (TREE_CODE (pointer) != ERROR_MARK)
1563 error ("invalid type argument of `%s'", errorstring);
1564 return error_mark_node;
1567 /* This handles expressions of the form "a[i]", which denotes
1570 This is logically equivalent in C to *(a+i), but we may do it differently.
1571 If A is a variable or a member, we generate a primitive ARRAY_REF.
1572 This avoids forcing the array out of registers, and can work on
1573 arrays that are not lvalues (for example, members of structures returned
1577 build_array_ref (tree array, tree index)
1581 error ("subscript missing in array reference");
1582 return error_mark_node;
1585 if (TREE_TYPE (array) == error_mark_node
1586 || TREE_TYPE (index) == error_mark_node)
1587 return error_mark_node;
1589 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1593 /* Subscripting with type char is likely to lose
1594 on a machine where chars are signed.
1595 So warn on any machine, but optionally.
1596 Don't warn for unsigned char since that type is safe.
1597 Don't warn for signed char because anyone who uses that
1598 must have done so deliberately. */
1599 if (warn_char_subscripts
1600 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1601 warning ("array subscript has type `char'");
1603 /* Apply default promotions *after* noticing character types. */
1604 index = default_conversion (index);
1606 /* Require integer *after* promotion, for sake of enums. */
1607 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1609 error ("array subscript is not an integer");
1610 return error_mark_node;
1613 /* An array that is indexed by a non-constant
1614 cannot be stored in a register; we must be able to do
1615 address arithmetic on its address.
1616 Likewise an array of elements of variable size. */
1617 if (TREE_CODE (index) != INTEGER_CST
1618 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1619 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1621 if (!c_mark_addressable (array))
1622 return error_mark_node;
1624 /* An array that is indexed by a constant value which is not within
1625 the array bounds cannot be stored in a register either; because we
1626 would get a crash in store_bit_field/extract_bit_field when trying
1627 to access a non-existent part of the register. */
1628 if (TREE_CODE (index) == INTEGER_CST
1629 && TYPE_DOMAIN (TREE_TYPE (array))
1630 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1632 if (!c_mark_addressable (array))
1633 return error_mark_node;
1639 while (TREE_CODE (foo) == COMPONENT_REF)
1640 foo = TREE_OPERAND (foo, 0);
1641 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1642 pedwarn ("ISO C forbids subscripting `register' array");
1643 else if (! flag_isoc99 && ! lvalue_p (foo))
1644 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1647 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1648 rval = build (ARRAY_REF, type, array, index);
1649 /* Array ref is const/volatile if the array elements are
1650 or if the array is. */
1651 TREE_READONLY (rval)
1652 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1653 | TREE_READONLY (array));
1654 TREE_SIDE_EFFECTS (rval)
1655 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1656 | TREE_SIDE_EFFECTS (array));
1657 TREE_THIS_VOLATILE (rval)
1658 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1659 /* This was added by rms on 16 Nov 91.
1660 It fixes vol struct foo *a; a->elts[1]
1661 in an inline function.
1662 Hope it doesn't break something else. */
1663 | TREE_THIS_VOLATILE (array));
1664 return require_complete_type (fold (rval));
1668 tree ar = default_conversion (array);
1669 tree ind = default_conversion (index);
1671 /* Do the same warning check as above, but only on the part that's
1672 syntactically the index and only if it is also semantically
1674 if (warn_char_subscripts
1675 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1676 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1677 warning ("subscript has type `char'");
1679 /* Put the integer in IND to simplify error checking. */
1680 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1687 if (ar == error_mark_node)
1690 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1691 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1693 error ("subscripted value is neither array nor pointer");
1694 return error_mark_node;
1696 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1698 error ("array subscript is not an integer");
1699 return error_mark_node;
1702 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1707 /* Build an external reference to identifier ID. FUN indicates
1708 whether this will be used for a function call. */
1710 build_external_ref (tree id, int fun)
1713 tree decl = lookup_name (id);
1714 tree objc_ivar = lookup_objc_ivar (id);
1716 if (decl && decl != error_mark_node)
1718 /* Properly declared variable or function reference. */
1721 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1723 warning ("local declaration of `%s' hides instance variable",
1724 IDENTIFIER_POINTER (id));
1733 /* Implicit function declaration. */
1734 ref = implicitly_declare (id);
1735 else if (decl == error_mark_node)
1736 /* Don't complain about something that's already been
1737 complained about. */
1738 return error_mark_node;
1741 undeclared_variable (id);
1742 return error_mark_node;
1745 if (TREE_TYPE (ref) == error_mark_node)
1746 return error_mark_node;
1748 if (TREE_DEPRECATED (ref))
1749 warn_deprecated_use (ref);
1751 if (!skip_evaluation)
1752 assemble_external (ref);
1753 TREE_USED (ref) = 1;
1755 if (TREE_CODE (ref) == CONST_DECL)
1757 ref = DECL_INITIAL (ref);
1758 TREE_CONSTANT (ref) = 1;
1759 TREE_INVARIANT (ref) = 1;
1761 else if (current_function_decl != 0
1762 && !DECL_FILE_SCOPE_P (current_function_decl)
1763 && (TREE_CODE (ref) == VAR_DECL
1764 || TREE_CODE (ref) == PARM_DECL
1765 || TREE_CODE (ref) == FUNCTION_DECL))
1767 tree context = decl_function_context (ref);
1769 if (context != 0 && context != current_function_decl)
1770 DECL_NONLOCAL (ref) = 1;
1776 /* Build a function call to function FUNCTION with parameters PARAMS.
1777 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1778 TREE_VALUE of each node is a parameter-expression.
1779 FUNCTION's data type may be a function type or a pointer-to-function. */
1782 build_function_call (tree function, tree params)
1784 tree fntype, fundecl = 0;
1785 tree coerced_params;
1786 tree name = NULL_TREE, result;
1789 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1790 STRIP_TYPE_NOPS (function);
1792 /* Convert anything with function type to a pointer-to-function. */
1793 if (TREE_CODE (function) == FUNCTION_DECL)
1795 name = DECL_NAME (function);
1797 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1798 (because calling an inline function does not mean the function
1799 needs to be separately compiled). */
1800 fntype = build_type_variant (TREE_TYPE (function),
1801 TREE_READONLY (function),
1802 TREE_THIS_VOLATILE (function));
1804 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1807 function = default_conversion (function);
1809 fntype = TREE_TYPE (function);
1811 if (TREE_CODE (fntype) == ERROR_MARK)
1812 return error_mark_node;
1814 if (!(TREE_CODE (fntype) == POINTER_TYPE
1815 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1817 error ("called object is not a function");
1818 return error_mark_node;
1821 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1822 current_function_returns_abnormally = 1;
1824 /* fntype now gets the type of function pointed to. */
1825 fntype = TREE_TYPE (fntype);
1827 /* Check that the function is called through a compatible prototype.
1828 If it is not, replace the call by a trap, wrapped up in a compound
1829 expression if necessary. This has the nice side-effect to prevent
1830 the tree-inliner from generating invalid assignment trees which may
1831 blow up in the RTL expander later.
1833 ??? This doesn't work for Objective-C because objc_comptypes
1834 refuses to compare function prototypes, yet the compiler appears
1835 to build calls that are flagged as invalid by C's comptypes. */
1836 if (! c_dialect_objc ()
1837 && TREE_CODE (function) == NOP_EXPR
1838 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1839 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1840 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1842 tree return_type = TREE_TYPE (fntype);
1843 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1846 /* This situation leads to run-time undefined behavior. We can't,
1847 therefore, simply error unless we can prove that all possible
1848 executions of the program must execute the code. */
1849 warning ("function called through a non-compatible type");
1851 /* We can, however, treat "undefined" any way we please.
1852 Call abort to encourage the user to fix the program. */
1853 inform ("if this code is reached, the program will abort");
1855 if (VOID_TYPE_P (return_type))
1861 if (AGGREGATE_TYPE_P (return_type))
1862 rhs = build_compound_literal (return_type,
1863 build_constructor (return_type,
1866 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1868 return build (COMPOUND_EXPR, return_type, trap, rhs);
1872 /* Convert the parameters to the types declared in the
1873 function prototype, or apply default promotions. */
1876 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1878 /* Check that the arguments to the function are valid. */
1880 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1882 /* Recognize certain built-in functions so we can make tree-codes
1883 other than CALL_EXPR. We do this when it enables fold-const.c
1884 to do something useful. */
1886 if (TREE_CODE (function) == ADDR_EXPR
1887 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1888 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1890 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1891 params, coerced_params);
1896 result = build (CALL_EXPR, TREE_TYPE (fntype),
1897 function, coerced_params, NULL_TREE);
1898 TREE_SIDE_EFFECTS (result) = 1;
1900 if (require_constant_value)
1902 result = fold_initializer (result);
1904 if (TREE_CONSTANT (result)
1905 && (name == NULL_TREE
1906 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
1907 pedwarn_init ("initializer element is not constant");
1910 result = fold (result);
1912 if (VOID_TYPE_P (TREE_TYPE (result)))
1914 return require_complete_type (result);
1917 /* Convert the argument expressions in the list VALUES
1918 to the types in the list TYPELIST. The result is a list of converted
1919 argument expressions.
1921 If TYPELIST is exhausted, or when an element has NULL as its type,
1922 perform the default conversions.
1924 PARMLIST is the chain of parm decls for the function being called.
1925 It may be 0, if that info is not available.
1926 It is used only for generating error messages.
1928 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1930 This is also where warnings about wrong number of args are generated.
1932 Both VALUES and the returned value are chains of TREE_LIST nodes
1933 with the elements of the list in the TREE_VALUE slots of those nodes. */
1936 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1938 tree typetail, valtail;
1942 /* Scan the given expressions and types, producing individual
1943 converted arguments and pushing them on RESULT in reverse order. */
1945 for (valtail = values, typetail = typelist, parmnum = 0;
1947 valtail = TREE_CHAIN (valtail), parmnum++)
1949 tree type = typetail ? TREE_VALUE (typetail) : 0;
1950 tree val = TREE_VALUE (valtail);
1952 if (type == void_type_node)
1955 error ("too many arguments to function `%s'",
1956 IDENTIFIER_POINTER (name));
1958 error ("too many arguments to function");
1962 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1963 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1964 to convert automatically to a pointer. */
1965 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1966 val = TREE_OPERAND (val, 0);
1968 val = default_function_array_conversion (val);
1970 val = require_complete_type (val);
1974 /* Formal parm type is specified by a function prototype. */
1977 if (!COMPLETE_TYPE_P (type))
1979 error ("type of formal parameter %d is incomplete", parmnum + 1);
1984 /* Optionally warn about conversions that
1985 differ from the default conversions. */
1986 if (warn_conversion || warn_traditional)
1988 int formal_prec = TYPE_PRECISION (type);
1990 if (INTEGRAL_TYPE_P (type)
1991 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1992 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1993 if (INTEGRAL_TYPE_P (type)
1994 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1995 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1996 else if (TREE_CODE (type) == COMPLEX_TYPE
1997 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1998 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1999 else if (TREE_CODE (type) == REAL_TYPE
2000 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2001 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
2002 else if (TREE_CODE (type) == COMPLEX_TYPE
2003 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2004 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
2005 else if (TREE_CODE (type) == REAL_TYPE
2006 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2007 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
2008 /* ??? At some point, messages should be written about
2009 conversions between complex types, but that's too messy
2011 else if (TREE_CODE (type) == REAL_TYPE
2012 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2014 /* Warn if any argument is passed as `float',
2015 since without a prototype it would be `double'. */
2016 if (formal_prec == TYPE_PRECISION (float_type_node))
2017 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
2019 /* Detect integer changing in width or signedness.
2020 These warnings are only activated with
2021 -Wconversion, not with -Wtraditional. */
2022 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2023 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2025 tree would_have_been = default_conversion (val);
2026 tree type1 = TREE_TYPE (would_have_been);
2028 if (TREE_CODE (type) == ENUMERAL_TYPE
2029 && (TYPE_MAIN_VARIANT (type)
2030 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2031 /* No warning if function asks for enum
2032 and the actual arg is that enum type. */
2034 else if (formal_prec != TYPE_PRECISION (type1))
2035 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2036 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2038 /* Don't complain if the formal parameter type
2039 is an enum, because we can't tell now whether
2040 the value was an enum--even the same enum. */
2041 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2043 else if (TREE_CODE (val) == INTEGER_CST
2044 && int_fits_type_p (val, type))
2045 /* Change in signedness doesn't matter
2046 if a constant value is unaffected. */
2048 /* Likewise for a constant in a NOP_EXPR. */
2049 else if (TREE_CODE (val) == NOP_EXPR
2050 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2051 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2053 /* If the value is extended from a narrower
2054 unsigned type, it doesn't matter whether we
2055 pass it as signed or unsigned; the value
2056 certainly is the same either way. */
2057 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2058 && TYPE_UNSIGNED (TREE_TYPE (val)))
2060 else if (TYPE_UNSIGNED (type))
2061 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2063 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2067 parmval = convert_for_assignment (type, val,
2068 (char *) 0, /* arg passing */
2069 fundecl, name, parmnum + 1);
2071 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2072 && INTEGRAL_TYPE_P (type)
2073 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2074 parmval = default_conversion (parmval);
2076 result = tree_cons (NULL_TREE, parmval, result);
2078 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2079 && (TYPE_PRECISION (TREE_TYPE (val))
2080 < TYPE_PRECISION (double_type_node)))
2081 /* Convert `float' to `double'. */
2082 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2084 /* Convert `short' and `char' to full-size `int'. */
2085 result = tree_cons (NULL_TREE, default_conversion (val), result);
2088 typetail = TREE_CHAIN (typetail);
2091 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2094 error ("too few arguments to function `%s'",
2095 IDENTIFIER_POINTER (name));
2097 error ("too few arguments to function");
2100 return nreverse (result);
2103 /* This is the entry point used by the parser
2104 for binary operators in the input.
2105 In addition to constructing the expression,
2106 we check for operands that were written with other binary operators
2107 in a way that is likely to confuse the user. */
2110 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
2112 tree result = build_binary_op (code, arg1, arg2, 1);
2115 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2116 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2117 enum tree_code code1 = ERROR_MARK;
2118 enum tree_code code2 = ERROR_MARK;
2120 if (TREE_CODE (result) == ERROR_MARK)
2121 return error_mark_node;
2123 if (IS_EXPR_CODE_CLASS (class1))
2124 code1 = C_EXP_ORIGINAL_CODE (arg1);
2125 if (IS_EXPR_CODE_CLASS (class2))
2126 code2 = C_EXP_ORIGINAL_CODE (arg2);
2128 /* Check for cases such as x+y<<z which users are likely
2129 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2130 is cleared to prevent these warnings. */
2131 if (warn_parentheses)
2133 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2135 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2136 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2137 warning ("suggest parentheses around + or - inside shift");
2140 if (code == TRUTH_ORIF_EXPR)
2142 if (code1 == TRUTH_ANDIF_EXPR
2143 || code2 == TRUTH_ANDIF_EXPR)
2144 warning ("suggest parentheses around && within ||");
2147 if (code == BIT_IOR_EXPR)
2149 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2150 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2151 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2152 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2153 warning ("suggest parentheses around arithmetic in operand of |");
2154 /* Check cases like x|y==z */
2155 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2156 warning ("suggest parentheses around comparison in operand of |");
2159 if (code == BIT_XOR_EXPR)
2161 if (code1 == BIT_AND_EXPR
2162 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2163 || code2 == BIT_AND_EXPR
2164 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2165 warning ("suggest parentheses around arithmetic in operand of ^");
2166 /* Check cases like x^y==z */
2167 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2168 warning ("suggest parentheses around comparison in operand of ^");
2171 if (code == BIT_AND_EXPR)
2173 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2174 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2175 warning ("suggest parentheses around + or - in operand of &");
2176 /* Check cases like x&y==z */
2177 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2178 warning ("suggest parentheses around comparison in operand of &");
2182 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2183 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2184 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2185 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2187 unsigned_conversion_warning (result, arg1);
2188 unsigned_conversion_warning (result, arg2);
2189 overflow_warning (result);
2191 class = TREE_CODE_CLASS (TREE_CODE (result));
2193 /* Record the code that was specified in the source,
2194 for the sake of warnings about confusing nesting. */
2195 if (IS_EXPR_CODE_CLASS (class))
2196 C_SET_EXP_ORIGINAL_CODE (result, code);
2199 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2200 so that convert_for_assignment wouldn't strip it.
2201 That way, we got warnings for things like p = (1 - 1).
2202 But it turns out we should not get those warnings. */
2203 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2204 C_SET_EXP_ORIGINAL_CODE (result, code);
2211 /* Return true if `t' is known to be non-negative. */
2214 c_tree_expr_nonnegative_p (tree t)
2216 if (TREE_CODE (t) == STMT_EXPR)
2218 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2220 /* Find the last statement in the chain, ignoring the final
2221 * scope statement */
2222 while (TREE_CHAIN (t) != NULL_TREE
2223 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2225 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2227 return tree_expr_nonnegative_p (t);
2230 /* Return a tree for the difference of pointers OP0 and OP1.
2231 The resulting tree has type int. */
2234 pointer_diff (tree op0, tree op1)
2236 tree restype = ptrdiff_type_node;
2238 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2239 tree con0, con1, lit0, lit1;
2240 tree orig_op1 = op1;
2242 if (pedantic || warn_pointer_arith)
2244 if (TREE_CODE (target_type) == VOID_TYPE)
2245 pedwarn ("pointer of type `void *' used in subtraction");
2246 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2247 pedwarn ("pointer to a function used in subtraction");
2250 /* If the conversion to ptrdiff_type does anything like widening or
2251 converting a partial to an integral mode, we get a convert_expression
2252 that is in the way to do any simplifications.
2253 (fold-const.c doesn't know that the extra bits won't be needed.
2254 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2255 different mode in place.)
2256 So first try to find a common term here 'by hand'; we want to cover
2257 at least the cases that occur in legal static initializers. */
2258 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2259 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2261 if (TREE_CODE (con0) == PLUS_EXPR)
2263 lit0 = TREE_OPERAND (con0, 1);
2264 con0 = TREE_OPERAND (con0, 0);
2267 lit0 = integer_zero_node;
2269 if (TREE_CODE (con1) == PLUS_EXPR)
2271 lit1 = TREE_OPERAND (con1, 1);
2272 con1 = TREE_OPERAND (con1, 0);
2275 lit1 = integer_zero_node;
2277 if (operand_equal_p (con0, con1, 0))
2284 /* First do the subtraction as integers;
2285 then drop through to build the divide operator.
2286 Do not do default conversions on the minus operator
2287 in case restype is a short type. */
2289 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2290 convert (restype, op1), 0);
2291 /* This generates an error if op1 is pointer to incomplete type. */
2292 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2293 error ("arithmetic on pointer to an incomplete type");
2295 /* This generates an error if op0 is pointer to incomplete type. */
2296 op1 = c_size_in_bytes (target_type);
2298 /* Divide by the size, in easiest possible way. */
2299 return fold (build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2302 /* Construct and perhaps optimize a tree representation
2303 for a unary operation. CODE, a tree_code, specifies the operation
2304 and XARG is the operand.
2305 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2306 the default promotions (such as from short to int).
2307 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2308 allows non-lvalues; this is only used to handle conversion of non-lvalue
2309 arrays to pointers in C99. */
2312 build_unary_op (enum tree_code code, tree xarg, int flag)
2314 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2317 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2319 int noconvert = flag;
2321 if (typecode == ERROR_MARK)
2322 return error_mark_node;
2323 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2324 typecode = INTEGER_TYPE;
2329 /* This is used for unary plus, because a CONVERT_EXPR
2330 is enough to prevent anybody from looking inside for
2331 associativity, but won't generate any code. */
2332 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2333 || typecode == COMPLEX_TYPE))
2335 error ("wrong type argument to unary plus");
2336 return error_mark_node;
2338 else if (!noconvert)
2339 arg = default_conversion (arg);
2340 arg = non_lvalue (arg);
2344 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2345 || typecode == COMPLEX_TYPE
2346 || typecode == VECTOR_TYPE))
2348 error ("wrong type argument to unary minus");
2349 return error_mark_node;
2351 else if (!noconvert)
2352 arg = default_conversion (arg);
2356 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2359 arg = default_conversion (arg);
2361 else if (typecode == COMPLEX_TYPE)
2365 pedwarn ("ISO C does not support `~' for complex conjugation");
2367 arg = default_conversion (arg);
2371 error ("wrong type argument to bit-complement");
2372 return error_mark_node;
2377 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2379 error ("wrong type argument to abs");
2380 return error_mark_node;
2382 else if (!noconvert)
2383 arg = default_conversion (arg);
2387 /* Conjugating a real value is a no-op, but allow it anyway. */
2388 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2389 || typecode == COMPLEX_TYPE))
2391 error ("wrong type argument to conjugation");
2392 return error_mark_node;
2394 else if (!noconvert)
2395 arg = default_conversion (arg);
2398 case TRUTH_NOT_EXPR:
2399 if (typecode != INTEGER_TYPE
2400 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2401 && typecode != COMPLEX_TYPE
2402 /* These will convert to a pointer. */
2403 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2405 error ("wrong type argument to unary exclamation mark");
2406 return error_mark_node;
2408 arg = lang_hooks.truthvalue_conversion (arg);
2409 return invert_truthvalue (arg);
2415 if (TREE_CODE (arg) == COMPLEX_CST)
2416 return TREE_REALPART (arg);
2417 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2418 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2423 if (TREE_CODE (arg) == COMPLEX_CST)
2424 return TREE_IMAGPART (arg);
2425 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2426 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2428 return convert (TREE_TYPE (arg), integer_zero_node);
2430 case PREINCREMENT_EXPR:
2431 case POSTINCREMENT_EXPR:
2432 case PREDECREMENT_EXPR:
2433 case POSTDECREMENT_EXPR:
2435 /* Increment or decrement the real part of the value,
2436 and don't change the imaginary part. */
2437 if (typecode == COMPLEX_TYPE)
2442 pedwarn ("ISO C does not support `++' and `--' on complex types");
2444 arg = stabilize_reference (arg);
2445 real = build_unary_op (REALPART_EXPR, arg, 1);
2446 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2447 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2448 build_unary_op (code, real, 1), imag);
2451 /* Report invalid types. */
2453 if (typecode != POINTER_TYPE
2454 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2456 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2457 error ("wrong type argument to increment");
2459 error ("wrong type argument to decrement");
2461 return error_mark_node;
2466 tree result_type = TREE_TYPE (arg);
2468 arg = get_unwidened (arg, 0);
2469 argtype = TREE_TYPE (arg);
2471 /* Compute the increment. */
2473 if (typecode == POINTER_TYPE)
2475 /* If pointer target is an undefined struct,
2476 we just cannot know how to do the arithmetic. */
2477 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2479 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2480 error ("increment of pointer to unknown structure");
2482 error ("decrement of pointer to unknown structure");
2484 else if ((pedantic || warn_pointer_arith)
2485 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2486 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2488 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2489 pedwarn ("wrong type argument to increment");
2491 pedwarn ("wrong type argument to decrement");
2494 inc = c_size_in_bytes (TREE_TYPE (result_type));
2497 inc = integer_one_node;
2499 inc = convert (argtype, inc);
2501 /* Complain about anything else that is not a true lvalue. */
2502 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2503 || code == POSTINCREMENT_EXPR)
2504 ? "invalid lvalue in increment"
2505 : "invalid lvalue in decrement")))
2506 return error_mark_node;
2508 /* Report a read-only lvalue. */
2509 if (TREE_READONLY (arg))
2510 readonly_error (arg,
2511 ((code == PREINCREMENT_EXPR
2512 || code == POSTINCREMENT_EXPR)
2513 ? "increment" : "decrement"));
2515 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2516 val = boolean_increment (code, arg);
2518 val = build (code, TREE_TYPE (arg), arg, inc);
2519 TREE_SIDE_EFFECTS (val) = 1;
2520 val = convert (result_type, val);
2521 if (TREE_CODE (val) != code)
2522 TREE_NO_WARNING (val) = 1;
2527 /* Note that this operation never does default_conversion. */
2529 /* Let &* cancel out to simplify resulting code. */
2530 if (TREE_CODE (arg) == INDIRECT_REF)
2532 /* Don't let this be an lvalue. */
2533 if (lvalue_p (TREE_OPERAND (arg, 0)))
2534 return non_lvalue (TREE_OPERAND (arg, 0));
2535 return TREE_OPERAND (arg, 0);
2538 /* For &x[y], return x+y */
2539 if (TREE_CODE (arg) == ARRAY_REF)
2541 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2542 return error_mark_node;
2543 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2544 TREE_OPERAND (arg, 1), 1);
2547 /* Anything not already handled and not a true memory reference
2548 or a non-lvalue array is an error. */
2549 else if (typecode != FUNCTION_TYPE && !flag
2550 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2551 return error_mark_node;
2553 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2554 argtype = TREE_TYPE (arg);
2556 /* If the lvalue is const or volatile, merge that into the type
2557 to which the address will point. Note that you can't get a
2558 restricted pointer by taking the address of something, so we
2559 only have to deal with `const' and `volatile' here. */
2560 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2561 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2562 argtype = c_build_type_variant (argtype,
2563 TREE_READONLY (arg),
2564 TREE_THIS_VOLATILE (arg));
2566 argtype = build_pointer_type (argtype);
2568 if (!c_mark_addressable (arg))
2569 return error_mark_node;
2574 if (TREE_CODE (arg) == COMPONENT_REF)
2576 tree field = TREE_OPERAND (arg, 1);
2578 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2580 if (DECL_C_BIT_FIELD (field))
2582 error ("attempt to take address of bit-field structure member `%s'",
2583 IDENTIFIER_POINTER (DECL_NAME (field)));
2584 return error_mark_node;
2587 addr = fold (build (PLUS_EXPR, argtype,
2588 convert (argtype, addr),
2589 convert (argtype, byte_position (field))));
2592 addr = build1 (code, argtype, arg);
2602 argtype = TREE_TYPE (arg);
2603 val = build1 (code, argtype, arg);
2604 return require_constant_value ? fold_initializer (val) : fold (val);
2607 /* Return nonzero if REF is an lvalue valid for this language.
2608 Lvalues can be assigned, unless their type has TYPE_READONLY.
2609 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2614 enum tree_code code = TREE_CODE (ref);
2621 return lvalue_p (TREE_OPERAND (ref, 0));
2623 case COMPOUND_LITERAL_EXPR:
2633 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2634 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2638 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2645 /* Return nonzero if REF is an lvalue valid for this language;
2646 otherwise, print an error message and return zero. */
2649 lvalue_or_else (tree ref, const char *msgid)
2651 int win = lvalue_p (ref);
2654 error ("%s", msgid);
2660 /* Warn about storing in something that is `const'. */
2663 readonly_error (tree arg, const char *msgid)
2665 if (TREE_CODE (arg) == COMPONENT_REF)
2667 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2668 readonly_error (TREE_OPERAND (arg, 0), msgid);
2670 error ("%s of read-only member `%s'", _(msgid),
2671 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2673 else if (TREE_CODE (arg) == VAR_DECL)
2674 error ("%s of read-only variable `%s'", _(msgid),
2675 IDENTIFIER_POINTER (DECL_NAME (arg)));
2677 error ("%s of read-only location", _(msgid));
2680 /* Mark EXP saying that we need to be able to take the
2681 address of it; it should not be allocated in a register.
2682 Returns true if successful. */
2685 c_mark_addressable (tree exp)
2690 switch (TREE_CODE (x))
2693 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2695 error ("cannot take address of bit-field `%s'",
2696 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2700 /* ... fall through ... */
2706 x = TREE_OPERAND (x, 0);
2709 case COMPOUND_LITERAL_EXPR:
2711 TREE_ADDRESSABLE (x) = 1;
2718 if (C_DECL_REGISTER (x)
2719 && DECL_NONLOCAL (x))
2721 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2723 error ("global register variable `%s' used in nested function",
2724 IDENTIFIER_POINTER (DECL_NAME (x)));
2727 pedwarn ("register variable `%s' used in nested function",
2728 IDENTIFIER_POINTER (DECL_NAME (x)));
2730 else if (C_DECL_REGISTER (x))
2732 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2734 error ("address of global register variable `%s' requested",
2735 IDENTIFIER_POINTER (DECL_NAME (x)));
2739 pedwarn ("address of register variable `%s' requested",
2740 IDENTIFIER_POINTER (DECL_NAME (x)));
2742 put_var_into_stack (x, /*rescan=*/true);
2746 TREE_ADDRESSABLE (x) = 1;
2753 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2756 build_conditional_expr (tree ifexp, tree op1, tree op2)
2760 enum tree_code code1;
2761 enum tree_code code2;
2762 tree result_type = NULL;
2763 tree orig_op1 = op1, orig_op2 = op2;
2765 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2767 /* Promote both alternatives. */
2769 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2770 op1 = default_conversion (op1);
2771 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2772 op2 = default_conversion (op2);
2774 if (TREE_CODE (ifexp) == ERROR_MARK
2775 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2776 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2777 return error_mark_node;
2779 type1 = TREE_TYPE (op1);
2780 code1 = TREE_CODE (type1);
2781 type2 = TREE_TYPE (op2);
2782 code2 = TREE_CODE (type2);
2784 /* C90 does not permit non-lvalue arrays in conditional expressions.
2785 In C99 they will be pointers by now. */
2786 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2788 error ("non-lvalue array in conditional expression");
2789 return error_mark_node;
2792 /* Quickly detect the usual case where op1 and op2 have the same type
2794 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2797 result_type = type1;
2799 result_type = TYPE_MAIN_VARIANT (type1);
2801 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2802 || code1 == COMPLEX_TYPE)
2803 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2804 || code2 == COMPLEX_TYPE))
2806 result_type = common_type (type1, type2);
2808 /* If -Wsign-compare, warn here if type1 and type2 have
2809 different signedness. We'll promote the signed to unsigned
2810 and later code won't know it used to be different.
2811 Do this check on the original types, so that explicit casts
2812 will be considered, but default promotions won't. */
2813 if (warn_sign_compare && !skip_evaluation)
2815 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2816 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2818 if (unsigned_op1 ^ unsigned_op2)
2820 /* Do not warn if the result type is signed, since the
2821 signed type will only be chosen if it can represent
2822 all the values of the unsigned type. */
2823 if (! TYPE_UNSIGNED (result_type))
2825 /* Do not warn if the signed quantity is an unsuffixed
2826 integer literal (or some static constant expression
2827 involving such literals) and it is non-negative. */
2828 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2829 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2832 warning ("signed and unsigned type in conditional expression");
2836 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2838 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2839 pedwarn ("ISO C forbids conditional expr with only one void side");
2840 result_type = void_type_node;
2842 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2844 if (comp_target_types (type1, type2, 1))
2845 result_type = common_pointer_type (type1, type2);
2846 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2847 && TREE_CODE (orig_op1) != NOP_EXPR)
2848 result_type = qualify_type (type2, type1);
2849 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2850 && TREE_CODE (orig_op2) != NOP_EXPR)
2851 result_type = qualify_type (type1, type2);
2852 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2854 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2855 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2856 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2857 TREE_TYPE (type2)));
2859 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2861 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2862 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2863 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2864 TREE_TYPE (type1)));
2868 pedwarn ("pointer type mismatch in conditional expression");
2869 result_type = build_pointer_type (void_type_node);
2872 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2874 if (! integer_zerop (op2))
2875 pedwarn ("pointer/integer type mismatch in conditional expression");
2878 op2 = null_pointer_node;
2880 result_type = type1;
2882 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2884 if (!integer_zerop (op1))
2885 pedwarn ("pointer/integer type mismatch in conditional expression");
2888 op1 = null_pointer_node;
2890 result_type = type2;
2895 if (flag_cond_mismatch)
2896 result_type = void_type_node;
2899 error ("type mismatch in conditional expression");
2900 return error_mark_node;
2904 /* Merge const and volatile flags of the incoming types. */
2906 = build_type_variant (result_type,
2907 TREE_READONLY (op1) || TREE_READONLY (op2),
2908 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2910 if (result_type != TREE_TYPE (op1))
2911 op1 = convert_and_check (result_type, op1);
2912 if (result_type != TREE_TYPE (op2))
2913 op2 = convert_and_check (result_type, op2);
2915 if (TREE_CODE (ifexp) == INTEGER_CST)
2916 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2918 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2921 /* Given a list of expressions, return a compound expression
2922 that performs them all and returns the value of the last of them. */
2925 build_compound_expr (tree list)
2927 return internal_build_compound_expr (list, TRUE);
2931 internal_build_compound_expr (tree list, int first_p)
2935 if (TREE_CHAIN (list) == 0)
2937 /* Convert arrays and functions to pointers when there
2938 really is a comma operator. */
2941 = default_function_array_conversion (TREE_VALUE (list));
2943 /* Don't let (0, 0) be null pointer constant. */
2944 if (!first_p && integer_zerop (TREE_VALUE (list)))
2945 return non_lvalue (TREE_VALUE (list));
2946 return TREE_VALUE (list);
2949 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2951 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2953 /* The left-hand operand of a comma expression is like an expression
2954 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2955 any side-effects, unless it was explicitly cast to (void). */
2956 if (warn_unused_value
2957 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2958 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2959 warning ("left-hand operand of comma expression has no effect");
2962 /* With -Wunused, we should also warn if the left-hand operand does have
2963 side-effects, but computes a value which is not used. For example, in
2964 `foo() + bar(), baz()' the result of the `+' operator is not used,
2965 so we should issue a warning. */
2966 else if (warn_unused_value)
2967 warn_if_unused_value (TREE_VALUE (list));
2969 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2972 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2975 build_c_cast (tree type, tree expr)
2979 if (type == error_mark_node || expr == error_mark_node)
2980 return error_mark_node;
2982 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2983 only in <protocol> qualifications. But when constructing cast expressions,
2984 the protocols do matter and must be kept around. */
2985 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2986 type = TYPE_MAIN_VARIANT (type);
2988 if (TREE_CODE (type) == ARRAY_TYPE)
2990 error ("cast specifies array type");
2991 return error_mark_node;
2994 if (TREE_CODE (type) == FUNCTION_TYPE)
2996 error ("cast specifies function type");
2997 return error_mark_node;
3000 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3004 if (TREE_CODE (type) == RECORD_TYPE
3005 || TREE_CODE (type) == UNION_TYPE)
3006 pedwarn ("ISO C forbids casting nonscalar to the same type");
3009 else if (TREE_CODE (type) == UNION_TYPE)
3012 value = default_function_array_conversion (value);
3014 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3015 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3016 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
3024 pedwarn ("ISO C forbids casts to union type");
3025 t = digest_init (type,
3026 build_constructor (type,
3027 build_tree_list (field, value)),
3029 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3030 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3033 error ("cast to union type from type not present in union");
3034 return error_mark_node;
3040 /* If casting to void, avoid the error that would come
3041 from default_conversion in the case of a non-lvalue array. */
3042 if (type == void_type_node)
3043 return build1 (CONVERT_EXPR, type, value);
3045 /* Convert functions and arrays to pointers,
3046 but don't convert any other types. */
3047 value = default_function_array_conversion (value);
3048 otype = TREE_TYPE (value);
3050 /* Optionally warn about potentially worrisome casts. */
3053 && TREE_CODE (type) == POINTER_TYPE
3054 && TREE_CODE (otype) == POINTER_TYPE)
3056 tree in_type = type;
3057 tree in_otype = otype;
3061 /* Check that the qualifiers on IN_TYPE are a superset of
3062 the qualifiers of IN_OTYPE. The outermost level of
3063 POINTER_TYPE nodes is uninteresting and we stop as soon
3064 as we hit a non-POINTER_TYPE node on either type. */
3067 in_otype = TREE_TYPE (in_otype);
3068 in_type = TREE_TYPE (in_type);
3070 /* GNU C allows cv-qualified function types. 'const'
3071 means the function is very pure, 'volatile' means it
3072 can't return. We need to warn when such qualifiers
3073 are added, not when they're taken away. */
3074 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3075 && TREE_CODE (in_type) == FUNCTION_TYPE)
3076 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3078 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3080 while (TREE_CODE (in_type) == POINTER_TYPE
3081 && TREE_CODE (in_otype) == POINTER_TYPE);
3084 warning ("cast adds new qualifiers to function type");
3087 /* There are qualifiers present in IN_OTYPE that are not
3088 present in IN_TYPE. */
3089 warning ("cast discards qualifiers from pointer target type");
3092 /* Warn about possible alignment problems. */
3093 if (STRICT_ALIGNMENT && warn_cast_align
3094 && TREE_CODE (type) == POINTER_TYPE
3095 && TREE_CODE (otype) == POINTER_TYPE
3096 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3097 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3098 /* Don't warn about opaque types, where the actual alignment
3099 restriction is unknown. */
3100 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3101 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3102 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3103 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3104 warning ("cast increases required alignment of target type");
3106 if (TREE_CODE (type) == INTEGER_TYPE
3107 && TREE_CODE (otype) == POINTER_TYPE
3108 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3109 && !TREE_CONSTANT (value))
3110 warning ("cast from pointer to integer of different size");
3112 if (warn_bad_function_cast
3113 && TREE_CODE (value) == CALL_EXPR
3114 && TREE_CODE (type) != TREE_CODE (otype))
3115 warning ("cast does not match function type");
3117 if (TREE_CODE (type) == POINTER_TYPE
3118 && TREE_CODE (otype) == INTEGER_TYPE
3119 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3120 /* Don't warn about converting any constant. */
3121 && !TREE_CONSTANT (value))
3122 warning ("cast to pointer from integer of different size");
3124 if (TREE_CODE (type) == POINTER_TYPE
3125 && TREE_CODE (otype) == POINTER_TYPE
3126 && TREE_CODE (expr) == ADDR_EXPR
3127 && DECL_P (TREE_OPERAND (expr, 0))
3128 && flag_strict_aliasing && warn_strict_aliasing
3129 && !VOID_TYPE_P (TREE_TYPE (type)))
3131 /* Casting the address of a decl to non void pointer. Warn
3132 if the cast breaks type based aliasing. */
3133 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3134 warning ("type-punning to incomplete type might break strict-aliasing rules");
3137 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3138 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3140 if (!alias_sets_conflict_p (set1, set2))
3141 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3142 else if (warn_strict_aliasing > 1
3143 && !alias_sets_might_conflict_p (set1, set2))
3144 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3148 /* If pedantic, warn for conversions between function and object
3149 pointer types, except for converting a null pointer constant
3150 to function pointer type. */
3152 && TREE_CODE (type) == POINTER_TYPE
3153 && TREE_CODE (otype) == POINTER_TYPE
3154 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3155 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3156 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3159 && TREE_CODE (type) == POINTER_TYPE
3160 && TREE_CODE (otype) == POINTER_TYPE
3161 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3162 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3163 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3164 && TREE_CODE (expr) != NOP_EXPR))
3165 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3168 /* Replace a nonvolatile const static variable with its value. */
3169 if (optimize && TREE_CODE (value) == VAR_DECL)
3170 value = decl_constant_value (value);
3171 value = convert (type, value);
3173 /* Ignore any integer overflow caused by the cast. */
3174 if (TREE_CODE (value) == INTEGER_CST)
3176 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3178 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3179 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3183 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3184 if (TREE_CODE (value) == INTEGER_CST
3185 && TREE_CODE (expr) == INTEGER_CST
3186 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3187 value = non_lvalue (value);
3189 /* Don't let a cast be an lvalue. */
3191 value = non_lvalue (value);
3196 /* Interpret a cast of expression EXPR to type TYPE. */
3198 c_cast_expr (tree type, tree expr)
3200 int saved_wsp = warn_strict_prototypes;
3202 /* This avoids warnings about unprototyped casts on
3203 integers. E.g. "#define SIG_DFL (void(*)())0". */
3204 if (TREE_CODE (expr) == INTEGER_CST)
3205 warn_strict_prototypes = 0;
3206 type = groktypename (type);
3207 warn_strict_prototypes = saved_wsp;
3209 return build_c_cast (type, expr);
3213 /* Build an assignment expression of lvalue LHS from value RHS.
3214 MODIFYCODE is the code for a binary operator that we use
3215 to combine the old value of LHS with RHS to get the new value.
3216 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3219 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3223 tree lhstype = TREE_TYPE (lhs);
3224 tree olhstype = lhstype;
3226 /* Types that aren't fully specified cannot be used in assignments. */
3227 lhs = require_complete_type (lhs);
3229 /* Avoid duplicate error messages from operands that had errors. */
3230 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3231 return error_mark_node;
3233 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3234 /* Do not use STRIP_NOPS here. We do not want an enumerator
3235 whose value is 0 to count as a null pointer constant. */
3236 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3237 rhs = TREE_OPERAND (rhs, 0);
3241 /* If a binary op has been requested, combine the old LHS value with the RHS
3242 producing the value we should actually store into the LHS. */
3244 if (modifycode != NOP_EXPR)
3246 lhs = stabilize_reference (lhs);
3247 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3250 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3251 return error_mark_node;
3253 /* Warn about storing in something that is `const'. */
3255 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3256 || ((TREE_CODE (lhstype) == RECORD_TYPE
3257 || TREE_CODE (lhstype) == UNION_TYPE)
3258 && C_TYPE_FIELDS_READONLY (lhstype)))
3259 readonly_error (lhs, "assignment");
3261 /* If storing into a structure or union member,
3262 it has probably been given type `int'.
3263 Compute the type that would go with
3264 the actual amount of storage the member occupies. */
3266 if (TREE_CODE (lhs) == COMPONENT_REF
3267 && (TREE_CODE (lhstype) == INTEGER_TYPE
3268 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3269 || TREE_CODE (lhstype) == REAL_TYPE
3270 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3271 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3273 /* If storing in a field that is in actuality a short or narrower than one,
3274 we must store in the field in its actual type. */
3276 if (lhstype != TREE_TYPE (lhs))
3278 lhs = copy_node (lhs);
3279 TREE_TYPE (lhs) = lhstype;
3282 /* Convert new value to destination type. */
3284 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3285 NULL_TREE, NULL_TREE, 0);
3286 if (TREE_CODE (newrhs) == ERROR_MARK)
3287 return error_mark_node;
3291 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3292 TREE_SIDE_EFFECTS (result) = 1;
3294 /* If we got the LHS in a different type for storing in,
3295 convert the result back to the nominal type of LHS
3296 so that the value we return always has the same type
3297 as the LHS argument. */
3299 if (olhstype == TREE_TYPE (result))
3301 return convert_for_assignment (olhstype, result, _("assignment"),
3302 NULL_TREE, NULL_TREE, 0);
3305 /* Convert value RHS to type TYPE as preparation for an assignment
3306 to an lvalue of type TYPE.
3307 The real work of conversion is done by `convert'.
3308 The purpose of this function is to generate error messages
3309 for assignments that are not allowed in C.
3310 ERRTYPE is a string to use in error messages:
3311 "assignment", "return", etc. If it is null, this is parameter passing
3312 for a function call (and different error messages are output).
3314 FUNNAME is the name of the function being called,
3315 as an IDENTIFIER_NODE, or null.
3316 PARMNUM is the number of the argument, for printing in error messages. */
3319 convert_for_assignment (tree type, tree rhs, const char *errtype,
3320 tree fundecl, tree funname, int parmnum)
3322 enum tree_code codel = TREE_CODE (type);
3324 enum tree_code coder;
3326 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3327 /* Do not use STRIP_NOPS here. We do not want an enumerator
3328 whose value is 0 to count as a null pointer constant. */
3329 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3330 rhs = TREE_OPERAND (rhs, 0);
3332 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3333 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3334 rhs = default_conversion (rhs);
3335 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3336 rhs = decl_constant_value_for_broken_optimization (rhs);
3338 rhstype = TREE_TYPE (rhs);
3339 coder = TREE_CODE (rhstype);
3341 if (coder == ERROR_MARK)
3342 return error_mark_node;
3344 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3346 overflow_warning (rhs);
3347 /* Check for Objective-C protocols. This will automatically
3348 issue a warning if there are protocol violations. No need to
3349 use the return value. */
3350 if (c_dialect_objc ())
3351 objc_comptypes (type, rhstype, 0);
3355 if (coder == VOID_TYPE)
3357 error ("void value not ignored as it ought to be");
3358 return error_mark_node;
3360 /* A type converts to a reference to it.
3361 This code doesn't fully support references, it's just for the
3362 special case of va_start and va_copy. */
3363 if (codel == REFERENCE_TYPE
3364 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3366 if (!lvalue_p (rhs))
3368 error ("cannot pass rvalue to reference parameter");
3369 return error_mark_node;
3371 if (!c_mark_addressable (rhs))
3372 return error_mark_node;
3373 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3375 /* We already know that these two types are compatible, but they
3376 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3377 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3378 likely to be va_list, a typedef to __builtin_va_list, which
3379 is different enough that it will cause problems later. */
3380 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3381 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3383 rhs = build1 (NOP_EXPR, type, rhs);
3386 /* Some types can interconvert without explicit casts. */
3387 else if (codel == VECTOR_TYPE
3388 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3389 return convert (type, rhs);
3390 /* Arithmetic types all interconvert, and enum is treated like int. */
3391 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3392 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3393 || codel == BOOLEAN_TYPE)
3394 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3395 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3396 || coder == BOOLEAN_TYPE))
3397 return convert_and_check (type, rhs);
3399 /* Conversion to a transparent union from its member types.
3400 This applies only to function arguments. */
3401 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3404 tree marginal_memb_type = 0;
3406 for (memb_types = TYPE_FIELDS (type); memb_types;
3407 memb_types = TREE_CHAIN (memb_types))
3409 tree memb_type = TREE_TYPE (memb_types);
3411 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3412 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3415 if (TREE_CODE (memb_type) != POINTER_TYPE)
3418 if (coder == POINTER_TYPE)
3420 tree ttl = TREE_TYPE (memb_type);
3421 tree ttr = TREE_TYPE (rhstype);
3423 /* Any non-function converts to a [const][volatile] void *
3424 and vice versa; otherwise, targets must be the same.
3425 Meanwhile, the lhs target must have all the qualifiers of
3427 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3428 || comp_target_types (memb_type, rhstype, 0))
3430 /* If this type won't generate any warnings, use it. */
3431 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3432 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3433 && TREE_CODE (ttl) == FUNCTION_TYPE)
3434 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3435 == TYPE_QUALS (ttr))
3436 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3437 == TYPE_QUALS (ttl))))
3440 /* Keep looking for a better type, but remember this one. */
3441 if (! marginal_memb_type)
3442 marginal_memb_type = memb_type;
3446 /* Can convert integer zero to any pointer type. */
3447 if (integer_zerop (rhs)
3448 || (TREE_CODE (rhs) == NOP_EXPR
3449 && integer_zerop (TREE_OPERAND (rhs, 0))))
3451 rhs = null_pointer_node;
3456 if (memb_types || marginal_memb_type)
3460 /* We have only a marginally acceptable member type;
3461 it needs a warning. */
3462 tree ttl = TREE_TYPE (marginal_memb_type);
3463 tree ttr = TREE_TYPE (rhstype);
3465 /* Const and volatile mean something different for function
3466 types, so the usual warnings are not appropriate. */
3467 if (TREE_CODE (ttr) == FUNCTION_TYPE
3468 && TREE_CODE (ttl) == FUNCTION_TYPE)
3470 /* Because const and volatile on functions are
3471 restrictions that say the function will not do
3472 certain things, it is okay to use a const or volatile
3473 function where an ordinary one is wanted, but not
3475 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3476 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3477 errtype, funname, parmnum);
3479 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3480 warn_for_assignment ("%s discards qualifiers from pointer target type",
3485 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3486 pedwarn ("ISO C prohibits argument conversion to union type");
3488 return build1 (NOP_EXPR, type, rhs);
3492 /* Conversions among pointers */
3493 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3494 && (coder == codel))
3496 tree ttl = TREE_TYPE (type);
3497 tree ttr = TREE_TYPE (rhstype);
3498 bool is_opaque_pointer;
3499 int target_cmp = 0; /* Cache comp_target_types () result. */
3501 /* Opaque pointers are treated like void pointers. */
3502 is_opaque_pointer = (targetm.vector_opaque_p (type)
3503 || targetm.vector_opaque_p (rhstype))
3504 && TREE_CODE (ttl) == VECTOR_TYPE
3505 && TREE_CODE (ttr) == VECTOR_TYPE;
3507 /* Any non-function converts to a [const][volatile] void *
3508 and vice versa; otherwise, targets must be the same.
3509 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3510 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3511 || (target_cmp = comp_target_types (type, rhstype, 0))
3512 || is_opaque_pointer
3513 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3514 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3517 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3520 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3521 which are not ANSI null ptr constants. */
3522 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3523 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3524 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3525 errtype, funname, parmnum);
3526 /* Const and volatile mean something different for function types,
3527 so the usual warnings are not appropriate. */
3528 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3529 && TREE_CODE (ttl) != FUNCTION_TYPE)
3531 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3532 warn_for_assignment ("%s discards qualifiers from pointer target type",
3533 errtype, funname, parmnum);
3534 /* If this is not a case of ignoring a mismatch in signedness,
3536 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3539 /* If there is a mismatch, do warn. */
3541 warn_for_assignment ("pointer targets in %s differ in signedness",
3542 errtype, funname, parmnum);
3544 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3545 && TREE_CODE (ttr) == FUNCTION_TYPE)
3547 /* Because const and volatile on functions are restrictions
3548 that say the function will not do certain things,
3549 it is okay to use a const or volatile function
3550 where an ordinary one is wanted, but not vice-versa. */
3551 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3552 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3553 errtype, funname, parmnum);
3557 warn_for_assignment ("%s from incompatible pointer type",
3558 errtype, funname, parmnum);
3559 return convert (type, rhs);
3561 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3563 error ("invalid use of non-lvalue array");
3564 return error_mark_node;
3566 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3568 /* An explicit constant 0 can convert to a pointer,
3569 or one that results from arithmetic, even including
3570 a cast to integer type. */
3571 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3573 ! (TREE_CODE (rhs) == NOP_EXPR
3574 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3575 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3576 && integer_zerop (TREE_OPERAND (rhs, 0))))
3577 warn_for_assignment ("%s makes pointer from integer without a cast",
3578 errtype, funname, parmnum);
3580 return convert (type, rhs);
3582 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3584 warn_for_assignment ("%s makes integer from pointer without a cast",
3585 errtype, funname, parmnum);
3586 return convert (type, rhs);
3588 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3589 return convert (type, rhs);
3595 tree selector = objc_message_selector ();
3597 if (selector && parmnum > 2)
3598 error ("incompatible type for argument %d of `%s'",
3599 parmnum - 2, IDENTIFIER_POINTER (selector));
3601 error ("incompatible type for argument %d of `%s'",
3602 parmnum, IDENTIFIER_POINTER (funname));
3605 error ("incompatible type for argument %d of indirect function call",
3609 error ("incompatible types in %s", errtype);
3611 return error_mark_node;
3614 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3615 is used for error and waring reporting and indicates which argument
3616 is being processed. */
3619 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3623 /* If FN was prototyped, the value has been converted already
3624 in convert_arguments. */
3625 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3628 type = TREE_TYPE (parm);
3629 ret = convert_for_assignment (type, value,
3630 (char *) 0 /* arg passing */, fn,
3631 DECL_NAME (fn), argnum);
3632 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3633 && INTEGRAL_TYPE_P (type)
3634 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3635 ret = default_conversion (ret);
3639 /* Print a warning using MSGID.
3640 It gets OPNAME as its one parameter.
3641 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3642 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3643 FUNCTION and ARGNUM are handled specially if we are building an
3644 Objective-C selector. */
3647 warn_for_assignment (const char *msgid, const char *opname, tree function,
3652 tree selector = objc_message_selector ();
3655 if (selector && argnum > 2)
3657 function = selector;
3664 /* Function name is known; supply it. */
3665 const char *const argstring = _("passing arg of `%s'");
3666 new_opname = alloca (IDENTIFIER_LENGTH (function)
3667 + strlen (argstring) + 1 + 1);
3668 sprintf (new_opname, argstring,
3669 IDENTIFIER_POINTER (function));
3673 /* Function name unknown (call through ptr). */
3674 const char *const argnofun = _("passing arg of pointer to function");
3675 new_opname = alloca (strlen (argnofun) + 1 + 1);
3676 sprintf (new_opname, argnofun);
3681 /* Function name is known; supply it. */
3682 const char *const argstring = _("passing arg %d of `%s'");
3683 new_opname = alloca (IDENTIFIER_LENGTH (function)
3684 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3685 sprintf (new_opname, argstring, argnum,
3686 IDENTIFIER_POINTER (function));
3690 /* Function name unknown (call through ptr); just give arg number. */
3691 const char *const argnofun = _("passing arg %d of pointer to function");
3692 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3693 sprintf (new_opname, argnofun, argnum);
3695 opname = new_opname;
3697 pedwarn (msgid, opname);
3700 /* If VALUE is a compound expr all of whose expressions are constant, then
3701 return its value. Otherwise, return error_mark_node.
3703 This is for handling COMPOUND_EXPRs as initializer elements
3704 which is allowed with a warning when -pedantic is specified. */
3707 valid_compound_expr_initializer (tree value, tree endtype)
3709 if (TREE_CODE (value) == COMPOUND_EXPR)
3711 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3713 return error_mark_node;
3714 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3717 else if (! TREE_CONSTANT (value)
3718 && ! initializer_constant_valid_p (value, endtype))
3719 return error_mark_node;
3724 /* Perform appropriate conversions on the initial value of a variable,
3725 store it in the declaration DECL,
3726 and print any error messages that are appropriate.
3727 If the init is invalid, store an ERROR_MARK. */
3730 store_init_value (tree decl, tree init)
3734 /* If variable's type was invalidly declared, just ignore it. */
3736 type = TREE_TYPE (decl);
3737 if (TREE_CODE (type) == ERROR_MARK)
3740 /* Digest the specified initializer into an expression. */
3742 value = digest_init (type, init, TREE_STATIC (decl));
3744 /* Store the expression if valid; else report error. */
3746 if (warn_traditional && !in_system_header
3747 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3748 warning ("traditional C rejects automatic aggregate initialization");
3750 DECL_INITIAL (decl) = value;
3752 /* ANSI wants warnings about out-of-range constant initializers. */
3753 STRIP_TYPE_NOPS (value);
3754 constant_expression_warning (value);
3756 /* Check if we need to set array size from compound literal size. */
3757 if (TREE_CODE (type) == ARRAY_TYPE
3758 && TYPE_DOMAIN (type) == 0
3759 && value != error_mark_node)
3761 tree inside_init = init;
3763 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3764 inside_init = TREE_OPERAND (init, 0);
3765 inside_init = fold (inside_init);
3767 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3769 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3771 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3773 /* For int foo[] = (int [3]){1}; we need to set array size
3774 now since later on array initializer will be just the
3775 brace enclosed list of the compound literal. */
3776 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3778 layout_decl (decl, 0);
3784 /* Methods for storing and printing names for error messages. */
3786 /* Implement a spelling stack that allows components of a name to be pushed
3787 and popped. Each element on the stack is this structure. */
3799 #define SPELLING_STRING 1
3800 #define SPELLING_MEMBER 2
3801 #define SPELLING_BOUNDS 3
3803 static struct spelling *spelling; /* Next stack element (unused). */
3804 static struct spelling *spelling_base; /* Spelling stack base. */
3805 static int spelling_size; /* Size of the spelling stack. */
3807 /* Macros to save and restore the spelling stack around push_... functions.
3808 Alternative to SAVE_SPELLING_STACK. */
3810 #define SPELLING_DEPTH() (spelling - spelling_base)
3811 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3813 /* Push an element on the spelling stack with type KIND and assign VALUE
3816 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3818 int depth = SPELLING_DEPTH (); \
3820 if (depth >= spelling_size) \
3822 spelling_size += 10; \
3823 if (spelling_base == 0) \
3824 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3826 spelling_base = xrealloc (spelling_base, \
3827 spelling_size * sizeof (struct spelling)); \
3828 RESTORE_SPELLING_DEPTH (depth); \
3831 spelling->kind = (KIND); \
3832 spelling->MEMBER = (VALUE); \
3836 /* Push STRING on the stack. Printed literally. */
3839 push_string (const char *string)
3841 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3844 /* Push a member name on the stack. Printed as '.' STRING. */
3847 push_member_name (tree decl)
3849 const char *const string
3850 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3851 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3854 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3857 push_array_bounds (int bounds)
3859 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3862 /* Compute the maximum size in bytes of the printed spelling. */
3865 spelling_length (void)
3870 for (p = spelling_base; p < spelling; p++)
3872 if (p->kind == SPELLING_BOUNDS)
3875 size += strlen (p->u.s) + 1;
3881 /* Print the spelling to BUFFER and return it. */
3884 print_spelling (char *buffer)
3889 for (p = spelling_base; p < spelling; p++)
3890 if (p->kind == SPELLING_BOUNDS)
3892 sprintf (d, "[%d]", p->u.i);
3898 if (p->kind == SPELLING_MEMBER)
3900 for (s = p->u.s; (*d = *s++); d++)
3907 /* Issue an error message for a bad initializer component.
3908 MSGID identifies the message.
3909 The component name is taken from the spelling stack. */
3912 error_init (const char *msgid)
3916 error ("%s", _(msgid));
3917 ofwhat = print_spelling (alloca (spelling_length () + 1));
3919 error ("(near initialization for `%s')", ofwhat);
3922 /* Issue a pedantic warning for a bad initializer component.
3923 MSGID identifies the message.
3924 The component name is taken from the spelling stack. */
3927 pedwarn_init (const char *msgid)
3931 pedwarn ("%s", _(msgid));
3932 ofwhat = print_spelling (alloca (spelling_length () + 1));
3934 pedwarn ("(near initialization for `%s')", ofwhat);
3937 /* Issue a warning for a bad initializer component.
3938 MSGID identifies the message.
3939 The component name is taken from the spelling stack. */
3942 warning_init (const char *msgid)
3946 warning ("%s", _(msgid));
3947 ofwhat = print_spelling (alloca (spelling_length () + 1));
3949 warning ("(near initialization for `%s')", ofwhat);
3952 /* Digest the parser output INIT as an initializer for type TYPE.
3953 Return a C expression of type TYPE to represent the initial value.
3955 REQUIRE_CONSTANT requests an error if non-constant initializers or
3956 elements are seen. */
3959 digest_init (tree type, tree init, int require_constant)
3961 enum tree_code code = TREE_CODE (type);
3962 tree inside_init = init;
3964 if (type == error_mark_node
3965 || init == error_mark_node
3966 || TREE_TYPE (init) == error_mark_node)
3967 return error_mark_node;
3969 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3970 /* Do not use STRIP_NOPS here. We do not want an enumerator
3971 whose value is 0 to count as a null pointer constant. */
3972 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3973 inside_init = TREE_OPERAND (init, 0);
3975 inside_init = fold (inside_init);
3977 /* Initialization of an array of chars from a string constant
3978 optionally enclosed in braces. */
3980 if (code == ARRAY_TYPE)
3982 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3983 if ((typ1 == char_type_node
3984 || typ1 == signed_char_type_node
3985 || typ1 == unsigned_char_type_node
3986 || typ1 == unsigned_wchar_type_node
3987 || typ1 == signed_wchar_type_node)
3988 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3990 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3991 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3994 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3996 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3998 error_init ("char-array initialized from wide string");
3999 return error_mark_node;
4001 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4003 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4005 error_init ("int-array initialized from non-wide string");
4006 return error_mark_node;