OSDN Git Service

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