OSDN Git Service

* decl.c (cplus_expand_expr_stmt): Always complain about unresolved
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
1 /* Functions related to invoking methods and overloaded functions.
2    Copyright (C) 1987, 92-97, 1998 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com) and
4    modified by Brendan Kehoe (brendan@cygnus.com).
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* High-level class interface.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "output.h"
31 #include "flags.h"
32 #include "rtl.h"
33 #include "toplev.h"
34
35 #include "obstack.h"
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
38
39 extern int inhibit_warnings;
40 extern tree ctor_label, dtor_label;
41
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
43
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static tree find_scoped_type PROTO((tree, tree, tree));
46 static struct z_candidate * tourney PROTO((struct z_candidate *));
47 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
48 static int compare_ics PROTO((tree, tree));
49 static tree build_over_call PROTO((struct z_candidate *, tree, int));
50 static tree convert_like PROTO((tree, tree));
51 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
52                             tree, char *));
53 static tree build_object_call PROTO((tree, tree));
54 static tree resolve_args PROTO((tree));
55 static struct z_candidate * build_user_type_conversion_1
56         PROTO ((tree, tree, int));
57 static void print_z_candidates PROTO((struct z_candidate *));
58 static tree build_this PROTO((tree));
59 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
60 static int any_viable PROTO((struct z_candidate *));
61 static struct z_candidate * add_template_candidate
62         PROTO((struct z_candidate *, tree, tree, tree, tree, int,
63                unification_kind_t));
64 static struct z_candidate * add_template_candidate_real
65         PROTO((struct z_candidate *, tree, tree, tree, tree, int,
66                tree, unification_kind_t));
67 static struct z_candidate * add_template_conv_candidate 
68         PROTO((struct z_candidate *, tree, tree, tree, tree));
69 static struct z_candidate * add_builtin_candidates
70         PROTO((struct z_candidate *, enum tree_code, enum tree_code,
71                tree, tree *, int));
72 static struct z_candidate * add_builtin_candidate
73         PROTO((struct z_candidate *, enum tree_code, enum tree_code,
74                tree, tree, tree, tree *, tree *, int));
75 static int is_complete PROTO((tree));
76 static struct z_candidate * build_builtin_candidate 
77         PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
78                int));
79 static struct z_candidate * add_conv_candidate 
80         PROTO((struct z_candidate *, tree, tree, tree));
81 static struct z_candidate * add_function_candidate 
82         PROTO((struct z_candidate *, tree, tree, int));
83 static tree implicit_conversion PROTO((tree, tree, tree, int));
84 static tree standard_conversion PROTO((tree, tree, tree));
85 static tree reference_binding PROTO((tree, tree, tree, int));
86 static tree strip_top_quals PROTO((tree));
87 static tree non_reference PROTO((tree));
88 static tree build_conv PROTO((enum tree_code, tree, tree));
89 static int is_subseq PROTO((tree, tree));
90 static int is_properly_derived_from PROTO((tree, tree));
91 static int maybe_handle_ref_bind PROTO((tree*, tree*));
92 static void maybe_handle_implicit_object PROTO((tree*));
93
94 tree
95 build_vfield_ref (datum, type)
96      tree datum, type;
97 {
98   tree rval;
99
100   if (datum == error_mark_node)
101     return error_mark_node;
102
103   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
104     datum = convert_from_reference (datum);
105
106   if (! TYPE_USES_COMPLEX_INHERITANCE (type))
107     rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
108                   datum, CLASSTYPE_VFIELD (type));
109   else
110     rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
111
112   return rval;
113 }
114
115 /* Build a call to a member of an object.  I.e., one that overloads
116    operator ()(), or is a pointer-to-function or pointer-to-method.  */
117
118 static tree
119 build_field_call (basetype_path, instance_ptr, name, parms)
120      tree basetype_path, instance_ptr, name, parms;
121 {
122   tree field, instance;
123
124   if (name == ctor_identifier || name == dtor_identifier)
125     return NULL_TREE;
126
127   if (instance_ptr == current_class_ptr)
128     {
129       /* Check to see if we really have a reference to an instance variable
130          with `operator()()' overloaded.  */
131       field = IDENTIFIER_CLASS_VALUE (name);
132
133       if (field == NULL_TREE)
134         {
135           cp_error ("`this' has no member named `%D'", name);
136           return error_mark_node;
137         }
138
139       if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
140         {
141           /* If it's a field, try overloading operator (),
142              or calling if the field is a pointer-to-function.  */
143           instance = build_component_ref_1 (current_class_ref, field, 0);
144           if (instance == error_mark_node)
145             return error_mark_node;
146
147           if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance)))
148             return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
149
150           if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
151             {
152               if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
153                 return build_function_call (instance, parms);
154               else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
155                 return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms));
156             }
157         }
158       return NULL_TREE;
159     }
160
161   /* Check to see if this is not really a reference to an instance variable
162      with `operator()()' overloaded.  */
163   field = lookup_field (basetype_path, name, 1, 0);
164
165   /* This can happen if the reference was ambiguous or for access
166      violations.  */
167   if (field == error_mark_node)
168     return error_mark_node;
169
170   if (field && (TREE_CODE (field) == FIELD_DECL ||
171                 TREE_CODE (field) == VAR_DECL))
172     {
173       tree basetype;
174       tree ftype = TREE_TYPE (field);
175
176       if (TREE_CODE (ftype) == REFERENCE_TYPE)
177         ftype = TREE_TYPE (ftype);
178
179       if (TYPE_LANG_SPECIFIC (ftype))
180         {
181           /* Make the next search for this field very short.  */
182           basetype = DECL_FIELD_CONTEXT (field);
183           instance_ptr = convert_pointer_to (basetype, instance_ptr);
184
185           instance = build_indirect_ref (instance_ptr, NULL_PTR);
186           return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
187                                  build_component_ref_1 (instance, field, 0),
188                                  parms, NULL_TREE);
189         }
190       if (TREE_CODE (ftype) == POINTER_TYPE)
191         {
192           if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
193               || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
194             {
195               /* This is a member which is a pointer to function.  */
196               tree ref
197                 = build_component_ref_1 (build_indirect_ref (instance_ptr,
198                                                              NULL_PTR),
199                                          field, LOOKUP_COMPLAIN);
200               if (ref == error_mark_node)
201                 return error_mark_node;
202               return build_function_call (ref, parms);
203             }
204         }
205       else if (TREE_CODE (ftype) == METHOD_TYPE)
206         {
207           error ("invalid call via pointer-to-member function");
208           return error_mark_node;
209         }
210       else
211         return NULL_TREE;
212     }
213   return NULL_TREE;
214 }
215
216 static tree
217 find_scoped_type (type, inner_name, inner_types)
218      tree type, inner_name, inner_types;
219 {
220   tree tags = CLASSTYPE_TAGS (type);
221
222   while (tags)
223     {
224       /* The TREE_PURPOSE of an enum tag (which becomes a member of the
225          enclosing class) is set to the name for the enum type.  So, if
226          inner_name is `bar', and we strike `baz' for `enum bar { baz }',
227          then this test will be true.  */
228       if (TREE_PURPOSE (tags) == inner_name)
229         {
230           if (inner_types == NULL_TREE)
231             return TYPE_MAIN_DECL (TREE_VALUE (tags));
232           return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
233         }
234       tags = TREE_CHAIN (tags);
235     }
236
237   /* Look for a TYPE_DECL.  */
238   for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
239     if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
240       {
241         /* Code by raeburn.  */
242         if (inner_types == NULL_TREE)
243           return tags;
244         return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
245       }
246
247   return NULL_TREE;
248 }
249
250 /* Resolve an expression NAME1::NAME2::...::NAMEn to
251    the name that names the above nested type.  INNER_TYPES
252    is a chain of nested type names (held together by SCOPE_REFs);
253    OUTER_TYPE is the type we know to enclose INNER_TYPES.
254    Returns NULL_TREE if there is an error.  */
255
256 tree
257 resolve_scope_to_name (outer_type, inner_stuff)
258      tree outer_type, inner_stuff;
259 {
260   register tree tmp;
261   tree inner_name, inner_type;
262
263   if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
264     {
265       /* We first try to look for a nesting in our current class context,
266          then try any enclosing classes.  */
267       tree type = current_class_type;
268       
269       while (type && (TREE_CODE (type) == RECORD_TYPE
270                       || TREE_CODE (type) == UNION_TYPE))
271         {
272           tree rval = resolve_scope_to_name (type, inner_stuff);
273
274           if (rval != NULL_TREE)
275             return rval;
276           type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
277         }
278     }
279
280   if (TREE_CODE (inner_stuff) == SCOPE_REF)
281     {
282       inner_name = TREE_OPERAND (inner_stuff, 0);
283       inner_type = TREE_OPERAND (inner_stuff, 1);
284     }
285   else
286     {
287       inner_name = inner_stuff;
288       inner_type = NULL_TREE;
289     }
290
291   if (outer_type == NULL_TREE)
292     {
293       tree x;
294       /* If we have something that's already a type by itself,
295          use that.  */
296       if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
297         {
298           if (inner_type)
299             return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
300                                           inner_type);
301           return inner_name;
302         }
303       
304       x = lookup_name (inner_name, 0);
305
306       if (x && TREE_CODE (x) == NAMESPACE_DECL)
307         {
308           x = lookup_namespace_name (x, inner_type);
309           return x;
310         }
311       return NULL_TREE;
312     }
313
314   if (! IS_AGGR_TYPE (outer_type))
315     return NULL_TREE;
316
317   /* Look for member classes or enums.  */
318   tmp = find_scoped_type (outer_type, inner_name, inner_type);
319
320   /* If it's not a type in this class, then go down into the
321      base classes and search there.  */
322   if (! tmp && TYPE_BINFO (outer_type))
323     {
324       tree binfos = TYPE_BINFO_BASETYPES (outer_type);
325       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
326
327       for (i = 0; i < n_baselinks; i++)
328         {
329           tree base_binfo = TREE_VEC_ELT (binfos, i);
330           tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
331           if (tmp)
332             return tmp;
333         }
334       tmp = NULL_TREE;
335     }
336
337   return tmp;
338 }
339
340 /* Returns nonzero iff the destructor name specified in NAME
341    (a BIT_NOT_EXPR) matches BASETYPE.  The operand of NAME can take many
342    forms...  */
343
344 int
345 check_dtor_name (basetype, name)
346      tree basetype, name;
347 {
348   name = TREE_OPERAND (name, 0);
349
350   if (TREE_CODE (name) == TYPE_DECL)
351     name = TREE_TYPE (name);
352   else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
353     /* OK */;
354   else if (TREE_CODE (name) == IDENTIFIER_NODE)
355     {
356       if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
357           || (TREE_CODE (basetype) == ENUMERAL_TYPE
358               && name == TYPE_IDENTIFIER (basetype)))
359         name = basetype;
360       else
361         name = get_type_value (name);
362     }
363   else
364     my_friendly_abort (980605);
365
366   if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
367     return 1;
368   return 0;
369 }
370
371 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
372    This is how virtual function calls are avoided.  */
373
374 tree
375 build_scoped_method_call (exp, basetype, name, parms)
376      tree exp, basetype, name, parms;
377 {
378   /* Because this syntactic form does not allow
379      a pointer to a base class to be `stolen',
380      we need not protect the derived->base conversion
381      that happens here.
382      
383      @@ But we do have to check access privileges later.  */
384   tree binfo, decl;
385   tree type = TREE_TYPE (exp);
386
387   if (type == error_mark_node
388       || basetype == error_mark_node)
389     return error_mark_node;
390
391   if (processing_template_decl)
392     {
393       if (TREE_CODE (name) == BIT_NOT_EXPR
394           && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
395         {
396           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
397           if (type)
398             name = build_min_nt (BIT_NOT_EXPR, type);
399         }
400       name = build_min_nt (SCOPE_REF, basetype, name);
401       return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
402     }
403
404   if (TREE_CODE (type) == REFERENCE_TYPE)
405     type = TREE_TYPE (type);
406
407   if (TREE_CODE (basetype) == TREE_VEC)
408     {
409       binfo = basetype;
410       basetype = BINFO_TYPE (binfo);
411     }
412   else
413     binfo = NULL_TREE;
414
415   /* Check the destructor call syntax.  */
416   if (TREE_CODE (name) == BIT_NOT_EXPR)
417     {
418       /* We can get here if someone writes their destructor call like
419          `obj.NS::~T()'; this isn't really a scoped method call, so hand
420          it off.  */
421       if (TREE_CODE (basetype) == NAMESPACE_DECL)
422         return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
423
424       if (! check_dtor_name (basetype, name))
425         cp_error ("qualified type `%T' does not match destructor name `~%T'",
426                   basetype, TREE_OPERAND (name, 0));
427
428       /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
429          that explicit ~int is caught in the parser; this deals with typedefs
430          and template parms.  */
431       if (! IS_AGGR_TYPE (basetype))
432         {
433           if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
434             cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
435                       exp, basetype, type);
436
437           return cp_convert (void_type_node, exp);
438         }
439     }
440
441   if (! is_aggr_type (basetype, 1))
442     return error_mark_node;
443
444   if (! IS_AGGR_TYPE (type))
445     {
446       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
447                 exp, type);
448       return error_mark_node;
449     }
450
451   if (! binfo)
452     {
453       binfo = get_binfo (basetype, type, 1);
454       if (binfo == error_mark_node)
455         return error_mark_node;
456       if (! binfo)
457         error_not_base_type (basetype, type);
458     }
459
460   if (binfo)
461     {
462       if (TREE_CODE (exp) == INDIRECT_REF)
463         decl = build_indirect_ref
464           (convert_pointer_to_real
465            (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
466       else
467         decl = build_scoped_ref (exp, basetype);
468
469       /* Call to a destructor.  */
470       if (TREE_CODE (name) == BIT_NOT_EXPR)
471         {
472           if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
473             return cp_convert (void_type_node, exp);
474           
475           return build_delete (TREE_TYPE (decl), decl, integer_two_node,
476                                LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
477                                0);
478         }
479
480       /* Call to a method.  */
481       return build_method_call (decl, name, parms, binfo,
482                                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
483     }
484   return error_mark_node;
485 }
486
487 /* We want the address of a function or method.  We avoid creating a
488    pointer-to-member function.  */
489
490 tree
491 build_addr_func (function)
492      tree function;
493 {
494   tree type = TREE_TYPE (function);
495
496   /* We have to do these by hand to avoid real pointer to member
497      functions.  */
498   if (TREE_CODE (type) == METHOD_TYPE)
499     {
500       tree addr;
501
502       type = build_pointer_type (type);
503
504       if (mark_addressable (function) == 0)
505         return error_mark_node;
506
507       addr = build1 (ADDR_EXPR, type, function);
508
509       /* Address of a static or external variable or function counts
510          as a constant */
511       if (staticp (function))
512         TREE_CONSTANT (addr) = 1;
513
514       function = addr;
515     }
516   else
517     function = default_conversion (function);
518
519   return function;
520 }
521
522 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
523    POINTER_TYPE to those.  Note, pointer to member function types
524    (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
525
526 tree
527 build_call (function, result_type, parms)
528      tree function, result_type, parms;
529 {
530   int is_constructor = 0;
531   tree tmp;
532   tree decl;
533
534   function = build_addr_func (function);
535
536   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
537     {
538       sorry ("unable to call pointer to member function here");
539       return error_mark_node;
540     }
541
542   if (TREE_CODE (function) == ADDR_EXPR
543       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
544     decl = TREE_OPERAND (function, 0);
545   else
546     decl = NULL_TREE;
547
548   if (decl && DECL_CONSTRUCTOR_P (decl))
549     is_constructor = 1;
550
551   /* Don't pass empty class objects by value.  This is useful
552      for tags in STL, which are used to control overload resolution.
553      We don't need to handle other cases of copying empty classes.  */
554   if (! decl || ! DECL_BUILT_IN (decl))
555     for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
556       if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
557           && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
558         {
559           tree t = make_node (RTL_EXPR);
560           TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
561           RTL_EXPR_RTL (t) = const0_rtx;
562           RTL_EXPR_SEQUENCE (t) = NULL_RTX;
563           TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
564                                     TREE_VALUE (tmp), t);
565         }
566
567   function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
568   TREE_HAS_CONSTRUCTOR (function) = is_constructor;
569   TREE_TYPE (function) = result_type;
570   TREE_SIDE_EFFECTS (function) = 1;
571   
572   return function;
573 }
574
575 /* Build something of the form ptr->method (args)
576    or object.method (args).  This can also build
577    calls to constructors, and find friends.
578
579    Member functions always take their class variable
580    as a pointer.
581
582    INSTANCE is a class instance.
583
584    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
585
586    PARMS help to figure out what that NAME really refers to.
587
588    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
589    down to the real instance type to use for access checking.  We need this
590    information to get protected accesses correct.  This parameter is used
591    by build_member_call.
592
593    FLAGS is the logical disjunction of zero or more LOOKUP_
594    flags.  See cp-tree.h for more info.
595
596    If this is all OK, calls build_function_call with the resolved
597    member function.
598
599    This function must also handle being called to perform
600    initialization, promotion/coercion of arguments, and
601    instantiation of default parameters.
602
603    Note that NAME may refer to an instance variable name.  If
604    `operator()()' is defined for the type of that field, then we return
605    that result.  */
606
607 tree
608 build_method_call (instance, name, parms, basetype_path, flags)
609      tree instance, name, parms, basetype_path;
610      int flags;
611 {
612   tree basetype, instance_ptr;
613
614 #ifdef GATHER_STATISTICS
615   n_build_method_call++;
616 #endif
617
618   if (instance == error_mark_node
619       || name == error_mark_node
620       || parms == error_mark_node
621       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
622     return error_mark_node;
623
624   if (processing_template_decl)
625     {
626       /* We need to process template parm names here so that tsubst catches
627          them properly.  Other type names can wait.  */
628       if (TREE_CODE (name) == BIT_NOT_EXPR)
629         {
630           tree type = NULL_TREE;
631
632           if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
633             type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
634           else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
635             type = TREE_TYPE (TREE_OPERAND (name, 0));
636
637           if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
638             name = build_min_nt (BIT_NOT_EXPR, type);
639         }
640
641       return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
642     }
643
644   /* This is the logic that magically deletes the second argument to
645      operator delete, if it is not needed.  */
646   if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
647     {
648       tree save_last = TREE_CHAIN (parms);
649
650       /* get rid of unneeded argument */
651       TREE_CHAIN (parms) = NULL_TREE;
652       if (build_method_call (instance, name, parms, basetype_path,
653                              (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
654         {
655           /* If it finds a match, return it.  */
656           return build_method_call (instance, name, parms, basetype_path, flags);
657         }
658       /* If it doesn't work, two argument delete must work */
659       TREE_CHAIN (parms) = save_last;
660     }
661   /* We already know whether it's needed or not for vec delete.  */
662   else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
663            && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
664            && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
665     TREE_CHAIN (parms) = NULL_TREE;
666
667   if (TREE_CODE (name) == BIT_NOT_EXPR)
668     {
669       if (parms)
670         error ("destructors take no parameters");
671       basetype = TREE_TYPE (instance);
672       if (TREE_CODE (basetype) == REFERENCE_TYPE)
673         basetype = TREE_TYPE (basetype);
674
675       if (! check_dtor_name (basetype, name))
676         cp_error
677           ("destructor name `~%T' does not match type `%T' of expression",
678            TREE_OPERAND (name, 0), basetype);
679
680       if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
681         return cp_convert (void_type_node, instance);
682       instance = default_conversion (instance);
683       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
684       return build_delete (build_pointer_type (basetype),
685                            instance_ptr, integer_two_node,
686                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
687     }
688
689   return build_new_method_call (instance, name, parms, basetype_path, flags);
690 }
691
692 /* New overloading code.  */
693
694 struct z_candidate {
695   tree fn;
696   tree convs;
697   tree second_conv;
698   int viable;
699   tree basetype_path;
700   tree template;
701   tree warnings;
702   struct z_candidate *next;
703 };
704
705 #define IDENTITY_RANK 0
706 #define EXACT_RANK 1
707 #define PROMO_RANK 2
708 #define STD_RANK 3
709 #define PBOOL_RANK 4
710 #define USER_RANK 5
711 #define ELLIPSIS_RANK 6
712 #define BAD_RANK 7
713
714 #define ICS_RANK(NODE)                          \
715   (ICS_BAD_FLAG (NODE) ? BAD_RANK   \
716    : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK   \
717    : ICS_USER_FLAG (NODE) ? USER_RANK           \
718    : ICS_STD_RANK (NODE))
719
720 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
721
722 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
723 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
724 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
725 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
726
727 #define USER_CONV_CAND(NODE) \
728   ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
729 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
730
731 int
732 null_ptr_cst_p (t)
733      tree t;
734 {
735   if (t == null_node
736       || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
737     return 1;
738   return 0;
739 }
740
741 static tree
742 build_conv (code, type, from)
743      enum tree_code code;
744      tree type, from;
745 {
746   tree t = build1 (code, type, from);
747   int rank = ICS_STD_RANK (from);
748   switch (code)
749     {
750     case PTR_CONV:
751     case PMEM_CONV:
752     case BASE_CONV:
753     case STD_CONV:
754       if (rank < STD_RANK)
755         rank = STD_RANK;
756       break;
757
758     case QUAL_CONV:
759       if (rank < EXACT_RANK)
760         rank = EXACT_RANK;
761
762     default:
763       break;
764     }
765   ICS_STD_RANK (t) = rank;
766   ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
767   ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
768   return t;
769 }
770
771 static tree
772 non_reference (t)
773      tree t;
774 {
775   if (TREE_CODE (t) == REFERENCE_TYPE)
776     t = TREE_TYPE (t);
777   return t;
778 }
779
780 static tree
781 strip_top_quals (t)
782      tree t;
783 {
784   if (TREE_CODE (t) == ARRAY_TYPE)
785     return t;
786   return TYPE_MAIN_VARIANT (t);
787 }
788
789 /* Returns the standard conversion path (see [conv]) from type FROM to type
790    TO, if any.  For proper handling of null pointer constants, you must
791    also pass the expression EXPR to convert from.  */
792
793 static tree
794 standard_conversion (to, from, expr)
795      tree to, from, expr;
796 {
797   enum tree_code fcode, tcode;
798   tree conv;
799   int fromref = 0;
800
801   if (TREE_CODE (to) == REFERENCE_TYPE)
802     to = TREE_TYPE (to);
803   if (TREE_CODE (from) == REFERENCE_TYPE)
804     {
805       fromref = 1;
806       from = TREE_TYPE (from);
807     }
808   to = strip_top_quals (to);
809   from = strip_top_quals (from);
810
811   if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
812       && expr && type_unknown_p (expr))
813     {
814       expr = instantiate_type (to, expr, 0);
815       if (expr == error_mark_node)
816         return NULL_TREE;
817       from = TREE_TYPE (expr);
818     }
819
820   fcode = TREE_CODE (from);
821   tcode = TREE_CODE (to);
822
823   conv = build1 (IDENTITY_CONV, from, expr);
824
825   if (fcode == FUNCTION_TYPE)
826     {
827       from = build_pointer_type (from);
828       fcode = TREE_CODE (from);
829       conv = build_conv (LVALUE_CONV, from, conv);
830     }
831   else if (fcode == ARRAY_TYPE)
832     {
833       from = build_pointer_type (TREE_TYPE (from));
834       fcode = TREE_CODE (from);
835       conv = build_conv (LVALUE_CONV, from, conv);
836     }
837   else if (fromref || (expr && real_lvalue_p (expr)))
838     conv = build_conv (RVALUE_CONV, from, conv);
839
840   if (from == to)
841     return conv;
842
843   if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
844       && expr && null_ptr_cst_p (expr))
845     {
846       conv = build_conv (STD_CONV, to, conv);
847     }
848   else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
849     {
850       enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
851       enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
852
853       if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
854                        TYPE_MAIN_VARIANT (TREE_TYPE (to))))
855         ;
856       else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
857                && ufcode != FUNCTION_TYPE)
858         {
859           from = build_pointer_type
860             (cp_build_qualified_type (void_type_node, 
861                                       CP_TYPE_QUALS (TREE_TYPE (from))));
862           conv = build_conv (PTR_CONV, from, conv);
863         }
864       else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
865         {
866           tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
867           tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
868
869           if (DERIVED_FROM_P (fbase, tbase)
870               && (same_type_p 
871                   (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
872                    TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
873             {
874               from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
875               from = build_pointer_type (from);
876               conv = build_conv (PMEM_CONV, from, conv);
877             }
878         }
879       else if (IS_AGGR_TYPE (TREE_TYPE (from))
880                && IS_AGGR_TYPE (TREE_TYPE (to)))
881         {
882           if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
883             {
884               from = 
885                 cp_build_qualified_type (TREE_TYPE (to),
886                                          CP_TYPE_QUALS (TREE_TYPE (from)));
887               from = build_pointer_type (from);
888               conv = build_conv (PTR_CONV, from, conv);
889             }
890         }
891
892       if (same_type_p (from, to))
893         /* OK */;
894       else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
895         conv = build_conv (QUAL_CONV, to, conv);
896       else if (expr && string_conv_p (to, expr, 0))
897         /* converting from string constant to char *.  */
898         conv = build_conv (QUAL_CONV, to, conv);
899       else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
900         {
901           conv = build_conv (PTR_CONV, to, conv);
902           ICS_BAD_FLAG (conv) = 1;
903         }
904       else
905         return 0;
906
907       from = to;
908     }
909   else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
910     {
911       tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
912       tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
913       tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
914       tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
915
916       if (! DERIVED_FROM_P (fbase, tbase)
917           || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
918           || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
919                           TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
920           || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
921         return 0;
922
923       from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
924       from = build_cplus_method_type (from, TREE_TYPE (fromfn),
925                                       TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
926       from = build_ptrmemfunc_type (build_pointer_type (from));
927       conv = build_conv (PMEM_CONV, from, conv);
928     }
929   else if (tcode == BOOLEAN_TYPE)
930     {
931       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
932              || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
933         return 0;
934
935       conv = build_conv (STD_CONV, to, conv);
936       if (fcode == POINTER_TYPE
937           || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
938         ICS_STD_RANK (conv) = PBOOL_RANK;
939     }
940   /* We don't check for ENUMERAL_TYPE here because there are no standard
941      conversions to enum type.  */
942   else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
943            || tcode == REAL_TYPE)
944     {
945       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
946         return 0;
947       conv = build_conv (STD_CONV, to, conv);
948
949       /* Give this a better rank if it's a promotion.  */
950       if (to == type_promotes_to (from)
951           && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
952         ICS_STD_RANK (conv) = PROMO_RANK;
953     }
954   else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
955            && DERIVED_FROM_P (to, from))
956     {
957       if (TREE_CODE (conv) == RVALUE_CONV)
958         conv = TREE_OPERAND (conv, 0);
959       conv = build_conv (BASE_CONV, to, conv);
960     }
961   else
962     return 0;
963
964   return conv;
965 }
966
967 /* Returns the conversion path from type FROM to reference type TO for
968    purposes of reference binding.  For lvalue binding, either pass a
969    reference type to FROM or an lvalue expression to EXPR.
970
971    Currently does not distinguish in the generated trees between binding to
972    an lvalue and a temporary.  Should it?  */
973
974 static tree
975 reference_binding (rto, rfrom, expr, flags)
976      tree rto, rfrom, expr;
977      int flags;
978 {
979   tree conv;
980   int lvalue = 1;
981   tree to = TREE_TYPE (rto);
982   tree from = rfrom;
983   int related;
984
985   if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
986     {
987       expr = instantiate_type (to, expr, 0);
988       if (expr == error_mark_node)
989         return NULL_TREE;
990       from = TREE_TYPE (expr);
991     }
992
993   if (TREE_CODE (from) == REFERENCE_TYPE)
994     from = TREE_TYPE (from);
995   else if (! expr || ! real_lvalue_p (expr))
996     lvalue = 0;
997
998   related = (same_type_p (TYPE_MAIN_VARIANT (to),
999                           TYPE_MAIN_VARIANT (from))
1000              || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
1001                  && DERIVED_FROM_P (to, from)));
1002
1003   if (lvalue && related && at_least_as_qualified_p (to, from))
1004     {
1005       conv = build1 (IDENTITY_CONV, from, expr);
1006
1007       if (same_type_p (TYPE_MAIN_VARIANT (to),
1008                        TYPE_MAIN_VARIANT (from)))
1009         conv = build_conv (REF_BIND, rto, conv);
1010       else
1011         {
1012           conv = build_conv (REF_BIND, rto, conv);
1013           ICS_STD_RANK (conv) = STD_RANK;
1014         }
1015     }
1016   else
1017     conv = NULL_TREE;
1018
1019   if (! conv)
1020     {
1021       conv = standard_conversion (to, rfrom, expr);
1022       if (conv)
1023         {
1024           conv = build_conv (REF_BIND, rto, conv);
1025
1026           /* Bind directly to a base subobject of a class rvalue.  Do it
1027              after building the conversion for proper handling of ICS_RANK.  */
1028           if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
1029             TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
1030         }
1031       if (conv
1032           && ((! (CP_TYPE_CONST_NON_VOLATILE_P (to)
1033                   && (flags & LOOKUP_NO_TEMP_BIND) == 0))
1034               /* If T1 is reference-related to T2, cv1 must be the same
1035                  cv-qualification as, or greater cv-qualification than,
1036                  cv2; otherwise, the program is ill-formed.  */
1037               || (related && !at_least_as_qualified_p (to, from))))
1038         ICS_BAD_FLAG (conv) = 1;
1039     }
1040
1041   return conv;
1042 }
1043
1044 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1045    to type TO.  The optional expression EXPR may affect the conversion.
1046    FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
1047    significant.  */
1048
1049 static tree
1050 implicit_conversion (to, from, expr, flags)
1051      tree to, from, expr;
1052      int flags;
1053 {
1054   tree conv;
1055   struct z_candidate *cand;
1056
1057   if (TREE_CODE (to) == REFERENCE_TYPE)
1058     conv = reference_binding (to, from, expr, flags);
1059   else
1060     conv = standard_conversion (to, from, expr);
1061
1062   if (conv)
1063     ;
1064   else if (expr != NULL_TREE
1065            && (IS_AGGR_TYPE (non_reference (from))
1066                || IS_AGGR_TYPE (non_reference (to)))
1067            && (flags & LOOKUP_NO_CONVERSION) == 0)
1068     {
1069       cand = build_user_type_conversion_1
1070         (to, expr, LOOKUP_ONLYCONVERTING);
1071       if (cand)
1072         conv = cand->second_conv;
1073       if ((! conv || ICS_BAD_FLAG (conv))
1074           && TREE_CODE (to) == REFERENCE_TYPE
1075           && (flags & LOOKUP_NO_TEMP_BIND) == 0)
1076         {
1077           cand = build_user_type_conversion_1
1078             (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
1079           if (cand)
1080             {
1081               if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
1082                 ICS_BAD_FLAG (cand->second_conv) = 1;
1083               if (!conv || (ICS_BAD_FLAG (conv)
1084                             > ICS_BAD_FLAG (cand->second_conv)))
1085                 conv = build_conv (REF_BIND, to, cand->second_conv);
1086             }
1087         }
1088     }
1089
1090   return conv;
1091 }
1092
1093 /* Add a new entry to the list of candidates.  Used by the add_*_candidate
1094    functions.  */
1095
1096 static struct z_candidate *
1097 add_candidate (candidates, fn, convs, viable)
1098      struct z_candidate *candidates;
1099      tree fn, convs;
1100      int viable;
1101 {
1102   struct z_candidate *cand
1103     = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
1104
1105   cand->fn = fn;
1106   cand->convs = convs;
1107   cand->second_conv = NULL_TREE;
1108   cand->viable = viable;
1109   cand->basetype_path = NULL_TREE;
1110   cand->template = NULL_TREE;
1111   cand->warnings = NULL_TREE;
1112   cand->next = candidates;
1113
1114   return cand;
1115 }
1116
1117 /* Create an overload candidate for the function or method FN called with
1118    the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
1119    to implicit_conversion.  */
1120
1121 static struct z_candidate *
1122 add_function_candidate (candidates, fn, arglist, flags)
1123      struct z_candidate *candidates;
1124      tree fn, arglist;
1125      int flags;
1126 {
1127   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1128   int i, len;
1129   tree convs;
1130   tree parmnode = parmlist;
1131   tree argnode = arglist;
1132   int viable = 1;
1133
1134   /* The `this' and `in_chrg' arguments to constructors are not considered
1135      in overload resolution.  */
1136   if (DECL_CONSTRUCTOR_P (fn))
1137     {
1138       parmnode = TREE_CHAIN (parmnode);
1139       argnode = TREE_CHAIN (argnode);
1140       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1141         {
1142           parmnode = TREE_CHAIN (parmnode);
1143           argnode = TREE_CHAIN (argnode);
1144         }
1145     }
1146
1147   len = list_length (argnode);
1148   convs = make_scratch_vec (len);
1149
1150   for (i = 0; i < len; ++i)
1151     {
1152       tree arg = TREE_VALUE (argnode);
1153       tree argtype = lvalue_type (arg);
1154       tree t;
1155
1156       if (parmnode == void_list_node)
1157         break;
1158
1159       if (parmnode)
1160         {
1161           tree parmtype = TREE_VALUE (parmnode);
1162
1163           /* [over.match.funcs] For conversion functions, the function is
1164              considered to be a member of the class of the implicit object
1165              argument for the purpose of defining the type of the implicit
1166              object parameter.
1167
1168              Since build_over_call ignores the ICS for the `this' parameter,
1169              we can just change the parm type.  */
1170           if (DECL_CONV_FN_P (fn) && i == 0)
1171             {
1172               parmtype
1173                 = build_qualified_type (TREE_TYPE (argtype),
1174                                         TYPE_QUALS (TREE_TYPE (parmtype)));
1175               parmtype = build_pointer_type (parmtype);
1176             }
1177
1178           t = implicit_conversion (parmtype, argtype, arg, flags);
1179         }
1180       else
1181         {
1182           t = build1 (IDENTITY_CONV, argtype, arg);
1183           ICS_ELLIPSIS_FLAG (t) = 1;
1184         }
1185
1186       if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1187           && ! DECL_CONSTRUCTOR_P (fn))
1188         ICS_THIS_FLAG (t) = 1;
1189
1190       TREE_VEC_ELT (convs, i) = t;
1191       if (! t)
1192         break;
1193
1194       if (ICS_BAD_FLAG (t))
1195         viable = -1;
1196
1197       if (parmnode)
1198         parmnode = TREE_CHAIN (parmnode);
1199       argnode = TREE_CHAIN (argnode);
1200     }
1201
1202   if (i < len)
1203     viable = 0;
1204
1205   /* Make sure there are default args for the rest of the parms.  */
1206   for (; parmnode && parmnode != void_list_node;
1207        parmnode = TREE_CHAIN (parmnode))
1208     if (! TREE_PURPOSE (parmnode))
1209       {
1210         viable = 0;
1211         break;
1212       }
1213
1214   return add_candidate (candidates, fn, convs, viable);
1215 }
1216
1217 /* Create an overload candidate for the conversion function FN which will
1218    be invoked for expression OBJ, producing a pointer-to-function which
1219    will in turn be called with the argument list ARGLIST, and add it to
1220    CANDIDATES.  FLAGS is passed on to implicit_conversion.  */
1221
1222 static struct z_candidate *
1223 add_conv_candidate (candidates, fn, obj, arglist)
1224      struct z_candidate *candidates;
1225      tree fn, obj, arglist;
1226 {
1227   tree totype = TREE_TYPE (TREE_TYPE (fn));
1228   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1229   int i, len = list_length (arglist) + 1;
1230   tree convs = make_scratch_vec (len);
1231   tree parmnode = parmlist;
1232   tree argnode = arglist;
1233   int viable = 1;
1234   int flags = LOOKUP_NORMAL;
1235
1236   for (i = 0; i < len; ++i)
1237     {
1238       tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1239       tree argtype = lvalue_type (arg);
1240       tree t;
1241
1242       if (i == 0)
1243         t = implicit_conversion (totype, argtype, arg, flags);
1244       else if (parmnode == void_list_node)
1245         break;
1246       else if (parmnode)
1247         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1248       else
1249         {
1250           t = build1 (IDENTITY_CONV, argtype, arg);
1251           ICS_ELLIPSIS_FLAG (t) = 1;
1252         }
1253
1254       TREE_VEC_ELT (convs, i) = t;
1255       if (! t)
1256         break;
1257
1258       if (ICS_BAD_FLAG (t))
1259         viable = -1;
1260
1261       if (i == 0)
1262         continue;
1263
1264       if (parmnode)
1265         parmnode = TREE_CHAIN (parmnode);
1266       argnode = TREE_CHAIN (argnode);
1267     }
1268
1269   if (i < len)
1270     viable = 0;
1271
1272   for (; parmnode && parmnode != void_list_node;
1273        parmnode = TREE_CHAIN (parmnode))
1274     if (! TREE_PURPOSE (parmnode))
1275       {
1276         viable = 0;
1277         break;
1278       }
1279
1280   return add_candidate (candidates, fn, convs, viable);
1281 }
1282
1283 static struct z_candidate *
1284 build_builtin_candidate (candidates, fnname, type1, type2,
1285                          args, argtypes, flags)
1286      struct z_candidate *candidates;
1287      tree fnname, type1, type2, *args, *argtypes;
1288      int flags;
1289
1290 {
1291   tree t, convs;
1292   int viable = 1, i;
1293   tree types[2];
1294
1295   types[0] = type1;
1296   types[1] = type2;
1297
1298   convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1299
1300   for (i = 0; i < 2; ++i)
1301     {
1302       if (! args[i])
1303         break;
1304
1305       t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1306       if (! t)
1307         {
1308           viable = 0;
1309           /* We need something for printing the candidate.  */
1310           t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1311         }
1312       else if (ICS_BAD_FLAG (t))
1313         viable = 0;
1314       TREE_VEC_ELT (convs, i) = t;
1315     }
1316
1317   /* For COND_EXPR we rearranged the arguments; undo that now.  */
1318   if (args[2])
1319     {
1320       TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1321       TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1322       t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1323       if (t)
1324         TREE_VEC_ELT (convs, 0) = t;
1325       else
1326         viable = 0;
1327     }      
1328
1329   return add_candidate (candidates, fnname, convs, viable);
1330 }
1331
1332 static int
1333 is_complete (t)
1334      tree t;
1335 {
1336   return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1337 }
1338
1339 /* Create any builtin operator overload candidates for the operator in
1340    question given the converted operand types TYPE1 and TYPE2.  The other
1341    args are passed through from add_builtin_candidates to
1342    build_builtin_candidate.  */
1343
1344 static struct z_candidate *
1345 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1346                        args, argtypes, flags)
1347      struct z_candidate *candidates;
1348      enum tree_code code, code2;
1349      tree fnname, type1, type2, *args, *argtypes;
1350      int flags;
1351 {
1352   switch (code)
1353     {
1354     case POSTINCREMENT_EXPR:
1355     case POSTDECREMENT_EXPR:
1356       args[1] = integer_zero_node;
1357       type2 = integer_type_node;
1358       break;
1359     default:
1360       break;
1361     }
1362
1363   switch (code)
1364     {
1365
1366 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1367      and  VQ  is  either  volatile or empty, there exist candidate operator
1368      functions of the form
1369              VQ T&   operator++(VQ T&);
1370              T       operator++(VQ T&, int);
1371    5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1372      type  other than bool, and VQ is either volatile or empty, there exist
1373      candidate operator functions of the form
1374              VQ T&   operator--(VQ T&);
1375              T       operator--(VQ T&, int);
1376    6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
1377      complete  object type, and VQ is either volatile or empty, there exist
1378      candidate operator functions of the form
1379              T*VQ&   operator++(T*VQ&);
1380              T*VQ&   operator--(T*VQ&);
1381              T*      operator++(T*VQ&, int);
1382              T*      operator--(T*VQ&, int);  */
1383
1384     case POSTDECREMENT_EXPR:
1385     case PREDECREMENT_EXPR:
1386       if (TREE_CODE (type1) == BOOLEAN_TYPE)
1387         return candidates;
1388     case POSTINCREMENT_EXPR:
1389     case PREINCREMENT_EXPR:
1390       if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1391           || TYPE_PTROB_P (type1))
1392         {
1393           type1 = build_reference_type (type1);
1394           break;
1395         }
1396       return candidates;
1397
1398 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1399      exist candidate operator functions of the form
1400
1401              T&      operator*(T*);
1402
1403    8 For every function type T, there exist candidate operator functions of
1404      the form
1405              T&      operator*(T*);  */
1406
1407     case INDIRECT_REF:
1408       if (TREE_CODE (type1) == POINTER_TYPE
1409           && (TYPE_PTROB_P (type1)
1410               || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1411         break;
1412       return candidates;
1413
1414 /* 9 For every type T, there exist candidate operator functions of the form
1415              T*      operator+(T*);
1416
1417    10For  every  promoted arithmetic type T, there exist candidate operator
1418      functions of the form
1419              T       operator+(T);
1420              T       operator-(T);  */
1421
1422     case CONVERT_EXPR: /* unary + */
1423       if (TREE_CODE (type1) == POINTER_TYPE
1424           && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1425         break;
1426     case NEGATE_EXPR:
1427       if (ARITHMETIC_TYPE_P (type1))
1428         break;
1429       return candidates;
1430
1431 /* 11For every promoted integral type T,  there  exist  candidate  operator
1432      functions of the form
1433              T       operator~(T);  */
1434
1435     case BIT_NOT_EXPR:
1436       if (INTEGRAL_TYPE_P (type1))
1437         break;
1438       return candidates;
1439
1440 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1441      is the same type as C2 or is a derived class of C2, T  is  a  complete
1442      object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1443      there exist candidate operator functions of the form
1444              CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1445      where CV12 is the union of CV1 and CV2.  */
1446
1447     case MEMBER_REF:
1448       if (TREE_CODE (type1) == POINTER_TYPE
1449           && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1450         {
1451           tree c1 = TREE_TYPE (type1);
1452           tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1453                      ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1454                      : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1455
1456           if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1457               && (TYPE_PTRMEMFUNC_P (type2)
1458                   || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1459             break;
1460         }
1461       return candidates;
1462
1463 /* 13For every pair of promoted arithmetic types L and R, there exist  can-
1464      didate operator functions of the form
1465              LR      operator*(L, R);
1466              LR      operator/(L, R);
1467              LR      operator+(L, R);
1468              LR      operator-(L, R);
1469              bool    operator<(L, R);
1470              bool    operator>(L, R);
1471              bool    operator<=(L, R);
1472              bool    operator>=(L, R);
1473              bool    operator==(L, R);
1474              bool    operator!=(L, R);
1475      where  LR  is  the  result of the usual arithmetic conversions between
1476      types L and R.
1477
1478    14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
1479      unqualified  complete  object  type and I is a promoted integral type,
1480      there exist candidate operator functions of the form
1481              T*      operator+(T*, I);
1482              T&      operator[](T*, I);
1483              T*      operator-(T*, I);
1484              T*      operator+(I, T*);
1485              T&      operator[](I, T*);
1486
1487    15For every T, where T is a pointer to complete object type, there exist
1488      candidate operator functions of the form112)
1489              ptrdiff_t operator-(T, T);
1490
1491    16For  every pointer type T, there exist candidate operator functions of
1492      the form
1493              bool    operator<(T, T);
1494              bool    operator>(T, T);
1495              bool    operator<=(T, T);
1496              bool    operator>=(T, T);
1497              bool    operator==(T, T);
1498              bool    operator!=(T, T);
1499
1500    17For every pointer to member type T,  there  exist  candidate  operator
1501      functions of the form
1502              bool    operator==(T, T);
1503              bool    operator!=(T, T);  */
1504
1505     case MINUS_EXPR:
1506       if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1507         break;
1508       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1509         {
1510           type2 = ptrdiff_type_node;
1511           break;
1512         }
1513     case MULT_EXPR:
1514     case TRUNC_DIV_EXPR:
1515       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1516         break;
1517       return candidates;
1518
1519     case EQ_EXPR:
1520     case NE_EXPR:
1521       if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1522           || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1523         break;
1524       if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1525           && null_ptr_cst_p (args[1]))
1526         {
1527           type2 = type1;
1528           break;
1529         }
1530       if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1531           && null_ptr_cst_p (args[0]))
1532         {
1533           type1 = type2;
1534           break;
1535         }
1536     case LT_EXPR:
1537     case GT_EXPR:
1538     case LE_EXPR:
1539     case GE_EXPR:
1540     case MAX_EXPR:
1541     case MIN_EXPR:
1542       if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1543           || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1544         break;
1545       if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1546         {
1547           type2 = type1;
1548           break;
1549         }
1550       if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1551         {
1552           type1 = type2;
1553           break;
1554         }
1555       return candidates;
1556
1557     case PLUS_EXPR:
1558       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1559         break;
1560     case ARRAY_REF:
1561       if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1562         {
1563           type1 = ptrdiff_type_node;
1564           break;
1565         }
1566       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1567         {
1568           type2 = ptrdiff_type_node;
1569           break;
1570         }
1571       return candidates;
1572
1573 /* 18For  every pair of promoted integral types L and R, there exist candi-
1574      date operator functions of the form
1575              LR      operator%(L, R);
1576              LR      operator&(L, R);
1577              LR      operator^(L, R);
1578              LR      operator|(L, R);
1579              L       operator<<(L, R);
1580              L       operator>>(L, R);
1581      where LR is the result of the  usual  arithmetic  conversions  between
1582      types L and R.  */
1583
1584     case TRUNC_MOD_EXPR:
1585     case BIT_AND_EXPR:
1586     case BIT_IOR_EXPR:
1587     case BIT_XOR_EXPR:
1588     case LSHIFT_EXPR:
1589     case RSHIFT_EXPR:
1590       if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1591         break;
1592       return candidates;
1593
1594 /* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
1595      type, VQ is either volatile or empty, and R is a  promoted  arithmetic
1596      type, there exist candidate operator functions of the form
1597              VQ L&   operator=(VQ L&, R);
1598              VQ L&   operator*=(VQ L&, R);
1599              VQ L&   operator/=(VQ L&, R);
1600              VQ L&   operator+=(VQ L&, R);
1601              VQ L&   operator-=(VQ L&, R);
1602
1603    20For  every  pair T, VQ), where T is any type and VQ is either volatile
1604      or empty, there exist candidate operator functions of the form
1605              T*VQ&   operator=(T*VQ&, T*);
1606
1607    21For every pair T, VQ), where T is a pointer to member type and  VQ  is
1608      either  volatile or empty, there exist candidate operator functions of
1609      the form
1610              VQ T&   operator=(VQ T&, T);
1611
1612    22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
1613      unqualified  complete object type, VQ is either volatile or empty, and
1614      I is a promoted integral type, there exist  candidate  operator  func-
1615      tions of the form
1616              T*VQ&   operator+=(T*VQ&, I);
1617              T*VQ&   operator-=(T*VQ&, I);
1618
1619    23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
1620      type, VQ is either volatile or empty, and R  is  a  promoted  integral
1621      type, there exist candidate operator functions of the form
1622
1623              VQ L&   operator%=(VQ L&, R);
1624              VQ L&   operator<<=(VQ L&, R);
1625              VQ L&   operator>>=(VQ L&, R);
1626              VQ L&   operator&=(VQ L&, R);
1627              VQ L&   operator^=(VQ L&, R);
1628              VQ L&   operator|=(VQ L&, R);  */
1629
1630     case MODIFY_EXPR:
1631       switch (code2)
1632         {
1633         case PLUS_EXPR:
1634         case MINUS_EXPR:
1635           if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1636             {
1637               type2 = ptrdiff_type_node;
1638               break;
1639             }
1640         case MULT_EXPR:
1641         case TRUNC_DIV_EXPR:
1642           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1643             break;
1644           return candidates;
1645
1646         case TRUNC_MOD_EXPR:
1647         case BIT_AND_EXPR:
1648         case BIT_IOR_EXPR:
1649         case BIT_XOR_EXPR:
1650         case LSHIFT_EXPR:
1651         case RSHIFT_EXPR:
1652           if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1653             break;
1654           return candidates;
1655
1656         case NOP_EXPR:
1657           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1658             break;
1659           if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1660               || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1661               || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1662               || ((TYPE_PTRMEMFUNC_P (type1)
1663                    || TREE_CODE (type1) == POINTER_TYPE)
1664                   && null_ptr_cst_p (args[1])))
1665             {
1666               type2 = type1;
1667               break;
1668             }
1669           return candidates;
1670
1671         default:
1672           my_friendly_abort (367);
1673         }
1674       type1 = build_reference_type (type1);
1675       break;
1676
1677     case COND_EXPR:
1678       /* Kludge around broken overloading rules whereby
1679          bool ? const char& : enum is ambiguous
1680          (between int and const char&).  */
1681       flags |= LOOKUP_NO_TEMP_BIND;
1682
1683       /* Extension: Support ?: of enumeral type.  Hopefully this will not
1684          be an extension for long.  */
1685       if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1686         break;
1687       else if (TREE_CODE (type1) == ENUMERAL_TYPE
1688                || TREE_CODE (type2) == ENUMERAL_TYPE)
1689         return candidates;
1690       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1691         break;
1692       if (TREE_CODE (type1) == TREE_CODE (type2)
1693           && (TREE_CODE (type1) == REFERENCE_TYPE
1694               || TREE_CODE (type1) == POINTER_TYPE
1695               || TYPE_PTRMEMFUNC_P (type1)
1696               || IS_AGGR_TYPE (type1)))
1697         break;
1698       if (TREE_CODE (type1) == REFERENCE_TYPE
1699           || TREE_CODE (type2) == REFERENCE_TYPE)
1700         return candidates;
1701       if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1702            && null_ptr_cst_p (args[1]))
1703           || IS_AGGR_TYPE (type1))
1704         {
1705           type2 = type1;
1706           break;
1707         }
1708       if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1709            && null_ptr_cst_p (args[0]))
1710           || IS_AGGR_TYPE (type2))
1711         {
1712           type1 = type2;
1713           break;
1714         }
1715       return candidates;
1716
1717     default:
1718       my_friendly_abort (367);
1719     }
1720
1721   /* If we're dealing with two pointer types, we need candidates
1722      for both of them.  */
1723   if (type2 && type1 != type2
1724       && TREE_CODE (type1) == TREE_CODE (type2)
1725       && (TREE_CODE (type1) == REFERENCE_TYPE
1726           || (TREE_CODE (type1) == POINTER_TYPE
1727               && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1728           || TYPE_PTRMEMFUNC_P (type1)
1729           || IS_AGGR_TYPE (type1)))
1730     {
1731       candidates = build_builtin_candidate
1732         (candidates, fnname, type1, type1, args, argtypes, flags);
1733       return build_builtin_candidate
1734         (candidates, fnname, type2, type2, args, argtypes, flags);
1735     }
1736
1737   return build_builtin_candidate
1738     (candidates, fnname, type1, type2, args, argtypes, flags);
1739 }
1740
1741 tree
1742 type_decays_to (type)
1743      tree type;
1744 {
1745   if (TREE_CODE (type) == ARRAY_TYPE)
1746     return build_pointer_type (TREE_TYPE (type));
1747   if (TREE_CODE (type) == FUNCTION_TYPE)
1748     return build_pointer_type (type);
1749   return type;
1750 }
1751
1752 /* There are three conditions of builtin candidates:
1753
1754    1) bool-taking candidates.  These are the same regardless of the input.
1755    2) pointer-pair taking candidates.  These are generated for each type
1756       one of the input types converts to.
1757    3) arithmetic candidates.  According to the WP, we should generate
1758       all of these, but I'm trying not to... */
1759
1760 static struct z_candidate *
1761 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1762      struct z_candidate *candidates;
1763      enum tree_code code, code2;
1764      tree fnname, *args;
1765      int flags;
1766 {
1767   int ref1, i;
1768   tree type, argtypes[3], types[2];
1769
1770   for (i = 0; i < 3; ++i)
1771     {
1772       if (args[i])
1773         argtypes[i]  = lvalue_type (args[i]);
1774       else
1775         argtypes[i] = NULL_TREE;
1776     }
1777
1778   switch (code)
1779     {
1780 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1781      and  VQ  is  either  volatile or empty, there exist candidate operator
1782      functions of the form
1783                  VQ T&   operator++(VQ T&);  */
1784
1785     case POSTINCREMENT_EXPR:
1786     case PREINCREMENT_EXPR:
1787     case POSTDECREMENT_EXPR:
1788     case PREDECREMENT_EXPR:
1789     case MODIFY_EXPR:
1790       ref1 = 1;
1791       break;
1792
1793 /* 24There also exist candidate operator functions of the form
1794              bool    operator!(bool);
1795              bool    operator&&(bool, bool);
1796              bool    operator||(bool, bool);  */
1797
1798     case TRUTH_NOT_EXPR:
1799       return build_builtin_candidate
1800         (candidates, fnname, boolean_type_node,
1801          NULL_TREE, args, argtypes, flags);
1802
1803     case TRUTH_ORIF_EXPR:
1804     case TRUTH_ANDIF_EXPR:
1805       return build_builtin_candidate
1806         (candidates, fnname, boolean_type_node,
1807          boolean_type_node, args, argtypes, flags);
1808
1809     case ADDR_EXPR:
1810     case COMPOUND_EXPR:
1811     case COMPONENT_REF:
1812       return candidates;
1813
1814     default:
1815       ref1 = 0;
1816     }
1817
1818   types[0] = types[1] = NULL_TREE;
1819
1820   for (i = 0; i < 2; ++i)
1821     {
1822       if (! args[i])
1823         ;
1824       else if (IS_AGGR_TYPE (argtypes[i]))
1825         {
1826           tree convs = lookup_conversions (argtypes[i]);
1827
1828           if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1829             return candidates;
1830
1831           convs = lookup_conversions (argtypes[i]);
1832
1833           if (code == COND_EXPR)
1834             {
1835               if (real_lvalue_p (args[i]))
1836                 types[i] = scratch_tree_cons
1837                   (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1838
1839               types[i] = scratch_tree_cons
1840                 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1841             }
1842
1843           else if (! convs)
1844             return candidates;
1845
1846           for (; convs; convs = TREE_CHAIN (convs))
1847             {
1848               type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
1849
1850               if (i == 0 && ref1
1851                   && (TREE_CODE (type) != REFERENCE_TYPE
1852                       || CP_TYPE_CONST_P (TREE_TYPE (type))))
1853                 continue;
1854
1855               if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1856                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1857
1858               type = non_reference (type);
1859               if (i != 0 || ! ref1)
1860                 {
1861                   type = TYPE_MAIN_VARIANT (type_decays_to (type));
1862                   if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1863                     types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1864                   if (INTEGRAL_TYPE_P (type))
1865                     type = type_promotes_to (type);
1866                 }
1867
1868               if (! value_member (type, types[i]))
1869                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1870             }
1871         }
1872       else
1873         {
1874           if (code == COND_EXPR && real_lvalue_p (args[i]))
1875             types[i] = scratch_tree_cons
1876               (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1877           type = non_reference (argtypes[i]);
1878           if (i != 0 || ! ref1)
1879             {
1880               type = TYPE_MAIN_VARIANT (type_decays_to (type));
1881               if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1882                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1883               if (INTEGRAL_TYPE_P (type))
1884                 type = type_promotes_to (type);
1885             }
1886           types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1887         }
1888     }
1889
1890   for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1891     {
1892       if (types[1])
1893         for (type = types[1]; type; type = TREE_CHAIN (type))
1894           candidates = add_builtin_candidate
1895             (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1896              TREE_VALUE (type), args, argtypes, flags);
1897       else
1898         candidates = add_builtin_candidate
1899           (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1900            NULL_TREE, args, argtypes, flags);
1901     }
1902
1903   return candidates;
1904 }
1905
1906
1907 /* If TMPL can be successfully instantiated as indicated by
1908    EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1909
1910    TMPL is the template.  EXPLICIT_TARGS are any explicit template
1911    arguments.  ARGLIST is the arguments provided at the call-site.
1912    The RETURN_TYPE is the desired type for conversion operators.  If
1913    OBJ is NULL_TREE, FLAGS are as for add_function_candidate.  If an
1914    OBJ is supplied, FLAGS are ignored, and OBJ is as for
1915    add_conv_candidate.  */
1916
1917 static struct z_candidate*
1918 add_template_candidate_real (candidates, tmpl, explicit_targs,
1919                              arglist, return_type, flags,
1920                              obj, strict)
1921      struct z_candidate *candidates;
1922      tree tmpl, explicit_targs, arglist, return_type;
1923      int flags;
1924      tree obj;
1925      unification_kind_t strict;
1926 {
1927   int ntparms = DECL_NTPARMS (tmpl);
1928   tree targs = make_scratch_vec (ntparms);
1929   struct z_candidate *cand;
1930   int i;
1931   tree fn;
1932
1933   i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1934                            return_type, strict, NULL_TREE);
1935
1936   if (i != 0)
1937     return candidates;
1938
1939   fn = instantiate_template (tmpl, targs);
1940   if (fn == error_mark_node)
1941     return candidates;
1942
1943   if (obj != NULL_TREE)
1944     /* Aha, this is a conversion function.  */
1945     cand = add_conv_candidate (candidates, fn, obj, arglist);
1946   else
1947     cand = add_function_candidate (candidates, fn, arglist, flags);
1948   if (DECL_TI_TEMPLATE (fn) != tmpl)
1949     /* This situation can occur if a member template of a template
1950        class is specialized.  Then, instantiate_template might return
1951        an instantiation of the specialization, in which case the
1952        DECL_TI_TEMPLATE field will point at the original
1953        specialization.  For example:
1954
1955          template <class T> struct S { template <class U> void f(U);
1956                                        template <> void f(int) {}; };
1957          S<double> sd;
1958          sd.f(3);
1959
1960        Here, TMPL will be template <class U> S<double>::f(U).
1961        And, instantiate template will give us the specialization
1962        template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
1963        for this will point at template <class T> template <> S<T>::f(int),
1964        so that we can find the definition.  For the purposes of
1965        overload resolution, however, we want the original TMPL.  */
1966     cand->template = tree_cons (tmpl, targs, NULL_TREE);
1967   else
1968     cand->template = DECL_TEMPLATE_INFO (fn);
1969
1970   return cand;
1971 }
1972
1973
1974 static struct z_candidate *
1975 add_template_candidate (candidates, tmpl, explicit_targs, 
1976                         arglist, return_type, flags, strict)
1977      struct z_candidate *candidates;
1978      tree tmpl, explicit_targs, arglist, return_type;
1979      int flags;
1980      unification_kind_t strict;
1981 {
1982   return 
1983     add_template_candidate_real (candidates, tmpl, explicit_targs,
1984                                  arglist, return_type, flags,
1985                                  NULL_TREE, strict);
1986 }
1987
1988
1989 static struct z_candidate *
1990 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1991      struct z_candidate *candidates;
1992      tree tmpl, obj, arglist, return_type;
1993 {
1994   return 
1995     add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1996                                  return_type, 0, obj, DEDUCE_CONV);
1997 }
1998
1999
2000 static int
2001 any_viable (cands)
2002      struct z_candidate *cands;
2003 {
2004   for (; cands; cands = cands->next)
2005     if (pedantic ? cands->viable == 1 : cands->viable)
2006       return 1;
2007   return 0;
2008 }
2009
2010 static struct z_candidate *
2011 splice_viable (cands)
2012      struct z_candidate *cands;
2013 {
2014   struct z_candidate **p = &cands;
2015
2016   for (; *p; )
2017     {
2018       if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2019         p = &((*p)->next);
2020       else
2021         *p = (*p)->next;
2022     }
2023
2024   return cands;
2025 }
2026
2027 static tree
2028 build_this (obj)
2029      tree obj;
2030 {
2031   /* Fix this to work on non-lvalues.  */
2032   if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
2033       || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
2034     return obj;
2035   else
2036     return build_unary_op (ADDR_EXPR, obj, 0);
2037 }
2038
2039 static void
2040 print_z_candidates (candidates)
2041      struct z_candidate *candidates;
2042 {
2043   char *str = "candidates are:";
2044   for (; candidates; candidates = candidates->next)
2045     {
2046       if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2047         {
2048           if (candidates->fn == ansi_opname [COND_EXPR])
2049             cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2050                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2051                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2052                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2053           else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2054             cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2055                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2056                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2057           else
2058             cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2059                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2060         }
2061       else
2062         cp_error_at ("%s %+D%s", str, candidates->fn,
2063                      candidates->viable == -1 ? " <near match>" : "");
2064       str = "               "; 
2065     }
2066 }
2067
2068 /* Returns the best overload candidate to perform the requested
2069    conversion.  This function is used for three the overloading situations
2070    described in [over.match.copy], [over.match.conv], and [over.match.ref].
2071    If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2072    per [dcl.init.ref], so we ignore temporary bindings.  */
2073
2074 static struct z_candidate *
2075 build_user_type_conversion_1 (totype, expr, flags)
2076      tree totype, expr;
2077      int flags;
2078 {
2079   struct z_candidate *candidates, *cand;
2080   tree fromtype = TREE_TYPE (expr);
2081   tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2082   tree args = NULL_TREE;
2083   tree templates = NULL_TREE;
2084
2085   if (IS_AGGR_TYPE (totype))
2086     ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2087   if (IS_AGGR_TYPE (fromtype)
2088       && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2089     convs = lookup_conversions (fromtype);
2090
2091   candidates = 0;
2092   flags |= LOOKUP_NO_CONVERSION;
2093
2094   if (ctors)
2095     {
2096       tree t = build_int_2 (0, 0);
2097       TREE_TYPE (t) = build_pointer_type (totype);
2098       args = build_scratch_list (NULL_TREE, expr);
2099       if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2100         args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2101       args = scratch_tree_cons (NULL_TREE, t, args);
2102
2103       ctors = TREE_VALUE (ctors);
2104     }
2105   for (; ctors; ctors = OVL_NEXT (ctors))
2106     {
2107       tree ctor = OVL_CURRENT (ctors);
2108       if (DECL_NONCONVERTING_P (ctor))
2109         continue;
2110
2111       if (TREE_CODE (ctor) == TEMPLATE_DECL) 
2112         {
2113           templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2114           candidates = 
2115             add_template_candidate (candidates, ctor,
2116                                     NULL_TREE, args, NULL_TREE, flags,
2117                                     DEDUCE_CALL);
2118         } 
2119       else 
2120         candidates = add_function_candidate (candidates, ctor,
2121                                              args, flags); 
2122
2123       if (candidates) 
2124         {
2125           candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2126           candidates->basetype_path = TYPE_BINFO (totype);
2127         } 
2128     }
2129
2130   if (convs)
2131     args = build_scratch_list (NULL_TREE, build_this (expr));
2132
2133   for (; convs; convs = TREE_CHAIN (convs))
2134     {
2135       tree fns = TREE_VALUE (convs);
2136       int convflags = LOOKUP_NO_CONVERSION;
2137       tree ics;
2138
2139       /* If we are called to convert to a reference type, we are trying to
2140          find an lvalue binding, so don't even consider temporaries.  If
2141          we don't find an lvalue binding, the caller will try again to
2142          look for a temporary binding.  */
2143       if (TREE_CODE (totype) == REFERENCE_TYPE)
2144         convflags |= LOOKUP_NO_TEMP_BIND;
2145
2146       if (TREE_CODE (fns) != TEMPLATE_DECL)
2147         ics = implicit_conversion
2148           (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2149       else
2150         /* We can't compute this yet.  */
2151         ics = error_mark_node;
2152
2153       if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2154         /* ignore the near match.  */;
2155       else if (ics)
2156         for (; fns; fns = OVL_NEXT (fns))
2157           {
2158             tree fn = OVL_CURRENT (fns);
2159             struct z_candidate *old_candidates = candidates;
2160
2161             if (TREE_CODE (fn) == TEMPLATE_DECL)
2162               {
2163                 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2164                 candidates = 
2165                   add_template_candidate (candidates, fn, NULL_TREE,
2166                                           args, totype, flags,
2167                                           DEDUCE_CONV);
2168               } 
2169             else 
2170               candidates = add_function_candidate (candidates, fn,
2171                                                    args, flags); 
2172
2173             if (candidates != old_candidates)
2174               {
2175                 if (TREE_CODE (fn) == TEMPLATE_DECL)
2176                   ics = implicit_conversion
2177                     (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2178                      0, convflags);
2179
2180                 candidates->second_conv = ics;
2181                 candidates->basetype_path = TREE_PURPOSE (convs);
2182
2183                 if (ics == NULL_TREE)
2184                   candidates->viable = 0;
2185                 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2186                   candidates->viable = -1;
2187               }
2188           }
2189     }
2190
2191   if (! any_viable (candidates))
2192     {
2193 #if 0
2194       if (flags & LOOKUP_COMPLAIN)
2195         {
2196           if (candidates && ! candidates->next)
2197             /* say why this one won't work or try to be loose */;
2198           else
2199             cp_error ("no viable candidates");
2200         }
2201 #endif
2202
2203       return 0;
2204     }
2205
2206   candidates = splice_viable (candidates);
2207   cand = tourney (candidates);
2208
2209   if (cand == 0)
2210     {
2211       if (flags & LOOKUP_COMPLAIN)
2212         {
2213           cp_error ("conversion from `%T' to `%T' is ambiguous",
2214                     fromtype, totype);
2215           print_z_candidates (candidates);
2216         }
2217
2218       cand = candidates;        /* any one will do */
2219       cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2220       ICS_USER_FLAG (cand->second_conv) = 1;
2221       ICS_BAD_FLAG (cand->second_conv) = 1;
2222
2223       return cand;
2224     }
2225
2226   for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2227     p = &(TREE_OPERAND (*p, 0));
2228
2229   /* Pedantically, normal function declarations are never considered
2230      to refer to template instantiations, so we only do this with
2231      -fguiding-decls.  */ 
2232   if (flag_guiding_decls && templates && ! cand->template 
2233       && !DECL_INITIAL (cand->fn) 
2234       && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2235     add_maybe_template (cand->fn, templates);
2236
2237   *p = build
2238     (USER_CONV,
2239      (DECL_CONSTRUCTOR_P (cand->fn)
2240       ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2241      expr, build_expr_ptr_wrapper (cand));
2242   ICS_USER_FLAG (cand->second_conv) = 1;
2243   if (cand->viable == -1)
2244     ICS_BAD_FLAG (cand->second_conv) = 1;
2245
2246   return cand;
2247 }
2248
2249 tree
2250 build_user_type_conversion (totype, expr, flags)
2251      tree totype, expr;
2252      int flags;
2253 {
2254   struct z_candidate *cand
2255     = build_user_type_conversion_1 (totype, expr, flags);
2256
2257   if (cand)
2258     {
2259       if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2260         return error_mark_node;
2261       return convert_from_reference (convert_like (cand->second_conv, expr));
2262     }
2263   return NULL_TREE;
2264 }
2265
2266 /* Do any initial processing on the arguments to a function call.  */
2267
2268 static tree
2269 resolve_args (args)
2270      tree args;
2271 {
2272   tree t;
2273   for (t = args; t; t = TREE_CHAIN (t))
2274     {
2275       if (TREE_VALUE (t) == error_mark_node)
2276         return error_mark_node;
2277       else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2278         {
2279           error ("invalid use of void expression");
2280           return error_mark_node;
2281         }
2282       else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2283         TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2284     }
2285   return args;
2286 }
2287       
2288 tree
2289 build_new_function_call (fn, args)
2290      tree fn, args;
2291 {
2292   struct z_candidate *candidates = 0, *cand;
2293   tree explicit_targs = NULL_TREE;
2294   int template_only = 0;
2295
2296   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2297     {
2298       explicit_targs = TREE_OPERAND (fn, 1);
2299       fn = TREE_OPERAND (fn, 0);
2300       template_only = 1;
2301     }
2302
2303   if (really_overloaded_fn (fn))
2304     {
2305       tree t1;
2306       tree templates = NULL_TREE;
2307
2308       args = resolve_args (args);
2309
2310       if (args == error_mark_node)
2311         return error_mark_node;
2312
2313       for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2314         {
2315           tree t = OVL_FUNCTION (t1);
2316           if (TREE_CODE (t) == TEMPLATE_DECL)
2317             {
2318               templates = scratch_tree_cons (NULL_TREE, t, templates);
2319               candidates = add_template_candidate
2320                 (candidates, t, explicit_targs, args, NULL_TREE,
2321                  LOOKUP_NORMAL, DEDUCE_CALL);  
2322             }
2323           else if (! template_only)
2324             candidates = add_function_candidate
2325               (candidates, t, args, LOOKUP_NORMAL);
2326         }
2327
2328       if (! any_viable (candidates))
2329         {
2330           if (candidates && ! candidates->next)
2331             return build_function_call (candidates->fn, args);
2332           cp_error ("no matching function for call to `%D (%A)'",
2333                     DECL_NAME (OVL_FUNCTION (fn)), args);
2334           if (candidates)
2335             print_z_candidates (candidates);
2336           return error_mark_node;
2337         }
2338       candidates = splice_viable (candidates);
2339       cand = tourney (candidates);
2340
2341       if (cand == 0)
2342         {
2343           cp_error ("call of overloaded `%D (%A)' is ambiguous",
2344                     DECL_NAME (OVL_FUNCTION (fn)), args);
2345           print_z_candidates (candidates);
2346           return error_mark_node;
2347         }
2348
2349       /* Pedantically, normal function declarations are never considered
2350          to refer to template instantiations, so we only do this with
2351          -fguiding-decls.  */
2352       if (flag_guiding_decls && templates && ! cand->template 
2353           && ! DECL_INITIAL (cand->fn))
2354         add_maybe_template (cand->fn, templates);
2355
2356       return build_over_call (cand, args, LOOKUP_NORMAL);
2357     }
2358
2359   /* This is not really overloaded. */
2360   fn = OVL_CURRENT (fn);
2361
2362   return build_function_call (fn, args);
2363 }
2364
2365 static tree
2366 build_object_call (obj, args)
2367      tree obj, args;
2368 {
2369   struct z_candidate *candidates = 0, *cand;
2370   tree fns, convs, mem_args = NULL_TREE;
2371   tree type = TREE_TYPE (obj);
2372   tree templates = NULL_TREE;
2373
2374   if (TYPE_PTRMEMFUNC_P (type))
2375     {
2376       /* It's no good looking for an overloaded operator() on a
2377          pointer-to-member-function.  */
2378       cp_error ("pointer-to-member function %E cannot be called", obj);
2379       cp_error ("without an object; consider using .* or ->*");
2380       return error_mark_node;
2381     }
2382
2383   fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2384   if (fns == error_mark_node)
2385     return error_mark_node;
2386
2387   args = resolve_args (args);
2388
2389   if (args == error_mark_node)
2390     return error_mark_node;
2391
2392   if (fns)
2393     {
2394       tree base = TREE_PURPOSE (fns);
2395       mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2396
2397       for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2398         {
2399           tree fn = OVL_CURRENT (fns);
2400           if (TREE_CODE (fn) == TEMPLATE_DECL)
2401             {
2402               templates = scratch_tree_cons (NULL_TREE, fn, templates);
2403               candidates 
2404                 = add_template_candidate (candidates, fn, NULL_TREE,
2405                                           mem_args, NULL_TREE, 
2406                                           LOOKUP_NORMAL, DEDUCE_CALL);
2407             }
2408           else
2409             candidates = add_function_candidate
2410               (candidates, fn, mem_args, LOOKUP_NORMAL);
2411
2412           if (candidates)
2413             candidates->basetype_path = base;
2414         }
2415     }
2416
2417   convs = lookup_conversions (type);
2418
2419   for (; convs; convs = TREE_CHAIN (convs))
2420     {
2421       tree fns = TREE_VALUE (convs);
2422       tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2423
2424       if ((TREE_CODE (totype) == POINTER_TYPE
2425            || TREE_CODE (totype) == REFERENCE_TYPE)
2426           && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2427         for (; fns; fns = OVL_NEXT (fns))
2428           {
2429             tree fn = OVL_CURRENT (fns);
2430             if (TREE_CODE (fn) == TEMPLATE_DECL) 
2431               {
2432                 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2433                 candidates = add_template_conv_candidate (candidates,
2434                                                           fn,
2435                                                           obj,
2436                                                           args,
2437                                                           totype);
2438               }
2439             else
2440               candidates = add_conv_candidate (candidates, fn, obj, args);
2441
2442             if (candidates)
2443               candidates->basetype_path = TREE_PURPOSE (convs);
2444           }
2445     }
2446
2447   if (! any_viable (candidates))
2448     {
2449       cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2450       print_z_candidates (candidates);
2451       return error_mark_node;
2452     }
2453
2454   candidates = splice_viable (candidates);
2455   cand = tourney (candidates);
2456
2457   if (cand == 0)
2458     {
2459       cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2460       print_z_candidates (candidates);
2461       return error_mark_node;
2462     }
2463
2464   if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2465     return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2466
2467   obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2468
2469   /* FIXME */
2470   return build_function_call (obj, args);
2471 }
2472
2473 static void
2474 op_error (code, code2, arg1, arg2, arg3, problem)
2475      enum tree_code code, code2;
2476      tree arg1, arg2, arg3;
2477      char *problem;
2478 {
2479   char * opname
2480     = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2481
2482   switch (code)
2483     {
2484     case COND_EXPR:
2485       cp_error ("%s for `%T ? %T : %T'", problem,
2486                 error_type (arg1), error_type (arg2), error_type (arg3));
2487       break;
2488     case POSTINCREMENT_EXPR:
2489     case POSTDECREMENT_EXPR:
2490       cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2491       break;
2492     case ARRAY_REF:
2493       cp_error ("%s for `%T[%T]'", problem,
2494                 error_type (arg1), error_type (arg2));
2495       break;
2496     default:
2497       if (arg2)
2498         cp_error ("%s for `%T %s %T'", problem,
2499                   error_type (arg1), opname, error_type (arg2));
2500       else
2501         cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2502     }
2503 }
2504
2505 tree
2506 build_new_op (code, flags, arg1, arg2, arg3)
2507      enum tree_code code;
2508      int flags;
2509      tree arg1, arg2, arg3;
2510 {
2511   struct z_candidate *candidates = 0, *cand;
2512   tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2513   enum tree_code code2 = NOP_EXPR;
2514   tree templates = NULL_TREE;
2515   tree conv;
2516
2517   if (arg1 == error_mark_node
2518       || arg2 == error_mark_node
2519       || arg3 == error_mark_node)
2520     return error_mark_node;
2521
2522   /* This can happen if a template takes all non-type parameters, e.g.
2523      undeclared_template<1, 5, 72>a;  */
2524   if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2525     {
2526       cp_error ("`%D' must be declared before use", arg1);
2527       return error_mark_node;
2528     }
2529
2530   if (code == MODIFY_EXPR)
2531     {
2532       code2 = TREE_CODE (arg3);
2533       arg3 = NULL_TREE;
2534       fnname = ansi_assopname[code2];
2535     }
2536   else
2537     fnname = ansi_opname[code];
2538
2539   switch (code)
2540     {
2541     case NEW_EXPR:
2542     case VEC_NEW_EXPR:
2543     case VEC_DELETE_EXPR:
2544     case DELETE_EXPR:
2545       /* Use build_op_new_call and build_op_delete_call instead. */
2546       my_friendly_abort (981018);
2547
2548     case CALL_EXPR:
2549       return build_object_call (arg1, arg2);
2550
2551     default:
2552       break;
2553     }
2554
2555   /* The comma operator can have void args.  */
2556   if (TREE_CODE (arg1) == OFFSET_REF)
2557     arg1 = resolve_offset_ref (arg1);
2558   if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2559     arg2 = resolve_offset_ref (arg2);
2560   if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2561     arg3 = resolve_offset_ref (arg3);
2562
2563   if (code == COND_EXPR)
2564     {
2565       if (arg2 == NULL_TREE
2566           || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2567           || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2568           || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2569               && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2570         goto builtin;
2571     }
2572   else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2573            && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2574     goto builtin;
2575
2576   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2577     arg2 = integer_zero_node;
2578
2579   if (arg2 && arg3)
2580     arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2581                       (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2582   else if (arg2)
2583     arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2584   else
2585     arglist = build_scratch_list (NULL_TREE, arg1);
2586
2587   fns = lookup_function_nonclass (fnname, arglist);
2588
2589   if (fns && TREE_CODE (fns) == TREE_LIST)
2590     fns = TREE_VALUE (fns);
2591   for (; fns; fns = OVL_NEXT (fns))
2592     {
2593       tree fn = OVL_CURRENT (fns);
2594       if (TREE_CODE (fn) == TEMPLATE_DECL)
2595         {
2596           templates = scratch_tree_cons (NULL_TREE, fn, templates);
2597           candidates 
2598             = add_template_candidate (candidates, fn, NULL_TREE,
2599                                       arglist, TREE_TYPE (fnname),
2600                                       flags, DEDUCE_CALL); 
2601         }
2602       else
2603         candidates = add_function_candidate (candidates, fn, arglist, flags);
2604     }
2605
2606   if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2607     {
2608       fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2609       if (fns == error_mark_node)
2610         return fns;
2611     }
2612   else
2613     fns = NULL_TREE;
2614
2615   if (fns)
2616     {
2617       tree basetype = TREE_PURPOSE (fns);
2618       mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2619       for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2620         {
2621           tree fn = OVL_CURRENT (fns);
2622           tree this_arglist;
2623
2624           if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2625             this_arglist = mem_arglist;
2626           else
2627             this_arglist = arglist;
2628
2629           if (TREE_CODE (fn) == TEMPLATE_DECL)
2630             {
2631               /* A member template. */
2632               templates = scratch_tree_cons (NULL_TREE, fn, templates);
2633               candidates 
2634                 = add_template_candidate (candidates, fn, NULL_TREE,
2635                                           this_arglist,  TREE_TYPE (fnname),
2636                                           flags, DEDUCE_CALL); 
2637             }
2638           else
2639             candidates = add_function_candidate
2640               (candidates, fn, this_arglist, flags);
2641
2642           if (candidates) 
2643             candidates->basetype_path = basetype;
2644         }
2645     }
2646
2647   {
2648     tree args[3];
2649
2650     /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2651        to know about two args; a builtin candidate will always have a first
2652        parameter of type bool.  We'll handle that in
2653        build_builtin_candidate.  */
2654     if (code == COND_EXPR)
2655       {
2656         args[0] = arg2;
2657         args[1] = arg3;
2658         args[2] = arg1;
2659       }
2660     else
2661       {
2662         args[0] = arg1;
2663         args[1] = arg2;
2664         args[2] = NULL_TREE;
2665       }
2666
2667     candidates = add_builtin_candidates
2668       (candidates, code, code2, fnname, args, flags);
2669   }
2670
2671   if (! any_viable (candidates))
2672     {
2673       switch (code)
2674         {
2675         case POSTINCREMENT_EXPR:
2676         case POSTDECREMENT_EXPR:
2677           /* Look for an `operator++ (int)'.  If they didn't have
2678              one, then we fall back to the old way of doing things.  */
2679           if (flags & LOOKUP_COMPLAIN)
2680             cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2681                         fnname, opname_tab [code]);
2682           if (code == POSTINCREMENT_EXPR)
2683             code = PREINCREMENT_EXPR;
2684           else
2685             code = PREDECREMENT_EXPR;   
2686           return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2687           
2688           /* The caller will deal with these.  */
2689         case ADDR_EXPR:
2690         case COMPOUND_EXPR:
2691         case COMPONENT_REF:
2692           return NULL_TREE;
2693
2694         default:
2695           break;
2696         }
2697       if (flags & LOOKUP_COMPLAIN)
2698         {
2699           op_error (code, code2, arg1, arg2, arg3, "no match");
2700           print_z_candidates (candidates);
2701         }
2702       return error_mark_node;
2703     }
2704   candidates = splice_viable (candidates);
2705   cand = tourney (candidates);
2706
2707   if (cand == 0)
2708     {
2709       if (flags & LOOKUP_COMPLAIN)
2710         {
2711           op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2712           print_z_candidates (candidates);
2713         }
2714       return error_mark_node;
2715     }
2716
2717   if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2718     {
2719       extern int warn_synth;
2720       if (warn_synth
2721           && fnname == ansi_opname[MODIFY_EXPR]
2722           && DECL_ARTIFICIAL (cand->fn)
2723           && candidates->next
2724           && ! candidates->next->next)
2725         {
2726           cp_warning ("using synthesized `%#D' for copy assignment",
2727                       cand->fn);
2728           cp_warning_at ("  where cfront would use `%#D'",
2729                          cand == candidates
2730                          ? candidates->next->fn
2731                          : candidates->fn);
2732         }
2733
2734       /* Pedantically, normal function declarations are never considered
2735          to refer to template instantiations, so we only do this with
2736          -fguiding-decls.  */ 
2737       if (flag_guiding_decls && templates && ! cand->template 
2738           && ! DECL_INITIAL (cand->fn)
2739           && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2740         add_maybe_template (cand->fn, templates);
2741
2742       return build_over_call
2743         (cand,
2744          TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2745          ? mem_arglist : arglist,
2746          LOOKUP_NORMAL);
2747     }
2748
2749   /* Check for comparison of different enum types.  */
2750   switch (code)
2751     {
2752     case GT_EXPR:
2753     case LT_EXPR:
2754     case GE_EXPR:
2755     case LE_EXPR:
2756     case EQ_EXPR:
2757     case NE_EXPR:
2758       if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 
2759           && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 
2760           && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2761               != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2762         {
2763           cp_warning ("comparison between `%#T' and `%#T'", 
2764                       TREE_TYPE (arg1), TREE_TYPE (arg2));
2765         }
2766       break;
2767     default:
2768       break;
2769     }
2770
2771   /* We need to strip any leading REF_BIND so that bitfields don't cause
2772      errors.  This should not remove any important conversions, because
2773      builtins don't apply to class objects directly.  */
2774   conv = TREE_VEC_ELT (cand->convs, 0);
2775   if (TREE_CODE (conv) == REF_BIND)
2776     conv = TREE_OPERAND (conv, 0);
2777   arg1 = convert_like (conv, arg1);
2778   if (arg2)
2779     arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2780   if (arg3)
2781     arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2782
2783 builtin:
2784   switch (code)
2785     {
2786     case MODIFY_EXPR:
2787       return build_modify_expr (arg1, code2, arg2);
2788
2789     case INDIRECT_REF:
2790       return build_indirect_ref (arg1, "unary *");
2791
2792     case PLUS_EXPR:
2793     case MINUS_EXPR:
2794     case MULT_EXPR:
2795     case TRUNC_DIV_EXPR:
2796     case GT_EXPR:
2797     case LT_EXPR:
2798     case GE_EXPR:
2799     case LE_EXPR:
2800     case EQ_EXPR:
2801     case NE_EXPR:
2802     case MAX_EXPR:
2803     case MIN_EXPR:
2804     case LSHIFT_EXPR:
2805     case RSHIFT_EXPR:
2806     case TRUNC_MOD_EXPR:
2807     case BIT_AND_EXPR:
2808     case BIT_IOR_EXPR:
2809     case BIT_XOR_EXPR:
2810     case TRUTH_ANDIF_EXPR:
2811     case TRUTH_ORIF_EXPR:
2812       return build_binary_op_nodefault (code, arg1, arg2, code);
2813
2814     case CONVERT_EXPR:
2815     case NEGATE_EXPR:
2816     case BIT_NOT_EXPR:
2817     case TRUTH_NOT_EXPR:
2818     case PREINCREMENT_EXPR:
2819     case POSTINCREMENT_EXPR:
2820     case PREDECREMENT_EXPR:
2821     case POSTDECREMENT_EXPR:
2822     case REALPART_EXPR:
2823     case IMAGPART_EXPR:
2824       return build_unary_op (code, arg1, candidates != 0);
2825
2826     case ARRAY_REF:
2827       return build_array_ref (arg1, arg2);
2828
2829     case COND_EXPR:
2830       return build_conditional_expr (arg1, arg2, arg3);
2831
2832     case MEMBER_REF:
2833       return build_m_component_ref
2834         (build_indirect_ref (arg1, NULL_PTR), arg2);
2835
2836       /* The caller will deal with these.  */
2837     case ADDR_EXPR:
2838     case COMPONENT_REF:
2839     case COMPOUND_EXPR:
2840       return NULL_TREE;
2841
2842     default:
2843       my_friendly_abort (367);
2844       return NULL_TREE;
2845     }
2846 }
2847
2848 /* Build up a call to operator new.  This has to be handled differently
2849    from other operators in the way lookup is handled; first members are
2850    considered, then globals.  CODE is either NEW_EXPR or VEC_NEW_EXPR.
2851    TYPE is the type to be created.  ARGS are any new-placement args.
2852    FLAGS are the usual overloading flags.  */
2853
2854 tree
2855 build_op_new_call (code, type, args, flags)
2856      enum tree_code code;
2857      tree type, args;
2858      int flags;
2859 {
2860   tree fnname = ansi_opname[code];
2861
2862   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2863       && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2864     {
2865       return build_method_call (build_dummy_object (type),
2866                                 fnname, args, NULL_TREE, flags);
2867     }
2868   else
2869     return build_new_function_call 
2870       (lookup_function_nonclass (fnname, args), args);
2871 }
2872
2873 /* Build a call to operator delete.  This has to be handled very specially,
2874    because the restrictions on what signatures match are different from all
2875    other call instances.  For a normal delete, only a delete taking (void *)
2876    or (void *, size_t) is accepted.  For a placement delete, only an exact
2877    match with the placement new is accepted.
2878
2879    CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2880    ADDR is the pointer to be deleted.  For placement delete, it is also
2881      used to determine what the corresponding new looked like.
2882    SIZE is the size of the memory block to be deleted.
2883    FLAGS are the usual overloading flags.
2884    PLACEMENT is the corresponding placement new call, or 0.  */
2885
2886 tree
2887 build_op_delete_call (code, addr, size, flags, placement)
2888      enum tree_code code;
2889      tree addr, size, placement;
2890      int flags;
2891 {
2892   tree fn, fns, fnname, fntype, argtypes, args, type;
2893
2894   if (addr == error_mark_node)
2895     return error_mark_node;
2896
2897   type = TREE_TYPE (TREE_TYPE (addr));
2898   fnname = ansi_opname[code];
2899
2900   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2901     /* In [class.free]
2902
2903        If the result of the lookup is ambiguous or inaccessible, or if
2904        the lookup selects a placement deallocation function, the
2905        program is ill-formed.
2906   
2907        Therefore, we ask lookup_fnfields to complain ambout ambiguity.  */
2908     {
2909       fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2910       if (fns == error_mark_node)
2911         return error_mark_node;
2912     }
2913   else
2914     fns = NULL_TREE;
2915
2916   if (fns == NULL_TREE)
2917     fns = lookup_name_nonclass (fnname);
2918
2919   if (placement)
2920     {
2921       /* placement is a CALL_EXPR around an ADDR_EXPR around a function.  */
2922
2923       /* Extract the function.  */
2924       argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2925       /* Then the second parm type.  */
2926       argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2927
2928       /* Also the second argument.  */
2929       args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2930     }
2931   else
2932     {
2933       /* First try it without the size argument.  */
2934       argtypes = void_list_node;
2935       args = NULL_TREE;
2936     }
2937
2938   argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2939   fntype = build_function_type (void_type_node, argtypes);
2940
2941   /* Strip const and volatile from addr.  */
2942   if (type != TYPE_MAIN_VARIANT (type))
2943     addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2944
2945   fn = instantiate_type (fntype, fns, 0);
2946
2947   if (fn != error_mark_node)
2948     {
2949       if (TREE_CODE (fns) == TREE_LIST)
2950         /* Member functions.  */
2951         enforce_access (TREE_PURPOSE (fns), fn);
2952       return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
2953     }
2954
2955   /* If we are doing placement delete we do nothing if we don't find a
2956      matching op delete.  */
2957   if (placement)
2958     return NULL_TREE;
2959
2960   /* Normal delete; now try to find a match including the size argument.  */
2961   argtypes = tree_cons (NULL_TREE, ptr_type_node,
2962                         tree_cons (NULL_TREE, sizetype, void_list_node));
2963   fntype = build_function_type (void_type_node, argtypes);
2964
2965   fn = instantiate_type (fntype, fns, 0);
2966
2967   if (fn != error_mark_node)
2968     {
2969       if (TREE_CODE (fns) == TREE_LIST)
2970         /* Member functions.  */
2971         enforce_access (TREE_PURPOSE (fns), fn);
2972       return build_function_call
2973         (fn, expr_tree_cons (NULL_TREE, addr,
2974                              build_expr_list (NULL_TREE, size)));
2975     }
2976
2977   /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
2978      destructor, in which case the error should be deferred
2979      until someone actually tries to delete one of these.  */
2980   if (flags & LOOKUP_SPECULATIVELY)
2981     return NULL_TREE;
2982
2983   cp_error ("no suitable operator delete for `%T'", type);
2984   return error_mark_node;
2985 }
2986
2987 /* If the current scope isn't allowed to access DECL along
2988    BASETYPE_PATH, give an error.  */
2989
2990 void
2991 enforce_access (basetype_path, decl)
2992      tree basetype_path, decl;
2993 {
2994   tree access = compute_access (basetype_path, decl);
2995
2996   if (access == access_private_node)
2997     {
2998       cp_error_at ("`%+#D' is %s", decl, 
2999                    TREE_PRIVATE (decl) ? "private"
3000                    : "from private base class");
3001       error ("within this context");
3002     }
3003   else if (access == access_protected_node)
3004     {
3005       cp_error_at ("`%+#D' %s", decl,
3006                    TREE_PROTECTED (decl) ? "is protected"
3007                    : "has protected accessibility");
3008       error ("within this context");
3009     }
3010 }
3011
3012 /* Perform the conversions in CONVS on the expression EXPR.  */
3013
3014 static tree
3015 convert_like (convs, expr)
3016      tree convs, expr;
3017 {
3018   if (ICS_BAD_FLAG (convs)
3019       && TREE_CODE (convs) != USER_CONV
3020       && TREE_CODE (convs) != AMBIG_CONV)
3021     {
3022       tree t = convs; 
3023       for (; t; t = TREE_OPERAND (t, 0))
3024         {
3025           if (TREE_CODE (t) == USER_CONV)
3026             {
3027               expr = convert_like (t, expr);
3028               break;
3029             }
3030           else if (TREE_CODE (t) == AMBIG_CONV)
3031             return convert_like (t, expr);
3032           else if (TREE_CODE (t) == IDENTITY_CONV)
3033             break;
3034         }
3035       return convert_for_initialization
3036         (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3037          "conversion", NULL_TREE, 0);
3038     }
3039
3040   switch (TREE_CODE (convs))
3041     {
3042     case USER_CONV:
3043       {
3044         struct z_candidate *cand
3045           = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3046         tree fn = cand->fn;
3047         tree args;
3048
3049         if (DECL_CONSTRUCTOR_P (fn))
3050           {
3051             tree t = build_int_2 (0, 0);
3052             TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3053
3054             args = build_scratch_list (NULL_TREE, expr);
3055             if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3056               args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3057             args = scratch_tree_cons (NULL_TREE, t, args);
3058           }
3059         else
3060           args = build_this (expr);
3061         expr = build_over_call (cand, args, LOOKUP_NORMAL);
3062
3063         /* If this is a constructor or a function returning an aggr type,
3064            we need to build up a TARGET_EXPR.  */
3065         if (DECL_CONSTRUCTOR_P (fn))
3066           expr = build_cplus_new (TREE_TYPE (convs), expr);
3067
3068         return expr;
3069       }
3070     case IDENTITY_CONV:
3071       if (type_unknown_p (expr))
3072         expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3073       if (TREE_READONLY_DECL_P (expr))
3074         expr = decl_constant_value (expr);
3075       return expr;
3076     case AMBIG_CONV:
3077       /* Call build_user_type_conversion again for the error.  */
3078       return build_user_type_conversion
3079         (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3080
3081     default:
3082       break;
3083     };
3084
3085   expr = convert_like (TREE_OPERAND (convs, 0), expr);
3086   if (expr == error_mark_node)
3087     return error_mark_node;
3088
3089   switch (TREE_CODE (convs))
3090     {
3091     case RVALUE_CONV:
3092       if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3093         return expr;
3094       /* else fall through */
3095     case BASE_CONV:
3096       {
3097         tree cvt_expr = build_user_type_conversion
3098           (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3099         if (!cvt_expr) 
3100           {
3101             /* This can occur if, for example, the EXPR has incomplete
3102                type.  We can't check for that before attempting the
3103                conversion because the type might be an incomplete
3104                array type, which is OK if some constructor for the
3105                destination type takes a pointer argument.  */
3106             if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
3107               {
3108                 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
3109                   incomplete_type_error (expr, TREE_TYPE (expr));
3110                 else
3111                   cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3112                             expr, TREE_TYPE (expr), TREE_TYPE (convs));
3113               }
3114             else
3115               cp_error ("could not convert `%E' to `%T'",
3116                         expr, TREE_TYPE (convs));
3117             return error_mark_node;
3118           }
3119         return cvt_expr;
3120       }
3121
3122     case REF_BIND:
3123       return convert_to_reference
3124         (TREE_TYPE (convs), expr,
3125          CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3126          error_mark_node);
3127     case LVALUE_CONV:
3128       return decay_conversion (expr);
3129
3130     case QUAL_CONV:
3131       /* Warn about deprecated conversion if appropriate.  */
3132       string_conv_p (TREE_TYPE (convs), expr, 1);
3133       break;
3134       
3135     default:
3136       break;
3137     }
3138   return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3139                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3140 }
3141
3142 /* ARG is being passed to a varargs function.  Perform any conversions
3143    required.  Return the converted value.  */
3144
3145 tree
3146 convert_arg_to_ellipsis (arg)
3147      tree arg;
3148 {
3149   if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3150       && (TYPE_PRECISION (TREE_TYPE (arg))
3151           < TYPE_PRECISION (double_type_node)))
3152     /* Convert `float' to `double'.  */
3153     arg = cp_convert (double_type_node, arg);
3154   else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3155            && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3156     cp_warning ("cannot pass objects of type `%T' through `...'",
3157                 TREE_TYPE (arg));
3158   else
3159     /* Convert `short' and `char' to full-size `int'.  */
3160     arg = default_conversion (arg);
3161
3162   return arg;
3163 }
3164
3165 /* ARG is a default argument expression being passed to a parameter of
3166    the indicated TYPE.  Do any required conversions.  Return the
3167    converted value.  */
3168
3169 tree
3170 convert_default_arg (type, arg)
3171      tree type, arg;
3172 {
3173   arg = break_out_target_exprs (arg);
3174
3175   if (TREE_CODE (arg) == CONSTRUCTOR)
3176     {
3177       arg = digest_init (type, arg, 0);
3178       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3179                                         "default argument", 0, 0);
3180     }
3181   else
3182     {
3183       /* This could get clobbered by the following call.  */
3184       if (TREE_HAS_CONSTRUCTOR (arg))
3185         arg = copy_node (arg);
3186
3187       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3188                                         "default argument", 0, 0);
3189 #ifdef PROMOTE_PROTOTYPES
3190       if ((TREE_CODE (type) == INTEGER_TYPE
3191            || TREE_CODE (type) == ENUMERAL_TYPE)
3192           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3193         arg = default_conversion (arg);
3194 #endif
3195     }
3196
3197   return arg;
3198 }
3199
3200 static tree
3201 build_over_call (cand, args, flags)
3202      struct z_candidate *cand;
3203      tree args;
3204      int flags;
3205 {
3206   tree fn = cand->fn;
3207   tree convs = cand->convs;
3208   tree converted_args = NULL_TREE;
3209   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3210   tree conv, arg, val;
3211   int i = 0;
3212   int is_method = 0;
3213
3214   /* Give any warnings we noticed during overload resolution.  */
3215   if (cand->warnings)
3216     for (val = cand->warnings; val; val = TREE_CHAIN (val))
3217       joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3218
3219   if (DECL_FUNCTION_MEMBER_P (fn))
3220     enforce_access (cand->basetype_path, fn);
3221
3222   if (args && TREE_CODE (args) != TREE_LIST)
3223     args = build_scratch_list (NULL_TREE, args);
3224   arg = args;
3225
3226   /* The implicit parameters to a constructor are not considered by overload
3227      resolution, and must be of the proper type.  */
3228   if (DECL_CONSTRUCTOR_P (fn))
3229     {
3230       converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3231       arg = TREE_CHAIN (arg);
3232       parm = TREE_CHAIN (parm);
3233       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3234         {
3235           converted_args = expr_tree_cons
3236             (NULL_TREE, TREE_VALUE (arg), converted_args);
3237           arg = TREE_CHAIN (arg);
3238           parm = TREE_CHAIN (parm);
3239         }
3240     }      
3241   /* Bypass access control for 'this' parameter.  */
3242   else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3243     {
3244       tree parmtype = TREE_VALUE (parm);
3245       tree argtype = TREE_TYPE (TREE_VALUE (arg));
3246       tree t;
3247       if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3248         cp_error ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3249                   TREE_TYPE (argtype), fn);
3250
3251       /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3252          X is called for an object that is not of type X, or of a type
3253          derived from X, the behavior is undefined.
3254
3255          So we can assume that anything passed as 'this' is non-null, and
3256          optimize accordingly.  */
3257       if (TREE_CODE (parmtype) == POINTER_TYPE)
3258         t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3259       else
3260         /* This happens with signatures.  */
3261         t = convert_force (parmtype, TREE_VALUE (arg), CONV_C_CAST);
3262       converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3263       parm = TREE_CHAIN (parm);
3264       arg = TREE_CHAIN (arg);
3265       ++i;
3266       is_method = 1;
3267     }
3268
3269   for (; arg && parm;
3270        parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3271     {
3272       tree type = TREE_VALUE (parm);
3273
3274       conv = TREE_VEC_ELT (convs, i);
3275       if (ICS_BAD_FLAG (conv))
3276         {
3277           tree t = conv;
3278           val = TREE_VALUE (arg);
3279
3280           for (; t; t = TREE_OPERAND (t, 0))
3281             {
3282               if (TREE_CODE (t) == USER_CONV
3283                   || TREE_CODE (t) == AMBIG_CONV)
3284                 {
3285                   val = convert_like (t, val);
3286                   break;
3287                 }
3288               else if (TREE_CODE (t) == IDENTITY_CONV)
3289                 break;
3290             }
3291           val = convert_for_initialization
3292             (NULL_TREE, type, val, LOOKUP_NORMAL,
3293              "argument passing", fn, i - is_method);
3294         }
3295       else
3296         {
3297           /* Issue warnings about peculiar, but legal, uses of NULL.  */
3298           if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3299               && TREE_VALUE (arg) == null_node)
3300             cp_warning ("converting NULL to non-pointer type");
3301             
3302           val = convert_like (conv, TREE_VALUE (arg));
3303         }
3304
3305 #ifdef PROMOTE_PROTOTYPES
3306       if ((TREE_CODE (type) == INTEGER_TYPE
3307            || TREE_CODE (type) == ENUMERAL_TYPE)
3308           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3309         val = default_conversion (val);
3310 #endif
3311       converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3312     }
3313
3314   /* Default arguments */
3315   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3316     {
3317       tree arg = TREE_PURPOSE (parm);
3318
3319       if (DECL_TEMPLATE_INFO (fn))
3320         {
3321           /* This came from a template.  Instantiate the default arg here,
3322              not in tsubst.  In the case of something like:
3323
3324                template <class T>
3325                struct S {
3326                  static T t();
3327                  void f(T = t());
3328                };
3329
3330              we must be careful to do name lookup in the scope of
3331              S<T>, rather than in the current class.  */
3332           if (DECL_CLASS_SCOPE_P (fn))
3333             pushclass (DECL_REAL_CONTEXT (fn), 2);
3334
3335           arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
3336
3337           if (DECL_CLASS_SCOPE_P (fn))
3338             popclass (1);
3339         }
3340       converted_args = expr_tree_cons
3341         (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
3342          converted_args);
3343     }
3344
3345   /* Ellipsis */
3346   for (; arg; arg = TREE_CHAIN (arg))
3347     converted_args 
3348       = expr_tree_cons (NULL_TREE,
3349                         convert_arg_to_ellipsis (TREE_VALUE (arg)),
3350                         converted_args);
3351
3352   converted_args = nreverse (converted_args);
3353
3354   /* Avoid actually calling copy constructors and copy assignment operators,
3355      if possible.  */
3356
3357   if (! flag_elide_constructors)
3358     /* Do things the hard way.  */;
3359   else if (DECL_CONSTRUCTOR_P (fn)
3360            && TREE_VEC_LENGTH (convs) == 1
3361       && copy_args_p (fn))
3362     {
3363       tree targ;
3364       arg = TREE_CHAIN (converted_args);
3365       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3366         arg = TREE_CHAIN (arg);
3367       arg = TREE_VALUE (arg);
3368
3369       /* Pull out the real argument, disregarding const-correctness.  */
3370       targ = arg;
3371       while (TREE_CODE (targ) == NOP_EXPR
3372              || TREE_CODE (targ) == NON_LVALUE_EXPR
3373              || TREE_CODE (targ) == CONVERT_EXPR)
3374         targ = TREE_OPERAND (targ, 0);
3375       if (TREE_CODE (targ) == ADDR_EXPR)
3376         {
3377           targ = TREE_OPERAND (targ, 0);
3378           if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3379                             TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3380             targ = NULL_TREE;
3381         }
3382       else
3383         targ = NULL_TREE;
3384
3385       if (targ)
3386         arg = targ;
3387       else
3388         arg = build_indirect_ref (arg, 0);
3389
3390       /* [class.copy]: the copy constructor is implicitly defined even if
3391          the implementation elided its use.  */
3392       if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3393         mark_used (fn);
3394
3395       /* If we're creating a temp and we already have one, don't create a
3396          new one.  If we're not creating a temp but we get one, use
3397          INIT_EXPR to collapse the temp into our target.  Otherwise, if the
3398          ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3399          temp or an INIT_EXPR otherwise.  */
3400       if (integer_zerop (TREE_VALUE (args)))
3401         {
3402           if (! real_lvalue_p (arg))
3403             return arg;
3404           else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3405             {
3406               val = build (VAR_DECL, DECL_CONTEXT (fn));
3407               layout_decl (val, 0);
3408               val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3409               TREE_SIDE_EFFECTS (val) = 1;
3410               return val;
3411             }
3412         }
3413       else if (! real_lvalue_p (arg)
3414                || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3415         {
3416           tree to = stabilize_reference
3417             (build_indirect_ref (TREE_VALUE (args), 0));
3418
3419           /* Don't copy the padding byte; it might not have been allocated
3420              if to is a base subobject.  */
3421           if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3422             return build_unary_op
3423               (ADDR_EXPR, build (COMPOUND_EXPR, TREE_TYPE (to),
3424                                  cp_convert (void_type_node, arg), to),
3425                0);
3426
3427           val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3428           TREE_SIDE_EFFECTS (val) = 1;
3429           return build_unary_op (ADDR_EXPR, val, 0);
3430         }
3431     }
3432   else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3433            && copy_args_p (fn)
3434            && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3435     {
3436       tree to = stabilize_reference
3437         (build_indirect_ref (TREE_VALUE (converted_args), 0));
3438
3439       arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3440
3441       /* Don't copy the padding byte; it might not have been allocated
3442          if to is a base subobject.  */
3443       if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3444         return build (COMPOUND_EXPR, TREE_TYPE (to),
3445                       cp_convert (void_type_node, arg), to);
3446
3447       val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3448       TREE_SIDE_EFFECTS (val) = 1;
3449       return val;
3450     }
3451
3452   mark_used (fn);
3453
3454   if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3455     return build_signature_method_call (fn, converted_args);
3456   else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3457     {
3458       tree t, *p = &TREE_VALUE (converted_args);
3459       tree binfo = get_binfo
3460         (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3461       *p = convert_pointer_to_real (binfo, *p);
3462       if (TREE_SIDE_EFFECTS (*p))
3463         *p = save_expr (*p);
3464       t = build_pointer_type (TREE_TYPE (fn));
3465       fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3466       TREE_TYPE (fn) = t;
3467     }
3468   else if (DECL_INLINE (fn))
3469     fn = inline_conversion (fn);
3470   else
3471     fn = build_addr_func (fn);
3472
3473   /* Recognize certain built-in functions so we can make tree-codes
3474      other than CALL_EXPR.  We do this when it enables fold-const.c
3475      to do something useful.  */
3476
3477   if (TREE_CODE (fn) == ADDR_EXPR
3478       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3479       && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3480     switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3481       {
3482       case BUILT_IN_ABS:
3483       case BUILT_IN_LABS:
3484       case BUILT_IN_FABS:
3485         if (converted_args == 0)
3486           return integer_zero_node;
3487         return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3488       default:
3489         break;
3490       }
3491
3492   fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3493   if (TREE_TYPE (fn) == void_type_node)
3494     return fn;
3495   fn = require_complete_type (fn);
3496   if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3497     fn = build_cplus_new (TREE_TYPE (fn), fn);
3498   return convert_from_reference (fn);
3499 }
3500
3501 static tree
3502 build_new_method_call (instance, name, args, basetype_path, flags)
3503      tree instance, name, args, basetype_path;
3504      int flags;
3505 {
3506   struct z_candidate *candidates = 0, *cand;
3507   tree explicit_targs = NULL_TREE;
3508   tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3509   tree pretty_name;
3510   tree user_args = args;
3511   tree templates = NULL_TREE;
3512   int template_only = 0;
3513
3514   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3515     {
3516       explicit_targs = TREE_OPERAND (name, 1);
3517       name = TREE_OPERAND (name, 0);
3518       if (TREE_CODE (name) == TEMPLATE_DECL)
3519         name = DECL_NAME (name);
3520       template_only = 1;
3521     }
3522
3523   /* If there is an extra argument for controlling virtual bases,
3524      remove it for error reporting.  */
3525   if (flags & LOOKUP_HAS_IN_CHARGE)
3526     user_args = TREE_CHAIN (args);
3527
3528   args = resolve_args (args);
3529
3530   if (args == error_mark_node)
3531     return error_mark_node;
3532
3533   if (instance == NULL_TREE)
3534     basetype = BINFO_TYPE (basetype_path);
3535   else
3536     {
3537       if (TREE_CODE (instance) == OFFSET_REF)
3538         instance = resolve_offset_ref (instance);
3539       if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3540         instance = convert_from_reference (instance);
3541       basetype = TREE_TYPE (instance);
3542
3543       /* XXX this should be handled before we get here.  */
3544       if (! IS_AGGR_TYPE (basetype)
3545           && ! (TYPE_LANG_SPECIFIC (basetype)
3546                 && (IS_SIGNATURE_POINTER (basetype)
3547                     || IS_SIGNATURE_REFERENCE (basetype))))
3548         {
3549           if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3550             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3551                       name, instance, basetype);
3552
3553           return error_mark_node;
3554         }
3555
3556       /* If `instance' is a signature pointer/reference and `name' is
3557          not a constructor, we are calling a signature member function.
3558          In that case set the `basetype' to the signature type.  */
3559       if ((IS_SIGNATURE_POINTER (basetype)
3560            || IS_SIGNATURE_REFERENCE (basetype))
3561           && TYPE_IDENTIFIER (basetype) != name)
3562         basetype = SIGNATURE_TYPE (basetype);
3563     }
3564
3565   if (basetype_path == NULL_TREE)
3566     basetype_path = TYPE_BINFO (basetype);
3567
3568   if (instance)
3569     {
3570       instance_ptr = build_this (instance);
3571
3572       if (! template_only)
3573         {
3574           /* XXX this should be handled before we get here.  */
3575           fns = build_field_call (basetype_path, instance_ptr, name, args);
3576           if (fns)
3577             return fns;
3578         }
3579     }
3580   else
3581     {
3582       instance_ptr = build_int_2 (0, 0);
3583       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3584     }
3585
3586   pretty_name
3587     = (name == ctor_identifier ? constructor_name (basetype) : name);
3588
3589   fns = lookup_fnfields (basetype_path, name, 1);
3590
3591   if (fns == error_mark_node)
3592     return error_mark_node;
3593   if (fns)
3594     {
3595       tree fn = TREE_VALUE (fns);
3596       if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3597           && ! (flags & LOOKUP_HAS_IN_CHARGE))
3598         {
3599           flags |= LOOKUP_HAS_IN_CHARGE;
3600           args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3601         }
3602       mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3603       for (; fn; fn = OVL_NEXT (fn))
3604         {
3605           tree t = OVL_CURRENT (fn);
3606           tree this_arglist;
3607
3608           /* We can end up here for copy-init of same or base class.  */
3609           if (name == ctor_identifier
3610               && (flags & LOOKUP_ONLYCONVERTING)
3611               && DECL_NONCONVERTING_P (t))
3612             continue;
3613           if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3614             this_arglist = mem_args;
3615           else
3616             this_arglist = args;
3617
3618           if (TREE_CODE (t) == TEMPLATE_DECL)
3619             {
3620               /* A member template. */
3621               templates = scratch_tree_cons (NULL_TREE, t, templates);
3622               candidates = 
3623                 add_template_candidate (candidates, t, explicit_targs,
3624                                         this_arglist,
3625                                         TREE_TYPE (name), flags, DEDUCE_CALL); 
3626             }
3627           else if (! template_only)
3628             candidates = add_function_candidate (candidates, t,
3629                                                  this_arglist, flags);
3630
3631           if (candidates)
3632             candidates->basetype_path = TREE_PURPOSE (fns);
3633         }
3634     }
3635
3636   if (! any_viable (candidates))
3637     {
3638       /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
3639       if (flags & LOOKUP_SPECULATIVELY)
3640         return NULL_TREE;
3641       if (TYPE_SIZE (basetype) == 0)
3642         incomplete_type_error (instance_ptr, basetype);
3643       else
3644         cp_error ("no matching function for call to `%T::%D (%A)%V'",
3645                   basetype, pretty_name, user_args,
3646                   TREE_TYPE (TREE_TYPE (instance_ptr)));
3647       print_z_candidates (candidates);
3648       return error_mark_node;
3649     }
3650   candidates = splice_viable (candidates);
3651   cand = tourney (candidates);
3652
3653   if (cand == 0)
3654     {
3655       cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3656                 user_args);
3657       print_z_candidates (candidates);
3658       return error_mark_node;
3659     }
3660
3661   if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3662       && instance == current_class_ref
3663       && DECL_CONSTRUCTOR_P (current_function_decl)
3664       && ! (flags & LOOKUP_NONVIRTUAL)
3665       && value_member (cand->fn, get_abstract_virtuals (basetype)))
3666     cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3667   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3668       && is_dummy_object (instance_ptr))
3669     cp_error ("cannot call member function `%D' without object", cand->fn);
3670
3671   if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3672       && ((instance == current_class_ref && (dtor_label || ctor_label))
3673           || resolves_to_fixed_type_p (instance, 0)))
3674     flags |= LOOKUP_NONVIRTUAL;
3675
3676   /* Pedantically, normal function declarations are never considered
3677      to refer to template instantiations, so we only do this with
3678      -fguiding-decls.  */ 
3679   if (flag_guiding_decls && templates && ! cand->template 
3680       && ! DECL_INITIAL (cand->fn))
3681     add_maybe_template (cand->fn, templates);
3682
3683   return build_over_call
3684     (cand,
3685      TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3686      flags);
3687 }
3688
3689 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3690    subsequence of ICS2.  */
3691
3692 static int
3693 is_subseq (ics1, ics2)
3694      tree ics1, ics2;
3695 {
3696   /* We can assume that a conversion of the same code
3697      between the same types indicates a subsequence since we only get
3698      here if the types we are converting from are the same.  */
3699
3700   while (TREE_CODE (ics1) == RVALUE_CONV
3701          || TREE_CODE (ics1) == LVALUE_CONV)
3702     ics1 = TREE_OPERAND (ics1, 0);
3703
3704   while (1)
3705     {
3706       while (TREE_CODE (ics2) == RVALUE_CONV
3707           || TREE_CODE (ics2) == LVALUE_CONV)
3708         ics2 = TREE_OPERAND (ics2, 0);
3709
3710       if (TREE_CODE (ics2) == USER_CONV
3711           || TREE_CODE (ics2) == AMBIG_CONV
3712           || TREE_CODE (ics2) == IDENTITY_CONV)
3713         /* At this point, ICS1 cannot be a proper subsequence of
3714            ICS2.  We can get a USER_CONV when we are comparing the
3715            second standard conversion sequence of two user conversion
3716            sequences.  */
3717         return 0;
3718
3719       ics2 = TREE_OPERAND (ics2, 0);
3720
3721       if (TREE_CODE (ics2) == TREE_CODE (ics1)
3722           && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
3723           && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3724                              TREE_TYPE (TREE_OPERAND (ics1, 0))))
3725         return 1;
3726     }
3727 }
3728
3729 /* Returns non-zero iff DERIVED is derived from BASE.  The inputs may
3730    be any _TYPE nodes.  */
3731
3732 static int
3733 is_properly_derived_from (derived, base)
3734      tree derived;
3735      tree base;
3736 {
3737   if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3738       || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3739     return 0;
3740
3741   /* We only allow proper derivation here.  The DERIVED_FROM_P macro
3742      considers every class derived from itself.  */
3743   return (!same_type_p (TYPE_MAIN_VARIANT (derived),
3744                         TYPE_MAIN_VARIANT (base))
3745           && DERIVED_FROM_P (base, derived));
3746 }
3747
3748 /* We build the ICS for an implicit object parameter as a pointer
3749    conversion sequence.  However, such a sequence should be compared
3750    as if it were a reference conversion sequence.  If ICS is the
3751    implicit conversion sequence for an implicit object parameter,
3752    modify it accordingly.  */
3753
3754 static void
3755 maybe_handle_implicit_object (ics)
3756      tree* ics;
3757 {
3758   if (ICS_THIS_FLAG (*ics))
3759     {
3760       /* [over.match.funcs]
3761          
3762          For non-static member functions, the type of the
3763          implicit object parameter is "reference to cv X"
3764          where X is the class of which the function is a
3765          member and cv is the cv-qualification on the member
3766          function declaration.  */
3767       tree t = *ics;
3768       if (TREE_CODE (t) == QUAL_CONV)
3769         t = TREE_OPERAND (t, 0);
3770       if (TREE_CODE (t) == PTR_CONV)
3771         t = TREE_OPERAND (t, 0);
3772       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3773       t = build_conv (REF_BIND, 
3774                       build_reference_type (TREE_TYPE (TREE_TYPE (*ics))), 
3775                       t);
3776       ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3777       *ics = t;
3778     }
3779 }
3780
3781 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3782    to the type the reference originally referred to, and return 1.
3783    Otherwise, return 0.  */
3784
3785 static int
3786 maybe_handle_ref_bind (ics, target_type)
3787      tree* ics;
3788      tree* target_type;
3789 {
3790   if (TREE_CODE (*ics) == REF_BIND)
3791     {
3792       /* [over.ics.rank] 
3793          
3794          When a parameter of reference type binds directly
3795          (_dcl.init.ref_) to an argument expression, the implicit
3796          conversion sequence is the identity conversion, unless the
3797          argument expression has a type that is a derived class of the
3798          parameter type, in which case the implicit conversion
3799          sequence is a derived-to-base Conversion.
3800          
3801          If the parameter binds directly to the result of applying a
3802          conversion function to the argument expression, the implicit
3803          conversion sequence is a user-defined conversion sequence
3804          (_over.ics.user_), with the second standard conversion
3805          sequence either an identity conversion or, if the conversion
3806          function returns an entity of a type that is a derived class
3807          of the parameter type, a derived-to-base Conversion.
3808          
3809          When a parameter of reference type is not bound directly to
3810          an argument expression, the conversion sequence is the one
3811          required to convert the argument expression to the underlying
3812          type of the reference according to _over.best.ics_.
3813          Conceptually, this conversion sequence corresponds to
3814          copy-initializing a temporary of the underlying type with the
3815          argument expression.  Any difference in top-level
3816          cv-qualification is subsumed by the initialization itself and
3817          does not constitute a conversion.  */
3818
3819       tree old_ics = *ics;
3820
3821       *target_type = TREE_TYPE (TREE_TYPE (*ics));
3822       *ics = TREE_OPERAND (*ics, 0);
3823       if (TREE_CODE (*ics) == IDENTITY_CONV
3824           && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3825         *ics = build_conv (BASE_CONV, *target_type, *ics);
3826       ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3827       ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3828       
3829       return 1;
3830     }
3831   
3832   return 0;
3833 }
3834
3835 /* Compare two implicit conversion sequences according to the rules set out in
3836    [over.ics.rank].  Return values:
3837
3838       1: ics1 is better than ics2
3839      -1: ics2 is better than ics1
3840       0: ics1 and ics2 are indistinguishable */
3841
3842 static int
3843 compare_ics (ics1, ics2)
3844      tree ics1, ics2;
3845 {
3846   tree from_type1;
3847   tree from_type2;
3848   tree to_type1;
3849   tree to_type2;
3850   tree deref_from_type1 = NULL_TREE;
3851   tree deref_from_type2 = NULL_TREE;
3852   tree deref_to_type1 = NULL_TREE;
3853   tree deref_to_type2 = NULL_TREE;
3854
3855   /* REF_BINDING is non-zero if the result of the conversion sequence
3856      is a reference type.   In that case TARGET_TYPE is the
3857      type referred to by the reference.  */
3858   int ref_binding1;
3859   int ref_binding2;
3860   tree target_type1;
3861   tree target_type2;
3862
3863   /* Handle implicit object parameters.  */
3864   maybe_handle_implicit_object (&ics1);
3865   maybe_handle_implicit_object (&ics2);
3866
3867   /* Handle reference parameters.  */
3868   ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3869   ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3870
3871   /* [over.ics.rank]
3872
3873      When  comparing  the  basic forms of implicit conversion sequences (as
3874      defined in _over.best.ics_)
3875
3876      --a standard conversion sequence (_over.ics.scs_) is a better
3877        conversion sequence than a user-defined conversion sequence
3878        or an ellipsis conversion sequence, and
3879      
3880      --a user-defined conversion sequence (_over.ics.user_) is a
3881        better conversion sequence than an ellipsis conversion sequence
3882        (_over.ics.ellipsis_).  */
3883   if (ICS_RANK (ics1) > ICS_RANK (ics2))
3884     return -1;
3885   else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3886     return 1;
3887
3888   if (ICS_RANK (ics1) == BAD_RANK)
3889     {
3890       /* Both ICS are bad.  We try to make a decision based on what
3891          would have happenned if they'd been good.  */
3892       if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3893           || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3894         return -1;
3895       else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3896                || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3897         return 1;
3898
3899       /* We couldn't make up our minds; try to figure it out below.  */
3900     }
3901
3902   if (ICS_ELLIPSIS_FLAG (ics1))
3903     /* Both conversions are ellipsis conversions.  */
3904     return 0;
3905
3906   /* User-defined  conversion sequence U1 is a better conversion sequence
3907      than another user-defined conversion sequence U2 if they contain the
3908      same user-defined conversion operator or constructor and if the sec-
3909      ond standard conversion sequence of U1 is  better  than  the  second
3910      standard conversion sequence of U2.  */
3911
3912   if (ICS_USER_FLAG (ics1))
3913     {
3914       tree t1, t2;
3915
3916       for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3917         if (TREE_CODE (t1) == AMBIG_CONV)
3918           return 0;
3919       for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3920         if (TREE_CODE (t2) == AMBIG_CONV)
3921           return 0;
3922
3923       if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3924         return 0;
3925
3926       /* We can just fall through here, after setting up
3927          FROM_TYPE1 and FROM_TYPE2.  */
3928       from_type1 = TREE_TYPE (t1);
3929       from_type2 = TREE_TYPE (t2);
3930     }
3931   else
3932     {
3933       /* We're dealing with two standard conversion sequences. 
3934
3935          [over.ics.rank]
3936          
3937          Standard conversion sequence S1 is a better conversion
3938          sequence than standard conversion sequence S2 if
3939      
3940          --S1 is a proper subsequence of S2 (comparing the conversion
3941            sequences in the canonical form defined by _over.ics.scs_,
3942            excluding any Lvalue Transformation; the identity
3943            conversion sequence is considered to be a subsequence of
3944            any non-identity conversion sequence */
3945       
3946       from_type1 = ics1;
3947       while (TREE_CODE (from_type1) != IDENTITY_CONV)
3948         from_type1 = TREE_OPERAND (from_type1, 0);
3949       from_type1 = TREE_TYPE (from_type1);
3950       
3951       from_type2 = ics2;
3952       while (TREE_CODE (from_type2) != IDENTITY_CONV)
3953         from_type2 = TREE_OPERAND (from_type2, 0);
3954       from_type2 = TREE_TYPE (from_type2);
3955     }
3956
3957   if (same_type_p (from_type1, from_type2))
3958     {
3959       if (is_subseq (ics1, ics2))
3960         return 1;
3961       if (is_subseq (ics2, ics1))
3962         return -1;
3963     }
3964   /* Otherwise, one sequence cannot be a subsequence of the other; they
3965      don't start with the same type.  This can happen when comparing the
3966      second standard conversion sequence in two user-defined conversion
3967      sequences.  */
3968
3969   /* [over.ics.rank]
3970
3971      Or, if not that,
3972
3973      --the rank of S1 is better than the rank of S2 (by the rules
3974        defined below):
3975
3976     Standard conversion sequences are ordered by their ranks: an Exact
3977     Match is a better conversion than a Promotion, which is a better
3978     conversion than a Conversion.
3979
3980     Two conversion sequences with the same rank are indistinguishable
3981     unless one of the following rules applies:
3982
3983     --A conversion that is not a conversion of a pointer, or pointer
3984       to member, to bool is better than another conversion that is such
3985       a conversion.  
3986
3987     The ICS_STD_RANK automatically handles the pointer-to-bool rule,
3988     so that we do not have to check it explicitly.  */
3989   if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3990     return 1;
3991   else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
3992     return -1;
3993
3994   to_type1 = TREE_TYPE (ics1);
3995   to_type2 = TREE_TYPE (ics2);
3996
3997   if (TYPE_PTR_P (from_type1)
3998       && TYPE_PTR_P (from_type2)
3999       && TYPE_PTR_P (to_type1)
4000       && TYPE_PTR_P (to_type2))
4001     {
4002       deref_from_type1 = TREE_TYPE (from_type1);
4003       deref_from_type2 = TREE_TYPE (from_type2);
4004       deref_to_type1 = TREE_TYPE (to_type1);
4005       deref_to_type2 = TREE_TYPE (to_type2);
4006     }
4007   /* The rules for pointers to members A::* are just like the rules
4008      for pointers A*, except opposite: if B is derived from A then
4009      A::* converts to B::*, not vice versa.  For that reason, we
4010      switch the from_ and to_ variables here.  */
4011   else if (TYPE_PTRMEM_P (from_type1)
4012            && TYPE_PTRMEM_P (from_type2)
4013            && TYPE_PTRMEM_P (to_type1)
4014            && TYPE_PTRMEM_P (to_type2))
4015     {
4016       deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4017       deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4018       deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4019       deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4020     }
4021   else if (TYPE_PTRMEMFUNC_P (from_type1)
4022            && TYPE_PTRMEMFUNC_P (from_type2)
4023            && TYPE_PTRMEMFUNC_P (to_type1)
4024            && TYPE_PTRMEMFUNC_P (to_type2))
4025     {
4026       deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4027       deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4028       deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4029       deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4030     }
4031
4032   if (deref_from_type1 != NULL_TREE
4033       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4034       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4035     {
4036       /* This was one of the pointer or pointer-like conversions.  
4037
4038          [over.ics.rank]
4039          
4040          --If class B is derived directly or indirectly from class A,
4041            conversion of B* to A* is better than conversion of B* to
4042            void*, and conversion of A* to void* is better than
4043            conversion of B* to void*.  */
4044       if (TREE_CODE (deref_to_type1) == VOID_TYPE
4045           && TREE_CODE (deref_to_type2) == VOID_TYPE)
4046         {
4047           if (is_properly_derived_from (deref_from_type1,
4048                                         deref_from_type2))
4049             return -1;
4050           else if (is_properly_derived_from (deref_from_type2,
4051                                              deref_from_type1))
4052             return 1;
4053         }
4054       else if (TREE_CODE (deref_to_type1) == VOID_TYPE
4055                || TREE_CODE (deref_to_type2) == VOID_TYPE)
4056         {
4057           if (same_type_p (deref_from_type1, deref_from_type2))
4058             {
4059               if (TREE_CODE (deref_to_type2) == VOID_TYPE)
4060                 {
4061                   if (is_properly_derived_from (deref_from_type1,
4062                                                 deref_to_type1))
4063                     return 1;
4064                 }
4065               /* We know that DEREF_TO_TYPE1 is `void' here.  */
4066               else if (is_properly_derived_from (deref_from_type1,
4067                                                  deref_to_type2))
4068                 return -1;
4069             }
4070         }
4071       else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
4072                && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
4073         {
4074           /* [over.ics.rank]
4075
4076              --If class B is derived directly or indirectly from class A
4077                and class C is derived directly or indirectly from B,
4078              
4079              --conversion of C* to B* is better than conversion of C* to
4080                A*, 
4081              
4082              --conversion of B* to A* is better than conversion of C* to
4083                A*  */
4084           if (same_type_p (deref_from_type1, deref_from_type2))
4085             {
4086               if (is_properly_derived_from (deref_to_type1,
4087                                             deref_to_type2))
4088                 return 1;
4089               else if (is_properly_derived_from (deref_to_type2,
4090                                                  deref_to_type1))
4091                 return -1;
4092             }
4093           else if (same_type_p (deref_to_type1, deref_to_type2))
4094             {
4095               if (is_properly_derived_from (deref_from_type2,
4096                                             deref_from_type1))
4097                 return 1;
4098               else if (is_properly_derived_from (deref_from_type1,
4099                                                  deref_from_type2))
4100                 return -1;
4101             }
4102         }
4103     }
4104   else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4105            && same_type_p (from_type1, from_type2))
4106     {
4107       /* [over.ics.rank]
4108          
4109          --binding of an expression of type C to a reference of type
4110            B& is better than binding an expression of type C to a
4111            reference of type A&
4112
4113          --conversion of C to B is better than conversion of C to A,  */
4114       if (is_properly_derived_from (from_type1, to_type1)
4115           && is_properly_derived_from (from_type1, to_type2))
4116         {
4117           if (is_properly_derived_from (to_type1, to_type2))
4118             return 1;
4119           else if (is_properly_derived_from (to_type2, to_type1))
4120             return -1;
4121         }
4122     }
4123   else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4124            && same_type_p (to_type1, to_type2))
4125     {
4126       /* [over.ics.rank]
4127
4128          --binding of an expression of type B to a reference of type
4129            A& is better than binding an expression of type C to a
4130            reference of type A&, 
4131
4132          --onversion of B to A is better than conversion of C to A  */
4133       if (is_properly_derived_from (from_type1, to_type1)
4134           && is_properly_derived_from (from_type2, to_type1))
4135         {
4136           if (is_properly_derived_from (from_type2, from_type1))
4137             return 1;
4138           else if (is_properly_derived_from (from_type1, from_type2))
4139             return -1;
4140         }
4141     }
4142
4143   /* [over.ics.rank]
4144
4145      --S1 and S2 differ only in their qualification conversion and  yield
4146        similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
4147        qualification signature of type T1 is a proper subset of  the  cv-
4148        qualification signature of type T2  */
4149   if (TREE_CODE (ics1) == QUAL_CONV 
4150       && TREE_CODE (ics2) == QUAL_CONV
4151       && same_type_p (from_type1, from_type2))
4152     return comp_cv_qual_signature (to_type1, to_type2);
4153
4154   /* [over.ics.rank]
4155      
4156      --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4157      types to which the references refer are the same type except for
4158      top-level cv-qualifiers, and the type to which the reference
4159      initialized by S2 refers is more cv-qualified than the type to
4160      which the reference initialized by S1 refers */
4161       
4162   if (ref_binding1 && ref_binding2
4163       && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4164                       TYPE_MAIN_VARIANT (to_type2)))
4165     return comp_cv_qualification (target_type2, target_type1);
4166
4167   /* Neither conversion sequence is better than the other.  */
4168   return 0;
4169 }
4170
4171 /* The source type for this standard conversion sequence.  */
4172
4173 static tree
4174 source_type (t)
4175      tree t;
4176 {
4177   for (;; t = TREE_OPERAND (t, 0))
4178     {
4179       if (TREE_CODE (t) == USER_CONV
4180           || TREE_CODE (t) == AMBIG_CONV
4181           || TREE_CODE (t) == IDENTITY_CONV)
4182         return TREE_TYPE (t);
4183     }
4184   my_friendly_abort (1823);
4185 }
4186
4187 /* Note a warning about preferring WINNER to LOSER.  We do this by storing
4188    a pointer to LOSER and re-running joust to produce the warning if WINNER
4189    is actually used.  */
4190
4191 static void
4192 add_warning (winner, loser)
4193      struct z_candidate *winner, *loser;
4194 {
4195   winner->warnings = expr_tree_cons (NULL_PTR,
4196                                      build_expr_ptr_wrapper (loser),
4197                                      winner->warnings);
4198 }
4199
4200 /* Compare two candidates for overloading as described in
4201    [over.match.best].  Return values:
4202
4203       1: cand1 is better than cand2
4204      -1: cand2 is better than cand1
4205       0: cand1 and cand2 are indistinguishable */
4206
4207 static int
4208 joust (cand1, cand2, warn)
4209      struct z_candidate *cand1, *cand2;
4210      int warn;
4211 {
4212   int winner = 0;
4213   int i, off1 = 0, off2 = 0, len;
4214
4215   /* Candidates that involve bad conversions are always worse than those
4216      that don't.  */
4217   if (cand1->viable > cand2->viable)
4218     return 1;
4219   if (cand1->viable < cand2->viable)
4220     return -1;
4221
4222   /* a viable function F1
4223      is defined to be a better function than another viable function F2  if
4224      for  all arguments i, ICSi(F1) is not a worse conversion sequence than
4225      ICSi(F2), and then */
4226
4227   /* for some argument j, ICSj(F1) is a better conversion  sequence  than
4228      ICSj(F2) */
4229
4230   /* For comparing static and non-static member functions, we ignore the
4231      implicit object parameter of the non-static function.  The WP says to
4232      pretend that the static function has an object parm, but that won't
4233      work with operator overloading.  */
4234   len = TREE_VEC_LENGTH (cand1->convs);
4235   if (len != TREE_VEC_LENGTH (cand2->convs))
4236     {
4237       if (DECL_STATIC_FUNCTION_P (cand1->fn)
4238           && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4239         off2 = 1;
4240       else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4241                && DECL_STATIC_FUNCTION_P (cand2->fn))
4242         {
4243           off1 = 1;
4244           --len;
4245         }
4246       else
4247         my_friendly_abort (42);
4248     }
4249
4250   for (i = 0; i < len; ++i)
4251     {
4252       tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4253       tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4254       int comp = compare_ics (t1, t2);
4255
4256       if (comp != 0)
4257         {
4258           if (warn_sign_promo
4259               && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4260               && TREE_CODE (t1) == STD_CONV
4261               && TREE_CODE (t2) == STD_CONV
4262               && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4263               && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4264               && (TYPE_PRECISION (TREE_TYPE (t1))
4265                   == TYPE_PRECISION (TREE_TYPE (t2)))
4266               && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4267                   || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4268                       == ENUMERAL_TYPE)))
4269             {
4270               tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4271               tree type1, type2;
4272               struct z_candidate *w, *l;
4273               if (comp > 0)
4274                 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4275                   w = cand1, l = cand2;
4276               else
4277                 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4278                   w = cand2, l = cand1;
4279
4280               if (warn)
4281                 {
4282                   cp_warning ("passing `%T' chooses `%T' over `%T'",
4283                               type, type1, type2);
4284                   cp_warning ("  in call to `%D'", w->fn);
4285                 }
4286               else
4287                 add_warning (w, l);
4288             }
4289
4290           if (winner && comp != winner)
4291             {
4292               winner = 0;
4293               goto tweak;
4294             }
4295           winner = comp;
4296         }
4297     }
4298
4299   /* warn about confusing overload resolution for user-defined conversions,
4300      either between a constructor and a conversion op, or between two
4301      conversion ops.  */
4302   if (winner && cand1->second_conv
4303       && ((DECL_CONSTRUCTOR_P (cand1->fn)
4304            != DECL_CONSTRUCTOR_P (cand2->fn))
4305           /* Don't warn if the two conv ops convert to the same type...  */
4306           || (! DECL_CONSTRUCTOR_P (cand1->fn)
4307               && ! same_type_p (TREE_TYPE (cand1->second_conv),
4308                                 TREE_TYPE (cand2->second_conv)))))
4309     {
4310       int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4311       if (comp != winner)
4312         {
4313           struct z_candidate *w, *l;
4314           if (winner == 1)
4315             w = cand1, l = cand2;
4316           else
4317             w = cand2, l = cand1;
4318           if (warn)
4319             {
4320               tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4321               if (! DECL_CONSTRUCTOR_P (w->fn))
4322                 source = TREE_TYPE (source);
4323               cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4324               cp_warning ("  for conversion from `%T' to `%T'",
4325                           source, TREE_TYPE (w->second_conv));
4326               cp_warning ("  because conversion sequence for the argument is better");
4327             }
4328           else
4329             add_warning (w, l);
4330         }
4331     }
4332
4333   if (winner)
4334     return winner;
4335
4336   /* or, if not that,
4337      F1 is a non-template function and F2 is a template function */
4338
4339   if (! cand1->template && cand2->template)
4340     return 1;
4341   else if (cand1->template && ! cand2->template)
4342     return -1;
4343   else if (cand1->template && cand2->template)
4344     winner = more_specialized
4345       (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4346        NULL_TREE);
4347
4348   /* or, if not that,
4349      the  context  is  an  initialization by user-defined conversion (see
4350      _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
4351      sequence  from  the return type of F1 to the destination type (i.e.,
4352      the type of the entity being initialized)  is  a  better  conversion
4353      sequence  than the standard conversion sequence from the return type
4354      of F2 to the destination type.  */
4355
4356   if (! winner && cand1->second_conv)
4357     winner = compare_ics (cand1->second_conv, cand2->second_conv);
4358
4359   /* If the built-in candidates are the same, arbitrarily pick one.  */
4360   if (! winner && cand1->fn == cand2->fn
4361       && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4362     {
4363       for (i = 0; i < len; ++i)
4364         if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4365                           TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4366           break;
4367       if (i == TREE_VEC_LENGTH (cand1->convs))
4368         return 1;
4369
4370       /* Kludge around broken overloading rules whereby
4371          Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4372          that takes references and another that takes values.  */
4373       if (cand1->fn == ansi_opname[COND_EXPR])
4374         {
4375           tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4376           tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4377           tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4378           tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4379
4380           if (same_type_p (t1, t2))
4381             {
4382               if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4383                 return 1;
4384               if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4385                 return -1;
4386             }
4387         }
4388     }
4389
4390 tweak:
4391
4392   /* Extension: If the worst conversion for one candidate is worse than the
4393      worst conversion for the other, take the first.  */
4394   if (! winner && ! pedantic)
4395     {
4396       int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4397
4398       for (i = 0; i < len; ++i)
4399         {
4400           if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4401             rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4402           if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4403             rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4404         }
4405
4406       if (rank1 < rank2)
4407         return 1;
4408       if (rank1 > rank2)
4409         return -1;
4410     }
4411
4412   return winner;
4413 }
4414
4415 /* Given a list of candidates for overloading, find the best one, if any.
4416    This algorithm has a worst case of O(2n) (winner is last), and a best
4417    case of O(n/2) (totally ambiguous); much better than a sorting
4418    algorithm.  */
4419
4420 static struct z_candidate *
4421 tourney (candidates)
4422      struct z_candidate *candidates;
4423 {
4424   struct z_candidate *champ = candidates, *challenger;
4425   int fate;
4426   int champ_compared_to_predecessor = 0;
4427
4428   /* Walk through the list once, comparing each current champ to the next
4429      candidate, knocking out a candidate or two with each comparison.  */
4430
4431   for (challenger = champ->next; challenger; )
4432     {
4433       fate = joust (champ, challenger, 0);
4434       if (fate == 1)
4435         challenger = challenger->next;
4436       else
4437         {
4438           if (fate == 0)
4439             {
4440               champ = challenger->next;
4441               if (champ == 0)
4442                 return 0;
4443               champ_compared_to_predecessor = 0;
4444             }
4445           else
4446             {
4447               champ = challenger;
4448               champ_compared_to_predecessor = 1;
4449             }
4450
4451           challenger = champ->next;
4452         }
4453     }
4454
4455   /* Make sure the champ is better than all the candidates it hasn't yet
4456      been compared to.  */
4457
4458   for (challenger = candidates; 
4459        challenger != champ 
4460          && !(champ_compared_to_predecessor && challenger->next == champ);
4461        challenger = challenger->next)
4462     {
4463       fate = joust (champ, challenger, 0);
4464       if (fate != 1)
4465         return 0;
4466     }
4467
4468   return champ;
4469 }
4470
4471 int
4472 can_convert (to, from)
4473      tree to, from;
4474 {
4475   tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4476   return (t && ! ICS_BAD_FLAG (t));
4477 }
4478
4479 int
4480 can_convert_arg (to, from, arg)
4481      tree to, from, arg;
4482 {
4483   tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4484   return (t && ! ICS_BAD_FLAG (t));
4485 }