OSDN Git Service

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