OSDN Git Service

* cvt.c (ocp_convert): Obtain the constant values from constant
[pf3gnuchains/gcc-fork.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           {
822             if (saw_protected)
823               *msgp = "only private and protected conversions apply";
824             else
825               *msgp = "only private conversions apply";
826           }
827         else if (saw_protected)
828           *msgp = "only protected conversions apply";
829         else
830           *msgp = "no appropriate conversion to type `%s'";
831       }
832     return error_mark_node;
833   }
834   /* NOTREACHED */
835
836  found:
837   if (access == access_private_node)
838     if (! can_be_private)
839       {
840         if (msgp)
841           *msgp = TREE_PRIVATE (fndecl)
842             ? "conversion to type `%s' is private"
843             : "conversion to type `%s' is from private base class";
844         return error_mark_node;
845       }
846   if (access == access_protected_node)
847     if (! can_be_protected)
848       {
849         if (msgp)
850           *msgp = TREE_PRIVATE (fndecl)
851             ? "conversion to type `%s' is protected"
852             : "conversion to type `%s' is from protected base class";
853         return error_mark_node;
854       }
855   function = fndecl;
856  found_and_ok:
857
858   /* It will convert, but we don't do anything about it yet.  */
859   if (msgp == 0)
860     return NULL_TREE;
861
862   fntype = TREE_TYPE (function);
863
864   parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
865                                 parmlist, NULL_TREE, LOOKUP_NORMAL);
866
867   result = build_call (function, TREE_TYPE (fntype), parmlist);
868   return result;
869 }
870
871 /* Call this when we know (for any reason) that expr is not, in fact,
872    zero.  This routine is like convert_pointer_to, but it pays
873    attention to which specific instance of what type we want to
874    convert to.  This routine should eventually become
875    convert_to_pointer after all references to convert_to_pointer
876    are removed.  */
877
878 tree
879 convert_pointer_to_real (binfo, expr)
880      tree binfo, expr;
881 {
882   register tree intype = TREE_TYPE (expr);
883   tree ptr_type;
884   tree type, rval;
885
886   if (TREE_CODE (binfo) == TREE_VEC)
887     type = BINFO_TYPE (binfo);
888   else if (IS_AGGR_TYPE (binfo))
889     {
890       type = binfo;
891     }
892   else
893     {
894       type = binfo;
895       binfo = NULL_TREE;
896     }
897
898   ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
899                                     TYPE_VOLATILE (TREE_TYPE (intype)));
900   ptr_type = build_pointer_type (ptr_type);
901   if (ptr_type == TYPE_MAIN_VARIANT (intype))
902     return expr;
903
904   if (intype == error_mark_node)
905     return error_mark_node;
906
907   my_friendly_assert (!integer_zerop (expr), 191);
908
909   if (TREE_CODE (type) == RECORD_TYPE
910       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
911       && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
912     {
913       tree path;
914       int distance
915         = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
916                              0, &path);
917
918       /* This function shouldn't be called with unqualified arguments
919          but if it is, give them an error message that they can read.  */
920       if (distance < 0)
921         {
922           cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
923                     TREE_TYPE (intype), type);
924
925           if (distance == -2)
926             cp_error ("because `%T' is an ambiguous base class", type);
927           return error_mark_node;
928         }
929
930       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
931     }
932   rval = build1 (NOP_EXPR, ptr_type,
933                  TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
934   TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
935   return rval;
936 }
937
938 /* Call this when we know (for any reason) that expr is
939    not, in fact, zero.  This routine gets a type out of the first
940    argument and uses it to search for the type to convert to.  If there
941    is more than one instance of that type in the expr, the conversion is
942    ambiguous.  This routine should eventually go away, and all
943    callers should use convert_to_pointer_real.  */
944
945 tree
946 convert_pointer_to (binfo, expr)
947      tree binfo, expr;
948 {
949   tree type;
950
951   if (TREE_CODE (binfo) == TREE_VEC)
952     type = BINFO_TYPE (binfo);
953   else if (IS_AGGR_TYPE (binfo))
954       type = binfo;
955   else
956       type = binfo;
957   return convert_pointer_to_real (type, expr);
958 }
959 \f
960 /* C++ conversions, preference to static cast conversions.  */
961
962 tree
963 cp_convert (type, expr)
964      tree type, expr;
965 {
966   return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
967 }
968
969 /* Conversion...
970
971    FLAGS indicates how we should behave.  */
972
973 tree
974 ocp_convert (type, expr, convtype, flags)
975      tree type, expr;
976      int convtype, flags;
977 {
978   register tree e = expr;
979   register enum tree_code code = TREE_CODE (type);
980
981   if (e == error_mark_node
982       || TREE_TYPE (e) == error_mark_node)
983     return error_mark_node;
984
985   if (TREE_READONLY_DECL_P (e))
986     e = decl_constant_value (e);
987
988   if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
989     /* We need a new temporary; don't take this shortcut.  */;
990   else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
991     /* Trivial conversion: cv-qualifiers do not matter on rvalues.  */
992     return fold (build1 (NOP_EXPR, type, e));
993   
994   if (code == VOID_TYPE && (convtype & CONV_STATIC))
995     return build1 (CONVERT_EXPR, type, e);
996
997 #if 0
998   /* This is incorrect.  A truncation can't be stripped this way.
999      Extensions will be stripped by the use of get_unwidened.  */
1000   if (TREE_CODE (e) == NOP_EXPR)
1001     return cp_convert (type, TREE_OPERAND (e, 0));
1002 #endif
1003
1004   /* Just convert to the type of the member.  */
1005   if (code == OFFSET_TYPE)
1006     {
1007       type = TREE_TYPE (type);
1008       code = TREE_CODE (type);
1009     }
1010
1011 #if 0
1012   if (code == REFERENCE_TYPE)
1013     return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1014   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1015     e = convert_from_reference (e);
1016 #endif
1017
1018   if (TREE_CODE (e) == OFFSET_REF)
1019     e = resolve_offset_ref (e);
1020
1021   if (INTEGRAL_CODE_P (code))
1022     {
1023       tree intype = TREE_TYPE (e);
1024       /* enum = enum, enum = int, enum = float, (enum)pointer are all
1025          errors.  */
1026       if (flag_int_enum_equivalence == 0
1027           && TREE_CODE (type) == ENUMERAL_TYPE
1028           && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
1029               || (TREE_CODE (intype) == POINTER_TYPE)))
1030         {
1031           cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1032
1033           if (flag_pedantic_errors)
1034             return error_mark_node;
1035         }
1036       if (IS_AGGR_TYPE (intype))
1037         {
1038           tree rval;
1039           rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1040           if (rval)
1041             return rval;
1042           if (flags & LOOKUP_COMPLAIN)
1043             cp_error ("`%#T' used where a `%T' was expected", intype, type);
1044           if (flags & LOOKUP_SPECULATIVELY)
1045             return NULL_TREE;
1046           return error_mark_node;
1047         }
1048       if (code == BOOLEAN_TYPE)
1049         {
1050           /* Common Ada/Pascal programmer's mistake.  We always warn
1051              about this since it is so bad.  */
1052           if (TREE_CODE (expr) == FUNCTION_DECL)
1053             cp_warning ("the address of `%D', will always be `true'", expr);
1054           return truthvalue_conversion (e);
1055         }
1056       return fold (convert_to_integer (type, e));
1057     }
1058   if (code == POINTER_TYPE || code == REFERENCE_TYPE
1059       || TYPE_PTRMEMFUNC_P (type))
1060     return fold (cp_convert_to_pointer (type, e));
1061   if (code == REAL_TYPE || code == COMPLEX_TYPE)
1062     {
1063       if (IS_AGGR_TYPE (TREE_TYPE (e)))
1064         {
1065           tree rval;
1066           rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1067           if (rval)
1068             return rval;
1069           else
1070             if (flags & LOOKUP_COMPLAIN)
1071               cp_error ("`%#T' used where a floating point value was expected",
1072                         TREE_TYPE (e));
1073         }
1074       if (code == REAL_TYPE)
1075         return fold (convert_to_real (type, e));
1076       else if (code == COMPLEX_TYPE)
1077         return fold (convert_to_complex (type, e));
1078     }
1079
1080   /* New C++ semantics:  since assignment is now based on
1081      memberwise copying,  if the rhs type is derived from the
1082      lhs type, then we may still do a conversion.  */
1083   if (IS_AGGR_TYPE_CODE (code))
1084     {
1085       tree dtype = TREE_TYPE (e);
1086       tree ctor = NULL_TREE;
1087       tree conversion = NULL_TREE;
1088
1089       dtype = TYPE_MAIN_VARIANT (dtype);
1090
1091       /* Conversion of object pointers or signature pointers/references
1092          to signature pointers/references.  */
1093
1094       if (TYPE_LANG_SPECIFIC (type)
1095           && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1096         {
1097           tree constructor = build_signature_pointer_constructor (type, expr);
1098           tree sig_ty = SIGNATURE_TYPE (type);
1099           tree sig_ptr;
1100
1101           if (constructor == error_mark_node)
1102             return error_mark_node;
1103
1104           sig_ptr = get_temp_name (type, 1);
1105           DECL_INITIAL (sig_ptr) = constructor;
1106           CLEAR_SIGNATURE (sig_ty);
1107           cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1108           SET_SIGNATURE (sig_ty);
1109           TREE_READONLY (sig_ptr) = 1;
1110
1111           return sig_ptr;
1112         }
1113
1114       /* Conversion between aggregate types.  New C++ semantics allow
1115          objects of derived type to be cast to objects of base type.
1116          Old semantics only allowed this between pointers.
1117
1118          There may be some ambiguity between using a constructor
1119          vs. using a type conversion operator when both apply.  */
1120
1121       if (flag_ansi_overloading)
1122         {
1123           ctor = e;
1124           
1125           if ((flags & LOOKUP_ONLYCONVERTING)
1126               && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
1127             {
1128               ctor = build_user_type_conversion (type, ctor, flags);
1129               flags |= LOOKUP_NO_CONVERSION;
1130             }
1131           if (ctor)
1132             ctor = build_method_call (NULL_TREE, ctor_identifier,
1133                                       build_expr_list (NULL_TREE, ctor),
1134                                       TYPE_BINFO (type), flags);
1135           if (ctor)
1136             return build_cplus_new (type, ctor);
1137         }
1138       else
1139         {
1140           if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1141               && TYPE_HAS_CONVERSION (dtype))
1142             conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1143
1144           if (conversion == error_mark_node)
1145             {
1146               if (flags & LOOKUP_COMPLAIN)
1147                 error ("ambiguous pointer conversion");
1148               return conversion;
1149             }
1150
1151           if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1152             ctor = build_method_call (NULL_TREE, ctor_identifier,
1153                                       build_expr_list (NULL_TREE, e),
1154                                       TYPE_BINFO (type),
1155                                       (flags & LOOKUP_NORMAL)
1156                                       | LOOKUP_SPECULATIVELY
1157                                       | (flags & LOOKUP_ONLYCONVERTING)
1158                                       | (flags & LOOKUP_NO_CONVERSION)
1159                                       | (conversion ? LOOKUP_NO_CONVERSION : 0));
1160
1161           if (ctor == error_mark_node)
1162             {
1163               if (flags & LOOKUP_COMPLAIN)
1164                 cp_error ("in conversion to type `%T'", type);
1165               if (flags & LOOKUP_SPECULATIVELY)
1166                 return NULL_TREE;
1167               return error_mark_node;
1168             }
1169       
1170           if (conversion && ctor)
1171             {
1172               if (flags & LOOKUP_COMPLAIN)
1173                 error ("both constructor and type conversion operator apply");
1174               if (flags & LOOKUP_SPECULATIVELY)
1175                 return NULL_TREE;
1176               return error_mark_node;
1177             }
1178           else if (conversion)
1179             return conversion;
1180           else if (ctor)
1181             {
1182               ctor = build_cplus_new (type, ctor);
1183               return ctor;
1184             }
1185         }
1186     }
1187
1188   /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1189      then it won't be hashed and hence compare as not equal,
1190      even when it is.  */
1191   if (code == ARRAY_TYPE
1192       && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1193       && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1194     return e;
1195
1196   if (flags & LOOKUP_COMPLAIN)
1197     cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1198               TREE_TYPE (expr), type);
1199   if (flags & LOOKUP_SPECULATIVELY)
1200     return NULL_TREE;
1201   return error_mark_node;
1202 }
1203
1204 /* Create an expression whose value is that of EXPR,
1205    converted to type TYPE.  The TREE_TYPE of the value
1206    is always TYPE.  This function implements all reasonable
1207    conversions; callers should filter out those that are
1208    not permitted by the language being compiled.
1209
1210    Most of this routine is from build_reinterpret_cast.
1211
1212    The backend cannot call cp_convert (what was convert) because
1213    conversions to/from basetypes may involve memory references
1214    (vbases) and adding or subtracting small values (multiple
1215    inheritance), but it calls convert from the constant folding code
1216    on subtrees of already build trees after it has ripped them apart.
1217
1218    Also, if we ever support range variables, we'll probably also have to
1219    do a little bit more work.  */
1220
1221 tree
1222 convert (type, expr)
1223      tree type, expr;
1224 {
1225   tree intype;
1226
1227   if (type == error_mark_node || expr == error_mark_node)
1228     return error_mark_node;
1229
1230   intype = TREE_TYPE (expr);
1231
1232   if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1233     {
1234       if (TREE_READONLY_DECL_P (expr))
1235         expr = decl_constant_value (expr);
1236       return fold (build1 (NOP_EXPR, type, expr));
1237     }
1238
1239   return ocp_convert (type, expr, CONV_OLD_CONVERT,
1240                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1241 }
1242
1243 /* Like cp_convert, except permit conversions to take place which
1244    are not normally allowed due to access restrictions
1245    (such as conversion from sub-type to private super-type).  */
1246
1247 tree
1248 convert_force (type, expr, convtype)
1249      tree type;
1250      tree expr;
1251      int convtype;
1252 {
1253   register tree e = expr;
1254   register enum tree_code code = TREE_CODE (type);
1255
1256   if (code == REFERENCE_TYPE)
1257     return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1258                                        NULL_TREE));
1259   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1260     e = convert_from_reference (e);
1261
1262   if (code == POINTER_TYPE)
1263     return fold (convert_to_pointer_force (type, e));
1264
1265   /* From typeck.c convert_for_assignment */
1266   if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1267         && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1268         && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1269        || integer_zerop (e)
1270        || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1271       && TYPE_PTRMEMFUNC_P (type))
1272     {
1273       /* compatible pointer to member functions.  */
1274       return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1275     }
1276
1277   return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1278 }
1279
1280 /* Subroutine of build_type_conversion.  */
1281
1282 static tree
1283 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1284      tree xtype, basetype;
1285      tree expr;
1286      tree typename;
1287      int for_sure;
1288 {
1289   tree rval;
1290   int flags;
1291
1292   if (for_sure == 0)
1293     flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1294   else
1295     flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1296
1297   rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1298   if (rval == error_mark_node)
1299     {
1300       if (for_sure == 0)
1301         return NULL_TREE;
1302       return error_mark_node;
1303     }
1304
1305   if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1306     return rval;
1307
1308   if (warn_cast_qual
1309       && TREE_TYPE (xtype)
1310       && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1311           > TREE_READONLY (TREE_TYPE (xtype))))
1312     warning ("user-defined conversion casting away `const'");
1313   return cp_convert (xtype, rval);
1314 }
1315
1316 /* Convert an aggregate EXPR to type XTYPE.  If a conversion
1317    exists, return the attempted conversion.  This may
1318    return ERROR_MARK_NODE if the conversion is not
1319    allowed (references private members, etc).
1320    If no conversion exists, NULL_TREE is returned.
1321
1322    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1323    to take place immediately.  Otherwise, we build a SAVE_EXPR
1324    which can be evaluated if the results are ever needed.
1325
1326    Changes to this functions should be mirrored in user_harshness.
1327
1328    FIXME: Ambiguity checking is wrong.  Should choose one by the implicit
1329    object parameter, or by the second standard conversion sequence if
1330    that doesn't do it.  This will probably wait for an overloading rewrite.
1331    (jason 8/9/95)  */
1332
1333 tree
1334 build_type_conversion (code, xtype, expr, for_sure)
1335      enum tree_code code;
1336      tree xtype, expr;
1337      int for_sure;
1338 {
1339   /* C++: check to see if we can convert this aggregate type
1340      into the required type.  */
1341   tree basetype;
1342   tree conv;
1343   tree winner = NULL_TREE;
1344
1345   if (flag_ansi_overloading)
1346     return build_user_type_conversion
1347       (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1348
1349   if (expr == error_mark_node)
1350     return error_mark_node;
1351
1352   basetype = TREE_TYPE (expr);
1353   if (TREE_CODE (basetype) == REFERENCE_TYPE)
1354     basetype = TREE_TYPE (basetype);
1355
1356   basetype = TYPE_MAIN_VARIANT (basetype);
1357   if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1358     return NULL_TREE;
1359
1360   /* Do we have an exact match?  */
1361   {
1362     tree typename = build_typename_overload (xtype);
1363     if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1364       return build_type_conversion_1 (xtype, basetype, expr, typename,
1365                                       for_sure);
1366   }
1367
1368   /* Nope; try looking for others.  */
1369   for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1370     {
1371       tree cand = TREE_VALUE (conv);
1372
1373       if (winner && winner == cand)
1374         continue;
1375
1376       if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1377         {
1378           if (winner)
1379             {
1380               if (for_sure)
1381                 {
1382                   cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1383                             xtype);
1384                   cp_error ("  candidate conversions include `%D' and `%D'",
1385                             winner, cand);
1386                 }
1387               return NULL_TREE;
1388             }
1389           else
1390             winner = cand;
1391         }
1392     }
1393
1394   if (winner)
1395     return build_type_conversion_1 (xtype, basetype, expr,
1396                                     DECL_NAME (winner), for_sure);
1397
1398   return NULL_TREE;
1399 }
1400
1401 /* Convert the given EXPR to one of a group of types suitable for use in an
1402    expression.  DESIRES is a combination of various WANT_* flags (q.v.)
1403    which indicates which types are suitable.  If COMPLAIN is 1, complain
1404    about ambiguity; otherwise, the caller will deal with it.  */
1405
1406 tree
1407 build_expr_type_conversion (desires, expr, complain)
1408      int desires;
1409      tree expr;
1410      int complain;
1411 {
1412   tree basetype = TREE_TYPE (expr);
1413   tree conv;
1414   tree winner = NULL_TREE;
1415
1416   if (TREE_CODE (basetype) == OFFSET_TYPE)
1417     expr = resolve_offset_ref (expr);
1418   expr = convert_from_reference (expr);
1419   basetype = TREE_TYPE (expr);
1420
1421   if (! IS_AGGR_TYPE (basetype))
1422     switch (TREE_CODE (basetype))
1423       {
1424       case INTEGER_TYPE:
1425         if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1426             && integer_zerop (expr))
1427           return expr;
1428         /* else fall through...  */
1429
1430       case BOOLEAN_TYPE:
1431         return (desires & WANT_INT) ? expr : NULL_TREE;
1432       case ENUMERAL_TYPE:
1433         return (desires & WANT_ENUM) ? expr : NULL_TREE;
1434       case REAL_TYPE:
1435         return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1436       case POINTER_TYPE:
1437         return (desires & WANT_POINTER) ? expr : NULL_TREE;
1438         
1439       case FUNCTION_TYPE:
1440       case ARRAY_TYPE:
1441         return (desires & WANT_POINTER) ? default_conversion (expr)
1442                                         : NULL_TREE;
1443       default:
1444         return NULL_TREE;
1445       }
1446
1447   if (! TYPE_HAS_CONVERSION (basetype))
1448     return NULL_TREE;
1449
1450   for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1451     {
1452       int win = 0;
1453       tree candidate;
1454       tree cand = TREE_VALUE (conv);
1455
1456       if (winner && winner == cand)
1457         continue;
1458
1459       candidate = TREE_TYPE (TREE_TYPE (cand));
1460       if (TREE_CODE (candidate) == REFERENCE_TYPE)
1461         candidate = TREE_TYPE (candidate);
1462
1463       switch (TREE_CODE (candidate))
1464         {
1465         case BOOLEAN_TYPE:
1466         case INTEGER_TYPE:
1467           win = (desires & WANT_INT); break;
1468         case ENUMERAL_TYPE:
1469           win = (desires & WANT_ENUM); break;
1470         case REAL_TYPE:
1471           win = (desires & WANT_FLOAT); break;
1472         case POINTER_TYPE:
1473           win = (desires & WANT_POINTER); break;
1474
1475         default:
1476           break;
1477         }
1478
1479       if (win)
1480         {
1481           if (winner)
1482             {
1483               if (complain)
1484                 {
1485                   cp_error ("ambiguous default type conversion from `%T'",
1486                             basetype);
1487                   cp_error ("  candidate conversions include `%D' and `%D'",
1488                             winner, cand);
1489                 }
1490               return error_mark_node;
1491             }
1492           else
1493             winner = cand;
1494         }
1495     }
1496
1497   if (winner)
1498     {
1499       tree type = TREE_TYPE (TREE_TYPE (winner));
1500       if (TREE_CODE (type) == REFERENCE_TYPE)
1501         type = TREE_TYPE (type);
1502       return build_type_conversion_1 (type, basetype, expr,
1503                                       DECL_NAME (winner), 1);
1504     }
1505
1506   return NULL_TREE;
1507 }
1508
1509 /* Must convert two aggregate types to non-aggregate type.
1510    Attempts to find a non-ambiguous, "best" type conversion.
1511
1512    Return 1 on success, 0 on failure.
1513
1514    @@ What are the real semantics of this supposed to be??? */
1515
1516 int
1517 build_default_binary_type_conversion (code, arg1, arg2)
1518      enum tree_code code;
1519      tree *arg1, *arg2;
1520 {
1521   switch (code)
1522     {
1523     case MULT_EXPR:
1524     case TRUNC_DIV_EXPR:
1525     case CEIL_DIV_EXPR:
1526     case FLOOR_DIV_EXPR:
1527     case ROUND_DIV_EXPR:
1528     case EXACT_DIV_EXPR:
1529       *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1530       *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1531       break;
1532
1533     case TRUNC_MOD_EXPR:
1534     case FLOOR_MOD_EXPR:
1535     case LSHIFT_EXPR:
1536     case RSHIFT_EXPR:
1537     case BIT_AND_EXPR:
1538     case BIT_XOR_EXPR:
1539     case BIT_IOR_EXPR:
1540       *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1541       *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1542       break;
1543
1544     case PLUS_EXPR:
1545       {
1546         tree a1, a2, p1, p2;
1547         int wins;
1548
1549         a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1550         a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1551         p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1552         p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1553
1554         wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1555
1556         if (wins > 1)
1557           error ("ambiguous default type conversion for `operator +'");
1558
1559         if (a1 && a2)
1560           *arg1 = a1, *arg2 = a2;
1561         else if (a1 && p2)
1562           *arg1 = a1, *arg2 = p2;
1563         else
1564           *arg1 = p1, *arg2 = a2;
1565         break;
1566       }
1567
1568     case MINUS_EXPR:
1569       {
1570         tree a1, a2, p1, p2;
1571         int wins;
1572
1573         a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1574         a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1575         p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1576         p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1577
1578         wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1579
1580         if (wins > 1)
1581           error ("ambiguous default type conversion for `operator -'");
1582
1583         if (a1 && a2)
1584           *arg1 = a1, *arg2 = a2;
1585         else if (p1 && p2)
1586           *arg1 = p1, *arg2 = p2;
1587         else
1588           *arg1 = p1, *arg2 = a2;
1589         break;
1590       }
1591
1592     case GT_EXPR:
1593     case LT_EXPR:
1594     case GE_EXPR:
1595     case LE_EXPR:
1596     case EQ_EXPR:
1597     case NE_EXPR:
1598       {
1599         tree a1, a2, p1, p2;
1600         int wins;
1601
1602         a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1603         a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1604         p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1605         p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1606
1607         wins = (a1 && a2) + (p1 && p2);
1608
1609         if (wins > 1)
1610           cp_error ("ambiguous default type conversion for `%O'", code);
1611
1612         if (a1 && a2)
1613           *arg1 = a1, *arg2 = a2;
1614         else
1615           *arg1 = p1, *arg2 = p2;
1616         break;
1617       }
1618
1619     case TRUTH_ANDIF_EXPR:
1620     case TRUTH_ORIF_EXPR:
1621       *arg1 = cp_convert (boolean_type_node, *arg1);
1622       *arg2 = cp_convert (boolean_type_node, *arg2);
1623       break;
1624
1625     default:
1626       *arg1 = NULL_TREE;
1627       *arg2 = NULL_TREE;
1628     }
1629
1630   if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1631     cp_error ("ambiguous default type conversion for `%O'", code);
1632
1633   if (*arg1 && *arg2)
1634     return 1;
1635
1636   return 0;
1637 }
1638
1639 /* Implements integral promotion (4.1) and float->double promotion.  */
1640
1641 tree
1642 type_promotes_to (type)
1643      tree type;
1644 {
1645   int constp, volatilep;
1646
1647   if (type == error_mark_node)
1648     return error_mark_node;
1649
1650   constp = TYPE_READONLY (type);
1651   volatilep = TYPE_VOLATILE (type);
1652   type = TYPE_MAIN_VARIANT (type);
1653
1654   /* bool always promotes to int (not unsigned), even if it's the same
1655      size.  */
1656   if (type == boolean_type_node)
1657     type = integer_type_node;
1658
1659   /* Normally convert enums to int, but convert wide enums to something
1660      wider.  */
1661   else if (TREE_CODE (type) == ENUMERAL_TYPE
1662            || type == wchar_type_node)
1663     {
1664       int precision = MAX (TYPE_PRECISION (type),
1665                            TYPE_PRECISION (integer_type_node));
1666       tree totype = type_for_size (precision, 0);
1667       if (TREE_UNSIGNED (type)
1668           && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1669         type = type_for_size (precision, 1);
1670       else
1671         type = totype;
1672     }
1673   else if (C_PROMOTING_INTEGER_TYPE_P (type))
1674     {
1675       /* Retain unsignedness if really not getting bigger.  */
1676       if (TREE_UNSIGNED (type)
1677           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1678         type = unsigned_type_node;
1679       else
1680         type = integer_type_node;
1681     }
1682   else if (type == float_type_node)
1683     type = double_type_node;
1684
1685   return cp_build_type_variant (type, constp, volatilep);
1686 }
1687
1688
1689 /* The routines below this point are carefully written to conform to
1690    the standard.  They use the same terminology, and follow the rules
1691    closely.  Although they are used only in pt.c at the moment, they
1692    should presumably be used everywhere in the future.  */
1693
1694 /* Attempt to perform qualification conversions on EXPR to convert it
1695    to TYPE.  Return the resulting expression, or error_mark_node if
1696    the conversion was impossible.  */
1697
1698 tree 
1699 perform_qualification_conversions (type, expr)
1700      tree type;
1701      tree expr;
1702 {
1703   if (comp_ptr_ttypes (type, TREE_TYPE(expr)))
1704     return build1 (NOP_EXPR, type, expr);
1705   else
1706     return error_mark_node;
1707 }