OSDN Git Service

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