OSDN Git Service

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