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. */
36 #define NULL (char *)0
38 tree build_user_type_conversion ();
40 /* Change of width--truncation and extension of integers or reals--
41 is represented with NOP_EXPR. Proper functioning of many things
42 assumes that no other conversions can be NOP_EXPRs.
44 Conversion between integer and pointer is represented with CONVERT_EXPR.
45 Converting integer to real uses FLOAT_EXPR
46 and real to integer uses FIX_TRUNC_EXPR.
48 Here is a list of all the functions that assume that widening and
49 narrowing is always done with a NOP_EXPR:
50 In convert.c, convert_to_integer.
51 In c-typeck.c, build_binary_op_nodefault (boolean ops),
52 and truthvalue_conversion.
53 In expr.c: expand_expr, for operands of a MULT_EXPR.
54 In fold-const.c: fold.
55 In tree.c: get_narrower and get_unwidened.
57 C++: in multiple-inheritance, converting between pointers may involve
58 adjusting them by a delta stored within the class definition. */
60 /* Subroutines of `convert'. */
62 /* Build a thunk. What it is, is an entry point that when called will
63 adjust the this pointer (the first argument) by offset, and then
64 goto the real address of the function given by REAL_ADDR that we
65 would like called. What we return is the address of the thunk. */
68 build_thunk (offset, real_addr)
69 tree offset, real_addr;
71 if (TREE_CODE (real_addr) != ADDR_EXPR
72 || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
74 sorry ("MI pointer to member conversion too complex");
75 return error_mark_node;
77 sorry ("MI pointer to member conversion too complex");
78 return error_mark_node;
81 /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
82 another `pointer to method'. This may involved the creation of
83 a thunk to handle the this offset calculation. */
86 convert_fn_ptr (type, expr)
89 #if 0 /* We don't use thunks for pmfs. */
90 if (flag_vtable_thunks)
92 tree intype = TREE_TYPE (expr);
93 tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype)),
94 TYPE_METHOD_BASETYPE (TREE_TYPE (type)), 1);
95 if (binfo == error_mark_node)
97 error (" in pointer to member conversion");
98 return error_mark_node;
100 if (binfo == NULL_TREE)
102 /* ARM 4.8 restriction. */
103 error ("invalid pointer to member conversion");
104 return error_mark_node;
107 if (BINFO_OFFSET_ZEROP (binfo))
108 return build1 (NOP_EXPR, type, expr);
109 return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
113 return build_ptrmemfunc (type, expr, 1);
116 /* if converting pointer to pointer
117 if dealing with classes, check for derived->base or vice versa
118 else if dealing with method pointers, delegate
120 else if converting class, pass off to build_type_conversion
121 else try C-style pointer conversion */
124 cp_convert_to_pointer (type, expr)
127 register tree intype = TREE_TYPE (expr);
128 register enum tree_code form;
130 if (IS_AGGR_TYPE (intype))
134 intype = complete_type (intype);
135 if (TYPE_SIZE (intype) == NULL_TREE)
137 cp_error ("can't convert from incomplete type `%T' to `%T'",
139 return error_mark_node;
142 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
145 if (rval == error_mark_node)
146 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
152 if (TYPE_PTRMEMFUNC_P (type))
153 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
154 if (TYPE_PTRMEMFUNC_P (intype))
155 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
157 form = TREE_CODE (intype);
159 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
161 intype = TYPE_MAIN_VARIANT (intype);
163 if (TYPE_MAIN_VARIANT (type) != intype
164 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
165 && IS_AGGR_TYPE (TREE_TYPE (type))
166 && IS_AGGR_TYPE (TREE_TYPE (intype))
167 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
169 enum tree_code code = PLUS_EXPR;
170 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
171 if (binfo == error_mark_node)
172 return error_mark_node;
173 if (binfo == NULL_TREE)
175 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
176 if (binfo == error_mark_node)
177 return error_mark_node;
182 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
183 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
184 || ! BINFO_OFFSET_ZEROP (binfo))
186 /* Need to get the path we took. */
189 if (code == PLUS_EXPR)
190 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
192 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
193 return build_vbase_path (code, type, expr, path, 0);
197 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
198 && TREE_CODE (type) == POINTER_TYPE
199 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
200 return convert_fn_ptr (type, expr);
202 if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
203 && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
205 tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
206 tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
207 tree binfo = get_binfo (b1, b2, 1);
208 if (binfo == NULL_TREE)
209 binfo = get_binfo (b2, b1, 1);
210 if (binfo == error_mark_node)
211 return error_mark_node;
214 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
215 || (TREE_CODE (type) == POINTER_TYPE
216 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
218 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
220 return error_mark_node;
223 return build1 (NOP_EXPR, type, expr);
226 my_friendly_assert (form != OFFSET_TYPE, 186);
228 if (TYPE_LANG_SPECIFIC (intype)
229 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
230 return convert_to_pointer (type, build_optr_ref (expr));
232 if (integer_zerop (expr))
234 expr = build_int_2 (0, 0);
235 TREE_TYPE (expr) = type;
239 if (INTEGRAL_CODE_P (form))
241 if (type_precision (intype) == POINTER_SIZE)
242 return build1 (CONVERT_EXPR, type, expr);
243 expr = convert (type_for_size (POINTER_SIZE, 0), expr);
244 /* Modes may be different but sizes should be the same. */
245 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
246 != GET_MODE_SIZE (TYPE_MODE (type)))
247 /* There is supposed to be some integral type
248 that is the same width as a pointer. */
250 return convert_to_pointer (type, expr);
253 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
255 return error_mark_node;
258 /* Like convert, except permit conversions to take place which
259 are not normally allowed due to access restrictions
260 (such as conversion from sub-type to private super-type). */
263 convert_to_pointer_force (type, expr)
266 register tree intype = TREE_TYPE (expr);
267 register enum tree_code form = TREE_CODE (intype);
269 if (integer_zerop (expr))
271 expr = build_int_2 (0, 0);
272 TREE_TYPE (expr) = type;
276 /* Convert signature pointer/reference to `void *' first. */
277 if (form == RECORD_TYPE
278 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
280 expr = build_optr_ref (expr);
281 intype = TREE_TYPE (expr);
282 form = TREE_CODE (intype);
285 if (form == POINTER_TYPE)
287 intype = TYPE_MAIN_VARIANT (intype);
289 if (TYPE_MAIN_VARIANT (type) != intype
290 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
291 && IS_AGGR_TYPE (TREE_TYPE (type))
292 && IS_AGGR_TYPE (TREE_TYPE (intype))
293 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
295 enum tree_code code = PLUS_EXPR;
297 int distance = get_base_distance (TREE_TYPE (type),
298 TREE_TYPE (intype), 0, &path);
302 cp_error ("type `%T' is ambiguous baseclass of `%s'", TREE_TYPE (type),
303 TYPE_NAME_STRING (TREE_TYPE (intype)));
304 return error_mark_node;
308 distance = get_base_distance (TREE_TYPE (intype),
309 TREE_TYPE (type), 0, &path);
313 /* Doesn't need any special help from us. */
314 return build1 (NOP_EXPR, type, expr);
318 return build_vbase_path (code, type, expr, path, 0);
320 return build1 (NOP_EXPR, type, expr);
323 return cp_convert_to_pointer (type, expr);
326 /* We are passing something to a function which requires a reference.
327 The type we are interested in is in TYPE. The initial
328 value we have to begin with is in ARG.
330 FLAGS controls how we manage access checking.
331 INDIRECT_BIND in FLAGS controls how any temporarys are generated.
332 CHECKCONST controls if we report error messages on const subversion. */
335 build_up_reference (type, arg, flags, checkconst)
337 int flags, checkconst;
340 int literal_flag = 0;
341 tree argtype = TREE_TYPE (arg);
342 tree target_type = TREE_TYPE (type);
343 tree binfo = NULL_TREE;
345 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
346 if ((flags & LOOKUP_PROTECT)
347 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
348 && IS_AGGR_TYPE (argtype)
349 && IS_AGGR_TYPE (target_type))
351 binfo = get_binfo (target_type, argtype, 1);
352 if (binfo == error_mark_node)
353 return error_mark_node;
354 if (binfo == NULL_TREE)
355 return error_not_base_type (target_type, argtype);
358 /* Pass along const and volatile down into the type. */
359 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
360 target_type = cp_build_type_variant (target_type, TYPE_READONLY (type),
361 TYPE_VOLATILE (type));
363 if (TREE_CODE (targ) == SAVE_EXPR)
364 targ = TREE_OPERAND (targ, 0);
365 while (TREE_CODE (targ) == NOP_EXPR
366 && (TYPE_MAIN_VARIANT (argtype)
367 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0)))))
368 targ = TREE_OPERAND (targ, 0);
370 switch (TREE_CODE (targ))
373 /* This is a call to a constructor which did not know what it was
374 initializing until now: it needs to initialize a temporary. */
375 if (TREE_HAS_CONSTRUCTOR (targ))
377 tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0));
378 TREE_HAS_CONSTRUCTOR (targ) = 0;
379 return build_up_reference (type, temp, flags, 1);
381 /* Let &* cancel out to simplify resulting code.
382 Also, throw away intervening NOP_EXPRs. */
383 arg = TREE_OPERAND (targ, 0);
384 if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
385 || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
386 arg = TREE_OPERAND (arg, 0);
388 /* in doing a &*, we have to get rid of the const'ness on the pointer
389 value. Haven't thought about volatile here. Pointers come to mind
391 if (TREE_READONLY (arg))
393 arg = copy_node (arg);
394 TREE_READONLY (arg) = 0;
397 rval = build1 (CONVERT_EXPR, type, arg);
398 TREE_REFERENCE_EXPR (rval) = 1;
400 /* propagate the const flag on something like:
407 class Derived : public Base {
414 void func2(const Derived& d) {
418 on the d parameter. The below could have been avoided, if the flags
419 were down in the tree, not sure why they are not. (mrs) */
420 /* The below code may have to be propagated to other parts of this
422 if (TREE_READONLY (targ) && !TREE_READONLY (arg)
423 && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
424 && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
425 && (TYPE_READONLY (target_type) && checkconst))
427 arg = copy_node (arg);
428 TREE_READONLY (arg) = TREE_READONLY (targ);
430 literal_flag = TREE_CONSTANT (arg);
434 /* Get this out of a register if we happened to be in one by accident.
435 Also, build up references to non-lvalues it we must. */
436 /* For &x[y], return (&) x+y */
438 if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
439 return error_mark_node;
440 rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
441 TREE_OPERAND (targ, 1), 1);
442 TREE_TYPE (rval) = type;
443 if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
444 && staticp (TREE_OPERAND (targ, 0)))
445 TREE_CONSTANT (rval) = 1;
449 /* Could be a reference to a static member. */
451 tree field = TREE_OPERAND (targ, 1);
452 if (TREE_STATIC (field))
454 rval = build1 (ADDR_EXPR, type, field);
460 /* We should have farmed out member pointers above. */
461 my_friendly_abort (188);
464 rval = build_component_addr (targ, build_pointer_type (argtype),
465 "attempt to make a reference to bit-field structure member `%s'");
466 TREE_TYPE (rval) = type;
467 literal_flag = staticp (TREE_OPERAND (targ, 0));
471 /* Anything not already handled and not a true memory reference
472 needs to have a reference built up. Do so silently for
473 things like integers and return values from function,
474 but complain if we need a reference to something declared
480 TREE_ADDRESSABLE (targ) = 1;
481 put_var_into_stack (targ);
486 if (targ == current_class_ptr)
488 error ("address of `this' not available");
490 /* This code makes the following core dump the compiler on a sun4,
491 if the code below is used.
495 typedef a_decl* a_ref;
500 void* append(a_ref& item);
504 a_decl (e_decl *parent);
518 a_decl::a_decl(e_decl *parent) {
519 parent->implementations.append(this);
523 TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
524 put_var_into_stack (targ);
527 return error_mark_node;
534 if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ)
535 && !DECL_ARTIFICIAL (targ))
536 cp_warning ("address needed to build reference for `%D', which is declared `register'",
538 else if (staticp (targ))
541 TREE_ADDRESSABLE (targ) = 1;
542 put_var_into_stack (targ);
547 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
549 rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
550 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
554 case PREINCREMENT_EXPR:
555 case PREDECREMENT_EXPR:
559 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
561 rval = build (COMPOUND_EXPR, type, arg, real_reference);
562 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
567 return build (COND_EXPR, type,
568 TREE_OPERAND (targ, 0),
569 build_up_reference (type, TREE_OPERAND (targ, 1),
571 build_up_reference (type, TREE_OPERAND (targ, 2),
574 /* Undo the folding... */
577 return build (COND_EXPR, type,
578 build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR,
579 boolean_type_node, TREE_OPERAND (targ, 0),
580 TREE_OPERAND (targ, 1)),
581 build_up_reference (type, TREE_OPERAND (targ, 0),
583 build_up_reference (type, TREE_OPERAND (targ, 1),
587 arg = TREE_OPERAND (targ, 1);
588 if (arg == NULL_TREE)
590 compiler_error ("({ ... }) expression not expanded when needed for reference");
591 return error_mark_node;
593 rval = build1 (ADDR_EXPR, type, arg);
594 TREE_REFERENCE_EXPR (rval) = 1;
601 if (TREE_ADDRESSABLE (targ) == 0)
603 if (! (flags&INDIRECT_BIND)
604 && toplevel_bindings_p ())
606 tree temp = get_temp_name (argtype, 0);
607 /* Give this new temp some rtl and initialize it. */
608 DECL_INITIAL (temp) = targ;
609 TREE_STATIC (temp) = 1;
610 cp_finish_decl (temp, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
611 /* Do this after declaring it static. */
612 rval = build_unary_op (ADDR_EXPR, temp, 0);
613 TREE_TYPE (rval) = type;
614 literal_flag = TREE_CONSTANT (rval);
618 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
620 arg = build_cplus_new (argtype, targ);
622 else if (flags&INDIRECT_BIND)
624 /* This should be the default, not the below code. */
625 /* All callers except grok_reference_init should probably
626 use INDIRECT_BIND. */
627 tree slot = build (VAR_DECL, argtype);
628 layout_decl (slot, 0);
629 arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
633 tree temp = get_temp_name (argtype, 0);
634 rval = build_unary_op (ADDR_EXPR, temp, 0);
635 if (binfo && !BINFO_OFFSET_ZEROP (binfo))
636 rval = convert_pointer_to (target_type, rval);
638 TREE_TYPE (rval) = type;
640 temp = build (MODIFY_EXPR, argtype, temp, arg);
641 TREE_SIDE_EFFECTS (temp) = 1;
642 return build (COMPOUND_EXPR, type, temp, rval);
646 if (! (flags&INDIRECT_BIND))
648 if (TREE_CODE (arg) == TARGET_EXPR)
650 tree decl = TREE_OPERAND (arg, 0);
653 if (! toplevel_bindings_p () && ! DECL_RTL (decl))
656 cleanup = maybe_build_cleanup (decl);
658 expand_decl_cleanup (decl, cleanup);
663 rval = build1 (ADDR_EXPR, type, arg);
666 if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
667 || TYPE_USES_COMPLEX_INHERITANCE (target_type))
669 TREE_TYPE (rval) = build_pointer_type (argtype);
670 if (flags & LOOKUP_PROTECT)
671 rval = convert_pointer_to (target_type, rval);
674 = convert_to_pointer_force (build_pointer_type (target_type), rval);
675 TREE_TYPE (rval) = type;
676 if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR)
677 TREE_TYPE (TREE_OPERAND (rval, 0))
678 = TREE_TYPE (TREE_OPERAND (rval, 1)) = type;
680 TREE_CONSTANT (rval) = literal_flag;
684 /* For C++: Only need to do one-level references, but cannot
685 get tripped up on signed/unsigned differences.
687 DECL is either NULL_TREE or the _DECL node for a reference that is being
688 initialized. It can be error_mark_node if we don't know the _DECL but
689 we know it's an initialization. */
692 convert_to_reference (reftype, expr, convtype, flags, decl)
697 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
698 register tree intype = TREE_TYPE (expr);
699 tree rval = NULL_TREE;
700 tree rval_as_conversion = NULL_TREE;
703 if (TREE_CODE (intype) == REFERENCE_TYPE)
704 my_friendly_abort (364);
706 intype = TYPE_MAIN_VARIANT (intype);
708 i = comp_target_types (type, intype, 0);
710 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
711 && ! (flags & LOOKUP_NO_CONVERSION))
713 /* Look for a user-defined conversion to lvalue that we can use. */
715 if (flag_ansi_overloading)
717 = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
719 rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
721 if (rval_as_conversion && rval_as_conversion != error_mark_node
722 && real_lvalue_p (rval_as_conversion))
724 expr = rval_as_conversion;
725 rval_as_conversion = NULL_TREE;
731 if (((convtype & CONV_STATIC) && i == -1)
732 || ((convtype & CONV_IMPLICIT) && i == 1))
734 if (flags & LOOKUP_COMPLAIN)
736 tree ttl = TREE_TYPE (reftype);
740 int r = TREE_READONLY (expr);
741 int v = TREE_THIS_VOLATILE (expr);
742 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
745 if (! real_lvalue_p (expr) &&
746 (decl == NULL_TREE || ! TYPE_READONLY (ttl)))
749 /* Ensure semantics of [dcl.init.ref] */
750 cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'",
753 cp_pedwarn ("conversion to `%T' from rvalue `%T'",
756 else if (! (convtype & CONV_CONST))
758 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
759 cp_pedwarn ("conversion from `%T' to `%T' discards const",
761 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
762 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
767 return build_up_reference (reftype, expr, flags,
768 ! (convtype & CONV_CONST));
770 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
772 /* When casting an lvalue to a reference type, just convert into
773 a pointer to the new type and deference it. This is allowed
774 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
775 should be done directly (jason). (int &)ri ---> *(int*)&ri */
777 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
779 if (TREE_CODE (intype) == POINTER_TYPE
780 && (comptypes (TREE_TYPE (intype), type, -1)))
781 cp_warning ("casting `%T' to `%T' does not dereference pointer",
784 rval = build_unary_op (ADDR_EXPR, expr, 0);
785 if (rval != error_mark_node)
786 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
787 if (rval != error_mark_node)
788 rval = build1 (NOP_EXPR, reftype, rval);
792 tree rval_as_ctor = NULL_TREE;
794 if (rval_as_conversion)
796 if (rval_as_conversion == error_mark_node)
798 cp_error ("conversion from `%T' to `%T' is ambiguous",
800 return error_mark_node;
802 rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
806 /* Definitely need to go through a constructor here. */
807 if (TYPE_HAS_CONSTRUCTOR (type)
808 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
809 && (rval = build_method_call
810 (NULL_TREE, ctor_identifier,
811 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
812 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
813 | LOOKUP_ONLYCONVERTING)))
817 if (toplevel_bindings_p ())
819 extern tree static_aggregates;
820 tree t = get_temp_name (type, toplevel_bindings_p ());
821 init = build_method_call (t, ctor_identifier,
822 build_tree_list (NULL_TREE, expr),
824 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
825 | LOOKUP_ONLYCONVERTING);
827 if (init == error_mark_node)
828 return error_mark_node;
830 make_decl_rtl (t, NULL_PTR, 1);
831 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
832 rval = build_unary_op (ADDR_EXPR, t, 0);
836 init = build_method_call (NULL_TREE, ctor_identifier,
837 build_tree_list (NULL_TREE, expr),
839 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
840 |LOOKUP_ONLYCONVERTING);
842 if (init == error_mark_node)
843 return error_mark_node;
845 rval = build_cplus_new (type, init);
846 rval = build_up_reference (reftype, rval, flags, 1);
851 if (rval_as_ctor && rval_as_conversion)
853 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
855 return error_mark_node;
857 else if (rval_as_ctor)
859 else if (rval_as_conversion)
860 rval = rval_as_conversion;
861 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
863 rval = convert (type, expr);
864 if (rval == error_mark_node)
865 return error_mark_node;
867 rval = build_up_reference (reftype, rval, flags, 1);
870 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
871 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
877 /* If we found a way to convert earlier, then use it. */
881 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
883 if (flags & LOOKUP_COMPLAIN)
884 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
886 if (flags & LOOKUP_SPECULATIVELY)
889 return error_mark_node;
892 /* We are using a reference VAL for its value. Bash that reference all the
893 way down to its lowest form. */
896 convert_from_reference (val)
899 tree type = TREE_TYPE (val);
901 if (TREE_CODE (type) == OFFSET_TYPE)
902 type = TREE_TYPE (type);
903 if (TREE_CODE (type) == REFERENCE_TYPE)
904 return build_indirect_ref (val, NULL_PTR);
908 /* See if there is a constructor of type TYPE which will convert
909 EXPR. The reference manual seems to suggest (8.5.6) that we need
910 not worry about finding constructors for base classes, then converting
911 to the derived class.
913 MSGP is a pointer to a message that would be an appropriate error
914 string. If MSGP is NULL, then we are not interested in reporting
918 convert_to_aggr (type, expr, msgp, protect)
923 tree basetype = type;
924 tree name = TYPE_IDENTIFIER (basetype);
925 tree function, fndecl, fntype, parmtypes, parmlist, result;
927 /* See code below that used this. */
931 int can_be_private, can_be_protected;
933 if (! TYPE_HAS_CONSTRUCTOR (basetype))
936 *msgp = "type `%s' does not have a constructor";
937 return error_mark_node;
940 access = access_public_node;
942 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
944 parmlist = build_tree_list (NULL_TREE, expr);
945 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
947 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
949 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
950 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
953 /* The type of the first argument will be filled in inside the loop. */
954 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
955 parmtypes = tree_cons (NULL_TREE, build_pointer_type (basetype), parmtypes);
957 /* No exact conversion was found. See if an approximate
959 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
963 int saw_protected = 0;
964 struct candidate *candidates =
965 (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
966 struct candidate *cp = candidates;
972 cp->harshness = (struct harshness_code *)
973 alloca (3 * sizeof (struct harshness_code));
975 compute_conversion_costs (fndecl, parmlist, cp, 2);
976 if ((cp->h.code & EVIL_CODE) == 0)
978 cp->u.field = fndecl;
981 if (TREE_PRIVATE (fndecl))
982 access = access_private_node;
983 else if (TREE_PROTECTED (fndecl))
984 access = access_protected_node;
986 access = access_public_node;
989 access = access_public_node;
991 if (access == access_private_node
992 ? (basetype == current_class_type
993 || is_friend (basetype, cp->function)
994 || purpose_member (basetype, DECL_ACCESS (fndecl)))
995 : access == access_protected_node
997 || purpose_member (basetype, DECL_ACCESS (fndecl)))
1000 if (cp->h.code <= TRIVIAL_CODE)
1006 if (access == access_private_node)
1012 fndecl = DECL_CHAIN (fndecl);
1014 if (cp - candidates)
1016 /* Rank from worst to best. Then cp will point to best one.
1017 Private fields have their bits flipped. For unsigned
1018 numbers, this should make them look very large.
1019 If the best alternate has a (signed) negative value,
1020 then all we ever saw were private members. */
1021 if (cp - candidates > 1)
1022 qsort (candidates, /* char *base */
1023 cp - candidates, /* int nel */
1024 sizeof (struct candidate), /* int width */
1025 rank_for_overload); /* int (*compar)() */
1028 if (cp->h.code & EVIL_CODE)
1031 *msgp = "ambiguous type conversion possible for `%s'";
1032 return error_mark_node;
1035 function = cp->function;
1036 fndecl = cp->u.field;
1043 *msgp = "only private and protected conversions apply";
1045 *msgp = "only private conversions apply";
1046 else if (saw_protected)
1047 *msgp = "only protected conversions apply";
1049 *msgp = "no appropriate conversion to type `%s'";
1051 return error_mark_node;
1056 if (access == access_private_node)
1057 if (! can_be_private)
1060 *msgp = TREE_PRIVATE (fndecl)
1061 ? "conversion to type `%s' is private"
1062 : "conversion to type `%s' is from private base class";
1063 return error_mark_node;
1065 if (access == access_protected_node)
1066 if (! can_be_protected)
1069 *msgp = TREE_PRIVATE (fndecl)
1070 ? "conversion to type `%s' is protected"
1071 : "conversion to type `%s' is from protected base class";
1072 return error_mark_node;
1077 /* It will convert, but we don't do anything about it yet. */
1081 fntype = TREE_TYPE (function);
1083 parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1084 parmlist, NULL_TREE, LOOKUP_NORMAL);
1086 result = build_call (function, TREE_TYPE (fntype), parmlist);
1090 /* Call this when we know (for any reason) that expr is not, in fact,
1091 zero. This routine is like convert_pointer_to, but it pays
1092 attention to which specific instance of what type we want to
1093 convert to. This routine should eventually become
1094 convert_to_pointer after all references to convert_to_pointer
1098 convert_pointer_to_real (binfo, expr)
1101 register tree intype = TREE_TYPE (expr);
1105 if (TREE_CODE (binfo) == TREE_VEC)
1106 type = BINFO_TYPE (binfo);
1107 else if (IS_AGGR_TYPE (binfo))
1117 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
1118 TYPE_VOLATILE (TREE_TYPE (intype)));
1119 ptr_type = build_pointer_type (ptr_type);
1120 if (ptr_type == TYPE_MAIN_VARIANT (intype))
1123 if (intype == error_mark_node)
1124 return error_mark_node;
1126 my_friendly_assert (!integer_zerop (expr), 191);
1128 if (TREE_CODE (type) == RECORD_TYPE
1129 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1130 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1134 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1137 /* This function shouldn't be called with unqualified arguments
1138 but if it is, give them an error message that they can read. */
1141 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1142 TREE_TYPE (intype), type);
1145 cp_error ("because `%T' is an ambiguous base class", type);
1146 return error_mark_node;
1149 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1151 rval = build1 (NOP_EXPR, ptr_type,
1152 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1153 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1157 /* Call this when we know (for any reason) that expr is
1158 not, in fact, zero. This routine gets a type out of the first
1159 argument and uses it to search for the type to convert to. If there
1160 is more than one instance of that type in the expr, the conversion is
1161 ambiguous. This routine should eventually go away, and all
1162 callers should use convert_to_pointer_real. */
1165 convert_pointer_to (binfo, expr)
1170 if (TREE_CODE (binfo) == TREE_VEC)
1171 type = BINFO_TYPE (binfo);
1172 else if (IS_AGGR_TYPE (binfo))
1176 return convert_pointer_to_real (type, expr);
1181 FLAGS indicates how we should behave. */
1184 cp_convert (type, expr, convtype, flags)
1186 int convtype, flags;
1188 register tree e = expr;
1189 register enum tree_code code = TREE_CODE (type);
1191 if (TREE_CODE (e) == ERROR_MARK
1192 || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
1193 return error_mark_node;
1195 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
1196 /* We need a new temporary; don't take this shortcut. */;
1197 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
1198 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
1199 return fold (build1 (NOP_EXPR, type, e));
1201 if (code == VOID_TYPE && (convtype & CONV_STATIC))
1202 return build1 (CONVERT_EXPR, type, e);
1205 /* This is incorrect. A truncation can't be stripped this way.
1206 Extensions will be stripped by the use of get_unwidened. */
1207 if (TREE_CODE (e) == NOP_EXPR)
1208 return convert (type, TREE_OPERAND (e, 0));
1211 /* Just convert to the type of the member. */
1212 if (code == OFFSET_TYPE)
1214 type = TREE_TYPE (type);
1215 code = TREE_CODE (type);
1219 if (code == REFERENCE_TYPE)
1220 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1221 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1222 e = convert_from_reference (e);
1225 if (TREE_CODE (e) == OFFSET_REF)
1226 e = resolve_offset_ref (e);
1228 if (TREE_READONLY_DECL_P (e))
1229 e = decl_constant_value (e);
1231 if (INTEGRAL_CODE_P (code))
1233 tree intype = TREE_TYPE (e);
1234 /* enum = enum, enum = int, enum = float are all errors. */
1235 if (flag_int_enum_equivalence == 0
1236 && TREE_CODE (type) == ENUMERAL_TYPE
1237 && ARITHMETIC_TYPE_P (intype)
1238 && ! (convtype & CONV_STATIC))
1240 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1242 if (flag_pedantic_errors)
1243 return error_mark_node;
1245 if (IS_AGGR_TYPE (intype))
1248 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1251 if (flags & LOOKUP_COMPLAIN)
1252 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1253 if (flags & LOOKUP_SPECULATIVELY)
1255 return error_mark_node;
1257 if (code == BOOLEAN_TYPE)
1259 /* Common Ada/Pascal programmer's mistake. We always warn
1260 about this since it is so bad. */
1261 if (TREE_CODE (expr) == FUNCTION_DECL)
1262 cp_warning ("the address of `%D', will always be `true'", expr);
1263 return truthvalue_conversion (e);
1265 return fold (convert_to_integer (type, e));
1267 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1268 || TYPE_PTRMEMFUNC_P (type))
1269 return fold (cp_convert_to_pointer (type, e));
1270 if (code == REAL_TYPE)
1272 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1275 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1279 if (flags & LOOKUP_COMPLAIN)
1280 cp_error ("`%#T' used where a floating point value was expected",
1283 return fold (convert_to_real (type, e));
1286 /* New C++ semantics: since assignment is now based on
1287 memberwise copying, if the rhs type is derived from the
1288 lhs type, then we may still do a conversion. */
1289 if (IS_AGGR_TYPE_CODE (code))
1291 tree dtype = TREE_TYPE (e);
1292 tree ctor = NULL_TREE;
1293 tree conversion = NULL_TREE;
1295 dtype = TYPE_MAIN_VARIANT (dtype);
1297 /* Conversion of object pointers or signature pointers/references
1298 to signature pointers/references. */
1300 if (TYPE_LANG_SPECIFIC (type)
1301 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1303 tree constructor = build_signature_pointer_constructor (type, expr);
1304 tree sig_ty = SIGNATURE_TYPE (type);
1307 if (constructor == error_mark_node)
1308 return error_mark_node;
1310 sig_ptr = get_temp_name (type, 1);
1311 DECL_INITIAL (sig_ptr) = constructor;
1312 CLEAR_SIGNATURE (sig_ty);
1313 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1314 SET_SIGNATURE (sig_ty);
1315 TREE_READONLY (sig_ptr) = 1;
1320 /* Conversion between aggregate types. New C++ semantics allow
1321 objects of derived type to be cast to objects of base type.
1322 Old semantics only allowed this between pointers.
1324 There may be some ambiguity between using a constructor
1325 vs. using a type conversion operator when both apply. */
1327 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1328 && TYPE_HAS_CONVERSION (dtype))
1329 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1331 if (conversion == error_mark_node)
1333 if (flags & LOOKUP_COMPLAIN)
1334 error ("ambiguous pointer conversion");
1338 if (TYPE_HAS_CONSTRUCTOR (complete_type (type))
1339 && (! flag_ansi_overloading || ! conversion))
1340 ctor = build_method_call (NULL_TREE, ctor_identifier,
1341 build_tree_list (NULL_TREE, e),
1343 (flags & LOOKUP_NORMAL) | LOOKUP_SPECULATIVELY
1344 | (convtype & CONV_NONCONVERTING ? 0 : LOOKUP_ONLYCONVERTING)
1345 | (flags & LOOKUP_NO_CONVERSION)
1346 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1348 if (ctor == error_mark_node)
1350 if (flags & LOOKUP_COMPLAIN)
1351 cp_error ("in conversion to type `%T'", type);
1352 if (flags & LOOKUP_SPECULATIVELY)
1354 return error_mark_node;
1357 if (conversion && ctor)
1359 if (flags & LOOKUP_COMPLAIN)
1360 error ("both constructor and type conversion operator apply");
1361 if (flags & LOOKUP_SPECULATIVELY)
1363 return error_mark_node;
1365 else if (conversion)
1369 ctor = build_cplus_new (type, ctor);
1374 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1375 then the it won't be hashed and hence compare as not equal,
1377 if (code == ARRAY_TYPE
1378 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1379 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1382 if (flags & LOOKUP_COMPLAIN)
1383 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1384 TREE_TYPE (expr), type);
1385 if (flags & LOOKUP_SPECULATIVELY)
1387 return error_mark_node;
1390 /* Create an expression whose value is that of EXPR,
1391 converted to type TYPE. The TREE_TYPE of the value
1392 is always TYPE. This function implements all reasonable
1393 conversions; callers should filter out those that are
1394 not permitted by the language being compiled. */
1397 convert (type, expr)
1400 return cp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
1403 /* Like convert, except permit conversions to take place which
1404 are not normally allowed due to access restrictions
1405 (such as conversion from sub-type to private super-type). */
1408 convert_force (type, expr, convtype)
1413 register tree e = expr;
1414 register enum tree_code code = TREE_CODE (type);
1416 if (code == REFERENCE_TYPE)
1417 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1419 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1420 e = convert_from_reference (e);
1422 if (code == POINTER_TYPE)
1423 return fold (convert_to_pointer_force (type, e));
1425 /* From typeck.c convert_for_assignment */
1426 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1427 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1428 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1429 || integer_zerop (e)
1430 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1431 && TYPE_PTRMEMFUNC_P (type))
1433 /* compatible pointer to member functions. */
1434 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1437 return cp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1440 /* Subroutine of build_type_conversion. */
1443 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1444 tree xtype, basetype;
1453 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1455 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1457 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1458 if (rval == error_mark_node)
1462 return error_mark_node;
1465 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1469 && TREE_TYPE (xtype)
1470 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1471 > TREE_READONLY (TREE_TYPE (xtype))))
1472 warning ("user-defined conversion casting away `const'");
1473 return convert (xtype, rval);
1476 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1477 exists, return the attempted conversion. This may
1478 return ERROR_MARK_NODE if the conversion is not
1479 allowed (references private members, etc).
1480 If no conversion exists, NULL_TREE is returned.
1482 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1483 to take place immediately. Otherwise, we build a SAVE_EXPR
1484 which can be evaluated if the results are ever needed.
1486 Changes to this functions should be mirrored in user_harshness.
1488 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1489 object parameter, or by the second standard conversion sequence if
1490 that doesn't do it. This will probably wait for an overloading rewrite.
1494 build_type_conversion (code, xtype, expr, for_sure)
1495 enum tree_code code;
1499 /* C++: check to see if we can convert this aggregate type
1500 into the required type. */
1503 tree winner = NULL_TREE;
1505 if (flag_ansi_overloading)
1506 return build_user_type_conversion
1507 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1509 if (expr == error_mark_node)
1510 return error_mark_node;
1512 basetype = TREE_TYPE (expr);
1513 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1514 basetype = TREE_TYPE (basetype);
1516 basetype = TYPE_MAIN_VARIANT (basetype);
1517 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1520 /* Do we have an exact match? */
1522 tree typename = build_typename_overload (xtype);
1523 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1524 return build_type_conversion_1 (xtype, basetype, expr, typename,
1528 /* Nope; try looking for others. */
1529 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1531 tree cand = TREE_VALUE (conv);
1533 if (winner && winner == cand)
1536 if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1542 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1544 cp_error (" candidate conversions include `%D' and `%D'",
1555 return build_type_conversion_1 (xtype, basetype, expr,
1556 DECL_NAME (winner), for_sure);
1561 /* Convert the given EXPR to one of a group of types suitable for use in an
1562 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1563 which indicates which types are suitable. If COMPLAIN is 1, complain
1564 about ambiguity; otherwise, the caller will deal with it. */
1567 build_expr_type_conversion (desires, expr, complain)
1572 tree basetype = TREE_TYPE (expr);
1574 tree winner = NULL_TREE;
1576 if (TREE_CODE (basetype) == OFFSET_TYPE)
1577 expr = resolve_offset_ref (expr);
1578 expr = convert_from_reference (expr);
1579 basetype = TREE_TYPE (expr);
1581 if (! IS_AGGR_TYPE (basetype))
1582 switch (TREE_CODE (basetype))
1585 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1586 && integer_zerop (expr))
1588 /* else fall through... */
1591 return (desires & WANT_INT) ? expr : NULL_TREE;
1593 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1595 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1597 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1601 return (desires & WANT_POINTER) ? default_conversion (expr)
1607 if (! TYPE_HAS_CONVERSION (basetype))
1610 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1614 tree cand = TREE_VALUE (conv);
1616 if (winner && winner == cand)
1619 candidate = TREE_TYPE (TREE_TYPE (cand));
1620 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1621 candidate = TREE_TYPE (candidate);
1623 switch (TREE_CODE (candidate))
1627 win = (desires & WANT_INT); break;
1629 win = (desires & WANT_ENUM); break;
1631 win = (desires & WANT_FLOAT); break;
1633 win = (desires & WANT_POINTER); break;
1642 cp_error ("ambiguous default type conversion from `%T'",
1644 cp_error (" candidate conversions include `%D' and `%D'",
1647 return error_mark_node;
1656 tree type = TREE_TYPE (TREE_TYPE (winner));
1657 if (TREE_CODE (type) == REFERENCE_TYPE)
1658 type = TREE_TYPE (type);
1659 return build_type_conversion_1 (type, basetype, expr,
1660 DECL_NAME (winner), 1);
1666 /* Must convert two aggregate types to non-aggregate type.
1667 Attempts to find a non-ambiguous, "best" type conversion.
1669 Return 1 on success, 0 on failure.
1671 @@ What are the real semantics of this supposed to be??? */
1674 build_default_binary_type_conversion (code, arg1, arg2)
1675 enum tree_code code;
1681 case TRUNC_DIV_EXPR:
1683 case FLOOR_DIV_EXPR:
1684 case ROUND_DIV_EXPR:
1685 case EXACT_DIV_EXPR:
1686 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1687 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1690 case TRUNC_MOD_EXPR:
1691 case FLOOR_MOD_EXPR:
1697 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1698 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1703 tree a1, a2, p1, p2;
1706 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1707 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1708 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1709 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1711 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1714 error ("ambiguous default type conversion for `operator +'");
1717 *arg1 = a1, *arg2 = a2;
1719 *arg1 = a1, *arg2 = p2;
1721 *arg1 = p1, *arg2 = a2;
1727 tree a1, a2, p1, p2;
1730 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1731 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1732 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1733 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1735 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1738 error ("ambiguous default type conversion for `operator -'");
1741 *arg1 = a1, *arg2 = a2;
1743 *arg1 = p1, *arg2 = p2;
1745 *arg1 = p1, *arg2 = a2;
1756 tree a1, a2, p1, p2;
1759 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1760 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1761 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1762 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1764 wins = (a1 && a2) + (p1 && p2);
1767 cp_error ("ambiguous default type conversion for `%O'", code);
1770 *arg1 = a1, *arg2 = a2;
1772 *arg1 = p1, *arg2 = p2;
1776 case TRUTH_ANDIF_EXPR:
1777 case TRUTH_ORIF_EXPR:
1778 *arg1 = convert (boolean_type_node, *arg1);
1779 *arg2 = convert (boolean_type_node, *arg2);
1787 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1788 cp_error ("ambiguous default type conversion for `%O'", code);
1796 /* Implements integral promotion (4.1) and float->double promotion. */
1799 type_promotes_to (type)
1802 int constp, volatilep;
1804 if (type == error_mark_node)
1805 return error_mark_node;
1807 constp = TYPE_READONLY (type);
1808 volatilep = TYPE_VOLATILE (type);
1809 type = TYPE_MAIN_VARIANT (type);
1811 /* bool always promotes to int (not unsigned), even if it's the same
1813 if (type == boolean_type_node)
1814 type = integer_type_node;
1816 /* Normally convert enums to int, but convert wide enums to something
1818 else if (TREE_CODE (type) == ENUMERAL_TYPE
1819 || type == wchar_type_node)
1821 int precision = MAX (TYPE_PRECISION (type),
1822 TYPE_PRECISION (integer_type_node));
1823 tree totype = type_for_size (precision, 0);
1824 if (TREE_UNSIGNED (type)
1825 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1826 type = type_for_size (precision, 1);
1830 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1832 /* Traditionally, unsignedness is preserved in default promotions.
1833 Otherwise, retain unsignedness if really not getting bigger. */
1834 if (TREE_UNSIGNED (type)
1835 && (flag_traditional
1836 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1837 type = unsigned_type_node;
1839 type = integer_type_node;
1841 else if (type == float_type_node)
1842 type = double_type_node;
1844 return cp_build_type_variant (type, constp, volatilep);