1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
34 #include "coretypes.h"
38 #include "langhooks.h"
49 /* Nonzero if we've already printed a "missing braces around initializer"
50 message within this initializer. */
51 static int missing_braces_mentioned;
53 static tree qualify_type (tree, tree);
54 static int tagged_types_tu_compatible_p (tree, tree, int);
55 static int comp_target_types (tree, tree, int);
56 static int function_types_compatible_p (tree, tree, int);
57 static int type_lists_compatible_p (tree, tree, int);
58 static tree decl_constant_value_for_broken_optimization (tree);
59 static tree default_function_array_conversion (tree);
60 static tree lookup_field (tree, tree);
61 static tree convert_arguments (tree, tree, tree, tree);
62 static tree pointer_diff (tree, tree);
63 static tree internal_build_compound_expr (tree, int);
64 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
66 static void warn_for_assignment (const char *, const char *, tree, int);
67 static tree valid_compound_expr_initializer (tree, tree);
68 static void push_string (const char *);
69 static void push_member_name (tree);
70 static void push_array_bounds (int);
71 static int spelling_length (void);
72 static char *print_spelling (char *);
73 static void warning_init (const char *);
74 static tree digest_init (tree, tree, int);
75 static void output_init_element (tree, tree, tree, int);
76 static void output_pending_init_elements (int);
77 static int set_designator (int);
78 static void push_range_stack (tree);
79 static void add_pending_init (tree, tree);
80 static void set_nonincremental_init (void);
81 static void set_nonincremental_init_from_string (tree);
82 static tree find_init_member (tree);
84 /* Do `exp = require_complete_type (exp);' to make sure exp
85 does not have an incomplete type. (That includes void types.) */
88 require_complete_type (tree value)
90 tree type = TREE_TYPE (value);
92 if (value == error_mark_node || type == error_mark_node)
93 return error_mark_node;
95 /* First, detect a valid value with a complete type. */
96 if (COMPLETE_TYPE_P (type))
99 c_incomplete_type_error (value, type);
100 return error_mark_node;
103 /* Print an error message for invalid use of an incomplete type.
104 VALUE is the expression that was used (or 0 if that isn't known)
105 and TYPE is the type that was invalid. */
108 c_incomplete_type_error (tree value, tree type)
110 const char *type_code_string;
112 /* Avoid duplicate error message. */
113 if (TREE_CODE (type) == ERROR_MARK)
116 if (value != 0 && (TREE_CODE (value) == VAR_DECL
117 || TREE_CODE (value) == PARM_DECL))
118 error ("`%s' has an incomplete type",
119 IDENTIFIER_POINTER (DECL_NAME (value)));
123 /* We must print an error message. Be clever about what it says. */
125 switch (TREE_CODE (type))
128 type_code_string = "struct";
132 type_code_string = "union";
136 type_code_string = "enum";
140 error ("invalid use of void expression");
144 if (TYPE_DOMAIN (type))
146 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
148 error ("invalid use of flexible array member");
151 type = TREE_TYPE (type);
154 error ("invalid use of array with unspecified bounds");
161 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
162 error ("invalid use of undefined type `%s %s'",
163 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
165 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
166 error ("invalid use of incomplete typedef `%s'",
167 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
171 /* Given a type, apply default promotions wrt unnamed function
172 arguments and return the new type. */
175 c_type_promotes_to (tree type)
177 if (TYPE_MAIN_VARIANT (type) == float_type_node)
178 return double_type_node;
180 if (c_promoting_integer_type_p (type))
182 /* Preserve unsignedness if not really getting any wider. */
183 if (TYPE_UNSIGNED (type)
184 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
185 return unsigned_type_node;
186 return integer_type_node;
192 /* Return a variant of TYPE which has all the type qualifiers of LIKE
193 as well as those of TYPE. */
196 qualify_type (tree type, tree like)
198 return c_build_qualified_type (type,
199 TYPE_QUALS (type) | TYPE_QUALS (like));
202 /* Return the composite type of two compatible types, or the common
203 type for two arithmetic types under the usual arithmetic
206 Unless both types are arithmetic types, we assume that comptypes
207 has already been done and returned 1; if that isn't so, this may
208 crash. In particular, we assume that qualifiers match.
210 This is the type for the result of most arithmetic operations
211 if the operands have the given two types. */
214 common_type (tree t1, tree t2)
216 enum tree_code code1;
217 enum tree_code code2;
220 /* Save time if the two types are the same. */
222 if (t1 == t2) return t1;
224 /* If one type is nonsense, use the other. */
225 if (t1 == error_mark_node)
227 if (t2 == error_mark_node)
230 /* Merge the attributes. */
231 attributes = targetm.merge_type_attributes (t1, t2);
233 /* Treat an enum type as the unsigned integer type of the same width. */
235 if (TREE_CODE (t1) == ENUMERAL_TYPE)
236 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
237 if (TREE_CODE (t2) == ENUMERAL_TYPE)
238 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
240 code1 = TREE_CODE (t1);
241 code2 = TREE_CODE (t2);
243 /* If one type is complex, form the common type of the non-complex
244 components, then make that complex. Use T1 or T2 if it is the
246 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
248 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
249 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
250 tree subtype = common_type (subtype1, subtype2);
252 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
253 return build_type_attribute_variant (t1, attributes);
254 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
255 return build_type_attribute_variant (t2, attributes);
257 return build_type_attribute_variant (build_complex_type (subtype),
265 /* If only one is real, use it as the result. */
267 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
268 return build_type_attribute_variant (t1, attributes);
270 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
271 return build_type_attribute_variant (t2, attributes);
273 /* Both real or both integers; use the one with greater precision. */
275 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
276 return build_type_attribute_variant (t1, attributes);
277 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
278 return build_type_attribute_variant (t2, attributes);
280 /* Same precision. Prefer long longs to longs to ints when the
281 same precision, following the C99 rules on integer type rank
282 (which are equivalent to the C90 rules for C90 types). */
284 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
285 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
286 return build_type_attribute_variant (long_long_unsigned_type_node,
289 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
290 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
292 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
293 t1 = long_long_unsigned_type_node;
295 t1 = long_long_integer_type_node;
296 return build_type_attribute_variant (t1, attributes);
299 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
300 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
301 return build_type_attribute_variant (long_unsigned_type_node,
304 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
305 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
307 /* But preserve unsignedness from the other type,
308 since long cannot hold all the values of an unsigned int. */
309 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
310 t1 = long_unsigned_type_node;
312 t1 = long_integer_type_node;
313 return build_type_attribute_variant (t1, attributes);
316 /* Likewise, prefer long double to double even if same size. */
317 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
318 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
319 return build_type_attribute_variant (long_double_type_node,
322 /* Otherwise prefer the unsigned one. */
324 if (TYPE_UNSIGNED (t1))
325 return build_type_attribute_variant (t1, attributes);
327 return build_type_attribute_variant (t2, attributes);
330 /* For two pointers, do this recursively on the target type,
331 and combine the qualifiers of the two types' targets. */
332 /* This code was turned off; I don't know why.
333 But ANSI C specifies doing this with the qualifiers.
334 So I turned it on again. */
336 tree pointed_to_1 = TREE_TYPE (t1);
337 tree pointed_to_2 = TREE_TYPE (t2);
338 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
339 TYPE_MAIN_VARIANT (pointed_to_2));
340 t1 = build_pointer_type (c_build_qualified_type
342 TYPE_QUALS (pointed_to_1) |
343 TYPE_QUALS (pointed_to_2)));
344 return build_type_attribute_variant (t1, attributes);
349 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
350 /* Save space: see if the result is identical to one of the args. */
351 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
352 return build_type_attribute_variant (t1, attributes);
353 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
354 return build_type_attribute_variant (t2, attributes);
355 /* Merge the element types, and have a size if either arg has one. */
356 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
357 return build_type_attribute_variant (t1, attributes);
361 /* Function types: prefer the one that specified arg types.
362 If both do, merge the arg types. Also merge the return types. */
364 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
365 tree p1 = TYPE_ARG_TYPES (t1);
366 tree p2 = TYPE_ARG_TYPES (t2);
371 /* Save space: see if the result is identical to one of the args. */
372 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
373 return build_type_attribute_variant (t1, attributes);
374 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
375 return build_type_attribute_variant (t2, attributes);
377 /* Simple way if one arg fails to specify argument types. */
378 if (TYPE_ARG_TYPES (t1) == 0)
380 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
381 return build_type_attribute_variant (t1, attributes);
383 if (TYPE_ARG_TYPES (t2) == 0)
385 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
386 return build_type_attribute_variant (t1, attributes);
389 /* If both args specify argument types, we must merge the two
390 lists, argument by argument. */
391 /* Tell global_bindings_p to return false so that variable_size
392 doesn't abort on VLAs in parameter types. */
393 c_override_global_bindings_to_false = true;
395 len = list_length (p1);
398 for (i = 0; i < len; i++)
399 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
404 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
406 /* A null type means arg type is not specified.
407 Take whatever the other function type has. */
408 if (TREE_VALUE (p1) == 0)
410 TREE_VALUE (n) = TREE_VALUE (p2);
413 if (TREE_VALUE (p2) == 0)
415 TREE_VALUE (n) = TREE_VALUE (p1);
419 /* Given wait (union {union wait *u; int *i} *)
420 and wait (union wait *),
421 prefer union wait * as type of parm. */
422 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
423 && TREE_VALUE (p1) != TREE_VALUE (p2))
426 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
427 memb; memb = TREE_CHAIN (memb))
428 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
431 TREE_VALUE (n) = TREE_VALUE (p2);
433 pedwarn ("function types not truly compatible in ISO C");
437 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
438 && TREE_VALUE (p2) != TREE_VALUE (p1))
441 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
442 memb; memb = TREE_CHAIN (memb))
443 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
446 TREE_VALUE (n) = TREE_VALUE (p1);
448 pedwarn ("function types not truly compatible in ISO C");
452 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
456 c_override_global_bindings_to_false = false;
457 t1 = build_function_type (valtype, newargs);
458 /* ... falls through ... */
462 return build_type_attribute_variant (t1, attributes);
467 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
468 or various other operations. Return 2 if they are compatible
469 but a warning may be needed if you use them together. */
472 comptypes (tree type1, tree type2, int flags)
478 /* Suppress errors caused by previously reported errors. */
480 if (t1 == t2 || !t1 || !t2
481 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
484 /* If either type is the internal version of sizetype, return the
486 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
487 && TYPE_ORIG_SIZE_TYPE (t1))
488 t1 = TYPE_ORIG_SIZE_TYPE (t1);
490 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
491 && TYPE_ORIG_SIZE_TYPE (t2))
492 t2 = TYPE_ORIG_SIZE_TYPE (t2);
495 /* Enumerated types are compatible with integer types, but this is
496 not transitive: two enumerated types in the same translation unit
497 are compatible with each other only if they are the same type. */
499 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
500 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
501 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
502 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
507 /* Different classes of types can't be compatible. */
509 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
511 /* Qualifiers must match. */
513 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
516 /* Allow for two different type nodes which have essentially the same
517 definition. Note that we already checked for equality of the type
518 qualifiers (just above). */
520 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
523 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
524 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
527 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
530 switch (TREE_CODE (t1))
533 /* We must give ObjC the first crack at comparing pointers, since
534 protocol qualifiers may be involved. */
535 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
537 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
538 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
542 val = function_types_compatible_p (t1, t2, flags);
547 tree d1 = TYPE_DOMAIN (t1);
548 tree d2 = TYPE_DOMAIN (t2);
549 bool d1_variable, d2_variable;
550 bool d1_zero, d2_zero;
553 /* Target types must match incl. qualifiers. */
554 if (TREE_TYPE (t1) != TREE_TYPE (t2)
555 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
559 /* Sizes must match unless one is missing or variable. */
560 if (d1 == 0 || d2 == 0 || d1 == d2)
563 d1_zero = ! TYPE_MAX_VALUE (d1);
564 d2_zero = ! TYPE_MAX_VALUE (d2);
566 d1_variable = (! d1_zero
567 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
568 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
569 d2_variable = (! d2_zero
570 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
571 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
573 if (d1_variable || d2_variable)
575 if (d1_zero && d2_zero)
577 if (d1_zero || d2_zero
578 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
579 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
586 /* We are dealing with two distinct structs. In assorted Objective-C
587 corner cases, however, these can still be deemed equivalent. */
588 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
593 if (val != 1 && !same_translation_unit_p (t1, t2))
594 val = tagged_types_tu_compatible_p (t1, t2, flags);
598 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
599 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), 0);
605 return attrval == 2 && val == 1 ? 2 : val;
608 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
609 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
610 to 1 or 0 depending if the check of the pointer types is meant to
611 be reflexive or not (typically, assignments are not reflexive,
612 while comparisons are reflexive).
616 comp_target_types (tree ttl, tree ttr, int reflexive)
620 /* Give objc_comptypes a crack at letting these types through. */
621 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
624 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
625 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
627 if (val == 2 && pedantic)
628 pedwarn ("types are not quite compatible");
632 /* Subroutines of `comptypes'. */
634 /* Determine whether two trees derive from the same translation unit.
635 If the CONTEXT chain ends in a null, that tree's context is still
636 being parsed, so if two trees have context chains ending in null,
637 they're in the same translation unit. */
639 same_translation_unit_p (tree t1, tree t2)
641 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
642 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
644 case 'd': t1 = DECL_CONTEXT (t1); break;
645 case 't': t1 = TYPE_CONTEXT (t1); break;
646 case 'x': t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
650 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
651 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
653 case 'd': t2 = DECL_CONTEXT (t2); break;
654 case 't': t2 = TYPE_CONTEXT (t2); break;
655 case 'x': t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
662 /* The C standard says that two structures in different translation
663 units are compatible with each other only if the types of their
664 fields are compatible (among other things). So, consider two copies
665 of this structure: */
667 struct tagged_tu_seen {
668 const struct tagged_tu_seen * next;
673 /* Can they be compatible with each other? We choose to break the
674 recursion by allowing those types to be compatible. */
676 static const struct tagged_tu_seen * tagged_tu_seen_base;
678 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
679 compatible. If the two types are not the same (which has been
680 checked earlier), this can only happen when multiple translation
681 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
685 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
688 bool needs_warning = false;
690 /* We have to verify that the tags of the types are the same. This
691 is harder than it looks because this may be a typedef, so we have
692 to go look at the original type. It may even be a typedef of a
694 In the case of compiler-created builtin structs the TYPE_DECL
695 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
696 while (TYPE_NAME (t1)
697 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
698 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
699 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
701 while (TYPE_NAME (t2)
702 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
703 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
704 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
706 /* C90 didn't have the requirement that the two tags be the same. */
707 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
710 /* C90 didn't say what happened if one or both of the types were
711 incomplete; we choose to follow C99 rules here, which is that they
713 if (TYPE_SIZE (t1) == NULL
714 || TYPE_SIZE (t2) == NULL)
718 const struct tagged_tu_seen * tts_i;
719 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
720 if (tts_i->t1 == t1 && tts_i->t2 == t2)
724 switch (TREE_CODE (t1))
729 /* Speed up the case where the type values are in the same order. */
730 tree tv1 = TYPE_VALUES (t1);
731 tree tv2 = TYPE_VALUES (t2);
736 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
738 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
740 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
744 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
746 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
749 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
752 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
754 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
756 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
764 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
767 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
770 struct tagged_tu_seen tts;
772 tts.next = tagged_tu_seen_base;
775 tagged_tu_seen_base = &tts;
777 if (DECL_NAME (s1) != NULL)
778 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
779 if (DECL_NAME (s1) == DECL_NAME (s2))
782 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
786 needs_warning = true;
788 if (TREE_CODE (s1) == FIELD_DECL
789 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
790 DECL_FIELD_BIT_OFFSET (s2)) != 1)
796 tagged_tu_seen_base = tts.next;
800 return needs_warning ? 2 : 1;
805 struct tagged_tu_seen tts;
807 tts.next = tagged_tu_seen_base;
810 tagged_tu_seen_base = &tts;
812 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
814 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
817 if (TREE_CODE (s1) != TREE_CODE (s2)
818 || DECL_NAME (s1) != DECL_NAME (s2))
820 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
824 needs_warning = true;
826 if (TREE_CODE (s1) == FIELD_DECL
827 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
828 DECL_FIELD_BIT_OFFSET (s2)) != 1)
831 tagged_tu_seen_base = tts.next;
834 return needs_warning ? 2 : 1;
842 /* Return 1 if two function types F1 and F2 are compatible.
843 If either type specifies no argument types,
844 the other must specify a fixed number of self-promoting arg types.
845 Otherwise, if one type specifies only the number of arguments,
846 the other must specify that number of self-promoting arg types.
847 Otherwise, the argument types must match. */
850 function_types_compatible_p (tree f1, tree f2, int flags)
853 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
858 ret1 = TREE_TYPE (f1);
859 ret2 = TREE_TYPE (f2);
861 /* 'volatile' qualifiers on a function's return type mean the function
863 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
864 pedwarn ("function return types not compatible due to `volatile'");
865 if (TYPE_VOLATILE (ret1))
866 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
867 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
868 if (TYPE_VOLATILE (ret2))
869 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
870 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
871 val = comptypes (ret1, ret2, flags);
875 args1 = TYPE_ARG_TYPES (f1);
876 args2 = TYPE_ARG_TYPES (f2);
878 /* An unspecified parmlist matches any specified parmlist
879 whose argument types don't need default promotions. */
883 if (!self_promoting_args_p (args2))
885 /* If one of these types comes from a non-prototype fn definition,
886 compare that with the other type's arglist.
887 If they don't match, ask for a warning (but no error). */
888 if (TYPE_ACTUAL_ARG_TYPES (f1)
889 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
896 if (!self_promoting_args_p (args1))
898 if (TYPE_ACTUAL_ARG_TYPES (f2)
899 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
905 /* Both types have argument lists: compare them and propagate results. */
906 val1 = type_lists_compatible_p (args1, args2, flags);
907 return val1 != 1 ? val1 : val;
910 /* Check two lists of types for compatibility,
911 returning 0 for incompatible, 1 for compatible,
912 or 2 for compatible with warning. */
915 type_lists_compatible_p (tree args1, tree args2, int flags)
917 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
923 if (args1 == 0 && args2 == 0)
925 /* If one list is shorter than the other,
926 they fail to match. */
927 if (args1 == 0 || args2 == 0)
929 /* A null pointer instead of a type
930 means there is supposed to be an argument
931 but nothing is specified about what type it has.
932 So match anything that self-promotes. */
933 if (TREE_VALUE (args1) == 0)
935 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
938 else if (TREE_VALUE (args2) == 0)
940 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
943 /* If one of the lists has an error marker, ignore this arg. */
944 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
945 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
947 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
948 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
951 /* Allow wait (union {union wait *u; int *i} *)
952 and wait (union wait *) to be compatible. */
953 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
954 && (TYPE_NAME (TREE_VALUE (args1)) == 0
955 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
956 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
957 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
958 TYPE_SIZE (TREE_VALUE (args2))))
961 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
962 memb; memb = TREE_CHAIN (memb))
963 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
969 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
970 && (TYPE_NAME (TREE_VALUE (args2)) == 0
971 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
972 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
973 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
974 TYPE_SIZE (TREE_VALUE (args1))))
977 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
978 memb; memb = TREE_CHAIN (memb))
979 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
989 /* comptypes said ok, but record if it said to warn. */
993 args1 = TREE_CHAIN (args1);
994 args2 = TREE_CHAIN (args2);
998 /* Compute the size to increment a pointer by. */
1001 c_size_in_bytes (tree type)
1003 enum tree_code code = TREE_CODE (type);
1005 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1006 return size_one_node;
1008 if (!COMPLETE_OR_VOID_TYPE_P (type))
1010 error ("arithmetic on pointer to an incomplete type");
1011 return size_one_node;
1014 /* Convert in case a char is more than one unit. */
1015 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1016 size_int (TYPE_PRECISION (char_type_node)
1020 /* Return either DECL or its known constant value (if it has one). */
1023 decl_constant_value (tree decl)
1025 if (/* Don't change a variable array bound or initial value to a constant
1026 in a place where a variable is invalid. Note that DECL_INITIAL
1027 isn't valid for a PARM_DECL. */
1028 current_function_decl != 0
1029 && TREE_CODE (decl) != PARM_DECL
1030 && ! TREE_THIS_VOLATILE (decl)
1031 && TREE_READONLY (decl)
1032 && DECL_INITIAL (decl) != 0
1033 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1034 /* This is invalid if initial value is not constant.
1035 If it has either a function call, a memory reference,
1036 or a variable, then re-evaluating it could give different results. */
1037 && TREE_CONSTANT (DECL_INITIAL (decl))
1038 /* Check for cases where this is sub-optimal, even though valid. */
1039 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1040 return DECL_INITIAL (decl);
1044 /* Return either DECL or its known constant value (if it has one), but
1045 return DECL if pedantic or DECL has mode BLKmode. This is for
1046 bug-compatibility with the old behavior of decl_constant_value
1047 (before GCC 3.0); every use of this function is a bug and it should
1048 be removed before GCC 3.1. It is not appropriate to use pedantic
1049 in a way that affects optimization, and BLKmode is probably not the
1050 right test for avoiding misoptimizations either. */
1053 decl_constant_value_for_broken_optimization (tree decl)
1055 if (pedantic || DECL_MODE (decl) == BLKmode)
1058 return decl_constant_value (decl);
1062 /* Perform the default conversion of arrays and functions to pointers.
1063 Return the result of converting EXP. For any other expression, just
1067 default_function_array_conversion (tree exp)
1070 tree type = TREE_TYPE (exp);
1071 enum tree_code code = TREE_CODE (type);
1074 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1077 Do not use STRIP_NOPS here! It will remove conversions from pointer
1078 to integer and cause infinite recursion. */
1080 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1081 || (TREE_CODE (exp) == NOP_EXPR
1082 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1084 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1086 exp = TREE_OPERAND (exp, 0);
1089 /* Preserve the original expression code. */
1090 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1091 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1093 if (code == FUNCTION_TYPE)
1095 return build_unary_op (ADDR_EXPR, exp, 0);
1097 if (code == ARRAY_TYPE)
1100 tree restype = TREE_TYPE (type);
1106 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1108 constp = TREE_READONLY (exp);
1109 volatilep = TREE_THIS_VOLATILE (exp);
1112 if (TYPE_QUALS (type) || constp || volatilep)
1114 = c_build_qualified_type (restype,
1116 | (constp * TYPE_QUAL_CONST)
1117 | (volatilep * TYPE_QUAL_VOLATILE));
1119 if (TREE_CODE (exp) == INDIRECT_REF)
1120 return convert (TYPE_POINTER_TO (restype),
1121 TREE_OPERAND (exp, 0));
1123 if (TREE_CODE (exp) == COMPOUND_EXPR)
1125 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1126 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1127 TREE_OPERAND (exp, 0), op1);
1130 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1131 if (!flag_isoc99 && !lvalue_array_p)
1133 /* Before C99, non-lvalue arrays do not decay to pointers.
1134 Normally, using such an array would be invalid; but it can
1135 be used correctly inside sizeof or as a statement expression.
1136 Thus, do not give an error here; an error will result later. */
1140 ptrtype = build_pointer_type (restype);
1142 if (TREE_CODE (exp) == VAR_DECL)
1144 /* ??? This is not really quite correct
1145 in that the type of the operand of ADDR_EXPR
1146 is not the target type of the type of the ADDR_EXPR itself.
1147 Question is, can this lossage be avoided? */
1148 adr = build1 (ADDR_EXPR, ptrtype, exp);
1149 if (!c_mark_addressable (exp))
1150 return error_mark_node;
1151 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1154 /* This way is better for a COMPONENT_REF since it can
1155 simplify the offset for a component. */
1156 adr = build_unary_op (ADDR_EXPR, exp, 1);
1157 return convert (ptrtype, adr);
1162 /* Perform default promotions for C data used in expressions.
1163 Arrays and functions are converted to pointers;
1164 enumeral types or short or char, to int.
1165 In addition, manifest constants symbols are replaced by their values. */
1168 default_conversion (tree exp)
1171 tree type = TREE_TYPE (exp);
1172 enum tree_code code = TREE_CODE (type);
1174 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1175 return default_function_array_conversion (exp);
1177 /* Constants can be used directly unless they're not loadable. */
1178 if (TREE_CODE (exp) == CONST_DECL)
1179 exp = DECL_INITIAL (exp);
1181 /* Replace a nonvolatile const static variable with its value unless
1182 it is an array, in which case we must be sure that taking the
1183 address of the array produces consistent results. */
1184 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1186 exp = decl_constant_value_for_broken_optimization (exp);
1187 type = TREE_TYPE (exp);
1190 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1193 Do not use STRIP_NOPS here! It will remove conversions from pointer
1194 to integer and cause infinite recursion. */
1196 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1197 || (TREE_CODE (exp) == NOP_EXPR
1198 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1199 exp = TREE_OPERAND (exp, 0);
1201 /* Preserve the original expression code. */
1202 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1203 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1205 /* Normally convert enums to int,
1206 but convert wide enums to something wider. */
1207 if (code == ENUMERAL_TYPE)
1209 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1210 TYPE_PRECISION (integer_type_node)),
1211 ((TYPE_PRECISION (type)
1212 >= TYPE_PRECISION (integer_type_node))
1213 && TYPE_UNSIGNED (type)));
1215 return convert (type, exp);
1218 if (TREE_CODE (exp) == COMPONENT_REF
1219 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1220 /* If it's thinner than an int, promote it like a
1221 c_promoting_integer_type_p, otherwise leave it alone. */
1222 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1223 TYPE_PRECISION (integer_type_node)))
1224 return convert (integer_type_node, exp);
1226 if (c_promoting_integer_type_p (type))
1228 /* Preserve unsignedness if not really getting any wider. */
1229 if (TYPE_UNSIGNED (type)
1230 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1231 return convert (unsigned_type_node, exp);
1233 return convert (integer_type_node, exp);
1236 if (code == VOID_TYPE)
1238 error ("void value not ignored as it ought to be");
1239 return error_mark_node;
1244 /* Look up COMPONENT in a structure or union DECL.
1246 If the component name is not found, returns NULL_TREE. Otherwise,
1247 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1248 stepping down the chain to the component, which is in the last
1249 TREE_VALUE of the list. Normally the list is of length one, but if
1250 the component is embedded within (nested) anonymous structures or
1251 unions, the list steps down the chain to the component. */
1254 lookup_field (tree decl, tree component)
1256 tree type = TREE_TYPE (decl);
1259 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1260 to the field elements. Use a binary search on this array to quickly
1261 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1262 will always be set for structures which have many elements. */
1264 if (TYPE_LANG_SPECIFIC (type))
1267 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1269 field = TYPE_FIELDS (type);
1271 top = TYPE_LANG_SPECIFIC (type)->s->len;
1272 while (top - bot > 1)
1274 half = (top - bot + 1) >> 1;
1275 field = field_array[bot+half];
1277 if (DECL_NAME (field) == NULL_TREE)
1279 /* Step through all anon unions in linear fashion. */
1280 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1282 field = field_array[bot++];
1283 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1284 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1286 tree anon = lookup_field (field, component);
1289 return tree_cons (NULL_TREE, field, anon);
1293 /* Entire record is only anon unions. */
1297 /* Restart the binary search, with new lower bound. */
1301 if (DECL_NAME (field) == component)
1303 if (DECL_NAME (field) < component)
1309 if (DECL_NAME (field_array[bot]) == component)
1310 field = field_array[bot];
1311 else if (DECL_NAME (field) != component)
1316 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1318 if (DECL_NAME (field) == NULL_TREE
1319 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1320 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1322 tree anon = lookup_field (field, component);
1325 return tree_cons (NULL_TREE, field, anon);
1328 if (DECL_NAME (field) == component)
1332 if (field == NULL_TREE)
1336 return tree_cons (NULL_TREE, field, NULL_TREE);
1339 /* Make an expression to refer to the COMPONENT field of
1340 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1343 build_component_ref (tree datum, tree component)
1345 tree type = TREE_TYPE (datum);
1346 enum tree_code code = TREE_CODE (type);
1350 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1351 Ensure that the arguments are not lvalues; otherwise,
1352 if the component is an array, it would wrongly decay to a pointer in
1354 We cannot do this with a COND_EXPR, because in a conditional expression
1355 the default promotions are applied to both sides, and this would yield
1356 the wrong type of the result; for example, if the components have
1358 switch (TREE_CODE (datum))
1362 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1363 return build (COMPOUND_EXPR, TREE_TYPE (value),
1364 TREE_OPERAND (datum, 0), non_lvalue (value));
1370 /* See if there is a field or component with name COMPONENT. */
1372 if (code == RECORD_TYPE || code == UNION_TYPE)
1374 if (!COMPLETE_TYPE_P (type))
1376 c_incomplete_type_error (NULL_TREE, type);
1377 return error_mark_node;
1380 field = lookup_field (datum, component);
1384 error ("%s has no member named `%s'",
1385 code == RECORD_TYPE ? "structure" : "union",
1386 IDENTIFIER_POINTER (component));
1387 return error_mark_node;
1390 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1391 This might be better solved in future the way the C++ front
1392 end does it - by giving the anonymous entities each a
1393 separate name and type, and then have build_component_ref
1394 recursively call itself. We can't do that here. */
1397 tree subdatum = TREE_VALUE (field);
1399 if (TREE_TYPE (subdatum) == error_mark_node)
1400 return error_mark_node;
1402 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1403 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1404 TREE_READONLY (ref) = 1;
1405 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1406 TREE_THIS_VOLATILE (ref) = 1;
1408 if (TREE_DEPRECATED (subdatum))
1409 warn_deprecated_use (subdatum);
1413 field = TREE_CHAIN (field);
1419 else if (code != ERROR_MARK)
1420 error ("request for member `%s' in something not a structure or union",
1421 IDENTIFIER_POINTER (component));
1423 return error_mark_node;
1426 /* Given an expression PTR for a pointer, return an expression
1427 for the value pointed to.
1428 ERRORSTRING is the name of the operator to appear in error messages. */
1431 build_indirect_ref (tree ptr, const char *errorstring)
1433 tree pointer = default_conversion (ptr);
1434 tree type = TREE_TYPE (pointer);
1436 if (TREE_CODE (type) == POINTER_TYPE)
1438 if (TREE_CODE (pointer) == ADDR_EXPR
1439 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1440 == TREE_TYPE (type)))
1441 return TREE_OPERAND (pointer, 0);
1444 tree t = TREE_TYPE (type);
1445 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1447 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1449 error ("dereferencing pointer to incomplete type");
1450 return error_mark_node;
1452 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1453 warning ("dereferencing `void *' pointer");
1455 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1456 so that we get the proper error message if the result is used
1457 to assign to. Also, &* is supposed to be a no-op.
1458 And ANSI C seems to specify that the type of the result
1459 should be the const type. */
1460 /* A de-reference of a pointer to const is not a const. It is valid
1461 to change it via some other pointer. */
1462 TREE_READONLY (ref) = TYPE_READONLY (t);
1463 TREE_SIDE_EFFECTS (ref)
1464 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1465 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1469 else if (TREE_CODE (pointer) != ERROR_MARK)
1470 error ("invalid type argument of `%s'", errorstring);
1471 return error_mark_node;
1474 /* This handles expressions of the form "a[i]", which denotes
1477 This is logically equivalent in C to *(a+i), but we may do it differently.
1478 If A is a variable or a member, we generate a primitive ARRAY_REF.
1479 This avoids forcing the array out of registers, and can work on
1480 arrays that are not lvalues (for example, members of structures returned
1484 build_array_ref (tree array, tree index)
1488 error ("subscript missing in array reference");
1489 return error_mark_node;
1492 if (TREE_TYPE (array) == error_mark_node
1493 || TREE_TYPE (index) == error_mark_node)
1494 return error_mark_node;
1496 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1500 /* Subscripting with type char is likely to lose
1501 on a machine where chars are signed.
1502 So warn on any machine, but optionally.
1503 Don't warn for unsigned char since that type is safe.
1504 Don't warn for signed char because anyone who uses that
1505 must have done so deliberately. */
1506 if (warn_char_subscripts
1507 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1508 warning ("array subscript has type `char'");
1510 /* Apply default promotions *after* noticing character types. */
1511 index = default_conversion (index);
1513 /* Require integer *after* promotion, for sake of enums. */
1514 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1516 error ("array subscript is not an integer");
1517 return error_mark_node;
1520 /* An array that is indexed by a non-constant
1521 cannot be stored in a register; we must be able to do
1522 address arithmetic on its address.
1523 Likewise an array of elements of variable size. */
1524 if (TREE_CODE (index) != INTEGER_CST
1525 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1526 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1528 if (!c_mark_addressable (array))
1529 return error_mark_node;
1531 /* An array that is indexed by a constant value which is not within
1532 the array bounds cannot be stored in a register either; because we
1533 would get a crash in store_bit_field/extract_bit_field when trying
1534 to access a non-existent part of the register. */
1535 if (TREE_CODE (index) == INTEGER_CST
1536 && TYPE_DOMAIN (TREE_TYPE (array))
1537 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1539 if (!c_mark_addressable (array))
1540 return error_mark_node;
1546 while (TREE_CODE (foo) == COMPONENT_REF)
1547 foo = TREE_OPERAND (foo, 0);
1548 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1549 pedwarn ("ISO C forbids subscripting `register' array");
1550 else if (! flag_isoc99 && ! lvalue_p (foo))
1551 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1554 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1555 rval = build (ARRAY_REF, type, array, index);
1556 /* Array ref is const/volatile if the array elements are
1557 or if the array is. */
1558 TREE_READONLY (rval)
1559 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1560 | TREE_READONLY (array));
1561 TREE_SIDE_EFFECTS (rval)
1562 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1563 | TREE_SIDE_EFFECTS (array));
1564 TREE_THIS_VOLATILE (rval)
1565 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1566 /* This was added by rms on 16 Nov 91.
1567 It fixes vol struct foo *a; a->elts[1]
1568 in an inline function.
1569 Hope it doesn't break something else. */
1570 | TREE_THIS_VOLATILE (array));
1571 return require_complete_type (fold (rval));
1575 tree ar = default_conversion (array);
1576 tree ind = default_conversion (index);
1578 /* Do the same warning check as above, but only on the part that's
1579 syntactically the index and only if it is also semantically
1581 if (warn_char_subscripts
1582 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1583 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1584 warning ("subscript has type `char'");
1586 /* Put the integer in IND to simplify error checking. */
1587 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1594 if (ar == error_mark_node)
1597 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1598 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1600 error ("subscripted value is neither array nor pointer");
1601 return error_mark_node;
1603 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1605 error ("array subscript is not an integer");
1606 return error_mark_node;
1609 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1614 /* Build an external reference to identifier ID. FUN indicates
1615 whether this will be used for a function call. */
1617 build_external_ref (tree id, int fun)
1620 tree decl = lookup_name (id);
1621 tree objc_ivar = lookup_objc_ivar (id);
1623 if (decl && decl != error_mark_node)
1625 /* Properly declared variable or function reference. */
1628 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1630 warning ("local declaration of `%s' hides instance variable",
1631 IDENTIFIER_POINTER (id));
1640 /* Implicit function declaration. */
1641 ref = implicitly_declare (id);
1642 else if (decl == error_mark_node)
1643 /* Don't complain about something that's already been
1644 complained about. */
1645 return error_mark_node;
1648 undeclared_variable (id);
1649 return error_mark_node;
1652 if (TREE_TYPE (ref) == error_mark_node)
1653 return error_mark_node;
1655 if (TREE_DEPRECATED (ref))
1656 warn_deprecated_use (ref);
1658 if (!skip_evaluation)
1659 assemble_external (ref);
1660 TREE_USED (ref) = 1;
1662 if (TREE_CODE (ref) == CONST_DECL)
1664 ref = DECL_INITIAL (ref);
1665 TREE_CONSTANT (ref) = 1;
1666 TREE_INVARIANT (ref) = 1;
1668 else if (current_function_decl != 0
1669 && !DECL_FILE_SCOPE_P (current_function_decl)
1670 && (TREE_CODE (ref) == VAR_DECL
1671 || TREE_CODE (ref) == PARM_DECL
1672 || TREE_CODE (ref) == FUNCTION_DECL))
1674 tree context = decl_function_context (ref);
1676 if (context != 0 && context != current_function_decl)
1677 DECL_NONLOCAL (ref) = 1;
1683 /* Build a function call to function FUNCTION with parameters PARAMS.
1684 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1685 TREE_VALUE of each node is a parameter-expression.
1686 FUNCTION's data type may be a function type or a pointer-to-function. */
1689 build_function_call (tree function, tree params)
1691 tree fntype, fundecl = 0;
1692 tree coerced_params;
1693 tree name = NULL_TREE, result;
1696 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1697 STRIP_TYPE_NOPS (function);
1699 /* Convert anything with function type to a pointer-to-function. */
1700 if (TREE_CODE (function) == FUNCTION_DECL)
1702 name = DECL_NAME (function);
1704 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1705 (because calling an inline function does not mean the function
1706 needs to be separately compiled). */
1707 fntype = build_type_variant (TREE_TYPE (function),
1708 TREE_READONLY (function),
1709 TREE_THIS_VOLATILE (function));
1711 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1714 function = default_conversion (function);
1716 fntype = TREE_TYPE (function);
1718 if (TREE_CODE (fntype) == ERROR_MARK)
1719 return error_mark_node;
1721 if (!(TREE_CODE (fntype) == POINTER_TYPE
1722 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1724 error ("called object is not a function");
1725 return error_mark_node;
1728 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1729 current_function_returns_abnormally = 1;
1731 /* fntype now gets the type of function pointed to. */
1732 fntype = TREE_TYPE (fntype);
1734 /* Check that the function is called through a compatible prototype.
1735 If it is not, replace the call by a trap, wrapped up in a compound
1736 expression if necessary. This has the nice side-effect to prevent
1737 the tree-inliner from generating invalid assignment trees which may
1738 blow up in the RTL expander later.
1740 ??? This doesn't work for Objective-C because objc_comptypes
1741 refuses to compare function prototypes, yet the compiler appears
1742 to build calls that are flagged as invalid by C's comptypes. */
1743 if (! c_dialect_objc ()
1744 && TREE_CODE (function) == NOP_EXPR
1745 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1746 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1747 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1749 tree return_type = TREE_TYPE (fntype);
1750 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1753 /* This situation leads to run-time undefined behavior. We can't,
1754 therefore, simply error unless we can prove that all possible
1755 executions of the program must execute the code. */
1756 warning ("function called through a non-compatible type");
1758 /* We can, however, treat "undefined" any way we please.
1759 Call abort to encourage the user to fix the program. */
1760 inform ("if this code is reached, the program will abort");
1762 if (VOID_TYPE_P (return_type))
1768 if (AGGREGATE_TYPE_P (return_type))
1769 rhs = build_compound_literal (return_type,
1770 build_constructor (return_type,
1773 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1775 return build (COMPOUND_EXPR, return_type, trap, rhs);
1779 /* Convert the parameters to the types declared in the
1780 function prototype, or apply default promotions. */
1783 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1785 /* Check that the arguments to the function are valid. */
1787 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1789 /* Recognize certain built-in functions so we can make tree-codes
1790 other than CALL_EXPR. We do this when it enables fold-const.c
1791 to do something useful. */
1793 if (TREE_CODE (function) == ADDR_EXPR
1794 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1795 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1797 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1798 params, coerced_params);
1803 result = build (CALL_EXPR, TREE_TYPE (fntype),
1804 function, coerced_params, NULL_TREE);
1805 TREE_SIDE_EFFECTS (result) = 1;
1806 result = fold (result);
1808 if (VOID_TYPE_P (TREE_TYPE (result)))
1810 return require_complete_type (result);
1813 /* Convert the argument expressions in the list VALUES
1814 to the types in the list TYPELIST. The result is a list of converted
1815 argument expressions.
1817 If TYPELIST is exhausted, or when an element has NULL as its type,
1818 perform the default conversions.
1820 PARMLIST is the chain of parm decls for the function being called.
1821 It may be 0, if that info is not available.
1822 It is used only for generating error messages.
1824 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1826 This is also where warnings about wrong number of args are generated.
1828 Both VALUES and the returned value are chains of TREE_LIST nodes
1829 with the elements of the list in the TREE_VALUE slots of those nodes. */
1832 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1834 tree typetail, valtail;
1838 /* Scan the given expressions and types, producing individual
1839 converted arguments and pushing them on RESULT in reverse order. */
1841 for (valtail = values, typetail = typelist, parmnum = 0;
1843 valtail = TREE_CHAIN (valtail), parmnum++)
1845 tree type = typetail ? TREE_VALUE (typetail) : 0;
1846 tree val = TREE_VALUE (valtail);
1848 if (type == void_type_node)
1851 error ("too many arguments to function `%s'",
1852 IDENTIFIER_POINTER (name));
1854 error ("too many arguments to function");
1858 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1859 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1860 to convert automatically to a pointer. */
1861 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1862 val = TREE_OPERAND (val, 0);
1864 val = default_function_array_conversion (val);
1866 val = require_complete_type (val);
1870 /* Formal parm type is specified by a function prototype. */
1873 if (!COMPLETE_TYPE_P (type))
1875 error ("type of formal parameter %d is incomplete", parmnum + 1);
1880 /* Optionally warn about conversions that
1881 differ from the default conversions. */
1882 if (warn_conversion || warn_traditional)
1884 int formal_prec = TYPE_PRECISION (type);
1886 if (INTEGRAL_TYPE_P (type)
1887 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1888 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1889 if (INTEGRAL_TYPE_P (type)
1890 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1891 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1892 else if (TREE_CODE (type) == COMPLEX_TYPE
1893 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1894 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1895 else if (TREE_CODE (type) == REAL_TYPE
1896 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1897 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1898 else if (TREE_CODE (type) == COMPLEX_TYPE
1899 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1900 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1901 else if (TREE_CODE (type) == REAL_TYPE
1902 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1903 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1904 /* ??? At some point, messages should be written about
1905 conversions between complex types, but that's too messy
1907 else if (TREE_CODE (type) == REAL_TYPE
1908 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1910 /* Warn if any argument is passed as `float',
1911 since without a prototype it would be `double'. */
1912 if (formal_prec == TYPE_PRECISION (float_type_node))
1913 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1915 /* Detect integer changing in width or signedness.
1916 These warnings are only activated with
1917 -Wconversion, not with -Wtraditional. */
1918 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1919 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1921 tree would_have_been = default_conversion (val);
1922 tree type1 = TREE_TYPE (would_have_been);
1924 if (TREE_CODE (type) == ENUMERAL_TYPE
1925 && (TYPE_MAIN_VARIANT (type)
1926 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1927 /* No warning if function asks for enum
1928 and the actual arg is that enum type. */
1930 else if (formal_prec != TYPE_PRECISION (type1))
1931 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1932 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
1934 /* Don't complain if the formal parameter type
1935 is an enum, because we can't tell now whether
1936 the value was an enum--even the same enum. */
1937 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1939 else if (TREE_CODE (val) == INTEGER_CST
1940 && int_fits_type_p (val, type))
1941 /* Change in signedness doesn't matter
1942 if a constant value is unaffected. */
1944 /* Likewise for a constant in a NOP_EXPR. */
1945 else if (TREE_CODE (val) == NOP_EXPR
1946 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1947 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1949 /* If the value is extended from a narrower
1950 unsigned type, it doesn't matter whether we
1951 pass it as signed or unsigned; the value
1952 certainly is the same either way. */
1953 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1954 && TYPE_UNSIGNED (TREE_TYPE (val)))
1956 else if (TYPE_UNSIGNED (type))
1957 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1959 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1963 parmval = convert_for_assignment (type, val,
1964 (char *) 0, /* arg passing */
1965 fundecl, name, parmnum + 1);
1967 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1968 && INTEGRAL_TYPE_P (type)
1969 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1970 parmval = default_conversion (parmval);
1972 result = tree_cons (NULL_TREE, parmval, result);
1974 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1975 && (TYPE_PRECISION (TREE_TYPE (val))
1976 < TYPE_PRECISION (double_type_node)))
1977 /* Convert `float' to `double'. */
1978 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1980 /* Convert `short' and `char' to full-size `int'. */
1981 result = tree_cons (NULL_TREE, default_conversion (val), result);
1984 typetail = TREE_CHAIN (typetail);
1987 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1990 error ("too few arguments to function `%s'",
1991 IDENTIFIER_POINTER (name));
1993 error ("too few arguments to function");
1996 return nreverse (result);
1999 /* This is the entry point used by the parser
2000 for binary operators in the input.
2001 In addition to constructing the expression,
2002 we check for operands that were written with other binary operators
2003 in a way that is likely to confuse the user. */
2006 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
2008 tree result = build_binary_op (code, arg1, arg2, 1);
2011 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2012 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2013 enum tree_code code1 = ERROR_MARK;
2014 enum tree_code code2 = ERROR_MARK;
2016 if (TREE_CODE (result) == ERROR_MARK)
2017 return error_mark_node;
2019 if (IS_EXPR_CODE_CLASS (class1))
2020 code1 = C_EXP_ORIGINAL_CODE (arg1);
2021 if (IS_EXPR_CODE_CLASS (class2))
2022 code2 = C_EXP_ORIGINAL_CODE (arg2);
2024 /* Check for cases such as x+y<<z which users are likely
2025 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2026 is cleared to prevent these warnings. */
2027 if (warn_parentheses)
2029 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2031 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2032 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2033 warning ("suggest parentheses around + or - inside shift");
2036 if (code == TRUTH_ORIF_EXPR)
2038 if (code1 == TRUTH_ANDIF_EXPR
2039 || code2 == TRUTH_ANDIF_EXPR)
2040 warning ("suggest parentheses around && within ||");
2043 if (code == BIT_IOR_EXPR)
2045 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2046 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2047 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2048 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2049 warning ("suggest parentheses around arithmetic in operand of |");
2050 /* Check cases like x|y==z */
2051 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2052 warning ("suggest parentheses around comparison in operand of |");
2055 if (code == BIT_XOR_EXPR)
2057 if (code1 == BIT_AND_EXPR
2058 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2059 || code2 == BIT_AND_EXPR
2060 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2061 warning ("suggest parentheses around arithmetic in operand of ^");
2062 /* Check cases like x^y==z */
2063 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2064 warning ("suggest parentheses around comparison in operand of ^");
2067 if (code == BIT_AND_EXPR)
2069 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2070 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2071 warning ("suggest parentheses around + or - in operand of &");
2072 /* Check cases like x&y==z */
2073 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2074 warning ("suggest parentheses around comparison in operand of &");
2078 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2079 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2080 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2081 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2083 unsigned_conversion_warning (result, arg1);
2084 unsigned_conversion_warning (result, arg2);
2085 overflow_warning (result);
2087 class = TREE_CODE_CLASS (TREE_CODE (result));
2089 /* Record the code that was specified in the source,
2090 for the sake of warnings about confusing nesting. */
2091 if (IS_EXPR_CODE_CLASS (class))
2092 C_SET_EXP_ORIGINAL_CODE (result, code);
2095 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2096 so that convert_for_assignment wouldn't strip it.
2097 That way, we got warnings for things like p = (1 - 1).
2098 But it turns out we should not get those warnings. */
2099 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2100 C_SET_EXP_ORIGINAL_CODE (result, code);
2107 /* Return true if `t' is known to be non-negative. */
2110 c_tree_expr_nonnegative_p (tree t)
2112 if (TREE_CODE (t) == STMT_EXPR)
2114 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2116 /* Find the last statement in the chain, ignoring the final
2117 * scope statement */
2118 while (TREE_CHAIN (t) != NULL_TREE
2119 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2121 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2123 return tree_expr_nonnegative_p (t);
2126 /* Return a tree for the difference of pointers OP0 and OP1.
2127 The resulting tree has type int. */
2130 pointer_diff (tree op0, tree op1)
2132 tree restype = ptrdiff_type_node;
2134 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2135 tree con0, con1, lit0, lit1;
2136 tree orig_op1 = op1;
2138 if (pedantic || warn_pointer_arith)
2140 if (TREE_CODE (target_type) == VOID_TYPE)
2141 pedwarn ("pointer of type `void *' used in subtraction");
2142 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2143 pedwarn ("pointer to a function used in subtraction");
2146 /* If the conversion to ptrdiff_type does anything like widening or
2147 converting a partial to an integral mode, we get a convert_expression
2148 that is in the way to do any simplifications.
2149 (fold-const.c doesn't know that the extra bits won't be needed.
2150 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2151 different mode in place.)
2152 So first try to find a common term here 'by hand'; we want to cover
2153 at least the cases that occur in legal static initializers. */
2154 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2155 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2157 if (TREE_CODE (con0) == PLUS_EXPR)
2159 lit0 = TREE_OPERAND (con0, 1);
2160 con0 = TREE_OPERAND (con0, 0);
2163 lit0 = integer_zero_node;
2165 if (TREE_CODE (con1) == PLUS_EXPR)
2167 lit1 = TREE_OPERAND (con1, 1);
2168 con1 = TREE_OPERAND (con1, 0);
2171 lit1 = integer_zero_node;
2173 if (operand_equal_p (con0, con1, 0))
2180 /* First do the subtraction as integers;
2181 then drop through to build the divide operator.
2182 Do not do default conversions on the minus operator
2183 in case restype is a short type. */
2185 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2186 convert (restype, op1), 0);
2187 /* This generates an error if op1 is pointer to incomplete type. */
2188 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2189 error ("arithmetic on pointer to an incomplete type");
2191 /* This generates an error if op0 is pointer to incomplete type. */
2192 op1 = c_size_in_bytes (target_type);
2194 /* Divide by the size, in easiest possible way. */
2195 return fold (build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2198 /* Construct and perhaps optimize a tree representation
2199 for a unary operation. CODE, a tree_code, specifies the operation
2200 and XARG is the operand.
2201 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2202 the default promotions (such as from short to int).
2203 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2204 allows non-lvalues; this is only used to handle conversion of non-lvalue
2205 arrays to pointers in C99. */
2208 build_unary_op (enum tree_code code, tree xarg, int flag)
2210 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2213 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2215 int noconvert = flag;
2217 if (typecode == ERROR_MARK)
2218 return error_mark_node;
2219 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2220 typecode = INTEGER_TYPE;
2225 /* This is used for unary plus, because a CONVERT_EXPR
2226 is enough to prevent anybody from looking inside for
2227 associativity, but won't generate any code. */
2228 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2229 || typecode == COMPLEX_TYPE))
2231 error ("wrong type argument to unary plus");
2232 return error_mark_node;
2234 else if (!noconvert)
2235 arg = default_conversion (arg);
2236 arg = non_lvalue (arg);
2240 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2241 || typecode == COMPLEX_TYPE
2242 || typecode == VECTOR_TYPE))
2244 error ("wrong type argument to unary minus");
2245 return error_mark_node;
2247 else if (!noconvert)
2248 arg = default_conversion (arg);
2252 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2255 arg = default_conversion (arg);
2257 else if (typecode == COMPLEX_TYPE)
2261 pedwarn ("ISO C does not support `~' for complex conjugation");
2263 arg = default_conversion (arg);
2267 error ("wrong type argument to bit-complement");
2268 return error_mark_node;
2273 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2275 error ("wrong type argument to abs");
2276 return error_mark_node;
2278 else if (!noconvert)
2279 arg = default_conversion (arg);
2283 /* Conjugating a real value is a no-op, but allow it anyway. */
2284 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2285 || typecode == COMPLEX_TYPE))
2287 error ("wrong type argument to conjugation");
2288 return error_mark_node;
2290 else if (!noconvert)
2291 arg = default_conversion (arg);
2294 case TRUTH_NOT_EXPR:
2295 if (typecode != INTEGER_TYPE
2296 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2297 && typecode != COMPLEX_TYPE
2298 /* These will convert to a pointer. */
2299 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2301 error ("wrong type argument to unary exclamation mark");
2302 return error_mark_node;
2304 arg = lang_hooks.truthvalue_conversion (arg);
2305 return invert_truthvalue (arg);
2311 if (TREE_CODE (arg) == COMPLEX_CST)
2312 return TREE_REALPART (arg);
2313 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2314 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2319 if (TREE_CODE (arg) == COMPLEX_CST)
2320 return TREE_IMAGPART (arg);
2321 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2322 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2324 return convert (TREE_TYPE (arg), integer_zero_node);
2326 case PREINCREMENT_EXPR:
2327 case POSTINCREMENT_EXPR:
2328 case PREDECREMENT_EXPR:
2329 case POSTDECREMENT_EXPR:
2331 /* Increment or decrement the real part of the value,
2332 and don't change the imaginary part. */
2333 if (typecode == COMPLEX_TYPE)
2338 pedwarn ("ISO C does not support `++' and `--' on complex types");
2340 arg = stabilize_reference (arg);
2341 real = build_unary_op (REALPART_EXPR, arg, 1);
2342 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2343 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2344 build_unary_op (code, real, 1), imag);
2347 /* Report invalid types. */
2349 if (typecode != POINTER_TYPE
2350 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2352 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2353 error ("wrong type argument to increment");
2355 error ("wrong type argument to decrement");
2357 return error_mark_node;
2362 tree result_type = TREE_TYPE (arg);
2364 arg = get_unwidened (arg, 0);
2365 argtype = TREE_TYPE (arg);
2367 /* Compute the increment. */
2369 if (typecode == POINTER_TYPE)
2371 /* If pointer target is an undefined struct,
2372 we just cannot know how to do the arithmetic. */
2373 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2375 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2376 error ("increment of pointer to unknown structure");
2378 error ("decrement of pointer to unknown structure");
2380 else if ((pedantic || warn_pointer_arith)
2381 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2382 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2384 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2385 pedwarn ("wrong type argument to increment");
2387 pedwarn ("wrong type argument to decrement");
2390 inc = c_size_in_bytes (TREE_TYPE (result_type));
2393 inc = integer_one_node;
2395 inc = convert (argtype, inc);
2397 /* Complain about anything else that is not a true lvalue. */
2398 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2399 || code == POSTINCREMENT_EXPR)
2400 ? "invalid lvalue in increment"
2401 : "invalid lvalue in decrement")))
2402 return error_mark_node;
2404 /* Report a read-only lvalue. */
2405 if (TREE_READONLY (arg))
2406 readonly_error (arg,
2407 ((code == PREINCREMENT_EXPR
2408 || code == POSTINCREMENT_EXPR)
2409 ? "increment" : "decrement"));
2411 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2412 val = boolean_increment (code, arg);
2414 val = build (code, TREE_TYPE (arg), arg, inc);
2415 TREE_SIDE_EFFECTS (val) = 1;
2416 val = convert (result_type, val);
2417 if (TREE_CODE (val) != code)
2418 TREE_NO_WARNING (val) = 1;
2423 /* Note that this operation never does default_conversion. */
2425 /* Let &* cancel out to simplify resulting code. */
2426 if (TREE_CODE (arg) == INDIRECT_REF)
2428 /* Don't let this be an lvalue. */
2429 if (lvalue_p (TREE_OPERAND (arg, 0)))
2430 return non_lvalue (TREE_OPERAND (arg, 0));
2431 return TREE_OPERAND (arg, 0);
2434 /* For &x[y], return x+y */
2435 if (TREE_CODE (arg) == ARRAY_REF)
2437 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2438 return error_mark_node;
2439 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2440 TREE_OPERAND (arg, 1), 1);
2443 /* Anything not already handled and not a true memory reference
2444 or a non-lvalue array is an error. */
2445 else if (typecode != FUNCTION_TYPE && !flag
2446 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2447 return error_mark_node;
2449 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2450 argtype = TREE_TYPE (arg);
2452 /* If the lvalue is const or volatile, merge that into the type
2453 to which the address will point. Note that you can't get a
2454 restricted pointer by taking the address of something, so we
2455 only have to deal with `const' and `volatile' here. */
2456 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2457 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2458 argtype = c_build_type_variant (argtype,
2459 TREE_READONLY (arg),
2460 TREE_THIS_VOLATILE (arg));
2462 argtype = build_pointer_type (argtype);
2464 if (!c_mark_addressable (arg))
2465 return error_mark_node;
2470 if (TREE_CODE (arg) == COMPONENT_REF)
2472 tree field = TREE_OPERAND (arg, 1);
2474 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2476 if (DECL_C_BIT_FIELD (field))
2478 error ("attempt to take address of bit-field structure member `%s'",
2479 IDENTIFIER_POINTER (DECL_NAME (field)));
2480 return error_mark_node;
2483 addr = fold (build (PLUS_EXPR, argtype,
2484 convert (argtype, addr),
2485 convert (argtype, byte_position (field))));
2488 addr = build1 (code, argtype, arg);
2498 argtype = TREE_TYPE (arg);
2499 return fold (build1 (code, argtype, arg));
2502 /* Return nonzero if REF is an lvalue valid for this language.
2503 Lvalues can be assigned, unless their type has TYPE_READONLY.
2504 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2509 enum tree_code code = TREE_CODE (ref);
2516 return lvalue_p (TREE_OPERAND (ref, 0));
2518 case COMPOUND_LITERAL_EXPR:
2528 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2529 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2533 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2540 /* Return nonzero if REF is an lvalue valid for this language;
2541 otherwise, print an error message and return zero. */
2544 lvalue_or_else (tree ref, const char *msgid)
2546 int win = lvalue_p (ref);
2549 error ("%s", msgid);
2555 /* Warn about storing in something that is `const'. */
2558 readonly_error (tree arg, const char *msgid)
2560 if (TREE_CODE (arg) == COMPONENT_REF)
2562 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2563 readonly_error (TREE_OPERAND (arg, 0), msgid);
2565 error ("%s of read-only member `%s'", _(msgid),
2566 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2568 else if (TREE_CODE (arg) == VAR_DECL)
2569 error ("%s of read-only variable `%s'", _(msgid),
2570 IDENTIFIER_POINTER (DECL_NAME (arg)));
2572 error ("%s of read-only location", _(msgid));
2575 /* Mark EXP saying that we need to be able to take the
2576 address of it; it should not be allocated in a register.
2577 Returns true if successful. */
2580 c_mark_addressable (tree exp)
2585 switch (TREE_CODE (x))
2588 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2590 error ("cannot take address of bit-field `%s'",
2591 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2595 /* ... fall through ... */
2601 x = TREE_OPERAND (x, 0);
2604 case COMPOUND_LITERAL_EXPR:
2606 TREE_ADDRESSABLE (x) = 1;
2613 if (C_DECL_REGISTER (x)
2614 && DECL_NONLOCAL (x))
2616 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2618 error ("global register variable `%s' used in nested function",
2619 IDENTIFIER_POINTER (DECL_NAME (x)));
2622 pedwarn ("register variable `%s' used in nested function",
2623 IDENTIFIER_POINTER (DECL_NAME (x)));
2625 else if (C_DECL_REGISTER (x))
2627 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2629 error ("address of global register variable `%s' requested",
2630 IDENTIFIER_POINTER (DECL_NAME (x)));
2634 pedwarn ("address of register variable `%s' requested",
2635 IDENTIFIER_POINTER (DECL_NAME (x)));
2637 put_var_into_stack (x, /*rescan=*/true);
2641 TREE_ADDRESSABLE (x) = 1;
2648 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2651 build_conditional_expr (tree ifexp, tree op1, tree op2)
2655 enum tree_code code1;
2656 enum tree_code code2;
2657 tree result_type = NULL;
2658 tree orig_op1 = op1, orig_op2 = op2;
2660 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2662 /* Promote both alternatives. */
2664 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2665 op1 = default_conversion (op1);
2666 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2667 op2 = default_conversion (op2);
2669 if (TREE_CODE (ifexp) == ERROR_MARK
2670 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2671 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2672 return error_mark_node;
2674 type1 = TREE_TYPE (op1);
2675 code1 = TREE_CODE (type1);
2676 type2 = TREE_TYPE (op2);
2677 code2 = TREE_CODE (type2);
2679 /* C90 does not permit non-lvalue arrays in conditional expressions.
2680 In C99 they will be pointers by now. */
2681 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2683 error ("non-lvalue array in conditional expression");
2684 return error_mark_node;
2687 /* Quickly detect the usual case where op1 and op2 have the same type
2689 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2692 result_type = type1;
2694 result_type = TYPE_MAIN_VARIANT (type1);
2696 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2697 || code1 == COMPLEX_TYPE)
2698 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2699 || code2 == COMPLEX_TYPE))
2701 result_type = common_type (type1, type2);
2703 /* If -Wsign-compare, warn here if type1 and type2 have
2704 different signedness. We'll promote the signed to unsigned
2705 and later code won't know it used to be different.
2706 Do this check on the original types, so that explicit casts
2707 will be considered, but default promotions won't. */
2708 if (warn_sign_compare && !skip_evaluation)
2710 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2711 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2713 if (unsigned_op1 ^ unsigned_op2)
2715 /* Do not warn if the result type is signed, since the
2716 signed type will only be chosen if it can represent
2717 all the values of the unsigned type. */
2718 if (! TYPE_UNSIGNED (result_type))
2720 /* Do not warn if the signed quantity is an unsuffixed
2721 integer literal (or some static constant expression
2722 involving such literals) and it is non-negative. */
2723 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2724 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2727 warning ("signed and unsigned type in conditional expression");
2731 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2733 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2734 pedwarn ("ISO C forbids conditional expr with only one void side");
2735 result_type = void_type_node;
2737 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2739 if (comp_target_types (type1, type2, 1))
2740 result_type = common_type (type1, type2);
2741 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2742 && TREE_CODE (orig_op1) != NOP_EXPR)
2743 result_type = qualify_type (type2, type1);
2744 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2745 && TREE_CODE (orig_op2) != NOP_EXPR)
2746 result_type = qualify_type (type1, type2);
2747 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2749 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2750 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2751 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2752 TREE_TYPE (type2)));
2754 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2756 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2757 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2758 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2759 TREE_TYPE (type1)));
2763 pedwarn ("pointer type mismatch in conditional expression");
2764 result_type = build_pointer_type (void_type_node);
2767 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2769 if (! integer_zerop (op2))
2770 pedwarn ("pointer/integer type mismatch in conditional expression");
2773 op2 = null_pointer_node;
2775 result_type = type1;
2777 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2779 if (!integer_zerop (op1))
2780 pedwarn ("pointer/integer type mismatch in conditional expression");
2783 op1 = null_pointer_node;
2785 result_type = type2;
2790 if (flag_cond_mismatch)
2791 result_type = void_type_node;
2794 error ("type mismatch in conditional expression");
2795 return error_mark_node;
2799 /* Merge const and volatile flags of the incoming types. */
2801 = build_type_variant (result_type,
2802 TREE_READONLY (op1) || TREE_READONLY (op2),
2803 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2805 if (result_type != TREE_TYPE (op1))
2806 op1 = convert_and_check (result_type, op1);
2807 if (result_type != TREE_TYPE (op2))
2808 op2 = convert_and_check (result_type, op2);
2810 if (TREE_CODE (ifexp) == INTEGER_CST)
2811 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2813 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2816 /* Given a list of expressions, return a compound expression
2817 that performs them all and returns the value of the last of them. */
2820 build_compound_expr (tree list)
2822 return internal_build_compound_expr (list, TRUE);
2826 internal_build_compound_expr (tree list, int first_p)
2830 if (TREE_CHAIN (list) == 0)
2832 /* Convert arrays and functions to pointers when there
2833 really is a comma operator. */
2836 = default_function_array_conversion (TREE_VALUE (list));
2838 /* Don't let (0, 0) be null pointer constant. */
2839 if (!first_p && integer_zerop (TREE_VALUE (list)))
2840 return non_lvalue (TREE_VALUE (list));
2841 return TREE_VALUE (list);
2844 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2846 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2848 /* The left-hand operand of a comma expression is like an expression
2849 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2850 any side-effects, unless it was explicitly cast to (void). */
2851 if (warn_unused_value
2852 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2853 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2854 warning ("left-hand operand of comma expression has no effect");
2857 /* With -Wunused, we should also warn if the left-hand operand does have
2858 side-effects, but computes a value which is not used. For example, in
2859 `foo() + bar(), baz()' the result of the `+' operator is not used,
2860 so we should issue a warning. */
2861 else if (warn_unused_value)
2862 warn_if_unused_value (TREE_VALUE (list));
2864 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2867 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2870 build_c_cast (tree type, tree expr)
2874 if (type == error_mark_node || expr == error_mark_node)
2875 return error_mark_node;
2877 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2878 only in <protocol> qualifications. But when constructing cast expressions,
2879 the protocols do matter and must be kept around. */
2880 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2881 type = TYPE_MAIN_VARIANT (type);
2883 if (TREE_CODE (type) == ARRAY_TYPE)
2885 error ("cast specifies array type");
2886 return error_mark_node;
2889 if (TREE_CODE (type) == FUNCTION_TYPE)
2891 error ("cast specifies function type");
2892 return error_mark_node;
2895 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2899 if (TREE_CODE (type) == RECORD_TYPE
2900 || TREE_CODE (type) == UNION_TYPE)
2901 pedwarn ("ISO C forbids casting nonscalar to the same type");
2904 else if (TREE_CODE (type) == UNION_TYPE)
2907 value = default_function_array_conversion (value);
2909 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2910 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2911 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2919 pedwarn ("ISO C forbids casts to union type");
2920 t = digest_init (type,
2921 build_constructor (type,
2922 build_tree_list (field, value)),
2924 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2925 TREE_INVARIANT (t) = TREE_INVARIANT (value);
2928 error ("cast to union type from type not present in union");
2929 return error_mark_node;
2935 /* If casting to void, avoid the error that would come
2936 from default_conversion in the case of a non-lvalue array. */
2937 if (type == void_type_node)
2938 return build1 (CONVERT_EXPR, type, value);
2940 /* Convert functions and arrays to pointers,
2941 but don't convert any other types. */
2942 value = default_function_array_conversion (value);
2943 otype = TREE_TYPE (value);
2945 /* Optionally warn about potentially worrisome casts. */
2948 && TREE_CODE (type) == POINTER_TYPE
2949 && TREE_CODE (otype) == POINTER_TYPE)
2951 tree in_type = type;
2952 tree in_otype = otype;
2956 /* Check that the qualifiers on IN_TYPE are a superset of
2957 the qualifiers of IN_OTYPE. The outermost level of
2958 POINTER_TYPE nodes is uninteresting and we stop as soon
2959 as we hit a non-POINTER_TYPE node on either type. */
2962 in_otype = TREE_TYPE (in_otype);
2963 in_type = TREE_TYPE (in_type);
2965 /* GNU C allows cv-qualified function types. 'const'
2966 means the function is very pure, 'volatile' means it
2967 can't return. We need to warn when such qualifiers
2968 are added, not when they're taken away. */
2969 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2970 && TREE_CODE (in_type) == FUNCTION_TYPE)
2971 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2973 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2975 while (TREE_CODE (in_type) == POINTER_TYPE
2976 && TREE_CODE (in_otype) == POINTER_TYPE);
2979 warning ("cast adds new qualifiers to function type");
2982 /* There are qualifiers present in IN_OTYPE that are not
2983 present in IN_TYPE. */
2984 warning ("cast discards qualifiers from pointer target type");
2987 /* Warn about possible alignment problems. */
2988 if (STRICT_ALIGNMENT && warn_cast_align
2989 && TREE_CODE (type) == POINTER_TYPE
2990 && TREE_CODE (otype) == POINTER_TYPE
2991 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
2992 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
2993 /* Don't warn about opaque types, where the actual alignment
2994 restriction is unknown. */
2995 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
2996 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
2997 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
2998 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
2999 warning ("cast increases required alignment of target type");
3001 if (TREE_CODE (type) == INTEGER_TYPE
3002 && TREE_CODE (otype) == POINTER_TYPE
3003 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3004 && !TREE_CONSTANT (value))
3005 warning ("cast from pointer to integer of different size");
3007 if (warn_bad_function_cast
3008 && TREE_CODE (value) == CALL_EXPR
3009 && TREE_CODE (type) != TREE_CODE (otype))
3010 warning ("cast does not match function type");
3012 if (TREE_CODE (type) == POINTER_TYPE
3013 && TREE_CODE (otype) == INTEGER_TYPE
3014 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3015 /* Don't warn about converting any constant. */
3016 && !TREE_CONSTANT (value))
3017 warning ("cast to pointer from integer of different size");
3019 if (TREE_CODE (type) == POINTER_TYPE
3020 && TREE_CODE (otype) == POINTER_TYPE
3021 && TREE_CODE (expr) == ADDR_EXPR
3022 && DECL_P (TREE_OPERAND (expr, 0))
3023 && flag_strict_aliasing && warn_strict_aliasing
3024 && !VOID_TYPE_P (TREE_TYPE (type)))
3026 /* Casting the address of a decl to non void pointer. Warn
3027 if the cast breaks type based aliasing. */
3028 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3029 warning ("type-punning to incomplete type might break strict-aliasing rules");
3032 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3033 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3035 if (!alias_sets_conflict_p (set1, set2))
3036 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3037 else if (warn_strict_aliasing > 1
3038 && !alias_sets_might_conflict_p (set1, set2))
3039 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3043 /* If pedantic, warn for conversions between function and object
3044 pointer types, except for converting a null pointer constant
3045 to function pointer type. */
3047 && TREE_CODE (type) == POINTER_TYPE
3048 && TREE_CODE (otype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3050 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3051 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3054 && TREE_CODE (type) == POINTER_TYPE
3055 && TREE_CODE (otype) == POINTER_TYPE
3056 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3057 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3058 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3059 && TREE_CODE (expr) != NOP_EXPR))
3060 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3063 /* Replace a nonvolatile const static variable with its value. */
3064 if (optimize && TREE_CODE (value) == VAR_DECL)
3065 value = decl_constant_value (value);
3066 value = convert (type, value);
3068 /* Ignore any integer overflow caused by the cast. */
3069 if (TREE_CODE (value) == INTEGER_CST)
3071 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3073 if (TREE_CODE_CLASS (TREE_CODE (ovalue)) == 'c')
3074 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3078 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3079 if (TREE_CODE (value) == INTEGER_CST
3080 && TREE_CODE (expr) == INTEGER_CST
3081 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3082 value = non_lvalue (value);
3084 /* Don't let a cast be an lvalue. */
3086 value = non_lvalue (value);
3091 /* Interpret a cast of expression EXPR to type TYPE. */
3093 c_cast_expr (tree type, tree expr)
3095 int saved_wsp = warn_strict_prototypes;
3097 /* This avoids warnings about unprototyped casts on
3098 integers. E.g. "#define SIG_DFL (void(*)())0". */
3099 if (TREE_CODE (expr) == INTEGER_CST)
3100 warn_strict_prototypes = 0;
3101 type = groktypename (type);
3102 warn_strict_prototypes = saved_wsp;
3104 return build_c_cast (type, expr);
3108 /* Build an assignment expression of lvalue LHS from value RHS.
3109 MODIFYCODE is the code for a binary operator that we use
3110 to combine the old value of LHS with RHS to get the new value.
3111 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3114 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3118 tree lhstype = TREE_TYPE (lhs);
3119 tree olhstype = lhstype;
3121 /* Types that aren't fully specified cannot be used in assignments. */
3122 lhs = require_complete_type (lhs);
3124 /* Avoid duplicate error messages from operands that had errors. */
3125 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3126 return error_mark_node;
3128 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3129 /* Do not use STRIP_NOPS here. We do not want an enumerator
3130 whose value is 0 to count as a null pointer constant. */
3131 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3132 rhs = TREE_OPERAND (rhs, 0);
3136 /* If a binary op has been requested, combine the old LHS value with the RHS
3137 producing the value we should actually store into the LHS. */
3139 if (modifycode != NOP_EXPR)
3141 lhs = stabilize_reference (lhs);
3142 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3145 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3146 return error_mark_node;
3148 /* Warn about storing in something that is `const'. */
3150 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3151 || ((TREE_CODE (lhstype) == RECORD_TYPE
3152 || TREE_CODE (lhstype) == UNION_TYPE)
3153 && C_TYPE_FIELDS_READONLY (lhstype)))
3154 readonly_error (lhs, "assignment");
3156 /* If storing into a structure or union member,
3157 it has probably been given type `int'.
3158 Compute the type that would go with
3159 the actual amount of storage the member occupies. */
3161 if (TREE_CODE (lhs) == COMPONENT_REF
3162 && (TREE_CODE (lhstype) == INTEGER_TYPE
3163 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3164 || TREE_CODE (lhstype) == REAL_TYPE
3165 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3166 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3168 /* If storing in a field that is in actuality a short or narrower than one,
3169 we must store in the field in its actual type. */
3171 if (lhstype != TREE_TYPE (lhs))
3173 lhs = copy_node (lhs);
3174 TREE_TYPE (lhs) = lhstype;
3177 /* Convert new value to destination type. */
3179 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3180 NULL_TREE, NULL_TREE, 0);
3181 if (TREE_CODE (newrhs) == ERROR_MARK)
3182 return error_mark_node;
3186 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3187 TREE_SIDE_EFFECTS (result) = 1;
3189 /* If we got the LHS in a different type for storing in,
3190 convert the result back to the nominal type of LHS
3191 so that the value we return always has the same type
3192 as the LHS argument. */
3194 if (olhstype == TREE_TYPE (result))
3196 return convert_for_assignment (olhstype, result, _("assignment"),
3197 NULL_TREE, NULL_TREE, 0);
3200 /* Convert value RHS to type TYPE as preparation for an assignment
3201 to an lvalue of type TYPE.
3202 The real work of conversion is done by `convert'.
3203 The purpose of this function is to generate error messages
3204 for assignments that are not allowed in C.
3205 ERRTYPE is a string to use in error messages:
3206 "assignment", "return", etc. If it is null, this is parameter passing
3207 for a function call (and different error messages are output).
3209 FUNNAME is the name of the function being called,
3210 as an IDENTIFIER_NODE, or null.
3211 PARMNUM is the number of the argument, for printing in error messages. */
3214 convert_for_assignment (tree type, tree rhs, const char *errtype,
3215 tree fundecl, tree funname, int parmnum)
3217 enum tree_code codel = TREE_CODE (type);
3219 enum tree_code coder;
3221 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3222 /* Do not use STRIP_NOPS here. We do not want an enumerator
3223 whose value is 0 to count as a null pointer constant. */
3224 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3225 rhs = TREE_OPERAND (rhs, 0);
3227 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3228 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3229 rhs = default_conversion (rhs);
3230 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3231 rhs = decl_constant_value_for_broken_optimization (rhs);
3233 rhstype = TREE_TYPE (rhs);
3234 coder = TREE_CODE (rhstype);
3236 if (coder == ERROR_MARK)
3237 return error_mark_node;
3239 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3241 overflow_warning (rhs);
3242 /* Check for Objective-C protocols. This will automatically
3243 issue a warning if there are protocol violations. No need to
3244 use the return value. */
3245 if (c_dialect_objc ())
3246 objc_comptypes (type, rhstype, 0);
3250 if (coder == VOID_TYPE)
3252 error ("void value not ignored as it ought to be");
3253 return error_mark_node;
3255 /* A type converts to a reference to it.
3256 This code doesn't fully support references, it's just for the
3257 special case of va_start and va_copy. */
3258 if (codel == REFERENCE_TYPE
3259 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3261 if (!lvalue_p (rhs))
3263 error ("cannot pass rvalue to reference parameter");
3264 return error_mark_node;
3266 if (!c_mark_addressable (rhs))
3267 return error_mark_node;
3268 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3270 /* We already know that these two types are compatible, but they
3271 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3272 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3273 likely to be va_list, a typedef to __builtin_va_list, which
3274 is different enough that it will cause problems later. */
3275 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3276 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3278 rhs = build1 (NOP_EXPR, type, rhs);
3281 /* Some types can interconvert without explicit casts. */
3282 else if (codel == VECTOR_TYPE
3283 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3284 return convert (type, rhs);
3285 /* Arithmetic types all interconvert, and enum is treated like int. */
3286 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3287 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3288 || codel == BOOLEAN_TYPE)
3289 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3290 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3291 || coder == BOOLEAN_TYPE))
3292 return convert_and_check (type, rhs);
3294 /* Conversion to a transparent union from its member types.
3295 This applies only to function arguments. */
3296 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3299 tree marginal_memb_type = 0;
3301 for (memb_types = TYPE_FIELDS (type); memb_types;
3302 memb_types = TREE_CHAIN (memb_types))
3304 tree memb_type = TREE_TYPE (memb_types);
3306 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3307 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3310 if (TREE_CODE (memb_type) != POINTER_TYPE)
3313 if (coder == POINTER_TYPE)
3315 tree ttl = TREE_TYPE (memb_type);
3316 tree ttr = TREE_TYPE (rhstype);
3318 /* Any non-function converts to a [const][volatile] void *
3319 and vice versa; otherwise, targets must be the same.
3320 Meanwhile, the lhs target must have all the qualifiers of
3322 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3323 || comp_target_types (memb_type, rhstype, 0))
3325 /* If this type won't generate any warnings, use it. */
3326 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3327 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3328 && TREE_CODE (ttl) == FUNCTION_TYPE)
3329 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3330 == TYPE_QUALS (ttr))
3331 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3332 == TYPE_QUALS (ttl))))
3335 /* Keep looking for a better type, but remember this one. */
3336 if (! marginal_memb_type)
3337 marginal_memb_type = memb_type;
3341 /* Can convert integer zero to any pointer type. */
3342 if (integer_zerop (rhs)
3343 || (TREE_CODE (rhs) == NOP_EXPR
3344 && integer_zerop (TREE_OPERAND (rhs, 0))))
3346 rhs = null_pointer_node;
3351 if (memb_types || marginal_memb_type)
3355 /* We have only a marginally acceptable member type;
3356 it needs a warning. */
3357 tree ttl = TREE_TYPE (marginal_memb_type);
3358 tree ttr = TREE_TYPE (rhstype);
3360 /* Const and volatile mean something different for function
3361 types, so the usual warnings are not appropriate. */
3362 if (TREE_CODE (ttr) == FUNCTION_TYPE
3363 && TREE_CODE (ttl) == FUNCTION_TYPE)
3365 /* Because const and volatile on functions are
3366 restrictions that say the function will not do
3367 certain things, it is okay to use a const or volatile
3368 function where an ordinary one is wanted, but not
3370 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3371 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3372 errtype, funname, parmnum);
3374 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3375 warn_for_assignment ("%s discards qualifiers from pointer target type",
3380 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3381 pedwarn ("ISO C prohibits argument conversion to union type");
3383 return build1 (NOP_EXPR, type, rhs);
3387 /* Conversions among pointers */
3388 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3389 && (coder == codel))
3391 tree ttl = TREE_TYPE (type);
3392 tree ttr = TREE_TYPE (rhstype);
3393 bool is_opaque_pointer;
3394 int target_cmp = 0; /* Cache comp_target_types () result. */
3396 /* Opaque pointers are treated like void pointers. */
3397 is_opaque_pointer = (targetm.vector_opaque_p (type)
3398 || targetm.vector_opaque_p (rhstype))
3399 && TREE_CODE (ttl) == VECTOR_TYPE
3400 && TREE_CODE (ttr) == VECTOR_TYPE;
3402 /* Any non-function converts to a [const][volatile] void *
3403 and vice versa; otherwise, targets must be the same.
3404 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3405 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3406 || (target_cmp = comp_target_types (type, rhstype, 0))
3407 || is_opaque_pointer
3408 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3409 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))