1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 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.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* This file contains the functions for converting C++ expressions
25 to different data types. The only entry point is `convert'.
26 Every language front end must have a `convert' function
27 but what kind of conversions it does will depend on the language. */
31 #include "coretypes.h"
41 static tree
cp_convert_to_pointer (tree
, tree
);
42 static tree
convert_to_pointer_force (tree
, tree
);
43 static tree
build_type_conversion (tree
, tree
);
44 static tree
build_up_reference (tree
, tree
, int, tree
);
45 static void warn_ref_binding (tree
, tree
, tree
);
47 /* Change of width--truncation and extension of integers or reals--
48 is represented with NOP_EXPR. Proper functioning of many things
49 assumes that no other conversions can be NOP_EXPRs.
51 Conversion between integer and pointer is represented with CONVERT_EXPR.
52 Converting integer to real uses FLOAT_EXPR
53 and real to integer uses FIX_TRUNC_EXPR.
55 Here is a list of all the functions that assume that widening and
56 narrowing is always done with a NOP_EXPR:
57 In convert.c, convert_to_integer.
58 In c-typeck.c, build_binary_op_nodefault (boolean ops),
59 and c_common_truthvalue_conversion.
60 In expr.c: expand_expr, for operands of a MULT_EXPR.
61 In fold-const.c: fold.
62 In tree.c: get_narrower and get_unwidened.
64 C++: in multiple-inheritance, converting between pointers may involve
65 adjusting them by a delta stored within the class definition. */
67 /* Subroutines of `convert'. */
69 /* if converting pointer to pointer
70 if dealing with classes, check for derived->base or vice versa
71 else if dealing with method pointers, delegate
73 else if converting class, pass off to build_type_conversion
74 else try C-style pointer conversion. */
77 cp_convert_to_pointer (tree type
, tree expr
)
79 tree intype
= TREE_TYPE (expr
);
82 if (intype
== error_mark_node
)
83 return error_mark_node
;
85 if (MAYBE_CLASS_TYPE_P (intype
))
87 intype
= complete_type (intype
);
88 if (!COMPLETE_TYPE_P (intype
))
90 error ("can%'t convert from incomplete type %qT to %qT",
92 return error_mark_node
;
95 rval
= build_type_conversion (type
, expr
);
98 if (rval
== error_mark_node
)
99 error ("conversion of %qE from %qT to %qT is ambiguous",
105 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
106 if (TREE_CODE (type
) == POINTER_TYPE
107 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
108 || VOID_TYPE_P (TREE_TYPE (type
))))
110 if (TYPE_PTRMEMFUNC_P (intype
)
111 || TREE_CODE (intype
) == METHOD_TYPE
)
112 return convert_member_func_to_ptr (type
, expr
);
113 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
114 return build_nop (type
, expr
);
115 intype
= TREE_TYPE (expr
);
118 if (expr
== error_mark_node
)
119 return error_mark_node
;
121 form
= TREE_CODE (intype
);
123 if (POINTER_TYPE_P (intype
))
125 intype
= TYPE_MAIN_VARIANT (intype
);
127 if (TYPE_MAIN_VARIANT (type
) != intype
128 && TREE_CODE (type
) == POINTER_TYPE
129 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
130 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type
))
131 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype
))
132 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
134 enum tree_code code
= PLUS_EXPR
;
140 intype_class
= TREE_TYPE (intype
);
141 type_class
= TREE_TYPE (type
);
143 same_p
= same_type_p (TYPE_MAIN_VARIANT (intype_class
),
144 TYPE_MAIN_VARIANT (type_class
));
146 /* Try derived to base conversion. */
148 binfo
= lookup_base (intype_class
, type_class
, ba_check
, NULL
);
149 if (!same_p
&& !binfo
)
151 /* Try base to derived conversion. */
152 binfo
= lookup_base (type_class
, intype_class
, ba_check
, NULL
);
155 if (binfo
== error_mark_node
)
156 return error_mark_node
;
160 expr
= build_base_path (code
, expr
, binfo
, 0);
161 /* Add any qualifier conversions. */
162 return build_nop (type
, expr
);
166 if (TYPE_PTRMEMFUNC_P (type
))
168 error ("cannot convert %qE from type %qT to type %qT",
170 return error_mark_node
;
173 return build_nop (type
, expr
);
175 else if ((TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
176 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
177 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/false,
178 /*c_cast_p=*/false, tf_warning_or_error
);
179 else if (TYPE_PTRMEMFUNC_P (intype
))
183 if (TREE_CODE (expr
) == PTRMEM_CST
)
184 return cp_convert_to_pointer (type
,
185 PTRMEM_CST_MEMBER (expr
));
186 else if (TREE_CODE (expr
) == OFFSET_REF
)
188 tree object
= TREE_OPERAND (expr
, 0);
189 return get_member_function_from_ptrfunc (&object
,
190 TREE_OPERAND (expr
, 1));
193 error ("cannot convert %qE from type %qT to type %qT",
195 return error_mark_node
;
198 if (null_ptr_cst_p (expr
))
200 if (TYPE_PTRMEMFUNC_P (type
))
201 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0,
202 /*c_cast_p=*/false, tf_warning_or_error
);
204 if (TYPE_PTRMEM_P (type
))
206 /* A NULL pointer-to-member is represented by -1, not by
208 expr
= build_int_cst_type (type
, -1);
211 expr
= build_int_cst (type
, 0);
215 else if (TYPE_PTR_TO_MEMBER_P (type
) && INTEGRAL_CODE_P (form
))
217 error ("invalid conversion from %qT to %qT", intype
, type
);
218 return error_mark_node
;
221 if (INTEGRAL_CODE_P (form
))
223 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
224 return build1 (CONVERT_EXPR
, type
, expr
);
225 expr
= cp_convert (c_common_type_for_size (POINTER_SIZE
, 0), expr
);
226 /* Modes may be different but sizes should be the same. There
227 is supposed to be some integral type that is the same width
229 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
230 == GET_MODE_SIZE (TYPE_MODE (type
)));
232 return convert_to_pointer (type
, expr
);
235 if (type_unknown_p (expr
))
236 return instantiate_type (type
, expr
, tf_warning_or_error
);
238 error ("cannot convert %qE from type %qT to type %qT",
240 return error_mark_node
;
243 /* Like convert, except permit conversions to take place which
244 are not normally allowed due to access restrictions
245 (such as conversion from sub-type to private super-type). */
248 convert_to_pointer_force (tree type
, tree expr
)
250 tree intype
= TREE_TYPE (expr
);
251 enum tree_code form
= TREE_CODE (intype
);
253 if (form
== POINTER_TYPE
)
255 intype
= TYPE_MAIN_VARIANT (intype
);
257 if (TYPE_MAIN_VARIANT (type
) != intype
258 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
259 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type
))
260 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype
))
261 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
263 enum tree_code code
= PLUS_EXPR
;
266 binfo
= lookup_base (TREE_TYPE (intype
), TREE_TYPE (type
),
270 binfo
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
274 if (binfo
== error_mark_node
)
275 return error_mark_node
;
278 expr
= build_base_path (code
, expr
, binfo
, 0);
279 if (expr
== error_mark_node
)
280 return error_mark_node
;
281 /* Add any qualifier conversions. */
282 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr
)),
284 expr
= build_nop (type
, expr
);
290 return cp_convert_to_pointer (type
, expr
);
293 /* We are passing something to a function which requires a reference.
294 The type we are interested in is in TYPE. The initial
295 value we have to begin with is in ARG.
297 FLAGS controls how we manage access checking.
298 DIRECT_BIND in FLAGS controls how any temporaries are generated.
299 If DIRECT_BIND is set, DECL is the reference we're binding to. */
302 build_up_reference (tree type
, tree arg
, int flags
, tree decl
)
305 tree argtype
= TREE_TYPE (arg
);
306 tree target_type
= TREE_TYPE (type
);
308 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
310 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
312 /* Create a new temporary variable. We can't just use a TARGET_EXPR
313 here because it needs to live as long as DECL. */
316 arg
= make_temporary_var_for_ref_to_temp (decl
, target_type
);
318 /* Process the initializer for the declaration. */
319 DECL_INITIAL (arg
) = targ
;
320 cp_finish_decl (arg
, targ
, /*init_const_expr_p=*/false, NULL_TREE
,
321 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
323 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
324 return get_target_expr (arg
);
326 /* If we had a way to wrap this up, and say, if we ever needed its
327 address, transform all occurrences of the register, into a memory
328 reference we could win better. */
329 rval
= cp_build_addr_expr (arg
, tf_warning_or_error
);
330 if (rval
== error_mark_node
)
331 return error_mark_node
;
333 if ((flags
& LOOKUP_PROTECT
)
334 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
335 && MAYBE_CLASS_TYPE_P (argtype
)
336 && MAYBE_CLASS_TYPE_P (target_type
))
338 /* We go through lookup_base for the access control. */
339 tree binfo
= lookup_base (argtype
, target_type
, ba_check
, NULL
);
340 if (binfo
== error_mark_node
)
341 return error_mark_node
;
342 if (binfo
== NULL_TREE
)
343 return error_not_base_type (target_type
, argtype
);
344 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1);
348 = convert_to_pointer_force (build_pointer_type (target_type
), rval
);
349 return build_nop (type
, rval
);
352 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
353 INTYPE is the original rvalue type and DECL is an optional _DECL node
356 [dcl.init.ref] says that if an rvalue is used to
357 initialize a reference, then the reference must be to a
358 non-volatile const type. */
361 warn_ref_binding (tree reftype
, tree intype
, tree decl
)
363 tree ttl
= TREE_TYPE (reftype
);
365 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl
))
369 if (CP_TYPE_VOLATILE_P (ttl
) && decl
)
370 msg
= G_("initialization of volatile reference type %q#T from "
371 "rvalue of type %qT");
372 else if (CP_TYPE_VOLATILE_P (ttl
))
373 msg
= G_("conversion to volatile reference type %q#T "
374 "from rvalue of type %qT");
376 msg
= G_("initialization of non-const reference type %q#T from "
377 "rvalue of type %qT");
379 msg
= G_("conversion to non-const reference type %q#T from "
380 "rvalue of type %qT");
382 permerror (input_location
, msg
, reftype
, intype
);
386 /* For C++: Only need to do one-level references, but cannot
387 get tripped up on signed/unsigned differences.
389 DECL is either NULL_TREE or the _DECL node for a reference that is being
390 initialized. It can be error_mark_node if we don't know the _DECL but
391 we know it's an initialization. */
394 convert_to_reference (tree reftype
, tree expr
, int convtype
,
395 int flags
, tree decl
)
397 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
399 tree rval
= NULL_TREE
;
400 tree rval_as_conversion
= NULL_TREE
;
401 bool can_convert_intype_to_type
;
403 if (TREE_CODE (type
) == FUNCTION_TYPE
404 && TREE_TYPE (expr
) == unknown_type_node
)
405 expr
= instantiate_type (type
, expr
,
406 (flags
& LOOKUP_COMPLAIN
)
407 ? tf_warning_or_error
: tf_none
);
409 if (expr
== error_mark_node
)
410 return error_mark_node
;
412 intype
= TREE_TYPE (expr
);
414 gcc_assert (TREE_CODE (intype
) != REFERENCE_TYPE
);
415 gcc_assert (TREE_CODE (reftype
) == REFERENCE_TYPE
);
417 intype
= TYPE_MAIN_VARIANT (intype
);
419 can_convert_intype_to_type
= can_convert (type
, intype
);
420 if (!can_convert_intype_to_type
421 && (convtype
& CONV_IMPLICIT
) && MAYBE_CLASS_TYPE_P (intype
)
422 && ! (flags
& LOOKUP_NO_CONVERSION
))
424 /* Look for a user-defined conversion to lvalue that we can use. */
427 = build_type_conversion (reftype
, expr
);
429 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
430 && real_lvalue_p (rval_as_conversion
))
432 expr
= rval_as_conversion
;
433 rval_as_conversion
= NULL_TREE
;
435 can_convert_intype_to_type
= 1;
439 if (((convtype
& CONV_STATIC
) && can_convert (intype
, type
))
440 || ((convtype
& CONV_IMPLICIT
) && can_convert_intype_to_type
))
442 if (flags
& LOOKUP_COMPLAIN
)
444 tree ttl
= TREE_TYPE (reftype
);
445 tree ttr
= lvalue_type (expr
);
447 if (! real_lvalue_p (expr
))
448 warn_ref_binding (reftype
, intype
, decl
);
450 if (! (convtype
& CONV_CONST
)
451 && !at_least_as_qualified_p (ttl
, ttr
))
452 permerror (input_location
, "conversion from %qT to %qT discards qualifiers",
456 return build_up_reference (reftype
, expr
, flags
, decl
);
458 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
460 /* When casting an lvalue to a reference type, just convert into
461 a pointer to the new type and deference it. This is allowed
462 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
463 should be done directly (jason). (int &)ri ---> *(int*)&ri */
465 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
467 if (TREE_CODE (intype
) == POINTER_TYPE
468 && (comptypes (TREE_TYPE (intype
), type
,
469 COMPARE_BASE
| COMPARE_DERIVED
)))
470 warning (0, "casting %qT to %qT does not dereference pointer",
473 rval
= cp_build_addr_expr (expr
, tf_warning_or_error
);
474 if (rval
!= error_mark_node
)
475 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
477 if (rval
!= error_mark_node
)
478 rval
= build1 (NOP_EXPR
, reftype
, rval
);
482 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
483 ICR_CONVERTING
, 0, 0,
484 tf_warning_or_error
);
485 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
487 warn_ref_binding (reftype
, intype
, decl
);
488 rval
= build_up_reference (reftype
, rval
, flags
, decl
);
493 /* If we found a way to convert earlier, then use it. */
497 if (flags
& LOOKUP_COMPLAIN
)
498 error ("cannot convert type %qT to type %qT", intype
, reftype
);
500 return error_mark_node
;
503 /* We are using a reference VAL for its value. Bash that reference all the
504 way down to its lowest form. */
507 convert_from_reference (tree val
)
510 && TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
512 tree t
= TREE_TYPE (TREE_TYPE (val
));
513 tree ref
= build1 (INDIRECT_REF
, t
, val
);
516 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
517 so that we get the proper error message if the result is used
518 to assign to. Also, &* is supposed to be a no-op. */
519 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
520 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
521 TREE_SIDE_EFFECTS (ref
)
522 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (val
));
529 /* Really perform an lvalue-to-rvalue conversion, including copying an
530 argument of class type into a temporary. */
533 force_rvalue (tree expr
, tsubst_flags_t complain
)
535 tree type
= TREE_TYPE (expr
);
536 if (MAYBE_CLASS_TYPE_P (type
) && TREE_CODE (expr
) != TARGET_EXPR
)
538 VEC(tree
,gc
) *args
= make_tree_vector_single (expr
);
539 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
540 &args
, type
, LOOKUP_NORMAL
, complain
);
541 release_tree_vector (args
);
542 expr
= build_cplus_new (type
, expr
, complain
);
545 expr
= decay_conversion (expr
);
551 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
552 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
556 ignore_overflows (tree expr
, tree orig
)
558 if (TREE_CODE (expr
) == INTEGER_CST
559 && TREE_CODE (orig
) == INTEGER_CST
560 && TREE_OVERFLOW (expr
) != TREE_OVERFLOW (orig
))
562 gcc_assert (!TREE_OVERFLOW (orig
));
563 /* Ensure constant sharing. */
564 expr
= build_int_cst_wide (TREE_TYPE (expr
),
565 TREE_INT_CST_LOW (expr
),
566 TREE_INT_CST_HIGH (expr
));
571 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
575 cp_fold_convert (tree type
, tree expr
)
577 tree conv
= fold_convert (type
, expr
);
578 conv
= ignore_overflows (conv
, expr
);
582 /* C++ conversions, preference to static cast conversions. */
585 cp_convert (tree type
, tree expr
)
587 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
);
590 /* C++ equivalent of convert_and_check but using cp_convert as the
593 Convert EXPR to TYPE, warning about conversion problems with constants.
594 Invoke this function on every expression that is converted implicitly,
595 i.e. because of language rules and not because of an explicit cast. */
598 cp_convert_and_check (tree type
, tree expr
)
602 if (TREE_TYPE (expr
) == type
)
605 result
= cp_convert (type
, expr
);
607 if (c_inhibit_evaluation_warnings
== 0
608 && !TREE_OVERFLOW_P (expr
)
609 && result
!= error_mark_node
)
610 warnings_for_convert_and_check (type
, expr
, result
);
617 FLAGS indicates how we should behave. */
620 ocp_convert (tree type
, tree expr
, int convtype
, int flags
)
623 enum tree_code code
= TREE_CODE (type
);
624 const char *invalid_conv_diag
;
627 if (error_operand_p (e
) || type
== error_mark_node
)
628 return error_mark_node
;
630 complete_type (type
);
631 complete_type (TREE_TYPE (expr
));
633 if ((invalid_conv_diag
634 = targetm
.invalid_conversion (TREE_TYPE (expr
), type
)))
636 error (invalid_conv_diag
);
637 return error_mark_node
;
640 /* FIXME remove when moving to c_fully_fold model. */
641 /* FIXME do we still need this test? */
642 if (!CLASS_TYPE_P (type
))
643 e
= integral_constant_value (e
);
644 if (error_operand_p (e
))
645 return error_mark_node
;
647 if (MAYBE_CLASS_TYPE_P (type
) && (convtype
& CONV_FORCE_TEMP
))
648 /* We need a new temporary; don't take this shortcut. */;
649 else if (same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (e
)))
651 if (same_type_p (type
, TREE_TYPE (e
)))
652 /* The call to fold will not always remove the NOP_EXPR as
653 might be expected, since if one of the types is a typedef;
654 the comparison in fold is just equality of pointers, not a
655 call to comptypes. We don't call fold in this case because
656 that can result in infinite recursion; fold will call
657 convert, which will call ocp_convert, etc. */
659 /* For complex data types, we need to perform componentwise
661 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
662 return fold_if_not_in_template (convert_to_complex (type
, e
));
663 else if (TREE_CODE (e
) == TARGET_EXPR
)
665 /* Don't build a NOP_EXPR of class type. Instead, change the
666 type of the temporary. */
667 TREE_TYPE (e
) = TREE_TYPE (TARGET_EXPR_SLOT (e
)) = type
;
672 /* We shouldn't be treating objects of ADDRESSABLE type as
674 gcc_assert (!TREE_ADDRESSABLE (type
));
675 return fold_if_not_in_template (build_nop (type
, e
));
679 e1
= targetm
.convert_to_type (type
, e
);
683 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
685 e
= convert_to_void (e
, ICV_CAST
, tf_warning_or_error
);
689 if (INTEGRAL_CODE_P (code
))
691 tree intype
= TREE_TYPE (e
);
694 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
696 /* enum = enum, enum = int, enum = float, (enum)pointer are all
698 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
699 || TREE_CODE (intype
) == REAL_TYPE
)
700 && ! (convtype
& CONV_STATIC
))
701 || TREE_CODE (intype
) == POINTER_TYPE
)
703 if (flags
& LOOKUP_COMPLAIN
)
704 permerror (input_location
, "conversion from %q#T to %q#T", intype
, type
);
706 if (!flag_permissive
)
707 return error_mark_node
;
710 /* [expr.static.cast]
712 8. A value of integral or enumeration type can be explicitly
713 converted to an enumeration type. The value is unchanged if
714 the original value is within the range of the enumeration
715 values. Otherwise, the resulting enumeration value is
717 if (TREE_CODE (expr
) == INTEGER_CST
718 && !int_fits_type_p (expr
, ENUM_UNDERLYING_TYPE (type
)))
719 warning (OPT_Wconversion
,
720 "the result of the conversion is unspecified because "
721 "%qE is outside the range of type %qT",
724 if (MAYBE_CLASS_TYPE_P (intype
))
727 rval
= build_type_conversion (type
, e
);
730 if (flags
& LOOKUP_COMPLAIN
)
731 error ("%q#T used where a %qT was expected", intype
, type
);
732 return error_mark_node
;
734 if (code
== BOOLEAN_TYPE
)
736 /* We can't implicitly convert a scoped enum to bool, so convert
737 to the underlying type first. */
738 if (SCOPED_ENUM_P (intype
) && (convtype
& CONV_STATIC
))
739 e
= build_nop (ENUM_UNDERLYING_TYPE (intype
), e
);
740 return cp_truthvalue_conversion (e
);
743 converted
= fold_if_not_in_template (convert_to_integer (type
, e
));
745 /* Ignore any integer overflow caused by the conversion. */
746 return ignore_overflows (converted
, e
);
748 if (NULLPTR_TYPE_P (type
) && e
&& null_ptr_cst_p (e
))
750 if (POINTER_TYPE_P (type
) || TYPE_PTR_TO_MEMBER_P (type
))
751 return fold_if_not_in_template (cp_convert_to_pointer (type
, e
));
752 if (code
== VECTOR_TYPE
)
754 tree in_vtype
= TREE_TYPE (e
);
755 if (MAYBE_CLASS_TYPE_P (in_vtype
))
758 ret_val
= build_type_conversion (type
, e
);
761 if (flags
& LOOKUP_COMPLAIN
)
762 error ("%q#T used where a %qT was expected", in_vtype
, type
);
763 return error_mark_node
;
765 return fold_if_not_in_template (convert_to_vector (type
, e
));
767 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
769 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e
)))
772 rval
= build_type_conversion (type
, e
);
776 if (flags
& LOOKUP_COMPLAIN
)
777 error ("%q#T used where a floating point value was expected",
780 if (code
== REAL_TYPE
)
781 return fold_if_not_in_template (convert_to_real (type
, e
));
782 else if (code
== COMPLEX_TYPE
)
783 return fold_if_not_in_template (convert_to_complex (type
, e
));
786 /* New C++ semantics: since assignment is now based on
787 memberwise copying, if the rhs type is derived from the
788 lhs type, then we may still do a conversion. */
789 if (RECORD_OR_UNION_CODE_P (code
))
791 tree dtype
= TREE_TYPE (e
);
792 tree ctor
= NULL_TREE
;
794 dtype
= TYPE_MAIN_VARIANT (dtype
);
796 /* Conversion between aggregate types. New C++ semantics allow
797 objects of derived type to be cast to objects of base type.
798 Old semantics only allowed this between pointers.
800 There may be some ambiguity between using a constructor
801 vs. using a type conversion operator when both apply. */
805 if (abstract_virtuals_error (NULL_TREE
, type
))
806 return error_mark_node
;
808 if (BRACE_ENCLOSED_INITIALIZER_P (ctor
))
809 ctor
= perform_implicit_conversion (type
, ctor
, tf_warning_or_error
);
810 else if ((flags
& LOOKUP_ONLYCONVERTING
)
811 && ! (CLASS_TYPE_P (dtype
) && DERIVED_FROM_P (type
, dtype
)))
812 /* For copy-initialization, first we create a temp of the proper type
813 with a user-defined conversion sequence, then we direct-initialize
814 the target with the temp (see [dcl.init]). */
815 ctor
= build_user_type_conversion (type
, ctor
, flags
);
818 VEC(tree
,gc
) *ctor_vec
= make_tree_vector_single (ctor
);
819 ctor
= build_special_member_call (NULL_TREE
,
820 complete_ctor_identifier
,
823 tf_warning_or_error
);
824 release_tree_vector (ctor_vec
);
827 return build_cplus_new (type
, ctor
, tf_warning_or_error
);
830 if (flags
& LOOKUP_COMPLAIN
)
832 /* If the conversion failed and expr was an invalid use of pointer to
833 member function, try to report a meaningful error. */
834 if (invalid_nonstatic_memfn_p (expr
, tf_warning_or_error
))
835 /* We displayed the error message. */;
837 error ("conversion from %qT to non-scalar type %qT requested",
838 TREE_TYPE (expr
), type
);
840 return error_mark_node
;
843 /* When an expression is used in a void context, its value is discarded and
844 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
845 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
846 in a void context. The C++ standard does not define what an `access' to an
847 object is, but there is reason to believe that it is the lvalue to rvalue
848 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
849 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
850 indicates that volatile semantics should be the same between C and C++
851 where ever possible. C leaves it implementation defined as to what
852 constitutes an access to a volatile. So, we interpret `*vp' as a read of
853 the volatile object `vp' points to, unless that is an incomplete type. For
854 volatile references we do not do this interpretation, because that would
855 make it impossible to ignore the reference return value from functions. We
856 issue warnings in the confusing cases.
858 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
859 to void via a cast. If an expression is being implicitly converted, IMPLICIT
860 indicates the context of the implicit conversion. */
863 convert_to_void (tree expr
, impl_conv_void implicit
, tsubst_flags_t complain
)
865 if (expr
== error_mark_node
866 || TREE_TYPE (expr
) == error_mark_node
)
867 return error_mark_node
;
869 if (implicit
== ICV_CAST
)
870 mark_exp_read (expr
);
875 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
876 exprv
= TREE_OPERAND (exprv
, 1);
878 || handled_component_p (exprv
)
879 || TREE_CODE (exprv
) == INDIRECT_REF
)
880 /* Expr is not being 'used' here, otherwise we whould have
881 called mark_{rl}value_use use here, which would have in turn
882 called mark_exp_read. Rather, we call mark_exp_read directly
883 to avoid some warnings when
884 -Wunused-but-set-{variable,parameter} is in effect. */
885 mark_exp_read (exprv
);
888 if (!TREE_TYPE (expr
))
890 if (invalid_nonstatic_memfn_p (expr
, complain
))
891 return error_mark_node
;
892 if (TREE_CODE (expr
) == PSEUDO_DTOR_EXPR
)
894 if (complain
& tf_error
)
895 error ("pseudo-destructor is not called");
896 return error_mark_node
;
898 if (VOID_TYPE_P (TREE_TYPE (expr
)))
900 switch (TREE_CODE (expr
))
904 /* The two parts of a cond expr might be separate lvalues. */
905 tree op1
= TREE_OPERAND (expr
,1);
906 tree op2
= TREE_OPERAND (expr
,2);
907 bool side_effects
= ((op1
&& TREE_SIDE_EFFECTS (op1
))
908 || TREE_SIDE_EFFECTS (op2
));
909 tree new_op1
, new_op2
;
911 if (implicit
!= ICV_CAST
&& !side_effects
)
914 new_op1
= convert_to_void (op1
, ICV_SECOND_OF_COND
, complain
);
915 new_op2
= convert_to_void (op2
, ICV_THIRD_OF_COND
, complain
);
920 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
921 new_op2
= convert_to_void (op2
, ICV_CAST
, complain
);
924 expr
= build3 (COND_EXPR
, TREE_TYPE (new_op2
),
925 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
931 /* The second part of a compound expr contains the value. */
932 tree op1
= TREE_OPERAND (expr
,1);
934 if (implicit
!= ICV_CAST
&& !TREE_NO_WARNING (expr
))
935 new_op1
= convert_to_void (op1
, ICV_RIGHT_OF_COMMA
, complain
);
937 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
941 tree t
= build2 (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
942 TREE_OPERAND (expr
, 0), new_op1
);
949 case NON_LVALUE_EXPR
:
951 /* These have already decayed to rvalue. */
954 case CALL_EXPR
: /* We have a special meaning for volatile void fn(). */
959 tree type
= TREE_TYPE (expr
);
960 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
962 int is_volatile
= TYPE_VOLATILE (type
);
963 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
965 /* Can't load the value if we don't know the type. */
966 if (is_volatile
&& !is_complete
)
968 if (complain
& tf_warning
)
972 warning (0, "conversion to void will not access "
973 "object of incomplete type %qT", type
);
975 case ICV_SECOND_OF_COND
:
976 warning (0, "indirection will not access object of "
977 "incomplete type %qT in second operand "
978 "of conditional expression", type
);
980 case ICV_THIRD_OF_COND
:
981 warning (0, "indirection will not access object of "
982 "incomplete type %qT in third operand "
983 "of conditional expression", type
);
985 case ICV_RIGHT_OF_COMMA
:
986 warning (0, "indirection will not access object of "
987 "incomplete type %qT in right operand of "
988 "comma operator", type
);
990 case ICV_LEFT_OF_COMMA
:
991 warning (0, "indirection will not access object of "
992 "incomplete type %qT in left operand of "
993 "comma operator", type
);
996 warning (0, "indirection will not access object of "
997 "incomplete type %qT in statement", type
);
999 case ICV_THIRD_IN_FOR
:
1000 warning (0, "indirection will not access object of "
1001 "incomplete type %qT in for increment "
1002 "expression", type
);
1008 /* Don't load the value if this is an implicit dereference, or if
1009 the type needs to be handled by ctors/dtors. */
1010 else if (is_volatile
&& is_reference
)
1012 if (complain
& tf_warning
)
1016 warning (0, "conversion to void will not access "
1017 "object of type %qT", type
);
1019 case ICV_SECOND_OF_COND
:
1020 warning (0, "implicit dereference will not access object "
1021 "of type %qT in second operand of "
1022 "conditional expression", type
);
1024 case ICV_THIRD_OF_COND
:
1025 warning (0, "implicit dereference will not access object "
1026 "of type %qT in third operand of "
1027 "conditional expression", type
);
1029 case ICV_RIGHT_OF_COMMA
:
1030 warning (0, "implicit dereference will not access object "
1031 "of type %qT in right operand of "
1032 "comma operator", type
);
1034 case ICV_LEFT_OF_COMMA
:
1035 warning (0, "implicit dereference will not access object "
1036 "of type %qT in left operand of comma operator",
1040 warning (0, "implicit dereference will not access object "
1041 "of type %qT in statement", type
);
1043 case ICV_THIRD_IN_FOR
:
1044 warning (0, "implicit dereference will not access object "
1045 "of type %qT in for increment expression",
1052 else if (is_volatile
&& TREE_ADDRESSABLE (type
))
1054 if (complain
& tf_warning
)
1058 warning (0, "conversion to void will not access "
1059 "object of non-trivially-copyable type %qT",
1062 case ICV_SECOND_OF_COND
:
1063 warning (0, "indirection will not access object of "
1064 "non-trivially-copyable type %qT in second "
1065 "operand of conditional expression", type
);
1067 case ICV_THIRD_OF_COND
:
1068 warning (0, "indirection will not access object of "
1069 "non-trivially-copyable type %qT in third "
1070 "operand of conditional expression", type
);
1072 case ICV_RIGHT_OF_COMMA
:
1073 warning (0, "indirection will not access object of "
1074 "non-trivially-copyable type %qT in right "
1075 "operand of comma operator", type
);
1077 case ICV_LEFT_OF_COMMA
:
1078 warning (0, "indirection will not access object of "
1079 "non-trivially-copyable type %qT in left "
1080 "operand of comma operator", type
);
1083 warning (0, "indirection will not access object of "
1084 "non-trivially-copyable type %qT in statement",
1087 case ICV_THIRD_IN_FOR
:
1088 warning (0, "indirection will not access object of "
1089 "non-trivially-copyable type %qT in for "
1090 "increment expression", type
);
1096 if (is_reference
|| !is_volatile
|| !is_complete
|| TREE_ADDRESSABLE (type
))
1098 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1099 operation is stripped off. Note that we don't warn about
1100 - an expression with TREE_NO_WARNING set. (For an example of
1101 such expressions, see build_over_call in call.c.)
1102 - automatic dereferencing of references, since the user cannot
1103 control it. (See also warn_if_unused_value() in stmt.c.) */
1104 if (warn_unused_value
1105 && implicit
!= ICV_CAST
1106 && (complain
& tf_warning
)
1107 && !TREE_NO_WARNING (expr
)
1109 warning (OPT_Wunused_value
, "value computed is not used");
1110 expr
= TREE_OPERAND (expr
, 0);
1118 /* External variables might be incomplete. */
1119 tree type
= TREE_TYPE (expr
);
1120 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
1122 if (TYPE_VOLATILE (type
) && !is_complete
&& (complain
& tf_warning
))
1126 warning (0, "conversion to void will not access "
1127 "object %qE of incomplete type %qT", expr
, type
);
1129 case ICV_SECOND_OF_COND
:
1130 warning (0, "variable %qE of incomplete type %qT will not "
1131 "be accessed in second operand of "
1132 "conditional expression", expr
, type
);
1134 case ICV_THIRD_OF_COND
:
1135 warning (0, "variable %qE of incomplete type %qT will not "
1136 "be accessed in third operand of "
1137 "conditional expression", expr
, type
);
1139 case ICV_RIGHT_OF_COMMA
:
1140 warning (0, "variable %qE of incomplete type %qT will not "
1141 "be accessed in right operand of comma operator",
1144 case ICV_LEFT_OF_COMMA
:
1145 warning (0, "variable %qE of incomplete type %qT will not "
1146 "be accessed in left operand of comma operator",
1150 warning (0, "variable %qE of incomplete type %qT will not "
1151 "be accessed in statement", expr
, type
);
1153 case ICV_THIRD_IN_FOR
:
1154 warning (0, "variable %qE of incomplete type %qT will not "
1155 "be accessed in for increment expression",
1166 /* Don't bother with the temporary object returned from a function if
1167 we don't use it and don't need to destroy it. We'll still
1168 allocate space for it in expand_call or declare_return_variable,
1169 but we don't need to track it through all the tree phases. */
1170 if (TARGET_EXPR_IMPLICIT_P (expr
)
1171 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr
)))
1173 tree init
= TARGET_EXPR_INITIAL (expr
);
1174 if (TREE_CODE (init
) == AGGR_INIT_EXPR
1175 && !AGGR_INIT_VIA_CTOR_P (init
))
1177 tree fn
= AGGR_INIT_EXPR_FN (init
);
1178 expr
= build_call_array_loc (input_location
,
1179 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
))),
1181 aggr_init_expr_nargs (init
),
1182 AGGR_INIT_EXPR_ARGP (init
));
1189 expr
= resolve_nondeduced_context (expr
);
1193 if (TREE_CODE (probe
) == ADDR_EXPR
)
1194 probe
= TREE_OPERAND (expr
, 0);
1195 if (type_unknown_p (probe
))
1197 /* [over.over] enumerates the places where we can take the address
1198 of an overloaded function, and this is not one of them. */
1199 if (complain
& tf_error
)
1203 error ("conversion to void "
1204 "cannot resolve address of overloaded function");
1206 case ICV_SECOND_OF_COND
:
1207 error ("second operand of conditional expression "
1208 "cannot resolve address of overloaded function");
1210 case ICV_THIRD_OF_COND
:
1211 error ("third operand of conditional expression "
1212 "cannot resolve address of overloaded function");
1214 case ICV_RIGHT_OF_COMMA
:
1215 error ("right operand of comma operator "
1216 "cannot resolve address of overloaded function");
1218 case ICV_LEFT_OF_COMMA
:
1219 error ("left operand of comma operator "
1220 "cannot resolve address of overloaded function");
1224 "cannot resolve address of overloaded function");
1226 case ICV_THIRD_IN_FOR
:
1227 error ("for increment expression "
1228 "cannot resolve address of overloaded function");
1232 return error_mark_node
;
1233 expr
= void_zero_node
;
1235 else if (implicit
!= ICV_CAST
&& probe
== expr
&& is_overloaded_fn (probe
))
1237 /* Only warn when there is no &. */
1238 if (complain
& tf_warning
)
1241 case ICV_SECOND_OF_COND
:
1242 warning (OPT_Waddress
,
1243 "second operand of conditional expression "
1244 "is a reference, not call, to function %qE", expr
);
1246 case ICV_THIRD_OF_COND
:
1247 warning (OPT_Waddress
,
1248 "third operand of conditional expression "
1249 "is a reference, not call, to function %qE", expr
);
1251 case ICV_RIGHT_OF_COMMA
:
1252 warning (OPT_Waddress
,
1253 "right operand of comma operator "
1254 "is a reference, not call, to function %qE", expr
);
1256 case ICV_LEFT_OF_COMMA
:
1257 warning (OPT_Waddress
,
1258 "left operand of comma operator "
1259 "is a reference, not call, to function %qE", expr
);
1262 warning (OPT_Waddress
,
1263 "statement is a reference, not call, to function %qE",
1266 case ICV_THIRD_IN_FOR
:
1267 warning (OPT_Waddress
,
1268 "for increment expression "
1269 "is a reference, not call, to function %qE", expr
);
1275 if (TREE_CODE (expr
) == COMPONENT_REF
)
1276 expr
= TREE_OPERAND (expr
, 0);
1280 if (expr
!= error_mark_node
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
1282 if (implicit
!= ICV_CAST
1283 && warn_unused_value
1284 && !TREE_NO_WARNING (expr
)
1285 && !processing_template_decl
)
1287 /* The middle end does not warn about expressions that have
1288 been explicitly cast to void, so we must do so here. */
1289 if (!TREE_SIDE_EFFECTS (expr
)) {
1290 if (complain
& tf_warning
)
1293 case ICV_SECOND_OF_COND
:
1294 warning (OPT_Wunused_value
,
1295 "second operand of conditional expression has no effect");
1297 case ICV_THIRD_OF_COND
:
1298 warning (OPT_Wunused_value
,
1299 "third operand of conditional expression has no effect");
1301 case ICV_RIGHT_OF_COMMA
:
1302 warning (OPT_Wunused_value
,
1303 "right operand of comma operator has no effect");
1305 case ICV_LEFT_OF_COMMA
:
1306 warning (OPT_Wunused_value
,
1307 "left operand of comma operator has no effect");
1310 warning (OPT_Wunused_value
,
1311 "statement has no effect");
1313 case ICV_THIRD_IN_FOR
:
1314 warning (OPT_Wunused_value
,
1315 "for increment expression has no effect");
1324 enum tree_code code
;
1325 enum tree_code_class tclass
;
1328 /* We might like to warn about (say) "(int) f()", as the
1329 cast has no effect, but the compiler itself will
1330 generate implicit conversions under some
1331 circumstances. (For example a block copy will be
1332 turned into a call to "__builtin_memcpy", with a
1333 conversion of the return value to an appropriate
1334 type.) So, to avoid false positives, we strip
1335 conversions. Do not use STRIP_NOPs because it will
1336 not strip conversions to "void", as that is not a
1337 mode-preserving conversion. */
1338 while (TREE_CODE (e
) == NOP_EXPR
)
1339 e
= TREE_OPERAND (e
, 0);
1341 code
= TREE_CODE (e
);
1342 tclass
= TREE_CODE_CLASS (code
);
1343 if ((tclass
== tcc_comparison
1344 || tclass
== tcc_unary
1345 || (tclass
== tcc_binary
1346 && !(code
== MODIFY_EXPR
1347 || code
== INIT_EXPR
1348 || code
== PREDECREMENT_EXPR
1349 || code
== PREINCREMENT_EXPR
1350 || code
== POSTDECREMENT_EXPR
1351 || code
== POSTINCREMENT_EXPR
)))
1352 && (complain
& tf_warning
))
1353 warning (OPT_Wunused_value
, "value computed is not used");
1356 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
1358 if (! TREE_SIDE_EFFECTS (expr
))
1359 expr
= void_zero_node
;
1363 /* Create an expression whose value is that of EXPR,
1364 converted to type TYPE. The TREE_TYPE of the value
1365 is always TYPE. This function implements all reasonable
1366 conversions; callers should filter out those that are
1367 not permitted by the language being compiled.
1369 Most of this routine is from build_reinterpret_cast.
1371 The back end cannot call cp_convert (what was convert) because
1372 conversions to/from basetypes may involve memory references
1373 (vbases) and adding or subtracting small values (multiple
1374 inheritance), but it calls convert from the constant folding code
1375 on subtrees of already built trees after it has ripped them apart.
1377 Also, if we ever support range variables, we'll probably also have to
1378 do a little bit more work. */
1381 convert (tree type
, tree expr
)
1385 if (type
== error_mark_node
|| expr
== error_mark_node
)
1386 return error_mark_node
;
1388 intype
= TREE_TYPE (expr
);
1390 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
1391 return fold_if_not_in_template (build_nop (type
, expr
));
1393 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
1394 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
1397 /* Like cp_convert, except permit conversions to take place which
1398 are not normally allowed due to access restrictions
1399 (such as conversion from sub-type to private super-type). */
1402 convert_force (tree type
, tree expr
, int convtype
)
1405 enum tree_code code
= TREE_CODE (type
);
1407 if (code
== REFERENCE_TYPE
)
1408 return (fold_if_not_in_template
1409 (convert_to_reference (type
, e
, CONV_C_CAST
, LOOKUP_COMPLAIN
,
1412 if (code
== POINTER_TYPE
)
1413 return fold_if_not_in_template (convert_to_pointer_force (type
, e
));
1415 /* From typeck.c convert_for_assignment */
1416 if (((TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
&& TREE_CODE (e
) == ADDR_EXPR
1417 && TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
1418 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1419 || integer_zerop (e
)
1420 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1421 && TYPE_PTRMEMFUNC_P (type
))
1422 /* compatible pointer to member functions. */
1423 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1,
1424 /*c_cast_p=*/1, tf_warning_or_error
);
1426 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
);
1429 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1430 exists, return the attempted conversion. This may
1431 return ERROR_MARK_NODE if the conversion is not
1432 allowed (references private members, etc).
1433 If no conversion exists, NULL_TREE is returned.
1435 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1436 object parameter, or by the second standard conversion sequence if
1437 that doesn't do it. This will probably wait for an overloading rewrite.
1441 build_type_conversion (tree xtype
, tree expr
)
1443 /* C++: check to see if we can convert this aggregate type
1444 into the required type. */
1445 return build_user_type_conversion (xtype
, expr
, LOOKUP_NORMAL
);
1448 /* Convert the given EXPR to one of a group of types suitable for use in an
1449 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1450 which indicates which types are suitable. If COMPLAIN is true, complain
1451 about ambiguity; otherwise, the caller will deal with it. */
1454 build_expr_type_conversion (int desires
, tree expr
, bool complain
)
1456 tree basetype
= TREE_TYPE (expr
);
1457 tree conv
= NULL_TREE
;
1458 tree winner
= NULL_TREE
;
1460 if (expr
== null_node
1461 && (desires
& WANT_INT
)
1462 && !(desires
& WANT_NULL
))
1463 warning_at (input_location
, OPT_Wconversion_null
,
1464 "converting NULL to non-pointer type");
1466 basetype
= TREE_TYPE (expr
);
1468 if (basetype
== error_mark_node
)
1469 return error_mark_node
;
1471 if (! MAYBE_CLASS_TYPE_P (basetype
))
1472 switch (TREE_CODE (basetype
))
1475 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1477 /* else fall through... */
1480 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1482 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1484 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1486 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1490 return (desires
& WANT_POINTER
) ? decay_conversion (expr
)
1495 if ((desires
& WANT_VECTOR_OR_COMPLEX
) == 0)
1497 switch (TREE_CODE (TREE_TYPE (basetype
)))
1501 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1503 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1505 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1514 /* The code for conversions from class type is currently only used for
1515 delete expressions. Other expressions are handled by build_new_op. */
1516 if (!complete_type_or_maybe_complain (basetype
, expr
, complain
))
1517 return error_mark_node
;
1518 if (!TYPE_HAS_CONVERSION (basetype
))
1521 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1525 tree cand
= TREE_VALUE (conv
);
1526 cand
= OVL_CURRENT (cand
);
1528 if (winner
&& winner
== cand
)
1531 if (DECL_NONCONVERTING_P (cand
))
1534 candidate
= non_reference (TREE_TYPE (TREE_TYPE (cand
)));
1536 switch (TREE_CODE (candidate
))
1540 win
= (desires
& WANT_INT
); break;
1542 win
= (desires
& WANT_ENUM
); break;
1544 win
= (desires
& WANT_FLOAT
); break;
1546 win
= (desires
& WANT_POINTER
); break;
1550 if ((desires
& WANT_VECTOR_OR_COMPLEX
) == 0)
1552 switch (TREE_CODE (TREE_TYPE (candidate
)))
1556 win
= (desires
& WANT_INT
); break;
1558 win
= (desires
& WANT_ENUM
); break;
1560 win
= (desires
& WANT_FLOAT
); break;
1576 error ("ambiguous default type conversion from %qT",
1578 error (" candidate conversions include %qD and %qD",
1581 return error_mark_node
;
1590 tree type
= non_reference (TREE_TYPE (TREE_TYPE (winner
)));
1591 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
);
1597 /* Implements integral promotion (4.1) and float->double promotion. */
1600 type_promotes_to (tree type
)
1604 if (type
== error_mark_node
)
1605 return error_mark_node
;
1607 type
= TYPE_MAIN_VARIANT (type
);
1609 /* Check for promotions of target-defined types first. */
1610 promoted_type
= targetm
.promoted_type (type
);
1612 return promoted_type
;
1614 /* bool always promotes to int (not unsigned), even if it's the same
1616 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
1617 type
= integer_type_node
;
1619 /* scoped enums don't promote. */
1620 else if (SCOPED_ENUM_P (type
) && abi_version_at_least (6))
1623 /* Normally convert enums to int, but convert wide enums to something
1625 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1626 || type
== char16_type_node
1627 || type
== char32_type_node
1628 || type
== wchar_type_node
)
1630 int precision
= MAX (TYPE_PRECISION (type
),
1631 TYPE_PRECISION (integer_type_node
));
1632 tree totype
= c_common_type_for_size (precision
, 0);
1633 if (SCOPED_ENUM_P (type
))
1634 warning (OPT_Wabi
, "scoped enum %qT will not promote to an integral "
1635 "type in a future version of GCC", type
);
1636 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1637 type
= ENUM_UNDERLYING_TYPE (type
);
1638 if (TYPE_UNSIGNED (type
)
1639 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1640 type
= c_common_type_for_size (precision
, 1);
1644 else if (c_promoting_integer_type_p (type
))
1646 /* Retain unsignedness if really not getting bigger. */
1647 if (TYPE_UNSIGNED (type
)
1648 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1649 type
= unsigned_type_node
;
1651 type
= integer_type_node
;
1653 else if (type
== float_type_node
)
1654 type
= double_type_node
;
1659 /* The routines below this point are carefully written to conform to
1660 the standard. They use the same terminology, and follow the rules
1661 closely. Although they are used only in pt.c at the moment, they
1662 should presumably be used everywhere in the future. */
1664 /* Attempt to perform qualification conversions on EXPR to convert it
1665 to TYPE. Return the resulting expression, or error_mark_node if
1666 the conversion was impossible. */
1669 perform_qualification_conversions (tree type
, tree expr
)
1673 expr_type
= TREE_TYPE (expr
);
1675 if (same_type_p (type
, expr_type
))
1677 else if (TYPE_PTR_P (type
) && TYPE_PTR_P (expr_type
)
1678 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (expr_type
)))
1679 return build_nop (type
, expr
);
1680 else if (TYPE_PTR_TO_MEMBER_P (type
)
1681 && TYPE_PTR_TO_MEMBER_P (expr_type
)
1682 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
1683 TYPE_PTRMEM_CLASS_TYPE (expr_type
))
1684 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type
),
1685 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type
)))
1686 return build_nop (type
, expr
);
1688 return error_mark_node
;