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