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 temporarys are generated.
375 CHECKCONST controls if we report error messages on const subversion. */
378 build_up_reference (type, arg, flags, checkconst)
380 int flags, checkconst;
383 int literal_flag = 0;
384 tree argtype = TREE_TYPE (arg);
385 tree target_type = TREE_TYPE (type);
386 tree binfo = NULL_TREE;
388 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
389 if ((flags & LOOKUP_PROTECT)
390 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
391 && IS_AGGR_TYPE (argtype)
392 && IS_AGGR_TYPE (target_type))
394 binfo = get_binfo (target_type, argtype, 1);
395 if (binfo == error_mark_node)
396 return error_mark_node;
397 if (binfo == NULL_TREE)
398 return error_not_base_type (target_type, argtype);
401 /* Pass along const and volatile down into the type. */
402 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
403 target_type = cp_build_type_variant (target_type, TYPE_READONLY (type),
404 TYPE_VOLATILE (type));
406 if (TREE_CODE (targ) == SAVE_EXPR)
407 targ = TREE_OPERAND (targ, 0);
408 while (TREE_CODE (targ) == NOP_EXPR
409 && (TYPE_MAIN_VARIANT (argtype)
410 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0)))))
411 targ = TREE_OPERAND (targ, 0);
413 switch (TREE_CODE (targ))
416 /* This is a call to a constructor which did not know what it was
417 initializing until now: it needs to initialize a temporary. */
418 if (TREE_HAS_CONSTRUCTOR (targ))
420 tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0));
421 TREE_HAS_CONSTRUCTOR (targ) = 0;
422 return build_up_reference (type, temp, flags, 1);
424 /* Let &* cancel out to simplify resulting code.
425 Also, throw away intervening NOP_EXPRs. */
426 arg = TREE_OPERAND (targ, 0);
427 if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
428 || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
429 arg = TREE_OPERAND (arg, 0);
431 /* in doing a &*, we have to get rid of the const'ness on the pointer
432 value. Haven't thought about volatile here. Pointers come to mind
434 if (TREE_READONLY (arg))
436 arg = copy_node (arg);
437 TREE_READONLY (arg) = 0;
440 rval = build1 (CONVERT_EXPR, type, arg);
441 TREE_REFERENCE_EXPR (rval) = 1;
443 /* propagate the const flag on something like:
450 class Derived : public Base {
457 void func2(const Derived& d) {
461 on the d parameter. The below could have been avoided, if the flags
462 were down in the tree, not sure why they are not. (mrs) */
463 /* The below code may have to be propagated to other parts of this
465 if (TREE_READONLY (targ) && !TREE_READONLY (arg)
466 && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
467 && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
468 && (TYPE_READONLY (target_type) && checkconst))
470 arg = copy_node (arg);
471 TREE_READONLY (arg) = TREE_READONLY (targ);
473 literal_flag = TREE_CONSTANT (arg);
477 /* Get this out of a register if we happened to be in one by accident.
478 Also, build up references to non-lvalues it we must. */
479 /* For &x[y], return (&) x+y */
481 if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
482 return error_mark_node;
483 rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
484 TREE_OPERAND (targ, 1), 1);
485 TREE_TYPE (rval) = type;
486 if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
487 && staticp (TREE_OPERAND (targ, 0)))
488 TREE_CONSTANT (rval) = 1;
492 /* Could be a reference to a static member. */
494 tree field = TREE_OPERAND (targ, 1);
495 if (TREE_STATIC (field))
497 rval = build1 (ADDR_EXPR, type, field);
503 /* We should have farmed out member pointers above. */
504 my_friendly_abort (188);
507 rval = build_component_addr (targ, build_pointer_type (argtype),
508 "attempt to make a reference to bit-field structure member `%s'");
509 TREE_TYPE (rval) = type;
510 literal_flag = staticp (TREE_OPERAND (targ, 0));
514 /* Anything not already handled and not a true memory reference
515 needs to have a reference built up. Do so silently for
516 things like integers and return values from function,
517 but complain if we need a reference to something declared
521 /* 'this' is not an lvalue. */
522 if (targ == current_class_ptr && ! flag_this_is_variable)
533 mark_addressable (targ);
538 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
540 rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
541 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
545 case PREINCREMENT_EXPR:
546 case PREDECREMENT_EXPR:
550 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
552 rval = build (COMPOUND_EXPR, type, arg, real_reference);
553 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
558 return build (COND_EXPR, type,
559 TREE_OPERAND (targ, 0),
560 build_up_reference (type, TREE_OPERAND (targ, 1),
562 build_up_reference (type, TREE_OPERAND (targ, 2),
565 /* Undo the folding... */
568 return build (COND_EXPR, type,
569 build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR,
570 boolean_type_node, TREE_OPERAND (targ, 0),
571 TREE_OPERAND (targ, 1)),
572 build_up_reference (type, TREE_OPERAND (targ, 0),
574 build_up_reference (type, TREE_OPERAND (targ, 1),
578 arg = TREE_OPERAND (targ, 1);
579 if (arg == NULL_TREE)
581 compiler_error ("({ ... }) expression not expanded when needed for reference");
582 return error_mark_node;
584 rval = build1 (ADDR_EXPR, type, arg);
585 TREE_REFERENCE_EXPR (rval) = 1;
592 if ((flags & DIRECT_BIND)
593 && ! real_lvalue_p (targ))
595 if (toplevel_bindings_p ())
597 arg = get_temp_name (argtype, 1);
602 arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
603 DECL_ARTIFICIAL (arg) = 1;
605 DECL_INITIAL (arg) = targ;
606 cp_finish_decl (arg, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
608 else if (TREE_ADDRESSABLE (targ) == 0 && !(flags & DIRECT_BIND))
610 tree slot = build_decl (VAR_DECL, NULL_TREE, argtype);
611 arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
614 /* If we had a way to wrap this up, and say, if we ever needed it's
615 address, transform all occurrences of the register, into a memory
616 reference we could win better. */
617 mark_addressable (arg);
618 rval = build1 (ADDR_EXPR, type, arg);
621 if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
622 || TYPE_USES_COMPLEX_INHERITANCE (target_type))
624 TREE_TYPE (rval) = build_pointer_type (argtype);
625 if (flags & LOOKUP_PROTECT)
626 rval = convert_pointer_to (target_type, rval);
629 = convert_to_pointer_force (build_pointer_type (target_type), rval);
630 TREE_TYPE (rval) = type;
631 if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR)
632 TREE_TYPE (TREE_OPERAND (rval, 0))
633 = TREE_TYPE (TREE_OPERAND (rval, 1)) = type;
635 TREE_CONSTANT (rval) = literal_flag;
639 /* For C++: Only need to do one-level references, but cannot
640 get tripped up on signed/unsigned differences.
642 DECL is either NULL_TREE or the _DECL node for a reference that is being
643 initialized. It can be error_mark_node if we don't know the _DECL but
644 we know it's an initialization. */
647 convert_to_reference (reftype, expr, convtype, flags, decl)
652 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
653 register tree intype = TREE_TYPE (expr);
654 tree rval = NULL_TREE;
655 tree rval_as_conversion = NULL_TREE;
658 if (TREE_CODE (intype) == REFERENCE_TYPE)
659 my_friendly_abort (364);
661 intype = TYPE_MAIN_VARIANT (intype);
663 i = comp_target_types (type, intype, 0);
665 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
666 && ! (flags & LOOKUP_NO_CONVERSION))
668 /* Look for a user-defined conversion to lvalue that we can use. */
670 if (flag_ansi_overloading)
672 = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
674 rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
676 if (rval_as_conversion && rval_as_conversion != error_mark_node
677 && real_lvalue_p (rval_as_conversion))
679 expr = rval_as_conversion;
680 rval_as_conversion = NULL_TREE;
686 if (((convtype & CONV_STATIC) && i == -1)
687 || ((convtype & CONV_IMPLICIT) && i == 1))
689 if (flags & LOOKUP_COMPLAIN)
691 tree ttl = TREE_TYPE (reftype);
695 int r = TREE_READONLY (expr);
696 int v = TREE_THIS_VOLATILE (expr);
697 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
700 if (! real_lvalue_p (expr) && ! TYPE_READONLY (ttl))
703 /* Ensure semantics of [dcl.init.ref] */
704 cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'",
707 cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'",
710 else if (! (convtype & CONV_CONST))
712 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
713 cp_pedwarn ("conversion from `%T' to `%T' discards const",
715 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
716 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
721 return build_up_reference (reftype, expr, flags,
722 ! (convtype & CONV_CONST));
724 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
726 /* When casting an lvalue to a reference type, just convert into
727 a pointer to the new type and deference it. This is allowed
728 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
729 should be done directly (jason). (int &)ri ---> *(int*)&ri */
731 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
733 if (TREE_CODE (intype) == POINTER_TYPE
734 && (comptypes (TREE_TYPE (intype), type, -1)))
735 cp_warning ("casting `%T' to `%T' does not dereference pointer",
738 rval = build_unary_op (ADDR_EXPR, expr, 0);
739 if (rval != error_mark_node)
740 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
741 if (rval != error_mark_node)
742 rval = build1 (NOP_EXPR, reftype, rval);
744 else if (flag_ansi_overloading)
746 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
748 if (rval == error_mark_node)
749 return error_mark_node;
750 rval = build_up_reference (reftype, rval, flags, 1);
752 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
753 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
758 tree rval_as_ctor = NULL_TREE;
760 if (rval_as_conversion)
762 if (rval_as_conversion == error_mark_node)
764 cp_error ("conversion from `%T' to `%T' is ambiguous",
766 return error_mark_node;
768 rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
772 /* Definitely need to go through a constructor here. */
773 if (TYPE_HAS_CONSTRUCTOR (type)
774 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
775 && (rval = build_method_call
776 (NULL_TREE, ctor_identifier,
777 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
778 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
779 | LOOKUP_ONLYCONVERTING)))
783 if (toplevel_bindings_p ())
785 tree t = get_temp_name (type, toplevel_bindings_p ());
786 init = build_method_call (t, ctor_identifier,
787 build_tree_list (NULL_TREE, expr),
789 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
790 | LOOKUP_ONLYCONVERTING);
792 if (init == error_mark_node)
793 return error_mark_node;
795 make_decl_rtl (t, NULL_PTR, 1);
796 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
797 rval = build_unary_op (ADDR_EXPR, t, 0);
801 init = build_method_call (NULL_TREE, ctor_identifier,
802 build_tree_list (NULL_TREE, expr),
804 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
805 |LOOKUP_ONLYCONVERTING);
807 if (init == error_mark_node)
808 return error_mark_node;
810 rval = build_cplus_new (type, init);
811 rval = build_up_reference (reftype, rval, flags, 1);
816 if (rval_as_ctor && rval_as_conversion)
818 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
820 return error_mark_node;
822 else if (rval_as_ctor)
824 else if (rval_as_conversion)
825 rval = rval_as_conversion;
826 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
828 rval = convert (type, expr);
829 if (rval == error_mark_node)
830 return error_mark_node;
832 rval = build_up_reference (reftype, rval, flags, 1);
835 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
836 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
842 /* If we found a way to convert earlier, then use it. */
846 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
848 if (flags & LOOKUP_COMPLAIN)
849 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
851 if (flags & LOOKUP_SPECULATIVELY)
854 return error_mark_node;
857 /* We are using a reference VAL for its value. Bash that reference all the
858 way down to its lowest form. */
861 convert_from_reference (val)
864 tree type = TREE_TYPE (val);
866 if (TREE_CODE (type) == OFFSET_TYPE)
867 type = TREE_TYPE (type);
868 if (TREE_CODE (type) == REFERENCE_TYPE)
869 return build_indirect_ref (val, NULL_PTR);
873 /* See if there is a constructor of type TYPE which will convert
874 EXPR. The reference manual seems to suggest (8.5.6) that we need
875 not worry about finding constructors for base classes, then converting
876 to the derived class.
878 MSGP is a pointer to a message that would be an appropriate error
879 string. If MSGP is NULL, then we are not interested in reporting
883 convert_to_aggr (type, expr, msgp, protect)
888 tree basetype = type;
889 tree name = TYPE_IDENTIFIER (basetype);
890 tree function, fndecl, fntype, parmtypes, parmlist, result;
892 /* See code below that used this. */
896 int can_be_private, can_be_protected;
898 if (! TYPE_HAS_CONSTRUCTOR (basetype))
901 *msgp = "type `%s' does not have a constructor";
902 return error_mark_node;
905 access = access_public_node;
907 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
909 parmlist = build_tree_list (NULL_TREE, expr);
910 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
912 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
914 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
915 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
918 /* The type of the first argument will be filled in inside the loop. */
919 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
920 parmtypes = tree_cons (NULL_TREE, build_pointer_type (basetype), parmtypes);
922 /* No exact conversion was found. See if an approximate
924 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
928 int saw_protected = 0;
929 struct candidate *candidates =
930 (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
931 struct candidate *cp = candidates;
937 cp->harshness = (struct harshness_code *)
938 alloca (3 * sizeof (struct harshness_code));
940 compute_conversion_costs (fndecl, parmlist, cp, 2);
941 if ((cp->h.code & EVIL_CODE) == 0)
943 cp->u.field = fndecl;
946 if (TREE_PRIVATE (fndecl))
947 access = access_private_node;
948 else if (TREE_PROTECTED (fndecl))
949 access = access_protected_node;
951 access = access_public_node;
954 access = access_public_node;
956 if (access == access_private_node
957 ? (basetype == current_class_type
958 || is_friend (basetype, cp->function)
959 || purpose_member (basetype, DECL_ACCESS (fndecl)))
960 : access == access_protected_node
962 || purpose_member (basetype, DECL_ACCESS (fndecl)))
965 if (cp->h.code <= TRIVIAL_CODE)
971 if (access == access_private_node)
977 fndecl = DECL_CHAIN (fndecl);
981 /* Rank from worst to best. Then cp will point to best one.
982 Private fields have their bits flipped. For unsigned
983 numbers, this should make them look very large.
984 If the best alternate has a (signed) negative value,
985 then all we ever saw were private members. */
986 if (cp - candidates > 1)
987 qsort (candidates, /* char *base */
988 cp - candidates, /* int nel */
989 sizeof (struct candidate), /* int width */
990 rank_for_overload); /* int (*compar)() */
993 if (cp->h.code & EVIL_CODE)
996 *msgp = "ambiguous type conversion possible for `%s'";
997 return error_mark_node;
1000 function = cp->function;
1001 fndecl = cp->u.field;
1008 *msgp = "only private and protected conversions apply";
1010 *msgp = "only private conversions apply";
1011 else if (saw_protected)
1012 *msgp = "only protected conversions apply";
1014 *msgp = "no appropriate conversion to type `%s'";
1016 return error_mark_node;
1021 if (access == access_private_node)
1022 if (! can_be_private)
1025 *msgp = TREE_PRIVATE (fndecl)
1026 ? "conversion to type `%s' is private"
1027 : "conversion to type `%s' is from private base class";
1028 return error_mark_node;
1030 if (access == access_protected_node)
1031 if (! can_be_protected)
1034 *msgp = TREE_PRIVATE (fndecl)
1035 ? "conversion to type `%s' is protected"
1036 : "conversion to type `%s' is from protected base class";
1037 return error_mark_node;
1042 /* It will convert, but we don't do anything about it yet. */
1046 fntype = TREE_TYPE (function);
1048 parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1049 parmlist, NULL_TREE, LOOKUP_NORMAL);
1051 result = build_call (function, TREE_TYPE (fntype), parmlist);
1055 /* Call this when we know (for any reason) that expr is not, in fact,
1056 zero. This routine is like convert_pointer_to, but it pays
1057 attention to which specific instance of what type we want to
1058 convert to. This routine should eventually become
1059 convert_to_pointer after all references to convert_to_pointer
1063 convert_pointer_to_real (binfo, expr)
1066 register tree intype = TREE_TYPE (expr);
1070 if (TREE_CODE (binfo) == TREE_VEC)
1071 type = BINFO_TYPE (binfo);
1072 else if (IS_AGGR_TYPE (binfo))
1082 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
1083 TYPE_VOLATILE (TREE_TYPE (intype)));
1084 ptr_type = build_pointer_type (ptr_type);
1085 if (ptr_type == TYPE_MAIN_VARIANT (intype))
1088 if (intype == error_mark_node)
1089 return error_mark_node;
1091 my_friendly_assert (!integer_zerop (expr), 191);
1093 if (TREE_CODE (type) == RECORD_TYPE
1094 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1095 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1099 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1102 /* This function shouldn't be called with unqualified arguments
1103 but if it is, give them an error message that they can read. */
1106 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1107 TREE_TYPE (intype), type);
1110 cp_error ("because `%T' is an ambiguous base class", type);
1111 return error_mark_node;
1114 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1116 rval = build1 (NOP_EXPR, ptr_type,
1117 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1118 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1122 /* Call this when we know (for any reason) that expr is
1123 not, in fact, zero. This routine gets a type out of the first
1124 argument and uses it to search for the type to convert to. If there
1125 is more than one instance of that type in the expr, the conversion is
1126 ambiguous. This routine should eventually go away, and all
1127 callers should use convert_to_pointer_real. */
1130 convert_pointer_to (binfo, expr)
1135 if (TREE_CODE (binfo) == TREE_VEC)
1136 type = BINFO_TYPE (binfo);
1137 else if (IS_AGGR_TYPE (binfo))
1141 return convert_pointer_to_real (type, expr);
1146 FLAGS indicates how we should behave. */
1149 cp_convert (type, expr, convtype, flags)
1151 int convtype, flags;
1153 register tree e = expr;
1154 register enum tree_code code = TREE_CODE (type);
1156 if (TREE_CODE (e) == ERROR_MARK
1157 || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
1158 return error_mark_node;
1160 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
1161 /* We need a new temporary; don't take this shortcut. */;
1162 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
1163 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
1164 return fold (build1 (NOP_EXPR, type, e));
1166 if (code == VOID_TYPE && (convtype & CONV_STATIC))
1167 return build1 (CONVERT_EXPR, type, e);
1170 /* This is incorrect. A truncation can't be stripped this way.
1171 Extensions will be stripped by the use of get_unwidened. */
1172 if (TREE_CODE (e) == NOP_EXPR)
1173 return convert (type, TREE_OPERAND (e, 0));
1176 /* Just convert to the type of the member. */
1177 if (code == OFFSET_TYPE)
1179 type = TREE_TYPE (type);
1180 code = TREE_CODE (type);
1184 if (code == REFERENCE_TYPE)
1185 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1186 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1187 e = convert_from_reference (e);
1190 if (TREE_CODE (e) == OFFSET_REF)
1191 e = resolve_offset_ref (e);
1193 if (TREE_READONLY_DECL_P (e))
1194 e = decl_constant_value (e);
1196 if (INTEGRAL_CODE_P (code))
1198 tree intype = TREE_TYPE (e);
1199 /* enum = enum, enum = int, enum = float, (enum)pointer are all
1201 if (flag_int_enum_equivalence == 0
1202 && TREE_CODE (type) == ENUMERAL_TYPE
1203 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
1204 || (TREE_CODE (intype) == POINTER_TYPE)))
1206 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1208 if (flag_pedantic_errors)
1209 return error_mark_node;
1211 if (IS_AGGR_TYPE (intype))
1214 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1217 if (flags & LOOKUP_COMPLAIN)
1218 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1219 if (flags & LOOKUP_SPECULATIVELY)
1221 return error_mark_node;
1223 if (code == BOOLEAN_TYPE)
1225 /* Common Ada/Pascal programmer's mistake. We always warn
1226 about this since it is so bad. */
1227 if (TREE_CODE (expr) == FUNCTION_DECL)
1228 cp_warning ("the address of `%D', will always be `true'", expr);
1229 return truthvalue_conversion (e);
1231 return fold (convert_to_integer (type, e));
1233 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1234 || TYPE_PTRMEMFUNC_P (type))
1235 return fold (cp_convert_to_pointer (type, e));
1236 if (code == REAL_TYPE)
1238 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1241 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1245 if (flags & LOOKUP_COMPLAIN)
1246 cp_error ("`%#T' used where a floating point value was expected",
1249 return fold (convert_to_real (type, e));
1252 /* New C++ semantics: since assignment is now based on
1253 memberwise copying, if the rhs type is derived from the
1254 lhs type, then we may still do a conversion. */
1255 if (IS_AGGR_TYPE_CODE (code))
1257 tree dtype = TREE_TYPE (e);
1258 tree ctor = NULL_TREE;
1259 tree conversion = NULL_TREE;
1261 dtype = TYPE_MAIN_VARIANT (dtype);
1263 /* Conversion of object pointers or signature pointers/references
1264 to signature pointers/references. */
1266 if (TYPE_LANG_SPECIFIC (type)
1267 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1269 tree constructor = build_signature_pointer_constructor (type, expr);
1270 tree sig_ty = SIGNATURE_TYPE (type);
1273 if (constructor == error_mark_node)
1274 return error_mark_node;
1276 sig_ptr = get_temp_name (type, 1);
1277 DECL_INITIAL (sig_ptr) = constructor;
1278 CLEAR_SIGNATURE (sig_ty);
1279 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1280 SET_SIGNATURE (sig_ty);
1281 TREE_READONLY (sig_ptr) = 1;
1286 /* Conversion between aggregate types. New C++ semantics allow
1287 objects of derived type to be cast to objects of base type.
1288 Old semantics only allowed this between pointers.
1290 There may be some ambiguity between using a constructor
1291 vs. using a type conversion operator when both apply. */
1293 if (flag_ansi_overloading)
1297 if ((flags & LOOKUP_ONLYCONVERTING)
1298 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
1299 ctor = build_user_type_conversion (type, ctor, flags);
1301 ctor = build_method_call (NULL_TREE, ctor_identifier,
1302 build_tree_list (NULL_TREE, ctor),
1303 TYPE_BINFO (type), flags);
1305 return build_cplus_new (type, ctor);
1309 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1310 && TYPE_HAS_CONVERSION (dtype))
1311 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1313 if (conversion == error_mark_node)
1315 if (flags & LOOKUP_COMPLAIN)
1316 error ("ambiguous pointer conversion");
1320 if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1321 ctor = build_method_call (NULL_TREE, ctor_identifier,
1322 build_tree_list (NULL_TREE, e),
1324 (flags & LOOKUP_NORMAL)
1325 | LOOKUP_SPECULATIVELY
1326 | (flags & LOOKUP_ONLYCONVERTING)
1327 | (flags & LOOKUP_NO_CONVERSION)
1328 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1330 if (ctor == error_mark_node)
1332 if (flags & LOOKUP_COMPLAIN)
1333 cp_error ("in conversion to type `%T'", type);
1334 if (flags & LOOKUP_SPECULATIVELY)
1336 return error_mark_node;
1339 if (conversion && ctor)
1341 if (flags & LOOKUP_COMPLAIN)
1342 error ("both constructor and type conversion operator apply");
1343 if (flags & LOOKUP_SPECULATIVELY)
1345 return error_mark_node;
1347 else if (conversion)
1351 ctor = build_cplus_new (type, ctor);
1357 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1358 then the it won't be hashed and hence compare as not equal,
1360 if (code == ARRAY_TYPE
1361 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1362 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1365 if (flags & LOOKUP_COMPLAIN)
1366 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1367 TREE_TYPE (expr), type);
1368 if (flags & LOOKUP_SPECULATIVELY)
1370 return error_mark_node;
1373 /* Create an expression whose value is that of EXPR,
1374 converted to type TYPE. The TREE_TYPE of the value
1375 is always TYPE. This function implements all reasonable
1376 conversions; callers should filter out those that are
1377 not permitted by the language being compiled. */
1380 convert (type, expr)
1383 return cp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
1386 /* Like convert, except permit conversions to take place which
1387 are not normally allowed due to access restrictions
1388 (such as conversion from sub-type to private super-type). */
1391 convert_force (type, expr, convtype)
1396 register tree e = expr;
1397 register enum tree_code code = TREE_CODE (type);
1399 if (code == REFERENCE_TYPE)
1400 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1402 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1403 e = convert_from_reference (e);
1405 if (code == POINTER_TYPE)
1406 return fold (convert_to_pointer_force (type, e));
1408 /* From typeck.c convert_for_assignment */
1409 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1410 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1411 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1412 || integer_zerop (e)
1413 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1414 && TYPE_PTRMEMFUNC_P (type))
1416 /* compatible pointer to member functions. */
1417 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1420 return cp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1423 /* Subroutine of build_type_conversion. */
1426 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1427 tree xtype, basetype;
1436 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1438 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1440 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1441 if (rval == error_mark_node)
1445 return error_mark_node;
1448 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1452 && TREE_TYPE (xtype)
1453 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1454 > TREE_READONLY (TREE_TYPE (xtype))))
1455 warning ("user-defined conversion casting away `const'");
1456 return convert (xtype, rval);
1459 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1460 exists, return the attempted conversion. This may
1461 return ERROR_MARK_NODE if the conversion is not
1462 allowed (references private members, etc).
1463 If no conversion exists, NULL_TREE is returned.
1465 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1466 to take place immediately. Otherwise, we build a SAVE_EXPR
1467 which can be evaluated if the results are ever needed.
1469 Changes to this functions should be mirrored in user_harshness.
1471 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1472 object parameter, or by the second standard conversion sequence if
1473 that doesn't do it. This will probably wait for an overloading rewrite.
1477 build_type_conversion (code, xtype, expr, for_sure)
1478 enum tree_code code;
1482 /* C++: check to see if we can convert this aggregate type
1483 into the required type. */
1486 tree winner = NULL_TREE;
1488 if (flag_ansi_overloading)
1489 return build_user_type_conversion
1490 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1492 if (expr == error_mark_node)
1493 return error_mark_node;
1495 basetype = TREE_TYPE (expr);
1496 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1497 basetype = TREE_TYPE (basetype);
1499 basetype = TYPE_MAIN_VARIANT (basetype);
1500 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1503 /* Do we have an exact match? */
1505 tree typename = build_typename_overload (xtype);
1506 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1507 return build_type_conversion_1 (xtype, basetype, expr, typename,
1511 /* Nope; try looking for others. */
1512 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1514 tree cand = TREE_VALUE (conv);
1516 if (winner && winner == cand)
1519 if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1525 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1527 cp_error (" candidate conversions include `%D' and `%D'",
1538 return build_type_conversion_1 (xtype, basetype, expr,
1539 DECL_NAME (winner), for_sure);
1544 /* Convert the given EXPR to one of a group of types suitable for use in an
1545 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1546 which indicates which types are suitable. If COMPLAIN is 1, complain
1547 about ambiguity; otherwise, the caller will deal with it. */
1550 build_expr_type_conversion (desires, expr, complain)
1555 tree basetype = TREE_TYPE (expr);
1557 tree winner = NULL_TREE;
1559 if (TREE_CODE (basetype) == OFFSET_TYPE)
1560 expr = resolve_offset_ref (expr);
1561 expr = convert_from_reference (expr);
1562 basetype = TREE_TYPE (expr);
1564 if (! IS_AGGR_TYPE (basetype))
1565 switch (TREE_CODE (basetype))
1568 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1569 && integer_zerop (expr))
1571 /* else fall through... */
1574 return (desires & WANT_INT) ? expr : NULL_TREE;
1576 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1578 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1580 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1584 return (desires & WANT_POINTER) ? default_conversion (expr)
1590 if (! TYPE_HAS_CONVERSION (basetype))
1593 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1597 tree cand = TREE_VALUE (conv);
1599 if (winner && winner == cand)
1602 candidate = TREE_TYPE (TREE_TYPE (cand));
1603 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1604 candidate = TREE_TYPE (candidate);
1606 switch (TREE_CODE (candidate))
1610 win = (desires & WANT_INT); break;
1612 win = (desires & WANT_ENUM); break;
1614 win = (desires & WANT_FLOAT); break;
1616 win = (desires & WANT_POINTER); break;
1625 cp_error ("ambiguous default type conversion from `%T'",
1627 cp_error (" candidate conversions include `%D' and `%D'",
1630 return error_mark_node;
1639 tree type = TREE_TYPE (TREE_TYPE (winner));
1640 if (TREE_CODE (type) == REFERENCE_TYPE)
1641 type = TREE_TYPE (type);
1642 return build_type_conversion_1 (type, basetype, expr,
1643 DECL_NAME (winner), 1);
1649 /* Must convert two aggregate types to non-aggregate type.
1650 Attempts to find a non-ambiguous, "best" type conversion.
1652 Return 1 on success, 0 on failure.
1654 @@ What are the real semantics of this supposed to be??? */
1657 build_default_binary_type_conversion (code, arg1, arg2)
1658 enum tree_code code;
1664 case TRUNC_DIV_EXPR:
1666 case FLOOR_DIV_EXPR:
1667 case ROUND_DIV_EXPR:
1668 case EXACT_DIV_EXPR:
1669 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1670 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1673 case TRUNC_MOD_EXPR:
1674 case FLOOR_MOD_EXPR:
1680 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1681 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1686 tree a1, a2, p1, p2;
1689 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1690 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1691 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1692 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1694 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1697 error ("ambiguous default type conversion for `operator +'");
1700 *arg1 = a1, *arg2 = a2;
1702 *arg1 = a1, *arg2 = p2;
1704 *arg1 = p1, *arg2 = a2;
1710 tree a1, a2, p1, p2;
1713 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1714 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1715 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1716 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1718 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1721 error ("ambiguous default type conversion for `operator -'");
1724 *arg1 = a1, *arg2 = a2;
1726 *arg1 = p1, *arg2 = p2;
1728 *arg1 = p1, *arg2 = a2;
1739 tree a1, a2, p1, p2;
1742 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1743 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1744 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1745 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1747 wins = (a1 && a2) + (p1 && p2);
1750 cp_error ("ambiguous default type conversion for `%O'", code);
1753 *arg1 = a1, *arg2 = a2;
1755 *arg1 = p1, *arg2 = p2;
1759 case TRUTH_ANDIF_EXPR:
1760 case TRUTH_ORIF_EXPR:
1761 *arg1 = convert (boolean_type_node, *arg1);
1762 *arg2 = convert (boolean_type_node, *arg2);
1770 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1771 cp_error ("ambiguous default type conversion for `%O'", code);
1779 /* Implements integral promotion (4.1) and float->double promotion. */
1782 type_promotes_to (type)
1785 int constp, volatilep;
1787 if (type == error_mark_node)
1788 return error_mark_node;
1790 constp = TYPE_READONLY (type);
1791 volatilep = TYPE_VOLATILE (type);
1792 type = TYPE_MAIN_VARIANT (type);
1794 /* bool always promotes to int (not unsigned), even if it's the same
1796 if (type == boolean_type_node)
1797 type = integer_type_node;
1799 /* Normally convert enums to int, but convert wide enums to something
1801 else if (TREE_CODE (type) == ENUMERAL_TYPE
1802 || type == wchar_type_node)
1804 int precision = MAX (TYPE_PRECISION (type),
1805 TYPE_PRECISION (integer_type_node));
1806 tree totype = type_for_size (precision, 0);
1807 if (TREE_UNSIGNED (type)
1808 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1809 type = type_for_size (precision, 1);
1813 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1815 /* Retain unsignedness if really not getting bigger. */
1816 if (TREE_UNSIGNED (type)
1817 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1818 type = unsigned_type_node;
1820 type = integer_type_node;
1822 else if (type == float_type_node)
1823 type = double_type_node;
1825 return cp_build_type_variant (type, constp, volatilep);