OSDN Git Service

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