1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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. */
30 #include "coretypes.h"
33 #include "langhooks.h"
41 #include "tree-iterator.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
54 /* Whether we are building a boolean conversion inside
55 convert_for_assignment, or some other late binary operation. If
56 build_binary_op is called (from code shared with C++) in this case,
57 then the operands have already been folded and the result will not
58 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
59 bool in_late_binary_op;
61 /* The level of nesting inside "__alignof__". */
64 /* The level of nesting inside "sizeof". */
67 /* The level of nesting inside "typeof". */
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static bool null_pointer_constant_p (const_tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
81 static int comp_target_types (location_t, tree, tree);
82 static int function_types_compatible_p (const_tree, const_tree, bool *,
84 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
85 static tree lookup_field (tree, tree);
86 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
88 static tree pointer_diff (location_t, tree, tree);
89 static tree convert_for_assignment (location_t, tree, tree, tree,
90 enum impl_conv, bool, tree, tree, int);
91 static tree valid_compound_expr_initializer (tree, tree);
92 static void push_string (const char *);
93 static void push_member_name (tree);
94 static int spelling_length (void);
95 static char *print_spelling (char *);
96 static void warning_init (int, const char *);
97 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
98 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
100 static void output_pending_init_elements (int, struct obstack *);
101 static int set_designator (int, struct obstack *);
102 static void push_range_stack (tree, struct obstack *);
103 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
104 static void set_nonincremental_init (struct obstack *);
105 static void set_nonincremental_init_from_string (tree, struct obstack *);
106 static tree find_init_member (tree, struct obstack *);
107 static void readonly_error (tree, enum lvalue_use);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
113 /* Return true if EXP is a null pointer constant, false otherwise. */
116 null_pointer_constant_p (const_tree expr)
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
122 && !TREE_OVERFLOW (expr)
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 /* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
136 note_integer_operands (tree expr)
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
158 remove_c_maybe_const_expr (tree expr)
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
166 \f/* This is a cache to hold if two types are compatible or not. */
168 struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
184 require_complete_type (tree value)
186 tree type = TREE_TYPE (value);
188 if (value == error_mark_node || type == error_mark_node)
189 return error_mark_node;
191 /* First, detect a valid value with a complete type. */
192 if (COMPLETE_TYPE_P (type))
195 c_incomplete_type_error (value, type);
196 return error_mark_node;
199 /* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
204 c_incomplete_type_error (const_tree value, const_tree type)
206 const char *type_code_string;
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
214 error ("%qD has an incomplete type", value);
218 /* We must print an error message. Be clever about what it says. */
220 switch (TREE_CODE (type))
223 type_code_string = "struct";
227 type_code_string = "union";
231 type_code_string = "enum";
235 error ("invalid use of void expression");
239 if (TYPE_DOMAIN (type))
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 error ("invalid use of flexible array member");
246 type = TREE_TYPE (type);
249 error ("invalid use of array with unspecified bounds");
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
269 c_type_promotes_to (tree type)
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
274 if (c_promoting_integer_type_p (type))
276 /* Preserve unsignedness if not really getting any wider. */
277 if (TYPE_UNSIGNED (type)
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
280 return integer_type_node;
286 /* Return true if between two named address spaces, whether there is a superset
287 named address space that encompasses both address spaces. If there is a
288 superset, return which address space is the superset. */
291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
298 else if (targetm.addr_space.subset_p (as1, as2))
303 else if (targetm.addr_space.subset_p (as2, as1))
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313 as well as those of TYPE. */
316 qualify_type (tree type, tree like)
318 addr_space_t as_type = TYPE_ADDR_SPACE (type);
319 addr_space_t as_like = TYPE_ADDR_SPACE (like);
320 addr_space_t as_common;
322 /* If the two named address spaces are different, determine the common
323 superset address space. If there isn't one, raise an error. */
324 if (!addr_space_superset (as_type, as_like, &as_common))
327 error ("%qT and %qT are in disjoint named address spaces",
331 return c_build_qualified_type (type,
332 TYPE_QUALS_NO_ADDR_SPACE (type)
333 | TYPE_QUALS_NO_ADDR_SPACE (like)
334 | ENCODE_QUAL_ADDR_SPACE (as_common));
337 /* Return true iff the given tree T is a variable length array. */
340 c_vla_type_p (const_tree t)
342 if (TREE_CODE (t) == ARRAY_TYPE
343 && C_TYPE_VARIABLE_SIZE (t))
348 /* Return the composite type of two compatible types.
350 We assume that comptypes has already been done and returned
351 nonzero; if that isn't so, this may crash. In particular, we
352 assume that qualifiers match. */
355 composite_type (tree t1, tree t2)
357 enum tree_code code1;
358 enum tree_code code2;
361 /* Save time if the two types are the same. */
363 if (t1 == t2) return t1;
365 /* If one type is nonsense, use the other. */
366 if (t1 == error_mark_node)
368 if (t2 == error_mark_node)
371 code1 = TREE_CODE (t1);
372 code2 = TREE_CODE (t2);
374 /* Merge the attributes. */
375 attributes = targetm.merge_type_attributes (t1, t2);
377 /* If one is an enumerated type and the other is the compatible
378 integer type, the composite type might be either of the two
379 (DR#013 question 3). For consistency, use the enumerated type as
380 the composite type. */
382 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
384 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
387 gcc_assert (code1 == code2);
392 /* For two pointers, do this recursively on the target type. */
394 tree pointed_to_1 = TREE_TYPE (t1);
395 tree pointed_to_2 = TREE_TYPE (t2);
396 tree target = composite_type (pointed_to_1, pointed_to_2);
397 t1 = build_pointer_type (target);
398 t1 = build_type_attribute_variant (t1, attributes);
399 return qualify_type (t1, t2);
404 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
407 tree d1 = TYPE_DOMAIN (t1);
408 tree d2 = TYPE_DOMAIN (t2);
409 bool d1_variable, d2_variable;
410 bool d1_zero, d2_zero;
411 bool t1_complete, t2_complete;
413 /* We should not have any type quals on arrays at all. */
414 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
417 t1_complete = COMPLETE_TYPE_P (t1);
418 t2_complete = COMPLETE_TYPE_P (t2);
420 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423 d1_variable = (!d1_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426 d2_variable = (!d2_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
432 /* Save space: see if the result is identical to one of the args. */
433 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434 && (d2_variable || d2_zero || !d1_variable))
435 return build_type_attribute_variant (t1, attributes);
436 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437 && (d1_variable || d1_zero || !d2_variable))
438 return build_type_attribute_variant (t2, attributes);
440 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441 return build_type_attribute_variant (t1, attributes);
442 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t2, attributes);
445 /* Merge the element types, and have a size if either arg has
446 one. We may have qualifiers on the element types. To set
447 up TYPE_MAIN_VARIANT correctly, we need to form the
448 composite of the unqualified types and add the qualifiers
450 quals = TYPE_QUALS (strip_array_types (elt));
451 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452 t1 = build_array_type (unqual_elt,
453 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
459 /* Ensure a composite type involving a zero-length array type
460 is a zero-length type not an incomplete type. */
461 if (d1_zero && d2_zero
462 && (t1_complete || t2_complete)
463 && !COMPLETE_TYPE_P (t1))
465 TYPE_SIZE (t1) = bitsize_zero_node;
466 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 t1 = c_build_qualified_type (t1, quals);
469 return build_type_attribute_variant (t1, attributes);
475 if (attributes != NULL)
477 /* Try harder not to create a new aggregate type. */
478 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
483 return build_type_attribute_variant (t1, attributes);
486 /* Function types: prefer the one that specified arg types.
487 If both do, merge the arg types. Also merge the return types. */
489 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490 tree p1 = TYPE_ARG_TYPES (t1);
491 tree p2 = TYPE_ARG_TYPES (t2);
496 /* Save space: see if the result is identical to one of the args. */
497 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498 return build_type_attribute_variant (t1, attributes);
499 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500 return build_type_attribute_variant (t2, attributes);
502 /* Simple way if one arg fails to specify argument types. */
503 if (TYPE_ARG_TYPES (t1) == 0)
505 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506 t1 = build_type_attribute_variant (t1, attributes);
507 return qualify_type (t1, t2);
509 if (TYPE_ARG_TYPES (t2) == 0)
511 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512 t1 = build_type_attribute_variant (t1, attributes);
513 return qualify_type (t1, t2);
516 /* If both args specify argument types, we must merge the two
517 lists, argument by argument. */
518 /* Tell global_bindings_p to return false so that variable_size
519 doesn't die on VLAs in parameter types. */
520 c_override_global_bindings_to_false = true;
522 len = list_length (p1);
525 for (i = 0; i < len; i++)
526 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
531 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
533 /* A null type means arg type is not specified.
534 Take whatever the other function type has. */
535 if (TREE_VALUE (p1) == 0)
537 TREE_VALUE (n) = TREE_VALUE (p2);
540 if (TREE_VALUE (p2) == 0)
542 TREE_VALUE (n) = TREE_VALUE (p1);
546 /* Given wait (union {union wait *u; int *i} *)
547 and wait (union wait *),
548 prefer union wait * as type of parm. */
549 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
550 && TREE_VALUE (p1) != TREE_VALUE (p2))
553 tree mv2 = TREE_VALUE (p2);
554 if (mv2 && mv2 != error_mark_node
555 && TREE_CODE (mv2) != ARRAY_TYPE)
556 mv2 = TYPE_MAIN_VARIANT (mv2);
557 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
558 memb; memb = DECL_CHAIN (memb))
560 tree mv3 = TREE_TYPE (memb);
561 if (mv3 && mv3 != error_mark_node
562 && TREE_CODE (mv3) != ARRAY_TYPE)
563 mv3 = TYPE_MAIN_VARIANT (mv3);
564 if (comptypes (mv3, mv2))
566 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
568 pedwarn (input_location, OPT_pedantic,
569 "function types not truly compatible in ISO C");
574 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
575 && TREE_VALUE (p2) != TREE_VALUE (p1))
578 tree mv1 = TREE_VALUE (p1);
579 if (mv1 && mv1 != error_mark_node
580 && TREE_CODE (mv1) != ARRAY_TYPE)
581 mv1 = TYPE_MAIN_VARIANT (mv1);
582 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
583 memb; memb = DECL_CHAIN (memb))
585 tree mv3 = TREE_TYPE (memb);
586 if (mv3 && mv3 != error_mark_node
587 && TREE_CODE (mv3) != ARRAY_TYPE)
588 mv3 = TYPE_MAIN_VARIANT (mv3);
589 if (comptypes (mv3, mv1))
591 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
593 pedwarn (input_location, OPT_pedantic,
594 "function types not truly compatible in ISO C");
599 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
603 c_override_global_bindings_to_false = false;
604 t1 = build_function_type (valtype, newargs);
605 t1 = qualify_type (t1, t2);
606 /* ... falls through ... */
610 return build_type_attribute_variant (t1, attributes);
615 /* Return the type of a conditional expression between pointers to
616 possibly differently qualified versions of compatible types.
618 We assume that comp_target_types has already been done and returned
619 nonzero; if that isn't so, this may crash. */
622 common_pointer_type (tree t1, tree t2)
625 tree pointed_to_1, mv1;
626 tree pointed_to_2, mv2;
628 unsigned target_quals;
629 addr_space_t as1, as2, as_common;
632 /* Save time if the two types are the same. */
634 if (t1 == t2) return t1;
636 /* If one type is nonsense, use the other. */
637 if (t1 == error_mark_node)
639 if (t2 == error_mark_node)
642 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
643 && TREE_CODE (t2) == POINTER_TYPE);
645 /* Merge the attributes. */
646 attributes = targetm.merge_type_attributes (t1, t2);
648 /* Find the composite type of the target types, and combine the
649 qualifiers of the two types' targets. Do not lose qualifiers on
650 array element types by taking the TYPE_MAIN_VARIANT. */
651 mv1 = pointed_to_1 = TREE_TYPE (t1);
652 mv2 = pointed_to_2 = TREE_TYPE (t2);
653 if (TREE_CODE (mv1) != ARRAY_TYPE)
654 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
655 if (TREE_CODE (mv2) != ARRAY_TYPE)
656 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
657 target = composite_type (mv1, mv2);
659 /* For function types do not merge const qualifiers, but drop them
660 if used inconsistently. The middle-end uses these to mark const
661 and noreturn functions. */
662 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
663 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
665 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
666 target_quals = (quals1 & quals2);
668 target_quals = (quals1 | quals2);
670 /* If the two named address spaces are different, determine the common
671 superset address space. This is guaranteed to exist due to the
672 assumption that comp_target_type returned non-zero. */
673 as1 = TYPE_ADDR_SPACE (pointed_to_1);
674 as2 = TYPE_ADDR_SPACE (pointed_to_2);
675 if (!addr_space_superset (as1, as2, &as_common))
678 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
680 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
681 return build_type_attribute_variant (t1, attributes);
684 /* Return the common type for two arithmetic types under the usual
685 arithmetic conversions. The default conversions have already been
686 applied, and enumerated types converted to their compatible integer
687 types. The resulting type is unqualified and has no attributes.
689 This is the type for the result of most arithmetic operations
690 if the operands have the given two types. */
693 c_common_type (tree t1, tree t2)
695 enum tree_code code1;
696 enum tree_code code2;
698 /* If one type is nonsense, use the other. */
699 if (t1 == error_mark_node)
701 if (t2 == error_mark_node)
704 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
705 t1 = TYPE_MAIN_VARIANT (t1);
707 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
708 t2 = TYPE_MAIN_VARIANT (t2);
710 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
711 t1 = build_type_attribute_variant (t1, NULL_TREE);
713 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
714 t2 = build_type_attribute_variant (t2, NULL_TREE);
716 /* Save time if the two types are the same. */
718 if (t1 == t2) return t1;
720 code1 = TREE_CODE (t1);
721 code2 = TREE_CODE (t2);
723 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
724 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
725 || code1 == INTEGER_TYPE);
726 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
727 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
728 || code2 == INTEGER_TYPE);
730 /* When one operand is a decimal float type, the other operand cannot be
731 a generic float type or a complex type. We also disallow vector types
733 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
734 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
736 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
738 error ("can%'t mix operands of decimal float and vector types");
739 return error_mark_node;
741 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
743 error ("can%'t mix operands of decimal float and complex types");
744 return error_mark_node;
746 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
748 error ("can%'t mix operands of decimal float and other float types");
749 return error_mark_node;
753 /* If one type is a vector type, return that type. (How the usual
754 arithmetic conversions apply to the vector types extension is not
755 precisely specified.) */
756 if (code1 == VECTOR_TYPE)
759 if (code2 == VECTOR_TYPE)
762 /* If one type is complex, form the common type of the non-complex
763 components, then make that complex. Use T1 or T2 if it is the
765 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
767 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
768 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
769 tree subtype = c_common_type (subtype1, subtype2);
771 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
773 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
776 return build_complex_type (subtype);
779 /* If only one is real, use it as the result. */
781 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
784 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
787 /* If both are real and either are decimal floating point types, use
788 the decimal floating point type with the greater precision. */
790 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
792 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
793 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
794 return dfloat128_type_node;
795 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
797 return dfloat64_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
800 return dfloat32_type_node;
803 /* Deal with fixed-point types. */
804 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
806 unsigned int unsignedp = 0, satp = 0;
807 enum machine_mode m1, m2;
808 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
813 /* If one input type is saturating, the result type is saturating. */
814 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
817 /* If both fixed-point types are unsigned, the result type is unsigned.
818 When mixing fixed-point and integer types, follow the sign of the
820 Otherwise, the result type is signed. */
821 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
822 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
823 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t1))
825 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t2)))
829 /* The result type is signed. */
832 /* If the input type is unsigned, we need to convert to the
834 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
836 enum mode_class mclass = (enum mode_class) 0;
837 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
839 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
843 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
845 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
847 enum mode_class mclass = (enum mode_class) 0;
848 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
850 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
854 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
858 if (code1 == FIXED_POINT_TYPE)
860 fbit1 = GET_MODE_FBIT (m1);
861 ibit1 = GET_MODE_IBIT (m1);
866 /* Signed integers need to subtract one sign bit. */
867 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
870 if (code2 == FIXED_POINT_TYPE)
872 fbit2 = GET_MODE_FBIT (m2);
873 ibit2 = GET_MODE_IBIT (m2);
878 /* Signed integers need to subtract one sign bit. */
879 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
882 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
883 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
884 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
888 /* Both real or both integers; use the one with greater precision. */
890 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
892 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
895 /* Same precision. Prefer long longs to longs to ints when the
896 same precision, following the C99 rules on integer type rank
897 (which are equivalent to the C90 rules for C90 types). */
899 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
901 return long_long_unsigned_type_node;
903 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
906 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
907 return long_long_unsigned_type_node;
909 return long_long_integer_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
914 return long_unsigned_type_node;
916 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
919 /* But preserve unsignedness from the other type,
920 since long cannot hold all the values of an unsigned int. */
921 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
922 return long_unsigned_type_node;
924 return long_integer_type_node;
927 /* Likewise, prefer long double to double even if same size. */
928 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
929 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
930 return long_double_type_node;
932 /* Otherwise prefer the unsigned one. */
934 if (TYPE_UNSIGNED (t1))
940 /* Wrapper around c_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions. ENUMERAL_TYPEs
942 are allowed here and are converted to their compatible integer types.
943 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
944 preferably a non-Boolean type as the common type. */
946 common_type (tree t1, tree t2)
948 if (TREE_CODE (t1) == ENUMERAL_TYPE)
949 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
950 if (TREE_CODE (t2) == ENUMERAL_TYPE)
951 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
953 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
954 if (TREE_CODE (t1) == BOOLEAN_TYPE
955 && TREE_CODE (t2) == BOOLEAN_TYPE)
956 return boolean_type_node;
958 /* If either type is BOOLEAN_TYPE, then return the other. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE)
961 if (TREE_CODE (t2) == BOOLEAN_TYPE)
964 return c_common_type (t1, t2);
967 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
968 or various other operations. Return 2 if they are compatible
969 but a warning may be needed if you use them together. */
972 comptypes (tree type1, tree type2)
974 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
977 val = comptypes_internal (type1, type2, NULL, NULL);
978 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
983 /* Like comptypes, but if it returns non-zero because enum and int are
984 compatible, it sets *ENUM_AND_INT_P to true. */
987 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
989 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
992 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
998 /* Like comptypes, but if it returns nonzero for different types, it
999 sets *DIFFERENT_TYPES_P to true. */
1002 comptypes_check_different_types (tree type1, tree type2,
1003 bool *different_types_p)
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1008 val = comptypes_internal (type1, type2, NULL, different_types_p);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1014 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1015 or various other operations. Return 2 if they are compatible
1016 but a warning may be needed if you use them together. If
1017 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1018 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1019 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1020 NULL, and the types are compatible but different enough not to be
1021 permitted in C1X typedef redeclarations, then this sets
1022 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1023 false, but may or may not be set if the types are incompatible.
1024 This differs from comptypes, in that we don't free the seen
1028 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1029 bool *different_types_p)
1031 const_tree t1 = type1;
1032 const_tree t2 = type2;
1035 /* Suppress errors caused by previously reported errors. */
1037 if (t1 == t2 || !t1 || !t2
1038 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1041 /* Enumerated types are compatible with integer types, but this is
1042 not transitive: two enumerated types in the same translation unit
1043 are compatible with each other only if they are the same type. */
1045 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1047 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1048 if (TREE_CODE (t2) != VOID_TYPE)
1050 if (enum_and_int_p != NULL)
1051 *enum_and_int_p = true;
1052 if (different_types_p != NULL)
1053 *different_types_p = true;
1056 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1058 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1059 if (TREE_CODE (t1) != VOID_TYPE)
1061 if (enum_and_int_p != NULL)
1062 *enum_and_int_p = true;
1063 if (different_types_p != NULL)
1064 *different_types_p = true;
1071 /* Different classes of types can't be compatible. */
1073 if (TREE_CODE (t1) != TREE_CODE (t2))
1076 /* Qualifiers must match. C99 6.7.3p9 */
1078 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1081 /* Allow for two different type nodes which have essentially the same
1082 definition. Note that we already checked for equality of the type
1083 qualifiers (just above). */
1085 if (TREE_CODE (t1) != ARRAY_TYPE
1086 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1090 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1093 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1096 switch (TREE_CODE (t1))
1099 /* Do not remove mode or aliasing information. */
1100 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1101 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1103 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1104 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1105 enum_and_int_p, different_types_p));
1109 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1115 tree d1 = TYPE_DOMAIN (t1);
1116 tree d2 = TYPE_DOMAIN (t2);
1117 bool d1_variable, d2_variable;
1118 bool d1_zero, d2_zero;
1121 /* Target types must match incl. qualifiers. */
1122 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1123 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1125 different_types_p)))
1128 if (different_types_p != NULL
1129 && (d1 == 0) != (d2 == 0))
1130 *different_types_p = true;
1131 /* Sizes must match unless one is missing or variable. */
1132 if (d1 == 0 || d2 == 0 || d1 == d2)
1135 d1_zero = !TYPE_MAX_VALUE (d1);
1136 d2_zero = !TYPE_MAX_VALUE (d2);
1138 d1_variable = (!d1_zero
1139 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1140 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1141 d2_variable = (!d2_zero
1142 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1143 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1144 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1145 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1147 if (different_types_p != NULL
1148 && d1_variable != d2_variable)
1149 *different_types_p = true;
1150 if (d1_variable || d2_variable)
1152 if (d1_zero && d2_zero)
1154 if (d1_zero || d2_zero
1155 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1156 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1165 if (val != 1 && !same_translation_unit_p (t1, t2))
1167 tree a1 = TYPE_ATTRIBUTES (t1);
1168 tree a2 = TYPE_ATTRIBUTES (t2);
1170 if (! attribute_list_contained (a1, a2)
1171 && ! attribute_list_contained (a2, a1))
1175 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1177 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1183 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1184 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1185 enum_and_int_p, different_types_p));
1191 return attrval == 2 && val == 1 ? 2 : val;
1194 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1195 their qualifiers, except for named address spaces. If the pointers point to
1196 different named addresses, then we must determine if one address space is a
1197 subset of the other. */
1200 comp_target_types (location_t location, tree ttl, tree ttr)
1203 tree mvl = TREE_TYPE (ttl);
1204 tree mvr = TREE_TYPE (ttr);
1205 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1206 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1207 addr_space_t as_common;
1208 bool enum_and_int_p;
1210 /* Fail if pointers point to incompatible address spaces. */
1211 if (!addr_space_superset (asl, asr, &as_common))
1214 /* Do not lose qualifiers on element types of array types that are
1215 pointer targets by taking their TYPE_MAIN_VARIANT. */
1216 if (TREE_CODE (mvl) != ARRAY_TYPE)
1217 mvl = TYPE_MAIN_VARIANT (mvl);
1218 if (TREE_CODE (mvr) != ARRAY_TYPE)
1219 mvr = TYPE_MAIN_VARIANT (mvr);
1220 enum_and_int_p = false;
1221 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1224 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1226 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1227 warning_at (location, OPT_Wc___compat,
1228 "pointer target types incompatible in C++");
1233 /* Subroutines of `comptypes'. */
1235 /* Determine whether two trees derive from the same translation unit.
1236 If the CONTEXT chain ends in a null, that tree's context is still
1237 being parsed, so if two trees have context chains ending in null,
1238 they're in the same translation unit. */
1240 same_translation_unit_p (const_tree t1, const_tree t2)
1242 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1243 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1245 case tcc_declaration:
1246 t1 = DECL_CONTEXT (t1); break;
1248 t1 = TYPE_CONTEXT (t1); break;
1249 case tcc_exceptional:
1250 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1251 default: gcc_unreachable ();
1254 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1255 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1257 case tcc_declaration:
1258 t2 = DECL_CONTEXT (t2); break;
1260 t2 = TYPE_CONTEXT (t2); break;
1261 case tcc_exceptional:
1262 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1263 default: gcc_unreachable ();
1269 /* Allocate the seen two types, assuming that they are compatible. */
1271 static struct tagged_tu_seen_cache *
1272 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1274 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1275 tu->next = tagged_tu_seen_base;
1279 tagged_tu_seen_base = tu;
1281 /* The C standard says that two structures in different translation
1282 units are compatible with each other only if the types of their
1283 fields are compatible (among other things). We assume that they
1284 are compatible until proven otherwise when building the cache.
1285 An example where this can occur is:
1290 If we are comparing this against a similar struct in another TU,
1291 and did not assume they were compatible, we end up with an infinite
1297 /* Free the seen types until we get to TU_TIL. */
1300 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1302 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1303 while (tu != tu_til)
1305 const struct tagged_tu_seen_cache *const tu1
1306 = (const struct tagged_tu_seen_cache *) tu;
1308 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1310 tagged_tu_seen_base = tu_til;
1313 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1314 compatible. If the two types are not the same (which has been
1315 checked earlier), this can only happen when multiple translation
1316 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1317 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1318 comptypes_internal. */
1321 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1322 bool *enum_and_int_p, bool *different_types_p)
1325 bool needs_warning = false;
1327 /* We have to verify that the tags of the types are the same. This
1328 is harder than it looks because this may be a typedef, so we have
1329 to go look at the original type. It may even be a typedef of a
1331 In the case of compiler-created builtin structs the TYPE_DECL
1332 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1333 while (TYPE_NAME (t1)
1334 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1335 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1336 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1338 while (TYPE_NAME (t2)
1339 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1340 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1341 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1343 /* C90 didn't have the requirement that the two tags be the same. */
1344 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1347 /* C90 didn't say what happened if one or both of the types were
1348 incomplete; we choose to follow C99 rules here, which is that they
1350 if (TYPE_SIZE (t1) == NULL
1351 || TYPE_SIZE (t2) == NULL)
1355 const struct tagged_tu_seen_cache * tts_i;
1356 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1357 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1361 switch (TREE_CODE (t1))
1365 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1366 /* Speed up the case where the type values are in the same order. */
1367 tree tv1 = TYPE_VALUES (t1);
1368 tree tv2 = TYPE_VALUES (t2);
1375 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1377 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1379 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1386 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1390 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1396 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1402 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1404 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1406 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1417 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1418 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1424 /* Speed up the common case where the fields are in the same order. */
1425 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1426 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1430 if (DECL_NAME (s1) != DECL_NAME (s2))
1432 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1433 enum_and_int_p, different_types_p);
1435 if (result != 1 && !DECL_NAME (s1))
1443 needs_warning = true;
1445 if (TREE_CODE (s1) == FIELD_DECL
1446 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1447 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1455 tu->val = needs_warning ? 2 : 1;
1459 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1463 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1464 if (DECL_NAME (s1) == DECL_NAME (s2))
1468 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1472 if (result != 1 && !DECL_NAME (s1))
1480 needs_warning = true;
1482 if (TREE_CODE (s1) == FIELD_DECL
1483 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1484 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1496 tu->val = needs_warning ? 2 : 10;
1502 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1504 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1506 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1509 if (TREE_CODE (s1) != TREE_CODE (s2)
1510 || DECL_NAME (s1) != DECL_NAME (s2))
1512 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1513 enum_and_int_p, different_types_p);
1517 needs_warning = true;
1519 if (TREE_CODE (s1) == FIELD_DECL
1520 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1521 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1527 tu->val = needs_warning ? 2 : 1;
1536 /* Return 1 if two function types F1 and F2 are compatible.
1537 If either type specifies no argument types,
1538 the other must specify a fixed number of self-promoting arg types.
1539 Otherwise, if one type specifies only the number of arguments,
1540 the other must specify that number of self-promoting arg types.
1541 Otherwise, the argument types must match.
1542 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1545 function_types_compatible_p (const_tree f1, const_tree f2,
1546 bool *enum_and_int_p, bool *different_types_p)
1549 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1554 ret1 = TREE_TYPE (f1);
1555 ret2 = TREE_TYPE (f2);
1557 /* 'volatile' qualifiers on a function's return type used to mean
1558 the function is noreturn. */
1559 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1560 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1561 if (TYPE_VOLATILE (ret1))
1562 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1563 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1564 if (TYPE_VOLATILE (ret2))
1565 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1566 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1567 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1571 args1 = TYPE_ARG_TYPES (f1);
1572 args2 = TYPE_ARG_TYPES (f2);
1574 if (different_types_p != NULL
1575 && (args1 == 0) != (args2 == 0))
1576 *different_types_p = true;
1578 /* An unspecified parmlist matches any specified parmlist
1579 whose argument types don't need default promotions. */
1583 if (!self_promoting_args_p (args2))
1585 /* If one of these types comes from a non-prototype fn definition,
1586 compare that with the other type's arglist.
1587 If they don't match, ask for a warning (but no error). */
1588 if (TYPE_ACTUAL_ARG_TYPES (f1)
1589 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1590 enum_and_int_p, different_types_p))
1596 if (!self_promoting_args_p (args1))
1598 if (TYPE_ACTUAL_ARG_TYPES (f2)
1599 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1600 enum_and_int_p, different_types_p))
1605 /* Both types have argument lists: compare them and propagate results. */
1606 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1608 return val1 != 1 ? val1 : val;
1611 /* Check two lists of types for compatibility, returning 0 for
1612 incompatible, 1 for compatible, or 2 for compatible with
1613 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1614 comptypes_internal. */
1617 type_lists_compatible_p (const_tree args1, const_tree args2,
1618 bool *enum_and_int_p, bool *different_types_p)
1620 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1626 tree a1, mv1, a2, mv2;
1627 if (args1 == 0 && args2 == 0)
1629 /* If one list is shorter than the other,
1630 they fail to match. */
1631 if (args1 == 0 || args2 == 0)
1633 mv1 = a1 = TREE_VALUE (args1);
1634 mv2 = a2 = TREE_VALUE (args2);
1635 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1636 mv1 = TYPE_MAIN_VARIANT (mv1);
1637 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1638 mv2 = TYPE_MAIN_VARIANT (mv2);
1639 /* A null pointer instead of a type
1640 means there is supposed to be an argument
1641 but nothing is specified about what type it has.
1642 So match anything that self-promotes. */
1643 if (different_types_p != NULL
1644 && (a1 == 0) != (a2 == 0))
1645 *different_types_p = true;
1648 if (c_type_promotes_to (a2) != a2)
1653 if (c_type_promotes_to (a1) != a1)
1656 /* If one of the lists has an error marker, ignore this arg. */
1657 else if (TREE_CODE (a1) == ERROR_MARK
1658 || TREE_CODE (a2) == ERROR_MARK)
1660 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1661 different_types_p)))
1663 if (different_types_p != NULL)
1664 *different_types_p = true;
1665 /* Allow wait (union {union wait *u; int *i} *)
1666 and wait (union wait *) to be compatible. */
1667 if (TREE_CODE (a1) == UNION_TYPE
1668 && (TYPE_NAME (a1) == 0
1669 || TYPE_TRANSPARENT_AGGR (a1))
1670 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1671 && tree_int_cst_equal (TYPE_SIZE (a1),
1675 for (memb = TYPE_FIELDS (a1);
1676 memb; memb = DECL_CHAIN (memb))
1678 tree mv3 = TREE_TYPE (memb);
1679 if (mv3 && mv3 != error_mark_node
1680 && TREE_CODE (mv3) != ARRAY_TYPE)
1681 mv3 = TYPE_MAIN_VARIANT (mv3);
1682 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1689 else if (TREE_CODE (a2) == UNION_TYPE
1690 && (TYPE_NAME (a2) == 0
1691 || TYPE_TRANSPARENT_AGGR (a2))
1692 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1693 && tree_int_cst_equal (TYPE_SIZE (a2),
1697 for (memb = TYPE_FIELDS (a2);
1698 memb; memb = DECL_CHAIN (memb))
1700 tree mv3 = TREE_TYPE (memb);
1701 if (mv3 && mv3 != error_mark_node
1702 && TREE_CODE (mv3) != ARRAY_TYPE)
1703 mv3 = TYPE_MAIN_VARIANT (mv3);
1704 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1715 /* comptypes said ok, but record if it said to warn. */
1719 args1 = TREE_CHAIN (args1);
1720 args2 = TREE_CHAIN (args2);
1724 /* Compute the size to increment a pointer by. */
1727 c_size_in_bytes (const_tree type)
1729 enum tree_code code = TREE_CODE (type);
1731 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1732 return size_one_node;
1734 if (!COMPLETE_OR_VOID_TYPE_P (type))
1736 error ("arithmetic on pointer to an incomplete type");
1737 return size_one_node;
1740 /* Convert in case a char is more than one unit. */
1741 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1742 size_int (TYPE_PRECISION (char_type_node)
1746 /* Return either DECL or its known constant value (if it has one). */
1749 decl_constant_value (tree decl)
1751 if (/* Don't change a variable array bound or initial value to a constant
1752 in a place where a variable is invalid. Note that DECL_INITIAL
1753 isn't valid for a PARM_DECL. */
1754 current_function_decl != 0
1755 && TREE_CODE (decl) != PARM_DECL
1756 && !TREE_THIS_VOLATILE (decl)
1757 && TREE_READONLY (decl)
1758 && DECL_INITIAL (decl) != 0
1759 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1760 /* This is invalid if initial value is not constant.
1761 If it has either a function call, a memory reference,
1762 or a variable, then re-evaluating it could give different results. */
1763 && TREE_CONSTANT (DECL_INITIAL (decl))
1764 /* Check for cases where this is sub-optimal, even though valid. */
1765 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1766 return DECL_INITIAL (decl);
1770 /* Convert the array expression EXP to a pointer. */
1772 array_to_pointer_conversion (location_t loc, tree exp)
1774 tree orig_exp = exp;
1775 tree type = TREE_TYPE (exp);
1777 tree restype = TREE_TYPE (type);
1780 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1782 STRIP_TYPE_NOPS (exp);
1784 if (TREE_NO_WARNING (orig_exp))
1785 TREE_NO_WARNING (exp) = 1;
1787 ptrtype = build_pointer_type (restype);
1789 if (TREE_CODE (exp) == INDIRECT_REF)
1790 return convert (ptrtype, TREE_OPERAND (exp, 0));
1792 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1793 return convert (ptrtype, adr);
1796 /* Convert the function expression EXP to a pointer. */
1798 function_to_pointer_conversion (location_t loc, tree exp)
1800 tree orig_exp = exp;
1802 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1804 STRIP_TYPE_NOPS (exp);
1806 if (TREE_NO_WARNING (orig_exp))
1807 TREE_NO_WARNING (exp) = 1;
1809 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1812 /* Mark EXP as read, not just set, for set but not used -Wunused
1813 warning purposes. */
1816 mark_exp_read (tree exp)
1818 switch (TREE_CODE (exp))
1822 DECL_READ_P (exp) = 1;
1831 mark_exp_read (TREE_OPERAND (exp, 0));
1834 case C_MAYBE_CONST_EXPR:
1835 mark_exp_read (TREE_OPERAND (exp, 1));
1842 /* Perform the default conversion of arrays and functions to pointers.
1843 Return the result of converting EXP. For any other expression, just
1846 LOC is the location of the expression. */
1849 default_function_array_conversion (location_t loc, struct c_expr exp)
1851 tree orig_exp = exp.value;
1852 tree type = TREE_TYPE (exp.value);
1853 enum tree_code code = TREE_CODE (type);
1859 bool not_lvalue = false;
1860 bool lvalue_array_p;
1862 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1863 || CONVERT_EXPR_P (exp.value))
1864 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1866 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1868 exp.value = TREE_OPERAND (exp.value, 0);
1871 if (TREE_NO_WARNING (orig_exp))
1872 TREE_NO_WARNING (exp.value) = 1;
1874 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1875 if (!flag_isoc99 && !lvalue_array_p)
1877 /* Before C99, non-lvalue arrays do not decay to pointers.
1878 Normally, using such an array would be invalid; but it can
1879 be used correctly inside sizeof or as a statement expression.
1880 Thus, do not give an error here; an error will result later. */
1884 exp.value = array_to_pointer_conversion (loc, exp.value);
1888 exp.value = function_to_pointer_conversion (loc, exp.value);
1898 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1900 mark_exp_read (exp.value);
1901 return default_function_array_conversion (loc, exp);
1904 /* EXP is an expression of integer type. Apply the integer promotions
1905 to it and return the promoted value. */
1908 perform_integral_promotions (tree exp)
1910 tree type = TREE_TYPE (exp);
1911 enum tree_code code = TREE_CODE (type);
1913 gcc_assert (INTEGRAL_TYPE_P (type));
1915 /* Normally convert enums to int,
1916 but convert wide enums to something wider. */
1917 if (code == ENUMERAL_TYPE)
1919 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1920 TYPE_PRECISION (integer_type_node)),
1921 ((TYPE_PRECISION (type)
1922 >= TYPE_PRECISION (integer_type_node))
1923 && TYPE_UNSIGNED (type)));
1925 return convert (type, exp);
1928 /* ??? This should no longer be needed now bit-fields have their
1930 if (TREE_CODE (exp) == COMPONENT_REF
1931 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1932 /* If it's thinner than an int, promote it like a
1933 c_promoting_integer_type_p, otherwise leave it alone. */
1934 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1935 TYPE_PRECISION (integer_type_node)))
1936 return convert (integer_type_node, exp);
1938 if (c_promoting_integer_type_p (type))
1940 /* Preserve unsignedness if not really getting any wider. */
1941 if (TYPE_UNSIGNED (type)
1942 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1943 return convert (unsigned_type_node, exp);
1945 return convert (integer_type_node, exp);
1952 /* Perform default promotions for C data used in expressions.
1953 Enumeral types or short or char are converted to int.
1954 In addition, manifest constants symbols are replaced by their values. */
1957 default_conversion (tree exp)
1960 tree type = TREE_TYPE (exp);
1961 enum tree_code code = TREE_CODE (type);
1964 mark_exp_read (exp);
1966 /* Functions and arrays have been converted during parsing. */
1967 gcc_assert (code != FUNCTION_TYPE);
1968 if (code == ARRAY_TYPE)
1971 /* Constants can be used directly unless they're not loadable. */
1972 if (TREE_CODE (exp) == CONST_DECL)
1973 exp = DECL_INITIAL (exp);
1975 /* Strip no-op conversions. */
1977 STRIP_TYPE_NOPS (exp);
1979 if (TREE_NO_WARNING (orig_exp))
1980 TREE_NO_WARNING (exp) = 1;
1982 if (code == VOID_TYPE)
1984 error ("void value not ignored as it ought to be");
1985 return error_mark_node;
1988 exp = require_complete_type (exp);
1989 if (exp == error_mark_node)
1990 return error_mark_node;
1992 promoted_type = targetm.promoted_type (type);
1994 return convert (promoted_type, exp);
1996 if (INTEGRAL_TYPE_P (type))
1997 return perform_integral_promotions (exp);
2002 /* Look up COMPONENT in a structure or union TYPE.
2004 If the component name is not found, returns NULL_TREE. Otherwise,
2005 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2006 stepping down the chain to the component, which is in the last
2007 TREE_VALUE of the list. Normally the list is of length one, but if
2008 the component is embedded within (nested) anonymous structures or
2009 unions, the list steps down the chain to the component. */
2012 lookup_field (tree type, tree component)
2016 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2017 to the field elements. Use a binary search on this array to quickly
2018 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2019 will always be set for structures which have many elements. */
2021 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2024 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2026 field = TYPE_FIELDS (type);
2028 top = TYPE_LANG_SPECIFIC (type)->s->len;
2029 while (top - bot > 1)
2031 half = (top - bot + 1) >> 1;
2032 field = field_array[bot+half];
2034 if (DECL_NAME (field) == NULL_TREE)
2036 /* Step through all anon unions in linear fashion. */
2037 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2039 field = field_array[bot++];
2040 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2041 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2043 tree anon = lookup_field (TREE_TYPE (field), component);
2046 return tree_cons (NULL_TREE, field, anon);
2048 /* The Plan 9 compiler permits referring
2049 directly to an anonymous struct/union field
2050 using a typedef name. */
2051 if (flag_plan9_extensions
2052 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2053 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2055 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2061 /* Entire record is only anon unions. */
2065 /* Restart the binary search, with new lower bound. */
2069 if (DECL_NAME (field) == component)
2071 if (DECL_NAME (field) < component)
2077 if (DECL_NAME (field_array[bot]) == component)
2078 field = field_array[bot];
2079 else if (DECL_NAME (field) != component)
2084 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2086 if (DECL_NAME (field) == NULL_TREE
2087 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2088 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2090 tree anon = lookup_field (TREE_TYPE (field), component);
2093 return tree_cons (NULL_TREE, field, anon);
2095 /* The Plan 9 compiler permits referring directly to an
2096 anonymous struct/union field using a typedef
2098 if (flag_plan9_extensions
2099 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2100 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2101 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2106 if (DECL_NAME (field) == component)
2110 if (field == NULL_TREE)
2114 return tree_cons (NULL_TREE, field, NULL_TREE);
2117 /* Make an expression to refer to the COMPONENT field of structure or
2118 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2119 location of the COMPONENT_REF. */
2122 build_component_ref (location_t loc, tree datum, tree component)
2124 tree type = TREE_TYPE (datum);
2125 enum tree_code code = TREE_CODE (type);
2128 bool datum_lvalue = lvalue_p (datum);
2130 if (!objc_is_public (datum, component))
2131 return error_mark_node;
2133 /* Detect Objective-C property syntax object.property. */
2134 if (c_dialect_objc ()
2135 && (ref = objc_maybe_build_component_ref (datum, component)))
2138 /* See if there is a field or component with name COMPONENT. */
2140 if (code == RECORD_TYPE || code == UNION_TYPE)
2142 if (!COMPLETE_TYPE_P (type))
2144 c_incomplete_type_error (NULL_TREE, type);
2145 return error_mark_node;
2148 field = lookup_field (type, component);
2152 error_at (loc, "%qT has no member named %qE", type, component);
2153 return error_mark_node;
2156 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2157 This might be better solved in future the way the C++ front
2158 end does it - by giving the anonymous entities each a
2159 separate name and type, and then have build_component_ref
2160 recursively call itself. We can't do that here. */
2163 tree subdatum = TREE_VALUE (field);
2166 bool use_datum_quals;
2168 if (TREE_TYPE (subdatum) == error_mark_node)
2169 return error_mark_node;
2171 /* If this is an rvalue, it does not have qualifiers in C
2172 standard terms and we must avoid propagating such
2173 qualifiers down to a non-lvalue array that is then
2174 converted to a pointer. */
2175 use_datum_quals = (datum_lvalue
2176 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2178 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2179 if (use_datum_quals)
2180 quals |= TYPE_QUALS (TREE_TYPE (datum));
2181 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2183 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2185 SET_EXPR_LOCATION (ref, loc);
2186 if (TREE_READONLY (subdatum)
2187 || (use_datum_quals && TREE_READONLY (datum)))
2188 TREE_READONLY (ref) = 1;
2189 if (TREE_THIS_VOLATILE (subdatum)
2190 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2191 TREE_THIS_VOLATILE (ref) = 1;
2193 if (TREE_DEPRECATED (subdatum))
2194 warn_deprecated_use (subdatum, NULL_TREE);
2198 field = TREE_CHAIN (field);
2204 else if (code != ERROR_MARK)
2206 "request for member %qE in something not a structure or union",
2209 return error_mark_node;
2212 /* Given an expression PTR for a pointer, return an expression
2213 for the value pointed to.
2214 ERRORSTRING is the name of the operator to appear in error messages.
2216 LOC is the location to use for the generated tree. */
2219 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2221 tree pointer = default_conversion (ptr);
2222 tree type = TREE_TYPE (pointer);
2225 if (TREE_CODE (type) == POINTER_TYPE)
2227 if (CONVERT_EXPR_P (pointer)
2228 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2230 /* If a warning is issued, mark it to avoid duplicates from
2231 the backend. This only needs to be done at
2232 warn_strict_aliasing > 2. */
2233 if (warn_strict_aliasing > 2)
2234 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2235 type, TREE_OPERAND (pointer, 0)))
2236 TREE_NO_WARNING (pointer) = 1;
2239 if (TREE_CODE (pointer) == ADDR_EXPR
2240 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2241 == TREE_TYPE (type)))
2243 ref = TREE_OPERAND (pointer, 0);
2244 protected_set_expr_location (ref, loc);
2249 tree t = TREE_TYPE (type);
2251 ref = build1 (INDIRECT_REF, t, pointer);
2253 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2255 error_at (loc, "dereferencing pointer to incomplete type");
2256 return error_mark_node;
2258 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2259 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2261 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2262 so that we get the proper error message if the result is used
2263 to assign to. Also, &* is supposed to be a no-op.
2264 And ANSI C seems to specify that the type of the result
2265 should be the const type. */
2266 /* A de-reference of a pointer to const is not a const. It is valid
2267 to change it via some other pointer. */
2268 TREE_READONLY (ref) = TYPE_READONLY (t);
2269 TREE_SIDE_EFFECTS (ref)
2270 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2271 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2272 protected_set_expr_location (ref, loc);
2276 else if (TREE_CODE (pointer) != ERROR_MARK)
2279 case RO_ARRAY_INDEXING:
2281 "invalid type argument of array indexing (have %qT)",
2286 "invalid type argument of unary %<*%> (have %qT)",
2291 "invalid type argument of %<->%> (have %qT)",
2297 return error_mark_node;
2300 /* This handles expressions of the form "a[i]", which denotes
2303 This is logically equivalent in C to *(a+i), but we may do it differently.
2304 If A is a variable or a member, we generate a primitive ARRAY_REF.
2305 This avoids forcing the array out of registers, and can work on
2306 arrays that are not lvalues (for example, members of structures returned
2309 For vector types, allow vector[i] but not i[vector], and create
2310 *(((type*)&vectortype) + i) for the expression.
2312 LOC is the location to use for the returned expression. */
2315 build_array_ref (location_t loc, tree array, tree index)
2318 bool swapped = false;
2319 if (TREE_TYPE (array) == error_mark_node
2320 || TREE_TYPE (index) == error_mark_node)
2321 return error_mark_node;
2323 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2324 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2325 /* Allow vector[index] but not index[vector]. */
2326 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2329 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2330 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2333 "subscripted value is neither array nor pointer nor vector");
2335 return error_mark_node;
2343 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2345 error_at (loc, "array subscript is not an integer");
2346 return error_mark_node;
2349 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2351 error_at (loc, "subscripted value is pointer to function");
2352 return error_mark_node;
2355 /* ??? Existing practice has been to warn only when the char
2356 index is syntactically the index, not for char[array]. */
2358 warn_array_subscript_with_type_char (index);
2360 /* Apply default promotions *after* noticing character types. */
2361 index = default_conversion (index);
2363 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2365 /* For vector[index], convert the vector to a
2366 pointer of the underlying type. */
2367 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2369 tree type = TREE_TYPE (array);
2372 if (TREE_CODE (index) == INTEGER_CST)
2373 if (!host_integerp (index, 1)
2374 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2375 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2376 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2378 c_common_mark_addressable_vec (array);
2379 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2380 type = build_pointer_type (type);
2381 type1 = build_pointer_type (TREE_TYPE (array));
2382 array = build1 (ADDR_EXPR, type1, array);
2383 array = convert (type, array);
2386 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2390 /* An array that is indexed by a non-constant
2391 cannot be stored in a register; we must be able to do
2392 address arithmetic on its address.
2393 Likewise an array of elements of variable size. */
2394 if (TREE_CODE (index) != INTEGER_CST
2395 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2396 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2398 if (!c_mark_addressable (array))
2399 return error_mark_node;
2401 /* An array that is indexed by a constant value which is not within
2402 the array bounds cannot be stored in a register either; because we
2403 would get a crash in store_bit_field/extract_bit_field when trying
2404 to access a non-existent part of the register. */
2405 if (TREE_CODE (index) == INTEGER_CST
2406 && TYPE_DOMAIN (TREE_TYPE (array))
2407 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2409 if (!c_mark_addressable (array))
2410 return error_mark_node;
2416 while (TREE_CODE (foo) == COMPONENT_REF)
2417 foo = TREE_OPERAND (foo, 0);
2418 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2419 pedwarn (loc, OPT_pedantic,
2420 "ISO C forbids subscripting %<register%> array");
2421 else if (!flag_isoc99 && !lvalue_p (foo))
2422 pedwarn (loc, OPT_pedantic,
2423 "ISO C90 forbids subscripting non-lvalue array");
2426 type = TREE_TYPE (TREE_TYPE (array));
2427 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2428 /* Array ref is const/volatile if the array elements are
2429 or if the array is. */
2430 TREE_READONLY (rval)
2431 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2432 | TREE_READONLY (array));
2433 TREE_SIDE_EFFECTS (rval)
2434 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2435 | TREE_SIDE_EFFECTS (array));
2436 TREE_THIS_VOLATILE (rval)
2437 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2438 /* This was added by rms on 16 Nov 91.
2439 It fixes vol struct foo *a; a->elts[1]
2440 in an inline function.
2441 Hope it doesn't break something else. */
2442 | TREE_THIS_VOLATILE (array));
2443 ret = require_complete_type (rval);
2444 protected_set_expr_location (ret, loc);
2449 tree ar = default_conversion (array);
2451 if (ar == error_mark_node)
2454 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2455 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2457 return build_indirect_ref
2458 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2463 /* Build an external reference to identifier ID. FUN indicates
2464 whether this will be used for a function call. LOC is the source
2465 location of the identifier. This sets *TYPE to the type of the
2466 identifier, which is not the same as the type of the returned value
2467 for CONST_DECLs defined as enum constants. If the type of the
2468 identifier is not available, *TYPE is set to NULL. */
2470 build_external_ref (location_t loc, tree id, int fun, tree *type)
2473 tree decl = lookup_name (id);
2475 /* In Objective-C, an instance variable (ivar) may be preferred to
2476 whatever lookup_name() found. */
2477 decl = objc_lookup_ivar (decl, id);
2480 if (decl && decl != error_mark_node)
2483 *type = TREE_TYPE (ref);
2486 /* Implicit function declaration. */
2487 ref = implicitly_declare (loc, id);
2488 else if (decl == error_mark_node)
2489 /* Don't complain about something that's already been
2490 complained about. */
2491 return error_mark_node;
2494 undeclared_variable (loc, id);
2495 return error_mark_node;
2498 if (TREE_TYPE (ref) == error_mark_node)
2499 return error_mark_node;
2501 if (TREE_DEPRECATED (ref))
2502 warn_deprecated_use (ref, NULL_TREE);
2504 /* Recursive call does not count as usage. */
2505 if (ref != current_function_decl)
2507 TREE_USED (ref) = 1;
2510 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2512 if (!in_sizeof && !in_typeof)
2513 C_DECL_USED (ref) = 1;
2514 else if (DECL_INITIAL (ref) == 0
2515 && DECL_EXTERNAL (ref)
2516 && !TREE_PUBLIC (ref))
2517 record_maybe_used_decl (ref);
2520 if (TREE_CODE (ref) == CONST_DECL)
2522 used_types_insert (TREE_TYPE (ref));
2525 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2526 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2528 warning_at (loc, OPT_Wc___compat,
2529 ("enum constant defined in struct or union "
2530 "is not visible in C++"));
2531 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2534 ref = DECL_INITIAL (ref);
2535 TREE_CONSTANT (ref) = 1;
2537 else if (current_function_decl != 0
2538 && !DECL_FILE_SCOPE_P (current_function_decl)
2539 && (TREE_CODE (ref) == VAR_DECL
2540 || TREE_CODE (ref) == PARM_DECL
2541 || TREE_CODE (ref) == FUNCTION_DECL))
2543 tree context = decl_function_context (ref);
2545 if (context != 0 && context != current_function_decl)
2546 DECL_NONLOCAL (ref) = 1;
2548 /* C99 6.7.4p3: An inline definition of a function with external
2549 linkage ... shall not contain a reference to an identifier with
2550 internal linkage. */
2551 else if (current_function_decl != 0
2552 && DECL_DECLARED_INLINE_P (current_function_decl)
2553 && DECL_EXTERNAL (current_function_decl)
2554 && VAR_OR_FUNCTION_DECL_P (ref)
2555 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2556 && ! TREE_PUBLIC (ref)
2557 && DECL_CONTEXT (ref) != current_function_decl)
2558 record_inline_static (loc, current_function_decl, ref,
2564 /* Record details of decls possibly used inside sizeof or typeof. */
2565 struct maybe_used_decl
2569 /* The level seen at (in_sizeof + in_typeof). */
2571 /* The next one at this level or above, or NULL. */
2572 struct maybe_used_decl *next;
2575 static struct maybe_used_decl *maybe_used_decls;
2577 /* Record that DECL, an undefined static function reference seen
2578 inside sizeof or typeof, might be used if the operand of sizeof is
2579 a VLA type or the operand of typeof is a variably modified
2583 record_maybe_used_decl (tree decl)
2585 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2587 t->level = in_sizeof + in_typeof;
2588 t->next = maybe_used_decls;
2589 maybe_used_decls = t;
2592 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2593 USED is false, just discard them. If it is true, mark them used
2594 (if no longer inside sizeof or typeof) or move them to the next
2595 level up (if still inside sizeof or typeof). */
2598 pop_maybe_used (bool used)
2600 struct maybe_used_decl *p = maybe_used_decls;
2601 int cur_level = in_sizeof + in_typeof;
2602 while (p && p->level > cur_level)
2607 C_DECL_USED (p->decl) = 1;
2609 p->level = cur_level;
2613 if (!used || cur_level == 0)
2614 maybe_used_decls = p;
2617 /* Return the result of sizeof applied to EXPR. */
2620 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2623 if (expr.value == error_mark_node)
2625 ret.value = error_mark_node;
2626 ret.original_code = ERROR_MARK;
2627 ret.original_type = NULL;
2628 pop_maybe_used (false);
2632 bool expr_const_operands = true;
2633 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2634 &expr_const_operands);
2635 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2636 ret.original_code = ERROR_MARK;
2637 ret.original_type = NULL;
2638 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2640 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2641 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2642 folded_expr, ret.value);
2643 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2644 SET_EXPR_LOCATION (ret.value, loc);
2646 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2651 /* Return the result of sizeof applied to T, a structure for the type
2652 name passed to sizeof (rather than the type itself). LOC is the
2653 location of the original expression. */
2656 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2660 tree type_expr = NULL_TREE;
2661 bool type_expr_const = true;
2662 type = groktypename (t, &type_expr, &type_expr_const);
2663 ret.value = c_sizeof (loc, type);
2664 ret.original_code = ERROR_MARK;
2665 ret.original_type = NULL;
2666 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2667 && c_vla_type_p (type))
2669 /* If the type is a [*] array, it is a VLA but is represented as
2670 having a size of zero. In such a case we must ensure that
2671 the result of sizeof does not get folded to a constant by
2672 c_fully_fold, because if the size is evaluated the result is
2673 not constant and so constraints on zero or negative size
2674 arrays must not be applied when this sizeof call is inside
2675 another array declarator. */
2677 type_expr = integer_zero_node;
2678 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2679 type_expr, ret.value);
2680 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2682 pop_maybe_used (type != error_mark_node
2683 ? C_TYPE_VARIABLE_SIZE (type) : false);
2687 /* Build a function call to function FUNCTION with parameters PARAMS.
2688 The function call is at LOC.
2689 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2690 TREE_VALUE of each node is a parameter-expression.
2691 FUNCTION's data type may be a function type or a pointer-to-function. */
2694 build_function_call (location_t loc, tree function, tree params)
2699 vec = VEC_alloc (tree, gc, list_length (params));
2700 for (; params; params = TREE_CHAIN (params))
2701 VEC_quick_push (tree, vec, TREE_VALUE (params));
2702 ret = build_function_call_vec (loc, function, vec, NULL);
2703 VEC_free (tree, gc, vec);
2707 /* Build a function call to function FUNCTION with parameters PARAMS.
2708 ORIGTYPES, if not NULL, is a vector of types; each element is
2709 either NULL or the original type of the corresponding element in
2710 PARAMS. The original type may differ from TREE_TYPE of the
2711 parameter for enums. FUNCTION's data type may be a function type
2712 or pointer-to-function. This function changes the elements of
2716 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2717 VEC(tree,gc) *origtypes)
2719 tree fntype, fundecl = 0;
2720 tree name = NULL_TREE, result;
2726 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2727 STRIP_TYPE_NOPS (function);
2729 /* Convert anything with function type to a pointer-to-function. */
2730 if (TREE_CODE (function) == FUNCTION_DECL)
2732 /* Implement type-directed function overloading for builtins.
2733 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2734 handle all the type checking. The result is a complete expression
2735 that implements this function call. */
2736 tem = resolve_overloaded_builtin (loc, function, params);
2740 name = DECL_NAME (function);
2743 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2744 function = function_to_pointer_conversion (loc, function);
2746 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2747 expressions, like those used for ObjC messenger dispatches. */
2748 if (!VEC_empty (tree, params))
2749 function = objc_rewrite_function_call (function,
2750 VEC_index (tree, params, 0));
2752 function = c_fully_fold (function, false, NULL);
2754 fntype = TREE_TYPE (function);
2756 if (TREE_CODE (fntype) == ERROR_MARK)
2757 return error_mark_node;
2759 if (!(TREE_CODE (fntype) == POINTER_TYPE
2760 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2762 error_at (loc, "called object %qE is not a function", function);
2763 return error_mark_node;
2766 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2767 current_function_returns_abnormally = 1;
2769 /* fntype now gets the type of function pointed to. */
2770 fntype = TREE_TYPE (fntype);
2772 /* Convert the parameters to the types declared in the
2773 function prototype, or apply default promotions. */
2775 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2778 return error_mark_node;
2780 /* Check that the function is called through a compatible prototype.
2781 If it is not, replace the call by a trap, wrapped up in a compound
2782 expression if necessary. This has the nice side-effect to prevent
2783 the tree-inliner from generating invalid assignment trees which may
2784 blow up in the RTL expander later. */
2785 if (CONVERT_EXPR_P (function)
2786 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2787 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2788 && !comptypes (fntype, TREE_TYPE (tem)))
2790 tree return_type = TREE_TYPE (fntype);
2791 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2795 /* This situation leads to run-time undefined behavior. We can't,
2796 therefore, simply error unless we can prove that all possible
2797 executions of the program must execute the code. */
2798 if (warning_at (loc, 0, "function called through a non-compatible type"))
2799 /* We can, however, treat "undefined" any way we please.
2800 Call abort to encourage the user to fix the program. */
2801 inform (loc, "if this code is reached, the program will abort");
2802 /* Before the abort, allow the function arguments to exit or
2804 for (i = 0; i < nargs; i++)
2805 trap = build2 (COMPOUND_EXPR, void_type_node,
2806 VEC_index (tree, params, i), trap);
2808 if (VOID_TYPE_P (return_type))
2810 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2812 "function with qualified void return type called");
2819 if (AGGREGATE_TYPE_P (return_type))
2820 rhs = build_compound_literal (loc, return_type,
2821 build_constructor (return_type, 0),
2824 rhs = build_zero_cst (return_type);
2826 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2831 argarray = VEC_address (tree, params);
2833 /* Check that arguments to builtin functions match the expectations. */
2835 && DECL_BUILT_IN (fundecl)
2836 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2837 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2838 return error_mark_node;
2840 /* Check that the arguments to the function are valid. */
2841 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2842 TYPE_ARG_TYPES (fntype));
2844 if (name != NULL_TREE
2845 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2847 if (require_constant_value)
2849 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2850 function, nargs, argarray);
2852 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2853 function, nargs, argarray);
2854 if (TREE_CODE (result) == NOP_EXPR
2855 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2856 STRIP_TYPE_NOPS (result);
2859 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2860 function, nargs, argarray);
2862 if (VOID_TYPE_P (TREE_TYPE (result)))
2864 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2866 "function with qualified void return type called");
2869 return require_complete_type (result);
2872 /* Convert the argument expressions in the vector VALUES
2873 to the types in the list TYPELIST.
2875 If TYPELIST is exhausted, or when an element has NULL as its type,
2876 perform the default conversions.
2878 ORIGTYPES is the original types of the expressions in VALUES. This
2879 holds the type of enum values which have been converted to integral
2880 types. It may be NULL.
2882 FUNCTION is a tree for the called function. It is used only for
2883 error messages, where it is formatted with %qE.
2885 This is also where warnings about wrong number of args are generated.
2887 Returns the actual number of arguments processed (which may be less
2888 than the length of VALUES in some error situations), or -1 on
2892 convert_arguments (tree typelist, VEC(tree,gc) *values,
2893 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2896 unsigned int parmnum;
2897 bool error_args = false;
2898 const bool type_generic = fundecl
2899 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2900 bool type_generic_remove_excess_precision = false;
2903 /* Change pointer to function to the function itself for
2905 if (TREE_CODE (function) == ADDR_EXPR
2906 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2907 function = TREE_OPERAND (function, 0);
2909 /* Handle an ObjC selector specially for diagnostics. */
2910 selector = objc_message_selector ();
2912 /* For type-generic built-in functions, determine whether excess
2913 precision should be removed (classification) or not
2916 && DECL_BUILT_IN (fundecl)
2917 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2919 switch (DECL_FUNCTION_CODE (fundecl))
2921 case BUILT_IN_ISFINITE:
2922 case BUILT_IN_ISINF:
2923 case BUILT_IN_ISINF_SIGN:
2924 case BUILT_IN_ISNAN:
2925 case BUILT_IN_ISNORMAL:
2926 case BUILT_IN_FPCLASSIFY:
2927 type_generic_remove_excess_precision = true;
2931 type_generic_remove_excess_precision = false;
2936 /* Scan the given expressions and types, producing individual
2937 converted arguments. */
2939 for (typetail = typelist, parmnum = 0;
2940 VEC_iterate (tree, values, parmnum, val);
2943 tree type = typetail ? TREE_VALUE (typetail) : 0;
2944 tree valtype = TREE_TYPE (val);
2945 tree rname = function;
2946 int argnum = parmnum + 1;
2947 const char *invalid_func_diag;
2948 bool excess_precision = false;
2952 if (type == void_type_node)
2955 error_at (input_location,
2956 "too many arguments to method %qE", selector);
2958 error_at (input_location,
2959 "too many arguments to function %qE", function);
2961 if (fundecl && !DECL_BUILT_IN (fundecl))
2962 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2966 if (selector && argnum > 2)
2972 npc = null_pointer_constant_p (val);
2974 /* If there is excess precision and a prototype, convert once to
2975 the required type rather than converting via the semantic
2976 type. Likewise without a prototype a float value represented
2977 as long double should be converted once to double. But for
2978 type-generic classification functions excess precision must
2980 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2981 && (type || !type_generic || !type_generic_remove_excess_precision))
2983 val = TREE_OPERAND (val, 0);
2984 excess_precision = true;
2986 val = c_fully_fold (val, false, NULL);
2987 STRIP_TYPE_NOPS (val);
2989 val = require_complete_type (val);
2993 /* Formal parm type is specified by a function prototype. */
2995 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2997 error ("type of formal parameter %d is incomplete", parmnum + 1);
3004 /* Optionally warn about conversions that
3005 differ from the default conversions. */
3006 if (warn_traditional_conversion || warn_traditional)
3008 unsigned int formal_prec = TYPE_PRECISION (type);
3010 if (INTEGRAL_TYPE_P (type)
3011 && TREE_CODE (valtype) == REAL_TYPE)
3012 warning (0, "passing argument %d of %qE as integer "
3013 "rather than floating due to prototype",
3015 if (INTEGRAL_TYPE_P (type)
3016 && TREE_CODE (valtype) == COMPLEX_TYPE)
3017 warning (0, "passing argument %d of %qE as integer "
3018 "rather than complex due to prototype",
3020 else if (TREE_CODE (type) == COMPLEX_TYPE
3021 && TREE_CODE (valtype) == REAL_TYPE)
3022 warning (0, "passing argument %d of %qE as complex "
3023 "rather than floating due to prototype",
3025 else if (TREE_CODE (type) == REAL_TYPE
3026 && INTEGRAL_TYPE_P (valtype))
3027 warning (0, "passing argument %d of %qE as floating "
3028 "rather than integer due to prototype",
3030 else if (TREE_CODE (type) == COMPLEX_TYPE
3031 && INTEGRAL_TYPE_P (valtype))
3032 warning (0, "passing argument %d of %qE as complex "
3033 "rather than integer due to prototype",
3035 else if (TREE_CODE (type) == REAL_TYPE
3036 && TREE_CODE (valtype) == COMPLEX_TYPE)
3037 warning (0, "passing argument %d of %qE as floating "
3038 "rather than complex due to prototype",
3040 /* ??? At some point, messages should be written about
3041 conversions between complex types, but that's too messy
3043 else if (TREE_CODE (type) == REAL_TYPE
3044 && TREE_CODE (valtype) == REAL_TYPE)
3046 /* Warn if any argument is passed as `float',
3047 since without a prototype it would be `double'. */
3048 if (formal_prec == TYPE_PRECISION (float_type_node)
3049 && type != dfloat32_type_node)
3050 warning (0, "passing argument %d of %qE as %<float%> "
3051 "rather than %<double%> due to prototype",
3054 /* Warn if mismatch between argument and prototype
3055 for decimal float types. Warn of conversions with
3056 binary float types and of precision narrowing due to
3058 else if (type != valtype
3059 && (type == dfloat32_type_node
3060 || type == dfloat64_type_node
3061 || type == dfloat128_type_node
3062 || valtype == dfloat32_type_node
3063 || valtype == dfloat64_type_node
3064 || valtype == dfloat128_type_node)
3066 <= TYPE_PRECISION (valtype)
3067 || (type == dfloat128_type_node
3069 != dfloat64_type_node
3071 != dfloat32_type_node)))
3072 || (type == dfloat64_type_node
3074 != dfloat32_type_node))))
3075 warning (0, "passing argument %d of %qE as %qT "
3076 "rather than %qT due to prototype",
3077 argnum, rname, type, valtype);
3080 /* Detect integer changing in width or signedness.
3081 These warnings are only activated with
3082 -Wtraditional-conversion, not with -Wtraditional. */
3083 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3084 && INTEGRAL_TYPE_P (valtype))
3086 tree would_have_been = default_conversion (val);
3087 tree type1 = TREE_TYPE (would_have_been);
3089 if (TREE_CODE (type) == ENUMERAL_TYPE
3090 && (TYPE_MAIN_VARIANT (type)
3091 == TYPE_MAIN_VARIANT (valtype)))
3092 /* No warning if function asks for enum
3093 and the actual arg is that enum type. */
3095 else if (formal_prec != TYPE_PRECISION (type1))
3096 warning (OPT_Wtraditional_conversion,
3097 "passing argument %d of %qE "
3098 "with different width due to prototype",
3100 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3102 /* Don't complain if the formal parameter type
3103 is an enum, because we can't tell now whether
3104 the value was an enum--even the same enum. */
3105 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3107 else if (TREE_CODE (val) == INTEGER_CST
3108 && int_fits_type_p (val, type))
3109 /* Change in signedness doesn't matter
3110 if a constant value is unaffected. */
3112 /* If the value is extended from a narrower
3113 unsigned type, it doesn't matter whether we
3114 pass it as signed or unsigned; the value
3115 certainly is the same either way. */
3116 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3117 && TYPE_UNSIGNED (valtype))
3119 else if (TYPE_UNSIGNED (type))
3120 warning (OPT_Wtraditional_conversion,
3121 "passing argument %d of %qE "
3122 "as unsigned due to prototype",
3125 warning (OPT_Wtraditional_conversion,
3126 "passing argument %d of %qE "
3127 "as signed due to prototype", argnum, rname);
3131 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3132 sake of better warnings from convert_and_check. */
3133 if (excess_precision)
3134 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3135 origtype = (origtypes == NULL
3137 : VEC_index (tree, origtypes, parmnum));
3138 parmval = convert_for_assignment (input_location, type, val,
3139 origtype, ic_argpass, npc,
3143 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3144 && INTEGRAL_TYPE_P (type)
3145 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3146 parmval = default_conversion (parmval);
3149 else if (TREE_CODE (valtype) == REAL_TYPE
3150 && (TYPE_PRECISION (valtype)
3151 < TYPE_PRECISION (double_type_node))
3152 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3158 /* Convert `float' to `double'. */
3159 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3160 warning (OPT_Wdouble_promotion,
3161 "implicit conversion from %qT to %qT when passing "
3162 "argument to function",
3163 valtype, double_type_node);
3164 parmval = convert (double_type_node, val);
3167 else if (excess_precision && !type_generic)
3168 /* A "double" argument with excess precision being passed
3169 without a prototype or in variable arguments. */
3170 parmval = convert (valtype, val);
3171 else if ((invalid_func_diag =
3172 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3174 error (invalid_func_diag);
3178 /* Convert `short' and `char' to full-size `int'. */
3179 parmval = default_conversion (val);
3181 VEC_replace (tree, values, parmnum, parmval);
3182 if (parmval == error_mark_node)
3186 typetail = TREE_CHAIN (typetail);
3189 gcc_assert (parmnum == VEC_length (tree, values));
3191 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3193 error_at (input_location,
3194 "too few arguments to function %qE", function);
3195 if (fundecl && !DECL_BUILT_IN (fundecl))
3196 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3200 return error_args ? -1 : (int) parmnum;
3203 /* This is the entry point used by the parser to build unary operators
3204 in the input. CODE, a tree_code, specifies the unary operator, and
3205 ARG is the operand. For unary plus, the C parser currently uses
3206 CONVERT_EXPR for code.
3208 LOC is the location to use for the tree generated.
3212 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3214 struct c_expr result;
3216 result.value = build_unary_op (loc, code, arg.value, 0);
3217 result.original_code = code;
3218 result.original_type = NULL;
3220 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3221 overflow_warning (loc, result.value);
3226 /* This is the entry point used by the parser to build binary operators
3227 in the input. CODE, a tree_code, specifies the binary operator, and
3228 ARG1 and ARG2 are the operands. In addition to constructing the
3229 expression, we check for operands that were written with other binary
3230 operators in a way that is likely to confuse the user.
3232 LOCATION is the location of the binary operator. */
3235 parser_build_binary_op (location_t location, enum tree_code code,
3236 struct c_expr arg1, struct c_expr arg2)
3238 struct c_expr result;
3240 enum tree_code code1 = arg1.original_code;
3241 enum tree_code code2 = arg2.original_code;
3242 tree type1 = (arg1.original_type
3243 ? arg1.original_type
3244 : TREE_TYPE (arg1.value));
3245 tree type2 = (arg2.original_type
3246 ? arg2.original_type
3247 : TREE_TYPE (arg2.value));
3249 result.value = build_binary_op (location, code,
3250 arg1.value, arg2.value, 1);
3251 result.original_code = code;
3252 result.original_type = NULL;
3254 if (TREE_CODE (result.value) == ERROR_MARK)
3257 if (location != UNKNOWN_LOCATION)
3258 protected_set_expr_location (result.value, location);
3260 /* Check for cases such as x+y<<z which users are likely
3262 if (warn_parentheses)
3263 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3265 if (warn_logical_op)
3266 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3267 code1, arg1.value, code2, arg2.value);
3269 /* Warn about comparisons against string literals, with the exception
3270 of testing for equality or inequality of a string literal with NULL. */
3271 if (code == EQ_EXPR || code == NE_EXPR)
3273 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3274 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3275 warning_at (location, OPT_Waddress,
3276 "comparison with string literal results in unspecified behavior");
3278 else if (TREE_CODE_CLASS (code) == tcc_comparison
3279 && (code1 == STRING_CST || code2 == STRING_CST))
3280 warning_at (location, OPT_Waddress,
3281 "comparison with string literal results in unspecified behavior");
3283 if (TREE_OVERFLOW_P (result.value)
3284 && !TREE_OVERFLOW_P (arg1.value)
3285 && !TREE_OVERFLOW_P (arg2.value))
3286 overflow_warning (location, result.value);
3288 /* Warn about comparisons of different enum types. */
3289 if (warn_enum_compare
3290 && TREE_CODE_CLASS (code) == tcc_comparison
3291 && TREE_CODE (type1) == ENUMERAL_TYPE
3292 && TREE_CODE (type2) == ENUMERAL_TYPE
3293 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3294 warning_at (location, OPT_Wenum_compare,
3295 "comparison between %qT and %qT",
3301 /* Return a tree for the difference of pointers OP0 and OP1.
3302 The resulting tree has type int. */
3305 pointer_diff (location_t loc, tree op0, tree op1)
3307 tree restype = ptrdiff_type_node;
3308 tree result, inttype;
3310 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3311 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3312 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3313 tree con0, con1, lit0, lit1;
3314 tree orig_op1 = op1;
3316 /* If the operands point into different address spaces, we need to
3317 explicitly convert them to pointers into the common address space
3318 before we can subtract the numerical address values. */
3321 addr_space_t as_common;
3324 /* Determine the common superset address space. This is guaranteed
3325 to exist because the caller verified that comp_target_types
3326 returned non-zero. */
3327 if (!addr_space_superset (as0, as1, &as_common))
3330 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3331 op0 = convert (common_type, op0);
3332 op1 = convert (common_type, op1);
3335 /* Determine integer type to perform computations in. This will usually
3336 be the same as the result type (ptrdiff_t), but may need to be a wider
3337 type if pointers for the address space are wider than ptrdiff_t. */
3338 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3339 inttype = lang_hooks.types.type_for_size
3340 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3345 if (TREE_CODE (target_type) == VOID_TYPE)
3346 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3347 "pointer of type %<void *%> used in subtraction");
3348 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3349 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3350 "pointer to a function used in subtraction");
3352 /* If the conversion to ptrdiff_type does anything like widening or
3353 converting a partial to an integral mode, we get a convert_expression
3354 that is in the way to do any simplifications.
3355 (fold-const.c doesn't know that the extra bits won't be needed.
3356 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3357 different mode in place.)
3358 So first try to find a common term here 'by hand'; we want to cover
3359 at least the cases that occur in legal static initializers. */
3360 if (CONVERT_EXPR_P (op0)
3361 && (TYPE_PRECISION (TREE_TYPE (op0))
3362 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3363 con0 = TREE_OPERAND (op0, 0);
3366 if (CONVERT_EXPR_P (op1)
3367 && (TYPE_PRECISION (TREE_TYPE (op1))
3368 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3369 con1 = TREE_OPERAND (op1, 0);
3373 if (TREE_CODE (con0) == PLUS_EXPR)
3375 lit0 = TREE_OPERAND (con0, 1);
3376 con0 = TREE_OPERAND (con0, 0);
3379 lit0 = integer_zero_node;
3381 if (TREE_CODE (con1) == PLUS_EXPR)
3383 lit1 = TREE_OPERAND (con1, 1);
3384 con1 = TREE_OPERAND (con1, 0);
3387 lit1 = integer_zero_node;
3389 if (operand_equal_p (con0, con1, 0))
3396 /* First do the subtraction as integers;
3397 then drop through to build the divide operator.
3398 Do not do default conversions on the minus operator
3399 in case restype is a short type. */
3401 op0 = build_binary_op (loc,
3402 MINUS_EXPR, convert (inttype, op0),
3403 convert (inttype, op1), 0);
3404 /* This generates an error if op1 is pointer to incomplete type. */
3405 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3406 error_at (loc, "arithmetic on pointer to an incomplete type");
3408 /* This generates an error if op0 is pointer to incomplete type. */
3409 op1 = c_size_in_bytes (target_type);
3411 /* Divide by the size, in easiest possible way. */
3412 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3413 op0, convert (inttype, op1));
3415 /* Convert to final result type if necessary. */
3416 return convert (restype, result);
3419 /* Construct and perhaps optimize a tree representation
3420 for a unary operation. CODE, a tree_code, specifies the operation
3421 and XARG is the operand.
3422 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3423 the default promotions (such as from short to int).
3424 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3425 allows non-lvalues; this is only used to handle conversion of non-lvalue
3426 arrays to pointers in C99.
3428 LOCATION is the location of the operator. */
3431 build_unary_op (location_t location,
3432 enum tree_code code, tree xarg, int flag)
3434 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3437 enum tree_code typecode;
3439 tree ret = error_mark_node;
3440 tree eptype = NULL_TREE;
3441 int noconvert = flag;
3442 const char *invalid_op_diag;
3445 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3447 arg = remove_c_maybe_const_expr (arg);
3449 if (code != ADDR_EXPR)
3450 arg = require_complete_type (arg);
3452 typecode = TREE_CODE (TREE_TYPE (arg));
3453 if (typecode == ERROR_MARK)
3454 return error_mark_node;
3455 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3456 typecode = INTEGER_TYPE;
3458 if ((invalid_op_diag
3459 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3461 error_at (location, invalid_op_diag);
3462 return error_mark_node;
3465 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3467 eptype = TREE_TYPE (arg);
3468 arg = TREE_OPERAND (arg, 0);
3474 /* This is used for unary plus, because a CONVERT_EXPR
3475 is enough to prevent anybody from looking inside for
3476 associativity, but won't generate any code. */
3477 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3478 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3479 || typecode == VECTOR_TYPE))
3481 error_at (location, "wrong type argument to unary plus");
3482 return error_mark_node;
3484 else if (!noconvert)
3485 arg = default_conversion (arg);
3486 arg = non_lvalue_loc (location, arg);
3490 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3491 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3492 || typecode == VECTOR_TYPE))
3494 error_at (location, "wrong type argument to unary minus");
3495 return error_mark_node;
3497 else if (!noconvert)
3498 arg = default_conversion (arg);
3502 /* ~ works on integer types and non float vectors. */
3503 if (typecode == INTEGER_TYPE
3504 || (typecode == VECTOR_TYPE
3505 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3508 arg = default_conversion (arg);
3510 else if (typecode == COMPLEX_TYPE)
3513 pedwarn (location, OPT_pedantic,
3514 "ISO C does not support %<~%> for complex conjugation");
3516 arg = default_conversion (arg);
3520 error_at (location, "wrong type argument to bit-complement");
3521 return error_mark_node;
3526 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3528 error_at (location, "wrong type argument to abs");
3529 return error_mark_node;
3531 else if (!noconvert)
3532 arg = default_conversion (arg);
3536 /* Conjugating a real value is a no-op, but allow it anyway. */
3537 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3538 || typecode == COMPLEX_TYPE))
3540 error_at (location, "wrong type argument to conjugation");
3541 return error_mark_node;
3543 else if (!noconvert)
3544 arg = default_conversion (arg);
3547 case TRUTH_NOT_EXPR:
3548 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3549 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3550 && typecode != COMPLEX_TYPE)
3553 "wrong type argument to unary exclamation mark");
3554 return error_mark_node;
3556 arg = c_objc_common_truthvalue_conversion (location, arg);
3557 ret = invert_truthvalue_loc (location, arg);
3558 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3559 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3560 location = EXPR_LOCATION (ret);
3561 goto return_build_unary_op;
3565 ret = build_real_imag_expr (location, code, arg);
3566 if (ret == error_mark_node)
3567 return error_mark_node;
3568 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3569 eptype = TREE_TYPE (eptype);
3570 goto return_build_unary_op;
3572 case PREINCREMENT_EXPR:
3573 case POSTINCREMENT_EXPR:
3574 case PREDECREMENT_EXPR:
3575 case POSTDECREMENT_EXPR:
3577 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3579 tree inner = build_unary_op (location, code,
3580 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3581 if (inner == error_mark_node)
3582 return error_mark_node;
3583 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3584 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3585 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3586 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3587 goto return_build_unary_op;
3590 /* Complain about anything that is not a true lvalue. In
3591 Objective-C, skip this check for property_refs. */
3592 if (!objc_is_property_ref (arg)
3593 && !lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3594 || code == POSTINCREMENT_EXPR)
3597 return error_mark_node;
3599 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3601 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3602 warning_at (location, OPT_Wc___compat,
3603 "increment of enumeration value is invalid in C++");
3605 warning_at (location, OPT_Wc___compat,
3606 "decrement of enumeration value is invalid in C++");
3609 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3610 arg = c_fully_fold (arg, false, NULL);
3612 /* Increment or decrement the real part of the value,
3613 and don't change the imaginary part. */
3614 if (typecode == COMPLEX_TYPE)
3618 pedwarn (location, OPT_pedantic,
3619 "ISO C does not support %<++%> and %<--%> on complex types");
3621 arg = stabilize_reference (arg);
3622 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3623 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3624 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3625 if (real == error_mark_node || imag == error_mark_node)
3626 return error_mark_node;
3627 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3629 goto return_build_unary_op;
3632 /* Report invalid types. */
3634 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3635 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3637 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3638 error_at (location, "wrong type argument to increment");
3640 error_at (location, "wrong type argument to decrement");
3642 return error_mark_node;
3648 argtype = TREE_TYPE (arg);
3650 /* Compute the increment. */
3652 if (typecode == POINTER_TYPE)
3654 /* If pointer target is an undefined struct,
3655 we just cannot know how to do the arithmetic. */
3656 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3658 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3660 "increment of pointer to unknown structure");
3663 "decrement of pointer to unknown structure");
3665 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3666 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3668 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3669 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3670 "wrong type argument to increment");
3672 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3673 "wrong type argument to decrement");
3676 inc = c_size_in_bytes (TREE_TYPE (argtype));
3677 inc = fold_convert_loc (location, sizetype, inc);
3679 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3681 /* For signed fract types, we invert ++ to -- or
3682 -- to ++, and change inc from 1 to -1, because
3683 it is not possible to represent 1 in signed fract constants.
3684 For unsigned fract types, the result always overflows and
3685 we get an undefined (original) or the maximum value. */
3686 if (code == PREINCREMENT_EXPR)
3687 code = PREDECREMENT_EXPR;
3688 else if (code == PREDECREMENT_EXPR)
3689 code = PREINCREMENT_EXPR;
3690 else if (code == POSTINCREMENT_EXPR)
3691 code = POSTDECREMENT_EXPR;
3692 else /* code == POSTDECREMENT_EXPR */
3693 code = POSTINCREMENT_EXPR;
3695 inc = integer_minus_one_node;
3696 inc = convert (argtype, inc);
3700 inc = integer_one_node;
3701 inc = convert (argtype, inc);
3704 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3705 need to ask Objective-C to build the increment or decrement
3706 expression for it. */
3707 if (objc_is_property_ref (arg))
3708 return objc_build_incr_expr_for_property_ref (location, code,
3711 /* Report a read-only lvalue. */
3712 if (TYPE_READONLY (argtype))
3714 readonly_error (arg,
3715 ((code == PREINCREMENT_EXPR
3716 || code == POSTINCREMENT_EXPR)
3717 ? lv_increment : lv_decrement));
3718 return error_mark_node;
3720 else if (TREE_READONLY (arg))
3721 readonly_warning (arg,
3722 ((code == PREINCREMENT_EXPR
3723 || code == POSTINCREMENT_EXPR)
3724 ? lv_increment : lv_decrement));
3726 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3727 val = boolean_increment (code, arg);
3729 val = build2 (code, TREE_TYPE (arg), arg, inc);
3730 TREE_SIDE_EFFECTS (val) = 1;
3731 if (TREE_CODE (val) != code)
3732 TREE_NO_WARNING (val) = 1;
3734 goto return_build_unary_op;
3738 /* Note that this operation never does default_conversion. */
3740 /* The operand of unary '&' must be an lvalue (which excludes
3741 expressions of type void), or, in C99, the result of a [] or
3742 unary '*' operator. */
3743 if (VOID_TYPE_P (TREE_TYPE (arg))
3744 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3745 && (TREE_CODE (arg) != INDIRECT_REF
3747 pedwarn (location, 0, "taking address of expression of type %<void%>");
3749 /* Let &* cancel out to simplify resulting code. */
3750 if (TREE_CODE (arg) == INDIRECT_REF)
3752 /* Don't let this be an lvalue. */
3753 if (lvalue_p (TREE_OPERAND (arg, 0)))
3754 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3755 ret = TREE_OPERAND (arg, 0);
3756 goto return_build_unary_op;
3759 /* For &x[y], return x+y */
3760 if (TREE_CODE (arg) == ARRAY_REF)
3762 tree op0 = TREE_OPERAND (arg, 0);
3763 if (!c_mark_addressable (op0))
3764 return error_mark_node;
3765 return build_binary_op (location, PLUS_EXPR,
3766 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3767 ? array_to_pointer_conversion (location,
3770 TREE_OPERAND (arg, 1), 1);
3773 /* Anything not already handled and not a true memory reference
3774 or a non-lvalue array is an error. */
3775 else if (typecode != FUNCTION_TYPE && !flag
3776 && !lvalue_or_else (arg, lv_addressof))
3777 return error_mark_node;
3779 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3781 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3783 tree inner = build_unary_op (location, code,
3784 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3785 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3786 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3787 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3788 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3789 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3790 goto return_build_unary_op;
3793 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3794 argtype = TREE_TYPE (arg);
3796 /* If the lvalue is const or volatile, merge that into the type
3797 to which the address will point. This should only be needed
3798 for function types. */
3799 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3800 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3802 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3803 int quals = orig_quals;
3805 if (TREE_READONLY (arg))
3806 quals |= TYPE_QUAL_CONST;
3807 if (TREE_THIS_VOLATILE (arg))
3808 quals |= TYPE_QUAL_VOLATILE;
3810 gcc_assert (quals == orig_quals
3811 || TREE_CODE (argtype) == FUNCTION_TYPE);
3813 argtype = c_build_qualified_type (argtype, quals);
3816 if (!c_mark_addressable (arg))
3817 return error_mark_node;
3819 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3820 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3822 argtype = build_pointer_type (argtype);
3824 /* ??? Cope with user tricks that amount to offsetof. Delete this
3825 when we have proper support for integer constant expressions. */
3826 val = get_base_address (arg);
3827 if (val && TREE_CODE (val) == INDIRECT_REF
3828 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3830 tree op0 = fold_convert_loc (location, sizetype,
3831 fold_offsetof (arg, val)), op1;
3833 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3834 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3835 goto return_build_unary_op;
3838 val = build1 (ADDR_EXPR, argtype, arg);
3841 goto return_build_unary_op;
3848 argtype = TREE_TYPE (arg);
3849 if (TREE_CODE (arg) == INTEGER_CST)
3850 ret = (require_constant_value
3851 ? fold_build1_initializer_loc (location, code, argtype, arg)
3852 : fold_build1_loc (location, code, argtype, arg));
3854 ret = build1 (code, argtype, arg);
3855 return_build_unary_op:
3856 gcc_assert (ret != error_mark_node);
3857 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3858 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3859 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3860 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3861 ret = note_integer_operands (ret);
3863 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3864 protected_set_expr_location (ret, location);
3868 /* Return nonzero if REF is an lvalue valid for this language.
3869 Lvalues can be assigned, unless their type has TYPE_READONLY.
3870 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3873 lvalue_p (const_tree ref)
3875 const enum tree_code code = TREE_CODE (ref);
3882 return lvalue_p (TREE_OPERAND (ref, 0));
3884 case C_MAYBE_CONST_EXPR:
3885 return lvalue_p (TREE_OPERAND (ref, 1));
3887 case COMPOUND_LITERAL_EXPR:
3897 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3898 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3901 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3908 /* Give an error for storing in something that is 'const'. */
3911 readonly_error (tree arg, enum lvalue_use use)
3913 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3915 /* Using this macro rather than (for example) arrays of messages
3916 ensures that all the format strings are checked at compile
3918 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3919 : (use == lv_increment ? (I) \
3920 : (use == lv_decrement ? (D) : (AS))))
3921 if (TREE_CODE (arg) == COMPONENT_REF)
3923 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3924 readonly_error (TREE_OPERAND (arg, 0), use);
3926 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3927 G_("increment of read-only member %qD"),
3928 G_("decrement of read-only member %qD"),
3929 G_("read-only member %qD used as %<asm%> output")),
3930 TREE_OPERAND (arg, 1));
3932 else if (TREE_CODE (arg) == VAR_DECL)
3933 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3934 G_("increment of read-only variable %qD"),
3935 G_("decrement of read-only variable %qD"),
3936 G_("read-only variable %qD used as %<asm%> output")),
3939 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3940 G_("increment of read-only location %qE"),
3941 G_("decrement of read-only location %qE"),
3942 G_("read-only location %qE used as %<asm%> output")),
3946 /* Give a warning for storing in something that is read-only in GCC
3947 terms but not const in ISO C terms. */
3950 readonly_warning (tree arg, enum lvalue_use use)
3955 warning (0, "assignment of read-only location %qE", arg);
3958 warning (0, "increment of read-only location %qE", arg);
3961 warning (0, "decrement of read-only location %qE", arg);
3970 /* Return nonzero if REF is an lvalue valid for this language;
3971 otherwise, print an error message and return zero. USE says
3972 how the lvalue is being used and so selects the error message. */
3975 lvalue_or_else (const_tree ref, enum lvalue_use use)
3977 int win = lvalue_p (ref);
3985 /* Mark EXP saying that we need to be able to take the
3986 address of it; it should not be allocated in a register.
3987 Returns true if successful. */
3990 c_mark_addressable (tree exp)