1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* This file contains the functions for converting C expressions
23 to different data types. The only entry point is `convert'.
24 Every language front end must have a `convert' function
25 but what kind of conversions it does will depend on the language. */
35 #define NULL (char *)0
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. */
64 build_thunk (offset, real_addr)
65 tree offset, real_addr;
67 if (TREE_CODE (real_addr) != ADDR_EXPR
68 || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
70 sorry ("MI pointer to member conversion too complex");
71 return error_mark_node;
73 sorry ("MI pointer to member conversion too complex");
74 return error_mark_node;
77 /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
78 another `pointer to method'. This may involved the creation of
79 a thunk to handle the this offset calculation. */
81 convert_fn_ptr (type, expr)
84 tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (expr))),
85 TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
87 if (binfo == error_mark_node)
89 error (" in pointer to member conversion");
90 return error_mark_node;
92 if (binfo == NULL_TREE)
94 /* ARM 4.8 restriction. */
95 error ("invalid pointer to member conversion");
96 return error_mark_node;
98 if (BINFO_OFFSET_ZEROP (binfo))
99 return build1 (NOP_EXPR, type, expr);
100 return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
103 /* if converting pointer to pointer
104 if dealing with classes, check for derived->base or vice versa
105 else if dealing with method pointers, delegate
107 else if converting class, pass off to build_type_conversion
108 else try C-style pointer conversion */
110 cp_convert_to_pointer (type, expr)
113 register tree intype = TREE_TYPE (expr);
114 register enum tree_code form = TREE_CODE (intype);
116 if (form == POINTER_TYPE)
118 intype = TYPE_MAIN_VARIANT (intype);
120 if (TYPE_MAIN_VARIANT (type) != intype
121 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
122 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
124 enum tree_code code = PLUS_EXPR;
125 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
126 if (binfo == error_mark_node)
127 return error_mark_node;
128 if (binfo == NULL_TREE)
130 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
131 if (binfo == error_mark_node)
132 return error_mark_node;
137 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
138 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
139 || ! BINFO_OFFSET_ZEROP (binfo))
141 /* Need to get the path we took. */
144 if (code == PLUS_EXPR)
145 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
147 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
148 return build_vbase_path (code, type, expr, path, 0);
152 if (TYPE_MAIN_VARIANT (type) != intype
153 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
154 && TREE_CODE (type) == POINTER_TYPE
155 && TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE)
156 return convert_fn_ptr (type, expr);
158 return build1 (NOP_EXPR, type, expr);
161 my_friendly_assert (form != OFFSET_TYPE, 186);
163 if (TYPE_LANG_SPECIFIC (intype)
164 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
165 return convert_to_pointer (type, build_optr_ref (expr));
167 if (IS_AGGR_TYPE (intype))
170 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
173 if (rval == error_mark_node)
174 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
180 if (integer_zerop (expr))
182 if (type == TREE_TYPE (null_pointer_node))
183 return null_pointer_node;
184 expr = build_int_2 (0, 0);
185 TREE_TYPE (expr) = type;
189 if (INTEGRAL_CODE_P (form))
191 if (type_precision (intype) == POINTER_SIZE)
192 return build1 (CONVERT_EXPR, type, expr);
193 expr = convert (type_for_size (POINTER_SIZE, 0), expr);
194 /* Modes may be different but sizes should be the same. */
195 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
196 != GET_MODE_SIZE (TYPE_MODE (type)))
197 /* There is supposed to be some integral type
198 that is the same width as a pointer. */
200 return convert_to_pointer (type, expr);
203 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
205 return error_mark_node;
208 /* Like convert, except permit conversions to take place which
209 are not normally allowed due to access restrictions
210 (such as conversion from sub-type to private super-type). */
212 convert_to_pointer_force (type, expr)
215 register tree intype = TREE_TYPE (expr);
216 register enum tree_code form = TREE_CODE (intype);
218 if (integer_zerop (expr))
220 if (type == TREE_TYPE (null_pointer_node))
221 return null_pointer_node;
222 expr = build_int_2 (0, 0);
223 TREE_TYPE (expr) = type;
227 /* Convert signature pointer/reference to `void *' first. */
228 if (form == RECORD_TYPE
229 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
231 expr = build_optr_ref (expr);
232 intype = TREE_TYPE (expr);
233 form = TREE_CODE (intype);
236 if (form == POINTER_TYPE)
238 intype = TYPE_MAIN_VARIANT (intype);
240 if (TYPE_MAIN_VARIANT (type) != intype
241 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
242 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
244 enum tree_code code = PLUS_EXPR;
246 int distance = get_base_distance (TREE_TYPE (type),
247 TREE_TYPE (intype), 0, &path);
251 cp_error ("type `%T' is ambiguous baseclass of `%s'", TREE_TYPE (type),
252 TYPE_NAME_STRING (TREE_TYPE (intype)));
253 return error_mark_node;
257 distance = get_base_distance (TREE_TYPE (intype),
258 TREE_TYPE (type), 0, &path);
262 /* Doesn't need any special help from us. */
263 return build1 (NOP_EXPR, type, expr);
267 return build_vbase_path (code, type, expr, path, 0);
269 return build1 (NOP_EXPR, type, expr);
272 return cp_convert_to_pointer (type, expr);
275 /* We are passing something to a function which requires a reference.
276 The type we are interested in is in TYPE. The initial
277 value we have to begin with is in ARG.
279 FLAGS controls how we manage access checking.
280 CHECKCONST controls if we report error messages on const subversion. */
282 build_up_reference (type, arg, flags, checkconst)
284 int flags, checkconst;
287 int literal_flag = 0;
288 tree argtype = TREE_TYPE (arg);
289 tree target_type = TREE_TYPE (type);
290 tree binfo = NULL_TREE;
292 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
293 if ((flags & LOOKUP_PROTECT)
294 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
295 && IS_AGGR_TYPE (argtype)
296 && IS_AGGR_TYPE (target_type))
298 binfo = get_binfo (target_type, argtype, 1);
299 if (binfo == error_mark_node)
300 return error_mark_node;
301 if (binfo == NULL_TREE)
302 return error_not_base_type (target_type, argtype);
305 /* Pass along const and volatile down into the type. */
306 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
307 target_type = build_type_variant (target_type, TYPE_READONLY (type),
308 TYPE_VOLATILE (type));
310 if (TREE_CODE (targ) == SAVE_EXPR)
311 targ = TREE_OPERAND (targ, 0);
313 switch (TREE_CODE (targ))
316 /* This is a call to a constructor which did not know what it was
317 initializing until now: it needs to initialize a temporary. */
318 if (TREE_HAS_CONSTRUCTOR (targ))
320 tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1);
321 TREE_HAS_CONSTRUCTOR (targ) = 0;
322 return build_up_reference (type, temp, flags, 1);
324 /* Let &* cancel out to simplify resulting code.
325 Also, throw away intervening NOP_EXPRs. */
326 arg = TREE_OPERAND (targ, 0);
327 if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
328 || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
329 arg = TREE_OPERAND (arg, 0);
331 /* in doing a &*, we have to get rid of the const'ness on the pointer
332 value. Haven't thought about volatile here. Pointers come to mind
334 if (TREE_READONLY (arg))
336 arg = copy_node (arg);
337 TREE_READONLY (arg) = 0;
340 rval = build1 (CONVERT_EXPR, type, arg);
341 TREE_REFERENCE_EXPR (rval) = 1;
343 /* propagate the const flag on something like:
350 class Derived : public Base {
357 void func2(const Derived& d) {
361 on the d parameter. The below could have been avoided, if the flags
362 were down in the tree, not sure why they are not. (mrs) */
363 /* The below code may have to be propagated to other parts of this
365 if (TREE_READONLY (targ) && !TREE_READONLY (arg)
366 && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
367 && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
368 && (TYPE_READONLY (target_type) && checkconst))
370 arg = copy_node (arg);
371 TREE_READONLY (arg) = TREE_READONLY (targ);
373 literal_flag = TREE_CONSTANT (arg);
375 goto done_but_maybe_warn;
377 /* Get this out of a register if we happened to be in one by accident.
378 Also, build up references to non-lvalues it we must. */
379 /* For &x[y], return (&) x+y */
381 if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
382 return error_mark_node;
383 rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
384 TREE_OPERAND (targ, 1), 1);
385 TREE_TYPE (rval) = type;
386 if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
387 && staticp (TREE_OPERAND (targ, 0)))
388 TREE_CONSTANT (rval) = 1;
392 /* Could be a reference to a static member. */
394 tree field = TREE_OPERAND (targ, 1);
395 if (TREE_STATIC (field))
397 rval = build1 (ADDR_EXPR, type, field);
403 /* We should have farmed out member pointers above. */
404 my_friendly_abort (188);
407 rval = build_component_addr (targ, build_pointer_type (argtype),
408 "attempt to make a reference to bit-field structure member `%s'");
409 TREE_TYPE (rval) = type;
410 literal_flag = staticp (TREE_OPERAND (targ, 0));
412 goto done_but_maybe_warn;
414 /* Anything not already handled and not a true memory reference
415 needs to have a reference built up. Do so silently for
416 things like integers and return values from function,
417 but complain if we need a reference to something declared
423 TREE_ADDRESSABLE (targ) = 1;
424 put_var_into_stack (targ);
428 if (targ == current_class_decl)
430 error ("address of `this' not available");
432 /* This code makes the following core dump the compiler on a sun4,
433 if the code below is used.
437 typedef a_decl* a_ref;
442 void* append(a_ref& item);
446 a_decl (e_decl *parent);
460 a_decl::a_decl(e_decl *parent) {
461 parent->implementations.append(this);
465 TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
466 put_var_into_stack (targ);
469 return error_mark_node;
475 if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ))
476 warning ("address needed to build reference for `%s', which is declared `register'",
477 IDENTIFIER_POINTER (DECL_NAME (targ)));
478 else if (staticp (targ))
481 TREE_ADDRESSABLE (targ) = 1;
482 put_var_into_stack (targ);
487 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
488 LOOKUP_PROTECT, checkconst);
489 rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
490 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
497 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
498 LOOKUP_PROTECT, checkconst);
499 rval = build (COMPOUND_EXPR, type, arg, real_reference);
500 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
505 return build (COND_EXPR, type,
506 TREE_OPERAND (targ, 0),
507 build_up_reference (type, TREE_OPERAND (targ, 1),
508 LOOKUP_PROTECT, checkconst),
509 build_up_reference (type, TREE_OPERAND (targ, 2),
510 LOOKUP_PROTECT, checkconst));
512 case WITH_CLEANUP_EXPR:
513 return build (WITH_CLEANUP_EXPR, type,
514 build_up_reference (type, TREE_OPERAND (targ, 0),
515 LOOKUP_PROTECT, checkconst),
516 0, TREE_OPERAND (targ, 2));
519 arg = TREE_OPERAND (targ, 1);
520 if (arg == NULL_TREE)
522 compiler_error ("({ ... }) expression not expanded when needed for reference");
523 return error_mark_node;
525 rval = build1 (ADDR_EXPR, type, arg);
526 TREE_REFERENCE_EXPR (rval) = 1;
533 if (TREE_ADDRESSABLE (targ) == 0)
537 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
539 temp = build_cplus_new (argtype, targ, 1);
540 rval = build1 (ADDR_EXPR, type, temp);
545 temp = get_temp_name (argtype, 0);
546 if (global_bindings_p ())
548 /* Give this new temp some rtl and initialize it. */
549 DECL_INITIAL (temp) = targ;
550 TREE_STATIC (temp) = 1;
551 finish_decl (temp, targ, NULL_TREE, 0);
552 /* Do this after declaring it static. */
553 rval = build_unary_op (ADDR_EXPR, temp, 0);
554 TREE_TYPE (rval) = type;
555 literal_flag = TREE_CONSTANT (rval);
560 rval = build_unary_op (ADDR_EXPR, temp, 0);
561 if (binfo && !BINFO_OFFSET_ZEROP (binfo))
562 rval = convert_pointer_to (target_type, rval);
564 TREE_TYPE (rval) = type;
566 temp = build (MODIFY_EXPR, argtype, temp, arg);
567 TREE_SIDE_EFFECTS (temp) = 1;
568 return build (COMPOUND_EXPR, type, temp, rval);
573 rval = build1 (ADDR_EXPR, type, arg);
576 if (checkconst && TREE_READONLY (arg) && ! TYPE_READONLY (target_type))
577 readonly_error (arg, "conversion to reference", 1);
580 if (TYPE_USES_COMPLEX_INHERITANCE (argtype))
582 TREE_TYPE (rval) = build_pointer_type (argtype);
583 if (flags & LOOKUP_PROTECT)
584 rval = convert_pointer_to (target_type, rval);
587 = convert_to_pointer_force (build_pointer_type (target_type), rval);
588 TREE_TYPE (rval) = type;
590 TREE_CONSTANT (rval) = literal_flag;
594 /* For C++: Only need to do one-level references, but cannot
595 get tripped up on signed/unsigned differences.
597 DECL is either NULL_TREE or the _DECL node for a reference that is being
598 initialized. It can be error_mark_node if we don't know the _DECL but
599 we know it's an initialization. */
601 tree cp_convert PROTO((tree, tree, int, int));
604 convert_to_reference (reftype, expr, convtype, flags, decl)
609 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
610 register tree intype = TREE_TYPE (expr);
611 register enum tree_code form = TREE_CODE (intype);
612 tree rval = NULL_TREE;
614 if (form == REFERENCE_TYPE)
615 intype = TREE_TYPE (intype);
616 intype = TYPE_MAIN_VARIANT (intype);
618 if (IS_AGGR_TYPE (intype)
619 && ! (flags & LOOKUP_NO_CONVERSION)
620 && (rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1)))
622 if (rval == error_mark_node)
623 cp_error ("conversion from `%T' to `%T' is ambiguous",
628 if (((convtype & CONV_STATIC) && comptypes (type, intype, -1))
629 || ((convtype & CONV_IMPLICIT) && comptypes (type, intype, 0)))
631 if (flags & LOOKUP_COMPLAIN)
633 tree ttl = TREE_TYPE (reftype);
636 if (form == REFERENCE_TYPE)
637 ttr = TREE_TYPE (TREE_TYPE (expr));
639 ttr = TREE_TYPE (expr);
641 if (! lvalue_p (expr) &&
642 (decl == NULL_TREE || ! TYPE_READONLY (ttl)))
645 /* Ensure semantics of [dcl.init.ref] */
646 cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'",
649 cp_pedwarn ("conversion to `%T' from rvalue `%T'",
652 else if (! (convtype & CONV_CONST))
654 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
655 cp_pedwarn ("conversion from `%T' to `%T' discards const",
656 TREE_TYPE (expr), reftype);
657 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
658 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
659 TREE_TYPE (expr), reftype);
663 /* If EXPR is of aggregate type, and is really a CALL_EXPR,
664 then we don't need to convert it to reference type if
665 it is only being used to initialize DECL which is also
666 of the same aggregate type. */
667 if (decl != NULL_TREE && decl != error_mark_node
668 && IS_AGGR_TYPE (type)
669 && TREE_CODE (expr) == CALL_EXPR
670 && TYPE_MAIN_VARIANT (type) == intype)
672 tree e1 = build (INIT_EXPR, void_type_node, decl, expr);
675 TREE_SIDE_EFFECTS (e1) = 1;
676 if (form == REFERENCE_TYPE)
677 e2 = build1 (NOP_EXPR, reftype, decl);
680 e2 = build_unary_op (ADDR_EXPR, decl, 0);
681 TREE_TYPE (e2) = reftype;
682 TREE_REFERENCE_EXPR (e2) = 1;
684 return build_compound_expr
685 (tree_cons (NULL_TREE, e1, build_tree_list (NULL_TREE, e2)));
688 else if (form == REFERENCE_TYPE)
690 rval = build1 (NOP_EXPR,
691 build_pointer_type (TREE_TYPE (TREE_TYPE (expr))),
693 rval = cp_convert (build_pointer_type (TREE_TYPE (reftype)), rval,
695 rval = build1 (NOP_EXPR, reftype, rval);
699 return build_up_reference (reftype, expr, flags,
700 ! (convtype & CONV_CONST));
703 if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
705 /* When casting an lvalue to a reference type, just convert into
706 a pointer to the new type and deference it. This is allowed
707 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
708 should be done directly (jason). (int &)ri ---> *(int*)&ri */
710 /* B* bp; A& ar = (A&)bp; is legal, but it's probably not what they
712 if (form == POINTER_TYPE
713 && (comptypes (TREE_TYPE (intype), type, -1)))
714 cp_warning ("casting `%T' to `%T' does not dereference pointer",
717 rval = build_unary_op (ADDR_EXPR, expr, 0);
718 if (rval != error_mark_node)
719 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval);
720 if (rval != error_mark_node)
721 rval = build1 (NOP_EXPR, reftype, rval);
725 tree rval_as_conversion = NULL_TREE;
726 tree rval_as_ctor = NULL_TREE;
728 if (IS_AGGR_TYPE (intype)
729 && (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1)))
731 if (rval == error_mark_node)
734 rval_as_conversion = build_up_reference (reftype, rval, flags, 1);
737 /* Definitely need to go through a constructor here. */
738 if (TYPE_HAS_CONSTRUCTOR (type)
739 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
740 && (rval = build_method_call
741 (NULL_TREE, constructor_name_full (type),
742 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
743 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY)))
747 if (global_bindings_p ())
749 extern tree static_aggregates;
750 tree t = get_temp_name (type, global_bindings_p ());
751 init = build_method_call (t, constructor_name_full (type),
752 build_tree_list (NULL_TREE, expr),
754 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
756 if (init == error_mark_node)
757 return error_mark_node;
759 make_decl_rtl (t, NULL_PTR, 1);
760 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
761 rval = build_unary_op (ADDR_EXPR, t, 0);
765 init = build_method_call (NULL_TREE, constructor_name_full (type),
766 build_tree_list (NULL_TREE, expr),
768 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
770 if (init == error_mark_node)
771 return error_mark_node;
773 rval = build_cplus_new (type, init, 1);
774 rval = build_up_reference (reftype, rval, flags, 1);
779 if (rval_as_ctor && rval_as_conversion)
781 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
783 return error_mark_node;
785 else if (rval_as_ctor)
787 else if (rval_as_conversion)
788 rval = rval_as_conversion;
789 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
791 rval = convert (type, expr);
792 if (rval == error_mark_node)
793 return error_mark_node;
795 rval = build_up_reference (reftype, rval, flags, 1);
798 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
799 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
805 /* If we found a way to convert earlier, then use it. */
809 my_friendly_assert (form != OFFSET_TYPE, 189);
811 if (flags & LOOKUP_SPECULATIVELY)
814 else if (flags & LOOKUP_COMPLAIN)
815 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
817 return error_mark_node;
820 /* We are using a reference VAL for its value. Bash that reference all the
821 way down to its lowest form. */
823 convert_from_reference (val)
826 tree type = TREE_TYPE (val);
828 if (TREE_CODE (type) == OFFSET_TYPE)
829 type = TREE_TYPE (type);
830 if (TREE_CODE (type) == REFERENCE_TYPE)
832 tree target_type = TREE_TYPE (type);
835 /* This can happen if we cast to a reference type. */
836 if (TREE_CODE (val) == ADDR_EXPR)
838 nval = build1 (NOP_EXPR, build_pointer_type (target_type), val);
839 nval = build_indirect_ref (nval, NULL_PTR);
840 /* The below was missing, are other important flags missing too? */
841 TREE_SIDE_EFFECTS (nval) = TREE_SIDE_EFFECTS (val);
845 nval = build1 (INDIRECT_REF, target_type, val);
847 TREE_THIS_VOLATILE (nval) = TYPE_VOLATILE (target_type);
848 TREE_SIDE_EFFECTS (nval) = TYPE_VOLATILE (target_type);
849 TREE_READONLY (nval) = TYPE_READONLY (target_type);
850 /* The below was missing, are other important flags missing too? */
851 TREE_SIDE_EFFECTS (nval) |= TREE_SIDE_EFFECTS (val);
857 /* See if there is a constructor of type TYPE which will convert
858 EXPR. The reference manual seems to suggest (8.5.6) that we need
859 not worry about finding constructors for base classes, then converting
860 to the derived class.
862 MSGP is a pointer to a message that would be an appropriate error
863 string. If MSGP is NULL, then we are not interested in reporting
866 convert_to_aggr (type, expr, msgp, protect)
871 tree basetype = type;
872 tree name = TYPE_IDENTIFIER (basetype);
873 tree function, fndecl, fntype, parmtypes, parmlist, result;
875 enum access_type access;
876 int can_be_private, can_be_protected;
878 if (! TYPE_HAS_CONSTRUCTOR (basetype))
881 *msgp = "type `%s' does not have a constructor";
882 return error_mark_node;
885 access = access_public;
887 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
889 parmlist = build_tree_list (NULL_TREE, expr);
890 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
892 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
894 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
895 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
898 /* The type of the first argument will be filled in inside the loop. */
899 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
900 parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
902 method_name = build_decl_overload (name, parmtypes, 1);
904 /* constructors are up front. */
905 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
906 if (TYPE_HAS_DESTRUCTOR (basetype))
907 fndecl = DECL_CHAIN (fndecl);
911 if (DECL_ASSEMBLER_NAME (fndecl) == method_name)
916 if (TREE_PRIVATE (fndecl))
919 (basetype == current_class_type
920 || is_friend (basetype, current_function_decl)
921 || purpose_member (basetype, DECL_ACCESS (fndecl)));
922 if (! can_be_private)
925 else if (TREE_PROTECTED (fndecl))
927 if (! can_be_protected)
933 fndecl = DECL_CHAIN (fndecl);
936 /* No exact conversion was found. See if an approximate
938 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
939 if (TYPE_HAS_DESTRUCTOR (basetype))
940 fndecl = DECL_CHAIN (fndecl);
944 int saw_protected = 0;
945 struct candidate *candidates =
946 (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
947 struct candidate *cp = candidates;
953 cp->harshness = (struct harshness_code *)
954 alloca (3 * sizeof (struct harshness_code));
956 compute_conversion_costs (fndecl, parmlist, cp, 2);
957 if ((cp->h.code & EVIL_CODE) == 0)
959 cp->u.field = fndecl;
962 if (TREE_PRIVATE (fndecl))
963 access = access_private;
964 else if (TREE_PROTECTED (fndecl))
965 access = access_protected;
967 access = access_public;
970 access = access_public;
972 if (access == access_private
973 ? (basetype == current_class_type
974 || is_friend (basetype, cp->function)
975 || purpose_member (basetype, DECL_ACCESS (fndecl)))
976 : access == access_protected
978 || purpose_member (basetype, DECL_ACCESS (fndecl)))
981 if (cp->h.code <= TRIVIAL_CODE)
987 if (access == access_private)
993 fndecl = DECL_CHAIN (fndecl);
997 /* Rank from worst to best. Then cp will point to best one.
998 Private fields have their bits flipped. For unsigned
999 numbers, this should make them look very large.
1000 If the best alternate has a (signed) negative value,
1001 then all we ever saw were private members. */
1002 if (cp - candidates > 1)
1003 qsort (candidates, /* char *base */
1004 cp - candidates, /* int nel */
1005 sizeof (struct candidate), /* int width */
1006 rank_for_overload); /* int (*compar)() */
1009 if (cp->h.code & EVIL_CODE)
1012 *msgp = "ambiguous type conversion possible for `%s'";
1013 return error_mark_node;
1016 function = cp->function;
1017 fndecl = cp->u.field;
1024 *msgp = "only private and protected conversions apply";
1026 *msgp = "only private conversions apply";
1027 else if (saw_protected)
1028 *msgp = "only protected conversions apply";
1030 *msgp = "no appropriate conversion to type `%s'";
1032 return error_mark_node;
1037 if (access == access_private)
1038 if (! can_be_private)
1041 *msgp = TREE_PRIVATE (fndecl)
1042 ? "conversion to type `%s' is private"
1043 : "conversion to type `%s' is from private base class";
1044 return error_mark_node;
1046 if (access == access_protected)
1047 if (! can_be_protected)
1050 *msgp = TREE_PRIVATE (fndecl)
1051 ? "conversion to type `%s' is protected"
1052 : "conversion to type `%s' is from protected base class";
1053 return error_mark_node;
1058 /* It will convert, but we don't do anything about it yet. */
1062 fntype = TREE_TYPE (function);
1063 if (DECL_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
1064 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1066 function = default_conversion (function);
1068 result = build_nt (CALL_EXPR, function,
1069 convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1070 parmlist, NULL_TREE, LOOKUP_NORMAL),
1072 TREE_TYPE (result) = TREE_TYPE (fntype);
1073 TREE_SIDE_EFFECTS (result) = 1;
1074 TREE_RAISES (result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
1078 /* Call this when we know (for any reason) that expr is not, in fact,
1079 zero. This routine is like convert_pointer_to, but it pays
1080 attention to which specific instance of what type we want to
1081 convert to. This routine should eventually become
1082 convert_to_pointer after all references to convert_to_pointer
1085 convert_pointer_to_real (binfo, expr)
1088 register tree intype = TREE_TYPE (expr);
1092 if (TREE_CODE (binfo) == TREE_VEC)
1093 type = BINFO_TYPE (binfo);
1094 else if (IS_AGGR_TYPE (binfo))
1104 ptr_type = build_pointer_type (type);
1105 if (ptr_type == TYPE_MAIN_VARIANT (intype))
1108 if (intype == error_mark_node)
1109 return error_mark_node;
1111 my_friendly_assert (!integer_zerop (expr), 191);
1113 if (TREE_CODE (type) == RECORD_TYPE
1114 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1115 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1119 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1122 /* This function shouldn't be called with unqualified arguments
1123 but if it is, give them an error message that they can read. */
1126 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1127 TREE_TYPE (intype), type);
1130 cp_error ("because `%T' is an ambiguous base class", type);
1131 return error_mark_node;
1134 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1136 rval = build1 (NOP_EXPR, ptr_type,
1137 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1138 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1142 /* Call this when we know (for any reason) that expr is
1143 not, in fact, zero. This routine gets a type out of the first
1144 argument and uses it to search for the type to convert to. If there
1145 is more than one instance of that type in the expr, the conversion is
1146 ambiguous. This routine should eventually go away, and all
1147 callers should use convert_to_pointer_real. */
1149 convert_pointer_to (binfo, expr)
1154 if (TREE_CODE (binfo) == TREE_VEC)
1155 type = BINFO_TYPE (binfo);
1156 else if (IS_AGGR_TYPE (binfo))
1160 return convert_pointer_to_real (type, expr);
1163 /* Same as above, but don't abort if we get an "ambiguous" baseclass.
1164 There's only one virtual baseclass we are looking for, and once
1165 we find one such virtual baseclass, we have found them all. */
1168 convert_pointer_to_vbase (binfo, expr)
1172 tree intype = TREE_TYPE (TREE_TYPE (expr));
1173 tree binfos = TYPE_BINFO_BASETYPES (intype);
1176 for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
1178 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1179 if (BINFO_TYPE (binfo) == basetype)
1180 return convert_pointer_to (binfo, expr);
1181 if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype)))
1182 return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr));
1184 my_friendly_abort (6);
1190 cp_convert (type, expr, convtype, flags)
1192 int convtype, flags;
1194 register tree e = expr;
1195 register enum tree_code code = TREE_CODE (type);
1197 if (type == TREE_TYPE (e)
1198 || TREE_CODE (e) == ERROR_MARK)
1200 if (TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
1201 return error_mark_node;
1203 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
1204 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
1205 return fold (build1 (NOP_EXPR, type, e));
1207 if (code == VOID_TYPE && (convtype & CONV_STATIC))
1208 return build1 (CONVERT_EXPR, type, e);
1211 /* This is incorrect. A truncation can't be stripped this way.
1212 Extensions will be stripped by the use of get_unwidened. */
1213 if (TREE_CODE (e) == NOP_EXPR)
1214 return convert (type, TREE_OPERAND (e, 0));
1217 /* Just convert to the type of the member. */
1218 if (code == OFFSET_TYPE)
1220 type = TREE_TYPE (type);
1221 code = TREE_CODE (type);
1224 if (code == REFERENCE_TYPE)
1225 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1226 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1227 e = convert_from_reference (e);
1229 if (TREE_READONLY_DECL_P (e))
1230 e = decl_constant_value (e);
1232 if (INTEGRAL_CODE_P (code))
1234 tree intype = TREE_TYPE (e);
1235 enum tree_code form = TREE_CODE (intype);
1236 /* enum = enum, enum = int, enum = float are all errors. */
1237 if (flag_int_enum_equivalence == 0
1238 && TREE_CODE (type) == ENUMERAL_TYPE
1239 && ARITHMETIC_TYPE_P (intype))
1241 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1243 if (flag_pedantic_errors)
1244 return error_mark_node;
1246 if (form == OFFSET_TYPE)
1247 cp_error_at ("pointer-to-member expression object not composed with type `%D' object",
1248 TYPE_NAME (TYPE_OFFSET_BASETYPE (intype)));
1249 else if (IS_AGGR_TYPE (intype))
1252 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1253 if (rval) return rval;
1254 if (code == BOOLEAN_TYPE)
1255 cp_error ("`%#T' used where a `bool' was expected", intype);
1257 cp_error ("`%#T' used where an `int' was expected", intype);
1258 return error_mark_node;
1260 if (code == BOOLEAN_TYPE)
1262 tree newe = truthvalue_conversion (e);
1263 /* Avoid stupid (infinite) recursion from backend. */
1264 if (TREE_CODE (newe) != NOP_EXPR || e != TREE_OPERAND (newe, 0))
1266 if (TREE_TYPE (e) == bool_type_node)
1268 else if (TREE_CODE (e) == INTEGER_CST)
1270 if (e == integer_zero_node)
1276 return build1 (NOP_EXPR, bool_type_node, e);
1278 return fold (convert_to_integer (type, e));
1280 if (code == POINTER_TYPE)
1281 return fold (cp_convert_to_pointer (type, e));
1282 if (code == REAL_TYPE)
1284 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1287 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1291 cp_error ("`%#T' used where a floating point value was expected",
1294 return fold (convert_to_real (type, e));
1297 /* New C++ semantics: since assignment is now based on
1298 memberwise copying, if the rhs type is derived from the
1299 lhs type, then we may still do a conversion. */
1300 if (IS_AGGR_TYPE_CODE (code))
1302 tree dtype = TREE_TYPE (e);
1304 if (TREE_CODE (dtype) == REFERENCE_TYPE)
1306 e = convert_from_reference (e);
1307 dtype = TREE_TYPE (e);
1309 dtype = TYPE_MAIN_VARIANT (dtype);
1311 /* Conversion of object pointers or signature pointers/references
1312 to signature pointers/references. */
1314 if (TYPE_LANG_SPECIFIC (type)
1315 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1317 tree constructor = build_signature_pointer_constructor (type, expr);
1318 tree sig_ty = SIGNATURE_TYPE (type);
1321 if (constructor == error_mark_node)
1322 return error_mark_node;
1324 sig_ptr = get_temp_name (type, 1);
1325 DECL_INITIAL (sig_ptr) = constructor;
1326 CLEAR_SIGNATURE (sig_ty);
1327 finish_decl (sig_ptr, constructor, 0, 0);
1328 SET_SIGNATURE (sig_ty);
1329 TREE_READONLY (sig_ptr) = 1;
1334 /* Conversion between aggregate types. New C++ semantics allow
1335 objects of derived type to be cast to objects of base type.
1336 Old semantics only allowed this between pointers.
1338 There may be some ambiguity between using a constructor
1339 vs. using a type conversion operator when both apply. */
1341 else if (IS_AGGR_TYPE (dtype))
1347 if (! DERIVED_FROM_P (type, dtype) && TYPE_HAS_CONVERSION (dtype))
1348 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1350 conversion = NULL_TREE;
1352 if (TYPE_HAS_CONSTRUCTOR (type))
1354 tree rval = build_method_call (NULL_TREE, constructor_name_full (type),
1355 build_tree_list (NULL_TREE, e),
1357 conversion ? LOOKUP_NO_CONVERSION : 0);
1359 if (rval != error_mark_node)
1363 error ("both constructor and type conversion operator apply");
1364 return error_mark_node;
1366 /* call to constructor successful. */
1367 rval = build_cplus_new (type, rval, 0);
1371 /* Type conversion successful/applies. */
1374 if (conversion == error_mark_node)
1375 error ("ambiguous pointer conversion");
1379 /* now try normal C++ assignment semantics. */
1380 binfo = TYPE_BINFO (dtype);
1381 if (BINFO_TYPE (binfo) == type
1382 || (binfo = get_binfo (type, dtype, 1)))
1384 if (binfo == error_mark_node)
1385 return error_mark_node;
1387 if (binfo != NULL_TREE)
1391 e = build_unary_op (ADDR_EXPR, e, 0);
1393 if (! BINFO_OFFSET_ZEROP (binfo))
1394 e = build (PLUS_EXPR, TYPE_POINTER_TO (type),
1395 e, BINFO_OFFSET (binfo));
1396 return build1 (INDIRECT_REF, type, e);
1399 sorry ("addressable aggregates");
1400 return error_mark_node;
1402 error ("conversion between incompatible aggregate types requested");
1403 return error_mark_node;
1405 /* conversion from non-aggregate to aggregate type requires
1407 else if (TYPE_HAS_CONSTRUCTOR (type))
1410 tree init = build_method_call (NULL_TREE, constructor_name_full (type),
1411 build_tree_list (NULL_TREE, e),
1412 TYPE_BINFO (type), LOOKUP_NORMAL);
1413 if (init == error_mark_node)
1415 cp_error ("in conversion to type `%T'", type);
1416 return error_mark_node;
1418 rval = build_cplus_new (type, init, 0);
1423 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1424 then the it won't be hashed and hence compare as not equal,
1426 if (code == ARRAY_TYPE
1427 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1428 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1431 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1432 TREE_TYPE (expr), type);
1433 return error_mark_node;
1436 /* Create an expression whose value is that of EXPR,
1437 converted to type TYPE. The TREE_TYPE of the value
1438 is always TYPE. This function implements all reasonable
1439 conversions; callers should filter out those that are
1440 not permitted by the language being compiled. */
1443 convert (type, expr)
1446 return cp_convert (type, expr, CONV_OLD_CONVERT, 0);
1449 /* Like convert, except permit conversions to take place which
1450 are not normally allowed due to access restrictions
1451 (such as conversion from sub-type to private super-type). */
1453 convert_force (type, expr)
1457 register tree e = expr;
1458 register enum tree_code code = TREE_CODE (type);
1460 if (code == REFERENCE_TYPE)
1461 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1463 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1464 e = convert_from_reference (e);
1466 if (code == POINTER_TYPE)
1467 return fold (convert_to_pointer_force (type, e));
1469 /* From typeck.c convert_for_assignment */
1470 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1471 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1472 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1473 || integer_zerop (e)
1474 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1475 && TYPE_PTRMEMFUNC_P (type))
1477 /* compatible pointer to member functions. */
1478 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1481 int old_equiv = flag_int_enum_equivalence;
1482 flag_int_enum_equivalence = 1;
1483 e = convert (type, e);
1484 flag_int_enum_equivalence = old_equiv;
1489 /* Subroutine of build_type_conversion. */
1491 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1492 tree xtype, basetype;
1497 tree first_arg = expr;
1503 if (! lvalue_p (expr))
1504 first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
1505 flags = LOOKUP_PROTECT;
1508 flags = LOOKUP_NORMAL;
1510 rval = build_method_call (first_arg, typename, NULL_TREE, NULL_TREE, flags);
1511 if (rval == error_mark_node)
1515 return error_mark_node;
1517 if (first_arg != expr)
1519 expr = build_up_reference (build_reference_type (TREE_TYPE (expr)), expr,
1520 LOOKUP_COMPLAIN, 1);
1521 TREE_VALUE (TREE_OPERAND (rval, 1)) = build_unary_op (ADDR_EXPR, expr, 0);
1523 if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE
1524 && TREE_CODE (xtype) != REFERENCE_TYPE)
1525 rval = default_conversion (rval);
1528 && TREE_TYPE (xtype)
1529 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1530 > TREE_READONLY (TREE_TYPE (xtype))))
1531 warning ("user-defined conversion casting away `const'");
1532 return convert (xtype, rval);
1535 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1536 exists, return the attempted conversion. This may
1537 return ERROR_MARK_NODE if the conversion is not
1538 allowed (references private members, etc).
1539 If no conversion exists, NULL_TREE is returned.
1541 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1542 to take place immediately. Otherwise, we build a SAVE_EXPR
1543 which can be evaluated if the results are ever needed.
1545 If FOR_SURE >= 2, then we only look for exact conversions.
1547 TYPE may be a reference type, in which case we first look
1548 for something that will convert to a reference type. If
1549 that fails, we will try to look for something of the
1550 reference's target type, and then return a reference to that. */
1552 build_type_conversion (code, xtype, expr, for_sure)
1553 enum tree_code code;
1557 /* C++: check to see if we can convert this aggregate type
1558 into the required scalar type. */
1559 tree type, type_default;
1560 tree typename = build_typename_overload (xtype), *typenames;
1562 tree basetype, save_basetype;
1564 int exact_conversion = for_sure >= 2;
1567 if (expr == error_mark_node)
1568 return error_mark_node;
1570 basetype = TREE_TYPE (expr);
1571 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1572 basetype = TREE_TYPE (basetype);
1574 if (TYPE_PTRMEMFUNC_P (basetype) && TREE_CODE (xtype) == BOOLEAN_TYPE)
1576 /* We convert a pointer to member function into a boolean,
1577 by just checking the index value, for == 0, we want false, for
1578 != 0, we want true. */
1579 return convert (xtype, build_component_ref (expr, index_identifier, 0, 0));
1582 basetype = TYPE_MAIN_VARIANT (basetype);
1583 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1586 if (TREE_CODE (xtype) == POINTER_TYPE
1587 || TREE_CODE (xtype) == REFERENCE_TYPE)
1589 /* Prepare to match a variant of this type. */
1590 type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
1591 for (n_variants = 0; type; type = TYPE_NEXT_VARIANT (type))
1593 typenames = (tree *)alloca (n_variants * sizeof (tree));
1594 for (n_variants = 0, type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
1595 type; n_variants++, type = TYPE_NEXT_VARIANT (type))
1597 if (type == TREE_TYPE (xtype))
1598 typenames[n_variants] = typename;
1599 else if (TREE_CODE (xtype) == POINTER_TYPE)
1600 typenames[n_variants] = build_typename_overload (build_pointer_type (type));
1602 typenames[n_variants] = build_typename_overload (build_reference_type (type));
1606 save_basetype = basetype;
1609 while (TYPE_HAS_CONVERSION (basetype))
1612 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1613 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1614 for (i = 0; i < n_variants; i++)
1615 if (typenames[i] != typename
1616 && lookup_fnfields (TYPE_BINFO (basetype), typenames[i], 0))
1617 return build_type_conversion_1 (xtype, basetype, expr, typenames[i], for_sure);
1619 if (TYPE_BINFO_BASETYPES (basetype))
1620 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1625 if (TREE_CODE (type) == REFERENCE_TYPE)
1627 tree first_arg = expr;
1628 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1629 basetype = save_basetype;
1631 /* May need to build a temporary for this. */
1632 while (TYPE_HAS_CONVERSION (basetype))
1634 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1640 if (! lvalue_p (expr))
1641 first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
1642 flags = LOOKUP_PROTECT;
1645 flags = LOOKUP_NORMAL;
1646 rval = build_method_call (first_arg, constructor_name_full (typename),
1647 NULL_TREE, NULL_TREE, flags);
1648 if (rval == error_mark_node)
1652 return error_mark_node;
1654 TREE_VALUE (TREE_OPERAND (rval, 1)) = expr;
1656 if (IS_AGGR_TYPE (type))
1658 tree init = build_method_call (NULL_TREE,
1659 constructor_name_full (type),
1660 build_tree_list (NULL_TREE, rval), NULL_TREE, LOOKUP_NORMAL);
1661 tree temp = build_cplus_new (type, init, 1);
1662 return build_up_reference (TYPE_REFERENCE_TO (type), temp,
1663 LOOKUP_COMPLAIN, 1);
1665 return convert (xtype, rval);
1667 if (TYPE_BINFO_BASETYPES (basetype))
1668 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1672 /* No free conversions for reference types, right?. */
1676 if (exact_conversion)
1679 if (TREE_CODE (type) == BOOLEAN_TYPE)
1681 tree as_int = build_type_conversion (code, long_long_unsigned_type_node, expr, 0);
1682 tree as_ptr = build_type_conversion (code, ptr_type_node, expr, 0);
1683 /* We are missing the conversion to pointer to member type. */
1684 /* We are missing the conversion to floating type. */
1685 if (as_int && as_ptr && for_sure)
1687 cp_error ("ambiguous conversion from `%T' to `bool', can convert to integral type or pointer", TREE_TYPE (expr));
1688 return error_mark_node;
1692 as_int = build_type_conversion (code, long_long_unsigned_type_node, expr, for_sure+exact_conversion*2);
1693 return convert (xtype, as_int);
1697 as_ptr = build_type_conversion (code, ptr_type_node, expr, for_sure+exact_conversion*2);
1698 return convert (xtype, as_ptr);
1703 /* No perfect match found, try default. */
1704 #if 0 /* This is wrong; there is no standard conversion from void* to
1706 if (code == CONVERT_EXPR && TREE_CODE (type) == POINTER_TYPE)
1707 type_default = ptr_type_node;
1710 if (type == void_type_node)
1714 tree tmp = default_conversion (build1 (NOP_EXPR, type, integer_zero_node));
1715 if (tmp == error_mark_node)
1717 type_default = TREE_TYPE (tmp);
1720 basetype = save_basetype;
1722 if (type_default != type)
1724 type = type_default;
1725 typename = build_typename_overload (type);
1727 while (TYPE_HAS_CONVERSION (basetype))
1729 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1730 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1731 if (TYPE_BINFO_BASETYPES (basetype))
1732 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1738 if (TREE_CODE (type) == POINTER_TYPE && TYPE_READONLY (TREE_TYPE (type)))
1740 /* Try converting to some other const pointer type and then using
1741 standard conversions. */
1743 while (TYPE_HAS_CONVERSION (basetype))
1745 if (CLASSTYPE_CONVERSION (basetype, constptr_conv) != 0)
1747 if (CLASSTYPE_CONVERSION (basetype, constptr_conv) == error_mark_node)
1748 return error_mark_node;
1749 typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, constptr_conv));
1750 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1752 if (TYPE_BINFO_BASETYPES (basetype))
1753 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1758 if (TREE_CODE (type) == POINTER_TYPE)
1760 /* Try converting to some other pointer type and then using standard
1763 while (TYPE_HAS_CONVERSION (basetype))
1765 if (CLASSTYPE_CONVERSION (basetype, ptr_conv) != 0)
1767 if (CLASSTYPE_CONVERSION (basetype, ptr_conv) == error_mark_node)
1768 return error_mark_node;
1769 typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, ptr_conv));
1770 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1772 if (TYPE_BINFO_BASETYPES (basetype))
1773 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1779 /* Use the longer or shorter conversion that is appropriate. Have
1780 to check against 0 because the conversion may come from a baseclass. */
1781 if (TREE_CODE (type) == INTEGER_TYPE
1782 && TYPE_HAS_INT_CONVERSION (basetype)
1783 && CLASSTYPE_CONVERSION (basetype, int_conv) != 0
1784 && CLASSTYPE_CONVERSION (basetype, int_conv) != error_mark_node)
1786 typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, int_conv));
1787 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1790 if (TREE_CODE (type) == REAL_TYPE
1791 && TYPE_HAS_REAL_CONVERSION (basetype)
1792 && CLASSTYPE_CONVERSION (basetype, real_conv) != 0
1793 && CLASSTYPE_CONVERSION (basetype, real_conv) != error_mark_node)
1795 /* Only accept using an operator double() if there isn't a conflicting
1797 if (TYPE_HAS_INT_CONVERSION (basetype))
1801 cp_error ("two possible conversions for type `%T'", type);
1802 return error_mark_node;
1808 typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, real_conv));
1809 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1812 /* THESE ARE TOTAL KLUDGES. */
1813 /* Default promotion yields no new alternatives, try
1814 conversions which are anti-default, such as
1816 double -> float or int -> unsigned or unsigned -> long
1819 if (type_default == type
1820 && (INTEGRAL_TYPE_P (type) || TREE_CODE (type) == REAL_TYPE))
1824 if (type == double_type_node)
1825 typename = build_typename_overload (float_type_node);
1826 else if (type == integer_type_node)
1827 typename = build_typename_overload (unsigned_type_node);
1828 else if (type == unsigned_type_node)
1829 typename = build_typename_overload (long_integer_type_node);
1832 basetype = save_basetype;
1833 while (TYPE_HAS_CONVERSION (basetype))
1835 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1836 return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1837 if (TYPE_BINFO_BASETYPES (basetype))
1838 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1844 if (type == integer_type_node)
1846 typename = build_typename_overload (long_integer_type_node);
1852 typename = build_typename_overload (integer_type_node);
1859 /* Now, try C promotions...
1864 basetype = save_basetype;
1865 if (TREE_CODE (type) == REAL_TYPE)
1866 type = integer_type_node;
1867 else if (TREE_CODE (type) == INTEGER_TYPE)
1868 if (TYPE_HAS_REAL_CONVERSION (basetype))
1869 type = double_type_node;
1875 typename = build_typename_overload (type);
1876 while (TYPE_HAS_CONVERSION (basetype))
1878 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1880 rval = build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
1883 if (TYPE_BINFO_BASETYPES (basetype))
1884 basetype = TYPE_BINFO_BASETYPE (basetype, 0);
1892 /* Must convert two aggregate types to non-aggregate type.
1893 Attempts to find a non-ambiguous, "best" type conversion.
1895 Return 1 on success, 0 on failure.
1897 @@ What are the real semantics of this supposed to be??? */
1899 build_default_binary_type_conversion (code, arg1, arg2)
1900 enum tree_code code;
1903 tree type1 = TREE_TYPE (*arg1);
1904 tree type2 = TREE_TYPE (*arg2);
1906 if (TREE_CODE (type1) == REFERENCE_TYPE
1907 || TREE_CODE (type1) == POINTER_TYPE)
1908 type1 = TREE_TYPE (type1);
1909 if (TREE_CODE (type2) == REFERENCE_TYPE
1910 || TREE_CODE (type2) == POINTER_TYPE)
1911 type2 = TREE_TYPE (type2);
1913 if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL)
1915 tree decl = typedecl_for_tag (type1);
1917 error ("type conversion nonexistent for type `%s'",
1918 IDENTIFIER_POINTER (DECL_NAME (decl)));
1920 error ("type conversion nonexistent for non-C++ type");
1923 if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL)
1925 tree decl = typedecl_for_tag (type2);
1927 error ("type conversion nonexistent for type `%s'",
1928 IDENTIFIER_POINTER (decl));
1930 error ("type conversion nonexistent for non-C++ type");
1934 if (!IS_AGGR_TYPE (type1) || !TYPE_HAS_CONVERSION (type1))
1936 if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
1937 cp_error ("no conversion from `%T' and `%T' to types with default `%O' ",
1938 type1, type2, code);
1940 cp_error ("no conversion from `%T' to type with default `%O'",
1944 else if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
1946 cp_error ("no conversion from `%T' to type with default `%O'",
1951 if (code == TRUTH_ANDIF_EXPR
1952 || code == TRUTH_ORIF_EXPR)
1954 *arg1 = convert (bool_type_node, *arg1);
1955 *arg2 = convert (bool_type_node, *arg2);
1957 else if (TYPE_HAS_INT_CONVERSION (type1))
1959 if (TYPE_HAS_REAL_CONVERSION (type1))
1960 cp_pedwarn ("ambiguous type conversion for type `%T', defaulting to int",
1962 *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1);
1963 *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1);
1965 else if (TYPE_HAS_REAL_CONVERSION (type1))
1967 *arg1 = build_type_conversion (code, double_type_node, *arg1, 1);
1968 *arg2 = build_type_conversion (code, double_type_node, *arg2, 1);
1972 *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1);
1973 if (*arg1 == error_mark_node)
1974 error ("ambiguous pointer conversion");
1975 *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1);
1976 if (*arg1 != error_mark_node && *arg2 == error_mark_node)
1977 error ("ambiguous pointer conversion");
1981 if (*arg2 == 0 && type1 != type2)
1982 cp_error ("default type conversion for types `%T' and `%T' failed",
1985 cp_error ("default type conversion for type `%T' failed", type1);
1988 else if (*arg2 == 0)
1990 cp_error ("default type conversion for type `%T' failed", type2);
1996 /* Must convert an aggregate type to non-aggregate type.
1997 Attempts to find a non-ambiguous, "best" type conversion.
1999 Return 1 on success, 0 on failure.
2001 The type of the argument is expected to be of aggregate type here.
2003 @@ What are the real semantics of this supposed to be??? */
2005 build_default_unary_type_conversion (code, arg)
2006 enum tree_code code;
2009 tree type = TREE_TYPE (*arg);
2011 if (! TYPE_HAS_CONVERSION (type))
2013 cp_error ("type conversion required for type `%T'", type);
2017 if (code == TRUTH_NOT_EXPR)
2018 *arg = convert (bool_type_node, *arg);
2019 else if (TYPE_HAS_INT_CONVERSION (type))
2021 if (TYPE_HAS_REAL_CONVERSION (type))
2022 cp_pedwarn ("ambiguous type conversion for type `%T', defaulting to int",
2024 *arg = build_type_conversion (code, integer_type_node, *arg, 1);
2026 else if (TYPE_HAS_REAL_CONVERSION (type))
2027 *arg = build_type_conversion (code, double_type_node, *arg, 1);
2030 *arg = build_type_conversion (code, ptr_type_node, *arg, 1);
2031 if (*arg == error_mark_node)
2032 error ("ambiguous pointer conversion");
2034 if (*arg == NULL_TREE)
2036 cp_error ("default type conversion for type `%T' failed", type);
2042 /* Implements integral promotion (4.1) and float->double promotion. */
2044 type_promotes_to (type)
2047 int constp = TYPE_READONLY (type);
2048 int volatilep = TYPE_VOLATILE (type);
2049 type = TYPE_MAIN_VARIANT (type);
2051 /* bool always promotes to int (not unsigned), even if it's the same
2053 if (type == bool_type_node)
2054 type = integer_type_node;
2056 /* Normally convert enums to int, but convert wide enums to something
2058 else if (TREE_CODE (type) == ENUMERAL_TYPE
2059 || type == wchar_type_node)
2060 type = type_for_size
2061 (MAX (TYPE_PRECISION (type), TYPE_PRECISION (integer_type_node)),
2063 || (TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node)))
2064 && TREE_UNSIGNED (type));
2065 else if (C_PROMOTING_INTEGER_TYPE_P (type))
2067 /* Traditionally, unsignedness is preserved in default promotions.
2068 Otherwise, retain unsignedness if really not getting bigger. */
2069 if (TREE_UNSIGNED (type)
2070 && (flag_traditional
2071 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
2072 type = unsigned_type_node;
2074 type = integer_type_node;
2076 else if (type == float_type_node)
2077 type = double_type_node;
2079 return build_type_variant (type, constp, volatilep);