1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987-1988, 1992-1999 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file contains the functions for converting C expressions
24 to different data types. The only entry point is `convert'.
25 Every language front end must have a `convert' function
26 but what kind of conversions it does will depend on the language. */
37 static tree cp_convert_to_pointer
PROTO((tree
, tree
));
38 static tree convert_to_pointer_force
PROTO((tree
, tree
));
39 static tree build_up_reference
PROTO((tree
, tree
, int));
41 /* Change of width--truncation and extension of integers or reals--
42 is represented with NOP_EXPR. Proper functioning of many things
43 assumes that no other conversions can be NOP_EXPRs.
45 Conversion between integer and pointer is represented with CONVERT_EXPR.
46 Converting integer to real uses FLOAT_EXPR
47 and real to integer uses FIX_TRUNC_EXPR.
49 Here is a list of all the functions that assume that widening and
50 narrowing is always done with a NOP_EXPR:
51 In convert.c, convert_to_integer.
52 In c-typeck.c, build_binary_op_nodefault (boolean ops),
53 and truthvalue_conversion.
54 In expr.c: expand_expr, for operands of a MULT_EXPR.
55 In fold-const.c: fold.
56 In tree.c: get_narrower and get_unwidened.
58 C++: in multiple-inheritance, converting between pointers may involve
59 adjusting them by a delta stored within the class definition. */
61 /* Subroutines of `convert'. */
63 /* if converting pointer to pointer
64 if dealing with classes, check for derived->base or vice versa
65 else if dealing with method pointers, delegate
67 else if converting class, pass off to build_type_conversion
68 else try C-style pointer conversion */
71 cp_convert_to_pointer (type
, expr
)
74 register tree intype
= TREE_TYPE (expr
);
75 register enum tree_code form
;
78 if (IS_AGGR_TYPE (intype
))
80 intype
= complete_type (intype
);
81 if (TYPE_SIZE (intype
) == NULL_TREE
)
83 cp_error ("can't convert from incomplete type `%T' to `%T'",
85 return error_mark_node
;
88 rval
= build_type_conversion (type
, expr
, 1);
91 if (rval
== error_mark_node
)
92 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
98 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
99 if (TREE_CODE (type
) == POINTER_TYPE
100 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
101 || TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
))
103 /* Allow an implicit this pointer for pointer to member
105 if (TYPE_PTRMEMFUNC_P (intype
))
107 tree fntype
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype
));
108 tree decl
= maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype
), 0);
109 expr
= build (OFFSET_REF
, fntype
, decl
, expr
);
112 if (TREE_CODE (expr
) == OFFSET_REF
113 && TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
114 expr
= resolve_offset_ref (expr
);
115 if (TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
116 expr
= build_addr_func (expr
);
117 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
119 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == METHOD_TYPE
)
120 if (pedantic
|| warn_pmf2ptr
)
121 cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr
),
123 return build1 (NOP_EXPR
, type
, expr
);
125 intype
= TREE_TYPE (expr
);
128 form
= TREE_CODE (intype
);
130 if (POINTER_TYPE_P (intype
))
132 intype
= TYPE_MAIN_VARIANT (intype
);
134 if (TYPE_MAIN_VARIANT (type
) != intype
135 && TREE_CODE (type
) == POINTER_TYPE
136 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
137 && IS_AGGR_TYPE (TREE_TYPE (type
))
138 && IS_AGGR_TYPE (TREE_TYPE (intype
))
139 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
140 /* If EXPR is NULL, then we don't need to do any arithmetic
145 The null pointer value is converted to the null pointer
146 value of the destination type. */
147 && !integer_zerop (expr
))
149 enum tree_code code
= PLUS_EXPR
;
150 tree binfo
= get_binfo (TREE_TYPE (type
), TREE_TYPE (intype
), 1);
151 if (binfo
== error_mark_node
)
152 return error_mark_node
;
153 if (binfo
== NULL_TREE
)
155 binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 1);
156 if (binfo
== error_mark_node
)
157 return error_mark_node
;
162 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type
))
163 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype
))
164 || ! BINFO_OFFSET_ZEROP (binfo
))
166 /* Need to get the path we took. */
169 if (code
== PLUS_EXPR
)
170 get_base_distance (TREE_TYPE (type
), TREE_TYPE (intype
),
173 get_base_distance (TREE_TYPE (intype
), TREE_TYPE (type
),
175 return build_vbase_path (code
, type
, expr
, path
, 0);
180 if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
187 b1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (type
));
188 b2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
));
189 binfo
= get_binfo (b2
, b1
, 1);
191 if (binfo
== NULL_TREE
)
193 binfo
= get_binfo (b1
, b2
, 1);
199 if (binfo
== error_mark_node
)
200 return error_mark_node
;
202 if (binfo_from_vbase (binfo
))
204 cp_error ("conversion to `%T' from pointer to member of virtual base `%T'",
206 return error_mark_node
;
209 if (TREE_CODE (expr
) == PTRMEM_CST
)
210 expr
= cplus_expand_constant (expr
);
212 if (binfo
&& ! TREE_VIA_VIRTUAL (binfo
))
213 expr
= size_binop (code
, expr
, BINFO_OFFSET (binfo
));
215 else if (TYPE_PTRMEMFUNC_P (type
))
217 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
219 return error_mark_node
;
222 rval
= build1 (NOP_EXPR
, type
, expr
);
223 TREE_CONSTANT (rval
) = TREE_CONSTANT (expr
);
226 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
227 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
228 else if (TYPE_PTRMEMFUNC_P (intype
))
230 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
232 return error_mark_node
;
235 my_friendly_assert (form
!= OFFSET_TYPE
, 186);
237 if (integer_zerop (expr
))
239 if (TYPE_PTRMEMFUNC_P (type
))
240 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
241 expr
= build_int_2 (0, 0);
242 TREE_TYPE (expr
) = type
;
246 if (INTEGRAL_CODE_P (form
))
248 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
249 return build1 (CONVERT_EXPR
, type
, expr
);
250 expr
= cp_convert (type_for_size (POINTER_SIZE
, 0), expr
);
251 /* Modes may be different but sizes should be the same. */
252 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
253 != GET_MODE_SIZE (TYPE_MODE (type
)))
254 /* There is supposed to be some integral type
255 that is the same width as a pointer. */
257 return convert_to_pointer (type
, expr
);
260 if (type_unknown_p (expr
))
261 return instantiate_type (type
, expr
, 1);
263 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
265 return error_mark_node
;
268 /* Like convert, except permit conversions to take place which
269 are not normally allowed due to access restrictions
270 (such as conversion from sub-type to private super-type). */
273 convert_to_pointer_force (type
, expr
)
276 register tree intype
= TREE_TYPE (expr
);
277 register enum tree_code form
= TREE_CODE (intype
);
279 if (integer_zerop (expr
))
281 expr
= build_int_2 (0, 0);
282 TREE_TYPE (expr
) = type
;
286 if (form
== POINTER_TYPE
)
288 intype
= TYPE_MAIN_VARIANT (intype
);
290 if (TYPE_MAIN_VARIANT (type
) != intype
291 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
292 && IS_AGGR_TYPE (TREE_TYPE (type
))
293 && IS_AGGR_TYPE (TREE_TYPE (intype
))
294 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
296 enum tree_code code
= PLUS_EXPR
;
298 int distance
= get_base_distance (TREE_TYPE (type
),
299 TREE_TYPE (intype
), 0, &path
);
303 cp_error ("type `%T' is ambiguous baseclass of `%s'",
305 TYPE_NAME_STRING (TREE_TYPE (intype
)));
306 return error_mark_node
;
310 distance
= get_base_distance (TREE_TYPE (intype
),
311 TREE_TYPE (type
), 0, &path
);
315 /* Doesn't need any special help from us. */
316 return build1 (NOP_EXPR
, type
, expr
);
320 return build_vbase_path (code
, type
, expr
, path
, 0);
324 return cp_convert_to_pointer (type
, expr
);
327 /* We are passing something to a function which requires a reference.
328 The type we are interested in is in TYPE. The initial
329 value we have to begin with is in ARG.
331 FLAGS controls how we manage access checking.
332 DIRECT_BIND in FLAGS controls how any temporaries are generated. */
335 build_up_reference (type
, arg
, flags
)
340 tree argtype
= TREE_TYPE (arg
);
341 tree target_type
= TREE_TYPE (type
);
342 tree stmt_expr
= NULL_TREE
;
344 my_friendly_assert (TREE_CODE (type
) == REFERENCE_TYPE
, 187);
346 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
348 /* Create a new temporary variable. */
350 if (toplevel_bindings_p ())
351 arg
= get_temp_name (argtype
, 1);
354 arg
= pushdecl (build_decl (VAR_DECL
, NULL_TREE
, argtype
));
355 DECL_ARTIFICIAL (arg
) = 1;
358 /* Process the initializer for the declaration. */
359 DECL_INITIAL (arg
) = targ
;
360 cp_finish_decl (arg
, targ
, NULL_TREE
, 0,
361 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
363 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
365 tree slot
= build_decl (VAR_DECL
, NULL_TREE
, argtype
);
366 DECL_ARTIFICIAL (slot
) = 1;
367 arg
= build_target_expr (slot
, arg
);
370 /* If we had a way to wrap this up, and say, if we ever needed it's
371 address, transform all occurrences of the register, into a memory
372 reference we could win better. */
373 rval
= build_unary_op (ADDR_EXPR
, arg
, 1);
374 if (rval
== error_mark_node
)
375 return error_mark_node
;
377 if ((flags
& LOOKUP_PROTECT
)
378 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
379 && IS_AGGR_TYPE (argtype
)
380 && IS_AGGR_TYPE (target_type
))
382 /* We go through get_binfo for the access control. */
383 tree binfo
= get_binfo (target_type
, argtype
, 1);
384 if (binfo
== error_mark_node
)
385 return error_mark_node
;
386 if (binfo
== NULL_TREE
)
387 return error_not_base_type (target_type
, argtype
);
388 rval
= convert_pointer_to_real (binfo
, rval
);
392 = convert_to_pointer_force (build_pointer_type (target_type
), rval
);
393 rval
= build1 (NOP_EXPR
, type
, rval
);
394 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
396 /* If we created and initialized a new temporary variable, add the
397 representation of that initialization to the RVAL. */
399 rval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
), stmt_expr
, rval
);
401 /* And return the result. */
405 /* For C++: Only need to do one-level references, but cannot
406 get tripped up on signed/unsigned differences.
408 DECL is either NULL_TREE or the _DECL node for a reference that is being
409 initialized. It can be error_mark_node if we don't know the _DECL but
410 we know it's an initialization. */
413 convert_to_reference (reftype
, expr
, convtype
, flags
, decl
)
418 register tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
419 register tree intype
= TREE_TYPE (expr
);
420 tree rval
= NULL_TREE
;
421 tree rval_as_conversion
= NULL_TREE
;
424 if (TREE_CODE (type
) == FUNCTION_TYPE
&& intype
== unknown_type_node
)
426 expr
= instantiate_type (type
, expr
,
427 (flags
& LOOKUP_COMPLAIN
) != 0);
428 if (expr
== error_mark_node
)
429 return error_mark_node
;
431 intype
= TREE_TYPE (expr
);
434 if (TREE_CODE (intype
) == REFERENCE_TYPE
)
435 my_friendly_abort (364);
437 intype
= TYPE_MAIN_VARIANT (intype
);
439 i
= comp_target_types (type
, intype
, 0);
441 if (i
<= 0 && (convtype
& CONV_IMPLICIT
) && IS_AGGR_TYPE (intype
)
442 && ! (flags
& LOOKUP_NO_CONVERSION
))
444 /* Look for a user-defined conversion to lvalue that we can use. */
447 = build_type_conversion (reftype
, expr
, 1);
449 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
450 && real_lvalue_p (rval_as_conversion
))
452 expr
= rval_as_conversion
;
453 rval_as_conversion
= NULL_TREE
;
459 if (((convtype
& CONV_STATIC
) && i
== -1)
460 || ((convtype
& CONV_IMPLICIT
) && i
== 1))
462 if (flags
& LOOKUP_COMPLAIN
)
464 tree ttl
= TREE_TYPE (reftype
);
465 tree ttr
= lvalue_type (expr
);
467 /* [dcl.init.ref] says that if an rvalue is used to
468 initialize a reference, then the reference must be to a
469 non-volatile const type. */
470 if (! real_lvalue_p (expr
)
471 && !CP_TYPE_CONST_NON_VOLATILE_P (ttl
))
475 if (CP_TYPE_VOLATILE_P (ttl
) && decl
)
476 msg
= "initialization of volatile reference type `%#T'";
477 else if (CP_TYPE_VOLATILE_P (ttl
))
478 msg
= "conversion to volatile reference type `%#T'";
480 msg
= "initialization of non-const reference type `%#T'";
482 msg
= "conversion to non-const reference type `%#T'";
484 cp_pedwarn (msg
, reftype
);
485 cp_pedwarn ("from rvalue of type `%T'", intype
);
487 else if (! (convtype
& CONV_CONST
)
488 && !at_least_as_qualified_p (ttl
, ttr
))
489 cp_pedwarn ("conversion from `%T' to `%T' discards qualifiers",
493 return build_up_reference (reftype
, expr
, flags
);
495 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
497 /* When casting an lvalue to a reference type, just convert into
498 a pointer to the new type and deference it. This is allowed
499 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
500 should be done directly (jason). (int &)ri ---> *(int*)&ri */
502 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
504 if (TREE_CODE (intype
) == POINTER_TYPE
505 && (comptypes (TREE_TYPE (intype
), type
,
506 COMPARE_BASE
| COMPARE_RELAXED
)))
507 cp_warning ("casting `%T' to `%T' does not dereference pointer",
510 rval
= build_unary_op (ADDR_EXPR
, expr
, 0);
511 if (rval
!= error_mark_node
)
512 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
514 if (rval
!= error_mark_node
)
515 rval
= build1 (NOP_EXPR
, reftype
, rval
);
519 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
521 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
523 rval
= build_up_reference (reftype
, rval
, flags
);
525 if (rval
&& ! CP_TYPE_CONST_P (TREE_TYPE (reftype
)))
526 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
532 /* If we found a way to convert earlier, then use it. */
536 my_friendly_assert (TREE_CODE (intype
) != OFFSET_TYPE
, 189);
538 if (flags
& LOOKUP_COMPLAIN
)
539 cp_error ("cannot convert type `%T' to type `%T'", intype
, reftype
);
541 if (flags
& LOOKUP_SPECULATIVELY
)
544 return error_mark_node
;
547 /* We are using a reference VAL for its value. Bash that reference all the
548 way down to its lowest form. */
551 convert_from_reference (val
)
554 tree type
= TREE_TYPE (val
);
556 if (TREE_CODE (type
) == OFFSET_TYPE
)
557 type
= TREE_TYPE (type
);
558 if (TREE_CODE (type
) == REFERENCE_TYPE
)
559 return build_indirect_ref (val
, NULL_PTR
);
563 /* Call this when we know (for any reason) that expr is not, in fact,
564 zero. This routine is like convert_pointer_to, but it pays
565 attention to which specific instance of what type we want to
566 convert to. This routine should eventually become
567 convert_to_pointer after all references to convert_to_pointer
571 convert_pointer_to_real (binfo
, expr
)
574 register tree intype
= TREE_TYPE (expr
);
578 if (intype
== error_mark_node
)
579 return error_mark_node
;
581 if (TREE_CODE (binfo
) == TREE_VEC
)
582 type
= BINFO_TYPE (binfo
);
583 else if (IS_AGGR_TYPE (binfo
))
593 ptr_type
= cp_build_qualified_type (type
,
594 CP_TYPE_QUALS (TREE_TYPE (intype
)));
595 ptr_type
= build_pointer_type (ptr_type
);
596 if (same_type_p (ptr_type
, TYPE_MAIN_VARIANT (intype
)))
599 my_friendly_assert (!integer_zerop (expr
), 191);
601 intype
= TYPE_MAIN_VARIANT (TREE_TYPE (intype
));
602 if (TREE_CODE (type
) == RECORD_TYPE
603 && TREE_CODE (intype
) == RECORD_TYPE
608 = get_base_distance (binfo
, intype
, 0, &path
);
610 /* This function shouldn't be called with unqualified arguments
611 but if it is, give them an error message that they can read. */
614 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
618 cp_error ("because `%T' is an ambiguous base class", type
);
619 return error_mark_node
;
622 return build_vbase_path (PLUS_EXPR
, ptr_type
, expr
, path
, 1);
624 rval
= build1 (NOP_EXPR
, ptr_type
,
625 TREE_CODE (expr
) == NOP_EXPR
? TREE_OPERAND (expr
, 0) : expr
);
626 TREE_CONSTANT (rval
) = TREE_CONSTANT (expr
);
630 /* Call this when we know (for any reason) that expr is
631 not, in fact, zero. This routine gets a type out of the first
632 argument and uses it to search for the type to convert to. If there
633 is more than one instance of that type in the expr, the conversion is
634 ambiguous. This routine should eventually go away, and all
635 callers should use convert_to_pointer_real. */
638 convert_pointer_to (binfo
, expr
)
643 if (TREE_CODE (binfo
) == TREE_VEC
)
644 type
= BINFO_TYPE (binfo
);
645 else if (IS_AGGR_TYPE (binfo
))
649 return convert_pointer_to_real (type
, expr
);
652 /* C++ conversions, preference to static cast conversions. */
655 cp_convert (type
, expr
)
658 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
);
663 FLAGS indicates how we should behave. */
666 ocp_convert (type
, expr
, convtype
, flags
)
670 register tree e
= expr
;
671 register enum tree_code code
= TREE_CODE (type
);
673 if (e
== error_mark_node
674 || TREE_TYPE (e
) == error_mark_node
)
675 return error_mark_node
;
677 complete_type (type
);
678 complete_type (TREE_TYPE (expr
));
680 if (TREE_READONLY_DECL_P (e
))
681 e
= decl_constant_value (e
);
683 if (IS_AGGR_TYPE (type
) && (convtype
& CONV_FORCE_TEMP
)
684 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
685 don't go through finish_struct, so they don't have the synthesized
686 constructors. So don't force a temporary. */
687 && TYPE_HAS_CONSTRUCTOR (type
))
688 /* We need a new temporary; don't take this shortcut. */;
689 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (e
)))
691 if (same_type_p (type
, TREE_TYPE (e
)))
692 /* The call to fold will not always remove the NOP_EXPR as
693 might be expected, since if one of the types is a typedef;
694 the comparsion in fold is just equality of pointers, not a
695 call to comptypes. We don't call fold in this case because
696 that can result in infinite recursion; fold will call
697 convert, which will call ocp_convert, etc. */
700 return fold (build1 (NOP_EXPR
, type
, e
));
703 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
705 e
= convert_to_void (e
, /*implicit=*/NULL
);
709 /* Just convert to the type of the member. */
710 if (code
== OFFSET_TYPE
)
712 type
= TREE_TYPE (type
);
713 code
= TREE_CODE (type
);
716 if (TREE_CODE (e
) == OFFSET_REF
)
717 e
= resolve_offset_ref (e
);
719 if (INTEGRAL_CODE_P (code
))
721 tree intype
= TREE_TYPE (e
);
722 /* enum = enum, enum = int, enum = float, (enum)pointer are all
724 if (TREE_CODE (type
) == ENUMERAL_TYPE
725 && ((ARITHMETIC_TYPE_P (intype
) && ! (convtype
& CONV_STATIC
))
726 || (TREE_CODE (intype
) == POINTER_TYPE
)))
728 cp_pedwarn ("conversion from `%#T' to `%#T'", intype
, type
);
730 if (flag_pedantic_errors
)
731 return error_mark_node
;
733 if (IS_AGGR_TYPE (intype
))
736 rval
= build_type_conversion (type
, e
, 1);
739 if (flags
& LOOKUP_COMPLAIN
)
740 cp_error ("`%#T' used where a `%T' was expected", intype
, type
);
741 if (flags
& LOOKUP_SPECULATIVELY
)
743 return error_mark_node
;
745 if (code
== BOOLEAN_TYPE
)
749 /* Common Ada/Pascal programmer's mistake. We always warn
750 about this since it is so bad. */
751 if (TREE_CODE (expr
) == FUNCTION_DECL
)
753 else if (TREE_CODE (expr
) == ADDR_EXPR
754 && TREE_CODE (TREE_OPERAND (expr
, 0)) == FUNCTION_DECL
)
755 fn
= TREE_OPERAND (expr
, 0);
757 cp_warning ("the address of `%D', will always be `true'", fn
);
758 return truthvalue_conversion (e
);
760 return fold (convert_to_integer (type
, e
));
762 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
763 || TYPE_PTRMEMFUNC_P (type
))
764 return fold (cp_convert_to_pointer (type
, e
));
765 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
767 if (IS_AGGR_TYPE (TREE_TYPE (e
)))
770 rval
= build_type_conversion (type
, e
, 1);
774 if (flags
& LOOKUP_COMPLAIN
)
775 cp_error ("`%#T' used where a floating point value was expected",
778 if (code
== REAL_TYPE
)
779 return fold (convert_to_real (type
, e
));
780 else if (code
== COMPLEX_TYPE
)
781 return fold (convert_to_complex (type
, e
));
784 /* New C++ semantics: since assignment is now based on
785 memberwise copying, if the rhs type is derived from the
786 lhs type, then we may still do a conversion. */
787 if (IS_AGGR_TYPE_CODE (code
))
789 tree dtype
= TREE_TYPE (e
);
790 tree ctor
= NULL_TREE
;
792 dtype
= TYPE_MAIN_VARIANT (dtype
);
794 /* Conversion between aggregate types. New C++ semantics allow
795 objects of derived type to be cast to objects of base type.
796 Old semantics only allowed this between pointers.
798 There may be some ambiguity between using a constructor
799 vs. using a type conversion operator when both apply. */
803 if (abstract_virtuals_error (NULL_TREE
, type
))
804 return error_mark_node
;
806 if ((flags
& LOOKUP_ONLYCONVERTING
)
807 && ! (IS_AGGR_TYPE (dtype
) && DERIVED_FROM_P (type
, dtype
)))
808 /* For copy-initialization, first we create a temp of the proper type
809 with a user-defined conversion sequence, then we direct-initialize
810 the target with the temp (see [dcl.init]). */
811 ctor
= build_user_type_conversion (type
, ctor
, flags
);
813 ctor
= build_method_call (NULL_TREE
, ctor_identifier
,
814 build_expr_list (NULL_TREE
, ctor
),
815 TYPE_BINFO (type
), flags
);
817 return build_cplus_new (type
, ctor
);
820 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
821 then it won't be hashed and hence compare as not equal,
823 if (code
== ARRAY_TYPE
824 && TREE_TYPE (TREE_TYPE (e
)) == TREE_TYPE (type
)
825 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e
)), TYPE_DOMAIN (type
)))
828 if (flags
& LOOKUP_COMPLAIN
)
829 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
830 TREE_TYPE (expr
), type
);
831 if (flags
& LOOKUP_SPECULATIVELY
)
833 return error_mark_node
;
836 /* When an expression is used in a void context, its value is discarded and
837 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
838 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
839 in a void context. The C++ standard does not define what an `access' to an
840 object is, but there is reason to beleive that it is the lvalue to rvalue
841 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
842 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
843 indicates that volatile semantics should be the same between C and C++
844 where ever possible. C leaves it implementation defined as to what
845 constitutes an access to a volatile. So, we interpret `*vp' as a read of
846 the volatile object `vp' points to, unless that is an incomplete type. For
847 volatile references we do not do this interpretation, because that would
848 make it impossible to ignore the reference return value from functions. We
849 issue warnings in the confusing cases.
851 IMPLICIT is tells us the context of an implicit void conversion. */
854 convert_to_void (expr
, implicit
)
856 const char *implicit
;
858 if (expr
== error_mark_node
)
860 if (!TREE_TYPE (expr
))
862 if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)), void_type_node
))
864 switch (TREE_CODE (expr
))
868 /* The two parts of a cond expr might be separate lvalues. */
869 tree op1
= TREE_OPERAND (expr
,1);
870 tree op2
= TREE_OPERAND (expr
,2);
871 tree new_op1
= convert_to_void (op1
, implicit
);
872 tree new_op2
= convert_to_void (op2
, implicit
);
874 if (new_op1
!= op1
|| new_op2
!= op2
)
875 expr
= build (COND_EXPR
,
876 implicit
? TREE_TYPE (expr
) : void_type_node
,
877 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
883 /* The second part of a compound expr contains the value. */
884 tree op1
= TREE_OPERAND (expr
,1);
885 tree new_op1
= convert_to_void (op1
, implicit
);
888 expr
= build (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
889 TREE_OPERAND (expr
, 0), new_op1
);
893 case NON_LVALUE_EXPR
:
895 /* These have already decayed to rvalue. */
898 case CALL_EXPR
: /* we have a special meaning for volatile void fn() */
903 tree type
= TREE_TYPE (expr
);
904 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
906 int is_volatile
= TYPE_VOLATILE (type
);
907 int is_complete
= TYPE_SIZE (complete_type (type
)) != NULL_TREE
;
909 if (is_volatile
&& !is_complete
)
910 cp_warning ("object of incomplete type `%T' will not be accessed in %s",
911 type
, implicit
? implicit
: "void context");
912 else if (is_reference
&& is_volatile
)
913 cp_warning ("object of type `%T' will not be accessed in %s",
914 TREE_TYPE (TREE_OPERAND (expr
, 0)),
915 implicit
? implicit
: "void context");
916 if (is_reference
|| !is_volatile
|| !is_complete
)
917 expr
= TREE_OPERAND (expr
, 0);
924 /* External variables might be incomplete. */
925 tree type
= TREE_TYPE (expr
);
926 int is_complete
= TYPE_SIZE (complete_type (type
)) != NULL_TREE
;
928 if (TYPE_VOLATILE (type
) && !is_complete
)
929 cp_warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
930 expr
, type
, implicit
? implicit
: "void context");
939 if (TREE_CODE (probe
) == ADDR_EXPR
)
940 probe
= TREE_OPERAND (expr
, 0);
941 if (!is_overloaded_fn (probe
))
943 else if (really_overloaded_fn (probe
))
945 /* [over.over] enumerates the places where we can take the address
946 of an overloaded function, and this is not one of them. */
947 cp_pedwarn ("%s has no context for overloaded function name `%E'",
948 implicit
? implicit
: "void cast", expr
);
950 else if (implicit
&& probe
== expr
)
951 /* Only warn when there is no &. */
952 cp_warning ("%s is a reference, not call, to function `%E'",
956 if (expr
!= error_mark_node
957 && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)), void_type_node
))
959 /* FIXME: This is where we should check for expressions with no
960 effects. At the moment we do that in both build_x_component_expr
961 and expand_expr_stmt -- inconsistently too. For the moment
962 leave implicit void conversions unadorned so that expand_expr_stmt
963 has a chance of detecting some of the cases. */
965 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
970 /* Create an expression whose value is that of EXPR,
971 converted to type TYPE. The TREE_TYPE of the value
972 is always TYPE. This function implements all reasonable
973 conversions; callers should filter out those that are
974 not permitted by the language being compiled.
976 Most of this routine is from build_reinterpret_cast.
978 The backend cannot call cp_convert (what was convert) because
979 conversions to/from basetypes may involve memory references
980 (vbases) and adding or subtracting small values (multiple
981 inheritance), but it calls convert from the constant folding code
982 on subtrees of already build trees after it has ripped them apart.
984 Also, if we ever support range variables, we'll probably also have to
985 do a little bit more work. */
993 if (type
== error_mark_node
|| expr
== error_mark_node
)
994 return error_mark_node
;
996 intype
= TREE_TYPE (expr
);
998 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
1000 if (TREE_READONLY_DECL_P (expr
))
1001 expr
= decl_constant_value (expr
);
1002 return fold (build1 (NOP_EXPR
, type
, expr
));
1005 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
1006 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
1009 /* Like cp_convert, except permit conversions to take place which
1010 are not normally allowed due to access restrictions
1011 (such as conversion from sub-type to private super-type). */
1014 convert_force (type
, expr
, convtype
)
1019 register tree e
= expr
;
1020 register enum tree_code code
= TREE_CODE (type
);
1022 if (code
== REFERENCE_TYPE
)
1023 return fold (convert_to_reference (type
, e
, CONV_C_CAST
, LOOKUP_COMPLAIN
,
1025 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
1026 e
= convert_from_reference (e
);
1028 if (code
== POINTER_TYPE
)
1029 return fold (convert_to_pointer_force (type
, e
));
1031 /* From typeck.c convert_for_assignment */
1032 if (((TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
&& TREE_CODE (e
) == ADDR_EXPR
1033 && TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
1034 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1035 || integer_zerop (e
)
1036 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1037 && TYPE_PTRMEMFUNC_P (type
))
1039 /* compatible pointer to member functions. */
1040 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1);
1043 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
);
1046 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1047 exists, return the attempted conversion. This may
1048 return ERROR_MARK_NODE if the conversion is not
1049 allowed (references private members, etc).
1050 If no conversion exists, NULL_TREE is returned.
1052 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1053 to take place immediately. Otherwise, we build a SAVE_EXPR
1054 which can be evaluated if the results are ever needed.
1056 Changes to this functions should be mirrored in user_harshness.
1058 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1059 object parameter, or by the second standard conversion sequence if
1060 that doesn't do it. This will probably wait for an overloading rewrite.
1064 build_type_conversion (xtype
, expr
, for_sure
)
1068 /* C++: check to see if we can convert this aggregate type
1069 into the required type. */
1070 return build_user_type_conversion
1071 (xtype
, expr
, for_sure
? LOOKUP_NORMAL
: 0);
1074 /* Convert the given EXPR to one of a group of types suitable for use in an
1075 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1076 which indicates which types are suitable. If COMPLAIN is 1, complain
1077 about ambiguity; otherwise, the caller will deal with it. */
1080 build_expr_type_conversion (desires
, expr
, complain
)
1085 tree basetype
= TREE_TYPE (expr
);
1086 tree conv
= NULL_TREE
;
1087 tree winner
= NULL_TREE
;
1089 if (expr
== null_node
1090 && (desires
& WANT_INT
)
1091 && !(desires
& WANT_NULL
))
1092 cp_warning ("converting NULL to non-pointer type");
1094 if (TREE_CODE (expr
) == OFFSET_REF
)
1095 expr
= resolve_offset_ref (expr
);
1096 expr
= convert_from_reference (expr
);
1097 basetype
= TREE_TYPE (expr
);
1099 if (! IS_AGGR_TYPE (basetype
))
1100 switch (TREE_CODE (basetype
))
1103 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1105 /* else fall through... */
1108 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1110 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1112 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1114 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1118 return (desires
& WANT_POINTER
) ? default_conversion (expr
)
1124 /* The code for conversions from class type is currently only used for
1125 delete expressions. Other expressions are handled by build_new_op. */
1127 if (! TYPE_HAS_CONVERSION (basetype
))
1130 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1134 tree cand
= TREE_VALUE (conv
);
1136 if (winner
&& winner
== cand
)
1139 candidate
= TREE_TYPE (TREE_TYPE (cand
));
1140 if (TREE_CODE (candidate
) == REFERENCE_TYPE
)
1141 candidate
= TREE_TYPE (candidate
);
1143 switch (TREE_CODE (candidate
))
1147 win
= (desires
& WANT_INT
); break;
1149 win
= (desires
& WANT_ENUM
); break;
1151 win
= (desires
& WANT_FLOAT
); break;
1153 win
= (desires
& WANT_POINTER
); break;
1165 cp_error ("ambiguous default type conversion from `%T'",
1167 cp_error (" candidate conversions include `%D' and `%D'",
1170 return error_mark_node
;
1179 tree type
= TREE_TYPE (TREE_TYPE (winner
));
1180 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1181 type
= TREE_TYPE (type
);
1182 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
);
1188 /* Implements integral promotion (4.1) and float->double promotion. */
1191 type_promotes_to (type
)
1196 if (type
== error_mark_node
)
1197 return error_mark_node
;
1199 type_quals
= CP_TYPE_QUALS (type
);
1200 type
= TYPE_MAIN_VARIANT (type
);
1202 /* bool always promotes to int (not unsigned), even if it's the same
1204 if (type
== boolean_type_node
)
1205 type
= integer_type_node
;
1207 /* Normally convert enums to int, but convert wide enums to something
1209 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1210 || type
== wchar_type_node
)
1212 int precision
= MAX (TYPE_PRECISION (type
),
1213 TYPE_PRECISION (integer_type_node
));
1214 tree totype
= type_for_size (precision
, 0);
1215 if (TREE_UNSIGNED (type
)
1216 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1217 type
= type_for_size (precision
, 1);
1221 else if (C_PROMOTING_INTEGER_TYPE_P (type
))
1223 /* Retain unsignedness if really not getting bigger. */
1224 if (TREE_UNSIGNED (type
)
1225 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1226 type
= unsigned_type_node
;
1228 type
= integer_type_node
;
1230 else if (type
== float_type_node
)
1231 type
= double_type_node
;
1233 return cp_build_qualified_type (type
, type_quals
);
1236 /* The routines below this point are carefully written to conform to
1237 the standard. They use the same terminology, and follow the rules
1238 closely. Although they are used only in pt.c at the moment, they
1239 should presumably be used everywhere in the future. */
1241 /* Attempt to perform qualification conversions on EXPR to convert it
1242 to TYPE. Return the resulting expression, or error_mark_node if
1243 the conversion was impossible. */
1246 perform_qualification_conversions (type
, expr
)
1250 if (TREE_CODE (type
) == POINTER_TYPE
1251 && TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1252 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (TREE_TYPE (expr
))))
1253 return build1 (NOP_EXPR
, type
, expr
);
1255 return error_mark_node
;