1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 /* This file contains the functions for converting C++ expressions
25 to different data types. The only entry point is `convert'.
26 Every language front end must have a `convert' function
27 but what kind of conversions it does will depend on the language. */
31 #include "coretypes.h"
41 static tree cp_convert_to_pointer (tree, tree, bool);
42 static tree convert_to_pointer_force (tree, tree);
43 static tree build_type_conversion (tree, tree);
44 static tree build_up_reference (tree, tree, int, tree);
45 static void warn_ref_binding (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. If FORCE is true then allow
75 conversions via virtual bases (these are permitted by reinterpret_cast,
76 but not static_cast). */
79 cp_convert_to_pointer (tree type, tree expr, bool force)
81 tree intype = TREE_TYPE (expr);
84 if (intype == error_mark_node)
85 return error_mark_node;
87 if (IS_AGGR_TYPE (intype))
89 intype = complete_type (intype);
90 if (!COMPLETE_TYPE_P (intype))
92 error ("can't convert from incomplete type %qT to %qT",
94 return error_mark_node;
97 rval = build_type_conversion (type, expr);
100 if (rval == error_mark_node)
101 error ("conversion of %qE from %qT to %qT is ambiguous",
107 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
108 if (TREE_CODE (type) == POINTER_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);
115 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
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
130 && TREE_CODE (type) == POINTER_TYPE
131 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
132 && IS_AGGR_TYPE (TREE_TYPE (type))
133 && IS_AGGR_TYPE (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, NULL);
151 if (!same_p && !binfo)
153 /* Try base to derived conversion. */
154 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
157 if (binfo == error_mark_node)
158 return error_mark_node;
162 expr = build_base_path (code, expr, binfo, 0);
163 /* Add any qualifier conversions. */
164 return build_nop (type, expr);
168 if (TYPE_PTRMEMFUNC_P (type))
170 error ("cannot convert %qE from type %qT to type %qT",
172 return error_mark_node;
175 return build_nop (type, expr);
177 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
182 enum tree_code code = PLUS_EXPR;
185 b1 = TYPE_PTRMEM_CLASS_TYPE (type);
186 b2 = TYPE_PTRMEM_CLASS_TYPE (intype);
187 binfo = lookup_base (b1, b2, ba_check, &bk);
190 binfo = lookup_base (b2, b1, ba_check, &bk);
193 if (binfo == error_mark_node)
194 return error_mark_node;
196 if (bk == bk_via_virtual)
199 warning (0, "pointer to member cast from %qT to %qT is via"
200 " virtual base", intype, type);
203 error ("pointer to member cast from %qT to %qT is"
204 " via virtual base", intype, type);
205 return error_mark_node;
207 /* This is a reinterpret cast, whose result is unspecified.
208 We choose to do nothing. */
209 return build1 (NOP_EXPR, type, expr);
212 if (TREE_CODE (expr) == PTRMEM_CST)
213 expr = cplus_expand_constant (expr);
215 if (binfo && !integer_zerop (BINFO_OFFSET (binfo)))
216 expr = size_binop (code,
217 build_nop (sizetype, expr),
218 BINFO_OFFSET (binfo));
219 return build_nop (type, expr);
221 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
222 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
224 else if (TYPE_PTRMEMFUNC_P (intype))
228 if (TREE_CODE (expr) == PTRMEM_CST)
229 return cp_convert_to_pointer (type,
230 PTRMEM_CST_MEMBER (expr),
232 else if (TREE_CODE (expr) == OFFSET_REF)
234 tree object = TREE_OPERAND (expr, 0);
235 return get_member_function_from_ptrfunc (&object,
236 TREE_OPERAND (expr, 1));
239 error ("cannot convert %qE from type %qT to type %qT",
241 return error_mark_node;
244 if (integer_zerop (expr))
246 if (TYPE_PTRMEMFUNC_P (type))
247 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
250 if (TYPE_PTRMEM_P (type))
252 /* A NULL pointer-to-member is represented by -1, not by
254 expr = build_int_cst_type (type, -1);
257 expr = build_int_cst (type, 0);
261 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
263 error ("invalid conversion from %qT to %qT", intype, type);
264 return error_mark_node;
267 if (INTEGRAL_CODE_P (form))
269 if (TYPE_PRECISION (intype) == POINTER_SIZE)
270 return build1 (CONVERT_EXPR, type, expr);
271 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
272 /* Modes may be different but sizes should be the same. There
273 is supposed to be some integral type that is the same width
275 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
276 == GET_MODE_SIZE (TYPE_MODE (type)));
278 return convert_to_pointer (type, expr);
281 if (type_unknown_p (expr))
282 return instantiate_type (type, expr, tf_warning_or_error);
284 error ("cannot convert %qE from type %qT to type %qT",
286 return error_mark_node;
289 /* Like convert, except permit conversions to take place which
290 are not normally allowed due to access restrictions
291 (such as conversion from sub-type to private super-type). */
294 convert_to_pointer_force (tree type, tree expr)
296 tree intype = TREE_TYPE (expr);
297 enum tree_code form = TREE_CODE (intype);
299 if (form == POINTER_TYPE)
301 intype = TYPE_MAIN_VARIANT (intype);
303 if (TYPE_MAIN_VARIANT (type) != intype
304 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
305 && IS_AGGR_TYPE (TREE_TYPE (type))
306 && IS_AGGR_TYPE (TREE_TYPE (intype))
307 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
309 enum tree_code code = PLUS_EXPR;
312 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
316 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
320 if (binfo == error_mark_node)
321 return error_mark_node;
324 expr = build_base_path (code, expr, binfo, 0);
325 if (expr == error_mark_node)
326 return error_mark_node;
327 /* Add any qualifier conversions. */
328 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
330 expr = build_nop (type, expr);
336 return cp_convert_to_pointer (type, expr, true);
339 /* We are passing something to a function which requires a reference.
340 The type we are interested in is in TYPE. The initial
341 value we have to begin with is in ARG.
343 FLAGS controls how we manage access checking.
344 DIRECT_BIND in FLAGS controls how any temporaries are generated.
345 If DIRECT_BIND is set, DECL is the reference we're binding to. */
348 build_up_reference (tree type, tree arg, int flags, tree decl)
351 tree argtype = TREE_TYPE (arg);
352 tree target_type = TREE_TYPE (type);
354 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
356 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
358 /* Create a new temporary variable. We can't just use a TARGET_EXPR
359 here because it needs to live as long as DECL. */
362 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
364 /* Process the initializer for the declaration. */
365 DECL_INITIAL (arg) = targ;
366 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
367 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
369 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
370 return get_target_expr (arg);
372 /* If we had a way to wrap this up, and say, if we ever needed its
373 address, transform all occurrences of the register, into a memory
374 reference we could win better. */
375 rval = build_unary_op (ADDR_EXPR, arg, 1);
376 if (rval == error_mark_node)
377 return error_mark_node;
379 if ((flags & LOOKUP_PROTECT)
380 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
381 && IS_AGGR_TYPE (argtype)
382 && IS_AGGR_TYPE (target_type))
384 /* We go through lookup_base for the access control. */
385 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
386 if (binfo == error_mark_node)
387 return error_mark_node;
388 if (binfo == NULL_TREE)
389 return error_not_base_type (target_type, argtype);
390 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
394 = convert_to_pointer_force (build_pointer_type (target_type), rval);
395 return build_nop (type, rval);
398 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
399 INTYPE is the original rvalue type and DECL is an optional _DECL node
402 [dcl.init.ref] says that if an rvalue is used to
403 initialize a reference, then the reference must be to a
404 non-volatile const type. */
407 warn_ref_binding (tree reftype, tree intype, tree decl)
409 tree ttl = TREE_TYPE (reftype);
411 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
415 if (CP_TYPE_VOLATILE_P (ttl) && decl)
416 msg = "initialization of volatile reference type %q#T from"
417 " rvalue of type %qT";
418 else if (CP_TYPE_VOLATILE_P (ttl))
419 msg = "conversion to volatile reference type %q#T "
420 " from rvalue of type %qT";
422 msg = "initialization of non-const reference type %q#T from"
423 " rvalue of type %qT";
425 msg = "conversion to non-const reference type %q#T from"
426 " rvalue of type %qT";
428 pedwarn (msg, reftype, intype);
432 /* For C++: Only need to do one-level references, but cannot
433 get tripped up on signed/unsigned differences.
435 DECL is either NULL_TREE or the _DECL node for a reference that is being
436 initialized. It can be error_mark_node if we don't know the _DECL but
437 we know it's an initialization. */
440 convert_to_reference (tree reftype, tree expr, int convtype,
441 int flags, tree decl)
443 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
445 tree rval = NULL_TREE;
446 tree rval_as_conversion = NULL_TREE;
447 bool can_convert_intype_to_type;
449 if (TREE_CODE (type) == FUNCTION_TYPE
450 && TREE_TYPE (expr) == unknown_type_node)
451 expr = instantiate_type (type, expr,
452 (flags & LOOKUP_COMPLAIN)
453 ? tf_warning_or_error : tf_none);
455 if (expr == error_mark_node)
456 return error_mark_node;
458 intype = TREE_TYPE (expr);
460 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
461 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
463 intype = TYPE_MAIN_VARIANT (intype);
465 can_convert_intype_to_type = can_convert (type, intype);
466 if (!can_convert_intype_to_type
467 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
468 && ! (flags & LOOKUP_NO_CONVERSION))
470 /* Look for a user-defined conversion to lvalue that we can use. */
473 = build_type_conversion (reftype, expr);
475 if (rval_as_conversion && rval_as_conversion != error_mark_node
476 && real_lvalue_p (rval_as_conversion))
478 expr = rval_as_conversion;
479 rval_as_conversion = NULL_TREE;
481 can_convert_intype_to_type = 1;
485 if (((convtype & CONV_STATIC) && can_convert (intype, type))
486 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
488 if (flags & LOOKUP_COMPLAIN)
490 tree ttl = TREE_TYPE (reftype);
491 tree ttr = lvalue_type (expr);
493 if (! real_lvalue_p (expr))
494 warn_ref_binding (reftype, intype, decl);
496 if (! (convtype & CONV_CONST)
497 && !at_least_as_qualified_p (ttl, ttr))
498 pedwarn ("conversion from %qT to %qT discards qualifiers",
502 return build_up_reference (reftype, expr, flags, decl);
504 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
506 /* When casting an lvalue to a reference type, just convert into
507 a pointer to the new type and deference it. This is allowed
508 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
509 should be done directly (jason). (int &)ri ---> *(int*)&ri */
511 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
513 if (TREE_CODE (intype) == POINTER_TYPE
514 && (comptypes (TREE_TYPE (intype), type,
515 COMPARE_BASE | COMPARE_DERIVED)))
516 warning (0, "casting %qT to %qT does not dereference pointer",
519 rval = build_unary_op (ADDR_EXPR, expr, 0);
520 if (rval != error_mark_node)
521 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
523 if (rval != error_mark_node)
524 rval = build1 (NOP_EXPR, reftype, rval);
528 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
530 if (rval == NULL_TREE || rval == error_mark_node)
532 warn_ref_binding (reftype, intype, decl);
533 rval = build_up_reference (reftype, rval, flags, decl);
538 /* If we found a way to convert earlier, then use it. */
542 if (flags & LOOKUP_COMPLAIN)
543 error ("cannot convert type %qT to type %qT", intype, reftype);
545 return error_mark_node;
548 /* We are using a reference VAL for its value. Bash that reference all the
549 way down to its lowest form. */
552 convert_from_reference (tree val)
554 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
556 tree t = canonical_type_variant (TREE_TYPE (TREE_TYPE (val)));
557 tree ref = build1 (INDIRECT_REF, t, val);
559 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
560 so that we get the proper error message if the result is used
561 to assign to. Also, &* is supposed to be a no-op. */
562 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
563 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
564 TREE_SIDE_EFFECTS (ref)
565 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
566 REFERENCE_REF_P (ref) = 1;
573 /* Really perform an lvalue-to-rvalue conversion, including copying an
574 argument of class type into a temporary. */
577 force_rvalue (tree expr)
579 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
580 expr = ocp_convert (TREE_TYPE (expr), expr,
581 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
583 expr = decay_conversion (expr);
588 /* C++ conversions, preference to static cast conversions. */
591 cp_convert (tree type, tree expr)
593 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
596 /* C++ equivalent of convert_and_check but using cp_convert as the
599 Convert EXPR to TYPE, warning about conversion problems with constants.
600 Invoke this function on every expression that is converted implicitly,
601 i.e. because of language rules and not because of an explicit cast. */
604 cp_convert_and_check (tree type, tree expr)
608 if (TREE_TYPE (expr) == type)
611 result = cp_convert (type, expr);
613 if (!skip_evaluation && !TREE_OVERFLOW_P (expr) && result != error_mark_node)
614 warnings_for_convert_and_check (type, expr, result);
621 FLAGS indicates how we should behave. */
624 ocp_convert (tree type, tree expr, int convtype, int flags)
627 enum tree_code code = TREE_CODE (type);
628 const char *invalid_conv_diag;
630 if (error_operand_p (e) || type == error_mark_node)
631 return error_mark_node;
633 complete_type (type);
634 complete_type (TREE_TYPE (expr));
636 if ((invalid_conv_diag
637 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
639 error (invalid_conv_diag);
640 return error_mark_node;
643 e = integral_constant_value (e);
645 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
646 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
647 don't go through finish_struct, so they don't have the synthesized
648 constructors. So don't force a temporary. */
649 && TYPE_HAS_CONSTRUCTOR (type))
650 /* We need a new temporary; don't take this shortcut. */;
651 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
653 if (same_type_p (type, TREE_TYPE (e)))
654 /* The call to fold will not always remove the NOP_EXPR as
655 might be expected, since if one of the types is a typedef;
656 the comparison in fold is just equality of pointers, not a
657 call to comptypes. We don't call fold in this case because
658 that can result in infinite recursion; fold will call
659 convert, which will call ocp_convert, etc. */
661 /* For complex data types, we need to perform componentwise
663 else if (TREE_CODE (type) == COMPLEX_TYPE)
664 return fold_if_not_in_template (convert_to_complex (type, e));
665 else if (TREE_CODE (e) == TARGET_EXPR)
667 /* Don't build a NOP_EXPR of class type. Instead, change the
668 type of the temporary. Only allow this for cv-qual changes,
670 gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
671 TYPE_MAIN_VARIANT (type)));
672 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
677 /* We shouldn't be treating objects of ADDRESSABLE type as
679 gcc_assert (!TREE_ADDRESSABLE (type));
680 return fold_if_not_in_template (build_nop (type, e));
684 if (code == VOID_TYPE && (convtype & CONV_STATIC))
686 e = convert_to_void (e, /*implicit=*/NULL);
690 if (INTEGRAL_CODE_P (code))
692 tree intype = TREE_TYPE (e);
693 /* enum = enum, enum = int, enum = float, (enum)pointer are all
695 if (TREE_CODE (type) == ENUMERAL_TYPE
696 && (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
697 || TREE_CODE (intype) == REAL_TYPE)
698 && ! (convtype & CONV_STATIC))
699 || TREE_CODE (intype) == POINTER_TYPE))
701 if (flags & LOOKUP_COMPLAIN)
702 pedwarn ("conversion from %q#T to %q#T", intype, type);
704 if (flag_pedantic_errors)
705 return error_mark_node;
707 if (IS_AGGR_TYPE (intype))
710 rval = build_type_conversion (type, e);
713 if (flags & LOOKUP_COMPLAIN)
714 error ("%q#T used where a %qT was expected", intype, type);
715 return error_mark_node;
717 if (code == BOOLEAN_TYPE)
718 return cp_truthvalue_conversion (e);
720 return fold_if_not_in_template (convert_to_integer (type, e));
722 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
723 return fold_if_not_in_template (cp_convert_to_pointer (type, e, false));
724 if (code == VECTOR_TYPE)
726 tree in_vtype = TREE_TYPE (e);
727 if (IS_AGGR_TYPE (in_vtype))
730 ret_val = build_type_conversion (type, e);
733 if (flags & LOOKUP_COMPLAIN)
734 error ("%q#T used where a %qT was expected", in_vtype, type);
735 return error_mark_node;
737 return fold_if_not_in_template (convert_to_vector (type, e));
739 if (code == REAL_TYPE || code == COMPLEX_TYPE)
741 if (IS_AGGR_TYPE (TREE_TYPE (e)))
744 rval = build_type_conversion (type, e);
748 if (flags & LOOKUP_COMPLAIN)
749 error ("%q#T used where a floating point value was expected",
752 if (code == REAL_TYPE)
753 return fold_if_not_in_template (convert_to_real (type, e));
754 else if (code == COMPLEX_TYPE)
755 return fold_if_not_in_template (convert_to_complex (type, e));
758 /* New C++ semantics: since assignment is now based on
759 memberwise copying, if the rhs type is derived from the
760 lhs type, then we may still do a conversion. */
761 if (IS_AGGR_TYPE_CODE (code))
763 tree dtype = TREE_TYPE (e);
764 tree ctor = NULL_TREE;
766 dtype = TYPE_MAIN_VARIANT (dtype);
768 /* Conversion between aggregate types. New C++ semantics allow
769 objects of derived type to be cast to objects of base type.
770 Old semantics only allowed this between pointers.
772 There may be some ambiguity between using a constructor
773 vs. using a type conversion operator when both apply. */
777 if (abstract_virtuals_error (NULL_TREE, type))
778 return error_mark_node;
780 if ((flags & LOOKUP_ONLYCONVERTING)
781 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
782 /* For copy-initialization, first we create a temp of the proper type
783 with a user-defined conversion sequence, then we direct-initialize
784 the target with the temp (see [dcl.init]). */
785 ctor = build_user_type_conversion (type, ctor, flags);
787 ctor = build_special_member_call (NULL_TREE,
788 complete_ctor_identifier,
789 build_tree_list (NULL_TREE, ctor),
792 return build_cplus_new (type, ctor);
795 if (flags & LOOKUP_COMPLAIN)
796 error ("conversion from %qT to non-scalar type %qT requested",
797 TREE_TYPE (expr), type);
798 return error_mark_node;
801 /* When an expression is used in a void context, its value is discarded and
802 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
803 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
804 in a void context. The C++ standard does not define what an `access' to an
805 object is, but there is reason to believe that it is the lvalue to rvalue
806 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
807 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
808 indicates that volatile semantics should be the same between C and C++
809 where ever possible. C leaves it implementation defined as to what
810 constitutes an access to a volatile. So, we interpret `*vp' as a read of
811 the volatile object `vp' points to, unless that is an incomplete type. For
812 volatile references we do not do this interpretation, because that would
813 make it impossible to ignore the reference return value from functions. We
814 issue warnings in the confusing cases.
816 IMPLICIT is tells us the context of an implicit void conversion. */
819 convert_to_void (tree expr, const char *implicit)
821 if (expr == error_mark_node
822 || TREE_TYPE (expr) == error_mark_node)
823 return error_mark_node;
824 if (!TREE_TYPE (expr))
826 if (invalid_nonstatic_memfn_p (expr))
827 return error_mark_node;
828 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
830 error ("pseudo-destructor is not called");
831 return error_mark_node;
833 if (VOID_TYPE_P (TREE_TYPE (expr)))
835 switch (TREE_CODE (expr))
839 /* The two parts of a cond expr might be separate lvalues. */
840 tree op1 = TREE_OPERAND (expr,1);
841 tree op2 = TREE_OPERAND (expr,2);
842 tree new_op1 = convert_to_void
843 (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
844 ? "second operand of conditional" : NULL));
845 tree new_op2 = convert_to_void
846 (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
847 ? "third operand of conditional" : NULL));
849 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
850 TREE_OPERAND (expr, 0), new_op1, new_op2);
856 /* The second part of a compound expr contains the value. */
857 tree op1 = TREE_OPERAND (expr,1);
858 tree new_op1 = convert_to_void
859 (op1, (implicit && !TREE_NO_WARNING (expr)
860 ? "right-hand operand of comma" : NULL));
864 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
865 TREE_OPERAND (expr, 0), new_op1);
872 case NON_LVALUE_EXPR:
874 /* These have already decayed to rvalue. */
877 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
882 tree type = TREE_TYPE (expr);
883 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
885 int is_volatile = TYPE_VOLATILE (type);
886 int is_complete = COMPLETE_TYPE_P (complete_type (type));
888 /* Can't load the value if we don't know the type. */
889 if (is_volatile && !is_complete)
890 warning (0, "object of incomplete type %qT will not be accessed in %s",
891 type, implicit ? implicit : "void context");
892 /* Don't load the value if this is an implicit dereference, or if
893 the type needs to be handled by ctors/dtors. */
894 else if (is_volatile && (is_reference || TREE_ADDRESSABLE (type)))
895 warning (0, "object of type %qT will not be accessed in %s",
896 TREE_TYPE (TREE_OPERAND (expr, 0)),
897 implicit ? implicit : "void context");
898 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
899 expr = TREE_OPERAND (expr, 0);
906 /* External variables might be incomplete. */
907 tree type = TREE_TYPE (expr);
908 int is_complete = COMPLETE_TYPE_P (complete_type (type));
910 if (TYPE_VOLATILE (type) && !is_complete)
911 warning (0, "object %qE of incomplete type %qT will not be accessed in %s",
912 expr, type, implicit ? implicit : "void context");
917 /* Don't bother with the temporary object returned from a function if
918 we don't use it and don't need to destroy it. We'll still
919 allocate space for it in expand_call or declare_return_variable,
920 but we don't need to track it through all the tree phases. */
921 if (TARGET_EXPR_IMPLICIT_P (expr)
922 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
924 tree init = TARGET_EXPR_INITIAL (expr);
925 if (TREE_CODE (init) == AGGR_INIT_EXPR
926 && !AGGR_INIT_VIA_CTOR_P (init))
928 tree fn = AGGR_INIT_EXPR_FN (init);
929 expr = build_call_array (TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
931 aggr_init_expr_nargs (init),
932 AGGR_INIT_EXPR_ARGP (init));
942 if (TREE_CODE (probe) == ADDR_EXPR)
943 probe = TREE_OPERAND (expr, 0);
944 if (type_unknown_p (probe))
946 /* [over.over] enumerates the places where we can take the address
947 of an overloaded function, and this is not one of them. */
948 pedwarn ("%s cannot resolve address of overloaded function",
949 implicit ? implicit : "void cast");
950 expr = void_zero_node;
952 else if (implicit && probe == expr && is_overloaded_fn (probe))
954 /* Only warn when there is no &. */
955 warning (OPT_Waddress, "%s is a reference, not call, to function %qE",
957 if (TREE_CODE (expr) == COMPONENT_REF)
958 expr = TREE_OPERAND (expr, 0);
962 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
966 && !TREE_NO_WARNING (expr)
967 && !processing_template_decl)
969 /* The middle end does not warn about expressions that have
970 been explicitly cast to void, so we must do so here. */
971 if (!TREE_SIDE_EFFECTS (expr))
972 warning (OPT_Wunused_value, "%s has no effect", implicit);
977 enum tree_code_class class;
980 /* We might like to warn about (say) "(int) f()", as the
981 cast has no effect, but the compiler itself will
982 generate implicit conversions under some
983 circumstances. (For example a block copy will be
984 turned into a call to "__builtin_memcpy", with a
985 conversion of the return value to an appropriate
986 type.) So, to avoid false positives, we strip
987 conversions. Do not use STRIP_NOPs because it will
988 not strip conversions to "void", as that is not a
989 mode-preserving conversion. */
990 while (TREE_CODE (e) == NOP_EXPR)
991 e = TREE_OPERAND (e, 0);
993 code = TREE_CODE (e);
994 class = TREE_CODE_CLASS (code);
995 if (class == tcc_comparison
996 || class == tcc_unary
997 || (class == tcc_binary
998 && !(code == MODIFY_EXPR
1000 || code == PREDECREMENT_EXPR
1001 || code == PREINCREMENT_EXPR
1002 || code == POSTDECREMENT_EXPR
1003 || code == POSTINCREMENT_EXPR)))
1004 warning (OPT_Wunused_value, "value computed is not used");
1007 expr = build1 (CONVERT_EXPR, void_type_node, expr);
1009 if (! TREE_SIDE_EFFECTS (expr))
1010 expr = void_zero_node;
1014 /* Create an expression whose value is that of EXPR,
1015 converted to type TYPE. The TREE_TYPE of the value
1016 is always TYPE. This function implements all reasonable
1017 conversions; callers should filter out those that are
1018 not permitted by the language being compiled.
1020 Most of this routine is from build_reinterpret_cast.
1022 The back end cannot call cp_convert (what was convert) because
1023 conversions to/from basetypes may involve memory references
1024 (vbases) and adding or subtracting small values (multiple
1025 inheritance), but it calls convert from the constant folding code
1026 on subtrees of already built trees after it has ripped them apart.
1028 Also, if we ever support range variables, we'll probably also have to
1029 do a little bit more work. */
1032 convert (tree type, tree expr)
1036 if (type == error_mark_node || expr == error_mark_node)
1037 return error_mark_node;
1039 intype = TREE_TYPE (expr);
1041 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1042 return fold_if_not_in_template (build_nop (type, expr));
1044 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1045 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1048 /* Like cp_convert, except permit conversions to take place which
1049 are not normally allowed due to access restrictions
1050 (such as conversion from sub-type to private super-type). */
1053 convert_force (tree type, tree expr, int convtype)
1056 enum tree_code code = TREE_CODE (type);
1058 if (code == REFERENCE_TYPE)
1059 return (fold_if_not_in_template
1060 (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1063 if (code == POINTER_TYPE)
1064 return fold_if_not_in_template (convert_to_pointer_force (type, e));
1066 /* From typeck.c convert_for_assignment */
1067 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1068 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1069 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1070 || integer_zerop (e)
1071 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1072 && TYPE_PTRMEMFUNC_P (type))
1073 /* compatible pointer to member functions. */
1074 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1077 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1080 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1081 exists, return the attempted conversion. This may
1082 return ERROR_MARK_NODE if the conversion is not
1083 allowed (references private members, etc).
1084 If no conversion exists, NULL_TREE is returned.
1086 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1087 object parameter, or by the second standard conversion sequence if
1088 that doesn't do it. This will probably wait for an overloading rewrite.
1092 build_type_conversion (tree xtype, tree expr)
1094 /* C++: check to see if we can convert this aggregate type
1095 into the required type. */
1096 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1099 /* Convert the given EXPR to one of a group of types suitable for use in an
1100 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1101 which indicates which types are suitable. If COMPLAIN is true, complain
1102 about ambiguity; otherwise, the caller will deal with it. */
1105 build_expr_type_conversion (int desires, tree expr, bool complain)
1107 tree basetype = TREE_TYPE (expr);
1108 tree conv = NULL_TREE;
1109 tree winner = NULL_TREE;
1111 if (expr == null_node
1112 && (desires & WANT_INT)
1113 && !(desires & WANT_NULL))
1114 warning (OPT_Wconversion, "converting NULL to non-pointer type");
1116 basetype = TREE_TYPE (expr);
1118 if (basetype == error_mark_node)
1119 return error_mark_node;
1121 if (! IS_AGGR_TYPE (basetype))
1122 switch (TREE_CODE (basetype))
1125 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1127 /* else fall through... */
1130 return (desires & WANT_INT) ? expr : NULL_TREE;
1132 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1134 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1136 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1140 return (desires & WANT_POINTER) ? decay_conversion (expr)
1144 if ((desires & WANT_VECTOR) == 0)
1146 switch (TREE_CODE (TREE_TYPE (basetype)))
1150 return (desires & WANT_INT) ? expr : NULL_TREE;
1152 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1154 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1163 /* The code for conversions from class type is currently only used for
1164 delete expressions. Other expressions are handled by build_new_op. */
1165 if (!complete_type_or_else (basetype, expr))
1166 return error_mark_node;
1167 if (!TYPE_HAS_CONVERSION (basetype))
1170 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1174 tree cand = TREE_VALUE (conv);
1176 if (winner && winner == cand)
1179 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1181 switch (TREE_CODE (candidate))
1185 win = (desires & WANT_INT); break;
1187 win = (desires & WANT_ENUM); break;
1189 win = (desires & WANT_FLOAT); break;
1191 win = (desires & WANT_POINTER); break;
1194 if ((desires & WANT_VECTOR) == 0)
1196 switch (TREE_CODE (TREE_TYPE (candidate)))
1200 win = (desires & WANT_INT); break;
1202 win = (desires & WANT_ENUM); break;
1204 win = (desires & WANT_FLOAT); break;
1220 error ("ambiguous default type conversion from %qT",
1222 error (" candidate conversions include %qD and %qD",
1225 return error_mark_node;
1234 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1235 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1241 /* Implements integral promotion (4.1) and float->double promotion. */
1244 type_promotes_to (tree type)
1246 if (type == error_mark_node)
1247 return error_mark_node;
1249 type = TYPE_MAIN_VARIANT (type);
1251 /* bool always promotes to int (not unsigned), even if it's the same
1253 if (type == boolean_type_node)
1254 type = integer_type_node;
1256 /* Normally convert enums to int, but convert wide enums to something
1258 else if (TREE_CODE (type) == ENUMERAL_TYPE
1259 || type == wchar_type_node)
1261 int precision = MAX (TYPE_PRECISION (type),
1262 TYPE_PRECISION (integer_type_node));
1263 tree totype = c_common_type_for_size (precision, 0);
1264 if (TYPE_UNSIGNED (type)
1265 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1266 type = c_common_type_for_size (precision, 1);
1270 else if (c_promoting_integer_type_p (type))
1272 /* Retain unsignedness if really not getting bigger. */
1273 if (TYPE_UNSIGNED (type)
1274 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1275 type = unsigned_type_node;
1277 type = integer_type_node;
1279 else if (type == float_type_node)
1280 type = double_type_node;
1285 /* The routines below this point are carefully written to conform to
1286 the standard. They use the same terminology, and follow the rules
1287 closely. Although they are used only in pt.c at the moment, they
1288 should presumably be used everywhere in the future. */
1290 /* Attempt to perform qualification conversions on EXPR to convert it
1291 to TYPE. Return the resulting expression, or error_mark_node if
1292 the conversion was impossible. */
1295 perform_qualification_conversions (tree type, tree expr)
1299 expr_type = TREE_TYPE (expr);
1301 if (same_type_p (type, expr_type))
1303 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1304 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1305 return build_nop (type, expr);
1306 else if (TYPE_PTR_TO_MEMBER_P (type)
1307 && TYPE_PTR_TO_MEMBER_P (expr_type)
1308 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1309 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1310 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1311 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1312 return build_nop (type, expr);
1314 return error_mark_node;