OSDN Git Service

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