OSDN Git Service

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