OSDN Git Service

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