OSDN Git Service

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