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