OSDN Git Service

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