OSDN Git Service

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