1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987-2013 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"
39 static tree
cp_convert_to_pointer (tree
, tree
, tsubst_flags_t
);
40 static tree
convert_to_pointer_force (tree
, tree
, tsubst_flags_t
);
41 static tree
build_type_conversion (tree
, tree
);
42 static tree
build_up_reference (tree
, tree
, int, tree
, tsubst_flags_t
);
43 static void diagnose_ref_binding (location_t
, tree
, tree
, tree
);
45 /* Change of width--truncation and extension of integers or reals--
46 is represented with NOP_EXPR. Proper functioning of many things
47 assumes that no other conversions can be NOP_EXPRs.
49 Conversion between integer and pointer is represented with CONVERT_EXPR.
50 Converting integer to real uses FLOAT_EXPR
51 and real to integer uses FIX_TRUNC_EXPR.
53 Here is a list of all the functions that assume that widening and
54 narrowing is always done with a NOP_EXPR:
55 In convert.c, convert_to_integer.
56 In c-typeck.c, build_binary_op_nodefault (boolean ops),
57 and c_common_truthvalue_conversion.
58 In expr.c: expand_expr, for operands of a MULT_EXPR.
59 In fold-const.c: fold.
60 In tree.c: get_narrower and get_unwidened.
62 C++: in multiple-inheritance, converting between pointers may involve
63 adjusting them by a delta stored within the class definition. */
65 /* Subroutines of `convert'. */
67 /* if converting pointer to pointer
68 if dealing with classes, check for derived->base or vice versa
69 else if dealing with method pointers, delegate
71 else if converting class, pass off to build_type_conversion
72 else try C-style pointer conversion. */
75 cp_convert_to_pointer (tree type
, tree expr
, tsubst_flags_t complain
)
77 tree intype
= TREE_TYPE (expr
);
80 location_t loc
= EXPR_LOC_OR_HERE (expr
);
82 if (intype
== error_mark_node
)
83 return error_mark_node
;
85 if (MAYBE_CLASS_TYPE_P (intype
))
87 intype
= complete_type (intype
);
88 if (!COMPLETE_TYPE_P (intype
))
90 if (complain
& tf_error
)
91 error_at (loc
, "can%'t convert from incomplete type %qT to %qT",
93 return error_mark_node
;
96 rval
= build_type_conversion (type
, expr
);
99 if ((complain
& tf_error
)
100 && rval
== error_mark_node
)
101 error_at (loc
, "conversion of %qE from %qT to %qT is ambiguous",
107 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
108 if (TYPE_PTR_P (type
)
109 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
110 || VOID_TYPE_P (TREE_TYPE (type
))))
112 if (TYPE_PTRMEMFUNC_P (intype
)
113 || TREE_CODE (intype
) == METHOD_TYPE
)
114 return convert_member_func_to_ptr (type
, expr
, complain
);
115 if (TYPE_PTR_P (TREE_TYPE (expr
)))
116 return build_nop (type
, expr
);
117 intype
= TREE_TYPE (expr
);
120 if (expr
== error_mark_node
)
121 return error_mark_node
;
123 form
= TREE_CODE (intype
);
125 if (POINTER_TYPE_P (intype
))
127 intype
= TYPE_MAIN_VARIANT (intype
);
129 if (TYPE_MAIN_VARIANT (type
) != intype
131 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
132 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type
))
133 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype
))
134 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
136 enum tree_code code
= PLUS_EXPR
;
142 intype_class
= TREE_TYPE (intype
);
143 type_class
= TREE_TYPE (type
);
145 same_p
= same_type_p (TYPE_MAIN_VARIANT (intype_class
),
146 TYPE_MAIN_VARIANT (type_class
));
148 /* Try derived to base conversion. */
150 binfo
= lookup_base (intype_class
, type_class
, ba_check
,
152 if (!same_p
&& !binfo
)
154 /* Try base to derived conversion. */
155 binfo
= lookup_base (type_class
, intype_class
, ba_check
,
159 if (binfo
== error_mark_node
)
160 return error_mark_node
;
164 expr
= build_base_path (code
, expr
, binfo
, 0, complain
);
165 /* Add any qualifier conversions. */
166 return build_nop (type
, expr
);
170 if (TYPE_PTRMEMFUNC_P (type
))
172 if (complain
& tf_error
)
173 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
175 return error_mark_node
;
178 return build_nop (type
, expr
);
180 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
181 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
182 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/false,
183 /*c_cast_p=*/false, complain
);
184 else if (TYPE_PTRMEMFUNC_P (intype
))
188 if (TREE_CODE (expr
) == PTRMEM_CST
)
189 return cp_convert_to_pointer (type
, PTRMEM_CST_MEMBER (expr
),
191 else if (TREE_CODE (expr
) == OFFSET_REF
)
193 tree object
= TREE_OPERAND (expr
, 0);
194 return get_member_function_from_ptrfunc (&object
,
195 TREE_OPERAND (expr
, 1),
199 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
201 return error_mark_node
;
204 if (null_ptr_cst_p (expr
))
206 if (complain
& tf_warning
)
207 maybe_warn_zero_as_null_pointer_constant (expr
, loc
);
209 if (TYPE_PTRMEMFUNC_P (type
))
210 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0,
211 /*c_cast_p=*/false, complain
);
213 /* A NULL pointer-to-data-member is represented by -1, not by
215 tree val
= (TYPE_PTRDATAMEM_P (type
)
216 ? build_int_cst_type (type
, -1)
217 : build_int_cst (type
, 0));
219 return (TREE_SIDE_EFFECTS (expr
)
220 ? build2 (COMPOUND_EXPR
, type
, expr
, val
) : val
);
222 else if (TYPE_PTRMEM_P (type
) && INTEGRAL_CODE_P (form
))
224 if (complain
& tf_error
)
225 error_at (loc
, "invalid conversion from %qT to %qT", intype
, type
);
226 return error_mark_node
;
229 if (INTEGRAL_CODE_P (form
))
231 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
232 return build1 (CONVERT_EXPR
, type
, expr
);
233 expr
= cp_convert (c_common_type_for_size (POINTER_SIZE
, 0), expr
,
235 /* Modes may be different but sizes should be the same. There
236 is supposed to be some integral type that is the same width
238 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
239 == GET_MODE_SIZE (TYPE_MODE (type
)));
241 return convert_to_pointer (type
, expr
);
244 if (type_unknown_p (expr
))
245 return instantiate_type (type
, expr
, complain
);
247 if (complain
& tf_error
)
248 error_at (loc
, "cannot convert %qE from type %qT to type %qT",
250 return error_mark_node
;
253 /* Like convert, except permit conversions to take place which
254 are not normally allowed due to access restrictions
255 (such as conversion from sub-type to private super-type). */
258 convert_to_pointer_force (tree type
, tree expr
, tsubst_flags_t complain
)
260 tree intype
= TREE_TYPE (expr
);
261 enum tree_code form
= TREE_CODE (intype
);
263 if (form
== POINTER_TYPE
)
265 intype
= TYPE_MAIN_VARIANT (intype
);
267 if (TYPE_MAIN_VARIANT (type
) != intype
268 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
269 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type
))
270 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype
))
271 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
273 enum tree_code code
= PLUS_EXPR
;
276 binfo
= lookup_base (TREE_TYPE (intype
), TREE_TYPE (type
),
277 ba_unique
, NULL
, complain
);
280 binfo
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
281 ba_unique
, NULL
, complain
);
284 if (binfo
== error_mark_node
)
285 return error_mark_node
;
288 expr
= build_base_path (code
, expr
, binfo
, 0, complain
);
289 if (expr
== error_mark_node
)
290 return error_mark_node
;
291 /* Add any qualifier conversions. */
292 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr
)),
294 expr
= build_nop (type
, expr
);
300 return cp_convert_to_pointer (type
, expr
, complain
);
303 /* We are passing something to a function which requires a reference.
304 The type we are interested in is in TYPE. The initial
305 value we have to begin with is in ARG.
307 FLAGS controls how we manage access checking.
308 DIRECT_BIND in FLAGS controls how any temporaries are generated.
309 If DIRECT_BIND is set, DECL is the reference we're binding to. */
312 build_up_reference (tree type
, tree arg
, int flags
, tree decl
,
313 tsubst_flags_t complain
)
316 tree argtype
= TREE_TYPE (arg
);
317 tree target_type
= TREE_TYPE (type
);
319 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
321 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
323 /* Create a new temporary variable. We can't just use a TARGET_EXPR
324 here because it needs to live as long as DECL. */
327 arg
= make_temporary_var_for_ref_to_temp (decl
, target_type
);
329 /* Process the initializer for the declaration. */
330 DECL_INITIAL (arg
) = targ
;
331 cp_finish_decl (arg
, targ
, /*init_const_expr_p=*/false, NULL_TREE
,
332 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
334 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
335 return get_target_expr_sfinae (arg
, complain
);
337 /* If we had a way to wrap this up, and say, if we ever needed its
338 address, transform all occurrences of the register, into a memory
339 reference we could win better. */
340 rval
= cp_build_addr_expr (arg
, complain
);
341 if (rval
== error_mark_node
)
342 return error_mark_node
;
344 if ((flags
& LOOKUP_PROTECT
)
345 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
346 && MAYBE_CLASS_TYPE_P (argtype
)
347 && MAYBE_CLASS_TYPE_P (target_type
))
349 /* We go through lookup_base for the access control. */
350 tree binfo
= lookup_base (argtype
, target_type
, ba_check
,
352 if (binfo
== error_mark_node
)
353 return error_mark_node
;
354 if (binfo
== NULL_TREE
)
355 return error_not_base_type (target_type
, argtype
);
356 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1, complain
);
360 = convert_to_pointer_force (build_pointer_type (target_type
),
362 return build_nop (type
, rval
);
365 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
366 INTYPE is the original rvalue type and DECL is an optional _DECL node
369 [dcl.init.ref] says that if an rvalue is used to
370 initialize a reference, then the reference must be to a
371 non-volatile const type. */
374 diagnose_ref_binding (location_t loc
, tree reftype
, tree intype
, tree decl
)
376 tree ttl
= TREE_TYPE (reftype
);
378 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl
))
382 if (CP_TYPE_VOLATILE_P (ttl
) && decl
)
383 msg
= G_("initialization of volatile reference type %q#T from "
384 "rvalue of type %qT");
385 else if (CP_TYPE_VOLATILE_P (ttl
))
386 msg
= G_("conversion to volatile reference type %q#T "
387 "from rvalue of type %qT");
389 msg
= G_("initialization of non-const reference type %q#T from "
390 "rvalue of type %qT");
392 msg
= G_("conversion to non-const reference type %q#T from "
393 "rvalue of type %qT");
395 permerror (loc
, msg
, reftype
, intype
);
399 /* For C++: Only need to do one-level references, but cannot
400 get tripped up on signed/unsigned differences.
402 DECL is either NULL_TREE or the _DECL node for a reference that is being
403 initialized. It can be error_mark_node if we don't know the _DECL but
404 we know it's an initialization. */
407 convert_to_reference (tree reftype
, tree expr
, int convtype
,
408 int flags
, tree decl
, tsubst_flags_t complain
)
410 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
412 tree rval
= NULL_TREE
;
413 tree rval_as_conversion
= NULL_TREE
;
414 bool can_convert_intype_to_type
;
415 location_t loc
= EXPR_LOC_OR_HERE (expr
);
417 if (TREE_CODE (type
) == FUNCTION_TYPE
418 && TREE_TYPE (expr
) == unknown_type_node
)
419 expr
= instantiate_type (type
, expr
, complain
);
421 if (expr
== error_mark_node
)
422 return error_mark_node
;
424 intype
= TREE_TYPE (expr
);
426 gcc_assert (TREE_CODE (intype
) != REFERENCE_TYPE
);
427 gcc_assert (TREE_CODE (reftype
) == REFERENCE_TYPE
);
429 intype
= TYPE_MAIN_VARIANT (intype
);
431 can_convert_intype_to_type
= can_convert_standard (type
, intype
, complain
);
433 if (!can_convert_intype_to_type
434 && (convtype
& CONV_IMPLICIT
) && MAYBE_CLASS_TYPE_P (intype
)
435 && ! (flags
& LOOKUP_NO_CONVERSION
))
437 /* Look for a user-defined conversion to lvalue that we can use. */
440 = build_type_conversion (reftype
, expr
);
442 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
443 && real_lvalue_p (rval_as_conversion
))
445 expr
= rval_as_conversion
;
446 rval_as_conversion
= NULL_TREE
;
448 can_convert_intype_to_type
= 1;
452 if (((convtype
& CONV_STATIC
)
453 && can_convert_standard (intype
, type
, complain
))
454 || ((convtype
& CONV_IMPLICIT
) && can_convert_intype_to_type
))
457 tree ttl
= TREE_TYPE (reftype
);
458 tree ttr
= lvalue_type (expr
);
460 if ((complain
& tf_error
)
461 && ! real_lvalue_p (expr
))
462 diagnose_ref_binding (loc
, reftype
, intype
, decl
);
464 if (! (convtype
& CONV_CONST
)
465 && !at_least_as_qualified_p (ttl
, ttr
))
467 if (complain
& tf_error
)
468 permerror (loc
, "conversion from %qT to %qT discards qualifiers",
471 return error_mark_node
;
475 return build_up_reference (reftype
, expr
, flags
, decl
, complain
);
477 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
479 /* When casting an lvalue to a reference type, just convert into
480 a pointer to the new type and deference it. This is allowed
481 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
482 should be done directly (jason). (int &)ri ---> *(int*)&ri */
484 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
486 if ((complain
& tf_warning
)
487 && TYPE_PTR_P (intype
)
488 && (comptypes (TREE_TYPE (intype
), type
,
489 COMPARE_BASE
| COMPARE_DERIVED
)))
490 warning_at (loc
, 0, "casting %qT to %qT does not dereference pointer",
493 rval
= cp_build_addr_expr (expr
, complain
);
494 if (rval
!= error_mark_node
)
495 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
497 if (rval
!= error_mark_node
)
498 rval
= build1 (NOP_EXPR
, reftype
, rval
);
502 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
503 ICR_CONVERTING
, 0, 0, complain
);
504 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
506 if (complain
& tf_error
)
507 diagnose_ref_binding (loc
, reftype
, intype
, decl
);
508 rval
= build_up_reference (reftype
, rval
, flags
, decl
, complain
);
513 /* If we found a way to convert earlier, then use it. */
517 if (complain
& tf_error
)
518 error_at (loc
, "cannot convert type %qT to type %qT", intype
, reftype
);
520 return error_mark_node
;
523 /* We are using a reference VAL for its value. Bash that reference all the
524 way down to its lowest form. */
527 convert_from_reference (tree val
)
530 && TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
532 tree t
= TREE_TYPE (TREE_TYPE (val
));
533 tree ref
= build1 (INDIRECT_REF
, t
, val
);
536 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
537 so that we get the proper error message if the result is used
538 to assign to. Also, &* is supposed to be a no-op. */
539 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
540 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
541 TREE_SIDE_EFFECTS (ref
)
542 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (val
));
549 /* Really perform an lvalue-to-rvalue conversion, including copying an
550 argument of class type into a temporary. */
553 force_rvalue (tree expr
, tsubst_flags_t complain
)
555 tree type
= TREE_TYPE (expr
);
556 if (MAYBE_CLASS_TYPE_P (type
) && TREE_CODE (expr
) != TARGET_EXPR
)
558 vec
<tree
, va_gc
> *args
= make_tree_vector_single (expr
);
559 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
560 &args
, type
, LOOKUP_NORMAL
, complain
);
561 release_tree_vector (args
);
562 expr
= build_cplus_new (type
, expr
, complain
);
565 expr
= decay_conversion (expr
, complain
);
571 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
572 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
576 ignore_overflows (tree expr
, tree orig
)
578 if (TREE_CODE (expr
) == INTEGER_CST
579 && TREE_CODE (orig
) == INTEGER_CST
580 && TREE_OVERFLOW (expr
) != TREE_OVERFLOW (orig
))
582 gcc_assert (!TREE_OVERFLOW (orig
));
583 /* Ensure constant sharing. */
584 expr
= build_int_cst_wide (TREE_TYPE (expr
),
585 TREE_INT_CST_LOW (expr
),
586 TREE_INT_CST_HIGH (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
;
632 tree folded_result
= cp_convert (type
, folded
, complain
);
634 /* maybe_constant_value wraps an INTEGER_CST with TREE_OVERFLOW in a
635 NOP_EXPR so that it isn't TREE_CONSTANT anymore. */
636 STRIP_NOPS (stripped
);
638 if (!TREE_OVERFLOW_P (stripped
)
639 && folded_result
!= error_mark_node
)
640 warnings_for_convert_and_check (type
, folded
, folded_result
);
648 FLAGS indicates how we should behave. */
651 ocp_convert (tree type
, tree expr
, int convtype
, int flags
,
652 tsubst_flags_t complain
)
655 enum tree_code code
= TREE_CODE (type
);
656 const char *invalid_conv_diag
;
658 location_t loc
= EXPR_LOC_OR_HERE (expr
);
660 if (error_operand_p (e
) || type
== error_mark_node
)
661 return error_mark_node
;
663 complete_type (type
);
664 complete_type (TREE_TYPE (expr
));
666 if ((invalid_conv_diag
667 = targetm
.invalid_conversion (TREE_TYPE (expr
), type
)))
669 if (complain
& tf_error
)
670 error (invalid_conv_diag
);
671 return error_mark_node
;
674 /* FIXME remove when moving to c_fully_fold model. */
675 /* FIXME do we still need this test? */
676 if (!CLASS_TYPE_P (type
))
677 e
= integral_constant_value (e
);
678 if (error_operand_p (e
))
679 return error_mark_node
;
681 if (MAYBE_CLASS_TYPE_P (type
) && (convtype
& CONV_FORCE_TEMP
))
682 /* We need a new temporary; don't take this shortcut. */;
683 else if (same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (e
)))
685 if (same_type_p (type
, TREE_TYPE (e
)))
686 /* The call to fold will not always remove the NOP_EXPR as
687 might be expected, since if one of the types is a typedef;
688 the comparison in fold is just equality of pointers, not a
689 call to comptypes. We don't call fold in this case because
690 that can result in infinite recursion; fold will call
691 convert, which will call ocp_convert, etc. */
693 /* For complex data types, we need to perform componentwise
695 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
696 return fold_if_not_in_template (convert_to_complex (type
, e
));
697 else if (TREE_CODE (type
) == VECTOR_TYPE
)
698 return fold_if_not_in_template (convert_to_vector (type
, e
));
699 else if (TREE_CODE (e
) == TARGET_EXPR
)
701 /* Don't build a NOP_EXPR of class type. Instead, change the
702 type of the temporary. */
703 TREE_TYPE (e
) = TREE_TYPE (TARGET_EXPR_SLOT (e
)) = type
;
708 /* We shouldn't be treating objects of ADDRESSABLE type as
710 gcc_assert (!TREE_ADDRESSABLE (type
));
711 return fold_if_not_in_template (build_nop (type
, e
));
715 e1
= targetm
.convert_to_type (type
, e
);
719 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
721 e
= convert_to_void (e
, ICV_CAST
, complain
);
725 if (INTEGRAL_CODE_P (code
))
727 tree intype
= TREE_TYPE (e
);
730 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
732 /* enum = enum, enum = int, enum = float, (enum)pointer are all
734 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
735 || TREE_CODE (intype
) == REAL_TYPE
)
736 && ! (convtype
& CONV_STATIC
))
737 || TYPE_PTR_P (intype
))
739 if (complain
& tf_error
)
740 permerror (loc
, "conversion from %q#T to %q#T", intype
, type
);
742 return error_mark_node
;
745 /* [expr.static.cast]
747 8. A value of integral or enumeration type can be explicitly
748 converted to an enumeration type. The value is unchanged if
749 the original value is within the range of the enumeration
750 values. Otherwise, the resulting enumeration value is
752 if ((complain
& tf_warning
)
753 && TREE_CODE (e
) == INTEGER_CST
754 && !int_fits_type_p (e
, ENUM_UNDERLYING_TYPE (type
)))
755 warning_at (loc
, OPT_Wconversion
,
756 "the result of the conversion is unspecified because "
757 "%qE is outside the range of type %qT",
760 if (MAYBE_CLASS_TYPE_P (intype
))
763 rval
= build_type_conversion (type
, e
);
766 if (complain
& tf_error
)
767 error_at (loc
, "%q#T used where a %qT was expected", intype
, type
);
768 return error_mark_node
;
770 if (code
== BOOLEAN_TYPE
)
772 if (VOID_TYPE_P (intype
))
774 if (complain
& tf_error
)
776 "could not convert %qE from %<void%> to %<bool%>",
778 return error_mark_node
;
781 /* We can't implicitly convert a scoped enum to bool, so convert
782 to the underlying type first. */
783 if (SCOPED_ENUM_P (intype
) && (convtype
& CONV_STATIC
))
784 e
= build_nop (ENUM_UNDERLYING_TYPE (intype
), e
);
785 return cp_truthvalue_conversion (e
);
788 converted
= fold_if_not_in_template (convert_to_integer (type
, e
));
790 /* Ignore any integer overflow caused by the conversion. */
791 return ignore_overflows (converted
, e
);
793 if (NULLPTR_TYPE_P (type
) && e
&& null_ptr_cst_p (e
))
795 if (complain
& tf_warning
)
796 maybe_warn_zero_as_null_pointer_constant (e
, loc
);
799 if (POINTER_TYPE_P (type
) || TYPE_PTRMEM_P (type
))
800 return fold_if_not_in_template (cp_convert_to_pointer (type
, e
, complain
));
801 if (code
== VECTOR_TYPE
)
803 tree in_vtype
= TREE_TYPE (e
);
804 if (MAYBE_CLASS_TYPE_P (in_vtype
))
807 ret_val
= build_type_conversion (type
, e
);
810 if (complain
& tf_error
)
811 error_at (loc
, "%q#T used where a %qT was expected",
813 return error_mark_node
;
815 return fold_if_not_in_template (convert_to_vector (type
, e
));
817 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
819 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e
)))
822 rval
= build_type_conversion (type
, e
);
825 else if (complain
& tf_error
)
827 "%q#T used where a floating point value was expected",
830 if (code
== REAL_TYPE
)
831 return fold_if_not_in_template (convert_to_real (type
, e
));
832 else if (code
== COMPLEX_TYPE
)
833 return fold_if_not_in_template (convert_to_complex (type
, e
));
836 /* New C++ semantics: since assignment is now based on
837 memberwise copying, if the rhs type is derived from the
838 lhs type, then we may still do a conversion. */
839 if (RECORD_OR_UNION_CODE_P (code
))
841 tree dtype
= TREE_TYPE (e
);
842 tree ctor
= NULL_TREE
;
844 dtype
= TYPE_MAIN_VARIANT (dtype
);
846 /* Conversion between aggregate types. New C++ semantics allow
847 objects of derived type to be cast to objects of base type.
848 Old semantics only allowed this between pointers.
850 There may be some ambiguity between using a constructor
851 vs. using a type conversion operator when both apply. */
855 if (abstract_virtuals_error_sfinae (NULL_TREE
, type
, complain
))
856 return error_mark_node
;
858 if (BRACE_ENCLOSED_INITIALIZER_P (ctor
))
859 ctor
= perform_implicit_conversion (type
, ctor
, complain
);
860 else if ((flags
& LOOKUP_ONLYCONVERTING
)
861 && ! (CLASS_TYPE_P (dtype
) && DERIVED_FROM_P (type
, dtype
)))
862 /* For copy-initialization, first we create a temp of the proper type
863 with a user-defined conversion sequence, then we direct-initialize
864 the target with the temp (see [dcl.init]). */
865 ctor
= build_user_type_conversion (type
, ctor
, flags
, complain
);
868 vec
<tree
, va_gc
> *ctor_vec
= make_tree_vector_single (ctor
);
869 ctor
= build_special_member_call (NULL_TREE
,
870 complete_ctor_identifier
,
872 type
, flags
, complain
);
873 release_tree_vector (ctor_vec
);
876 return build_cplus_new (type
, ctor
, complain
);
879 if (complain
& tf_error
)
881 /* If the conversion failed and expr was an invalid use of pointer to
882 member function, try to report a meaningful error. */
883 if (invalid_nonstatic_memfn_p (expr
, complain
))
884 /* We displayed the error message. */;
886 error_at (loc
, "conversion from %qT to non-scalar type %qT requested",
887 TREE_TYPE (expr
), type
);
889 return error_mark_node
;
892 /* When an expression is used in a void context, its value is discarded and
893 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
894 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
895 in a void context. The C++ standard does not define what an `access' to an
896 object is, but there is reason to believe that it is the lvalue to rvalue
897 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
898 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
899 indicates that volatile semantics should be the same between C and C++
900 where ever possible. C leaves it implementation defined as to what
901 constitutes an access to a volatile. So, we interpret `*vp' as a read of
902 the volatile object `vp' points to, unless that is an incomplete type. For
903 volatile references we do not do this interpretation, because that would
904 make it impossible to ignore the reference return value from functions. We
905 issue warnings in the confusing cases.
907 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
908 to void via a cast. If an expression is being implicitly converted, IMPLICIT
909 indicates the context of the implicit conversion. */
912 convert_to_void (tree expr
, impl_conv_void implicit
, tsubst_flags_t complain
)
914 location_t loc
= EXPR_LOC_OR_HERE (expr
);
916 if (expr
== error_mark_node
917 || TREE_TYPE (expr
) == error_mark_node
)
918 return error_mark_node
;
920 if (implicit
== ICV_CAST
)
921 mark_exp_read (expr
);
926 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
927 exprv
= TREE_OPERAND (exprv
, 1);
929 || handled_component_p (exprv
)
930 || INDIRECT_REF_P (exprv
))
931 /* Expr is not being 'used' here, otherwise we whould have
932 called mark_{rl}value_use use here, which would have in turn
933 called mark_exp_read. Rather, we call mark_exp_read directly
934 to avoid some warnings when
935 -Wunused-but-set-{variable,parameter} is in effect. */
936 mark_exp_read (exprv
);
939 if (!TREE_TYPE (expr
))
941 if (invalid_nonstatic_memfn_p (expr
, complain
))
942 return error_mark_node
;
943 if (TREE_CODE (expr
) == PSEUDO_DTOR_EXPR
)
945 if (complain
& tf_error
)
946 error_at (loc
, "pseudo-destructor is not called");
947 return error_mark_node
;
949 if (VOID_TYPE_P (TREE_TYPE (expr
)))
951 switch (TREE_CODE (expr
))
955 /* The two parts of a cond expr might be separate lvalues. */
956 tree op1
= TREE_OPERAND (expr
,1);
957 tree op2
= TREE_OPERAND (expr
,2);
958 bool side_effects
= ((op1
&& TREE_SIDE_EFFECTS (op1
))
959 || TREE_SIDE_EFFECTS (op2
));
960 tree new_op1
, new_op2
;
962 if (implicit
!= ICV_CAST
&& !side_effects
)
965 new_op1
= convert_to_void (op1
, ICV_SECOND_OF_COND
, complain
);
966 new_op2
= convert_to_void (op2
, ICV_THIRD_OF_COND
, complain
);
971 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
972 new_op2
= convert_to_void (op2
, ICV_CAST
, complain
);
975 expr
= build3 (COND_EXPR
, TREE_TYPE (new_op2
),
976 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
982 /* The second part of a compound expr contains the value. */
983 tree op1
= TREE_OPERAND (expr
,1);
985 if (implicit
!= ICV_CAST
&& !TREE_NO_WARNING (expr
))
986 new_op1
= convert_to_void (op1
, ICV_RIGHT_OF_COMMA
, complain
);
988 new_op1
= convert_to_void (op1
, ICV_CAST
, complain
);
992 tree t
= build2 (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
993 TREE_OPERAND (expr
, 0), new_op1
);
1000 case NON_LVALUE_EXPR
:
1002 /* These have already decayed to rvalue. */
1005 case CALL_EXPR
: /* We have a special meaning for volatile void fn(). */
1010 tree type
= TREE_TYPE (expr
);
1011 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
1013 int is_volatile
= TYPE_VOLATILE (type
);
1014 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
1016 /* Can't load the value if we don't know the type. */
1017 if (is_volatile
&& !is_complete
)
1019 if (complain
& tf_warning
)
1023 warning_at (loc
, 0, "conversion to void will not access "
1024 "object of incomplete type %qT", type
);
1026 case ICV_SECOND_OF_COND
:
1027 warning_at (loc
, 0, "indirection will not access object of "
1028 "incomplete type %qT in second operand "
1029 "of conditional expression", type
);
1031 case ICV_THIRD_OF_COND
:
1032 warning_at (loc
, 0, "indirection will not access object of "
1033 "incomplete type %qT in third operand "
1034 "of conditional expression", type
);
1036 case ICV_RIGHT_OF_COMMA
:
1037 warning_at (loc
, 0, "indirection will not access object of "
1038 "incomplete type %qT in right operand of "
1039 "comma operator", type
);
1041 case ICV_LEFT_OF_COMMA
:
1042 warning_at (loc
, 0, "indirection will not access object of "
1043 "incomplete type %qT in left operand of "
1044 "comma operator", type
);
1047 warning_at (loc
, 0, "indirection will not access object of "
1048 "incomplete type %qT in statement", type
);
1050 case ICV_THIRD_IN_FOR
:
1051 warning_at (loc
, 0, "indirection will not access object of "
1052 "incomplete type %qT in for increment "
1053 "expression", type
);
1059 /* Don't load the value if this is an implicit dereference, or if
1060 the type needs to be handled by ctors/dtors. */
1061 else if (is_volatile
&& is_reference
)
1063 if (complain
& tf_warning
)
1067 warning_at (loc
, 0, "conversion to void will not access "
1068 "object of type %qT", type
);
1070 case ICV_SECOND_OF_COND
:
1071 warning_at (loc
, 0, "implicit dereference will not access "
1072 "object of type %qT in second operand of "
1073 "conditional expression", type
);
1075 case ICV_THIRD_OF_COND
:
1076 warning_at (loc
, 0, "implicit dereference will not access "
1077 "object of type %qT in third operand of "
1078 "conditional expression", type
);
1080 case ICV_RIGHT_OF_COMMA
:
1081 warning_at (loc
, 0, "implicit dereference will not access "
1082 "object of type %qT in right operand of "
1083 "comma operator", type
);
1085 case ICV_LEFT_OF_COMMA
:
1086 warning_at (loc
, 0, "implicit dereference will not access "
1087 "object of type %qT in left operand of comma "
1091 warning_at (loc
, 0, "implicit dereference will not access "
1092 "object of type %qT in statement", type
);
1094 case ICV_THIRD_IN_FOR
:
1095 warning_at (loc
, 0, "implicit dereference will not access "
1096 "object of type %qT in for increment expression",
1103 else if (is_volatile
&& TREE_ADDRESSABLE (type
))
1105 if (complain
& tf_warning
)
1109 warning_at (loc
, 0, "conversion to void will not access "
1110 "object of non-trivially-copyable type %qT",
1113 case ICV_SECOND_OF_COND
:
1114 warning_at (loc
, 0, "indirection will not access object of "
1115 "non-trivially-copyable type %qT in second "
1116 "operand of conditional expression", type
);
1118 case ICV_THIRD_OF_COND
:
1119 warning_at (loc
, 0, "indirection will not access object of "
1120 "non-trivially-copyable type %qT in third "
1121 "operand of conditional expression", type
);
1123 case ICV_RIGHT_OF_COMMA
:
1124 warning_at (loc
, 0, "indirection will not access object of "
1125 "non-trivially-copyable type %qT in right "
1126 "operand of comma operator", type
);
1128 case ICV_LEFT_OF_COMMA
:
1129 warning_at (loc
, 0, "indirection will not access object of "
1130 "non-trivially-copyable type %qT in left "
1131 "operand of comma operator", type
);
1134 warning_at (loc
, 0, "indirection will not access object of "
1135 "non-trivially-copyable type %qT in statement",
1138 case ICV_THIRD_IN_FOR
:
1139 warning_at (loc
, 0, "indirection will not access object of "
1140 "non-trivially-copyable type %qT in for "
1141 "increment expression", type
);
1147 if (is_reference
|| !is_volatile
|| !is_complete
|| TREE_ADDRESSABLE (type
))
1149 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1150 operation is stripped off. Note that we don't warn about
1151 - an expression with TREE_NO_WARNING set. (For an example of
1152 such expressions, see build_over_call in call.c.)
1153 - automatic dereferencing of references, since the user cannot
1154 control it. (See also warn_if_unused_value() in c-common.c.) */
1155 if (warn_unused_value
1156 && implicit
!= ICV_CAST
1157 && (complain
& tf_warning
)
1158 && !TREE_NO_WARNING (expr
)
1160 warning_at (loc
, OPT_Wunused_value
, "value computed is not used");
1161 expr
= TREE_OPERAND (expr
, 0);
1169 /* External variables might be incomplete. */
1170 tree type
= TREE_TYPE (expr
);
1171 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
1173 if (TYPE_VOLATILE (type
) && !is_complete
&& (complain
& tf_warning
))
1177 warning_at (loc
, 0, "conversion to void will not access "
1178 "object %qE of incomplete type %qT", expr
, type
);
1180 case ICV_SECOND_OF_COND
:
1181 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1182 "not be accessed in second operand of "
1183 "conditional expression", expr
, type
);
1185 case ICV_THIRD_OF_COND
:
1186 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1187 "not be accessed in third operand of "
1188 "conditional expression", expr
, type
);
1190 case ICV_RIGHT_OF_COMMA
:
1191 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1192 "not be accessed in right operand of comma operator",
1195 case ICV_LEFT_OF_COMMA
:
1196 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1197 "not be accessed in left operand of comma operator",
1201 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1202 "not be accessed in statement", expr
, type
);
1204 case ICV_THIRD_IN_FOR
:
1205 warning_at (loc
, 0, "variable %qE of incomplete type %qT will "
1206 "not be accessed in for increment expression",
1217 /* Don't bother with the temporary object returned from a function if
1218 we don't use it and don't need to destroy it. We'll still
1219 allocate space for it in expand_call or declare_return_variable,
1220 but we don't need to track it through all the tree phases. */
1221 if (TARGET_EXPR_IMPLICIT_P (expr
)
1222 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr
)))
1224 tree init
= TARGET_EXPR_INITIAL (expr
);
1225 if (TREE_CODE (init
) == AGGR_INIT_EXPR
1226 && !AGGR_INIT_VIA_CTOR_P (init
))
1228 tree fn
= AGGR_INIT_EXPR_FN (init
);
1229 expr
= build_call_array_loc (input_location
,
1230 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
))),
1232 aggr_init_expr_nargs (init
),
1233 AGGR_INIT_EXPR_ARGP (init
));
1240 expr
= resolve_nondeduced_context (expr
);
1244 if (TREE_CODE (probe
) == ADDR_EXPR
)
1245 probe
= TREE_OPERAND (expr
, 0);
1246 if (type_unknown_p (probe
))
1248 /* [over.over] enumerates the places where we can take the address
1249 of an overloaded function, and this is not one of them. */
1250 if (complain
& tf_error
)
1254 error_at (loc
, "conversion to void "
1255 "cannot resolve address of overloaded function");
1257 case ICV_SECOND_OF_COND
:
1258 error_at (loc
, "second operand of conditional expression "
1259 "cannot resolve address of overloaded function");
1261 case ICV_THIRD_OF_COND
:
1262 error_at (loc
, "third operand of conditional expression "
1263 "cannot resolve address of overloaded function");
1265 case ICV_RIGHT_OF_COMMA
:
1266 error_at (loc
, "right operand of comma operator "
1267 "cannot resolve address of overloaded function");
1269 case ICV_LEFT_OF_COMMA
:
1270 error_at (loc
, "left operand of comma operator "
1271 "cannot resolve address of overloaded function");
1274 error_at (loc
, "statement "
1275 "cannot resolve address of overloaded function");
1277 case ICV_THIRD_IN_FOR
:
1278 error_at (loc
, "for increment expression "
1279 "cannot resolve address of overloaded function");
1283 return error_mark_node
;
1284 expr
= void_zero_node
;
1286 else if (implicit
!= ICV_CAST
&& probe
== expr
&& is_overloaded_fn (probe
))
1288 /* Only warn when there is no &. */
1289 if (complain
& tf_warning
)
1292 case ICV_SECOND_OF_COND
:
1293 warning_at (loc
, OPT_Waddress
,
1294 "second operand of conditional expression "
1295 "is a reference, not call, to function %qE", expr
);
1297 case ICV_THIRD_OF_COND
:
1298 warning_at (loc
, OPT_Waddress
,
1299 "third operand of conditional expression "
1300 "is a reference, not call, to function %qE", expr
);
1302 case ICV_RIGHT_OF_COMMA
:
1303 warning_at (loc
, OPT_Waddress
,
1304 "right operand of comma operator "
1305 "is a reference, not call, to function %qE", expr
);
1307 case ICV_LEFT_OF_COMMA
:
1308 warning_at (loc
, OPT_Waddress
,
1309 "left operand of comma operator "
1310 "is a reference, not call, to function %qE", expr
);
1313 warning_at (loc
, OPT_Waddress
,
1314 "statement is a reference, not call, to function %qE",
1317 case ICV_THIRD_IN_FOR
:
1318 warning_at (loc
, OPT_Waddress
,
1319 "for increment expression "
1320 "is a reference, not call, to function %qE", expr
);
1326 if (TREE_CODE (expr
) == COMPONENT_REF
)
1327 expr
= TREE_OPERAND (expr
, 0);
1331 if (expr
!= error_mark_node
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
1333 if (implicit
!= ICV_CAST
1334 && warn_unused_value
1335 && !TREE_NO_WARNING (expr
)
1336 && !processing_template_decl
)
1338 /* The middle end does not warn about expressions that have
1339 been explicitly cast to void, so we must do so here. */
1340 if (!TREE_SIDE_EFFECTS (expr
)) {
1341 if (complain
& tf_warning
)
1344 case ICV_SECOND_OF_COND
:
1345 warning_at (loc
, OPT_Wunused_value
,
1346 "second operand of conditional expression "
1349 case ICV_THIRD_OF_COND
:
1350 warning_at (loc
, OPT_Wunused_value
,
1351 "third operand of conditional expression "
1354 case ICV_RIGHT_OF_COMMA
:
1355 warning_at (loc
, OPT_Wunused_value
,
1356 "right operand of comma operator has no effect");
1358 case ICV_LEFT_OF_COMMA
:
1359 warning_at (loc
, OPT_Wunused_value
,
1360 "left operand of comma operator has no effect");
1363 warning_at (loc
, OPT_Wunused_value
,
1364 "statement has no effect");
1366 case ICV_THIRD_IN_FOR
:
1367 warning_at (loc
, OPT_Wunused_value
,
1368 "for increment expression has no effect");
1377 enum tree_code code
;
1378 enum tree_code_class tclass
;
1381 /* We might like to warn about (say) "(int) f()", as the
1382 cast has no effect, but the compiler itself will
1383 generate implicit conversions under some
1384 circumstances. (For example a block copy will be
1385 turned into a call to "__builtin_memcpy", with a
1386 conversion of the return value to an appropriate
1387 type.) So, to avoid false positives, we strip
1388 conversions. Do not use STRIP_NOPs because it will
1389 not strip conversions to "void", as that is not a
1390 mode-preserving conversion. */
1391 while (TREE_CODE (e
) == NOP_EXPR
)
1392 e
= TREE_OPERAND (e
, 0);
1394 code
= TREE_CODE (e
);
1395 tclass
= TREE_CODE_CLASS (code
);
1396 if ((tclass
== tcc_comparison
1397 || tclass
== tcc_unary
1398 || (tclass
== tcc_binary
1399 && !(code
== MODIFY_EXPR
1400 || code
== INIT_EXPR
1401 || code
== PREDECREMENT_EXPR
1402 || code
== PREINCREMENT_EXPR
1403 || code
== POSTDECREMENT_EXPR
1404 || code
== POSTINCREMENT_EXPR
)))
1405 && (complain
& tf_warning
))
1406 warning_at (loc
, OPT_Wunused_value
, "value computed is not used");
1409 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
1411 if (! TREE_SIDE_EFFECTS (expr
))
1412 expr
= void_zero_node
;
1416 /* Create an expression whose value is that of EXPR,
1417 converted to type TYPE. The TREE_TYPE of the value
1418 is always TYPE. This function implements all reasonable
1419 conversions; callers should filter out those that are
1420 not permitted by the language being compiled.
1422 Most of this routine is from build_reinterpret_cast.
1424 The back end cannot call cp_convert (what was convert) because
1425 conversions to/from basetypes may involve memory references
1426 (vbases) and adding or subtracting small values (multiple
1427 inheritance), but it calls convert from the constant folding code
1428 on subtrees of already built trees after it has ripped them apart.
1430 Also, if we ever support range variables, we'll probably also have to
1431 do a little bit more work. */
1434 convert (tree type
, tree expr
)
1438 if (type
== error_mark_node
|| expr
== error_mark_node
)
1439 return error_mark_node
;
1441 intype
= TREE_TYPE (expr
);
1443 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
1444 return fold_if_not_in_template (build_nop (type
, expr
));
1446 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
1447 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
,
1448 tf_warning_or_error
);
1451 /* Like cp_convert, except permit conversions to take place which
1452 are not normally allowed due to access restrictions
1453 (such as conversion from sub-type to private super-type). */
1456 convert_force (tree type
, tree expr
, int convtype
, tsubst_flags_t complain
)
1459 enum tree_code code
= TREE_CODE (type
);
1461 if (code
== REFERENCE_TYPE
)
1462 return (fold_if_not_in_template
1463 (convert_to_reference (type
, e
, CONV_C_CAST
, 0,
1464 NULL_TREE
, complain
)));
1466 if (code
== POINTER_TYPE
)
1467 return fold_if_not_in_template (convert_to_pointer_force (type
, e
,
1470 /* From typeck.c convert_for_assignment */
1471 if (((TYPE_PTR_P (TREE_TYPE (e
)) && TREE_CODE (e
) == ADDR_EXPR
1472 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1473 || integer_zerop (e
)
1474 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1475 && TYPE_PTRMEMFUNC_P (type
))
1476 /* compatible pointer to member functions. */
1477 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1,
1478 /*c_cast_p=*/1, complain
);
1480 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
, complain
);
1483 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1484 exists, return the attempted conversion. This may
1485 return ERROR_MARK_NODE if the conversion is not
1486 allowed (references private members, etc).
1487 If no conversion exists, NULL_TREE is returned.
1489 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1490 object parameter, or by the second standard conversion sequence if
1491 that doesn't do it. This will probably wait for an overloading rewrite.
1495 build_type_conversion (tree xtype
, tree expr
)
1497 /* C++: check to see if we can convert this aggregate type
1498 into the required type. */
1499 return build_user_type_conversion (xtype
, expr
, LOOKUP_NORMAL
,
1500 tf_warning_or_error
);
1503 /* Convert the given EXPR to one of a group of types suitable for use in an
1504 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1505 which indicates which types are suitable. If COMPLAIN is true, complain
1506 about ambiguity; otherwise, the caller will deal with it. */
1509 build_expr_type_conversion (int desires
, tree expr
, bool complain
)
1511 tree basetype
= TREE_TYPE (expr
);
1512 tree conv
= NULL_TREE
;
1513 tree winner
= NULL_TREE
;
1515 if (expr
== null_node
1516 && (desires
& WANT_INT
)
1517 && !(desires
& WANT_NULL
))
1519 source_location loc
=
1520 expansion_point_location_if_in_system_header (input_location
);
1522 warning_at (loc
, OPT_Wconversion_null
,
1523 "converting NULL to non-pointer type");
1526 if (basetype
== error_mark_node
)
1527 return error_mark_node
;
1529 if (! MAYBE_CLASS_TYPE_P (basetype
))
1530 switch (TREE_CODE (basetype
))
1533 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1535 /* else fall through... */
1538 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1540 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1542 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1544 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1548 return (desires
& WANT_POINTER
) ? decay_conversion (expr
,
1549 tf_warning_or_error
)
1554 if ((desires
& WANT_VECTOR_OR_COMPLEX
) == 0)
1556 switch (TREE_CODE (TREE_TYPE (basetype
)))
1560 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1562 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1564 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1573 /* The code for conversions from class type is currently only used for
1574 delete expressions. Other expressions are handled by build_new_op. */
1575 if (!complete_type_or_maybe_complain (basetype
, expr
, complain
))
1576 return error_mark_node
;
1577 if (!TYPE_HAS_CONVERSION (basetype
))
1580 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1584 tree cand
= TREE_VALUE (conv
);
1585 cand
= OVL_CURRENT (cand
);
1587 if (winner
&& winner
== cand
)
1590 if (DECL_NONCONVERTING_P (cand
))
1593 candidate
= non_reference (TREE_TYPE (TREE_TYPE (cand
)));
1595 switch (TREE_CODE (candidate
))
1599 win
= (desires
& WANT_INT
); break;
1601 win
= (desires
& WANT_ENUM
); break;
1603 win
= (desires
& WANT_FLOAT
); break;
1605 win
= (desires
& WANT_POINTER
); break;
1609 if ((desires
& WANT_VECTOR_OR_COMPLEX
) == 0)
1611 switch (TREE_CODE (TREE_TYPE (candidate
)))
1615 win
= (desires
& WANT_INT
); break;
1617 win
= (desires
& WANT_ENUM
); break;
1619 win
= (desires
& WANT_FLOAT
); break;
1626 /* A wildcard could be instantiated to match any desired
1627 type, but we can't deduce the template argument. */
1628 if (WILDCARD_TYPE_P (candidate
))
1635 if (TREE_CODE (cand
) == TEMPLATE_DECL
)
1638 error ("default type conversion can't deduce template"
1639 " argument for %qD", cand
);
1640 return error_mark_node
;
1646 = non_reference (TREE_TYPE (TREE_TYPE (winner
)));
1648 if (!same_type_ignoring_top_level_qualifiers_p (winner_type
,
1653 error ("ambiguous default type conversion from %qT",
1655 error (" candidate conversions include %qD and %qD",
1658 return error_mark_node
;
1668 tree type
= non_reference (TREE_TYPE (TREE_TYPE (winner
)));
1669 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
,
1670 tf_warning_or_error
);
1676 /* Implements integral promotion (4.1) and float->double promotion. */
1679 type_promotes_to (tree type
)
1683 if (type
== error_mark_node
)
1684 return error_mark_node
;
1686 type
= TYPE_MAIN_VARIANT (type
);
1688 /* Check for promotions of target-defined types first. */
1689 promoted_type
= targetm
.promoted_type (type
);
1691 return promoted_type
;
1693 /* bool always promotes to int (not unsigned), even if it's the same
1695 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
1696 type
= integer_type_node
;
1698 /* Scoped enums don't promote, but pretend they do for backward ABI bug
1699 compatibility wrt varargs. */
1700 else if (SCOPED_ENUM_P (type
) && abi_version_at_least (6))
1703 /* Normally convert enums to int, but convert wide enums to something
1705 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1706 || type
== char16_type_node
1707 || type
== char32_type_node
1708 || type
== wchar_type_node
)
1710 int precision
= MAX (TYPE_PRECISION (type
),
1711 TYPE_PRECISION (integer_type_node
));
1712 tree totype
= c_common_type_for_size (precision
, 0);
1713 if (SCOPED_ENUM_P (type
))
1714 warning (OPT_Wabi
, "scoped enum %qT will not promote to an integral "
1715 "type in a future version of GCC", type
);
1716 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1717 type
= ENUM_UNDERLYING_TYPE (type
);
1718 if (TYPE_UNSIGNED (type
)
1719 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1720 type
= c_common_type_for_size (precision
, 1);
1724 else if (c_promoting_integer_type_p (type
))
1726 /* Retain unsignedness if really not getting bigger. */
1727 if (TYPE_UNSIGNED (type
)
1728 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1729 type
= unsigned_type_node
;
1731 type
= integer_type_node
;
1733 else if (type
== float_type_node
)
1734 type
= double_type_node
;
1739 /* The routines below this point are carefully written to conform to
1740 the standard. They use the same terminology, and follow the rules
1741 closely. Although they are used only in pt.c at the moment, they
1742 should presumably be used everywhere in the future. */
1744 /* Attempt to perform qualification conversions on EXPR to convert it
1745 to TYPE. Return the resulting expression, or error_mark_node if
1746 the conversion was impossible. */
1749 perform_qualification_conversions (tree type
, tree expr
)
1753 expr_type
= TREE_TYPE (expr
);
1755 if (same_type_p (type
, expr_type
))
1757 else if (TYPE_PTR_P (type
) && TYPE_PTR_P (expr_type
)
1758 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (expr_type
)))
1759 return build_nop (type
, expr
);
1760 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (expr_type
)
1761 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
1762 TYPE_PTRMEM_CLASS_TYPE (expr_type
))
1763 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type
),
1764 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type
)))
1765 return build_nop (type
, expr
);
1767 return error_mark_node
;