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. */
40 extern tree static_aggregates
;
42 static tree build_thunk
PROTO((tree
, tree
));
43 static tree convert_fn_ptr
PROTO((tree
, tree
));
44 static tree cp_convert_to_pointer
PROTO((tree
, tree
));
45 static tree convert_to_pointer_force
PROTO((tree
, tree
));
46 static tree build_up_reference
PROTO((tree
, tree
, int, int));
47 static tree build_type_conversion_1
PROTO((tree
, tree
, tree
, tree
,
50 /* Change of width--truncation and extension of integers or reals--
51 is represented with NOP_EXPR. Proper functioning of many things
52 assumes that no other conversions can be NOP_EXPRs.
54 Conversion between integer and pointer is represented with CONVERT_EXPR.
55 Converting integer to real uses FLOAT_EXPR
56 and real to integer uses FIX_TRUNC_EXPR.
58 Here is a list of all the functions that assume that widening and
59 narrowing is always done with a NOP_EXPR:
60 In convert.c, convert_to_integer.
61 In c-typeck.c, build_binary_op_nodefault (boolean ops),
62 and truthvalue_conversion.
63 In expr.c: expand_expr, for operands of a MULT_EXPR.
64 In fold-const.c: fold.
65 In tree.c: get_narrower and get_unwidened.
67 C++: in multiple-inheritance, converting between pointers may involve
68 adjusting them by a delta stored within the class definition. */
70 /* Subroutines of `convert'. */
72 /* Build a thunk. What it is, is an entry point that when called will
73 adjust the this pointer (the first argument) by offset, and then
74 goto the real address of the function given by REAL_ADDR that we
75 would like called. What we return is the address of the thunk. */
78 build_thunk (offset
, real_addr
)
79 tree offset
, real_addr
;
81 if (TREE_CODE (real_addr
) != ADDR_EXPR
82 || TREE_CODE (TREE_OPERAND (real_addr
, 0)) != FUNCTION_DECL
)
84 sorry ("MI pointer to member conversion too complex");
85 return error_mark_node
;
87 sorry ("MI pointer to member conversion too complex");
88 return error_mark_node
;
91 /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
92 another `pointer to method'. This may involved the creation of
93 a thunk to handle the this offset calculation. */
96 convert_fn_ptr (type
, expr
)
99 #if 0 /* We don't use thunks for pmfs. */
100 if (flag_vtable_thunks
)
102 tree intype
= TREE_TYPE (expr
);
103 tree binfo
= get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype
)),
104 TYPE_METHOD_BASETYPE (TREE_TYPE (type
)), 1);
105 if (binfo
== error_mark_node
)
107 error (" in pointer to member conversion");
108 return error_mark_node
;
110 if (binfo
== NULL_TREE
)
112 /* ARM 4.8 restriction. */
113 error ("invalid pointer to member conversion");
114 return error_mark_node
;
117 if (BINFO_OFFSET_ZEROP (binfo
))
118 return build1 (NOP_EXPR
, type
, expr
);
119 return build1 (NOP_EXPR
, type
, build_thunk (BINFO_OFFSET (binfo
), expr
));
123 return build_ptrmemfunc (type
, expr
, 1);
126 /* if converting pointer to pointer
127 if dealing with classes, check for derived->base or vice versa
128 else if dealing with method pointers, delegate
130 else if converting class, pass off to build_type_conversion
131 else try C-style pointer conversion */
134 cp_convert_to_pointer (type
, expr
)
137 register tree intype
= TREE_TYPE (expr
);
138 register enum tree_code form
;
140 if (IS_AGGR_TYPE (intype
))
144 intype
= complete_type (intype
);
145 if (TYPE_SIZE (intype
) == NULL_TREE
)
147 cp_error ("can't convert from incomplete type `%T' to `%T'",
149 return error_mark_node
;
152 rval
= build_type_conversion (CONVERT_EXPR
, type
, expr
, 1);
155 if (rval
== error_mark_node
)
156 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
162 if (TYPE_PTRMEMFUNC_P (type
))
163 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
165 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
166 if (TREE_CODE (type
) == POINTER_TYPE
167 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
168 || TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
))
170 /* Allow an implicit this pointer for pointer to member
172 if (TYPE_PTRMEMFUNC_P (intype
))
174 tree decl
, basebinfo
;
175 tree fntype
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype
));
176 tree t
= TYPE_METHOD_BASETYPE (fntype
);
178 if (current_class_type
== 0
179 || get_base_distance (t
, current_class_type
, 0, &basebinfo
)
182 decl
= build1 (NOP_EXPR
, t
, error_mark_node
);
184 else if (current_class_ptr
== 0)
185 decl
= build1 (NOP_EXPR
, t
, error_mark_node
);
187 decl
= current_class_ref
;
189 expr
= build (OFFSET_REF
, fntype
, decl
, expr
);
192 if (TREE_CODE (expr
) == OFFSET_REF
193 && TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
194 expr
= resolve_offset_ref (expr
);
195 if (TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
196 expr
= build_addr_func (expr
);
197 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
199 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == METHOD_TYPE
)
200 if (pedantic
|| warn_pmf2ptr
)
201 cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr
),
203 return build1 (NOP_EXPR
, type
, expr
);
205 intype
= TREE_TYPE (expr
);
208 if (TYPE_PTRMEMFUNC_P (intype
))
209 intype
= TYPE_PTRMEMFUNC_FN_TYPE (intype
);
211 form
= TREE_CODE (intype
);
213 if (form
== POINTER_TYPE
|| form
== REFERENCE_TYPE
)
215 intype
= TYPE_MAIN_VARIANT (intype
);
217 if (TYPE_MAIN_VARIANT (type
) != intype
218 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
219 && IS_AGGR_TYPE (TREE_TYPE (type
))
220 && IS_AGGR_TYPE (TREE_TYPE (intype
))
221 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
223 enum tree_code code
= PLUS_EXPR
;
224 tree binfo
= get_binfo (TREE_TYPE (type
), TREE_TYPE (intype
), 1);
225 if (binfo
== error_mark_node
)
226 return error_mark_node
;
227 if (binfo
== NULL_TREE
)
229 binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 1);
230 if (binfo
== error_mark_node
)
231 return error_mark_node
;
236 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type
))
237 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype
))
238 || ! BINFO_OFFSET_ZEROP (binfo
))
240 /* Need to get the path we took. */
243 if (code
== PLUS_EXPR
)
244 get_base_distance (TREE_TYPE (type
), TREE_TYPE (intype
), 0, &path
);
246 get_base_distance (TREE_TYPE (intype
), TREE_TYPE (type
), 0, &path
);
247 return build_vbase_path (code
, type
, expr
, path
, 0);
251 if (TREE_CODE (TREE_TYPE (intype
)) == METHOD_TYPE
252 && TREE_CODE (type
) == POINTER_TYPE
253 && TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)
254 return convert_fn_ptr (type
, expr
);
256 if (TREE_CODE (TREE_TYPE (type
)) == OFFSET_TYPE
257 && TREE_CODE (TREE_TYPE (intype
)) == OFFSET_TYPE
)
259 tree b1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (type
));
260 tree b2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
));
261 tree binfo
= get_binfo (b2
, b1
, 1);
262 enum tree_code code
= PLUS_EXPR
;
264 if (binfo
== NULL_TREE
)
266 binfo
= get_binfo (b1
, b2
, 1);
270 if (binfo
== error_mark_node
)
271 return error_mark_node
;
272 if (binfo
&& ! TREE_VIA_VIRTUAL (binfo
))
273 expr
= size_binop (code
, expr
, BINFO_OFFSET (binfo
));
276 if (TREE_CODE (TREE_TYPE (intype
)) == METHOD_TYPE
277 || (TREE_CODE (type
) == POINTER_TYPE
278 && TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
))
280 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
282 return error_mark_node
;
285 return build1 (NOP_EXPR
, type
, expr
);
288 my_friendly_assert (form
!= OFFSET_TYPE
, 186);
290 if (TYPE_LANG_SPECIFIC (intype
)
291 && (IS_SIGNATURE_POINTER (intype
) || IS_SIGNATURE_REFERENCE (intype
)))
292 return convert_to_pointer (type
, build_optr_ref (expr
));
294 if (integer_zerop (expr
))
296 if (TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)
297 return build_ptrmemfunc (type
, expr
, 0);
298 expr
= build_int_2 (0, 0);
299 TREE_TYPE (expr
) = type
;
303 if (INTEGRAL_CODE_P (form
))
305 if (type_precision (intype
) == POINTER_SIZE
)
306 return build1 (CONVERT_EXPR
, type
, expr
);
307 expr
= cp_convert (type_for_size (POINTER_SIZE
, 0), expr
);
308 /* Modes may be different but sizes should be the same. */
309 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
310 != GET_MODE_SIZE (TYPE_MODE (type
)))
311 /* There is supposed to be some integral type
312 that is the same width as a pointer. */
314 return convert_to_pointer (type
, expr
);
317 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
319 return error_mark_node
;
322 /* Like convert, except permit conversions to take place which
323 are not normally allowed due to access restrictions
324 (such as conversion from sub-type to private super-type). */
327 convert_to_pointer_force (type
, expr
)
330 register tree intype
= TREE_TYPE (expr
);
331 register enum tree_code form
= TREE_CODE (intype
);
333 if (integer_zerop (expr
))
335 expr
= build_int_2 (0, 0);
336 TREE_TYPE (expr
) = type
;
340 /* Convert signature pointer/reference to `void *' first. */
341 if (form
== RECORD_TYPE
342 && (IS_SIGNATURE_POINTER (intype
) || IS_SIGNATURE_REFERENCE (intype
)))
344 expr
= build_optr_ref (expr
);
345 intype
= TREE_TYPE (expr
);
346 form
= TREE_CODE (intype
);
349 if (form
== POINTER_TYPE
)
351 intype
= TYPE_MAIN_VARIANT (intype
);
353 if (TYPE_MAIN_VARIANT (type
) != intype
354 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
355 && IS_AGGR_TYPE (TREE_TYPE (type
))
356 && IS_AGGR_TYPE (TREE_TYPE (intype
))
357 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
359 enum tree_code code
= PLUS_EXPR
;
361 int distance
= get_base_distance (TREE_TYPE (type
),
362 TREE_TYPE (intype
), 0, &path
);
366 cp_error ("type `%T' is ambiguous baseclass of `%s'",
368 TYPE_NAME_STRING (TREE_TYPE (intype
)));
369 return error_mark_node
;
373 distance
= get_base_distance (TREE_TYPE (intype
),
374 TREE_TYPE (type
), 0, &path
);
378 /* Doesn't need any special help from us. */
379 return build1 (NOP_EXPR
, type
, expr
);
383 return build_vbase_path (code
, type
, expr
, path
, 0);
387 return cp_convert_to_pointer (type
, expr
);
390 /* We are passing something to a function which requires a reference.
391 The type we are interested in is in TYPE. The initial
392 value we have to begin with is in ARG.
394 FLAGS controls how we manage access checking.
395 DIRECT_BIND in FLAGS controls how any temporaries are generated. */
398 build_up_reference (type
, arg
, flags
, checkconst
)
400 int flags
, checkconst
;
403 tree argtype
= TREE_TYPE (arg
);
404 tree target_type
= TREE_TYPE (type
);
406 my_friendly_assert (TREE_CODE (type
) == REFERENCE_TYPE
, 187);
408 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
411 if (toplevel_bindings_p ())
412 arg
= get_temp_name (argtype
, 1);
415 arg
= pushdecl (build_decl (VAR_DECL
, NULL_TREE
, argtype
));
416 DECL_ARTIFICIAL (arg
) = 1;
418 DECL_INITIAL (arg
) = targ
;
419 cp_finish_decl (arg
, targ
, NULL_TREE
, 0, LOOKUP_ONLYCONVERTING
);
421 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
423 tree slot
= build_decl (VAR_DECL
, NULL_TREE
, argtype
);
424 arg
= build (TARGET_EXPR
, argtype
, slot
, arg
, NULL_TREE
, NULL_TREE
);
425 TREE_SIDE_EFFECTS (arg
) = 1;
428 /* If we had a way to wrap this up, and say, if we ever needed it's
429 address, transform all occurrences of the register, into a memory
430 reference we could win better. */
431 rval
= build_unary_op (ADDR_EXPR
, arg
, 1);
432 if ((flags
& LOOKUP_PROTECT
)
433 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
434 && IS_AGGR_TYPE (argtype
)
435 && IS_AGGR_TYPE (target_type
))
437 /* We go through get_binfo for the access control. */
438 tree binfo
= get_binfo (target_type
, argtype
, 1);
439 if (binfo
== error_mark_node
)
440 return error_mark_node
;
441 if (binfo
== NULL_TREE
)
442 return error_not_base_type (target_type
, argtype
);
443 rval
= convert_pointer_to_real (binfo
, rval
);
447 = convert_to_pointer_force (build_pointer_type (target_type
), rval
);
448 rval
= build1 (NOP_EXPR
, type
, rval
);
449 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
453 /* For C++: Only need to do one-level references, but cannot
454 get tripped up on signed/unsigned differences.
456 DECL is either NULL_TREE or the _DECL node for a reference that is being
457 initialized. It can be error_mark_node if we don't know the _DECL but
458 we know it's an initialization. */
461 convert_to_reference (reftype
, expr
, convtype
, flags
, decl
)
466 register tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
467 register tree intype
= TREE_TYPE (expr
);
468 tree rval
= NULL_TREE
;
469 tree rval_as_conversion
= NULL_TREE
;
472 if (TREE_CODE (intype
) == REFERENCE_TYPE
)
473 my_friendly_abort (364);
475 intype
= TYPE_MAIN_VARIANT (intype
);
477 i
= comp_target_types (type
, intype
, 0);
479 if (i
<= 0 && (convtype
& CONV_IMPLICIT
) && IS_AGGR_TYPE (intype
)
480 && ! (flags
& LOOKUP_NO_CONVERSION
))
482 /* Look for a user-defined conversion to lvalue that we can use. */
484 if (flag_ansi_overloading
)
486 = build_type_conversion (CONVERT_EXPR
, reftype
, expr
, 1);
488 rval_as_conversion
= build_type_conversion (CONVERT_EXPR
, type
, 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
);
509 int r
= TREE_READONLY (expr
);
510 int v
= TREE_THIS_VOLATILE (expr
);
511 ttr
= cp_build_type_variant (TREE_TYPE (expr
), r
, v
);
514 if (! real_lvalue_p (expr
) && ! TYPE_READONLY (ttl
))
517 /* Ensure semantics of [dcl.init.ref] */
518 cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'",
521 cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'",
524 else if (! (convtype
& CONV_CONST
))
526 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
527 cp_pedwarn ("conversion from `%T' to `%T' discards const",
529 else if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
530 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
535 return build_up_reference (reftype
, expr
, flags
,
536 ! (convtype
& CONV_CONST
));
538 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
540 /* When casting an lvalue to a reference type, just convert into
541 a pointer to the new type and deference it. This is allowed
542 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
543 should be done directly (jason). (int &)ri ---> *(int*)&ri */
545 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
547 if (TREE_CODE (intype
) == POINTER_TYPE
548 && (comptypes (TREE_TYPE (intype
), type
, -1)))
549 cp_warning ("casting `%T' to `%T' does not dereference pointer",
552 rval
= build_unary_op (ADDR_EXPR
, expr
, 0);
553 if (rval
!= error_mark_node
)
554 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)), rval
, 0);
555 if (rval
!= error_mark_node
)
556 rval
= build1 (NOP_EXPR
, reftype
, rval
);
558 else if (flag_ansi_overloading
)
560 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
562 if (rval
== error_mark_node
)
563 return error_mark_node
;
564 rval
= build_up_reference (reftype
, rval
, flags
, 1);
566 if (rval
&& ! TYPE_READONLY (TREE_TYPE (reftype
)))
567 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
572 tree rval_as_ctor
= NULL_TREE
;
574 if (rval_as_conversion
)
576 if (rval_as_conversion
== error_mark_node
)
578 cp_error ("conversion from `%T' to `%T' is ambiguous",
580 return error_mark_node
;
582 rval_as_conversion
= build_up_reference (reftype
, rval_as_conversion
,
586 /* Definitely need to go through a constructor here. */
587 if (TYPE_HAS_CONSTRUCTOR (type
)
588 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type
)
589 && (rval
= build_method_call
590 (NULL_TREE
, ctor_identifier
,
591 build_expr_list (NULL_TREE
, expr
), TYPE_BINFO (type
),
592 LOOKUP_NO_CONVERSION
|LOOKUP_SPECULATIVELY
593 | LOOKUP_ONLYCONVERTING
)))
597 if (toplevel_bindings_p ())
599 tree t
= get_temp_name (type
, toplevel_bindings_p ());
600 init
= build_method_call (t
, ctor_identifier
,
601 build_expr_list (NULL_TREE
, expr
),
603 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
604 | LOOKUP_ONLYCONVERTING
);
606 if (init
== error_mark_node
)
607 return error_mark_node
;
609 make_decl_rtl (t
, NULL_PTR
, 1);
610 static_aggregates
= perm_tree_cons (expr
, t
, static_aggregates
);
611 rval
= build_unary_op (ADDR_EXPR
, t
, 0);
615 init
= build_method_call (NULL_TREE
, ctor_identifier
,
616 build_expr_list (NULL_TREE
, expr
),
618 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
619 |LOOKUP_ONLYCONVERTING
);
621 if (init
== error_mark_node
)
622 return error_mark_node
;
624 rval
= build_cplus_new (type
, init
);
625 rval
= build_up_reference (reftype
, rval
, flags
, 1);
630 if (rval_as_ctor
&& rval_as_conversion
)
632 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
634 return error_mark_node
;
636 else if (rval_as_ctor
)
638 else if (rval_as_conversion
)
639 rval
= rval_as_conversion
;
640 else if (! IS_AGGR_TYPE (type
) && ! IS_AGGR_TYPE (intype
))
642 rval
= cp_convert (type
, expr
);
643 if (rval
== error_mark_node
)
644 return error_mark_node
;
646 rval
= build_up_reference (reftype
, rval
, flags
, 1);
649 if (rval
&& ! TYPE_READONLY (TREE_TYPE (reftype
)))
650 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
656 /* If we found a way to convert earlier, then use it. */
660 my_friendly_assert (TREE_CODE (intype
) != OFFSET_TYPE
, 189);
662 if (flags
& LOOKUP_COMPLAIN
)
663 cp_error ("cannot convert type `%T' to type `%T'", intype
, reftype
);
665 if (flags
& LOOKUP_SPECULATIVELY
)
668 return error_mark_node
;
671 /* We are using a reference VAL for its value. Bash that reference all the
672 way down to its lowest form. */
675 convert_from_reference (val
)
678 tree type
= TREE_TYPE (val
);
680 if (TREE_CODE (type
) == OFFSET_TYPE
)
681 type
= TREE_TYPE (type
);
682 if (TREE_CODE (type
) == REFERENCE_TYPE
)
683 return build_indirect_ref (val
, NULL_PTR
);
687 /* See if there is a constructor of type TYPE which will convert
688 EXPR. The reference manual seems to suggest (8.5.6) that we need
689 not worry about finding constructors for base classes, then converting
690 to the derived class.
692 MSGP is a pointer to a message that would be an appropriate error
693 string. If MSGP is NULL, then we are not interested in reporting
697 convert_to_aggr (type
, expr
, msgp
, protect
)
702 tree basetype
= type
;
703 tree name
= TYPE_IDENTIFIER (basetype
);
704 tree function
, fndecl
, fntype
, parmtypes
, parmlist
, result
;
706 /* See code below that used this. */
710 int can_be_private
, can_be_protected
;
712 if (! TYPE_HAS_CONSTRUCTOR (basetype
))
715 *msgp
= "type `%s' does not have a constructor";
716 return error_mark_node
;
719 access
= access_public_node
;
721 can_be_protected
= IDENTIFIER_CLASS_VALUE (name
) || name
== current_class_name
;
723 parmlist
= build_expr_list (NULL_TREE
, expr
);
724 parmtypes
= scratch_tree_cons (NULL_TREE
, TREE_TYPE (expr
), void_list_node
);
726 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
728 parmtypes
= expr_tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
729 parmlist
= scratch_tree_cons (NULL_TREE
, integer_one_node
, parmlist
);
732 /* The type of the first argument will be filled in inside the loop. */
733 parmlist
= expr_tree_cons (NULL_TREE
, integer_zero_node
, parmlist
);
734 parmtypes
= scratch_tree_cons (NULL_TREE
, build_pointer_type (basetype
), parmtypes
);
736 /* No exact conversion was found. See if an approximate
738 fndecl
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 0);
742 int saw_protected
= 0;
743 struct candidate
*candidates
744 = (struct candidate
*) alloca ((decl_list_length (fndecl
)+1) * sizeof (struct candidate
));
745 struct candidate
*cp
= candidates
;
751 cp
->harshness
= (struct harshness_code
*)
752 alloca (3 * sizeof (struct harshness_code
));
754 compute_conversion_costs (fndecl
, parmlist
, cp
, 2);
755 if ((cp
->h
.code
& EVIL_CODE
) == 0)
757 cp
->u
.field
= fndecl
;
760 if (TREE_PRIVATE (fndecl
))
761 access
= access_private_node
;
762 else if (TREE_PROTECTED (fndecl
))
763 access
= access_protected_node
;
765 access
= access_public_node
;
768 access
= access_public_node
;
770 if (access
== access_private_node
771 ? (basetype
== current_class_type
772 || is_friend (basetype
, cp
->function
)
773 || purpose_member (basetype
, DECL_ACCESS (fndecl
)))
774 : access
== access_protected_node
776 || purpose_member (basetype
, DECL_ACCESS (fndecl
)))
779 if (cp
->h
.code
<= TRIVIAL_CODE
)
785 if (access
== access_private_node
)
791 fndecl
= DECL_CHAIN (fndecl
);
795 /* Rank from worst to best. Then cp will point to best one.
796 Private fields have their bits flipped. For unsigned
797 numbers, this should make them look very large.
798 If the best alternate has a (signed) negative value,
799 then all we ever saw were private members. */
800 if (cp
- candidates
> 1)
801 qsort (candidates
, /* char *base */
802 cp
- candidates
, /* int nel */
803 sizeof (struct candidate
), /* int width */
804 (int (*) PROTO((const void *, const void *))) rank_for_overload
); /* int (*compar)() */
807 if (cp
->h
.code
& EVIL_CODE
)
810 *msgp
= "ambiguous type conversion possible for `%s'";
811 return error_mark_node
;
814 function
= cp
->function
;
815 fndecl
= cp
->u
.field
;
822 *msgp
= "only private and protected conversions apply";
824 *msgp
= "only private conversions apply";
825 else if (saw_protected
)
826 *msgp
= "only protected conversions apply";
828 *msgp
= "no appropriate conversion to type `%s'";
830 return error_mark_node
;
835 if (access
== access_private_node
)
836 if (! can_be_private
)
839 *msgp
= TREE_PRIVATE (fndecl
)
840 ? "conversion to type `%s' is private"
841 : "conversion to type `%s' is from private base class";
842 return error_mark_node
;
844 if (access
== access_protected_node
)
845 if (! can_be_protected
)
848 *msgp
= TREE_PRIVATE (fndecl
)
849 ? "conversion to type `%s' is protected"
850 : "conversion to type `%s' is from protected base class";
851 return error_mark_node
;
856 /* It will convert, but we don't do anything about it yet. */
860 fntype
= TREE_TYPE (function
);
862 parmlist
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
863 parmlist
, NULL_TREE
, LOOKUP_NORMAL
);
865 result
= build_call (function
, TREE_TYPE (fntype
), parmlist
);
869 /* Call this when we know (for any reason) that expr is not, in fact,
870 zero. This routine is like convert_pointer_to, but it pays
871 attention to which specific instance of what type we want to
872 convert to. This routine should eventually become
873 convert_to_pointer after all references to convert_to_pointer
877 convert_pointer_to_real (binfo
, expr
)
880 register tree intype
= TREE_TYPE (expr
);
884 if (TREE_CODE (binfo
) == TREE_VEC
)
885 type
= BINFO_TYPE (binfo
);
886 else if (IS_AGGR_TYPE (binfo
))
896 ptr_type
= cp_build_type_variant (type
, TYPE_READONLY (TREE_TYPE (intype
)),
897 TYPE_VOLATILE (TREE_TYPE (intype
)));
898 ptr_type
= build_pointer_type (ptr_type
);
899 if (ptr_type
== TYPE_MAIN_VARIANT (intype
))
902 if (intype
== error_mark_node
)
903 return error_mark_node
;
905 my_friendly_assert (!integer_zerop (expr
), 191);
907 if (TREE_CODE (type
) == RECORD_TYPE
908 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
909 && type
!= TYPE_MAIN_VARIANT (TREE_TYPE (intype
)))
913 = get_base_distance (binfo
, TYPE_MAIN_VARIANT (TREE_TYPE (intype
)),
916 /* This function shouldn't be called with unqualified arguments
917 but if it is, give them an error message that they can read. */
920 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
921 TREE_TYPE (intype
), type
);
924 cp_error ("because `%T' is an ambiguous base class", type
);
925 return error_mark_node
;
928 return build_vbase_path (PLUS_EXPR
, ptr_type
, expr
, path
, 1);
930 rval
= build1 (NOP_EXPR
, ptr_type
,
931 TREE_CODE (expr
) == NOP_EXPR
? TREE_OPERAND (expr
, 0) : expr
);
932 TREE_CONSTANT (rval
) = TREE_CONSTANT (expr
);
936 /* Call this when we know (for any reason) that expr is
937 not, in fact, zero. This routine gets a type out of the first
938 argument and uses it to search for the type to convert to. If there
939 is more than one instance of that type in the expr, the conversion is
940 ambiguous. This routine should eventually go away, and all
941 callers should use convert_to_pointer_real. */
944 convert_pointer_to (binfo
, expr
)
949 if (TREE_CODE (binfo
) == TREE_VEC
)
950 type
= BINFO_TYPE (binfo
);
951 else if (IS_AGGR_TYPE (binfo
))
955 return convert_pointer_to_real (type
, expr
);
958 /* C++ conversions, preference to static cast conversions. */
961 cp_convert (type
, expr
)
964 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
);
969 FLAGS indicates how we should behave. */
972 ocp_convert (type
, expr
, convtype
, flags
)
976 register tree e
= expr
;
977 register enum tree_code code
= TREE_CODE (type
);
979 if (e
== error_mark_node
980 || TREE_TYPE (e
) == error_mark_node
)
981 return error_mark_node
;
983 if (IS_AGGR_TYPE (type
) && (convtype
& CONV_FORCE_TEMP
))
984 /* We need a new temporary; don't take this shortcut. */;
985 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (e
)))
986 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
987 return fold (build1 (NOP_EXPR
, type
, e
));
989 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
990 return build1 (CONVERT_EXPR
, type
, e
);
993 /* This is incorrect. A truncation can't be stripped this way.
994 Extensions will be stripped by the use of get_unwidened. */
995 if (TREE_CODE (e
) == NOP_EXPR
)
996 return cp_convert (type
, TREE_OPERAND (e
, 0));
999 /* Just convert to the type of the member. */
1000 if (code
== OFFSET_TYPE
)
1002 type
= TREE_TYPE (type
);
1003 code
= TREE_CODE (type
);
1007 if (code
== REFERENCE_TYPE
)
1008 return fold (convert_to_reference (type
, e
, convtype
, flags
, NULL_TREE
));
1009 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
1010 e
= convert_from_reference (e
);
1013 if (TREE_CODE (e
) == OFFSET_REF
)
1014 e
= resolve_offset_ref (e
);
1016 if (TREE_READONLY_DECL_P (e
))
1017 e
= decl_constant_value (e
);
1019 if (INTEGRAL_CODE_P (code
))
1021 tree intype
= TREE_TYPE (e
);
1022 /* enum = enum, enum = int, enum = float, (enum)pointer are all
1024 if (flag_int_enum_equivalence
== 0
1025 && TREE_CODE (type
) == ENUMERAL_TYPE
1026 && ((ARITHMETIC_TYPE_P (intype
) && ! (convtype
& CONV_STATIC
))
1027 || (TREE_CODE (intype
) == POINTER_TYPE
)))
1029 cp_pedwarn ("conversion from `%#T' to `%#T'", intype
, type
);
1031 if (flag_pedantic_errors
)
1032 return error_mark_node
;
1034 if (IS_AGGR_TYPE (intype
))
1037 rval
= build_type_conversion (CONVERT_EXPR
, type
, e
, 1);
1040 if (flags
& LOOKUP_COMPLAIN
)
1041 cp_error ("`%#T' used where a `%T' was expected", intype
, type
);
1042 if (flags
& LOOKUP_SPECULATIVELY
)
1044 return error_mark_node
;
1046 if (code
== BOOLEAN_TYPE
)
1048 /* Common Ada/Pascal programmer's mistake. We always warn
1049 about this since it is so bad. */
1050 if (TREE_CODE (expr
) == FUNCTION_DECL
)
1051 cp_warning ("the address of `%D', will always be `true'", expr
);
1052 return truthvalue_conversion (e
);
1054 return fold (convert_to_integer (type
, e
));
1056 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
1057 || TYPE_PTRMEMFUNC_P (type
))
1058 return fold (cp_convert_to_pointer (type
, e
));
1059 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
1061 if (IS_AGGR_TYPE (TREE_TYPE (e
)))
1064 rval
= build_type_conversion (CONVERT_EXPR
, type
, e
, 1);
1068 if (flags
& LOOKUP_COMPLAIN
)
1069 cp_error ("`%#T' used where a floating point value was expected",
1072 if (code
== REAL_TYPE
)
1073 return fold (convert_to_real (type
, e
));
1074 else if (code
== COMPLEX_TYPE
)
1075 return fold (convert_to_complex (type
, e
));
1078 /* New C++ semantics: since assignment is now based on
1079 memberwise copying, if the rhs type is derived from the
1080 lhs type, then we may still do a conversion. */
1081 if (IS_AGGR_TYPE_CODE (code
))
1083 tree dtype
= TREE_TYPE (e
);
1084 tree ctor
= NULL_TREE
;
1085 tree conversion
= NULL_TREE
;
1087 dtype
= TYPE_MAIN_VARIANT (dtype
);
1089 /* Conversion of object pointers or signature pointers/references
1090 to signature pointers/references. */
1092 if (TYPE_LANG_SPECIFIC (type
)
1093 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
1095 tree constructor
= build_signature_pointer_constructor (type
, expr
);
1096 tree sig_ty
= SIGNATURE_TYPE (type
);
1099 if (constructor
== error_mark_node
)
1100 return error_mark_node
;
1102 sig_ptr
= get_temp_name (type
, 1);
1103 DECL_INITIAL (sig_ptr
) = constructor
;
1104 CLEAR_SIGNATURE (sig_ty
);
1105 cp_finish_decl (sig_ptr
, constructor
, NULL_TREE
, 0, 0);
1106 SET_SIGNATURE (sig_ty
);
1107 TREE_READONLY (sig_ptr
) = 1;
1112 /* Conversion between aggregate types. New C++ semantics allow
1113 objects of derived type to be cast to objects of base type.
1114 Old semantics only allowed this between pointers.
1116 There may be some ambiguity between using a constructor
1117 vs. using a type conversion operator when both apply. */
1119 if (flag_ansi_overloading
)
1123 if ((flags
& LOOKUP_ONLYCONVERTING
)
1124 && ! (IS_AGGR_TYPE (dtype
) && DERIVED_FROM_P (type
, dtype
)))
1126 ctor
= build_user_type_conversion (type
, ctor
, flags
);
1127 flags
|= LOOKUP_NO_CONVERSION
;
1130 ctor
= build_method_call (NULL_TREE
, ctor_identifier
,
1131 build_expr_list (NULL_TREE
, ctor
),
1132 TYPE_BINFO (type
), flags
);
1134 return build_cplus_new (type
, ctor
);
1138 if (IS_AGGR_TYPE (dtype
) && ! DERIVED_FROM_P (type
, dtype
)
1139 && TYPE_HAS_CONVERSION (dtype
))
1140 conversion
= build_type_conversion (CONVERT_EXPR
, type
, e
, 1);
1142 if (conversion
== error_mark_node
)
1144 if (flags
& LOOKUP_COMPLAIN
)
1145 error ("ambiguous pointer conversion");
1149 if (TYPE_HAS_CONSTRUCTOR (complete_type (type
)))
1150 ctor
= build_method_call (NULL_TREE
, ctor_identifier
,
1151 build_expr_list (NULL_TREE
, e
),
1153 (flags
& LOOKUP_NORMAL
)
1154 | LOOKUP_SPECULATIVELY
1155 | (flags
& LOOKUP_ONLYCONVERTING
)
1156 | (flags
& LOOKUP_NO_CONVERSION
)
1157 | (conversion
? LOOKUP_NO_CONVERSION
: 0));
1159 if (ctor
== error_mark_node
)
1161 if (flags
& LOOKUP_COMPLAIN
)
1162 cp_error ("in conversion to type `%T'", type
);
1163 if (flags
& LOOKUP_SPECULATIVELY
)
1165 return error_mark_node
;
1168 if (conversion
&& ctor
)
1170 if (flags
& LOOKUP_COMPLAIN
)
1171 error ("both constructor and type conversion operator apply");
1172 if (flags
& LOOKUP_SPECULATIVELY
)
1174 return error_mark_node
;
1176 else if (conversion
)
1180 ctor
= build_cplus_new (type
, ctor
);
1186 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1187 then the it won't be hashed and hence compare as not equal,
1189 if (code
== ARRAY_TYPE
1190 && TREE_TYPE (TREE_TYPE (e
)) == TREE_TYPE (type
)
1191 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e
)), TYPE_DOMAIN (type
)))
1194 if (flags
& LOOKUP_COMPLAIN
)
1195 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1196 TREE_TYPE (expr
), type
);
1197 if (flags
& LOOKUP_SPECULATIVELY
)
1199 return error_mark_node
;
1202 /* Create an expression whose value is that of EXPR,
1203 converted to type TYPE. The TREE_TYPE of the value
1204 is always TYPE. This function implements all reasonable
1205 conversions; callers should filter out those that are
1206 not permitted by the language being compiled.
1208 Most of this routine is from build_reinterpret_cast.
1210 The backend cannot call cp_convert (what was convert) because
1211 conversions to/from basetypes may involve memory references
1212 (vbases) and adding or subtracting small values (multiple
1213 inheritance), but it calls convert from the constant folding code
1214 on subtrees of already build trees after it has ripped them apart.
1216 Also, if we ever support range variables, we'll probably also have to
1217 do a little bit more work. */
1220 convert (type
, expr
)
1225 if (type
== error_mark_node
|| expr
== error_mark_node
)
1226 return error_mark_node
;
1228 intype
= TREE_TYPE (expr
);
1230 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
1232 if (TREE_READONLY_DECL_P (expr
))
1233 expr
= decl_constant_value (expr
);
1234 return fold (build1 (NOP_EXPR
, type
, expr
));
1237 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
1238 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
1241 /* Like cp_convert, except permit conversions to take place which
1242 are not normally allowed due to access restrictions
1243 (such as conversion from sub-type to private super-type). */
1246 convert_force (type
, expr
, convtype
)
1251 register tree e
= expr
;
1252 register enum tree_code code
= TREE_CODE (type
);
1254 if (code
== REFERENCE_TYPE
)
1255 return fold (convert_to_reference (type
, e
, CONV_C_CAST
, LOOKUP_COMPLAIN
,
1257 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
1258 e
= convert_from_reference (e
);
1260 if (code
== POINTER_TYPE
)
1261 return fold (convert_to_pointer_force (type
, e
));
1263 /* From typeck.c convert_for_assignment */
1264 if (((TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
&& TREE_CODE (e
) == ADDR_EXPR
1265 && TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
1266 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1267 || integer_zerop (e
)
1268 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1269 && TYPE_PTRMEMFUNC_P (type
))
1271 /* compatible pointer to member functions. */
1272 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1);
1275 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
);
1278 /* Subroutine of build_type_conversion. */
1281 build_type_conversion_1 (xtype
, basetype
, expr
, typename
, for_sure
)
1282 tree xtype
, basetype
;
1291 flags
= LOOKUP_PROTECT
|LOOKUP_ONLYCONVERTING
;
1293 flags
= LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
;
1295 rval
= build_method_call (expr
, typename
, NULL_TREE
, NULL_TREE
, flags
);
1296 if (rval
== error_mark_node
)
1300 return error_mark_node
;
1303 if (IS_AGGR_TYPE (TREE_TYPE (rval
)))
1307 && TREE_TYPE (xtype
)
1308 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval
)))
1309 > TREE_READONLY (TREE_TYPE (xtype
))))
1310 warning ("user-defined conversion casting away `const'");
1311 return cp_convert (xtype
, rval
);
1314 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1315 exists, return the attempted conversion. This may
1316 return ERROR_MARK_NODE if the conversion is not
1317 allowed (references private members, etc).
1318 If no conversion exists, NULL_TREE is returned.
1320 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1321 to take place immediately. Otherwise, we build a SAVE_EXPR
1322 which can be evaluated if the results are ever needed.
1324 Changes to this functions should be mirrored in user_harshness.
1326 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1327 object parameter, or by the second standard conversion sequence if
1328 that doesn't do it. This will probably wait for an overloading rewrite.
1332 build_type_conversion (code
, xtype
, expr
, for_sure
)
1333 enum tree_code code
;
1337 /* C++: check to see if we can convert this aggregate type
1338 into the required type. */
1341 tree winner
= NULL_TREE
;
1343 if (flag_ansi_overloading
)
1344 return build_user_type_conversion
1345 (xtype
, expr
, for_sure
? LOOKUP_NORMAL
: 0);
1347 if (expr
== error_mark_node
)
1348 return error_mark_node
;
1350 basetype
= TREE_TYPE (expr
);
1351 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1352 basetype
= TREE_TYPE (basetype
);
1354 basetype
= TYPE_MAIN_VARIANT (basetype
);
1355 if (! TYPE_LANG_SPECIFIC (basetype
) || ! TYPE_HAS_CONVERSION (basetype
))
1358 /* Do we have an exact match? */
1360 tree typename
= build_typename_overload (xtype
);
1361 if (lookup_fnfields (TYPE_BINFO (basetype
), typename
, 0))
1362 return build_type_conversion_1 (xtype
, basetype
, expr
, typename
,
1366 /* Nope; try looking for others. */
1367 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1369 tree cand
= TREE_VALUE (conv
);
1371 if (winner
&& winner
== cand
)
1374 if (can_convert (xtype
, TREE_TYPE (TREE_TYPE (cand
))))
1380 cp_error ("ambiguous conversion from `%T' to `%T'", basetype
,
1382 cp_error (" candidate conversions include `%D' and `%D'",
1393 return build_type_conversion_1 (xtype
, basetype
, expr
,
1394 DECL_NAME (winner
), for_sure
);
1399 /* Convert the given EXPR to one of a group of types suitable for use in an
1400 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1401 which indicates which types are suitable. If COMPLAIN is 1, complain
1402 about ambiguity; otherwise, the caller will deal with it. */
1405 build_expr_type_conversion (desires
, expr
, complain
)
1410 tree basetype
= TREE_TYPE (expr
);
1412 tree winner
= NULL_TREE
;
1414 if (TREE_CODE (basetype
) == OFFSET_TYPE
)
1415 expr
= resolve_offset_ref (expr
);
1416 expr
= convert_from_reference (expr
);
1417 basetype
= TREE_TYPE (expr
);
1419 if (! IS_AGGR_TYPE (basetype
))
1420 switch (TREE_CODE (basetype
))
1423 if ((desires
& WANT_NULL
) && TREE_CODE (expr
) == INTEGER_CST
1424 && integer_zerop (expr
))
1426 /* else fall through... */
1429 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1431 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1433 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1435 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1439 return (desires
& WANT_POINTER
) ? default_conversion (expr
)
1445 if (! TYPE_HAS_CONVERSION (basetype
))
1448 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1452 tree cand
= TREE_VALUE (conv
);
1454 if (winner
&& winner
== cand
)
1457 candidate
= TREE_TYPE (TREE_TYPE (cand
));
1458 if (TREE_CODE (candidate
) == REFERENCE_TYPE
)
1459 candidate
= TREE_TYPE (candidate
);
1461 switch (TREE_CODE (candidate
))
1465 win
= (desires
& WANT_INT
); break;
1467 win
= (desires
& WANT_ENUM
); break;
1469 win
= (desires
& WANT_FLOAT
); break;
1471 win
= (desires
& WANT_POINTER
); break;
1483 cp_error ("ambiguous default type conversion from `%T'",
1485 cp_error (" candidate conversions include `%D' and `%D'",
1488 return error_mark_node
;
1497 tree type
= TREE_TYPE (TREE_TYPE (winner
));
1498 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1499 type
= TREE_TYPE (type
);
1500 return build_type_conversion_1 (type
, basetype
, expr
,
1501 DECL_NAME (winner
), 1);
1507 /* Must convert two aggregate types to non-aggregate type.
1508 Attempts to find a non-ambiguous, "best" type conversion.
1510 Return 1 on success, 0 on failure.
1512 @@ What are the real semantics of this supposed to be??? */
1515 build_default_binary_type_conversion (code
, arg1
, arg2
)
1516 enum tree_code code
;
1522 case TRUNC_DIV_EXPR
:
1524 case FLOOR_DIV_EXPR
:
1525 case ROUND_DIV_EXPR
:
1526 case EXACT_DIV_EXPR
:
1527 *arg1
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg1
, 0);
1528 *arg2
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg2
, 0);
1531 case TRUNC_MOD_EXPR
:
1532 case FLOOR_MOD_EXPR
:
1538 *arg1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, *arg1
, 0);
1539 *arg2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, *arg2
, 0);
1544 tree a1
, a2
, p1
, p2
;
1547 a1
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg1
, 0);
1548 a2
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg2
, 0);
1549 p1
= build_expr_type_conversion (WANT_POINTER
, *arg1
, 0);
1550 p2
= build_expr_type_conversion (WANT_POINTER
, *arg2
, 0);
1552 wins
= (a1
&& a2
) + (a1
&& p2
) + (p1
&& a2
);
1555 error ("ambiguous default type conversion for `operator +'");
1558 *arg1
= a1
, *arg2
= a2
;
1560 *arg1
= a1
, *arg2
= p2
;
1562 *arg1
= p1
, *arg2
= a2
;
1568 tree a1
, a2
, p1
, p2
;
1571 a1
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg1
, 0);
1572 a2
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg2
, 0);
1573 p1
= build_expr_type_conversion (WANT_POINTER
, *arg1
, 0);
1574 p2
= build_expr_type_conversion (WANT_POINTER
, *arg2
, 0);
1576 wins
= (a1
&& a2
) + (p1
&& p2
) + (p1
&& a2
);
1579 error ("ambiguous default type conversion for `operator -'");
1582 *arg1
= a1
, *arg2
= a2
;
1584 *arg1
= p1
, *arg2
= p2
;
1586 *arg1
= p1
, *arg2
= a2
;
1597 tree a1
, a2
, p1
, p2
;
1600 a1
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg1
, 0);
1601 a2
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, *arg2
, 0);
1602 p1
= build_expr_type_conversion (WANT_POINTER
| WANT_NULL
, *arg1
, 0);
1603 p2
= build_expr_type_conversion (WANT_POINTER
| WANT_NULL
, *arg2
, 0);
1605 wins
= (a1
&& a2
) + (p1
&& p2
);
1608 cp_error ("ambiguous default type conversion for `%O'", code
);
1611 *arg1
= a1
, *arg2
= a2
;
1613 *arg1
= p1
, *arg2
= p2
;
1617 case TRUTH_ANDIF_EXPR
:
1618 case TRUTH_ORIF_EXPR
:
1619 *arg1
= cp_convert (boolean_type_node
, *arg1
);
1620 *arg2
= cp_convert (boolean_type_node
, *arg2
);
1628 if (*arg1
== error_mark_node
|| *arg2
== error_mark_node
)
1629 cp_error ("ambiguous default type conversion for `%O'", code
);
1637 /* Implements integral promotion (4.1) and float->double promotion. */
1640 type_promotes_to (type
)
1643 int constp
, volatilep
;
1645 if (type
== error_mark_node
)
1646 return error_mark_node
;
1648 constp
= TYPE_READONLY (type
);
1649 volatilep
= TYPE_VOLATILE (type
);
1650 type
= TYPE_MAIN_VARIANT (type
);
1652 /* bool always promotes to int (not unsigned), even if it's the same
1654 if (type
== boolean_type_node
)
1655 type
= integer_type_node
;
1657 /* Normally convert enums to int, but convert wide enums to something
1659 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1660 || type
== wchar_type_node
)
1662 int precision
= MAX (TYPE_PRECISION (type
),
1663 TYPE_PRECISION (integer_type_node
));
1664 tree totype
= type_for_size (precision
, 0);
1665 if (TREE_UNSIGNED (type
)
1666 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1667 type
= type_for_size (precision
, 1);
1671 else if (C_PROMOTING_INTEGER_TYPE_P (type
))
1673 /* Retain unsignedness if really not getting bigger. */
1674 if (TREE_UNSIGNED (type
)
1675 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1676 type
= unsigned_type_node
;
1678 type
= integer_type_node
;
1680 else if (type
== float_type_node
)
1681 type
= double_type_node
;
1683 return cp_build_type_variant (type
, constp
, volatilep
);
1687 /* The routines below this point are carefully written to conform to
1688 the standard. They use the same terminology, and follow the rules
1689 closely. Although they are used only in pt.c at the moment, they
1690 should presumably be used everywhere in the future. */
1692 /* Attempt to perform qualification conversions on EXPR to convert it
1693 to TYPE. Return the resulting expression, or error_mark_node if
1694 the conversion was impossible. */
1697 perform_qualification_conversions (type
, expr
)
1701 if (comp_ptr_ttypes (type
, TREE_TYPE(expr
)))
1702 return build1 (NOP_EXPR
, type
, expr
);
1704 return error_mark_node
;