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   if (CLASS_TYPE_P (TREE_TYPE (arg1))
3347       && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg1)))
3348     /* Make sure the template type is instantiated now.  */
3349     instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)));
3350   
3351   switch (code)
3352     {
3353     case NEW_EXPR:
3354     case VEC_NEW_EXPR:
3355     case VEC_DELETE_EXPR:
3356     case DELETE_EXPR:
3357       /* Use build_op_new_call and build_op_delete_call instead.  */
3358       abort ();
3359
3360     case CALL_EXPR:
3361       return build_object_call (arg1, arg2);
3362
3363     default:
3364       break;
3365     }
3366
3367   if (arg2)
3368     {
3369       if (TREE_CODE (arg2) == OFFSET_REF)
3370         arg2 = resolve_offset_ref (arg2);
3371       arg2 = convert_from_reference (arg2);
3372       if (CLASS_TYPE_P (TREE_TYPE (arg2))
3373           && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg2)))
3374         instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg2)));
3375     }
3376   if (arg3)
3377     {
3378       if (TREE_CODE (arg3) == OFFSET_REF)
3379         arg3 = resolve_offset_ref (arg3);
3380       arg3 = convert_from_reference (arg3);
3381       if (CLASS_TYPE_P (TREE_TYPE (arg3))
3382           && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg3)))
3383         instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg3)));
3384     }
3385   
3386   if (code == COND_EXPR)
3387     {
3388       if (arg2 == NULL_TREE
3389           || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3390           || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3391           || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3392               && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3393         goto builtin;
3394     }
3395   else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3396            && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3397     goto builtin;
3398
3399   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3400     arg2 = integer_zero_node;
3401
3402   arglist = NULL_TREE;
3403   if (arg3)
3404     arglist = tree_cons (NULL_TREE, arg3, arglist);
3405   if (arg2)
3406     arglist = tree_cons (NULL_TREE, arg2, arglist);
3407   arglist = tree_cons (NULL_TREE, arg1, arglist);
3408
3409   fns = lookup_function_nonclass (fnname, arglist);
3410
3411   if (fns && TREE_CODE (fns) == TREE_LIST)
3412     fns = TREE_VALUE (fns);
3413   for (; fns; fns = OVL_NEXT (fns))
3414     {
3415       tree fn = OVL_CURRENT (fns);
3416       if (TREE_CODE (fn) == TEMPLATE_DECL)
3417         {
3418           templates = tree_cons (NULL_TREE, fn, templates);
3419           candidates 
3420             = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3421                                       arglist, TREE_TYPE (fnname),
3422                                       /*access_path=*/NULL_TREE,
3423                                       /*conversion_path=*/NULL_TREE,
3424                                       flags, DEDUCE_CALL); 
3425         }
3426       else
3427         candidates = add_function_candidate (candidates, fn, NULL_TREE,
3428                                              arglist,
3429                                              /*access_path=*/NULL_TREE,
3430                                              /*conversion_path=*/NULL_TREE,
3431                                              flags);
3432     }
3433
3434   if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3435     {
3436       fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3437       if (fns == error_mark_node)
3438         return fns;
3439     }
3440   else
3441     fns = NULL_TREE;
3442
3443   if (fns)
3444     {
3445       tree conversion_path = BASELINK_BINFO (fns);
3446
3447       mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3448       for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
3449         {
3450           tree fn = OVL_CURRENT (fns);
3451           tree this_arglist;
3452           tree access_path = TYPE_BINFO (TREE_TYPE (arg1));
3453
3454           if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3455             this_arglist = mem_arglist;
3456           else
3457             this_arglist = arglist;
3458
3459           if (TREE_CODE (fn) == TEMPLATE_DECL)
3460             {
3461               /* A member template.  */
3462               templates = tree_cons (NULL_TREE, fn, templates);
3463               candidates 
3464                 = add_template_candidate (candidates, fn, 
3465                                           BINFO_TYPE (conversion_path),
3466                                           NULL_TREE,
3467                                           this_arglist,  TREE_TYPE (fnname),
3468                                           access_path, conversion_path,
3469                                           flags, DEDUCE_CALL); 
3470             }
3471           else
3472             candidates = add_function_candidate
3473               (candidates, fn, BINFO_TYPE (conversion_path), this_arglist, 
3474                access_path, conversion_path, flags);
3475         }
3476     }
3477
3478   {
3479     tree args[3];
3480
3481     /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3482        to know about two args; a builtin candidate will always have a first
3483        parameter of type bool.  We'll handle that in
3484        build_builtin_candidate.  */
3485     if (code == COND_EXPR)
3486       {
3487         args[0] = arg2;
3488         args[1] = arg3;
3489         args[2] = arg1;
3490       }
3491     else
3492       {
3493         args[0] = arg1;
3494         args[1] = arg2;
3495         args[2] = NULL_TREE;
3496       }
3497
3498     candidates = add_builtin_candidates
3499       (candidates, code, code2, fnname, args, flags);
3500   }
3501
3502   switch (code)
3503     {
3504     case COMPOUND_EXPR:
3505     case ADDR_EXPR:
3506       /* For these, the built-in candidates set is empty
3507          [over.match.oper]/3.  We don't want non-strict matches
3508          because exact matches are always possible with built-in
3509          operators.  The built-in candidate set for COMPONENT_REF
3510          would be empty too, but since there are no such built-in
3511          operators, we accept non-strict matches for them.  */
3512       viable_candidates = any_strictly_viable (candidates);
3513       break;
3514
3515     default:
3516       viable_candidates = any_viable (candidates);
3517       break;
3518     }      
3519
3520   if (! viable_candidates)
3521     {
3522       switch (code)
3523         {
3524         case POSTINCREMENT_EXPR:
3525         case POSTDECREMENT_EXPR:
3526           /* Look for an `operator++ (int)'.  If they didn't have
3527              one, then we fall back to the old way of doing things.  */
3528           if (flags & LOOKUP_COMPLAIN)
3529             pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3530                         fnname, 
3531                         operator_name_info[code].name);
3532           if (code == POSTINCREMENT_EXPR)
3533             code = PREINCREMENT_EXPR;
3534           else
3535             code = PREDECREMENT_EXPR;   
3536           return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3537           
3538           /* The caller will deal with these.  */
3539         case ADDR_EXPR:
3540         case COMPOUND_EXPR:
3541         case COMPONENT_REF:
3542           return NULL_TREE;
3543
3544         default:
3545           break;
3546         }
3547       if (flags & LOOKUP_COMPLAIN)
3548         {
3549           op_error (code, code2, arg1, arg2, arg3, "no match");
3550           print_z_candidates (candidates);
3551         }
3552       return error_mark_node;
3553     }
3554   candidates = splice_viable (candidates);
3555   cand = tourney (candidates);
3556
3557   if (cand == 0)
3558     {
3559       if (flags & LOOKUP_COMPLAIN)
3560         {
3561           op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3562           print_z_candidates (candidates);
3563         }
3564       return error_mark_node;
3565     }
3566
3567   if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3568     {
3569       extern int warn_synth;
3570       if (warn_synth
3571           && fnname == ansi_assopname (NOP_EXPR)
3572           && DECL_ARTIFICIAL (cand->fn)
3573           && candidates->next
3574           && ! candidates->next->next)
3575         {
3576           warning ("using synthesized `%#D' for copy assignment",
3577                       cand->fn);
3578           cp_warning_at ("  where cfront would use `%#D'",
3579                          cand == candidates
3580                          ? candidates->next->fn
3581                          : candidates->fn);
3582         }
3583
3584       return build_over_call
3585         (cand,
3586          TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3587          ? mem_arglist : arglist,
3588          LOOKUP_NORMAL);
3589     }
3590
3591   /* Check for comparison of different enum types.  */
3592   switch (code)
3593     {
3594     case GT_EXPR:
3595     case LT_EXPR:
3596     case GE_EXPR:
3597     case LE_EXPR:
3598     case EQ_EXPR:
3599     case NE_EXPR:
3600       if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 
3601           && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 
3602           && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3603               != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3604         {
3605           warning ("comparison between `%#T' and `%#T'", 
3606                       TREE_TYPE (arg1), TREE_TYPE (arg2));
3607         }
3608       break;
3609     default:
3610       break;
3611     }
3612
3613   /* We need to strip any leading REF_BIND so that bitfields don't cause
3614      errors.  This should not remove any important conversions, because
3615      builtins don't apply to class objects directly.  */
3616   conv = TREE_VEC_ELT (cand->convs, 0);
3617   if (TREE_CODE (conv) == REF_BIND)
3618     conv = TREE_OPERAND (conv, 0);
3619   arg1 = convert_like (conv, arg1);
3620   if (arg2)
3621     {
3622       conv = TREE_VEC_ELT (cand->convs, 1);
3623       if (TREE_CODE (conv) == REF_BIND)
3624         conv = TREE_OPERAND (conv, 0);
3625       arg2 = convert_like (conv, arg2);
3626     }
3627   if (arg3)
3628     {
3629       conv = TREE_VEC_ELT (cand->convs, 2);
3630       if (TREE_CODE (conv) == REF_BIND)
3631         conv = TREE_OPERAND (conv, 0);
3632       arg3 = convert_like (conv, arg3);
3633     }
3634
3635 builtin:
3636   switch (code)
3637     {
3638     case MODIFY_EXPR:
3639       return build_modify_expr (arg1, code2, arg2);
3640
3641     case INDIRECT_REF:
3642       return build_indirect_ref (arg1, "unary *");
3643
3644     case PLUS_EXPR:
3645     case MINUS_EXPR:
3646     case MULT_EXPR:
3647     case TRUNC_DIV_EXPR:
3648     case GT_EXPR:
3649     case LT_EXPR:
3650     case GE_EXPR:
3651     case LE_EXPR:
3652     case EQ_EXPR:
3653     case NE_EXPR:
3654     case MAX_EXPR:
3655     case MIN_EXPR:
3656     case LSHIFT_EXPR:
3657     case RSHIFT_EXPR:
3658     case TRUNC_MOD_EXPR:
3659     case BIT_AND_EXPR:
3660     case BIT_IOR_EXPR:
3661     case BIT_XOR_EXPR:
3662     case TRUTH_ANDIF_EXPR:
3663     case TRUTH_ORIF_EXPR:
3664       return cp_build_binary_op (code, arg1, arg2);
3665
3666     case CONVERT_EXPR:
3667     case NEGATE_EXPR:
3668     case BIT_NOT_EXPR:
3669     case TRUTH_NOT_EXPR:
3670     case PREINCREMENT_EXPR:
3671     case POSTINCREMENT_EXPR:
3672     case PREDECREMENT_EXPR:
3673     case POSTDECREMENT_EXPR:
3674     case REALPART_EXPR:
3675     case IMAGPART_EXPR:
3676       return build_unary_op (code, arg1, candidates != 0);
3677
3678     case ARRAY_REF:
3679       return build_array_ref (arg1, arg2);
3680
3681     case COND_EXPR:
3682       return build_conditional_expr (arg1, arg2, arg3);
3683
3684     case MEMBER_REF:
3685       return build_m_component_ref
3686         (build_indirect_ref (arg1, NULL), arg2);
3687
3688       /* The caller will deal with these.  */
3689     case ADDR_EXPR:
3690     case COMPONENT_REF:
3691     case COMPOUND_EXPR:
3692       return NULL_TREE;
3693
3694     default:
3695       abort ();
3696       return NULL_TREE;
3697     }
3698 }
3699
3700 /* Build a call to operator delete.  This has to be handled very specially,
3701    because the restrictions on what signatures match are different from all
3702    other call instances.  For a normal delete, only a delete taking (void *)
3703    or (void *, size_t) is accepted.  For a placement delete, only an exact
3704    match with the placement new is accepted.
3705
3706    CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3707    ADDR is the pointer to be deleted.
3708    SIZE is the size of the memory block to be deleted.
3709    FLAGS are the usual overloading flags.
3710    PLACEMENT is the corresponding placement new call, or NULL_TREE.  */
3711
3712 tree
3713 build_op_delete_call (enum tree_code code, tree addr, tree size,
3714                       int flags, tree placement)
3715 {
3716   tree fn = NULL_TREE;
3717   tree fns, fnname, fntype, argtypes, args, type;
3718   int pass;
3719
3720   if (addr == error_mark_node)
3721     return error_mark_node;
3722
3723   type = TREE_TYPE (TREE_TYPE (addr));
3724   while (TREE_CODE (type) == ARRAY_TYPE)
3725     type = TREE_TYPE (type);
3726
3727   fnname = ansi_opname (code);
3728
3729   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3730     /* In [class.free]
3731
3732        If the result of the lookup is ambiguous or inaccessible, or if
3733        the lookup selects a placement deallocation function, the
3734        program is ill-formed.
3735   
3736        Therefore, we ask lookup_fnfields to complain ambout ambiguity.  */
3737     {
3738       fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3739       if (fns == error_mark_node)
3740         return error_mark_node;
3741     }
3742   else
3743     fns = NULL_TREE;
3744
3745   if (fns == NULL_TREE)
3746     fns = lookup_name_nonclass (fnname);
3747
3748   if (placement)
3749     {
3750       tree alloc_fn;
3751       tree call_expr;
3752
3753       /* Find the allocation function that is being called.  */
3754       call_expr = placement;
3755       /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3756          CALL_EXPR.  */
3757       while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3758         call_expr = TREE_OPERAND (call_expr, 1);
3759       /* Extract the function.  */
3760       alloc_fn = get_callee_fndecl (call_expr);
3761       my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3762       /* Then the second parm type.  */
3763       argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3764       /* Also the second argument.  */
3765       args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3766     }
3767   else
3768     {
3769       /* First try it without the size argument.  */
3770       argtypes = void_list_node;
3771       args = NULL_TREE;
3772     }
3773
3774   /* Strip const and volatile from addr.  */
3775   addr = cp_convert (ptr_type_node, addr);
3776
3777   /* We make two tries at finding a matching `operator delete'.  On
3778      the first pass, we look for an one-operator (or placement)
3779      operator delete.  If we're not doing placement delete, then on
3780      the second pass we look for a two-argument delete.  */
3781   for (pass = 0; pass < (placement ? 1 : 2); ++pass) 
3782     {
3783       if (pass == 0)
3784         argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3785       else 
3786         /* Normal delete; now try to find a match including the size
3787            argument.  */
3788         argtypes = tree_cons (NULL_TREE, ptr_type_node,
3789                               tree_cons (NULL_TREE, sizetype, 
3790                                          void_list_node));
3791       fntype = build_function_type (void_type_node, argtypes);
3792
3793       /* Go through the `operator delete' functions looking for one
3794          with a matching type.  */
3795       for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns; 
3796            fn; 
3797            fn = OVL_NEXT (fn))
3798         {
3799           tree t;
3800
3801           /* Exception specifications on the `delete' operator do not
3802              matter.  */
3803           t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3804                                        NULL_TREE);
3805           /* We also don't compare attributes.  We're really just
3806              trying to check the types of the first two parameters.  */
3807           if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3808             break;
3809         }
3810
3811       /* If we found a match, we're done.  */
3812       if (fn)
3813         break;
3814     }
3815
3816   /* If we have a matching function, call it.  */
3817   if (fn)
3818     {
3819       /* Make sure we have the actual function, and not an
3820          OVERLOAD.  */
3821       fn = OVL_CURRENT (fn);
3822
3823       /* If the FN is a member function, make sure that it is
3824          accessible.  */
3825       if (DECL_CLASS_SCOPE_P (fn))
3826         enforce_access (type, fn);
3827
3828       if (pass == 0)
3829         args = tree_cons (NULL_TREE, addr, args);
3830       else
3831         args = tree_cons (NULL_TREE, addr, 
3832                           build_tree_list (NULL_TREE, size));
3833
3834       return build_function_call (fn, args);
3835     }
3836
3837   /* If we are doing placement delete we do nothing if we don't find a
3838      matching op delete.  */
3839   if (placement)
3840     return NULL_TREE;
3841
3842   error ("no suitable `operator delete' for `%T'", type);
3843   return error_mark_node;
3844 }
3845
3846 /* If the current scope isn't allowed to access DECL along
3847    BASETYPE_PATH, give an error.  The most derived class in
3848    BASETYPE_PATH is the one used to qualify DECL.  */
3849
3850 bool
3851 enforce_access (tree basetype_path, tree decl)
3852 {
3853   if (!accessible_p (basetype_path, decl))
3854     {
3855       if (TREE_PRIVATE (decl))
3856         cp_error_at ("`%+#D' is private", decl);
3857       else if (TREE_PROTECTED (decl))
3858         cp_error_at ("`%+#D' is protected", decl);
3859       else
3860         cp_error_at ("`%+#D' is inaccessible", decl);
3861       error ("within this context");
3862       return false;
3863     }
3864
3865   return true;
3866 }
3867
3868 /* Perform the conversions in CONVS on the expression EXPR. 
3869    FN and ARGNUM are used for diagnostics.  ARGNUM is zero based, -1
3870    indicates the `this' argument of a method.  INNER is nonzero when
3871    being called to continue a conversion chain. It is negative when a
3872    reference binding will be applied, positive otherwise.  */
3873
3874 static tree
3875 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner)
3876 {
3877   int savew, savee;
3878
3879   tree totype = TREE_TYPE (convs);
3880
3881   if (ICS_BAD_FLAG (convs)
3882       && TREE_CODE (convs) != USER_CONV
3883       && TREE_CODE (convs) != AMBIG_CONV
3884       && TREE_CODE (convs) != REF_BIND)
3885     {
3886       tree t = convs; 
3887       for (; t; t = TREE_OPERAND (t, 0))
3888         {
3889           if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3890             {
3891               expr = convert_like_real (t, expr, fn, argnum, 1);
3892               break;
3893             }
3894           else if (TREE_CODE (t) == AMBIG_CONV)
3895             return convert_like_real (t, expr, fn, argnum, 1);
3896           else if (TREE_CODE (t) == IDENTITY_CONV)
3897             break;
3898         }
3899       pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3900       if (fn)
3901         pedwarn ("  initializing argument %P of `%D'", argnum, fn);
3902       return cp_convert (totype, expr);
3903     }
3904   
3905   if (!inner)
3906     expr = dubious_conversion_warnings
3907              (totype, expr, "argument", fn, argnum);
3908   switch (TREE_CODE (convs))
3909     {
3910     case USER_CONV:
3911       {
3912         struct z_candidate *cand
3913           = WRAPPER_ZC (TREE_OPERAND (convs, 1));
3914         tree convfn = cand->fn;
3915         tree args;
3916
3917         if (DECL_CONSTRUCTOR_P (convfn))
3918           {
3919             tree t = build_int_2 (0, 0);
3920             TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3921
3922             args = build_tree_list (NULL_TREE, expr);
3923             if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3924                 || DECL_HAS_VTT_PARM_P (convfn))
3925               /* We should never try to call the abstract or base constructor
3926                  from here.  */
3927               abort ();
3928             args = tree_cons (NULL_TREE, t, args);
3929           }
3930         else
3931           args = build_this (expr);
3932         expr = build_over_call (cand, args, LOOKUP_NORMAL);
3933
3934         /* If this is a constructor or a function returning an aggr type,
3935            we need to build up a TARGET_EXPR.  */
3936         if (DECL_CONSTRUCTOR_P (convfn))
3937           expr = build_cplus_new (totype, expr);
3938
3939         /* The result of the call is then used to direct-initialize the object
3940            that is the destination of the copy-initialization.  [dcl.init]
3941
3942            Note that this step is not reflected in the conversion sequence;
3943            it affects the semantics when we actually perform the
3944            conversion, but is not considered during overload resolution.
3945
3946            If the target is a class, that means call a ctor.  */
3947         if (IS_AGGR_TYPE (totype)
3948             && (inner >= 0 || !lvalue_p (expr)))
3949           {
3950             savew = warningcount, savee = errorcount;
3951             expr = build_special_member_call
3952               (NULL_TREE, complete_ctor_identifier,
3953                build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3954                /* Core issue 84, now a DR, says that we don't allow UDCs
3955                   for these args (which deliberately breaks copy-init of an
3956                   auto_ptr<Base> from an auto_ptr<Derived>).  */
3957                LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3958
3959             /* Tell the user where this failing constructor call came from.  */
3960             if (fn)
3961               {
3962                 if (warningcount > savew)
3963                   warning
3964                     ("  initializing argument %P of `%D' from result of `%D'",
3965                      argnum, fn, convfn);
3966                 else if (errorcount > savee)
3967                   error
3968                     ("  initializing argument %P of `%D' from result of `%D'",
3969                      argnum, fn, convfn);
3970               }
3971             else
3972               {
3973                 if (warningcount > savew)
3974                   warning ("  initializing temporary from result of `%D'",
3975                               convfn);
3976                 else if (errorcount > savee)
3977                   error ("  initializing temporary from result of `%D'",
3978                             convfn);
3979               }
3980             expr = build_cplus_new (totype, expr);
3981           }
3982         return expr;
3983       }
3984     case IDENTITY_CONV:
3985       if (type_unknown_p (expr))
3986         expr = instantiate_type (totype, expr, tf_error | tf_warning);
3987       return expr;
3988     case AMBIG_CONV:
3989       /* Call build_user_type_conversion again for the error.  */
3990       return build_user_type_conversion
3991         (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3992
3993     default:
3994       break;
3995     };
3996
3997   expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3998                             TREE_CODE (convs) == REF_BIND ? -1 : 1);
3999   if (expr == error_mark_node)
4000     return error_mark_node;
4001
4002   /* Convert a non-array constant variable to its underlying value, unless we
4003      are about to bind it to a reference, in which case we need to
4004      leave it as an lvalue.  */
4005   if (TREE_CODE (convs) != REF_BIND
4006       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4007     expr = decl_constant_value (expr);
4008
4009   switch (TREE_CODE (convs))
4010     {
4011     case RVALUE_CONV:
4012       if (! IS_AGGR_TYPE (totype))
4013         return expr;
4014       /* else fall through */
4015     case BASE_CONV:
4016       if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4017         {
4018           /* We are going to bind a reference directly to a base-class
4019              subobject of EXPR.  */
4020           tree base_ptr = build_pointer_type (totype);
4021
4022           /* Build an expression for `*((base*) &expr)'.  */
4023           expr = build_unary_op (ADDR_EXPR, expr, 0);
4024           expr = perform_implicit_conversion (base_ptr, expr);
4025           expr = build_indirect_ref (expr, "implicit conversion");
4026           return expr;
4027         }
4028
4029       /* Copy-initialization where the cv-unqualified version of the source
4030          type is the same class as, or a derived class of, the class of the
4031          destination [is treated as direct-initialization].  [dcl.init] */
4032       savew = warningcount, savee = errorcount;
4033       expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4034                                         build_tree_list (NULL_TREE, expr),
4035                                         TYPE_BINFO (totype),
4036                                         LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
4037       if (fn)
4038         {
4039           if (warningcount > savew)
4040             warning ("  initializing argument %P of `%D'", argnum, fn);
4041           else if (errorcount > savee)
4042             error ("  initializing argument %P of `%D'", argnum, fn);
4043         }
4044       return build_cplus_new (totype, expr);
4045
4046     case REF_BIND:
4047       {
4048         tree ref_type = totype;
4049
4050         /* If necessary, create a temporary.  */
4051         if (NEED_TEMPORARY_P (convs) || !non_cast_lvalue_p (expr))
4052           {
4053             tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4054             expr = build_target_expr_with_type (expr, type);
4055           }
4056
4057         /* Take the address of the thing to which we will bind the
4058            reference.  */
4059         expr = build_unary_op (ADDR_EXPR, expr, 1);
4060         if (expr == error_mark_node)
4061           return error_mark_node;
4062
4063         /* Convert it to a pointer to the type referred to by the
4064            reference.  This will adjust the pointer if a derived to
4065            base conversion is being performed.  */
4066         expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)), 
4067                            expr);
4068         /* Convert the pointer to the desired reference type.  */
4069         expr = build1 (NOP_EXPR, ref_type, expr);
4070
4071         return expr;
4072       }
4073
4074     case LVALUE_CONV:
4075       return decay_conversion (expr);
4076
4077     case QUAL_CONV:
4078       /* Warn about deprecated conversion if appropriate.  */
4079       string_conv_p (totype, expr, 1);
4080       break;
4081       
4082     default:
4083       break;
4084     }
4085   return ocp_convert (totype, expr, CONV_IMPLICIT,
4086                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4087 }
4088
4089 /* Build a call to __builtin_trap which can be used in an expression.  */
4090
4091 static tree
4092 call_builtin_trap (void)
4093 {
4094   tree fn = get_identifier ("__builtin_trap");
4095   if (IDENTIFIER_GLOBAL_VALUE (fn))
4096     fn = IDENTIFIER_GLOBAL_VALUE (fn);
4097   else
4098     abort ();
4099
4100   fn = build_call (fn, NULL_TREE);
4101   fn = build (COMPOUND_EXPR, integer_type_node, fn, integer_zero_node);
4102   return fn;
4103 }
4104
4105 /* ARG is being passed to a varargs function.  Perform any conversions
4106    required.  Array/function to pointer decay must have already happened.
4107    Return the converted value.  */
4108
4109 tree
4110 convert_arg_to_ellipsis (tree arg)
4111 {
4112   if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4113       && (TYPE_PRECISION (TREE_TYPE (arg))
4114           < TYPE_PRECISION (double_type_node)))
4115     /* Convert `float' to `double'.  */
4116     arg = cp_convert (double_type_node, arg);
4117   else
4118     /* Convert `short' and `char' to full-size `int'.  */
4119     arg = default_conversion (arg);
4120
4121   arg = require_complete_type (arg);
4122   
4123   if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4124     {
4125       /* Undefined behavior [expr.call] 5.2.2/7.  We used to just warn
4126          here and do a bitwise copy, but now cp_expr_size will abort if we
4127          try to do that.  */
4128       warning ("cannot pass objects of non-POD type `%#T' through `...'; \
4129 call will abort at runtime",
4130                TREE_TYPE (arg));
4131       arg = call_builtin_trap ();
4132     }
4133
4134   return arg;
4135 }
4136
4137 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused.  */
4138
4139 tree
4140 build_x_va_arg (tree expr, tree type)
4141 {
4142   if (processing_template_decl)
4143     return build_min (VA_ARG_EXPR, type, expr);
4144   
4145   type = complete_type_or_else (type, NULL_TREE);
4146
4147   if (expr == error_mark_node || !type)
4148     return error_mark_node;
4149   
4150   if (! pod_type_p (type))
4151     {
4152       /* Undefined behavior [expr.call] 5.2.2/7.  */
4153       warning ("cannot receive objects of non-POD type `%#T' through `...'",
4154                   type);
4155     }
4156   
4157   return build_va_arg (expr, type);
4158 }
4159
4160 /* TYPE has been given to va_arg.  Apply the default conversions which
4161    would have happened when passed via ellipsis.  Return the promoted
4162    type, or the passed type if there is no change.  */
4163
4164 tree
4165 cxx_type_promotes_to (tree type)
4166 {
4167   tree promote;
4168
4169   if (TREE_CODE (type) == ARRAY_TYPE)
4170     return build_pointer_type (TREE_TYPE (type));
4171
4172   if (TREE_CODE (type) == FUNCTION_TYPE)
4173     return build_pointer_type (type);
4174
4175   promote = type_promotes_to (type);
4176   if (same_type_p (type, promote))
4177     promote = type;
4178   
4179   return promote;
4180 }
4181
4182 /* ARG is a default argument expression being passed to a parameter of
4183    the indicated TYPE, which is a parameter to FN.  Do any required
4184    conversions.  Return the converted value.  */
4185
4186 tree
4187 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4188 {
4189   if (TREE_CODE (arg) == DEFAULT_ARG)
4190     {
4191       /* When processing the default args for a class, we can find that
4192          there is an ordering constraint, and we call a function who's
4193          default args have not yet been converted. For instance,
4194           class A {
4195               A (int = 0);
4196               void Foo (A const & = A ());
4197           };
4198          We must process A::A before A::Foo's default arg can be converted.
4199          Remember the dependent function, so do_pending_defargs can retry,
4200          and check loops.  */
4201       unprocessed_defarg_fn (fn);
4202       
4203       /* Don't return error_mark node, as we won't be able to distinguish
4204          genuine errors from this case, and that would lead to repeated
4205          diagnostics.  Just make something of the right type.  */
4206       return build1 (NOP_EXPR, type, integer_zero_node);
4207     }
4208
4209   if (fn && DECL_TEMPLATE_INFO (fn))
4210     arg = tsubst_default_argument (fn, type, arg);
4211
4212   arg = break_out_target_exprs (arg);
4213
4214   if (TREE_CODE (arg) == CONSTRUCTOR)
4215     {
4216       arg = digest_init (type, arg, 0);
4217       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4218                                         "default argument", fn, parmnum);
4219     }
4220   else
4221     {
4222       /* This could get clobbered by the following call.  */
4223       if (TREE_HAS_CONSTRUCTOR (arg))
4224         arg = copy_node (arg);
4225
4226       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4227                                         "default argument", fn, parmnum);
4228       arg = convert_for_arg_passing (type, arg);
4229     }
4230
4231   return arg;
4232 }
4233
4234 /* Returns the type which will really be used for passing an argument of
4235    type TYPE.  */
4236
4237 tree
4238 type_passed_as (tree type)
4239 {
4240   /* Pass classes with copy ctors by invisible reference.  */
4241   if (TREE_ADDRESSABLE (type))
4242     type = build_reference_type (type);
4243   else if (PROMOTE_PROTOTYPES
4244            && INTEGRAL_TYPE_P (type)
4245            && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4246     type = integer_type_node;
4247
4248   return type;
4249 }
4250
4251 /* Actually perform the appropriate conversion.  */
4252
4253 tree
4254 convert_for_arg_passing (tree type, tree val)
4255 {
4256   if (val == error_mark_node)
4257     ;
4258   /* Pass classes with copy ctors by invisible reference.  */
4259   else if (TREE_ADDRESSABLE (type))
4260     val = build1 (ADDR_EXPR, build_reference_type (type), val);
4261   else if (PROMOTE_PROTOTYPES
4262            && INTEGRAL_TYPE_P (type)
4263            && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4264     val = default_conversion (val);
4265   return val;
4266 }
4267
4268 /* Subroutine of the various build_*_call functions.  Overload resolution
4269    has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4270    ARGS is a TREE_LIST of the unconverted arguments to the call.  FLAGS is a
4271    bitmask of various LOOKUP_* flags which apply to the call itself.  */
4272
4273 static tree
4274 build_over_call (struct z_candidate *cand, tree args, int flags)
4275 {
4276   tree fn = cand->fn;
4277   tree convs = cand->convs;
4278   tree converted_args = NULL_TREE;
4279   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4280   tree conv, arg, val;
4281   int i = 0;
4282   int is_method = 0;
4283
4284   /* Give any warnings we noticed during overload resolution.  */
4285   if (cand->warnings)
4286     for (val = cand->warnings; val; val = TREE_CHAIN (val))
4287       joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4288
4289   if (DECL_FUNCTION_MEMBER_P (fn))
4290     enforce_access (cand->access_path, fn);
4291
4292   if (args && TREE_CODE (args) != TREE_LIST)
4293     args = build_tree_list (NULL_TREE, args);
4294   arg = args;
4295
4296   /* The implicit parameters to a constructor are not considered by overload
4297      resolution, and must be of the proper type.  */
4298   if (DECL_CONSTRUCTOR_P (fn))
4299     {
4300       converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4301       arg = TREE_CHAIN (arg);
4302       parm = TREE_CHAIN (parm);
4303       if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4304         /* We should never try to call the abstract constructor.  */
4305         abort ();
4306       if (DECL_HAS_VTT_PARM_P (fn))
4307         {
4308           converted_args = tree_cons
4309             (NULL_TREE, TREE_VALUE (arg), converted_args);
4310           arg = TREE_CHAIN (arg);
4311           parm = TREE_CHAIN (parm);
4312         }
4313     }      
4314   /* Bypass access control for 'this' parameter.  */
4315   else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4316     {
4317       tree parmtype = TREE_VALUE (parm);
4318       tree argtype = TREE_TYPE (TREE_VALUE (arg));
4319       tree converted_arg;
4320       tree base_binfo;
4321       
4322       if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4323         pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4324                     TREE_TYPE (argtype), fn);
4325
4326       /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4327          X is called for an object that is not of type X, or of a type
4328          derived from X, the behavior is undefined.
4329
4330          So we can assume that anything passed as 'this' is non-null, and
4331          optimize accordingly.  */
4332       my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4333       /* Convert to the base in which the function was declared.  */
4334       my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4335       converted_arg = build_base_path (PLUS_EXPR,
4336                                        TREE_VALUE (arg),
4337                                        cand->conversion_path,
4338                                        1);
4339       /* If fn was found by a using declaration, the conversion path
4340          will be to the derived class, not the base declaring fn. We
4341          must convert from derived to base.  */
4342       base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4343                                 TREE_TYPE (parmtype), ba_ignore, NULL);
4344       
4345       converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4346                                        base_binfo, 1);
4347       
4348       converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4349       parm = TREE_CHAIN (parm);
4350       arg = TREE_CHAIN (arg);
4351       ++i;
4352       is_method = 1;
4353     }
4354
4355   for (; arg && parm;
4356        parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4357     {
4358       tree type = TREE_VALUE (parm);
4359
4360       conv = TREE_VEC_ELT (convs, i);
4361       val = convert_like_with_context
4362         (conv, TREE_VALUE (arg), fn, i - is_method);
4363
4364       val = convert_for_arg_passing (type, val);
4365       converted_args = tree_cons (NULL_TREE, val, converted_args);
4366     }
4367
4368   /* Default arguments */
4369   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4370     converted_args 
4371       = tree_cons (NULL_TREE, 
4372                    convert_default_arg (TREE_VALUE (parm), 
4373                                         TREE_PURPOSE (parm),
4374                                         fn, i - is_method),
4375                    converted_args);
4376
4377   /* Ellipsis */
4378   for (; arg; arg = TREE_CHAIN (arg))
4379     converted_args 
4380       = tree_cons (NULL_TREE,
4381                    convert_arg_to_ellipsis (TREE_VALUE (arg)),
4382                    converted_args);
4383
4384   converted_args = nreverse (converted_args);
4385
4386   if (warn_format)
4387     check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4388                            converted_args);
4389
4390   /* Avoid actually calling copy constructors and copy assignment operators,
4391      if possible.  */
4392
4393   if (! flag_elide_constructors)
4394     /* Do things the hard way.  */;
4395   else if (TREE_VEC_LENGTH (convs) == 1
4396            && DECL_COPY_CONSTRUCTOR_P (fn))
4397     {
4398       tree targ;
4399       arg = skip_artificial_parms_for (fn, converted_args);
4400       arg = TREE_VALUE (arg);
4401
4402       /* Pull out the real argument, disregarding const-correctness.  */
4403       targ = arg;
4404       while (TREE_CODE (targ) == NOP_EXPR
4405              || TREE_CODE (targ) == NON_LVALUE_EXPR
4406              || TREE_CODE (targ) == CONVERT_EXPR)
4407         targ = TREE_OPERAND (targ, 0);
4408       if (TREE_CODE (targ) == ADDR_EXPR)
4409         {
4410           targ = TREE_OPERAND (targ, 0);
4411           if (!same_type_ignoring_top_level_qualifiers_p 
4412               (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4413             targ = NULL_TREE;
4414         }
4415       else
4416         targ = NULL_TREE;
4417
4418       if (targ)
4419         arg = targ;
4420       else
4421         arg = build_indirect_ref (arg, 0);
4422
4423       /* [class.copy]: the copy constructor is implicitly defined even if
4424          the implementation elided its use.  */
4425       if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4426         mark_used (fn);
4427
4428       /* If we're creating a temp and we already have one, don't create a
4429          new one.  If we're not creating a temp but we get one, use
4430          INIT_EXPR to collapse the temp into our target.  Otherwise, if the
4431          ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4432          temp or an INIT_EXPR otherwise.  */
4433       if (integer_zerop (TREE_VALUE (args)))
4434         {
4435           if (TREE_CODE (arg) == TARGET_EXPR)
4436             return arg;
4437           else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4438             return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4439         }
4440       else if (TREE_CODE (arg) == TARGET_EXPR
4441                || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4442         {
4443           tree address;
4444           tree to = stabilize_reference
4445             (build_indirect_ref (TREE_VALUE (args), 0));
4446
4447           val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4448           address = build_unary_op (ADDR_EXPR, val, 0);
4449           /* Avoid a warning about this expression, if the address is
4450              never used.  */
4451           TREE_USED (address) = 1;
4452           return address;
4453         }
4454     }
4455   else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4456            && copy_fn_p (fn)
4457            && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4458     {
4459       tree to = stabilize_reference
4460         (build_indirect_ref (TREE_VALUE (converted_args), 0));
4461
4462       arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4463       val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4464       return val;
4465     }
4466
4467   mark_used (fn);
4468
4469   if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4470     {
4471       tree t, *p = &TREE_VALUE (converted_args);
4472       tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4473                                 DECL_CONTEXT (fn),
4474                                 ba_any, NULL);
4475       my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4476       
4477       *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4478       if (TREE_SIDE_EFFECTS (*p))
4479         *p = save_expr (*p);
4480       t = build_pointer_type (TREE_TYPE (fn));
4481       if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4482         fn = build_java_interface_fn_ref (fn, *p);
4483       else
4484         fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4485       TREE_TYPE (fn) = t;
4486     }
4487   else if (DECL_INLINE (fn))
4488     fn = inline_conversion (fn);
4489   else
4490     fn = build_addr_func (fn);
4491
4492   /* Recognize certain built-in functions so we can make tree-codes
4493      other than CALL_EXPR.  We do this when it enables fold-const.c
4494      to do something useful.  */
4495
4496   if (TREE_CODE (fn) == ADDR_EXPR
4497       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4498       && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4499     {
4500       tree exp;
4501       exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4502       if (exp)
4503         return exp;
4504     }
4505
4506   /* Some built-in function calls will be evaluated at
4507      compile-time in fold ().  */
4508   fn = fold (build_call (fn, converted_args));
4509   if (VOID_TYPE_P (TREE_TYPE (fn)))
4510     return fn;
4511   fn = require_complete_type (fn);
4512   if (fn == error_mark_node)
4513     return error_mark_node;
4514   if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4515     fn = build_cplus_new (TREE_TYPE (fn), fn);
4516   return convert_from_reference (fn);
4517 }
4518
4519 static GTY(()) tree java_iface_lookup_fn;
4520
4521 /* Make an expression which yields the address of the Java interface
4522    method FN.  This is achieved by generating a call to libjava's
4523    _Jv_LookupInterfaceMethodIdx().  */
4524
4525 static tree
4526 build_java_interface_fn_ref (tree fn, tree instance)
4527 {
4528   tree lookup_args, lookup_fn, method, idx;
4529   tree klass_ref, iface, iface_ref;
4530   int i;
4531   
4532   if (!java_iface_lookup_fn)
4533     {
4534       tree endlink = build_void_list_node ();
4535       tree t = tree_cons (NULL_TREE, ptr_type_node,
4536                           tree_cons (NULL_TREE, ptr_type_node,
4537                                      tree_cons (NULL_TREE, java_int_type_node,
4538                                                 endlink)));
4539       java_iface_lookup_fn 
4540         = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4541                             build_function_type (ptr_type_node, t),
4542                             0, NOT_BUILT_IN, NULL, NULL_TREE);
4543     }
4544
4545   /* Look up the pointer to the runtime java.lang.Class object for `instance'. 
4546      This is the first entry in the vtable.  */
4547   klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0), 
4548                               integer_zero_node);
4549
4550   /* Get the java.lang.Class pointer for the interface being called.  */
4551   iface = DECL_CONTEXT (fn);
4552   iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4553   if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4554       || DECL_CONTEXT (iface_ref) != iface)
4555     {
4556       error ("could not find class$ field in java interface type `%T'", 
4557                 iface);
4558       return error_mark_node;
4559     }
4560   iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4561   
4562   /* Determine the itable index of FN.  */
4563   i = 1;
4564   for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4565     {
4566       if (!DECL_VIRTUAL_P (method))
4567         continue;
4568       if (fn == method)
4569         break;
4570       i++;
4571     }
4572   idx = build_int_2 (i, 0);
4573
4574   lookup_args = tree_cons (NULL_TREE, klass_ref, 
4575                            tree_cons (NULL_TREE, iface_ref,
4576                                       build_tree_list (NULL_TREE, idx)));
4577   lookup_fn = build1 (ADDR_EXPR, 
4578                       build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4579                       java_iface_lookup_fn);
4580   return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4581 }
4582
4583 /* Returns the value to use for the in-charge parameter when making a
4584    call to a function with the indicated NAME.  */
4585
4586 tree
4587 in_charge_arg_for_name (tree name)
4588 {
4589   if (name == base_ctor_identifier
4590       || name == base_dtor_identifier)
4591     return integer_zero_node;
4592   else if (name == complete_ctor_identifier)
4593     return integer_one_node;
4594   else if (name == complete_dtor_identifier)
4595     return integer_two_node;
4596   else if (name == deleting_dtor_identifier)
4597     return integer_three_node;
4598
4599   /* This function should only be called with one of the names listed
4600      above.  */
4601   abort ();
4602   return NULL_TREE;
4603 }
4604
4605 /* Build a call to a constructor, destructor, or an assignment
4606    operator for INSTANCE, an expression with class type.  NAME
4607    indicates the special member function to call; ARGS are the
4608    arguments.  BINFO indicates the base of INSTANCE that is to be
4609    passed as the `this' parameter to the member function called.
4610
4611    FLAGS are the LOOKUP_* flags to use when processing the call.
4612
4613    If NAME indicates a complete object constructor, INSTANCE may be
4614    NULL_TREE.  In this case, the caller will call build_cplus_new to
4615    store the newly constructed object into a VAR_DECL.  */
4616
4617 tree
4618 build_special_member_call (tree instance, tree name, tree args, 
4619                            tree binfo, int flags)
4620 {
4621   tree fns;
4622   /* The type of the subobject to be constructed or destroyed.  */
4623   tree class_type;
4624
4625   my_friendly_assert (name == complete_ctor_identifier
4626                       || name == base_ctor_identifier
4627                       || name == complete_dtor_identifier
4628                       || name == base_dtor_identifier
4629                       || name == deleting_dtor_identifier
4630                       || name == ansi_assopname (NOP_EXPR),
4631                       20020712);
4632   my_friendly_assert (binfo != NULL_TREE, 20020712);
4633
4634   class_type = BINFO_TYPE (binfo);
4635
4636   /* Handle the special case where INSTANCE is NULL_TREE.  */
4637   if (name == complete_ctor_identifier && !instance)
4638     {
4639       instance = build_int_2 (0, 0);
4640       TREE_TYPE (instance) = build_pointer_type (class_type);
4641       instance = build1 (INDIRECT_REF, class_type, instance);
4642     }
4643   else if (name == complete_dtor_identifier 
4644            || name == base_dtor_identifier
4645            || name == deleting_dtor_identifier)
4646     my_friendly_assert (args == NULL_TREE, 20020712);
4647
4648   my_friendly_assert (instance != NULL_TREE, 20020712);
4649
4650   /* Resolve the name.  */
4651   if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4652     return error_mark_node;
4653
4654   fns = lookup_fnfields (binfo, name, 1);
4655     
4656   /* When making a call to a constructor or destructor for a subobject
4657      that uses virtual base classes, pass down a pointer to a VTT for
4658      the subobject.  */
4659   if ((name == base_ctor_identifier
4660        || name == base_dtor_identifier)
4661       && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4662     {
4663       tree vtt;
4664       tree sub_vtt;
4665
4666       /* If the current function is a complete object constructor
4667          or destructor, then we fetch the VTT directly.
4668          Otherwise, we look it up using the VTT we were given.  */
4669       vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4670       vtt = decay_conversion (vtt);
4671       vtt = build (COND_EXPR, TREE_TYPE (vtt),
4672                    build (EQ_EXPR, boolean_type_node,
4673                           current_in_charge_parm, integer_zero_node),
4674                    current_vtt_parm,
4675                    vtt);
4676       if (TREE_VIA_VIRTUAL (binfo))
4677         binfo = binfo_for_vbase (class_type, current_class_type);
4678       my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4679       sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4680                        BINFO_SUBVTT_INDEX (binfo));
4681
4682       args = tree_cons (NULL_TREE, sub_vtt, args);
4683     }
4684
4685   return build_new_method_call (instance, fns, args, binfo, flags);
4686 }
4687
4688 /* Build a call to "INSTANCE.FN (ARGS)".  */
4689
4690 tree
4691 build_new_method_call (tree instance, tree fns, tree args, 
4692                        tree conversion_path, int flags)
4693 {
4694   struct z_candidate *candidates = 0, *cand;
4695   tree explicit_targs = NULL_TREE;
4696   tree basetype = NULL_TREE;
4697   tree access_binfo;
4698   tree optype;
4699   tree mem_args = NULL_TREE, instance_ptr;
4700   tree name, pretty_name;
4701   tree user_args;
4702   tree templates = NULL_TREE;
4703   tree call;
4704   int template_only = 0;
4705
4706   my_friendly_assert (instance != NULL_TREE, 20020729);
4707
4708   if (instance == error_mark_node || fns == error_mark_node 
4709       || args == error_mark_node)
4710     return error_mark_node;
4711
4712   /* Process the argument list.  */
4713   user_args = args;
4714   args = resolve_args (args);
4715   if (args == error_mark_node)
4716     return error_mark_node;
4717
4718   if (TREE_CODE (instance) == OFFSET_REF)
4719     instance = resolve_offset_ref (instance);
4720   if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4721     instance = convert_from_reference (instance);
4722   basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4723   instance_ptr = build_this (instance);
4724
4725   if (!BASELINK_P (fns))
4726     {
4727       call = build_field_call (instance_ptr, fns, args);
4728       if (call)
4729         return call;
4730       error ("call to non-function `%D'", fns);
4731       return error_mark_node;
4732     }
4733
4734   if (!conversion_path)
4735     conversion_path = BASELINK_BINFO (fns);
4736   access_binfo = BASELINK_ACCESS_BINFO (fns);
4737   optype = BASELINK_OPTYPE (fns);
4738   fns = BASELINK_FUNCTIONS (fns);
4739
4740   if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4741     {
4742       explicit_targs = TREE_OPERAND (fns, 1);
4743       fns = TREE_OPERAND (fns, 0);
4744       template_only = 1;
4745     }
4746
4747   my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4748                       || TREE_CODE (fns) == TEMPLATE_DECL
4749                       || TREE_CODE (fns) == OVERLOAD,
4750                       20020712);
4751
4752   /* XXX this should be handled before we get here.  */
4753   if (! IS_AGGR_TYPE (basetype))
4754     {
4755       if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4756         error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4757                fns, instance, basetype);
4758
4759       return error_mark_node;
4760     }
4761
4762   name = DECL_NAME (get_first_fn (fns));
4763
4764   if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4765     {
4766       /* Callers should explicitly indicate whether they want to construct
4767          the complete object or just the part without virtual bases.  */
4768       my_friendly_assert (name != ctor_identifier, 20000408);
4769       /* Similarly for destructors.  */
4770       my_friendly_assert (name != dtor_identifier, 20000408);
4771       
4772       if (name == complete_ctor_identifier
4773           || name == base_ctor_identifier)
4774         pretty_name = constructor_name (basetype);
4775       else
4776         pretty_name = dtor_identifier;
4777     }
4778   else
4779     pretty_name = name;
4780
4781   if (fns)
4782     {
4783       tree fn;
4784       tree class_type = (conversion_path 
4785                          ? BINFO_TYPE (conversion_path)
4786                          : NULL_TREE);
4787
4788       mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4789       for (fn = fns; fn; fn = OVL_NEXT (fn))
4790         {
4791           tree t = OVL_CURRENT (fn);
4792           tree this_arglist;
4793
4794           /* We can end up here for copy-init of same or base class.  */
4795           if ((flags & LOOKUP_ONLYCONVERTING)
4796               && DECL_NONCONVERTING_P (t))
4797             continue;
4798
4799           if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4800             this_arglist = mem_args;
4801           else
4802             this_arglist = args;
4803
4804           if (TREE_CODE (t) == TEMPLATE_DECL)
4805             {
4806               /* A member template.  */
4807               templates = tree_cons (NULL_TREE, t, templates);
4808               candidates = 
4809                 add_template_candidate (candidates, t, 
4810                                         class_type,
4811                                         explicit_targs,
4812                                         this_arglist, optype,
4813                                         access_binfo, 
4814                                         conversion_path,
4815                                         flags,
4816                                         DEDUCE_CALL);
4817             }
4818           else if (! template_only)
4819             candidates = add_function_candidate (candidates, t, 
4820                                                  class_type,
4821                                                  this_arglist,
4822                                                  access_binfo,
4823                                                  conversion_path,
4824                                                  flags);
4825         }
4826     }
4827
4828   if (! any_viable (candidates))
4829     {
4830       /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
4831       if (flags & LOOKUP_SPECULATIVELY)
4832         return NULL_TREE;
4833       if (!COMPLETE_TYPE_P (basetype))
4834         cxx_incomplete_type_error (instance_ptr, basetype);
4835       else
4836         error ("no matching function for call to `%T::%D(%A)%#V'",
4837                basetype, pretty_name, user_args,
4838                TREE_TYPE (TREE_TYPE (instance_ptr)));
4839       print_z_candidates (candidates);
4840       return error_mark_node;
4841     }
4842   candidates = splice_viable (candidates);
4843   cand = tourney (candidates);
4844
4845   if (cand == 0)
4846     {
4847       error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4848                 user_args);
4849       print_z_candidates (candidates);
4850       return error_mark_node;
4851     }
4852
4853   if (DECL_PURE_VIRTUAL_P (cand->fn)
4854       && instance == current_class_ref
4855       && (DECL_CONSTRUCTOR_P (current_function_decl)
4856           || DECL_DESTRUCTOR_P (current_function_decl))
4857       && ! (flags & LOOKUP_NONVIRTUAL)
4858       && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4859     error ((DECL_CONSTRUCTOR_P (current_function_decl) ? 
4860                "abstract virtual `%#D' called from constructor"
4861                : "abstract virtual `%#D' called from destructor"),
4862               cand->fn);
4863   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4864       && is_dummy_object (instance_ptr))
4865     {
4866       error ("cannot call member function `%D' without object", cand->fn);
4867       return error_mark_node;
4868     }
4869
4870   if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4871       && resolves_to_fixed_type_p (instance, 0))
4872     flags |= LOOKUP_NONVIRTUAL;
4873
4874   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4875     call = build_over_call (cand, mem_args, flags);
4876   else
4877     {
4878       call = build_over_call (cand, args, flags);
4879       /* In an expression of the form `a->f()' where `f' turns out to
4880          be a static member function, `a' is none-the-less evaluated.  */
4881       if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
4882         call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4883     }
4884
4885   return call;
4886 }
4887
4888 /* Returns true iff standard conversion sequence ICS1 is a proper
4889    subsequence of ICS2.  */
4890
4891 static bool
4892 is_subseq (tree ics1, tree ics2)
4893 {
4894   /* We can assume that a conversion of the same code
4895      between the same types indicates a subsequence since we only get
4896      here if the types we are converting from are the same.  */
4897
4898   while (TREE_CODE (ics1) == RVALUE_CONV
4899          || TREE_CODE (ics1) == LVALUE_CONV)
4900     ics1 = TREE_OPERAND (ics1, 0);
4901
4902   while (1)
4903     {
4904       while (TREE_CODE (ics2) == RVALUE_CONV
4905           || TREE_CODE (ics2) == LVALUE_CONV)
4906         ics2 = TREE_OPERAND (ics2, 0);
4907
4908       if (TREE_CODE (ics2) == USER_CONV
4909           || TREE_CODE (ics2) == AMBIG_CONV
4910           || TREE_CODE (ics2) == IDENTITY_CONV)
4911         /* At this point, ICS1 cannot be a proper subsequence of
4912            ICS2.  We can get a USER_CONV when we are comparing the
4913            second standard conversion sequence of two user conversion
4914            sequences.  */
4915         return false;
4916
4917       ics2 = TREE_OPERAND (ics2, 0);
4918
4919       if (TREE_CODE (ics2) == TREE_CODE (ics1)
4920           && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4921           && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4922                              TREE_TYPE (TREE_OPERAND (ics1, 0))))
4923         return true;
4924     }
4925 }
4926
4927 /* Returns nonzero iff DERIVED is derived from BASE.  The inputs may
4928    be any _TYPE nodes.  */
4929
4930 bool
4931 is_properly_derived_from (tree derived, tree base)
4932 {
4933   if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4934       || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4935     return false;
4936
4937   /* We only allow proper derivation here.  The DERIVED_FROM_P macro
4938      considers every class derived from itself.  */
4939   return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4940           && DERIVED_FROM_P (base, derived));
4941 }
4942
4943 /* We build the ICS for an implicit object parameter as a pointer
4944    conversion sequence.  However, such a sequence should be compared
4945    as if it were a reference conversion sequence.  If ICS is the
4946    implicit conversion sequence for an implicit object parameter,
4947    modify it accordingly.  */
4948
4949 static void
4950 maybe_handle_implicit_object (tree *ics)
4951 {
4952   if (ICS_THIS_FLAG (*ics))
4953     {
4954       /* [over.match.funcs]
4955          
4956          For non-static member functions, the type of the
4957          implicit object parameter is "reference to cv X"
4958          where X is the class of which the function is a
4959          member and cv is the cv-qualification on the member
4960          function declaration.  */
4961       tree t = *ics;
4962       tree reference_type;
4963
4964       /* The `this' parameter is a pointer to a class type.  Make the
4965          implict conversion talk about a reference to that same class
4966          type.  */
4967       reference_type = TREE_TYPE (TREE_TYPE (*ics));
4968       reference_type = build_reference_type (reference_type);
4969
4970       if (TREE_CODE (t) == QUAL_CONV)
4971         t = TREE_OPERAND (t, 0);
4972       if (TREE_CODE (t) == PTR_CONV)
4973         t = TREE_OPERAND (t, 0);
4974       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4975       t = direct_reference_binding (reference_type, t); 
4976       *ics = t;
4977     }
4978 }
4979
4980 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4981    and return the type to which the reference refers.  Otherwise,
4982    leave *ICS unchanged and return NULL_TREE.  */
4983
4984 static tree
4985 maybe_handle_ref_bind (tree *ics)
4986 {
4987   if (TREE_CODE (*ics) == REF_BIND)
4988     {
4989       tree old_ics = *ics;
4990       tree type = TREE_TYPE (TREE_TYPE (old_ics));
4991       *ics = TREE_OPERAND (old_ics, 0);
4992       ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4993       ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4994       return type;
4995     }
4996
4997   return NULL_TREE;
4998 }
4999
5000 /* Compare two implicit conversion sequences according to the rules set out in
5001    [over.ics.rank].  Return values:
5002
5003       1: ics1 is better than ics2
5004      -1: ics2 is better than ics1
5005       0: ics1 and ics2 are indistinguishable */
5006
5007 static int
5008 compare_ics (tree ics1, tree ics2)
5009 {
5010   tree from_type1;
5011   tree from_type2;
5012   tree to_type1;
5013   tree to_type2;
5014   tree deref_from_type1 = NULL_TREE;
5015   tree deref_from_type2 = NULL_TREE;
5016   tree deref_to_type1 = NULL_TREE;
5017   tree deref_to_type2 = NULL_TREE;
5018   int rank1, rank2;
5019
5020   /* REF_BINDING is nonzero if the result of the conversion sequence
5021      is a reference type.   In that case TARGET_TYPE is the
5022      type referred to by the reference.  */
5023   tree target_type1;
5024   tree target_type2;
5025
5026   /* Handle implicit object parameters.  */
5027   maybe_handle_implicit_object (&ics1);
5028   maybe_handle_implicit_object (&ics2);
5029
5030   /* Handle reference parameters.  */
5031   target_type1 = maybe_handle_ref_bind (&ics1);
5032   target_type2 = maybe_handle_ref_bind (&ics2);
5033
5034   /* [over.ics.rank]
5035
5036      When  comparing  the  basic forms of implicit conversion sequences (as
5037      defined in _over.best.ics_)
5038
5039      --a standard conversion sequence (_over.ics.scs_) is a better
5040        conversion sequence than a user-defined conversion sequence
5041        or an ellipsis conversion sequence, and
5042      
5043      --a user-defined conversion sequence (_over.ics.user_) is a
5044        better conversion sequence than an ellipsis conversion sequence
5045        (_over.ics.ellipsis_).  */
5046   rank1 = ICS_RANK (ics1);
5047   rank2 = ICS_RANK (ics2);
5048   
5049   if (rank1 > rank2)
5050     return -1;
5051   else if (rank1 < rank2)
5052     return 1;
5053
5054   if (rank1 == BAD_RANK)
5055     {
5056       /* XXX Isn't this an extension? */
5057       /* Both ICS are bad.  We try to make a decision based on what
5058          would have happenned if they'd been good.  */
5059       if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5060           || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5061         return -1;
5062       else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5063                || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5064         return 1;
5065
5066       /* We couldn't make up our minds; try to figure it out below.  */
5067     }
5068
5069   if (ICS_ELLIPSIS_FLAG (ics1))
5070     /* Both conversions are ellipsis conversions.  */
5071     return 0;
5072
5073   /* User-defined  conversion sequence U1 is a better conversion sequence
5074      than another user-defined conversion sequence U2 if they contain the
5075      same user-defined conversion operator or constructor and if the sec-
5076      ond standard conversion sequence of U1 is  better  than  the  second
5077      standard conversion sequence of U2.  */
5078
5079   if (ICS_USER_FLAG (ics1))
5080     {
5081       tree t1, t2;
5082
5083       for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5084         if (TREE_CODE (t1) == AMBIG_CONV)
5085           return 0;
5086       for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5087         if (TREE_CODE (t2) == AMBIG_CONV)
5088           return 0;
5089
5090       if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5091         return 0;
5092
5093       /* We can just fall through here, after setting up
5094          FROM_TYPE1 and FROM_TYPE2.  */
5095       from_type1 = TREE_TYPE (t1);
5096       from_type2 = TREE_TYPE (t2);
5097     }
5098   else
5099     {
5100       /* We're dealing with two standard conversion sequences. 
5101
5102          [over.ics.rank]
5103          
5104          Standard conversion sequence S1 is a better conversion
5105          sequence than standard conversion sequence S2 if
5106      
5107          --S1 is a proper subsequence of S2 (comparing the conversion
5108            sequences in the canonical form defined by _over.ics.scs_,
5109            excluding any Lvalue Transformation; the identity
5110            conversion sequence is considered to be a subsequence of
5111            any non-identity conversion sequence */
5112       
5113       from_type1 = ics1;
5114       while (TREE_CODE (from_type1) != IDENTITY_CONV)
5115         from_type1 = TREE_OPERAND (from_type1, 0);
5116       from_type1 = TREE_TYPE (from_type1);
5117       
5118       from_type2 = ics2;
5119       while (TREE_CODE (from_type2) != IDENTITY_CONV)
5120         from_type2 = TREE_OPERAND (from_type2, 0);
5121       from_type2 = TREE_TYPE (from_type2);
5122     }
5123
5124   if (same_type_p (from_type1, from_type2))
5125     {
5126       if (is_subseq (ics1, ics2))
5127         return 1;
5128       if (is_subseq (ics2, ics1))
5129         return -1;
5130     }
5131   /* Otherwise, one sequence cannot be a subsequence of the other; they
5132      don't start with the same type.  This can happen when comparing the
5133      second standard conversion sequence in two user-defined conversion
5134      sequences.  */
5135
5136   /* [over.ics.rank]
5137
5138      Or, if not that,
5139
5140      --the rank of S1 is better than the rank of S2 (by the rules
5141        defined below):
5142
5143     Standard conversion sequences are ordered by their ranks: an Exact
5144     Match is a better conversion than a Promotion, which is a better
5145     conversion than a Conversion.
5146
5147     Two conversion sequences with the same rank are indistinguishable
5148     unless one of the following rules applies:
5149
5150     --A conversion that is not a conversion of a pointer, or pointer
5151       to member, to bool is better than another conversion that is such
5152       a conversion.  
5153
5154     The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5155     so that we do not have to check it explicitly.  */
5156   if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5157     return 1;
5158   else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5159     return -1;
5160
5161   to_type1 = TREE_TYPE (ics1);
5162   to_type2 = TREE_TYPE (ics2);
5163
5164   if (TYPE_PTR_P (from_type1)
5165       && TYPE_PTR_P (from_type2)
5166       && TYPE_PTR_P (to_type1)
5167       && TYPE_PTR_P (to_type2))
5168     {
5169       deref_from_type1 = TREE_TYPE (from_type1);
5170       deref_from_type2 = TREE_TYPE (from_type2);
5171       deref_to_type1 = TREE_TYPE (to_type1);
5172       deref_to_type2 = TREE_TYPE (to_type2);
5173     }
5174   /* The rules for pointers to members A::* are just like the rules
5175      for pointers A*, except opposite: if B is derived from A then
5176      A::* converts to B::*, not vice versa.  For that reason, we
5177      switch the from_ and to_ variables here.  */
5178   else if (TYPE_PTRMEM_P (from_type1)
5179            && TYPE_PTRMEM_P (from_type2)
5180            && TYPE_PTRMEM_P (to_type1)
5181            && TYPE_PTRMEM_P (to_type2))
5182     {
5183       deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5184       deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5185       deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5186       deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5187     }
5188   else if (TYPE_PTRMEMFUNC_P (from_type1)
5189            && TYPE_PTRMEMFUNC_P (from_type2)
5190            && TYPE_PTRMEMFUNC_P (to_type1)
5191            && TYPE_PTRMEMFUNC_P (to_type2))
5192     {
5193       deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5194       deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5195       deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5196       deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5197     }
5198
5199   if (deref_from_type1 != NULL_TREE
5200       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5201       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5202     {
5203       /* This was one of the pointer or pointer-like conversions.  
5204
5205          [over.ics.rank]
5206          
5207          --If class B is derived directly or indirectly from class A,
5208            conversion of B* to A* is better than conversion of B* to
5209            void*, and conversion of A* to void* is better than
5210            conversion of B* to void*.  */
5211       if (TREE_CODE (deref_to_type1) == VOID_TYPE
5212           && TREE_CODE (deref_to_type2) == VOID_TYPE)
5213         {
5214           if (is_properly_derived_from (deref_from_type1,
5215                                         deref_from_type2))
5216             return -1;
5217           else if (is_properly_derived_from (deref_from_type2,
5218                                              deref_from_type1))
5219             return 1;
5220         }
5221       else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5222                || TREE_CODE (deref_to_type2) == VOID_TYPE)
5223         {
5224           if (same_type_p (deref_from_type1, deref_from_type2))
5225             {
5226               if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5227                 {
5228                   if (is_properly_derived_from (deref_from_type1,
5229                                                 deref_to_type1))
5230                     return 1;
5231                 }
5232               /* We know that DEREF_TO_TYPE1 is `void' here.  */
5233               else if (is_properly_derived_from (deref_from_type1,
5234                                                  deref_to_type2))
5235                 return -1;
5236             }
5237         }
5238       else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5239                && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5240         {
5241           /* [over.ics.rank]
5242
5243              --If class B is derived directly or indirectly from class A
5244                and class C is derived directly or indirectly from B,
5245              
5246              --conversion of C* to B* is better than conversion of C* to
5247                A*, 
5248              
5249              --conversion of B* to A* is better than conversion of C* to
5250                A*  */
5251           if (same_type_p (deref_from_type1, deref_from_type2))
5252             {
5253               if (is_properly_derived_from (deref_to_type1,
5254                                             deref_to_type2))
5255                 return 1;
5256               else if (is_properly_derived_from (deref_to_type2,
5257                                                  deref_to_type1))
5258                 return -1;
5259             }
5260           else if (same_type_p (deref_to_type1, deref_to_type2))
5261             {
5262               if (is_properly_derived_from (deref_from_type2,
5263                                             deref_from_type1))
5264                 return 1;
5265               else if (is_properly_derived_from (deref_from_type1,
5266                                                  deref_from_type2))
5267                 return -1;
5268             }
5269         }
5270     }
5271   else if (CLASS_TYPE_P (non_reference (from_type1))
5272            && same_type_p (from_type1, from_type2))
5273     {
5274       tree from = non_reference (from_type1);
5275
5276       /* [over.ics.rank]
5277          
5278          --binding of an expression of type C to a reference of type
5279            B& is better than binding an expression of type C to a
5280            reference of type A&
5281
5282          --conversion of C to B is better than conversion of C to A,  */
5283       if (is_properly_derived_from (from, to_type1)
5284           && is_properly_derived_from (from, to_type2))
5285         {
5286           if (is_properly_derived_from (to_type1, to_type2))
5287             return 1;
5288           else if (is_properly_derived_from (to_type2, to_type1))
5289             return -1;
5290         }
5291     }
5292   else if (CLASS_TYPE_P (non_reference (to_type1))
5293            && same_type_p (to_type1, to_type2))
5294     {
5295       tree to = non_reference (to_type1);
5296
5297       /* [over.ics.rank]
5298
5299          --binding of an expression of type B to a reference of type
5300            A& is better than binding an expression of type C to a
5301            reference of type A&, 
5302
5303          --onversion of B to A is better than conversion of C to A  */
5304       if (is_properly_derived_from (from_type1, to)
5305           && is_properly_derived_from (from_type2, to))
5306         {
5307           if (is_properly_derived_from (from_type2, from_type1))
5308             return 1;
5309           else if (is_properly_derived_from (from_type1, from_type2))
5310             return -1;
5311         }
5312     }
5313
5314   /* [over.ics.rank]
5315
5316      --S1 and S2 differ only in their qualification conversion and  yield
5317        similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
5318        qualification signature of type T1 is a proper subset of  the  cv-
5319        qualification signature of type T2  */
5320   if (TREE_CODE (ics1) == QUAL_CONV 
5321       && TREE_CODE (ics2) == QUAL_CONV
5322       && same_type_p (from_type1, from_type2))
5323     return comp_cv_qual_signature (to_type1, to_type2);
5324
5325   /* [over.ics.rank]
5326      
5327      --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5328      types to which the references refer are the same type except for
5329      top-level cv-qualifiers, and the type to which the reference
5330      initialized by S2 refers is more cv-qualified than the type to
5331      which the reference initialized by S1 refers */
5332       
5333   if (target_type1 && target_type2
5334       && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5335     return comp_cv_qualification (target_type2, target_type1);
5336
5337   /* Neither conversion sequence is better than the other.  */
5338   return 0;
5339 }
5340
5341 /* The source type for this standard conversion sequence.  */
5342
5343 static tree
5344 source_type (tree t)
5345 {
5346   for (;; t = TREE_OPERAND (t, 0))
5347     {
5348       if (TREE_CODE (t) == USER_CONV
5349           || TREE_CODE (t) == AMBIG_CONV
5350           || TREE_CODE (t) == IDENTITY_CONV)
5351         return TREE_TYPE (t);
5352     }
5353   abort ();
5354 }
5355
5356 /* Note a warning about preferring WINNER to LOSER.  We do this by storing
5357    a pointer to LOSER and re-running joust to produce the warning if WINNER
5358    is actually used.  */
5359
5360 static void
5361 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5362 {
5363   winner->warnings = tree_cons (NULL_TREE,
5364                                 build_zc_wrapper (loser),
5365                                 winner->warnings);
5366 }
5367
5368 /* Returns true iff functions are equivalent. Equivalent functions are
5369    not '==' only if one is a function-local extern function or if
5370    both are extern "C".  */
5371
5372 static inline int
5373 equal_functions (tree fn1, tree fn2)
5374 {
5375   if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5376       || DECL_EXTERN_C_FUNCTION_P (fn1))
5377     return decls_match (fn1, fn2);
5378   return fn1 == fn2;
5379 }
5380
5381 /* Compare two candidates for overloading as described in
5382    [over.match.best].  Return values:
5383
5384       1: cand1 is better than cand2
5385      -1: cand2 is better than cand1
5386       0: cand1 and cand2 are indistinguishable */
5387
5388 static int
5389 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5390 {
5391   int winner = 0;
5392   int i, off1 = 0, off2 = 0, len;
5393
5394   /* Candidates that involve bad conversions are always worse than those
5395      that don't.  */
5396   if (cand1->viable > cand2->viable)
5397     return 1;
5398   if (cand1->viable < cand2->viable)
5399     return -1;
5400
5401   /* If we have two pseudo-candidates for conversions to the same type,
5402      or two candidates for the same function, arbitrarily pick one.  */
5403   if (cand1->fn == cand2->fn
5404       && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5405     return 1;
5406
5407   /* a viable function F1
5408      is defined to be a better function than another viable function F2  if
5409      for  all arguments i, ICSi(F1) is not a worse conversion sequence than
5410      ICSi(F2), and then */
5411
5412   /* for some argument j, ICSj(F1) is a better conversion  sequence  than
5413      ICSj(F2) */
5414
5415   /* For comparing static and non-static member functions, we ignore
5416      the implicit object parameter of the non-static function.  The
5417      standard says to pretend that the static function has an object
5418      parm, but that won't work with operator overloading.  */
5419   len = TREE_VEC_LENGTH (cand1->convs);
5420   if (len != TREE_VEC_LENGTH (cand2->convs))
5421     {
5422       if (DECL_STATIC_FUNCTION_P (cand1->fn)
5423           && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5424         off2 = 1;
5425       else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5426                && DECL_STATIC_FUNCTION_P (cand2->fn))
5427         {
5428           off1 = 1;
5429           --len;
5430         }
5431       else
5432         abort ();
5433     }
5434
5435   for (i = 0; i < len; ++i)
5436     {
5437       tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5438       tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5439       int comp = compare_ics (t1, t2);
5440
5441       if (comp != 0)
5442         {
5443           if (warn_sign_promo
5444               && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5445               && TREE_CODE (t1) == STD_CONV
5446               && TREE_CODE (t2) == STD_CONV
5447               && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5448               && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5449               && (TYPE_PRECISION (TREE_TYPE (t1))
5450                   == TYPE_PRECISION (TREE_TYPE (t2)))
5451               && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5452                   || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5453                       == ENUMERAL_TYPE)))
5454             {
5455               tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5456               tree type1, type2;
5457               struct z_candidate *w, *l;
5458               if (comp > 0)
5459                 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5460                   w = cand1, l = cand2;
5461               else
5462                 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5463                   w = cand2, l = cand1;
5464
5465               if (warn)
5466                 {
5467                   warning ("passing `%T' chooses `%T' over `%T'",
5468                               type, type1, type2);
5469                   warning ("  in call to `%D'", w->fn);
5470                 }
5471               else
5472                 add_warning (w, l);
5473             }
5474
5475           if (winner && comp != winner)
5476             {
5477               winner = 0;
5478               goto tweak;
5479             }
5480           winner = comp;
5481         }
5482     }
5483
5484   /* warn about confusing overload resolution for user-defined conversions,
5485      either between a constructor and a conversion op, or between two
5486      conversion ops.  */
5487   if (winner && cand1->second_conv
5488       && ((DECL_CONSTRUCTOR_P (cand1->fn)
5489            != DECL_CONSTRUCTOR_P (cand2->fn))
5490           /* Don't warn if the two conv ops convert to the same type...  */
5491           || (! DECL_CONSTRUCTOR_P (cand1->fn)
5492               && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5493                                 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5494     {
5495       int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5496       if (comp != winner)
5497         {
5498           struct z_candidate *w, *l;
5499           tree convn;
5500           if (winner == 1)
5501             w = cand1, l = cand2;
5502           else
5503             w = cand2, l = cand1;
5504           if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5505               && ! DECL_CONSTRUCTOR_P (cand1->fn)
5506               && ! DECL_CONSTRUCTOR_P (cand2->fn)
5507               && (convn = standard_conversion
5508                   (TREE_TYPE (TREE_TYPE (l->fn)),
5509                    TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5510               && TREE_CODE (convn) == QUAL_CONV)
5511             /* Don't complain about `operator char *()' beating
5512                `operator const char *() const'.  */;
5513           else if (warn)
5514             {
5515               tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5516               if (! DECL_CONSTRUCTOR_P (w->fn))
5517                 source = TREE_TYPE (source);
5518               warning ("choosing `%D' over `%D'", w->fn, l->fn);
5519               warning ("  for conversion from `%T' to `%T'",
5520                           source, TREE_TYPE (w->second_conv));
5521               warning ("  because conversion sequence for the argument is better");
5522             }
5523           else
5524             add_warning (w, l);
5525         }
5526     }
5527
5528   if (winner)
5529     return winner;
5530
5531   /* or, if not that,
5532      F1 is a non-template function and F2 is a template function
5533      specialization.  */
5534          
5535   if (! cand1->template && cand2->template)
5536     return 1;
5537   else if (cand1->template && ! cand2->template)
5538     return -1;
5539   
5540   /* or, if not that,
5541      F1 and F2 are template functions and the function template for F1 is
5542      more specialized than the template for F2 according to the partial
5543      ordering rules.  */
5544   
5545   if (cand1->template && cand2->template)
5546     {
5547       winner = more_specialized
5548         (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5549          DEDUCE_ORDER,
5550          /* Tell the deduction code how many real function arguments
5551             we saw, not counting the implicit 'this' argument.  But,
5552             add_function_candidate() suppresses the "this" argument
5553             for constructors.
5554
5555             [temp.func.order]: The presence of unused ellipsis and default
5556             arguments has no effect on the partial ordering of function
5557             templates.  */
5558          TREE_VEC_LENGTH (cand1->convs)
5559          - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5560             - DECL_CONSTRUCTOR_P (cand1->fn)));
5561       /* HERE */
5562       if (winner)
5563         return winner;
5564     }
5565
5566   /* or, if not that,
5567      the  context  is  an  initialization by user-defined conversion (see
5568      _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
5569      sequence  from  the return type of F1 to the destination type (i.e.,
5570      the type of the entity being initialized)  is  a  better  conversion
5571      sequence  than the standard conversion sequence from the return type
5572      of F2 to the destination type.  */
5573
5574   if (cand1->second_conv)
5575     {
5576       winner = compare_ics (cand1->second_conv, cand2->second_conv);
5577       if (winner)
5578         return winner;
5579     }
5580   
5581   /* Check whether we can discard a builtin candidate, either because we
5582      have two identical ones or matching builtin and non-builtin candidates.
5583
5584      (Pedantically in the latter case the builtin which matched the user
5585      function should not be added to the overload set, but we spot it here.
5586      
5587      [over.match.oper]
5588      ... the builtin candidates include ...
5589      - do not have the same parameter type list as any non-template
5590        non-member candidate.  */
5591                             
5592   if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5593       || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5594     {
5595       for (i = 0; i < len; ++i)
5596         if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5597                           TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5598           break;
5599       if (i == TREE_VEC_LENGTH (cand1->convs))
5600         {
5601           if (cand1->fn == cand2->fn)
5602             /* Two built-in candidates; arbitrarily pick one.  */
5603             return 1;
5604           else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5605             /* cand1 is built-in; prefer cand2.  */
5606             return -1;
5607           else
5608             /* cand2 is built-in; prefer cand1.  */
5609             return 1;
5610         }
5611     }
5612
5613   /* If the two functions are the same (this can happen with declarations
5614      in multiple scopes and arg-dependent lookup), arbitrarily choose one.  */
5615   if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5616       && equal_functions (cand1->fn, cand2->fn))
5617     return 1;
5618
5619 tweak:
5620
5621   /* Extension: If the worst conversion for one candidate is worse than the
5622      worst conversion for the other, take the first.  */
5623   if (!pedantic)
5624     {
5625       int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5626       struct z_candidate *w = 0, *l = 0;
5627
5628       for (i = 0; i < len; ++i)
5629         {
5630           if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5631             rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5632           if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5633             rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5634         }
5635       if (rank1 < rank2)
5636         winner = 1, w = cand1, l = cand2;
5637       if (rank1 > rank2)
5638         winner = -1, w = cand2, l = cand1;
5639       if (winner)
5640         {
5641           if (warn)
5642             {
5643               pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5644               pedwarn (
5645 "  because worst conversion for the former is better than worst conversion for the latter");
5646             }
5647           else
5648             add_warning (w, l);
5649           return winner;
5650         }
5651     }
5652
5653   my_friendly_assert (!winner, 20010121);
5654   return 0;
5655 }
5656
5657 /* Given a list of candidates for overloading, find the best one, if any.
5658    This algorithm has a worst case of O(2n) (winner is last), and a best
5659    case of O(n/2) (totally ambiguous); much better than a sorting
5660    algorithm.  */
5661
5662 static struct z_candidate *
5663 tourney (struct z_candidate *candidates)
5664 {
5665   struct z_candidate *champ = candidates, *challenger;
5666   int fate;
5667   int champ_compared_to_predecessor = 0;
5668
5669   /* Walk through the list once, comparing each current champ to the next
5670      candidate, knocking out a candidate or two with each comparison.  */
5671
5672   for (challenger = champ->next; challenger; )
5673     {
5674       fate = joust (champ, challenger, 0);
5675       if (fate == 1)
5676         challenger = challenger->next;
5677       else
5678         {
5679           if (fate == 0)
5680             {
5681               champ = challenger->next;
5682               if (champ == 0)
5683                 return 0;
5684               champ_compared_to_predecessor = 0;
5685             }
5686           else
5687             {
5688               champ = challenger;
5689               champ_compared_to_predecessor = 1;
5690             }
5691
5692           challenger = champ->next;
5693         }
5694     }
5695
5696   /* Make sure the champ is better than all the candidates it hasn't yet
5697      been compared to.  */
5698
5699   for (challenger = candidates; 
5700        challenger != champ 
5701          && !(champ_compared_to_predecessor && challenger->next == champ);
5702        challenger = challenger->next)
5703     {
5704       fate = joust (champ, challenger, 0);
5705       if (fate != 1)
5706         return 0;
5707     }
5708
5709   return champ;
5710 }
5711
5712 /* Returns nonzero if things of type FROM can be converted to TO.  */
5713
5714 bool
5715 can_convert (tree to, tree from)
5716 {
5717   return can_convert_arg (to, from, NULL_TREE);
5718 }
5719
5720 /* Returns nonzero if ARG (of type FROM) can be converted to TO.  */
5721
5722 bool
5723 can_convert_arg (tree to, tree from, tree arg)
5724 {
5725   tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5726   return (t && ! ICS_BAD_FLAG (t));
5727 }
5728
5729 /* Like can_convert_arg, but allows dubious conversions as well.  */
5730
5731 bool
5732 can_convert_arg_bad (tree to, tree from, tree arg)
5733 {
5734   return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
5735 }
5736
5737 /* Convert EXPR to TYPE.  Return the converted expression.
5738
5739    Note that we allow bad conversions here because by the time we get to
5740    this point we are committed to doing the conversion.  If we end up
5741    doing a bad conversion, convert_like will complain.  */
5742
5743 tree
5744 perform_implicit_conversion (tree type, tree expr)
5745 {
5746   tree conv;
5747   
5748   if (expr == error_mark_node)
5749     return error_mark_node;
5750   conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5751                               LOOKUP_NORMAL);
5752   if (!conv)
5753     {
5754       error ("could not convert `%E' to `%T'", expr, type);
5755       return error_mark_node;
5756     }
5757
5758   return convert_like (conv, expr);
5759 }
5760
5761 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5762    initializing a variable of that TYPE.  Return the converted
5763    expression.  */
5764
5765 tree
5766 initialize_reference (tree type, tree expr)
5767 {
5768   tree conv;
5769
5770   conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5771   if (!conv || ICS_BAD_FLAG (conv))
5772     {
5773       error ("could not convert `%E' to `%T'", expr, type);
5774       return error_mark_node;
5775     }
5776
5777   return convert_like (conv, expr);
5778 }
5779
5780 #include "gt-cp-call.h"