OSDN Git Service

90th Cygnus<->FSF quick merge
[pf3gnuchains/gcc-fork.git] / gcc / cp / cvt.c
1 /* Language-level data type conversion for GNU C++.
2    Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 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 "tree.h"
30 #include "flags.h"
31 #include "cp-tree.h"
32 #include "class.h"
33 #include "convert.h"
34
35 extern tree static_aggregates;
36
37 /* Change of width--truncation and extension of integers or reals--
38    is represented with NOP_EXPR.  Proper functioning of many things
39    assumes that no other conversions can be NOP_EXPRs.
40
41    Conversion between integer and pointer is represented with CONVERT_EXPR.
42    Converting integer to real uses FLOAT_EXPR
43    and real to integer uses FIX_TRUNC_EXPR.
44
45    Here is a list of all the functions that assume that widening and
46    narrowing is always done with a NOP_EXPR:
47      In convert.c, convert_to_integer.
48      In c-typeck.c, build_binary_op_nodefault (boolean ops),
49         and truthvalue_conversion.
50      In expr.c: expand_expr, for operands of a MULT_EXPR.
51      In fold-const.c: fold.
52      In tree.c: get_narrower and get_unwidened.
53
54    C++: in multiple-inheritance, converting between pointers may involve
55    adjusting them by a delta stored within the class definition.  */
56 \f
57 /* Subroutines of `convert'.  */
58
59 /* Build a thunk.  What it is, is an entry point that when called will
60    adjust the this pointer (the first argument) by offset, and then
61    goto the real address of the function given by REAL_ADDR that we
62    would like called.  What we return is the address of the thunk.  */
63
64 static tree
65 build_thunk (offset, real_addr)
66      tree offset, real_addr;
67 {
68   if (TREE_CODE (real_addr) != ADDR_EXPR
69       || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
70     {
71       sorry ("MI pointer to member conversion too complex");
72       return error_mark_node;
73     }
74   sorry ("MI pointer to member conversion too complex");
75   return error_mark_node;
76 }
77
78 /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
79    another `pointer to method'.  This may involved the creation of
80    a thunk to handle the this offset calculation.  */
81
82 static tree
83 convert_fn_ptr (type, expr)
84      tree type, expr;
85 {
86 #if 0                           /* We don't use thunks for pmfs.  */
87   if (flag_vtable_thunks)
88     {
89       tree intype = TREE_TYPE (expr);
90       tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype)),
91                               TYPE_METHOD_BASETYPE (TREE_TYPE (type)), 1);
92       if (binfo == error_mark_node)
93         {
94           error ("  in pointer to member conversion");
95           return error_mark_node;
96         }
97       if (binfo == NULL_TREE)
98         {
99           /* ARM 4.8 restriction.  */
100           error ("invalid pointer to member conversion");
101           return error_mark_node;
102         }
103
104       if (BINFO_OFFSET_ZEROP (binfo))
105         return build1 (NOP_EXPR, type, expr);
106       return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
107     }
108   else
109 #endif
110     return build_ptrmemfunc (type, expr, 1);
111 }
112
113 /* if converting pointer to pointer
114      if dealing with classes, check for derived->base or vice versa
115      else if dealing with method pointers, delegate
116      else convert blindly
117    else if converting class, pass off to build_type_conversion
118    else try C-style pointer conversion  */
119
120 static tree
121 cp_convert_to_pointer (type, expr)
122      tree type, expr;
123 {
124   register tree intype = TREE_TYPE (expr);
125   register enum tree_code form;
126
127   if (IS_AGGR_TYPE (intype))
128     {
129       tree rval;
130
131       intype = complete_type (intype);
132       if (TYPE_SIZE (intype) == NULL_TREE)
133         {
134           cp_error ("can't convert from incomplete type `%T' to `%T'",
135                     intype, type);
136           return error_mark_node;
137         }
138
139       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
140       if (rval)
141         {
142           if (rval == error_mark_node)
143             cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
144                       expr, intype, type);
145           return rval;
146         }
147     }
148
149   if (TYPE_PTRMEMFUNC_P (type))
150     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
151
152   /* Handle anachronistic conversions from (::*)() to cv void* or (*)().  */
153   if (TREE_CODE (type) == POINTER_TYPE
154       && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
155           || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node))
156     {
157       /* Allow an implicit this pointer for pointer to member
158          functions.  */
159       if (TYPE_PTRMEMFUNC_P (intype))
160         {
161           tree decl, basebinfo;
162           tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));
163           tree t = TYPE_METHOD_BASETYPE (fntype);
164
165           if (current_class_type == 0
166               || get_base_distance (t, current_class_type, 0, &basebinfo)
167               == -1)
168             {
169               decl = build1 (NOP_EXPR, t, error_mark_node);
170             }
171           else if (current_class_ptr == 0)
172             decl = build1 (NOP_EXPR, t, error_mark_node);
173           else
174             decl = current_class_ref;
175
176           expr = build (OFFSET_REF, fntype, decl, expr);
177         }
178
179       if (TREE_CODE (expr) == OFFSET_REF
180           && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
181         expr = resolve_offset_ref (expr);
182       if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)
183         expr = build_addr_func (expr);
184       if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
185         {
186           if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)
187             if (pedantic || warn_pmf2ptr)
188               cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),
189                           type);
190           return build1 (NOP_EXPR, type, expr);
191         }
192       intype = TREE_TYPE (expr);
193     }
194
195   if (TYPE_PTRMEMFUNC_P (intype))
196     intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
197
198   form = TREE_CODE (intype);
199
200   if (form == POINTER_TYPE || form == REFERENCE_TYPE)
201     {
202       intype = TYPE_MAIN_VARIANT (intype);
203
204       if (TYPE_MAIN_VARIANT (type) != intype
205           && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
206           && IS_AGGR_TYPE (TREE_TYPE (type))
207           && IS_AGGR_TYPE (TREE_TYPE (intype))
208           && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
209         {
210           enum tree_code code = PLUS_EXPR;
211           tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
212           if (binfo == error_mark_node)
213             return error_mark_node;
214           if (binfo == NULL_TREE)
215             {
216               binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
217               if (binfo == error_mark_node)
218                 return error_mark_node;
219               code = MINUS_EXPR;
220             }
221           if (binfo)
222             {
223               if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
224                   || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
225                   || ! BINFO_OFFSET_ZEROP (binfo))
226                 {
227                   /* Need to get the path we took.  */
228                   tree path;
229
230                   if (code == PLUS_EXPR)
231                     get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
232                   else
233                     get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
234                   return build_vbase_path (code, type, expr, path, 0);
235                 }
236             }
237         }
238       if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
239           && TREE_CODE (type) == POINTER_TYPE
240           && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
241         return convert_fn_ptr (type, expr);
242
243       if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
244           && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
245         {
246           tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
247           tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
248           tree binfo = get_binfo (b1, b2, 1);
249           if (binfo == NULL_TREE)
250             binfo = get_binfo (b2, b1, 1);
251           if (binfo == error_mark_node)
252             return error_mark_node;
253         }
254
255       if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
256           || (TREE_CODE (type) == POINTER_TYPE
257               && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
258         {
259           cp_error ("cannot convert `%E' from type `%T' to type `%T'",
260                     expr, intype, type);
261           return error_mark_node;
262         }
263
264       return build1 (NOP_EXPR, type, expr);
265     }
266
267   my_friendly_assert (form != OFFSET_TYPE, 186);
268
269   if (TYPE_LANG_SPECIFIC (intype)
270       && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
271     return convert_to_pointer (type, build_optr_ref (expr));
272
273   if (integer_zerop (expr))
274     {
275       if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
276         return build_ptrmemfunc (type, expr, 0);
277       expr = build_int_2 (0, 0);
278       TREE_TYPE (expr) = type;
279       return expr;
280     }
281
282   if (INTEGRAL_CODE_P (form))
283     {
284       if (type_precision (intype) == POINTER_SIZE)
285         return build1 (CONVERT_EXPR, type, expr);
286       expr = convert (type_for_size (POINTER_SIZE, 0), expr);
287       /* Modes may be different but sizes should be the same.  */
288       if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
289           != GET_MODE_SIZE (TYPE_MODE (type)))
290         /* There is supposed to be some integral type
291            that is the same width as a pointer.  */
292         abort ();
293       return convert_to_pointer (type, expr);
294     }
295
296   cp_error ("cannot convert `%E' from type `%T' to type `%T'",
297             expr, intype, type);
298   return error_mark_node;
299 }
300
301 /* Like convert, except permit conversions to take place which
302    are not normally allowed due to access restrictions
303    (such as conversion from sub-type to private super-type).  */
304
305 static tree
306 convert_to_pointer_force (type, expr)
307      tree type, expr;
308 {
309   register tree intype = TREE_TYPE (expr);
310   register enum tree_code form = TREE_CODE (intype);
311   
312   if (integer_zerop (expr))
313     {
314       expr = build_int_2 (0, 0);
315       TREE_TYPE (expr) = type;
316       return expr;
317     }
318
319   /* Convert signature pointer/reference to `void *' first.  */
320   if (form == RECORD_TYPE
321       && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
322     {
323       expr = build_optr_ref (expr);
324       intype = TREE_TYPE (expr);
325       form = TREE_CODE (intype);
326     }
327
328   if (form == POINTER_TYPE)
329     {
330       intype = TYPE_MAIN_VARIANT (intype);
331
332       if (TYPE_MAIN_VARIANT (type) != intype
333           && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
334           && IS_AGGR_TYPE (TREE_TYPE (type))
335           && IS_AGGR_TYPE (TREE_TYPE (intype))
336           && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
337         {
338           enum tree_code code = PLUS_EXPR;
339           tree path;
340           int distance = get_base_distance (TREE_TYPE (type),
341                                             TREE_TYPE (intype), 0, &path);
342           if (distance == -2)
343             {
344             ambig:
345               cp_error ("type `%T' is ambiguous baseclass of `%s'",
346                         TREE_TYPE (type),
347                         TYPE_NAME_STRING (TREE_TYPE (intype)));
348               return error_mark_node;
349             }
350           if (distance == -1)
351             {
352               distance = get_base_distance (TREE_TYPE (intype),
353                                             TREE_TYPE (type), 0, &path);
354               if (distance == -2)
355                 goto ambig;
356               if (distance < 0)
357                 /* Doesn't need any special help from us.  */
358                 return build1 (NOP_EXPR, type, expr);
359
360               code = MINUS_EXPR;
361             }
362           return build_vbase_path (code, type, expr, path, 0);
363         }
364     }
365
366   return cp_convert_to_pointer (type, expr);
367 }
368
369 /* We are passing something to a function which requires a reference.
370    The type we are interested in is in TYPE. The initial
371    value we have to begin with is in ARG.
372
373    FLAGS controls how we manage access checking.
374    DIRECT_BIND in FLAGS controls how any temporarys are generated.
375    CHECKCONST controls if we report error messages on const subversion.  */
376
377 static tree
378 build_up_reference (type, arg, flags, checkconst)
379      tree type, arg;
380      int flags, checkconst;
381 {
382   tree rval, targ;
383   int literal_flag = 0;
384   tree argtype = TREE_TYPE (arg);
385   tree target_type = TREE_TYPE (type);
386   tree binfo = NULL_TREE;
387
388   my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
389   if ((flags & LOOKUP_PROTECT)
390       && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
391       && IS_AGGR_TYPE (argtype)
392       && IS_AGGR_TYPE (target_type))
393     {
394       binfo = get_binfo (target_type, argtype, 1);
395       if (binfo == error_mark_node)
396         return error_mark_node;
397       if (binfo == NULL_TREE)
398         return error_not_base_type (target_type, argtype);
399     }
400
401   /* Pass along const and volatile down into the type.  */
402   if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
403     target_type = cp_build_type_variant (target_type, TYPE_READONLY (type),
404                                         TYPE_VOLATILE (type));
405   targ = arg;
406   if (TREE_CODE (targ) == SAVE_EXPR)
407     targ = TREE_OPERAND (targ, 0);
408   while (TREE_CODE (targ) == NOP_EXPR
409          && (TYPE_MAIN_VARIANT (argtype)
410              == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0)))))
411     targ = TREE_OPERAND (targ, 0);
412
413   switch (TREE_CODE (targ))
414     {
415     case INDIRECT_REF:
416       /* This is a call to a constructor which did not know what it was
417          initializing until now: it needs to initialize a temporary.  */
418       if (TREE_HAS_CONSTRUCTOR (targ))
419         {
420           tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0));
421           TREE_HAS_CONSTRUCTOR (targ) = 0;
422           return build_up_reference (type, temp, flags, 1);
423         }
424       /* Let &* cancel out to simplify resulting code.
425          Also, throw away intervening NOP_EXPRs.  */
426       arg = TREE_OPERAND (targ, 0);
427       if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
428           || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
429         arg = TREE_OPERAND (arg, 0);
430
431       /* in doing a &*, we have to get rid of the const'ness on the pointer
432          value.  Haven't thought about volatile here.  Pointers come to mind
433          here.  */
434       if (TREE_READONLY (arg))
435         {
436           arg = copy_node (arg);
437           TREE_READONLY (arg) = 0;
438         }
439
440       rval = build1 (CONVERT_EXPR, type, arg);
441       TREE_REFERENCE_EXPR (rval) = 1;
442
443       /* propagate the const flag on something like:
444
445          class Base {
446          public:
447            int foo;
448          };
449
450       class Derived : public Base {
451       public:
452         int bar;
453       };
454
455       void func(Base&);
456
457       void func2(const Derived& d) {
458         func(d);
459       }
460
461         on the d parameter.  The below could have been avoided, if the flags
462         were down in the tree, not sure why they are not.  (mrs) */
463       /* The below code may have to be propagated to other parts of this
464          switch.  */
465       if (TREE_READONLY (targ) && !TREE_READONLY (arg)
466           && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
467           && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
468           && (TYPE_READONLY (target_type) && checkconst))
469         {
470           arg = copy_node (arg);
471           TREE_READONLY (arg) = TREE_READONLY (targ);
472         }
473       literal_flag = TREE_CONSTANT (arg);
474
475       goto done;
476
477       /* Get this out of a register if we happened to be in one by accident.
478          Also, build up references to non-lvalues it we must.  */
479       /* For &x[y], return (&) x+y */
480     case ARRAY_REF:
481       if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
482         return error_mark_node;
483       rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
484                               TREE_OPERAND (targ, 1), 1);
485       TREE_TYPE (rval) = type;
486       if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
487           && staticp (TREE_OPERAND (targ, 0)))
488         TREE_CONSTANT (rval) = 1;
489       goto done;
490
491     case SCOPE_REF:
492       /* Could be a reference to a static member.  */
493       {
494         tree field = TREE_OPERAND (targ, 1);
495         if (TREE_STATIC (field))
496           {
497             rval = build1 (ADDR_EXPR, type, field);
498             literal_flag = 1;
499             goto done;
500           }
501       }
502
503       /* We should have farmed out member pointers above.  */
504       my_friendly_abort (188);
505
506     case COMPONENT_REF:
507       rval = build_component_addr (targ, build_pointer_type (argtype),
508                                    "attempt to make a reference to bit-field structure member `%s'");
509       TREE_TYPE (rval) = type;
510       literal_flag = staticp (TREE_OPERAND (targ, 0));
511
512       goto done;
513
514       /* Anything not already handled and not a true memory reference
515          needs to have a reference built up.  Do so silently for
516          things like integers and return values from function,
517          but complain if we need a reference to something declared
518          as `register'.  */
519
520     case PARM_DECL:
521       /* 'this' is not an lvalue.  */
522       if (targ == current_class_ptr && ! flag_this_is_variable)
523         break;
524
525     case RESULT_DECL:
526     case VAR_DECL:
527     case CONST_DECL:
528       if (staticp (targ))
529         literal_flag = 1;
530
531       /* Fall through.  */
532     case TARGET_EXPR:
533       mark_addressable (targ);
534       break;
535
536     case COMPOUND_EXPR:
537       {
538         tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
539                                                   flags, checkconst);
540         rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
541         TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
542         return rval;
543       }
544
545     case PREINCREMENT_EXPR:
546     case PREDECREMENT_EXPR:
547     case MODIFY_EXPR:
548     case INIT_EXPR:
549       {
550         tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
551                                                   flags, checkconst);
552         rval = build (COMPOUND_EXPR, type, arg, real_reference);
553         TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
554         return rval;
555       }
556
557     case COND_EXPR:
558       return build (COND_EXPR, type,
559                     TREE_OPERAND (targ, 0),
560                     build_up_reference (type, TREE_OPERAND (targ, 1),
561                                         flags, checkconst),
562                     build_up_reference (type, TREE_OPERAND (targ, 2),
563                                         flags, checkconst));
564
565       /* Undo the folding...  */
566     case MIN_EXPR:
567     case MAX_EXPR:
568       return build (COND_EXPR, type,
569                     build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR,
570                            boolean_type_node, TREE_OPERAND (targ, 0),
571                            TREE_OPERAND (targ, 1)),
572                     build_up_reference (type, TREE_OPERAND (targ, 0),
573                                         flags, checkconst),
574                     build_up_reference (type, TREE_OPERAND (targ, 1),
575                                         flags, checkconst));
576
577     case BIND_EXPR:
578       arg = TREE_OPERAND (targ, 1);
579       if (arg == NULL_TREE)
580         {
581           compiler_error ("({ ... }) expression not expanded when needed for reference");
582           return error_mark_node;
583         }
584       rval = build1 (ADDR_EXPR, type, arg);
585       TREE_REFERENCE_EXPR (rval) = 1;
586       return rval;
587
588     default:
589       break;
590     }
591
592   if ((flags & DIRECT_BIND)
593       && ! real_lvalue_p (targ))
594     {
595       if (toplevel_bindings_p ())
596         {
597           arg = get_temp_name (argtype, 1);
598           literal_flag = 1;
599         }
600       else
601         {
602           arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));
603           DECL_ARTIFICIAL (arg) = 1;
604         }
605       DECL_INITIAL (arg) = targ;
606       cp_finish_decl (arg, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
607     }
608   else if (TREE_ADDRESSABLE (targ) == 0 && !(flags & DIRECT_BIND))
609     {
610       tree slot = build_decl (VAR_DECL, NULL_TREE, argtype);
611       arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
612     }
613
614   /* If we had a way to wrap this up, and say, if we ever needed it's
615      address, transform all occurrences of the register, into a memory
616      reference we could win better.  */
617   mark_addressable (arg);
618   rval = build1 (ADDR_EXPR, type, arg);
619
620  done:
621   if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
622       || TYPE_USES_COMPLEX_INHERITANCE (target_type))
623     {
624       TREE_TYPE (rval) = build_pointer_type (argtype);
625       if (flags & LOOKUP_PROTECT)
626         rval = convert_pointer_to (target_type, rval);
627       else
628         rval
629           = convert_to_pointer_force (build_pointer_type (target_type), rval);
630       TREE_TYPE (rval) = type;
631       if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR)
632         TREE_TYPE (TREE_OPERAND (rval, 0))
633           = TREE_TYPE (TREE_OPERAND (rval, 1)) = type;
634     }
635   TREE_CONSTANT (rval) = literal_flag;
636   return rval;
637 }
638
639 /* For C++: Only need to do one-level references, but cannot
640    get tripped up on signed/unsigned differences.
641
642    DECL is either NULL_TREE or the _DECL node for a reference that is being
643    initialized.  It can be error_mark_node if we don't know the _DECL but
644    we know it's an initialization.  */
645
646 tree
647 convert_to_reference (reftype, expr, convtype, flags, decl)
648      tree reftype, expr;
649      int convtype, flags;
650      tree decl;
651 {
652   register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
653   register tree intype = TREE_TYPE (expr);
654   tree rval = NULL_TREE;
655   tree rval_as_conversion = NULL_TREE;
656   int i;
657
658   if (TREE_CODE (intype) == REFERENCE_TYPE)
659     my_friendly_abort (364);
660
661   intype = TYPE_MAIN_VARIANT (intype);
662
663   i = comp_target_types (type, intype, 0);
664
665   if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
666       && ! (flags & LOOKUP_NO_CONVERSION))
667     {
668       /* Look for a user-defined conversion to lvalue that we can use.  */
669
670       if (flag_ansi_overloading)
671         rval_as_conversion
672           = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
673       else
674         rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
675
676       if (rval_as_conversion && rval_as_conversion != error_mark_node
677           && real_lvalue_p (rval_as_conversion))
678         {
679           expr = rval_as_conversion;
680           rval_as_conversion = NULL_TREE;
681           intype = type;
682           i = 1;
683         }
684     }
685
686   if (((convtype & CONV_STATIC) && i == -1)
687       || ((convtype & CONV_IMPLICIT) && i == 1))
688     {
689       if (flags & LOOKUP_COMPLAIN)
690         {
691           tree ttl = TREE_TYPE (reftype);
692           tree ttr;
693           
694           {
695             int r = TREE_READONLY (expr);
696             int v = TREE_THIS_VOLATILE (expr);
697             ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
698           }
699
700           if (! real_lvalue_p (expr) && ! TYPE_READONLY (ttl))
701             {
702               if (decl)
703                 /* Ensure semantics of [dcl.init.ref] */
704                 cp_pedwarn ("initialization of non-const reference `%#T' from rvalue `%T'",
705                             reftype, intype);
706               else
707                 cp_pedwarn ("conversion to non-const `%T' from rvalue `%T'",
708                             reftype, intype);
709             }
710           else if (! (convtype & CONV_CONST))
711             {
712               if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
713                 cp_pedwarn ("conversion from `%T' to `%T' discards const",
714                             ttr, reftype);
715               else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
716                 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
717                             ttr, reftype);
718             }
719         }
720
721       return build_up_reference (reftype, expr, flags,
722                                  ! (convtype & CONV_CONST));
723     }
724   else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
725     {
726       /* When casting an lvalue to a reference type, just convert into
727          a pointer to the new type and deference it.  This is allowed
728          by San Diego WP section 5.2.9 paragraph 12, though perhaps it
729          should be done directly (jason).  (int &)ri ---> *(int*)&ri */
730
731       /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
732          meant.  */
733       if (TREE_CODE (intype) == POINTER_TYPE
734           && (comptypes (TREE_TYPE (intype), type, -1)))
735         cp_warning ("casting `%T' to `%T' does not dereference pointer",
736                     intype, reftype);
737           
738       rval = build_unary_op (ADDR_EXPR, expr, 0);
739       if (rval != error_mark_node)
740         rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
741       if (rval != error_mark_node)
742         rval = build1 (NOP_EXPR, reftype, rval);
743     }
744   else if (flag_ansi_overloading)
745     {
746       rval = convert_for_initialization (NULL_TREE, type, expr, flags,
747                                          "converting", 0, 0);
748       if (rval == error_mark_node)
749         return error_mark_node;
750       rval = build_up_reference (reftype, rval, flags, 1);
751
752       if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
753         cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
754                     reftype, intype);
755     }
756   else
757     {
758       tree rval_as_ctor = NULL_TREE;
759       
760       if (rval_as_conversion)
761         {
762           if (rval_as_conversion == error_mark_node)
763             {
764               cp_error ("conversion from `%T' to `%T' is ambiguous",
765                         intype, reftype);
766               return error_mark_node;
767             }
768           rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
769                                                    flags, 1);
770         }
771       
772       /* Definitely need to go through a constructor here.  */
773       if (TYPE_HAS_CONSTRUCTOR (type)
774           && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
775           && (rval = build_method_call
776               (NULL_TREE, ctor_identifier,
777                build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
778                LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
779                | LOOKUP_ONLYCONVERTING)))
780         {
781           tree init;
782
783           if (toplevel_bindings_p ())
784             {
785               tree t = get_temp_name (type, toplevel_bindings_p ());
786               init = build_method_call (t, ctor_identifier,
787                                         build_tree_list (NULL_TREE, expr),
788                                         TYPE_BINFO (type),
789                                         LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
790                                         | LOOKUP_ONLYCONVERTING);
791
792               if (init == error_mark_node)
793                 return error_mark_node;
794
795               make_decl_rtl (t, NULL_PTR, 1);
796               static_aggregates = perm_tree_cons (expr, t, static_aggregates);
797               rval = build_unary_op (ADDR_EXPR, t, 0);
798             }
799           else
800             {
801               init = build_method_call (NULL_TREE, ctor_identifier,
802                                         build_tree_list (NULL_TREE, expr),
803                                         TYPE_BINFO (type),
804                                         LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
805                                         |LOOKUP_ONLYCONVERTING);
806
807               if (init == error_mark_node)
808                 return error_mark_node;
809
810               rval = build_cplus_new (type, init);
811               rval = build_up_reference (reftype, rval, flags, 1);
812             }
813           rval_as_ctor = rval;
814         }
815
816       if (rval_as_ctor && rval_as_conversion)
817         {
818           cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
819                     intype, reftype);
820           return error_mark_node;
821         }
822       else if (rval_as_ctor)
823         rval = rval_as_ctor;
824       else if (rval_as_conversion)
825         rval = rval_as_conversion;
826       else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
827         {
828           rval = convert (type, expr);
829           if (rval == error_mark_node)
830             return error_mark_node;
831           
832           rval = build_up_reference (reftype, rval, flags, 1);
833         }
834
835       if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
836         cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
837                     reftype, intype);
838     }
839
840   if (rval)
841     {
842       /* If we found a way to convert earlier, then use it.  */
843       return rval;
844     }
845
846   my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
847
848   if (flags & LOOKUP_COMPLAIN)
849     cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
850
851   if (flags & LOOKUP_SPECULATIVELY)
852     return NULL_TREE;
853
854   return error_mark_node;
855 }
856
857 /* We are using a reference VAL for its value. Bash that reference all the
858    way down to its lowest form.  */
859
860 tree
861 convert_from_reference (val)
862      tree val;
863 {
864   tree type = TREE_TYPE (val);
865
866   if (TREE_CODE (type) == OFFSET_TYPE)
867     type = TREE_TYPE (type);
868   if (TREE_CODE (type) == REFERENCE_TYPE)
869     return build_indirect_ref (val, NULL_PTR);
870   return val;
871 }
872 \f
873 /* See if there is a constructor of type TYPE which will convert
874    EXPR.  The reference manual seems to suggest (8.5.6) that we need
875    not worry about finding constructors for base classes, then converting
876    to the derived class.
877
878    MSGP is a pointer to a message that would be an appropriate error
879    string.  If MSGP is NULL, then we are not interested in reporting
880    errors.  */
881
882 tree
883 convert_to_aggr (type, expr, msgp, protect)
884      tree type, expr;
885      char **msgp;
886      int protect;
887 {
888   tree basetype = type;
889   tree name = TYPE_IDENTIFIER (basetype);
890   tree function, fndecl, fntype, parmtypes, parmlist, result;
891 #if 0
892   /* See code below that used this.  */
893   tree method_name;
894 #endif
895   tree access;
896   int can_be_private, can_be_protected;
897
898   if (! TYPE_HAS_CONSTRUCTOR (basetype))
899     {
900       if (msgp)
901         *msgp = "type `%s' does not have a constructor";
902       return error_mark_node;
903     }
904
905   access = access_public_node;
906   can_be_private = 0;
907   can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
908
909   parmlist = build_tree_list (NULL_TREE, expr);
910   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
911
912   if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
913     {
914       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
915       parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
916     }
917
918   /* The type of the first argument will be filled in inside the loop.  */
919   parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
920   parmtypes = tree_cons (NULL_TREE, build_pointer_type (basetype), parmtypes);
921
922   /* No exact conversion was found.  See if an approximate
923      one will do.  */
924   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
925
926   {
927     int saw_private = 0;
928     int saw_protected = 0;
929     struct candidate *candidates =
930       (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
931     struct candidate *cp = candidates;
932
933     while (fndecl)
934       {
935         function = fndecl;
936         cp->h_len = 2;
937         cp->harshness = (struct harshness_code *)
938           alloca (3 * sizeof (struct harshness_code));
939
940         compute_conversion_costs (fndecl, parmlist, cp, 2);
941         if ((cp->h.code & EVIL_CODE) == 0)
942           {
943             cp->u.field = fndecl;
944             if (protect)
945               {
946                 if (TREE_PRIVATE (fndecl))
947                   access = access_private_node;
948                 else if (TREE_PROTECTED (fndecl))
949                   access = access_protected_node;
950                 else
951                   access = access_public_node;
952               }
953             else
954               access = access_public_node;
955
956             if (access == access_private_node
957                 ? (basetype == current_class_type
958                    || is_friend (basetype, cp->function)
959                    || purpose_member (basetype, DECL_ACCESS (fndecl)))
960                 : access == access_protected_node
961                 ? (can_be_protected
962                    || purpose_member (basetype, DECL_ACCESS (fndecl)))
963                 : 1)
964               {
965                 if (cp->h.code <= TRIVIAL_CODE)
966                   goto found_and_ok;
967                 cp++;
968               }
969             else
970               {
971                 if (access == access_private_node)
972                   saw_private = 1;
973                 else
974                   saw_protected = 1;
975               }
976           }
977         fndecl = DECL_CHAIN (fndecl);
978       }
979     if (cp - candidates)
980       {
981         /* Rank from worst to best.  Then cp will point to best one.
982            Private fields have their bits flipped.  For unsigned
983            numbers, this should make them look very large.
984            If the best alternate has a (signed) negative value,
985            then all we ever saw were private members.  */
986         if (cp - candidates > 1)
987           qsort (candidates,    /* char *base */
988                  cp - candidates, /* int nel */
989                  sizeof (struct candidate), /* int width */
990                  rank_for_overload); /* int (*compar)() */
991
992         --cp;
993         if (cp->h.code & EVIL_CODE)
994           {
995             if (msgp)
996               *msgp = "ambiguous type conversion possible for `%s'";
997             return error_mark_node;
998           }
999
1000         function = cp->function;
1001         fndecl = cp->u.field;
1002         goto found_and_ok;
1003       }
1004     else if (msgp)
1005       {
1006         if (saw_private)
1007           if (saw_protected)
1008             *msgp = "only private and protected conversions apply";
1009           else
1010             *msgp = "only private conversions apply";
1011         else if (saw_protected)
1012           *msgp = "only protected conversions apply";
1013         else
1014           *msgp = "no appropriate conversion to type `%s'";
1015       }
1016     return error_mark_node;
1017   }
1018   /* NOTREACHED */
1019
1020  found:
1021   if (access == access_private_node)
1022     if (! can_be_private)
1023       {
1024         if (msgp)
1025           *msgp = TREE_PRIVATE (fndecl)
1026             ? "conversion to type `%s' is private"
1027             : "conversion to type `%s' is from private base class";
1028         return error_mark_node;
1029       }
1030   if (access == access_protected_node)
1031     if (! can_be_protected)
1032       {
1033         if (msgp)
1034           *msgp = TREE_PRIVATE (fndecl)
1035             ? "conversion to type `%s' is protected"
1036             : "conversion to type `%s' is from protected base class";
1037         return error_mark_node;
1038       }
1039   function = fndecl;
1040  found_and_ok:
1041
1042   /* It will convert, but we don't do anything about it yet.  */
1043   if (msgp == 0)
1044     return NULL_TREE;
1045
1046   fntype = TREE_TYPE (function);
1047
1048   parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1049                                 parmlist, NULL_TREE, LOOKUP_NORMAL);
1050
1051   result = build_call (function, TREE_TYPE (fntype), parmlist);
1052   return result;
1053 }
1054
1055 /* Call this when we know (for any reason) that expr is not, in fact,
1056    zero.  This routine is like convert_pointer_to, but it pays
1057    attention to which specific instance of what type we want to
1058    convert to.  This routine should eventually become
1059    convert_to_pointer after all references to convert_to_pointer
1060    are removed.  */
1061
1062 tree
1063 convert_pointer_to_real (binfo, expr)
1064      tree binfo, expr;
1065 {
1066   register tree intype = TREE_TYPE (expr);
1067   tree ptr_type;
1068   tree type, rval;
1069
1070   if (TREE_CODE (binfo) == TREE_VEC)
1071     type = BINFO_TYPE (binfo);
1072   else if (IS_AGGR_TYPE (binfo))
1073     {
1074       type = binfo;
1075     }
1076   else
1077     {
1078       type = binfo;
1079       binfo = NULL_TREE;
1080     }
1081
1082   ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
1083                                     TYPE_VOLATILE (TREE_TYPE (intype)));
1084   ptr_type = build_pointer_type (ptr_type);
1085   if (ptr_type == TYPE_MAIN_VARIANT (intype))
1086     return expr;
1087
1088   if (intype == error_mark_node)
1089     return error_mark_node;
1090
1091   my_friendly_assert (!integer_zerop (expr), 191);
1092
1093   if (TREE_CODE (type) == RECORD_TYPE
1094       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1095       && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1096     {
1097       tree path;
1098       int distance
1099         = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1100                              0, &path);
1101
1102       /* This function shouldn't be called with unqualified arguments
1103          but if it is, give them an error message that they can read.  */
1104       if (distance < 0)
1105         {
1106           cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1107                     TREE_TYPE (intype), type);
1108
1109           if (distance == -2)
1110             cp_error ("because `%T' is an ambiguous base class", type);
1111           return error_mark_node;
1112         }
1113
1114       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1115     }
1116   rval = build1 (NOP_EXPR, ptr_type,
1117                  TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1118   TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1119   return rval;
1120 }
1121
1122 /* Call this when we know (for any reason) that expr is
1123    not, in fact, zero.  This routine gets a type out of the first
1124    argument and uses it to search for the type to convert to.  If there
1125    is more than one instance of that type in the expr, the conversion is
1126    ambiguous.  This routine should eventually go away, and all
1127    callers should use convert_to_pointer_real.  */
1128
1129 tree
1130 convert_pointer_to (binfo, expr)
1131      tree binfo, expr;
1132 {
1133   tree type;
1134
1135   if (TREE_CODE (binfo) == TREE_VEC)
1136     type = BINFO_TYPE (binfo);
1137   else if (IS_AGGR_TYPE (binfo))
1138       type = binfo;
1139   else
1140       type = binfo;
1141   return convert_pointer_to_real (type, expr);
1142 }
1143 \f
1144 /* Conversion...
1145
1146    FLAGS indicates how we should behave.  */
1147
1148 tree
1149 cp_convert (type, expr, convtype, flags)
1150      tree type, expr;
1151      int convtype, flags;
1152 {
1153   register tree e = expr;
1154   register enum tree_code code = TREE_CODE (type);
1155
1156   if (TREE_CODE (e) == ERROR_MARK
1157       || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
1158     return error_mark_node;
1159
1160   if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
1161     /* We need a new temporary; don't take this shortcut.  */;
1162   else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
1163     /* Trivial conversion: cv-qualifiers do not matter on rvalues.  */
1164     return fold (build1 (NOP_EXPR, type, e));
1165   
1166   if (code == VOID_TYPE && (convtype & CONV_STATIC))
1167     return build1 (CONVERT_EXPR, type, e);
1168
1169 #if 0
1170   /* This is incorrect.  A truncation can't be stripped this way.
1171      Extensions will be stripped by the use of get_unwidened.  */
1172   if (TREE_CODE (e) == NOP_EXPR)
1173     return convert (type, TREE_OPERAND (e, 0));
1174 #endif
1175
1176   /* Just convert to the type of the member.  */
1177   if (code == OFFSET_TYPE)
1178     {
1179       type = TREE_TYPE (type);
1180       code = TREE_CODE (type);
1181     }
1182
1183 #if 0
1184   if (code == REFERENCE_TYPE)
1185     return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1186   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1187     e = convert_from_reference (e);
1188 #endif
1189
1190   if (TREE_CODE (e) == OFFSET_REF)
1191     e = resolve_offset_ref (e);
1192
1193   if (TREE_READONLY_DECL_P (e))
1194     e = decl_constant_value (e);
1195
1196   if (INTEGRAL_CODE_P (code))
1197     {
1198       tree intype = TREE_TYPE (e);
1199       /* enum = enum, enum = int, enum = float, (enum)pointer are all
1200          errors.  */
1201       if (flag_int_enum_equivalence == 0
1202           && TREE_CODE (type) == ENUMERAL_TYPE
1203           && ((ARITHMETIC_TYPE_P (intype) && ! (convtype & CONV_STATIC))
1204               || (TREE_CODE (intype) == POINTER_TYPE)))
1205         {
1206           cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1207
1208           if (flag_pedantic_errors)
1209             return error_mark_node;
1210         }
1211       if (IS_AGGR_TYPE (intype))
1212         {
1213           tree rval;
1214           rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1215           if (rval)
1216             return rval;
1217           if (flags & LOOKUP_COMPLAIN)
1218             cp_error ("`%#T' used where a `%T' was expected", intype, type);
1219           if (flags & LOOKUP_SPECULATIVELY)
1220             return NULL_TREE;
1221           return error_mark_node;
1222         }
1223       if (code == BOOLEAN_TYPE)
1224         {
1225           /* Common Ada/Pascal programmer's mistake.  We always warn
1226              about this since it is so bad.  */
1227           if (TREE_CODE (expr) == FUNCTION_DECL)
1228             cp_warning ("the address of `%D', will always be `true'", expr);
1229           return truthvalue_conversion (e);
1230         }
1231       return fold (convert_to_integer (type, e));
1232     }
1233   if (code == POINTER_TYPE || code == REFERENCE_TYPE
1234       || TYPE_PTRMEMFUNC_P (type))
1235     return fold (cp_convert_to_pointer (type, e));
1236   if (code == REAL_TYPE)
1237     {
1238       if (IS_AGGR_TYPE (TREE_TYPE (e)))
1239         {
1240           tree rval;
1241           rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1242           if (rval)
1243             return rval;
1244           else
1245             if (flags & LOOKUP_COMPLAIN)
1246               cp_error ("`%#T' used where a floating point value was expected",
1247                         TREE_TYPE (e));
1248         }
1249       return fold (convert_to_real (type, e));
1250     }
1251
1252   /* New C++ semantics:  since assignment is now based on
1253      memberwise copying,  if the rhs type is derived from the
1254      lhs type, then we may still do a conversion.  */
1255   if (IS_AGGR_TYPE_CODE (code))
1256     {
1257       tree dtype = TREE_TYPE (e);
1258       tree ctor = NULL_TREE;
1259       tree conversion = NULL_TREE;
1260
1261       dtype = TYPE_MAIN_VARIANT (dtype);
1262
1263       /* Conversion of object pointers or signature pointers/references
1264          to signature pointers/references.  */
1265
1266       if (TYPE_LANG_SPECIFIC (type)
1267           && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1268         {
1269           tree constructor = build_signature_pointer_constructor (type, expr);
1270           tree sig_ty = SIGNATURE_TYPE (type);
1271           tree sig_ptr;
1272
1273           if (constructor == error_mark_node)
1274             return error_mark_node;
1275
1276           sig_ptr = get_temp_name (type, 1);
1277           DECL_INITIAL (sig_ptr) = constructor;
1278           CLEAR_SIGNATURE (sig_ty);
1279           cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1280           SET_SIGNATURE (sig_ty);
1281           TREE_READONLY (sig_ptr) = 1;
1282
1283           return sig_ptr;
1284         }
1285
1286       /* Conversion between aggregate types.  New C++ semantics allow
1287          objects of derived type to be cast to objects of base type.
1288          Old semantics only allowed this between pointers.
1289
1290          There may be some ambiguity between using a constructor
1291          vs. using a type conversion operator when both apply.  */
1292
1293       if (flag_ansi_overloading)
1294         {
1295           ctor = e;
1296           
1297           if ((flags & LOOKUP_ONLYCONVERTING)
1298               && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
1299             ctor = build_user_type_conversion (type, ctor, flags);
1300           if (ctor)
1301             ctor = build_method_call (NULL_TREE, ctor_identifier,
1302                                       build_tree_list (NULL_TREE, ctor),
1303                                       TYPE_BINFO (type), flags);
1304           if (ctor)
1305             return build_cplus_new (type, ctor);
1306         }
1307       else
1308         {
1309           if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1310               && TYPE_HAS_CONVERSION (dtype))
1311             conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1312
1313           if (conversion == error_mark_node)
1314             {
1315               if (flags & LOOKUP_COMPLAIN)
1316                 error ("ambiguous pointer conversion");
1317               return conversion;
1318             }
1319
1320           if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1321             ctor = build_method_call (NULL_TREE, ctor_identifier,
1322                                       build_tree_list (NULL_TREE, e),
1323                                       TYPE_BINFO (type),
1324                                       (flags & LOOKUP_NORMAL)
1325                                       | LOOKUP_SPECULATIVELY
1326                                       | (flags & LOOKUP_ONLYCONVERTING)
1327                                       | (flags & LOOKUP_NO_CONVERSION)
1328                                       | (conversion ? LOOKUP_NO_CONVERSION : 0));
1329
1330           if (ctor == error_mark_node)
1331             {
1332               if (flags & LOOKUP_COMPLAIN)
1333                 cp_error ("in conversion to type `%T'", type);
1334               if (flags & LOOKUP_SPECULATIVELY)
1335                 return NULL_TREE;
1336               return error_mark_node;
1337             }
1338       
1339           if (conversion && ctor)
1340             {
1341               if (flags & LOOKUP_COMPLAIN)
1342                 error ("both constructor and type conversion operator apply");
1343               if (flags & LOOKUP_SPECULATIVELY)
1344                 return NULL_TREE;
1345               return error_mark_node;
1346             }
1347           else if (conversion)
1348             return conversion;
1349           else if (ctor)
1350             {
1351               ctor = build_cplus_new (type, ctor);
1352               return ctor;
1353             }
1354         }
1355     }
1356
1357   /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1358      then the it won't be hashed and hence compare as not equal,
1359      even when it is.  */
1360   if (code == ARRAY_TYPE
1361       && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1362       && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1363     return e;
1364
1365   if (flags & LOOKUP_COMPLAIN)
1366     cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1367               TREE_TYPE (expr), type);
1368   if (flags & LOOKUP_SPECULATIVELY)
1369     return NULL_TREE;
1370   return error_mark_node;
1371 }
1372
1373 /* Create an expression whose value is that of EXPR,
1374    converted to type TYPE.  The TREE_TYPE of the value
1375    is always TYPE.  This function implements all reasonable
1376    conversions; callers should filter out those that are
1377    not permitted by the language being compiled.  */
1378
1379 tree
1380 convert (type, expr)
1381      tree type, expr;
1382 {
1383   return cp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
1384 }
1385
1386 /* Like convert, except permit conversions to take place which
1387    are not normally allowed due to access restrictions
1388    (such as conversion from sub-type to private super-type).  */
1389
1390 tree
1391 convert_force (type, expr, convtype)
1392      tree type;
1393      tree expr;
1394      int convtype;
1395 {
1396   register tree e = expr;
1397   register enum tree_code code = TREE_CODE (type);
1398
1399   if (code == REFERENCE_TYPE)
1400     return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1401                                        NULL_TREE));
1402   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1403     e = convert_from_reference (e);
1404
1405   if (code == POINTER_TYPE)
1406     return fold (convert_to_pointer_force (type, e));
1407
1408   /* From typeck.c convert_for_assignment */
1409   if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1410         && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1411         && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1412        || integer_zerop (e)
1413        || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1414       && TYPE_PTRMEMFUNC_P (type))
1415     {
1416       /* compatible pointer to member functions.  */
1417       return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1418     }
1419
1420   return cp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1421 }
1422
1423 /* Subroutine of build_type_conversion.  */
1424
1425 static tree
1426 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1427      tree xtype, basetype;
1428      tree expr;
1429      tree typename;
1430      int for_sure;
1431 {
1432   tree rval;
1433   int flags;
1434
1435   if (for_sure == 0)
1436     flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1437   else
1438     flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1439
1440   rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1441   if (rval == error_mark_node)
1442     {
1443       if (for_sure == 0)
1444         return NULL_TREE;
1445       return error_mark_node;
1446     }
1447
1448   if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1449     return rval;
1450
1451   if (warn_cast_qual
1452       && TREE_TYPE (xtype)
1453       && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1454           > TREE_READONLY (TREE_TYPE (xtype))))
1455     warning ("user-defined conversion casting away `const'");
1456   return convert (xtype, rval);
1457 }
1458
1459 /* Convert an aggregate EXPR to type XTYPE.  If a conversion
1460    exists, return the attempted conversion.  This may
1461    return ERROR_MARK_NODE if the conversion is not
1462    allowed (references private members, etc).
1463    If no conversion exists, NULL_TREE is returned.
1464
1465    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1466    to take place immediately.  Otherwise, we build a SAVE_EXPR
1467    which can be evaluated if the results are ever needed.
1468
1469    Changes to this functions should be mirrored in user_harshness.
1470
1471    FIXME: Ambiguity checking is wrong.  Should choose one by the implicit
1472    object parameter, or by the second standard conversion sequence if
1473    that doesn't do it.  This will probably wait for an overloading rewrite.
1474    (jason 8/9/95)  */
1475
1476 tree
1477 build_type_conversion (code, xtype, expr, for_sure)
1478      enum tree_code code;
1479      tree xtype, expr;
1480      int for_sure;
1481 {
1482   /* C++: check to see if we can convert this aggregate type
1483      into the required type.  */
1484   tree basetype;
1485   tree conv;
1486   tree winner = NULL_TREE;
1487
1488   if (flag_ansi_overloading)
1489     return build_user_type_conversion
1490       (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1491
1492   if (expr == error_mark_node)
1493     return error_mark_node;
1494
1495   basetype = TREE_TYPE (expr);
1496   if (TREE_CODE (basetype) == REFERENCE_TYPE)
1497     basetype = TREE_TYPE (basetype);
1498
1499   basetype = TYPE_MAIN_VARIANT (basetype);
1500   if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1501     return NULL_TREE;
1502
1503   /* Do we have an exact match?  */
1504   {
1505     tree typename = build_typename_overload (xtype);
1506     if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1507       return build_type_conversion_1 (xtype, basetype, expr, typename,
1508                                       for_sure);
1509   }
1510
1511   /* Nope; try looking for others.  */
1512   for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1513     {
1514       tree cand = TREE_VALUE (conv);
1515
1516       if (winner && winner == cand)
1517         continue;
1518
1519       if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1520         {
1521           if (winner)
1522             {
1523               if (for_sure)
1524                 {
1525                   cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1526                             xtype);
1527                   cp_error ("  candidate conversions include `%D' and `%D'",
1528                             winner, cand);
1529                 }
1530               return NULL_TREE;
1531             }
1532           else
1533             winner = cand;
1534         }
1535     }
1536
1537   if (winner)
1538     return build_type_conversion_1 (xtype, basetype, expr,
1539                                     DECL_NAME (winner), for_sure);
1540
1541   return NULL_TREE;
1542 }
1543
1544 /* Convert the given EXPR to one of a group of types suitable for use in an
1545    expression.  DESIRES is a combination of various WANT_* flags (q.v.)
1546    which indicates which types are suitable.  If COMPLAIN is 1, complain
1547    about ambiguity; otherwise, the caller will deal with it.  */
1548
1549 tree
1550 build_expr_type_conversion (desires, expr, complain)
1551      int desires;
1552      tree expr;
1553      int complain;
1554 {
1555   tree basetype = TREE_TYPE (expr);
1556   tree conv;
1557   tree winner = NULL_TREE;
1558
1559   if (TREE_CODE (basetype) == OFFSET_TYPE)
1560     expr = resolve_offset_ref (expr);
1561   expr = convert_from_reference (expr);
1562   basetype = TREE_TYPE (expr);
1563
1564   if (! IS_AGGR_TYPE (basetype))
1565     switch (TREE_CODE (basetype))
1566       {
1567       case INTEGER_TYPE:
1568         if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1569             && integer_zerop (expr))
1570           return expr;
1571         /* else fall through...  */
1572
1573       case BOOLEAN_TYPE:
1574         return (desires & WANT_INT) ? expr : NULL_TREE;
1575       case ENUMERAL_TYPE:
1576         return (desires & WANT_ENUM) ? expr : NULL_TREE;
1577       case REAL_TYPE:
1578         return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1579       case POINTER_TYPE:
1580         return (desires & WANT_POINTER) ? expr : NULL_TREE;
1581         
1582       case FUNCTION_TYPE:
1583       case ARRAY_TYPE:
1584         return (desires & WANT_POINTER) ? default_conversion (expr)
1585                                         : NULL_TREE;
1586       default:
1587         return NULL_TREE;
1588       }
1589
1590   if (! TYPE_HAS_CONVERSION (basetype))
1591     return NULL_TREE;
1592
1593   for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1594     {
1595       int win = 0;
1596       tree candidate;
1597       tree cand = TREE_VALUE (conv);
1598
1599       if (winner && winner == cand)
1600         continue;
1601
1602       candidate = TREE_TYPE (TREE_TYPE (cand));
1603       if (TREE_CODE (candidate) == REFERENCE_TYPE)
1604         candidate = TREE_TYPE (candidate);
1605
1606       switch (TREE_CODE (candidate))
1607         {
1608         case BOOLEAN_TYPE:
1609         case INTEGER_TYPE:
1610           win = (desires & WANT_INT); break;
1611         case ENUMERAL_TYPE:
1612           win = (desires & WANT_ENUM); break;
1613         case REAL_TYPE:
1614           win = (desires & WANT_FLOAT); break;
1615         case POINTER_TYPE:
1616           win = (desires & WANT_POINTER); break;
1617         }
1618
1619       if (win)
1620         {
1621           if (winner)
1622             {
1623               if (complain)
1624                 {
1625                   cp_error ("ambiguous default type conversion from `%T'",
1626                             basetype);
1627                   cp_error ("  candidate conversions include `%D' and `%D'",
1628                             winner, cand);
1629                 }
1630               return error_mark_node;
1631             }
1632           else
1633             winner = cand;
1634         }
1635     }
1636
1637   if (winner)
1638     {
1639       tree type = TREE_TYPE (TREE_TYPE (winner));
1640       if (TREE_CODE (type) == REFERENCE_TYPE)
1641         type = TREE_TYPE (type);
1642       return build_type_conversion_1 (type, basetype, expr,
1643                                       DECL_NAME (winner), 1);
1644     }
1645
1646   return NULL_TREE;
1647 }
1648
1649 /* Must convert two aggregate types to non-aggregate type.
1650    Attempts to find a non-ambiguous, "best" type conversion.
1651
1652    Return 1 on success, 0 on failure.
1653
1654    @@ What are the real semantics of this supposed to be??? */
1655
1656 int
1657 build_default_binary_type_conversion (code, arg1, arg2)
1658      enum tree_code code;
1659      tree *arg1, *arg2;
1660 {
1661   switch (code)
1662     {
1663     case MULT_EXPR:
1664     case TRUNC_DIV_EXPR:
1665     case CEIL_DIV_EXPR:
1666     case FLOOR_DIV_EXPR:
1667     case ROUND_DIV_EXPR:
1668     case EXACT_DIV_EXPR:
1669       *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1670       *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1671       break;
1672
1673     case TRUNC_MOD_EXPR:
1674     case FLOOR_MOD_EXPR:
1675     case LSHIFT_EXPR:
1676     case RSHIFT_EXPR:
1677     case BIT_AND_EXPR:
1678     case BIT_XOR_EXPR:
1679     case BIT_IOR_EXPR:
1680       *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1681       *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1682       break;
1683
1684     case PLUS_EXPR:
1685       {
1686         tree a1, a2, p1, p2;
1687         int wins;
1688
1689         a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1690         a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1691         p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1692         p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1693
1694         wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1695
1696         if (wins > 1)
1697           error ("ambiguous default type conversion for `operator +'");
1698
1699         if (a1 && a2)
1700           *arg1 = a1, *arg2 = a2;
1701         else if (a1 && p2)
1702           *arg1 = a1, *arg2 = p2;
1703         else
1704           *arg1 = p1, *arg2 = a2;
1705         break;
1706       }
1707
1708     case MINUS_EXPR:
1709       {
1710         tree a1, a2, p1, p2;
1711         int wins;
1712
1713         a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1714         a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1715         p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1716         p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1717
1718         wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1719
1720         if (wins > 1)
1721           error ("ambiguous default type conversion for `operator -'");
1722
1723         if (a1 && a2)
1724           *arg1 = a1, *arg2 = a2;
1725         else if (p1 && p2)
1726           *arg1 = p1, *arg2 = p2;
1727         else
1728           *arg1 = p1, *arg2 = a2;
1729         break;
1730       }
1731
1732     case GT_EXPR:
1733     case LT_EXPR:
1734     case GE_EXPR:
1735     case LE_EXPR:
1736     case EQ_EXPR:
1737     case NE_EXPR:
1738       {
1739         tree a1, a2, p1, p2;
1740         int wins;
1741
1742         a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1743         a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1744         p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1745         p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1746
1747         wins = (a1 && a2) + (p1 && p2);
1748
1749         if (wins > 1)
1750           cp_error ("ambiguous default type conversion for `%O'", code);
1751
1752         if (a1 && a2)
1753           *arg1 = a1, *arg2 = a2;
1754         else
1755           *arg1 = p1, *arg2 = p2;
1756         break;
1757       }
1758
1759     case TRUTH_ANDIF_EXPR:
1760     case TRUTH_ORIF_EXPR:
1761       *arg1 = convert (boolean_type_node, *arg1);
1762       *arg2 = convert (boolean_type_node, *arg2);
1763       break;
1764
1765     default:
1766       *arg1 = NULL_TREE;
1767       *arg2 = NULL_TREE;
1768     }
1769
1770   if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1771     cp_error ("ambiguous default type conversion for `%O'", code);
1772
1773   if (*arg1 && *arg2)
1774     return 1;
1775
1776   return 0;
1777 }
1778
1779 /* Implements integral promotion (4.1) and float->double promotion.  */
1780
1781 tree
1782 type_promotes_to (type)
1783      tree type;
1784 {
1785   int constp, volatilep;
1786
1787   if (type == error_mark_node)
1788     return error_mark_node;
1789
1790   constp = TYPE_READONLY (type);
1791   volatilep = TYPE_VOLATILE (type);
1792   type = TYPE_MAIN_VARIANT (type);
1793
1794   /* bool always promotes to int (not unsigned), even if it's the same
1795      size.  */
1796   if (type == boolean_type_node)
1797     type = integer_type_node;
1798
1799   /* Normally convert enums to int, but convert wide enums to something
1800      wider.  */
1801   else if (TREE_CODE (type) == ENUMERAL_TYPE
1802            || type == wchar_type_node)
1803     {
1804       int precision = MAX (TYPE_PRECISION (type),
1805                            TYPE_PRECISION (integer_type_node));
1806       tree totype = type_for_size (precision, 0);
1807       if (TREE_UNSIGNED (type)
1808           && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1809         type = type_for_size (precision, 1);
1810       else
1811         type = totype;
1812     }
1813   else if (C_PROMOTING_INTEGER_TYPE_P (type))
1814     {
1815       /* Retain unsignedness if really not getting bigger.  */
1816       if (TREE_UNSIGNED (type)
1817           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1818         type = unsigned_type_node;
1819       else
1820         type = integer_type_node;
1821     }
1822   else if (type == float_type_node)
1823     type = double_type_node;
1824
1825   return cp_build_type_variant (type, constp, volatilep);
1826 }