OSDN Git Service

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