OSDN Git Service

75e7b5615344f9c5ba7b7633cacf405c9c413171
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
1 /* Functions related to invoking methods and overloaded functions.
2    Copyright (C) 1987, 1992, 1993, 1994, 1995 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 "class.h"
31 #include "output.h"
32 #include "flags.h"
33
34 #include "obstack.h"
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
37
38 extern void sorry ();
39
40 extern int inhibit_warnings;
41 extern tree ctor_label, dtor_label;
42
43 /* Compute the ease with which a conversion can be performed
44    between an expected and the given type.  */
45
46 static struct harshness_code convert_harshness PROTO((register tree, register tree, tree));
47
48 #define EVIL_RETURN(ARG)        ((ARG).code = EVIL_CODE, (ARG))
49 #define STD_RETURN(ARG)         ((ARG).code = STD_CODE, (ARG))
50 #define QUAL_RETURN(ARG)        ((ARG).code = QUAL_CODE, (ARG))
51 #define TRIVIAL_RETURN(ARG)     ((ARG).code = TRIVIAL_CODE, (ARG))
52 #define ZERO_RETURN(ARG)        ((ARG).code = 0, (ARG))
53
54 /* Ordering function for overload resolution.  Compare two candidates
55    by gross quality.  */
56
57 int
58 rank_for_overload (x, y)
59      struct candidate *x, *y;
60 {
61   if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
62     return y->h.code - x->h.code;
63   if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
64     return -1;
65
66   /* This is set by compute_conversion_costs, for calling a non-const
67      member function from a const member function.  */
68   if ((y->harshness[0].code & CONST_CODE) ^ (x->harshness[0].code & CONST_CODE))
69     return y->harshness[0].code - x->harshness[0].code;
70
71   if (y->h.code & STD_CODE)
72     {
73       if (x->h.code & STD_CODE)
74         return y->h.distance - x->h.distance;
75       return 1;
76     }
77   if (x->h.code & STD_CODE)
78     return -1;
79
80   return y->h.code - x->h.code;
81 }
82
83 /* Compare two candidates, argument by argument.  */
84
85 static int
86 rank_for_ideal (x, y)
87      struct candidate *x, *y;
88 {
89   int i;
90
91   if (x->h_len != y->h_len)
92     abort ();
93
94   for (i = 0; i < x->h_len; i++)
95     {
96       if (y->harshness[i].code - x->harshness[i].code)
97         return y->harshness[i].code - x->harshness[i].code;
98       if ((y->harshness[i].code & STD_CODE)
99           && (y->harshness[i].distance - x->harshness[i].distance))
100         return y->harshness[i].distance - x->harshness[i].distance;
101
102       /* They're both the same code.  Now see if we're dealing with an
103          integral promotion that needs a finer grain of accuracy.  */
104       if (y->harshness[0].code & PROMO_CODE
105           && (y->harshness[i].int_penalty ^ x->harshness[i].int_penalty))
106         return y->harshness[i].int_penalty - x->harshness[i].int_penalty;
107     }
108   return 0;
109 }
110
111 /* TYPE is the type we wish to convert to.  PARM is the parameter
112    we have to work with.  We use a somewhat arbitrary cost function
113    to measure this conversion.  */
114
115 static struct harshness_code
116 convert_harshness (type, parmtype, parm)
117      register tree type, parmtype;
118      tree parm;
119 {
120   struct harshness_code h;
121   register enum tree_code codel;
122   register enum tree_code coder;
123   int lvalue;
124
125   h.code = 0;
126   h.distance = 0;
127   h.int_penalty = 0;
128
129 #ifdef GATHER_STATISTICS
130   n_convert_harshness++;
131 #endif
132
133   if (TREE_CODE (parmtype) == REFERENCE_TYPE)
134     {
135       if (parm)
136         parm = convert_from_reference (parm);
137       parmtype = TREE_TYPE (parmtype);
138       lvalue = 1;
139     }
140   else if (parm)
141     lvalue = lvalue_p (parm);
142   else
143     lvalue = 0;
144
145   if (TYPE_PTRMEMFUNC_P (type))
146     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
147   if (TYPE_PTRMEMFUNC_P (parmtype))
148     parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
149
150   codel = TREE_CODE (type);
151   coder = TREE_CODE (parmtype);
152
153   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
154     return ZERO_RETURN (h);
155
156   if (coder == ERROR_MARK)
157     return EVIL_RETURN (h);
158
159   if (codel == REFERENCE_TYPE)
160     {
161       tree ttl, ttr;
162       int constp = parm ? TREE_READONLY (parm) : TYPE_READONLY (parmtype);
163       int volatilep = (parm ? TREE_THIS_VOLATILE (parm)
164                        : TYPE_VOLATILE (parmtype));
165       register tree intype = TYPE_MAIN_VARIANT (parmtype);
166       register enum tree_code form = TREE_CODE (intype);
167       int penalty = 0;
168
169       ttl = TREE_TYPE (type);
170
171       /* Only allow const reference binding if we were given a parm to deal
172          with, since it isn't really a conversion.  This is a hack to
173          prevent build_type_conversion from finding this conversion, but
174          still allow overloading to find it.  */
175       if (! lvalue && ! (parm && TYPE_READONLY (ttl)))
176         return EVIL_RETURN (h);
177
178       if ((TYPE_READONLY (ttl) < constp)
179           || (TYPE_VOLATILE (ttl) < volatilep))
180         return EVIL_RETURN (h);
181
182       /* When passing a non-const argument into a const reference, dig it a
183          little, so a non-const reference is preferred over this one.  */
184       penalty = ((TYPE_READONLY (ttl) > constp)
185                  + (TYPE_VOLATILE (ttl) > volatilep));
186
187       ttl = TYPE_MAIN_VARIANT (ttl);
188
189       if (form == OFFSET_TYPE)
190         {
191           intype = TREE_TYPE (intype);
192           form = TREE_CODE (intype);
193         }
194
195       ttr = intype;
196
197       if (TREE_CODE (ttl) == ARRAY_TYPE && TREE_CODE (ttr) == ARRAY_TYPE)
198         {
199           if (comptypes (ttl, ttr, 1))
200             return ZERO_RETURN (h);
201           return EVIL_RETURN (h);
202         }
203
204       h = convert_harshness (ttl, ttr, NULL_TREE);
205       if (penalty && h.code == 0)
206         {
207           h.code = QUAL_CODE;
208           h.int_penalty = penalty;
209         }
210       return h;
211     }
212
213   if (codel == POINTER_TYPE && fntype_p (parmtype))
214     {
215       tree p1, p2;
216       struct harshness_code h1, h2;
217
218       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
219       type = TREE_TYPE (type);
220
221       if (coder == POINTER_TYPE)
222         {
223           parmtype = TREE_TYPE (parmtype);
224           coder = TREE_CODE (parmtype);
225         }
226
227       if (coder != TREE_CODE (type))
228         return EVIL_RETURN (h);
229
230       if (type != parmtype && coder == METHOD_TYPE)
231         {
232           tree ttl = TYPE_METHOD_BASETYPE (type);
233           tree ttr = TYPE_METHOD_BASETYPE (parmtype);
234
235           int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
236           if (b_or_d < 0)
237             {
238               b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
239               if (b_or_d < 0)
240                 return EVIL_RETURN (h);
241               h.distance = -b_or_d;
242             }
243           else
244             h.distance = b_or_d;
245           h.code = STD_CODE;
246
247           type = build_function_type
248             (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
249           parmtype = build_function_type
250             (TREE_TYPE (parmtype), TREE_CHAIN (TYPE_ARG_TYPES (parmtype)));
251         }
252
253       /* We allow the default conversion between function type
254          and pointer-to-function type for free.  */
255       if (comptypes (type, parmtype, 1))
256         return h;
257
258       if (pedantic)
259         return EVIL_RETURN (h);
260
261       /* Compare return types.  */
262       p1 = TREE_TYPE (type);
263       p2 = TREE_TYPE (parmtype);
264       h2 = convert_harshness (p1, p2, NULL_TREE);
265       if (h2.code & EVIL_CODE)
266         return h2;
267
268       h1.code = TRIVIAL_CODE;
269       h1.distance = 0;
270
271       if (h2.distance != 0)
272         {
273           tree binfo;
274
275           /* This only works for pointers.  */
276           if (TREE_CODE (p1) != POINTER_TYPE
277               && TREE_CODE (p1) != REFERENCE_TYPE)
278             return EVIL_RETURN (h);
279
280           p1 = TREE_TYPE (p1);
281           p2 = TREE_TYPE (p2);
282           /* Don't die if we happen to be dealing with void*.  */
283           if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
284             return EVIL_RETURN (h);
285           if (h2.distance < 0)
286             binfo = get_binfo (p2, p1, 0);
287           else
288             binfo = get_binfo (p1, p2, 0);
289
290           if (! BINFO_OFFSET_ZEROP (binfo))
291             {
292 #if 0
293               static int explained = 0;
294               if (h2.distance < 0)
295                 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p2, p1);
296               else
297                 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p1, p2);
298
299               if (! explained++)
300                 sorry ("(because pointer values change during conversion)");
301 #endif
302               return EVIL_RETURN (h);
303             }
304         }
305
306       h1.code |= h2.code;
307       if (h2.distance > h1.distance)
308         h1.distance = h2.distance;
309
310       p1 = TYPE_ARG_TYPES (type);
311       p2 = TYPE_ARG_TYPES (parmtype);
312       while (p1 && TREE_VALUE (p1) != void_type_node
313              && p2 && TREE_VALUE (p2) != void_type_node)
314         {
315           h2 = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2),
316                                        NULL_TREE);
317           if (h2.code & EVIL_CODE)
318             return h2;
319
320           if (h2.distance)
321             {
322               /* This only works for pointers and references.  */
323               if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
324                   && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
325                 return EVIL_RETURN (h);
326               h2.distance = - h2.distance;
327             }
328
329           h1.code |= h2.code;
330           if (h2.distance > h1.distance)
331             h1.distance = h2.distance;
332           p1 = TREE_CHAIN (p1);
333           p2 = TREE_CHAIN (p2);
334         }
335       if (p1 == p2)
336         return h1;
337       if (p2)
338         {
339           if (p1)
340             return EVIL_RETURN (h);
341           h1.code |= ELLIPSIS_CODE;
342           return h1;
343         }
344       if (p1)
345         {
346           if (TREE_PURPOSE (p1) == NULL_TREE)
347             h1.code |= EVIL_CODE;
348           return h1;
349         }
350     }
351   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
352     {
353       tree ttl, ttr;
354
355       /* Get to the OFFSET_TYPE that this might be.  */
356       type = TREE_TYPE (type);
357
358       if (coder != TREE_CODE (type))
359         return EVIL_RETURN (h);
360
361       ttl = TYPE_OFFSET_BASETYPE (type);
362       ttr = TYPE_OFFSET_BASETYPE (parmtype);
363
364       if (ttl == ttr)
365         h.code = 0;
366       else
367         {
368           int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
369           if (b_or_d < 0)
370             {
371               b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
372               if (b_or_d < 0)
373                 return EVIL_RETURN (h);
374               h.distance = -b_or_d;
375             }
376           else
377             h.distance = b_or_d;
378           h.code = STD_CODE;
379         }
380
381       /* Now test the OFFSET_TYPE's target compatibility.  */
382       type = TREE_TYPE (type);
383       parmtype = TREE_TYPE (parmtype);
384     }
385
386   if (coder == UNKNOWN_TYPE)
387     {
388       if (codel == FUNCTION_TYPE
389           || codel == METHOD_TYPE
390           || (codel == POINTER_TYPE
391               && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
392                   || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
393         return TRIVIAL_RETURN (h);
394       return EVIL_RETURN (h);
395     }
396
397   if (coder == VOID_TYPE)
398     return EVIL_RETURN (h);
399
400   if (codel == BOOLEAN_TYPE)
401     {
402       if (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE)
403         return STD_RETURN (h);
404       else if (coder == POINTER_TYPE || coder == OFFSET_TYPE)
405         {
406           /* Make this worse than any conversion to another pointer.
407              FIXME this is how I think the language should work, but it may not
408              end up being how the language is standardized (jason 1/30/95).  */
409           h.distance = 32767;
410           return STD_RETURN (h);
411         }
412       return EVIL_RETURN (h);
413     }
414
415   if (INTEGRAL_CODE_P (codel))
416     {
417       /* Control equivalence of ints an enums.  */
418
419       if (codel == ENUMERAL_TYPE
420           && flag_int_enum_equivalence == 0)
421         {
422           /* Enums can be converted to ints, but not vice-versa.  */
423           if (coder != ENUMERAL_TYPE
424               || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
425             return EVIL_RETURN (h);
426         }
427
428       /* else enums and ints (almost) freely interconvert.  */
429
430       if (INTEGRAL_CODE_P (coder))
431         {
432           if (TYPE_MAIN_VARIANT (type)
433               == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
434             {
435               h.code = PROMO_CODE;
436             }
437           else
438             h.code = STD_CODE;
439             
440           return h;
441         }
442       else if (coder == REAL_TYPE)
443         {
444           h.code = STD_CODE;
445           h.distance = 0;
446           return h;
447         }
448     }
449
450   if (codel == REAL_TYPE)
451     {
452       if (coder == REAL_TYPE)
453         {
454           if (TYPE_MAIN_VARIANT (type)
455               == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
456             h.code = PROMO_CODE;
457           else
458             h.code = STD_CODE;
459             
460           return h;
461         }
462       else if (INTEGRAL_CODE_P (coder))
463         {
464           h.code = STD_CODE;
465           h.distance = 0;
466           return h;
467         }
468     }
469
470   /* Convert arrays which have not previously been converted.  */
471   if (coder == ARRAY_TYPE)
472     {
473       coder = POINTER_TYPE;
474       if (parm)
475         {
476           parm = decay_conversion (parm);
477           parmtype = TREE_TYPE (parm);
478         }
479       else
480         parmtype = build_pointer_type (TREE_TYPE (parmtype));
481     }
482
483   /* Conversions among pointers */
484   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
485     {
486       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
487       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
488       int penalty = 4 * (ttl != ttr);
489
490       /* Anything converts to void *.  Since this may be `const void *'
491          (etc.) use VOID_TYPE instead of void_type_node.  Otherwise, the
492          targets must be the same, except that we do allow (at some cost)
493          conversion between signed and unsigned pointer types.  */
494
495       if ((TREE_CODE (ttl) == METHOD_TYPE
496            || TREE_CODE (ttl) == FUNCTION_TYPE)
497           && TREE_CODE (ttl) == TREE_CODE (ttr))
498         {
499           if (comptypes (ttl, ttr, -1))
500             {
501               h.code = penalty ? STD_CODE : 0;
502               h.distance =  0;
503             }
504           else
505             h.code = EVIL_CODE;
506           return h;
507         }
508
509 #if 1
510       if (TREE_CODE (ttl) != VOID_TYPE
511           && (TREE_CODE (ttr) != VOID_TYPE || !parm || !null_ptr_cst_p (parm)))
512         {
513           if (comp_target_types (type, parmtype, 1) <= 0)
514             return EVIL_RETURN (h);
515         }
516 #else
517       if (!(TREE_CODE (ttl) == VOID_TYPE
518             || TREE_CODE (ttr) == VOID_TYPE
519             || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
520                 && (ttl = unsigned_type (ttl),
521                     ttr = unsigned_type (ttr),
522                     penalty = 10, 0))
523             || (comp_target_types (ttl, ttr, 0) > 0)))
524         return EVIL_RETURN (h);
525 #endif
526
527       if (ttr == ttl)
528         {
529           tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
530
531           h.code = 0;
532           /* Note conversion from `T*' to `const T*',
533                                or `T*' to `volatile T*'.  */
534           if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
535               || (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
536             h.code = EVIL_CODE;
537           else if ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
538                    || (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2)))
539             h.code |= QUAL_CODE;
540
541           h.distance = 0;
542           return h;
543         }
544
545
546       if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
547         {
548           int b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
549           if (b_or_d < 0)
550             {
551               b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
552               if (b_or_d < 0)
553                 return EVIL_RETURN (h);
554               h.distance = -b_or_d;
555             }
556           else
557             h.distance = b_or_d;
558           h.code = STD_CODE;
559           return h;
560         }
561
562       /* If converting from a `class*' to a `void*', make it
563          less favorable than any inheritance relationship.  */
564       if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
565         {
566           h.code = STD_CODE;
567           h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
568           return h;
569         }
570
571       h.code = penalty ? STD_CODE : PROMO_CODE;
572       /* Catch things like `const char *' -> `const void *'
573          vs `const char *' -> `void *'.  */
574       if (ttl != ttr)
575         {
576           tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
577           if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
578               || (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
579             h.code = EVIL_CODE;
580           else if ((TYPE_READONLY (tmp1) > TREE_READONLY (tmp2))
581                    || (TYPE_VOLATILE (tmp1) > TYPE_VOLATILE (tmp2)))
582             h.code |= QUAL_CODE;
583         }
584       return h;
585     }
586
587   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
588     {
589       /* This is not a bad match, but don't let it beat
590          integer-enum combinations.  */
591       if (parm && integer_zerop (parm))
592         {
593           h.code = STD_CODE;
594           h.distance = 0;
595           return h;
596         }
597     }
598
599   /* C++: Since the `this' parameter of a signature member function
600      is represented as a signature pointer to handle default implementations
601      correctly, we can have the case that `type' is a signature pointer
602      while `parmtype' is a pointer to a signature table.  We don't really
603      do any conversions in this case, so just return 0.  */
604
605   if (codel == RECORD_TYPE && coder == POINTER_TYPE
606       && IS_SIGNATURE_POINTER (type) && IS_SIGNATURE (TREE_TYPE (parmtype)))
607     return ZERO_RETURN (h);
608
609   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
610     {
611       int b_or_d = get_base_distance (type, parmtype, 0, (tree*)0);
612       if (b_or_d < 0)
613         {
614           b_or_d = get_base_distance (parmtype, type, 0, (tree*)0);
615           if (b_or_d < 0)
616             return EVIL_RETURN (h);
617           h.distance = -b_or_d;
618         }
619       else
620         h.distance = b_or_d;
621       h.code = STD_CODE;
622       return h;
623     }
624   return EVIL_RETURN (h);
625 }
626
627 /* A clone of build_type_conversion for checking user-defined conversions in
628    overload resolution.  */
629
630 static int
631 user_harshness (type, parmtype)
632      register tree type, parmtype;
633 {
634   tree conv;
635   tree winner = NULL_TREE;
636   int code;
637
638   {
639     tree typename = build_typename_overload (type);
640     if (lookup_fnfields (TYPE_BINFO (parmtype), typename, 0))
641       return 0;
642   }
643                         
644   for (conv = lookup_conversions (parmtype); conv; conv = TREE_CHAIN (conv))
645     {
646       struct harshness_code tmp;
647       tree cand = TREE_VALUE (conv);
648
649       if (winner && winner == cand)
650         continue;
651
652       tmp = convert_harshness (type, TREE_TYPE (TREE_TYPE (cand)), NULL_TREE);
653       if ((tmp.code < USER_CODE) && (tmp.distance >= 0))
654         {
655           if (winner)
656             return EVIL_CODE;
657           else
658             {
659               winner = cand;
660               code = tmp.code;
661             }
662         }
663     }
664
665   if (winner)
666     return code;
667
668   return -1;
669 }
670
671 #ifdef DEBUG_MATCHING
672 static char *
673 print_harshness (h)
674      struct harshness_code *h;
675 {
676   static char buf[1024];
677   char tmp[1024];
678
679   bzero (buf, 1024 * sizeof (char));
680   strcat (buf, "codes=[");
681   if (h->code & EVIL_CODE)
682     strcat (buf, "EVIL");
683   if (h->code & CONST_CODE)
684     strcat (buf, " CONST");
685   if (h->code & ELLIPSIS_CODE)
686     strcat (buf, " ELLIPSIS");
687   if (h->code & USER_CODE)
688     strcat (buf, " USER");
689   if (h->code & STD_CODE)
690     strcat (buf, " STD");
691   if (h->code & PROMO_CODE)
692     strcat (buf, " PROMO");
693   if (h->code & QUAL_CODE)
694     strcat (buf, " QUAL");
695   if (h->code & TRIVIAL_CODE)
696     strcat (buf, " TRIVIAL");
697   if (buf[0] == '\0')
698     strcat (buf, "0");
699
700   sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
701
702   strcat (buf, tmp);
703
704   return buf;
705 }
706 #endif
707
708 /* Algorithm: For each argument, calculate how difficult it is to
709    make FUNCTION accept that argument.  If we can easily tell that
710    FUNCTION won't be acceptable to one of the arguments, then we
711    don't need to compute the ease of converting the other arguments,
712    since it will never show up in the intersection of all arguments'
713    favorite functions.
714
715    Conversions between builtin and user-defined types are allowed, but
716    no function involving such a conversion is preferred to one which
717    does not require such a conversion.  Furthermore, such conversions
718    must be unique.  */
719
720 void
721 compute_conversion_costs (function, tta_in, cp, arglen)
722      tree function;
723      tree tta_in;
724      struct candidate *cp;
725      int arglen;
726 {
727   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
728   tree ttf = ttf_in;
729   tree tta = tta_in;
730
731   /* Start out with no strikes against.  */
732   int evil_strikes = 0;
733   int ellipsis_strikes = 0;
734   int user_strikes = 0;
735   int b_or_d_strikes = 0;
736   int easy_strikes = 0;
737
738   int strike_index = 0, win;
739   struct harshness_code lose;
740   extern int cp_silent;
741
742 #ifdef GATHER_STATISTICS
743   n_compute_conversion_costs++;
744 #endif
745
746 #ifndef DEBUG_MATCHING
747   /* We don't emit any warnings or errors while trying out each candidate.  */
748   cp_silent = 1;
749 #endif
750
751   cp->function = function;
752   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
753   cp->u.bad_arg = 0;            /* optimistic!  */
754
755   cp->h.code = 0;
756   cp->h.distance = 0;
757   cp->h.int_penalty = 0;
758   bzero ((char *) cp->harshness,
759          (cp->h_len + 1) * sizeof (struct harshness_code));
760
761   while (ttf && tta)
762     {
763       struct harshness_code h;
764
765       if (ttf == void_list_node)
766         break;
767
768       if (type_unknown_p (TREE_VALUE (tta)))
769         {         
770           /* Must perform some instantiation here.  */
771           tree rhs = TREE_VALUE (tta);
772           tree lhstype = TREE_VALUE (ttf);
773
774           /* Keep quiet about possible contravariance violations.  */
775           int old_inhibit_warnings = inhibit_warnings;
776           inhibit_warnings = 1;
777
778           /* @@ This is to undo what `grokdeclarator' does to
779              parameter types.  It really should go through
780              something more general.  */
781
782           TREE_TYPE (tta) = unknown_type_node;
783           rhs = instantiate_type (lhstype, rhs, 0);
784           inhibit_warnings = old_inhibit_warnings;
785
786           if (TREE_CODE (rhs) == ERROR_MARK)
787             h.code = EVIL_CODE;
788           else
789             h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
790         }
791       else
792         {
793 #ifdef DEBUG_MATCHING
794           static tree old_function = NULL_TREE;
795
796           if (!old_function || function != old_function)
797             {
798               cp_error ("trying %D", function);
799               old_function = function;
800             }
801
802           cp_error ("      doing (%T) %E against arg %T",
803                     TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
804                     TREE_VALUE (ttf));
805 #endif
806
807           h = convert_harshness (TREE_VALUE (ttf),
808                                  TREE_TYPE (TREE_VALUE (tta)),
809                                  TREE_VALUE (tta));
810
811 #ifdef DEBUG_MATCHING
812           cp_error ("     evaluated %s", print_harshness (&h));
813 #endif
814         }
815
816       cp->harshness[strike_index] = h;
817       if ((h.code & EVIL_CODE)
818           || ((h.code & STD_CODE) && h.distance < 0))
819         {
820           cp->u.bad_arg = strike_index;
821           evil_strikes = 1;
822         }
823      else if (h.code & ELLIPSIS_CODE)
824        ellipsis_strikes += 1;
825 #if 0
826       /* This is never set by `convert_harshness'.  */
827       else if (h.code & USER_CODE)
828         {
829           user_strikes += 1;
830         }
831 #endif
832       else
833         {
834           if ((h.code & STD_CODE) && h.distance)
835             {
836               if (h.distance > b_or_d_strikes)
837                 b_or_d_strikes = h.distance;
838             }
839           else
840             easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
841           cp->h.code |= h.code;
842           /* Make sure we communicate this.  */
843           cp->h.int_penalty += h.int_penalty;
844         }
845
846       ttf = TREE_CHAIN (ttf);
847       tta = TREE_CHAIN (tta);
848       strike_index += 1;
849     }
850
851   if (tta)
852     {
853       /* ran out of formals, and parmlist is fixed size.  */
854       if (ttf /* == void_type_node */)
855         {
856           cp->h.code = EVIL_CODE;
857           cp->u.bad_arg = -1;
858           cp_silent = 0;
859           return;
860         }
861       else
862         {
863           struct harshness_code h;
864           int l = list_length (tta);
865           ellipsis_strikes += l;
866           h.code = ELLIPSIS_CODE;
867           h.distance = 0;
868           h.int_penalty = 0;
869           for (; l; --l)
870             cp->harshness[strike_index++] = h;
871         }
872     }
873   else if (ttf && ttf != void_list_node)
874     {
875       /* ran out of actuals, and no defaults.  */
876       if (TREE_PURPOSE (ttf) == NULL_TREE)
877         {
878           cp->h.code = EVIL_CODE;
879           cp->u.bad_arg = -2;
880           cp_silent = 0;
881           return;
882         }
883       /* Store index of first default.  */
884       cp->harshness[arglen].distance = strike_index+1;
885     }
886   else
887     cp->harshness[arglen].distance = 0;
888
889   /* Argument list lengths work out, so don't need to check them again.  */
890   if (evil_strikes)
891     {
892       /* We do not check for derived->base conversions here, since in
893          no case would they give evil strike counts, unless such conversions
894          are somehow ambiguous.  */
895
896       /* See if any user-defined conversions apply.
897          But make sure that we do not loop.  */
898       static int dont_convert_types = 0;
899
900       if (dont_convert_types)
901         {
902           cp->h.code = EVIL_CODE;
903           cp_silent = 0;
904           return;
905         }
906
907       win = 0;                  /* Only get one chance to win.  */
908       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
909       tta = tta_in;
910       strike_index = 0;
911       evil_strikes = 0;
912
913       while (ttf && tta)
914         {
915           if (ttf == void_list_node)
916             break;
917
918           lose = cp->harshness[strike_index];
919           if ((lose.code & EVIL_CODE)
920               || ((lose.code & STD_CODE) && lose.distance < 0))
921             {
922               tree actual_type = TREE_TYPE (TREE_VALUE (tta));
923               tree formal_type = TREE_VALUE (ttf);
924               int extra_conversions = 0;
925
926               dont_convert_types = 1;
927
928               if (TREE_CODE (formal_type) == REFERENCE_TYPE)
929                 formal_type = TREE_TYPE (formal_type);
930               if (TREE_CODE (actual_type) == REFERENCE_TYPE)
931                 actual_type = TREE_TYPE (actual_type);
932
933               if (formal_type != error_mark_node
934                   && actual_type != error_mark_node)
935                 {
936                   formal_type = complete_type (TYPE_MAIN_VARIANT (formal_type));
937                   actual_type = complete_type (TYPE_MAIN_VARIANT (actual_type));
938
939                   if (TYPE_HAS_CONSTRUCTOR (formal_type))
940                     {
941                       /* If it has a constructor for this type,
942                          try to use it.  */
943                       /* @@ There is no way to save this result yet, so
944                          success is a NULL_TREE for now.  */
945                       if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
946                           != error_mark_node)
947                         win++;
948                     }
949                   if (TYPE_LANG_SPECIFIC (actual_type)
950                       && TYPE_HAS_CONVERSION (actual_type))
951                     {
952                       int extra = user_harshness (formal_type, actual_type);
953
954                       if (extra == EVIL_CODE)
955                         win += 2;
956                       else if (extra >= 0)
957                         {
958                           win++;
959                           extra_conversions = extra;
960                         }
961                     }
962                 }
963               dont_convert_types = 0;
964
965               if (win == 1)
966                 {
967                   user_strikes += 1;
968                   cp->harshness[strike_index].code
969                     = USER_CODE | (extra_conversions ? STD_CODE : 0);
970                   win = 0;
971                 }
972               else
973                 {
974                   if (cp->u.bad_arg > strike_index)
975                     cp->u.bad_arg = strike_index;
976
977                   evil_strikes = win ? 2 : 1;
978                   break;
979                 }
980             }
981
982           ttf = TREE_CHAIN (ttf);
983           tta = TREE_CHAIN (tta);
984           strike_index += 1;
985         }
986     }
987
988   /* Const member functions get a small penalty because defaulting
989      to const is less useful than defaulting to non-const.  */
990   /* This is bogus, it does not correspond to anything in the ARM.
991      This code will be fixed when this entire section is rewritten
992      to conform to the ARM.  (mrs)  */
993   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
994     {
995       tree this_parm = TREE_VALUE (ttf_in);
996
997       if (TREE_CODE (this_parm) == RECORD_TYPE  /* Is `this' a sig ptr?  */
998             ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
999             : TYPE_READONLY (TREE_TYPE (this_parm)))
1000         {
1001           cp->harshness[0].code |= TRIVIAL_CODE;
1002           ++easy_strikes;
1003         }
1004       else
1005         {
1006           /* Calling a non-const member function from a const member function
1007              is probably invalid, but for now we let it only draw a warning.
1008              We indicate that such a mismatch has occurred by setting the
1009              harshness to a maximum value.  */
1010           if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1011               && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1012             cp->harshness[0].code |= CONST_CODE;
1013         }
1014     }
1015
1016   if (evil_strikes)
1017     cp->h.code = EVIL_CODE;
1018   if (ellipsis_strikes)
1019     cp->h.code |= ELLIPSIS_CODE;
1020   if (user_strikes)
1021     cp->h.code |= USER_CODE;
1022   cp_silent = 0;
1023 #ifdef DEBUG_MATCHING
1024   cp_error ("final eval %s", print_harshness (&cp->h));
1025 #endif
1026 }
1027
1028 /* Subroutine of ideal_candidate.  See if X or Y is a better match
1029    than the other.  */
1030
1031 static int
1032 strictly_better (x, y)
1033      unsigned short x, y;
1034 {
1035   unsigned short xor;
1036
1037   if (x == y)
1038     return 0;
1039
1040   xor = x ^ y;
1041   if (xor >= x || xor >= y)
1042     return 1;
1043   return 0;
1044 }
1045
1046 /* When one of several possible overloaded functions and/or methods
1047    can be called, choose the best candidate for overloading.
1048
1049    BASETYPE is the context from which we start method resolution
1050    or NULL if we are comparing overloaded functions.
1051    CANDIDATES is the array of candidates we have to choose from.
1052    N_CANDIDATES is the length of CANDIDATES.
1053    PARMS is a TREE_LIST of parameters to the function we'll ultimately
1054    choose.  It is modified in place when resolving methods.  It is not
1055    modified in place when resolving overloaded functions.
1056    LEN is the length of the parameter list.  */
1057
1058 static struct candidate *
1059 ideal_candidate (candidates, n_candidates, len)
1060      struct candidate *candidates;
1061      int n_candidates;
1062      int len;
1063 {
1064   struct candidate *cp = candidates+n_candidates;
1065   int i, j = -1, best_code;
1066
1067   /* For each argument, sort the functions from best to worst for the arg.
1068      For each function that's not best for this arg, set its overall
1069      harshness to EVIL so that other args won't like it.  The candidate
1070      list for the last argument is the intersection of all the best-liked
1071      functions.  */
1072
1073   qsort (candidates, n_candidates, sizeof (struct candidate),
1074          rank_for_overload);
1075   best_code = cp[-1].h.code;
1076
1077   /* If they're at least as good as each other, do an arg-by-arg check.  */
1078   if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
1079     {
1080       int better = 0;
1081       int worse = 0;
1082
1083       for (j = 0; j < n_candidates; j++)
1084         if (! strictly_better (candidates[j].h.code, best_code))
1085           break;
1086
1087       qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
1088              rank_for_ideal);
1089       for (i = 0; i < len; i++)
1090         {
1091           if (cp[-1].harshness[i].code < cp[-2].harshness[i].code)
1092             better = 1;
1093           else if (cp[-1].harshness[i].code > cp[-2].harshness[i].code)
1094             worse = 1;
1095           else if (cp[-1].harshness[i].code & STD_CODE)
1096             {
1097               /* If it involves a standard conversion, let the
1098                  inheritance lattice be the final arbiter.  */
1099               if (cp[-1].harshness[i].distance > cp[-2].harshness[i].distance)
1100                 worse = 1;
1101               else if (cp[-1].harshness[i].distance < cp[-2].harshness[i].distance)
1102                 better = 1;
1103             }
1104           else if (cp[-1].harshness[i].code & PROMO_CODE)
1105             {
1106               /* For integral promotions, take into account a finer
1107                  granularity for determining which types should be favored
1108                  over others in such promotions.  */
1109               if (cp[-1].harshness[i].int_penalty > cp[-2].harshness[i].int_penalty)
1110                 worse = 1;
1111               else if (cp[-1].harshness[i].int_penalty < cp[-2].harshness[i].int_penalty)
1112                 better = 1;
1113             }
1114         }
1115
1116       if (! better || worse)
1117         return NULL;
1118     }
1119   return cp-1;
1120 }
1121
1122 /* Assume that if the class referred to is not in the
1123    current class hierarchy, that it may be remote.
1124    PARENT is assumed to be of aggregate type here.  */
1125
1126 static int
1127 may_be_remote (parent)
1128      tree parent;
1129 {
1130   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
1131     return 0;
1132
1133   if (current_class_type == NULL_TREE)
1134     return 0;
1135
1136   if (parent == current_class_type)
1137     return 0;
1138
1139   if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
1140     return 0;
1141   return 1;
1142 }
1143
1144 tree
1145 build_vfield_ref (datum, type)
1146      tree datum, type;
1147 {
1148   tree rval;
1149   int old_assume_nonnull_objects = flag_assume_nonnull_objects;
1150
1151   if (datum == error_mark_node)
1152     return error_mark_node;
1153
1154   /* Vtable references are always made from non-null objects.  */
1155   flag_assume_nonnull_objects = 1;
1156   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
1157     datum = convert_from_reference (datum);
1158
1159   if (! TYPE_USES_COMPLEX_INHERITANCE (type))
1160     rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
1161                   datum, CLASSTYPE_VFIELD (type));
1162   else
1163     rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
1164   flag_assume_nonnull_objects = old_assume_nonnull_objects;
1165
1166   return rval;
1167 }
1168
1169 /* Build a call to a member of an object.  I.e., one that overloads
1170    operator ()(), or is a pointer-to-function or pointer-to-method.  */
1171
1172 static tree
1173 build_field_call (basetype_path, instance_ptr, name, parms)
1174      tree basetype_path, instance_ptr, name, parms;
1175 {
1176   tree field, instance;
1177
1178   if (instance_ptr == current_class_ptr)
1179     {
1180       /* Check to see if we really have a reference to an instance variable
1181          with `operator()()' overloaded.  */
1182       field = IDENTIFIER_CLASS_VALUE (name);
1183
1184       if (field == NULL_TREE)
1185         {
1186           cp_error ("`this' has no member named `%D'", name);
1187           return error_mark_node;
1188         }
1189
1190       if (TREE_CODE (field) == FIELD_DECL)
1191         {
1192           /* If it's a field, try overloading operator (),
1193              or calling if the field is a pointer-to-function.  */
1194           instance = build_component_ref_1 (current_class_ref, field, 0);
1195           if (instance == error_mark_node)
1196             return error_mark_node;
1197
1198           if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1199               && (TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance))
1200                   || flag_ansi_overloading))
1201             return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
1202
1203           if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1204             {
1205               if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
1206                 return build_function_call (instance, parms);
1207               else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
1208                 return build_function_call (instance, tree_cons (NULL_TREE, current_class_ptr, parms));
1209             }
1210         }
1211       return NULL_TREE;
1212     }
1213
1214   /* Check to see if this is not really a reference to an instance variable
1215      with `operator()()' overloaded.  */
1216   field = lookup_field (basetype_path, name, 1, 0);
1217
1218   /* This can happen if the reference was ambiguous or for access
1219      violations.  */
1220   if (field == error_mark_node)
1221     return error_mark_node;
1222
1223   if (field)
1224     {
1225       tree basetype;
1226       tree ftype = TREE_TYPE (field);
1227
1228       if (TREE_CODE (ftype) == REFERENCE_TYPE)
1229         ftype = TREE_TYPE (ftype);
1230
1231       if (TYPE_LANG_SPECIFIC (ftype)
1232           && (TYPE_OVERLOADS_CALL_EXPR (ftype) || flag_ansi_overloading))
1233         {
1234           /* Make the next search for this field very short.  */
1235           basetype = DECL_FIELD_CONTEXT (field);
1236           instance_ptr = convert_pointer_to (basetype, instance_ptr);
1237
1238           instance = build_indirect_ref (instance_ptr, NULL_PTR);
1239           return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
1240                                  build_component_ref_1 (instance, field, 0),
1241                                  parms, NULL_TREE);
1242         }
1243       if (TREE_CODE (ftype) == POINTER_TYPE)
1244         {
1245           if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
1246               || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
1247             {
1248               /* This is a member which is a pointer to function.  */
1249               tree ref
1250                 = build_component_ref_1 (build_indirect_ref (instance_ptr,
1251                                                              NULL_PTR),
1252                                          field, LOOKUP_COMPLAIN);
1253               if (ref == error_mark_node)
1254                 return error_mark_node;
1255               return build_function_call (ref, parms);
1256             }
1257         }
1258       else if (TREE_CODE (ftype) == METHOD_TYPE)
1259         {
1260           error ("invalid call via pointer-to-member function");
1261           return error_mark_node;
1262         }
1263       else
1264         return NULL_TREE;
1265     }
1266   return NULL_TREE;
1267 }
1268
1269 tree
1270 find_scoped_type (type, inner_name, inner_types)
1271      tree type, inner_name, inner_types;
1272 {
1273   tree tags = CLASSTYPE_TAGS (type);
1274
1275   while (tags)
1276     {
1277       /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1278          enclosing class) is set to the name for the enum type.  So, if
1279          inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1280          then this test will be true.  */
1281       if (TREE_PURPOSE (tags) == inner_name)
1282         {
1283           if (inner_types == NULL_TREE)
1284             return TYPE_MAIN_DECL (TREE_VALUE (tags));
1285           return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
1286         }
1287       tags = TREE_CHAIN (tags);
1288     }
1289
1290   /* Look for a TYPE_DECL.  */
1291   for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
1292     if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
1293       {
1294         /* Code by raeburn.  */
1295         if (inner_types == NULL_TREE)
1296           return tags;
1297         return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
1298       }
1299
1300   return NULL_TREE;
1301 }
1302
1303 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1304    the name that names the above nested type.  INNER_TYPES
1305    is a chain of nested type names (held together by SCOPE_REFs);
1306    OUTER_TYPE is the type we know to enclose INNER_TYPES.
1307    Returns NULL_TREE if there is an error.  */
1308
1309 tree
1310 resolve_scope_to_name (outer_type, inner_stuff)
1311      tree outer_type, inner_stuff;
1312 {
1313   register tree tmp;
1314   tree inner_name, inner_type;
1315
1316   if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
1317     {
1318       /* We first try to look for a nesting in our current class context,
1319          then try any enclosing classes.  */
1320       tree type = current_class_type;
1321       
1322       while (type && (TREE_CODE (type) == RECORD_TYPE
1323                       || TREE_CODE (type) == UNION_TYPE))
1324         {
1325           tree rval = resolve_scope_to_name (type, inner_stuff);
1326
1327           if (rval != NULL_TREE)
1328             return rval;
1329           type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
1330         }
1331     }
1332
1333   if (TREE_CODE (inner_stuff) == SCOPE_REF)
1334     {
1335       inner_name = TREE_OPERAND (inner_stuff, 0);
1336       inner_type = TREE_OPERAND (inner_stuff, 1);
1337     }
1338   else
1339     {
1340       inner_name = inner_stuff;
1341       inner_type = NULL_TREE;
1342     }
1343
1344   if (outer_type == NULL_TREE)
1345     {
1346       tree x;
1347       /* If we have something that's already a type by itself,
1348          use that.  */
1349       if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
1350         {
1351           if (inner_type)
1352             return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
1353                                           inner_type);
1354           return inner_name;
1355         }
1356       
1357       x = lookup_name (inner_name, 0);
1358
1359       if (x && TREE_CODE (x) == NAMESPACE_DECL)
1360         {
1361           x = lookup_namespace_name (x, inner_type);
1362           return x;
1363         }
1364       return NULL_TREE;
1365     }
1366
1367   if (! IS_AGGR_TYPE (outer_type))
1368     return NULL_TREE;
1369
1370   /* Look for member classes or enums.  */
1371   tmp = find_scoped_type (outer_type, inner_name, inner_type);
1372
1373   /* If it's not a type in this class, then go down into the
1374      base classes and search there.  */
1375   if (! tmp && TYPE_BINFO (outer_type))
1376     {
1377       tree binfos = TYPE_BINFO_BASETYPES (outer_type);
1378       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1379
1380       for (i = 0; i < n_baselinks; i++)
1381         {
1382           tree base_binfo = TREE_VEC_ELT (binfos, i);
1383           tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
1384           if (tmp)
1385             return tmp;
1386         }
1387       tmp = NULL_TREE;
1388     }
1389
1390   return tmp;
1391 }
1392
1393 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1394    This is how virtual function calls are avoided.  */
1395
1396 tree
1397 build_scoped_method_call (exp, basetype, name, parms)
1398      tree exp, basetype, name, parms;
1399 {
1400   /* Because this syntactic form does not allow
1401      a pointer to a base class to be `stolen',
1402      we need not protect the derived->base conversion
1403      that happens here.
1404      
1405      @@ But we do have to check access privileges later.  */
1406   tree binfo, decl;
1407   tree type = TREE_TYPE (exp);
1408
1409   if (type == error_mark_node
1410       || basetype == error_mark_node)
1411     return error_mark_node;
1412
1413   if (processing_template_decl)
1414     {
1415       if (TREE_CODE (name) == BIT_NOT_EXPR)
1416         {
1417           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1418           name = build_min_nt (BIT_NOT_EXPR, type);
1419         }
1420       name = build_min_nt (SCOPE_REF, basetype, name);
1421       return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
1422     }
1423
1424   if (TREE_CODE (type) == REFERENCE_TYPE)
1425     type = TREE_TYPE (type);
1426
1427   /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1428      that explicit ~int is caught in the parser; this deals with typedefs
1429      and template parms.  */
1430   if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
1431     {
1432       if (type != basetype)
1433         cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1434                   exp, basetype, type);
1435       name = TREE_OPERAND (name, 0);
1436       if (basetype != name && basetype != get_type_value (name))
1437         cp_error ("qualified type `%T' does not match destructor name `~%T'",
1438                   basetype, name);
1439       return convert (void_type_node, exp);
1440     }
1441
1442   if (! is_aggr_type (basetype, 1))
1443     return error_mark_node;
1444
1445   if (! IS_AGGR_TYPE (type))
1446     {
1447       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1448                 exp, type);
1449       return error_mark_node;
1450     }
1451
1452   if ((binfo = binfo_or_else (basetype, type)))
1453     {
1454       if (binfo == error_mark_node)
1455         return error_mark_node;
1456       if (TREE_CODE (exp) == INDIRECT_REF)
1457         decl = build_indirect_ref (convert_pointer_to (binfo,
1458                                                        build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
1459       else
1460         decl = build_scoped_ref (exp, basetype);
1461
1462       /* Call to a destructor.  */
1463       if (TREE_CODE (name) == BIT_NOT_EXPR)
1464         {
1465           /* Explicit call to destructor.  */
1466           name = TREE_OPERAND (name, 0);
1467           if (! (name == TYPE_MAIN_VARIANT (TREE_TYPE (decl))
1468                  || name == constructor_name (TREE_TYPE (decl))
1469                  || TREE_TYPE (decl) == get_type_value (name)))
1470             {
1471               cp_error
1472                 ("qualified type `%T' does not match destructor name `~%T'",
1473                  TREE_TYPE (decl), name);
1474               return error_mark_node;
1475             }
1476           if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
1477             return convert (void_type_node, exp);
1478           
1479           return build_delete (TREE_TYPE (decl), decl, integer_two_node,
1480                                LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
1481                                0);
1482         }
1483
1484       /* Call to a method.  */
1485       return build_method_call (decl, name, parms, binfo,
1486                                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1487     }
1488   return error_mark_node;
1489 }
1490
1491 static void
1492 print_candidates (candidates)
1493      tree candidates;
1494 {
1495   cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
1496   candidates = TREE_CHAIN (candidates);
1497
1498   while (candidates)
1499     {
1500       cp_error_at ("                %D", TREE_VALUE (candidates));
1501       candidates = TREE_CHAIN (candidates);
1502     }
1503 }
1504
1505 static void
1506 print_n_candidates (candidates, n)
1507      struct candidate *candidates;
1508      int n;
1509 {
1510   int i;
1511
1512   cp_error_at ("candidates are: %D", candidates[0].function);
1513   for (i = 1; i < n; i++)
1514     cp_error_at ("                %D", candidates[i].function);
1515 }
1516
1517 /* We want the address of a function or method.  We avoid creating a
1518    pointer-to-member function.  */
1519
1520 tree
1521 build_addr_func (function)
1522      tree function;
1523 {
1524   tree type = TREE_TYPE (function);
1525
1526   /* We have to do these by hand to avoid real pointer to member
1527      functions.  */
1528   if (TREE_CODE (type) == METHOD_TYPE)
1529     {
1530       tree addr;
1531
1532       type = build_pointer_type (type);
1533
1534       if (mark_addressable (function) == 0)
1535         return error_mark_node;
1536
1537       addr = build1 (ADDR_EXPR, type, function);
1538
1539       /* Address of a static or external variable or function counts
1540          as a constant */
1541       if (staticp (function))
1542         TREE_CONSTANT (addr) = 1;
1543
1544       function = addr;
1545     }
1546   else
1547     function = default_conversion (function);
1548
1549   return function;
1550 }
1551
1552 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1553    POINTER_TYPE to those.  Note, pointer to member function types
1554    (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
1555
1556 tree
1557 build_call (function, result_type, parms)
1558      tree function, result_type, parms;
1559 {
1560   int is_constructor = 0;
1561
1562   function = build_addr_func (function);
1563
1564   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
1565     {
1566       sorry ("unable to call pointer to member function here");
1567       return error_mark_node;
1568     }
1569
1570   if (TREE_CODE (function) == ADDR_EXPR
1571       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1572       && DECL_CONSTRUCTOR_P (TREE_OPERAND (function, 0)))
1573     is_constructor = 1;
1574
1575   function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
1576   TREE_HAS_CONSTRUCTOR (function) = is_constructor;
1577   TREE_TYPE (function) = result_type;
1578   TREE_SIDE_EFFECTS (function) = 1;
1579   
1580   return function;
1581 }
1582
1583 static tree
1584 default_parm_conversions (parms, last)
1585      tree parms, *last;
1586 {
1587   tree parm, parmtypes = NULL_TREE;
1588
1589   *last = NULL_TREE;
1590
1591   for (parm = parms; parm; parm = TREE_CHAIN (parm))
1592     {
1593       tree t = TREE_TYPE (TREE_VALUE (parm));
1594
1595       if (TREE_CODE (t) == OFFSET_TYPE
1596           || TREE_CODE (t) == METHOD_TYPE
1597           || TREE_CODE (t) == FUNCTION_TYPE)
1598         {
1599           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
1600           t = TREE_TYPE (TREE_VALUE (parm));
1601         }
1602
1603       if (t == error_mark_node)
1604           return error_mark_node;
1605
1606       *last = build_tree_list (NULL_TREE, t);
1607       parmtypes = chainon (parmtypes, *last);
1608     }
1609
1610   return parmtypes;
1611 }
1612
1613
1614 /* Build something of the form ptr->method (args)
1615    or object.method (args).  This can also build
1616    calls to constructors, and find friends.
1617
1618    Member functions always take their class variable
1619    as a pointer.
1620
1621    INSTANCE is a class instance.
1622
1623    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1624
1625    PARMS help to figure out what that NAME really refers to.
1626
1627    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1628    down to the real instance type to use for access checking.  We need this
1629    information to get protected accesses correct.  This parameter is used
1630    by build_member_call.
1631
1632    FLAGS is the logical disjunction of zero or more LOOKUP_
1633    flags.  See cp-tree.h for more info.
1634
1635    If this is all OK, calls build_function_call with the resolved
1636    member function.
1637
1638    This function must also handle being called to perform
1639    initialization, promotion/coercion of arguments, and
1640    instantiation of default parameters.
1641
1642    Note that NAME may refer to an instance variable name.  If
1643    `operator()()' is defined for the type of that field, then we return
1644    that result.  */
1645
1646 tree
1647 build_method_call (instance, name, parms, basetype_path, flags)
1648      tree instance, name, parms, basetype_path;
1649      int flags;
1650 {
1651   register tree function, fntype, value_type;
1652   register tree basetype, save_basetype;
1653   register tree baselink, result, parmtypes;
1654   tree last;
1655   int pass;
1656   tree access = access_public_node;
1657   tree orig_basetype = basetype_path ? BINFO_TYPE (basetype_path) : NULL_TREE;
1658
1659   /* Range of cases for vtable optimization.  */
1660   enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
1661   enum vtable_needs need_vtbl = not_needed;
1662
1663   char *name_kind;
1664   tree save_name = name;
1665   int ever_seen = 0;
1666   tree instance_ptr = NULL_TREE;
1667   int all_virtual = flag_all_virtual;
1668   int static_call_context = 0;
1669   tree found_fns = NULL_TREE;
1670
1671   /* Keep track of `const' and `volatile' objects.  */
1672   int constp, volatilep;
1673
1674 #ifdef GATHER_STATISTICS
1675   n_build_method_call++;
1676 #endif
1677
1678   if (instance == error_mark_node
1679       || name == error_mark_node
1680       || parms == error_mark_node
1681       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
1682     return error_mark_node;
1683
1684   if (processing_template_decl)
1685     {
1686       if (TREE_CODE (name) == BIT_NOT_EXPR)
1687         {
1688           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1689           name = build_min_nt (BIT_NOT_EXPR, type);
1690         }
1691
1692       return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
1693     }
1694
1695   /* This is the logic that magically deletes the second argument to
1696      operator delete, if it is not needed.  */
1697   if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
1698     {
1699       tree save_last = TREE_CHAIN (parms);
1700       tree result;
1701       /* get rid of unneeded argument */
1702       TREE_CHAIN (parms) = NULL_TREE;
1703       result = build_method_call (instance, name, parms, basetype_path,
1704                                   (LOOKUP_SPECULATIVELY|flags)
1705                                   &~LOOKUP_COMPLAIN);
1706       /* If it finds a match, return it.  */
1707       if (result)
1708         return build_method_call (instance, name, parms, basetype_path, flags);
1709       /* If it doesn't work, two argument delete must work */
1710       TREE_CHAIN (parms) = save_last;
1711     }
1712   /* We already know whether it's needed or not for vec delete.  */
1713   else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
1714            && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1715            && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
1716     TREE_CHAIN (parms) = NULL_TREE;
1717
1718   if (TREE_CODE (name) == BIT_NOT_EXPR)
1719     {
1720       flags |= LOOKUP_DESTRUCTOR;
1721       name = TREE_OPERAND (name, 0);
1722       if (parms)
1723         error ("destructors take no parameters");
1724       basetype = TREE_TYPE (instance);
1725       if (TREE_CODE (basetype) == REFERENCE_TYPE)
1726         basetype = TREE_TYPE (basetype);
1727       if (! (name == basetype
1728              || (IS_AGGR_TYPE (basetype)
1729                  && name == constructor_name (basetype))
1730              || basetype == get_type_value (name)))
1731         {
1732           cp_error ("destructor name `~%D' does not match type `%T' of expression",
1733                     name, basetype);
1734           return convert (void_type_node, instance);
1735         }
1736
1737       if (! TYPE_HAS_DESTRUCTOR (basetype))
1738         return convert (void_type_node, instance);
1739       instance = default_conversion (instance);
1740       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1741       return build_delete (build_pointer_type (basetype),
1742                            instance_ptr, integer_two_node,
1743                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
1744     }
1745
1746   if (flag_ansi_overloading)
1747     return build_new_method_call (instance, name, parms, basetype_path, flags);
1748
1749   {
1750     char *xref_name;
1751     
1752     /* Initialize name for error reporting.  */
1753     if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
1754       {
1755         char *p = operator_name_string (name);
1756         xref_name = (char *)alloca (strlen (p) + 10);
1757         sprintf (xref_name, "operator %s", p);
1758       }
1759     else if (TREE_CODE (name) == SCOPE_REF)
1760       xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
1761     else
1762       xref_name = IDENTIFIER_POINTER (name);
1763
1764     GNU_xref_call (current_function_decl, xref_name);
1765   }
1766
1767   if (instance == NULL_TREE)
1768     {
1769       basetype = NULL_TREE;
1770       /* Check cases where this is really a call to raise
1771          an exception.  */
1772       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
1773         {
1774           basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
1775           if (basetype)
1776             basetype = TREE_VALUE (basetype);
1777         }
1778       else if (TREE_CODE (name) == SCOPE_REF
1779                && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
1780         {
1781           if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
1782             return error_mark_node;
1783           basetype = purpose_member (TREE_OPERAND (name, 1),
1784                                      CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
1785           if (basetype)
1786             basetype = TREE_VALUE (basetype);
1787         }
1788
1789       if (basetype != NULL_TREE)
1790         ;
1791       /* call to a constructor...  */
1792       else if (basetype_path)
1793         {
1794           basetype = BINFO_TYPE (basetype_path);
1795           if (name == TYPE_IDENTIFIER (basetype))
1796             name = ctor_identifier;
1797         }
1798       else if (IDENTIFIER_HAS_TYPE_VALUE (name))
1799         {
1800           basetype = IDENTIFIER_TYPE_VALUE (name);
1801           name = ctor_identifier;
1802         }
1803       else
1804         {
1805           tree typedef_name = lookup_name (name, 1);
1806           if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
1807             {
1808               /* Canonicalize the typedef name.  */
1809               basetype = TREE_TYPE (typedef_name);
1810               name = ctor_identifier;
1811             }
1812           else
1813             {
1814               cp_error ("no constructor named `%T' in scope",
1815                         name);
1816               return error_mark_node;
1817             }
1818         }
1819
1820       if (! IS_AGGR_TYPE (basetype))
1821         {
1822         non_aggr_error:
1823           if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
1824             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1825                       name, instance, basetype);
1826
1827           return error_mark_node;
1828         }
1829     }
1830   else if (instance == current_class_ref || instance == current_class_ptr)
1831     {
1832       /* When doing initialization, we side-effect the TREE_TYPE of
1833          current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
1834       basetype = TREE_TYPE (current_class_ref);
1835
1836       /* Anything manifestly `this' in constructors and destructors
1837          has a known type, so virtual function tables are not needed.  */
1838       if (TYPE_VIRTUAL_P (basetype)
1839           && !(flags & LOOKUP_NONVIRTUAL))
1840         need_vtbl = (dtor_label || ctor_label)
1841           ? unneeded : maybe_needed;
1842
1843       /* If `this' is a signature pointer and `name' is not a constructor,
1844          we are calling a signature member function.  In that case, set the
1845          `basetype' to the signature type and dereference the `optr' field.  */
1846       if (IS_SIGNATURE_POINTER (basetype)
1847           && TYPE_IDENTIFIER (basetype) != name)
1848         {
1849           basetype = SIGNATURE_TYPE (basetype);
1850           instance_ptr = instance;
1851           basetype_path = TYPE_BINFO (basetype);
1852         }
1853       else
1854         {
1855           instance = current_class_ref;
1856           instance_ptr = current_class_ptr;
1857           basetype_path = TYPE_BINFO (current_class_type);
1858         }
1859       result = build_field_call (basetype_path, instance_ptr, name, parms);
1860
1861       if (result)
1862         return result;
1863     }
1864   else if (TREE_CODE (instance) == RESULT_DECL)
1865     {
1866       basetype = TREE_TYPE (instance);
1867       /* Should we ever have to make a virtual function reference
1868          from a RESULT_DECL, know that it must be of fixed type
1869          within the scope of this function.  */
1870       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1871         need_vtbl = maybe_needed;
1872       instance_ptr = build1 (ADDR_EXPR, build_pointer_type (basetype), instance);
1873     }
1874   else
1875     {
1876       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
1877       tree inst_ptr_basetype;
1878
1879       static_call_context =
1880         (TREE_CODE (instance) == INDIRECT_REF
1881          && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
1882          && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
1883
1884       if (TREE_CODE (instance) == OFFSET_REF)
1885         instance = resolve_offset_ref (instance);
1886
1887       /* the base type of an instance variable is pointer to class */
1888       basetype = TREE_TYPE (instance);
1889
1890       if (TREE_CODE (basetype) == REFERENCE_TYPE)
1891         {
1892           basetype = TREE_TYPE (basetype);
1893           if (! IS_AGGR_TYPE (basetype))
1894             goto non_aggr_error;
1895           /* Call to convert not needed because we are remaining
1896              within the same type.  */
1897           instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
1898                                  instance);
1899           inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
1900         }
1901       else
1902         {
1903           if (! IS_AGGR_TYPE (basetype)
1904               && ! (TYPE_LANG_SPECIFIC (basetype)
1905                     && (IS_SIGNATURE_POINTER (basetype)
1906                         || IS_SIGNATURE_REFERENCE (basetype))))
1907             goto non_aggr_error;
1908
1909           /* If `instance' is a signature pointer/reference and `name' is
1910              not a constructor, we are calling a signature member function.
1911              In that case set the `basetype' to the signature type.  */
1912           if ((IS_SIGNATURE_POINTER (basetype)
1913                || IS_SIGNATURE_REFERENCE (basetype))
1914               && TYPE_IDENTIFIER (basetype) != name)
1915             basetype = SIGNATURE_TYPE (basetype);
1916
1917           basetype = complete_type (basetype);
1918
1919           if ((IS_SIGNATURE (basetype)
1920                && (instance_ptr = instance))
1921               || (lvalue_p (instance)
1922                   && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
1923               || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
1924             {
1925               if (instance_ptr == error_mark_node)
1926                 return error_mark_node;
1927             }
1928           else if (TREE_CODE (instance) == NOP_EXPR
1929                    || TREE_CODE (instance) == CONSTRUCTOR)
1930             {
1931               /* A cast is not an lvalue.  Initialize a fresh temp
1932                  with the value we are casting from, and proceed with
1933                  that temporary.  We can't cast to a reference type,
1934                  so that simplifies the initialization to something
1935                  we can manage.  */
1936               tree temp = get_temp_name (TREE_TYPE (instance), 0);
1937               if (IS_AGGR_TYPE (TREE_TYPE (instance)))
1938                 expand_aggr_init (temp, instance, 0, flags);
1939               else
1940                 {
1941                   store_init_value (temp, instance);
1942                   expand_decl_init (temp);
1943                 }
1944               instance = temp;
1945               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1946             }
1947           else
1948             {
1949               if (TREE_CODE (instance) != CALL_EXPR)
1950                 my_friendly_abort (125);
1951               if (TYPE_NEEDS_CONSTRUCTING (basetype))
1952                 instance = build_cplus_new (basetype, instance);
1953               else
1954                 {
1955                   instance = get_temp_name (basetype, 0);
1956                   TREE_ADDRESSABLE (instance) = 1;
1957                 }
1958               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1959             }
1960           /* @@ Should we call comp_target_types here?  */
1961           if (IS_SIGNATURE (basetype))
1962             inst_ptr_basetype = basetype;
1963           else
1964             inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
1965           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
1966             basetype = inst_ptr_basetype;
1967           else
1968             {
1969               instance_ptr = convert (build_pointer_type (basetype), instance_ptr);
1970               if (instance_ptr == error_mark_node)
1971                 return error_mark_node;
1972             }
1973         }
1974
1975       /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1976          not updated, so we use `basetype' instead.  */
1977       if (basetype_path == NULL_TREE
1978           && IS_SIGNATURE (basetype))
1979         basetype_path = TYPE_BINFO (basetype);
1980       else if (basetype_path == NULL_TREE ||
1981         BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
1982         basetype_path = TYPE_BINFO (inst_ptr_basetype);
1983
1984       result = build_field_call (basetype_path, instance_ptr, name, parms);
1985       if (result)
1986         return result;
1987
1988       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1989         {
1990           if (TREE_SIDE_EFFECTS (instance_ptr))
1991             {
1992               /* This action is needed because the instance is needed
1993                  for providing the base of the virtual function table.
1994                  Without using a SAVE_EXPR, the function we are building
1995                  may be called twice, or side effects on the instance
1996                  variable (such as a post-increment), may happen twice.  */
1997               instance_ptr = save_expr (instance_ptr);
1998               instance = build_indirect_ref (instance_ptr, NULL_PTR);
1999             }
2000           else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
2001             {
2002               /* This happens when called for operator new ().  */
2003               instance = build_indirect_ref (instance, NULL_PTR);
2004             }
2005
2006           need_vtbl = maybe_needed;
2007         }
2008     }
2009
2010   if (save_name == ctor_identifier)
2011     save_name = TYPE_IDENTIFIER (basetype);
2012
2013   if (TYPE_SIZE (complete_type (basetype)) == 0)
2014     {
2015       /* This is worth complaining about, I think.  */
2016       cp_error ("cannot lookup method in incomplete type `%T'", basetype);
2017       return error_mark_node;
2018     }
2019
2020   save_basetype = TYPE_MAIN_VARIANT (basetype);
2021
2022   parmtypes = default_parm_conversions (parms, &last);
2023   if (parmtypes == error_mark_node)
2024     {
2025       return error_mark_node;
2026     }
2027
2028   if (instance && IS_SIGNATURE (basetype))
2029     {
2030       /* @@ Should this be the constp/volatilep flags for the optr field
2031          of the signature pointer?  */
2032       constp = TYPE_READONLY (basetype);
2033       volatilep = TYPE_VOLATILE (basetype);
2034       parms = tree_cons (NULL_TREE, instance_ptr, parms);
2035     }
2036   else if (instance)
2037     {
2038       /* TREE_READONLY (instance) fails for references.  */
2039       constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
2040       volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
2041       parms = tree_cons (NULL_TREE, instance_ptr, parms);
2042     }
2043   else
2044     {
2045       /* Raw constructors are always in charge.  */
2046       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2047           && ! (flags & LOOKUP_HAS_IN_CHARGE))
2048         {
2049           flags |= LOOKUP_HAS_IN_CHARGE;
2050           parms = tree_cons (NULL_TREE, integer_one_node, parms);
2051           parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
2052         }
2053
2054       constp = 0;
2055       volatilep = 0;
2056       instance_ptr = build_int_2 (0, 0);
2057       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
2058       parms = tree_cons (NULL_TREE, instance_ptr, parms);
2059     }
2060
2061   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
2062
2063   if (last == NULL_TREE)
2064     last = parmtypes;
2065
2066   /* Look up function name in the structure type definition.  */
2067
2068   /* FIXME Axe most of this now?  */
2069   if ((IDENTIFIER_HAS_TYPE_VALUE (name)
2070        && ! IDENTIFIER_OPNAME_P (name)
2071        && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name)))
2072       || name == constructor_name (basetype)
2073       || name == ctor_identifier)
2074     {
2075       tree tmp = NULL_TREE;
2076       if (IDENTIFIER_TYPE_VALUE (name) == basetype
2077           || name == constructor_name (basetype)
2078           || name == ctor_identifier)
2079         tmp = TYPE_BINFO (basetype);
2080       else
2081         tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
2082       
2083       if (tmp != NULL_TREE)
2084         {
2085           name_kind = "constructor";
2086           
2087           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2088               && ! (flags & LOOKUP_HAS_IN_CHARGE))
2089             {
2090               /* Constructors called for initialization
2091                  only are never in charge.  */
2092               tree tmplist;
2093               
2094               flags |= LOOKUP_HAS_IN_CHARGE;
2095               tmplist = tree_cons (NULL_TREE, integer_zero_node,
2096                                    TREE_CHAIN (parms));
2097               TREE_CHAIN (parms) = tmplist;
2098               tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
2099               TREE_CHAIN (parmtypes) = tmplist;
2100             }
2101           basetype = BINFO_TYPE (tmp);
2102         }
2103       else
2104         name_kind = "method";
2105     }
2106   else
2107     name_kind = "method";
2108   
2109   if (basetype_path == NULL_TREE
2110       || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
2111     basetype_path = TYPE_BINFO (basetype);
2112   result = lookup_fnfields (basetype_path, name,
2113                             (flags & LOOKUP_COMPLAIN));
2114   if (result == error_mark_node)
2115     return error_mark_node;
2116
2117   for (pass = 0; pass < 2; pass++)
2118     {
2119       struct candidate *candidates;
2120       struct candidate *cp;
2121       int len;
2122       unsigned best = 1;
2123
2124       baselink = result;
2125
2126       if (pass > 0)
2127         {
2128           candidates
2129             = (struct candidate *) alloca ((ever_seen+1)
2130                                            * sizeof (struct candidate));
2131           bzero ((char *) candidates, (ever_seen + 1) * sizeof (struct candidate));
2132           cp = candidates;
2133           len = list_length (parms);
2134           ever_seen = 0;
2135
2136           /* First see if a global function has a shot at it.  */
2137           if (flags & LOOKUP_GLOBAL)
2138             {
2139               tree friend_parms;
2140               tree parm = instance_ptr;
2141
2142               if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
2143                 parm = convert_from_reference (parm);
2144               else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
2145                 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
2146               else
2147                 my_friendly_abort (167);
2148
2149               friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
2150
2151               cp->h_len = len;
2152               cp->harshness = (struct harshness_code *)
2153                 alloca ((len + 1) * sizeof (struct harshness_code));
2154
2155               result = build_overload_call_real (name, friend_parms, 0, cp, 1);
2156
2157               /* If it turns out to be the one we were actually looking for
2158                  (it was probably a friend function), the return the
2159                  good result.  */
2160               if (TREE_CODE (result) == CALL_EXPR)
2161                 return result;
2162
2163               while ((cp->h.code & EVIL_CODE) == 0)
2164                 {
2165                   /* non-standard uses: set the field to 0 to indicate
2166                      we are using a non-member function.  */
2167                   cp->u.field = 0;
2168                   if (cp->harshness[len].distance == 0
2169                       && cp->h.code < best)
2170                     best = cp->h.code;
2171                   cp += 1;
2172                 }
2173             }
2174         }
2175
2176       if (baselink)
2177         {
2178           /* We have a hit (of sorts). If the parameter list is
2179              "error_mark_node", or some variant thereof, it won't
2180              match any methods.  Since we have verified that the is
2181              some method vaguely matching this one (in name at least),
2182              silently return.
2183              
2184              Don't stop for friends, however.  */
2185           basetype_path = TREE_PURPOSE (baselink);
2186
2187           function = TREE_VALUE (baselink);
2188           if (TREE_CODE (basetype_path) == TREE_LIST)
2189             basetype_path = TREE_VALUE (basetype_path);
2190           basetype = BINFO_TYPE (basetype_path);
2191
2192           for (; function; function = DECL_CHAIN (function))
2193             {
2194 #ifdef GATHER_STATISTICS
2195               n_inner_fields_searched++;
2196 #endif
2197               ever_seen++;
2198               if (pass > 0)
2199                 found_fns = tree_cons (NULL_TREE, function, found_fns);
2200
2201               /* Not looking for friends here.  */
2202               if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
2203                   && ! DECL_STATIC_FUNCTION_P (function))
2204                 continue;
2205
2206               if (pass > 0)
2207                 {
2208                   tree these_parms = parms;
2209
2210 #ifdef GATHER_STATISTICS
2211                   n_inner_fields_searched++;
2212 #endif
2213                   cp->h_len = len;
2214                   cp->harshness = (struct harshness_code *)
2215                     alloca ((len + 1) * sizeof (struct harshness_code));
2216
2217                   if (DECL_STATIC_FUNCTION_P (function))
2218                     these_parms = TREE_CHAIN (these_parms);
2219                   compute_conversion_costs (function, these_parms, cp, len);
2220
2221                   if ((cp->h.code & EVIL_CODE) == 0)
2222                     {
2223                       cp->u.field = function;
2224                       cp->function = function;
2225                       cp->basetypes = basetype_path;
2226
2227                       /* Don't allow non-converting constructors to convert.  */
2228                       if (flags & LOOKUP_ONLYCONVERTING
2229                           && DECL_LANG_SPECIFIC (function)
2230                           && DECL_NONCONVERTING_P (function))
2231                         continue;
2232
2233                       /* No "two-level" conversions.  */
2234                       if (flags & LOOKUP_NO_CONVERSION
2235                           && (cp->h.code & USER_CODE))
2236                         continue;
2237
2238                       cp++;
2239                     }
2240                 }
2241             }
2242         }
2243
2244       if (pass == 0)
2245         {
2246           tree igv = lookup_name_nonclass (name);
2247
2248           /* No exact match could be found.  Now try to find match
2249              using default conversions.  */
2250           if ((flags & LOOKUP_GLOBAL) && igv)
2251             {
2252               if (TREE_CODE (igv) == FUNCTION_DECL)
2253                 ever_seen += 1;
2254               else if (TREE_CODE (igv) == TREE_LIST)
2255                 ever_seen += count_functions (igv);
2256             }
2257
2258           if (ever_seen == 0)
2259             {
2260               if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2261                   == LOOKUP_SPECULATIVELY)
2262                 return NULL_TREE;
2263               
2264               TREE_CHAIN (last) = void_list_node;
2265               if (flags & LOOKUP_GLOBAL)
2266                 cp_error ("no global or member function `%D(%A)' defined",
2267                           save_name, parmtypes);
2268               else
2269                 cp_error ("no member function `%T::%D(%A)' defined",
2270                           save_basetype, save_name, TREE_CHAIN (parmtypes));
2271               return error_mark_node;
2272             }
2273           continue;
2274         }
2275
2276       if (cp - candidates != 0)
2277         {
2278           /* Rank from worst to best.  Then cp will point to best one.
2279              Private fields have their bits flipped.  For unsigned
2280              numbers, this should make them look very large.
2281              If the best alternate has a (signed) negative value,
2282              then all we ever saw were private members.  */
2283           if (cp - candidates > 1)
2284             {
2285               int n_candidates = cp - candidates;
2286               extern int warn_synth;
2287               TREE_VALUE (parms) = instance_ptr;
2288               cp = ideal_candidate (candidates, n_candidates, len);
2289               if (cp == (struct candidate *)0)
2290                 {
2291                   if (flags & LOOKUP_COMPLAIN)
2292                     {
2293                       TREE_CHAIN (last) = void_list_node;
2294                       cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2295                                 name_kind, save_name, TREE_CHAIN (parmtypes));
2296                       print_n_candidates (candidates, n_candidates);
2297                     }
2298                   return error_mark_node;
2299                 }
2300               if (cp->h.code & EVIL_CODE)
2301                 return error_mark_node;
2302               if (warn_synth
2303                   && DECL_NAME (cp->function) == ansi_opname[MODIFY_EXPR]
2304                   && DECL_ARTIFICIAL (cp->function)
2305                   && n_candidates == 2)
2306                 {
2307                   cp_warning ("using synthesized `%#D' for copy assignment",
2308                               cp->function);
2309                   cp_warning_at ("  where cfront would use `%#D'",
2310                                  candidates->function);
2311                 }
2312             }
2313           else if (cp[-1].h.code & EVIL_CODE)
2314             {
2315               if (flags & LOOKUP_COMPLAIN)
2316                 cp_error ("ambiguous type conversion requested for %s `%D'",
2317                           name_kind, save_name);
2318               return error_mark_node;
2319             }
2320           else
2321             cp--;
2322
2323           /* The global function was the best, so use it.  */
2324           if (cp->u.field == 0)
2325             {
2326               /* We must convert the instance pointer into a reference type.
2327                  Global overloaded functions can only either take
2328                  aggregate objects (which come for free from references)
2329                  or reference data types anyway.  */
2330               TREE_VALUE (parms) = copy_node (instance_ptr);
2331               TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
2332               return build_function_call (cp->function, parms);
2333             }
2334
2335           function = cp->function;
2336           basetype_path = cp->basetypes;
2337           if (! DECL_STATIC_FUNCTION_P (function))
2338             TREE_VALUE (parms) = cp->arg;
2339           goto found_and_maybe_warn;
2340         }
2341
2342       if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
2343         {
2344           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2345               == LOOKUP_SPECULATIVELY)
2346             return NULL_TREE;
2347
2348           if (DECL_STATIC_FUNCTION_P (cp->function))
2349             parms = TREE_CHAIN (parms);
2350           if (ever_seen)
2351             {
2352               if (flags & LOOKUP_SPECULATIVELY)
2353                 return NULL_TREE;
2354               if (static_call_context
2355                   && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
2356                 cp_error ("object missing in call to `%D'", cp->function);
2357               else if (ever_seen > 1)
2358                 {
2359                   TREE_CHAIN (last) = void_list_node;
2360                   cp_error ("no matching function for call to `%T::%D (%A)%V'",
2361                             TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr))),
2362                             save_name, TREE_CHAIN (parmtypes),
2363                             TREE_TYPE (TREE_TYPE (instance_ptr)));
2364                   TREE_CHAIN (last) = NULL_TREE;
2365                   print_candidates (found_fns);
2366                 }
2367               else
2368                 report_type_mismatch (cp, parms, name_kind);
2369               return error_mark_node;
2370             }
2371
2372           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2373               == LOOKUP_COMPLAIN)
2374             {
2375               cp_error ("%T has no method named %D", save_basetype, save_name);
2376               return error_mark_node;
2377             }
2378           return NULL_TREE;
2379         }
2380       continue;
2381
2382     found_and_maybe_warn:
2383       if ((cp->harshness[0].code & CONST_CODE)
2384           /* 12.1p2: Constructors can be called for const objects.  */
2385           && ! DECL_CONSTRUCTOR_P (cp->function))
2386         {
2387           if (flags & LOOKUP_COMPLAIN)
2388             {
2389               cp_error_at ("non-const member function `%D'", cp->function);
2390               error ("called for const object at this point in file");
2391             }
2392           /* Not good enough for a match.  */
2393           else
2394             return error_mark_node;
2395         }
2396       goto found;
2397     }
2398   /* Silently return error_mark_node.  */
2399   return error_mark_node;
2400
2401  found:
2402   if (flags & LOOKUP_PROTECT)
2403     access = compute_access (basetype_path, function);
2404
2405   if (access == access_private_node)
2406     {
2407       if (flags & LOOKUP_COMPLAIN)
2408         {
2409           cp_error_at ("%s `%+#D' is %s", name_kind, function, 
2410                        TREE_PRIVATE (function) ? "private"
2411                        : "from private base class");
2412           error ("within this context");
2413         }
2414       return error_mark_node;
2415     }
2416   else if (access == access_protected_node)
2417     {
2418       if (flags & LOOKUP_COMPLAIN)
2419         {
2420           cp_error_at ("%s `%+#D' %s", name_kind, function,
2421                        TREE_PROTECTED (function) ? "is protected"
2422                        : "has protected accessibility");
2423           error ("within this context");
2424         }
2425       return error_mark_node;
2426     }
2427
2428   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2429      type (if it exists) is a pointer to.  */
2430
2431   if (DECL_ABSTRACT_VIRTUAL_P (function)
2432       && instance == current_class_ref
2433       && DECL_CONSTRUCTOR_P (current_function_decl)
2434       && ! (flags & LOOKUP_NONVIRTUAL)
2435       && value_member (function, get_abstract_virtuals (basetype)))
2436     cp_error ("abstract virtual `%#D' called from constructor", function);
2437
2438   if (IS_SIGNATURE (basetype))
2439     {
2440       if (static_call_context)
2441         {
2442           cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2443                     basetype, save_name);
2444           return error_mark_node;
2445         }
2446       return build_signature_method_call (function, parms);
2447     }
2448
2449   function = DECL_MAIN_VARIANT (function);
2450   mark_used (function);
2451
2452   fntype = TREE_TYPE (function);
2453   if (TREE_CODE (fntype) == POINTER_TYPE)
2454     fntype = TREE_TYPE (fntype);
2455   basetype = DECL_CLASS_CONTEXT (function);
2456
2457   /* If we are referencing a virtual function from an object
2458      of effectively static type, then there is no need
2459      to go through the virtual function table.  */
2460   if (need_vtbl == maybe_needed)
2461     {
2462       int fixed_type = resolves_to_fixed_type_p (instance, 0);
2463
2464       if (all_virtual == 1
2465           && DECL_VINDEX (function)
2466           && may_be_remote (basetype))
2467         need_vtbl = needed;
2468       else if (DECL_VINDEX (function))
2469         need_vtbl = fixed_type ? unneeded : needed;
2470       else
2471         need_vtbl = not_needed;
2472     }
2473
2474   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
2475       && !DECL_CONSTRUCTOR_P (function))
2476     {
2477       /* Let's be nasty to the user now, and give reasonable
2478          error messages.  */
2479       instance_ptr = current_class_ptr;
2480       if (instance_ptr)
2481         {
2482           if (basetype != current_class_type)
2483             {
2484               if (basetype == error_mark_node)
2485                 return error_mark_node;
2486               else 
2487                 {
2488                   if (orig_basetype != NULL_TREE)
2489                     error_not_base_type (orig_basetype, current_class_type);
2490                   else
2491                     error_not_base_type (function, current_class_type);
2492                   return error_mark_node;
2493                 }
2494             }
2495         }
2496       /* Only allow a static member function to call another static member
2497          function.  */
2498       else if (DECL_LANG_SPECIFIC (function)
2499                && !DECL_STATIC_FUNCTION_P (function))
2500         {
2501           cp_error ("cannot call member function `%D' without object",
2502                     function);
2503           return error_mark_node;
2504         }
2505     }
2506
2507   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2508
2509   if (TYPE_SIZE (complete_type (value_type)) == 0)
2510     {
2511       if (flags & LOOKUP_COMPLAIN)
2512         incomplete_type_error (0, value_type);
2513       return error_mark_node;
2514     }
2515
2516   if (DECL_STATIC_FUNCTION_P (function))
2517     parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2518                                TREE_CHAIN (parms), function, LOOKUP_NORMAL);
2519   else if (need_vtbl == unneeded)
2520     {
2521       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
2522       basetype = TREE_TYPE (instance);
2523       if (TYPE_METHOD_BASETYPE (TREE_TYPE (function))
2524           != TYPE_MAIN_VARIANT (basetype))
2525         {
2526           basetype = DECL_CLASS_CONTEXT (function);
2527           instance_ptr = convert_pointer_to (basetype, instance_ptr);
2528           instance = build_indirect_ref (instance_ptr, NULL_PTR);
2529         }
2530       parms = tree_cons (NULL_TREE, instance_ptr,
2531                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, sub_flags));
2532     }
2533   else
2534     {
2535       if ((flags & LOOKUP_NONVIRTUAL) == 0)
2536         basetype = DECL_CONTEXT (function);
2537
2538       /* First parm could be integer_zerop with casts like
2539          ((Object*)0)->Object::IsA()  */
2540       if (!integer_zerop (TREE_VALUE (parms)))
2541         {
2542           /* Since we can't have inheritance with a union, doing get_binfo
2543              on it won't work.  We do all the convert_pointer_to_real
2544              stuff to handle MI correctly...for unions, that's not
2545              an issue, so we must short-circuit that extra work here.  */
2546           tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
2547           if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
2548             instance_ptr = TREE_VALUE (parms);
2549           else
2550             {
2551               tree binfo = get_binfo (basetype,
2552                                       TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
2553                                       0);
2554               instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
2555             }
2556           instance_ptr
2557             = convert_pointer_to (build_type_variant (basetype,
2558                                                       constp, volatilep),
2559                                   instance_ptr);
2560
2561           if (TREE_CODE (instance_ptr) == COND_EXPR)
2562             {
2563               instance_ptr = save_expr (instance_ptr);
2564               instance = build_indirect_ref (instance_ptr, NULL_PTR);
2565             }
2566           else if (TREE_CODE (instance_ptr) == NOP_EXPR
2567                    && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
2568                    && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
2569             ;
2570           /* The call to `convert_pointer_to' may return error_mark_node.  */
2571           else if (TREE_CODE (instance_ptr) == ERROR_MARK)
2572             return instance_ptr;
2573           else if (instance == NULL_TREE
2574                    || TREE_CODE (instance) != INDIRECT_REF
2575                    || TREE_OPERAND (instance, 0) != instance_ptr)
2576             instance = build_indirect_ref (instance_ptr, NULL_PTR);
2577         }
2578       parms = tree_cons (NULL_TREE, instance_ptr,
2579                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, LOOKUP_NORMAL));
2580     }
2581
2582   if (parms == error_mark_node
2583       || (parms && TREE_CHAIN (parms) == error_mark_node))
2584     return error_mark_node;
2585
2586   if (need_vtbl == needed)
2587     {
2588       function = build_vfn_ref (&TREE_VALUE (parms), instance,
2589                                 DECL_VINDEX (function));
2590       TREE_TYPE (function) = build_pointer_type (fntype);
2591     }
2592
2593   if (TREE_CODE (function) == FUNCTION_DECL)
2594     GNU_xref_call (current_function_decl,
2595                    IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
2596
2597   result = build_call (function, value_type, parms);
2598   if (IS_AGGR_TYPE (value_type))
2599     result = build_cplus_new (value_type, result);
2600   result = convert_from_reference (result);
2601   return result;
2602 }
2603
2604 /* Similar to `build_method_call', but for overloaded non-member functions.
2605    The name of this function comes through NAME.  The name depends
2606    on PARMS.
2607
2608    Note that this function must handle simple `C' promotions,
2609    as well as variable numbers of arguments (...), and
2610    default arguments to boot.
2611
2612    If the overloading is successful, we return a tree node which
2613    contains the call to the function.
2614
2615    If overloading produces candidates which are probable, but not definite,
2616    we hold these candidates.  If FINAL_CP is non-zero, then we are free
2617    to assume that final_cp points to enough storage for all candidates that
2618    this function might generate.  The `harshness' array is preallocated for
2619    the first candidate, but not for subsequent ones.
2620
2621    Note that the DECL_RTL of FUNCTION must be made to agree with this
2622    function's new name.  */
2623
2624 tree
2625 build_overload_call_real (fnname, parms, flags, final_cp, require_complete)
2626      tree fnname, parms;
2627      int flags;
2628      struct candidate *final_cp;
2629      int require_complete;
2630 {
2631   /* must check for overloading here */
2632   tree functions, function;
2633   tree parmtypes, last;
2634   register tree outer;
2635   int length;
2636   int parmlength = list_length (parms);
2637
2638   struct candidate *candidates, *cp;
2639
2640   if (final_cp)
2641     {
2642       final_cp[0].h.code = 0;
2643       final_cp[0].h.distance = 0;
2644       final_cp[0].function = 0;
2645       /* end marker.  */
2646       final_cp[1].h.code = EVIL_CODE;
2647     }
2648
2649   parmtypes = default_parm_conversions (parms, &last);
2650   if (parmtypes == error_mark_node)
2651     {
2652       if (final_cp)
2653         final_cp->h.code = EVIL_CODE;
2654       return error_mark_node;
2655     }
2656
2657   if (last)
2658     TREE_CHAIN (last) = void_list_node;
2659   else
2660     parmtypes = void_list_node;
2661
2662   if (is_overloaded_fn (fnname))
2663     {
2664       functions = fnname;
2665       if (TREE_CODE (fnname) == TREE_LIST)
2666         fnname = TREE_PURPOSE (functions);
2667       else if (TREE_CODE (fnname) == FUNCTION_DECL)
2668         fnname = DECL_NAME (functions);
2669     }
2670   else 
2671     functions = lookup_name_nonclass (fnname);
2672
2673   if (functions == NULL_TREE)
2674     {
2675       if (flags & LOOKUP_SPECULATIVELY)
2676         return NULL_TREE;
2677       if (flags & LOOKUP_COMPLAIN)
2678         error ("only member functions apply");
2679       if (final_cp)
2680         final_cp->h.code = EVIL_CODE;
2681       return error_mark_node;
2682     }
2683
2684   if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
2685     {
2686       functions = DECL_MAIN_VARIANT (functions);
2687       if (final_cp)
2688         {
2689           /* We are just curious whether this is a viable alternative or
2690              not.  */
2691           compute_conversion_costs (functions, parms, final_cp, parmlength);
2692           return functions;
2693         }
2694       else
2695         return build_function_call_real (functions, parms, 1, flags);
2696     }
2697
2698   if (TREE_CODE (functions) == TREE_LIST
2699       && TREE_VALUE (functions) == NULL_TREE)
2700     {
2701       if (flags & LOOKUP_SPECULATIVELY)
2702         return NULL_TREE;
2703       
2704       if (flags & LOOKUP_COMPLAIN)
2705         cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2706                   TREE_PURPOSE (functions));
2707       if (final_cp)
2708         final_cp->h.code = EVIL_CODE;
2709       return error_mark_node;
2710     }
2711
2712   length = count_functions (functions);
2713   
2714   if (final_cp)
2715     candidates = final_cp;
2716   else
2717     {
2718       candidates
2719         = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
2720       bzero ((char *) candidates, (length + 1) * sizeof (struct candidate));
2721     }
2722
2723   cp = candidates;
2724
2725   my_friendly_assert (is_overloaded_fn (functions), 169);
2726
2727   functions = get_first_fn (functions);
2728
2729   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
2730   for (outer = functions; outer; outer = DECL_CHAIN (outer))
2731     {
2732       int template_cost = 0;
2733       function = outer;
2734       if (TREE_CODE (function) != FUNCTION_DECL
2735           && ! (TREE_CODE (function) == TEMPLATE_DECL
2736                 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
2737         {
2738           enum tree_code code = TREE_CODE (function);
2739           if (code == TEMPLATE_DECL)
2740             code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
2741           if (code == CONST_DECL)
2742             cp_error_at
2743               ("enumeral value `%D' conflicts with function of same name",
2744                function);
2745           else if (code == VAR_DECL)
2746             {
2747               if (TREE_STATIC (function))
2748                 cp_error_at
2749                   ("variable `%D' conflicts with function of same name",
2750                    function);
2751               else
2752                 cp_error_at
2753                   ("constant field `%D' conflicts with function of same name",
2754                    function);
2755             }
2756           else if (code == TYPE_DECL)
2757             continue;
2758           else
2759             my_friendly_abort (2);
2760           error ("at this point in file");
2761           continue;
2762         }
2763       if (TREE_CODE (function) == TEMPLATE_DECL)
2764         {
2765           int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
2766           tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
2767           int i;
2768
2769           i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
2770                                 TYPE_ARG_TYPES (TREE_TYPE (function)),
2771                                 parms, &template_cost, 0, 0);
2772           if (i == 0)
2773             {
2774               function = instantiate_template (function, targs);
2775               if (function == error_mark_node)
2776                 return function;
2777             }
2778         }
2779
2780       if (TREE_CODE (function) == TEMPLATE_DECL)
2781         {
2782           /* Unconverted template -- failed match.  */
2783           cp->function = function;
2784           cp->u.bad_arg = -4;
2785           cp->h.code = EVIL_CODE;
2786         }
2787       else
2788         {
2789           struct candidate *cp2;
2790
2791           /* Check that this decl is not the same as a function that's in
2792              the list due to some template instantiation.  */
2793           cp2 = candidates;
2794           while (cp2 != cp)
2795             if (cp2->function == function)
2796               break;
2797             else
2798               cp2 += 1;
2799           if (cp2->function == function)
2800             continue;
2801
2802           function = DECL_MAIN_VARIANT (function);
2803
2804           /* Can't use alloca here, since result might be
2805              passed to calling function.  */
2806           cp->h_len = parmlength;
2807           cp->harshness = (struct harshness_code *)
2808             oballoc ((parmlength + 1) * sizeof (struct harshness_code));
2809
2810           compute_conversion_costs (function, parms, cp, parmlength);
2811
2812           /* Make sure this is clear as well.  */
2813           cp->h.int_penalty += template_cost;
2814
2815           if ((cp[0].h.code & EVIL_CODE) == 0)
2816             {
2817               cp[1].h.code = EVIL_CODE;
2818               cp++;
2819             }
2820         }
2821     }
2822
2823   if (cp - candidates)
2824     {
2825       tree rval = error_mark_node;
2826
2827       /* Leave marker.  */
2828       cp[0].h.code = EVIL_CODE;
2829       if (cp - candidates > 1)
2830         {
2831           struct candidate *best_cp
2832             = ideal_candidate (candidates, cp - candidates, parmlength);
2833           if (best_cp == (struct candidate *)0)
2834             {
2835               if (flags & LOOKUP_COMPLAIN)
2836                 {
2837                   cp_error ("call of overloaded `%D' is ambiguous", fnname);
2838                   print_n_candidates (candidates, cp - candidates);
2839                 }
2840               return error_mark_node;
2841             }
2842           else
2843             rval = best_cp->function;
2844         }
2845       else
2846         {
2847           cp -= 1;
2848           if (cp->h.code & EVIL_CODE)
2849             {
2850               if (flags & LOOKUP_COMPLAIN)
2851                 error ("type conversion ambiguous");
2852             }
2853           else
2854             rval = cp->function;
2855         }
2856
2857       if (final_cp)
2858         return rval;
2859
2860       return build_function_call_real (rval, parms, require_complete, flags);
2861     }
2862
2863   if (flags & LOOKUP_SPECULATIVELY)
2864     return NULL_TREE;
2865   
2866   if (flags & LOOKUP_COMPLAIN)
2867     report_type_mismatch (cp, parms, "function",
2868                           decl_as_string (cp->function, 1));
2869
2870   return error_mark_node;
2871 }
2872
2873 /* This requires a complete type on the result of the call.  */
2874
2875 tree
2876 build_overload_call (fnname, parms, flags)
2877      tree fnname, parms;
2878      int flags;
2879 {
2880   return build_overload_call_real (fnname, parms, flags, (struct candidate *)0, 1);
2881 }
2882
2883 /* New overloading code.  */
2884
2885 struct z_candidate {
2886   tree fn;
2887   tree convs;
2888   tree second_conv;
2889   int viable;
2890   tree basetype_path;
2891   tree template;
2892   struct z_candidate *next;
2893 };
2894
2895 #define IDENTITY_RANK 0
2896 #define EXACT_RANK 1
2897 #define PROMO_RANK 2
2898 #define STD_RANK 3
2899 #define PBOOL_RANK 4
2900 #define USER_RANK 5
2901 #define ELLIPSIS_RANK 6
2902 #define BAD_RANK 7
2903
2904 #define ICS_RANK(NODE)                          \
2905   (ICS_BAD_FLAG (NODE) ? BAD_RANK   \
2906    : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK   \
2907    : ICS_USER_FLAG (NODE) ? USER_RANK           \
2908    : ICS_STD_RANK (NODE))
2909
2910 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
2911
2912 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
2913 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
2914 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
2915 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
2916
2917 #define USER_CONV_FN(NODE) TREE_OPERAND (NODE, 1)
2918
2919 static struct z_candidate * build_user_type_conversion_1 ();
2920 static tree convert_like ();
2921 static tree build_over_call ();
2922 static struct z_candidate * tourney ();
2923 static void enforce_access ();
2924
2925 int
2926 null_ptr_cst_p (t)
2927      tree t;
2928 {
2929   if (t == null_node
2930       || integer_zerop (t) && INTEGRAL_TYPE_P (TREE_TYPE (t)))
2931     return 1;
2932   /* Remove this eventually.  */
2933   if (! pedantic && TREE_TYPE (t) == ptr_type_node && integer_zerop (t))
2934     return 1;
2935   return 0;
2936 }
2937
2938 static tree
2939 build_conv (code, type, from)
2940      enum tree_code code;
2941      tree type, from;
2942 {
2943   tree t = build1 (code, type, from);
2944   int rank = ICS_STD_RANK (from);
2945   switch (code)
2946     {
2947     case PTR_CONV:
2948     case PMEM_CONV:
2949     case BASE_CONV:
2950     case STD_CONV:
2951       if (rank < STD_RANK)
2952         rank = STD_RANK;
2953       break;
2954
2955     case LVALUE_CONV:
2956     case QUAL_CONV:
2957     case RVALUE_CONV:
2958       if (rank < EXACT_RANK)
2959         rank = EXACT_RANK;
2960
2961     default:
2962       break;
2963     }
2964   ICS_STD_RANK (t) = rank;
2965   ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
2966   ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
2967   return t;
2968 }
2969
2970 static tree
2971 non_reference (t)
2972      tree t;
2973 {
2974   if (TREE_CODE (t) == REFERENCE_TYPE)
2975     t = TREE_TYPE (t);
2976   return t;
2977 }
2978
2979 static tree
2980 strip_top_quals (t)
2981      tree t;
2982 {
2983   if (TREE_CODE (t) == ARRAY_TYPE)
2984     return t;
2985   return TYPE_MAIN_VARIANT (t);
2986 }
2987
2988 /* Returns the standard conversion path (see [conv]) from type FROM to type
2989    TO, if any.  For proper handling of null pointer constants, you must
2990    also pass the expression EXPR to convert from.  */
2991
2992 static tree
2993 standard_conversion (to, from, expr)
2994      tree to, from, expr;
2995 {
2996   enum tree_code fcode, tcode;
2997   tree conv;
2998   int fromref = 0;
2999
3000   if (TREE_CODE (to) == REFERENCE_TYPE)
3001     to = TREE_TYPE (to);
3002   if (TREE_CODE (from) == REFERENCE_TYPE)
3003     {
3004       fromref = 1;
3005       from = TREE_TYPE (from);
3006     }
3007   to = strip_top_quals (to);
3008   from = strip_top_quals (from);
3009
3010   fcode = TREE_CODE (from);
3011   tcode = TREE_CODE (to);
3012
3013   conv = build1 (IDENTITY_CONV, from, expr);
3014
3015   if (fcode == FUNCTION_TYPE)
3016     {
3017       from = build_pointer_type (from);
3018       fcode = TREE_CODE (from);
3019       conv = build_conv (LVALUE_CONV, from, conv);
3020     }
3021   else if (fcode == ARRAY_TYPE)
3022     {
3023       from = build_pointer_type (TREE_TYPE (from));
3024       fcode = TREE_CODE (from);
3025       conv = build_conv (LVALUE_CONV, from, conv);
3026     }
3027   else if (fromref || (expr && real_lvalue_p (expr)))
3028     conv = build_conv (RVALUE_CONV, from, conv);
3029
3030   if (from == to)
3031     return conv;
3032
3033   if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
3034       && expr && null_ptr_cst_p (expr))
3035     {
3036       conv = build_conv (STD_CONV, to, conv);
3037     }
3038   else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
3039     {
3040       enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
3041       enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
3042       tree nconv = NULL_TREE;
3043
3044       if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
3045                      TYPE_MAIN_VARIANT (TREE_TYPE (to)), 1))
3046         nconv = conv;
3047       else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
3048                && ufcode != FUNCTION_TYPE)
3049         {
3050           from = build_pointer_type
3051             (cp_build_type_variant (void_type_node,
3052                                     TYPE_READONLY (TREE_TYPE (from)),
3053                                     TYPE_VOLATILE (TREE_TYPE (from))));
3054           nconv = build_conv (PTR_CONV, from, conv);
3055         }
3056       else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
3057         {
3058           tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
3059           tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
3060
3061           if (DERIVED_FROM_P (fbase, tbase)
3062               && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
3063                              TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))),
3064                              1)))
3065             {
3066               from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
3067               from = build_pointer_type (from);
3068               nconv = build_conv (PMEM_CONV, from, conv);
3069             }
3070         }
3071       else if (IS_AGGR_TYPE (TREE_TYPE (from))
3072                && IS_AGGR_TYPE (TREE_TYPE (to)))
3073         {
3074           if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
3075             {
3076               from = cp_build_type_variant (TREE_TYPE (to),
3077                                             TYPE_READONLY (TREE_TYPE (from)),
3078                                             TYPE_VOLATILE (TREE_TYPE (from)));
3079               from = build_pointer_type (from);
3080               nconv = build_conv (PTR_CONV, from, conv);
3081             }
3082         }
3083
3084       if (nconv && comptypes (from, to, 1))
3085         conv = nconv;
3086       else if (nconv && comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
3087         conv = build_conv (QUAL_CONV, to, nconv);
3088       else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
3089         {
3090           conv = build_conv (PTR_CONV, to, conv);
3091           ICS_BAD_FLAG (conv) = 1;
3092         }
3093       else
3094         return 0;
3095
3096       from = to;
3097     }
3098   else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
3099     {
3100       tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
3101       tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
3102       tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
3103       tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
3104
3105       if (! DERIVED_FROM_P (fbase, tbase)
3106           || ! comptypes (TREE_TYPE (fromfn), TREE_TYPE (tofn), 1)
3107           || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
3108                           TREE_CHAIN (TYPE_ARG_TYPES (tofn)), 1)
3109           || TYPE_READONLY (fbase) != TYPE_READONLY (tbase)
3110           || TYPE_VOLATILE (fbase) != TYPE_VOLATILE (tbase))
3111         return 0;
3112
3113       from = cp_build_type_variant (tbase, TYPE_READONLY (fbase),
3114                                     TYPE_VOLATILE (fbase));
3115       from = build_cplus_method_type (from, TREE_TYPE (fromfn),
3116                                       TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
3117       from = build_ptrmemfunc_type (build_pointer_type (from));
3118       conv = build_conv (PMEM_CONV, from, conv);
3119     }
3120   else if (tcode == BOOLEAN_TYPE)
3121     {
3122       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
3123              || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
3124         return 0;
3125
3126       conv = build_conv (STD_CONV, to, conv);
3127       if (fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)
3128           && ICS_STD_RANK (conv) < PBOOL_RANK)
3129         ICS_STD_RANK (conv) = PBOOL_RANK;
3130     }
3131   /* We don't check for ENUMERAL_TYPE here because there are no standard
3132      conversions to enum type.  */
3133   else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
3134            || tcode == REAL_TYPE)
3135     {
3136       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
3137         return 0;
3138       conv = build_conv (STD_CONV, to, conv);
3139
3140       /* Give this a better rank if it's a promotion.  */
3141       if (to == type_promotes_to (from)
3142           && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
3143         ICS_STD_RANK (conv) = PROMO_RANK;
3144     }
3145   else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
3146            && DERIVED_FROM_P (to, from))
3147     conv = build_conv (BASE_CONV, to, conv);
3148   else
3149     return 0;
3150
3151   return conv;
3152 }
3153
3154 /* Returns the conversion path from type FROM to reference type TO for
3155    purposes of reference binding.  For lvalue binding, either pass a
3156    reference type to FROM or an lvalue expression to EXPR.
3157
3158    Currently does not distinguish in the generated trees between binding to
3159    an lvalue and a temporary.  Should it?  */
3160
3161 static tree
3162 reference_binding (rto, rfrom, expr, flags)
3163      tree rto, rfrom, expr;
3164      int flags;
3165 {
3166   tree conv;
3167   int lvalue = 1;
3168   tree to = TREE_TYPE (rto);
3169   tree from = rfrom;
3170
3171   if (TREE_CODE (from) == REFERENCE_TYPE)
3172     from = TREE_TYPE (from);
3173   else if (! expr || ! real_lvalue_p (expr))
3174     lvalue = 0;
3175
3176   if (lvalue
3177       && TYPE_READONLY (to) >= TYPE_READONLY (from)
3178       && TYPE_VOLATILE (to) >= TYPE_VOLATILE (from))
3179     {
3180       conv = build1 (IDENTITY_CONV, from, expr);
3181
3182       if (TYPE_MAIN_VARIANT (to) == TYPE_MAIN_VARIANT (from))
3183         conv = build_conv (REF_BIND, rto, conv);
3184       else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
3185                && DERIVED_FROM_P (to, from))
3186         {
3187           conv = build_conv (REF_BIND, rto, conv);
3188           ICS_STD_RANK (conv) = STD_RANK;
3189         }
3190       else
3191         conv = NULL_TREE;
3192     }
3193   else
3194     conv = NULL_TREE;
3195
3196   if (! conv)
3197     {
3198       conv = standard_conversion (to, rfrom, expr);
3199       if (conv)
3200         {
3201           conv = build_conv (REF_BIND, rto, conv);
3202
3203           /* Bind directly to a base subobject of a class rvalue.  Do it
3204              after building the conversion for proper handling of ICS_RANK.  */
3205           if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
3206             TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
3207         }
3208       if (conv && ! (TYPE_READONLY (to) && ! TYPE_VOLATILE (to)
3209                      && (flags & LOOKUP_NO_TEMP_BIND) == 0))
3210         ICS_BAD_FLAG (conv) = 1;
3211     }
3212
3213   return conv;
3214 }
3215
3216 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
3217    to type TO.  The optional expression EXPR may affect the conversion.
3218    FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
3219    significant.  */
3220
3221 static tree
3222 implicit_conversion (to, from, expr, flags)
3223      tree to, from, expr;
3224      int flags;
3225 {
3226   tree conv;
3227   struct z_candidate *cand;
3228
3229   if (expr && type_unknown_p (expr))
3230     {
3231       expr = instantiate_type (to, expr, 0);
3232       if (expr == error_mark_node)
3233         return 0;
3234       from = TREE_TYPE (expr);
3235     }
3236
3237   if (TREE_CODE (to) == REFERENCE_TYPE)
3238     conv = reference_binding (to, from, expr, flags);
3239   else
3240     conv = standard_conversion (to, from, expr);
3241
3242   if (conv)
3243     ;
3244   else if ((IS_AGGR_TYPE (non_reference (from))
3245             || IS_AGGR_TYPE (non_reference (to)))
3246            && (flags & LOOKUP_NO_CONVERSION) == 0)
3247     {
3248       if (TREE_CODE (to) == REFERENCE_TYPE
3249           && TYPE_READONLY (TREE_TYPE (to))
3250           && ! TYPE_VOLATILE (TREE_TYPE (to))
3251           && (flags & LOOKUP_NO_TEMP_BIND) == 0)
3252         {
3253           cand = build_user_type_conversion_1
3254             (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
3255           if (cand)
3256             conv = build_conv (REF_BIND, to, cand->second_conv);
3257         }
3258       else
3259         {
3260           cand = build_user_type_conversion_1
3261             (to, expr, LOOKUP_ONLYCONVERTING);
3262           if (cand)
3263             conv = cand->second_conv;
3264         }
3265     }
3266
3267   return conv;
3268 }
3269
3270 /* Create an overload candidate for the function or method FN called with
3271    the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
3272    to implicit_conversion.  */
3273
3274 static struct z_candidate *
3275 add_function_candidate (candidates, fn, arglist, flags)
3276      struct z_candidate *candidates;
3277      tree fn, arglist;
3278      int flags;
3279 {
3280   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
3281   int i, len;
3282   tree convs;
3283   tree parmnode = parmlist;
3284   tree argnode = arglist;
3285   int viable = 1;
3286   struct z_candidate *cand;
3287
3288   /* The `this' and `in_chrg' arguments to constructors are not considered
3289      in overload resolution.  */
3290   if (DECL_CONSTRUCTOR_P (fn))
3291     {
3292       parmnode = TREE_CHAIN (parmnode);
3293       argnode = TREE_CHAIN (argnode);
3294       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3295         {
3296           parmnode = TREE_CHAIN (parmnode);
3297           argnode = TREE_CHAIN (argnode);
3298         }
3299     }
3300
3301   len = list_length (argnode);
3302   convs = make_tree_vec (len);
3303
3304   for (i = 0; i < len; ++i)
3305     {
3306       tree arg = TREE_VALUE (argnode);
3307       tree argtype = TREE_TYPE (arg);
3308       tree t;
3309
3310       argtype = cp_build_type_variant
3311         (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
3312
3313       if (parmnode == void_list_node)
3314         break;
3315       else if (parmnode)
3316         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
3317       else
3318         {
3319           t = build1 (IDENTITY_CONV, argtype, arg);
3320           ICS_ELLIPSIS_FLAG (t) = 1;
3321         }
3322
3323       if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
3324           && ! DECL_CONSTRUCTOR_P (fn))
3325         ICS_THIS_FLAG (t) = 1;
3326
3327       TREE_VEC_ELT (convs, i) = t;
3328       if (! t)
3329         break;
3330
3331       if (ICS_BAD_FLAG (t))
3332         viable = -1;
3333
3334       if (parmnode)
3335         parmnode = TREE_CHAIN (parmnode);
3336       argnode = TREE_CHAIN (argnode);
3337     }
3338
3339   if (i < len)
3340     viable = 0;
3341
3342   /* Make sure there are default args for the rest of the parms.  */
3343   for (; parmnode && parmnode != void_list_node;
3344        parmnode = TREE_CHAIN (parmnode))
3345     if (! TREE_PURPOSE (parmnode))
3346       {
3347         viable = 0;
3348         break;
3349       }
3350
3351   cand = (struct z_candidate *) oballoc (sizeof (struct z_candidate));
3352
3353   cand->fn = fn;
3354   cand->convs = convs;
3355   cand->second_conv = NULL_TREE;
3356   cand->viable = viable;
3357   cand->basetype_path = NULL_TREE;
3358   cand->template = NULL_TREE;
3359   cand->next = candidates;
3360
3361   return cand;
3362 }
3363
3364 /* Create an overload candidate for the conversion function FN which will
3365    be invoked for expression OBJ, producing a pointer-to-function which
3366    will in turn be called with the argument list ARGLIST, and add it to
3367    CANDIDATES.  FLAGS is passed on to implicit_conversion.  */
3368
3369 static struct z_candidate *
3370 add_conv_candidate (candidates, fn, obj, arglist)
3371      struct z_candidate *candidates;
3372      tree fn, obj, arglist;
3373 {
3374   tree totype = TREE_TYPE (TREE_TYPE (fn));
3375   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
3376   int i, len = list_length (arglist) + 1;
3377   tree convs = make_tree_vec (len);
3378   tree parmnode = parmlist;
3379   tree argnode = arglist;
3380   int viable = 1;
3381   struct z_candidate *cand;
3382   int flags = LOOKUP_NORMAL;
3383
3384   for (i = 0; i < len; ++i)
3385     {
3386       tree arg = i == 0 ? obj : TREE_VALUE (argnode);
3387       tree argtype = lvalue_type (arg);
3388       tree t;
3389
3390       if (i == 0)
3391         t = implicit_conversion (totype, argtype, arg, flags);
3392       else if (parmnode == void_list_node)
3393         break;
3394       else if (parmnode)
3395         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
3396       else
3397         {
3398           t = build1 (IDENTITY_CONV, argtype, arg);
3399           ICS_ELLIPSIS_FLAG (t) = 1;
3400         }
3401
3402       TREE_VEC_ELT (convs, i) = t;
3403       if (! t)
3404         break;
3405
3406       if (ICS_BAD_FLAG (t))
3407         viable = -1;
3408
3409       if (i == 0)
3410         continue;
3411
3412       if (parmnode)
3413         parmnode = TREE_CHAIN (parmnode);
3414       argnode = TREE_CHAIN (argnode);
3415     }
3416
3417   if (i < len)
3418     viable = 0;
3419
3420   for (; parmnode && parmnode != void_list_node;
3421        parmnode = TREE_CHAIN (parmnode))
3422     if (! TREE_PURPOSE (parmnode))
3423       {
3424         viable = 0;
3425         break;
3426       }
3427
3428   cand = (struct z_candidate *) oballoc (sizeof (struct z_candidate));
3429
3430   cand->fn = fn;
3431   cand->convs = convs;
3432   cand->second_conv = NULL_TREE;
3433   cand->viable = viable;
3434   cand->basetype_path = NULL_TREE;
3435   cand->template = NULL_TREE;
3436   cand->next = candidates;
3437
3438   return cand;
3439 }
3440
3441 static struct z_candidate *
3442 build_builtin_candidate (candidates, fnname, type1, type2,
3443                          args, argtypes, flags)
3444      struct z_candidate *candidates;
3445      tree fnname, type1, type2, *args, *argtypes;
3446      int flags;
3447
3448 {
3449   tree t, convs;
3450   int viable = 1, i;
3451   struct z_candidate *cand;
3452   tree types[2];
3453
3454   types[0] = type1;
3455   types[1] = type2;
3456
3457   convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
3458
3459   for (i = 0; i < 2; ++i)
3460     {
3461       if (! args[i])
3462         break;
3463
3464       t = implicit_conversion (types[i], argtypes[i], args[i], flags);
3465       if (! t)
3466         {
3467           viable = 0;
3468           /* We need something for printing the candidate.  */
3469           t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
3470         }
3471       else if (ICS_BAD_FLAG (t))
3472         viable = 0;
3473       TREE_VEC_ELT (convs, i) = t;
3474     }
3475
3476   /* For COND_EXPR we rearranged the arguments; undo that now.  */
3477   if (args[2])
3478     {
3479       TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
3480       TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
3481       t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
3482       if (t)
3483         TREE_VEC_ELT (convs, 0) = t;
3484       else
3485         viable = 0;
3486     }      
3487
3488   cand = (struct z_candidate *) oballoc (sizeof (struct z_candidate));
3489
3490   cand->fn = fnname;
3491   cand->convs = convs;
3492   cand->second_conv = NULL_TREE;
3493   cand->viable = viable;
3494   cand->basetype_path = NULL_TREE;
3495   cand->template = NULL_TREE;
3496   cand->next = candidates;
3497
3498   return cand;
3499 }
3500
3501 static int
3502 is_complete (t)
3503      tree t;
3504 {
3505   return TYPE_SIZE (complete_type (t)) != NULL_TREE;
3506 }
3507
3508 /* Create any builtin operator overload candidates for the operator in
3509    question given the converted operand types TYPE1 and TYPE2.  The other
3510    args are passed through from add_builtin_candidates to
3511    build_builtin_candidate.  */
3512
3513 static struct z_candidate *
3514 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
3515                        args, argtypes, flags)
3516      struct z_candidate *candidates;
3517      enum tree_code code, code2;
3518      tree fnname, type1, type2, *args, *argtypes;
3519      int flags;
3520 {
3521   switch (code)
3522     {
3523     case POSTINCREMENT_EXPR:
3524     case POSTDECREMENT_EXPR:
3525       args[1] = integer_zero_node;
3526       type2 = integer_type_node;
3527     }
3528
3529   switch (code)
3530     {
3531
3532 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
3533      and  VQ  is  either  volatile or empty, there exist candidate operator
3534      functions of the form
3535              VQ T&   operator++(VQ T&);
3536              T       operator++(VQ T&, int);
3537    5 For every pair T, VQ), where T is an enumeration type or an arithmetic
3538      type  other than bool, and VQ is either volatile or empty, there exist
3539      candidate operator functions of the form
3540              VQ T&   operator--(VQ T&);
3541              T       operator--(VQ T&, int);
3542    6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
3543      complete  object type, and VQ is either volatile or empty, there exist
3544      candidate operator functions of the form
3545              T*VQ&   operator++(T*VQ&);
3546              T*VQ&   operator--(T*VQ&);
3547              T*      operator++(T*VQ&, int);
3548              T*      operator--(T*VQ&, int);  */
3549
3550     case POSTDECREMENT_EXPR:
3551     case PREDECREMENT_EXPR:
3552       if (TREE_CODE (type1) == BOOLEAN_TYPE)
3553         return candidates;
3554     case POSTINCREMENT_EXPR:
3555     case PREINCREMENT_EXPR:
3556       if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
3557           || TYPE_PTROB_P (type1))
3558         {
3559           type1 = build_reference_type (type1);
3560           break;
3561         }
3562       return candidates;
3563
3564 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
3565      exist candidate operator functions of the form
3566
3567              T&      operator*(T*);
3568
3569    8 For every function type T, there exist candidate operator functions of
3570      the form
3571              T&      operator*(T*);  */
3572
3573     case INDIRECT_REF:
3574       if (TREE_CODE (type1) == POINTER_TYPE
3575           && (TYPE_PTROB_P (type1)
3576               || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
3577         break;
3578       return candidates;
3579
3580 /* 9 For every type T, there exist candidate operator functions of the form
3581              T*      operator+(T*);
3582
3583    10For  every  promoted arithmetic type T, there exist candidate operator
3584      functions of the form
3585              T       operator+(T);
3586              T       operator-(T);  */
3587
3588     case CONVERT_EXPR: /* unary + */
3589       if (TREE_CODE (type1) == POINTER_TYPE
3590           && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
3591         break;
3592     case NEGATE_EXPR:
3593       if (ARITHMETIC_TYPE_P (type1))
3594         break;
3595       return candidates;
3596
3597 /* 11For every promoted integral type T,  there  exist  candidate  operator
3598      functions of the form
3599              T       operator~(T);  */
3600
3601     case BIT_NOT_EXPR:
3602       if (INTEGRAL_TYPE_P (type1))
3603         break;
3604       return candidates;
3605
3606 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
3607      is the same type as C2 or is a derived class of C2, T  is  a  complete
3608      object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
3609      there exist candidate operator functions of the form
3610              CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3611      where CV12 is the union of CV1 and CV2.  */
3612
3613     case MEMBER_REF:
3614       if (TREE_CODE (type1) == POINTER_TYPE
3615           && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
3616         {
3617           tree c1 = TREE_TYPE (type1);
3618           tree c2 = (TYPE_PTRMEMFUNC_P (type2)
3619                      ? TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2))
3620                      : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
3621
3622           if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
3623               && (TYPE_PTRMEMFUNC_P (type2)
3624                   || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
3625             break;
3626         }
3627       return candidates;
3628
3629 /* 13For every pair of promoted arithmetic types L and R, there exist  can-
3630      didate operator functions of the form
3631              LR      operator*(L, R);
3632              LR      operator/(L, R);
3633              LR      operator+(L, R);
3634              LR      operator-(L, R);
3635              bool    operator<(L, R);
3636              bool    operator>(L, R);
3637              bool    operator<=(L, R);
3638              bool    operator>=(L, R);
3639              bool    operator==(L, R);
3640              bool    operator!=(L, R);
3641      where  LR  is  the  result of the usual arithmetic conversions between
3642      types L and R.
3643
3644    14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
3645      unqualified  complete  object  type and I is a promoted integral type,
3646      there exist candidate operator functions of the form
3647              T*      operator+(T*, I);
3648              T&      operator[](T*, I);
3649              T*      operator-(T*, I);
3650              T*      operator+(I, T*);
3651              T&      operator[](I, T*);
3652
3653    15For every T, where T is a pointer to complete object type, there exist
3654      candidate operator functions of the form112)
3655              ptrdiff_t operator-(T, T);
3656
3657    16For  every pointer type T, there exist candidate operator functions of
3658      the form
3659              bool    operator<(T, T);
3660              bool    operator>(T, T);
3661              bool    operator<=(T, T);
3662              bool    operator>=(T, T);
3663              bool    operator==(T, T);
3664              bool    operator!=(T, T);
3665
3666    17For every pointer to member type T,  there  exist  candidate  operator
3667      functions of the form
3668              bool    operator==(T, T);
3669              bool    operator!=(T, T);  */
3670
3671     case MINUS_EXPR:
3672       if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3673         break;
3674       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
3675         {
3676           type2 = ptrdiff_type_node;
3677           break;
3678         }
3679     case MULT_EXPR:
3680     case TRUNC_DIV_EXPR:
3681       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3682         break;
3683       return candidates;
3684
3685     case EQ_EXPR:
3686     case NE_EXPR:
3687       if (TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)
3688           || TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
3689         break;
3690       if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
3691           && null_ptr_cst_p (args[1]))
3692         {
3693           type2 = type1;
3694           break;
3695         }
3696       if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
3697           && null_ptr_cst_p (args[0]))
3698         {
3699           type1 = type2;
3700           break;
3701         }
3702     case LT_EXPR:
3703     case GT_EXPR:
3704     case LE_EXPR:
3705     case GE_EXPR:
3706     case MAX_EXPR:
3707     case MIN_EXPR:
3708       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)
3709           || TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3710         break;
3711       if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
3712         {
3713           type2 = type1;
3714           break;
3715         }
3716       if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
3717         {
3718           type1 = type2;
3719           break;
3720         }
3721       return candidates;
3722
3723     case PLUS_EXPR:
3724       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3725         break;
3726     case ARRAY_REF:
3727       if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
3728         {
3729           type1 = ptrdiff_type_node;
3730           break;
3731         }
3732       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
3733         {
3734           type2 = ptrdiff_type_node;
3735           break;
3736         }
3737       return candidates;
3738
3739 /* 18For  every pair of promoted integral types L and R, there exist candi-
3740      date operator functions of the form
3741              LR      operator%(L, R);
3742              LR      operator&(L, R);
3743              LR      operator^(L, R);
3744              LR      operator|(L, R);
3745              L       operator<<(L, R);
3746              L       operator>>(L, R);
3747      where LR is the result of the  usual  arithmetic  conversions  between
3748      types L and R.  */
3749
3750     case TRUNC_MOD_EXPR:
3751     case BIT_AND_EXPR:
3752     case BIT_IOR_EXPR:
3753     case BIT_XOR_EXPR:
3754     case LSHIFT_EXPR:
3755     case RSHIFT_EXPR:
3756       if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
3757         break;
3758       return candidates;
3759
3760 /* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
3761      type, VQ is either volatile or empty, and R is a  promoted  arithmetic
3762      type, there exist candidate operator functions of the form
3763              VQ L&   operator=(VQ L&, R);
3764              VQ L&   operator*=(VQ L&, R);
3765              VQ L&   operator/=(VQ L&, R);
3766              VQ L&   operator+=(VQ L&, R);
3767              VQ L&   operator-=(VQ L&, R);
3768
3769    20For  every  pair T, VQ), where T is any type and VQ is either volatile
3770      or empty, there exist candidate operator functions of the form
3771              T*VQ&   operator=(T*VQ&, T*);
3772
3773    21For every pair T, VQ), where T is a pointer to member type and  VQ  is
3774      either  volatile or empty, there exist candidate operator functions of
3775      the form
3776              VQ T&   operator=(VQ T&, T);
3777
3778    22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
3779      unqualified  complete object type, VQ is either volatile or empty, and
3780      I is a promoted integral type, there exist  candidate  operator  func-
3781      tions of the form
3782              T*VQ&   operator+=(T*VQ&, I);
3783              T*VQ&   operator-=(T*VQ&, I);
3784
3785    23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
3786      type, VQ is either volatile or empty, and R  is  a  promoted  integral
3787      type, there exist candidate operator functions of the form
3788
3789              VQ L&   operator%=(VQ L&, R);
3790              VQ L&   operator<<=(VQ L&, R);
3791              VQ L&   operator>>=(VQ L&, R);
3792              VQ L&   operator&=(VQ L&, R);
3793              VQ L&   operator^=(VQ L&, R);
3794              VQ L&   operator|=(VQ L&, R);  */
3795
3796     case MODIFY_EXPR:
3797       switch (code2)
3798         {
3799         case PLUS_EXPR:
3800         case MINUS_EXPR:
3801           if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
3802             {
3803               type2 = ptrdiff_type_node;
3804               break;
3805             }
3806         case MULT_EXPR:
3807         case TRUNC_DIV_EXPR:
3808           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3809             break;
3810           return candidates;
3811
3812         case TRUNC_MOD_EXPR:
3813         case BIT_AND_EXPR:
3814         case BIT_IOR_EXPR:
3815         case BIT_XOR_EXPR:
3816         case LSHIFT_EXPR:
3817         case RSHIFT_EXPR:
3818           if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
3819             break;
3820           return candidates;
3821
3822         case NOP_EXPR:
3823           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3824             break;
3825           if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3826               || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3827               || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
3828               || ((TYPE_PTRMEMFUNC_P (type1)
3829                    || TREE_CODE (type1) == POINTER_TYPE)
3830                   && null_ptr_cst_p (args[1])))
3831             {
3832               type2 = type1;
3833               break;
3834             }
3835           return candidates;
3836
3837         default:
3838           my_friendly_abort (367);
3839         }
3840       type1 = build_reference_type (type1);
3841       break;
3842
3843     case COND_EXPR:
3844       /* Kludge around broken overloading rules whereby
3845          bool ? const char& : enum is ambiguous
3846          (between int and const char&).  */
3847       flags |= LOOKUP_NO_TEMP_BIND;
3848
3849       /* Extension: Support ?: of enumeral type.  Hopefully this will not
3850          be an extension for long.  */
3851       if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
3852         break;
3853       else if (TREE_CODE (type1) == ENUMERAL_TYPE
3854                || TREE_CODE (type2) == ENUMERAL_TYPE)
3855         return candidates;
3856       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3857         break;
3858       if (TREE_CODE (type1) == TREE_CODE (type2)
3859           && (TREE_CODE (type1) == REFERENCE_TYPE
3860               || TREE_CODE (type1) == POINTER_TYPE
3861               || TYPE_PTRMEMFUNC_P (type1)
3862               || IS_AGGR_TYPE (type1)))
3863         break;
3864       if (TREE_CODE (type1) == REFERENCE_TYPE
3865           || TREE_CODE (type2) == REFERENCE_TYPE)
3866         return candidates;
3867       if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
3868            && null_ptr_cst_p (args[1]))
3869           || IS_AGGR_TYPE (type1))
3870         {
3871           type2 = type1;
3872           break;
3873         }
3874       if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
3875            && null_ptr_cst_p (args[0]))
3876           || IS_AGGR_TYPE (type2))
3877         {
3878           type1 = type2;
3879           break;
3880         }
3881       return candidates;
3882
3883     default:
3884       my_friendly_abort (367);
3885     }
3886
3887   /* If we're dealing with two pointer types, we need candidates
3888      for both of them.  */
3889   if (type2 && type1 != type2
3890       && TREE_CODE (type1) == TREE_CODE (type2)
3891       && (TREE_CODE (type1) == REFERENCE_TYPE
3892           || (TREE_CODE (type1) == POINTER_TYPE
3893               && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
3894           || TYPE_PTRMEMFUNC_P (type1)
3895           || IS_AGGR_TYPE (type1)))
3896     {
3897       candidates = build_builtin_candidate
3898         (candidates, fnname, type1, type1, args, argtypes, flags);
3899       return build_builtin_candidate
3900         (candidates, fnname, type2, type2, args, argtypes, flags);
3901     }
3902
3903   return build_builtin_candidate
3904     (candidates, fnname, type1, type2, args, argtypes, flags);
3905 }
3906
3907 tree
3908 type_decays_to (type)
3909      tree type;
3910 {
3911   if (TREE_CODE (type) == ARRAY_TYPE)
3912     return build_pointer_type (TREE_TYPE (type));
3913   if (TREE_CODE (type) == FUNCTION_TYPE)
3914     return build_pointer_type (type);
3915   return type;
3916 }
3917
3918 /* There are three conditions of builtin candidates:
3919
3920    1) bool-taking candidates.  These are the same regardless of the input.
3921    2) pointer-pair taking candidates.  These are generated for each type
3922       one of the input types converts to.
3923    3) arithmetic candidates.  According to the WP, we should generate
3924       all of these, but I'm trying not to... */
3925
3926 static struct z_candidate *
3927 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
3928      struct z_candidate *candidates;
3929      enum tree_code code, code2;
3930      tree fnname, *args;
3931      int flags;
3932 {
3933   int ref1, i;
3934   tree type, argtypes[3], types[2];
3935
3936   for (i = 0; i < 3; ++i)
3937     {
3938       if (args[i])
3939         argtypes[i]  = lvalue_type (args[i]);
3940       else
3941         argtypes[i] = NULL_TREE;
3942     }
3943
3944   switch (code)
3945     {
3946 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
3947      and  VQ  is  either  volatile or empty, there exist candidate operator
3948      functions of the form
3949                  VQ T&   operator++(VQ T&);  */
3950
3951     case POSTINCREMENT_EXPR:
3952     case PREINCREMENT_EXPR:
3953     case POSTDECREMENT_EXPR:
3954     case PREDECREMENT_EXPR:
3955     case MODIFY_EXPR:
3956       ref1 = 1;
3957       break;
3958
3959 /* 24There also exist candidate operator functions of the form
3960              bool    operator!(bool);
3961              bool    operator&&(bool, bool);
3962              bool    operator||(bool, bool);  */
3963
3964     case TRUTH_NOT_EXPR:
3965       return build_builtin_candidate
3966         (candidates, fnname, boolean_type_node,
3967          NULL_TREE, args, argtypes, flags);
3968
3969     case TRUTH_ORIF_EXPR:
3970     case TRUTH_ANDIF_EXPR:
3971       return build_builtin_candidate
3972         (candidates, fnname, boolean_type_node,
3973          boolean_type_node, args, argtypes, flags);
3974
3975     case ADDR_EXPR:
3976     case COMPOUND_EXPR:
3977     case COMPONENT_REF:
3978       return candidates;
3979
3980     default:
3981       ref1 = 0;
3982     }
3983
3984   types[0] = types[1] = NULL_TREE;
3985
3986   for (i = 0; i < 2; ++i)
3987     {
3988       if (! args[i])
3989         ;
3990       else if (IS_AGGR_TYPE (argtypes[i]))
3991         {
3992           tree convs = lookup_conversions (argtypes[i]);
3993
3994           if (code == COND_EXPR)
3995             {
3996               if (real_lvalue_p (args[i]))
3997                 types[i] = tree_cons
3998                   (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
3999
4000               types[i] = tree_cons
4001                 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
4002             }
4003                 
4004           else if (! convs || (i == 0 && code == MODIFY_EXPR
4005                                && code2 == NOP_EXPR))
4006             return candidates;
4007
4008           for (; convs; convs = TREE_CHAIN (convs))
4009             {
4010               type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
4011
4012               if (i == 0 && ref1
4013                   && (TREE_CODE (type) != REFERENCE_TYPE
4014                       || TYPE_READONLY (TREE_TYPE (type))))
4015                 continue;
4016
4017               if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
4018                 types[i] = tree_cons (NULL_TREE, type, types[i]);
4019
4020               type = non_reference (type);
4021               if (i != 0 || ! ref1)
4022                 {
4023                   type = TYPE_MAIN_VARIANT (type_decays_to (type));
4024                   if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
4025                     types[i] = tree_cons (NULL_TREE, type, types[i]);
4026                   if (INTEGRAL_TYPE_P (type))
4027                     type = type_promotes_to (type);
4028                 }
4029
4030               if (! value_member (type, types[i]))
4031                 types[i] = tree_cons (NULL_TREE, type, types[i]);
4032             }
4033         }
4034       else
4035         {
4036           if (code == COND_EXPR && real_lvalue_p (args[i]))
4037             types[i] = tree_cons
4038               (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
4039           type = non_reference (argtypes[i]);
4040           if (i != 0 || ! ref1)
4041             {
4042               type = TYPE_MAIN_VARIANT (type_decays_to (type));
4043               if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
4044                 types[i] = tree_cons (NULL_TREE, type, types[i]);
4045               if (INTEGRAL_TYPE_P (type))
4046                 type = type_promotes_to (type);
4047             }
4048           types[i] = tree_cons (NULL_TREE, type, types[i]);
4049         }
4050     }
4051
4052   for (; types[0]; types[0] = TREE_CHAIN (types[0]))
4053     {
4054       if (types[1])
4055         for (type = types[1]; type; type = TREE_CHAIN (type))
4056           candidates = add_builtin_candidate
4057             (candidates, code, code2, fnname, TREE_VALUE (types[0]),
4058              TREE_VALUE (type), args, argtypes, flags);
4059       else
4060         candidates = add_builtin_candidate
4061           (candidates, code, code2, fnname, TREE_VALUE (types[0]),
4062            NULL_TREE, args, argtypes, flags);
4063     }
4064
4065   return candidates;
4066 }
4067
4068 static struct z_candidate *
4069 add_template_candidate (candidates, tmpl, arglist, flags)
4070      struct z_candidate *candidates;
4071      tree tmpl, arglist;
4072      int flags;
4073 {
4074   int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
4075   tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
4076   struct z_candidate *cand;
4077   int i, dummy; 
4078   tree fn;
4079
4080   i = type_unification (DECL_TEMPLATE_PARMS (tmpl), targs,
4081                         TYPE_ARG_TYPES (TREE_TYPE (tmpl)),
4082                         arglist, &dummy, 0, 0);
4083   if (i != 0)
4084     return candidates;
4085
4086   fn = instantiate_template (tmpl, targs);
4087   if (fn == error_mark_node)
4088     return candidates;
4089
4090   cand = add_function_candidate (candidates, fn, arglist, flags);
4091   cand->template = DECL_TEMPLATE_INFO (fn);
4092   return cand;
4093 }
4094
4095 static int
4096 any_viable (cands)
4097      struct z_candidate *cands;
4098 {
4099   for (; cands; cands = cands->next)
4100     if (pedantic ? cands->viable == 1 : cands->viable)
4101       return 1;
4102   return 0;
4103 }
4104
4105 static struct z_candidate *
4106 splice_viable (cands)
4107      struct z_candidate *cands;
4108 {
4109   struct z_candidate **p = &cands;
4110
4111   for (; *p; )
4112     {
4113       if (pedantic ? (*p)->viable == 1 : (*p)->viable)
4114         p = &((*p)->next);
4115       else
4116         *p = (*p)->next;
4117     }
4118
4119   return cands;
4120 }
4121
4122 static tree
4123 build_this (obj)
4124      tree obj;
4125 {
4126   /* Fix this to work on non-lvalues.  */
4127   if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
4128       || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
4129     return obj;
4130   else
4131     return build_unary_op (ADDR_EXPR, obj, 0);
4132 }
4133
4134 static void
4135 print_z_candidates (candidates)
4136      struct z_candidate *candidates;
4137 {
4138   char *str = "candidates are:";
4139   for (; candidates; candidates = candidates->next)
4140     {
4141       if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
4142         {
4143           if (candidates->fn == ansi_opname [COND_EXPR])
4144             cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
4145                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
4146                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
4147                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
4148           else if (TREE_VEC_LENGTH (candidates->convs) == 2)
4149             cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
4150                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
4151                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
4152           else
4153             cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
4154                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
4155         }
4156       else
4157         cp_error_at ("%s %+D%s", str, candidates->fn,
4158                      candidates->viable == -1 ? " <near match>" : "");
4159       str = "               "; 
4160     }
4161 }
4162
4163 /* Returns the best overload candidate to perform the requested
4164    conversion.  */
4165
4166 static struct z_candidate *
4167 build_user_type_conversion_1 (totype, expr, flags)
4168      tree totype, expr;
4169      int flags;
4170 {
4171   struct z_candidate *candidates, *cand;
4172   tree fromtype = TREE_TYPE (expr);
4173   tree ctors = NULL_TREE, convs = NULL_TREE, *p;
4174   tree args;
4175
4176   if (IS_AGGR_TYPE (totype))
4177     ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
4178   if (IS_AGGR_TYPE (fromtype)
4179       && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
4180     convs = lookup_conversions (fromtype);
4181
4182   candidates = 0;
4183   flags |= LOOKUP_NO_CONVERSION;
4184
4185   if (ctors)
4186     {
4187       tree t = build_int_2 (0, 0);
4188       TREE_TYPE (t) = build_pointer_type (totype);
4189       args = build_tree_list (NULL_TREE, expr);
4190       if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
4191         args = tree_cons (NULL_TREE, integer_one_node, args);
4192       args = tree_cons (NULL_TREE, t, args);
4193
4194       ctors = TREE_VALUE (ctors);
4195     }
4196   for (; ctors; ctors = DECL_CHAIN (ctors))
4197     {
4198       if (DECL_NONCONVERTING_P (ctors))
4199         continue;
4200
4201       candidates = add_function_candidate (candidates, ctors, args, flags);
4202       candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
4203       candidates->basetype_path = TYPE_BINFO (totype);
4204     }
4205
4206   if (convs)
4207     args = build_tree_list (NULL_TREE, build_this (expr));
4208
4209   for (; convs; convs = TREE_CHAIN (convs))
4210     {
4211       tree fn = TREE_VALUE (convs);
4212       tree ics = implicit_conversion
4213         (totype, TREE_TYPE (TREE_TYPE (fn)), 0, LOOKUP_NO_CONVERSION);
4214       if (ics)
4215         for (; fn; fn = DECL_CHAIN (fn))
4216           {
4217             candidates = add_function_candidate (candidates, fn, args, flags);
4218             candidates->second_conv = ics;
4219             candidates->basetype_path = TREE_PURPOSE (convs);
4220             if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
4221               candidates->viable = -1;
4222           }
4223     }
4224
4225   if (! any_viable (candidates))
4226     {
4227 #if 0
4228       if (flags & LOOKUP_COMPLAIN)
4229         {
4230           if (candidates && ! candidates->next)
4231             /* say why this one won't work or try to be loose */;
4232           else
4233             cp_error ("no viable candidates");
4234         }
4235 #endif
4236
4237       return 0;
4238     }
4239
4240   candidates = splice_viable (candidates);
4241   cand = tourney (candidates, totype);
4242
4243   if (cand == 0)
4244     {
4245       if (flags & LOOKUP_COMPLAIN)
4246         {
4247           cp_error ("conversion from `%T' to `%T' is ambiguous",
4248                     fromtype, totype);
4249           print_z_candidates (candidates);
4250         }
4251
4252       cand = candidates;        /* any one will do */
4253       cand->second_conv = build1 (AMBIG_CONV, totype, expr);
4254       ICS_USER_FLAG (cand->second_conv) = 1;
4255       ICS_BAD_FLAG (cand->second_conv) = 1;
4256
4257       return cand;
4258     }
4259
4260   for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
4261     p = &(TREE_OPERAND (*p, 0));
4262
4263   *p = build
4264     (USER_CONV,
4265      (DECL_CONSTRUCTOR_P (cand->fn)
4266       ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
4267      NULL_TREE, cand->fn, cand->convs, cand->basetype_path);
4268   ICS_USER_FLAG (cand->second_conv) = 1;
4269   if (cand->viable == -1)
4270     ICS_BAD_FLAG (cand->second_conv) = 1;
4271
4272   return cand;
4273 }
4274
4275 tree
4276 build_user_type_conversion (totype, expr, flags)
4277      tree totype, expr;
4278      int flags;
4279 {
4280   struct z_candidate *cand
4281     = build_user_type_conversion_1 (totype, expr, flags);
4282
4283   if (cand)
4284     {
4285       if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
4286         return error_mark_node;
4287       return convert_from_reference (convert_like (cand->second_conv, expr));
4288     }
4289   return NULL_TREE;
4290 }
4291
4292 tree
4293 build_new_function_call (fn, args, obj)
4294      tree fn, args, obj;
4295 {
4296   struct z_candidate *candidates = 0, *cand;
4297  
4298   if (obj == NULL_TREE && TREE_CODE (fn) == TREE_LIST)
4299     {
4300       tree t;
4301       tree templates = NULL_TREE;
4302
4303       for (t = args; t; t = TREE_CHAIN (t))
4304         {
4305           if (TREE_VALUE (t) == error_mark_node)
4306             return error_mark_node;
4307           else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
4308             {
4309               error ("invalid use of void expression");
4310               return error_mark_node;
4311             }
4312         }
4313         
4314       for (t = TREE_VALUE (fn); t; t = DECL_CHAIN (t))
4315         {
4316           if (TREE_CODE (t) == TEMPLATE_DECL)
4317             {
4318               templates = decl_tree_cons (NULL_TREE, t, templates);
4319               candidates = add_template_candidate
4320                 (candidates, t, args, LOOKUP_NORMAL);
4321             }
4322           else
4323             candidates = add_function_candidate
4324               (candidates, t, args, LOOKUP_NORMAL);
4325         }
4326
4327       if (! any_viable (candidates))
4328         {
4329           if (candidates && ! candidates->next)
4330             return build_function_call (candidates->fn, args);
4331           else
4332             cp_error ("no matching function for call to `%D (%A)'",
4333                       TREE_PURPOSE (fn), args);
4334           return error_mark_node;
4335         }
4336       candidates = splice_viable (candidates);
4337       cand = tourney (candidates, NULL_TREE);
4338
4339       if (cand == 0)
4340         {
4341           cp_error ("call of overloaded `%D (%A)' is ambiguous",
4342                     TREE_PURPOSE (fn), args);
4343           print_z_candidates (candidates);
4344           return error_mark_node;
4345         }
4346
4347       /* Pedantically, it is ill-formed to define a function that could
4348          also be a template instantiation, but we won't implement that
4349          until things settle down.  */
4350       if (templates && ! cand->template && ! DECL_INITIAL (cand->fn))
4351         add_maybe_template (cand->fn, templates);
4352
4353       return build_over_call (cand->fn, cand->convs, args, LOOKUP_NORMAL);
4354     }
4355
4356   return build_function_call (fn, args);
4357 }
4358
4359 tree
4360 build_object_call (obj, args)
4361      tree obj, args;
4362 {
4363   struct z_candidate *candidates = 0, *cand;
4364   tree fns, convs, mem_args;
4365   tree type = TREE_TYPE (obj);
4366
4367   fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 0);
4368
4369   if (fns)
4370     {
4371       tree fn = TREE_VALUE (fns);
4372       mem_args = tree_cons (NULL_TREE, build_this (obj), args);
4373
4374       for (; fn; fn = DECL_CHAIN (fn))
4375         {
4376           candidates = add_function_candidate
4377             (candidates, fn, mem_args, LOOKUP_NORMAL);
4378           candidates->basetype_path = TREE_PURPOSE (fns);
4379         }
4380     }
4381
4382   convs = lookup_conversions (type);
4383
4384   for (; convs; convs = TREE_CHAIN (convs))
4385     {
4386       tree fn = TREE_VALUE (convs);
4387       tree totype = TREE_TYPE (TREE_TYPE (fn));
4388
4389       if (TREE_CODE (totype) == POINTER_TYPE
4390           && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
4391         for (; fn; fn = DECL_CHAIN (fn))
4392           {
4393             candidates = add_conv_candidate (candidates, fn, obj, args);
4394             candidates->basetype_path = TREE_PURPOSE (convs);
4395           }
4396     }
4397
4398   if (! any_viable (candidates))
4399     {
4400       cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
4401       print_z_candidates (candidates);
4402       return error_mark_node;
4403     }
4404
4405   candidates = splice_viable (candidates);
4406   cand = tourney (candidates, NULL_TREE);
4407
4408   if (cand == 0)
4409     {
4410       cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
4411       print_z_candidates (candidates);
4412       return error_mark_node;
4413     }
4414
4415   if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
4416     return build_over_call (cand->fn, cand->convs, mem_args, LOOKUP_NORMAL);
4417
4418   obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
4419
4420   /* FIXME */
4421   return build_function_call (obj, args);
4422 }
4423
4424 static void
4425 op_error (code, code2, arg1, arg2, arg3, problem)
4426      enum tree_code code, code2;
4427      tree arg1, arg2, arg3;
4428      char *problem;
4429 {
4430   char * opname
4431     = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
4432
4433   switch (code)
4434     {
4435     case COND_EXPR:
4436       cp_error ("%s for `%T ? %T : %T'", problem,
4437                 error_type (arg1), error_type (arg2), error_type (arg3));
4438       break;
4439     case POSTINCREMENT_EXPR:
4440     case POSTDECREMENT_EXPR:
4441       cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
4442       break;
4443     case ARRAY_REF:
4444       cp_error ("%s for `%T[%T]'", problem,
4445                 error_type (arg1), error_type (arg2));
4446       break;
4447     default:
4448       if (arg2)
4449         cp_error ("%s for `%T %s %T'", problem,
4450                   error_type (arg1), opname, error_type (arg2));
4451       else
4452         cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
4453     }
4454 }
4455
4456 tree
4457 build_new_op (code, flags, arg1, arg2, arg3)
4458      enum tree_code code;
4459      int flags;
4460      tree arg1, arg2, arg3;
4461 {
4462   struct z_candidate *candidates = 0, *cand;
4463   tree fns, mem_arglist, arglist, fnname;
4464   enum tree_code code2 = NOP_EXPR;
4465   tree templates = NULL_TREE;
4466
4467   if (arg1 == error_mark_node
4468       || arg2 == error_mark_node
4469       || arg3 == error_mark_node)
4470     return error_mark_node;
4471
4472   if (code == MODIFY_EXPR)
4473     {
4474       code2 = TREE_CODE (arg3);
4475       arg3 = NULL_TREE;
4476       fnname = ansi_assopname[code2];
4477     }
4478   else
4479     fnname = ansi_opname[code];
4480
4481   switch (code)
4482     {
4483     case NEW_EXPR:
4484     case VEC_NEW_EXPR:
4485       {
4486         tree rval;
4487
4488         arglist = tree_cons (NULL_TREE, arg2, arg3);
4489         if (flags & LOOKUP_GLOBAL)
4490           return build_new_function_call
4491             (lookup_name_nonclass (fnname), arglist, NULL_TREE);
4492
4493         /* FIXME */
4494         rval = build_method_call
4495           (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
4496                                "new"),
4497            fnname, arglist, NULL_TREE, flags);
4498         if (rval == error_mark_node)
4499           /* User might declare fancy operator new, but invoke it
4500              like standard one.  */
4501           return rval;
4502
4503         TREE_TYPE (rval) = arg1;
4504         TREE_CALLS_NEW (rval) = 1;
4505         return rval;
4506       }
4507
4508     case VEC_DELETE_EXPR:
4509     case DELETE_EXPR:
4510       {
4511         tree rval;
4512
4513         if (flags & LOOKUP_GLOBAL)
4514           return build_new_function_call
4515             (lookup_name_nonclass (fnname),
4516              build_tree_list (NULL_TREE, arg1), NULL_TREE);
4517
4518         arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
4519
4520         arg1 = TREE_TYPE (arg1);
4521
4522         /* This handles the case where we're trying to delete
4523            X (*a)[10];
4524            a=new X[5][10];
4525            delete[] a; */
4526            
4527         if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
4528           {
4529             /* Strip off the pointer and the array.  */
4530             arg1 = TREE_TYPE (TREE_TYPE (arg1));
4531
4532             while (TREE_CODE (arg1) == ARRAY_TYPE)
4533                 arg1 = (TREE_TYPE (arg1));
4534
4535             arg1 = build_pointer_type (arg1);
4536           }
4537
4538         /* FIXME */
4539         rval = build_method_call
4540           (build_indirect_ref (build1 (NOP_EXPR, arg1,
4541                                        error_mark_node),
4542                                NULL_PTR),
4543            fnname, arglist, NULL_TREE, flags);
4544 #if 0
4545         /* This can happen when operator delete is protected.  */
4546         my_friendly_assert (rval != error_mark_node, 250);
4547         TREE_TYPE (rval) = void_type_node;
4548 #endif
4549         return rval;
4550       }
4551
4552     case CALL_EXPR:
4553       return build_object_call (arg1, arg2);
4554     }
4555
4556   /* The comma operator can have void args.  */
4557   if (TREE_CODE (arg1) == OFFSET_REF)
4558     arg1 = resolve_offset_ref (arg1);
4559   if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
4560     arg2 = resolve_offset_ref (arg2);
4561   if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
4562     arg3 = resolve_offset_ref (arg3);
4563
4564   if (code == COND_EXPR)
4565     {
4566       if (TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
4567           || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
4568           || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
4569               && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
4570         goto builtin;
4571     }
4572   else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
4573            && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
4574     goto builtin;
4575
4576   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
4577     arg2 = integer_zero_node;
4578
4579   fns = lookup_name_nonclass (fnname);
4580   /* + Koenig lookup */
4581
4582   if (arg2 && arg3)
4583     arglist = tree_cons (NULL_TREE, arg1, tree_cons
4584                       (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
4585   else if (arg2)
4586     arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
4587   else
4588     arglist = build_tree_list (NULL_TREE, arg1);
4589
4590   if (fns && TREE_CODE (fns) == TREE_LIST)
4591     fns = TREE_VALUE (fns);
4592   for (; fns; fns = DECL_CHAIN (fns))
4593     {
4594       if (TREE_CODE (fns) == TEMPLATE_DECL)
4595         {
4596           templates = decl_tree_cons (NULL_TREE, fns, templates);
4597           candidates = add_template_candidate
4598             (candidates, fns, arglist, flags);
4599         }
4600       else
4601         candidates = add_function_candidate (candidates, fns, arglist, flags);
4602     }
4603
4604   if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
4605     fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 0);
4606   else
4607     fns = NULL_TREE;
4608
4609   if (fns)
4610     {
4611       tree fn = TREE_VALUE (fns);
4612       mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
4613       for (; fn; fn = DECL_CHAIN (fn))
4614         {
4615           if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4616             candidates = add_function_candidate
4617               (candidates, fn, mem_arglist, flags);
4618           else
4619             candidates = add_function_candidate (candidates, fn, arglist, flags);
4620           
4621           candidates->basetype_path = TREE_PURPOSE (fns);
4622         }
4623     }
4624
4625   {
4626     tree args[3];
4627
4628     /* Rearrange the arguments for ?: so that add_builtin_candidate only has
4629        to know about two args; a builtin candidate will always have a first
4630        parameter of type bool.  We'll handle that in
4631        build_builtin_candidate.  */
4632     if (code == COND_EXPR)
4633       {
4634         args[0] = arg2;
4635         args[1] = arg3;
4636         args[2] = arg1;
4637       }
4638     else
4639       {
4640         args[0] = arg1;
4641         args[1] = arg2;
4642         args[2] = NULL_TREE;
4643       }
4644
4645     candidates = add_builtin_candidates
4646       (candidates, code, code2, fnname, args, flags);
4647   }
4648
4649   if (! any_viable (candidates))
4650     {
4651       switch (code)
4652         {
4653         case POSTINCREMENT_EXPR:
4654         case POSTDECREMENT_EXPR:
4655           /* Look for an `operator++ (int)'.  If they didn't have
4656              one, then we fall back to the old way of doing things.  */
4657           if (flags & LOOKUP_COMPLAIN)
4658             cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
4659                         fnname, opname_tab [code]);
4660           if (code == POSTINCREMENT_EXPR)
4661             code = PREINCREMENT_EXPR;
4662           else
4663             code = PREDECREMENT_EXPR;   
4664           return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
4665           
4666           /* The caller will deal with these.  */
4667         case ADDR_EXPR:
4668         case COMPOUND_EXPR:
4669         case COMPONENT_REF:
4670           return NULL_TREE;
4671         }
4672       if (flags & LOOKUP_COMPLAIN)
4673         {
4674           op_error (code, code2, arg1, arg2, arg3, "no match");
4675           print_z_candidates (candidates);
4676         }
4677       return error_mark_node;
4678     }
4679   candidates = splice_viable (candidates);
4680   cand = tourney (candidates, NULL_TREE);
4681
4682   if (cand == 0)
4683     {
4684       if (flags & LOOKUP_COMPLAIN)
4685         {
4686           op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
4687           print_z_candidates (candidates);
4688         }
4689       return error_mark_node;
4690     }
4691
4692   if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4693     {
4694       extern int warn_synth;
4695       if (warn_synth
4696           && fnname == ansi_opname[MODIFY_EXPR]
4697           && DECL_ARTIFICIAL (cand->fn)
4698           && candidates->next
4699           && ! candidates->next->next)
4700         {
4701           cp_warning ("using synthesized `%#D' for copy assignment",
4702                       cand->fn);
4703           cp_warning_at ("  where cfront would use `%#D'",
4704                          cand == candidates
4705                          ? candidates->next->fn
4706                          : candidates->fn);
4707         }
4708
4709       if (DECL_FUNCTION_MEMBER_P (cand->fn))
4710         enforce_access (cand->basetype_path, cand->fn);
4711
4712       /* Pedantically, it is ill-formed to define a function that could
4713          also be a template instantiation, but we won't implement that
4714          until things settle down.  */
4715       if (templates && ! cand->template && ! DECL_INITIAL (cand->fn)
4716           && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
4717         add_maybe_template (cand->fn, templates);
4718
4719       return build_over_call
4720         (cand->fn, cand->convs,
4721          TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4722          ? mem_arglist : arglist,
4723          LOOKUP_NORMAL);
4724     }
4725
4726   /* Check for comparison of different enum types.  */
4727   switch (code)
4728     {
4729     case GT_EXPR:
4730     case LT_EXPR:
4731     case GE_EXPR:
4732     case LE_EXPR:
4733     case EQ_EXPR:
4734     case NE_EXPR:
4735       if (flag_int_enum_equivalence == 0 
4736           && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 
4737           && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 
4738           && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
4739               != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
4740         {
4741           cp_warning ("comparison between `%#T' and `%#T'", 
4742                       TREE_TYPE (arg1), TREE_TYPE (arg2));
4743         }
4744     }
4745
4746   arg1 = convert_from_reference
4747     (convert_like (TREE_VEC_ELT (cand->convs, 0), arg1));
4748   if (arg2)
4749     arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
4750   if (arg3)
4751     arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
4752
4753 builtin:
4754   switch (code)
4755     {
4756     case MODIFY_EXPR:
4757       return build_modify_expr (arg1, code2, arg2);
4758
4759     case INDIRECT_REF:
4760       return build_indirect_ref (arg1, "unary *");
4761
4762     case PLUS_EXPR:
4763     case MINUS_EXPR:
4764     case MULT_EXPR:
4765     case TRUNC_DIV_EXPR:
4766     case GT_EXPR:
4767     case LT_EXPR:
4768     case GE_EXPR:
4769     case LE_EXPR:
4770     case EQ_EXPR:
4771     case NE_EXPR:
4772     case MAX_EXPR:
4773     case MIN_EXPR:
4774     case LSHIFT_EXPR:
4775     case RSHIFT_EXPR:
4776     case TRUNC_MOD_EXPR:
4777     case BIT_AND_EXPR:
4778     case BIT_IOR_EXPR:
4779     case BIT_XOR_EXPR:
4780     case TRUTH_ANDIF_EXPR:
4781     case TRUTH_ORIF_EXPR:
4782       return build_binary_op_nodefault (code, arg1, arg2, code);
4783
4784     case CONVERT_EXPR:
4785     case NEGATE_EXPR:
4786     case BIT_NOT_EXPR:
4787     case TRUTH_NOT_EXPR:
4788     case PREINCREMENT_EXPR:
4789     case POSTINCREMENT_EXPR:
4790     case PREDECREMENT_EXPR:
4791     case POSTDECREMENT_EXPR:
4792       return build_unary_op (code, arg1, candidates != 0);
4793
4794     case ARRAY_REF:
4795       return build_array_ref (arg1, arg2);
4796
4797     case COND_EXPR:
4798       return build_conditional_expr (arg1, arg2, arg3);
4799
4800     case MEMBER_REF:
4801       return build_m_component_ref
4802         (build_indirect_ref (arg1, NULL_PTR), arg2);
4803
4804       /* The caller will deal with these.  */
4805     case ADDR_EXPR:
4806     case COMPONENT_REF:
4807     case COMPOUND_EXPR:
4808       return NULL_TREE;
4809
4810     default:
4811       my_friendly_abort (367);
4812     }
4813 }
4814
4815 static void
4816 enforce_access (basetype_path, function)
4817      tree basetype_path, function;
4818 {
4819   tree access = compute_access (basetype_path, function);
4820
4821   if (access == access_private_node)
4822     {
4823       cp_error_at ("`%+#D' is %s", function, 
4824                    TREE_PRIVATE (function) ? "private"
4825                    : "from private base class");
4826       error ("within this context");
4827     }
4828   else if (access == access_protected_node)
4829     {
4830       cp_error_at ("`%+#D' %s", function,
4831                    TREE_PROTECTED (function) ? "is protected"
4832                    : "has protected accessibility");
4833       error ("within this context");
4834     }
4835 }
4836
4837 /* Perform the conversions in CONVS on the expression EXPR.  */
4838
4839 static tree
4840 convert_like (convs, expr)
4841      tree convs, expr;
4842 {
4843   if (ICS_BAD_FLAG (convs)
4844       && TREE_CODE (convs) != USER_CONV
4845       && TREE_CODE (convs) != AMBIG_CONV)
4846     {
4847       tree t = convs; 
4848       for (; t; t = TREE_OPERAND (t, 0))
4849         {
4850           if (TREE_CODE (t) == USER_CONV)
4851             {
4852               expr = convert_like (t, expr);
4853               break;
4854             }
4855           else if (TREE_CODE (t) == AMBIG_CONV)
4856             return convert_like (t, expr);
4857           else if (TREE_CODE (t) == IDENTITY_CONV)
4858             break;
4859         }
4860       return convert_for_initialization
4861         (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
4862          "conversion", NULL_TREE, 0);
4863     }
4864
4865   switch (TREE_CODE (convs))
4866     {
4867     case USER_CONV:
4868       {
4869         tree fn = TREE_OPERAND (convs, 1);
4870         tree args;
4871         enforce_access (TREE_OPERAND (convs, 3), fn);
4872
4873         if (DECL_CONSTRUCTOR_P (fn))
4874           {
4875             tree t = build_int_2 (0, 0);
4876             TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
4877
4878             args = build_tree_list (NULL_TREE, expr);
4879             if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
4880               args = tree_cons (NULL_TREE, integer_one_node, args);
4881             args = tree_cons (NULL_TREE, t, args);
4882           }
4883         else
4884           args = build_this (expr);
4885         expr = build_over_call
4886           (TREE_OPERAND (convs, 1), TREE_OPERAND (convs, 2),
4887            args, LOOKUP_NORMAL);
4888
4889         /* If this is a constructor or a function returning an aggr type,
4890            we need to build up a TARGET_EXPR.  */
4891         if (DECL_CONSTRUCTOR_P (fn))
4892           expr = build_cplus_new (TREE_TYPE (convs), expr);
4893
4894         return expr;
4895       }
4896     case IDENTITY_CONV:
4897       if (type_unknown_p (expr))
4898         expr = instantiate_type (TREE_TYPE (convs), expr, 1);
4899       if (TREE_READONLY_DECL_P (expr))
4900         expr = decl_constant_value (expr);
4901       return expr;
4902     case AMBIG_CONV:
4903       /* Call build_user_type_conversion again for the error.  */
4904       return build_user_type_conversion
4905         (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
4906     };
4907
4908   expr = convert_like (TREE_OPERAND (convs, 0), expr);
4909   if (expr == error_mark_node)
4910     return error_mark_node;
4911
4912   switch (TREE_CODE (convs))
4913     {
4914     case RVALUE_CONV:
4915       if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
4916         return expr;
4917       /* else fall through */
4918     case BASE_CONV:
4919       return build_user_type_conversion
4920         (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
4921     case REF_BIND:
4922       return convert_to_reference
4923         (TREE_TYPE (convs), expr,
4924          CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
4925          error_mark_node);
4926     case LVALUE_CONV:
4927       return decay_conversion (expr);
4928     }
4929   return cp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
4930                      LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4931 }
4932
4933 static tree
4934 convert_default_arg (type, arg)
4935      tree type, arg;
4936 {
4937   arg = break_out_target_exprs (arg);
4938
4939   if (TREE_CODE (arg) == CONSTRUCTOR)
4940     {
4941       arg = digest_init (type, arg, 0);
4942       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4943                                         "default argument", 0, 0);
4944     }
4945   else
4946     {
4947       /* This could get clobbered by the following call.  */
4948       if (TREE_HAS_CONSTRUCTOR (arg))
4949         arg = copy_node (arg);
4950
4951       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4952                                         "default argument", 0, 0);
4953 #ifdef PROMOTE_PROTOTYPES
4954       if ((TREE_CODE (type) == INTEGER_TYPE
4955            || TREE_CODE (type) == ENUMERAL_TYPE)
4956           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4957         arg = default_conversion (arg);
4958 #endif
4959     }
4960
4961   return arg;
4962 }
4963
4964 static tree
4965 build_over_call (fn, convs, args, flags)
4966      tree fn, convs, args;
4967      int flags;
4968 {
4969   tree converted_args = NULL_TREE;
4970   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4971   tree conv, arg, val;
4972   int i = 0;
4973   int is_method = 0;
4974
4975   if (args && TREE_CODE (args) != TREE_LIST)
4976     args = build_tree_list (NULL_TREE, args);
4977   arg = args;
4978
4979   /* The implicit parameters to a constructor are not considered by overload
4980      resolution, and must be of the proper type.  */
4981   if (DECL_CONSTRUCTOR_P (fn))
4982     {
4983       converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4984       arg = TREE_CHAIN (arg);
4985       parm = TREE_CHAIN (parm);
4986       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
4987         {
4988           converted_args = tree_cons
4989             (NULL_TREE, TREE_VALUE (arg), converted_args);
4990           arg = TREE_CHAIN (arg);
4991           parm = TREE_CHAIN (parm);
4992         }
4993     }      
4994   /* Bypass access control for 'this' parameter.  */
4995   else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4996     {
4997       tree parmtype = TREE_VALUE (parm);
4998       tree argtype = TREE_TYPE (TREE_VALUE (arg));
4999       if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
5000         {
5001           int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype))
5002                     < TYPE_VOLATILE (TREE_TYPE (argtype)));
5003           int dc = (TYPE_READONLY (TREE_TYPE (parmtype))
5004                     < TYPE_READONLY (TREE_TYPE (argtype)));
5005           char *p = (dv && dc ? "const and volatile" :
5006                      dc ? "const" : dv ? "volatile" : "");
5007
5008           cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s",
5009                       TREE_TYPE (argtype), fn, p);
5010         }
5011       converted_args = tree_cons
5012         (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
5013          converted_args);
5014       parm = TREE_CHAIN (parm);
5015       arg = TREE_CHAIN (arg);
5016       ++i;
5017       is_method = 1;
5018     }
5019
5020   for (; conv = TREE_VEC_ELT (convs, i), arg && parm;
5021        parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
5022     {
5023       tree type = TREE_VALUE (parm);
5024
5025       if (ICS_BAD_FLAG (conv))
5026         {
5027           tree t = conv;
5028           val = TREE_VALUE (arg);
5029
5030           for (; t; t = TREE_OPERAND (t, 0))
5031             {
5032               if (TREE_CODE (t) == USER_CONV
5033                   || TREE_CODE (t) == AMBIG_CONV)
5034                 {
5035                   val = convert_like (t, val);
5036                   break;
5037                 }
5038               else if (TREE_CODE (t) == IDENTITY_CONV)
5039                 break;
5040             }
5041           val = convert_for_initialization
5042             (NULL_TREE, type, val, LOOKUP_NORMAL,
5043              "argument passing", fn, i - is_method);
5044         }
5045       else
5046         val = convert_like (conv, TREE_VALUE (arg));
5047
5048 #ifdef PROMOTE_PROTOTYPES
5049       if ((TREE_CODE (type) == INTEGER_TYPE
5050            || TREE_CODE (type) == ENUMERAL_TYPE)
5051           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
5052         val = default_conversion (val);
5053 #endif
5054       converted_args = tree_cons (NULL_TREE, val, converted_args);
5055     }
5056
5057   /* Default arguments */
5058   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
5059     {
5060       tree arg = TREE_PURPOSE (parm);
5061
5062       if (PARM_DEFAULT_FROM_TEMPLATE (parm))
5063         /* This came from a template.  Instantiate the default arg here,
5064            not in tsubst.  */
5065         arg = tsubst_expr (arg, &TREE_VEC_ELT (DECL_TI_ARGS (fn), 0),
5066                            TREE_VEC_LENGTH (DECL_TI_ARGS (fn)), NULL_TREE);
5067       converted_args = tree_cons
5068         (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
5069          converted_args);
5070     }
5071
5072   /* Ellipsis */
5073   for (; arg; arg = TREE_CHAIN (arg))
5074     {
5075       val = TREE_VALUE (arg);
5076
5077       if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
5078           && (TYPE_PRECISION (TREE_TYPE (val))
5079               < TYPE_PRECISION (double_type_node)))
5080         /* Convert `float' to `double'.  */
5081         val = convert (double_type_node, val);
5082       else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
5083                && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
5084         cp_warning ("cannot pass objects of type `%T' through `...'",
5085                     TREE_TYPE (val));
5086       else
5087         /* Convert `short' and `char' to full-size `int'.  */
5088         val = default_conversion (val);
5089
5090       converted_args = tree_cons (NULL_TREE, val, converted_args);
5091     }
5092
5093   converted_args = nreverse (converted_args);
5094
5095   /* [class.copy]: the copy constructor is implicitly defined even if the
5096      implementation  elided  its  use.  */
5097   mark_used (fn);
5098
5099   /* Avoid actually calling copy constructors and copy assignment operators,
5100      if possible.  */
5101   if (DECL_CONSTRUCTOR_P (fn)
5102       && TREE_VEC_LENGTH (convs) == 1
5103       && copy_args_p (fn))
5104     {
5105       tree targ = NULL_TREE;
5106       arg = TREE_VALUE (TREE_CHAIN (converted_args));
5107
5108       /* Pull out the real argument, disregarding const-correctness.  */
5109       if (TREE_CODE (arg) == ADDR_EXPR)
5110         {
5111           targ = TREE_OPERAND (arg, 0);
5112           if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
5113                            TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1))
5114             targ = NULL_TREE;
5115         }
5116
5117       if (targ)
5118         arg = targ;
5119       else
5120         arg = build_indirect_ref (arg, 0);
5121
5122       /* If we're creating a temp and we already have one, don't create a
5123          new one.  If we're not creating a temp but we get one, use
5124          INIT_EXPR to collapse the temp into our target.  Otherwise, if the
5125          ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
5126          temp or an INIT_EXPR otherwise.  */
5127       if (integer_zerop (TREE_VALUE (args)))
5128         {
5129           if (! real_lvalue_p (arg))
5130             return arg;
5131           else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
5132             {
5133               val = build (VAR_DECL, DECL_CONTEXT (fn));
5134               layout_decl (val, 0);
5135               val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
5136               TREE_SIDE_EFFECTS (val) = 1;
5137               return val;
5138             }
5139         }
5140       else if (! real_lvalue_p (arg)
5141                || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
5142         {
5143           tree to = stabilize_reference
5144             (build_indirect_ref (TREE_VALUE (args), 0));
5145           val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
5146           TREE_SIDE_EFFECTS (val) = 1;
5147           return build_unary_op (ADDR_EXPR, val, 0);
5148         }
5149     }
5150   else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
5151            && copy_args_p (fn)
5152            && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
5153     {
5154       tree to = stabilize_reference
5155         (build_indirect_ref (TREE_VALUE (converted_args), 0));
5156       arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
5157       val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
5158       TREE_SIDE_EFFECTS (val) = 1;
5159       return val;
5160     }
5161
5162   if (DECL_CONTEXT (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
5163     return build_signature_method_call (fn, converted_args);
5164   else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
5165     {
5166       tree t, *p = &TREE_VALUE (converted_args);
5167       tree binfo = get_binfo
5168         (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
5169       *p = convert_pointer_to_real (binfo, *p);
5170       if (TREE_SIDE_EFFECTS (*p))
5171         *p = save_expr (*p);
5172       t = build_pointer_type (TREE_TYPE (fn));
5173       fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
5174       TREE_TYPE (fn) = t;
5175     }
5176   else if (DECL_INLINE (fn))
5177     fn = inline_conversion (fn);
5178   else
5179     fn = build_addr_func (fn);
5180
5181   fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
5182   if (TREE_TYPE (fn) == void_type_node)
5183     return fn;
5184   if (IS_AGGR_TYPE (TREE_TYPE (fn)))
5185     fn = build_cplus_new (TREE_TYPE (fn), fn);
5186   return convert_from_reference (require_complete_type (fn));
5187 }
5188
5189 tree
5190 build_new_method_call (instance, name, args, basetype_path, flags)
5191      tree instance, name, args, basetype_path;
5192      int flags;
5193 {
5194   struct z_candidate *candidates = 0, *cand;
5195   tree basetype, mem_args, fns, instance_ptr;
5196   tree pretty_name;
5197   tree user_args = args;
5198
5199   /* If there is an extra argument for controlling virtual bases,
5200      remove it for error reporting.  */
5201   if (flags & LOOKUP_HAS_IN_CHARGE)
5202     user_args = TREE_CHAIN (args);
5203
5204   for (fns = args; fns; fns = TREE_CHAIN (fns))
5205     if (TREE_VALUE (fns) == error_mark_node)
5206       return error_mark_node;
5207
5208   if (instance == NULL_TREE)
5209     basetype = BINFO_TYPE (basetype_path);
5210   else
5211     {
5212       if (TREE_CODE (instance) == OFFSET_REF)
5213         instance = resolve_offset_ref (instance);
5214       if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5215         instance = convert_from_reference (instance);
5216       basetype = TREE_TYPE (instance);
5217
5218       /* XXX this should be handled before we get here.  */
5219       if (! IS_AGGR_TYPE (basetype)
5220           && ! (TYPE_LANG_SPECIFIC (basetype)
5221                 && (IS_SIGNATURE_POINTER (basetype)
5222                     || IS_SIGNATURE_REFERENCE (basetype))))
5223         {
5224           if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5225             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5226                       name, instance, basetype);
5227
5228           return error_mark_node;
5229         }
5230
5231       /* If `instance' is a signature pointer/reference and `name' is
5232          not a constructor, we are calling a signature member function.
5233          In that case set the `basetype' to the signature type.  */
5234       if ((IS_SIGNATURE_POINTER (basetype)
5235            || IS_SIGNATURE_REFERENCE (basetype))
5236           && TYPE_IDENTIFIER (basetype) != name)
5237         basetype = SIGNATURE_TYPE (basetype);
5238     }
5239
5240   if (basetype_path == NULL_TREE)
5241     basetype_path = TYPE_BINFO (basetype);
5242
5243   if (instance)
5244     {
5245       instance_ptr = build_this (instance);
5246
5247       /* XXX this should be handled before we get here.  */
5248       fns = build_field_call (basetype_path, instance_ptr, name, args);
5249       if (fns)
5250         return fns;
5251     }
5252   else
5253     {
5254       instance_ptr = build_int_2 (0, 0);
5255       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
5256     }
5257
5258   pretty_name =
5259     (name == ctor_identifier ? constructor_name_full (basetype) : name);
5260
5261   fns = lookup_fnfields (basetype_path, name, 1);
5262
5263   if (fns == error_mark_node)
5264     return error_mark_node;
5265   if (fns)
5266     {
5267       tree t = TREE_VALUE (fns);
5268       if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
5269           && ! (flags & LOOKUP_HAS_IN_CHARGE))
5270         {
5271           flags |= LOOKUP_HAS_IN_CHARGE;
5272           args = tree_cons (NULL_TREE, integer_one_node, args);
5273         }
5274       mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5275       for (; t; t = DECL_CHAIN (t))
5276         {
5277           /* We can end up here for copy-init of same or base class.  */
5278           if (name == ctor_identifier
5279               && (flags & LOOKUP_ONLYCONVERTING)
5280               && DECL_NONCONVERTING_P (t))
5281             continue;
5282           if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
5283             candidates = add_function_candidate
5284               (candidates, t, mem_args, flags);
5285           else
5286             candidates = add_function_candidate (candidates, t, args, flags);
5287           candidates->basetype_path = TREE_PURPOSE (fns);
5288         }
5289     }
5290
5291   if (! any_viable (candidates))
5292     {
5293       /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
5294       if (flags & LOOKUP_SPECULATIVELY)
5295         return NULL_TREE;
5296       cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype,
5297                 pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr)));
5298       print_z_candidates (candidates);
5299       return error_mark_node;
5300     }
5301   candidates = splice_viable (candidates);
5302   cand = tourney (candidates, NULL_TREE);
5303
5304   if (cand == 0)
5305     {
5306       cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
5307                 user_args);
5308       print_z_candidates (candidates);
5309       return error_mark_node;
5310     }
5311
5312   enforce_access (cand->basetype_path, cand->fn);
5313   if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
5314       && instance == current_class_ref
5315       && DECL_CONSTRUCTOR_P (current_function_decl)
5316       && ! (flags & LOOKUP_NONVIRTUAL)
5317       && value_member (cand->fn, get_abstract_virtuals (basetype)))
5318     cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
5319   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5320       && TREE_CODE (instance_ptr) == NOP_EXPR
5321       && TREE_OPERAND (instance_ptr, 0) == error_mark_node)
5322     cp_error ("cannot call member function `%D' without object", cand->fn);
5323
5324   if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5325       && ((instance == current_class_ref && (dtor_label || ctor_label))
5326           || resolves_to_fixed_type_p (instance, 0)))
5327     flags |= LOOKUP_NONVIRTUAL;
5328
5329   return build_over_call
5330     (cand->fn, cand->convs,
5331      TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
5332      flags);
5333 }
5334
5335 /* Compare two implicit conversion sequences that differ only in their
5336    qualification conversion.  Subroutine of compare_ics.  */
5337
5338 static int
5339 compare_qual (ics1, ics2)
5340      tree ics1, ics2;
5341 {
5342   tree to1 = TREE_TYPE (ics1);
5343   tree to2 = TREE_TYPE (ics2);
5344
5345   to1 = TREE_TYPE (to1);
5346   to2 = TREE_TYPE (to2);
5347
5348   if (TREE_CODE (to1) == OFFSET_TYPE)
5349     {
5350       to1 = TREE_TYPE (to1);
5351       to2 = TREE_TYPE (to2);
5352     }
5353
5354   if (TYPE_READONLY (to1) >= TYPE_READONLY (to2)
5355       && TYPE_VOLATILE (to1) > TYPE_VOLATILE (to2))
5356     return -1;
5357   else if (TYPE_READONLY (to1) > TYPE_READONLY (to2)
5358            && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2))
5359     return -1;
5360   else if (TYPE_READONLY (to1) <= TYPE_READONLY (to2)
5361            && TYPE_VOLATILE (to1) < TYPE_VOLATILE (to2))
5362     return 1;
5363   else if (TYPE_READONLY (to1) < TYPE_READONLY (to2)
5364            && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2))
5365     return 1;
5366   return 0;
5367 }
5368
5369 /* Compare two implicit conversion sequences according to the rules set out in
5370    [over.ics.rank].  Return values:
5371
5372       1: ics1 is better than ics2
5373      -1: ics2 is better than ics1
5374       0: ics1 and ics2 are indistinguishable */
5375
5376 static int
5377 compare_ics (ics1, ics2)
5378      tree ics1, ics2;
5379 {
5380   tree main1, main2;
5381
5382   if (TREE_CODE (ics1) == QUAL_CONV)
5383     main1 = TREE_OPERAND (ics1, 0);
5384   else
5385     main1 = ics1;
5386
5387   if (TREE_CODE (ics2) == QUAL_CONV)
5388     main2 = TREE_OPERAND (ics2, 0);
5389   else
5390     main2 = ics2;
5391
5392   /* Conversions for `this' are PTR_CONVs, but we compare them as though
5393      they were REF_BINDs.  */
5394   if (ICS_THIS_FLAG (ics1))
5395     {
5396       ics1 = build_conv (REF_BIND, TREE_TYPE (ics1), main1);
5397       TREE_OPERAND (ics1, 0) = TREE_OPERAND (main1, 0);
5398       main1 = ics1;
5399     }
5400   if (ICS_THIS_FLAG (ics2))
5401     {
5402       ics2 = build_conv (REF_BIND, TREE_TYPE (ics2), main2);
5403       TREE_OPERAND (ics2, 0) = TREE_OPERAND (main2, 0);
5404       main2 = ics2;
5405     }
5406
5407   if (ICS_RANK (ics1) > ICS_RANK (ics2))
5408     return -1;
5409   else if (ICS_RANK (ics1) < ICS_RANK (ics2))
5410     return 1;
5411
5412   if (ICS_RANK (ics1) == BAD_RANK)
5413     {
5414       if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5415           || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5416         return -1;
5417       else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5418                || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5419         return 1;
5420
5421       /* else fall through */
5422     }
5423
5424   /* User-defined  conversion sequence U1 is a better conversion sequence
5425      than another user-defined conversion sequence U2 if they contain the
5426      same user-defined conversion operator or constructor and if the sec-
5427      ond standard conversion sequence of U1 is  better  than  the  second
5428      standard conversion sequence of U2.  */
5429
5430   if (ICS_USER_FLAG (ics1))
5431     {
5432       tree t1, t2;
5433
5434       for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5435         if (TREE_CODE (t1) == AMBIG_CONV)
5436           return 0;
5437       for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5438         if (TREE_CODE (t2) == AMBIG_CONV)
5439           return 0;
5440
5441       if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5442         return 0;
5443       else if (ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5444         return -1;
5445       else if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5446         return 1;
5447
5448       /* else fall through */
5449     }
5450
5451 #if 0 /* Handled by ranking */
5452   /* A conversion that is not a conversion of a pointer,  or  pointer  to
5453      member,  to  bool  is  better than another conversion that is such a
5454      conversion.  */
5455 #endif
5456
5457   if (TREE_CODE (main1) != TREE_CODE (main2))
5458     return 0;
5459
5460   if (TREE_CODE (main1) == PTR_CONV || TREE_CODE (main1) == PMEM_CONV
5461       || TREE_CODE (main1) == REF_BIND || TREE_CODE (main1) == BASE_CONV)
5462     {
5463       tree to1 = TREE_TYPE (main1);
5464       tree from1 = TREE_TYPE (TREE_OPERAND (main1, 0));
5465       tree to2 = TREE_TYPE (main2);
5466       tree from2 = TREE_TYPE (TREE_OPERAND (main2, 0));
5467       int distf, distt;
5468
5469       /* Standard conversion sequence S1 is a better conversion sequence than
5470          standard conversion sequence S2 if...
5471
5472          S1 and S2 differ only in their qualification conversion  and  they
5473          yield types identical except for cv-qualifiers and S2 adds all the
5474          qualifiers that S1 adds (and in the same places) and S2  adds  yet
5475          more  cv-qualifiers  than  S1,  or the similar case with reference
5476          binding15).  */
5477       if (TREE_CODE (main1) == REF_BIND)
5478         {
5479           if (TYPE_MAIN_VARIANT (TREE_TYPE (to1))
5480               == TYPE_MAIN_VARIANT (TREE_TYPE (to2)))
5481             return compare_qual (ics1, ics2);
5482         }
5483       else if (TREE_CODE (main1) != BASE_CONV && from1 == from2 && to1 == to2)
5484         return compare_qual (ics1, ics2);
5485         
5486       if (TYPE_PTRMEMFUNC_P (to1))
5487         {
5488           to1 = TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (to1));
5489           from1 = TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (from1));
5490         }
5491       else if (TREE_CODE (main1) != BASE_CONV)
5492         {
5493           to1 = TREE_TYPE (to1);
5494           if (TREE_CODE (main1) != REF_BIND)
5495             from1 = TREE_TYPE (from1);
5496
5497           if (TREE_CODE (to1) == OFFSET_TYPE)
5498             {
5499               to1 = TYPE_OFFSET_BASETYPE (to1);
5500               from1 = TYPE_OFFSET_BASETYPE (from1);
5501             }
5502         }
5503
5504       if (TYPE_PTRMEMFUNC_P (to2))
5505         {
5506           to2 = TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (to2));
5507           from2 = TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (from2));
5508         }
5509       else if (TREE_CODE (main1) != BASE_CONV)
5510         {
5511           to2 = TREE_TYPE (to2);
5512           if (TREE_CODE (main1) != REF_BIND)
5513             from2 = TREE_TYPE (from2);
5514
5515           if (TREE_CODE (to2) == OFFSET_TYPE)
5516             {
5517               to2 = TYPE_OFFSET_BASETYPE (to2);
5518               from2 = TYPE_OFFSET_BASETYPE (from2);
5519             }
5520         }
5521
5522       if (! (IS_AGGR_TYPE (from1) && IS_AGGR_TYPE (from2)))
5523         return 0;
5524
5525       /* The sense of pmem conversions is reversed from that of the other
5526          conversions.  */
5527       if (TREE_CODE (main1) == PMEM_CONV)
5528         {
5529           tree t = from1; from1 = from2; from2 = t;
5530           t = to1; to1 = to2; to2 = t;
5531         }
5532
5533       distf = get_base_distance (from1, from2, 0, 0);
5534       if (distf == -1)
5535         {
5536           distf = -get_base_distance (from2, from1, 0, 0);
5537           if (distf == 1)
5538             return 0;
5539         }
5540
5541       /* If class B is derived directly or indirectly from class A,
5542          conver- sion of B* to A* is better than conversion of B* to
5543          void*, and conversion of A* to void* is better than
5544          conversion of B* to void*.  */
5545
5546       if (TREE_CODE (to1) == VOID_TYPE && TREE_CODE (to2) == VOID_TYPE)
5547         {
5548           if (distf > 0)
5549             return 1;
5550           else if (distf < 0)
5551             return -1;
5552         }
5553       else if (TREE_CODE (to2) == VOID_TYPE && IS_AGGR_TYPE (to1)
5554                && get_base_distance (to1, from1, 0, 0) != -1)
5555         return 1;
5556       else if (TREE_CODE (to1) == VOID_TYPE && IS_AGGR_TYPE (to2)
5557                && get_base_distance (to2, from2, 0, 0) != -1)
5558         return -1;
5559
5560       if (! (IS_AGGR_TYPE (to1) && IS_AGGR_TYPE (to2)))
5561         return 0;
5562
5563       /* If  class B is derived directly or indirectly from class A and class
5564          C is derived directly or indirectly from B */
5565
5566       distt = get_base_distance (to1, to2, 0, 0);
5567       if (distt == -1)
5568         {
5569           distt = -get_base_distance (to2, to1, 0, 0);
5570           if (distt == 1)
5571             return 0;
5572         }
5573
5574       /* --conversion of C* to B* is better than conversion of C* to A*, */
5575       if (distf == 0)
5576         {
5577           if (distt > 0)
5578             return -1;
5579           else if (distt < 0)
5580             return 1;
5581         }
5582       /* --conversion of B* to A* is better than conversion of C* to A*, */
5583       else if (distt == 0)
5584         {
5585           if (distf > 0)
5586             return 1;
5587           else if (distf < 0)
5588             return -1;
5589         }
5590     }
5591   else if (TREE_CODE (TREE_TYPE (main1)) == POINTER_TYPE
5592            || TYPE_PTRMEMFUNC_P (TREE_TYPE (main1)))
5593     {
5594       if (TREE_TYPE (main1) == TREE_TYPE (main2))
5595         return compare_qual (ics1, ics2);
5596
5597 #if 0 /* This is now handled by making identity better than anything else.  */
5598       /* existing practice, not WP-endorsed: const char * -> const char *
5599          is better than char * -> const char *.  (jason 6/29/96) */
5600       if (TREE_TYPE (ics1) == TREE_TYPE (ics2))
5601         return -compare_qual (main1, main2);
5602 #endif
5603     }
5604
5605   return 0;
5606 }
5607
5608 /* Compare two candidates for overloading as described in
5609    [over.match.best].  Return values:
5610
5611       1: cand1 is better than cand2
5612      -1: cand2 is better than cand1
5613       0: cand1 and cand2 are indistinguishable */
5614
5615 static int
5616 joust (cand1, cand2)
5617      struct z_candidate *cand1, *cand2;
5618 {
5619   int winner = 0;
5620   int i, off1 = 0, off2 = 0, len;
5621
5622   /* Candidates that involve bad conversions are always worse than those
5623      that don't.  */
5624   if (cand1->viable > cand2->viable)
5625     return 1;
5626   if (cand1->viable < cand2->viable)
5627     return -1;
5628
5629   /* a viable function F1
5630      is defined to be a better function than another viable function F2  if
5631      for  all arguments i, ICSi(F1) is not a worse conversion sequence than
5632      ICSi(F2), and then */
5633
5634   /* for some argument j, ICSj(F1) is a better conversion  sequence  than
5635      ICSj(F2) */
5636
5637   /* For comparing static and non-static member functions, we ignore the
5638      implicit object parameter of the non-static function.  The WP says to
5639      pretend that the static function has an object parm, but that won't
5640      work with operator overloading.  */
5641   len = TREE_VEC_LENGTH (cand1->convs);
5642   if (len != TREE_VEC_LENGTH (cand2->convs))
5643     {
5644       if (DECL_STATIC_FUNCTION_P (cand1->fn)
5645           && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5646         off2 = 1;
5647       else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5648                && DECL_STATIC_FUNCTION_P (cand2->fn))
5649         {
5650           off1 = 1;
5651           --len;
5652         }
5653       else
5654         my_friendly_abort (42);
5655     }
5656
5657   for (i = 0; i < len; ++i)
5658     {
5659       int comp = compare_ics (TREE_VEC_ELT (cand1->convs, i+off1),
5660                               TREE_VEC_ELT (cand2->convs, i+off2));
5661
5662       if (comp != 0)
5663         {
5664           if (winner && comp != winner)
5665             {
5666               winner = 0;
5667               goto tweak;
5668             }
5669           winner = comp;
5670         }
5671     }
5672
5673   if (winner)
5674     return winner;
5675
5676   /* or, if not that,
5677      F1 is a non-template function and F2 is a template function */
5678
5679   if (! cand1->template && cand2->template)
5680     return 1;
5681   else if (cand1->template && ! cand2->template)
5682     return -1;
5683   else if (cand1->template && cand2->template)
5684     winner = more_specialized
5685       (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template));
5686
5687   /* or, if not that,
5688      the  context  is  an  initialization by user-defined conversion (see
5689      _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
5690      sequence  from  the return type of F1 to the destination type (i.e.,
5691      the type of the entity being initialized)  is  a  better  conversion
5692      sequence  than the standard conversion sequence from the return type
5693      of F2 to the destination type.  */
5694
5695   if (! winner && cand1->second_conv)
5696     winner = compare_ics (cand1->second_conv, cand2->second_conv);
5697
5698   /* If the built-in candidates are the same, arbitrarily pick one.  */
5699   if (! winner && cand1->fn == cand2->fn
5700       && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5701     {
5702       for (i = 0; i < len; ++i)
5703         if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5704                          TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
5705           break;
5706       if (i == TREE_VEC_LENGTH (cand1->convs))
5707         return 1;
5708 #if 0
5709       /* Kludge around broken overloading rules whereby
5710          bool ? void *const & : void *const & is ambiguous.  */
5711       /* Huh?  Explain the problem better.  */
5712       if (cand1->fn == ansi_opname[COND_EXPR])
5713         {
5714           tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5715           tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5716           tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5717           tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5718
5719           if (comptypes (t1, t2, 1))
5720             {
5721               if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5722                 return 1;
5723               if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5724                 return -1;
5725             }
5726         }
5727 #endif
5728     }
5729
5730 tweak:
5731
5732   /* Extension: If the worst conversion for one candidate is worse than the
5733      worst conversion for the other, take the first.  */
5734   if (! winner && ! pedantic)
5735     {
5736       int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5737
5738       for (i = 0; i < len; ++i)
5739         {
5740           if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5741             rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5742           if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5743             rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5744         }
5745
5746       if (rank1 < rank2)
5747         return 1;
5748       if (rank1 > rank2)
5749         return -1;
5750     }
5751
5752   return winner;
5753 }
5754
5755 /* Given a list of candidates for overloading, find the best one, if any.
5756    This algorithm has a worst case of O(2n) (winner is last), and a best
5757    case of O(n/2) (totally ambiguous); much better than a sorting
5758    algorithm.  */
5759
5760 static struct z_candidate *
5761 tourney (candidates)
5762      struct z_candidate *candidates;
5763 {
5764   struct z_candidate *champ = candidates, *challenger;
5765   int fate;
5766
5767   /* Walk through the list once, comparing each current champ to the next
5768      candidate, knocking out a candidate or two with each comparison.  */
5769
5770   for (challenger = champ->next; challenger; )
5771     {
5772       fate = joust (champ, challenger);
5773       if (fate == 1)
5774         challenger = challenger->next;
5775       else
5776         {
5777           if (fate == 0)
5778             {
5779               champ = challenger->next;
5780               if (champ == 0)
5781                 return 0;
5782             }
5783           else
5784             champ = challenger;
5785
5786           challenger = champ->next;
5787         }
5788     }
5789
5790   /* Make sure the champ is better than all the candidates it hasn't yet
5791      been compared to.  This may do one more comparison than necessary.  Oh
5792      well.  */
5793
5794   for (challenger = candidates; challenger != champ;
5795        challenger = challenger->next)
5796     {
5797       fate = joust (champ, challenger);
5798       if (fate != 1)
5799         return 0;
5800     }
5801
5802   return champ;
5803 }
5804
5805 int
5806 can_convert (to, from)
5807      tree to, from;
5808 {
5809   if (flag_ansi_overloading)
5810     {
5811       tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
5812       return (t && ! ICS_BAD_FLAG (t));
5813     }
5814   else
5815     {
5816       struct harshness_code h;
5817       h = convert_harshness (to, from, NULL_TREE);
5818       return (h.code < USER_CODE) && (h.distance >= 0);
5819     }
5820 }
5821
5822 int
5823 can_convert_arg (to, from, arg)
5824      tree to, from, arg;
5825 {
5826   if (flag_ansi_overloading)
5827     {
5828       tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5829       return (t && ! ICS_BAD_FLAG (t));
5830     }
5831   else
5832     {
5833       struct harshness_code h;
5834       h = convert_harshness (to, from, arg);
5835       return (h.code < USER_CODE) && (h.distance >= 0);
5836     }
5837 }