OSDN Git Service

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