OSDN Git Service

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