1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987-2014 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"
32 #include "stor-layout.h"
40 static tree
cp_convert_to_pointer (tree
, tree
, tsubst_flags_t
);
41 static tree
convert_to_pointer_force (tree
, tree
, tsubst_flags_t
);
42 static tree
build_type_conversion (tree
, tree
);
43 static tree
build_up_reference (tree
, tree
, int, tree
, tsubst_flags_t
);
44 static void diagnose_ref_binding (location_t
, tree
, tree
, tree
);
46 /* Change of width--truncation and extension of integers or reals--
47 is represented with NOP_EXPR. Proper functioning of many things
48 assumes that no other conversions can be NOP_EXPRs.
50 Conversion between integer and pointer is represented with CONVERT_EXPR.
51 Converting integer to real uses FLOAT_EXPR
52 and real to integer uses FIX_TRUNC_EXPR.
54 Here is a list of all the functions that assume that widening and
55 narrowing is always done with a NOP_EXPR:
56 In convert.c, convert_to_integer.
57 In c-typeck.c, build_binary_op_nodefault (boolean ops),
58 and c_common_truthvalue_conversion.
59 In expr.c: expand_expr, for operands of a MULT_EXPR.
60 In fold-const.c: fold.
61 In tree.c: get_narrower and get_unwidened.
63 C++: in multiple-inheritance, converting between pointers may involve
64 adjusting them by a delta stored within the class definition. */
66 /* Subroutines of `convert'. */
68 /* if converting pointer to pointer
69 if dealing with classes, check for derived->base or vice versa
70 else if dealing with method pointers, delegate
72 else if converting class, pass off to build_type_conversion
73 else try C-style pointer conversion. */
76 cp_convert_to_pointer (tree type
, tree expr
, tsubst_flags_t complain
)
78 tree intype
= TREE_TYPE (expr
);
81 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
83 if (intype
== error_mark_node
)
84 return error_mark_node
;
86 if (MAYBE_CLASS_TYPE_P (intype
))
88 intype
= complete_type (intype
);
89 if (!COMPLETE_TYPE_P (intype
))
91 if (complain
& tf_error
)
92 error_at (loc
, "can%'t convert from incomplete type %qT to %qT",
94 return error_mark_node
;
97 rval
= build_type_conversion (type
, expr
);
100 if ((complain
& tf_error
)
101 && rval
== error_mark_node
)
102 error_at (loc
, "conversion of %qE from %qT to %qT is ambiguous",
108 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
109 if (TYPE_PTR_P (type
)
110 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
111 || VOID_TYPE_P (TREE_TYPE (type
))))
113 if (TYPE_PTRMEMFUNC_P (intype
)
114 || TREE_CODE (intype
) == METHOD_TYPE
)
115 return convert_member_func_to_ptr (type
, expr
, complain
);
116 if (TYPE_PTR_P (TREE_TYPE (expr
)))
117 return build_nop (type
, expr
);
118 intype
= TREE_TYPE (expr
);
121 if (expr
== error_mark_node
)
122 return error_mark_node
;
124 form
= TREE_CODE (intype
);
126 if (POINTER_TYPE_P (intype
))
128 intype
= TYPE_MAIN_VARIANT (intype
);
130 if (TYPE_MAIN_VARIANT (type
) != intype
132 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
133 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type
))
134 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype
))
135 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
137 enum tree_code code
= PLUS_EXPR
;
143 intype_class
= TREE_TYPE (intype
);
144 type_class
= TREE_TYPE (type
);
146 same_p
= same_type_p (TYPE_MAIN_VARIANT (intype_class
),
147 TYPE_MAIN_VARIANT (type_class
));
149 /* Try derived to base conversion. */
151 binfo
= lookup_base (intype_class
, type_class
, ba_check
,
153 if (!same_p
&& !binfo
)
155 /* Try base to derived conversion. */
156 binfo
= lookup_base (type_class
, intype_class
, ba_check
,
160 if (binfo
== error_mark_node
)
161 return error_mark_node
;
165 expr
= build_base_path (code
, expr
, binfo
, 0, complain
);
166 /* Add any qualifier conversions. */
167 return build_nop (type
, expr
);
171 if (TYPE_PTRMEMFUNC_P (type
))
173 if (complain
& tf_error
)
174 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
176 return error_mark_node
;
179 return build_nop (type
, expr
);
181 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
182 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
183 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/false,
184 /*c_cast_p=*/false, complain
);
185 else if (TYPE_PTRMEMFUNC_P (intype
))
189 if (TREE_CODE (expr
) == PTRMEM_CST
)
190 return cp_convert_to_pointer (type
, PTRMEM_CST_MEMBER (expr
),
192 else if (TREE_CODE (expr
) == OFFSET_REF
)
194 tree object
= TREE_OPERAND (expr
, 0);
195 return get_member_function_from_ptrfunc (&object
,
196 TREE_OPERAND (expr
, 1),
200 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
202 return error_mark_node
;
205 if (null_ptr_cst_p (expr
))
207 if (TYPE_PTRMEMFUNC_P (type
))
208 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0,
209 /*c_cast_p=*/false, complain
);
211 if (complain
& tf_warning
)
212 maybe_warn_zero_as_null_pointer_constant (expr
, loc
);
214 /* A NULL pointer-to-data-member is represented by -1, not by
216 tree val
= (TYPE_PTRDATAMEM_P (type
)
217 ? build_int_cst_type (type
, -1)
218 : build_int_cst (type
, 0));
220 return (TREE_SIDE_EFFECTS (expr
)
221 ? build2 (COMPOUND_EXPR
, type
, expr
, val
) : val
);
223 else if (TYPE_PTRMEM_P (type
) && INTEGRAL_CODE_P (form
))
225 if (complain
& tf_error
)
226 error_at (loc
, "invalid conversion from %qT to %qT", intype
, type
);
227 return error_mark_node
;
230 if (INTEGRAL_CODE_P (form
))
232 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
233 return build1 (CONVERT_EXPR
, type
, expr
);
234 expr
= cp_convert (c_common_type_for_size (POINTER_SIZE
, 0), expr
,
236 /* Modes may be different but sizes should be the same. There
237 is supposed to be some integral type that is the same width
239 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
240 == GET_MODE_SIZE (TYPE_MODE (type
)));
242 return convert_to_pointer (type
, expr
);
245 if (type_unknown_p (expr
))
246 return instantiate_type (type
, expr
, complain
);
248 if (complain
& tf_error
)
249 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
251 return error_mark_node
;
254 /* Like convert, except permit conversions to take place which
255 are not normally allowed due to access restrictions
256 (such as conversion from sub-type to private super-type). */
259 convert_to_pointer_force (tree type
, tree expr
, tsubst_flags_t complain
)
261 tree intype
= TREE_TYPE (expr
);
262 enum tree_code form
= TREE_CODE (intype
);
264 if (form
== POINTER_TYPE
)
266 intype
= TYPE_MAIN_VARIANT (intype
);
268 if (TYPE_MAIN_VARIANT (type
) != intype
269 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
270 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type
))
271 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype
))
272 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
274 enum tree_code code
= PLUS_EXPR
;
277 binfo
= lookup_base (TREE_TYPE (intype
), TREE_TYPE (type
),
278 ba_unique
, NULL
, complain
);
281 binfo
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
282 ba_unique
, NULL
, complain
);
285 if (binfo
== error_mark_node
)
286 return error_mark_node
;
289 expr
= build_base_path (code
, expr
, binfo
, 0, complain
);
290 if (expr
== error_mark_node
)
291 return error_mark_node
;
292 /* Add any qualifier conversions. */
293 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr
)),
295 expr
= build_nop (type
, expr
);
301 return cp_convert_to_pointer (type
, expr
, complain
);
304 /* We are passing something to a function which requires a reference.
305 The type we are interested in is in TYPE. The initial
306 value we have to begin with is in ARG.
308 FLAGS controls how we manage access checking.
309 DIRECT_BIND in FLAGS controls how any temporaries are generated.
310 If DIRECT_BIND is set, DECL is the reference we're binding to. */
313 build_up_reference (tree type
, tree arg
, int flags
, tree decl
,
314 tsubst_flags_t complain
)
317 tree argtype
= TREE_TYPE (arg
);
318 tree target_type
= TREE_TYPE (type
);
320 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
322 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
324 /* Create a new temporary variable. We can't just use a TARGET_EXPR
325 here because it needs to live as long as DECL. */
328 arg
= make_temporary_var_for_ref_to_temp (decl
, target_type
);
330 /* Process the initializer for the declaration. */
331 DECL_INITIAL (arg
) = targ
;
332 cp_finish_decl (arg
, targ
, /*init_const_expr_p=*/false, NULL_TREE
,
333 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
335 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
336 return get_target_expr_sfinae (arg
, complain
);
338 /* If we had a way to wrap this up, and say, if we ever needed its
339 address, transform all occurrences of the register, into a memory
340 reference we could win better. */
341 rval
= cp_build_addr_expr (arg
, complain
);
342 if (rval
== error_mark_node
)
343 return error_mark_node
;
345 if ((flags
& LOOKUP_PROTECT
)
346 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
347 && MAYBE_CLASS_TYPE_P (argtype
)
348 && MAYBE_CLASS_TYPE_P (target_type
))
350 /* We go through lookup_base for the access control. */
351 tree binfo
= lookup_base (argtype
, target_type
, ba_check
,
353 if (binfo
== error_mark_node
)
354 return error_mark_node
;
355 if (binfo
== NULL_TREE
)
356 return error_not_base_type (target_type
, argtype
);
357 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1, complain
);
361 = convert_to_pointer_force (build_pointer_type (target_type
),
363 return build_nop (type
, rval
);
366 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
367 INTYPE is the original rvalue type and DECL is an optional _DECL node
370 [dcl.init.ref] says that if an rvalue is used to
371 initialize a reference, then the reference must be to a
372 non-volatile const type. */
375 diagnose_ref_binding (location_t loc
, tree reftype
, tree intype
, tree decl
)
377 tree ttl
= TREE_TYPE (reftype
);
379 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl
))
383 if (CP_TYPE_VOLATILE_P (ttl
) && decl
)
384 msg
= G_("initialization of volatile reference type %q#T from "
385 "rvalue of type %qT");
386 else if (CP_TYPE_VOLATILE_P (ttl
))
387 msg
= G_("conversion to volatile reference type %q#T "
388 "from rvalue of type %qT");
390 msg
= G_("initialization of non-const reference type %q#T from "
391 "rvalue of type %qT");
393 msg
= G_("conversion to non-const reference type %q#T from "
394 "rvalue of type %qT");
396 permerror (loc
, msg
, reftype
, intype
);
400 /* For C++: Only need to do one-level references, but cannot
401 get tripped up on signed/unsigned differences.
403 DECL is either NULL_TREE or the _DECL node for a reference that is being
404 initialized. It can be error_mark_node if we don't know the _DECL but
405 we know it's an initialization. */
408 convert_to_reference (tree reftype
, tree expr
, int convtype
,
409 int flags
, tree decl
, tsubst_flags_t complain
)
411 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
413 tree rval
= NULL_TREE
;
414 tree rval_as_conversion
= NULL_TREE
;
415 bool can_convert_intype_to_type
;
416 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
418 if (TREE_CODE (type
) == FUNCTION_TYPE
419 && TREE_TYPE (expr
) == unknown_type_node
)
420 expr
= instantiate_type (type
, expr
, complain
);
422 if (expr
== error_mark_node
)
423 return error_mark_node
;
425 intype
= TREE_TYPE (expr
);
427 gcc_assert (TREE_CODE (intype
) != REFERENCE_TYPE
);
428 gcc_assert (TREE_CODE (reftype
) == REFERENCE_TYPE
);
430 intype
= TYPE_MAIN_VARIANT (intype
);
432 can_convert_intype_to_type
= can_convert_standard (type
, intype
, complain
);
434 if (!can_convert_intype_to_type
435 && (convtype
& CONV_IMPLICIT
) && MAYBE_CLASS_TYPE_P (intype
)
436 && ! (flags
& LOOKUP_NO_CONVERSION
))
438 /* Look for a user-defined conversion to lvalue that we can use. */
441 = build_type_conversion (reftype
, expr
);
443 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
444 && real_lvalue_p (rval_as_conversion
))
446 expr
= rval_as_conversion
;
447 rval_as_conversion
= NULL_TREE
;
449 can_convert_intype_to_type
= 1;
453 if (((convtype
& CONV_STATIC
)
454 && can_convert_standard (intype
, type
, complain
))
455 || ((convtype
& CONV_IMPLICIT
) && can_convert_intype_to_type
))
458 tree ttl
= TREE_TYPE (reftype
);
459 tree ttr
= lvalue_type (expr
);
461 if ((complain
& tf_error
)
462 && ! real_lvalue_p (expr
))
463 diagnose_ref_binding (loc
, reftype
, intype
, decl
);
465 if (! (convtype
& CONV_CONST
)
466 && !at_least_as_qualified_p (ttl
, ttr
))
468 if (complain
& tf_error
)
469 permerror (loc
, "conversion from %qT to %qT discards qualifiers",
472 return error_mark_node
;
476 return build_up_reference (reftype
, expr
, flags
, decl
, complain
);
478 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
480 /* When casting an lvalue to a reference type, just convert into
481 a pointer to the new type and deference it. This is allowed
482 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
483 should be done directly (jason). (int &)ri ---> *(int*)&ri */
485 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
487 if ((complain
& tf_warning
)
488 && TYPE_PTR_P (intype
)
489 && (comptypes (TREE_TYPE (intype
), type
,
490 COMPARE_BASE
| COMPARE_DERIVED
)))
491 warning_at (loc
, 0, "casting %qT to %qT does not dereference pointer",
494 rval
= cp_build_addr_expr (expr
, complain
);
495 if (rval
!= error_mark_node
)
496 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
498 if (rval
!= error_mark_node
)
499 rval
= build1 (NOP_EXPR
, reftype
, rval
);
503 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
504 ICR_CONVERTING
, 0, 0, complain
);
505 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
507 if (complain
& tf_error
)
508 diagnose_ref_binding (loc
, reftype
, intype
, decl
);
509 rval
= build_up_reference (reftype
, rval
, flags
, decl
, complain
);
514 /* If we found a way to convert earlier, then use it. */
518 if (complain
& tf_error
)
519 error_at (loc
, "cannot convert type %qT to type %qT", intype
, reftype
);
521 return error_mark_node
;
524 /* We are using a reference VAL for its value. Bash that reference all the
525 way down to its lowest form. */
528 convert_from_reference (tree val
)
531 && TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
533 tree t
= TREE_TYPE (TREE_TYPE (val
));
534 tree ref
= build1 (INDIRECT_REF
, t
, val
);
537 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
538 so that we get the proper error message if the result is used
539 to assign to. Also, &* is supposed to be a no-op. */
540 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
541 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
542 TREE_SIDE_EFFECTS (ref
)
543 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (val
));
550 /* Really perform an lvalue-to-rvalue conversion, including copying an
551 argument of class type into a temporary. */
554 force_rvalue (tree expr
, tsubst_flags_t complain
)
556 tree type
= TREE_TYPE (expr
);
557 if (MAYBE_CLASS_TYPE_P (type
) && TREE_CODE (expr
) != TARGET_EXPR
)
559 vec
<tree
, va_gc
> *args
= make_tree_vector_single (expr
);
560 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
561 &args
, type
, LOOKUP_NORMAL
, complain
);
562 release_tree_vector (args
);
563 expr
= build_cplus_new (type
, expr
, complain
);
566 expr
= decay_conversion (expr
, complain
);
572 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
573 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
577 ignore_overflows (tree expr
, tree orig
)
579 if (TREE_CODE (expr
) == INTEGER_CST
580 && TREE_CODE (orig
) == INTEGER_CST
581 && TREE_OVERFLOW (expr
) != TREE_OVERFLOW (orig
))
583 gcc_assert (!TREE_OVERFLOW (orig
));
584 /* Ensure constant sharing. */
585 expr
= build_int_cst_wide (TREE_TYPE (expr
),
586 TREE_INT_CST_LOW (expr
),
587 TREE_INT_CST_HIGH (expr
));
592 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
596 cp_fold_convert (tree type
, tree expr
)
598 tree conv
= fold_convert (type
, expr
);
599 conv
= ignore_overflows (conv
, expr
);
603 /* C++ conversions, preference to static cast conversions. */
606 cp_convert (tree type
, tree expr
, tsubst_flags_t complain
)
608 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
, complain
);
611 /* C++ equivalent of convert_and_check but using cp_convert as the
614 Convert EXPR to TYPE, warning about conversion problems with constants.
615 Invoke this function on every expression that is converted implicitly,
616 i.e. because of language rules and not because of an explicit cast. */
619 cp_convert_and_check (tree type
, tree expr
, tsubst_flags_t complain
)
623 if (TREE_TYPE (expr
) == type
)
626 result
= cp_convert (type
, expr
, complain
);
628 if ((complain
& tf_warning
)
629 && c_inhibit_evaluation_warnings
== 0)
631 tree folded
= maybe_constant_value (expr
);
632 tree stripped
= folded
;
634 = folded
!= expr
? cp_convert (type
, folded
, complain
) : result
;
636 /* maybe_constant_value wraps an INTEGER_CST with TREE_OVERFLOW in a
637 NOP_EXPR so that it isn't TREE_CONSTANT anymore. */
638 STRIP_NOPS (stripped
);
640 if (!TREE_OVERFLOW_P (stripped
)
641 && folded_result
!= error_mark_node
)
642 warnings_for_convert_and_check (type
, folded
, folded_result
);
650 FLAGS indicates how we should behave. */
653 ocp_convert (tree type
, tree expr
, int convtype
, int flags
,
654 tsubst_flags_t complain
)
657 enum tree_code code
= TREE_CODE (type
);
658 const char *invalid_conv_diag
;
660 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
662 if (error_operand_p (e
) || type
== error_mark_node
)
663 return error_mark_node
;
665 complete_type (type
);
666 complete_type (TREE_TYPE (expr
));
668 if ((invalid_conv_diag
669 = targetm
.invalid_conversion (TREE_TYPE (expr
), type
)))
671 if (complain
& tf_error
)
672 error (invalid_conv_diag
);
673 return error_mark_node
;
676 /* FIXME remove when moving to c_fully_fold model. */
677 /* FIXME do we still need this test? */
678 if (!CLASS_TYPE_P (type
))
679 e
= integral_constant_value (e
);
680 if (error_operand_p (e
))
681 return error_mark_node
;
683 if (MAYBE_CLASS_TYPE_P (type
) && (convtype
& CONV_FORCE_TEMP
))
684 /* We need a new temporary; don't take this shortcut. */;
685 else if (same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (e
)))
687 if (same_type_p (type
, TREE_TYPE (e
)))
688 /* The call to fold will not always remove the NOP_EXPR as
689 might be expected, since if one of the types is a typedef;
690 the comparison in fold is just equality of pointers, not a
691 call to comptypes. We don't call fold in this case because
692 that can result in infinite recursion; fold will call
693 convert, which will call ocp_convert, etc. */
695 /* For complex data types, we need to perform componentwise
697 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
698 return fold_if_not_in_template (convert_to_complex (type
, e
));
699 else if (TREE_CODE (type
) == VECTOR_TYPE
)
700 return fold_if_not_in_template (convert_to_vector (type
, e
));
701 else if (TREE_CODE (e
) == TARGET_EXPR
)
703 /* Don't build a NOP_EXPR of class type. Instead, change the
704 type of the temporary. */
705 TREE_TYPE (e
) = TREE_TYPE (TARGET_EXPR_SLOT (e
)) = type
;
710 /* We shouldn't be treating objects of ADDRESSABLE type as
712 gcc_assert (!TREE_ADDRESSABLE (type
));
713 return fold_if_not_in_template (build_nop (type
, e
));
717 e1
= targetm
.convert_to_type (type
, e
);
721 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
723 e
= convert_to_void (e
, ICV_CAST
, complain
);
727 if (INTEGRAL_CODE_P (code
))
729 tree intype
= TREE_TYPE (e
);
732 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
734 /* enum = enum, enum = int, enum = float, (enum)pointer are all
736 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
737 || TREE_CODE (intype
) == REAL_TYPE
)
738 && ! (convtype
& CONV_STATIC
))
739 || TYPE_PTR_P (intype
))
741 if (complain
& tf_error
)
742 permerror (loc
, "conversion from %q#T to %q#T", intype
, type
);
744 return error_mark_node
;
747 /* [expr.static.cast]
749 8. A value of integral or enumeration type can be explicitly
750 converted to an enumeration type. The value is unchanged if
751 the original value is within the range of the enumeration
752 values. Otherwise, the resulting enumeration value is
754 if ((complain
& tf_warning
)
755 && TREE_CODE (e
) == INTEGER_CST
756 && !int_fits_type_p (e
, ENUM_UNDERLYING_TYPE (type
)))
757 warning_at (loc
, OPT_Wconversion
,
758 "the result of the conversion is unspecified because "
759 "%qE is outside the range of type %qT",
762 if (MAYBE_CLASS_TYPE_P (intype
))
765 rval
= build_type_conversion (type
, e
);
768 if (complain
& tf_error
)
769 error_at (loc
, "%q#T used where a %qT was expected", intype
, type
);
770 return error_mark_node
;
772 if (code
== BOOLEAN_TYPE
)
774 if (VOID_TYPE_P (intype
))
776 if (complain
& tf_error
)
778 "could not convert %qE from %<void%> to %<bool%>",
780 return error_mark_node
;
783 /* We can't implicitly convert a scoped enum to bool, so convert
784 to the underlying type first. */
785 if (SCOPED_ENUM_P (intype
) && (convtype
& CONV_STATIC
))
786 e
= build_nop (ENUM_UNDERLYING_TYPE (intype
), e
);
787 return cp_truthvalue_conversion (e
);
790 converted
= fold_if_not_in_template (convert_to_integer (type
, e
));
792 /* Ignore any integer overflow caused by the conversion. */
793 return ignore_overflows (converted
, e
);
795 if (NULLPTR_TYPE_P (type
) && e
&& null_ptr_cst_p (e
))
797 if (complain
& tf_warning
)
798 maybe_warn_zero_as_null_pointer_constant (e
, loc
);
801 if (POINTER_TYPE_P (type
) || TYPE_PTRMEM_P (type
))
802 return fold_if_not_in_template (cp_convert_to_pointer (type
, e
, complain
));
803 if (code
== VECTOR_TYPE
)
805 tree in_vtype
= TREE_TYPE (e
);
806 if (MAYBE_CLASS_TYPE_P (in_vtype
))
809 ret_val
= build_type_conversion (type
, e
);
812 if (complain
& tf_error
)
813 error_at (loc
, "%q#T used where a %qT was expected",
815 return error_mark_node
;
817 return fold_if_not_in_template (convert_to_vector (type
, e
));
819 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
821 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e
)))
824 rval
= build_type_conversion (type
, e
);
827 else if (complain
& tf_error
)
829 "%q#T used where a floating point value was expected",
832 if (code
== REAL_TYPE
)
833 return fold_if_not_in_template (convert_to_real (type
, e
));
834 else if (code
== COMPLEX_TYPE
)
835 return fold_if_not_in_template (convert_to_complex (type
, e
));
838 /* New C++ semantics: since assignment is now based on
839 memberwise copying, if the rhs type is derived from the
840 lhs type, then we may still do a conversion. */
841 if (RECORD_OR_UNION_CODE_P (code
))
843 tree dtype
= TREE_TYPE (e
);
844 tree ctor
= NULL_TREE
;
846 dtype
= TYPE_MAIN_VARIANT (dtype
);
848 /* Conversion between aggregate types. New C++ semantics allow
849 objects of derived type to be cast to objects of base type.
850 Old semantics only allowed this between pointers.
852 There may be some ambiguity between using a constructor
853 vs. using a type conversion operator when both apply. */
857 if (abstract_virtuals_error_sfinae (NULL_TREE
, type
, complain
))
858 return error_mark_node
;
860 if (BRACE_ENCLOSED_INITIALIZER_P (ctor
))
861 ctor
= perform_implicit_conversion (type
, ctor
, complain
);
862 else if ((flags
& LOOKUP_ONLYCONVERTING
)
863 && ! (CLASS_TYPE_P (dtype
) && DERIVED_FROM_P (type
, dtype
)))
864 /* For copy-initialization, first we create a temp of the proper type
865 with a user-defined conversion sequence, then we direct-initialize
866 the target with the temp (see [dcl.init]). */
867 ctor
= build_user_type_conversion (type
, ctor
, flags
, complain
);
870 vec
<tree
, va_gc
> *ctor_vec
= make_tree_vector_single (ctor
);
871 ctor
= build_special_member_call (NULL_TREE
,
872 complete_ctor_identifier
,
874 type
, flags
, complain
);
875 release_tree_vector (ctor_vec
);
878 return build_cplus_new (type
, ctor
, complain
);
881 if (complain
& tf_error
)
883 /* If the conversion failed and expr was an invalid use of pointer to
884 member function, try to report a meaningful error. */
885 if (invalid_nonstatic_memfn_p (expr
, complain
))
886 /* We displayed the error message. */;
888 error_at (loc
, "conversion from %qT to non-scalar type %qT requested",
889 TREE_TYPE (expr
), type
);
891 return error_mark_node
;
894 /* When an expression is used in a void context, its value is discarded and
895 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
896 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
897 in a void context. The C++ standard does not define what an `access' to an
898 object is, but there is reason to believe that it is the lvalue to rvalue
899 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
900 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
901 indicates that volatile semantics should be the same between C and C++
902 where ever possible. C leaves it implementation defined as to what
903 constitutes an access to a volatile. So, we interpret `*vp' as a read of
904 the volatile object `vp' points to, unless that is an incomplete type. For
905 volatile references we do not do this interpretation, because that would
906 make it impossible to ignore the reference return value from functions. We
907 issue warnings in the confusing cases.
909 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
910 to void via a cast. If an expression is being implicitly converted, IMPLICIT
911 indicates the context of the implicit conversion. */
914 convert_to_void (tree expr
, impl_conv_void implicit
, tsubst_flags_t complain
)
916 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
918 if (expr
== error_mark_node
919 || TREE_TYPE (expr
) == error_mark_node
)
920 return error_mark_node
;
922 if (implicit
== ICV_CAST
)
923 mark_exp_read (expr
);
928 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
929 exprv
= TREE_OPERAND (exprv
, 1);
931 || handled_component_p (exprv
)
932 || INDIRECT_REF_P (exprv
))
933 /* Expr is not being 'used' here, otherwise we whould have
934 called mark_{rl}value_use use here, which would have in turn
935 called mark_exp_read. Rather, we call mark_exp_read directly
936 to avoid some warnings when
937 -Wunused-but-set-{variable,parameter} is in effect. */
938 mark_exp_read (exprv
);
941 if (!TREE_TYPE (expr
))
943 if (invalid_nonstatic_memfn_p (expr
, complain
))
944 return error_mark_node
;
945 if (TREE_CODE (expr
) == PSEUDO_DTOR_EXPR
)
947 if (complain
& tf_error
)
948 error_at (loc
, "pseudo-destructor is not called");
949 return error_mark_node
;
951 if (VOID_TYPE_P (TREE_TYPE (expr
)))
953 switch (TREE_CODE (expr
))
957 /* The two parts of a cond expr might be separate lvalues. */
958 tree op1
= TREE_OPERAND (expr
,1);
959 tree op2
= TREE_OPERAND (expr
,2);
960 bool side_effects
= ((op1
&& TREE_SIDE_EFFECTS (op1
))
961 || TREE_SIDE_EFFECTS (op2
));
962 tree new_op1
, new_op2
;
964 if (implicit
!= ICV_CAST
&& !side_effects
)
967 new_op1
= convert_to_void (op1
, ICV_SECOND_OF_COND
, complain
);
968 new_op2
= convert_to_void (op2
, ICV_THIRD_OF_COND
, complain
);
973 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
974 new_op2
= convert_to_void (op2
, ICV_CAST
, complain
);
977 expr
= build3 (COND_EXPR
, TREE_TYPE (new_op2
),
978 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
984 /* The second part of a compound expr contains the value. */
985 tree op1
= TREE_OPERAND (expr
,1);
987 if (implicit
!= ICV_CAST
&& !TREE_NO_WARNING (expr
))
988 new_op1
= convert_to_void (op1
, ICV_RIGHT_OF_COMMA
, complain
);
990 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
994 tree t
= build2 (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
995 TREE_OPERAND (expr
, 0), new_op1
);
1002 case NON_LVALUE_EXPR
:
1004 /* These have already decayed to rvalue. */
1007 case CALL_EXPR
: /* We have a special meaning for volatile void fn(). */
1012 tree type
= TREE_TYPE (expr
);
1013 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
1015 int is_volatile
= TYPE_VOLATILE (type
);
1016 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
1018 /* Can't load the value if we don't know the type. */
1019 if (is_volatile
&& !is_complete
)
1021 if (complain
& tf_warning
)
1025 warning_at (loc
, 0, "conversion to void will not access "
1026 "object of incomplete type %qT", type
);
1028 case ICV_SECOND_OF_COND
:
1029 warning_at (loc
, 0, "indirection will not access object of "
1030 "incomplete type %qT in second operand "
1031 "of conditional expression", type
);
1033 case ICV_THIRD_OF_COND
:
1034 warning_at (loc
, 0, "indirection will not access object of "
1035 "incomplete type %qT in third operand "
1036 "of conditional expression", type
);
1038 case ICV_RIGHT_OF_COMMA
:
1039 warning_at (loc
, 0, "indirection will not access object of "
1040 "incomplete type %qT in right operand of "
1041 "comma operator", type
);
1043 case ICV_LEFT_OF_COMMA
:
1044 warning_at (loc
, 0, "indirection will not access object of "
1045 "incomplete type %qT in left operand of "
1046 "comma operator", type
);
1049 warning_at (loc
, 0, "indirection will not access object of "
1050 "incomplete type %qT in statement", type
);
1052 case ICV_THIRD_IN_FOR
:
1053 warning_at (loc
, 0, "indirection will not access object of "
1054 "incomplete type %qT in for increment "
1055 "expression", type
);
1061 /* Don't load the value if this is an implicit dereference, or if
1062 the type needs to be handled by ctors/dtors. */
1063 else if (is_volatile
&& is_reference
)
1065 if (complain
& tf_warning
)
1069 warning_at (loc
, 0, "conversion to void will not access "
1070 "object of type %qT", type
);
1072 case ICV_SECOND_OF_COND
:
1073 warning_at (loc
, 0, "implicit dereference will not access "
1074 "object of type %qT in second operand of "
1075 "conditional expression", type
);
1077 case ICV_THIRD_OF_COND
:
1078 warning_at (loc
, 0, "implicit dereference will not access "
1079 "object of type %qT in third operand of "
1080 "conditional expression", type
);
1082 case ICV_RIGHT_OF_COMMA
:
1083 warning_at (loc
, 0, "implicit dereference will not access "
1084 "object of type %qT in right operand of "
1085 "comma operator", type
);
1087 case ICV_LEFT_OF_COMMA
:
1088 warning_at (loc
, 0, "implicit dereference will not access "
1089 "object of type %qT in left operand of comma "
1093 warning_at (loc
, 0, "implicit dereference will not access "
1094 "object of type %qT in statement", type
);
1096 case ICV_THIRD_IN_FOR
:
1097 warning_at (loc
, 0, "implicit dereference will not access "
1098 "object of type %qT in for increment expression",
1105 else if (is_volatile
&& TREE_ADDRESSABLE (type
))
1107 if (complain
& tf_warning
)
1111 warning_at (loc
, 0, "conversion to void will not access "
1112 "object of non-trivially-copyable type %qT",
1115 case ICV_SECOND_OF_COND
:
1116 warning_at (loc
, 0, "indirection will not access object of "
1117 "non-trivially-copyable type %qT in second "
1118 "operand of conditional expression", type
);
1120 case ICV_THIRD_OF_COND
:
1121 warning_at (loc
, 0, "indirection will not access object of "
1122 "non-trivially-copyable type %qT in third "
1123 "operand of conditional expression", type
);
1125 case ICV_RIGHT_OF_COMMA
:
1126 warning_at (loc
, 0, "indirection will not access object of "
1127 "non-trivially-copyable type %qT in right "
1128 "operand of comma operator", type
);
1130 case ICV_LEFT_OF_COMMA
:
1131 warning_at (loc
, 0, "indirection will not access object of "
1132 "non-trivially-copyable type %qT in left "
1133 "operand of comma operator", type
);
1136 warning_at (loc
, 0, "indirection will not access object of "
1137 "non-trivially-copyable type %qT in statement",
1140 case ICV_THIRD_IN_FOR
:
1141 warning_at (loc
, 0, "indirection will not access object of "
1142 "non-trivially-copyable type %qT in for "
1143 "increment expression", type
);
1149 if (is_reference
|| !is_volatile
|| !is_complete
|| TREE_ADDRESSABLE (type
))
1151 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1152 operation is stripped off. Note that we don't warn about
1153 - an expression with TREE_NO_WARNING set. (For an example of
1154 such expressions, see build_over_call in call.c.)
1155 - automatic dereferencing of references, since the user cannot
1156 control it. (See also warn_if_unused_value() in c-common.c.) */
1157 if (warn_unused_value
1158 && implicit
!= ICV_CAST
1159 && (complain
& tf_warning
)
1160 && !TREE_NO_WARNING (expr
)
1162 warning_at (loc
, OPT_Wunused_value
, "value computed is not used");
1163 expr
= TREE_OPERAND (expr
, 0);
1171 /* External variables might be incomplete. */
1172 tree type
= TREE_TYPE (expr
);
1173 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
1175 if (TYPE_VOLATILE (type
) && !is_complete
&& (complain
& tf_warning
))
1179 warning_at (loc
, 0, "conversion to void will not access "
1180 "object %qE of incomplete type %qT", expr
, type
);
1182 case ICV_SECOND_OF_COND
:
1183 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1184 "not be accessed in second operand of "
1185 "conditional expression", expr
, type
);
1187 case ICV_THIRD_OF_COND
:
1188 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1189 "not be accessed in third operand of "
1190 "conditional expression", expr
, type
);
1192 case ICV_RIGHT_OF_COMMA
:
1193 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1194 "not be accessed in right operand of comma operator",
1197 case ICV_LEFT_OF_COMMA
:
1198 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1199 "not be accessed in left operand of comma operator",
1203 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1204 "not be accessed in statement", expr
, type
);
1206 case ICV_THIRD_IN_FOR
:
1207 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1208 "not be accessed in for increment expression",
1219 /* Don't bother with the temporary object returned from a function if
1220 we don't use it and don't need to destroy it. We'll still
1221 allocate space for it in expand_call or declare_return_variable,
1222 but we don't need to track it through all the tree phases. */
1223 if (TARGET_EXPR_IMPLICIT_P (expr
)
1224 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr
)))
1226 tree init
= TARGET_EXPR_INITIAL (expr
);
1227 if (TREE_CODE (init
) == AGGR_INIT_EXPR
1228 && !AGGR_INIT_VIA_CTOR_P (init
))
1230 tree fn
= AGGR_INIT_EXPR_FN (init
);
1231 expr
= build_call_array_loc (input_location
,
1232 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
))),
1234 aggr_init_expr_nargs (init
),
1235 AGGR_INIT_EXPR_ARGP (init
));
1242 expr
= resolve_nondeduced_context (expr
);
1246 if (TREE_CODE (probe
) == ADDR_EXPR
)
1247 probe
= TREE_OPERAND (expr
, 0);
1248 if (type_unknown_p (probe
))
1250 /* [over.over] enumerates the places where we can take the address
1251 of an overloaded function, and this is not one of them. */
1252 if (complain
& tf_error
)
1256 error_at (loc
, "conversion to void "
1257 "cannot resolve address of overloaded function");
1259 case ICV_SECOND_OF_COND
:
1260 error_at (loc
, "second operand of conditional expression "
1261 "cannot resolve address of overloaded function");
1263 case ICV_THIRD_OF_COND
:
1264 error_at (loc
, "third operand of conditional expression "
1265 "cannot resolve address of overloaded function");
1267 case ICV_RIGHT_OF_COMMA
:
1268 error_at (loc
, "right operand of comma operator "
1269 "cannot resolve address of overloaded function");
1271 case ICV_LEFT_OF_COMMA
:
1272 error_at (loc
, "left operand of comma operator "
1273 "cannot resolve address of overloaded function");
1276 error_at (loc
, "statement "
1277 "cannot resolve address of overloaded function");
1279 case ICV_THIRD_IN_FOR
:
1280 error_at (loc
, "for increment expression "
1281 "cannot resolve address of overloaded function");
1285 return error_mark_node
;
1286 expr
= void_zero_node
;
1288 else if (implicit
!= ICV_CAST
&& probe
== expr
&& is_overloaded_fn (probe
))
1290 /* Only warn when there is no &. */
1291 if (complain
& tf_warning
)
1294 case ICV_SECOND_OF_COND
:
1295 warning_at (loc
, OPT_Waddress
,
1296 "second operand of conditional expression "
1297 "is a reference, not call, to function %qE", expr
);
1299 case ICV_THIRD_OF_COND
:
1300 warning_at (loc
, OPT_Waddress
,
1301 "third operand of conditional expression "
1302 "is a reference, not call, to function %qE", expr
);
1304 case ICV_RIGHT_OF_COMMA
:
1305 warning_at (loc
, OPT_Waddress
,
1306 "right operand of comma operator "
1307 "is a reference, not call, to function %qE", expr
);
1309 case ICV_LEFT_OF_COMMA
:
1310 warning_at (loc
, OPT_Waddress
,
1311 "left operand of comma operator "
1312 "is a reference, not call, to function %qE", expr
);
1315 warning_at (loc
, OPT_Waddress
,
1316 "statement is a reference, not call, to function %qE",
1319 case ICV_THIRD_IN_FOR
:
1320 warning_at (loc
, OPT_Waddress
,
1321 "for increment expression "
1322 "is a reference, not call, to function %qE", expr
);
1328 if (TREE_CODE (expr
) == COMPONENT_REF
)
1329 expr
= TREE_OPERAND (expr
, 0);
1333 if (expr
!= error_mark_node
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
1335 if (implicit
!= ICV_CAST
1336 && warn_unused_value
1337 && !TREE_NO_WARNING (expr
)
1338 && !processing_template_decl
)
1340 /* The middle end does not warn about expressions that have
1341 been explicitly cast to void, so we must do so here. */
1342 if (!TREE_SIDE_EFFECTS (expr
)) {
1343 if (complain
& tf_warning
)
1346 case ICV_SECOND_OF_COND
:
1347 warning_at (loc
, OPT_Wunused_value
,
1348 "second operand of conditional expression "
1351 case ICV_THIRD_OF_COND
:
1352 warning_at (loc
, OPT_Wunused_value
,
1353 "third operand of conditional expression "
1356 case ICV_RIGHT_OF_COMMA
:
1357 warning_at (loc
, OPT_Wunused_value
,
1358 "right operand of comma operator has no effect");
1360 case ICV_LEFT_OF_COMMA
:
1361 warning_at (loc
, OPT_Wunused_value
,
1362 "left operand of comma operator has no effect");
1365 warning_at (loc
, OPT_Wunused_value
,
1366 "statement has no effect");
1368 case ICV_THIRD_IN_FOR
:
1369 warning_at (loc
, OPT_Wunused_value
,
1370 "for increment expression has no effect");
1379 enum tree_code code
;
1380 enum tree_code_class tclass
;
1383 /* We might like to warn about (say) "(int) f()", as the
1384 cast has no effect, but the compiler itself will
1385 generate implicit conversions under some
1386 circumstances. (For example a block copy will be
1387 turned into a call to "__builtin_memcpy", with a
1388 conversion of the return value to an appropriate
1389 type.) So, to avoid false positives, we strip
1390 conversions. Do not use STRIP_NOPs because it will
1391 not strip conversions to "void", as that is not a
1392 mode-preserving conversion. */
1393 while (TREE_CODE (e
) == NOP_EXPR
)
1394 e
= TREE_OPERAND (e
, 0);
1396 code
= TREE_CODE (e
);
1397 tclass
= TREE_CODE_CLASS (code
);
1398 if ((tclass
== tcc_comparison
1399 || tclass
== tcc_unary
1400 || (tclass
== tcc_binary
1401 && !(code
== MODIFY_EXPR
1402 || code
== INIT_EXPR
1403 || code
== PREDECREMENT_EXPR
1404 || code
== PREINCREMENT_EXPR
1405 || code
== POSTDECREMENT_EXPR
1406 || code
== POSTINCREMENT_EXPR
))
1407 || code
== VEC_PERM_EXPR
1408 || code
== VEC_COND_EXPR
)
1409 && (complain
& tf_warning
))
1410 warning_at (loc
, OPT_Wunused_value
, "value computed is not used");
1413 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
1415 if (! TREE_SIDE_EFFECTS (expr
))
1416 expr
= void_zero_node
;
1420 /* Create an expression whose value is that of EXPR,
1421 converted to type TYPE. The TREE_TYPE of the value
1422 is always TYPE. This function implements all reasonable
1423 conversions; callers should filter out those that are
1424 not permitted by the language being compiled.
1426 Most of this routine is from build_reinterpret_cast.
1428 The back end cannot call cp_convert (what was convert) because
1429 conversions to/from basetypes may involve memory references
1430 (vbases) and adding or subtracting small values (multiple
1431 inheritance), but it calls convert from the constant folding code
1432 on subtrees of already built trees after it has ripped them apart.
1434 Also, if we ever support range variables, we'll probably also have to
1435 do a little bit more work. */
1438 convert (tree type
, tree expr
)
1442 if (type
== error_mark_node
|| expr
== error_mark_node
)
1443 return error_mark_node
;
1445 intype
= TREE_TYPE (expr
);
1447 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
1448 return fold_if_not_in_template (build_nop (type
, expr
));
1450 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
1451 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
,
1452 tf_warning_or_error
);
1455 /* Like cp_convert, except permit conversions to take place which
1456 are not normally allowed due to access restrictions
1457 (such as conversion from sub-type to private super-type). */
1460 convert_force (tree type
, tree expr
, int convtype
, tsubst_flags_t complain
)
1463 enum tree_code code
= TREE_CODE (type
);
1465 if (code
== REFERENCE_TYPE
)
1466 return (fold_if_not_in_template
1467 (convert_to_reference (type
, e
, CONV_C_CAST
, 0,
1468 NULL_TREE
, complain
)));
1470 if (code
== POINTER_TYPE
)
1471 return fold_if_not_in_template (convert_to_pointer_force (type
, e
,
1474 /* From typeck.c convert_for_assignment */
1475 if (((TYPE_PTR_P (TREE_TYPE (e
)) && TREE_CODE (e
) == ADDR_EXPR
1476 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1477 || integer_zerop (e
)
1478 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1479 && TYPE_PTRMEMFUNC_P (type
))
1480 /* compatible pointer to member functions. */
1481 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1,
1482 /*c_cast_p=*/1, complain
);
1484 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
, complain
);
1487 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1488 exists, return the attempted conversion. This may
1489 return ERROR_MARK_NODE if the conversion is not
1490 allowed (references private members, etc).
1491 If no conversion exists, NULL_TREE is returned.
1493 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1494 object parameter, or by the second standard conversion sequence if
1495 that doesn't do it. This will probably wait for an overloading rewrite.
1499 build_type_conversion (tree xtype
, tree expr
)
1501 /* C++: check to see if we can convert this aggregate type
1502 into the required type. */
1503 return build_user_type_conversion (xtype
, expr
, LOOKUP_NORMAL
,
1504 tf_warning_or_error
);
1507 /* Convert the given EXPR to one of a group of types suitable for use in an
1508 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1509 which indicates which types are suitable. If COMPLAIN is true, complain
1510 about ambiguity; otherwise, the caller will deal with it. */
1513 build_expr_type_conversion (int desires
, tree expr
, bool complain
)
1515 tree basetype
= TREE_TYPE (expr
);
1516 tree conv
= NULL_TREE
;
1517 tree winner
= NULL_TREE
;
1519 if (expr
== null_node
1520 && (desires
& WANT_INT
)
1521 && !(desires
& WANT_NULL
))
1523 source_location loc
=
1524 expansion_point_location_if_in_system_header (input_location
);
1526 warning_at (loc
, OPT_Wconversion_null
,
1527 "converting NULL to non-pointer type");
1530 if (basetype
== error_mark_node
)
1531 return error_mark_node
;
1533 if (! MAYBE_CLASS_TYPE_P (basetype
))
1534 switch (TREE_CODE (basetype
))
1537 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1539 /* else fall through... */
1542 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1544 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1546 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1548 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1552 return (desires
& WANT_POINTER
) ? decay_conversion (expr
,
1553 tf_warning_or_error
)
1558 if ((desires
& WANT_VECTOR_OR_COMPLEX
) == 0)
1560 switch (TREE_CODE (TREE_TYPE (basetype
)))
1564 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1566 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1568 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1577 /* The code for conversions from class type is currently only used for
1578 delete expressions. Other expressions are handled by build_new_op. */
1579 if (!complete_type_or_maybe_complain (basetype
, expr
, complain
))
1580 return error_mark_node
;
1581 if (!TYPE_HAS_CONVERSION (basetype
))
1584 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1588 tree cand
= TREE_VALUE (conv
);
1589 cand
= OVL_CURRENT (cand
);
1591 if (winner
&& winner
== cand
)
1594 if (DECL_NONCONVERTING_P (cand
))
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;
1630 /* A wildcard could be instantiated to match any desired
1631 type, but we can't deduce the template argument. */
1632 if (WILDCARD_TYPE_P (candidate
))
1639 if (TREE_CODE (cand
) == TEMPLATE_DECL
)
1642 error ("default type conversion can't deduce template"
1643 " argument for %qD", cand
);
1644 return error_mark_node
;
1650 = non_reference (TREE_TYPE (TREE_TYPE (winner
)));
1652 if (!same_type_ignoring_top_level_qualifiers_p (winner_type
,
1657 error ("ambiguous default type conversion from %qT",
1659 error (" candidate conversions include %qD and %qD",
1662 return error_mark_node
;
1672 tree type
= non_reference (TREE_TYPE (TREE_TYPE (winner
)));
1673 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
,
1674 tf_warning_or_error
);
1680 /* Implements integral promotion (4.1) and float->double promotion. */
1683 type_promotes_to (tree type
)
1687 if (type
== error_mark_node
)
1688 return error_mark_node
;
1690 type
= TYPE_MAIN_VARIANT (type
);
1692 /* Check for promotions of target-defined types first. */
1693 promoted_type
= targetm
.promoted_type (type
);
1695 return promoted_type
;
1697 /* bool always promotes to int (not unsigned), even if it's the same
1699 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
1700 type
= integer_type_node
;
1702 /* Scoped enums don't promote, but pretend they do for backward ABI bug
1703 compatibility wrt varargs. */
1704 else if (SCOPED_ENUM_P (type
) && abi_version_at_least (6))
1707 /* Normally convert enums to int, but convert wide enums to something
1709 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1710 || type
== char16_type_node
1711 || type
== char32_type_node
1712 || type
== wchar_type_node
)
1714 int precision
= MAX (TYPE_PRECISION (type
),
1715 TYPE_PRECISION (integer_type_node
));
1716 tree totype
= c_common_type_for_size (precision
, 0);
1717 if (SCOPED_ENUM_P (type
))
1718 warning (OPT_Wabi
, "scoped enum %qT will not promote to an integral "
1719 "type in a future version of GCC", type
);
1720 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1721 type
= ENUM_UNDERLYING_TYPE (type
);
1722 if (TYPE_UNSIGNED (type
)
1723 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1724 type
= c_common_type_for_size (precision
, 1);
1728 else if (c_promoting_integer_type_p (type
))
1730 /* Retain unsignedness if really not getting bigger. */
1731 if (TYPE_UNSIGNED (type
)
1732 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1733 type
= unsigned_type_node
;
1735 type
= integer_type_node
;
1737 else if (type
== float_type_node
)
1738 type
= double_type_node
;
1743 /* The routines below this point are carefully written to conform to
1744 the standard. They use the same terminology, and follow the rules
1745 closely. Although they are used only in pt.c at the moment, they
1746 should presumably be used everywhere in the future. */
1748 /* Attempt to perform qualification conversions on EXPR to convert it
1749 to TYPE. Return the resulting expression, or error_mark_node if
1750 the conversion was impossible. */
1753 perform_qualification_conversions (tree type
, tree expr
)
1757 expr_type
= TREE_TYPE (expr
);
1759 if (same_type_p (type
, expr_type
))
1761 else if (TYPE_PTR_P (type
) && TYPE_PTR_P (expr_type
)
1762 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (expr_type
)))
1763 return build_nop (type
, expr
);
1764 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (expr_type
)
1765 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
1766 TYPE_PTRMEM_CLASS_TYPE (expr_type
))
1767 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type
),
1768 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type
)))
1769 return build_nop (type
, expr
);
1771 return error_mark_node
;