OSDN Git Service

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