OSDN Git Service

Warning patches:
[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
1099   /* The `this' and `in_chrg' arguments to constructors are not considered
1100      in overload resolution.  */
1101   if (DECL_CONSTRUCTOR_P (fn))
1102     {
1103       parmnode = TREE_CHAIN (parmnode);
1104       argnode = TREE_CHAIN (argnode);
1105       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1106         {
1107           parmnode = TREE_CHAIN (parmnode);
1108           argnode = TREE_CHAIN (argnode);
1109         }
1110     }
1111
1112   len = list_length (argnode);
1113   convs = make_scratch_vec (len);
1114
1115   for (i = 0; i < len; ++i)
1116     {
1117       tree arg = TREE_VALUE (argnode);
1118       tree argtype = TREE_TYPE (arg);
1119       tree t;
1120
1121       /* An overloaded function does not have an argument type */
1122       if (TREE_CODE (arg) == OVERLOAD)
1123         argtype = unknown_type_node;
1124       argtype = cp_build_type_variant
1125         (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
1126
1127       if (parmnode == void_list_node)
1128         break;
1129       else if (parmnode)
1130         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1131       else
1132         {
1133           t = build1 (IDENTITY_CONV, argtype, arg);
1134           ICS_ELLIPSIS_FLAG (t) = 1;
1135         }
1136
1137       if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1138           && ! DECL_CONSTRUCTOR_P (fn))
1139         ICS_THIS_FLAG (t) = 1;
1140
1141       TREE_VEC_ELT (convs, i) = t;
1142       if (! t)
1143         break;
1144
1145       if (ICS_BAD_FLAG (t))
1146         viable = -1;
1147
1148       if (parmnode)
1149         parmnode = TREE_CHAIN (parmnode);
1150       argnode = TREE_CHAIN (argnode);
1151     }
1152
1153   if (i < len)
1154     viable = 0;
1155
1156   /* Make sure there are default args for the rest of the parms.  */
1157   for (; parmnode && parmnode != void_list_node;
1158        parmnode = TREE_CHAIN (parmnode))
1159     if (! TREE_PURPOSE (parmnode))
1160       {
1161         viable = 0;
1162         break;
1163       }
1164
1165   return add_candidate (candidates, fn, convs, viable);
1166 }
1167
1168 /* Create an overload candidate for the conversion function FN which will
1169    be invoked for expression OBJ, producing a pointer-to-function which
1170    will in turn be called with the argument list ARGLIST, and add it to
1171    CANDIDATES.  FLAGS is passed on to implicit_conversion.  */
1172
1173 static struct z_candidate *
1174 add_conv_candidate (candidates, fn, obj, arglist)
1175      struct z_candidate *candidates;
1176      tree fn, obj, arglist;
1177 {
1178   tree totype = TREE_TYPE (TREE_TYPE (fn));
1179   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
1180   int i, len = list_length (arglist) + 1;
1181   tree convs = make_scratch_vec (len);
1182   tree parmnode = parmlist;
1183   tree argnode = arglist;
1184   int viable = 1;
1185   int flags = LOOKUP_NORMAL;
1186
1187   for (i = 0; i < len; ++i)
1188     {
1189       tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1190       tree argtype = lvalue_type (arg);
1191       tree t;
1192
1193       if (i == 0)
1194         t = implicit_conversion (totype, argtype, arg, flags);
1195       else if (parmnode == void_list_node)
1196         break;
1197       else if (parmnode)
1198         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1199       else
1200         {
1201           t = build1 (IDENTITY_CONV, argtype, arg);
1202           ICS_ELLIPSIS_FLAG (t) = 1;
1203         }
1204
1205       TREE_VEC_ELT (convs, i) = t;
1206       if (! t)
1207         break;
1208
1209       if (ICS_BAD_FLAG (t))
1210         viable = -1;
1211
1212       if (i == 0)
1213         continue;
1214
1215       if (parmnode)
1216         parmnode = TREE_CHAIN (parmnode);
1217       argnode = TREE_CHAIN (argnode);
1218     }
1219
1220   if (i < len)
1221     viable = 0;
1222
1223   for (; parmnode && parmnode != void_list_node;
1224        parmnode = TREE_CHAIN (parmnode))
1225     if (! TREE_PURPOSE (parmnode))
1226       {
1227         viable = 0;
1228         break;
1229       }
1230
1231   return add_candidate (candidates, fn, convs, viable);
1232 }
1233
1234 static struct z_candidate *
1235 build_builtin_candidate (candidates, fnname, type1, type2,
1236                          args, argtypes, flags)
1237      struct z_candidate *candidates;
1238      tree fnname, type1, type2, *args, *argtypes;
1239      int flags;
1240
1241 {
1242   tree t, convs;
1243   int viable = 1, i;
1244   tree types[2];
1245
1246   types[0] = type1;
1247   types[1] = type2;
1248
1249   convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1250
1251   for (i = 0; i < 2; ++i)
1252     {
1253       if (! args[i])
1254         break;
1255
1256       t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1257       if (! t)
1258         {
1259           viable = 0;
1260           /* We need something for printing the candidate.  */
1261           t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1262         }
1263       else if (ICS_BAD_FLAG (t))
1264         viable = 0;
1265       TREE_VEC_ELT (convs, i) = t;
1266     }
1267
1268   /* For COND_EXPR we rearranged the arguments; undo that now.  */
1269   if (args[2])
1270     {
1271       TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1272       TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1273       t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1274       if (t)
1275         TREE_VEC_ELT (convs, 0) = t;
1276       else
1277         viable = 0;
1278     }      
1279
1280   return add_candidate (candidates, fnname, convs, viable);
1281 }
1282
1283 static int
1284 is_complete (t)
1285      tree t;
1286 {
1287   return TYPE_SIZE (complete_type (t)) != NULL_TREE;
1288 }
1289
1290 /* Create any builtin operator overload candidates for the operator in
1291    question given the converted operand types TYPE1 and TYPE2.  The other
1292    args are passed through from add_builtin_candidates to
1293    build_builtin_candidate.  */
1294
1295 static struct z_candidate *
1296 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1297                        args, argtypes, flags)
1298      struct z_candidate *candidates;
1299      enum tree_code code, code2;
1300      tree fnname, type1, type2, *args, *argtypes;
1301      int flags;
1302 {
1303   switch (code)
1304     {
1305     case POSTINCREMENT_EXPR:
1306     case POSTDECREMENT_EXPR:
1307       args[1] = integer_zero_node;
1308       type2 = integer_type_node;
1309       break;
1310     default:
1311       break;
1312     }
1313
1314   switch (code)
1315     {
1316
1317 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1318      and  VQ  is  either  volatile or empty, there exist candidate operator
1319      functions of the form
1320              VQ T&   operator++(VQ T&);
1321              T       operator++(VQ T&, int);
1322    5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1323      type  other than bool, and VQ is either volatile or empty, there exist
1324      candidate operator functions of the form
1325              VQ T&   operator--(VQ T&);
1326              T       operator--(VQ T&, int);
1327    6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
1328      complete  object type, and VQ is either volatile or empty, there exist
1329      candidate operator functions of the form
1330              T*VQ&   operator++(T*VQ&);
1331              T*VQ&   operator--(T*VQ&);
1332              T*      operator++(T*VQ&, int);
1333              T*      operator--(T*VQ&, int);  */
1334
1335     case POSTDECREMENT_EXPR:
1336     case PREDECREMENT_EXPR:
1337       if (TREE_CODE (type1) == BOOLEAN_TYPE)
1338         return candidates;
1339     case POSTINCREMENT_EXPR:
1340     case PREINCREMENT_EXPR:
1341       if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
1342           || TYPE_PTROB_P (type1))
1343         {
1344           type1 = build_reference_type (type1);
1345           break;
1346         }
1347       return candidates;
1348
1349 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1350      exist candidate operator functions of the form
1351
1352              T&      operator*(T*);
1353
1354    8 For every function type T, there exist candidate operator functions of
1355      the form
1356              T&      operator*(T*);  */
1357
1358     case INDIRECT_REF:
1359       if (TREE_CODE (type1) == POINTER_TYPE
1360           && (TYPE_PTROB_P (type1)
1361               || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1362         break;
1363       return candidates;
1364
1365 /* 9 For every type T, there exist candidate operator functions of the form
1366              T*      operator+(T*);
1367
1368    10For  every  promoted arithmetic type T, there exist candidate operator
1369      functions of the form
1370              T       operator+(T);
1371              T       operator-(T);  */
1372
1373     case CONVERT_EXPR: /* unary + */
1374       if (TREE_CODE (type1) == POINTER_TYPE
1375           && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1376         break;
1377     case NEGATE_EXPR:
1378       if (ARITHMETIC_TYPE_P (type1))
1379         break;
1380       return candidates;
1381
1382 /* 11For every promoted integral type T,  there  exist  candidate  operator
1383      functions of the form
1384              T       operator~(T);  */
1385
1386     case BIT_NOT_EXPR:
1387       if (INTEGRAL_TYPE_P (type1))
1388         break;
1389       return candidates;
1390
1391 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1392      is the same type as C2 or is a derived class of C2, T  is  a  complete
1393      object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1394      there exist candidate operator functions of the form
1395              CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1396      where CV12 is the union of CV1 and CV2.  */
1397
1398     case MEMBER_REF:
1399       if (TREE_CODE (type1) == POINTER_TYPE
1400           && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1401         {
1402           tree c1 = TREE_TYPE (type1);
1403           tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1404                      ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1405                      : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1406
1407           if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1408               && (TYPE_PTRMEMFUNC_P (type2)
1409                   || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1410             break;
1411         }
1412       return candidates;
1413
1414 /* 13For every pair of promoted arithmetic types L and R, there exist  can-
1415      didate operator functions of the form
1416              LR      operator*(L, R);
1417              LR      operator/(L, R);
1418              LR      operator+(L, R);
1419              LR      operator-(L, R);
1420              bool    operator<(L, R);
1421              bool    operator>(L, R);
1422              bool    operator<=(L, R);
1423              bool    operator>=(L, R);
1424              bool    operator==(L, R);
1425              bool    operator!=(L, R);
1426      where  LR  is  the  result of the usual arithmetic conversions between
1427      types L and R.
1428
1429    14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
1430      unqualified  complete  object  type and I is a promoted integral type,
1431      there exist candidate operator functions of the form
1432              T*      operator+(T*, I);
1433              T&      operator[](T*, I);
1434              T*      operator-(T*, I);
1435              T*      operator+(I, T*);
1436              T&      operator[](I, T*);
1437
1438    15For every T, where T is a pointer to complete object type, there exist
1439      candidate operator functions of the form112)
1440              ptrdiff_t operator-(T, T);
1441
1442    16For  every pointer type T, there exist candidate operator functions of
1443      the form
1444              bool    operator<(T, T);
1445              bool    operator>(T, T);
1446              bool    operator<=(T, T);
1447              bool    operator>=(T, T);
1448              bool    operator==(T, T);
1449              bool    operator!=(T, T);
1450
1451    17For every pointer to member type T,  there  exist  candidate  operator
1452      functions of the form
1453              bool    operator==(T, T);
1454              bool    operator!=(T, T);  */
1455
1456     case MINUS_EXPR:
1457       if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1458         break;
1459       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1460         {
1461           type2 = ptrdiff_type_node;
1462           break;
1463         }
1464     case MULT_EXPR:
1465     case TRUNC_DIV_EXPR:
1466       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1467         break;
1468       return candidates;
1469
1470     case EQ_EXPR:
1471     case NE_EXPR:
1472       if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1473           || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1474         break;
1475       if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1476           && null_ptr_cst_p (args[1]))
1477         {
1478           type2 = type1;
1479           break;
1480         }
1481       if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1482           && null_ptr_cst_p (args[0]))
1483         {
1484           type1 = type2;
1485           break;
1486         }
1487     case LT_EXPR:
1488     case GT_EXPR:
1489     case LE_EXPR:
1490     case GE_EXPR:
1491     case MAX_EXPR:
1492     case MIN_EXPR:
1493       if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1494           || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
1495         break;
1496       if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1497         {
1498           type2 = type1;
1499           break;
1500         }
1501       if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1502         {
1503           type1 = type2;
1504           break;
1505         }
1506       return candidates;
1507
1508     case PLUS_EXPR:
1509       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1510         break;
1511     case ARRAY_REF:
1512       if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1513         {
1514           type1 = ptrdiff_type_node;
1515           break;
1516         }
1517       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1518         {
1519           type2 = ptrdiff_type_node;
1520           break;
1521         }
1522       return candidates;
1523
1524 /* 18For  every pair of promoted integral types L and R, there exist candi-
1525      date operator functions of the form
1526              LR      operator%(L, R);
1527              LR      operator&(L, R);
1528              LR      operator^(L, R);
1529              LR      operator|(L, R);
1530              L       operator<<(L, R);
1531              L       operator>>(L, R);
1532      where LR is the result of the  usual  arithmetic  conversions  between
1533      types L and R.  */
1534
1535     case TRUNC_MOD_EXPR:
1536     case BIT_AND_EXPR:
1537     case BIT_IOR_EXPR:
1538     case BIT_XOR_EXPR:
1539     case LSHIFT_EXPR:
1540     case RSHIFT_EXPR:
1541       if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1542         break;
1543       return candidates;
1544
1545 /* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
1546      type, VQ is either volatile or empty, and R is a  promoted  arithmetic
1547      type, there exist candidate operator functions of the form
1548              VQ L&   operator=(VQ L&, R);
1549              VQ L&   operator*=(VQ L&, R);
1550              VQ L&   operator/=(VQ L&, R);
1551              VQ L&   operator+=(VQ L&, R);
1552              VQ L&   operator-=(VQ L&, R);
1553
1554    20For  every  pair T, VQ), where T is any type and VQ is either volatile
1555      or empty, there exist candidate operator functions of the form
1556              T*VQ&   operator=(T*VQ&, T*);
1557
1558    21For every pair T, VQ), where T is a pointer to member type and  VQ  is
1559      either  volatile or empty, there exist candidate operator functions of
1560      the form
1561              VQ T&   operator=(VQ T&, T);
1562
1563    22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
1564      unqualified  complete object type, VQ is either volatile or empty, and
1565      I is a promoted integral type, there exist  candidate  operator  func-
1566      tions of the form
1567              T*VQ&   operator+=(T*VQ&, I);
1568              T*VQ&   operator-=(T*VQ&, I);
1569
1570    23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
1571      type, VQ is either volatile or empty, and R  is  a  promoted  integral
1572      type, there exist candidate operator functions of the form
1573
1574              VQ L&   operator%=(VQ L&, R);
1575              VQ L&   operator<<=(VQ L&, R);
1576              VQ L&   operator>>=(VQ L&, R);
1577              VQ L&   operator&=(VQ L&, R);
1578              VQ L&   operator^=(VQ L&, R);
1579              VQ L&   operator|=(VQ L&, R);  */
1580
1581     case MODIFY_EXPR:
1582       switch (code2)
1583         {
1584         case PLUS_EXPR:
1585         case MINUS_EXPR:
1586           if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1587             {
1588               type2 = ptrdiff_type_node;
1589               break;
1590             }
1591         case MULT_EXPR:
1592         case TRUNC_DIV_EXPR:
1593           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1594             break;
1595           return candidates;
1596
1597         case TRUNC_MOD_EXPR:
1598         case BIT_AND_EXPR:
1599         case BIT_IOR_EXPR:
1600         case BIT_XOR_EXPR:
1601         case LSHIFT_EXPR:
1602         case RSHIFT_EXPR:
1603           if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1604             break;
1605           return candidates;
1606
1607         case NOP_EXPR:
1608           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1609             break;
1610           if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1611               || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1612               || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1613               || ((TYPE_PTRMEMFUNC_P (type1)
1614                    || TREE_CODE (type1) == POINTER_TYPE)
1615                   && null_ptr_cst_p (args[1])))
1616             {
1617               type2 = type1;
1618               break;
1619             }
1620           return candidates;
1621
1622         default:
1623           my_friendly_abort (367);
1624         }
1625       type1 = build_reference_type (type1);
1626       break;
1627
1628     case COND_EXPR:
1629       /* Kludge around broken overloading rules whereby
1630          bool ? const char& : enum is ambiguous
1631          (between int and const char&).  */
1632       flags |= LOOKUP_NO_TEMP_BIND;
1633
1634       /* Extension: Support ?: of enumeral type.  Hopefully this will not
1635          be an extension for long.  */
1636       if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
1637         break;
1638       else if (TREE_CODE (type1) == ENUMERAL_TYPE
1639                || TREE_CODE (type2) == ENUMERAL_TYPE)
1640         return candidates;
1641       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1642         break;
1643       if (TREE_CODE (type1) == TREE_CODE (type2)
1644           && (TREE_CODE (type1) == REFERENCE_TYPE
1645               || TREE_CODE (type1) == POINTER_TYPE
1646               || TYPE_PTRMEMFUNC_P (type1)
1647               || IS_AGGR_TYPE (type1)))
1648         break;
1649       if (TREE_CODE (type1) == REFERENCE_TYPE
1650           || TREE_CODE (type2) == REFERENCE_TYPE)
1651         return candidates;
1652       if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
1653            && null_ptr_cst_p (args[1]))
1654           || IS_AGGR_TYPE (type1))
1655         {
1656           type2 = type1;
1657           break;
1658         }
1659       if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
1660            && null_ptr_cst_p (args[0]))
1661           || IS_AGGR_TYPE (type2))
1662         {
1663           type1 = type2;
1664           break;
1665         }
1666       return candidates;
1667
1668     default:
1669       my_friendly_abort (367);
1670     }
1671
1672   /* If we're dealing with two pointer types, we need candidates
1673      for both of them.  */
1674   if (type2 && type1 != type2
1675       && TREE_CODE (type1) == TREE_CODE (type2)
1676       && (TREE_CODE (type1) == REFERENCE_TYPE
1677           || (TREE_CODE (type1) == POINTER_TYPE
1678               && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1679           || TYPE_PTRMEMFUNC_P (type1)
1680           || IS_AGGR_TYPE (type1)))
1681     {
1682       candidates = build_builtin_candidate
1683         (candidates, fnname, type1, type1, args, argtypes, flags);
1684       return build_builtin_candidate
1685         (candidates, fnname, type2, type2, args, argtypes, flags);
1686     }
1687
1688   return build_builtin_candidate
1689     (candidates, fnname, type1, type2, args, argtypes, flags);
1690 }
1691
1692 tree
1693 type_decays_to (type)
1694      tree type;
1695 {
1696   if (TREE_CODE (type) == ARRAY_TYPE)
1697     return build_pointer_type (TREE_TYPE (type));
1698   if (TREE_CODE (type) == FUNCTION_TYPE)
1699     return build_pointer_type (type);
1700   return type;
1701 }
1702
1703 /* There are three conditions of builtin candidates:
1704
1705    1) bool-taking candidates.  These are the same regardless of the input.
1706    2) pointer-pair taking candidates.  These are generated for each type
1707       one of the input types converts to.
1708    3) arithmetic candidates.  According to the WP, we should generate
1709       all of these, but I'm trying not to... */
1710
1711 static struct z_candidate *
1712 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
1713      struct z_candidate *candidates;
1714      enum tree_code code, code2;
1715      tree fnname, *args;
1716      int flags;
1717 {
1718   int ref1, i;
1719   tree type, argtypes[3], types[2];
1720
1721   for (i = 0; i < 3; ++i)
1722     {
1723       if (args[i])
1724         argtypes[i]  = lvalue_type (args[i]);
1725       else
1726         argtypes[i] = NULL_TREE;
1727     }
1728
1729   switch (code)
1730     {
1731 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
1732      and  VQ  is  either  volatile or empty, there exist candidate operator
1733      functions of the form
1734                  VQ T&   operator++(VQ T&);  */
1735
1736     case POSTINCREMENT_EXPR:
1737     case PREINCREMENT_EXPR:
1738     case POSTDECREMENT_EXPR:
1739     case PREDECREMENT_EXPR:
1740     case MODIFY_EXPR:
1741       ref1 = 1;
1742       break;
1743
1744 /* 24There also exist candidate operator functions of the form
1745              bool    operator!(bool);
1746              bool    operator&&(bool, bool);
1747              bool    operator||(bool, bool);  */
1748
1749     case TRUTH_NOT_EXPR:
1750       return build_builtin_candidate
1751         (candidates, fnname, boolean_type_node,
1752          NULL_TREE, args, argtypes, flags);
1753
1754     case TRUTH_ORIF_EXPR:
1755     case TRUTH_ANDIF_EXPR:
1756       return build_builtin_candidate
1757         (candidates, fnname, boolean_type_node,
1758          boolean_type_node, args, argtypes, flags);
1759
1760     case ADDR_EXPR:
1761     case COMPOUND_EXPR:
1762     case COMPONENT_REF:
1763       return candidates;
1764
1765     default:
1766       ref1 = 0;
1767     }
1768
1769   types[0] = types[1] = NULL_TREE;
1770
1771   for (i = 0; i < 2; ++i)
1772     {
1773       if (! args[i])
1774         ;
1775       else if (IS_AGGR_TYPE (argtypes[i]))
1776         {
1777           tree convs = lookup_conversions (argtypes[i]);
1778
1779           if (code == COND_EXPR)
1780             {
1781               if (real_lvalue_p (args[i]))
1782                 types[i] = scratch_tree_cons
1783                   (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1784
1785               types[i] = scratch_tree_cons
1786                 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1787             }
1788                 
1789           else if (! convs || (i == 0 && code == MODIFY_EXPR
1790                                && code2 == NOP_EXPR))
1791             return candidates;
1792
1793           for (; convs; convs = TREE_CHAIN (convs))
1794             {
1795               type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
1796
1797               if (i == 0 && ref1
1798                   && (TREE_CODE (type) != REFERENCE_TYPE
1799                       || TYPE_READONLY (TREE_TYPE (type))))
1800                 continue;
1801
1802               if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1803                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1804
1805               type = non_reference (type);
1806               if (i != 0 || ! ref1)
1807                 {
1808                   type = TYPE_MAIN_VARIANT (type_decays_to (type));
1809                   if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1810                     types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1811                   if (INTEGRAL_TYPE_P (type))
1812                     type = type_promotes_to (type);
1813                 }
1814
1815               if (! value_member (type, types[i]))
1816                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1817             }
1818         }
1819       else
1820         {
1821           if (code == COND_EXPR && real_lvalue_p (args[i]))
1822             types[i] = scratch_tree_cons
1823               (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1824           type = non_reference (argtypes[i]);
1825           if (i != 0 || ! ref1)
1826             {
1827               type = TYPE_MAIN_VARIANT (type_decays_to (type));
1828               if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
1829                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1830               if (INTEGRAL_TYPE_P (type))
1831                 type = type_promotes_to (type);
1832             }
1833           types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
1834         }
1835     }
1836
1837   for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1838     {
1839       if (types[1])
1840         for (type = types[1]; type; type = TREE_CHAIN (type))
1841           candidates = add_builtin_candidate
1842             (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1843              TREE_VALUE (type), args, argtypes, flags);
1844       else
1845         candidates = add_builtin_candidate
1846           (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1847            NULL_TREE, args, argtypes, flags);
1848     }
1849
1850   return candidates;
1851 }
1852
1853
1854 /* If TMPL can be successfully instantiated as indicated by
1855    EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
1856
1857    TMPL is the template.  EXPLICIT_TARGS are any explicit template
1858    arguments.  ARGLIST is the arguments provided at the call-site.
1859    The RETURN_TYPE is the desired type for conversion operators.  If
1860    OBJ is NULL_TREE, FLAGS are as for add_function_candidate.  If an
1861    OBJ is supplied, FLAGS are ignored, and OBJ is as for
1862    add_conv_candidate.  */
1863
1864 static struct z_candidate*
1865 add_template_candidate_real (candidates, tmpl, explicit_targs,
1866                              arglist, return_type, flags,
1867                              obj, strict)
1868      struct z_candidate *candidates;
1869      tree tmpl, explicit_targs, arglist, return_type;
1870      int flags;
1871      tree obj;
1872      unification_kind_t strict;
1873 {
1874   int ntparms = DECL_NTPARMS (tmpl);
1875   tree targs = make_scratch_vec (ntparms);
1876   struct z_candidate *cand;
1877   int i;
1878   tree fn;
1879
1880   i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
1881                            return_type, strict, NULL_TREE);
1882
1883   if (i != 0)
1884     return candidates;
1885
1886   fn = instantiate_template (tmpl, targs);
1887   if (fn == error_mark_node)
1888     return candidates;
1889
1890   if (obj != NULL_TREE)
1891     /* Aha, this is a conversion function.  */
1892     cand = add_conv_candidate (candidates, fn, obj, arglist);
1893   else
1894     cand = add_function_candidate (candidates, fn, arglist, flags);
1895   if (DECL_TI_TEMPLATE (fn) != tmpl)
1896     /* This situation can occur if a member template of a template
1897        class is specialized.  Then, instantiate_template might return
1898        an instantiation of the specialization, in which case the
1899        DECL_TI_TEMPLATE field will point at the original
1900        specialization.  For example:
1901
1902          template <class T> struct S { template <class U> void f(U);
1903                                        template <> void f(int) {}; };
1904          S<double> sd;
1905          sd.f(3);
1906
1907        Here, TMPL will be template <class U> S<double>::f(U).
1908        And, instantiate template will give us the specialization
1909        template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
1910        for this will point at template <class T> template <> S<T>::f(int),
1911        so that we can find the definition.  For the purposes of
1912        overload resolution, however, we want the original TMPL.  */
1913     cand->template = tree_cons (tmpl, targs, NULL_TREE);
1914   else
1915     cand->template = DECL_TEMPLATE_INFO (fn);
1916
1917   return cand;
1918 }
1919
1920
1921 static struct z_candidate *
1922 add_template_candidate (candidates, tmpl, explicit_targs, 
1923                         arglist, return_type, flags, strict)
1924      struct z_candidate *candidates;
1925      tree tmpl, explicit_targs, arglist, return_type;
1926      int flags;
1927      unification_kind_t strict;
1928 {
1929   return 
1930     add_template_candidate_real (candidates, tmpl, explicit_targs,
1931                                  arglist, return_type, flags,
1932                                  NULL_TREE, strict);
1933 }
1934
1935
1936 static struct z_candidate *
1937 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
1938      struct z_candidate *candidates;
1939      tree tmpl, obj, arglist, return_type;
1940 {
1941   return 
1942     add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
1943                                  return_type, 0, obj, DEDUCE_CONV);
1944 }
1945
1946
1947 static int
1948 any_viable (cands)
1949      struct z_candidate *cands;
1950 {
1951   for (; cands; cands = cands->next)
1952     if (pedantic ? cands->viable == 1 : cands->viable)
1953       return 1;
1954   return 0;
1955 }
1956
1957 static struct z_candidate *
1958 splice_viable (cands)
1959      struct z_candidate *cands;
1960 {
1961   struct z_candidate **p = &cands;
1962
1963   for (; *p; )
1964     {
1965       if (pedantic ? (*p)->viable == 1 : (*p)->viable)
1966         p = &((*p)->next);
1967       else
1968         *p = (*p)->next;
1969     }
1970
1971   return cands;
1972 }
1973
1974 static tree
1975 build_this (obj)
1976      tree obj;
1977 {
1978   /* Fix this to work on non-lvalues.  */
1979   if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
1980       || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
1981     return obj;
1982   else
1983     return build_unary_op (ADDR_EXPR, obj, 0);
1984 }
1985
1986 static void
1987 print_z_candidates (candidates)
1988      struct z_candidate *candidates;
1989 {
1990   char *str = "candidates are:";
1991   for (; candidates; candidates = candidates->next)
1992     {
1993       if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
1994         {
1995           if (candidates->fn == ansi_opname [COND_EXPR])
1996             cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
1997                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
1998                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
1999                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2000           else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2001             cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2002                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2003                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2004           else
2005             cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2006                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2007         }
2008       else
2009         cp_error_at ("%s %+D%s", str, candidates->fn,
2010                      candidates->viable == -1 ? " <near match>" : "");
2011       str = "               "; 
2012     }
2013 }
2014
2015 /* Returns the best overload candidate to perform the requested
2016    conversion.  This function is used for three the overloading situations
2017    described in [over.match.copy], [over.match.conv], and [over.match.ref].
2018    If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2019    per [dcl.init.ref], so we ignore temporary bindings.  */
2020
2021 static struct z_candidate *
2022 build_user_type_conversion_1 (totype, expr, flags)
2023      tree totype, expr;
2024      int flags;
2025 {
2026   struct z_candidate *candidates, *cand;
2027   tree fromtype = TREE_TYPE (expr);
2028   tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2029   tree args = NULL_TREE;
2030   tree templates = NULL_TREE;
2031
2032   if (IS_AGGR_TYPE (totype))
2033     ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
2034   if (IS_AGGR_TYPE (fromtype)
2035       && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
2036     convs = lookup_conversions (fromtype);
2037
2038   candidates = 0;
2039   flags |= LOOKUP_NO_CONVERSION;
2040
2041   if (ctors)
2042     {
2043       tree t = build_int_2 (0, 0);
2044       TREE_TYPE (t) = build_pointer_type (totype);
2045       args = build_scratch_list (NULL_TREE, expr);
2046       if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
2047         args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
2048       args = scratch_tree_cons (NULL_TREE, t, args);
2049
2050       ctors = TREE_VALUE (ctors);
2051     }
2052   for (; ctors; ctors = OVL_NEXT (ctors))
2053     {
2054       tree ctor = OVL_CURRENT (ctors);
2055       if (DECL_NONCONVERTING_P (ctor))
2056         continue;
2057
2058       if (TREE_CODE (ctor) == TEMPLATE_DECL) 
2059         {
2060           templates = scratch_tree_cons (NULL_TREE, ctor, templates);
2061           candidates = 
2062             add_template_candidate (candidates, ctor,
2063                                     NULL_TREE, args, NULL_TREE, flags,
2064                                     DEDUCE_CALL);
2065         } 
2066       else 
2067         candidates = add_function_candidate (candidates, ctor,
2068                                              args, flags); 
2069
2070       if (candidates) 
2071         {
2072           candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2073           candidates->basetype_path = TYPE_BINFO (totype);
2074         } 
2075     }
2076
2077   if (convs)
2078     args = build_scratch_list (NULL_TREE, build_this (expr));
2079
2080   for (; convs; convs = TREE_CHAIN (convs))
2081     {
2082       tree fns = TREE_VALUE (convs);
2083       int convflags = LOOKUP_NO_CONVERSION;
2084       tree ics;
2085
2086       /* If we are called to convert to a reference type, we are trying to
2087          find an lvalue binding, so don't even consider temporaries.  If
2088          we don't find an lvalue binding, the caller will try again to
2089          look for a temporary binding.  */
2090       if (TREE_CODE (totype) == REFERENCE_TYPE)
2091         convflags |= LOOKUP_NO_TEMP_BIND;
2092
2093       if (TREE_CODE (fns) != TEMPLATE_DECL)
2094         ics = implicit_conversion
2095           (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2096       else
2097         /* We can't compute this yet.  */
2098         ics = error_mark_node;
2099
2100       if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2101         /* ignore the near match.  */;
2102       else if (ics)
2103         for (; fns; fns = OVL_NEXT (fns))
2104           {
2105             tree fn = OVL_CURRENT (fns);
2106             struct z_candidate *old_candidates = candidates;
2107
2108             if (TREE_CODE (fn) == TEMPLATE_DECL)
2109               {
2110                 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2111                 candidates = 
2112                   add_template_candidate (candidates, fn, NULL_TREE,
2113                                           args, totype, flags,
2114                                           DEDUCE_CONV);
2115               } 
2116             else 
2117               candidates = add_function_candidate (candidates, fn,
2118                                                    args, flags); 
2119
2120             if (candidates != old_candidates)
2121               {
2122                 if (TREE_CODE (fn) == TEMPLATE_DECL)
2123                   ics = implicit_conversion
2124                     (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2125                      0, convflags);
2126
2127                 candidates->second_conv = ics;
2128                 candidates->basetype_path = TREE_PURPOSE (convs);
2129
2130                 if (ics == NULL_TREE)
2131                   candidates->viable = 0;
2132                 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2133                   candidates->viable = -1;
2134               }
2135           }
2136     }
2137
2138   if (! any_viable (candidates))
2139     {
2140 #if 0
2141       if (flags & LOOKUP_COMPLAIN)
2142         {
2143           if (candidates && ! candidates->next)
2144             /* say why this one won't work or try to be loose */;
2145           else
2146             cp_error ("no viable candidates");
2147         }
2148 #endif
2149
2150       return 0;
2151     }
2152
2153   candidates = splice_viable (candidates);
2154   cand = tourney (candidates);
2155
2156   if (cand == 0)
2157     {
2158       if (flags & LOOKUP_COMPLAIN)
2159         {
2160           cp_error ("conversion from `%T' to `%T' is ambiguous",
2161                     fromtype, totype);
2162           print_z_candidates (candidates);
2163         }
2164
2165       cand = candidates;        /* any one will do */
2166       cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2167       ICS_USER_FLAG (cand->second_conv) = 1;
2168       ICS_BAD_FLAG (cand->second_conv) = 1;
2169
2170       return cand;
2171     }
2172
2173   for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2174     p = &(TREE_OPERAND (*p, 0));
2175
2176   /* Pedantically, normal function declarations are never considered
2177      to refer to template instantiations, so we only do this with
2178      -fguiding-decls.  */ 
2179   if (flag_guiding_decls && templates && ! cand->template 
2180       && !DECL_INITIAL (cand->fn) 
2181       && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2182     add_maybe_template (cand->fn, templates);
2183
2184   *p = build
2185     (USER_CONV,
2186      (DECL_CONSTRUCTOR_P (cand->fn)
2187       ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2188      expr, build_expr_ptr_wrapper (cand));
2189   ICS_USER_FLAG (cand->second_conv) = 1;
2190   if (cand->viable == -1)
2191     ICS_BAD_FLAG (cand->second_conv) = 1;
2192
2193   return cand;
2194 }
2195
2196 tree
2197 build_user_type_conversion (totype, expr, flags)
2198      tree totype, expr;
2199      int flags;
2200 {
2201   struct z_candidate *cand
2202     = build_user_type_conversion_1 (totype, expr, flags);
2203
2204   if (cand)
2205     {
2206       if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2207         return error_mark_node;
2208       return convert_from_reference (convert_like (cand->second_conv, expr));
2209     }
2210   return NULL_TREE;
2211 }
2212
2213 /* Do any initial processing on the arguments to a function call.  */
2214
2215 static tree
2216 resolve_args (args)
2217      tree args;
2218 {
2219   tree t;
2220   for (t = args; t; t = TREE_CHAIN (t))
2221     {
2222       if (TREE_VALUE (t) == error_mark_node)
2223         return error_mark_node;
2224       else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2225         {
2226           error ("invalid use of void expression");
2227           return error_mark_node;
2228         }
2229       else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2230         TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2231     }
2232   return args;
2233 }
2234       
2235 tree
2236 build_new_function_call (fn, args)
2237      tree fn, args;
2238 {
2239   struct z_candidate *candidates = 0, *cand;
2240   tree explicit_targs = NULL_TREE;
2241   int template_only = 0;
2242
2243   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2244     {
2245       explicit_targs = TREE_OPERAND (fn, 1);
2246       fn = TREE_OPERAND (fn, 0);
2247       template_only = 1;
2248     }
2249
2250   if (really_overloaded_fn (fn))
2251     {
2252       tree t1;
2253       tree templates = NULL_TREE;
2254
2255       args = resolve_args (args);
2256
2257       if (args == error_mark_node)
2258         return error_mark_node;
2259
2260       for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2261         {
2262           tree t = OVL_FUNCTION (t1);
2263           if (TREE_CODE (t) == TEMPLATE_DECL)
2264             {
2265               templates = scratch_tree_cons (NULL_TREE, t, templates);
2266               candidates = add_template_candidate
2267                 (candidates, t, explicit_targs, args, NULL_TREE,
2268                  LOOKUP_NORMAL, DEDUCE_CALL);  
2269             }
2270           else if (! template_only)
2271             candidates = add_function_candidate
2272               (candidates, t, args, LOOKUP_NORMAL);
2273         }
2274
2275       if (! any_viable (candidates))
2276         {
2277           if (candidates && ! candidates->next)
2278             return build_function_call (candidates->fn, args);
2279           cp_error ("no matching function for call to `%D (%A)'",
2280                     DECL_NAME (OVL_FUNCTION (fn)), args);
2281           if (candidates)
2282             print_z_candidates (candidates);
2283           return error_mark_node;
2284         }
2285       candidates = splice_viable (candidates);
2286       cand = tourney (candidates);
2287
2288       if (cand == 0)
2289         {
2290           cp_error ("call of overloaded `%D (%A)' is ambiguous",
2291                     DECL_NAME (OVL_FUNCTION (fn)), args);
2292           print_z_candidates (candidates);
2293           return error_mark_node;
2294         }
2295
2296       /* Pedantically, normal function declarations are never considered
2297          to refer to template instantiations, so we only do this with
2298          -fguiding-decls.  */
2299       if (flag_guiding_decls && templates && ! cand->template 
2300           && ! DECL_INITIAL (cand->fn))
2301         add_maybe_template (cand->fn, templates);
2302
2303       return build_over_call (cand, args, LOOKUP_NORMAL);
2304     }
2305
2306   /* This is not really overloaded. */
2307   fn = OVL_CURRENT (fn);
2308
2309   return build_function_call (fn, args);
2310 }
2311
2312 static tree
2313 build_object_call (obj, args)
2314      tree obj, args;
2315 {
2316   struct z_candidate *candidates = 0, *cand;
2317   tree fns, convs, mem_args = NULL_TREE;
2318   tree type = TREE_TYPE (obj);
2319   tree templates = NULL_TREE;
2320
2321   if (TYPE_PTRMEMFUNC_P (type))
2322     {
2323       /* It's no good looking for an overloaded operator() on a
2324          pointer-to-member-function.  */
2325       cp_error ("pointer-to-member function %E cannot be called", obj);
2326       cp_error ("without an object; consider using .* or ->*");
2327       return error_mark_node;
2328     }
2329
2330   fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 1);
2331   if (fns == error_mark_node)
2332     return error_mark_node;
2333
2334   args = resolve_args (args);
2335
2336   if (args == error_mark_node)
2337     return error_mark_node;
2338
2339   if (fns)
2340     {
2341       tree base = TREE_PURPOSE (fns);
2342       mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
2343
2344       for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2345         {
2346           tree fn = OVL_CURRENT (fns);
2347           if (TREE_CODE (fn) == TEMPLATE_DECL)
2348             {
2349               templates = scratch_tree_cons (NULL_TREE, fn, templates);
2350               candidates 
2351                 = add_template_candidate (candidates, fn, NULL_TREE,
2352                                           mem_args, NULL_TREE, 
2353                                           LOOKUP_NORMAL, DEDUCE_CALL);
2354             }
2355           else
2356             candidates = add_function_candidate
2357               (candidates, fn, mem_args, LOOKUP_NORMAL);
2358
2359           if (candidates)
2360             candidates->basetype_path = base;
2361         }
2362     }
2363
2364   convs = lookup_conversions (type);
2365
2366   for (; convs; convs = TREE_CHAIN (convs))
2367     {
2368       tree fns = TREE_VALUE (convs);
2369       tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2370       tree fn;
2371
2372       if (TREE_CODE (totype) == POINTER_TYPE
2373           && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2374         for (; fns; fns = OVL_NEXT (fn))
2375           {
2376             fn = OVL_CURRENT (fn);
2377             if (TREE_CODE (fn) == TEMPLATE_DECL) 
2378               {
2379                 templates = scratch_tree_cons (NULL_TREE, fn, templates);
2380                 candidates = add_template_conv_candidate (candidates,
2381                                                           fn,
2382                                                           obj,
2383                                                           args,
2384                                                           totype);
2385               }
2386             else
2387               candidates = add_conv_candidate (candidates, fn, obj, args);
2388
2389             if (candidates)
2390               candidates->basetype_path = TREE_PURPOSE (convs);
2391           }
2392     }
2393
2394   if (! any_viable (candidates))
2395     {
2396       cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2397       print_z_candidates (candidates);
2398       return error_mark_node;
2399     }
2400
2401   candidates = splice_viable (candidates);
2402   cand = tourney (candidates);
2403
2404   if (cand == 0)
2405     {
2406       cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2407       print_z_candidates (candidates);
2408       return error_mark_node;
2409     }
2410
2411   if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
2412     return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2413
2414   obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
2415
2416   /* FIXME */
2417   return build_function_call (obj, args);
2418 }
2419
2420 static void
2421 op_error (code, code2, arg1, arg2, arg3, problem)
2422      enum tree_code code, code2;
2423      tree arg1, arg2, arg3;
2424      char *problem;
2425 {
2426   char * opname
2427     = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
2428
2429   switch (code)
2430     {
2431     case COND_EXPR:
2432       cp_error ("%s for `%T ? %T : %T'", problem,
2433                 error_type (arg1), error_type (arg2), error_type (arg3));
2434       break;
2435     case POSTINCREMENT_EXPR:
2436     case POSTDECREMENT_EXPR:
2437       cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
2438       break;
2439     case ARRAY_REF:
2440       cp_error ("%s for `%T[%T]'", problem,
2441                 error_type (arg1), error_type (arg2));
2442       break;
2443     default:
2444       if (arg2)
2445         cp_error ("%s for `%T %s %T'", problem,
2446                   error_type (arg1), opname, error_type (arg2));
2447       else
2448         cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
2449     }
2450 }
2451
2452 tree
2453 build_new_op (code, flags, arg1, arg2, arg3)
2454      enum tree_code code;
2455      int flags;
2456      tree arg1, arg2, arg3;
2457 {
2458   struct z_candidate *candidates = 0, *cand;
2459   tree fns, mem_arglist = NULL_TREE, arglist, fnname;
2460   enum tree_code code2 = NOP_EXPR;
2461   tree templates = NULL_TREE;
2462   tree conv;
2463
2464   if (arg1 == error_mark_node
2465       || arg2 == error_mark_node
2466       || arg3 == error_mark_node)
2467     return error_mark_node;
2468
2469   /* This can happen if a template takes all non-type parameters, e.g.
2470      undeclared_template<1, 5, 72>a;  */
2471   if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
2472     {
2473       cp_error ("`%D' must be declared before use", arg1);
2474       return error_mark_node;
2475     }
2476
2477   if (code == MODIFY_EXPR)
2478     {
2479       code2 = TREE_CODE (arg3);
2480       arg3 = NULL_TREE;
2481       fnname = ansi_assopname[code2];
2482     }
2483   else
2484     fnname = ansi_opname[code];
2485
2486   switch (code)
2487     {
2488     case NEW_EXPR:
2489     case VEC_NEW_EXPR:
2490       {
2491         tree rval;
2492
2493         arglist = scratch_tree_cons (NULL_TREE, arg2, arg3);
2494         if (flags & LOOKUP_GLOBAL)
2495           return build_new_function_call
2496             (lookup_function_nonclass (fnname, arglist), arglist);
2497
2498         /* FIXME */
2499         rval = build_method_call
2500           (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
2501                                "new"),
2502            fnname, arglist, NULL_TREE, flags);
2503         if (rval == error_mark_node)
2504           /* User might declare fancy operator new, but invoke it
2505              like standard one.  */
2506           return rval;
2507
2508         TREE_TYPE (rval) = arg1;
2509         return rval;
2510       }
2511
2512     case VEC_DELETE_EXPR:
2513     case DELETE_EXPR:
2514       {
2515         tree rval;
2516
2517         if (flags & LOOKUP_GLOBAL)
2518           {
2519             arglist = build_scratch_list (NULL_TREE, arg1);
2520             return build_new_function_call
2521               (lookup_function_nonclass (fnname, arglist), arglist);
2522           }    
2523
2524         arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2525
2526         arg1 = TREE_TYPE (arg1);
2527
2528         /* This handles the case where we're trying to delete
2529            X (*a)[10];
2530            a=new X[5][10];
2531            delete[] a; */
2532            
2533         if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
2534           {
2535             /* Strip off the pointer and the array.  */
2536             arg1 = TREE_TYPE (TREE_TYPE (arg1));
2537
2538             while (TREE_CODE (arg1) == ARRAY_TYPE)
2539                 arg1 = (TREE_TYPE (arg1));
2540
2541             arg1 = build_pointer_type (arg1);
2542           }
2543
2544         /* FIXME */
2545         rval = build_method_call
2546           (build_indirect_ref (build1 (NOP_EXPR, arg1,
2547                                        error_mark_node),
2548                                NULL_PTR),
2549            fnname, arglist, NULL_TREE, flags);
2550 #if 0
2551         /* This can happen when operator delete is protected.  */
2552         my_friendly_assert (rval != error_mark_node, 250);
2553         TREE_TYPE (rval) = void_type_node;
2554 #endif
2555         return rval;
2556       }
2557
2558     case CALL_EXPR:
2559       return build_object_call (arg1, arg2);
2560
2561     default:
2562       break;
2563     }
2564
2565   /* The comma operator can have void args.  */
2566   if (TREE_CODE (arg1) == OFFSET_REF)
2567     arg1 = resolve_offset_ref (arg1);
2568   if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
2569     arg2 = resolve_offset_ref (arg2);
2570   if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
2571     arg3 = resolve_offset_ref (arg3);
2572
2573   if (code == COND_EXPR)
2574     {
2575       if (arg2 == NULL_TREE
2576           || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
2577           || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
2578           || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
2579               && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
2580         goto builtin;
2581     }
2582   else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
2583            && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
2584     goto builtin;
2585
2586   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2587     arg2 = integer_zero_node;
2588
2589   if (arg2 && arg3)
2590     arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
2591                       (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
2592   else if (arg2)
2593     arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
2594   else
2595     arglist = build_scratch_list (NULL_TREE, arg1);
2596
2597   fns = lookup_function_nonclass (fnname, arglist);
2598
2599   if (fns && TREE_CODE (fns) == TREE_LIST)
2600     fns = TREE_VALUE (fns);
2601   for (; fns; fns = OVL_NEXT (fns))
2602     {
2603       tree fn = OVL_CURRENT (fns);
2604       if (TREE_CODE (fn) == TEMPLATE_DECL)
2605         {
2606           templates = scratch_tree_cons (NULL_TREE, fn, templates);
2607           candidates 
2608             = add_template_candidate (candidates, fn, NULL_TREE,
2609                                       arglist, TREE_TYPE (fnname),
2610                                       flags, DEDUCE_CALL); 
2611         }
2612       else
2613         candidates = add_function_candidate (candidates, fn, arglist, flags);
2614     }
2615
2616   if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
2617     {
2618       fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
2619       if (fns == error_mark_node)
2620         return fns;
2621     }
2622   else
2623     fns = NULL_TREE;
2624
2625   if (fns)
2626     {
2627       tree basetype = TREE_PURPOSE (fns);
2628       mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
2629       for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2630         {
2631           tree fn = OVL_CURRENT (fns);
2632           tree this_arglist;
2633
2634           if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
2635             this_arglist = mem_arglist;
2636           else
2637             this_arglist = arglist;
2638
2639           if (TREE_CODE (fn) == TEMPLATE_DECL)
2640             {
2641               /* A member template. */
2642               templates = scratch_tree_cons (NULL_TREE, fn, templates);
2643               candidates 
2644                 = add_template_candidate (candidates, fn, NULL_TREE,
2645                                           this_arglist,  TREE_TYPE (fnname),
2646                                           flags, DEDUCE_CALL); 
2647             }
2648           else
2649             candidates = add_function_candidate
2650               (candidates, fn, this_arglist, flags);
2651
2652           if (candidates) 
2653             candidates->basetype_path = basetype;
2654         }
2655     }
2656
2657   {
2658     tree args[3];
2659
2660     /* Rearrange the arguments for ?: so that add_builtin_candidate only has
2661        to know about two args; a builtin candidate will always have a first
2662        parameter of type bool.  We'll handle that in
2663        build_builtin_candidate.  */
2664     if (code == COND_EXPR)
2665       {
2666         args[0] = arg2;
2667         args[1] = arg3;
2668         args[2] = arg1;
2669       }
2670     else
2671       {
2672         args[0] = arg1;
2673         args[1] = arg2;
2674         args[2] = NULL_TREE;
2675       }
2676
2677     candidates = add_builtin_candidates
2678       (candidates, code, code2, fnname, args, flags);
2679   }
2680
2681   if (! any_viable (candidates))
2682     {
2683       switch (code)
2684         {
2685         case POSTINCREMENT_EXPR:
2686         case POSTDECREMENT_EXPR:
2687           /* Look for an `operator++ (int)'.  If they didn't have
2688              one, then we fall back to the old way of doing things.  */
2689           if (flags & LOOKUP_COMPLAIN)
2690             cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
2691                         fnname, opname_tab [code]);
2692           if (code == POSTINCREMENT_EXPR)
2693             code = PREINCREMENT_EXPR;
2694           else
2695             code = PREDECREMENT_EXPR;   
2696           return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
2697           
2698           /* The caller will deal with these.  */
2699         case ADDR_EXPR:
2700         case COMPOUND_EXPR:
2701         case COMPONENT_REF:
2702           return NULL_TREE;
2703
2704         default:
2705           break;
2706         }
2707       if (flags & LOOKUP_COMPLAIN)
2708         {
2709           op_error (code, code2, arg1, arg2, arg3, "no match");
2710           print_z_candidates (candidates);
2711         }
2712       return error_mark_node;
2713     }
2714   candidates = splice_viable (candidates);
2715   cand = tourney (candidates);
2716
2717   if (cand == 0)
2718     {
2719       if (flags & LOOKUP_COMPLAIN)
2720         {
2721           op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
2722           print_z_candidates (candidates);
2723         }
2724       return error_mark_node;
2725     }
2726
2727   if (TREE_CODE (cand->fn) == FUNCTION_DECL)
2728     {
2729       extern int warn_synth;
2730       if (warn_synth
2731           && fnname == ansi_opname[MODIFY_EXPR]
2732           && DECL_ARTIFICIAL (cand->fn)
2733           && candidates->next
2734           && ! candidates->next->next)
2735         {
2736           cp_warning ("using synthesized `%#D' for copy assignment",
2737                       cand->fn);
2738           cp_warning_at ("  where cfront would use `%#D'",
2739                          cand == candidates
2740                          ? candidates->next->fn
2741                          : candidates->fn);
2742         }
2743
2744       /* Pedantically, normal function declarations are never considered
2745          to refer to template instantiations, so we only do this with
2746          -fguiding-decls.  */ 
2747       if (flag_guiding_decls && templates && ! cand->template 
2748           && ! DECL_INITIAL (cand->fn)
2749           && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
2750         add_maybe_template (cand->fn, templates);
2751
2752       return build_over_call
2753         (cand,
2754          TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
2755          ? mem_arglist : arglist,
2756          LOOKUP_NORMAL);
2757     }
2758
2759   /* Check for comparison of different enum types.  */
2760   switch (code)
2761     {
2762     case GT_EXPR:
2763     case LT_EXPR:
2764     case GE_EXPR:
2765     case LE_EXPR:
2766     case EQ_EXPR:
2767     case NE_EXPR:
2768       if (flag_int_enum_equivalence == 0 
2769           && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 
2770           && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 
2771           && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
2772               != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
2773         {
2774           cp_warning ("comparison between `%#T' and `%#T'", 
2775                       TREE_TYPE (arg1), TREE_TYPE (arg2));
2776         }
2777       break;
2778     default:
2779       break;
2780     }
2781
2782   /* We need to strip any leading REF_BIND so that bitfields don't cause
2783      errors.  This should not remove any important conversions, because
2784      builtins don't apply to class objects directly.  */
2785   conv = TREE_VEC_ELT (cand->convs, 0);
2786   if (TREE_CODE (conv) == REF_BIND)
2787     conv = TREE_OPERAND (conv, 0);
2788   arg1 = convert_like (conv, arg1);
2789   if (arg2)
2790     arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
2791   if (arg3)
2792     arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
2793
2794 builtin:
2795   switch (code)
2796     {
2797     case MODIFY_EXPR:
2798       return build_modify_expr (arg1, code2, arg2);
2799
2800     case INDIRECT_REF:
2801       return build_indirect_ref (arg1, "unary *");
2802
2803     case PLUS_EXPR:
2804     case MINUS_EXPR:
2805     case MULT_EXPR:
2806     case TRUNC_DIV_EXPR:
2807     case GT_EXPR:
2808     case LT_EXPR:
2809     case GE_EXPR:
2810     case LE_EXPR:
2811     case EQ_EXPR:
2812     case NE_EXPR:
2813     case MAX_EXPR:
2814     case MIN_EXPR:
2815     case LSHIFT_EXPR:
2816     case RSHIFT_EXPR:
2817     case TRUNC_MOD_EXPR:
2818     case BIT_AND_EXPR:
2819     case BIT_IOR_EXPR:
2820     case BIT_XOR_EXPR:
2821     case TRUTH_ANDIF_EXPR:
2822     case TRUTH_ORIF_EXPR:
2823       return build_binary_op_nodefault (code, arg1, arg2, code);
2824
2825     case CONVERT_EXPR:
2826     case NEGATE_EXPR:
2827     case BIT_NOT_EXPR:
2828     case TRUTH_NOT_EXPR:
2829     case PREINCREMENT_EXPR:
2830     case POSTINCREMENT_EXPR:
2831     case PREDECREMENT_EXPR:
2832     case POSTDECREMENT_EXPR:
2833     case REALPART_EXPR:
2834     case IMAGPART_EXPR:
2835       return build_unary_op (code, arg1, candidates != 0);
2836
2837     case ARRAY_REF:
2838       return build_array_ref (arg1, arg2);
2839
2840     case COND_EXPR:
2841       return build_conditional_expr (arg1, arg2, arg3);
2842
2843     case MEMBER_REF:
2844       return build_m_component_ref
2845         (build_indirect_ref (arg1, NULL_PTR), arg2);
2846
2847       /* The caller will deal with these.  */
2848     case ADDR_EXPR:
2849     case COMPONENT_REF:
2850     case COMPOUND_EXPR:
2851       return NULL_TREE;
2852
2853     default:
2854       my_friendly_abort (367);
2855       return NULL_TREE;
2856     }
2857 }
2858
2859 /* Build up a call to operator new.  This has to be handled differently
2860    from other operators in the way lookup is handled; first members are
2861    considered, then globals.  CODE is either NEW_EXPR or VEC_NEW_EXPR.
2862    TYPE is the type to be created.  ARGS are any new-placement args.
2863    FLAGS are the usual overloading flags.  */
2864
2865 tree
2866 build_op_new_call (code, type, args, flags)
2867      enum tree_code code;
2868      tree type, args;
2869      int flags;
2870 {
2871   tree fnname = ansi_opname[code];
2872
2873   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
2874       && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
2875     {
2876       tree dummy = build1 (NOP_EXPR, build_pointer_type (type),
2877                            error_mark_node);
2878       dummy = build_indirect_ref (dummy, "new");
2879       return build_method_call (dummy, fnname, args, NULL_TREE, flags);
2880     }
2881   else
2882     return build_new_function_call 
2883       (lookup_function_nonclass (fnname, args), args);
2884 }
2885
2886 /* Build a call to operator delete.  This has to be handled very specially,
2887    because the restrictions on what signatures match are different from all
2888    other call instances.  For a normal delete, only a delete taking (void *)
2889    or (void *, size_t) is accepted.  For a placement delete, only an exact
2890    match with the placement new is accepted.
2891
2892    CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
2893    ADDR is the pointer to be deleted.  For placement delete, it is also
2894      used to determine what the corresponding new looked like.
2895    SIZE is the size of the memory block to be deleted.
2896    FLAGS are the usual overloading flags.  */
2897
2898 tree
2899 build_op_delete_call (code, addr, size, flags)
2900      enum tree_code code;
2901      tree addr, size;
2902      int flags;
2903 {
2904   tree fn, fns, fnname, fntype, argtypes, args, type;
2905   int placement;
2906
2907   if (addr == error_mark_node)
2908     return error_mark_node;
2909
2910   type = TREE_TYPE (TREE_TYPE (addr));
2911   fnname = ansi_opname[code];
2912
2913   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
2914     /* In [class.free]
2915
2916        If the result of the lookup is ambiguous or inaccessible, or if
2917        the lookup selects a placement deallocation function, the
2918        program is ill-formed.
2919   
2920        Therefore, we ask lookup_fnfields to complain ambout ambiguity.  */
2921     {
2922       fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
2923       if (fns == error_mark_node)
2924         return error_mark_node;
2925     }
2926   else
2927     fns = NULL_TREE;
2928
2929   if (fns)
2930     {
2931 #if 0
2932       /* It is unnecessary to wrap another TREE_LIST around it. (MvL) */
2933       /* Build this up like build_offset_ref does.  */
2934       fns = build_tree_list (error_mark_node, fns);
2935       TREE_TYPE (fns) = build_offset_type (type, unknown_type_node);
2936 #endif
2937     }
2938   else
2939     fns = lookup_name_nonclass (fnname);
2940
2941   /* We can recognize a placement delete because of LOOKUP_SPECULATIVELY;
2942      if we are doing placement delete we do nothing if we don't find a
2943      matching op delete.  */
2944   placement = !!(flags & LOOKUP_SPECULATIVELY);
2945   if (placement)
2946     {
2947       /* If placement, we are coming from build_new, and we know that addr
2948          is the allocation expression, so extract the info we need from it.
2949          Obviously, if the build_new process changes this may have to
2950          change as well.  */
2951
2952       /* The NOP_EXPR.  */
2953       tree t = TREE_OPERAND (addr, 1);
2954       /* The CALL_EXPR.  */
2955       t = TREE_OPERAND (t, 0);
2956       /* The function.  */
2957       argtypes = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2958       /* The second parm type.  */
2959       argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
2960       /* The second argument.  */
2961       args = TREE_CHAIN (TREE_OPERAND (t, 1));
2962
2963       /* Pull the dummy var out of the TARGET_EXPR for use in our call.  */
2964       addr = TREE_OPERAND (addr, 0);
2965     }
2966   else
2967     {
2968       /* First try it without the size argument.  */
2969       argtypes = void_list_node;
2970       args = NULL_TREE;
2971     }
2972
2973   argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
2974   fntype = build_function_type (void_type_node, argtypes);
2975
2976   /* Strip const and volatile from addr.  */
2977   if (type != TYPE_MAIN_VARIANT (type))
2978     addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
2979
2980   /* instantiate_type will always return a plain function; pretend it's
2981      overloaded.  */
2982   if (TREE_CODE (fns) == FUNCTION_DECL)
2983     fns = scratch_ovl_cons (fns, NULL_TREE);
2984
2985   fn = instantiate_type (fntype, fns, 0);
2986
2987   if (fn != error_mark_node)
2988     {
2989       if (TREE_CODE (fns) == TREE_LIST)
2990         /* Member functions.  */
2991         enforce_access (TREE_PURPOSE (fns), fn);
2992       return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
2993     }
2994
2995   if (placement)
2996     return NULL_TREE;
2997
2998   /* Normal delete; now try to find a match including the size argument.  */
2999   argtypes = tree_cons (NULL_TREE, ptr_type_node,
3000                         tree_cons (NULL_TREE, sizetype, void_list_node));
3001   fntype = build_function_type (void_type_node, argtypes);
3002
3003   fn = instantiate_type (fntype, fns, 0);
3004
3005   if (fn != error_mark_node)
3006     return build_function_call
3007       (fn, expr_tree_cons (NULL_TREE, addr,
3008                            build_expr_list (NULL_TREE, size)));
3009
3010   cp_error ("no suitable operator delete for `%T'", type);
3011   return error_mark_node;
3012 }
3013
3014 /* If the current scope isn't allowed to access DECL along
3015    BASETYPE_PATH, give an error.  */
3016
3017 void
3018 enforce_access (basetype_path, decl)
3019      tree basetype_path, decl;
3020 {
3021   tree access = compute_access (basetype_path, decl);
3022
3023   if (access == access_private_node)
3024     {
3025       cp_error_at ("`%+#D' is %s", decl, 
3026                    TREE_PRIVATE (decl) ? "private"
3027                    : "from private base class");
3028       error ("within this context");
3029     }
3030   else if (access == access_protected_node)
3031     {
3032       cp_error_at ("`%+#D' %s", decl,
3033                    TREE_PROTECTED (decl) ? "is protected"
3034                    : "has protected accessibility");
3035       error ("within this context");
3036     }
3037 }
3038
3039 /* Perform the conversions in CONVS on the expression EXPR.  */
3040
3041 static tree
3042 convert_like (convs, expr)
3043      tree convs, expr;
3044 {
3045   if (ICS_BAD_FLAG (convs)
3046       && TREE_CODE (convs) != USER_CONV
3047       && TREE_CODE (convs) != AMBIG_CONV)
3048     {
3049       tree t = convs; 
3050       for (; t; t = TREE_OPERAND (t, 0))
3051         {
3052           if (TREE_CODE (t) == USER_CONV)
3053             {
3054               expr = convert_like (t, expr);
3055               break;
3056             }
3057           else if (TREE_CODE (t) == AMBIG_CONV)
3058             return convert_like (t, expr);
3059           else if (TREE_CODE (t) == IDENTITY_CONV)
3060             break;
3061         }
3062       return convert_for_initialization
3063         (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
3064          "conversion", NULL_TREE, 0);
3065     }
3066
3067   switch (TREE_CODE (convs))
3068     {
3069     case USER_CONV:
3070       {
3071         struct z_candidate *cand
3072           = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3073         tree fn = cand->fn;
3074         tree args;
3075
3076         if (DECL_CONSTRUCTOR_P (fn))
3077           {
3078             tree t = build_int_2 (0, 0);
3079             TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3080
3081             args = build_scratch_list (NULL_TREE, expr);
3082             if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3083               args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3084             args = scratch_tree_cons (NULL_TREE, t, args);
3085           }
3086         else
3087           args = build_this (expr);
3088         expr = build_over_call (cand, args, LOOKUP_NORMAL);
3089
3090         /* If this is a constructor or a function returning an aggr type,
3091            we need to build up a TARGET_EXPR.  */
3092         if (DECL_CONSTRUCTOR_P (fn))
3093           expr = build_cplus_new (TREE_TYPE (convs), expr);
3094
3095         return expr;
3096       }
3097     case IDENTITY_CONV:
3098       if (type_unknown_p (expr))
3099         expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3100       if (TREE_READONLY_DECL_P (expr))
3101         expr = decl_constant_value (expr);
3102       return expr;
3103     case AMBIG_CONV:
3104       /* Call build_user_type_conversion again for the error.  */
3105       return build_user_type_conversion
3106         (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3107
3108     default:
3109       break;
3110     };
3111
3112   expr = convert_like (TREE_OPERAND (convs, 0), expr);
3113   if (expr == error_mark_node)
3114     return error_mark_node;
3115
3116   switch (TREE_CODE (convs))
3117     {
3118     case RVALUE_CONV:
3119       if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
3120         return expr;
3121       /* else fall through */
3122     case BASE_CONV:
3123       return build_user_type_conversion
3124         (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3125     case REF_BIND:
3126       return convert_to_reference
3127         (TREE_TYPE (convs), expr,
3128          CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
3129          error_mark_node);
3130     case LVALUE_CONV:
3131       return decay_conversion (expr);
3132
3133     default:
3134       break;
3135     }
3136   return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3137                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3138 }
3139
3140 static tree
3141 convert_default_arg (type, arg)
3142      tree type, arg;
3143 {
3144   arg = break_out_target_exprs (arg);
3145
3146   if (TREE_CODE (arg) == CONSTRUCTOR)
3147     {
3148       arg = digest_init (type, arg, 0);
3149       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3150                                         "default argument", 0, 0);
3151     }
3152   else
3153     {
3154       /* This could get clobbered by the following call.  */
3155       if (TREE_HAS_CONSTRUCTOR (arg))
3156         arg = copy_node (arg);
3157
3158       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3159                                         "default argument", 0, 0);
3160 #ifdef PROMOTE_PROTOTYPES
3161       if ((TREE_CODE (type) == INTEGER_TYPE
3162            || TREE_CODE (type) == ENUMERAL_TYPE)
3163           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3164         arg = default_conversion (arg);
3165 #endif
3166     }
3167
3168   return arg;
3169 }
3170
3171 static tree
3172 build_over_call (cand, args, flags)
3173      struct z_candidate *cand;
3174      tree args;
3175      int flags;
3176 {
3177   tree fn = cand->fn;
3178   tree convs = cand->convs;
3179   tree converted_args = NULL_TREE;
3180   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3181   tree conv, arg, val;
3182   int i = 0;
3183   int is_method = 0;
3184
3185   /* Give any warnings we noticed during overload resolution.  */
3186   if (cand->warnings)
3187     for (val = cand->warnings; val; val = TREE_CHAIN (val))
3188       joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
3189
3190   if (DECL_FUNCTION_MEMBER_P (fn))
3191     enforce_access (cand->basetype_path, fn);
3192
3193   if (args && TREE_CODE (args) != TREE_LIST)
3194     args = build_scratch_list (NULL_TREE, args);
3195   arg = args;
3196
3197   /* The implicit parameters to a constructor are not considered by overload
3198      resolution, and must be of the proper type.  */
3199   if (DECL_CONSTRUCTOR_P (fn))
3200     {
3201       converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
3202       arg = TREE_CHAIN (arg);
3203       parm = TREE_CHAIN (parm);
3204       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3205         {
3206           converted_args = expr_tree_cons
3207             (NULL_TREE, TREE_VALUE (arg), converted_args);
3208           arg = TREE_CHAIN (arg);
3209           parm = TREE_CHAIN (parm);
3210         }
3211     }      
3212   /* Bypass access control for 'this' parameter.  */
3213   else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3214     {
3215       tree parmtype = TREE_VALUE (parm);
3216       tree argtype = TREE_TYPE (TREE_VALUE (arg));
3217       if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
3218         {
3219           int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype))
3220                     < TYPE_VOLATILE (TREE_TYPE (argtype)));
3221           int dc = (TYPE_READONLY (TREE_TYPE (parmtype))
3222                     < TYPE_READONLY (TREE_TYPE (argtype)));
3223           char *p = (dv && dc ? "const and volatile"
3224                               : dc ? "const" : dv ? "volatile" : "");
3225
3226           cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s",
3227                       TREE_TYPE (argtype), fn, p);
3228         }
3229       converted_args = expr_tree_cons
3230         (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
3231          converted_args);
3232       parm = TREE_CHAIN (parm);
3233       arg = TREE_CHAIN (arg);
3234       ++i;
3235       is_method = 1;
3236     }
3237
3238   for (; arg && parm;
3239        parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3240     {
3241       tree type = TREE_VALUE (parm);
3242
3243       conv = TREE_VEC_ELT (convs, i);
3244       if (ICS_BAD_FLAG (conv))
3245         {
3246           tree t = conv;
3247           val = TREE_VALUE (arg);
3248
3249           for (; t; t = TREE_OPERAND (t, 0))
3250             {
3251               if (TREE_CODE (t) == USER_CONV
3252                   || TREE_CODE (t) == AMBIG_CONV)
3253                 {
3254                   val = convert_like (t, val);
3255                   break;
3256                 }
3257               else if (TREE_CODE (t) == IDENTITY_CONV)
3258                 break;
3259             }
3260           val = convert_for_initialization
3261             (NULL_TREE, type, val, LOOKUP_NORMAL,
3262              "argument passing", fn, i - is_method);
3263         }
3264       else
3265         val = convert_like (conv, TREE_VALUE (arg));
3266
3267 #ifdef PROMOTE_PROTOTYPES
3268       if ((TREE_CODE (type) == INTEGER_TYPE
3269            || TREE_CODE (type) == ENUMERAL_TYPE)
3270           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3271         val = default_conversion (val);
3272 #endif
3273       converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3274     }
3275
3276   /* Default arguments */
3277   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3278     {
3279       tree arg = TREE_PURPOSE (parm);
3280
3281       if (DECL_TEMPLATE_INFO (fn))
3282         {
3283           /* This came from a template.  Instantiate the default arg here,
3284              not in tsubst.  In the case of something like:
3285
3286                template <class T>
3287                struct S {
3288                  static T t();
3289                  void f(T = t());
3290                };
3291
3292              we must be careful to do name lookup in the scope of
3293              S<T>, rather than in the current class.  */
3294           if (DECL_CLASS_SCOPE_P (fn))
3295             pushclass (DECL_REAL_CONTEXT (fn), 2);
3296
3297           arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
3298
3299           if (DECL_CLASS_SCOPE_P (fn))
3300             popclass (0);
3301         }
3302       converted_args = expr_tree_cons
3303         (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
3304          converted_args);
3305     }
3306
3307   /* Ellipsis */
3308   for (; arg; arg = TREE_CHAIN (arg))
3309     {
3310       val = TREE_VALUE (arg);
3311
3312       if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
3313           && (TYPE_PRECISION (TREE_TYPE (val))
3314               < TYPE_PRECISION (double_type_node)))
3315         /* Convert `float' to `double'.  */
3316         val = cp_convert (double_type_node, val);
3317       else if (IS_AGGR_TYPE (TREE_TYPE (val))
3318                && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
3319         cp_warning ("cannot pass objects of type `%T' through `...'",
3320                     TREE_TYPE (val));
3321       else
3322         /* Convert `short' and `char' to full-size `int'.  */
3323         val = default_conversion (val);
3324
3325       converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
3326     }
3327
3328   converted_args = nreverse (converted_args);
3329
3330   /* Avoid actually calling copy constructors and copy assignment operators,
3331      if possible.  */
3332   if (DECL_CONSTRUCTOR_P (fn)
3333       && TREE_VEC_LENGTH (convs) == 1
3334       && copy_args_p (fn))
3335     {
3336       tree targ;
3337       arg = TREE_CHAIN (converted_args);
3338       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3339         arg = TREE_CHAIN (arg);
3340       arg = TREE_VALUE (arg);
3341
3342       /* Pull out the real argument, disregarding const-correctness.  */
3343       targ = arg;
3344       while (TREE_CODE (targ) == NOP_EXPR
3345              || TREE_CODE (targ) == NON_LVALUE_EXPR
3346              || TREE_CODE (targ) == CONVERT_EXPR)
3347         targ = TREE_OPERAND (targ, 0);
3348       if (TREE_CODE (targ) == ADDR_EXPR)
3349         {
3350           targ = TREE_OPERAND (targ, 0);
3351           if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
3352                            TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1))
3353             targ = NULL_TREE;
3354         }
3355       else
3356         targ = NULL_TREE;
3357
3358       if (targ)
3359         arg = targ;
3360       else
3361         arg = build_indirect_ref (arg, 0);
3362
3363       /* [class.copy]: the copy constructor is implicitly defined even if
3364          the implementation elided its use.  */
3365       if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
3366         mark_used (fn);
3367
3368       /* If we're creating a temp and we already have one, don't create a
3369          new one.  If we're not creating a temp but we get one, use
3370          INIT_EXPR to collapse the temp into our target.  Otherwise, if the
3371          ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
3372          temp or an INIT_EXPR otherwise.  */
3373       if (integer_zerop (TREE_VALUE (args)))
3374         {
3375           if (! real_lvalue_p (arg))
3376             return arg;
3377           else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3378             {
3379               val = build (VAR_DECL, DECL_CONTEXT (fn));
3380               layout_decl (val, 0);
3381               val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
3382               TREE_SIDE_EFFECTS (val) = 1;
3383               return val;
3384             }
3385         }
3386       else if (! real_lvalue_p (arg)
3387                || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
3388         {
3389           tree to = stabilize_reference
3390             (build_indirect_ref (TREE_VALUE (args), 0));
3391
3392           /* Don't copy the padding byte; it might not have been allocated
3393              if to is a base subobject.  */
3394           if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3395             return build (COMPOUND_EXPR, TREE_TYPE (to),
3396                           cp_convert (void_type_node, arg), to);
3397
3398           val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
3399           TREE_SIDE_EFFECTS (val) = 1;
3400           return build_unary_op (ADDR_EXPR, val, 0);
3401         }
3402     }
3403   else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
3404            && copy_args_p (fn)
3405            && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CLASS_CONTEXT (fn)))
3406     {
3407       tree to = stabilize_reference
3408         (build_indirect_ref (TREE_VALUE (converted_args), 0));
3409
3410       arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
3411
3412       /* Don't copy the padding byte; it might not have been allocated
3413          if to is a base subobject.  */
3414       if (is_empty_class (DECL_CLASS_CONTEXT (fn)))
3415         return build (COMPOUND_EXPR, TREE_TYPE (to),
3416                       cp_convert (void_type_node, arg), to);
3417
3418       val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
3419       TREE_SIDE_EFFECTS (val) = 1;
3420       return val;
3421     }
3422
3423   mark_used (fn);
3424
3425   if (DECL_CONTEXT (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
3426     return build_signature_method_call (fn, converted_args);
3427   else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3428     {
3429       tree t, *p = &TREE_VALUE (converted_args);
3430       tree binfo = get_binfo
3431         (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
3432       *p = convert_pointer_to_real (binfo, *p);
3433       if (TREE_SIDE_EFFECTS (*p))
3434         *p = save_expr (*p);
3435       t = build_pointer_type (TREE_TYPE (fn));
3436       fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
3437       TREE_TYPE (fn) = t;
3438     }
3439   else if (DECL_INLINE (fn))
3440     fn = inline_conversion (fn);
3441   else
3442     fn = build_addr_func (fn);
3443
3444   /* Recognize certain built-in functions so we can make tree-codes
3445      other than CALL_EXPR.  We do this when it enables fold-const.c
3446      to do something useful.  */
3447
3448   if (TREE_CODE (fn) == ADDR_EXPR
3449       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3450       && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
3451     switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
3452       {
3453       case BUILT_IN_ABS:
3454       case BUILT_IN_LABS:
3455       case BUILT_IN_FABS:
3456         if (converted_args == 0)
3457           return integer_zero_node;
3458         return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
3459       default:
3460         break;
3461       }
3462
3463   fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3464   if (TREE_TYPE (fn) == void_type_node)
3465     return fn;
3466   fn = require_complete_type (fn);
3467   if (IS_AGGR_TYPE (TREE_TYPE (fn)))
3468     fn = build_cplus_new (TREE_TYPE (fn), fn);
3469   return convert_from_reference (fn);
3470 }
3471
3472 static tree
3473 build_new_method_call (instance, name, args, basetype_path, flags)
3474      tree instance, name, args, basetype_path;
3475      int flags;
3476 {
3477   struct z_candidate *candidates = 0, *cand;
3478   tree explicit_targs = NULL_TREE;
3479   tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
3480   tree pretty_name;
3481   tree user_args = args;
3482   tree templates = NULL_TREE;
3483   int template_only = 0;
3484
3485   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3486     {
3487       explicit_targs = TREE_OPERAND (name, 1);
3488       name = TREE_OPERAND (name, 0);
3489       if (TREE_CODE (name) == TEMPLATE_DECL)
3490         name = DECL_NAME (name);
3491       template_only = 1;
3492     }
3493
3494   /* If there is an extra argument for controlling virtual bases,
3495      remove it for error reporting.  */
3496   if (flags & LOOKUP_HAS_IN_CHARGE)
3497     user_args = TREE_CHAIN (args);
3498
3499   args = resolve_args (args);
3500
3501   if (args == error_mark_node)
3502     return error_mark_node;
3503
3504   if (instance == NULL_TREE)
3505     basetype = BINFO_TYPE (basetype_path);
3506   else
3507     {
3508       if (TREE_CODE (instance) == OFFSET_REF)
3509         instance = resolve_offset_ref (instance);
3510       if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
3511         instance = convert_from_reference (instance);
3512       basetype = TREE_TYPE (instance);
3513
3514       /* XXX this should be handled before we get here.  */
3515       if (! IS_AGGR_TYPE (basetype)
3516           && ! (TYPE_LANG_SPECIFIC (basetype)
3517                 && (IS_SIGNATURE_POINTER (basetype)
3518                     || IS_SIGNATURE_REFERENCE (basetype))))
3519         {
3520           if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
3521             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
3522                       name, instance, basetype);
3523
3524           return error_mark_node;
3525         }
3526
3527       /* If `instance' is a signature pointer/reference and `name' is
3528          not a constructor, we are calling a signature member function.
3529          In that case set the `basetype' to the signature type.  */
3530       if ((IS_SIGNATURE_POINTER (basetype)
3531            || IS_SIGNATURE_REFERENCE (basetype))
3532           && TYPE_IDENTIFIER (basetype) != name)
3533         basetype = SIGNATURE_TYPE (basetype);
3534     }
3535
3536   if (basetype_path == NULL_TREE)
3537     basetype_path = TYPE_BINFO (basetype);
3538
3539   if (instance)
3540     {
3541       instance_ptr = build_this (instance);
3542
3543       if (! template_only)
3544         {
3545           /* XXX this should be handled before we get here.  */
3546           fns = build_field_call (basetype_path, instance_ptr, name, args);
3547           if (fns)
3548             return fns;
3549         }
3550     }
3551   else
3552     {
3553       instance_ptr = build_int_2 (0, 0);
3554       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
3555     }
3556
3557   pretty_name
3558     = (name == ctor_identifier ? constructor_name (basetype) : name);
3559
3560   fns = lookup_fnfields (basetype_path, name, 1);
3561
3562   if (fns == error_mark_node)
3563     return error_mark_node;
3564   if (fns)
3565     {
3566       tree fn = TREE_VALUE (fns);
3567       if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3568           && ! (flags & LOOKUP_HAS_IN_CHARGE))
3569         {
3570           flags |= LOOKUP_HAS_IN_CHARGE;
3571           args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
3572         }
3573       mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
3574       for (; fn; fn = OVL_NEXT (fn))
3575         {
3576           tree t = OVL_CURRENT (fn);
3577           tree this_arglist;
3578
3579           /* We can end up here for copy-init of same or base class.  */
3580           if (name == ctor_identifier
3581               && (flags & LOOKUP_ONLYCONVERTING)
3582               && DECL_NONCONVERTING_P (t))
3583             continue;
3584           if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
3585             this_arglist = mem_args;
3586           else
3587             this_arglist = args;
3588
3589           if (TREE_CODE (t) == TEMPLATE_DECL)
3590             {
3591               /* A member template. */
3592               templates = scratch_tree_cons (NULL_TREE, t, templates);
3593               candidates = 
3594                 add_template_candidate (candidates, t, explicit_targs,
3595                                         this_arglist,
3596                                         TREE_TYPE (name), flags, DEDUCE_CALL); 
3597             }
3598           else if (! template_only)
3599             candidates = add_function_candidate (candidates, t,
3600                                                  this_arglist, flags);
3601
3602           if (candidates)
3603             candidates->basetype_path = TREE_PURPOSE (fns);
3604         }
3605     }
3606
3607   if (! any_viable (candidates))
3608     {
3609       /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
3610       if (flags & LOOKUP_SPECULATIVELY)
3611         return NULL_TREE;
3612       cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype,
3613                 pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr)));
3614       print_z_candidates (candidates);
3615       return error_mark_node;
3616     }
3617   candidates = splice_viable (candidates);
3618   cand = tourney (candidates);
3619
3620   if (cand == 0)
3621     {
3622       cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
3623                 user_args);
3624       print_z_candidates (candidates);
3625       return error_mark_node;
3626     }
3627
3628   if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
3629       && instance == current_class_ref
3630       && DECL_CONSTRUCTOR_P (current_function_decl)
3631       && ! (flags & LOOKUP_NONVIRTUAL)
3632       && value_member (cand->fn, get_abstract_virtuals (basetype)))
3633     cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
3634   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3635       && TREE_CODE (instance_ptr) == NOP_EXPR
3636       && TREE_OPERAND (instance_ptr, 0) == error_mark_node)
3637     cp_error ("cannot call member function `%D' without object", cand->fn);
3638
3639   if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
3640       && ((instance == current_class_ref && (dtor_label || ctor_label))
3641           || resolves_to_fixed_type_p (instance, 0)))
3642     flags |= LOOKUP_NONVIRTUAL;
3643
3644   /* Pedantically, normal function declarations are never considered
3645      to refer to template instantiations, so we only do this with
3646      -fguiding-decls.  */ 
3647   if (flag_guiding_decls && templates && ! cand->template 
3648       && ! DECL_INITIAL (cand->fn))
3649     add_maybe_template (cand->fn, templates);
3650
3651   return build_over_call
3652     (cand,
3653      TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
3654      flags);
3655 }
3656
3657 /* Compare two implicit conversion sequences that differ only in their
3658    qualification conversion.  Subroutine of compare_ics.  */
3659
3660 static int
3661 compare_qual (ics1, ics2)
3662      tree ics1, ics2;
3663 {
3664   tree to1 = TREE_TYPE (ics1);
3665   tree to2 = TREE_TYPE (ics2);
3666
3667   if (TYPE_PTRMEMFUNC_P (to1))
3668     to1 = TYPE_PTRMEMFUNC_FN_TYPE (to1);
3669   if (TYPE_PTRMEMFUNC_P (to2))
3670     to2 = TYPE_PTRMEMFUNC_FN_TYPE (to2);
3671
3672   to1 = TREE_TYPE (to1);
3673   to2 = TREE_TYPE (to2);
3674
3675   if (TREE_CODE (to1) == OFFSET_TYPE)
3676     {
3677       to1 = TREE_TYPE (to1);
3678       to2 = TREE_TYPE (to2);
3679     }
3680
3681   if (TYPE_READONLY (to1) >= TYPE_READONLY (to2)
3682       && TYPE_VOLATILE (to1) > TYPE_VOLATILE (to2))
3683     return -1;
3684   else 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   return 0;
3694 }
3695
3696 /* Determine whether standard conversion sequence ICS1 is a proper
3697    subsequence of ICS2.  We assume that a conversion of the same code
3698    between the same types indicates a subsequence.  */
3699
3700 static int
3701 is_subseq (ics1, ics2)
3702      tree ics1, ics2;
3703 {
3704   /* Do not consider lvalue transformations here.  */
3705   if (TREE_CODE (ics2) == RVALUE_CONV
3706       || TREE_CODE (ics2) == LVALUE_CONV)
3707     return 0;
3708
3709   for (;; ics2 = TREE_OPERAND (ics2, 0))
3710     {
3711       if (TREE_CODE (ics2) == TREE_CODE (ics1)
3712           && comptypes (TREE_TYPE (ics2), TREE_TYPE (ics1), 1)
3713           && comptypes (TREE_TYPE (TREE_OPERAND (ics2, 0)),
3714                         TREE_TYPE (TREE_OPERAND (ics1, 0)), 1))
3715         return 1;
3716
3717       if (TREE_CODE (ics2) == USER_CONV
3718           || TREE_CODE (ics2) == AMBIG_CONV
3719           || TREE_CODE (ics2) == IDENTITY_CONV)
3720         return 0;
3721     }
3722 }
3723
3724 /* Compare two implicit conversion sequences according to the rules set out in
3725    [over.ics.rank].  Return values:
3726
3727       1: ics1 is better than ics2
3728      -1: ics2 is better than ics1
3729       0: ics1 and ics2 are indistinguishable */
3730
3731 static int
3732 compare_ics (ics1, ics2)
3733      tree ics1, ics2;
3734 {
3735   tree main1, main2;
3736
3737   if (TREE_CODE (ics1) == QUAL_CONV)
3738     main1 = TREE_OPERAND (ics1, 0);
3739   else
3740     main1 = ics1;
3741
3742   if (TREE_CODE (ics2) == QUAL_CONV)
3743     main2 = TREE_OPERAND (ics2, 0);
3744   else
3745     main2 = ics2;
3746
3747   /* Conversions for `this' are PTR_CONVs, but we compare them as though
3748      they were REF_BINDs.  */
3749   if (ICS_THIS_FLAG (ics1))
3750     {
3751       tree t = main1;
3752       if (TREE_CODE (t) == PTR_CONV)
3753         t = TREE_OPERAND (t, 0);
3754       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3755       t = build_conv (REF_BIND, TREE_TYPE (ics1), t);
3756       ICS_STD_RANK (t) = ICS_STD_RANK (main1);
3757       main1 = ics1 = t;
3758     }
3759   if (ICS_THIS_FLAG (ics2))
3760     {
3761       tree t = main2;
3762       if (TREE_CODE (t) == PTR_CONV)
3763         t = TREE_OPERAND (t, 0);
3764       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
3765       t = build_conv (REF_BIND, TREE_TYPE (ics2), t);
3766       ICS_STD_RANK (t) = ICS_STD_RANK (main2);
3767       main2 = ics2 = t;
3768     }
3769
3770   if (ICS_RANK (ics1) > ICS_RANK (ics2))
3771     return -1;
3772   else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3773     return 1;
3774
3775   if (ICS_RANK (ics1) == BAD_RANK)
3776     {
3777       if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
3778           || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3779         return -1;
3780       else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
3781                || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3782         return 1;
3783
3784       /* else fall through */
3785     }
3786
3787   /* User-defined  conversion sequence U1 is a better conversion sequence
3788      than another user-defined conversion sequence U2 if they contain the
3789      same user-defined conversion operator or constructor and if the sec-
3790      ond standard conversion sequence of U1 is  better  than  the  second
3791      standard conversion sequence of U2.  */
3792
3793   if (ICS_USER_FLAG (ics1))
3794     {
3795       tree t1, t2;
3796
3797       for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3798         if (TREE_CODE (t1) == AMBIG_CONV)
3799           return 0;
3800       for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3801         if (TREE_CODE (t2) == AMBIG_CONV)
3802           return 0;
3803
3804       if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3805         return 0;
3806       else if (ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3807         return -1;
3808       else if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3809         return 1;
3810
3811       /* else fall through */
3812     }
3813
3814 #if 0 /* Handled by ranking */
3815   /* A conversion that is not a conversion of a pointer,  or  pointer  to
3816      member,  to  bool  is  better than another conversion that is such a
3817      conversion.  */
3818 #endif
3819
3820   if (TREE_CODE (main1) != TREE_CODE (main2))
3821     {
3822       /* ...if S1  is  a  proper  subsequence  of  S2  */
3823       if (is_subseq (main1, main2))
3824         return 1;
3825       if (is_subseq (main2, main1))
3826         return -1;
3827       return 0;
3828     }
3829
3830   if (TREE_CODE (main1) == PTR_CONV || TREE_CODE (main1) == PMEM_CONV
3831       || TREE_CODE (main1) == REF_BIND || TREE_CODE (main1) == BASE_CONV)
3832     {
3833       tree to1 = TREE_TYPE (main1);
3834       tree from1 = TREE_TYPE (TREE_OPERAND (main1, 0));
3835       tree to2 = TREE_TYPE (main2);
3836       tree from2 = TREE_TYPE (TREE_OPERAND (main2, 0));
3837       int distf, distt;
3838
3839       /* Standard conversion sequence S1 is a better conversion sequence than
3840          standard conversion sequence S2 if...
3841
3842          S1 and S2 differ only in their qualification conversion  and  they
3843          yield types identical except for cv-qualifiers and S2 adds all the
3844          qualifiers that S1 adds (and in the same places) and S2  adds  yet
3845          more  cv-qualifiers  than  S1,  or the similar case with reference
3846          binding15).  */
3847       if (TREE_CODE (main1) == REF_BIND)
3848         {
3849           if (TYPE_MAIN_VARIANT (TREE_TYPE (to1))
3850               == TYPE_MAIN_VARIANT (TREE_TYPE (to2)))
3851             return compare_qual (ics1, ics2);
3852         }
3853       else if (TREE_CODE (main1) != BASE_CONV && from1 == from2 && to1 == to2)
3854         return compare_qual (ics1, ics2);
3855         
3856       if (TYPE_PTRMEMFUNC_P (to1))
3857         {
3858           to1 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to1)));
3859           from1 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from1)));
3860         }
3861       else if (TREE_CODE (main1) != BASE_CONV)
3862         {
3863           to1 = TREE_TYPE (to1);
3864           if (TREE_CODE (main1) != REF_BIND)
3865             from1 = TREE_TYPE (from1);
3866
3867           if (TREE_CODE (to1) == OFFSET_TYPE)
3868             {
3869               to1 = TYPE_OFFSET_BASETYPE (to1);
3870               from1 = TYPE_OFFSET_BASETYPE (from1);
3871             }
3872         }
3873
3874       if (TYPE_PTRMEMFUNC_P (to2))
3875         {
3876           to2 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to2)));
3877           from2 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from2)));
3878         }
3879       else if (TREE_CODE (main1) != BASE_CONV)
3880         {
3881           to2 = TREE_TYPE (to2);
3882           if (TREE_CODE (main1) != REF_BIND)
3883             from2 = TREE_TYPE (from2);
3884
3885           if (TREE_CODE (to2) == OFFSET_TYPE)
3886             {
3887               to2 = TYPE_OFFSET_BASETYPE (to2);
3888               from2 = TYPE_OFFSET_BASETYPE (from2);
3889             }
3890         }
3891
3892       if (! (IS_AGGR_TYPE (from1) && IS_AGGR_TYPE (from2)))
3893         return 0;
3894
3895       /* The sense of pmem conversions is reversed from that of the other
3896          conversions.  */
3897       if (TREE_CODE (main1) == PMEM_CONV)
3898         {
3899           tree t = from1; from1 = from2; from2 = t;
3900           t = to1; to1 = to2; to2 = t;
3901         }
3902
3903       distf = get_base_distance (from1, from2, 0, 0);
3904       if (distf == -1)
3905         {
3906           distf = -get_base_distance (from2, from1, 0, 0);
3907           if (distf == 1)
3908             return 0;
3909         }
3910
3911       /* If class B is derived directly or indirectly from class A,
3912          conver- sion of B* to A* is better than conversion of B* to
3913          void*, and conversion of A* to void* is better than
3914          conversion of B* to void*.  */
3915
3916       if (TREE_CODE (to1) == VOID_TYPE && TREE_CODE (to2) == VOID_TYPE)
3917         {
3918           if (distf > 0)
3919             return 1;
3920           else if (distf < 0)
3921             return -1;
3922         }
3923       else if (TREE_CODE (to2) == VOID_TYPE && IS_AGGR_TYPE (to1)
3924                && DERIVED_FROM_P (to1, from1))
3925         return 1;
3926       else if (TREE_CODE (to1) == VOID_TYPE && IS_AGGR_TYPE (to2)
3927                && DERIVED_FROM_P (to2, from2))
3928         return -1;
3929
3930       if (! (IS_AGGR_TYPE (to1) && IS_AGGR_TYPE (to2)))
3931         return 0;
3932
3933       /* If  class B is derived directly or indirectly from class A and class
3934          C is derived directly or indirectly from B */
3935
3936       distt = get_base_distance (to1, to2, 0, 0);
3937       if (distt == -1)
3938         {
3939           distt = -get_base_distance (to2, to1, 0, 0);
3940           if (distt == 1)
3941             return 0;
3942         }
3943
3944       /* --conversion of C* to B* is better than conversion of C* to A*, */
3945       if (distf == 0)
3946         {
3947           if (distt > 0)
3948             return -1;
3949           else if (distt < 0)
3950             return 1;
3951         }
3952       /* --conversion of B* to A* is better than conversion of C* to A*, */
3953       else if (distt == 0)
3954         {
3955           if (distf > 0)
3956             return 1;
3957           else if (distf < 0)
3958             return -1;
3959         }
3960     }
3961   else if (TREE_CODE (TREE_TYPE (main1)) == POINTER_TYPE
3962            || TYPE_PTRMEMFUNC_P (TREE_TYPE (main1)))
3963     {
3964       if (TREE_TYPE (main1) == TREE_TYPE (main2))
3965         return compare_qual (ics1, ics2);
3966
3967 #if 0 /* This is now handled by making identity better than anything else.  */
3968       /* existing practice, not WP-endorsed: const char * -> const char *
3969          is better than char * -> const char *.  (jason 6/29/96) */
3970       if (TREE_TYPE (ics1) == TREE_TYPE (ics2))
3971         return -compare_qual (main1, main2);
3972 #endif
3973     }
3974
3975   return 0;
3976 }
3977
3978 /* The source type for this standard conversion sequence.  */
3979
3980 static tree
3981 source_type (t)
3982      tree t;
3983 {
3984   for (;; t = TREE_OPERAND (t, 0))
3985     {
3986       if (TREE_CODE (t) == USER_CONV
3987           || TREE_CODE (t) == AMBIG_CONV
3988           || TREE_CODE (t) == IDENTITY_CONV)
3989         return TREE_TYPE (t);
3990     }
3991   my_friendly_abort (1823);
3992 }
3993
3994 /* Note a warning about preferring WINNER to LOSER.  We do this by storing
3995    a pointer to LOSER and re-running joust to produce the warning if WINNER
3996    is actually used.  */
3997
3998 static void
3999 add_warning (winner, loser)
4000      struct z_candidate *winner, *loser;
4001 {
4002   winner->warnings = expr_tree_cons (NULL_PTR,
4003                                      build_expr_ptr_wrapper (loser),
4004                                      winner->warnings);
4005 }
4006
4007 /* Compare two candidates for overloading as described in
4008    [over.match.best].  Return values:
4009
4010       1: cand1 is better than cand2
4011      -1: cand2 is better than cand1
4012       0: cand1 and cand2 are indistinguishable */
4013
4014 static int
4015 joust (cand1, cand2, warn)
4016      struct z_candidate *cand1, *cand2;
4017      int warn;
4018 {
4019   int winner = 0;
4020   int i, off1 = 0, off2 = 0, len;
4021
4022   /* Candidates that involve bad conversions are always worse than those
4023      that don't.  */
4024   if (cand1->viable > cand2->viable)
4025     return 1;
4026   if (cand1->viable < cand2->viable)
4027     return -1;
4028
4029   /* a viable function F1
4030      is defined to be a better function than another viable function F2  if
4031      for  all arguments i, ICSi(F1) is not a worse conversion sequence than
4032      ICSi(F2), and then */
4033
4034   /* for some argument j, ICSj(F1) is a better conversion  sequence  than
4035      ICSj(F2) */
4036
4037   /* For comparing static and non-static member functions, we ignore the
4038      implicit object parameter of the non-static function.  The WP says to
4039      pretend that the static function has an object parm, but that won't
4040      work with operator overloading.  */
4041   len = TREE_VEC_LENGTH (cand1->convs);
4042   if (len != TREE_VEC_LENGTH (cand2->convs))
4043     {
4044       if (DECL_STATIC_FUNCTION_P (cand1->fn)
4045           && ! DECL_STATIC_FUNCTION_P (cand2->fn))
4046         off2 = 1;
4047       else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
4048                && DECL_STATIC_FUNCTION_P (cand2->fn))
4049         {
4050           off1 = 1;
4051           --len;
4052         }
4053       else
4054         my_friendly_abort (42);
4055     }
4056
4057   for (i = 0; i < len; ++i)
4058     {
4059       tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
4060       tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
4061       int comp = compare_ics (t1, t2);
4062
4063       if (comp != 0)
4064         {
4065           if (warn_sign_promo
4066               && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
4067               && TREE_CODE (t1) == STD_CONV
4068               && TREE_CODE (t2) == STD_CONV
4069               && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
4070               && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
4071               && (TYPE_PRECISION (TREE_TYPE (t1))
4072                   == TYPE_PRECISION (TREE_TYPE (t2)))
4073               && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
4074                   || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
4075                       == ENUMERAL_TYPE)))
4076             {
4077               tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
4078               tree type1, type2;
4079               struct z_candidate *w, *l;
4080               if (comp > 0)
4081                 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
4082                   w = cand1, l = cand2;
4083               else
4084                 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
4085                   w = cand2, l = cand1;
4086
4087               if (warn)
4088                 {
4089                   cp_warning ("passing `%T' chooses `%T' over `%T'",
4090                               type, type1, type2);
4091                   cp_warning ("  in call to `%D'", w->fn);
4092                 }
4093               else
4094                 add_warning (w, l);
4095             }
4096
4097           if (winner && comp != winner)
4098             {
4099               winner = 0;
4100               goto tweak;
4101             }
4102           winner = comp;
4103         }
4104     }
4105
4106   /* warn about confusing overload resolution */
4107   if (winner && cand1->second_conv
4108       && ! DECL_CONSTRUCTOR_P (cand1->fn)
4109       && ! DECL_CONSTRUCTOR_P (cand2->fn))
4110     {
4111       int comp = compare_ics (cand1->second_conv, cand2->second_conv);
4112       if (comp && comp != winner)
4113         {
4114           struct z_candidate *w, *l;
4115           if (winner == 1)
4116             w = cand1, l = cand2;
4117           else
4118             w = cand2, l = cand1;
4119           if (warn)
4120             {
4121               cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
4122               cp_warning ("  for conversion from `%T' to `%T'",
4123                           TREE_TYPE (source_type (TREE_VEC_ELT (w->convs, 0))),
4124                           TREE_TYPE (w->second_conv));
4125               cp_warning ("  because conversion sequence for `this' argument is better");
4126             }
4127           else
4128             add_warning (w, l);
4129         }
4130     }
4131
4132   if (winner)
4133     return winner;
4134
4135   /* or, if not that,
4136      F1 is a non-template function and F2 is a template function */
4137
4138   if (! cand1->template && cand2->template)
4139     return 1;
4140   else if (cand1->template && ! cand2->template)
4141     return -1;
4142   else if (cand1->template && cand2->template)
4143     winner = more_specialized
4144       (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
4145        NULL_TREE);
4146
4147   /* or, if not that,
4148      the  context  is  an  initialization by user-defined conversion (see
4149      _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
4150      sequence  from  the return type of F1 to the destination type (i.e.,
4151      the type of the entity being initialized)  is  a  better  conversion
4152      sequence  than the standard conversion sequence from the return type
4153      of F2 to the destination type.  */
4154
4155   if (! winner && cand1->second_conv)
4156     winner = compare_ics (cand1->second_conv, cand2->second_conv);
4157
4158   /* If the built-in candidates are the same, arbitrarily pick one.  */
4159   if (! winner && cand1->fn == cand2->fn
4160       && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4161     {
4162       for (i = 0; i < len; ++i)
4163         if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4164                          TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
4165           break;
4166       if (i == TREE_VEC_LENGTH (cand1->convs))
4167         return 1;
4168
4169       /* Kludge around broken overloading rules whereby
4170          Integer a, b; test ? a : b; is ambiguous, since there's a builtin
4171          that takes references and another that takes values.  */
4172       if (cand1->fn == ansi_opname[COND_EXPR])
4173         {
4174           tree c1 = TREE_VEC_ELT (cand1->convs, 1);
4175           tree c2 = TREE_VEC_ELT (cand2->convs, 1);
4176           tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
4177           tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
4178
4179           if (comptypes (t1, t2, 1))
4180             {
4181               if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
4182                 return 1;
4183               if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
4184                 return -1;
4185             }
4186         }
4187     }
4188
4189 tweak:
4190
4191   /* Extension: If the worst conversion for one candidate is worse than the
4192      worst conversion for the other, take the first.  */
4193   if (! winner && ! pedantic)
4194     {
4195       int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
4196
4197       for (i = 0; i < len; ++i)
4198         {
4199           if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
4200             rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
4201           if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
4202             rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
4203         }
4204
4205       if (rank1 < rank2)
4206         return 1;
4207       if (rank1 > rank2)
4208         return -1;
4209     }
4210
4211   return winner;
4212 }
4213
4214 /* Given a list of candidates for overloading, find the best one, if any.
4215    This algorithm has a worst case of O(2n) (winner is last), and a best
4216    case of O(n/2) (totally ambiguous); much better than a sorting
4217    algorithm.  */
4218
4219 static struct z_candidate *
4220 tourney (candidates)
4221      struct z_candidate *candidates;
4222 {
4223   struct z_candidate *champ = candidates, *challenger;
4224   int fate;
4225
4226   /* Walk through the list once, comparing each current champ to the next
4227      candidate, knocking out a candidate or two with each comparison.  */
4228
4229   for (challenger = champ->next; challenger; )
4230     {
4231       fate = joust (champ, challenger, 0);
4232       if (fate == 1)
4233         challenger = challenger->next;
4234       else
4235         {
4236           if (fate == 0)
4237             {
4238               champ = challenger->next;
4239               if (champ == 0)
4240                 return 0;
4241             }
4242           else
4243             champ = challenger;
4244
4245           challenger = champ->next;
4246         }
4247     }
4248
4249   /* Make sure the champ is better than all the candidates it hasn't yet
4250      been compared to.  This may do one more comparison than necessary.  Oh
4251      well.  */
4252
4253   for (challenger = candidates; challenger != champ;
4254        challenger = challenger->next)
4255     {
4256       fate = joust (champ, challenger, 0);
4257       if (fate != 1)
4258         return 0;
4259     }
4260
4261   return champ;
4262 }
4263
4264 int
4265 can_convert (to, from)
4266      tree to, from;
4267 {
4268   tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
4269   return (t && ! ICS_BAD_FLAG (t));
4270 }
4271
4272 int
4273 can_convert_arg (to, from, arg)
4274      tree to, from, arg;
4275 {
4276   tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
4277   return (t && ! ICS_BAD_FLAG (t));
4278 }