OSDN Git Service

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