OSDN Git Service

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