1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains the functions for converting C++ expressions
23 to different data types. The only entry point is `convert'.
24 Every language front end must have a `convert' function
25 but what kind of conversions it does will depend on the language. */
29 #include "coretypes.h"
34 #include "double-int.h"
41 #include "stor-layout.h"
49 static tree cp_convert_to_pointer (tree, tree, tsubst_flags_t);
50 static tree convert_to_pointer_force (tree, tree, tsubst_flags_t);
51 static tree build_type_conversion (tree, tree);
52 static tree build_up_reference (tree, tree, int, tree, tsubst_flags_t);
53 static void diagnose_ref_binding (location_t, tree, tree, tree);
55 /* Change of width--truncation and extension of integers or reals--
56 is represented with NOP_EXPR. Proper functioning of many things
57 assumes that no other conversions can be NOP_EXPRs.
59 Conversion between integer and pointer is represented with CONVERT_EXPR.
60 Converting integer to real uses FLOAT_EXPR
61 and real to integer uses FIX_TRUNC_EXPR.
63 Here is a list of all the functions that assume that widening and
64 narrowing is always done with a NOP_EXPR:
65 In convert.c, convert_to_integer.
66 In c-typeck.c, build_binary_op_nodefault (boolean ops),
67 and c_common_truthvalue_conversion.
68 In expr.c: expand_expr, for operands of a MULT_EXPR.
69 In fold-const.c: fold.
70 In tree.c: get_narrower and get_unwidened.
72 C++: in multiple-inheritance, converting between pointers may involve
73 adjusting them by a delta stored within the class definition. */
75 /* Subroutines of `convert'. */
77 /* if converting pointer to pointer
78 if dealing with classes, check for derived->base or vice versa
79 else if dealing with method pointers, delegate
81 else if converting class, pass off to build_type_conversion
82 else try C-style pointer conversion. */
85 cp_convert_to_pointer (tree type, tree expr, tsubst_flags_t complain)
87 tree intype = TREE_TYPE (expr);
90 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
92 if (intype == error_mark_node)
93 return error_mark_node;
95 if (MAYBE_CLASS_TYPE_P (intype))
97 intype = complete_type (intype);
98 if (!COMPLETE_TYPE_P (intype))
100 if (complain & tf_error)
101 error_at (loc, "can%'t convert from incomplete type %qT to %qT",
103 return error_mark_node;
106 rval = build_type_conversion (type, expr);
109 if ((complain & tf_error)
110 && rval == error_mark_node)
111 error_at (loc, "conversion of %qE from %qT to %qT is ambiguous",
117 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
118 if (TYPE_PTR_P (type)
119 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
120 || VOID_TYPE_P (TREE_TYPE (type))))
122 if (TYPE_PTRMEMFUNC_P (intype)
123 || TREE_CODE (intype) == METHOD_TYPE)
124 return convert_member_func_to_ptr (type, expr, complain);
125 if (TYPE_PTR_P (TREE_TYPE (expr)))
126 return build_nop (type, expr);
127 intype = TREE_TYPE (expr);
130 if (expr == error_mark_node)
131 return error_mark_node;
133 form = TREE_CODE (intype);
135 if (POINTER_TYPE_P (intype))
137 intype = TYPE_MAIN_VARIANT (intype);
139 if (TYPE_MAIN_VARIANT (type) != intype
141 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
142 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
143 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
144 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
146 enum tree_code code = PLUS_EXPR;
152 intype_class = TREE_TYPE (intype);
153 type_class = TREE_TYPE (type);
155 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
156 TYPE_MAIN_VARIANT (type_class));
158 /* Try derived to base conversion. */
160 binfo = lookup_base (intype_class, type_class, ba_check,
162 if (!same_p && !binfo)
164 /* Try base to derived conversion. */
165 binfo = lookup_base (type_class, intype_class, ba_check,
169 if (binfo == error_mark_node)
170 return error_mark_node;
174 expr = build_base_path (code, expr, binfo, 0, complain);
175 /* Add any qualifier conversions. */
176 return build_nop (type, expr);
180 if (TYPE_PTRMEMFUNC_P (type))
182 if (complain & tf_error)
183 error_at (loc, "cannot convert %qE from type %qT to type %qT",
185 return error_mark_node;
188 return build_nop (type, expr);
190 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
191 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
192 return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
193 /*c_cast_p=*/false, complain);
194 else if (TYPE_PTRMEMFUNC_P (intype))
198 if (TREE_CODE (expr) == PTRMEM_CST)
199 return cp_convert_to_pointer (type, PTRMEM_CST_MEMBER (expr),
201 else if (TREE_CODE (expr) == OFFSET_REF)
203 tree object = TREE_OPERAND (expr, 0);
204 return get_member_function_from_ptrfunc (&object,
205 TREE_OPERAND (expr, 1),
209 if (complain & tf_error)
210 error_at (loc, "cannot convert %qE from type %qT to type %qT",
212 return error_mark_node;
215 if (null_ptr_cst_p (expr))
217 if (TYPE_PTRMEMFUNC_P (type))
218 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
219 /*c_cast_p=*/false, complain);
221 if (complain & tf_warning)
222 maybe_warn_zero_as_null_pointer_constant (expr, loc);
224 /* A NULL pointer-to-data-member is represented by -1, not by
226 tree val = (TYPE_PTRDATAMEM_P (type)
227 ? build_int_cst_type (type, -1)
228 : build_int_cst (type, 0));
230 return (TREE_SIDE_EFFECTS (expr)
231 ? build2 (COMPOUND_EXPR, type, expr, val) : val);
233 else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form))
235 if (complain & tf_error)
236 error_at (loc, "invalid conversion from %qT to %qT", intype, type);
237 return error_mark_node;
240 if (INTEGRAL_CODE_P (form))
242 if (TYPE_PRECISION (intype) == POINTER_SIZE)
243 return build1 (CONVERT_EXPR, type, expr);
244 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
246 /* Modes may be different but sizes should be the same. There
247 is supposed to be some integral type that is the same width
249 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
250 == GET_MODE_SIZE (TYPE_MODE (type)));
252 return convert_to_pointer (type, expr);
255 if (type_unknown_p (expr))
256 return instantiate_type (type, expr, complain);
258 if (complain & tf_error)
259 error_at (loc, "cannot convert %qE from type %qT to type %qT",
261 return error_mark_node;
264 /* Like convert, except permit conversions to take place which
265 are not normally allowed due to access restrictions
266 (such as conversion from sub-type to private super-type). */
269 convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain)
271 tree intype = TREE_TYPE (expr);
272 enum tree_code form = TREE_CODE (intype);
274 if (form == POINTER_TYPE)
276 intype = TYPE_MAIN_VARIANT (intype);
278 if (TYPE_MAIN_VARIANT (type) != intype
279 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
280 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
281 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
282 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
284 enum tree_code code = PLUS_EXPR;
287 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
288 ba_unique, NULL, complain);
291 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
292 ba_unique, NULL, complain);
295 if (binfo == error_mark_node)
296 return error_mark_node;
299 expr = build_base_path (code, expr, binfo, 0, complain);
300 if (expr == error_mark_node)
301 return error_mark_node;
302 /* Add any qualifier conversions. */
303 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
305 expr = build_nop (type, expr);
311 return cp_convert_to_pointer (type, expr, complain);
314 /* We are passing something to a function which requires a reference.
315 The type we are interested in is in TYPE. The initial
316 value we have to begin with is in ARG.
318 FLAGS controls how we manage access checking.
319 DIRECT_BIND in FLAGS controls how any temporaries are generated.
320 If DIRECT_BIND is set, DECL is the reference we're binding to. */
323 build_up_reference (tree type, tree arg, int flags, tree decl,
324 tsubst_flags_t complain)
327 tree argtype = TREE_TYPE (arg);
328 tree target_type = TREE_TYPE (type);
330 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
332 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
334 /* Create a new temporary variable. We can't just use a TARGET_EXPR
335 here because it needs to live as long as DECL. */
338 arg = make_temporary_var_for_ref_to_temp (decl, target_type);
340 /* Process the initializer for the declaration. */
341 DECL_INITIAL (arg) = targ;
342 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
343 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
345 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
346 return get_target_expr_sfinae (arg, complain);
348 /* If we had a way to wrap this up, and say, if we ever needed its
349 address, transform all occurrences of the register, into a memory
350 reference we could win better. */
351 rval = cp_build_addr_expr (arg, complain);
352 if (rval == error_mark_node)
353 return error_mark_node;
355 if ((flags & LOOKUP_PROTECT)
356 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
357 && MAYBE_CLASS_TYPE_P (argtype)
358 && MAYBE_CLASS_TYPE_P (target_type))
360 /* We go through lookup_base for the access control. */
361 tree binfo = lookup_base (argtype, target_type, ba_check,
363 if (binfo == error_mark_node)
364 return error_mark_node;
365 if (binfo == NULL_TREE)
366 return error_not_base_type (target_type, argtype);
367 rval = build_base_path (PLUS_EXPR, rval, binfo, 1, complain);
371 = convert_to_pointer_force (build_pointer_type (target_type),
373 return build_nop (type, rval);
376 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
377 INTYPE is the original rvalue type and DECL is an optional _DECL node
380 [dcl.init.ref] says that if an rvalue is used to
381 initialize a reference, then the reference must be to a
382 non-volatile const type. */
385 diagnose_ref_binding (location_t loc, tree reftype, tree intype, tree decl)
387 tree ttl = TREE_TYPE (reftype);
389 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
393 if (CP_TYPE_VOLATILE_P (ttl) && decl)
394 msg = G_("initialization of volatile reference type %q#T from "
395 "rvalue of type %qT");
396 else if (CP_TYPE_VOLATILE_P (ttl))
397 msg = G_("conversion to volatile reference type %q#T "
398 "from rvalue of type %qT");
400 msg = G_("initialization of non-const reference type %q#T from "
401 "rvalue of type %qT");
403 msg = G_("conversion to non-const reference type %q#T from "
404 "rvalue of type %qT");
406 permerror (loc, msg, reftype, intype);
410 /* For C++: Only need to do one-level references, but cannot
411 get tripped up on signed/unsigned differences.
413 DECL is either NULL_TREE or the _DECL node for a reference that is being
414 initialized. It can be error_mark_node if we don't know the _DECL but
415 we know it's an initialization. */
418 convert_to_reference (tree reftype, tree expr, int convtype,
419 int flags, tree decl, tsubst_flags_t complain)
421 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
423 tree rval = NULL_TREE;
424 tree rval_as_conversion = NULL_TREE;
425 bool can_convert_intype_to_type;
426 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
428 if (TREE_CODE (type) == FUNCTION_TYPE
429 && TREE_TYPE (expr) == unknown_type_node)
430 expr = instantiate_type (type, expr, complain);
432 if (expr == error_mark_node)
433 return error_mark_node;
435 intype = TREE_TYPE (expr);
437 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
438 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
440 intype = TYPE_MAIN_VARIANT (intype);
442 can_convert_intype_to_type = can_convert_standard (type, intype, complain);
444 if (!can_convert_intype_to_type
445 && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
446 && ! (flags & LOOKUP_NO_CONVERSION))
448 /* Look for a user-defined conversion to lvalue that we can use. */
451 = build_type_conversion (reftype, expr);
453 if (rval_as_conversion && rval_as_conversion != error_mark_node
454 && real_lvalue_p (rval_as_conversion))
456 expr = rval_as_conversion;
457 rval_as_conversion = NULL_TREE;
459 can_convert_intype_to_type = 1;
463 if (((convtype & CONV_STATIC)
464 && can_convert_standard (intype, type, complain))
465 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
468 tree ttl = TREE_TYPE (reftype);
469 tree ttr = lvalue_type (expr);
471 if ((complain & tf_error)
472 && ! real_lvalue_p (expr))
473 diagnose_ref_binding (loc, reftype, intype, decl);
475 if (! (convtype & CONV_CONST)
476 && !at_least_as_qualified_p (ttl, ttr))
478 if (complain & tf_error)
479 permerror (loc, "conversion from %qT to %qT discards qualifiers",
482 return error_mark_node;
486 return build_up_reference (reftype, expr, flags, decl, complain);
488 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
490 /* When casting an lvalue to a reference type, just convert into
491 a pointer to the new type and deference it. This is allowed
492 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
493 should be done directly (jason). (int &)ri ---> *(int*)&ri */
495 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
497 if ((complain & tf_warning)
498 && TYPE_PTR_P (intype)
499 && (comptypes (TREE_TYPE (intype), type,
500 COMPARE_BASE | COMPARE_DERIVED)))
501 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
504 rval = cp_build_addr_expr (expr, complain);
505 if (rval != error_mark_node)
506 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
508 if (rval != error_mark_node)
509 rval = build1 (NOP_EXPR, reftype, rval);
513 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
514 ICR_CONVERTING, 0, 0, complain);
515 if (rval == NULL_TREE || rval == error_mark_node)
517 if (complain & tf_error)
518 diagnose_ref_binding (loc, reftype, intype, decl);
519 rval = build_up_reference (reftype, rval, flags, decl, complain);
524 /* If we found a way to convert earlier, then use it. */
528 if (complain & tf_error)
529 error_at (loc, "cannot convert type %qT to type %qT", intype, reftype);
531 return error_mark_node;
534 /* We are using a reference VAL for its value. Bash that reference all the
535 way down to its lowest form. */
538 convert_from_reference (tree val)
541 && TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
543 tree t = TREE_TYPE (TREE_TYPE (val));
544 tree ref = build1 (INDIRECT_REF, t, val);
547 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
548 so that we get the proper error message if the result is used
549 to assign to. Also, &* is supposed to be a no-op. */
550 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
551 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
552 TREE_SIDE_EFFECTS (ref)
553 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
560 /* Really perform an lvalue-to-rvalue conversion, including copying an
561 argument of class type into a temporary. */
564 force_rvalue (tree expr, tsubst_flags_t complain)
566 tree type = TREE_TYPE (expr);
567 if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR)
569 vec<tree, va_gc> *args = make_tree_vector_single (expr);
570 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
571 &args, type, LOOKUP_NORMAL, complain);
572 release_tree_vector (args);
573 expr = build_cplus_new (type, expr, complain);
576 expr = decay_conversion (expr, complain);
582 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
583 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
587 ignore_overflows (tree expr, tree orig)
589 if (TREE_CODE (expr) == INTEGER_CST
590 && TREE_CODE (orig) == INTEGER_CST
591 && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig))
593 gcc_assert (!TREE_OVERFLOW (orig));
594 /* Ensure constant sharing. */
595 expr = wide_int_to_tree (TREE_TYPE (expr), expr);
600 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
604 cp_fold_convert (tree type, tree expr)
606 tree conv = fold_convert (type, expr);
607 conv = ignore_overflows (conv, expr);
611 /* C++ conversions, preference to static cast conversions. */
614 cp_convert (tree type, tree expr, tsubst_flags_t complain)
616 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain);
619 /* C++ equivalent of convert_and_check but using cp_convert as the
622 Convert EXPR to TYPE, warning about conversion problems with constants.
623 Invoke this function on every expression that is converted implicitly,
624 i.e. because of language rules and not because of an explicit cast. */
627 cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
631 if (TREE_TYPE (expr) == type)
634 result = cp_convert (type, expr, complain);
636 if ((complain & tf_warning)
637 && c_inhibit_evaluation_warnings == 0)
639 tree folded = maybe_constant_value (expr);
640 tree stripped = folded;
642 = folded != expr ? cp_convert (type, folded, complain) : result;
644 /* maybe_constant_value wraps an INTEGER_CST with TREE_OVERFLOW in a
645 NOP_EXPR so that it isn't TREE_CONSTANT anymore. */
646 STRIP_NOPS (stripped);
648 if (!TREE_OVERFLOW_P (stripped)
649 && folded_result != error_mark_node)
650 warnings_for_convert_and_check (input_location, type, folded,
659 FLAGS indicates how we should behave. */
662 ocp_convert (tree type, tree expr, int convtype, int flags,
663 tsubst_flags_t complain)
666 enum tree_code code = TREE_CODE (type);
667 const char *invalid_conv_diag;
669 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
671 if (error_operand_p (e) || type == error_mark_node)
672 return error_mark_node;
674 complete_type (type);
675 complete_type (TREE_TYPE (expr));
677 if ((invalid_conv_diag
678 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
680 if (complain & tf_error)
681 error (invalid_conv_diag);
682 return error_mark_node;
685 /* FIXME remove when moving to c_fully_fold model. */
686 e = scalar_constant_value (e);
687 if (error_operand_p (e))
688 return error_mark_node;
690 if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
691 /* We need a new temporary; don't take this shortcut. */;
692 else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
694 if (same_type_p (type, TREE_TYPE (e)))
695 /* The call to fold will not always remove the NOP_EXPR as
696 might be expected, since if one of the types is a typedef;
697 the comparison in fold is just equality of pointers, not a
698 call to comptypes. We don't call fold in this case because
699 that can result in infinite recursion; fold will call
700 convert, which will call ocp_convert, etc. */
702 /* For complex data types, we need to perform componentwise
704 else if (TREE_CODE (type) == COMPLEX_TYPE)
705 return fold_if_not_in_template (convert_to_complex (type, e));
706 else if (TREE_CODE (type) == VECTOR_TYPE)
707 return fold_if_not_in_template (convert_to_vector (type, e));
708 else if (TREE_CODE (e) == TARGET_EXPR)
710 /* Don't build a NOP_EXPR of class type. Instead, change the
711 type of the temporary. */
712 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
717 /* We shouldn't be treating objects of ADDRESSABLE type as
719 gcc_assert (!TREE_ADDRESSABLE (type));
720 return fold_if_not_in_template (build_nop (type, e));
724 e1 = targetm.convert_to_type (type, e);
728 if (code == VOID_TYPE && (convtype & CONV_STATIC))
730 e = convert_to_void (e, ICV_CAST, complain);
734 if (INTEGRAL_CODE_P (code))
736 tree intype = TREE_TYPE (e);
739 if (TREE_CODE (type) == ENUMERAL_TYPE)
741 /* enum = enum, enum = int, enum = float, (enum)pointer are all
743 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
744 || TREE_CODE (intype) == REAL_TYPE)
745 && ! (convtype & CONV_STATIC))
746 || TYPE_PTR_P (intype))
748 if (complain & tf_error)
749 permerror (loc, "conversion from %q#T to %q#T", intype, type);
751 return error_mark_node;
754 /* [expr.static.cast]
756 8. A value of integral or enumeration type can be explicitly
757 converted to an enumeration type. The value is unchanged if
758 the original value is within the range of the enumeration
759 values. Otherwise, the resulting enumeration value is
761 if ((complain & tf_warning)
762 && TREE_CODE (e) == INTEGER_CST
763 && ENUM_UNDERLYING_TYPE (type)
764 && !int_fits_type_p (e, ENUM_UNDERLYING_TYPE (type)))
765 warning_at (loc, OPT_Wconversion,
766 "the result of the conversion is unspecified because "
767 "%qE is outside the range of type %qT",
770 if (MAYBE_CLASS_TYPE_P (intype))
773 rval = build_type_conversion (type, e);
776 if (complain & tf_error)
777 error_at (loc, "%q#T used where a %qT was expected", intype, type);
778 return error_mark_node;
780 if (code == BOOLEAN_TYPE)
782 if (VOID_TYPE_P (intype))
784 if (complain & tf_error)
786 "could not convert %qE from %<void%> to %<bool%>",
788 return error_mark_node;
791 /* We can't implicitly convert a scoped enum to bool, so convert
792 to the underlying type first. */
793 if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
794 e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
795 return cp_truthvalue_conversion (e);
798 converted = fold_if_not_in_template (convert_to_integer (type, e));
800 /* Ignore any integer overflow caused by the conversion. */
801 return ignore_overflows (converted, e);
803 if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e))
805 if (complain & tf_warning)
806 maybe_warn_zero_as_null_pointer_constant (e, loc);
809 if (POINTER_TYPE_P (type) || TYPE_PTRMEM_P (type))
810 return fold_if_not_in_template (cp_convert_to_pointer (type, e, complain));
811 if (code == VECTOR_TYPE)
813 tree in_vtype = TREE_TYPE (e);
814 if (MAYBE_CLASS_TYPE_P (in_vtype))
817 ret_val = build_type_conversion (type, e);
820 if (complain & tf_error)
821 error_at (loc, "%q#T used where a %qT was expected",
823 return error_mark_node;
825 return fold_if_not_in_template (convert_to_vector (type, e));
827 if (code == REAL_TYPE || code == COMPLEX_TYPE)
829 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
832 rval = build_type_conversion (type, e);
835 else if (complain & tf_error)
837 "%q#T used where a floating point value was expected",
840 if (code == REAL_TYPE)
841 return fold_if_not_in_template (convert_to_real (type, e));
842 else if (code == COMPLEX_TYPE)
843 return fold_if_not_in_template (convert_to_complex (type, e));
846 /* New C++ semantics: since assignment is now based on
847 memberwise copying, if the rhs type is derived from the
848 lhs type, then we may still do a conversion. */
849 if (RECORD_OR_UNION_CODE_P (code))
851 tree dtype = TREE_TYPE (e);
852 tree ctor = NULL_TREE;
854 dtype = TYPE_MAIN_VARIANT (dtype);
856 /* Conversion between aggregate types. New C++ semantics allow
857 objects of derived type to be cast to objects of base type.
858 Old semantics only allowed this between pointers.
860 There may be some ambiguity between using a constructor
861 vs. using a type conversion operator when both apply. */
865 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
866 return error_mark_node;
868 if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
869 ctor = perform_implicit_conversion (type, ctor, complain);
870 else if ((flags & LOOKUP_ONLYCONVERTING)
871 && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
872 /* For copy-initialization, first we create a temp of the proper type
873 with a user-defined conversion sequence, then we direct-initialize
874 the target with the temp (see [dcl.init]). */
875 ctor = build_user_type_conversion (type, ctor, flags, complain);
878 vec<tree, va_gc> *ctor_vec = make_tree_vector_single (ctor);
879 ctor = build_special_member_call (NULL_TREE,
880 complete_ctor_identifier,
882 type, flags, complain);
883 release_tree_vector (ctor_vec);
886 return build_cplus_new (type, ctor, complain);
889 if (complain & tf_error)
891 /* If the conversion failed and expr was an invalid use of pointer to
892 member function, try to report a meaningful error. */
893 if (invalid_nonstatic_memfn_p (expr, complain))
894 /* We displayed the error message. */;
896 error_at (loc, "conversion from %qT to non-scalar type %qT requested",
897 TREE_TYPE (expr), type);
899 return error_mark_node;
902 /* When an expression is used in a void context, its value is discarded and
903 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
904 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
905 in a void context. The C++ standard does not define what an `access' to an
906 object is, but there is reason to believe that it is the lvalue to rvalue
907 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
908 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
909 indicates that volatile semantics should be the same between C and C++
910 where ever possible. C leaves it implementation defined as to what
911 constitutes an access to a volatile. So, we interpret `*vp' as a read of
912 the volatile object `vp' points to, unless that is an incomplete type. For
913 volatile references we do not do this interpretation, because that would
914 make it impossible to ignore the reference return value from functions. We
915 issue warnings in the confusing cases.
917 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
918 to void via a cast. If an expression is being implicitly converted, IMPLICIT
919 indicates the context of the implicit conversion. */
922 convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
924 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
926 if (expr == error_mark_node
927 || TREE_TYPE (expr) == error_mark_node)
928 return error_mark_node;
930 if (implicit == ICV_CAST)
931 mark_exp_read (expr);
936 while (TREE_CODE (exprv) == COMPOUND_EXPR)
937 exprv = TREE_OPERAND (exprv, 1);
939 || handled_component_p (exprv)
940 || INDIRECT_REF_P (exprv))
941 /* Expr is not being 'used' here, otherwise we whould have
942 called mark_{rl}value_use use here, which would have in turn
943 called mark_exp_read. Rather, we call mark_exp_read directly
944 to avoid some warnings when
945 -Wunused-but-set-{variable,parameter} is in effect. */
946 mark_exp_read (exprv);
949 if (!TREE_TYPE (expr))
951 if (invalid_nonstatic_memfn_p (expr, complain))
952 return error_mark_node;
953 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
955 if (complain & tf_error)
956 error_at (loc, "pseudo-destructor is not called");
957 return error_mark_node;
959 if (VOID_TYPE_P (TREE_TYPE (expr)))
961 switch (TREE_CODE (expr))
965 /* The two parts of a cond expr might be separate lvalues. */
966 tree op1 = TREE_OPERAND (expr,1);
967 tree op2 = TREE_OPERAND (expr,2);
968 bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1))
969 || TREE_SIDE_EFFECTS (op2));
970 tree new_op1, new_op2;
972 if (implicit != ICV_CAST && !side_effects)
975 new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
976 new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
981 new_op1 = convert_to_void (op1, ICV_CAST, complain);
982 new_op2 = convert_to_void (op2, ICV_CAST, complain);
985 expr = build3 (COND_EXPR, TREE_TYPE (new_op2),
986 TREE_OPERAND (expr, 0), new_op1, new_op2);
992 /* The second part of a compound expr contains the value. */
993 tree op1 = TREE_OPERAND (expr,1);
995 if (implicit != ICV_CAST && !TREE_NO_WARNING (expr))
996 new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
998 new_op1 = convert_to_void (op1, ICV_CAST, complain);
1002 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
1003 TREE_OPERAND (expr, 0), new_op1);
1010 case NON_LVALUE_EXPR:
1012 /* These have already decayed to rvalue. */
1015 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
1020 tree type = TREE_TYPE (expr);
1021 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
1023 int is_volatile = TYPE_VOLATILE (type);
1024 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1026 /* Can't load the value if we don't know the type. */
1027 if (is_volatile && !is_complete)
1029 if (complain & tf_warning)
1033 warning_at (loc, 0, "conversion to void will not access "
1034 "object of incomplete type %qT", type);
1036 case ICV_SECOND_OF_COND:
1037 warning_at (loc, 0, "indirection will not access object of "
1038 "incomplete type %qT in second operand "
1039 "of conditional expression", type);
1041 case ICV_THIRD_OF_COND:
1042 warning_at (loc, 0, "indirection will not access object of "
1043 "incomplete type %qT in third operand "
1044 "of conditional expression", type);
1046 case ICV_RIGHT_OF_COMMA:
1047 warning_at (loc, 0, "indirection will not access object of "
1048 "incomplete type %qT in right operand of "
1049 "comma operator", type);
1051 case ICV_LEFT_OF_COMMA:
1052 warning_at (loc, 0, "indirection will not access object of "
1053 "incomplete type %qT in left operand of "
1054 "comma operator", type);
1057 warning_at (loc, 0, "indirection will not access object of "
1058 "incomplete type %qT in statement", type);
1060 case ICV_THIRD_IN_FOR:
1061 warning_at (loc, 0, "indirection will not access object of "
1062 "incomplete type %qT in for increment "
1063 "expression", type);
1069 /* Don't load the value if this is an implicit dereference, or if
1070 the type needs to be handled by ctors/dtors. */
1071 else if (is_volatile && is_reference)
1073 if (complain & tf_warning)
1077 warning_at (loc, 0, "conversion to void will not access "
1078 "object of type %qT", type);
1080 case ICV_SECOND_OF_COND:
1081 warning_at (loc, 0, "implicit dereference will not access "
1082 "object of type %qT in second operand of "
1083 "conditional expression", type);
1085 case ICV_THIRD_OF_COND:
1086 warning_at (loc, 0, "implicit dereference will not access "
1087 "object of type %qT in third operand of "
1088 "conditional expression", type);
1090 case ICV_RIGHT_OF_COMMA:
1091 warning_at (loc, 0, "implicit dereference will not access "
1092 "object of type %qT in right operand of "
1093 "comma operator", type);
1095 case ICV_LEFT_OF_COMMA:
1096 warning_at (loc, 0, "implicit dereference will not access "
1097 "object of type %qT in left operand of comma "
1101 warning_at (loc, 0, "implicit dereference will not access "
1102 "object of type %qT in statement", type);
1104 case ICV_THIRD_IN_FOR:
1105 warning_at (loc, 0, "implicit dereference will not access "
1106 "object of type %qT in for increment expression",
1113 else if (is_volatile && TREE_ADDRESSABLE (type))
1115 if (complain & tf_warning)
1119 warning_at (loc, 0, "conversion to void will not access "
1120 "object of non-trivially-copyable type %qT",
1123 case ICV_SECOND_OF_COND:
1124 warning_at (loc, 0, "indirection will not access object of "
1125 "non-trivially-copyable type %qT in second "
1126 "operand of conditional expression", type);
1128 case ICV_THIRD_OF_COND:
1129 warning_at (loc, 0, "indirection will not access object of "
1130 "non-trivially-copyable type %qT in third "
1131 "operand of conditional expression", type);
1133 case ICV_RIGHT_OF_COMMA:
1134 warning_at (loc, 0, "indirection will not access object of "
1135 "non-trivially-copyable type %qT in right "
1136 "operand of comma operator", type);
1138 case ICV_LEFT_OF_COMMA:
1139 warning_at (loc, 0, "indirection will not access object of "
1140 "non-trivially-copyable type %qT in left "
1141 "operand of comma operator", type);
1144 warning_at (loc, 0, "indirection will not access object of "
1145 "non-trivially-copyable type %qT in statement",
1148 case ICV_THIRD_IN_FOR:
1149 warning_at (loc, 0, "indirection will not access object of "
1150 "non-trivially-copyable type %qT in for "
1151 "increment expression", type);
1157 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
1159 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1160 operation is stripped off. Note that we don't warn about
1161 - an expression with TREE_NO_WARNING set. (For an example of
1162 such expressions, see build_over_call in call.c.)
1163 - automatic dereferencing of references, since the user cannot
1164 control it. (See also warn_if_unused_value() in c-common.c.) */
1165 if (warn_unused_value
1166 && implicit != ICV_CAST
1167 && (complain & tf_warning)
1168 && !TREE_NO_WARNING (expr)
1170 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1171 expr = TREE_OPERAND (expr, 0);
1179 /* External variables might be incomplete. */
1180 tree type = TREE_TYPE (expr);
1181 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1183 if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning))
1187 warning_at (loc, 0, "conversion to void will not access "
1188 "object %qE of incomplete type %qT", expr, type);
1190 case ICV_SECOND_OF_COND:
1191 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1192 "not be accessed in second operand of "
1193 "conditional expression", expr, type);
1195 case ICV_THIRD_OF_COND:
1196 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1197 "not be accessed in third operand of "
1198 "conditional expression", expr, type);
1200 case ICV_RIGHT_OF_COMMA:
1201 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1202 "not be accessed in right operand of comma operator",
1205 case ICV_LEFT_OF_COMMA:
1206 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1207 "not be accessed in left operand of comma operator",
1211 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1212 "not be accessed in statement", expr, type);
1214 case ICV_THIRD_IN_FOR:
1215 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1216 "not be accessed in for increment expression",
1227 /* Don't bother with the temporary object returned from a function if
1228 we don't use it and don't need to destroy it. We'll still
1229 allocate space for it in expand_call or declare_return_variable,
1230 but we don't need to track it through all the tree phases. */
1231 if (TARGET_EXPR_IMPLICIT_P (expr)
1232 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
1234 tree init = TARGET_EXPR_INITIAL (expr);
1235 if (TREE_CODE (init) == AGGR_INIT_EXPR
1236 && !AGGR_INIT_VIA_CTOR_P (init))
1238 tree fn = AGGR_INIT_EXPR_FN (init);
1239 expr = build_call_array_loc (input_location,
1240 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
1242 aggr_init_expr_nargs (init),
1243 AGGR_INIT_EXPR_ARGP (init));
1250 expr = resolve_nondeduced_context (expr);
1254 if (TREE_CODE (probe) == ADDR_EXPR)
1255 probe = TREE_OPERAND (expr, 0);
1256 if (type_unknown_p (probe))
1258 /* [over.over] enumerates the places where we can take the address
1259 of an overloaded function, and this is not one of them. */
1260 if (complain & tf_error)
1264 error_at (loc, "conversion to void "
1265 "cannot resolve address of overloaded function");
1267 case ICV_SECOND_OF_COND:
1268 error_at (loc, "second operand of conditional expression "
1269 "cannot resolve address of overloaded function");
1271 case ICV_THIRD_OF_COND:
1272 error_at (loc, "third operand of conditional expression "
1273 "cannot resolve address of overloaded function");
1275 case ICV_RIGHT_OF_COMMA:
1276 error_at (loc, "right operand of comma operator "
1277 "cannot resolve address of overloaded function");
1279 case ICV_LEFT_OF_COMMA:
1280 error_at (loc, "left operand of comma operator "
1281 "cannot resolve address of overloaded function");
1284 error_at (loc, "statement "
1285 "cannot resolve address of overloaded function");
1287 case ICV_THIRD_IN_FOR:
1288 error_at (loc, "for increment expression "
1289 "cannot resolve address of overloaded function");
1293 return error_mark_node;
1296 else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
1298 /* Only warn when there is no &. */
1299 if (complain & tf_warning)
1302 case ICV_SECOND_OF_COND:
1303 warning_at (loc, OPT_Waddress,
1304 "second operand of conditional expression "
1305 "is a reference, not call, to function %qE", expr);
1307 case ICV_THIRD_OF_COND:
1308 warning_at (loc, OPT_Waddress,
1309 "third operand of conditional expression "
1310 "is a reference, not call, to function %qE", expr);
1312 case ICV_RIGHT_OF_COMMA:
1313 warning_at (loc, OPT_Waddress,
1314 "right operand of comma operator "
1315 "is a reference, not call, to function %qE", expr);
1317 case ICV_LEFT_OF_COMMA:
1318 warning_at (loc, OPT_Waddress,
1319 "left operand of comma operator "
1320 "is a reference, not call, to function %qE", expr);
1323 warning_at (loc, OPT_Waddress,
1324 "statement is a reference, not call, to function %qE",
1327 case ICV_THIRD_IN_FOR:
1328 warning_at (loc, OPT_Waddress,
1329 "for increment expression "
1330 "is a reference, not call, to function %qE", expr);
1336 if (TREE_CODE (expr) == COMPONENT_REF)
1337 expr = TREE_OPERAND (expr, 0);
1341 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1343 if (implicit != ICV_CAST
1344 && warn_unused_value
1345 && !TREE_NO_WARNING (expr)
1346 && !processing_template_decl)
1348 /* The middle end does not warn about expressions that have
1349 been explicitly cast to void, so we must do so here. */
1350 if (!TREE_SIDE_EFFECTS (expr)) {
1351 if (complain & tf_warning)
1354 case ICV_SECOND_OF_COND:
1355 warning_at (loc, OPT_Wunused_value,
1356 "second operand of conditional expression "
1359 case ICV_THIRD_OF_COND:
1360 warning_at (loc, OPT_Wunused_value,
1361 "third operand of conditional expression "
1364 case ICV_RIGHT_OF_COMMA:
1365 warning_at (loc, OPT_Wunused_value,
1366 "right operand of comma operator has no effect");
1368 case ICV_LEFT_OF_COMMA:
1369 warning_at (loc, OPT_Wunused_value,
1370 "left operand of comma operator has no effect");
1373 warning_at (loc, OPT_Wunused_value,
1374 "statement has no effect");
1376 case ICV_THIRD_IN_FOR:
1377 warning_at (loc, OPT_Wunused_value,
1378 "for increment expression has no effect");
1387 enum tree_code code;
1388 enum tree_code_class tclass;
1391 /* We might like to warn about (say) "(int) f()", as the
1392 cast has no effect, but the compiler itself will
1393 generate implicit conversions under some
1394 circumstances. (For example a block copy will be
1395 turned into a call to "__builtin_memcpy", with a
1396 conversion of the return value to an appropriate
1397 type.) So, to avoid false positives, we strip
1398 conversions. Do not use STRIP_NOPs because it will
1399 not strip conversions to "void", as that is not a
1400 mode-preserving conversion. */
1401 while (TREE_CODE (e) == NOP_EXPR)
1402 e = TREE_OPERAND (e, 0);
1404 code = TREE_CODE (e);
1405 tclass = TREE_CODE_CLASS (code);
1406 if ((tclass == tcc_comparison
1407 || tclass == tcc_unary
1408 || (tclass == tcc_binary
1409 && !(code == MODIFY_EXPR
1410 || code == INIT_EXPR
1411 || code == PREDECREMENT_EXPR
1412 || code == PREINCREMENT_EXPR
1413 || code == POSTDECREMENT_EXPR
1414 || code == POSTINCREMENT_EXPR))
1415 || code == VEC_PERM_EXPR
1416 || code == VEC_COND_EXPR)
1417 && (complain & tf_warning))
1418 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1421 expr = build1 (CONVERT_EXPR, void_type_node, expr);
1423 if (! TREE_SIDE_EFFECTS (expr))
1428 /* Create an expression whose value is that of EXPR,
1429 converted to type TYPE. The TREE_TYPE of the value
1430 is always TYPE. This function implements all reasonable
1431 conversions; callers should filter out those that are
1432 not permitted by the language being compiled.
1434 Most of this routine is from build_reinterpret_cast.
1436 The back end cannot call cp_convert (what was convert) because
1437 conversions to/from basetypes may involve memory references
1438 (vbases) and adding or subtracting small values (multiple
1439 inheritance), but it calls convert from the constant folding code
1440 on subtrees of already built trees after it has ripped them apart.
1442 Also, if we ever support range variables, we'll probably also have to
1443 do a little bit more work. */
1446 convert (tree type, tree expr)
1450 if (type == error_mark_node || expr == error_mark_node)
1451 return error_mark_node;
1453 intype = TREE_TYPE (expr);
1455 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1456 return fold_if_not_in_template (build_nop (type, expr));
1458 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1459 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
1460 tf_warning_or_error);
1463 /* Like cp_convert, except permit conversions to take place which
1464 are not normally allowed due to access restrictions
1465 (such as conversion from sub-type to private super-type). */
1468 convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain)
1471 enum tree_code code = TREE_CODE (type);
1473 if (code == REFERENCE_TYPE)
1474 return (fold_if_not_in_template
1475 (convert_to_reference (type, e, CONV_C_CAST, 0,
1476 NULL_TREE, complain)));
1478 if (code == POINTER_TYPE)
1479 return fold_if_not_in_template (convert_to_pointer_force (type, e,
1482 /* From typeck.c convert_for_assignment */
1483 if (((TYPE_PTR_P (TREE_TYPE (e)) && TREE_CODE (e) == ADDR_EXPR
1484 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1485 || integer_zerop (e)
1486 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1487 && TYPE_PTRMEMFUNC_P (type))
1488 /* compatible pointer to member functions. */
1489 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1490 /*c_cast_p=*/1, complain);
1492 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain);
1495 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1496 exists, return the attempted conversion. This may
1497 return ERROR_MARK_NODE if the conversion is not
1498 allowed (references private members, etc).
1499 If no conversion exists, NULL_TREE is returned.
1501 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1502 object parameter, or by the second standard conversion sequence if
1503 that doesn't do it. This will probably wait for an overloading rewrite.
1507 build_type_conversion (tree xtype, tree expr)
1509 /* C++: check to see if we can convert this aggregate type
1510 into the required type. */
1511 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL,
1512 tf_warning_or_error);
1515 /* Convert the given EXPR to one of a group of types suitable for use in an
1516 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1517 which indicates which types are suitable. If COMPLAIN is true, complain
1518 about ambiguity; otherwise, the caller will deal with it. */
1521 build_expr_type_conversion (int desires, tree expr, bool complain)
1523 tree basetype = TREE_TYPE (expr);
1524 tree conv = NULL_TREE;
1525 tree winner = NULL_TREE;
1527 if (expr == null_node
1528 && (desires & WANT_INT)
1529 && !(desires & WANT_NULL))
1531 source_location loc =
1532 expansion_point_location_if_in_system_header (input_location);
1534 warning_at (loc, OPT_Wconversion_null,
1535 "converting NULL to non-pointer type");
1538 if (basetype == error_mark_node)
1539 return error_mark_node;
1541 if (! MAYBE_CLASS_TYPE_P (basetype))
1542 switch (TREE_CODE (basetype))
1545 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1547 /* else fall through... */
1550 return (desires & WANT_INT) ? expr : NULL_TREE;
1552 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1554 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1556 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1560 return (desires & WANT_POINTER) ? decay_conversion (expr,
1561 tf_warning_or_error)
1566 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1568 switch (TREE_CODE (TREE_TYPE (basetype)))
1572 return (desires & WANT_INT) ? expr : NULL_TREE;
1574 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1576 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1585 /* The code for conversions from class type is currently only used for
1586 delete expressions. Other expressions are handled by build_new_op. */
1587 if (!complete_type_or_maybe_complain (basetype, expr, complain))
1588 return error_mark_node;
1589 if (!TYPE_HAS_CONVERSION (basetype))
1592 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1596 tree cand = TREE_VALUE (conv);
1597 cand = OVL_CURRENT (cand);
1599 if (winner && winner == cand)
1602 if (DECL_NONCONVERTING_P (cand))
1605 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1607 switch (TREE_CODE (candidate))
1611 win = (desires & WANT_INT); break;
1613 win = (desires & WANT_ENUM); break;
1615 win = (desires & WANT_FLOAT); break;
1617 win = (desires & WANT_POINTER); break;
1621 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1623 switch (TREE_CODE (TREE_TYPE (candidate)))
1627 win = (desires & WANT_INT); break;
1629 win = (desires & WANT_ENUM); break;
1631 win = (desires & WANT_FLOAT); break;
1638 /* A wildcard could be instantiated to match any desired
1639 type, but we can't deduce the template argument. */
1640 if (WILDCARD_TYPE_P (candidate))
1647 if (TREE_CODE (cand) == TEMPLATE_DECL)
1650 error ("default type conversion can't deduce template"
1651 " argument for %qD", cand);
1652 return error_mark_node;
1658 = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1660 if (!same_type_ignoring_top_level_qualifiers_p (winner_type,
1665 error ("ambiguous default type conversion from %qT",
1667 inform (input_location,
1668 " candidate conversions include %qD and %qD",
1671 return error_mark_node;
1681 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1682 return build_user_type_conversion (type, expr, LOOKUP_NORMAL,
1683 tf_warning_or_error);
1689 /* Implements integral promotion (4.1) and float->double promotion. */
1692 type_promotes_to (tree type)
1696 if (type == error_mark_node)
1697 return error_mark_node;
1699 type = TYPE_MAIN_VARIANT (type);
1701 /* Check for promotions of target-defined types first. */
1702 promoted_type = targetm.promoted_type (type);
1704 return promoted_type;
1706 /* bool always promotes to int (not unsigned), even if it's the same
1708 if (TREE_CODE (type) == BOOLEAN_TYPE)
1709 type = integer_type_node;
1711 /* Normally convert enums to int, but convert wide enums to something
1712 wider. Scoped enums don't promote, but pretend they do for backward
1713 ABI bug compatibility wrt varargs. */
1714 else if (TREE_CODE (type) == ENUMERAL_TYPE
1715 || type == char16_type_node
1716 || type == char32_type_node
1717 || type == wchar_type_node)
1719 int precision = MAX (TYPE_PRECISION (type),
1720 TYPE_PRECISION (integer_type_node));
1721 tree totype = c_common_type_for_size (precision, 0);
1723 if (TREE_CODE (prom) == ENUMERAL_TYPE)
1724 prom = ENUM_UNDERLYING_TYPE (prom);
1725 if (TYPE_UNSIGNED (prom)
1726 && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype))
1727 prom = c_common_type_for_size (precision, 1);
1730 if (SCOPED_ENUM_P (type))
1732 if (abi_version_crosses (6)
1733 && TYPE_MODE (prom) != TYPE_MODE (type))
1734 warning (OPT_Wabi, "scoped enum %qT passed through ... as "
1735 "%qT before -fabi-version=6, %qT after",
1736 type, prom, ENUM_UNDERLYING_TYPE (type));
1737 if (!abi_version_at_least (6))
1743 else if (c_promoting_integer_type_p (type))
1745 /* Retain unsignedness if really not getting bigger. */
1746 if (TYPE_UNSIGNED (type)
1747 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1748 type = unsigned_type_node;
1750 type = integer_type_node;
1752 else if (type == float_type_node)
1753 type = double_type_node;
1758 /* The routines below this point are carefully written to conform to
1759 the standard. They use the same terminology, and follow the rules
1760 closely. Although they are used only in pt.c at the moment, they
1761 should presumably be used everywhere in the future. */
1763 /* Attempt to perform qualification conversions on EXPR to convert it
1764 to TYPE. Return the resulting expression, or error_mark_node if
1765 the conversion was impossible. */
1768 perform_qualification_conversions (tree type, tree expr)
1772 expr_type = TREE_TYPE (expr);
1774 if (same_type_p (type, expr_type))
1776 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1777 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1778 return build_nop (type, expr);
1779 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type)
1780 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1781 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1782 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1783 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1784 return build_nop (type, expr);
1786 return error_mark_node;