OSDN Git Service

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