OSDN Git Service

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