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 2011, 2012 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
, tsubst_flags_t
);
42 static tree
convert_to_pointer_force (tree
, tree
, tsubst_flags_t
);
43 static tree
build_type_conversion (tree
, tree
);
44 static tree
build_up_reference (tree
, tree
, int, tree
, tsubst_flags_t
);
45 static void warn_ref_binding (location_t
, 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
, tsubst_flags_t complain
)
79 tree intype
= TREE_TYPE (expr
);
82 location_t loc
= EXPR_LOC_OR_HERE (expr
);
84 if (intype
== error_mark_node
)
85 return error_mark_node
;
87 if (MAYBE_CLASS_TYPE_P (intype
))
89 intype
= complete_type (intype
);
90 if (!COMPLETE_TYPE_P (intype
))
92 if (complain
& tf_error
)
93 error_at (loc
, "can%'t convert from incomplete type %qT to %qT",
95 return error_mark_node
;
98 rval
= build_type_conversion (type
, expr
);
101 if ((complain
& tf_error
)
102 && rval
== error_mark_node
)
103 error_at (loc
, "conversion of %qE from %qT to %qT is ambiguous",
109 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
110 if (TREE_CODE (type
) == POINTER_TYPE
111 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
112 || VOID_TYPE_P (TREE_TYPE (type
))))
114 if (TYPE_PTRMEMFUNC_P (intype
)
115 || TREE_CODE (intype
) == METHOD_TYPE
)
116 return convert_member_func_to_ptr (type
, expr
, complain
);
117 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
118 return build_nop (type
, expr
);
119 intype
= TREE_TYPE (expr
);
122 if (expr
== error_mark_node
)
123 return error_mark_node
;
125 form
= TREE_CODE (intype
);
127 if (POINTER_TYPE_P (intype
))
129 intype
= TYPE_MAIN_VARIANT (intype
);
131 if (TYPE_MAIN_VARIANT (type
) != intype
132 && TREE_CODE (type
) == POINTER_TYPE
133 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
134 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type
))
135 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype
))
136 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
138 enum tree_code code
= PLUS_EXPR
;
144 intype_class
= TREE_TYPE (intype
);
145 type_class
= TREE_TYPE (type
);
147 same_p
= same_type_p (TYPE_MAIN_VARIANT (intype_class
),
148 TYPE_MAIN_VARIANT (type_class
));
150 /* Try derived to base conversion. */
152 binfo
= lookup_base (intype_class
, type_class
, ba_check
,
154 if (!same_p
&& !binfo
)
156 /* Try base to derived conversion. */
157 binfo
= lookup_base (type_class
, intype_class
, ba_check
,
161 if (binfo
== error_mark_node
)
162 return error_mark_node
;
166 expr
= build_base_path (code
, expr
, binfo
, 0, complain
);
167 /* Add any qualifier conversions. */
168 return build_nop (type
, expr
);
172 if (TYPE_PTRMEMFUNC_P (type
))
174 if (complain
& tf_error
)
175 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
177 return error_mark_node
;
180 return build_nop (type
, expr
);
182 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
183 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
184 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/false,
185 /*c_cast_p=*/false, complain
);
186 else if (TYPE_PTRMEMFUNC_P (intype
))
190 if (TREE_CODE (expr
) == PTRMEM_CST
)
191 return cp_convert_to_pointer (type
, PTRMEM_CST_MEMBER (expr
),
193 else if (TREE_CODE (expr
) == OFFSET_REF
)
195 tree object
= TREE_OPERAND (expr
, 0);
196 return get_member_function_from_ptrfunc (&object
,
197 TREE_OPERAND (expr
, 1),
201 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
203 return error_mark_node
;
206 if (null_ptr_cst_p (expr
))
208 if ((complain
& tf_warning
)
209 && c_inhibit_evaluation_warnings
== 0
210 && !NULLPTR_TYPE_P (TREE_TYPE (expr
)))
211 warning_at (loc
, OPT_Wzero_as_null_pointer_constant
,
212 "zero as null pointer constant");
214 if (TYPE_PTRMEMFUNC_P (type
))
215 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0,
216 /*c_cast_p=*/false, complain
);
218 if (TYPE_PTRDATAMEM_P (type
))
220 /* A NULL pointer-to-member is represented by -1, not by
222 expr
= build_int_cst_type (type
, -1);
225 expr
= build_int_cst (type
, 0);
229 else if (TYPE_PTRMEM_P (type
) && INTEGRAL_CODE_P (form
))
231 if (complain
& tf_error
)
232 error_at (loc
, "invalid conversion from %qT to %qT", intype
, type
);
233 return error_mark_node
;
236 if (INTEGRAL_CODE_P (form
))
238 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
239 return build1 (CONVERT_EXPR
, type
, expr
);
240 expr
= cp_convert (c_common_type_for_size (POINTER_SIZE
, 0), expr
,
242 /* Modes may be different but sizes should be the same. There
243 is supposed to be some integral type that is the same width
245 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
246 == GET_MODE_SIZE (TYPE_MODE (type
)));
248 return convert_to_pointer (type
, expr
);
251 if (type_unknown_p (expr
))
252 return instantiate_type (type
, expr
, complain
);
254 if (complain
& tf_error
)
255 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
257 return error_mark_node
;
260 /* Like convert, except permit conversions to take place which
261 are not normally allowed due to access restrictions
262 (such as conversion from sub-type to private super-type). */
265 convert_to_pointer_force (tree type
, tree expr
, tsubst_flags_t complain
)
267 tree intype
= TREE_TYPE (expr
);
268 enum tree_code form
= TREE_CODE (intype
);
270 if (form
== POINTER_TYPE
)
272 intype
= TYPE_MAIN_VARIANT (intype
);
274 if (TYPE_MAIN_VARIANT (type
) != intype
275 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
276 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type
))
277 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype
))
278 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
280 enum tree_code code
= PLUS_EXPR
;
283 binfo
= lookup_base (TREE_TYPE (intype
), TREE_TYPE (type
),
284 ba_unique
, NULL
, complain
);
287 binfo
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
288 ba_unique
, NULL
, complain
);
291 if (binfo
== error_mark_node
)
292 return error_mark_node
;
295 expr
= build_base_path (code
, expr
, binfo
, 0, complain
);
296 if (expr
== error_mark_node
)
297 return error_mark_node
;
298 /* Add any qualifier conversions. */
299 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr
)),
301 expr
= build_nop (type
, expr
);
307 return cp_convert_to_pointer (type
, expr
, complain
);
310 /* We are passing something to a function which requires a reference.
311 The type we are interested in is in TYPE. The initial
312 value we have to begin with is in ARG.
314 FLAGS controls how we manage access checking.
315 DIRECT_BIND in FLAGS controls how any temporaries are generated.
316 If DIRECT_BIND is set, DECL is the reference we're binding to. */
319 build_up_reference (tree type
, tree arg
, int flags
, tree decl
,
320 tsubst_flags_t complain
)
323 tree argtype
= TREE_TYPE (arg
);
324 tree target_type
= TREE_TYPE (type
);
326 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
328 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
330 /* Create a new temporary variable. We can't just use a TARGET_EXPR
331 here because it needs to live as long as DECL. */
334 arg
= make_temporary_var_for_ref_to_temp (decl
, target_type
);
336 /* Process the initializer for the declaration. */
337 DECL_INITIAL (arg
) = targ
;
338 cp_finish_decl (arg
, targ
, /*init_const_expr_p=*/false, NULL_TREE
,
339 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
341 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
342 return get_target_expr (arg
);
344 /* If we had a way to wrap this up, and say, if we ever needed its
345 address, transform all occurrences of the register, into a memory
346 reference we could win better. */
347 rval
= cp_build_addr_expr (arg
, tf_warning_or_error
);
348 if (rval
== error_mark_node
)
349 return error_mark_node
;
351 if ((flags
& LOOKUP_PROTECT
)
352 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
353 && MAYBE_CLASS_TYPE_P (argtype
)
354 && MAYBE_CLASS_TYPE_P (target_type
))
356 /* We go through lookup_base for the access control. */
357 tree binfo
= lookup_base (argtype
, target_type
, ba_check
,
359 if (binfo
== error_mark_node
)
360 return error_mark_node
;
361 if (binfo
== NULL_TREE
)
362 return error_not_base_type (target_type
, argtype
);
363 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1, complain
);
367 = convert_to_pointer_force (build_pointer_type (target_type
),
369 return build_nop (type
, rval
);
372 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
373 INTYPE is the original rvalue type and DECL is an optional _DECL node
376 [dcl.init.ref] says that if an rvalue is used to
377 initialize a reference, then the reference must be to a
378 non-volatile const type. */
381 warn_ref_binding (location_t loc
, tree reftype
, tree intype
, tree decl
)
383 tree ttl
= TREE_TYPE (reftype
);
385 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl
))
389 if (CP_TYPE_VOLATILE_P (ttl
) && decl
)
390 msg
= G_("initialization of volatile reference type %q#T from "
391 "rvalue of type %qT");
392 else if (CP_TYPE_VOLATILE_P (ttl
))
393 msg
= G_("conversion to volatile reference type %q#T "
394 "from rvalue of type %qT");
396 msg
= G_("initialization of non-const reference type %q#T from "
397 "rvalue of type %qT");
399 msg
= G_("conversion to non-const reference type %q#T from "
400 "rvalue of type %qT");
402 permerror (loc
, msg
, reftype
, intype
);
406 /* For C++: Only need to do one-level references, but cannot
407 get tripped up on signed/unsigned differences.
409 DECL is either NULL_TREE or the _DECL node for a reference that is being
410 initialized. It can be error_mark_node if we don't know the _DECL but
411 we know it's an initialization. */
414 convert_to_reference (tree reftype
, tree expr
, int convtype
,
415 int flags
, tree decl
, tsubst_flags_t complain
)
417 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
419 tree rval
= NULL_TREE
;
420 tree rval_as_conversion
= NULL_TREE
;
421 bool can_convert_intype_to_type
;
422 location_t loc
= EXPR_LOC_OR_HERE (expr
);
424 if (TREE_CODE (type
) == FUNCTION_TYPE
425 && TREE_TYPE (expr
) == unknown_type_node
)
426 expr
= instantiate_type (type
, expr
, complain
);
428 if (expr
== error_mark_node
)
429 return error_mark_node
;
431 intype
= TREE_TYPE (expr
);
433 gcc_assert (TREE_CODE (intype
) != REFERENCE_TYPE
);
434 gcc_assert (TREE_CODE (reftype
) == REFERENCE_TYPE
);
436 intype
= TYPE_MAIN_VARIANT (intype
);
438 can_convert_intype_to_type
= can_convert (type
, intype
, complain
);
440 if (!can_convert_intype_to_type
441 && (convtype
& CONV_IMPLICIT
) && MAYBE_CLASS_TYPE_P (intype
)
442 && ! (flags
& LOOKUP_NO_CONVERSION
))
444 /* Look for a user-defined conversion to lvalue that we can use. */
447 = build_type_conversion (reftype
, expr
);
449 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
450 && real_lvalue_p (rval_as_conversion
))
452 expr
= rval_as_conversion
;
453 rval_as_conversion
= NULL_TREE
;
455 can_convert_intype_to_type
= 1;
459 if (((convtype
& CONV_STATIC
) && can_convert (intype
, type
, complain
))
460 || ((convtype
& CONV_IMPLICIT
) && can_convert_intype_to_type
))
463 tree ttl
= TREE_TYPE (reftype
);
464 tree ttr
= lvalue_type (expr
);
466 if ((complain
& tf_warning
)
467 && ! real_lvalue_p (expr
))
468 warn_ref_binding (loc
, reftype
, intype
, decl
);
470 if (! (convtype
& CONV_CONST
)
471 && !at_least_as_qualified_p (ttl
, ttr
))
473 if (complain
& tf_error
)
474 permerror (loc
, "conversion from %qT to %qT discards qualifiers",
477 return error_mark_node
;
481 return build_up_reference (reftype
, expr
, flags
, decl
, complain
);
483 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
485 /* When casting an lvalue to a reference type, just convert into
486 a pointer to the new type and deference it. This is allowed
487 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
488 should be done directly (jason). (int &)ri ---> *(int*)&ri */
490 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
492 if ((complain
& tf_warning
)
493 && TREE_CODE (intype
) == POINTER_TYPE
494 && (comptypes (TREE_TYPE (intype
), type
,
495 COMPARE_BASE
| COMPARE_DERIVED
)))
496 warning_at (loc
, 0, "casting %qT to %qT does not dereference pointer",
499 rval
= cp_build_addr_expr (expr
, complain
);
500 if (rval
!= error_mark_node
)
501 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
503 if (rval
!= error_mark_node
)
504 rval
= build1 (NOP_EXPR
, reftype
, rval
);
508 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
509 ICR_CONVERTING
, 0, 0, complain
);
510 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
512 if (complain
& tf_warning
)
513 warn_ref_binding (loc
, reftype
, intype
, decl
);
514 rval
= build_up_reference (reftype
, rval
, flags
, decl
, complain
);
519 /* If we found a way to convert earlier, then use it. */
523 if (complain
& tf_error
)
524 error_at (loc
, "cannot convert type %qT to type %qT", intype
, reftype
);
526 return error_mark_node
;
529 /* We are using a reference VAL for its value. Bash that reference all the
530 way down to its lowest form. */
533 convert_from_reference (tree val
)
536 && TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
538 tree t
= TREE_TYPE (TREE_TYPE (val
));
539 tree ref
= build1 (INDIRECT_REF
, t
, val
);
542 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
543 so that we get the proper error message if the result is used
544 to assign to. Also, &* is supposed to be a no-op. */
545 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
546 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
547 TREE_SIDE_EFFECTS (ref
)
548 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (val
));
555 /* Really perform an lvalue-to-rvalue conversion, including copying an
556 argument of class type into a temporary. */
559 force_rvalue (tree expr
, tsubst_flags_t complain
)
561 tree type
= TREE_TYPE (expr
);
562 if (MAYBE_CLASS_TYPE_P (type
) && TREE_CODE (expr
) != TARGET_EXPR
)
564 VEC(tree
,gc
) *args
= make_tree_vector_single (expr
);
565 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
566 &args
, type
, LOOKUP_NORMAL
, complain
);
567 release_tree_vector (args
);
568 expr
= build_cplus_new (type
, expr
, complain
);
571 expr
= decay_conversion (expr
, complain
);
577 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
578 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
582 ignore_overflows (tree expr
, tree orig
)
584 if (TREE_CODE (expr
) == INTEGER_CST
585 && TREE_CODE (orig
) == INTEGER_CST
586 && TREE_OVERFLOW (expr
) != TREE_OVERFLOW (orig
))
588 gcc_assert (!TREE_OVERFLOW (orig
));
589 /* Ensure constant sharing. */
590 expr
= build_int_cst_wide (TREE_TYPE (expr
),
591 TREE_INT_CST_LOW (expr
),
592 TREE_INT_CST_HIGH (expr
));
597 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
601 cp_fold_convert (tree type
, tree expr
)
603 tree conv
= fold_convert (type
, expr
);
604 conv
= ignore_overflows (conv
, expr
);
608 /* C++ conversions, preference to static cast conversions. */
611 cp_convert (tree type
, tree expr
, tsubst_flags_t complain
)
613 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
, complain
);
616 /* C++ equivalent of convert_and_check but using cp_convert as the
619 Convert EXPR to TYPE, warning about conversion problems with constants.
620 Invoke this function on every expression that is converted implicitly,
621 i.e. because of language rules and not because of an explicit cast. */
624 cp_convert_and_check (tree type
, tree expr
, tsubst_flags_t complain
)
628 if (TREE_TYPE (expr
) == type
)
631 result
= cp_convert (type
, expr
, complain
);
633 if ((complain
& tf_warning
)
634 && c_inhibit_evaluation_warnings
== 0
635 && !TREE_OVERFLOW_P (expr
)
636 && result
!= error_mark_node
)
637 warnings_for_convert_and_check (type
, expr
, result
);
644 FLAGS indicates how we should behave. */
647 ocp_convert (tree type
, tree expr
, int convtype
, int flags
,
648 tsubst_flags_t complain
)
651 enum tree_code code
= TREE_CODE (type
);
652 const char *invalid_conv_diag
;
654 location_t loc
= EXPR_LOC_OR_HERE (expr
);
656 if (error_operand_p (e
) || type
== error_mark_node
)
657 return error_mark_node
;
659 complete_type (type
);
660 complete_type (TREE_TYPE (expr
));
662 if ((invalid_conv_diag
663 = targetm
.invalid_conversion (TREE_TYPE (expr
), type
)))
665 if (complain
& tf_error
)
666 error (invalid_conv_diag
);
667 return error_mark_node
;
670 /* FIXME remove when moving to c_fully_fold model. */
671 /* FIXME do we still need this test? */
672 if (!CLASS_TYPE_P (type
))
673 e
= integral_constant_value (e
);
674 if (error_operand_p (e
))
675 return error_mark_node
;
677 if (MAYBE_CLASS_TYPE_P (type
) && (convtype
& CONV_FORCE_TEMP
))
678 /* We need a new temporary; don't take this shortcut. */;
679 else if (same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (e
)))
681 if (same_type_p (type
, TREE_TYPE (e
)))
682 /* The call to fold will not always remove the NOP_EXPR as
683 might be expected, since if one of the types is a typedef;
684 the comparison in fold is just equality of pointers, not a
685 call to comptypes. We don't call fold in this case because
686 that can result in infinite recursion; fold will call
687 convert, which will call ocp_convert, etc. */
689 /* For complex data types, we need to perform componentwise
691 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
692 return fold_if_not_in_template (convert_to_complex (type
, e
));
693 else if (TREE_CODE (e
) == TARGET_EXPR
)
695 /* Don't build a NOP_EXPR of class type. Instead, change the
696 type of the temporary. */
697 TREE_TYPE (e
) = TREE_TYPE (TARGET_EXPR_SLOT (e
)) = type
;
702 /* We shouldn't be treating objects of ADDRESSABLE type as
704 gcc_assert (!TREE_ADDRESSABLE (type
));
705 return fold_if_not_in_template (build_nop (type
, e
));
709 e1
= targetm
.convert_to_type (type
, e
);
713 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
715 e
= convert_to_void (e
, ICV_CAST
, complain
);
719 if (INTEGRAL_CODE_P (code
))
721 tree intype
= TREE_TYPE (e
);
724 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
726 /* enum = enum, enum = int, enum = float, (enum)pointer are all
728 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
729 || TREE_CODE (intype
) == REAL_TYPE
)
730 && ! (convtype
& CONV_STATIC
))
731 || TREE_CODE (intype
) == POINTER_TYPE
)
733 if (complain
& tf_error
)
734 permerror (loc
, "conversion from %q#T to %q#T", intype
, type
);
736 return error_mark_node
;
739 /* [expr.static.cast]
741 8. A value of integral or enumeration type can be explicitly
742 converted to an enumeration type. The value is unchanged if
743 the original value is within the range of the enumeration
744 values. Otherwise, the resulting enumeration value is
746 if ((complain
& tf_warning
)
747 && TREE_CODE (e
) == INTEGER_CST
748 && !int_fits_type_p (e
, ENUM_UNDERLYING_TYPE (type
)))
749 warning_at (loc
, OPT_Wconversion
,
750 "the result of the conversion is unspecified because "
751 "%qE is outside the range of type %qT",
754 if (MAYBE_CLASS_TYPE_P (intype
))
757 rval
= build_type_conversion (type
, e
);
760 if (complain
& tf_error
)
761 error_at (loc
, "%q#T used where a %qT was expected", intype
, type
);
762 return error_mark_node
;
764 if (code
== BOOLEAN_TYPE
)
766 if (TREE_CODE (intype
) == VOID_TYPE
)
768 if (complain
& tf_error
)
770 "could not convert %qE from %<void%> to %<bool%>",
772 return error_mark_node
;
775 /* We can't implicitly convert a scoped enum to bool, so convert
776 to the underlying type first. */
777 if (SCOPED_ENUM_P (intype
) && (convtype
& CONV_STATIC
))
778 e
= build_nop (ENUM_UNDERLYING_TYPE (intype
), e
);
779 return cp_truthvalue_conversion (e
);
782 converted
= fold_if_not_in_template (convert_to_integer (type
, e
));
784 /* Ignore any integer overflow caused by the conversion. */
785 return ignore_overflows (converted
, e
);
787 if (NULLPTR_TYPE_P (type
) && e
&& null_ptr_cst_p (e
))
789 if (POINTER_TYPE_P (type
) || TYPE_PTRMEM_P (type
))
790 return fold_if_not_in_template (cp_convert_to_pointer (type
, e
, complain
));
791 if (code
== VECTOR_TYPE
)
793 tree in_vtype
= TREE_TYPE (e
);
794 if (MAYBE_CLASS_TYPE_P (in_vtype
))
797 ret_val
= build_type_conversion (type
, e
);
800 if (complain
& tf_error
)
801 error_at (loc
, "%q#T used where a %qT was expected",
803 return error_mark_node
;
805 return fold_if_not_in_template (convert_to_vector (type
, e
));
807 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
809 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e
)))
812 rval
= build_type_conversion (type
, e
);
815 else if (complain
& tf_error
)
817 "%q#T used where a floating point value was expected",
820 if (code
== REAL_TYPE
)
821 return fold_if_not_in_template (convert_to_real (type
, e
));
822 else if (code
== COMPLEX_TYPE
)
823 return fold_if_not_in_template (convert_to_complex (type
, e
));
826 /* New C++ semantics: since assignment is now based on
827 memberwise copying, if the rhs type is derived from the
828 lhs type, then we may still do a conversion. */
829 if (RECORD_OR_UNION_CODE_P (code
))
831 tree dtype
= TREE_TYPE (e
);
832 tree ctor
= NULL_TREE
;
834 dtype
= TYPE_MAIN_VARIANT (dtype
);
836 /* Conversion between aggregate types. New C++ semantics allow
837 objects of derived type to be cast to objects of base type.
838 Old semantics only allowed this between pointers.
840 There may be some ambiguity between using a constructor
841 vs. using a type conversion operator when both apply. */
845 if (abstract_virtuals_error (NULL_TREE
, type
))
846 return error_mark_node
;
848 if (BRACE_ENCLOSED_INITIALIZER_P (ctor
))
849 ctor
= perform_implicit_conversion (type
, ctor
, complain
);
850 else if ((flags
& LOOKUP_ONLYCONVERTING
)
851 && ! (CLASS_TYPE_P (dtype
) && DERIVED_FROM_P (type
, dtype
)))
852 /* For copy-initialization, first we create a temp of the proper type
853 with a user-defined conversion sequence, then we direct-initialize
854 the target with the temp (see [dcl.init]). */
855 ctor
= build_user_type_conversion (type
, ctor
, flags
, complain
);
858 VEC(tree
,gc
) *ctor_vec
= make_tree_vector_single (ctor
);
859 ctor
= build_special_member_call (NULL_TREE
,
860 complete_ctor_identifier
,
862 type
, flags
, complain
);
863 release_tree_vector (ctor_vec
);
866 return build_cplus_new (type
, ctor
, complain
);
869 if (complain
& tf_error
)
871 /* If the conversion failed and expr was an invalid use of pointer to
872 member function, try to report a meaningful error. */
873 if (invalid_nonstatic_memfn_p (expr
, complain
))
874 /* We displayed the error message. */;
876 error_at (loc
, "conversion from %qT to non-scalar type %qT requested",
877 TREE_TYPE (expr
), type
);
879 return error_mark_node
;
882 /* When an expression is used in a void context, its value is discarded and
883 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
884 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
885 in a void context. The C++ standard does not define what an `access' to an
886 object is, but there is reason to believe that it is the lvalue to rvalue
887 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
888 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
889 indicates that volatile semantics should be the same between C and C++
890 where ever possible. C leaves it implementation defined as to what
891 constitutes an access to a volatile. So, we interpret `*vp' as a read of
892 the volatile object `vp' points to, unless that is an incomplete type. For
893 volatile references we do not do this interpretation, because that would
894 make it impossible to ignore the reference return value from functions. We
895 issue warnings in the confusing cases.
897 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
898 to void via a cast. If an expression is being implicitly converted, IMPLICIT
899 indicates the context of the implicit conversion. */
902 convert_to_void (tree expr
, impl_conv_void implicit
, tsubst_flags_t complain
)
904 location_t loc
= EXPR_LOC_OR_HERE (expr
);
906 if (expr
== error_mark_node
907 || TREE_TYPE (expr
) == error_mark_node
)
908 return error_mark_node
;
910 if (implicit
== ICV_CAST
)
911 mark_exp_read (expr
);
916 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
917 exprv
= TREE_OPERAND (exprv
, 1);
919 || handled_component_p (exprv
)
920 || TREE_CODE (exprv
) == INDIRECT_REF
)
921 /* Expr is not being 'used' here, otherwise we whould have
922 called mark_{rl}value_use use here, which would have in turn
923 called mark_exp_read. Rather, we call mark_exp_read directly
924 to avoid some warnings when
925 -Wunused-but-set-{variable,parameter} is in effect. */
926 mark_exp_read (exprv
);
929 if (!TREE_TYPE (expr
))
931 if (invalid_nonstatic_memfn_p (expr
, complain
))
932 return error_mark_node
;
933 if (TREE_CODE (expr
) == PSEUDO_DTOR_EXPR
)
935 if (complain
& tf_error
)
936 error_at (loc
, "pseudo-destructor is not called");
937 return error_mark_node
;
939 if (VOID_TYPE_P (TREE_TYPE (expr
)))
941 switch (TREE_CODE (expr
))
945 /* The two parts of a cond expr might be separate lvalues. */
946 tree op1
= TREE_OPERAND (expr
,1);
947 tree op2
= TREE_OPERAND (expr
,2);
948 bool side_effects
= ((op1
&& TREE_SIDE_EFFECTS (op1
))
949 || TREE_SIDE_EFFECTS (op2
));
950 tree new_op1
, new_op2
;
952 if (implicit
!= ICV_CAST
&& !side_effects
)
955 new_op1
= convert_to_void (op1
, ICV_SECOND_OF_COND
, complain
);
956 new_op2
= convert_to_void (op2
, ICV_THIRD_OF_COND
, complain
);
961 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
962 new_op2
= convert_to_void (op2
, ICV_CAST
, complain
);
965 expr
= build3 (COND_EXPR
, TREE_TYPE (new_op2
),
966 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
972 /* The second part of a compound expr contains the value. */
973 tree op1
= TREE_OPERAND (expr
,1);
975 if (implicit
!= ICV_CAST
&& !TREE_NO_WARNING (expr
))
976 new_op1
= convert_to_void (op1
, ICV_RIGHT_OF_COMMA
, complain
);
978 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
982 tree t
= build2 (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
983 TREE_OPERAND (expr
, 0), new_op1
);
990 case NON_LVALUE_EXPR
:
992 /* These have already decayed to rvalue. */
995 case CALL_EXPR
: /* We have a special meaning for volatile void fn(). */
1000 tree type
= TREE_TYPE (expr
);
1001 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
1003 int is_volatile
= TYPE_VOLATILE (type
);
1004 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
1006 /* Can't load the value if we don't know the type. */
1007 if (is_volatile
&& !is_complete
)
1009 if (complain
& tf_warning
)
1013 warning_at (loc
, 0, "conversion to void will not access "
1014 "object of incomplete type %qT", type
);
1016 case ICV_SECOND_OF_COND
:
1017 warning_at (loc
, 0, "indirection will not access object of "
1018 "incomplete type %qT in second operand "
1019 "of conditional expression", type
);
1021 case ICV_THIRD_OF_COND
:
1022 warning_at (loc
, 0, "indirection will not access object of "
1023 "incomplete type %qT in third operand "
1024 "of conditional expression", type
);
1026 case ICV_RIGHT_OF_COMMA
:
1027 warning_at (loc
, 0, "indirection will not access object of "
1028 "incomplete type %qT in right operand of "
1029 "comma operator", type
);
1031 case ICV_LEFT_OF_COMMA
:
1032 warning_at (loc
, 0, "indirection will not access object of "
1033 "incomplete type %qT in left operand of "
1034 "comma operator", type
);
1037 warning_at (loc
, 0, "indirection will not access object of "
1038 "incomplete type %qT in statement", type
);
1040 case ICV_THIRD_IN_FOR
:
1041 warning_at (loc
, 0, "indirection will not access object of "
1042 "incomplete type %qT in for increment "
1043 "expression", type
);
1049 /* Don't load the value if this is an implicit dereference, or if
1050 the type needs to be handled by ctors/dtors. */
1051 else if (is_volatile
&& is_reference
)
1053 if (complain
& tf_warning
)
1057 warning_at (loc
, 0, "conversion to void will not access "
1058 "object of type %qT", type
);
1060 case ICV_SECOND_OF_COND
:
1061 warning_at (loc
, 0, "implicit dereference will not access "
1062 "object of type %qT in second operand of "
1063 "conditional expression", type
);
1065 case ICV_THIRD_OF_COND
:
1066 warning_at (loc
, 0, "implicit dereference will not access "
1067 "object of type %qT in third operand of "
1068 "conditional expression", type
);
1070 case ICV_RIGHT_OF_COMMA
:
1071 warning_at (loc
, 0, "implicit dereference will not access "
1072 "object of type %qT in right operand of "
1073 "comma operator", type
);
1075 case ICV_LEFT_OF_COMMA
:
1076 warning_at (loc
, 0, "implicit dereference will not access "
1077 "object of type %qT in left operand of comma "
1081 warning_at (loc
, 0, "implicit dereference will not access "
1082 "object of type %qT in statement", type
);
1084 case ICV_THIRD_IN_FOR
:
1085 warning_at (loc
, 0, "implicit dereference will not access "
1086 "object of type %qT in for increment expression",
1093 else if (is_volatile
&& TREE_ADDRESSABLE (type
))
1095 if (complain
& tf_warning
)
1099 warning_at (loc
, 0, "conversion to void will not access "
1100 "object of non-trivially-copyable type %qT",
1103 case ICV_SECOND_OF_COND
:
1104 warning_at (loc
, 0, "indirection will not access object of "
1105 "non-trivially-copyable type %qT in second "
1106 "operand of conditional expression", type
);
1108 case ICV_THIRD_OF_COND
:
1109 warning_at (loc
, 0, "indirection will not access object of "
1110 "non-trivially-copyable type %qT in third "
1111 "operand of conditional expression", type
);
1113 case ICV_RIGHT_OF_COMMA
:
1114 warning_at (loc
, 0, "indirection will not access object of "
1115 "non-trivially-copyable type %qT in right "
1116 "operand of comma operator", type
);
1118 case ICV_LEFT_OF_COMMA
:
1119 warning_at (loc
, 0, "indirection will not access object of "
1120 "non-trivially-copyable type %qT in left "
1121 "operand of comma operator", type
);
1124 warning_at (loc
, 0, "indirection will not access object of "
1125 "non-trivially-copyable type %qT in statement",
1128 case ICV_THIRD_IN_FOR
:
1129 warning_at (loc
, 0, "indirection will not access object of "
1130 "non-trivially-copyable type %qT in for "
1131 "increment expression", type
);
1137 if (is_reference
|| !is_volatile
|| !is_complete
|| TREE_ADDRESSABLE (type
))
1139 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1140 operation is stripped off. Note that we don't warn about
1141 - an expression with TREE_NO_WARNING set. (For an example of
1142 such expressions, see build_over_call in call.c.)
1143 - automatic dereferencing of references, since the user cannot
1144 control it. (See also warn_if_unused_value() in c-common.c.) */
1145 if (warn_unused_value
1146 && implicit
!= ICV_CAST
1147 && (complain
& tf_warning
)
1148 && !TREE_NO_WARNING (expr
)
1150 warning_at (loc
, OPT_Wunused_value
, "value computed is not used");
1151 expr
= TREE_OPERAND (expr
, 0);
1159 /* External variables might be incomplete. */
1160 tree type
= TREE_TYPE (expr
);
1161 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
1163 if (TYPE_VOLATILE (type
) && !is_complete
&& (complain
& tf_warning
))
1167 warning_at (loc
, 0, "conversion to void will not access "
1168 "object %qE of incomplete type %qT", expr
, type
);
1170 case ICV_SECOND_OF_COND
:
1171 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1172 "not be accessed in second operand of "
1173 "conditional expression", expr
, type
);
1175 case ICV_THIRD_OF_COND
:
1176 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1177 "not be accessed in third operand of "
1178 "conditional expression", expr
, type
);
1180 case ICV_RIGHT_OF_COMMA
:
1181 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1182 "not be accessed in right operand of comma operator",
1185 case ICV_LEFT_OF_COMMA
:
1186 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1187 "not be accessed in left operand of comma operator",
1191 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1192 "not be accessed in statement", expr
, type
);
1194 case ICV_THIRD_IN_FOR
:
1195 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1196 "not be accessed in for increment expression",
1207 /* Don't bother with the temporary object returned from a function if
1208 we don't use it and don't need to destroy it. We'll still
1209 allocate space for it in expand_call or declare_return_variable,
1210 but we don't need to track it through all the tree phases. */
1211 if (TARGET_EXPR_IMPLICIT_P (expr
)
1212 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr
)))
1214 tree init
= TARGET_EXPR_INITIAL (expr
);
1215 if (TREE_CODE (init
) == AGGR_INIT_EXPR
1216 && !AGGR_INIT_VIA_CTOR_P (init
))
1218 tree fn
= AGGR_INIT_EXPR_FN (init
);
1219 expr
= build_call_array_loc (input_location
,
1220 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
))),
1222 aggr_init_expr_nargs (init
),
1223 AGGR_INIT_EXPR_ARGP (init
));
1230 expr
= resolve_nondeduced_context (expr
);
1234 if (TREE_CODE (probe
) == ADDR_EXPR
)
1235 probe
= TREE_OPERAND (expr
, 0);
1236 if (type_unknown_p (probe
))
1238 /* [over.over] enumerates the places where we can take the address
1239 of an overloaded function, and this is not one of them. */
1240 if (complain
& tf_error
)
1244 error_at (loc
, "conversion to void "
1245 "cannot resolve address of overloaded function");
1247 case ICV_SECOND_OF_COND
:
1248 error_at (loc
, "second operand of conditional expression "
1249 "cannot resolve address of overloaded function");
1251 case ICV_THIRD_OF_COND
:
1252 error_at (loc
, "third operand of conditional expression "
1253 "cannot resolve address of overloaded function");
1255 case ICV_RIGHT_OF_COMMA
:
1256 error_at (loc
, "right operand of comma operator "
1257 "cannot resolve address of overloaded function");
1259 case ICV_LEFT_OF_COMMA
:
1260 error_at (loc
, "left operand of comma operator "
1261 "cannot resolve address of overloaded function");
1264 error_at (loc
, "statement "
1265 "cannot resolve address of overloaded function");
1267 case ICV_THIRD_IN_FOR
:
1268 error_at (loc
, "for increment expression "
1269 "cannot resolve address of overloaded function");
1273 return error_mark_node
;
1274 expr
= void_zero_node
;
1276 else if (implicit
!= ICV_CAST
&& probe
== expr
&& is_overloaded_fn (probe
))
1278 /* Only warn when there is no &. */
1279 if (complain
& tf_warning
)
1282 case ICV_SECOND_OF_COND
:
1283 warning_at (loc
, OPT_Waddress
,
1284 "second operand of conditional expression "
1285 "is a reference, not call, to function %qE", expr
);
1287 case ICV_THIRD_OF_COND
:
1288 warning_at (loc
, OPT_Waddress
,
1289 "third operand of conditional expression "
1290 "is a reference, not call, to function %qE", expr
);
1292 case ICV_RIGHT_OF_COMMA
:
1293 warning_at (loc
, OPT_Waddress
,
1294 "right operand of comma operator "
1295 "is a reference, not call, to function %qE", expr
);
1297 case ICV_LEFT_OF_COMMA
:
1298 warning_at (loc
, OPT_Waddress
,
1299 "left operand of comma operator "
1300 "is a reference, not call, to function %qE", expr
);
1303 warning_at (loc
, OPT_Waddress
,
1304 "statement is a reference, not call, to function %qE",
1307 case ICV_THIRD_IN_FOR
:
1308 warning_at (loc
, OPT_Waddress
,
1309 "for increment expression "
1310 "is a reference, not call, to function %qE", expr
);
1316 if (TREE_CODE (expr
) == COMPONENT_REF
)
1317 expr
= TREE_OPERAND (expr
, 0);
1321 if (expr
!= error_mark_node
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
1323 if (implicit
!= ICV_CAST
1324 && warn_unused_value
1325 && !TREE_NO_WARNING (expr
)
1326 && !processing_template_decl
)
1328 /* The middle end does not warn about expressions that have
1329 been explicitly cast to void, so we must do so here. */
1330 if (!TREE_SIDE_EFFECTS (expr
)) {
1331 if (complain
& tf_warning
)
1334 case ICV_SECOND_OF_COND
:
1335 warning_at (loc
, OPT_Wunused_value
,
1336 "second operand of conditional expression "
1339 case ICV_THIRD_OF_COND
:
1340 warning_at (loc
, OPT_Wunused_value
,
1341 "third operand of conditional expression "
1344 case ICV_RIGHT_OF_COMMA
:
1345 warning_at (loc
, OPT_Wunused_value
,
1346 "right operand of comma operator has no effect");
1348 case ICV_LEFT_OF_COMMA
:
1349 warning_at (loc
, OPT_Wunused_value
,
1350 "left operand of comma operator has no effect");
1353 warning_at (loc
, OPT_Wunused_value
,
1354 "statement has no effect");
1356 case ICV_THIRD_IN_FOR
:
1357 warning_at (loc
, OPT_Wunused_value
,
1358 "for increment expression has no effect");
1367 enum tree_code code
;
1368 enum tree_code_class tclass
;
1371 /* We might like to warn about (say) "(int) f()", as the
1372 cast has no effect, but the compiler itself will
1373 generate implicit conversions under some
1374 circumstances. (For example a block copy will be
1375 turned into a call to "__builtin_memcpy", with a
1376 conversion of the return value to an appropriate
1377 type.) So, to avoid false positives, we strip
1378 conversions. Do not use STRIP_NOPs because it will
1379 not strip conversions to "void", as that is not a
1380 mode-preserving conversion. */
1381 while (TREE_CODE (e
) == NOP_EXPR
)
1382 e
= TREE_OPERAND (e
, 0);
1384 code
= TREE_CODE (e
);
1385 tclass
= TREE_CODE_CLASS (code
);
1386 if ((tclass
== tcc_comparison
1387 || tclass
== tcc_unary
1388 || (tclass
== tcc_binary
1389 && !(code
== MODIFY_EXPR
1390 || code
== INIT_EXPR
1391 || code
== PREDECREMENT_EXPR
1392 || code
== PREINCREMENT_EXPR
1393 || code
== POSTDECREMENT_EXPR
1394 || code
== POSTINCREMENT_EXPR
)))
1395 && (complain
& tf_warning
))
1396 warning_at (loc
, OPT_Wunused_value
, "value computed is not used");
1399 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
1401 if (! TREE_SIDE_EFFECTS (expr
))
1402 expr
= void_zero_node
;
1406 /* Create an expression whose value is that of EXPR,
1407 converted to type TYPE. The TREE_TYPE of the value
1408 is always TYPE. This function implements all reasonable
1409 conversions; callers should filter out those that are
1410 not permitted by the language being compiled.
1412 Most of this routine is from build_reinterpret_cast.
1414 The back end cannot call cp_convert (what was convert) because
1415 conversions to/from basetypes may involve memory references
1416 (vbases) and adding or subtracting small values (multiple
1417 inheritance), but it calls convert from the constant folding code
1418 on subtrees of already built trees after it has ripped them apart.
1420 Also, if we ever support range variables, we'll probably also have to
1421 do a little bit more work. */
1424 convert (tree type
, tree expr
)
1428 if (type
== error_mark_node
|| expr
== error_mark_node
)
1429 return error_mark_node
;
1431 intype
= TREE_TYPE (expr
);
1433 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
1434 return fold_if_not_in_template (build_nop (type
, expr
));
1436 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
1437 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
,
1438 tf_warning_or_error
);
1441 /* Like cp_convert, except permit conversions to take place which
1442 are not normally allowed due to access restrictions
1443 (such as conversion from sub-type to private super-type). */
1446 convert_force (tree type
, tree expr
, int convtype
, tsubst_flags_t complain
)
1449 enum tree_code code
= TREE_CODE (type
);
1451 if (code
== REFERENCE_TYPE
)
1452 return (fold_if_not_in_template
1453 (convert_to_reference (type
, e
, CONV_C_CAST
, 0,
1454 NULL_TREE
, complain
)));
1456 if (code
== POINTER_TYPE
)
1457 return fold_if_not_in_template (convert_to_pointer_force (type
, e
,
1460 /* From typeck.c convert_for_assignment */
1461 if (((TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
&& TREE_CODE (e
) == ADDR_EXPR
1462 && TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
1463 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1464 || integer_zerop (e
)
1465 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1466 && TYPE_PTRMEMFUNC_P (type
))
1467 /* compatible pointer to member functions. */
1468 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1,
1469 /*c_cast_p=*/1, complain
);
1471 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
, complain
);
1474 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1475 exists, return the attempted conversion. This may
1476 return ERROR_MARK_NODE if the conversion is not
1477 allowed (references private members, etc).
1478 If no conversion exists, NULL_TREE is returned.
1480 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1481 object parameter, or by the second standard conversion sequence if
1482 that doesn't do it. This will probably wait for an overloading rewrite.
1486 build_type_conversion (tree xtype
, tree expr
)
1488 /* C++: check to see if we can convert this aggregate type
1489 into the required type. */
1490 return build_user_type_conversion (xtype
, expr
, LOOKUP_NORMAL
,
1491 tf_warning_or_error
);
1494 /* Convert the given EXPR to one of a group of types suitable for use in an
1495 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1496 which indicates which types are suitable. If COMPLAIN is true, complain
1497 about ambiguity; otherwise, the caller will deal with it. */
1500 build_expr_type_conversion (int desires
, tree expr
, bool complain
)
1502 tree basetype
= TREE_TYPE (expr
);
1503 tree conv
= NULL_TREE
;
1504 tree winner
= NULL_TREE
;
1506 if (expr
== null_node
1507 && (desires
& WANT_INT
)
1508 && !(desires
& WANT_NULL
))
1510 source_location loc
=
1511 expansion_point_location_if_in_system_header (input_location
);
1513 warning_at (loc
, OPT_Wconversion_null
,
1514 "converting NULL to non-pointer type");
1517 basetype
= TREE_TYPE (expr
);
1519 if (basetype
== error_mark_node
)
1520 return error_mark_node
;
1522 if (! MAYBE_CLASS_TYPE_P (basetype
))
1523 switch (TREE_CODE (basetype
))
1526 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1528 /* else fall through... */
1531 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1533 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1535 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1537 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1541 return (desires
& WANT_POINTER
) ? decay_conversion (expr
,
1542 tf_warning_or_error
)
1547 if ((desires
& WANT_VECTOR_OR_COMPLEX
) == 0)
1549 switch (TREE_CODE (TREE_TYPE (basetype
)))
1553 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1555 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1557 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1566 /* The code for conversions from class type is currently only used for
1567 delete expressions. Other expressions are handled by build_new_op. */
1568 if (!complete_type_or_maybe_complain (basetype
, expr
, complain
))
1569 return error_mark_node
;
1570 if (!TYPE_HAS_CONVERSION (basetype
))
1573 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1577 tree cand
= TREE_VALUE (conv
);
1578 cand
= OVL_CURRENT (cand
);
1580 if (winner
&& winner
== cand
)
1583 if (DECL_NONCONVERTING_P (cand
))
1586 if (TREE_CODE (cand
) == TEMPLATE_DECL
)
1590 error ("ambiguous default type conversion from %qT",
1592 error (" candidate conversions include %qD", cand
);
1594 return error_mark_node
;
1597 candidate
= non_reference (TREE_TYPE (TREE_TYPE (cand
)));
1599 switch (TREE_CODE (candidate
))
1603 win
= (desires
& WANT_INT
); break;
1605 win
= (desires
& WANT_ENUM
); break;
1607 win
= (desires
& WANT_FLOAT
); break;
1609 win
= (desires
& WANT_POINTER
); break;
1613 if ((desires
& WANT_VECTOR_OR_COMPLEX
) == 0)
1615 switch (TREE_CODE (TREE_TYPE (candidate
)))
1619 win
= (desires
& WANT_INT
); break;
1621 win
= (desires
& WANT_ENUM
); break;
1623 win
= (desires
& WANT_FLOAT
); break;
1639 error ("ambiguous default type conversion from %qT",
1641 error (" candidate conversions include %qD and %qD",
1644 return error_mark_node
;
1653 tree type
= non_reference (TREE_TYPE (TREE_TYPE (winner
)));
1654 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
,
1655 tf_warning_or_error
);
1661 /* Implements integral promotion (4.1) and float->double promotion. */
1664 type_promotes_to (tree type
)
1668 if (type
== error_mark_node
)
1669 return error_mark_node
;
1671 type
= TYPE_MAIN_VARIANT (type
);
1673 /* Check for promotions of target-defined types first. */
1674 promoted_type
= targetm
.promoted_type (type
);
1676 return promoted_type
;
1678 /* bool always promotes to int (not unsigned), even if it's the same
1680 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
1681 type
= integer_type_node
;
1683 /* Scoped enums don't promote, but pretend they do for backward ABI bug
1684 compatibility wrt varargs. */
1685 else if (SCOPED_ENUM_P (type
) && abi_version_at_least (6))
1688 /* Normally convert enums to int, but convert wide enums to something
1690 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1691 || type
== char16_type_node
1692 || type
== char32_type_node
1693 || type
== wchar_type_node
)
1695 int precision
= MAX (TYPE_PRECISION (type
),
1696 TYPE_PRECISION (integer_type_node
));
1697 tree totype
= c_common_type_for_size (precision
, 0);
1698 if (SCOPED_ENUM_P (type
))
1699 warning (OPT_Wabi
, "scoped enum %qT will not promote to an integral "
1700 "type in a future version of GCC", type
);
1701 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1702 type
= ENUM_UNDERLYING_TYPE (type
);
1703 if (TYPE_UNSIGNED (type
)
1704 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1705 type
= c_common_type_for_size (precision
, 1);
1709 else if (c_promoting_integer_type_p (type
))
1711 /* Retain unsignedness if really not getting bigger. */
1712 if (TYPE_UNSIGNED (type
)
1713 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1714 type
= unsigned_type_node
;
1716 type
= integer_type_node
;
1718 else if (type
== float_type_node
)
1719 type
= double_type_node
;
1724 /* The routines below this point are carefully written to conform to
1725 the standard. They use the same terminology, and follow the rules
1726 closely. Although they are used only in pt.c at the moment, they
1727 should presumably be used everywhere in the future. */
1729 /* Attempt to perform qualification conversions on EXPR to convert it
1730 to TYPE. Return the resulting expression, or error_mark_node if
1731 the conversion was impossible. */
1734 perform_qualification_conversions (tree type
, tree expr
)
1738 expr_type
= TREE_TYPE (expr
);
1740 if (same_type_p (type
, expr_type
))
1742 else if (TYPE_PTR_P (type
) && TYPE_PTR_P (expr_type
)
1743 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (expr_type
)))
1744 return build_nop (type
, expr
);
1745 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (expr_type
)
1746 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
1747 TYPE_PTRMEM_CLASS_TYPE (expr_type
))
1748 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type
),
1749 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type
)))
1750 return build_nop (type
, expr
);
1752 return error_mark_node
;