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