OSDN Git Service

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