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"
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 diagnose_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_LOC (expr
, input_location
);
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 (TYPE_PTR_P (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 (TYPE_PTR_P (TREE_TYPE (expr
)))
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
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 (TYPE_PTRMEMFUNC_P (type
))
209 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0,
210 /*c_cast_p=*/false, complain
);
212 if (complain
& tf_warning
)
213 maybe_warn_zero_as_null_pointer_constant (expr
, loc
);
215 /* A NULL pointer-to-data-member is represented by -1, not by
217 tree val
= (TYPE_PTRDATAMEM_P (type
)
218 ? build_int_cst_type (type
, -1)
219 : build_int_cst (type
, 0));
221 return (TREE_SIDE_EFFECTS (expr
)
222 ? build2 (COMPOUND_EXPR
, type
, expr
, val
) : val
);
224 else if (TYPE_PTRMEM_P (type
) && INTEGRAL_CODE_P (form
))
226 if (complain
& tf_error
)
227 error_at (loc
, "invalid conversion from %qT to %qT", intype
, type
);
228 return error_mark_node
;
231 if (INTEGRAL_CODE_P (form
))
233 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
234 return build1 (CONVERT_EXPR
, type
, expr
);
235 expr
= cp_convert (c_common_type_for_size (POINTER_SIZE
, 0), expr
,
237 /* Modes may be different but sizes should be the same. There
238 is supposed to be some integral type that is the same width
240 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
241 == GET_MODE_SIZE (TYPE_MODE (type
)));
243 return convert_to_pointer (type
, expr
);
246 if (type_unknown_p (expr
))
247 return instantiate_type (type
, expr
, complain
);
249 if (complain
& tf_error
)
250 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
252 return error_mark_node
;
255 /* Like convert, except permit conversions to take place which
256 are not normally allowed due to access restrictions
257 (such as conversion from sub-type to private super-type). */
260 convert_to_pointer_force (tree type
, tree expr
, tsubst_flags_t complain
)
262 tree intype
= TREE_TYPE (expr
);
263 enum tree_code form
= TREE_CODE (intype
);
265 if (form
== POINTER_TYPE
)
267 intype
= TYPE_MAIN_VARIANT (intype
);
269 if (TYPE_MAIN_VARIANT (type
) != intype
270 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
271 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type
))
272 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype
))
273 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
275 enum tree_code code
= PLUS_EXPR
;
278 binfo
= lookup_base (TREE_TYPE (intype
), TREE_TYPE (type
),
279 ba_unique
, NULL
, complain
);
282 binfo
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
283 ba_unique
, NULL
, complain
);
286 if (binfo
== error_mark_node
)
287 return error_mark_node
;
290 expr
= build_base_path (code
, expr
, binfo
, 0, complain
);
291 if (expr
== error_mark_node
)
292 return error_mark_node
;
293 /* Add any qualifier conversions. */
294 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr
)),
296 expr
= build_nop (type
, expr
);
302 return cp_convert_to_pointer (type
, expr
, complain
);
305 /* We are passing something to a function which requires a reference.
306 The type we are interested in is in TYPE. The initial
307 value we have to begin with is in ARG.
309 FLAGS controls how we manage access checking.
310 DIRECT_BIND in FLAGS controls how any temporaries are generated.
311 If DIRECT_BIND is set, DECL is the reference we're binding to. */
314 build_up_reference (tree type
, tree arg
, int flags
, tree decl
,
315 tsubst_flags_t complain
)
318 tree argtype
= TREE_TYPE (arg
);
319 tree target_type
= TREE_TYPE (type
);
321 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
323 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
325 /* Create a new temporary variable. We can't just use a TARGET_EXPR
326 here because it needs to live as long as DECL. */
329 arg
= make_temporary_var_for_ref_to_temp (decl
, target_type
);
331 /* Process the initializer for the declaration. */
332 DECL_INITIAL (arg
) = targ
;
333 cp_finish_decl (arg
, targ
, /*init_const_expr_p=*/false, NULL_TREE
,
334 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
336 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
337 return get_target_expr_sfinae (arg
, complain
);
339 /* If we had a way to wrap this up, and say, if we ever needed its
340 address, transform all occurrences of the register, into a memory
341 reference we could win better. */
342 rval
= cp_build_addr_expr (arg
, complain
);
343 if (rval
== error_mark_node
)
344 return error_mark_node
;
346 if ((flags
& LOOKUP_PROTECT
)
347 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
348 && MAYBE_CLASS_TYPE_P (argtype
)
349 && MAYBE_CLASS_TYPE_P (target_type
))
351 /* We go through lookup_base for the access control. */
352 tree binfo
= lookup_base (argtype
, target_type
, ba_check
,
354 if (binfo
== error_mark_node
)
355 return error_mark_node
;
356 if (binfo
== NULL_TREE
)
357 return error_not_base_type (target_type
, argtype
);
358 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1, complain
);
362 = convert_to_pointer_force (build_pointer_type (target_type
),
364 return build_nop (type
, rval
);
367 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
368 INTYPE is the original rvalue type and DECL is an optional _DECL node
371 [dcl.init.ref] says that if an rvalue is used to
372 initialize a reference, then the reference must be to a
373 non-volatile const type. */
376 diagnose_ref_binding (location_t loc
, tree reftype
, tree intype
, tree decl
)
378 tree ttl
= TREE_TYPE (reftype
);
380 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl
))
384 if (CP_TYPE_VOLATILE_P (ttl
) && decl
)
385 msg
= G_("initialization of volatile reference type %q#T from "
386 "rvalue of type %qT");
387 else if (CP_TYPE_VOLATILE_P (ttl
))
388 msg
= G_("conversion to volatile reference type %q#T "
389 "from rvalue of type %qT");
391 msg
= G_("initialization of non-const reference type %q#T from "
392 "rvalue of type %qT");
394 msg
= G_("conversion to non-const reference type %q#T from "
395 "rvalue of type %qT");
397 permerror (loc
, msg
, reftype
, intype
);
401 /* For C++: Only need to do one-level references, but cannot
402 get tripped up on signed/unsigned differences.
404 DECL is either NULL_TREE or the _DECL node for a reference that is being
405 initialized. It can be error_mark_node if we don't know the _DECL but
406 we know it's an initialization. */
409 convert_to_reference (tree reftype
, tree expr
, int convtype
,
410 int flags
, tree decl
, tsubst_flags_t complain
)
412 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
414 tree rval
= NULL_TREE
;
415 tree rval_as_conversion
= NULL_TREE
;
416 bool can_convert_intype_to_type
;
417 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
419 if (TREE_CODE (type
) == FUNCTION_TYPE
420 && TREE_TYPE (expr
) == unknown_type_node
)
421 expr
= instantiate_type (type
, expr
, complain
);
423 if (expr
== error_mark_node
)
424 return error_mark_node
;
426 intype
= TREE_TYPE (expr
);
428 gcc_assert (TREE_CODE (intype
) != REFERENCE_TYPE
);
429 gcc_assert (TREE_CODE (reftype
) == REFERENCE_TYPE
);
431 intype
= TYPE_MAIN_VARIANT (intype
);
433 can_convert_intype_to_type
= can_convert_standard (type
, intype
, complain
);
435 if (!can_convert_intype_to_type
436 && (convtype
& CONV_IMPLICIT
) && MAYBE_CLASS_TYPE_P (intype
)
437 && ! (flags
& LOOKUP_NO_CONVERSION
))
439 /* Look for a user-defined conversion to lvalue that we can use. */
442 = build_type_conversion (reftype
, expr
);
444 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
445 && real_lvalue_p (rval_as_conversion
))
447 expr
= rval_as_conversion
;
448 rval_as_conversion
= NULL_TREE
;
450 can_convert_intype_to_type
= 1;
454 if (((convtype
& CONV_STATIC
)
455 && can_convert_standard (intype
, type
, complain
))
456 || ((convtype
& CONV_IMPLICIT
) && can_convert_intype_to_type
))
459 tree ttl
= TREE_TYPE (reftype
);
460 tree ttr
= lvalue_type (expr
);
462 if ((complain
& tf_error
)
463 && ! real_lvalue_p (expr
))
464 diagnose_ref_binding (loc
, reftype
, intype
, decl
);
466 if (! (convtype
& CONV_CONST
)
467 && !at_least_as_qualified_p (ttl
, ttr
))
469 if (complain
& tf_error
)
470 permerror (loc
, "conversion from %qT to %qT discards qualifiers",
473 return error_mark_node
;
477 return build_up_reference (reftype
, expr
, flags
, decl
, complain
);
479 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
481 /* When casting an lvalue to a reference type, just convert into
482 a pointer to the new type and deference it. This is allowed
483 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
484 should be done directly (jason). (int &)ri ---> *(int*)&ri */
486 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
488 if ((complain
& tf_warning
)
489 && TYPE_PTR_P (intype
)
490 && (comptypes (TREE_TYPE (intype
), type
,
491 COMPARE_BASE
| COMPARE_DERIVED
)))
492 warning_at (loc
, 0, "casting %qT to %qT does not dereference pointer",
495 rval
= cp_build_addr_expr (expr
, complain
);
496 if (rval
!= error_mark_node
)
497 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
499 if (rval
!= error_mark_node
)
500 rval
= build1 (NOP_EXPR
, reftype
, rval
);
504 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
505 ICR_CONVERTING
, 0, 0, complain
);
506 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
508 if (complain
& tf_error
)
509 diagnose_ref_binding (loc
, reftype
, intype
, decl
);
510 rval
= build_up_reference (reftype
, rval
, flags
, decl
, complain
);
515 /* If we found a way to convert earlier, then use it. */
519 if (complain
& tf_error
)
520 error_at (loc
, "cannot convert type %qT to type %qT", intype
, reftype
);
522 return error_mark_node
;
525 /* We are using a reference VAL for its value. Bash that reference all the
526 way down to its lowest form. */
529 convert_from_reference (tree val
)
532 && TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
534 tree t
= TREE_TYPE (TREE_TYPE (val
));
535 tree ref
= build1 (INDIRECT_REF
, t
, val
);
538 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
539 so that we get the proper error message if the result is used
540 to assign to. Also, &* is supposed to be a no-op. */
541 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
542 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
543 TREE_SIDE_EFFECTS (ref
)
544 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (val
));
551 /* Really perform an lvalue-to-rvalue conversion, including copying an
552 argument of class type into a temporary. */
555 force_rvalue (tree expr
, tsubst_flags_t complain
)
557 tree type
= TREE_TYPE (expr
);
558 if (MAYBE_CLASS_TYPE_P (type
) && TREE_CODE (expr
) != TARGET_EXPR
)
560 vec
<tree
, va_gc
> *args
= make_tree_vector_single (expr
);
561 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
562 &args
, type
, LOOKUP_NORMAL
, complain
);
563 release_tree_vector (args
);
564 expr
= build_cplus_new (type
, expr
, complain
);
567 expr
= decay_conversion (expr
, complain
);
573 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
574 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
578 ignore_overflows (tree expr
, tree orig
)
580 if (TREE_CODE (expr
) == INTEGER_CST
581 && TREE_CODE (orig
) == INTEGER_CST
582 && TREE_OVERFLOW (expr
) != TREE_OVERFLOW (orig
))
584 gcc_assert (!TREE_OVERFLOW (orig
));
585 /* Ensure constant sharing. */
586 expr
= wide_int_to_tree (TREE_TYPE (expr
), expr
);
591 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
595 cp_fold_convert (tree type
, tree expr
)
597 tree conv
= fold_convert (type
, expr
);
598 conv
= ignore_overflows (conv
, expr
);
602 /* C++ conversions, preference to static cast conversions. */
605 cp_convert (tree type
, tree expr
, tsubst_flags_t complain
)
607 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
, complain
);
610 /* C++ equivalent of convert_and_check but using cp_convert as the
613 Convert EXPR to TYPE, warning about conversion problems with constants.
614 Invoke this function on every expression that is converted implicitly,
615 i.e. because of language rules and not because of an explicit cast. */
618 cp_convert_and_check (tree type
, tree expr
, tsubst_flags_t complain
)
622 if (TREE_TYPE (expr
) == type
)
625 result
= cp_convert (type
, expr
, complain
);
627 if ((complain
& tf_warning
)
628 && c_inhibit_evaluation_warnings
== 0)
630 tree folded
= maybe_constant_value (expr
);
631 tree stripped
= folded
;
633 = folded
!= expr
? cp_convert (type
, folded
, complain
) : result
;
635 /* maybe_constant_value wraps an INTEGER_CST with TREE_OVERFLOW in a
636 NOP_EXPR so that it isn't TREE_CONSTANT anymore. */
637 STRIP_NOPS (stripped
);
639 if (!TREE_OVERFLOW_P (stripped
)
640 && folded_result
!= error_mark_node
)
641 warnings_for_convert_and_check (input_location
, type
, folded
,
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 && ENUM_UNDERLYING_TYPE (type
)
757 && !int_fits_type_p (e
, ENUM_UNDERLYING_TYPE (type
)))
758 warning_at (loc
, OPT_Wconversion
,
759 "the result of the conversion is unspecified because "
760 "%qE is outside the range of type %qT",
763 if (MAYBE_CLASS_TYPE_P (intype
))
766 rval
= build_type_conversion (type
, e
);
769 if (complain
& tf_error
)
770 error_at (loc
, "%q#T used where a %qT was expected", intype
, type
);
771 return error_mark_node
;
773 if (code
== BOOLEAN_TYPE
)
775 if (VOID_TYPE_P (intype
))
777 if (complain
& tf_error
)
779 "could not convert %qE from %<void%> to %<bool%>",
781 return error_mark_node
;
784 /* We can't implicitly convert a scoped enum to bool, so convert
785 to the underlying type first. */
786 if (SCOPED_ENUM_P (intype
) && (convtype
& CONV_STATIC
))
787 e
= build_nop (ENUM_UNDERLYING_TYPE (intype
), e
);
788 return cp_truthvalue_conversion (e
);
791 converted
= fold_if_not_in_template (convert_to_integer (type
, e
));
793 /* Ignore any integer overflow caused by the conversion. */
794 return ignore_overflows (converted
, e
);
796 if (NULLPTR_TYPE_P (type
) && e
&& null_ptr_cst_p (e
))
798 if (complain
& tf_warning
)
799 maybe_warn_zero_as_null_pointer_constant (e
, loc
);
802 if (POINTER_TYPE_P (type
) || TYPE_PTRMEM_P (type
))
803 return fold_if_not_in_template (cp_convert_to_pointer (type
, e
, complain
));
804 if (code
== VECTOR_TYPE
)
806 tree in_vtype
= TREE_TYPE (e
);
807 if (MAYBE_CLASS_TYPE_P (in_vtype
))
810 ret_val
= build_type_conversion (type
, e
);
813 if (complain
& tf_error
)
814 error_at (loc
, "%q#T used where a %qT was expected",
816 return error_mark_node
;
818 return fold_if_not_in_template (convert_to_vector (type
, e
));
820 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
822 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e
)))
825 rval
= build_type_conversion (type
, e
);
828 else if (complain
& tf_error
)
830 "%q#T used where a floating point value was expected",
833 if (code
== REAL_TYPE
)
834 return fold_if_not_in_template (convert_to_real (type
, e
));
835 else if (code
== COMPLEX_TYPE
)
836 return fold_if_not_in_template (convert_to_complex (type
, e
));
839 /* New C++ semantics: since assignment is now based on
840 memberwise copying, if the rhs type is derived from the
841 lhs type, then we may still do a conversion. */
842 if (RECORD_OR_UNION_CODE_P (code
))
844 tree dtype
= TREE_TYPE (e
);
845 tree ctor
= NULL_TREE
;
847 dtype
= TYPE_MAIN_VARIANT (dtype
);
849 /* Conversion between aggregate types. New C++ semantics allow
850 objects of derived type to be cast to objects of base type.
851 Old semantics only allowed this between pointers.
853 There may be some ambiguity between using a constructor
854 vs. using a type conversion operator when both apply. */
858 if (abstract_virtuals_error_sfinae (NULL_TREE
, type
, complain
))
859 return error_mark_node
;
861 if (BRACE_ENCLOSED_INITIALIZER_P (ctor
))
862 ctor
= perform_implicit_conversion (type
, ctor
, complain
);
863 else if ((flags
& LOOKUP_ONLYCONVERTING
)
864 && ! (CLASS_TYPE_P (dtype
) && DERIVED_FROM_P (type
, dtype
)))
865 /* For copy-initialization, first we create a temp of the proper type
866 with a user-defined conversion sequence, then we direct-initialize
867 the target with the temp (see [dcl.init]). */
868 ctor
= build_user_type_conversion (type
, ctor
, flags
, complain
);
871 vec
<tree
, va_gc
> *ctor_vec
= make_tree_vector_single (ctor
);
872 ctor
= build_special_member_call (NULL_TREE
,
873 complete_ctor_identifier
,
875 type
, flags
, complain
);
876 release_tree_vector (ctor_vec
);
879 return build_cplus_new (type
, ctor
, complain
);
882 if (complain
& tf_error
)
884 /* If the conversion failed and expr was an invalid use of pointer to
885 member function, try to report a meaningful error. */
886 if (invalid_nonstatic_memfn_p (expr
, complain
))
887 /* We displayed the error message. */;
889 error_at (loc
, "conversion from %qT to non-scalar type %qT requested",
890 TREE_TYPE (expr
), type
);
892 return error_mark_node
;
895 /* When an expression is used in a void context, its value is discarded and
896 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
897 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
898 in a void context. The C++ standard does not define what an `access' to an
899 object is, but there is reason to believe that it is the lvalue to rvalue
900 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
901 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
902 indicates that volatile semantics should be the same between C and C++
903 where ever possible. C leaves it implementation defined as to what
904 constitutes an access to a volatile. So, we interpret `*vp' as a read of
905 the volatile object `vp' points to, unless that is an incomplete type. For
906 volatile references we do not do this interpretation, because that would
907 make it impossible to ignore the reference return value from functions. We
908 issue warnings in the confusing cases.
910 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
911 to void via a cast. If an expression is being implicitly converted, IMPLICIT
912 indicates the context of the implicit conversion. */
915 convert_to_void (tree expr
, impl_conv_void implicit
, tsubst_flags_t complain
)
917 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
919 if (expr
== error_mark_node
920 || TREE_TYPE (expr
) == error_mark_node
)
921 return error_mark_node
;
923 if (implicit
== ICV_CAST
)
924 mark_exp_read (expr
);
929 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
930 exprv
= TREE_OPERAND (exprv
, 1);
932 || handled_component_p (exprv
)
933 || INDIRECT_REF_P (exprv
))
934 /* Expr is not being 'used' here, otherwise we whould have
935 called mark_{rl}value_use use here, which would have in turn
936 called mark_exp_read. Rather, we call mark_exp_read directly
937 to avoid some warnings when
938 -Wunused-but-set-{variable,parameter} is in effect. */
939 mark_exp_read (exprv
);
942 if (!TREE_TYPE (expr
))
944 if (invalid_nonstatic_memfn_p (expr
, complain
))
945 return error_mark_node
;
946 if (TREE_CODE (expr
) == PSEUDO_DTOR_EXPR
)
948 if (complain
& tf_error
)
949 error_at (loc
, "pseudo-destructor is not called");
950 return error_mark_node
;
952 if (VOID_TYPE_P (TREE_TYPE (expr
)))
954 switch (TREE_CODE (expr
))
958 /* The two parts of a cond expr might be separate lvalues. */
959 tree op1
= TREE_OPERAND (expr
,1);
960 tree op2
= TREE_OPERAND (expr
,2);
961 bool side_effects
= ((op1
&& TREE_SIDE_EFFECTS (op1
))
962 || TREE_SIDE_EFFECTS (op2
));
963 tree new_op1
, new_op2
;
965 if (implicit
!= ICV_CAST
&& !side_effects
)
968 new_op1
= convert_to_void (op1
, ICV_SECOND_OF_COND
, complain
);
969 new_op2
= convert_to_void (op2
, ICV_THIRD_OF_COND
, complain
);
974 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
975 new_op2
= convert_to_void (op2
, ICV_CAST
, complain
);
978 expr
= build3 (COND_EXPR
, TREE_TYPE (new_op2
),
979 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
985 /* The second part of a compound expr contains the value. */
986 tree op1
= TREE_OPERAND (expr
,1);
988 if (implicit
!= ICV_CAST
&& !TREE_NO_WARNING (expr
))
989 new_op1
= convert_to_void (op1
, ICV_RIGHT_OF_COMMA
, complain
);
991 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
995 tree t
= build2 (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
996 TREE_OPERAND (expr
, 0), new_op1
);
1003 case NON_LVALUE_EXPR
:
1005 /* These have already decayed to rvalue. */
1008 case CALL_EXPR
: /* We have a special meaning for volatile void fn(). */
1013 tree type
= TREE_TYPE (expr
);
1014 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
1016 int is_volatile
= TYPE_VOLATILE (type
);
1017 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
1019 /* Can't load the value if we don't know the type. */
1020 if (is_volatile
&& !is_complete
)
1022 if (complain
& tf_warning
)
1026 warning_at (loc
, 0, "conversion to void will not access "
1027 "object of incomplete type %qT", type
);
1029 case ICV_SECOND_OF_COND
:
1030 warning_at (loc
, 0, "indirection will not access object of "
1031 "incomplete type %qT in second operand "
1032 "of conditional expression", type
);
1034 case ICV_THIRD_OF_COND
:
1035 warning_at (loc
, 0, "indirection will not access object of "
1036 "incomplete type %qT in third operand "
1037 "of conditional expression", type
);
1039 case ICV_RIGHT_OF_COMMA
:
1040 warning_at (loc
, 0, "indirection will not access object of "
1041 "incomplete type %qT in right operand of "
1042 "comma operator", type
);
1044 case ICV_LEFT_OF_COMMA
:
1045 warning_at (loc
, 0, "indirection will not access object of "
1046 "incomplete type %qT in left operand of "
1047 "comma operator", type
);
1050 warning_at (loc
, 0, "indirection will not access object of "
1051 "incomplete type %qT in statement", type
);
1053 case ICV_THIRD_IN_FOR
:
1054 warning_at (loc
, 0, "indirection will not access object of "
1055 "incomplete type %qT in for increment "
1056 "expression", type
);
1062 /* Don't load the value if this is an implicit dereference, or if
1063 the type needs to be handled by ctors/dtors. */
1064 else if (is_volatile
&& is_reference
)
1066 if (complain
& tf_warning
)
1070 warning_at (loc
, 0, "conversion to void will not access "
1071 "object of type %qT", type
);
1073 case ICV_SECOND_OF_COND
:
1074 warning_at (loc
, 0, "implicit dereference will not access "
1075 "object of type %qT in second operand of "
1076 "conditional expression", type
);
1078 case ICV_THIRD_OF_COND
:
1079 warning_at (loc
, 0, "implicit dereference will not access "
1080 "object of type %qT in third operand of "
1081 "conditional expression", type
);
1083 case ICV_RIGHT_OF_COMMA
:
1084 warning_at (loc
, 0, "implicit dereference will not access "
1085 "object of type %qT in right operand of "
1086 "comma operator", type
);
1088 case ICV_LEFT_OF_COMMA
:
1089 warning_at (loc
, 0, "implicit dereference will not access "
1090 "object of type %qT in left operand of comma "
1094 warning_at (loc
, 0, "implicit dereference will not access "
1095 "object of type %qT in statement", type
);
1097 case ICV_THIRD_IN_FOR
:
1098 warning_at (loc
, 0, "implicit dereference will not access "
1099 "object of type %qT in for increment expression",
1106 else if (is_volatile
&& TREE_ADDRESSABLE (type
))
1108 if (complain
& tf_warning
)
1112 warning_at (loc
, 0, "conversion to void will not access "
1113 "object of non-trivially-copyable type %qT",
1116 case ICV_SECOND_OF_COND
:
1117 warning_at (loc
, 0, "indirection will not access object of "
1118 "non-trivially-copyable type %qT in second "
1119 "operand of conditional expression", type
);
1121 case ICV_THIRD_OF_COND
:
1122 warning_at (loc
, 0, "indirection will not access object of "
1123 "non-trivially-copyable type %qT in third "
1124 "operand of conditional expression", type
);
1126 case ICV_RIGHT_OF_COMMA
:
1127 warning_at (loc
, 0, "indirection will not access object of "
1128 "non-trivially-copyable type %qT in right "
1129 "operand of comma operator", type
);
1131 case ICV_LEFT_OF_COMMA
:
1132 warning_at (loc
, 0, "indirection will not access object of "
1133 "non-trivially-copyable type %qT in left "
1134 "operand of comma operator", type
);
1137 warning_at (loc
, 0, "indirection will not access object of "
1138 "non-trivially-copyable type %qT in statement",
1141 case ICV_THIRD_IN_FOR
:
1142 warning_at (loc
, 0, "indirection will not access object of "
1143 "non-trivially-copyable type %qT in for "
1144 "increment expression", type
);
1150 if (is_reference
|| !is_volatile
|| !is_complete
|| TREE_ADDRESSABLE (type
))
1152 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1153 operation is stripped off. Note that we don't warn about
1154 - an expression with TREE_NO_WARNING set. (For an example of
1155 such expressions, see build_over_call in call.c.)
1156 - automatic dereferencing of references, since the user cannot
1157 control it. (See also warn_if_unused_value() in c-common.c.) */
1158 if (warn_unused_value
1159 && implicit
!= ICV_CAST
1160 && (complain
& tf_warning
)
1161 && !TREE_NO_WARNING (expr
)
1163 warning_at (loc
, OPT_Wunused_value
, "value computed is not used");
1164 expr
= TREE_OPERAND (expr
, 0);
1172 /* External variables might be incomplete. */
1173 tree type
= TREE_TYPE (expr
);
1174 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
1176 if (TYPE_VOLATILE (type
) && !is_complete
&& (complain
& tf_warning
))
1180 warning_at (loc
, 0, "conversion to void will not access "
1181 "object %qE of incomplete type %qT", expr
, type
);
1183 case ICV_SECOND_OF_COND
:
1184 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1185 "not be accessed in second operand of "
1186 "conditional expression", expr
, type
);
1188 case ICV_THIRD_OF_COND
:
1189 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1190 "not be accessed in third operand of "
1191 "conditional expression", expr
, type
);
1193 case ICV_RIGHT_OF_COMMA
:
1194 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1195 "not be accessed in right operand of comma operator",
1198 case ICV_LEFT_OF_COMMA
:
1199 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1200 "not be accessed in left operand of comma operator",
1204 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1205 "not be accessed in statement", expr
, type
);
1207 case ICV_THIRD_IN_FOR
:
1208 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1209 "not be accessed in for increment expression",
1220 /* Don't bother with the temporary object returned from a function if
1221 we don't use it and don't need to destroy it. We'll still
1222 allocate space for it in expand_call or declare_return_variable,
1223 but we don't need to track it through all the tree phases. */
1224 if (TARGET_EXPR_IMPLICIT_P (expr
)
1225 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr
)))
1227 tree init
= TARGET_EXPR_INITIAL (expr
);
1228 if (TREE_CODE (init
) == AGGR_INIT_EXPR
1229 && !AGGR_INIT_VIA_CTOR_P (init
))
1231 tree fn
= AGGR_INIT_EXPR_FN (init
);
1232 expr
= build_call_array_loc (input_location
,
1233 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
))),
1235 aggr_init_expr_nargs (init
),
1236 AGGR_INIT_EXPR_ARGP (init
));
1243 expr
= resolve_nondeduced_context (expr
);
1247 if (TREE_CODE (probe
) == ADDR_EXPR
)
1248 probe
= TREE_OPERAND (expr
, 0);
1249 if (type_unknown_p (probe
))
1251 /* [over.over] enumerates the places where we can take the address
1252 of an overloaded function, and this is not one of them. */
1253 if (complain
& tf_error
)
1257 error_at (loc
, "conversion to void "
1258 "cannot resolve address of overloaded function");
1260 case ICV_SECOND_OF_COND
:
1261 error_at (loc
, "second operand of conditional expression "
1262 "cannot resolve address of overloaded function");
1264 case ICV_THIRD_OF_COND
:
1265 error_at (loc
, "third operand of conditional expression "
1266 "cannot resolve address of overloaded function");
1268 case ICV_RIGHT_OF_COMMA
:
1269 error_at (loc
, "right operand of comma operator "
1270 "cannot resolve address of overloaded function");
1272 case ICV_LEFT_OF_COMMA
:
1273 error_at (loc
, "left operand of comma operator "
1274 "cannot resolve address of overloaded function");
1277 error_at (loc
, "statement "
1278 "cannot resolve address of overloaded function");
1280 case ICV_THIRD_IN_FOR
:
1281 error_at (loc
, "for increment expression "
1282 "cannot resolve address of overloaded function");
1286 return error_mark_node
;
1287 expr
= void_zero_node
;
1289 else if (implicit
!= ICV_CAST
&& probe
== expr
&& is_overloaded_fn (probe
))
1291 /* Only warn when there is no &. */
1292 if (complain
& tf_warning
)
1295 case ICV_SECOND_OF_COND
:
1296 warning_at (loc
, OPT_Waddress
,
1297 "second operand of conditional expression "
1298 "is a reference, not call, to function %qE", expr
);
1300 case ICV_THIRD_OF_COND
:
1301 warning_at (loc
, OPT_Waddress
,
1302 "third operand of conditional expression "
1303 "is a reference, not call, to function %qE", expr
);
1305 case ICV_RIGHT_OF_COMMA
:
1306 warning_at (loc
, OPT_Waddress
,
1307 "right operand of comma operator "
1308 "is a reference, not call, to function %qE", expr
);
1310 case ICV_LEFT_OF_COMMA
:
1311 warning_at (loc
, OPT_Waddress
,
1312 "left operand of comma operator "
1313 "is a reference, not call, to function %qE", expr
);
1316 warning_at (loc
, OPT_Waddress
,
1317 "statement is a reference, not call, to function %qE",
1320 case ICV_THIRD_IN_FOR
:
1321 warning_at (loc
, OPT_Waddress
,
1322 "for increment expression "
1323 "is a reference, not call, to function %qE", expr
);
1329 if (TREE_CODE (expr
) == COMPONENT_REF
)
1330 expr
= TREE_OPERAND (expr
, 0);
1334 if (expr
!= error_mark_node
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
1336 if (implicit
!= ICV_CAST
1337 && warn_unused_value
1338 && !TREE_NO_WARNING (expr
)
1339 && !processing_template_decl
)
1341 /* The middle end does not warn about expressions that have
1342 been explicitly cast to void, so we must do so here. */
1343 if (!TREE_SIDE_EFFECTS (expr
)) {
1344 if (complain
& tf_warning
)
1347 case ICV_SECOND_OF_COND
:
1348 warning_at (loc
, OPT_Wunused_value
,
1349 "second operand of conditional expression "
1352 case ICV_THIRD_OF_COND
:
1353 warning_at (loc
, OPT_Wunused_value
,
1354 "third operand of conditional expression "
1357 case ICV_RIGHT_OF_COMMA
:
1358 warning_at (loc
, OPT_Wunused_value
,
1359 "right operand of comma operator has no effect");
1361 case ICV_LEFT_OF_COMMA
:
1362 warning_at (loc
, OPT_Wunused_value
,
1363 "left operand of comma operator has no effect");
1366 warning_at (loc
, OPT_Wunused_value
,
1367 "statement has no effect");
1369 case ICV_THIRD_IN_FOR
:
1370 warning_at (loc
, OPT_Wunused_value
,
1371 "for increment expression has no effect");
1380 enum tree_code code
;
1381 enum tree_code_class tclass
;
1384 /* We might like to warn about (say) "(int) f()", as the
1385 cast has no effect, but the compiler itself will
1386 generate implicit conversions under some
1387 circumstances. (For example a block copy will be
1388 turned into a call to "__builtin_memcpy", with a
1389 conversion of the return value to an appropriate
1390 type.) So, to avoid false positives, we strip
1391 conversions. Do not use STRIP_NOPs because it will
1392 not strip conversions to "void", as that is not a
1393 mode-preserving conversion. */
1394 while (TREE_CODE (e
) == NOP_EXPR
)
1395 e
= TREE_OPERAND (e
, 0);
1397 code
= TREE_CODE (e
);
1398 tclass
= TREE_CODE_CLASS (code
);
1399 if ((tclass
== tcc_comparison
1400 || tclass
== tcc_unary
1401 || (tclass
== tcc_binary
1402 && !(code
== MODIFY_EXPR
1403 || code
== INIT_EXPR
1404 || code
== PREDECREMENT_EXPR
1405 || code
== PREINCREMENT_EXPR
1406 || code
== POSTDECREMENT_EXPR
1407 || code
== POSTINCREMENT_EXPR
))
1408 || code
== VEC_PERM_EXPR
1409 || code
== VEC_COND_EXPR
)
1410 && (complain
& tf_warning
))
1411 warning_at (loc
, OPT_Wunused_value
, "value computed is not used");
1414 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
1416 if (! TREE_SIDE_EFFECTS (expr
))
1417 expr
= void_zero_node
;
1421 /* Create an expression whose value is that of EXPR,
1422 converted to type TYPE. The TREE_TYPE of the value
1423 is always TYPE. This function implements all reasonable
1424 conversions; callers should filter out those that are
1425 not permitted by the language being compiled.
1427 Most of this routine is from build_reinterpret_cast.
1429 The back end cannot call cp_convert (what was convert) because
1430 conversions to/from basetypes may involve memory references
1431 (vbases) and adding or subtracting small values (multiple
1432 inheritance), but it calls convert from the constant folding code
1433 on subtrees of already built trees after it has ripped them apart.
1435 Also, if we ever support range variables, we'll probably also have to
1436 do a little bit more work. */
1439 convert (tree type
, tree expr
)
1443 if (type
== error_mark_node
|| expr
== error_mark_node
)
1444 return error_mark_node
;
1446 intype
= TREE_TYPE (expr
);
1448 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
1449 return fold_if_not_in_template (build_nop (type
, expr
));
1451 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
1452 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
,
1453 tf_warning_or_error
);
1456 /* Like cp_convert, except permit conversions to take place which
1457 are not normally allowed due to access restrictions
1458 (such as conversion from sub-type to private super-type). */
1461 convert_force (tree type
, tree expr
, int convtype
, tsubst_flags_t complain
)
1464 enum tree_code code
= TREE_CODE (type
);
1466 if (code
== REFERENCE_TYPE
)
1467 return (fold_if_not_in_template
1468 (convert_to_reference (type
, e
, CONV_C_CAST
, 0,
1469 NULL_TREE
, complain
)));
1471 if (code
== POINTER_TYPE
)
1472 return fold_if_not_in_template (convert_to_pointer_force (type
, e
,
1475 /* From typeck.c convert_for_assignment */
1476 if (((TYPE_PTR_P (TREE_TYPE (e
)) && TREE_CODE (e
) == ADDR_EXPR
1477 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1478 || integer_zerop (e
)
1479 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1480 && TYPE_PTRMEMFUNC_P (type
))
1481 /* compatible pointer to member functions. */
1482 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1,
1483 /*c_cast_p=*/1, complain
);
1485 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
, complain
);
1488 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1489 exists, return the attempted conversion. This may
1490 return ERROR_MARK_NODE if the conversion is not
1491 allowed (references private members, etc).
1492 If no conversion exists, NULL_TREE is returned.
1494 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1495 object parameter, or by the second standard conversion sequence if
1496 that doesn't do it. This will probably wait for an overloading rewrite.
1500 build_type_conversion (tree xtype
, tree expr
)
1502 /* C++: check to see if we can convert this aggregate type
1503 into the required type. */
1504 return build_user_type_conversion (xtype
, expr
, LOOKUP_NORMAL
,
1505 tf_warning_or_error
);
1508 /* Convert the given EXPR to one of a group of types suitable for use in an
1509 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1510 which indicates which types are suitable. If COMPLAIN is true, complain
1511 about ambiguity; otherwise, the caller will deal with it. */
1514 build_expr_type_conversion (int desires
, tree expr
, bool complain
)
1516 tree basetype
= TREE_TYPE (expr
);
1517 tree conv
= NULL_TREE
;
1518 tree winner
= NULL_TREE
;
1520 if (expr
== null_node
1521 && (desires
& WANT_INT
)
1522 && !(desires
& WANT_NULL
))
1524 source_location loc
=
1525 expansion_point_location_if_in_system_header (input_location
);
1527 warning_at (loc
, OPT_Wconversion_null
,
1528 "converting NULL to non-pointer type");
1531 if (basetype
== error_mark_node
)
1532 return error_mark_node
;
1534 if (! MAYBE_CLASS_TYPE_P (basetype
))
1535 switch (TREE_CODE (basetype
))
1538 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1540 /* else fall through... */
1543 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1545 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1547 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1549 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1553 return (desires
& WANT_POINTER
) ? decay_conversion (expr
,
1554 tf_warning_or_error
)
1559 if ((desires
& WANT_VECTOR_OR_COMPLEX
) == 0)
1561 switch (TREE_CODE (TREE_TYPE (basetype
)))
1565 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1567 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1569 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1578 /* The code for conversions from class type is currently only used for
1579 delete expressions. Other expressions are handled by build_new_op. */
1580 if (!complete_type_or_maybe_complain (basetype
, expr
, complain
))
1581 return error_mark_node
;
1582 if (!TYPE_HAS_CONVERSION (basetype
))
1585 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1589 tree cand
= TREE_VALUE (conv
);
1590 cand
= OVL_CURRENT (cand
);
1592 if (winner
&& winner
== cand
)
1595 if (DECL_NONCONVERTING_P (cand
))
1598 candidate
= non_reference (TREE_TYPE (TREE_TYPE (cand
)));
1600 switch (TREE_CODE (candidate
))
1604 win
= (desires
& WANT_INT
); break;
1606 win
= (desires
& WANT_ENUM
); break;
1608 win
= (desires
& WANT_FLOAT
); break;
1610 win
= (desires
& WANT_POINTER
); break;
1614 if ((desires
& WANT_VECTOR_OR_COMPLEX
) == 0)
1616 switch (TREE_CODE (TREE_TYPE (candidate
)))
1620 win
= (desires
& WANT_INT
); break;
1622 win
= (desires
& WANT_ENUM
); break;
1624 win
= (desires
& WANT_FLOAT
); break;
1631 /* A wildcard could be instantiated to match any desired
1632 type, but we can't deduce the template argument. */
1633 if (WILDCARD_TYPE_P (candidate
))
1640 if (TREE_CODE (cand
) == TEMPLATE_DECL
)
1643 error ("default type conversion can't deduce template"
1644 " argument for %qD", cand
);
1645 return error_mark_node
;
1651 = non_reference (TREE_TYPE (TREE_TYPE (winner
)));
1653 if (!same_type_ignoring_top_level_qualifiers_p (winner_type
,
1658 error ("ambiguous default type conversion from %qT",
1660 error (" candidate conversions include %qD and %qD",
1663 return error_mark_node
;
1673 tree type
= non_reference (TREE_TYPE (TREE_TYPE (winner
)));
1674 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
,
1675 tf_warning_or_error
);
1681 /* Implements integral promotion (4.1) and float->double promotion. */
1684 type_promotes_to (tree type
)
1688 if (type
== error_mark_node
)
1689 return error_mark_node
;
1691 type
= TYPE_MAIN_VARIANT (type
);
1693 /* Check for promotions of target-defined types first. */
1694 promoted_type
= targetm
.promoted_type (type
);
1696 return promoted_type
;
1698 /* bool always promotes to int (not unsigned), even if it's the same
1700 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
1701 type
= integer_type_node
;
1703 /* Scoped enums don't promote, but pretend they do for backward ABI bug
1704 compatibility wrt varargs. */
1705 else if (SCOPED_ENUM_P (type
) && abi_version_at_least (6))
1708 /* Normally convert enums to int, but convert wide enums to something
1710 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1711 || type
== char16_type_node
1712 || type
== char32_type_node
1713 || type
== wchar_type_node
)
1715 int precision
= MAX (TYPE_PRECISION (type
),
1716 TYPE_PRECISION (integer_type_node
));
1717 tree totype
= c_common_type_for_size (precision
, 0);
1718 if (SCOPED_ENUM_P (type
))
1719 warning (OPT_Wabi
, "scoped enum %qT will not promote to an integral "
1720 "type in a future version of GCC", type
);
1721 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1722 type
= ENUM_UNDERLYING_TYPE (type
);
1723 if (TYPE_UNSIGNED (type
)
1724 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1725 type
= c_common_type_for_size (precision
, 1);
1729 else if (c_promoting_integer_type_p (type
))
1731 /* Retain unsignedness if really not getting bigger. */
1732 if (TYPE_UNSIGNED (type
)
1733 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1734 type
= unsigned_type_node
;
1736 type
= integer_type_node
;
1738 else if (type
== float_type_node
)
1739 type
= double_type_node
;
1744 /* The routines below this point are carefully written to conform to
1745 the standard. They use the same terminology, and follow the rules
1746 closely. Although they are used only in pt.c at the moment, they
1747 should presumably be used everywhere in the future. */
1749 /* Attempt to perform qualification conversions on EXPR to convert it
1750 to TYPE. Return the resulting expression, or error_mark_node if
1751 the conversion was impossible. */
1754 perform_qualification_conversions (tree type
, tree expr
)
1758 expr_type
= TREE_TYPE (expr
);
1760 if (same_type_p (type
, expr_type
))
1762 else if (TYPE_PTR_P (type
) && TYPE_PTR_P (expr_type
)
1763 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (expr_type
)))
1764 return build_nop (type
, expr
);
1765 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (expr_type
)
1766 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
1767 TYPE_PTRMEM_CLASS_TYPE (expr_type
))
1768 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type
),
1769 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type
)))
1770 return build_nop (type
, expr
);
1772 return error_mark_node
;