OSDN Git Service

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