OSDN Git Service

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