OSDN Git Service

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