OSDN Git Service

9f29c6586975be28a6e3480818bc6783c0ef3a42
[pf3gnuchains/gcc-fork.git] / gcc / cp / cvt.c
1 /* Language-level data type conversion for GNU C++.
2    Copyright (C) 1987, 88, 92-97, 1998 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 "system.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "cp-tree.h"
33 #include "convert.h"
34 #include "toplev.h"
35 #include "decl.h"
36
37 static tree cp_convert_to_pointer PROTO((tree, tree));
38 static tree convert_to_pointer_force PROTO((tree, tree));
39 static tree build_up_reference PROTO((tree, tree, int));
40
41 /* Change of width--truncation and extension of integers or reals--
42    is represented with NOP_EXPR.  Proper functioning of many things
43    assumes that no other conversions can be NOP_EXPRs.
44
45    Conversion between integer and pointer is represented with CONVERT_EXPR.
46    Converting integer to real uses FLOAT_EXPR
47    and real to integer uses FIX_TRUNC_EXPR.
48
49    Here is a list of all the functions that assume that widening and
50    narrowing is always done with a NOP_EXPR:
51      In convert.c, convert_to_integer.
52      In c-typeck.c, build_binary_op_nodefault (boolean ops),
53         and truthvalue_conversion.
54      In expr.c: expand_expr, for operands of a MULT_EXPR.
55      In fold-const.c: fold.
56      In tree.c: get_narrower and get_unwidened.
57
58    C++: in multiple-inheritance, converting between pointers may involve
59    adjusting them by a delta stored within the class definition.  */
60 \f
61 /* Subroutines of `convert'.  */
62
63 /* if converting pointer to pointer
64      if dealing with classes, check for derived->base or vice versa
65      else if dealing with method pointers, delegate
66      else convert blindly
67    else if converting class, pass off to build_type_conversion
68    else try C-style pointer conversion  */
69
70 static tree
71 cp_convert_to_pointer (type, expr)
72      tree type, expr;
73 {
74   register tree intype = TREE_TYPE (expr);
75   register enum tree_code form;
76   tree rval;
77
78   if (IS_AGGR_TYPE (intype))
79     {
80       intype = complete_type (intype);
81       if (TYPE_SIZE (intype) == NULL_TREE)
82         {
83           cp_error ("can't convert from incomplete type `%T' to `%T'",
84                     intype, type);
85           return error_mark_node;
86         }
87
88       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
89       if (rval)
90         {
91           if (rval == error_mark_node)
92             cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
93                       expr, intype, type);
94           return rval;
95         }
96     }
97
98   /* Handle anachronistic conversions from (::*)() to cv void* or (*)().  */
99   if (TREE_CODE (type) == POINTER_TYPE
100       && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
101           || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node))
102     {
103       /* Allow an implicit this pointer for pointer to member
104          functions.  */
105       if (TYPE_PTRMEMFUNC_P (intype))
106         {
107           tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
108           tree decl = maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype), 0);
109           expr = build (OFFSET_REF, fntype, decl, expr);
110         }
111
112       if (TREE_CODE (expr) == OFFSET_REF
113           && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
114         expr = resolve_offset_ref (expr);
115       if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
116         expr = build_addr_func (expr);
117       if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
118         {
119           if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)
120             if (pedantic || warn_pmf2ptr)
121               cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),
122                           type);
123           return build1 (NOP_EXPR, type, expr);
124         }
125       intype = TREE_TYPE (expr);
126     }
127
128   form = TREE_CODE (intype);
129
130   if (POINTER_TYPE_P (intype))
131     {
132       intype = TYPE_MAIN_VARIANT (intype);
133
134       if (TYPE_MAIN_VARIANT (type) != intype
135           && TREE_CODE (type) == POINTER_TYPE
136           && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
137           && IS_AGGR_TYPE (TREE_TYPE (type))
138           && IS_AGGR_TYPE (TREE_TYPE (intype))
139           && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
140           /* If EXPR is NULL, then we don't need to do any arithmetic
141              to convert it:
142
143                [conv.ptr]
144
145                The null pointer value is converted to the null pointer
146                value of the destination type.  */
147           && !integer_zerop (expr))
148         {
149           enum tree_code code = PLUS_EXPR;
150           tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
151           if (binfo == error_mark_node)
152             return error_mark_node;
153           if (binfo == NULL_TREE)
154             {
155               binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
156               if (binfo == error_mark_node)
157                 return error_mark_node;
158               code = MINUS_EXPR;
159             }
160           if (binfo)
161             {
162               if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
163                   || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
164                   || ! BINFO_OFFSET_ZEROP (binfo))
165                 {
166                   /* Need to get the path we took.  */
167                   tree path;
168
169                   if (code == PLUS_EXPR)
170                     get_base_distance (TREE_TYPE (type), TREE_TYPE (intype),
171                                        0, &path);
172                   else
173                     get_base_distance (TREE_TYPE (intype), TREE_TYPE (type),
174                                        0, &path);
175                   return build_vbase_path (code, type, expr, path, 0);
176                 }
177             }
178         }
179
180       if (TREE_CODE (type) == POINTER_TYPE
181           && TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
182           && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
183         {
184           tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
185           tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
186           tree binfo = get_binfo (b2, b1, 1);
187           enum tree_code code = PLUS_EXPR;
188
189           if (binfo == NULL_TREE)
190             {
191               binfo = get_binfo (b1, b2, 1);
192               code = MINUS_EXPR;
193             }
194
195           if (binfo == error_mark_node)
196             return error_mark_node;
197           if (binfo && ! TREE_VIA_VIRTUAL (binfo))
198             expr = size_binop (code, expr, BINFO_OFFSET (binfo));
199         }
200       else if (TYPE_PTRMEMFUNC_P (type))
201         {
202           cp_error ("cannot convert `%E' from type `%T' to type `%T'",
203                     expr, intype, type);
204           return error_mark_node;
205         }
206
207       rval = build1 (NOP_EXPR, type, expr);
208       TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
209       return rval;
210     }
211   else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
212     return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 1);
213   else if (TYPE_PTRMEMFUNC_P (intype))
214     {
215       cp_error ("cannot convert `%E' from type `%T' to type `%T'",
216                 expr, intype, type);
217       return error_mark_node;
218     }
219
220   my_friendly_assert (form != OFFSET_TYPE, 186);
221
222   if (TYPE_LANG_SPECIFIC (intype)
223       && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
224     return convert_to_pointer (type, build_optr_ref (expr));
225
226   if (integer_zerop (expr))
227     {
228       if (TYPE_PTRMEMFUNC_P (type))
229         return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0);
230       expr = build_int_2 (0, 0);
231       TREE_TYPE (expr) = type;
232       return expr;
233     }
234
235   if (INTEGRAL_CODE_P (form))
236     {
237       if (TYPE_PRECISION (intype) == POINTER_SIZE)
238         return build1 (CONVERT_EXPR, type, expr);
239       expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr);
240       /* Modes may be different but sizes should be the same.  */
241       if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
242           != GET_MODE_SIZE (TYPE_MODE (type)))
243         /* There is supposed to be some integral type
244            that is the same width as a pointer.  */
245         abort ();
246       return convert_to_pointer (type, expr);
247     }
248
249   if (type_unknown_p (expr))
250     return instantiate_type (type, expr, 1);
251
252   cp_error ("cannot convert `%E' from type `%T' to type `%T'",
253             expr, intype, type);
254   return error_mark_node;
255 }
256
257 /* Like convert, except permit conversions to take place which
258    are not normally allowed due to access restrictions
259    (such as conversion from sub-type to private super-type).  */
260
261 static tree
262 convert_to_pointer_force (type, expr)
263      tree type, expr;
264 {
265   register tree intype = TREE_TYPE (expr);
266   register enum tree_code form = TREE_CODE (intype);
267   
268   if (integer_zerop (expr))
269     {
270       expr = build_int_2 (0, 0);
271       TREE_TYPE (expr) = type;
272       return expr;
273     }
274
275   /* Convert signature pointer/reference to `void *' first.  */
276   if (form == RECORD_TYPE
277       && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
278     {
279       expr = build_optr_ref (expr);
280       intype = TREE_TYPE (expr);
281       form = TREE_CODE (intype);
282     }
283
284   if (form == POINTER_TYPE)
285     {
286       intype = TYPE_MAIN_VARIANT (intype);
287
288       if (TYPE_MAIN_VARIANT (type) != intype
289           && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
290           && IS_AGGR_TYPE (TREE_TYPE (type))
291           && IS_AGGR_TYPE (TREE_TYPE (intype))
292           && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
293         {
294           enum tree_code code = PLUS_EXPR;
295           tree path;
296           int distance = get_base_distance (TREE_TYPE (type),
297                                             TREE_TYPE (intype), 0, &path);
298           if (distance == -2)
299             {
300             ambig:
301               cp_error ("type `%T' is ambiguous baseclass of `%s'",
302                         TREE_TYPE (type),
303                         TYPE_NAME_STRING (TREE_TYPE (intype)));
304               return error_mark_node;
305             }
306           if (distance == -1)
307             {
308               distance = get_base_distance (TREE_TYPE (intype),
309                                             TREE_TYPE (type), 0, &path);
310               if (distance == -2)
311                 goto ambig;
312               if (distance < 0)
313                 /* Doesn't need any special help from us.  */
314                 return build1 (NOP_EXPR, type, expr);
315
316               code = MINUS_EXPR;
317             }
318           return build_vbase_path (code, type, expr, path, 0);
319         }
320     }
321
322   return cp_convert_to_pointer (type, expr);
323 }
324
325 /* We are passing something to a function which requires a reference.
326    The type we are interested in is in TYPE. The initial
327    value we have to begin with is in ARG.
328
329    FLAGS controls how we manage access checking.
330    DIRECT_BIND in FLAGS controls how any temporaries are generated.  */
331
332 static tree
333 build_up_reference (type, arg, flags)
334      tree type, arg;
335      int flags;
336 {
337   tree rval;
338   tree argtype = TREE_TYPE (arg);
339   tree target_type = TREE_TYPE (type);
340
341   my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
342
343   if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
344     {
345       tree targ = arg;
346       if (toplevel_bindings_p ())
347         arg = get_temp_name (argtype, 1);
348       else
349         {
350           arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
351           DECL_ARTIFICIAL (arg) = 1;
352         }
353       DECL_INITIAL (arg) = targ;
354       cp_finish_decl (arg, targ, NULL_TREE, 0,
355                       LOOKUP_ONLYCONVERTING|DIRECT_BIND);
356     }
357   else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
358     {
359       tree slot = build_decl (VAR_DECL, NULL_TREE, argtype);
360       DECL_ARTIFICIAL (slot) = 1;
361       arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
362       TREE_SIDE_EFFECTS (arg) = 1;
363     }
364
365   /* If we had a way to wrap this up, and say, if we ever needed it's
366      address, transform all occurrences of the register, into a memory
367      reference we could win better.  */
368   rval = build_unary_op (ADDR_EXPR, arg, 1);
369   if (rval == error_mark_node)
370     return error_mark_node;
371
372   if ((flags & LOOKUP_PROTECT)
373       && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
374       && IS_AGGR_TYPE (argtype)
375       && IS_AGGR_TYPE (target_type))
376     {
377       /* We go through get_binfo for the access control.  */
378       tree binfo = get_binfo (target_type, argtype, 1);
379       if (binfo == error_mark_node)
380         return error_mark_node;
381       if (binfo == NULL_TREE)
382         return error_not_base_type (target_type, argtype);
383       rval = convert_pointer_to_real (binfo, rval);
384     }
385   else
386     rval
387       = convert_to_pointer_force (build_pointer_type (target_type), rval);
388   rval = build1 (NOP_EXPR, type, rval);
389   TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
390   return rval;
391 }
392
393 /* For C++: Only need to do one-level references, but cannot
394    get tripped up on signed/unsigned differences.
395
396    DECL is either NULL_TREE or the _DECL node for a reference that is being
397    initialized.  It can be error_mark_node if we don't know the _DECL but
398    we know it's an initialization.  */
399
400 tree
401 convert_to_reference (reftype, expr, convtype, flags, decl)
402      tree reftype, expr;
403      int convtype, flags;
404      tree decl;
405 {
406   register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
407   register tree intype = TREE_TYPE (expr);
408   tree rval = NULL_TREE;
409   tree rval_as_conversion = NULL_TREE;
410   int i;
411
412   if (TREE_CODE (type) == FUNCTION_TYPE && intype == unknown_type_node)
413     {
414       expr = instantiate_type (type, expr, 
415                                (flags & LOOKUP_COMPLAIN) != 0);
416       if (expr == error_mark_node)
417         return error_mark_node;
418
419       intype = TREE_TYPE (expr);
420     }
421
422   if (TREE_CODE (intype) == REFERENCE_TYPE)
423     my_friendly_abort (364);
424
425   intype = TYPE_MAIN_VARIANT (intype);
426
427   i = comp_target_types (type, intype, 0);
428
429   if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
430       && ! (flags & LOOKUP_NO_CONVERSION))
431     {
432       /* Look for a user-defined conversion to lvalue that we can use.  */
433
434       rval_as_conversion
435         = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
436
437       if (rval_as_conversion && rval_as_conversion != error_mark_node
438           && real_lvalue_p (rval_as_conversion))
439         {
440           expr = rval_as_conversion;
441           rval_as_conversion = NULL_TREE;
442           intype = type;
443           i = 1;
444         }
445     }
446
447   if (((convtype & CONV_STATIC) && i == -1)
448       || ((convtype & CONV_IMPLICIT) && i == 1))
449     {
450       if (flags & LOOKUP_COMPLAIN)
451         {
452           tree ttl = TREE_TYPE (reftype);
453           tree ttr = lvalue_type (expr);
454
455           /* [dcl.init.ref] says that if an rvalue is used to
456              initialize a reference, then the reference must be to a
457              non-volatile const type.  */
458           if (! real_lvalue_p (expr)
459               && !CP_TYPE_CONST_NON_VOLATILE_P (ttl))
460             {
461               char* msg;
462
463               if (CP_TYPE_VOLATILE_P (ttl) && decl)
464                 msg = "initialization of volatile reference type `%#T'";
465               else if (CP_TYPE_VOLATILE_P (ttl))
466                 msg = "conversion to volatile reference type `%#T'";
467               else if (decl)
468                 msg = "initialization of non-const reference type `%#T'";
469               else
470                 msg = "conversion to non-const reference type `%#T'";
471
472               cp_pedwarn (msg, reftype);
473               cp_pedwarn ("from rvalue of type `%T'", intype);
474             }
475           else if (! (convtype & CONV_CONST)
476                    && !at_least_as_qualified_p (ttl, ttr))
477             cp_pedwarn ("conversion from `%T' to `%T' discards qualifiers",
478                         ttr, reftype);
479         }
480
481       return build_up_reference (reftype, expr, flags);
482     }
483   else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
484     {
485       /* When casting an lvalue to a reference type, just convert into
486          a pointer to the new type and deference it.  This is allowed
487          by San Diego WP section 5.2.9 paragraph 12, though perhaps it
488          should be done directly (jason).  (int &)ri ---> *(int*)&ri */
489
490       /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
491          meant.  */
492       if (TREE_CODE (intype) == POINTER_TYPE
493           && (comptypes (TREE_TYPE (intype), type, 
494                          COMPARE_BASE | COMPARE_RELAXED )))
495         cp_warning ("casting `%T' to `%T' does not dereference pointer",
496                     intype, reftype);
497           
498       rval = build_unary_op (ADDR_EXPR, expr, 0);
499       if (rval != error_mark_node)
500         rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
501                               rval, 0);
502       if (rval != error_mark_node)
503         rval = build1 (NOP_EXPR, reftype, rval);
504     }
505   else
506     {
507       rval = convert_for_initialization (NULL_TREE, type, expr, flags,
508                                          "converting", 0, 0);
509       if (rval == NULL_TREE || rval == error_mark_node)
510         return rval;
511       rval = build_up_reference (reftype, rval, flags);
512
513       if (rval && ! CP_TYPE_CONST_P (TREE_TYPE (reftype)))
514         cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
515                     reftype, intype);
516     }
517
518   if (rval)
519     {
520       /* If we found a way to convert earlier, then use it.  */
521       return rval;
522     }
523
524   my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
525
526   if (flags & LOOKUP_COMPLAIN)
527     cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
528
529   if (flags & LOOKUP_SPECULATIVELY)
530     return NULL_TREE;
531
532   return error_mark_node;
533 }
534
535 /* We are using a reference VAL for its value. Bash that reference all the
536    way down to its lowest form.  */
537
538 tree
539 convert_from_reference (val)
540      tree val;
541 {
542   tree type = TREE_TYPE (val);
543
544   if (TREE_CODE (type) == OFFSET_TYPE)
545     type = TREE_TYPE (type);
546   if (TREE_CODE (type) == REFERENCE_TYPE)
547     return build_indirect_ref (val, NULL_PTR);
548   return val;
549 }
550 \f
551 /* Call this when we know (for any reason) that expr is not, in fact,
552    zero.  This routine is like convert_pointer_to, but it pays
553    attention to which specific instance of what type we want to
554    convert to.  This routine should eventually become
555    convert_to_pointer after all references to convert_to_pointer
556    are removed.  */
557
558 tree
559 convert_pointer_to_real (binfo, expr)
560      tree binfo, expr;
561 {
562   register tree intype = TREE_TYPE (expr);
563   tree ptr_type;
564   tree type, rval;
565
566   if (intype == error_mark_node)
567     return error_mark_node;
568
569   if (TREE_CODE (binfo) == TREE_VEC)
570     type = BINFO_TYPE (binfo);
571   else if (IS_AGGR_TYPE (binfo))
572     {
573       type = binfo;
574     }
575   else
576     {
577       type = binfo;
578       binfo = NULL_TREE;
579     }
580
581   ptr_type = cp_build_qualified_type (type,
582                                       CP_TYPE_QUALS (TREE_TYPE (intype)));
583   ptr_type = build_pointer_type (ptr_type);
584   if (ptr_type == TYPE_MAIN_VARIANT (intype))
585     return expr;
586
587   my_friendly_assert (!integer_zerop (expr), 191);
588
589   intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
590   if (TREE_CODE (type) == RECORD_TYPE
591       && TREE_CODE (intype) == RECORD_TYPE
592       && type != intype)
593     {
594       tree path;
595       int distance
596         = get_base_distance (binfo, intype, 0, &path);
597
598       /* This function shouldn't be called with unqualified arguments
599          but if it is, give them an error message that they can read.  */
600       if (distance < 0)
601         {
602           cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
603                     intype, type);
604
605           if (distance == -2)
606             cp_error ("because `%T' is an ambiguous base class", type);
607           return error_mark_node;
608         }
609
610       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
611     }
612   rval = build1 (NOP_EXPR, ptr_type,
613                  TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
614   TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
615   return rval;
616 }
617
618 /* Call this when we know (for any reason) that expr is
619    not, in fact, zero.  This routine gets a type out of the first
620    argument and uses it to search for the type to convert to.  If there
621    is more than one instance of that type in the expr, the conversion is
622    ambiguous.  This routine should eventually go away, and all
623    callers should use convert_to_pointer_real.  */
624
625 tree
626 convert_pointer_to (binfo, expr)
627      tree binfo, expr;
628 {
629   tree type;
630
631   if (TREE_CODE (binfo) == TREE_VEC)
632     type = BINFO_TYPE (binfo);
633   else if (IS_AGGR_TYPE (binfo))
634       type = binfo;
635   else
636       type = binfo;
637   return convert_pointer_to_real (type, expr);
638 }
639 \f
640 /* C++ conversions, preference to static cast conversions.  */
641
642 tree
643 cp_convert (type, expr)
644      tree type, expr;
645 {
646   return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
647 }
648
649 /* Conversion...
650
651    FLAGS indicates how we should behave.  */
652
653 tree
654 ocp_convert (type, expr, convtype, flags)
655      tree type, expr;
656      int convtype, flags;
657 {
658   register tree e = expr;
659   register enum tree_code code = TREE_CODE (type);
660
661   if (e == error_mark_node
662       || TREE_TYPE (e) == error_mark_node)
663     return error_mark_node;
664
665   if (TREE_READONLY_DECL_P (e))
666     e = decl_constant_value (e);
667
668   if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
669       /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
670          don't go through finish_struct, so they don't have the synthesized
671          constructors.  So don't force a temporary.  */
672       && TYPE_HAS_CONSTRUCTOR (type))
673     /* We need a new temporary; don't take this shortcut.  */;
674   else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
675     {
676       if (same_type_p (type, TREE_TYPE (e)))
677         /* The call to fold will not always remove the NOP_EXPR as
678            might be expected, since if one of the types is a typedef;
679            the comparsion in fold is just equality of pointers, not a
680            call to comptypes.  We don't call fold in this case because
681            that can result in infinite recursion; fold will call
682            convert, which will call ocp_convert, etc.  */
683         return e;
684       else
685         return fold (build1 (NOP_EXPR, type, e));
686     }
687
688   if (code == VOID_TYPE && (convtype & CONV_STATIC))
689     {
690       e = require_complete_type_in_void (e);
691       if (e != error_mark_node)
692         e = build1 (CONVERT_EXPR, void_type_node, e);
693
694       return e;
695     }
696
697 #if 0
698   /* This is incorrect.  A truncation can't be stripped this way.
699      Extensions will be stripped by the use of get_unwidened.  */
700   if (TREE_CODE (e) == NOP_EXPR)
701     return cp_convert (type, TREE_OPERAND (e, 0));
702 #endif
703
704   /* Just convert to the type of the member.  */
705   if (code == OFFSET_TYPE)
706     {
707       type = TREE_TYPE (type);
708       code = TREE_CODE (type);
709     }
710
711 #if 0
712   if (code == REFERENCE_TYPE)
713     return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
714   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
715     e = convert_from_reference (e);
716 #endif
717
718   if (TREE_CODE (e) == OFFSET_REF)
719     e = resolve_offset_ref (e);
720
721   if (INTEGRAL_CODE_P (code))
722     {
723       tree intype = TREE_TYPE (e);
724       /* enum = enum, enum = int, enum = float, (enum)pointer are all
725          errors.  */
726       if (TREE_CODE (type) == ENUMERAL_TYPE
727           && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
728               || (TREE_CODE (intype) == POINTER_TYPE)))
729         {
730           cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
731
732           if (flag_pedantic_errors)
733             return error_mark_node;
734         }
735       if (IS_AGGR_TYPE (intype))
736         {
737           tree rval;
738           rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
739           if (rval)
740             return rval;
741           if (flags & LOOKUP_COMPLAIN)
742             cp_error ("`%#T' used where a `%T' was expected", intype, type);
743           if (flags & LOOKUP_SPECULATIVELY)
744             return NULL_TREE;
745           return error_mark_node;
746         }
747       if (code == BOOLEAN_TYPE)
748         {
749           /* Common Ada/Pascal programmer's mistake.  We always warn
750              about this since it is so bad.  */
751           if (TREE_CODE (expr) == FUNCTION_DECL)
752             cp_warning ("the address of `%D', will always be `true'", expr);
753           return truthvalue_conversion (e);
754         }
755       return fold (convert_to_integer (type, e));
756     }
757   if (code == POINTER_TYPE || code == REFERENCE_TYPE
758       || TYPE_PTRMEMFUNC_P (type))
759     return fold (cp_convert_to_pointer (type, e));
760   if (code == REAL_TYPE || code == COMPLEX_TYPE)
761     {
762       if (IS_AGGR_TYPE (TREE_TYPE (e)))
763         {
764           tree rval;
765           rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
766           if (rval)
767             return rval;
768           else
769             if (flags & LOOKUP_COMPLAIN)
770               cp_error ("`%#T' used where a floating point value was expected",
771                         TREE_TYPE (e));
772         }
773       if (code == REAL_TYPE)
774         return fold (convert_to_real (type, e));
775       else if (code == COMPLEX_TYPE)
776         return fold (convert_to_complex (type, e));
777     }
778
779   /* New C++ semantics:  since assignment is now based on
780      memberwise copying,  if the rhs type is derived from the
781      lhs type, then we may still do a conversion.  */
782   if (IS_AGGR_TYPE_CODE (code))
783     {
784       tree dtype = TREE_TYPE (e);
785       tree ctor = NULL_TREE;
786
787       dtype = TYPE_MAIN_VARIANT (dtype);
788
789       /* Conversion of object pointers or signature pointers/references
790          to signature pointers/references.  */
791
792       if (TYPE_LANG_SPECIFIC (type)
793           && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
794         {
795           tree constructor = build_signature_pointer_constructor (type, expr);
796           tree sig_ty = SIGNATURE_TYPE (type);
797           tree sig_ptr;
798
799           if (constructor == error_mark_node)
800             return error_mark_node;
801
802           sig_ptr = get_temp_name (type, 1);
803           DECL_INITIAL (sig_ptr) = constructor;
804           CLEAR_SIGNATURE (sig_ty);
805           cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
806           SET_SIGNATURE (sig_ty);
807           TREE_READONLY (sig_ptr) = 1;
808
809           return sig_ptr;
810         }
811
812       /* Conversion between aggregate types.  New C++ semantics allow
813          objects of derived type to be cast to objects of base type.
814          Old semantics only allowed this between pointers.
815
816          There may be some ambiguity between using a constructor
817          vs. using a type conversion operator when both apply.  */
818
819       ctor = e;
820
821       if (IS_AGGR_TYPE (type) && CLASSTYPE_ABSTRACT_VIRTUALS (type))
822         {
823           abstract_virtuals_error (NULL_TREE, type);
824           return error_mark_node;
825         }
826
827       if ((flags & LOOKUP_ONLYCONVERTING)
828           && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
829         /* For copy-initialization, first we create a temp of the proper type
830            with a user-defined conversion sequence, then we direct-initialize
831            the target with the temp (see [dcl.init]).  */
832         ctor = build_user_type_conversion (type, ctor, flags);
833       if (ctor)
834         ctor = build_method_call (NULL_TREE, ctor_identifier,
835                                   build_expr_list (NULL_TREE, ctor),
836                                   TYPE_BINFO (type), flags);
837       if (ctor)
838         return build_cplus_new (type, ctor);
839     }
840
841   /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
842      then it won't be hashed and hence compare as not equal,
843      even when it is.  */
844   if (code == ARRAY_TYPE
845       && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
846       && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
847     return e;
848
849   if (flags & LOOKUP_COMPLAIN)
850     cp_error ("conversion from `%T' to non-scalar type `%T' requested",
851               TREE_TYPE (expr), type);
852   if (flags & LOOKUP_SPECULATIVELY)
853     return NULL_TREE;
854   return error_mark_node;
855 }
856
857 /* Create an expression whose value is that of EXPR,
858    converted to type TYPE.  The TREE_TYPE of the value
859    is always TYPE.  This function implements all reasonable
860    conversions; callers should filter out those that are
861    not permitted by the language being compiled.
862
863    Most of this routine is from build_reinterpret_cast.
864
865    The backend cannot call cp_convert (what was convert) because
866    conversions to/from basetypes may involve memory references
867    (vbases) and adding or subtracting small values (multiple
868    inheritance), but it calls convert from the constant folding code
869    on subtrees of already build trees after it has ripped them apart.
870
871    Also, if we ever support range variables, we'll probably also have to
872    do a little bit more work.  */
873
874 tree
875 convert (type, expr)
876      tree type, expr;
877 {
878   tree intype;
879
880   if (type == error_mark_node || expr == error_mark_node)
881     return error_mark_node;
882
883   intype = TREE_TYPE (expr);
884
885   if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
886     {
887       if (TREE_READONLY_DECL_P (expr))
888         expr = decl_constant_value (expr);
889       return fold (build1 (NOP_EXPR, type, expr));
890     }
891
892   return ocp_convert (type, expr, CONV_OLD_CONVERT,
893                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
894 }
895
896 /* Like cp_convert, except permit conversions to take place which
897    are not normally allowed due to access restrictions
898    (such as conversion from sub-type to private super-type).  */
899
900 tree
901 convert_force (type, expr, convtype)
902      tree type;
903      tree expr;
904      int convtype;
905 {
906   register tree e = expr;
907   register enum tree_code code = TREE_CODE (type);
908
909   if (code == REFERENCE_TYPE)
910     return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
911                                        NULL_TREE));
912   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
913     e = convert_from_reference (e);
914
915   if (code == POINTER_TYPE)
916     return fold (convert_to_pointer_force (type, e));
917
918   /* From typeck.c convert_for_assignment */
919   if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
920         && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
921         && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
922        || integer_zerop (e)
923        || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
924       && TYPE_PTRMEMFUNC_P (type))
925     {
926       /* compatible pointer to member functions.  */
927       return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
928     }
929
930   return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
931 }
932
933 /* Convert an aggregate EXPR to type XTYPE.  If a conversion
934    exists, return the attempted conversion.  This may
935    return ERROR_MARK_NODE if the conversion is not
936    allowed (references private members, etc).
937    If no conversion exists, NULL_TREE is returned.
938
939    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
940    to take place immediately.  Otherwise, we build a SAVE_EXPR
941    which can be evaluated if the results are ever needed.
942
943    Changes to this functions should be mirrored in user_harshness.
944
945    FIXME: Ambiguity checking is wrong.  Should choose one by the implicit
946    object parameter, or by the second standard conversion sequence if
947    that doesn't do it.  This will probably wait for an overloading rewrite.
948    (jason 8/9/95)  */
949
950 tree
951 build_type_conversion (code, xtype, expr, for_sure)
952      enum tree_code code ATTRIBUTE_UNUSED;
953      tree xtype, expr;
954      int for_sure;
955 {
956   /* C++: check to see if we can convert this aggregate type
957      into the required type.  */
958   return build_user_type_conversion
959     (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
960 }
961
962 /* Convert the given EXPR to one of a group of types suitable for use in an
963    expression.  DESIRES is a combination of various WANT_* flags (q.v.)
964    which indicates which types are suitable.  If COMPLAIN is 1, complain
965    about ambiguity; otherwise, the caller will deal with it.  */
966
967 tree
968 build_expr_type_conversion (desires, expr, complain)
969      int desires;
970      tree expr;
971      int complain;
972 {
973   tree basetype = TREE_TYPE (expr);
974   tree conv = NULL_TREE;
975   tree winner = NULL_TREE;
976
977   if (expr == null_node 
978       && (desires & WANT_INT) 
979       && !(desires & WANT_NULL))
980     cp_warning ("converting NULL to non-pointer type");
981     
982   if (TREE_CODE (basetype) == OFFSET_TYPE)
983     expr = resolve_offset_ref (expr);
984   expr = convert_from_reference (expr);
985   basetype = TREE_TYPE (expr);
986
987   if (! IS_AGGR_TYPE (basetype))
988     switch (TREE_CODE (basetype))
989       {
990       case INTEGER_TYPE:
991         if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
992           return expr;
993         /* else fall through...  */
994
995       case BOOLEAN_TYPE:
996         return (desires & WANT_INT) ? expr : NULL_TREE;
997       case ENUMERAL_TYPE:
998         return (desires & WANT_ENUM) ? expr : NULL_TREE;
999       case REAL_TYPE:
1000         return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1001       case POINTER_TYPE:
1002         return (desires & WANT_POINTER) ? expr : NULL_TREE;
1003         
1004       case FUNCTION_TYPE:
1005       case ARRAY_TYPE:
1006         return (desires & WANT_POINTER) ? default_conversion (expr)
1007                                         : NULL_TREE;
1008       default:
1009         return NULL_TREE;
1010       }
1011
1012   /* The code for conversions from class type is currently only used for
1013      delete expressions.  Other expressions are handled by build_new_op.  */
1014
1015   if (! TYPE_HAS_CONVERSION (basetype))
1016     return NULL_TREE;
1017
1018   for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1019     {
1020       int win = 0;
1021       tree candidate;
1022       tree cand = TREE_VALUE (conv);
1023
1024       if (winner && winner == cand)
1025         continue;
1026
1027       candidate = TREE_TYPE (TREE_TYPE (cand));
1028       if (TREE_CODE (candidate) == REFERENCE_TYPE)
1029         candidate = TREE_TYPE (candidate);
1030
1031       switch (TREE_CODE (candidate))
1032         {
1033         case BOOLEAN_TYPE:
1034         case INTEGER_TYPE:
1035           win = (desires & WANT_INT); break;
1036         case ENUMERAL_TYPE:
1037           win = (desires & WANT_ENUM); break;
1038         case REAL_TYPE:
1039           win = (desires & WANT_FLOAT); break;
1040         case POINTER_TYPE:
1041           win = (desires & WANT_POINTER); break;
1042
1043         default:
1044           break;
1045         }
1046
1047       if (win)
1048         {
1049           if (winner)
1050             {
1051               if (complain)
1052                 {
1053                   cp_error ("ambiguous default type conversion from `%T'",
1054                             basetype);
1055                   cp_error ("  candidate conversions include `%D' and `%D'",
1056                             winner, cand);
1057                 }
1058               return error_mark_node;
1059             }
1060           else
1061             winner = cand;
1062         }
1063     }
1064
1065   if (winner)
1066     {
1067       tree type = TREE_TYPE (TREE_TYPE (winner));
1068       if (TREE_CODE (type) == REFERENCE_TYPE)
1069         type = TREE_TYPE (type);
1070       return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1071     }
1072
1073   return NULL_TREE;
1074 }
1075
1076 /* Implements integral promotion (4.1) and float->double promotion.  */
1077
1078 tree
1079 type_promotes_to (type)
1080      tree type;
1081 {
1082   int type_quals;
1083
1084   if (type == error_mark_node)
1085     return error_mark_node;
1086
1087   type_quals = CP_TYPE_QUALS (type);
1088   type = TYPE_MAIN_VARIANT (type);
1089
1090   /* bool always promotes to int (not unsigned), even if it's the same
1091      size.  */
1092   if (type == boolean_type_node)
1093     type = integer_type_node;
1094
1095   /* Normally convert enums to int, but convert wide enums to something
1096      wider.  */
1097   else if (TREE_CODE (type) == ENUMERAL_TYPE
1098            || type == wchar_type_node)
1099     {
1100       int precision = MAX (TYPE_PRECISION (type),
1101                            TYPE_PRECISION (integer_type_node));
1102       tree totype = type_for_size (precision, 0);
1103       if (TREE_UNSIGNED (type)
1104           && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1105         type = type_for_size (precision, 1);
1106       else
1107         type = totype;
1108     }
1109   else if (C_PROMOTING_INTEGER_TYPE_P (type))
1110     {
1111       /* Retain unsignedness if really not getting bigger.  */
1112       if (TREE_UNSIGNED (type)
1113           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1114         type = unsigned_type_node;
1115       else
1116         type = integer_type_node;
1117     }
1118   else if (type == float_type_node)
1119     type = double_type_node;
1120
1121   return cp_build_qualified_type (type, type_quals);
1122 }
1123
1124 /* The routines below this point are carefully written to conform to
1125    the standard.  They use the same terminology, and follow the rules
1126    closely.  Although they are used only in pt.c at the moment, they
1127    should presumably be used everywhere in the future.  */
1128
1129 /* Attempt to perform qualification conversions on EXPR to convert it
1130    to TYPE.  Return the resulting expression, or error_mark_node if
1131    the conversion was impossible.  */
1132
1133 tree 
1134 perform_qualification_conversions (type, expr)
1135      tree type;
1136      tree expr;
1137 {
1138   if (TREE_CODE (type) == POINTER_TYPE
1139       && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
1140       && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (expr))))
1141     return build1 (NOP_EXPR, type, expr);
1142   else
1143     return error_mark_node;
1144 }