OSDN Git Service

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