1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file contains the functions for converting C expressions
24 to different data types. The only entry point is `convert'.
25 Every language front end must have a `convert' function
26 but what kind of conversions it does will depend on the language. */
36 #define NULL (char *)0
38 tree build_user_type_conversion ();
40 /* Change of width--truncation and extension of integers or reals--
41 is represented with NOP_EXPR. Proper functioning of many things
42 assumes that no other conversions can be NOP_EXPRs.
44 Conversion between integer and pointer is represented with CONVERT_EXPR.
45 Converting integer to real uses FLOAT_EXPR
46 and real to integer uses FIX_TRUNC_EXPR.
48 Here is a list of all the functions that assume that widening and
49 narrowing is always done with a NOP_EXPR:
50 In convert.c, convert_to_integer.
51 In c-typeck.c, build_binary_op_nodefault (boolean ops),
52 and truthvalue_conversion.
53 In expr.c: expand_expr, for operands of a MULT_EXPR.
54 In fold-const.c: fold.
55 In tree.c: get_narrower and get_unwidened.
57 C++: in multiple-inheritance, converting between pointers may involve
58 adjusting them by a delta stored within the class definition. */
60 /* Subroutines of `convert'. */
62 /* Build a thunk. What it is, is an entry point that when called will
63 adjust the this pointer (the first argument) by offset, and then
64 goto the real address of the function given by REAL_ADDR that we
65 would like called. What we return is the address of the thunk. */
68 build_thunk (offset, real_addr)
69 tree offset, real_addr;
71 if (TREE_CODE (real_addr) != ADDR_EXPR
72 || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
74 sorry ("MI pointer to member conversion too complex");
75 return error_mark_node;
77 sorry ("MI pointer to member conversion too complex");
78 return error_mark_node;
81 /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
82 another `pointer to method'. This may involved the creation of
83 a thunk to handle the this offset calculation. */
86 convert_fn_ptr (type, expr)
89 #if 0 /* We don't use thunks for pmfs. */
90 if (flag_vtable_thunks)
92 tree intype = TREE_TYPE (expr);
93 tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype)),
94 TYPE_METHOD_BASETYPE (TREE_TYPE (type)), 1);
95 if (binfo == error_mark_node)
97 error (" in pointer to member conversion");
98 return error_mark_node;
100 if (binfo == NULL_TREE)
102 /* ARM 4.8 restriction. */
103 error ("invalid pointer to member conversion");
104 return error_mark_node;
107 if (BINFO_OFFSET_ZEROP (binfo))
108 return build1 (NOP_EXPR, type, expr);
109 return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
113 return build_ptrmemfunc (type, expr, 1);
116 /* if converting pointer to pointer
117 if dealing with classes, check for derived->base or vice versa
118 else if dealing with method pointers, delegate
120 else if converting class, pass off to build_type_conversion
121 else try C-style pointer conversion */
124 cp_convert_to_pointer (type, expr)
127 register tree intype = TREE_TYPE (expr);
128 register enum tree_code form;
130 if (IS_AGGR_TYPE (intype))
134 intype = complete_type (intype);
135 if (TYPE_SIZE (intype) == NULL_TREE)
137 cp_error ("can't convert from incomplete type `%T' to `%T'",
139 return error_mark_node;
142 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
145 if (rval == error_mark_node)
146 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
152 if (TYPE_PTRMEMFUNC_P (type))
153 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
154 if (TYPE_PTRMEMFUNC_P (intype))
155 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
157 form = TREE_CODE (intype);
159 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
161 intype = TYPE_MAIN_VARIANT (intype);
163 if (TYPE_MAIN_VARIANT (type) != intype
164 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
165 && IS_AGGR_TYPE (TREE_TYPE (type))
166 && IS_AGGR_TYPE (TREE_TYPE (intype))
167 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
169 enum tree_code code = PLUS_EXPR;
170 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
171 if (binfo == error_mark_node)
172 return error_mark_node;
173 if (binfo == NULL_TREE)
175 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
176 if (binfo == error_mark_node)
177 return error_mark_node;
182 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
183 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
184 || ! BINFO_OFFSET_ZEROP (binfo))
186 /* Need to get the path we took. */
189 if (code == PLUS_EXPR)
190 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
192 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
193 return build_vbase_path (code, type, expr, path, 0);
197 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
198 && TREE_CODE (type) == POINTER_TYPE
199 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
200 return convert_fn_ptr (type, expr);
202 if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
203 && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
205 tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
206 tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
207 tree binfo = get_binfo (b1, b2, 1);
208 if (binfo == NULL_TREE)
209 binfo = get_binfo (b2, b1, 1);
210 if (binfo == error_mark_node)
211 return error_mark_node;
214 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
215 || (TREE_CODE (type) == POINTER_TYPE
216 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
218 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
220 return error_mark_node;
223 return build1 (NOP_EXPR, type, expr);
226 my_friendly_assert (form != OFFSET_TYPE, 186);
228 if (TYPE_LANG_SPECIFIC (intype)
229 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
230 return convert_to_pointer (type, build_optr_ref (expr));
232 if (integer_zerop (expr))
234 expr = build_int_2 (0, 0);
235 TREE_TYPE (expr) = type;
239 if (INTEGRAL_CODE_P (form))
241 if (type_precision (intype) == POINTER_SIZE)
242 return build1 (CONVERT_EXPR, type, expr);
243 expr = convert (type_for_size (POINTER_SIZE, 0), expr);
244 /* Modes may be different but sizes should be the same. */
245 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
246 != GET_MODE_SIZE (TYPE_MODE (type)))
247 /* There is supposed to be some integral type
248 that is the same width as a pointer. */
250 return convert_to_pointer (type, expr);
253 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
255 return error_mark_node;
258 /* Like convert, except permit conversions to take place which
259 are not normally allowed due to access restrictions
260 (such as conversion from sub-type to private super-type). */
263 convert_to_pointer_force (type, expr)
266 register tree intype = TREE_TYPE (expr);
267 register enum tree_code form = TREE_CODE (intype);
269 if (integer_zerop (expr))
271 expr = build_int_2 (0, 0);
272 TREE_TYPE (expr) = type;
276 /* Convert signature pointer/reference to `void *' first. */
277 if (form == RECORD_TYPE
278 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
280 expr = build_optr_ref (expr);
281 intype = TREE_TYPE (expr);
282 form = TREE_CODE (intype);
285 if (TREE_CODE (type) == POINTER_TYPE
286 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
288 /* Allow an implicit this pointer for pointer to member
290 if (TYPE_PTRMEMFUNC_P (intype))
292 tree decl, basebinfo;
293 tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
294 tree t = TYPE_METHOD_BASETYPE (fntype);
296 if (current_class_type == 0
297 || get_base_distance (t, current_class_type, 0, &basebinfo) == -1)
299 decl = build1 (NOP_EXPR, t, error_mark_node);
301 else if (current_class_ptr == 0)
302 decl = build1 (NOP_EXPR, t, error_mark_node);
304 decl = current_class_ref;
306 expr = build (OFFSET_REF, fntype, decl, expr);
307 intype = TREE_TYPE (expr);
310 if (TREE_CODE (expr) == OFFSET_REF && TREE_CODE (intype) == METHOD_TYPE)
311 expr = resolve_offset_ref (expr);
312 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
313 expr = build_addr_func (expr);
314 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
317 && TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)
318 cp_pedwarn ("cannot convert `%T' to `%T'", intype, type);
319 return build1 (NOP_EXPR, type, expr);
323 if (form == POINTER_TYPE)
325 intype = TYPE_MAIN_VARIANT (intype);
327 if (TYPE_MAIN_VARIANT (type) != intype
328 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
329 && IS_AGGR_TYPE (TREE_TYPE (type))
330 && IS_AGGR_TYPE (TREE_TYPE (intype))
331 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
333 enum tree_code code = PLUS_EXPR;
335 int distance = get_base_distance (TREE_TYPE (type),
336 TREE_TYPE (intype), 0, &path);
340 cp_error ("type `%T' is ambiguous baseclass of `%s'", TREE_TYPE (type),
341 TYPE_NAME_STRING (TREE_TYPE (intype)));
342 return error_mark_node;
346 distance = get_base_distance (TREE_TYPE (intype),
347 TREE_TYPE (type), 0, &path);
351 /* Doesn't need any special help from us. */
352 return build1 (NOP_EXPR, type, expr);
356 return build_vbase_path (code, type, expr, path, 0);
358 return build1 (NOP_EXPR, type, expr);
361 return cp_convert_to_pointer (type, expr);
364 /* We are passing something to a function which requires a reference.
365 The type we are interested in is in TYPE. The initial
366 value we have to begin with is in ARG.
368 FLAGS controls how we manage access checking.
369 INDIRECT_BIND in FLAGS controls how any temporarys are generated.
370 CHECKCONST controls if we report error messages on const subversion. */
373 build_up_reference (type, arg, flags, checkconst)
375 int flags, checkconst;
378 int literal_flag = 0;
379 tree argtype = TREE_TYPE (arg);
380 tree target_type = TREE_TYPE (type);
381 tree binfo = NULL_TREE;
383 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
384 if ((flags & LOOKUP_PROTECT)
385 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
386 && IS_AGGR_TYPE (argtype)
387 && IS_AGGR_TYPE (target_type))
389 binfo = get_binfo (target_type, argtype, 1);
390 if (binfo == error_mark_node)
391 return error_mark_node;
392 if (binfo == NULL_TREE)
393 return error_not_base_type (target_type, argtype);
396 /* Pass along const and volatile down into the type. */
397 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
398 target_type = cp_build_type_variant (target_type, TYPE_READONLY (type),
399 TYPE_VOLATILE (type));
401 if (TREE_CODE (targ) == SAVE_EXPR)
402 targ = TREE_OPERAND (targ, 0);
403 while (TREE_CODE (targ) == NOP_EXPR
404 && (TYPE_MAIN_VARIANT (argtype)
405 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0)))))
406 targ = TREE_OPERAND (targ, 0);
408 switch (TREE_CODE (targ))
411 /* This is a call to a constructor which did not know what it was
412 initializing until now: it needs to initialize a temporary. */
413 if (TREE_HAS_CONSTRUCTOR (targ))
415 tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0));
416 TREE_HAS_CONSTRUCTOR (targ) = 0;
417 return build_up_reference (type, temp, flags, 1);
419 /* Let &* cancel out to simplify resulting code.
420 Also, throw away intervening NOP_EXPRs. */
421 arg = TREE_OPERAND (targ, 0);
422 if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
423 || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
424 arg = TREE_OPERAND (arg, 0);
426 /* in doing a &*, we have to get rid of the const'ness on the pointer
427 value. Haven't thought about volatile here. Pointers come to mind
429 if (TREE_READONLY (arg))
431 arg = copy_node (arg);
432 TREE_READONLY (arg) = 0;
435 rval = build1 (CONVERT_EXPR, type, arg);
436 TREE_REFERENCE_EXPR (rval) = 1;
438 /* propagate the const flag on something like:
445 class Derived : public Base {
452 void func2(const Derived& d) {
456 on the d parameter. The below could have been avoided, if the flags
457 were down in the tree, not sure why they are not. (mrs) */
458 /* The below code may have to be propagated to other parts of this
460 if (TREE_READONLY (targ) && !TREE_READONLY (arg)
461 && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
462 && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
463 && (TYPE_READONLY (target_type) && checkconst))
465 arg = copy_node (arg);
466 TREE_READONLY (arg) = TREE_READONLY (targ);
468 literal_flag = TREE_CONSTANT (arg);
472 /* Get this out of a register if we happened to be in one by accident.
473 Also, build up references to non-lvalues it we must. */
474 /* For &x[y], return (&) x+y */
476 if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
477 return error_mark_node;
478 rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
479 TREE_OPERAND (targ, 1), 1);
480 TREE_TYPE (rval) = type;
481 if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
482 && staticp (TREE_OPERAND (targ, 0)))
483 TREE_CONSTANT (rval) = 1;
487 /* Could be a reference to a static member. */
489 tree field = TREE_OPERAND (targ, 1);
490 if (TREE_STATIC (field))
492 rval = build1 (ADDR_EXPR, type, field);
498 /* We should have farmed out member pointers above. */
499 my_friendly_abort (188);
502 rval = build_component_addr (targ, build_pointer_type (argtype),
503 "attempt to make a reference to bit-field structure member `%s'");
504 TREE_TYPE (rval) = type;
505 literal_flag = staticp (TREE_OPERAND (targ, 0));
509 /* Anything not already handled and not a true memory reference
510 needs to have a reference built up. Do so silently for
511 things like integers and return values from function,
512 but complain if we need a reference to something declared
518 TREE_ADDRESSABLE (targ) = 1;
519 put_var_into_stack (targ);
524 if (targ == current_class_ptr)
526 error ("address of `this' not available");
528 /* This code makes the following core dump the compiler on a sun4,
529 if the code below is used.
533 typedef a_decl* a_ref;
538 void* append(a_ref& item);
542 a_decl (e_decl *parent);
556 a_decl::a_decl(e_decl *parent) {
557 parent->implementations.append(this);
561 TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
562 put_var_into_stack (targ);
565 return error_mark_node;
572 if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ)
573 && !DECL_ARTIFICIAL (targ))
574 cp_warning ("address needed to build reference for `%D', which is declared `register'",
576 else if (staticp (targ))
579 TREE_ADDRESSABLE (targ) = 1;
580 put_var_into_stack (targ);
585 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
587 rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
588 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
592 case PREINCREMENT_EXPR:
593 case PREDECREMENT_EXPR:
597 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
599 rval = build (COMPOUND_EXPR, type, arg, real_reference);
600 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
605 return build (COND_EXPR, type,
606 TREE_OPERAND (targ, 0),
607 build_up_reference (type, TREE_OPERAND (targ, 1),
609 build_up_reference (type, TREE_OPERAND (targ, 2),
612 /* Undo the folding... */
615 return build (COND_EXPR, type,
616 build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR,
617 boolean_type_node, TREE_OPERAND (targ, 0),
618 TREE_OPERAND (targ, 1)),
619 build_up_reference (type, TREE_OPERAND (targ, 0),
621 build_up_reference (type, TREE_OPERAND (targ, 1),
625 arg = TREE_OPERAND (targ, 1);
626 if (arg == NULL_TREE)
628 compiler_error ("({ ... }) expression not expanded when needed for reference");
629 return error_mark_node;
631 rval = build1 (ADDR_EXPR, type, arg);
632 TREE_REFERENCE_EXPR (rval) = 1;
636 TREE_ADDRESSABLE (targ) = 1;
637 put_var_into_stack (TREE_OPERAND (targ, 0));
644 if (TREE_ADDRESSABLE (targ) == 0)
646 if (! (flags&INDIRECT_BIND)
647 && toplevel_bindings_p ())
649 tree temp = get_temp_name (argtype, 0);
650 /* Give this new temp some rtl and initialize it. */
651 DECL_INITIAL (temp) = targ;
652 TREE_STATIC (temp) = 1;
653 cp_finish_decl (temp, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
654 /* Do this after declaring it static. */
655 rval = build_unary_op (ADDR_EXPR, temp, 0);
656 TREE_TYPE (rval) = type;
657 literal_flag = TREE_CONSTANT (rval);
661 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
663 arg = build_cplus_new (argtype, targ);
665 else if (flags&INDIRECT_BIND)
667 /* This should be the default, not the below code. */
668 /* All callers except grok_reference_init should probably
669 use INDIRECT_BIND. */
670 tree slot = build (VAR_DECL, argtype);
671 layout_decl (slot, 0);
672 arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
676 tree temp = get_temp_name (argtype, 0);
677 rval = build_unary_op (ADDR_EXPR, temp, 0);
678 if (binfo && !BINFO_OFFSET_ZEROP (binfo))
679 rval = convert_pointer_to (target_type, rval);
681 TREE_TYPE (rval) = type;
683 temp = build (MODIFY_EXPR, argtype, temp, arg);
684 TREE_SIDE_EFFECTS (temp) = 1;
685 return build (COMPOUND_EXPR, type, temp, rval);
689 if (! (flags&INDIRECT_BIND))
691 if (TREE_CODE (arg) == TARGET_EXPR)
693 tree decl = TREE_OPERAND (arg, 0);
696 if (! toplevel_bindings_p () && ! DECL_RTL (decl))
699 cleanup = maybe_build_cleanup (decl);
701 expand_decl_cleanup (decl, cleanup);
706 rval = build1 (ADDR_EXPR, type, arg);
709 if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
710 || TYPE_USES_COMPLEX_INHERITANCE (target_type))
712 TREE_TYPE (rval) = build_pointer_type (argtype);
713 if (flags & LOOKUP_PROTECT)
714 rval = convert_pointer_to (target_type, rval);
717 = convert_to_pointer_force (build_pointer_type (target_type), rval);
718 TREE_TYPE (rval) = type;
719 if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR)
720 TREE_TYPE (TREE_OPERAND (rval, 0))
721 = TREE_TYPE (TREE_OPERAND (rval, 1)) = type;
723 TREE_CONSTANT (rval) = literal_flag;
727 /* For C++: Only need to do one-level references, but cannot
728 get tripped up on signed/unsigned differences.
730 DECL is either NULL_TREE or the _DECL node for a reference that is being
731 initialized. It can be error_mark_node if we don't know the _DECL but
732 we know it's an initialization. */
735 convert_to_reference (reftype, expr, convtype, flags, decl)
740 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
741 register tree intype = TREE_TYPE (expr);
742 tree rval = NULL_TREE;
743 tree rval_as_conversion = NULL_TREE;
746 if (TREE_CODE (intype) == REFERENCE_TYPE)
747 my_friendly_abort (364);
749 intype = TYPE_MAIN_VARIANT (intype);
751 i = comp_target_types (type, intype, 0);
753 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
754 && ! (flags & LOOKUP_NO_CONVERSION))
756 /* Look for a user-defined conversion to lvalue that we can use. */
758 if (flag_ansi_overloading)
760 = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
762 rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
764 if (rval_as_conversion && rval_as_conversion != error_mark_node
765 && real_lvalue_p (rval_as_conversion))
767 expr = rval_as_conversion;
768 rval_as_conversion = NULL_TREE;
774 if (((convtype & CONV_STATIC) && i == -1)
775 || ((convtype & CONV_IMPLICIT) && i == 1))
777 if (flags & LOOKUP_COMPLAIN)
779 tree ttl = TREE_TYPE (reftype);
783 int r = TREE_READONLY (expr);
784 int v = TREE_THIS_VOLATILE (expr);
785 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
788 if (! real_lvalue_p (expr) && ! TYPE_READONLY (ttl))
791 /* Ensure semantics of [dcl.init.ref] */
792 cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'",
795 cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'",
798 else if (! (convtype & CONV_CONST))
800 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
801 cp_pedwarn ("conversion from `%T' to `%T' discards const",
803 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
804 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
809 return build_up_reference (reftype, expr, flags,
810 ! (convtype & CONV_CONST));
812 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
814 /* When casting an lvalue to a reference type, just convert into
815 a pointer to the new type and deference it. This is allowed
816 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
817 should be done directly (jason). (int &)ri ---> *(int*)&ri */
819 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
821 if (TREE_CODE (intype) == POINTER_TYPE
822 && (comptypes (TREE_TYPE (intype), type, -1)))
823 cp_warning ("casting `%T' to `%T' does not dereference pointer",
826 rval = build_unary_op (ADDR_EXPR, expr, 0);
827 if (rval != error_mark_node)
828 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
829 if (rval != error_mark_node)
830 rval = build1 (NOP_EXPR, reftype, rval);
834 tree rval_as_ctor = NULL_TREE;
836 if (rval_as_conversion)
838 if (rval_as_conversion == error_mark_node)
840 cp_error ("conversion from `%T' to `%T' is ambiguous",
842 return error_mark_node;
844 rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
848 /* Definitely need to go through a constructor here. */
849 if (TYPE_HAS_CONSTRUCTOR (type)
850 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
851 && (rval = build_method_call
852 (NULL_TREE, ctor_identifier,
853 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
854 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
855 | LOOKUP_ONLYCONVERTING)))
859 if (toplevel_bindings_p ())
861 extern tree static_aggregates;
862 tree t = get_temp_name (type, toplevel_bindings_p ());
863 init = build_method_call (t, ctor_identifier,
864 build_tree_list (NULL_TREE, expr),
866 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
867 | LOOKUP_ONLYCONVERTING);
869 if (init == error_mark_node)
870 return error_mark_node;
872 make_decl_rtl (t, NULL_PTR, 1);
873 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
874 rval = build_unary_op (ADDR_EXPR, t, 0);
878 init = build_method_call (NULL_TREE, ctor_identifier,
879 build_tree_list (NULL_TREE, expr),
881 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
882 |LOOKUP_ONLYCONVERTING);
884 if (init == error_mark_node)
885 return error_mark_node;
887 rval = build_cplus_new (type, init);
888 rval = build_up_reference (reftype, rval, flags, 1);
893 if (rval_as_ctor && rval_as_conversion)
895 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
897 return error_mark_node;
899 else if (rval_as_ctor)
901 else if (rval_as_conversion)
902 rval = rval_as_conversion;
903 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
905 rval = convert (type, expr);
906 if (rval == error_mark_node)
907 return error_mark_node;
909 rval = build_up_reference (reftype, rval, flags, 1);
912 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
913 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
919 /* If we found a way to convert earlier, then use it. */
923 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
925 if (flags & LOOKUP_COMPLAIN)
926 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
928 if (flags & LOOKUP_SPECULATIVELY)
931 return error_mark_node;
934 /* We are using a reference VAL for its value. Bash that reference all the
935 way down to its lowest form. */
938 convert_from_reference (val)
941 tree type = TREE_TYPE (val);
943 if (TREE_CODE (type) == OFFSET_TYPE)
944 type = TREE_TYPE (type);
945 if (TREE_CODE (type) == REFERENCE_TYPE)
946 return build_indirect_ref (val, NULL_PTR);
950 /* See if there is a constructor of type TYPE which will convert
951 EXPR. The reference manual seems to suggest (8.5.6) that we need
952 not worry about finding constructors for base classes, then converting
953 to the derived class.
955 MSGP is a pointer to a message that would be an appropriate error
956 string. If MSGP is NULL, then we are not interested in reporting
960 convert_to_aggr (type, expr, msgp, protect)
965 tree basetype = type;
966 tree name = TYPE_IDENTIFIER (basetype);
967 tree function, fndecl, fntype, parmtypes, parmlist, result;
969 /* See code below that used this. */
973 int can_be_private, can_be_protected;
975 if (! TYPE_HAS_CONSTRUCTOR (basetype))
978 *msgp = "type `%s' does not have a constructor";
979 return error_mark_node;
982 access = access_public_node;
984 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
986 parmlist = build_tree_list (NULL_TREE, expr);
987 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
989 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
991 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
992 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
995 /* The type of the first argument will be filled in inside the loop. */
996 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
997 parmtypes = tree_cons (NULL_TREE, build_pointer_type (basetype), parmtypes);
999 /* No exact conversion was found. See if an approximate
1001 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
1004 int saw_private = 0;
1005 int saw_protected = 0;
1006 struct candidate *candidates =
1007 (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
1008 struct candidate *cp = candidates;
1014 cp->harshness = (struct harshness_code *)
1015 alloca (3 * sizeof (struct harshness_code));
1017 compute_conversion_costs (fndecl, parmlist, cp, 2);
1018 if ((cp->h.code & EVIL_CODE) == 0)
1020 cp->u.field = fndecl;
1023 if (TREE_PRIVATE (fndecl))
1024 access = access_private_node;
1025 else if (TREE_PROTECTED (fndecl))
1026 access = access_protected_node;
1028 access = access_public_node;
1031 access = access_public_node;
1033 if (access == access_private_node
1034 ? (basetype == current_class_type
1035 || is_friend (basetype, cp->function)
1036 || purpose_member (basetype, DECL_ACCESS (fndecl)))
1037 : access == access_protected_node
1039 || purpose_member (basetype, DECL_ACCESS (fndecl)))
1042 if (cp->h.code <= TRIVIAL_CODE)
1048 if (access == access_private_node)
1054 fndecl = DECL_CHAIN (fndecl);
1056 if (cp - candidates)
1058 /* Rank from worst to best. Then cp will point to best one.
1059 Private fields have their bits flipped. For unsigned
1060 numbers, this should make them look very large.
1061 If the best alternate has a (signed) negative value,
1062 then all we ever saw were private members. */
1063 if (cp - candidates > 1)
1064 qsort (candidates, /* char *base */
1065 cp - candidates, /* int nel */
1066 sizeof (struct candidate), /* int width */
1067 rank_for_overload); /* int (*compar)() */
1070 if (cp->h.code & EVIL_CODE)
1073 *msgp = "ambiguous type conversion possible for `%s'";
1074 return error_mark_node;
1077 function = cp->function;
1078 fndecl = cp->u.field;
1085 *msgp = "only private and protected conversions apply";
1087 *msgp = "only private conversions apply";
1088 else if (saw_protected)
1089 *msgp = "only protected conversions apply";
1091 *msgp = "no appropriate conversion to type `%s'";
1093 return error_mark_node;
1098 if (access == access_private_node)
1099 if (! can_be_private)
1102 *msgp = TREE_PRIVATE (fndecl)
1103 ? "conversion to type `%s' is private"
1104 : "conversion to type `%s' is from private base class";
1105 return error_mark_node;
1107 if (access == access_protected_node)
1108 if (! can_be_protected)
1111 *msgp = TREE_PRIVATE (fndecl)
1112 ? "conversion to type `%s' is protected"
1113 : "conversion to type `%s' is from protected base class";
1114 return error_mark_node;
1119 /* It will convert, but we don't do anything about it yet. */
1123 fntype = TREE_TYPE (function);
1125 parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1126 parmlist, NULL_TREE, LOOKUP_NORMAL);
1128 result = build_call (function, TREE_TYPE (fntype), parmlist);
1132 /* Call this when we know (for any reason) that expr is not, in fact,
1133 zero. This routine is like convert_pointer_to, but it pays
1134 attention to which specific instance of what type we want to
1135 convert to. This routine should eventually become
1136 convert_to_pointer after all references to convert_to_pointer
1140 convert_pointer_to_real (binfo, expr)
1143 register tree intype = TREE_TYPE (expr);
1147 if (TREE_CODE (binfo) == TREE_VEC)
1148 type = BINFO_TYPE (binfo);
1149 else if (IS_AGGR_TYPE (binfo))
1159 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
1160 TYPE_VOLATILE (TREE_TYPE (intype)));
1161 ptr_type = build_pointer_type (ptr_type);
1162 if (ptr_type == TYPE_MAIN_VARIANT (intype))
1165 if (intype == error_mark_node)
1166 return error_mark_node;
1168 my_friendly_assert (!integer_zerop (expr), 191);
1170 if (TREE_CODE (type) == RECORD_TYPE
1171 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1172 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1176 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1179 /* This function shouldn't be called with unqualified arguments
1180 but if it is, give them an error message that they can read. */
1183 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1184 TREE_TYPE (intype), type);
1187 cp_error ("because `%T' is an ambiguous base class", type);
1188 return error_mark_node;
1191 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1193 rval = build1 (NOP_EXPR, ptr_type,
1194 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1195 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1199 /* Call this when we know (for any reason) that expr is
1200 not, in fact, zero. This routine gets a type out of the first
1201 argument and uses it to search for the type to convert to. If there
1202 is more than one instance of that type in the expr, the conversion is
1203 ambiguous. This routine should eventually go away, and all
1204 callers should use convert_to_pointer_real. */
1207 convert_pointer_to (binfo, expr)
1212 if (TREE_CODE (binfo) == TREE_VEC)
1213 type = BINFO_TYPE (binfo);
1214 else if (IS_AGGR_TYPE (binfo))
1218 return convert_pointer_to_real (type, expr);
1223 FLAGS indicates how we should behave. */
1226 cp_convert (type, expr, convtype, flags)
1228 int convtype, flags;
1230 register tree e = expr;
1231 register enum tree_code code = TREE_CODE (type);
1233 if (TREE_CODE (e) == ERROR_MARK
1234 || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
1235 return error_mark_node;
1237 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
1238 /* We need a new temporary; don't take this shortcut. */;
1239 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
1240 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
1241 return fold (build1 (NOP_EXPR, type, e));
1243 if (code == VOID_TYPE && (convtype & CONV_STATIC))
1244 return build1 (CONVERT_EXPR, type, e);
1247 /* This is incorrect. A truncation can't be stripped this way.
1248 Extensions will be stripped by the use of get_unwidened. */
1249 if (TREE_CODE (e) == NOP_EXPR)
1250 return convert (type, TREE_OPERAND (e, 0));
1253 /* Just convert to the type of the member. */
1254 if (code == OFFSET_TYPE)
1256 type = TREE_TYPE (type);
1257 code = TREE_CODE (type);
1261 if (code == REFERENCE_TYPE)
1262 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1263 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1264 e = convert_from_reference (e);
1267 if (TREE_CODE (e) == OFFSET_REF)
1268 e = resolve_offset_ref (e);
1270 if (TREE_READONLY_DECL_P (e))
1271 e = decl_constant_value (e);
1273 if (INTEGRAL_CODE_P (code))
1275 tree intype = TREE_TYPE (e);
1276 /* enum = enum, enum = int, enum = float are all errors. */
1277 if (flag_int_enum_equivalence == 0
1278 && TREE_CODE (type) == ENUMERAL_TYPE
1279 && ARITHMETIC_TYPE_P (intype)
1280 && ! (convtype & CONV_STATIC))
1282 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1284 if (flag_pedantic_errors)
1285 return error_mark_node;
1287 if (IS_AGGR_TYPE (intype))
1290 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1293 if (flags & LOOKUP_COMPLAIN)
1294 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1295 if (flags & LOOKUP_SPECULATIVELY)
1297 return error_mark_node;
1299 if (code == BOOLEAN_TYPE)
1301 /* Common Ada/Pascal programmer's mistake. We always warn
1302 about this since it is so bad. */
1303 if (TREE_CODE (expr) == FUNCTION_DECL)
1304 cp_warning ("the address of `%D', will always be `true'", expr);
1305 return truthvalue_conversion (e);
1307 return fold (convert_to_integer (type, e));
1309 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1310 || TYPE_PTRMEMFUNC_P (type))
1311 return fold (cp_convert_to_pointer (type, e));
1312 if (code == REAL_TYPE)
1314 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1317 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1321 if (flags & LOOKUP_COMPLAIN)
1322 cp_error ("`%#T' used where a floating point value was expected",
1325 return fold (convert_to_real (type, e));
1328 /* New C++ semantics: since assignment is now based on
1329 memberwise copying, if the rhs type is derived from the
1330 lhs type, then we may still do a conversion. */
1331 if (IS_AGGR_TYPE_CODE (code))
1333 tree dtype = TREE_TYPE (e);
1334 tree ctor = NULL_TREE;
1335 tree conversion = NULL_TREE;
1337 dtype = TYPE_MAIN_VARIANT (dtype);
1339 /* Conversion of object pointers or signature pointers/references
1340 to signature pointers/references. */
1342 if (TYPE_LANG_SPECIFIC (type)
1343 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1345 tree constructor = build_signature_pointer_constructor (type, expr);
1346 tree sig_ty = SIGNATURE_TYPE (type);
1349 if (constructor == error_mark_node)
1350 return error_mark_node;
1352 sig_ptr = get_temp_name (type, 1);
1353 DECL_INITIAL (sig_ptr) = constructor;
1354 CLEAR_SIGNATURE (sig_ty);
1355 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1356 SET_SIGNATURE (sig_ty);
1357 TREE_READONLY (sig_ptr) = 1;
1362 /* Conversion between aggregate types. New C++ semantics allow
1363 objects of derived type to be cast to objects of base type.
1364 Old semantics only allowed this between pointers.
1366 There may be some ambiguity between using a constructor
1367 vs. using a type conversion operator when both apply. */
1369 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1370 && TYPE_HAS_CONVERSION (dtype))
1371 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1373 if (conversion == error_mark_node)
1375 if (flags & LOOKUP_COMPLAIN)
1376 error ("ambiguous pointer conversion");
1380 if (TYPE_HAS_CONSTRUCTOR (complete_type (type))
1381 && (! flag_ansi_overloading || ! conversion))
1382 ctor = build_method_call (NULL_TREE, ctor_identifier,
1383 build_tree_list (NULL_TREE, e),
1385 (flags & LOOKUP_NORMAL) | LOOKUP_SPECULATIVELY
1386 | (convtype & CONV_NONCONVERTING ? 0 : LOOKUP_ONLYCONVERTING)
1387 | (flags & LOOKUP_NO_CONVERSION)
1388 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1390 if (ctor == error_mark_node)
1392 if (flags & LOOKUP_COMPLAIN)
1393 cp_error ("in conversion to type `%T'", type);
1394 if (flags & LOOKUP_SPECULATIVELY)
1396 return error_mark_node;
1399 if (conversion && ctor)
1401 if (flags & LOOKUP_COMPLAIN)
1402 error ("both constructor and type conversion operator apply");
1403 if (flags & LOOKUP_SPECULATIVELY)
1405 return error_mark_node;
1407 else if (conversion)
1411 ctor = build_cplus_new (type, ctor);
1416 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1417 then the it won't be hashed and hence compare as not equal,
1419 if (code == ARRAY_TYPE
1420 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1421 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1424 if (flags & LOOKUP_COMPLAIN)
1425 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1426 TREE_TYPE (expr), type);
1427 if (flags & LOOKUP_SPECULATIVELY)
1429 return error_mark_node;
1432 /* Create an expression whose value is that of EXPR,
1433 converted to type TYPE. The TREE_TYPE of the value
1434 is always TYPE. This function implements all reasonable
1435 conversions; callers should filter out those that are
1436 not permitted by the language being compiled. */
1439 convert (type, expr)
1442 return cp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
1445 /* Like convert, except permit conversions to take place which
1446 are not normally allowed due to access restrictions
1447 (such as conversion from sub-type to private super-type). */
1450 convert_force (type, expr, convtype)
1455 register tree e = expr;
1456 register enum tree_code code = TREE_CODE (type);
1458 if (code == REFERENCE_TYPE)
1459 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1461 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1462 e = convert_from_reference (e);
1464 if (code == POINTER_TYPE)
1465 return fold (convert_to_pointer_force (type, e));
1467 /* From typeck.c convert_for_assignment */
1468 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1469 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1470 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1471 || integer_zerop (e)
1472 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1473 && TYPE_PTRMEMFUNC_P (type))
1475 /* compatible pointer to member functions. */
1476 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1479 return cp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1482 /* Subroutine of build_type_conversion. */
1485 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1486 tree xtype, basetype;
1495 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1497 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1499 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1500 if (rval == error_mark_node)
1504 return error_mark_node;
1507 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1511 && TREE_TYPE (xtype)
1512 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1513 > TREE_READONLY (TREE_TYPE (xtype))))
1514 warning ("user-defined conversion casting away `const'");
1515 return convert (xtype, rval);
1518 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1519 exists, return the attempted conversion. This may
1520 return ERROR_MARK_NODE if the conversion is not
1521 allowed (references private members, etc).
1522 If no conversion exists, NULL_TREE is returned.
1524 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1525 to take place immediately. Otherwise, we build a SAVE_EXPR
1526 which can be evaluated if the results are ever needed.
1528 Changes to this functions should be mirrored in user_harshness.
1530 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1531 object parameter, or by the second standard conversion sequence if
1532 that doesn't do it. This will probably wait for an overloading rewrite.
1536 build_type_conversion (code, xtype, expr, for_sure)
1537 enum tree_code code;
1541 /* C++: check to see if we can convert this aggregate type
1542 into the required type. */
1545 tree winner = NULL_TREE;
1547 if (flag_ansi_overloading)
1548 return build_user_type_conversion
1549 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1551 if (expr == error_mark_node)
1552 return error_mark_node;
1554 basetype = TREE_TYPE (expr);
1555 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1556 basetype = TREE_TYPE (basetype);
1558 basetype = TYPE_MAIN_VARIANT (basetype);
1559 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1562 /* Do we have an exact match? */
1564 tree typename = build_typename_overload (xtype);
1565 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1566 return build_type_conversion_1 (xtype, basetype, expr, typename,
1570 /* Nope; try looking for others. */
1571 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1573 tree cand = TREE_VALUE (conv);
1575 if (winner && winner == cand)
1578 if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1584 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1586 cp_error (" candidate conversions include `%D' and `%D'",
1597 return build_type_conversion_1 (xtype, basetype, expr,
1598 DECL_NAME (winner), for_sure);
1603 /* Convert the given EXPR to one of a group of types suitable for use in an
1604 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1605 which indicates which types are suitable. If COMPLAIN is 1, complain
1606 about ambiguity; otherwise, the caller will deal with it. */
1609 build_expr_type_conversion (desires, expr, complain)
1614 tree basetype = TREE_TYPE (expr);
1616 tree winner = NULL_TREE;
1618 if (TREE_CODE (basetype) == OFFSET_TYPE)
1619 expr = resolve_offset_ref (expr);
1620 expr = convert_from_reference (expr);
1621 basetype = TREE_TYPE (expr);
1623 if (! IS_AGGR_TYPE (basetype))
1624 switch (TREE_CODE (basetype))
1627 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1628 && integer_zerop (expr))
1630 /* else fall through... */
1633 return (desires & WANT_INT) ? expr : NULL_TREE;
1635 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1637 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1639 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1643 return (desires & WANT_POINTER) ? default_conversion (expr)
1649 if (! TYPE_HAS_CONVERSION (basetype))
1652 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1656 tree cand = TREE_VALUE (conv);
1658 if (winner && winner == cand)
1661 candidate = TREE_TYPE (TREE_TYPE (cand));
1662 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1663 candidate = TREE_TYPE (candidate);
1665 switch (TREE_CODE (candidate))
1669 win = (desires & WANT_INT); break;
1671 win = (desires & WANT_ENUM); break;
1673 win = (desires & WANT_FLOAT); break;
1675 win = (desires & WANT_POINTER); break;
1684 cp_error ("ambiguous default type conversion from `%T'",
1686 cp_error (" candidate conversions include `%D' and `%D'",
1689 return error_mark_node;
1698 tree type = TREE_TYPE (TREE_TYPE (winner));
1699 if (TREE_CODE (type) == REFERENCE_TYPE)
1700 type = TREE_TYPE (type);
1701 return build_type_conversion_1 (type, basetype, expr,
1702 DECL_NAME (winner), 1);
1708 /* Must convert two aggregate types to non-aggregate type.
1709 Attempts to find a non-ambiguous, "best" type conversion.
1711 Return 1 on success, 0 on failure.
1713 @@ What are the real semantics of this supposed to be??? */
1716 build_default_binary_type_conversion (code, arg1, arg2)
1717 enum tree_code code;
1723 case TRUNC_DIV_EXPR:
1725 case FLOOR_DIV_EXPR:
1726 case ROUND_DIV_EXPR:
1727 case EXACT_DIV_EXPR:
1728 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1729 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1732 case TRUNC_MOD_EXPR:
1733 case FLOOR_MOD_EXPR:
1739 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1740 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1745 tree a1, a2, p1, p2;
1748 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1749 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1750 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1751 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1753 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1756 error ("ambiguous default type conversion for `operator +'");
1759 *arg1 = a1, *arg2 = a2;
1761 *arg1 = a1, *arg2 = p2;
1763 *arg1 = p1, *arg2 = a2;
1769 tree a1, a2, p1, p2;
1772 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1773 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1774 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1775 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1777 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1780 error ("ambiguous default type conversion for `operator -'");
1783 *arg1 = a1, *arg2 = a2;
1785 *arg1 = p1, *arg2 = p2;
1787 *arg1 = p1, *arg2 = a2;
1798 tree a1, a2, p1, p2;
1801 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1802 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1803 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1804 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1806 wins = (a1 && a2) + (p1 && p2);
1809 cp_error ("ambiguous default type conversion for `%O'", code);
1812 *arg1 = a1, *arg2 = a2;
1814 *arg1 = p1, *arg2 = p2;
1818 case TRUTH_ANDIF_EXPR:
1819 case TRUTH_ORIF_EXPR:
1820 *arg1 = convert (boolean_type_node, *arg1);
1821 *arg2 = convert (boolean_type_node, *arg2);
1829 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1830 cp_error ("ambiguous default type conversion for `%O'", code);
1838 /* Implements integral promotion (4.1) and float->double promotion. */
1841 type_promotes_to (type)
1844 int constp, volatilep;
1846 if (type == error_mark_node)
1847 return error_mark_node;
1849 constp = TYPE_READONLY (type);
1850 volatilep = TYPE_VOLATILE (type);
1851 type = TYPE_MAIN_VARIANT (type);
1853 /* bool always promotes to int (not unsigned), even if it's the same
1855 if (type == boolean_type_node)
1856 type = integer_type_node;
1858 /* Normally convert enums to int, but convert wide enums to something
1860 else if (TREE_CODE (type) == ENUMERAL_TYPE
1861 || type == wchar_type_node)
1863 int precision = MAX (TYPE_PRECISION (type),
1864 TYPE_PRECISION (integer_type_node));
1865 tree totype = type_for_size (precision, 0);
1866 if (TREE_UNSIGNED (type)
1867 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1868 type = type_for_size (precision, 1);
1872 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1874 /* Retain unsignedness if really not getting bigger. */
1875 if (TREE_UNSIGNED (type)
1876 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1877 type = unsigned_type_node;
1879 type = integer_type_node;
1881 else if (type == float_type_node)
1882 type = double_type_node;
1884 return cp_build_type_variant (type, constp, volatilep);