* except.c (use_eh_context): Don't copy_rtx a REG.
[official-gcc.git] / gcc / cp / cvt.c
blob299649280055997126d30ac20f4af55ace03b0fd
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)
10 any later version.
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. */
28 #include "config.h"
29 #include <stdio.h>
30 #include "tree.h"
31 #include "flags.h"
32 #include "cp-tree.h"
33 #include "class.h"
34 #include "convert.h"
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
40 extern tree static_aggregates;
42 static tree build_thunk PROTO((tree, tree));
43 static tree convert_fn_ptr PROTO((tree, tree));
44 static tree cp_convert_to_pointer PROTO((tree, tree));
45 static tree convert_to_pointer_force PROTO((tree, tree));
46 static tree build_up_reference PROTO((tree, tree, int, int));
47 static tree build_type_conversion_1 PROTO((tree, tree, tree, tree,
48 int));
50 /* Change of width--truncation and extension of integers or reals--
51 is represented with NOP_EXPR. Proper functioning of many things
52 assumes that no other conversions can be NOP_EXPRs.
54 Conversion between integer and pointer is represented with CONVERT_EXPR.
55 Converting integer to real uses FLOAT_EXPR
56 and real to integer uses FIX_TRUNC_EXPR.
58 Here is a list of all the functions that assume that widening and
59 narrowing is always done with a NOP_EXPR:
60 In convert.c, convert_to_integer.
61 In c-typeck.c, build_binary_op_nodefault (boolean ops),
62 and truthvalue_conversion.
63 In expr.c: expand_expr, for operands of a MULT_EXPR.
64 In fold-const.c: fold.
65 In tree.c: get_narrower and get_unwidened.
67 C++: in multiple-inheritance, converting between pointers may involve
68 adjusting them by a delta stored within the class definition. */
70 /* Subroutines of `convert'. */
72 /* Build a thunk. What it is, is an entry point that when called will
73 adjust the this pointer (the first argument) by offset, and then
74 goto the real address of the function given by REAL_ADDR that we
75 would like called. What we return is the address of the thunk. */
77 static tree
78 build_thunk (offset, real_addr)
79 tree offset, real_addr;
81 if (TREE_CODE (real_addr) != ADDR_EXPR
82 || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
84 sorry ("MI pointer to member conversion too complex");
85 return error_mark_node;
87 sorry ("MI pointer to member conversion too complex");
88 return error_mark_node;
91 /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
92 another `pointer to method'. This may involved the creation of
93 a thunk to handle the this offset calculation. */
95 static tree
96 convert_fn_ptr (type, expr)
97 tree type, expr;
99 #if 0 /* We don't use thunks for pmfs. */
100 if (flag_vtable_thunks)
102 tree intype = TREE_TYPE (expr);
103 tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype)),
104 TYPE_METHOD_BASETYPE (TREE_TYPE (type)), 1);
105 if (binfo == error_mark_node)
107 error (" in pointer to member conversion");
108 return error_mark_node;
110 if (binfo == NULL_TREE)
112 /* ARM 4.8 restriction. */
113 error ("invalid pointer to member conversion");
114 return error_mark_node;
117 if (BINFO_OFFSET_ZEROP (binfo))
118 return build1 (NOP_EXPR, type, expr);
119 return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
121 else
122 #endif
123 return build_ptrmemfunc (type, expr, 1);
126 /* if converting pointer to pointer
127 if dealing with classes, check for derived->base or vice versa
128 else if dealing with method pointers, delegate
129 else convert blindly
130 else if converting class, pass off to build_type_conversion
131 else try C-style pointer conversion */
133 static tree
134 cp_convert_to_pointer (type, expr)
135 tree type, expr;
137 register tree intype = TREE_TYPE (expr);
138 register enum tree_code form;
140 if (IS_AGGR_TYPE (intype))
142 tree rval;
144 intype = complete_type (intype);
145 if (TYPE_SIZE (intype) == NULL_TREE)
147 cp_error ("can't convert from incomplete type `%T' to `%T'",
148 intype, type);
149 return error_mark_node;
152 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
153 if (rval)
155 if (rval == error_mark_node)
156 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
157 expr, intype, type);
158 return rval;
162 if (TYPE_PTRMEMFUNC_P (type))
163 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
165 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
166 if (TREE_CODE (type) == POINTER_TYPE
167 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
168 || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node))
170 /* Allow an implicit this pointer for pointer to member
171 functions. */
172 if (TYPE_PTRMEMFUNC_P (intype))
174 tree decl, basebinfo;
175 tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
176 tree t = TYPE_METHOD_BASETYPE (fntype);
178 if (current_class_type == 0
179 || get_base_distance (t, current_class_type, 0, &basebinfo)
180 == -1)
182 decl = build1 (NOP_EXPR, t, error_mark_node);
184 else if (current_class_ptr == 0)
185 decl = build1 (NOP_EXPR, t, error_mark_node);
186 else
187 decl = current_class_ref;
189 expr = build (OFFSET_REF, fntype, decl, expr);
192 if (TREE_CODE (expr) == OFFSET_REF
193 && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
194 expr = resolve_offset_ref (expr);
195 if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
196 expr = build_addr_func (expr);
197 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
199 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)
200 if (pedantic || warn_pmf2ptr)
201 cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),
202 type);
203 return build1 (NOP_EXPR, type, expr);
205 intype = TREE_TYPE (expr);
208 if (TYPE_PTRMEMFUNC_P (intype))
209 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
211 form = TREE_CODE (intype);
213 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
215 intype = TYPE_MAIN_VARIANT (intype);
217 if (TYPE_MAIN_VARIANT (type) != intype
218 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
219 && IS_AGGR_TYPE (TREE_TYPE (type))
220 && IS_AGGR_TYPE (TREE_TYPE (intype))
221 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
223 enum tree_code code = PLUS_EXPR;
224 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
225 if (binfo == error_mark_node)
226 return error_mark_node;
227 if (binfo == NULL_TREE)
229 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
230 if (binfo == error_mark_node)
231 return error_mark_node;
232 code = MINUS_EXPR;
234 if (binfo)
236 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
237 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
238 || ! BINFO_OFFSET_ZEROP (binfo))
240 /* Need to get the path we took. */
241 tree path;
243 if (code == PLUS_EXPR)
244 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
245 else
246 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
247 return build_vbase_path (code, type, expr, path, 0);
251 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
252 && TREE_CODE (type) == POINTER_TYPE
253 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
254 return convert_fn_ptr (type, expr);
256 if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
257 && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
259 tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
260 tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
261 tree binfo = get_binfo (b1, b2, 1);
262 if (binfo == NULL_TREE)
263 binfo = get_binfo (b2, b1, 1);
264 if (binfo == error_mark_node)
265 return error_mark_node;
268 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
269 || (TREE_CODE (type) == POINTER_TYPE
270 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
272 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
273 expr, intype, type);
274 return error_mark_node;
277 return build1 (NOP_EXPR, type, expr);
280 my_friendly_assert (form != OFFSET_TYPE, 186);
282 if (TYPE_LANG_SPECIFIC (intype)
283 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
284 return convert_to_pointer (type, build_optr_ref (expr));
286 if (integer_zerop (expr))
288 if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
289 return build_ptrmemfunc (type, expr, 0);
290 expr = build_int_2 (0, 0);
291 TREE_TYPE (expr) = type;
292 return expr;
295 if (INTEGRAL_CODE_P (form))
297 if (type_precision (intype) == POINTER_SIZE)
298 return build1 (CONVERT_EXPR, type, expr);
299 expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr);
300 /* Modes may be different but sizes should be the same. */
301 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
302 != GET_MODE_SIZE (TYPE_MODE (type)))
303 /* There is supposed to be some integral type
304 that is the same width as a pointer. */
305 abort ();
306 return convert_to_pointer (type, expr);
309 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
310 expr, intype, type);
311 return error_mark_node;
314 /* Like convert, except permit conversions to take place which
315 are not normally allowed due to access restrictions
316 (such as conversion from sub-type to private super-type). */
318 static tree
319 convert_to_pointer_force (type, expr)
320 tree type, expr;
322 register tree intype = TREE_TYPE (expr);
323 register enum tree_code form = TREE_CODE (intype);
325 if (integer_zerop (expr))
327 expr = build_int_2 (0, 0);
328 TREE_TYPE (expr) = type;
329 return expr;
332 /* Convert signature pointer/reference to `void *' first. */
333 if (form == RECORD_TYPE
334 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
336 expr = build_optr_ref (expr);
337 intype = TREE_TYPE (expr);
338 form = TREE_CODE (intype);
341 if (form == POINTER_TYPE)
343 intype = TYPE_MAIN_VARIANT (intype);
345 if (TYPE_MAIN_VARIANT (type) != intype
346 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
347 && IS_AGGR_TYPE (TREE_TYPE (type))
348 && IS_AGGR_TYPE (TREE_TYPE (intype))
349 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
351 enum tree_code code = PLUS_EXPR;
352 tree path;
353 int distance = get_base_distance (TREE_TYPE (type),
354 TREE_TYPE (intype), 0, &path);
355 if (distance == -2)
357 ambig:
358 cp_error ("type `%T' is ambiguous baseclass of `%s'",
359 TREE_TYPE (type),
360 TYPE_NAME_STRING (TREE_TYPE (intype)));
361 return error_mark_node;
363 if (distance == -1)
365 distance = get_base_distance (TREE_TYPE (intype),
366 TREE_TYPE (type), 0, &path);
367 if (distance == -2)
368 goto ambig;
369 if (distance < 0)
370 /* Doesn't need any special help from us. */
371 return build1 (NOP_EXPR, type, expr);
373 code = MINUS_EXPR;
375 return build_vbase_path (code, type, expr, path, 0);
379 return cp_convert_to_pointer (type, expr);
382 /* We are passing something to a function which requires a reference.
383 The type we are interested in is in TYPE. The initial
384 value we have to begin with is in ARG.
386 FLAGS controls how we manage access checking.
387 DIRECT_BIND in FLAGS controls how any temporaries are generated. */
389 static tree
390 build_up_reference (type, arg, flags, checkconst)
391 tree type, arg;
392 int flags, checkconst;
394 tree rval;
395 tree argtype = TREE_TYPE (arg);
396 tree target_type = TREE_TYPE (type);
398 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
400 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
402 tree targ = arg;
403 if (toplevel_bindings_p ())
404 arg = get_temp_name (argtype, 1);
405 else
407 arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
408 DECL_ARTIFICIAL (arg) = 1;
410 DECL_INITIAL (arg) = targ;
411 cp_finish_decl (arg, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
413 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
415 tree slot = build_decl (VAR_DECL, NULL_TREE, argtype);
416 arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
419 /* If we had a way to wrap this up, and say, if we ever needed it's
420 address, transform all occurrences of the register, into a memory
421 reference we could win better. */
422 rval = build_unary_op (ADDR_EXPR, arg, 1);
423 if ((flags & LOOKUP_PROTECT)
424 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
425 && IS_AGGR_TYPE (argtype)
426 && IS_AGGR_TYPE (target_type))
428 /* We go through get_binfo for the access control. */
429 tree binfo = get_binfo (target_type, argtype, 1);
430 if (binfo == error_mark_node)
431 return error_mark_node;
432 if (binfo == NULL_TREE)
433 return error_not_base_type (target_type, argtype);
434 rval = convert_pointer_to_real (binfo, rval);
436 else
437 rval
438 = convert_to_pointer_force (build_pointer_type (target_type), rval);
439 rval = build1 (NOP_EXPR, type, rval);
440 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
441 return rval;
444 /* For C++: Only need to do one-level references, but cannot
445 get tripped up on signed/unsigned differences.
447 DECL is either NULL_TREE or the _DECL node for a reference that is being
448 initialized. It can be error_mark_node if we don't know the _DECL but
449 we know it's an initialization. */
451 tree
452 convert_to_reference (reftype, expr, convtype, flags, decl)
453 tree reftype, expr;
454 int convtype, flags;
455 tree decl;
457 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
458 register tree intype = TREE_TYPE (expr);
459 tree rval = NULL_TREE;
460 tree rval_as_conversion = NULL_TREE;
461 int i;
463 if (TREE_CODE (intype) == REFERENCE_TYPE)
464 my_friendly_abort (364);
466 intype = TYPE_MAIN_VARIANT (intype);
468 i = comp_target_types (type, intype, 0);
470 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
471 && ! (flags & LOOKUP_NO_CONVERSION))
473 /* Look for a user-defined conversion to lvalue that we can use. */
475 if (flag_ansi_overloading)
476 rval_as_conversion
477 = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
478 else
479 rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
481 if (rval_as_conversion && rval_as_conversion != error_mark_node
482 && real_lvalue_p (rval_as_conversion))
484 expr = rval_as_conversion;
485 rval_as_conversion = NULL_TREE;
486 intype = type;
487 i = 1;
491 if (((convtype & CONV_STATIC) && i == -1)
492 || ((convtype & CONV_IMPLICIT) && i == 1))
494 if (flags & LOOKUP_COMPLAIN)
496 tree ttl = TREE_TYPE (reftype);
497 tree ttr;
500 int r = TREE_READONLY (expr);
501 int v = TREE_THIS_VOLATILE (expr);
502 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
505 if (! real_lvalue_p (expr) && ! TYPE_READONLY (ttl))
507 if (decl)
508 /* Ensure semantics of [dcl.init.ref] */
509 cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'",
510 reftype, intype);
511 else
512 cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'",
513 reftype, intype);
515 else if (! (convtype & CONV_CONST))
517 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
518 cp_pedwarn ("conversion from `%T' to `%T' discards const",
519 ttr, reftype);
520 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
521 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
522 ttr, reftype);
526 return build_up_reference (reftype, expr, flags,
527 ! (convtype & CONV_CONST));
529 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
531 /* When casting an lvalue to a reference type, just convert into
532 a pointer to the new type and deference it. This is allowed
533 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
534 should be done directly (jason). (int &)ri ---> *(int*)&ri */
536 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
537 meant. */
538 if (TREE_CODE (intype) == POINTER_TYPE
539 && (comptypes (TREE_TYPE (intype), type, -1)))
540 cp_warning ("casting `%T' to `%T' does not dereference pointer",
541 intype, reftype);
543 rval = build_unary_op (ADDR_EXPR, expr, 0);
544 if (rval != error_mark_node)
545 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
546 if (rval != error_mark_node)
547 rval = build1 (NOP_EXPR, reftype, rval);
549 else if (flag_ansi_overloading)
551 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
552 "converting", 0, 0);
553 if (rval == error_mark_node)
554 return error_mark_node;
555 rval = build_up_reference (reftype, rval, flags, 1);
557 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
558 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
559 reftype, intype);
561 else
563 tree rval_as_ctor = NULL_TREE;
565 if (rval_as_conversion)
567 if (rval_as_conversion == error_mark_node)
569 cp_error ("conversion from `%T' to `%T' is ambiguous",
570 intype, reftype);
571 return error_mark_node;
573 rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
574 flags, 1);
577 /* Definitely need to go through a constructor here. */
578 if (TYPE_HAS_CONSTRUCTOR (type)
579 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
580 && (rval = build_method_call
581 (NULL_TREE, ctor_identifier,
582 build_expr_list (NULL_TREE, expr), TYPE_BINFO (type),
583 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
584 | LOOKUP_ONLYCONVERTING)))
586 tree init;
588 if (toplevel_bindings_p ())
590 tree t = get_temp_name (type, toplevel_bindings_p ());
591 init = build_method_call (t, ctor_identifier,
592 build_expr_list (NULL_TREE, expr),
593 TYPE_BINFO (type),
594 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
595 | LOOKUP_ONLYCONVERTING);
597 if (init == error_mark_node)
598 return error_mark_node;
600 make_decl_rtl (t, NULL_PTR, 1);
601 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
602 rval = build_unary_op (ADDR_EXPR, t, 0);
604 else
606 init = build_method_call (NULL_TREE, ctor_identifier,
607 build_expr_list (NULL_TREE, expr),
608 TYPE_BINFO (type),
609 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
610 |LOOKUP_ONLYCONVERTING);
612 if (init == error_mark_node)
613 return error_mark_node;
615 rval = build_cplus_new (type, init);
616 rval = build_up_reference (reftype, rval, flags, 1);
618 rval_as_ctor = rval;
621 if (rval_as_ctor && rval_as_conversion)
623 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
624 intype, reftype);
625 return error_mark_node;
627 else if (rval_as_ctor)
628 rval = rval_as_ctor;
629 else if (rval_as_conversion)
630 rval = rval_as_conversion;
631 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
633 rval = cp_convert (type, expr);
634 if (rval == error_mark_node)
635 return error_mark_node;
637 rval = build_up_reference (reftype, rval, flags, 1);
640 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
641 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
642 reftype, intype);
645 if (rval)
647 /* If we found a way to convert earlier, then use it. */
648 return rval;
651 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
653 if (flags & LOOKUP_COMPLAIN)
654 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
656 if (flags & LOOKUP_SPECULATIVELY)
657 return NULL_TREE;
659 return error_mark_node;
662 /* We are using a reference VAL for its value. Bash that reference all the
663 way down to its lowest form. */
665 tree
666 convert_from_reference (val)
667 tree val;
669 tree type = TREE_TYPE (val);
671 if (TREE_CODE (type) == OFFSET_TYPE)
672 type = TREE_TYPE (type);
673 if (TREE_CODE (type) == REFERENCE_TYPE)
674 return build_indirect_ref (val, NULL_PTR);
675 return val;
678 /* See if there is a constructor of type TYPE which will convert
679 EXPR. The reference manual seems to suggest (8.5.6) that we need
680 not worry about finding constructors for base classes, then converting
681 to the derived class.
683 MSGP is a pointer to a message that would be an appropriate error
684 string. If MSGP is NULL, then we are not interested in reporting
685 errors. */
687 tree
688 convert_to_aggr (type, expr, msgp, protect)
689 tree type, expr;
690 char **msgp;
691 int protect;
693 tree basetype = type;
694 tree name = TYPE_IDENTIFIER (basetype);
695 tree function, fndecl, fntype, parmtypes, parmlist, result;
696 #if 0
697 /* See code below that used this. */
698 tree method_name;
699 #endif
700 tree access;
701 int can_be_private, can_be_protected;
703 if (! TYPE_HAS_CONSTRUCTOR (basetype))
705 if (msgp)
706 *msgp = "type `%s' does not have a constructor";
707 return error_mark_node;
710 access = access_public_node;
711 can_be_private = 0;
712 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
714 parmlist = build_expr_list (NULL_TREE, expr);
715 parmtypes = scratch_tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
717 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
719 parmtypes = expr_tree_cons (NULL_TREE, integer_type_node, parmtypes);
720 parmlist = scratch_tree_cons (NULL_TREE, integer_one_node, parmlist);
723 /* The type of the first argument will be filled in inside the loop. */
724 parmlist = expr_tree_cons (NULL_TREE, integer_zero_node, parmlist);
725 parmtypes = scratch_tree_cons (NULL_TREE, build_pointer_type (basetype), parmtypes);
727 /* No exact conversion was found. See if an approximate
728 one will do. */
729 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
732 int saw_private = 0;
733 int saw_protected = 0;
734 struct candidate *candidates
735 = (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
736 struct candidate *cp = candidates;
738 while (fndecl)
740 function = fndecl;
741 cp->h_len = 2;
742 cp->harshness = (struct harshness_code *)
743 alloca (3 * sizeof (struct harshness_code));
745 compute_conversion_costs (fndecl, parmlist, cp, 2);
746 if ((cp->h.code & EVIL_CODE) == 0)
748 cp->u.field = fndecl;
749 if (protect)
751 if (TREE_PRIVATE (fndecl))
752 access = access_private_node;
753 else if (TREE_PROTECTED (fndecl))
754 access = access_protected_node;
755 else
756 access = access_public_node;
758 else
759 access = access_public_node;
761 if (access == access_private_node
762 ? (basetype == current_class_type
763 || is_friend (basetype, cp->function)
764 || purpose_member (basetype, DECL_ACCESS (fndecl)))
765 : access == access_protected_node
766 ? (can_be_protected
767 || purpose_member (basetype, DECL_ACCESS (fndecl)))
768 : 1)
770 if (cp->h.code <= TRIVIAL_CODE)
771 goto found_and_ok;
772 cp++;
774 else
776 if (access == access_private_node)
777 saw_private = 1;
778 else
779 saw_protected = 1;
782 fndecl = DECL_CHAIN (fndecl);
784 if (cp - candidates)
786 /* Rank from worst to best. Then cp will point to best one.
787 Private fields have their bits flipped. For unsigned
788 numbers, this should make them look very large.
789 If the best alternate has a (signed) negative value,
790 then all we ever saw were private members. */
791 if (cp - candidates > 1)
792 qsort (candidates, /* char *base */
793 cp - candidates, /* int nel */
794 sizeof (struct candidate), /* int width */
795 (int (*) PROTO((const void *, const void *))) rank_for_overload); /* int (*compar)() */
797 --cp;
798 if (cp->h.code & EVIL_CODE)
800 if (msgp)
801 *msgp = "ambiguous type conversion possible for `%s'";
802 return error_mark_node;
805 function = cp->function;
806 fndecl = cp->u.field;
807 goto found_and_ok;
809 else if (msgp)
811 if (saw_private)
812 if (saw_protected)
813 *msgp = "only private and protected conversions apply";
814 else
815 *msgp = "only private conversions apply";
816 else if (saw_protected)
817 *msgp = "only protected conversions apply";
818 else
819 *msgp = "no appropriate conversion to type `%s'";
821 return error_mark_node;
823 /* NOTREACHED */
825 found:
826 if (access == access_private_node)
827 if (! can_be_private)
829 if (msgp)
830 *msgp = TREE_PRIVATE (fndecl)
831 ? "conversion to type `%s' is private"
832 : "conversion to type `%s' is from private base class";
833 return error_mark_node;
835 if (access == access_protected_node)
836 if (! can_be_protected)
838 if (msgp)
839 *msgp = TREE_PRIVATE (fndecl)
840 ? "conversion to type `%s' is protected"
841 : "conversion to type `%s' is from protected base class";
842 return error_mark_node;
844 function = fndecl;
845 found_and_ok:
847 /* It will convert, but we don't do anything about it yet. */
848 if (msgp == 0)
849 return NULL_TREE;
851 fntype = TREE_TYPE (function);
853 parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
854 parmlist, NULL_TREE, LOOKUP_NORMAL);
856 result = build_call (function, TREE_TYPE (fntype), parmlist);
857 return result;
860 /* Call this when we know (for any reason) that expr is not, in fact,
861 zero. This routine is like convert_pointer_to, but it pays
862 attention to which specific instance of what type we want to
863 convert to. This routine should eventually become
864 convert_to_pointer after all references to convert_to_pointer
865 are removed. */
867 tree
868 convert_pointer_to_real (binfo, expr)
869 tree binfo, expr;
871 register tree intype = TREE_TYPE (expr);
872 tree ptr_type;
873 tree type, rval;
875 if (TREE_CODE (binfo) == TREE_VEC)
876 type = BINFO_TYPE (binfo);
877 else if (IS_AGGR_TYPE (binfo))
879 type = binfo;
881 else
883 type = binfo;
884 binfo = NULL_TREE;
887 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
888 TYPE_VOLATILE (TREE_TYPE (intype)));
889 ptr_type = build_pointer_type (ptr_type);
890 if (ptr_type == TYPE_MAIN_VARIANT (intype))
891 return expr;
893 if (intype == error_mark_node)
894 return error_mark_node;
896 my_friendly_assert (!integer_zerop (expr), 191);
898 if (TREE_CODE (type) == RECORD_TYPE
899 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
900 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
902 tree path;
903 int distance
904 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
905 0, &path);
907 /* This function shouldn't be called with unqualified arguments
908 but if it is, give them an error message that they can read. */
909 if (distance < 0)
911 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
912 TREE_TYPE (intype), type);
914 if (distance == -2)
915 cp_error ("because `%T' is an ambiguous base class", type);
916 return error_mark_node;
919 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
921 rval = build1 (NOP_EXPR, ptr_type,
922 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
923 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
924 return rval;
927 /* Call this when we know (for any reason) that expr is
928 not, in fact, zero. This routine gets a type out of the first
929 argument and uses it to search for the type to convert to. If there
930 is more than one instance of that type in the expr, the conversion is
931 ambiguous. This routine should eventually go away, and all
932 callers should use convert_to_pointer_real. */
934 tree
935 convert_pointer_to (binfo, expr)
936 tree binfo, expr;
938 tree type;
940 if (TREE_CODE (binfo) == TREE_VEC)
941 type = BINFO_TYPE (binfo);
942 else if (IS_AGGR_TYPE (binfo))
943 type = binfo;
944 else
945 type = binfo;
946 return convert_pointer_to_real (type, expr);
949 /* C++ conversions, preference to static cast conversions. */
951 tree
952 cp_convert (type, expr)
953 tree type, expr;
955 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
958 /* Conversion...
960 FLAGS indicates how we should behave. */
962 tree
963 ocp_convert (type, expr, convtype, flags)
964 tree type, expr;
965 int convtype, flags;
967 register tree e = expr;
968 register enum tree_code code = TREE_CODE (type);
970 if (e == error_mark_node
971 || TREE_TYPE (e) == error_mark_node)
972 return error_mark_node;
974 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
975 /* We need a new temporary; don't take this shortcut. */;
976 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
977 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
978 return fold (build1 (NOP_EXPR, type, e));
980 if (code == VOID_TYPE && (convtype & CONV_STATIC))
981 return build1 (CONVERT_EXPR, type, e);
983 #if 0
984 /* This is incorrect. A truncation can't be stripped this way.
985 Extensions will be stripped by the use of get_unwidened. */
986 if (TREE_CODE (e) == NOP_EXPR)
987 return cp_convert (type, TREE_OPERAND (e, 0));
988 #endif
990 /* Just convert to the type of the member. */
991 if (code == OFFSET_TYPE)
993 type = TREE_TYPE (type);
994 code = TREE_CODE (type);
997 #if 0
998 if (code == REFERENCE_TYPE)
999 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1000 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1001 e = convert_from_reference (e);
1002 #endif
1004 if (TREE_CODE (e) == OFFSET_REF)
1005 e = resolve_offset_ref (e);
1007 if (TREE_READONLY_DECL_P (e))
1008 e = decl_constant_value (e);
1010 if (INTEGRAL_CODE_P (code))
1012 tree intype = TREE_TYPE (e);
1013 /* enum = enum, enum = int, enum = float, (enum)pointer are all
1014 errors. */
1015 if (flag_int_enum_equivalence == 0
1016 && TREE_CODE (type) == ENUMERAL_TYPE
1017 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
1018 || (TREE_CODE (intype) == POINTER_TYPE)))
1020 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1022 if (flag_pedantic_errors)
1023 return error_mark_node;
1025 if (IS_AGGR_TYPE (intype))
1027 tree rval;
1028 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1029 if (rval)
1030 return rval;
1031 if (flags & LOOKUP_COMPLAIN)
1032 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1033 if (flags & LOOKUP_SPECULATIVELY)
1034 return NULL_TREE;
1035 return error_mark_node;
1037 if (code == BOOLEAN_TYPE)
1039 /* Common Ada/Pascal programmer's mistake. We always warn
1040 about this since it is so bad. */
1041 if (TREE_CODE (expr) == FUNCTION_DECL)
1042 cp_warning ("the address of `%D', will always be `true'", expr);
1043 return truthvalue_conversion (e);
1045 return fold (convert_to_integer (type, e));
1047 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1048 || TYPE_PTRMEMFUNC_P (type))
1049 return fold (cp_convert_to_pointer (type, e));
1050 if (code == REAL_TYPE || code == COMPLEX_TYPE)
1052 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1054 tree rval;
1055 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1056 if (rval)
1057 return rval;
1058 else
1059 if (flags & LOOKUP_COMPLAIN)
1060 cp_error ("`%#T' used where a floating point value was expected",
1061 TREE_TYPE (e));
1063 if (code == REAL_TYPE)
1064 return fold (convert_to_real (type, e));
1065 else if (code == COMPLEX_TYPE)
1066 return fold (convert_to_complex (type, e));
1069 /* New C++ semantics: since assignment is now based on
1070 memberwise copying, if the rhs type is derived from the
1071 lhs type, then we may still do a conversion. */
1072 if (IS_AGGR_TYPE_CODE (code))
1074 tree dtype = TREE_TYPE (e);
1075 tree ctor = NULL_TREE;
1076 tree conversion = NULL_TREE;
1078 dtype = TYPE_MAIN_VARIANT (dtype);
1080 /* Conversion of object pointers or signature pointers/references
1081 to signature pointers/references. */
1083 if (TYPE_LANG_SPECIFIC (type)
1084 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1086 tree constructor = build_signature_pointer_constructor (type, expr);
1087 tree sig_ty = SIGNATURE_TYPE (type);
1088 tree sig_ptr;
1090 if (constructor == error_mark_node)
1091 return error_mark_node;
1093 sig_ptr = get_temp_name (type, 1);
1094 DECL_INITIAL (sig_ptr) = constructor;
1095 CLEAR_SIGNATURE (sig_ty);
1096 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1097 SET_SIGNATURE (sig_ty);
1098 TREE_READONLY (sig_ptr) = 1;
1100 return sig_ptr;
1103 /* Conversion between aggregate types. New C++ semantics allow
1104 objects of derived type to be cast to objects of base type.
1105 Old semantics only allowed this between pointers.
1107 There may be some ambiguity between using a constructor
1108 vs. using a type conversion operator when both apply. */
1110 if (flag_ansi_overloading)
1112 ctor = e;
1114 if ((flags & LOOKUP_ONLYCONVERTING)
1115 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
1117 ctor = build_user_type_conversion (type, ctor, flags);
1118 flags |= LOOKUP_NO_CONVERSION;
1120 if (ctor)
1121 ctor = build_method_call (NULL_TREE, ctor_identifier,
1122 build_expr_list (NULL_TREE, ctor),
1123 TYPE_BINFO (type), flags);
1124 if (ctor)
1125 return build_cplus_new (type, ctor);
1127 else
1129 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1130 && TYPE_HAS_CONVERSION (dtype))
1131 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1133 if (conversion == error_mark_node)
1135 if (flags & LOOKUP_COMPLAIN)
1136 error ("ambiguous pointer conversion");
1137 return conversion;
1140 if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1141 ctor = build_method_call (NULL_TREE, ctor_identifier,
1142 build_expr_list (NULL_TREE, e),
1143 TYPE_BINFO (type),
1144 (flags & LOOKUP_NORMAL)
1145 | LOOKUP_SPECULATIVELY
1146 | (flags & LOOKUP_ONLYCONVERTING)
1147 | (flags & LOOKUP_NO_CONVERSION)
1148 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1150 if (ctor == error_mark_node)
1152 if (flags & LOOKUP_COMPLAIN)
1153 cp_error ("in conversion to type `%T'", type);
1154 if (flags & LOOKUP_SPECULATIVELY)
1155 return NULL_TREE;
1156 return error_mark_node;
1159 if (conversion && ctor)
1161 if (flags & LOOKUP_COMPLAIN)
1162 error ("both constructor and type conversion operator apply");
1163 if (flags & LOOKUP_SPECULATIVELY)
1164 return NULL_TREE;
1165 return error_mark_node;
1167 else if (conversion)
1168 return conversion;
1169 else if (ctor)
1171 ctor = build_cplus_new (type, ctor);
1172 return ctor;
1177 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1178 then the it won't be hashed and hence compare as not equal,
1179 even when it is. */
1180 if (code == ARRAY_TYPE
1181 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1182 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1183 return e;
1185 if (flags & LOOKUP_COMPLAIN)
1186 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1187 TREE_TYPE (expr), type);
1188 if (flags & LOOKUP_SPECULATIVELY)
1189 return NULL_TREE;
1190 return error_mark_node;
1193 /* Create an expression whose value is that of EXPR,
1194 converted to type TYPE. The TREE_TYPE of the value
1195 is always TYPE. This function implements all reasonable
1196 conversions; callers should filter out those that are
1197 not permitted by the language being compiled.
1199 Most of this routine is from build_reinterpret_cast.
1201 The backend cannot call cp_convert (what was convert) because
1202 conversions to/from basetypes may involve memory references
1203 (vbases) and adding or subtracting small values (multiple
1204 inheritance), but it calls convert from the constant folding code
1205 on subtrees of already build trees after it has ripped them apart.
1207 Also, if we ever support range variables, we'll probably also have to
1208 do a little bit more work. */
1210 tree
1211 convert (type, expr)
1212 tree type, expr;
1214 tree intype;
1216 if (type == error_mark_node || expr == error_mark_node)
1217 return error_mark_node;
1219 intype = TREE_TYPE (expr);
1221 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1223 if (TREE_READONLY_DECL_P (expr))
1224 expr = decl_constant_value (expr);
1225 return fold (build1 (NOP_EXPR, type, expr));
1228 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1229 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1232 /* Like cp_convert, except permit conversions to take place which
1233 are not normally allowed due to access restrictions
1234 (such as conversion from sub-type to private super-type). */
1236 tree
1237 convert_force (type, expr, convtype)
1238 tree type;
1239 tree expr;
1240 int convtype;
1242 register tree e = expr;
1243 register enum tree_code code = TREE_CODE (type);
1245 if (code == REFERENCE_TYPE)
1246 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1247 NULL_TREE));
1248 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1249 e = convert_from_reference (e);
1251 if (code == POINTER_TYPE)
1252 return fold (convert_to_pointer_force (type, e));
1254 /* From typeck.c convert_for_assignment */
1255 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1256 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1257 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1258 || integer_zerop (e)
1259 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1260 && TYPE_PTRMEMFUNC_P (type))
1262 /* compatible pointer to member functions. */
1263 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1266 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1269 /* Subroutine of build_type_conversion. */
1271 static tree
1272 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1273 tree xtype, basetype;
1274 tree expr;
1275 tree typename;
1276 int for_sure;
1278 tree rval;
1279 int flags;
1281 if (for_sure == 0)
1282 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1283 else
1284 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1286 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1287 if (rval == error_mark_node)
1289 if (for_sure == 0)
1290 return NULL_TREE;
1291 return error_mark_node;
1294 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1295 return rval;
1297 if (warn_cast_qual
1298 && TREE_TYPE (xtype)
1299 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1300 > TREE_READONLY (TREE_TYPE (xtype))))
1301 warning ("user-defined conversion casting away `const'");
1302 return cp_convert (xtype, rval);
1305 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1306 exists, return the attempted conversion. This may
1307 return ERROR_MARK_NODE if the conversion is not
1308 allowed (references private members, etc).
1309 If no conversion exists, NULL_TREE is returned.
1311 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1312 to take place immediately. Otherwise, we build a SAVE_EXPR
1313 which can be evaluated if the results are ever needed.
1315 Changes to this functions should be mirrored in user_harshness.
1317 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1318 object parameter, or by the second standard conversion sequence if
1319 that doesn't do it. This will probably wait for an overloading rewrite.
1320 (jason 8/9/95) */
1322 tree
1323 build_type_conversion (code, xtype, expr, for_sure)
1324 enum tree_code code;
1325 tree xtype, expr;
1326 int for_sure;
1328 /* C++: check to see if we can convert this aggregate type
1329 into the required type. */
1330 tree basetype;
1331 tree conv;
1332 tree winner = NULL_TREE;
1334 if (flag_ansi_overloading)
1335 return build_user_type_conversion
1336 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1338 if (expr == error_mark_node)
1339 return error_mark_node;
1341 basetype = TREE_TYPE (expr);
1342 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1343 basetype = TREE_TYPE (basetype);
1345 basetype = TYPE_MAIN_VARIANT (basetype);
1346 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1347 return NULL_TREE;
1349 /* Do we have an exact match? */
1351 tree typename = build_typename_overload (xtype);
1352 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1353 return build_type_conversion_1 (xtype, basetype, expr, typename,
1354 for_sure);
1357 /* Nope; try looking for others. */
1358 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1360 tree cand = TREE_VALUE (conv);
1362 if (winner && winner == cand)
1363 continue;
1365 if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1367 if (winner)
1369 if (for_sure)
1371 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1372 xtype);
1373 cp_error (" candidate conversions include `%D' and `%D'",
1374 winner, cand);
1376 return NULL_TREE;
1378 else
1379 winner = cand;
1383 if (winner)
1384 return build_type_conversion_1 (xtype, basetype, expr,
1385 DECL_NAME (winner), for_sure);
1387 return NULL_TREE;
1390 /* Convert the given EXPR to one of a group of types suitable for use in an
1391 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1392 which indicates which types are suitable. If COMPLAIN is 1, complain
1393 about ambiguity; otherwise, the caller will deal with it. */
1395 tree
1396 build_expr_type_conversion (desires, expr, complain)
1397 int desires;
1398 tree expr;
1399 int complain;
1401 tree basetype = TREE_TYPE (expr);
1402 tree conv;
1403 tree winner = NULL_TREE;
1405 if (TREE_CODE (basetype) == OFFSET_TYPE)
1406 expr = resolve_offset_ref (expr);
1407 expr = convert_from_reference (expr);
1408 basetype = TREE_TYPE (expr);
1410 if (! IS_AGGR_TYPE (basetype))
1411 switch (TREE_CODE (basetype))
1413 case INTEGER_TYPE:
1414 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1415 && integer_zerop (expr))
1416 return expr;
1417 /* else fall through... */
1419 case BOOLEAN_TYPE:
1420 return (desires & WANT_INT) ? expr : NULL_TREE;
1421 case ENUMERAL_TYPE:
1422 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1423 case REAL_TYPE:
1424 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1425 case POINTER_TYPE:
1426 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1428 case FUNCTION_TYPE:
1429 case ARRAY_TYPE:
1430 return (desires & WANT_POINTER) ? default_conversion (expr)
1431 : NULL_TREE;
1432 default:
1433 return NULL_TREE;
1436 if (! TYPE_HAS_CONVERSION (basetype))
1437 return NULL_TREE;
1439 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1441 int win = 0;
1442 tree candidate;
1443 tree cand = TREE_VALUE (conv);
1445 if (winner && winner == cand)
1446 continue;
1448 candidate = TREE_TYPE (TREE_TYPE (cand));
1449 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1450 candidate = TREE_TYPE (candidate);
1452 switch (TREE_CODE (candidate))
1454 case BOOLEAN_TYPE:
1455 case INTEGER_TYPE:
1456 win = (desires & WANT_INT); break;
1457 case ENUMERAL_TYPE:
1458 win = (desires & WANT_ENUM); break;
1459 case REAL_TYPE:
1460 win = (desires & WANT_FLOAT); break;
1461 case POINTER_TYPE:
1462 win = (desires & WANT_POINTER); break;
1465 if (win)
1467 if (winner)
1469 if (complain)
1471 cp_error ("ambiguous default type conversion from `%T'",
1472 basetype);
1473 cp_error (" candidate conversions include `%D' and `%D'",
1474 winner, cand);
1476 return error_mark_node;
1478 else
1479 winner = cand;
1483 if (winner)
1485 tree type = TREE_TYPE (TREE_TYPE (winner));
1486 if (TREE_CODE (type) == REFERENCE_TYPE)
1487 type = TREE_TYPE (type);
1488 return build_type_conversion_1 (type, basetype, expr,
1489 DECL_NAME (winner), 1);
1492 return NULL_TREE;
1495 /* Must convert two aggregate types to non-aggregate type.
1496 Attempts to find a non-ambiguous, "best" type conversion.
1498 Return 1 on success, 0 on failure.
1500 @@ What are the real semantics of this supposed to be??? */
1503 build_default_binary_type_conversion (code, arg1, arg2)
1504 enum tree_code code;
1505 tree *arg1, *arg2;
1507 switch (code)
1509 case MULT_EXPR:
1510 case TRUNC_DIV_EXPR:
1511 case CEIL_DIV_EXPR:
1512 case FLOOR_DIV_EXPR:
1513 case ROUND_DIV_EXPR:
1514 case EXACT_DIV_EXPR:
1515 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1516 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1517 break;
1519 case TRUNC_MOD_EXPR:
1520 case FLOOR_MOD_EXPR:
1521 case LSHIFT_EXPR:
1522 case RSHIFT_EXPR:
1523 case BIT_AND_EXPR:
1524 case BIT_XOR_EXPR:
1525 case BIT_IOR_EXPR:
1526 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1527 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1528 break;
1530 case PLUS_EXPR:
1532 tree a1, a2, p1, p2;
1533 int wins;
1535 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1536 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1537 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1538 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1540 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1542 if (wins > 1)
1543 error ("ambiguous default type conversion for `operator +'");
1545 if (a1 && a2)
1546 *arg1 = a1, *arg2 = a2;
1547 else if (a1 && p2)
1548 *arg1 = a1, *arg2 = p2;
1549 else
1550 *arg1 = p1, *arg2 = a2;
1551 break;
1554 case MINUS_EXPR:
1556 tree a1, a2, p1, p2;
1557 int wins;
1559 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1560 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1561 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1562 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1564 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1566 if (wins > 1)
1567 error ("ambiguous default type conversion for `operator -'");
1569 if (a1 && a2)
1570 *arg1 = a1, *arg2 = a2;
1571 else if (p1 && p2)
1572 *arg1 = p1, *arg2 = p2;
1573 else
1574 *arg1 = p1, *arg2 = a2;
1575 break;
1578 case GT_EXPR:
1579 case LT_EXPR:
1580 case GE_EXPR:
1581 case LE_EXPR:
1582 case EQ_EXPR:
1583 case NE_EXPR:
1585 tree a1, a2, p1, p2;
1586 int wins;
1588 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1589 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1590 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1591 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1593 wins = (a1 && a2) + (p1 && p2);
1595 if (wins > 1)
1596 cp_error ("ambiguous default type conversion for `%O'", code);
1598 if (a1 && a2)
1599 *arg1 = a1, *arg2 = a2;
1600 else
1601 *arg1 = p1, *arg2 = p2;
1602 break;
1605 case TRUTH_ANDIF_EXPR:
1606 case TRUTH_ORIF_EXPR:
1607 *arg1 = cp_convert (boolean_type_node, *arg1);
1608 *arg2 = cp_convert (boolean_type_node, *arg2);
1609 break;
1611 default:
1612 *arg1 = NULL_TREE;
1613 *arg2 = NULL_TREE;
1616 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1617 cp_error ("ambiguous default type conversion for `%O'", code);
1619 if (*arg1 && *arg2)
1620 return 1;
1622 return 0;
1625 /* Implements integral promotion (4.1) and float->double promotion. */
1627 tree
1628 type_promotes_to (type)
1629 tree type;
1631 int constp, volatilep;
1633 if (type == error_mark_node)
1634 return error_mark_node;
1636 constp = TYPE_READONLY (type);
1637 volatilep = TYPE_VOLATILE (type);
1638 type = TYPE_MAIN_VARIANT (type);
1640 /* bool always promotes to int (not unsigned), even if it's the same
1641 size. */
1642 if (type == boolean_type_node)
1643 type = integer_type_node;
1645 /* Normally convert enums to int, but convert wide enums to something
1646 wider. */
1647 else if (TREE_CODE (type) == ENUMERAL_TYPE
1648 || type == wchar_type_node)
1650 int precision = MAX (TYPE_PRECISION (type),
1651 TYPE_PRECISION (integer_type_node));
1652 tree totype = type_for_size (precision, 0);
1653 if (TREE_UNSIGNED (type)
1654 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1655 type = type_for_size (precision, 1);
1656 else
1657 type = totype;
1659 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1661 /* Retain unsignedness if really not getting bigger. */
1662 if (TREE_UNSIGNED (type)
1663 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1664 type = unsigned_type_node;
1665 else
1666 type = integer_type_node;
1668 else if (type == float_type_node)
1669 type = double_type_node;
1671 return cp_build_type_variant (type, constp, volatilep);