OSDN Git Service

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