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 = 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 = 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);
927 FLAGS indicates how we should behave. */
930 cp_convert (type, expr, convtype, flags)
934 register tree e = expr;
935 register enum tree_code code = TREE_CODE (type);
937 if (e == error_mark_node
938 || TREE_TYPE (e) == error_mark_node)
939 return error_mark_node;
941 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
942 /* We need a new temporary; don't take this shortcut. */;
943 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
944 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
945 return fold (build1 (NOP_EXPR, type, e));
947 if (code == VOID_TYPE && (convtype & CONV_STATIC))
948 return build1 (CONVERT_EXPR, type, e);
951 /* This is incorrect. A truncation can't be stripped this way.
952 Extensions will be stripped by the use of get_unwidened. */
953 if (TREE_CODE (e) == NOP_EXPR)
954 return convert (type, TREE_OPERAND (e, 0));
957 /* Just convert to the type of the member. */
958 if (code == OFFSET_TYPE)
960 type = TREE_TYPE (type);
961 code = TREE_CODE (type);
965 if (code == REFERENCE_TYPE)
966 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
967 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
968 e = convert_from_reference (e);
971 if (TREE_CODE (e) == OFFSET_REF)
972 e = resolve_offset_ref (e);
974 if (TREE_READONLY_DECL_P (e))
975 e = decl_constant_value (e);
977 if (INTEGRAL_CODE_P (code))
979 tree intype = TREE_TYPE (e);
980 /* enum = enum, enum = int, enum = float, (enum)pointer are all
982 if (flag_int_enum_equivalence == 0
983 && TREE_CODE (type) == ENUMERAL_TYPE
984 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
985 || (TREE_CODE (intype) == POINTER_TYPE)))
987 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
989 if (flag_pedantic_errors)
990 return error_mark_node;
992 if (IS_AGGR_TYPE (intype))
995 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
998 if (flags & LOOKUP_COMPLAIN)
999 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1000 if (flags & LOOKUP_SPECULATIVELY)
1002 return error_mark_node;
1004 if (code == BOOLEAN_TYPE)
1006 /* Common Ada/Pascal programmer's mistake. We always warn
1007 about this since it is so bad. */
1008 if (TREE_CODE (expr) == FUNCTION_DECL)
1009 cp_warning ("the address of `%D', will always be `true'", expr);
1010 return truthvalue_conversion (e);
1012 return fold (convert_to_integer (type, e));
1014 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1015 || TYPE_PTRMEMFUNC_P (type))
1016 return fold (cp_convert_to_pointer (type, e));
1017 if (code == REAL_TYPE)
1019 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1022 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1026 if (flags & LOOKUP_COMPLAIN)
1027 cp_error ("`%#T' used where a floating point value was expected",
1030 return fold (convert_to_real (type, e));
1033 /* New C++ semantics: since assignment is now based on
1034 memberwise copying, if the rhs type is derived from the
1035 lhs type, then we may still do a conversion. */
1036 if (IS_AGGR_TYPE_CODE (code))
1038 tree dtype = TREE_TYPE (e);
1039 tree ctor = NULL_TREE;
1040 tree conversion = NULL_TREE;
1042 dtype = TYPE_MAIN_VARIANT (dtype);
1044 /* Conversion of object pointers or signature pointers/references
1045 to signature pointers/references. */
1047 if (TYPE_LANG_SPECIFIC (type)
1048 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1050 tree constructor = build_signature_pointer_constructor (type, expr);
1051 tree sig_ty = SIGNATURE_TYPE (type);
1054 if (constructor == error_mark_node)
1055 return error_mark_node;
1057 sig_ptr = get_temp_name (type, 1);
1058 DECL_INITIAL (sig_ptr) = constructor;
1059 CLEAR_SIGNATURE (sig_ty);
1060 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1061 SET_SIGNATURE (sig_ty);
1062 TREE_READONLY (sig_ptr) = 1;
1067 /* Conversion between aggregate types. New C++ semantics allow
1068 objects of derived type to be cast to objects of base type.
1069 Old semantics only allowed this between pointers.
1071 There may be some ambiguity between using a constructor
1072 vs. using a type conversion operator when both apply. */
1074 if (flag_ansi_overloading)
1078 if ((flags & LOOKUP_ONLYCONVERTING)
1079 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
1080 ctor = build_user_type_conversion (type, ctor, flags);
1082 ctor = build_method_call (NULL_TREE, ctor_identifier,
1083 build_tree_list (NULL_TREE, ctor),
1084 TYPE_BINFO (type), flags);
1086 return build_cplus_new (type, ctor);
1090 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1091 && TYPE_HAS_CONVERSION (dtype))
1092 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1094 if (conversion == error_mark_node)
1096 if (flags & LOOKUP_COMPLAIN)
1097 error ("ambiguous pointer conversion");
1101 if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1102 ctor = build_method_call (NULL_TREE, ctor_identifier,
1103 build_tree_list (NULL_TREE, e),
1105 (flags & LOOKUP_NORMAL)
1106 | LOOKUP_SPECULATIVELY
1107 | (flags & LOOKUP_ONLYCONVERTING)
1108 | (flags & LOOKUP_NO_CONVERSION)
1109 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1111 if (ctor == error_mark_node)
1113 if (flags & LOOKUP_COMPLAIN)
1114 cp_error ("in conversion to type `%T'", type);
1115 if (flags & LOOKUP_SPECULATIVELY)
1117 return error_mark_node;
1120 if (conversion && ctor)
1122 if (flags & LOOKUP_COMPLAIN)
1123 error ("both constructor and type conversion operator apply");
1124 if (flags & LOOKUP_SPECULATIVELY)
1126 return error_mark_node;
1128 else if (conversion)
1132 ctor = build_cplus_new (type, ctor);
1138 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1139 then the it won't be hashed and hence compare as not equal,
1141 if (code == ARRAY_TYPE
1142 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1143 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1146 if (flags & LOOKUP_COMPLAIN)
1147 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1148 TREE_TYPE (expr), type);
1149 if (flags & LOOKUP_SPECULATIVELY)
1151 return error_mark_node;
1154 /* Create an expression whose value is that of EXPR,
1155 converted to type TYPE. The TREE_TYPE of the value
1156 is always TYPE. This function implements all reasonable
1157 conversions; callers should filter out those that are
1158 not permitted by the language being compiled. */
1161 convert (type, expr)
1164 return cp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
1167 /* Like convert, except permit conversions to take place which
1168 are not normally allowed due to access restrictions
1169 (such as conversion from sub-type to private super-type). */
1172 convert_force (type, expr, convtype)
1177 register tree e = expr;
1178 register enum tree_code code = TREE_CODE (type);
1180 if (code == REFERENCE_TYPE)
1181 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1183 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1184 e = convert_from_reference (e);
1186 if (code == POINTER_TYPE)
1187 return fold (convert_to_pointer_force (type, e));
1189 /* From typeck.c convert_for_assignment */
1190 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1191 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1192 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1193 || integer_zerop (e)
1194 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1195 && TYPE_PTRMEMFUNC_P (type))
1197 /* compatible pointer to member functions. */
1198 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1201 return cp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1204 /* Subroutine of build_type_conversion. */
1207 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1208 tree xtype, basetype;
1217 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1219 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1221 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1222 if (rval == error_mark_node)
1226 return error_mark_node;
1229 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1233 && TREE_TYPE (xtype)
1234 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1235 > TREE_READONLY (TREE_TYPE (xtype))))
1236 warning ("user-defined conversion casting away `const'");
1237 return convert (xtype, rval);
1240 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1241 exists, return the attempted conversion. This may
1242 return ERROR_MARK_NODE if the conversion is not
1243 allowed (references private members, etc).
1244 If no conversion exists, NULL_TREE is returned.
1246 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1247 to take place immediately. Otherwise, we build a SAVE_EXPR
1248 which can be evaluated if the results are ever needed.
1250 Changes to this functions should be mirrored in user_harshness.
1252 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1253 object parameter, or by the second standard conversion sequence if
1254 that doesn't do it. This will probably wait for an overloading rewrite.
1258 build_type_conversion (code, xtype, expr, for_sure)
1259 enum tree_code code;
1263 /* C++: check to see if we can convert this aggregate type
1264 into the required type. */
1267 tree winner = NULL_TREE;
1269 if (flag_ansi_overloading)
1270 return build_user_type_conversion
1271 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1273 if (expr == error_mark_node)
1274 return error_mark_node;
1276 basetype = TREE_TYPE (expr);
1277 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1278 basetype = TREE_TYPE (basetype);
1280 basetype = TYPE_MAIN_VARIANT (basetype);
1281 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1284 /* Do we have an exact match? */
1286 tree typename = build_typename_overload (xtype);
1287 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1288 return build_type_conversion_1 (xtype, basetype, expr, typename,
1292 /* Nope; try looking for others. */
1293 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1295 tree cand = TREE_VALUE (conv);
1297 if (winner && winner == cand)
1300 if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1306 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1308 cp_error (" candidate conversions include `%D' and `%D'",
1319 return build_type_conversion_1 (xtype, basetype, expr,
1320 DECL_NAME (winner), for_sure);
1325 /* Convert the given EXPR to one of a group of types suitable for use in an
1326 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1327 which indicates which types are suitable. If COMPLAIN is 1, complain
1328 about ambiguity; otherwise, the caller will deal with it. */
1331 build_expr_type_conversion (desires, expr, complain)
1336 tree basetype = TREE_TYPE (expr);
1338 tree winner = NULL_TREE;
1340 if (TREE_CODE (basetype) == OFFSET_TYPE)
1341 expr = resolve_offset_ref (expr);
1342 expr = convert_from_reference (expr);
1343 basetype = TREE_TYPE (expr);
1345 if (! IS_AGGR_TYPE (basetype))
1346 switch (TREE_CODE (basetype))
1349 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1350 && integer_zerop (expr))
1352 /* else fall through... */
1355 return (desires & WANT_INT) ? expr : NULL_TREE;
1357 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1359 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1361 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1365 return (desires & WANT_POINTER) ? default_conversion (expr)
1371 if (! TYPE_HAS_CONVERSION (basetype))
1374 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1378 tree cand = TREE_VALUE (conv);
1380 if (winner && winner == cand)
1383 candidate = TREE_TYPE (TREE_TYPE (cand));
1384 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1385 candidate = TREE_TYPE (candidate);
1387 switch (TREE_CODE (candidate))
1391 win = (desires & WANT_INT); break;
1393 win = (desires & WANT_ENUM); break;
1395 win = (desires & WANT_FLOAT); break;
1397 win = (desires & WANT_POINTER); break;
1406 cp_error ("ambiguous default type conversion from `%T'",
1408 cp_error (" candidate conversions include `%D' and `%D'",
1411 return error_mark_node;
1420 tree type = TREE_TYPE (TREE_TYPE (winner));
1421 if (TREE_CODE (type) == REFERENCE_TYPE)
1422 type = TREE_TYPE (type);
1423 return build_type_conversion_1 (type, basetype, expr,
1424 DECL_NAME (winner), 1);
1430 /* Must convert two aggregate types to non-aggregate type.
1431 Attempts to find a non-ambiguous, "best" type conversion.
1433 Return 1 on success, 0 on failure.
1435 @@ What are the real semantics of this supposed to be??? */
1438 build_default_binary_type_conversion (code, arg1, arg2)
1439 enum tree_code code;
1445 case TRUNC_DIV_EXPR:
1447 case FLOOR_DIV_EXPR:
1448 case ROUND_DIV_EXPR:
1449 case EXACT_DIV_EXPR:
1450 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1451 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1454 case TRUNC_MOD_EXPR:
1455 case FLOOR_MOD_EXPR:
1461 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1462 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1467 tree a1, a2, p1, p2;
1470 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1471 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1472 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1473 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1475 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1478 error ("ambiguous default type conversion for `operator +'");
1481 *arg1 = a1, *arg2 = a2;
1483 *arg1 = a1, *arg2 = p2;
1485 *arg1 = p1, *arg2 = a2;
1491 tree a1, a2, p1, p2;
1494 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1495 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1496 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1497 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1499 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1502 error ("ambiguous default type conversion for `operator -'");
1505 *arg1 = a1, *arg2 = a2;
1507 *arg1 = p1, *arg2 = p2;
1509 *arg1 = p1, *arg2 = a2;
1520 tree a1, a2, p1, p2;
1523 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1524 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1525 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1526 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1528 wins = (a1 && a2) + (p1 && p2);
1531 cp_error ("ambiguous default type conversion for `%O'", code);
1534 *arg1 = a1, *arg2 = a2;
1536 *arg1 = p1, *arg2 = p2;
1540 case TRUTH_ANDIF_EXPR:
1541 case TRUTH_ORIF_EXPR:
1542 *arg1 = convert (boolean_type_node, *arg1);
1543 *arg2 = convert (boolean_type_node, *arg2);
1551 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1552 cp_error ("ambiguous default type conversion for `%O'", code);
1560 /* Implements integral promotion (4.1) and float->double promotion. */
1563 type_promotes_to (type)
1566 int constp, volatilep;
1568 if (type == error_mark_node)
1569 return error_mark_node;
1571 constp = TYPE_READONLY (type);
1572 volatilep = TYPE_VOLATILE (type);
1573 type = TYPE_MAIN_VARIANT (type);
1575 /* bool always promotes to int (not unsigned), even if it's the same
1577 if (type == boolean_type_node)
1578 type = integer_type_node;
1580 /* Normally convert enums to int, but convert wide enums to something
1582 else if (TREE_CODE (type) == ENUMERAL_TYPE
1583 || type == wchar_type_node)
1585 int precision = MAX (TYPE_PRECISION (type),
1586 TYPE_PRECISION (integer_type_node));
1587 tree totype = type_for_size (precision, 0);
1588 if (TREE_UNSIGNED (type)
1589 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1590 type = type_for_size (precision, 1);
1594 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1596 /* Retain unsignedness if really not getting bigger. */
1597 if (TREE_UNSIGNED (type)
1598 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1599 type = unsigned_type_node;
1601 type = integer_type_node;
1603 else if (type == float_type_node)
1604 type = double_type_node;
1606 return cp_build_type_variant (type, constp, volatilep);