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 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 register tree intype
= TREE_TYPE (expr
);
80 register enum tree_code form
;
83 if (IS_AGGR_TYPE (intype
))
85 intype
= complete_type (intype
);
86 if (!COMPLETE_TYPE_P (intype
))
88 error ("can't convert from incomplete type `%T' to `%T'",
90 return error_mark_node
;
93 rval
= build_type_conversion (type
, expr
);
96 if (rval
== error_mark_node
)
97 error ("conversion of `%E' from `%T' to `%T' is ambiguous",
103 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
104 if (TREE_CODE (type
) == POINTER_TYPE
105 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
106 || VOID_TYPE_P (TREE_TYPE (type
))))
108 /* Allow an implicit this pointer for pointer to member
110 if (TYPE_PTRMEMFUNC_P (intype
))
112 tree fntype
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype
));
113 tree decl
= maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype
), 0);
114 expr
= build (OFFSET_REF
, fntype
, decl
, expr
);
117 if (TREE_CODE (expr
) == OFFSET_REF
118 && TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
119 expr
= resolve_offset_ref (expr
);
120 if (TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
121 expr
= build_addr_func (expr
);
122 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
124 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == METHOD_TYPE
)
125 if (pedantic
|| warn_pmf2ptr
)
126 pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr
),
128 return build1 (NOP_EXPR
, type
, expr
);
130 intype
= TREE_TYPE (expr
);
133 if (expr
== error_mark_node
)
134 return error_mark_node
;
136 form
= TREE_CODE (intype
);
138 if (POINTER_TYPE_P (intype
))
140 intype
= TYPE_MAIN_VARIANT (intype
);
142 if (TYPE_MAIN_VARIANT (type
) != intype
143 && TREE_CODE (type
) == POINTER_TYPE
144 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
145 && IS_AGGR_TYPE (TREE_TYPE (type
))
146 && IS_AGGR_TYPE (TREE_TYPE (intype
))
147 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
149 enum tree_code code
= PLUS_EXPR
;
155 intype_class
= TREE_TYPE (intype
);
156 type_class
= TREE_TYPE (type
);
158 same_p
= same_type_p (TYPE_MAIN_VARIANT (intype_class
),
159 TYPE_MAIN_VARIANT (type_class
));
161 /* Try derived to base conversion. */
163 binfo
= lookup_base (intype_class
, type_class
, ba_check
, NULL
);
164 if (!same_p
&& !binfo
)
166 /* Try base to derived conversion. */
167 binfo
= lookup_base (type_class
, intype_class
, ba_check
, NULL
);
170 if (binfo
== error_mark_node
)
171 return error_mark_node
;
175 expr
= build_base_path (code
, expr
, binfo
, 0);
176 /* Add any qualifier conversions. */
177 return build_nop (type
, expr
);
181 if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
186 enum tree_code code
= PLUS_EXPR
;
189 b1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (type
));
190 b2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
));
191 binfo
= lookup_base (b1
, b2
, ba_check
, &bk
);
194 binfo
= lookup_base (b2
, b1
, ba_check
, &bk
);
197 if (binfo
== error_mark_node
)
198 return error_mark_node
;
200 if (bk
== bk_via_virtual
)
203 warning ("pointer to member cast from `%T' to `%T' is via virtual base",
204 TREE_TYPE (intype
), TREE_TYPE (type
));
207 error ("pointer to member cast from `%T' to `%T' is via virtual base",
208 TREE_TYPE (intype
), TREE_TYPE (type
));
209 return error_mark_node
;
211 /* This is a reinterpret cast, whose result is unspecified.
212 We choose to do nothing. */
213 return build1 (NOP_EXPR
, type
, expr
);
216 if (TREE_CODE (expr
) == PTRMEM_CST
)
217 expr
= cplus_expand_constant (expr
);
220 expr
= size_binop (code
, convert (sizetype
, expr
),
221 BINFO_OFFSET (binfo
));
223 else if (TYPE_PTRMEMFUNC_P (type
))
225 error ("cannot convert `%E' from type `%T' to type `%T'",
227 return error_mark_node
;
230 return build_nop (type
, expr
);
232 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
233 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
234 else if (TYPE_PTRMEMFUNC_P (intype
))
236 error ("cannot convert `%E' from type `%T' to type `%T'",
238 return error_mark_node
;
241 my_friendly_assert (form
!= OFFSET_TYPE
, 186);
243 if (integer_zerop (expr
))
245 if (TYPE_PTRMEMFUNC_P (type
))
246 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
248 if (TYPE_PTRMEM_P (type
))
249 /* A NULL pointer-to-member is represented by -1, not by
251 expr
= build_int_2 (-1, -1);
253 expr
= build_int_2 (0, 0);
254 TREE_TYPE (expr
) = type
;
255 /* Fix up the representation of -1 if appropriate. */
256 force_fit_type (expr
, 0);
259 else if ((TYPE_PTRMEM_P (type
) || TYPE_PTRMEMFUNC_P (type
))
260 && INTEGRAL_CODE_P (form
))
262 error ("invalid conversion from '%T' to '%T'", intype
, type
);
263 return error_mark_node
;
266 if (INTEGRAL_CODE_P (form
))
268 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
269 return build1 (CONVERT_EXPR
, type
, expr
);
270 expr
= cp_convert (c_common_type_for_size (POINTER_SIZE
, 0), expr
);
271 /* Modes may be different but sizes should be the same. */
272 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
273 != GET_MODE_SIZE (TYPE_MODE (type
)))
274 /* There is supposed to be some integral type
275 that is the same width as a pointer. */
277 return convert_to_pointer (type
, expr
);
280 if (type_unknown_p (expr
))
281 return instantiate_type (type
, expr
, tf_error
| tf_warning
);
283 error ("cannot convert `%E' from type `%T' to type `%T'",
285 return error_mark_node
;
288 /* Like convert, except permit conversions to take place which
289 are not normally allowed due to access restrictions
290 (such as conversion from sub-type to private super-type). */
293 convert_to_pointer_force (tree type
, tree expr
)
295 register tree intype
= TREE_TYPE (expr
);
296 register enum tree_code form
= TREE_CODE (intype
);
298 if (form
== POINTER_TYPE
)
300 intype
= TYPE_MAIN_VARIANT (intype
);
302 if (TYPE_MAIN_VARIANT (type
) != intype
303 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
304 && IS_AGGR_TYPE (TREE_TYPE (type
))
305 && IS_AGGR_TYPE (TREE_TYPE (intype
))
306 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
308 enum tree_code code
= PLUS_EXPR
;
311 binfo
= lookup_base (TREE_TYPE (intype
), TREE_TYPE (type
),
315 binfo
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
319 if (binfo
== error_mark_node
)
320 return error_mark_node
;
323 expr
= build_base_path (code
, expr
, binfo
, 0);
324 if (expr
== error_mark_node
)
325 return error_mark_node
;
326 /* Add any qualifier conversions. */
327 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr
)),
329 expr
= build_nop (type
, expr
);
335 return cp_convert_to_pointer (type
, expr
, true);
338 /* We are passing something to a function which requires a reference.
339 The type we are interested in is in TYPE. The initial
340 value we have to begin with is in ARG.
342 FLAGS controls how we manage access checking.
343 DIRECT_BIND in FLAGS controls how any temporaries are generated.
344 If DIRECT_BIND is set, DECL is the reference we're binding to. */
347 build_up_reference (tree type
, tree arg
, int flags
, tree decl
)
350 tree argtype
= TREE_TYPE (arg
);
351 tree target_type
= TREE_TYPE (type
);
353 my_friendly_assert (TREE_CODE (type
) == REFERENCE_TYPE
, 187);
355 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
357 /* Create a new temporary variable. We can't just use a TARGET_EXPR
358 here because it needs to live as long as DECL. */
361 arg
= make_temporary_var_for_ref_to_temp (decl
, TREE_TYPE (arg
));
363 /* Process the initializer for the declaration. */
364 DECL_INITIAL (arg
) = targ
;
365 cp_finish_decl (arg
, targ
, NULL_TREE
,
366 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
368 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
369 return get_target_expr (arg
);
371 /* If we had a way to wrap this up, and say, if we ever needed its
372 address, transform all occurrences of the register, into a memory
373 reference we could win better. */
374 rval
= build_unary_op (ADDR_EXPR
, arg
, 1);
375 if (rval
== error_mark_node
)
376 return error_mark_node
;
378 if ((flags
& LOOKUP_PROTECT
)
379 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
380 && IS_AGGR_TYPE (argtype
)
381 && IS_AGGR_TYPE (target_type
))
383 /* We go through lookup_base for the access control. */
384 tree binfo
= lookup_base (argtype
, target_type
, ba_check
, NULL
);
385 if (binfo
== error_mark_node
)
386 return error_mark_node
;
387 if (binfo
== NULL_TREE
)
388 return error_not_base_type (target_type
, argtype
);
389 rval
= build_base_path (PLUS_EXPR
, rval
, binfo
, 1);
393 = convert_to_pointer_force (build_pointer_type (target_type
), rval
);
394 return build_nop (type
, rval
);
397 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
398 INTYPE is the original rvalue type and DECL is an optional _DECL node
401 [dcl.init.ref] says that if an rvalue is used to
402 initialize a reference, then the reference must be to a
403 non-volatile const type. */
406 warn_ref_binding (tree reftype
, tree intype
, tree decl
)
408 tree ttl
= TREE_TYPE (reftype
);
410 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl
))
414 if (CP_TYPE_VOLATILE_P (ttl
) && decl
)
415 msg
= "initialization of volatile reference type `%#T' from rvalue of type `%T'";
416 else if (CP_TYPE_VOLATILE_P (ttl
))
417 msg
= "conversion to volatile reference type `%#T' from rvalue of type `%T'";
419 msg
= "initialization of non-const reference type `%#T' from rvalue of type `%T'";
421 msg
= "conversion to non-const reference type `%#T' from rvalue of type `%T'";
423 pedwarn (msg
, reftype
, intype
);
427 /* For C++: Only need to do one-level references, but cannot
428 get tripped up on signed/unsigned differences.
430 DECL is either NULL_TREE or the _DECL node for a reference that is being
431 initialized. It can be error_mark_node if we don't know the _DECL but
432 we know it's an initialization. */
435 convert_to_reference (tree reftype
, tree expr
, int convtype
,
436 int flags
, tree decl
)
438 register tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
439 register tree intype
;
440 tree rval
= NULL_TREE
;
441 tree rval_as_conversion
= NULL_TREE
;
444 if (TREE_CODE (type
) == FUNCTION_TYPE
445 && TREE_TYPE (expr
) == unknown_type_node
)
446 expr
= instantiate_type (type
, expr
,
447 (flags
& LOOKUP_COMPLAIN
)
448 ? tf_error
| tf_warning
: tf_none
);
450 expr
= convert_from_reference (expr
);
452 if (expr
== error_mark_node
)
453 return error_mark_node
;
455 intype
= TREE_TYPE (expr
);
457 my_friendly_assert (TREE_CODE (intype
) != REFERENCE_TYPE
, 364);
459 intype
= TYPE_MAIN_VARIANT (intype
);
461 i
= comp_target_types (type
, intype
, 0);
463 if (i
<= 0 && (convtype
& CONV_IMPLICIT
) && IS_AGGR_TYPE (intype
)
464 && ! (flags
& LOOKUP_NO_CONVERSION
))
466 /* Look for a user-defined conversion to lvalue that we can use. */
469 = build_type_conversion (reftype
, expr
);
471 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
472 && real_lvalue_p (rval_as_conversion
))
474 expr
= rval_as_conversion
;
475 rval_as_conversion
= NULL_TREE
;
481 if (((convtype
& CONV_STATIC
) && i
== -1)
482 || ((convtype
& CONV_IMPLICIT
) && i
== 1))
484 if (flags
& LOOKUP_COMPLAIN
)
486 tree ttl
= TREE_TYPE (reftype
);
487 tree ttr
= lvalue_type (expr
);
489 if (! real_lvalue_p (expr
))
490 warn_ref_binding (reftype
, intype
, decl
);
492 if (! (convtype
& CONV_CONST
)
493 && !at_least_as_qualified_p (ttl
, ttr
))
494 pedwarn ("conversion from `%T' to `%T' discards qualifiers",
498 return build_up_reference (reftype
, expr
, flags
, decl
);
500 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
502 /* When casting an lvalue to a reference type, just convert into
503 a pointer to the new type and deference it. This is allowed
504 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
505 should be done directly (jason). (int &)ri ---> *(int*)&ri */
507 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
509 if (TREE_CODE (intype
) == POINTER_TYPE
510 && (comptypes (TREE_TYPE (intype
), type
,
511 COMPARE_BASE
| COMPARE_RELAXED
)))
512 warning ("casting `%T' to `%T' does not dereference pointer",
515 rval
= build_unary_op (ADDR_EXPR
, expr
, 0);
516 if (rval
!= error_mark_node
)
517 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
519 if (rval
!= error_mark_node
)
520 rval
= build1 (NOP_EXPR
, reftype
, rval
);
524 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
526 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
528 warn_ref_binding (reftype
, intype
, decl
);
529 rval
= build_up_reference (reftype
, rval
, flags
, decl
);
534 /* If we found a way to convert earlier, then use it. */
538 my_friendly_assert (TREE_CODE (intype
) != OFFSET_TYPE
, 189);
540 if (flags
& LOOKUP_COMPLAIN
)
541 error ("cannot convert type `%T' to type `%T'", intype
, reftype
);
543 if (flags
& LOOKUP_SPECULATIVELY
)
546 return error_mark_node
;
549 /* We are using a reference VAL for its value. Bash that reference all the
550 way down to its lowest form. */
553 convert_from_reference (tree val
)
555 tree type
= TREE_TYPE (val
);
557 if (TREE_CODE (type
) == OFFSET_TYPE
)
558 type
= TREE_TYPE (type
);
559 if (TREE_CODE (type
) == REFERENCE_TYPE
)
560 return build_indirect_ref (val
, NULL
);
564 /* Implicitly convert the lvalue EXPR to another lvalue of type TOTYPE,
565 preserving cv-qualification. */
568 convert_lvalue (tree totype
, tree expr
)
570 totype
= cp_build_qualified_type (totype
, TYPE_QUALS (TREE_TYPE (expr
)));
571 totype
= build_reference_type (totype
);
572 expr
= convert_to_reference (totype
, expr
, CONV_IMPLICIT
, LOOKUP_NORMAL
,
574 return convert_from_reference (expr
);
577 /* C++ conversions, preference to static cast conversions. */
580 cp_convert (tree type
, tree expr
)
582 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
);
587 FLAGS indicates how we should behave. */
590 ocp_convert (tree type
, tree expr
, int convtype
, int flags
)
592 register tree e
= expr
;
593 register enum tree_code code
= TREE_CODE (type
);
595 if (e
== error_mark_node
596 || TREE_TYPE (e
) == error_mark_node
)
597 return error_mark_node
;
599 complete_type (type
);
600 complete_type (TREE_TYPE (expr
));
602 e
= decl_constant_value (e
);
604 if (IS_AGGR_TYPE (type
) && (convtype
& CONV_FORCE_TEMP
)
605 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
606 don't go through finish_struct, so they don't have the synthesized
607 constructors. So don't force a temporary. */
608 && TYPE_HAS_CONSTRUCTOR (type
))
609 /* We need a new temporary; don't take this shortcut. */;
610 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (e
)))
612 if (same_type_p (type
, TREE_TYPE (e
)))
613 /* The call to fold will not always remove the NOP_EXPR as
614 might be expected, since if one of the types is a typedef;
615 the comparsion in fold is just equality of pointers, not a
616 call to comptypes. We don't call fold in this case because
617 that can result in infinite recursion; fold will call
618 convert, which will call ocp_convert, etc. */
620 /* For complex data types, we need to perform componentwise
622 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
623 return fold (convert_to_complex (type
, e
));
624 else if (TREE_CODE (e
) == TARGET_EXPR
)
626 /* Don't build a NOP_EXPR of class type. Instead, change the
627 type of the temporary. Only allow this for cv-qual changes,
629 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e
)),
630 TYPE_MAIN_VARIANT (type
)))
632 TREE_TYPE (e
) = TREE_TYPE (TARGET_EXPR_SLOT (e
)) = type
;
635 else if (CLASS_TYPE_P (type
))
638 return fold (build1 (NOP_EXPR
, type
, e
));
641 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
643 e
= convert_to_void (e
, /*implicit=*/NULL
);
647 /* Just convert to the type of the member. */
648 if (code
== OFFSET_TYPE
)
650 type
= TREE_TYPE (type
);
651 code
= TREE_CODE (type
);
654 if (TREE_CODE (e
) == OFFSET_REF
)
655 e
= resolve_offset_ref (e
);
657 if (INTEGRAL_CODE_P (code
))
659 tree intype
= TREE_TYPE (e
);
660 /* enum = enum, enum = int, enum = float, (enum)pointer are all
662 if (TREE_CODE (type
) == ENUMERAL_TYPE
663 && ((ARITHMETIC_TYPE_P (intype
) && ! (convtype
& CONV_STATIC
))
664 || (TREE_CODE (intype
) == POINTER_TYPE
)))
666 pedwarn ("conversion from `%#T' to `%#T'", intype
, type
);
668 if (flag_pedantic_errors
)
669 return error_mark_node
;
671 if (IS_AGGR_TYPE (intype
))
674 rval
= build_type_conversion (type
, e
);
677 if (flags
& LOOKUP_COMPLAIN
)
678 error ("`%#T' used where a `%T' was expected", intype
, type
);
679 if (flags
& LOOKUP_SPECULATIVELY
)
681 return error_mark_node
;
683 if (code
== BOOLEAN_TYPE
)
687 /* Common Ada/Pascal programmer's mistake. We always warn
688 about this since it is so bad. */
689 if (TREE_CODE (expr
) == FUNCTION_DECL
)
691 else if (TREE_CODE (expr
) == ADDR_EXPR
692 && TREE_CODE (TREE_OPERAND (expr
, 0)) == FUNCTION_DECL
)
693 fn
= TREE_OPERAND (expr
, 0);
694 if (fn
&& !DECL_WEAK (fn
))
695 warning ("the address of `%D', will always be `true'", fn
);
696 return cp_truthvalue_conversion (e
);
698 return fold (convert_to_integer (type
, e
));
700 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
701 || TYPE_PTRMEMFUNC_P (type
))
702 return fold (cp_convert_to_pointer (type
, e
, false));
703 if (code
== VECTOR_TYPE
)
704 return fold (convert_to_vector (type
, e
));
705 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
707 if (IS_AGGR_TYPE (TREE_TYPE (e
)))
710 rval
= build_type_conversion (type
, e
);
714 if (flags
& LOOKUP_COMPLAIN
)
715 error ("`%#T' used where a floating point value was expected",
718 if (code
== REAL_TYPE
)
719 return fold (convert_to_real (type
, e
));
720 else if (code
== COMPLEX_TYPE
)
721 return fold (convert_to_complex (type
, e
));
724 /* New C++ semantics: since assignment is now based on
725 memberwise copying, if the rhs type is derived from the
726 lhs type, then we may still do a conversion. */
727 if (IS_AGGR_TYPE_CODE (code
))
729 tree dtype
= TREE_TYPE (e
);
730 tree ctor
= NULL_TREE
;
732 dtype
= TYPE_MAIN_VARIANT (dtype
);
734 /* Conversion between aggregate types. New C++ semantics allow
735 objects of derived type to be cast to objects of base type.
736 Old semantics only allowed this between pointers.
738 There may be some ambiguity between using a constructor
739 vs. using a type conversion operator when both apply. */
743 if (abstract_virtuals_error (NULL_TREE
, type
))
744 return error_mark_node
;
746 if ((flags
& LOOKUP_ONLYCONVERTING
)
747 && ! (IS_AGGR_TYPE (dtype
) && DERIVED_FROM_P (type
, dtype
)))
748 /* For copy-initialization, first we create a temp of the proper type
749 with a user-defined conversion sequence, then we direct-initialize
750 the target with the temp (see [dcl.init]). */
751 ctor
= build_user_type_conversion (type
, ctor
, flags
);
753 ctor
= build_special_member_call (NULL_TREE
,
754 complete_ctor_identifier
,
755 build_tree_list (NULL_TREE
, ctor
),
756 TYPE_BINFO (type
), flags
);
758 return build_cplus_new (type
, ctor
);
761 if (flags
& LOOKUP_COMPLAIN
)
762 error ("conversion from `%T' to non-scalar type `%T' requested",
763 TREE_TYPE (expr
), type
);
764 if (flags
& LOOKUP_SPECULATIVELY
)
766 return error_mark_node
;
769 /* When an expression is used in a void context, its value is discarded and
770 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
771 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
772 in a void context. The C++ standard does not define what an `access' to an
773 object is, but there is reason to beleive that it is the lvalue to rvalue
774 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
775 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
776 indicates that volatile semantics should be the same between C and C++
777 where ever possible. C leaves it implementation defined as to what
778 constitutes an access to a volatile. So, we interpret `*vp' as a read of
779 the volatile object `vp' points to, unless that is an incomplete type. For
780 volatile references we do not do this interpretation, because that would
781 make it impossible to ignore the reference return value from functions. We
782 issue warnings in the confusing cases.
784 IMPLICIT is tells us the context of an implicit void conversion. */
787 convert_to_void (tree expr
, const char *implicit
)
789 if (expr
== error_mark_node
790 || TREE_TYPE (expr
) == error_mark_node
)
791 return error_mark_node
;
792 if (!TREE_TYPE (expr
))
794 if (VOID_TYPE_P (TREE_TYPE (expr
)))
796 switch (TREE_CODE (expr
))
800 /* The two parts of a cond expr might be separate lvalues. */
801 tree op1
= TREE_OPERAND (expr
,1);
802 tree op2
= TREE_OPERAND (expr
,2);
803 tree new_op1
= convert_to_void (op1
, implicit
);
804 tree new_op2
= convert_to_void (op2
, implicit
);
806 expr
= build (COND_EXPR
, TREE_TYPE (new_op1
),
807 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
813 /* The second part of a compound expr contains the value. */
814 tree op1
= TREE_OPERAND (expr
,1);
815 tree new_op1
= convert_to_void (op1
, implicit
);
819 tree t
= build (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
820 TREE_OPERAND (expr
, 0), new_op1
);
821 TREE_SIDE_EFFECTS (t
) = TREE_SIDE_EFFECTS (expr
);
822 TREE_NO_UNUSED_WARNING (t
) = TREE_NO_UNUSED_WARNING (expr
);
829 case NON_LVALUE_EXPR
:
831 /* These have already decayed to rvalue. */
834 case CALL_EXPR
: /* we have a special meaning for volatile void fn() */
839 tree type
= TREE_TYPE (expr
);
840 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
842 int is_volatile
= TYPE_VOLATILE (type
);
843 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
845 if (is_volatile
&& !is_complete
)
846 warning ("object of incomplete type `%T' will not be accessed in %s",
847 type
, implicit
? implicit
: "void context");
848 else if (is_reference
&& is_volatile
)
849 warning ("object of type `%T' will not be accessed in %s",
850 TREE_TYPE (TREE_OPERAND (expr
, 0)),
851 implicit
? implicit
: "void context");
852 if (is_reference
|| !is_volatile
|| !is_complete
)
853 expr
= TREE_OPERAND (expr
, 0);
860 /* External variables might be incomplete. */
861 tree type
= TREE_TYPE (expr
);
862 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
864 if (TYPE_VOLATILE (type
) && !is_complete
)
865 warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
866 expr
, type
, implicit
? implicit
: "void context");
871 expr
= resolve_offset_ref (expr
);
879 if (TREE_CODE (probe
) == ADDR_EXPR
)
880 probe
= TREE_OPERAND (expr
, 0);
881 if (type_unknown_p (probe
))
883 /* [over.over] enumerates the places where we can take the address
884 of an overloaded function, and this is not one of them. */
885 pedwarn ("%s cannot resolve address of overloaded function",
886 implicit
? implicit
: "void cast");
888 else if (implicit
&& probe
== expr
&& is_overloaded_fn (probe
))
889 /* Only warn when there is no &. */
890 warning ("%s is a reference, not call, to function `%E'",
894 if (expr
!= error_mark_node
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
896 /* FIXME: This is where we should check for expressions with no
897 effects. At the moment we do that in both build_x_component_expr
898 and expand_expr_stmt -- inconsistently too. For the moment
899 leave implicit void conversions unadorned so that expand_expr_stmt
900 has a chance of detecting some of the cases. */
902 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
907 /* Create an expression whose value is that of EXPR,
908 converted to type TYPE. The TREE_TYPE of the value
909 is always TYPE. This function implements all reasonable
910 conversions; callers should filter out those that are
911 not permitted by the language being compiled.
913 Most of this routine is from build_reinterpret_cast.
915 The backend cannot call cp_convert (what was convert) because
916 conversions to/from basetypes may involve memory references
917 (vbases) and adding or subtracting small values (multiple
918 inheritance), but it calls convert from the constant folding code
919 on subtrees of already built trees after it has ripped them apart.
921 Also, if we ever support range variables, we'll probably also have to
922 do a little bit more work. */
925 convert (tree type
, tree expr
)
929 if (type
== error_mark_node
|| expr
== error_mark_node
)
930 return error_mark_node
;
932 intype
= TREE_TYPE (expr
);
934 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
936 expr
= decl_constant_value (expr
);
937 return fold (build1 (NOP_EXPR
, type
, expr
));
940 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
941 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
944 /* Like cp_convert, except permit conversions to take place which
945 are not normally allowed due to access restrictions
946 (such as conversion from sub-type to private super-type). */
949 convert_force (tree type
, tree expr
, int convtype
)
951 register tree e
= expr
;
952 register enum tree_code code
= TREE_CODE (type
);
954 if (code
== REFERENCE_TYPE
)
955 return fold (convert_to_reference (type
, e
, CONV_C_CAST
, LOOKUP_COMPLAIN
,
957 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
958 e
= convert_from_reference (e
);
960 if (code
== POINTER_TYPE
)
961 return fold (convert_to_pointer_force (type
, e
));
963 /* From typeck.c convert_for_assignment */
964 if (((TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
&& TREE_CODE (e
) == ADDR_EXPR
965 && TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
966 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
968 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
969 && TYPE_PTRMEMFUNC_P (type
))
971 /* compatible pointer to member functions. */
972 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1);
975 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
);
978 /* Convert an aggregate EXPR to type XTYPE. If a conversion
979 exists, return the attempted conversion. This may
980 return ERROR_MARK_NODE if the conversion is not
981 allowed (references private members, etc).
982 If no conversion exists, NULL_TREE is returned.
984 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
985 object parameter, or by the second standard conversion sequence if
986 that doesn't do it. This will probably wait for an overloading rewrite.
990 build_type_conversion (tree xtype
, tree expr
)
992 /* C++: check to see if we can convert this aggregate type
993 into the required type. */
994 return build_user_type_conversion (xtype
, expr
, LOOKUP_NORMAL
);
997 /* Convert the given EXPR to one of a group of types suitable for use in an
998 expression. DESIRES is a combination of various WANT_* flags (q.v.)
999 which indicates which types are suitable. If COMPLAIN is true, complain
1000 about ambiguity; otherwise, the caller will deal with it. */
1003 build_expr_type_conversion (int desires
, tree expr
, bool complain
)
1005 tree basetype
= TREE_TYPE (expr
);
1006 tree conv
= NULL_TREE
;
1007 tree winner
= NULL_TREE
;
1009 if (expr
== null_node
1010 && (desires
& WANT_INT
)
1011 && !(desires
& WANT_NULL
))
1012 warning ("converting NULL to non-pointer type");
1014 if (TREE_CODE (expr
) == OFFSET_REF
)
1015 expr
= resolve_offset_ref (expr
);
1016 expr
= convert_from_reference (expr
);
1017 basetype
= TREE_TYPE (expr
);
1019 if (basetype
== error_mark_node
)
1020 return error_mark_node
;
1022 if (! IS_AGGR_TYPE (basetype
))
1023 switch (TREE_CODE (basetype
))
1026 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1028 /* else fall through... */
1031 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1033 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1035 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1037 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1041 return (desires
& WANT_POINTER
) ? default_conversion (expr
)
1047 /* The code for conversions from class type is currently only used for
1048 delete expressions. Other expressions are handled by build_new_op. */
1050 if (! TYPE_HAS_CONVERSION (basetype
))
1053 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1057 tree cand
= TREE_VALUE (conv
);
1059 if (winner
&& winner
== cand
)
1062 candidate
= TREE_TYPE (TREE_TYPE (cand
));
1063 if (TREE_CODE (candidate
) == REFERENCE_TYPE
)
1064 candidate
= TREE_TYPE (candidate
);
1066 switch (TREE_CODE (candidate
))
1070 win
= (desires
& WANT_INT
); break;
1072 win
= (desires
& WANT_ENUM
); break;
1074 win
= (desires
& WANT_FLOAT
); break;
1076 win
= (desires
& WANT_POINTER
); break;
1088 error ("ambiguous default type conversion from `%T'",
1090 error (" candidate conversions include `%D' and `%D'",
1093 return error_mark_node
;
1102 tree type
= TREE_TYPE (TREE_TYPE (winner
));
1103 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1104 type
= TREE_TYPE (type
);
1105 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
);
1111 /* Implements integral promotion (4.1) and float->double promotion. */
1114 type_promotes_to (tree type
)
1118 if (type
== error_mark_node
)
1119 return error_mark_node
;
1121 type_quals
= cp_type_quals (type
);
1122 type
= TYPE_MAIN_VARIANT (type
);
1124 /* bool always promotes to int (not unsigned), even if it's the same
1126 if (type
== boolean_type_node
)
1127 type
= integer_type_node
;
1129 /* Normally convert enums to int, but convert wide enums to something
1131 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1132 || type
== wchar_type_node
)
1134 int precision
= MAX (TYPE_PRECISION (type
),
1135 TYPE_PRECISION (integer_type_node
));
1136 tree totype
= c_common_type_for_size (precision
, 0);
1137 if (TREE_UNSIGNED (type
)
1138 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1139 type
= c_common_type_for_size (precision
, 1);
1143 else if (c_promoting_integer_type_p (type
))
1145 /* Retain unsignedness if really not getting bigger. */
1146 if (TREE_UNSIGNED (type
)
1147 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1148 type
= unsigned_type_node
;
1150 type
= integer_type_node
;
1152 else if (type
== float_type_node
)
1153 type
= double_type_node
;
1155 return cp_build_qualified_type (type
, type_quals
);
1158 /* The routines below this point are carefully written to conform to
1159 the standard. They use the same terminology, and follow the rules
1160 closely. Although they are used only in pt.c at the moment, they
1161 should presumably be used everywhere in the future. */
1163 /* Attempt to perform qualification conversions on EXPR to convert it
1164 to TYPE. Return the resulting expression, or error_mark_node if
1165 the conversion was impossible. */
1168 perform_qualification_conversions (tree type
, tree expr
)
1170 if (TREE_CODE (type
) == POINTER_TYPE
1171 && TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1172 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (TREE_TYPE (expr
))))
1173 return build1 (NOP_EXPR
, type
, expr
);
1175 return error_mark_node
;