OSDN Git Service

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