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 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, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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"
40 static tree
cp_convert_to_pointer (tree
, tree
, bool);
41 static tree
convert_to_pointer_force (tree
, tree
);
42 static tree
build_up_reference (tree
, tree
, int, tree
);
43 static void warn_ref_binding (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. If FORCE is true then allow
73 conversions via virtual bases (these are permitted by reinterpret_cast,
74 but not static_cast). */
77 cp_convert_to_pointer (tree type
, tree expr
, bool force
)
79 tree intype
= TREE_TYPE (expr
);
82 if (intype
== error_mark_node
)
83 return error_mark_node
;
85 if (IS_AGGR_TYPE (intype
))
87 intype
= complete_type (intype
);
88 if (!COMPLETE_TYPE_P (intype
))
90 error ("can't convert from incomplete type `%T' to `%T'",
92 return error_mark_node
;
95 rval
= build_type_conversion (type
, expr
);
98 if (rval
== error_mark_node
)
99 error ("conversion of `%E' from `%T' to `%T' is ambiguous",
105 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
106 if (TREE_CODE (type
) == POINTER_TYPE
107 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
108 || VOID_TYPE_P (TREE_TYPE (type
))))
110 /* Allow an implicit this pointer for pointer to member
112 if (TYPE_PTRMEMFUNC_P (intype
))
114 if (pedantic
|| warn_pmf2ptr
)
115 pedwarn ("converting from `%T' to `%T'", intype
, type
);
116 if (TREE_CODE (expr
) == PTRMEM_CST
)
117 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
120 tree decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
),
122 decl
= build_address (decl
);
123 expr
= get_member_function_from_ptrfunc (&decl
, expr
);
126 else if (TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
128 if (pedantic
|| warn_pmf2ptr
)
129 pedwarn ("converting from `%T' to `%T'", intype
, type
);
130 expr
= build_addr_func (expr
);
132 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
133 return build_nop (type
, expr
);
134 intype
= TREE_TYPE (expr
);
137 if (expr
== error_mark_node
)
138 return error_mark_node
;
140 form
= TREE_CODE (intype
);
142 if (POINTER_TYPE_P (intype
))
144 intype
= TYPE_MAIN_VARIANT (intype
);
146 if (TYPE_MAIN_VARIANT (type
) != intype
147 && TREE_CODE (type
) == POINTER_TYPE
148 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
149 && IS_AGGR_TYPE (TREE_TYPE (type
))
150 && IS_AGGR_TYPE (TREE_TYPE (intype
))
151 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
153 enum tree_code code
= PLUS_EXPR
;
159 intype_class
= TREE_TYPE (intype
);
160 type_class
= TREE_TYPE (type
);
162 same_p
= same_type_p (TYPE_MAIN_VARIANT (intype_class
),
163 TYPE_MAIN_VARIANT (type_class
));
165 /* Try derived to base conversion. */
167 binfo
= lookup_base (intype_class
, type_class
, ba_check
, NULL
);
168 if (!same_p
&& !binfo
)
170 /* Try base to derived conversion. */
171 binfo
= lookup_base (type_class
, intype_class
, ba_check
, NULL
);
174 if (binfo
== error_mark_node
)
175 return error_mark_node
;
179 expr
= build_base_path (code
, expr
, binfo
, 0);
180 /* Add any qualifier conversions. */
181 return build_nop (type
, expr
);
185 if (TYPE_PTRMEMFUNC_P (type
))
187 error ("cannot convert `%E' from type `%T' to type `%T'",
189 return error_mark_node
;
192 return build_nop (type
, expr
);
194 else if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
199 enum tree_code code
= PLUS_EXPR
;
202 b1
= TYPE_PTRMEM_CLASS_TYPE (type
);
203 b2
= TYPE_PTRMEM_CLASS_TYPE (intype
);
204 binfo
= lookup_base (b1
, b2
, ba_check
, &bk
);
207 binfo
= lookup_base (b2
, b1
, ba_check
, &bk
);
210 if (binfo
== error_mark_node
)
211 return error_mark_node
;
213 if (bk
== bk_via_virtual
)
216 warning ("pointer to member cast from `%T' to `%T' is via virtual base",
220 error ("pointer to member cast from `%T' to `%T' is via virtual base",
222 return error_mark_node
;
224 /* This is a reinterpret cast, whose result is unspecified.
225 We choose to do nothing. */
226 return build1 (NOP_EXPR
, type
, expr
);
229 if (TREE_CODE (expr
) == PTRMEM_CST
)
230 expr
= cplus_expand_constant (expr
);
232 if (binfo
&& !integer_zerop (BINFO_OFFSET (binfo
)))
233 expr
= size_binop (code
,
234 build_nop (sizetype
, expr
),
235 BINFO_OFFSET (binfo
));
236 return build_nop (type
, expr
);
238 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
239 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
240 else if (TYPE_PTRMEMFUNC_P (intype
))
244 if (TREE_CODE (expr
) == PTRMEM_CST
)
245 return cp_convert_to_pointer (type
,
246 PTRMEM_CST_MEMBER (expr
),
248 else if (TREE_CODE (expr
) == OFFSET_REF
)
250 tree object
= TREE_OPERAND (expr
, 0);
251 return get_member_function_from_ptrfunc (&object
,
252 TREE_OPERAND (expr
, 1));
255 error ("cannot convert `%E' from type `%T' to type `%T'",
257 return error_mark_node
;
260 if (integer_zerop (expr
))
262 if (TYPE_PTRMEMFUNC_P (type
))
263 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
265 if (TYPE_PTRMEM_P (type
))
267 /* A NULL pointer-to-member is represented by -1, not by
269 expr
= build_int_cst (type
, -1);
270 /* Fix up the representation of -1 if appropriate. */
271 expr
= force_fit_type (expr
, 0, false, false);
274 expr
= build_int_cst (type
, 0);
278 else if (TYPE_PTR_TO_MEMBER_P (type
) && INTEGRAL_CODE_P (form
))
280 error ("invalid conversion from '%T' to '%T'", intype
, type
);
281 return error_mark_node
;
284 if (INTEGRAL_CODE_P (form
))
286 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
287 return build1 (CONVERT_EXPR
, type
, expr
);
288 expr
= cp_convert (c_common_type_for_size (POINTER_SIZE
, 0), expr
);
289 /* Modes may be different but sizes should be the same. There
290 is supposed to be some integral type that is the same width
292 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
293 == GET_MODE_SIZE (TYPE_MODE (type
)));
295 return convert_to_pointer (type
, expr
);
298 if (type_unknown_p (expr
))
299 return instantiate_type (type
, expr
, tf_error
| tf_warning
);
301 error ("cannot convert `%E' from type `%T' to type `%T'",
303 return error_mark_node
;
306 /* Like convert, except permit conversions to take place which
307 are not normally allowed due to access restrictions
308 (such as conversion from sub-type to private super-type). */
311 convert_to_pointer_force (tree type
, tree expr
)
313 tree intype
= TREE_TYPE (expr
);
314 enum tree_code form
= TREE_CODE (intype
);
316 if (form
== POINTER_TYPE
)
318 intype
= TYPE_MAIN_VARIANT (intype
);
320 if (TYPE_MAIN_VARIANT (type
) != intype
321 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
322 && IS_AGGR_TYPE (TREE_TYPE (type
))
323 && IS_AGGR_TYPE (TREE_TYPE (intype
))
324 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
326 enum tree_code code
= PLUS_EXPR
;
329 binfo
= lookup_base (TREE_TYPE (intype
), TREE_TYPE (type
),
333 binfo
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
337 if (binfo
== error_mark_node
)
338 return error_mark_node
;
341 expr
= build_base_path (code
, expr
, binfo
, 0);
342 if (expr
== error_mark_node
)
343 return error_mark_node
;
344 /* Add any qualifier conversions. */
345 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr
)),
347 expr
= build_nop (type
, expr
);
353 return cp_convert_to_pointer (type
, expr
, true);
356 /* We are passing something to a function which requires a reference.
357 The type we are interested in is in TYPE. The initial
358 value we have to begin with is in ARG.
360 FLAGS controls how we manage access checking.
361 DIRECT_BIND in FLAGS controls how any temporaries are generated.
362 If DIRECT_BIND is set, DECL is the reference we're binding to. */
365 build_up_reference (tree type
, tree arg
, int flags
, tree decl
)
368 tree argtype
= TREE_TYPE (arg
);
369 tree target_type
= TREE_TYPE (type
);
371 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
373 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
375 /* Create a new temporary variable. We can't just use a TARGET_EXPR
376 here because it needs to live as long as DECL. */
379 arg
= make_temporary_var_for_ref_to_temp (decl
, TREE_TYPE (arg
));
381 /* Process the initializer for the declaration. */
382 DECL_INITIAL (arg
) = targ
;
383 cp_finish_decl (arg
, targ
, NULL_TREE
,
384 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
386 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
387 return get_target_expr (arg
);
389 /* If we had a way to wrap this up, and say, if we ever needed its
390 address, transform all occurrences of the register, into a memory
391 reference we could win better. */
392 rval
= build_unary_op (ADDR_EXPR
, arg
, 1);
393 if (rval
== error_mark_node
)
394 return error_mark_node
;
396 if ((flags
& LOOKUP_PROTECT
)
397 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
398 && IS_AGGR_TYPE (argtype
)
399 && IS_AGGR_TYPE (target_type
))
401 /* We go through lookup_base for the access control. */
402 tree binfo
= lookup_base (argtype
, target_type
, ba_check
, NULL
);
403 if (binfo
== error_mark_node
)
404 return error_mark_node
;
405 if (binfo
== NULL_TREE
)
406 return error_not_base_type (target_type
, argtype
);
407 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1);
411 = convert_to_pointer_force (build_pointer_type (target_type
), rval
);
412 return build_nop (type
, rval
);
415 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
416 INTYPE is the original rvalue type and DECL is an optional _DECL node
419 [dcl.init.ref] says that if an rvalue is used to
420 initialize a reference, then the reference must be to a
421 non-volatile const type. */
424 warn_ref_binding (tree reftype
, tree intype
, tree decl
)
426 tree ttl
= TREE_TYPE (reftype
);
428 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl
))
432 if (CP_TYPE_VOLATILE_P (ttl
) && decl
)
433 msg
= "initialization of volatile reference type `%#T' from rvalue of type `%T'";
434 else if (CP_TYPE_VOLATILE_P (ttl
))
435 msg
= "conversion to volatile reference type `%#T' from rvalue of type `%T'";
437 msg
= "initialization of non-const reference type `%#T' from rvalue of type `%T'";
439 msg
= "conversion to non-const reference type `%#T' from rvalue of type `%T'";
441 pedwarn (msg
, reftype
, intype
);
445 /* For C++: Only need to do one-level references, but cannot
446 get tripped up on signed/unsigned differences.
448 DECL is either NULL_TREE or the _DECL node for a reference that is being
449 initialized. It can be error_mark_node if we don't know the _DECL but
450 we know it's an initialization. */
453 convert_to_reference (tree reftype
, tree expr
, int convtype
,
454 int flags
, tree decl
)
456 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
458 tree rval
= NULL_TREE
;
459 tree rval_as_conversion
= NULL_TREE
;
460 bool can_convert_intype_to_type
;
462 if (TREE_CODE (type
) == FUNCTION_TYPE
463 && TREE_TYPE (expr
) == unknown_type_node
)
464 expr
= instantiate_type (type
, expr
,
465 (flags
& LOOKUP_COMPLAIN
)
466 ? tf_error
| tf_warning
: tf_none
);
468 expr
= convert_from_reference (expr
);
470 if (expr
== error_mark_node
)
471 return error_mark_node
;
473 intype
= TREE_TYPE (expr
);
475 gcc_assert (TREE_CODE (intype
) != REFERENCE_TYPE
);
477 intype
= TYPE_MAIN_VARIANT (intype
);
479 can_convert_intype_to_type
= can_convert (type
, intype
);
480 if (!can_convert_intype_to_type
481 && (convtype
& CONV_IMPLICIT
) && IS_AGGR_TYPE (intype
)
482 && ! (flags
& LOOKUP_NO_CONVERSION
))
484 /* Look for a user-defined conversion to lvalue that we can use. */
487 = build_type_conversion (reftype
, expr
);
489 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
490 && real_lvalue_p (rval_as_conversion
))
492 expr
= rval_as_conversion
;
493 rval_as_conversion
= NULL_TREE
;
495 can_convert_intype_to_type
= 1;
499 if (((convtype
& CONV_STATIC
) && can_convert (intype
, type
))
500 || ((convtype
& CONV_IMPLICIT
) && can_convert_intype_to_type
))
502 if (flags
& LOOKUP_COMPLAIN
)
504 tree ttl
= TREE_TYPE (reftype
);
505 tree ttr
= lvalue_type (expr
);
507 if (! real_lvalue_p (expr
))
508 warn_ref_binding (reftype
, intype
, decl
);
510 if (! (convtype
& CONV_CONST
)
511 && !at_least_as_qualified_p (ttl
, ttr
))
512 pedwarn ("conversion from `%T' to `%T' discards qualifiers",
516 return build_up_reference (reftype
, expr
, flags
, decl
);
518 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
520 /* When casting an lvalue to a reference type, just convert into
521 a pointer to the new type and deference it. This is allowed
522 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
523 should be done directly (jason). (int &)ri ---> *(int*)&ri */
525 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
527 if (TREE_CODE (intype
) == POINTER_TYPE
528 && (comptypes (TREE_TYPE (intype
), type
,
529 COMPARE_BASE
| COMPARE_DERIVED
)))
530 warning ("casting `%T' to `%T' does not dereference pointer",
533 rval
= build_unary_op (ADDR_EXPR
, expr
, 0);
534 if (rval
!= error_mark_node
)
535 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
537 if (rval
!= error_mark_node
)
538 rval
= build1 (NOP_EXPR
, reftype
, rval
);
542 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
544 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
546 warn_ref_binding (reftype
, intype
, decl
);
547 rval
= build_up_reference (reftype
, rval
, flags
, decl
);
552 /* If we found a way to convert earlier, then use it. */
556 if (flags
& LOOKUP_COMPLAIN
)
557 error ("cannot convert type `%T' to type `%T'", intype
, reftype
);
559 return error_mark_node
;
562 /* We are using a reference VAL for its value. Bash that reference all the
563 way down to its lowest form. */
566 convert_from_reference (tree val
)
568 if (TREE_CODE (TREE_TYPE (val
)) == REFERENCE_TYPE
)
569 return build_indirect_ref (val
, NULL
);
573 /* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
574 preserving cv-qualification. */
577 convert_lvalue (tree totype
, tree expr
)
579 totype
= cp_build_qualified_type (totype
, TYPE_QUALS (TREE_TYPE (expr
)));
580 totype
= build_reference_type (totype
);
581 expr
= convert_to_reference (totype
, expr
, CONV_IMPLICIT
, LOOKUP_NORMAL
,
583 return convert_from_reference (expr
);
586 /* Really perform an lvalue-to-rvalue conversion, including copying an
587 argument of class type into a temporary. */
590 force_rvalue (tree expr
)
592 if (IS_AGGR_TYPE (TREE_TYPE (expr
)) && TREE_CODE (expr
) != TARGET_EXPR
)
593 expr
= ocp_convert (TREE_TYPE (expr
), expr
,
594 CONV_IMPLICIT
|CONV_FORCE_TEMP
, LOOKUP_NORMAL
);
596 expr
= decay_conversion (expr
);
601 /* C++ conversions, preference to static cast conversions. */
604 cp_convert (tree type
, tree expr
)
606 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
);
611 FLAGS indicates how we should behave. */
614 ocp_convert (tree type
, tree expr
, int convtype
, int flags
)
617 enum tree_code code
= TREE_CODE (type
);
619 if (error_operand_p (e
) || type
== error_mark_node
)
620 return error_mark_node
;
622 complete_type (type
);
623 complete_type (TREE_TYPE (expr
));
625 e
= decl_constant_value (e
);
627 if (IS_AGGR_TYPE (type
) && (convtype
& CONV_FORCE_TEMP
)
628 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
629 don't go through finish_struct, so they don't have the synthesized
630 constructors. So don't force a temporary. */
631 && TYPE_HAS_CONSTRUCTOR (type
))
632 /* We need a new temporary; don't take this shortcut. */;
633 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (e
)))
635 if (same_type_p (type
, TREE_TYPE (e
)))
636 /* The call to fold will not always remove the NOP_EXPR as
637 might be expected, since if one of the types is a typedef;
638 the comparison in fold is just equality of pointers, not a
639 call to comptypes. We don't call fold in this case because
640 that can result in infinite recursion; fold will call
641 convert, which will call ocp_convert, etc. */
643 /* For complex data types, we need to perform componentwise
645 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
646 return fold_if_not_in_template (convert_to_complex (type
, e
));
647 else if (TREE_CODE (e
) == TARGET_EXPR
)
649 /* Don't build a NOP_EXPR of class type. Instead, change the
650 type of the temporary. Only allow this for cv-qual changes,
652 gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e
)),
653 TYPE_MAIN_VARIANT (type
)));
654 TREE_TYPE (e
) = TREE_TYPE (TARGET_EXPR_SLOT (e
)) = type
;
659 /* We shouldn't be treating objects of ADDRESSABLE type as
661 gcc_assert (!TREE_ADDRESSABLE (type
));
662 return fold_if_not_in_template (build_nop (type
, e
));
666 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
668 e
= convert_to_void (e
, /*implicit=*/NULL
);
672 if (INTEGRAL_CODE_P (code
))
674 tree intype
= TREE_TYPE (e
);
675 /* enum = enum, enum = int, enum = float, (enum)pointer are all
677 if (TREE_CODE (type
) == ENUMERAL_TYPE
678 && ((ARITHMETIC_TYPE_P (intype
) && ! (convtype
& CONV_STATIC
))
679 || (TREE_CODE (intype
) == POINTER_TYPE
)))
681 pedwarn ("conversion from `%#T' to `%#T'", intype
, type
);
683 if (flag_pedantic_errors
)
684 return error_mark_node
;
686 if (IS_AGGR_TYPE (intype
))
689 rval
= build_type_conversion (type
, e
);
692 if (flags
& LOOKUP_COMPLAIN
)
693 error ("`%#T' used where a `%T' was expected", intype
, type
);
694 return error_mark_node
;
696 if (code
== BOOLEAN_TYPE
)
697 return cp_truthvalue_conversion (e
);
699 return fold_if_not_in_template (convert_to_integer (type
, e
));
701 if (POINTER_TYPE_P (type
) || TYPE_PTR_TO_MEMBER_P (type
))
702 return fold_if_not_in_template (cp_convert_to_pointer (type
, e
, false));
703 if (code
== VECTOR_TYPE
)
705 tree in_vtype
= TREE_TYPE (e
);
706 if (IS_AGGR_TYPE (in_vtype
))
709 ret_val
= build_type_conversion (type
, e
);
712 if (flags
& LOOKUP_COMPLAIN
)
713 error ("`%#T' used where a `%T' was expected", in_vtype
, type
);
714 return error_mark_node
;
716 return fold_if_not_in_template (convert_to_vector (type
, e
));
718 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
720 if (IS_AGGR_TYPE (TREE_TYPE (e
)))
723 rval
= build_type_conversion (type
, e
);
727 if (flags
& LOOKUP_COMPLAIN
)
728 error ("`%#T' used where a floating point value was expected",
731 if (code
== REAL_TYPE
)
732 return fold_if_not_in_template (convert_to_real (type
, e
));
733 else if (code
== COMPLEX_TYPE
)
734 return fold_if_not_in_template (convert_to_complex (type
, e
));
737 /* New C++ semantics: since assignment is now based on
738 memberwise copying, if the rhs type is derived from the
739 lhs type, then we may still do a conversion. */
740 if (IS_AGGR_TYPE_CODE (code
))
742 tree dtype
= TREE_TYPE (e
);
743 tree ctor
= NULL_TREE
;
745 dtype
= TYPE_MAIN_VARIANT (dtype
);
747 /* Conversion between aggregate types. New C++ semantics allow
748 objects of derived type to be cast to objects of base type.
749 Old semantics only allowed this between pointers.
751 There may be some ambiguity between using a constructor
752 vs. using a type conversion operator when both apply. */
756 if (abstract_virtuals_error (NULL_TREE
, type
))
757 return error_mark_node
;
759 if ((flags
& LOOKUP_ONLYCONVERTING
)
760 && ! (IS_AGGR_TYPE (dtype
) && DERIVED_FROM_P (type
, dtype
)))
761 /* For copy-initialization, first we create a temp of the proper type
762 with a user-defined conversion sequence, then we direct-initialize
763 the target with the temp (see [dcl.init]). */
764 ctor
= build_user_type_conversion (type
, ctor
, flags
);
766 ctor
= build_special_member_call (NULL_TREE
,
767 complete_ctor_identifier
,
768 build_tree_list (NULL_TREE
, ctor
),
771 return build_cplus_new (type
, ctor
);
774 if (flags
& LOOKUP_COMPLAIN
)
775 error ("conversion from `%T' to non-scalar type `%T' requested",
776 TREE_TYPE (expr
), type
);
777 return error_mark_node
;
780 /* When an expression is used in a void context, its value is discarded and
781 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
782 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
783 in a void context. The C++ standard does not define what an `access' to an
784 object is, but there is reason to believe that it is the lvalue to rvalue
785 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
786 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
787 indicates that volatile semantics should be the same between C and C++
788 where ever possible. C leaves it implementation defined as to what
789 constitutes an access to a volatile. So, we interpret `*vp' as a read of
790 the volatile object `vp' points to, unless that is an incomplete type. For
791 volatile references we do not do this interpretation, because that would
792 make it impossible to ignore the reference return value from functions. We
793 issue warnings in the confusing cases.
795 IMPLICIT is tells us the context of an implicit void conversion. */
798 convert_to_void (tree expr
, const char *implicit
)
800 if (expr
== error_mark_node
801 || TREE_TYPE (expr
) == error_mark_node
)
802 return error_mark_node
;
803 if (!TREE_TYPE (expr
))
805 if (invalid_nonstatic_memfn_p (expr
))
806 return error_mark_node
;
807 if (VOID_TYPE_P (TREE_TYPE (expr
)))
809 switch (TREE_CODE (expr
))
813 /* The two parts of a cond expr might be separate lvalues. */
814 tree op1
= TREE_OPERAND (expr
,1);
815 tree op2
= TREE_OPERAND (expr
,2);
816 tree new_op1
= convert_to_void
817 (op1
, (implicit
&& !TREE_SIDE_EFFECTS (op2
)
818 ? "second operand of conditional" : NULL
));
819 tree new_op2
= convert_to_void
820 (op2
, (implicit
&& !TREE_SIDE_EFFECTS (op1
)
821 ? "third operand of conditional" : NULL
));
823 expr
= build3 (COND_EXPR
, TREE_TYPE (new_op1
),
824 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
830 /* The second part of a compound expr contains the value. */
831 tree op1
= TREE_OPERAND (expr
,1);
832 tree new_op1
= convert_to_void
833 (op1
, (implicit
&& !TREE_NO_WARNING (expr
)
834 ? "right-hand operand of comma" : NULL
));
838 tree t
= build2 (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
839 TREE_OPERAND (expr
, 0), new_op1
);
846 case NON_LVALUE_EXPR
:
848 /* These have already decayed to rvalue. */
851 case CALL_EXPR
: /* We have a special meaning for volatile void fn(). */
856 tree type
= TREE_TYPE (expr
);
857 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
859 int is_volatile
= TYPE_VOLATILE (type
);
860 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
862 if (is_volatile
&& !is_complete
)
863 warning ("object of incomplete type `%T' will not be accessed in %s",
864 type
, implicit
? implicit
: "void context");
865 else if (is_reference
&& is_volatile
)
866 warning ("object of type `%T' will not be accessed in %s",
867 TREE_TYPE (TREE_OPERAND (expr
, 0)),
868 implicit
? implicit
: "void context");
869 if (is_reference
|| !is_volatile
|| !is_complete
)
870 expr
= TREE_OPERAND (expr
, 0);
877 /* External variables might be incomplete. */
878 tree type
= TREE_TYPE (expr
);
879 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
881 if (TYPE_VOLATILE (type
) && !is_complete
)
882 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
883 expr
, type
, implicit
? implicit
: "void context");
892 if (TREE_CODE (probe
) == ADDR_EXPR
)
893 probe
= TREE_OPERAND (expr
, 0);
894 if (type_unknown_p (probe
))
896 /* [over.over] enumerates the places where we can take the address
897 of an overloaded function, and this is not one of them. */
898 pedwarn ("%s cannot resolve address of overloaded function",
899 implicit
? implicit
: "void cast");
900 expr
= void_zero_node
;
902 else if (implicit
&& probe
== expr
&& is_overloaded_fn (probe
))
903 /* Only warn when there is no &. */
904 warning ("%s is a reference, not call, to function `%E'",
908 if (expr
!= error_mark_node
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
910 if (implicit
&& warn_unused_value
911 && !TREE_SIDE_EFFECTS (expr
) && !TREE_NO_WARNING (expr
))
912 warning ("%s has no effect", implicit
);
913 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
918 /* Create an expression whose value is that of EXPR,
919 converted to type TYPE. The TREE_TYPE of the value
920 is always TYPE. This function implements all reasonable
921 conversions; callers should filter out those that are
922 not permitted by the language being compiled.
924 Most of this routine is from build_reinterpret_cast.
926 The backend cannot call cp_convert (what was convert) because
927 conversions to/from basetypes may involve memory references
928 (vbases) and adding or subtracting small values (multiple
929 inheritance), but it calls convert from the constant folding code
930 on subtrees of already built trees after it has ripped them apart.
932 Also, if we ever support range variables, we'll probably also have to
933 do a little bit more work. */
936 convert (tree type
, tree expr
)
940 if (type
== error_mark_node
|| expr
== error_mark_node
)
941 return error_mark_node
;
943 intype
= TREE_TYPE (expr
);
945 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
947 expr
= decl_constant_value (expr
);
948 return fold_if_not_in_template (build_nop (type
, expr
));
951 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
952 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
955 /* Like cp_convert, except permit conversions to take place which
956 are not normally allowed due to access restrictions
957 (such as conversion from sub-type to private super-type). */
960 convert_force (tree type
, tree expr
, int convtype
)
963 enum tree_code code
= TREE_CODE (type
);
965 if (code
== REFERENCE_TYPE
)
966 return (fold_if_not_in_template
967 (convert_to_reference (type
, e
, CONV_C_CAST
, LOOKUP_COMPLAIN
,
969 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
970 e
= convert_from_reference (e
);
972 if (code
== POINTER_TYPE
)
973 return fold_if_not_in_template (convert_to_pointer_force (type
, e
));
975 /* From typeck.c convert_for_assignment */
976 if (((TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
&& TREE_CODE (e
) == ADDR_EXPR
977 && TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
978 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
980 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
981 && TYPE_PTRMEMFUNC_P (type
))
983 /* compatible pointer to member functions. */
984 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1);
987 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
);
990 /* Convert an aggregate EXPR to type XTYPE. If a conversion
991 exists, return the attempted conversion. This may
992 return ERROR_MARK_NODE if the conversion is not
993 allowed (references private members, etc).
994 If no conversion exists, NULL_TREE is returned.
996 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
997 object parameter, or by the second standard conversion sequence if
998 that doesn't do it. This will probably wait for an overloading rewrite.
1002 build_type_conversion (tree xtype
, tree expr
)
1004 /* C++: check to see if we can convert this aggregate type
1005 into the required type. */
1006 return build_user_type_conversion (xtype
, expr
, LOOKUP_NORMAL
);
1009 /* Convert the given EXPR to one of a group of types suitable for use in an
1010 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1011 which indicates which types are suitable. If COMPLAIN is true, complain
1012 about ambiguity; otherwise, the caller will deal with it. */
1015 build_expr_type_conversion (int desires
, tree expr
, bool complain
)
1017 tree basetype
= TREE_TYPE (expr
);
1018 tree conv
= NULL_TREE
;
1019 tree winner
= NULL_TREE
;
1021 if (expr
== null_node
1022 && (desires
& WANT_INT
)
1023 && !(desires
& WANT_NULL
))
1024 warning ("converting NULL to non-pointer type");
1026 expr
= convert_from_reference (expr
);
1027 basetype
= TREE_TYPE (expr
);
1029 if (basetype
== error_mark_node
)
1030 return error_mark_node
;
1032 if (! IS_AGGR_TYPE (basetype
))
1033 switch (TREE_CODE (basetype
))
1036 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1038 /* else fall through... */
1041 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1043 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1045 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1047 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1051 return (desires
& WANT_POINTER
) ? decay_conversion (expr
)
1057 /* The code for conversions from class type is currently only used for
1058 delete expressions. Other expressions are handled by build_new_op. */
1059 if (!complete_type_or_else (basetype
, expr
))
1060 return error_mark_node
;
1061 if (!TYPE_HAS_CONVERSION (basetype
))
1064 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1068 tree cand
= TREE_VALUE (conv
);
1070 if (winner
&& winner
== cand
)
1073 candidate
= non_reference (TREE_TYPE (TREE_TYPE (cand
)));
1075 switch (TREE_CODE (candidate
))
1079 win
= (desires
& WANT_INT
); break;
1081 win
= (desires
& WANT_ENUM
); break;
1083 win
= (desires
& WANT_FLOAT
); break;
1085 win
= (desires
& WANT_POINTER
); break;
1097 error ("ambiguous default type conversion from `%T'",
1099 error (" candidate conversions include `%D' and `%D'",
1102 return error_mark_node
;
1111 tree type
= non_reference (TREE_TYPE (TREE_TYPE (winner
)));
1112 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
);
1118 /* Implements integral promotion (4.1) and float->double promotion. */
1121 type_promotes_to (tree type
)
1123 if (type
== error_mark_node
)
1124 return error_mark_node
;
1126 type
= TYPE_MAIN_VARIANT (type
);
1128 /* bool always promotes to int (not unsigned), even if it's the same
1130 if (type
== boolean_type_node
)
1131 type
= integer_type_node
;
1133 /* Normally convert enums to int, but convert wide enums to something
1135 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1136 || type
== wchar_type_node
)
1138 int precision
= MAX (TYPE_PRECISION (type
),
1139 TYPE_PRECISION (integer_type_node
));
1140 tree totype
= c_common_type_for_size (precision
, 0);
1141 if (TYPE_UNSIGNED (type
)
1142 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1143 type
= c_common_type_for_size (precision
, 1);
1147 else if (c_promoting_integer_type_p (type
))
1149 /* Retain unsignedness if really not getting bigger. */
1150 if (TYPE_UNSIGNED (type
)
1151 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1152 type
= unsigned_type_node
;
1154 type
= integer_type_node
;
1156 else if (type
== float_type_node
)
1157 type
= double_type_node
;
1162 /* The routines below this point are carefully written to conform to
1163 the standard. They use the same terminology, and follow the rules
1164 closely. Although they are used only in pt.c at the moment, they
1165 should presumably be used everywhere in the future. */
1167 /* Attempt to perform qualification conversions on EXPR to convert it
1168 to TYPE. Return the resulting expression, or error_mark_node if
1169 the conversion was impossible. */
1172 perform_qualification_conversions (tree type
, tree expr
)
1176 expr_type
= TREE_TYPE (expr
);
1178 if (TYPE_PTR_P (type
) && TYPE_PTR_P (expr_type
)
1179 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (expr_type
)))
1180 return build_nop (type
, expr
);
1181 else if (TYPE_PTR_TO_MEMBER_P (type
)
1182 && TYPE_PTR_TO_MEMBER_P (expr_type
)
1183 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
1184 TYPE_PTRMEM_CLASS_TYPE (expr_type
))
1185 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type
),
1186 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type
)))
1187 return build_nop (type
, expr
);
1189 return error_mark_node
;