OSDN Git Service

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