OSDN Git Service

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