1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC 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 GNU CC 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 GNU CC; 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. */
38 static tree cp_convert_to_pointer
PARAMS ((tree
, tree
));
39 static tree convert_to_pointer_force
PARAMS ((tree
, tree
));
40 static tree build_up_reference
PARAMS ((tree
, tree
, int));
41 static void warn_ref_binding
PARAMS ((tree
, tree
, tree
));
43 /* Change of width--truncation and extension of integers or reals--
44 is represented with NOP_EXPR. Proper functioning of many things
45 assumes that no other conversions can be NOP_EXPRs.
47 Conversion between integer and pointer is represented with CONVERT_EXPR.
48 Converting integer to real uses FLOAT_EXPR
49 and real to integer uses FIX_TRUNC_EXPR.
51 Here is a list of all the functions that assume that widening and
52 narrowing is always done with a NOP_EXPR:
53 In convert.c, convert_to_integer.
54 In c-typeck.c, build_binary_op_nodefault (boolean ops),
55 and truthvalue_conversion.
56 In expr.c: expand_expr, for operands of a MULT_EXPR.
57 In fold-const.c: fold.
58 In tree.c: get_narrower and get_unwidened.
60 C++: in multiple-inheritance, converting between pointers may involve
61 adjusting them by a delta stored within the class definition. */
63 /* Subroutines of `convert'. */
65 /* if converting pointer to pointer
66 if dealing with classes, check for derived->base or vice versa
67 else if dealing with method pointers, delegate
69 else if converting class, pass off to build_type_conversion
70 else try C-style pointer conversion */
73 cp_convert_to_pointer (type
, expr
)
76 register tree intype
= TREE_TYPE (expr
);
77 register enum tree_code form
;
80 if (IS_AGGR_TYPE (intype
))
82 intype
= complete_type (intype
);
83 if (!COMPLETE_TYPE_P (intype
))
85 cp_error ("can't convert from incomplete type `%T' to `%T'",
87 return error_mark_node
;
90 rval
= build_type_conversion (type
, expr
, 1);
93 if (rval
== error_mark_node
)
94 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
100 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
101 if (TREE_CODE (type
) == POINTER_TYPE
102 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
103 || VOID_TYPE_P (TREE_TYPE (type
))))
105 /* Allow an implicit this pointer for pointer to member
107 if (TYPE_PTRMEMFUNC_P (intype
))
109 tree fntype
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype
));
110 tree decl
= maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype
), 0);
111 expr
= build (OFFSET_REF
, fntype
, decl
, expr
);
114 if (TREE_CODE (expr
) == OFFSET_REF
115 && TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
116 expr
= resolve_offset_ref (expr
);
117 if (TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
118 expr
= build_addr_func (expr
);
119 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
121 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == METHOD_TYPE
)
122 if (pedantic
|| warn_pmf2ptr
)
123 cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr
),
125 return build1 (NOP_EXPR
, type
, expr
);
127 intype
= TREE_TYPE (expr
);
130 form
= TREE_CODE (intype
);
132 if (POINTER_TYPE_P (intype
))
134 intype
= TYPE_MAIN_VARIANT (intype
);
136 if (TYPE_MAIN_VARIANT (type
) != intype
137 && TREE_CODE (type
) == POINTER_TYPE
138 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
139 && IS_AGGR_TYPE (TREE_TYPE (type
))
140 && IS_AGGR_TYPE (TREE_TYPE (intype
))
141 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
142 /* If EXPR is NULL, then we don't need to do any arithmetic
147 The null pointer value is converted to the null pointer
148 value of the destination type. */
149 && !integer_zerop (expr
))
151 enum tree_code code
= PLUS_EXPR
;
152 tree binfo
= get_binfo (TREE_TYPE (type
), TREE_TYPE (intype
), 1);
153 if (binfo
== error_mark_node
)
154 return error_mark_node
;
155 if (binfo
== NULL_TREE
)
157 binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 1);
158 if (binfo
== error_mark_node
)
159 return error_mark_node
;
164 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type
))
165 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype
))
166 || ! BINFO_OFFSET_ZEROP (binfo
))
168 /* Need to get the path we took. */
171 if (code
== PLUS_EXPR
)
172 get_base_distance (TREE_TYPE (type
), TREE_TYPE (intype
),
175 get_base_distance (TREE_TYPE (intype
), TREE_TYPE (type
),
177 return build_vbase_path (code
, type
, expr
, path
, 0);
182 if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
189 b1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (type
));
190 b2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
));
191 binfo
= get_binfo (b2
, b1
, 1);
193 if (binfo
== NULL_TREE
)
195 binfo
= get_binfo (b1
, b2
, 1);
201 if (binfo
== error_mark_node
)
202 return error_mark_node
;
204 if (binfo_from_vbase (binfo
))
206 cp_error ("conversion to `%T' from pointer to member of virtual base `%T'",
208 return error_mark_node
;
211 if (TREE_CODE (expr
) == PTRMEM_CST
)
212 expr
= cplus_expand_constant (expr
);
214 if (binfo
&& ! TREE_VIA_VIRTUAL (binfo
))
215 expr
= size_binop (code
, convert (sizetype
,expr
),
216 BINFO_OFFSET (binfo
));
218 else if (TYPE_PTRMEMFUNC_P (type
))
220 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
222 return error_mark_node
;
225 rval
= build1 (NOP_EXPR
, type
, expr
);
226 TREE_CONSTANT (rval
) = TREE_CONSTANT (expr
);
229 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
230 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
231 else if (TYPE_PTRMEMFUNC_P (intype
))
233 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
235 return error_mark_node
;
238 my_friendly_assert (form
!= OFFSET_TYPE
, 186);
240 if (integer_zerop (expr
))
242 if (TYPE_PTRMEMFUNC_P (type
))
243 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
245 if (flag_new_abi
&& TYPE_PTRMEM_P (type
))
246 /* Under the new ABI, a NULL pointer-to-member is represented
247 by -1, not by zero. */
248 expr
= build_int_2 (-1, -1);
250 expr
= build_int_2 (0, 0);
251 TREE_TYPE (expr
) = type
;
255 if (INTEGRAL_CODE_P (form
))
257 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
258 return build1 (CONVERT_EXPR
, type
, expr
);
259 expr
= cp_convert (type_for_size (POINTER_SIZE
, 0), expr
);
260 /* Modes may be different but sizes should be the same. */
261 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
262 != GET_MODE_SIZE (TYPE_MODE (type
)))
263 /* There is supposed to be some integral type
264 that is the same width as a pointer. */
266 return convert_to_pointer (type
, expr
);
269 if (type_unknown_p (expr
))
270 return instantiate_type (type
, expr
, itf_complain
);
272 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
274 return error_mark_node
;
277 /* Like convert, except permit conversions to take place which
278 are not normally allowed due to access restrictions
279 (such as conversion from sub-type to private super-type). */
282 convert_to_pointer_force (type
, expr
)
285 register tree intype
= TREE_TYPE (expr
);
286 register enum tree_code form
= TREE_CODE (intype
);
288 if (integer_zerop (expr
))
290 expr
= build_int_2 (0, 0);
291 TREE_TYPE (expr
) = type
;
295 if (form
== POINTER_TYPE
)
297 intype
= TYPE_MAIN_VARIANT (intype
);
299 if (TYPE_MAIN_VARIANT (type
) != intype
300 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
301 && IS_AGGR_TYPE (TREE_TYPE (type
))
302 && IS_AGGR_TYPE (TREE_TYPE (intype
))
303 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
305 enum tree_code code
= PLUS_EXPR
;
307 int distance
= get_base_distance (TREE_TYPE (type
),
308 TREE_TYPE (intype
), 0, &path
);
311 cp_error ("type `%T' is ambiguous base of `%T'",
314 return error_mark_node
;
318 distance
= get_base_distance (TREE_TYPE (intype
),
319 TREE_TYPE (type
), 0, &path
);
322 cp_error ("type `%T' is ambiguous base of `%T'",
325 return error_mark_node
;
328 /* Doesn't need any special help from us. */
329 return build1 (NOP_EXPR
, type
, expr
);
333 return build_vbase_path (code
, type
, expr
, path
, 0);
337 return cp_convert_to_pointer (type
, expr
);
340 /* We are passing something to a function which requires a reference.
341 The type we are interested in is in TYPE. The initial
342 value we have to begin with is in ARG.
344 FLAGS controls how we manage access checking.
345 DIRECT_BIND in FLAGS controls how any temporaries are generated. */
348 build_up_reference (type
, arg
, flags
)
353 tree argtype
= TREE_TYPE (arg
);
354 tree target_type
= TREE_TYPE (type
);
355 tree stmt_expr
= NULL_TREE
;
357 my_friendly_assert (TREE_CODE (type
) == REFERENCE_TYPE
, 187);
359 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
361 /* Create a new temporary variable. */
363 if (toplevel_bindings_p ())
364 arg
= get_temp_name (argtype
);
367 arg
= pushdecl (build_decl (VAR_DECL
, NULL_TREE
, argtype
));
368 DECL_ARTIFICIAL (arg
) = 1;
371 /* Process the initializer for the declaration. */
372 DECL_INITIAL (arg
) = targ
;
373 cp_finish_decl (arg
, targ
, NULL_TREE
,
374 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
376 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
377 return get_target_expr (arg
);
379 /* If we had a way to wrap this up, and say, if we ever needed it's
380 address, transform all occurrences of the register, into a memory
381 reference we could win better. */
382 rval
= build_unary_op (ADDR_EXPR
, arg
, 1);
383 if (rval
== error_mark_node
)
384 return error_mark_node
;
386 if ((flags
& LOOKUP_PROTECT
)
387 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
388 && IS_AGGR_TYPE (argtype
)
389 && IS_AGGR_TYPE (target_type
))
391 /* We go through get_binfo for the access control. */
392 tree binfo
= get_binfo (target_type
, argtype
, 1);
393 if (binfo
== error_mark_node
)
394 return error_mark_node
;
395 if (binfo
== NULL_TREE
)
396 return error_not_base_type (target_type
, argtype
);
397 rval
= convert_pointer_to_real (binfo
, rval
);
401 = convert_to_pointer_force (build_pointer_type (target_type
), rval
);
402 rval
= build1 (NOP_EXPR
, type
, rval
);
403 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
405 /* If we created and initialized a new temporary variable, add the
406 representation of that initialization to the RVAL. */
408 rval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
), stmt_expr
, rval
);
410 /* And return the result. */
414 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
415 INTYPE is the original rvalue type and DECL is an optional _DECL node
418 [dcl.init.ref] says that if an rvalue is used to
419 initialize a reference, then the reference must be to a
420 non-volatile const type. */
423 warn_ref_binding (reftype
, intype
, decl
)
424 tree reftype
, intype
, 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 cp_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 (reftype
, expr
, convtype
, flags
, decl
)
458 register tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
459 register tree intype
= TREE_TYPE (expr
);
460 tree rval
= NULL_TREE
;
461 tree rval_as_conversion
= NULL_TREE
;
464 if (TREE_CODE (type
) == FUNCTION_TYPE
&& intype
== unknown_type_node
)
466 expr
= instantiate_type (type
, expr
,
467 (flags
& LOOKUP_COMPLAIN
)
468 ? itf_complain
: itf_none
);
469 if (expr
== error_mark_node
)
470 return error_mark_node
;
472 intype
= TREE_TYPE (expr
);
475 if (TREE_CODE (intype
) == REFERENCE_TYPE
)
476 my_friendly_abort (364);
478 intype
= TYPE_MAIN_VARIANT (intype
);
480 i
= comp_target_types (type
, intype
, 0);
482 if (i
<= 0 && (convtype
& CONV_IMPLICIT
) && IS_AGGR_TYPE (intype
)
483 && ! (flags
& LOOKUP_NO_CONVERSION
))
485 /* Look for a user-defined conversion to lvalue that we can use. */
488 = build_type_conversion (reftype
, expr
, 1);
490 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
491 && real_lvalue_p (rval_as_conversion
))
493 expr
= rval_as_conversion
;
494 rval_as_conversion
= NULL_TREE
;
500 if (((convtype
& CONV_STATIC
) && i
== -1)
501 || ((convtype
& CONV_IMPLICIT
) && i
== 1))
503 if (flags
& LOOKUP_COMPLAIN
)
505 tree ttl
= TREE_TYPE (reftype
);
506 tree ttr
= lvalue_type (expr
);
508 if (! real_lvalue_p (expr
))
509 warn_ref_binding (reftype
, intype
, decl
);
511 if (! (convtype
& CONV_CONST
)
512 && !at_least_as_qualified_p (ttl
, ttr
))
513 cp_pedwarn ("conversion from `%T' to `%T' discards qualifiers",
517 return build_up_reference (reftype
, expr
, flags
);
519 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
521 /* When casting an lvalue to a reference type, just convert into
522 a pointer to the new type and deference it. This is allowed
523 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
524 should be done directly (jason). (int &)ri ---> *(int*)&ri */
526 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
528 if (TREE_CODE (intype
) == POINTER_TYPE
529 && (comptypes (TREE_TYPE (intype
), type
,
530 COMPARE_BASE
| COMPARE_RELAXED
)))
531 cp_warning ("casting `%T' to `%T' does not dereference pointer",
534 rval
= build_unary_op (ADDR_EXPR
, expr
, 0);
535 if (rval
!= error_mark_node
)
536 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
538 if (rval
!= error_mark_node
)
539 rval
= build1 (NOP_EXPR
, reftype
, rval
);
543 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
545 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
547 warn_ref_binding (reftype
, intype
, decl
);
548 rval
= build_up_reference (reftype
, rval
, flags
);
553 /* If we found a way to convert earlier, then use it. */
557 my_friendly_assert (TREE_CODE (intype
) != OFFSET_TYPE
, 189);
559 if (flags
& LOOKUP_COMPLAIN
)
560 cp_error ("cannot convert type `%T' to type `%T'", intype
, reftype
);
562 if (flags
& LOOKUP_SPECULATIVELY
)
565 return error_mark_node
;
568 /* We are using a reference VAL for its value. Bash that reference all the
569 way down to its lowest form. */
572 convert_from_reference (val
)
575 tree type
= TREE_TYPE (val
);
577 if (TREE_CODE (type
) == OFFSET_TYPE
)
578 type
= TREE_TYPE (type
);
579 if (TREE_CODE (type
) == REFERENCE_TYPE
)
580 return build_indirect_ref (val
, NULL_PTR
);
584 /* Call this when we know (for any reason) that expr is not, in fact,
585 zero. This routine is like convert_pointer_to, but it pays
586 attention to which specific instance of what type we want to
587 convert to. This routine should eventually become
588 convert_to_pointer after all references to convert_to_pointer
592 convert_pointer_to_real (binfo
, expr
)
595 register tree intype
= TREE_TYPE (expr
);
599 if (intype
== error_mark_node
)
600 return error_mark_node
;
602 if (TREE_CODE (binfo
) == TREE_VEC
)
603 type
= BINFO_TYPE (binfo
);
604 else if (IS_AGGR_TYPE (binfo
))
614 ptr_type
= cp_build_qualified_type (type
,
615 CP_TYPE_QUALS (TREE_TYPE (intype
)));
616 ptr_type
= build_pointer_type (ptr_type
);
617 if (same_type_p (ptr_type
, TYPE_MAIN_VARIANT (intype
)))
620 my_friendly_assert (!integer_zerop (expr
), 191);
622 intype
= TYPE_MAIN_VARIANT (TREE_TYPE (intype
));
623 if (TREE_CODE (type
) == RECORD_TYPE
624 && TREE_CODE (intype
) == RECORD_TYPE
629 = get_base_distance (binfo
, intype
, 0, &path
);
631 /* This function shouldn't be called with unqualified arguments
632 but if it is, give them an error message that they can read. */
635 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
639 cp_error ("because `%T' is an ambiguous base class", type
);
640 return error_mark_node
;
643 return build_vbase_path (PLUS_EXPR
, ptr_type
, expr
, path
, 1);
645 rval
= build1 (NOP_EXPR
, ptr_type
,
646 TREE_CODE (expr
) == NOP_EXPR
? TREE_OPERAND (expr
, 0) : expr
);
647 TREE_CONSTANT (rval
) = TREE_CONSTANT (expr
);
651 /* Call this when we know (for any reason) that expr is
652 not, in fact, zero. This routine gets a type out of the first
653 argument and uses it to search for the type to convert to. If there
654 is more than one instance of that type in the expr, the conversion is
655 ambiguous. This routine should eventually go away, and all
656 callers should use convert_to_pointer_real. */
659 convert_pointer_to (binfo
, expr
)
662 return convert_pointer_to_real (binfo
, expr
);
665 /* C++ conversions, preference to static cast conversions. */
668 cp_convert (type
, expr
)
671 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
);
676 FLAGS indicates how we should behave. */
679 ocp_convert (type
, expr
, convtype
, flags
)
683 register tree e
= expr
;
684 register enum tree_code code
= TREE_CODE (type
);
686 if (e
== error_mark_node
687 || TREE_TYPE (e
) == error_mark_node
)
688 return error_mark_node
;
690 complete_type (type
);
691 complete_type (TREE_TYPE (expr
));
693 e
= decl_constant_value (e
);
695 if (IS_AGGR_TYPE (type
) && (convtype
& CONV_FORCE_TEMP
)
696 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
697 don't go through finish_struct, so they don't have the synthesized
698 constructors. So don't force a temporary. */
699 && TYPE_HAS_CONSTRUCTOR (type
))
700 /* We need a new temporary; don't take this shortcut. */;
701 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (e
)))
703 if (same_type_p (type
, TREE_TYPE (e
)))
704 /* The call to fold will not always remove the NOP_EXPR as
705 might be expected, since if one of the types is a typedef;
706 the comparsion in fold is just equality of pointers, not a
707 call to comptypes. We don't call fold in this case because
708 that can result in infinite recursion; fold will call
709 convert, which will call ocp_convert, etc. */
711 /* For complex data types, we need to perform componentwise
713 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
714 return fold (convert_to_complex (type
, e
));
716 return fold (build1 (NOP_EXPR
, type
, e
));
719 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
721 e
= convert_to_void (e
, /*implicit=*/NULL
);
725 /* Just convert to the type of the member. */
726 if (code
== OFFSET_TYPE
)
728 type
= TREE_TYPE (type
);
729 code
= TREE_CODE (type
);
732 if (TREE_CODE (e
) == OFFSET_REF
)
733 e
= resolve_offset_ref (e
);
735 if (INTEGRAL_CODE_P (code
))
737 tree intype
= TREE_TYPE (e
);
738 /* enum = enum, enum = int, enum = float, (enum)pointer are all
740 if (TREE_CODE (type
) == ENUMERAL_TYPE
741 && ((ARITHMETIC_TYPE_P (intype
) && ! (convtype
& CONV_STATIC
))
742 || (TREE_CODE (intype
) == POINTER_TYPE
)))
744 cp_pedwarn ("conversion from `%#T' to `%#T'", intype
, type
);
746 if (flag_pedantic_errors
)
747 return error_mark_node
;
749 if (IS_AGGR_TYPE (intype
))
752 rval
= build_type_conversion (type
, e
, 1);
755 if (flags
& LOOKUP_COMPLAIN
)
756 cp_error ("`%#T' used where a `%T' was expected", intype
, type
);
757 if (flags
& LOOKUP_SPECULATIVELY
)
759 return error_mark_node
;
761 if (code
== BOOLEAN_TYPE
)
765 /* Common Ada/Pascal programmer's mistake. We always warn
766 about this since it is so bad. */
767 if (TREE_CODE (expr
) == FUNCTION_DECL
)
769 else if (TREE_CODE (expr
) == ADDR_EXPR
770 && TREE_CODE (TREE_OPERAND (expr
, 0)) == FUNCTION_DECL
)
771 fn
= TREE_OPERAND (expr
, 0);
773 cp_warning ("the address of `%D', will always be `true'", fn
);
774 return truthvalue_conversion (e
);
776 return fold (convert_to_integer (type
, e
));
778 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
779 || TYPE_PTRMEMFUNC_P (type
))
780 return fold (cp_convert_to_pointer (type
, e
));
781 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
783 if (IS_AGGR_TYPE (TREE_TYPE (e
)))
786 rval
= build_type_conversion (type
, e
, 1);
790 if (flags
& LOOKUP_COMPLAIN
)
791 cp_error ("`%#T' used where a floating point value was expected",
794 if (code
== REAL_TYPE
)
795 return fold (convert_to_real (type
, e
));
796 else if (code
== COMPLEX_TYPE
)
797 return fold (convert_to_complex (type
, e
));
800 /* New C++ semantics: since assignment is now based on
801 memberwise copying, if the rhs type is derived from the
802 lhs type, then we may still do a conversion. */
803 if (IS_AGGR_TYPE_CODE (code
))
805 tree dtype
= TREE_TYPE (e
);
806 tree ctor
= NULL_TREE
;
808 dtype
= TYPE_MAIN_VARIANT (dtype
);
810 /* Conversion between aggregate types. New C++ semantics allow
811 objects of derived type to be cast to objects of base type.
812 Old semantics only allowed this between pointers.
814 There may be some ambiguity between using a constructor
815 vs. using a type conversion operator when both apply. */
819 if (abstract_virtuals_error (NULL_TREE
, type
))
820 return error_mark_node
;
822 if ((flags
& LOOKUP_ONLYCONVERTING
)
823 && ! (IS_AGGR_TYPE (dtype
) && DERIVED_FROM_P (type
, dtype
)))
824 /* For copy-initialization, first we create a temp of the proper type
825 with a user-defined conversion sequence, then we direct-initialize
826 the target with the temp (see [dcl.init]). */
827 ctor
= build_user_type_conversion (type
, ctor
, flags
);
829 ctor
= build_method_call (NULL_TREE
,
830 complete_ctor_identifier
,
831 build_tree_list (NULL_TREE
, ctor
),
832 TYPE_BINFO (type
), flags
);
834 return build_cplus_new (type
, ctor
);
837 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
838 then it won't be hashed and hence compare as not equal,
840 if (code
== ARRAY_TYPE
841 && TREE_TYPE (TREE_TYPE (e
)) == TREE_TYPE (type
)
842 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e
)), TYPE_DOMAIN (type
)))
845 if (flags
& LOOKUP_COMPLAIN
)
846 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
847 TREE_TYPE (expr
), type
);
848 if (flags
& LOOKUP_SPECULATIVELY
)
850 return error_mark_node
;
853 /* When an expression is used in a void context, its value is discarded and
854 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
855 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
856 in a void context. The C++ standard does not define what an `access' to an
857 object is, but there is reason to beleive that it is the lvalue to rvalue
858 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
859 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
860 indicates that volatile semantics should be the same between C and C++
861 where ever possible. C leaves it implementation defined as to what
862 constitutes an access to a volatile. So, we interpret `*vp' as a read of
863 the volatile object `vp' points to, unless that is an incomplete type. For
864 volatile references we do not do this interpretation, because that would
865 make it impossible to ignore the reference return value from functions. We
866 issue warnings in the confusing cases.
868 IMPLICIT is tells us the context of an implicit void conversion. */
871 convert_to_void (expr
, implicit
)
873 const char *implicit
;
875 if (expr
== error_mark_node
876 || TREE_TYPE (expr
) == error_mark_node
)
877 return error_mark_node
;
878 if (!TREE_TYPE (expr
))
880 if (VOID_TYPE_P (TREE_TYPE (expr
)))
882 switch (TREE_CODE (expr
))
886 /* The two parts of a cond expr might be separate lvalues. */
887 tree op1
= TREE_OPERAND (expr
,1);
888 tree op2
= TREE_OPERAND (expr
,2);
889 tree new_op1
= convert_to_void (op1
, implicit
);
890 tree new_op2
= convert_to_void (op2
, implicit
);
892 if (new_op1
!= op1
|| new_op2
!= op2
)
893 expr
= build (COND_EXPR
,
894 implicit
? TREE_TYPE (expr
) : void_type_node
,
895 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
901 /* The second part of a compound expr contains the value. */
902 tree op1
= TREE_OPERAND (expr
,1);
903 tree new_op1
= convert_to_void (op1
, implicit
);
906 expr
= build (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
907 TREE_OPERAND (expr
, 0), new_op1
);
911 case NON_LVALUE_EXPR
:
913 /* These have already decayed to rvalue. */
916 case CALL_EXPR
: /* we have a special meaning for volatile void fn() */
921 tree type
= TREE_TYPE (expr
);
922 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
924 int is_volatile
= TYPE_VOLATILE (type
);
925 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
927 if (is_volatile
&& !is_complete
)
928 cp_warning ("object of incomplete type `%T' will not be accessed in %s",
929 type
, implicit
? implicit
: "void context");
930 else if (is_reference
&& is_volatile
)
931 cp_warning ("object of type `%T' will not be accessed in %s",
932 TREE_TYPE (TREE_OPERAND (expr
, 0)),
933 implicit
? implicit
: "void context");
934 if (is_reference
|| !is_volatile
|| !is_complete
)
935 expr
= TREE_OPERAND (expr
, 0);
942 /* External variables might be incomplete. */
943 tree type
= TREE_TYPE (expr
);
944 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
946 if (TYPE_VOLATILE (type
) && !is_complete
)
947 cp_warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
948 expr
, type
, implicit
? implicit
: "void context");
953 expr
= resolve_offset_ref (expr
);
961 if (TREE_CODE (probe
) == ADDR_EXPR
)
962 probe
= TREE_OPERAND (expr
, 0);
963 if (!is_overloaded_fn (probe
))
965 else if (really_overloaded_fn (probe
))
967 /* [over.over] enumerates the places where we can take the address
968 of an overloaded function, and this is not one of them. */
969 cp_pedwarn ("%s has no context for overloaded function name `%E'",
970 implicit
? implicit
: "void cast", expr
);
972 else if (implicit
&& probe
== expr
)
973 /* Only warn when there is no &. */
974 cp_warning ("%s is a reference, not call, to function `%E'",
978 if (expr
!= error_mark_node
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
980 /* FIXME: This is where we should check for expressions with no
981 effects. At the moment we do that in both build_x_component_expr
982 and expand_expr_stmt -- inconsistently too. For the moment
983 leave implicit void conversions unadorned so that expand_expr_stmt
984 has a chance of detecting some of the cases. */
986 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
991 /* Create an expression whose value is that of EXPR,
992 converted to type TYPE. The TREE_TYPE of the value
993 is always TYPE. This function implements all reasonable
994 conversions; callers should filter out those that are
995 not permitted by the language being compiled.
997 Most of this routine is from build_reinterpret_cast.
999 The backend cannot call cp_convert (what was convert) because
1000 conversions to/from basetypes may involve memory references
1001 (vbases) and adding or subtracting small values (multiple
1002 inheritance), but it calls convert from the constant folding code
1003 on subtrees of already build trees after it has ripped them apart.
1005 Also, if we ever support range variables, we'll probably also have to
1006 do a little bit more work. */
1009 convert (type
, expr
)
1014 if (type
== error_mark_node
|| expr
== error_mark_node
)
1015 return error_mark_node
;
1017 intype
= TREE_TYPE (expr
);
1019 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
1021 expr
= decl_constant_value (expr
);
1022 return fold (build1 (NOP_EXPR
, type
, expr
));
1025 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
1026 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
1029 /* Like cp_convert, except permit conversions to take place which
1030 are not normally allowed due to access restrictions
1031 (such as conversion from sub-type to private super-type). */
1034 convert_force (type
, expr
, convtype
)
1039 register tree e
= expr
;
1040 register enum tree_code code
= TREE_CODE (type
);
1042 if (code
== REFERENCE_TYPE
)
1043 return fold (convert_to_reference (type
, e
, CONV_C_CAST
, LOOKUP_COMPLAIN
,
1045 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
1046 e
= convert_from_reference (e
);
1048 if (code
== POINTER_TYPE
)
1049 return fold (convert_to_pointer_force (type
, e
));
1051 /* From typeck.c convert_for_assignment */
1052 if (((TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
&& TREE_CODE (e
) == ADDR_EXPR
1053 && TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
1054 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1055 || integer_zerop (e
)
1056 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1057 && TYPE_PTRMEMFUNC_P (type
))
1059 /* compatible pointer to member functions. */
1060 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1);
1063 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
);
1066 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1067 exists, return the attempted conversion. This may
1068 return ERROR_MARK_NODE if the conversion is not
1069 allowed (references private members, etc).
1070 If no conversion exists, NULL_TREE is returned.
1072 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1073 to take place immediately. Otherwise, we build a SAVE_EXPR
1074 which can be evaluated if the results are ever needed.
1076 Changes to this functions should be mirrored in user_harshness.
1078 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1079 object parameter, or by the second standard conversion sequence if
1080 that doesn't do it. This will probably wait for an overloading rewrite.
1084 build_type_conversion (xtype
, expr
, for_sure
)
1088 /* C++: check to see if we can convert this aggregate type
1089 into the required type. */
1090 return build_user_type_conversion
1091 (xtype
, expr
, for_sure
? LOOKUP_NORMAL
: 0);
1094 /* Convert the given EXPR to one of a group of types suitable for use in an
1095 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1096 which indicates which types are suitable. If COMPLAIN is 1, complain
1097 about ambiguity; otherwise, the caller will deal with it. */
1100 build_expr_type_conversion (desires
, expr
, complain
)
1105 tree basetype
= TREE_TYPE (expr
);
1106 tree conv
= NULL_TREE
;
1107 tree winner
= NULL_TREE
;
1109 if (expr
== null_node
1110 && (desires
& WANT_INT
)
1111 && !(desires
& WANT_NULL
))
1112 cp_warning ("converting NULL to non-pointer type");
1114 if (TREE_CODE (expr
) == OFFSET_REF
)
1115 expr
= resolve_offset_ref (expr
);
1116 expr
= convert_from_reference (expr
);
1117 basetype
= TREE_TYPE (expr
);
1119 if (basetype
== error_mark_node
)
1120 return error_mark_node
;
1122 if (! IS_AGGR_TYPE (basetype
))
1123 switch (TREE_CODE (basetype
))
1126 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1128 /* else fall through... */
1131 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1133 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1135 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1137 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1141 return (desires
& WANT_POINTER
) ? default_conversion (expr
)
1147 /* The code for conversions from class type is currently only used for
1148 delete expressions. Other expressions are handled by build_new_op. */
1150 if (! TYPE_HAS_CONVERSION (basetype
))
1153 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1157 tree cand
= TREE_VALUE (conv
);
1159 if (winner
&& winner
== cand
)
1162 candidate
= TREE_TYPE (TREE_TYPE (cand
));
1163 if (TREE_CODE (candidate
) == REFERENCE_TYPE
)
1164 candidate
= TREE_TYPE (candidate
);
1166 switch (TREE_CODE (candidate
))
1170 win
= (desires
& WANT_INT
); break;
1172 win
= (desires
& WANT_ENUM
); break;
1174 win
= (desires
& WANT_FLOAT
); break;
1176 win
= (desires
& WANT_POINTER
); break;
1188 cp_error ("ambiguous default type conversion from `%T'",
1190 cp_error (" candidate conversions include `%D' and `%D'",
1193 return error_mark_node
;
1202 tree type
= TREE_TYPE (TREE_TYPE (winner
));
1203 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1204 type
= TREE_TYPE (type
);
1205 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
);
1211 /* Implements integral promotion (4.1) and float->double promotion. */
1214 type_promotes_to (type
)
1219 if (type
== error_mark_node
)
1220 return error_mark_node
;
1222 type_quals
= CP_TYPE_QUALS (type
);
1223 type
= TYPE_MAIN_VARIANT (type
);
1225 /* bool always promotes to int (not unsigned), even if it's the same
1227 if (type
== boolean_type_node
)
1228 type
= integer_type_node
;
1230 /* Normally convert enums to int, but convert wide enums to something
1232 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1233 || type
== wchar_type_node
)
1235 int precision
= MAX (TYPE_PRECISION (type
),
1236 TYPE_PRECISION (integer_type_node
));
1237 tree totype
= type_for_size (precision
, 0);
1238 if (TREE_UNSIGNED (type
)
1239 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1240 type
= type_for_size (precision
, 1);
1244 else if (C_PROMOTING_INTEGER_TYPE_P (type
))
1246 /* Retain unsignedness if really not getting bigger. */
1247 if (TREE_UNSIGNED (type
)
1248 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1249 type
= unsigned_type_node
;
1251 type
= integer_type_node
;
1253 else if (type
== float_type_node
)
1254 type
= double_type_node
;
1256 return cp_build_qualified_type (type
, type_quals
);
1259 /* The routines below this point are carefully written to conform to
1260 the standard. They use the same terminology, and follow the rules
1261 closely. Although they are used only in pt.c at the moment, they
1262 should presumably be used everywhere in the future. */
1264 /* Attempt to perform qualification conversions on EXPR to convert it
1265 to TYPE. Return the resulting expression, or error_mark_node if
1266 the conversion was impossible. */
1269 perform_qualification_conversions (type
, expr
)
1273 if (TREE_CODE (type
) == POINTER_TYPE
1274 && TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1275 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (TREE_TYPE (expr
))))
1276 return build1 (NOP_EXPR
, type
, expr
);
1278 return error_mark_node
;