OSDN Git Service

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