OSDN Git Service

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