OSDN Git Service

* call.c (find_scoped_type, resolve_scope_to_name): Lose.
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
1 /* Functions related to invoking methods and overloaded functions.
2    Copyright (C) 1987, 92-97, 1998, 1999 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com) and
4    modified by Brendan Kehoe (brendan@cygnus.com).
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* High-level class interface.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "output.h"
31 #include "flags.h"
32 #include "rtl.h"
33 #include "toplev.h"
34
35 #include "obstack.h"
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
38
39 extern int inhibit_warnings;
40 extern tree ctor_label, dtor_label;
41
42 static tree build_new_method_call PROTO((tree, tree, tree, tree, int));
43
44 static tree build_field_call PROTO((tree, tree, tree, tree));
45 static struct z_candidate * tourney PROTO((struct z_candidate *));
46 static int joust PROTO((struct z_candidate *, struct z_candidate *, int));
47 static int compare_ics PROTO((tree, tree));
48 static tree build_over_call PROTO((struct z_candidate *, tree, int));
49 static tree convert_like PROTO((tree, tree));
50 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
51                             tree, const char *));
52 static tree build_object_call PROTO((tree, tree));
53 static tree resolve_args PROTO((tree));
54 static struct z_candidate * build_user_type_conversion_1
55         PROTO ((tree, tree, int));
56 static void print_z_candidates PROTO((struct z_candidate *));
57 static tree build_this PROTO((tree));
58 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
59 static int any_viable PROTO((struct z_candidate *));
60 static struct z_candidate * add_template_candidate
61         PROTO((struct z_candidate *, tree, tree, tree, tree, int,
62                unification_kind_t));
63 static struct z_candidate * add_template_candidate_real
64         PROTO((struct z_candidate *, tree, tree, tree, tree, int,
65                tree, unification_kind_t));
66 static struct z_candidate * add_template_conv_candidate 
67         PROTO((struct z_candidate *, tree, tree, tree, tree));
68 static struct z_candidate * add_builtin_candidates
69         PROTO((struct z_candidate *, enum tree_code, enum tree_code,
70                tree, tree *, int));
71 static struct z_candidate * add_builtin_candidate
72         PROTO((struct z_candidate *, enum tree_code, enum tree_code,
73                tree, tree, tree, tree *, tree *, int));
74 static int is_complete PROTO((tree));
75 static struct z_candidate * build_builtin_candidate 
76         PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
77                int));
78 static struct z_candidate * add_conv_candidate 
79         PROTO((struct z_candidate *, tree, tree, tree));
80 static struct z_candidate * add_function_candidate 
81         PROTO((struct z_candidate *, tree, tree, int));
82 static tree implicit_conversion PROTO((tree, tree, tree, int));
83 static tree standard_conversion PROTO((tree, tree, tree));
84 static tree reference_binding PROTO((tree, tree, tree, int));
85 static tree strip_top_quals PROTO((tree));
86 static tree non_reference PROTO((tree));
87 static tree build_conv PROTO((enum tree_code, tree, tree));
88 static int is_subseq PROTO((tree, tree));
89 static int maybe_handle_ref_bind PROTO((tree*, tree*));
90 static void maybe_handle_implicit_object PROTO((tree*));
91 static struct z_candidate * add_candidate PROTO((struct z_candidate *,
92                                                  tree, tree, int));
93 static tree source_type PROTO((tree));
94 static void add_warning PROTO((struct z_candidate *, struct z_candidate *));
95
96 tree
97 build_vfield_ref (datum, type)
98      tree datum, type;
99 {
100   tree rval;
101
102   if (datum == error_mark_node)
103     return error_mark_node;
104
105   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
106     datum = convert_from_reference (datum);
107
108   if (! TYPE_USES_COMPLEX_INHERITANCE (type))
109     rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
110                   datum, CLASSTYPE_VFIELD (type));
111   else
112     rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
113
114   return rval;
115 }
116
117 /* Build a call to a member of an object.  I.e., one that overloads
118    operator ()(), or is a pointer-to-function or pointer-to-method.  */
119
120 static tree
121 build_field_call (basetype_path, instance_ptr, name, parms)
122      tree basetype_path, instance_ptr, name, parms;
123 {
124   tree field, instance;
125
126   if (name == ctor_identifier || name == dtor_identifier)
127     return NULL_TREE;
128
129   /* Speed up the common case.  */
130   if (instance_ptr == current_class_ptr
131       && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
132     return NULL_TREE;
133
134   field = lookup_field (basetype_path, name, 1, 0);
135
136   if (field == error_mark_node || field == NULL_TREE)
137     return field;
138
139   if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
140     {
141       /* If it's a field, try overloading operator (),
142          or calling if the field is a pointer-to-function.  */
143       instance = build_indirect_ref (instance_ptr, NULL_PTR);
144       instance = build_component_ref_1 (instance, field, 0);
145
146       if (instance == error_mark_node)
147         return error_mark_node;
148
149       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
150         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
151                                instance, parms, NULL_TREE);
152       else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
153         {
154           if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
155             return build_function_call (instance, parms);
156           else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
157                    == METHOD_TYPE)
158             return build_function_call
159               (instance, expr_tree_cons (NULL_TREE, instance_ptr, parms));
160         }
161     }
162
163   return NULL_TREE;
164 }
165
166 /* Returns nonzero iff the destructor name specified in NAME
167    (a BIT_NOT_EXPR) matches BASETYPE.  The operand of NAME can take many
168    forms...  */
169
170 int
171 check_dtor_name (basetype, name)
172      tree basetype, name;
173 {
174   name = TREE_OPERAND (name, 0);
175
176   /* Just accept something we've already complained about.  */
177   if (name == error_mark_node)
178     return 1;
179
180   if (TREE_CODE (name) == TYPE_DECL)
181     name = TREE_TYPE (name);
182   else if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
183     /* OK */;
184   else if (TREE_CODE (name) == IDENTIFIER_NODE)
185     {
186       if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
187           || (TREE_CODE (basetype) == ENUMERAL_TYPE
188               && name == TYPE_IDENTIFIER (basetype)))
189         name = basetype;
190       else
191         name = get_type_value (name);
192     }
193   else
194     my_friendly_abort (980605);
195
196   if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
197     return 1;
198   return 0;
199 }
200
201 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
202    This is how virtual function calls are avoided.  */
203
204 tree
205 build_scoped_method_call (exp, basetype, name, parms)
206      tree exp, basetype, name, parms;
207 {
208   /* Because this syntactic form does not allow
209      a pointer to a base class to be `stolen',
210      we need not protect the derived->base conversion
211      that happens here.
212      
213      @@ But we do have to check access privileges later.  */
214   tree binfo, decl;
215   tree type = TREE_TYPE (exp);
216
217   if (type == error_mark_node
218       || basetype == error_mark_node)
219     return error_mark_node;
220
221   if (processing_template_decl)
222     {
223       if (TREE_CODE (name) == BIT_NOT_EXPR
224           && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
225         {
226           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
227           if (type)
228             name = build_min_nt (BIT_NOT_EXPR, type);
229         }
230       name = build_min_nt (SCOPE_REF, basetype, name);
231       return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
232     }
233
234   if (TREE_CODE (type) == REFERENCE_TYPE)
235     type = TREE_TYPE (type);
236
237   if (TREE_CODE (basetype) == TREE_VEC)
238     {
239       binfo = basetype;
240       basetype = BINFO_TYPE (binfo);
241     }
242   else
243     binfo = NULL_TREE;
244
245   /* Check the destructor call syntax.  */
246   if (TREE_CODE (name) == BIT_NOT_EXPR)
247     {
248       /* We can get here if someone writes their destructor call like
249          `obj.NS::~T()'; this isn't really a scoped method call, so hand
250          it off.  */
251       if (TREE_CODE (basetype) == NAMESPACE_DECL)
252         return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
253
254       if (! check_dtor_name (basetype, name))
255         cp_error ("qualified type `%T' does not match destructor name `~%T'",
256                   basetype, TREE_OPERAND (name, 0));
257
258       /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
259          that explicit ~int is caught in the parser; this deals with typedefs
260          and template parms.  */
261       if (! IS_AGGR_TYPE (basetype))
262         {
263           if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
264             cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
265                       exp, basetype, type);
266
267           return cp_convert (void_type_node, exp);
268         }
269     }
270
271   if (! is_aggr_type (basetype, 1))
272     return error_mark_node;
273
274   if (! IS_AGGR_TYPE (type))
275     {
276       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
277                 exp, type);
278       return error_mark_node;
279     }
280
281   if (! binfo)
282     {
283       binfo = get_binfo (basetype, type, 1);
284       if (binfo == error_mark_node)
285         return error_mark_node;
286       if (! binfo)
287         error_not_base_type (basetype, type);
288     }
289
290   if (binfo)
291     {
292       if (TREE_CODE (exp) == INDIRECT_REF)
293         decl = build_indirect_ref
294           (convert_pointer_to_real
295            (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
296       else
297         decl = build_scoped_ref (exp, basetype);
298
299       /* Call to a destructor.  */
300       if (TREE_CODE (name) == BIT_NOT_EXPR)
301         {
302           if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
303             return cp_convert (void_type_node, exp);
304           
305           return build_delete (TREE_TYPE (decl), decl, integer_two_node,
306                                LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
307                                0);
308         }
309
310       /* Call to a method.  */
311       return build_method_call (decl, name, parms, binfo,
312                                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
313     }
314   return error_mark_node;
315 }
316
317 /* We want the address of a function or method.  We avoid creating a
318    pointer-to-member function.  */
319
320 tree
321 build_addr_func (function)
322      tree function;
323 {
324   tree type = TREE_TYPE (function);
325
326   /* We have to do these by hand to avoid real pointer to member
327      functions.  */
328   if (TREE_CODE (type) == METHOD_TYPE)
329     {
330       tree addr;
331
332       type = build_pointer_type (type);
333
334       if (mark_addressable (function) == 0)
335         return error_mark_node;
336
337       addr = build1 (ADDR_EXPR, type, function);
338
339       /* Address of a static or external variable or function counts
340          as a constant */
341       if (staticp (function))
342         TREE_CONSTANT (addr) = 1;
343
344       function = addr;
345     }
346   else
347     function = default_conversion (function);
348
349   return function;
350 }
351
352 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
353    POINTER_TYPE to those.  Note, pointer to member function types
354    (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
355
356 tree
357 build_call (function, result_type, parms)
358      tree function, result_type, parms;
359 {
360   int is_constructor = 0;
361   tree tmp;
362   tree decl;
363
364   function = build_addr_func (function);
365
366   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
367     {
368       sorry ("unable to call pointer to member function here");
369       return error_mark_node;
370     }
371
372   if (TREE_CODE (function) == ADDR_EXPR
373       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
374     decl = TREE_OPERAND (function, 0);
375   else
376     decl = NULL_TREE;
377
378   if (decl && DECL_CONSTRUCTOR_P (decl))
379     is_constructor = 1;
380
381   if (decl)
382     my_friendly_assert (TREE_USED (decl), 990125);
383
384   /* Don't pass empty class objects by value.  This is useful
385      for tags in STL, which are used to control overload resolution.
386      We don't need to handle other cases of copying empty classes.  */
387   if (! decl || ! DECL_BUILT_IN (decl))
388     for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
389       if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
390           && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
391         {
392           tree t = make_node (RTL_EXPR);
393           TREE_TYPE (t) = TREE_TYPE (TREE_VALUE (tmp));
394           RTL_EXPR_RTL (t) = const0_rtx;
395           RTL_EXPR_SEQUENCE (t) = NULL_RTX;
396           TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
397                                     TREE_VALUE (tmp), t);
398         }
399
400   function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
401   TREE_HAS_CONSTRUCTOR (function) = is_constructor;
402   TREE_TYPE (function) = result_type;
403   TREE_SIDE_EFFECTS (function) = 1;
404   
405   return function;
406 }
407
408 /* Build something of the form ptr->method (args)
409    or object.method (args).  This can also build
410    calls to constructors, and find friends.
411
412    Member functions always take their class variable
413    as a pointer.
414
415    INSTANCE is a class instance.
416
417    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
418
419    PARMS help to figure out what that NAME really refers to.
420
421    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
422    down to the real instance type to use for access checking.  We need this
423    information to get protected accesses correct.  This parameter is used
424    by build_member_call.
425
426    FLAGS is the logical disjunction of zero or more LOOKUP_
427    flags.  See cp-tree.h for more info.
428
429    If this is all OK, calls build_function_call with the resolved
430    member function.
431
432    This function must also handle being called to perform
433    initialization, promotion/coercion of arguments, and
434    instantiation of default parameters.
435
436    Note that NAME may refer to an instance variable name.  If
437    `operator()()' is defined for the type of that field, then we return
438    that result.  */
439
440 tree
441 build_method_call (instance, name, parms, basetype_path, flags)
442      tree instance, name, parms, basetype_path;
443      int flags;
444 {
445   tree basetype, instance_ptr;
446
447 #ifdef GATHER_STATISTICS
448   n_build_method_call++;
449 #endif
450
451   if (instance == error_mark_node
452       || name == error_mark_node
453       || parms == error_mark_node
454       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
455     return error_mark_node;
456
457   if (processing_template_decl)
458     {
459       /* We need to process template parm names here so that tsubst catches
460          them properly.  Other type names can wait.  */
461       if (TREE_CODE (name) == BIT_NOT_EXPR)
462         {
463           tree type = NULL_TREE;
464
465           if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
466             type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
467           else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
468             type = TREE_TYPE (TREE_OPERAND (name, 0));
469
470           if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
471             name = build_min_nt (BIT_NOT_EXPR, type);
472         }
473
474       return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
475     }
476
477   /* This is the logic that magically deletes the second argument to
478      operator delete, if it is not needed.  */
479   if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
480     {
481       tree save_last = TREE_CHAIN (parms);
482
483       /* get rid of unneeded argument */
484       TREE_CHAIN (parms) = NULL_TREE;
485       if (build_method_call (instance, name, parms, basetype_path,
486                              (LOOKUP_SPECULATIVELY|flags) & ~LOOKUP_COMPLAIN))
487         {
488           /* If it finds a match, return it.  */
489           return build_method_call (instance, name, parms, basetype_path, flags);
490         }
491       /* If it doesn't work, two argument delete must work */
492       TREE_CHAIN (parms) = save_last;
493     }
494   /* We already know whether it's needed or not for vec delete.  */
495   else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
496            && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
497            && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
498     TREE_CHAIN (parms) = NULL_TREE;
499
500   if (TREE_CODE (name) == BIT_NOT_EXPR)
501     {
502       if (parms)
503         error ("destructors take no parameters");
504       basetype = TREE_TYPE (instance);
505       if (TREE_CODE (basetype) == REFERENCE_TYPE)
506         basetype = TREE_TYPE (basetype);
507
508       if (! check_dtor_name (basetype, name))
509         cp_error
510           ("destructor name `~%T' does not match type `%T' of expression",
511            TREE_OPERAND (name, 0), basetype);
512
513       if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
514         return cp_convert (void_type_node, instance);
515       instance = default_conversion (instance);
516       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
517       return build_delete (build_pointer_type (basetype),
518                            instance_ptr, integer_two_node,
519                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
520     }
521
522   return build_new_method_call (instance, name, parms, basetype_path, flags);
523 }
524
525 /* New overloading code.  */
526
527 struct z_candidate {
528   tree fn;
529   tree convs;
530   tree second_conv;
531   int viable;
532   tree basetype_path;
533   tree template;
534   tree warnings;
535   struct z_candidate *next;
536 };
537
538 #define IDENTITY_RANK 0
539 #define EXACT_RANK 1
540 #define PROMO_RANK 2
541 #define STD_RANK 3
542 #define PBOOL_RANK 4
543 #define USER_RANK 5
544 #define ELLIPSIS_RANK 6
545 #define BAD_RANK 7
546
547 #define ICS_RANK(NODE)                          \
548   (ICS_BAD_FLAG (NODE) ? BAD_RANK   \
549    : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK   \
550    : ICS_USER_FLAG (NODE) ? USER_RANK           \
551    : ICS_STD_RANK (NODE))
552
553 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
554
555 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
556 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
557 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
558 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
559
560 #define USER_CONV_CAND(NODE) \
561   ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
562 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
563
564 int
565 null_ptr_cst_p (t)
566      tree t;
567 {
568   if (t == null_node
569       || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
570     return 1;
571   return 0;
572 }
573
574 static tree
575 build_conv (code, type, from)
576      enum tree_code code;
577      tree type, from;
578 {
579   tree t = build1 (code, type, from);
580   int rank = ICS_STD_RANK (from);
581   switch (code)
582     {
583     case PTR_CONV:
584     case PMEM_CONV:
585     case BASE_CONV:
586     case STD_CONV:
587       if (rank < STD_RANK)
588         rank = STD_RANK;
589       break;
590
591     case QUAL_CONV:
592       if (rank < EXACT_RANK)
593         rank = EXACT_RANK;
594
595     default:
596       break;
597     }
598   ICS_STD_RANK (t) = rank;
599   ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
600   ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
601   return t;
602 }
603
604 static tree
605 non_reference (t)
606      tree t;
607 {
608   if (TREE_CODE (t) == REFERENCE_TYPE)
609     t = TREE_TYPE (t);
610   return t;
611 }
612
613 static tree
614 strip_top_quals (t)
615      tree t;
616 {
617   if (TREE_CODE (t) == ARRAY_TYPE)
618     return t;
619   return TYPE_MAIN_VARIANT (t);
620 }
621
622 /* Returns the standard conversion path (see [conv]) from type FROM to type
623    TO, if any.  For proper handling of null pointer constants, you must
624    also pass the expression EXPR to convert from.  */
625
626 static tree
627 standard_conversion (to, from, expr)
628      tree to, from, expr;
629 {
630   enum tree_code fcode, tcode;
631   tree conv;
632   int fromref = 0;
633
634   if (TREE_CODE (to) == REFERENCE_TYPE)
635     to = TREE_TYPE (to);
636   if (TREE_CODE (from) == REFERENCE_TYPE)
637     {
638       fromref = 1;
639       from = TREE_TYPE (from);
640     }
641   to = strip_top_quals (to);
642   from = strip_top_quals (from);
643
644   if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
645       && expr && type_unknown_p (expr))
646     {
647       expr = instantiate_type (to, expr, 0);
648       if (expr == error_mark_node)
649         return NULL_TREE;
650       from = TREE_TYPE (expr);
651     }
652
653   fcode = TREE_CODE (from);
654   tcode = TREE_CODE (to);
655
656   conv = build1 (IDENTITY_CONV, from, expr);
657
658   if (fcode == FUNCTION_TYPE)
659     {
660       from = build_pointer_type (from);
661       fcode = TREE_CODE (from);
662       conv = build_conv (LVALUE_CONV, from, conv);
663     }
664   else if (fcode == ARRAY_TYPE)
665     {
666       from = build_pointer_type (TREE_TYPE (from));
667       fcode = TREE_CODE (from);
668       conv = build_conv (LVALUE_CONV, from, conv);
669     }
670   else if (fromref || (expr && real_lvalue_p (expr)))
671     conv = build_conv (RVALUE_CONV, from, conv);
672
673   if (from == to)
674     return conv;
675
676   if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
677       && expr && null_ptr_cst_p (expr))
678     {
679       conv = build_conv (STD_CONV, to, conv);
680     }
681   else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
682     {
683       enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
684       enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
685
686       if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
687                        TYPE_MAIN_VARIANT (TREE_TYPE (to))))
688         ;
689       else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
690                && ufcode != FUNCTION_TYPE)
691         {
692           from = build_pointer_type
693             (cp_build_qualified_type (void_type_node, 
694                                       CP_TYPE_QUALS (TREE_TYPE (from))));
695           conv = build_conv (PTR_CONV, from, conv);
696         }
697       else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
698         {
699           tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
700           tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
701
702           if (DERIVED_FROM_P (fbase, tbase)
703               && (same_type_p 
704                   (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
705                    TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))))))
706             {
707               from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
708               from = build_pointer_type (from);
709               conv = build_conv (PMEM_CONV, from, conv);
710             }
711         }
712       else if (IS_AGGR_TYPE (TREE_TYPE (from))
713                && IS_AGGR_TYPE (TREE_TYPE (to)))
714         {
715           if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
716             {
717               from = 
718                 cp_build_qualified_type (TREE_TYPE (to),
719                                          CP_TYPE_QUALS (TREE_TYPE (from)));
720               from = build_pointer_type (from);
721               conv = build_conv (PTR_CONV, from, conv);
722             }
723         }
724
725       if (same_type_p (from, to))
726         /* OK */;
727       else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
728         conv = build_conv (QUAL_CONV, to, conv);
729       else if (expr && string_conv_p (to, expr, 0))
730         /* converting from string constant to char *.  */
731         conv = build_conv (QUAL_CONV, to, conv);
732       else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
733         {
734           conv = build_conv (PTR_CONV, to, conv);
735           ICS_BAD_FLAG (conv) = 1;
736         }
737       else
738         return 0;
739
740       from = to;
741     }
742   else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
743     {
744       tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
745       tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
746       tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
747       tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
748
749       if (! DERIVED_FROM_P (fbase, tbase)
750           || ! same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
751           || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
752                           TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
753           || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
754         return 0;
755
756       from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
757       from = build_cplus_method_type (from, TREE_TYPE (fromfn),
758                                       TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
759       from = build_ptrmemfunc_type (build_pointer_type (from));
760       conv = build_conv (PMEM_CONV, from, conv);
761     }
762   else if (tcode == BOOLEAN_TYPE)
763     {
764       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
765              || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
766         return 0;
767
768       conv = build_conv (STD_CONV, to, conv);
769       if (fcode == POINTER_TYPE
770           || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
771         ICS_STD_RANK (conv) = PBOOL_RANK;
772     }
773   /* We don't check for ENUMERAL_TYPE here because there are no standard
774      conversions to enum type.  */
775   else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
776            || tcode == REAL_TYPE)
777     {
778       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
779         return 0;
780       conv = build_conv (STD_CONV, to, conv);
781
782       /* Give this a better rank if it's a promotion.  */
783       if (to == type_promotes_to (from)
784           && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
785         ICS_STD_RANK (conv) = PROMO_RANK;
786     }
787   else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
788            && DERIVED_FROM_P (to, from))
789     {
790       if (TREE_CODE (conv) == RVALUE_CONV)
791         conv = TREE_OPERAND (conv, 0);
792       conv = build_conv (BASE_CONV, to, conv);
793     }
794   else
795     return 0;
796
797   return conv;
798 }
799
800 /* Returns the conversion path from type FROM to reference type TO for
801    purposes of reference binding.  For lvalue binding, either pass a
802    reference type to FROM or an lvalue expression to EXPR.
803
804    Currently does not distinguish in the generated trees between binding to
805    an lvalue and a temporary.  Should it?  */
806
807 static tree
808 reference_binding (rto, rfrom, expr, flags)
809      tree rto, rfrom, expr;
810      int flags;
811 {
812   tree conv;
813   int lvalue = 1;
814   tree to = TREE_TYPE (rto);
815   tree from = rfrom;
816   int related;
817
818   if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
819     {
820       expr = instantiate_type (to, expr, 0);
821       if (expr == error_mark_node)
822         return NULL_TREE;
823       from = TREE_TYPE (expr);
824     }
825
826   if (TREE_CODE (from) == REFERENCE_TYPE)
827     from = TREE_TYPE (from);
828   else if (! expr || ! real_lvalue_p (expr))
829     lvalue = 0;
830
831   related = (same_type_p (TYPE_MAIN_VARIANT (to),
832                           TYPE_MAIN_VARIANT (from))
833              || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
834                  && DERIVED_FROM_P (to, from)));
835
836   if (lvalue && related && at_least_as_qualified_p (to, from))
837     {
838       conv = build1 (IDENTITY_CONV, from, expr);
839
840       if (same_type_p (TYPE_MAIN_VARIANT (to),
841                        TYPE_MAIN_VARIANT (from)))
842         conv = build_conv (REF_BIND, rto, conv);
843       else
844         {
845           conv = build_conv (REF_BIND, rto, conv);
846           ICS_STD_RANK (conv) = STD_RANK;
847         }
848     }
849   else
850     conv = NULL_TREE;
851
852   if (! conv)
853     {
854       conv = standard_conversion (to, rfrom, expr);
855       if (conv)
856         {
857           conv = build_conv (REF_BIND, rto, conv);
858
859           /* Bind directly to a base subobject of a class rvalue.  Do it
860              after building the conversion for proper handling of ICS_RANK.  */
861           if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
862             TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
863         }
864       if (conv
865           && ((! (CP_TYPE_CONST_NON_VOLATILE_P (to)
866                   && (flags & LOOKUP_NO_TEMP_BIND) == 0))
867               /* If T1 is reference-related to T2, cv1 must be the same
868                  cv-qualification as, or greater cv-qualification than,
869                  cv2; otherwise, the program is ill-formed.  */
870               || (related && !at_least_as_qualified_p (to, from))))
871         ICS_BAD_FLAG (conv) = 1;
872     }
873
874   return conv;
875 }
876
877 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
878    to type TO.  The optional expression EXPR may affect the conversion.
879    FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
880    significant.  */
881
882 static tree
883 implicit_conversion (to, from, expr, flags)
884      tree to, from, expr;
885      int flags;
886 {
887   tree conv;
888   struct z_candidate *cand;
889
890   if (TREE_CODE (to) == REFERENCE_TYPE)
891     conv = reference_binding (to, from, expr, flags);
892   else
893     conv = standard_conversion (to, from, expr);
894
895   if (conv)
896     ;
897   else if (expr != NULL_TREE
898            && (IS_AGGR_TYPE (non_reference (from))
899                || IS_AGGR_TYPE (non_reference (to)))
900            && (flags & LOOKUP_NO_CONVERSION) == 0)
901     {
902       cand = build_user_type_conversion_1
903         (to, expr, LOOKUP_ONLYCONVERTING);
904       if (cand)
905         conv = cand->second_conv;
906       if ((! conv || ICS_BAD_FLAG (conv))
907           && TREE_CODE (to) == REFERENCE_TYPE
908           && (flags & LOOKUP_NO_TEMP_BIND) == 0)
909         {
910           cand = build_user_type_conversion_1
911             (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
912           if (cand)
913             {
914               if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to)))
915                 ICS_BAD_FLAG (cand->second_conv) = 1;
916               if (!conv || (ICS_BAD_FLAG (conv)
917                             > ICS_BAD_FLAG (cand->second_conv)))
918                 conv = build_conv (REF_BIND, to, cand->second_conv);
919             }
920         }
921     }
922
923   return conv;
924 }
925
926 /* Add a new entry to the list of candidates.  Used by the add_*_candidate
927    functions.  */
928
929 static struct z_candidate *
930 add_candidate (candidates, fn, convs, viable)
931      struct z_candidate *candidates;
932      tree fn, convs;
933      int viable;
934 {
935   struct z_candidate *cand
936     = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
937
938   cand->fn = fn;
939   cand->convs = convs;
940   cand->second_conv = NULL_TREE;
941   cand->viable = viable;
942   cand->basetype_path = NULL_TREE;
943   cand->template = NULL_TREE;
944   cand->warnings = NULL_TREE;
945   cand->next = candidates;
946
947   return cand;
948 }
949
950 /* Create an overload candidate for the function or method FN called with
951    the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
952    to implicit_conversion.  */
953
954 static struct z_candidate *
955 add_function_candidate (candidates, fn, arglist, flags)
956      struct z_candidate *candidates;
957      tree fn, arglist;
958      int flags;
959 {
960   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
961   int i, len;
962   tree convs;
963   tree parmnode, argnode;
964   int viable = 1;
965
966   /* The `this' and `in_chrg' arguments to constructors are not considered
967      in overload resolution.  */
968   if (DECL_CONSTRUCTOR_P (fn))
969     {
970       parmlist = TREE_CHAIN (parmlist);
971       arglist = TREE_CHAIN (arglist);
972       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
973         {
974           parmlist = TREE_CHAIN (parmlist);
975           arglist = TREE_CHAIN (arglist);
976         }
977     }
978
979   len = list_length (arglist);
980   convs = make_scratch_vec (len);
981
982   /* 13.3.2 - Viable functions [over.match.viable]
983      First, to be a viable function, a candidate function shall have enough
984      parameters to agree in number with the arguments in the list.
985
986      We need to check this first; otherwise, checking the ICSes might cause
987      us to produce an ill-formed template instantiation.  */
988
989   parmnode = parmlist;
990   for (i = 0; i < len; ++i)
991     {
992       if (parmnode == NULL_TREE || parmnode == void_list_node)
993         break;
994       parmnode = TREE_CHAIN (parmnode);
995     }
996
997   if (i < len && parmnode)
998     viable = 0;
999
1000   /* Make sure there are default args for the rest of the parms.  */
1001   else for (; parmnode && parmnode != void_list_node;
1002             parmnode = TREE_CHAIN (parmnode))
1003     if (! TREE_PURPOSE (parmnode))
1004       {
1005         viable = 0;
1006         break;
1007       }
1008
1009   if (! viable)
1010     goto out;
1011
1012   /* Second, for F to be a viable function, there shall exist for each
1013      argument an implicit conversion sequence that converts that argument
1014      to the corresponding parameter of F.  */
1015
1016   parmnode = parmlist;
1017   argnode = arglist;
1018
1019   for (i = 0; i < len; ++i)
1020     {
1021       tree arg = TREE_VALUE (argnode);
1022       tree argtype = lvalue_type (arg);
1023       tree t;
1024
1025       if (parmnode == void_list_node)
1026         break;
1027
1028       if (parmnode)
1029         {
1030           tree parmtype = TREE_VALUE (parmnode);
1031
1032           /* [over.match.funcs] For conversion functions, the function is
1033              considered to be a member of the class of the implicit object
1034              argument for the purpose of defining the type of the implicit
1035              object parameter.
1036
1037              Since build_over_call ignores the ICS for the `this' parameter,
1038              we can just change the parm type.  */
1039           if (DECL_CONV_FN_P (fn) && i == 0)
1040             {
1041               parmtype
1042                 = build_qualified_type (TREE_TYPE (argtype),
1043                                         TYPE_QUALS (TREE_TYPE (parmtype)));
1044               parmtype = build_pointer_type (parmtype);
1045             }
1046
1047           t = implicit_conversion (parmtype, argtype, arg, flags);
1048         }
1049       else
1050         {
1051           t = build1 (IDENTITY_CONV, argtype, arg);
1052           ICS_ELLIPSIS_FLAG (t) = 1;
1053         }
1054
1055       if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1056           && ! DECL_CONSTRUCTOR_P (fn))
1057         ICS_THIS_FLAG (t) = 1;
1058
1059       TREE_VEC_ELT (convs, i) = t;
1060       if (! t)
1061         {
1062           viable = 0;
1063           break;
1064         }
1065
1066       if (ICS_BAD_FLAG (t))
1067         viable = -1;
1068
1069       if (parmnode)
1070         parmnode = TREE_CHAIN (parmnode);
1071       argnode = TREE_CHAIN (argnode);
1072     }
1073
1074  out:
1075   return add_candidate (candidates, fn, convs, viable);
1076 }
1077
1078 /* Create an overload candidate for the conversion function FN which will
1079    be invoked for expression OBJ, producing a pointer-to-function which
1080    will in turn be called with the argument list ARGLIST, and add it to
1081    CANDIDATES.  FLAGS is passed on to implicit_conversion.
1082
1083    Actually, we don't really care about FN; we care about the type it
1084    converts to.  There may be multiple conversion functions that will
1085    convert to that type, and we rely on build_user_type_conversion_1 to
1086    choose the best one; so when we create our candidate, we record the type
1087    instead of the function.  */
1088
1089 static struct z_candidate *
1090 add_conv_candidate (candidates, fn, obj, arglist)
1091      struct z_candidate *candidates;
1092      tree fn, obj, arglist;
1093 {
1094   tree totype = TREE_TYPE (TREE_TYPE (fn));
1095   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1096   int i, len = list_length (arglist) + 1;
1097   tree convs = make_scratch_vec (len);
1098   tree parmnode = parmlist;
1099   tree argnode = arglist;
1100   int viable = 1;
1101   int flags = LOOKUP_NORMAL;
1102
1103   /* Don't bother looking up the same type twice.  */
1104   if (candidates && candidates->fn == totype)
1105     return candidates;
1106
1107   for (i = 0; i < len; ++i)
1108     {
1109       tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1110       tree argtype = lvalue_type (arg);
1111       tree t;
1112
1113       if (i == 0)
1114         t = implicit_conversion (totype, argtype, arg, flags);
1115       else if (parmnode == void_list_node)
1116         break;
1117       else if (parmnode)
1118         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1119       else
1120         {
1121           t = build1 (IDENTITY_CONV, argtype, arg);
1122           ICS_ELLIPSIS_FLAG (t) = 1;
1123         }
1124
1125       TREE_VEC_ELT (convs, i) = t;
1126       if (! t)
1127         break;
1128
1129       if (ICS_BAD_FLAG (t))
1130         viable = -1;
1131
1132       if (i == 0)
1133         continue;
1134
1135       if (parmnode)
1136         parmnode = TREE_CHAIN (parmnode);
1137       argnode = TREE_CHAIN (argnode);
1138     }
1139
1140   if (i < len)
1141     viable = 0;
1142
1143   for (; parmnode && parmnode != void_list_node;
1144        parmnode = TREE_CHAIN (parmnode))
1145     if (! TREE_PURPOSE (parmnode))
1146       {
1147         viable = 0;
1148         break;
1149       }
1150
1151   return add_candidate (candidates, totype, convs, viable);
1152 }
1153
1154 static struct z_candidate *
1155 build_builtin_candidate (candidates, fnname, type1, type2,
1156                          args, argtypes, flags)
1157      struct z_candidate *candidates;
1158      tree fnname, type1, type2, *args, *argtypes;
1159      int flags;
1160
1161 {
1162   tree t, convs;
1163   int viable = 1, i;
1164   tree types[2];
1165
1166   types[0] = type1;
1167   types[1] = type2;
1168
1169   convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1170
1171   for (i = 0; i < 2; ++i)
1172     {
1173       if (! args[i])
1174         break;
1175
1176       t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1177       if (! t)
1178         {
1179           viable = 0;
1180           /* We need something for printing the candidate.  */
1181           t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1182         }
1183       else if (ICS_BAD_FLAG (t))
1184         viable = 0;
1185       TREE_VEC_ELT (convs, i) = t;
1186     }
1187
1188   /* For COND_EXPR we rearranged the arguments; undo that now.  */
1189   if (args[2])
1190     {
1191       TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1192       TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1193       t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1194       if (t)
1195         TREE_VEC_ELT (convs, 0) = t;
1196       else
1197         viable = 0;
1198     }      
1199
1200   return add_candidate (candidates, fnname, convs, viable);
1201 }
1202
1203 static int
1204 is_complete (t)
1205      tree t;
1206 {
1207   return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1208 }
1209
1210 /* Create any builtin operator overload candidates for the operator in
1211    question given the converted operand types TYPE1 and TYPE2.  The other
1212    args are passed through from add_builtin_candidates to
1213    build_builtin_candidate.  */
1214
1215 static struct z_candidate *
1216 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1217                        args, argtypes, flags)
1218      struct z_candidate *candidates;
1219      enum tree_code code, code2;
1220      tree fnname, type1, type2, *args, *argtypes;
1221      int flags;
1222 {
1223   switch (code)
1224     {
1225     case POSTINCREMENT_EXPR:
1226     case POSTDECREMENT_EXPR:
1227       args[1] = integer_zero_node;
1228       type2 = integer_type_node;
1229       break;
1230     default:
1231       break;
1232     }
1233
1234   switch (code)
1235     {
1236
1237 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1238      and  VQ  is  either  volatile or empty, there exist candidate operator
1239      functions of the form
1240              VQ T&   operator++(VQ T&);
1241              T       operator++(VQ T&, int);
1242    5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1243      type  other than bool, and VQ is either volatile or empty, there exist
1244      candidate operator functions of the form
1245              VQ T&   operator--(VQ T&);
1246              T       operator--(VQ T&, int);
1247    6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
1248      complete  object type, and VQ is either volatile or empty, there exist
1249      candidate operator functions of the form
1250              T*VQ&   operator++(T*VQ&);
1251              T*VQ&   operator--(T*VQ&);
1252              T*      operator++(T*VQ&, int);
1253              T*      operator--(T*VQ&, int);  */
1254
1255     case POSTDECREMENT_EXPR:
1256     case PREDECREMENT_EXPR:
1257       if (TREE_CODE (type1) == BOOLEAN_TYPE)
1258         return candidates;
1259     case POSTINCREMENT_EXPR:
1260     case PREINCREMENT_EXPR:
1261       if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1262           || TYPE_PTROB_P (type1))
1263         {
1264           type1 = build_reference_type (type1);
1265           break;
1266         }
1267       return candidates;
1268
1269 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1270      exist candidate operator functions of the form
1271
1272              T&      operator*(T*);
1273
1274    8 For every function type T, there exist candidate operator functions of
1275      the form
1276              T&      operator*(T*);  */
1277
1278     case INDIRECT_REF:
1279       if (TREE_CODE (type1) == POINTER_TYPE
1280           && (TYPE_PTROB_P (type1)
1281               || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1282         break;
1283       return candidates;
1284
1285 /* 9 For every type T, there exist candidate operator functions of the form
1286              T*      operator+(T*);
1287
1288    10For  every  promoted arithmetic type T, there exist candidate operator
1289      functions of the form
1290              T       operator+(T);
1291              T       operator-(T);  */
1292
1293     case CONVERT_EXPR: /* unary + */
1294       if (TREE_CODE (type1) == POINTER_TYPE
1295           && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1296         break;
1297     case NEGATE_EXPR:
1298       if (ARITHMETIC_TYPE_P (type1))
1299         break;
1300       return candidates;
1301
1302 /* 11For every promoted integral type T,  there  exist  candidate  operator
1303      functions of the form
1304              T       operator~(T);  */
1305
1306     case BIT_NOT_EXPR:
1307       if (INTEGRAL_TYPE_P (type1))
1308         break;
1309       return candidates;
1310
1311 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1312      is the same type as C2 or is a derived class of C2, T  is  a  complete
1313      object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1314      there exist candidate operator functions of the form
1315              CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1316      where CV12 is the union of CV1 and CV2.  */
1317
1318     case MEMBER_REF:
1319       if (TREE_CODE (type1) == POINTER_TYPE
1320           && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1321         {
1322           tree c1 = TREE_TYPE (type1);
1323           tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1324                      ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1325                      : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1326
1327           if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1328               && (TYPE_PTRMEMFUNC_P (type2)
1329                   || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1330             break;
1331         }
1332       return candidates;
1333
1334 /* 13For every pair of promoted arithmetic types L and R, there exist  can-
1335      didate operator functions of the form
1336              LR      operator*(L, R);
1337              LR      operator/(L, R);
1338              LR      operator+(L, R);
1339              LR      operator-(L, R);
1340              bool    operator<(L, R);
1341              bool    operator>(L, R);
1342              bool    operator<=(L, R);
1343              bool    operator>=(L, R);
1344              bool    operator==(L, R);
1345              bool    operator!=(L, R);
1346      where  LR  is  the  result of the usual arithmetic conversions between
1347      types L and R.
1348
1349    14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
1350      unqualified  complete  object  type and I is a promoted integral type,
1351      there exist candidate operator functions of the form
1352              T*      operator+(T*, I);
1353              T&      operator[](T*, I);
1354              T*      operator-(T*, I);
1355              T*      operator+(I, T*);
1356              T&      operator[](I, T*);
1357
1358    15For every T, where T is a pointer to complete object type, there exist
1359      candidate operator functions of the form112)
1360              ptrdiff_t operator-(T, T);
1361
1362    16For  every pointer type T, there exist candidate operator functions of
1363      the form
1364              bool    operator<(T, T);
1365              bool    operator>(T, T);
1366              bool    operator<=(T, T);
1367              bool    operator>=(T, T);
1368              bool    operator==(T, T);
1369              bool    operator!=(T, T);
1370
1371    17For every pointer to member type T,  there  exist  candidate  operator
1372      functions of the form
1373              bool    operator==(T, T);
1374              bool    operator!=(T, T);  */
1375
1376     case MINUS_EXPR:
1377       if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1378         break;
1379       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1380         {
1381           type2 = ptrdiff_type_node;
1382           break;
1383         }
1384     case MULT_EXPR:
1385     case TRUNC_DIV_EXPR:
1386       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1387         break;
1388       return candidates;
1389
1390     case EQ_EXPR:
1391     case NE_EXPR:
1392       if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1393           || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1394         break;
1395       if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1396           && null_ptr_cst_p (args[1]))
1397         {
1398           type2 = type1;
1399           break;
1400         }
1401       if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1402           && null_ptr_cst_p (args[0]))
1403         {
1404           type1 = type2;
1405           break;
1406         }
1407     case LT_EXPR:
1408     case GT_EXPR:
1409     case LE_EXPR:
1410     case GE_EXPR:
1411     case MAX_EXPR:
1412     case MIN_EXPR:
1413       if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1414           || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1415         break;
1416       if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1417         {
1418           type2 = type1;
1419           break;
1420         }
1421       if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1422         {
1423           type1 = type2;
1424           break;
1425         }
1426       return candidates;
1427
1428     case PLUS_EXPR:
1429       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1430         break;
1431     case ARRAY_REF:
1432       if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1433         {
1434           type1 = ptrdiff_type_node;
1435           break;
1436         }
1437       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1438         {
1439           type2 = ptrdiff_type_node;
1440           break;
1441         }
1442       return candidates;
1443
1444 /* 18For  every pair of promoted integral types L and R, there exist candi-
1445      date operator functions of the form
1446              LR      operator%(L, R);
1447              LR      operator&(L, R);
1448              LR      operator^(L, R);
1449              LR      operator|(L, R);
1450              L       operator<<(L, R);
1451              L       operator>>(L, R);
1452      where LR is the result of the  usual  arithmetic  conversions  between
1453      types L and R.  */
1454
1455     case TRUNC_MOD_EXPR:
1456     case BIT_AND_EXPR:
1457     case BIT_IOR_EXPR:
1458     case BIT_XOR_EXPR:
1459     case LSHIFT_EXPR:
1460     case RSHIFT_EXPR:
1461       if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1462         break;
1463       return candidates;
1464
1465 /* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
1466      type, VQ is either volatile or empty, and R is a  promoted  arithmetic
1467      type, there exist candidate operator functions of the form
1468              VQ L&   operator=(VQ L&, R);
1469              VQ L&   operator*=(VQ L&, R);
1470              VQ L&   operator/=(VQ L&, R);
1471              VQ L&   operator+=(VQ L&, R);
1472              VQ L&   operator-=(VQ L&, R);
1473
1474    20For  every  pair T, VQ), where T is any type and VQ is either volatile
1475      or empty, there exist candidate operator functions of the form
1476              T*VQ&   operator=(T*VQ&, T*);
1477
1478    21For every pair T, VQ), where T is a pointer to member type and  VQ  is
1479      either  volatile or empty, there exist candidate operator functions of
1480      the form
1481              VQ T&   operator=(VQ T&, T);
1482
1483    22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
1484      unqualified  complete object type, VQ is either volatile or empty, and
1485      I is a promoted integral type, there exist  candidate  operator  func-
1486      tions of the form
1487              T*VQ&   operator+=(T*VQ&, I);
1488              T*VQ&   operator-=(T*VQ&, I);
1489
1490    23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
1491      type, VQ is either volatile or empty, and R  is  a  promoted  integral
1492      type, there exist candidate operator functions of the form
1493
1494              VQ L&   operator%=(VQ L&, R);
1495              VQ L&   operator<<=(VQ L&, R);
1496              VQ L&   operator>>=(VQ L&, R);
1497              VQ L&   operator&=(VQ L&, R);
1498              VQ L&   operator^=(VQ L&, R);
1499              VQ L&   operator|=(VQ L&, R);  */
1500
1501     case MODIFY_EXPR:
1502       switch (code2)
1503         {
1504         case PLUS_EXPR:
1505         case MINUS_EXPR:
1506           if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1507             {
1508               type2 = ptrdiff_type_node;
1509               break;
1510             }
1511         case MULT_EXPR:
1512         case TRUNC_DIV_EXPR:
1513           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1514             break;
1515           return candidates;
1516
1517         case TRUNC_MOD_EXPR:
1518         case BIT_AND_EXPR:
1519         case BIT_IOR_EXPR:
1520         case BIT_XOR_EXPR:
1521         case LSHIFT_EXPR:
1522         case RSHIFT_EXPR:
1523           if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1524             break;
1525           return candidates;
1526
1527         case NOP_EXPR:
1528           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1529             break;
1530           if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1531               || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1532               || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1533               || ((TYPE_PTRMEMFUNC_P (type1)
1534                    || TREE_CODE (type1) == POINTER_TYPE)
1535                   && null_ptr_cst_p (args[1])))
1536             {
1537               type2 = type1;
1538               break;
1539             }
1540           return candidates;
1541
1542         default:
1543           my_friendly_abort (367);
1544         }
1545       type1 = build_reference_type (type1);
1546       break;
1547
1548     case COND_EXPR:
1549       /* Kludge around broken overloading rules whereby
1550          bool ? const char& : enum is ambiguous
1551          (between int and const char&).  */
1552       flags |= LOOKUP_NO_TEMP_BIND;
1553
1554       /* Extension: Support ?: of enumeral type.  Hopefully this will not
1555          be an extension for long.  */
1556       if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1557         break;
1558       else if (TREE_CODE (type1) == ENUMERAL_TYPE
1559                || TREE_CODE (type2) == ENUMERAL_TYPE)
1560         return candidates;
1561       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1562         break;
1563       if (TREE_CODE (type1) == TREE_CODE (type2)
1564           && (TREE_CODE (type1) == REFERENCE_TYPE
1565               || TREE_CODE (type1) == POINTER_TYPE
1566               || TYPE_PTRMEMFUNC_P (type1)
1567               || IS_AGGR_TYPE (type1)))
1568         break;
1569       if (TREE_CODE (type1) == REFERENCE_TYPE
1570           || TREE_CODE (type2) == REFERENCE_TYPE)
1571         return candidates;
1572       if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1573            && null_ptr_cst_p (args[1]))
1574           || IS_AGGR_TYPE (type1))
1575         {
1576           type2 = type1;
1577           break;
1578         }
1579       if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1580            && null_ptr_cst_p (args[0]))
1581           || IS_AGGR_TYPE (type2))
1582         {
1583           type1 = type2;
1584           break;
1585         }
1586       return candidates;
1587
1588     default:
1589       my_friendly_abort (367);
1590     }
1591
1592   /* If we're dealing with two pointer types, we need candidates
1593      for both of them.  */
1594   if (type2 && type1 != type2
1595       && TREE_CODE (type1) == TREE_CODE (type2)
1596       && (TREE_CODE (type1) == REFERENCE_TYPE
1597           || (TREE_CODE (type1) == POINTER_TYPE
1598               && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1599           || TYPE_PTRMEMFUNC_P (type1)
1600           || IS_AGGR_TYPE (type1)))
1601     {
1602       candidates = build_builtin_candidate
1603         (candidates, fnname, type1, type1, args, argtypes, flags);
1604       return build_builtin_candidate
1605         (candidates, fnname, type2, type2, args, argtypes, flags);
1606     }
1607
1608   return build_builtin_candidate
1609     (candidates, fnname, type1, type2, args, argtypes, flags);
1610 }
1611
1612 tree
1613 type_decays_to (type)
1614      tree type;
1615 {
1616   if (TREE_CODE (type) == ARRAY_TYPE)
1617     return build_pointer_type (TREE_TYPE (type));
1618   if (TREE_CODE (type) == FUNCTION_TYPE)
1619     return build_pointer_type (type);
1620   return type;
1621 }
1622
1623 /* There are three conditions of builtin candidates:
1624
1625    1) bool-taking candidates.  These are the same regardless of the input.
1626    2) pointer-pair taking candidates.  These are generated for each type
1627       one of the input types converts to.
1628    3) arithmetic candidates.  According to the WP, we should generate
1629       all of these, but I'm trying not to... */
1630
1631 static struct z_candidate *
1632 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1633      struct z_candidate *candidates;
1634      enum tree_code code, code2;
1635      tree fnname, *args;
1636      int flags;
1637 {
1638   int ref1, i;
1639   tree type, argtypes[3], types[2];
1640
1641   for (i = 0; i < 3; ++i)
1642     {
1643       if (args[i])
1644         argtypes[i]  = lvalue_type (args[i]);
1645       else
1646         argtypes[i] = NULL_TREE;
1647     }
1648
1649   switch (code)
1650     {
1651 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1652      and  VQ  is  either  volatile or empty, there exist candidate operator
1653      functions of the form
1654                  VQ T&   operator++(VQ T&);  */
1655
1656     case POSTINCREMENT_EXPR:
1657     case PREINCREMENT_EXPR:
1658     case POSTDECREMENT_EXPR:
1659     case PREDECREMENT_EXPR:
1660     case MODIFY_EXPR:
1661       ref1 = 1;
1662       break;
1663
1664 /* 24There also exist candidate operator functions of the form
1665              bool    operator!(bool);
1666              bool    operator&&(bool, bool);
1667              bool    operator||(bool, bool);  */
1668
1669     case TRUTH_NOT_EXPR:
1670       return build_builtin_candidate
1671         (candidates, fnname, boolean_type_node,
1672          NULL_TREE, args, argtypes, flags);
1673
1674     case TRUTH_ORIF_EXPR:
1675     case TRUTH_ANDIF_EXPR:
1676       return build_builtin_candidate
1677         (candidates, fnname, boolean_type_node,
1678          boolean_type_node, args, argtypes, flags);
1679
1680     case ADDR_EXPR:
1681     case COMPOUND_EXPR:
1682     case COMPONENT_REF:
1683       return candidates;
1684
1685     default:
1686       ref1 = 0;
1687     }
1688
1689   types[0] = types[1] = NULL_TREE;
1690
1691   for (i = 0; i < 2; ++i)
1692     {
1693       if (! args[i])
1694         ;
1695       else if (IS_AGGR_TYPE (argtypes[i]))
1696         {
1697           tree convs;
1698
1699           if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1700             return candidates;
1701
1702           convs = lookup_conversions (argtypes[i]);
1703
1704           if (code == COND_EXPR)
1705             {
1706               if (real_lvalue_p (args[i]))
1707                 types[i] = scratch_tree_cons
1708                   (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1709
1710               types[i] = scratch_tree_cons
1711                 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1712             }
1713
1714           else if (! convs)
1715             return candidates;
1716
1717           for (; convs; convs = TREE_CHAIN (convs))
1718             {
1719               type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
1720
1721               if (i == 0 && ref1
1722                   && (TREE_CODE (type) != REFERENCE_TYPE
1723                       || CP_TYPE_CONST_P (TREE_TYPE (type))))
1724                 continue;
1725
1726               if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1727                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1728
1729               type = non_reference (type);
1730               if (i != 0 || ! ref1)
1731                 {
1732                   type = TYPE_MAIN_VARIANT (type_decays_to (type));
1733                   if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1734                     types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1735                   if (INTEGRAL_TYPE_P (type))
1736                     type = type_promotes_to (type);
1737                 }
1738
1739               if (! value_member (type, types[i]))
1740                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1741             }
1742         }
1743       else
1744         {
1745           if (code == COND_EXPR && real_lvalue_p (args[i]))
1746             types[i] = scratch_tree_cons
1747               (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1748           type = non_reference (argtypes[i]);
1749           if (i != 0 || ! ref1)
1750             {
1751               type = TYPE_MAIN_VARIANT (type_decays_to (type));
1752               if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1753                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1754               if (INTEGRAL_TYPE_P (type))
1755                 type = type_promotes_to (type);
1756             }
1757           types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1758         }
1759     }
1760
1761   for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1762     {
1763       if (types[1])
1764         for (type = types[1]; type; type = TREE_CHAIN (type))
1765           candidates = add_builtin_candidate
1766             (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1767              TREE_VALUE (type), args, argtypes, flags);
1768       else
1769         candidates = add_builtin_candidate
1770           (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1771            NULL_TREE, args, argtypes, flags);
1772     }
1773
1774   return candidates;
1775 }
1776
1777
1778 /* If TMPL can be successfully instantiated as indicated by
1779    EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1780
1781    TMPL is the template.  EXPLICIT_TARGS are any explicit template
1782    arguments.  ARGLIST is the arguments provided at the call-site.
1783    The RETURN_TYPE is the desired type for conversion operators.  If
1784    OBJ is NULL_TREE, FLAGS are as for add_function_candidate.  If an
1785    OBJ is supplied, FLAGS are ignored, and OBJ is as for
1786    add_conv_candidate.  */
1787
1788 static struct z_candidate*
1789 add_template_candidate_real (candidates, tmpl, explicit_targs,
1790                              arglist, return_type, flags,
1791                              obj, strict)
1792      struct z_candidate *candidates;
1793      tree tmpl, explicit_targs, arglist, return_type;
1794      int flags;
1795      tree obj;
1796      unification_kind_t strict;
1797 {
1798   int ntparms = DECL_NTPARMS (tmpl);
1799   tree targs = make_scratch_vec (ntparms);
1800   struct z_candidate *cand;
1801   int i;
1802   tree fn;
1803
1804   i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1805                            return_type, strict);
1806
1807   if (i != 0)
1808     return candidates;
1809
1810   fn = instantiate_template (tmpl, targs);
1811   if (fn == error_mark_node)
1812     return candidates;
1813
1814   if (obj != NULL_TREE)
1815     /* Aha, this is a conversion function.  */
1816     cand = add_conv_candidate (candidates, fn, obj, arglist);
1817   else
1818     cand = add_function_candidate (candidates, fn, arglist, flags);
1819   if (DECL_TI_TEMPLATE (fn) != tmpl)
1820     /* This situation can occur if a member template of a template
1821        class is specialized.  Then, instantiate_template might return
1822        an instantiation of the specialization, in which case the
1823        DECL_TI_TEMPLATE field will point at the original
1824        specialization.  For example:
1825
1826          template <class T> struct S { template <class U> void f(U);
1827                                        template <> void f(int) {}; };
1828          S<double> sd;
1829          sd.f(3);
1830
1831        Here, TMPL will be template <class U> S<double>::f(U).
1832        And, instantiate template will give us the specialization
1833        template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
1834        for this will point at template <class T> template <> S<T>::f(int),
1835        so that we can find the definition.  For the purposes of
1836        overload resolution, however, we want the original TMPL.  */
1837     cand->template = tree_cons (tmpl, targs, NULL_TREE);
1838   else
1839     cand->template = DECL_TEMPLATE_INFO (fn);
1840
1841   return cand;
1842 }
1843
1844
1845 static struct z_candidate *
1846 add_template_candidate (candidates, tmpl, explicit_targs, 
1847                         arglist, return_type, flags, strict)
1848      struct z_candidate *candidates;
1849      tree tmpl, explicit_targs, arglist, return_type;
1850      int flags;
1851      unification_kind_t strict;
1852 {
1853   return 
1854     add_template_candidate_real (candidates, tmpl, explicit_targs,
1855                                  arglist, return_type, flags,
1856                                  NULL_TREE, strict);
1857 }
1858
1859
1860 static struct z_candidate *
1861 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1862      struct z_candidate *candidates;
1863      tree tmpl, obj, arglist, return_type;
1864 {
1865   return 
1866     add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1867                                  return_type, 0, obj, DEDUCE_CONV);
1868 }
1869
1870
1871 static int
1872 any_viable (cands)
1873      struct z_candidate *cands;
1874 {
1875   for (; cands; cands = cands->next)
1876     if (pedantic ? cands->viable == 1 : cands->viable)
1877       return 1;
1878   return 0;
1879 }
1880
1881 static struct z_candidate *
1882 splice_viable (cands)
1883      struct z_candidate *cands;
1884 {
1885   struct z_candidate **p = &cands;
1886
1887   for (; *p; )
1888     {
1889       if (pedantic ? (*p)->viable == 1 : (*p)->viable)
1890         p = &((*p)->next);
1891       else
1892         *p = (*p)->next;
1893     }
1894
1895   return cands;
1896 }
1897
1898 static tree
1899 build_this (obj)
1900      tree obj;
1901 {
1902   /* Fix this to work on non-lvalues.  */
1903   if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
1904       || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
1905     return obj;
1906   else
1907     return build_unary_op (ADDR_EXPR, obj, 0);
1908 }
1909
1910 static void
1911 print_z_candidates (candidates)
1912      struct z_candidate *candidates;
1913 {
1914   const char *str = "candidates are:";
1915   for (; candidates; candidates = candidates->next)
1916     {
1917       if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
1918         {
1919           if (candidates->fn == ansi_opname [COND_EXPR])
1920             cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
1921                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
1922                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
1923                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
1924           else if (TREE_VEC_LENGTH (candidates->convs) == 2)
1925             cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
1926                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
1927                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
1928           else
1929             cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
1930                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
1931         }
1932       else if (TYPE_P (candidates->fn))
1933         cp_error ("%s %T <conversion>", str, candidates->fn);
1934       else
1935         cp_error_at ("%s %+#D%s", str, candidates->fn,
1936                      candidates->viable == -1 ? " <near match>" : "");
1937       str = "               "; 
1938     }
1939 }
1940
1941 /* Returns the best overload candidate to perform the requested
1942    conversion.  This function is used for three the overloading situations
1943    described in [over.match.copy], [over.match.conv], and [over.match.ref].
1944    If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
1945    per [dcl.init.ref], so we ignore temporary bindings.  */
1946
1947 static struct z_candidate *
1948 build_user_type_conversion_1 (totype, expr, flags)
1949      tree totype, expr;
1950      int flags;
1951 {
1952   struct z_candidate *candidates, *cand;
1953   tree fromtype = TREE_TYPE (expr);
1954   tree ctors = NULL_TREE, convs = NULL_TREE, *p;
1955   tree args = NULL_TREE;
1956   tree templates = NULL_TREE;
1957
1958   if (IS_AGGR_TYPE (totype))
1959     ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
1960   if (IS_AGGR_TYPE (fromtype)
1961       && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
1962     convs = lookup_conversions (fromtype);
1963
1964   candidates = 0;
1965   flags |= LOOKUP_NO_CONVERSION;
1966
1967   if (ctors)
1968     {
1969       tree t = build_int_2 (0, 0);
1970       TREE_TYPE (t) = build_pointer_type (totype);
1971       args = build_scratch_list (NULL_TREE, expr);
1972       if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
1973         args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
1974       args = scratch_tree_cons (NULL_TREE, t, args);
1975
1976       ctors = TREE_VALUE (ctors);
1977     }
1978   for (; ctors; ctors = OVL_NEXT (ctors))
1979     {
1980       tree ctor = OVL_CURRENT (ctors);
1981       if (DECL_NONCONVERTING_P (ctor))
1982         continue;
1983
1984       if (TREE_CODE (ctor) == TEMPLATE_DECL) 
1985         {
1986           templates = scratch_tree_cons (NULL_TREE, ctor, templates);
1987           candidates = 
1988             add_template_candidate (candidates, ctor,
1989                                     NULL_TREE, args, NULL_TREE, flags,
1990                                     DEDUCE_CALL);
1991         } 
1992       else 
1993         candidates = add_function_candidate (candidates, ctor,
1994                                              args, flags); 
1995
1996       if (candidates) 
1997         {
1998           candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
1999           candidates->basetype_path = TYPE_BINFO (totype);
2000         } 
2001     }
2002
2003   if (convs)
2004     args = build_scratch_list (NULL_TREE, build_this (expr));
2005
2006   for (; convs; convs = TREE_CHAIN (convs))
2007     {
2008       tree fns = TREE_VALUE (convs);
2009       int convflags = LOOKUP_NO_CONVERSION;
2010       tree ics;
2011
2012       /* If we are called to convert to a reference type, we are trying to
2013          find an lvalue binding, so don't even consider temporaries.  If
2014          we don't find an lvalue binding, the caller will try again to
2015          look for a temporary binding.  */
2016       if (TREE_CODE (totype) == REFERENCE_TYPE)
2017         convflags |= LOOKUP_NO_TEMP_BIND;
2018
2019       if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2020         ics = implicit_conversion
2021           (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2022       else
2023         /* We can't compute this yet.  */
2024         ics = error_mark_node;
2025
2026       if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2027         /* ignore the near match.  */;
2028       else if (ics)
2029         for (; fns; fns = OVL_NEXT (fns))
2030           {
2031             tree fn = OVL_CURRENT (fns);
2032             struct z_candidate *old_candidates = candidates;
2033
2034             if (TREE_CODE (fn) == TEMPLATE_DECL)
2035               {
2036                 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2037                 candidates = 
2038                   add_template_candidate (candidates, fn, NULL_TREE,
2039                                           args, totype, flags,
2040                                           DEDUCE_CONV);
2041               } 
2042             else 
2043               candidates = add_function_candidate (candidates, fn,
2044                                                    args, flags); 
2045
2046             if (candidates != old_candidates)
2047               {
2048                 if (TREE_CODE (fn) == TEMPLATE_DECL)
2049                   ics = implicit_conversion
2050                     (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2051                      0, convflags);
2052
2053                 candidates->second_conv = ics;
2054                 candidates->basetype_path = TREE_PURPOSE (convs);
2055
2056                 if (ics == NULL_TREE)
2057                   candidates->viable = 0;
2058                 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2059                   candidates->viable = -1;
2060               }
2061           }
2062     }
2063
2064   if (! any_viable (candidates))
2065     {
2066 #if 0
2067       if (flags & LOOKUP_COMPLAIN)
2068         {
2069           if (candidates && ! candidates->next)
2070             /* say why this one won't work or try to be loose */;
2071           else
2072             cp_error ("no viable candidates");
2073         }
2074 #endif
2075
2076       return 0;
2077     }
2078
2079   candidates = splice_viable (candidates);
2080   cand = tourney (candidates);
2081
2082   if (cand == 0)
2083     {
2084       if (flags & LOOKUP_COMPLAIN)
2085         {
2086           cp_error ("conversion from `%T' to `%T' is ambiguous",
2087                     fromtype, totype);
2088           print_z_candidates (candidates);
2089         }
2090
2091       cand = candidates;        /* any one will do */
2092       cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2093       ICS_USER_FLAG (cand->second_conv) = 1;
2094       ICS_BAD_FLAG (cand->second_conv) = 1;
2095
2096       return cand;
2097     }
2098
2099   for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2100     p = &(TREE_OPERAND (*p, 0));
2101
2102   /* Pedantically, normal function declarations are never considered
2103      to refer to template instantiations, so we only do this with
2104      -fguiding-decls.  */ 
2105   if (flag_guiding_decls && templates && ! cand->template 
2106       && !DECL_INITIAL (cand->fn) 
2107       && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2108     add_maybe_template (cand->fn, templates);
2109
2110   *p = build
2111     (USER_CONV,
2112      (DECL_CONSTRUCTOR_P (cand->fn)
2113       ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2114      expr, build_expr_ptr_wrapper (cand));
2115   ICS_USER_FLAG (cand->second_conv) = 1;
2116   if (cand->viable == -1)
2117     ICS_BAD_FLAG (cand->second_conv) = 1;
2118
2119   return cand;
2120 }
2121
2122 tree
2123 build_user_type_conversion (totype, expr, flags)
2124      tree totype, expr;
2125      int flags;
2126 {
2127   struct z_candidate *cand
2128     = build_user_type_conversion_1 (totype, expr, flags);
2129
2130   if (cand)
2131     {
2132       if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2133         return error_mark_node;
2134       return convert_from_reference (convert_like (cand->second_conv, expr));
2135     }
2136   return NULL_TREE;
2137 }
2138
2139 /* Do any initial processing on the arguments to a function call.  */
2140
2141 static tree
2142 resolve_args (args)
2143      tree args;
2144 {
2145   tree t;
2146   for (t = args; t; t = TREE_CHAIN (t))
2147     {
2148       if (TREE_VALUE (t) == error_mark_node)
2149         return error_mark_node;
2150       else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2151         {
2152           error ("invalid use of void expression");
2153           return error_mark_node;
2154         }
2155       else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2156         TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2157     }
2158   return args;
2159 }
2160       
2161 tree
2162 build_new_function_call (fn, args)
2163      tree fn, args;
2164 {
2165   struct z_candidate *candidates = 0, *cand;
2166   tree explicit_targs = NULL_TREE;
2167   int template_only = 0;
2168
2169   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2170     {
2171       explicit_targs = TREE_OPERAND (fn, 1);
2172       fn = TREE_OPERAND (fn, 0);
2173       template_only = 1;
2174     }
2175
2176   if (really_overloaded_fn (fn))
2177     {
2178       tree t1;
2179       tree templates = NULL_TREE;
2180
2181       args = resolve_args (args);
2182
2183       if (args == error_mark_node)
2184         return error_mark_node;
2185
2186       for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2187         {
2188           tree t = OVL_FUNCTION (t1);
2189           struct z_candidate *old_candidates = candidates;
2190
2191           if (TREE_CODE (t) == TEMPLATE_DECL)
2192             {
2193               templates = scratch_tree_cons (NULL_TREE, t, templates);
2194               candidates = add_template_candidate
2195                 (candidates, t, explicit_targs, args, NULL_TREE,
2196                  LOOKUP_NORMAL, DEDUCE_CALL);  
2197             }
2198           else if (! template_only)
2199             candidates = add_function_candidate
2200               (candidates, t, args, LOOKUP_NORMAL);
2201
2202           if (candidates != old_candidates)
2203             candidates->basetype_path = DECL_REAL_CONTEXT (t);
2204         }
2205
2206       if (! any_viable (candidates))
2207         {
2208           if (candidates && ! candidates->next)
2209             return build_function_call (candidates->fn, args);
2210           cp_error ("no matching function for call to `%D (%A)'",
2211                     DECL_NAME (OVL_FUNCTION (fn)), args);
2212           if (candidates)
2213             print_z_candidates (candidates);
2214           return error_mark_node;
2215         }
2216       candidates = splice_viable (candidates);
2217       cand = tourney (candidates);
2218
2219       if (cand == 0)
2220         {
2221           cp_error ("call of overloaded `%D (%A)' is ambiguous",
2222                     DECL_NAME (OVL_FUNCTION (fn)), args);
2223           print_z_candidates (candidates);
2224           return error_mark_node;
2225         }
2226
2227       /* Pedantically, normal function declarations are never considered
2228          to refer to template instantiations, so we only do this with
2229          -fguiding-decls.  */
2230       if (flag_guiding_decls && templates && ! cand->template 
2231           && ! DECL_INITIAL (cand->fn))
2232         add_maybe_template (cand->fn, templates);
2233
2234       return build_over_call (cand, args, LOOKUP_NORMAL);
2235     }
2236
2237   /* This is not really overloaded. */
2238   fn = OVL_CURRENT (fn);
2239
2240   return build_function_call (fn, args);
2241 }
2242
2243 static tree
2244 build_object_call (obj, args)
2245      tree obj, args;
2246 {
2247   struct z_candidate *candidates = 0, *cand;
2248   tree fns, convs, mem_args = NULL_TREE;
2249   tree type = TREE_TYPE (obj);
2250
2251   if (TYPE_PTRMEMFUNC_P (type))
2252     {
2253       /* It's no good looking for an overloaded operator() on a
2254          pointer-to-member-function.  */
2255       cp_error ("pointer-to-member function %E cannot be called", obj);
2256       cp_error ("without an object; consider using .* or ->*");
2257       return error_mark_node;
2258     }
2259
2260   fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2261   if (fns == error_mark_node)
2262     return error_mark_node;
2263
2264   args = resolve_args (args);
2265
2266   if (args == error_mark_node)
2267     return error_mark_node;
2268
2269   if (fns)
2270     {
2271       tree base = TREE_PURPOSE (fns);
2272       mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2273
2274       for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2275         {
2276           tree fn = OVL_CURRENT (fns);
2277           if (TREE_CODE (fn) == TEMPLATE_DECL)
2278             {
2279               candidates 
2280                 = add_template_candidate (candidates, fn, NULL_TREE,
2281                                           mem_args, NULL_TREE, 
2282                                           LOOKUP_NORMAL, DEDUCE_CALL);
2283             }
2284           else
2285             candidates = add_function_candidate
2286               (candidates, fn, mem_args, LOOKUP_NORMAL);
2287
2288           if (candidates)
2289             candidates->basetype_path = base;
2290         }
2291     }
2292
2293   convs = lookup_conversions (type);
2294
2295   for (; convs; convs = TREE_CHAIN (convs))
2296     {
2297       tree fns = TREE_VALUE (convs);
2298       tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2299
2300       if ((TREE_CODE (totype) == POINTER_TYPE
2301            || TREE_CODE (totype) == REFERENCE_TYPE)
2302           && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2303         for (; fns; fns = OVL_NEXT (fns))
2304           {
2305             tree fn = OVL_CURRENT (fns);
2306             if (TREE_CODE (fn) == TEMPLATE_DECL) 
2307               {
2308                 candidates = add_template_conv_candidate (candidates,
2309                                                           fn,
2310                                                           obj,
2311                                                           args,
2312                                                           totype);
2313               }
2314             else
2315               candidates = add_conv_candidate (candidates, fn, obj, args);
2316
2317             if (candidates)
2318               candidates->basetype_path = TREE_PURPOSE (convs);
2319           }
2320     }
2321
2322   if (! any_viable (candidates))
2323     {
2324       cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2325       print_z_candidates (candidates);
2326       return error_mark_node;
2327     }
2328
2329   candidates = splice_viable (candidates);
2330   cand = tourney (candidates);
2331
2332   if (cand == 0)
2333     {
2334       cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2335       print_z_candidates (candidates);
2336       return error_mark_node;
2337     }
2338
2339   if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2340     return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2341
2342   obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2343
2344   /* FIXME */
2345   return build_function_call (obj, args);
2346 }
2347
2348 static void
2349 op_error (code, code2, arg1, arg2, arg3, problem)
2350      enum tree_code code, code2;
2351      tree arg1, arg2, arg3;
2352      const char *problem;
2353 {
2354   const char * opname
2355     = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2356
2357   switch (code)
2358     {
2359     case COND_EXPR:
2360       cp_error ("%s for `%T ? %T : %T'", problem,
2361                 error_type (arg1), error_type (arg2), error_type (arg3));
2362       break;
2363     case POSTINCREMENT_EXPR:
2364     case POSTDECREMENT_EXPR:
2365       cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2366       break;
2367     case ARRAY_REF:
2368       cp_error ("%s for `%T[%T]'", problem,
2369                 error_type (arg1), error_type (arg2));
2370       break;
2371     default:
2372       if (arg2)
2373         cp_error ("%s for `%T %s %T'", problem,
2374                   error_type (arg1), opname, error_type (arg2));
2375       else
2376         cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2377     }
2378 }
2379
2380 tree
2381 build_new_op (code, flags, arg1, arg2, arg3)
2382      enum tree_code code;
2383      int flags;
2384      tree arg1, arg2, arg3;
2385 {
2386   struct z_candidate *candidates = 0, *cand;
2387   tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2388   enum tree_code code2 = NOP_EXPR;
2389   tree templates = NULL_TREE;
2390   tree conv;
2391
2392   if (arg1 == error_mark_node
2393       || arg2 == error_mark_node
2394       || arg3 == error_mark_node)
2395     return error_mark_node;
2396
2397   /* This can happen if a template takes all non-type parameters, e.g.
2398      undeclared_template<1, 5, 72>a;  */
2399   if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2400     {
2401       cp_error ("`%D' must be declared before use", arg1);
2402       return error_mark_node;
2403     }
2404
2405   if (code == MODIFY_EXPR)
2406     {
2407       code2 = TREE_CODE (arg3);
2408       arg3 = NULL_TREE;
2409       fnname = ansi_assopname[code2];
2410     }
2411   else
2412     fnname = ansi_opname[code];
2413
2414   switch (code)
2415     {
2416     case NEW_EXPR:
2417     case VEC_NEW_EXPR:
2418     case VEC_DELETE_EXPR:
2419     case DELETE_EXPR:
2420       /* Use build_op_new_call and build_op_delete_call instead. */
2421       my_friendly_abort (981018);
2422
2423     case CALL_EXPR:
2424       return build_object_call (arg1, arg2);
2425
2426     default:
2427       break;
2428     }
2429
2430   /* The comma operator can have void args.  */
2431   if (TREE_CODE (arg1) == OFFSET_REF)
2432     arg1 = resolve_offset_ref (arg1);
2433   if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2434     arg2 = resolve_offset_ref (arg2);
2435   if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2436     arg3 = resolve_offset_ref (arg3);
2437
2438   if (code == COND_EXPR)
2439     {
2440       if (arg2 == NULL_TREE
2441           || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2442           || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2443           || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2444               && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2445         goto builtin;
2446     }
2447   else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2448            && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2449     goto builtin;
2450
2451   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2452     arg2 = integer_zero_node;
2453
2454   if (arg2 && arg3)
2455     arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2456                       (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2457   else if (arg2)
2458     arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2459   else
2460     arglist = build_scratch_list (NULL_TREE, arg1);
2461
2462   fns = lookup_function_nonclass (fnname, arglist);
2463
2464   if (fns && TREE_CODE (fns) == TREE_LIST)
2465     fns = TREE_VALUE (fns);
2466   for (; fns; fns = OVL_NEXT (fns))
2467     {
2468       tree fn = OVL_CURRENT (fns);
2469       if (TREE_CODE (fn) == TEMPLATE_DECL)
2470         {
2471           templates = scratch_tree_cons (NULL_TREE, fn, templates);
2472           candidates 
2473             = add_template_candidate (candidates, fn, NULL_TREE,
2474                                       arglist, TREE_TYPE (fnname),
2475                                       flags, DEDUCE_CALL); 
2476         }
2477       else
2478         candidates = add_function_candidate (candidates, fn, arglist, flags);
2479     }
2480
2481   if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2482     {
2483       fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2484       if (fns == error_mark_node)
2485         return fns;
2486     }
2487   else
2488     fns = NULL_TREE;
2489
2490   if (fns)
2491     {
2492       tree basetype = TREE_PURPOSE (fns);
2493       mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2494       for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2495         {
2496           tree fn = OVL_CURRENT (fns);
2497           tree this_arglist;
2498
2499           if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2500             this_arglist = mem_arglist;
2501           else
2502             this_arglist = arglist;
2503
2504           if (TREE_CODE (fn) == TEMPLATE_DECL)
2505             {
2506               /* A member template. */
2507               templates = scratch_tree_cons (NULL_TREE, fn, templates);
2508               candidates 
2509                 = add_template_candidate (candidates, fn, NULL_TREE,
2510                                           this_arglist,  TREE_TYPE (fnname),
2511                                           flags, DEDUCE_CALL); 
2512             }
2513           else
2514             candidates = add_function_candidate
2515               (candidates, fn, this_arglist, flags);
2516
2517           if (candidates) 
2518             candidates->basetype_path = basetype;
2519         }
2520     }
2521
2522   {
2523     tree args[3];
2524
2525     /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2526        to know about two args; a builtin candidate will always have a first
2527        parameter of type bool.  We'll handle that in
2528        build_builtin_candidate.  */
2529     if (code == COND_EXPR)
2530       {
2531         args[0] = arg2;
2532         args[1] = arg3;
2533         args[2] = arg1;
2534       }
2535     else
2536       {
2537         args[0] = arg1;
2538         args[1] = arg2;
2539         args[2] = NULL_TREE;
2540       }
2541
2542     candidates = add_builtin_candidates
2543       (candidates, code, code2, fnname, args, flags);
2544   }
2545
2546   if (! any_viable (candidates))
2547     {
2548       switch (code)
2549         {
2550         case POSTINCREMENT_EXPR:
2551         case POSTDECREMENT_EXPR:
2552           /* Look for an `operator++ (int)'.  If they didn't have
2553              one, then we fall back to the old way of doing things.  */
2554           if (flags & LOOKUP_COMPLAIN)
2555             cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2556                         fnname, opname_tab [code]);
2557           if (code == POSTINCREMENT_EXPR)
2558             code = PREINCREMENT_EXPR;
2559           else
2560             code = PREDECREMENT_EXPR;   
2561           return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2562           
2563           /* The caller will deal with these.  */
2564         case ADDR_EXPR:
2565         case COMPOUND_EXPR:
2566         case COMPONENT_REF:
2567           return NULL_TREE;
2568
2569         default:
2570           break;
2571         }
2572       if (flags & LOOKUP_COMPLAIN)
2573         {
2574           op_error (code, code2, arg1, arg2, arg3, "no match");
2575           print_z_candidates (candidates);
2576         }
2577       return error_mark_node;
2578     }
2579   candidates = splice_viable (candidates);
2580   cand = tourney (candidates);
2581
2582   if (cand == 0)
2583     {
2584       if (flags & LOOKUP_COMPLAIN)
2585         {
2586           op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2587           print_z_candidates (candidates);
2588         }
2589       return error_mark_node;
2590     }
2591
2592   if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2593     {
2594       extern int warn_synth;
2595       if (warn_synth
2596           && fnname == ansi_opname[MODIFY_EXPR]
2597           && DECL_ARTIFICIAL (cand->fn)
2598           && candidates->next
2599           && ! candidates->next->next)
2600         {
2601           cp_warning ("using synthesized `%#D' for copy assignment",
2602                       cand->fn);
2603           cp_warning_at ("  where cfront would use `%#D'",
2604                          cand == candidates
2605                          ? candidates->next->fn
2606                          : candidates->fn);
2607         }
2608
2609       /* Pedantically, normal function declarations are never considered
2610          to refer to template instantiations, so we only do this with
2611          -fguiding-decls.  */ 
2612       if (flag_guiding_decls && templates && ! cand->template 
2613           && ! DECL_INITIAL (cand->fn)
2614           && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2615         add_maybe_template (cand->fn, templates);
2616
2617       return build_over_call
2618         (cand,
2619          TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2620          ? mem_arglist : arglist,
2621          LOOKUP_NORMAL);
2622     }
2623
2624   /* Check for comparison of different enum types.  */
2625   switch (code)
2626     {
2627     case GT_EXPR:
2628     case LT_EXPR:
2629     case GE_EXPR:
2630     case LE_EXPR:
2631     case EQ_EXPR:
2632     case NE_EXPR:
2633       if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 
2634           && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 
2635           && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2636               != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2637         {
2638           cp_warning ("comparison between `%#T' and `%#T'", 
2639                       TREE_TYPE (arg1), TREE_TYPE (arg2));
2640         }
2641       break;
2642     default:
2643       break;
2644     }
2645
2646   /* We need to strip any leading REF_BIND so that bitfields don't cause
2647      errors.  This should not remove any important conversions, because
2648      builtins don't apply to class objects directly.  */
2649   conv = TREE_VEC_ELT (cand->convs, 0);
2650   if (TREE_CODE (conv) == REF_BIND)
2651     conv = TREE_OPERAND (conv, 0);
2652   arg1 = convert_like (conv, arg1);
2653   if (arg2)
2654     arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2655   if (arg3)
2656     arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2657
2658 builtin:
2659   switch (code)
2660     {
2661     case MODIFY_EXPR:
2662       return build_modify_expr (arg1, code2, arg2);
2663
2664     case INDIRECT_REF:
2665       return build_indirect_ref (arg1, "unary *");
2666
2667     case PLUS_EXPR:
2668     case MINUS_EXPR:
2669     case MULT_EXPR:
2670     case TRUNC_DIV_EXPR:
2671     case GT_EXPR:
2672     case LT_EXPR:
2673     case GE_EXPR:
2674     case LE_EXPR:
2675     case EQ_EXPR:
2676     case NE_EXPR:
2677     case MAX_EXPR:
2678     case MIN_EXPR:
2679     case LSHIFT_EXPR:
2680     case RSHIFT_EXPR:
2681     case TRUNC_MOD_EXPR:
2682     case BIT_AND_EXPR:
2683     case BIT_IOR_EXPR:
2684     case BIT_XOR_EXPR:
2685     case TRUTH_ANDIF_EXPR:
2686     case TRUTH_ORIF_EXPR:
2687       return build_binary_op_nodefault (code, arg1, arg2, code);
2688
2689     case CONVERT_EXPR:
2690     case NEGATE_EXPR:
2691     case BIT_NOT_EXPR:
2692     case TRUTH_NOT_EXPR:
2693     case PREINCREMENT_EXPR:
2694     case POSTINCREMENT_EXPR:
2695     case PREDECREMENT_EXPR:
2696     case POSTDECREMENT_EXPR:
2697     case REALPART_EXPR:
2698     case IMAGPART_EXPR:
2699       return build_unary_op (code, arg1, candidates != 0);
2700
2701     case ARRAY_REF:
2702       return build_array_ref (arg1, arg2);
2703
2704     case COND_EXPR:
2705       return build_conditional_expr (arg1, arg2, arg3);
2706
2707     case MEMBER_REF:
2708       return build_m_component_ref
2709         (build_indirect_ref (arg1, NULL_PTR), arg2);
2710
2711       /* The caller will deal with these.  */
2712     case ADDR_EXPR:
2713     case COMPONENT_REF:
2714     case COMPOUND_EXPR:
2715       return NULL_TREE;
2716
2717     default:
2718       my_friendly_abort (367);
2719       return NULL_TREE;
2720     }
2721 }
2722
2723 /* Build up a call to operator new.  This has to be handled differently
2724    from other operators in the way lookup is handled; first members are
2725    considered, then globals.  CODE is either NEW_EXPR or VEC_NEW_EXPR.
2726    TYPE is the type to be created.  ARGS are any new-placement args.
2727    FLAGS are the usual overloading flags.  */
2728
2729 tree
2730 build_op_new_call (code, type, args, flags)
2731      enum tree_code code;
2732      tree type, args;
2733      int flags;
2734 {
2735   tree fnname = ansi_opname[code];
2736
2737   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2738       && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2739     {
2740       return build_method_call (build_dummy_object (type),
2741                                 fnname, args, NULL_TREE, flags);
2742     }
2743   else
2744     return build_new_function_call 
2745       (lookup_function_nonclass (fnname, args), args);
2746 }
2747
2748 /* Build a call to operator delete.  This has to be handled very specially,
2749    because the restrictions on what signatures match are different from all
2750    other call instances.  For a normal delete, only a delete taking (void *)
2751    or (void *, size_t) is accepted.  For a placement delete, only an exact
2752    match with the placement new is accepted.
2753
2754    CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2755    ADDR is the pointer to be deleted.  For placement delete, it is also
2756      used to determine what the corresponding new looked like.
2757    SIZE is the size of the memory block to be deleted.
2758    FLAGS are the usual overloading flags.
2759    PLACEMENT is the corresponding placement new call, or 0.  */
2760
2761 tree
2762 build_op_delete_call (code, addr, size, flags, placement)
2763      enum tree_code code;
2764      tree addr, size, placement;
2765      int flags;
2766 {
2767   tree fn, fns, fnname, fntype, argtypes, args, type;
2768
2769   if (addr == error_mark_node)
2770     return error_mark_node;
2771
2772   type = TREE_TYPE (TREE_TYPE (addr));
2773   fnname = ansi_opname[code];
2774
2775   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2776     /* In [class.free]
2777
2778        If the result of the lookup is ambiguous or inaccessible, or if
2779        the lookup selects a placement deallocation function, the
2780        program is ill-formed.
2781   
2782        Therefore, we ask lookup_fnfields to complain ambout ambiguity.  */
2783     {
2784       fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2785       if (fns == error_mark_node)
2786         return error_mark_node;
2787     }
2788   else
2789     fns = NULL_TREE;
2790
2791   if (fns == NULL_TREE)
2792     fns = lookup_name_nonclass (fnname);
2793
2794   if (placement)
2795     {
2796       /* placement is a CALL_EXPR around an ADDR_EXPR around a function.  */
2797
2798       /* Extract the function.  */
2799       argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
2800       /* Then the second parm type.  */
2801       argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2802
2803       /* Also the second argument.  */
2804       args = TREE_CHAIN (TREE_OPERAND (placement, 1));
2805     }
2806   else
2807     {
2808       /* First try it without the size argument.  */
2809       argtypes = void_list_node;
2810       args = NULL_TREE;
2811     }
2812
2813   argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2814   fntype = build_function_type (void_type_node, argtypes);
2815
2816   /* Strip const and volatile from addr.  */
2817   if (type != TYPE_MAIN_VARIANT (type))
2818     addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2819
2820   fn = instantiate_type (fntype, fns, 2);
2821
2822   if (fn != error_mark_node)
2823     {
2824       if (TREE_CODE (fns) == TREE_LIST)
2825         /* Member functions.  */
2826         enforce_access (TREE_PURPOSE (fns), fn);
2827       return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
2828     }
2829
2830   /* If we are doing placement delete we do nothing if we don't find a
2831      matching op delete.  */
2832   if (placement)
2833     return NULL_TREE;
2834
2835   /* Normal delete; now try to find a match including the size argument.  */
2836   argtypes = tree_cons (NULL_TREE, ptr_type_node,
2837                         tree_cons (NULL_TREE, sizetype, void_list_node));
2838   fntype = build_function_type (void_type_node, argtypes);
2839
2840   fn = instantiate_type (fntype, fns, 2);
2841
2842   if (fn != error_mark_node)
2843     {
2844       if (BASELINK_P (fns))
2845         /* Member functions.  */
2846         enforce_access (TREE_PURPOSE (fns), fn);
2847       return build_function_call
2848         (fn, expr_tree_cons (NULL_TREE, addr,
2849                              build_expr_list (NULL_TREE, size)));
2850     }
2851
2852   /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
2853      destructor, in which case the error should be deferred
2854      until someone actually tries to delete one of these.  */
2855   if (flags & LOOKUP_SPECULATIVELY)
2856     return NULL_TREE;
2857
2858   cp_error ("no suitable operator delete for `%T'", type);
2859   return error_mark_node;
2860 }
2861
2862 /* If the current scope isn't allowed to access DECL along
2863    BASETYPE_PATH, give an error.  The most derived class in
2864    BASETYPE_PATH is the one used to qualify DECL.  */
2865
2866 int
2867 enforce_access (basetype_path, decl)
2868      tree basetype_path;
2869      tree decl;
2870 {
2871   int accessible;
2872
2873   accessible = accessible_p (basetype_path, decl);
2874   if (!accessible)
2875     {
2876       if (TREE_PRIVATE (decl))
2877         cp_error_at ("`%+#D' is private", decl);
2878       else if (TREE_PROTECTED (decl))
2879         cp_error_at ("`%+#D' is protected", decl);
2880       else
2881         cp_error_at ("`%+#D' is inaccessible", decl);
2882       cp_error ("within this context");
2883       return 0;
2884     }
2885
2886   return 1;
2887 }
2888
2889 /* Perform the conversions in CONVS on the expression EXPR.  */
2890
2891 static tree
2892 convert_like (convs, expr)
2893      tree convs, expr;
2894 {
2895   if (ICS_BAD_FLAG (convs)
2896       && TREE_CODE (convs) != USER_CONV
2897       && TREE_CODE (convs) != AMBIG_CONV)
2898     {
2899       tree t = convs; 
2900       for (; t; t = TREE_OPERAND (t, 0))
2901         {
2902           if (TREE_CODE (t) == USER_CONV)
2903             {
2904               expr = convert_like (t, expr);
2905               break;
2906             }
2907           else if (TREE_CODE (t) == AMBIG_CONV)
2908             return convert_like (t, expr);
2909           else if (TREE_CODE (t) == IDENTITY_CONV)
2910             break;
2911         }
2912       return convert_for_initialization
2913         (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
2914          "conversion", NULL_TREE, 0);
2915     }
2916
2917   switch (TREE_CODE (convs))
2918     {
2919     case USER_CONV:
2920       {
2921         struct z_candidate *cand
2922           = WRAPPER_PTR (TREE_OPERAND (convs, 1));
2923         tree fn = cand->fn;
2924         tree args;
2925
2926         if (DECL_CONSTRUCTOR_P (fn))
2927           {
2928             tree t = build_int_2 (0, 0);
2929             TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
2930
2931             args = build_scratch_list (NULL_TREE, expr);
2932             if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
2933               args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2934             args = scratch_tree_cons (NULL_TREE, t, args);
2935           }
2936         else
2937           args = build_this (expr);
2938         expr = build_over_call (cand, args, LOOKUP_NORMAL);
2939
2940         /* If this is a constructor or a function returning an aggr type,
2941            we need to build up a TARGET_EXPR.  */
2942         if (DECL_CONSTRUCTOR_P (fn))
2943           expr = build_cplus_new (TREE_TYPE (convs), expr);
2944
2945         return expr;
2946       }
2947     case IDENTITY_CONV:
2948       if (type_unknown_p (expr))
2949         expr = instantiate_type (TREE_TYPE (convs), expr, 1);
2950       if (TREE_READONLY_DECL_P (expr))
2951         expr = decl_constant_value (expr);
2952       return expr;
2953     case AMBIG_CONV:
2954       /* Call build_user_type_conversion again for the error.  */
2955       return build_user_type_conversion
2956         (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
2957
2958     default:
2959       break;
2960     };
2961
2962   expr = convert_like (TREE_OPERAND (convs, 0), expr);
2963   if (expr == error_mark_node)
2964     return error_mark_node;
2965
2966   switch (TREE_CODE (convs))
2967     {
2968     case RVALUE_CONV:
2969       if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
2970         return expr;
2971       /* else fall through */
2972     case BASE_CONV:
2973       {
2974         tree cvt_expr = build_user_type_conversion
2975           (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
2976         if (!cvt_expr) 
2977           {
2978             /* This can occur if, for example, the EXPR has incomplete
2979                type.  We can't check for that before attempting the
2980                conversion because the type might be an incomplete
2981                array type, which is OK if some constructor for the
2982                destination type takes a pointer argument.  */
2983             if (TYPE_SIZE (TREE_TYPE (expr)) == 0)
2984               {
2985                 if (same_type_p (TREE_TYPE (expr), TREE_TYPE (convs)))
2986                   incomplete_type_error (expr, TREE_TYPE (expr));
2987                 else
2988                   cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
2989                             expr, TREE_TYPE (expr), TREE_TYPE (convs));
2990               }
2991             else
2992               cp_error ("could not convert `%E' to `%T'",
2993                         expr, TREE_TYPE (convs));
2994             return error_mark_node;
2995           }
2996         return cvt_expr;
2997       }
2998
2999     case REF_BIND:
3000       return convert_to_reference
3001         (TREE_TYPE (convs), expr,
3002          CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3003          error_mark_node);
3004     case LVALUE_CONV:
3005       return decay_conversion (expr);
3006
3007     case QUAL_CONV:
3008       /* Warn about deprecated conversion if appropriate.  */
3009       string_conv_p (TREE_TYPE (convs), expr, 1);
3010       break;
3011       
3012     default:
3013       break;
3014     }
3015   return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3016                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3017 }
3018
3019 /* ARG is being passed to a varargs function.  Perform any conversions
3020    required.  Return the converted value.  */
3021
3022 tree
3023 convert_arg_to_ellipsis (arg)
3024      tree arg;
3025 {
3026   if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3027       && (TYPE_PRECISION (TREE_TYPE (arg))
3028           < TYPE_PRECISION (double_type_node)))
3029     /* Convert `float' to `double'.  */
3030     arg = cp_convert (double_type_node, arg);
3031   else if (IS_AGGR_TYPE (TREE_TYPE (arg))
3032            && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (arg)))
3033     cp_warning ("cannot pass objects of type `%T' through `...'",
3034                 TREE_TYPE (arg));
3035   else
3036     /* Convert `short' and `char' to full-size `int'.  */
3037     arg = default_conversion (arg);
3038
3039   arg = require_complete_type (arg);
3040   
3041   return arg;
3042 }
3043
3044 /* ARG is a default argument expression being passed to a parameter of
3045    the indicated TYPE, which is a parameter to FN.  Do any required
3046    conversions.  Return the converted value.  */
3047
3048 tree
3049 convert_default_arg (type, arg, fn)
3050      tree type;
3051      tree arg;
3052      tree fn;
3053 {
3054   if (fn && DECL_TEMPLATE_INFO (fn))
3055     {
3056       /* This default argument came from a template.  Instantiate the
3057          default argument here, not in tsubst.  In the case of
3058          something like: 
3059
3060            template <class T>
3061            struct S {
3062              static T t();
3063              void f(T = t());
3064            };
3065
3066          we must be careful to do name lookup in the scope of S<T>,
3067          rather than in the current class.  */
3068       if (DECL_CLASS_SCOPE_P (fn))
3069         pushclass (DECL_REAL_CONTEXT (fn), 2);
3070
3071       arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE);
3072
3073       if (DECL_CLASS_SCOPE_P (fn))
3074         popclass ();
3075
3076       /* Make sure the default argument is reasonable.  */
3077       arg = check_default_argument (type, arg);
3078     }
3079
3080   arg = break_out_target_exprs (arg);
3081
3082   if (TREE_CODE (arg) == CONSTRUCTOR)
3083     {
3084       arg = digest_init (type, arg, 0);
3085       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3086                                         "default argument", 0, 0);
3087     }
3088   else
3089     {
3090       /* This could get clobbered by the following call.  */
3091       if (TREE_HAS_CONSTRUCTOR (arg))
3092         arg = copy_node (arg);
3093
3094       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3095                                         "default argument", 0, 0);
3096 #ifdef PROMOTE_PROTOTYPES
3097       if ((TREE_CODE (type) == INTEGER_TYPE
3098            || TREE_CODE (type) == ENUMERAL_TYPE)
3099           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3100         arg = default_conversion (arg);
3101 #endif
3102     }
3103
3104   return arg;
3105 }
3106
3107 static tree
3108 build_over_call (cand, args, flags)
3109      struct z_candidate *cand;
3110      tree args;
3111      int flags;
3112 {
3113   tree fn = cand->fn;
3114   tree convs = cand->convs;
3115   tree converted_args = NULL_TREE;
3116   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3117   tree conv, arg, val;
3118   int i = 0;
3119   int is_method = 0;
3120
3121   /* Give any warnings we noticed during overload resolution.  */
3122   if (cand->warnings)
3123     for (val = cand->warnings; val; val = TREE_CHAIN (val))
3124       joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3125
3126   if (DECL_FUNCTION_MEMBER_P (fn))
3127     enforce_access (cand->basetype_path, fn);
3128
3129   if (args && TREE_CODE (args) != TREE_LIST)
3130     args = build_scratch_list (NULL_TREE, args);
3131   arg = args;
3132
3133   /* The implicit parameters to a constructor are not considered by overload
3134      resolution, and must be of the proper type.  */
3135   if (DECL_CONSTRUCTOR_P (fn))
3136     {
3137       converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3138       arg = TREE_CHAIN (arg);
3139       parm = TREE_CHAIN (parm);
3140       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3141         {
3142           converted_args = expr_tree_cons
3143             (NULL_TREE, TREE_VALUE (arg), converted_args);
3144           arg = TREE_CHAIN (arg);
3145           parm = TREE_CHAIN (parm);
3146         }
3147     }      
3148   /* Bypass access control for 'this' parameter.  */
3149   else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3150     {
3151       tree parmtype = TREE_VALUE (parm);
3152       tree argtype = TREE_TYPE (TREE_VALUE (arg));
3153       tree t;
3154       if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3155         cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3156                     TREE_TYPE (argtype), fn);
3157
3158       /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3159          X is called for an object that is not of type X, or of a type
3160          derived from X, the behavior is undefined.
3161
3162          So we can assume that anything passed as 'this' is non-null, and
3163          optimize accordingly.  */
3164       if (TREE_CODE (parmtype) == POINTER_TYPE)
3165         t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
3166       else
3167         /* This happens with signatures.  */
3168         t = convert_force (parmtype, TREE_VALUE (arg), CONV_C_CAST);
3169       converted_args = expr_tree_cons (NULL_TREE, t, converted_args);
3170       parm = TREE_CHAIN (parm);
3171       arg = TREE_CHAIN (arg);
3172       ++i;
3173       is_method = 1;
3174     }
3175
3176   for (; arg && parm;
3177        parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3178     {
3179       tree type = TREE_VALUE (parm);
3180
3181       conv = TREE_VEC_ELT (convs, i);
3182       if (ICS_BAD_FLAG (conv))
3183         {
3184           tree t = conv;
3185           val = TREE_VALUE (arg);
3186
3187           for (; t; t = TREE_OPERAND (t, 0))
3188             {
3189               if (TREE_CODE (t) == USER_CONV
3190                   || TREE_CODE (t) == AMBIG_CONV)
3191                 {
3192                   val = convert_like (t, val);
3193                   break;
3194                 }
3195               else if (TREE_CODE (t) == IDENTITY_CONV)
3196                 break;
3197             }
3198           val = convert_for_initialization
3199             (NULL_TREE, type, val, LOOKUP_NORMAL,
3200              "argument passing", fn, i - is_method);
3201         }
3202       else
3203         {
3204           /* Issue warnings about peculiar, but legal, uses of NULL.  */
3205           if (ARITHMETIC_TYPE_P (TREE_VALUE (parm))
3206               && TREE_VALUE (arg) == null_node)
3207             cp_warning ("converting NULL to non-pointer type");
3208             
3209           val = convert_like (conv, TREE_VALUE (arg));
3210         }
3211
3212 #ifdef PROMOTE_PROTOTYPES
3213       if ((TREE_CODE (type) == INTEGER_TYPE
3214            || TREE_CODE (type) == ENUMERAL_TYPE)
3215           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3216         val = default_conversion (val);
3217 #endif
3218       converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3219     }
3220
3221   /* Default arguments */
3222   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3223     converted_args 
3224       = expr_tree_cons (NULL_TREE, 
3225                         convert_default_arg (TREE_VALUE (parm), 
3226                                              TREE_PURPOSE (parm),
3227                                              fn),
3228                         converted_args);
3229
3230   /* Ellipsis */
3231   for (; arg; arg = TREE_CHAIN (arg))
3232     converted_args 
3233       = expr_tree_cons (NULL_TREE,
3234                         convert_arg_to_ellipsis (TREE_VALUE (arg)),
3235                         converted_args);
3236
3237   converted_args = nreverse (converted_args);
3238
3239   if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
3240     check_function_format (DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
3241                            converted_args); 
3242
3243   /* Avoid actually calling copy constructors and copy assignment operators,
3244      if possible.  */
3245
3246   if (! flag_elide_constructors)
3247     /* Do things the hard way.  */;
3248   else if (DECL_CONSTRUCTOR_P (fn)
3249            && TREE_VEC_LENGTH (convs) == 1
3250       && copy_args_p (fn))
3251     {
3252       tree targ;
3253       arg = TREE_CHAIN (converted_args);
3254       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3255         arg = TREE_CHAIN (arg);
3256       arg = TREE_VALUE (arg);
3257
3258       /* Pull out the real argument, disregarding const-correctness.  */
3259       targ = arg;
3260       while (TREE_CODE (targ) == NOP_EXPR
3261              || TREE_CODE (targ) == NON_LVALUE_EXPR
3262              || TREE_CODE (targ) == CONVERT_EXPR)
3263         targ = TREE_OPERAND (targ, 0);
3264       if (TREE_CODE (targ) == ADDR_EXPR)
3265         {
3266           targ = TREE_OPERAND (targ, 0);
3267           if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3268                             TYPE_MAIN_VARIANT (TREE_TYPE (targ))))
3269             targ = NULL_TREE;
3270         }
3271       else
3272         targ = NULL_TREE;
3273
3274       if (targ)
3275         arg = targ;
3276       else
3277         arg = build_indirect_ref (arg, 0);
3278
3279       /* [class.copy]: the copy constructor is implicitly defined even if
3280          the implementation elided its use.  */
3281       if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3282         mark_used (fn);
3283
3284       /* If we're creating a temp and we already have one, don't create a
3285          new one.  If we're not creating a temp but we get one, use
3286          INIT_EXPR to collapse the temp into our target.  Otherwise, if the
3287          ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3288          temp or an INIT_EXPR otherwise.  */
3289       if (integer_zerop (TREE_VALUE (args)))
3290         {
3291           if (! real_lvalue_p (arg))
3292             return arg;
3293           else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3294             {
3295               val = build_decl (VAR_DECL, NULL_TREE, DECL_CONTEXT (fn));
3296               val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3297               TREE_SIDE_EFFECTS (val) = 1;
3298               return val;
3299             }
3300         }
3301       else if (! real_lvalue_p (arg)
3302                || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3303         {
3304           tree address;
3305           tree to = stabilize_reference
3306             (build_indirect_ref (TREE_VALUE (args), 0));
3307
3308           /* If we're initializing an empty class, then we actually
3309              have to use a MODIFY_EXPR rather than an INIT_EXPR.  The
3310              reason is that the dummy padding member in the target may
3311              not actually be allocated if TO is a base class
3312              subobject.  Since we've set TYPE_NONCOPIED_PARTS on the
3313              padding, a MODIFY_EXPR will preserve its value, which is
3314              the right thing to do if it's not really padding at all.
3315           
3316              It's not safe to just throw away the ARG if we're looking
3317              at an empty class because the ARG might contain a
3318              TARGET_EXPR which wants to be bound to TO.  If it is not,
3319              expand_expr will assign a dummy slot for the TARGET_EXPR,
3320              and we will call a destructor for it, which is wrong,
3321              because we will also destroy TO, but will never have
3322              constructed it.  */
3323           val = build (is_empty_class (DECL_CLASS_CONTEXT (fn))
3324                        ? MODIFY_EXPR : INIT_EXPR, 
3325                        DECL_CONTEXT (fn), to, arg);
3326           TREE_SIDE_EFFECTS (val) = 1;
3327           address = build_unary_op (ADDR_EXPR, val, 0);
3328           /* Avoid a warning about this expression, if the address is
3329              never used.  */
3330           TREE_USED (address) = 1;
3331           return address;
3332         }
3333     }
3334   else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3335            && copy_args_p (fn)
3336            && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3337     {
3338       tree to = stabilize_reference
3339         (build_indirect_ref (TREE_VALUE (converted_args), 0));
3340
3341       arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3342
3343       val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3344       TREE_SIDE_EFFECTS (val) = 1;
3345       return val;
3346     }
3347
3348   mark_used (fn);
3349
3350   if (DECL_CLASS_SCOPE_P (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3351     return build_signature_method_call (fn, converted_args);
3352   else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3353     {
3354       tree t, *p = &TREE_VALUE (converted_args);
3355       tree binfo = get_binfo
3356         (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3357       *p = convert_pointer_to_real (binfo, *p);
3358       if (TREE_SIDE_EFFECTS (*p))
3359         *p = save_expr (*p);
3360       t = build_pointer_type (TREE_TYPE (fn));
3361       fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3362       TREE_TYPE (fn) = t;
3363     }
3364   else if (DECL_INLINE (fn))
3365     fn = inline_conversion (fn);
3366   else
3367     fn = build_addr_func (fn);
3368
3369   /* Recognize certain built-in functions so we can make tree-codes
3370      other than CALL_EXPR.  We do this when it enables fold-const.c
3371      to do something useful.  */
3372
3373   if (TREE_CODE (fn) == ADDR_EXPR
3374       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3375       && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3376     switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3377       {
3378       case BUILT_IN_ABS:
3379       case BUILT_IN_LABS:
3380       case BUILT_IN_FABS:
3381         if (converted_args == 0)
3382           return integer_zero_node;
3383         return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3384       default:
3385         break;
3386       }
3387
3388   fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3389   if (TREE_CODE (TREE_TYPE (fn)) == VOID_TYPE)
3390     return fn;
3391   fn = require_complete_type (fn);
3392   if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3393     fn = build_cplus_new (TREE_TYPE (fn), fn);
3394   return convert_from_reference (fn);
3395 }
3396
3397 static tree
3398 build_new_method_call (instance, name, args, basetype_path, flags)
3399      tree instance, name, args, basetype_path;
3400      int flags;
3401 {
3402   struct z_candidate *candidates = 0, *cand;
3403   tree explicit_targs = NULL_TREE;
3404   tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3405   tree pretty_name;
3406   tree user_args = args;
3407   tree templates = NULL_TREE;
3408   int template_only = 0;
3409
3410   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3411     {
3412       explicit_targs = TREE_OPERAND (name, 1);
3413       name = TREE_OPERAND (name, 0);
3414       if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
3415         name = DECL_NAME (name);
3416       else 
3417         {
3418           if (TREE_CODE (name) == COMPONENT_REF)
3419             name = TREE_OPERAND (name, 1);
3420           if (TREE_CODE (name) == OVERLOAD)
3421             name = DECL_NAME (OVL_CURRENT (name));
3422         }
3423
3424       template_only = 1;
3425     }
3426
3427   /* If there is an extra argument for controlling virtual bases,
3428      remove it for error reporting.  */
3429   if (flags & LOOKUP_HAS_IN_CHARGE)
3430     user_args = TREE_CHAIN (args);
3431
3432   args = resolve_args (args);
3433
3434   if (args == error_mark_node)
3435     return error_mark_node;
3436
3437   if (instance == NULL_TREE)
3438     basetype = BINFO_TYPE (basetype_path);
3439   else
3440     {
3441       if (TREE_CODE (instance) == OFFSET_REF)
3442         instance = resolve_offset_ref (instance);
3443       if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3444         instance = convert_from_reference (instance);
3445       basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
3446
3447       /* XXX this should be handled before we get here.  */
3448       if (! IS_AGGR_TYPE (basetype)
3449           && ! (TYPE_LANG_SPECIFIC (basetype)
3450                 && (IS_SIGNATURE_POINTER (basetype)
3451                     || IS_SIGNATURE_REFERENCE (basetype))))
3452         {
3453           if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3454             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3455                       name, instance, basetype);
3456
3457           return error_mark_node;
3458         }
3459
3460       /* If `instance' is a signature pointer/reference and `name' is
3461          not a constructor, we are calling a signature member function.
3462          In that case set the `basetype' to the signature type.  */
3463       if ((IS_SIGNATURE_POINTER (basetype)
3464            || IS_SIGNATURE_REFERENCE (basetype))
3465           && TYPE_IDENTIFIER (basetype) != name)
3466         basetype = SIGNATURE_TYPE (basetype);
3467     }
3468
3469   if (basetype_path == NULL_TREE)
3470     basetype_path = TYPE_BINFO (basetype);
3471
3472   if (instance)
3473     {
3474       instance_ptr = build_this (instance);
3475
3476       if (! template_only)
3477         {
3478           /* XXX this should be handled before we get here.  */
3479           fns = build_field_call (basetype_path, instance_ptr, name, args);
3480           if (fns)
3481             return fns;
3482         }
3483     }
3484   else
3485     {
3486       instance_ptr = build_int_2 (0, 0);
3487       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3488     }
3489
3490   pretty_name
3491     = (name == ctor_identifier ? constructor_name (basetype) : name);
3492
3493   fns = lookup_fnfields (basetype_path, name, 1);
3494
3495   if (fns == error_mark_node)
3496     return error_mark_node;
3497   if (fns)
3498     {
3499       tree fn = TREE_VALUE (fns);
3500       if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3501           && ! (flags & LOOKUP_HAS_IN_CHARGE))
3502         {
3503           flags |= LOOKUP_HAS_IN_CHARGE;
3504           args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3505         }
3506       mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3507       for (; fn; fn = OVL_NEXT (fn))
3508         {
3509           tree t = OVL_CURRENT (fn);
3510           tree this_arglist;
3511
3512           /* We can end up here for copy-init of same or base class.  */
3513           if (name == ctor_identifier
3514               && (flags & LOOKUP_ONLYCONVERTING)
3515               && DECL_NONCONVERTING_P (t))
3516             continue;
3517           if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3518             this_arglist = mem_args;
3519           else
3520             this_arglist = args;
3521
3522           if (TREE_CODE (t) == TEMPLATE_DECL)
3523             {
3524               /* A member template. */
3525               templates = scratch_tree_cons (NULL_TREE, t, templates);
3526               candidates = 
3527                 add_template_candidate (candidates, t, explicit_targs,
3528                                         this_arglist,
3529                                         TREE_TYPE (name), flags, DEDUCE_CALL); 
3530             }
3531           else if (! template_only)
3532             candidates = add_function_candidate (candidates, t,
3533                                                  this_arglist, flags);
3534
3535           if (candidates)
3536             candidates->basetype_path = TREE_PURPOSE (fns);
3537         }
3538     }
3539
3540   if (! any_viable (candidates))
3541     {
3542       /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
3543       if (flags & LOOKUP_SPECULATIVELY)
3544         return NULL_TREE;
3545       if (TYPE_SIZE (basetype) == 0)
3546         incomplete_type_error (instance_ptr, basetype);
3547       else
3548         cp_error ("no matching function for call to `%T::%D (%A)%V'",
3549                   basetype, pretty_name, user_args,
3550                   TREE_TYPE (TREE_TYPE (instance_ptr)));
3551       print_z_candidates (candidates);
3552       return error_mark_node;
3553     }
3554   candidates = splice_viable (candidates);
3555   cand = tourney (candidates);
3556
3557   if (cand == 0)
3558     {
3559       cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3560                 user_args);
3561       print_z_candidates (candidates);
3562       return error_mark_node;
3563     }
3564
3565   if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3566       && instance == current_class_ref
3567       && DECL_CONSTRUCTOR_P (current_function_decl)
3568       && ! (flags & LOOKUP_NONVIRTUAL)
3569       && value_member (cand->fn, CLASSTYPE_ABSTRACT_VIRTUALS (basetype)))
3570     cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3571   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3572       && is_dummy_object (instance_ptr))
3573     {
3574       cp_error ("cannot call member function `%D' without object", cand->fn);
3575       return error_mark_node;
3576     }
3577
3578   if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3579       && ((instance == current_class_ref && (dtor_label || ctor_label))
3580           || resolves_to_fixed_type_p (instance, 0)))
3581     flags |= LOOKUP_NONVIRTUAL;
3582
3583   /* Pedantically, normal function declarations are never considered
3584      to refer to template instantiations, so we only do this with
3585      -fguiding-decls.  */ 
3586   if (flag_guiding_decls && templates && ! cand->template 
3587       && ! DECL_INITIAL (cand->fn))
3588     add_maybe_template (cand->fn, templates);
3589
3590   return build_over_call
3591     (cand,
3592      TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3593      flags);
3594 }
3595
3596 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
3597    subsequence of ICS2.  */
3598
3599 static int
3600 is_subseq (ics1, ics2)
3601      tree ics1, ics2;
3602 {
3603   /* We can assume that a conversion of the same code
3604      between the same types indicates a subsequence since we only get
3605      here if the types we are converting from are the same.  */
3606
3607   while (TREE_CODE (ics1) == RVALUE_CONV
3608          || TREE_CODE (ics1) == LVALUE_CONV)
3609     ics1 = TREE_OPERAND (ics1, 0);
3610
3611   while (1)
3612     {
3613       while (TREE_CODE (ics2) == RVALUE_CONV
3614           || TREE_CODE (ics2) == LVALUE_CONV)
3615         ics2 = TREE_OPERAND (ics2, 0);
3616
3617       if (TREE_CODE (ics2) == USER_CONV
3618           || TREE_CODE (ics2) == AMBIG_CONV
3619           || TREE_CODE (ics2) == IDENTITY_CONV)
3620         /* At this point, ICS1 cannot be a proper subsequence of
3621            ICS2.  We can get a USER_CONV when we are comparing the
3622            second standard conversion sequence of two user conversion
3623            sequences.  */
3624         return 0;
3625
3626       ics2 = TREE_OPERAND (ics2, 0);
3627
3628       if (TREE_CODE (ics2) == TREE_CODE (ics1)
3629           && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
3630           && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3631                              TREE_TYPE (TREE_OPERAND (ics1, 0))))
3632         return 1;
3633     }
3634 }
3635
3636 /* Returns non-zero iff DERIVED is derived from BASE.  The inputs may
3637    be any _TYPE nodes.  */
3638
3639 int
3640 is_properly_derived_from (derived, base)
3641      tree derived;
3642      tree base;
3643 {
3644   if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
3645       || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
3646     return 0;
3647
3648   /* We only allow proper derivation here.  The DERIVED_FROM_P macro
3649      considers every class derived from itself.  */
3650   return (!same_type_p (TYPE_MAIN_VARIANT (derived),
3651                         TYPE_MAIN_VARIANT (base))
3652           && DERIVED_FROM_P (base, derived));
3653 }
3654
3655 /* We build the ICS for an implicit object parameter as a pointer
3656    conversion sequence.  However, such a sequence should be compared
3657    as if it were a reference conversion sequence.  If ICS is the
3658    implicit conversion sequence for an implicit object parameter,
3659    modify it accordingly.  */
3660
3661 static void
3662 maybe_handle_implicit_object (ics)
3663      tree* ics;
3664 {
3665   if (ICS_THIS_FLAG (*ics))
3666     {
3667       /* [over.match.funcs]
3668          
3669          For non-static member functions, the type of the
3670          implicit object parameter is "reference to cv X"
3671          where X is the class of which the function is a
3672          member and cv is the cv-qualification on the member
3673          function declaration.  */
3674       tree t = *ics;
3675       if (TREE_CODE (t) == QUAL_CONV)
3676         t = TREE_OPERAND (t, 0);
3677       if (TREE_CODE (t) == PTR_CONV)
3678         t = TREE_OPERAND (t, 0);
3679       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3680       t = build_conv (REF_BIND, 
3681                       build_reference_type (TREE_TYPE (TREE_TYPE (*ics))), 
3682                       t);
3683       ICS_STD_RANK (t) = ICS_STD_RANK (*ics);
3684       *ics = t;
3685     }
3686 }
3687
3688 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
3689    to the type the reference originally referred to, and return 1.
3690    Otherwise, return 0.  */
3691
3692 static int
3693 maybe_handle_ref_bind (ics, target_type)
3694      tree* ics;
3695      tree* target_type;
3696 {
3697   if (TREE_CODE (*ics) == REF_BIND)
3698     {
3699       /* [over.ics.rank] 
3700          
3701          When a parameter of reference type binds directly
3702          (_dcl.init.ref_) to an argument expression, the implicit
3703          conversion sequence is the identity conversion, unless the
3704          argument expression has a type that is a derived class of the
3705          parameter type, in which case the implicit conversion
3706          sequence is a derived-to-base Conversion.
3707          
3708          If the parameter binds directly to the result of applying a
3709          conversion function to the argument expression, the implicit
3710          conversion sequence is a user-defined conversion sequence
3711          (_over.ics.user_), with the second standard conversion
3712          sequence either an identity conversion or, if the conversion
3713          function returns an entity of a type that is a derived class
3714          of the parameter type, a derived-to-base Conversion.
3715          
3716          When a parameter of reference type is not bound directly to
3717          an argument expression, the conversion sequence is the one
3718          required to convert the argument expression to the underlying
3719          type of the reference according to _over.best.ics_.
3720          Conceptually, this conversion sequence corresponds to
3721          copy-initializing a temporary of the underlying type with the
3722          argument expression.  Any difference in top-level
3723          cv-qualification is subsumed by the initialization itself and
3724          does not constitute a conversion.  */
3725
3726       tree old_ics = *ics;
3727
3728       *target_type = TREE_TYPE (TREE_TYPE (*ics));
3729       *ics = TREE_OPERAND (*ics, 0);
3730       if (TREE_CODE (*ics) == IDENTITY_CONV
3731           && is_properly_derived_from (TREE_TYPE (*ics), *target_type))
3732         *ics = build_conv (BASE_CONV, *target_type, *ics);
3733       ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
3734       ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
3735       
3736       return 1;
3737     }
3738   
3739   return 0;
3740 }
3741
3742 /* Compare two implicit conversion sequences according to the rules set out in
3743    [over.ics.rank].  Return values:
3744
3745       1: ics1 is better than ics2
3746      -1: ics2 is better than ics1
3747       0: ics1 and ics2 are indistinguishable */
3748
3749 static int
3750 compare_ics (ics1, ics2)
3751      tree ics1, ics2;
3752 {
3753   tree from_type1;
3754   tree from_type2;
3755   tree to_type1;
3756   tree to_type2;
3757   tree deref_from_type1 = NULL_TREE;
3758   tree deref_from_type2 = NULL_TREE;
3759   tree deref_to_type1 = NULL_TREE;
3760   tree deref_to_type2 = NULL_TREE;
3761
3762   /* REF_BINDING is non-zero if the result of the conversion sequence
3763      is a reference type.   In that case TARGET_TYPE is the
3764      type referred to by the reference.  */
3765   int ref_binding1;
3766   int ref_binding2;
3767   tree target_type1;
3768   tree target_type2;
3769
3770   /* Handle implicit object parameters.  */
3771   maybe_handle_implicit_object (&ics1);
3772   maybe_handle_implicit_object (&ics2);
3773
3774   /* Handle reference parameters.  */
3775   ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
3776   ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
3777
3778   /* [over.ics.rank]
3779
3780      When  comparing  the  basic forms of implicit conversion sequences (as
3781      defined in _over.best.ics_)
3782
3783      --a standard conversion sequence (_over.ics.scs_) is a better
3784        conversion sequence than a user-defined conversion sequence
3785        or an ellipsis conversion sequence, and
3786      
3787      --a user-defined conversion sequence (_over.ics.user_) is a
3788        better conversion sequence than an ellipsis conversion sequence
3789        (_over.ics.ellipsis_).  */
3790   if (ICS_RANK (ics1) > ICS_RANK (ics2))
3791     return -1;
3792   else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3793     return 1;
3794
3795   if (ICS_RANK (ics1) == BAD_RANK)
3796     {
3797       /* Both ICS are bad.  We try to make a decision based on what
3798          would have happenned if they'd been good.  */
3799       if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3800           || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3801         return -1;
3802       else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3803                || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3804         return 1;
3805
3806       /* We couldn't make up our minds; try to figure it out below.  */
3807     }
3808
3809   if (ICS_ELLIPSIS_FLAG (ics1))
3810     /* Both conversions are ellipsis conversions.  */
3811     return 0;
3812
3813   /* User-defined  conversion sequence U1 is a better conversion sequence
3814      than another user-defined conversion sequence U2 if they contain the
3815      same user-defined conversion operator or constructor and if the sec-
3816      ond standard conversion sequence of U1 is  better  than  the  second
3817      standard conversion sequence of U2.  */
3818
3819   if (ICS_USER_FLAG (ics1))
3820     {
3821       tree t1, t2;
3822
3823       for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3824         if (TREE_CODE (t1) == AMBIG_CONV)
3825           return 0;
3826       for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3827         if (TREE_CODE (t2) == AMBIG_CONV)
3828           return 0;
3829
3830       if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3831         return 0;
3832
3833       /* We can just fall through here, after setting up
3834          FROM_TYPE1 and FROM_TYPE2.  */
3835       from_type1 = TREE_TYPE (t1);
3836       from_type2 = TREE_TYPE (t2);
3837     }
3838   else
3839     {
3840       /* We're dealing with two standard conversion sequences. 
3841
3842          [over.ics.rank]
3843          
3844          Standard conversion sequence S1 is a better conversion
3845          sequence than standard conversion sequence S2 if
3846      
3847          --S1 is a proper subsequence of S2 (comparing the conversion
3848            sequences in the canonical form defined by _over.ics.scs_,
3849            excluding any Lvalue Transformation; the identity
3850            conversion sequence is considered to be a subsequence of
3851            any non-identity conversion sequence */
3852       
3853       from_type1 = ics1;
3854       while (TREE_CODE (from_type1) != IDENTITY_CONV)
3855         from_type1 = TREE_OPERAND (from_type1, 0);
3856       from_type1 = TREE_TYPE (from_type1);
3857       
3858       from_type2 = ics2;
3859       while (TREE_CODE (from_type2) != IDENTITY_CONV)
3860         from_type2 = TREE_OPERAND (from_type2, 0);
3861       from_type2 = TREE_TYPE (from_type2);
3862     }
3863
3864   if (same_type_p (from_type1, from_type2))
3865     {
3866       if (is_subseq (ics1, ics2))
3867         return 1;
3868       if (is_subseq (ics2, ics1))
3869         return -1;
3870     }
3871   /* Otherwise, one sequence cannot be a subsequence of the other; they
3872      don't start with the same type.  This can happen when comparing the
3873      second standard conversion sequence in two user-defined conversion
3874      sequences.  */
3875
3876   /* [over.ics.rank]
3877
3878      Or, if not that,
3879
3880      --the rank of S1 is better than the rank of S2 (by the rules
3881        defined below):
3882
3883     Standard conversion sequences are ordered by their ranks: an Exact
3884     Match is a better conversion than a Promotion, which is a better
3885     conversion than a Conversion.
3886
3887     Two conversion sequences with the same rank are indistinguishable
3888     unless one of the following rules applies:
3889
3890     --A conversion that is not a conversion of a pointer, or pointer
3891       to member, to bool is better than another conversion that is such
3892       a conversion.  
3893
3894     The ICS_STD_RANK automatically handles the pointer-to-bool rule,
3895     so that we do not have to check it explicitly.  */
3896   if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3897     return 1;
3898   else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
3899     return -1;
3900
3901   to_type1 = TREE_TYPE (ics1);
3902   to_type2 = TREE_TYPE (ics2);
3903
3904   if (TYPE_PTR_P (from_type1)
3905       && TYPE_PTR_P (from_type2)
3906       && TYPE_PTR_P (to_type1)
3907       && TYPE_PTR_P (to_type2))
3908     {
3909       deref_from_type1 = TREE_TYPE (from_type1);
3910       deref_from_type2 = TREE_TYPE (from_type2);
3911       deref_to_type1 = TREE_TYPE (to_type1);
3912       deref_to_type2 = TREE_TYPE (to_type2);
3913     }
3914   /* The rules for pointers to members A::* are just like the rules
3915      for pointers A*, except opposite: if B is derived from A then
3916      A::* converts to B::*, not vice versa.  For that reason, we
3917      switch the from_ and to_ variables here.  */
3918   else if (TYPE_PTRMEM_P (from_type1)
3919            && TYPE_PTRMEM_P (from_type2)
3920            && TYPE_PTRMEM_P (to_type1)
3921            && TYPE_PTRMEM_P (to_type2))
3922     {
3923       deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
3924       deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
3925       deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
3926       deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
3927     }
3928   else if (TYPE_PTRMEMFUNC_P (from_type1)
3929            && TYPE_PTRMEMFUNC_P (from_type2)
3930            && TYPE_PTRMEMFUNC_P (to_type1)
3931            && TYPE_PTRMEMFUNC_P (to_type2))
3932     {
3933       deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
3934       deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
3935       deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
3936       deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
3937     }
3938
3939   if (deref_from_type1 != NULL_TREE
3940       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
3941       && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
3942     {
3943       /* This was one of the pointer or pointer-like conversions.  
3944
3945          [over.ics.rank]
3946          
3947          --If class B is derived directly or indirectly from class A,
3948            conversion of B* to A* is better than conversion of B* to
3949            void*, and conversion of A* to void* is better than
3950            conversion of B* to void*.  */
3951       if (TREE_CODE (deref_to_type1) == VOID_TYPE
3952           && TREE_CODE (deref_to_type2) == VOID_TYPE)
3953         {
3954           if (is_properly_derived_from (deref_from_type1,
3955                                         deref_from_type2))
3956             return -1;
3957           else if (is_properly_derived_from (deref_from_type2,
3958                                              deref_from_type1))
3959             return 1;
3960         }
3961       else if (TREE_CODE (deref_to_type1) == VOID_TYPE
3962                || TREE_CODE (deref_to_type2) == VOID_TYPE)
3963         {
3964           if (same_type_p (deref_from_type1, deref_from_type2))
3965             {
3966               if (TREE_CODE (deref_to_type2) == VOID_TYPE)
3967                 {
3968                   if (is_properly_derived_from (deref_from_type1,
3969                                                 deref_to_type1))
3970                     return 1;
3971                 }
3972               /* We know that DEREF_TO_TYPE1 is `void' here.  */
3973               else if (is_properly_derived_from (deref_from_type1,
3974                                                  deref_to_type2))
3975                 return -1;
3976             }
3977         }
3978       else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
3979                && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
3980         {
3981           /* [over.ics.rank]
3982
3983              --If class B is derived directly or indirectly from class A
3984                and class C is derived directly or indirectly from B,
3985              
3986              --conversion of C* to B* is better than conversion of C* to
3987                A*, 
3988              
3989              --conversion of B* to A* is better than conversion of C* to
3990                A*  */
3991           if (same_type_p (deref_from_type1, deref_from_type2))
3992             {
3993               if (is_properly_derived_from (deref_to_type1,
3994                                             deref_to_type2))
3995                 return 1;
3996               else if (is_properly_derived_from (deref_to_type2,
3997                                                  deref_to_type1))
3998                 return -1;
3999             }
4000           else if (same_type_p (deref_to_type1, deref_to_type2))
4001             {
4002               if (is_properly_derived_from (deref_from_type2,
4003                                             deref_from_type1))
4004                 return 1;
4005               else if (is_properly_derived_from (deref_from_type1,
4006                                                  deref_from_type2))
4007                 return -1;
4008             }
4009         }
4010     }
4011   else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
4012            && same_type_p (from_type1, from_type2))
4013     {
4014       /* [over.ics.rank]
4015          
4016          --binding of an expression of type C to a reference of type
4017            B& is better than binding an expression of type C to a
4018            reference of type A&
4019
4020          --conversion of C to B is better than conversion of C to A,  */
4021       if (is_properly_derived_from (from_type1, to_type1)
4022           && is_properly_derived_from (from_type1, to_type2))
4023         {
4024           if (is_properly_derived_from (to_type1, to_type2))
4025             return 1;
4026           else if (is_properly_derived_from (to_type2, to_type1))
4027             return -1;
4028         }
4029     }
4030   else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
4031            && same_type_p (to_type1, to_type2))
4032     {
4033       /* [over.ics.rank]
4034
4035          --binding of an expression of type B to a reference of type
4036            A& is better than binding an expression of type C to a
4037            reference of type A&, 
4038
4039          --onversion of B to A is better than conversion of C to A  */
4040       if (is_properly_derived_from (from_type1, to_type1)
4041           && is_properly_derived_from (from_type2, to_type1))
4042         {
4043           if (is_properly_derived_from (from_type2, from_type1))
4044             return 1;
4045           else if (is_properly_derived_from (from_type1, from_type2))
4046             return -1;
4047         }
4048     }
4049
4050   /* [over.ics.rank]
4051
4052      --S1 and S2 differ only in their qualification conversion and  yield
4053        similar  types  T1 and T2 (_conv.qual_), respectively, and the cv-
4054        qualification signature of type T1 is a proper subset of  the  cv-
4055        qualification signature of type T2  */
4056   if (TREE_CODE (ics1) == QUAL_CONV 
4057       && TREE_CODE (ics2) == QUAL_CONV
4058       && same_type_p (from_type1, from_type2))
4059     return comp_cv_qual_signature (to_type1, to_type2);
4060
4061   /* [over.ics.rank]
4062      
4063      --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4064      types to which the references refer are the same type except for
4065      top-level cv-qualifiers, and the type to which the reference
4066      initialized by S2 refers is more cv-qualified than the type to
4067      which the reference initialized by S1 refers */
4068       
4069   if (ref_binding1 && ref_binding2
4070       && same_type_p (TYPE_MAIN_VARIANT (to_type1),
4071                       TYPE_MAIN_VARIANT (to_type2)))
4072     return comp_cv_qualification (target_type2, target_type1);
4073
4074   /* Neither conversion sequence is better than the other.  */
4075   return 0;
4076 }
4077
4078 /* The source type for this standard conversion sequence.  */
4079
4080 static tree
4081 source_type (t)
4082      tree t;
4083 {
4084   for (;; t = TREE_OPERAND (t, 0))
4085     {
4086       if (TREE_CODE (t) == USER_CONV
4087           || TREE_CODE (t) == AMBIG_CONV
4088           || TREE_CODE (t) == IDENTITY_CONV)
4089         return TREE_TYPE (t);
4090     }
4091   my_friendly_abort (1823);
4092 }
4093
4094 /* Note a warning about preferring WINNER to LOSER.  We do this by storing
4095    a pointer to LOSER and re-running joust to produce the warning if WINNER
4096    is actually used.  */
4097
4098 static void
4099 add_warning (winner, loser)
4100      struct z_candidate *winner, *loser;
4101 {
4102   winner->warnings = expr_tree_cons (NULL_PTR,
4103                                      build_expr_ptr_wrapper (loser),
4104                                      winner->warnings);
4105 }
4106
4107 /* Compare two candidates for overloading as described in
4108    [over.match.best].  Return values:
4109
4110       1: cand1 is better than cand2
4111      -1: cand2 is better than cand1
4112       0: cand1 and cand2 are indistinguishable */
4113
4114 static int
4115 joust (cand1, cand2, warn)
4116      struct z_candidate *cand1, *cand2;
4117      int warn;
4118 {
4119   int winner = 0;
4120   int i, off1 = 0, off2 = 0, len;
4121
4122   /* Candidates that involve bad conversions are always worse than those
4123      that don't.  */
4124   if (cand1->viable > cand2->viable)
4125     return 1;
4126   if (cand1->viable < cand2->viable)
4127     return -1;
4128
4129   /* If we have two pseudo-candidates for conversions to the same type,
4130      arbitrarily pick one.  */
4131   if (TYPE_P (cand1->fn) && cand1->fn == cand2->fn)
4132     return 1;
4133
4134   /* a viable function F1
4135      is defined to be a better function than another viable function F2  if
4136      for  all arguments i, ICSi(F1) is not a worse conversion sequence than
4137      ICSi(F2), and then */
4138
4139   /* for some argument j, ICSj(F1) is a better conversion  sequence  than
4140      ICSj(F2) */
4141
4142   /* For comparing static and non-static member functions, we ignore the
4143      implicit object parameter of the non-static function.  The WP says to
4144      pretend that the static function has an object parm, but that won't
4145      work with operator overloading.  */
4146   len = TREE_VEC_LENGTH (cand1->convs);
4147   if (len != TREE_VEC_LENGTH (cand2->convs))
4148     {
4149       if (DECL_STATIC_FUNCTION_P (cand1->fn)
4150           && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4151         off2 = 1;
4152       else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4153                && DECL_STATIC_FUNCTION_P (cand2->fn))
4154         {
4155           off1 = 1;
4156           --len;
4157         }
4158       else
4159         my_friendly_abort (42);
4160     }
4161
4162   for (i = 0; i < len; ++i)
4163     {
4164       tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4165       tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4166       int comp = compare_ics (t1, t2);
4167
4168       if (comp != 0)
4169         {
4170           if (warn_sign_promo
4171               && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4172               && TREE_CODE (t1) == STD_CONV
4173               && TREE_CODE (t2) == STD_CONV
4174               && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4175               && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4176               && (TYPE_PRECISION (TREE_TYPE (t1))
4177                   == TYPE_PRECISION (TREE_TYPE (t2)))
4178               && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4179                   || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4180                       == ENUMERAL_TYPE)))
4181             {
4182               tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4183               tree type1, type2;
4184               struct z_candidate *w, *l;
4185               if (comp > 0)
4186                 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4187                   w = cand1, l = cand2;
4188               else
4189                 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4190                   w = cand2, l = cand1;
4191
4192               if (warn)
4193                 {
4194                   cp_warning ("passing `%T' chooses `%T' over `%T'",
4195                               type, type1, type2);
4196                   cp_warning ("  in call to `%D'", w->fn);
4197                 }
4198               else
4199                 add_warning (w, l);
4200             }
4201
4202           if (winner && comp != winner)
4203             {
4204               winner = 0;
4205               goto tweak;
4206             }
4207           winner = comp;
4208         }
4209     }
4210
4211   /* warn about confusing overload resolution for user-defined conversions,
4212      either between a constructor and a conversion op, or between two
4213      conversion ops.  */
4214   if (winner && cand1->second_conv
4215       && ((DECL_CONSTRUCTOR_P (cand1->fn)
4216            != DECL_CONSTRUCTOR_P (cand2->fn))
4217           /* Don't warn if the two conv ops convert to the same type...  */
4218           || (! DECL_CONSTRUCTOR_P (cand1->fn)
4219               && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
4220                                 TREE_TYPE (TREE_TYPE (cand2->fn))))))
4221     {
4222       int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4223       if (comp != winner)
4224         {
4225           struct z_candidate *w, *l;
4226           if (winner == 1)
4227             w = cand1, l = cand2;
4228           else
4229             w = cand2, l = cand1;
4230           if (warn)
4231             {
4232               tree source = source_type (TREE_VEC_ELT (w->convs, 0));
4233               if (! DECL_CONSTRUCTOR_P (w->fn))
4234                 source = TREE_TYPE (source);
4235               cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4236               cp_warning ("  for conversion from `%T' to `%T'",
4237                           source, TREE_TYPE (w->second_conv));
4238               cp_warning ("  because conversion sequence for the argument is better");
4239             }
4240           else
4241             add_warning (w, l);
4242         }
4243     }
4244
4245   if (winner)
4246     return winner;
4247
4248   /* or, if not that,
4249      F1 is a non-template function and F2 is a template function */
4250
4251   if (! cand1->template && cand2->template)
4252     return 1;
4253   else if (cand1->template && ! cand2->template)
4254     return -1;
4255   else if (cand1->template && cand2->template)
4256     winner = more_specialized
4257       (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4258        NULL_TREE);
4259
4260   /* or, if not that,
4261      the  context  is  an  initialization by user-defined conversion (see
4262      _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
4263      sequence  from  the return type of F1 to the destination type (i.e.,
4264      the type of the entity being initialized)  is  a  better  conversion
4265      sequence  than the standard conversion sequence from the return type
4266      of F2 to the destination type.  */
4267
4268   if (! winner && cand1->second_conv)
4269     winner = compare_ics (cand1->second_conv, cand2->second_conv);
4270
4271   /* If the built-in candidates are the same, arbitrarily pick one.  */
4272   if (! winner && cand1->fn == cand2->fn
4273       && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4274     {
4275       for (i = 0; i < len; ++i)
4276         if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4277                           TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
4278           break;
4279       if (i == TREE_VEC_LENGTH (cand1->convs))
4280         return 1;
4281
4282       /* Kludge around broken overloading rules whereby
4283          Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4284          that takes references and another that takes values.  */
4285       if (cand1->fn == ansi_opname[COND_EXPR])
4286         {
4287           tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4288           tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4289           tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4290           tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4291
4292           if (same_type_p (t1, t2))
4293             {
4294               if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4295                 return 1;
4296               if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4297                 return -1;
4298             }
4299         }
4300     }
4301
4302 tweak:
4303
4304   /* Extension: If the worst conversion for one candidate is worse than the
4305      worst conversion for the other, take the first.  */
4306   if (! winner && ! pedantic)
4307     {
4308       int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4309
4310       for (i = 0; i < len; ++i)
4311         {
4312           if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4313             rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4314           if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4315             rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4316         }
4317
4318       if (rank1 < rank2)
4319         return 1;
4320       if (rank1 > rank2)
4321         return -1;
4322     }
4323
4324   return winner;
4325 }
4326
4327 /* Given a list of candidates for overloading, find the best one, if any.
4328    This algorithm has a worst case of O(2n) (winner is last), and a best
4329    case of O(n/2) (totally ambiguous); much better than a sorting
4330    algorithm.  */
4331
4332 static struct z_candidate *
4333 tourney (candidates)
4334      struct z_candidate *candidates;
4335 {
4336   struct z_candidate *champ = candidates, *challenger;
4337   int fate;
4338   int champ_compared_to_predecessor = 0;
4339
4340   /* Walk through the list once, comparing each current champ to the next
4341      candidate, knocking out a candidate or two with each comparison.  */
4342
4343   for (challenger = champ->next; challenger; )
4344     {
4345       fate = joust (champ, challenger, 0);
4346       if (fate == 1)
4347         challenger = challenger->next;
4348       else
4349         {
4350           if (fate == 0)
4351             {
4352               champ = challenger->next;
4353               if (champ == 0)
4354                 return 0;
4355               champ_compared_to_predecessor = 0;
4356             }
4357           else
4358             {
4359               champ = challenger;
4360               champ_compared_to_predecessor = 1;
4361             }
4362
4363           challenger = champ->next;
4364         }
4365     }
4366
4367   /* Make sure the champ is better than all the candidates it hasn't yet
4368      been compared to.  */
4369
4370   for (challenger = candidates; 
4371        challenger != champ 
4372          && !(champ_compared_to_predecessor && challenger->next == champ);
4373        challenger = challenger->next)
4374     {
4375       fate = joust (champ, challenger, 0);
4376       if (fate != 1)
4377         return 0;
4378     }
4379
4380   return champ;
4381 }
4382
4383 int
4384 can_convert (to, from)
4385      tree to, from;
4386 {
4387   tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4388   return (t && ! ICS_BAD_FLAG (t));
4389 }
4390
4391 int
4392 can_convert_arg (to, from, arg)
4393      tree to, from, arg;
4394 {
4395   tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4396   return (t && ! ICS_BAD_FLAG (t));
4397 }