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