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 common type of two types.
203 We assume that comptypes has already been done and returned 1;
204 if that isn't so, this may crash. In particular, we assume that qualifiers
207 This is the type for the result of most arithmetic operations
208 if the operands have the given two types. */
211 common_type (tree t1, tree t2)
213 enum tree_code code1;
214 enum tree_code code2;
217 /* Save time if the two types are the same. */
219 if (t1 == t2) return t1;
221 /* If one type is nonsense, use the other. */
222 if (t1 == error_mark_node)
224 if (t2 == error_mark_node)
227 /* Merge the attributes. */
228 attributes = targetm.merge_type_attributes (t1, t2);
230 /* Treat an enum type as the unsigned integer type of the same width. */
232 if (TREE_CODE (t1) == ENUMERAL_TYPE)
233 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
234 if (TREE_CODE (t2) == ENUMERAL_TYPE)
235 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
237 code1 = TREE_CODE (t1);
238 code2 = TREE_CODE (t2);
240 /* If one type is complex, form the common type of the non-complex
241 components, then make that complex. Use T1 or T2 if it is the
243 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
245 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
246 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
247 tree subtype = common_type (subtype1, subtype2);
249 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
250 return build_type_attribute_variant (t1, attributes);
251 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
252 return build_type_attribute_variant (t2, attributes);
254 return build_type_attribute_variant (build_complex_type (subtype),
262 /* If only one is real, use it as the result. */
264 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
265 return build_type_attribute_variant (t1, attributes);
267 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
268 return build_type_attribute_variant (t2, attributes);
270 /* Both real or both integers; use the one with greater precision. */
272 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
273 return build_type_attribute_variant (t1, attributes);
274 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
275 return build_type_attribute_variant (t2, attributes);
277 /* Same precision. Prefer long longs to longs to ints when the
278 same precision, following the C99 rules on integer type rank
279 (which are equivalent to the C90 rules for C90 types). */
281 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
282 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
283 return build_type_attribute_variant (long_long_unsigned_type_node,
286 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
287 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
289 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
290 t1 = long_long_unsigned_type_node;
292 t1 = long_long_integer_type_node;
293 return build_type_attribute_variant (t1, attributes);
296 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
297 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
298 return build_type_attribute_variant (long_unsigned_type_node,
301 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
302 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
304 /* But preserve unsignedness from the other type,
305 since long cannot hold all the values of an unsigned int. */
306 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
307 t1 = long_unsigned_type_node;
309 t1 = long_integer_type_node;
310 return build_type_attribute_variant (t1, attributes);
313 /* Likewise, prefer long double to double even if same size. */
314 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
315 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
316 return build_type_attribute_variant (long_double_type_node,
319 /* Otherwise prefer the unsigned one. */
321 if (TYPE_UNSIGNED (t1))
322 return build_type_attribute_variant (t1, attributes);
324 return build_type_attribute_variant (t2, attributes);
327 /* For two pointers, do this recursively on the target type,
328 and combine the qualifiers of the two types' targets. */
329 /* This code was turned off; I don't know why.
330 But ANSI C specifies doing this with the qualifiers.
331 So I turned it on again. */
333 tree pointed_to_1 = TREE_TYPE (t1);
334 tree pointed_to_2 = TREE_TYPE (t2);
335 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
336 TYPE_MAIN_VARIANT (pointed_to_2));
337 t1 = build_pointer_type (c_build_qualified_type
339 TYPE_QUALS (pointed_to_1) |
340 TYPE_QUALS (pointed_to_2)));
341 return build_type_attribute_variant (t1, attributes);
346 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
347 /* Save space: see if the result is identical to one of the args. */
348 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
349 return build_type_attribute_variant (t1, attributes);
350 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
351 return build_type_attribute_variant (t2, attributes);
352 /* Merge the element types, and have a size if either arg has one. */
353 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
354 return build_type_attribute_variant (t1, attributes);
358 /* Function types: prefer the one that specified arg types.
359 If both do, merge the arg types. Also merge the return types. */
361 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
362 tree p1 = TYPE_ARG_TYPES (t1);
363 tree p2 = TYPE_ARG_TYPES (t2);
368 /* Save space: see if the result is identical to one of the args. */
369 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
370 return build_type_attribute_variant (t1, attributes);
371 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
372 return build_type_attribute_variant (t2, attributes);
374 /* Simple way if one arg fails to specify argument types. */
375 if (TYPE_ARG_TYPES (t1) == 0)
377 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
378 return build_type_attribute_variant (t1, attributes);
380 if (TYPE_ARG_TYPES (t2) == 0)
382 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
383 return build_type_attribute_variant (t1, attributes);
386 /* If both args specify argument types, we must merge the two
387 lists, argument by argument. */
388 /* Tell global_bindings_p to return false so that variable_size
389 doesn't abort on VLAs in parameter types. */
390 c_override_global_bindings_to_false = true;
392 len = list_length (p1);
395 for (i = 0; i < len; i++)
396 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
401 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
403 /* A null type means arg type is not specified.
404 Take whatever the other function type has. */
405 if (TREE_VALUE (p1) == 0)
407 TREE_VALUE (n) = TREE_VALUE (p2);
410 if (TREE_VALUE (p2) == 0)
412 TREE_VALUE (n) = TREE_VALUE (p1);
416 /* Given wait (union {union wait *u; int *i} *)
417 and wait (union wait *),
418 prefer union wait * as type of parm. */
419 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
420 && TREE_VALUE (p1) != TREE_VALUE (p2))
423 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
424 memb; memb = TREE_CHAIN (memb))
425 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
428 TREE_VALUE (n) = TREE_VALUE (p2);
430 pedwarn ("function types not truly compatible in ISO C");
434 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
435 && TREE_VALUE (p2) != TREE_VALUE (p1))
438 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
439 memb; memb = TREE_CHAIN (memb))
440 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
443 TREE_VALUE (n) = TREE_VALUE (p1);
445 pedwarn ("function types not truly compatible in ISO C");
449 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
453 c_override_global_bindings_to_false = false;
454 t1 = build_function_type (valtype, newargs);
455 /* ... falls through ... */
459 return build_type_attribute_variant (t1, attributes);
464 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
465 or various other operations. Return 2 if they are compatible
466 but a warning may be needed if you use them together. */
469 comptypes (tree type1, tree type2, int flags)
475 /* Suppress errors caused by previously reported errors. */
477 if (t1 == t2 || !t1 || !t2
478 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
481 /* If either type is the internal version of sizetype, return the
483 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
484 && TYPE_ORIG_SIZE_TYPE (t1))
485 t1 = TYPE_ORIG_SIZE_TYPE (t1);
487 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
488 && TYPE_ORIG_SIZE_TYPE (t2))
489 t2 = TYPE_ORIG_SIZE_TYPE (t2);
492 /* Enumerated types are compatible with integer types, but this is
493 not transitive: two enumerated types in the same translation unit
494 are compatible with each other only if they are the same type. */
496 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
497 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
498 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
499 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
504 /* Different classes of types can't be compatible. */
506 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
508 /* Qualifiers must match. */
510 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
513 /* Allow for two different type nodes which have essentially the same
514 definition. Note that we already checked for equality of the type
515 qualifiers (just above). */
517 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
520 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
521 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
524 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
527 switch (TREE_CODE (t1))
530 /* We must give ObjC the first crack at comparing pointers, since
531 protocol qualifiers may be involved. */
532 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
534 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
535 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
539 val = function_types_compatible_p (t1, t2, flags);
544 tree d1 = TYPE_DOMAIN (t1);
545 tree d2 = TYPE_DOMAIN (t2);
546 bool d1_variable, d2_variable;
547 bool d1_zero, d2_zero;
550 /* Target types must match incl. qualifiers. */
551 if (TREE_TYPE (t1) != TREE_TYPE (t2)
552 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
556 /* Sizes must match unless one is missing or variable. */
557 if (d1 == 0 || d2 == 0 || d1 == d2)
560 d1_zero = ! TYPE_MAX_VALUE (d1);
561 d2_zero = ! TYPE_MAX_VALUE (d2);
563 d1_variable = (! d1_zero
564 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
565 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
566 d2_variable = (! d2_zero
567 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
568 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
570 if (d1_variable || d2_variable)
572 if (d1_zero && d2_zero)
574 if (d1_zero || d2_zero
575 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
576 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
583 /* We are dealing with two distinct structs. In assorted Objective-C
584 corner cases, however, these can still be deemed equivalent. */
585 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
590 if (val != 1 && !same_translation_unit_p (t1, t2))
591 val = tagged_types_tu_compatible_p (t1, t2, flags);
595 /* The target might allow certain vector types to be compatible. */
596 val = targetm.vector_opaque_p (t1)
597 || targetm.vector_opaque_p (t2)
598 || TYPE_MODE (t1) == TYPE_MODE (t2);
604 return attrval == 2 && val == 1 ? 2 : val;
607 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
608 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
609 to 1 or 0 depending if the check of the pointer types is meant to
610 be reflexive or not (typically, assignments are not reflexive,
611 while comparisons are reflexive).
615 comp_target_types (tree ttl, tree ttr, int reflexive)
619 /* Give objc_comptypes a crack at letting these types through. */
620 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
623 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
624 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
626 if (val == 2 && pedantic)
627 pedwarn ("types are not quite compatible");
631 /* Subroutines of `comptypes'. */
633 /* Determine whether two trees derive from the same translation unit.
634 If the CONTEXT chain ends in a null, that tree's context is still
635 being parsed, so if two trees have context chains ending in null,
636 they're in the same translation unit. */
638 same_translation_unit_p (tree t1, tree t2)
640 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
641 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
643 case 'd': t1 = DECL_CONTEXT (t1); break;
644 case 't': t1 = TYPE_CONTEXT (t1); break;
645 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
649 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
650 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
652 case 'd': t2 = DECL_CONTEXT (t2); break;
653 case 't': t2 = TYPE_CONTEXT (t2); break;
654 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
661 /* The C standard says that two structures in different translation
662 units are compatible with each other only if the types of their
663 fields are compatible (among other things). So, consider two copies
664 of this structure: */
666 struct tagged_tu_seen {
667 const struct tagged_tu_seen * next;
672 /* Can they be compatible with each other? We choose to break the
673 recursion by allowing those types to be compatible. */
675 static const struct tagged_tu_seen * tagged_tu_seen_base;
677 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
678 compatible. If the two types are not the same (which has been
679 checked earlier), this can only happen when multiple translation
680 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
684 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
687 bool needs_warning = false;
689 /* We have to verify that the tags of the types are the same. This
690 is harder than it looks because this may be a typedef, so we have
691 to go look at the original type. It may even be a typedef of a
693 while (TYPE_NAME (t1)
694 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
695 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
696 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
698 while (TYPE_NAME (t2)
699 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
700 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
701 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
703 /* C90 didn't have the requirement that the two tags be the same. */
704 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
707 /* C90 didn't say what happened if one or both of the types were
708 incomplete; we choose to follow C99 rules here, which is that they
710 if (TYPE_SIZE (t1) == NULL
711 || TYPE_SIZE (t2) == NULL)
715 const struct tagged_tu_seen * tts_i;
716 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
717 if (tts_i->t1 == t1 && tts_i->t2 == t2)
721 switch (TREE_CODE (t1))
726 /* Speed up the case where the type values are in the same order. */
727 tree tv1 = TYPE_VALUES (t1);
728 tree tv2 = TYPE_VALUES (t2);
733 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
735 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
737 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
741 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
743 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
746 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
749 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
751 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
753 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
761 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
764 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
767 struct tagged_tu_seen tts;
769 tts.next = tagged_tu_seen_base;
772 tagged_tu_seen_base = &tts;
774 if (DECL_NAME (s1) != NULL)
775 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
776 if (DECL_NAME (s1) == DECL_NAME (s2))
779 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
783 needs_warning = true;
785 if (TREE_CODE (s1) == FIELD_DECL
786 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
787 DECL_FIELD_BIT_OFFSET (s2)) != 1)
793 tagged_tu_seen_base = tts.next;
797 return needs_warning ? 2 : 1;
802 struct tagged_tu_seen tts;
804 tts.next = tagged_tu_seen_base;
807 tagged_tu_seen_base = &tts;
809 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
811 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
814 if (TREE_CODE (s1) != TREE_CODE (s2)
815 || DECL_NAME (s1) != DECL_NAME (s2))
817 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
821 needs_warning = true;
823 if (TREE_CODE (s1) == FIELD_DECL
824 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
825 DECL_FIELD_BIT_OFFSET (s2)) != 1)
828 tagged_tu_seen_base = tts.next;
831 return needs_warning ? 2 : 1;
839 /* Return 1 if two function types F1 and F2 are compatible.
840 If either type specifies no argument types,
841 the other must specify a fixed number of self-promoting arg types.
842 Otherwise, if one type specifies only the number of arguments,
843 the other must specify that number of self-promoting arg types.
844 Otherwise, the argument types must match. */
847 function_types_compatible_p (tree f1, tree f2, int flags)
850 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
855 ret1 = TREE_TYPE (f1);
856 ret2 = TREE_TYPE (f2);
858 /* 'volatile' qualifiers on a function's return type mean the function
860 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
861 pedwarn ("function return types not compatible due to `volatile'");
862 if (TYPE_VOLATILE (ret1))
863 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
864 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
865 if (TYPE_VOLATILE (ret2))
866 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
867 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
868 val = comptypes (ret1, ret2, flags);
872 args1 = TYPE_ARG_TYPES (f1);
873 args2 = TYPE_ARG_TYPES (f2);
875 /* An unspecified parmlist matches any specified parmlist
876 whose argument types don't need default promotions. */
880 if (!self_promoting_args_p (args2))
882 /* If one of these types comes from a non-prototype fn definition,
883 compare that with the other type's arglist.
884 If they don't match, ask for a warning (but no error). */
885 if (TYPE_ACTUAL_ARG_TYPES (f1)
886 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
893 if (!self_promoting_args_p (args1))
895 if (TYPE_ACTUAL_ARG_TYPES (f2)
896 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
902 /* Both types have argument lists: compare them and propagate results. */
903 val1 = type_lists_compatible_p (args1, args2, flags);
904 return val1 != 1 ? val1 : val;
907 /* Check two lists of types for compatibility,
908 returning 0 for incompatible, 1 for compatible,
909 or 2 for compatible with warning. */
912 type_lists_compatible_p (tree args1, tree args2, int flags)
914 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
920 if (args1 == 0 && args2 == 0)
922 /* If one list is shorter than the other,
923 they fail to match. */
924 if (args1 == 0 || args2 == 0)
926 /* A null pointer instead of a type
927 means there is supposed to be an argument
928 but nothing is specified about what type it has.
929 So match anything that self-promotes. */
930 if (TREE_VALUE (args1) == 0)
932 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
935 else if (TREE_VALUE (args2) == 0)
937 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
940 /* If one of the lists has an error marker, ignore this arg. */
941 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
942 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
944 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
945 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
948 /* Allow wait (union {union wait *u; int *i} *)
949 and wait (union wait *) to be compatible. */
950 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
951 && (TYPE_NAME (TREE_VALUE (args1)) == 0
952 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
953 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
954 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
955 TYPE_SIZE (TREE_VALUE (args2))))
958 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
959 memb; memb = TREE_CHAIN (memb))
960 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
966 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
967 && (TYPE_NAME (TREE_VALUE (args2)) == 0
968 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
969 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
970 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
971 TYPE_SIZE (TREE_VALUE (args1))))
974 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
975 memb; memb = TREE_CHAIN (memb))
976 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
986 /* comptypes said ok, but record if it said to warn. */
990 args1 = TREE_CHAIN (args1);
991 args2 = TREE_CHAIN (args2);
995 /* Compute the size to increment a pointer by. */
998 c_size_in_bytes (tree type)
1000 enum tree_code code = TREE_CODE (type);
1002 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1003 return size_one_node;
1005 if (!COMPLETE_OR_VOID_TYPE_P (type))
1007 error ("arithmetic on pointer to an incomplete type");
1008 return size_one_node;
1011 /* Convert in case a char is more than one unit. */
1012 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1013 size_int (TYPE_PRECISION (char_type_node)
1017 /* Return either DECL or its known constant value (if it has one). */
1020 decl_constant_value (tree decl)
1022 if (/* Don't change a variable array bound or initial value to a constant
1023 in a place where a variable is invalid. Note that DECL_INITIAL
1024 isn't valid for a PARM_DECL. */
1025 current_function_decl != 0
1026 && TREE_CODE (decl) != PARM_DECL
1027 && ! TREE_THIS_VOLATILE (decl)
1028 && TREE_READONLY (decl)
1029 && DECL_INITIAL (decl) != 0
1030 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1031 /* This is invalid if initial value is not constant.
1032 If it has either a function call, a memory reference,
1033 or a variable, then re-evaluating it could give different results. */
1034 && TREE_CONSTANT (DECL_INITIAL (decl))
1035 /* Check for cases where this is sub-optimal, even though valid. */
1036 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1037 return DECL_INITIAL (decl);
1041 /* Return either DECL or its known constant value (if it has one), but
1042 return DECL if pedantic or DECL has mode BLKmode. This is for
1043 bug-compatibility with the old behavior of decl_constant_value
1044 (before GCC 3.0); every use of this function is a bug and it should
1045 be removed before GCC 3.1. It is not appropriate to use pedantic
1046 in a way that affects optimization, and BLKmode is probably not the
1047 right test for avoiding misoptimizations either. */
1050 decl_constant_value_for_broken_optimization (tree decl)
1052 if (pedantic || DECL_MODE (decl) == BLKmode)
1055 return decl_constant_value (decl);
1059 /* Perform the default conversion of arrays and functions to pointers.
1060 Return the result of converting EXP. For any other expression, just
1064 default_function_array_conversion (tree exp)
1067 tree type = TREE_TYPE (exp);
1068 enum tree_code code = TREE_CODE (type);
1071 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1074 Do not use STRIP_NOPS here! It will remove conversions from pointer
1075 to integer and cause infinite recursion. */
1077 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1078 || (TREE_CODE (exp) == NOP_EXPR
1079 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1081 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1083 exp = TREE_OPERAND (exp, 0);
1086 /* Preserve the original expression code. */
1087 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1088 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1090 if (code == FUNCTION_TYPE)
1092 return build_unary_op (ADDR_EXPR, exp, 0);
1094 if (code == ARRAY_TYPE)
1097 tree restype = TREE_TYPE (type);
1103 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1105 constp = TREE_READONLY (exp);
1106 volatilep = TREE_THIS_VOLATILE (exp);
1109 if (TYPE_QUALS (type) || constp || volatilep)
1111 = c_build_qualified_type (restype,
1113 | (constp * TYPE_QUAL_CONST)
1114 | (volatilep * TYPE_QUAL_VOLATILE));
1116 if (TREE_CODE (exp) == INDIRECT_REF)
1117 return convert (TYPE_POINTER_TO (restype),
1118 TREE_OPERAND (exp, 0));
1120 if (TREE_CODE (exp) == COMPOUND_EXPR)
1122 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1123 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1124 TREE_OPERAND (exp, 0), op1);
1127 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1128 if (!flag_isoc99 && !lvalue_array_p)
1130 /* Before C99, non-lvalue arrays do not decay to pointers.
1131 Normally, using such an array would be invalid; but it can
1132 be used correctly inside sizeof or as a statement expression.
1133 Thus, do not give an error here; an error will result later. */
1137 ptrtype = build_pointer_type (restype);
1139 if (TREE_CODE (exp) == VAR_DECL)
1141 /* ??? This is not really quite correct
1142 in that the type of the operand of ADDR_EXPR
1143 is not the target type of the type of the ADDR_EXPR itself.
1144 Question is, can this lossage be avoided? */
1145 adr = build1 (ADDR_EXPR, ptrtype, exp);
1146 if (!c_mark_addressable (exp))
1147 return error_mark_node;
1148 TREE_CONSTANT (adr) = staticp (exp);
1149 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1152 /* This way is better for a COMPONENT_REF since it can
1153 simplify the offset for a component. */
1154 adr = build_unary_op (ADDR_EXPR, exp, 1);
1155 return convert (ptrtype, adr);
1160 /* Perform default promotions for C data used in expressions.
1161 Arrays and functions are converted to pointers;
1162 enumeral types or short or char, to int.
1163 In addition, manifest constants symbols are replaced by their values. */
1166 default_conversion (tree exp)
1169 tree type = TREE_TYPE (exp);
1170 enum tree_code code = TREE_CODE (type);
1172 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1173 return default_function_array_conversion (exp);
1175 /* Constants can be used directly unless they're not loadable. */
1176 if (TREE_CODE (exp) == CONST_DECL)
1177 exp = DECL_INITIAL (exp);
1179 /* Replace a nonvolatile const static variable with its value unless
1180 it is an array, in which case we must be sure that taking the
1181 address of the array produces consistent results. */
1182 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1184 exp = decl_constant_value_for_broken_optimization (exp);
1185 type = TREE_TYPE (exp);
1188 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1191 Do not use STRIP_NOPS here! It will remove conversions from pointer
1192 to integer and cause infinite recursion. */
1194 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1195 || (TREE_CODE (exp) == NOP_EXPR
1196 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1197 exp = TREE_OPERAND (exp, 0);
1199 /* Preserve the original expression code. */
1200 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1201 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1203 /* Normally convert enums to int,
1204 but convert wide enums to something wider. */
1205 if (code == ENUMERAL_TYPE)
1207 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1208 TYPE_PRECISION (integer_type_node)),
1209 ((TYPE_PRECISION (type)
1210 >= TYPE_PRECISION (integer_type_node))
1211 && TYPE_UNSIGNED (type)));
1213 return convert (type, exp);
1216 if (TREE_CODE (exp) == COMPONENT_REF
1217 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1218 /* If it's thinner than an int, promote it like a
1219 c_promoting_integer_type_p, otherwise leave it alone. */
1220 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1221 TYPE_PRECISION (integer_type_node)))
1222 return convert (integer_type_node, exp);
1224 if (c_promoting_integer_type_p (type))
1226 /* Preserve unsignedness if not really getting any wider. */
1227 if (TYPE_UNSIGNED (type)
1228 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1229 return convert (unsigned_type_node, exp);
1231 return convert (integer_type_node, exp);
1234 if (code == VOID_TYPE)
1236 error ("void value not ignored as it ought to be");
1237 return error_mark_node;
1242 /* Look up COMPONENT in a structure or union DECL.
1244 If the component name is not found, returns NULL_TREE. Otherwise,
1245 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1246 stepping down the chain to the component, which is in the last
1247 TREE_VALUE of the list. Normally the list is of length one, but if
1248 the component is embedded within (nested) anonymous structures or
1249 unions, the list steps down the chain to the component. */
1252 lookup_field (tree decl, tree component)
1254 tree type = TREE_TYPE (decl);
1257 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1258 to the field elements. Use a binary search on this array to quickly
1259 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1260 will always be set for structures which have many elements. */
1262 if (TYPE_LANG_SPECIFIC (type))
1265 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1267 field = TYPE_FIELDS (type);
1269 top = TYPE_LANG_SPECIFIC (type)->s->len;
1270 while (top - bot > 1)
1272 half = (top - bot + 1) >> 1;
1273 field = field_array[bot+half];
1275 if (DECL_NAME (field) == NULL_TREE)
1277 /* Step through all anon unions in linear fashion. */
1278 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1280 field = field_array[bot++];
1281 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1282 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1284 tree anon = lookup_field (field, component);
1287 return tree_cons (NULL_TREE, field, anon);
1291 /* Entire record is only anon unions. */
1295 /* Restart the binary search, with new lower bound. */
1299 if (DECL_NAME (field) == component)
1301 if (DECL_NAME (field) < component)
1307 if (DECL_NAME (field_array[bot]) == component)
1308 field = field_array[bot];
1309 else if (DECL_NAME (field) != component)
1314 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1316 if (DECL_NAME (field) == NULL_TREE
1317 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1318 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1320 tree anon = lookup_field (field, component);
1323 return tree_cons (NULL_TREE, field, anon);
1326 if (DECL_NAME (field) == component)
1330 if (field == NULL_TREE)
1334 return tree_cons (NULL_TREE, field, NULL_TREE);
1337 /* Make an expression to refer to the COMPONENT field of
1338 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1341 build_component_ref (tree datum, tree component)
1343 tree type = TREE_TYPE (datum);
1344 enum tree_code code = TREE_CODE (type);
1348 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1349 Ensure that the arguments are not lvalues; otherwise,
1350 if the component is an array, it would wrongly decay to a pointer in
1352 We cannot do this with a COND_EXPR, because in a conditional expression
1353 the default promotions are applied to both sides, and this would yield
1354 the wrong type of the result; for example, if the components have
1356 switch (TREE_CODE (datum))
1360 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1361 return build (COMPOUND_EXPR, TREE_TYPE (value),
1362 TREE_OPERAND (datum, 0), non_lvalue (value));
1368 /* See if there is a field or component with name COMPONENT. */
1370 if (code == RECORD_TYPE || code == UNION_TYPE)
1372 if (!COMPLETE_TYPE_P (type))
1374 c_incomplete_type_error (NULL_TREE, type);
1375 return error_mark_node;
1378 field = lookup_field (datum, component);
1382 error ("%s has no member named `%s'",
1383 code == RECORD_TYPE ? "structure" : "union",
1384 IDENTIFIER_POINTER (component));
1385 return error_mark_node;
1388 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1389 This might be better solved in future the way the C++ front
1390 end does it - by giving the anonymous entities each a
1391 separate name and type, and then have build_component_ref
1392 recursively call itself. We can't do that here. */
1395 tree subdatum = TREE_VALUE (field);
1397 if (TREE_TYPE (subdatum) == error_mark_node)
1398 return error_mark_node;
1400 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1401 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1402 TREE_READONLY (ref) = 1;
1403 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1404 TREE_THIS_VOLATILE (ref) = 1;
1406 if (TREE_DEPRECATED (subdatum))
1407 warn_deprecated_use (subdatum);
1411 field = TREE_CHAIN (field);
1417 else if (code != ERROR_MARK)
1418 error ("request for member `%s' in something not a structure or union",
1419 IDENTIFIER_POINTER (component));
1421 return error_mark_node;
1424 /* Given an expression PTR for a pointer, return an expression
1425 for the value pointed to.
1426 ERRORSTRING is the name of the operator to appear in error messages. */
1429 build_indirect_ref (tree ptr, const char *errorstring)
1431 tree pointer = default_conversion (ptr);
1432 tree type = TREE_TYPE (pointer);
1434 if (TREE_CODE (type) == POINTER_TYPE)
1436 if (TREE_CODE (pointer) == ADDR_EXPR
1437 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1438 == TREE_TYPE (type)))
1439 return TREE_OPERAND (pointer, 0);
1442 tree t = TREE_TYPE (type);
1443 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1445 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1447 error ("dereferencing pointer to incomplete type");
1448 return error_mark_node;
1450 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1451 warning ("dereferencing `void *' pointer");
1453 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1454 so that we get the proper error message if the result is used
1455 to assign to. Also, &* is supposed to be a no-op.
1456 And ANSI C seems to specify that the type of the result
1457 should be the const type. */
1458 /* A de-reference of a pointer to const is not a const. It is valid
1459 to change it via some other pointer. */
1460 TREE_READONLY (ref) = TYPE_READONLY (t);
1461 TREE_SIDE_EFFECTS (ref)
1462 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1463 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1467 else if (TREE_CODE (pointer) != ERROR_MARK)
1468 error ("invalid type argument of `%s'", errorstring);
1469 return error_mark_node;
1472 /* This handles expressions of the form "a[i]", which denotes
1475 This is logically equivalent in C to *(a+i), but we may do it differently.
1476 If A is a variable or a member, we generate a primitive ARRAY_REF.
1477 This avoids forcing the array out of registers, and can work on
1478 arrays that are not lvalues (for example, members of structures returned
1482 build_array_ref (tree array, tree index)
1486 error ("subscript missing in array reference");
1487 return error_mark_node;
1490 if (TREE_TYPE (array) == error_mark_node
1491 || TREE_TYPE (index) == error_mark_node)
1492 return error_mark_node;
1494 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1495 && TREE_CODE (array) != INDIRECT_REF)
1499 /* Subscripting with type char is likely to lose
1500 on a machine where chars are signed.
1501 So warn on any machine, but optionally.
1502 Don't warn for unsigned char since that type is safe.
1503 Don't warn for signed char because anyone who uses that
1504 must have done so deliberately. */
1505 if (warn_char_subscripts
1506 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1507 warning ("array subscript has type `char'");
1509 /* Apply default promotions *after* noticing character types. */
1510 index = default_conversion (index);
1512 /* Require integer *after* promotion, for sake of enums. */
1513 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1515 error ("array subscript is not an integer");
1516 return error_mark_node;
1519 /* An array that is indexed by a non-constant
1520 cannot be stored in a register; we must be able to do
1521 address arithmetic on its address.
1522 Likewise an array of elements of variable size. */
1523 if (TREE_CODE (index) != INTEGER_CST
1524 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1525 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1527 if (!c_mark_addressable (array))
1528 return error_mark_node;
1530 /* An array that is indexed by a constant value which is not within
1531 the array bounds cannot be stored in a register either; because we
1532 would get a crash in store_bit_field/extract_bit_field when trying
1533 to access a non-existent part of the register. */
1534 if (TREE_CODE (index) == INTEGER_CST
1535 && TYPE_DOMAIN (TREE_TYPE (array))
1536 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1538 if (!c_mark_addressable (array))
1539 return error_mark_node;
1545 while (TREE_CODE (foo) == COMPONENT_REF)
1546 foo = TREE_OPERAND (foo, 0);
1547 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1548 pedwarn ("ISO C forbids subscripting `register' array");
1549 else if (! flag_isoc99 && ! lvalue_p (foo))
1550 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1553 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1554 rval = build (ARRAY_REF, type, array, index);
1555 /* Array ref is const/volatile if the array elements are
1556 or if the array is. */
1557 TREE_READONLY (rval)
1558 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1559 | TREE_READONLY (array));
1560 TREE_SIDE_EFFECTS (rval)
1561 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1562 | TREE_SIDE_EFFECTS (array));
1563 TREE_THIS_VOLATILE (rval)
1564 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1565 /* This was added by rms on 16 Nov 91.
1566 It fixes vol struct foo *a; a->elts[1]
1567 in an inline function.
1568 Hope it doesn't break something else. */
1569 | TREE_THIS_VOLATILE (array));
1570 return require_complete_type (fold (rval));
1574 tree ar = default_conversion (array);
1575 tree ind = default_conversion (index);
1577 /* Do the same warning check as above, but only on the part that's
1578 syntactically the index and only if it is also semantically
1580 if (warn_char_subscripts
1581 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1582 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1583 warning ("subscript has type `char'");
1585 /* Put the integer in IND to simplify error checking. */
1586 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1593 if (ar == error_mark_node)
1596 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1597 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1599 error ("subscripted value is neither array nor pointer");
1600 return error_mark_node;
1602 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1604 error ("array subscript is not an integer");
1605 return error_mark_node;
1608 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1613 /* Build an external reference to identifier ID. FUN indicates
1614 whether this will be used for a function call. */
1616 build_external_ref (tree id, int fun)
1619 tree decl = lookup_name (id);
1620 tree objc_ivar = lookup_objc_ivar (id);
1622 if (decl && decl != error_mark_node)
1624 /* Properly declared variable or function reference. */
1627 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1629 warning ("local declaration of `%s' hides instance variable",
1630 IDENTIFIER_POINTER (id));
1639 /* Implicit function declaration. */
1640 ref = implicitly_declare (id);
1641 else if (decl == error_mark_node)
1642 /* Don't complain about something that's already been
1643 complained about. */
1644 return error_mark_node;
1647 undeclared_variable (id);
1648 return error_mark_node;
1651 if (TREE_TYPE (ref) == error_mark_node)
1652 return error_mark_node;
1654 if (TREE_DEPRECATED (ref))
1655 warn_deprecated_use (ref);
1657 if (!skip_evaluation)
1658 assemble_external (ref);
1659 TREE_USED (ref) = 1;
1661 if (TREE_CODE (ref) == CONST_DECL)
1663 ref = DECL_INITIAL (ref);
1664 TREE_CONSTANT (ref) = 1;
1666 else if (current_function_decl != 0
1667 && !DECL_FILE_SCOPE_P (current_function_decl)
1668 && (TREE_CODE (ref) == VAR_DECL
1669 || TREE_CODE (ref) == PARM_DECL
1670 || TREE_CODE (ref) == FUNCTION_DECL))
1672 tree context = decl_function_context (ref);
1674 if (context != 0 && context != current_function_decl)
1675 DECL_NONLOCAL (ref) = 1;
1681 /* Build a function call to function FUNCTION with parameters PARAMS.
1682 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1683 TREE_VALUE of each node is a parameter-expression.
1684 FUNCTION's data type may be a function type or a pointer-to-function. */
1687 build_function_call (tree function, tree params)
1689 tree fntype, fundecl = 0;
1690 tree coerced_params;
1691 tree name = NULL_TREE, result;
1694 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1695 STRIP_TYPE_NOPS (function);
1697 /* Convert anything with function type to a pointer-to-function. */
1698 if (TREE_CODE (function) == FUNCTION_DECL)
1700 name = DECL_NAME (function);
1702 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1703 (because calling an inline function does not mean the function
1704 needs to be separately compiled). */
1705 fntype = build_type_variant (TREE_TYPE (function),
1706 TREE_READONLY (function),
1707 TREE_THIS_VOLATILE (function));
1709 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1712 function = default_conversion (function);
1714 fntype = TREE_TYPE (function);
1716 if (TREE_CODE (fntype) == ERROR_MARK)
1717 return error_mark_node;
1719 if (!(TREE_CODE (fntype) == POINTER_TYPE
1720 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1722 error ("called object is not a function");
1723 return error_mark_node;
1726 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1727 current_function_returns_abnormally = 1;
1729 /* fntype now gets the type of function pointed to. */
1730 fntype = TREE_TYPE (fntype);
1732 /* Check that the function is called through a compatible prototype.
1733 If it is not, replace the call by a trap, wrapped up in a compound
1734 expression if necessary. This has the nice side-effect to prevent
1735 the tree-inliner from generating invalid assignment trees which may
1736 blow up in the RTL expander later.
1738 ??? This doesn't work for Objective-C because objc_comptypes
1739 refuses to compare function prototypes, yet the compiler appears
1740 to build calls that are flagged as invalid by C's comptypes. */
1741 if (! c_dialect_objc ()
1742 && TREE_CODE (function) == NOP_EXPR
1743 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1744 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1745 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1747 tree return_type = TREE_TYPE (fntype);
1748 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1751 /* This situation leads to run-time undefined behavior. We can't,
1752 therefore, simply error unless we can prove that all possible
1753 executions of the program must execute the code. */
1754 warning ("function called through a non-compatible type");
1756 /* We can, however, treat "undefined" any way we please.
1757 Call abort to encourage the user to fix the program. */
1758 inform ("if this code is reached, the program will abort");
1760 if (VOID_TYPE_P (return_type))
1766 if (AGGREGATE_TYPE_P (return_type))
1767 rhs = build_compound_literal (return_type,
1768 build_constructor (return_type,
1771 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1773 return build (COMPOUND_EXPR, return_type, trap, rhs);
1777 /* Convert the parameters to the types declared in the
1778 function prototype, or apply default promotions. */
1781 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1783 /* Check that the arguments to the function are valid. */
1785 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1787 /* Recognize certain built-in functions so we can make tree-codes
1788 other than CALL_EXPR. We do this when it enables fold-const.c
1789 to do something useful. */
1791 if (TREE_CODE (function) == ADDR_EXPR
1792 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1793 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1795 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1796 params, coerced_params);
1801 result = build (CALL_EXPR, TREE_TYPE (fntype),
1802 function, coerced_params, NULL_TREE);
1803 TREE_SIDE_EFFECTS (result) = 1;
1804 result = fold (result);
1806 if (VOID_TYPE_P (TREE_TYPE (result)))
1808 return require_complete_type (result);
1811 /* Convert the argument expressions in the list VALUES
1812 to the types in the list TYPELIST. The result is a list of converted
1813 argument expressions.
1815 If TYPELIST is exhausted, or when an element has NULL as its type,
1816 perform the default conversions.
1818 PARMLIST is the chain of parm decls for the function being called.
1819 It may be 0, if that info is not available.
1820 It is used only for generating error messages.
1822 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1824 This is also where warnings about wrong number of args are generated.
1826 Both VALUES and the returned value are chains of TREE_LIST nodes
1827 with the elements of the list in the TREE_VALUE slots of those nodes. */
1830 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1832 tree typetail, valtail;
1836 /* Scan the given expressions and types, producing individual
1837 converted arguments and pushing them on RESULT in reverse order. */
1839 for (valtail = values, typetail = typelist, parmnum = 0;
1841 valtail = TREE_CHAIN (valtail), parmnum++)
1843 tree type = typetail ? TREE_VALUE (typetail) : 0;
1844 tree val = TREE_VALUE (valtail);
1846 if (type == void_type_node)
1849 error ("too many arguments to function `%s'",
1850 IDENTIFIER_POINTER (name));
1852 error ("too many arguments to function");
1856 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1857 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1858 to convert automatically to a pointer. */
1859 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1860 val = TREE_OPERAND (val, 0);
1862 val = default_function_array_conversion (val);
1864 val = require_complete_type (val);
1868 /* Formal parm type is specified by a function prototype. */
1871 if (!COMPLETE_TYPE_P (type))
1873 error ("type of formal parameter %d is incomplete", parmnum + 1);
1878 /* Optionally warn about conversions that
1879 differ from the default conversions. */
1880 if (warn_conversion || warn_traditional)
1882 int formal_prec = TYPE_PRECISION (type);
1884 if (INTEGRAL_TYPE_P (type)
1885 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1886 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1887 if (INTEGRAL_TYPE_P (type)
1888 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1889 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1890 else if (TREE_CODE (type) == COMPLEX_TYPE
1891 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1892 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1893 else if (TREE_CODE (type) == REAL_TYPE
1894 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1895 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1896 else if (TREE_CODE (type) == COMPLEX_TYPE
1897 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1898 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1899 else if (TREE_CODE (type) == REAL_TYPE
1900 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1901 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1902 /* ??? At some point, messages should be written about
1903 conversions between complex types, but that's too messy
1905 else if (TREE_CODE (type) == REAL_TYPE
1906 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1908 /* Warn if any argument is passed as `float',
1909 since without a prototype it would be `double'. */
1910 if (formal_prec == TYPE_PRECISION (float_type_node))
1911 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1913 /* Detect integer changing in width or signedness.
1914 These warnings are only activated with
1915 -Wconversion, not with -Wtraditional. */
1916 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1917 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1919 tree would_have_been = default_conversion (val);
1920 tree type1 = TREE_TYPE (would_have_been);
1922 if (TREE_CODE (type) == ENUMERAL_TYPE
1923 && (TYPE_MAIN_VARIANT (type)
1924 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1925 /* No warning if function asks for enum
1926 and the actual arg is that enum type. */
1928 else if (formal_prec != TYPE_PRECISION (type1))
1929 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1930 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
1932 /* Don't complain if the formal parameter type
1933 is an enum, because we can't tell now whether
1934 the value was an enum--even the same enum. */
1935 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1937 else if (TREE_CODE (val) == INTEGER_CST
1938 && int_fits_type_p (val, type))
1939 /* Change in signedness doesn't matter
1940 if a constant value is unaffected. */
1942 /* Likewise for a constant in a NOP_EXPR. */
1943 else if (TREE_CODE (val) == NOP_EXPR
1944 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1945 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1947 /* If the value is extended from a narrower
1948 unsigned type, it doesn't matter whether we
1949 pass it as signed or unsigned; the value
1950 certainly is the same either way. */
1951 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1952 && TYPE_UNSIGNED (TREE_TYPE (val)))
1954 else if (TYPE_UNSIGNED (type))
1955 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1957 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1961 parmval = convert_for_assignment (type, val,
1962 (char *) 0, /* arg passing */
1963 fundecl, name, parmnum + 1);
1965 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1966 && INTEGRAL_TYPE_P (type)
1967 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1968 parmval = default_conversion (parmval);
1970 result = tree_cons (NULL_TREE, parmval, result);
1972 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1973 && (TYPE_PRECISION (TREE_TYPE (val))
1974 < TYPE_PRECISION (double_type_node)))
1975 /* Convert `float' to `double'. */
1976 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1978 /* Convert `short' and `char' to full-size `int'. */
1979 result = tree_cons (NULL_TREE, default_conversion (val), result);
1982 typetail = TREE_CHAIN (typetail);
1985 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1988 error ("too few arguments to function `%s'",
1989 IDENTIFIER_POINTER (name));
1991 error ("too few arguments to function");
1994 return nreverse (result);
1997 /* This is the entry point used by the parser
1998 for binary operators in the input.
1999 In addition to constructing the expression,
2000 we check for operands that were written with other binary operators
2001 in a way that is likely to confuse the user. */
2004 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
2006 tree result = build_binary_op (code, arg1, arg2, 1);
2009 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2010 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2011 enum tree_code code1 = ERROR_MARK;
2012 enum tree_code code2 = ERROR_MARK;
2014 if (TREE_CODE (result) == ERROR_MARK)
2015 return error_mark_node;
2017 if (IS_EXPR_CODE_CLASS (class1))
2018 code1 = C_EXP_ORIGINAL_CODE (arg1);
2019 if (IS_EXPR_CODE_CLASS (class2))
2020 code2 = C_EXP_ORIGINAL_CODE (arg2);
2022 /* Check for cases such as x+y<<z which users are likely
2023 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2024 is cleared to prevent these warnings. */
2025 if (warn_parentheses)
2027 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2029 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2030 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2031 warning ("suggest parentheses around + or - inside shift");
2034 if (code == TRUTH_ORIF_EXPR)
2036 if (code1 == TRUTH_ANDIF_EXPR
2037 || code2 == TRUTH_ANDIF_EXPR)
2038 warning ("suggest parentheses around && within ||");
2041 if (code == BIT_IOR_EXPR)
2043 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2044 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2045 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2046 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2047 warning ("suggest parentheses around arithmetic in operand of |");
2048 /* Check cases like x|y==z */
2049 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2050 warning ("suggest parentheses around comparison in operand of |");
2053 if (code == BIT_XOR_EXPR)
2055 if (code1 == BIT_AND_EXPR
2056 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2057 || code2 == BIT_AND_EXPR
2058 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2059 warning ("suggest parentheses around arithmetic in operand of ^");
2060 /* Check cases like x^y==z */
2061 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2062 warning ("suggest parentheses around comparison in operand of ^");
2065 if (code == BIT_AND_EXPR)
2067 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2068 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2069 warning ("suggest parentheses around + or - in operand of &");
2070 /* Check cases like x&y==z */
2071 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2072 warning ("suggest parentheses around comparison in operand of &");
2076 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2077 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2078 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2079 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2081 unsigned_conversion_warning (result, arg1);
2082 unsigned_conversion_warning (result, arg2);
2083 overflow_warning (result);
2085 class = TREE_CODE_CLASS (TREE_CODE (result));
2087 /* Record the code that was specified in the source,
2088 for the sake of warnings about confusing nesting. */
2089 if (IS_EXPR_CODE_CLASS (class))
2090 C_SET_EXP_ORIGINAL_CODE (result, code);
2093 int flag = TREE_CONSTANT (result);
2094 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2095 so that convert_for_assignment wouldn't strip it.
2096 That way, we got warnings for things like p = (1 - 1).
2097 But it turns out we should not get those warnings. */
2098 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2099 C_SET_EXP_ORIGINAL_CODE (result, code);
2100 TREE_CONSTANT (result) = flag;
2107 /* Return true if `t' is known to be non-negative. */
2110 c_tree_expr_nonnegative_p (tree t)
2112 if (TREE_CODE (t) == STMT_EXPR)
2114 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2116 /* Find the last statement in the chain, ignoring the final
2117 * scope statement */
2118 while (TREE_CHAIN (t) != NULL_TREE
2119 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2121 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2123 return tree_expr_nonnegative_p (t);
2126 /* Return a tree for the difference of pointers OP0 and OP1.
2127 The resulting tree has type int. */
2130 pointer_diff (tree op0, tree op1)
2132 tree result, folded;
2133 tree restype = ptrdiff_type_node;
2135 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2136 tree con0, con1, lit0, lit1;
2137 tree orig_op1 = op1;
2139 if (pedantic || warn_pointer_arith)
2141 if (TREE_CODE (target_type) == VOID_TYPE)
2142 pedwarn ("pointer of type `void *' used in subtraction");
2143 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2144 pedwarn ("pointer to a function used in subtraction");
2147 /* If the conversion to ptrdiff_type does anything like widening or
2148 converting a partial to an integral mode, we get a convert_expression
2149 that is in the way to do any simplifications.
2150 (fold-const.c doesn't know that the extra bits won't be needed.
2151 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2152 different mode in place.)
2153 So first try to find a common term here 'by hand'; we want to cover
2154 at least the cases that occur in legal static initializers. */
2155 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2156 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2158 if (TREE_CODE (con0) == PLUS_EXPR)
2160 lit0 = TREE_OPERAND (con0, 1);
2161 con0 = TREE_OPERAND (con0, 0);
2164 lit0 = integer_zero_node;
2166 if (TREE_CODE (con1) == PLUS_EXPR)
2168 lit1 = TREE_OPERAND (con1, 1);
2169 con1 = TREE_OPERAND (con1, 0);
2172 lit1 = integer_zero_node;
2174 if (operand_equal_p (con0, con1, 0))
2181 /* First do the subtraction as integers;
2182 then drop through to build the divide operator.
2183 Do not do default conversions on the minus operator
2184 in case restype is a short type. */
2186 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2187 convert (restype, op1), 0);
2188 /* This generates an error if op1 is pointer to incomplete type. */
2189 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2190 error ("arithmetic on pointer to an incomplete type");
2192 /* This generates an error if op0 is pointer to incomplete type. */
2193 op1 = c_size_in_bytes (target_type);
2195 /* Divide by the size, in easiest possible way. */
2197 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2199 folded = fold (result);
2200 if (folded == result)
2201 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2205 /* Construct and perhaps optimize a tree representation
2206 for a unary operation. CODE, a tree_code, specifies the operation
2207 and XARG is the operand.
2208 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2209 the default promotions (such as from short to int).
2210 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2211 allows non-lvalues; this is only used to handle conversion of non-lvalue
2212 arrays to pointers in C99. */
2215 build_unary_op (enum tree_code code, tree xarg, int flag)
2217 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2220 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2222 int noconvert = flag;
2224 if (typecode == ERROR_MARK)
2225 return error_mark_node;
2226 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2227 typecode = INTEGER_TYPE;
2232 /* This is used for unary plus, because a CONVERT_EXPR
2233 is enough to prevent anybody from looking inside for
2234 associativity, but won't generate any code. */
2235 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2236 || typecode == COMPLEX_TYPE))
2238 error ("wrong type argument to unary plus");
2239 return error_mark_node;
2241 else if (!noconvert)
2242 arg = default_conversion (arg);
2243 arg = non_lvalue (arg);
2247 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2248 || typecode == COMPLEX_TYPE
2249 || typecode == VECTOR_TYPE))
2251 error ("wrong type argument to unary minus");
2252 return error_mark_node;
2254 else if (!noconvert)
2255 arg = default_conversion (arg);
2259 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2262 arg = default_conversion (arg);
2264 else if (typecode == COMPLEX_TYPE)
2268 pedwarn ("ISO C does not support `~' for complex conjugation");
2270 arg = default_conversion (arg);
2274 error ("wrong type argument to bit-complement");
2275 return error_mark_node;
2280 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2282 error ("wrong type argument to abs");
2283 return error_mark_node;
2285 else if (!noconvert)
2286 arg = default_conversion (arg);
2290 /* Conjugating a real value is a no-op, but allow it anyway. */
2291 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2292 || typecode == COMPLEX_TYPE))
2294 error ("wrong type argument to conjugation");
2295 return error_mark_node;
2297 else if (!noconvert)
2298 arg = default_conversion (arg);
2301 case TRUTH_NOT_EXPR:
2302 if (typecode != INTEGER_TYPE
2303 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2304 && typecode != COMPLEX_TYPE
2305 /* These will convert to a pointer. */
2306 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2308 error ("wrong type argument to unary exclamation mark");
2309 return error_mark_node;
2311 arg = lang_hooks.truthvalue_conversion (arg);
2312 return invert_truthvalue (arg);
2318 if (TREE_CODE (arg) == COMPLEX_CST)
2319 return TREE_REALPART (arg);
2320 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2321 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2326 if (TREE_CODE (arg) == COMPLEX_CST)
2327 return TREE_IMAGPART (arg);
2328 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2329 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2331 return convert (TREE_TYPE (arg), integer_zero_node);
2333 case PREINCREMENT_EXPR:
2334 case POSTINCREMENT_EXPR:
2335 case PREDECREMENT_EXPR:
2336 case POSTDECREMENT_EXPR:
2338 /* Increment or decrement the real part of the value,
2339 and don't change the imaginary part. */
2340 if (typecode == COMPLEX_TYPE)
2345 pedwarn ("ISO C does not support `++' and `--' on complex types");
2347 arg = stabilize_reference (arg);
2348 real = build_unary_op (REALPART_EXPR, arg, 1);
2349 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2350 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2351 build_unary_op (code, real, 1), imag);
2354 /* Report invalid types. */
2356 if (typecode != POINTER_TYPE
2357 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2359 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2360 error ("wrong type argument to increment");
2362 error ("wrong type argument to decrement");
2364 return error_mark_node;
2369 tree result_type = TREE_TYPE (arg);
2371 arg = get_unwidened (arg, 0);
2372 argtype = TREE_TYPE (arg);
2374 /* Compute the increment. */
2376 if (typecode == POINTER_TYPE)
2378 /* If pointer target is an undefined struct,
2379 we just cannot know how to do the arithmetic. */
2380 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2382 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2383 error ("increment of pointer to unknown structure");
2385 error ("decrement of pointer to unknown structure");
2387 else if ((pedantic || warn_pointer_arith)
2388 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2389 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2391 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2392 pedwarn ("wrong type argument to increment");
2394 pedwarn ("wrong type argument to decrement");
2397 inc = c_size_in_bytes (TREE_TYPE (result_type));
2400 inc = integer_one_node;
2402 inc = convert (argtype, inc);
2404 /* Complain about anything else that is not a true lvalue. */
2405 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2406 || code == POSTINCREMENT_EXPR)
2407 ? "invalid lvalue in increment"
2408 : "invalid lvalue in decrement")))
2409 return error_mark_node;
2411 /* Report a read-only lvalue. */
2412 if (TREE_READONLY (arg))
2413 readonly_error (arg,
2414 ((code == PREINCREMENT_EXPR
2415 || code == POSTINCREMENT_EXPR)
2416 ? "increment" : "decrement"));
2418 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2419 val = boolean_increment (code, arg);
2421 val = build (code, TREE_TYPE (arg), arg, inc);
2422 TREE_SIDE_EFFECTS (val) = 1;
2423 val = convert (result_type, val);
2424 if (TREE_CODE (val) != code)
2425 TREE_NO_UNUSED_WARNING (val) = 1;
2430 /* Note that this operation never does default_conversion. */
2432 /* Let &* cancel out to simplify resulting code. */
2433 if (TREE_CODE (arg) == INDIRECT_REF)
2435 /* Don't let this be an lvalue. */
2436 if (lvalue_p (TREE_OPERAND (arg, 0)))
2437 return non_lvalue (TREE_OPERAND (arg, 0));
2438 return TREE_OPERAND (arg, 0);
2441 /* For &x[y], return x+y */
2442 if (TREE_CODE (arg) == ARRAY_REF)
2444 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2445 return error_mark_node;
2446 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2447 TREE_OPERAND (arg, 1), 1);
2450 /* Anything not already handled and not a true memory reference
2451 or a non-lvalue array is an error. */
2452 else if (typecode != FUNCTION_TYPE && !flag
2453 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2454 return error_mark_node;
2456 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2457 argtype = TREE_TYPE (arg);
2459 /* If the lvalue is const or volatile, merge that into the type
2460 to which the address will point. Note that you can't get a
2461 restricted pointer by taking the address of something, so we
2462 only have to deal with `const' and `volatile' here. */
2463 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2464 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2465 argtype = c_build_type_variant (argtype,
2466 TREE_READONLY (arg),
2467 TREE_THIS_VOLATILE (arg));
2469 argtype = build_pointer_type (argtype);
2471 if (!c_mark_addressable (arg))
2472 return error_mark_node;
2477 if (TREE_CODE (arg) == COMPONENT_REF)
2479 tree field = TREE_OPERAND (arg, 1);
2481 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2483 if (DECL_C_BIT_FIELD (field))
2485 error ("attempt to take address of bit-field structure member `%s'",
2486 IDENTIFIER_POINTER (DECL_NAME (field)));
2487 return error_mark_node;
2490 addr = fold (build (PLUS_EXPR, argtype,
2491 convert (argtype, addr),
2492 convert (argtype, byte_position (field))));
2495 addr = build1 (code, argtype, arg);
2497 /* Address of a static or external variable or
2498 file-scope function counts as a constant. */
2500 && ! (TREE_CODE (arg) == FUNCTION_DECL
2501 && !DECL_FILE_SCOPE_P (arg)))
2502 TREE_CONSTANT (addr) = 1;
2511 argtype = TREE_TYPE (arg);
2512 return fold (build1 (code, argtype, arg));
2515 /* Return nonzero if REF is an lvalue valid for this language.
2516 Lvalues can be assigned, unless their type has TYPE_READONLY.
2517 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2522 enum tree_code code = TREE_CODE (ref);
2529 return lvalue_p (TREE_OPERAND (ref, 0));
2531 case COMPOUND_LITERAL_EXPR:
2541 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2542 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2546 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2553 /* Return nonzero if REF is an lvalue valid for this language;
2554 otherwise, print an error message and return zero. */
2557 lvalue_or_else (tree ref, const char *msgid)
2559 int win = lvalue_p (ref);
2562 error ("%s", msgid);
2568 /* Warn about storing in something that is `const'. */
2571 readonly_error (tree arg, const char *msgid)
2573 if (TREE_CODE (arg) == COMPONENT_REF)
2575 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2576 readonly_error (TREE_OPERAND (arg, 0), msgid);
2578 error ("%s of read-only member `%s'", _(msgid),
2579 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2581 else if (TREE_CODE (arg) == VAR_DECL)
2582 error ("%s of read-only variable `%s'", _(msgid),
2583 IDENTIFIER_POINTER (DECL_NAME (arg)));
2585 error ("%s of read-only location", _(msgid));
2588 /* Mark EXP saying that we need to be able to take the
2589 address of it; it should not be allocated in a register.
2590 Returns true if successful. */
2593 c_mark_addressable (tree exp)
2598 switch (TREE_CODE (x))
2601 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2603 error ("cannot take address of bit-field `%s'",
2604 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2608 /* ... fall through ... */
2614 x = TREE_OPERAND (x, 0);
2617 case COMPOUND_LITERAL_EXPR:
2619 TREE_ADDRESSABLE (x) = 1;
2626 if (C_DECL_REGISTER (x)
2627 && DECL_NONLOCAL (x))
2629 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2631 error ("global register variable `%s' used in nested function",
2632 IDENTIFIER_POINTER (DECL_NAME (x)));
2635 pedwarn ("register variable `%s' used in nested function",
2636 IDENTIFIER_POINTER (DECL_NAME (x)));
2638 else if (C_DECL_REGISTER (x))
2640 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2642 error ("address of global register variable `%s' requested",
2643 IDENTIFIER_POINTER (DECL_NAME (x)));
2647 pedwarn ("address of register variable `%s' requested",
2648 IDENTIFIER_POINTER (DECL_NAME (x)));
2650 put_var_into_stack (x, /*rescan=*/true);
2654 TREE_ADDRESSABLE (x) = 1;
2661 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2664 build_conditional_expr (tree ifexp, tree op1, tree op2)
2668 enum tree_code code1;
2669 enum tree_code code2;
2670 tree result_type = NULL;
2671 tree orig_op1 = op1, orig_op2 = op2;
2673 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2675 /* Promote both alternatives. */
2677 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2678 op1 = default_conversion (op1);
2679 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2680 op2 = default_conversion (op2);
2682 if (TREE_CODE (ifexp) == ERROR_MARK
2683 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2684 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2685 return error_mark_node;
2687 type1 = TREE_TYPE (op1);
2688 code1 = TREE_CODE (type1);
2689 type2 = TREE_TYPE (op2);
2690 code2 = TREE_CODE (type2);
2692 /* C90 does not permit non-lvalue arrays in conditional expressions.
2693 In C99 they will be pointers by now. */
2694 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2696 error ("non-lvalue array in conditional expression");
2697 return error_mark_node;
2700 /* Quickly detect the usual case where op1 and op2 have the same type
2702 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2705 result_type = type1;
2707 result_type = TYPE_MAIN_VARIANT (type1);
2709 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2710 || code1 == COMPLEX_TYPE)
2711 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2712 || code2 == COMPLEX_TYPE))
2714 result_type = common_type (type1, type2);
2716 /* If -Wsign-compare, warn here if type1 and type2 have
2717 different signedness. We'll promote the signed to unsigned
2718 and later code won't know it used to be different.
2719 Do this check on the original types, so that explicit casts
2720 will be considered, but default promotions won't. */
2721 if (warn_sign_compare && !skip_evaluation)
2723 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2724 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2726 if (unsigned_op1 ^ unsigned_op2)
2728 /* Do not warn if the result type is signed, since the
2729 signed type will only be chosen if it can represent
2730 all the values of the unsigned type. */
2731 if (! TYPE_UNSIGNED (result_type))
2733 /* Do not warn if the signed quantity is an unsuffixed
2734 integer literal (or some static constant expression
2735 involving such literals) and it is non-negative. */
2736 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2737 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2740 warning ("signed and unsigned type in conditional expression");
2744 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2746 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2747 pedwarn ("ISO C forbids conditional expr with only one void side");
2748 result_type = void_type_node;
2750 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2752 if (comp_target_types (type1, type2, 1))
2753 result_type = common_type (type1, type2);
2754 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2755 && TREE_CODE (orig_op1) != NOP_EXPR)
2756 result_type = qualify_type (type2, type1);
2757 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2758 && TREE_CODE (orig_op2) != NOP_EXPR)
2759 result_type = qualify_type (type1, type2);
2760 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2762 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2763 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2764 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2765 TREE_TYPE (type2)));
2767 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2769 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2770 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2771 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2772 TREE_TYPE (type1)));
2776 pedwarn ("pointer type mismatch in conditional expression");
2777 result_type = build_pointer_type (void_type_node);
2780 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2782 if (! integer_zerop (op2))
2783 pedwarn ("pointer/integer type mismatch in conditional expression");
2786 op2 = null_pointer_node;
2788 result_type = type1;
2790 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2792 if (!integer_zerop (op1))
2793 pedwarn ("pointer/integer type mismatch in conditional expression");
2796 op1 = null_pointer_node;
2798 result_type = type2;
2803 if (flag_cond_mismatch)
2804 result_type = void_type_node;
2807 error ("type mismatch in conditional expression");
2808 return error_mark_node;
2812 /* Merge const and volatile flags of the incoming types. */
2814 = build_type_variant (result_type,
2815 TREE_READONLY (op1) || TREE_READONLY (op2),
2816 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2818 if (result_type != TREE_TYPE (op1))
2819 op1 = convert_and_check (result_type, op1);
2820 if (result_type != TREE_TYPE (op2))
2821 op2 = convert_and_check (result_type, op2);
2823 if (TREE_CODE (ifexp) == INTEGER_CST)
2824 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2826 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2829 /* Given a list of expressions, return a compound expression
2830 that performs them all and returns the value of the last of them. */
2833 build_compound_expr (tree list)
2835 return internal_build_compound_expr (list, TRUE);
2839 internal_build_compound_expr (tree list, int first_p)
2843 if (TREE_CHAIN (list) == 0)
2845 /* Convert arrays and functions to pointers when there
2846 really is a comma operator. */
2849 = default_function_array_conversion (TREE_VALUE (list));
2851 /* Don't let (0, 0) be null pointer constant. */
2852 if (!first_p && integer_zerop (TREE_VALUE (list)))
2853 return non_lvalue (TREE_VALUE (list));
2854 return TREE_VALUE (list);
2857 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2859 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2861 /* The left-hand operand of a comma expression is like an expression
2862 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2863 any side-effects, unless it was explicitly cast to (void). */
2864 if (warn_unused_value
2865 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2866 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2867 warning ("left-hand operand of comma expression has no effect");
2870 /* With -Wunused, we should also warn if the left-hand operand does have
2871 side-effects, but computes a value which is not used. For example, in
2872 `foo() + bar(), baz()' the result of the `+' operator is not used,
2873 so we should issue a warning. */
2874 else if (warn_unused_value)
2875 warn_if_unused_value (TREE_VALUE (list));
2877 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2880 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2883 build_c_cast (tree type, tree expr)
2887 if (type == error_mark_node || expr == error_mark_node)
2888 return error_mark_node;
2890 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2891 only in <protocol> qualifications. But when constructing cast expressions,
2892 the protocols do matter and must be kept around. */
2893 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2894 type = TYPE_MAIN_VARIANT (type);
2896 if (TREE_CODE (type) == ARRAY_TYPE)
2898 error ("cast specifies array type");
2899 return error_mark_node;
2902 if (TREE_CODE (type) == FUNCTION_TYPE)
2904 error ("cast specifies function type");
2905 return error_mark_node;
2908 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2912 if (TREE_CODE (type) == RECORD_TYPE
2913 || TREE_CODE (type) == UNION_TYPE)
2914 pedwarn ("ISO C forbids casting nonscalar to the same type");
2917 else if (TREE_CODE (type) == UNION_TYPE)
2920 value = default_function_array_conversion (value);
2922 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2923 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2924 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2932 pedwarn ("ISO C forbids casts to union type");
2933 t = digest_init (type,
2934 build_constructor (type,
2935 build_tree_list (field, value)),
2937 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2940 error ("cast to union type from type not present in union");
2941 return error_mark_node;
2947 /* If casting to void, avoid the error that would come
2948 from default_conversion in the case of a non-lvalue array. */
2949 if (type == void_type_node)
2950 return build1 (CONVERT_EXPR, type, value);
2952 /* Convert functions and arrays to pointers,
2953 but don't convert any other types. */
2954 value = default_function_array_conversion (value);
2955 otype = TREE_TYPE (value);
2957 /* Optionally warn about potentially worrisome casts. */
2960 && TREE_CODE (type) == POINTER_TYPE
2961 && TREE_CODE (otype) == POINTER_TYPE)
2963 tree in_type = type;
2964 tree in_otype = otype;
2968 /* Check that the qualifiers on IN_TYPE are a superset of
2969 the qualifiers of IN_OTYPE. The outermost level of
2970 POINTER_TYPE nodes is uninteresting and we stop as soon
2971 as we hit a non-POINTER_TYPE node on either type. */
2974 in_otype = TREE_TYPE (in_otype);
2975 in_type = TREE_TYPE (in_type);
2977 /* GNU C allows cv-qualified function types. 'const'
2978 means the function is very pure, 'volatile' means it
2979 can't return. We need to warn when such qualifiers
2980 are added, not when they're taken away. */
2981 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2982 && TREE_CODE (in_type) == FUNCTION_TYPE)
2983 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2985 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2987 while (TREE_CODE (in_type) == POINTER_TYPE
2988 && TREE_CODE (in_otype) == POINTER_TYPE);
2991 warning ("cast adds new qualifiers to function type");
2994 /* There are qualifiers present in IN_OTYPE that are not
2995 present in IN_TYPE. */
2996 warning ("cast discards qualifiers from pointer target type");
2999 /* Warn about possible alignment problems. */
3000 if (STRICT_ALIGNMENT && warn_cast_align
3001 && TREE_CODE (type) == POINTER_TYPE
3002 && TREE_CODE (otype) == POINTER_TYPE
3003 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3004 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3005 /* Don't warn about opaque types, where the actual alignment
3006 restriction is unknown. */
3007 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3008 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3009 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3010 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3011 warning ("cast increases required alignment of target type");
3013 if (TREE_CODE (type) == INTEGER_TYPE
3014 && TREE_CODE (otype) == POINTER_TYPE
3015 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3016 && !TREE_CONSTANT (value))
3017 warning ("cast from pointer to integer of different size");
3019 if (warn_bad_function_cast
3020 && TREE_CODE (value) == CALL_EXPR
3021 && TREE_CODE (type) != TREE_CODE (otype))
3022 warning ("cast does not match function type");
3024 if (TREE_CODE (type) == POINTER_TYPE
3025 && TREE_CODE (otype) == INTEGER_TYPE
3026 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3027 /* Don't warn about converting any constant. */
3028 && !TREE_CONSTANT (value))
3029 warning ("cast to pointer from integer of different size");
3031 if (TREE_CODE (type) == POINTER_TYPE
3032 && TREE_CODE (otype) == POINTER_TYPE
3033 && TREE_CODE (expr) == ADDR_EXPR
3034 && DECL_P (TREE_OPERAND (expr, 0))
3035 && flag_strict_aliasing && warn_strict_aliasing
3036 && !VOID_TYPE_P (TREE_TYPE (type)))
3038 /* Casting the address of a decl to non void pointer. Warn
3039 if the cast breaks type based aliasing. */
3040 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3041 warning ("type-punning to incomplete type might break strict-aliasing rules");
3044 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3045 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3047 if (!alias_sets_conflict_p (set1, set2))
3048 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3049 else if (warn_strict_aliasing > 1
3050 && !alias_sets_might_conflict_p (set1, set2))
3051 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3055 /* If pedantic, warn for conversions between function and object
3056 pointer types, except for converting a null pointer constant
3057 to function pointer type. */
3059 && TREE_CODE (type) == POINTER_TYPE
3060 && TREE_CODE (otype) == POINTER_TYPE
3061 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3062 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3063 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3066 && TREE_CODE (type) == POINTER_TYPE
3067 && TREE_CODE (otype) == POINTER_TYPE
3068 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3069 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3070 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3071 && TREE_CODE (expr) != NOP_EXPR))
3072 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3075 /* Replace a nonvolatile const static variable with its value. */
3076 if (optimize && TREE_CODE (value) == VAR_DECL)
3077 value = decl_constant_value (value);
3078 value = convert (type, value);
3080 /* Ignore any integer overflow caused by the cast. */
3081 if (TREE_CODE (value) == INTEGER_CST)
3083 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3085 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3086 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3090 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3091 if (TREE_CODE (value) == INTEGER_CST
3092 && TREE_CODE (expr) == INTEGER_CST
3093 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3094 value = non_lvalue (value);
3096 /* Don't let a cast be an lvalue. */
3098 value = non_lvalue (value);
3103 /* Interpret a cast of expression EXPR to type TYPE. */
3105 c_cast_expr (tree type, tree expr)
3107 int saved_wsp = warn_strict_prototypes;
3109 /* This avoids warnings about unprototyped casts on
3110 integers. E.g. "#define SIG_DFL (void(*)())0". */
3111 if (TREE_CODE (expr) == INTEGER_CST)
3112 warn_strict_prototypes = 0;
3113 type = groktypename (type);
3114 warn_strict_prototypes = saved_wsp;
3116 return build_c_cast (type, expr);
3120 /* Build an assignment expression of lvalue LHS from value RHS.
3121 MODIFYCODE is the code for a binary operator that we use
3122 to combine the old value of LHS with RHS to get the new value.
3123 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3126 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3130 tree lhstype = TREE_TYPE (lhs);
3131 tree olhstype = lhstype;
3133 /* Types that aren't fully specified cannot be used in assignments. */
3134 lhs = require_complete_type (lhs);
3136 /* Avoid duplicate error messages from operands that had errors. */
3137 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3138 return error_mark_node;
3140 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3141 /* Do not use STRIP_NOPS here. We do not want an enumerator
3142 whose value is 0 to count as a null pointer constant. */
3143 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3144 rhs = TREE_OPERAND (rhs, 0);
3148 /* If a binary op has been requested, combine the old LHS value with the RHS
3149 producing the value we should actually store into the LHS. */
3151 if (modifycode != NOP_EXPR)
3153 lhs = stabilize_reference (lhs);
3154 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3157 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3158 return error_mark_node;
3160 /* Warn about storing in something that is `const'. */
3162 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3163 || ((TREE_CODE (lhstype) == RECORD_TYPE
3164 || TREE_CODE (lhstype) == UNION_TYPE)
3165 && C_TYPE_FIELDS_READONLY (lhstype)))
3166 readonly_error (lhs, "assignment");
3168 /* If storing into a structure or union member,
3169 it has probably been given type `int'.
3170 Compute the type that would go with
3171 the actual amount of storage the member occupies. */
3173 if (TREE_CODE (lhs) == COMPONENT_REF
3174 && (TREE_CODE (lhstype) == INTEGER_TYPE
3175 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3176 || TREE_CODE (lhstype) == REAL_TYPE
3177 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3178 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3180 /* If storing in a field that is in actuality a short or narrower than one,
3181 we must store in the field in its actual type. */
3183 if (lhstype != TREE_TYPE (lhs))
3185 lhs = copy_node (lhs);
3186 TREE_TYPE (lhs) = lhstype;
3189 /* Convert new value to destination type. */
3191 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3192 NULL_TREE, NULL_TREE, 0);
3193 if (TREE_CODE (newrhs) == ERROR_MARK)
3194 return error_mark_node;
3198 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3199 TREE_SIDE_EFFECTS (result) = 1;
3201 /* If we got the LHS in a different type for storing in,
3202 convert the result back to the nominal type of LHS
3203 so that the value we return always has the same type
3204 as the LHS argument. */
3206 if (olhstype == TREE_TYPE (result))
3208 return convert_for_assignment (olhstype, result, _("assignment"),
3209 NULL_TREE, NULL_TREE, 0);
3212 /* Convert value RHS to type TYPE as preparation for an assignment
3213 to an lvalue of type TYPE.
3214 The real work of conversion is done by `convert'.
3215 The purpose of this function is to generate error messages
3216 for assignments that are not allowed in C.
3217 ERRTYPE is a string to use in error messages:
3218 "assignment", "return", etc. If it is null, this is parameter passing
3219 for a function call (and different error messages are output).
3221 FUNNAME is the name of the function being called,
3222 as an IDENTIFIER_NODE, or null.
3223 PARMNUM is the number of the argument, for printing in error messages. */
3226 convert_for_assignment (tree type, tree rhs, const char *errtype,
3227 tree fundecl, tree funname, int parmnum)
3229 enum tree_code codel = TREE_CODE (type);
3231 enum tree_code coder;
3233 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3234 /* Do not use STRIP_NOPS here. We do not want an enumerator
3235 whose value is 0 to count as a null pointer constant. */
3236 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3237 rhs = TREE_OPERAND (rhs, 0);
3239 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3240 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3241 rhs = default_conversion (rhs);
3242 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3243 rhs = decl_constant_value_for_broken_optimization (rhs);
3245 rhstype = TREE_TYPE (rhs);
3246 coder = TREE_CODE (rhstype);
3248 if (coder == ERROR_MARK)
3249 return error_mark_node;
3251 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3253 overflow_warning (rhs);
3254 /* Check for Objective-C protocols. This will automatically
3255 issue a warning if there are protocol violations. No need to
3256 use the return value. */
3257 if (c_dialect_objc ())
3258 objc_comptypes (type, rhstype, 0);
3262 if (coder == VOID_TYPE)
3264 error ("void value not ignored as it ought to be");
3265 return error_mark_node;
3267 /* A type converts to a reference to it.
3268 This code doesn't fully support references, it's just for the
3269 special case of va_start and va_copy. */
3270 if (codel == REFERENCE_TYPE
3271 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3273 if (!lvalue_p (rhs))
3275 error ("cannot pass rvalue to reference parameter");
3276 return error_mark_node;
3278 if (!c_mark_addressable (rhs))
3279 return error_mark_node;
3280 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3282 /* We already know that these two types are compatible, but they
3283 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3284 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3285 likely to be va_list, a typedef to __builtin_va_list, which
3286 is different enough that it will cause problems later. */
3287 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3288 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3290 rhs = build1 (NOP_EXPR, type, rhs);
3293 /* Some types can interconvert without explicit casts. */
3294 else if (codel == VECTOR_TYPE
3295 && comptypes (type, TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3296 return convert (type, rhs);
3297 /* Arithmetic types all interconvert, and enum is treated like int. */
3298 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3299 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3300 || codel == BOOLEAN_TYPE)
3301 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3302 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3303 || coder == BOOLEAN_TYPE))
3304 return convert_and_check (type, rhs);
3306 /* Conversion to a transparent union from its member types.
3307 This applies only to function arguments. */
3308 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3311 tree marginal_memb_type = 0;
3313 for (memb_types = TYPE_FIELDS (type); memb_types;
3314 memb_types = TREE_CHAIN (memb_types))
3316 tree memb_type = TREE_TYPE (memb_types);
3318 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3319 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3322 if (TREE_CODE (memb_type) != POINTER_TYPE)
3325 if (coder == POINTER_TYPE)
3327 tree ttl = TREE_TYPE (memb_type);
3328 tree ttr = TREE_TYPE (rhstype);
3330 /* Any non-function converts to a [const][volatile] void *
3331 and vice versa; otherwise, targets must be the same.
3332 Meanwhile, the lhs target must have all the qualifiers of
3334 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3335 || comp_target_types (memb_type, rhstype, 0))
3337 /* If this type won't generate any warnings, use it. */
3338 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3339 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3340 && TREE_CODE (ttl) == FUNCTION_TYPE)
3341 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3342 == TYPE_QUALS (ttr))
3343 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3344 == TYPE_QUALS (ttl))))
3347 /* Keep looking for a better type, but remember this one. */
3348 if (! marginal_memb_type)
3349 marginal_memb_type = memb_type;
3353 /* Can convert integer zero to any pointer type. */
3354 if (integer_zerop (rhs)
3355 || (TREE_CODE (rhs) == NOP_EXPR
3356 && integer_zerop (TREE_OPERAND (rhs, 0))))
3358 rhs = null_pointer_node;
3363 if (memb_types || marginal_memb_type)
3367 /* We have only a marginally acceptable member type;
3368 it needs a warning. */
3369 tree ttl = TREE_TYPE (marginal_memb_type);
3370 tree ttr = TREE_TYPE (rhstype);
3372 /* Const and volatile mean something different for function
3373 types, so the usual warnings are not appropriate. */
3374 if (TREE_CODE (ttr) == FUNCTION_TYPE
3375 && TREE_CODE (ttl) == FUNCTION_TYPE)
3377 /* Because const and volatile on functions are
3378 restrictions that say the function will not do
3379 certain things, it is okay to use a const or volatile
3380 function where an ordinary one is wanted, but not
3382 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3383 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3384 errtype, funname, parmnum);
3386 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3387 warn_for_assignment ("%s discards qualifiers from pointer target type",
3392 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3393 pedwarn ("ISO C prohibits argument conversion to union type");
3395 return build1 (NOP_EXPR, type, rhs);
3399 /* Conversions among pointers */
3400 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3401 && (coder == codel))
3403 tree ttl = TREE_TYPE (type);
3404 tree ttr = TREE_TYPE (rhstype);
3405 bool is_opaque_pointer;
3406 int target_cmp = 0; /* Cache comp_target_types () result. */
3408 /* Opaque pointers are treated like void pointers. */