OSDN Git Service

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