OSDN Git Service

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