OSDN Git Service

42nd 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       if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
529         {
530           ttl = unsigned_type (ttl);
531           intype = unsigned_type (intype);
532           penalty += 2;
533         }
534
535       ttr = intype;
536
537       /* If the initializer is not an lvalue, then it does not
538          matter if we make life easier for the programmer
539          by creating a temporary variable with which to
540          hold the result.  */
541       if (parm && (INTEGRAL_CODE_P (coder)
542                    || coder == REAL_TYPE)
543           && ! lvalue_p (parm))
544         {
545           h = convert_harshness (ttl, ttr, NULL_TREE);
546           if (penalty > 2 || h.code != 0)
547             h.code |= STD_CODE;
548           else
549             h.code |= TRIVIAL_CODE;
550           h.distance = 0;
551           return h;
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
704 #ifdef GATHER_STATISTICS
705   n_compute_conversion_costs++;
706 #endif
707
708   cp->function = function;
709   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
710   cp->u.bad_arg = 0;            /* optimistic!  */
711
712   cp->h.code = 0;
713   cp->h.distance = 0;
714   cp->h.int_penalty = 0;
715   bzero (cp->harshness,
716          (cp->h_len + 1) * sizeof (struct harshness_code));
717
718   while (ttf && tta)
719     {
720       struct harshness_code h;
721
722       if (ttf == void_list_node)
723         break;
724
725       if (type_unknown_p (TREE_VALUE (tta)))
726         {         
727           /* Must perform some instantiation here.  */
728           tree rhs = TREE_VALUE (tta);
729           tree lhstype = TREE_VALUE (ttf);
730
731           /* Keep quiet about possible contravariance violations.  */
732           int old_inhibit_warnings = inhibit_warnings;
733           inhibit_warnings = 1;
734
735           /* @@ This is to undo what `grokdeclarator' does to
736              parameter types.  It really should go through
737              something more general.  */
738
739           TREE_TYPE (tta) = unknown_type_node;
740           rhs = instantiate_type (lhstype, rhs, 0);
741           inhibit_warnings = old_inhibit_warnings;
742
743           if (TREE_CODE (rhs) == ERROR_MARK)
744             h.code = EVIL_CODE;
745           else
746             h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
747         }
748       else
749         {
750 #ifdef DEBUG_MATCHING
751           static tree old_function = NULL_TREE;
752
753           if (!old_function || function != old_function)
754             {
755               cp_error ("trying %D", function);
756               old_function = function;
757             }
758
759           cp_error ("      doing (%T) %E against arg %T",
760                     TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
761                     TREE_VALUE (ttf));
762 #endif
763
764           h = convert_harshness (TREE_VALUE (ttf),
765                                  TREE_TYPE (TREE_VALUE (tta)),
766                                  TREE_VALUE (tta));
767
768 #ifdef DEBUG_MATCHING
769           cp_error ("     evaluated %s", print_harshness (&h));
770 #endif
771         }
772
773       cp->harshness[strike_index] = h;
774       if ((h.code & EVIL_CODE)
775           || ((h.code & STD_CODE) && h.distance < 0))
776         {
777           cp->u.bad_arg = strike_index;
778           evil_strikes = 1;
779         }
780      else if (h.code & ELLIPSIS_CODE)
781        ellipsis_strikes += 1;
782 #if 0
783       /* This is never set by `convert_harshness'.  */
784       else if (h.code & USER_CODE)
785         {
786           user_strikes += 1;
787         }
788 #endif
789       else
790         {
791           if ((h.code & STD_CODE) && h.distance)
792             {
793               if (h.distance > b_or_d_strikes)
794                 b_or_d_strikes = h.distance;
795             }
796           else
797             easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
798           cp->h.code |= h.code;
799           /* Make sure we communicate this.  */
800           cp->h.int_penalty += h.int_penalty;
801         }
802
803       ttf = TREE_CHAIN (ttf);
804       tta = TREE_CHAIN (tta);
805       strike_index += 1;
806     }
807
808   if (tta)
809     {
810       /* ran out of formals, and parmlist is fixed size.  */
811       if (ttf /* == void_type_node */)
812         {
813           cp->h.code = EVIL_CODE;
814           cp->u.bad_arg = -1;
815           return;
816         }
817       else
818         {
819           struct harshness_code h;
820           int l = list_length (tta);
821           ellipsis_strikes += l;
822           h.code = ELLIPSIS_CODE;
823           h.distance = 0;
824           h.int_penalty = 0;
825           for (; l; --l)
826             cp->harshness[strike_index++] = h;
827         }
828     }
829   else if (ttf && ttf != void_list_node)
830     {
831       /* ran out of actuals, and no defaults.  */
832       if (TREE_PURPOSE (ttf) == NULL_TREE)
833         {
834           cp->h.code = EVIL_CODE;
835           cp->u.bad_arg = -2;
836           return;
837         }
838       /* Store index of first default.  */
839       cp->harshness[arglen].distance = strike_index+1;
840     }
841   else
842     cp->harshness[arglen].distance = 0;
843
844   /* Argument list lengths work out, so don't need to check them again.  */
845   if (evil_strikes)
846     {
847       /* We do not check for derived->base conversions here, since in
848          no case would they give evil strike counts, unless such conversions
849          are somehow ambiguous.  */
850
851       /* See if any user-defined conversions apply.
852          But make sure that we do not loop.  */
853       static int dont_convert_types = 0;
854
855       if (dont_convert_types)
856         {
857           cp->h.code = EVIL_CODE;
858           return;
859         }
860
861       win = 0;                  /* Only get one chance to win.  */
862       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
863       tta = tta_in;
864       strike_index = 0;
865       evil_strikes = 0;
866
867       while (ttf && tta)
868         {
869           if (ttf == void_list_node)
870             break;
871
872           lose = cp->harshness[strike_index];
873           if ((lose.code & EVIL_CODE)
874               || ((lose.code & STD_CODE) && lose.distance < 0))
875             {
876               tree actual_type = TREE_TYPE (TREE_VALUE (tta));
877               tree formal_type = TREE_VALUE (ttf);
878               int extra_conversions = 0;
879
880               dont_convert_types = 1;
881
882               if (TREE_CODE (formal_type) == REFERENCE_TYPE)
883                 formal_type = TREE_TYPE (formal_type);
884               if (TREE_CODE (actual_type) == REFERENCE_TYPE)
885                 actual_type = TREE_TYPE (actual_type);
886
887               if (formal_type != error_mark_node
888                   && actual_type != error_mark_node)
889                 {
890                   formal_type = TYPE_MAIN_VARIANT (formal_type);
891                   actual_type = TYPE_MAIN_VARIANT (actual_type);
892
893                   if (TYPE_HAS_CONSTRUCTOR (formal_type))
894                     {
895                       /* If it has a constructor for this type,
896                          try to use it.  */
897                       /* @@ There is no way to save this result yet, so
898                          success is a NULL_TREE for now.  */
899                       if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
900                           != error_mark_node)
901                         win++;
902                     }
903                   if (TYPE_LANG_SPECIFIC (actual_type)
904                       && TYPE_HAS_CONVERSION (actual_type))
905                     {
906                       tree conv;
907                       /* Don't issue warnings since we're only groping
908                          around for the right answer, we haven't yet
909                          committed to going with this solution.  */
910                       int old_inhibit_warnings = inhibit_warnings;
911
912                       inhibit_warnings = 1;
913                       conv = build_type_conversion
914                         (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
915                       inhibit_warnings = old_inhibit_warnings;
916
917                       if (conv)
918                         {
919                           if (conv == error_mark_node)
920                             win += 2;
921                           else
922                             {
923                               win++;
924                               if (TREE_CODE (conv) != CALL_EXPR)
925                                 extra_conversions = 1;
926                             }
927                         }
928                       else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
929                         {
930                           conv = build_type_conversion (CALL_EXPR, formal_type,
931                                                         TREE_VALUE (tta), 0);
932                           if (conv)
933                             {
934                               if (conv == error_mark_node)
935                                 win += 2;
936                               else
937                                 {
938                                   win++;
939                                   if (TREE_CODE (conv) != CALL_EXPR)
940                                     extra_conversions = 1;
941                                 }
942                             }
943                         }
944                     }
945                 }
946               dont_convert_types = 0;
947
948               if (win == 1)
949                 {
950                   user_strikes += 1;
951                   cp->harshness[strike_index].code
952                     = USER_CODE | (extra_conversions ? STD_CODE : 0);
953                   win = 0;
954                 }
955               else
956                 {
957                   if (cp->u.bad_arg > strike_index)
958                     cp->u.bad_arg = strike_index;
959
960                   evil_strikes = win ? 2 : 1;
961                   break;
962                 }
963             }
964
965           ttf = TREE_CHAIN (ttf);
966           tta = TREE_CHAIN (tta);
967           strike_index += 1;
968         }
969     }
970
971   /* Const member functions get a small penalty because defaulting
972      to const is less useful than defaulting to non-const. */
973   /* This is bogus, it does not correspond to anything in the ARM.
974      This code will be fixed when this entire section is rewritten
975      to conform to the ARM.  (mrs)  */
976   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
977     {
978       tree this_parm = TREE_VALUE (ttf_in);
979
980       if (TREE_CODE (this_parm) == RECORD_TYPE  /* Is `this' a sig ptr?  */
981             ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
982             : TYPE_READONLY (TREE_TYPE (this_parm)))
983         {
984           cp->harshness[0].code |= TRIVIAL_CODE;
985           ++easy_strikes;
986         }
987       else
988         {
989           /* Calling a non-const member function from a const member function
990              is probably invalid, but for now we let it only draw a warning.
991              We indicate that such a mismatch has occurred by setting the
992              harshness to a maximum value.  */
993           if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
994               && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
995             cp->harshness[0].code |= CONST_CODE;
996         }
997     }
998
999   if (evil_strikes)
1000     cp->h.code = EVIL_CODE;
1001   if (ellipsis_strikes)
1002     cp->h.code |= ELLIPSIS_CODE;
1003   if (user_strikes)
1004     cp->h.code |= USER_CODE;
1005 #ifdef DEBUG_MATCHING
1006   cp_error ("final eval %s", print_harshness (&cp->h));
1007 #endif
1008 }
1009
1010 /* Subroutine of ideal_candidate.  See if X or Y is a better match
1011    than the other.  */
1012 static int
1013 strictly_better (x, y)
1014      unsigned short x, y;
1015 {
1016   unsigned short xor;
1017
1018   if (x == y)
1019     return 0;
1020
1021   xor = x ^ y;
1022   if (xor >= x || xor >= y)
1023     return 1;
1024   return 0;
1025 }
1026
1027 /* When one of several possible overloaded functions and/or methods
1028    can be called, choose the best candidate for overloading.
1029
1030    BASETYPE is the context from which we start method resolution
1031    or NULL if we are comparing overloaded functions.
1032    CANDIDATES is the array of candidates we have to choose from.
1033    N_CANDIDATES is the length of CANDIDATES.
1034    PARMS is a TREE_LIST of parameters to the function we'll ultimately
1035    choose.  It is modified in place when resolving methods.  It is not
1036    modified in place when resolving overloaded functions.
1037    LEN is the length of the parameter list.  */
1038
1039 static struct candidate *
1040 ideal_candidate (basetype, candidates, n_candidates, parms, len)
1041      tree basetype;
1042      struct candidate *candidates;
1043      int n_candidates;
1044      tree parms;
1045      int len;
1046 {
1047   struct candidate *cp = candidates+n_candidates;
1048   int i, j = -1, best_code;
1049
1050   /* For each argument, sort the functions from best to worst for the arg.
1051      For each function that's not best for this arg, set its overall
1052      harshness to EVIL so that other args won't like it.  The candidate
1053      list for the last argument is the intersection of all the best-liked
1054      functions.  */
1055
1056 #if 0
1057   for (i = 0; i < len; i++)
1058     {
1059       qsort (candidates, n_candidates, sizeof (struct candidate),
1060              rank_for_overload);
1061       best_code = cp[-1].h.code;
1062
1063       /* To find out functions that are worse than that represented
1064          by BEST_CODE, we can't just do a comparison like h.code>best_code.
1065          The total harshness for the "best" fn may be 8|8 for two args, and
1066          the harshness for the next-best may be 8|2.  If we just compared,
1067          that would be checking 8>10, which would lead to the next-best
1068          being disqualified.  What we actually want to do is get rid
1069          of functions that are definitely worse than that represented
1070          by best_code, i.e. those which have bits set higher than the
1071          highest in best_code.  Sooooo, what we do is clear out everything
1072          represented by best_code, and see if we still come up with something
1073          higher.  If so (e.g., 8|8 vs 8|16), it'll disqualify it properly.  */
1074       for (j = n_candidates-2; j >= 0; j--)
1075         if ((candidates[j].h.code & ~best_code) > best_code)
1076           candidates[j].h.code = EVIL_CODE;
1077     }
1078
1079   if (cp[-1].h.code & EVIL_CODE)
1080     return NULL;
1081 #else
1082   qsort (candidates, n_candidates, sizeof (struct candidate),
1083          rank_for_overload);
1084   best_code = cp[-1].h.code;
1085 #endif
1086
1087   /* If they're at least as good as each other, do an arg-by-arg check.  */
1088   if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
1089     {
1090       int better = 0;
1091       int worse = 0;
1092
1093       for (j = 0; j < n_candidates; j++)
1094         if (! strictly_better (candidates[j].h.code, best_code))
1095           break;
1096
1097       qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
1098              rank_for_ideal);
1099       for (i = 0; i < len; i++)
1100         {
1101           if (cp[-1].harshness[i].code < cp[-2].harshness[i].code)
1102             better = 1;
1103           else if (cp[-1].harshness[i].code > cp[-2].harshness[i].code)
1104             worse = 1;
1105           else if (cp[-1].harshness[i].code & STD_CODE)
1106             {
1107               /* If it involves a standard conversion, let the
1108                  inheritance lattice be the final arbiter.  */
1109               if (cp[-1].harshness[i].distance > cp[-2].harshness[i].distance)
1110                 worse = 1;
1111               else if (cp[-1].harshness[i].distance < cp[-2].harshness[i].distance)
1112                 better = 1;
1113             }
1114           else if (cp[-1].harshness[i].code & PROMO_CODE)
1115             {
1116               /* For integral promotions, take into account a finer
1117                  granularity for determining which types should be favored
1118                  over others in such promotions.  */
1119               if (cp[-1].harshness[i].int_penalty > cp[-2].harshness[i].int_penalty)
1120                 worse = 1;
1121               else if (cp[-1].harshness[i].int_penalty < cp[-2].harshness[i].int_penalty)
1122                 better = 1;
1123             }
1124         }
1125
1126       if (! better || worse)
1127         return NULL;
1128     }
1129   return cp-1;
1130 }
1131
1132 /* Assume that if the class referred to is not in the
1133    current class hierarchy, that it may be remote.
1134    PARENT is assumed to be of aggregate type here.  */
1135 static int
1136 may_be_remote (parent)
1137      tree parent;
1138 {
1139   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
1140     return 0;
1141
1142   if (current_class_type == NULL_TREE)
1143     return 0;
1144
1145   if (parent == current_class_type)
1146     return 0;
1147
1148   if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
1149     return 0;
1150   return 1;
1151 }
1152
1153 tree
1154 build_vfield_ref (datum, type)
1155      tree datum, type;
1156 {
1157   tree rval;
1158   int old_assume_nonnull_objects = flag_assume_nonnull_objects;
1159
1160   if (datum == error_mark_node)
1161     return error_mark_node;
1162
1163   /* Vtable references are always made from non-null objects.  */
1164   flag_assume_nonnull_objects = 1;
1165   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
1166     datum = convert_from_reference (datum);
1167
1168   if (! TYPE_USES_COMPLEX_INHERITANCE (type))
1169     rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
1170                   datum, CLASSTYPE_VFIELD (type));
1171   else
1172     rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
1173   flag_assume_nonnull_objects = old_assume_nonnull_objects;
1174
1175   return rval;
1176 }
1177
1178 /* Build a call to a member of an object.  I.e., one that overloads
1179    operator ()(), or is a pointer-to-function or pointer-to-method.  */
1180 static tree
1181 build_field_call (basetype_path, instance_ptr, name, parms)
1182      tree basetype_path, instance_ptr, name, parms;
1183 {
1184   tree field, instance;
1185
1186   if (instance_ptr == current_class_decl)
1187     {
1188       /* Check to see if we really have a reference to an instance variable
1189          with `operator()()' overloaded.  */
1190       field = IDENTIFIER_CLASS_VALUE (name);
1191
1192       if (field == NULL_TREE)
1193         {
1194           cp_error ("`this' has no member named `%D'", name);
1195           return error_mark_node;
1196         }
1197
1198       if (TREE_CODE (field) == FIELD_DECL)
1199         {
1200           /* If it's a field, try overloading operator (),
1201              or calling if the field is a pointer-to-function.  */
1202           instance = build_component_ref_1 (C_C_D, field, 0);
1203           if (instance == error_mark_node)
1204             return error_mark_node;
1205
1206           if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1207               && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
1208             return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
1209
1210           if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1211             {
1212               if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
1213                 return build_function_call (instance, parms);
1214               else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
1215                 return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
1216             }
1217         }
1218       return NULL_TREE;
1219     }
1220
1221   /* Check to see if this is not really a reference to an instance variable
1222      with `operator()()' overloaded.  */
1223   field = lookup_field (basetype_path, name, 1, 0);
1224
1225   /* This can happen if the reference was ambiguous or for access
1226      violations.  */
1227   if (field == error_mark_node)
1228     return error_mark_node;
1229
1230   if (field)
1231     {
1232       tree basetype;
1233       tree ftype = TREE_TYPE (field);
1234
1235       if (TREE_CODE (ftype) == REFERENCE_TYPE)
1236         ftype = TREE_TYPE (ftype);
1237
1238       if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
1239         {
1240           /* Make the next search for this field very short.  */
1241           basetype = DECL_FIELD_CONTEXT (field);
1242           instance_ptr = convert_pointer_to (basetype, instance_ptr);
1243
1244           instance = build_indirect_ref (instance_ptr, NULL_PTR);
1245           return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
1246                                  build_component_ref_1 (instance, field, 0),
1247                                  parms, NULL_TREE);
1248         }
1249       if (TREE_CODE (ftype) == POINTER_TYPE)
1250         {
1251           if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
1252               || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
1253             {
1254               /* This is a member which is a pointer to function.  */
1255               tree ref
1256                 = build_component_ref_1 (build_indirect_ref (instance_ptr,
1257                                                              NULL_PTR),
1258                                          field, LOOKUP_COMPLAIN);
1259               if (ref == error_mark_node)
1260                 return error_mark_node;
1261               return build_function_call (ref, parms);
1262             }
1263         }
1264       else if (TREE_CODE (ftype) == METHOD_TYPE)
1265         {
1266           error ("invalid call via pointer-to-member function");
1267           return error_mark_node;
1268         }
1269       else
1270         return NULL_TREE;
1271     }
1272   return NULL_TREE;
1273 }
1274
1275 tree
1276 find_scoped_type (type, inner_name, inner_types)
1277      tree type, inner_name, inner_types;
1278 {
1279   tree tags = CLASSTYPE_TAGS (type);
1280
1281   while (tags)
1282     {
1283       /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1284          enclosing class) is set to the name for the enum type.  So, if
1285          inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1286          then this test will be true.  */
1287       if (TREE_PURPOSE (tags) == inner_name)
1288         {
1289           if (inner_types == NULL_TREE)
1290             return DECL_NESTED_TYPENAME (TYPE_NAME (TREE_VALUE (tags)));
1291           return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
1292         }
1293       tags = TREE_CHAIN (tags);
1294     }
1295
1296 #if 0
1297   /* XXX This needs to be fixed better.  */
1298   if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
1299     {
1300       sorry ("nested class lookup in template type");
1301       return NULL_TREE;
1302     }
1303 #endif
1304
1305   /* Look for a TYPE_DECL.  */
1306   for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
1307     if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
1308       {
1309         /* Code by raeburn.  */
1310         if (inner_types == NULL_TREE)
1311           return DECL_NESTED_TYPENAME (tags);
1312         return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
1313       }
1314
1315   return NULL_TREE;
1316 }
1317
1318 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1319    the name that names the above nested type.  INNER_TYPES
1320    is a chain of nested type names (held together by SCOPE_REFs);
1321    OUTER_TYPE is the type we know to enclose INNER_TYPES.
1322    Returns NULL_TREE if there is an error.  */
1323 tree
1324 resolve_scope_to_name (outer_type, inner_stuff)
1325      tree outer_type, inner_stuff;
1326 {
1327   register tree tmp;
1328   tree inner_name, inner_type;
1329
1330   if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
1331     {
1332       /* We first try to look for a nesting in our current class context,
1333          then try any enclosing classes.  */
1334       tree type = current_class_type;
1335       
1336       while (type && (TREE_CODE (type) == RECORD_TYPE
1337                       || TREE_CODE (type) == UNION_TYPE))
1338         {
1339           tree rval = resolve_scope_to_name (type, inner_stuff);
1340
1341           if (rval != NULL_TREE)
1342             return rval;
1343           type = DECL_CONTEXT (TYPE_NAME (type));
1344         }
1345     }
1346
1347   if (TREE_CODE (inner_stuff) == SCOPE_REF)
1348     {
1349       inner_name = TREE_OPERAND (inner_stuff, 0);
1350       inner_type = TREE_OPERAND (inner_stuff, 1);
1351     }
1352   else
1353     {
1354       inner_name = inner_stuff;
1355       inner_type = NULL_TREE;
1356     }
1357
1358   if (outer_type == NULL_TREE)
1359     {
1360       /* If we have something that's already a type by itself,
1361          use that.  */
1362       if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
1363         {
1364           if (inner_type)
1365             return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
1366                                           inner_type);
1367           return inner_name;
1368         }
1369       return NULL_TREE;
1370     }
1371
1372   if (! IS_AGGR_TYPE (outer_type))
1373     return NULL_TREE;
1374
1375   /* Look for member classes or enums.  */
1376   tmp = find_scoped_type (outer_type, inner_name, inner_type);
1377
1378   /* If it's not a type in this class, then go down into the
1379      base classes and search there.  */
1380   if (! tmp && TYPE_BINFO (outer_type))
1381     {
1382       tree binfos = TYPE_BINFO_BASETYPES (outer_type);
1383       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1384
1385       for (i = 0; i < n_baselinks; i++)
1386         {
1387           tree base_binfo = TREE_VEC_ELT (binfos, i);
1388           tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
1389           if (tmp)
1390             return tmp;
1391         }
1392       tmp = NULL_TREE;
1393     }
1394
1395   return tmp;
1396 }
1397
1398 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1399    This is how virtual function calls are avoided.  */
1400 tree
1401 build_scoped_method_call (exp, scopes, name, parms)
1402      tree exp, scopes, name, parms;
1403 {
1404   /* Because this syntactic form does not allow
1405      a pointer to a base class to be `stolen',
1406      we need not protect the derived->base conversion
1407      that happens here.
1408      
1409      @@ But we do have to check access privileges later.  */
1410   tree basename = resolve_scope_to_name (NULL_TREE, scopes);
1411   tree basetype, binfo, decl;
1412   tree type = TREE_TYPE (exp);
1413
1414   if (type == error_mark_node
1415       || basename == NULL_TREE)
1416     return error_mark_node;
1417
1418   basetype = IDENTIFIER_TYPE_VALUE (basename);
1419
1420   if (TREE_CODE (type) == REFERENCE_TYPE)
1421     type = TREE_TYPE (type);
1422
1423   /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1424      that explicit ~int is caught in the parser; this deals with typedefs
1425      and template parms.  */
1426   if (TREE_CODE (name) == BIT_NOT_EXPR && ! is_aggr_typedef (basename, 0))
1427     {
1428       if (type != basetype)
1429         cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1430                   exp, basetype, type);
1431       name = IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0));
1432       if (basetype != name)
1433         cp_error ("qualified type `%T' does not match destructor type `%T'",
1434                   basetype, name);
1435       return void_zero_node;
1436     }
1437
1438   if (! is_aggr_typedef (basename, 1))
1439     return error_mark_node;
1440
1441   if (! IS_AGGR_TYPE (type))
1442     {
1443       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1444                 exp, type);
1445       return error_mark_node;
1446     }
1447
1448   if ((binfo = binfo_or_else (basetype, type)))
1449     {
1450       if (binfo == error_mark_node)
1451         return error_mark_node;
1452       if (TREE_CODE (exp) == INDIRECT_REF)
1453         decl = build_indirect_ref (convert_pointer_to (binfo,
1454                                                        build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
1455       else
1456         decl = build_scoped_ref (exp, scopes);
1457
1458       /* Call to a destructor.  */
1459       if (TREE_CODE (name) == BIT_NOT_EXPR)
1460         {
1461           /* Explicit call to destructor.  */
1462           name = TREE_OPERAND (name, 0);
1463           if (name != constructor_name (TREE_TYPE (decl)))
1464             {
1465               cp_error
1466                 ("qualified type `%T' does not match destructor type `%T'",
1467                  TREE_TYPE (decl), name);
1468               return error_mark_node;
1469             }
1470           if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
1471             return void_zero_node;
1472           
1473           return build_delete (TREE_TYPE (decl), decl, integer_two_node,
1474                                LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
1475                                0);
1476         }
1477
1478       /* Call to a method.  */
1479       return build_method_call (decl, name, parms, binfo,
1480                                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1481     }
1482   return error_mark_node;
1483 }
1484
1485 static void
1486 print_candidates (candidates)
1487      tree candidates;
1488 {
1489   cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
1490   candidates = TREE_CHAIN (candidates);
1491
1492   while (candidates)
1493     {
1494       cp_error_at ("                %D", TREE_VALUE (candidates));
1495       candidates = TREE_CHAIN (candidates);
1496     }
1497 }
1498
1499 static void
1500 print_n_candidates (candidates, n)
1501      struct candidate *candidates;
1502      int n;
1503 {
1504   int i;
1505
1506   cp_error_at ("candidates are: %D", candidates[0].function);
1507   for (i = 1; i < n; i++)
1508     cp_error_at ("                %D", candidates[i].function);
1509 }
1510
1511 /* Build something of the form ptr->method (args)
1512    or object.method (args).  This can also build
1513    calls to constructors, and find friends.
1514
1515    Member functions always take their class variable
1516    as a pointer.
1517
1518    INSTANCE is a class instance.
1519
1520    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1521
1522    PARMS help to figure out what that NAME really refers to.
1523
1524    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1525    down to the real instance type to use for access checking.  We need this
1526    information to get protected accesses correct.  This parameter is used
1527    by build_member_call.
1528
1529    FLAGS is the logical disjunction of zero or more LOOKUP_
1530    flags.  See cp-tree.h for more info.
1531
1532    If this is all OK, calls build_function_call with the resolved
1533    member function.
1534
1535    This function must also handle being called to perform
1536    initialization, promotion/coercion of arguments, and
1537    instantiation of default parameters.
1538
1539    Note that NAME may refer to an instance variable name.  If
1540    `operator()()' is defined for the type of that field, then we return
1541    that result.  */
1542 tree
1543 build_method_call (instance, name, parms, basetype_path, flags)
1544      tree instance, name, parms, basetype_path;
1545      int flags;
1546 {
1547   register tree function, fntype, value_type;
1548   register tree basetype, save_basetype;
1549   register tree baselink, result, method_name, parmtypes, parm;
1550   tree last;
1551   int pass;
1552   enum access_type access = access_public;
1553
1554   /* Range of cases for vtable optimization.  */
1555   enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
1556   enum vtable_needs need_vtbl = not_needed;
1557
1558   char *name_kind;
1559   int ever_seen = 0;
1560   tree instance_ptr = NULL_TREE;
1561   int all_virtual = flag_all_virtual;
1562   int static_call_context = 0;
1563   tree found_fns = NULL_TREE;
1564
1565   /* Keep track of `const' and `volatile' objects.  */
1566   int constp, volatilep;
1567
1568 #ifdef GATHER_STATISTICS
1569   n_build_method_call++;
1570 #endif
1571
1572   if (instance == error_mark_node
1573       || name == error_mark_node
1574       || parms == error_mark_node
1575       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
1576     return error_mark_node;
1577
1578   /* This is the logic that magically deletes the second argument to
1579      operator delete, if it is not needed. */
1580   if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
1581     {
1582       tree save_last = TREE_CHAIN (parms);
1583       tree result;
1584       /* get rid of unneeded argument */
1585       TREE_CHAIN (parms) = NULL_TREE;
1586       result = build_method_call (instance, name, parms, basetype_path,
1587                                   (LOOKUP_SPECULATIVELY|flags)
1588                                   &~LOOKUP_COMPLAIN);
1589       /* If it works, return it. */
1590       if (result && result != error_mark_node)
1591         return build_method_call (instance, name, parms, basetype_path, flags);
1592       /* If it doesn't work, two argument delete must work */
1593       TREE_CHAIN (parms) = save_last;
1594     }
1595   /* We already know whether it's needed or not for vec delete.  */
1596   else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
1597            && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
1598     TREE_CHAIN (parms) = NULL_TREE;
1599
1600   if (TREE_CODE (name) == BIT_NOT_EXPR)
1601     {
1602       flags |= LOOKUP_DESTRUCTOR;
1603       name = TREE_OPERAND (name, 0);
1604       if (parms)
1605         error ("destructors take no parameters");
1606       basetype = TREE_TYPE (instance);
1607       if (IS_AGGR_TYPE (basetype))
1608         {
1609           if (name == constructor_name (basetype))
1610             goto huzzah;
1611         }
1612       else
1613         {
1614           if (basetype == get_type_value (name))
1615             goto huzzah;
1616         }
1617       cp_error ("destructor name `~%D' does not match type `%T' of expression",
1618                 name, basetype);
1619       return void_zero_node;
1620
1621     huzzah:
1622       if (! TYPE_HAS_DESTRUCTOR (basetype))
1623         return void_zero_node;
1624       instance = default_conversion (instance);
1625       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1626       return build_delete (build_pointer_type (basetype),
1627                            instance_ptr, integer_two_node,
1628                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
1629     }
1630
1631   {
1632     char *xref_name;
1633     
1634     /* Initialize name for error reporting.  */
1635     if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
1636       {
1637         char *p = operator_name_string (name);
1638         xref_name = (char *)alloca (strlen (p) + 10);
1639         sprintf (xref_name, "operator %s", p);
1640       }
1641     else if (TREE_CODE (name) == SCOPE_REF)
1642       xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
1643     else
1644       xref_name = IDENTIFIER_POINTER (name);
1645
1646     GNU_xref_call (current_function_decl, xref_name);
1647   }
1648
1649   if (instance == NULL_TREE)
1650     {
1651       basetype = NULL_TREE;
1652       /* Check cases where this is really a call to raise
1653          an exception.  */
1654       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
1655         {
1656           basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
1657           if (basetype)
1658             basetype = TREE_VALUE (basetype);
1659         }
1660       else if (TREE_CODE (name) == SCOPE_REF
1661                && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
1662         {
1663           if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
1664             return error_mark_node;
1665           basetype = purpose_member (TREE_OPERAND (name, 1),
1666                                      CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
1667           if (basetype)
1668             basetype = TREE_VALUE (basetype);
1669         }
1670
1671       if (basetype != NULL_TREE)
1672         ;
1673       /* call to a constructor... */
1674       else if (basetype_path)
1675         basetype = BINFO_TYPE (basetype_path);
1676       else if (IDENTIFIER_HAS_TYPE_VALUE (name))
1677         {
1678           basetype = IDENTIFIER_TYPE_VALUE (name);
1679           name = constructor_name_full (basetype);
1680         }
1681       else
1682         {
1683           tree typedef_name = lookup_name (name, 1);
1684           if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
1685             {
1686               /* Canonicalize the typedef name.  */
1687               basetype = TREE_TYPE (typedef_name);
1688               name = TYPE_IDENTIFIER (basetype);
1689             }
1690           else
1691             {
1692               cp_error ("no constructor named `%T' in scope",
1693                         name);
1694               return error_mark_node;
1695             }
1696         }
1697
1698       if (! IS_AGGR_TYPE (basetype))
1699         {
1700         non_aggr_error:
1701           if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
1702             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1703                       name, instance, basetype);
1704
1705           return error_mark_node;
1706         }
1707     }
1708   else if (instance == C_C_D || instance == current_class_decl)
1709     {
1710       /* When doing initialization, we side-effect the TREE_TYPE of
1711          C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
1712       basetype = TREE_TYPE (C_C_D);
1713
1714       /* Anything manifestly `this' in constructors and destructors
1715          has a known type, so virtual function tables are not needed.  */
1716       if (TYPE_VIRTUAL_P (basetype)
1717           && !(flags & LOOKUP_NONVIRTUAL))
1718         need_vtbl = (dtor_label || ctor_label)
1719           ? unneeded : maybe_needed;
1720
1721       instance = C_C_D;
1722       instance_ptr = current_class_decl;
1723       result = build_field_call (TYPE_BINFO (current_class_type),
1724                                  instance_ptr, name, parms);
1725
1726       if (result)
1727         return result;
1728     }
1729   else if (TREE_CODE (instance) == RESULT_DECL)
1730     {
1731       basetype = TREE_TYPE (instance);
1732       /* Should we ever have to make a virtual function reference
1733          from a RESULT_DECL, know that it must be of fixed type
1734          within the scope of this function.  */
1735       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1736         need_vtbl = maybe_needed;
1737       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
1738     }
1739   else
1740     {
1741       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
1742       tree inst_ptr_basetype;
1743
1744       static_call_context =
1745         (TREE_CODE (instance) == INDIRECT_REF
1746          && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
1747          && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
1748
1749       if (TREE_CODE (instance) == OFFSET_REF)
1750         instance = resolve_offset_ref (instance);
1751
1752       /* the base type of an instance variable is pointer to class */
1753       basetype = TREE_TYPE (instance);
1754
1755       if (TREE_CODE (basetype) == REFERENCE_TYPE)
1756         {
1757           basetype = TREE_TYPE (basetype);
1758           if (! IS_AGGR_TYPE (basetype))
1759             goto non_aggr_error;
1760           /* Call to convert not needed because we are remaining
1761              within the same type.  */
1762           instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
1763                                  instance);
1764           inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
1765         }
1766       else
1767         {
1768           if (! IS_AGGR_TYPE (basetype))
1769             goto non_aggr_error;
1770
1771           /* If `instance' is a signature pointer/reference and `name' is
1772              not a constructor, we are calling a signature member function.
1773              In that case set the `basetype' to the signature type.  */
1774           if ((IS_SIGNATURE_POINTER (basetype)
1775                || IS_SIGNATURE_REFERENCE (basetype))
1776               && TYPE_IDENTIFIER (basetype) != name)
1777             basetype = SIGNATURE_TYPE (basetype);
1778
1779           if ((IS_SIGNATURE (basetype)
1780                && (instance_ptr = build_optr_ref (instance)))
1781               || (lvalue_p (instance)
1782                && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
1783               || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
1784             {
1785               if (instance_ptr == error_mark_node)
1786                 return error_mark_node;
1787             }
1788           else if (TREE_CODE (instance) == NOP_EXPR
1789                    || TREE_CODE (instance) == CONSTRUCTOR)
1790             {
1791               /* A cast is not an lvalue.  Initialize a fresh temp
1792                  with the value we are casting from, and proceed with
1793                  that temporary.  We can't cast to a reference type,
1794                  so that simplifies the initialization to something
1795                  we can manage.  */
1796               tree temp = get_temp_name (TREE_TYPE (instance), 0);
1797               if (IS_AGGR_TYPE (TREE_TYPE (instance)))
1798                 expand_aggr_init (temp, instance, 0);
1799               else
1800                 {
1801                   store_init_value (temp, instance);
1802                   expand_decl_init (temp);
1803                 }
1804               instance = temp;
1805               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1806             }
1807           else
1808             {
1809               if (TREE_CODE (instance) != CALL_EXPR)
1810                 my_friendly_abort (125);
1811               if (TYPE_NEEDS_CONSTRUCTING (basetype))
1812                 instance = build_cplus_new (basetype, instance, 0);
1813               else
1814                 {
1815                   instance = get_temp_name (basetype, 0);
1816                   TREE_ADDRESSABLE (instance) = 1;
1817                 }
1818               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1819             }
1820           /* @@ Should we call comp_target_types here?  */
1821           inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
1822           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
1823             basetype = inst_ptr_basetype;
1824           else
1825             {
1826               instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
1827               if (instance_ptr == error_mark_node)
1828                 return error_mark_node;
1829             }
1830         }
1831
1832       /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1833          not updated, so we use `basetype' instead.  */
1834       if (basetype_path == NULL_TREE
1835           && IS_SIGNATURE (basetype))
1836         basetype_path = TYPE_BINFO (basetype);
1837       else if (basetype_path == NULL_TREE ||
1838         BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
1839         basetype_path = TYPE_BINFO (inst_ptr_basetype);
1840
1841       result = build_field_call (basetype_path, instance_ptr, name, parms);
1842       if (result)
1843         return result;
1844
1845       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1846         {
1847           if (TREE_SIDE_EFFECTS (instance_ptr))
1848             {
1849               /* This action is needed because the instance is needed
1850                  for providing the base of the virtual function table.
1851                  Without using a SAVE_EXPR, the function we are building
1852                  may be called twice, or side effects on the instance
1853                  variable (such as a post-increment), may happen twice.  */
1854               instance_ptr = save_expr (instance_ptr);
1855               instance = build_indirect_ref (instance_ptr, NULL_PTR);
1856             }
1857           else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1858             {
1859               /* This happens when called for operator new ().  */
1860               instance = build_indirect_ref (instance, NULL_PTR);
1861             }
1862
1863           need_vtbl = maybe_needed;
1864         }
1865     }
1866
1867   if (TYPE_SIZE (basetype) == 0)
1868     {
1869       /* This is worth complaining about, I think.  */
1870       cp_error ("cannot lookup method in incomplete type `%T'", basetype);
1871       return error_mark_node;
1872     }
1873
1874   save_basetype = TYPE_MAIN_VARIANT (basetype);
1875
1876 #if 0
1877   if (all_virtual == 1
1878       && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
1879                      OPERATOR_METHOD_LENGTH)
1880           || instance_ptr == NULL_TREE
1881           || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0)))
1882     all_virtual = 0;
1883 #endif
1884
1885   last = NULL_TREE;
1886   for (parmtypes = NULL_TREE, parm = parms; parm; parm = TREE_CHAIN (parm))
1887     {
1888       tree t = TREE_TYPE (TREE_VALUE (parm));
1889       if (TREE_CODE (t) == OFFSET_TYPE)
1890         {
1891           /* Convert OFFSET_TYPE entities to their normal selves.  */
1892           TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
1893           t = TREE_TYPE (TREE_VALUE (parm));
1894         }
1895       if (TREE_CODE (TREE_VALUE (parm)) == OFFSET_REF
1896           && TREE_CODE (t) == METHOD_TYPE)
1897         {
1898           TREE_VALUE (parm) = build_unary_op (ADDR_EXPR, TREE_VALUE (parm), 0);
1899         }
1900       if (TREE_CODE (t) == ARRAY_TYPE)
1901         {
1902           /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
1903              This eliminates needless calls to `compute_conversion_costs'.  */
1904           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
1905           t = TREE_TYPE (TREE_VALUE (parm));
1906         }
1907       if (t == error_mark_node)
1908         return error_mark_node;
1909       last = build_tree_list (NULL_TREE, t);
1910       parmtypes = chainon (parmtypes, last);
1911     }
1912
1913   if (instance)
1914     {
1915       /* TREE_READONLY (instance) fails for references.  */
1916       constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
1917       volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
1918       parms = tree_cons (NULL_TREE, instance_ptr, parms);
1919     }
1920   else
1921     {
1922       /* Raw constructors are always in charge.  */
1923       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
1924           && ! (flags & LOOKUP_HAS_IN_CHARGE))
1925         {
1926           flags |= LOOKUP_HAS_IN_CHARGE;
1927           parms = tree_cons (NULL_TREE, integer_one_node, parms);
1928           parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
1929         }
1930
1931       if (flag_this_is_variable > 0)
1932         {
1933           constp = 0;
1934           volatilep = 0;
1935           parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
1936         }
1937       else
1938         {
1939           constp = 0;
1940           volatilep = 0;
1941           instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
1942           if (instance_ptr == error_mark_node)
1943             return error_mark_node;
1944           instance_ptr = save_expr (instance_ptr);
1945           TREE_CALLS_NEW (instance_ptr) = 1;
1946           instance = build_indirect_ref (instance_ptr, NULL_PTR);
1947
1948           /* If it's a default argument initialized from a ctor, what we get
1949              from instance_ptr will match the arglist for the FUNCTION_DECL
1950              of the constructor.  */
1951           if (parms && TREE_CODE (TREE_VALUE (parms)) == CALL_EXPR
1952               && TREE_OPERAND (TREE_VALUE (parms), 1)
1953               && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms), 1))))
1954             parms = build_tree_list (NULL_TREE, instance_ptr);
1955           else
1956             parms = tree_cons (NULL_TREE, instance_ptr, parms);
1957         }
1958     }
1959
1960   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
1961
1962   if (last == NULL_TREE)
1963     last = parmtypes;
1964
1965   /* Look up function name in the structure type definition.  */
1966
1967   if ((IDENTIFIER_HAS_TYPE_VALUE (name)
1968        && ! IDENTIFIER_OPNAME_P (name)
1969        && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name))
1970        && TREE_CODE (IDENTIFIER_TYPE_VALUE (name)) != UNINSTANTIATED_P_TYPE)
1971       || name == constructor_name (basetype))
1972     {
1973       tree tmp = NULL_TREE;
1974       if (IDENTIFIER_TYPE_VALUE (name) == basetype
1975           || name == constructor_name (basetype))
1976         tmp = TYPE_BINFO (basetype);
1977       else
1978         tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
1979       
1980       if (tmp != NULL_TREE)
1981         {
1982           name_kind = "constructor";
1983           
1984           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
1985               && ! (flags & LOOKUP_HAS_IN_CHARGE))
1986             {
1987               /* Constructors called for initialization
1988                  only are never in charge.  */
1989               tree tmplist;
1990               
1991               flags |= LOOKUP_HAS_IN_CHARGE;
1992               tmplist = tree_cons (NULL_TREE, integer_zero_node,
1993                                    TREE_CHAIN (parms));
1994               TREE_CHAIN (parms) = tmplist;
1995               tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
1996               TREE_CHAIN (parmtypes) = tmplist;
1997             }
1998           basetype = BINFO_TYPE (tmp);
1999         }
2000       else
2001         name_kind = "method";
2002     }
2003   else
2004     name_kind = "method";
2005   
2006   if (basetype_path == NULL_TREE
2007       || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
2008     basetype_path = TYPE_BINFO (basetype);
2009   result = lookup_fnfields (basetype_path, name,
2010                             (flags & LOOKUP_COMPLAIN));
2011   if (result == error_mark_node)
2012     return error_mark_node;
2013
2014
2015   /* Now, go look for this method name.  We do not find destructors here.
2016
2017      Putting `void_list_node' on the end of the parmtypes
2018      fakes out `build_decl_overload' into doing the right thing.  */
2019   TREE_CHAIN (last) = void_list_node;
2020   method_name = build_decl_overload (name, parmtypes,
2021                                      1 + (name == constructor_name (save_basetype)
2022                                           || name == constructor_name_full (save_basetype)));
2023   TREE_CHAIN (last) = NULL_TREE;
2024
2025   for (pass = 0; pass < 2; pass++)
2026     {
2027       struct candidate *candidates;
2028       struct candidate *cp;
2029       int len;
2030       unsigned best = 1;
2031
2032       /* This increments every time we go up the type hierarchy.
2033          The idea is to prefer a function of the derived class if possible. */
2034       int b_or_d = 0;
2035
2036       baselink = result;
2037
2038       if (pass > 0)
2039         {
2040           candidates
2041             = (struct candidate *) alloca ((ever_seen+1)
2042                                            * sizeof (struct candidate));
2043           bzero (candidates, (ever_seen + 1) * sizeof (struct candidate));
2044           cp = candidates;
2045           len = list_length (parms);
2046           ever_seen = 0;
2047
2048           /* First see if a global function has a shot at it.  */
2049           if (flags & LOOKUP_GLOBAL)
2050             {
2051               tree friend_parms;
2052               tree parm = instance_ptr;
2053
2054               if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
2055                 {
2056                   /* TREE_VALUE (parms) may have been modified by now;
2057                      restore it to its original value. */
2058                   TREE_VALUE (parms) = parm;
2059                   friend_parms = parms;
2060                 }
2061               else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
2062                 {
2063                   tree new_type;
2064                   parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
2065                   new_type = c_build_type_variant (TREE_TYPE (parm), constp,
2066                                                    volatilep);
2067                   new_type = build_reference_type (new_type);
2068                   parm = convert (new_type, parm);
2069                   friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
2070                 }
2071               else
2072                 my_friendly_abort (167);
2073
2074               cp->h_len = len;
2075               cp->harshness = (struct harshness_code *)
2076                 alloca ((len + 1) * sizeof (struct harshness_code));
2077
2078               result = build_overload_call (name, friend_parms, 0, cp);
2079               /* If it turns out to be the one we were actually looking for
2080                  (it was probably a friend function), the return the
2081                  good result.  */
2082               if (TREE_CODE (result) == CALL_EXPR)
2083                 return result;
2084
2085               while ((cp->h.code & EVIL_CODE) == 0)
2086                 {
2087                   /* non-standard uses: set the field to 0 to indicate
2088                      we are using a non-member function.  */
2089                   cp->u.field = 0;
2090                   if (cp->harshness[len].distance == 0
2091                       && cp->h.code < best)
2092                     best = cp->h.code;
2093                   cp += 1;
2094                 }
2095             }
2096         }
2097
2098       while (baselink)
2099         {
2100           /* We have a hit (of sorts). If the parameter list is
2101              "error_mark_node", or some variant thereof, it won't
2102              match any methods.  Since we have verified that the is
2103              some method vaguely matching this one (in name at least),
2104              silently return.
2105              
2106              Don't stop for friends, however.  */
2107           basetype_path = TREE_PURPOSE (baselink);
2108
2109           function = TREE_VALUE (baselink);
2110           if (TREE_CODE (basetype_path) == TREE_LIST)
2111             basetype_path = TREE_VALUE (basetype_path);
2112           basetype = BINFO_TYPE (basetype_path);
2113
2114           /* Cast the instance variable if necessary.  */
2115           if (basetype != TYPE_MAIN_VARIANT
2116               (TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)))))
2117             {
2118               if (basetype == save_basetype)
2119                 TREE_VALUE (parms) = instance_ptr;
2120               else
2121                 {
2122                   tree type = build_pointer_type
2123                     (build_type_variant (basetype, constp, volatilep));
2124                   TREE_VALUE (parms) = convert_force (type, instance_ptr);
2125                 }
2126             }
2127
2128           /* FIXME: this is the wrong place to get an error.  Hopefully
2129              the access-control rewrite will make this change more cleanly.  */
2130           if (TREE_VALUE (parms) == error_mark_node)
2131             return error_mark_node;
2132
2133           if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
2134             function = DECL_CHAIN (function);
2135
2136           for (; function; function = DECL_CHAIN (function))
2137             {
2138 #ifdef GATHER_STATISTICS
2139               n_inner_fields_searched++;
2140 #endif
2141               ever_seen++;
2142               if (pass > 0)
2143                 found_fns = tree_cons (NULL_TREE, function, found_fns);
2144
2145               /* Not looking for friends here.  */
2146               if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
2147                   && ! DECL_STATIC_FUNCTION_P (function))
2148                 continue;
2149
2150               if (pass == 0
2151                   && DECL_ASSEMBLER_NAME (function) == method_name)
2152                 goto found;
2153
2154               if (pass > 0)
2155                 {
2156                   tree these_parms = parms;
2157
2158 #ifdef GATHER_STATISTICS
2159                   n_inner_fields_searched++;
2160 #endif
2161                   cp->h_len = len;
2162                   cp->harshness = (struct harshness_code *)
2163                     alloca ((len + 1) * sizeof (struct harshness_code));
2164
2165                   if (DECL_STATIC_FUNCTION_P (function))
2166                     these_parms = TREE_CHAIN (these_parms);
2167                   compute_conversion_costs (function, these_parms, cp, len);
2168
2169                   if ((cp->h.code & EVIL_CODE) == 0)
2170                     {
2171                       cp->u.field = function;
2172                       cp->function = function;
2173                       cp->basetypes = basetype_path;
2174
2175                       /* No "two-level" conversions.  */
2176                       if (flags & LOOKUP_NO_CONVERSION
2177                           && (cp->h.code & USER_CODE))
2178                         continue;
2179
2180                       /* If we used default parameters, we must
2181                          check to see whether anyone else might
2182                          use them also, and report a possible
2183                          ambiguity.  */
2184                       if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
2185                           && cp->harshness[len].distance == 0
2186                           && cp->h.code < best)
2187                         {
2188                           if (! DECL_STATIC_FUNCTION_P (function))
2189                             TREE_VALUE (parms) = cp->arg;
2190                           if (best == 1)
2191                             goto found_and_maybe_warn;
2192                         }
2193                       cp++;
2194                     }
2195                 }
2196             }
2197           /* Now we have run through one link's member functions.
2198              arrange to head-insert this link's links.  */
2199           baselink = next_baselink (baselink);
2200           b_or_d += 1;
2201           /* Don't grab functions from base classes.  lookup_fnfield will
2202              do the work to get us down into the right place.  */
2203           baselink = NULL_TREE;
2204         }
2205       if (pass == 0)
2206         {
2207           tree igv = lookup_name_nonclass (name);
2208
2209           /* No exact match could be found.  Now try to find match
2210              using default conversions.  */
2211           if ((flags & LOOKUP_GLOBAL) && igv)
2212             {
2213               if (TREE_CODE (igv) == FUNCTION_DECL)
2214                 ever_seen += 1;
2215               else if (TREE_CODE (igv) == TREE_LIST)
2216                 ever_seen += count_functions (igv);
2217             }
2218
2219           if (ever_seen == 0)
2220             {
2221               if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2222                   == LOOKUP_SPECULATIVELY)
2223                 return NULL_TREE;
2224               
2225               TREE_CHAIN (last) = void_list_node;
2226               if (flags & LOOKUP_GLOBAL)
2227                 cp_error ("no global or member function `%D(%A)' defined",
2228                           name, parmtypes);
2229               else
2230                 cp_error ("no member function `%T::%D(%A)' defined",
2231                           save_basetype, name, TREE_CHAIN (parmtypes));
2232               return error_mark_node;
2233             }
2234           continue;
2235         }
2236
2237       if (cp - candidates != 0)
2238         {
2239           /* Rank from worst to best.  Then cp will point to best one.
2240              Private fields have their bits flipped.  For unsigned
2241              numbers, this should make them look very large.
2242              If the best alternate has a (signed) negative value,
2243              then all we ever saw were private members.  */
2244           if (cp - candidates > 1)
2245             {
2246               int n_candidates = cp - candidates;
2247               TREE_VALUE (parms) = instance_ptr;
2248               cp = ideal_candidate (save_basetype, candidates,
2249                                     n_candidates, parms, len);
2250               if (cp == (struct candidate *)0)
2251                 {
2252                   if (flags & LOOKUP_COMPLAIN)
2253                     {
2254                       cp_error ("call of overloaded %s `%D' is ambiguous",
2255                                 name_kind, name);
2256                       print_n_candidates (candidates, n_candidates);
2257                     }
2258                   return error_mark_node;
2259                 }
2260               if (cp->h.code & EVIL_CODE)
2261                 return error_mark_node;
2262             }
2263           else if (cp[-1].h.code & EVIL_CODE)
2264             {
2265               if (flags & LOOKUP_COMPLAIN)
2266                 cp_error ("ambiguous type conversion requested for %s `%D'",
2267                           name_kind, name);
2268               return error_mark_node;
2269             }
2270           else
2271             cp--;
2272
2273           /* The global function was the best, so use it.  */
2274           if (cp->u.field == 0)
2275             {
2276               /* We must convert the instance pointer into a reference type.
2277                  Global overloaded functions can only either take
2278                  aggregate objects (which come for free from references)
2279                  or reference data types anyway.  */
2280               TREE_VALUE (parms) = copy_node (instance_ptr);
2281               TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
2282               return build_function_call (cp->function, parms);
2283             }
2284
2285           function = cp->function;
2286           basetype_path = cp->basetypes;
2287           if (! DECL_STATIC_FUNCTION_P (function))
2288             TREE_VALUE (parms) = cp->arg;
2289           goto found_and_maybe_warn;
2290         }
2291
2292       if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
2293         {
2294           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2295               == LOOKUP_SPECULATIVELY)
2296             return NULL_TREE;
2297
2298           if (DECL_STATIC_FUNCTION_P (cp->function))
2299             parms = TREE_CHAIN (parms);
2300           if (ever_seen)
2301             {
2302               if (flags & LOOKUP_SPECULATIVELY)
2303                 return NULL_TREE;
2304               if (static_call_context
2305                   && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
2306                 cp_error ("object missing in call to `%D'", cp->function);
2307               else if (ever_seen > 1)
2308                 {
2309                   TREE_CHAIN (last) = void_list_node;
2310                   cp_error ("no matching function for call to `%T::%D (%A)'",
2311                             TREE_TYPE (TREE_TYPE (instance_ptr)),
2312                             name, TREE_CHAIN (parmtypes));
2313                   TREE_CHAIN (last) = NULL_TREE;
2314                   print_candidates (found_fns);
2315                 }
2316               else
2317                 report_type_mismatch (cp, parms, name_kind);
2318               return error_mark_node;
2319             }
2320
2321           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2322               == LOOKUP_COMPLAIN)
2323             {
2324               cp_error ("%T has no method named %D", save_basetype, name);
2325               return error_mark_node;
2326             }
2327           return NULL_TREE;
2328         }
2329       continue;
2330
2331     found_and_maybe_warn:
2332       if ((cp->harshness[0].code & CONST_CODE)
2333           /* 12.1p2: Constructors can be called for const objects.  */
2334           && ! DECL_CONSTRUCTOR_P (cp->function))
2335         {
2336           if (flags & LOOKUP_COMPLAIN)
2337             {
2338               cp_error_at ("non-const member function `%D'", cp->function);
2339               error ("called for const object at this point in file");
2340             }
2341           /* Not good enough for a match.  */
2342           else
2343             return error_mark_node;
2344         }
2345       goto found;
2346     }
2347   /* Silently return error_mark_node.  */
2348   return error_mark_node;
2349
2350  found:
2351   if (flags & LOOKUP_PROTECT)
2352     access = compute_access (basetype_path, function);
2353
2354   if (access == access_private)
2355     {
2356       if (flags & LOOKUP_COMPLAIN)
2357         {
2358           cp_error_at ("%s `%+#D' is %s", name_kind, function, 
2359                        TREE_PRIVATE (function) ? "private"
2360                        : "from private base class");
2361           error ("within this context");
2362         }
2363       return error_mark_node;
2364     }
2365   else if (access == access_protected)
2366     {
2367       if (flags & LOOKUP_COMPLAIN)
2368         {
2369           cp_error_at ("%s `%+#D' %s", name_kind, function,
2370                        TREE_PROTECTED (function) ? "is protected"
2371                        : "has protected accessibility");
2372           error ("within this context");
2373         }
2374       return error_mark_node;
2375     }
2376
2377   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2378      type (if it exists) is a pointer to.  */
2379
2380   if (DECL_ABSTRACT_VIRTUAL_P (function)
2381       && instance == C_C_D
2382       && DECL_CONSTRUCTOR_P (current_function_decl)
2383       && ! (flags & LOOKUP_NONVIRTUAL)
2384       && value_member (function, get_abstract_virtuals (basetype)))
2385     cp_error ("abstract virtual `%#D' called from constructor", function);
2386
2387   if (IS_SIGNATURE (basetype) && static_call_context)
2388     {
2389       cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2390                 basetype, name);
2391       return error_mark_node;
2392         }
2393   else if (IS_SIGNATURE (basetype))
2394     return build_signature_method_call (basetype, instance, function, parms);
2395
2396   function = DECL_MAIN_VARIANT (function);
2397   /* Declare external function if necessary. */
2398   assemble_external (function);
2399
2400   fntype = TREE_TYPE (function);
2401   if (TREE_CODE (fntype) == POINTER_TYPE)
2402     fntype = TREE_TYPE (fntype);
2403   basetype = DECL_CLASS_CONTEXT (function);
2404
2405   /* If we are referencing a virtual function from an object
2406      of effectively static type, then there is no need
2407      to go through the virtual function table.  */
2408   if (need_vtbl == maybe_needed)
2409     {
2410       int fixed_type = resolves_to_fixed_type_p (instance, 0);
2411
2412       if (all_virtual == 1
2413           && DECL_VINDEX (function)
2414           && may_be_remote (basetype))
2415         need_vtbl = needed;
2416       else if (DECL_VINDEX (function))
2417         need_vtbl = fixed_type ? unneeded : needed;
2418       else
2419         need_vtbl = not_needed;
2420     }
2421
2422   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
2423       && !DECL_CONSTRUCTOR_P (function))
2424     {
2425       /* Let's be nice to the user for now, and give reasonable
2426          default behavior.  */
2427       instance_ptr = current_class_decl;
2428       if (instance_ptr)
2429         {
2430           if (basetype != current_class_type)
2431             {
2432               tree binfo = get_binfo (basetype, current_class_type, 1);
2433               if (binfo == NULL_TREE)
2434                 {
2435                   error_not_base_type (function, current_class_type);
2436                   return error_mark_node;
2437                 }
2438               else if (basetype == error_mark_node)
2439                 return error_mark_node;
2440             }
2441         }
2442       /* Only allow a static member function to call another static member
2443          function.  */
2444       else if (DECL_LANG_SPECIFIC (function)
2445                && !DECL_STATIC_FUNCTION_P (function))
2446         {
2447           cp_error ("cannot call member function `%D' without object",
2448                     function);
2449           return error_mark_node;
2450         }
2451     }
2452
2453   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2454
2455   if (TYPE_SIZE (value_type) == 0)
2456     {
2457       if (flags & LOOKUP_COMPLAIN)
2458         incomplete_type_error (0, value_type);
2459       return error_mark_node;
2460     }
2461
2462   if (DECL_STATIC_FUNCTION_P (function))
2463     parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2464                                TREE_CHAIN (parms), function, LOOKUP_NORMAL);
2465   else if (need_vtbl == unneeded)
2466     {
2467       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
2468       basetype = TREE_TYPE (instance);
2469       if (TYPE_METHOD_BASETYPE (TREE_TYPE (function)) != TYPE_MAIN_VARIANT (basetype)
2470           && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2471         {
2472           basetype = DECL_CLASS_CONTEXT (function);
2473           instance_ptr = convert_pointer_to (basetype, instance_ptr);
2474           instance = build_indirect_ref (instance_ptr, NULL_PTR);
2475         }
2476       parms = tree_cons (NULL_TREE, instance_ptr,
2477                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, sub_flags));
2478     }
2479   else
2480     {
2481       if ((flags & LOOKUP_NONVIRTUAL) == 0)
2482         basetype = DECL_CONTEXT (function);
2483
2484       /* First parm could be integer_zerop with casts like
2485          ((Object*)0)->Object::IsA()  */
2486       if (!integer_zerop (TREE_VALUE (parms)))
2487         {
2488           /* Since we can't have inheritance with a union, doing get_binfo
2489              on it won't work.  We do all the convert_pointer_to_real
2490              stuff to handle MI correctly...for unions, that's not
2491              an issue, so we must short-circuit that extra work here.  */
2492           tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
2493           if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
2494             instance_ptr = TREE_VALUE (parms);
2495           else
2496             {
2497               tree binfo = get_binfo (basetype,
2498                                       TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
2499                                       0);
2500               instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
2501             }
2502           instance_ptr
2503             = convert_pointer_to (build_type_variant (basetype,
2504                                                       constp, volatilep),
2505                                   instance_ptr);
2506
2507           if (TREE_CODE (instance_ptr) == COND_EXPR)
2508             {
2509               instance_ptr = save_expr (instance_ptr);
2510               instance = build_indirect_ref (instance_ptr, NULL_PTR);
2511             }
2512           else if (TREE_CODE (instance_ptr) == NOP_EXPR
2513                    && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
2514                    && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
2515             ;
2516           /* The call to `convert_pointer_to' may return error_mark_node.  */
2517           else if (TREE_CODE (instance_ptr) == ERROR_MARK)
2518             return instance_ptr;
2519           else if (instance == NULL_TREE
2520                    || TREE_CODE (instance) != INDIRECT_REF
2521                    || TREE_OPERAND (instance, 0) != instance_ptr)
2522             instance = build_indirect_ref (instance_ptr, NULL_PTR);
2523         }
2524       parms = tree_cons (NULL_TREE, instance_ptr,
2525                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, LOOKUP_NORMAL));
2526     }
2527
2528 #if 0
2529   /* Constructors do not overload method calls.  */
2530   else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
2531            && name != TYPE_IDENTIFIER (basetype)
2532            && (TREE_CODE (function) != FUNCTION_DECL
2533                || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)),
2534                            OPERATOR_METHOD_FORMAT,
2535                            OPERATOR_METHOD_LENGTH))
2536            && (may_be_remote (basetype) || instance != C_C_D))
2537     {
2538       tree fn_as_int;
2539
2540       parms = TREE_CHAIN (parms);
2541
2542       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
2543         fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
2544       else
2545         fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
2546       if (all_virtual == 1)
2547         fn_as_int = convert (integer_type_node, fn_as_int);
2548
2549       result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
2550
2551       if (result == NULL_TREE)
2552         {
2553           compiler_error ("could not overload `operator->()(...)'");
2554           return error_mark_node;
2555         }
2556       else if (result == error_mark_node)
2557         return error_mark_node;
2558
2559 #if 0
2560       /* Do this if we want the result of operator->() to inherit
2561          the type of the function it is subbing for.  */
2562       TREE_TYPE (result) = value_type;
2563 #endif
2564
2565       return result;
2566     }
2567 #endif
2568
2569   if (need_vtbl == needed)
2570     {
2571       function = build_vfn_ref (&TREE_VALUE (parms), instance,
2572                                 DECL_VINDEX (function));
2573       TREE_TYPE (function) = build_pointer_type (fntype);
2574     }
2575
2576   if (TREE_CODE (function) == FUNCTION_DECL)
2577     GNU_xref_call (current_function_decl,
2578                    IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
2579
2580   {
2581     int is_constructor;
2582     
2583     if (TREE_CODE (function) == FUNCTION_DECL)
2584       {
2585         is_constructor = DECL_CONSTRUCTOR_P (function);
2586         if (DECL_INLINE (function))
2587           function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
2588         else
2589           {
2590             assemble_external (function);
2591             TREE_USED (function) = 1;
2592             function = default_conversion (function);
2593           }
2594       }
2595     else
2596       {
2597         is_constructor = 0;
2598         function = default_conversion (function);
2599       }
2600
2601     result = build_nt (CALL_EXPR, function, parms, NULL_TREE);
2602
2603     TREE_TYPE (result) = value_type;
2604     TREE_SIDE_EFFECTS (result) = 1;
2605     TREE_RAISES (result)
2606       = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
2607     TREE_HAS_CONSTRUCTOR (result) = is_constructor;
2608     return result;
2609   }
2610 }
2611
2612 /* Similar to `build_method_call', but for overloaded non-member functions.
2613    The name of this function comes through NAME.  The name depends
2614    on PARMS.
2615
2616    Note that this function must handle simple `C' promotions,
2617    as well as variable numbers of arguments (...), and
2618    default arguments to boot.
2619
2620    If the overloading is successful, we return a tree node which
2621    contains the call to the function.
2622
2623    If overloading produces candidates which are probable, but not definite,
2624    we hold these candidates.  If FINAL_CP is non-zero, then we are free
2625    to assume that final_cp points to enough storage for all candidates that
2626    this function might generate.  The `harshness' array is preallocated for
2627    the first candidate, but not for subsequent ones.
2628
2629    Note that the DECL_RTL of FUNCTION must be made to agree with this
2630    function's new name.  */
2631
2632 tree
2633 build_overload_call_real (fnname, parms, flags, final_cp, buildxxx)
2634      tree fnname, parms;
2635      int flags;
2636      struct candidate *final_cp;
2637      int buildxxx;
2638 {
2639   /* must check for overloading here */
2640   tree overload_name, functions, function, parm;
2641   tree parmtypes = NULL_TREE, last = NULL_TREE;
2642   register tree outer;
2643   int length;
2644   int parmlength = list_length (parms);
2645
2646   struct candidate *candidates, *cp;
2647
2648   if (final_cp)
2649     {
2650       final_cp[0].h.code = 0;
2651       final_cp[0].h.distance = 0;
2652       final_cp[0].function = 0;
2653       /* end marker.  */
2654       final_cp[1].h.code = EVIL_CODE;
2655     }
2656
2657   for (parm = parms; parm; parm = TREE_CHAIN (parm))
2658     {
2659       register tree t = TREE_TYPE (TREE_VALUE (parm));
2660
2661       if (t == error_mark_node)
2662         {
2663           if (final_cp)
2664             final_cp->h.code = EVIL_CODE;
2665           return error_mark_node;
2666         }
2667       if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
2668         {
2669           /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
2670              Also convert OFFSET_TYPE entities to their normal selves.
2671              This eliminates needless calls to `compute_conversion_costs'.  */
2672           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
2673           t = TREE_TYPE (TREE_VALUE (parm));
2674         }
2675       last = build_tree_list (NULL_TREE, t);
2676       parmtypes = chainon (parmtypes, last);
2677     }
2678   if (last)
2679     TREE_CHAIN (last) = void_list_node;
2680   else
2681     parmtypes = void_list_node;
2682
2683   functions = lookup_name_nonclass (fnname);
2684
2685   if (functions == NULL_TREE)
2686     {
2687       if (flags & LOOKUP_SPECULATIVELY)
2688         return NULL_TREE;
2689       if (flags & LOOKUP_COMPLAIN)
2690         error ("only member functions apply");
2691       if (final_cp)
2692         final_cp->h.code = EVIL_CODE;
2693       return error_mark_node;
2694     }
2695
2696   if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
2697     {
2698       functions = DECL_MAIN_VARIANT (functions);
2699       if (final_cp)
2700         {
2701           /* We are just curious whether this is a viable alternative or
2702              not.  */
2703           compute_conversion_costs (functions, parms, final_cp, parmlength);
2704           return functions;
2705         }
2706       else
2707         return build_function_call_real (functions, parms, 1, flags);
2708     }
2709
2710   if (TREE_CODE (functions) == TREE_LIST
2711       && TREE_VALUE (functions) == NULL_TREE)
2712     {
2713       if (flags & LOOKUP_SPECULATIVELY)
2714         return NULL_TREE;
2715       
2716       if (flags & LOOKUP_COMPLAIN)
2717         cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2718                   TREE_PURPOSE (functions));
2719       if (final_cp)
2720         final_cp->h.code = EVIL_CODE;
2721       return error_mark_node;
2722     }
2723
2724   length = count_functions (functions);
2725   
2726   if (final_cp)
2727     candidates = final_cp;
2728   else
2729     {
2730       candidates
2731         = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
2732       bzero (candidates, (length + 1) * sizeof (struct candidate));
2733     }
2734
2735   cp = candidates;
2736
2737   my_friendly_assert (is_overloaded_fn (functions), 169);
2738
2739   functions = get_first_fn (functions);
2740
2741   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
2742   for (outer = functions; outer; outer = DECL_CHAIN (outer))
2743     {
2744       int template_cost = 0;
2745       function = outer;
2746       if (TREE_CODE (function) != FUNCTION_DECL
2747           && ! (TREE_CODE (function) == TEMPLATE_DECL
2748                 && ! DECL_TEMPLATE_IS_CLASS (function)
2749                 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
2750         {
2751           enum tree_code code = TREE_CODE (function);
2752           if (code == TEMPLATE_DECL)
2753             code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
2754           if (code == CONST_DECL)
2755             cp_error_at
2756               ("enumeral value `%D' conflicts with function of same name",
2757                function);
2758           else if (code == VAR_DECL)
2759             {
2760               if (TREE_STATIC (function))
2761                 cp_error_at
2762                   ("variable `%D' conflicts with function of same name",
2763                    function);
2764               else
2765                 cp_error_at
2766                   ("constant field `%D' conflicts with function of same name",
2767                    function);
2768             }
2769           else if (code == TYPE_DECL)
2770             continue;
2771           else
2772             my_friendly_abort (2);
2773           error ("at this point in file");
2774           continue;
2775         }
2776       if (TREE_CODE (function) == TEMPLATE_DECL)
2777         {
2778           int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
2779           tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
2780           int i;
2781
2782           i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
2783                                 TYPE_ARG_TYPES (TREE_TYPE (function)),
2784                                 parms, &template_cost, 0);
2785           if (i == 0)
2786             function = instantiate_template (function, targs);
2787         }
2788
2789       if (TREE_CODE (function) == TEMPLATE_DECL)
2790         {
2791           /* Unconverted template -- failed match.  */
2792           cp->function = function;
2793           cp->u.bad_arg = -4;
2794           cp->h.code = EVIL_CODE;
2795         }
2796       else
2797         {
2798           struct candidate *cp2;
2799
2800           /* Check that this decl is not the same as a function that's in
2801              the list due to some template instantiation.  */
2802           cp2 = candidates;
2803           while (cp2 != cp)
2804             if (cp2->function == function)
2805               break;
2806             else
2807               cp2 += 1;
2808           if (cp2->function == function)
2809             continue;
2810
2811           function = DECL_MAIN_VARIANT (function);
2812
2813           /* Can't use alloca here, since result might be
2814              passed to calling function.  */
2815           cp->h_len = parmlength;
2816           cp->harshness = (struct harshness_code *)
2817             oballoc ((parmlength + 1) * sizeof (struct harshness_code));
2818
2819           compute_conversion_costs (function, parms, cp, parmlength);
2820
2821           /* Make sure this is clear as well.  */
2822           cp->h.int_penalty += template_cost;
2823
2824           if ((cp[0].h.code & EVIL_CODE) == 0)
2825             {
2826               cp[1].h.code = EVIL_CODE;
2827               cp++;
2828             }
2829         }
2830     }
2831
2832   if (cp - candidates)
2833     {
2834       tree rval = error_mark_node;
2835
2836       /* Leave marker.  */
2837       cp[0].h.code = EVIL_CODE;
2838       if (cp - candidates > 1)
2839         {
2840           struct candidate *best_cp
2841             = ideal_candidate (NULL_TREE, candidates,
2842                                cp - candidates, parms, parmlength);
2843           if (best_cp == (struct candidate *)0)
2844             {
2845               if (flags & LOOKUP_COMPLAIN)
2846                 {
2847                   cp_error ("call of overloaded `%D' is ambiguous", fnname);
2848                   print_n_candidates (candidates, cp - candidates);
2849                 }
2850               return error_mark_node;
2851             }
2852           else
2853             rval = best_cp->function;
2854         }
2855       else
2856         {
2857           cp -= 1;
2858           if (cp->h.code & EVIL_CODE)
2859             {
2860               if (flags & LOOKUP_COMPLAIN)
2861                 error ("type conversion ambiguous");
2862             }
2863           else
2864             rval = cp->function;
2865         }
2866
2867       if (final_cp)
2868         return rval;
2869
2870       return buildxxx ? build_function_call_real (rval, parms, 0, flags)
2871         : build_function_call_real (rval, parms, 1, flags);
2872     }
2873
2874   if (flags & LOOKUP_SPECULATIVELY)
2875     return NULL_TREE;
2876   
2877   if (flags & LOOKUP_COMPLAIN)
2878     report_type_mismatch (cp, parms, "function",
2879                           decl_as_string (cp->function, 1));
2880
2881   return error_mark_node;
2882 }
2883
2884 tree
2885 build_overload_call (fnname, parms, flags, final_cp)
2886      tree fnname, parms;
2887      int flags;
2888      struct candidate *final_cp;
2889 {
2890   return build_overload_call_real (fnname, parms, flags, final_cp, 0);
2891 }
2892
2893 tree
2894 build_overload_call_maybe (fnname, parms, flags, final_cp)
2895      tree fnname, parms;
2896      int flags;
2897      struct candidate *final_cp;
2898 {
2899   return build_overload_call_real (fnname, parms, flags, final_cp, 1);
2900 }