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;
823 *msgp = "only private and protected conversions apply";
825 *msgp = "only private conversions apply";
827 else if (saw_protected)
828 *msgp = "only protected conversions apply";
830 *msgp = "no appropriate conversion to type `%s'";
832 return error_mark_node;
837 if (access == access_private_node)
838 if (! can_be_private)
841 *msgp = TREE_PRIVATE (fndecl)
842 ? "conversion to type `%s' is private"
843 : "conversion to type `%s' is from private base class";
844 return error_mark_node;
846 if (access == access_protected_node)
847 if (! can_be_protected)
850 *msgp = TREE_PRIVATE (fndecl)
851 ? "conversion to type `%s' is protected"
852 : "conversion to type `%s' is from protected base class";
853 return error_mark_node;
858 /* It will convert, but we don't do anything about it yet. */
862 fntype = TREE_TYPE (function);
864 parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
865 parmlist, NULL_TREE, LOOKUP_NORMAL);
867 result = build_call (function, TREE_TYPE (fntype), parmlist);
871 /* Call this when we know (for any reason) that expr is not, in fact,
872 zero. This routine is like convert_pointer_to, but it pays
873 attention to which specific instance of what type we want to
874 convert to. This routine should eventually become
875 convert_to_pointer after all references to convert_to_pointer
879 convert_pointer_to_real (binfo, expr)
882 register tree intype = TREE_TYPE (expr);
886 if (TREE_CODE (binfo) == TREE_VEC)
887 type = BINFO_TYPE (binfo);
888 else if (IS_AGGR_TYPE (binfo))
898 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
899 TYPE_VOLATILE (TREE_TYPE (intype)));
900 ptr_type = build_pointer_type (ptr_type);
901 if (ptr_type == TYPE_MAIN_VARIANT (intype))
904 if (intype == error_mark_node)
905 return error_mark_node;
907 my_friendly_assert (!integer_zerop (expr), 191);
909 if (TREE_CODE (type) == RECORD_TYPE
910 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
911 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
915 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
918 /* This function shouldn't be called with unqualified arguments
919 but if it is, give them an error message that they can read. */
922 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
923 TREE_TYPE (intype), type);
926 cp_error ("because `%T' is an ambiguous base class", type);
927 return error_mark_node;
930 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
932 rval = build1 (NOP_EXPR, ptr_type,
933 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
934 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
938 /* Call this when we know (for any reason) that expr is
939 not, in fact, zero. This routine gets a type out of the first
940 argument and uses it to search for the type to convert to. If there
941 is more than one instance of that type in the expr, the conversion is
942 ambiguous. This routine should eventually go away, and all
943 callers should use convert_to_pointer_real. */
946 convert_pointer_to (binfo, expr)
951 if (TREE_CODE (binfo) == TREE_VEC)
952 type = BINFO_TYPE (binfo);
953 else if (IS_AGGR_TYPE (binfo))
957 return convert_pointer_to_real (type, expr);
960 /* C++ conversions, preference to static cast conversions. */
963 cp_convert (type, expr)
966 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
971 FLAGS indicates how we should behave. */
974 ocp_convert (type, expr, convtype, flags)
978 register tree e = expr;
979 register enum tree_code code = TREE_CODE (type);
981 if (e == error_mark_node
982 || TREE_TYPE (e) == error_mark_node)
983 return error_mark_node;
985 if (TREE_READONLY_DECL_P (e))
986 e = decl_constant_value (e);
988 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
989 /* We need a new temporary; don't take this shortcut. */;
990 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
991 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
992 return fold (build1 (NOP_EXPR, type, e));
994 if (code == VOID_TYPE && (convtype & CONV_STATIC))
995 return build1 (CONVERT_EXPR, type, e);
998 /* This is incorrect. A truncation can't be stripped this way.
999 Extensions will be stripped by the use of get_unwidened. */
1000 if (TREE_CODE (e) == NOP_EXPR)
1001 return cp_convert (type, TREE_OPERAND (e, 0));
1004 /* Just convert to the type of the member. */
1005 if (code == OFFSET_TYPE)
1007 type = TREE_TYPE (type);
1008 code = TREE_CODE (type);
1012 if (code == REFERENCE_TYPE)
1013 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1014 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1015 e = convert_from_reference (e);
1018 if (TREE_CODE (e) == OFFSET_REF)
1019 e = resolve_offset_ref (e);
1021 if (INTEGRAL_CODE_P (code))
1023 tree intype = TREE_TYPE (e);
1024 /* enum = enum, enum = int, enum = float, (enum)pointer are all
1026 if (flag_int_enum_equivalence == 0
1027 && TREE_CODE (type) == ENUMERAL_TYPE
1028 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
1029 || (TREE_CODE (intype) == POINTER_TYPE)))
1031 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1033 if (flag_pedantic_errors)
1034 return error_mark_node;
1036 if (IS_AGGR_TYPE (intype))
1039 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1042 if (flags & LOOKUP_COMPLAIN)
1043 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1044 if (flags & LOOKUP_SPECULATIVELY)
1046 return error_mark_node;
1048 if (code == BOOLEAN_TYPE)
1050 /* Common Ada/Pascal programmer's mistake. We always warn
1051 about this since it is so bad. */
1052 if (TREE_CODE (expr) == FUNCTION_DECL)
1053 cp_warning ("the address of `%D', will always be `true'", expr);
1054 return truthvalue_conversion (e);
1056 return fold (convert_to_integer (type, e));
1058 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1059 || TYPE_PTRMEMFUNC_P (type))
1060 return fold (cp_convert_to_pointer (type, e));
1061 if (code == REAL_TYPE || code == COMPLEX_TYPE)
1063 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1066 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1070 if (flags & LOOKUP_COMPLAIN)
1071 cp_error ("`%#T' used where a floating point value was expected",
1074 if (code == REAL_TYPE)
1075 return fold (convert_to_real (type, e));
1076 else if (code == COMPLEX_TYPE)
1077 return fold (convert_to_complex (type, e));
1080 /* New C++ semantics: since assignment is now based on
1081 memberwise copying, if the rhs type is derived from the
1082 lhs type, then we may still do a conversion. */
1083 if (IS_AGGR_TYPE_CODE (code))
1085 tree dtype = TREE_TYPE (e);
1086 tree ctor = NULL_TREE;
1087 tree conversion = NULL_TREE;
1089 dtype = TYPE_MAIN_VARIANT (dtype);
1091 /* Conversion of object pointers or signature pointers/references
1092 to signature pointers/references. */
1094 if (TYPE_LANG_SPECIFIC (type)
1095 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1097 tree constructor = build_signature_pointer_constructor (type, expr);
1098 tree sig_ty = SIGNATURE_TYPE (type);
1101 if (constructor == error_mark_node)
1102 return error_mark_node;
1104 sig_ptr = get_temp_name (type, 1);
1105 DECL_INITIAL (sig_ptr) = constructor;
1106 CLEAR_SIGNATURE (sig_ty);
1107 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1108 SET_SIGNATURE (sig_ty);
1109 TREE_READONLY (sig_ptr) = 1;
1114 /* Conversion between aggregate types. New C++ semantics allow
1115 objects of derived type to be cast to objects of base type.
1116 Old semantics only allowed this between pointers.
1118 There may be some ambiguity between using a constructor
1119 vs. using a type conversion operator when both apply. */
1121 if (flag_ansi_overloading)
1125 if ((flags & LOOKUP_ONLYCONVERTING)
1126 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
1128 ctor = build_user_type_conversion (type, ctor, flags);
1129 flags |= LOOKUP_NO_CONVERSION;
1132 ctor = build_method_call (NULL_TREE, ctor_identifier,
1133 build_expr_list (NULL_TREE, ctor),
1134 TYPE_BINFO (type), flags);
1136 return build_cplus_new (type, ctor);
1140 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1141 && TYPE_HAS_CONVERSION (dtype))
1142 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1144 if (conversion == error_mark_node)
1146 if (flags & LOOKUP_COMPLAIN)
1147 error ("ambiguous pointer conversion");
1151 if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1152 ctor = build_method_call (NULL_TREE, ctor_identifier,
1153 build_expr_list (NULL_TREE, e),
1155 (flags & LOOKUP_NORMAL)
1156 | LOOKUP_SPECULATIVELY
1157 | (flags & LOOKUP_ONLYCONVERTING)
1158 | (flags & LOOKUP_NO_CONVERSION)
1159 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1161 if (ctor == error_mark_node)
1163 if (flags & LOOKUP_COMPLAIN)
1164 cp_error ("in conversion to type `%T'", type);
1165 if (flags & LOOKUP_SPECULATIVELY)
1167 return error_mark_node;
1170 if (conversion && ctor)
1172 if (flags & LOOKUP_COMPLAIN)
1173 error ("both constructor and type conversion operator apply");
1174 if (flags & LOOKUP_SPECULATIVELY)
1176 return error_mark_node;
1178 else if (conversion)
1182 ctor = build_cplus_new (type, ctor);
1188 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1189 then it won't be hashed and hence compare as not equal,
1191 if (code == ARRAY_TYPE
1192 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1193 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1196 if (flags & LOOKUP_COMPLAIN)
1197 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1198 TREE_TYPE (expr), type);
1199 if (flags & LOOKUP_SPECULATIVELY)
1201 return error_mark_node;
1204 /* Create an expression whose value is that of EXPR,
1205 converted to type TYPE. The TREE_TYPE of the value
1206 is always TYPE. This function implements all reasonable
1207 conversions; callers should filter out those that are
1208 not permitted by the language being compiled.
1210 Most of this routine is from build_reinterpret_cast.
1212 The backend cannot call cp_convert (what was convert) because
1213 conversions to/from basetypes may involve memory references
1214 (vbases) and adding or subtracting small values (multiple
1215 inheritance), but it calls convert from the constant folding code
1216 on subtrees of already build trees after it has ripped them apart.
1218 Also, if we ever support range variables, we'll probably also have to
1219 do a little bit more work. */
1222 convert (type, expr)
1227 if (type == error_mark_node || expr == error_mark_node)
1228 return error_mark_node;
1230 intype = TREE_TYPE (expr);
1232 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1234 if (TREE_READONLY_DECL_P (expr))
1235 expr = decl_constant_value (expr);
1236 return fold (build1 (NOP_EXPR, type, expr));
1239 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1240 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1243 /* Like cp_convert, except permit conversions to take place which
1244 are not normally allowed due to access restrictions
1245 (such as conversion from sub-type to private super-type). */
1248 convert_force (type, expr, convtype)
1253 register tree e = expr;
1254 register enum tree_code code = TREE_CODE (type);
1256 if (code == REFERENCE_TYPE)
1257 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1259 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1260 e = convert_from_reference (e);
1262 if (code == POINTER_TYPE)
1263 return fold (convert_to_pointer_force (type, e));
1265 /* From typeck.c convert_for_assignment */
1266 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1267 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1268 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1269 || integer_zerop (e)
1270 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1271 && TYPE_PTRMEMFUNC_P (type))
1273 /* compatible pointer to member functions. */
1274 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1277 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1280 /* Subroutine of build_type_conversion. */
1283 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1284 tree xtype, basetype;
1293 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1295 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1297 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1298 if (rval == error_mark_node)
1302 return error_mark_node;
1305 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1309 && TREE_TYPE (xtype)
1310 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1311 > TREE_READONLY (TREE_TYPE (xtype))))
1312 warning ("user-defined conversion casting away `const'");
1313 return cp_convert (xtype, rval);
1316 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1317 exists, return the attempted conversion. This may
1318 return ERROR_MARK_NODE if the conversion is not
1319 allowed (references private members, etc).
1320 If no conversion exists, NULL_TREE is returned.
1322 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1323 to take place immediately. Otherwise, we build a SAVE_EXPR
1324 which can be evaluated if the results are ever needed.
1326 Changes to this functions should be mirrored in user_harshness.
1328 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1329 object parameter, or by the second standard conversion sequence if
1330 that doesn't do it. This will probably wait for an overloading rewrite.
1334 build_type_conversion (code, xtype, expr, for_sure)
1335 enum tree_code code;
1339 /* C++: check to see if we can convert this aggregate type
1340 into the required type. */
1343 tree winner = NULL_TREE;
1345 if (flag_ansi_overloading)
1346 return build_user_type_conversion
1347 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1349 if (expr == error_mark_node)
1350 return error_mark_node;
1352 basetype = TREE_TYPE (expr);
1353 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1354 basetype = TREE_TYPE (basetype);
1356 basetype = TYPE_MAIN_VARIANT (basetype);
1357 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1360 /* Do we have an exact match? */
1362 tree typename = build_typename_overload (xtype);
1363 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1364 return build_type_conversion_1 (xtype, basetype, expr, typename,
1368 /* Nope; try looking for others. */
1369 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1371 tree cand = TREE_VALUE (conv);
1373 if (winner && winner == cand)
1376 if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1382 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1384 cp_error (" candidate conversions include `%D' and `%D'",
1395 return build_type_conversion_1 (xtype, basetype, expr,
1396 DECL_NAME (winner), for_sure);
1401 /* Convert the given EXPR to one of a group of types suitable for use in an
1402 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1403 which indicates which types are suitable. If COMPLAIN is 1, complain
1404 about ambiguity; otherwise, the caller will deal with it. */
1407 build_expr_type_conversion (desires, expr, complain)
1412 tree basetype = TREE_TYPE (expr);
1414 tree winner = NULL_TREE;
1416 if (TREE_CODE (basetype) == OFFSET_TYPE)
1417 expr = resolve_offset_ref (expr);
1418 expr = convert_from_reference (expr);
1419 basetype = TREE_TYPE (expr);
1421 if (! IS_AGGR_TYPE (basetype))
1422 switch (TREE_CODE (basetype))
1425 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1426 && integer_zerop (expr))
1428 /* else fall through... */
1431 return (desires & WANT_INT) ? expr : NULL_TREE;
1433 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1435 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1437 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1441 return (desires & WANT_POINTER) ? default_conversion (expr)
1447 if (! TYPE_HAS_CONVERSION (basetype))
1450 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1454 tree cand = TREE_VALUE (conv);
1456 if (winner && winner == cand)
1459 candidate = TREE_TYPE (TREE_TYPE (cand));
1460 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1461 candidate = TREE_TYPE (candidate);
1463 switch (TREE_CODE (candidate))
1467 win = (desires & WANT_INT); break;
1469 win = (desires & WANT_ENUM); break;
1471 win = (desires & WANT_FLOAT); break;
1473 win = (desires & WANT_POINTER); break;
1485 cp_error ("ambiguous default type conversion from `%T'",
1487 cp_error (" candidate conversions include `%D' and `%D'",
1490 return error_mark_node;
1499 tree type = TREE_TYPE (TREE_TYPE (winner));
1500 if (TREE_CODE (type) == REFERENCE_TYPE)
1501 type = TREE_TYPE (type);
1502 return build_type_conversion_1 (type, basetype, expr,
1503 DECL_NAME (winner), 1);
1509 /* Must convert two aggregate types to non-aggregate type.
1510 Attempts to find a non-ambiguous, "best" type conversion.
1512 Return 1 on success, 0 on failure.
1514 @@ What are the real semantics of this supposed to be??? */
1517 build_default_binary_type_conversion (code, arg1, arg2)
1518 enum tree_code code;
1524 case TRUNC_DIV_EXPR:
1526 case FLOOR_DIV_EXPR:
1527 case ROUND_DIV_EXPR:
1528 case EXACT_DIV_EXPR:
1529 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1530 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1533 case TRUNC_MOD_EXPR:
1534 case FLOOR_MOD_EXPR:
1540 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1541 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1546 tree a1, a2, p1, p2;
1549 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1550 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1551 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1552 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1554 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1557 error ("ambiguous default type conversion for `operator +'");
1560 *arg1 = a1, *arg2 = a2;
1562 *arg1 = a1, *arg2 = p2;
1564 *arg1 = p1, *arg2 = a2;
1570 tree a1, a2, p1, p2;
1573 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1574 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1575 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1576 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1578 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1581 error ("ambiguous default type conversion for `operator -'");
1584 *arg1 = a1, *arg2 = a2;
1586 *arg1 = p1, *arg2 = p2;
1588 *arg1 = p1, *arg2 = a2;
1599 tree a1, a2, p1, p2;
1602 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1603 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1604 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1605 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1607 wins = (a1 && a2) + (p1 && p2);
1610 cp_error ("ambiguous default type conversion for `%O'", code);
1613 *arg1 = a1, *arg2 = a2;
1615 *arg1 = p1, *arg2 = p2;
1619 case TRUTH_ANDIF_EXPR:
1620 case TRUTH_ORIF_EXPR:
1621 *arg1 = cp_convert (boolean_type_node, *arg1);
1622 *arg2 = cp_convert (boolean_type_node, *arg2);
1630 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1631 cp_error ("ambiguous default type conversion for `%O'", code);
1639 /* Implements integral promotion (4.1) and float->double promotion. */
1642 type_promotes_to (type)
1645 int constp, volatilep;
1647 if (type == error_mark_node)
1648 return error_mark_node;
1650 constp = TYPE_READONLY (type);
1651 volatilep = TYPE_VOLATILE (type);
1652 type = TYPE_MAIN_VARIANT (type);
1654 /* bool always promotes to int (not unsigned), even if it's the same
1656 if (type == boolean_type_node)
1657 type = integer_type_node;
1659 /* Normally convert enums to int, but convert wide enums to something
1661 else if (TREE_CODE (type) == ENUMERAL_TYPE
1662 || type == wchar_type_node)
1664 int precision = MAX (TYPE_PRECISION (type),
1665 TYPE_PRECISION (integer_type_node));
1666 tree totype = type_for_size (precision, 0);
1667 if (TREE_UNSIGNED (type)
1668 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1669 type = type_for_size (precision, 1);
1673 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1675 /* Retain unsignedness if really not getting bigger. */
1676 if (TREE_UNSIGNED (type)
1677 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1678 type = unsigned_type_node;
1680 type = integer_type_node;
1682 else if (type == float_type_node)
1683 type = double_type_node;
1685 return cp_build_type_variant (type, constp, volatilep);
1689 /* The routines below this point are carefully written to conform to
1690 the standard. They use the same terminology, and follow the rules
1691 closely. Although they are used only in pt.c at the moment, they
1692 should presumably be used everywhere in the future. */
1694 /* Attempt to perform qualification conversions on EXPR to convert it
1695 to TYPE. Return the resulting expression, or error_mark_node if
1696 the conversion was impossible. */
1699 perform_qualification_conversions (type, expr)
1703 if (comp_ptr_ttypes (type, TREE_TYPE(expr)))
1704 return build1 (NOP_EXPR, type, expr);
1706 return error_mark_node;