OSDN Git Service

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