OSDN Git Service

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