OSDN Git Service

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