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 /* Change of width--truncation and extension of integers or reals--
39 is represented with NOP_EXPR. Proper functioning of many things
40 assumes that no other conversions can be NOP_EXPRs.
42 Conversion between integer and pointer is represented with CONVERT_EXPR.
43 Converting integer to real uses FLOAT_EXPR
44 and real to integer uses FIX_TRUNC_EXPR.
46 Here is a list of all the functions that assume that widening and
47 narrowing is always done with a NOP_EXPR:
48 In convert.c, convert_to_integer.
49 In c-typeck.c, build_binary_op_nodefault (boolean ops),
50 and truthvalue_conversion.
51 In expr.c: expand_expr, for operands of a MULT_EXPR.
52 In fold-const.c: fold.
53 In tree.c: get_narrower and get_unwidened.
55 C++: in multiple-inheritance, converting between pointers may involve
56 adjusting them by a delta stored within the class definition. */
58 /* Subroutines of `convert'. */
60 /* Build a thunk. What it is, is an entry point that when called will
61 adjust the this pointer (the first argument) by offset, and then
62 goto the real address of the function given by REAL_ADDR that we
63 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. */
82 convert_fn_ptr (type, expr)
85 if (flag_vtable_thunks)
87 tree intype = TREE_TYPE (expr);
88 tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype)),
89 TYPE_METHOD_BASETYPE (TREE_TYPE (type)), 1);
90 if (binfo == error_mark_node)
92 error (" in pointer to member conversion");
93 return error_mark_node;
95 if (binfo == NULL_TREE)
97 /* ARM 4.8 restriction. */
98 error ("invalid pointer to member conversion");
99 return error_mark_node;
102 if (BINFO_OFFSET_ZEROP (binfo))
103 return build1 (NOP_EXPR, type, expr);
104 return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
107 return build_ptrmemfunc (type, expr, 1);
110 /* if converting pointer to pointer
111 if dealing with classes, check for derived->base or vice versa
112 else if dealing with method pointers, delegate
114 else if converting class, pass off to build_type_conversion
115 else try C-style pointer conversion */
117 cp_convert_to_pointer (type, expr)
120 register tree intype = TREE_TYPE (expr);
121 register enum tree_code form;
123 if (TYPE_PTRMEMFUNC_P (type))
124 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
125 if (TYPE_PTRMEMFUNC_P (intype))
126 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
128 form = TREE_CODE (intype);
130 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
132 intype = TYPE_MAIN_VARIANT (intype);
134 if (TYPE_MAIN_VARIANT (type) != intype
135 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
136 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
138 enum tree_code code = PLUS_EXPR;
139 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
140 if (binfo == error_mark_node)
141 return error_mark_node;
142 if (binfo == NULL_TREE)
144 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
145 if (binfo == error_mark_node)
146 return error_mark_node;
151 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
152 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
153 || ! BINFO_OFFSET_ZEROP (binfo))
155 /* Need to get the path we took. */
158 if (code == PLUS_EXPR)
159 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
161 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
162 return build_vbase_path (code, type, expr, path, 0);
166 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
167 && TREE_CODE (type) == POINTER_TYPE
168 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
169 return convert_fn_ptr (type, expr);
171 if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
172 && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
174 tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
175 tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
176 tree binfo = get_binfo (b1, b2, 1);
177 if (binfo == NULL_TREE)
178 binfo = get_binfo (b2, b1, 1);
179 if (binfo == error_mark_node)
180 return error_mark_node;
183 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
184 || (TREE_CODE (type) == POINTER_TYPE
185 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
187 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
189 return error_mark_node;
192 return build1 (NOP_EXPR, type, expr);
195 my_friendly_assert (form != OFFSET_TYPE, 186);
197 if (TYPE_LANG_SPECIFIC (intype)
198 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
199 return convert_to_pointer (type, build_optr_ref (expr));
201 if (IS_AGGR_TYPE (intype))
205 if (TYPE_SIZE (complete_type (intype)) == NULL_TREE)
207 cp_error ("can't convert from incomplete type `%T' to `%T'",
209 return error_mark_node;
212 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
215 if (rval == error_mark_node)
216 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
222 if (integer_zerop (expr))
224 if (type == TREE_TYPE (null_pointer_node))
225 return null_pointer_node;
226 expr = build_int_2 (0, 0);
227 TREE_TYPE (expr) = type;
231 if (INTEGRAL_CODE_P (form))
233 if (type_precision (intype) == POINTER_SIZE)
234 return build1 (CONVERT_EXPR, type, expr);
235 expr = convert (type_for_size (POINTER_SIZE, 0), expr);
236 /* Modes may be different but sizes should be the same. */
237 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
238 != GET_MODE_SIZE (TYPE_MODE (type)))
239 /* There is supposed to be some integral type
240 that is the same width as a pointer. */
242 return convert_to_pointer (type, expr);
245 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
247 return error_mark_node;
250 /* Like convert, except permit conversions to take place which
251 are not normally allowed due to access restrictions
252 (such as conversion from sub-type to private super-type). */
254 convert_to_pointer_force (type, expr)
257 register tree intype = TREE_TYPE (expr);
258 register enum tree_code form = TREE_CODE (intype);
260 if (integer_zerop (expr))
262 if (type == TREE_TYPE (null_pointer_node))
263 return null_pointer_node;
264 expr = build_int_2 (0, 0);
265 TREE_TYPE (expr) = type;
269 /* Convert signature pointer/reference to `void *' first. */
270 if (form == RECORD_TYPE
271 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
273 expr = build_optr_ref (expr);
274 intype = TREE_TYPE (expr);
275 form = TREE_CODE (intype);
278 if (form == POINTER_TYPE)
280 intype = TYPE_MAIN_VARIANT (intype);
282 if (TYPE_MAIN_VARIANT (type) != intype
283 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
284 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
286 enum tree_code code = PLUS_EXPR;
288 int distance = get_base_distance (TREE_TYPE (type),
289 TREE_TYPE (intype), 0, &path);
293 cp_error ("type `%T' is ambiguous baseclass of `%s'", TREE_TYPE (type),
294 TYPE_NAME_STRING (TREE_TYPE (intype)));
295 return error_mark_node;
299 distance = get_base_distance (TREE_TYPE (intype),
300 TREE_TYPE (type), 0, &path);
304 /* Doesn't need any special help from us. */
305 return build1 (NOP_EXPR, type, expr);
309 return build_vbase_path (code, type, expr, path, 0);
311 return build1 (NOP_EXPR, type, expr);
314 return cp_convert_to_pointer (type, expr);
317 /* We are passing something to a function which requires a reference.
318 The type we are interested in is in TYPE. The initial
319 value we have to begin with is in ARG.
321 FLAGS controls how we manage access checking.
322 INDIRECT_BIND in FLAGS controls how any temporarys are generated.
323 CHECKCONST controls if we report error messages on const subversion. */
325 build_up_reference (type, arg, flags, checkconst)
327 int flags, checkconst;
330 int literal_flag = 0;
331 tree argtype = TREE_TYPE (arg);
332 tree target_type = TREE_TYPE (type);
333 tree binfo = NULL_TREE;
335 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
336 if ((flags & LOOKUP_PROTECT)
337 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
338 && IS_AGGR_TYPE (argtype)
339 && IS_AGGR_TYPE (target_type))
341 binfo = get_binfo (target_type, argtype, 1);
342 if (binfo == error_mark_node)
343 return error_mark_node;
344 if (binfo == NULL_TREE)
345 return error_not_base_type (target_type, argtype);
348 /* Pass along const and volatile down into the type. */
349 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
350 target_type = cp_build_type_variant (target_type, TYPE_READONLY (type),
351 TYPE_VOLATILE (type));
353 if (TREE_CODE (targ) == SAVE_EXPR)
354 targ = TREE_OPERAND (targ, 0);
355 while (TREE_CODE (targ) == NOP_EXPR
356 && (TYPE_MAIN_VARIANT (argtype)
357 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0)))))
358 targ = TREE_OPERAND (targ, 0);
360 switch (TREE_CODE (targ))
363 /* This is a call to a constructor which did not know what it was
364 initializing until now: it needs to initialize a temporary. */
365 if (TREE_HAS_CONSTRUCTOR (targ))
367 tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0));
368 TREE_HAS_CONSTRUCTOR (targ) = 0;
369 return build_up_reference (type, temp, flags, 1);
371 /* Let &* cancel out to simplify resulting code.
372 Also, throw away intervening NOP_EXPRs. */
373 arg = TREE_OPERAND (targ, 0);
374 if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
375 || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
376 arg = TREE_OPERAND (arg, 0);
378 /* in doing a &*, we have to get rid of the const'ness on the pointer
379 value. Haven't thought about volatile here. Pointers come to mind
381 if (TREE_READONLY (arg))
383 arg = copy_node (arg);
384 TREE_READONLY (arg) = 0;
387 rval = build1 (CONVERT_EXPR, type, arg);
388 TREE_REFERENCE_EXPR (rval) = 1;
390 /* propagate the const flag on something like:
397 class Derived : public Base {
404 void func2(const Derived& d) {
408 on the d parameter. The below could have been avoided, if the flags
409 were down in the tree, not sure why they are not. (mrs) */
410 /* The below code may have to be propagated to other parts of this
412 if (TREE_READONLY (targ) && !TREE_READONLY (arg)
413 && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
414 && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
415 && (TYPE_READONLY (target_type) && checkconst))
417 arg = copy_node (arg);
418 TREE_READONLY (arg) = TREE_READONLY (targ);
420 literal_flag = TREE_CONSTANT (arg);
424 /* Get this out of a register if we happened to be in one by accident.
425 Also, build up references to non-lvalues it we must. */
426 /* For &x[y], return (&) x+y */
428 if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
429 return error_mark_node;
430 rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
431 TREE_OPERAND (targ, 1), 1);
432 TREE_TYPE (rval) = type;
433 if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
434 && staticp (TREE_OPERAND (targ, 0)))
435 TREE_CONSTANT (rval) = 1;
439 /* Could be a reference to a static member. */
441 tree field = TREE_OPERAND (targ, 1);
442 if (TREE_STATIC (field))
444 rval = build1 (ADDR_EXPR, type, field);
450 /* We should have farmed out member pointers above. */
451 my_friendly_abort (188);
454 rval = build_component_addr (targ, build_pointer_type (argtype),
455 "attempt to make a reference to bit-field structure member `%s'");
456 TREE_TYPE (rval) = type;
457 literal_flag = staticp (TREE_OPERAND (targ, 0));
461 /* Anything not already handled and not a true memory reference
462 needs to have a reference built up. Do so silently for
463 things like integers and return values from function,
464 but complain if we need a reference to something declared
470 TREE_ADDRESSABLE (targ) = 1;
471 put_var_into_stack (targ);
476 if (targ == current_class_decl)
478 error ("address of `this' not available");
480 /* This code makes the following core dump the compiler on a sun4,
481 if the code below is used.
485 typedef a_decl* a_ref;
490 void* append(a_ref& item);
494 a_decl (e_decl *parent);
508 a_decl::a_decl(e_decl *parent) {
509 parent->implementations.append(this);
513 TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
514 put_var_into_stack (targ);
517 return error_mark_node;
524 if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ)
525 && !DECL_ARTIFICIAL (targ))
526 cp_warning ("address needed to build reference for `%D', which is declared `register'",
528 else if (staticp (targ))
531 TREE_ADDRESSABLE (targ) = 1;
532 put_var_into_stack (targ);
537 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
538 LOOKUP_PROTECT, checkconst);
539 rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
540 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
544 case PREINCREMENT_EXPR:
545 case PREDECREMENT_EXPR:
549 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
550 LOOKUP_PROTECT, checkconst);
551 rval = build (COMPOUND_EXPR, type, arg, real_reference);
552 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
557 return build (COND_EXPR, type,
558 TREE_OPERAND (targ, 0),
559 build_up_reference (type, TREE_OPERAND (targ, 1),
560 LOOKUP_PROTECT, checkconst),
561 build_up_reference (type, TREE_OPERAND (targ, 2),
562 LOOKUP_PROTECT, checkconst));
564 /* Undo the folding... */
567 return build (COND_EXPR, type,
568 build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR,
569 boolean_type_node, TREE_OPERAND (targ, 0),
570 TREE_OPERAND (targ, 1)),
571 build_up_reference (type, TREE_OPERAND (targ, 0),
572 LOOKUP_PROTECT, checkconst),
573 build_up_reference (type, TREE_OPERAND (targ, 1),
574 LOOKUP_PROTECT, checkconst));
577 arg = TREE_OPERAND (targ, 1);
578 if (arg == NULL_TREE)
580 compiler_error ("({ ... }) expression not expanded when needed for reference");
581 return error_mark_node;
583 rval = build1 (ADDR_EXPR, type, arg);
584 TREE_REFERENCE_EXPR (rval) = 1;
591 if (TREE_ADDRESSABLE (targ) == 0)
595 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
597 temp = build_cplus_new (argtype, targ);
598 rval = build1 (ADDR_EXPR, type, temp);
601 else if (flags&INDIRECT_BIND)
603 /* This should be the default, not the below code. */
604 /* All callers except grok_reference_init should probably
605 use INDIRECT_BIND. */
606 tree slot = build (VAR_DECL, argtype);
607 layout_decl (slot, 0);
608 rval = build (TARGET_EXPR, argtype, slot, arg, 0);
609 rval = build1 (ADDR_EXPR, type, rval);
614 temp = get_temp_name (argtype, 0);
615 if (toplevel_bindings_p ())
617 /* Give this new temp some rtl and initialize it. */
618 DECL_INITIAL (temp) = targ;
619 TREE_STATIC (temp) = 1;
620 cp_finish_decl (temp, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
621 /* Do this after declaring it static. */
622 rval = build_unary_op (ADDR_EXPR, temp, 0);
623 TREE_TYPE (rval) = type;
624 literal_flag = TREE_CONSTANT (rval);
629 rval = build_unary_op (ADDR_EXPR, temp, 0);
630 if (binfo && !BINFO_OFFSET_ZEROP (binfo))
631 rval = convert_pointer_to (target_type, rval);
633 TREE_TYPE (rval) = type;
635 temp = build (MODIFY_EXPR, argtype, temp, arg);
636 TREE_SIDE_EFFECTS (temp) = 1;
637 return build (COMPOUND_EXPR, type, temp, rval);
642 rval = build1 (ADDR_EXPR, type, arg);
645 if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
646 || TYPE_USES_COMPLEX_INHERITANCE (target_type))
648 TREE_TYPE (rval) = build_pointer_type (argtype);
649 if (flags & LOOKUP_PROTECT)
650 rval = convert_pointer_to (target_type, rval);
653 = convert_to_pointer_force (build_pointer_type (target_type), rval);
654 TREE_TYPE (rval) = type;
655 if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR)
656 TREE_TYPE (TREE_OPERAND (rval, 0))
657 = TREE_TYPE (TREE_OPERAND (rval, 1)) = type;
659 TREE_CONSTANT (rval) = literal_flag;
663 /* For C++: Only need to do one-level references, but cannot
664 get tripped up on signed/unsigned differences.
666 DECL is either NULL_TREE or the _DECL node for a reference that is being
667 initialized. It can be error_mark_node if we don't know the _DECL but
668 we know it's an initialization. */
671 convert_to_reference (reftype, expr, convtype, flags, decl)
676 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
677 register tree intype = TREE_TYPE (expr);
678 tree rval = NULL_TREE;
679 tree rval_as_conversion = NULL_TREE;
682 if (TREE_CODE (intype) == REFERENCE_TYPE)
683 my_friendly_abort (364);
685 intype = TYPE_MAIN_VARIANT (intype);
687 i = comp_target_types (type, intype, 0);
689 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
690 && ! (flags & LOOKUP_NO_CONVERSION))
692 /* Look for a user-defined conversion to lvalue that we can use. */
694 rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
696 if (rval_as_conversion && rval_as_conversion != error_mark_node
697 && real_lvalue_p (rval_as_conversion))
699 expr = rval_as_conversion;
700 rval_as_conversion = NULL_TREE;
706 if (((convtype & CONV_STATIC) && i == -1)
707 || ((convtype & CONV_IMPLICIT) && i == 1))
709 if (flags & LOOKUP_COMPLAIN)
711 tree ttl = TREE_TYPE (reftype);
715 int r = TREE_READONLY (expr);
716 int v = TREE_THIS_VOLATILE (expr);
717 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
720 if (! real_lvalue_p (expr) &&
721 (decl == NULL_TREE || ! TYPE_READONLY (ttl)))
724 /* Ensure semantics of [dcl.init.ref] */
725 cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'",
728 cp_pedwarn ("conversion to `%T' from rvalue `%T'",
731 else if (! (convtype & CONV_CONST))
733 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
734 cp_pedwarn ("conversion from `%T' to `%T' discards const",
736 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
737 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
742 return build_up_reference (reftype, expr, flags,
743 ! (convtype & CONV_CONST));
745 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
747 /* When casting an lvalue to a reference type, just convert into
748 a pointer to the new type and deference it. This is allowed
749 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
750 should be done directly (jason). (int &)ri ---> *(int*)&ri */
752 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
754 if (TREE_CODE (intype) == POINTER_TYPE
755 && (comptypes (TREE_TYPE (intype), type, -1)))
756 cp_warning ("casting `%T' to `%T' does not dereference pointer",
759 rval = build_unary_op (ADDR_EXPR, expr, 0);
760 if (rval != error_mark_node)
761 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
762 if (rval != error_mark_node)
763 rval = build1 (NOP_EXPR, reftype, rval);
767 tree rval_as_ctor = NULL_TREE;
769 if (rval_as_conversion)
771 if (rval_as_conversion == error_mark_node)
773 cp_error ("conversion from `%T' to `%T' is ambiguous",
775 return error_mark_node;
777 rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
781 /* Definitely need to go through a constructor here. */
782 if (TYPE_HAS_CONSTRUCTOR (type)
783 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
784 && (rval = build_method_call
785 (NULL_TREE, ctor_identifier,
786 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
787 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
788 | LOOKUP_ONLYCONVERTING)))
792 if (toplevel_bindings_p ())
794 extern tree static_aggregates;
795 tree t = get_temp_name (type, toplevel_bindings_p ());
796 init = build_method_call (t, ctor_identifier,
797 build_tree_list (NULL_TREE, expr),
799 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
800 | LOOKUP_ONLYCONVERTING);
802 if (init == error_mark_node)
803 return error_mark_node;
805 make_decl_rtl (t, NULL_PTR, 1);
806 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
807 rval = build_unary_op (ADDR_EXPR, t, 0);
811 init = build_method_call (NULL_TREE, ctor_identifier,
812 build_tree_list (NULL_TREE, expr),
814 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
815 |LOOKUP_ONLYCONVERTING);
817 if (init == error_mark_node)
818 return error_mark_node;
820 rval = build_cplus_new (type, init);
821 rval = build_up_reference (reftype, rval, flags, 1);
826 if (rval_as_ctor && rval_as_conversion)
828 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
830 return error_mark_node;
832 else if (rval_as_ctor)
834 else if (rval_as_conversion)
835 rval = rval_as_conversion;
836 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
838 rval = convert (type, expr);
839 if (rval == error_mark_node)
840 return error_mark_node;
842 rval = build_up_reference (reftype, rval, flags, 1);
845 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
846 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
852 /* If we found a way to convert earlier, then use it. */
856 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
858 if (flags & LOOKUP_COMPLAIN)
859 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
861 if (flags & LOOKUP_SPECULATIVELY)
864 return error_mark_node;
867 /* We are using a reference VAL for its value. Bash that reference all the
868 way down to its lowest form. */
870 convert_from_reference (val)
873 tree type = TREE_TYPE (val);
875 if (TREE_CODE (type) == OFFSET_TYPE)
876 type = TREE_TYPE (type);
877 if (TREE_CODE (type) == REFERENCE_TYPE)
878 return build_indirect_ref (val, NULL_PTR);
882 /* See if there is a constructor of type TYPE which will convert
883 EXPR. The reference manual seems to suggest (8.5.6) that we need
884 not worry about finding constructors for base classes, then converting
885 to the derived class.
887 MSGP is a pointer to a message that would be an appropriate error
888 string. If MSGP is NULL, then we are not interested in reporting
891 convert_to_aggr (type, expr, msgp, protect)
896 tree basetype = type;
897 tree name = TYPE_IDENTIFIER (basetype);
898 tree function, fndecl, fntype, parmtypes, parmlist, result;
900 /* See code below that used this. */
904 int can_be_private, can_be_protected;
906 if (! TYPE_HAS_CONSTRUCTOR (basetype))
909 *msgp = "type `%s' does not have a constructor";
910 return error_mark_node;
913 access = access_public_node;
915 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
917 parmlist = build_tree_list (NULL_TREE, expr);
918 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
920 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
922 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
923 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
926 /* The type of the first argument will be filled in inside the loop. */
927 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
928 parmtypes = tree_cons (NULL_TREE, build_pointer_type (basetype), parmtypes);
930 /* No exact conversion was found. See if an approximate
932 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
936 int saw_protected = 0;
937 struct candidate *candidates =
938 (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
939 struct candidate *cp = candidates;
945 cp->harshness = (struct harshness_code *)
946 alloca (3 * sizeof (struct harshness_code));
948 compute_conversion_costs (fndecl, parmlist, cp, 2);
949 if ((cp->h.code & EVIL_CODE) == 0)
951 cp->u.field = fndecl;
954 if (TREE_PRIVATE (fndecl))
955 access = access_private_node;
956 else if (TREE_PROTECTED (fndecl))
957 access = access_protected_node;
959 access = access_public_node;
962 access = access_public_node;
964 if (access == access_private_node
965 ? (basetype == current_class_type
966 || is_friend (basetype, cp->function)
967 || purpose_member (basetype, DECL_ACCESS (fndecl)))
968 : access == access_protected_node
970 || purpose_member (basetype, DECL_ACCESS (fndecl)))
973 if (cp->h.code <= TRIVIAL_CODE)
979 if (access == access_private_node)
985 fndecl = DECL_CHAIN (fndecl);
989 /* Rank from worst to best. Then cp will point to best one.
990 Private fields have their bits flipped. For unsigned
991 numbers, this should make them look very large.
992 If the best alternate has a (signed) negative value,
993 then all we ever saw were private members. */
994 if (cp - candidates > 1)
995 qsort (candidates, /* char *base */
996 cp - candidates, /* int nel */
997 sizeof (struct candidate), /* int width */
998 rank_for_overload); /* int (*compar)() */
1001 if (cp->h.code & EVIL_CODE)
1004 *msgp = "ambiguous type conversion possible for `%s'";
1005 return error_mark_node;
1008 function = cp->function;
1009 fndecl = cp->u.field;
1016 *msgp = "only private and protected conversions apply";
1018 *msgp = "only private conversions apply";
1019 else if (saw_protected)
1020 *msgp = "only protected conversions apply";
1022 *msgp = "no appropriate conversion to type `%s'";
1024 return error_mark_node;
1029 if (access == access_private_node)
1030 if (! can_be_private)
1033 *msgp = TREE_PRIVATE (fndecl)
1034 ? "conversion to type `%s' is private"
1035 : "conversion to type `%s' is from private base class";
1036 return error_mark_node;
1038 if (access == access_protected_node)
1039 if (! can_be_protected)
1042 *msgp = TREE_PRIVATE (fndecl)
1043 ? "conversion to type `%s' is protected"
1044 : "conversion to type `%s' is from protected base class";
1045 return error_mark_node;
1050 /* It will convert, but we don't do anything about it yet. */
1054 fntype = TREE_TYPE (function);
1055 function = default_conversion (function);
1057 result = build_nt (CALL_EXPR, function,
1058 convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1059 parmlist, NULL_TREE, LOOKUP_NORMAL),
1061 TREE_TYPE (result) = TREE_TYPE (fntype);
1062 TREE_SIDE_EFFECTS (result) = 1;
1066 /* Call this when we know (for any reason) that expr is not, in fact,
1067 zero. This routine is like convert_pointer_to, but it pays
1068 attention to which specific instance of what type we want to
1069 convert to. This routine should eventually become
1070 convert_to_pointer after all references to convert_to_pointer
1073 convert_pointer_to_real (binfo, expr)
1076 register tree intype = TREE_TYPE (expr);
1080 if (TREE_CODE (binfo) == TREE_VEC)
1081 type = BINFO_TYPE (binfo);
1082 else if (IS_AGGR_TYPE (binfo))
1092 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
1093 TYPE_VOLATILE (TREE_TYPE (intype)));
1094 ptr_type = build_pointer_type (ptr_type);
1095 if (ptr_type == TYPE_MAIN_VARIANT (intype))
1098 if (intype == error_mark_node)
1099 return error_mark_node;
1101 my_friendly_assert (!integer_zerop (expr), 191);
1103 if (TREE_CODE (type) == RECORD_TYPE
1104 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1105 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1109 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1112 /* This function shouldn't be called with unqualified arguments
1113 but if it is, give them an error message that they can read. */
1116 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1117 TREE_TYPE (intype), type);
1120 cp_error ("because `%T' is an ambiguous base class", type);
1121 return error_mark_node;
1124 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1126 rval = build1 (NOP_EXPR, ptr_type,
1127 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1128 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1132 /* Call this when we know (for any reason) that expr is
1133 not, in fact, zero. This routine gets a type out of the first
1134 argument and uses it to search for the type to convert to. If there
1135 is more than one instance of that type in the expr, the conversion is
1136 ambiguous. This routine should eventually go away, and all
1137 callers should use convert_to_pointer_real. */
1139 convert_pointer_to (binfo, expr)
1144 if (TREE_CODE (binfo) == TREE_VEC)
1145 type = BINFO_TYPE (binfo);
1146 else if (IS_AGGR_TYPE (binfo))
1150 return convert_pointer_to_real (type, expr);
1155 FLAGS indicates how we should behave. */
1158 cp_convert (type, expr, convtype, flags)
1160 int convtype, flags;
1162 register tree e = expr;
1163 register enum tree_code code = TREE_CODE (type);
1165 if (TREE_CODE (e) == ERROR_MARK
1166 || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
1167 return error_mark_node;
1169 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
1170 /* We need a new temporary; don't take this shortcut. */;
1171 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
1172 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
1173 return fold (build1 (NOP_EXPR, type, e));
1175 if (code == VOID_TYPE && (convtype & CONV_STATIC))
1176 return build1 (CONVERT_EXPR, type, e);
1179 /* This is incorrect. A truncation can't be stripped this way.
1180 Extensions will be stripped by the use of get_unwidened. */
1181 if (TREE_CODE (e) == NOP_EXPR)
1182 return convert (type, TREE_OPERAND (e, 0));
1185 /* Just convert to the type of the member. */
1186 if (code == OFFSET_TYPE)
1188 type = TREE_TYPE (type);
1189 code = TREE_CODE (type);
1193 if (code == REFERENCE_TYPE)
1194 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1195 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1196 e = convert_from_reference (e);
1199 if (TREE_CODE (e) == OFFSET_REF)
1200 e = resolve_offset_ref (e);
1202 if (TREE_READONLY_DECL_P (e))
1203 e = decl_constant_value (e);
1205 if (INTEGRAL_CODE_P (code))
1207 tree intype = TREE_TYPE (e);
1208 /* enum = enum, enum = int, enum = float are all errors. */
1209 if (flag_int_enum_equivalence == 0
1210 && TREE_CODE (type) == ENUMERAL_TYPE
1211 && ARITHMETIC_TYPE_P (intype)
1212 && ! (convtype & CONV_STATIC))
1214 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1216 if (flag_pedantic_errors)
1217 return error_mark_node;
1219 if (IS_AGGR_TYPE (intype))
1222 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1225 if (flags & LOOKUP_COMPLAIN)
1226 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1227 if (flags & LOOKUP_SPECULATIVELY)
1229 return error_mark_node;
1231 if (code == BOOLEAN_TYPE)
1233 /* Common Ada/Pascal programmer's mistake. We always warn
1234 about this since it is so bad. */
1235 if (TREE_CODE (expr) == FUNCTION_DECL)
1236 cp_warning ("the address of `%D', will always be `true'", expr);
1237 return truthvalue_conversion (e);
1239 return fold (convert_to_integer (type, e));
1241 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1242 || TYPE_PTRMEMFUNC_P (type))
1243 return fold (cp_convert_to_pointer (type, e));
1244 if (code == REAL_TYPE)
1246 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1249 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1253 if (flags & LOOKUP_COMPLAIN)
1254 cp_error ("`%#T' used where a floating point value was expected",
1257 return fold (convert_to_real (type, e));
1260 /* New C++ semantics: since assignment is now based on
1261 memberwise copying, if the rhs type is derived from the
1262 lhs type, then we may still do a conversion. */
1263 if (IS_AGGR_TYPE_CODE (code))
1265 tree dtype = TREE_TYPE (e);
1266 tree ctor = NULL_TREE;
1267 tree conversion = NULL_TREE;
1269 dtype = TYPE_MAIN_VARIANT (dtype);
1271 /* Conversion of object pointers or signature pointers/references
1272 to signature pointers/references. */
1274 if (TYPE_LANG_SPECIFIC (type)
1275 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1277 tree constructor = build_signature_pointer_constructor (type, expr);
1278 tree sig_ty = SIGNATURE_TYPE (type);
1281 if (constructor == error_mark_node)
1282 return error_mark_node;
1284 sig_ptr = get_temp_name (type, 1);
1285 DECL_INITIAL (sig_ptr) = constructor;
1286 CLEAR_SIGNATURE (sig_ty);
1287 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1288 SET_SIGNATURE (sig_ty);
1289 TREE_READONLY (sig_ptr) = 1;
1294 /* Conversion between aggregate types. New C++ semantics allow
1295 objects of derived type to be cast to objects of base type.
1296 Old semantics only allowed this between pointers.
1298 There may be some ambiguity between using a constructor
1299 vs. using a type conversion operator when both apply. */
1301 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1302 && TYPE_HAS_CONVERSION (dtype))
1303 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1305 if (conversion == error_mark_node)
1307 if (flags & LOOKUP_COMPLAIN)
1308 error ("ambiguous pointer conversion");
1312 if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1313 ctor = build_method_call (NULL_TREE, ctor_identifier,
1314 build_tree_list (NULL_TREE, e),
1316 (flags & LOOKUP_NORMAL) | LOOKUP_SPECULATIVELY
1317 | (convtype & CONV_NONCONVERTING ? 0 : LOOKUP_ONLYCONVERTING)
1318 | (flags & LOOKUP_NO_CONVERSION)
1319 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1321 if (ctor == error_mark_node)
1323 if (flags & LOOKUP_COMPLAIN)
1324 cp_error ("in conversion to type `%T'", type);
1325 if (flags & LOOKUP_SPECULATIVELY)
1327 return error_mark_node;
1330 if (conversion && ctor)
1332 if (flags & LOOKUP_COMPLAIN)
1333 error ("both constructor and type conversion operator apply");
1334 if (flags & LOOKUP_SPECULATIVELY)
1336 return error_mark_node;
1338 else if (conversion)
1342 ctor = build_cplus_new (type, ctor);
1347 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1348 then the it won't be hashed and hence compare as not equal,
1350 if (code == ARRAY_TYPE
1351 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1352 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1355 if (flags & LOOKUP_COMPLAIN)
1356 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1357 TREE_TYPE (expr), type);
1358 if (flags & LOOKUP_SPECULATIVELY)
1360 return error_mark_node;
1363 /* Create an expression whose value is that of EXPR,
1364 converted to type TYPE. The TREE_TYPE of the value
1365 is always TYPE. This function implements all reasonable
1366 conversions; callers should filter out those that are
1367 not permitted by the language being compiled. */
1370 convert (type, expr)
1373 return cp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
1376 /* Like convert, except permit conversions to take place which
1377 are not normally allowed due to access restrictions
1378 (such as conversion from sub-type to private super-type). */
1380 convert_force (type, expr, convtype)
1385 register tree e = expr;
1386 register enum tree_code code = TREE_CODE (type);
1388 if (code == REFERENCE_TYPE)
1389 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1391 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1392 e = convert_from_reference (e);
1394 if (code == POINTER_TYPE)
1395 return fold (convert_to_pointer_force (type, e));
1397 /* From typeck.c convert_for_assignment */
1398 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1399 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1400 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1401 || integer_zerop (e)
1402 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1403 && TYPE_PTRMEMFUNC_P (type))
1405 /* compatible pointer to member functions. */
1406 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1409 return cp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1412 /* Subroutine of build_type_conversion. */
1414 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1415 tree xtype, basetype;
1424 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1426 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1428 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1429 if (rval == error_mark_node)
1433 return error_mark_node;
1436 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1440 && TREE_TYPE (xtype)
1441 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1442 > TREE_READONLY (TREE_TYPE (xtype))))
1443 warning ("user-defined conversion casting away `const'");
1444 return convert (xtype, rval);
1447 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1448 exists, return the attempted conversion. This may
1449 return ERROR_MARK_NODE if the conversion is not
1450 allowed (references private members, etc).
1451 If no conversion exists, NULL_TREE is returned.
1453 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1454 to take place immediately. Otherwise, we build a SAVE_EXPR
1455 which can be evaluated if the results are ever needed.
1457 Changes to this functions should be mirrored in user_harshness.
1459 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1460 object parameter, or by the second standard conversion sequence if
1461 that doesn't do it. This will probably wait for an overloading rewrite.
1465 build_type_conversion (code, xtype, expr, for_sure)
1466 enum tree_code code;
1470 /* C++: check to see if we can convert this aggregate type
1471 into the required type. */
1474 tree winner = NULL_TREE;
1476 if (expr == error_mark_node)
1477 return error_mark_node;
1479 basetype = TREE_TYPE (expr);
1480 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1481 basetype = TREE_TYPE (basetype);
1483 basetype = TYPE_MAIN_VARIANT (basetype);
1484 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1487 /* Do we have an exact match? */
1489 tree typename = build_typename_overload (xtype);
1490 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1491 return build_type_conversion_1 (xtype, basetype, expr, typename,
1495 /* Nope; try looking for others. */
1496 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1498 if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv))
1501 if (can_convert (xtype, TREE_VALUE (conv)))
1507 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1509 cp_error (" candidate conversions include `%T' and `%T'",
1510 TREE_VALUE (winner), TREE_VALUE (conv));
1520 return build_type_conversion_1 (xtype, basetype, expr,
1521 TREE_PURPOSE (winner), for_sure);
1526 /* Convert the given EXPR to one of a group of types suitable for use in an
1527 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1528 which indicates which types are suitable. If COMPLAIN is 1, complain
1529 about ambiguity; otherwise, the caller will deal with it. */
1532 build_expr_type_conversion (desires, expr, complain)
1537 tree basetype = TREE_TYPE (expr);
1539 tree winner = NULL_TREE;
1541 if (TREE_CODE (basetype) == OFFSET_TYPE)
1542 expr = resolve_offset_ref (expr);
1543 expr = convert_from_reference (expr);
1544 basetype = TREE_TYPE (expr);
1546 if (! IS_AGGR_TYPE (basetype))
1547 switch (TREE_CODE (basetype))
1550 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1551 && integer_zerop (expr))
1553 /* else fall through... */
1556 return (desires & WANT_INT) ? expr : NULL_TREE;
1558 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1560 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1562 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1566 return (desires & WANT_POINTER) ? default_conversion (expr)
1572 if (! TYPE_HAS_CONVERSION (basetype))
1575 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1580 if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv))
1583 candidate = TREE_VALUE (conv);
1584 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1585 candidate = TREE_TYPE (candidate);
1587 switch (TREE_CODE (candidate))
1591 win = (desires & WANT_INT); break;
1593 win = (desires & WANT_ENUM); break;
1595 win = (desires & WANT_FLOAT); break;
1597 win = (desires & WANT_POINTER); break;
1606 cp_error ("ambiguous default type conversion from `%T'",
1608 cp_error (" candidate conversions include `%T' and `%T'",
1609 TREE_VALUE (winner), TREE_VALUE (conv));
1611 return error_mark_node;
1620 tree type = TREE_VALUE (winner);
1621 if (TREE_CODE (type) == REFERENCE_TYPE)
1622 type = TREE_TYPE (type);
1623 return build_type_conversion_1 (type, basetype, expr,
1624 TREE_PURPOSE (winner), 1);
1630 /* Must convert two aggregate types to non-aggregate type.
1631 Attempts to find a non-ambiguous, "best" type conversion.
1633 Return 1 on success, 0 on failure.
1635 @@ What are the real semantics of this supposed to be??? */
1637 build_default_binary_type_conversion (code, arg1, arg2)
1638 enum tree_code code;
1644 case TRUNC_DIV_EXPR:
1646 case FLOOR_DIV_EXPR:
1647 case ROUND_DIV_EXPR:
1648 case EXACT_DIV_EXPR:
1649 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1650 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1653 case TRUNC_MOD_EXPR:
1654 case FLOOR_MOD_EXPR:
1660 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1661 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1666 tree a1, a2, p1, p2;
1669 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1670 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1671 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1672 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1674 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1677 error ("ambiguous default type conversion for `operator +'");
1680 *arg1 = a1, *arg2 = a2;
1682 *arg1 = a1, *arg2 = p2;
1684 *arg1 = p1, *arg2 = a2;
1690 tree a1, a2, p1, p2;
1693 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1694 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1695 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1696 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1698 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1701 error ("ambiguous default type conversion for `operator -'");
1704 *arg1 = a1, *arg2 = a2;
1706 *arg1 = p1, *arg2 = p2;
1708 *arg1 = p1, *arg2 = a2;
1719 tree a1, a2, p1, p2;
1722 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1723 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1724 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1725 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1727 wins = (a1 && a2) + (p1 && p2);
1730 cp_error ("ambiguous default type conversion for `%O'", code);
1733 *arg1 = a1, *arg2 = a2;
1735 *arg1 = p1, *arg2 = p2;
1739 case TRUTH_ANDIF_EXPR:
1740 case TRUTH_ORIF_EXPR:
1741 *arg1 = convert (boolean_type_node, *arg1);
1742 *arg2 = convert (boolean_type_node, *arg2);
1750 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1751 cp_error ("ambiguous default type conversion for `%O'", code);
1759 /* Implements integral promotion (4.1) and float->double promotion. */
1761 type_promotes_to (type)
1764 int constp, volatilep;
1766 if (type == error_mark_node)
1767 return error_mark_node;
1769 constp = TYPE_READONLY (type);
1770 volatilep = TYPE_VOLATILE (type);
1771 type = TYPE_MAIN_VARIANT (type);
1773 /* bool always promotes to int (not unsigned), even if it's the same
1775 if (type == boolean_type_node)
1776 type = integer_type_node;
1778 /* Normally convert enums to int, but convert wide enums to something
1780 else if (TREE_CODE (type) == ENUMERAL_TYPE
1781 || type == wchar_type_node)
1783 int precision = MAX (TYPE_PRECISION (type),
1784 TYPE_PRECISION (integer_type_node));
1785 tree totype = type_for_size (precision, 0);
1786 if (TREE_UNSIGNED (type)
1787 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1788 type = type_for_size (precision, 1);
1792 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1794 /* Traditionally, unsignedness is preserved in default promotions.
1795 Otherwise, retain unsignedness if really not getting bigger. */
1796 if (TREE_UNSIGNED (type)
1797 && (flag_traditional
1798 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1799 type = unsigned_type_node;
1801 type = integer_type_node;
1803 else if (type == float_type_node)
1804 type = double_type_node;
1806 return cp_build_type_variant (type, constp, volatilep);