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 if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
276 return build_ptrmemfunc (type, expr, 0);
277 expr = build_int_2 (0, 0);
278 TREE_TYPE (expr) = type;
282 if (INTEGRAL_CODE_P (form))
284 if (type_precision (intype) == POINTER_SIZE)
285 return build1 (CONVERT_EXPR, type, expr);
286 expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr);
287 /* Modes may be different but sizes should be the same. */
288 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
289 != GET_MODE_SIZE (TYPE_MODE (type)))
290 /* There is supposed to be some integral type
291 that is the same width as a pointer. */
293 return convert_to_pointer (type, expr);
296 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
298 return error_mark_node;
301 /* Like convert, except permit conversions to take place which
302 are not normally allowed due to access restrictions
303 (such as conversion from sub-type to private super-type). */
306 convert_to_pointer_force (type, expr)
309 register tree intype = TREE_TYPE (expr);
310 register enum tree_code form = TREE_CODE (intype);
312 if (integer_zerop (expr))
314 expr = build_int_2 (0, 0);
315 TREE_TYPE (expr) = type;
319 /* Convert signature pointer/reference to `void *' first. */
320 if (form == RECORD_TYPE
321 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
323 expr = build_optr_ref (expr);
324 intype = TREE_TYPE (expr);
325 form = TREE_CODE (intype);
328 if (form == POINTER_TYPE)
330 intype = TYPE_MAIN_VARIANT (intype);
332 if (TYPE_MAIN_VARIANT (type) != intype
333 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
334 && IS_AGGR_TYPE (TREE_TYPE (type))
335 && IS_AGGR_TYPE (TREE_TYPE (intype))
336 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
338 enum tree_code code = PLUS_EXPR;
340 int distance = get_base_distance (TREE_TYPE (type),
341 TREE_TYPE (intype), 0, &path);
345 cp_error ("type `%T' is ambiguous baseclass of `%s'",
347 TYPE_NAME_STRING (TREE_TYPE (intype)));
348 return error_mark_node;
352 distance = get_base_distance (TREE_TYPE (intype),
353 TREE_TYPE (type), 0, &path);
357 /* Doesn't need any special help from us. */
358 return build1 (NOP_EXPR, type, expr);
362 return build_vbase_path (code, type, expr, path, 0);
366 return cp_convert_to_pointer (type, expr);
369 /* We are passing something to a function which requires a reference.
370 The type we are interested in is in TYPE. The initial
371 value we have to begin with is in ARG.
373 FLAGS controls how we manage access checking.
374 DIRECT_BIND in FLAGS controls how any temporaries are generated. */
377 build_up_reference (type, arg, flags, checkconst)
379 int flags, checkconst;
382 tree argtype = TREE_TYPE (arg);
383 tree target_type = TREE_TYPE (type);
385 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
387 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
390 if (toplevel_bindings_p ())
391 arg = get_temp_name (argtype, 1);
394 arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
395 DECL_ARTIFICIAL (arg) = 1;
397 DECL_INITIAL (arg) = targ;
398 cp_finish_decl (arg, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
400 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
402 tree slot = build_decl (VAR_DECL, NULL_TREE, argtype);
403 arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
406 /* If we had a way to wrap this up, and say, if we ever needed it's
407 address, transform all occurrences of the register, into a memory
408 reference we could win better. */
409 rval = build_unary_op (ADDR_EXPR, arg, 1);
410 if (flags & LOOKUP_PROTECT)
411 rval = convert_pointer_to (target_type, rval);
414 = convert_to_pointer_force (build_pointer_type (target_type), rval);
415 rval = build1 (CONVERT_EXPR, type, rval);
416 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
420 /* For C++: Only need to do one-level references, but cannot
421 get tripped up on signed/unsigned differences.
423 DECL is either NULL_TREE or the _DECL node for a reference that is being
424 initialized. It can be error_mark_node if we don't know the _DECL but
425 we know it's an initialization. */
428 convert_to_reference (reftype, expr, convtype, flags, decl)
433 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
434 register tree intype = TREE_TYPE (expr);
435 tree rval = NULL_TREE;
436 tree rval_as_conversion = NULL_TREE;
439 if (TREE_CODE (intype) == REFERENCE_TYPE)
440 my_friendly_abort (364);
442 intype = TYPE_MAIN_VARIANT (intype);
444 i = comp_target_types (type, intype, 0);
446 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
447 && ! (flags & LOOKUP_NO_CONVERSION))
449 /* Look for a user-defined conversion to lvalue that we can use. */
451 if (flag_ansi_overloading)
453 = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
455 rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
457 if (rval_as_conversion && rval_as_conversion != error_mark_node
458 && real_lvalue_p (rval_as_conversion))
460 expr = rval_as_conversion;
461 rval_as_conversion = NULL_TREE;
467 if (((convtype & CONV_STATIC) && i == -1)
468 || ((convtype & CONV_IMPLICIT) && i == 1))
470 if (flags & LOOKUP_COMPLAIN)
472 tree ttl = TREE_TYPE (reftype);
476 int r = TREE_READONLY (expr);
477 int v = TREE_THIS_VOLATILE (expr);
478 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
481 if (! real_lvalue_p (expr) && ! TYPE_READONLY (ttl))
484 /* Ensure semantics of [dcl.init.ref] */
485 cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'",
488 cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'",
491 else if (! (convtype & CONV_CONST))
493 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
494 cp_pedwarn ("conversion from `%T' to `%T' discards const",
496 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
497 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
502 return build_up_reference (reftype, expr, flags,
503 ! (convtype & CONV_CONST));
505 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
507 /* When casting an lvalue to a reference type, just convert into
508 a pointer to the new type and deference it. This is allowed
509 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
510 should be done directly (jason). (int &)ri ---> *(int*)&ri */
512 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
514 if (TREE_CODE (intype) == POINTER_TYPE
515 && (comptypes (TREE_TYPE (intype), type, -1)))
516 cp_warning ("casting `%T' to `%T' does not dereference pointer",
519 rval = build_unary_op (ADDR_EXPR, expr, 0);
520 if (rval != error_mark_node)
521 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
522 if (rval != error_mark_node)
523 rval = build1 (NOP_EXPR, reftype, rval);
525 else if (flag_ansi_overloading)
527 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
529 if (rval == error_mark_node)
530 return error_mark_node;
531 rval = build_up_reference (reftype, rval, flags, 1);
533 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
534 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
539 tree rval_as_ctor = NULL_TREE;
541 if (rval_as_conversion)
543 if (rval_as_conversion == error_mark_node)
545 cp_error ("conversion from `%T' to `%T' is ambiguous",
547 return error_mark_node;
549 rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
553 /* Definitely need to go through a constructor here. */
554 if (TYPE_HAS_CONSTRUCTOR (type)
555 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
556 && (rval = build_method_call
557 (NULL_TREE, ctor_identifier,
558 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
559 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
560 | LOOKUP_ONLYCONVERTING)))
564 if (toplevel_bindings_p ())
566 tree t = get_temp_name (type, toplevel_bindings_p ());
567 init = build_method_call (t, ctor_identifier,
568 build_tree_list (NULL_TREE, expr),
570 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
571 | LOOKUP_ONLYCONVERTING);
573 if (init == error_mark_node)
574 return error_mark_node;
576 make_decl_rtl (t, NULL_PTR, 1);
577 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
578 rval = build_unary_op (ADDR_EXPR, t, 0);
582 init = build_method_call (NULL_TREE, ctor_identifier,
583 build_tree_list (NULL_TREE, expr),
585 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
586 |LOOKUP_ONLYCONVERTING);
588 if (init == error_mark_node)
589 return error_mark_node;
591 rval = build_cplus_new (type, init);
592 rval = build_up_reference (reftype, rval, flags, 1);
597 if (rval_as_ctor && rval_as_conversion)
599 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
601 return error_mark_node;
603 else if (rval_as_ctor)
605 else if (rval_as_conversion)
606 rval = rval_as_conversion;
607 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
609 rval = cp_convert (type, expr);
610 if (rval == error_mark_node)
611 return error_mark_node;
613 rval = build_up_reference (reftype, rval, flags, 1);
616 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
617 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
623 /* If we found a way to convert earlier, then use it. */
627 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
629 if (flags & LOOKUP_COMPLAIN)
630 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
632 if (flags & LOOKUP_SPECULATIVELY)
635 return error_mark_node;
638 /* We are using a reference VAL for its value. Bash that reference all the
639 way down to its lowest form. */
642 convert_from_reference (val)
645 tree type = TREE_TYPE (val);
647 if (TREE_CODE (type) == OFFSET_TYPE)
648 type = TREE_TYPE (type);
649 if (TREE_CODE (type) == REFERENCE_TYPE)
650 return build_indirect_ref (val, NULL_PTR);
654 /* See if there is a constructor of type TYPE which will convert
655 EXPR. The reference manual seems to suggest (8.5.6) that we need
656 not worry about finding constructors for base classes, then converting
657 to the derived class.
659 MSGP is a pointer to a message that would be an appropriate error
660 string. If MSGP is NULL, then we are not interested in reporting
664 convert_to_aggr (type, expr, msgp, protect)
669 tree basetype = type;
670 tree name = TYPE_IDENTIFIER (basetype);
671 tree function, fndecl, fntype, parmtypes, parmlist, result;
673 /* See code below that used this. */
677 int can_be_private, can_be_protected;
679 if (! TYPE_HAS_CONSTRUCTOR (basetype))
682 *msgp = "type `%s' does not have a constructor";
683 return error_mark_node;
686 access = access_public_node;
688 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
690 parmlist = build_tree_list (NULL_TREE, expr);
691 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
693 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
695 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
696 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
699 /* The type of the first argument will be filled in inside the loop. */
700 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
701 parmtypes = tree_cons (NULL_TREE, build_pointer_type (basetype), parmtypes);
703 /* No exact conversion was found. See if an approximate
705 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
709 int saw_protected = 0;
710 struct candidate *candidates
711 = (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
712 struct candidate *cp = candidates;
718 cp->harshness = (struct harshness_code *)
719 alloca (3 * sizeof (struct harshness_code));
721 compute_conversion_costs (fndecl, parmlist, cp, 2);
722 if ((cp->h.code & EVIL_CODE) == 0)
724 cp->u.field = fndecl;
727 if (TREE_PRIVATE (fndecl))
728 access = access_private_node;
729 else if (TREE_PROTECTED (fndecl))
730 access = access_protected_node;
732 access = access_public_node;
735 access = access_public_node;
737 if (access == access_private_node
738 ? (basetype == current_class_type
739 || is_friend (basetype, cp->function)
740 || purpose_member (basetype, DECL_ACCESS (fndecl)))
741 : access == access_protected_node
743 || purpose_member (basetype, DECL_ACCESS (fndecl)))
746 if (cp->h.code <= TRIVIAL_CODE)
752 if (access == access_private_node)
758 fndecl = DECL_CHAIN (fndecl);
762 /* Rank from worst to best. Then cp will point to best one.
763 Private fields have their bits flipped. For unsigned
764 numbers, this should make them look very large.
765 If the best alternate has a (signed) negative value,
766 then all we ever saw were private members. */
767 if (cp - candidates > 1)
768 qsort (candidates, /* char *base */
769 cp - candidates, /* int nel */
770 sizeof (struct candidate), /* int width */
771 rank_for_overload); /* int (*compar)() */
774 if (cp->h.code & EVIL_CODE)
777 *msgp = "ambiguous type conversion possible for `%s'";
778 return error_mark_node;
781 function = cp->function;
782 fndecl = cp->u.field;
789 *msgp = "only private and protected conversions apply";
791 *msgp = "only private conversions apply";
792 else if (saw_protected)
793 *msgp = "only protected conversions apply";
795 *msgp = "no appropriate conversion to type `%s'";
797 return error_mark_node;
802 if (access == access_private_node)
803 if (! can_be_private)
806 *msgp = TREE_PRIVATE (fndecl)
807 ? "conversion to type `%s' is private"
808 : "conversion to type `%s' is from private base class";
809 return error_mark_node;
811 if (access == access_protected_node)
812 if (! can_be_protected)
815 *msgp = TREE_PRIVATE (fndecl)
816 ? "conversion to type `%s' is protected"
817 : "conversion to type `%s' is from protected base class";
818 return error_mark_node;
823 /* It will convert, but we don't do anything about it yet. */
827 fntype = TREE_TYPE (function);
829 parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
830 parmlist, NULL_TREE, LOOKUP_NORMAL);
832 result = build_call (function, TREE_TYPE (fntype), parmlist);
836 /* Call this when we know (for any reason) that expr is not, in fact,
837 zero. This routine is like convert_pointer_to, but it pays
838 attention to which specific instance of what type we want to
839 convert to. This routine should eventually become
840 convert_to_pointer after all references to convert_to_pointer
844 convert_pointer_to_real (binfo, expr)
847 register tree intype = TREE_TYPE (expr);
851 if (TREE_CODE (binfo) == TREE_VEC)
852 type = BINFO_TYPE (binfo);
853 else if (IS_AGGR_TYPE (binfo))
863 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
864 TYPE_VOLATILE (TREE_TYPE (intype)));
865 ptr_type = build_pointer_type (ptr_type);
866 if (ptr_type == TYPE_MAIN_VARIANT (intype))
869 if (intype == error_mark_node)
870 return error_mark_node;
872 my_friendly_assert (!integer_zerop (expr), 191);
874 if (TREE_CODE (type) == RECORD_TYPE
875 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
876 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
880 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
883 /* This function shouldn't be called with unqualified arguments
884 but if it is, give them an error message that they can read. */
887 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
888 TREE_TYPE (intype), type);
891 cp_error ("because `%T' is an ambiguous base class", type);
892 return error_mark_node;
895 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
897 rval = build1 (NOP_EXPR, ptr_type,
898 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
899 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
903 /* Call this when we know (for any reason) that expr is
904 not, in fact, zero. This routine gets a type out of the first
905 argument and uses it to search for the type to convert to. If there
906 is more than one instance of that type in the expr, the conversion is
907 ambiguous. This routine should eventually go away, and all
908 callers should use convert_to_pointer_real. */
911 convert_pointer_to (binfo, expr)
916 if (TREE_CODE (binfo) == TREE_VEC)
917 type = BINFO_TYPE (binfo);
918 else if (IS_AGGR_TYPE (binfo))
922 return convert_pointer_to_real (type, expr);
925 /* C++ conversions, preference to static cast conversions. */
928 cp_convert (type, expr)
931 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
936 FLAGS indicates how we should behave. */
939 ocp_convert (type, expr, convtype, flags)
943 register tree e = expr;
944 register enum tree_code code = TREE_CODE (type);
946 if (e == error_mark_node
947 || TREE_TYPE (e) == error_mark_node)
948 return error_mark_node;
950 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
951 /* We need a new temporary; don't take this shortcut. */;
952 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
953 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
954 return fold (build1 (NOP_EXPR, type, e));
956 if (code == VOID_TYPE && (convtype & CONV_STATIC))
957 return build1 (CONVERT_EXPR, type, e);
960 /* This is incorrect. A truncation can't be stripped this way.
961 Extensions will be stripped by the use of get_unwidened. */
962 if (TREE_CODE (e) == NOP_EXPR)
963 return cp_convert (type, TREE_OPERAND (e, 0));
966 /* Just convert to the type of the member. */
967 if (code == OFFSET_TYPE)
969 type = TREE_TYPE (type);
970 code = TREE_CODE (type);
974 if (code == REFERENCE_TYPE)
975 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
976 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
977 e = convert_from_reference (e);
980 if (TREE_CODE (e) == OFFSET_REF)
981 e = resolve_offset_ref (e);
983 if (TREE_READONLY_DECL_P (e))
984 e = decl_constant_value (e);
986 if (INTEGRAL_CODE_P (code))
988 tree intype = TREE_TYPE (e);
989 /* enum = enum, enum = int, enum = float, (enum)pointer are all
991 if (flag_int_enum_equivalence == 0
992 && TREE_CODE (type) == ENUMERAL_TYPE
993 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
994 || (TREE_CODE (intype) == POINTER_TYPE)))
996 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
998 if (flag_pedantic_errors)
999 return error_mark_node;
1001 if (IS_AGGR_TYPE (intype))
1004 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1007 if (flags & LOOKUP_COMPLAIN)
1008 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1009 if (flags & LOOKUP_SPECULATIVELY)
1011 return error_mark_node;
1013 if (code == BOOLEAN_TYPE)
1015 /* Common Ada/Pascal programmer's mistake. We always warn
1016 about this since it is so bad. */
1017 if (TREE_CODE (expr) == FUNCTION_DECL)
1018 cp_warning ("the address of `%D', will always be `true'", expr);
1019 return truthvalue_conversion (e);
1021 return fold (convert_to_integer (type, e));
1023 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1024 || TYPE_PTRMEMFUNC_P (type))
1025 return fold (cp_convert_to_pointer (type, e));
1026 if (code == REAL_TYPE || code == COMPLEX_TYPE)
1028 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1031 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1035 if (flags & LOOKUP_COMPLAIN)
1036 cp_error ("`%#T' used where a floating point value was expected",
1039 if (code == REAL_TYPE)
1040 return fold (convert_to_real (type, e));
1041 else if (code == COMPLEX_TYPE)
1042 return fold (convert_to_complex (type, e));
1045 /* New C++ semantics: since assignment is now based on
1046 memberwise copying, if the rhs type is derived from the
1047 lhs type, then we may still do a conversion. */
1048 if (IS_AGGR_TYPE_CODE (code))
1050 tree dtype = TREE_TYPE (e);
1051 tree ctor = NULL_TREE;
1052 tree conversion = NULL_TREE;
1054 dtype = TYPE_MAIN_VARIANT (dtype);
1056 /* Conversion of object pointers or signature pointers/references
1057 to signature pointers/references. */
1059 if (TYPE_LANG_SPECIFIC (type)
1060 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1062 tree constructor = build_signature_pointer_constructor (type, expr);
1063 tree sig_ty = SIGNATURE_TYPE (type);
1066 if (constructor == error_mark_node)
1067 return error_mark_node;
1069 sig_ptr = get_temp_name (type, 1);
1070 DECL_INITIAL (sig_ptr) = constructor;
1071 CLEAR_SIGNATURE (sig_ty);
1072 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1073 SET_SIGNATURE (sig_ty);
1074 TREE_READONLY (sig_ptr) = 1;
1079 /* Conversion between aggregate types. New C++ semantics allow
1080 objects of derived type to be cast to objects of base type.
1081 Old semantics only allowed this between pointers.
1083 There may be some ambiguity between using a constructor
1084 vs. using a type conversion operator when both apply. */
1086 if (flag_ansi_overloading)
1090 if ((flags & LOOKUP_ONLYCONVERTING)
1091 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
1092 ctor = build_user_type_conversion (type, ctor, flags);
1094 ctor = build_method_call (NULL_TREE, ctor_identifier,
1095 build_tree_list (NULL_TREE, ctor),
1096 TYPE_BINFO (type), flags);
1098 return build_cplus_new (type, ctor);
1102 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1103 && TYPE_HAS_CONVERSION (dtype))
1104 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1106 if (conversion == error_mark_node)
1108 if (flags & LOOKUP_COMPLAIN)
1109 error ("ambiguous pointer conversion");
1113 if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1114 ctor = build_method_call (NULL_TREE, ctor_identifier,
1115 build_tree_list (NULL_TREE, e),
1117 (flags & LOOKUP_NORMAL)
1118 | LOOKUP_SPECULATIVELY
1119 | (flags & LOOKUP_ONLYCONVERTING)
1120 | (flags & LOOKUP_NO_CONVERSION)
1121 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1123 if (ctor == error_mark_node)
1125 if (flags & LOOKUP_COMPLAIN)
1126 cp_error ("in conversion to type `%T'", type);
1127 if (flags & LOOKUP_SPECULATIVELY)
1129 return error_mark_node;
1132 if (conversion && ctor)
1134 if (flags & LOOKUP_COMPLAIN)
1135 error ("both constructor and type conversion operator apply");
1136 if (flags & LOOKUP_SPECULATIVELY)
1138 return error_mark_node;
1140 else if (conversion)
1144 ctor = build_cplus_new (type, ctor);
1150 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1151 then the it won't be hashed and hence compare as not equal,
1153 if (code == ARRAY_TYPE
1154 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1155 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1158 if (flags & LOOKUP_COMPLAIN)
1159 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1160 TREE_TYPE (expr), type);
1161 if (flags & LOOKUP_SPECULATIVELY)
1163 return error_mark_node;
1166 /* Create an expression whose value is that of EXPR,
1167 converted to type TYPE. The TREE_TYPE of the value
1168 is always TYPE. This function implements all reasonable
1169 conversions; callers should filter out those that are
1170 not permitted by the language being compiled.
1172 Most of this routine is from build_reinterpret_cast.
1174 The backend cannot call cp_convert (what was convert) because
1175 conversions to/from basetypes may involve memory references
1176 (vbases) and adding or subtracting small values (multiple
1177 inheritance), but it calls convert from the constant folding code
1178 on subtrees of already build trees after it has ripped them apart.
1180 Also, if we ever support range variables, we'll probably also have to
1181 do a little bit more work. */
1184 convert (type, expr)
1189 if (type == error_mark_node || expr == error_mark_node)
1190 return error_mark_node;
1192 if (TREE_TYPE (expr) == type)
1195 if (TREE_CODE (type) != REFERENCE_TYPE)
1197 expr = decay_conversion (expr);
1199 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1200 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
1201 if (TREE_CODE (expr) == NOP_EXPR
1202 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
1203 expr = TREE_OPERAND (expr, 0);
1206 intype = TREE_TYPE (expr);
1208 if (TREE_CODE (type) == REFERENCE_TYPE)
1210 expr = build_unary_op (ADDR_EXPR, expr, 0);
1211 if (expr != error_mark_node)
1212 expr = convert (build_pointer_type (TREE_TYPE (type)), expr);
1213 if (expr != error_mark_node)
1214 expr = build_indirect_ref (expr, 0);
1217 else if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
1218 return build_static_cast (type, expr);
1220 if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
1221 || TREE_CODE (intype) == ENUMERAL_TYPE))
1223 else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
1226 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
1227 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
1229 if (TREE_READONLY_DECL_P (expr))
1230 expr = decl_constant_value (expr);
1231 return fold (build1 (NOP_EXPR, type, expr));
1233 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
1234 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
1236 if (TREE_READONLY_DECL_P (expr))
1237 expr = decl_constant_value (expr);
1238 return fold (build1 (NOP_EXPR, type, expr));
1241 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1242 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1245 /* Like cp_convert, except permit conversions to take place which
1246 are not normally allowed due to access restrictions
1247 (such as conversion from sub-type to private super-type). */
1250 convert_force (type, expr, convtype)
1255 register tree e = expr;
1256 register enum tree_code code = TREE_CODE (type);
1258 if (code == REFERENCE_TYPE)
1259 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1261 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1262 e = convert_from_reference (e);
1264 if (code == POINTER_TYPE)
1265 return fold (convert_to_pointer_force (type, e));
1267 /* From typeck.c convert_for_assignment */
1268 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1269 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1270 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1271 || integer_zerop (e)
1272 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1273 && TYPE_PTRMEMFUNC_P (type))
1275 /* compatible pointer to member functions. */
1276 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1279 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1282 /* Subroutine of build_type_conversion. */
1285 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1286 tree xtype, basetype;
1295 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1297 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1299 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1300 if (rval == error_mark_node)
1304 return error_mark_node;
1307 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1311 && TREE_TYPE (xtype)
1312 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1313 > TREE_READONLY (TREE_TYPE (xtype))))
1314 warning ("user-defined conversion casting away `const'");
1315 return cp_convert (xtype, rval);
1318 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1319 exists, return the attempted conversion. This may
1320 return ERROR_MARK_NODE if the conversion is not
1321 allowed (references private members, etc).
1322 If no conversion exists, NULL_TREE is returned.
1324 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1325 to take place immediately. Otherwise, we build a SAVE_EXPR
1326 which can be evaluated if the results are ever needed.
1328 Changes to this functions should be mirrored in user_harshness.
1330 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1331 object parameter, or by the second standard conversion sequence if
1332 that doesn't do it. This will probably wait for an overloading rewrite.
1336 build_type_conversion (code, xtype, expr, for_sure)
1337 enum tree_code code;
1341 /* C++: check to see if we can convert this aggregate type
1342 into the required type. */
1345 tree winner = NULL_TREE;
1347 if (flag_ansi_overloading)
1348 return build_user_type_conversion
1349 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1351 if (expr == error_mark_node)
1352 return error_mark_node;
1354 basetype = TREE_TYPE (expr);
1355 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1356 basetype = TREE_TYPE (basetype);
1358 basetype = TYPE_MAIN_VARIANT (basetype);
1359 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1362 /* Do we have an exact match? */
1364 tree typename = build_typename_overload (xtype);
1365 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1366 return build_type_conversion_1 (xtype, basetype, expr, typename,
1370 /* Nope; try looking for others. */
1371 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1373 tree cand = TREE_VALUE (conv);
1375 if (winner && winner == cand)
1378 if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1384 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1386 cp_error (" candidate conversions include `%D' and `%D'",
1397 return build_type_conversion_1 (xtype, basetype, expr,
1398 DECL_NAME (winner), for_sure);
1403 /* Convert the given EXPR to one of a group of types suitable for use in an
1404 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1405 which indicates which types are suitable. If COMPLAIN is 1, complain
1406 about ambiguity; otherwise, the caller will deal with it. */
1409 build_expr_type_conversion (desires, expr, complain)
1414 tree basetype = TREE_TYPE (expr);
1416 tree winner = NULL_TREE;
1418 if (TREE_CODE (basetype) == OFFSET_TYPE)
1419 expr = resolve_offset_ref (expr);
1420 expr = convert_from_reference (expr);
1421 basetype = TREE_TYPE (expr);
1423 if (! IS_AGGR_TYPE (basetype))
1424 switch (TREE_CODE (basetype))
1427 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1428 && integer_zerop (expr))
1430 /* else fall through... */
1433 return (desires & WANT_INT) ? expr : NULL_TREE;
1435 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1437 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1439 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1443 return (desires & WANT_POINTER) ? default_conversion (expr)
1449 if (! TYPE_HAS_CONVERSION (basetype))
1452 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1456 tree cand = TREE_VALUE (conv);
1458 if (winner && winner == cand)
1461 candidate = TREE_TYPE (TREE_TYPE (cand));
1462 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1463 candidate = TREE_TYPE (candidate);
1465 switch (TREE_CODE (candidate))
1469 win = (desires & WANT_INT); break;
1471 win = (desires & WANT_ENUM); break;
1473 win = (desires & WANT_FLOAT); break;
1475 win = (desires & WANT_POINTER); break;
1484 cp_error ("ambiguous default type conversion from `%T'",
1486 cp_error (" candidate conversions include `%D' and `%D'",
1489 return error_mark_node;
1498 tree type = TREE_TYPE (TREE_TYPE (winner));
1499 if (TREE_CODE (type) == REFERENCE_TYPE)
1500 type = TREE_TYPE (type);
1501 return build_type_conversion_1 (type, basetype, expr,
1502 DECL_NAME (winner), 1);
1508 /* Must convert two aggregate types to non-aggregate type.
1509 Attempts to find a non-ambiguous, "best" type conversion.
1511 Return 1 on success, 0 on failure.
1513 @@ What are the real semantics of this supposed to be??? */
1516 build_default_binary_type_conversion (code, arg1, arg2)
1517 enum tree_code code;
1523 case TRUNC_DIV_EXPR:
1525 case FLOOR_DIV_EXPR:
1526 case ROUND_DIV_EXPR:
1527 case EXACT_DIV_EXPR:
1528 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1529 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1532 case TRUNC_MOD_EXPR:
1533 case FLOOR_MOD_EXPR:
1539 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1540 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1545 tree a1, a2, p1, p2;
1548 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1549 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1550 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1551 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1553 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1556 error ("ambiguous default type conversion for `operator +'");
1559 *arg1 = a1, *arg2 = a2;
1561 *arg1 = a1, *arg2 = p2;
1563 *arg1 = p1, *arg2 = a2;
1569 tree a1, a2, p1, p2;
1572 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1573 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1574 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1575 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1577 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1580 error ("ambiguous default type conversion for `operator -'");
1583 *arg1 = a1, *arg2 = a2;
1585 *arg1 = p1, *arg2 = p2;
1587 *arg1 = p1, *arg2 = a2;
1598 tree a1, a2, p1, p2;
1601 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1602 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1603 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1604 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1606 wins = (a1 && a2) + (p1 && p2);
1609 cp_error ("ambiguous default type conversion for `%O'", code);
1612 *arg1 = a1, *arg2 = a2;
1614 *arg1 = p1, *arg2 = p2;
1618 case TRUTH_ANDIF_EXPR:
1619 case TRUTH_ORIF_EXPR:
1620 *arg1 = cp_convert (boolean_type_node, *arg1);
1621 *arg2 = cp_convert (boolean_type_node, *arg2);
1629 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1630 cp_error ("ambiguous default type conversion for `%O'", code);
1638 /* Implements integral promotion (4.1) and float->double promotion. */
1641 type_promotes_to (type)
1644 int constp, volatilep;
1646 if (type == error_mark_node)
1647 return error_mark_node;
1649 constp = TYPE_READONLY (type);
1650 volatilep = TYPE_VOLATILE (type);
1651 type = TYPE_MAIN_VARIANT (type);
1653 /* bool always promotes to int (not unsigned), even if it's the same
1655 if (type == boolean_type_node)
1656 type = integer_type_node;
1658 /* Normally convert enums to int, but convert wide enums to something
1660 else if (TREE_CODE (type) == ENUMERAL_TYPE
1661 || type == wchar_type_node)
1663 int precision = MAX (TYPE_PRECISION (type),
1664 TYPE_PRECISION (integer_type_node));
1665 tree totype = type_for_size (precision, 0);
1666 if (TREE_UNSIGNED (type)
1667 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1668 type = type_for_size (precision, 1);
1672 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1674 /* Retain unsignedness if really not getting bigger. */
1675 if (TREE_UNSIGNED (type)
1676 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1677 type = unsigned_type_node;
1679 type = integer_type_node;
1681 else if (type == float_type_node)
1682 type = double_type_node;
1684 return cp_build_type_variant (type, constp, volatilep);