OSDN Git Service

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