OSDN Git Service

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