OSDN Git Service

c0cdf8b96aec9942945c044bb3a6018c586bf15b
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
1 /* Functions related to invoking methods and overloaded functions.
2    Copyright (C) 1987, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com) and
4    hacked by Brendan Kehoe (brendan@cygnus.com).
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* High-level class interface.  */
25
26 #include "config.h"
27 #include "tree.h"
28 #include <stdio.h>
29 #include "cp-tree.h"
30 #include "class.h"
31 #include "output.h"
32 #include "flags.h"
33
34 #include "obstack.h"
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
37
38 extern void sorry ();
39
40 extern int inhibit_warnings;
41 extern tree ctor_label, dtor_label;
42
43 /* Compute the ease with which a conversion can be performed
44    between an expected and the given type.  */
45
46 static struct harshness_code convert_harshness PROTO((register tree, register tree, tree));
47
48 #define EVIL_RETURN(ARG)        ((ARG).code = EVIL_CODE, (ARG))
49 #define STD_RETURN(ARG)         ((ARG).code = STD_CODE, (ARG))
50 #define QUAL_RETURN(ARG)        ((ARG).code = QUAL_CODE, (ARG))
51 #define TRIVIAL_RETURN(ARG)     ((ARG).code = TRIVIAL_CODE, (ARG))
52 #define ZERO_RETURN(ARG)        ((ARG).code = 0, (ARG))
53
54 /* Ordering function for overload resolution.  Compare two candidates
55    by gross quality.  */
56
57 int
58 rank_for_overload (x, y)
59      struct candidate *x, *y;
60 {
61   if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
62     return y->h.code - x->h.code;
63   if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
64     return -1;
65
66   /* This is set by compute_conversion_costs, for calling a non-const
67      member function from a const member function.  */
68   if ((y->harshness[0].code & CONST_CODE) ^ (x->harshness[0].code & CONST_CODE))
69     return y->harshness[0].code - x->harshness[0].code;
70
71   if (y->h.code & STD_CODE)
72     {
73       if (x->h.code & STD_CODE)
74         return y->h.distance - x->h.distance;
75       return 1;
76     }
77   if (x->h.code & STD_CODE)
78     return -1;
79
80   return y->h.code - x->h.code;
81 }
82
83 /* Compare two candidates, argument by argument.  */
84
85 int
86 rank_for_ideal (x, y)
87      struct candidate *x, *y;
88 {
89   int i;
90
91   if (x->h_len != y->h_len)
92     abort ();
93
94   for (i = 0; i < x->h_len; i++)
95     {
96       if (y->harshness[i].code - x->harshness[i].code)
97         return y->harshness[i].code - x->harshness[i].code;
98       if ((y->harshness[i].code & STD_CODE)
99           && (y->harshness[i].distance - x->harshness[i].distance))
100         return y->harshness[i].distance - x->harshness[i].distance;
101
102       /* They're both the same code.  Now see if we're dealing with an
103          integral promotion that needs a finer grain of accuracy.  */
104       if (y->harshness[0].code & PROMO_CODE
105           && (y->harshness[i].int_penalty ^ x->harshness[i].int_penalty))
106         return y->harshness[i].int_penalty - x->harshness[i].int_penalty;
107     }
108   return 0;
109 }
110
111 /* TYPE is the type we wish to convert to.  PARM is the parameter
112    we have to work with.  We use a somewhat arbitrary cost function
113    to measure this conversion.  */
114
115 static struct harshness_code
116 convert_harshness (type, parmtype, parm)
117      register tree type, parmtype;
118      tree parm;
119 {
120   struct harshness_code h;
121   register enum tree_code codel;
122   register enum tree_code coder;
123   int lvalue;
124
125   h.code = 0;
126   h.distance = 0;
127   h.int_penalty = 0;
128
129 #ifdef GATHER_STATISTICS
130   n_convert_harshness++;
131 #endif
132
133   if (TREE_CODE (parmtype) == REFERENCE_TYPE)
134     {
135       if (parm)
136         parm = convert_from_reference (parm);
137       parmtype = TREE_TYPE (parmtype);
138       lvalue = 1;
139     }
140   else if (parm)
141     lvalue = lvalue_p (parm);
142   else
143     lvalue = 0;
144
145   if (TYPE_PTRMEMFUNC_P (type))
146     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
147   if (TYPE_PTRMEMFUNC_P (parmtype))
148     parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
149
150   codel = TREE_CODE (type);
151   coder = TREE_CODE (parmtype);
152
153   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
154     return ZERO_RETURN (h);
155
156   if (coder == ERROR_MARK)
157     return EVIL_RETURN (h);
158
159   if (codel == REFERENCE_TYPE)
160     {
161       tree ttl, ttr;
162       int constp = parm ? TREE_READONLY (parm) : TYPE_READONLY (parmtype);
163       int volatilep = (parm ? TREE_THIS_VOLATILE (parm)
164                        : TYPE_VOLATILE (parmtype));
165       register tree intype = TYPE_MAIN_VARIANT (parmtype);
166       register enum tree_code form = TREE_CODE (intype);
167       int penalty = 0;
168
169       ttl = TREE_TYPE (type);
170
171       /* Only allow const reference binding if we were given a parm to deal
172          with, since it isn't really a conversion.  This is a hack to
173          prevent build_type_conversion from finding this conversion, but
174          still allow overloading to find it.  */
175       if (! lvalue && ! (parm && TYPE_READONLY (ttl)))
176         return EVIL_RETURN (h);
177
178       if ((TYPE_READONLY (ttl) < constp)
179           || (TYPE_VOLATILE (ttl) < volatilep))
180         return EVIL_RETURN (h);
181
182       /* When passing a non-const argument into a const reference, dig it a
183          little, so a non-const reference is preferred over this one.  */
184       penalty = ((TYPE_READONLY (ttl) > constp)
185                  + (TYPE_VOLATILE (ttl) > volatilep));
186
187       ttl = TYPE_MAIN_VARIANT (ttl);
188
189       if (form == OFFSET_TYPE)
190         {
191           intype = TREE_TYPE (intype);
192           form = TREE_CODE (intype);
193         }
194
195       ttr = intype;
196
197       if (TREE_CODE (ttl) == ARRAY_TYPE && TREE_CODE (ttr) == ARRAY_TYPE)
198         {
199           if (comptypes (ttl, ttr, 1))
200             return ZERO_RETURN (h);
201           return EVIL_RETURN (h);
202         }
203
204       h = convert_harshness (ttl, ttr, NULL_TREE);
205       if (penalty && h.code == 0)
206         {
207           h.code = QUAL_CODE;
208           h.int_penalty = penalty;
209         }
210       return h;
211     }
212
213   if (codel == POINTER_TYPE && fntype_p (parmtype))
214     {
215       tree p1, p2;
216       struct harshness_code h1, h2;
217
218       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
219       type = TREE_TYPE (type);
220
221       if (coder == POINTER_TYPE)
222         {
223           parmtype = TREE_TYPE (parmtype);
224           coder = TREE_CODE (parmtype);
225         }
226
227       if (coder != TREE_CODE (type))
228         return EVIL_RETURN (h);
229
230       if (type != parmtype && coder == METHOD_TYPE)
231         {
232           tree ttl = TYPE_METHOD_BASETYPE (type);
233           tree ttr = TYPE_METHOD_BASETYPE (parmtype);
234
235           int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
236           if (b_or_d < 0)
237             {
238               b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
239               if (b_or_d < 0)
240                 return EVIL_RETURN (h);
241               h.distance = -b_or_d;
242             }
243           else
244             h.distance = b_or_d;
245           h.code = STD_CODE;
246
247           type = build_function_type
248             (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
249           parmtype = build_function_type
250             (TREE_TYPE (parmtype), TREE_CHAIN (TYPE_ARG_TYPES (parmtype)));
251         }
252
253       /* We allow the default conversion between function type
254          and pointer-to-function type for free.  */
255       if (comptypes (type, parmtype, 1))
256         return h;
257
258       if (pedantic)
259         return EVIL_RETURN (h);
260
261       /* Compare return types.  */
262       p1 = TREE_TYPE (type);
263       p2 = TREE_TYPE (parmtype);
264       h2 = convert_harshness (p1, p2, NULL_TREE);
265       if (h2.code & EVIL_CODE)
266         return h2;
267
268       h1.code = TRIVIAL_CODE;
269       h1.distance = 0;
270
271       if (h2.distance != 0)
272         {
273           tree binfo;
274
275           /* This only works for pointers.  */
276           if (TREE_CODE (p1) != POINTER_TYPE
277               && TREE_CODE (p1) != REFERENCE_TYPE)
278             return EVIL_RETURN (h);
279
280           p1 = TREE_TYPE (p1);
281           p2 = TREE_TYPE (p2);
282           /* Don't die if we happen to be dealing with void*.  */
283           if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
284             return EVIL_RETURN (h);
285           if (h2.distance < 0)
286             binfo = get_binfo (p2, p1, 0);
287           else
288             binfo = get_binfo (p1, p2, 0);
289
290           if (! BINFO_OFFSET_ZEROP (binfo))
291             {
292 #if 0
293               static int explained = 0;
294               if (h2.distance < 0)
295                 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p2, p1);
296               else
297                 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p1, p2);
298
299               if (! explained++)
300                 sorry ("(because pointer values change during conversion)");
301 #endif
302               return EVIL_RETURN (h);
303             }
304         }
305
306       h1.code |= h2.code;
307       if (h2.distance > h1.distance)
308         h1.distance = h2.distance;
309
310       p1 = TYPE_ARG_TYPES (type);
311       p2 = TYPE_ARG_TYPES (parmtype);
312       while (p1 && TREE_VALUE (p1) != void_type_node
313              && p2 && TREE_VALUE (p2) != void_type_node)
314         {
315           h2 = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2),
316                                        NULL_TREE);
317           if (h2.code & EVIL_CODE)
318             return h2;
319
320           if (h2.distance)
321             {
322               /* This only works for pointers and references.  */
323               if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
324                   && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
325                 return EVIL_RETURN (h);
326               h2.distance = - h2.distance;
327             }
328
329           h1.code |= h2.code;
330           if (h2.distance > h1.distance)
331             h1.distance = h2.distance;
332           p1 = TREE_CHAIN (p1);
333           p2 = TREE_CHAIN (p2);
334         }
335       if (p1 == p2)
336         return h1;
337       if (p2)
338         {
339           if (p1)
340             return EVIL_RETURN (h);
341           h1.code |= ELLIPSIS_CODE;
342           return h1;
343         }
344       if (p1)
345         {
346           if (TREE_PURPOSE (p1) == NULL_TREE)
347             h1.code |= EVIL_CODE;
348           return h1;
349         }
350     }
351   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
352     {
353       tree ttl, ttr;
354
355       /* Get to the OFFSET_TYPE that this might be.  */
356       type = TREE_TYPE (type);
357
358       if (coder != TREE_CODE (type))
359         return EVIL_RETURN (h);
360
361       ttl = TYPE_OFFSET_BASETYPE (type);
362       ttr = TYPE_OFFSET_BASETYPE (parmtype);
363
364       if (ttl == ttr)
365         h.code = 0;
366       else
367         {
368           int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
369           if (b_or_d < 0)
370             {
371               b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
372               if (b_or_d < 0)
373                 return EVIL_RETURN (h);
374               h.distance = -b_or_d;
375             }
376           else
377             h.distance = b_or_d;
378           h.code = STD_CODE;
379         }
380
381       /* Now test the OFFSET_TYPE's target compatibility.  */
382       type = TREE_TYPE (type);
383       parmtype = TREE_TYPE (parmtype);
384     }
385
386   if (coder == UNKNOWN_TYPE)
387     {
388       if (codel == FUNCTION_TYPE
389           || codel == METHOD_TYPE
390           || (codel == POINTER_TYPE
391               && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
392                   || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
393         return TRIVIAL_RETURN (h);
394       return EVIL_RETURN (h);
395     }
396
397   if (coder == VOID_TYPE)
398     return EVIL_RETURN (h);
399
400   if (codel == BOOLEAN_TYPE)
401     {
402       if (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE)
403         return STD_RETURN (h);
404       else if (coder == POINTER_TYPE || coder == OFFSET_TYPE)
405         {
406           /* Make this worse than any conversion to another pointer.
407              FIXME this is how I think the language should work, but it may not
408              end up being how the language is standardized (jason 1/30/95).  */
409           h.distance = 32767;
410           return STD_RETURN (h);
411         }
412       return EVIL_RETURN (h);
413     }
414
415   if (INTEGRAL_CODE_P (codel))
416     {
417       /* Control equivalence of ints an enums.  */
418
419       if (codel == ENUMERAL_TYPE
420           && flag_int_enum_equivalence == 0)
421         {
422           /* Enums can be converted to ints, but not vice-versa.  */
423           if (coder != ENUMERAL_TYPE
424               || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
425             return EVIL_RETURN (h);
426         }
427
428       /* else enums and ints (almost) freely interconvert.  */
429
430       if (INTEGRAL_CODE_P (coder))
431         {
432           if (TYPE_MAIN_VARIANT (type)
433               == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
434             {
435               h.code = PROMO_CODE;
436             }
437           else
438             h.code = STD_CODE;
439             
440           return h;
441         }
442       else if (coder == REAL_TYPE)
443         {
444           h.code = STD_CODE;
445           h.distance = 0;
446           return h;
447         }
448     }
449
450   if (codel == REAL_TYPE)
451     {
452       if (coder == REAL_TYPE)
453         {
454           if (TYPE_MAIN_VARIANT (type)
455               == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
456             h.code = PROMO_CODE;
457           else
458             h.code = STD_CODE;
459             
460           return h;
461         }
462       else if (INTEGRAL_CODE_P (coder))
463         {
464           h.code = STD_CODE;
465           h.distance = 0;
466           return h;
467         }
468     }
469
470   /* Convert arrays which have not previously been converted.  */
471   if (coder == ARRAY_TYPE)
472     {
473       coder = POINTER_TYPE;
474       if (parm)
475         {
476           parm = decay_conversion (parm);
477           parmtype = TREE_TYPE (parm);
478         }
479       else
480         parmtype = build_pointer_type (TREE_TYPE (parmtype));
481     }
482
483   /* Conversions among pointers */
484   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
485     {
486       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
487       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
488       int penalty = 4 * (ttl != ttr);
489
490       /* Anything converts to void *.  Since this may be `const void *'
491          (etc.) use VOID_TYPE instead of void_type_node.  Otherwise, the
492          targets must be the same, except that we do allow (at some cost)
493          conversion between signed and unsigned pointer types.  */
494
495       if ((TREE_CODE (ttl) == METHOD_TYPE
496            || TREE_CODE (ttl) == FUNCTION_TYPE)
497           && TREE_CODE (ttl) == TREE_CODE (ttr))
498         {
499           if (comptypes (ttl, ttr, -1))
500             {
501               h.code = penalty ? STD_CODE : 0;
502               h.distance =  0;
503             }
504           else
505             h.code = EVIL_CODE;
506           return h;
507         }
508
509 #if 1
510       if (TREE_CODE (ttl) != VOID_TYPE
511           && (TREE_CODE (ttr) != VOID_TYPE || !parm || !integer_zerop (parm)))
512         {
513           if (comp_target_types (type, parmtype, 1) <= 0)
514             return EVIL_RETURN (h);
515         }
516 #else
517       if (!(TREE_CODE (ttl) == VOID_TYPE
518             || TREE_CODE (ttr) == VOID_TYPE
519             || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
520                 && (ttl = unsigned_type (ttl),
521                     ttr = unsigned_type (ttr),
522                     penalty = 10, 0))
523             || (comp_target_types (ttl, ttr, 0) > 0)))
524         return EVIL_RETURN (h);
525 #endif
526
527       if (ttr == ttl)
528         {
529           tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
530
531           h.code = 0;
532           /* Note conversion from `T*' to `const T*',
533                                or `T*' to `volatile T*'.  */
534           if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
535               || (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
536             h.code = EVIL_CODE;
537           else if ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
538                    || (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2)))
539             h.code |= QUAL_CODE;
540
541           h.distance = 0;
542           return h;
543         }
544
545
546       if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
547         {
548           int b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
549           if (b_or_d < 0)
550             {
551               b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
552               if (b_or_d < 0)
553                 return EVIL_RETURN (h);
554               h.distance = -b_or_d;
555             }
556           else
557             h.distance = b_or_d;
558           h.code = STD_CODE;
559           return h;
560         }
561
562       /* If converting from a `class*' to a `void*', make it
563          less favorable than any inheritance relationship.  */
564       if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
565         {
566           h.code = STD_CODE;
567           h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
568           return h;
569         }
570
571       h.code = penalty ? STD_CODE : PROMO_CODE;
572       /* Catch things like `const char *' -> `const void *'
573          vs `const char *' -> `void *'.  */
574       if (ttl != ttr)
575         {
576           tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
577           if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
578               || (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
579             h.code = EVIL_CODE;
580           else if ((TYPE_READONLY (tmp1) > TREE_READONLY (tmp2))
581                    || (TYPE_VOLATILE (tmp1) > TYPE_VOLATILE (tmp2)))
582             h.code |= QUAL_CODE;
583         }
584       return h;
585     }
586
587   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
588     {
589       /* This is not a bad match, but don't let it beat
590          integer-enum combinations.  */
591       if (parm && integer_zerop (parm))
592         {
593           h.code = STD_CODE;
594           h.distance = 0;
595           return h;
596         }
597     }
598
599   /* C++: Since the `this' parameter of a signature member function
600      is represented as a signature pointer to handle default implementations
601      correctly, we can have the case that `type' is a signature pointer
602      while `parmtype' is a pointer to a signature table.  We don't really
603      do any conversions in this case, so just return 0.  */
604
605   if (codel == RECORD_TYPE && coder == POINTER_TYPE
606       && IS_SIGNATURE_POINTER (type) && IS_SIGNATURE (TREE_TYPE (parmtype)))
607     return ZERO_RETURN (h);
608
609   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
610     {
611       int b_or_d = get_base_distance (type, parmtype, 0, (tree*)0);
612       if (b_or_d < 0)
613         {
614           b_or_d = get_base_distance (parmtype, type, 0, (tree*)0);
615           if (b_or_d < 0)
616             return EVIL_RETURN (h);
617           h.distance = -b_or_d;
618         }
619       else
620         h.distance = b_or_d;
621       h.code = STD_CODE;
622       return h;
623     }
624   return EVIL_RETURN (h);
625 }
626
627 /* A clone of build_type_conversion for checking user-defined conversions in
628    overload resolution.  */
629
630 int
631 user_harshness (type, parmtype)
632      register tree type, parmtype;
633 {
634   tree conv;
635   tree winner = NULL_TREE;
636   int code;
637
638   {
639     tree typename = build_typename_overload (type);
640     if (lookup_fnfields (TYPE_BINFO (parmtype), typename, 0))
641       return 0;
642   }
643                         
644   for (conv = lookup_conversions (parmtype); conv; conv = TREE_CHAIN (conv))
645     {
646       struct harshness_code tmp;
647       tree cand = TREE_VALUE (conv);
648
649       if (winner && winner == cand)
650         continue;
651
652       tmp = convert_harshness (type, TREE_TYPE (TREE_TYPE (cand)), NULL_TREE);
653       if ((tmp.code < USER_CODE) && (tmp.distance >= 0))
654         {
655           if (winner)
656             return EVIL_CODE;
657           else
658             {
659               winner = cand;
660               code = tmp.code;
661             }
662         }
663     }
664
665   if (winner)
666     return code;
667
668   return -1;
669 }
670
671 int
672 can_convert (to, from)
673      tree to, from;
674 {
675   struct harshness_code h;
676   h = convert_harshness (to, from, NULL_TREE);
677   return (h.code < USER_CODE) && (h.distance >= 0);
678 }
679
680 int
681 can_convert_arg (to, from, arg)
682      tree to, from, arg;
683 {
684   struct harshness_code h;
685   h = convert_harshness (to, from, arg);
686   return (h.code < USER_CODE) && (h.distance >= 0);
687 }
688
689 #ifdef DEBUG_MATCHING
690 static char *
691 print_harshness (h)
692      struct harshness_code *h;
693 {
694   static char buf[1024];
695   char tmp[1024];
696
697   bzero (buf, 1024 * sizeof (char));
698   strcat (buf, "codes=[");
699   if (h->code & EVIL_CODE)
700     strcat (buf, "EVIL");
701   if (h->code & CONST_CODE)
702     strcat (buf, " CONST");
703   if (h->code & ELLIPSIS_CODE)
704     strcat (buf, " ELLIPSIS");
705   if (h->code & USER_CODE)
706     strcat (buf, " USER");
707   if (h->code & STD_CODE)
708     strcat (buf, " STD");
709   if (h->code & PROMO_CODE)
710     strcat (buf, " PROMO");
711   if (h->code & QUAL_CODE)
712     strcat (buf, " QUAL");
713   if (h->code & TRIVIAL_CODE)
714     strcat (buf, " TRIVIAL");
715   if (buf[0] == '\0')
716     strcat (buf, "0");
717
718   sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
719
720   strcat (buf, tmp);
721
722   return buf;
723 }
724 #endif
725
726 /* Algorithm: For each argument, calculate how difficult it is to
727    make FUNCTION accept that argument.  If we can easily tell that
728    FUNCTION won't be acceptable to one of the arguments, then we
729    don't need to compute the ease of converting the other arguments,
730    since it will never show up in the intersection of all arguments'
731    favorite functions.
732
733    Conversions between builtin and user-defined types are allowed, but
734    no function involving such a conversion is preferred to one which
735    does not require such a conversion.  Furthermore, such conversions
736    must be unique.  */
737
738 void
739 compute_conversion_costs (function, tta_in, cp, arglen)
740      tree function;
741      tree tta_in;
742      struct candidate *cp;
743      int arglen;
744 {
745   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
746   tree ttf = ttf_in;
747   tree tta = tta_in;
748
749   /* Start out with no strikes against.  */
750   int evil_strikes = 0;
751   int ellipsis_strikes = 0;
752   int user_strikes = 0;
753   int b_or_d_strikes = 0;
754   int easy_strikes = 0;
755
756   int strike_index = 0, win;
757   struct harshness_code lose;
758   extern int cp_silent;
759
760 #ifdef GATHER_STATISTICS
761   n_compute_conversion_costs++;
762 #endif
763
764 #ifndef DEBUG_MATCHING
765   /* We don't emit any warnings or errors while trying out each candidate.  */
766   cp_silent = 1;
767 #endif
768
769   cp->function = function;
770   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
771   cp->u.bad_arg = 0;            /* optimistic!  */
772
773   cp->h.code = 0;
774   cp->h.distance = 0;
775   cp->h.int_penalty = 0;
776   bzero ((char *) cp->harshness,
777          (cp->h_len + 1) * sizeof (struct harshness_code));
778
779   while (ttf && tta)
780     {
781       struct harshness_code h;
782
783       if (ttf == void_list_node)
784         break;
785
786       if (type_unknown_p (TREE_VALUE (tta)))
787         {         
788           /* Must perform some instantiation here.  */
789           tree rhs = TREE_VALUE (tta);
790           tree lhstype = TREE_VALUE (ttf);
791
792           /* Keep quiet about possible contravariance violations.  */
793           int old_inhibit_warnings = inhibit_warnings;
794           inhibit_warnings = 1;
795
796           /* @@ This is to undo what `grokdeclarator' does to
797              parameter types.  It really should go through
798              something more general.  */
799
800           TREE_TYPE (tta) = unknown_type_node;
801           rhs = instantiate_type (lhstype, rhs, 0);
802           inhibit_warnings = old_inhibit_warnings;
803
804           if (TREE_CODE (rhs) == ERROR_MARK)
805             h.code = EVIL_CODE;
806           else
807             h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
808         }
809       else
810         {
811 #ifdef DEBUG_MATCHING
812           static tree old_function = NULL_TREE;
813
814           if (!old_function || function != old_function)
815             {
816               cp_error ("trying %D", function);
817               old_function = function;
818             }
819
820           cp_error ("      doing (%T) %E against arg %T",
821                     TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
822                     TREE_VALUE (ttf));
823 #endif
824
825           h = convert_harshness (TREE_VALUE (ttf),
826                                  TREE_TYPE (TREE_VALUE (tta)),
827                                  TREE_VALUE (tta));
828
829 #ifdef DEBUG_MATCHING
830           cp_error ("     evaluated %s", print_harshness (&h));
831 #endif
832         }
833
834       cp->harshness[strike_index] = h;
835       if ((h.code & EVIL_CODE)
836           || ((h.code & STD_CODE) && h.distance < 0))
837         {
838           cp->u.bad_arg = strike_index;
839           evil_strikes = 1;
840         }
841      else if (h.code & ELLIPSIS_CODE)
842        ellipsis_strikes += 1;
843 #if 0
844       /* This is never set by `convert_harshness'.  */
845       else if (h.code & USER_CODE)
846         {
847           user_strikes += 1;
848         }
849 #endif
850       else
851         {
852           if ((h.code & STD_CODE) && h.distance)
853             {
854               if (h.distance > b_or_d_strikes)
855                 b_or_d_strikes = h.distance;
856             }
857           else
858             easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
859           cp->h.code |= h.code;
860           /* Make sure we communicate this.  */
861           cp->h.int_penalty += h.int_penalty;
862         }
863
864       ttf = TREE_CHAIN (ttf);
865       tta = TREE_CHAIN (tta);
866       strike_index += 1;
867     }
868
869   if (tta)
870     {
871       /* ran out of formals, and parmlist is fixed size.  */
872       if (ttf /* == void_type_node */)
873         {
874           cp->h.code = EVIL_CODE;
875           cp->u.bad_arg = -1;
876           cp_silent = 0;
877           return;
878         }
879       else
880         {
881           struct harshness_code h;
882           int l = list_length (tta);
883           ellipsis_strikes += l;
884           h.code = ELLIPSIS_CODE;
885           h.distance = 0;
886           h.int_penalty = 0;
887           for (; l; --l)
888             cp->harshness[strike_index++] = h;
889         }
890     }
891   else if (ttf && ttf != void_list_node)
892     {
893       /* ran out of actuals, and no defaults.  */
894       if (TREE_PURPOSE (ttf) == NULL_TREE)
895         {
896           cp->h.code = EVIL_CODE;
897           cp->u.bad_arg = -2;
898           cp_silent = 0;
899           return;
900         }
901       /* Store index of first default.  */
902       cp->harshness[arglen].distance = strike_index+1;
903     }
904   else
905     cp->harshness[arglen].distance = 0;
906
907   /* Argument list lengths work out, so don't need to check them again.  */
908   if (evil_strikes)
909     {
910       /* We do not check for derived->base conversions here, since in
911          no case would they give evil strike counts, unless such conversions
912          are somehow ambiguous.  */
913
914       /* See if any user-defined conversions apply.
915          But make sure that we do not loop.  */
916       static int dont_convert_types = 0;
917
918       if (dont_convert_types)
919         {
920           cp->h.code = EVIL_CODE;
921           cp_silent = 0;
922           return;
923         }
924
925       win = 0;                  /* Only get one chance to win.  */
926       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
927       tta = tta_in;
928       strike_index = 0;
929       evil_strikes = 0;
930
931       while (ttf && tta)
932         {
933           if (ttf == void_list_node)
934             break;
935
936           lose = cp->harshness[strike_index];
937           if ((lose.code & EVIL_CODE)
938               || ((lose.code & STD_CODE) && lose.distance < 0))
939             {
940               tree actual_type = TREE_TYPE (TREE_VALUE (tta));
941               tree formal_type = TREE_VALUE (ttf);
942               int extra_conversions = 0;
943
944               dont_convert_types = 1;
945
946               if (TREE_CODE (formal_type) == REFERENCE_TYPE)
947                 formal_type = TREE_TYPE (formal_type);
948               if (TREE_CODE (actual_type) == REFERENCE_TYPE)
949                 actual_type = TREE_TYPE (actual_type);
950
951               if (formal_type != error_mark_node
952                   && actual_type != error_mark_node)
953                 {
954                   formal_type = complete_type (TYPE_MAIN_VARIANT (formal_type));
955                   actual_type = complete_type (TYPE_MAIN_VARIANT (actual_type));
956
957                   if (TYPE_HAS_CONSTRUCTOR (formal_type))
958                     {
959                       /* If it has a constructor for this type,
960                          try to use it.  */
961                       /* @@ There is no way to save this result yet, so
962                          success is a NULL_TREE for now.  */
963                       if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
964                           != error_mark_node)
965                         win++;
966                     }
967                   if (TYPE_LANG_SPECIFIC (actual_type)
968                       && TYPE_HAS_CONVERSION (actual_type))
969                     {
970                       int extra = user_harshness (formal_type, actual_type);
971
972                       if (extra == EVIL_CODE)
973                         win += 2;
974                       else if (extra >= 0)
975                         {
976                           win++;
977                           extra_conversions = extra;
978                         }
979                     }
980                 }
981               dont_convert_types = 0;
982
983               if (win == 1)
984                 {
985                   user_strikes += 1;
986                   cp->harshness[strike_index].code
987                     = USER_CODE | (extra_conversions ? STD_CODE : 0);
988                   win = 0;
989                 }
990               else
991                 {
992                   if (cp->u.bad_arg > strike_index)
993                     cp->u.bad_arg = strike_index;
994
995                   evil_strikes = win ? 2 : 1;
996                   break;
997                 }
998             }
999
1000           ttf = TREE_CHAIN (ttf);
1001           tta = TREE_CHAIN (tta);
1002           strike_index += 1;
1003         }
1004     }
1005
1006   /* Const member functions get a small penalty because defaulting
1007      to const is less useful than defaulting to non-const.  */
1008   /* This is bogus, it does not correspond to anything in the ARM.
1009      This code will be fixed when this entire section is rewritten
1010      to conform to the ARM.  (mrs)  */
1011   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1012     {
1013       tree this_parm = TREE_VALUE (ttf_in);
1014
1015       if (TREE_CODE (this_parm) == RECORD_TYPE  /* Is `this' a sig ptr?  */
1016             ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
1017             : TYPE_READONLY (TREE_TYPE (this_parm)))
1018         {
1019           cp->harshness[0].code |= TRIVIAL_CODE;
1020           ++easy_strikes;
1021         }
1022       else
1023         {
1024           /* Calling a non-const member function from a const member function
1025              is probably invalid, but for now we let it only draw a warning.
1026              We indicate that such a mismatch has occurred by setting the
1027              harshness to a maximum value.  */
1028           if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1029               && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1030             cp->harshness[0].code |= CONST_CODE;
1031         }
1032     }
1033
1034   if (evil_strikes)
1035     cp->h.code = EVIL_CODE;
1036   if (ellipsis_strikes)
1037     cp->h.code |= ELLIPSIS_CODE;
1038   if (user_strikes)
1039     cp->h.code |= USER_CODE;
1040   cp_silent = 0;
1041 #ifdef DEBUG_MATCHING
1042   cp_error ("final eval %s", print_harshness (&cp->h));
1043 #endif
1044 }
1045
1046 /* Subroutine of ideal_candidate.  See if X or Y is a better match
1047    than the other.  */
1048
1049 static int
1050 strictly_better (x, y)
1051      unsigned short x, y;
1052 {
1053   unsigned short xor;
1054
1055   if (x == y)
1056     return 0;
1057
1058   xor = x ^ y;
1059   if (xor >= x || xor >= y)
1060     return 1;
1061   return 0;
1062 }
1063
1064 /* When one of several possible overloaded functions and/or methods
1065    can be called, choose the best candidate for overloading.
1066
1067    BASETYPE is the context from which we start method resolution
1068    or NULL if we are comparing overloaded functions.
1069    CANDIDATES is the array of candidates we have to choose from.
1070    N_CANDIDATES is the length of CANDIDATES.
1071    PARMS is a TREE_LIST of parameters to the function we'll ultimately
1072    choose.  It is modified in place when resolving methods.  It is not
1073    modified in place when resolving overloaded functions.
1074    LEN is the length of the parameter list.  */
1075
1076 static struct candidate *
1077 ideal_candidate (candidates, n_candidates, len)
1078      struct candidate *candidates;
1079      int n_candidates;
1080      int len;
1081 {
1082   struct candidate *cp = candidates+n_candidates;
1083   int i, j = -1, best_code;
1084
1085   /* For each argument, sort the functions from best to worst for the arg.
1086      For each function that's not best for this arg, set its overall
1087      harshness to EVIL so that other args won't like it.  The candidate
1088      list for the last argument is the intersection of all the best-liked
1089      functions.  */
1090
1091   qsort (candidates, n_candidates, sizeof (struct candidate),
1092          rank_for_overload);
1093   best_code = cp[-1].h.code;
1094
1095   /* If they're at least as good as each other, do an arg-by-arg check.  */
1096   if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
1097     {
1098       int better = 0;
1099       int worse = 0;
1100
1101       for (j = 0; j < n_candidates; j++)
1102         if (! strictly_better (candidates[j].h.code, best_code))
1103           break;
1104
1105       qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
1106              rank_for_ideal);
1107       for (i = 0; i < len; i++)
1108         {
1109           if (cp[-1].harshness[i].code < cp[-2].harshness[i].code)
1110             better = 1;
1111           else if (cp[-1].harshness[i].code > cp[-2].harshness[i].code)
1112             worse = 1;
1113           else if (cp[-1].harshness[i].code & STD_CODE)
1114             {
1115               /* If it involves a standard conversion, let the
1116                  inheritance lattice be the final arbiter.  */
1117               if (cp[-1].harshness[i].distance > cp[-2].harshness[i].distance)
1118                 worse = 1;
1119               else if (cp[-1].harshness[i].distance < cp[-2].harshness[i].distance)
1120                 better = 1;
1121             }
1122           else if (cp[-1].harshness[i].code & PROMO_CODE)
1123             {
1124               /* For integral promotions, take into account a finer
1125                  granularity for determining which types should be favored
1126                  over others in such promotions.  */
1127               if (cp[-1].harshness[i].int_penalty > cp[-2].harshness[i].int_penalty)
1128                 worse = 1;
1129               else if (cp[-1].harshness[i].int_penalty < cp[-2].harshness[i].int_penalty)
1130                 better = 1;
1131             }
1132         }
1133
1134       if (! better || worse)
1135         return NULL;
1136     }
1137   return cp-1;
1138 }
1139
1140 /* Assume that if the class referred to is not in the
1141    current class hierarchy, that it may be remote.
1142    PARENT is assumed to be of aggregate type here.  */
1143
1144 static int
1145 may_be_remote (parent)
1146      tree parent;
1147 {
1148   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
1149     return 0;
1150
1151   if (current_class_type == NULL_TREE)
1152     return 0;
1153
1154   if (parent == current_class_type)
1155     return 0;
1156
1157   if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
1158     return 0;
1159   return 1;
1160 }
1161
1162 tree
1163 build_vfield_ref (datum, type)
1164      tree datum, type;
1165 {
1166   tree rval;
1167   int old_assume_nonnull_objects = flag_assume_nonnull_objects;
1168
1169   if (datum == error_mark_node)
1170     return error_mark_node;
1171
1172   /* Vtable references are always made from non-null objects.  */
1173   flag_assume_nonnull_objects = 1;
1174   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
1175     datum = convert_from_reference (datum);
1176
1177   if (! TYPE_USES_COMPLEX_INHERITANCE (type))
1178     rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
1179                   datum, CLASSTYPE_VFIELD (type));
1180   else
1181     rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
1182   flag_assume_nonnull_objects = old_assume_nonnull_objects;
1183
1184   return rval;
1185 }
1186
1187 /* Build a call to a member of an object.  I.e., one that overloads
1188    operator ()(), or is a pointer-to-function or pointer-to-method.  */
1189
1190 static tree
1191 build_field_call (basetype_path, instance_ptr, name, parms)
1192      tree basetype_path, instance_ptr, name, parms;
1193 {
1194   tree field, instance;
1195
1196   if (instance_ptr == current_class_ptr)
1197     {
1198       /* Check to see if we really have a reference to an instance variable
1199          with `operator()()' overloaded.  */
1200       field = IDENTIFIER_CLASS_VALUE (name);
1201
1202       if (field == NULL_TREE)
1203         {
1204           cp_error ("`this' has no member named `%D'", name);
1205           return error_mark_node;
1206         }
1207
1208       if (TREE_CODE (field) == FIELD_DECL)
1209         {
1210           /* If it's a field, try overloading operator (),
1211              or calling if the field is a pointer-to-function.  */
1212           instance = build_component_ref_1 (current_class_ref, field, 0);
1213           if (instance == error_mark_node)
1214             return error_mark_node;
1215
1216           if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1217               && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
1218             return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
1219
1220           if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1221             {
1222               if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
1223                 return build_function_call (instance, parms);
1224               else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
1225                 return build_function_call (instance, tree_cons (NULL_TREE, current_class_ptr, parms));
1226             }
1227         }
1228       return NULL_TREE;
1229     }
1230
1231   /* Check to see if this is not really a reference to an instance variable
1232      with `operator()()' overloaded.  */
1233   field = lookup_field (basetype_path, name, 1, 0);
1234
1235   /* This can happen if the reference was ambiguous or for access
1236      violations.  */
1237   if (field == error_mark_node)
1238     return error_mark_node;
1239
1240   if (field)
1241     {
1242       tree basetype;
1243       tree ftype = TREE_TYPE (field);
1244
1245       if (TREE_CODE (ftype) == REFERENCE_TYPE)
1246         ftype = TREE_TYPE (ftype);
1247
1248       if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
1249         {
1250           /* Make the next search for this field very short.  */
1251           basetype = DECL_FIELD_CONTEXT (field);
1252           instance_ptr = convert_pointer_to (basetype, instance_ptr);
1253
1254           instance = build_indirect_ref (instance_ptr, NULL_PTR);
1255           return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
1256                                  build_component_ref_1 (instance, field, 0),
1257                                  parms, NULL_TREE);
1258         }
1259       if (TREE_CODE (ftype) == POINTER_TYPE)
1260         {
1261           if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
1262               || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
1263             {
1264               /* This is a member which is a pointer to function.  */
1265               tree ref
1266                 = build_component_ref_1 (build_indirect_ref (instance_ptr,
1267                                                              NULL_PTR),
1268                                          field, LOOKUP_COMPLAIN);
1269               if (ref == error_mark_node)
1270                 return error_mark_node;
1271               return build_function_call (ref, parms);
1272             }
1273         }
1274       else if (TREE_CODE (ftype) == METHOD_TYPE)
1275         {
1276           error ("invalid call via pointer-to-member function");
1277           return error_mark_node;
1278         }
1279       else
1280         return NULL_TREE;
1281     }
1282   return NULL_TREE;
1283 }
1284
1285 tree
1286 find_scoped_type (type, inner_name, inner_types)
1287      tree type, inner_name, inner_types;
1288 {
1289   tree tags = CLASSTYPE_TAGS (type);
1290
1291   while (tags)
1292     {
1293       /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1294          enclosing class) is set to the name for the enum type.  So, if
1295          inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1296          then this test will be true.  */
1297       if (TREE_PURPOSE (tags) == inner_name)
1298         {
1299           if (inner_types == NULL_TREE)
1300             return TYPE_NAME (TREE_VALUE (tags));
1301           return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
1302         }
1303       tags = TREE_CHAIN (tags);
1304     }
1305
1306   /* Look for a TYPE_DECL.  */
1307   for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
1308     if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
1309       {
1310         /* Code by raeburn.  */
1311         if (inner_types == NULL_TREE)
1312           return tags;
1313         return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
1314       }
1315
1316   return NULL_TREE;
1317 }
1318
1319 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1320    the name that names the above nested type.  INNER_TYPES
1321    is a chain of nested type names (held together by SCOPE_REFs);
1322    OUTER_TYPE is the type we know to enclose INNER_TYPES.
1323    Returns NULL_TREE if there is an error.  */
1324
1325 tree
1326 resolve_scope_to_name (outer_type, inner_stuff)
1327      tree outer_type, inner_stuff;
1328 {
1329   register tree tmp;
1330   tree inner_name, inner_type;
1331
1332   if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
1333     {
1334       /* We first try to look for a nesting in our current class context,
1335          then try any enclosing classes.  */
1336       tree type = current_class_type;
1337       
1338       while (type && (TREE_CODE (type) == RECORD_TYPE
1339                       || TREE_CODE (type) == UNION_TYPE))
1340         {
1341           tree rval = resolve_scope_to_name (type, inner_stuff);
1342
1343           if (rval != NULL_TREE)
1344             return rval;
1345           type = DECL_CONTEXT (TYPE_NAME (type));
1346         }
1347     }
1348
1349   if (TREE_CODE (inner_stuff) == SCOPE_REF)
1350     {
1351       inner_name = TREE_OPERAND (inner_stuff, 0);
1352       inner_type = TREE_OPERAND (inner_stuff, 1);
1353     }
1354   else
1355     {
1356       inner_name = inner_stuff;
1357       inner_type = NULL_TREE;
1358     }
1359
1360   if (outer_type == NULL_TREE)
1361     {
1362       tree x;
1363       /* If we have something that's already a type by itself,
1364          use that.  */
1365       if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
1366         {
1367           if (inner_type)
1368             return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
1369                                           inner_type);
1370           return inner_name;
1371         }
1372       
1373       x = lookup_name (inner_name, 0);
1374
1375       if (x && TREE_CODE (x) == NAMESPACE_DECL)
1376         {
1377           x = lookup_namespace_name (x, inner_type);
1378           return x;
1379         }
1380       return NULL_TREE;
1381     }
1382
1383   if (! IS_AGGR_TYPE (outer_type))
1384     return NULL_TREE;
1385
1386   /* Look for member classes or enums.  */
1387   tmp = find_scoped_type (outer_type, inner_name, inner_type);
1388
1389   /* If it's not a type in this class, then go down into the
1390      base classes and search there.  */
1391   if (! tmp && TYPE_BINFO (outer_type))
1392     {
1393       tree binfos = TYPE_BINFO_BASETYPES (outer_type);
1394       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1395
1396       for (i = 0; i < n_baselinks; i++)
1397         {
1398           tree base_binfo = TREE_VEC_ELT (binfos, i);
1399           tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
1400           if (tmp)
1401             return tmp;
1402         }
1403       tmp = NULL_TREE;
1404     }
1405
1406   return tmp;
1407 }
1408
1409 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1410    This is how virtual function calls are avoided.  */
1411
1412 tree
1413 build_scoped_method_call (exp, basetype, name, parms)
1414      tree exp, basetype, name, parms;
1415 {
1416   /* Because this syntactic form does not allow
1417      a pointer to a base class to be `stolen',
1418      we need not protect the derived->base conversion
1419      that happens here.
1420      
1421      @@ But we do have to check access privileges later.  */
1422   tree binfo, decl;
1423   tree type = TREE_TYPE (exp);
1424
1425   if (type == error_mark_node
1426       || basetype == error_mark_node)
1427     return error_mark_node;
1428
1429   if (current_template_parms)
1430     {
1431       if (TREE_CODE (name) == BIT_NOT_EXPR)
1432         {
1433           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1434           name = build_min_nt (BIT_NOT_EXPR, type);
1435         }
1436       name = build_min_nt (SCOPE_REF, basetype, name);
1437       return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
1438     }
1439
1440   if (TREE_CODE (type) == REFERENCE_TYPE)
1441     type = TREE_TYPE (type);
1442
1443   /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1444      that explicit ~int is caught in the parser; this deals with typedefs
1445      and template parms.  */
1446   if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
1447     {
1448       if (type != basetype)
1449         cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1450                   exp, basetype, type);
1451       name = TREE_OPERAND (name, 0);
1452       if (basetype != name && basetype != get_type_value (name))
1453         cp_error ("qualified type `%T' does not match destructor name `~%T'",
1454                   basetype, name);
1455       return convert (void_type_node, exp);
1456     }
1457
1458   if (! is_aggr_type (basetype, 1))
1459     return error_mark_node;
1460
1461   if (! IS_AGGR_TYPE (type))
1462     {
1463       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1464                 exp, type);
1465       return error_mark_node;
1466     }
1467
1468   if ((binfo = binfo_or_else (basetype, type)))
1469     {
1470       if (binfo == error_mark_node)
1471         return error_mark_node;
1472       if (TREE_CODE (exp) == INDIRECT_REF)
1473         decl = build_indirect_ref (convert_pointer_to (binfo,
1474                                                        build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
1475       else
1476         decl = build_scoped_ref (exp, basetype);
1477
1478       /* Call to a destructor.  */
1479       if (TREE_CODE (name) == BIT_NOT_EXPR)
1480         {
1481           /* Explicit call to destructor.  */
1482           name = TREE_OPERAND (name, 0);
1483           if (! (name == TYPE_MAIN_VARIANT (TREE_TYPE (decl))
1484                  || name == constructor_name (TREE_TYPE (decl))
1485                  || TREE_TYPE (decl) == get_type_value (name)))
1486             {
1487               cp_error
1488                 ("qualified type `%T' does not match destructor name `~%T'",
1489                  TREE_TYPE (decl), name);
1490               return error_mark_node;
1491             }
1492           if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
1493             return convert (void_type_node, exp);
1494           
1495           return build_delete (TREE_TYPE (decl), decl, integer_two_node,
1496                                LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
1497                                0);
1498         }
1499
1500       /* Call to a method.  */
1501       return build_method_call (decl, name, parms, binfo,
1502                                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1503     }
1504   return error_mark_node;
1505 }
1506
1507 static void
1508 print_candidates (candidates)
1509      tree candidates;
1510 {
1511   cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
1512   candidates = TREE_CHAIN (candidates);
1513
1514   while (candidates)
1515     {
1516       cp_error_at ("                %D", TREE_VALUE (candidates));
1517       candidates = TREE_CHAIN (candidates);
1518     }
1519 }
1520
1521 static void
1522 print_n_candidates (candidates, n)
1523      struct candidate *candidates;
1524      int n;
1525 {
1526   int i;
1527
1528   cp_error_at ("candidates are: %D", candidates[0].function);
1529   for (i = 1; i < n; i++)
1530     cp_error_at ("                %D", candidates[i].function);
1531 }
1532
1533 /* We want the address of a function or method.  We avoid creating a
1534    pointer-to-member function.  */
1535
1536 tree
1537 build_addr_func (function)
1538      tree function;
1539 {
1540   tree type = TREE_TYPE (function);
1541
1542   /* We have to do these by hand to avoid real pointer to member
1543      functions.  */
1544   if (TREE_CODE (type) == METHOD_TYPE)
1545     {
1546       tree addr;
1547
1548       type = build_pointer_type (type);
1549
1550       if (mark_addressable (function) == 0)
1551         return error_mark_node;
1552
1553       addr = build1 (ADDR_EXPR, type, function);
1554
1555       /* Address of a static or external variable or function counts
1556          as a constant */
1557       if (staticp (function))
1558         TREE_CONSTANT (addr) = 1;
1559
1560       function = addr;
1561     }
1562   else
1563     function = default_conversion (function);
1564
1565   return function;
1566 }
1567
1568 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1569    POINTER_TYPE to those.  Note, pointer to member function types
1570    (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
1571
1572 tree
1573 build_call (function, result_type, parms)
1574      tree function, result_type, parms;
1575 {
1576   int is_constructor = 0;
1577
1578   function = build_addr_func (function);
1579
1580   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
1581     {
1582       sorry ("unable to call pointer to member function here");
1583       return error_mark_node;
1584     }
1585
1586   if (TREE_CODE (function) == ADDR_EXPR
1587       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1588       && DECL_CONSTRUCTOR_P (TREE_OPERAND (function, 0)))
1589     is_constructor = 1;
1590
1591   function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
1592   TREE_HAS_CONSTRUCTOR (function) = is_constructor;
1593   TREE_TYPE (function) = result_type;
1594   TREE_SIDE_EFFECTS (function) = 1;
1595   
1596   return function;
1597 }
1598
1599 static tree
1600 default_parm_conversions (parms, last)
1601      tree parms, *last;
1602 {
1603   tree parm, parmtypes = NULL_TREE;
1604
1605   *last = NULL_TREE;
1606
1607   for (parm = parms; parm; parm = TREE_CHAIN (parm))
1608     {
1609       tree t = TREE_TYPE (TREE_VALUE (parm));
1610
1611       if (TREE_CODE (t) == OFFSET_TYPE
1612           || TREE_CODE (t) == METHOD_TYPE
1613           || TREE_CODE (t) == FUNCTION_TYPE)
1614         {
1615           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
1616           t = TREE_TYPE (TREE_VALUE (parm));
1617         }
1618
1619       if (t == error_mark_node)
1620           return error_mark_node;
1621
1622       *last = build_tree_list (NULL_TREE, t);
1623       parmtypes = chainon (parmtypes, *last);
1624     }
1625
1626   return parmtypes;
1627 }
1628
1629
1630 /* Build something of the form ptr->method (args)
1631    or object.method (args).  This can also build
1632    calls to constructors, and find friends.
1633
1634    Member functions always take their class variable
1635    as a pointer.
1636
1637    INSTANCE is a class instance.
1638
1639    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1640
1641    PARMS help to figure out what that NAME really refers to.
1642
1643    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1644    down to the real instance type to use for access checking.  We need this
1645    information to get protected accesses correct.  This parameter is used
1646    by build_member_call.
1647
1648    FLAGS is the logical disjunction of zero or more LOOKUP_
1649    flags.  See cp-tree.h for more info.
1650
1651    If this is all OK, calls build_function_call with the resolved
1652    member function.
1653
1654    This function must also handle being called to perform
1655    initialization, promotion/coercion of arguments, and
1656    instantiation of default parameters.
1657
1658    Note that NAME may refer to an instance variable name.  If
1659    `operator()()' is defined for the type of that field, then we return
1660    that result.  */
1661
1662 tree
1663 build_method_call (instance, name, parms, basetype_path, flags)
1664      tree instance, name, parms, basetype_path;
1665      int flags;
1666 {
1667   register tree function, fntype, value_type;
1668   register tree basetype, save_basetype;
1669   register tree baselink, result, parmtypes, parm;
1670   tree last;
1671   int pass;
1672   tree access = access_public_node;
1673   tree orig_basetype = basetype_path ? BINFO_TYPE (basetype_path) : NULL_TREE;
1674
1675   /* Range of cases for vtable optimization.  */
1676   enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
1677   enum vtable_needs need_vtbl = not_needed;
1678
1679   char *name_kind;
1680   tree save_name = name;
1681   int ever_seen = 0;
1682   tree instance_ptr = NULL_TREE;
1683   int all_virtual = flag_all_virtual;
1684   int static_call_context = 0;
1685   tree found_fns = NULL_TREE;
1686
1687   /* Keep track of `const' and `volatile' objects.  */
1688   int constp, volatilep;
1689
1690 #ifdef GATHER_STATISTICS
1691   n_build_method_call++;
1692 #endif
1693
1694   if (instance == error_mark_node
1695       || name == error_mark_node
1696       || parms == error_mark_node
1697       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
1698     return error_mark_node;
1699
1700   if (current_template_parms)
1701     {
1702       if (TREE_CODE (name) == BIT_NOT_EXPR)
1703         {
1704           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1705           name = build_min_nt (BIT_NOT_EXPR, type);
1706         }
1707
1708       return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
1709     }
1710
1711   /* This is the logic that magically deletes the second argument to
1712      operator delete, if it is not needed.  */
1713   if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
1714     {
1715       tree save_last = TREE_CHAIN (parms);
1716       tree result;
1717       /* get rid of unneeded argument */
1718       TREE_CHAIN (parms) = NULL_TREE;
1719       result = build_method_call (instance, name, parms, basetype_path,
1720                                   (LOOKUP_SPECULATIVELY|flags)
1721                                   &~LOOKUP_COMPLAIN);
1722       /* If it finds a match, return it.  */
1723       if (result)
1724         return build_method_call (instance, name, parms, basetype_path, flags);
1725       /* If it doesn't work, two argument delete must work */
1726       TREE_CHAIN (parms) = save_last;
1727     }
1728   /* We already know whether it's needed or not for vec delete.  */
1729   else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
1730            && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1731            && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
1732     TREE_CHAIN (parms) = NULL_TREE;
1733
1734   if (TREE_CODE (name) == BIT_NOT_EXPR)
1735     {
1736       flags |= LOOKUP_DESTRUCTOR;
1737       name = TREE_OPERAND (name, 0);
1738       if (parms)
1739         error ("destructors take no parameters");
1740       basetype = TREE_TYPE (instance);
1741       if (TREE_CODE (basetype) == REFERENCE_TYPE)
1742         basetype = TREE_TYPE (basetype);
1743       if (! (name == basetype
1744              || (IS_AGGR_TYPE (basetype)
1745                  && name == constructor_name (basetype))
1746              || basetype == get_type_value (name)))
1747         {
1748           cp_error ("destructor name `~%D' does not match type `%T' of expression",
1749                     name, basetype);
1750           return convert (void_type_node, instance);
1751         }
1752
1753       if (! TYPE_HAS_DESTRUCTOR (basetype))
1754         return convert (void_type_node, instance);
1755       instance = default_conversion (instance);
1756       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1757       return build_delete (build_pointer_type (basetype),
1758                            instance_ptr, integer_two_node,
1759                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
1760     }
1761
1762   {
1763     char *xref_name;
1764     
1765     /* Initialize name for error reporting.  */
1766     if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
1767       {
1768         char *p = operator_name_string (name);
1769         xref_name = (char *)alloca (strlen (p) + 10);
1770         sprintf (xref_name, "operator %s", p);
1771       }
1772     else if (TREE_CODE (name) == SCOPE_REF)
1773       xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
1774     else
1775       xref_name = IDENTIFIER_POINTER (name);
1776
1777     GNU_xref_call (current_function_decl, xref_name);
1778   }
1779
1780   if (instance == NULL_TREE)
1781     {
1782       basetype = NULL_TREE;
1783       /* Check cases where this is really a call to raise
1784          an exception.  */
1785       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
1786         {
1787           basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
1788           if (basetype)
1789             basetype = TREE_VALUE (basetype);
1790         }
1791       else if (TREE_CODE (name) == SCOPE_REF
1792                && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
1793         {
1794           if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
1795             return error_mark_node;
1796           basetype = purpose_member (TREE_OPERAND (name, 1),
1797                                      CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
1798           if (basetype)
1799             basetype = TREE_VALUE (basetype);
1800         }
1801
1802       if (basetype != NULL_TREE)
1803         ;
1804       /* call to a constructor...  */
1805       else if (basetype_path)
1806         {
1807           basetype = BINFO_TYPE (basetype_path);
1808           if (name == TYPE_IDENTIFIER (basetype))
1809             name = ctor_identifier;
1810         }
1811       else if (IDENTIFIER_HAS_TYPE_VALUE (name))
1812         {
1813           basetype = IDENTIFIER_TYPE_VALUE (name);
1814           name = ctor_identifier;
1815         }
1816       else
1817         {
1818           tree typedef_name = lookup_name (name, 1);
1819           if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
1820             {
1821               /* Canonicalize the typedef name.  */
1822               basetype = TREE_TYPE (typedef_name);
1823               name = ctor_identifier;
1824             }
1825           else
1826             {
1827               cp_error ("no constructor named `%T' in scope",
1828                         name);
1829               return error_mark_node;
1830             }
1831         }
1832
1833       if (! IS_AGGR_TYPE (basetype))
1834         {
1835         non_aggr_error:
1836           if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
1837             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1838                       name, instance, basetype);
1839
1840           return error_mark_node;
1841         }
1842     }
1843   else if (instance == current_class_ref || instance == current_class_ptr)
1844     {
1845       /* When doing initialization, we side-effect the TREE_TYPE of
1846          current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
1847       basetype = TREE_TYPE (current_class_ref);
1848
1849       /* Anything manifestly `this' in constructors and destructors
1850          has a known type, so virtual function tables are not needed.  */
1851       if (TYPE_VIRTUAL_P (basetype)
1852           && !(flags & LOOKUP_NONVIRTUAL))
1853         need_vtbl = (dtor_label || ctor_label)
1854           ? unneeded : maybe_needed;
1855
1856       /* If `this' is a signature pointer and `name' is not a constructor,
1857          we are calling a signature member function.  In that case, set the
1858          `basetype' to the signature type and dereference the `optr' field.  */
1859       if (IS_SIGNATURE_POINTER (basetype)
1860           && TYPE_IDENTIFIER (basetype) != name)
1861         {
1862           basetype = SIGNATURE_TYPE (basetype);
1863           instance_ptr = build_optr_ref (instance);
1864           instance_ptr = convert (build_pointer_type (basetype), instance_ptr);
1865           basetype_path = TYPE_BINFO (basetype);
1866         }
1867       else
1868         {
1869           instance = current_class_ref;
1870           instance_ptr = current_class_ptr;
1871           basetype_path = TYPE_BINFO (current_class_type);
1872         }
1873       result = build_field_call (basetype_path, instance_ptr, name, parms);
1874
1875       if (result)
1876         return result;
1877     }
1878   else if (TREE_CODE (instance) == RESULT_DECL)
1879     {
1880       basetype = TREE_TYPE (instance);
1881       /* Should we ever have to make a virtual function reference
1882          from a RESULT_DECL, know that it must be of fixed type
1883          within the scope of this function.  */
1884       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1885         need_vtbl = maybe_needed;
1886       instance_ptr = build1 (ADDR_EXPR, build_pointer_type (basetype), instance);
1887     }
1888   else
1889     {
1890       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
1891       tree inst_ptr_basetype;
1892
1893       static_call_context =
1894         (TREE_CODE (instance) == INDIRECT_REF
1895          && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
1896          && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
1897
1898       if (TREE_CODE (instance) == OFFSET_REF)
1899         instance = resolve_offset_ref (instance);
1900
1901       /* the base type of an instance variable is pointer to class */
1902       basetype = TREE_TYPE (instance);
1903
1904       if (TREE_CODE (basetype) == REFERENCE_TYPE)
1905         {
1906           basetype = TREE_TYPE (basetype);
1907           if (! IS_AGGR_TYPE (basetype))
1908             goto non_aggr_error;
1909           /* Call to convert not needed because we are remaining
1910              within the same type.  */
1911           instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
1912                                  instance);
1913           inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
1914         }
1915       else
1916         {
1917           if (! IS_AGGR_TYPE (basetype)
1918               && ! (TYPE_LANG_SPECIFIC (basetype)
1919                     && (IS_SIGNATURE_POINTER (basetype)
1920                         || IS_SIGNATURE_REFERENCE (basetype))))
1921             goto non_aggr_error;
1922
1923           /* If `instance' is a signature pointer/reference and `name' is
1924              not a constructor, we are calling a signature member function.
1925              In that case set the `basetype' to the signature type.  */
1926           if ((IS_SIGNATURE_POINTER (basetype)
1927                || IS_SIGNATURE_REFERENCE (basetype))
1928               && TYPE_IDENTIFIER (basetype) != name)
1929             basetype = SIGNATURE_TYPE (basetype);
1930
1931           if ((IS_SIGNATURE (basetype)
1932                && (instance_ptr = instance))
1933               || (lvalue_p (instance)
1934                   && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
1935               || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
1936             {
1937               if (instance_ptr == error_mark_node)
1938                 return error_mark_node;
1939             }
1940           else if (TREE_CODE (instance) == NOP_EXPR
1941                    || TREE_CODE (instance) == CONSTRUCTOR)
1942             {
1943               /* A cast is not an lvalue.  Initialize a fresh temp
1944                  with the value we are casting from, and proceed with
1945                  that temporary.  We can't cast to a reference type,
1946                  so that simplifies the initialization to something
1947                  we can manage.  */
1948               tree temp = get_temp_name (TREE_TYPE (instance), 0);
1949               if (IS_AGGR_TYPE (TREE_TYPE (instance)))
1950                 expand_aggr_init (temp, instance, 0, flags);
1951               else
1952                 {
1953                   store_init_value (temp, instance);
1954                   expand_decl_init (temp);
1955                 }
1956               instance = temp;
1957               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1958             }
1959           else
1960             {
1961               if (TREE_CODE (instance) != CALL_EXPR)
1962                 my_friendly_abort (125);
1963               if (TYPE_NEEDS_CONSTRUCTING (basetype))
1964                 instance = build_cplus_new (basetype, instance);
1965               else
1966                 {
1967                   instance = get_temp_name (basetype, 0);
1968                   TREE_ADDRESSABLE (instance) = 1;
1969                 }
1970               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1971             }
1972           /* @@ Should we call comp_target_types here?  */
1973           if (IS_SIGNATURE (basetype))
1974             inst_ptr_basetype = basetype;
1975           else
1976             inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
1977           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
1978             basetype = inst_ptr_basetype;
1979           else
1980             {
1981               instance_ptr = convert (build_pointer_type (basetype), instance_ptr);
1982               if (instance_ptr == error_mark_node)
1983                 return error_mark_node;
1984             }
1985         }
1986
1987       /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1988          not updated, so we use `basetype' instead.  */
1989       if (basetype_path == NULL_TREE
1990           && IS_SIGNATURE (basetype))
1991         basetype_path = TYPE_BINFO (basetype);
1992       else if (basetype_path == NULL_TREE ||
1993         BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
1994         basetype_path = TYPE_BINFO (inst_ptr_basetype);
1995
1996       result = build_field_call (basetype_path, instance_ptr, name, parms);
1997       if (result)
1998         return result;
1999
2000       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
2001         {
2002           if (TREE_SIDE_EFFECTS (instance_ptr))
2003             {
2004               /* This action is needed because the instance is needed
2005                  for providing the base of the virtual function table.
2006                  Without using a SAVE_EXPR, the function we are building
2007                  may be called twice, or side effects on the instance
2008                  variable (such as a post-increment), may happen twice.  */
2009               instance_ptr = save_expr (instance_ptr);
2010               instance = build_indirect_ref (instance_ptr, NULL_PTR);
2011             }
2012           else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
2013             {
2014               /* This happens when called for operator new ().  */
2015               instance = build_indirect_ref (instance, NULL_PTR);
2016             }
2017
2018           need_vtbl = maybe_needed;
2019         }
2020     }
2021
2022   if (save_name == ctor_identifier)
2023     save_name = TYPE_IDENTIFIER (basetype);
2024
2025   if (TYPE_SIZE (complete_type (basetype)) == 0)
2026     {
2027       /* This is worth complaining about, I think.  */
2028       cp_error ("cannot lookup method in incomplete type `%T'", basetype);
2029       return error_mark_node;
2030     }
2031
2032   save_basetype = TYPE_MAIN_VARIANT (basetype);
2033
2034   parmtypes = default_parm_conversions (parms, &last);
2035   if (parmtypes == error_mark_node)
2036     {
2037       return error_mark_node;
2038     }
2039
2040   if (instance && IS_SIGNATURE (basetype))
2041     {
2042       /* @@ Should this be the constp/volatilep flags for the optr field
2043          of the signature pointer?  */
2044       constp = TYPE_READONLY (basetype);
2045       volatilep = TYPE_VOLATILE (basetype);
2046       parms = tree_cons (NULL_TREE, instance_ptr, parms);
2047     }
2048   else if (instance)
2049     {
2050       /* TREE_READONLY (instance) fails for references.  */
2051       constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
2052       volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
2053       parms = tree_cons (NULL_TREE, instance_ptr, parms);
2054     }
2055   else
2056     {
2057       /* Raw constructors are always in charge.  */
2058       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2059           && ! (flags & LOOKUP_HAS_IN_CHARGE))
2060         {
2061           flags |= LOOKUP_HAS_IN_CHARGE;
2062           parms = tree_cons (NULL_TREE, integer_one_node, parms);
2063           parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
2064         }
2065
2066       constp = 0;
2067       volatilep = 0;
2068       instance_ptr = build_int_2 (0, 0);
2069       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
2070       parms = tree_cons (NULL_TREE, instance_ptr, parms);
2071     }
2072
2073   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
2074
2075   if (last == NULL_TREE)
2076     last = parmtypes;
2077
2078   /* Look up function name in the structure type definition.  */
2079
2080   /* FIXME Axe most of this now?  */
2081   if ((IDENTIFIER_HAS_TYPE_VALUE (name)
2082        && ! IDENTIFIER_OPNAME_P (name)
2083        && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name)))
2084       || name == constructor_name (basetype)
2085       || name == ctor_identifier)
2086     {
2087       tree tmp = NULL_TREE;
2088       if (IDENTIFIER_TYPE_VALUE (name) == basetype
2089           || name == constructor_name (basetype)
2090           || name == ctor_identifier)
2091         tmp = TYPE_BINFO (basetype);
2092       else
2093         tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
2094       
2095       if (tmp != NULL_TREE)
2096         {
2097           name_kind = "constructor";
2098           
2099           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2100               && ! (flags & LOOKUP_HAS_IN_CHARGE))
2101             {
2102               /* Constructors called for initialization
2103                  only are never in charge.  */
2104               tree tmplist;
2105               
2106               flags |= LOOKUP_HAS_IN_CHARGE;
2107               tmplist = tree_cons (NULL_TREE, integer_zero_node,
2108                                    TREE_CHAIN (parms));
2109               TREE_CHAIN (parms) = tmplist;
2110               tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
2111               TREE_CHAIN (parmtypes) = tmplist;
2112             }
2113           basetype = BINFO_TYPE (tmp);
2114         }
2115       else
2116         name_kind = "method";
2117     }
2118   else
2119     name_kind = "method";
2120   
2121   if (basetype_path == NULL_TREE
2122       || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
2123     basetype_path = TYPE_BINFO (basetype);
2124   result = lookup_fnfields (basetype_path, name,
2125                             (flags & LOOKUP_COMPLAIN));
2126   if (result == error_mark_node)
2127     return error_mark_node;
2128
2129   for (pass = 0; pass < 2; pass++)
2130     {
2131       struct candidate *candidates;
2132       struct candidate *cp;
2133       int len;
2134       unsigned best = 1;
2135
2136       baselink = result;
2137
2138       if (pass > 0)
2139         {
2140           candidates
2141             = (struct candidate *) alloca ((ever_seen+1)
2142                                            * sizeof (struct candidate));
2143           bzero ((char *) candidates, (ever_seen + 1) * sizeof (struct candidate));
2144           cp = candidates;
2145           len = list_length (parms);
2146           ever_seen = 0;
2147
2148           /* First see if a global function has a shot at it.  */
2149           if (flags & LOOKUP_GLOBAL)
2150             {
2151               tree friend_parms;
2152               tree parm = instance_ptr;
2153
2154               if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
2155                 parm = convert_from_reference (parm);
2156               else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
2157                 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
2158               else
2159                 my_friendly_abort (167);
2160
2161               friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
2162
2163               cp->h_len = len;
2164               cp->harshness = (struct harshness_code *)
2165                 alloca ((len + 1) * sizeof (struct harshness_code));
2166
2167               result = build_overload_call_real (name, friend_parms, 0, cp, 1);
2168
2169               /* If it turns out to be the one we were actually looking for
2170                  (it was probably a friend function), the return the
2171                  good result.  */
2172               if (TREE_CODE (result) == CALL_EXPR)
2173                 return result;
2174
2175               while ((cp->h.code & EVIL_CODE) == 0)
2176                 {
2177                   /* non-standard uses: set the field to 0 to indicate
2178                      we are using a non-member function.  */
2179                   cp->u.field = 0;
2180                   if (cp->harshness[len].distance == 0
2181                       && cp->h.code < best)
2182                     best = cp->h.code;
2183                   cp += 1;
2184                 }
2185             }
2186         }
2187
2188       if (baselink)
2189         {
2190           /* We have a hit (of sorts). If the parameter list is
2191              "error_mark_node", or some variant thereof, it won't
2192              match any methods.  Since we have verified that the is
2193              some method vaguely matching this one (in name at least),
2194              silently return.
2195              
2196              Don't stop for friends, however.  */
2197           basetype_path = TREE_PURPOSE (baselink);
2198
2199           function = TREE_VALUE (baselink);
2200           if (TREE_CODE (basetype_path) == TREE_LIST)
2201             basetype_path = TREE_VALUE (basetype_path);
2202           basetype = BINFO_TYPE (basetype_path);
2203
2204           for (; function; function = DECL_CHAIN (function))
2205             {
2206 #ifdef GATHER_STATISTICS
2207               n_inner_fields_searched++;
2208 #endif
2209               ever_seen++;
2210               if (pass > 0)
2211                 found_fns = tree_cons (NULL_TREE, function, found_fns);
2212
2213               /* Not looking for friends here.  */
2214               if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
2215                   && ! DECL_STATIC_FUNCTION_P (function))
2216                 continue;
2217
2218               if (pass > 0)
2219                 {
2220                   tree these_parms = parms;
2221
2222 #ifdef GATHER_STATISTICS
2223                   n_inner_fields_searched++;
2224 #endif
2225                   cp->h_len = len;
2226                   cp->harshness = (struct harshness_code *)
2227                     alloca ((len + 1) * sizeof (struct harshness_code));
2228
2229                   if (DECL_STATIC_FUNCTION_P (function))
2230                     these_parms = TREE_CHAIN (these_parms);
2231                   compute_conversion_costs (function, these_parms, cp, len);
2232
2233                   if ((cp->h.code & EVIL_CODE) == 0)
2234                     {
2235                       cp->u.field = function;
2236                       cp->function = function;
2237                       cp->basetypes = basetype_path;
2238
2239                       /* Don't allow non-converting constructors to convert.  */
2240                       if (flags & LOOKUP_ONLYCONVERTING
2241                           && DECL_LANG_SPECIFIC (function)
2242                           && DECL_NONCONVERTING_P (function))
2243                         continue;
2244
2245                       /* No "two-level" conversions.  */
2246                       if (flags & LOOKUP_NO_CONVERSION
2247                           && (cp->h.code & USER_CODE))
2248                         continue;
2249
2250                       cp++;
2251                     }
2252                 }
2253             }
2254         }
2255
2256       if (pass == 0)
2257         {
2258           tree igv = lookup_name_nonclass (name);
2259
2260           /* No exact match could be found.  Now try to find match
2261              using default conversions.  */
2262           if ((flags & LOOKUP_GLOBAL) && igv)
2263             {
2264               if (TREE_CODE (igv) == FUNCTION_DECL)
2265                 ever_seen += 1;
2266               else if (TREE_CODE (igv) == TREE_LIST)
2267                 ever_seen += count_functions (igv);
2268             }
2269
2270           if (ever_seen == 0)
2271             {
2272               if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2273                   == LOOKUP_SPECULATIVELY)
2274                 return NULL_TREE;
2275               
2276               TREE_CHAIN (last) = void_list_node;
2277               if (flags & LOOKUP_GLOBAL)
2278                 cp_error ("no global or member function `%D(%A)' defined",
2279                           save_name, parmtypes);
2280               else
2281                 cp_error ("no member function `%T::%D(%A)' defined",
2282                           save_basetype, save_name, TREE_CHAIN (parmtypes));
2283               return error_mark_node;
2284             }
2285           continue;
2286         }
2287
2288       if (cp - candidates != 0)
2289         {
2290           /* Rank from worst to best.  Then cp will point to best one.
2291              Private fields have their bits flipped.  For unsigned
2292              numbers, this should make them look very large.
2293              If the best alternate has a (signed) negative value,
2294              then all we ever saw were private members.  */
2295           if (cp - candidates > 1)
2296             {
2297               int n_candidates = cp - candidates;
2298               extern int warn_synth;
2299               TREE_VALUE (parms) = instance_ptr;
2300               cp = ideal_candidate (candidates, n_candidates, len);
2301               if (cp == (struct candidate *)0)
2302                 {
2303                   if (flags & LOOKUP_COMPLAIN)
2304                     {
2305                       TREE_CHAIN (last) = void_list_node;
2306                       cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2307                                 name_kind, save_name, TREE_CHAIN (parmtypes));
2308                       print_n_candidates (candidates, n_candidates);
2309                     }
2310                   return error_mark_node;
2311                 }
2312               if (cp->h.code & EVIL_CODE)
2313                 return error_mark_node;
2314               if (warn_synth
2315                   && DECL_NAME (cp->function) == ansi_opname[MODIFY_EXPR]
2316                   && DECL_ARTIFICIAL (cp->function)
2317                   && n_candidates == 2)
2318                 {
2319                   cp_warning ("using synthesized `%#D' for copy assignment",
2320                               cp->function);
2321                   cp_warning_at ("  where cfront would use `%#D'",
2322                                  candidates->function);
2323                 }
2324             }
2325           else if (cp[-1].h.code & EVIL_CODE)
2326             {
2327               if (flags & LOOKUP_COMPLAIN)
2328                 cp_error ("ambiguous type conversion requested for %s `%D'",
2329                           name_kind, save_name);
2330               return error_mark_node;
2331             }
2332           else
2333             cp--;
2334
2335           /* The global function was the best, so use it.  */
2336           if (cp->u.field == 0)
2337             {
2338               /* We must convert the instance pointer into a reference type.
2339                  Global overloaded functions can only either take
2340                  aggregate objects (which come for free from references)
2341                  or reference data types anyway.  */
2342               TREE_VALUE (parms) = copy_node (instance_ptr);
2343               TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
2344               return build_function_call (cp->function, parms);
2345             }
2346
2347           function = cp->function;
2348           basetype_path = cp->basetypes;
2349           if (! DECL_STATIC_FUNCTION_P (function))
2350             TREE_VALUE (parms) = cp->arg;
2351           goto found_and_maybe_warn;
2352         }
2353
2354       if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
2355         {
2356           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2357               == LOOKUP_SPECULATIVELY)
2358             return NULL_TREE;
2359
2360           if (DECL_STATIC_FUNCTION_P (cp->function))
2361             parms = TREE_CHAIN (parms);
2362           if (ever_seen)
2363             {
2364               if (flags & LOOKUP_SPECULATIVELY)
2365                 return NULL_TREE;
2366               if (static_call_context
2367                   && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
2368                 cp_error ("object missing in call to `%D'", cp->function);
2369               else if (ever_seen > 1)
2370                 {
2371                   TREE_CHAIN (last) = void_list_node;
2372                   cp_error ("no matching function for call to `%T::%D (%A)%V'",
2373                             TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr))),
2374                             save_name, TREE_CHAIN (parmtypes),
2375                             TREE_TYPE (TREE_TYPE (instance_ptr)));
2376                   TREE_CHAIN (last) = NULL_TREE;
2377                   print_candidates (found_fns);
2378                 }
2379               else
2380                 report_type_mismatch (cp, parms, name_kind);
2381               return error_mark_node;
2382             }
2383
2384           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2385               == LOOKUP_COMPLAIN)
2386             {
2387               cp_error ("%T has no method named %D", save_basetype, save_name);
2388               return error_mark_node;
2389             }
2390           return NULL_TREE;
2391         }
2392       continue;
2393
2394     found_and_maybe_warn:
2395       if ((cp->harshness[0].code & CONST_CODE)
2396           /* 12.1p2: Constructors can be called for const objects.  */
2397           && ! DECL_CONSTRUCTOR_P (cp->function))
2398         {
2399           if (flags & LOOKUP_COMPLAIN)
2400             {
2401               cp_error_at ("non-const member function `%D'", cp->function);
2402               error ("called for const object at this point in file");
2403             }
2404           /* Not good enough for a match.  */
2405           else
2406             return error_mark_node;
2407         }
2408       goto found;
2409     }
2410   /* Silently return error_mark_node.  */
2411   return error_mark_node;
2412
2413  found:
2414   if (flags & LOOKUP_PROTECT)
2415     access = compute_access (basetype_path, function);
2416
2417   if (access == access_private_node)
2418     {
2419       if (flags & LOOKUP_COMPLAIN)
2420         {
2421           cp_error_at ("%s `%+#D' is %s", name_kind, function, 
2422                        TREE_PRIVATE (function) ? "private"
2423                        : "from private base class");
2424           error ("within this context");
2425         }
2426       return error_mark_node;
2427     }
2428   else if (access == access_protected_node)
2429     {
2430       if (flags & LOOKUP_COMPLAIN)
2431         {
2432           cp_error_at ("%s `%+#D' %s", name_kind, function,
2433                        TREE_PROTECTED (function) ? "is protected"
2434                        : "has protected accessibility");
2435           error ("within this context");
2436         }
2437       return error_mark_node;
2438     }
2439
2440   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2441      type (if it exists) is a pointer to.  */
2442
2443   if (DECL_ABSTRACT_VIRTUAL_P (function)
2444       && instance == current_class_ref
2445       && DECL_CONSTRUCTOR_P (current_function_decl)
2446       && ! (flags & LOOKUP_NONVIRTUAL)
2447       && value_member (function, get_abstract_virtuals (basetype)))
2448     cp_error ("abstract virtual `%#D' called from constructor", function);
2449
2450   if (IS_SIGNATURE (basetype))
2451     {
2452       if (static_call_context)
2453         {
2454           cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2455                     basetype, save_name);
2456           return error_mark_node;
2457         }
2458       return build_signature_method_call (basetype, instance, function, parms);
2459     }
2460
2461   function = DECL_MAIN_VARIANT (function);
2462   mark_used (function);
2463
2464   /* Is it a synthesized method that needs to be synthesized?  */
2465   if (DECL_ARTIFICIAL (function) && ! DECL_INITIAL (function)
2466       /* Kludge: don't synthesize for default args.  */
2467       && current_function_decl)
2468     synthesize_method (function);
2469
2470   if (pedantic && DECL_THIS_INLINE (function) && ! DECL_ARTIFICIAL (function)
2471       && ! DECL_INITIAL (function) && ! DECL_PENDING_INLINE_INFO (function)
2472       && ! (DECL_TEMPLATE_INFO (function)
2473             && TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (function))))
2474     cp_warning ("inline function `%#D' called before definition", function);
2475
2476   fntype = TREE_TYPE (function);
2477   if (TREE_CODE (fntype) == POINTER_TYPE)
2478     fntype = TREE_TYPE (fntype);
2479   basetype = DECL_CLASS_CONTEXT (function);
2480
2481   /* If we are referencing a virtual function from an object
2482      of effectively static type, then there is no need
2483      to go through the virtual function table.  */
2484   if (need_vtbl == maybe_needed)
2485     {
2486       int fixed_type = resolves_to_fixed_type_p (instance, 0);
2487
2488       if (all_virtual == 1
2489           && DECL_VINDEX (function)
2490           && may_be_remote (basetype))
2491         need_vtbl = needed;
2492       else if (DECL_VINDEX (function))
2493         need_vtbl = fixed_type ? unneeded : needed;
2494       else
2495         need_vtbl = not_needed;
2496     }
2497
2498   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
2499       && !DECL_CONSTRUCTOR_P (function))
2500     {
2501       /* Let's be nasty to the user now, and give reasonable
2502          error messages.  */
2503       instance_ptr = current_class_ptr;
2504       if (instance_ptr)
2505         {
2506           if (basetype != current_class_type)
2507             {
2508               if (basetype == error_mark_node)
2509                 return error_mark_node;
2510               else 
2511                 {
2512                   if (orig_basetype != NULL_TREE)
2513                     error_not_base_type (orig_basetype, current_class_type);
2514                   else
2515                     error_not_base_type (function, current_class_type);
2516                   return error_mark_node;
2517                 }
2518             }
2519         }
2520       /* Only allow a static member function to call another static member
2521          function.  */
2522       else if (DECL_LANG_SPECIFIC (function)
2523                && !DECL_STATIC_FUNCTION_P (function))
2524         {
2525           cp_error ("cannot call member function `%D' without object",
2526                     function);
2527           return error_mark_node;
2528         }
2529     }
2530
2531   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2532
2533   if (TYPE_SIZE (complete_type (value_type)) == 0)
2534     {
2535       if (flags & LOOKUP_COMPLAIN)
2536         incomplete_type_error (0, value_type);
2537       return error_mark_node;
2538     }
2539
2540   if (DECL_STATIC_FUNCTION_P (function))
2541     parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2542                                TREE_CHAIN (parms), function, LOOKUP_NORMAL);
2543   else if (need_vtbl == unneeded)
2544     {
2545       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
2546       basetype = TREE_TYPE (instance);
2547       if (TYPE_METHOD_BASETYPE (TREE_TYPE (function))
2548           != TYPE_MAIN_VARIANT (basetype))
2549         {
2550           basetype = DECL_CLASS_CONTEXT (function);
2551           instance_ptr = convert_pointer_to (basetype, instance_ptr);
2552           instance = build_indirect_ref (instance_ptr, NULL_PTR);
2553         }
2554       parms = tree_cons (NULL_TREE, instance_ptr,
2555                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, sub_flags));
2556     }
2557   else
2558     {
2559       if ((flags & LOOKUP_NONVIRTUAL) == 0)
2560         basetype = DECL_CONTEXT (function);
2561
2562       /* First parm could be integer_zerop with casts like
2563          ((Object*)0)->Object::IsA()  */
2564       if (!integer_zerop (TREE_VALUE (parms)))
2565         {
2566           /* Since we can't have inheritance with a union, doing get_binfo
2567              on it won't work.  We do all the convert_pointer_to_real
2568              stuff to handle MI correctly...for unions, that's not
2569              an issue, so we must short-circuit that extra work here.  */
2570           tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
2571           if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
2572             instance_ptr = TREE_VALUE (parms);
2573           else
2574             {
2575               tree binfo = get_binfo (basetype,
2576                                       TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
2577                                       0);
2578               instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
2579             }
2580           instance_ptr
2581             = convert_pointer_to (build_type_variant (basetype,
2582                                                       constp, volatilep),
2583                                   instance_ptr);
2584
2585           if (TREE_CODE (instance_ptr) == COND_EXPR)
2586             {
2587               instance_ptr = save_expr (instance_ptr);
2588               instance = build_indirect_ref (instance_ptr, NULL_PTR);
2589             }
2590           else if (TREE_CODE (instance_ptr) == NOP_EXPR
2591                    && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
2592                    && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
2593             ;
2594           /* The call to `convert_pointer_to' may return error_mark_node.  */
2595           else if (TREE_CODE (instance_ptr) == ERROR_MARK)
2596             return instance_ptr;
2597           else if (instance == NULL_TREE
2598                    || TREE_CODE (instance) != INDIRECT_REF
2599                    || TREE_OPERAND (instance, 0) != instance_ptr)
2600             instance = build_indirect_ref (instance_ptr, NULL_PTR);
2601         }
2602       parms = tree_cons (NULL_TREE, instance_ptr,
2603                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, LOOKUP_NORMAL));
2604     }
2605
2606   if (parms == error_mark_node
2607       || (parms && TREE_CHAIN (parms) == error_mark_node))
2608     return error_mark_node;
2609
2610   if (need_vtbl == needed)
2611     {
2612       function = build_vfn_ref (&TREE_VALUE (parms), instance,
2613                                 DECL_VINDEX (function));
2614       TREE_TYPE (function) = build_pointer_type (fntype);
2615     }
2616
2617   if (TREE_CODE (function) == FUNCTION_DECL)
2618     GNU_xref_call (current_function_decl,
2619                    IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
2620
2621   result = build_call (function, value_type, parms);
2622   result = convert_from_reference (result);
2623   return result;
2624 }
2625
2626 /* Similar to `build_method_call', but for overloaded non-member functions.
2627    The name of this function comes through NAME.  The name depends
2628    on PARMS.
2629
2630    Note that this function must handle simple `C' promotions,
2631    as well as variable numbers of arguments (...), and
2632    default arguments to boot.
2633
2634    If the overloading is successful, we return a tree node which
2635    contains the call to the function.
2636
2637    If overloading produces candidates which are probable, but not definite,
2638    we hold these candidates.  If FINAL_CP is non-zero, then we are free
2639    to assume that final_cp points to enough storage for all candidates that
2640    this function might generate.  The `harshness' array is preallocated for
2641    the first candidate, but not for subsequent ones.
2642
2643    Note that the DECL_RTL of FUNCTION must be made to agree with this
2644    function's new name.  */
2645
2646 tree
2647 build_overload_call_real (fnname, parms, flags, final_cp, require_complete)
2648      tree fnname, parms;
2649      int flags;
2650      struct candidate *final_cp;
2651      int require_complete;
2652 {
2653   /* must check for overloading here */
2654   tree functions, function, parm;
2655   tree parmtypes, last;
2656   register tree outer;
2657   int length;
2658   int parmlength = list_length (parms);
2659
2660   struct candidate *candidates, *cp;
2661
2662   if (final_cp)
2663     {
2664       final_cp[0].h.code = 0;
2665       final_cp[0].h.distance = 0;
2666       final_cp[0].function = 0;
2667       /* end marker.  */
2668       final_cp[1].h.code = EVIL_CODE;
2669     }
2670
2671   parmtypes = default_parm_conversions (parms, &last);
2672   if (parmtypes == error_mark_node)
2673     {
2674       if (final_cp)
2675         final_cp->h.code = EVIL_CODE;
2676       return error_mark_node;
2677     }
2678
2679   if (last)
2680     TREE_CHAIN (last) = void_list_node;
2681   else
2682     parmtypes = void_list_node;
2683
2684   if (is_overloaded_fn (fnname))
2685     {
2686       functions = fnname;
2687       if (TREE_CODE (fnname) == TREE_LIST)
2688         fnname = TREE_PURPOSE (functions);
2689       else if (TREE_CODE (fnname) == FUNCTION_DECL)
2690         fnname = DECL_NAME (functions);
2691     }
2692   else 
2693     functions = lookup_name_nonclass (fnname);
2694
2695   if (functions == NULL_TREE)
2696     {
2697       if (flags & LOOKUP_SPECULATIVELY)
2698         return NULL_TREE;
2699       if (flags & LOOKUP_COMPLAIN)
2700         error ("only member functions apply");
2701       if (final_cp)
2702         final_cp->h.code = EVIL_CODE;
2703       return error_mark_node;
2704     }
2705
2706   if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
2707     {
2708       functions = DECL_MAIN_VARIANT (functions);
2709       if (final_cp)
2710         {
2711           /* We are just curious whether this is a viable alternative or
2712              not.  */
2713           compute_conversion_costs (functions, parms, final_cp, parmlength);
2714           return functions;
2715         }
2716       else
2717         return build_function_call_real (functions, parms, 1, flags);
2718     }
2719
2720   if (TREE_CODE (functions) == TREE_LIST
2721       && TREE_VALUE (functions) == NULL_TREE)
2722     {
2723       if (flags & LOOKUP_SPECULATIVELY)
2724         return NULL_TREE;
2725       
2726       if (flags & LOOKUP_COMPLAIN)
2727         cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2728                   TREE_PURPOSE (functions));
2729       if (final_cp)
2730         final_cp->h.code = EVIL_CODE;
2731       return error_mark_node;
2732     }
2733
2734   length = count_functions (functions);
2735   
2736   if (final_cp)
2737     candidates = final_cp;
2738   else
2739     {
2740       candidates
2741         = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
2742       bzero ((char *) candidates, (length + 1) * sizeof (struct candidate));
2743     }
2744
2745   cp = candidates;
2746
2747   my_friendly_assert (is_overloaded_fn (functions), 169);
2748
2749   functions = get_first_fn (functions);
2750
2751   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
2752   for (outer = functions; outer; outer = DECL_CHAIN (outer))
2753     {
2754       int template_cost = 0;
2755       function = outer;
2756       if (TREE_CODE (function) != FUNCTION_DECL
2757           && ! (TREE_CODE (function) == TEMPLATE_DECL
2758                 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
2759         {
2760           enum tree_code code = TREE_CODE (function);
2761           if (code == TEMPLATE_DECL)
2762             code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
2763           if (code == CONST_DECL)
2764             cp_error_at
2765               ("enumeral value `%D' conflicts with function of same name",
2766                function);
2767           else if (code == VAR_DECL)
2768             {
2769               if (TREE_STATIC (function))
2770                 cp_error_at
2771                   ("variable `%D' conflicts with function of same name",
2772                    function);
2773               else
2774                 cp_error_at
2775                   ("constant field `%D' conflicts with function of same name",
2776                    function);
2777             }
2778           else if (code == TYPE_DECL)
2779             continue;
2780           else
2781             my_friendly_abort (2);
2782           error ("at this point in file");
2783           continue;
2784         }
2785       if (TREE_CODE (function) == TEMPLATE_DECL)
2786         {
2787           int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
2788           tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
2789           int i;
2790
2791           i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
2792                                 TYPE_ARG_TYPES (TREE_TYPE (function)),
2793                                 parms, &template_cost, 0);
2794           if (i == 0)
2795             {
2796               function = instantiate_template (function, targs);
2797               if (function == error_mark_node)
2798                 return function;
2799             }
2800         }
2801
2802       if (TREE_CODE (function) == TEMPLATE_DECL)
2803         {
2804           /* Unconverted template -- failed match.  */
2805           cp->function = function;
2806           cp->u.bad_arg = -4;
2807           cp->h.code = EVIL_CODE;
2808         }
2809       else
2810         {
2811           struct candidate *cp2;
2812
2813           /* Check that this decl is not the same as a function that's in
2814              the list due to some template instantiation.  */
2815           cp2 = candidates;
2816           while (cp2 != cp)
2817             if (cp2->function == function)
2818               break;
2819             else
2820               cp2 += 1;
2821           if (cp2->function == function)
2822             continue;
2823
2824           function = DECL_MAIN_VARIANT (function);
2825
2826           /* Can't use alloca here, since result might be
2827              passed to calling function.  */
2828           cp->h_len = parmlength;
2829           cp->harshness = (struct harshness_code *)
2830             oballoc ((parmlength + 1) * sizeof (struct harshness_code));
2831
2832           compute_conversion_costs (function, parms, cp, parmlength);
2833
2834           /* Make sure this is clear as well.  */
2835           cp->h.int_penalty += template_cost;
2836
2837           if ((cp[0].h.code & EVIL_CODE) == 0)
2838             {
2839               cp[1].h.code = EVIL_CODE;
2840               cp++;
2841             }
2842         }
2843     }
2844
2845   if (cp - candidates)
2846     {
2847       tree rval = error_mark_node;
2848
2849       /* Leave marker.  */
2850       cp[0].h.code = EVIL_CODE;
2851       if (cp - candidates > 1)
2852         {
2853           struct candidate *best_cp
2854             = ideal_candidate (candidates, cp - candidates, parmlength);
2855           if (best_cp == (struct candidate *)0)
2856             {
2857               if (flags & LOOKUP_COMPLAIN)
2858                 {
2859                   cp_error ("call of overloaded `%D' is ambiguous", fnname);
2860                   print_n_candidates (candidates, cp - candidates);
2861                 }
2862               return error_mark_node;
2863             }
2864           else
2865             rval = best_cp->function;
2866         }
2867       else
2868         {
2869           cp -= 1;
2870           if (cp->h.code & EVIL_CODE)
2871             {
2872               if (flags & LOOKUP_COMPLAIN)
2873                 error ("type conversion ambiguous");
2874             }
2875           else
2876             rval = cp->function;
2877         }
2878
2879       if (final_cp)
2880         return rval;
2881
2882       return build_function_call_real (rval, parms, require_complete, flags);
2883     }
2884
2885   if (flags & LOOKUP_SPECULATIVELY)
2886     return NULL_TREE;
2887   
2888   if (flags & LOOKUP_COMPLAIN)
2889     report_type_mismatch (cp, parms, "function",
2890                           decl_as_string (cp->function, 1));
2891
2892   return error_mark_node;
2893 }
2894
2895 /* This requires a complete type on the result of the call.  */
2896
2897 tree
2898 build_overload_call (fnname, parms, flags)
2899      tree fnname, parms;
2900      int flags;
2901 {
2902   return build_overload_call_real (fnname, parms, flags, (struct candidate *)0, 1);
2903 }