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, 2011
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"
40 #include "tree-iterator.h"
43 #include "c-family/c-objc.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
54 /* Possibe cases of scalar_to_vector conversion. */
56 stv_error, /* Error occured. */
57 stv_nothing, /* Nothing happened. */
58 stv_firstarg, /* First argument must be expanded. */
59 stv_secondarg /* Second argument must be expanded. */
62 /* The level of nesting inside "__alignof__". */
65 /* The level of nesting inside "sizeof". */
68 /* The level of nesting inside "typeof". */
71 /* Nonzero if we've already printed a "missing braces around initializer"
72 message within this initializer. */
73 static int missing_braces_mentioned;
75 static int require_constant_value;
76 static int require_constant_elements;
78 static bool null_pointer_constant_p (const_tree);
79 static tree qualify_type (tree, tree);
80 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
82 static int comp_target_types (location_t, tree, tree);
83 static int function_types_compatible_p (const_tree, const_tree, bool *,
85 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
86 static tree lookup_field (tree, tree);
87 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
89 static tree pointer_diff (location_t, tree, tree);
90 static tree convert_for_assignment (location_t, tree, tree, tree,
91 enum impl_conv, bool, tree, tree, int);
92 static tree valid_compound_expr_initializer (tree, tree);
93 static void push_string (const char *);
94 static void push_member_name (tree);
95 static int spelling_length (void);
96 static char *print_spelling (char *);
97 static void warning_init (int, const char *);
98 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
99 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
101 static void output_pending_init_elements (int, struct obstack *);
102 static int set_designator (int, struct obstack *);
103 static void push_range_stack (tree, struct obstack *);
104 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
105 static void set_nonincremental_init (struct obstack *);
106 static void set_nonincremental_init_from_string (tree, struct obstack *);
107 static tree find_init_member (tree, struct obstack *);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (location_t, 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_for_mode (target, TYPE_MODE (t1), false);
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. */
519 len = list_length (p1);
522 for (i = 0; i < len; i++)
523 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
528 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
530 /* A null type means arg type is not specified.
531 Take whatever the other function type has. */
532 if (TREE_VALUE (p1) == 0)
534 TREE_VALUE (n) = TREE_VALUE (p2);
537 if (TREE_VALUE (p2) == 0)
539 TREE_VALUE (n) = TREE_VALUE (p1);
543 /* Given wait (union {union wait *u; int *i} *)
544 and wait (union wait *),
545 prefer union wait * as type of parm. */
546 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
547 && TREE_VALUE (p1) != TREE_VALUE (p2))
550 tree mv2 = TREE_VALUE (p2);
551 if (mv2 && mv2 != error_mark_node
552 && TREE_CODE (mv2) != ARRAY_TYPE)
553 mv2 = TYPE_MAIN_VARIANT (mv2);
554 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
555 memb; memb = DECL_CHAIN (memb))
557 tree mv3 = TREE_TYPE (memb);
558 if (mv3 && mv3 != error_mark_node
559 && TREE_CODE (mv3) != ARRAY_TYPE)
560 mv3 = TYPE_MAIN_VARIANT (mv3);
561 if (comptypes (mv3, mv2))
563 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
565 pedwarn (input_location, OPT_pedantic,
566 "function types not truly compatible in ISO C");
571 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
572 && TREE_VALUE (p2) != TREE_VALUE (p1))
575 tree mv1 = TREE_VALUE (p1);
576 if (mv1 && mv1 != error_mark_node
577 && TREE_CODE (mv1) != ARRAY_TYPE)
578 mv1 = TYPE_MAIN_VARIANT (mv1);
579 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
580 memb; memb = DECL_CHAIN (memb))
582 tree mv3 = TREE_TYPE (memb);
583 if (mv3 && mv3 != error_mark_node
584 && TREE_CODE (mv3) != ARRAY_TYPE)
585 mv3 = TYPE_MAIN_VARIANT (mv3);
586 if (comptypes (mv3, mv1))
588 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
590 pedwarn (input_location, OPT_pedantic,
591 "function types not truly compatible in ISO C");
596 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
600 t1 = build_function_type (valtype, newargs);
601 t1 = qualify_type (t1, t2);
602 /* ... falls through ... */
606 return build_type_attribute_variant (t1, attributes);
611 /* Return the type of a conditional expression between pointers to
612 possibly differently qualified versions of compatible types.
614 We assume that comp_target_types has already been done and returned
615 nonzero; if that isn't so, this may crash. */
618 common_pointer_type (tree t1, tree t2)
621 tree pointed_to_1, mv1;
622 tree pointed_to_2, mv2;
624 unsigned target_quals;
625 addr_space_t as1, as2, as_common;
628 /* Save time if the two types are the same. */
630 if (t1 == t2) return t1;
632 /* If one type is nonsense, use the other. */
633 if (t1 == error_mark_node)
635 if (t2 == error_mark_node)
638 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
639 && TREE_CODE (t2) == POINTER_TYPE);
641 /* Merge the attributes. */
642 attributes = targetm.merge_type_attributes (t1, t2);
644 /* Find the composite type of the target types, and combine the
645 qualifiers of the two types' targets. Do not lose qualifiers on
646 array element types by taking the TYPE_MAIN_VARIANT. */
647 mv1 = pointed_to_1 = TREE_TYPE (t1);
648 mv2 = pointed_to_2 = TREE_TYPE (t2);
649 if (TREE_CODE (mv1) != ARRAY_TYPE)
650 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
651 if (TREE_CODE (mv2) != ARRAY_TYPE)
652 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
653 target = composite_type (mv1, mv2);
655 /* For function types do not merge const qualifiers, but drop them
656 if used inconsistently. The middle-end uses these to mark const
657 and noreturn functions. */
658 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
659 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
661 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
662 target_quals = (quals1 & quals2);
664 target_quals = (quals1 | quals2);
666 /* If the two named address spaces are different, determine the common
667 superset address space. This is guaranteed to exist due to the
668 assumption that comp_target_type returned non-zero. */
669 as1 = TYPE_ADDR_SPACE (pointed_to_1);
670 as2 = TYPE_ADDR_SPACE (pointed_to_2);
671 if (!addr_space_superset (as1, as2, &as_common))
674 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
676 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
677 return build_type_attribute_variant (t1, attributes);
680 /* Return the common type for two arithmetic types under the usual
681 arithmetic conversions. The default conversions have already been
682 applied, and enumerated types converted to their compatible integer
683 types. The resulting type is unqualified and has no attributes.
685 This is the type for the result of most arithmetic operations
686 if the operands have the given two types. */
689 c_common_type (tree t1, tree t2)
691 enum tree_code code1;
692 enum tree_code code2;
694 /* If one type is nonsense, use the other. */
695 if (t1 == error_mark_node)
697 if (t2 == error_mark_node)
700 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
701 t1 = TYPE_MAIN_VARIANT (t1);
703 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
704 t2 = TYPE_MAIN_VARIANT (t2);
706 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
707 t1 = build_type_attribute_variant (t1, NULL_TREE);
709 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
710 t2 = build_type_attribute_variant (t2, NULL_TREE);
712 /* Save time if the two types are the same. */
714 if (t1 == t2) return t1;
716 code1 = TREE_CODE (t1);
717 code2 = TREE_CODE (t2);
719 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
720 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
721 || code1 == INTEGER_TYPE);
722 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
723 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
724 || code2 == INTEGER_TYPE);
726 /* When one operand is a decimal float type, the other operand cannot be
727 a generic float type or a complex type. We also disallow vector types
729 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
730 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
732 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
734 error ("can%'t mix operands of decimal float and vector types");
735 return error_mark_node;
737 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
739 error ("can%'t mix operands of decimal float and complex types");
740 return error_mark_node;
742 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
744 error ("can%'t mix operands of decimal float and other float types");
745 return error_mark_node;
749 /* If one type is a vector type, return that type. (How the usual
750 arithmetic conversions apply to the vector types extension is not
751 precisely specified.) */
752 if (code1 == VECTOR_TYPE)
755 if (code2 == VECTOR_TYPE)
758 /* If one type is complex, form the common type of the non-complex
759 components, then make that complex. Use T1 or T2 if it is the
761 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
763 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
764 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
765 tree subtype = c_common_type (subtype1, subtype2);
767 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
769 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
772 return build_complex_type (subtype);
775 /* If only one is real, use it as the result. */
777 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
780 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
783 /* If both are real and either are decimal floating point types, use
784 the decimal floating point type with the greater precision. */
786 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
788 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
789 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
790 return dfloat128_type_node;
791 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
793 return dfloat64_type_node;
794 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
795 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
796 return dfloat32_type_node;
799 /* Deal with fixed-point types. */
800 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
802 unsigned int unsignedp = 0, satp = 0;
803 enum machine_mode m1, m2;
804 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
809 /* If one input type is saturating, the result type is saturating. */
810 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
813 /* If both fixed-point types are unsigned, the result type is unsigned.
814 When mixing fixed-point and integer types, follow the sign of the
816 Otherwise, the result type is signed. */
817 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
818 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
819 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
820 && TYPE_UNSIGNED (t1))
821 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
822 && TYPE_UNSIGNED (t2)))
825 /* The result type is signed. */
828 /* If the input type is unsigned, we need to convert to the
830 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
832 enum mode_class mclass = (enum mode_class) 0;
833 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
835 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
839 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
841 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
843 enum mode_class mclass = (enum mode_class) 0;
844 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
846 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
850 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
854 if (code1 == FIXED_POINT_TYPE)
856 fbit1 = GET_MODE_FBIT (m1);
857 ibit1 = GET_MODE_IBIT (m1);
862 /* Signed integers need to subtract one sign bit. */
863 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
866 if (code2 == FIXED_POINT_TYPE)
868 fbit2 = GET_MODE_FBIT (m2);
869 ibit2 = GET_MODE_IBIT (m2);
874 /* Signed integers need to subtract one sign bit. */
875 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
878 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
879 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
880 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
884 /* Both real or both integers; use the one with greater precision. */
886 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
888 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
891 /* Same precision. Prefer long longs to longs to ints when the
892 same precision, following the C99 rules on integer type rank
893 (which are equivalent to the C90 rules for C90 types). */
895 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
896 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
897 return long_long_unsigned_type_node;
899 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
902 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
903 return long_long_unsigned_type_node;
905 return long_long_integer_type_node;
908 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
910 return long_unsigned_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
915 /* But preserve unsignedness from the other type,
916 since long cannot hold all the values of an unsigned int. */
917 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
918 return long_unsigned_type_node;
920 return long_integer_type_node;
923 /* Likewise, prefer long double to double even if same size. */
924 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
925 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
926 return long_double_type_node;
928 /* Otherwise prefer the unsigned one. */
930 if (TYPE_UNSIGNED (t1))
936 /* Wrapper around c_common_type that is used by c-common.c and other
937 front end optimizations that remove promotions. ENUMERAL_TYPEs
938 are allowed here and are converted to their compatible integer types.
939 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
940 preferably a non-Boolean type as the common type. */
942 common_type (tree t1, tree t2)
944 if (TREE_CODE (t1) == ENUMERAL_TYPE)
945 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
946 if (TREE_CODE (t2) == ENUMERAL_TYPE)
947 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
949 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
950 if (TREE_CODE (t1) == BOOLEAN_TYPE
951 && TREE_CODE (t2) == BOOLEAN_TYPE)
952 return boolean_type_node;
954 /* If either type is BOOLEAN_TYPE, then return the other. */
955 if (TREE_CODE (t1) == BOOLEAN_TYPE)
957 if (TREE_CODE (t2) == BOOLEAN_TYPE)
960 return c_common_type (t1, t2);
963 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
964 or various other operations. Return 2 if they are compatible
965 but a warning may be needed if you use them together. */
968 comptypes (tree type1, tree type2)
970 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
973 val = comptypes_internal (type1, type2, NULL, NULL);
974 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
979 /* Like comptypes, but if it returns non-zero because enum and int are
980 compatible, it sets *ENUM_AND_INT_P to true. */
983 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
985 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
988 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
989 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
994 /* Like comptypes, but if it returns nonzero for different types, it
995 sets *DIFFERENT_TYPES_P to true. */
998 comptypes_check_different_types (tree type1, tree type2,
999 bool *different_types_p)
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1004 val = comptypes_internal (type1, type2, NULL, different_types_p);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1010 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1011 or various other operations. Return 2 if they are compatible
1012 but a warning may be needed if you use them together. If
1013 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1014 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1015 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1016 NULL, and the types are compatible but different enough not to be
1017 permitted in C11 typedef redeclarations, then this sets
1018 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1019 false, but may or may not be set if the types are incompatible.
1020 This differs from comptypes, in that we don't free the seen
1024 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1025 bool *different_types_p)
1027 const_tree t1 = type1;
1028 const_tree t2 = type2;
1031 /* Suppress errors caused by previously reported errors. */
1033 if (t1 == t2 || !t1 || !t2
1034 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1037 /* Enumerated types are compatible with integer types, but this is
1038 not transitive: two enumerated types in the same translation unit
1039 are compatible with each other only if they are the same type. */
1041 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1043 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1044 if (TREE_CODE (t2) != VOID_TYPE)
1046 if (enum_and_int_p != NULL)
1047 *enum_and_int_p = true;
1048 if (different_types_p != NULL)
1049 *different_types_p = true;
1052 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1054 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1055 if (TREE_CODE (t1) != VOID_TYPE)
1057 if (enum_and_int_p != NULL)
1058 *enum_and_int_p = true;
1059 if (different_types_p != NULL)
1060 *different_types_p = true;
1067 /* Different classes of types can't be compatible. */
1069 if (TREE_CODE (t1) != TREE_CODE (t2))
1072 /* Qualifiers must match. C99 6.7.3p9 */
1074 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1077 /* Allow for two different type nodes which have essentially the same
1078 definition. Note that we already checked for equality of the type
1079 qualifiers (just above). */
1081 if (TREE_CODE (t1) != ARRAY_TYPE
1082 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1085 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1086 if (!(attrval = comp_type_attributes (t1, t2)))
1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1092 switch (TREE_CODE (t1))
1095 /* Do not remove mode or aliasing information. */
1096 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1097 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1099 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1100 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1101 enum_and_int_p, different_types_p));
1105 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1111 tree d1 = TYPE_DOMAIN (t1);
1112 tree d2 = TYPE_DOMAIN (t2);
1113 bool d1_variable, d2_variable;
1114 bool d1_zero, d2_zero;
1117 /* Target types must match incl. qualifiers. */
1118 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1119 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1121 different_types_p)))
1124 if (different_types_p != NULL
1125 && (d1 == 0) != (d2 == 0))
1126 *different_types_p = true;
1127 /* Sizes must match unless one is missing or variable. */
1128 if (d1 == 0 || d2 == 0 || d1 == d2)
1131 d1_zero = !TYPE_MAX_VALUE (d1);
1132 d2_zero = !TYPE_MAX_VALUE (d2);
1134 d1_variable = (!d1_zero
1135 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1136 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1137 d2_variable = (!d2_zero
1138 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1139 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1140 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1141 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1143 if (different_types_p != NULL
1144 && d1_variable != d2_variable)
1145 *different_types_p = true;
1146 if (d1_variable || d2_variable)
1148 if (d1_zero && d2_zero)
1150 if (d1_zero || d2_zero
1151 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1152 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1161 if (val != 1 && !same_translation_unit_p (t1, t2))
1163 tree a1 = TYPE_ATTRIBUTES (t1);
1164 tree a2 = TYPE_ATTRIBUTES (t2);
1166 if (! attribute_list_contained (a1, a2)
1167 && ! attribute_list_contained (a2, a1))
1171 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1173 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1179 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1180 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p, different_types_p));
1187 return attrval == 2 && val == 1 ? 2 : val;
1190 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1191 their qualifiers, except for named address spaces. If the pointers point to
1192 different named addresses, then we must determine if one address space is a
1193 subset of the other. */
1196 comp_target_types (location_t location, tree ttl, tree ttr)
1199 tree mvl = TREE_TYPE (ttl);
1200 tree mvr = TREE_TYPE (ttr);
1201 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1202 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1203 addr_space_t as_common;
1204 bool enum_and_int_p;
1206 /* Fail if pointers point to incompatible address spaces. */
1207 if (!addr_space_superset (asl, asr, &as_common))
1210 /* Do not lose qualifiers on element types of array types that are
1211 pointer targets by taking their TYPE_MAIN_VARIANT. */
1212 if (TREE_CODE (mvl) != ARRAY_TYPE)
1213 mvl = TYPE_MAIN_VARIANT (mvl);
1214 if (TREE_CODE (mvr) != ARRAY_TYPE)
1215 mvr = TYPE_MAIN_VARIANT (mvr);
1216 enum_and_int_p = false;
1217 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1220 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1222 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1223 warning_at (location, OPT_Wc___compat,
1224 "pointer target types incompatible in C++");
1229 /* Subroutines of `comptypes'. */
1231 /* Determine whether two trees derive from the same translation unit.
1232 If the CONTEXT chain ends in a null, that tree's context is still
1233 being parsed, so if two trees have context chains ending in null,
1234 they're in the same translation unit. */
1236 same_translation_unit_p (const_tree t1, const_tree t2)
1238 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1239 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1241 case tcc_declaration:
1242 t1 = DECL_CONTEXT (t1); break;
1244 t1 = TYPE_CONTEXT (t1); break;
1245 case tcc_exceptional:
1246 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1247 default: gcc_unreachable ();
1250 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1251 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1253 case tcc_declaration:
1254 t2 = DECL_CONTEXT (t2); break;
1256 t2 = TYPE_CONTEXT (t2); break;
1257 case tcc_exceptional:
1258 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1259 default: gcc_unreachable ();
1265 /* Allocate the seen two types, assuming that they are compatible. */
1267 static struct tagged_tu_seen_cache *
1268 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1270 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1271 tu->next = tagged_tu_seen_base;
1275 tagged_tu_seen_base = tu;
1277 /* The C standard says that two structures in different translation
1278 units are compatible with each other only if the types of their
1279 fields are compatible (among other things). We assume that they
1280 are compatible until proven otherwise when building the cache.
1281 An example where this can occur is:
1286 If we are comparing this against a similar struct in another TU,
1287 and did not assume they were compatible, we end up with an infinite
1293 /* Free the seen types until we get to TU_TIL. */
1296 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1298 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1299 while (tu != tu_til)
1301 const struct tagged_tu_seen_cache *const tu1
1302 = (const struct tagged_tu_seen_cache *) tu;
1304 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1306 tagged_tu_seen_base = tu_til;
1309 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1310 compatible. If the two types are not the same (which has been
1311 checked earlier), this can only happen when multiple translation
1312 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1313 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1314 comptypes_internal. */
1317 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1318 bool *enum_and_int_p, bool *different_types_p)
1321 bool needs_warning = false;
1323 /* We have to verify that the tags of the types are the same. This
1324 is harder than it looks because this may be a typedef, so we have
1325 to go look at the original type. It may even be a typedef of a
1327 In the case of compiler-created builtin structs the TYPE_DECL
1328 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1329 while (TYPE_NAME (t1)
1330 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1331 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1332 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1334 while (TYPE_NAME (t2)
1335 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1336 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1337 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1339 /* C90 didn't have the requirement that the two tags be the same. */
1340 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1343 /* C90 didn't say what happened if one or both of the types were
1344 incomplete; we choose to follow C99 rules here, which is that they
1346 if (TYPE_SIZE (t1) == NULL
1347 || TYPE_SIZE (t2) == NULL)
1351 const struct tagged_tu_seen_cache * tts_i;
1352 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1353 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1357 switch (TREE_CODE (t1))
1361 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1362 /* Speed up the case where the type values are in the same order. */
1363 tree tv1 = TYPE_VALUES (t1);
1364 tree tv2 = TYPE_VALUES (t2);
1371 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1373 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1375 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1382 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1386 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1392 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1398 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1400 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1402 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1413 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1414 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1420 /* Speed up the common case where the fields are in the same order. */
1421 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1422 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1426 if (DECL_NAME (s1) != DECL_NAME (s2))
1428 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1429 enum_and_int_p, different_types_p);
1431 if (result != 1 && !DECL_NAME (s1))
1439 needs_warning = true;
1441 if (TREE_CODE (s1) == FIELD_DECL
1442 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1443 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1451 tu->val = needs_warning ? 2 : 1;
1455 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1459 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1460 if (DECL_NAME (s1) == DECL_NAME (s2))
1464 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1468 if (result != 1 && !DECL_NAME (s1))
1476 needs_warning = true;
1478 if (TREE_CODE (s1) == FIELD_DECL
1479 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1480 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1492 tu->val = needs_warning ? 2 : 10;
1498 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1500 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1505 if (TREE_CODE (s1) != TREE_CODE (s2)
1506 || DECL_NAME (s1) != DECL_NAME (s2))
1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509 enum_and_int_p, different_types_p);
1513 needs_warning = true;
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1523 tu->val = needs_warning ? 2 : 1;
1532 /* Return 1 if two function types F1 and F2 are compatible.
1533 If either type specifies no argument types,
1534 the other must specify a fixed number of self-promoting arg types.
1535 Otherwise, if one type specifies only the number of arguments,
1536 the other must specify that number of self-promoting arg types.
1537 Otherwise, the argument types must match.
1538 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1541 function_types_compatible_p (const_tree f1, const_tree f2,
1542 bool *enum_and_int_p, bool *different_types_p)
1545 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1550 ret1 = TREE_TYPE (f1);
1551 ret2 = TREE_TYPE (f2);
1553 /* 'volatile' qualifiers on a function's return type used to mean
1554 the function is noreturn. */
1555 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1556 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1557 if (TYPE_VOLATILE (ret1))
1558 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1559 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1560 if (TYPE_VOLATILE (ret2))
1561 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1562 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1563 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1567 args1 = TYPE_ARG_TYPES (f1);
1568 args2 = TYPE_ARG_TYPES (f2);
1570 if (different_types_p != NULL
1571 && (args1 == 0) != (args2 == 0))
1572 *different_types_p = true;
1574 /* An unspecified parmlist matches any specified parmlist
1575 whose argument types don't need default promotions. */
1579 if (!self_promoting_args_p (args2))
1581 /* If one of these types comes from a non-prototype fn definition,
1582 compare that with the other type's arglist.
1583 If they don't match, ask for a warning (but no error). */
1584 if (TYPE_ACTUAL_ARG_TYPES (f1)
1585 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1586 enum_and_int_p, different_types_p))
1592 if (!self_promoting_args_p (args1))
1594 if (TYPE_ACTUAL_ARG_TYPES (f2)
1595 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1596 enum_and_int_p, different_types_p))
1601 /* Both types have argument lists: compare them and propagate results. */
1602 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1604 return val1 != 1 ? val1 : val;
1607 /* Check two lists of types for compatibility, returning 0 for
1608 incompatible, 1 for compatible, or 2 for compatible with
1609 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1610 comptypes_internal. */
1613 type_lists_compatible_p (const_tree args1, const_tree args2,
1614 bool *enum_and_int_p, bool *different_types_p)
1616 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1622 tree a1, mv1, a2, mv2;
1623 if (args1 == 0 && args2 == 0)
1625 /* If one list is shorter than the other,
1626 they fail to match. */
1627 if (args1 == 0 || args2 == 0)
1629 mv1 = a1 = TREE_VALUE (args1);
1630 mv2 = a2 = TREE_VALUE (args2);
1631 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1632 mv1 = TYPE_MAIN_VARIANT (mv1);
1633 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1634 mv2 = TYPE_MAIN_VARIANT (mv2);
1635 /* A null pointer instead of a type
1636 means there is supposed to be an argument
1637 but nothing is specified about what type it has.
1638 So match anything that self-promotes. */
1639 if (different_types_p != NULL
1640 && (a1 == 0) != (a2 == 0))
1641 *different_types_p = true;
1644 if (c_type_promotes_to (a2) != a2)
1649 if (c_type_promotes_to (a1) != a1)
1652 /* If one of the lists has an error marker, ignore this arg. */
1653 else if (TREE_CODE (a1) == ERROR_MARK
1654 || TREE_CODE (a2) == ERROR_MARK)
1656 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1657 different_types_p)))
1659 if (different_types_p != NULL)
1660 *different_types_p = true;
1661 /* Allow wait (union {union wait *u; int *i} *)
1662 and wait (union wait *) to be compatible. */
1663 if (TREE_CODE (a1) == UNION_TYPE
1664 && (TYPE_NAME (a1) == 0
1665 || TYPE_TRANSPARENT_AGGR (a1))
1666 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1667 && tree_int_cst_equal (TYPE_SIZE (a1),
1671 for (memb = TYPE_FIELDS (a1);
1672 memb; memb = DECL_CHAIN (memb))
1674 tree mv3 = TREE_TYPE (memb);
1675 if (mv3 && mv3 != error_mark_node
1676 && TREE_CODE (mv3) != ARRAY_TYPE)
1677 mv3 = TYPE_MAIN_VARIANT (mv3);
1678 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1685 else if (TREE_CODE (a2) == UNION_TYPE
1686 && (TYPE_NAME (a2) == 0
1687 || TYPE_TRANSPARENT_AGGR (a2))
1688 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1689 && tree_int_cst_equal (TYPE_SIZE (a2),
1693 for (memb = TYPE_FIELDS (a2);
1694 memb; memb = DECL_CHAIN (memb))
1696 tree mv3 = TREE_TYPE (memb);
1697 if (mv3 && mv3 != error_mark_node
1698 && TREE_CODE (mv3) != ARRAY_TYPE)
1699 mv3 = TYPE_MAIN_VARIANT (mv3);
1700 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1711 /* comptypes said ok, but record if it said to warn. */
1715 args1 = TREE_CHAIN (args1);
1716 args2 = TREE_CHAIN (args2);
1720 /* Compute the size to increment a pointer by. */
1723 c_size_in_bytes (const_tree type)
1725 enum tree_code code = TREE_CODE (type);
1727 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1728 return size_one_node;
1730 if (!COMPLETE_OR_VOID_TYPE_P (type))
1732 error ("arithmetic on pointer to an incomplete type");
1733 return size_one_node;
1736 /* Convert in case a char is more than one unit. */
1737 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1738 size_int (TYPE_PRECISION (char_type_node)
1742 /* Return either DECL or its known constant value (if it has one). */
1745 decl_constant_value (tree decl)
1747 if (/* Don't change a variable array bound or initial value to a constant
1748 in a place where a variable is invalid. Note that DECL_INITIAL
1749 isn't valid for a PARM_DECL. */
1750 current_function_decl != 0
1751 && TREE_CODE (decl) != PARM_DECL
1752 && !TREE_THIS_VOLATILE (decl)
1753 && TREE_READONLY (decl)
1754 && DECL_INITIAL (decl) != 0
1755 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1756 /* This is invalid if initial value is not constant.
1757 If it has either a function call, a memory reference,
1758 or a variable, then re-evaluating it could give different results. */
1759 && TREE_CONSTANT (DECL_INITIAL (decl))
1760 /* Check for cases where this is sub-optimal, even though valid. */
1761 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1762 return DECL_INITIAL (decl);
1766 /* Convert the array expression EXP to a pointer. */
1768 array_to_pointer_conversion (location_t loc, tree exp)
1770 tree orig_exp = exp;
1771 tree type = TREE_TYPE (exp);
1773 tree restype = TREE_TYPE (type);
1776 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1778 STRIP_TYPE_NOPS (exp);
1780 if (TREE_NO_WARNING (orig_exp))
1781 TREE_NO_WARNING (exp) = 1;
1783 ptrtype = build_pointer_type (restype);
1785 if (TREE_CODE (exp) == INDIRECT_REF)
1786 return convert (ptrtype, TREE_OPERAND (exp, 0));
1788 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1789 return convert (ptrtype, adr);
1792 /* Convert the function expression EXP to a pointer. */
1794 function_to_pointer_conversion (location_t loc, tree exp)
1796 tree orig_exp = exp;
1798 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1800 STRIP_TYPE_NOPS (exp);
1802 if (TREE_NO_WARNING (orig_exp))
1803 TREE_NO_WARNING (exp) = 1;
1805 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1808 /* Mark EXP as read, not just set, for set but not used -Wunused
1809 warning purposes. */
1812 mark_exp_read (tree exp)
1814 switch (TREE_CODE (exp))
1818 DECL_READ_P (exp) = 1;
1827 mark_exp_read (TREE_OPERAND (exp, 0));
1830 case C_MAYBE_CONST_EXPR:
1831 mark_exp_read (TREE_OPERAND (exp, 1));
1838 /* Perform the default conversion of arrays and functions to pointers.
1839 Return the result of converting EXP. For any other expression, just
1842 LOC is the location of the expression. */
1845 default_function_array_conversion (location_t loc, struct c_expr exp)
1847 tree orig_exp = exp.value;
1848 tree type = TREE_TYPE (exp.value);
1849 enum tree_code code = TREE_CODE (type);
1855 bool not_lvalue = false;
1856 bool lvalue_array_p;
1858 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1859 || CONVERT_EXPR_P (exp.value))
1860 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1862 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1864 exp.value = TREE_OPERAND (exp.value, 0);
1867 if (TREE_NO_WARNING (orig_exp))
1868 TREE_NO_WARNING (exp.value) = 1;
1870 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1871 if (!flag_isoc99 && !lvalue_array_p)
1873 /* Before C99, non-lvalue arrays do not decay to pointers.
1874 Normally, using such an array would be invalid; but it can
1875 be used correctly inside sizeof or as a statement expression.
1876 Thus, do not give an error here; an error will result later. */
1880 exp.value = array_to_pointer_conversion (loc, exp.value);
1884 exp.value = function_to_pointer_conversion (loc, exp.value);
1894 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1896 mark_exp_read (exp.value);
1897 return default_function_array_conversion (loc, exp);
1900 /* EXP is an expression of integer type. Apply the integer promotions
1901 to it and return the promoted value. */
1904 perform_integral_promotions (tree exp)
1906 tree type = TREE_TYPE (exp);
1907 enum tree_code code = TREE_CODE (type);
1909 gcc_assert (INTEGRAL_TYPE_P (type));
1911 /* Normally convert enums to int,
1912 but convert wide enums to something wider. */
1913 if (code == ENUMERAL_TYPE)
1915 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1916 TYPE_PRECISION (integer_type_node)),
1917 ((TYPE_PRECISION (type)
1918 >= TYPE_PRECISION (integer_type_node))
1919 && TYPE_UNSIGNED (type)));
1921 return convert (type, exp);
1924 /* ??? This should no longer be needed now bit-fields have their
1926 if (TREE_CODE (exp) == COMPONENT_REF
1927 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1928 /* If it's thinner than an int, promote it like a
1929 c_promoting_integer_type_p, otherwise leave it alone. */
1930 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1931 TYPE_PRECISION (integer_type_node)))
1932 return convert (integer_type_node, exp);
1934 if (c_promoting_integer_type_p (type))
1936 /* Preserve unsignedness if not really getting any wider. */
1937 if (TYPE_UNSIGNED (type)
1938 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1939 return convert (unsigned_type_node, exp);
1941 return convert (integer_type_node, exp);
1948 /* Perform default promotions for C data used in expressions.
1949 Enumeral types or short or char are converted to int.
1950 In addition, manifest constants symbols are replaced by their values. */
1953 default_conversion (tree exp)
1956 tree type = TREE_TYPE (exp);
1957 enum tree_code code = TREE_CODE (type);
1960 mark_exp_read (exp);
1962 /* Functions and arrays have been converted during parsing. */
1963 gcc_assert (code != FUNCTION_TYPE);
1964 if (code == ARRAY_TYPE)
1967 /* Constants can be used directly unless they're not loadable. */
1968 if (TREE_CODE (exp) == CONST_DECL)
1969 exp = DECL_INITIAL (exp);
1971 /* Strip no-op conversions. */
1973 STRIP_TYPE_NOPS (exp);
1975 if (TREE_NO_WARNING (orig_exp))
1976 TREE_NO_WARNING (exp) = 1;
1978 if (code == VOID_TYPE)
1980 error ("void value not ignored as it ought to be");
1981 return error_mark_node;
1984 exp = require_complete_type (exp);
1985 if (exp == error_mark_node)
1986 return error_mark_node;
1988 promoted_type = targetm.promoted_type (type);
1990 return convert (promoted_type, exp);
1992 if (INTEGRAL_TYPE_P (type))
1993 return perform_integral_promotions (exp);
1998 /* Look up COMPONENT in a structure or union TYPE.
2000 If the component name is not found, returns NULL_TREE. Otherwise,
2001 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2002 stepping down the chain to the component, which is in the last
2003 TREE_VALUE of the list. Normally the list is of length one, but if
2004 the component is embedded within (nested) anonymous structures or
2005 unions, the list steps down the chain to the component. */
2008 lookup_field (tree type, tree component)
2012 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2013 to the field elements. Use a binary search on this array to quickly
2014 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2015 will always be set for structures which have many elements. */
2017 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2020 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2022 field = TYPE_FIELDS (type);
2024 top = TYPE_LANG_SPECIFIC (type)->s->len;
2025 while (top - bot > 1)
2027 half = (top - bot + 1) >> 1;
2028 field = field_array[bot+half];
2030 if (DECL_NAME (field) == NULL_TREE)
2032 /* Step through all anon unions in linear fashion. */
2033 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2035 field = field_array[bot++];
2036 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2037 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2039 tree anon = lookup_field (TREE_TYPE (field), component);
2042 return tree_cons (NULL_TREE, field, anon);
2044 /* The Plan 9 compiler permits referring
2045 directly to an anonymous struct/union field
2046 using a typedef name. */
2047 if (flag_plan9_extensions
2048 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2049 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2051 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2057 /* Entire record is only anon unions. */
2061 /* Restart the binary search, with new lower bound. */
2065 if (DECL_NAME (field) == component)
2067 if (DECL_NAME (field) < component)
2073 if (DECL_NAME (field_array[bot]) == component)
2074 field = field_array[bot];
2075 else if (DECL_NAME (field) != component)
2080 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2082 if (DECL_NAME (field) == NULL_TREE
2083 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2084 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2086 tree anon = lookup_field (TREE_TYPE (field), component);
2089 return tree_cons (NULL_TREE, field, anon);
2091 /* The Plan 9 compiler permits referring directly to an
2092 anonymous struct/union field using a typedef
2094 if (flag_plan9_extensions
2095 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2096 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2097 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2102 if (DECL_NAME (field) == component)
2106 if (field == NULL_TREE)
2110 return tree_cons (NULL_TREE, field, NULL_TREE);
2113 /* Make an expression to refer to the COMPONENT field of structure or
2114 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2115 location of the COMPONENT_REF. */
2118 build_component_ref (location_t loc, tree datum, tree component)
2120 tree type = TREE_TYPE (datum);
2121 enum tree_code code = TREE_CODE (type);
2124 bool datum_lvalue = lvalue_p (datum);
2126 if (!objc_is_public (datum, component))
2127 return error_mark_node;
2129 /* Detect Objective-C property syntax object.property. */
2130 if (c_dialect_objc ()
2131 && (ref = objc_maybe_build_component_ref (datum, component)))
2134 /* See if there is a field or component with name COMPONENT. */
2136 if (code == RECORD_TYPE || code == UNION_TYPE)
2138 if (!COMPLETE_TYPE_P (type))
2140 c_incomplete_type_error (NULL_TREE, type);
2141 return error_mark_node;
2144 field = lookup_field (type, component);
2148 error_at (loc, "%qT has no member named %qE", type, component);
2149 return error_mark_node;
2152 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2153 This might be better solved in future the way the C++ front
2154 end does it - by giving the anonymous entities each a
2155 separate name and type, and then have build_component_ref
2156 recursively call itself. We can't do that here. */
2159 tree subdatum = TREE_VALUE (field);
2162 bool use_datum_quals;
2164 if (TREE_TYPE (subdatum) == error_mark_node)
2165 return error_mark_node;
2167 /* If this is an rvalue, it does not have qualifiers in C
2168 standard terms and we must avoid propagating such
2169 qualifiers down to a non-lvalue array that is then
2170 converted to a pointer. */
2171 use_datum_quals = (datum_lvalue
2172 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2174 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2175 if (use_datum_quals)
2176 quals |= TYPE_QUALS (TREE_TYPE (datum));
2177 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2179 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2181 SET_EXPR_LOCATION (ref, loc);
2182 if (TREE_READONLY (subdatum)
2183 || (use_datum_quals && TREE_READONLY (datum)))
2184 TREE_READONLY (ref) = 1;
2185 if (TREE_THIS_VOLATILE (subdatum)
2186 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2187 TREE_THIS_VOLATILE (ref) = 1;
2189 if (TREE_DEPRECATED (subdatum))
2190 warn_deprecated_use (subdatum, NULL_TREE);
2194 field = TREE_CHAIN (field);
2200 else if (code != ERROR_MARK)
2202 "request for member %qE in something not a structure or union",
2205 return error_mark_node;
2208 /* Given an expression PTR for a pointer, return an expression
2209 for the value pointed to.
2210 ERRORSTRING is the name of the operator to appear in error messages.
2212 LOC is the location to use for the generated tree. */
2215 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2217 tree pointer = default_conversion (ptr);
2218 tree type = TREE_TYPE (pointer);
2221 if (TREE_CODE (type) == POINTER_TYPE)
2223 if (CONVERT_EXPR_P (pointer)
2224 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2226 /* If a warning is issued, mark it to avoid duplicates from
2227 the backend. This only needs to be done at
2228 warn_strict_aliasing > 2. */
2229 if (warn_strict_aliasing > 2)
2230 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2231 type, TREE_OPERAND (pointer, 0)))
2232 TREE_NO_WARNING (pointer) = 1;
2235 if (TREE_CODE (pointer) == ADDR_EXPR
2236 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2237 == TREE_TYPE (type)))
2239 ref = TREE_OPERAND (pointer, 0);
2240 protected_set_expr_location (ref, loc);
2245 tree t = TREE_TYPE (type);
2247 ref = build1 (INDIRECT_REF, t, pointer);
2249 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2251 error_at (loc, "dereferencing pointer to incomplete type");
2252 return error_mark_node;
2254 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2255 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2257 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2258 so that we get the proper error message if the result is used
2259 to assign to. Also, &* is supposed to be a no-op.
2260 And ANSI C seems to specify that the type of the result
2261 should be the const type. */
2262 /* A de-reference of a pointer to const is not a const. It is valid
2263 to change it via some other pointer. */
2264 TREE_READONLY (ref) = TYPE_READONLY (t);
2265 TREE_SIDE_EFFECTS (ref)
2266 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2267 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2268 protected_set_expr_location (ref, loc);
2272 else if (TREE_CODE (pointer) != ERROR_MARK)
2273 invalid_indirection_error (loc, type, errstring);
2275 return error_mark_node;
2278 /* This handles expressions of the form "a[i]", which denotes
2281 This is logically equivalent in C to *(a+i), but we may do it differently.
2282 If A is a variable or a member, we generate a primitive ARRAY_REF.
2283 This avoids forcing the array out of registers, and can work on
2284 arrays that are not lvalues (for example, members of structures returned
2287 For vector types, allow vector[i] but not i[vector], and create
2288 *(((type*)&vectortype) + i) for the expression.
2290 LOC is the location to use for the returned expression. */
2293 build_array_ref (location_t loc, tree array, tree index)
2296 bool swapped = false;
2297 if (TREE_TYPE (array) == error_mark_node
2298 || TREE_TYPE (index) == error_mark_node)
2299 return error_mark_node;
2301 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2302 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2303 /* Allow vector[index] but not index[vector]. */
2304 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2307 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2308 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2311 "subscripted value is neither array nor pointer nor vector");
2313 return error_mark_node;
2321 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2323 error_at (loc, "array subscript is not an integer");
2324 return error_mark_node;
2327 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2329 error_at (loc, "subscripted value is pointer to function");
2330 return error_mark_node;
2333 /* ??? Existing practice has been to warn only when the char
2334 index is syntactically the index, not for char[array]. */
2336 warn_array_subscript_with_type_char (index);
2338 /* Apply default promotions *after* noticing character types. */
2339 index = default_conversion (index);
2341 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2343 /* For vector[index], convert the vector to a
2344 pointer of the underlying type. */
2345 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2347 tree type = TREE_TYPE (array);
2350 if (TREE_CODE (index) == INTEGER_CST)
2351 if (!host_integerp (index, 1)
2352 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2353 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2354 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2356 c_common_mark_addressable_vec (array);
2357 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2358 type = build_pointer_type (type);
2359 type1 = build_pointer_type (TREE_TYPE (array));
2360 array = build1 (ADDR_EXPR, type1, array);
2361 array = convert (type, array);
2364 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2368 /* An array that is indexed by a non-constant
2369 cannot be stored in a register; we must be able to do
2370 address arithmetic on its address.
2371 Likewise an array of elements of variable size. */
2372 if (TREE_CODE (index) != INTEGER_CST
2373 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2374 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2376 if (!c_mark_addressable (array))
2377 return error_mark_node;
2379 /* An array that is indexed by a constant value which is not within
2380 the array bounds cannot be stored in a register either; because we
2381 would get a crash in store_bit_field/extract_bit_field when trying
2382 to access a non-existent part of the register. */
2383 if (TREE_CODE (index) == INTEGER_CST
2384 && TYPE_DOMAIN (TREE_TYPE (array))
2385 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2387 if (!c_mark_addressable (array))
2388 return error_mark_node;
2394 while (TREE_CODE (foo) == COMPONENT_REF)
2395 foo = TREE_OPERAND (foo, 0);
2396 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2397 pedwarn (loc, OPT_pedantic,
2398 "ISO C forbids subscripting %<register%> array");
2399 else if (!flag_isoc99 && !lvalue_p (foo))
2400 pedwarn (loc, OPT_pedantic,
2401 "ISO C90 forbids subscripting non-lvalue array");
2404 type = TREE_TYPE (TREE_TYPE (array));
2405 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2406 /* Array ref is const/volatile if the array elements are
2407 or if the array is. */
2408 TREE_READONLY (rval)
2409 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2410 | TREE_READONLY (array));
2411 TREE_SIDE_EFFECTS (rval)
2412 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2413 | TREE_SIDE_EFFECTS (array));
2414 TREE_THIS_VOLATILE (rval)
2415 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2416 /* This was added by rms on 16 Nov 91.
2417 It fixes vol struct foo *a; a->elts[1]
2418 in an inline function.
2419 Hope it doesn't break something else. */
2420 | TREE_THIS_VOLATILE (array));
2421 ret = require_complete_type (rval);
2422 protected_set_expr_location (ret, loc);
2427 tree ar = default_conversion (array);
2429 if (ar == error_mark_node)
2432 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2433 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2435 return build_indirect_ref
2436 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2441 /* Build an external reference to identifier ID. FUN indicates
2442 whether this will be used for a function call. LOC is the source
2443 location of the identifier. This sets *TYPE to the type of the
2444 identifier, which is not the same as the type of the returned value
2445 for CONST_DECLs defined as enum constants. If the type of the
2446 identifier is not available, *TYPE is set to NULL. */
2448 build_external_ref (location_t loc, tree id, int fun, tree *type)
2451 tree decl = lookup_name (id);
2453 /* In Objective-C, an instance variable (ivar) may be preferred to
2454 whatever lookup_name() found. */
2455 decl = objc_lookup_ivar (decl, id);
2458 if (decl && decl != error_mark_node)
2461 *type = TREE_TYPE (ref);
2464 /* Implicit function declaration. */
2465 ref = implicitly_declare (loc, id);
2466 else if (decl == error_mark_node)
2467 /* Don't complain about something that's already been
2468 complained about. */
2469 return error_mark_node;
2472 undeclared_variable (loc, id);
2473 return error_mark_node;
2476 if (TREE_TYPE (ref) == error_mark_node)
2477 return error_mark_node;
2479 if (TREE_DEPRECATED (ref))
2480 warn_deprecated_use (ref, NULL_TREE);
2482 /* Recursive call does not count as usage. */
2483 if (ref != current_function_decl)
2485 TREE_USED (ref) = 1;
2488 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2490 if (!in_sizeof && !in_typeof)
2491 C_DECL_USED (ref) = 1;
2492 else if (DECL_INITIAL (ref) == 0
2493 && DECL_EXTERNAL (ref)
2494 && !TREE_PUBLIC (ref))
2495 record_maybe_used_decl (ref);
2498 if (TREE_CODE (ref) == CONST_DECL)
2500 used_types_insert (TREE_TYPE (ref));
2503 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2504 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2506 warning_at (loc, OPT_Wc___compat,
2507 ("enum constant defined in struct or union "
2508 "is not visible in C++"));
2509 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2512 ref = DECL_INITIAL (ref);
2513 TREE_CONSTANT (ref) = 1;
2515 else if (current_function_decl != 0
2516 && !DECL_FILE_SCOPE_P (current_function_decl)
2517 && (TREE_CODE (ref) == VAR_DECL
2518 || TREE_CODE (ref) == PARM_DECL
2519 || TREE_CODE (ref) == FUNCTION_DECL))
2521 tree context = decl_function_context (ref);
2523 if (context != 0 && context != current_function_decl)
2524 DECL_NONLOCAL (ref) = 1;
2526 /* C99 6.7.4p3: An inline definition of a function with external
2527 linkage ... shall not contain a reference to an identifier with
2528 internal linkage. */
2529 else if (current_function_decl != 0
2530 && DECL_DECLARED_INLINE_P (current_function_decl)
2531 && DECL_EXTERNAL (current_function_decl)
2532 && VAR_OR_FUNCTION_DECL_P (ref)
2533 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2534 && ! TREE_PUBLIC (ref)
2535 && DECL_CONTEXT (ref) != current_function_decl)
2536 record_inline_static (loc, current_function_decl, ref,
2542 /* Record details of decls possibly used inside sizeof or typeof. */
2543 struct maybe_used_decl
2547 /* The level seen at (in_sizeof + in_typeof). */
2549 /* The next one at this level or above, or NULL. */
2550 struct maybe_used_decl *next;
2553 static struct maybe_used_decl *maybe_used_decls;
2555 /* Record that DECL, an undefined static function reference seen
2556 inside sizeof or typeof, might be used if the operand of sizeof is
2557 a VLA type or the operand of typeof is a variably modified
2561 record_maybe_used_decl (tree decl)
2563 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2565 t->level = in_sizeof + in_typeof;
2566 t->next = maybe_used_decls;
2567 maybe_used_decls = t;
2570 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2571 USED is false, just discard them. If it is true, mark them used
2572 (if no longer inside sizeof or typeof) or move them to the next
2573 level up (if still inside sizeof or typeof). */
2576 pop_maybe_used (bool used)
2578 struct maybe_used_decl *p = maybe_used_decls;
2579 int cur_level = in_sizeof + in_typeof;
2580 while (p && p->level > cur_level)
2585 C_DECL_USED (p->decl) = 1;
2587 p->level = cur_level;
2591 if (!used || cur_level == 0)
2592 maybe_used_decls = p;
2595 /* Return the result of sizeof applied to EXPR. */
2598 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2601 if (expr.value == error_mark_node)
2603 ret.value = error_mark_node;
2604 ret.original_code = ERROR_MARK;
2605 ret.original_type = NULL;
2606 pop_maybe_used (false);
2610 bool expr_const_operands = true;
2611 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2612 &expr_const_operands);
2613 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2614 ret.original_code = ERROR_MARK;
2615 ret.original_type = NULL;
2616 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2618 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2619 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2620 folded_expr, ret.value);
2621 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2622 SET_EXPR_LOCATION (ret.value, loc);
2624 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2629 /* Return the result of sizeof applied to T, a structure for the type
2630 name passed to sizeof (rather than the type itself). LOC is the
2631 location of the original expression. */
2634 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2638 tree type_expr = NULL_TREE;
2639 bool type_expr_const = true;
2640 type = groktypename (t, &type_expr, &type_expr_const);
2641 ret.value = c_sizeof (loc, type);
2642 ret.original_code = ERROR_MARK;
2643 ret.original_type = NULL;
2644 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2645 && c_vla_type_p (type))
2647 /* If the type is a [*] array, it is a VLA but is represented as
2648 having a size of zero. In such a case we must ensure that
2649 the result of sizeof does not get folded to a constant by
2650 c_fully_fold, because if the size is evaluated the result is
2651 not constant and so constraints on zero or negative size
2652 arrays must not be applied when this sizeof call is inside
2653 another array declarator. */
2655 type_expr = integer_zero_node;
2656 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2657 type_expr, ret.value);
2658 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2660 pop_maybe_used (type != error_mark_node
2661 ? C_TYPE_VARIABLE_SIZE (type) : false);
2665 /* Build a function call to function FUNCTION with parameters PARAMS.
2666 The function call is at LOC.
2667 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2668 TREE_VALUE of each node is a parameter-expression.
2669 FUNCTION's data type may be a function type or a pointer-to-function. */
2672 build_function_call (location_t loc, tree function, tree params)
2677 vec = VEC_alloc (tree, gc, list_length (params));
2678 for (; params; params = TREE_CHAIN (params))
2679 VEC_quick_push (tree, vec, TREE_VALUE (params));
2680 ret = build_function_call_vec (loc, function, vec, NULL);
2681 VEC_free (tree, gc, vec);
2685 /* Build a function call to function FUNCTION with parameters PARAMS.
2686 ORIGTYPES, if not NULL, is a vector of types; each element is
2687 either NULL or the original type of the corresponding element in
2688 PARAMS. The original type may differ from TREE_TYPE of the
2689 parameter for enums. FUNCTION's data type may be a function type
2690 or pointer-to-function. This function changes the elements of
2694 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2695 VEC(tree,gc) *origtypes)
2697 tree fntype, fundecl = 0;
2698 tree name = NULL_TREE, result;
2704 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2705 STRIP_TYPE_NOPS (function);
2707 /* Convert anything with function type to a pointer-to-function. */
2708 if (TREE_CODE (function) == FUNCTION_DECL)
2710 /* Implement type-directed function overloading for builtins.
2711 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2712 handle all the type checking. The result is a complete expression
2713 that implements this function call. */
2714 tem = resolve_overloaded_builtin (loc, function, params);
2718 name = DECL_NAME (function);
2721 tm_malloc_replacement (function);
2723 /* Atomic functions have type checking/casting already done. They are
2724 often rewritten and don't match the original parameter list. */
2725 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2728 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2729 function = function_to_pointer_conversion (loc, function);
2731 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2732 expressions, like those used for ObjC messenger dispatches. */
2733 if (!VEC_empty (tree, params))
2734 function = objc_rewrite_function_call (function,
2735 VEC_index (tree, params, 0));
2737 function = c_fully_fold (function, false, NULL);
2739 fntype = TREE_TYPE (function);
2741 if (TREE_CODE (fntype) == ERROR_MARK)
2742 return error_mark_node;
2744 if (!(TREE_CODE (fntype) == POINTER_TYPE
2745 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2747 error_at (loc, "called object %qE is not a function", function);
2748 return error_mark_node;
2751 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2752 current_function_returns_abnormally = 1;
2754 /* fntype now gets the type of function pointed to. */
2755 fntype = TREE_TYPE (fntype);
2757 /* Convert the parameters to the types declared in the
2758 function prototype, or apply default promotions. */
2760 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2763 return error_mark_node;
2765 /* Check that the function is called through a compatible prototype.
2766 If it is not, replace the call by a trap, wrapped up in a compound
2767 expression if necessary. This has the nice side-effect to prevent
2768 the tree-inliner from generating invalid assignment trees which may
2769 blow up in the RTL expander later. */
2770 if (CONVERT_EXPR_P (function)
2771 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2772 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2773 && !comptypes (fntype, TREE_TYPE (tem)))
2775 tree return_type = TREE_TYPE (fntype);
2776 tree trap = build_function_call (loc,
2777 builtin_decl_explicit (BUILT_IN_TRAP),
2781 /* This situation leads to run-time undefined behavior. We can't,
2782 therefore, simply error unless we can prove that all possible
2783 executions of the program must execute the code. */
2784 if (warning_at (loc, 0, "function called through a non-compatible type"))
2785 /* We can, however, treat "undefined" any way we please.
2786 Call abort to encourage the user to fix the program. */
2787 inform (loc, "if this code is reached, the program will abort");
2788 /* Before the abort, allow the function arguments to exit or
2790 for (i = 0; i < nargs; i++)
2791 trap = build2 (COMPOUND_EXPR, void_type_node,
2792 VEC_index (tree, params, i), trap);
2794 if (VOID_TYPE_P (return_type))
2796 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2798 "function with qualified void return type called");
2805 if (AGGREGATE_TYPE_P (return_type))
2806 rhs = build_compound_literal (loc, return_type,
2807 build_constructor (return_type, 0),
2810 rhs = build_zero_cst (return_type);
2812 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2817 argarray = VEC_address (tree, params);
2819 /* Check that arguments to builtin functions match the expectations. */
2821 && DECL_BUILT_IN (fundecl)
2822 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2823 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2824 return error_mark_node;
2826 /* Check that the arguments to the function are valid. */
2827 check_function_arguments (fntype, nargs, argarray);
2829 if (name != NULL_TREE
2830 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2832 if (require_constant_value)
2834 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2835 function, nargs, argarray);
2837 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2838 function, nargs, argarray);
2839 if (TREE_CODE (result) == NOP_EXPR
2840 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2841 STRIP_TYPE_NOPS (result);
2844 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2845 function, nargs, argarray);
2847 if (VOID_TYPE_P (TREE_TYPE (result)))
2849 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2851 "function with qualified void return type called");
2854 return require_complete_type (result);
2857 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2858 and have vector types, V0 has the same type as V1, and the number of
2859 elements of V0, V1, MASK is the same.
2861 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2862 called with two arguments. In this case implementation passes the
2863 first argument twice in order to share the same tree code. This fact
2864 could enable the mask-values being twice the vector length. This is
2865 an implementation accident and this semantics is not guaranteed to
2868 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2872 bool maybe_const = false;
2873 bool two_arguments = false;
2875 if (v1 == NULL_TREE)
2877 two_arguments = true;
2881 if (v0 == error_mark_node || v1 == error_mark_node
2882 || mask == error_mark_node)
2883 return error_mark_node;
2885 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2886 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2888 error_at (loc, "__builtin_shuffle last argument must "
2889 "be an integer vector");
2890 return error_mark_node;
2893 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2894 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2896 error_at (loc, "__builtin_shuffle arguments must be vectors");
2897 return error_mark_node;
2900 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2902 error_at (loc, "__builtin_shuffle argument vectors must be of "
2904 return error_mark_node;
2907 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2908 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2909 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2910 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2912 error_at (loc, "__builtin_shuffle number of elements of the "
2913 "argument vector(s) and the mask vector should "
2915 return error_mark_node;
2918 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2919 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2921 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2922 "must have the same size as inner type of the mask");
2923 return error_mark_node;
2926 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2927 v0 = c_fully_fold (v0, false, &maybe_const);
2928 wrap &= maybe_const;
2931 v1 = v0 = save_expr (v0);
2934 v1 = c_fully_fold (v1, false, &maybe_const);
2935 wrap &= maybe_const;
2938 mask = c_fully_fold (mask, false, &maybe_const);
2939 wrap &= maybe_const;
2941 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2944 ret = c_wrap_maybe_const (ret, true);
2949 /* Convert the argument expressions in the vector VALUES
2950 to the types in the list TYPELIST.
2952 If TYPELIST is exhausted, or when an element has NULL as its type,
2953 perform the default conversions.
2955 ORIGTYPES is the original types of the expressions in VALUES. This
2956 holds the type of enum values which have been converted to integral
2957 types. It may be NULL.
2959 FUNCTION is a tree for the called function. It is used only for
2960 error messages, where it is formatted with %qE.
2962 This is also where warnings about wrong number of args are generated.
2964 Returns the actual number of arguments processed (which may be less
2965 than the length of VALUES in some error situations), or -1 on
2969 convert_arguments (tree typelist, VEC(tree,gc) *values,
2970 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2973 unsigned int parmnum;
2974 bool error_args = false;
2975 const bool type_generic = fundecl
2976 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2977 bool type_generic_remove_excess_precision = false;
2980 /* Change pointer to function to the function itself for
2982 if (TREE_CODE (function) == ADDR_EXPR
2983 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2984 function = TREE_OPERAND (function, 0);
2986 /* Handle an ObjC selector specially for diagnostics. */
2987 selector = objc_message_selector ();
2989 /* For type-generic built-in functions, determine whether excess
2990 precision should be removed (classification) or not
2993 && DECL_BUILT_IN (fundecl)
2994 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2996 switch (DECL_FUNCTION_CODE (fundecl))
2998 case BUILT_IN_ISFINITE:
2999 case BUILT_IN_ISINF:
3000 case BUILT_IN_ISINF_SIGN:
3001 case BUILT_IN_ISNAN:
3002 case BUILT_IN_ISNORMAL:
3003 case BUILT_IN_FPCLASSIFY:
3004 type_generic_remove_excess_precision = true;
3008 type_generic_remove_excess_precision = false;
3013 /* Scan the given expressions and types, producing individual
3014 converted arguments. */
3016 for (typetail = typelist, parmnum = 0;
3017 VEC_iterate (tree, values, parmnum, val);
3020 tree type = typetail ? TREE_VALUE (typetail) : 0;
3021 tree valtype = TREE_TYPE (val);
3022 tree rname = function;
3023 int argnum = parmnum + 1;
3024 const char *invalid_func_diag;
3025 bool excess_precision = false;
3029 if (type == void_type_node)
3032 error_at (input_location,
3033 "too many arguments to method %qE", selector);
3035 error_at (input_location,
3036 "too many arguments to function %qE", function);
3038 if (fundecl && !DECL_BUILT_IN (fundecl))
3039 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3043 if (selector && argnum > 2)
3049 npc = null_pointer_constant_p (val);
3051 /* If there is excess precision and a prototype, convert once to
3052 the required type rather than converting via the semantic
3053 type. Likewise without a prototype a float value represented
3054 as long double should be converted once to double. But for
3055 type-generic classification functions excess precision must
3057 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3058 && (type || !type_generic || !type_generic_remove_excess_precision))
3060 val = TREE_OPERAND (val, 0);
3061 excess_precision = true;
3063 val = c_fully_fold (val, false, NULL);
3064 STRIP_TYPE_NOPS (val);
3066 val = require_complete_type (val);
3070 /* Formal parm type is specified by a function prototype. */
3072 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3074 error ("type of formal parameter %d is incomplete", parmnum + 1);
3081 /* Optionally warn about conversions that
3082 differ from the default conversions. */
3083 if (warn_traditional_conversion || warn_traditional)
3085 unsigned int formal_prec = TYPE_PRECISION (type);
3087 if (INTEGRAL_TYPE_P (type)
3088 && TREE_CODE (valtype) == REAL_TYPE)
3089 warning (0, "passing argument %d of %qE as integer "
3090 "rather than floating due to prototype",
3092 if (INTEGRAL_TYPE_P (type)
3093 && TREE_CODE (valtype) == COMPLEX_TYPE)
3094 warning (0, "passing argument %d of %qE as integer "
3095 "rather than complex due to prototype",
3097 else if (TREE_CODE (type) == COMPLEX_TYPE
3098 && TREE_CODE (valtype) == REAL_TYPE)
3099 warning (0, "passing argument %d of %qE as complex "
3100 "rather than floating due to prototype",
3102 else if (TREE_CODE (type) == REAL_TYPE
3103 && INTEGRAL_TYPE_P (valtype))
3104 warning (0, "passing argument %d of %qE as floating "
3105 "rather than integer due to prototype",
3107 else if (TREE_CODE (type) == COMPLEX_TYPE
3108 && INTEGRAL_TYPE_P (valtype))
3109 warning (0, "passing argument %d of %qE as complex "
3110 "rather than integer due to prototype",
3112 else if (TREE_CODE (type) == REAL_TYPE
3113 && TREE_CODE (valtype) == COMPLEX_TYPE)
3114 warning (0, "passing argument %d of %qE as floating "
3115 "rather than complex due to prototype",
3117 /* ??? At some point, messages should be written about
3118 conversions between complex types, but that's too messy
3120 else if (TREE_CODE (type) == REAL_TYPE
3121 && TREE_CODE (valtype) == REAL_TYPE)
3123 /* Warn if any argument is passed as `float',
3124 since without a prototype it would be `double'. */
3125 if (formal_prec == TYPE_PRECISION (float_type_node)
3126 && type != dfloat32_type_node)
3127 warning (0, "passing argument %d of %qE as %<float%> "
3128 "rather than %<double%> due to prototype",
3131 /* Warn if mismatch between argument and prototype
3132 for decimal float types. Warn of conversions with
3133 binary float types and of precision narrowing due to
3135 else if (type != valtype
3136 && (type == dfloat32_type_node
3137 || type == dfloat64_type_node
3138 || type == dfloat128_type_node
3139 || valtype == dfloat32_type_node
3140 || valtype == dfloat64_type_node
3141 || valtype == dfloat128_type_node)
3143 <= TYPE_PRECISION (valtype)
3144 || (type == dfloat128_type_node
3146 != dfloat64_type_node
3148 != dfloat32_type_node)))
3149 || (type == dfloat64_type_node
3151 != dfloat32_type_node))))
3152 warning (0, "passing argument %d of %qE as %qT "
3153 "rather than %qT due to prototype",
3154 argnum, rname, type, valtype);
3157 /* Detect integer changing in width or signedness.
3158 These warnings are only activated with
3159 -Wtraditional-conversion, not with -Wtraditional. */
3160 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3161 && INTEGRAL_TYPE_P (valtype))
3163 tree would_have_been = default_conversion (val);
3164 tree type1 = TREE_TYPE (would_have_been);
3166 if (TREE_CODE (type) == ENUMERAL_TYPE
3167 && (TYPE_MAIN_VARIANT (type)
3168 == TYPE_MAIN_VARIANT (valtype)))
3169 /* No warning if function asks for enum
3170 and the actual arg is that enum type. */
3172 else if (formal_prec != TYPE_PRECISION (type1))
3173 warning (OPT_Wtraditional_conversion,
3174 "passing argument %d of %qE "
3175 "with different width due to prototype",
3177 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3179 /* Don't complain if the formal parameter type
3180 is an enum, because we can't tell now whether
3181 the value was an enum--even the same enum. */
3182 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3184 else if (TREE_CODE (val) == INTEGER_CST
3185 && int_fits_type_p (val, type))
3186 /* Change in signedness doesn't matter
3187 if a constant value is unaffected. */
3189 /* If the value is extended from a narrower
3190 unsigned type, it doesn't matter whether we
3191 pass it as signed or unsigned; the value
3192 certainly is the same either way. */
3193 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3194 && TYPE_UNSIGNED (valtype))
3196 else if (TYPE_UNSIGNED (type))
3197 warning (OPT_Wtraditional_conversion,
3198 "passing argument %d of %qE "
3199 "as unsigned due to prototype",
3202 warning (OPT_Wtraditional_conversion,
3203 "passing argument %d of %qE "
3204 "as signed due to prototype", argnum, rname);
3208 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3209 sake of better warnings from convert_and_check. */
3210 if (excess_precision)
3211 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3212 origtype = (origtypes == NULL
3214 : VEC_index (tree, origtypes, parmnum));
3215 parmval = convert_for_assignment (input_location, type, val,
3216 origtype, ic_argpass, npc,
3220 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3221 && INTEGRAL_TYPE_P (type)
3222 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3223 parmval = default_conversion (parmval);
3226 else if (TREE_CODE (valtype) == REAL_TYPE
3227 && (TYPE_PRECISION (valtype)
3228 < TYPE_PRECISION (double_type_node))
3229 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3235 /* Convert `float' to `double'. */
3236 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3237 warning (OPT_Wdouble_promotion,
3238 "implicit conversion from %qT to %qT when passing "
3239 "argument to function",
3240 valtype, double_type_node);
3241 parmval = convert (double_type_node, val);
3244 else if (excess_precision && !type_generic)
3245 /* A "double" argument with excess precision being passed
3246 without a prototype or in variable arguments. */
3247 parmval = convert (valtype, val);
3248 else if ((invalid_func_diag =
3249 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3251 error (invalid_func_diag);
3255 /* Convert `short' and `char' to full-size `int'. */
3256 parmval = default_conversion (val);
3258 VEC_replace (tree, values, parmnum, parmval);
3259 if (parmval == error_mark_node)
3263 typetail = TREE_CHAIN (typetail);
3266 gcc_assert (parmnum == VEC_length (tree, values));
3268 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3270 error_at (input_location,
3271 "too few arguments to function %qE", function);
3272 if (fundecl && !DECL_BUILT_IN (fundecl))
3273 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3277 return error_args ? -1 : (int) parmnum;
3280 /* This is the entry point used by the parser to build unary operators
3281 in the input. CODE, a tree_code, specifies the unary operator, and
3282 ARG is the operand. For unary plus, the C parser currently uses
3283 CONVERT_EXPR for code.
3285 LOC is the location to use for the tree generated.
3289 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3291 struct c_expr result;
3293 result.value = build_unary_op (loc, code, arg.value, 0);
3294 result.original_code = code;
3295 result.original_type = NULL;
3297 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3298 overflow_warning (loc, result.value);
3303 /* This is the entry point used by the parser to build binary operators
3304 in the input. CODE, a tree_code, specifies the binary operator, and
3305 ARG1 and ARG2 are the operands. In addition to constructing the
3306 expression, we check for operands that were written with other binary
3307 operators in a way that is likely to confuse the user.
3309 LOCATION is the location of the binary operator. */
3312 parser_build_binary_op (location_t location, enum tree_code code,
3313 struct c_expr arg1, struct c_expr arg2)
3315 struct c_expr result;
3317 enum tree_code code1 = arg1.original_code;
3318 enum tree_code code2 = arg2.original_code;
3319 tree type1 = (arg1.original_type
3320 ? arg1.original_type
3321 : TREE_TYPE (arg1.value));
3322 tree type2 = (arg2.original_type
3323 ? arg2.original_type
3324 : TREE_TYPE (arg2.value));
3326 result.value = build_binary_op (location, code,
3327 arg1.value, arg2.value, 1);
3328 result.original_code = code;
3329 result.original_type = NULL;
3331 if (TREE_CODE (result.value) == ERROR_MARK)
3334 if (location != UNKNOWN_LOCATION)
3335 protected_set_expr_location (result.value, location);
3337 /* Check for cases such as x+y<<z which users are likely
3339 if (warn_parentheses)
3340 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3342 if (warn_logical_op)
3343 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3344 code1, arg1.value, code2, arg2.value);
3346 /* Warn about comparisons against string literals, with the exception
3347 of testing for equality or inequality of a string literal with NULL. */
3348 if (code == EQ_EXPR || code == NE_EXPR)
3350 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3351 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3352 warning_at (location, OPT_Waddress,
3353 "comparison with string literal results in unspecified behavior");
3355 else if (TREE_CODE_CLASS (code) == tcc_comparison
3356 && (code1 == STRING_CST || code2 == STRING_CST))
3357 warning_at (location, OPT_Waddress,
3358 "comparison with string literal results in unspecified behavior");
3360 if (TREE_OVERFLOW_P (result.value)
3361 && !TREE_OVERFLOW_P (arg1.value)
3362 && !TREE_OVERFLOW_P (arg2.value))
3363 overflow_warning (location, result.value);
3365 /* Warn about comparisons of different enum types. */
3366 if (warn_enum_compare
3367 && TREE_CODE_CLASS (code) == tcc_comparison
3368 && TREE_CODE (type1) == ENUMERAL_TYPE
3369 && TREE_CODE (type2) == ENUMERAL_TYPE
3370 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3371 warning_at (location, OPT_Wenum_compare,
3372 "comparison between %qT and %qT",
3378 /* Return a tree for the difference of pointers OP0 and OP1.
3379 The resulting tree has type int. */
3382 pointer_diff (location_t loc, tree op0, tree op1)
3384 tree restype = ptrdiff_type_node;
3385 tree result, inttype;
3387 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3388 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3389 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3390 tree con0, con1, lit0, lit1;
3391 tree orig_op1 = op1;
3393 /* If the operands point into different address spaces, we need to
3394 explicitly convert them to pointers into the common address space
3395 before we can subtract the numerical address values. */
3398 addr_space_t as_common;
3401 /* Determine the common superset address space. This is guaranteed
3402 to exist because the caller verified that comp_target_types
3403 returned non-zero. */
3404 if (!addr_space_superset (as0, as1, &as_common))
3407 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3408 op0 = convert (common_type, op0);
3409 op1 = convert (common_type, op1);
3412 /* Determine integer type to perform computations in. This will usually
3413 be the same as the result type (ptrdiff_t), but may need to be a wider
3414 type if pointers for the address space are wider than ptrdiff_t. */
3415 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3416 inttype = lang_hooks.types.type_for_size
3417 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3422 if (TREE_CODE (target_type) == VOID_TYPE)
3423 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3424 "pointer of type %<void *%> used in subtraction");
3425 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3426 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3427 "pointer to a function used in subtraction");
3429 /* If the conversion to ptrdiff_type does anything like widening or
3430 converting a partial to an integral mode, we get a convert_expression
3431 that is in the way to do any simplifications.
3432 (fold-const.c doesn't know that the extra bits won't be needed.
3433 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3434 different mode in place.)
3435 So first try to find a common term here 'by hand'; we want to cover
3436 at least the cases that occur in legal static initializers. */
3437 if (CONVERT_EXPR_P (op0)
3438 && (TYPE_PRECISION (TREE_TYPE (op0))
3439 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3440 con0 = TREE_OPERAND (op0, 0);
3443 if (CONVERT_EXPR_P (op1)
3444 && (TYPE_PRECISION (TREE_TYPE (op1))
3445 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3446 con1 = TREE_OPERAND (op1, 0);
3450 if (TREE_CODE (con0) == PLUS_EXPR)
3452 lit0 = TREE_OPERAND (con0, 1);
3453 con0 = TREE_OPERAND (con0, 0);
3456 lit0 = integer_zero_node;
3458 if (TREE_CODE (con1) == PLUS_EXPR)
3460 lit1 = TREE_OPERAND (con1, 1);
3461 con1 = TREE_OPERAND (con1, 0);
3464 lit1 = integer_zero_node;
3466 if (operand_equal_p (con0, con1, 0))
3473 /* First do the subtraction as integers;
3474 then drop through to build the divide operator.
3475 Do not do default conversions on the minus operator
3476 in case restype is a short type. */
3478 op0 = build_binary_op (loc,
3479 MINUS_EXPR, convert (inttype, op0),
3480 convert (inttype, op1), 0);
3481 /* This generates an error if op1 is pointer to incomplete type. */
3482 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3483 error_at (loc, "arithmetic on pointer to an incomplete type");
3485 /* This generates an error if op0 is pointer to incomplete type. */
3486 op1 = c_size_in_bytes (target_type);
3488 /* Divide by the size, in easiest possible way. */
3489 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3490 op0, convert (inttype, op1));
3492 /* Convert to final result type if necessary. */
3493 return convert (restype, result);
3496 /* Construct and perhaps optimize a tree representation
3497 for a unary operation. CODE, a tree_code, specifies the operation
3498 and XARG is the operand.
3499 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3500 the default promotions (such as from short to int).
3501 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3502 allows non-lvalues; this is only used to handle conversion of non-lvalue
3503 arrays to pointers in C99.
3505 LOCATION is the location of the operator. */
3508 build_unary_op (location_t location,
3509 enum tree_code code, tree xarg, int flag)
3511 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3514 enum tree_code typecode;
3516 tree ret = error_mark_node;
3517 tree eptype = NULL_TREE;
3518 int noconvert = flag;
3519 const char *invalid_op_diag;
3522 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3524 arg = remove_c_maybe_const_expr (arg);
3526 if (code != ADDR_EXPR)
3527 arg = require_complete_type (arg);
3529 typecode = TREE_CODE (TREE_TYPE (arg));
3530 if (typecode == ERROR_MARK)
3531 return error_mark_node;
3532 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3533 typecode = INTEGER_TYPE;
3535 if ((invalid_op_diag
3536 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3538 error_at (location, invalid_op_diag);
3539 return error_mark_node;
3542 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3544 eptype = TREE_TYPE (arg);
3545 arg = TREE_OPERAND (arg, 0);
3551 /* This is used for unary plus, because a CONVERT_EXPR
3552 is enough to prevent anybody from looking inside for
3553 associativity, but won't generate any code. */
3554 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3555 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3556 || typecode == VECTOR_TYPE))
3558 error_at (location, "wrong type argument to unary plus");
3559 return error_mark_node;
3561 else if (!noconvert)
3562 arg = default_conversion (arg);
3563 arg = non_lvalue_loc (location, arg);
3567 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3568 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3569 || typecode == VECTOR_TYPE))
3571 error_at (location, "wrong type argument to unary minus");
3572 return error_mark_node;
3574 else if (!noconvert)
3575 arg = default_conversion (arg);
3579 /* ~ works on integer types and non float vectors. */
3580 if (typecode == INTEGER_TYPE
3581 || (typecode == VECTOR_TYPE
3582 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3585 arg = default_conversion (arg);
3587 else if (typecode == COMPLEX_TYPE)
3590 pedwarn (location, OPT_pedantic,
3591 "ISO C does not support %<~%> for complex conjugation");
3593 arg = default_conversion (arg);
3597 error_at (location, "wrong type argument to bit-complement");
3598 return error_mark_node;
3603 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3605 error_at (location, "wrong type argument to abs");
3606 return error_mark_node;
3608 else if (!noconvert)
3609 arg = default_conversion (arg);
3613 /* Conjugating a real value is a no-op, but allow it anyway. */
3614 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3615 || typecode == COMPLEX_TYPE))
3617 error_at (location, "wrong type argument to conjugation");
3618 return error_mark_node;
3620 else if (!noconvert)
3621 arg = default_conversion (arg);
3624 case TRUTH_NOT_EXPR:
3625 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3626 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3627 && typecode != COMPLEX_TYPE)
3630 "wrong type argument to unary exclamation mark");
3631 return error_mark_node;
3633 arg = c_objc_common_truthvalue_conversion (location, arg);
3634 ret = invert_truthvalue_loc (location, arg);
3635 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3636 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3637 location = EXPR_LOCATION (ret);
3638 goto return_build_unary_op;
3642 ret = build_real_imag_expr (location, code, arg);
3643 if (ret == error_mark_node)
3644 return error_mark_node;
3645 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3646 eptype = TREE_TYPE (eptype);
3647 goto return_build_unary_op;
3649 case PREINCREMENT_EXPR:
3650 case POSTINCREMENT_EXPR:
3651 case PREDECREMENT_EXPR:
3652 case POSTDECREMENT_EXPR:
3654 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3656 tree inner = build_unary_op (location, code,
3657 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3658 if (inner == error_mark_node)
3659 return error_mark_node;
3660 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3661 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3662 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3663 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3664 goto return_build_unary_op;
3667 /* Complain about anything that is not a true lvalue. In
3668 Objective-C, skip this check for property_refs. */
3669 if (!objc_is_property_ref (arg)
3670 && !lvalue_or_else (location,
3671 arg, ((code == PREINCREMENT_EXPR
3672 || code == POSTINCREMENT_EXPR)
3675 return error_mark_node;
3677 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3679 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3680 warning_at (location, OPT_Wc___compat,
3681 "increment of enumeration value is invalid in C++");
3683 warning_at (location, OPT_Wc___compat,
3684 "decrement of enumeration value is invalid in C++");
3687 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3688 arg = c_fully_fold (arg, false, NULL);
3690 /* Increment or decrement the real part of the value,
3691 and don't change the imaginary part. */
3692 if (typecode == COMPLEX_TYPE)
3696 pedwarn (location, OPT_pedantic,
3697 "ISO C does not support %<++%> and %<--%> on complex types");
3699 arg = stabilize_reference (arg);
3700 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3701 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3702 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3703 if (real == error_mark_node || imag == error_mark_node)
3704 return error_mark_node;
3705 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3707 goto return_build_unary_op;
3710 /* Report invalid types. */
3712 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3713 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3715 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3716 error_at (location, "wrong type argument to increment");
3718 error_at (location, "wrong type argument to decrement");
3720 return error_mark_node;
3726 argtype = TREE_TYPE (arg);
3728 /* Compute the increment. */
3730 if (typecode == POINTER_TYPE)
3732 /* If pointer target is an undefined struct,
3733 we just cannot know how to do the arithmetic. */
3734 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3736 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3738 "increment of pointer to unknown structure");
3741 "decrement of pointer to unknown structure");
3743 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3744 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3746 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3747 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3748 "wrong type argument to increment");
3750 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3751 "wrong type argument to decrement");
3754 inc = c_size_in_bytes (TREE_TYPE (argtype));
3755 inc = convert_to_ptrofftype_loc (location, inc);
3757 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3759 /* For signed fract types, we invert ++ to -- or
3760 -- to ++, and change inc from 1 to -1, because
3761 it is not possible to represent 1 in signed fract constants.
3762 For unsigned fract types, the result always overflows and
3763 we get an undefined (original) or the maximum value. */
3764 if (code == PREINCREMENT_EXPR)
3765 code = PREDECREMENT_EXPR;
3766 else if (code == PREDECREMENT_EXPR)
3767 code = PREINCREMENT_EXPR;
3768 else if (code == POSTINCREMENT_EXPR)
3769 code = POSTDECREMENT_EXPR;
3770 else /* code == POSTDECREMENT_EXPR */
3771 code = POSTINCREMENT_EXPR;
3773 inc = integer_minus_one_node;
3774 inc = convert (argtype, inc);
3778 inc = integer_one_node;
3779 inc = convert (argtype, inc);
3782 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3783 need to ask Objective-C to build the increment or decrement
3784 expression for it. */
3785 if (objc_is_property_ref (arg))
3786 return objc_build_incr_expr_for_property_ref (location, code,
3789 /* Report a read-only lvalue. */
3790 if (TYPE_READONLY (argtype))
3792 readonly_error (arg,
3793 ((code == PREINCREMENT_EXPR
3794 || code == POSTINCREMENT_EXPR)
3795 ? lv_increment : lv_decrement));
3796 return error_mark_node;
3798 else if (TREE_READONLY (arg))
3799 readonly_warning (arg,
3800 ((code == PREINCREMENT_EXPR
3801 || code == POSTINCREMENT_EXPR)
3802 ? lv_increment : lv_decrement));
3804 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3805 val = boolean_increment (code, arg);
3807 val = build2 (code, TREE_TYPE (arg), arg, inc);
3808 TREE_SIDE_EFFECTS (val) = 1;
3809 if (TREE_CODE (val) != code)
3810 TREE_NO_WARNING (val) = 1;
3812 goto return_build_unary_op;
3816 /* Note that this operation never does default_conversion. */
3818 /* The operand of unary '&' must be an lvalue (which excludes
3819 expressions of type void), or, in C99, the result of a [] or
3820 unary '*' operator. */
3821 if (VOID_TYPE_P (TREE_TYPE (arg))
3822 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3823 && (TREE_CODE (arg) != INDIRECT_REF
3825 pedwarn (location, 0, "taking address of expression of type %<void%>");
3827 /* Let &* cancel out to simplify resulting code. */
3828 if (TREE_CODE (arg) == INDIRECT_REF)
3830 /* Don't let this be an lvalue. */
3831 if (lvalue_p (TREE_OPERAND (arg, 0)))
3832 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3833 ret = TREE_OPERAND (arg, 0);
3834 goto return_build_unary_op;
3837 /* For &x[y], return x+y */
3838 if (TREE_CODE (arg) == ARRAY_REF)
3840 tree op0 = TREE_OPERAND (arg, 0);
3841 if (!c_mark_addressable (op0))
3842 return error_mark_node;
3845 /* Anything not already handled and not a true memory reference
3846 or a non-lvalue array is an error. */
3847 else if (typecode != FUNCTION_TYPE && !flag
3848 && !lvalue_or_else (location, arg, lv_addressof))
3849 return error_mark_node;
3851 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3853 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3855 tree inner = build_unary_op (location, code,
3856 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3857 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3858 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3859 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3860 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3861 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3862 goto return_build_unary_op;
3865 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3866 argtype = TREE_TYPE (arg);
3868 /* If the lvalue is const or volatile, merge that into the type
3869 to which the address will point. This is only needed
3870 for function types. */
3871 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3872 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3873 && TREE_CODE (argtype) == FUNCTION_TYPE)
3875 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3876 int quals = orig_quals;
3878 if (TREE_READONLY (arg))
3879 quals |= TYPE_QUAL_CONST;
3880 if (TREE_THIS_VOLATILE (arg))
3881 quals |= TYPE_QUAL_VOLATILE;
3883 argtype = c_build_qualified_type (argtype, quals);
3886 if (!c_mark_addressable (arg))
3887 return error_mark_node;
3889 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3890 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3892 argtype = build_pointer_type (argtype);
3894 /* ??? Cope with user tricks that amount to offsetof. Delete this
3895 when we have proper support for integer constant expressions. */
3896 val = get_base_address (arg);
3897 if (val && TREE_CODE (val) == INDIRECT_REF
3898 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3900 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3901 goto return_build_unary_op;
3904 val = build1 (ADDR_EXPR, argtype, arg);
3907 goto return_build_unary_op;
3914 argtype = TREE_TYPE (arg);
3915 if (TREE_CODE (arg) == INTEGER_CST)
3916 ret = (require_constant_value
3917 ? fold_build1_initializer_loc (location, code, argtype, arg)
3918 : fold_build1_loc (location, code, argtype, arg));
3920 ret = build1 (code, argtype, arg);
3921 return_build_unary_op:
3922 gcc_assert (ret != error_mark_node);
3923 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3924 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3925 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3926 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3927 ret = note_integer_operands (ret);
3929 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3930 protected_set_expr_location (ret, location);
3934 /* Return nonzero if REF is an lvalue valid for this language.
3935 Lvalues can be assigned, unless their type has TYPE_READONLY.
3936 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3939 lvalue_p (const_tree ref)
3941 const enum tree_code code = TREE_CODE (ref);
3948 return lvalue_p (TREE_OPERAND (ref, 0));
3950 case C_MAYBE_CONST_EXPR:
3951 return lvalue_p (TREE_OPERAND (ref, 1));
3953 case COMPOUND_LITERAL_EXPR:
3963 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3964 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3967 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3974 /* Give a warning for storing in something that is read-only in GCC
3975 terms but not const in ISO C terms. */
3978 readonly_warning (tree arg, enum lvalue_use use)
3983 warning (0, "assignment of read-only location %qE", arg);
3986 warning (0, "increment of read-only location %qE", arg);
3989 warning (0, "decrement of read-only location %qE", arg);
3998 /* Return nonzero if REF is an lvalue valid for this language;
3999 otherwise, print an error message and return zero. USE says
4000 how the lvalue is being used and so selects the error message.
4001 LOCATION is the location at which any error should be reported. */
4004 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4006 int win = lvalue_p (ref);