OSDN Git Service

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