OSDN Git Service

2001-07-30 H.J. Lu (hjl@gnu.org)
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
1 /* Functions related to invoking methods and overloaded functions.
2    Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001 Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com) and
5    modified by Brendan Kehoe (brendan@cygnus.com).
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24
25 /* High-level class interface.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "output.h"
32 #include "flags.h"
33 #include "rtl.h"
34 #include "toplev.h"
35 #include "expr.h"
36 #include "ggc.h"
37 #include "diagnostic.h"
38
39 extern int inhibit_warnings;
40
41 static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
42
43 static tree build_field_call PARAMS ((tree, tree, tree, tree));
44 static struct z_candidate * tourney PARAMS ((struct z_candidate *));
45 static int equal_functions PARAMS ((tree, tree));
46 static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
47 static int compare_ics PARAMS ((tree, tree));
48 static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
49 static tree build_java_interface_fn_ref PARAMS ((tree, tree));
50 #define convert_like(CONV, EXPR) convert_like_real (CONV, EXPR, NULL_TREE, 0, 0)
51 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) convert_like_real (CONV, EXPR, FN, ARGNO, 0)
52 static tree convert_like_real PARAMS ((tree, tree, tree, int, int));
53 static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
54                             tree, const char *));
55 static tree build_object_call PARAMS ((tree, tree));
56 static tree resolve_args PARAMS ((tree));
57 static struct z_candidate * build_user_type_conversion_1
58         PARAMS ((tree, tree, int));
59 static void print_z_candidates PARAMS ((struct z_candidate *));
60 static tree build_this PARAMS ((tree));
61 static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
62 static int any_viable PARAMS ((struct z_candidate *));
63 static struct z_candidate * add_template_candidate
64         PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
65                unification_kind_t));
66 static struct z_candidate * add_template_candidate_real
67         PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
68                tree, unification_kind_t));
69 static struct z_candidate * add_template_conv_candidate 
70         PARAMS ((struct z_candidate *, tree, tree, tree, tree));
71 static struct z_candidate * add_builtin_candidates
72         PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
73                tree, tree *, int));
74 static struct z_candidate * add_builtin_candidate
75         PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
76                tree, tree, tree, tree *, tree *, int));
77 static int is_complete PARAMS ((tree));
78 static struct z_candidate * build_builtin_candidate 
79         PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
80                int));
81 static struct z_candidate * add_conv_candidate 
82         PARAMS ((struct z_candidate *, tree, tree, tree));
83 static struct z_candidate * add_function_candidate 
84         PARAMS ((struct z_candidate *, tree, tree, tree, int));
85 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
86 static tree standard_conversion PARAMS ((tree, tree, tree));
87 static tree reference_binding PARAMS ((tree, tree, tree, int));
88 static tree non_reference PARAMS ((tree));
89 static tree build_conv PARAMS ((enum tree_code, tree, tree));
90 static int is_subseq PARAMS ((tree, tree));
91 static int maybe_handle_ref_bind PARAMS ((tree*, tree*));
92 static void maybe_handle_implicit_object PARAMS ((tree*));
93 static struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
94                                                    tree, tree, int));
95 static tree source_type PARAMS ((tree));
96 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
97 static int reference_related_p PARAMS ((tree, tree));
98 static int reference_compatible_p PARAMS ((tree, tree));
99 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
100 static tree direct_reference_binding PARAMS ((tree, tree));
101 static int promoted_arithmetic_type_p PARAMS ((tree));
102 static tree conditional_conversion PARAMS ((tree, tree));
103
104 tree
105 build_vfield_ref (datum, type)
106      tree datum, type;
107 {
108   tree rval;
109
110   if (datum == error_mark_node)
111     return error_mark_node;
112
113   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
114     datum = convert_from_reference (datum);
115
116   if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
117     rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
118                   datum, TYPE_VFIELD (type));
119   else
120     rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
121
122   return rval;
123 }
124
125 /* Build a call to a member of an object.  I.e., one that overloads
126    operator ()(), or is a pointer-to-function or pointer-to-method.  */
127
128 static tree
129 build_field_call (basetype_path, instance_ptr, name, parms)
130      tree basetype_path, instance_ptr, name, parms;
131 {
132   tree field, instance;
133
134   if (IDENTIFIER_CTOR_OR_DTOR_P (name))
135     return NULL_TREE;
136
137   /* Speed up the common case.  */
138   if (instance_ptr == current_class_ptr
139       && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
140     return NULL_TREE;
141
142   field = lookup_field (basetype_path, name, 1, 0);
143
144   if (field == error_mark_node || field == NULL_TREE)
145     return field;
146
147   if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
148     {
149       /* If it's a field, try overloading operator (),
150          or calling if the field is a pointer-to-function.  */
151       instance = build_indirect_ref (instance_ptr, NULL);
152       instance = build_component_ref_1 (instance, field, 0);
153
154       if (instance == error_mark_node)
155         return error_mark_node;
156
157       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
158         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
159                                instance, parms, NULL_TREE);
160       else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
161                || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
162                    && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
163                        == FUNCTION_TYPE)))
164         return build_function_call (instance, parms);
165     }
166
167   return NULL_TREE;
168 }
169
170 /* Returns nonzero iff the destructor name specified in NAME
171    (a BIT_NOT_EXPR) matches BASETYPE.  The operand of NAME can take many
172    forms...  */
173
174 int
175 check_dtor_name (basetype, name)
176      tree basetype, name;
177 {
178   name = TREE_OPERAND (name, 0);
179
180   /* Just accept something we've already complained about.  */
181   if (name == error_mark_node)
182     return 1;
183
184   if (TREE_CODE (name) == TYPE_DECL)
185     name = TREE_TYPE (name);
186   else if (TYPE_P (name))
187     /* OK */;
188   else if (TREE_CODE (name) == IDENTIFIER_NODE)
189     {
190       if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
191           || (TREE_CODE (basetype) == ENUMERAL_TYPE
192               && name == TYPE_IDENTIFIER (basetype)))
193         name = basetype;
194       else
195         name = get_type_value (name);
196     }
197   /* In the case of:
198       
199        template <class T> struct S { ~S(); };
200        int i;
201        i.~S();
202
203      NAME will be a class template.  */
204   else if (DECL_CLASS_TEMPLATE_P (name))
205     return 0;
206   else
207     my_friendly_abort (980605);
208
209   if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
210     return 1;
211   return 0;
212 }
213
214 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
215    This is how virtual function calls are avoided.  */
216
217 tree
218 build_scoped_method_call (exp, basetype, name, parms)
219      tree exp, basetype, name, parms;
220 {
221   /* Because this syntactic form does not allow
222      a pointer to a base class to be `stolen',
223      we need not protect the derived->base conversion
224      that happens here.
225      
226      @@ But we do have to check access privileges later.  */
227   tree binfo, decl;
228   tree type = TREE_TYPE (exp);
229
230   if (type == error_mark_node
231       || basetype == error_mark_node)
232     return error_mark_node;
233
234   if (processing_template_decl)
235     {
236       if (TREE_CODE (name) == BIT_NOT_EXPR
237           && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
238         {
239           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
240           if (type)
241             name = build_min_nt (BIT_NOT_EXPR, type);
242         }
243       name = build_min_nt (SCOPE_REF, basetype, name);
244       return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
245     }
246
247   if (TREE_CODE (type) == REFERENCE_TYPE)
248     type = TREE_TYPE (type);
249
250   if (TREE_CODE (basetype) == TREE_VEC)
251     {
252       binfo = basetype;
253       basetype = BINFO_TYPE (binfo);
254     }
255   else
256     binfo = NULL_TREE;
257
258   /* Check the destructor call syntax.  */
259   if (TREE_CODE (name) == BIT_NOT_EXPR)
260     {
261       /* We can get here if someone writes their destructor call like
262          `obj.NS::~T()'; this isn't really a scoped method call, so hand
263          it off.  */
264       if (TREE_CODE (basetype) == NAMESPACE_DECL)
265         return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
266
267       if (! check_dtor_name (basetype, name))
268         cp_error ("qualified type `%T' does not match destructor name `~%T'",
269                   basetype, TREE_OPERAND (name, 0));
270
271       /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
272          that explicit ~int is caught in the parser; this deals with typedefs
273          and template parms.  */
274       if (! IS_AGGR_TYPE (basetype))
275         {
276           if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
277             cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
278                       exp, basetype, type);
279
280           return cp_convert (void_type_node, exp);
281         }
282     }
283
284   if (TREE_CODE (basetype) == NAMESPACE_DECL)
285     {
286       cp_error ("`%D' is a namespace", basetype);
287       return error_mark_node;
288     }
289   if (! is_aggr_type (basetype, 1))
290     return error_mark_node;
291
292   if (! IS_AGGR_TYPE (type))
293     {
294       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
295                 exp, type);
296       return error_mark_node;
297     }
298
299   if (! binfo)
300     {
301       binfo = get_binfo (basetype, type, 1);
302       if (binfo == error_mark_node)
303         return error_mark_node;
304       if (! binfo)
305         error_not_base_type (basetype, type);
306     }
307
308   if (binfo)
309     {
310       if (TREE_CODE (exp) == INDIRECT_REF)
311         decl = build_indirect_ref
312           (convert_pointer_to_real
313            (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL);
314       else
315         decl = build_scoped_ref (exp, basetype);
316
317       /* Call to a destructor.  */
318       if (TREE_CODE (name) == BIT_NOT_EXPR)
319         {
320           if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
321             return cp_convert (void_type_node, exp);
322           
323           return build_delete (TREE_TYPE (decl), decl, 
324                                sfk_complete_destructor,
325                                LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
326                                0);
327         }
328
329       /* Call to a method.  */
330       return build_method_call (decl, name, parms, binfo,
331                                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
332     }
333   return error_mark_node;
334 }
335
336 /* We want the address of a function or method.  We avoid creating a
337    pointer-to-member function.  */
338
339 tree
340 build_addr_func (function)
341      tree function;
342 {
343   tree type = TREE_TYPE (function);
344
345   /* We have to do these by hand to avoid real pointer to member
346      functions.  */
347   if (TREE_CODE (type) == METHOD_TYPE)
348     {
349       tree addr;
350
351       type = build_pointer_type (type);
352
353       if (mark_addressable (function) == 0)
354         return error_mark_node;
355
356       addr = build1 (ADDR_EXPR, type, function);
357
358       /* Address of a static or external variable or function counts
359          as a constant */
360       if (staticp (function))
361         TREE_CONSTANT (addr) = 1;
362
363       function = addr;
364     }
365   else
366     function = default_conversion (function);
367
368   return function;
369 }
370
371 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
372    POINTER_TYPE to those.  Note, pointer to member function types
373    (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
374
375 tree
376 build_call (function, parms)
377      tree function, parms;
378 {
379   int is_constructor = 0;
380   int nothrow;
381   tree tmp;
382   tree decl;
383   tree result_type;
384
385   function = build_addr_func (function);
386
387   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
388     {
389       sorry ("unable to call pointer to member function here");
390       return error_mark_node;
391     }
392
393   result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
394
395   if (TREE_CODE (function) == ADDR_EXPR
396       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
397     decl = TREE_OPERAND (function, 0);
398   else
399     decl = NULL_TREE;
400
401   /* We check both the decl and the type; a function may be known not to
402      throw without being declared throw().  */
403   nothrow = ((decl && TREE_NOTHROW (decl))
404              || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
405   
406   if (decl && DECL_CONSTRUCTOR_P (decl))
407     is_constructor = 1;
408
409   if (decl && ! TREE_USED (decl))
410     {
411       /* We invoke build_call directly for several library functions.
412          These may have been declared normally if we're building libgcc,
413          so we can't just check DECL_ARTIFICIAL.  */
414       if (DECL_ARTIFICIAL (decl)
415           || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
416         mark_used (decl);
417       else
418         my_friendly_abort (990125);
419     }
420
421   /* Don't pass empty class objects by value.  This is useful
422      for tags in STL, which are used to control overload resolution.
423      We don't need to handle other cases of copying empty classes.  */
424   if (! decl || ! DECL_BUILT_IN (decl))
425     for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
426       if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
427           && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
428         {
429           tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
430           TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
431                                     TREE_VALUE (tmp), t);
432         }
433
434   function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
435   TREE_HAS_CONSTRUCTOR (function) = is_constructor;
436   TREE_TYPE (function) = result_type;
437   TREE_SIDE_EFFECTS (function) = 1;
438   TREE_NOTHROW (function) = nothrow;
439   
440   return function;
441 }
442
443 /* Build something of the form ptr->method (args)
444    or object.method (args).  This can also build
445    calls to constructors, and find friends.
446
447    Member functions always take their class variable
448    as a pointer.
449
450    INSTANCE is a class instance.
451
452    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
453
454    PARMS help to figure out what that NAME really refers to.
455
456    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
457    down to the real instance type to use for access checking.  We need this
458    information to get protected accesses correct.  This parameter is used
459    by build_member_call.
460
461    FLAGS is the logical disjunction of zero or more LOOKUP_
462    flags.  See cp-tree.h for more info.
463
464    If this is all OK, calls build_function_call with the resolved
465    member function.
466
467    This function must also handle being called to perform
468    initialization, promotion/coercion of arguments, and
469    instantiation of default parameters.
470
471    Note that NAME may refer to an instance variable name.  If
472    `operator()()' is defined for the type of that field, then we return
473    that result.  */
474
475 #ifdef GATHER_STATISTICS
476 extern int n_build_method_call;
477 #endif
478
479 tree
480 build_method_call (instance, name, parms, basetype_path, flags)
481      tree instance, name, parms, basetype_path;
482      int flags;
483 {
484   tree basetype, instance_ptr;
485
486 #ifdef GATHER_STATISTICS
487   n_build_method_call++;
488 #endif
489
490   if (instance == error_mark_node
491       || name == error_mark_node
492       || parms == error_mark_node
493       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
494     return error_mark_node;
495
496   if (processing_template_decl)
497     {
498       /* We need to process template parm names here so that tsubst catches
499          them properly.  Other type names can wait.  */
500       if (TREE_CODE (name) == BIT_NOT_EXPR)
501         {
502           tree type = NULL_TREE;
503
504           if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
505             type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
506           else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
507             type = TREE_TYPE (TREE_OPERAND (name, 0));
508
509           if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
510             name = build_min_nt (BIT_NOT_EXPR, type);
511         }
512
513       return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
514     }
515
516   if (TREE_CODE (name) == BIT_NOT_EXPR)
517     {
518       if (parms)
519         error ("destructors take no parameters");
520       basetype = TREE_TYPE (instance);
521       if (TREE_CODE (basetype) == REFERENCE_TYPE)
522         basetype = TREE_TYPE (basetype);
523
524       if (! check_dtor_name (basetype, name))
525         cp_error
526           ("destructor name `~%T' does not match type `%T' of expression",
527            TREE_OPERAND (name, 0), basetype);
528
529       if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
530         return cp_convert (void_type_node, instance);
531       instance = default_conversion (instance);
532       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
533       return build_delete (build_pointer_type (basetype),
534                            instance_ptr, sfk_complete_destructor,
535                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
536     }
537
538   return build_new_method_call (instance, name, parms, basetype_path, flags);
539 }
540
541 /* New overloading code.  */
542
543 struct z_candidate {
544   tree fn;
545   tree convs;
546   tree second_conv;
547   int viable;
548   tree basetype_path;
549   tree template;
550   tree warnings;
551   struct z_candidate *next;
552 };
553
554 #define IDENTITY_RANK 0
555 #define EXACT_RANK 1
556 #define PROMO_RANK 2
557 #define STD_RANK 3
558 #define PBOOL_RANK 4
559 #define USER_RANK 5
560 #define ELLIPSIS_RANK 6
561 #define BAD_RANK 7
562
563 #define ICS_RANK(NODE)                          \
564   (ICS_BAD_FLAG (NODE) ? BAD_RANK   \
565    : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK   \
566    : ICS_USER_FLAG (NODE) ? USER_RANK           \
567    : ICS_STD_RANK (NODE))
568
569 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
570
571 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
572 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
573 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
574 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
575
576 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
577    should be created to hold the result of the conversion.  */
578 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
579
580 #define USER_CONV_CAND(NODE) \
581   ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
582 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
583
584 int
585 null_ptr_cst_p (t)
586      tree t;
587 {
588   /* [conv.ptr]
589
590      A null pointer constant is an integral constant expression
591      (_expr.const_) rvalue of integer type that evaluates to zero.  */
592   if (t == null_node
593       || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
594     return 1;
595   return 0;
596 }
597
598
599 /* Returns non-zero if PARMLIST consists of only default parms and/or
600    ellipsis. */
601
602 int
603 sufficient_parms_p (parmlist)
604      tree parmlist;
605 {
606   for (; parmlist && parmlist != void_list_node;
607        parmlist = TREE_CHAIN (parmlist))
608     if (!TREE_PURPOSE (parmlist))
609       return 0;
610   return 1;
611 }
612
613 static tree
614 build_conv (code, type, from)
615      enum tree_code code;
616      tree type, from;
617 {
618   tree t;
619   int rank = ICS_STD_RANK (from);
620
621   /* We can't use buildl1 here because CODE could be USER_CONV, which
622      takes two arguments.  In that case, the caller is responsible for
623      filling in the second argument.  */
624   t = make_node (code);
625   TREE_TYPE (t) = type;
626   TREE_OPERAND (t, 0) = from;
627
628   switch (code)
629     {
630     case PTR_CONV:
631     case PMEM_CONV:
632     case BASE_CONV:
633     case STD_CONV:
634       if (rank < STD_RANK)
635         rank = STD_RANK;
636       break;
637
638     case QUAL_CONV:
639       if (rank < EXACT_RANK)
640         rank = EXACT_RANK;
641
642     default:
643       break;
644     }
645   ICS_STD_RANK (t) = rank;
646   ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
647   ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
648   return t;
649 }
650
651 static tree
652 non_reference (t)
653      tree t;
654 {
655   if (TREE_CODE (t) == REFERENCE_TYPE)
656     t = TREE_TYPE (t);
657   return t;
658 }
659
660 tree
661 strip_top_quals (t)
662      tree t;
663 {
664   if (TREE_CODE (t) == ARRAY_TYPE)
665     return t;
666   return TYPE_MAIN_VARIANT (t);
667 }
668
669 /* Returns the standard conversion path (see [conv]) from type FROM to type
670    TO, if any.  For proper handling of null pointer constants, you must
671    also pass the expression EXPR to convert from.  */
672
673 static tree
674 standard_conversion (to, from, expr)
675      tree to, from, expr;
676 {
677   enum tree_code fcode, tcode;
678   tree conv;
679   int fromref = 0;
680
681   if (TREE_CODE (to) == REFERENCE_TYPE)
682     to = TREE_TYPE (to);
683   if (TREE_CODE (from) == REFERENCE_TYPE)
684     {
685       fromref = 1;
686       from = TREE_TYPE (from);
687     }
688   to = strip_top_quals (to);
689   from = strip_top_quals (from);
690
691   if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
692       && expr && type_unknown_p (expr))
693     {
694       expr = instantiate_type (to, expr, itf_none);
695       if (expr == error_mark_node)
696         return NULL_TREE;
697       from = TREE_TYPE (expr);
698     }
699
700   fcode = TREE_CODE (from);
701   tcode = TREE_CODE (to);
702
703   conv = build1 (IDENTITY_CONV, from, expr);
704
705   if (fcode == FUNCTION_TYPE)
706     {
707       from = build_pointer_type (from);
708       fcode = TREE_CODE (from);
709       conv = build_conv (LVALUE_CONV, from, conv);
710     }
711   else if (fcode == ARRAY_TYPE)
712     {
713       from = build_pointer_type (TREE_TYPE (from));
714       fcode = TREE_CODE (from);
715       conv = build_conv (LVALUE_CONV, from, conv);
716     }
717   else if (fromref || (expr && lvalue_p (expr)))
718     conv = build_conv (RVALUE_CONV, from, conv);
719
720    /* Allow conversion between `__complex__' data types  */
721   if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
722     {
723       /* The standard conversion sequence to convert FROM to TO is
724          the standard conversion sequence to perform componentwise
725          conversion.  */
726       tree part_conv = standard_conversion
727         (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
728       
729       if (part_conv)
730         {
731           conv = build_conv (TREE_CODE (part_conv), to, conv);
732           ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
733         }
734       else
735         conv = NULL_TREE;
736
737       return conv;
738     }
739
740   if (same_type_p (from, to))
741     return conv;
742
743   if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
744       && expr && null_ptr_cst_p (expr))
745     {
746       conv = build_conv (STD_CONV, to, conv);
747     }
748   else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
749     {
750       enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
751       enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
752
753       if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
754                                                      TREE_TYPE (to)))
755         ;
756       else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
757                && ufcode != FUNCTION_TYPE)
758         {
759           from = build_pointer_type
760             (cp_build_qualified_type (void_type_node, 
761                                       CP_TYPE_QUALS (TREE_TYPE (from))));
762           conv = build_conv (PTR_CONV, from, conv);
763         }
764       else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
765         {
766           tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
767           tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
768           tree binfo = get_binfo (fbase, tbase, 1);
769
770           if (binfo && !binfo_from_vbase (binfo)
771               && (same_type_ignoring_top_level_qualifiers_p
772                   (TREE_TYPE (TREE_TYPE (from)),
773                    TREE_TYPE (TREE_TYPE (to)))))
774             {
775               from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
776               from = build_pointer_type (from);
777               conv = build_conv (PMEM_CONV, from, conv);
778             }
779         }
780       else if (IS_AGGR_TYPE (TREE_TYPE (from))
781                && IS_AGGR_TYPE (TREE_TYPE (to)))
782         {
783           if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
784             {
785               from = 
786                 cp_build_qualified_type (TREE_TYPE (to),
787                                          CP_TYPE_QUALS (TREE_TYPE (from)));
788               from = build_pointer_type (from);
789               conv = build_conv (PTR_CONV, from, conv);
790             }
791         }
792
793       if (same_type_p (from, to))
794         /* OK */;
795       else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
796         conv = build_conv (QUAL_CONV, to, conv);
797       else if (expr && string_conv_p (to, expr, 0))
798         /* converting from string constant to char *.  */
799         conv = build_conv (QUAL_CONV, to, conv);
800       else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
801         {
802           conv = build_conv (PTR_CONV, to, conv);
803           ICS_BAD_FLAG (conv) = 1;
804         }
805       else
806         return 0;
807
808       from = to;
809     }
810   else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
811     {
812       tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
813       tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
814       tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
815       tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
816       tree binfo = get_binfo (fbase, tbase, 1);
817
818       if (!binfo || binfo_from_vbase (binfo)
819           || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
820           || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
821                          TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
822           || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
823         return 0;
824
825       from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
826       from = build_cplus_method_type (from, TREE_TYPE (fromfn),
827                                       TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
828       from = build_ptrmemfunc_type (build_pointer_type (from));
829       conv = build_conv (PMEM_CONV, from, conv);
830     }
831   else if (tcode == BOOLEAN_TYPE)
832     {
833       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
834              || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
835         return 0;
836
837       conv = build_conv (STD_CONV, to, conv);
838       if (fcode == POINTER_TYPE
839           || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
840         ICS_STD_RANK (conv) = PBOOL_RANK;
841     }
842   /* We don't check for ENUMERAL_TYPE here because there are no standard
843      conversions to enum type.  */
844   else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
845            || tcode == REAL_TYPE)
846     {
847       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
848         return 0;
849       conv = build_conv (STD_CONV, to, conv);
850
851       /* Give this a better rank if it's a promotion.  */
852       if (to == type_promotes_to (from)
853           && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
854         ICS_STD_RANK (conv) = PROMO_RANK;
855     }
856   else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
857            && is_properly_derived_from (from, to))
858     {
859       if (TREE_CODE (conv) == RVALUE_CONV)
860         conv = TREE_OPERAND (conv, 0);
861       conv = build_conv (BASE_CONV, to, conv);
862       /* The derived-to-base conversion indicates the initialization
863          of a parameter with base type from an object of a derived
864          type.  A temporary object is created to hold the result of
865          the conversion.  */
866       NEED_TEMPORARY_P (conv) = 1;
867     }
868   else
869     return 0;
870
871   return conv;
872 }
873
874 /* Returns non-zero if T1 is reference-related to T2.  */
875
876 static int
877 reference_related_p (t1, t2)
878      tree t1;
879      tree t2;
880 {
881   t1 = TYPE_MAIN_VARIANT (t1);
882   t2 = TYPE_MAIN_VARIANT (t2);
883
884   /* [dcl.init.ref]
885
886      Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
887      to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
888      of T2.  */
889   return (same_type_p (t1, t2)
890           || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
891               && DERIVED_FROM_P (t1, t2)));
892 }
893
894 /* Returns non-zero if T1 is reference-compatible with T2.  */
895
896 static int
897 reference_compatible_p (t1, t2)
898      tree t1;
899      tree t2;
900 {
901   /* [dcl.init.ref]
902
903      "cv1 T1" is reference compatible with "cv2 T2" if T1 is
904      reference-related to T2 and cv1 is the same cv-qualification as,
905      or greater cv-qualification than, cv2.  */
906   return (reference_related_p (t1, t2)
907           && at_least_as_qualified_p (t1, t2));
908 }
909
910 /* Determine whether or not the EXPR (of class type S) can be
911    converted to T as in [over.match.ref].  */
912
913 static tree
914 convert_class_to_reference (t, s, expr)
915      tree t;
916      tree s;
917      tree expr;
918 {
919   tree conversions;
920   tree arglist;
921   tree conv;
922   struct z_candidate *candidates;
923   struct z_candidate *cand;
924
925   /* [over.match.ref]
926
927      Assuming that "cv1 T" is the underlying type of the reference
928      being initialized, and "cv S" is the type of the initializer
929      expression, with S a class type, the candidate functions are
930      selected as follows:
931
932      --The conversion functions of S and its base classes are
933        considered.  Those that are not hidden within S and yield type
934        "reference to cv2 T2", where "cv1 T" is reference-compatible
935        (_dcl.init.ref_) with "cv2 T2", are candidate functions.
936
937      The argument list has one argument, which is the initializer
938      expression.  */
939
940   candidates = 0;
941
942   /* Conceptually, we should take the address of EXPR and put it in
943      the argument list.  Unfortunately, however, that can result in
944      error messages, which we should not issue now because we are just
945      trying to find a conversion operator.  Therefore, we use NULL,
946      cast to the appropriate type.  */
947   arglist = build_int_2 (0, 0);
948   TREE_TYPE (arglist) = build_pointer_type (s);
949   arglist = build_tree_list (NULL_TREE, arglist);
950   
951   for (conversions = lookup_conversions (s);
952        conversions;
953        conversions = TREE_CHAIN (conversions))
954     {
955       tree fns = TREE_VALUE (conversions);
956
957       for (; fns; fns = OVL_NEXT (fns))
958         {
959           tree f = OVL_CURRENT (fns);
960           tree t2 = TREE_TYPE (TREE_TYPE (f));
961           struct z_candidate *old_candidates = candidates;
962
963           /* If this is a template function, try to get an exact
964              match.  */
965           if (TREE_CODE (f) == TEMPLATE_DECL)
966             {
967               candidates 
968                 = add_template_candidate (candidates,
969                                           f, s,
970                                           NULL_TREE,
971                                           arglist,
972                                           build_reference_type (t),
973                                           LOOKUP_NORMAL,
974                                           DEDUCE_CONV);
975               
976               if (candidates != old_candidates)
977                 {
978                   /* Now, see if the conversion function really returns
979                      an lvalue of the appropriate type.  From the
980                      point of view of unification, simply returning an
981                      rvalue of the right type is good enough.  */
982                   f = candidates->fn;
983                   t2 = TREE_TYPE (TREE_TYPE (f));
984                   if (TREE_CODE (t2) != REFERENCE_TYPE
985                       || !reference_compatible_p (t, TREE_TYPE (t2)))
986                     candidates = candidates->next;
987                 }
988             }
989           else if (TREE_CODE (t2) == REFERENCE_TYPE
990                    && reference_compatible_p (t, TREE_TYPE (t2)))
991             candidates 
992               = add_function_candidate (candidates, f, s, arglist, 
993                                         LOOKUP_NORMAL);
994
995           if (candidates != old_candidates)
996             candidates->basetype_path = TYPE_BINFO (s);
997         }
998     }
999
1000   /* If none of the conversion functions worked out, let our caller
1001      know.  */
1002   if (!any_viable (candidates))
1003     return NULL_TREE;
1004   
1005   candidates = splice_viable (candidates);
1006   cand = tourney (candidates);
1007   if (!cand)
1008     return NULL_TREE;
1009
1010   conv = build1 (IDENTITY_CONV, s, expr);
1011   conv = build_conv (USER_CONV,
1012                      non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
1013                      conv);
1014   TREE_OPERAND (conv, 1) = build_ptr_wrapper (cand);
1015   ICS_USER_FLAG (conv) = 1;
1016   if (cand->viable == -1)
1017     ICS_BAD_FLAG (conv) = 1;
1018   cand->second_conv = conv;
1019
1020   return conv;
1021 }
1022
1023 /* A reference of the indicated TYPE is being bound directly to the
1024    expression represented by the implicit conversion sequence CONV.
1025    Return a conversion sequence for this binding.  */
1026
1027 static tree
1028 direct_reference_binding (type, conv)
1029      tree type;
1030      tree conv;
1031 {
1032   tree t = TREE_TYPE (type);
1033
1034   /* [over.ics.rank] 
1035      
1036      When a parameter of reference type binds directly
1037      (_dcl.init.ref_) to an argument expression, the implicit
1038      conversion sequence is the identity conversion, unless the
1039      argument expression has a type that is a derived class of the
1040      parameter type, in which case the implicit conversion sequence is
1041      a derived-to-base Conversion.
1042          
1043      If the parameter binds directly to the result of applying a
1044      conversion function to the argument expression, the implicit
1045      conversion sequence is a user-defined conversion sequence
1046      (_over.ics.user_), with the second standard conversion sequence
1047      either an identity conversion or, if the conversion function
1048      returns an entity of a type that is a derived class of the
1049      parameter type, a derived-to-base conversion.  */
1050   if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1051     {
1052       /* Represent the derived-to-base conversion.  */
1053       conv = build_conv (BASE_CONV, t, conv);
1054       /* We will actually be binding to the base-class subobject in
1055          the derived class, so we mark this conversion appropriately.
1056          That way, convert_like knows not to generate a temporary.  */
1057       NEED_TEMPORARY_P (conv) = 0;
1058     }
1059   return build_conv (REF_BIND, type, conv);
1060 }
1061
1062 /* Returns the conversion path from type FROM to reference type TO for
1063    purposes of reference binding.  For lvalue binding, either pass a
1064    reference type to FROM or an lvalue expression to EXPR.  If the
1065    reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1066    the conversion returned.  */
1067
1068 static tree
1069 reference_binding (rto, rfrom, expr, flags)
1070      tree rto, rfrom, expr;
1071      int flags;
1072 {
1073   tree conv = NULL_TREE;
1074   tree to = TREE_TYPE (rto);
1075   tree from = rfrom;
1076   int related_p;
1077   int compatible_p;
1078   cp_lvalue_kind lvalue_p = clk_none;
1079
1080   if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1081     {
1082       expr = instantiate_type (to, expr, itf_none);
1083       if (expr == error_mark_node)
1084         return NULL_TREE;
1085       from = TREE_TYPE (expr);
1086     }
1087
1088   if (TREE_CODE (from) == REFERENCE_TYPE)
1089     {
1090       /* Anything with reference type is an lvalue.  */
1091       lvalue_p = clk_ordinary;
1092       from = TREE_TYPE (from);
1093     }
1094   else if (expr)
1095     lvalue_p = real_lvalue_p (expr);
1096
1097   /* Figure out whether or not the types are reference-related and
1098      reference compatible.  We have do do this after stripping
1099      references from FROM.  */
1100   related_p = reference_related_p (to, from);
1101   compatible_p = reference_compatible_p (to, from);
1102
1103   if (lvalue_p && compatible_p)
1104     {
1105       /* [dcl.init.ref]
1106
1107          If the intializer expression 
1108          
1109          -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1110             is reference-compatible with "cv2 T2,"
1111          
1112          the reference is bound directly to the initializer exprssion
1113          lvalue.  */
1114       conv = build1 (IDENTITY_CONV, from, expr);
1115       conv = direct_reference_binding (rto, conv);
1116       if ((lvalue_p & clk_bitfield) != 0 
1117           && CP_TYPE_CONST_NON_VOLATILE_P (to))
1118         /* For the purposes of overload resolution, we ignore the fact
1119            this expression is a bitfield. (In particular,
1120            [over.ics.ref] says specifically that a function with a
1121            non-const reference parameter is viable even if the
1122            argument is a bitfield.)
1123
1124            However, when we actually call the function we must create
1125            a temporary to which to bind the reference.  If the
1126            reference is volatile, or isn't const, then we cannot make
1127            a temporary, so we just issue an error when the conversion
1128            actually occurs.  */
1129         NEED_TEMPORARY_P (conv) = 1;
1130       return conv;
1131     }
1132   else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1133     {
1134       /* [dcl.init.ref]
1135
1136          If the initializer exprsesion
1137
1138          -- has a class type (i.e., T2 is a class type) can be
1139             implicitly converted to an lvalue of type "cv3 T3," where
1140             "cv1 T1" is reference-compatible with "cv3 T3".  (this
1141             conversion is selected by enumerating the applicable
1142             conversion functions (_over.match.ref_) and choosing the
1143             best one through overload resolution.  (_over.match_). 
1144
1145         the reference is bound to the lvalue result of the conversion
1146         in the second case.  */
1147       conv = convert_class_to_reference (to, from, expr);
1148       if (conv)
1149         return direct_reference_binding (rto, conv);
1150     }
1151
1152   /* From this point on, we conceptually need temporaries, even if we
1153      elide them.  Only the cases above are "direct bindings".  */
1154   if (flags & LOOKUP_NO_TEMP_BIND)
1155     return NULL_TREE;
1156
1157   /* [over.ics.rank]
1158      
1159      When a parameter of reference type is not bound directly to an
1160      argument expression, the conversion sequence is the one required
1161      to convert the argument expression to the underlying type of the
1162      reference according to _over.best.ics_.  Conceptually, this
1163      conversion sequence corresponds to copy-initializing a temporary
1164      of the underlying type with the argument expression.  Any
1165      difference in top-level cv-qualification is subsumed by the
1166      initialization itself and does not constitute a conversion.  */
1167
1168   /* [dcl.init.ref]
1169
1170      Otherwise, the reference shall be to a non-volatile const type.  */
1171   if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1172     return NULL_TREE;
1173
1174   /* [dcl.init.ref]
1175      
1176      If the initializer expression is an rvalue, with T2 a class type,
1177      and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1178      is bound in one of the following ways:
1179      
1180      -- The reference is bound to the object represented by the rvalue
1181         or to a sub-object within that object.  
1182
1183      In this case, the implicit conversion sequence is supposed to be
1184      same as we would obtain by generating a temporary.  Fortunately,
1185      if the types are reference compatible, then this is either an
1186      identity conversion or the derived-to-base conversion, just as
1187      for direct binding.  */
1188   if (CLASS_TYPE_P (from) && compatible_p)
1189     {
1190       conv = build1 (IDENTITY_CONV, from, expr);
1191       return direct_reference_binding (rto, conv);
1192     }
1193
1194   /* [dcl.init.ref]
1195
1196      Otherwise, a temporary of type "cv1 T1" is created and
1197      initialized from the initializer expression using the rules for a
1198      non-reference copy initialization.  If T1 is reference-related to
1199      T2, cv1 must be the same cv-qualification as, or greater
1200      cv-qualification than, cv2; otherwise, the program is ill-formed.  */
1201   if (related_p && !at_least_as_qualified_p (to, from))
1202     return NULL_TREE;
1203
1204   conv = implicit_conversion (to, from, expr, flags);
1205   if (!conv)
1206     return NULL_TREE;
1207
1208   conv = build_conv (REF_BIND, rto, conv);
1209   /* This reference binding, unlike those above, requires the
1210      creation of a temporary.  */
1211   NEED_TEMPORARY_P (conv) = 1;
1212
1213   return conv;
1214 }
1215
1216 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1217    to type TO.  The optional expression EXPR may affect the conversion.
1218    FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
1219    significant.  */
1220
1221 static tree
1222 implicit_conversion (to, from, expr, flags)
1223      tree to, from, expr;
1224      int flags;
1225 {
1226   tree conv;
1227   struct z_candidate *cand;
1228
1229   /* Resolve expressions like `A::p' that we thought might become
1230      pointers-to-members.  */
1231   if (expr && TREE_CODE (expr) == OFFSET_REF)
1232     {
1233       expr = resolve_offset_ref (expr);
1234       from = TREE_TYPE (expr);
1235     }
1236
1237   if (from == error_mark_node || to == error_mark_node
1238       || expr == error_mark_node)
1239     return NULL_TREE;
1240
1241   /* Make sure both the FROM and TO types are complete so that
1242      user-defined conversions are available.  */
1243   complete_type (from);
1244   complete_type (to);
1245
1246   if (TREE_CODE (to) == REFERENCE_TYPE)
1247     conv = reference_binding (to, from, expr, flags);
1248   else
1249     conv = standard_conversion (to, from, expr);
1250
1251   if (conv)
1252     ;
1253   else if (expr != NULL_TREE
1254            && (IS_AGGR_TYPE (from)
1255                || IS_AGGR_TYPE (to))
1256            && (flags & LOOKUP_NO_CONVERSION) == 0)
1257     {
1258       cand = build_user_type_conversion_1
1259         (to, expr, LOOKUP_ONLYCONVERTING);
1260       if (cand)
1261         conv = cand->second_conv;
1262
1263       /* We used to try to bind a reference to a temporary here, but that
1264          is now handled by the recursive call to this function at the end
1265          of reference_binding.  */
1266     }
1267
1268   return conv;
1269 }
1270
1271 /* Add a new entry to the list of candidates.  Used by the add_*_candidate
1272    functions.  */
1273
1274 static struct z_candidate *
1275 add_candidate (candidates, fn, convs, viable)
1276      struct z_candidate *candidates;
1277      tree fn, convs;
1278      int viable;
1279 {
1280   struct z_candidate *cand
1281     = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1282
1283   cand->fn = fn;
1284   cand->convs = convs;
1285   cand->viable = viable;
1286   cand->next = candidates;
1287
1288   return cand;
1289 }
1290
1291 /* Create an overload candidate for the function or method FN called with
1292    the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
1293    to implicit_conversion.
1294
1295    CTYPE, if non-NULL, is the type we want to pretend this function
1296    comes from for purposes of overload resolution.  */
1297
1298 static struct z_candidate *
1299 add_function_candidate (candidates, fn, ctype, arglist, flags)
1300      struct z_candidate *candidates;
1301      tree fn, ctype, arglist;
1302      int flags;
1303 {
1304   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1305   int i, len;
1306   tree convs;
1307   tree parmnode, argnode;
1308   int viable = 1;
1309
1310   /* The `this', `in_chrg' and VTT arguments to constructors are not
1311      considered in overload resolution.  */
1312   if (DECL_CONSTRUCTOR_P (fn))
1313     {
1314       parmlist = skip_artificial_parms_for (fn, parmlist);
1315       arglist = skip_artificial_parms_for (fn, arglist);
1316     }
1317
1318   len = list_length (arglist);
1319   convs = make_tree_vec (len);
1320
1321   /* 13.3.2 - Viable functions [over.match.viable]
1322      First, to be a viable function, a candidate function shall have enough
1323      parameters to agree in number with the arguments in the list.
1324
1325      We need to check this first; otherwise, checking the ICSes might cause
1326      us to produce an ill-formed template instantiation.  */
1327
1328   parmnode = parmlist;
1329   for (i = 0; i < len; ++i)
1330     {
1331       if (parmnode == NULL_TREE || parmnode == void_list_node)
1332         break;
1333       parmnode = TREE_CHAIN (parmnode);
1334     }
1335
1336   if (i < len && parmnode)
1337     viable = 0;
1338
1339   /* Make sure there are default args for the rest of the parms.  */
1340   else if (!sufficient_parms_p (parmnode))
1341     viable = 0;
1342
1343   if (! viable)
1344     goto out;
1345
1346   /* Second, for F to be a viable function, there shall exist for each
1347      argument an implicit conversion sequence that converts that argument
1348      to the corresponding parameter of F.  */
1349
1350   parmnode = parmlist;
1351   argnode = arglist;
1352
1353   for (i = 0; i < len; ++i)
1354     {
1355       tree arg = TREE_VALUE (argnode);
1356       tree argtype = lvalue_type (arg);
1357       tree t;
1358       int is_this;
1359
1360       if (parmnode == void_list_node)
1361         break;
1362
1363       is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1364                  && ! DECL_CONSTRUCTOR_P (fn));
1365
1366       if (parmnode)
1367         {
1368           tree parmtype = TREE_VALUE (parmnode);
1369
1370           /* The type of the implicit object parameter ('this') for
1371              overload resolution is not always the same as for the
1372              function itself; conversion functions are considered to
1373              be members of the class being converted, and functions
1374              introduced by a using-declaration are considered to be
1375              members of the class that uses them.
1376
1377              Since build_over_call ignores the ICS for the `this'
1378              parameter, we can just change the parm type.  */
1379           if (ctype && is_this)
1380             {
1381               parmtype
1382                 = build_qualified_type (ctype,
1383                                         TYPE_QUALS (TREE_TYPE (parmtype)));
1384               parmtype = build_pointer_type (parmtype);
1385             }
1386
1387           t = implicit_conversion (parmtype, argtype, arg, flags);
1388         }
1389       else
1390         {
1391           t = build1 (IDENTITY_CONV, argtype, arg);
1392           ICS_ELLIPSIS_FLAG (t) = 1;
1393         }
1394
1395       if (t && is_this)
1396         ICS_THIS_FLAG (t) = 1;
1397
1398       TREE_VEC_ELT (convs, i) = t;
1399       if (! t)
1400         {
1401           viable = 0;
1402           break;
1403         }
1404
1405       if (ICS_BAD_FLAG (t))
1406         viable = -1;
1407
1408       if (parmnode)
1409         parmnode = TREE_CHAIN (parmnode);
1410       argnode = TREE_CHAIN (argnode);
1411     }
1412
1413  out:
1414   return add_candidate (candidates, fn, convs, viable);
1415 }
1416
1417 /* Create an overload candidate for the conversion function FN which will
1418    be invoked for expression OBJ, producing a pointer-to-function which
1419    will in turn be called with the argument list ARGLIST, and add it to
1420    CANDIDATES.  FLAGS is passed on to implicit_conversion.
1421
1422    Actually, we don't really care about FN; we care about the type it
1423    converts to.  There may be multiple conversion functions that will
1424    convert to that type, and we rely on build_user_type_conversion_1 to
1425    choose the best one; so when we create our candidate, we record the type
1426    instead of the function.  */
1427
1428 static struct z_candidate *
1429 add_conv_candidate (candidates, fn, obj, arglist)
1430      struct z_candidate *candidates;
1431      tree fn, obj, arglist;
1432 {
1433   tree totype = TREE_TYPE (TREE_TYPE (fn));
1434   int i, len, viable, flags;
1435   tree parmlist, convs, parmnode, argnode;
1436
1437   for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1438     parmlist = TREE_TYPE (parmlist);
1439   parmlist = TYPE_ARG_TYPES (parmlist);
1440
1441   len = list_length (arglist) + 1;
1442   convs = make_tree_vec (len);
1443   parmnode = parmlist;
1444   argnode = arglist;
1445   viable = 1;
1446   flags = LOOKUP_NORMAL;
1447
1448   /* Don't bother looking up the same type twice.  */
1449   if (candidates && candidates->fn == totype)
1450     return candidates;
1451
1452   for (i = 0; i < len; ++i)
1453     {
1454       tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1455       tree argtype = lvalue_type (arg);
1456       tree t;
1457
1458       if (i == 0)
1459         t = implicit_conversion (totype, argtype, arg, flags);
1460       else if (parmnode == void_list_node)
1461         break;
1462       else if (parmnode)
1463         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1464       else
1465         {
1466           t = build1 (IDENTITY_CONV, argtype, arg);
1467           ICS_ELLIPSIS_FLAG (t) = 1;
1468         }
1469
1470       TREE_VEC_ELT (convs, i) = t;
1471       if (! t)
1472         break;
1473
1474       if (ICS_BAD_FLAG (t))
1475         viable = -1;
1476
1477       if (i == 0)
1478         continue;
1479
1480       if (parmnode)
1481         parmnode = TREE_CHAIN (parmnode);
1482       argnode = TREE_CHAIN (argnode);
1483     }
1484
1485   if (i < len)
1486     viable = 0;
1487
1488   if (!sufficient_parms_p (parmnode))
1489     viable = 0;
1490
1491   return add_candidate (candidates, totype, convs, viable);
1492 }
1493
1494 static struct z_candidate *
1495 build_builtin_candidate (candidates, fnname, type1, type2,
1496                          args, argtypes, flags)
1497      struct z_candidate *candidates;
1498      tree fnname, type1, type2, *args, *argtypes;
1499      int flags;
1500
1501 {
1502   tree t, convs;
1503   int viable = 1, i;
1504   tree types[2];
1505
1506   types[0] = type1;
1507   types[1] = type2;
1508
1509   convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1510
1511   for (i = 0; i < 2; ++i)
1512     {
1513       if (! args[i])
1514         break;
1515
1516       t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1517       if (! t)
1518         {
1519           viable = 0;
1520           /* We need something for printing the candidate.  */
1521           t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1522         }
1523       else if (ICS_BAD_FLAG (t))
1524         viable = 0;
1525       TREE_VEC_ELT (convs, i) = t;
1526     }
1527
1528   /* For COND_EXPR we rearranged the arguments; undo that now.  */
1529   if (args[2])
1530     {
1531       TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1532       TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1533       t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1534       if (t)
1535         TREE_VEC_ELT (convs, 0) = t;
1536       else
1537         viable = 0;
1538     }      
1539
1540   return add_candidate (candidates, fnname, convs, viable);
1541 }
1542
1543 static int
1544 is_complete (t)
1545      tree t;
1546 {
1547   return COMPLETE_TYPE_P (complete_type (t));
1548 }
1549
1550 /* Returns non-zero if TYPE is a promoted arithmetic type.  */
1551
1552 static int
1553 promoted_arithmetic_type_p (type)
1554      tree type;
1555 {
1556   /* [over.built]
1557
1558      In this section, the term promoted integral type is used to refer
1559      to those integral types which are preserved by integral promotion
1560      (including e.g.  int and long but excluding e.g.  char).
1561      Similarly, the term promoted arithmetic type refers to promoted
1562      integral types plus floating types.  */
1563   return ((INTEGRAL_TYPE_P (type)
1564            && same_type_p (type_promotes_to (type), type))
1565           || TREE_CODE (type) == REAL_TYPE);
1566 }
1567
1568 /* Create any builtin operator overload candidates for the operator in
1569    question given the converted operand types TYPE1 and TYPE2.  The other
1570    args are passed through from add_builtin_candidates to
1571    build_builtin_candidate.  
1572    
1573    TYPE1 and TYPE2 may not be permissible, and we must filter them. 
1574    If CODE is requires candidates operands of the same type of the kind
1575    of which TYPE1 and TYPE2 are, we add both candidates
1576    CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2).  */
1577
1578 static struct z_candidate *
1579 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1580                        args, argtypes, flags)
1581      struct z_candidate *candidates;
1582      enum tree_code code, code2;
1583      tree fnname, type1, type2, *args, *argtypes;
1584      int flags;
1585 {
1586   switch (code)
1587     {
1588     case POSTINCREMENT_EXPR:
1589     case POSTDECREMENT_EXPR:
1590       args[1] = integer_zero_node;
1591       type2 = integer_type_node;
1592       break;
1593     default:
1594       break;
1595     }
1596
1597   switch (code)
1598     {
1599
1600 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1601      and  VQ  is  either  volatile or empty, there exist candidate operator
1602      functions of the form
1603              VQ T&   operator++(VQ T&);
1604              T       operator++(VQ T&, int);
1605    5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1606      type  other than bool, and VQ is either volatile or empty, there exist
1607      candidate operator functions of the form
1608              VQ T&   operator--(VQ T&);
1609              T       operator--(VQ T&, int);
1610    6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
1611      complete  object type, and VQ is either volatile or empty, there exist
1612      candidate operator functions of the form
1613              T*VQ&   operator++(T*VQ&);
1614              T*VQ&   operator--(T*VQ&);
1615              T*      operator++(T*VQ&, int);
1616              T*      operator--(T*VQ&, int);  */
1617
1618     case POSTDECREMENT_EXPR:
1619     case PREDECREMENT_EXPR:
1620       if (TREE_CODE (type1) == BOOLEAN_TYPE)
1621         return candidates;
1622     case POSTINCREMENT_EXPR:
1623     case PREINCREMENT_EXPR:
1624       if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1625         {
1626           type1 = build_reference_type (type1);
1627           break;
1628         }
1629       return candidates;
1630
1631 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1632      exist candidate operator functions of the form
1633
1634              T&      operator*(T*);
1635
1636    8 For every function type T, there exist candidate operator functions of
1637      the form
1638              T&      operator*(T*);  */
1639
1640     case INDIRECT_REF:
1641       if (TREE_CODE (type1) == POINTER_TYPE
1642           && (TYPE_PTROB_P (type1)
1643               || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1644         break;
1645       return candidates;
1646
1647 /* 9 For every type T, there exist candidate operator functions of the form
1648              T*      operator+(T*);
1649
1650    10For  every  promoted arithmetic type T, there exist candidate operator
1651      functions of the form
1652              T       operator+(T);
1653              T       operator-(T);  */
1654
1655     case CONVERT_EXPR: /* unary + */
1656       if (TREE_CODE (type1) == POINTER_TYPE
1657           && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1658         break;
1659     case NEGATE_EXPR:
1660       if (ARITHMETIC_TYPE_P (type1))
1661         break;
1662       return candidates;
1663
1664 /* 11For every promoted integral type T,  there  exist  candidate  operator
1665      functions of the form
1666              T       operator~(T);  */
1667
1668     case BIT_NOT_EXPR:
1669       if (INTEGRAL_TYPE_P (type1))
1670         break;
1671       return candidates;
1672
1673 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1674      is the same type as C2 or is a derived class of C2, T  is  a  complete
1675      object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1676      there exist candidate operator functions of the form
1677              CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1678      where CV12 is the union of CV1 and CV2.  */
1679
1680     case MEMBER_REF:
1681       if (TREE_CODE (type1) == POINTER_TYPE
1682           && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1683         {
1684           tree c1 = TREE_TYPE (type1);
1685           tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1686                      ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1687                      : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1688
1689           if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1690               && (TYPE_PTRMEMFUNC_P (type2)
1691                   || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1692             break;
1693         }
1694       return candidates;
1695
1696 /* 13For every pair of promoted arithmetic types L and R, there exist  can-
1697      didate operator functions of the form
1698              LR      operator*(L, R);
1699              LR      operator/(L, R);
1700              LR      operator+(L, R);
1701              LR      operator-(L, R);
1702              bool    operator<(L, R);
1703              bool    operator>(L, R);
1704              bool    operator<=(L, R);
1705              bool    operator>=(L, R);
1706              bool    operator==(L, R);
1707              bool    operator!=(L, R);
1708      where  LR  is  the  result of the usual arithmetic conversions between
1709      types L and R.
1710
1711    14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
1712      unqualified  complete  object  type and I is a promoted integral type,
1713      there exist candidate operator functions of the form
1714              T*      operator+(T*, I);
1715              T&      operator[](T*, I);
1716              T*      operator-(T*, I);
1717              T*      operator+(I, T*);
1718              T&      operator[](I, T*);
1719
1720    15For every T, where T is a pointer to complete object type, there exist
1721      candidate operator functions of the form112)
1722              ptrdiff_t operator-(T, T);
1723
1724    16For every pointer or enumeration type T, there exist candidate operator
1725      functions of the form
1726              bool    operator<(T, T);
1727              bool    operator>(T, T);
1728              bool    operator<=(T, T);
1729              bool    operator>=(T, T);
1730              bool    operator==(T, T);
1731              bool    operator!=(T, T);
1732
1733    17For every pointer to member type T,  there  exist  candidate  operator
1734      functions of the form
1735              bool    operator==(T, T);
1736              bool    operator!=(T, T);  */
1737
1738     case MINUS_EXPR:
1739       if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1740         break;
1741       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1742         {
1743           type2 = ptrdiff_type_node;
1744           break;
1745         }
1746     case MULT_EXPR:
1747     case TRUNC_DIV_EXPR:
1748       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1749         break;
1750       return candidates;
1751
1752     case EQ_EXPR:
1753     case NE_EXPR:
1754       if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1755           || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1756         break;
1757       if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1758           && null_ptr_cst_p (args[1]))
1759         {
1760           type2 = type1;
1761           break;
1762         }
1763       if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1764           && null_ptr_cst_p (args[0]))
1765         {
1766           type1 = type2;
1767           break;
1768         }
1769       /* FALLTHROUGH */
1770     case LT_EXPR:
1771     case GT_EXPR:
1772     case LE_EXPR:
1773     case GE_EXPR:
1774     case MAX_EXPR:
1775     case MIN_EXPR:
1776       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1777         break;
1778       if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1779         break;
1780       if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1781         break;
1782       if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1783         {
1784           type2 = type1;
1785           break;
1786         }
1787       if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1788         {
1789           type1 = type2;
1790           break;
1791         }
1792       return candidates;
1793
1794     case PLUS_EXPR:
1795       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1796         break;
1797     case ARRAY_REF:
1798       if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1799         {
1800           type1 = ptrdiff_type_node;
1801           break;
1802         }
1803       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1804         {
1805           type2 = ptrdiff_type_node;
1806           break;
1807         }
1808       return candidates;
1809
1810 /* 18For  every pair of promoted integral types L and R, there exist candi-
1811      date operator functions of the form
1812              LR      operator%(L, R);
1813              LR      operator&(L, R);
1814              LR      operator^(L, R);
1815              LR      operator|(L, R);
1816              L       operator<<(L, R);
1817              L       operator>>(L, R);
1818      where LR is the result of the  usual  arithmetic  conversions  between
1819      types L and R.  */
1820
1821     case TRUNC_MOD_EXPR:
1822     case BIT_AND_EXPR:
1823     case BIT_IOR_EXPR:
1824     case BIT_XOR_EXPR:
1825     case LSHIFT_EXPR:
1826     case RSHIFT_EXPR:
1827       if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1828         break;
1829       return candidates;
1830
1831 /* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
1832      type, VQ is either volatile or empty, and R is a  promoted  arithmetic
1833      type, there exist candidate operator functions of the form
1834              VQ L&   operator=(VQ L&, R);
1835              VQ L&   operator*=(VQ L&, R);
1836              VQ L&   operator/=(VQ L&, R);
1837              VQ L&   operator+=(VQ L&, R);
1838              VQ L&   operator-=(VQ L&, R);
1839
1840    20For  every  pair T, VQ), where T is any type and VQ is either volatile
1841      or empty, there exist candidate operator functions of the form
1842              T*VQ&   operator=(T*VQ&, T*);
1843
1844    21For every pair T, VQ), where T is a pointer to member type and  VQ  is
1845      either  volatile or empty, there exist candidate operator functions of
1846      the form
1847              VQ T&   operator=(VQ T&, T);
1848
1849    22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
1850      unqualified  complete object type, VQ is either volatile or empty, and
1851      I is a promoted integral type, there exist  candidate  operator  func-
1852      tions of the form
1853              T*VQ&   operator+=(T*VQ&, I);
1854              T*VQ&   operator-=(T*VQ&, I);
1855
1856    23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
1857      type, VQ is either volatile or empty, and R  is  a  promoted  integral
1858      type, there exist candidate operator functions of the form
1859
1860              VQ L&   operator%=(VQ L&, R);
1861              VQ L&   operator<<=(VQ L&, R);
1862              VQ L&   operator>>=(VQ L&, R);
1863              VQ L&   operator&=(VQ L&, R);
1864              VQ L&   operator^=(VQ L&, R);
1865              VQ L&   operator|=(VQ L&, R);  */
1866
1867     case MODIFY_EXPR:
1868       switch (code2)
1869         {
1870         case PLUS_EXPR:
1871         case MINUS_EXPR:
1872           if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1873             {
1874               type2 = ptrdiff_type_node;
1875               break;
1876             }
1877         case MULT_EXPR:
1878         case TRUNC_DIV_EXPR:
1879           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1880             break;
1881           return candidates;
1882
1883         case TRUNC_MOD_EXPR:
1884         case BIT_AND_EXPR:
1885         case BIT_IOR_EXPR:
1886         case BIT_XOR_EXPR:
1887         case LSHIFT_EXPR:
1888         case RSHIFT_EXPR:
1889           if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1890             break;
1891           return candidates;
1892
1893         case NOP_EXPR:
1894           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1895             break;
1896           if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1897               || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1898               || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1899               || ((TYPE_PTRMEMFUNC_P (type1)
1900                    || TREE_CODE (type1) == POINTER_TYPE)
1901                   && null_ptr_cst_p (args[1])))
1902             {
1903               type2 = type1;
1904               break;
1905             }
1906           return candidates;
1907
1908         default:
1909           my_friendly_abort (367);
1910         }
1911       type1 = build_reference_type (type1);
1912       break;
1913
1914     case COND_EXPR:
1915       /* [over.builtin]
1916
1917          For every pair of promoted arithmetic types L and R, there
1918          exist candidate operator functions of the form 
1919
1920          LR operator?(bool, L, R); 
1921
1922          where LR is the result of the usual arithmetic conversions
1923          between types L and R.
1924
1925          For every type T, where T is a pointer or pointer-to-member
1926          type, there exist candidate operator functions of the form T
1927          operator?(bool, T, T);  */
1928
1929       if (promoted_arithmetic_type_p (type1)
1930           && promoted_arithmetic_type_p (type2))
1931         /* That's OK.  */
1932         break;
1933
1934       /* Otherwise, the types should be pointers.  */
1935       if (!(TREE_CODE (type1) == POINTER_TYPE
1936             || TYPE_PTRMEM_P (type1)
1937             || TYPE_PTRMEMFUNC_P (type1))
1938           || !(TREE_CODE (type2) == POINTER_TYPE
1939                || TYPE_PTRMEM_P (type2)
1940                || TYPE_PTRMEMFUNC_P (type2)))
1941         return candidates;
1942       
1943       /* We don't check that the two types are the same; the logic
1944          below will actually create two candidates; one in which both
1945          parameter types are TYPE1, and one in which both parameter
1946          types are TYPE2.  */
1947         break;
1948
1949       /* These arguments do not make for a legal overloaded operator.  */
1950       return candidates;
1951
1952     default:
1953       my_friendly_abort (367);
1954     }
1955
1956   /* If we're dealing with two pointer types or two enumeral types,
1957      we need candidates for both of them.  */
1958   if (type2 && !same_type_p (type1, type2)
1959       && TREE_CODE (type1) == TREE_CODE (type2)
1960       && (TREE_CODE (type1) == REFERENCE_TYPE
1961           || (TREE_CODE (type1) == POINTER_TYPE
1962               && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1963           || TYPE_PTRMEMFUNC_P (type1)
1964           || IS_AGGR_TYPE (type1)
1965           || TREE_CODE (type1) == ENUMERAL_TYPE))
1966     {
1967       candidates = build_builtin_candidate
1968         (candidates, fnname, type1, type1, args, argtypes, flags);
1969       return build_builtin_candidate
1970         (candidates, fnname, type2, type2, args, argtypes, flags);
1971     }
1972
1973   return build_builtin_candidate
1974     (candidates, fnname, type1, type2, args, argtypes, flags);
1975 }
1976
1977 tree
1978 type_decays_to (type)
1979      tree type;
1980 {
1981   if (TREE_CODE (type) == ARRAY_TYPE)
1982     return build_pointer_type (TREE_TYPE (type));
1983   if (TREE_CODE (type) == FUNCTION_TYPE)
1984     return build_pointer_type (type);
1985   return type;
1986 }
1987
1988 /* There are three conditions of builtin candidates:
1989
1990    1) bool-taking candidates.  These are the same regardless of the input.
1991    2) pointer-pair taking candidates.  These are generated for each type
1992       one of the input types converts to.
1993    3) arithmetic candidates.  According to the standard, we should generate
1994       all of these, but I'm trying not to...
1995    
1996    Here we generate a superset of the possible candidates for this particular
1997    case.  That is a subset of the full set the standard defines, plus some
1998    other cases which the standard disallows. add_builtin_candidate will
1999    filter out the illegal set.  */
2000
2001 static struct z_candidate *
2002 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2003      struct z_candidate *candidates;
2004      enum tree_code code, code2;
2005      tree fnname, *args;
2006      int flags;
2007 {
2008   int ref1, i;
2009   int enum_p = 0;
2010   tree type, argtypes[3];
2011   /* TYPES[i] is the set of possible builtin-operator parameter types
2012      we will consider for the Ith argument.  These are represented as
2013      a TREE_LIST; the TREE_VALUE of each node is the potential
2014      parameter type.  */
2015   tree types[2];
2016
2017   for (i = 0; i < 3; ++i)
2018     {
2019       if (args[i])
2020         argtypes[i]  = lvalue_type (args[i]);
2021       else
2022         argtypes[i] = NULL_TREE;
2023     }
2024
2025   switch (code)
2026     {
2027 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
2028      and  VQ  is  either  volatile or empty, there exist candidate operator
2029      functions of the form
2030                  VQ T&   operator++(VQ T&);  */
2031
2032     case POSTINCREMENT_EXPR:
2033     case PREINCREMENT_EXPR:
2034     case POSTDECREMENT_EXPR:
2035     case PREDECREMENT_EXPR:
2036     case MODIFY_EXPR:
2037       ref1 = 1;
2038       break;
2039
2040 /* 24There also exist candidate operator functions of the form
2041              bool    operator!(bool);
2042              bool    operator&&(bool, bool);
2043              bool    operator||(bool, bool);  */
2044
2045     case TRUTH_NOT_EXPR:
2046       return build_builtin_candidate
2047         (candidates, fnname, boolean_type_node,
2048          NULL_TREE, args, argtypes, flags);
2049
2050     case TRUTH_ORIF_EXPR:
2051     case TRUTH_ANDIF_EXPR:
2052       return build_builtin_candidate
2053         (candidates, fnname, boolean_type_node,
2054          boolean_type_node, args, argtypes, flags);
2055
2056     case ADDR_EXPR:
2057     case COMPOUND_EXPR:
2058     case COMPONENT_REF:
2059       return candidates;
2060
2061     case COND_EXPR:
2062     case EQ_EXPR:
2063     case NE_EXPR:
2064     case LT_EXPR:
2065     case LE_EXPR:
2066     case GT_EXPR:
2067     case GE_EXPR:
2068       enum_p = 1;
2069       /* FALLTHROUGH */
2070     
2071     default:
2072       ref1 = 0;
2073     }
2074
2075   types[0] = types[1] = NULL_TREE;
2076
2077   for (i = 0; i < 2; ++i)
2078     {
2079       if (! args[i])
2080         ;
2081       else if (IS_AGGR_TYPE (argtypes[i]))
2082         {
2083           tree convs;
2084
2085           if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2086             return candidates;
2087
2088           convs = lookup_conversions (argtypes[i]);
2089
2090           if (code == COND_EXPR)
2091             {
2092               if (real_lvalue_p (args[i]))
2093                 types[i] = tree_cons
2094                   (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2095
2096               types[i] = tree_cons
2097                 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2098             }
2099
2100           else if (! convs)
2101             return candidates;
2102
2103           for (; convs; convs = TREE_CHAIN (convs))
2104             {
2105               type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2106
2107               if (i == 0 && ref1
2108                   && (TREE_CODE (type) != REFERENCE_TYPE
2109                       || CP_TYPE_CONST_P (TREE_TYPE (type))))
2110                 continue;
2111
2112               if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2113                 types[i] = tree_cons (NULL_TREE, type, types[i]);
2114
2115               type = non_reference (type);
2116               if (i != 0 || ! ref1)
2117                 {
2118                   type = TYPE_MAIN_VARIANT (type_decays_to (type));
2119                   if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2120                     types[i] = tree_cons (NULL_TREE, type, types[i]);
2121                   if (INTEGRAL_TYPE_P (type))
2122                     type = type_promotes_to (type);
2123                 }
2124
2125               if (! value_member (type, types[i]))
2126                 types[i] = tree_cons (NULL_TREE, type, types[i]);
2127             }
2128         }
2129       else
2130         {
2131           if (code == COND_EXPR && real_lvalue_p (args[i]))
2132             types[i] = tree_cons
2133               (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2134           type = non_reference (argtypes[i]);
2135           if (i != 0 || ! ref1)
2136             {
2137               type = TYPE_MAIN_VARIANT (type_decays_to (type));
2138               if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2139                 types[i] = tree_cons (NULL_TREE, type, types[i]);
2140               if (INTEGRAL_TYPE_P (type))
2141                 type = type_promotes_to (type);
2142             }
2143           types[i] = tree_cons (NULL_TREE, type, types[i]);
2144         }
2145     }
2146
2147   /* Run through the possible parameter types of both arguments,
2148      creating candidates with those parameter types.  */
2149   for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2150     {
2151       if (types[1])
2152         for (type = types[1]; type; type = TREE_CHAIN (type))
2153           candidates = add_builtin_candidate
2154             (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2155              TREE_VALUE (type), args, argtypes, flags);
2156       else
2157         candidates = add_builtin_candidate
2158           (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2159            NULL_TREE, args, argtypes, flags);
2160     }
2161
2162   return candidates;
2163 }
2164
2165
2166 /* If TMPL can be successfully instantiated as indicated by
2167    EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2168
2169    TMPL is the template.  EXPLICIT_TARGS are any explicit template
2170    arguments.  ARGLIST is the arguments provided at the call-site.
2171    The RETURN_TYPE is the desired type for conversion operators.  If
2172    OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2173    If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2174    add_conv_candidate.  */
2175
2176 static struct z_candidate*
2177 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2178                              arglist, return_type, flags,
2179                              obj, strict)
2180      struct z_candidate *candidates;
2181      tree tmpl, ctype, explicit_targs, arglist, return_type;
2182      int flags;
2183      tree obj;
2184      unification_kind_t strict;
2185 {
2186   int ntparms = DECL_NTPARMS (tmpl);
2187   tree targs = make_tree_vec (ntparms);
2188   tree args_without_in_chrg = arglist;
2189   struct z_candidate *cand;
2190   int i;
2191   tree fn;
2192
2193   /* We don't do deduction on the in-charge parameter, the VTT
2194      parameter or 'this'.  */
2195   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2196     args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2197
2198   if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2199        || DECL_BASE_CONSTRUCTOR_P (tmpl))
2200       && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2201     args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2202
2203   i = fn_type_unification (tmpl, explicit_targs, targs,
2204                            args_without_in_chrg,
2205                            return_type, strict, -1);
2206
2207   if (i != 0)
2208     return candidates;
2209
2210   fn = instantiate_template (tmpl, targs);
2211   if (fn == error_mark_node)
2212     return candidates;
2213
2214   if (obj != NULL_TREE)
2215     /* Aha, this is a conversion function.  */
2216     cand = add_conv_candidate (candidates, fn, obj, arglist);
2217   else
2218     cand = add_function_candidate (candidates, fn, ctype,
2219                                    arglist, flags);
2220   if (DECL_TI_TEMPLATE (fn) != tmpl)
2221     /* This situation can occur if a member template of a template
2222        class is specialized.  Then, instantiate_template might return
2223        an instantiation of the specialization, in which case the
2224        DECL_TI_TEMPLATE field will point at the original
2225        specialization.  For example:
2226
2227          template <class T> struct S { template <class U> void f(U);
2228                                        template <> void f(int) {}; };
2229          S<double> sd;
2230          sd.f(3);
2231
2232        Here, TMPL will be template <class U> S<double>::f(U).
2233        And, instantiate template will give us the specialization
2234        template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
2235        for this will point at template <class T> template <> S<T>::f(int),
2236        so that we can find the definition.  For the purposes of
2237        overload resolution, however, we want the original TMPL.  */
2238     cand->template = tree_cons (tmpl, targs, NULL_TREE);
2239   else
2240     cand->template = DECL_TEMPLATE_INFO (fn);
2241
2242   return cand;
2243 }
2244
2245
2246 static struct z_candidate *
2247 add_template_candidate (candidates, tmpl, ctype, explicit_targs, 
2248                         arglist, return_type, flags, strict)
2249      struct z_candidate *candidates;
2250      tree tmpl, ctype, explicit_targs, arglist, return_type;
2251      int flags;
2252      unification_kind_t strict;
2253 {
2254   return 
2255     add_template_candidate_real (candidates, tmpl, ctype,
2256                                  explicit_targs, arglist, return_type, flags,
2257                                  NULL_TREE, strict);
2258 }
2259
2260
2261 static struct z_candidate *
2262 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2263      struct z_candidate *candidates;
2264      tree tmpl, obj, arglist, return_type;
2265 {
2266   return 
2267     add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2268                                  arglist, return_type, 0, obj, DEDUCE_CONV);
2269 }
2270
2271
2272 static int
2273 any_viable (cands)
2274      struct z_candidate *cands;
2275 {
2276   for (; cands; cands = cands->next)
2277     if (pedantic ? cands->viable == 1 : cands->viable)
2278       return 1;
2279   return 0;
2280 }
2281
2282 static struct z_candidate *
2283 splice_viable (cands)
2284      struct z_candidate *cands;
2285 {
2286   struct z_candidate **p = &cands;
2287
2288   for (; *p; )
2289     {
2290       if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2291         p = &((*p)->next);
2292       else
2293         *p = (*p)->next;
2294     }
2295
2296   return cands;
2297 }
2298
2299 static tree
2300 build_this (obj)
2301      tree obj;
2302 {
2303   /* Fix this to work on non-lvalues.  */
2304   return build_unary_op (ADDR_EXPR, obj, 0);
2305 }
2306
2307 static void
2308 print_z_candidates (candidates)
2309      struct z_candidate *candidates;
2310 {
2311   const char *str = "candidates are:";
2312   for (; candidates; candidates = candidates->next)
2313     {
2314       if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2315         {
2316           if (TREE_VEC_LENGTH (candidates->convs) == 3)
2317             cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2318                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2319                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2320                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2321           else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2322             cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2323                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2324                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2325           else
2326             cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2327                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2328         }
2329       else if (TYPE_P (candidates->fn))
2330         cp_error ("%s %T <conversion>", str, candidates->fn);
2331       else
2332         cp_error_at ("%s %+#D%s", str, candidates->fn,
2333                      candidates->viable == -1 ? " <near match>" : "");
2334       str = "               "; 
2335     }
2336 }
2337
2338 /* Returns the best overload candidate to perform the requested
2339    conversion.  This function is used for three the overloading situations
2340    described in [over.match.copy], [over.match.conv], and [over.match.ref].
2341    If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2342    per [dcl.init.ref], so we ignore temporary bindings.  */
2343
2344 static struct z_candidate *
2345 build_user_type_conversion_1 (totype, expr, flags)
2346      tree totype, expr;
2347      int flags;
2348 {
2349   struct z_candidate *candidates, *cand;
2350   tree fromtype = TREE_TYPE (expr);
2351   tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2352   tree args = NULL_TREE;
2353   tree templates = NULL_TREE;
2354
2355   /* We represent conversion within a hierarchy using RVALUE_CONV and
2356      BASE_CONV, as specified by [over.best.ics]; these become plain
2357      constructor calls, as specified in [dcl.init].  */
2358   if (IS_AGGR_TYPE (fromtype) && IS_AGGR_TYPE (totype)
2359       && DERIVED_FROM_P (totype, fromtype))
2360     abort ();
2361
2362   if (IS_AGGR_TYPE (totype))
2363     ctors = lookup_fnfields (TYPE_BINFO (totype),
2364                              complete_ctor_identifier,
2365                              0);
2366
2367   if (IS_AGGR_TYPE (fromtype))
2368     convs = lookup_conversions (fromtype);
2369
2370   candidates = 0;
2371   flags |= LOOKUP_NO_CONVERSION;
2372
2373   if (ctors)
2374     {
2375       tree t;
2376
2377       ctors = TREE_VALUE (ctors);
2378
2379       t = build_int_2 (0, 0);
2380       TREE_TYPE (t) = build_pointer_type (totype);
2381       args = build_tree_list (NULL_TREE, expr);
2382       if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2383           || DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)))
2384         /* We should never try to call the abstract or base constructor
2385            from here.  */
2386         abort ();
2387       args = tree_cons (NULL_TREE, t, args);
2388     }
2389   for (; ctors; ctors = OVL_NEXT (ctors))
2390     {
2391       tree ctor = OVL_CURRENT (ctors);
2392       if (DECL_NONCONVERTING_P (ctor))
2393         continue;
2394
2395       if (TREE_CODE (ctor) == TEMPLATE_DECL) 
2396         {
2397           templates = tree_cons (NULL_TREE, ctor, templates);
2398           candidates = 
2399             add_template_candidate (candidates, ctor, totype,
2400                                     NULL_TREE, args, NULL_TREE, flags,
2401                                     DEDUCE_CALL);
2402         } 
2403       else 
2404         candidates = add_function_candidate (candidates, ctor, totype,
2405                                              args, flags); 
2406
2407       if (candidates) 
2408         {
2409           candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2410           candidates->basetype_path = TYPE_BINFO (totype);
2411         } 
2412     }
2413
2414   if (convs)
2415     args = build_tree_list (NULL_TREE, build_this (expr));
2416
2417   for (; convs; convs = TREE_CHAIN (convs))
2418     {
2419       tree fns = TREE_VALUE (convs);
2420       int convflags = LOOKUP_NO_CONVERSION;
2421       tree ics;
2422
2423       /* If we are called to convert to a reference type, we are trying to
2424          find an lvalue binding, so don't even consider temporaries.  If
2425          we don't find an lvalue binding, the caller will try again to
2426          look for a temporary binding.  */
2427       if (TREE_CODE (totype) == REFERENCE_TYPE)
2428         convflags |= LOOKUP_NO_TEMP_BIND;
2429
2430       if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2431         ics = implicit_conversion
2432           (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2433       else
2434         /* We can't compute this yet.  */
2435         ics = error_mark_node;
2436
2437       if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2438         /* ignore the near match.  */;
2439       else if (ics)
2440         for (; fns; fns = OVL_NEXT (fns))
2441           {
2442             tree fn = OVL_CURRENT (fns);
2443             struct z_candidate *old_candidates = candidates;
2444
2445             /* [over.match.funcs] For conversion functions, the function is
2446                considered to be a member of the class of the implicit object
2447                argument for the purpose of defining the type of the implicit
2448                object parameter.
2449
2450                So we pass fromtype as CTYPE to add_*_candidate.  */
2451
2452             if (TREE_CODE (fn) == TEMPLATE_DECL)
2453               {
2454                 templates = tree_cons (NULL_TREE, fn, templates);
2455                 candidates = 
2456                   add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2457                                           args, totype, flags,
2458                                           DEDUCE_CONV);
2459               } 
2460             else 
2461               candidates = add_function_candidate (candidates, fn, fromtype,
2462                                                    args, flags); 
2463
2464             if (candidates != old_candidates)
2465               {
2466                 if (TREE_CODE (fn) == TEMPLATE_DECL)
2467                   ics = implicit_conversion
2468                     (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2469                      0, convflags);
2470
2471                 candidates->second_conv = ics;
2472                 candidates->basetype_path = TYPE_BINFO (fromtype);
2473
2474                 if (ics == NULL_TREE)
2475                   candidates->viable = 0;
2476                 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2477                   candidates->viable = -1;
2478               }
2479           }
2480     }
2481
2482   if (! any_viable (candidates))
2483     {
2484 #if 0
2485       if (flags & LOOKUP_COMPLAIN)
2486         {
2487           if (candidates && ! candidates->next)
2488             /* say why this one won't work or try to be loose */;
2489           else
2490             cp_error ("no viable candidates");
2491         }
2492 #endif
2493
2494       return 0;
2495     }
2496
2497   candidates = splice_viable (candidates);
2498   cand = tourney (candidates);
2499
2500   if (cand == 0)
2501     {
2502       if (flags & LOOKUP_COMPLAIN)
2503         {
2504           cp_error ("conversion from `%T' to `%T' is ambiguous",
2505                     fromtype, totype);
2506           print_z_candidates (candidates);
2507         }
2508
2509       cand = candidates;        /* any one will do */
2510       cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2511       ICS_USER_FLAG (cand->second_conv) = 1;
2512       ICS_BAD_FLAG (cand->second_conv) = 1;
2513
2514       return cand;
2515     }
2516
2517   for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2518     p = &(TREE_OPERAND (*p, 0));
2519
2520   *p = build
2521     (USER_CONV,
2522      (DECL_CONSTRUCTOR_P (cand->fn)
2523       ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2524      expr, build_ptr_wrapper (cand));
2525   
2526   ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2527   if (cand->viable == -1)
2528     ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2529
2530   return cand;
2531 }
2532
2533 tree
2534 build_user_type_conversion (totype, expr, flags)
2535      tree totype, expr;
2536      int flags;
2537 {
2538   struct z_candidate *cand
2539     = build_user_type_conversion_1 (totype, expr, flags);
2540
2541   if (cand)
2542     {
2543       if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2544         return error_mark_node;
2545       return convert_from_reference (convert_like (cand->second_conv, expr));
2546     }
2547   return NULL_TREE;
2548 }
2549
2550 /* Do any initial processing on the arguments to a function call.  */
2551
2552 static tree
2553 resolve_args (args)
2554      tree args;
2555 {
2556   tree t;
2557   for (t = args; t; t = TREE_CHAIN (t))
2558     {
2559       tree arg = TREE_VALUE (t);
2560       
2561       if (arg == error_mark_node)
2562         return error_mark_node;
2563       else if (VOID_TYPE_P (TREE_TYPE (arg)))
2564         {
2565           error ("invalid use of void expression");
2566           return error_mark_node;
2567         }
2568       else if (TREE_CODE (arg) == OFFSET_REF)
2569         arg = resolve_offset_ref (arg);
2570       arg = convert_from_reference (arg);
2571       TREE_VALUE (t) = arg;
2572     }
2573   return args;
2574 }
2575       
2576 tree
2577 build_new_function_call (fn, args)
2578      tree fn, args;
2579 {
2580   struct z_candidate *candidates = 0, *cand;
2581   tree explicit_targs = NULL_TREE;
2582   int template_only = 0;
2583
2584   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2585     {
2586       explicit_targs = TREE_OPERAND (fn, 1);
2587       fn = TREE_OPERAND (fn, 0);
2588       template_only = 1;
2589     }
2590
2591   if (really_overloaded_fn (fn))
2592     {
2593       tree t1;
2594       tree templates = NULL_TREE;
2595
2596       args = resolve_args (args);
2597
2598       if (args == error_mark_node)
2599         return error_mark_node;
2600
2601       for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2602         {
2603           tree t = OVL_FUNCTION (t1);
2604
2605           if (TREE_CODE (t) == TEMPLATE_DECL)
2606             {
2607               templates = tree_cons (NULL_TREE, t, templates);
2608               candidates = add_template_candidate
2609                 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2610                  LOOKUP_NORMAL, DEDUCE_CALL);  
2611             }
2612           else if (! template_only)
2613             candidates = add_function_candidate
2614               (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2615         }
2616
2617       if (! any_viable (candidates))
2618         {
2619           if (candidates && ! candidates->next)
2620             return build_function_call (candidates->fn, args);
2621           cp_error ("no matching function for call to `%D(%A)'",
2622                     DECL_NAME (OVL_FUNCTION (fn)), args);
2623           if (candidates)
2624             print_z_candidates (candidates);
2625           return error_mark_node;
2626         }
2627       candidates = splice_viable (candidates);
2628       cand = tourney (candidates);
2629
2630       if (cand == 0)
2631         {
2632           cp_error ("call of overloaded `%D(%A)' is ambiguous",
2633                     DECL_NAME (OVL_FUNCTION (fn)), args);
2634           print_z_candidates (candidates);
2635           return error_mark_node;
2636         }
2637
2638       return build_over_call (cand, args, LOOKUP_NORMAL);
2639     }
2640
2641   /* This is not really overloaded. */
2642   fn = OVL_CURRENT (fn);
2643
2644   return build_function_call (fn, args);
2645 }
2646
2647 static tree
2648 build_object_call (obj, args)
2649      tree obj, args;
2650 {
2651   struct z_candidate *candidates = 0, *cand;
2652   tree fns, convs, mem_args = NULL_TREE;
2653   tree type = TREE_TYPE (obj);
2654
2655   if (TYPE_PTRMEMFUNC_P (type))
2656     {
2657       /* It's no good looking for an overloaded operator() on a
2658          pointer-to-member-function.  */
2659       cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2660       return error_mark_node;
2661     }
2662
2663   fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2664   if (fns == error_mark_node)
2665     return error_mark_node;
2666
2667   args = resolve_args (args);
2668
2669   if (args == error_mark_node)
2670     return error_mark_node;
2671
2672   if (fns)
2673     {
2674       tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2675       mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2676
2677       for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2678         {
2679           tree fn = OVL_CURRENT (fns);
2680           if (TREE_CODE (fn) == TEMPLATE_DECL)
2681             {
2682               candidates 
2683                 = add_template_candidate (candidates, fn, base, NULL_TREE,
2684                                           mem_args, NULL_TREE, 
2685                                           LOOKUP_NORMAL, DEDUCE_CALL);
2686             }
2687           else
2688             candidates = add_function_candidate
2689               (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2690
2691           if (candidates)
2692             candidates->basetype_path = TYPE_BINFO (type);
2693         }
2694     }
2695
2696   convs = lookup_conversions (type);
2697
2698   for (; convs; convs = TREE_CHAIN (convs))
2699     {
2700       tree fns = TREE_VALUE (convs);
2701       tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2702
2703       if ((TREE_CODE (totype) == POINTER_TYPE
2704            && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2705           || (TREE_CODE (totype) == REFERENCE_TYPE
2706               && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2707           || (TREE_CODE (totype) == REFERENCE_TYPE
2708               && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2709               && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2710         for (; fns; fns = OVL_NEXT (fns))
2711           {
2712             tree fn = OVL_CURRENT (fns);
2713             if (TREE_CODE (fn) == TEMPLATE_DECL) 
2714               {
2715                 candidates = add_template_conv_candidate (candidates,
2716                                                           fn,
2717                                                           obj,
2718                                                           args,
2719                                                           totype);
2720               }
2721             else
2722               candidates = add_conv_candidate (candidates, fn, obj, args);
2723           }
2724     }
2725
2726   if (! any_viable (candidates))
2727     {
2728       cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2729       print_z_candidates (candidates);
2730       return error_mark_node;
2731     }
2732
2733   candidates = splice_viable (candidates);
2734   cand = tourney (candidates);
2735
2736   if (cand == 0)
2737     {
2738       cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2739       print_z_candidates (candidates);
2740       return error_mark_node;
2741     }
2742
2743   /* Since cand->fn will be a type, not a function, for a conversion
2744      function, we must be careful not to unconditionally look at
2745      DECL_NAME here.  */
2746   if (TREE_CODE (cand->fn) == FUNCTION_DECL
2747       && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2748     return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2749
2750   obj = convert_like_with_context
2751           (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2752
2753   /* FIXME */
2754   return build_function_call (obj, args);
2755 }
2756
2757 static void
2758 op_error (code, code2, arg1, arg2, arg3, problem)
2759      enum tree_code code, code2;
2760      tree arg1, arg2, arg3;
2761      const char *problem;
2762 {
2763   const char *opname;
2764
2765   if (code == MODIFY_EXPR)
2766     opname = assignment_operator_name_info[code2].name;
2767   else
2768     opname = operator_name_info[code].name;
2769
2770   switch (code)
2771     {
2772     case COND_EXPR:
2773       cp_error ("%s for `%T ? %T : %T' operator", problem,
2774                 error_type (arg1), error_type (arg2), error_type (arg3));
2775       break;
2776     case POSTINCREMENT_EXPR:
2777     case POSTDECREMENT_EXPR:
2778       cp_error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2779       break;
2780     case ARRAY_REF:
2781       cp_error ("%s for `%T [%T]' operator", problem,
2782                 error_type (arg1), error_type (arg2));
2783       break;
2784     default:
2785       if (arg2)
2786         cp_error ("%s for `%T %s %T' operator", problem,
2787                   error_type (arg1), opname, error_type (arg2));
2788       else
2789         cp_error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2790     }
2791 }
2792
2793 /* Return the implicit conversion sequence that could be used to
2794    convert E1 to E2 in [expr.cond].  */
2795
2796 static tree
2797 conditional_conversion (e1, e2)
2798      tree e1;
2799      tree e2;
2800 {
2801   tree t1 = non_reference (TREE_TYPE (e1));
2802   tree t2 = non_reference (TREE_TYPE (e2));
2803   tree conv;
2804
2805   /* [expr.cond]
2806
2807      If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2808      implicitly converted (clause _conv_) to the type "reference to
2809      T2", subject to the constraint that in the conversion the
2810      reference must bind directly (_dcl.init.ref_) to E1.  */
2811   if (real_lvalue_p (e2))
2812     {
2813       conv = implicit_conversion (build_reference_type (t2), 
2814                                   t1,
2815                                   e1,
2816                                   LOOKUP_NO_TEMP_BIND);
2817       if (conv)
2818         return conv;
2819     }
2820
2821   /* [expr.cond]
2822
2823      If E1 and E2 have class type, and the underlying class types are
2824      the same or one is a base class of the other: E1 can be converted
2825      to match E2 if the class of T2 is the same type as, or a base
2826      class of, the class of T1, and the cv-qualification of T2 is the
2827      same cv-qualification as, or a greater cv-qualification than, the
2828      cv-qualification of T1.  If the conversion is applied, E1 is
2829      changed to an rvalue of type T2 that still refers to the original
2830      source class object (or the appropriate subobject thereof).  */
2831   if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2832       && same_or_base_type_p (TYPE_MAIN_VARIANT (t2), 
2833                               TYPE_MAIN_VARIANT (t1)))
2834     {
2835       if (at_least_as_qualified_p (t2, t1))
2836         {
2837           conv = build1 (IDENTITY_CONV, t1, e1);
2838           if (!same_type_p (TYPE_MAIN_VARIANT (t1), 
2839                             TYPE_MAIN_VARIANT (t2)))
2840             conv = build_conv (BASE_CONV, t2, conv);
2841           return conv;
2842         }
2843       else
2844         return NULL_TREE;
2845     }
2846
2847   /* [expr.cond]
2848
2849      E1 can be converted to match E2 if E1 can be implicitly converted
2850      to the type that expression E2 would have if E2 were converted to
2851      an rvalue (or the type it has, if E2 is an rvalue).  */
2852   return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2853 }
2854
2855 /* Implement [expr.cond].  ARG1, ARG2, and ARG3 are the three
2856    arguments to the conditional expression.  By the time this function
2857    is called, any suitable candidate functions are included in
2858    CANDIDATES.  */
2859
2860 tree
2861 build_conditional_expr (arg1, arg2, arg3)
2862      tree arg1;
2863      tree arg2;
2864      tree arg3;
2865 {
2866   tree arg2_type;
2867   tree arg3_type;
2868   tree result;
2869   tree result_type = NULL_TREE;
2870   int lvalue_p = 1;
2871   struct z_candidate *candidates = 0;
2872   struct z_candidate *cand;
2873
2874   /* As a G++ extension, the second argument to the conditional can be
2875      omitted.  (So that `a ? : c' is roughly equivalent to `a ? a :
2876      c'.)  If the second operand is omitted, make sure it is
2877      calculated only once.  */
2878   if (!arg2)
2879     {
2880       if (pedantic)
2881         pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2882       arg1 = arg2 = save_expr (arg1);
2883     }
2884
2885   /* [expr.cond]
2886   
2887      The first expr ession is implicitly converted to bool (clause
2888      _conv_).  */
2889   arg1 = cp_convert (boolean_type_node, arg1);
2890
2891   /* If something has already gone wrong, just pass that fact up the
2892      tree.  */
2893   if (arg1 == error_mark_node 
2894       || arg2 == error_mark_node 
2895       || arg3 == error_mark_node 
2896       || TREE_TYPE (arg1) == error_mark_node
2897       || TREE_TYPE (arg2) == error_mark_node
2898       || TREE_TYPE (arg3) == error_mark_node)
2899     return error_mark_node;
2900
2901   /* [expr.cond]
2902
2903      If either the second or the third operand has type (possibly
2904      cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2905      array-to-pointer (_conv.array_), and function-to-pointer
2906      (_conv.func_) standard conversions are performed on the second
2907      and third operands.  */
2908   arg2_type = TREE_TYPE (arg2);
2909   arg3_type = TREE_TYPE (arg3);
2910   if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2911     {
2912       /* Do the conversions.  We don't these for `void' type arguments
2913          since it can't have any effect and since decay_conversion
2914          does not handle that case gracefully.  */
2915       if (!VOID_TYPE_P (arg2_type))
2916         arg2 = decay_conversion (arg2);
2917       if (!VOID_TYPE_P (arg3_type))
2918         arg3 = decay_conversion (arg3);
2919       arg2_type = TREE_TYPE (arg2);
2920       arg3_type = TREE_TYPE (arg3);
2921
2922       /* [expr.cond]
2923
2924          One of the following shall hold:
2925
2926          --The second or the third operand (but not both) is a
2927            throw-expression (_except.throw_); the result is of the
2928            type of the other and is an rvalue.
2929
2930          --Both the second and the third operands have type void; the
2931            result is of type void and is an rvalue.   */
2932       if ((TREE_CODE (arg2) == THROW_EXPR)
2933           ^ (TREE_CODE (arg3) == THROW_EXPR))
2934         result_type = ((TREE_CODE (arg2) == THROW_EXPR) 
2935                        ? arg3_type : arg2_type);
2936       else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2937         result_type = void_type_node;
2938       else
2939         {
2940           cp_error ("`%E' has type `void' and is not a throw-expression",
2941                     VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2942           return error_mark_node;
2943         }
2944
2945       lvalue_p = 0;
2946       goto valid_operands;
2947     }
2948   /* [expr.cond]
2949
2950      Otherwise, if the second and third operand have different types,
2951      and either has (possibly cv-qualified) class type, an attempt is
2952      made to convert each of those operands to the type of the other.  */
2953   else if (!same_type_p (arg2_type, arg3_type)
2954            && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2955     {
2956       tree conv2 = conditional_conversion (arg2, arg3);
2957       tree conv3 = conditional_conversion (arg3, arg2);
2958       
2959       /* [expr.cond]
2960
2961          If both can be converted, or one can be converted but the
2962          conversion is ambiguous, the program is ill-formed.  If
2963          neither can be converted, the operands are left unchanged and
2964          further checking is performed as described below.  If exactly
2965          one conversion is possible, that conversion is applied to the
2966          chosen operand and the converted operand is used in place of
2967          the original operand for the remainder of this section.  */
2968       if ((conv2 && !ICS_BAD_FLAG (conv2) 
2969            && conv3 && !ICS_BAD_FLAG (conv3))
2970           || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2971           || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2972         {
2973           cp_error ("operands to ?: have different types");
2974           return error_mark_node;
2975         }
2976       else if (conv2 && !ICS_BAD_FLAG (conv2))
2977         {
2978           arg2 = convert_like (conv2, arg2);
2979           arg2 = convert_from_reference (arg2);
2980           /* That may not quite have done the trick.  If the two types
2981              are cv-qualified variants of one another, we will have
2982              just used an IDENTITY_CONV.  (There's no conversion from
2983              an lvalue of one class type to an lvalue of another type,
2984              even a cv-qualified variant, and we don't want to lose
2985              lvalue-ness here.)  So, we manually add a NOP_EXPR here
2986              if necessary.  */
2987           if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2988             arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2989           arg2_type = TREE_TYPE (arg2);
2990         }
2991       else if (conv3 && !ICS_BAD_FLAG (conv3))
2992         {
2993           arg3 = convert_like (conv3, arg3);
2994           arg3 = convert_from_reference (arg3);
2995           if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2996             arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2997           arg3_type = TREE_TYPE (arg3);
2998         }
2999     }
3000
3001   /* [expr.cond]
3002
3003      If the second and third operands are lvalues and have the same
3004      type, the result is of that type and is an lvalue.  */
3005   if (real_lvalue_p (arg2) && real_lvalue_p (arg3) && 
3006       same_type_p (arg2_type, arg3_type))
3007     {
3008       result_type = arg2_type;
3009       goto valid_operands;
3010     }
3011
3012   /* [expr.cond]
3013
3014      Otherwise, the result is an rvalue.  If the second and third
3015      operand do not have the same type, and either has (possibly
3016      cv-qualified) class type, overload resolution is used to
3017      determine the conversions (if any) to be applied to the operands
3018      (_over.match.oper_, _over.built_).  */
3019   lvalue_p = 0;
3020   if (!same_type_p (arg2_type, arg3_type)
3021       && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3022     {
3023       tree args[3];
3024       tree conv;
3025
3026       /* Rearrange the arguments so that add_builtin_candidate only has
3027          to know about two args.  In build_builtin_candidates, the
3028          arguments are unscrambled.  */
3029       args[0] = arg2;
3030       args[1] = arg3;
3031       args[2] = arg1;
3032       candidates = add_builtin_candidates (candidates, 
3033                                            COND_EXPR, 
3034                                            NOP_EXPR,
3035                                            ansi_opname (COND_EXPR),
3036                                            args,
3037                                            LOOKUP_NORMAL);
3038
3039       /* [expr.cond]
3040
3041          If the overload resolution fails, the program is
3042          ill-formed.  */
3043       if (!any_viable (candidates))
3044         {
3045           op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3046           print_z_candidates (candidates);
3047           return error_mark_node;
3048         }
3049       candidates = splice_viable (candidates);
3050       cand = tourney (candidates);
3051       if (!cand)
3052         {
3053           op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3054           print_z_candidates (candidates);
3055           return error_mark_node;
3056         }
3057
3058       /* [expr.cond]
3059
3060          Otherwise, the conversions thus determined are applied, and
3061          the converted operands are used in place of the original
3062          operands for the remainder of this section.  */
3063       conv = TREE_VEC_ELT (cand->convs, 0);
3064       arg1 = convert_like (conv, arg1);
3065       conv = TREE_VEC_ELT (cand->convs, 1);
3066       arg2 = convert_like (conv, arg2);
3067       conv = TREE_VEC_ELT (cand->convs, 2);
3068       arg3 = convert_like (conv, arg3);
3069     }
3070
3071   /* [expr.cond]
3072
3073      Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3074      and function-to-pointer (_conv.func_) standard conversions are
3075      performed on the second and third operands.
3076
3077      We need to force the lvalue-to-rvalue conversion here for class types,
3078      so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3079      that isn't wrapped with a TARGET_EXPR plays havoc with exception
3080      regions.
3081
3082      We use ocp_convert rather than build_user_type_conversion because the
3083      latter returns NULL_TREE on failure, while the former gives an error.  */
3084
3085   if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3086     arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3087                         CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3088   else
3089     arg2 = decay_conversion (arg2);
3090   arg2_type = TREE_TYPE (arg2);
3091
3092   if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3093     arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3094                         CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3095   else
3096     arg3 = decay_conversion (arg3);
3097   arg3_type = TREE_TYPE (arg3);
3098
3099   if (arg2 == error_mark_node || arg3 == error_mark_node)
3100     return error_mark_node;
3101   
3102   /* [expr.cond]
3103      
3104      After those conversions, one of the following shall hold:
3105
3106      --The second and third operands have the same type; the result  is  of
3107        that type.  */
3108   if (same_type_p (arg2_type, arg3_type))
3109     result_type = arg2_type;
3110   /* [expr.cond]
3111
3112      --The second and third operands have arithmetic or enumeration
3113        type; the usual arithmetic conversions are performed to bring
3114        them to a common type, and the result is of that type.  */
3115   else if ((ARITHMETIC_TYPE_P (arg2_type) 
3116             || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3117            && (ARITHMETIC_TYPE_P (arg3_type)
3118                || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3119     {
3120       /* In this case, there is always a common type.  */
3121       result_type = type_after_usual_arithmetic_conversions (arg2_type, 
3122                                                              arg3_type);
3123       
3124       if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3125           && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3126          cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3127                    arg2_type, arg3_type);
3128       else if (extra_warnings
3129                && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3130                     && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3131                    || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3132                        && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3133         cp_warning ("enumeral and non-enumeral type in conditional expression");
3134       
3135       arg2 = perform_implicit_conversion (result_type, arg2);
3136       arg3 = perform_implicit_conversion (result_type, arg3);
3137     }
3138   /* [expr.cond]
3139
3140      --The second and third operands have pointer type, or one has
3141        pointer type and the other is a null pointer constant; pointer
3142        conversions (_conv.ptr_) and qualification conversions
3143        (_conv.qual_) are performed to bring them to their composite
3144        pointer type (_expr.rel_).  The result is of the composite
3145        pointer type.
3146
3147      --The second and third operands have pointer to member type, or
3148        one has pointer to member type and the other is a null pointer
3149        constant; pointer to member conversions (_conv.mem_) and
3150        qualification conversions (_conv.qual_) are performed to bring
3151        them to a common type, whose cv-qualification shall match the
3152        cv-qualification of either the second or the third operand.
3153        The result is of the common type.   */
3154   else if ((null_ptr_cst_p (arg2) 
3155             && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3156                 || TYPE_PTRMEMFUNC_P (arg3_type)))
3157            || (null_ptr_cst_p (arg3) 
3158                && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3159                 || TYPE_PTRMEMFUNC_P (arg2_type)))
3160            || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3161            || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3162            || (TYPE_PTRMEMFUNC_P (arg2_type) 
3163                && TYPE_PTRMEMFUNC_P (arg3_type)))
3164     {
3165       result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3166                                             arg3, "conditional expression");
3167       arg2 = perform_implicit_conversion (result_type, arg2);
3168       arg3 = perform_implicit_conversion (result_type, arg3);
3169     }
3170
3171   if (!result_type)
3172     {
3173       cp_error ("operands to ?: have different types");
3174       return error_mark_node;
3175     }
3176
3177  valid_operands:
3178   result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3179   /* Expand both sides into the same slot, hopefully the target of the
3180      ?: expression.  We used to check for TARGET_EXPRs here, but now we
3181      sometimes wrap them in NOP_EXPRs so the test would fail.  */
3182   if (!lvalue_p && IS_AGGR_TYPE (result_type))
3183     result = build_target_expr_with_type (result, result_type);
3184   
3185   /* If this expression is an rvalue, but might be mistaken for an
3186      lvalue, we must add a NON_LVALUE_EXPR.  */
3187   if (!lvalue_p && real_lvalue_p (result))
3188     result = build1 (NON_LVALUE_EXPR, result_type, result);
3189
3190   return result;
3191 }
3192
3193 tree
3194 build_new_op (code, flags, arg1, arg2, arg3)
3195      enum tree_code code;
3196      int flags;
3197      tree arg1, arg2, arg3;
3198 {
3199   struct z_candidate *candidates = 0, *cand;
3200   tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3201   enum tree_code code2 = NOP_EXPR;
3202   tree templates = NULL_TREE;
3203   tree conv;
3204
3205   if (arg1 == error_mark_node
3206       || arg2 == error_mark_node
3207       || arg3 == error_mark_node)
3208     return error_mark_node;
3209
3210   /* This can happen if a template takes all non-type parameters, e.g.
3211      undeclared_template<1, 5, 72>a;  */
3212   if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3213     {
3214       cp_error ("`%D' must be declared before use", arg1);
3215       return error_mark_node;
3216     }
3217
3218   if (code == MODIFY_EXPR)
3219     {
3220       code2 = TREE_CODE (arg3);
3221       arg3 = NULL_TREE;
3222       fnname = ansi_assopname (code2);
3223     }
3224   else
3225     fnname = ansi_opname (code);
3226
3227   if (TREE_CODE (arg1) == OFFSET_REF)
3228     arg1 = resolve_offset_ref (arg1);
3229   arg1 = convert_from_reference (arg1);
3230   
3231   switch (code)
3232     {
3233     case NEW_EXPR:
3234     case VEC_NEW_EXPR:
3235     case VEC_DELETE_EXPR:
3236     case DELETE_EXPR:
3237       /* Use build_op_new_call and build_op_delete_call instead. */
3238       my_friendly_abort (981018);
3239
3240     case CALL_EXPR:
3241       return build_object_call (arg1, arg2);
3242
3243     default:
3244       break;
3245     }
3246
3247   if (arg2)
3248     {
3249       if (TREE_CODE (arg2) == OFFSET_REF)
3250         arg2 = resolve_offset_ref (arg2);
3251       arg2 = convert_from_reference (arg2);
3252     }
3253   if (arg3)
3254     {
3255       if (TREE_CODE (arg3) == OFFSET_REF)
3256         arg3 = resolve_offset_ref (arg3);
3257       arg3 = convert_from_reference (arg3);
3258     }
3259   
3260   if (code == COND_EXPR)
3261     {
3262       if (arg2 == NULL_TREE
3263           || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3264           || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3265           || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3266               && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3267         goto builtin;
3268     }
3269   else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3270            && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3271     goto builtin;
3272
3273   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3274     arg2 = integer_zero_node;
3275
3276   if (arg2 && arg3)
3277     arglist = tree_cons (NULL_TREE, arg1, tree_cons
3278                       (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3279   else if (arg2)
3280     arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3281   else
3282     arglist = build_tree_list (NULL_TREE, arg1);
3283
3284   fns = lookup_function_nonclass (fnname, arglist);
3285
3286   if (fns && TREE_CODE (fns) == TREE_LIST)
3287     fns = TREE_VALUE (fns);
3288   for (; fns; fns = OVL_NEXT (fns))
3289     {
3290       tree fn = OVL_CURRENT (fns);
3291       if (TREE_CODE (fn) == TEMPLATE_DECL)
3292         {
3293           templates = tree_cons (NULL_TREE, fn, templates);
3294           candidates 
3295             = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3296                                       arglist, TREE_TYPE (fnname),
3297                                       flags, DEDUCE_CALL); 
3298         }
3299       else
3300         candidates = add_function_candidate (candidates, fn, NULL_TREE,
3301                                              arglist, flags);
3302     }
3303
3304   if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3305     {
3306       fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3307       if (fns == error_mark_node)
3308         return fns;
3309     }
3310   else
3311     fns = NULL_TREE;
3312
3313   if (fns)
3314     {
3315       tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3316       mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3317       for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3318         {
3319           tree fn = OVL_CURRENT (fns);
3320           tree this_arglist;
3321
3322           if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3323             this_arglist = mem_arglist;
3324           else
3325             this_arglist = arglist;
3326
3327           if (TREE_CODE (fn) == TEMPLATE_DECL)
3328             {
3329               /* A member template. */
3330               templates = tree_cons (NULL_TREE, fn, templates);
3331               candidates 
3332                 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3333                                           this_arglist,  TREE_TYPE (fnname),
3334                                           flags, DEDUCE_CALL); 
3335             }
3336           else
3337             candidates = add_function_candidate
3338               (candidates, fn, basetype, this_arglist, flags);
3339
3340           if (candidates)
3341             candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3342         }
3343     }
3344
3345   {
3346     tree args[3];
3347
3348     /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3349        to know about two args; a builtin candidate will always have a first
3350        parameter of type bool.  We'll handle that in
3351        build_builtin_candidate.  */
3352     if (code == COND_EXPR)
3353       {
3354         args[0] = arg2;
3355         args[1] = arg3;
3356         args[2] = arg1;
3357       }
3358     else
3359       {
3360         args[0] = arg1;
3361         args[1] = arg2;
3362         args[2] = NULL_TREE;
3363       }
3364
3365     candidates = add_builtin_candidates
3366       (candidates, code, code2, fnname, args, flags);
3367   }
3368
3369   if (! any_viable (candidates))
3370     {
3371       switch (code)
3372         {
3373         case POSTINCREMENT_EXPR:
3374         case POSTDECREMENT_EXPR:
3375           /* Look for an `operator++ (int)'.  If they didn't have
3376              one, then we fall back to the old way of doing things.  */
3377           if (flags & LOOKUP_COMPLAIN)
3378             cp_pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3379                         fnname, 
3380                         operator_name_info[code].name);
3381           if (code == POSTINCREMENT_EXPR)
3382             code = PREINCREMENT_EXPR;
3383           else
3384             code = PREDECREMENT_EXPR;   
3385           return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3386           
3387           /* The caller will deal with these.  */
3388         case ADDR_EXPR:
3389         case COMPOUND_EXPR:
3390         case COMPONENT_REF:
3391           return NULL_TREE;
3392
3393         default:
3394           break;
3395         }
3396       if (flags & LOOKUP_COMPLAIN)
3397         {
3398           op_error (code, code2, arg1, arg2, arg3, "no match");
3399           print_z_candidates (candidates);
3400         }
3401       return error_mark_node;
3402     }
3403   candidates = splice_viable (candidates);
3404   cand = tourney (candidates);
3405
3406   if (cand == 0)
3407     {
3408       if (flags & LOOKUP_COMPLAIN)
3409         {
3410           op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3411           print_z_candidates (candidates);
3412         }
3413       return error_mark_node;
3414     }
3415
3416   if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3417     {
3418       extern int warn_synth;
3419       if (warn_synth
3420           && fnname == ansi_assopname (NOP_EXPR)
3421           && DECL_ARTIFICIAL (cand->fn)
3422           && candidates->next
3423           && ! candidates->next->next)
3424         {
3425           cp_warning ("using synthesized `%#D' for copy assignment",
3426                       cand->fn);
3427           cp_warning_at ("  where cfront would use `%#D'",
3428                          cand == candidates
3429                          ? candidates->next->fn
3430                          : candidates->fn);
3431         }
3432
3433       return build_over_call
3434         (cand,
3435          TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3436          ? mem_arglist : arglist,
3437          LOOKUP_NORMAL);
3438     }
3439
3440   /* Check for comparison of different enum types.  */
3441   switch (code)
3442     {
3443     case GT_EXPR:
3444     case LT_EXPR:
3445     case GE_EXPR:
3446     case LE_EXPR:
3447     case EQ_EXPR:
3448     case NE_EXPR:
3449       if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 
3450           && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 
3451           && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3452               != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3453         {
3454           cp_warning ("comparison between `%#T' and `%#T'", 
3455                       TREE_TYPE (arg1), TREE_TYPE (arg2));
3456         }
3457       break;
3458     default:
3459       break;
3460     }
3461
3462   /* We need to strip any leading REF_BIND so that bitfields don't cause
3463      errors.  This should not remove any important conversions, because
3464      builtins don't apply to class objects directly.  */
3465   conv = TREE_VEC_ELT (cand->convs, 0);
3466   if (TREE_CODE (conv) == REF_BIND)
3467     conv = TREE_OPERAND (conv, 0);
3468   arg1 = convert_like (conv, arg1);
3469   if (arg2)
3470     {
3471       conv = TREE_VEC_ELT (cand->convs, 1);
3472       if (TREE_CODE (conv) == REF_BIND)
3473         conv = TREE_OPERAND (conv, 0);
3474       arg2 = convert_like (conv, arg2);
3475     }
3476   if (arg3)
3477     {
3478       conv = TREE_VEC_ELT (cand->convs, 2);
3479       if (TREE_CODE (conv) == REF_BIND)
3480         conv = TREE_OPERAND (conv, 0);
3481       arg3 = convert_like (conv, arg3);
3482     }
3483
3484 builtin:
3485   switch (code)
3486     {
3487     case MODIFY_EXPR:
3488       return build_modify_expr (arg1, code2, arg2);
3489
3490     case INDIRECT_REF:
3491       return build_indirect_ref (arg1, "unary *");
3492
3493     case PLUS_EXPR:
3494     case MINUS_EXPR:
3495     case MULT_EXPR:
3496     case TRUNC_DIV_EXPR:
3497     case GT_EXPR:
3498     case LT_EXPR:
3499     case GE_EXPR:
3500     case LE_EXPR:
3501     case EQ_EXPR:
3502     case NE_EXPR:
3503     case MAX_EXPR:
3504     case MIN_EXPR:
3505     case LSHIFT_EXPR:
3506     case RSHIFT_EXPR:
3507     case TRUNC_MOD_EXPR:
3508     case BIT_AND_EXPR:
3509     case BIT_IOR_EXPR:
3510     case BIT_XOR_EXPR:
3511     case TRUTH_ANDIF_EXPR:
3512     case TRUTH_ORIF_EXPR:
3513       return cp_build_binary_op (code, arg1, arg2);
3514
3515     case CONVERT_EXPR:
3516     case NEGATE_EXPR:
3517     case BIT_NOT_EXPR:
3518     case TRUTH_NOT_EXPR:
3519     case PREINCREMENT_EXPR:
3520     case POSTINCREMENT_EXPR:
3521     case PREDECREMENT_EXPR:
3522     case POSTDECREMENT_EXPR:
3523     case REALPART_EXPR:
3524     case IMAGPART_EXPR:
3525       return build_unary_op (code, arg1, candidates != 0);
3526
3527     case ARRAY_REF:
3528       return build_array_ref (arg1, arg2);
3529
3530     case COND_EXPR:
3531       return build_conditional_expr (arg1, arg2, arg3);
3532
3533     case MEMBER_REF:
3534       return build_m_component_ref
3535         (build_indirect_ref (arg1, NULL), arg2);
3536
3537       /* The caller will deal with these.  */
3538     case ADDR_EXPR:
3539     case COMPONENT_REF:
3540     case COMPOUND_EXPR:
3541       return NULL_TREE;
3542
3543     default:
3544       my_friendly_abort (367);
3545       return NULL_TREE;
3546     }
3547 }
3548
3549 /* Build a call to operator delete.  This has to be handled very specially,
3550    because the restrictions on what signatures match are different from all
3551    other call instances.  For a normal delete, only a delete taking (void *)
3552    or (void *, size_t) is accepted.  For a placement delete, only an exact
3553    match with the placement new is accepted.
3554
3555    CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3556    ADDR is the pointer to be deleted.  For placement delete, it is also
3557      used to determine what the corresponding new looked like.
3558    SIZE is the size of the memory block to be deleted.
3559    FLAGS are the usual overloading flags.
3560    PLACEMENT is the corresponding placement new call, or NULL_TREE.  */
3561
3562 tree
3563 build_op_delete_call (code, addr, size, flags, placement)
3564      enum tree_code code;
3565      tree addr, size, placement;
3566      int flags;
3567 {
3568   tree fn = NULL_TREE;
3569   tree fns, fnname, fntype, argtypes, args, type;
3570   int pass;
3571
3572   if (addr == error_mark_node)
3573     return error_mark_node;
3574
3575   type = TREE_TYPE (TREE_TYPE (addr));
3576   while (TREE_CODE (type) == ARRAY_TYPE)
3577     type = TREE_TYPE (type);
3578
3579   fnname = ansi_opname (code);
3580
3581   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3582     /* In [class.free]
3583
3584        If the result of the lookup is ambiguous or inaccessible, or if
3585        the lookup selects a placement deallocation function, the
3586        program is ill-formed.
3587   
3588        Therefore, we ask lookup_fnfields to complain ambout ambiguity.  */
3589     {
3590       fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3591       if (fns == error_mark_node)
3592         return error_mark_node;
3593     }
3594   else
3595     fns = NULL_TREE;
3596
3597   if (fns == NULL_TREE)
3598     fns = lookup_name_nonclass (fnname);
3599
3600   if (placement)
3601     {
3602       /* placement is a CALL_EXPR around an ADDR_EXPR around a function.  */
3603
3604       /* Extract the function.  */
3605       argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3606       /* Then the second parm type.  */
3607       argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3608
3609       /* Also the second argument.  */
3610       args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3611     }
3612   else
3613     {
3614       /* First try it without the size argument.  */
3615       argtypes = void_list_node;
3616       args = NULL_TREE;
3617     }
3618
3619   /* Strip const and volatile from addr.  */
3620   addr = cp_convert (ptr_type_node, addr);
3621
3622   /* We make two tries at finding a matching `operator delete'.  On
3623      the first pass, we look for an one-operator (or placement)
3624      operator delete.  If we're not doing placement delete, then on
3625      the second pass we look for a two-argument delete.  */
3626   for (pass = 0; pass < (placement ? 1 : 2); ++pass) 
3627     {
3628       if (pass == 0)
3629         argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3630       else 
3631         /* Normal delete; now try to find a match including the size
3632            argument.  */
3633         argtypes = tree_cons (NULL_TREE, ptr_type_node,
3634                               tree_cons (NULL_TREE, sizetype, 
3635                                          void_list_node));
3636       fntype = build_function_type (void_type_node, argtypes);
3637
3638       /* Go through the `operator delete' functions looking for one
3639          with a matching type.  */
3640       for (fn = BASELINK_P (fns) ? TREE_VALUE (fns) : fns; 
3641            fn; 
3642            fn = OVL_NEXT (fn))
3643         {
3644           tree t;
3645
3646           /* Exception specifications on the `delete' operator do not
3647              matter.  */
3648           t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3649                                        NULL_TREE);
3650           /* We also don't compare attributes.  We're really just
3651              trying to check the types of the first two parameters.  */
3652           if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3653             break;
3654         }
3655
3656       /* If we found a match, we're done.  */
3657       if (fn)
3658         break;
3659     }
3660
3661   /* If we have a matching function, call it.  */
3662   if (fn)
3663     {
3664       /* Make sure we have the actual function, and not an
3665          OVERLOAD.  */
3666       fn = OVL_CURRENT (fn);
3667
3668       /* If the FN is a member function, make sure that it is
3669          accessible.  */
3670       if (DECL_CLASS_SCOPE_P (fn))
3671         enforce_access (type, fn);
3672
3673       if (pass == 0)
3674         args = tree_cons (NULL_TREE, addr, args);
3675       else
3676         args = tree_cons (NULL_TREE, addr, 
3677                           build_tree_list (NULL_TREE, size));
3678
3679       return build_function_call (fn, args);
3680     }
3681
3682   /* If we are doing placement delete we do nothing if we don't find a
3683      matching op delete.  */
3684   if (placement)
3685     return NULL_TREE;
3686
3687   cp_error ("no suitable `operator delete' for `%T'", type);
3688   return error_mark_node;
3689 }
3690
3691 /* If the current scope isn't allowed to access DECL along
3692    BASETYPE_PATH, give an error.  The most derived class in
3693    BASETYPE_PATH is the one used to qualify DECL.  */
3694
3695 int
3696 enforce_access (basetype_path, decl)
3697      tree basetype_path;
3698      tree decl;
3699 {
3700   int accessible;
3701
3702   accessible = accessible_p (basetype_path, decl);
3703   if (!accessible)
3704     {
3705       if (TREE_PRIVATE (decl))
3706         cp_error_at ("`%+#D' is private", decl);
3707       else if (TREE_PROTECTED (decl))
3708         cp_error_at ("`%+#D' is protected", decl);
3709       else
3710         cp_error_at ("`%+#D' is inaccessible", decl);
3711       cp_error ("within this context");
3712       return 0;
3713     }
3714
3715   return 1;
3716 }
3717
3718 /* Perform the conversions in CONVS on the expression EXPR. 
3719    FN and ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
3720    indicates the `this' argument of a method.  INNER is non-zero when
3721    being called to continue a conversion chain. It is negative when a
3722    reference binding will be applied, positive otherwise. */
3723
3724 static tree
3725 convert_like_real (convs, expr, fn, argnum, inner)
3726      tree convs, expr;
3727      tree fn;
3728      int argnum;
3729      int inner;
3730 {
3731   int savew, savee;
3732
3733   tree totype = TREE_TYPE (convs);
3734
3735   if (ICS_BAD_FLAG (convs)
3736       && TREE_CODE (convs) != USER_CONV
3737       && TREE_CODE (convs) != AMBIG_CONV
3738       && TREE_CODE (convs) != REF_BIND)
3739     {
3740       tree t = convs; 
3741       for (; t; t = TREE_OPERAND (t, 0))
3742         {
3743           if (TREE_CODE (t) == USER_CONV)
3744             {
3745               expr = convert_like_real (t, expr, fn, argnum, 1);
3746               break;
3747             }
3748           else if (TREE_CODE (t) == AMBIG_CONV)
3749             return convert_like_real (t, expr, fn, argnum, 1);
3750           else if (TREE_CODE (t) == IDENTITY_CONV)
3751             break;
3752         }
3753       return convert_for_initialization
3754         (NULL_TREE, totype, expr, LOOKUP_NORMAL,
3755          "conversion", fn, argnum);
3756     }
3757   
3758   if (!inner)
3759     expr = dubious_conversion_warnings
3760              (totype, expr, "argument", fn, argnum);
3761   switch (TREE_CODE (convs))
3762     {
3763     case USER_CONV:
3764       {
3765         struct z_candidate *cand
3766           = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3767         tree convfn = cand->fn;
3768         tree args;
3769
3770         if (DECL_CONSTRUCTOR_P (convfn))
3771           {
3772             tree t = build_int_2 (0, 0);
3773             TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3774
3775             args = build_tree_list (NULL_TREE, expr);
3776             if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3777                 || DECL_HAS_VTT_PARM_P (convfn))
3778               /* We should never try to call the abstract or base constructor
3779                  from here.  */
3780               abort ();
3781             args = tree_cons (NULL_TREE, t, args);
3782           }
3783         else
3784           args = build_this (expr);
3785         expr = build_over_call (cand, args, LOOKUP_NORMAL);
3786
3787         /* If this is a constructor or a function returning an aggr type,
3788            we need to build up a TARGET_EXPR.  */
3789         if (DECL_CONSTRUCTOR_P (convfn))
3790           expr = build_cplus_new (totype, expr);
3791
3792         /* The result of the call is then used to direct-initialize the object
3793            that is the destination of the copy-initialization.  [dcl.init]
3794
3795            Note that this step is not reflected in the conversion sequence;
3796            it affects the semantics when we actually perform the
3797            conversion, but is not considered during overload resolution.
3798
3799            If the target is a class, that means call a ctor.  */
3800         if (IS_AGGR_TYPE (totype)
3801             && (inner >= 0 || !real_lvalue_p (expr)))
3802           {
3803             savew = warningcount, savee = errorcount;
3804             expr = build_new_method_call
3805               (NULL_TREE, complete_ctor_identifier,
3806                build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3807                /* Core issue 84, now a DR, says that we don't allow UDCs
3808                   for these args (which deliberately breaks copy-init of an
3809                   auto_ptr<Base> from an auto_ptr<Derived>).  */
3810                LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3811
3812             /* Tell the user where this failing constructor call came from.  */
3813             if (fn)
3814               {
3815                 if (warningcount > savew)
3816                   cp_warning
3817                     ("  initializing argument %P of `%D' from result of `%D'",
3818                      argnum, fn, convfn);
3819                 else if (errorcount > savee)
3820                   cp_error
3821                     ("  initializing argument %P of `%D' from result of `%D'",
3822                      argnum, fn, convfn);
3823               }
3824             else
3825               {
3826                 if (warningcount > savew)
3827                   cp_warning ("  initializing temporary from result of `%D'",
3828                               convfn);
3829                 else if (errorcount > savee)
3830                   cp_error ("  initializing temporary from result of `%D'",
3831                             convfn);
3832               }
3833             expr = build_cplus_new (totype, expr);
3834           }
3835         return expr;
3836       }
3837     case IDENTITY_CONV:
3838       if (type_unknown_p (expr))
3839         expr = instantiate_type (totype, expr, itf_complain);
3840       return expr;
3841     case AMBIG_CONV:
3842       /* Call build_user_type_conversion again for the error.  */
3843       return build_user_type_conversion
3844         (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3845
3846     default:
3847       break;
3848     };
3849
3850   expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3851                             TREE_CODE (convs) == REF_BIND ? -1 : 1);
3852   if (expr == error_mark_node)
3853     return error_mark_node;
3854
3855   /* Convert a non-array constant variable to its underlying value, unless we
3856      are about to bind it to a reference, in which case we need to
3857      leave it as an lvalue.  */
3858   if (TREE_CODE (convs) != REF_BIND
3859       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3860     expr = decl_constant_value (expr);
3861
3862   switch (TREE_CODE (convs))
3863     {
3864     case RVALUE_CONV:
3865       if (! IS_AGGR_TYPE (totype))
3866         return expr;
3867       /* else fall through */
3868     case BASE_CONV:
3869       if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3870         {
3871           /* We are going to bind a reference directly to a base-class
3872              subobject of EXPR.  */
3873           tree base_ptr = build_pointer_type (totype);
3874
3875           /* Build an expression for `*((base*) &expr)'.  */
3876           expr = build_unary_op (ADDR_EXPR, expr, 0);
3877           expr = perform_implicit_conversion (base_ptr, expr);
3878           expr = build_indirect_ref (expr, "implicit conversion");
3879           return expr;
3880         }
3881
3882       /* Copy-initialization where the cv-unqualified version of the source
3883          type is the same class as, or a derived class of, the class of the
3884          destination [is treated as direct-initialization].  [dcl.init] */
3885       savew = warningcount, savee = errorcount;
3886       expr = build_new_method_call (NULL_TREE, complete_ctor_identifier,
3887                                     build_tree_list (NULL_TREE, expr),
3888                                     TYPE_BINFO (totype),
3889                                     LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
3890       if (fn)
3891         {
3892           if (warningcount > savew)
3893             cp_warning ("  initializing argument %P of `%D'", argnum, fn);
3894           else if (errorcount > savee)
3895             cp_error ("  initializing argument %P of `%D'", argnum, fn);
3896         }
3897       return build_cplus_new (totype, expr);
3898
3899     case REF_BIND:
3900       {
3901         tree ref_type = totype;
3902
3903         /* If necessary, create a temporary.  */
3904         if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
3905           {
3906             tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3907             expr = build_target_expr_with_type (expr, type);
3908           }
3909
3910         /* Take the address of the thing to which we will bind the
3911            reference.  */
3912         expr = build_unary_op (ADDR_EXPR, expr, 1);
3913         if (expr == error_mark_node)
3914           return error_mark_node;
3915
3916         /* Convert it to a pointer to the type referred to by the
3917            reference.  This will adjust the pointer if a derived to
3918            base conversion is being performed.  */
3919         expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)), 
3920                            expr);
3921         /* Convert the pointer to the desired reference type.  */
3922         expr = build1 (NOP_EXPR, ref_type, expr);
3923
3924         return expr;
3925       }
3926
3927     case LVALUE_CONV:
3928       return decay_conversion (expr);
3929
3930     case QUAL_CONV:
3931       /* Warn about deprecated conversion if appropriate.  */
3932       string_conv_p (totype, expr, 1);
3933       break;
3934       
3935     default:
3936       break;
3937     }
3938   return ocp_convert (totype, expr, CONV_IMPLICIT,
3939                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3940 }
3941
3942 /* ARG is being passed to a varargs function.  Perform any conversions
3943    required.  Array/function to pointer decay must have already happened.
3944    Return the converted value.  */
3945
3946 tree
3947 convert_arg_to_ellipsis (arg)
3948      tree arg;
3949 {
3950   if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3951       && (TYPE_PRECISION (TREE_TYPE (arg))
3952           < TYPE_PRECISION (double_type_node)))
3953     /* Convert `float' to `double'.  */
3954     arg = cp_convert (double_type_node, arg);
3955   else
3956     /* Convert `short' and `char' to full-size `int'.  */
3957     arg = default_conversion (arg);
3958
3959   arg = require_complete_type (arg);
3960   
3961   if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3962     {
3963       /* Undefined behaviour [expr.call] 5.2.2/7.  */
3964       cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3965                   TREE_TYPE (arg));
3966     }
3967
3968   return arg;
3969 }
3970
3971 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused.  */
3972
3973 tree
3974 build_x_va_arg (expr, type)
3975      tree expr;
3976      tree type;
3977 {
3978   if (processing_template_decl)
3979     return build_min (VA_ARG_EXPR, type, expr);
3980   
3981   type = complete_type_or_else (type, NULL_TREE);
3982
3983   if (expr == error_mark_node || !type)
3984     return error_mark_node;
3985   
3986   if (! pod_type_p (type))
3987     {
3988       /* Undefined behaviour [expr.call] 5.2.2/7.  */
3989       cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3990                   type);
3991     }
3992   
3993   return build_va_arg (expr, type);
3994 }
3995
3996 /* TYPE has been given to va_arg. Apply the default conversions which would
3997    have happened when passed via ellipsis. Return the promoted type, or
3998    NULL_TREE, if there is no change.  */
3999
4000 tree
4001 convert_type_from_ellipsis (type)
4002      tree type;
4003 {
4004   tree promote;
4005   
4006   if (TREE_CODE (type) == ARRAY_TYPE)
4007     promote = build_pointer_type (TREE_TYPE (type));
4008   else if (TREE_CODE (type) == FUNCTION_TYPE)
4009     promote = build_pointer_type (type);
4010   else
4011     promote = type_promotes_to (type);
4012   
4013   return same_type_p (type, promote) ? NULL_TREE : promote;
4014 }
4015
4016 /* ARG is a default argument expression being passed to a parameter of
4017    the indicated TYPE, which is a parameter to FN.  Do any required
4018    conversions.  Return the converted value.  */
4019
4020 tree
4021 convert_default_arg (type, arg, fn, parmnum)
4022      tree type;
4023      tree arg;
4024      tree fn;
4025      int parmnum;
4026 {
4027   if (TREE_CODE (arg) == DEFAULT_ARG)
4028     {
4029       /* When processing the default args for a class, we can find that
4030          there is an ordering constraint, and we call a function who's
4031          default args have not yet been converted. For instance,
4032           class A {
4033               A (int = 0);
4034               void Foo (A const & = A ());
4035           };
4036          We must process A::A before A::Foo's default arg can be converted.
4037          Remember the dependent function, so do_pending_defargs can retry,
4038          and check loops.  */
4039       unprocessed_defarg_fn (fn);
4040       
4041       /* Don't return error_mark node, as we won't be able to distinguish
4042          genuine errors from this case, and that would lead to repeated
4043          diagnostics.  Just make something of the right type.  */
4044       return build1 (NOP_EXPR, type, integer_zero_node);
4045     }
4046
4047   if (fn && DECL_TEMPLATE_INFO (fn))
4048     arg = tsubst_default_argument (fn, type, arg);
4049
4050   arg = break_out_target_exprs (arg);
4051
4052   if (TREE_CODE (arg) == CONSTRUCTOR)
4053     {
4054       arg = digest_init (type, arg, 0);
4055       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4056                                         "default argument", fn, parmnum);
4057     }
4058   else
4059     {
4060       /* This could get clobbered by the following call.  */
4061       if (TREE_HAS_CONSTRUCTOR (arg))
4062         arg = copy_node (arg);
4063
4064       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4065                                         "default argument", fn, parmnum);
4066       if (PROMOTE_PROTOTYPES
4067           && INTEGRAL_TYPE_P (type)
4068           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4069         arg = default_conversion (arg);
4070     }
4071
4072   return arg;
4073 }
4074
4075 /* Subroutine of the various build_*_call functions.  Overload resolution
4076    has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4077    ARGS is a TREE_LIST of the unconverted arguments to the call.  FLAGS is a
4078    bitmask of various LOOKUP_* flags which apply to the call itself.  */
4079
4080 static tree
4081 build_over_call (cand, args, flags)
4082      struct z_candidate *cand;
4083      tree args;
4084      int flags;
4085 {
4086   tree fn = cand->fn;
4087   tree convs = cand->convs;
4088   tree converted_args = NULL_TREE;
4089   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4090   tree conv, arg, val;
4091   int i = 0;
4092   int is_method = 0;
4093
4094   /* Give any warnings we noticed during overload resolution.  */
4095   if (cand->warnings)
4096     for (val = cand->warnings; val; val = TREE_CHAIN (val))
4097       joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
4098
4099   if (DECL_FUNCTION_MEMBER_P (fn))
4100     enforce_access (cand->basetype_path, fn);
4101
4102   if (args && TREE_CODE (args) != TREE_LIST)
4103     args = build_tree_list (NULL_TREE, args);
4104   arg = args;
4105
4106   /* The implicit parameters to a constructor are not considered by overload
4107      resolution, and must be of the proper type.  */
4108   if (DECL_CONSTRUCTOR_P (fn))
4109     {
4110       converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4111       arg = TREE_CHAIN (arg);
4112       parm = TREE_CHAIN (parm);
4113       if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4114         /* We should never try to call the abstract constructor.  */
4115         abort ();
4116       if (DECL_HAS_VTT_PARM_P (fn))
4117         {
4118           converted_args = tree_cons
4119             (NULL_TREE, TREE_VALUE (arg), converted_args);
4120           arg = TREE_CHAIN (arg);
4121           parm = TREE_CHAIN (parm);
4122         }
4123     }      
4124   /* Bypass access control for 'this' parameter.  */
4125   else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4126     {
4127       tree parmtype = TREE_VALUE (parm);
4128       tree argtype = TREE_TYPE (TREE_VALUE (arg));
4129       tree t;
4130       if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4131         cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4132                     TREE_TYPE (argtype), fn);
4133
4134       /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4135          X is called for an object that is not of type X, or of a type
4136          derived from X, the behavior is undefined.
4137
4138          So we can assume that anything passed as 'this' is non-null, and
4139          optimize accordingly.  */
4140       my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4141       t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
4142       converted_args = tree_cons (NULL_TREE, t, converted_args);
4143       parm = TREE_CHAIN (parm);
4144       arg = TREE_CHAIN (arg);
4145       ++i;
4146       is_method = 1;
4147     }
4148
4149   for (; arg && parm;
4150        parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4151     {
4152       tree type = TREE_VALUE (parm);
4153
4154       conv = TREE_VEC_ELT (convs, i);
4155       if (ICS_BAD_FLAG (conv))
4156         {
4157           tree t = conv;
4158           val = TREE_VALUE (arg);
4159
4160           for (; t; t = TREE_OPERAND (t, 0))
4161             {
4162               if (TREE_CODE (t) == USER_CONV
4163                   || TREE_CODE (t) == AMBIG_CONV)
4164                 {
4165                   val = convert_like_with_context (t, val, fn, i - is_method);
4166                   break;
4167                 }
4168               else if (TREE_CODE (t) == IDENTITY_CONV)
4169                 break;
4170             }
4171           val = convert_for_initialization
4172             (NULL_TREE, type, val, LOOKUP_NORMAL,
4173              "argument", fn, i - is_method);
4174         }
4175       else
4176         {
4177           val = TREE_VALUE (arg);
4178           val = convert_like_with_context
4179                   (conv, TREE_VALUE (arg), fn, i - is_method);
4180         }
4181
4182       if (PROMOTE_PROTOTYPES
4183           && INTEGRAL_TYPE_P (type)
4184           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4185         val = default_conversion (val);
4186       converted_args = tree_cons (NULL_TREE, val, converted_args);
4187     }
4188
4189   /* Default arguments */
4190   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4191     converted_args 
4192       = tree_cons (NULL_TREE, 
4193                    convert_default_arg (TREE_VALUE (parm), 
4194                                         TREE_PURPOSE (parm),
4195                                         fn, i - is_method),
4196                    converted_args);
4197
4198   /* Ellipsis */
4199   for (; arg; arg = TREE_CHAIN (arg))
4200     converted_args 
4201       = tree_cons (NULL_TREE,
4202                    convert_arg_to_ellipsis (TREE_VALUE (arg)),
4203                    converted_args);
4204
4205   converted_args = nreverse (converted_args);
4206
4207   if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
4208     check_function_format (NULL, DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
4209                            converted_args); 
4210
4211   /* Avoid actually calling copy constructors and copy assignment operators,
4212      if possible.  */
4213
4214   if (! flag_elide_constructors)
4215     /* Do things the hard way.  */;
4216   else if (TREE_VEC_LENGTH (convs) == 1
4217            && DECL_COPY_CONSTRUCTOR_P (fn))
4218     {
4219       tree targ;
4220       arg = skip_artificial_parms_for (fn, converted_args);
4221       arg = TREE_VALUE (arg);
4222
4223       /* Pull out the real argument, disregarding const-correctness.  */
4224       targ = arg;
4225       while (TREE_CODE (targ) == NOP_EXPR
4226              || TREE_CODE (targ) == NON_LVALUE_EXPR
4227              || TREE_CODE (targ) == CONVERT_EXPR)
4228         targ = TREE_OPERAND (targ, 0);
4229       if (TREE_CODE (targ) == ADDR_EXPR)
4230         {
4231           targ = TREE_OPERAND (targ, 0);
4232           if (!same_type_ignoring_top_level_qualifiers_p 
4233               (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4234             targ = NULL_TREE;
4235         }
4236       else
4237         targ = NULL_TREE;
4238
4239       if (targ)
4240         arg = targ;
4241       else
4242         arg = build_indirect_ref (arg, 0);
4243
4244       /* [class.copy]: the copy constructor is implicitly defined even if
4245          the implementation elided its use.  */
4246       if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4247         mark_used (fn);
4248
4249       /* If we're creating a temp and we already have one, don't create a
4250          new one.  If we're not creating a temp but we get one, use
4251          INIT_EXPR to collapse the temp into our target.  Otherwise, if the
4252          ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4253          temp or an INIT_EXPR otherwise.  */
4254       if (integer_zerop (TREE_VALUE (args)))
4255         {
4256           if (! real_lvalue_p (arg))
4257             return arg;
4258           else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4259             return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4260         }
4261       else if (! real_lvalue_p (arg)
4262                || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4263         {
4264           tree address;
4265           tree to = stabilize_reference
4266             (build_indirect_ref (TREE_VALUE (args), 0));
4267
4268           /* If we're initializing an empty class, then we actually
4269              have to use a MODIFY_EXPR rather than an INIT_EXPR.  The
4270              reason is that the dummy padding member in the target may
4271              not actually be allocated if TO is a base class
4272              subobject.  Since we've set TYPE_NONCOPIED_PARTS on the
4273              padding, a MODIFY_EXPR will preserve its value, which is
4274              the right thing to do if it's not really padding at all.
4275           
4276              It's not safe to just throw away the ARG if we're looking
4277              at an empty class because the ARG might contain a
4278              TARGET_EXPR which wants to be bound to TO.  If it is not,
4279              expand_expr will assign a dummy slot for the TARGET_EXPR,
4280              and we will call a destructor for it, which is wrong,
4281              because we will also destroy TO, but will never have
4282              constructed it.  */
4283           val = build (is_empty_class (DECL_CONTEXT (fn))
4284                        ? MODIFY_EXPR : INIT_EXPR, 
4285                        DECL_CONTEXT (fn), to, arg);
4286           address = build_unary_op (ADDR_EXPR, val, 0);
4287           /* Avoid a warning about this expression, if the address is
4288              never used.  */
4289           TREE_USED (address) = 1;
4290           return address;
4291         }
4292     }
4293   else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4294            && copy_args_p (fn)
4295            && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4296     {
4297       tree to = stabilize_reference
4298         (build_indirect_ref (TREE_VALUE (converted_args), 0));
4299
4300       arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4301
4302       val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4303       return val;
4304     }
4305
4306   mark_used (fn);
4307
4308   if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4309     {
4310       tree t, *p = &TREE_VALUE (converted_args);
4311       tree binfo = get_binfo
4312         (DECL_VIRTUAL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4313       *p = convert_pointer_to_real (binfo, *p);
4314       if (TREE_SIDE_EFFECTS (*p))
4315         *p = save_expr (*p);
4316       t = build_pointer_type (TREE_TYPE (fn));
4317       if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4318         fn = build_java_interface_fn_ref (fn, *p);
4319       else
4320         fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4321       TREE_TYPE (fn) = t;
4322     }
4323   else if (DECL_INLINE (fn))
4324     fn = inline_conversion (fn);
4325   else
4326     fn = build_addr_func (fn);
4327
4328   /* Recognize certain built-in functions so we can make tree-codes
4329      other than CALL_EXPR.  We do this when it enables fold-const.c
4330      to do something useful.  */
4331
4332   if (TREE_CODE (fn) == ADDR_EXPR
4333       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4334       && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4335     {
4336       tree exp;
4337       exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4338       if (exp)
4339         return exp;
4340     }
4341
4342   /* Some built-in function calls will be evaluated at
4343      compile-time in fold ().  */
4344   fn = fold (build_call (fn, converted_args));
4345   if (VOID_TYPE_P (TREE_TYPE (fn)))
4346     return fn;
4347   fn = require_complete_type (fn);
4348   if (fn == error_mark_node)
4349     return error_mark_node;
4350   if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4351     fn = build_cplus_new (TREE_TYPE (fn), fn);
4352   return convert_from_reference (fn);
4353 }
4354
4355 static tree java_iface_lookup_fn;
4356
4357 /* Make an expression which yields the address of the Java interface
4358    method FN.  This is achieved by generating a call to libjava's
4359    _Jv_LookupInterfaceMethodIdx().  */
4360
4361 static tree
4362 build_java_interface_fn_ref (fn, instance)
4363     tree fn, instance;
4364 {
4365   tree lookup_args, lookup_fn, method, idx;
4366   tree klass_ref, iface, iface_ref;
4367   int i;
4368   
4369   if (!java_iface_lookup_fn)
4370     {
4371       tree endlink = build_void_list_node ();
4372       tree t = tree_cons (NULL_TREE, ptr_type_node,
4373                           tree_cons (NULL_TREE, ptr_type_node,
4374                                      tree_cons (NULL_TREE, java_int_type_node,
4375                                                 endlink)));
4376       java_iface_lookup_fn 
4377         = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4378                             build_function_type (ptr_type_node, t),
4379                             0, NOT_BUILT_IN, NULL);
4380       ggc_add_tree_root (&java_iface_lookup_fn, 1);
4381     }
4382
4383   /* Look up the pointer to the runtime java.lang.Class object for `instance'. 
4384      This is the first entry in the vtable. */
4385   klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0), 
4386                               integer_zero_node);
4387
4388   /* Get the java.lang.Class pointer for the interface being called. */
4389   iface = DECL_CONTEXT (fn);
4390   iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4391   if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4392       || DECL_CONTEXT (iface_ref) != iface)
4393     {
4394       cp_error ("Could not find class$ field in java interface type `%T'", 
4395                 iface);
4396       return error_mark_node;
4397     }
4398   iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4399   
4400   /* Determine the itable index of FN. */
4401   i = 1;
4402   for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4403     {
4404       if (!DECL_VIRTUAL_P (method))
4405         continue;
4406       if (fn == method)
4407         break;
4408       i++;
4409     }
4410   idx = build_int_2 (i, 0);
4411
4412   lookup_args = tree_cons (NULL_TREE, klass_ref, 
4413                            tree_cons (NULL_TREE, iface_ref,
4414                                       build_tree_list (NULL_TREE, idx)));
4415   lookup_fn = build1 (ADDR_EXPR, 
4416                       build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4417                       java_iface_lookup_fn);
4418   return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4419 }
4420
4421 /* Returns the value to use for the in-charge parameter when making a
4422    call to a function with the indicated NAME.  */
4423
4424 tree
4425 in_charge_arg_for_name (name)
4426      tree name;
4427 {
4428   if (name == base_ctor_identifier
4429       || name == base_dtor_identifier)
4430     return integer_zero_node;
4431   else if (name == complete_ctor_identifier)
4432     return integer_one_node;
4433   else if (name == complete_dtor_identifier)
4434     return integer_two_node;
4435   else if (name == deleting_dtor_identifier)
4436     return integer_three_node;
4437
4438   /* This function should only be called with one of the names listed
4439      above.  */
4440   my_friendly_abort (20000411);
4441   return NULL_TREE;
4442 }
4443
4444 static tree
4445 build_new_method_call (instance, name, args, basetype_path, flags)
4446      tree instance, name, args, basetype_path;
4447      int flags;
4448 {
4449   struct z_candidate *candidates = 0, *cand;
4450   tree explicit_targs = NULL_TREE;
4451   tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4452   tree pretty_name;
4453   tree user_args;
4454   tree templates = NULL_TREE;
4455   tree call;
4456   int template_only = 0;
4457
4458   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4459     {
4460       explicit_targs = TREE_OPERAND (name, 1);
4461       name = TREE_OPERAND (name, 0);
4462       if (DECL_P (name))
4463         name = DECL_NAME (name);
4464       else
4465         {
4466           if (TREE_CODE (name) == COMPONENT_REF)
4467             name = TREE_OPERAND (name, 1);
4468           if (TREE_CODE (name) == OVERLOAD)
4469             name = DECL_NAME (OVL_CURRENT (name));
4470         }
4471
4472       template_only = 1;
4473     }
4474
4475   user_args = args;
4476   args = resolve_args (args);
4477
4478   if (args == error_mark_node)
4479     return error_mark_node;
4480
4481   if (instance == NULL_TREE)
4482     basetype = BINFO_TYPE (basetype_path);
4483   else
4484     {
4485       if (TREE_CODE (instance) == OFFSET_REF)
4486         instance = resolve_offset_ref (instance);
4487       if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4488         instance = convert_from_reference (instance);
4489       basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4490
4491       /* XXX this should be handled before we get here.  */
4492       if (! IS_AGGR_TYPE (basetype))
4493         {
4494           if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4495             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4496                       name, instance, basetype);
4497
4498           return error_mark_node;
4499         }
4500     }
4501
4502   if (basetype_path == NULL_TREE)
4503     basetype_path = TYPE_BINFO (basetype);
4504
4505   if (instance)
4506     {
4507       instance_ptr = build_this (instance);
4508
4509       if (! template_only)
4510         {
4511           /* XXX this should be handled before we get here.  */
4512           fns = build_field_call (basetype_path, instance_ptr, name, args);
4513           if (fns)
4514             return fns;
4515         }
4516     }
4517   else
4518     {
4519       instance_ptr = build_int_2 (0, 0);
4520       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4521     }
4522
4523   /* Callers should explicitly indicate whether they want to construct
4524      the complete object or just the part without virtual bases.  */
4525   my_friendly_assert (name != ctor_identifier, 20000408);
4526   /* Similarly for destructors.  */
4527   my_friendly_assert (name != dtor_identifier, 20000408);
4528
4529   if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4530     {
4531       int constructor_p;
4532
4533       constructor_p = (name == complete_ctor_identifier
4534                        || name == base_ctor_identifier);
4535       pretty_name = (constructor_p 
4536                      ? constructor_name (basetype) : dtor_identifier);
4537
4538       /* If we're a call to a constructor or destructor for a
4539          subobject that uses virtual base classes, then we need to
4540          pass down a pointer to a VTT for the subobject.  */
4541       if ((name == base_ctor_identifier
4542            || name == base_dtor_identifier)
4543           && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4544         {
4545           tree vtt;
4546           tree sub_vtt;
4547           tree basebinfo = basetype_path;
4548
4549           /* If the current function is a complete object constructor
4550              or destructor, then we fetch the VTT directly.
4551              Otherwise, we look it up using the VTT we were given.  */
4552           vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4553           vtt = decay_conversion (vtt);
4554           vtt = build (COND_EXPR, TREE_TYPE (vtt),
4555                        build (EQ_EXPR, boolean_type_node,
4556                               current_in_charge_parm, integer_zero_node),
4557                        current_vtt_parm,
4558                        vtt);
4559           if (TREE_VIA_VIRTUAL (basebinfo))
4560             basebinfo = binfo_for_vbase (basetype, current_class_type);
4561           my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4562           sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4563                            BINFO_SUBVTT_INDEX (basebinfo));
4564
4565           args = tree_cons (NULL_TREE, sub_vtt, args);
4566         }
4567     }
4568   else
4569     pretty_name = name;
4570
4571   fns = lookup_fnfields (basetype_path, name, 1);
4572
4573   if (fns == error_mark_node)
4574     return error_mark_node;
4575   if (fns)
4576     {
4577       tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4578       tree fn = TREE_VALUE (fns);
4579       mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4580       for (; fn; fn = OVL_NEXT (fn))
4581         {
4582           tree t = OVL_CURRENT (fn);
4583           tree this_arglist;
4584
4585           /* We can end up here for copy-init of same or base class.  */
4586           if ((flags & LOOKUP_ONLYCONVERTING)
4587               && DECL_NONCONVERTING_P (t))
4588             continue;
4589
4590           if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4591             this_arglist = mem_args;
4592           else
4593             this_arglist = args;
4594
4595           if (TREE_CODE (t) == TEMPLATE_DECL)
4596             {
4597               /* A member template. */
4598               templates = tree_cons (NULL_TREE, t, templates);
4599               candidates = 
4600                 add_template_candidate (candidates, t, base, explicit_targs,
4601                                         this_arglist,
4602                                         TREE_TYPE (name), flags, DEDUCE_CALL); 
4603             }
4604           else if (! template_only)
4605             candidates = add_function_candidate (candidates, t, base,
4606                                                  this_arglist, flags);
4607
4608           if (candidates)
4609             candidates->basetype_path = basetype_path;
4610         }
4611     }
4612
4613   if (! any_viable (candidates))
4614     {
4615       /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
4616       if (flags & LOOKUP_SPECULATIVELY)
4617         return NULL_TREE;
4618       if (!COMPLETE_TYPE_P (basetype))
4619         incomplete_type_error (instance_ptr, basetype);
4620       else
4621         cp_error ("no matching function for call to `%T::%D(%A)%V'",
4622                   basetype, pretty_name, user_args,
4623                   TREE_TYPE (TREE_TYPE (instance_ptr)));
4624       print_z_candidates (candidates);
4625       return error_mark_node;
4626     }
4627   candidates = splice_viable (candidates);
4628   cand = tourney (candidates);
4629
4630   if (cand == 0)
4631     {
4632       cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4633                 user_args);
4634       print_z_candidates (candidates);
4635       return error_mark_node;
4636     }
4637
4638   if (DECL_PURE_VIRTUAL_P (cand->fn)
4639       && instance == current_class_ref
4640       && (DECL_CONSTRUCTOR_P (current_function_decl)
4641           || DECL_DESTRUCTOR_P (current_function_decl))
4642       && ! (flags & LOOKUP_NONVIRTUAL)
4643       && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4644     cp_error ((DECL_CONSTRUCTOR_P (current_function_decl) ? 
4645                "abstract virtual `%#D' called from constructor"
4646                : "abstract virtual `%#D' called from destructor"),
4647               cand->fn);
4648   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4649       && is_dummy_object (instance_ptr))
4650     {
4651       cp_error ("cannot call member function `%D' without object", cand->fn);
4652       return error_mark_node;
4653     }
4654
4655   if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4656       && ((instance == current_class_ref && (dtor_label || ctor_label))
4657           || resolves_to_fixed_type_p (instance, 0)))
4658     flags |= LOOKUP_NONVIRTUAL;
4659
4660   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4661     call = build_over_call (cand, mem_args, flags);
4662   else
4663     {
4664       call = build_over_call (cand, args, flags);
4665       /* Do evaluate the object parameter in a call to a static member
4666          function.  */
4667       if (TREE_SIDE_EFFECTS (instance))
4668         call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4669     }
4670
4671   return call;
4672 }
4673
4674 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4675    subsequence of ICS2.  */
4676
4677 static int
4678 is_subseq (ics1, ics2)
4679      tree ics1, ics2;
4680 {
4681   /* We can assume that a conversion of the same code
4682      between the same types indicates a subsequence since we only get
4683      here if the types we are converting from are the same.  */
4684
4685   while (TREE_CODE (ics1) == RVALUE_CONV
4686          || TREE_CODE (ics1) == LVALUE_CONV)
4687     ics1 = TREE_OPERAND (ics1, 0);
4688
4689   while (1)
4690     {
4691       while (TREE_CODE (ics2) == RVALUE_CONV
4692           || TREE_CODE (ics2) == LVALUE_CONV)
4693         ics2 = TREE_OPERAND (ics2, 0);
4694
4695       if (TREE_CODE (ics2) == USER_CONV
4696           || TREE_CODE (ics2) == AMBIG_CONV
4697           || TREE_CODE (ics2) == IDENTITY_CONV)
4698         /* At this point, ICS1 cannot be a proper subsequence of
4699            ICS2.  We can get a USER_CONV when we are comparing the
4700            second standard conversion sequence of two user conversion
4701            sequences.  */
4702         return 0;
4703
4704       ics2 = TREE_OPERAND (ics2, 0);
4705
4706       if (TREE_CODE (ics2) == TREE_CODE (ics1)
4707           && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4708           && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4709                              TREE_TYPE (TREE_OPERAND (ics1, 0))))
4710         return 1;
4711     }
4712 }
4713
4714 /* Returns non-zero iff DERIVED is derived from BASE.  The inputs may
4715    be any _TYPE nodes.  */
4716
4717 int
4718 is_properly_derived_from (derived, base)
4719      tree derived;
4720      tree base;
4721 {
4722   if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4723       || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4724     return 0;
4725
4726   /* We only allow proper derivation here.  The DERIVED_FROM_P macro
4727      considers every class derived from itself.  */
4728   return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4729           && DERIVED_FROM_P (base, derived));
4730 }
4731
4732 /* We build the ICS for an implicit object parameter as a pointer
4733    conversion sequence.  However, such a sequence should be compared
4734    as if it were a reference conversion sequence.  If ICS is the
4735    implicit conversion sequence for an implicit object parameter,
4736    modify it accordingly.  */
4737
4738 static void
4739 maybe_handle_implicit_object (ics)
4740      tree* ics;
4741 {
4742   if (ICS_THIS_FLAG (*ics))
4743     {
4744       /* [over.match.funcs]
4745          
4746          For non-static member functions, the type of the
4747          implicit object parameter is "reference to cv X"
4748          where X is the class of which the function is a
4749          member and cv is the cv-qualification on the member
4750          function declaration.  */
4751       tree t = *ics;
4752       tree reference_type;
4753
4754       /* The `this' parameter is a pointer to a class type.  Make the
4755          implict conversion talk about a reference to that same class
4756          type.  */
4757       reference_type = TREE_TYPE (TREE_TYPE (*ics));
4758       reference_type = build_reference_type (reference_type);
4759
4760       if (TREE_CODE (t) == QUAL_CONV)
4761         t = TREE_OPERAND (t, 0);
4762       if (TREE_CODE (t) == PTR_CONV)
4763         t = TREE_OPERAND (t, 0);
4764       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4765       t = direct_reference_binding (reference_type, t); 
4766       *ics = t;
4767     }
4768 }
4769
4770 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4771    to the type the reference originally referred to, and return 1.
4772    Otherwise, return 0.  */
4773
4774 static int
4775 maybe_handle_ref_bind (ics, target_type)
4776      tree* ics;
4777      tree* target_type;
4778 {
4779   if (TREE_CODE (*ics) == REF_BIND)
4780     {
4781       tree old_ics = *ics;
4782       *target_type = TREE_TYPE (TREE_TYPE (*ics));
4783       *ics = TREE_OPERAND (*ics, 0);
4784       ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4785       ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4786       return 1;
4787     }
4788
4789   return 0;
4790 }
4791
4792 /* Compare two implicit conversion sequences according to the rules set out in
4793    [over.ics.rank].  Return values:
4794
4795       1: ics1 is better than ics2
4796      -1: ics2 is better than ics1
4797       0: ics1 and ics2 are indistinguishable */
4798
4799 static int
4800 compare_ics (ics1, ics2)
4801      tree ics1, ics2;
4802 {
4803   tree from_type1;
4804   tree from_type2;
4805   tree to_type1;
4806   tree to_type2;
4807   tree deref_from_type1 = NULL_TREE;
4808   tree deref_from_type2 = NULL_TREE;
4809   tree deref_to_type1 = NULL_TREE;
4810   tree deref_to_type2 = NULL_TREE;
4811   int rank1, rank2;
4812
4813   /* REF_BINDING is non-zero if the result of the conversion sequence
4814      is a reference type.   In that case TARGET_TYPE is the
4815      type referred to by the reference.  */
4816   int ref_binding1;
4817   int ref_binding2;
4818   tree target_type1;
4819   tree target_type2;
4820
4821   /* Handle implicit object parameters.  */
4822   maybe_handle_implicit_object (&ics1);
4823   maybe_handle_implicit_object (&ics2);
4824
4825   /* Handle reference parameters.  */
4826   ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4827   ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4828
4829   /* [over.ics.rank]
4830
4831      When  comparing  the  basic forms of implicit conversion sequences (as
4832      defined in _over.best.ics_)
4833
4834      --a standard conversion sequence (_over.ics.scs_) is a better
4835        conversion sequence than a user-defined conversion sequence
4836        or an ellipsis conversion sequence, and
4837      
4838      --a user-defined conversion sequence (_over.ics.user_) is a
4839        better conversion sequence than an ellipsis conversion sequence
4840        (_over.ics.ellipsis_).  */
4841   rank1 = ICS_RANK (ics1);
4842   rank2 = ICS_RANK (ics2);
4843   
4844   if (rank1 > rank2)
4845     return -1;
4846   else if (rank1 < rank2)
4847     return 1;
4848
4849   if (rank1 == BAD_RANK)
4850     {
4851       /* XXX Isn't this an extension? */
4852       /* Both ICS are bad.  We try to make a decision based on what
4853          would have happenned if they'd been good.  */
4854       if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4855           || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4856         return -1;
4857       else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4858                || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4859         return 1;
4860
4861       /* We couldn't make up our minds; try to figure it out below.  */
4862     }
4863
4864   if (ICS_ELLIPSIS_FLAG (ics1))
4865     /* Both conversions are ellipsis conversions.  */
4866     return 0;
4867
4868   /* User-defined  conversion sequence U1 is a better conversion sequence
4869      than another user-defined conversion sequence U2 if they contain the
4870      same user-defined conversion operator or constructor and if the sec-
4871      ond standard conversion sequence of U1 is  better  than  the  second
4872      standard conversion sequence of U2.  */
4873
4874   if (ICS_USER_FLAG (ics1))
4875     {
4876       tree t1, t2;
4877
4878       for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4879         if (TREE_CODE (t1) == AMBIG_CONV)
4880           return 0;
4881       for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4882         if (TREE_CODE (t2) == AMBIG_CONV)
4883           return 0;
4884
4885       if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4886         return 0;
4887
4888       /* We can just fall through here, after setting up
4889          FROM_TYPE1 and FROM_TYPE2.  */
4890       from_type1 = TREE_TYPE (t1);
4891       from_type2 = TREE_TYPE (t2);
4892     }
4893   else
4894     {
4895       /* We're dealing with two standard conversion sequences. 
4896
4897          [over.ics.rank]
4898          
4899          Standard conversion sequence S1 is a better conversion
4900          sequence than standard conversion sequence S2 if
4901      
4902          --S1 is a proper subsequence of S2 (comparing the conversion
4903            sequences in the canonical form defined by _over.ics.scs_,
4904            excluding any Lvalue Transformation; the identity
4905            conversion sequence is considered to be a subsequence of
4906            any non-identity conversion sequence */
4907       
4908       from_type1 = ics1;
4909       while (TREE_CODE (from_type1) != IDENTITY_CONV)
4910         from_type1 = TREE_OPERAND (from_type1, 0);
4911       from_type1 = TREE_TYPE (from_type1);
4912       
4913       from_type2 = ics2;
4914       while (TREE_CODE (from_type2) != IDENTITY_CONV)
4915         from_type2 = TREE_OPERAND (from_type2, 0);
4916       from_type2 = TREE_TYPE (from_type2);
4917     }
4918
4919   if (same_type_p (from_type1, from_type2))
4920     {
4921       if (is_subseq (ics1, ics2))
4922         return 1;
4923       if (is_subseq (ics2, ics1))
4924         return -1;
4925     }
4926   /* Otherwise, one sequence cannot be a subsequence of the other; they
4927      don't start with the same type.  This can happen when comparing the
4928      second standard conversion sequence in two user-defined conversion
4929      sequences.  */
4930
4931   /* [over.ics.rank]
4932
4933      Or, if not that,
4934
4935      --the rank of S1 is better than the rank of S2 (by the rules
4936        defined below):
4937
4938     Standard conversion sequences are ordered by their ranks: an Exact
4939     Match is a better conversion than a Promotion, which is a better
4940     conversion than a Conversion.
4941
4942     Two conversion sequences with the same rank are indistinguishable
4943     unless one of the following rules applies:
4944
4945     --A conversion that is not a conversion of a pointer, or pointer
4946       to member, to bool is better than another conversion that is such
4947       a conversion.  
4948
4949     The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4950     so that we do not have to check it explicitly.  */
4951   if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4952     return 1;
4953   else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4954     return -1;
4955
4956   to_type1 = TREE_TYPE (ics1);
4957   to_type2 = TREE_TYPE (ics2);
4958
4959   if (TYPE_PTR_P (from_type1)
4960       && TYPE_PTR_P (from_type2)
4961       && TYPE_PTR_P (to_type1)
4962       && TYPE_PTR_P (to_type2))
4963     {
4964       deref_from_type1 = TREE_TYPE (from_type1);
4965       deref_from_type2 = TREE_TYPE (from_type2);
4966       deref_to_type1 = TREE_TYPE (to_type1);
4967       deref_to_type2 = TREE_TYPE (to_type2);
4968     }
4969   /* The rules for pointers to members A::* are just like the rules
4970      for pointers A*, except opposite: if B is derived from A then
4971      A::* converts to B::*, not vice versa.  For that reason, we
4972      switch the from_ and to_ variables here.  */
4973   else if (TYPE_PTRMEM_P (from_type1)
4974            && TYPE_PTRMEM_P (from_type2)
4975            && TYPE_PTRMEM_P (to_type1)
4976            && TYPE_PTRMEM_P (to_type2))
4977     {
4978       deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4979       deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4980       deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4981       deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4982     }
4983   else if (TYPE_PTRMEMFUNC_P (from_type1)
4984            && TYPE_PTRMEMFUNC_P (from_type2)
4985            && TYPE_PTRMEMFUNC_P (to_type1)
4986            && TYPE_PTRMEMFUNC_P (to_type2))
4987     {
4988       deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4989       deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4990       deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4991       deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4992     }
4993
4994   if (deref_from_type1 != NULL_TREE
4995       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4996       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4997     {
4998       /* This was one of the pointer or pointer-like conversions.  
4999
5000          [over.ics.rank]
5001          
5002          --If class B is derived directly or indirectly from class A,
5003            conversion of B* to A* is better than conversion of B* to
5004            void*, and conversion of A* to void* is better than
5005            conversion of B* to void*.  */
5006       if (TREE_CODE (deref_to_type1) == VOID_TYPE
5007           && TREE_CODE (deref_to_type2) == VOID_TYPE)
5008         {
5009           if (is_properly_derived_from (deref_from_type1,
5010                                         deref_from_type2))
5011             return -1;
5012           else if (is_properly_derived_from (deref_from_type2,
5013                                              deref_from_type1))
5014             return 1;
5015         }
5016       else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5017                || TREE_CODE (deref_to_type2) == VOID_TYPE)
5018         {
5019           if (same_type_p (deref_from_type1, deref_from_type2))
5020             {
5021               if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5022                 {
5023                   if (is_properly_derived_from (deref_from_type1,
5024                                                 deref_to_type1))
5025                     return 1;
5026                 }
5027               /* We know that DEREF_TO_TYPE1 is `void' here.  */
5028               else if (is_properly_derived_from (deref_from_type1,
5029                                                  deref_to_type2))
5030                 return -1;
5031             }
5032         }
5033       else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5034                && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5035         {
5036           /* [over.ics.rank]
5037
5038              --If class B is derived directly or indirectly from class A
5039                and class C is derived directly or indirectly from B,
5040              
5041              --conversion of C* to B* is better than conversion of C* to
5042                A*, 
5043              
5044              --conversion of B* to A* is better than conversion of C* to
5045                A*  */
5046           if (same_type_p (deref_from_type1, deref_from_type2))
5047             {
5048               if (is_properly_derived_from (deref_to_type1,
5049                                             deref_to_type2))
5050                 return 1;
5051               else if (is_properly_derived_from (deref_to_type2,
5052                                                  deref_to_type1))
5053                 return -1;
5054             }
5055           else if (same_type_p (deref_to_type1, deref_to_type2))
5056             {
5057               if (is_properly_derived_from (deref_from_type2,
5058                                             deref_from_type1))
5059                 return 1;
5060               else if (is_properly_derived_from (deref_from_type1,
5061                                                  deref_from_type2))
5062                 return -1;
5063             }
5064         }
5065     }
5066   else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
5067            && same_type_p (from_type1, from_type2))
5068     {
5069       /* [over.ics.rank]
5070          
5071          --binding of an expression of type C to a reference of type
5072            B& is better than binding an expression of type C to a
5073            reference of type A&
5074
5075          --conversion of C to B is better than conversion of C to A,  */
5076       if (is_properly_derived_from (from_type1, to_type1)
5077           && is_properly_derived_from (from_type1, to_type2))
5078         {
5079           if (is_properly_derived_from (to_type1, to_type2))
5080             return 1;
5081           else if (is_properly_derived_from (to_type2, to_type1))
5082             return -1;
5083         }
5084     }
5085   else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
5086            && same_type_p (to_type1, to_type2))
5087     {
5088       /* [over.ics.rank]
5089
5090          --binding of an expression of type B to a reference of type
5091            A& is better than binding an expression of type C to a
5092            reference of type A&, 
5093
5094          --onversion of B to A is better than conversion of C to A  */
5095       if (is_properly_derived_from (from_type1, to_type1)
5096           && is_properly_derived_from (from_type2, to_type1))
5097         {
5098           if (is_properly_derived_from (from_type2, from_type1))
5099             return 1;
5100           else if (is_properly_derived_from (from_type1, from_type2))
5101             return -1;
5102         }
5103     }
5104
5105   /* [over.ics.rank]
5106
5107      --S1 and S2 differ only in their qualification conversion and  yield
5108        similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
5109        qualification signature of type T1 is a proper subset of  the  cv-
5110        qualification signature of type T2  */
5111   if (TREE_CODE (ics1) == QUAL_CONV 
5112       && TREE_CODE (ics2) == QUAL_CONV
5113       && same_type_p (from_type1, from_type2))
5114     return comp_cv_qual_signature (to_type1, to_type2);
5115
5116   /* [over.ics.rank]
5117      
5118      --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5119      types to which the references refer are the same type except for
5120      top-level cv-qualifiers, and the type to which the reference
5121      initialized by S2 refers is more cv-qualified than the type to
5122      which the reference initialized by S1 refers */
5123       
5124   if (ref_binding1 && ref_binding2
5125       && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5126     return comp_cv_qualification (target_type2, target_type1);
5127
5128   /* Neither conversion sequence is better than the other.  */
5129   return 0;
5130 }
5131
5132 /* The source type for this standard conversion sequence.  */
5133
5134 static tree
5135 source_type (t)
5136      tree t;
5137 {
5138   for (;; t = TREE_OPERAND (t, 0))
5139     {
5140       if (TREE_CODE (t) == USER_CONV
5141           || TREE_CODE (t) == AMBIG_CONV
5142           || TREE_CODE (t) == IDENTITY_CONV)
5143         return TREE_TYPE (t);
5144     }
5145   my_friendly_abort (1823);
5146 }
5147
5148 /* Note a warning about preferring WINNER to LOSER.  We do this by storing
5149    a pointer to LOSER and re-running joust to produce the warning if WINNER
5150    is actually used.  */
5151
5152 static void
5153 add_warning (winner, loser)
5154      struct z_candidate *winner, *loser;
5155 {
5156   winner->warnings = tree_cons (NULL_TREE,
5157                                 build_ptr_wrapper (loser),
5158                                 winner->warnings);
5159 }
5160
5161 /* Returns true iff functions are equivalent. Equivalent functions are
5162    not '==' only if one is a function-local extern function or if
5163    both are extern "C".  */
5164
5165 static inline int
5166 equal_functions (fn1, fn2)
5167      tree fn1;
5168      tree fn2;
5169 {
5170   if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5171       || DECL_EXTERN_C_FUNCTION_P (fn1))
5172     return decls_match (fn1, fn2);
5173   return fn1 == fn2;
5174 }
5175
5176 /* Compare two candidates for overloading as described in
5177    [over.match.best].  Return values:
5178
5179       1: cand1 is better than cand2
5180      -1: cand2 is better than cand1
5181       0: cand1 and cand2 are indistinguishable */
5182
5183 static int
5184 joust (cand1, cand2, warn)
5185      struct z_candidate *cand1, *cand2;
5186      int warn;
5187 {
5188   int winner = 0;
5189   int i, off1 = 0, off2 = 0, len;
5190
5191   /* Candidates that involve bad conversions are always worse than those
5192      that don't.  */
5193   if (cand1->viable > cand2->viable)
5194     return 1;
5195   if (cand1->viable < cand2->viable)
5196     return -1;
5197
5198   /* If we have two pseudo-candidates for conversions to the same type,
5199      or two candidates for the same function, arbitrarily pick one.  */
5200   if (cand1->fn == cand2->fn
5201       && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5202     return 1;
5203
5204   /* a viable function F1
5205      is defined to be a better function than another viable function F2  if
5206      for  all arguments i, ICSi(F1) is not a worse conversion sequence than
5207      ICSi(F2), and then */
5208
5209   /* for some argument j, ICSj(F1) is a better conversion  sequence  than
5210      ICSj(F2) */
5211
5212   /* For comparing static and non-static member functions, we ignore
5213      the implicit object parameter of the non-static function.  The
5214      standard says to pretend that the static function has an object
5215      parm, but that won't work with operator overloading.  */
5216   len = TREE_VEC_LENGTH (cand1->convs);
5217   if (len != TREE_VEC_LENGTH (cand2->convs))
5218     {
5219       if (DECL_STATIC_FUNCTION_P (cand1->fn)
5220           && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5221         off2 = 1;
5222       else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5223                && DECL_STATIC_FUNCTION_P (cand2->fn))
5224         {
5225           off1 = 1;
5226           --len;
5227         }
5228       else
5229         my_friendly_abort (42);
5230     }
5231
5232   for (i = 0; i < len; ++i)
5233     {
5234       tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5235       tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5236       int comp = compare_ics (t1, t2);
5237
5238       if (comp != 0)
5239         {
5240           if (warn_sign_promo
5241               && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5242               && TREE_CODE (t1) == STD_CONV
5243               && TREE_CODE (t2) == STD_CONV
5244               && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5245               && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5246               && (TYPE_PRECISION (TREE_TYPE (t1))
5247                   == TYPE_PRECISION (TREE_TYPE (t2)))
5248               && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5249                   || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5250                       == ENUMERAL_TYPE)))
5251             {
5252               tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5253               tree type1, type2;
5254               struct z_candidate *w, *l;
5255               if (comp > 0)
5256                 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5257                   w = cand1, l = cand2;
5258               else
5259                 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5260                   w = cand2, l = cand1;
5261
5262               if (warn)
5263                 {
5264                   cp_warning ("passing `%T' chooses `%T' over `%T'",
5265                               type, type1, type2);
5266                   cp_warning ("  in call to `%D'", w->fn);
5267                 }
5268               else
5269                 add_warning (w, l);
5270             }
5271
5272           if (winner && comp != winner)
5273             {
5274               winner = 0;
5275               goto tweak;
5276             }
5277           winner = comp;
5278         }
5279     }
5280
5281   /* warn about confusing overload resolution for user-defined conversions,
5282      either between a constructor and a conversion op, or between two
5283      conversion ops.  */
5284   if (winner && cand1->second_conv
5285       && ((DECL_CONSTRUCTOR_P (cand1->fn)
5286            != DECL_CONSTRUCTOR_P (cand2->fn))
5287           /* Don't warn if the two conv ops convert to the same type...  */
5288           || (! DECL_CONSTRUCTOR_P (cand1->fn)
5289               && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5290                                 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5291     {
5292       int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5293       if (comp != winner)
5294         {
5295           struct z_candidate *w, *l;
5296           tree convn;
5297           if (winner == 1)
5298             w = cand1, l = cand2;
5299           else
5300             w = cand2, l = cand1;
5301           if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5302               && ! DECL_CONSTRUCTOR_P (cand1->fn)
5303               && ! DECL_CONSTRUCTOR_P (cand2->fn)
5304               && (convn = standard_conversion
5305                   (TREE_TYPE (TREE_TYPE (l->fn)),
5306                    TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5307               && TREE_CODE (convn) == QUAL_CONV)
5308             /* Don't complain about `operator char *()' beating
5309                `operator const char *() const'.  */;
5310           else if (warn)
5311             {
5312               tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5313               if (! DECL_CONSTRUCTOR_P (w->fn))
5314                 source = TREE_TYPE (source);
5315               cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
5316               cp_warning ("  for conversion from `%T' to `%T'",
5317                           source, TREE_TYPE (w->second_conv));
5318               cp_warning ("  because conversion sequence for the argument is better");
5319             }
5320           else
5321             add_warning (w, l);
5322         }
5323     }
5324
5325   if (winner)
5326     return winner;
5327
5328   /* or, if not that,
5329      F1 is a non-template function and F2 is a template function
5330      specialization.  */
5331          
5332   if (! cand1->template && cand2->template)
5333     return 1;
5334   else if (cand1->template && ! cand2->template)
5335     return -1;
5336   
5337   /* or, if not that,
5338      F1 and F2 are template functions and the function template for F1 is
5339      more specialized than the template for F2 according to the partial
5340      ordering rules.  */
5341   
5342   if (cand1->template && cand2->template)
5343     {
5344       winner = more_specialized
5345         (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5346          DEDUCE_ORDER,
5347          /* Tell the deduction code how many real function arguments
5348             we saw, not counting the implicit 'this' argument.  But,
5349             add_function_candidate() suppresses the "this" argument
5350             for constructors.
5351
5352             [temp.func.order]: The presence of unused ellipsis and default
5353             arguments has no effect on the partial ordering of function
5354             templates.  */
5355          TREE_VEC_LENGTH (cand1->convs)
5356          - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5357             - DECL_CONSTRUCTOR_P (cand1->fn)));
5358       /* HERE */
5359       if (winner)
5360         return winner;
5361     }
5362
5363   /* or, if not that,
5364      the  context  is  an  initialization by user-defined conversion (see
5365      _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
5366      sequence  from  the return type of F1 to the destination type (i.e.,
5367      the type of the entity being initialized)  is  a  better  conversion
5368      sequence  than the standard conversion sequence from the return type
5369      of F2 to the destination type.  */
5370
5371   if (cand1->second_conv)
5372     {
5373       winner = compare_ics (cand1->second_conv, cand2->second_conv);
5374       if (winner)
5375         return winner;
5376     }
5377   
5378   /* Check whether we can discard a builtin candidate, either because we
5379      have two identical ones or matching builtin and non-builtin candidates.
5380
5381      (Pedantically in the latter case the builtin which matched the user
5382      function should not be added to the overload set, but we spot it here.
5383      
5384      [over.match.oper]
5385      ... the builtin candidates include ...
5386      - do not have the same parameter type list as any non-template
5387        non-member candidate.  */
5388                             
5389   if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5390       || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5391     {
5392       for (i = 0; i < len; ++i)
5393         if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5394                           TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5395           break;
5396       if (i == TREE_VEC_LENGTH (cand1->convs))
5397         {
5398           if (cand1->fn == cand2->fn)
5399             /* Two built-in candidates; arbitrarily pick one.  */
5400             return 1;
5401           else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5402             /* cand1 is built-in; prefer cand2.  */
5403             return -1;
5404           else
5405             /* cand2 is built-in; prefer cand1.  */
5406             return 1;
5407         }
5408
5409       /* Kludge around broken overloading rules whereby
5410          Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5411          that takes references and another that takes values.  */
5412       if (cand1->fn == cand2->fn
5413           && cand1->fn == ansi_opname (COND_EXPR))
5414         {
5415           tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5416           tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5417           tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5418           tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5419
5420           if (same_type_p (t1, t2))
5421             {
5422               if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5423                 return 1;
5424               if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5425                 return -1;
5426             }
5427         }
5428     }
5429
5430   /* If the two functions are the same (this can happen with declarations
5431      in multiple scopes and arg-dependent lookup), arbitrarily choose one.  */
5432   if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5433       && equal_functions (cand1->fn, cand2->fn))
5434     return 1;
5435
5436 tweak:
5437
5438   /* Extension: If the worst conversion for one candidate is worse than the
5439      worst conversion for the other, take the first.  */
5440   if (!pedantic)
5441     {
5442       int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5443       struct z_candidate *w = 0, *l = 0;
5444
5445       for (i = 0; i < len; ++i)
5446         {
5447           if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5448             rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5449           if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5450             rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5451         }
5452       if (rank1 < rank2)
5453         winner = 1, w = cand1, l = cand2;
5454       if (rank1 > rank2)
5455         winner = -1, w = cand2, l = cand1;
5456       if (winner)
5457         {
5458           if (warn)
5459             {
5460               cp_pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5461               cp_pedwarn (
5462 "  because worst conversion for the former is better than worst conversion for the latter");
5463             }
5464           else
5465             add_warning (w, l);
5466           return winner;
5467         }
5468     }
5469
5470   my_friendly_assert (!winner, 20010121);
5471   return 0;
5472 }
5473
5474 /* Given a list of candidates for overloading, find the best one, if any.
5475    This algorithm has a worst case of O(2n) (winner is last), and a best
5476    case of O(n/2) (totally ambiguous); much better than a sorting
5477    algorithm.  */
5478
5479 static struct z_candidate *
5480 tourney (candidates)
5481      struct z_candidate *candidates;
5482 {
5483   struct z_candidate *champ = candidates, *challenger;
5484   int fate;
5485   int champ_compared_to_predecessor = 0;
5486
5487   /* Walk through the list once, comparing each current champ to the next
5488      candidate, knocking out a candidate or two with each comparison.  */
5489
5490   for (challenger = champ->next; challenger; )
5491     {
5492       fate = joust (champ, challenger, 0);
5493       if (fate == 1)
5494         challenger = challenger->next;
5495       else
5496         {
5497           if (fate == 0)
5498             {
5499               champ = challenger->next;
5500               if (champ == 0)
5501                 return 0;
5502               champ_compared_to_predecessor = 0;
5503             }
5504           else
5505             {
5506               champ = challenger;
5507               champ_compared_to_predecessor = 1;
5508             }
5509
5510           challenger = champ->next;
5511         }
5512     }
5513
5514   /* Make sure the champ is better than all the candidates it hasn't yet
5515      been compared to.  */
5516
5517   for (challenger = candidates; 
5518        challenger != champ 
5519          && !(champ_compared_to_predecessor && challenger->next == champ);
5520        challenger = challenger->next)
5521     {
5522       fate = joust (champ, challenger, 0);
5523       if (fate != 1)
5524         return 0;
5525     }
5526
5527   return champ;
5528 }
5529
5530 /* Returns non-zero if things of type FROM can be converted to TO.  */
5531
5532 int
5533 can_convert (to, from)
5534      tree to, from;
5535 {
5536   return can_convert_arg (to, from, NULL_TREE);
5537 }
5538
5539 /* Returns non-zero if ARG (of type FROM) can be converted to TO.  */
5540
5541 int
5542 can_convert_arg (to, from, arg)
5543      tree to, from, arg;
5544 {
5545   tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5546   return (t && ! ICS_BAD_FLAG (t));
5547 }
5548
5549 /* Convert EXPR to TYPE.  Return the converted expression.  */
5550
5551 tree
5552 perform_implicit_conversion (type, expr)
5553      tree type;
5554      tree expr;
5555 {
5556   tree conv;
5557   
5558   if (expr == error_mark_node)
5559     return error_mark_node;
5560   conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5561                               LOOKUP_NORMAL);
5562   if (!conv || ICS_BAD_FLAG (conv))
5563     {
5564       cp_error ("could not convert `%E' to `%T'", expr, type);
5565       return error_mark_node;
5566     }
5567
5568   return convert_like (conv, expr);
5569 }
5570
5571 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5572    initializing a variable of that TYPE.  Return the converted
5573    expression.  */
5574
5575 tree
5576 initialize_reference (type, expr)
5577      tree type;
5578      tree expr;
5579 {
5580   tree conv;
5581
5582   conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5583   if (!conv || ICS_BAD_FLAG (conv))
5584     {
5585       cp_error ("could not convert `%E' to `%T'", expr, type);
5586       return error_mark_node;
5587     }
5588
5589   return convert_like (conv, expr);
5590 }