OSDN Git Service

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