1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
30 #include "coretypes.h"
34 #include "langhooks.h"
45 #include "tree-iterator.h"
47 #include "tree-flow.h"
49 /* Possible cases of implicit bad conversions. Used to select
50 diagnostic messages in convert_for_assignment. */
58 /* Whether we are building a boolean conversion inside
59 convert_for_assignment, or some other late binary operation. If
60 build_binary_op is called (from code shared with C++) in this case,
61 then the operands have already been folded and the result will not
62 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
63 bool in_late_binary_op;
65 /* The level of nesting inside "__alignof__". */
68 /* The level of nesting inside "sizeof". */
71 /* The level of nesting inside "typeof". */
74 /* Nonzero if we've already printed a "missing braces around initializer"
75 message within this initializer. */
76 static int missing_braces_mentioned;
78 static int require_constant_value;
79 static int require_constant_elements;
81 static bool null_pointer_constant_p (const_tree);
82 static tree qualify_type (tree, tree);
83 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *);
84 static int comp_target_types (location_t, tree, tree);
85 static int function_types_compatible_p (const_tree, const_tree, bool *);
86 static int type_lists_compatible_p (const_tree, const_tree, bool *);
87 static tree lookup_field (tree, tree);
88 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
90 static tree pointer_diff (location_t, tree, tree);
91 static tree convert_for_assignment (location_t, tree, tree, tree,
92 enum impl_conv, bool, tree, tree, int);
93 static tree valid_compound_expr_initializer (tree, tree);
94 static void push_string (const char *);
95 static void push_member_name (tree);
96 static int spelling_length (void);
97 static char *print_spelling (char *);
98 static void warning_init (int, const char *);
99 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
100 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
102 static void output_pending_init_elements (int, struct obstack *);
103 static int set_designator (int, struct obstack *);
104 static void push_range_stack (tree, struct obstack *);
105 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
106 static void set_nonincremental_init (struct obstack *);
107 static void set_nonincremental_init_from_string (tree, struct obstack *);
108 static tree find_init_member (tree, struct obstack *);
109 static void readonly_error (tree, enum lvalue_use);
110 static void readonly_warning (tree, enum lvalue_use);
111 static int lvalue_or_else (const_tree, enum lvalue_use);
112 static void record_maybe_used_decl (tree);
113 static int comptypes_internal (const_tree, const_tree, bool *);
115 /* Return true if EXP is a null pointer constant, false otherwise. */
118 null_pointer_constant_p (const_tree expr)
120 /* This should really operate on c_expr structures, but they aren't
121 yet available everywhere required. */
122 tree type = TREE_TYPE (expr);
123 return (TREE_CODE (expr) == INTEGER_CST
124 && !TREE_OVERFLOW (expr)
125 && integer_zerop (expr)
126 && (INTEGRAL_TYPE_P (type)
127 || (TREE_CODE (type) == POINTER_TYPE
128 && VOID_TYPE_P (TREE_TYPE (type))
129 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
132 /* EXPR may appear in an unevaluated part of an integer constant
133 expression, but not in an evaluated part. Wrap it in a
134 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
135 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
138 note_integer_operands (tree expr)
141 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
143 ret = copy_node (expr);
144 TREE_OVERFLOW (ret) = 1;
148 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
149 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
154 /* Having checked whether EXPR may appear in an unevaluated part of an
155 integer constant expression and found that it may, remove any
156 C_MAYBE_CONST_EXPR noting this fact and return the resulting
160 remove_c_maybe_const_expr (tree expr)
162 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
163 return C_MAYBE_CONST_EXPR_EXPR (expr);
168 \f/* This is a cache to hold if two types are compatible or not. */
170 struct tagged_tu_seen_cache {
171 const struct tagged_tu_seen_cache * next;
174 /* The return value of tagged_types_tu_compatible_p if we had seen
175 these two types already. */
179 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
180 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
182 /* Do `exp = require_complete_type (exp);' to make sure exp
183 does not have an incomplete type. (That includes void types.) */
186 require_complete_type (tree value)
188 tree type = TREE_TYPE (value);
190 if (value == error_mark_node || type == error_mark_node)
191 return error_mark_node;
193 /* First, detect a valid value with a complete type. */
194 if (COMPLETE_TYPE_P (type))
197 c_incomplete_type_error (value, type);
198 return error_mark_node;
201 /* Print an error message for invalid use of an incomplete type.
202 VALUE is the expression that was used (or 0 if that isn't known)
203 and TYPE is the type that was invalid. */
206 c_incomplete_type_error (const_tree value, const_tree type)
208 const char *type_code_string;
210 /* Avoid duplicate error message. */
211 if (TREE_CODE (type) == ERROR_MARK)
214 if (value != 0 && (TREE_CODE (value) == VAR_DECL
215 || TREE_CODE (value) == PARM_DECL))
216 error ("%qD has an incomplete type", value);
220 /* We must print an error message. Be clever about what it says. */
222 switch (TREE_CODE (type))
225 type_code_string = "struct";
229 type_code_string = "union";
233 type_code_string = "enum";
237 error ("invalid use of void expression");
241 if (TYPE_DOMAIN (type))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 error ("invalid use of flexible array member");
248 type = TREE_TYPE (type);
251 error ("invalid use of array with unspecified bounds");
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error ("invalid use of undefined type %<%s %E%>",
260 type_code_string, TYPE_NAME (type));
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
271 c_type_promotes_to (tree type)
273 if (TYPE_MAIN_VARIANT (type) == float_type_node)
274 return double_type_node;
276 if (c_promoting_integer_type_p (type))
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 return unsigned_type_node;
282 return integer_type_node;
288 /* Return true if between two named address spaces, whether there is a superset
289 named address space that encompasses both address spaces. If there is a
290 superset, return which address space is the superset. */
293 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
300 else if (targetm.addr_space.subset_p (as1, as2))
305 else if (targetm.addr_space.subset_p (as2, as1))
314 /* Return a variant of TYPE which has all the type qualifiers of LIKE
315 as well as those of TYPE. */
318 qualify_type (tree type, tree like)
320 addr_space_t as_type = TYPE_ADDR_SPACE (type);
321 addr_space_t as_like = TYPE_ADDR_SPACE (like);
322 addr_space_t as_common;
324 /* If the two named address spaces are different, determine the common
325 superset address space. If there isn't one, raise an error. */
326 if (!addr_space_superset (as_type, as_like, &as_common))
329 error ("%qT and %qT are in disjoint named address spaces",
333 return c_build_qualified_type (type,
334 TYPE_QUALS_NO_ADDR_SPACE (type)
335 | TYPE_QUALS_NO_ADDR_SPACE (like)
336 | ENCODE_QUAL_ADDR_SPACE (as_common));
339 /* Return true iff the given tree T is a variable length array. */
342 c_vla_type_p (const_tree t)
344 if (TREE_CODE (t) == ARRAY_TYPE
345 && C_TYPE_VARIABLE_SIZE (t))
350 /* Return the composite type of two compatible types.
352 We assume that comptypes has already been done and returned
353 nonzero; if that isn't so, this may crash. In particular, we
354 assume that qualifiers match. */
357 composite_type (tree t1, tree t2)
359 enum tree_code code1;
360 enum tree_code code2;
363 /* Save time if the two types are the same. */
365 if (t1 == t2) return t1;
367 /* If one type is nonsense, use the other. */
368 if (t1 == error_mark_node)
370 if (t2 == error_mark_node)
373 code1 = TREE_CODE (t1);
374 code2 = TREE_CODE (t2);
376 /* Merge the attributes. */
377 attributes = targetm.merge_type_attributes (t1, t2);
379 /* If one is an enumerated type and the other is the compatible
380 integer type, the composite type might be either of the two
381 (DR#013 question 3). For consistency, use the enumerated type as
382 the composite type. */
384 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
386 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
389 gcc_assert (code1 == code2);
394 /* For two pointers, do this recursively on the target type. */
396 tree pointed_to_1 = TREE_TYPE (t1);
397 tree pointed_to_2 = TREE_TYPE (t2);
398 tree target = composite_type (pointed_to_1, pointed_to_2);
399 t1 = build_pointer_type (target);
400 t1 = build_type_attribute_variant (t1, attributes);
401 return qualify_type (t1, t2);
406 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
409 tree d1 = TYPE_DOMAIN (t1);
410 tree d2 = TYPE_DOMAIN (t2);
411 bool d1_variable, d2_variable;
412 bool d1_zero, d2_zero;
413 bool t1_complete, t2_complete;
415 /* We should not have any type quals on arrays at all. */
416 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
417 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
419 t1_complete = COMPLETE_TYPE_P (t1);
420 t2_complete = COMPLETE_TYPE_P (t2);
422 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
423 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
425 d1_variable = (!d1_zero
426 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
427 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
428 d2_variable = (!d2_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
431 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
432 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
434 /* Save space: see if the result is identical to one of the args. */
435 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
436 && (d2_variable || d2_zero || !d1_variable))
437 return build_type_attribute_variant (t1, attributes);
438 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
439 && (d1_variable || d1_zero || !d2_variable))
440 return build_type_attribute_variant (t2, attributes);
442 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
445 return build_type_attribute_variant (t2, attributes);
447 /* Merge the element types, and have a size if either arg has
448 one. We may have qualifiers on the element types. To set
449 up TYPE_MAIN_VARIANT correctly, we need to form the
450 composite of the unqualified types and add the qualifiers
452 quals = TYPE_QUALS (strip_array_types (elt));
453 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
454 t1 = build_array_type (unqual_elt,
455 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
461 /* Ensure a composite type involving a zero-length array type
462 is a zero-length type not an incomplete type. */
463 if (d1_zero && d2_zero
464 && (t1_complete || t2_complete)
465 && !COMPLETE_TYPE_P (t1))
467 TYPE_SIZE (t1) = bitsize_zero_node;
468 TYPE_SIZE_UNIT (t1) = size_zero_node;
470 t1 = c_build_qualified_type (t1, quals);
471 return build_type_attribute_variant (t1, attributes);
477 if (attributes != NULL)
479 /* Try harder not to create a new aggregate type. */
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
485 return build_type_attribute_variant (t1, attributes);
488 /* Function types: prefer the one that specified arg types.
489 If both do, merge the arg types. Also merge the return types. */
491 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
492 tree p1 = TYPE_ARG_TYPES (t1);
493 tree p2 = TYPE_ARG_TYPES (t2);
498 /* Save space: see if the result is identical to one of the args. */
499 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
500 return build_type_attribute_variant (t1, attributes);
501 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
502 return build_type_attribute_variant (t2, attributes);
504 /* Simple way if one arg fails to specify argument types. */
505 if (TYPE_ARG_TYPES (t1) == 0)
507 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
508 t1 = build_type_attribute_variant (t1, attributes);
509 return qualify_type (t1, t2);
511 if (TYPE_ARG_TYPES (t2) == 0)
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
518 /* If both args specify argument types, we must merge the two
519 lists, argument by argument. */
520 /* Tell global_bindings_p to return false so that variable_size
521 doesn't die on VLAs in parameter types. */
522 c_override_global_bindings_to_false = true;
524 len = list_length (p1);
527 for (i = 0; i < len; i++)
528 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
533 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
535 /* A null type means arg type is not specified.
536 Take whatever the other function type has. */
537 if (TREE_VALUE (p1) == 0)
539 TREE_VALUE (n) = TREE_VALUE (p2);
542 if (TREE_VALUE (p2) == 0)
544 TREE_VALUE (n) = TREE_VALUE (p1);
548 /* Given wait (union {union wait *u; int *i} *)
549 and wait (union wait *),
550 prefer union wait * as type of parm. */
551 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
552 && TREE_VALUE (p1) != TREE_VALUE (p2))
555 tree mv2 = TREE_VALUE (p2);
556 if (mv2 && mv2 != error_mark_node
557 && TREE_CODE (mv2) != ARRAY_TYPE)
558 mv2 = TYPE_MAIN_VARIANT (mv2);
559 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
560 memb; memb = TREE_CHAIN (memb))
562 tree mv3 = TREE_TYPE (memb);
563 if (mv3 && mv3 != error_mark_node
564 && TREE_CODE (mv3) != ARRAY_TYPE)
565 mv3 = TYPE_MAIN_VARIANT (mv3);
566 if (comptypes (mv3, mv2))
568 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
570 pedwarn (input_location, OPT_pedantic,
571 "function types not truly compatible in ISO C");
576 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
577 && TREE_VALUE (p2) != TREE_VALUE (p1))
580 tree mv1 = TREE_VALUE (p1);
581 if (mv1 && mv1 != error_mark_node
582 && TREE_CODE (mv1) != ARRAY_TYPE)
583 mv1 = TYPE_MAIN_VARIANT (mv1);
584 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
585 memb; memb = TREE_CHAIN (memb))
587 tree mv3 = TREE_TYPE (memb);
588 if (mv3 && mv3 != error_mark_node
589 && TREE_CODE (mv3) != ARRAY_TYPE)
590 mv3 = TYPE_MAIN_VARIANT (mv3);
591 if (comptypes (mv3, mv1))
593 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
595 pedwarn (input_location, OPT_pedantic,
596 "function types not truly compatible in ISO C");
601 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
605 c_override_global_bindings_to_false = false;
606 t1 = build_function_type (valtype, newargs);
607 t1 = qualify_type (t1, t2);
608 /* ... falls through ... */
612 return build_type_attribute_variant (t1, attributes);
617 /* Return the type of a conditional expression between pointers to
618 possibly differently qualified versions of compatible types.
620 We assume that comp_target_types has already been done and returned
621 nonzero; if that isn't so, this may crash. */
624 common_pointer_type (tree t1, tree t2)
627 tree pointed_to_1, mv1;
628 tree pointed_to_2, mv2;
630 unsigned target_quals;
631 addr_space_t as1, as2, as_common;
634 /* Save time if the two types are the same. */
636 if (t1 == t2) return t1;
638 /* If one type is nonsense, use the other. */
639 if (t1 == error_mark_node)
641 if (t2 == error_mark_node)
644 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
645 && TREE_CODE (t2) == POINTER_TYPE);
647 /* Merge the attributes. */
648 attributes = targetm.merge_type_attributes (t1, t2);
650 /* Find the composite type of the target types, and combine the
651 qualifiers of the two types' targets. Do not lose qualifiers on
652 array element types by taking the TYPE_MAIN_VARIANT. */
653 mv1 = pointed_to_1 = TREE_TYPE (t1);
654 mv2 = pointed_to_2 = TREE_TYPE (t2);
655 if (TREE_CODE (mv1) != ARRAY_TYPE)
656 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
657 if (TREE_CODE (mv2) != ARRAY_TYPE)
658 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
659 target = composite_type (mv1, mv2);
661 /* For function types do not merge const qualifiers, but drop them
662 if used inconsistently. The middle-end uses these to mark const
663 and noreturn functions. */
664 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
665 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
667 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
668 target_quals = (quals1 & quals2);
670 target_quals = (quals1 | quals2);
672 /* If the two named address spaces are different, determine the common
673 superset address space. This is guaranteed to exist due to the
674 assumption that comp_target_type returned non-zero. */
675 as1 = TYPE_ADDR_SPACE (pointed_to_1);
676 as2 = TYPE_ADDR_SPACE (pointed_to_2);
677 if (!addr_space_superset (as1, as2, &as_common))
680 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
682 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
683 return build_type_attribute_variant (t1, attributes);
686 /* Return the common type for two arithmetic types under the usual
687 arithmetic conversions. The default conversions have already been
688 applied, and enumerated types converted to their compatible integer
689 types. The resulting type is unqualified and has no attributes.
691 This is the type for the result of most arithmetic operations
692 if the operands have the given two types. */
695 c_common_type (tree t1, tree t2)
697 enum tree_code code1;
698 enum tree_code code2;
700 /* If one type is nonsense, use the other. */
701 if (t1 == error_mark_node)
703 if (t2 == error_mark_node)
706 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
707 t1 = TYPE_MAIN_VARIANT (t1);
709 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
710 t2 = TYPE_MAIN_VARIANT (t2);
712 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
713 t1 = build_type_attribute_variant (t1, NULL_TREE);
715 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
716 t2 = build_type_attribute_variant (t2, NULL_TREE);
718 /* Save time if the two types are the same. */
720 if (t1 == t2) return t1;
722 code1 = TREE_CODE (t1);
723 code2 = TREE_CODE (t2);
725 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
726 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
727 || code1 == INTEGER_TYPE);
728 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
729 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
730 || code2 == INTEGER_TYPE);
732 /* When one operand is a decimal float type, the other operand cannot be
733 a generic float type or a complex type. We also disallow vector types
735 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
736 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
738 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
740 error ("can%'t mix operands of decimal float and vector types");
741 return error_mark_node;
743 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
745 error ("can%'t mix operands of decimal float and complex types");
746 return error_mark_node;
748 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
750 error ("can%'t mix operands of decimal float and other float types");
751 return error_mark_node;
755 /* If one type is a vector type, return that type. (How the usual
756 arithmetic conversions apply to the vector types extension is not
757 precisely specified.) */
758 if (code1 == VECTOR_TYPE)
761 if (code2 == VECTOR_TYPE)
764 /* If one type is complex, form the common type of the non-complex
765 components, then make that complex. Use T1 or T2 if it is the
767 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
769 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
770 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
771 tree subtype = c_common_type (subtype1, subtype2);
773 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
775 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
778 return build_complex_type (subtype);
781 /* If only one is real, use it as the result. */
783 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
786 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
789 /* If both are real and either are decimal floating point types, use
790 the decimal floating point type with the greater precision. */
792 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
794 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
795 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
796 return dfloat128_type_node;
797 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
798 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
799 return dfloat64_type_node;
800 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
802 return dfloat32_type_node;
805 /* Deal with fixed-point types. */
806 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
808 unsigned int unsignedp = 0, satp = 0;
809 enum machine_mode m1, m2;
810 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
815 /* If one input type is saturating, the result type is saturating. */
816 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
819 /* If both fixed-point types are unsigned, the result type is unsigned.
820 When mixing fixed-point and integer types, follow the sign of the
822 Otherwise, the result type is signed. */
823 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
824 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
825 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t1))
827 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t2)))
831 /* The result type is signed. */
834 /* If the input type is unsigned, we need to convert to the
836 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
838 enum mode_class mclass = (enum mode_class) 0;
839 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
841 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
845 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
847 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
849 enum mode_class mclass = (enum mode_class) 0;
850 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
852 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
856 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
860 if (code1 == FIXED_POINT_TYPE)
862 fbit1 = GET_MODE_FBIT (m1);
863 ibit1 = GET_MODE_IBIT (m1);
868 /* Signed integers need to subtract one sign bit. */
869 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
872 if (code2 == FIXED_POINT_TYPE)
874 fbit2 = GET_MODE_FBIT (m2);
875 ibit2 = GET_MODE_IBIT (m2);
880 /* Signed integers need to subtract one sign bit. */
881 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
884 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
885 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
886 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
890 /* Both real or both integers; use the one with greater precision. */
892 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
894 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
897 /* Same precision. Prefer long longs to longs to ints when the
898 same precision, following the C99 rules on integer type rank
899 (which are equivalent to the C90 rules for C90 types). */
901 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
902 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
903 return long_long_unsigned_type_node;
905 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
906 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
908 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
909 return long_long_unsigned_type_node;
911 return long_long_integer_type_node;
914 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
915 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
916 return long_unsigned_type_node;
918 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
919 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
921 /* But preserve unsignedness from the other type,
922 since long cannot hold all the values of an unsigned int. */
923 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
924 return long_unsigned_type_node;
926 return long_integer_type_node;
929 /* Likewise, prefer long double to double even if same size. */
930 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
931 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
932 return long_double_type_node;
934 /* Otherwise prefer the unsigned one. */
936 if (TYPE_UNSIGNED (t1))
942 /* Wrapper around c_common_type that is used by c-common.c and other
943 front end optimizations that remove promotions. ENUMERAL_TYPEs
944 are allowed here and are converted to their compatible integer types.
945 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
946 preferably a non-Boolean type as the common type. */
948 common_type (tree t1, tree t2)
950 if (TREE_CODE (t1) == ENUMERAL_TYPE)
951 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
952 if (TREE_CODE (t2) == ENUMERAL_TYPE)
953 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
955 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
956 if (TREE_CODE (t1) == BOOLEAN_TYPE
957 && TREE_CODE (t2) == BOOLEAN_TYPE)
958 return boolean_type_node;
960 /* If either type is BOOLEAN_TYPE, then return the other. */
961 if (TREE_CODE (t1) == BOOLEAN_TYPE)
963 if (TREE_CODE (t2) == BOOLEAN_TYPE)
966 return c_common_type (t1, t2);
969 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
970 or various other operations. Return 2 if they are compatible
971 but a warning may be needed if you use them together. */
974 comptypes (tree type1, tree type2)
976 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
979 val = comptypes_internal (type1, type2, NULL);
980 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
985 /* Like comptypes, but if it returns non-zero because enum and int are
986 compatible, it sets *ENUM_AND_INT_P to true. */
989 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
991 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
994 val = comptypes_internal (type1, type2, enum_and_int_p);
995 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1000 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1001 or various other operations. Return 2 if they are compatible
1002 but a warning may be needed if you use them together. If
1003 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1004 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1005 *ENUM_AND_INT_P is never set to false. This differs from
1006 comptypes, in that we don't free the seen types. */
1009 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p)
1011 const_tree t1 = type1;
1012 const_tree t2 = type2;
1015 /* Suppress errors caused by previously reported errors. */
1017 if (t1 == t2 || !t1 || !t2
1018 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1021 /* If either type is the internal version of sizetype, return the
1022 language version. */
1023 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
1024 && TYPE_ORIG_SIZE_TYPE (t1))
1025 t1 = TYPE_ORIG_SIZE_TYPE (t1);
1027 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
1028 && TYPE_ORIG_SIZE_TYPE (t2))
1029 t2 = TYPE_ORIG_SIZE_TYPE (t2);
1032 /* Enumerated types are compatible with integer types, but this is
1033 not transitive: two enumerated types in the same translation unit
1034 are compatible with each other only if they are the same type. */
1036 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1038 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1039 if (enum_and_int_p != NULL && TREE_CODE (t2) != VOID_TYPE)
1040 *enum_and_int_p = true;
1042 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1044 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1045 if (enum_and_int_p != NULL && TREE_CODE (t1) != VOID_TYPE)
1046 *enum_and_int_p = true;
1052 /* Different classes of types can't be compatible. */
1054 if (TREE_CODE (t1) != TREE_CODE (t2))
1057 /* Qualifiers must match. C99 6.7.3p9 */
1059 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1062 /* Allow for two different type nodes which have essentially the same
1063 definition. Note that we already checked for equality of the type
1064 qualifiers (just above). */
1066 if (TREE_CODE (t1) != ARRAY_TYPE
1067 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1070 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1071 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1074 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1077 switch (TREE_CODE (t1))
1080 /* Do not remove mode or aliasing information. */
1081 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1082 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1084 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1085 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1090 val = function_types_compatible_p (t1, t2, enum_and_int_p);
1095 tree d1 = TYPE_DOMAIN (t1);
1096 tree d2 = TYPE_DOMAIN (t2);
1097 bool d1_variable, d2_variable;
1098 bool d1_zero, d2_zero;
1101 /* Target types must match incl. qualifiers. */
1102 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1103 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1107 /* Sizes must match unless one is missing or variable. */
1108 if (d1 == 0 || d2 == 0 || d1 == d2)
1111 d1_zero = !TYPE_MAX_VALUE (d1);
1112 d2_zero = !TYPE_MAX_VALUE (d2);
1114 d1_variable = (!d1_zero
1115 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1116 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1117 d2_variable = (!d2_zero
1118 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1119 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1120 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1121 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1123 if (d1_variable || d2_variable)
1125 if (d1_zero && d2_zero)
1127 if (d1_zero || d2_zero
1128 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1129 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1138 if (val != 1 && !same_translation_unit_p (t1, t2))
1140 tree a1 = TYPE_ATTRIBUTES (t1);
1141 tree a2 = TYPE_ATTRIBUTES (t2);
1143 if (! attribute_list_contained (a1, a2)
1144 && ! attribute_list_contained (a2, a1))
1148 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1149 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1154 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1155 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1162 return attrval == 2 && val == 1 ? 2 : val;
1165 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1166 their qualifiers, except for named address spaces. If the pointers point to
1167 different named addresses, then we must determine if one address space is a
1168 subset of the other. */
1171 comp_target_types (location_t location, tree ttl, tree ttr)
1174 tree mvl = TREE_TYPE (ttl);
1175 tree mvr = TREE_TYPE (ttr);
1176 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1177 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1178 addr_space_t as_common;
1179 bool enum_and_int_p;
1181 /* Fail if pointers point to incompatible address spaces. */
1182 if (!addr_space_superset (asl, asr, &as_common))
1185 /* Do not lose qualifiers on element types of array types that are
1186 pointer targets by taking their TYPE_MAIN_VARIANT. */
1187 if (TREE_CODE (mvl) != ARRAY_TYPE)
1188 mvl = TYPE_MAIN_VARIANT (mvl);
1189 if (TREE_CODE (mvr) != ARRAY_TYPE)
1190 mvr = TYPE_MAIN_VARIANT (mvr);
1191 enum_and_int_p = false;
1192 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1195 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1197 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1198 warning_at (location, OPT_Wc___compat,
1199 "pointer target types incompatible in C++");
1204 /* Subroutines of `comptypes'. */
1206 /* Determine whether two trees derive from the same translation unit.
1207 If the CONTEXT chain ends in a null, that tree's context is still
1208 being parsed, so if two trees have context chains ending in null,
1209 they're in the same translation unit. */
1211 same_translation_unit_p (const_tree t1, const_tree t2)
1213 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1214 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1216 case tcc_declaration:
1217 t1 = DECL_CONTEXT (t1); break;
1219 t1 = TYPE_CONTEXT (t1); break;
1220 case tcc_exceptional:
1221 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1222 default: gcc_unreachable ();
1225 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1226 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1228 case tcc_declaration:
1229 t2 = DECL_CONTEXT (t2); break;
1231 t2 = TYPE_CONTEXT (t2); break;
1232 case tcc_exceptional:
1233 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1234 default: gcc_unreachable ();
1240 /* Allocate the seen two types, assuming that they are compatible. */
1242 static struct tagged_tu_seen_cache *
1243 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1245 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1246 tu->next = tagged_tu_seen_base;
1250 tagged_tu_seen_base = tu;
1252 /* The C standard says that two structures in different translation
1253 units are compatible with each other only if the types of their
1254 fields are compatible (among other things). We assume that they
1255 are compatible until proven otherwise when building the cache.
1256 An example where this can occur is:
1261 If we are comparing this against a similar struct in another TU,
1262 and did not assume they were compatible, we end up with an infinite
1268 /* Free the seen types until we get to TU_TIL. */
1271 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1273 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1274 while (tu != tu_til)
1276 const struct tagged_tu_seen_cache *const tu1
1277 = (const struct tagged_tu_seen_cache *) tu;
1279 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1281 tagged_tu_seen_base = tu_til;
1284 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1285 compatible. If the two types are not the same (which has been
1286 checked earlier), this can only happen when multiple translation
1287 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1288 rules. ENUM_AND_INT_P is as in comptypes_internal. */
1291 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1292 bool *enum_and_int_p)
1295 bool needs_warning = false;
1297 /* We have to verify that the tags of the types are the same. This
1298 is harder than it looks because this may be a typedef, so we have
1299 to go look at the original type. It may even be a typedef of a
1301 In the case of compiler-created builtin structs the TYPE_DECL
1302 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1303 while (TYPE_NAME (t1)
1304 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1305 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1306 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1308 while (TYPE_NAME (t2)
1309 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1310 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1311 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1313 /* C90 didn't have the requirement that the two tags be the same. */
1314 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1317 /* C90 didn't say what happened if one or both of the types were
1318 incomplete; we choose to follow C99 rules here, which is that they
1320 if (TYPE_SIZE (t1) == NULL
1321 || TYPE_SIZE (t2) == NULL)
1325 const struct tagged_tu_seen_cache * tts_i;
1326 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1327 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1331 switch (TREE_CODE (t1))
1335 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1336 /* Speed up the case where the type values are in the same order. */
1337 tree tv1 = TYPE_VALUES (t1);
1338 tree tv2 = TYPE_VALUES (t2);
1345 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1347 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1349 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1356 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1360 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1366 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1372 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1374 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1376 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1387 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1388 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1394 /* Speed up the common case where the fields are in the same order. */
1395 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1396 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1400 if (DECL_NAME (s1) != DECL_NAME (s2))
1402 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1405 if (result != 1 && !DECL_NAME (s1))
1413 needs_warning = true;
1415 if (TREE_CODE (s1) == FIELD_DECL
1416 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1417 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1425 tu->val = needs_warning ? 2 : 1;
1429 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1433 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1434 if (DECL_NAME (s1) == DECL_NAME (s2))
1438 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1441 if (result != 1 && !DECL_NAME (s1))
1449 needs_warning = true;
1451 if (TREE_CODE (s1) == FIELD_DECL
1452 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1453 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1465 tu->val = needs_warning ? 2 : 10;
1471 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1473 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1475 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1478 if (TREE_CODE (s1) != TREE_CODE (s2)
1479 || DECL_NAME (s1) != DECL_NAME (s2))
1481 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1486 needs_warning = true;
1488 if (TREE_CODE (s1) == FIELD_DECL
1489 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1490 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1496 tu->val = needs_warning ? 2 : 1;
1505 /* Return 1 if two function types F1 and F2 are compatible.
1506 If either type specifies no argument types,
1507 the other must specify a fixed number of self-promoting arg types.
1508 Otherwise, if one type specifies only the number of arguments,
1509 the other must specify that number of self-promoting arg types.
1510 Otherwise, the argument types must match.
1511 ENUM_AND_INT_P is as in comptypes_internal. */
1514 function_types_compatible_p (const_tree f1, const_tree f2,
1515 bool *enum_and_int_p)
1518 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1523 ret1 = TREE_TYPE (f1);
1524 ret2 = TREE_TYPE (f2);
1526 /* 'volatile' qualifiers on a function's return type used to mean
1527 the function is noreturn. */
1528 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1529 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1530 if (TYPE_VOLATILE (ret1))
1531 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1532 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1533 if (TYPE_VOLATILE (ret2))
1534 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1535 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1536 val = comptypes_internal (ret1, ret2, enum_and_int_p);
1540 args1 = TYPE_ARG_TYPES (f1);
1541 args2 = TYPE_ARG_TYPES (f2);
1543 /* An unspecified parmlist matches any specified parmlist
1544 whose argument types don't need default promotions. */
1548 if (!self_promoting_args_p (args2))
1550 /* If one of these types comes from a non-prototype fn definition,
1551 compare that with the other type's arglist.
1552 If they don't match, ask for a warning (but no error). */
1553 if (TYPE_ACTUAL_ARG_TYPES (f1)
1554 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1561 if (!self_promoting_args_p (args1))
1563 if (TYPE_ACTUAL_ARG_TYPES (f2)
1564 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1570 /* Both types have argument lists: compare them and propagate results. */
1571 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p);
1572 return val1 != 1 ? val1 : val;
1575 /* Check two lists of types for compatibility, returning 0 for
1576 incompatible, 1 for compatible, or 2 for compatible with
1577 warning. ENUM_AND_INT_P is as in comptypes_internal. */
1580 type_lists_compatible_p (const_tree args1, const_tree args2,
1581 bool *enum_and_int_p)
1583 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1589 tree a1, mv1, a2, mv2;
1590 if (args1 == 0 && args2 == 0)
1592 /* If one list is shorter than the other,
1593 they fail to match. */
1594 if (args1 == 0 || args2 == 0)
1596 mv1 = a1 = TREE_VALUE (args1);
1597 mv2 = a2 = TREE_VALUE (args2);
1598 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1599 mv1 = TYPE_MAIN_VARIANT (mv1);
1600 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1601 mv2 = TYPE_MAIN_VARIANT (mv2);
1602 /* A null pointer instead of a type
1603 means there is supposed to be an argument
1604 but nothing is specified about what type it has.
1605 So match anything that self-promotes. */
1608 if (c_type_promotes_to (a2) != a2)
1613 if (c_type_promotes_to (a1) != a1)
1616 /* If one of the lists has an error marker, ignore this arg. */
1617 else if (TREE_CODE (a1) == ERROR_MARK
1618 || TREE_CODE (a2) == ERROR_MARK)
1620 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p)))
1622 /* Allow wait (union {union wait *u; int *i} *)
1623 and wait (union wait *) to be compatible. */
1624 if (TREE_CODE (a1) == UNION_TYPE
1625 && (TYPE_NAME (a1) == 0
1626 || TYPE_TRANSPARENT_AGGR (a1))
1627 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1628 && tree_int_cst_equal (TYPE_SIZE (a1),
1632 for (memb = TYPE_FIELDS (a1);
1633 memb; memb = TREE_CHAIN (memb))
1635 tree mv3 = TREE_TYPE (memb);
1636 if (mv3 && mv3 != error_mark_node
1637 && TREE_CODE (mv3) != ARRAY_TYPE)
1638 mv3 = TYPE_MAIN_VARIANT (mv3);
1639 if (comptypes_internal (mv3, mv2, enum_and_int_p))
1645 else if (TREE_CODE (a2) == UNION_TYPE
1646 && (TYPE_NAME (a2) == 0
1647 || TYPE_TRANSPARENT_AGGR (a2))
1648 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1649 && tree_int_cst_equal (TYPE_SIZE (a2),
1653 for (memb = TYPE_FIELDS (a2);
1654 memb; memb = TREE_CHAIN (memb))
1656 tree mv3 = TREE_TYPE (memb);
1657 if (mv3 && mv3 != error_mark_node
1658 && TREE_CODE (mv3) != ARRAY_TYPE)
1659 mv3 = TYPE_MAIN_VARIANT (mv3);
1660 if (comptypes_internal (mv3, mv1, enum_and_int_p))
1670 /* comptypes said ok, but record if it said to warn. */
1674 args1 = TREE_CHAIN (args1);
1675 args2 = TREE_CHAIN (args2);
1679 /* Compute the size to increment a pointer by. */
1682 c_size_in_bytes (const_tree type)
1684 enum tree_code code = TREE_CODE (type);
1686 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1687 return size_one_node;
1689 if (!COMPLETE_OR_VOID_TYPE_P (type))
1691 error ("arithmetic on pointer to an incomplete type");
1692 return size_one_node;
1695 /* Convert in case a char is more than one unit. */
1696 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1697 size_int (TYPE_PRECISION (char_type_node)
1701 /* Return either DECL or its known constant value (if it has one). */
1704 decl_constant_value (tree decl)
1706 if (/* Don't change a variable array bound or initial value to a constant
1707 in a place where a variable is invalid. Note that DECL_INITIAL
1708 isn't valid for a PARM_DECL. */
1709 current_function_decl != 0
1710 && TREE_CODE (decl) != PARM_DECL
1711 && !TREE_THIS_VOLATILE (decl)
1712 && TREE_READONLY (decl)
1713 && DECL_INITIAL (decl) != 0
1714 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1715 /* This is invalid if initial value is not constant.
1716 If it has either a function call, a memory reference,
1717 or a variable, then re-evaluating it could give different results. */
1718 && TREE_CONSTANT (DECL_INITIAL (decl))
1719 /* Check for cases where this is sub-optimal, even though valid. */
1720 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1721 return DECL_INITIAL (decl);
1725 /* Convert the array expression EXP to a pointer. */
1727 array_to_pointer_conversion (location_t loc, tree exp)
1729 tree orig_exp = exp;
1730 tree type = TREE_TYPE (exp);
1732 tree restype = TREE_TYPE (type);
1735 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1737 STRIP_TYPE_NOPS (exp);
1739 if (TREE_NO_WARNING (orig_exp))
1740 TREE_NO_WARNING (exp) = 1;
1742 ptrtype = build_pointer_type (restype);
1744 if (TREE_CODE (exp) == INDIRECT_REF)
1745 return convert (ptrtype, TREE_OPERAND (exp, 0));
1747 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1748 return convert (ptrtype, adr);
1751 /* Convert the function expression EXP to a pointer. */
1753 function_to_pointer_conversion (location_t loc, tree exp)
1755 tree orig_exp = exp;
1757 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1759 STRIP_TYPE_NOPS (exp);
1761 if (TREE_NO_WARNING (orig_exp))
1762 TREE_NO_WARNING (exp) = 1;
1764 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1767 /* Mark EXP as read, not just set, for set but not used -Wunused
1768 warning purposes. */
1771 mark_exp_read (tree exp)
1773 switch (TREE_CODE (exp))
1777 DECL_READ_P (exp) = 1;
1786 mark_exp_read (TREE_OPERAND (exp, 0));
1789 mark_exp_read (TREE_OPERAND (exp, 1));
1796 /* Perform the default conversion of arrays and functions to pointers.
1797 Return the result of converting EXP. For any other expression, just
1800 LOC is the location of the expression. */
1803 default_function_array_conversion (location_t loc, struct c_expr exp)
1805 tree orig_exp = exp.value;
1806 tree type = TREE_TYPE (exp.value);
1807 enum tree_code code = TREE_CODE (type);
1813 bool not_lvalue = false;
1814 bool lvalue_array_p;
1816 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1817 || CONVERT_EXPR_P (exp.value))
1818 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1820 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1822 exp.value = TREE_OPERAND (exp.value, 0);
1825 if (TREE_NO_WARNING (orig_exp))
1826 TREE_NO_WARNING (exp.value) = 1;
1828 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1829 if (!flag_isoc99 && !lvalue_array_p)
1831 /* Before C99, non-lvalue arrays do not decay to pointers.
1832 Normally, using such an array would be invalid; but it can
1833 be used correctly inside sizeof or as a statement expression.
1834 Thus, do not give an error here; an error will result later. */
1838 exp.value = array_to_pointer_conversion (loc, exp.value);
1842 exp.value = function_to_pointer_conversion (loc, exp.value);
1852 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1854 mark_exp_read (exp.value);
1855 return default_function_array_conversion (loc, exp);
1858 /* EXP is an expression of integer type. Apply the integer promotions
1859 to it and return the promoted value. */
1862 perform_integral_promotions (tree exp)
1864 tree type = TREE_TYPE (exp);
1865 enum tree_code code = TREE_CODE (type);
1867 gcc_assert (INTEGRAL_TYPE_P (type));
1869 /* Normally convert enums to int,
1870 but convert wide enums to something wider. */
1871 if (code == ENUMERAL_TYPE)
1873 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1874 TYPE_PRECISION (integer_type_node)),
1875 ((TYPE_PRECISION (type)
1876 >= TYPE_PRECISION (integer_type_node))
1877 && TYPE_UNSIGNED (type)));
1879 return convert (type, exp);
1882 /* ??? This should no longer be needed now bit-fields have their
1884 if (TREE_CODE (exp) == COMPONENT_REF
1885 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1886 /* If it's thinner than an int, promote it like a
1887 c_promoting_integer_type_p, otherwise leave it alone. */
1888 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1889 TYPE_PRECISION (integer_type_node)))
1890 return convert (integer_type_node, exp);
1892 if (c_promoting_integer_type_p (type))
1894 /* Preserve unsignedness if not really getting any wider. */
1895 if (TYPE_UNSIGNED (type)
1896 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1897 return convert (unsigned_type_node, exp);
1899 return convert (integer_type_node, exp);
1906 /* Perform default promotions for C data used in expressions.
1907 Enumeral types or short or char are converted to int.
1908 In addition, manifest constants symbols are replaced by their values. */
1911 default_conversion (tree exp)
1914 tree type = TREE_TYPE (exp);
1915 enum tree_code code = TREE_CODE (type);
1918 mark_exp_read (exp);
1920 /* Functions and arrays have been converted during parsing. */
1921 gcc_assert (code != FUNCTION_TYPE);
1922 if (code == ARRAY_TYPE)
1925 /* Constants can be used directly unless they're not loadable. */
1926 if (TREE_CODE (exp) == CONST_DECL)
1927 exp = DECL_INITIAL (exp);
1929 /* Strip no-op conversions. */
1931 STRIP_TYPE_NOPS (exp);
1933 if (TREE_NO_WARNING (orig_exp))
1934 TREE_NO_WARNING (exp) = 1;
1936 if (code == VOID_TYPE)
1938 error ("void value not ignored as it ought to be");
1939 return error_mark_node;
1942 exp = require_complete_type (exp);
1943 if (exp == error_mark_node)
1944 return error_mark_node;
1946 promoted_type = targetm.promoted_type (type);
1948 return convert (promoted_type, exp);
1950 if (INTEGRAL_TYPE_P (type))
1951 return perform_integral_promotions (exp);
1956 /* Look up COMPONENT in a structure or union DECL.
1958 If the component name is not found, returns NULL_TREE. Otherwise,
1959 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1960 stepping down the chain to the component, which is in the last
1961 TREE_VALUE of the list. Normally the list is of length one, but if
1962 the component is embedded within (nested) anonymous structures or
1963 unions, the list steps down the chain to the component. */
1966 lookup_field (tree decl, tree component)
1968 tree type = TREE_TYPE (decl);
1971 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1972 to the field elements. Use a binary search on this array to quickly
1973 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1974 will always be set for structures which have many elements. */
1976 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1979 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1981 field = TYPE_FIELDS (type);
1983 top = TYPE_LANG_SPECIFIC (type)->s->len;
1984 while (top - bot > 1)
1986 half = (top - bot + 1) >> 1;
1987 field = field_array[bot+half];
1989 if (DECL_NAME (field) == NULL_TREE)
1991 /* Step through all anon unions in linear fashion. */
1992 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1994 field = field_array[bot++];
1995 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1996 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1998 tree anon = lookup_field (field, component);
2001 return tree_cons (NULL_TREE, field, anon);
2005 /* Entire record is only anon unions. */
2009 /* Restart the binary search, with new lower bound. */
2013 if (DECL_NAME (field) == component)
2015 if (DECL_NAME (field) < component)
2021 if (DECL_NAME (field_array[bot]) == component)
2022 field = field_array[bot];
2023 else if (DECL_NAME (field) != component)
2028 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2030 if (DECL_NAME (field) == NULL_TREE
2031 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2032 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2034 tree anon = lookup_field (field, component);
2037 return tree_cons (NULL_TREE, field, anon);
2040 if (DECL_NAME (field) == component)
2044 if (field == NULL_TREE)
2048 return tree_cons (NULL_TREE, field, NULL_TREE);
2051 /* Make an expression to refer to the COMPONENT field of structure or
2052 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2053 location of the COMPONENT_REF. */
2056 build_component_ref (location_t loc, tree datum, tree component)
2058 tree type = TREE_TYPE (datum);
2059 enum tree_code code = TREE_CODE (type);
2062 bool datum_lvalue = lvalue_p (datum);
2064 if (!objc_is_public (datum, component))
2065 return error_mark_node;
2067 /* See if there is a field or component with name COMPONENT. */
2069 if (code == RECORD_TYPE || code == UNION_TYPE)
2071 if (!COMPLETE_TYPE_P (type))
2073 c_incomplete_type_error (NULL_TREE, type);
2074 return error_mark_node;
2077 field = lookup_field (datum, component);
2081 error_at (loc, "%qT has no member named %qE", type, component);
2082 return error_mark_node;
2085 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2086 This might be better solved in future the way the C++ front
2087 end does it - by giving the anonymous entities each a
2088 separate name and type, and then have build_component_ref
2089 recursively call itself. We can't do that here. */
2092 tree subdatum = TREE_VALUE (field);
2095 bool use_datum_quals;
2097 if (TREE_TYPE (subdatum) == error_mark_node)
2098 return error_mark_node;
2100 /* If this is an rvalue, it does not have qualifiers in C
2101 standard terms and we must avoid propagating such
2102 qualifiers down to a non-lvalue array that is then
2103 converted to a pointer. */
2104 use_datum_quals = (datum_lvalue
2105 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2107 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2108 if (use_datum_quals)
2109 quals |= TYPE_QUALS (TREE_TYPE (datum));
2110 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2112 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2114 SET_EXPR_LOCATION (ref, loc);
2115 if (TREE_READONLY (subdatum)
2116 || (use_datum_quals && TREE_READONLY (datum)))
2117 TREE_READONLY (ref) = 1;
2118 if (TREE_THIS_VOLATILE (subdatum)
2119 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2120 TREE_THIS_VOLATILE (ref) = 1;
2122 if (TREE_DEPRECATED (subdatum))
2123 warn_deprecated_use (subdatum, NULL_TREE);
2127 field = TREE_CHAIN (field);
2133 else if (code != ERROR_MARK)
2135 "request for member %qE in something not a structure or union",
2138 return error_mark_node;
2141 /* Given an expression PTR for a pointer, return an expression
2142 for the value pointed to.
2143 ERRORSTRING is the name of the operator to appear in error messages.
2145 LOC is the location to use for the generated tree. */
2148 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2150 tree pointer = default_conversion (ptr);
2151 tree type = TREE_TYPE (pointer);
2154 if (TREE_CODE (type) == POINTER_TYPE)
2156 if (CONVERT_EXPR_P (pointer)
2157 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2159 /* If a warning is issued, mark it to avoid duplicates from
2160 the backend. This only needs to be done at
2161 warn_strict_aliasing > 2. */
2162 if (warn_strict_aliasing > 2)
2163 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2164 type, TREE_OPERAND (pointer, 0)))
2165 TREE_NO_WARNING (pointer) = 1;
2168 if (TREE_CODE (pointer) == ADDR_EXPR
2169 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2170 == TREE_TYPE (type)))
2172 ref = TREE_OPERAND (pointer, 0);
2173 protected_set_expr_location (ref, loc);
2178 tree t = TREE_TYPE (type);
2180 ref = build1 (INDIRECT_REF, t, pointer);
2182 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2184 error_at (loc, "dereferencing pointer to incomplete type");
2185 return error_mark_node;
2187 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2188 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2190 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2191 so that we get the proper error message if the result is used
2192 to assign to. Also, &* is supposed to be a no-op.
2193 And ANSI C seems to specify that the type of the result
2194 should be the const type. */
2195 /* A de-reference of a pointer to const is not a const. It is valid
2196 to change it via some other pointer. */
2197 TREE_READONLY (ref) = TYPE_READONLY (t);
2198 TREE_SIDE_EFFECTS (ref)
2199 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2200 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2201 protected_set_expr_location (ref, loc);
2205 else if (TREE_CODE (pointer) != ERROR_MARK)
2208 case RO_ARRAY_INDEXING:
2210 "invalid type argument of array indexing (have %qT)",
2215 "invalid type argument of unary %<*%> (have %qT)",
2220 "invalid type argument of %<->%> (have %qT)",
2226 return error_mark_node;
2229 /* This handles expressions of the form "a[i]", which denotes
2232 This is logically equivalent in C to *(a+i), but we may do it differently.
2233 If A is a variable or a member, we generate a primitive ARRAY_REF.
2234 This avoids forcing the array out of registers, and can work on
2235 arrays that are not lvalues (for example, members of structures returned
2238 LOC is the location to use for the returned expression. */
2241 build_array_ref (location_t loc, tree array, tree index)
2244 bool swapped = false;
2245 if (TREE_TYPE (array) == error_mark_node
2246 || TREE_TYPE (index) == error_mark_node)
2247 return error_mark_node;
2249 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2250 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2253 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2254 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2256 error_at (loc, "subscripted value is neither array nor pointer");
2257 return error_mark_node;
2265 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2267 error_at (loc, "array subscript is not an integer");
2268 return error_mark_node;
2271 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2273 error_at (loc, "subscripted value is pointer to function");
2274 return error_mark_node;
2277 /* ??? Existing practice has been to warn only when the char
2278 index is syntactically the index, not for char[array]. */
2280 warn_array_subscript_with_type_char (index);
2282 /* Apply default promotions *after* noticing character types. */
2283 index = default_conversion (index);
2285 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2287 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2291 /* An array that is indexed by a non-constant
2292 cannot be stored in a register; we must be able to do
2293 address arithmetic on its address.
2294 Likewise an array of elements of variable size. */
2295 if (TREE_CODE (index) != INTEGER_CST
2296 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2297 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2299 if (!c_mark_addressable (array))
2300 return error_mark_node;
2302 /* An array that is indexed by a constant value which is not within
2303 the array bounds cannot be stored in a register either; because we
2304 would get a crash in store_bit_field/extract_bit_field when trying
2305 to access a non-existent part of the register. */
2306 if (TREE_CODE (index) == INTEGER_CST
2307 && TYPE_DOMAIN (TREE_TYPE (array))
2308 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2310 if (!c_mark_addressable (array))
2311 return error_mark_node;
2317 while (TREE_CODE (foo) == COMPONENT_REF)
2318 foo = TREE_OPERAND (foo, 0);
2319 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2320 pedwarn (loc, OPT_pedantic,
2321 "ISO C forbids subscripting %<register%> array");
2322 else if (!flag_isoc99 && !lvalue_p (foo))
2323 pedwarn (loc, OPT_pedantic,
2324 "ISO C90 forbids subscripting non-lvalue array");
2327 type = TREE_TYPE (TREE_TYPE (array));
2328 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2329 /* Array ref is const/volatile if the array elements are
2330 or if the array is. */
2331 TREE_READONLY (rval)
2332 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2333 | TREE_READONLY (array));
2334 TREE_SIDE_EFFECTS (rval)
2335 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2336 | TREE_SIDE_EFFECTS (array));
2337 TREE_THIS_VOLATILE (rval)
2338 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2339 /* This was added by rms on 16 Nov 91.
2340 It fixes vol struct foo *a; a->elts[1]
2341 in an inline function.
2342 Hope it doesn't break something else. */
2343 | TREE_THIS_VOLATILE (array));
2344 ret = require_complete_type (rval);
2345 protected_set_expr_location (ret, loc);
2350 tree ar = default_conversion (array);
2352 if (ar == error_mark_node)
2355 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2356 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2358 return build_indirect_ref
2359 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2364 /* Build an external reference to identifier ID. FUN indicates
2365 whether this will be used for a function call. LOC is the source
2366 location of the identifier. This sets *TYPE to the type of the
2367 identifier, which is not the same as the type of the returned value
2368 for CONST_DECLs defined as enum constants. If the type of the
2369 identifier is not available, *TYPE is set to NULL. */
2371 build_external_ref (location_t loc, tree id, int fun, tree *type)
2374 tree decl = lookup_name (id);
2376 /* In Objective-C, an instance variable (ivar) may be preferred to
2377 whatever lookup_name() found. */
2378 decl = objc_lookup_ivar (decl, id);
2381 if (decl && decl != error_mark_node)
2384 *type = TREE_TYPE (ref);
2387 /* Implicit function declaration. */
2388 ref = implicitly_declare (loc, id);
2389 else if (decl == error_mark_node)
2390 /* Don't complain about something that's already been
2391 complained about. */
2392 return error_mark_node;
2395 undeclared_variable (loc, id);
2396 return error_mark_node;
2399 if (TREE_TYPE (ref) == error_mark_node)
2400 return error_mark_node;
2402 if (TREE_DEPRECATED (ref))
2403 warn_deprecated_use (ref, NULL_TREE);
2405 /* Recursive call does not count as usage. */
2406 if (ref != current_function_decl)
2408 TREE_USED (ref) = 1;
2411 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2413 if (!in_sizeof && !in_typeof)
2414 C_DECL_USED (ref) = 1;
2415 else if (DECL_INITIAL (ref) == 0
2416 && DECL_EXTERNAL (ref)
2417 && !TREE_PUBLIC (ref))
2418 record_maybe_used_decl (ref);
2421 if (TREE_CODE (ref) == CONST_DECL)
2423 used_types_insert (TREE_TYPE (ref));
2426 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2427 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2429 warning_at (loc, OPT_Wc___compat,
2430 ("enum constant defined in struct or union "
2431 "is not visible in C++"));
2432 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2435 ref = DECL_INITIAL (ref);
2436 TREE_CONSTANT (ref) = 1;
2438 else if (current_function_decl != 0
2439 && !DECL_FILE_SCOPE_P (current_function_decl)
2440 && (TREE_CODE (ref) == VAR_DECL
2441 || TREE_CODE (ref) == PARM_DECL
2442 || TREE_CODE (ref) == FUNCTION_DECL))
2444 tree context = decl_function_context (ref);
2446 if (context != 0 && context != current_function_decl)
2447 DECL_NONLOCAL (ref) = 1;
2449 /* C99 6.7.4p3: An inline definition of a function with external
2450 linkage ... shall not contain a reference to an identifier with
2451 internal linkage. */
2452 else if (current_function_decl != 0
2453 && DECL_DECLARED_INLINE_P (current_function_decl)
2454 && DECL_EXTERNAL (current_function_decl)
2455 && VAR_OR_FUNCTION_DECL_P (ref)
2456 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2457 && ! TREE_PUBLIC (ref)
2458 && DECL_CONTEXT (ref) != current_function_decl)
2459 record_inline_static (loc, current_function_decl, ref,
2465 /* Record details of decls possibly used inside sizeof or typeof. */
2466 struct maybe_used_decl
2470 /* The level seen at (in_sizeof + in_typeof). */
2472 /* The next one at this level or above, or NULL. */
2473 struct maybe_used_decl *next;
2476 static struct maybe_used_decl *maybe_used_decls;
2478 /* Record that DECL, an undefined static function reference seen
2479 inside sizeof or typeof, might be used if the operand of sizeof is
2480 a VLA type or the operand of typeof is a variably modified
2484 record_maybe_used_decl (tree decl)
2486 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2488 t->level = in_sizeof + in_typeof;
2489 t->next = maybe_used_decls;
2490 maybe_used_decls = t;
2493 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2494 USED is false, just discard them. If it is true, mark them used
2495 (if no longer inside sizeof or typeof) or move them to the next
2496 level up (if still inside sizeof or typeof). */
2499 pop_maybe_used (bool used)
2501 struct maybe_used_decl *p = maybe_used_decls;
2502 int cur_level = in_sizeof + in_typeof;
2503 while (p && p->level > cur_level)
2508 C_DECL_USED (p->decl) = 1;
2510 p->level = cur_level;
2514 if (!used || cur_level == 0)
2515 maybe_used_decls = p;
2518 /* Return the result of sizeof applied to EXPR. */
2521 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2524 if (expr.value == error_mark_node)
2526 ret.value = error_mark_node;
2527 ret.original_code = ERROR_MARK;
2528 ret.original_type = NULL;
2529 pop_maybe_used (false);
2533 bool expr_const_operands = true;
2534 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2535 &expr_const_operands);
2536 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2537 ret.original_code = ERROR_MARK;
2538 ret.original_type = NULL;
2539 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2541 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2542 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2543 folded_expr, ret.value);
2544 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2545 SET_EXPR_LOCATION (ret.value, loc);
2547 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2552 /* Return the result of sizeof applied to T, a structure for the type
2553 name passed to sizeof (rather than the type itself). LOC is the
2554 location of the original expression. */
2557 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2561 tree type_expr = NULL_TREE;
2562 bool type_expr_const = true;
2563 type = groktypename (t, &type_expr, &type_expr_const);
2564 ret.value = c_sizeof (loc, type);
2565 ret.original_code = ERROR_MARK;
2566 ret.original_type = NULL;
2567 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2568 && c_vla_type_p (type))
2570 /* If the type is a [*] array, it is a VLA but is represented as
2571 having a size of zero. In such a case we must ensure that
2572 the result of sizeof does not get folded to a constant by
2573 c_fully_fold, because if the size is evaluated the result is
2574 not constant and so constraints on zero or negative size
2575 arrays must not be applied when this sizeof call is inside
2576 another array declarator. */
2578 type_expr = integer_zero_node;
2579 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2580 type_expr, ret.value);
2581 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2583 pop_maybe_used (type != error_mark_node
2584 ? C_TYPE_VARIABLE_SIZE (type) : false);
2588 /* Build a function call to function FUNCTION with parameters PARAMS.
2589 The function call is at LOC.
2590 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2591 TREE_VALUE of each node is a parameter-expression.
2592 FUNCTION's data type may be a function type or a pointer-to-function. */
2595 build_function_call (location_t loc, tree function, tree params)
2600 vec = VEC_alloc (tree, gc, list_length (params));
2601 for (; params; params = TREE_CHAIN (params))
2602 VEC_quick_push (tree, vec, TREE_VALUE (params));
2603 ret = build_function_call_vec (loc, function, vec, NULL);
2604 VEC_free (tree, gc, vec);
2608 /* Build a function call to function FUNCTION with parameters PARAMS.
2609 ORIGTYPES, if not NULL, is a vector of types; each element is
2610 either NULL or the original type of the corresponding element in
2611 PARAMS. The original type may differ from TREE_TYPE of the
2612 parameter for enums. FUNCTION's data type may be a function type
2613 or pointer-to-function. This function changes the elements of
2617 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2618 VEC(tree,gc) *origtypes)
2620 tree fntype, fundecl = 0;
2621 tree name = NULL_TREE, result;
2627 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2628 STRIP_TYPE_NOPS (function);
2630 /* Convert anything with function type to a pointer-to-function. */
2631 if (TREE_CODE (function) == FUNCTION_DECL)
2633 /* Implement type-directed function overloading for builtins.
2634 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2635 handle all the type checking. The result is a complete expression
2636 that implements this function call. */
2637 tem = resolve_overloaded_builtin (loc, function, params);
2641 name = DECL_NAME (function);
2644 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2645 function = function_to_pointer_conversion (loc, function);
2647 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2648 expressions, like those used for ObjC messenger dispatches. */
2649 if (!VEC_empty (tree, params))
2650 function = objc_rewrite_function_call (function,
2651 VEC_index (tree, params, 0));
2653 function = c_fully_fold (function, false, NULL);
2655 fntype = TREE_TYPE (function);
2657 if (TREE_CODE (fntype) == ERROR_MARK)
2658 return error_mark_node;
2660 if (!(TREE_CODE (fntype) == POINTER_TYPE
2661 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2663 error_at (loc, "called object %qE is not a function", function);
2664 return error_mark_node;
2667 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2668 current_function_returns_abnormally = 1;
2670 /* fntype now gets the type of function pointed to. */
2671 fntype = TREE_TYPE (fntype);
2673 /* Convert the parameters to the types declared in the
2674 function prototype, or apply default promotions. */
2676 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2679 return error_mark_node;
2681 /* Check that the function is called through a compatible prototype.
2682 If it is not, replace the call by a trap, wrapped up in a compound
2683 expression if necessary. This has the nice side-effect to prevent
2684 the tree-inliner from generating invalid assignment trees which may
2685 blow up in the RTL expander later. */
2686 if (CONVERT_EXPR_P (function)
2687 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2688 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2689 && !comptypes (fntype, TREE_TYPE (tem)))
2691 tree return_type = TREE_TYPE (fntype);
2692 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2696 /* This situation leads to run-time undefined behavior. We can't,
2697 therefore, simply error unless we can prove that all possible
2698 executions of the program must execute the code. */
2699 if (warning_at (loc, 0, "function called through a non-compatible type"))
2700 /* We can, however, treat "undefined" any way we please.
2701 Call abort to encourage the user to fix the program. */
2702 inform (loc, "if this code is reached, the program will abort");
2703 /* Before the abort, allow the function arguments to exit or
2705 for (i = 0; i < nargs; i++)
2706 trap = build2 (COMPOUND_EXPR, void_type_node,
2707 VEC_index (tree, params, i), trap);
2709 if (VOID_TYPE_P (return_type))
2711 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2713 "function with qualified void return type called");
2720 if (AGGREGATE_TYPE_P (return_type))
2721 rhs = build_compound_literal (loc, return_type,
2722 build_constructor (return_type, 0),
2725 rhs = fold_convert_loc (loc, return_type, integer_zero_node);
2727 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2732 argarray = VEC_address (tree, params);
2734 /* Check that arguments to builtin functions match the expectations. */
2736 && DECL_BUILT_IN (fundecl)
2737 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2738 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2739 return error_mark_node;
2741 /* Check that the arguments to the function are valid. */
2742 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2743 TYPE_ARG_TYPES (fntype));
2745 if (name != NULL_TREE
2746 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2748 if (require_constant_value)
2750 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2751 function, nargs, argarray);
2753 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2754 function, nargs, argarray);
2755 if (TREE_CODE (result) == NOP_EXPR
2756 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2757 STRIP_TYPE_NOPS (result);
2760 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2761 function, nargs, argarray);
2763 if (VOID_TYPE_P (TREE_TYPE (result)))
2765 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2767 "function with qualified void return type called");
2770 return require_complete_type (result);
2773 /* Convert the argument expressions in the vector VALUES
2774 to the types in the list TYPELIST.
2776 If TYPELIST is exhausted, or when an element has NULL as its type,
2777 perform the default conversions.
2779 ORIGTYPES is the original types of the expressions in VALUES. This
2780 holds the type of enum values which have been converted to integral
2781 types. It may be NULL.
2783 FUNCTION is a tree for the called function. It is used only for
2784 error messages, where it is formatted with %qE.
2786 This is also where warnings about wrong number of args are generated.
2788 Returns the actual number of arguments processed (which may be less
2789 than the length of VALUES in some error situations), or -1 on
2793 convert_arguments (tree typelist, VEC(tree,gc) *values,
2794 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2797 unsigned int parmnum;
2798 bool error_args = false;
2799 const bool type_generic = fundecl
2800 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2801 bool type_generic_remove_excess_precision = false;
2804 /* Change pointer to function to the function itself for
2806 if (TREE_CODE (function) == ADDR_EXPR
2807 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2808 function = TREE_OPERAND (function, 0);
2810 /* Handle an ObjC selector specially for diagnostics. */
2811 selector = objc_message_selector ();
2813 /* For type-generic built-in functions, determine whether excess
2814 precision should be removed (classification) or not
2817 && DECL_BUILT_IN (fundecl)
2818 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2820 switch (DECL_FUNCTION_CODE (fundecl))
2822 case BUILT_IN_ISFINITE:
2823 case BUILT_IN_ISINF:
2824 case BUILT_IN_ISINF_SIGN:
2825 case BUILT_IN_ISNAN:
2826 case BUILT_IN_ISNORMAL:
2827 case BUILT_IN_FPCLASSIFY:
2828 type_generic_remove_excess_precision = true;
2832 type_generic_remove_excess_precision = false;
2837 /* Scan the given expressions and types, producing individual
2838 converted arguments. */
2840 for (typetail = typelist, parmnum = 0;
2841 VEC_iterate (tree, values, parmnum, val);
2844 tree type = typetail ? TREE_VALUE (typetail) : 0;
2845 tree valtype = TREE_TYPE (val);
2846 tree rname = function;
2847 int argnum = parmnum + 1;
2848 const char *invalid_func_diag;
2849 bool excess_precision = false;
2853 if (type == void_type_node)
2855 error_at (input_location,
2856 "too many arguments to function %qE", function);
2857 if (fundecl && !DECL_BUILT_IN (fundecl))
2858 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2862 if (selector && argnum > 2)
2868 npc = null_pointer_constant_p (val);
2870 /* If there is excess precision and a prototype, convert once to
2871 the required type rather than converting via the semantic
2872 type. Likewise without a prototype a float value represented
2873 as long double should be converted once to double. But for
2874 type-generic classification functions excess precision must
2876 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2877 && (type || !type_generic || !type_generic_remove_excess_precision))
2879 val = TREE_OPERAND (val, 0);
2880 excess_precision = true;
2882 val = c_fully_fold (val, false, NULL);
2883 STRIP_TYPE_NOPS (val);
2885 val = require_complete_type (val);
2889 /* Formal parm type is specified by a function prototype. */
2891 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2893 error ("type of formal parameter %d is incomplete", parmnum + 1);
2900 /* Optionally warn about conversions that
2901 differ from the default conversions. */
2902 if (warn_traditional_conversion || warn_traditional)
2904 unsigned int formal_prec = TYPE_PRECISION (type);
2906 if (INTEGRAL_TYPE_P (type)
2907 && TREE_CODE (valtype) == REAL_TYPE)
2908 warning (0, "passing argument %d of %qE as integer "
2909 "rather than floating due to prototype",
2911 if (INTEGRAL_TYPE_P (type)
2912 && TREE_CODE (valtype) == COMPLEX_TYPE)
2913 warning (0, "passing argument %d of %qE as integer "
2914 "rather than complex due to prototype",
2916 else if (TREE_CODE (type) == COMPLEX_TYPE
2917 && TREE_CODE (valtype) == REAL_TYPE)
2918 warning (0, "passing argument %d of %qE as complex "
2919 "rather than floating due to prototype",
2921 else if (TREE_CODE (type) == REAL_TYPE
2922 && INTEGRAL_TYPE_P (valtype))
2923 warning (0, "passing argument %d of %qE as floating "
2924 "rather than integer due to prototype",
2926 else if (TREE_CODE (type) == COMPLEX_TYPE
2927 && INTEGRAL_TYPE_P (valtype))
2928 warning (0, "passing argument %d of %qE as complex "
2929 "rather than integer due to prototype",
2931 else if (TREE_CODE (type) == REAL_TYPE
2932 && TREE_CODE (valtype) == COMPLEX_TYPE)
2933 warning (0, "passing argument %d of %qE as floating "
2934 "rather than complex due to prototype",
2936 /* ??? At some point, messages should be written about
2937 conversions between complex types, but that's too messy
2939 else if (TREE_CODE (type) == REAL_TYPE
2940 && TREE_CODE (valtype) == REAL_TYPE)
2942 /* Warn if any argument is passed as `float',
2943 since without a prototype it would be `double'. */
2944 if (formal_prec == TYPE_PRECISION (float_type_node)
2945 && type != dfloat32_type_node)
2946 warning (0, "passing argument %d of %qE as %<float%> "
2947 "rather than %<double%> due to prototype",
2950 /* Warn if mismatch between argument and prototype
2951 for decimal float types. Warn of conversions with
2952 binary float types and of precision narrowing due to
2954 else if (type != valtype
2955 && (type == dfloat32_type_node
2956 || type == dfloat64_type_node
2957 || type == dfloat128_type_node
2958 || valtype == dfloat32_type_node
2959 || valtype == dfloat64_type_node
2960 || valtype == dfloat128_type_node)
2962 <= TYPE_PRECISION (valtype)
2963 || (type == dfloat128_type_node
2965 != dfloat64_type_node
2967 != dfloat32_type_node)))
2968 || (type == dfloat64_type_node
2970 != dfloat32_type_node))))
2971 warning (0, "passing argument %d of %qE as %qT "
2972 "rather than %qT due to prototype",
2973 argnum, rname, type, valtype);
2976 /* Detect integer changing in width or signedness.
2977 These warnings are only activated with
2978 -Wtraditional-conversion, not with -Wtraditional. */
2979 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2980 && INTEGRAL_TYPE_P (valtype))
2982 tree would_have_been = default_conversion (val);
2983 tree type1 = TREE_TYPE (would_have_been);
2985 if (TREE_CODE (type) == ENUMERAL_TYPE
2986 && (TYPE_MAIN_VARIANT (type)
2987 == TYPE_MAIN_VARIANT (valtype)))
2988 /* No warning if function asks for enum
2989 and the actual arg is that enum type. */
2991 else if (formal_prec != TYPE_PRECISION (type1))
2992 warning (OPT_Wtraditional_conversion,
2993 "passing argument %d of %qE "
2994 "with different width due to prototype",
2996 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2998 /* Don't complain if the formal parameter type
2999 is an enum, because we can't tell now whether
3000 the value was an enum--even the same enum. */
3001 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3003 else if (TREE_CODE (val) == INTEGER_CST
3004 && int_fits_type_p (val, type))
3005 /* Change in signedness doesn't matter
3006 if a constant value is unaffected. */
3008 /* If the value is extended from a narrower
3009 unsigned type, it doesn't matter whether we
3010 pass it as signed or unsigned; the value
3011 certainly is the same either way. */
3012 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3013 && TYPE_UNSIGNED (valtype))
3015 else if (TYPE_UNSIGNED (type))
3016 warning (OPT_Wtraditional_conversion,
3017 "passing argument %d of %qE "
3018 "as unsigned due to prototype",
3021 warning (OPT_Wtraditional_conversion,
3022 "passing argument %d of %qE "
3023 "as signed due to prototype", argnum, rname);
3027 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3028 sake of better warnings from convert_and_check. */
3029 if (excess_precision)
3030 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3031 origtype = (origtypes == NULL
3033 : VEC_index (tree, origtypes, parmnum));
3034 parmval = convert_for_assignment (input_location, type, val,
3035 origtype, ic_argpass, npc,
3039 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3040 && INTEGRAL_TYPE_P (type)
3041 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3042 parmval = default_conversion (parmval);
3045 else if (TREE_CODE (valtype) == REAL_TYPE
3046 && (TYPE_PRECISION (valtype)
3047 < TYPE_PRECISION (double_type_node))
3048 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3053 /* Convert `float' to `double'. */
3054 parmval = convert (double_type_node, val);
3056 else if (excess_precision && !type_generic)
3057 /* A "double" argument with excess precision being passed
3058 without a prototype or in variable arguments. */
3059 parmval = convert (valtype, val);
3060 else if ((invalid_func_diag =
3061 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3063 error (invalid_func_diag);
3067 /* Convert `short' and `char' to full-size `int'. */
3068 parmval = default_conversion (val);
3070 VEC_replace (tree, values, parmnum, parmval);
3071 if (parmval == error_mark_node)
3075 typetail = TREE_CHAIN (typetail);
3078 gcc_assert (parmnum == VEC_length (tree, values));
3080 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3082 error_at (input_location,
3083 "too few arguments to function %qE", function);
3084 if (fundecl && !DECL_BUILT_IN (fundecl))
3085 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3089 return error_args ? -1 : (int) parmnum;
3092 /* This is the entry point used by the parser to build unary operators
3093 in the input. CODE, a tree_code, specifies the unary operator, and
3094 ARG is the operand. For unary plus, the C parser currently uses
3095 CONVERT_EXPR for code.
3097 LOC is the location to use for the tree generated.
3101 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3103 struct c_expr result;
3105 result.value = build_unary_op (loc, code, arg.value, 0);
3106 result.original_code = code;
3107 result.original_type = NULL;
3109 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3110 overflow_warning (loc, result.value);
3115 /* This is the entry point used by the parser to build binary operators
3116 in the input. CODE, a tree_code, specifies the binary operator, and
3117 ARG1 and ARG2 are the operands. In addition to constructing the
3118 expression, we check for operands that were written with other binary
3119 operators in a way that is likely to confuse the user.
3121 LOCATION is the location of the binary operator. */
3124 parser_build_binary_op (location_t location, enum tree_code code,
3125 struct c_expr arg1, struct c_expr arg2)
3127 struct c_expr result;
3129 enum tree_code code1 = arg1.original_code;
3130 enum tree_code code2 = arg2.original_code;
3131 tree type1 = (arg1.original_type
3132 ? arg1.original_type
3133 : TREE_TYPE (arg1.value));
3134 tree type2 = (arg2.original_type
3135 ? arg2.original_type
3136 : TREE_TYPE (arg2.value));
3138 result.value = build_binary_op (location, code,
3139 arg1.value, arg2.value, 1);
3140 result.original_code = code;
3141 result.original_type = NULL;
3143 if (TREE_CODE (result.value) == ERROR_MARK)
3146 if (location != UNKNOWN_LOCATION)
3147 protected_set_expr_location (result.value, location);
3149 /* Check for cases such as x+y<<z which users are likely
3151 if (warn_parentheses)
3152 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3154 if (warn_logical_op)
3155 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3156 code1, arg1.value, code2, arg2.value);
3158 /* Warn about comparisons against string literals, with the exception
3159 of testing for equality or inequality of a string literal with NULL. */
3160 if (code == EQ_EXPR || code == NE_EXPR)
3162 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3163 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3164 warning_at (location, OPT_Waddress,
3165 "comparison with string literal results in unspecified behavior");
3167 else if (TREE_CODE_CLASS (code) == tcc_comparison
3168 && (code1 == STRING_CST || code2 == STRING_CST))
3169 warning_at (location, OPT_Waddress,
3170 "comparison with string literal results in unspecified behavior");
3172 if (TREE_OVERFLOW_P (result.value)
3173 && !TREE_OVERFLOW_P (arg1.value)
3174 && !TREE_OVERFLOW_P (arg2.value))
3175 overflow_warning (location, result.value);
3177 /* Warn about comparisons of different enum types. */
3178 if (warn_enum_compare
3179 && TREE_CODE_CLASS (code) == tcc_comparison
3180 && TREE_CODE (type1) == ENUMERAL_TYPE
3181 && TREE_CODE (type2) == ENUMERAL_TYPE
3182 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3183 warning_at (location, OPT_Wenum_compare,
3184 "comparison between %qT and %qT",
3190 /* Return a tree for the difference of pointers OP0 and OP1.
3191 The resulting tree has type int. */
3194 pointer_diff (location_t loc, tree op0, tree op1)
3196 tree restype = ptrdiff_type_node;
3197 tree result, inttype;
3199 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3200 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3201 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3202 tree con0, con1, lit0, lit1;
3203 tree orig_op1 = op1;
3205 /* If the operands point into different address spaces, we need to
3206 explicitly convert them to pointers into the common address space
3207 before we can subtract the numerical address values. */
3210 addr_space_t as_common;
3213 /* Determine the common superset address space. This is guaranteed
3214 to exist because the caller verified that comp_target_types
3215 returned non-zero. */
3216 if (!addr_space_superset (as0, as1, &as_common))
3219 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3220 op0 = convert (common_type, op0);
3221 op1 = convert (common_type, op1);
3224 /* Determine integer type to perform computations in. This will usually
3225 be the same as the result type (ptrdiff_t), but may need to be a wider
3226 type if pointers for the address space are wider than ptrdiff_t. */
3227 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3228 inttype = lang_hooks.types.type_for_size
3229 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3234 if (TREE_CODE (target_type) == VOID_TYPE)
3235 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3236 "pointer of type %<void *%> used in subtraction");
3237 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3238 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3239 "pointer to a function used in subtraction");
3241 /* If the conversion to ptrdiff_type does anything like widening or
3242 converting a partial to an integral mode, we get a convert_expression
3243 that is in the way to do any simplifications.
3244 (fold-const.c doesn't know that the extra bits won't be needed.
3245 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3246 different mode in place.)
3247 So first try to find a common term here 'by hand'; we want to cover
3248 at least the cases that occur in legal static initializers. */
3249 if (CONVERT_EXPR_P (op0)
3250 && (TYPE_PRECISION (TREE_TYPE (op0))
3251 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3252 con0 = TREE_OPERAND (op0, 0);
3255 if (CONVERT_EXPR_P (op1)
3256 && (TYPE_PRECISION (TREE_TYPE (op1))
3257 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3258 con1 = TREE_OPERAND (op1, 0);
3262 if (TREE_CODE (con0) == PLUS_EXPR)
3264 lit0 = TREE_OPERAND (con0, 1);
3265 con0 = TREE_OPERAND (con0, 0);
3268 lit0 = integer_zero_node;
3270 if (TREE_CODE (con1) == PLUS_EXPR)
3272 lit1 = TREE_OPERAND (con1, 1);
3273 con1 = TREE_OPERAND (con1, 0);
3276 lit1 = integer_zero_node;
3278 if (operand_equal_p (con0, con1, 0))
3285 /* First do the subtraction as integers;
3286 then drop through to build the divide operator.
3287 Do not do default conversions on the minus operator
3288 in case restype is a short type. */
3290 op0 = build_binary_op (loc,
3291 MINUS_EXPR, convert (inttype, op0),
3292 convert (inttype, op1), 0);
3293 /* This generates an error if op1 is pointer to incomplete type. */
3294 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3295 error_at (loc, "arithmetic on pointer to an incomplete type");
3297 /* This generates an error if op0 is pointer to incomplete type. */
3298 op1 = c_size_in_bytes (target_type);
3300 /* Divide by the size, in easiest possible way. */
3301 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3302 op0, convert (inttype, op1));
3304 /* Convert to final result type if necessary. */
3305 return convert (restype, result);
3308 /* Construct and perhaps optimize a tree representation
3309 for a unary operation. CODE, a tree_code, specifies the operation
3310 and XARG is the operand.
3311 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3312 the default promotions (such as from short to int).
3313 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3314 allows non-lvalues; this is only used to handle conversion of non-lvalue
3315 arrays to pointers in C99.
3317 LOCATION is the location of the operator. */
3320 build_unary_op (location_t location,
3321 enum tree_code code, tree xarg, int flag)
3323 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3326 enum tree_code typecode;
3328 tree ret = error_mark_node;
3329 tree eptype = NULL_TREE;
3330 int noconvert = flag;
3331 const char *invalid_op_diag;
3334 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3336 arg = remove_c_maybe_const_expr (arg);
3338 if (code != ADDR_EXPR)
3339 arg = require_complete_type (arg);
3341 typecode = TREE_CODE (TREE_TYPE (arg));
3342 if (typecode == ERROR_MARK)
3343 return error_mark_node;
3344 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3345 typecode = INTEGER_TYPE;
3347 if ((invalid_op_diag
3348 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3350 error_at (location, invalid_op_diag);
3351 return error_mark_node;
3354 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3356 eptype = TREE_TYPE (arg);
3357 arg = TREE_OPERAND (arg, 0);
3363 /* This is used for unary plus, because a CONVERT_EXPR
3364 is enough to prevent anybody from looking inside for
3365 associativity, but won't generate any code. */
3366 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3367 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3368 || typecode == VECTOR_TYPE))
3370 error_at (location, "wrong type argument to unary plus");
3371 return error_mark_node;
3373 else if (!noconvert)
3374 arg = default_conversion (arg);
3375 arg = non_lvalue_loc (location, arg);
3379 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3380 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3381 || typecode == VECTOR_TYPE))
3383 error_at (location, "wrong type argument to unary minus");
3384 return error_mark_node;
3386 else if (!noconvert)
3387 arg = default_conversion (arg);
3391 /* ~ works on integer types and non float vectors. */
3392 if (typecode == INTEGER_TYPE
3393 || (typecode == VECTOR_TYPE
3394 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3397 arg = default_conversion (arg);
3399 else if (typecode == COMPLEX_TYPE)
3402 pedwarn (location, OPT_pedantic,
3403 "ISO C does not support %<~%> for complex conjugation");
3405 arg = default_conversion (arg);
3409 error_at (location, "wrong type argument to bit-complement");
3410 return error_mark_node;
3415 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3417 error_at (location, "wrong type argument to abs");
3418 return error_mark_node;
3420 else if (!noconvert)
3421 arg = default_conversion (arg);
3425 /* Conjugating a real value is a no-op, but allow it anyway. */
3426 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3427 || typecode == COMPLEX_TYPE))
3429 error_at (location, "wrong type argument to conjugation");
3430 return error_mark_node;
3432 else if (!noconvert)
3433 arg = default_conversion (arg);
3436 case TRUTH_NOT_EXPR:
3437 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3438 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3439 && typecode != COMPLEX_TYPE)