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