OSDN Git Service

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