1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 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. */
35 extern tree static_aggregates
;
37 /* Change of width--truncation and extension of integers or reals--
38 is represented with NOP_EXPR. Proper functioning of many things
39 assumes that no other conversions can be NOP_EXPRs.
41 Conversion between integer and pointer is represented with CONVERT_EXPR.
42 Converting integer to real uses FLOAT_EXPR
43 and real to integer uses FIX_TRUNC_EXPR.
45 Here is a list of all the functions that assume that widening and
46 narrowing is always done with a NOP_EXPR:
47 In convert.c, convert_to_integer.
48 In c-typeck.c, build_binary_op_nodefault (boolean ops),
49 and truthvalue_conversion.
50 In expr.c: expand_expr, for operands of a MULT_EXPR.
51 In fold-const.c: fold.
52 In tree.c: get_narrower and get_unwidened.
54 C++: in multiple-inheritance, converting between pointers may involve
55 adjusting them by a delta stored within the class definition. */
57 /* Subroutines of `convert'. */
59 /* Build a thunk. What it is, is an entry point that when called will
60 adjust the this pointer (the first argument) by offset, and then
61 goto the real address of the function given by REAL_ADDR that we
62 would like called. What we return is the address of the thunk. */
65 build_thunk (offset
, real_addr
)
66 tree offset
, real_addr
;
68 if (TREE_CODE (real_addr
) != ADDR_EXPR
69 || TREE_CODE (TREE_OPERAND (real_addr
, 0)) != FUNCTION_DECL
)
71 sorry ("MI pointer to member conversion too complex");
72 return error_mark_node
;
74 sorry ("MI pointer to member conversion too complex");
75 return error_mark_node
;
78 /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
79 another `pointer to method'. This may involved the creation of
80 a thunk to handle the this offset calculation. */
83 convert_fn_ptr (type
, expr
)
86 #if 0 /* We don't use thunks for pmfs. */
87 if (flag_vtable_thunks
)
89 tree intype
= TREE_TYPE (expr
);
90 tree binfo
= get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype
)),
91 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)), 1);
92 if (binfo
== error_mark_node
)
94 error (" in pointer to member conversion");
95 return error_mark_node
;
97 if (binfo
== NULL_TREE
)
99 /* ARM 4.8 restriction. */
100 error ("invalid pointer to member conversion");
101 return error_mark_node
;
104 if (BINFO_OFFSET_ZEROP (binfo
))
105 return build1 (NOP_EXPR
, type
, expr
);
106 return build1 (NOP_EXPR
, type
, build_thunk (BINFO_OFFSET (binfo
), expr
));
110 return build_ptrmemfunc (type
, expr
, 1);
113 /* if converting pointer to pointer
114 if dealing with classes, check for derived->base or vice versa
115 else if dealing with method pointers, delegate
117 else if converting class, pass off to build_type_conversion
118 else try C-style pointer conversion */
121 cp_convert_to_pointer (type
, expr
)
124 register tree intype
= TREE_TYPE (expr
);
125 register enum tree_code form
;
127 if (IS_AGGR_TYPE (intype
))
131 intype
= complete_type (intype
);
132 if (TYPE_SIZE (intype
) == NULL_TREE
)
134 cp_error ("can't convert from incomplete type `%T' to `%T'",
136 return error_mark_node
;
139 rval
= build_type_conversion (CONVERT_EXPR
, type
, expr
, 1);
142 if (rval
== error_mark_node
)
143 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
149 if (TYPE_PTRMEMFUNC_P (type
))
150 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
152 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
153 if (TREE_CODE (type
) == POINTER_TYPE
154 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
155 || TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
))
157 /* Allow an implicit this pointer for pointer to member
159 if (TYPE_PTRMEMFUNC_P (intype
))
161 tree decl
, basebinfo
;
162 tree fntype
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype
));
163 tree t
= TYPE_METHOD_BASETYPE (fntype
);
165 if (current_class_type
== 0
166 || get_base_distance (t
, current_class_type
, 0, &basebinfo
)
169 decl
= build1 (NOP_EXPR
, t
, error_mark_node
);
171 else if (current_class_ptr
== 0)
172 decl
= build1 (NOP_EXPR
, t
, error_mark_node
);
174 decl
= current_class_ref
;
176 expr
= build (OFFSET_REF
, fntype
, decl
, expr
);
179 if (TREE_CODE (expr
) == OFFSET_REF
180 && TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
181 expr
= resolve_offset_ref (expr
);
182 if (TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
183 expr
= build_addr_func (expr
);
184 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
186 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == METHOD_TYPE
)
187 if (pedantic
|| warn_pmf2ptr
)
188 cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr
),
190 return build1 (NOP_EXPR
, type
, expr
);
192 intype
= TREE_TYPE (expr
);
195 if (TYPE_PTRMEMFUNC_P (intype
))
196 intype
= TYPE_PTRMEMFUNC_FN_TYPE (intype
);
198 form
= TREE_CODE (intype
);
200 if (form
== POINTER_TYPE
|| form
== REFERENCE_TYPE
)
202 intype
= TYPE_MAIN_VARIANT (intype
);
204 if (TYPE_MAIN_VARIANT (type
) != intype
205 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
206 && IS_AGGR_TYPE (TREE_TYPE (type
))
207 && IS_AGGR_TYPE (TREE_TYPE (intype
))
208 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
210 enum tree_code code
= PLUS_EXPR
;
211 tree binfo
= get_binfo (TREE_TYPE (type
), TREE_TYPE (intype
), 1);
212 if (binfo
== error_mark_node
)
213 return error_mark_node
;
214 if (binfo
== NULL_TREE
)
216 binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 1);
217 if (binfo
== error_mark_node
)
218 return error_mark_node
;
223 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type
))
224 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype
))
225 || ! BINFO_OFFSET_ZEROP (binfo
))
227 /* Need to get the path we took. */
230 if (code
== PLUS_EXPR
)
231 get_base_distance (TREE_TYPE (type
), TREE_TYPE (intype
), 0, &path
);
233 get_base_distance (TREE_TYPE (intype
), TREE_TYPE (type
), 0, &path
);
234 return build_vbase_path (code
, type
, expr
, path
, 0);
238 if (TREE_CODE (TREE_TYPE (intype
)) == METHOD_TYPE
239 && TREE_CODE (type
) == POINTER_TYPE
240 && TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)
241 return convert_fn_ptr (type
, expr
);
243 if (TREE_CODE (TREE_TYPE (type
)) == OFFSET_TYPE
244 && TREE_CODE (TREE_TYPE (intype
)) == OFFSET_TYPE
)
246 tree b1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (type
));
247 tree b2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
));
248 tree binfo
= get_binfo (b1
, b2
, 1);
249 if (binfo
== NULL_TREE
)
250 binfo
= get_binfo (b2
, b1
, 1);
251 if (binfo
== error_mark_node
)
252 return error_mark_node
;
255 if (TREE_CODE (TREE_TYPE (intype
)) == METHOD_TYPE
256 || (TREE_CODE (type
) == POINTER_TYPE
257 && TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
))
259 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
261 return error_mark_node
;
264 return build1 (NOP_EXPR
, type
, expr
);
267 my_friendly_assert (form
!= OFFSET_TYPE
, 186);
269 if (TYPE_LANG_SPECIFIC (intype
)
270 && (IS_SIGNATURE_POINTER (intype
) || IS_SIGNATURE_REFERENCE (intype
)))
271 return convert_to_pointer (type
, build_optr_ref (expr
));
273 if (integer_zerop (expr
))
275 expr
= build_int_2 (0, 0);
276 TREE_TYPE (expr
) = type
;
280 if (INTEGRAL_CODE_P (form
))
282 if (type_precision (intype
) == POINTER_SIZE
)
283 return build1 (CONVERT_EXPR
, type
, expr
);
284 expr
= convert (type_for_size (POINTER_SIZE
, 0), expr
);
285 /* Modes may be different but sizes should be the same. */
286 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
287 != GET_MODE_SIZE (TYPE_MODE (type
)))
288 /* There is supposed to be some integral type
289 that is the same width as a pointer. */
291 return convert_to_pointer (type
, expr
);
294 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
296 return error_mark_node
;
299 /* Like convert, except permit conversions to take place which
300 are not normally allowed due to access restrictions
301 (such as conversion from sub-type to private super-type). */
304 convert_to_pointer_force (type
, expr
)
307 register tree intype
= TREE_TYPE (expr
);
308 register enum tree_code form
= TREE_CODE (intype
);
310 if (integer_zerop (expr
))
312 expr
= build_int_2 (0, 0);
313 TREE_TYPE (expr
) = type
;
317 /* Convert signature pointer/reference to `void *' first. */
318 if (form
== RECORD_TYPE
319 && (IS_SIGNATURE_POINTER (intype
) || IS_SIGNATURE_REFERENCE (intype
)))
321 expr
= build_optr_ref (expr
);
322 intype
= TREE_TYPE (expr
);
323 form
= TREE_CODE (intype
);
326 if (form
== POINTER_TYPE
)
328 intype
= TYPE_MAIN_VARIANT (intype
);
330 if (TYPE_MAIN_VARIANT (type
) != intype
331 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
332 && IS_AGGR_TYPE (TREE_TYPE (type
))
333 && IS_AGGR_TYPE (TREE_TYPE (intype
))
334 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
336 enum tree_code code
= PLUS_EXPR
;
338 int distance
= get_base_distance (TREE_TYPE (type
),
339 TREE_TYPE (intype
), 0, &path
);
343 cp_error ("type `%T' is ambiguous baseclass of `%s'",
345 TYPE_NAME_STRING (TREE_TYPE (intype
)));
346 return error_mark_node
;
350 distance
= get_base_distance (TREE_TYPE (intype
),
351 TREE_TYPE (type
), 0, &path
);
355 /* Doesn't need any special help from us. */
356 return build1 (NOP_EXPR
, type
, expr
);
360 return build_vbase_path (code
, type
, expr
, path
, 0);
364 return cp_convert_to_pointer (type
, expr
);
367 /* We are passing something to a function which requires a reference.
368 The type we are interested in is in TYPE. The initial
369 value we have to begin with is in ARG.
371 FLAGS controls how we manage access checking.
372 DIRECT_BIND in FLAGS controls how any temporarys are generated.
373 CHECKCONST controls if we report error messages on const subversion. */
376 build_up_reference (type
, arg
, flags
, checkconst
)
378 int flags
, checkconst
;
381 int literal_flag
= 0;
382 tree argtype
= TREE_TYPE (arg
);
383 tree target_type
= TREE_TYPE (type
);
384 tree binfo
= NULL_TREE
;
386 my_friendly_assert (TREE_CODE (type
) == REFERENCE_TYPE
, 187);
387 if ((flags
& LOOKUP_PROTECT
)
388 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
389 && IS_AGGR_TYPE (argtype
)
390 && IS_AGGR_TYPE (target_type
))
392 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
);
399 /* Pass along const and volatile down into the type. */
400 if (TYPE_READONLY (type
) || TYPE_VOLATILE (type
))
401 target_type
= cp_build_type_variant (target_type
, TYPE_READONLY (type
),
402 TYPE_VOLATILE (type
));
404 if (TREE_CODE (targ
) == SAVE_EXPR
)
405 targ
= TREE_OPERAND (targ
, 0);
406 while (TREE_CODE (targ
) == NOP_EXPR
407 && (TYPE_MAIN_VARIANT (argtype
)
408 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ
, 0)))))
409 targ
= TREE_OPERAND (targ
, 0);
411 switch (TREE_CODE (targ
))
414 /* This is a call to a constructor which did not know what it was
415 initializing until now: it needs to initialize a temporary. */
416 if (TREE_HAS_CONSTRUCTOR (targ
))
418 tree temp
= build_cplus_new (argtype
, TREE_OPERAND (targ
, 0));
419 TREE_HAS_CONSTRUCTOR (targ
) = 0;
420 return build_up_reference (type
, temp
, flags
, 1);
422 /* Let &* cancel out to simplify resulting code.
423 Also, throw away intervening NOP_EXPRs. */
424 arg
= TREE_OPERAND (targ
, 0);
425 if (TREE_CODE (arg
) == NOP_EXPR
|| TREE_CODE (arg
) == NON_LVALUE_EXPR
426 || (TREE_CODE (arg
) == CONVERT_EXPR
&& TREE_REFERENCE_EXPR (arg
)))
427 arg
= TREE_OPERAND (arg
, 0);
429 /* in doing a &*, we have to get rid of the const'ness on the pointer
430 value. Haven't thought about volatile here. Pointers come to mind
432 if (TREE_READONLY (arg
))
434 arg
= copy_node (arg
);
435 TREE_READONLY (arg
) = 0;
438 rval
= build1 (CONVERT_EXPR
, type
, arg
);
439 TREE_REFERENCE_EXPR (rval
) = 1;
441 /* propagate the const flag on something like:
448 class Derived : public Base {
455 void func2(const Derived& d) {
459 on the d parameter. The below could have been avoided, if the flags
460 were down in the tree, not sure why they are not. (mrs) */
461 /* The below code may have to be propagated to other parts of this
463 if (TREE_READONLY (targ
) && !TREE_READONLY (arg
)
464 && (TREE_CODE (arg
) == PARM_DECL
|| TREE_CODE (arg
) == VAR_DECL
)
465 && TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
466 && (TYPE_READONLY (target_type
) && checkconst
))
468 arg
= copy_node (arg
);
469 TREE_READONLY (arg
) = TREE_READONLY (targ
);
471 literal_flag
= TREE_CONSTANT (arg
);
475 /* Get this out of a register if we happened to be in one by accident.
476 Also, build up references to non-lvalues it we must. */
477 /* For &x[y], return (&) x+y */
479 if (mark_addressable (TREE_OPERAND (targ
, 0)) == 0)
480 return error_mark_node
;
481 rval
= build_binary_op (PLUS_EXPR
, TREE_OPERAND (targ
, 0),
482 TREE_OPERAND (targ
, 1), 1);
483 TREE_TYPE (rval
) = type
;
484 if (TREE_CONSTANT (TREE_OPERAND (targ
, 1))
485 && staticp (TREE_OPERAND (targ
, 0)))
486 TREE_CONSTANT (rval
) = 1;
490 /* Could be a reference to a static member. */
492 tree field
= TREE_OPERAND (targ
, 1);
493 if (TREE_STATIC (field
))
495 rval
= build1 (ADDR_EXPR
, type
, field
);
501 /* We should have farmed out member pointers above. */
502 my_friendly_abort (188);
505 rval
= build_component_addr (targ
, build_pointer_type (argtype
),
506 "attempt to make a reference to bit-field structure member `%s'");
507 TREE_TYPE (rval
) = type
;
508 literal_flag
= staticp (TREE_OPERAND (targ
, 0));
512 /* Anything not already handled and not a true memory reference
513 needs to have a reference built up. Do so silently for
514 things like integers and return values from function,
515 but complain if we need a reference to something declared
519 /* 'this' is not an lvalue. */
520 if (targ
== current_class_ptr
&& ! flag_this_is_variable
)
531 mark_addressable (targ
);
536 tree real_reference
= build_up_reference (type
, TREE_OPERAND (targ
, 1),
538 rval
= build (COMPOUND_EXPR
, type
, TREE_OPERAND (targ
, 0), real_reference
);
539 TREE_CONSTANT (rval
) = staticp (TREE_OPERAND (targ
, 1));
543 case PREINCREMENT_EXPR
:
544 case PREDECREMENT_EXPR
:
548 tree real_reference
= build_up_reference (type
, TREE_OPERAND (targ
, 0),
550 rval
= build (COMPOUND_EXPR
, type
, arg
, real_reference
);
551 TREE_CONSTANT (rval
) = staticp (TREE_OPERAND (targ
, 0));
556 return build (COND_EXPR
, type
,
557 TREE_OPERAND (targ
, 0),
558 build_up_reference (type
, TREE_OPERAND (targ
, 1),
560 build_up_reference (type
, TREE_OPERAND (targ
, 2),
563 /* Undo the folding... */
566 return build (COND_EXPR
, type
,
567 build (TREE_CODE (targ
) == MIN_EXPR
? LT_EXPR
: GT_EXPR
,
568 boolean_type_node
, TREE_OPERAND (targ
, 0),
569 TREE_OPERAND (targ
, 1)),
570 build_up_reference (type
, TREE_OPERAND (targ
, 0),
572 build_up_reference (type
, TREE_OPERAND (targ
, 1),
576 arg
= TREE_OPERAND (targ
, 1);
577 if (arg
== NULL_TREE
)
579 compiler_error ("({ ... }) expression not expanded when needed for reference");
580 return error_mark_node
;
582 rval
= build1 (ADDR_EXPR
, type
, arg
);
583 TREE_REFERENCE_EXPR (rval
) = 1;
590 if ((flags
& DIRECT_BIND
)
591 && ! real_lvalue_p (targ
))
593 if (toplevel_bindings_p ())
595 arg
= get_temp_name (argtype
, 1);
600 arg
= pushdecl (build_decl (VAR_DECL
, NULL_TREE
, argtype
));
601 DECL_ARTIFICIAL (arg
) = 1;
603 DECL_INITIAL (arg
) = targ
;
604 cp_finish_decl (arg
, targ
, NULL_TREE
, 0, LOOKUP_ONLYCONVERTING
);
606 else if (TREE_ADDRESSABLE (targ
) == 0 && !(flags
& DIRECT_BIND
))
608 tree slot
= build_decl (VAR_DECL
, NULL_TREE
, argtype
);
609 arg
= build (TARGET_EXPR
, argtype
, slot
, arg
, NULL_TREE
, NULL_TREE
);
612 /* If we had a way to wrap this up, and say, if we ever needed it's
613 address, transform all occurrences of the register, into a memory
614 reference we could win better. */
615 mark_addressable (arg
);
616 rval
= build1 (ADDR_EXPR
, type
, arg
);
619 if (TYPE_USES_COMPLEX_INHERITANCE (argtype
)
620 || TYPE_USES_COMPLEX_INHERITANCE (target_type
))
622 TREE_TYPE (rval
) = build_pointer_type (argtype
);
623 if (flags
& LOOKUP_PROTECT
)
624 rval
= convert_pointer_to (target_type
, rval
);
627 = convert_to_pointer_force (build_pointer_type (target_type
), rval
);
628 TREE_TYPE (rval
) = type
;
629 if (TREE_CODE (rval
) == PLUS_EXPR
|| TREE_CODE (rval
) == MINUS_EXPR
)
630 TREE_TYPE (TREE_OPERAND (rval
, 0))
631 = TREE_TYPE (TREE_OPERAND (rval
, 1)) = type
;
633 TREE_CONSTANT (rval
) = literal_flag
;
637 /* For C++: Only need to do one-level references, but cannot
638 get tripped up on signed/unsigned differences.
640 DECL is either NULL_TREE or the _DECL node for a reference that is being
641 initialized. It can be error_mark_node if we don't know the _DECL but
642 we know it's an initialization. */
645 convert_to_reference (reftype
, expr
, convtype
, flags
, decl
)
650 register tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
651 register tree intype
= TREE_TYPE (expr
);
652 tree rval
= NULL_TREE
;
653 tree rval_as_conversion
= NULL_TREE
;
656 if (TREE_CODE (intype
) == REFERENCE_TYPE
)
657 my_friendly_abort (364);
659 intype
= TYPE_MAIN_VARIANT (intype
);
661 i
= comp_target_types (type
, intype
, 0);
663 if (i
<= 0 && (convtype
& CONV_IMPLICIT
) && IS_AGGR_TYPE (intype
)
664 && ! (flags
& LOOKUP_NO_CONVERSION
))
666 /* Look for a user-defined conversion to lvalue that we can use. */
668 if (flag_ansi_overloading
)
670 = build_type_conversion (CONVERT_EXPR
, reftype
, expr
, 1);
672 rval_as_conversion
= build_type_conversion (CONVERT_EXPR
, type
, expr
, 1);
674 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
675 && real_lvalue_p (rval_as_conversion
))
677 expr
= rval_as_conversion
;
678 rval_as_conversion
= NULL_TREE
;
684 if (((convtype
& CONV_STATIC
) && i
== -1)
685 || ((convtype
& CONV_IMPLICIT
) && i
== 1))
687 if (flags
& LOOKUP_COMPLAIN
)
689 tree ttl
= TREE_TYPE (reftype
);
693 int r
= TREE_READONLY (expr
);
694 int v
= TREE_THIS_VOLATILE (expr
);
695 ttr
= cp_build_type_variant (TREE_TYPE (expr
), r
, v
);
698 if (! real_lvalue_p (expr
) && ! TYPE_READONLY (ttl
))
701 /* Ensure semantics of [dcl.init.ref] */
702 cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'",
705 cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'",
708 else if (! (convtype
& CONV_CONST
))
710 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
711 cp_pedwarn ("conversion from `%T' to `%T' discards const",
713 else if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
714 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
719 return build_up_reference (reftype
, expr
, flags
,
720 ! (convtype
& CONV_CONST
));
722 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
724 /* When casting an lvalue to a reference type, just convert into
725 a pointer to the new type and deference it. This is allowed
726 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
727 should be done directly (jason). (int &)ri ---> *(int*)&ri */
729 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
731 if (TREE_CODE (intype
) == POINTER_TYPE
732 && (comptypes (TREE_TYPE (intype
), type
, -1)))
733 cp_warning ("casting `%T' to `%T' does not dereference pointer",
736 rval
= build_unary_op (ADDR_EXPR
, expr
, 0);
737 if (rval
!= error_mark_node
)
738 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)), rval
, 0);
739 if (rval
!= error_mark_node
)
740 rval
= build1 (NOP_EXPR
, reftype
, rval
);
742 else if (flag_ansi_overloading
)
744 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
746 if (rval
== error_mark_node
)
747 return error_mark_node
;
748 rval
= build_up_reference (reftype
, rval
, flags
, 1);
750 if (rval
&& ! TYPE_READONLY (TREE_TYPE (reftype
)))
751 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
756 tree rval_as_ctor
= NULL_TREE
;
758 if (rval_as_conversion
)
760 if (rval_as_conversion
== error_mark_node
)
762 cp_error ("conversion from `%T' to `%T' is ambiguous",
764 return error_mark_node
;
766 rval_as_conversion
= build_up_reference (reftype
, rval_as_conversion
,
770 /* Definitely need to go through a constructor here. */
771 if (TYPE_HAS_CONSTRUCTOR (type
)
772 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type
)
773 && (rval
= build_method_call
774 (NULL_TREE
, ctor_identifier
,
775 build_tree_list (NULL_TREE
, expr
), TYPE_BINFO (type
),
776 LOOKUP_NO_CONVERSION
|LOOKUP_SPECULATIVELY
777 | LOOKUP_ONLYCONVERTING
)))
781 if (toplevel_bindings_p ())
783 tree t
= get_temp_name (type
, toplevel_bindings_p ());
784 init
= build_method_call (t
, ctor_identifier
,
785 build_tree_list (NULL_TREE
, expr
),
787 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
788 | LOOKUP_ONLYCONVERTING
);
790 if (init
== error_mark_node
)
791 return error_mark_node
;
793 make_decl_rtl (t
, NULL_PTR
, 1);
794 static_aggregates
= perm_tree_cons (expr
, t
, static_aggregates
);
795 rval
= build_unary_op (ADDR_EXPR
, t
, 0);
799 init
= build_method_call (NULL_TREE
, ctor_identifier
,
800 build_tree_list (NULL_TREE
, expr
),
802 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
803 |LOOKUP_ONLYCONVERTING
);
805 if (init
== error_mark_node
)
806 return error_mark_node
;
808 rval
= build_cplus_new (type
, init
);
809 rval
= build_up_reference (reftype
, rval
, flags
, 1);
814 if (rval_as_ctor
&& rval_as_conversion
)
816 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
818 return error_mark_node
;
820 else if (rval_as_ctor
)
822 else if (rval_as_conversion
)
823 rval
= rval_as_conversion
;
824 else if (! IS_AGGR_TYPE (type
) && ! IS_AGGR_TYPE (intype
))
826 rval
= convert (type
, expr
);
827 if (rval
== error_mark_node
)
828 return error_mark_node
;
830 rval
= build_up_reference (reftype
, rval
, flags
, 1);
833 if (rval
&& ! TYPE_READONLY (TREE_TYPE (reftype
)))
834 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
840 /* If we found a way to convert earlier, then use it. */
844 my_friendly_assert (TREE_CODE (intype
) != OFFSET_TYPE
, 189);
846 if (flags
& LOOKUP_COMPLAIN
)
847 cp_error ("cannot convert type `%T' to type `%T'", intype
, reftype
);
849 if (flags
& LOOKUP_SPECULATIVELY
)
852 return error_mark_node
;
855 /* We are using a reference VAL for its value. Bash that reference all the
856 way down to its lowest form. */
859 convert_from_reference (val
)
862 tree type
= TREE_TYPE (val
);
864 if (TREE_CODE (type
) == OFFSET_TYPE
)
865 type
= TREE_TYPE (type
);
866 if (TREE_CODE (type
) == REFERENCE_TYPE
)
867 return build_indirect_ref (val
, NULL_PTR
);
871 /* See if there is a constructor of type TYPE which will convert
872 EXPR. The reference manual seems to suggest (8.5.6) that we need
873 not worry about finding constructors for base classes, then converting
874 to the derived class.
876 MSGP is a pointer to a message that would be an appropriate error
877 string. If MSGP is NULL, then we are not interested in reporting
881 convert_to_aggr (type
, expr
, msgp
, protect
)
886 tree basetype
= type
;
887 tree name
= TYPE_IDENTIFIER (basetype
);
888 tree function
, fndecl
, fntype
, parmtypes
, parmlist
, result
;
890 /* See code below that used this. */
894 int can_be_private
, can_be_protected
;
896 if (! TYPE_HAS_CONSTRUCTOR (basetype
))
899 *msgp
= "type `%s' does not have a constructor";
900 return error_mark_node
;
903 access
= access_public_node
;
905 can_be_protected
= IDENTIFIER_CLASS_VALUE (name
) || name
== current_class_name
;
907 parmlist
= build_tree_list (NULL_TREE
, expr
);
908 parmtypes
= tree_cons (NULL_TREE
, TREE_TYPE (expr
), void_list_node
);
910 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
912 parmtypes
= tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
913 parmlist
= tree_cons (NULL_TREE
, integer_one_node
, parmlist
);
916 /* The type of the first argument will be filled in inside the loop. */
917 parmlist
= tree_cons (NULL_TREE
, integer_zero_node
, parmlist
);
918 parmtypes
= tree_cons (NULL_TREE
, build_pointer_type (basetype
), parmtypes
);
920 /* No exact conversion was found. See if an approximate
922 fndecl
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 0);
926 int saw_protected
= 0;
927 struct candidate
*candidates
=
928 (struct candidate
*) alloca ((decl_list_length (fndecl
)+1) * sizeof (struct candidate
));
929 struct candidate
*cp
= candidates
;
935 cp
->harshness
= (struct harshness_code
*)
936 alloca (3 * sizeof (struct harshness_code
));
938 compute_conversion_costs (fndecl
, parmlist
, cp
, 2);
939 if ((cp
->h
.code
& EVIL_CODE
) == 0)
941 cp
->u
.field
= fndecl
;
944 if (TREE_PRIVATE (fndecl
))
945 access
= access_private_node
;
946 else if (TREE_PROTECTED (fndecl
))
947 access
= access_protected_node
;
949 access
= access_public_node
;
952 access
= access_public_node
;
954 if (access
== access_private_node
955 ? (basetype
== current_class_type
956 || is_friend (basetype
, cp
->function
)
957 || purpose_member (basetype
, DECL_ACCESS (fndecl
)))
958 : access
== access_protected_node
960 || purpose_member (basetype
, DECL_ACCESS (fndecl
)))
963 if (cp
->h
.code
<= TRIVIAL_CODE
)
969 if (access
== access_private_node
)
975 fndecl
= DECL_CHAIN (fndecl
);
979 /* Rank from worst to best. Then cp will point to best one.
980 Private fields have their bits flipped. For unsigned
981 numbers, this should make them look very large.
982 If the best alternate has a (signed) negative value,
983 then all we ever saw were private members. */
984 if (cp
- candidates
> 1)
985 qsort (candidates
, /* char *base */
986 cp
- candidates
, /* int nel */
987 sizeof (struct candidate
), /* int width */
988 rank_for_overload
); /* int (*compar)() */
991 if (cp
->h
.code
& EVIL_CODE
)
994 *msgp
= "ambiguous type conversion possible for `%s'";
995 return error_mark_node
;
998 function
= cp
->function
;
999 fndecl
= cp
->u
.field
;
1006 *msgp
= "only private and protected conversions apply";
1008 *msgp
= "only private conversions apply";
1009 else if (saw_protected
)
1010 *msgp
= "only protected conversions apply";
1012 *msgp
= "no appropriate conversion to type `%s'";
1014 return error_mark_node
;
1019 if (access
== access_private_node
)
1020 if (! can_be_private
)
1023 *msgp
= TREE_PRIVATE (fndecl
)
1024 ? "conversion to type `%s' is private"
1025 : "conversion to type `%s' is from private base class";
1026 return error_mark_node
;
1028 if (access
== access_protected_node
)
1029 if (! can_be_protected
)
1032 *msgp
= TREE_PRIVATE (fndecl
)
1033 ? "conversion to type `%s' is protected"
1034 : "conversion to type `%s' is from protected base class";
1035 return error_mark_node
;
1040 /* It will convert, but we don't do anything about it yet. */
1044 fntype
= TREE_TYPE (function
);
1046 parmlist
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
1047 parmlist
, NULL_TREE
, LOOKUP_NORMAL
);
1049 result
= build_call (function
, TREE_TYPE (fntype
), parmlist
);
1053 /* Call this when we know (for any reason) that expr is not, in fact,
1054 zero. This routine is like convert_pointer_to, but it pays
1055 attention to which specific instance of what type we want to
1056 convert to. This routine should eventually become
1057 convert_to_pointer after all references to convert_to_pointer
1061 convert_pointer_to_real (binfo
, expr
)
1064 register tree intype
= TREE_TYPE (expr
);
1068 if (TREE_CODE (binfo
) == TREE_VEC
)
1069 type
= BINFO_TYPE (binfo
);
1070 else if (IS_AGGR_TYPE (binfo
))
1080 ptr_type
= cp_build_type_variant (type
, TYPE_READONLY (TREE_TYPE (intype
)),
1081 TYPE_VOLATILE (TREE_TYPE (intype
)));
1082 ptr_type
= build_pointer_type (ptr_type
);
1083 if (ptr_type
== TYPE_MAIN_VARIANT (intype
))
1086 if (intype
== error_mark_node
)
1087 return error_mark_node
;
1089 my_friendly_assert (!integer_zerop (expr
), 191);
1091 if (TREE_CODE (type
) == RECORD_TYPE
1092 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
1093 && type
!= TYPE_MAIN_VARIANT (TREE_TYPE (intype
)))
1097 = get_base_distance (binfo
, TYPE_MAIN_VARIANT (TREE_TYPE (intype
)),
1100 /* This function shouldn't be called with unqualified arguments
1101 but if it is, give them an error message that they can read. */
1104 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1105 TREE_TYPE (intype
), type
);
1108 cp_error ("because `%T' is an ambiguous base class", type
);
1109 return error_mark_node
;
1112 return build_vbase_path (PLUS_EXPR
, ptr_type
, expr
, path
, 1);
1114 rval
= build1 (NOP_EXPR
, ptr_type
,
1115 TREE_CODE (expr
) == NOP_EXPR
? TREE_OPERAND (expr
, 0) : expr
);
1116 TREE_CONSTANT (rval
) = TREE_CONSTANT (expr
);
1120 /* Call this when we know (for any reason) that expr is
1121 not, in fact, zero. This routine gets a type out of the first
1122 argument and uses it to search for the type to convert to. If there
1123 is more than one instance of that type in the expr, the conversion is
1124 ambiguous. This routine should eventually go away, and all
1125 callers should use convert_to_pointer_real. */
1128 convert_pointer_to (binfo
, expr
)
1133 if (TREE_CODE (binfo
) == TREE_VEC
)
1134 type
= BINFO_TYPE (binfo
);
1135 else if (IS_AGGR_TYPE (binfo
))
1139 return convert_pointer_to_real (type
, expr
);
1144 FLAGS indicates how we should behave. */
1147 cp_convert (type
, expr
, convtype
, flags
)
1149 int convtype
, flags
;
1151 register tree e
= expr
;
1152 register enum tree_code code
= TREE_CODE (type
);
1154 if (TREE_CODE (e
) == ERROR_MARK
1155 || TREE_CODE (TREE_TYPE (e
)) == ERROR_MARK
)
1156 return error_mark_node
;
1158 if (IS_AGGR_TYPE (type
) && (convtype
& CONV_FORCE_TEMP
))
1159 /* We need a new temporary; don't take this shortcut. */;
1160 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (e
)))
1161 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
1162 return fold (build1 (NOP_EXPR
, type
, e
));
1164 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
1165 return build1 (CONVERT_EXPR
, type
, e
);
1168 /* This is incorrect. A truncation can't be stripped this way.
1169 Extensions will be stripped by the use of get_unwidened. */
1170 if (TREE_CODE (e
) == NOP_EXPR
)
1171 return convert (type
, TREE_OPERAND (e
, 0));
1174 /* Just convert to the type of the member. */
1175 if (code
== OFFSET_TYPE
)
1177 type
= TREE_TYPE (type
);
1178 code
= TREE_CODE (type
);
1182 if (code
== REFERENCE_TYPE
)
1183 return fold (convert_to_reference (type
, e
, convtype
, flags
, NULL_TREE
));
1184 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
1185 e
= convert_from_reference (e
);
1188 if (TREE_CODE (e
) == OFFSET_REF
)
1189 e
= resolve_offset_ref (e
);
1191 if (TREE_READONLY_DECL_P (e
))
1192 e
= decl_constant_value (e
);
1194 if (INTEGRAL_CODE_P (code
))
1196 tree intype
= TREE_TYPE (e
);
1197 /* enum = enum, enum = int, enum = float, (enum)pointer are all
1199 if (flag_int_enum_equivalence
== 0
1200 && TREE_CODE (type
) == ENUMERAL_TYPE
1201 && ((ARITHMETIC_TYPE_P (intype
) && ! (convtype
& CONV_STATIC
))
1202 || (TREE_CODE (intype
) == POINTER_TYPE
)))
1204 cp_pedwarn ("conversion from `%#T' to `%#T'", intype
, type
);
1206 if (flag_pedantic_errors
)
1207 return error_mark_node
;
1209 if (IS_AGGR_TYPE (intype
))
1212 rval
= build_type_conversion (CONVERT_EXPR
, type
, e
, 1);
1215 if (flags
& LOOKUP_COMPLAIN
)
1216 cp_error ("`%#T' used where a `%T' was expected", intype
, type
);
1217 if (flags
& LOOKUP_SPECULATIVELY
)
1219 return error_mark_node
;
1221 if (code
== BOOLEAN_TYPE
)
1223 /* Common Ada/Pascal programmer's mistake. We always warn
1224 about this since it is so bad. */
1225 if (TREE_CODE (expr
) == FUNCTION_DECL
)
1226 cp_warning ("the address of `%D', will always be `true'", expr
);
1227 return truthvalue_conversion (e
);
1229 return fold (convert_to_integer (type
, e
));
1231 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
1232 || TYPE_PTRMEMFUNC_P (type
))
1233 return fold (cp_convert_to_pointer (type
, e
));
1234 if (code
== REAL_TYPE
)
1236 if (IS_AGGR_TYPE (TREE_TYPE (e
)))
1239 rval
= build_type_conversion (CONVERT_EXPR
, type
, e
, 1);
1243 if (flags
& LOOKUP_COMPLAIN
)
1244 cp_error ("`%#T' used where a floating point value was expected",
1247 return fold (convert_to_real (type
, e
));
1250 /* New C++ semantics: since assignment is now based on
1251 memberwise copying, if the rhs type is derived from the
1252 lhs type, then we may still do a conversion. */
1253 if (IS_AGGR_TYPE_CODE (code
))
1255 tree dtype
= TREE_TYPE (e
);
1256 tree ctor
= NULL_TREE
;
1257 tree conversion
= NULL_TREE
;
1259 dtype
= TYPE_MAIN_VARIANT (dtype
);
1261 /* Conversion of object pointers or signature pointers/references
1262 to signature pointers/references. */
1264 if (TYPE_LANG_SPECIFIC (type
)
1265 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
1267 tree constructor
= build_signature_pointer_constructor (type
, expr
);
1268 tree sig_ty
= SIGNATURE_TYPE (type
);
1271 if (constructor
== error_mark_node
)
1272 return error_mark_node
;
1274 sig_ptr
= get_temp_name (type
, 1);
1275 DECL_INITIAL (sig_ptr
) = constructor
;
1276 CLEAR_SIGNATURE (sig_ty
);
1277 cp_finish_decl (sig_ptr
, constructor
, NULL_TREE
, 0, 0);
1278 SET_SIGNATURE (sig_ty
);
1279 TREE_READONLY (sig_ptr
) = 1;
1284 /* Conversion between aggregate types. New C++ semantics allow
1285 objects of derived type to be cast to objects of base type.
1286 Old semantics only allowed this between pointers.
1288 There may be some ambiguity between using a constructor
1289 vs. using a type conversion operator when both apply. */
1291 if (flag_ansi_overloading
)
1295 if ((flags
& LOOKUP_ONLYCONVERTING
)
1296 && ! (IS_AGGR_TYPE (dtype
) && DERIVED_FROM_P (type
, dtype
)))
1297 ctor
= build_user_type_conversion (type
, ctor
, flags
);
1299 ctor
= build_method_call (NULL_TREE
, ctor_identifier
,
1300 build_tree_list (NULL_TREE
, ctor
),
1301 TYPE_BINFO (type
), flags
);
1303 return build_cplus_new (type
, ctor
);
1307 if (IS_AGGR_TYPE (dtype
) && ! DERIVED_FROM_P (type
, dtype
)
1308 && TYPE_HAS_CONVERSION (dtype
))
1309 conversion
= build_type_conversion (CONVERT_EXPR
, type
, e
, 1);
1311 if (conversion
== error_mark_node
)
1313 if (flags
& LOOKUP_COMPLAIN
)
1314 error ("ambiguous pointer conversion");
1318 if (TYPE_HAS_CONSTRUCTOR (complete_type (type
)))
1319 ctor
= build_method_call (NULL_TREE
, ctor_identifier
,
1320 build_tree_list (NULL_TREE
, e
),
1322 (flags
& LOOKUP_NORMAL
)
1323 | LOOKUP_SPECULATIVELY
1324 | (flags
& LOOKUP_ONLYCONVERTING
)
1325 | (flags
& LOOKUP_NO_CONVERSION
)
1326 | (conversion
? LOOKUP_NO_CONVERSION
: 0));
1328 if (ctor
== error_mark_node
)
1330 if (flags
& LOOKUP_COMPLAIN
)
1331 cp_error ("in conversion to type `%T'", type
);
1332 if (flags
& LOOKUP_SPECULATIVELY
)
1334 return error_mark_node
;
1337 if (conversion
&& ctor
)
1339 if (flags
& LOOKUP_COMPLAIN
)
1340 error ("both constructor and type conversion operator apply");
1341 if (flags
& LOOKUP_SPECULATIVELY
)
1343 return error_mark_node
;
1345 else if (conversion
)
1349 ctor
= build_cplus_new (type
, ctor
);
1355 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1356 then the it won't be hashed and hence compare as not equal,
1358 if (code
== ARRAY_TYPE
1359 && TREE_TYPE (TREE_TYPE (e
)) == TREE_TYPE (type
)
1360 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e
)), TYPE_DOMAIN (type
)))
1363 if (flags
& LOOKUP_COMPLAIN
)
1364 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1365 TREE_TYPE (expr
), type
);
1366 if (flags
& LOOKUP_SPECULATIVELY
)
1368 return error_mark_node
;
1371 /* Create an expression whose value is that of EXPR,
1372 converted to type TYPE. The TREE_TYPE of the value
1373 is always TYPE. This function implements all reasonable
1374 conversions; callers should filter out those that are
1375 not permitted by the language being compiled. */
1378 convert (type
, expr
)
1381 return cp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
);
1384 /* Like convert, except permit conversions to take place which
1385 are not normally allowed due to access restrictions
1386 (such as conversion from sub-type to private super-type). */
1389 convert_force (type
, expr
, convtype
)
1394 register tree e
= expr
;
1395 register enum tree_code code
= TREE_CODE (type
);
1397 if (code
== REFERENCE_TYPE
)
1398 return fold (convert_to_reference (type
, e
, CONV_C_CAST
, LOOKUP_COMPLAIN
,
1400 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
1401 e
= convert_from_reference (e
);
1403 if (code
== POINTER_TYPE
)
1404 return fold (convert_to_pointer_force (type
, e
));
1406 /* From typeck.c convert_for_assignment */
1407 if (((TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
&& TREE_CODE (e
) == ADDR_EXPR
1408 && TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
1409 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1410 || integer_zerop (e
)
1411 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1412 && TYPE_PTRMEMFUNC_P (type
))
1414 /* compatible pointer to member functions. */
1415 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1);
1418 return cp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
);
1421 /* Subroutine of build_type_conversion. */
1424 build_type_conversion_1 (xtype
, basetype
, expr
, typename
, for_sure
)
1425 tree xtype
, basetype
;
1434 flags
= LOOKUP_PROTECT
|LOOKUP_ONLYCONVERTING
;
1436 flags
= LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
;
1438 rval
= build_method_call (expr
, typename
, NULL_TREE
, NULL_TREE
, flags
);
1439 if (rval
== error_mark_node
)
1443 return error_mark_node
;
1446 if (IS_AGGR_TYPE (TREE_TYPE (rval
)))
1450 && TREE_TYPE (xtype
)
1451 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval
)))
1452 > TREE_READONLY (TREE_TYPE (xtype
))))
1453 warning ("user-defined conversion casting away `const'");
1454 return convert (xtype
, rval
);
1457 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1458 exists, return the attempted conversion. This may
1459 return ERROR_MARK_NODE if the conversion is not
1460 allowed (references private members, etc).
1461 If no conversion exists, NULL_TREE is returned.
1463 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1464 to take place immediately. Otherwise, we build a SAVE_EXPR
1465 which can be evaluated if the results are ever needed.
1467 Changes to this functions should be mirrored in user_harshness.
1469 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1470 object parameter, or by the second standard conversion sequence if
1471 that doesn't do it. This will probably wait for an overloading rewrite.
1475 build_type_conversion (code
, xtype
, expr
, for_sure
)
1476 enum tree_code code
;
1480 /* C++: check to see if we can convert this aggregate type
1481 into the required type. */
1484 tree winner
= NULL_TREE
;
1486 if (flag_ansi_overloading
)
1487 return build_user_type_conversion
1488 (xtype
, expr
, for_sure
? LOOKUP_NORMAL
: 0);
1490 if (expr
== error_mark_node
)
1491 return error_mark_node
;
1493 basetype
= TREE_TYPE (expr
);
1494 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1495 basetype
= TREE_TYPE (basetype
);
1497 basetype
= TYPE_MAIN_VARIANT (basetype
);
1498 if (! TYPE_LANG_SPECIFIC (basetype
) || ! TYPE_HAS_CONVERSION (basetype
))
1501 /* Do we have an exact match? */
1503 tree typename
= build_typename_overload (xtype
);
1504 if (lookup_fnfields (TYPE_BINFO (basetype
), typename
, 0))
1505 return build_type_conversion_1 (xtype
, basetype
, expr
, typename
,
1509 /* Nope; try looking for others. */
1510 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1512 tree cand
= TREE_VALUE (conv
);
1514 if (winner
&& winner
== cand
)
1517 if (can_convert (xtype
, TREE_TYPE (TREE_TYPE (cand
))))
1523 cp_error ("ambiguous conversion from `%T' to `%T'", basetype
,
1525 cp_error (" candidate conversions include `%D' and `%D'",
1536 return build_type_conversion_1 (xtype
, basetype
, expr
,
1537 DECL_NAME (winner
), for_sure
);
1542 /* Convert the given EXPR to one of a group of types suitable for use in an
1543 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1544 which indicates which types are suitable. If COMPLAIN is 1, complain
1545 about ambiguity; otherwise, the caller will deal with it. */
1548 build_expr_type_conversion (desires
, expr
, complain
)
1553 tree basetype
= TREE_TYPE (expr
);
1555 tree winner
= NULL_TREE
;
1557 if (TREE_CODE (basetype
) == OFFSET_TYPE
)
1558 expr
= resolve_offset_ref (expr
);
1559 expr
= convert_from_reference (expr
);
1560 basetype
= TREE_TYPE (expr
);
1562 if (! IS_AGGR_TYPE (basetype
))
1563 switch (TREE_CODE (basetype
))
1566 if ((desires
& WANT_NULL
) && TREE_CODE (expr
) == INTEGER_CST
1567 && integer_zerop (expr
))
1569 /* else fall through... */
1572 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1574 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1576 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1578 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1582 return (desires
& WANT_POINTER
) ? default_conversion (expr
)
1588 if (! TYPE_HAS_CONVERSION (basetype
))
1591 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1595 tree cand
= TREE_VALUE (conv
);
1597 if (winner
&& winner
== cand
)
1600 candidate
= TREE_TYPE (TREE_TYPE (cand
));
1601 if (TREE_CODE (candidate
) == REFERENCE_TYPE
)
1602 candidate
= TREE_TYPE (candidate
);
1604 switch (TREE_CODE (candidate
))
1608 win
= (desires
& WANT_INT
); break;
1610 win
= (desires
& WANT_ENUM
); break;
1612 win
= (desires
& WANT_FLOAT
); break;
1614 win
= (desires
& WANT_POINTER
); break;
1623 cp_error ("ambiguous default type conversion from `%T'",
1625 cp_error (" candidate conversions include `%D' and `%D'",
1628 return error_mark_node
;
1637 tree type
= TREE_TYPE (TREE_TYPE (winner
));
1638 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1639 type
= TREE_TYPE (type
);
1640 return build_type_conversion_1 (type
, basetype
, expr
,
1641 DECL_NAME (winner
), 1);
1647 /* Must convert two aggregate types to non-aggregate type.
1648 Attempts to find a non-ambiguous, "best" type conversion.
1650 Return 1 on success, 0 on failure.
1652 @@ What are the real semantics of this supposed to be??? */
1655 build_default_binary_type_conversion (code
, arg1
, arg2
)
1656 enum tree_code code
;
1662 case TRUNC_DIV_EXPR
:
1664 case FLOOR_DIV_EXPR
:
1665 case ROUND_DIV_EXPR
:
1666 case EXACT_DIV_EXPR
:
1667 *arg1
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg1
, 0);
1668 *arg2
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg2
, 0);
1671 case TRUNC_MOD_EXPR
:
1672 case FLOOR_MOD_EXPR
:
1678 *arg1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, *arg1
, 0);
1679 *arg2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, *arg2
, 0);
1684 tree a1
, a2
, p1
, p2
;
1687 a1
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg1
, 0);
1688 a2
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg2
, 0);
1689 p1
= build_expr_type_conversion (WANT_POINTER
, *arg1
, 0);
1690 p2
= build_expr_type_conversion (WANT_POINTER
, *arg2
, 0);
1692 wins
= (a1
&& a2
) + (a1
&& p2
) + (p1
&& a2
);
1695 error ("ambiguous default type conversion for `operator +'");
1698 *arg1
= a1
, *arg2
= a2
;
1700 *arg1
= a1
, *arg2
= p2
;
1702 *arg1
= p1
, *arg2
= a2
;
1708 tree a1
, a2
, p1
, p2
;
1711 a1
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg1
, 0);
1712 a2
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg2
, 0);
1713 p1
= build_expr_type_conversion (WANT_POINTER
, *arg1
, 0);
1714 p2
= build_expr_type_conversion (WANT_POINTER
, *arg2
, 0);
1716 wins
= (a1
&& a2
) + (p1
&& p2
) + (p1
&& a2
);
1719 error ("ambiguous default type conversion for `operator -'");
1722 *arg1
= a1
, *arg2
= a2
;
1724 *arg1
= p1
, *arg2
= p2
;
1726 *arg1
= p1
, *arg2
= a2
;
1737 tree a1
, a2
, p1
, p2
;
1740 a1
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg1
, 0);
1741 a2
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg2
, 0);
1742 p1
= build_expr_type_conversion (WANT_POINTER
| WANT_NULL
, *arg1
, 0);
1743 p2
= build_expr_type_conversion (WANT_POINTER
| WANT_NULL
, *arg2
, 0);
1745 wins
= (a1
&& a2
) + (p1
&& p2
);
1748 cp_error ("ambiguous default type conversion for `%O'", code
);
1751 *arg1
= a1
, *arg2
= a2
;
1753 *arg1
= p1
, *arg2
= p2
;
1757 case TRUTH_ANDIF_EXPR
:
1758 case TRUTH_ORIF_EXPR
:
1759 *arg1
= convert (boolean_type_node
, *arg1
);
1760 *arg2
= convert (boolean_type_node
, *arg2
);
1768 if (*arg1
== error_mark_node
|| *arg2
== error_mark_node
)
1769 cp_error ("ambiguous default type conversion for `%O'", code
);
1777 /* Implements integral promotion (4.1) and float->double promotion. */
1780 type_promotes_to (type
)
1783 int constp
, volatilep
;
1785 if (type
== error_mark_node
)
1786 return error_mark_node
;
1788 constp
= TYPE_READONLY (type
);
1789 volatilep
= TYPE_VOLATILE (type
);
1790 type
= TYPE_MAIN_VARIANT (type
);
1792 /* bool always promotes to int (not unsigned), even if it's the same
1794 if (type
== boolean_type_node
)
1795 type
= integer_type_node
;
1797 /* Normally convert enums to int, but convert wide enums to something
1799 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1800 || type
== wchar_type_node
)
1802 int precision
= MAX (TYPE_PRECISION (type
),
1803 TYPE_PRECISION (integer_type_node
));
1804 tree totype
= type_for_size (precision
, 0);
1805 if (TREE_UNSIGNED (type
)
1806 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1807 type
= type_for_size (precision
, 1);
1811 else if (C_PROMOTING_INTEGER_TYPE_P (type
))
1813 /* Retain unsignedness if really not getting bigger. */
1814 if (TREE_UNSIGNED (type
)
1815 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1816 type
= unsigned_type_node
;
1818 type
= integer_type_node
;
1820 else if (type
== float_type_node
)
1821 type
= double_type_node
;
1823 return cp_build_type_variant (type
, constp
, volatilep
);