OSDN Git Service

30th Cygnus<->FSF merge.
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
1 /* Functions related to invoking methods and overloaded functions.
2    Copyright (C) 1987, 1992, 1993 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com) and
4    hacked by Brendan Kehoe (brendan@cygnus.com).
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22
23 /* High-level class interface. */
24
25 #include "config.h"
26 #include "tree.h"
27 #include <stdio.h>
28 #include "cp-tree.h"
29 #include "class.h"
30 #include "flags.h"
31
32 #include "obstack.h"
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
35
36 extern void sorry ();
37
38 extern int inhibit_warnings;
39 extern int flag_assume_nonnull_objects;
40 extern tree ctor_label, dtor_label;
41
42 /* From typeck.c:  */
43 extern tree unary_complex_lvalue ();
44
45 /* Compute the ease with which a conversion can be performed
46    between an expected and the given type.  */
47 static int convert_harshness_old ();
48 static struct harshness_code convert_harshness_ansi ();
49
50 /* OLD METHOD */
51 /* Note the old method also uses USER_HARSHNESS, BASE_DERIVED_HARSHNESS,
52    CONST_HARSHNESS.  */
53 #define EVIL 1
54 #define TRIVIAL 0
55 #define EVIL_HARSHNESS(ARG) ((ARG) & 1)
56 #define ELLIPSIS_HARSHNESS(ARG) ((ARG) & 2)
57 #define CONTRAVARIANT_HARSHNESS(ARG) ((ARG) & 8)
58 #define INT_TO_BD_HARSHNESS(ARG) (((ARG) << 5) | 16)
59 #define INT_FROM_BD_HARSHNESS(ARG) ((ARG) >> 5)
60 #define INT_TO_EASY_HARSHNESS(ARG) ((ARG) << 5)
61 #define INT_FROM_EASY_HARSHNESS(ARG) ((ARG) >> 5)
62 #define ONLY_EASY_HARSHNESS(ARG) (((ARG) & 31) == 0)
63
64
65 /* NEW METHOD */
66 #define EVIL_RETURN(ARG)        ((ARG).code = EVIL_CODE, (ARG))
67 #define QUAL_RETURN(ARG)        ((ARG).code = QUAL_CODE, (ARG))
68 #define TRIVIAL_RETURN(ARG)     ((ARG).code = TRIVIAL_CODE, (ARG))
69 #define ZERO_RETURN(ARG)        ((ARG).code = 0, (ARG))
70
71 #define USER_HARSHNESS(ARG) ((ARG) & 4)
72 #define BASE_DERIVED_HARSHNESS(ARG) ((ARG) & 16)
73 #define CONST_HARSHNESS(ARG) ((ARG) & 2048)
74
75 /* Ordering function for overload resolution.  Compare two candidates
76    by gross quality.  */
77 int
78 rank_for_overload_ansi (x, y)
79      struct candidate *x, *y;
80 {
81   if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
82     return y->h.code - x->h.code;
83   if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
84     return -1;
85
86   /* This is set by compute_conversion_costs, for calling a non-const
87      member function from a const member function.  */
88   if ((y->v.ansi_harshness[0].code & CONST_CODE) ^ (x->v.ansi_harshness[0].code & CONST_CODE))
89     return y->v.ansi_harshness[0].code - x->v.ansi_harshness[0].code;
90
91   if (y->h.code & STD_CODE)
92     {
93       if (x->h.code & STD_CODE)
94         return y->h.distance - x->h.distance;
95       return 1;
96     }
97   if (x->h.code & STD_CODE)
98     return -1;
99
100   return y->h.code - x->h.code;
101 }
102
103 int
104 rank_for_overload_old (x, y)
105      struct candidate *x, *y;
106 {
107   if (y->evil - x->evil)
108     return y->evil - x->evil;
109   if (CONST_HARSHNESS (y->v.old_harshness[0]) ^ CONST_HARSHNESS (x->v.old_harshness[0]))
110     return y->v.old_harshness[0] - x->v.old_harshness[0];
111   if (y->ellipsis - x->ellipsis)
112     return y->ellipsis - x->ellipsis;
113   if (y->user - x->user)
114     return y->user - x->user;
115   if (y->b_or_d - x->b_or_d)
116     return y->b_or_d - x->b_or_d;
117   return y->easy - x->easy;
118 }
119
120 int
121 rank_for_overload (x, y)
122      struct candidate *x, *y;
123 {
124   if (flag_ansi_overloading)
125     return rank_for_overload_ansi (x, y);
126   else
127     return rank_for_overload_old (x, y);
128 }
129
130 /* Compare two candidates, argument by argument.  */
131 int
132 rank_for_ideal (x, y)
133      struct candidate *x, *y;
134 {
135   int i;
136
137   if (x->h_len != y->h_len)
138     abort ();
139
140   for (i = 0; i < x->h_len; i++)
141     {
142       if (y->v.ansi_harshness[i].code - x->v.ansi_harshness[i].code)
143         return y->v.ansi_harshness[i].code - x->v.ansi_harshness[i].code;
144       if ((y->v.ansi_harshness[i].code & STD_CODE)
145           && (y->v.ansi_harshness[i].distance - x->v.ansi_harshness[i].distance))
146         return y->v.ansi_harshness[i].distance - x->v.ansi_harshness[i].distance;
147
148       /* They're both the same code.  Now see if we're dealing with an
149          integral promotion that needs a finer grain of accuracy.  */
150       if (y->v.ansi_harshness[0].code & PROMO_CODE
151           && (y->v.ansi_harshness[i].int_penalty ^ x->v.ansi_harshness[i].int_penalty))
152         return y->v.ansi_harshness[i].int_penalty - x->v.ansi_harshness[i].int_penalty;
153     }
154   return 0;
155 }
156
157 /* TYPE is the type we wish to convert to.  PARM is the parameter
158    we have to work with.  We use a somewhat arbitrary cost function
159    to measure this conversion.  */
160 static struct harshness_code
161 convert_harshness_ansi (type, parmtype, parm)
162      register tree type, parmtype;
163      tree parm;
164 {
165   struct harshness_code h;
166   register enum tree_code codel;
167   register enum tree_code coder;
168
169   h.code = 0;
170   h.distance = 0;
171   h.int_penalty = 0;
172
173 #ifdef GATHER_STATISTICS
174   n_convert_harshness++;
175 #endif
176
177   if (TYPE_PTRMEMFUNC_P (type))
178     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
179   if (TYPE_PTRMEMFUNC_P (parmtype))
180     parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
181
182   codel = TREE_CODE (type);
183   coder = TREE_CODE (parmtype);
184
185   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
186     return ZERO_RETURN (h);
187
188   if (coder == ERROR_MARK)
189     return EVIL_RETURN (h);
190
191   if (codel == POINTER_TYPE && fntype_p (parmtype))
192     {
193       tree p1, p2;
194       struct harshness_code h1, h2;
195
196       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
197       type = TREE_TYPE (type);
198
199       if (coder == POINTER_TYPE)
200         {
201           parmtype = TREE_TYPE (parmtype);
202           coder = TREE_CODE (parmtype);
203         }
204
205       if (coder != TREE_CODE (type))
206         return EVIL_RETURN (h);
207
208       /* We allow the default conversion between function type
209          and pointer-to-function type for free.  */
210       if (type == parmtype)
211         return ZERO_RETURN (h);
212
213       /* Compare return types.  */
214       p1 = TREE_TYPE (type);
215       p2 = TREE_TYPE (parmtype);
216       h2 = convert_harshness_ansi (p1, p2, NULL_TREE);
217       if (h2.code & EVIL_CODE)
218         return h2;
219
220       h1.code = TRIVIAL_CODE;
221       h1.distance = 0;
222
223       if (h2.distance != 0)
224         {
225           tree binfo;
226
227           /* This only works for pointers.  */
228           if (TREE_CODE (p1) != POINTER_TYPE
229               && TREE_CODE (p1) != REFERENCE_TYPE)
230             return EVIL_RETURN (h);
231
232           p1 = TREE_TYPE (p1);
233           p2 = TREE_TYPE (p2);
234           /* Don't die if we happen to be dealing with void*.  */
235           if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
236             return EVIL_RETURN (h);
237           if (h2.distance < 0)
238             binfo = get_binfo (p2, p1, 0);
239           else
240             binfo = get_binfo (p1, p2, 0);
241
242           if (! BINFO_OFFSET_ZEROP (binfo))
243             {
244               static int explained = 0;
245               if (h2.distance < 0)
246                 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p2, p1);
247               else
248                 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p1, p2);
249
250               if (! explained++)
251                 sorry ("(because pointer values change during conversion)");
252               return EVIL_RETURN (h);
253             }
254         }
255
256       h1.code |= h2.code;
257       if (h2.distance > h1.distance)
258         h1.distance = h2.distance;
259
260       p1 = TYPE_ARG_TYPES (type);
261       p2 = TYPE_ARG_TYPES (parmtype);
262       while (p1 && TREE_VALUE (p1) != void_type_node
263              && p2 && TREE_VALUE (p2) != void_type_node)
264         {
265           h2 = convert_harshness_ansi (TREE_VALUE (p1), TREE_VALUE (p2),
266                                        NULL_TREE);
267           if (h2.code & EVIL_CODE)
268             return h2;
269
270           if (h2.distance)
271             {
272               /* This only works for pointers and references. */
273               if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
274                   && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
275                 return EVIL_RETURN (h);
276               h2.distance = - h2.distance;
277             }
278
279           h1.code |= h2.code;
280           if (h2.distance > h1.distance)
281             h1.distance = h2.distance;
282           p1 = TREE_CHAIN (p1);
283           p2 = TREE_CHAIN (p2);
284         }
285       if (p1 == p2)
286         return h1;
287       if (p2)
288         {
289           if (p1)
290             return EVIL_RETURN (h);
291           h1.code |= ELLIPSIS_CODE;
292           return h1;
293         }
294       if (p1)
295         {
296           if (TREE_PURPOSE (p1) == NULL_TREE)
297             h1.code |= EVIL_CODE;
298           return h1;
299         }
300     }
301   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
302     {
303       /* Get to the OFFSET_TYPE that this might be.  */
304       type = TREE_TYPE (type);
305
306       if (coder != TREE_CODE (type))
307         return EVIL_RETURN (h);
308
309       if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
310         h.code = 0;
311       else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (type),
312                                TYPE_OFFSET_BASETYPE (parmtype)))
313         {
314           h.code = STD_CODE;
315           h.distance = 1;
316         }
317       else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (parmtype),
318                                TYPE_OFFSET_BASETYPE (type)))
319         {
320           h.code = STD_CODE;
321           h.distance = -1;
322         }
323       else
324         return EVIL_RETURN (h);
325       /* Now test the OFFSET_TYPE's target compatibility.  */
326       type = TREE_TYPE (type);
327       parmtype = TREE_TYPE (parmtype);
328     }
329
330   if (coder == UNKNOWN_TYPE)
331     {
332       if (codel == FUNCTION_TYPE
333           || codel == METHOD_TYPE
334           || (codel == POINTER_TYPE
335               && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
336                   || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
337         return TRIVIAL_RETURN (h);
338       return EVIL_RETURN (h);
339     }
340
341   if (coder == VOID_TYPE)
342     return EVIL_RETURN (h);
343
344   if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
345     {
346       /* Control equivalence of ints an enums.  */
347
348       if (codel == ENUMERAL_TYPE
349           && flag_int_enum_equivalence == 0)
350         {
351           /* Enums can be converted to ints, but not vice-versa.  */
352           if (coder != ENUMERAL_TYPE
353               || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
354             return EVIL_RETURN (h);
355         }
356
357       /* else enums and ints (almost) freely interconvert.  */
358
359       if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
360         {
361           if ((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 (basetype != TREE_TYPE(instance))
2825         basetype = TREE_TYPE(instance);
2826       if (! TYPE_HAS_DESTRUCTOR (basetype))
2827         return void_zero_node;
2828       instance = default_conversion (instance);
2829       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
2830       return build_delete (build_pointer_type (basetype),
2831                            instance_ptr, integer_two_node,
2832                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
2833     }
2834
2835   {
2836     char *xref_name;
2837     
2838     /* Initialize name for error reporting.  */
2839     if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
2840       {
2841         char *p = operator_name_string (name);
2842         xref_name = (char *)alloca (strlen (p) + 10);
2843         sprintf (xref_name, "operator %s", p);
2844       }
2845     else if (TREE_CODE (name) == SCOPE_REF)
2846       xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
2847     else
2848       xref_name = IDENTIFIER_POINTER (name);
2849
2850     GNU_xref_call (current_function_decl, xref_name);
2851   }
2852
2853   if (instance == NULL_TREE)
2854     {
2855       basetype = NULL_TREE;
2856       /* Check cases where this is really a call to raise
2857          an exception.  */
2858       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
2859         {
2860           basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
2861           if (basetype)
2862             basetype = TREE_VALUE (basetype);
2863         }
2864       else if (TREE_CODE (name) == SCOPE_REF
2865                && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
2866         {
2867           if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
2868             return error_mark_node;
2869           basetype = purpose_member (TREE_OPERAND (name, 1),
2870                                      CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
2871           if (basetype)
2872             basetype = TREE_VALUE (basetype);
2873         }
2874
2875       if (basetype != NULL_TREE)
2876         ;
2877       /* call to a constructor... */
2878       else if (IDENTIFIER_HAS_TYPE_VALUE (name))
2879         {
2880           basetype = IDENTIFIER_TYPE_VALUE (name);
2881           name = constructor_name_full (basetype);
2882         }
2883       else
2884         {
2885           tree typedef_name = lookup_name (name, 1);
2886           if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
2887             {
2888               /* Canonicalize the typedef name.  */
2889               basetype = TREE_TYPE (typedef_name);
2890               name = TYPE_IDENTIFIER (basetype);
2891             }
2892           else
2893             {
2894               cp_error ("no constructor named `%T' in scope",
2895                         name);
2896               return error_mark_node;
2897             }
2898         }
2899
2900       if (! IS_AGGR_TYPE (basetype))
2901         {
2902         non_aggr_error:
2903           if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
2904             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2905                       name, instance, basetype);
2906
2907           return error_mark_node;
2908         }
2909     }
2910   else if (instance == C_C_D || instance == current_class_decl)
2911     {
2912       /* When doing initialization, we side-effect the TREE_TYPE of
2913          C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
2914       basetype = TREE_TYPE (C_C_D);
2915
2916       /* Anything manifestly `this' in constructors and destructors
2917          has a known type, so virtual function tables are not needed.  */
2918       if (TYPE_VIRTUAL_P (basetype)
2919           && !(flags & LOOKUP_NONVIRTUAL))
2920         need_vtbl = (dtor_label || ctor_label)
2921           ? unneeded : maybe_needed;
2922
2923       instance = C_C_D;
2924       instance_ptr = current_class_decl;
2925       result = build_field_call (TYPE_BINFO (current_class_type),
2926                                  instance_ptr, name, parms);
2927
2928       if (result)
2929         return result;
2930     }
2931   else if (TREE_CODE (instance) == RESULT_DECL)
2932     {
2933       basetype = TREE_TYPE (instance);
2934       /* Should we ever have to make a virtual function reference
2935          from a RESULT_DECL, know that it must be of fixed type
2936          within the scope of this function.  */
2937       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
2938         need_vtbl = maybe_needed;
2939       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
2940     }
2941   else if (instance == current_exception_object)
2942     {
2943       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (current_exception_type),
2944                             TREE_OPERAND (current_exception_object, 0));
2945       mark_addressable (TREE_OPERAND (current_exception_object, 0));
2946       result = build_field_call (TYPE_BINFO (current_exception_type),
2947                                  instance_ptr, name, parms);
2948       if (result)
2949         return result;
2950       cp_error ("exception member `%D' cannot be invoked", name);
2951       return error_mark_node;
2952     }
2953   else
2954     {
2955       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
2956       tree inst_ptr_basetype;
2957
2958       static_call_context =
2959         (TREE_CODE (instance) == INDIRECT_REF
2960          && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
2961          && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
2962
2963       /* the base type of an instance variable is pointer to class */
2964       basetype = TREE_TYPE (instance);
2965
2966       if (TREE_CODE (basetype) == REFERENCE_TYPE)
2967         {
2968           basetype = TYPE_MAIN_VARIANT (TREE_TYPE (basetype));
2969           if (! IS_AGGR_TYPE (basetype))
2970             goto non_aggr_error;
2971           /* Call to convert not needed because we are remaining
2972              within the same type.  */
2973           instance_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), instance);
2974           inst_ptr_basetype = basetype;
2975         }
2976       else
2977         {
2978           if (! IS_AGGR_TYPE (basetype))
2979             goto non_aggr_error;
2980
2981           if (IS_SIGNATURE_POINTER (basetype)
2982               || IS_SIGNATURE_REFERENCE (basetype))
2983             basetype = SIGNATURE_TYPE (basetype);
2984
2985           if ((IS_SIGNATURE (basetype)
2986                && (instance_ptr = build_optr_ref (instance)))
2987               || (lvalue_p (instance)
2988                && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
2989               || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
2990             {
2991               if (instance_ptr == error_mark_node)
2992                 return error_mark_node;
2993             }
2994           else if (TREE_CODE (instance) == NOP_EXPR
2995                    || TREE_CODE (instance) == CONSTRUCTOR)
2996             {
2997               /* A cast is not an lvalue.  Initialize a fresh temp
2998                  with the value we are casting from, and proceed with
2999                  that temporary.  We can't cast to a reference type,
3000                  so that simplifies the initialization to something
3001                  we can manage.  */
3002               tree temp = get_temp_name (TREE_TYPE (instance), 0);
3003               if (IS_AGGR_TYPE (TREE_TYPE (instance)))
3004                 expand_aggr_init (temp, instance, 0);
3005               else
3006                 {
3007                   store_init_value (temp, instance);
3008                   expand_decl_init (temp);
3009                 }
3010               instance = temp;
3011               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
3012             }
3013           else
3014             {
3015               if (TREE_CODE (instance) != CALL_EXPR)
3016                 my_friendly_abort (125);
3017               if (TYPE_NEEDS_CONSTRUCTING (basetype))
3018                 instance = build_cplus_new (basetype, instance, 0);
3019               else
3020                 {
3021                   instance = get_temp_name (basetype, 0);
3022                   TREE_ADDRESSABLE (instance) = 1;
3023                 }
3024               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
3025             }
3026           /* @@ Should we call comp_target_types here?  */
3027           inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
3028           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
3029             basetype = inst_ptr_basetype;
3030           else
3031             {
3032               instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
3033               if (instance_ptr == error_mark_node)
3034                 return error_mark_node;
3035             }
3036         }
3037
3038       /* After converting `instance_ptr' above, `inst_ptr_basetype' was
3039          not updated, so we use `basetype' instead.  */
3040       if (basetype_path == NULL_TREE
3041           && IS_SIGNATURE (basetype))
3042         basetype_path = TYPE_BINFO (basetype);
3043       else if (basetype_path == NULL_TREE ||
3044         BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
3045         basetype_path = TYPE_BINFO (inst_ptr_basetype);
3046
3047       result = build_field_call (basetype_path, instance_ptr, name, parms);
3048       if (result)
3049         return result;
3050
3051       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
3052         {
3053           if (TREE_SIDE_EFFECTS (instance_ptr))
3054             {
3055               /* This action is needed because the instance is needed
3056                  for providing the base of the virtual function table.
3057                  Without using a SAVE_EXPR, the function we are building
3058                  may be called twice, or side effects on the instance
3059                  variable (such as a post-increment), may happen twice.  */
3060               instance_ptr = save_expr (instance_ptr);
3061               instance = build_indirect_ref (instance_ptr, NULL_PTR);
3062             }
3063           else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
3064             {
3065               /* This happens when called for operator new ().  */
3066               instance = build_indirect_ref (instance, NULL_PTR);
3067             }
3068
3069           need_vtbl = maybe_needed;
3070         }
3071     }
3072
3073   if (TYPE_SIZE (basetype) == 0)
3074     {
3075       /* This is worth complaining about, I think.  */
3076       cp_error ("cannot lookup method in incomplete type `%T'", basetype);
3077       return error_mark_node;
3078     }
3079
3080   save_basetype = basetype;
3081
3082 #if 0
3083   if (all_virtual == 1
3084       && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
3085                      OPERATOR_METHOD_LENGTH)
3086           || instance_ptr == NULL_TREE
3087           || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0)))
3088     all_virtual = 0;
3089 #endif
3090
3091   last = NULL_TREE;
3092   for (parmtypes = NULL_TREE, parm = parms; parm; parm = TREE_CHAIN (parm))
3093     {
3094       tree t = TREE_TYPE (TREE_VALUE (parm));
3095       if (TREE_CODE (t) == OFFSET_TYPE)
3096         {
3097           /* Convert OFFSET_TYPE entities to their normal selves.  */
3098           TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
3099           t = TREE_TYPE (TREE_VALUE (parm));
3100         }
3101       if (TREE_CODE (TREE_VALUE (parm)) == OFFSET_REF
3102           && TREE_CODE (t) == METHOD_TYPE)
3103         {
3104           TREE_VALUE (parm) = build_unary_op (ADDR_EXPR, TREE_VALUE (parm), 0);
3105         }
3106       if (TREE_CODE (t) == ARRAY_TYPE)
3107         {
3108           /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3109              This eliminates needless calls to `compute_conversion_costs'.  */
3110           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
3111           t = TREE_TYPE (TREE_VALUE (parm));
3112         }
3113       if (t == error_mark_node)
3114         return error_mark_node;
3115       last = build_tree_list (NULL_TREE, t);
3116       parmtypes = chainon (parmtypes, last);
3117     }
3118
3119   if (instance)
3120     {
3121       constp = TREE_READONLY (instance);
3122       volatilep = TREE_THIS_VOLATILE (instance);
3123       parms = tree_cons (NULL_TREE, instance_ptr, parms);
3124     }
3125   else
3126     {
3127       /* Raw constructors are always in charge.  */
3128       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3129           && ! (flags & LOOKUP_HAS_IN_CHARGE))
3130         {
3131           flags |= LOOKUP_HAS_IN_CHARGE;
3132           parms = tree_cons (NULL_TREE, integer_one_node, parms);
3133           parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
3134         }
3135
3136       if (flag_this_is_variable > 0)
3137         {
3138           constp = 0;
3139           volatilep = 0;
3140           parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
3141         }
3142       else
3143         {
3144           constp = 0;
3145           volatilep = 0;
3146           instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
3147           if (instance_ptr == error_mark_node)
3148             return error_mark_node;
3149           instance_ptr = save_expr (instance_ptr);
3150           TREE_CALLS_NEW (instance_ptr) = 1;
3151           instance = build_indirect_ref (instance_ptr, NULL_PTR);
3152
3153           /* If it's a default argument initialized from a ctor, what we get
3154              from instance_ptr will match the arglist for the FUNCTION_DECL
3155              of the constructor.  */
3156           if (parms && TREE_CODE (TREE_VALUE (parms)) == CALL_EXPR
3157               && TREE_OPERAND (TREE_VALUE (parms), 1)
3158               && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms), 1))))
3159             parms = build_tree_list (NULL_TREE, instance_ptr);
3160           else
3161             parms = tree_cons (NULL_TREE, instance_ptr, parms);
3162         }
3163     }
3164   parmtypes = tree_cons (NULL_TREE,
3165                          build_pointer_type (build_type_variant (basetype, constp, volatilep)),
3166                          parmtypes);
3167   if (last == NULL_TREE)
3168     last = parmtypes;
3169
3170   /* Look up function name in the structure type definition.  */
3171
3172   if ((IDENTIFIER_HAS_TYPE_VALUE (name)
3173        && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name))
3174        && TREE_CODE(IDENTIFIER_TYPE_VALUE (name)) != UNINSTANTIATED_P_TYPE)
3175       || name == constructor_name (basetype))
3176     {
3177       tree tmp = NULL_TREE;
3178       if (IDENTIFIER_TYPE_VALUE (name) == basetype
3179           || name == constructor_name (basetype))
3180         tmp = TYPE_BINFO (basetype);
3181       else
3182         tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
3183       
3184       if (tmp != NULL_TREE)
3185         {
3186           name_kind = "constructor";
3187           
3188           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3189               && ! (flags & LOOKUP_HAS_IN_CHARGE))
3190             {
3191               /* Constructors called for initialization
3192                  only are never in charge.  */
3193               tree tmplist;
3194               
3195               flags |= LOOKUP_HAS_IN_CHARGE;
3196               tmplist = tree_cons (NULL_TREE, integer_zero_node,
3197                                    TREE_CHAIN (parms));
3198               TREE_CHAIN (parms) = tmplist;
3199               tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
3200               TREE_CHAIN (parmtypes) = tmplist;
3201             }
3202           basetype = BINFO_TYPE (tmp);
3203         }
3204       else
3205         name_kind = "method";
3206     }
3207   else
3208     name_kind = "method";
3209   
3210   if (basetype_path == NULL_TREE
3211       || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
3212     basetype_path = TYPE_BINFO (basetype);
3213   result = lookup_fnfields (basetype_path, name,
3214                             (flags & LOOKUP_COMPLAIN));
3215   if (result == error_mark_node)
3216     return error_mark_node;
3217
3218
3219   /* Now, go look for this method name.  We do not find destructors here.
3220
3221      Putting `void_list_node' on the end of the parmtypes
3222      fakes out `build_decl_overload' into doing the right thing.  */
3223   TREE_CHAIN (last) = void_list_node;
3224   method_name = build_decl_overload (name, parmtypes,
3225                                      1 + (name == constructor_name (save_basetype)
3226                                           || name == constructor_name_full (save_basetype)));
3227   TREE_CHAIN (last) = NULL_TREE;
3228
3229   for (pass = 0; pass < 2; pass++)
3230     {
3231       struct candidate *candidates;
3232       struct candidate *cp;
3233       int len;
3234       unsigned best = 1;
3235
3236       /* This increments every time we go up the type hierarchy.
3237          The idea is to prefer a function of the derived class if possible. */
3238       int b_or_d = 0;
3239
3240       baselink = result;
3241
3242       if (pass > 0)
3243         {
3244           candidates
3245             = (struct candidate *) alloca ((ever_seen+1)
3246                                            * sizeof (struct candidate));
3247           bzero (candidates, (ever_seen + 1) * sizeof (struct candidate));
3248           cp = candidates;
3249           len = list_length (parms);
3250           ever_seen = 0;
3251
3252           /* First see if a global function has a shot at it.  */
3253           if (flags & LOOKUP_GLOBAL)
3254             {
3255               tree friend_parms;
3256               tree parm = TREE_VALUE (parms);
3257
3258               if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
3259                 friend_parms = parms;
3260               else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
3261                 {
3262                   tree new_type;
3263                   parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
3264                   new_type = build_reference_type (TREE_TYPE (parm));
3265                   /* It is possible that this should go down a layer. */
3266                   new_type = build_type_variant (new_type,
3267                                                  TREE_READONLY (parm),
3268                                                  TREE_THIS_VOLATILE (parm));
3269                   parm = convert (new_type, parm);
3270                   friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
3271                 }
3272               else
3273                 my_friendly_abort (167);
3274
3275               cp->h_len = len;
3276               if (flag_ansi_overloading)
3277                 cp->v.ansi_harshness = (struct harshness_code *)
3278                   alloca ((len + 1) * sizeof (struct harshness_code));
3279               else
3280                 cp->v.old_harshness = (unsigned short *)
3281                   alloca ((len + 1) * sizeof (unsigned short));
3282
3283               result = build_overload_call (name, friend_parms, 0, cp);
3284               /* If it turns out to be the one we were actually looking for
3285                  (it was probably a friend function), the return the
3286                  good result.  */
3287               if (TREE_CODE (result) == CALL_EXPR)
3288                 return result;
3289
3290               if (flag_ansi_overloading)
3291                 while ((cp->h.code & EVIL_CODE) == 0)
3292                   {
3293                     /* non-standard uses: set the field to 0 to indicate
3294                        we are using a non-member function.  */
3295                     cp->u.field = 0;
3296                     if (cp->v.ansi_harshness[len].distance == 0
3297                         && cp->h.code < best)
3298                       best = cp->h.code;
3299                     cp += 1;
3300                   }
3301               else
3302                 while (cp->evil == 0)
3303                   {
3304                     /* non-standard uses: set the field to 0 to indicate
3305                        we are using a non-member function.  */
3306                     cp->u.field = 0;
3307                     if (cp->v.old_harshness[len] == 0
3308                         && cp->v.old_harshness[len] == 0
3309                         && cp->ellipsis == 0 && cp->user == 0 && cp->b_or_d == 0
3310                         && cp->easy < best)
3311                       best = cp->easy;
3312                     cp += 1;
3313                   }
3314             }
3315         }
3316
3317       while (baselink)
3318         {
3319           /* We have a hit (of sorts). If the parameter list is
3320              "error_mark_node", or some variant thereof, it won't
3321              match any methods.  Since we have verified that the is
3322              some method vaguely matching this one (in name at least),
3323              silently return.
3324              
3325              Don't stop for friends, however.  */
3326           basetype_path = TREE_PURPOSE (baselink);
3327
3328           function = TREE_VALUE (baselink);
3329           if (TREE_CODE (basetype_path) == TREE_LIST)
3330             basetype_path = TREE_VALUE (basetype_path);
3331           basetype = BINFO_TYPE (basetype_path);
3332
3333           /* Cast the instance variable to the appropriate type.  */
3334           TREE_VALUE (parms) = convert_force (TYPE_POINTER_TO (basetype),
3335                                               instance_ptr);
3336           /* FIXME: this is the wrong place to get an error.  Hopefully
3337              the access-control rewrite will make this change more cleanly.  */
3338           if (TREE_VALUE (parms) == error_mark_node)
3339             return error_mark_node;
3340
3341           if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
3342             function = DECL_CHAIN (function);
3343
3344           for (; function; function = DECL_CHAIN (function))
3345             {
3346 #ifdef GATHER_STATISTICS
3347               n_inner_fields_searched++;
3348 #endif
3349               ever_seen++;
3350               if (pass > 0)
3351                 found_fns = tree_cons (NULL_TREE, function, found_fns);
3352
3353               /* Not looking for friends here.  */
3354               if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
3355                   && ! DECL_STATIC_FUNCTION_P (function))
3356                 continue;
3357
3358               if (pass == 0
3359                   && DECL_ASSEMBLER_NAME (function) == method_name)
3360                 goto found;
3361
3362               if (pass > 0)
3363                 {
3364                   tree these_parms = parms;
3365
3366 #ifdef GATHER_STATISTICS
3367                   n_inner_fields_searched++;
3368 #endif
3369                   cp->h_len = len;
3370                   if (flag_ansi_overloading)
3371                     cp->v.ansi_harshness = (struct harshness_code *)
3372                       alloca ((len + 1) * sizeof (struct harshness_code));
3373                   else
3374                     cp->v.old_harshness = (unsigned short *)
3375                       alloca ((len + 1) * sizeof (unsigned short));
3376
3377                   if (DECL_STATIC_FUNCTION_P (function))
3378                     these_parms = TREE_CHAIN (these_parms);
3379                   compute_conversion_costs (function, these_parms, cp, len);
3380
3381                   if (!flag_ansi_overloading)
3382                       cp->b_or_d += b_or_d;
3383
3384                   if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE) == 0)
3385                       || (!flag_ansi_overloading && cp->evil == 0))
3386                     {
3387                       cp->u.field = function;
3388                       cp->function = function;
3389                       cp->basetypes = basetype_path;
3390
3391                       /* No "two-level" conversions.  */
3392                       if (flags & LOOKUP_NO_CONVERSION
3393                           && ((flag_ansi_overloading
3394                                && (cp->h.code & USER_CODE))
3395                               || (!flag_ansi_overloading
3396                                   && cp->user != 0)))
3397                         continue;
3398
3399                       /* If we used default parameters, we must
3400                          check to see whether anyone else might
3401                          use them also, and report a possible
3402                          ambiguity.  */
3403                       if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
3404                           && ((flag_ansi_overloading
3405                                && cp->v.ansi_harshness[len].distance == 0
3406                                && cp->h.code < best)
3407                               || (!flag_ansi_overloading
3408                                   && cp->v.old_harshness[len] == 0
3409                                   && CONST_HARSHNESS (cp->v.old_harshness[0]) == 0
3410                                   && cp->ellipsis == 0 && cp->user == 0 && cp->b_or_d == 0
3411                                   && cp->easy < best)))
3412                         {
3413                           if (! DECL_STATIC_FUNCTION_P (function))
3414                             TREE_VALUE (parms) = cp->arg;
3415                           if (best == 1)
3416                             goto found_and_maybe_warn;
3417                         }
3418                       cp++;
3419                     }
3420                 }
3421             }
3422           /* Now we have run through one link's member functions.
3423              arrange to head-insert this link's links.  */
3424           baselink = next_baselink (baselink);
3425           b_or_d += 1;
3426           /* Don't grab functions from base classes.  lookup_fnfield will
3427              do the work to get us down into the right place.  */
3428           baselink = NULL_TREE;
3429         }
3430       if (pass == 0)
3431         {
3432           tree igv = IDENTIFIER_GLOBAL_VALUE (name);
3433
3434           /* No exact match could be found.  Now try to find match
3435              using default conversions.  */
3436           if ((flags & LOOKUP_GLOBAL) && igv)
3437             {
3438               if (TREE_CODE (igv) == FUNCTION_DECL)
3439                 ever_seen += 1;
3440               else if (TREE_CODE (igv) == TREE_LIST)
3441                 ever_seen += count_functions (igv);
3442             }
3443
3444           if (ever_seen == 0)
3445             {
3446               if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3447                   == LOOKUP_SPECULATIVELY)
3448                 return NULL_TREE;
3449               
3450               TREE_CHAIN (last) = void_list_node;
3451               if (flags & LOOKUP_GLOBAL)
3452                 cp_error ("no global or member function `%D(%A)' defined",
3453                           name, parmtypes);
3454               else
3455                 cp_error ("no member function `%T::%D(%A)' defined",
3456                           save_basetype, name, TREE_CHAIN (parmtypes));
3457               return error_mark_node;
3458             }
3459           continue;
3460         }
3461
3462       if (cp - candidates != 0)
3463         {
3464           /* Rank from worst to best.  Then cp will point to best one.
3465              Private fields have their bits flipped.  For unsigned
3466              numbers, this should make them look very large.
3467              If the best alternate has a (signed) negative value,
3468              then all we ever saw were private members.  */
3469           if (cp - candidates > 1)
3470             {
3471               cp = ideal_candidate (save_basetype, candidates,
3472                                     cp - candidates, parms, len);
3473               if (cp == (struct candidate *)0)
3474                 {
3475                   cp_error ("ambiguous type conversion requested for %s `%D'",
3476                             name_kind, name);
3477                   return error_mark_node;
3478                 }
3479               if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE))
3480                   || (!flag_ansi_overloading && cp->evil))
3481                 return error_mark_node;
3482             }
3483           else if ((flag_ansi_overloading && (cp[-1].h.code & EVIL_CODE))
3484                    || (!flag_ansi_overloading && cp[-1].evil == 2))
3485             {
3486               cp_error ("ambiguous type conversion requested for %s `%D'",
3487                         name_kind, name);
3488               return error_mark_node;
3489             }
3490           else
3491             cp--;
3492
3493           /* The global function was the best, so use it.  */
3494           if (cp->u.field == 0)
3495             {
3496               /* We must convert the instance pointer into a reference type.
3497                  Global overloaded functions can only either take
3498                  aggregate objects (which come for free from references)
3499                  or reference data types anyway.  */
3500               TREE_VALUE (parms) = copy_node (instance_ptr);
3501               TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
3502               return build_function_call (cp->function, parms);
3503             }
3504
3505           function = cp->function;
3506           basetype_path = cp->basetypes;
3507           if (! DECL_STATIC_FUNCTION_P (function))
3508             TREE_VALUE (parms) = cp->arg;
3509           goto found_and_maybe_warn;
3510         }
3511
3512       if ((flags & ~LOOKUP_GLOBAL) & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
3513         {
3514           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3515               == LOOKUP_SPECULATIVELY)
3516             return NULL_TREE;
3517
3518           if (DECL_STATIC_FUNCTION_P (cp->function))
3519             parms = TREE_CHAIN (parms);
3520           if (ever_seen)
3521             {
3522               if (flags & LOOKUP_SPECULATIVELY)
3523                 return NULL_TREE;
3524               if (static_call_context
3525                   && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
3526                 cp_error ("object missing in call to `%D'", cp->function);
3527               else if (ever_seen > 1)
3528                 {
3529                   TREE_CHAIN (last) = void_list_node;
3530                   cp_error ("no matching function for call to `%T::%D (%A)'",
3531                             TREE_TYPE (TREE_TYPE (instance_ptr)),
3532                             name, TREE_CHAIN (parmtypes));
3533                   TREE_CHAIN (last) = NULL_TREE;
3534                   print_candidates (found_fns);
3535                 }
3536               else
3537                 report_type_mismatch (cp, parms, name_kind);
3538               return error_mark_node;
3539             }
3540
3541           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3542               == LOOKUP_COMPLAIN)
3543             {
3544               cp_error ("%T has no method named %D", save_basetype, name);
3545               return error_mark_node;
3546             }
3547           return NULL_TREE;
3548         }
3549       continue;
3550
3551     found_and_maybe_warn:
3552       if ((flag_ansi_overloading
3553            && (cp->v.ansi_harshness[0].code & CONST_CODE))
3554           || (!flag_ansi_overloading
3555               && CONST_HARSHNESS (cp->v.old_harshness[0])))
3556         {
3557           if (flags & LOOKUP_COMPLAIN)
3558             {
3559               cp_error_at ("non-const member function `%D'", cp->function);
3560               error ("called for const object at this point in file");
3561             }
3562           /* Not good enough for a match.  */
3563           else
3564             return error_mark_node;
3565         }
3566       goto found;
3567     }
3568   /* Silently return error_mark_node.  */
3569   return error_mark_node;
3570
3571  found:
3572   if (flags & LOOKUP_PROTECT)
3573     access = compute_access (basetype_path, function);
3574
3575   if (access == access_private)
3576     {
3577       if (flags & LOOKUP_COMPLAIN)
3578         {
3579           cp_error_at ("%s `%+#D' is %s", name_kind, function, 
3580                        TREE_PRIVATE (function) ? "private"
3581                        : "from private base class");
3582           error ("within this context");
3583         }
3584       return error_mark_node;
3585     }
3586   else if (access == access_protected)
3587     {
3588       if (flags & LOOKUP_COMPLAIN)
3589         {
3590           cp_error_at ("%s `%+#D' %s", name_kind, function,
3591                        TREE_PROTECTED (function) ? "is protected"
3592                        : "has protected accessibility");
3593           error ("within this context");
3594         }
3595       return error_mark_node;
3596     }
3597
3598   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
3599      type (if it exists) is a pointer to.  */
3600
3601   if (IS_SIGNATURE (basetype) && static_call_context)
3602     {
3603       cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
3604                 basetype, name);
3605       return error_mark_node;
3606         }
3607   else if (IS_SIGNATURE (basetype))
3608     return build_signature_method_call (basetype, instance, function, parms);
3609
3610   function = DECL_MAIN_VARIANT (function);
3611   /* Declare external function if necessary. */
3612   assemble_external (function);
3613
3614   fntype = TREE_TYPE (function);
3615   if (TREE_CODE (fntype) == POINTER_TYPE)
3616     fntype = TREE_TYPE (fntype);
3617   basetype = DECL_CLASS_CONTEXT (function);
3618
3619   /* If we are referencing a virtual function from an object
3620      of effectively static type, then there is no need
3621      to go through the virtual function table.  */
3622   if (need_vtbl == maybe_needed)
3623     {
3624       int fixed_type = resolves_to_fixed_type_p (instance, 0);
3625
3626       if (all_virtual == 1
3627           && DECL_VINDEX (function)
3628           && may_be_remote (basetype))
3629         need_vtbl = needed;
3630       else if (DECL_VINDEX (function))
3631         need_vtbl = fixed_type ? unneeded : needed;
3632       else
3633         need_vtbl = not_needed;
3634     }
3635
3636   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
3637       && !DECL_CONSTRUCTOR_P (function))
3638     {
3639       /* Let's be nice to the user for now, and give reasonable
3640          default behavior.  */
3641       instance_ptr = current_class_decl;
3642       if (instance_ptr)
3643         {
3644           if (basetype != current_class_type)
3645             {
3646               tree binfo = get_binfo (basetype, current_class_type, 1);
3647               if (binfo == NULL_TREE)
3648                 {
3649                   error_not_base_type (function, current_class_type);
3650                   return error_mark_node;
3651                 }
3652               else if (basetype == error_mark_node)
3653                 return error_mark_node;
3654             }
3655         }
3656       /* Only allow a static member function to call another static member
3657          function.  */
3658       else if (DECL_LANG_SPECIFIC (function)
3659                && !DECL_STATIC_FUNCTION_P (function))
3660         {
3661           cp_error ("cannot call member function `%D' without object",
3662                     function);
3663           return error_mark_node;
3664         }
3665     }
3666
3667   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3668
3669   if (TYPE_SIZE (value_type) == 0)
3670     {
3671       if (flags & LOOKUP_COMPLAIN)
3672         incomplete_type_error (0, value_type);
3673       return error_mark_node;
3674     }
3675
3676   /* We do not pass FUNCTION into `convert_arguments', because by
3677      now everything should be ok.  If not, then we have a serious error.  */
3678   if (DECL_STATIC_FUNCTION_P (function))
3679     parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
3680                                TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
3681   else if (need_vtbl == unneeded)
3682     {
3683       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
3684       basetype = TREE_TYPE (instance);
3685       if (TYPE_METHOD_BASETYPE (TREE_TYPE (function)) != TYPE_MAIN_VARIANT (basetype)
3686           && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3687         {
3688           basetype = DECL_CLASS_CONTEXT (function);
3689           instance_ptr = convert_pointer_to (basetype, instance_ptr);
3690           instance = build_indirect_ref (instance_ptr, NULL_PTR);
3691         }
3692       parms = tree_cons (NULL_TREE, instance_ptr,
3693                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
3694     }
3695   else
3696     {
3697       if ((flags & LOOKUP_NONVIRTUAL) == 0)
3698         basetype = DECL_CONTEXT (function);
3699
3700       /* First parm could be integer_zerop with casts like
3701          ((Object*)0)->Object::IsA()  */
3702       if (!integer_zerop (TREE_VALUE (parms)))
3703         {
3704           /* Since we can't have inheritance with a union, doing get_binfo
3705              on it won't work.  We do all the convert_pointer_to_real
3706              stuff to handle MI correctly...for unions, that's not
3707              an issue, so we must short-circuit that extra work here.  */
3708           tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
3709           if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
3710             instance_ptr = TREE_VALUE (parms);
3711           else
3712             {
3713               tree binfo = get_binfo (basetype,
3714                                       TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3715                                       0);
3716               instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3717             }
3718           instance_ptr
3719             = convert_pointer_to (build_type_variant (basetype,
3720                                                       constp, volatilep),
3721                                   instance_ptr);
3722
3723           if (TREE_CODE (instance_ptr) == COND_EXPR)
3724             {
3725               instance_ptr = save_expr (instance_ptr);
3726               instance = build_indirect_ref (instance_ptr, NULL_PTR);
3727             }
3728           else if (TREE_CODE (instance_ptr) == NOP_EXPR
3729                    && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
3730                    && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
3731             ;
3732           /* The call to `convert_pointer_to' may return error_mark_node.  */
3733           else if (TREE_CODE (instance_ptr) == ERROR_MARK)
3734             return instance_ptr;
3735           else if (instance == NULL_TREE
3736                    || TREE_CODE (instance) != INDIRECT_REF
3737                    || TREE_OPERAND (instance, 0) != instance_ptr)
3738             instance = build_indirect_ref (instance_ptr, NULL_PTR);
3739         }
3740       parms = tree_cons (NULL_TREE, instance_ptr,
3741                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL));
3742     }
3743
3744 #if 0
3745   /* Constructors do not overload method calls.  */
3746   else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
3747            && name != TYPE_IDENTIFIER (basetype)
3748            && (TREE_CODE (function) != FUNCTION_DECL
3749                || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)),
3750                            OPERATOR_METHOD_FORMAT,
3751                            OPERATOR_METHOD_LENGTH))
3752            && (may_be_remote (basetype) || instance != C_C_D))
3753     {
3754       tree fn_as_int;
3755
3756       parms = TREE_CHAIN (parms);
3757
3758       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
3759         fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
3760       else
3761         fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
3762       if (all_virtual == 1)
3763         fn_as_int = convert (integer_type_node, fn_as_int);
3764
3765       result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
3766
3767       if (result == NULL_TREE)
3768         {
3769           compiler_error ("could not overload `operator->()(...)'");
3770           return error_mark_node;
3771         }
3772       else if (result == error_mark_node)
3773         return error_mark_node;
3774
3775 #if 0
3776       /* Do this if we want the result of operator->() to inherit
3777          the type of the function it is subbing for.  */
3778       TREE_TYPE (result) = value_type;
3779 #endif
3780
3781       return result;
3782     }
3783 #endif
3784
3785   if (need_vtbl == needed)
3786     {
3787       function = build_vfn_ref (&TREE_VALUE (parms), instance, DECL_VINDEX (function));
3788       TREE_TYPE (function) = build_pointer_type (fntype);
3789     }
3790
3791   if (TREE_CODE (function) == FUNCTION_DECL)
3792     GNU_xref_call (current_function_decl,
3793                    IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
3794
3795   {
3796     int is_constructor;
3797     
3798     if (TREE_CODE (function) == FUNCTION_DECL)
3799       {
3800         is_constructor = DECL_CONSTRUCTOR_P (function);
3801         if (DECL_INLINE (function))
3802           function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
3803         else
3804           {
3805             assemble_external (function);
3806             TREE_USED (function) = 1;
3807             function = default_conversion (function);
3808           }
3809       }
3810     else
3811       {
3812         is_constructor = 0;
3813         function = default_conversion (function);
3814       }
3815
3816     result = build_nt (CALL_EXPR, function, parms, NULL_TREE);
3817
3818     TREE_TYPE (result) = value_type;
3819     TREE_SIDE_EFFECTS (result) = 1;
3820     TREE_RAISES (result)
3821       = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
3822     TREE_HAS_CONSTRUCTOR (result) = is_constructor;
3823     return result;
3824   }
3825 }
3826
3827 /* Similar to `build_method_call', but for overloaded non-member functions.
3828    The name of this function comes through NAME.  The name depends
3829    on PARMS.
3830
3831    Note that this function must handle simple `C' promotions,
3832    as well as variable numbers of arguments (...), and
3833    default arguments to boot.
3834
3835    If the overloading is successful, we return a tree node which
3836    contains the call to the function.
3837
3838    If overloading produces candidates which are probable, but not definite,
3839    we hold these candidates.  If FINAL_CP is non-zero, then we are free
3840    to assume that final_cp points to enough storage for all candidates that
3841    this function might generate.  The `harshness' array is preallocated for
3842    the first candidate, but not for subsequent ones.
3843
3844    Note that the DECL_RTL of FUNCTION must be made to agree with this
3845    function's new name.  */
3846
3847 tree
3848 build_overload_call_real (fnname, parms, flags, final_cp, buildxxx)
3849      tree fnname, parms;
3850      int flags;
3851      struct candidate *final_cp;
3852      int buildxxx;
3853 {
3854   /* must check for overloading here */
3855   tree overload_name, functions, function, parm;
3856   tree parmtypes = NULL_TREE, last = NULL_TREE;
3857   register tree outer;
3858   int length;
3859   int parmlength = list_length (parms);
3860
3861   struct candidate *candidates, *cp;
3862
3863   if (final_cp)
3864     {
3865       if (flag_ansi_overloading)
3866         {
3867           final_cp[0].h.code = 0;
3868           final_cp[0].h.distance = 0;
3869           final_cp[0].function = 0;
3870           /* end marker.  */
3871           final_cp[1].h.code = EVIL_CODE;
3872         }
3873       else
3874         {
3875           final_cp[0].evil = 0;
3876           final_cp[0].user = 0;
3877           final_cp[0].b_or_d = 0;
3878           final_cp[0].easy = 0;
3879           final_cp[0].function = 0;
3880           /* end marker.  */
3881           final_cp[1].evil = 1;
3882         }
3883     }
3884
3885   for (parm = parms; parm; parm = TREE_CHAIN (parm))
3886     {
3887       register tree t = TREE_TYPE (TREE_VALUE (parm));
3888
3889       if (t == error_mark_node)
3890         {
3891           if (final_cp)
3892             {
3893               if (flag_ansi_overloading)
3894                 final_cp->h.code = EVIL_CODE;
3895               else
3896                 final_cp->evil = 1;
3897             }
3898           return error_mark_node;
3899         }
3900       if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
3901         {
3902           /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3903              Also convert OFFSET_TYPE entities to their normal selves.
3904              This eliminates needless calls to `compute_conversion_costs'.  */
3905           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
3906           t = TREE_TYPE (TREE_VALUE (parm));
3907         }
3908       last = build_tree_list (NULL_TREE, t);
3909       parmtypes = chainon (parmtypes, last);
3910     }
3911   if (last)
3912     TREE_CHAIN (last) = void_list_node;
3913   else
3914     parmtypes = void_list_node;
3915
3916   if (! flag_ansi_overloading)
3917     {
3918       /* This is a speed improvement that ends up not working properly in
3919          the situation of fns with and without default parameters.  I turned
3920          this off in the new method so it'll go through the argument matching
3921          code to properly diagnose a match/failure. (bpk)  */
3922       overload_name = build_decl_overload (fnname, parmtypes, 0);
3923
3924       /* Now check to see whether or not we can win.
3925          Note that if we are called from `build_method_call',
3926          then we cannot have a mis-match, because we would have
3927          already found such a winning case.  */
3928
3929       if (IDENTIFIER_GLOBAL_VALUE (overload_name))
3930         if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (overload_name)) != TREE_LIST)
3931           return build_function_call (DECL_MAIN_VARIANT (IDENTIFIER_GLOBAL_VALUE (overload_name)), parms);
3932     }
3933
3934   functions = IDENTIFIER_GLOBAL_VALUE (fnname);
3935
3936   if (functions == NULL_TREE)
3937     {
3938       if (flags & LOOKUP_SPECULATIVELY)
3939         return NULL_TREE;
3940       if (flags & LOOKUP_COMPLAIN)
3941         error ("only member functions apply");
3942       if (final_cp)
3943         {
3944           if (flag_ansi_overloading)
3945             final_cp->h.code = EVIL_CODE;
3946           else
3947             final_cp->evil = 1;
3948         }
3949       return error_mark_node;
3950     }
3951
3952   if (! TREE_OVERLOADED (fnname))
3953     {
3954       functions = DECL_MAIN_VARIANT (functions);
3955       if (final_cp)
3956         {
3957           /* We are just curious whether this is a viable alternative or
3958              not.  */
3959           compute_conversion_costs (functions, parms, final_cp, parmlength);
3960           return functions;
3961         }
3962       else
3963         return build_function_call_real (functions, parms, 1, flags);
3964     }
3965
3966   if (TREE_CODE (functions) == TREE_LIST
3967       && TREE_VALUE (functions) == NULL_TREE)
3968     {
3969       if (flags & LOOKUP_SPECULATIVELY)
3970         return NULL_TREE;
3971       
3972       if (flags & LOOKUP_COMPLAIN)
3973         cp_error ("function `%D' declared overloaded, but no instances of that function declared",
3974                   TREE_PURPOSE (functions));
3975       if (final_cp)
3976         {
3977           if (flag_ansi_overloading)
3978             final_cp->h.code = EVIL_CODE;
3979           else
3980             final_cp->evil = 1;
3981         }
3982       return error_mark_node;
3983     }
3984
3985   length = count_functions (functions);
3986   
3987   if (final_cp)
3988     candidates = final_cp;
3989   else
3990     {
3991       candidates
3992         = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
3993       bzero (candidates, (length + 1) * sizeof (struct candidate));
3994     }
3995
3996   cp = candidates;
3997
3998   my_friendly_assert (is_overloaded_fn (functions), 169);
3999
4000   functions = get_first_fn (functions);
4001
4002   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
4003   for (outer = functions; outer; outer = DECL_CHAIN (outer))
4004     {
4005       int template_cost = 0;
4006       function = outer;
4007       if (TREE_CODE (function) != FUNCTION_DECL
4008           && ! (TREE_CODE (function) == TEMPLATE_DECL
4009                 && ! DECL_TEMPLATE_IS_CLASS (function)
4010                 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
4011         {
4012           enum tree_code code = TREE_CODE (function);
4013           if (code == TEMPLATE_DECL)
4014             code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
4015           if (code == CONST_DECL)
4016             cp_error_at
4017               ("enumeral value `%D' conflicts with function of same name",
4018                function);
4019           else if (code == VAR_DECL)
4020             {
4021               if (TREE_STATIC (function))
4022                 cp_error_at
4023                   ("variable `%D' conflicts with function of same name",
4024                    function);
4025               else
4026                 cp_error_at
4027                   ("constant field `%D' conflicts with function of same name",
4028                    function);
4029             }
4030           else if (code == TYPE_DECL)
4031             continue;
4032           else
4033             my_friendly_abort (2);
4034           error ("at this point in file");
4035           continue;
4036         }
4037       if (TREE_CODE (function) == TEMPLATE_DECL)
4038         {
4039           int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
4040           tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
4041           int i;
4042
4043           i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
4044                                 TYPE_ARG_TYPES (TREE_TYPE (function)),
4045                                 parms, &template_cost, 0);
4046           if (i == 0)
4047             {
4048               struct candidate *cp2;
4049
4050               function = instantiate_template (function, targs);
4051               /* Now check that the template instantiated for this is not
4052                  the same as a function that's in the list due to some
4053                  previous instantiation.  */
4054               cp2 = candidates;
4055               while (cp2 != cp)
4056                 if (cp2->function == function)
4057                   break;
4058                 else
4059                   cp2 += 1;
4060               if (cp2->function == function)
4061                 continue;
4062             }
4063         }
4064
4065       if (TREE_CODE (function) == TEMPLATE_DECL)
4066         {
4067           /* Unconverted template -- failed match.  */
4068           cp->function = function;
4069           cp->u.bad_arg = -4;
4070           if (flag_ansi_overloading)
4071             cp->h.code = EVIL_CODE;
4072           else
4073             cp->evil = 1;
4074         }
4075       else
4076         {
4077           function = DECL_MAIN_VARIANT (function);
4078
4079           /* Can't use alloca here, since result might be
4080              passed to calling function.  */
4081           cp->h_len = parmlength;
4082           if (flag_ansi_overloading)
4083             cp->v.ansi_harshness = (struct harshness_code *)
4084               oballoc ((parmlength + 1) * sizeof (struct harshness_code));
4085           else
4086             cp->v.old_harshness = (unsigned short *)
4087               oballoc ((parmlength + 1) * sizeof (unsigned short));
4088
4089           compute_conversion_costs (function, parms, cp, parmlength);
4090
4091           if (flag_ansi_overloading)
4092             /* Make sure this is clear as well.  */
4093             cp->h.int_penalty += template_cost;
4094           else
4095             /* Should really add another field...  */
4096             cp->easy = cp->easy * 128 + template_cost;
4097
4098           /* It seemed easier to have both if stmts in here, rather
4099              than excluding the hell out of it with flag_ansi_overloading
4100              everywhere. (bpk) */
4101           if (flag_ansi_overloading)
4102             {
4103               if ((cp[0].h.code & EVIL_CODE) == 0)
4104                 {
4105                   cp[1].h.code = EVIL_CODE;
4106
4107                   /* int_penalty is set by convert_harshness_ansi for cases
4108                      where we need to know about any penalties that would
4109                      otherwise make a TRIVIAL_CODE pass.  */
4110                   if (final_cp
4111                       && template_cost == 0
4112                       && cp[0].h.code <= TRIVIAL_CODE
4113                       && cp[0].h.int_penalty == 0)
4114                     {
4115                       final_cp[0].h = cp[0].h;
4116                       return function;
4117                     }
4118                   cp++;
4119                 }
4120             }
4121           else
4122             {
4123               if (cp[0].evil == 0)
4124                 {
4125                   cp[1].evil = 1;
4126                   if (final_cp
4127                       && cp[0].user == 0 && cp[0].b_or_d == 0
4128                       && template_cost == 0
4129                       && cp[0].easy <= 1)
4130                     {
4131                       final_cp[0].easy = cp[0].easy;
4132                       return function;
4133                     }
4134                   cp++;
4135                 }
4136             }
4137         }
4138     }
4139
4140   if (cp - candidates)
4141     {
4142       tree rval = error_mark_node;
4143
4144       /* Leave marker.  */
4145       if (flag_ansi_overloading)
4146         cp[0].h.code = EVIL_CODE;
4147       else
4148         cp[0].evil = 1;
4149       if (cp - candidates > 1)
4150         {
4151           struct candidate *best_cp
4152             = ideal_candidate (NULL_TREE, candidates,
4153                                cp - candidates, parms, parmlength);
4154           if (best_cp == (struct candidate *)0)
4155             {
4156               if (flags & LOOKUP_COMPLAIN)
4157                 {
4158                   cp_error ("call of overloaded `%D' is ambiguous", fnname);
4159                   print_n_candidates (candidates, cp - candidates);
4160                 }
4161               return error_mark_node;
4162             }
4163           else
4164             rval = best_cp->function;
4165         }
4166       else
4167         {
4168           cp -= 1;
4169           if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE))
4170               || (!flag_ansi_overloading && cp->evil > 1))
4171             {
4172               if (flags & LOOKUP_COMPLAIN)
4173                 error ("type conversion ambiguous");
4174             }
4175           else
4176             rval = cp->function;
4177         }
4178
4179       if (final_cp)
4180         return rval;
4181
4182       return buildxxx ? build_function_call_real (rval, parms, 0, flags)
4183         : build_function_call_real (rval, parms, 1, flags);
4184     }
4185
4186   if (flags & LOOKUP_SPECULATIVELY)
4187     return NULL_TREE;
4188   
4189   if (flags & LOOKUP_COMPLAIN)
4190     report_type_mismatch (cp, parms, "function",
4191                           decl_as_string (cp->function, 1));
4192
4193   return error_mark_node;
4194 }
4195
4196 tree
4197 build_overload_call (fnname, parms, flags, final_cp)
4198      tree fnname, parms;
4199      int flags;
4200      struct candidate *final_cp;
4201 {
4202   return build_overload_call_real (fnname, parms, flags, final_cp, 0);
4203 }
4204
4205 tree
4206 build_overload_call_maybe (fnname, parms, flags, final_cp)
4207      tree fnname, parms;
4208      int flags;
4209      struct candidate *final_cp;
4210 {
4211   return build_overload_call_real (fnname, parms, flags, final_cp, 1);
4212 }