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