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 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
596 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), 0);
602 return attrval == 2 && val == 1 ? 2 : val;
605 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
606 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
607 to 1 or 0 depending if the check of the pointer types is meant to
608 be reflexive or not (typically, assignments are not reflexive,
609 while comparisons are reflexive).
613 comp_target_types (tree ttl, tree ttr, int reflexive)
617 /* Give objc_comptypes a crack at letting these types through. */
618 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
621 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
622 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
624 if (val == 2 && pedantic)
625 pedwarn ("types are not quite compatible");
629 /* Subroutines of `comptypes'. */
631 /* Determine whether two trees derive from the same translation unit.
632 If the CONTEXT chain ends in a null, that tree's context is still
633 being parsed, so if two trees have context chains ending in null,
634 they're in the same translation unit. */
636 same_translation_unit_p (tree t1, tree t2)
638 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
639 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
641 case 'd': t1 = DECL_CONTEXT (t1); break;
642 case 't': t1 = TYPE_CONTEXT (t1); break;
643 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
647 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
648 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
650 case 'd': t2 = DECL_CONTEXT (t2); break;
651 case 't': t2 = TYPE_CONTEXT (t2); break;
652 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
659 /* The C standard says that two structures in different translation
660 units are compatible with each other only if the types of their
661 fields are compatible (among other things). So, consider two copies
662 of this structure: */
664 struct tagged_tu_seen {
665 const struct tagged_tu_seen * next;
670 /* Can they be compatible with each other? We choose to break the
671 recursion by allowing those types to be compatible. */
673 static const struct tagged_tu_seen * tagged_tu_seen_base;
675 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
676 compatible. If the two types are not the same (which has been
677 checked earlier), this can only happen when multiple translation
678 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
682 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
685 bool needs_warning = false;
687 /* We have to verify that the tags of the types are the same. This
688 is harder than it looks because this may be a typedef, so we have
689 to go look at the original type. It may even be a typedef of a
691 while (TYPE_NAME (t1)
692 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
693 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
694 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
696 while (TYPE_NAME (t2)
697 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
698 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
699 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
701 /* C90 didn't have the requirement that the two tags be the same. */
702 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
705 /* C90 didn't say what happened if one or both of the types were
706 incomplete; we choose to follow C99 rules here, which is that they
708 if (TYPE_SIZE (t1) == NULL
709 || TYPE_SIZE (t2) == NULL)
713 const struct tagged_tu_seen * tts_i;
714 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
715 if (tts_i->t1 == t1 && tts_i->t2 == t2)
719 switch (TREE_CODE (t1))
724 /* Speed up the case where the type values are in the same order. */
725 tree tv1 = TYPE_VALUES (t1);
726 tree tv2 = TYPE_VALUES (t2);
731 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
733 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
735 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
739 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
741 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
744 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
747 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
749 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
751 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
759 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
762 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
765 struct tagged_tu_seen tts;
767 tts.next = tagged_tu_seen_base;
770 tagged_tu_seen_base = &tts;
772 if (DECL_NAME (s1) != NULL)
773 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
774 if (DECL_NAME (s1) == DECL_NAME (s2))
777 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
781 needs_warning = true;
783 if (TREE_CODE (s1) == FIELD_DECL
784 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
785 DECL_FIELD_BIT_OFFSET (s2)) != 1)
791 tagged_tu_seen_base = tts.next;
795 return needs_warning ? 2 : 1;
800 struct tagged_tu_seen tts;
802 tts.next = tagged_tu_seen_base;
805 tagged_tu_seen_base = &tts;
807 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
809 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
812 if (TREE_CODE (s1) != TREE_CODE (s2)
813 || DECL_NAME (s1) != DECL_NAME (s2))
815 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
819 needs_warning = true;
821 if (TREE_CODE (s1) == FIELD_DECL
822 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
823 DECL_FIELD_BIT_OFFSET (s2)) != 1)
826 tagged_tu_seen_base = tts.next;
829 return needs_warning ? 2 : 1;
837 /* Return 1 if two function types F1 and F2 are compatible.
838 If either type specifies no argument types,
839 the other must specify a fixed number of self-promoting arg types.
840 Otherwise, if one type specifies only the number of arguments,
841 the other must specify that number of self-promoting arg types.
842 Otherwise, the argument types must match. */
845 function_types_compatible_p (tree f1, tree f2, int flags)
848 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
853 ret1 = TREE_TYPE (f1);
854 ret2 = TREE_TYPE (f2);
856 /* 'volatile' qualifiers on a function's return type mean the function
858 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
859 pedwarn ("function return types not compatible due to `volatile'");
860 if (TYPE_VOLATILE (ret1))
861 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
862 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
863 if (TYPE_VOLATILE (ret2))
864 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
865 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
866 val = comptypes (ret1, ret2, flags);
870 args1 = TYPE_ARG_TYPES (f1);
871 args2 = TYPE_ARG_TYPES (f2);
873 /* An unspecified parmlist matches any specified parmlist
874 whose argument types don't need default promotions. */
878 if (!self_promoting_args_p (args2))
880 /* If one of these types comes from a non-prototype fn definition,
881 compare that with the other type's arglist.
882 If they don't match, ask for a warning (but no error). */
883 if (TYPE_ACTUAL_ARG_TYPES (f1)
884 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
891 if (!self_promoting_args_p (args1))
893 if (TYPE_ACTUAL_ARG_TYPES (f2)
894 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
900 /* Both types have argument lists: compare them and propagate results. */
901 val1 = type_lists_compatible_p (args1, args2, flags);
902 return val1 != 1 ? val1 : val;
905 /* Check two lists of types for compatibility,
906 returning 0 for incompatible, 1 for compatible,
907 or 2 for compatible with warning. */
910 type_lists_compatible_p (tree args1, tree args2, int flags)
912 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
918 if (args1 == 0 && args2 == 0)
920 /* If one list is shorter than the other,
921 they fail to match. */
922 if (args1 == 0 || args2 == 0)
924 /* A null pointer instead of a type
925 means there is supposed to be an argument
926 but nothing is specified about what type it has.
927 So match anything that self-promotes. */
928 if (TREE_VALUE (args1) == 0)
930 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
933 else if (TREE_VALUE (args2) == 0)
935 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
938 /* If one of the lists has an error marker, ignore this arg. */
939 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
940 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
942 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
943 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
946 /* Allow wait (union {union wait *u; int *i} *)
947 and wait (union wait *) to be compatible. */
948 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
949 && (TYPE_NAME (TREE_VALUE (args1)) == 0
950 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
951 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
952 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
953 TYPE_SIZE (TREE_VALUE (args2))))
956 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
957 memb; memb = TREE_CHAIN (memb))
958 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
964 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
965 && (TYPE_NAME (TREE_VALUE (args2)) == 0
966 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
967 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
968 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
969 TYPE_SIZE (TREE_VALUE (args1))))
972 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
973 memb; memb = TREE_CHAIN (memb))
974 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
984 /* comptypes said ok, but record if it said to warn. */
988 args1 = TREE_CHAIN (args1);
989 args2 = TREE_CHAIN (args2);
993 /* Compute the size to increment a pointer by. */
996 c_size_in_bytes (tree type)
998 enum tree_code code = TREE_CODE (type);
1000 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1001 return size_one_node;
1003 if (!COMPLETE_OR_VOID_TYPE_P (type))
1005 error ("arithmetic on pointer to an incomplete type");
1006 return size_one_node;
1009 /* Convert in case a char is more than one unit. */
1010 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1011 size_int (TYPE_PRECISION (char_type_node)
1015 /* Return either DECL or its known constant value (if it has one). */
1018 decl_constant_value (tree decl)
1020 if (/* Don't change a variable array bound or initial value to a constant
1021 in a place where a variable is invalid. Note that DECL_INITIAL
1022 isn't valid for a PARM_DECL. */
1023 current_function_decl != 0
1024 && TREE_CODE (decl) != PARM_DECL
1025 && ! TREE_THIS_VOLATILE (decl)
1026 && TREE_READONLY (decl)
1027 && DECL_INITIAL (decl) != 0
1028 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1029 /* This is invalid if initial value is not constant.
1030 If it has either a function call, a memory reference,
1031 or a variable, then re-evaluating it could give different results. */
1032 && TREE_CONSTANT (DECL_INITIAL (decl))
1033 /* Check for cases where this is sub-optimal, even though valid. */
1034 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1035 return DECL_INITIAL (decl);
1039 /* Return either DECL or its known constant value (if it has one), but
1040 return DECL if pedantic or DECL has mode BLKmode. This is for
1041 bug-compatibility with the old behavior of decl_constant_value
1042 (before GCC 3.0); every use of this function is a bug and it should
1043 be removed before GCC 3.1. It is not appropriate to use pedantic
1044 in a way that affects optimization, and BLKmode is probably not the
1045 right test for avoiding misoptimizations either. */
1048 decl_constant_value_for_broken_optimization (tree decl)
1050 if (pedantic || DECL_MODE (decl) == BLKmode)
1053 return decl_constant_value (decl);
1057 /* Perform the default conversion of arrays and functions to pointers.
1058 Return the result of converting EXP. For any other expression, just
1062 default_function_array_conversion (tree exp)
1065 tree type = TREE_TYPE (exp);
1066 enum tree_code code = TREE_CODE (type);
1069 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1072 Do not use STRIP_NOPS here! It will remove conversions from pointer
1073 to integer and cause infinite recursion. */
1075 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1076 || (TREE_CODE (exp) == NOP_EXPR
1077 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1079 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1081 exp = TREE_OPERAND (exp, 0);
1084 /* Preserve the original expression code. */
1085 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1086 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1088 if (code == FUNCTION_TYPE)
1090 return build_unary_op (ADDR_EXPR, exp, 0);
1092 if (code == ARRAY_TYPE)
1095 tree restype = TREE_TYPE (type);
1101 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1103 constp = TREE_READONLY (exp);
1104 volatilep = TREE_THIS_VOLATILE (exp);
1107 if (TYPE_QUALS (type) || constp || volatilep)
1109 = c_build_qualified_type (restype,
1111 | (constp * TYPE_QUAL_CONST)
1112 | (volatilep * TYPE_QUAL_VOLATILE));
1114 if (TREE_CODE (exp) == INDIRECT_REF)
1115 return convert (TYPE_POINTER_TO (restype),
1116 TREE_OPERAND (exp, 0));
1118 if (TREE_CODE (exp) == COMPOUND_EXPR)
1120 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1121 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1122 TREE_OPERAND (exp, 0), op1);
1125 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1126 if (!flag_isoc99 && !lvalue_array_p)
1128 /* Before C99, non-lvalue arrays do not decay to pointers.
1129 Normally, using such an array would be invalid; but it can
1130 be used correctly inside sizeof or as a statement expression.
1131 Thus, do not give an error here; an error will result later. */
1135 ptrtype = build_pointer_type (restype);
1137 if (TREE_CODE (exp) == VAR_DECL)
1139 /* ??? This is not really quite correct
1140 in that the type of the operand of ADDR_EXPR
1141 is not the target type of the type of the ADDR_EXPR itself.
1142 Question is, can this lossage be avoided? */
1143 adr = build1 (ADDR_EXPR, ptrtype, exp);
1144 if (!c_mark_addressable (exp))
1145 return error_mark_node;
1146 TREE_CONSTANT (adr) = staticp (exp);
1147 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1150 /* This way is better for a COMPONENT_REF since it can
1151 simplify the offset for a component. */
1152 adr = build_unary_op (ADDR_EXPR, exp, 1);
1153 return convert (ptrtype, adr);
1158 /* Perform default promotions for C data used in expressions.
1159 Arrays and functions are converted to pointers;
1160 enumeral types or short or char, to int.
1161 In addition, manifest constants symbols are replaced by their values. */
1164 default_conversion (tree exp)
1167 tree type = TREE_TYPE (exp);
1168 enum tree_code code = TREE_CODE (type);
1170 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1171 return default_function_array_conversion (exp);
1173 /* Constants can be used directly unless they're not loadable. */
1174 if (TREE_CODE (exp) == CONST_DECL)
1175 exp = DECL_INITIAL (exp);
1177 /* Replace a nonvolatile const static variable with its value unless
1178 it is an array, in which case we must be sure that taking the
1179 address of the array produces consistent results. */
1180 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1182 exp = decl_constant_value_for_broken_optimization (exp);
1183 type = TREE_TYPE (exp);
1186 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1189 Do not use STRIP_NOPS here! It will remove conversions from pointer
1190 to integer and cause infinite recursion. */
1192 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1193 || (TREE_CODE (exp) == NOP_EXPR
1194 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1195 exp = TREE_OPERAND (exp, 0);
1197 /* Preserve the original expression code. */
1198 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1199 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1201 /* Normally convert enums to int,
1202 but convert wide enums to something wider. */
1203 if (code == ENUMERAL_TYPE)
1205 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1206 TYPE_PRECISION (integer_type_node)),
1207 ((TYPE_PRECISION (type)
1208 >= TYPE_PRECISION (integer_type_node))
1209 && TYPE_UNSIGNED (type)));
1211 return convert (type, exp);
1214 if (TREE_CODE (exp) == COMPONENT_REF
1215 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1216 /* If it's thinner than an int, promote it like a
1217 c_promoting_integer_type_p, otherwise leave it alone. */
1218 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1219 TYPE_PRECISION (integer_type_node)))
1220 return convert (integer_type_node, exp);
1222 if (c_promoting_integer_type_p (type))
1224 /* Preserve unsignedness if not really getting any wider. */
1225 if (TYPE_UNSIGNED (type)
1226 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1227 return convert (unsigned_type_node, exp);
1229 return convert (integer_type_node, exp);
1232 if (code == VOID_TYPE)
1234 error ("void value not ignored as it ought to be");
1235 return error_mark_node;
1240 /* Look up COMPONENT in a structure or union DECL.
1242 If the component name is not found, returns NULL_TREE. Otherwise,
1243 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1244 stepping down the chain to the component, which is in the last
1245 TREE_VALUE of the list. Normally the list is of length one, but if
1246 the component is embedded within (nested) anonymous structures or
1247 unions, the list steps down the chain to the component. */
1250 lookup_field (tree decl, tree component)
1252 tree type = TREE_TYPE (decl);
1255 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1256 to the field elements. Use a binary search on this array to quickly
1257 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1258 will always be set for structures which have many elements. */
1260 if (TYPE_LANG_SPECIFIC (type))
1263 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1265 field = TYPE_FIELDS (type);
1267 top = TYPE_LANG_SPECIFIC (type)->s->len;
1268 while (top - bot > 1)
1270 half = (top - bot + 1) >> 1;
1271 field = field_array[bot+half];
1273 if (DECL_NAME (field) == NULL_TREE)
1275 /* Step through all anon unions in linear fashion. */
1276 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1278 field = field_array[bot++];
1279 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1280 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1282 tree anon = lookup_field (field, component);
1285 return tree_cons (NULL_TREE, field, anon);
1289 /* Entire record is only anon unions. */
1293 /* Restart the binary search, with new lower bound. */
1297 if (DECL_NAME (field) == component)
1299 if (DECL_NAME (field) < component)
1305 if (DECL_NAME (field_array[bot]) == component)
1306 field = field_array[bot];
1307 else if (DECL_NAME (field) != component)
1312 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1314 if (DECL_NAME (field) == NULL_TREE
1315 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1316 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1318 tree anon = lookup_field (field, component);
1321 return tree_cons (NULL_TREE, field, anon);
1324 if (DECL_NAME (field) == component)
1328 if (field == NULL_TREE)
1332 return tree_cons (NULL_TREE, field, NULL_TREE);
1335 /* Make an expression to refer to the COMPONENT field of
1336 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1339 build_component_ref (tree datum, tree component)
1341 tree type = TREE_TYPE (datum);
1342 enum tree_code code = TREE_CODE (type);
1346 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1347 Ensure that the arguments are not lvalues; otherwise,
1348 if the component is an array, it would wrongly decay to a pointer in
1350 We cannot do this with a COND_EXPR, because in a conditional expression
1351 the default promotions are applied to both sides, and this would yield
1352 the wrong type of the result; for example, if the components have
1354 switch (TREE_CODE (datum))
1358 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1359 return build (COMPOUND_EXPR, TREE_TYPE (value),
1360 TREE_OPERAND (datum, 0), non_lvalue (value));
1366 /* See if there is a field or component with name COMPONENT. */
1368 if (code == RECORD_TYPE || code == UNION_TYPE)
1370 if (!COMPLETE_TYPE_P (type))
1372 c_incomplete_type_error (NULL_TREE, type);
1373 return error_mark_node;
1376 field = lookup_field (datum, component);
1380 error ("%s has no member named `%s'",
1381 code == RECORD_TYPE ? "structure" : "union",
1382 IDENTIFIER_POINTER (component));
1383 return error_mark_node;
1386 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1387 This might be better solved in future the way the C++ front
1388 end does it - by giving the anonymous entities each a
1389 separate name and type, and then have build_component_ref
1390 recursively call itself. We can't do that here. */
1393 tree subdatum = TREE_VALUE (field);
1395 if (TREE_TYPE (subdatum) == error_mark_node)
1396 return error_mark_node;
1398 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1399 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1400 TREE_READONLY (ref) = 1;
1401 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1402 TREE_THIS_VOLATILE (ref) = 1;
1404 if (TREE_DEPRECATED (subdatum))
1405 warn_deprecated_use (subdatum);
1409 field = TREE_CHAIN (field);
1415 else if (code != ERROR_MARK)
1416 error ("request for member `%s' in something not a structure or union",
1417 IDENTIFIER_POINTER (component));
1419 return error_mark_node;
1422 /* Given an expression PTR for a pointer, return an expression
1423 for the value pointed to.
1424 ERRORSTRING is the name of the operator to appear in error messages. */
1427 build_indirect_ref (tree ptr, const char *errorstring)
1429 tree pointer = default_conversion (ptr);
1430 tree type = TREE_TYPE (pointer);
1432 if (TREE_CODE (type) == POINTER_TYPE)
1434 if (TREE_CODE (pointer) == ADDR_EXPR
1435 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1436 == TREE_TYPE (type)))
1437 return TREE_OPERAND (pointer, 0);
1440 tree t = TREE_TYPE (type);
1441 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1443 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1445 error ("dereferencing pointer to incomplete type");
1446 return error_mark_node;
1448 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1449 warning ("dereferencing `void *' pointer");
1451 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1452 so that we get the proper error message if the result is used
1453 to assign to. Also, &* is supposed to be a no-op.
1454 And ANSI C seems to specify that the type of the result
1455 should be the const type. */
1456 /* A de-reference of a pointer to const is not a const. It is valid
1457 to change it via some other pointer. */
1458 TREE_READONLY (ref) = TYPE_READONLY (t);
1459 TREE_SIDE_EFFECTS (ref)
1460 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1461 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1465 else if (TREE_CODE (pointer) != ERROR_MARK)
1466 error ("invalid type argument of `%s'", errorstring);
1467 return error_mark_node;
1470 /* This handles expressions of the form "a[i]", which denotes
1473 This is logically equivalent in C to *(a+i), but we may do it differently.
1474 If A is a variable or a member, we generate a primitive ARRAY_REF.
1475 This avoids forcing the array out of registers, and can work on
1476 arrays that are not lvalues (for example, members of structures returned
1480 build_array_ref (tree array, tree index)
1484 error ("subscript missing in array reference");
1485 return error_mark_node;
1488 if (TREE_TYPE (array) == error_mark_node
1489 || TREE_TYPE (index) == error_mark_node)
1490 return error_mark_node;
1492 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1493 && TREE_CODE (array) != INDIRECT_REF)
1497 /* Subscripting with type char is likely to lose
1498 on a machine where chars are signed.
1499 So warn on any machine, but optionally.
1500 Don't warn for unsigned char since that type is safe.
1501 Don't warn for signed char because anyone who uses that
1502 must have done so deliberately. */
1503 if (warn_char_subscripts
1504 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1505 warning ("array subscript has type `char'");
1507 /* Apply default promotions *after* noticing character types. */
1508 index = default_conversion (index);
1510 /* Require integer *after* promotion, for sake of enums. */
1511 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1513 error ("array subscript is not an integer");
1514 return error_mark_node;
1517 /* An array that is indexed by a non-constant
1518 cannot be stored in a register; we must be able to do
1519 address arithmetic on its address.
1520 Likewise an array of elements of variable size. */
1521 if (TREE_CODE (index) != INTEGER_CST
1522 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1523 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1525 if (!c_mark_addressable (array))
1526 return error_mark_node;
1528 /* An array that is indexed by a constant value which is not within
1529 the array bounds cannot be stored in a register either; because we
1530 would get a crash in store_bit_field/extract_bit_field when trying
1531 to access a non-existent part of the register. */
1532 if (TREE_CODE (index) == INTEGER_CST
1533 && TYPE_DOMAIN (TREE_TYPE (array))
1534 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1536 if (!c_mark_addressable (array))
1537 return error_mark_node;
1543 while (TREE_CODE (foo) == COMPONENT_REF)
1544 foo = TREE_OPERAND (foo, 0);
1545 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1546 pedwarn ("ISO C forbids subscripting `register' array");
1547 else if (! flag_isoc99 && ! lvalue_p (foo))
1548 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1551 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1552 rval = build (ARRAY_REF, type, array, index);
1553 /* Array ref is const/volatile if the array elements are
1554 or if the array is. */
1555 TREE_READONLY (rval)
1556 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1557 | TREE_READONLY (array));
1558 TREE_SIDE_EFFECTS (rval)
1559 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1560 | TREE_SIDE_EFFECTS (array));
1561 TREE_THIS_VOLATILE (rval)
1562 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1563 /* This was added by rms on 16 Nov 91.
1564 It fixes vol struct foo *a; a->elts[1]
1565 in an inline function.
1566 Hope it doesn't break something else. */
1567 | TREE_THIS_VOLATILE (array));
1568 return require_complete_type (fold (rval));
1572 tree ar = default_conversion (array);
1573 tree ind = default_conversion (index);
1575 /* Do the same warning check as above, but only on the part that's
1576 syntactically the index and only if it is also semantically
1578 if (warn_char_subscripts
1579 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1580 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1581 warning ("subscript has type `char'");
1583 /* Put the integer in IND to simplify error checking. */
1584 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1591 if (ar == error_mark_node)
1594 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1595 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1597 error ("subscripted value is neither array nor pointer");
1598 return error_mark_node;
1600 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1602 error ("array subscript is not an integer");
1603 return error_mark_node;
1606 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1611 /* Build an external reference to identifier ID. FUN indicates
1612 whether this will be used for a function call. */
1614 build_external_ref (tree id, int fun)
1617 tree decl = lookup_name (id);
1618 tree objc_ivar = lookup_objc_ivar (id);
1620 if (decl && decl != error_mark_node)
1622 /* Properly declared variable or function reference. */
1625 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1627 warning ("local declaration of `%s' hides instance variable",
1628 IDENTIFIER_POINTER (id));
1637 /* Implicit function declaration. */
1638 ref = implicitly_declare (id);
1639 else if (decl == error_mark_node)
1640 /* Don't complain about something that's already been
1641 complained about. */
1642 return error_mark_node;
1645 undeclared_variable (id);
1646 return error_mark_node;
1649 if (TREE_TYPE (ref) == error_mark_node)
1650 return error_mark_node;
1652 if (TREE_DEPRECATED (ref))
1653 warn_deprecated_use (ref);
1655 if (!skip_evaluation)
1656 assemble_external (ref);
1657 TREE_USED (ref) = 1;
1659 if (TREE_CODE (ref) == CONST_DECL)
1661 ref = DECL_INITIAL (ref);
1662 TREE_CONSTANT (ref) = 1;
1664 else if (current_function_decl != 0
1665 && !DECL_FILE_SCOPE_P (current_function_decl)
1666 && (TREE_CODE (ref) == VAR_DECL
1667 || TREE_CODE (ref) == PARM_DECL
1668 || TREE_CODE (ref) == FUNCTION_DECL))
1670 tree context = decl_function_context (ref);
1672 if (context != 0 && context != current_function_decl)
1673 DECL_NONLOCAL (ref) = 1;
1679 /* Build a function call to function FUNCTION with parameters PARAMS.
1680 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1681 TREE_VALUE of each node is a parameter-expression.
1682 FUNCTION's data type may be a function type or a pointer-to-function. */
1685 build_function_call (tree function, tree params)
1687 tree fntype, fundecl = 0;
1688 tree coerced_params;
1689 tree name = NULL_TREE, result;
1692 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1693 STRIP_TYPE_NOPS (function);
1695 /* Convert anything with function type to a pointer-to-function. */
1696 if (TREE_CODE (function) == FUNCTION_DECL)
1698 name = DECL_NAME (function);
1700 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1701 (because calling an inline function does not mean the function
1702 needs to be separately compiled). */
1703 fntype = build_type_variant (TREE_TYPE (function),
1704 TREE_READONLY (function),
1705 TREE_THIS_VOLATILE (function));
1707 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1710 function = default_conversion (function);
1712 fntype = TREE_TYPE (function);
1714 if (TREE_CODE (fntype) == ERROR_MARK)
1715 return error_mark_node;
1717 if (!(TREE_CODE (fntype) == POINTER_TYPE
1718 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1720 error ("called object is not a function");
1721 return error_mark_node;
1724 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1725 current_function_returns_abnormally = 1;
1727 /* fntype now gets the type of function pointed to. */
1728 fntype = TREE_TYPE (fntype);
1730 /* Check that the function is called through a compatible prototype.
1731 If it is not, replace the call by a trap, wrapped up in a compound
1732 expression if necessary. This has the nice side-effect to prevent
1733 the tree-inliner from generating invalid assignment trees which may
1734 blow up in the RTL expander later.
1736 ??? This doesn't work for Objective-C because objc_comptypes
1737 refuses to compare function prototypes, yet the compiler appears
1738 to build calls that are flagged as invalid by C's comptypes. */
1739 if (! c_dialect_objc ()
1740 && TREE_CODE (function) == NOP_EXPR
1741 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1742 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1743 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1745 tree return_type = TREE_TYPE (fntype);
1746 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1749 /* This situation leads to run-time undefined behavior. We can't,
1750 therefore, simply error unless we can prove that all possible
1751 executions of the program must execute the code. */
1752 warning ("function called through a non-compatible type");
1754 /* We can, however, treat "undefined" any way we please.
1755 Call abort to encourage the user to fix the program. */
1756 inform ("if this code is reached, the program will abort");
1758 if (VOID_TYPE_P (return_type))
1764 if (AGGREGATE_TYPE_P (return_type))
1765 rhs = build_compound_literal (return_type,
1766 build_constructor (return_type,
1769 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1771 return build (COMPOUND_EXPR, return_type, trap, rhs);
1775 /* Convert the parameters to the types declared in the
1776 function prototype, or apply default promotions. */
1779 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1781 /* Check that the arguments to the function are valid. */
1783 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1785 /* Recognize certain built-in functions so we can make tree-codes
1786 other than CALL_EXPR. We do this when it enables fold-const.c
1787 to do something useful. */
1789 if (TREE_CODE (function) == ADDR_EXPR
1790 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1791 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1793 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1794 params, coerced_params);
1799 result = build (CALL_EXPR, TREE_TYPE (fntype),
1800 function, coerced_params, NULL_TREE);
1801 TREE_SIDE_EFFECTS (result) = 1;
1802 result = fold (result);
1804 if (VOID_TYPE_P (TREE_TYPE (result)))
1806 return require_complete_type (result);
1809 /* Convert the argument expressions in the list VALUES
1810 to the types in the list TYPELIST. The result is a list of converted
1811 argument expressions.
1813 If TYPELIST is exhausted, or when an element has NULL as its type,
1814 perform the default conversions.
1816 PARMLIST is the chain of parm decls for the function being called.
1817 It may be 0, if that info is not available.
1818 It is used only for generating error messages.
1820 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1822 This is also where warnings about wrong number of args are generated.
1824 Both VALUES and the returned value are chains of TREE_LIST nodes
1825 with the elements of the list in the TREE_VALUE slots of those nodes. */
1828 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1830 tree typetail, valtail;
1834 /* Scan the given expressions and types, producing individual
1835 converted arguments and pushing them on RESULT in reverse order. */
1837 for (valtail = values, typetail = typelist, parmnum = 0;
1839 valtail = TREE_CHAIN (valtail), parmnum++)
1841 tree type = typetail ? TREE_VALUE (typetail) : 0;
1842 tree val = TREE_VALUE (valtail);
1844 if (type == void_type_node)
1847 error ("too many arguments to function `%s'",
1848 IDENTIFIER_POINTER (name));
1850 error ("too many arguments to function");
1854 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1855 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1856 to convert automatically to a pointer. */
1857 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1858 val = TREE_OPERAND (val, 0);
1860 val = default_function_array_conversion (val);
1862 val = require_complete_type (val);
1866 /* Formal parm type is specified by a function prototype. */
1869 if (!COMPLETE_TYPE_P (type))
1871 error ("type of formal parameter %d is incomplete", parmnum + 1);
1876 /* Optionally warn about conversions that
1877 differ from the default conversions. */
1878 if (warn_conversion || warn_traditional)
1880 int formal_prec = TYPE_PRECISION (type);
1882 if (INTEGRAL_TYPE_P (type)
1883 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1884 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1885 if (INTEGRAL_TYPE_P (type)
1886 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1887 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1888 else if (TREE_CODE (type) == COMPLEX_TYPE
1889 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1890 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1891 else if (TREE_CODE (type) == REAL_TYPE
1892 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1893 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1894 else if (TREE_CODE (type) == COMPLEX_TYPE
1895 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1896 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1897 else if (TREE_CODE (type) == REAL_TYPE
1898 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1899 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1900 /* ??? At some point, messages should be written about
1901 conversions between complex types, but that's too messy
1903 else if (TREE_CODE (type) == REAL_TYPE
1904 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1906 /* Warn if any argument is passed as `float',
1907 since without a prototype it would be `double'. */
1908 if (formal_prec == TYPE_PRECISION (float_type_node))
1909 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1911 /* Detect integer changing in width or signedness.
1912 These warnings are only activated with
1913 -Wconversion, not with -Wtraditional. */
1914 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1915 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1917 tree would_have_been = default_conversion (val);
1918 tree type1 = TREE_TYPE (would_have_been);
1920 if (TREE_CODE (type) == ENUMERAL_TYPE
1921 && (TYPE_MAIN_VARIANT (type)
1922 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1923 /* No warning if function asks for enum
1924 and the actual arg is that enum type. */
1926 else if (formal_prec != TYPE_PRECISION (type1))
1927 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1928 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
1930 /* Don't complain if the formal parameter type
1931 is an enum, because we can't tell now whether
1932 the value was an enum--even the same enum. */
1933 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1935 else if (TREE_CODE (val) == INTEGER_CST
1936 && int_fits_type_p (val, type))
1937 /* Change in signedness doesn't matter
1938 if a constant value is unaffected. */
1940 /* Likewise for a constant in a NOP_EXPR. */
1941 else if (TREE_CODE (val) == NOP_EXPR
1942 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1943 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1945 /* If the value is extended from a narrower
1946 unsigned type, it doesn't matter whether we
1947 pass it as signed or unsigned; the value
1948 certainly is the same either way. */
1949 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1950 && TYPE_UNSIGNED (TREE_TYPE (val)))
1952 else if (TYPE_UNSIGNED (type))
1953 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1955 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1959 parmval = convert_for_assignment (type, val,
1960 (char *) 0, /* arg passing */
1961 fundecl, name, parmnum + 1);
1963 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1964 && INTEGRAL_TYPE_P (type)
1965 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1966 parmval = default_conversion (parmval);
1968 result = tree_cons (NULL_TREE, parmval, result);
1970 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1971 && (TYPE_PRECISION (TREE_TYPE (val))
1972 < TYPE_PRECISION (double_type_node)))
1973 /* Convert `float' to `double'. */
1974 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1976 /* Convert `short' and `char' to full-size `int'. */
1977 result = tree_cons (NULL_TREE, default_conversion (val), result);
1980 typetail = TREE_CHAIN (typetail);
1983 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1986 error ("too few arguments to function `%s'",
1987 IDENTIFIER_POINTER (name));
1989 error ("too few arguments to function");
1992 return nreverse (result);
1995 /* This is the entry point used by the parser
1996 for binary operators in the input.
1997 In addition to constructing the expression,
1998 we check for operands that were written with other binary operators
1999 in a way that is likely to confuse the user. */
2002 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
2004 tree result = build_binary_op (code, arg1, arg2, 1);
2007 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2008 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2009 enum tree_code code1 = ERROR_MARK;
2010 enum tree_code code2 = ERROR_MARK;
2012 if (TREE_CODE (result) == ERROR_MARK)
2013 return error_mark_node;
2015 if (IS_EXPR_CODE_CLASS (class1))
2016 code1 = C_EXP_ORIGINAL_CODE (arg1);
2017 if (IS_EXPR_CODE_CLASS (class2))
2018 code2 = C_EXP_ORIGINAL_CODE (arg2);
2020 /* Check for cases such as x+y<<z which users are likely
2021 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2022 is cleared to prevent these warnings. */
2023 if (warn_parentheses)
2025 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2027 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2028 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2029 warning ("suggest parentheses around + or - inside shift");
2032 if (code == TRUTH_ORIF_EXPR)
2034 if (code1 == TRUTH_ANDIF_EXPR
2035 || code2 == TRUTH_ANDIF_EXPR)
2036 warning ("suggest parentheses around && within ||");
2039 if (code == BIT_IOR_EXPR)
2041 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2042 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2043 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2044 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2045 warning ("suggest parentheses around arithmetic in operand of |");
2046 /* Check cases like x|y==z */
2047 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2048 warning ("suggest parentheses around comparison in operand of |");
2051 if (code == BIT_XOR_EXPR)
2053 if (code1 == BIT_AND_EXPR
2054 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2055 || code2 == BIT_AND_EXPR
2056 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2057 warning ("suggest parentheses around arithmetic in operand of ^");
2058 /* Check cases like x^y==z */
2059 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2060 warning ("suggest parentheses around comparison in operand of ^");
2063 if (code == BIT_AND_EXPR)
2065 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2066 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2067 warning ("suggest parentheses around + or - in operand of &");
2068 /* Check cases like x&y==z */
2069 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2070 warning ("suggest parentheses around comparison in operand of &");
2074 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2075 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2076 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2077 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2079 unsigned_conversion_warning (result, arg1);
2080 unsigned_conversion_warning (result, arg2);
2081 overflow_warning (result);
2083 class = TREE_CODE_CLASS (TREE_CODE (result));
2085 /* Record the code that was specified in the source,
2086 for the sake of warnings about confusing nesting. */
2087 if (IS_EXPR_CODE_CLASS (class))
2088 C_SET_EXP_ORIGINAL_CODE (result, code);
2091 int flag = TREE_CONSTANT (result);
2092 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2093 so that convert_for_assignment wouldn't strip it.
2094 That way, we got warnings for things like p = (1 - 1).
2095 But it turns out we should not get those warnings. */
2096 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2097 C_SET_EXP_ORIGINAL_CODE (result, code);
2098 TREE_CONSTANT (result) = flag;
2105 /* Return true if `t' is known to be non-negative. */
2108 c_tree_expr_nonnegative_p (tree t)
2110 if (TREE_CODE (t) == STMT_EXPR)
2112 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2114 /* Find the last statement in the chain, ignoring the final
2115 * scope statement */
2116 while (TREE_CHAIN (t) != NULL_TREE
2117 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2119 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2121 return tree_expr_nonnegative_p (t);
2124 /* Return a tree for the difference of pointers OP0 and OP1.
2125 The resulting tree has type int. */
2128 pointer_diff (tree op0, tree op1)
2130 tree result, folded;
2131 tree restype = ptrdiff_type_node;
2133 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2134 tree con0, con1, lit0, lit1;
2135 tree orig_op1 = op1;
2137 if (pedantic || warn_pointer_arith)
2139 if (TREE_CODE (target_type) == VOID_TYPE)
2140 pedwarn ("pointer of type `void *' used in subtraction");
2141 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2142 pedwarn ("pointer to a function used in subtraction");
2145 /* If the conversion to ptrdiff_type does anything like widening or
2146 converting a partial to an integral mode, we get a convert_expression
2147 that is in the way to do any simplifications.
2148 (fold-const.c doesn't know that the extra bits won't be needed.
2149 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2150 different mode in place.)
2151 So first try to find a common term here 'by hand'; we want to cover
2152 at least the cases that occur in legal static initializers. */
2153 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2154 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2156 if (TREE_CODE (con0) == PLUS_EXPR)
2158 lit0 = TREE_OPERAND (con0, 1);
2159 con0 = TREE_OPERAND (con0, 0);
2162 lit0 = integer_zero_node;
2164 if (TREE_CODE (con1) == PLUS_EXPR)
2166 lit1 = TREE_OPERAND (con1, 1);
2167 con1 = TREE_OPERAND (con1, 0);
2170 lit1 = integer_zero_node;
2172 if (operand_equal_p (con0, con1, 0))
2179 /* First do the subtraction as integers;
2180 then drop through to build the divide operator.
2181 Do not do default conversions on the minus operator
2182 in case restype is a short type. */
2184 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2185 convert (restype, op1), 0);
2186 /* This generates an error if op1 is pointer to incomplete type. */
2187 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2188 error ("arithmetic on pointer to an incomplete type");
2190 /* This generates an error if op0 is pointer to incomplete type. */
2191 op1 = c_size_in_bytes (target_type);
2193 /* Divide by the size, in easiest possible way. */
2195 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2197 folded = fold (result);
2198 if (folded == result)
2199 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2203 /* Construct and perhaps optimize a tree representation
2204 for a unary operation. CODE, a tree_code, specifies the operation
2205 and XARG is the operand.
2206 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2207 the default promotions (such as from short to int).
2208 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2209 allows non-lvalues; this is only used to handle conversion of non-lvalue
2210 arrays to pointers in C99. */
2213 build_unary_op (enum tree_code code, tree xarg, int flag)
2215 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2218 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2220 int noconvert = flag;
2222 if (typecode == ERROR_MARK)
2223 return error_mark_node;
2224 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2225 typecode = INTEGER_TYPE;
2230 /* This is used for unary plus, because a CONVERT_EXPR
2231 is enough to prevent anybody from looking inside for
2232 associativity, but won't generate any code. */
2233 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2234 || typecode == COMPLEX_TYPE))
2236 error ("wrong type argument to unary plus");
2237 return error_mark_node;
2239 else if (!noconvert)
2240 arg = default_conversion (arg);
2241 arg = non_lvalue (arg);
2245 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2246 || typecode == COMPLEX_TYPE
2247 || typecode == VECTOR_TYPE))
2249 error ("wrong type argument to unary minus");
2250 return error_mark_node;
2252 else if (!noconvert)
2253 arg = default_conversion (arg);
2257 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2260 arg = default_conversion (arg);
2262 else if (typecode == COMPLEX_TYPE)
2266 pedwarn ("ISO C does not support `~' for complex conjugation");
2268 arg = default_conversion (arg);
2272 error ("wrong type argument to bit-complement");
2273 return error_mark_node;
2278 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2280 error ("wrong type argument to abs");
2281 return error_mark_node;
2283 else if (!noconvert)
2284 arg = default_conversion (arg);
2288 /* Conjugating a real value is a no-op, but allow it anyway. */
2289 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2290 || typecode == COMPLEX_TYPE))
2292 error ("wrong type argument to conjugation");
2293 return error_mark_node;
2295 else if (!noconvert)
2296 arg = default_conversion (arg);
2299 case TRUTH_NOT_EXPR:
2300 if (typecode != INTEGER_TYPE
2301 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2302 && typecode != COMPLEX_TYPE
2303 /* These will convert to a pointer. */
2304 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2306 error ("wrong type argument to unary exclamation mark");
2307 return error_mark_node;
2309 arg = lang_hooks.truthvalue_conversion (arg);
2310 return invert_truthvalue (arg);
2316 if (TREE_CODE (arg) == COMPLEX_CST)
2317 return TREE_REALPART (arg);
2318 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2319 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2324 if (TREE_CODE (arg) == COMPLEX_CST)
2325 return TREE_IMAGPART (arg);
2326 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2327 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2329 return convert (TREE_TYPE (arg), integer_zero_node);
2331 case PREINCREMENT_EXPR:
2332 case POSTINCREMENT_EXPR:
2333 case PREDECREMENT_EXPR:
2334 case POSTDECREMENT_EXPR:
2336 /* Increment or decrement the real part of the value,
2337 and don't change the imaginary part. */
2338 if (typecode == COMPLEX_TYPE)
2343 pedwarn ("ISO C does not support `++' and `--' on complex types");
2345 arg = stabilize_reference (arg);
2346 real = build_unary_op (REALPART_EXPR, arg, 1);
2347 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2348 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2349 build_unary_op (code, real, 1), imag);
2352 /* Report invalid types. */
2354 if (typecode != POINTER_TYPE
2355 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2357 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2358 error ("wrong type argument to increment");
2360 error ("wrong type argument to decrement");
2362 return error_mark_node;
2367 tree result_type = TREE_TYPE (arg);
2369 arg = get_unwidened (arg, 0);
2370 argtype = TREE_TYPE (arg);
2372 /* Compute the increment. */
2374 if (typecode == POINTER_TYPE)
2376 /* If pointer target is an undefined struct,
2377 we just cannot know how to do the arithmetic. */
2378 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2380 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2381 error ("increment of pointer to unknown structure");
2383 error ("decrement of pointer to unknown structure");
2385 else if ((pedantic || warn_pointer_arith)
2386 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2387 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2389 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2390 pedwarn ("wrong type argument to increment");
2392 pedwarn ("wrong type argument to decrement");
2395 inc = c_size_in_bytes (TREE_TYPE (result_type));
2398 inc = integer_one_node;
2400 inc = convert (argtype, inc);
2402 /* Complain about anything else that is not a true lvalue. */
2403 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2404 || code == POSTINCREMENT_EXPR)
2405 ? "invalid lvalue in increment"
2406 : "invalid lvalue in decrement")))
2407 return error_mark_node;
2409 /* Report a read-only lvalue. */
2410 if (TREE_READONLY (arg))
2411 readonly_error (arg,
2412 ((code == PREINCREMENT_EXPR
2413 || code == POSTINCREMENT_EXPR)
2414 ? "increment" : "decrement"));
2416 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2417 val = boolean_increment (code, arg);
2419 val = build (code, TREE_TYPE (arg), arg, inc);
2420 TREE_SIDE_EFFECTS (val) = 1;
2421 val = convert (result_type, val);
2422 if (TREE_CODE (val) != code)
2423 TREE_NO_UNUSED_WARNING (val) = 1;
2428 /* Note that this operation never does default_conversion. */
2430 /* Let &* cancel out to simplify resulting code. */
2431 if (TREE_CODE (arg) == INDIRECT_REF)
2433 /* Don't let this be an lvalue. */
2434 if (lvalue_p (TREE_OPERAND (arg, 0)))
2435 return non_lvalue (TREE_OPERAND (arg, 0));
2436 return TREE_OPERAND (arg, 0);
2439 /* For &x[y], return x+y */
2440 if (TREE_CODE (arg) == ARRAY_REF)
2442 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2443 return error_mark_node;
2444 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2445 TREE_OPERAND (arg, 1), 1);
2448 /* Anything not already handled and not a true memory reference
2449 or a non-lvalue array is an error. */
2450 else if (typecode != FUNCTION_TYPE && !flag
2451 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2452 return error_mark_node;
2454 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2455 argtype = TREE_TYPE (arg);
2457 /* If the lvalue is const or volatile, merge that into the type
2458 to which the address will point. Note that you can't get a
2459 restricted pointer by taking the address of something, so we
2460 only have to deal with `const' and `volatile' here. */
2461 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2462 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2463 argtype = c_build_type_variant (argtype,
2464 TREE_READONLY (arg),
2465 TREE_THIS_VOLATILE (arg));
2467 argtype = build_pointer_type (argtype);
2469 if (!c_mark_addressable (arg))
2470 return error_mark_node;
2475 if (TREE_CODE (arg) == COMPONENT_REF)
2477 tree field = TREE_OPERAND (arg, 1);
2479 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2481 if (DECL_C_BIT_FIELD (field))
2483 error ("attempt to take address of bit-field structure member `%s'",
2484 IDENTIFIER_POINTER (DECL_NAME (field)));
2485 return error_mark_node;
2488 addr = fold (build (PLUS_EXPR, argtype,
2489 convert (argtype, addr),
2490 convert (argtype, byte_position (field))));
2493 addr = build1 (code, argtype, arg);
2495 /* Address of a static or external variable or
2496 file-scope function counts as a constant. */
2498 && ! (TREE_CODE (arg) == FUNCTION_DECL
2499 && !DECL_FILE_SCOPE_P (arg)))
2500 TREE_CONSTANT (addr) = 1;
2509 argtype = TREE_TYPE (arg);
2510 return fold (build1 (code, argtype, arg));
2513 /* Return nonzero if REF is an lvalue valid for this language.
2514 Lvalues can be assigned, unless their type has TYPE_READONLY.
2515 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2520 enum tree_code code = TREE_CODE (ref);
2527 return lvalue_p (TREE_OPERAND (ref, 0));
2529 case COMPOUND_LITERAL_EXPR:
2539 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2540 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2544 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2551 /* Return nonzero if REF is an lvalue valid for this language;
2552 otherwise, print an error message and return zero. */
2555 lvalue_or_else (tree ref, const char *msgid)
2557 int win = lvalue_p (ref);
2560 error ("%s", msgid);
2566 /* Warn about storing in something that is `const'. */
2569 readonly_error (tree arg, const char *msgid)
2571 if (TREE_CODE (arg) == COMPONENT_REF)
2573 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2574 readonly_error (TREE_OPERAND (arg, 0), msgid);
2576 error ("%s of read-only member `%s'", _(msgid),
2577 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2579 else if (TREE_CODE (arg) == VAR_DECL)
2580 error ("%s of read-only variable `%s'", _(msgid),
2581 IDENTIFIER_POINTER (DECL_NAME (arg)));
2583 error ("%s of read-only location", _(msgid));
2586 /* Mark EXP saying that we need to be able to take the
2587 address of it; it should not be allocated in a register.
2588 Returns true if successful. */
2591 c_mark_addressable (tree exp)
2596 switch (TREE_CODE (x))
2599 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2601 error ("cannot take address of bit-field `%s'",
2602 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2606 /* ... fall through ... */
2612 x = TREE_OPERAND (x, 0);
2615 case COMPOUND_LITERAL_EXPR:
2617 TREE_ADDRESSABLE (x) = 1;
2624 if (C_DECL_REGISTER (x)
2625 && DECL_NONLOCAL (x))
2627 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2629 error ("global register variable `%s' used in nested function",
2630 IDENTIFIER_POINTER (DECL_NAME (x)));
2633 pedwarn ("register variable `%s' used in nested function",
2634 IDENTIFIER_POINTER (DECL_NAME (x)));
2636 else if (C_DECL_REGISTER (x))
2638 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2640 error ("address of global register variable `%s' requested",
2641 IDENTIFIER_POINTER (DECL_NAME (x)));
2645 pedwarn ("address of register variable `%s' requested",
2646 IDENTIFIER_POINTER (DECL_NAME (x)));
2648 put_var_into_stack (x, /*rescan=*/true);
2652 TREE_ADDRESSABLE (x) = 1;
2659 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2662 build_conditional_expr (tree ifexp, tree op1, tree op2)
2666 enum tree_code code1;
2667 enum tree_code code2;
2668 tree result_type = NULL;
2669 tree orig_op1 = op1, orig_op2 = op2;
2671 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2673 /* Promote both alternatives. */
2675 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2676 op1 = default_conversion (op1);
2677 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2678 op2 = default_conversion (op2);
2680 if (TREE_CODE (ifexp) == ERROR_MARK
2681 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2682 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2683 return error_mark_node;
2685 type1 = TREE_TYPE (op1);
2686 code1 = TREE_CODE (type1);
2687 type2 = TREE_TYPE (op2);
2688 code2 = TREE_CODE (type2);
2690 /* C90 does not permit non-lvalue arrays in conditional expressions.
2691 In C99 they will be pointers by now. */
2692 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2694 error ("non-lvalue array in conditional expression");
2695 return error_mark_node;
2698 /* Quickly detect the usual case where op1 and op2 have the same type
2700 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2703 result_type = type1;
2705 result_type = TYPE_MAIN_VARIANT (type1);
2707 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2708 || code1 == COMPLEX_TYPE)
2709 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2710 || code2 == COMPLEX_TYPE))
2712 result_type = common_type (type1, type2);
2714 /* If -Wsign-compare, warn here if type1 and type2 have
2715 different signedness. We'll promote the signed to unsigned
2716 and later code won't know it used to be different.
2717 Do this check on the original types, so that explicit casts
2718 will be considered, but default promotions won't. */
2719 if (warn_sign_compare && !skip_evaluation)
2721 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2722 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2724 if (unsigned_op1 ^ unsigned_op2)
2726 /* Do not warn if the result type is signed, since the
2727 signed type will only be chosen if it can represent
2728 all the values of the unsigned type. */
2729 if (! TYPE_UNSIGNED (result_type))
2731 /* Do not warn if the signed quantity is an unsuffixed
2732 integer literal (or some static constant expression
2733 involving such literals) and it is non-negative. */
2734 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2735 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2738 warning ("signed and unsigned type in conditional expression");
2742 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2744 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2745 pedwarn ("ISO C forbids conditional expr with only one void side");
2746 result_type = void_type_node;
2748 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2750 if (comp_target_types (type1, type2, 1))
2751 result_type = common_type (type1, type2);
2752 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2753 && TREE_CODE (orig_op1) != NOP_EXPR)
2754 result_type = qualify_type (type2, type1);
2755 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2756 && TREE_CODE (orig_op2) != NOP_EXPR)
2757 result_type = qualify_type (type1, type2);
2758 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2760 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2761 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2762 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2763 TREE_TYPE (type2)));
2765 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2767 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2768 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2769 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2770 TREE_TYPE (type1)));
2774 pedwarn ("pointer type mismatch in conditional expression");
2775 result_type = build_pointer_type (void_type_node);
2778 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2780 if (! integer_zerop (op2))
2781 pedwarn ("pointer/integer type mismatch in conditional expression");
2784 op2 = null_pointer_node;
2786 result_type = type1;
2788 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2790 if (!integer_zerop (op1))
2791 pedwarn ("pointer/integer type mismatch in conditional expression");
2794 op1 = null_pointer_node;
2796 result_type = type2;
2801 if (flag_cond_mismatch)
2802 result_type = void_type_node;
2805 error ("type mismatch in conditional expression");
2806 return error_mark_node;
2810 /* Merge const and volatile flags of the incoming types. */
2812 = build_type_variant (result_type,
2813 TREE_READONLY (op1) || TREE_READONLY (op2),
2814 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2816 if (result_type != TREE_TYPE (op1))
2817 op1 = convert_and_check (result_type, op1);
2818 if (result_type != TREE_TYPE (op2))
2819 op2 = convert_and_check (result_type, op2);
2821 if (TREE_CODE (ifexp) == INTEGER_CST)
2822 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2824 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2827 /* Given a list of expressions, return a compound expression
2828 that performs them all and returns the value of the last of them. */
2831 build_compound_expr (tree list)
2833 return internal_build_compound_expr (list, TRUE);
2837 internal_build_compound_expr (tree list, int first_p)
2841 if (TREE_CHAIN (list) == 0)
2843 /* Convert arrays and functions to pointers when there
2844 really is a comma operator. */
2847 = default_function_array_conversion (TREE_VALUE (list));
2849 /* Don't let (0, 0) be null pointer constant. */
2850 if (!first_p && integer_zerop (TREE_VALUE (list)))
2851 return non_lvalue (TREE_VALUE (list));
2852 return TREE_VALUE (list);
2855 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2857 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2859 /* The left-hand operand of a comma expression is like an expression
2860 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2861 any side-effects, unless it was explicitly cast to (void). */
2862 if (warn_unused_value
2863 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2864 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2865 warning ("left-hand operand of comma expression has no effect");
2868 /* With -Wunused, we should also warn if the left-hand operand does have
2869 side-effects, but computes a value which is not used. For example, in
2870 `foo() + bar(), baz()' the result of the `+' operator is not used,
2871 so we should issue a warning. */
2872 else if (warn_unused_value)
2873 warn_if_unused_value (TREE_VALUE (list));
2875 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2878 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2881 build_c_cast (tree type, tree expr)
2885 if (type == error_mark_node || expr == error_mark_node)
2886 return error_mark_node;
2888 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2889 only in <protocol> qualifications. But when constructing cast expressions,
2890 the protocols do matter and must be kept around. */
2891 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2892 type = TYPE_MAIN_VARIANT (type);
2894 if (TREE_CODE (type) == ARRAY_TYPE)
2896 error ("cast specifies array type");
2897 return error_mark_node;
2900 if (TREE_CODE (type) == FUNCTION_TYPE)
2902 error ("cast specifies function type");
2903 return error_mark_node;
2906 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2910 if (TREE_CODE (type) == RECORD_TYPE
2911 || TREE_CODE (type) == UNION_TYPE)
2912 pedwarn ("ISO C forbids casting nonscalar to the same type");
2915 else if (TREE_CODE (type) == UNION_TYPE)
2918 value = default_function_array_conversion (value);
2920 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2921 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2922 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2930 pedwarn ("ISO C forbids casts to union type");
2931 t = digest_init (type,
2932 build_constructor (type,
2933 build_tree_list (field, value)),
2935 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2938 error ("cast to union type from type not present in union");
2939 return error_mark_node;
2945 /* If casting to void, avoid the error that would come
2946 from default_conversion in the case of a non-lvalue array. */
2947 if (type == void_type_node)
2948 return build1 (CONVERT_EXPR, type, value);
2950 /* Convert functions and arrays to pointers,
2951 but don't convert any other types. */
2952 value = default_function_array_conversion (value);
2953 otype = TREE_TYPE (value);
2955 /* Optionally warn about potentially worrisome casts. */
2958 && TREE_CODE (type) == POINTER_TYPE
2959 && TREE_CODE (otype) == POINTER_TYPE)
2961 tree in_type = type;
2962 tree in_otype = otype;
2966 /* Check that the qualifiers on IN_TYPE are a superset of
2967 the qualifiers of IN_OTYPE. The outermost level of
2968 POINTER_TYPE nodes is uninteresting and we stop as soon
2969 as we hit a non-POINTER_TYPE node on either type. */
2972 in_otype = TREE_TYPE (in_otype);
2973 in_type = TREE_TYPE (in_type);
2975 /* GNU C allows cv-qualified function types. 'const'
2976 means the function is very pure, 'volatile' means it
2977 can't return. We need to warn when such qualifiers
2978 are added, not when they're taken away. */
2979 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2980 && TREE_CODE (in_type) == FUNCTION_TYPE)
2981 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2983 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2985 while (TREE_CODE (in_type) == POINTER_TYPE
2986 && TREE_CODE (in_otype) == POINTER_TYPE);
2989 warning ("cast adds new qualifiers to function type");
2992 /* There are qualifiers present in IN_OTYPE that are not
2993 present in IN_TYPE. */
2994 warning ("cast discards qualifiers from pointer target type");
2997 /* Warn about possible alignment problems. */
2998 if (STRICT_ALIGNMENT && warn_cast_align
2999 && TREE_CODE (type) == POINTER_TYPE
3000 && TREE_CODE (otype) == POINTER_TYPE
3001 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3002 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3003 /* Don't warn about opaque types, where the actual alignment
3004 restriction is unknown. */
3005 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3006 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3007 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3008 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3009 warning ("cast increases required alignment of target type");
3011 if (TREE_CODE (type) == INTEGER_TYPE
3012 && TREE_CODE (otype) == POINTER_TYPE
3013 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3014 && !TREE_CONSTANT (value))
3015 warning ("cast from pointer to integer of different size");
3017 if (warn_bad_function_cast
3018 && TREE_CODE (value) == CALL_EXPR
3019 && TREE_CODE (type) != TREE_CODE (otype))
3020 warning ("cast does not match function type");
3022 if (TREE_CODE (type) == POINTER_TYPE
3023 && TREE_CODE (otype) == INTEGER_TYPE
3024 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3025 /* Don't warn about converting any constant. */
3026 && !TREE_CONSTANT (value))
3027 warning ("cast to pointer from integer of different size");
3029 if (TREE_CODE (type) == POINTER_TYPE
3030 && TREE_CODE (otype) == POINTER_TYPE
3031 && TREE_CODE (expr) == ADDR_EXPR
3032 && DECL_P (TREE_OPERAND (expr, 0))
3033 && flag_strict_aliasing && warn_strict_aliasing
3034 && !VOID_TYPE_P (TREE_TYPE (type)))
3036 /* Casting the address of a decl to non void pointer. Warn
3037 if the cast breaks type based aliasing. */
3038 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3039 warning ("type-punning to incomplete type might break strict-aliasing rules");
3042 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3043 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3045 if (!alias_sets_conflict_p (set1, set2))
3046 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3047 else if (warn_strict_aliasing > 1
3048 && !alias_sets_might_conflict_p (set1, set2))
3049 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3053 /* If pedantic, warn for conversions between function and object
3054 pointer types, except for converting a null pointer constant
3055 to function pointer type. */
3057 && TREE_CODE (type) == POINTER_TYPE
3058 && TREE_CODE (otype) == POINTER_TYPE
3059 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3060 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3061 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3064 && TREE_CODE (type) == POINTER_TYPE
3065 && TREE_CODE (otype) == POINTER_TYPE
3066 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3067 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3068 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3069 && TREE_CODE (expr) != NOP_EXPR))
3070 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3073 /* Replace a nonvolatile const static variable with its value. */
3074 if (optimize && TREE_CODE (value) == VAR_DECL)
3075 value = decl_constant_value (value);
3076 value = convert (type, value);
3078 /* Ignore any integer overflow caused by the cast. */
3079 if (TREE_CODE (value) == INTEGER_CST)
3081 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3083 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3084 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3088 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3089 if (TREE_CODE (value) == INTEGER_CST
3090 && TREE_CODE (expr) == INTEGER_CST
3091 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3092 value = non_lvalue (value);
3094 /* Don't let a cast be an lvalue. */
3096 value = non_lvalue (value);
3101 /* Interpret a cast of expression EXPR to type TYPE. */
3103 c_cast_expr (tree type, tree expr)
3105 int saved_wsp = warn_strict_prototypes;
3107 /* This avoids warnings about unprototyped casts on
3108 integers. E.g. "#define SIG_DFL (void(*)())0". */
3109 if (TREE_CODE (expr) == INTEGER_CST)
3110 warn_strict_prototypes = 0;
3111 type = groktypename (type);
3112 warn_strict_prototypes = saved_wsp;
3114 return build_c_cast (type, expr);
3118 /* Build an assignment expression of lvalue LHS from value RHS.
3119 MODIFYCODE is the code for a binary operator that we use
3120 to combine the old value of LHS with RHS to get the new value.
3121 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3124 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3128 tree lhstype = TREE_TYPE (lhs);
3129 tree olhstype = lhstype;
3131 /* Types that aren't fully specified cannot be used in assignments. */
3132 lhs = require_complete_type (lhs);
3134 /* Avoid duplicate error messages from operands that had errors. */
3135 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3136 return error_mark_node;
3138 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3139 /* Do not use STRIP_NOPS here. We do not want an enumerator
3140 whose value is 0 to count as a null pointer constant. */
3141 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3142 rhs = TREE_OPERAND (rhs, 0);
3146 /* If a binary op has been requested, combine the old LHS value with the RHS
3147 producing the value we should actually store into the LHS. */
3149 if (modifycode != NOP_EXPR)
3151 lhs = stabilize_reference (lhs);
3152 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3155 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3156 return error_mark_node;
3158 /* Warn about storing in something that is `const'. */
3160 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3161 || ((TREE_CODE (lhstype) == RECORD_TYPE
3162 || TREE_CODE (lhstype) == UNION_TYPE)
3163 && C_TYPE_FIELDS_READONLY (lhstype)))
3164 readonly_error (lhs, "assignment");
3166 /* If storing into a structure or union member,
3167 it has probably been given type `int'.
3168 Compute the type that would go with
3169 the actual amount of storage the member occupies. */
3171 if (TREE_CODE (lhs) == COMPONENT_REF
3172 && (TREE_CODE (lhstype) == INTEGER_TYPE
3173 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3174 || TREE_CODE (lhstype) == REAL_TYPE
3175 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3176 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3178 /* If storing in a field that is in actuality a short or narrower than one,
3179 we must store in the field in its actual type. */
3181 if (lhstype != TREE_TYPE (lhs))
3183 lhs = copy_node (lhs);
3184 TREE_TYPE (lhs) = lhstype;
3187 /* Convert new value to destination type. */
3189 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3190 NULL_TREE, NULL_TREE, 0);
3191 if (TREE_CODE (newrhs) == ERROR_MARK)
3192 return error_mark_node;
3196 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3197 TREE_SIDE_EFFECTS (result) = 1;
3199 /* If we got the LHS in a different type for storing in,
3200 convert the result back to the nominal type of LHS
3201 so that the value we return always has the same type
3202 as the LHS argument. */
3204 if (olhstype == TREE_TYPE (result))
3206 return convert_for_assignment (olhstype, result, _("assignment"),
3207 NULL_TREE, NULL_TREE, 0);
3210 /* Convert value RHS to type TYPE as preparation for an assignment
3211 to an lvalue of type TYPE.
3212 The real work of conversion is done by `convert'.
3213 The purpose of this function is to generate error messages
3214 for assignments that are not allowed in C.
3215 ERRTYPE is a string to use in error messages:
3216 "assignment", "return", etc. If it is null, this is parameter passing
3217 for a function call (and different error messages are output).
3219 FUNNAME is the name of the function being called,
3220 as an IDENTIFIER_NODE, or null.
3221 PARMNUM is the number of the argument, for printing in error messages. */
3224 convert_for_assignment (tree type, tree rhs, const char *errtype,
3225 tree fundecl, tree funname, int parmnum)
3227 enum tree_code codel = TREE_CODE (type);
3229 enum tree_code coder;
3231 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3232 /* Do not use STRIP_NOPS here. We do not want an enumerator
3233 whose value is 0 to count as a null pointer constant. */
3234 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3235 rhs = TREE_OPERAND (rhs, 0);
3237 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3238 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3239 rhs = default_conversion (rhs);
3240 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3241 rhs = decl_constant_value_for_broken_optimization (rhs);
3243 rhstype = TREE_TYPE (rhs);
3244 coder = TREE_CODE (rhstype);
3246 if (coder == ERROR_MARK)
3247 return error_mark_node;
3249 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3251 overflow_warning (rhs);
3252 /* Check for Objective-C protocols. This will automatically
3253 issue a warning if there are protocol violations. No need to
3254 use the return value. */
3255 if (c_dialect_objc ())
3256 objc_comptypes (type, rhstype, 0);
3260 if (coder == VOID_TYPE)
3262 error ("void value not ignored as it ought to be");
3263 return error_mark_node;
3265 /* A type converts to a reference to it.
3266 This code doesn't fully support references, it's just for the
3267 special case of va_start and va_copy. */
3268 if (codel == REFERENCE_TYPE
3269 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3271 if (!lvalue_p (rhs))
3273 error ("cannot pass rvalue to reference parameter");
3274 return error_mark_node;
3276 if (!c_mark_addressable (rhs))
3277 return error_mark_node;
3278 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3280 /* We already know that these two types are compatible, but they
3281 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3282 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3283 likely to be va_list, a typedef to __builtin_va_list, which
3284 is different enough that it will cause problems later. */
3285 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3286 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3288 rhs = build1 (NOP_EXPR, type, rhs);
3291 /* Some types can interconvert without explicit casts. */
3292 else if (codel == VECTOR_TYPE
3293 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3294 return convert (type, rhs);
3295 /* Arithmetic types all interconvert, and enum is treated like int. */
3296 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3297 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3298 || codel == BOOLEAN_TYPE)
3299 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3300 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3301 || coder == BOOLEAN_TYPE))
3302 return convert_and_check (type, rhs);
3304 /* Conversion to a transparent union from its member types.
3305 This applies only to function arguments. */
3306 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3309 tree marginal_memb_type = 0;
3311 for (memb_types = TYPE_FIELDS (type); memb_types;
3312 memb_types = TREE_CHAIN (memb_types))
3314 tree memb_type = TREE_TYPE (memb_types);
3316 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3317 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3320 if (TREE_CODE (memb_type) != POINTER_TYPE)
3323 if (coder == POINTER_TYPE)
3325 tree ttl = TREE_TYPE (memb_type);
3326 tree ttr = TREE_TYPE (rhstype);
3328 /* Any non-function converts to a [const][volatile] void *
3329 and vice versa; otherwise, targets must be the same.
3330 Meanwhile, the lhs target must have all the qualifiers of
3332 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3333 || comp_target_types (memb_type, rhstype, 0))
3335 /* If this type won't generate any warnings, use it. */
3336 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3337 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3338 && TREE_CODE (ttl) == FUNCTION_TYPE)
3339 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3340 == TYPE_QUALS (ttr))
3341 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3342 == TYPE_QUALS (ttl))))
3345 /* Keep looking for a better type, but remember this one. */
3346 if (! marginal_memb_type)
3347 marginal_memb_type = memb_type;
3351 /* Can convert integer zero to any pointer type. */
3352 if (integer_zerop (rhs)
3353 || (TREE_CODE (rhs) == NOP_EXPR
3354 && integer_zerop (TREE_OPERAND (rhs, 0))))
3356 rhs = null_pointer_node;
3361 if (memb_types || marginal_memb_type)
3365 /* We have only a marginally acceptable member type;
3366 it needs a warning. */
3367 tree ttl = TREE_TYPE (marginal_memb_type);
3368 tree ttr = TREE_TYPE (rhstype);
3370 /* Const and volatile mean something different for function
3371 types, so the usual warnings are not appropriate. */
3372 if (TREE_CODE (ttr) == FUNCTION_TYPE
3373 && TREE_CODE (ttl) == FUNCTION_TYPE)
3375 /* Because const and volatile on functions are
3376 restrictions that say the function will not do
3377 certain things, it is okay to use a const or volatile
3378 function where an ordinary one is wanted, but not
3380 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3381 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3382 errtype, funname, parmnum);
3384 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3385 warn_for_assignment ("%s discards qualifiers from pointer target type",
3390 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3391 pedwarn ("ISO C prohibits argument conversion to union type");
3393 return build1 (NOP_EXPR, type, rhs);
3397 /* Conversions among pointers */
3398 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3399 && (coder == codel))
3401 tree ttl = TREE_TYPE (type);
3402 tree ttr = TREE_TYPE (rhstype);
3403 bool is_opaque_pointer;
3404 int target_cmp = 0; /* Cache comp_target_types () result. */
3406 /* Opaque pointers are treated like void pointers. */
3407 is_opaque_pointer = (targetm.vector_opaque_p (type)
3408 || targetm.vector_opaque_p (rhstype))
3409 && TREE_CODE (ttl) == VECTOR_TYPE
3410 && TREE_CODE (ttr) == VECTOR_TYPE;
3412 /* Any non-function converts to a [const][volatile] void *
3413 and vice versa; otherwise, targets must be the same.
3414 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3415 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3416 || (target_cmp = comp_target_types (type, rhstype, 0))
3417 || is_opaque_pointer
3418 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3419 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3422 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3425 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3426 which are not ANSI null ptr constants. */
3427 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3428 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3429 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3430 errtype, funname, parmnum);
3431 /* Const and volatile mean something different for function types,
3432 so the usual warnings are not appropriate. */
3433 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3434 && TREE_CODE (ttl) != FUNCTION_TYPE)
3436 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3437 warn_for_assignment ("%s discards qualifiers from pointer target type",
3438 errtype, funname, parmnum);
3439 /* If this is not a case of ignoring a mismatch in signedness,
3441 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3444 /* If there is a mismatch, do warn. */
3446 warn_for_assignment ("pointer targets in %s differ in signedness",
3447 errtype, funname, parmnum);
3449 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3450 && TREE_CODE (ttr) == FUNCTION_TYPE)
3452 /* Because const and volatile on functions are restrictions
3453 that say the function will not do certain things,
3454 it is okay to use a const or volatile function
3455 where an ordinary one is wanted, but not vice-versa. */
3456 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3457 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3458 errtype, funname, parmnum);
3462 warn_for_assignment ("%s from incompatible pointer type",
3463 errtype, funname, parmnum);
3464 return convert (type, rhs);
3466 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3468 error ("invalid use of non-lvalue array");
3469 return error_mark_node;
3471 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3473 /* An explicit constant 0 can convert to a pointer,
3474 or one that results from arithmetic, even including
3475 a cast to integer type. */
3476 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3478 ! (TREE_CODE (rhs) == NOP_EXPR
3479 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3480 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3481 && integer_zerop (TREE_OPERAND (rhs, 0))))
3482 warn_for_assignment ("%s makes pointer from integer without a cast",
3483 errtype, funname, parmnum);
3485 return convert (type, rhs);
3487 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3489 warn_for_assignment ("%s makes integer from pointer without a cast",
3490 errtype, funname, parmnum);
3491 return convert (type, rhs);
3493 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3494 return convert (type, rhs);
3500 tree selector = objc_message_selector ();
3502 if (selector && parmnum > 2)
3503 error ("incompatible type for argument %d of `%s'",
3504 parmnum - 2, IDENTIFIER_POINTER (selector));
3506 error ("incompatible type for argument %d of `%s'",
3507 parmnum, IDENTIFIER_POINTER (funname));
3510 error ("incompatible type for argument %d of indirect function call",
3514 error ("incompatible types in %s", errtype);
3516 return error_mark_node;
3519 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3520 is used for error and waring reporting and indicates which argument
3521 is being processed. */
3524 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3528 /* If FN was prototyped, the value has been converted already
3529 in convert_arguments. */
3530 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3533 type = TREE_TYPE (parm);
3534 ret = convert_for_assignment (type, value,
3535 (char *) 0 /* arg passing */, fn,
3536 DECL_NAME (fn), argnum);
3537 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3538 && INTEGRAL_TYPE_P (type)
3539 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3540 ret = default_conversion (ret);
3544 /* Print a warning using MSGID.
3545 It gets OPNAME as its one parameter.
3546 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3547 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3548 FUNCTION and ARGNUM are handled specially if we are building an
3549 Objective-C selector. */
3552 warn_for_assignment (const char *msgid, const char *opname, tree function,
3557 tree selector = objc_message_selector ();
3560 if (selector && argnum > 2)
3562 function = selector;
3569 /* Function name is known; supply it. */
3570 const char *const argstring = _("passing arg of `%s'");
3571 new_opname = alloca (IDENTIFIER_LENGTH (function)
3572 + strlen (argstring) + 1 + 1);
3573 sprintf (new_opname, argstring,
3574 IDENTIFIER_POINTER (function));
3578 /* Function name unknown (call through ptr). */
3579 const char *const argnofun = _("passing arg of pointer to function");
3580 new_opname = alloca (strlen (argnofun) + 1 + 1);
3581 sprintf (new_opname, argnofun);
3586 /* Function name is known; supply it. */
3587 const char *const argstring = _("passing arg %d of `%s'");
3588 new_opname = alloca (IDENTIFIER_LENGTH (function)
3589 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3590 sprintf (new_opname, argstring, argnum,
3591 IDENTIFIER_POINTER (function));
3595 /* Function name unknown (call through ptr); just give arg number. */
3596 const char *const argnofun = _("passing arg %d of pointer to function");
3597 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3598 sprintf (new_opname, argnofun, argnum);
3600 opname = new_opname;
3602 pedwarn (msgid, opname);
3605 /* If VALUE is a compound expr all of whose expressions are constant, then
3606 return its value. Otherwise, return error_mark_node.
3608 This is for handling COMPOUND_EXPRs as initializer elements
3609 which is allowed with a warning when -pedantic is specified. */
3612 valid_compound_expr_initializer (tree value, tree endtype)
3614 if (TREE_CODE (value) == COMPOUND_EXPR)
3616 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3618 return error_mark_node;
3619 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3622 else if (! TREE_CONSTANT (value)
3623 && ! initializer_constant_valid_p (value, endtype))
3624 return error_mark_node;
3629 /* Perform appropriate conversions on the initial value of a variable,
3630 store it in the declaration DECL,
3631 and print any error messages that are appropriate.
3632 If the init is invalid, store an ERROR_MARK. */
3635 store_init_value (tree decl, tree init)
3639 /* If variable's type was invalidly declared, just ignore it. */
3641 type = TREE_TYPE (decl);
3642 if (TREE_CODE (type) == ERROR_MARK)
3645 /* Digest the specified initializer into an expression. */
3647 value = digest_init (type, init, TREE_STATIC (decl));
3649 /* Store the expression if valid; else report error. */
3651 if (warn_traditional && !in_system_header
3652 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3653 warning ("traditional C rejects automatic aggregate initialization");
3655 DECL_INITIAL (decl) = value;
3657 /* ANSI wants warnings about out-of-range constant initializers. */
3658 STRIP_TYPE_NOPS (value);
3659 constant_expression_warning (value);
3661 /* Check if we need to set array size from compound literal size. */
3662 if (TREE_CODE (type) == ARRAY_TYPE
3663 && TYPE_DOMAIN (type) == 0
3664 && value != error_mark_node)
3666 tree inside_init = init;
3668 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3669 inside_init = TREE_OPERAND (init, 0);
3670 inside_init = fold (inside_init);
3672 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3674 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3676 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3678 /* For int foo[] = (int [3]){1}; we need to set array size
3679 now since later on array initializer will be just the
3680 brace enclosed list of the compound literal. */
3681 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3683 layout_decl (decl, 0);
3689 /* Methods for storing and printing names for error messages. */
3691 /* Implement a spelling stack that allows components of a name to be pushed
3692 and popped. Each element on the stack is this structure. */
3704 #define SPELLING_STRING 1
3705 #define SPELLING_MEMBER 2
3706 #define SPELLING_BOUNDS 3
3708 static struct spelling *spelling; /* Next stack element (unused). */
3709 static struct spelling *spelling_base; /* Spelling stack base. */
3710 static int spelling_size; /* Size of the spelling stack. */
3712 /* Macros to save and restore the spelling stack around push_... functions.
3713 Alternative to SAVE_SPELLING_STACK. */
3715 #define SPELLING_DEPTH() (spelling - spelling_base)
3716 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3718 /* Push an element on the spelling stack with type KIND and assign VALUE
3721 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3723 int depth = SPELLING_DEPTH (); \
3725 if (depth >= spelling_size) \
3727 spelling_size += 10; \
3728 if (spelling_base == 0) \
3729 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3731 spelling_base = xrealloc (spelling_base, \
3732 spelling_size * sizeof (struct spelling)); \
3733 RESTORE_SPELLING_DEPTH (depth); \
3736 spelling->kind = (KIND); \
3737 spelling->MEMBER = (VALUE); \
3741 /* Push STRING on the stack. Printed literally. */
3744 push_string (const char *string)
3746 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3749 /* Push a member name on the stack. Printed as '.' STRING. */
3752 push_member_name (tree decl)
3754 const char *const string
3755 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3756 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3759 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3762 push_array_bounds (int bounds)
3764 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3767 /* Compute the maximum size in bytes of the printed spelling. */
3770 spelling_length (void)
3775 for (p = spelling_base; p < spelling; p++)
3777 if (p->kind == SPELLING_BOUNDS)
3780 size += strlen (p->u.s) + 1;
3786 /* Print the spelling to BUFFER and return it. */
3789 print_spelling (char *buffer)
3794 for (p = spelling_base; p < spelling; p++)
3795 if (p->kind == SPELLING_BOUNDS)
3797 sprintf (d, "[%d]", p->u.i);
3803 if (p->kind == SPELLING_MEMBER)
3805 for (s = p->u.s; (*d = *s++); d++)
3812 /* Issue an error message for a bad initializer component.
3813 MSGID identifies the message.
3814 The component name is taken from the spelling stack. */
3817 error_init (const char *msgid)
3821 error ("%s", _(msgid));
3822 ofwhat = print_spelling (alloca (spelling_length () + 1));
3824 error ("(near initialization for `%s')", ofwhat);
3827 /* Issue a pedantic warning for a bad initializer component.
3828 MSGID identifies the message.
3829 The component name is taken from the spelling stack. */
3832 pedwarn_init (const char *msgid)
3836 pedwarn ("%s", _(msgid));
3837 ofwhat = print_spelling (alloca (spelling_length () + 1));
3839 pedwarn ("(near initialization for `%s')", ofwhat);
3842 /* Issue a warning for a bad initializer component.
3843 MSGID identifies the message.
3844 The component name is taken from the spelling stack. */
3847 warning_init (const char *msgid)
3851 warning ("%s", _(msgid));
3852 ofwhat = print_spelling (alloca (spelling_length () + 1));
3854 warning ("(near initialization for `%s')", ofwhat);
3857 /* Digest the parser output INIT as an initializer for type TYPE.
3858 Return a C expression of type TYPE to represent the initial value.
3860 REQUIRE_CONSTANT requests an error if non-constant initializers or
3861 elements are seen. */
3864 digest_init (tree type, tree init, int require_constant)
3866 enum tree_code code = TREE_CODE (type);
3867 tree inside_init = init;
3869 if (type == error_mark_node
3870 || init == error_mark_node
3871 || TREE_TYPE (init) == error_mark_node)
3872 return error_mark_node;
3874 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3875 /* Do not use STRIP_NOPS here. We do not want an enumerator
3876 whose value is 0 to count as a null pointer constant. */
3877 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3878 inside_init = TREE_OPERAND (init, 0);
3880 inside_init = fold (inside_init);
3882 /* Initialization of an array of chars from a string constant
3883 optionally enclosed in braces. */
3885 if (code == ARRAY_TYPE)
3887 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3888 if ((typ1 == char_type_node
3889 || typ1 == signed_char_type_node
3890 || typ1 == unsigned_char_type_node
3891 || typ1 == unsigned_wchar_type_node
3892 || typ1 == signed_wchar_type_node)
3893 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3895 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3896 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3899 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3901 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3903 error_init ("char-array initialized from wide string");
3904 return error_mark_node;
3906 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3908 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3910 error_init ("int-array initialized from non-wide string");
3911 return error_mark_node;
3914 TREE_TYPE (inside_init) = type;
3915 if (TYPE_DOMAIN (type) != 0
3916 && TYPE_SIZE (type) != 0
3917 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3918 /* Subtract 1 (or sizeof (wchar_t))
3919 because it's ok to ignore the terminating null char
3920 that is counted in the length of the constant. */
3921 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3922 TREE_STRING_LENGTH (inside_init)
3923 - ((TYPE_PRECISION (typ1)
3924 != TYPE_PRECISION (char_type_node))
3925 ? (TYPE_PRECISION (wchar_type_node)
3928 pedwarn_init ("initializer-string for array of chars is too long");
3934 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3935 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3936 below and handle as a constructor. */
3937 if (code == VECTOR_TYPE
3938 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
3939 && TREE_CONSTANT (inside_init))
3941 if (TREE_CODE (inside_init) == VECTOR_CST
3942 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3943 TYPE_MAIN_VARIANT (type),
3947 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
3950 /* Any type can be initialized
3951 from an expression of the same type, optionally with braces. */
3953 if (inside_init && TREE_TYPE (inside_init) != 0
3954 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3955 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
3956 || (code == ARRAY_TYPE
3957 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3958 || (code == VECTOR_TYPE
3959 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3960 || (code == POINTER_TYPE
3961 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3962 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3963 TREE_TYPE (type), COMPARE_STRICT))
3964 || (code == POINTER_TYPE
3965 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
3966 && comptypes (TREE_TYPE (inside_init),
3967 TREE_TYPE (type), COMPARE_STRICT))))
3969 if (code == POINTER_TYPE)
3971 inside_init = default_function_array_conversion (inside_init);
3973 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
3975 error_init ("invalid use of non-lvalue array");
3976 return error_mark_node;
3980 if (code == VECTOR_TYPE)
3981 /* Although the types are compatible, we may require a
3983 inside_init = convert (type, inside_init);
3985 if (require_constant && !flag_isoc99
3986 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3988 /* As an extension, allow initializing objects with static storage
3989 duration with compound literals (which are then treated just as
3990 the brace enclosed list they contain). */
3991 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3992 inside_init = DECL_INITIAL (decl);
3995 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
3996 && TREE_CODE (inside_init) != CONSTRUCTOR)
3998 error_init ("array initialized from non-constant array expression");