1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
34 #include "coretypes.h"
38 #include "langhooks.h"
49 /* Nonzero if we've already printed a "missing braces around initializer"
50 message within this initializer. */
51 static int missing_braces_mentioned;
53 static tree qualify_type (tree, tree);
54 static int tagged_types_tu_compatible_p (tree, tree, int);
55 static int comp_target_types (tree, tree, int);
56 static int function_types_compatible_p (tree, tree, int);
57 static int type_lists_compatible_p (tree, tree, int);
58 static tree decl_constant_value_for_broken_optimization (tree);
59 static tree default_function_array_conversion (tree);
60 static tree lookup_field (tree, tree);
61 static tree convert_arguments (tree, tree, tree, tree);
62 static tree pointer_diff (tree, tree);
63 static tree internal_build_compound_expr (tree, int);
64 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
66 static void warn_for_assignment (const char *, const char *, tree, int);
67 static tree valid_compound_expr_initializer (tree, tree);
68 static void push_string (const char *);
69 static void push_member_name (tree);
70 static void push_array_bounds (int);
71 static int spelling_length (void);
72 static char *print_spelling (char *);
73 static void warning_init (const char *);
74 static tree digest_init (tree, tree, int);
75 static void output_init_element (tree, tree, tree, int);
76 static void output_pending_init_elements (int);
77 static int set_designator (int);
78 static void push_range_stack (tree);
79 static void add_pending_init (tree, tree);
80 static void set_nonincremental_init (void);
81 static void set_nonincremental_init_from_string (tree);
82 static tree find_init_member (tree);
84 /* Do `exp = require_complete_type (exp);' to make sure exp
85 does not have an incomplete type. (That includes void types.) */
88 require_complete_type (tree value)
90 tree type = TREE_TYPE (value);
92 if (value == error_mark_node || type == error_mark_node)
93 return error_mark_node;
95 /* First, detect a valid value with a complete type. */
96 if (COMPLETE_TYPE_P (type))
99 c_incomplete_type_error (value, type);
100 return error_mark_node;
103 /* Print an error message for invalid use of an incomplete type.
104 VALUE is the expression that was used (or 0 if that isn't known)
105 and TYPE is the type that was invalid. */
108 c_incomplete_type_error (tree value, tree type)
110 const char *type_code_string;
112 /* Avoid duplicate error message. */
113 if (TREE_CODE (type) == ERROR_MARK)
116 if (value != 0 && (TREE_CODE (value) == VAR_DECL
117 || TREE_CODE (value) == PARM_DECL))
118 error ("`%s' has an incomplete type",
119 IDENTIFIER_POINTER (DECL_NAME (value)));
123 /* We must print an error message. Be clever about what it says. */
125 switch (TREE_CODE (type))
128 type_code_string = "struct";
132 type_code_string = "union";
136 type_code_string = "enum";
140 error ("invalid use of void expression");
144 if (TYPE_DOMAIN (type))
146 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
148 error ("invalid use of flexible array member");
151 type = TREE_TYPE (type);
154 error ("invalid use of array with unspecified bounds");
161 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
162 error ("invalid use of undefined type `%s %s'",
163 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
165 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
166 error ("invalid use of incomplete typedef `%s'",
167 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
171 /* Given a type, apply default promotions wrt unnamed function
172 arguments and return the new type. */
175 c_type_promotes_to (tree type)
177 if (TYPE_MAIN_VARIANT (type) == float_type_node)
178 return double_type_node;
180 if (c_promoting_integer_type_p (type))
182 /* Preserve unsignedness if not really getting any wider. */
183 if (TYPE_UNSIGNED (type)
184 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
185 return unsigned_type_node;
186 return integer_type_node;
192 /* Return a variant of TYPE which has all the type qualifiers of LIKE
193 as well as those of TYPE. */
196 qualify_type (tree type, tree like)
198 return c_build_qualified_type (type,
199 TYPE_QUALS (type) | TYPE_QUALS (like));
202 /* Return the composite type of two compatible types.
204 We assume that comptypes has already been done and returned
205 nonzero; if that isn't so, this may crash. In particular, we
206 assume that qualifiers match. */
209 composite_type (tree t1, tree t2)
211 enum tree_code code1;
212 enum tree_code code2;
215 /* Save time if the two types are the same. */
217 if (t1 == t2) return t1;
219 /* If one type is nonsense, use the other. */
220 if (t1 == error_mark_node)
222 if (t2 == error_mark_node)
225 code1 = TREE_CODE (t1);
226 code2 = TREE_CODE (t2);
228 /* Merge the attributes. */
229 attributes = targetm.merge_type_attributes (t1, t2);
231 /* If one is an enumerated type and the other is the compatible
232 integer type, the composite type might be either of the two
233 (DR#013 question 3). For consistency, use the enumerated type as
234 the composite type. */
236 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
238 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
247 /* For two pointers, do this recursively on the target type. */
249 tree pointed_to_1 = TREE_TYPE (t1);
250 tree pointed_to_2 = TREE_TYPE (t2);
251 tree target = composite_type (pointed_to_1, pointed_to_2);
252 t1 = build_pointer_type (target);
253 return build_type_attribute_variant (t1, attributes);
258 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
259 /* Save space: see if the result is identical to one of the args. */
260 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
261 return build_type_attribute_variant (t1, attributes);
262 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
263 return build_type_attribute_variant (t2, attributes);
264 /* Merge the element types, and have a size if either arg has one. */
265 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
266 return build_type_attribute_variant (t1, attributes);
270 /* Function types: prefer the one that specified arg types.
271 If both do, merge the arg types. Also merge the return types. */
273 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
274 tree p1 = TYPE_ARG_TYPES (t1);
275 tree p2 = TYPE_ARG_TYPES (t2);
280 /* Save space: see if the result is identical to one of the args. */
281 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
282 return build_type_attribute_variant (t1, attributes);
283 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
284 return build_type_attribute_variant (t2, attributes);
286 /* Simple way if one arg fails to specify argument types. */
287 if (TYPE_ARG_TYPES (t1) == 0)
289 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
290 return build_type_attribute_variant (t1, attributes);
292 if (TYPE_ARG_TYPES (t2) == 0)
294 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
295 return build_type_attribute_variant (t1, attributes);
298 /* If both args specify argument types, we must merge the two
299 lists, argument by argument. */
300 /* Tell global_bindings_p to return false so that variable_size
301 doesn't abort on VLAs in parameter types. */
302 c_override_global_bindings_to_false = true;
304 len = list_length (p1);
307 for (i = 0; i < len; i++)
308 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
313 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
315 /* A null type means arg type is not specified.
316 Take whatever the other function type has. */
317 if (TREE_VALUE (p1) == 0)
319 TREE_VALUE (n) = TREE_VALUE (p2);
322 if (TREE_VALUE (p2) == 0)
324 TREE_VALUE (n) = TREE_VALUE (p1);
328 /* Given wait (union {union wait *u; int *i} *)
329 and wait (union wait *),
330 prefer union wait * as type of parm. */
331 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
332 && TREE_VALUE (p1) != TREE_VALUE (p2))
335 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
336 memb; memb = TREE_CHAIN (memb))
337 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
340 TREE_VALUE (n) = TREE_VALUE (p2);
342 pedwarn ("function types not truly compatible in ISO C");
346 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
347 && TREE_VALUE (p2) != TREE_VALUE (p1))
350 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
351 memb; memb = TREE_CHAIN (memb))
352 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
355 TREE_VALUE (n) = TREE_VALUE (p1);
357 pedwarn ("function types not truly compatible in ISO C");
361 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
365 c_override_global_bindings_to_false = false;
366 t1 = build_function_type (valtype, newargs);
367 /* ... falls through ... */
371 return build_type_attribute_variant (t1, attributes);
376 /* Return the type of a conditional expression between pointers to
377 possibly differently qualified versions of compatible types.
379 We assume that comp_target_types has already been done and returned
380 nonzero; if that isn't so, this may crash. */
383 common_pointer_type (tree t1, tree t2)
390 /* Save time if the two types are the same. */
392 if (t1 == t2) return t1;
394 /* If one type is nonsense, use the other. */
395 if (t1 == error_mark_node)
397 if (t2 == error_mark_node)
400 if (TREE_CODE (t1) != POINTER_TYPE || TREE_CODE (t2) != POINTER_TYPE)
403 /* Merge the attributes. */
404 attributes = targetm.merge_type_attributes (t1, t2);
406 /* Find the composite type of the target types, and combine the
407 qualifiers of the two types' targets. */
408 pointed_to_1 = TREE_TYPE (t1);
409 pointed_to_2 = TREE_TYPE (t2);
410 target = composite_type (TYPE_MAIN_VARIANT (pointed_to_1),
411 TYPE_MAIN_VARIANT (pointed_to_2));
412 t1 = build_pointer_type (c_build_qualified_type
414 TYPE_QUALS (pointed_to_1) |
415 TYPE_QUALS (pointed_to_2)));
416 return build_type_attribute_variant (t1, attributes);
419 /* Return the common type for two arithmetic types under the usual
420 arithmetic conversions. The default conversions have already been
421 applied, and enumerated types converted to their compatible integer
422 types. The resulting type is unqualified and has no attributes.
424 This is the type for the result of most arithmetic operations
425 if the operands have the given two types. */
428 common_type (tree t1, tree t2)
430 enum tree_code code1;
431 enum tree_code code2;
433 /* If one type is nonsense, use the other. */
434 if (t1 == error_mark_node)
436 if (t2 == error_mark_node)
439 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
440 t1 = TYPE_MAIN_VARIANT (t1);
442 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
443 t2 = TYPE_MAIN_VARIANT (t2);
445 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
446 t1 = build_type_attribute_variant (t1, NULL_TREE);
448 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
449 t2 = build_type_attribute_variant (t2, NULL_TREE);
451 /* Save time if the two types are the same. */
453 if (t1 == t2) return t1;
455 code1 = TREE_CODE (t1);
456 code2 = TREE_CODE (t2);
458 if (code1 != VECTOR_TYPE && code1 != COMPLEX_TYPE
459 && code1 != REAL_TYPE && code1 != INTEGER_TYPE)
462 if (code2 != VECTOR_TYPE && code2 != COMPLEX_TYPE
463 && code2 != REAL_TYPE && code2 != INTEGER_TYPE)
466 /* If one type is a vector type, return that type. (How the usual
467 arithmetic conversions apply to the vector types extension is not
468 precisely specified.) */
469 if (code1 == VECTOR_TYPE)
472 if (code2 == VECTOR_TYPE)
475 /* If one type is complex, form the common type of the non-complex
476 components, then make that complex. Use T1 or T2 if it is the
478 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
480 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
481 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
482 tree subtype = common_type (subtype1, subtype2);
484 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
486 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
489 return build_complex_type (subtype);
492 /* If only one is real, use it as the result. */
494 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
497 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
500 /* Both real or both integers; use the one with greater precision. */
502 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
504 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
507 /* Same precision. Prefer long longs to longs to ints when the
508 same precision, following the C99 rules on integer type rank
509 (which are equivalent to the C90 rules for C90 types). */
511 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
512 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
513 return long_long_unsigned_type_node;
515 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
516 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
518 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
519 return long_long_unsigned_type_node;
521 return long_long_integer_type_node;
524 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
525 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
526 return long_unsigned_type_node;
528 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
529 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
531 /* But preserve unsignedness from the other type,
532 since long cannot hold all the values of an unsigned int. */
533 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
534 return long_unsigned_type_node;
536 return long_integer_type_node;
539 /* Likewise, prefer long double to double even if same size. */
540 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
541 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
542 return long_double_type_node;
544 /* Otherwise prefer the unsigned one. */
546 if (TYPE_UNSIGNED (t1))
552 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
553 or various other operations. Return 2 if they are compatible
554 but a warning may be needed if you use them together. */
557 comptypes (tree type1, tree type2, int flags)
563 /* Suppress errors caused by previously reported errors. */
565 if (t1 == t2 || !t1 || !t2
566 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
569 /* If either type is the internal version of sizetype, return the
571 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
572 && TYPE_ORIG_SIZE_TYPE (t1))
573 t1 = TYPE_ORIG_SIZE_TYPE (t1);
575 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
576 && TYPE_ORIG_SIZE_TYPE (t2))
577 t2 = TYPE_ORIG_SIZE_TYPE (t2);
580 /* Enumerated types are compatible with integer types, but this is
581 not transitive: two enumerated types in the same translation unit
582 are compatible with each other only if they are the same type. */
584 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
585 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
586 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
587 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
592 /* Different classes of types can't be compatible. */
594 if (TREE_CODE (t1) != TREE_CODE (t2))
597 /* Qualifiers must match. */
599 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
602 /* Allow for two different type nodes which have essentially the same
603 definition. Note that we already checked for equality of the type
604 qualifiers (just above). */
606 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
609 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
610 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
613 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
616 switch (TREE_CODE (t1))
619 /* We must give ObjC the first crack at comparing pointers, since
620 protocol qualifiers may be involved. */
621 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
623 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
624 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
628 val = function_types_compatible_p (t1, t2, flags);
633 tree d1 = TYPE_DOMAIN (t1);
634 tree d2 = TYPE_DOMAIN (t2);
635 bool d1_variable, d2_variable;
636 bool d1_zero, d2_zero;
639 /* Target types must match incl. qualifiers. */
640 if (TREE_TYPE (t1) != TREE_TYPE (t2)
641 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
645 /* Sizes must match unless one is missing or variable. */
646 if (d1 == 0 || d2 == 0 || d1 == d2)
649 d1_zero = ! TYPE_MAX_VALUE (d1);
650 d2_zero = ! TYPE_MAX_VALUE (d2);
652 d1_variable = (! d1_zero
653 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
654 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
655 d2_variable = (! d2_zero
656 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
657 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
659 if (d1_variable || d2_variable)
661 if (d1_zero && d2_zero)
663 if (d1_zero || d2_zero
664 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
665 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
672 /* We are dealing with two distinct structs. In assorted Objective-C
673 corner cases, however, these can still be deemed equivalent. */
674 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
679 if (val != 1 && !same_translation_unit_p (t1, t2))
680 val = tagged_types_tu_compatible_p (t1, t2, flags);
684 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
685 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), 0);
691 return attrval == 2 && val == 1 ? 2 : val;
694 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
695 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
696 to 1 or 0 depending if the check of the pointer types is meant to
697 be reflexive or not (typically, assignments are not reflexive,
698 while comparisons are reflexive).
702 comp_target_types (tree ttl, tree ttr, int reflexive)
706 /* Give objc_comptypes a crack at letting these types through. */
707 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
710 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
711 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
713 if (val == 2 && pedantic)
714 pedwarn ("types are not quite compatible");
718 /* Subroutines of `comptypes'. */
720 /* Determine whether two trees derive from the same translation unit.
721 If the CONTEXT chain ends in a null, that tree's context is still
722 being parsed, so if two trees have context chains ending in null,
723 they're in the same translation unit. */
725 same_translation_unit_p (tree t1, tree t2)
727 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
728 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
730 case 'd': t1 = DECL_CONTEXT (t1); break;
731 case 't': t1 = TYPE_CONTEXT (t1); break;
732 case 'x': t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
736 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
737 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
739 case 'd': t2 = DECL_CONTEXT (t2); break;
740 case 't': t2 = TYPE_CONTEXT (t2); break;
741 case 'x': t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
748 /* The C standard says that two structures in different translation
749 units are compatible with each other only if the types of their
750 fields are compatible (among other things). So, consider two copies
751 of this structure: */
753 struct tagged_tu_seen {
754 const struct tagged_tu_seen * next;
759 /* Can they be compatible with each other? We choose to break the
760 recursion by allowing those types to be compatible. */
762 static const struct tagged_tu_seen * tagged_tu_seen_base;
764 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
765 compatible. If the two types are not the same (which has been
766 checked earlier), this can only happen when multiple translation
767 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
771 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
774 bool needs_warning = false;
776 /* We have to verify that the tags of the types are the same. This
777 is harder than it looks because this may be a typedef, so we have
778 to go look at the original type. It may even be a typedef of a
780 In the case of compiler-created builtin structs the TYPE_DECL
781 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
782 while (TYPE_NAME (t1)
783 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
784 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
785 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
787 while (TYPE_NAME (t2)
788 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
789 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
790 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
792 /* C90 didn't have the requirement that the two tags be the same. */
793 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
796 /* C90 didn't say what happened if one or both of the types were
797 incomplete; we choose to follow C99 rules here, which is that they
799 if (TYPE_SIZE (t1) == NULL
800 || TYPE_SIZE (t2) == NULL)
804 const struct tagged_tu_seen * tts_i;
805 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
806 if (tts_i->t1 == t1 && tts_i->t2 == t2)
810 switch (TREE_CODE (t1))
815 /* Speed up the case where the type values are in the same order. */
816 tree tv1 = TYPE_VALUES (t1);
817 tree tv2 = TYPE_VALUES (t2);
822 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
824 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
826 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
830 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
832 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
835 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
838 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
840 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
842 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
850 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
853 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
856 struct tagged_tu_seen tts;
858 tts.next = tagged_tu_seen_base;
861 tagged_tu_seen_base = &tts;
863 if (DECL_NAME (s1) != NULL)
864 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
865 if (DECL_NAME (s1) == DECL_NAME (s2))
868 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
872 needs_warning = true;
874 if (TREE_CODE (s1) == FIELD_DECL
875 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
876 DECL_FIELD_BIT_OFFSET (s2)) != 1)
882 tagged_tu_seen_base = tts.next;
886 return needs_warning ? 2 : 1;
891 struct tagged_tu_seen tts;
893 tts.next = tagged_tu_seen_base;
896 tagged_tu_seen_base = &tts;
898 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
900 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
903 if (TREE_CODE (s1) != TREE_CODE (s2)
904 || DECL_NAME (s1) != DECL_NAME (s2))
906 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
910 needs_warning = true;
912 if (TREE_CODE (s1) == FIELD_DECL
913 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
914 DECL_FIELD_BIT_OFFSET (s2)) != 1)
917 tagged_tu_seen_base = tts.next;
920 return needs_warning ? 2 : 1;
928 /* Return 1 if two function types F1 and F2 are compatible.
929 If either type specifies no argument types,
930 the other must specify a fixed number of self-promoting arg types.
931 Otherwise, if one type specifies only the number of arguments,
932 the other must specify that number of self-promoting arg types.
933 Otherwise, the argument types must match. */
936 function_types_compatible_p (tree f1, tree f2, int flags)
939 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
944 ret1 = TREE_TYPE (f1);
945 ret2 = TREE_TYPE (f2);
947 /* 'volatile' qualifiers on a function's return type mean the function
949 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
950 pedwarn ("function return types not compatible due to `volatile'");
951 if (TYPE_VOLATILE (ret1))
952 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
953 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
954 if (TYPE_VOLATILE (ret2))
955 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
956 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
957 val = comptypes (ret1, ret2, flags);
961 args1 = TYPE_ARG_TYPES (f1);
962 args2 = TYPE_ARG_TYPES (f2);
964 /* An unspecified parmlist matches any specified parmlist
965 whose argument types don't need default promotions. */
969 if (!self_promoting_args_p (args2))
971 /* If one of these types comes from a non-prototype fn definition,
972 compare that with the other type's arglist.
973 If they don't match, ask for a warning (but no error). */
974 if (TYPE_ACTUAL_ARG_TYPES (f1)
975 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
982 if (!self_promoting_args_p (args1))
984 if (TYPE_ACTUAL_ARG_TYPES (f2)
985 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
991 /* Both types have argument lists: compare them and propagate results. */
992 val1 = type_lists_compatible_p (args1, args2, flags);
993 return val1 != 1 ? val1 : val;
996 /* Check two lists of types for compatibility,
997 returning 0 for incompatible, 1 for compatible,
998 or 2 for compatible with warning. */
1001 type_lists_compatible_p (tree args1, tree args2, int flags)
1003 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1009 if (args1 == 0 && args2 == 0)
1011 /* If one list is shorter than the other,
1012 they fail to match. */
1013 if (args1 == 0 || args2 == 0)
1015 /* A null pointer instead of a type
1016 means there is supposed to be an argument
1017 but nothing is specified about what type it has.
1018 So match anything that self-promotes. */
1019 if (TREE_VALUE (args1) == 0)
1021 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
1024 else if (TREE_VALUE (args2) == 0)
1026 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
1029 /* If one of the lists has an error marker, ignore this arg. */
1030 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
1031 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
1033 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
1034 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
1037 /* Allow wait (union {union wait *u; int *i} *)
1038 and wait (union wait *) to be compatible. */
1039 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
1040 && (TYPE_NAME (TREE_VALUE (args1)) == 0
1041 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
1042 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
1043 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
1044 TYPE_SIZE (TREE_VALUE (args2))))
1047 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
1048 memb; memb = TREE_CHAIN (memb))
1049 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
1055 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
1056 && (TYPE_NAME (TREE_VALUE (args2)) == 0
1057 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
1058 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
1059 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
1060 TYPE_SIZE (TREE_VALUE (args1))))
1063 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
1064 memb; memb = TREE_CHAIN (memb))
1065 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
1075 /* comptypes said ok, but record if it said to warn. */
1079 args1 = TREE_CHAIN (args1);
1080 args2 = TREE_CHAIN (args2);
1084 /* Compute the size to increment a pointer by. */
1087 c_size_in_bytes (tree type)
1089 enum tree_code code = TREE_CODE (type);
1091 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1092 return size_one_node;
1094 if (!COMPLETE_OR_VOID_TYPE_P (type))
1096 error ("arithmetic on pointer to an incomplete type");
1097 return size_one_node;
1100 /* Convert in case a char is more than one unit. */
1101 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1102 size_int (TYPE_PRECISION (char_type_node)
1106 /* Return either DECL or its known constant value (if it has one). */
1109 decl_constant_value (tree decl)
1111 if (/* Don't change a variable array bound or initial value to a constant
1112 in a place where a variable is invalid. Note that DECL_INITIAL
1113 isn't valid for a PARM_DECL. */
1114 current_function_decl != 0
1115 && TREE_CODE (decl) != PARM_DECL
1116 && ! TREE_THIS_VOLATILE (decl)
1117 && TREE_READONLY (decl)
1118 && DECL_INITIAL (decl) != 0
1119 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1120 /* This is invalid if initial value is not constant.
1121 If it has either a function call, a memory reference,
1122 or a variable, then re-evaluating it could give different results. */
1123 && TREE_CONSTANT (DECL_INITIAL (decl))
1124 /* Check for cases where this is sub-optimal, even though valid. */
1125 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1126 return DECL_INITIAL (decl);
1130 /* Return either DECL or its known constant value (if it has one), but
1131 return DECL if pedantic or DECL has mode BLKmode. This is for
1132 bug-compatibility with the old behavior of decl_constant_value
1133 (before GCC 3.0); every use of this function is a bug and it should
1134 be removed before GCC 3.1. It is not appropriate to use pedantic
1135 in a way that affects optimization, and BLKmode is probably not the
1136 right test for avoiding misoptimizations either. */
1139 decl_constant_value_for_broken_optimization (tree decl)
1141 if (pedantic || DECL_MODE (decl) == BLKmode)
1144 return decl_constant_value (decl);
1148 /* Perform the default conversion of arrays and functions to pointers.
1149 Return the result of converting EXP. For any other expression, just
1153 default_function_array_conversion (tree exp)
1156 tree type = TREE_TYPE (exp);
1157 enum tree_code code = TREE_CODE (type);
1160 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1163 Do not use STRIP_NOPS here! It will remove conversions from pointer
1164 to integer and cause infinite recursion. */
1166 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1167 || (TREE_CODE (exp) == NOP_EXPR
1168 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1170 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1172 exp = TREE_OPERAND (exp, 0);
1175 /* Preserve the original expression code. */
1176 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1177 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1179 if (code == FUNCTION_TYPE)
1181 return build_unary_op (ADDR_EXPR, exp, 0);
1183 if (code == ARRAY_TYPE)
1186 tree restype = TREE_TYPE (type);
1192 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1194 constp = TREE_READONLY (exp);
1195 volatilep = TREE_THIS_VOLATILE (exp);
1198 if (TYPE_QUALS (type) || constp || volatilep)
1200 = c_build_qualified_type (restype,
1202 | (constp * TYPE_QUAL_CONST)
1203 | (volatilep * TYPE_QUAL_VOLATILE));
1205 if (TREE_CODE (exp) == INDIRECT_REF)
1206 return convert (TYPE_POINTER_TO (restype),
1207 TREE_OPERAND (exp, 0));
1209 if (TREE_CODE (exp) == COMPOUND_EXPR)
1211 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1212 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1213 TREE_OPERAND (exp, 0), op1);
1216 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1217 if (!flag_isoc99 && !lvalue_array_p)
1219 /* Before C99, non-lvalue arrays do not decay to pointers.
1220 Normally, using such an array would be invalid; but it can
1221 be used correctly inside sizeof or as a statement expression.
1222 Thus, do not give an error here; an error will result later. */
1226 ptrtype = build_pointer_type (restype);
1228 if (TREE_CODE (exp) == VAR_DECL)
1230 /* ??? This is not really quite correct
1231 in that the type of the operand of ADDR_EXPR
1232 is not the target type of the type of the ADDR_EXPR itself.
1233 Question is, can this lossage be avoided? */
1234 adr = build1 (ADDR_EXPR, ptrtype, exp);
1235 if (!c_mark_addressable (exp))
1236 return error_mark_node;
1237 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1240 /* This way is better for a COMPONENT_REF since it can
1241 simplify the offset for a component. */
1242 adr = build_unary_op (ADDR_EXPR, exp, 1);
1243 return convert (ptrtype, adr);
1248 /* Perform default promotions for C data used in expressions.
1249 Arrays and functions are converted to pointers;
1250 enumeral types or short or char, to int.
1251 In addition, manifest constants symbols are replaced by their values. */
1254 default_conversion (tree exp)
1257 tree type = TREE_TYPE (exp);
1258 enum tree_code code = TREE_CODE (type);
1260 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1261 return default_function_array_conversion (exp);
1263 /* Constants can be used directly unless they're not loadable. */
1264 if (TREE_CODE (exp) == CONST_DECL)
1265 exp = DECL_INITIAL (exp);
1267 /* Replace a nonvolatile const static variable with its value unless
1268 it is an array, in which case we must be sure that taking the
1269 address of the array produces consistent results. */
1270 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1272 exp = decl_constant_value_for_broken_optimization (exp);
1273 type = TREE_TYPE (exp);
1276 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1279 Do not use STRIP_NOPS here! It will remove conversions from pointer
1280 to integer and cause infinite recursion. */
1282 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1283 || (TREE_CODE (exp) == NOP_EXPR
1284 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1285 exp = TREE_OPERAND (exp, 0);
1287 /* Preserve the original expression code. */
1288 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1289 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1291 /* Normally convert enums to int,
1292 but convert wide enums to something wider. */
1293 if (code == ENUMERAL_TYPE)
1295 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1296 TYPE_PRECISION (integer_type_node)),
1297 ((TYPE_PRECISION (type)
1298 >= TYPE_PRECISION (integer_type_node))
1299 && TYPE_UNSIGNED (type)));
1301 return convert (type, exp);
1304 if (TREE_CODE (exp) == COMPONENT_REF
1305 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1306 /* If it's thinner than an int, promote it like a
1307 c_promoting_integer_type_p, otherwise leave it alone. */
1308 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1309 TYPE_PRECISION (integer_type_node)))
1310 return convert (integer_type_node, exp);
1312 if (c_promoting_integer_type_p (type))
1314 /* Preserve unsignedness if not really getting any wider. */
1315 if (TYPE_UNSIGNED (type)
1316 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1317 return convert (unsigned_type_node, exp);
1319 return convert (integer_type_node, exp);
1322 if (code == VOID_TYPE)
1324 error ("void value not ignored as it ought to be");
1325 return error_mark_node;
1330 /* Look up COMPONENT in a structure or union DECL.
1332 If the component name is not found, returns NULL_TREE. Otherwise,
1333 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1334 stepping down the chain to the component, which is in the last
1335 TREE_VALUE of the list. Normally the list is of length one, but if
1336 the component is embedded within (nested) anonymous structures or
1337 unions, the list steps down the chain to the component. */
1340 lookup_field (tree decl, tree component)
1342 tree type = TREE_TYPE (decl);
1345 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1346 to the field elements. Use a binary search on this array to quickly
1347 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1348 will always be set for structures which have many elements. */
1350 if (TYPE_LANG_SPECIFIC (type))
1353 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1355 field = TYPE_FIELDS (type);
1357 top = TYPE_LANG_SPECIFIC (type)->s->len;
1358 while (top - bot > 1)
1360 half = (top - bot + 1) >> 1;
1361 field = field_array[bot+half];
1363 if (DECL_NAME (field) == NULL_TREE)
1365 /* Step through all anon unions in linear fashion. */
1366 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1368 field = field_array[bot++];
1369 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1370 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1372 tree anon = lookup_field (field, component);
1375 return tree_cons (NULL_TREE, field, anon);
1379 /* Entire record is only anon unions. */
1383 /* Restart the binary search, with new lower bound. */
1387 if (DECL_NAME (field) == component)
1389 if (DECL_NAME (field) < component)
1395 if (DECL_NAME (field_array[bot]) == component)
1396 field = field_array[bot];
1397 else if (DECL_NAME (field) != component)
1402 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1404 if (DECL_NAME (field) == NULL_TREE
1405 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1406 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1408 tree anon = lookup_field (field, component);
1411 return tree_cons (NULL_TREE, field, anon);
1414 if (DECL_NAME (field) == component)
1418 if (field == NULL_TREE)
1422 return tree_cons (NULL_TREE, field, NULL_TREE);
1425 /* Make an expression to refer to the COMPONENT field of
1426 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1429 build_component_ref (tree datum, tree component)
1431 tree type = TREE_TYPE (datum);
1432 enum tree_code code = TREE_CODE (type);
1436 if (!objc_is_public (datum, component))
1437 return error_mark_node;
1439 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1440 Ensure that the arguments are not lvalues; otherwise,
1441 if the component is an array, it would wrongly decay to a pointer in
1443 We cannot do this with a COND_EXPR, because in a conditional expression
1444 the default promotions are applied to both sides, and this would yield
1445 the wrong type of the result; for example, if the components have
1447 switch (TREE_CODE (datum))
1451 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1452 return build (COMPOUND_EXPR, TREE_TYPE (value),
1453 TREE_OPERAND (datum, 0), non_lvalue (value));
1459 /* See if there is a field or component with name COMPONENT. */
1461 if (code == RECORD_TYPE || code == UNION_TYPE)
1463 if (!COMPLETE_TYPE_P (type))
1465 c_incomplete_type_error (NULL_TREE, type);
1466 return error_mark_node;
1469 field = lookup_field (datum, component);
1473 error ("%s has no member named `%s'",
1474 code == RECORD_TYPE ? "structure" : "union",
1475 IDENTIFIER_POINTER (component));
1476 return error_mark_node;
1479 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1480 This might be better solved in future the way the C++ front
1481 end does it - by giving the anonymous entities each a
1482 separate name and type, and then have build_component_ref
1483 recursively call itself. We can't do that here. */
1486 tree subdatum = TREE_VALUE (field);
1488 if (TREE_TYPE (subdatum) == error_mark_node)
1489 return error_mark_node;
1491 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1492 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1493 TREE_READONLY (ref) = 1;
1494 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1495 TREE_THIS_VOLATILE (ref) = 1;
1497 if (TREE_DEPRECATED (subdatum))
1498 warn_deprecated_use (subdatum);
1502 field = TREE_CHAIN (field);
1508 else if (code != ERROR_MARK)
1509 error ("request for member `%s' in something not a structure or union",
1510 IDENTIFIER_POINTER (component));
1512 return error_mark_node;
1515 /* Given an expression PTR for a pointer, return an expression
1516 for the value pointed to.
1517 ERRORSTRING is the name of the operator to appear in error messages. */
1520 build_indirect_ref (tree ptr, const char *errorstring)
1522 tree pointer = default_conversion (ptr);
1523 tree type = TREE_TYPE (pointer);
1525 if (TREE_CODE (type) == POINTER_TYPE)
1527 if (TREE_CODE (pointer) == ADDR_EXPR
1528 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1529 == TREE_TYPE (type)))
1530 return TREE_OPERAND (pointer, 0);
1533 tree t = TREE_TYPE (type);
1534 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1536 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1538 error ("dereferencing pointer to incomplete type");
1539 return error_mark_node;
1541 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1542 warning ("dereferencing `void *' pointer");
1544 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1545 so that we get the proper error message if the result is used
1546 to assign to. Also, &* is supposed to be a no-op.
1547 And ANSI C seems to specify that the type of the result
1548 should be the const type. */
1549 /* A de-reference of a pointer to const is not a const. It is valid
1550 to change it via some other pointer. */
1551 TREE_READONLY (ref) = TYPE_READONLY (t);
1552 TREE_SIDE_EFFECTS (ref)
1553 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1554 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1558 else if (TREE_CODE (pointer) != ERROR_MARK)
1559 error ("invalid type argument of `%s'", errorstring);
1560 return error_mark_node;
1563 /* This handles expressions of the form "a[i]", which denotes
1566 This is logically equivalent in C to *(a+i), but we may do it differently.
1567 If A is a variable or a member, we generate a primitive ARRAY_REF.
1568 This avoids forcing the array out of registers, and can work on
1569 arrays that are not lvalues (for example, members of structures returned
1573 build_array_ref (tree array, tree index)
1577 error ("subscript missing in array reference");
1578 return error_mark_node;
1581 if (TREE_TYPE (array) == error_mark_node
1582 || TREE_TYPE (index) == error_mark_node)
1583 return error_mark_node;
1585 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1589 /* Subscripting with type char is likely to lose
1590 on a machine where chars are signed.
1591 So warn on any machine, but optionally.
1592 Don't warn for unsigned char since that type is safe.
1593 Don't warn for signed char because anyone who uses that
1594 must have done so deliberately. */
1595 if (warn_char_subscripts
1596 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1597 warning ("array subscript has type `char'");
1599 /* Apply default promotions *after* noticing character types. */
1600 index = default_conversion (index);
1602 /* Require integer *after* promotion, for sake of enums. */
1603 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1605 error ("array subscript is not an integer");
1606 return error_mark_node;
1609 /* An array that is indexed by a non-constant
1610 cannot be stored in a register; we must be able to do
1611 address arithmetic on its address.
1612 Likewise an array of elements of variable size. */
1613 if (TREE_CODE (index) != INTEGER_CST
1614 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1615 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1617 if (!c_mark_addressable (array))
1618 return error_mark_node;
1620 /* An array that is indexed by a constant value which is not within
1621 the array bounds cannot be stored in a register either; because we
1622 would get a crash in store_bit_field/extract_bit_field when trying
1623 to access a non-existent part of the register. */
1624 if (TREE_CODE (index) == INTEGER_CST
1625 && TYPE_DOMAIN (TREE_TYPE (array))
1626 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1628 if (!c_mark_addressable (array))
1629 return error_mark_node;
1635 while (TREE_CODE (foo) == COMPONENT_REF)
1636 foo = TREE_OPERAND (foo, 0);
1637 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1638 pedwarn ("ISO C forbids subscripting `register' array");
1639 else if (! flag_isoc99 && ! lvalue_p (foo))
1640 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1643 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1644 rval = build (ARRAY_REF, type, array, index);
1645 /* Array ref is const/volatile if the array elements are
1646 or if the array is. */
1647 TREE_READONLY (rval)
1648 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1649 | TREE_READONLY (array));
1650 TREE_SIDE_EFFECTS (rval)
1651 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1652 | TREE_SIDE_EFFECTS (array));
1653 TREE_THIS_VOLATILE (rval)
1654 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1655 /* This was added by rms on 16 Nov 91.
1656 It fixes vol struct foo *a; a->elts[1]
1657 in an inline function.
1658 Hope it doesn't break something else. */
1659 | TREE_THIS_VOLATILE (array));
1660 return require_complete_type (fold (rval));
1664 tree ar = default_conversion (array);
1665 tree ind = default_conversion (index);
1667 /* Do the same warning check as above, but only on the part that's
1668 syntactically the index and only if it is also semantically
1670 if (warn_char_subscripts
1671 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1672 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1673 warning ("subscript has type `char'");
1675 /* Put the integer in IND to simplify error checking. */
1676 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1683 if (ar == error_mark_node)
1686 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1687 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1689 error ("subscripted value is neither array nor pointer");
1690 return error_mark_node;
1692 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1694 error ("array subscript is not an integer");
1695 return error_mark_node;
1698 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1703 /* Build an external reference to identifier ID. FUN indicates
1704 whether this will be used for a function call. */
1706 build_external_ref (tree id, int fun)
1709 tree decl = lookup_name (id);
1710 tree objc_ivar = lookup_objc_ivar (id);
1712 if (decl && decl != error_mark_node)
1714 /* Properly declared variable or function reference. */
1717 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1719 warning ("local declaration of `%s' hides instance variable",
1720 IDENTIFIER_POINTER (id));
1729 /* Implicit function declaration. */
1730 ref = implicitly_declare (id);
1731 else if (decl == error_mark_node)
1732 /* Don't complain about something that's already been
1733 complained about. */
1734 return error_mark_node;
1737 undeclared_variable (id);
1738 return error_mark_node;
1741 if (TREE_TYPE (ref) == error_mark_node)
1742 return error_mark_node;
1744 if (TREE_DEPRECATED (ref))
1745 warn_deprecated_use (ref);
1747 if (!skip_evaluation)
1748 assemble_external (ref);
1749 TREE_USED (ref) = 1;
1751 if (TREE_CODE (ref) == CONST_DECL)
1753 ref = DECL_INITIAL (ref);
1754 TREE_CONSTANT (ref) = 1;
1755 TREE_INVARIANT (ref) = 1;
1757 else if (current_function_decl != 0
1758 && !DECL_FILE_SCOPE_P (current_function_decl)
1759 && (TREE_CODE (ref) == VAR_DECL
1760 || TREE_CODE (ref) == PARM_DECL
1761 || TREE_CODE (ref) == FUNCTION_DECL))
1763 tree context = decl_function_context (ref);
1765 if (context != 0 && context != current_function_decl)
1766 DECL_NONLOCAL (ref) = 1;
1772 /* Build a function call to function FUNCTION with parameters PARAMS.
1773 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1774 TREE_VALUE of each node is a parameter-expression.
1775 FUNCTION's data type may be a function type or a pointer-to-function. */
1778 build_function_call (tree function, tree params)
1780 tree fntype, fundecl = 0;
1781 tree coerced_params;
1782 tree name = NULL_TREE, result;
1785 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1786 STRIP_TYPE_NOPS (function);
1788 /* Convert anything with function type to a pointer-to-function. */
1789 if (TREE_CODE (function) == FUNCTION_DECL)
1791 name = DECL_NAME (function);
1793 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1794 (because calling an inline function does not mean the function
1795 needs to be separately compiled). */
1796 fntype = build_type_variant (TREE_TYPE (function),
1797 TREE_READONLY (function),
1798 TREE_THIS_VOLATILE (function));
1800 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1803 function = default_conversion (function);
1805 fntype = TREE_TYPE (function);
1807 if (TREE_CODE (fntype) == ERROR_MARK)
1808 return error_mark_node;
1810 if (!(TREE_CODE (fntype) == POINTER_TYPE
1811 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1813 error ("called object is not a function");
1814 return error_mark_node;
1817 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1818 current_function_returns_abnormally = 1;
1820 /* fntype now gets the type of function pointed to. */
1821 fntype = TREE_TYPE (fntype);
1823 /* Check that the function is called through a compatible prototype.
1824 If it is not, replace the call by a trap, wrapped up in a compound
1825 expression if necessary. This has the nice side-effect to prevent
1826 the tree-inliner from generating invalid assignment trees which may
1827 blow up in the RTL expander later.
1829 ??? This doesn't work for Objective-C because objc_comptypes
1830 refuses to compare function prototypes, yet the compiler appears
1831 to build calls that are flagged as invalid by C's comptypes. */
1832 if (! c_dialect_objc ()
1833 && TREE_CODE (function) == NOP_EXPR
1834 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1835 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1836 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1838 tree return_type = TREE_TYPE (fntype);
1839 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1842 /* This situation leads to run-time undefined behavior. We can't,
1843 therefore, simply error unless we can prove that all possible
1844 executions of the program must execute the code. */
1845 warning ("function called through a non-compatible type");
1847 /* We can, however, treat "undefined" any way we please.
1848 Call abort to encourage the user to fix the program. */
1849 inform ("if this code is reached, the program will abort");
1851 if (VOID_TYPE_P (return_type))
1857 if (AGGREGATE_TYPE_P (return_type))
1858 rhs = build_compound_literal (return_type,
1859 build_constructor (return_type,
1862 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1864 return build (COMPOUND_EXPR, return_type, trap, rhs);
1868 /* Convert the parameters to the types declared in the
1869 function prototype, or apply default promotions. */
1872 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1874 /* Check that the arguments to the function are valid. */
1876 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1878 /* Recognize certain built-in functions so we can make tree-codes
1879 other than CALL_EXPR. We do this when it enables fold-const.c
1880 to do something useful. */
1882 if (TREE_CODE (function) == ADDR_EXPR
1883 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1884 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1886 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1887 params, coerced_params);
1892 result = build (CALL_EXPR, TREE_TYPE (fntype),
1893 function, coerced_params, NULL_TREE);
1894 TREE_SIDE_EFFECTS (result) = 1;
1895 result = fold (result);
1897 if (VOID_TYPE_P (TREE_TYPE (result)))
1899 return require_complete_type (result);
1902 /* Convert the argument expressions in the list VALUES
1903 to the types in the list TYPELIST. The result is a list of converted
1904 argument expressions.
1906 If TYPELIST is exhausted, or when an element has NULL as its type,
1907 perform the default conversions.
1909 PARMLIST is the chain of parm decls for the function being called.
1910 It may be 0, if that info is not available.
1911 It is used only for generating error messages.
1913 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1915 This is also where warnings about wrong number of args are generated.
1917 Both VALUES and the returned value are chains of TREE_LIST nodes
1918 with the elements of the list in the TREE_VALUE slots of those nodes. */
1921 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1923 tree typetail, valtail;
1927 /* Scan the given expressions and types, producing individual
1928 converted arguments and pushing them on RESULT in reverse order. */
1930 for (valtail = values, typetail = typelist, parmnum = 0;
1932 valtail = TREE_CHAIN (valtail), parmnum++)
1934 tree type = typetail ? TREE_VALUE (typetail) : 0;
1935 tree val = TREE_VALUE (valtail);
1937 if (type == void_type_node)
1940 error ("too many arguments to function `%s'",
1941 IDENTIFIER_POINTER (name));
1943 error ("too many arguments to function");
1947 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1948 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1949 to convert automatically to a pointer. */
1950 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1951 val = TREE_OPERAND (val, 0);
1953 val = default_function_array_conversion (val);
1955 val = require_complete_type (val);
1959 /* Formal parm type is specified by a function prototype. */
1962 if (!COMPLETE_TYPE_P (type))
1964 error ("type of formal parameter %d is incomplete", parmnum + 1);
1969 /* Optionally warn about conversions that
1970 differ from the default conversions. */
1971 if (warn_conversion || warn_traditional)
1973 int formal_prec = TYPE_PRECISION (type);
1975 if (INTEGRAL_TYPE_P (type)
1976 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1977 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1978 if (INTEGRAL_TYPE_P (type)
1979 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1980 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1981 else if (TREE_CODE (type) == COMPLEX_TYPE
1982 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1983 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1984 else if (TREE_CODE (type) == REAL_TYPE
1985 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1986 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1987 else if (TREE_CODE (type) == COMPLEX_TYPE
1988 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1989 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1990 else if (TREE_CODE (type) == REAL_TYPE
1991 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1992 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1993 /* ??? At some point, messages should be written about
1994 conversions between complex types, but that's too messy
1996 else if (TREE_CODE (type) == REAL_TYPE
1997 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1999 /* Warn if any argument is passed as `float',
2000 since without a prototype it would be `double'. */
2001 if (formal_prec == TYPE_PRECISION (float_type_node))
2002 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
2004 /* Detect integer changing in width or signedness.
2005 These warnings are only activated with
2006 -Wconversion, not with -Wtraditional. */
2007 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2008 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2010 tree would_have_been = default_conversion (val);
2011 tree type1 = TREE_TYPE (would_have_been);
2013 if (TREE_CODE (type) == ENUMERAL_TYPE
2014 && (TYPE_MAIN_VARIANT (type)
2015 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2016 /* No warning if function asks for enum
2017 and the actual arg is that enum type. */
2019 else if (formal_prec != TYPE_PRECISION (type1))
2020 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2021 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2023 /* Don't complain if the formal parameter type
2024 is an enum, because we can't tell now whether
2025 the value was an enum--even the same enum. */
2026 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2028 else if (TREE_CODE (val) == INTEGER_CST
2029 && int_fits_type_p (val, type))
2030 /* Change in signedness doesn't matter
2031 if a constant value is unaffected. */
2033 /* Likewise for a constant in a NOP_EXPR. */
2034 else if (TREE_CODE (val) == NOP_EXPR
2035 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2036 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2038 /* If the value is extended from a narrower
2039 unsigned type, it doesn't matter whether we
2040 pass it as signed or unsigned; the value
2041 certainly is the same either way. */
2042 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2043 && TYPE_UNSIGNED (TREE_TYPE (val)))
2045 else if (TYPE_UNSIGNED (type))
2046 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2048 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2052 parmval = convert_for_assignment (type, val,
2053 (char *) 0, /* arg passing */
2054 fundecl, name, parmnum + 1);
2056 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2057 && INTEGRAL_TYPE_P (type)
2058 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2059 parmval = default_conversion (parmval);
2061 result = tree_cons (NULL_TREE, parmval, result);
2063 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2064 && (TYPE_PRECISION (TREE_TYPE (val))
2065 < TYPE_PRECISION (double_type_node)))
2066 /* Convert `float' to `double'. */
2067 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2069 /* Convert `short' and `char' to full-size `int'. */
2070 result = tree_cons (NULL_TREE, default_conversion (val), result);
2073 typetail = TREE_CHAIN (typetail);
2076 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2079 error ("too few arguments to function `%s'",
2080 IDENTIFIER_POINTER (name));
2082 error ("too few arguments to function");
2085 return nreverse (result);
2088 /* This is the entry point used by the parser
2089 for binary operators in the input.
2090 In addition to constructing the expression,
2091 we check for operands that were written with other binary operators
2092 in a way that is likely to confuse the user. */
2095 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
2097 tree result = build_binary_op (code, arg1, arg2, 1);
2100 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2101 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2102 enum tree_code code1 = ERROR_MARK;
2103 enum tree_code code2 = ERROR_MARK;
2105 if (TREE_CODE (result) == ERROR_MARK)
2106 return error_mark_node;
2108 if (IS_EXPR_CODE_CLASS (class1))
2109 code1 = C_EXP_ORIGINAL_CODE (arg1);
2110 if (IS_EXPR_CODE_CLASS (class2))
2111 code2 = C_EXP_ORIGINAL_CODE (arg2);
2113 /* Check for cases such as x+y<<z which users are likely
2114 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2115 is cleared to prevent these warnings. */
2116 if (warn_parentheses)
2118 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2120 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2121 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2122 warning ("suggest parentheses around + or - inside shift");
2125 if (code == TRUTH_ORIF_EXPR)
2127 if (code1 == TRUTH_ANDIF_EXPR
2128 || code2 == TRUTH_ANDIF_EXPR)
2129 warning ("suggest parentheses around && within ||");
2132 if (code == BIT_IOR_EXPR)
2134 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2135 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2136 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2137 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2138 warning ("suggest parentheses around arithmetic in operand of |");
2139 /* Check cases like x|y==z */
2140 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2141 warning ("suggest parentheses around comparison in operand of |");
2144 if (code == BIT_XOR_EXPR)
2146 if (code1 == BIT_AND_EXPR
2147 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2148 || code2 == BIT_AND_EXPR
2149 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2150 warning ("suggest parentheses around arithmetic in operand of ^");
2151 /* Check cases like x^y==z */
2152 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2153 warning ("suggest parentheses around comparison in operand of ^");
2156 if (code == BIT_AND_EXPR)
2158 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2159 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2160 warning ("suggest parentheses around + or - in operand of &");
2161 /* Check cases like x&y==z */
2162 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2163 warning ("suggest parentheses around comparison in operand of &");
2167 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2168 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2169 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2170 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2172 unsigned_conversion_warning (result, arg1);
2173 unsigned_conversion_warning (result, arg2);
2174 overflow_warning (result);
2176 class = TREE_CODE_CLASS (TREE_CODE (result));
2178 /* Record the code that was specified in the source,
2179 for the sake of warnings about confusing nesting. */
2180 if (IS_EXPR_CODE_CLASS (class))
2181 C_SET_EXP_ORIGINAL_CODE (result, code);
2184 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2185 so that convert_for_assignment wouldn't strip it.
2186 That way, we got warnings for things like p = (1 - 1).
2187 But it turns out we should not get those warnings. */
2188 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2189 C_SET_EXP_ORIGINAL_CODE (result, code);
2196 /* Return true if `t' is known to be non-negative. */
2199 c_tree_expr_nonnegative_p (tree t)
2201 if (TREE_CODE (t) == STMT_EXPR)
2203 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2205 /* Find the last statement in the chain, ignoring the final
2206 * scope statement */
2207 while (TREE_CHAIN (t) != NULL_TREE
2208 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2210 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2212 return tree_expr_nonnegative_p (t);
2215 /* Return a tree for the difference of pointers OP0 and OP1.
2216 The resulting tree has type int. */
2219 pointer_diff (tree op0, tree op1)
2221 tree restype = ptrdiff_type_node;
2223 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2224 tree con0, con1, lit0, lit1;
2225 tree orig_op1 = op1;
2227 if (pedantic || warn_pointer_arith)
2229 if (TREE_CODE (target_type) == VOID_TYPE)
2230 pedwarn ("pointer of type `void *' used in subtraction");
2231 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2232 pedwarn ("pointer to a function used in subtraction");
2235 /* If the conversion to ptrdiff_type does anything like widening or
2236 converting a partial to an integral mode, we get a convert_expression
2237 that is in the way to do any simplifications.
2238 (fold-const.c doesn't know that the extra bits won't be needed.
2239 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2240 different mode in place.)
2241 So first try to find a common term here 'by hand'; we want to cover
2242 at least the cases that occur in legal static initializers. */
2243 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2244 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2246 if (TREE_CODE (con0) == PLUS_EXPR)
2248 lit0 = TREE_OPERAND (con0, 1);
2249 con0 = TREE_OPERAND (con0, 0);
2252 lit0 = integer_zero_node;
2254 if (TREE_CODE (con1) == PLUS_EXPR)
2256 lit1 = TREE_OPERAND (con1, 1);
2257 con1 = TREE_OPERAND (con1, 0);
2260 lit1 = integer_zero_node;
2262 if (operand_equal_p (con0, con1, 0))
2269 /* First do the subtraction as integers;
2270 then drop through to build the divide operator.
2271 Do not do default conversions on the minus operator
2272 in case restype is a short type. */
2274 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2275 convert (restype, op1), 0);
2276 /* This generates an error if op1 is pointer to incomplete type. */
2277 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2278 error ("arithmetic on pointer to an incomplete type");
2280 /* This generates an error if op0 is pointer to incomplete type. */
2281 op1 = c_size_in_bytes (target_type);
2283 /* Divide by the size, in easiest possible way. */
2284 return fold (build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2287 /* Construct and perhaps optimize a tree representation
2288 for a unary operation. CODE, a tree_code, specifies the operation
2289 and XARG is the operand.
2290 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2291 the default promotions (such as from short to int).
2292 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2293 allows non-lvalues; this is only used to handle conversion of non-lvalue
2294 arrays to pointers in C99. */
2297 build_unary_op (enum tree_code code, tree xarg, int flag)
2299 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2302 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2304 int noconvert = flag;
2306 if (typecode == ERROR_MARK)
2307 return error_mark_node;
2308 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2309 typecode = INTEGER_TYPE;
2314 /* This is used for unary plus, because a CONVERT_EXPR
2315 is enough to prevent anybody from looking inside for
2316 associativity, but won't generate any code. */
2317 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2318 || typecode == COMPLEX_TYPE))
2320 error ("wrong type argument to unary plus");
2321 return error_mark_node;
2323 else if (!noconvert)
2324 arg = default_conversion (arg);
2325 arg = non_lvalue (arg);
2329 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2330 || typecode == COMPLEX_TYPE
2331 || typecode == VECTOR_TYPE))
2333 error ("wrong type argument to unary minus");
2334 return error_mark_node;
2336 else if (!noconvert)
2337 arg = default_conversion (arg);
2341 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2344 arg = default_conversion (arg);
2346 else if (typecode == COMPLEX_TYPE)
2350 pedwarn ("ISO C does not support `~' for complex conjugation");
2352 arg = default_conversion (arg);
2356 error ("wrong type argument to bit-complement");
2357 return error_mark_node;
2362 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2364 error ("wrong type argument to abs");
2365 return error_mark_node;
2367 else if (!noconvert)
2368 arg = default_conversion (arg);
2372 /* Conjugating a real value is a no-op, but allow it anyway. */
2373 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2374 || typecode == COMPLEX_TYPE))
2376 error ("wrong type argument to conjugation");
2377 return error_mark_node;
2379 else if (!noconvert)
2380 arg = default_conversion (arg);
2383 case TRUTH_NOT_EXPR:
2384 if (typecode != INTEGER_TYPE
2385 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2386 && typecode != COMPLEX_TYPE
2387 /* These will convert to a pointer. */
2388 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2390 error ("wrong type argument to unary exclamation mark");
2391 return error_mark_node;
2393 arg = lang_hooks.truthvalue_conversion (arg);
2394 return invert_truthvalue (arg);
2400 if (TREE_CODE (arg) == COMPLEX_CST)
2401 return TREE_REALPART (arg);
2402 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2403 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2408 if (TREE_CODE (arg) == COMPLEX_CST)
2409 return TREE_IMAGPART (arg);
2410 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2411 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2413 return convert (TREE_TYPE (arg), integer_zero_node);
2415 case PREINCREMENT_EXPR:
2416 case POSTINCREMENT_EXPR:
2417 case PREDECREMENT_EXPR:
2418 case POSTDECREMENT_EXPR:
2420 /* Increment or decrement the real part of the value,
2421 and don't change the imaginary part. */
2422 if (typecode == COMPLEX_TYPE)
2427 pedwarn ("ISO C does not support `++' and `--' on complex types");
2429 arg = stabilize_reference (arg);
2430 real = build_unary_op (REALPART_EXPR, arg, 1);
2431 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2432 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2433 build_unary_op (code, real, 1), imag);
2436 /* Report invalid types. */
2438 if (typecode != POINTER_TYPE
2439 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2441 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2442 error ("wrong type argument to increment");
2444 error ("wrong type argument to decrement");
2446 return error_mark_node;
2451 tree result_type = TREE_TYPE (arg);
2453 arg = get_unwidened (arg, 0);
2454 argtype = TREE_TYPE (arg);
2456 /* Compute the increment. */
2458 if (typecode == POINTER_TYPE)
2460 /* If pointer target is an undefined struct,
2461 we just cannot know how to do the arithmetic. */
2462 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2464 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2465 error ("increment of pointer to unknown structure");
2467 error ("decrement of pointer to unknown structure");
2469 else if ((pedantic || warn_pointer_arith)
2470 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2471 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2473 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2474 pedwarn ("wrong type argument to increment");
2476 pedwarn ("wrong type argument to decrement");
2479 inc = c_size_in_bytes (TREE_TYPE (result_type));
2482 inc = integer_one_node;
2484 inc = convert (argtype, inc);
2486 /* Complain about anything else that is not a true lvalue. */
2487 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2488 || code == POSTINCREMENT_EXPR)
2489 ? "invalid lvalue in increment"
2490 : "invalid lvalue in decrement")))
2491 return error_mark_node;
2493 /* Report a read-only lvalue. */
2494 if (TREE_READONLY (arg))
2495 readonly_error (arg,
2496 ((code == PREINCREMENT_EXPR
2497 || code == POSTINCREMENT_EXPR)
2498 ? "increment" : "decrement"));
2500 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2501 val = boolean_increment (code, arg);
2503 val = build (code, TREE_TYPE (arg), arg, inc);
2504 TREE_SIDE_EFFECTS (val) = 1;
2505 val = convert (result_type, val);
2506 if (TREE_CODE (val) != code)
2507 TREE_NO_WARNING (val) = 1;
2512 /* Note that this operation never does default_conversion. */
2514 /* Let &* cancel out to simplify resulting code. */
2515 if (TREE_CODE (arg) == INDIRECT_REF)
2517 /* Don't let this be an lvalue. */
2518 if (lvalue_p (TREE_OPERAND (arg, 0)))
2519 return non_lvalue (TREE_OPERAND (arg, 0));
2520 return TREE_OPERAND (arg, 0);
2523 /* For &x[y], return x+y */
2524 if (TREE_CODE (arg) == ARRAY_REF)
2526 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2527 return error_mark_node;
2528 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2529 TREE_OPERAND (arg, 1), 1);
2532 /* Anything not already handled and not a true memory reference
2533 or a non-lvalue array is an error. */
2534 else if (typecode != FUNCTION_TYPE && !flag
2535 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2536 return error_mark_node;
2538 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2539 argtype = TREE_TYPE (arg);
2541 /* If the lvalue is const or volatile, merge that into the type
2542 to which the address will point. Note that you can't get a
2543 restricted pointer by taking the address of something, so we
2544 only have to deal with `const' and `volatile' here. */
2545 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2546 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2547 argtype = c_build_type_variant (argtype,
2548 TREE_READONLY (arg),
2549 TREE_THIS_VOLATILE (arg));
2551 argtype = build_pointer_type (argtype);
2553 if (!c_mark_addressable (arg))
2554 return error_mark_node;
2559 if (TREE_CODE (arg) == COMPONENT_REF)
2561 tree field = TREE_OPERAND (arg, 1);
2563 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2565 if (DECL_C_BIT_FIELD (field))
2567 error ("attempt to take address of bit-field structure member `%s'",
2568 IDENTIFIER_POINTER (DECL_NAME (field)));
2569 return error_mark_node;
2572 addr = fold (build (PLUS_EXPR, argtype,
2573 convert (argtype, addr),
2574 convert (argtype, byte_position (field))));
2577 addr = build1 (code, argtype, arg);
2587 argtype = TREE_TYPE (arg);
2588 return fold (build1 (code, argtype, arg));
2591 /* Return nonzero if REF is an lvalue valid for this language.
2592 Lvalues can be assigned, unless their type has TYPE_READONLY.
2593 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2598 enum tree_code code = TREE_CODE (ref);
2605 return lvalue_p (TREE_OPERAND (ref, 0));
2607 case COMPOUND_LITERAL_EXPR:
2617 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2618 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2622 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2629 /* Return nonzero if REF is an lvalue valid for this language;
2630 otherwise, print an error message and return zero. */
2633 lvalue_or_else (tree ref, const char *msgid)
2635 int win = lvalue_p (ref);
2638 error ("%s", msgid);
2644 /* Warn about storing in something that is `const'. */
2647 readonly_error (tree arg, const char *msgid)
2649 if (TREE_CODE (arg) == COMPONENT_REF)
2651 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2652 readonly_error (TREE_OPERAND (arg, 0), msgid);
2654 error ("%s of read-only member `%s'", _(msgid),
2655 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2657 else if (TREE_CODE (arg) == VAR_DECL)
2658 error ("%s of read-only variable `%s'", _(msgid),
2659 IDENTIFIER_POINTER (DECL_NAME (arg)));
2661 error ("%s of read-only location", _(msgid));
2664 /* Mark EXP saying that we need to be able to take the
2665 address of it; it should not be allocated in a register.
2666 Returns true if successful. */
2669 c_mark_addressable (tree exp)
2674 switch (TREE_CODE (x))
2677 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2679 error ("cannot take address of bit-field `%s'",
2680 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2684 /* ... fall through ... */
2690 x = TREE_OPERAND (x, 0);
2693 case COMPOUND_LITERAL_EXPR:
2695 TREE_ADDRESSABLE (x) = 1;
2702 if (C_DECL_REGISTER (x)
2703 && DECL_NONLOCAL (x))
2705 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2707 error ("global register variable `%s' used in nested function",
2708 IDENTIFIER_POINTER (DECL_NAME (x)));
2711 pedwarn ("register variable `%s' used in nested function",
2712 IDENTIFIER_POINTER (DECL_NAME (x)));
2714 else if (C_DECL_REGISTER (x))
2716 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2718 error ("address of global register variable `%s' requested",
2719 IDENTIFIER_POINTER (DECL_NAME (x)));
2723 pedwarn ("address of register variable `%s' requested",
2724 IDENTIFIER_POINTER (DECL_NAME (x)));
2726 put_var_into_stack (x, /*rescan=*/true);
2730 TREE_ADDRESSABLE (x) = 1;
2737 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2740 build_conditional_expr (tree ifexp, tree op1, tree op2)
2744 enum tree_code code1;
2745 enum tree_code code2;
2746 tree result_type = NULL;
2747 tree orig_op1 = op1, orig_op2 = op2;
2749 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2751 /* Promote both alternatives. */
2753 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2754 op1 = default_conversion (op1);
2755 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2756 op2 = default_conversion (op2);
2758 if (TREE_CODE (ifexp) == ERROR_MARK
2759 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2760 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2761 return error_mark_node;
2763 type1 = TREE_TYPE (op1);
2764 code1 = TREE_CODE (type1);
2765 type2 = TREE_TYPE (op2);
2766 code2 = TREE_CODE (type2);
2768 /* C90 does not permit non-lvalue arrays in conditional expressions.
2769 In C99 they will be pointers by now. */
2770 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2772 error ("non-lvalue array in conditional expression");
2773 return error_mark_node;
2776 /* Quickly detect the usual case where op1 and op2 have the same type
2778 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2781 result_type = type1;
2783 result_type = TYPE_MAIN_VARIANT (type1);
2785 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2786 || code1 == COMPLEX_TYPE)
2787 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2788 || code2 == COMPLEX_TYPE))
2790 result_type = common_type (type1, type2);
2792 /* If -Wsign-compare, warn here if type1 and type2 have
2793 different signedness. We'll promote the signed to unsigned
2794 and later code won't know it used to be different.
2795 Do this check on the original types, so that explicit casts
2796 will be considered, but default promotions won't. */
2797 if (warn_sign_compare && !skip_evaluation)
2799 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2800 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2802 if (unsigned_op1 ^ unsigned_op2)
2804 /* Do not warn if the result type is signed, since the
2805 signed type will only be chosen if it can represent
2806 all the values of the unsigned type. */
2807 if (! TYPE_UNSIGNED (result_type))
2809 /* Do not warn if the signed quantity is an unsuffixed
2810 integer literal (or some static constant expression
2811 involving such literals) and it is non-negative. */
2812 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2813 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2816 warning ("signed and unsigned type in conditional expression");
2820 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2822 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2823 pedwarn ("ISO C forbids conditional expr with only one void side");
2824 result_type = void_type_node;
2826 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2828 if (comp_target_types (type1, type2, 1))
2829 result_type = common_pointer_type (type1, type2);
2830 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2831 && TREE_CODE (orig_op1) != NOP_EXPR)
2832 result_type = qualify_type (type2, type1);
2833 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2834 && TREE_CODE (orig_op2) != NOP_EXPR)
2835 result_type = qualify_type (type1, type2);
2836 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2838 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2839 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2840 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2841 TREE_TYPE (type2)));
2843 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2845 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2846 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2847 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2848 TREE_TYPE (type1)));
2852 pedwarn ("pointer type mismatch in conditional expression");
2853 result_type = build_pointer_type (void_type_node);
2856 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2858 if (! integer_zerop (op2))
2859 pedwarn ("pointer/integer type mismatch in conditional expression");
2862 op2 = null_pointer_node;
2864 result_type = type1;
2866 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2868 if (!integer_zerop (op1))
2869 pedwarn ("pointer/integer type mismatch in conditional expression");
2872 op1 = null_pointer_node;
2874 result_type = type2;
2879 if (flag_cond_mismatch)
2880 result_type = void_type_node;
2883 error ("type mismatch in conditional expression");
2884 return error_mark_node;
2888 /* Merge const and volatile flags of the incoming types. */
2890 = build_type_variant (result_type,
2891 TREE_READONLY (op1) || TREE_READONLY (op2),
2892 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2894 if (result_type != TREE_TYPE (op1))
2895 op1 = convert_and_check (result_type, op1);
2896 if (result_type != TREE_TYPE (op2))
2897 op2 = convert_and_check (result_type, op2);
2899 if (TREE_CODE (ifexp) == INTEGER_CST)
2900 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2902 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2905 /* Given a list of expressions, return a compound expression
2906 that performs them all and returns the value of the last of them. */
2909 build_compound_expr (tree list)
2911 return internal_build_compound_expr (list, TRUE);
2915 internal_build_compound_expr (tree list, int first_p)
2919 if (TREE_CHAIN (list) == 0)
2921 /* Convert arrays and functions to pointers when there
2922 really is a comma operator. */
2925 = default_function_array_conversion (TREE_VALUE (list));
2927 /* Don't let (0, 0) be null pointer constant. */
2928 if (!first_p && integer_zerop (TREE_VALUE (list)))
2929 return non_lvalue (TREE_VALUE (list));
2930 return TREE_VALUE (list);
2933 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2935 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2937 /* The left-hand operand of a comma expression is like an expression
2938 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2939 any side-effects, unless it was explicitly cast to (void). */
2940 if (warn_unused_value
2941 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2942 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2943 warning ("left-hand operand of comma expression has no effect");
2946 /* With -Wunused, we should also warn if the left-hand operand does have
2947 side-effects, but computes a value which is not used. For example, in
2948 `foo() + bar(), baz()' the result of the `+' operator is not used,
2949 so we should issue a warning. */
2950 else if (warn_unused_value)
2951 warn_if_unused_value (TREE_VALUE (list));
2953 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2956 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2959 build_c_cast (tree type, tree expr)
2963 if (type == error_mark_node || expr == error_mark_node)
2964 return error_mark_node;
2966 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2967 only in <protocol> qualifications. But when constructing cast expressions,
2968 the protocols do matter and must be kept around. */
2969 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2970 type = TYPE_MAIN_VARIANT (type);
2972 if (TREE_CODE (type) == ARRAY_TYPE)
2974 error ("cast specifies array type");
2975 return error_mark_node;
2978 if (TREE_CODE (type) == FUNCTION_TYPE)
2980 error ("cast specifies function type");
2981 return error_mark_node;
2984 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2988 if (TREE_CODE (type) == RECORD_TYPE
2989 || TREE_CODE (type) == UNION_TYPE)
2990 pedwarn ("ISO C forbids casting nonscalar to the same type");
2993 else if (TREE_CODE (type) == UNION_TYPE)
2996 value = default_function_array_conversion (value);
2998 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2999 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3000 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
3008 pedwarn ("ISO C forbids casts to union type");
3009 t = digest_init (type,
3010 build_constructor (type,
3011 build_tree_list (field, value)),
3013 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3014 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3017 error ("cast to union type from type not present in union");
3018 return error_mark_node;
3024 /* If casting to void, avoid the error that would come
3025 from default_conversion in the case of a non-lvalue array. */
3026 if (type == void_type_node)
3027 return build1 (CONVERT_EXPR, type, value);
3029 /* Convert functions and arrays to pointers,
3030 but don't convert any other types. */
3031 value = default_function_array_conversion (value);
3032 otype = TREE_TYPE (value);
3034 /* Optionally warn about potentially worrisome casts. */
3037 && TREE_CODE (type) == POINTER_TYPE
3038 && TREE_CODE (otype) == POINTER_TYPE)
3040 tree in_type = type;
3041 tree in_otype = otype;
3045 /* Check that the qualifiers on IN_TYPE are a superset of
3046 the qualifiers of IN_OTYPE. The outermost level of
3047 POINTER_TYPE nodes is uninteresting and we stop as soon
3048 as we hit a non-POINTER_TYPE node on either type. */
3051 in_otype = TREE_TYPE (in_otype);
3052 in_type = TREE_TYPE (in_type);
3054 /* GNU C allows cv-qualified function types. 'const'
3055 means the function is very pure, 'volatile' means it
3056 can't return. We need to warn when such qualifiers
3057 are added, not when they're taken away. */
3058 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3059 && TREE_CODE (in_type) == FUNCTION_TYPE)
3060 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3062 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3064 while (TREE_CODE (in_type) == POINTER_TYPE
3065 && TREE_CODE (in_otype) == POINTER_TYPE);
3068 warning ("cast adds new qualifiers to function type");
3071 /* There are qualifiers present in IN_OTYPE that are not
3072 present in IN_TYPE. */
3073 warning ("cast discards qualifiers from pointer target type");
3076 /* Warn about possible alignment problems. */
3077 if (STRICT_ALIGNMENT && warn_cast_align
3078 && TREE_CODE (type) == POINTER_TYPE
3079 && TREE_CODE (otype) == POINTER_TYPE
3080 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3081 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3082 /* Don't warn about opaque types, where the actual alignment
3083 restriction is unknown. */
3084 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3085 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3086 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3087 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3088 warning ("cast increases required alignment of target type");
3090 if (TREE_CODE (type) == INTEGER_TYPE
3091 && TREE_CODE (otype) == POINTER_TYPE
3092 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3093 && !TREE_CONSTANT (value))
3094 warning ("cast from pointer to integer of different size");
3096 if (warn_bad_function_cast
3097 && TREE_CODE (value) == CALL_EXPR
3098 && TREE_CODE (type) != TREE_CODE (otype))
3099 warning ("cast does not match function type");
3101 if (TREE_CODE (type) == POINTER_TYPE
3102 && TREE_CODE (otype) == INTEGER_TYPE
3103 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3104 /* Don't warn about converting any constant. */
3105 && !TREE_CONSTANT (value))
3106 warning ("cast to pointer from integer of different size");
3108 if (TREE_CODE (type) == POINTER_TYPE
3109 && TREE_CODE (otype) == POINTER_TYPE
3110 && TREE_CODE (expr) == ADDR_EXPR
3111 && DECL_P (TREE_OPERAND (expr, 0))
3112 && flag_strict_aliasing && warn_strict_aliasing
3113 && !VOID_TYPE_P (TREE_TYPE (type)))
3115 /* Casting the address of a decl to non void pointer. Warn
3116 if the cast breaks type based aliasing. */
3117 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3118 warning ("type-punning to incomplete type might break strict-aliasing rules");
3121 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3122 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3124 if (!alias_sets_conflict_p (set1, set2))
3125 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3126 else if (warn_strict_aliasing > 1
3127 && !alias_sets_might_conflict_p (set1, set2))
3128 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3132 /* If pedantic, warn for conversions between function and object
3133 pointer types, except for converting a null pointer constant
3134 to function pointer type. */
3136 && TREE_CODE (type) == POINTER_TYPE
3137 && TREE_CODE (otype) == POINTER_TYPE
3138 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3139 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3140 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3143 && TREE_CODE (type) == POINTER_TYPE
3144 && TREE_CODE (otype) == POINTER_TYPE
3145 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3146 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3147 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3148 && TREE_CODE (expr) != NOP_EXPR))
3149 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3152 /* Replace a nonvolatile const static variable with its value. */
3153 if (optimize && TREE_CODE (value) == VAR_DECL)
3154 value = decl_constant_value (value);
3155 value = convert (type, value);
3157 /* Ignore any integer overflow caused by the cast. */
3158 if (TREE_CODE (value) == INTEGER_CST)
3160 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3162 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3163 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3167 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3168 if (TREE_CODE (value) == INTEGER_CST
3169 && TREE_CODE (expr) == INTEGER_CST
3170 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3171 value = non_lvalue (value);
3173 /* Don't let a cast be an lvalue. */
3175 value = non_lvalue (value);
3180 /* Interpret a cast of expression EXPR to type TYPE. */
3182 c_cast_expr (tree type, tree expr)
3184 int saved_wsp = warn_strict_prototypes;
3186 /* This avoids warnings about unprototyped casts on
3187 integers. E.g. "#define SIG_DFL (void(*)())0". */
3188 if (TREE_CODE (expr) == INTEGER_CST)
3189 warn_strict_prototypes = 0;
3190 type = groktypename (type);
3191 warn_strict_prototypes = saved_wsp;
3193 return build_c_cast (type, expr);
3197 /* Build an assignment expression of lvalue LHS from value RHS.
3198 MODIFYCODE is the code for a binary operator that we use
3199 to combine the old value of LHS with RHS to get the new value.
3200 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3203 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3207 tree lhstype = TREE_TYPE (lhs);
3208 tree olhstype = lhstype;
3210 /* Types that aren't fully specified cannot be used in assignments. */
3211 lhs = require_complete_type (lhs);
3213 /* Avoid duplicate error messages from operands that had errors. */
3214 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3215 return error_mark_node;
3217 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3218 /* Do not use STRIP_NOPS here. We do not want an enumerator
3219 whose value is 0 to count as a null pointer constant. */
3220 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3221 rhs = TREE_OPERAND (rhs, 0);
3225 /* If a binary op has been requested, combine the old LHS value with the RHS
3226 producing the value we should actually store into the LHS. */
3228 if (modifycode != NOP_EXPR)
3230 lhs = stabilize_reference (lhs);
3231 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3234 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3235 return error_mark_node;
3237 /* Warn about storing in something that is `const'. */
3239 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3240 || ((TREE_CODE (lhstype) == RECORD_TYPE
3241 || TREE_CODE (lhstype) == UNION_TYPE)
3242 && C_TYPE_FIELDS_READONLY (lhstype)))
3243 readonly_error (lhs, "assignment");
3245 /* If storing into a structure or union member,
3246 it has probably been given type `int'.
3247 Compute the type that would go with
3248 the actual amount of storage the member occupies. */
3250 if (TREE_CODE (lhs) == COMPONENT_REF
3251 && (TREE_CODE (lhstype) == INTEGER_TYPE
3252 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3253 || TREE_CODE (lhstype) == REAL_TYPE
3254 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3255 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3257 /* If storing in a field that is in actuality a short or narrower than one,
3258 we must store in the field in its actual type. */
3260 if (lhstype != TREE_TYPE (lhs))
3262 lhs = copy_node (lhs);
3263 TREE_TYPE (lhs) = lhstype;
3266 /* Convert new value to destination type. */
3268 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3269 NULL_TREE, NULL_TREE, 0);
3270 if (TREE_CODE (newrhs) == ERROR_MARK)
3271 return error_mark_node;
3275 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3276 TREE_SIDE_EFFECTS (result) = 1;
3278 /* If we got the LHS in a different type for storing in,
3279 convert the result back to the nominal type of LHS
3280 so that the value we return always has the same type
3281 as the LHS argument. */
3283 if (olhstype == TREE_TYPE (result))
3285 return convert_for_assignment (olhstype, result, _("assignment"),
3286 NULL_TREE, NULL_TREE, 0);
3289 /* Convert value RHS to type TYPE as preparation for an assignment
3290 to an lvalue of type TYPE.
3291 The real work of conversion is done by `convert'.
3292 The purpose of this function is to generate error messages
3293 for assignments that are not allowed in C.
3294 ERRTYPE is a string to use in error messages:
3295 "assignment", "return", etc. If it is null, this is parameter passing
3296 for a function call (and different error messages are output).
3298 FUNNAME is the name of the function being called,
3299 as an IDENTIFIER_NODE, or null.
3300 PARMNUM is the number of the argument, for printing in error messages. */
3303 convert_for_assignment (tree type, tree rhs, const char *errtype,
3304 tree fundecl, tree funname, int parmnum)
3306 enum tree_code codel = TREE_CODE (type);
3308 enum tree_code coder;
3310 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3311 /* Do not use STRIP_NOPS here. We do not want an enumerator
3312 whose value is 0 to count as a null pointer constant. */
3313 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3314 rhs = TREE_OPERAND (rhs, 0);
3316 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3317 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3318 rhs = default_conversion (rhs);
3319 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3320 rhs = decl_constant_value_for_broken_optimization (rhs);
3322 rhstype = TREE_TYPE (rhs);
3323 coder = TREE_CODE (rhstype);
3325 if (coder == ERROR_MARK)
3326 return error_mark_node;
3328 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3330 overflow_warning (rhs);
3331 /* Check for Objective-C protocols. This will automatically
3332 issue a warning if there are protocol violations. No need to
3333 use the return value. */
3334 if (c_dialect_objc ())
3335 objc_comptypes (type, rhstype, 0);
3339 if (coder == VOID_TYPE)
3341 error ("void value not ignored as it ought to be");
3342 return error_mark_node;
3344 /* A type converts to a reference to it.
3345 This code doesn't fully support references, it's just for the
3346 special case of va_start and va_copy. */
3347 if (codel == REFERENCE_TYPE
3348 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3350 if (!lvalue_p (rhs))
3352 error ("cannot pass rvalue to reference parameter");
3353 return error_mark_node;
3355 if (!c_mark_addressable (rhs))
3356 return error_mark_node;
3357 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3359 /* We already know that these two types are compatible, but they
3360 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3361 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3362 likely to be va_list, a typedef to __builtin_va_list, which
3363 is different enough that it will cause problems later. */
3364 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3365 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3367 rhs = build1 (NOP_EXPR, type, rhs);
3370 /* Some types can interconvert without explicit casts. */
3371 else if (codel == VECTOR_TYPE
3372 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3373 return convert (type, rhs);
3374 /* Arithmetic types all interconvert, and enum is treated like int. */
3375 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3376 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3377 || codel == BOOLEAN_TYPE)
3378 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3379 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3380 || coder == BOOLEAN_TYPE))
3381 return convert_and_check (type, rhs);
3383 /* Conversion to a transparent union from its member types.
3384 This applies only to function arguments. */
3385 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3388 tree marginal_memb_type = 0;
3390 for (memb_types = TYPE_FIELDS (type); memb_types;
3391 memb_types = TREE_CHAIN (memb_types))
3393 tree memb_type = TREE_TYPE (memb_types);
3395 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3396 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3399 if (TREE_CODE (memb_type) != POINTER_TYPE)
3402 if (coder == POINTER_TYPE)
3404 tree ttl = TREE_TYPE (memb_type);
3405 tree ttr = TREE_TYPE (rhstype);
3407 /* Any non-function converts to a [const][volatile] void *
3408 and vice versa; otherwise, targets must be the same.
3409 Meanwhile, the lhs target must have all the qualifiers of
3411 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3412 || comp_target_types (memb_type, rhstype, 0))
3414 /* If this type won't generate any warnings, use it. */
3415 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3416 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3417 && TREE_CODE (ttl) == FUNCTION_TYPE)
3418 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3419 == TYPE_QUALS (ttr))
3420 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3421 == TYPE_QUALS (ttl))))
3424 /* Keep looking for a better type, but remember this one. */
3425 if (! marginal_memb_type)
3426 marginal_memb_type = memb_type;
3430 /* Can convert integer zero to any pointer type. */
3431 if (integer_zerop (rhs)
3432 || (TREE_CODE (rhs) == NOP_EXPR
3433 && integer_zerop (TREE_OPERAND (rhs, 0))))
3435 rhs = null_pointer_node;