OSDN Git Service

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