fe3c794d70a81b142666529f869fcd0d2479b8ab
[gcc.git] / gcc / cp / cvt.c
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)
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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. */
21
22
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. */
27
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"
35
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39
40 extern tree static_aggregates;
41
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));
49
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.
53
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.
57
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.
66
67 C++: in multiple-inheritance, converting between pointers may involve
68 adjusting them by a delta stored within the class definition. */
69 \f
70 /* Subroutines of `convert'. */
71
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. */
76
77 static tree
78 build_thunk (offset, real_addr)
79 tree offset, real_addr;
80 {
81 if (TREE_CODE (real_addr) != ADDR_EXPR
82 || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
83 {
84 sorry ("MI pointer to member conversion too complex");
85 return error_mark_node;
86 }
87 sorry ("MI pointer to member conversion too complex");
88 return error_mark_node;
89 }
90
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. */
94
95 static tree
96 convert_fn_ptr (type, expr)
97 tree type, expr;
98 {
99 #if 0 /* We don't use thunks for pmfs. */
100 if (flag_vtable_thunks)
101 {
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)
106 {
107 error (" in pointer to member conversion");
108 return error_mark_node;
109 }
110 if (binfo == NULL_TREE)
111 {
112 /* ARM 4.8 restriction. */
113 error ("invalid pointer to member conversion");
114 return error_mark_node;
115 }
116
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));
120 }
121 else
122 #endif
123 return build_ptrmemfunc (type, expr, 1);
124 }
125
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 */
132
133 static tree
134 cp_convert_to_pointer (type, expr)
135 tree type, expr;
136 {
137 register tree intype = TREE_TYPE (expr);
138 register enum tree_code form;
139
140 if (IS_AGGR_TYPE (intype))
141 {
142 tree rval;
143
144 intype = complete_type (intype);
145 if (TYPE_SIZE (intype) == NULL_TREE)
146 {
147 cp_error ("can't convert from incomplete type `%T' to `%T'",
148 intype, type);
149 return error_mark_node;
150 }
151
152 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
153 if (rval)
154 {
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;
159 }
160 }
161
162 if (TYPE_PTRMEMFUNC_P (type))
163 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
164
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))
169 {
170 /* Allow an implicit this pointer for pointer to member
171 functions. */
172 if (TYPE_PTRMEMFUNC_P (intype))
173 {
174 tree decl, basebinfo;
175 tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
176 tree t = TYPE_METHOD_BASETYPE (fntype);
177
178 if (current_class_type == 0
179 || get_base_distance (t, current_class_type, 0, &basebinfo)
180 == -1)
181 {
182 decl = build1 (NOP_EXPR, t, error_mark_node);
183 }
184 else if (current_class_ptr == 0)
185 decl = build1 (NOP_EXPR, t, error_mark_node);
186 else
187 decl = current_class_ref;
188
189 expr = build (OFFSET_REF, fntype, decl, expr);
190 }
191
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)
198 {
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);
204 }
205 intype = TREE_TYPE (expr);
206 }
207
208 if (TYPE_PTRMEMFUNC_P (intype))
209 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
210
211 form = TREE_CODE (intype);
212
213 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
214 {
215 intype = TYPE_MAIN_VARIANT (intype);
216
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)
222 {
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)
228 {
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;
233 }
234 if (binfo)
235 {
236 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
237 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
238 || ! BINFO_OFFSET_ZEROP (binfo))
239 {
240 /* Need to get the path we took. */
241 tree path;
242
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);
248 }
249 }
250 }
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);
255
256 if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
257 && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
258 {
259 tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
260 tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
261 tree binfo = get_binfo (b2, b1, 1);
262 enum tree_code code = PLUS_EXPR;
263
264 if (binfo == NULL_TREE)
265 {
266 binfo = get_binfo (b1, b2, 1);
267 code = MINUS_EXPR;
268 }
269
270 if (binfo == error_mark_node)
271 return error_mark_node;
272 if (binfo && ! TREE_VIA_VIRTUAL (binfo))
273 expr = size_binop (code, expr, BINFO_OFFSET (binfo));
274 }
275
276 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
277 || (TREE_CODE (type) == POINTER_TYPE
278 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
279 {
280 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
281 expr, intype, type);
282 return error_mark_node;
283 }
284
285 return build1 (NOP_EXPR, type, expr);
286 }
287
288 my_friendly_assert (form != OFFSET_TYPE, 186);
289
290 if (TYPE_LANG_SPECIFIC (intype)
291 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
292 return convert_to_pointer (type, build_optr_ref (expr));
293
294 if (integer_zerop (expr))
295 {
296 if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
297 return build_ptrmemfunc (type, expr, 0);
298 expr = build_int_2 (0, 0);
299 TREE_TYPE (expr) = type;
300 return expr;
301 }
302
303 if (INTEGRAL_CODE_P (form))
304 {
305 if (type_precision (intype) == POINTER_SIZE)
306 return build1 (CONVERT_EXPR, type, expr);
307 expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr);
308 /* Modes may be different but sizes should be the same. */
309 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
310 != GET_MODE_SIZE (TYPE_MODE (type)))
311 /* There is supposed to be some integral type
312 that is the same width as a pointer. */
313 abort ();
314 return convert_to_pointer (type, expr);
315 }
316
317 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
318 expr, intype, type);
319 return error_mark_node;
320 }
321
322 /* Like convert, except permit conversions to take place which
323 are not normally allowed due to access restrictions
324 (such as conversion from sub-type to private super-type). */
325
326 static tree
327 convert_to_pointer_force (type, expr)
328 tree type, expr;
329 {
330 register tree intype = TREE_TYPE (expr);
331 register enum tree_code form = TREE_CODE (intype);
332
333 if (integer_zerop (expr))
334 {
335 expr = build_int_2 (0, 0);
336 TREE_TYPE (expr) = type;
337 return expr;
338 }
339
340 /* Convert signature pointer/reference to `void *' first. */
341 if (form == RECORD_TYPE
342 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
343 {
344 expr = build_optr_ref (expr);
345 intype = TREE_TYPE (expr);
346 form = TREE_CODE (intype);
347 }
348
349 if (form == POINTER_TYPE)
350 {
351 intype = TYPE_MAIN_VARIANT (intype);
352
353 if (TYPE_MAIN_VARIANT (type) != intype
354 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
355 && IS_AGGR_TYPE (TREE_TYPE (type))
356 && IS_AGGR_TYPE (TREE_TYPE (intype))
357 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
358 {
359 enum tree_code code = PLUS_EXPR;
360 tree path;
361 int distance = get_base_distance (TREE_TYPE (type),
362 TREE_TYPE (intype), 0, &path);
363 if (distance == -2)
364 {
365 ambig:
366 cp_error ("type `%T' is ambiguous baseclass of `%s'",
367 TREE_TYPE (type),
368 TYPE_NAME_STRING (TREE_TYPE (intype)));
369 return error_mark_node;
370 }
371 if (distance == -1)
372 {
373 distance = get_base_distance (TREE_TYPE (intype),
374 TREE_TYPE (type), 0, &path);
375 if (distance == -2)
376 goto ambig;
377 if (distance < 0)
378 /* Doesn't need any special help from us. */
379 return build1 (NOP_EXPR, type, expr);
380
381 code = MINUS_EXPR;
382 }
383 return build_vbase_path (code, type, expr, path, 0);
384 }
385 }
386
387 return cp_convert_to_pointer (type, expr);
388 }
389
390 /* We are passing something to a function which requires a reference.
391 The type we are interested in is in TYPE. The initial
392 value we have to begin with is in ARG.
393
394 FLAGS controls how we manage access checking.
395 DIRECT_BIND in FLAGS controls how any temporaries are generated. */
396
397 static tree
398 build_up_reference (type, arg, flags, checkconst)
399 tree type, arg;
400 int flags, checkconst;
401 {
402 tree rval;
403 tree argtype = TREE_TYPE (arg);
404 tree target_type = TREE_TYPE (type);
405
406 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
407
408 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
409 {
410 tree targ = arg;
411 if (toplevel_bindings_p ())
412 arg = get_temp_name (argtype, 1);
413 else
414 {
415 arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
416 DECL_ARTIFICIAL (arg) = 1;
417 }
418 DECL_INITIAL (arg) = targ;
419 cp_finish_decl (arg, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
420 }
421 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
422 {
423 tree slot = build_decl (VAR_DECL, NULL_TREE, argtype);
424 arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
425 TREE_SIDE_EFFECTS (arg) = 1;
426 }
427
428 /* If we had a way to wrap this up, and say, if we ever needed it's
429 address, transform all occurrences of the register, into a memory
430 reference we could win better. */
431 rval = build_unary_op (ADDR_EXPR, arg, 1);
432 if ((flags & LOOKUP_PROTECT)
433 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
434 && IS_AGGR_TYPE (argtype)
435 && IS_AGGR_TYPE (target_type))
436 {
437 /* We go through get_binfo for the access control. */
438 tree binfo = get_binfo (target_type, argtype, 1);
439 if (binfo == error_mark_node)
440 return error_mark_node;
441 if (binfo == NULL_TREE)
442 return error_not_base_type (target_type, argtype);
443 rval = convert_pointer_to_real (binfo, rval);
444 }
445 else
446 rval
447 = convert_to_pointer_force (build_pointer_type (target_type), rval);
448 rval = build1 (NOP_EXPR, type, rval);
449 TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
450 return rval;
451 }
452
453 /* For C++: Only need to do one-level references, but cannot
454 get tripped up on signed/unsigned differences.
455
456 DECL is either NULL_TREE or the _DECL node for a reference that is being
457 initialized. It can be error_mark_node if we don't know the _DECL but
458 we know it's an initialization. */
459
460 tree
461 convert_to_reference (reftype, expr, convtype, flags, decl)
462 tree reftype, expr;
463 int convtype, flags;
464 tree decl;
465 {
466 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
467 register tree intype = TREE_TYPE (expr);
468 tree rval = NULL_TREE;
469 tree rval_as_conversion = NULL_TREE;
470 int i;
471
472 if (TREE_CODE (intype) == REFERENCE_TYPE)
473 my_friendly_abort (364);
474
475 intype = TYPE_MAIN_VARIANT (intype);
476
477 i = comp_target_types (type, intype, 0);
478
479 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
480 && ! (flags & LOOKUP_NO_CONVERSION))
481 {
482 /* Look for a user-defined conversion to lvalue that we can use. */
483
484 if (flag_ansi_overloading)
485 rval_as_conversion
486 = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
487 else
488 rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
489
490 if (rval_as_conversion && rval_as_conversion != error_mark_node
491 && real_lvalue_p (rval_as_conversion))
492 {
493 expr = rval_as_conversion;
494 rval_as_conversion = NULL_TREE;
495 intype = type;
496 i = 1;
497 }
498 }
499
500 if (((convtype & CONV_STATIC) && i == -1)
501 || ((convtype & CONV_IMPLICIT) && i == 1))
502 {
503 if (flags & LOOKUP_COMPLAIN)
504 {
505 tree ttl = TREE_TYPE (reftype);
506 tree ttr;
507
508 {
509 int r = TREE_READONLY (expr);
510 int v = TREE_THIS_VOLATILE (expr);
511 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
512 }
513
514 if (! real_lvalue_p (expr) && ! TYPE_READONLY (ttl))
515 {
516 if (decl)
517 /* Ensure semantics of [dcl.init.ref] */
518 cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'",
519 reftype, intype);
520 else
521 cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'",
522 reftype, intype);
523 }
524 else if (! (convtype & CONV_CONST))
525 {
526 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
527 cp_pedwarn ("conversion from `%T' to `%T' discards const",
528 ttr, reftype);
529 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
530 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
531 ttr, reftype);
532 }
533 }
534
535 return build_up_reference (reftype, expr, flags,
536 ! (convtype & CONV_CONST));
537 }
538 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
539 {
540 /* When casting an lvalue to a reference type, just convert into
541 a pointer to the new type and deference it. This is allowed
542 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
543 should be done directly (jason). (int &)ri ---> *(int*)&ri */
544
545 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
546 meant. */
547 if (TREE_CODE (intype) == POINTER_TYPE
548 && (comptypes (TREE_TYPE (intype), type, -1)))
549 cp_warning ("casting `%T' to `%T' does not dereference pointer",
550 intype, reftype);
551
552 rval = build_unary_op (ADDR_EXPR, expr, 0);
553 if (rval != error_mark_node)
554 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
555 if (rval != error_mark_node)
556 rval = build1 (NOP_EXPR, reftype, rval);
557 }
558 else if (flag_ansi_overloading)
559 {
560 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
561 "converting", 0, 0);
562 if (rval == error_mark_node)
563 return error_mark_node;
564 rval = build_up_reference (reftype, rval, flags, 1);
565
566 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
567 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
568 reftype, intype);
569 }
570 else
571 {
572 tree rval_as_ctor = NULL_TREE;
573
574 if (rval_as_conversion)
575 {
576 if (rval_as_conversion == error_mark_node)
577 {
578 cp_error ("conversion from `%T' to `%T' is ambiguous",
579 intype, reftype);
580 return error_mark_node;
581 }
582 rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
583 flags, 1);
584 }
585
586 /* Definitely need to go through a constructor here. */
587 if (TYPE_HAS_CONSTRUCTOR (type)
588 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
589 && (rval = build_method_call
590 (NULL_TREE, ctor_identifier,
591 build_expr_list (NULL_TREE, expr), TYPE_BINFO (type),
592 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
593 | LOOKUP_ONLYCONVERTING)))
594 {
595 tree init;
596
597 if (toplevel_bindings_p ())
598 {
599 tree t = get_temp_name (type, toplevel_bindings_p ());
600 init = build_method_call (t, ctor_identifier,
601 build_expr_list (NULL_TREE, expr),
602 TYPE_BINFO (type),
603 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
604 | LOOKUP_ONLYCONVERTING);
605
606 if (init == error_mark_node)
607 return error_mark_node;
608
609 make_decl_rtl (t, NULL_PTR, 1);
610 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
611 rval = build_unary_op (ADDR_EXPR, t, 0);
612 }
613 else
614 {
615 init = build_method_call (NULL_TREE, ctor_identifier,
616 build_expr_list (NULL_TREE, expr),
617 TYPE_BINFO (type),
618 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
619 |LOOKUP_ONLYCONVERTING);
620
621 if (init == error_mark_node)
622 return error_mark_node;
623
624 rval = build_cplus_new (type, init);
625 rval = build_up_reference (reftype, rval, flags, 1);
626 }
627 rval_as_ctor = rval;
628 }
629
630 if (rval_as_ctor && rval_as_conversion)
631 {
632 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
633 intype, reftype);
634 return error_mark_node;
635 }
636 else if (rval_as_ctor)
637 rval = rval_as_ctor;
638 else if (rval_as_conversion)
639 rval = rval_as_conversion;
640 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
641 {
642 rval = cp_convert (type, expr);
643 if (rval == error_mark_node)
644 return error_mark_node;
645
646 rval = build_up_reference (reftype, rval, flags, 1);
647 }
648
649 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
650 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
651 reftype, intype);
652 }
653
654 if (rval)
655 {
656 /* If we found a way to convert earlier, then use it. */
657 return rval;
658 }
659
660 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
661
662 if (flags & LOOKUP_COMPLAIN)
663 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
664
665 if (flags & LOOKUP_SPECULATIVELY)
666 return NULL_TREE;
667
668 return error_mark_node;
669 }
670
671 /* We are using a reference VAL for its value. Bash that reference all the
672 way down to its lowest form. */
673
674 tree
675 convert_from_reference (val)
676 tree val;
677 {
678 tree type = TREE_TYPE (val);
679
680 if (TREE_CODE (type) == OFFSET_TYPE)
681 type = TREE_TYPE (type);
682 if (TREE_CODE (type) == REFERENCE_TYPE)
683 return build_indirect_ref (val, NULL_PTR);
684 return val;
685 }
686 \f
687 /* See if there is a constructor of type TYPE which will convert
688 EXPR. The reference manual seems to suggest (8.5.6) that we need
689 not worry about finding constructors for base classes, then converting
690 to the derived class.
691
692 MSGP is a pointer to a message that would be an appropriate error
693 string. If MSGP is NULL, then we are not interested in reporting
694 errors. */
695
696 tree
697 convert_to_aggr (type, expr, msgp, protect)
698 tree type, expr;
699 char **msgp;
700 int protect;
701 {
702 tree basetype = type;
703 tree name = TYPE_IDENTIFIER (basetype);
704 tree function, fndecl, fntype, parmtypes, parmlist, result;
705 #if 0
706 /* See code below that used this. */
707 tree method_name;
708 #endif
709 tree access;
710 int can_be_private, can_be_protected;
711
712 if (! TYPE_HAS_CONSTRUCTOR (basetype))
713 {
714 if (msgp)
715 *msgp = "type `%s' does not have a constructor";
716 return error_mark_node;
717 }
718
719 access = access_public_node;
720 can_be_private = 0;
721 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
722
723 parmlist = build_expr_list (NULL_TREE, expr);
724 parmtypes = scratch_tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
725
726 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
727 {
728 parmtypes = expr_tree_cons (NULL_TREE, integer_type_node, parmtypes);
729 parmlist = scratch_tree_cons (NULL_TREE, integer_one_node, parmlist);
730 }
731
732 /* The type of the first argument will be filled in inside the loop. */
733 parmlist = expr_tree_cons (NULL_TREE, integer_zero_node, parmlist);
734 parmtypes = scratch_tree_cons (NULL_TREE, build_pointer_type (basetype), parmtypes);
735
736 /* No exact conversion was found. See if an approximate
737 one will do. */
738 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
739
740 {
741 int saw_private = 0;
742 int saw_protected = 0;
743 struct candidate *candidates
744 = (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
745 struct candidate *cp = candidates;
746
747 while (fndecl)
748 {
749 function = fndecl;
750 cp->h_len = 2;
751 cp->harshness = (struct harshness_code *)
752 alloca (3 * sizeof (struct harshness_code));
753
754 compute_conversion_costs (fndecl, parmlist, cp, 2);
755 if ((cp->h.code & EVIL_CODE) == 0)
756 {
757 cp->u.field = fndecl;
758 if (protect)
759 {
760 if (TREE_PRIVATE (fndecl))
761 access = access_private_node;
762 else if (TREE_PROTECTED (fndecl))
763 access = access_protected_node;
764 else
765 access = access_public_node;
766 }
767 else
768 access = access_public_node;
769
770 if (access == access_private_node
771 ? (basetype == current_class_type
772 || is_friend (basetype, cp->function)
773 || purpose_member (basetype, DECL_ACCESS (fndecl)))
774 : access == access_protected_node
775 ? (can_be_protected
776 || purpose_member (basetype, DECL_ACCESS (fndecl)))
777 : 1)
778 {
779 if (cp->h.code <= TRIVIAL_CODE)
780 goto found_and_ok;
781 cp++;
782 }
783 else
784 {
785 if (access == access_private_node)
786 saw_private = 1;
787 else
788 saw_protected = 1;
789 }
790 }
791 fndecl = DECL_CHAIN (fndecl);
792 }
793 if (cp - candidates)
794 {
795 /* Rank from worst to best. Then cp will point to best one.
796 Private fields have their bits flipped. For unsigned
797 numbers, this should make them look very large.
798 If the best alternate has a (signed) negative value,
799 then all we ever saw were private members. */
800 if (cp - candidates > 1)
801 qsort (candidates, /* char *base */
802 cp - candidates, /* int nel */
803 sizeof (struct candidate), /* int width */
804 (int (*) PROTO((const void *, const void *))) rank_for_overload); /* int (*compar)() */
805
806 --cp;
807 if (cp->h.code & EVIL_CODE)
808 {
809 if (msgp)
810 *msgp = "ambiguous type conversion possible for `%s'";
811 return error_mark_node;
812 }
813
814 function = cp->function;
815 fndecl = cp->u.field;
816 goto found_and_ok;
817 }
818 else if (msgp)
819 {
820 if (saw_private)
821 if (saw_protected)
822 *msgp = "only private and protected conversions apply";
823 else
824 *msgp = "only private conversions apply";
825 else if (saw_protected)
826 *msgp = "only protected conversions apply";
827 else
828 *msgp = "no appropriate conversion to type `%s'";
829 }
830 return error_mark_node;
831 }
832 /* NOTREACHED */
833
834 found:
835 if (access == access_private_node)
836 if (! can_be_private)
837 {
838 if (msgp)
839 *msgp = TREE_PRIVATE (fndecl)
840 ? "conversion to type `%s' is private"
841 : "conversion to type `%s' is from private base class";
842 return error_mark_node;
843 }
844 if (access == access_protected_node)
845 if (! can_be_protected)
846 {
847 if (msgp)
848 *msgp = TREE_PRIVATE (fndecl)
849 ? "conversion to type `%s' is protected"
850 : "conversion to type `%s' is from protected base class";
851 return error_mark_node;
852 }
853 function = fndecl;
854 found_and_ok:
855
856 /* It will convert, but we don't do anything about it yet. */
857 if (msgp == 0)
858 return NULL_TREE;
859
860 fntype = TREE_TYPE (function);
861
862 parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
863 parmlist, NULL_TREE, LOOKUP_NORMAL);
864
865 result = build_call (function, TREE_TYPE (fntype), parmlist);
866 return result;
867 }
868
869 /* Call this when we know (for any reason) that expr is not, in fact,
870 zero. This routine is like convert_pointer_to, but it pays
871 attention to which specific instance of what type we want to
872 convert to. This routine should eventually become
873 convert_to_pointer after all references to convert_to_pointer
874 are removed. */
875
876 tree
877 convert_pointer_to_real (binfo, expr)
878 tree binfo, expr;
879 {
880 register tree intype = TREE_TYPE (expr);
881 tree ptr_type;
882 tree type, rval;
883
884 if (TREE_CODE (binfo) == TREE_VEC)
885 type = BINFO_TYPE (binfo);
886 else if (IS_AGGR_TYPE (binfo))
887 {
888 type = binfo;
889 }
890 else
891 {
892 type = binfo;
893 binfo = NULL_TREE;
894 }
895
896 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
897 TYPE_VOLATILE (TREE_TYPE (intype)));
898 ptr_type = build_pointer_type (ptr_type);
899 if (ptr_type == TYPE_MAIN_VARIANT (intype))
900 return expr;
901
902 if (intype == error_mark_node)
903 return error_mark_node;
904
905 my_friendly_assert (!integer_zerop (expr), 191);
906
907 if (TREE_CODE (type) == RECORD_TYPE
908 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
909 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
910 {
911 tree path;
912 int distance
913 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
914 0, &path);
915
916 /* This function shouldn't be called with unqualified arguments
917 but if it is, give them an error message that they can read. */
918 if (distance < 0)
919 {
920 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
921 TREE_TYPE (intype), type);
922
923 if (distance == -2)
924 cp_error ("because `%T' is an ambiguous base class", type);
925 return error_mark_node;
926 }
927
928 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
929 }
930 rval = build1 (NOP_EXPR, ptr_type,
931 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
932 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
933 return rval;
934 }
935
936 /* Call this when we know (for any reason) that expr is
937 not, in fact, zero. This routine gets a type out of the first
938 argument and uses it to search for the type to convert to. If there
939 is more than one instance of that type in the expr, the conversion is
940 ambiguous. This routine should eventually go away, and all
941 callers should use convert_to_pointer_real. */
942
943 tree
944 convert_pointer_to (binfo, expr)
945 tree binfo, expr;
946 {
947 tree type;
948
949 if (TREE_CODE (binfo) == TREE_VEC)
950 type = BINFO_TYPE (binfo);
951 else if (IS_AGGR_TYPE (binfo))
952 type = binfo;
953 else
954 type = binfo;
955 return convert_pointer_to_real (type, expr);
956 }
957 \f
958 /* C++ conversions, preference to static cast conversions. */
959
960 tree
961 cp_convert (type, expr)
962 tree type, expr;
963 {
964 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
965 }
966
967 /* Conversion...
968
969 FLAGS indicates how we should behave. */
970
971 tree
972 ocp_convert (type, expr, convtype, flags)
973 tree type, expr;
974 int convtype, flags;
975 {
976 register tree e = expr;
977 register enum tree_code code = TREE_CODE (type);
978
979 if (e == error_mark_node
980 || TREE_TYPE (e) == error_mark_node)
981 return error_mark_node;
982
983 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
984 /* We need a new temporary; don't take this shortcut. */;
985 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
986 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
987 return fold (build1 (NOP_EXPR, type, e));
988
989 if (code == VOID_TYPE && (convtype & CONV_STATIC))
990 return build1 (CONVERT_EXPR, type, e);
991
992 #if 0
993 /* This is incorrect. A truncation can't be stripped this way.
994 Extensions will be stripped by the use of get_unwidened. */
995 if (TREE_CODE (e) == NOP_EXPR)
996 return cp_convert (type, TREE_OPERAND (e, 0));
997 #endif
998
999 /* Just convert to the type of the member. */
1000 if (code == OFFSET_TYPE)
1001 {
1002 type = TREE_TYPE (type);
1003 code = TREE_CODE (type);
1004 }
1005
1006 #if 0
1007 if (code == REFERENCE_TYPE)
1008 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1009 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1010 e = convert_from_reference (e);
1011 #endif
1012
1013 if (TREE_CODE (e) == OFFSET_REF)
1014 e = resolve_offset_ref (e);
1015
1016 if (TREE_READONLY_DECL_P (e))
1017 e = decl_constant_value (e);
1018
1019 if (INTEGRAL_CODE_P (code))
1020 {
1021 tree intype = TREE_TYPE (e);
1022 /* enum = enum, enum = int, enum = float, (enum)pointer are all
1023 errors. */
1024 if (flag_int_enum_equivalence == 0
1025 && TREE_CODE (type) == ENUMERAL_TYPE
1026 && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
1027 || (TREE_CODE (intype) == POINTER_TYPE)))
1028 {
1029 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1030
1031 if (flag_pedantic_errors)
1032 return error_mark_node;
1033 }
1034 if (IS_AGGR_TYPE (intype))
1035 {
1036 tree rval;
1037 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1038 if (rval)
1039 return rval;
1040 if (flags & LOOKUP_COMPLAIN)
1041 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1042 if (flags & LOOKUP_SPECULATIVELY)
1043 return NULL_TREE;
1044 return error_mark_node;
1045 }
1046 if (code == BOOLEAN_TYPE)
1047 {
1048 /* Common Ada/Pascal programmer's mistake. We always warn
1049 about this since it is so bad. */
1050 if (TREE_CODE (expr) == FUNCTION_DECL)
1051 cp_warning ("the address of `%D', will always be `true'", expr);
1052 return truthvalue_conversion (e);
1053 }
1054 return fold (convert_to_integer (type, e));
1055 }
1056 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1057 || TYPE_PTRMEMFUNC_P (type))
1058 return fold (cp_convert_to_pointer (type, e));
1059 if (code == REAL_TYPE || code == COMPLEX_TYPE)
1060 {
1061 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1062 {
1063 tree rval;
1064 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1065 if (rval)
1066 return rval;
1067 else
1068 if (flags & LOOKUP_COMPLAIN)
1069 cp_error ("`%#T' used where a floating point value was expected",
1070 TREE_TYPE (e));
1071 }
1072 if (code == REAL_TYPE)
1073 return fold (convert_to_real (type, e));
1074 else if (code == COMPLEX_TYPE)
1075 return fold (convert_to_complex (type, e));
1076 }
1077
1078 /* New C++ semantics: since assignment is now based on
1079 memberwise copying, if the rhs type is derived from the
1080 lhs type, then we may still do a conversion. */
1081 if (IS_AGGR_TYPE_CODE (code))
1082 {
1083 tree dtype = TREE_TYPE (e);
1084 tree ctor = NULL_TREE;
1085 tree conversion = NULL_TREE;
1086
1087 dtype = TYPE_MAIN_VARIANT (dtype);
1088
1089 /* Conversion of object pointers or signature pointers/references
1090 to signature pointers/references. */
1091
1092 if (TYPE_LANG_SPECIFIC (type)
1093 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1094 {
1095 tree constructor = build_signature_pointer_constructor (type, expr);
1096 tree sig_ty = SIGNATURE_TYPE (type);
1097 tree sig_ptr;
1098
1099 if (constructor == error_mark_node)
1100 return error_mark_node;
1101
1102 sig_ptr = get_temp_name (type, 1);
1103 DECL_INITIAL (sig_ptr) = constructor;
1104 CLEAR_SIGNATURE (sig_ty);
1105 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1106 SET_SIGNATURE (sig_ty);
1107 TREE_READONLY (sig_ptr) = 1;
1108
1109 return sig_ptr;
1110 }
1111
1112 /* Conversion between aggregate types. New C++ semantics allow
1113 objects of derived type to be cast to objects of base type.
1114 Old semantics only allowed this between pointers.
1115
1116 There may be some ambiguity between using a constructor
1117 vs. using a type conversion operator when both apply. */
1118
1119 if (flag_ansi_overloading)
1120 {
1121 ctor = e;
1122
1123 if ((flags & LOOKUP_ONLYCONVERTING)
1124 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
1125 {
1126 ctor = build_user_type_conversion (type, ctor, flags);
1127 flags |= LOOKUP_NO_CONVERSION;
1128 }
1129 if (ctor)
1130 ctor = build_method_call (NULL_TREE, ctor_identifier,
1131 build_expr_list (NULL_TREE, ctor),
1132 TYPE_BINFO (type), flags);
1133 if (ctor)
1134 return build_cplus_new (type, ctor);
1135 }
1136 else
1137 {
1138 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1139 && TYPE_HAS_CONVERSION (dtype))
1140 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1141
1142 if (conversion == error_mark_node)
1143 {
1144 if (flags & LOOKUP_COMPLAIN)
1145 error ("ambiguous pointer conversion");
1146 return conversion;
1147 }
1148
1149 if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1150 ctor = build_method_call (NULL_TREE, ctor_identifier,
1151 build_expr_list (NULL_TREE, e),
1152 TYPE_BINFO (type),
1153 (flags & LOOKUP_NORMAL)
1154 | LOOKUP_SPECULATIVELY
1155 | (flags & LOOKUP_ONLYCONVERTING)
1156 | (flags & LOOKUP_NO_CONVERSION)
1157 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1158
1159 if (ctor == error_mark_node)
1160 {
1161 if (flags & LOOKUP_COMPLAIN)
1162 cp_error ("in conversion to type `%T'", type);
1163 if (flags & LOOKUP_SPECULATIVELY)
1164 return NULL_TREE;
1165 return error_mark_node;
1166 }
1167
1168 if (conversion && ctor)
1169 {
1170 if (flags & LOOKUP_COMPLAIN)
1171 error ("both constructor and type conversion operator apply");
1172 if (flags & LOOKUP_SPECULATIVELY)
1173 return NULL_TREE;
1174 return error_mark_node;
1175 }
1176 else if (conversion)
1177 return conversion;
1178 else if (ctor)
1179 {
1180 ctor = build_cplus_new (type, ctor);
1181 return ctor;
1182 }
1183 }
1184 }
1185
1186 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1187 then the it won't be hashed and hence compare as not equal,
1188 even when it is. */
1189 if (code == ARRAY_TYPE
1190 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1191 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1192 return e;
1193
1194 if (flags & LOOKUP_COMPLAIN)
1195 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1196 TREE_TYPE (expr), type);
1197 if (flags & LOOKUP_SPECULATIVELY)
1198 return NULL_TREE;
1199 return error_mark_node;
1200 }
1201
1202 /* Create an expression whose value is that of EXPR,
1203 converted to type TYPE. The TREE_TYPE of the value
1204 is always TYPE. This function implements all reasonable
1205 conversions; callers should filter out those that are
1206 not permitted by the language being compiled.
1207
1208 Most of this routine is from build_reinterpret_cast.
1209
1210 The backend cannot call cp_convert (what was convert) because
1211 conversions to/from basetypes may involve memory references
1212 (vbases) and adding or subtracting small values (multiple
1213 inheritance), but it calls convert from the constant folding code
1214 on subtrees of already build trees after it has ripped them apart.
1215
1216 Also, if we ever support range variables, we'll probably also have to
1217 do a little bit more work. */
1218
1219 tree
1220 convert (type, expr)
1221 tree type, expr;
1222 {
1223 tree intype;
1224
1225 if (type == error_mark_node || expr == error_mark_node)
1226 return error_mark_node;
1227
1228 intype = TREE_TYPE (expr);
1229
1230 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1231 {
1232 if (TREE_READONLY_DECL_P (expr))
1233 expr = decl_constant_value (expr);
1234 return fold (build1 (NOP_EXPR, type, expr));
1235 }
1236
1237 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1238 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1239 }
1240
1241 /* Like cp_convert, except permit conversions to take place which
1242 are not normally allowed due to access restrictions
1243 (such as conversion from sub-type to private super-type). */
1244
1245 tree
1246 convert_force (type, expr, convtype)
1247 tree type;
1248 tree expr;
1249 int convtype;
1250 {
1251 register tree e = expr;
1252 register enum tree_code code = TREE_CODE (type);
1253
1254 if (code == REFERENCE_TYPE)
1255 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1256 NULL_TREE));
1257 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1258 e = convert_from_reference (e);
1259
1260 if (code == POINTER_TYPE)
1261 return fold (convert_to_pointer_force (type, e));
1262
1263 /* From typeck.c convert_for_assignment */
1264 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1265 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1266 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1267 || integer_zerop (e)
1268 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1269 && TYPE_PTRMEMFUNC_P (type))
1270 {
1271 /* compatible pointer to member functions. */
1272 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1273 }
1274
1275 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1276 }
1277
1278 /* Subroutine of build_type_conversion. */
1279
1280 static tree
1281 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1282 tree xtype, basetype;
1283 tree expr;
1284 tree typename;
1285 int for_sure;
1286 {
1287 tree rval;
1288 int flags;
1289
1290 if (for_sure == 0)
1291 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1292 else
1293 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1294
1295 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1296 if (rval == error_mark_node)
1297 {
1298 if (for_sure == 0)
1299 return NULL_TREE;
1300 return error_mark_node;
1301 }
1302
1303 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1304 return rval;
1305
1306 if (warn_cast_qual
1307 && TREE_TYPE (xtype)
1308 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1309 > TREE_READONLY (TREE_TYPE (xtype))))
1310 warning ("user-defined conversion casting away `const'");
1311 return cp_convert (xtype, rval);
1312 }
1313
1314 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1315 exists, return the attempted conversion. This may
1316 return ERROR_MARK_NODE if the conversion is not
1317 allowed (references private members, etc).
1318 If no conversion exists, NULL_TREE is returned.
1319
1320 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1321 to take place immediately. Otherwise, we build a SAVE_EXPR
1322 which can be evaluated if the results are ever needed.
1323
1324 Changes to this functions should be mirrored in user_harshness.
1325
1326 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1327 object parameter, or by the second standard conversion sequence if
1328 that doesn't do it. This will probably wait for an overloading rewrite.
1329 (jason 8/9/95) */
1330
1331 tree
1332 build_type_conversion (code, xtype, expr, for_sure)
1333 enum tree_code code;
1334 tree xtype, expr;
1335 int for_sure;
1336 {
1337 /* C++: check to see if we can convert this aggregate type
1338 into the required type. */
1339 tree basetype;
1340 tree conv;
1341 tree winner = NULL_TREE;
1342
1343 if (flag_ansi_overloading)
1344 return build_user_type_conversion
1345 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1346
1347 if (expr == error_mark_node)
1348 return error_mark_node;
1349
1350 basetype = TREE_TYPE (expr);
1351 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1352 basetype = TREE_TYPE (basetype);
1353
1354 basetype = TYPE_MAIN_VARIANT (basetype);
1355 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1356 return NULL_TREE;
1357
1358 /* Do we have an exact match? */
1359 {
1360 tree typename = build_typename_overload (xtype);
1361 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1362 return build_type_conversion_1 (xtype, basetype, expr, typename,
1363 for_sure);
1364 }
1365
1366 /* Nope; try looking for others. */
1367 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1368 {
1369 tree cand = TREE_VALUE (conv);
1370
1371 if (winner && winner == cand)
1372 continue;
1373
1374 if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1375 {
1376 if (winner)
1377 {
1378 if (for_sure)
1379 {
1380 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1381 xtype);
1382 cp_error (" candidate conversions include `%D' and `%D'",
1383 winner, cand);
1384 }
1385 return NULL_TREE;
1386 }
1387 else
1388 winner = cand;
1389 }
1390 }
1391
1392 if (winner)
1393 return build_type_conversion_1 (xtype, basetype, expr,
1394 DECL_NAME (winner), for_sure);
1395
1396 return NULL_TREE;
1397 }
1398
1399 /* Convert the given EXPR to one of a group of types suitable for use in an
1400 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1401 which indicates which types are suitable. If COMPLAIN is 1, complain
1402 about ambiguity; otherwise, the caller will deal with it. */
1403
1404 tree
1405 build_expr_type_conversion (desires, expr, complain)
1406 int desires;
1407 tree expr;
1408 int complain;
1409 {
1410 tree basetype = TREE_TYPE (expr);
1411 tree conv;
1412 tree winner = NULL_TREE;
1413
1414 if (TREE_CODE (basetype) == OFFSET_TYPE)
1415 expr = resolve_offset_ref (expr);
1416 expr = convert_from_reference (expr);
1417 basetype = TREE_TYPE (expr);
1418
1419 if (! IS_AGGR_TYPE (basetype))
1420 switch (TREE_CODE (basetype))
1421 {
1422 case INTEGER_TYPE:
1423 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1424 && integer_zerop (expr))
1425 return expr;
1426 /* else fall through... */
1427
1428 case BOOLEAN_TYPE:
1429 return (desires & WANT_INT) ? expr : NULL_TREE;
1430 case ENUMERAL_TYPE:
1431 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1432 case REAL_TYPE:
1433 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1434 case POINTER_TYPE:
1435 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1436
1437 case FUNCTION_TYPE:
1438 case ARRAY_TYPE:
1439 return (desires & WANT_POINTER) ? default_conversion (expr)
1440 : NULL_TREE;
1441 default:
1442 return NULL_TREE;
1443 }
1444
1445 if (! TYPE_HAS_CONVERSION (basetype))
1446 return NULL_TREE;
1447
1448 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1449 {
1450 int win = 0;
1451 tree candidate;
1452 tree cand = TREE_VALUE (conv);
1453
1454 if (winner && winner == cand)
1455 continue;
1456
1457 candidate = TREE_TYPE (TREE_TYPE (cand));
1458 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1459 candidate = TREE_TYPE (candidate);
1460
1461 switch (TREE_CODE (candidate))
1462 {
1463 case BOOLEAN_TYPE:
1464 case INTEGER_TYPE:
1465 win = (desires & WANT_INT); break;
1466 case ENUMERAL_TYPE:
1467 win = (desires & WANT_ENUM); break;
1468 case REAL_TYPE:
1469 win = (desires & WANT_FLOAT); break;
1470 case POINTER_TYPE:
1471 win = (desires & WANT_POINTER); break;
1472
1473 default:
1474 break;
1475 }
1476
1477 if (win)
1478 {
1479 if (winner)
1480 {
1481 if (complain)
1482 {
1483 cp_error ("ambiguous default type conversion from `%T'",
1484 basetype);
1485 cp_error (" candidate conversions include `%D' and `%D'",
1486 winner, cand);
1487 }
1488 return error_mark_node;
1489 }
1490 else
1491 winner = cand;
1492 }
1493 }
1494
1495 if (winner)
1496 {
1497 tree type = TREE_TYPE (TREE_TYPE (winner));
1498 if (TREE_CODE (type) == REFERENCE_TYPE)
1499 type = TREE_TYPE (type);
1500 return build_type_conversion_1 (type, basetype, expr,
1501 DECL_NAME (winner), 1);
1502 }
1503
1504 return NULL_TREE;
1505 }
1506
1507 /* Must convert two aggregate types to non-aggregate type.
1508 Attempts to find a non-ambiguous, "best" type conversion.
1509
1510 Return 1 on success, 0 on failure.
1511
1512 @@ What are the real semantics of this supposed to be??? */
1513
1514 int
1515 build_default_binary_type_conversion (code, arg1, arg2)
1516 enum tree_code code;
1517 tree *arg1, *arg2;
1518 {
1519 switch (code)
1520 {
1521 case MULT_EXPR:
1522 case TRUNC_DIV_EXPR:
1523 case CEIL_DIV_EXPR:
1524 case FLOOR_DIV_EXPR:
1525 case ROUND_DIV_EXPR:
1526 case EXACT_DIV_EXPR:
1527 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1528 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1529 break;
1530
1531 case TRUNC_MOD_EXPR:
1532 case FLOOR_MOD_EXPR:
1533 case LSHIFT_EXPR:
1534 case RSHIFT_EXPR:
1535 case BIT_AND_EXPR:
1536 case BIT_XOR_EXPR:
1537 case BIT_IOR_EXPR:
1538 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1539 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1540 break;
1541
1542 case PLUS_EXPR:
1543 {
1544 tree a1, a2, p1, p2;
1545 int wins;
1546
1547 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1548 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1549 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1550 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1551
1552 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1553
1554 if (wins > 1)
1555 error ("ambiguous default type conversion for `operator +'");
1556
1557 if (a1 && a2)
1558 *arg1 = a1, *arg2 = a2;
1559 else if (a1 && p2)
1560 *arg1 = a1, *arg2 = p2;
1561 else
1562 *arg1 = p1, *arg2 = a2;
1563 break;
1564 }
1565
1566 case MINUS_EXPR:
1567 {
1568 tree a1, a2, p1, p2;
1569 int wins;
1570
1571 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1572 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1573 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1574 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1575
1576 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1577
1578 if (wins > 1)
1579 error ("ambiguous default type conversion for `operator -'");
1580
1581 if (a1 && a2)
1582 *arg1 = a1, *arg2 = a2;
1583 else if (p1 && p2)
1584 *arg1 = p1, *arg2 = p2;
1585 else
1586 *arg1 = p1, *arg2 = a2;
1587 break;
1588 }
1589
1590 case GT_EXPR:
1591 case LT_EXPR:
1592 case GE_EXPR:
1593 case LE_EXPR:
1594 case EQ_EXPR:
1595 case NE_EXPR:
1596 {
1597 tree a1, a2, p1, p2;
1598 int wins;
1599
1600 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1601 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1602 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1603 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1604
1605 wins = (a1 && a2) + (p1 && p2);
1606
1607 if (wins > 1)
1608 cp_error ("ambiguous default type conversion for `%O'", code);
1609
1610 if (a1 && a2)
1611 *arg1 = a1, *arg2 = a2;
1612 else
1613 *arg1 = p1, *arg2 = p2;
1614 break;
1615 }
1616
1617 case TRUTH_ANDIF_EXPR:
1618 case TRUTH_ORIF_EXPR:
1619 *arg1 = cp_convert (boolean_type_node, *arg1);
1620 *arg2 = cp_convert (boolean_type_node, *arg2);
1621 break;
1622
1623 default:
1624 *arg1 = NULL_TREE;
1625 *arg2 = NULL_TREE;
1626 }
1627
1628 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1629 cp_error ("ambiguous default type conversion for `%O'", code);
1630
1631 if (*arg1 && *arg2)
1632 return 1;
1633
1634 return 0;
1635 }
1636
1637 /* Implements integral promotion (4.1) and float->double promotion. */
1638
1639 tree
1640 type_promotes_to (type)
1641 tree type;
1642 {
1643 int constp, volatilep;
1644
1645 if (type == error_mark_node)
1646 return error_mark_node;
1647
1648 constp = TYPE_READONLY (type);
1649 volatilep = TYPE_VOLATILE (type);
1650 type = TYPE_MAIN_VARIANT (type);
1651
1652 /* bool always promotes to int (not unsigned), even if it's the same
1653 size. */
1654 if (type == boolean_type_node)
1655 type = integer_type_node;
1656
1657 /* Normally convert enums to int, but convert wide enums to something
1658 wider. */
1659 else if (TREE_CODE (type) == ENUMERAL_TYPE
1660 || type == wchar_type_node)
1661 {
1662 int precision = MAX (TYPE_PRECISION (type),
1663 TYPE_PRECISION (integer_type_node));
1664 tree totype = type_for_size (precision, 0);
1665 if (TREE_UNSIGNED (type)
1666 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1667 type = type_for_size (precision, 1);
1668 else
1669 type = totype;
1670 }
1671 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1672 {
1673 /* Retain unsignedness if really not getting bigger. */
1674 if (TREE_UNSIGNED (type)
1675 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1676 type = unsigned_type_node;
1677 else
1678 type = integer_type_node;
1679 }
1680 else if (type == float_type_node)
1681 type = double_type_node;
1682
1683 return cp_build_type_variant (type, constp, volatilep);
1684 }
1685
1686
1687 /* The routines below this point are carefully written to conform to
1688 the standard. They use the same terminology, and follow the rules
1689 closely. Although they are used only in pt.c at the moment, they
1690 should presumably be used everywhere in the future. */
1691
1692 /* Attempt to perform qualification conversions on EXPR to convert it
1693 to TYPE. Return the resulting expression, or error_mark_node if
1694 the conversion was impossible. */
1695
1696 tree
1697 perform_qualification_conversions (type, expr)
1698 tree type;
1699 tree expr;
1700 {
1701 if (comp_ptr_ttypes (type, TREE_TYPE(expr)))
1702 return build1 (NOP_EXPR, type, expr);
1703 else
1704 return error_mark_node;
1705 }