OSDN Git Service

37th Cygnus<->FSF merge
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
1 /* Functions related to invoking methods and overloaded functions.
2    Copyright (C) 1987, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22
23 /* High-level class interface. */
24
25 #include "config.h"
26 #include "tree.h"
27 #include <stdio.h>
28 #include "cp-tree.h"
29 #include "class.h"
30 #include "flags.h"
31
32 #include "obstack.h"
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
35
36 extern void sorry ();
37
38 extern int inhibit_warnings;
39 extern int flag_assume_nonnull_objects;
40 extern tree ctor_label, dtor_label;
41
42 /* From typeck.c:  */
43 extern tree unary_complex_lvalue ();
44
45 /* Compute the ease with which a conversion can be performed
46    between an expected and the given type.  */
47 static int convert_harshness_old ();
48 static struct harshness_code convert_harshness_ansi ();
49
50 /* OLD METHOD */
51 /* Note the old method also uses USER_HARSHNESS, BASE_DERIVED_HARSHNESS,
52    CONST_HARSHNESS.  */
53 #define EVIL 1
54 #define TRIVIAL 0
55 #define EVIL_HARSHNESS(ARG) ((ARG) & 1)
56 #define ELLIPSIS_HARSHNESS(ARG) ((ARG) & 2)
57 #define CONTRAVARIANT_HARSHNESS(ARG) ((ARG) & 8)
58 #define INT_TO_BD_HARSHNESS(ARG) (((ARG) << 5) | 16)
59 #define INT_FROM_BD_HARSHNESS(ARG) ((ARG) >> 5)
60 #define INT_TO_EASY_HARSHNESS(ARG) ((ARG) << 5)
61 #define INT_FROM_EASY_HARSHNESS(ARG) ((ARG) >> 5)
62 #define ONLY_EASY_HARSHNESS(ARG) (((ARG) & 31) == 0)
63
64
65 /* NEW METHOD */
66 #define EVIL_RETURN(ARG)        ((ARG).code = EVIL_CODE, (ARG))
67 #define QUAL_RETURN(ARG)        ((ARG).code = QUAL_CODE, (ARG))
68 #define TRIVIAL_RETURN(ARG)     ((ARG).code = TRIVIAL_CODE, (ARG))
69 #define ZERO_RETURN(ARG)        ((ARG).code = 0, (ARG))
70
71 #define USER_HARSHNESS(ARG) ((ARG) & 4)
72 #define BASE_DERIVED_HARSHNESS(ARG) ((ARG) & 16)
73 #define CONST_HARSHNESS(ARG) ((ARG) & 2048)
74
75 /* Ordering function for overload resolution.  Compare two candidates
76    by gross quality.  */
77 int
78 rank_for_overload_ansi (x, y)
79      struct candidate *x, *y;
80 {
81   if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
82     return y->h.code - x->h.code;
83   if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
84     return -1;
85
86   /* This is set by compute_conversion_costs, for calling a non-const
87      member function from a const member function.  */
88   if ((y->v.ansi_harshness[0].code & CONST_CODE) ^ (x->v.ansi_harshness[0].code & CONST_CODE))
89     return y->v.ansi_harshness[0].code - x->v.ansi_harshness[0].code;
90
91   if (y->h.code & STD_CODE)
92     {
93       if (x->h.code & STD_CODE)
94         return y->h.distance - x->h.distance;
95       return 1;
96     }
97   if (x->h.code & STD_CODE)
98     return -1;
99
100   return y->h.code - x->h.code;
101 }
102
103 int
104 rank_for_overload_old (x, y)
105      struct candidate *x, *y;
106 {
107   if (y->evil - x->evil)
108     return y->evil - x->evil;
109   if (CONST_HARSHNESS (y->v.old_harshness[0]) ^ CONST_HARSHNESS (x->v.old_harshness[0]))
110     return y->v.old_harshness[0] - x->v.old_harshness[0];
111   if (y->ellipsis - x->ellipsis)
112     return y->ellipsis - x->ellipsis;
113   if (y->user - x->user)
114     return y->user - x->user;
115   if (y->b_or_d - x->b_or_d)
116     return y->b_or_d - x->b_or_d;
117   return y->easy - x->easy;
118 }
119
120 int
121 rank_for_overload (x, y)
122      struct candidate *x, *y;
123 {
124   if (flag_ansi_overloading)
125     return rank_for_overload_ansi (x, y);
126   else
127     return rank_for_overload_old (x, y);
128 }
129
130 /* Compare two candidates, argument by argument.  */
131 int
132 rank_for_ideal (x, y)
133      struct candidate *x, *y;
134 {
135   int i;
136
137   if (x->h_len != y->h_len)
138     abort ();
139
140   for (i = 0; i < x->h_len; i++)
141     {
142       if (y->v.ansi_harshness[i].code - x->v.ansi_harshness[i].code)
143         return y->v.ansi_harshness[i].code - x->v.ansi_harshness[i].code;
144       if ((y->v.ansi_harshness[i].code & STD_CODE)
145           && (y->v.ansi_harshness[i].distance - x->v.ansi_harshness[i].distance))
146         return y->v.ansi_harshness[i].distance - x->v.ansi_harshness[i].distance;
147
148       /* They're both the same code.  Now see if we're dealing with an
149          integral promotion that needs a finer grain of accuracy.  */
150       if (y->v.ansi_harshness[0].code & PROMO_CODE
151           && (y->v.ansi_harshness[i].int_penalty ^ x->v.ansi_harshness[i].int_penalty))
152         return y->v.ansi_harshness[i].int_penalty - x->v.ansi_harshness[i].int_penalty;
153     }
154   return 0;
155 }
156
157 /* TYPE is the type we wish to convert to.  PARM is the parameter
158    we have to work with.  We use a somewhat arbitrary cost function
159    to measure this conversion.  */
160 static struct harshness_code
161 convert_harshness_ansi (type, parmtype, parm)
162      register tree type, parmtype;
163      tree parm;
164 {
165   struct harshness_code h;
166   register enum tree_code codel;
167   register enum tree_code coder;
168
169   h.code = 0;
170   h.distance = 0;
171   h.int_penalty = 0;
172
173 #ifdef GATHER_STATISTICS
174   n_convert_harshness++;
175 #endif
176
177   if (TYPE_PTRMEMFUNC_P (type))
178     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
179   if (TYPE_PTRMEMFUNC_P (parmtype))
180     parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
181
182   codel = TREE_CODE (type);
183   coder = TREE_CODE (parmtype);
184
185   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
186     return ZERO_RETURN (h);
187
188   if (coder == ERROR_MARK)
189     return EVIL_RETURN (h);
190
191   if (codel == POINTER_TYPE && fntype_p (parmtype))
192     {
193       tree p1, p2;
194       struct harshness_code h1, h2;
195
196       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
197       type = TREE_TYPE (type);
198
199       if (coder == POINTER_TYPE)
200         {
201           parmtype = TREE_TYPE (parmtype);
202           coder = TREE_CODE (parmtype);
203         }
204
205       if (coder != TREE_CODE (type))
206         return EVIL_RETURN (h);
207
208       /* We allow the default conversion between function type
209          and pointer-to-function type for free.  */
210       if (type == parmtype)
211         return ZERO_RETURN (h);
212
213       /* Compare return types.  */
214       p1 = TREE_TYPE (type);
215       p2 = TREE_TYPE (parmtype);
216       h2 = convert_harshness_ansi (p1, p2, NULL_TREE);
217       if (h2.code & EVIL_CODE)
218         return h2;
219
220       h1.code = TRIVIAL_CODE;
221       h1.distance = 0;
222
223       if (h2.distance != 0)
224         {
225           tree binfo;
226
227           /* This only works for pointers.  */
228           if (TREE_CODE (p1) != POINTER_TYPE
229               && TREE_CODE (p1) != REFERENCE_TYPE)
230             return EVIL_RETURN (h);
231
232           p1 = TREE_TYPE (p1);
233           p2 = TREE_TYPE (p2);
234           /* Don't die if we happen to be dealing with void*.  */
235           if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
236             return EVIL_RETURN (h);
237           if (h2.distance < 0)
238             binfo = get_binfo (p2, p1, 0);
239           else
240             binfo = get_binfo (p1, p2, 0);
241
242           if (! BINFO_OFFSET_ZEROP (binfo))
243             {
244               static int explained = 0;
245               if (h2.distance < 0)
246                 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p2, p1);
247               else
248                 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p1, p2);
249
250               if (! explained++)
251                 sorry ("(because pointer values change during conversion)");
252               return EVIL_RETURN (h);
253             }
254         }
255
256       h1.code |= h2.code;
257       if (h2.distance > h1.distance)
258         h1.distance = h2.distance;
259
260       p1 = TYPE_ARG_TYPES (type);
261       p2 = TYPE_ARG_TYPES (parmtype);
262       while (p1 && TREE_VALUE (p1) != void_type_node
263              && p2 && TREE_VALUE (p2) != void_type_node)
264         {
265           h2 = convert_harshness_ansi (TREE_VALUE (p1), TREE_VALUE (p2),
266                                        NULL_TREE);
267           if (h2.code & EVIL_CODE)
268             return h2;
269
270           if (h2.distance)
271             {
272               /* This only works for pointers and references. */
273               if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
274                   && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
275                 return EVIL_RETURN (h);
276               h2.distance = - h2.distance;
277             }
278
279           h1.code |= h2.code;
280           if (h2.distance > h1.distance)
281             h1.distance = h2.distance;
282           p1 = TREE_CHAIN (p1);
283           p2 = TREE_CHAIN (p2);
284         }
285       if (p1 == p2)
286         return h1;
287       if (p2)
288         {
289           if (p1)
290             return EVIL_RETURN (h);
291           h1.code |= ELLIPSIS_CODE;
292           return h1;
293         }
294       if (p1)
295         {
296           if (TREE_PURPOSE (p1) == NULL_TREE)
297             h1.code |= EVIL_CODE;
298           return h1;
299         }
300     }
301   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
302     {
303       /* Get to the OFFSET_TYPE that this might be.  */
304       type = TREE_TYPE (type);
305
306       if (coder != TREE_CODE (type))
307         return EVIL_RETURN (h);
308
309       if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
310         h.code = 0;
311       else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (type),
312                                TYPE_OFFSET_BASETYPE (parmtype)))
313         {
314           h.code = STD_CODE;
315           h.distance = 1;
316         }
317       else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (parmtype),
318                                TYPE_OFFSET_BASETYPE (type)))
319         {
320           h.code = STD_CODE;
321           h.distance = -1;
322         }
323       else
324         return EVIL_RETURN (h);
325       /* Now test the OFFSET_TYPE's target compatibility.  */
326       type = TREE_TYPE (type);
327       parmtype = TREE_TYPE (parmtype);
328     }
329
330   if (coder == UNKNOWN_TYPE)
331     {
332       if (codel == FUNCTION_TYPE
333           || codel == METHOD_TYPE
334           || (codel == POINTER_TYPE
335               && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
336                   || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
337         return TRIVIAL_RETURN (h);
338       return EVIL_RETURN (h);
339     }
340
341   if (coder == VOID_TYPE)
342     return EVIL_RETURN (h);
343
344   if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
345     {
346       /* Control equivalence of ints an enums.  */
347
348       if (codel == ENUMERAL_TYPE
349           && flag_int_enum_equivalence == 0)
350         {
351           /* Enums can be converted to ints, but not vice-versa.  */
352           if (coder != ENUMERAL_TYPE
353               || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
354             return EVIL_RETURN (h);
355         }
356
357       /* else enums and ints (almost) freely interconvert.  */
358
359       if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
360         {
361           if (TYPE_MAIN_VARIANT (type)
362               == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
363             {
364               h.code = PROMO_CODE;
365 #if 0 /* What purpose does this serve?  -jason */
366               /* A char, short, wchar_t, etc., should promote to an int if
367                  it can handle it, otherwise to an unsigned.  So we'll make
368                  an unsigned.  */
369               if (type != integer_type_node)
370                 h.int_penalty = 1;
371 #endif
372             }
373           else
374             h.code = STD_CODE;
375             
376           return h;
377         }
378       else if (coder == REAL_TYPE)
379         {
380           h.code = STD_CODE;
381           h.distance = 0;
382           return h;
383         }
384     }
385
386   if (codel == REAL_TYPE)
387     {
388       if (coder == REAL_TYPE)
389         {
390           if (TYPE_MAIN_VARIANT (type)
391               == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
392             h.code = PROMO_CODE;
393           else
394             h.code = STD_CODE;
395             
396           return h;
397         }
398       else if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
399         {
400           h.code = STD_CODE;
401           h.distance = 0;
402           return h;
403         }
404     }
405
406   /* Convert arrays which have not previously been converted.  */
407   if (codel == ARRAY_TYPE)
408     codel = POINTER_TYPE;
409   if (coder == ARRAY_TYPE)
410     coder = POINTER_TYPE;
411
412   /* Conversions among pointers */
413   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
414     {
415       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
416       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
417       int penalty = 4 * (ttl != ttr);
418
419       /* Anything converts to void *.  void * converts to anything.
420          Since these may be `const void *' (etc.) use VOID_TYPE
421          instead of void_type_node.  Otherwise, the targets must be the same,
422          except that we do allow (at some cost) conversion between signed and
423          unsigned pointer types.  */
424
425       if ((TREE_CODE (ttl) == METHOD_TYPE
426            || TREE_CODE (ttl) == FUNCTION_TYPE)
427           && TREE_CODE (ttl) == TREE_CODE (ttr))
428         {
429           if (comptypes (ttl, ttr, -1))
430             {
431               h.code = penalty ? STD_CODE : 0;
432               h.distance =  0;
433             }
434           else
435             h.code = EVIL_CODE;
436           return h;
437         }
438
439 #if 1
440       if (TREE_CODE (ttl) != VOID_TYPE && TREE_CODE (ttr) != VOID_TYPE)
441         {
442           if (TREE_UNSIGNED (ttl) != TREE_UNSIGNED (ttr))
443             {
444               ttl = unsigned_type (ttl);
445               ttr = unsigned_type (ttr);
446               penalty = 10;
447             }
448           if (! comp_target_types (ttl, ttr, 0))
449             return EVIL_RETURN (h);
450         }
451 #else
452       if (!(TREE_CODE (ttl) == VOID_TYPE
453             || TREE_CODE (ttr) == VOID_TYPE
454             || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
455                 && (ttl = unsigned_type (ttl),
456                     ttr = unsigned_type (ttr),
457                     penalty = 10, 0))
458             || (comp_target_types (ttl, ttr, 0))))
459         return EVIL_RETURN (h);
460 #endif
461
462       if (penalty == 10 || ttr == ttl)
463         {
464           tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
465
466           /* If one was unsigned but the other wasn't, then we need to
467              do a standard conversion from T to unsigned T.  */
468           if (penalty == 10)
469             h.code = PROMO_CODE; /* was STD_CODE */
470           else
471             h.code = 0;
472
473           /* Note conversion from `T*' to `const T*',
474                                or `T*' to `volatile T*'.  */
475           if (ttl == ttr
476               && ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
477                   || (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2))))
478             h.code |= QUAL_CODE;
479
480           h.distance = 0;
481           return h;
482         }
483
484
485       if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
486         {
487           int b_or_d = get_base_distance (ttl, ttr, 0, 0);
488           if (b_or_d < 0)
489             {
490               b_or_d = get_base_distance (ttr, ttl, 0, 0);
491               if (b_or_d < 0)
492                 return EVIL_RETURN (h);
493               h.distance = -b_or_d;
494             }
495           else
496             h.distance = b_or_d;
497           h.code = STD_CODE;
498           return h;
499         }
500
501       /* If converting from a `class*' to a `void*', make it
502          less favorable than any inheritance relationship.  */
503       if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
504         {
505           h.code = STD_CODE;
506           h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
507           return h;
508         }
509       h.code = penalty ? STD_CODE : PROMO_CODE;
510       return h;
511     }
512
513   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
514     {
515       /* This is not a bad match, but don't let it beat
516          integer-enum combinations.  */
517       if (parm && integer_zerop (parm))
518         {
519           h.code = STD_CODE;
520           h.distance = 0;
521           return h;
522         }
523     }
524
525   /* C++: one of the types must be a reference type.  */
526   {
527     tree ttl, ttr;
528     register tree intype = TYPE_MAIN_VARIANT (parmtype);
529     register enum tree_code form = TREE_CODE (intype);
530     int penalty = 0;
531
532     if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
533       {
534         ttl = TYPE_MAIN_VARIANT (type);
535
536         if (codel == REFERENCE_TYPE)
537           {
538             ttl = TREE_TYPE (ttl);
539
540             /* When passing a non-const argument into a const reference,
541                dig it a little, so a non-const reference is preferred over
542                this one. (mrs) */
543             if (parm && TREE_READONLY (ttl) && ! TREE_READONLY (parm))
544               penalty = 2;
545             else
546               penalty = 0;
547
548             ttl = TYPE_MAIN_VARIANT (ttl);
549
550             if (form == OFFSET_TYPE)
551               {
552                 intype = TREE_TYPE (intype);
553                 form = TREE_CODE (intype);
554               }
555
556             if (form == REFERENCE_TYPE)
557               {
558                 intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
559
560                 if (ttl == intype)
561                   return ZERO_RETURN (h);
562                 penalty = 2;
563               }
564             else
565               {
566                 /* Can reference be built up?  */
567                 if (ttl == intype && penalty == 0) {
568                   /* Because the READONLY and VIRTUAL bits are not always in
569                      the type, this extra check is necessary.  The problem
570                      should be fixed someplace else, and this extra code
571                      removed.
572
573                      Also, if type if a reference, the readonly bits could
574                      either be in the outer type (with reference) or on the
575                      inner type (the thing being referenced).  (mrs)  */
576                   if (parm
577                       && ((TREE_READONLY (parm)
578                            && ! (TYPE_READONLY (type)
579                                  || (TREE_CODE (type) == REFERENCE_TYPE
580                                      && TYPE_READONLY (TREE_TYPE (type)))))
581                           || (TREE_SIDE_EFFECTS (parm)
582                               && ! (TYPE_VOLATILE (type)
583                                     || (TREE_CODE (type) == REFERENCE_TYPE
584                                         && TYPE_VOLATILE (TREE_TYPE (type)))))))
585                     penalty = 2;
586                   else
587                     return ZERO_RETURN (h);
588                 }
589                 else
590                   penalty = 2;
591               }
592           }
593         else if (form == REFERENCE_TYPE)
594           {
595             if (parm)
596               {
597                 tree tmp = convert_from_reference (parm);
598                 intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
599               }
600             else
601               {
602                 intype = parmtype;
603                 do
604                   intype = TREE_TYPE (intype);
605                 while (TREE_CODE (intype) == REFERENCE_TYPE);
606                 intype = TYPE_MAIN_VARIANT (intype);
607               }
608
609             if (ttl == intype)
610               return ZERO_RETURN (h);
611             else
612               penalty = 2;
613           }
614
615         if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
616           {
617             ttl = unsigned_type (ttl);
618             intype = unsigned_type (intype);
619             penalty += 2;
620           }
621
622         ttr = intype;
623
624         /* If the initializer is not an lvalue, then it does not
625            matter if we make life easier for the programmer
626            by creating a temporary variable with which to
627            hold the result.  */
628         if (parm && (coder == INTEGER_TYPE
629                      || coder == ENUMERAL_TYPE
630                      || coder == REAL_TYPE)
631             && ! lvalue_p (parm))
632           {
633             h = convert_harshness_ansi (ttl, ttr, NULL_TREE);
634             if (penalty > 2 || h.code != 0)
635               h.code |= STD_CODE;
636             else
637               h.code |= TRIVIAL_CODE;
638             h.distance = 0;
639             return h;
640           }
641
642         if (ttl == ttr)
643           {
644             if (penalty > 2)
645               {
646                 h.code = STD_CODE;
647                 h.distance = 0;
648               }
649             else
650               {
651                 h.code = TRIVIAL_CODE;
652                 /* We set this here so that build_overload_call_real will be
653                    able to see the penalty we found, rather than just looking
654                    at a TRIVIAL_CODE with no other information.  */
655                 h.int_penalty = penalty;
656               }
657             return h;
658           }
659
660         /* Pointers to voids always convert for pointers.  But
661            make them less natural than more specific matches.  */
662         if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
663           {
664             if (TREE_TYPE (ttl) == void_type_node
665                 || TREE_TYPE (ttr) == void_type_node)
666               {
667                 h.code = STD_CODE;
668                 h.distance = 0;
669                 return h;
670               }
671           }
672
673         if (parm && codel != REFERENCE_TYPE)
674           {
675             h = convert_harshness_ansi (ttl, ttr, NULL_TREE);
676             if (penalty == 2)
677               h.code |= QUAL_CODE;
678             else if (penalty == 4)
679               h.code |= STD_CODE;
680             h.distance = 0;
681             return h;
682           }
683
684         /* Here it does matter.  If this conversion is from derived to base,
685            allow it.  Otherwise, types must be compatible in the strong sense.  */
686         if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
687           {
688             int b_or_d = get_base_distance (ttl, ttr, 0, 0);
689             if (b_or_d < 0)
690               {
691                 b_or_d = get_base_distance (ttr, ttl, 0, 0);
692                 if (b_or_d < 0)
693                   return EVIL_RETURN (h);
694                 h.distance = -b_or_d;
695               }
696             /* Say that this conversion is relatively painless.
697                If it turns out that there is a user-defined X(X&)
698                constructor, then that will be invoked, but that's
699                preferable to dealing with other user-defined conversions
700                that may produce surprising results.  */
701             else
702               h.distance = b_or_d;
703             h.code = STD_CODE;
704             return h;
705           }
706
707         if (comp_target_types (ttl, intype, 1))
708           {
709             if (penalty)
710               h.code = STD_CODE;
711             h.distance = 0;
712             return h;
713           }
714       }
715   }
716   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
717     {
718       int b_or_d = get_base_distance (type, parmtype, 0, 0);
719       if (b_or_d < 0)
720         {
721           b_or_d = get_base_distance (parmtype, type, 0, 0);
722           if (b_or_d < 0)
723             return EVIL_RETURN (h);
724           h.distance = -b_or_d;
725         }
726       else
727         h.distance = b_or_d;
728       h.code = STD_CODE;
729       return h;
730     }
731   return EVIL_RETURN (h);
732 }
733
734 /* TYPE is the type we wish to convert to.  PARM is the parameter
735    we have to work with.  We use a somewhat arbitrary cost function
736    to measure this conversion.  */
737 static int
738 convert_harshness_old (type, parmtype, parm)
739      register tree type, parmtype;
740      tree parm;
741 {
742   register enum tree_code codel;
743   register enum tree_code coder;
744
745 #ifdef GATHER_STATISTICS
746   n_convert_harshness++;
747 #endif
748
749   if (TYPE_PTRMEMFUNC_P (type))
750     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
751   if (TYPE_PTRMEMFUNC_P (parmtype))
752     parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
753
754   codel = TREE_CODE (type);
755   coder = TREE_CODE (parmtype);
756
757   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
758     return TRIVIAL;
759
760   if (coder == ERROR_MARK)
761     return EVIL;
762
763   if (codel == POINTER_TYPE && fntype_p (parmtype))
764     {
765       tree p1, p2;
766       int harshness, new_harshness;
767
768       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
769       type = TREE_TYPE (type);
770
771       if (coder == POINTER_TYPE)
772         {
773           parmtype = TREE_TYPE (parmtype);
774           coder = TREE_CODE (parmtype);
775         }
776
777       if (coder != TREE_CODE (type))
778         return EVIL;
779
780       harshness = 0;
781
782       /* We allow the default conversion between function type
783          and pointer-to-function type for free.  */
784       if (type == parmtype)
785         return TRIVIAL;
786
787       /* Compare return types.  */
788       p1 = TREE_TYPE (type);
789       p2 = TREE_TYPE (parmtype);
790       new_harshness = convert_harshness_old (p1, p2, NULL_TREE);
791       if (EVIL_HARSHNESS (new_harshness))
792         return EVIL;
793
794       if (BASE_DERIVED_HARSHNESS (new_harshness))
795         {
796           tree binfo;
797
798           /* This only works for pointers.  */
799           if (TREE_CODE (p1) != POINTER_TYPE
800               && TREE_CODE (p1) != REFERENCE_TYPE)
801             return EVIL;
802
803           p1 = TREE_TYPE (p1);
804           p2 = TREE_TYPE (p2);
805           /* Don't die if we happen to be dealing with void*.  */
806           if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
807             return EVIL;
808           if (CONTRAVARIANT_HARSHNESS (new_harshness))
809             binfo = get_binfo (p2, p1, 0);
810           else
811             binfo = get_binfo (p1, p2, 0);
812
813           if (! BINFO_OFFSET_ZEROP (binfo))
814             {
815               static int explained = 0;
816               if (CONTRAVARIANT_HARSHNESS (new_harshness))
817                 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p2, p1);
818               else
819                 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p1, p2);
820
821               if (! explained++)
822                 sorry ("(because pointer values change during conversion)");
823               return EVIL;
824             }
825         }
826
827       harshness |= new_harshness;
828
829       p1 = TYPE_ARG_TYPES (type);
830       p2 = TYPE_ARG_TYPES (parmtype);
831       while (p1 && TREE_VALUE (p1) != void_type_node
832              && p2 && TREE_VALUE (p2) != void_type_node)
833         {
834           new_harshness = convert_harshness_old (TREE_VALUE (p1),
835                                                  TREE_VALUE (p2), NULL_TREE);
836           if (EVIL_HARSHNESS (new_harshness))
837             return EVIL;
838
839           if (BASE_DERIVED_HARSHNESS (new_harshness))
840             {
841               /* This only works for pointers and references. */
842               if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
843                   && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
844                 return EVIL;
845               new_harshness ^= CONTRAVARIANT_HARSHNESS (new_harshness);
846               harshness |= new_harshness;
847             }
848           /* This trick allows use to accumulate easy type
849              conversions without messing up the bits that encode
850              info about more involved things.  */
851           else if (ONLY_EASY_HARSHNESS (new_harshness))
852             harshness += new_harshness;
853           else
854             harshness |= new_harshness;
855           p1 = TREE_CHAIN (p1);
856           p2 = TREE_CHAIN (p2);
857         }
858       if (p1 == p2)
859         return harshness;
860       if (p2)
861         return p1 ? EVIL : (harshness | ELLIPSIS_HARSHNESS (-1));
862       if (p1)
863         return harshness | (TREE_PURPOSE (p1) == NULL_TREE);
864     }
865   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
866     {
867       /* XXX: Note this is set a few times, but it's never actually
868          used! (bpk) */
869       int harshness;
870
871       /* Get to the OFFSET_TYPE that this might be.  */
872       type = TREE_TYPE (type);
873
874       if (coder != TREE_CODE (type))
875         return EVIL;
876
877       harshness = 0;
878
879       if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
880         harshness = 0;
881       else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (type),
882                                TYPE_OFFSET_BASETYPE (parmtype)))
883         harshness = INT_TO_BD_HARSHNESS (1);
884       else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (parmtype),
885                                TYPE_OFFSET_BASETYPE (type)))
886         harshness = CONTRAVARIANT_HARSHNESS (-1);
887       else
888         return EVIL;
889       /* Now test the OFFSET_TYPE's target compatibility.  */
890       type = TREE_TYPE (type);
891       parmtype = TREE_TYPE (parmtype);
892     }
893
894   if (coder == UNKNOWN_TYPE)
895     {
896       if (codel == FUNCTION_TYPE
897           || codel == METHOD_TYPE
898           || (codel == POINTER_TYPE
899               && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
900                   || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
901         return TRIVIAL;
902       return EVIL;
903     }
904
905   if (coder == VOID_TYPE)
906     return EVIL;
907
908   if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
909     {
910       /* Control equivalence of ints an enums.  */
911
912       if (codel == ENUMERAL_TYPE
913           && flag_int_enum_equivalence == 0)
914         {
915           /* Enums can be converted to ints, but not vice-versa.  */
916           if (coder != ENUMERAL_TYPE
917               || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
918             return EVIL;
919         }
920
921       /* else enums and ints (almost) freely interconvert.  */
922
923       if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
924         {
925           int easy = TREE_UNSIGNED (type) ^ TREE_UNSIGNED (parmtype);
926           if (codel != coder)
927             easy += 1;
928           if (TYPE_MODE (type) != TYPE_MODE (parmtype))
929             easy += 2;
930           return INT_TO_EASY_HARSHNESS (easy);
931         }
932       else if (coder == REAL_TYPE)
933         return INT_TO_EASY_HARSHNESS (4);
934     }
935
936   if (codel == REAL_TYPE)
937     if (coder == REAL_TYPE)
938       /* Shun converting between float and double if a choice exists.  */
939       {
940         if (TYPE_MODE (type) != TYPE_MODE (parmtype))
941           return INT_TO_EASY_HARSHNESS (2);
942         return TRIVIAL;
943       }
944     else if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
945       return INT_TO_EASY_HARSHNESS (4);
946
947   /* convert arrays which have not previously been converted.  */
948   if (codel == ARRAY_TYPE)
949     codel = POINTER_TYPE;
950   if (coder == ARRAY_TYPE)
951     coder = POINTER_TYPE;
952
953   /* Conversions among pointers */
954   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
955     {
956       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
957       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
958       int penalty = 4 * (ttl != ttr);
959       /* Anything converts to void *.  void * converts to anything.
960          Since these may be `const void *' (etc.) use VOID_TYPE
961          instead of void_type_node.
962          Otherwise, the targets must be the same,
963          except that we do allow (at some cost) conversion
964          between signed and unsinged pointer types.  */
965
966       if ((TREE_CODE (ttl) == METHOD_TYPE
967            || TREE_CODE (ttl) == FUNCTION_TYPE)
968           && TREE_CODE (ttl) == TREE_CODE (ttr))
969         {
970           if (comptypes (ttl, ttr, -1))
971             return INT_TO_EASY_HARSHNESS (penalty);
972           return EVIL;
973         }
974
975       if (!(TREE_CODE (ttl) == VOID_TYPE
976             || TREE_CODE (ttr) == VOID_TYPE
977             || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
978                 && (ttl = unsigned_type (ttl),
979                     ttr = unsigned_type (ttr),
980                     penalty = 10, 0))
981             || (comp_target_types (ttl, ttr, 0))))
982         return EVIL;
983
984       if (penalty == 10)
985         return INT_TO_EASY_HARSHNESS (10);
986       if (ttr == ttl)
987         return INT_TO_BD_HARSHNESS (0);
988
989       if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
990         {
991           int b_or_d = get_base_distance (ttl, ttr, 0, 0);
992           if (b_or_d < 0)
993             {
994               b_or_d = get_base_distance (ttr, ttl, 0, 0);
995               if (b_or_d < 0)
996                 return EVIL;
997               return CONTRAVARIANT_HARSHNESS (-1);
998             }
999           return INT_TO_BD_HARSHNESS (b_or_d);
1000         }
1001       /* If converting from a `class*' to a `void*', make it
1002          less favorable than any inheritance relationship.  */
1003       if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
1004         return INT_TO_BD_HARSHNESS (CLASSTYPE_MAX_DEPTH (ttr)+1);
1005       return INT_TO_EASY_HARSHNESS (penalty);
1006     }
1007
1008   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
1009     {
1010       /* This is not a bad match, but don't let it beat
1011          integer-enum combinations.  */
1012       if (parm && integer_zerop (parm))
1013         return INT_TO_EASY_HARSHNESS (4);
1014     }
1015
1016   /* C++: Since the `this' parameter of a signature member function
1017      is represented as a signature pointer to handle default implementations
1018      correctly, we can have the case that `type' is a signature pointer
1019      while `parmtype' is a pointer to a signature table.  We don't really
1020      do any conversions in this case, so just return 0.  */
1021
1022   if (codel == RECORD_TYPE && coder == POINTER_TYPE
1023       && IS_SIGNATURE_POINTER (type) && IS_SIGNATURE (TREE_TYPE (parmtype)))
1024     return 0;
1025
1026   /* C++: one of the types must be a reference type.  */
1027   {
1028     tree ttl, ttr;
1029     register tree intype = TYPE_MAIN_VARIANT (parmtype);
1030     register enum tree_code form = TREE_CODE (intype);
1031     int penalty;
1032
1033     if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
1034       {
1035         ttl = TYPE_MAIN_VARIANT (type);
1036
1037         if (codel == REFERENCE_TYPE)
1038           {
1039             ttl = TREE_TYPE (ttl);
1040
1041             /* When passing a non-const argument into a const reference,
1042                dig it a little, so a non-const reference is preferred over
1043                this one. (mrs) */
1044             if (parm && TREE_READONLY (ttl) && ! TREE_READONLY (parm))
1045               penalty = 2;
1046             else
1047               penalty = 0;
1048
1049             ttl = TYPE_MAIN_VARIANT (ttl);
1050
1051             if (form == OFFSET_TYPE)
1052               {
1053                 intype = TREE_TYPE (intype);
1054                 form = TREE_CODE (intype);
1055               }
1056
1057             if (form == REFERENCE_TYPE)
1058               {
1059                 intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
1060
1061                 if (ttl == intype)
1062                   return TRIVIAL;
1063                 penalty = 2;
1064               }
1065             else
1066               {
1067                 /* Can reference be built up?  */
1068                 if (ttl == intype && penalty == 0) {
1069                   /* Because the READONLY bits and VIRTUAL bits are not always
1070                      in the type, this extra check is necessary.  The problem
1071                      should be fixed someplace else, and this extra code
1072                      removed.
1073
1074                      Also, if type if a reference, the readonly bits could
1075                      either be in the outer type (with reference) or on the
1076                      inner type (the thing being referenced).  (mrs)  */
1077                   if (parm
1078                       && ((TREE_READONLY (parm)
1079                            && ! (TYPE_READONLY (type)
1080                                  || (TREE_CODE (type) == REFERENCE_TYPE
1081                                      && TYPE_READONLY (TREE_TYPE (type)))))
1082                           || (TREE_SIDE_EFFECTS (parm)
1083                               && ! (TYPE_VOLATILE (type)
1084                                     || (TREE_CODE (type) == REFERENCE_TYPE
1085                                         && TYPE_VOLATILE (TREE_TYPE (type)))))))
1086                     penalty = 2;
1087                   else
1088                     return TRIVIAL;
1089                 }
1090                 else
1091                   penalty = 2;
1092               }
1093           }
1094         else if (form == REFERENCE_TYPE)
1095           {
1096             if (parm)
1097               {
1098                 tree tmp = convert_from_reference (parm);
1099                 intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
1100               }
1101             else
1102               {
1103                 intype = parmtype;
1104                 do
1105                   {
1106                     intype = TREE_TYPE (intype);
1107                   }
1108                 while (TREE_CODE (intype) == REFERENCE_TYPE);
1109                 intype = TYPE_MAIN_VARIANT (intype);
1110               }
1111
1112             if (ttl == intype)
1113               return TRIVIAL;
1114             else
1115               penalty = 2;
1116           }
1117
1118         if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
1119           {
1120             ttl = unsigned_type (ttl);
1121             intype = unsigned_type (intype);
1122             penalty += 2;
1123           }
1124
1125         ttr = intype;
1126
1127         /* If the initializer is not an lvalue, then it does not
1128            matter if we make life easier for the programmer
1129            by creating a temporary variable with which to
1130            hold the result.  */
1131         if (parm && (coder == INTEGER_TYPE
1132                      || coder == ENUMERAL_TYPE
1133                      || coder == REAL_TYPE)
1134             && ! lvalue_p (parm))
1135           return (convert_harshness_old (ttl, ttr, NULL_TREE)
1136                   | INT_TO_EASY_HARSHNESS (penalty));
1137
1138         if (ttl == ttr)
1139           {
1140             if (penalty)
1141               return INT_TO_EASY_HARSHNESS (penalty);
1142             return INT_TO_BD_HARSHNESS (0);
1143           }
1144
1145         /* Pointers to voids always convert for pointers.  But
1146            make them less natural than more specific matches.  */
1147         if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
1148           if (TREE_TYPE (ttl) == void_type_node
1149               || TREE_TYPE (ttr) == void_type_node)
1150             return INT_TO_EASY_HARSHNESS (penalty+1);
1151
1152         if (parm && codel != REFERENCE_TYPE)
1153           return (convert_harshness_old (ttl, ttr, NULL_TREE)
1154                   | INT_TO_EASY_HARSHNESS (penalty));
1155
1156         /* Here it does matter.  If this conversion is from
1157            derived to base, allow it.  Otherwise, types must
1158            be compatible in the strong sense.  */
1159         if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
1160           {
1161             int b_or_d = get_base_distance (ttl, ttr, 0, 0);
1162             if (b_or_d < 0)
1163               {
1164                 b_or_d = get_base_distance (ttr, ttl, 0, 0);
1165                 if (b_or_d < 0)
1166                   return EVIL;
1167                 return CONTRAVARIANT_HARSHNESS (-1);
1168               }
1169             /* Say that this conversion is relatively painless.
1170                If it turns out that there is a user-defined X(X&)
1171                constructor, then that will be invoked, but that's
1172                preferable to dealing with other user-defined conversions
1173                that may produce surprising results.  */
1174             return INT_TO_BD_HARSHNESS (b_or_d);
1175           }
1176
1177         if (comp_target_types (ttl, intype, 1))
1178           return INT_TO_EASY_HARSHNESS (penalty);
1179       }
1180   }
1181   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
1182     {
1183       int b_or_d = get_base_distance (type, parmtype, 0, 0);
1184       if (b_or_d < 0)
1185         {
1186           b_or_d = get_base_distance (parmtype, type, 0, 0);
1187           if (b_or_d < 0)
1188             return EVIL;
1189           return CONTRAVARIANT_HARSHNESS (-1);
1190         }
1191       return INT_TO_BD_HARSHNESS (b_or_d);
1192     }
1193   return EVIL;
1194 }
1195
1196 #ifdef DEBUG_MATCHING
1197 static char *
1198 print_harshness (h)
1199      struct harshness_code *h;
1200 {
1201   static char buf[1024];
1202   char tmp[1024];
1203
1204   bzero (buf, 1024 * sizeof (char));
1205   strcat (buf, "codes=[");
1206   if (h->code & EVIL_CODE)
1207     strcat (buf, "EVIL");
1208   if (h->code & CONST_CODE)
1209     strcat (buf, " CONST");
1210   if (h->code & ELLIPSIS_CODE)
1211     strcat (buf, " ELLIPSIS");
1212   if (h->code & USER_CODE)
1213     strcat (buf, " USER");
1214   if (h->code & STD_CODE)
1215     strcat (buf, " STD");
1216   if (h->code & PROMO_CODE)
1217     strcat (buf, " PROMO");
1218   if (h->code & QUAL_CODE)
1219     strcat (buf, " QUAL");
1220   if (h->code & TRIVIAL_CODE)
1221     strcat (buf, " TRIVIAL");
1222   if (buf[0] == '\0')
1223     strcat (buf, "0");
1224
1225   sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
1226
1227   strcat (buf, tmp);
1228
1229   return buf;
1230 }
1231 #endif
1232
1233 /* Algorithm: For each argument, calculate how difficult it is to
1234    make FUNCTION accept that argument.  If we can easily tell that
1235    FUNCTION won't be acceptable to one of the arguments, then we
1236    don't need to compute the ease of converting the other arguments,
1237    since it will never show up in the intersection of all arguments'
1238    favorite functions.
1239
1240    Conversions between builtin and user-defined types are allowed, but
1241    no function involving such a conversion is preferred to one which
1242    does not require such a conversion.  Furthermore, such conversions
1243    must be unique.  */
1244
1245 void
1246 compute_conversion_costs_ansi (function, tta_in, cp, arglen)
1247      tree function;
1248      tree tta_in;
1249      struct candidate *cp;
1250      int arglen;
1251 {
1252   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
1253   tree ttf = ttf_in;
1254   tree tta = tta_in;
1255
1256   /* Start out with no strikes against.  */
1257   int evil_strikes = 0;
1258   int ellipsis_strikes = 0;
1259   int user_strikes = 0;
1260   int b_or_d_strikes = 0;
1261   int easy_strikes = 0;
1262
1263   int strike_index = 0, win;
1264   struct harshness_code lose;
1265
1266 #ifdef GATHER_STATISTICS
1267   n_compute_conversion_costs++;
1268 #endif
1269
1270   cp->function = function;
1271   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
1272   cp->u.bad_arg = 0;            /* optimistic!  */
1273
1274   cp->h.code = 0;
1275   cp->h.distance = 0;
1276   cp->h.int_penalty = 0;
1277   bzero (cp->v.ansi_harshness,
1278          (cp->h_len + 1) * sizeof (struct harshness_code));
1279
1280   while (ttf && tta)
1281     {
1282       struct harshness_code h;
1283
1284       if (ttf == void_list_node)
1285         break;
1286
1287       if (type_unknown_p (TREE_VALUE (tta)))
1288         {         
1289           /* Must perform some instantiation here.  */
1290           tree rhs = TREE_VALUE (tta);
1291           tree lhstype = TREE_VALUE (ttf);
1292
1293           /* Keep quiet about possible contravariance violations.  */
1294           int old_inhibit_warnings = inhibit_warnings;
1295           inhibit_warnings = 1;
1296
1297           /* @@ This is to undo what `grokdeclarator' does to
1298              parameter types.  It really should go through
1299              something more general.  */
1300
1301           TREE_TYPE (tta) = unknown_type_node;
1302           rhs = instantiate_type (lhstype, rhs, 0);
1303           inhibit_warnings = old_inhibit_warnings;
1304
1305           if (TREE_CODE (rhs) == ERROR_MARK)
1306             h.code = EVIL_CODE;
1307           else
1308             h = convert_harshness_ansi (lhstype, TREE_TYPE (rhs), rhs);
1309         }
1310       else
1311         {
1312 #ifdef DEBUG_MATCHING
1313           static tree old_function = NULL_TREE;
1314
1315           if (!old_function || function != old_function)
1316             {
1317               cp_error ("trying %D", function);
1318               old_function = function;
1319             }
1320
1321           cp_error ("      doing (%T) %E against arg %T",
1322                     TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
1323                     TREE_VALUE (ttf));
1324 #endif
1325
1326           h = convert_harshness_ansi (TREE_VALUE (ttf),
1327                                       TREE_TYPE (TREE_VALUE (tta)),
1328                                       TREE_VALUE (tta));
1329
1330 #ifdef DEBUG_MATCHING
1331           cp_error ("     evaluated %s", print_harshness (&h));
1332 #endif
1333         }
1334
1335       cp->v.ansi_harshness[strike_index] = h;
1336       if ((h.code & EVIL_CODE)
1337           || ((h.code & STD_CODE) && h.distance < 0))
1338         {
1339           cp->u.bad_arg = strike_index;
1340           evil_strikes = 1;
1341         }
1342      else if (h.code & ELLIPSIS_CODE)
1343        ellipsis_strikes += 1;
1344 #if 0
1345       /* This is never set by `convert_harshness_ansi'.  */
1346       else if (h.code & USER_CODE)
1347         {
1348           user_strikes += 1;
1349         }
1350 #endif
1351       else
1352         {
1353           if ((h.code & STD_CODE) && h.distance)
1354             {
1355               if (h.distance > b_or_d_strikes)
1356                 b_or_d_strikes = h.distance;
1357             }
1358           else
1359             easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
1360           cp->h.code |= h.code;
1361           /* Make sure we communicate this.  */
1362           cp->h.int_penalty += h.int_penalty;
1363         }
1364
1365       ttf = TREE_CHAIN (ttf);
1366       tta = TREE_CHAIN (tta);
1367       strike_index += 1;
1368     }
1369
1370   if (tta)
1371     {
1372       /* ran out of formals, and parmlist is fixed size.  */
1373       if (ttf /* == void_type_node */)
1374         {
1375           cp->h.code = EVIL_CODE;
1376           cp->u.bad_arg = -1;
1377           return;
1378         }
1379       else
1380         {
1381           struct harshness_code h;
1382           int l = list_length (tta);
1383           ellipsis_strikes += l;
1384           h.code = ELLIPSIS_CODE;
1385           h.distance = 0;
1386           h.int_penalty = 0;
1387           for (; l; --l)
1388             cp->v.ansi_harshness[strike_index++] = h;
1389         }
1390     }
1391   else if (ttf && ttf != void_list_node)
1392     {
1393       /* ran out of actuals, and no defaults.  */
1394       if (TREE_PURPOSE (ttf) == NULL_TREE)
1395         {
1396           cp->h.code = EVIL_CODE;
1397           cp->u.bad_arg = -2;
1398           return;
1399         }
1400       /* Store index of first default.  */
1401       cp->v.ansi_harshness[arglen].distance = strike_index+1;
1402     }
1403   else
1404     cp->v.ansi_harshness[arglen].distance = 0;
1405
1406   /* Argument list lengths work out, so don't need to check them again.  */
1407   if (evil_strikes)
1408     {
1409       /* We do not check for derived->base conversions here, since in
1410          no case would they give evil strike counts, unless such conversions
1411          are somehow ambiguous.  */
1412
1413       /* See if any user-defined conversions apply.
1414          But make sure that we do not loop.  */
1415       static int dont_convert_types = 0;
1416
1417       if (dont_convert_types)
1418         {
1419           cp->h.code = EVIL_CODE;
1420           return;
1421         }
1422
1423       win = 0;                  /* Only get one chance to win.  */
1424       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
1425       tta = tta_in;
1426       strike_index = 0;
1427       evil_strikes = 0;
1428
1429       while (ttf && tta)
1430         {
1431           if (ttf == void_list_node)
1432             break;
1433
1434           lose = cp->v.ansi_harshness[strike_index];
1435           if ((lose.code & EVIL_CODE)
1436               || ((lose.code & STD_CODE) && lose.distance < 0))
1437             {
1438               tree actual_type = TREE_TYPE (TREE_VALUE (tta));
1439               tree formal_type = TREE_VALUE (ttf);
1440               int extra_conversions = 0;
1441
1442               dont_convert_types = 1;
1443
1444               if (TREE_CODE (formal_type) == REFERENCE_TYPE)
1445                 formal_type = TREE_TYPE (formal_type);
1446               if (TREE_CODE (actual_type) == REFERENCE_TYPE)
1447                 actual_type = TREE_TYPE (actual_type);
1448
1449               if (formal_type != error_mark_node
1450                   && actual_type != error_mark_node)
1451                 {
1452                   formal_type = TYPE_MAIN_VARIANT (formal_type);
1453                   actual_type = TYPE_MAIN_VARIANT (actual_type);
1454
1455                   if (TYPE_HAS_CONSTRUCTOR (formal_type))
1456                     {
1457                       /* If it has a constructor for this type,
1458                          try to use it.  */
1459                       /* @@ There is no way to save this result yet, so
1460                          success is a NULL_TREE for now.  */
1461                       if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
1462                           != error_mark_node)
1463                         win++;
1464                     }
1465                   if (TYPE_LANG_SPECIFIC (actual_type)
1466                       && TYPE_HAS_CONVERSION (actual_type))
1467                     {
1468                       tree conv;
1469                       /* Don't issue warnings since we're only groping
1470                          around for the right answer, we haven't yet
1471                          committed to going with this solution.  */
1472                       int old_inhibit_warnings = inhibit_warnings;
1473
1474                       inhibit_warnings = 1;
1475                       conv = build_type_conversion
1476                         (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
1477                       inhibit_warnings = old_inhibit_warnings;
1478
1479                       if (conv)
1480                         {
1481                           if (conv == error_mark_node)
1482                             win += 2;
1483                           else
1484                             {
1485                               win++;
1486                               if (TREE_CODE (conv) != CALL_EXPR)
1487                                 extra_conversions = 1;
1488                             }
1489                         }
1490                       else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
1491                         {
1492                           conv = build_type_conversion (CALL_EXPR, formal_type,
1493                                                         TREE_VALUE (tta), 0);
1494                           if (conv)
1495                             {
1496                               if (conv == error_mark_node)
1497                                 win += 2;
1498                               else
1499                                 {
1500                                   win++;
1501                                   if (TREE_CODE (conv) != CALL_EXPR)
1502                                     extra_conversions = 1;
1503                                 }
1504                             }
1505                         }
1506                     }
1507                 }
1508               dont_convert_types = 0;
1509
1510               if (win == 1)
1511                 {
1512                   user_strikes += 1;
1513                   cp->v.ansi_harshness[strike_index].code
1514                     = USER_CODE | (extra_conversions ? STD_CODE : 0);
1515                   win = 0;
1516                 }
1517               else
1518                 {
1519                   if (cp->u.bad_arg > strike_index)
1520                     cp->u.bad_arg = strike_index;
1521
1522                   evil_strikes = win ? 2 : 1;
1523                   break;
1524                 }
1525             }
1526
1527           ttf = TREE_CHAIN (ttf);
1528           tta = TREE_CHAIN (tta);
1529           strike_index += 1;
1530         }
1531     }
1532
1533   /* Const member functions get a small penalty because defaulting
1534      to const is less useful than defaulting to non-const. */
1535   /* This is bogus, it does not correspond to anything in the ARM.
1536      This code will be fixed when this entire section is rewritten
1537      to conform to the ARM.  (mrs)  */
1538   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1539     {
1540       tree this_parm = TREE_VALUE (ttf_in);
1541
1542       if (TREE_CODE (this_parm) == RECORD_TYPE  /* Is `this' a sig ptr?  */
1543             ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
1544             : TYPE_READONLY (TREE_TYPE (this_parm)))
1545         {
1546           cp->v.ansi_harshness[0].code |= TRIVIAL_CODE;
1547           ++easy_strikes;
1548         }
1549       else
1550         {
1551           /* Calling a non-const member function from a const member function
1552              is probably invalid, but for now we let it only draw a warning.
1553              We indicate that such a mismatch has occurred by setting the
1554              harshness to a maximum value.  */
1555           if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1556               && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1557             cp->v.ansi_harshness[0].code |= CONST_CODE;
1558         }
1559     }
1560
1561   if (evil_strikes)
1562     cp->h.code = EVIL_CODE;
1563   if (ellipsis_strikes)
1564     cp->h.code |= ELLIPSIS_CODE;
1565   if (user_strikes)
1566     cp->h.code |= USER_CODE;
1567 #ifdef DEBUG_MATCHING
1568   cp_error ("final eval %s", print_harshness (&cp->h));
1569 #endif
1570 }
1571
1572 void
1573 compute_conversion_costs_old (function, tta_in, cp, arglen)
1574      tree function;
1575      tree tta_in;
1576      struct candidate *cp;
1577      int arglen;
1578 {
1579   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
1580   tree ttf = ttf_in;
1581   tree tta = tta_in;
1582
1583   /* Start out with no strikes against.  */
1584   int evil_strikes = 0;
1585   int ellipsis_strikes = 0;
1586   int user_strikes = 0;
1587   int b_or_d_strikes = 0;
1588   int easy_strikes = 0;
1589
1590   int strike_index = 0, win, lose;
1591
1592 #ifdef GATHER_STATISTICS
1593   n_compute_conversion_costs++;
1594 #endif
1595
1596   cp->function = function;
1597   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
1598   cp->u.bad_arg = 0;            /* optimistic!  */
1599
1600   bzero (cp->v.old_harshness, (cp->h_len + 1) * sizeof (unsigned short));
1601
1602   while (ttf && tta)
1603     {
1604       int harshness;
1605
1606       if (ttf == void_list_node)
1607         break;
1608
1609       if (type_unknown_p (TREE_VALUE (tta)))
1610         {         
1611           /* Must perform some instantiation here.  */
1612           tree rhs = TREE_VALUE (tta);
1613           tree lhstype = TREE_VALUE (ttf);
1614
1615           /* Keep quiet about possible contravariance violations.  */
1616           int old_inhibit_warnings = inhibit_warnings;
1617           inhibit_warnings = 1;
1618
1619           /* @@ This is to undo what `grokdeclarator' does to
1620              parameter types.  It really should go through
1621              something more general.  */
1622
1623           TREE_TYPE (tta) = unknown_type_node;
1624           rhs = instantiate_type (lhstype, rhs, 0);
1625           inhibit_warnings = old_inhibit_warnings;
1626
1627           if (TREE_CODE (rhs) == ERROR_MARK)
1628             harshness = 1;
1629           else
1630             {
1631               harshness = convert_harshness_old (lhstype, TREE_TYPE (rhs),
1632                                                  rhs);
1633               /* harshness |= 2; */
1634             }
1635         }
1636       else
1637         harshness = convert_harshness_old (TREE_VALUE (ttf),
1638                                            TREE_TYPE (TREE_VALUE (tta)),
1639                                            TREE_VALUE (tta));
1640
1641       cp->v.old_harshness[strike_index] = harshness;
1642       if (EVIL_HARSHNESS (harshness)
1643           || CONTRAVARIANT_HARSHNESS (harshness))
1644         {
1645           cp->u.bad_arg = strike_index;
1646           evil_strikes = 1;
1647         }
1648      else if (ELLIPSIS_HARSHNESS (harshness))
1649         {
1650           ellipsis_strikes += 1;
1651         }
1652 #if 0
1653       /* This is never set by `convert_harshness_old'.  */
1654       else if (USER_HARSHNESS (harshness))
1655         {
1656           user_strikes += 1;
1657         }
1658 #endif
1659       else if (BASE_DERIVED_HARSHNESS (harshness))
1660         {
1661           b_or_d_strikes += INT_FROM_BD_HARSHNESS (harshness);
1662         }
1663       else
1664         easy_strikes += INT_FROM_EASY_HARSHNESS (harshness);
1665       ttf = TREE_CHAIN (ttf);
1666       tta = TREE_CHAIN (tta);
1667       strike_index += 1;
1668     }
1669
1670   if (tta)
1671     {
1672       /* ran out of formals, and parmlist is fixed size.  */
1673       if (ttf /* == void_type_node */)
1674         {
1675           cp->evil = 1;
1676           cp->u.bad_arg = -1;
1677           return;
1678         }
1679       else ellipsis_strikes += list_length (tta);
1680     }
1681   else if (ttf && ttf != void_list_node)
1682     {
1683       /* ran out of actuals, and no defaults.  */
1684       if (TREE_PURPOSE (ttf) == NULL_TREE)
1685         {
1686           cp->evil = 1;
1687           cp->u.bad_arg = -2;
1688           return;
1689         }
1690       /* Store index of first default.  */
1691       cp->v.old_harshness[arglen] = strike_index+1;
1692     }
1693   else
1694     cp->v.old_harshness[arglen] = 0;
1695
1696   /* Argument list lengths work out, so don't need to check them again.  */
1697   if (evil_strikes)
1698     {
1699       /* We do not check for derived->base conversions here, since in
1700          no case would they give evil strike counts, unless such conversions
1701          are somehow ambiguous.  */
1702
1703       /* See if any user-defined conversions apply.
1704          But make sure that we do not loop.  */
1705       static int dont_convert_types = 0;
1706
1707       if (dont_convert_types)
1708         {
1709           cp->evil = 1;
1710           return;
1711         }
1712
1713       win = 0;                  /* Only get one chance to win.  */
1714       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
1715       tta = tta_in;
1716       strike_index = 0;
1717       evil_strikes = 0;
1718
1719       while (ttf && tta)
1720         {
1721           if (ttf == void_list_node)
1722             break;
1723
1724           lose = cp->v.old_harshness[strike_index];
1725           if (EVIL_HARSHNESS (lose)
1726               || CONTRAVARIANT_HARSHNESS (lose))
1727             {
1728               tree actual_type = TREE_TYPE (TREE_VALUE (tta));
1729               tree formal_type = TREE_VALUE (ttf);
1730
1731               dont_convert_types = 1;
1732
1733               if (TREE_CODE (formal_type) == REFERENCE_TYPE)
1734                 formal_type = TREE_TYPE (formal_type);
1735               if (TREE_CODE (actual_type) == REFERENCE_TYPE)
1736                 actual_type = TREE_TYPE (actual_type);
1737
1738               if (formal_type != error_mark_node
1739                   && actual_type != error_mark_node)
1740                 {
1741                   formal_type = TYPE_MAIN_VARIANT (formal_type);
1742                   actual_type = TYPE_MAIN_VARIANT (actual_type);
1743
1744                   if (TYPE_HAS_CONSTRUCTOR (formal_type))
1745                     {
1746                       /* If it has a constructor for this type, try to use it.  */
1747                       if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
1748                           != error_mark_node)
1749                         {
1750                           /* @@ There is no way to save this result yet.
1751                              @@ So success is NULL_TREE for now.  */
1752                           win++;
1753                         }
1754                     }
1755                   if (TYPE_LANG_SPECIFIC (actual_type) && TYPE_HAS_CONVERSION (actual_type))
1756                     {
1757                       if (TREE_CODE (formal_type) == INTEGER_TYPE
1758                           && TYPE_HAS_INT_CONVERSION (actual_type))
1759                         win++;
1760                       else if (TREE_CODE (formal_type) == REAL_TYPE
1761                                && TYPE_HAS_REAL_CONVERSION (actual_type))
1762                         win++;
1763                       else
1764                         {
1765                           tree conv = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
1766                           if (conv)
1767                             {
1768                               if (conv == error_mark_node)
1769                                 win += 2;
1770                               else
1771                                 win++;
1772                             }
1773                           else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
1774                             {
1775                               conv = build_type_conversion (CALL_EXPR, formal_type, TREE_VALUE (tta), 0);
1776                               if (conv)
1777                                 {
1778                                   if (conv == error_mark_node)
1779                                     win += 2;
1780                                   else
1781                                     win++;
1782                                 }
1783                             }
1784                         }
1785                     }
1786                 }
1787               dont_convert_types = 0;
1788
1789               if (win == 1)
1790                 {
1791                   user_strikes += 1;
1792                   cp->v.old_harshness[strike_index] = USER_HARSHNESS (-1);
1793                   win = 0;
1794                 }
1795               else
1796                 {
1797                   if (cp->u.bad_arg > strike_index)
1798                     cp->u.bad_arg = strike_index;
1799
1800                   evil_strikes = win ? 2 : 1;
1801                   break;
1802                 }
1803             }
1804
1805           ttf = TREE_CHAIN (ttf);
1806           tta = TREE_CHAIN (tta);
1807           strike_index += 1;
1808         }
1809     }
1810
1811   /* Const member functions get a small penalty because defaulting
1812      to const is less useful than defaulting to non-const. */
1813   /* This is bogus, it does not correspond to anything in the ARM.
1814      This code will be fixed when this entire section is rewritten
1815      to conform to the ARM.  (mrs)  */
1816   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1817     {
1818       tree this_parm = TREE_VALUE (ttf_in);
1819
1820       if (TREE_CODE (this_parm) == RECORD_TYPE  /* Is `this' a sig ptr?  */
1821             ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
1822             : TYPE_READONLY (TREE_TYPE (this_parm)))
1823         {
1824           cp->v.old_harshness[0] += INT_TO_EASY_HARSHNESS (1);
1825           ++easy_strikes;
1826         }
1827       else
1828         {
1829           /* Calling a non-const member function from a const member function
1830              is probably invalid, but for now we let it only draw a warning.
1831              We indicate that such a mismatch has occurred by setting the
1832              harshness to a maximum value.  */
1833           if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1834               && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1835             cp->v.old_harshness[0] |= CONST_HARSHNESS (-1);
1836         }
1837     }
1838
1839   cp->evil = evil_strikes;
1840   cp->ellipsis = ellipsis_strikes;
1841   cp->user = user_strikes;
1842   cp->b_or_d = b_or_d_strikes;
1843   cp->easy = easy_strikes;
1844 }
1845
1846 void
1847 compute_conversion_costs (function, tta_in, cp, arglen)
1848      tree function;
1849      tree tta_in;
1850      struct candidate *cp;
1851      int arglen;
1852 {
1853   if (flag_ansi_overloading)
1854     compute_conversion_costs_ansi (function, tta_in, cp, arglen);
1855   else
1856     compute_conversion_costs_old (function, tta_in, cp, arglen);
1857 }
1858
1859 /* When one of several possible overloaded functions and/or methods
1860    can be called, choose the best candidate for overloading.
1861
1862    BASETYPE is the context from which we start method resolution
1863    or NULL if we are comparing overloaded functions.
1864    CANDIDATES is the array of candidates we have to choose from.
1865    N_CANDIDATES is the length of CANDIDATES.
1866    PARMS is a TREE_LIST of parameters to the function we'll ultimately
1867    choose.  It is modified in place when resolving methods.  It is not
1868    modified in place when resolving overloaded functions.
1869    LEN is the length of the parameter list.  */
1870
1871 static struct candidate *
1872 ideal_candidate_old (basetype, candidates, n_candidates, parms, len)
1873      tree basetype;
1874      struct candidate *candidates;
1875      int n_candidates;
1876      tree parms;
1877      int len;
1878 {
1879   struct candidate *cp = candidates + n_candidates;
1880   int index, i;
1881   tree ttf;
1882
1883   qsort (candidates,            /* char *base */
1884          n_candidates,          /* int nel */
1885          sizeof (struct candidate), /* int width */
1886          rank_for_overload);    /* int (*compar)() */
1887
1888   /* If the best candidate requires user-defined conversions,
1889      and its user-defined conversions are a strict subset
1890      of all other candidates requiring user-defined conversions,
1891      then it is, in fact, the best.  */
1892   for (i = -1; cp + i != candidates; i--)
1893     if (cp[i].user == 0)
1894       break;
1895
1896   if (i < -1)
1897     {
1898       tree ttf0;
1899
1900       /* Check that every other candidate requires those conversions
1901          as a strict subset of their conversions.  */
1902       if (cp[i].user == cp[-1].user)
1903         goto non_subset;
1904
1905       /* Look at subset relationship more closely.  */
1906       while (i != -1)
1907         {
1908           for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)),
1909                ttf0 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function)),
1910                index = 0; index < len; index++)
1911             {
1912               if (USER_HARSHNESS (cp[i].v.old_harshness[index]))
1913                 {
1914                   /* If our "best" candidate also needs a conversion,
1915                      it must be the same one.  */
1916                   if (USER_HARSHNESS (cp[-1].v.old_harshness[index])
1917                       && TREE_VALUE (ttf) != TREE_VALUE (ttf0))
1918                     goto non_subset;
1919                 }
1920               ttf = TREE_CHAIN (ttf);
1921               ttf0 = TREE_CHAIN (ttf0);
1922               /* Handle `...' gracefully.  */
1923               if (ttf == NULL_TREE || ttf0 == NULL_TREE)
1924                 break;
1925             }
1926           i++;
1927         }
1928       /* The best was the best.  */
1929       return cp - 1;
1930     non_subset:
1931       /* Use other rules for determining "bestness".  */
1932       ;
1933     }
1934
1935   /* If the best two candidates we find require user-defined
1936      conversions, we may need to report and error message.  */
1937   if (cp[-1].user && cp[-2].user
1938       && (cp[-1].b_or_d || cp[-2].b_or_d == 0))
1939     {
1940       /* If the best two methods found involved user-defined
1941          type conversions, then we must see whether one
1942          of them is exactly what we wanted.  If not, then
1943          we have an ambiguity.  */
1944       int best = 0;
1945       tree tta = parms;
1946       tree f1;
1947 #if 0
1948       /* for LUCID */
1949       tree p1;
1950 #endif
1951
1952       /* Stash all of our parameters in safe places
1953          so that we can perform type conversions in place.  */
1954       while (tta)
1955         {
1956           TREE_PURPOSE (tta) = TREE_VALUE (tta);
1957           tta = TREE_CHAIN (tta);
1958         }
1959
1960       i = 0;
1961       do
1962         {
1963           int exact_conversions = 0;
1964
1965           i -= 1;
1966           tta = parms;
1967           if (DECL_STATIC_FUNCTION_P (cp[i].function))
1968             tta = TREE_CHAIN (tta);
1969           /* special note, we don't go through len parameters, because we
1970              may only need len-1 parameters because of a call to a static
1971              member. */
1972           for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)), index = 0;
1973                tta;
1974                tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
1975             {
1976               /* If this is a varargs function, there's no conversion to do,
1977                  but don't accept an arg that needs a copy ctor.  */
1978               if (ttf == NULL_TREE)
1979                 {
1980                   /* FIXME: verify that we cannot get here with an
1981                      arg that needs a ctor.  */
1982                   break;
1983                 }
1984
1985               if (USER_HARSHNESS (cp[i].v.old_harshness[index]))
1986                 {
1987                   tree this_parm = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_PURPOSE (tta), 2);
1988                   if (basetype != NULL_TREE)
1989                     TREE_VALUE (tta) = this_parm;
1990                   if (this_parm)
1991                     {
1992                       if (TREE_CODE (this_parm) != CONVERT_EXPR
1993                           && (TREE_CODE (this_parm) != NOP_EXPR
1994                               || comp_target_types (TREE_TYPE (this_parm),
1995                                                     TREE_TYPE (TREE_OPERAND (this_parm, 0)), 1)))
1996                         exact_conversions += 1;
1997                     }
1998                   else if (PROMOTES_TO_AGGR_TYPE (TREE_VALUE (ttf), REFERENCE_TYPE))
1999                     {
2000                       /* To get here we had to have succeeded via
2001                          a constructor.  */
2002                       TREE_VALUE (tta) = TREE_PURPOSE (tta);
2003                       exact_conversions += 1;
2004                     }
2005                 }
2006             }
2007           if (exact_conversions == cp[i].user)
2008             {
2009               if (best == 0)
2010                 {
2011                   best = i;
2012                   f1 = cp[best].function;
2013 #if 0
2014                   /* For LUCID */
2015                   p1 = TYPE_ARG_TYPES (TREE_TYPE (f1));
2016 #endif
2017                 }
2018               else
2019                 {
2020                   /* Don't complain if next best is from base class.  */
2021                   tree f2 = cp[i].function;
2022
2023                   if (TREE_CODE (TREE_TYPE (f1)) == METHOD_TYPE
2024                       && TREE_CODE (TREE_TYPE (f2)) == METHOD_TYPE
2025                       && BASE_DERIVED_HARSHNESS (cp[i].v.old_harshness[0])
2026                       && cp[best].v.old_harshness[0] < cp[i].v.old_harshness[0])
2027                     {
2028 #if 0
2029                       tree p2 = TYPE_ARG_TYPES (TREE_TYPE (f2));
2030                       /* For LUCID.  */
2031                       if (! compparms (TREE_CHAIN (p1), TREE_CHAIN (p2), 1))
2032                         goto ret0;
2033                       else
2034 #endif
2035                         continue;
2036                     }
2037                   else
2038                     {
2039                       /* Ensure that there's nothing ambiguous about these
2040                          two fns.  */
2041                       int identical = 1;
2042                       for (index = 0; index < len; index++)
2043                         {
2044                           /* Type conversions must be piecewise equivalent.  */
2045                           if (USER_HARSHNESS (cp[best].v.old_harshness[index])
2046                               != USER_HARSHNESS (cp[i].v.old_harshness[index]))
2047                             goto ret0;
2048                           /* If there's anything we like better about the
2049                              other function, consider it ambiguous.  */
2050                           if (cp[i].v.old_harshness[index] < cp[best].v.old_harshness[index])
2051                             goto ret0;
2052                           /* If any single one it diffent, then the whole is
2053                              not identical.  */
2054                           if (cp[i].v.old_harshness[index] != cp[best].v.old_harshness[index])
2055                             identical = 0;
2056                         }
2057
2058                       /* If we can't tell the difference between the two, it
2059                          is ambiguous.  */
2060                       if (identical)
2061                         goto ret0;
2062
2063                       /* If we made it to here, it means we're satisfied that
2064                          BEST is still best.  */
2065                       continue;
2066                     }
2067                 }
2068             }
2069         } while (cp + i != candidates);
2070
2071       if (best)
2072         {
2073           int exact_conversions = cp[best].user;
2074           tta = parms;
2075           if (DECL_STATIC_FUNCTION_P (cp[best].function))
2076             tta = TREE_CHAIN (parms);
2077           for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[best].function)), index = 0;
2078                exact_conversions > 0;
2079                tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
2080             {
2081               if (USER_HARSHNESS (cp[best].v.old_harshness[index]))
2082                 {
2083                   /* We must now fill in the slot we left behind.
2084                      @@ This could be optimized to use the value previously
2085                      @@ computed by build_type_conversion in some cases.  */
2086                   if (basetype != NULL_TREE)
2087                     TREE_VALUE (tta) = convert (TREE_VALUE (ttf), TREE_PURPOSE (tta));
2088                   exact_conversions -= 1;
2089                 }
2090               else
2091                 TREE_VALUE (tta) = TREE_PURPOSE (tta);
2092             }
2093           return cp + best;
2094         }
2095       goto ret0;
2096     }
2097   /* If the best two candidates we find both use default parameters,
2098      we may need to report and error.  Don't need to worry if next-best
2099      candidate is forced to use user-defined conversion when best is not.  */
2100   if (cp[-2].user == 0
2101       && cp[-1].v.old_harshness[len] != 0 && cp[-2].v.old_harshness[len] != 0)
2102     {
2103       tree tt1 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function));
2104       tree tt2 = TYPE_ARG_TYPES (TREE_TYPE (cp[-2].function));
2105       unsigned i = cp[-1].v.old_harshness[len];
2106
2107       if (cp[-2].v.old_harshness[len] < i)
2108         i = cp[-2].v.old_harshness[len];
2109       while (--i > 0)
2110         {
2111           if (TYPE_MAIN_VARIANT (TREE_VALUE (tt1))
2112               != TYPE_MAIN_VARIANT (TREE_VALUE (tt2)))
2113             /* These lists are not identical, so we can choose our best candidate.  */
2114             return cp - 1;
2115           tt1 = TREE_CHAIN (tt1);
2116           tt2 = TREE_CHAIN (tt2);
2117         }
2118       /* To get here, both lists had the same parameters up to the defaults
2119          which were used.  This is an ambiguous request.  */
2120       goto ret0;
2121     }
2122
2123   /* Otherwise, return our best candidate.  Note that if we get candidates
2124      from independent base classes, we have an ambiguity, even if one
2125      argument list look a little better than another one.  */
2126   if (cp[-1].b_or_d && basetype && TYPE_USES_MULTIPLE_INHERITANCE (basetype))
2127     {
2128       int i = n_candidates - 1, best = i;
2129       tree base1 = NULL_TREE;
2130
2131       if (TREE_CODE (TREE_TYPE (candidates[i].function)) == FUNCTION_TYPE)
2132         return cp - 1;
2133
2134       for (; i >= 0 && candidates[i].user == 0 && candidates[i].evil == 0; i--)
2135         {
2136           if (TREE_CODE (TREE_TYPE (candidates[i].function)) == METHOD_TYPE)
2137             {
2138               tree newbase = DECL_CLASS_CONTEXT (candidates[i].function);
2139
2140               if (base1 != NULL_TREE)
2141                 {
2142                   /* newbase could be a base or a parent of base1 */
2143                   if (newbase != base1 && ! UNIQUELY_DERIVED_FROM_P (newbase, base1)
2144                       && ! UNIQUELY_DERIVED_FROM_P (base1, newbase))
2145                     {
2146                       cp_error ("ambiguous request for function from distinct base classes of type `%T'", basetype);
2147                       cp_error_at ("  first candidate is `%#D'",
2148                                      candidates[best].function);
2149                       cp_error_at ("  second candidate is `%#D'",
2150                                      candidates[i].function);
2151                       cp[-1].evil = 1;
2152                       return cp - 1;
2153                     }
2154                 }
2155               else
2156                 {
2157                   best = i;
2158                   base1 = newbase;
2159                 }
2160             }
2161           else
2162             return cp - 1;
2163         }
2164     }
2165
2166   /* Don't accept a candidate as being ideal if it's indistinguishable
2167      from another candidate.  */
2168   if (rank_for_overload (cp-1, cp-2) == 0)
2169     {
2170       /* If the types are distinguishably different (like
2171          `long' vs. `unsigned long'), that's ok.  But if they are arbitrarily
2172          different, such as `int (*)(void)' vs. `void (*)(int)',
2173          that's not ok.  */
2174       tree p1 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function));
2175       tree p2 = TYPE_ARG_TYPES (TREE_TYPE (cp[-2].function));
2176       while (p1 && p2)
2177         {
2178           if (TREE_CODE (TREE_VALUE (p1)) == POINTER_TYPE
2179               && TREE_CODE (TREE_TYPE (TREE_VALUE (p1))) == FUNCTION_TYPE
2180               && TREE_VALUE (p1) != TREE_VALUE (p2))
2181             return NULL;
2182           p1 = TREE_CHAIN (p1);
2183           p2 = TREE_CHAIN (p2);
2184         }
2185       if (p1 || p2)
2186         return NULL;
2187     }
2188
2189   return cp - 1;
2190
2191  ret0:
2192   /* In the case where there is no ideal candidate, restore
2193      TREE_VALUE slots of PARMS from TREE_PURPOSE slots.  */
2194   while (parms)
2195     {
2196       TREE_VALUE (parms) = TREE_PURPOSE (parms);
2197       parms = TREE_CHAIN (parms);
2198     }
2199   return NULL;
2200 }
2201
2202 /* Subroutine of ideal_candidate.  See if X or Y is a better match
2203    than the other.  */
2204 static int
2205 strictly_better (x, y)
2206      unsigned short x, y;
2207 {
2208   unsigned short xor;
2209
2210   if (x == y)
2211     return 0;
2212
2213   xor = x ^ y;
2214   if (xor >= x || xor >= y)
2215     return 1;
2216   return 0;
2217 }
2218
2219 static struct candidate *
2220 ideal_candidate_ansi (basetype, candidates, n_candidates, parms, len)
2221      tree basetype;
2222      struct candidate *candidates;
2223      int n_candidates;
2224      tree parms;
2225      int len;
2226 {
2227   struct candidate *cp = candidates+n_candidates;
2228   int i, j = -1, best_code;
2229
2230   /* For each argument, sort the functions from best to worst for the arg.
2231      For each function that's not best for this arg, set its overall
2232      harshness to EVIL so that other args won't like it.  The candidate
2233      list for the last argument is the intersection of all the best-liked
2234      functions.  */
2235
2236 #if 0
2237   for (i = 0; i < len; i++)
2238     {
2239       qsort (candidates, n_candidates, sizeof (struct candidate),
2240              rank_for_overload);
2241       best_code = cp[-1].h.code;
2242
2243       /* To find out functions that are worse than that represented
2244          by BEST_CODE, we can't just do a comparison like h.code>best_code.
2245          The total harshness for the "best" fn may be 8|8 for two args, and
2246          the harshness for the next-best may be 8|2.  If we just compared,
2247          that would be checking 8>10, which would lead to the next-best
2248          being disqualified.  What we actually want to do is get rid
2249          of functions that are definitely worse than that represented
2250          by best_code, i.e. those which have bits set higher than the
2251          highest in best_code.  Sooooo, what we do is clear out everything
2252          represented by best_code, and see if we still come up with something
2253          higher.  If so (e.g., 8|8 vs 8|16), it'll disqualify it properly.  */
2254       for (j = n_candidates-2; j >= 0; j--)
2255         if ((candidates[j].h.code & ~best_code) > best_code)
2256           candidates[j].h.code = EVIL_CODE;
2257     }
2258
2259   if (cp[-1].h.code & EVIL_CODE)
2260     return NULL;
2261 #else
2262   qsort (candidates, n_candidates, sizeof (struct candidate),
2263          rank_for_overload);
2264   best_code = cp[-1].h.code;
2265 #endif
2266
2267   /* If they're at least as good as each other, do an arg-by-arg check.  */
2268   if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
2269     {
2270       int better = 0;
2271       int worse = 0;
2272
2273       for (j = 0; j < n_candidates; j++)
2274         if (! strictly_better (candidates[j].h.code, best_code))
2275           break;
2276
2277       qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
2278              rank_for_ideal);
2279       for (i = 0; i < len; i++)
2280         {
2281           if (cp[-1].v.ansi_harshness[i].code < cp[-2].v.ansi_harshness[i].code)
2282             better = 1;
2283           else if (cp[-1].v.ansi_harshness[i].code > cp[-2].v.ansi_harshness[i].code)
2284             worse = 1;
2285           else if (cp[-1].v.ansi_harshness[i].code & STD_CODE)
2286             {
2287               /* If it involves a standard conversion, let the
2288                  inheritance lattice be the final arbiter.  */
2289               if (cp[-1].v.ansi_harshness[i].distance > cp[-2].v.ansi_harshness[i].distance)
2290                 worse = 1;
2291               else if (cp[-1].v.ansi_harshness[i].distance < cp[-2].v.ansi_harshness[i].distance)
2292                 better = 1;
2293             }
2294           else if (cp[-1].v.ansi_harshness[i].code & PROMO_CODE)
2295             {
2296               /* For integral promotions, take into account a finer
2297                  granularity for determining which types should be favored
2298                  over others in such promotions.  */
2299               if (cp[-1].v.ansi_harshness[i].int_penalty > cp[-2].v.ansi_harshness[i].int_penalty)
2300                 worse = 1;
2301               else if (cp[-1].v.ansi_harshness[i].int_penalty < cp[-2].v.ansi_harshness[i].int_penalty)
2302                 better = 1;
2303             }
2304         }
2305
2306       if (! better || worse)
2307         return NULL;
2308     }
2309   return cp-1;
2310 }
2311
2312 static struct candidate *
2313 ideal_candidate (basetype, candidates, n_candidates, parms, len)
2314      tree basetype;
2315      struct candidate *candidates;
2316      int n_candidates;
2317      tree parms;
2318      int len;
2319 {
2320   if (flag_ansi_overloading)
2321     return ideal_candidate_ansi (basetype, candidates, n_candidates, parms,
2322                                  len);
2323   else
2324     return ideal_candidate_old (basetype, candidates, n_candidates, parms,
2325                                 len);
2326 }
2327
2328 /* Assume that if the class referred to is not in the
2329    current class hierarchy, that it may be remote.
2330    PARENT is assumed to be of aggregate type here.  */
2331 static int
2332 may_be_remote (parent)
2333      tree parent;
2334 {
2335   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
2336     return 0;
2337
2338   if (current_class_type == NULL_TREE)
2339     return 0;
2340
2341   if (parent == current_class_type)
2342     return 0;
2343
2344   if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
2345     return 0;
2346   return 1;
2347 }
2348
2349 tree
2350 build_vfield_ref (datum, type)
2351      tree datum, type;
2352 {
2353   tree rval;
2354   int old_assume_nonnull_objects = flag_assume_nonnull_objects;
2355
2356   if (datum == error_mark_node)
2357     return error_mark_node;
2358
2359   /* Vtable references are always made from non-null objects.  */
2360   flag_assume_nonnull_objects = 1;
2361   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
2362     datum = convert_from_reference (datum);
2363
2364   if (! TYPE_USES_COMPLEX_INHERITANCE (type))
2365     rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
2366                   datum, CLASSTYPE_VFIELD (type));
2367   else
2368     rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
2369   flag_assume_nonnull_objects = old_assume_nonnull_objects;
2370
2371   return rval;
2372 }
2373
2374 /* Build a call to a member of an object.  I.e., one that overloads
2375    operator ()(), or is a pointer-to-function or pointer-to-method.  */
2376 static tree
2377 build_field_call (basetype_path, instance_ptr, name, parms)
2378      tree basetype_path, instance_ptr, name, parms;
2379 {
2380   tree field, instance;
2381
2382   if (instance_ptr == current_class_decl)
2383     {
2384       /* Check to see if we really have a reference to an instance variable
2385          with `operator()()' overloaded.  */
2386       field = IDENTIFIER_CLASS_VALUE (name);
2387
2388       if (field == NULL_TREE)
2389         {
2390           cp_error ("`this' has no member named `%D'", name);
2391           return error_mark_node;
2392         }
2393
2394       if (TREE_CODE (field) == FIELD_DECL)
2395         {
2396           /* If it's a field, try overloading operator (),
2397              or calling if the field is a pointer-to-function.  */
2398           instance = build_component_ref_1 (C_C_D, field, 0);
2399           if (instance == error_mark_node)
2400             return error_mark_node;
2401
2402           if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
2403               && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
2404             return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
2405
2406           if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
2407             {
2408               if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
2409                 return build_function_call (instance, parms);
2410               else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
2411                 return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
2412             }
2413         }
2414       return NULL_TREE;
2415     }
2416
2417   /* Check to see if this is not really a reference to an instance variable
2418      with `operator()()' overloaded.  */
2419   field = lookup_field (basetype_path, name, 1, 0);
2420
2421   /* This can happen if the reference was ambiguous or for access
2422      violations.  */
2423   if (field == error_mark_node)
2424     return error_mark_node;
2425
2426   if (field)
2427     {
2428       tree basetype;
2429       tree ftype = TREE_TYPE (field);
2430
2431       if (TREE_CODE (ftype) == REFERENCE_TYPE)
2432         ftype = TREE_TYPE (ftype);
2433
2434       if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
2435         {
2436           /* Make the next search for this field very short.  */
2437           basetype = DECL_FIELD_CONTEXT (field);
2438           instance_ptr = convert_pointer_to (basetype, instance_ptr);
2439
2440           instance = build_indirect_ref (instance_ptr, NULL_PTR);
2441           return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
2442                                  build_component_ref_1 (instance, field, 0),
2443                                  parms, NULL_TREE);
2444         }
2445       if (TREE_CODE (ftype) == POINTER_TYPE)
2446         {
2447           if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
2448               || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
2449             {
2450               /* This is a member which is a pointer to function.  */
2451               tree ref
2452                 = build_component_ref_1 (build_indirect_ref (instance_ptr,
2453                                                              NULL_PTR),
2454                                          field, LOOKUP_COMPLAIN);
2455               if (ref == error_mark_node)
2456                 return error_mark_node;
2457               return build_function_call (ref, parms);
2458             }
2459         }
2460       else if (TREE_CODE (ftype) == METHOD_TYPE)
2461         {
2462           error ("invalid call via pointer-to-member function");
2463           return error_mark_node;
2464         }
2465       else
2466         return NULL_TREE;
2467     }
2468   return NULL_TREE;
2469 }
2470
2471 tree
2472 find_scoped_type (type, inner_name, inner_types)
2473      tree type, inner_name, inner_types;
2474 {
2475   tree tags = CLASSTYPE_TAGS (type);
2476
2477   while (tags)
2478     {
2479       /* The TREE_PURPOSE of an enum tag (which becomes a member of the
2480          enclosing class) is set to the name for the enum type.  So, if
2481          inner_name is `bar', and we strike `baz' for `enum bar { baz }',
2482          then this test will be true.  */
2483       if (TREE_PURPOSE (tags) == inner_name)
2484         {
2485           if (inner_types == NULL_TREE)
2486             return DECL_NESTED_TYPENAME (TYPE_NAME (TREE_VALUE (tags)));
2487           return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
2488         }
2489       tags = TREE_CHAIN (tags);
2490     }
2491
2492 #if 0
2493   /* XXX This needs to be fixed better.  */
2494   if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
2495     {
2496       sorry ("nested class lookup in template type");
2497       return NULL_TREE;
2498     }
2499 #endif
2500
2501   /* Look for a TYPE_DECL.  */
2502   for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
2503     if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
2504       {
2505         /* Code by raeburn.  */
2506         if (inner_types == NULL_TREE)
2507           return DECL_NESTED_TYPENAME (tags);
2508         return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
2509       }
2510
2511   return NULL_TREE;
2512 }
2513
2514 /* Resolve an expression NAME1::NAME2::...::NAMEn to
2515    the name that names the above nested type.  INNER_TYPES
2516    is a chain of nested type names (held together by SCOPE_REFs);
2517    OUTER_TYPE is the type we know to enclose INNER_TYPES.
2518    Returns NULL_TREE if there is an error.  */
2519 tree
2520 resolve_scope_to_name (outer_type, inner_stuff)
2521      tree outer_type, inner_stuff;
2522 {
2523   register tree tmp;
2524   tree inner_name, inner_type;
2525
2526   if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
2527     {
2528       /* We first try to look for a nesting in our current class context,
2529          then try any enclosing classes.  */
2530       tree type = current_class_type;
2531       
2532       while (type && (TREE_CODE (type) == RECORD_TYPE
2533                       || TREE_CODE (type) == UNION_TYPE))
2534         {
2535           tree rval = resolve_scope_to_name (type, inner_stuff);
2536
2537           if (rval != NULL_TREE)
2538             return rval;
2539           type = DECL_CONTEXT (TYPE_NAME (type));
2540         }
2541     }
2542
2543   if (TREE_CODE (inner_stuff) == SCOPE_REF)
2544     {
2545       inner_name = TREE_OPERAND (inner_stuff, 0);
2546       inner_type = TREE_OPERAND (inner_stuff, 1);
2547     }
2548   else
2549     {
2550       inner_name = inner_stuff;
2551       inner_type = NULL_TREE;
2552     }
2553
2554   if (outer_type == NULL_TREE)
2555     {
2556       /* If we have something that's already a type by itself,
2557          use that.  */
2558       if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
2559         {
2560           if (inner_type)
2561             return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
2562                                           inner_type);
2563           return inner_name;
2564         }
2565       return NULL_TREE;
2566     }
2567
2568   if (! IS_AGGR_TYPE (outer_type))
2569     return NULL_TREE;
2570
2571   /* Look for member classes or enums.  */
2572   tmp = find_scoped_type (outer_type, inner_name, inner_type);
2573
2574   /* If it's not a type in this class, then go down into the
2575      base classes and search there.  */
2576   if (! tmp && TYPE_BINFO (outer_type))
2577     {
2578       tree binfos = TYPE_BINFO_BASETYPES (outer_type);
2579       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2580
2581       for (i = 0; i < n_baselinks; i++)
2582         {
2583           tree base_binfo = TREE_VEC_ELT (binfos, i);
2584           tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
2585           if (tmp)
2586             return tmp;
2587         }
2588       tmp = NULL_TREE;
2589     }
2590
2591   return tmp;
2592 }
2593
2594 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
2595    This is how virtual function calls are avoided.  */
2596 tree
2597 build_scoped_method_call (exp, scopes, name, parms)
2598      tree exp, scopes, name, parms;
2599 {
2600   /* Because this syntactic form does not allow
2601      a pointer to a base class to be `stolen',
2602      we need not protect the derived->base conversion
2603      that happens here.
2604      
2605      @@ But we do have to check access privileges later.  */
2606   tree basename = resolve_scope_to_name (NULL_TREE, scopes);
2607   tree basetype, binfo, decl;
2608   tree type = TREE_TYPE (exp);
2609
2610   if (type == error_mark_node
2611       || basename == NULL_TREE)
2612     return error_mark_node;
2613
2614   basetype = IDENTIFIER_TYPE_VALUE (basename);
2615
2616   if (TREE_CODE (type) == REFERENCE_TYPE)
2617     type = TREE_TYPE (type);
2618
2619   /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
2620      that explicit ~int is caught in the parser; this deals with typedefs
2621      and template parms.  */
2622   if (TREE_CODE (name) == BIT_NOT_EXPR && ! is_aggr_typedef (basename, 0))
2623     {
2624       if (type != basetype)
2625         cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
2626                   exp, basetype, type);
2627       name = IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0));
2628       if (basetype != name)
2629         cp_error ("qualified type `%T' does not match destructor type `%T'",
2630                   basetype, name);
2631       return void_zero_node;
2632     }
2633
2634   if (! is_aggr_typedef (basename, 1))
2635     return error_mark_node;
2636
2637   if (! IS_AGGR_TYPE (type))
2638     {
2639       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
2640                 exp, type);
2641       return error_mark_node;
2642     }
2643
2644   if ((binfo = binfo_or_else (basetype, type)))
2645     {
2646       if (binfo == error_mark_node)
2647         return error_mark_node;
2648       if (TREE_CODE (exp) == INDIRECT_REF)
2649         decl = build_indirect_ref (convert_pointer_to (binfo,
2650                                                        build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
2651       else
2652         decl = build_scoped_ref (exp, scopes);
2653
2654       /* Call to a destructor.  */
2655       if (TREE_CODE (name) == BIT_NOT_EXPR)
2656         {
2657           /* Explicit call to destructor.  */
2658           name = TREE_OPERAND (name, 0);
2659           if (name != constructor_name (TREE_TYPE (decl)))
2660             {
2661               cp_error
2662                 ("qualified type `%T' does not match destructor type `%T'",
2663                  TREE_TYPE (decl), name);
2664               return error_mark_node;
2665             }
2666           if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
2667             return void_zero_node;
2668           
2669           return build_delete (TREE_TYPE (decl), decl, integer_two_node,
2670                                LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
2671                                0);
2672         }
2673
2674       /* Call to a method.  */
2675       return build_method_call (decl, name, parms, binfo,
2676                                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
2677     }
2678   return error_mark_node;
2679 }
2680
2681 static void
2682 print_candidates (candidates)
2683      tree candidates;
2684 {
2685   cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
2686   candidates = TREE_CHAIN (candidates);
2687
2688   while (candidates)
2689     {
2690       cp_error_at ("                %D", TREE_VALUE (candidates));
2691       candidates = TREE_CHAIN (candidates);
2692     }
2693 }
2694
2695 static void
2696 print_n_candidates (candidates, n)
2697      struct candidate *candidates;
2698      int n;
2699 {
2700   int i;
2701
2702   cp_error_at ("candidates are: %D", candidates[0].function);
2703   for (i = 1; i < n; i++)
2704     cp_error_at ("                %D", candidates[i].function);
2705 }
2706
2707 /* Build something of the form ptr->method (args)
2708    or object.method (args).  This can also build
2709    calls to constructors, and find friends.
2710
2711    Member functions always take their class variable
2712    as a pointer.
2713
2714    INSTANCE is a class instance.
2715
2716    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
2717
2718    PARMS help to figure out what that NAME really refers to.
2719
2720    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
2721    down to the real instance type to use for access checking.  We need this
2722    information to get protected accesses correct.  This parameter is used
2723    by build_member_call.
2724
2725    FLAGS is the logical disjunction of zero or more LOOKUP_
2726    flags.  See cp-tree.h for more info.
2727
2728    If this is all OK, calls build_function_call with the resolved
2729    member function.
2730
2731    This function must also handle being called to perform
2732    initialization, promotion/coercion of arguments, and
2733    instantiation of default parameters.
2734
2735    Note that NAME may refer to an instance variable name.  If
2736    `operator()()' is defined for the type of that field, then we return
2737    that result.  */
2738 tree
2739 build_method_call (instance, name, parms, basetype_path, flags)
2740      tree instance, name, parms, basetype_path;
2741      int flags;
2742 {
2743   register tree function, fntype, value_type;
2744   register tree basetype, save_basetype;
2745   register tree baselink, result, method_name, parmtypes, parm;
2746   tree last;
2747   int pass;
2748   enum access_type access = access_public;
2749
2750   /* Range of cases for vtable optimization.  */
2751   enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
2752   enum vtable_needs need_vtbl = not_needed;
2753
2754   char *name_kind;
2755   int ever_seen = 0;
2756   tree instance_ptr = NULL_TREE;
2757   int all_virtual = flag_all_virtual;
2758   int static_call_context = 0;
2759   tree found_fns = NULL_TREE;
2760
2761   /* Keep track of `const' and `volatile' objects.  */
2762   int constp, volatilep;
2763
2764 #ifdef GATHER_STATISTICS
2765   n_build_method_call++;
2766 #endif
2767
2768   if (instance == error_mark_node
2769       || name == error_mark_node
2770       || parms == error_mark_node
2771       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
2772     return error_mark_node;
2773
2774   /* This is the logic that magically deletes the second argument to
2775      operator delete, if it is not needed. */
2776   if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
2777     {
2778       tree save_last = TREE_CHAIN (parms);
2779       tree result;
2780       /* get rid of unneeded argument */
2781       TREE_CHAIN (parms) = NULL_TREE;
2782       result = build_method_call (instance, name, parms, basetype_path,
2783                                   (LOOKUP_SPECULATIVELY|flags)
2784                                   &~LOOKUP_COMPLAIN);
2785       /* If it works, return it. */
2786       if (result && result != error_mark_node)
2787         return build_method_call (instance, name, parms, basetype_path, flags);
2788       /* If it doesn't work, two argument delete must work */
2789       TREE_CHAIN (parms) = save_last;
2790     }
2791   /* We already know whether it's needed or not for vec delete.  */
2792   else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
2793            && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
2794     TREE_CHAIN (parms) = NULL_TREE;
2795
2796   if (TREE_CODE (name) == BIT_NOT_EXPR)
2797     {
2798       flags |= LOOKUP_DESTRUCTOR;
2799       name = TREE_OPERAND (name, 0);
2800       if (parms)
2801         error ("destructors take no parameters");
2802       basetype = get_type_value (name);
2803       if (basetype == NULL_TREE)
2804         {
2805           cp_error ("call to destructor for non-type `%D'", name);
2806           return void_zero_node;
2807         }
2808       if (basetype != TREE_TYPE(instance))
2809         basetype = TREE_TYPE(instance);
2810       if (! TYPE_HAS_DESTRUCTOR (basetype))
2811         return void_zero_node;
2812       instance = default_conversion (instance);
2813       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
2814       return build_delete (build_pointer_type (basetype),
2815                            instance_ptr, integer_two_node,
2816                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
2817     }
2818
2819   {
2820     char *xref_name;
2821     
2822     /* Initialize name for error reporting.  */
2823     if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
2824       {
2825         char *p = operator_name_string (name);
2826         xref_name = (char *)alloca (strlen (p) + 10);
2827         sprintf (xref_name, "operator %s", p);
2828       }
2829     else if (TREE_CODE (name) == SCOPE_REF)
2830       xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
2831     else
2832       xref_name = IDENTIFIER_POINTER (name);
2833
2834     GNU_xref_call (current_function_decl, xref_name);
2835   }
2836
2837   if (instance == NULL_TREE)
2838     {
2839       basetype = NULL_TREE;
2840       /* Check cases where this is really a call to raise
2841          an exception.  */
2842       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
2843         {
2844           basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
2845           if (basetype)
2846             basetype = TREE_VALUE (basetype);
2847         }
2848       else if (TREE_CODE (name) == SCOPE_REF
2849                && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
2850         {
2851           if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
2852             return error_mark_node;
2853           basetype = purpose_member (TREE_OPERAND (name, 1),
2854                                      CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
2855           if (basetype)
2856             basetype = TREE_VALUE (basetype);
2857         }
2858
2859       if (basetype != NULL_TREE)
2860         ;
2861       /* call to a constructor... */
2862       else if (basetype_path)
2863         basetype = BINFO_TYPE (basetype_path);
2864       else if (IDENTIFIER_HAS_TYPE_VALUE (name))
2865         {
2866           basetype = IDENTIFIER_TYPE_VALUE (name);
2867           name = constructor_name_full (basetype);
2868         }
2869       else
2870         {
2871           tree typedef_name = lookup_name (name, 1);
2872           if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
2873             {
2874               /* Canonicalize the typedef name.  */
2875               basetype = TREE_TYPE (typedef_name);
2876               name = TYPE_IDENTIFIER (basetype);
2877             }
2878           else
2879             {
2880               cp_error ("no constructor named `%T' in scope",
2881                         name);
2882               return error_mark_node;
2883             }
2884         }
2885
2886       if (! IS_AGGR_TYPE (basetype))
2887         {
2888         non_aggr_error:
2889           if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
2890             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2891                       name, instance, basetype);
2892
2893           return error_mark_node;
2894         }
2895     }
2896   else if (instance == C_C_D || instance == current_class_decl)
2897     {
2898       /* When doing initialization, we side-effect the TREE_TYPE of
2899          C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
2900       basetype = TREE_TYPE (C_C_D);
2901
2902       /* Anything manifestly `this' in constructors and destructors
2903          has a known type, so virtual function tables are not needed.  */
2904       if (TYPE_VIRTUAL_P (basetype)
2905           && !(flags & LOOKUP_NONVIRTUAL))
2906         need_vtbl = (dtor_label || ctor_label)
2907           ? unneeded : maybe_needed;
2908
2909       instance = C_C_D;
2910       instance_ptr = current_class_decl;
2911       result = build_field_call (TYPE_BINFO (current_class_type),
2912                                  instance_ptr, name, parms);
2913
2914       if (result)
2915         return result;
2916     }
2917   else if (TREE_CODE (instance) == RESULT_DECL)
2918     {
2919       basetype = TREE_TYPE (instance);
2920       /* Should we ever have to make a virtual function reference
2921          from a RESULT_DECL, know that it must be of fixed type
2922          within the scope of this function.  */
2923       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
2924         need_vtbl = maybe_needed;
2925       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
2926     }
2927   else
2928     {
2929       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
2930       tree inst_ptr_basetype;
2931
2932       static_call_context =
2933         (TREE_CODE (instance) == INDIRECT_REF
2934          && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
2935          && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
2936
2937       /* the base type of an instance variable is pointer to class */
2938       basetype = TREE_TYPE (instance);
2939
2940       if (TREE_CODE (basetype) == REFERENCE_TYPE)
2941         {
2942           basetype = TREE_TYPE (basetype);
2943           if (! IS_AGGR_TYPE (basetype))
2944             goto non_aggr_error;
2945           /* Call to convert not needed because we are remaining
2946              within the same type.  */
2947           instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
2948                                  instance);
2949           inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
2950         }
2951       else
2952         {
2953           if (! IS_AGGR_TYPE (basetype))
2954             goto non_aggr_error;
2955
2956           if (IS_SIGNATURE_POINTER (basetype)
2957               || IS_SIGNATURE_REFERENCE (basetype))
2958             basetype = SIGNATURE_TYPE (basetype);
2959
2960           if ((IS_SIGNATURE (basetype)
2961                && (instance_ptr = build_optr_ref (instance)))
2962               || (lvalue_p (instance)
2963                && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
2964               || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
2965             {
2966               if (instance_ptr == error_mark_node)
2967                 return error_mark_node;
2968             }
2969           else if (TREE_CODE (instance) == NOP_EXPR
2970                    || TREE_CODE (instance) == CONSTRUCTOR)
2971             {
2972               /* A cast is not an lvalue.  Initialize a fresh temp
2973                  with the value we are casting from, and proceed with
2974                  that temporary.  We can't cast to a reference type,
2975                  so that simplifies the initialization to something
2976                  we can manage.  */
2977               tree temp = get_temp_name (TREE_TYPE (instance), 0);
2978               if (IS_AGGR_TYPE (TREE_TYPE (instance)))
2979                 expand_aggr_init (temp, instance, 0);
2980               else
2981                 {
2982                   store_init_value (temp, instance);
2983                   expand_decl_init (temp);
2984                 }
2985               instance = temp;
2986               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
2987             }
2988           else
2989             {
2990               if (TREE_CODE (instance) != CALL_EXPR)
2991                 my_friendly_abort (125);
2992               if (TYPE_NEEDS_CONSTRUCTING (basetype))
2993                 instance = build_cplus_new (basetype, instance, 0);
2994               else
2995                 {
2996                   instance = get_temp_name (basetype, 0);
2997                   TREE_ADDRESSABLE (instance) = 1;
2998                 }
2999               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
3000             }
3001           /* @@ Should we call comp_target_types here?  */
3002           inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
3003           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
3004             basetype = inst_ptr_basetype;
3005           else
3006             {
3007               instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
3008               if (instance_ptr == error_mark_node)
3009                 return error_mark_node;
3010             }
3011         }
3012
3013       /* After converting `instance_ptr' above, `inst_ptr_basetype' was
3014          not updated, so we use `basetype' instead.  */
3015       if (basetype_path == NULL_TREE
3016           && IS_SIGNATURE (basetype))
3017         basetype_path = TYPE_BINFO (basetype);
3018       else if (basetype_path == NULL_TREE ||
3019         BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
3020         basetype_path = TYPE_BINFO (inst_ptr_basetype);
3021
3022       result = build_field_call (basetype_path, instance_ptr, name, parms);
3023       if (result)
3024         return result;
3025
3026       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
3027         {
3028           if (TREE_SIDE_EFFECTS (instance_ptr))
3029             {
3030               /* This action is needed because the instance is needed
3031                  for providing the base of the virtual function table.
3032                  Without using a SAVE_EXPR, the function we are building
3033                  may be called twice, or side effects on the instance
3034                  variable (such as a post-increment), may happen twice.  */
3035               instance_ptr = save_expr (instance_ptr);
3036               instance = build_indirect_ref (instance_ptr, NULL_PTR);
3037             }
3038           else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
3039             {
3040               /* This happens when called for operator new ().  */
3041               instance = build_indirect_ref (instance, NULL_PTR);
3042             }
3043
3044           need_vtbl = maybe_needed;
3045         }
3046     }
3047
3048   if (TYPE_SIZE (basetype) == 0)
3049     {
3050       /* This is worth complaining about, I think.  */
3051       cp_error ("cannot lookup method in incomplete type `%T'", basetype);
3052       return error_mark_node;
3053     }
3054
3055   save_basetype = TYPE_MAIN_VARIANT (basetype);
3056
3057 #if 0
3058   if (all_virtual == 1
3059       && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
3060                      OPERATOR_METHOD_LENGTH)
3061           || instance_ptr == NULL_TREE
3062           || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0)))
3063     all_virtual = 0;
3064 #endif
3065
3066   last = NULL_TREE;
3067   for (parmtypes = NULL_TREE, parm = parms; parm; parm = TREE_CHAIN (parm))
3068     {
3069       tree t = TREE_TYPE (TREE_VALUE (parm));
3070       if (TREE_CODE (t) == OFFSET_TYPE)
3071         {
3072           /* Convert OFFSET_TYPE entities to their normal selves.  */
3073           TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
3074           t = TREE_TYPE (TREE_VALUE (parm));
3075         }
3076       if (TREE_CODE (TREE_VALUE (parm)) == OFFSET_REF
3077           && TREE_CODE (t) == METHOD_TYPE)
3078         {
3079           TREE_VALUE (parm) = build_unary_op (ADDR_EXPR, TREE_VALUE (parm), 0);
3080         }
3081       if (TREE_CODE (t) == ARRAY_TYPE)
3082         {
3083           /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3084              This eliminates needless calls to `compute_conversion_costs'.  */
3085           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
3086           t = TREE_TYPE (TREE_VALUE (parm));
3087         }
3088       if (t == error_mark_node)
3089         return error_mark_node;
3090       last = build_tree_list (NULL_TREE, t);
3091       parmtypes = chainon (parmtypes, last);
3092     }
3093
3094   if (instance)
3095     {
3096       /* TREE_READONLY (instance) fails for references.  */
3097       constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
3098       volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
3099       parms = tree_cons (NULL_TREE, instance_ptr, parms);
3100     }
3101   else
3102     {
3103       /* Raw constructors are always in charge.  */
3104       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3105           && ! (flags & LOOKUP_HAS_IN_CHARGE))
3106         {
3107           flags |= LOOKUP_HAS_IN_CHARGE;
3108           parms = tree_cons (NULL_TREE, integer_one_node, parms);
3109           parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
3110         }
3111
3112       if (flag_this_is_variable > 0)
3113         {
3114           constp = 0;
3115           volatilep = 0;
3116           parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
3117         }
3118       else
3119         {
3120           constp = 0;
3121           volatilep = 0;
3122           instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
3123           if (instance_ptr == error_mark_node)
3124             return error_mark_node;
3125           instance_ptr = save_expr (instance_ptr);
3126           TREE_CALLS_NEW (instance_ptr) = 1;
3127           instance = build_indirect_ref (instance_ptr, NULL_PTR);
3128
3129           /* If it's a default argument initialized from a ctor, what we get
3130              from instance_ptr will match the arglist for the FUNCTION_DECL
3131              of the constructor.  */
3132           if (parms && TREE_CODE (TREE_VALUE (parms)) == CALL_EXPR
3133               && TREE_OPERAND (TREE_VALUE (parms), 1)
3134               && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms), 1))))
3135             parms = build_tree_list (NULL_TREE, instance_ptr);
3136           else
3137             parms = tree_cons (NULL_TREE, instance_ptr, parms);
3138         }
3139     }
3140
3141   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
3142
3143   if (last == NULL_TREE)
3144     last = parmtypes;
3145
3146   /* Look up function name in the structure type definition.  */
3147
3148   if ((IDENTIFIER_HAS_TYPE_VALUE (name)
3149        && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name))
3150        && TREE_CODE(IDENTIFIER_TYPE_VALUE (name)) != UNINSTANTIATED_P_TYPE)
3151       || name == constructor_name (basetype))
3152     {
3153       tree tmp = NULL_TREE;
3154       if (IDENTIFIER_TYPE_VALUE (name) == basetype
3155           || name == constructor_name (basetype))
3156         tmp = TYPE_BINFO (basetype);
3157       else
3158         tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
3159       
3160       if (tmp != NULL_TREE)
3161         {
3162           name_kind = "constructor";
3163           
3164           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3165               && ! (flags & LOOKUP_HAS_IN_CHARGE))
3166             {
3167               /* Constructors called for initialization
3168                  only are never in charge.  */
3169               tree tmplist;
3170               
3171               flags |= LOOKUP_HAS_IN_CHARGE;
3172               tmplist = tree_cons (NULL_TREE, integer_zero_node,
3173                                    TREE_CHAIN (parms));
3174               TREE_CHAIN (parms) = tmplist;
3175               tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
3176               TREE_CHAIN (parmtypes) = tmplist;
3177             }
3178           basetype = BINFO_TYPE (tmp);
3179         }
3180       else
3181         name_kind = "method";
3182     }
3183   else
3184     name_kind = "method";
3185   
3186   if (basetype_path == NULL_TREE
3187       || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
3188     basetype_path = TYPE_BINFO (basetype);
3189   result = lookup_fnfields (basetype_path, name,
3190                             (flags & LOOKUP_COMPLAIN));
3191   if (result == error_mark_node)
3192     return error_mark_node;
3193
3194
3195   /* Now, go look for this method name.  We do not find destructors here.
3196
3197      Putting `void_list_node' on the end of the parmtypes
3198      fakes out `build_decl_overload' into doing the right thing.  */
3199   TREE_CHAIN (last) = void_list_node;
3200   method_name = build_decl_overload (name, parmtypes,
3201                                      1 + (name == constructor_name (save_basetype)
3202                                           || name == constructor_name_full (save_basetype)));
3203   TREE_CHAIN (last) = NULL_TREE;
3204
3205   for (pass = 0; pass < 2; pass++)
3206     {
3207       struct candidate *candidates;
3208       struct candidate *cp;
3209       int len;
3210       unsigned best = 1;
3211
3212       /* This increments every time we go up the type hierarchy.
3213          The idea is to prefer a function of the derived class if possible. */
3214       int b_or_d = 0;
3215
3216       baselink = result;
3217
3218       if (pass > 0)
3219         {
3220           candidates
3221             = (struct candidate *) alloca ((ever_seen+1)
3222                                            * sizeof (struct candidate));
3223           bzero (candidates, (ever_seen + 1) * sizeof (struct candidate));
3224           cp = candidates;
3225           len = list_length (parms);
3226           ever_seen = 0;
3227
3228           /* First see if a global function has a shot at it.  */
3229           if (flags & LOOKUP_GLOBAL)
3230             {
3231               tree friend_parms;
3232               tree parm = instance_ptr;
3233
3234               if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
3235                 {
3236                   /* TREE_VALUE (parms) may have been modified by now;
3237                      restore it to its original value. */
3238                   TREE_VALUE (parms) = parm;
3239                   friend_parms = parms;
3240                 }
3241               else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
3242                 {
3243                   tree new_type;
3244                   parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
3245                   new_type = build_reference_type (TREE_TYPE (parm));
3246                   /* It is possible that this should go down a layer. */
3247                   new_type = build_type_variant (new_type, constp, volatilep);
3248                   parm = convert (new_type, parm);
3249                   friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
3250                 }
3251               else
3252                 my_friendly_abort (167);
3253
3254               cp->h_len = len;
3255               if (flag_ansi_overloading)
3256                 cp->v.ansi_harshness = (struct harshness_code *)
3257                   alloca ((len + 1) * sizeof (struct harshness_code));
3258               else
3259                 cp->v.old_harshness = (unsigned short *)
3260                   alloca ((len + 1) * sizeof (unsigned short));
3261
3262               result = build_overload_call (name, friend_parms, 0, cp);
3263               /* If it turns out to be the one we were actually looking for
3264                  (it was probably a friend function), the return the
3265                  good result.  */
3266               if (TREE_CODE (result) == CALL_EXPR)
3267                 return result;
3268
3269               if (flag_ansi_overloading)
3270                 while ((cp->h.code & EVIL_CODE) == 0)
3271                   {
3272                     /* non-standard uses: set the field to 0 to indicate
3273                        we are using a non-member function.  */
3274                     cp->u.field = 0;
3275                     if (cp->v.ansi_harshness[len].distance == 0
3276                         && cp->h.code < best)
3277                       best = cp->h.code;
3278                     cp += 1;
3279                   }
3280               else
3281                 while (cp->evil == 0)
3282                   {
3283                     /* non-standard uses: set the field to 0 to indicate
3284                        we are using a non-member function.  */
3285                     cp->u.field = 0;
3286                     if (cp->v.old_harshness[len] == 0
3287                         && cp->v.old_harshness[len] == 0
3288                         && cp->ellipsis == 0 && cp->user == 0 && cp->b_or_d == 0
3289                         && cp->easy < best)
3290                       best = cp->easy;
3291                     cp += 1;
3292                   }
3293             }
3294         }
3295
3296       while (baselink)
3297         {
3298           /* We have a hit (of sorts). If the parameter list is
3299              "error_mark_node", or some variant thereof, it won't
3300              match any methods.  Since we have verified that the is
3301              some method vaguely matching this one (in name at least),
3302              silently return.
3303              
3304              Don't stop for friends, however.  */
3305           basetype_path = TREE_PURPOSE (baselink);
3306
3307           function = TREE_VALUE (baselink);
3308           if (TREE_CODE (basetype_path) == TREE_LIST)
3309             basetype_path = TREE_VALUE (basetype_path);
3310           basetype = BINFO_TYPE (basetype_path);
3311
3312           /* Cast the instance variable if necessary.  */
3313           if (basetype != TYPE_MAIN_VARIANT
3314               (TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)))))
3315             {
3316               if (basetype == save_basetype)
3317                 TREE_VALUE (parms) = instance_ptr;
3318               else
3319                 {
3320                   tree type = build_pointer_type
3321                     (build_type_variant (basetype, constp, volatilep));
3322                   TREE_VALUE (parms) = convert_force (type, instance_ptr);
3323                 }
3324             }
3325
3326           /* FIXME: this is the wrong place to get an error.  Hopefully
3327              the access-control rewrite will make this change more cleanly.  */
3328           if (TREE_VALUE (parms) == error_mark_node)
3329             return error_mark_node;
3330
3331           if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
3332             function = DECL_CHAIN (function);
3333
3334           for (; function; function = DECL_CHAIN (function))
3335             {
3336 #ifdef GATHER_STATISTICS
3337               n_inner_fields_searched++;
3338 #endif
3339               ever_seen++;
3340               if (pass > 0)
3341                 found_fns = tree_cons (NULL_TREE, function, found_fns);
3342
3343               /* Not looking for friends here.  */
3344               if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
3345                   && ! DECL_STATIC_FUNCTION_P (function))
3346                 continue;
3347
3348               if (pass == 0
3349                   && DECL_ASSEMBLER_NAME (function) == method_name)
3350                 goto found;
3351
3352               if (pass > 0)
3353                 {
3354                   tree these_parms = parms;
3355
3356 #ifdef GATHER_STATISTICS
3357                   n_inner_fields_searched++;
3358 #endif
3359                   cp->h_len = len;
3360                   if (flag_ansi_overloading)
3361                     cp->v.ansi_harshness = (struct harshness_code *)
3362                       alloca ((len + 1) * sizeof (struct harshness_code));
3363                   else
3364                     cp->v.old_harshness = (unsigned short *)
3365                       alloca ((len + 1) * sizeof (unsigned short));
3366
3367                   if (DECL_STATIC_FUNCTION_P (function))
3368                     these_parms = TREE_CHAIN (these_parms);
3369                   compute_conversion_costs (function, these_parms, cp, len);
3370
3371                   if (!flag_ansi_overloading)
3372                       cp->b_or_d += b_or_d;
3373
3374                   if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE) == 0)
3375                       || (!flag_ansi_overloading && cp->evil == 0))
3376                     {
3377                       cp->u.field = function;
3378                       cp->function = function;
3379                       cp->basetypes = basetype_path;
3380
3381                       /* No "two-level" conversions.  */
3382                       if (flags & LOOKUP_NO_CONVERSION
3383                           && ((flag_ansi_overloading
3384                                && (cp->h.code & USER_CODE))
3385                               || (!flag_ansi_overloading
3386                                   && cp->user != 0)))
3387                         continue;
3388
3389                       /* If we used default parameters, we must
3390                          check to see whether anyone else might
3391                          use them also, and report a possible
3392                          ambiguity.  */
3393                       if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
3394                           && ((flag_ansi_overloading
3395                                && cp->v.ansi_harshness[len].distance == 0
3396                                && cp->h.code < best)
3397                               || (!flag_ansi_overloading
3398                                   && cp->v.old_harshness[len] == 0
3399                                   && CONST_HARSHNESS (cp->v.old_harshness[0]) == 0
3400                                   && cp->ellipsis == 0 && cp->user == 0 && cp->b_or_d == 0
3401                                   && cp->easy < best)))
3402                         {
3403                           if (! DECL_STATIC_FUNCTION_P (function))
3404                             TREE_VALUE (parms) = cp->arg;
3405                           if (best == 1)
3406                             goto found_and_maybe_warn;
3407                         }
3408                       cp++;
3409                     }
3410                 }
3411             }
3412           /* Now we have run through one link's member functions.
3413              arrange to head-insert this link's links.  */
3414           baselink = next_baselink (baselink);
3415           b_or_d += 1;
3416           /* Don't grab functions from base classes.  lookup_fnfield will
3417              do the work to get us down into the right place.  */
3418           baselink = NULL_TREE;
3419         }
3420       if (pass == 0)
3421         {
3422           tree igv = lookup_name_nonclass (name);
3423
3424           /* No exact match could be found.  Now try to find match
3425              using default conversions.  */
3426           if ((flags & LOOKUP_GLOBAL) && igv)
3427             {
3428               if (TREE_CODE (igv) == FUNCTION_DECL)
3429                 ever_seen += 1;
3430               else if (TREE_CODE (igv) == TREE_LIST)
3431                 ever_seen += count_functions (igv);
3432             }
3433
3434           if (ever_seen == 0)
3435             {
3436               if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3437                   == LOOKUP_SPECULATIVELY)
3438                 return NULL_TREE;
3439               
3440               TREE_CHAIN (last) = void_list_node;
3441               if (flags & LOOKUP_GLOBAL)
3442                 cp_error ("no global or member function `%D(%A)' defined",
3443                           name, parmtypes);
3444               else
3445                 cp_error ("no member function `%T::%D(%A)' defined",
3446                           save_basetype, name, TREE_CHAIN (parmtypes));
3447               return error_mark_node;
3448             }
3449           continue;
3450         }
3451
3452       if (cp - candidates != 0)
3453         {
3454           /* Rank from worst to best.  Then cp will point to best one.
3455              Private fields have their bits flipped.  For unsigned
3456              numbers, this should make them look very large.
3457              If the best alternate has a (signed) negative value,
3458              then all we ever saw were private members.  */
3459           if (cp - candidates > 1)
3460             {
3461               int n_candidates = cp - candidates;
3462               TREE_VALUE (parms) = instance_ptr;
3463               cp = ideal_candidate (save_basetype, candidates,
3464                                     n_candidates, parms, len);
3465               if (cp == (struct candidate *)0)
3466                 {
3467                   if (flags & LOOKUP_COMPLAIN)
3468                     {
3469                       cp_error ("call of overloaded %s `%D' is ambiguous",
3470                                 name_kind, name);
3471                       print_n_candidates (candidates, n_candidates);
3472                     }
3473                   return error_mark_node;
3474                 }
3475               if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE))
3476                   || (!flag_ansi_overloading && cp->evil))
3477                 return error_mark_node;
3478             }
3479           else if ((flag_ansi_overloading && (cp[-1].h.code & EVIL_CODE))
3480                    || (!flag_ansi_overloading && cp[-1].evil == 2))
3481             {
3482               if (flags & LOOKUP_COMPLAIN)
3483                 cp_error ("ambiguous type conversion requested for %s `%D'",
3484                           name_kind, name);
3485               return error_mark_node;
3486             }
3487           else
3488             cp--;
3489
3490           /* The global function was the best, so use it.  */
3491           if (cp->u.field == 0)
3492             {
3493               /* We must convert the instance pointer into a reference type.
3494                  Global overloaded functions can only either take
3495                  aggregate objects (which come for free from references)
3496                  or reference data types anyway.  */
3497               TREE_VALUE (parms) = copy_node (instance_ptr);
3498               TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
3499               return build_function_call (cp->function, parms);
3500             }
3501
3502           function = cp->function;
3503           basetype_path = cp->basetypes;
3504           if (! DECL_STATIC_FUNCTION_P (function))
3505             TREE_VALUE (parms) = cp->arg;
3506           goto found_and_maybe_warn;
3507         }
3508
3509       if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
3510         {
3511           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3512               == LOOKUP_SPECULATIVELY)
3513             return NULL_TREE;
3514
3515           if (DECL_STATIC_FUNCTION_P (cp->function))
3516             parms = TREE_CHAIN (parms);
3517           if (ever_seen)
3518             {
3519               if (flags & LOOKUP_SPECULATIVELY)
3520                 return NULL_TREE;
3521               if (static_call_context
3522                   && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
3523                 cp_error ("object missing in call to `%D'", cp->function);
3524               else if (ever_seen > 1)
3525                 {
3526                   TREE_CHAIN (last) = void_list_node;
3527                   cp_error ("no matching function for call to `%T::%D (%A)'",
3528                             TREE_TYPE (TREE_TYPE (instance_ptr)),
3529                             name, TREE_CHAIN (parmtypes));
3530                   TREE_CHAIN (last) = NULL_TREE;
3531                   print_candidates (found_fns);
3532                 }
3533               else
3534                 report_type_mismatch (cp, parms, name_kind);
3535               return error_mark_node;
3536             }
3537
3538           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3539               == LOOKUP_COMPLAIN)
3540             {
3541               cp_error ("%T has no method named %D", save_basetype, name);
3542               return error_mark_node;
3543             }
3544           return NULL_TREE;
3545         }
3546       continue;
3547
3548     found_and_maybe_warn:
3549       if (((flag_ansi_overloading
3550             && (cp->v.ansi_harshness[0].code & CONST_CODE))
3551            || (!flag_ansi_overloading
3552                && CONST_HARSHNESS (cp->v.old_harshness[0])))
3553           /* 12.1p2: Constructors can be called for const objects.  */
3554           && ! DECL_CONSTRUCTOR_P (cp->function))
3555         {
3556           if (flags & LOOKUP_COMPLAIN)
3557             {
3558               cp_error_at ("non-const member function `%D'", cp->function);
3559               error ("called for const object at this point in file");
3560             }
3561           /* Not good enough for a match.  */
3562           else
3563             return error_mark_node;
3564         }
3565       goto found;
3566     }
3567   /* Silently return error_mark_node.  */
3568   return error_mark_node;
3569
3570  found:
3571   if (flags & LOOKUP_PROTECT)
3572     access = compute_access (basetype_path, function);
3573
3574   if (access == access_private)
3575     {
3576       if (flags & LOOKUP_COMPLAIN)
3577         {
3578           cp_error_at ("%s `%+#D' is %s", name_kind, function, 
3579                        TREE_PRIVATE (function) ? "private"
3580                        : "from private base class");
3581           error ("within this context");
3582         }
3583       return error_mark_node;
3584     }
3585   else if (access == access_protected)
3586     {
3587       if (flags & LOOKUP_COMPLAIN)
3588         {
3589           cp_error_at ("%s `%+#D' %s", name_kind, function,
3590                        TREE_PROTECTED (function) ? "is protected"
3591                        : "has protected accessibility");
3592           error ("within this context");
3593         }
3594       return error_mark_node;
3595     }
3596
3597   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
3598      type (if it exists) is a pointer to.  */
3599
3600   if (DECL_ABSTRACT_VIRTUAL_P (function)
3601       && instance == C_C_D
3602       && DECL_CONSTRUCTOR_P (current_function_decl)
3603       && ! (flags & LOOKUP_NONVIRTUAL)
3604       && value_member (function, get_abstract_virtuals (basetype)))
3605     cp_error ("abstract virtual `%#D' called from constructor", function);
3606
3607   if (IS_SIGNATURE (basetype) && static_call_context)
3608     {
3609       cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
3610                 basetype, name);
3611       return error_mark_node;
3612         }
3613   else if (IS_SIGNATURE (basetype))
3614     return build_signature_method_call (basetype, instance, function, parms);
3615
3616   function = DECL_MAIN_VARIANT (function);
3617   /* Declare external function if necessary. */
3618   assemble_external (function);
3619
3620   fntype = TREE_TYPE (function);
3621   if (TREE_CODE (fntype) == POINTER_TYPE)
3622     fntype = TREE_TYPE (fntype);
3623   basetype = DECL_CLASS_CONTEXT (function);
3624
3625   /* If we are referencing a virtual function from an object
3626      of effectively static type, then there is no need
3627      to go through the virtual function table.  */
3628   if (need_vtbl == maybe_needed)
3629     {
3630       int fixed_type = resolves_to_fixed_type_p (instance, 0);
3631
3632       if (all_virtual == 1
3633           && DECL_VINDEX (function)
3634           && may_be_remote (basetype))
3635         need_vtbl = needed;
3636       else if (DECL_VINDEX (function))
3637         need_vtbl = fixed_type ? unneeded : needed;
3638       else
3639         need_vtbl = not_needed;
3640     }
3641
3642   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
3643       && !DECL_CONSTRUCTOR_P (function))
3644     {
3645       /* Let's be nice to the user for now, and give reasonable
3646          default behavior.  */
3647       instance_ptr = current_class_decl;
3648       if (instance_ptr)
3649         {
3650           if (basetype != current_class_type)
3651             {
3652               tree binfo = get_binfo (basetype, current_class_type, 1);
3653               if (binfo == NULL_TREE)
3654                 {
3655                   error_not_base_type (function, current_class_type);
3656                   return error_mark_node;
3657                 }
3658               else if (basetype == error_mark_node)
3659                 return error_mark_node;
3660             }
3661         }
3662       /* Only allow a static member function to call another static member
3663          function.  */
3664       else if (DECL_LANG_SPECIFIC (function)
3665                && !DECL_STATIC_FUNCTION_P (function))
3666         {
3667           cp_error ("cannot call member function `%D' without object",
3668                     function);
3669           return error_mark_node;
3670         }
3671     }
3672
3673   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3674
3675   if (TYPE_SIZE (value_type) == 0)
3676     {
3677       if (flags & LOOKUP_COMPLAIN)
3678         incomplete_type_error (0, value_type);
3679       return error_mark_node;
3680     }
3681
3682   /* We do not pass FUNCTION into `convert_arguments', because by
3683      now everything should be ok.  If not, then we have a serious error.  */
3684   if (DECL_STATIC_FUNCTION_P (function))
3685     parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
3686                                TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
3687   else if (need_vtbl == unneeded)
3688     {
3689       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
3690       basetype = TREE_TYPE (instance);
3691       if (TYPE_METHOD_BASETYPE (TREE_TYPE (function)) != TYPE_MAIN_VARIANT (basetype)
3692           && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3693         {
3694           basetype = DECL_CLASS_CONTEXT (function);
3695           instance_ptr = convert_pointer_to (basetype, instance_ptr);
3696           instance = build_indirect_ref (instance_ptr, NULL_PTR);
3697         }
3698       parms = tree_cons (NULL_TREE, instance_ptr,
3699                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
3700     }
3701   else
3702     {
3703       if ((flags & LOOKUP_NONVIRTUAL) == 0)
3704         basetype = DECL_CONTEXT (function);
3705
3706       /* First parm could be integer_zerop with casts like
3707          ((Object*)0)->Object::IsA()  */
3708       if (!integer_zerop (TREE_VALUE (parms)))
3709         {
3710           /* Since we can't have inheritance with a union, doing get_binfo
3711              on it won't work.  We do all the convert_pointer_to_real
3712              stuff to handle MI correctly...for unions, that's not
3713              an issue, so we must short-circuit that extra work here.  */
3714           tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
3715           if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
3716             instance_ptr = TREE_VALUE (parms);
3717           else
3718             {
3719               tree binfo = get_binfo (basetype,
3720                                       TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3721                                       0);
3722               instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3723             }
3724           instance_ptr
3725             = convert_pointer_to (build_type_variant (basetype,
3726                                                       constp, volatilep),
3727                                   instance_ptr);
3728
3729           if (TREE_CODE (instance_ptr) == COND_EXPR)
3730             {
3731               instance_ptr = save_expr (instance_ptr);
3732               instance = build_indirect_ref (instance_ptr, NULL_PTR);
3733             }
3734           else if (TREE_CODE (instance_ptr) == NOP_EXPR
3735                    && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
3736                    && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
3737             ;
3738           /* The call to `convert_pointer_to' may return error_mark_node.  */
3739           else if (TREE_CODE (instance_ptr) == ERROR_MARK)
3740             return instance_ptr;
3741           else if (instance == NULL_TREE
3742                    || TREE_CODE (instance) != INDIRECT_REF
3743                    || TREE_OPERAND (instance, 0) != instance_ptr)
3744             instance = build_indirect_ref (instance_ptr, NULL_PTR);
3745         }
3746       parms = tree_cons (NULL_TREE, instance_ptr,
3747                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL));
3748     }
3749
3750 #if 0
3751   /* Constructors do not overload method calls.  */
3752   else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
3753            && name != TYPE_IDENTIFIER (basetype)
3754            && (TREE_CODE (function) != FUNCTION_DECL
3755                || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)),
3756                            OPERATOR_METHOD_FORMAT,
3757                            OPERATOR_METHOD_LENGTH))
3758            && (may_be_remote (basetype) || instance != C_C_D))
3759     {
3760       tree fn_as_int;
3761
3762       parms = TREE_CHAIN (parms);
3763
3764       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
3765         fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
3766       else
3767         fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
3768       if (all_virtual == 1)
3769         fn_as_int = convert (integer_type_node, fn_as_int);
3770
3771       result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
3772
3773       if (result == NULL_TREE)
3774         {
3775           compiler_error ("could not overload `operator->()(...)'");
3776           return error_mark_node;
3777         }
3778       else if (result == error_mark_node)
3779         return error_mark_node;
3780
3781 #if 0
3782       /* Do this if we want the result of operator->() to inherit
3783          the type of the function it is subbing for.  */
3784       TREE_TYPE (result) = value_type;
3785 #endif
3786
3787       return result;
3788     }
3789 #endif
3790
3791   if (need_vtbl == needed)
3792     {
3793       function = build_vfn_ref (&TREE_VALUE (parms), instance,
3794                                 DECL_VINDEX (function));
3795       TREE_TYPE (function) = build_pointer_type (fntype);
3796     }
3797
3798   if (TREE_CODE (function) == FUNCTION_DECL)
3799     GNU_xref_call (current_function_decl,
3800                    IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
3801
3802   {
3803     int is_constructor;
3804     
3805     if (TREE_CODE (function) == FUNCTION_DECL)
3806       {
3807         is_constructor = DECL_CONSTRUCTOR_P (function);
3808         if (DECL_INLINE (function))
3809           function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
3810         else
3811           {
3812             assemble_external (function);
3813             TREE_USED (function) = 1;
3814             function = default_conversion (function);
3815           }
3816       }
3817     else
3818       {
3819         is_constructor = 0;
3820         function = default_conversion (function);
3821       }
3822
3823     result = build_nt (CALL_EXPR, function, parms, NULL_TREE);
3824
3825     TREE_TYPE (result) = value_type;
3826     TREE_SIDE_EFFECTS (result) = 1;
3827     TREE_RAISES (result)
3828       = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
3829     TREE_HAS_CONSTRUCTOR (result) = is_constructor;
3830     return result;
3831   }
3832 }
3833
3834 /* Similar to `build_method_call', but for overloaded non-member functions.
3835    The name of this function comes through NAME.  The name depends
3836    on PARMS.
3837
3838    Note that this function must handle simple `C' promotions,
3839    as well as variable numbers of arguments (...), and
3840    default arguments to boot.
3841
3842    If the overloading is successful, we return a tree node which
3843    contains the call to the function.
3844
3845    If overloading produces candidates which are probable, but not definite,
3846    we hold these candidates.  If FINAL_CP is non-zero, then we are free
3847    to assume that final_cp points to enough storage for all candidates that
3848    this function might generate.  The `harshness' array is preallocated for
3849    the first candidate, but not for subsequent ones.
3850
3851    Note that the DECL_RTL of FUNCTION must be made to agree with this
3852    function's new name.  */
3853
3854 tree
3855 build_overload_call_real (fnname, parms, flags, final_cp, buildxxx)
3856      tree fnname, parms;
3857      int flags;
3858      struct candidate *final_cp;
3859      int buildxxx;
3860 {
3861   /* must check for overloading here */
3862   tree overload_name, functions, function, parm;
3863   tree parmtypes = NULL_TREE, last = NULL_TREE;
3864   register tree outer;
3865   int length;
3866   int parmlength = list_length (parms);
3867
3868   struct candidate *candidates, *cp;
3869
3870   if (final_cp)
3871     {
3872       if (flag_ansi_overloading)
3873         {
3874           final_cp[0].h.code = 0;
3875           final_cp[0].h.distance = 0;
3876           final_cp[0].function = 0;
3877           /* end marker.  */
3878           final_cp[1].h.code = EVIL_CODE;
3879         }
3880       else
3881         {
3882           final_cp[0].evil = 0;
3883           final_cp[0].user = 0;
3884           final_cp[0].b_or_d = 0;
3885           final_cp[0].easy = 0;
3886           final_cp[0].function = 0;
3887           /* end marker.  */
3888           final_cp[1].evil = 1;
3889         }
3890     }
3891
3892   for (parm = parms; parm; parm = TREE_CHAIN (parm))
3893     {
3894       register tree t = TREE_TYPE (TREE_VALUE (parm));
3895
3896       if (t == error_mark_node)
3897         {
3898           if (final_cp)
3899             {
3900               if (flag_ansi_overloading)
3901                 final_cp->h.code = EVIL_CODE;
3902               else
3903                 final_cp->evil = 1;
3904             }
3905           return error_mark_node;
3906         }
3907       if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
3908         {
3909           /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3910              Also convert OFFSET_TYPE entities to their normal selves.
3911              This eliminates needless calls to `compute_conversion_costs'.  */
3912           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
3913           t = TREE_TYPE (TREE_VALUE (parm));
3914         }
3915       last = build_tree_list (NULL_TREE, t);
3916       parmtypes = chainon (parmtypes, last);
3917     }
3918   if (last)
3919     TREE_CHAIN (last) = void_list_node;
3920   else
3921     parmtypes = void_list_node;
3922
3923   if (! flag_ansi_overloading)
3924     {
3925       tree fn;
3926
3927       /* This is a speed improvement that ends up not working properly in
3928          the situation of fns with and without default parameters.  I turned
3929          this off in the new method so it'll go through the argument matching
3930          code to properly diagnose a match/failure. (bpk)  */
3931       overload_name = build_decl_overload (fnname, parmtypes, 0);
3932       fn = lookup_name_nonclass (overload_name);
3933
3934       /* Now check to see whether or not we can win.
3935          Note that if we are called from `build_method_call',
3936          then we cannot have a mis-match, because we would have
3937          already found such a winning case.  */
3938
3939       if (fn && TREE_CODE (fn) == FUNCTION_DECL)
3940         return build_function_call (DECL_MAIN_VARIANT (fn), parms);
3941     }
3942
3943   functions = lookup_name_nonclass (fnname);
3944
3945   if (functions == NULL_TREE)
3946     {
3947       if (flags & LOOKUP_SPECULATIVELY)
3948         return NULL_TREE;
3949       if (flags & LOOKUP_COMPLAIN)
3950         error ("only member functions apply");
3951       if (final_cp)
3952         {
3953           if (flag_ansi_overloading)
3954             final_cp->h.code = EVIL_CODE;
3955           else
3956             final_cp->evil = 1;
3957         }
3958       return error_mark_node;
3959     }
3960
3961   if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
3962     {
3963       functions = DECL_MAIN_VARIANT (functions);
3964       if (final_cp)
3965         {
3966           /* We are just curious whether this is a viable alternative or
3967              not.  */
3968           compute_conversion_costs (functions, parms, final_cp, parmlength);
3969           return functions;
3970         }
3971       else
3972         return build_function_call_real (functions, parms, 1, flags);
3973     }
3974
3975   if (TREE_CODE (functions) == TREE_LIST
3976       && TREE_VALUE (functions) == NULL_TREE)
3977     {
3978       if (flags & LOOKUP_SPECULATIVELY)
3979         return NULL_TREE;
3980       
3981       if (flags & LOOKUP_COMPLAIN)
3982         cp_error ("function `%D' declared overloaded, but no instances of that function declared",
3983                   TREE_PURPOSE (functions));
3984       if (final_cp)
3985         {
3986           if (flag_ansi_overloading)
3987             final_cp->h.code = EVIL_CODE;
3988           else
3989             final_cp->evil = 1;
3990         }
3991       return error_mark_node;
3992     }
3993
3994   length = count_functions (functions);
3995   
3996   if (final_cp)
3997     candidates = final_cp;
3998   else
3999     {
4000       candidates
4001         = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
4002       bzero (candidates, (length + 1) * sizeof (struct candidate));
4003     }
4004
4005   cp = candidates;
4006
4007   my_friendly_assert (is_overloaded_fn (functions), 169);
4008
4009   functions = get_first_fn (functions);
4010
4011   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
4012   for (outer = functions; outer; outer = DECL_CHAIN (outer))
4013     {
4014       int template_cost = 0;
4015       function = outer;
4016       if (TREE_CODE (function) != FUNCTION_DECL
4017           && ! (TREE_CODE (function) == TEMPLATE_DECL
4018                 && ! DECL_TEMPLATE_IS_CLASS (function)
4019                 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
4020         {
4021           enum tree_code code = TREE_CODE (function);
4022           if (code == TEMPLATE_DECL)
4023             code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
4024           if (code == CONST_DECL)
4025             cp_error_at
4026               ("enumeral value `%D' conflicts with function of same name",
4027                function);
4028           else if (code == VAR_DECL)
4029             {
4030               if (TREE_STATIC (function))
4031                 cp_error_at
4032                   ("variable `%D' conflicts with function of same name",
4033                    function);
4034               else
4035                 cp_error_at
4036                   ("constant field `%D' conflicts with function of same name",
4037                    function);
4038             }
4039           else if (code == TYPE_DECL)
4040             continue;
4041           else
4042             my_friendly_abort (2);
4043           error ("at this point in file");
4044           continue;
4045         }
4046       if (TREE_CODE (function) == TEMPLATE_DECL)
4047         {
4048           int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
4049           tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
4050           int i;
4051
4052           i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
4053                                 TYPE_ARG_TYPES (TREE_TYPE (function)),
4054                                 parms, &template_cost, 0);
4055           if (i == 0)
4056             function = instantiate_template (function, targs);
4057         }
4058
4059       if (TREE_CODE (function) == TEMPLATE_DECL)
4060         {
4061           /* Unconverted template -- failed match.  */
4062           cp->function = function;
4063           cp->u.bad_arg = -4;
4064           if (flag_ansi_overloading)
4065             cp->h.code = EVIL_CODE;
4066           else
4067             cp->evil = 1;
4068         }
4069       else
4070         {
4071           struct candidate *cp2;
4072
4073           /* Check that this decl is not the same as a function that's in
4074              the list due to some template instantiation.  */
4075           cp2 = candidates;
4076           while (cp2 != cp)
4077             if (cp2->function == function)
4078               break;
4079             else
4080               cp2 += 1;
4081           if (cp2->function == function)
4082             continue;
4083
4084           function = DECL_MAIN_VARIANT (function);
4085
4086           /* Can't use alloca here, since result might be
4087              passed to calling function.  */
4088           cp->h_len = parmlength;
4089           if (flag_ansi_overloading)
4090             cp->v.ansi_harshness = (struct harshness_code *)
4091               oballoc ((parmlength + 1) * sizeof (struct harshness_code));
4092           else
4093             cp->v.old_harshness = (unsigned short *)
4094               oballoc ((parmlength + 1) * sizeof (unsigned short));
4095
4096           compute_conversion_costs (function, parms, cp, parmlength);
4097
4098           if (flag_ansi_overloading)
4099             /* Make sure this is clear as well.  */
4100             cp->h.int_penalty += template_cost;
4101           else
4102             /* Should really add another field...  */
4103             cp->easy = cp->easy * 128 + template_cost;
4104
4105           /* It seemed easier to have both if stmts in here, rather
4106              than excluding the hell out of it with flag_ansi_overloading
4107              everywhere. (bpk) */
4108           if (flag_ansi_overloading)
4109             {
4110               if ((cp[0].h.code & EVIL_CODE) == 0)
4111                 {
4112                   cp[1].h.code = EVIL_CODE;
4113
4114                   /* int_penalty is set by convert_harshness_ansi for cases
4115                      where we need to know about any penalties that would
4116                      otherwise make a TRIVIAL_CODE pass.  */
4117                   if (final_cp
4118                       && template_cost == 0
4119                       && cp[0].h.code <= TRIVIAL_CODE
4120                       && cp[0].h.int_penalty == 0)
4121                     {
4122                       final_cp[0].h = cp[0].h;
4123                       return function;
4124                     }
4125                   cp++;
4126                 }
4127             }
4128           else
4129             {
4130               if (cp[0].evil == 0)
4131                 {
4132                   cp[1].evil = 1;
4133                   if (final_cp
4134                       && cp[0].user == 0 && cp[0].b_or_d == 0
4135                       && template_cost == 0
4136                       && cp[0].easy <= 1)
4137                     {
4138                       final_cp[0].easy = cp[0].easy;
4139                       return function;
4140                     }
4141                   cp++;
4142                 }
4143             }
4144         }
4145     }
4146
4147   if (cp - candidates)
4148     {
4149       tree rval = error_mark_node;
4150
4151       /* Leave marker.  */
4152       if (flag_ansi_overloading)
4153         cp[0].h.code = EVIL_CODE;
4154       else
4155         cp[0].evil = 1;
4156       if (cp - candidates > 1)
4157         {
4158           struct candidate *best_cp
4159             = ideal_candidate (NULL_TREE, candidates,
4160                                cp - candidates, parms, parmlength);
4161           if (best_cp == (struct candidate *)0)
4162             {
4163               if (flags & LOOKUP_COMPLAIN)
4164                 {
4165                   cp_error ("call of overloaded `%D' is ambiguous", fnname);
4166                   print_n_candidates (candidates, cp - candidates);
4167                 }
4168               return error_mark_node;
4169             }
4170           else
4171             rval = best_cp->function;
4172         }
4173       else
4174         {
4175           cp -= 1;
4176           if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE))
4177               || (!flag_ansi_overloading && cp->evil > 1))
4178             {
4179               if (flags & LOOKUP_COMPLAIN)
4180                 error ("type conversion ambiguous");
4181             }
4182           else
4183             rval = cp->function;
4184         }
4185
4186       if (final_cp)
4187         return rval;
4188
4189       return buildxxx ? build_function_call_real (rval, parms, 0, flags)
4190         : build_function_call_real (rval, parms, 1, flags);
4191     }
4192
4193   if (flags & LOOKUP_SPECULATIVELY)
4194     return NULL_TREE;
4195   
4196   if (flags & LOOKUP_COMPLAIN)
4197     report_type_mismatch (cp, parms, "function",
4198                           decl_as_string (cp->function, 1));
4199
4200   return error_mark_node;
4201 }
4202
4203 tree
4204 build_overload_call (fnname, parms, flags, final_cp)
4205      tree fnname, parms;
4206      int flags;
4207      struct candidate *final_cp;
4208 {
4209   return build_overload_call_real (fnname, parms, flags, final_cp, 0);
4210 }
4211
4212 tree
4213 build_overload_call_maybe (fnname, parms, flags, final_cp)
4214      tree fnname, parms;
4215      int flags;
4216      struct candidate *final_cp;
4217 {
4218   return build_overload_call_real (fnname, parms, flags, final_cp, 1);
4219 }