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