OSDN Git Service

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