OSDN Git Service

* pt.c, call.c, decl.c, method.c, cp-tree.h: Remove unused NARGS
[pf3gnuchains/gcc-fork.git] / gcc / cp / call.c
1 /* Functions related to invoking methods and overloaded functions.
2    Copyright (C) 1987, 92, 93, 94, 95, 96, 1997 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 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37
38 #include "obstack.h"
39 #define obstack_chunk_alloc xmalloc
40 #define obstack_chunk_free free
41
42 extern int inhibit_warnings;
43 extern tree ctor_label, dtor_label;
44
45 /* Compute the ease with which a conversion can be performed
46    between an expected and the given type.  */
47
48 static struct harshness_code convert_harshness PROTO((register tree, register tree, tree));
49 static tree build_new_method_call               PROTO((tree, tree, tree, tree, int));
50
51 static int rank_for_ideal PROTO((struct candidate *,
52                                  struct candidate *));
53 static int user_harshness PROTO((tree, tree));
54 static int strictly_better PROTO((unsigned int, unsigned int));
55 static struct candidate * ideal_candidate PROTO((struct candidate *,
56                                                  int, int));
57 static int may_be_remote PROTO((tree));
58 static tree build_field_call PROTO((tree, tree, tree, tree));
59 static tree find_scoped_type PROTO((tree, tree, tree));
60 static void print_candidates PROTO((tree));
61 static struct z_candidate * tourney PROTO((struct z_candidate *));
62 static int joust PROTO((struct z_candidate *, struct z_candidate *));
63 static int compare_qual PROTO((tree, tree));
64 static int compare_ics PROTO((tree, tree));
65 static tree build_over_call PROTO((tree, tree, tree, int));
66 static tree convert_default_arg PROTO((tree, tree));
67 static void enforce_access PROTO((tree, tree));
68 static tree convert_like PROTO((tree, tree));
69 static void op_error PROTO((enum tree_code, enum tree_code, tree, tree,
70                             tree, char *));
71 static tree build_object_call PROTO((tree, tree));
72 static tree resolve_args PROTO((tree));
73 static struct z_candidate * build_user_type_conversion_1
74         PROTO ((tree, tree, int));
75 static void print_z_candidates PROTO((struct z_candidate *));
76 static tree build_this PROTO((tree));
77 static struct z_candidate * splice_viable PROTO((struct z_candidate *));
78 static int any_viable PROTO((struct z_candidate *));
79 static struct z_candidate * add_template_candidate
80         PROTO((struct z_candidate *, tree, tree, tree, tree, int));
81 static struct z_candidate * add_template_conv_candidate 
82         PROTO((struct z_candidate *, tree, tree, tree, tree));
83 static struct z_candidate * add_builtin_candidates
84         PROTO((struct z_candidate *, enum tree_code, enum tree_code,
85                tree, tree *, int));
86 static struct z_candidate * add_builtin_candidate
87         PROTO((struct z_candidate *, enum tree_code, enum tree_code,
88                tree, tree, tree, tree *, tree *, int));
89 static int is_complete PROTO((tree));
90 static struct z_candidate * build_builtin_candidate 
91         PROTO((struct z_candidate *, tree, tree, tree, tree *, tree *,
92                int));
93 static struct z_candidate * add_conv_candidate 
94         PROTO((struct z_candidate *, tree, tree, tree));
95 static struct z_candidate * add_function_candidate 
96         PROTO((struct z_candidate *, tree, tree, int));
97 static tree implicit_conversion PROTO((tree, tree, tree, int));
98 static tree standard_conversion PROTO((tree, tree, tree));
99 static tree reference_binding PROTO((tree, tree, tree, int));
100 static tree strip_top_quals PROTO((tree));
101 static tree non_reference PROTO((tree));
102 static tree build_conv PROTO((enum tree_code, tree, tree));
103 static void print_n_candidates PROTO((struct candidate *, int));
104 static tree default_parm_conversions PROTO((tree, tree *));
105 static int is_subseq PROTO((tree, tree));
106
107 #define EVIL_RETURN(ARG)        ((ARG).code = EVIL_CODE, (ARG))
108 #define STD_RETURN(ARG)         ((ARG).code = STD_CODE, (ARG))
109 #define QUAL_RETURN(ARG)        ((ARG).code = QUAL_CODE, (ARG))
110 #define TRIVIAL_RETURN(ARG)     ((ARG).code = TRIVIAL_CODE, (ARG))
111 #define ZERO_RETURN(ARG)        ((ARG).code = 0, (ARG))
112
113 /* Ordering function for overload resolution.  Compare two candidates
114    by gross quality.  */
115
116 int
117 rank_for_overload (x, y)
118      struct candidate *x, *y;
119 {
120   if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
121     return y->h.code - x->h.code;
122   if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
123     return -1;
124
125   /* This is set by compute_conversion_costs, for calling a non-const
126      member function from a const member function.  */
127   if ((y->harshness[0].code & CONST_CODE) ^ (x->harshness[0].code & CONST_CODE))
128     return y->harshness[0].code - x->harshness[0].code;
129
130   if (y->h.code & STD_CODE)
131     {
132       if (x->h.code & STD_CODE)
133         return y->h.distance - x->h.distance;
134       return 1;
135     }
136   if (x->h.code & STD_CODE)
137     return -1;
138
139   return y->h.code - x->h.code;
140 }
141
142 /* Compare two candidates, argument by argument.  */
143
144 static int
145 rank_for_ideal (x, y)
146      struct candidate *x, *y;
147 {
148   int i;
149
150   if (x->h_len != y->h_len)
151     abort ();
152
153   for (i = 0; i < x->h_len; i++)
154     {
155       if (y->harshness[i].code - x->harshness[i].code)
156         return y->harshness[i].code - x->harshness[i].code;
157       if ((y->harshness[i].code & STD_CODE)
158           && (y->harshness[i].distance - x->harshness[i].distance))
159         return y->harshness[i].distance - x->harshness[i].distance;
160
161       /* They're both the same code.  Now see if we're dealing with an
162          integral promotion that needs a finer grain of accuracy.  */
163       if (y->harshness[0].code & PROMO_CODE
164           && (y->harshness[i].int_penalty ^ x->harshness[i].int_penalty))
165         return y->harshness[i].int_penalty - x->harshness[i].int_penalty;
166     }
167   return 0;
168 }
169
170 /* TYPE is the type we wish to convert to.  PARM is the parameter
171    we have to work with.  We use a somewhat arbitrary cost function
172    to measure this conversion.  */
173
174 static struct harshness_code
175 convert_harshness (type, parmtype, parm)
176      register tree type, parmtype;
177      tree parm;
178 {
179   struct harshness_code h;
180   register enum tree_code codel;
181   register enum tree_code coder;
182   int lvalue;
183
184   h.code = 0;
185   h.distance = 0;
186   h.int_penalty = 0;
187
188 #ifdef GATHER_STATISTICS
189   n_convert_harshness++;
190 #endif
191
192   if (TREE_CODE (parmtype) == REFERENCE_TYPE)
193     {
194       if (parm)
195         parm = convert_from_reference (parm);
196       parmtype = TREE_TYPE (parmtype);
197       lvalue = 1;
198     }
199   else if (parm)
200     lvalue = lvalue_p (parm);
201   else
202     lvalue = 0;
203
204   if (TYPE_PTRMEMFUNC_P (type))
205     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
206   if (TYPE_PTRMEMFUNC_P (parmtype))
207     parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
208
209   codel = TREE_CODE (type);
210   coder = TREE_CODE (parmtype);
211
212   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
213     return ZERO_RETURN (h);
214
215   if (coder == ERROR_MARK)
216     return EVIL_RETURN (h);
217
218   if (codel == REFERENCE_TYPE)
219     {
220       tree ttl, ttr;
221       int constp = parm ? TREE_READONLY (parm) : TYPE_READONLY (parmtype);
222       int volatilep = (parm ? TREE_THIS_VOLATILE (parm)
223                        : TYPE_VOLATILE (parmtype));
224       register tree intype = TYPE_MAIN_VARIANT (parmtype);
225       register enum tree_code form = TREE_CODE (intype);
226       int penalty = 0;
227
228       ttl = TREE_TYPE (type);
229
230       /* Only allow const reference binding if we were given a parm to deal
231          with, since it isn't really a conversion.  This is a hack to
232          prevent build_type_conversion from finding this conversion, but
233          still allow overloading to find it.  */
234       if (! lvalue && ! (parm && TYPE_READONLY (ttl)))
235         return EVIL_RETURN (h);
236
237       if ((TYPE_READONLY (ttl) < constp)
238           || (TYPE_VOLATILE (ttl) < volatilep))
239         return EVIL_RETURN (h);
240
241       /* When passing a non-const argument into a const reference, dig it a
242          little, so a non-const reference is preferred over this one.  */
243       penalty = ((TYPE_READONLY (ttl) > constp)
244                  + (TYPE_VOLATILE (ttl) > volatilep));
245
246       ttl = TYPE_MAIN_VARIANT (ttl);
247
248       if (form == OFFSET_TYPE)
249         {
250           intype = TREE_TYPE (intype);
251           form = TREE_CODE (intype);
252         }
253
254       ttr = intype;
255
256       if (TREE_CODE (ttl) == ARRAY_TYPE && TREE_CODE (ttr) == ARRAY_TYPE)
257         {
258           if (comptypes (ttl, ttr, 1))
259             return ZERO_RETURN (h);
260           return EVIL_RETURN (h);
261         }
262
263       h = convert_harshness (ttl, ttr, NULL_TREE);
264       if (penalty && h.code == 0)
265         {
266           h.code = QUAL_CODE;
267           h.int_penalty = penalty;
268         }
269       return h;
270     }
271
272   if (codel == POINTER_TYPE && fntype_p (parmtype))
273     {
274       tree p1, p2;
275       struct harshness_code h1, h2;
276
277       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
278       type = TREE_TYPE (type);
279
280       if (coder == POINTER_TYPE)
281         {
282           parmtype = TREE_TYPE (parmtype);
283           coder = TREE_CODE (parmtype);
284         }
285
286       if (coder != TREE_CODE (type))
287         return EVIL_RETURN (h);
288
289       if (type != parmtype && coder == METHOD_TYPE)
290         {
291           tree ttl = TYPE_METHOD_BASETYPE (type);
292           tree ttr = TYPE_METHOD_BASETYPE (parmtype);
293
294           int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
295           if (b_or_d < 0)
296             {
297               b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
298               if (b_or_d < 0)
299                 return EVIL_RETURN (h);
300               h.distance = -b_or_d;
301             }
302           else
303             h.distance = b_or_d;
304           h.code = STD_CODE;
305
306           type = build_function_type
307             (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
308           parmtype = build_function_type
309             (TREE_TYPE (parmtype), TREE_CHAIN (TYPE_ARG_TYPES (parmtype)));
310         }
311
312       /* We allow the default conversion between function type
313          and pointer-to-function type for free.  */
314       if (comptypes (type, parmtype, 1))
315         return h;
316
317       if (pedantic)
318         return EVIL_RETURN (h);
319
320       /* Compare return types.  */
321       p1 = TREE_TYPE (type);
322       p2 = TREE_TYPE (parmtype);
323       h2 = convert_harshness (p1, p2, NULL_TREE);
324       if (h2.code & EVIL_CODE)
325         return h2;
326
327       h1.code = TRIVIAL_CODE;
328       h1.distance = 0;
329
330       if (h2.distance != 0)
331         {
332           tree binfo;
333
334           /* This only works for pointers.  */
335           if (TREE_CODE (p1) != POINTER_TYPE
336               && TREE_CODE (p1) != REFERENCE_TYPE)
337             return EVIL_RETURN (h);
338
339           p1 = TREE_TYPE (p1);
340           p2 = TREE_TYPE (p2);
341           /* Don't die if we happen to be dealing with void*.  */
342           if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
343             return EVIL_RETURN (h);
344           if (h2.distance < 0)
345             binfo = get_binfo (p2, p1, 0);
346           else
347             binfo = get_binfo (p1, p2, 0);
348
349           if (! BINFO_OFFSET_ZEROP (binfo))
350             {
351 #if 0
352               static int explained = 0;
353               if (h2.distance < 0)
354                 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p2, p1);
355               else
356                 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p1, p2);
357
358               if (! explained++)
359                 sorry ("(because pointer values change during conversion)");
360 #endif
361               return EVIL_RETURN (h);
362             }
363         }
364
365       h1.code |= h2.code;
366       if (h2.distance > h1.distance)
367         h1.distance = h2.distance;
368
369       p1 = TYPE_ARG_TYPES (type);
370       p2 = TYPE_ARG_TYPES (parmtype);
371       while (p1 && TREE_VALUE (p1) != void_type_node
372              && p2 && TREE_VALUE (p2) != void_type_node)
373         {
374           h2 = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2),
375                                        NULL_TREE);
376           if (h2.code & EVIL_CODE)
377             return h2;
378
379           if (h2.distance)
380             {
381               /* This only works for pointers and references.  */
382               if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
383                   && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
384                 return EVIL_RETURN (h);
385               h2.distance = - h2.distance;
386             }
387
388           h1.code |= h2.code;
389           if (h2.distance > h1.distance)
390             h1.distance = h2.distance;
391           p1 = TREE_CHAIN (p1);
392           p2 = TREE_CHAIN (p2);
393         }
394       if (p1 == p2)
395         return h1;
396       if (p2)
397         {
398           if (p1)
399             return EVIL_RETURN (h);
400           h1.code |= ELLIPSIS_CODE;
401           return h1;
402         }
403       if (p1)
404         {
405           if (TREE_PURPOSE (p1) == NULL_TREE)
406             h1.code |= EVIL_CODE;
407           return h1;
408         }
409     }
410   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
411     {
412       tree ttl, ttr;
413
414       /* Get to the OFFSET_TYPE that this might be.  */
415       type = TREE_TYPE (type);
416
417       if (coder != TREE_CODE (type))
418         return EVIL_RETURN (h);
419
420       ttl = TYPE_OFFSET_BASETYPE (type);
421       ttr = TYPE_OFFSET_BASETYPE (parmtype);
422
423       if (ttl == ttr)
424         h.code = 0;
425       else
426         {
427           int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
428           if (b_or_d < 0)
429             {
430               b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
431               if (b_or_d < 0)
432                 return EVIL_RETURN (h);
433               h.distance = -b_or_d;
434             }
435           else
436             h.distance = b_or_d;
437           h.code = STD_CODE;
438         }
439
440       /* Now test the OFFSET_TYPE's target compatibility.  */
441       type = TREE_TYPE (type);
442       parmtype = TREE_TYPE (parmtype);
443     }
444
445   if (coder == UNKNOWN_TYPE)
446     {
447       if (codel == FUNCTION_TYPE
448           || codel == METHOD_TYPE
449           || (codel == POINTER_TYPE
450               && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
451                   || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
452         return TRIVIAL_RETURN (h);
453       return EVIL_RETURN (h);
454     }
455
456   if (coder == VOID_TYPE)
457     return EVIL_RETURN (h);
458
459   if (codel == BOOLEAN_TYPE)
460     {
461       if (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE)
462         return STD_RETURN (h);
463       else if (coder == POINTER_TYPE || coder == OFFSET_TYPE)
464         {
465           /* Make this worse than any conversion to another pointer.
466              FIXME this is how I think the language should work, but it may not
467              end up being how the language is standardized (jason 1/30/95).  */
468           h.distance = 32767;
469           return STD_RETURN (h);
470         }
471       return EVIL_RETURN (h);
472     }
473
474   if (INTEGRAL_CODE_P (codel))
475     {
476       /* Control equivalence of ints an enums.  */
477
478       if (codel == ENUMERAL_TYPE
479           && flag_int_enum_equivalence == 0)
480         {
481           /* Enums can be converted to ints, but not vice-versa.  */
482           if (coder != ENUMERAL_TYPE
483               || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
484             return EVIL_RETURN (h);
485         }
486
487       /* else enums and ints (almost) freely interconvert.  */
488
489       if (INTEGRAL_CODE_P (coder))
490         {
491           if (TYPE_MAIN_VARIANT (type)
492               == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
493             {
494               h.code = PROMO_CODE;
495             }
496           else
497             h.code = STD_CODE;
498             
499           return h;
500         }
501       else if (coder == REAL_TYPE)
502         {
503           h.code = STD_CODE;
504           h.distance = 0;
505           return h;
506         }
507     }
508
509   if (codel == REAL_TYPE)
510     {
511       if (coder == REAL_TYPE)
512         {
513           if (TYPE_MAIN_VARIANT (type)
514               == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
515             h.code = PROMO_CODE;
516           else
517             h.code = STD_CODE;
518             
519           return h;
520         }
521       else if (INTEGRAL_CODE_P (coder))
522         {
523           h.code = STD_CODE;
524           h.distance = 0;
525           return h;
526         }
527     }
528
529   /* Convert arrays which have not previously been converted.  */
530   if (coder == ARRAY_TYPE)
531     {
532       coder = POINTER_TYPE;
533       if (parm)
534         {
535           parm = decay_conversion (parm);
536           parmtype = TREE_TYPE (parm);
537         }
538       else
539         parmtype = build_pointer_type (TREE_TYPE (parmtype));
540     }
541
542   /* Conversions among pointers */
543   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
544     {
545       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
546       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
547       int penalty = 4 * (ttl != ttr);
548
549       /* Anything converts to void *.  Since this may be `const void *'
550          (etc.) use VOID_TYPE instead of void_type_node.  Otherwise, the
551          targets must be the same, except that we do allow (at some cost)
552          conversion between signed and unsigned pointer types.  */
553
554       if ((TREE_CODE (ttl) == METHOD_TYPE
555            || TREE_CODE (ttl) == FUNCTION_TYPE)
556           && TREE_CODE (ttl) == TREE_CODE (ttr))
557         {
558           if (comptypes (ttl, ttr, -1))
559             {
560               h.code = penalty ? STD_CODE : 0;
561               h.distance =  0;
562             }
563           else
564             h.code = EVIL_CODE;
565           return h;
566         }
567
568 #if 1
569       if (TREE_CODE (ttl) != VOID_TYPE
570           && (TREE_CODE (ttr) != VOID_TYPE || !parm || !null_ptr_cst_p (parm)))
571         {
572           if (comp_target_types (type, parmtype, 1) <= 0)
573             return EVIL_RETURN (h);
574         }
575 #else
576       if (!(TREE_CODE (ttl) == VOID_TYPE
577             || TREE_CODE (ttr) == VOID_TYPE
578             || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
579                 && (ttl = unsigned_type (ttl),
580                     ttr = unsigned_type (ttr),
581                     penalty = 10, 0))
582             || (comp_target_types (ttl, ttr, 0) > 0)))
583         return EVIL_RETURN (h);
584 #endif
585
586       if (ttr == ttl)
587         {
588           tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
589
590           h.code = 0;
591           /* Note conversion from `T*' to `const T*',
592                                or `T*' to `volatile T*'.  */
593           if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
594               || (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
595             h.code = EVIL_CODE;
596           else if ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
597                    || (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2)))
598             h.code |= QUAL_CODE;
599
600           h.distance = 0;
601           return h;
602         }
603
604
605       if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
606         {
607           int b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
608           if (b_or_d < 0)
609             {
610               b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
611               if (b_or_d < 0)
612                 return EVIL_RETURN (h);
613               h.distance = -b_or_d;
614             }
615           else
616             h.distance = b_or_d;
617           h.code = STD_CODE;
618           return h;
619         }
620
621       /* If converting from a `class*' to a `void*', make it
622          less favorable than any inheritance relationship.  */
623       if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
624         {
625           h.code = STD_CODE;
626           h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
627           return h;
628         }
629
630       h.code = penalty ? STD_CODE : PROMO_CODE;
631       /* Catch things like `const char *' -> `const void *'
632          vs `const char *' -> `void *'.  */
633       if (ttl != ttr)
634         {
635           tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
636           if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
637               || (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
638             h.code = EVIL_CODE;
639           else if ((TYPE_READONLY (tmp1) > TREE_READONLY (tmp2))
640                    || (TYPE_VOLATILE (tmp1) > TYPE_VOLATILE (tmp2)))
641             h.code |= QUAL_CODE;
642         }
643       return h;
644     }
645
646   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
647     {
648       /* This is not a bad match, but don't let it beat
649          integer-enum combinations.  */
650       if (parm && integer_zerop (parm))
651         {
652           h.code = STD_CODE;
653           h.distance = 0;
654           return h;
655         }
656     }
657
658   /* C++: Since the `this' parameter of a signature member function
659      is represented as a signature pointer to handle default implementations
660      correctly, we can have the case that `type' is a signature pointer
661      while `parmtype' is a pointer to a signature table.  We don't really
662      do any conversions in this case, so just return 0.  */
663
664   if (codel == RECORD_TYPE && coder == POINTER_TYPE
665       && IS_SIGNATURE_POINTER (type) && IS_SIGNATURE (TREE_TYPE (parmtype)))
666     return ZERO_RETURN (h);
667
668   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
669     {
670       int b_or_d = get_base_distance (type, parmtype, 0, (tree*)0);
671       if (b_or_d < 0)
672         {
673           b_or_d = get_base_distance (parmtype, type, 0, (tree*)0);
674           if (b_or_d < 0)
675             return EVIL_RETURN (h);
676           h.distance = -b_or_d;
677         }
678       else
679         h.distance = b_or_d;
680       h.code = STD_CODE;
681       return h;
682     }
683   return EVIL_RETURN (h);
684 }
685
686 /* A clone of build_type_conversion for checking user-defined conversions in
687    overload resolution.  */
688
689 static int
690 user_harshness (type, parmtype)
691      register tree type, parmtype;
692 {
693   tree conv;
694   tree winner = NULL_TREE;
695   int code = 0;
696
697   {
698     tree typename = build_typename_overload (type);
699     if (lookup_fnfields (TYPE_BINFO (parmtype), typename, 0))
700       return 0;
701   }
702                         
703   for (conv = lookup_conversions (parmtype); conv; conv = TREE_CHAIN (conv))
704     {
705       struct harshness_code tmp;
706       tree cand = TREE_VALUE (conv);
707
708       if (winner && winner == cand)
709         continue;
710
711       tmp = convert_harshness (type, TREE_TYPE (TREE_TYPE (cand)), NULL_TREE);
712       if ((tmp.code < USER_CODE) && (tmp.distance >= 0))
713         {
714           if (winner)
715             return EVIL_CODE;
716           else
717             {
718               winner = cand;
719               code = tmp.code;
720             }
721         }
722     }
723
724   if (winner)
725     return code;
726
727   return -1;
728 }
729
730 #ifdef DEBUG_MATCHING
731 static char *
732 print_harshness (h)
733      struct harshness_code *h;
734 {
735   static char buf[1024];
736   char tmp[1024];
737
738   bzero (buf, 1024 * sizeof (char));
739   strcat (buf, "codes=[");
740   if (h->code & EVIL_CODE)
741     strcat (buf, "EVIL");
742   if (h->code & CONST_CODE)
743     strcat (buf, " CONST");
744   if (h->code & ELLIPSIS_CODE)
745     strcat (buf, " ELLIPSIS");
746   if (h->code & USER_CODE)
747     strcat (buf, " USER");
748   if (h->code & STD_CODE)
749     strcat (buf, " STD");
750   if (h->code & PROMO_CODE)
751     strcat (buf, " PROMO");
752   if (h->code & QUAL_CODE)
753     strcat (buf, " QUAL");
754   if (h->code & TRIVIAL_CODE)
755     strcat (buf, " TRIVIAL");
756   if (buf[0] == '\0')
757     strcat (buf, "0");
758
759   sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
760
761   strcat (buf, tmp);
762
763   return buf;
764 }
765 #endif
766
767 /* Algorithm: For each argument, calculate how difficult it is to
768    make FUNCTION accept that argument.  If we can easily tell that
769    FUNCTION won't be acceptable to one of the arguments, then we
770    don't need to compute the ease of converting the other arguments,
771    since it will never show up in the intersection of all arguments'
772    favorite functions.
773
774    Conversions between builtin and user-defined types are allowed, but
775    no function involving such a conversion is preferred to one which
776    does not require such a conversion.  Furthermore, such conversions
777    must be unique.  */
778
779 void
780 compute_conversion_costs (function, tta_in, cp, arglen)
781      tree function;
782      tree tta_in;
783      struct candidate *cp;
784      int arglen;
785 {
786   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
787   tree ttf = ttf_in;
788   tree tta = tta_in;
789
790   /* Start out with no strikes against.  */
791   int evil_strikes = 0;
792   int ellipsis_strikes = 0;
793   int user_strikes = 0;
794   int b_or_d_strikes = 0;
795   int easy_strikes = 0;
796
797   int strike_index = 0, win;
798   struct harshness_code lose;
799   extern int cp_silent;
800
801 #ifdef GATHER_STATISTICS
802   n_compute_conversion_costs++;
803 #endif
804
805 #ifndef DEBUG_MATCHING
806   /* We don't emit any warnings or errors while trying out each candidate.  */
807   cp_silent = 1;
808 #endif
809
810   cp->function = function;
811   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
812   cp->u.bad_arg = 0;            /* optimistic!  */
813
814   cp->h.code = 0;
815   cp->h.distance = 0;
816   cp->h.int_penalty = 0;
817   bzero ((char *) cp->harshness,
818          (cp->h_len + 1) * sizeof (struct harshness_code));
819
820   while (ttf && tta)
821     {
822       struct harshness_code h;
823
824       if (ttf == void_list_node)
825         break;
826
827       if (type_unknown_p (TREE_VALUE (tta)))
828         {         
829           /* Must perform some instantiation here.  */
830           tree rhs = TREE_VALUE (tta);
831           tree lhstype = TREE_VALUE (ttf);
832
833           /* Keep quiet about possible contravariance violations.  */
834           int old_inhibit_warnings = inhibit_warnings;
835           inhibit_warnings = 1;
836
837           /* @@ This is to undo what `grokdeclarator' does to
838              parameter types.  It really should go through
839              something more general.  */
840
841           TREE_TYPE (tta) = unknown_type_node;
842           rhs = instantiate_type (lhstype, rhs, 0);
843           inhibit_warnings = old_inhibit_warnings;
844
845           if (TREE_CODE (rhs) == ERROR_MARK)
846             h.code = EVIL_CODE;
847           else
848             h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
849         }
850       else
851         {
852 #ifdef DEBUG_MATCHING
853           static tree old_function = NULL_TREE;
854
855           if (!old_function || function != old_function)
856             {
857               cp_error ("trying %D", function);
858               old_function = function;
859             }
860
861           cp_error ("      doing (%T) %E against arg %T",
862                     TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
863                     TREE_VALUE (ttf));
864 #endif
865
866           h = convert_harshness (TREE_VALUE (ttf),
867                                  TREE_TYPE (TREE_VALUE (tta)),
868                                  TREE_VALUE (tta));
869
870 #ifdef DEBUG_MATCHING
871           cp_error ("     evaluated %s", print_harshness (&h));
872 #endif
873         }
874
875       cp->harshness[strike_index] = h;
876       if ((h.code & EVIL_CODE)
877           || ((h.code & STD_CODE) && h.distance < 0))
878         {
879           cp->u.bad_arg = strike_index;
880           evil_strikes = 1;
881         }
882      else if (h.code & ELLIPSIS_CODE)
883        ellipsis_strikes += 1;
884 #if 0
885       /* This is never set by `convert_harshness'.  */
886       else if (h.code & USER_CODE)
887         {
888           user_strikes += 1;
889         }
890 #endif
891       else
892         {
893           if ((h.code & STD_CODE) && h.distance)
894             {
895               if (h.distance > b_or_d_strikes)
896                 b_or_d_strikes = h.distance;
897             }
898           else
899             easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
900           cp->h.code |= h.code;
901           /* Make sure we communicate this.  */
902           cp->h.int_penalty += h.int_penalty;
903         }
904
905       ttf = TREE_CHAIN (ttf);
906       tta = TREE_CHAIN (tta);
907       strike_index += 1;
908     }
909
910   if (tta)
911     {
912       /* ran out of formals, and parmlist is fixed size.  */
913       if (ttf /* == void_type_node */)
914         {
915           cp->h.code = EVIL_CODE;
916           cp->u.bad_arg = -1;
917           cp_silent = 0;
918           return;
919         }
920       else
921         {
922           struct harshness_code h;
923           int l = list_length (tta);
924           ellipsis_strikes += l;
925           h.code = ELLIPSIS_CODE;
926           h.distance = 0;
927           h.int_penalty = 0;
928           for (; l; --l)
929             cp->harshness[strike_index++] = h;
930         }
931     }
932   else if (ttf && ttf != void_list_node)
933     {
934       /* ran out of actuals, and no defaults.  */
935       if (TREE_PURPOSE (ttf) == NULL_TREE)
936         {
937           cp->h.code = EVIL_CODE;
938           cp->u.bad_arg = -2;
939           cp_silent = 0;
940           return;
941         }
942       /* Store index of first default.  */
943       cp->harshness[arglen].distance = strike_index+1;
944     }
945   else
946     cp->harshness[arglen].distance = 0;
947
948   /* Argument list lengths work out, so don't need to check them again.  */
949   if (evil_strikes)
950     {
951       /* We do not check for derived->base conversions here, since in
952          no case would they give evil strike counts, unless such conversions
953          are somehow ambiguous.  */
954
955       /* See if any user-defined conversions apply.
956          But make sure that we do not loop.  */
957       static int dont_convert_types = 0;
958
959       if (dont_convert_types)
960         {
961           cp->h.code = EVIL_CODE;
962           cp_silent = 0;
963           return;
964         }
965
966       win = 0;                  /* Only get one chance to win.  */
967       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
968       tta = tta_in;
969       strike_index = 0;
970       evil_strikes = 0;
971
972       while (ttf && tta)
973         {
974           if (ttf == void_list_node)
975             break;
976
977           lose = cp->harshness[strike_index];
978           if ((lose.code & EVIL_CODE)
979               || ((lose.code & STD_CODE) && lose.distance < 0))
980             {
981               tree actual_type = TREE_TYPE (TREE_VALUE (tta));
982               tree formal_type = TREE_VALUE (ttf);
983               int extra_conversions = 0;
984
985               dont_convert_types = 1;
986
987               if (TREE_CODE (formal_type) == REFERENCE_TYPE)
988                 formal_type = TREE_TYPE (formal_type);
989               if (TREE_CODE (actual_type) == REFERENCE_TYPE)
990                 actual_type = TREE_TYPE (actual_type);
991
992               if (formal_type != error_mark_node
993                   && actual_type != error_mark_node)
994                 {
995                   formal_type = complete_type (TYPE_MAIN_VARIANT (formal_type));
996                   actual_type = complete_type (TYPE_MAIN_VARIANT (actual_type));
997
998                   if (TYPE_HAS_CONSTRUCTOR (formal_type))
999                     {
1000                       /* If it has a constructor for this type,
1001                          try to use it.  */
1002                       /* @@ There is no way to save this result yet, so
1003                          success is a NULL_TREE for now.  */
1004                       if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
1005                           != error_mark_node)
1006                         win++;
1007                     }
1008                   if (TYPE_LANG_SPECIFIC (actual_type)
1009                       && TYPE_HAS_CONVERSION (actual_type))
1010                     {
1011                       int extra = user_harshness (formal_type, actual_type);
1012
1013                       if (extra == EVIL_CODE)
1014                         win += 2;
1015                       else if (extra >= 0)
1016                         {
1017                           win++;
1018                           extra_conversions = extra;
1019                         }
1020                     }
1021                 }
1022               dont_convert_types = 0;
1023
1024               if (win == 1)
1025                 {
1026                   user_strikes += 1;
1027                   cp->harshness[strike_index].code
1028                     = USER_CODE | (extra_conversions ? STD_CODE : 0);
1029                   win = 0;
1030                 }
1031               else
1032                 {
1033                   if (cp->u.bad_arg > strike_index)
1034                     cp->u.bad_arg = strike_index;
1035
1036                   evil_strikes = win ? 2 : 1;
1037                   break;
1038                 }
1039             }
1040
1041           ttf = TREE_CHAIN (ttf);
1042           tta = TREE_CHAIN (tta);
1043           strike_index += 1;
1044         }
1045     }
1046
1047   /* Const member functions get a small penalty because defaulting
1048      to const is less useful than defaulting to non-const.  */
1049   /* This is bogus, it does not correspond to anything in the ARM.
1050      This code will be fixed when this entire section is rewritten
1051      to conform to the ARM.  (mrs)  */
1052   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1053     {
1054       tree this_parm = TREE_VALUE (ttf_in);
1055
1056       if (TREE_CODE (this_parm) == RECORD_TYPE  /* Is `this' a sig ptr?  */
1057             ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
1058             : TYPE_READONLY (TREE_TYPE (this_parm)))
1059         {
1060           cp->harshness[0].code |= TRIVIAL_CODE;
1061           ++easy_strikes;
1062         }
1063       else
1064         {
1065           /* Calling a non-const member function from a const member function
1066              is probably invalid, but for now we let it only draw a warning.
1067              We indicate that such a mismatch has occurred by setting the
1068              harshness to a maximum value.  */
1069           if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1070               && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1071             cp->harshness[0].code |= CONST_CODE;
1072         }
1073     }
1074
1075   if (evil_strikes)
1076     cp->h.code = EVIL_CODE;
1077   if (ellipsis_strikes)
1078     cp->h.code |= ELLIPSIS_CODE;
1079   if (user_strikes)
1080     cp->h.code |= USER_CODE;
1081   cp_silent = 0;
1082 #ifdef DEBUG_MATCHING
1083   cp_error ("final eval %s", print_harshness (&cp->h));
1084 #endif
1085 }
1086
1087 /* Subroutine of ideal_candidate.  See if X or Y is a better match
1088    than the other.  */
1089
1090 static int
1091 strictly_better (x, y)
1092      unsigned int x, y;
1093 {
1094   unsigned short xor;
1095
1096   if (x == y)
1097     return 0;
1098
1099   xor = x ^ y;
1100   if (xor >= x || xor >= y)
1101     return 1;
1102   return 0;
1103 }
1104
1105 /* When one of several possible overloaded functions and/or methods
1106    can be called, choose the best candidate for overloading.
1107
1108    BASETYPE is the context from which we start method resolution
1109    or NULL if we are comparing overloaded functions.
1110    CANDIDATES is the array of candidates we have to choose from.
1111    N_CANDIDATES is the length of CANDIDATES.
1112    PARMS is a TREE_LIST of parameters to the function we'll ultimately
1113    choose.  It is modified in place when resolving methods.  It is not
1114    modified in place when resolving overloaded functions.
1115    LEN is the length of the parameter list.  */
1116
1117 static struct candidate *
1118 ideal_candidate (candidates, n_candidates, len)
1119      struct candidate *candidates;
1120      int n_candidates;
1121      int len;
1122 {
1123   struct candidate *cp = candidates+n_candidates;
1124   int i, j = -1, best_code;
1125
1126   /* For each argument, sort the functions from best to worst for the arg.
1127      For each function that's not best for this arg, set its overall
1128      harshness to EVIL so that other args won't like it.  The candidate
1129      list for the last argument is the intersection of all the best-liked
1130      functions.  */
1131
1132   qsort (candidates, n_candidates, sizeof (struct candidate),
1133          (int (*) PROTO((const void *, const void *))) rank_for_overload);
1134   best_code = cp[-1].h.code;
1135
1136   /* If they're at least as good as each other, do an arg-by-arg check.  */
1137   if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
1138     {
1139       int better = 0;
1140       int worse = 0;
1141
1142       for (j = 0; j < n_candidates; j++)
1143         if (! strictly_better (candidates[j].h.code, best_code))
1144           break;
1145
1146       qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
1147              (int (*) PROTO((const void *, const void *))) rank_for_ideal);
1148       for (i = 0; i < len; i++)
1149         {
1150           if (cp[-1].harshness[i].code < cp[-2].harshness[i].code)
1151             better = 1;
1152           else if (cp[-1].harshness[i].code > cp[-2].harshness[i].code)
1153             worse = 1;
1154           else if (cp[-1].harshness[i].code & STD_CODE)
1155             {
1156               /* If it involves a standard conversion, let the
1157                  inheritance lattice be the final arbiter.  */
1158               if (cp[-1].harshness[i].distance > cp[-2].harshness[i].distance)
1159                 worse = 1;
1160               else if (cp[-1].harshness[i].distance < cp[-2].harshness[i].distance)
1161                 better = 1;
1162             }
1163           else if (cp[-1].harshness[i].code & PROMO_CODE)
1164             {
1165               /* For integral promotions, take into account a finer
1166                  granularity for determining which types should be favored
1167                  over others in such promotions.  */
1168               if (cp[-1].harshness[i].int_penalty > cp[-2].harshness[i].int_penalty)
1169                 worse = 1;
1170               else if (cp[-1].harshness[i].int_penalty < cp[-2].harshness[i].int_penalty)
1171                 better = 1;
1172             }
1173         }
1174
1175       if (! better || worse)
1176         return NULL;
1177     }
1178   return cp-1;
1179 }
1180
1181 /* Assume that if the class referred to is not in the
1182    current class hierarchy, that it may be remote.
1183    PARENT is assumed to be of aggregate type here.  */
1184
1185 static int
1186 may_be_remote (parent)
1187      tree parent;
1188 {
1189   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
1190     return 0;
1191
1192   if (current_class_type == NULL_TREE)
1193     return 0;
1194
1195   if (parent == current_class_type)
1196     return 0;
1197
1198   if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
1199     return 0;
1200   return 1;
1201 }
1202
1203 tree
1204 build_vfield_ref (datum, type)
1205      tree datum, type;
1206 {
1207   tree rval;
1208   int old_assume_nonnull_objects = flag_assume_nonnull_objects;
1209
1210   if (datum == error_mark_node)
1211     return error_mark_node;
1212
1213   /* Vtable references are always made from non-null objects.  */
1214   flag_assume_nonnull_objects = 1;
1215   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
1216     datum = convert_from_reference (datum);
1217
1218   if (! TYPE_USES_COMPLEX_INHERITANCE (type))
1219     rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
1220                   datum, CLASSTYPE_VFIELD (type));
1221   else
1222     rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
1223   flag_assume_nonnull_objects = old_assume_nonnull_objects;
1224
1225   return rval;
1226 }
1227
1228 /* Build a call to a member of an object.  I.e., one that overloads
1229    operator ()(), or is a pointer-to-function or pointer-to-method.  */
1230
1231 static tree
1232 build_field_call (basetype_path, instance_ptr, name, parms)
1233      tree basetype_path, instance_ptr, name, parms;
1234 {
1235   tree field, instance;
1236
1237   if (name == ctor_identifier || name == dtor_identifier)
1238     return NULL_TREE;
1239
1240   if (instance_ptr == current_class_ptr)
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 (current_class_ref, 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                   || flag_ansi_overloading))
1263             return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
1264
1265           if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1266             {
1267               if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
1268                 return build_function_call (instance, parms);
1269               else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
1270                 return build_function_call (instance, expr_tree_cons (NULL_TREE, current_class_ptr, parms));
1271             }
1272         }
1273       return NULL_TREE;
1274     }
1275
1276   /* Check to see if this is not really a reference to an instance variable
1277      with `operator()()' overloaded.  */
1278   field = lookup_field (basetype_path, name, 1, 0);
1279
1280   /* This can happen if the reference was ambiguous or for access
1281      violations.  */
1282   if (field == error_mark_node)
1283     return error_mark_node;
1284
1285   if (field)
1286     {
1287       tree basetype;
1288       tree ftype = TREE_TYPE (field);
1289
1290       if (TREE_CODE (ftype) == REFERENCE_TYPE)
1291         ftype = TREE_TYPE (ftype);
1292
1293       if (TYPE_LANG_SPECIFIC (ftype)
1294           && (TYPE_OVERLOADS_CALL_EXPR (ftype) || flag_ansi_overloading))
1295         {
1296           /* Make the next search for this field very short.  */
1297           basetype = DECL_FIELD_CONTEXT (field);
1298           instance_ptr = convert_pointer_to (basetype, instance_ptr);
1299
1300           instance = build_indirect_ref (instance_ptr, NULL_PTR);
1301           return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
1302                                  build_component_ref_1 (instance, field, 0),
1303                                  parms, NULL_TREE);
1304         }
1305       if (TREE_CODE (ftype) == POINTER_TYPE)
1306         {
1307           if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
1308               || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
1309             {
1310               /* This is a member which is a pointer to function.  */
1311               tree ref
1312                 = build_component_ref_1 (build_indirect_ref (instance_ptr,
1313                                                              NULL_PTR),
1314                                          field, LOOKUP_COMPLAIN);
1315               if (ref == error_mark_node)
1316                 return error_mark_node;
1317               return build_function_call (ref, parms);
1318             }
1319         }
1320       else if (TREE_CODE (ftype) == METHOD_TYPE)
1321         {
1322           error ("invalid call via pointer-to-member function");
1323           return error_mark_node;
1324         }
1325       else
1326         return NULL_TREE;
1327     }
1328   return NULL_TREE;
1329 }
1330
1331 static tree
1332 find_scoped_type (type, inner_name, inner_types)
1333      tree type, inner_name, inner_types;
1334 {
1335   tree tags = CLASSTYPE_TAGS (type);
1336
1337   while (tags)
1338     {
1339       /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1340          enclosing class) is set to the name for the enum type.  So, if
1341          inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1342          then this test will be true.  */
1343       if (TREE_PURPOSE (tags) == inner_name)
1344         {
1345           if (inner_types == NULL_TREE)
1346             return TYPE_MAIN_DECL (TREE_VALUE (tags));
1347           return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
1348         }
1349       tags = TREE_CHAIN (tags);
1350     }
1351
1352   /* Look for a TYPE_DECL.  */
1353   for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
1354     if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
1355       {
1356         /* Code by raeburn.  */
1357         if (inner_types == NULL_TREE)
1358           return tags;
1359         return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
1360       }
1361
1362   return NULL_TREE;
1363 }
1364
1365 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1366    the name that names the above nested type.  INNER_TYPES
1367    is a chain of nested type names (held together by SCOPE_REFs);
1368    OUTER_TYPE is the type we know to enclose INNER_TYPES.
1369    Returns NULL_TREE if there is an error.  */
1370
1371 tree
1372 resolve_scope_to_name (outer_type, inner_stuff)
1373      tree outer_type, inner_stuff;
1374 {
1375   register tree tmp;
1376   tree inner_name, inner_type;
1377
1378   if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
1379     {
1380       /* We first try to look for a nesting in our current class context,
1381          then try any enclosing classes.  */
1382       tree type = current_class_type;
1383       
1384       while (type && (TREE_CODE (type) == RECORD_TYPE
1385                       || TREE_CODE (type) == UNION_TYPE))
1386         {
1387           tree rval = resolve_scope_to_name (type, inner_stuff);
1388
1389           if (rval != NULL_TREE)
1390             return rval;
1391           type = DECL_CONTEXT (TYPE_MAIN_DECL (type));
1392         }
1393     }
1394
1395   if (TREE_CODE (inner_stuff) == SCOPE_REF)
1396     {
1397       inner_name = TREE_OPERAND (inner_stuff, 0);
1398       inner_type = TREE_OPERAND (inner_stuff, 1);
1399     }
1400   else
1401     {
1402       inner_name = inner_stuff;
1403       inner_type = NULL_TREE;
1404     }
1405
1406   if (outer_type == NULL_TREE)
1407     {
1408       tree x;
1409       /* If we have something that's already a type by itself,
1410          use that.  */
1411       if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
1412         {
1413           if (inner_type)
1414             return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
1415                                           inner_type);
1416           return inner_name;
1417         }
1418       
1419       x = lookup_name (inner_name, 0);
1420
1421       if (x && TREE_CODE (x) == NAMESPACE_DECL)
1422         {
1423           x = lookup_namespace_name (x, inner_type);
1424           return x;
1425         }
1426       return NULL_TREE;
1427     }
1428
1429   if (! IS_AGGR_TYPE (outer_type))
1430     return NULL_TREE;
1431
1432   /* Look for member classes or enums.  */
1433   tmp = find_scoped_type (outer_type, inner_name, inner_type);
1434
1435   /* If it's not a type in this class, then go down into the
1436      base classes and search there.  */
1437   if (! tmp && TYPE_BINFO (outer_type))
1438     {
1439       tree binfos = TYPE_BINFO_BASETYPES (outer_type);
1440       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1441
1442       for (i = 0; i < n_baselinks; i++)
1443         {
1444           tree base_binfo = TREE_VEC_ELT (binfos, i);
1445           tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
1446           if (tmp)
1447             return tmp;
1448         }
1449       tmp = NULL_TREE;
1450     }
1451
1452   return tmp;
1453 }
1454
1455 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1456    This is how virtual function calls are avoided.  */
1457
1458 tree
1459 build_scoped_method_call (exp, basetype, name, parms)
1460      tree exp, basetype, name, parms;
1461 {
1462   /* Because this syntactic form does not allow
1463      a pointer to a base class to be `stolen',
1464      we need not protect the derived->base conversion
1465      that happens here.
1466      
1467      @@ But we do have to check access privileges later.  */
1468   tree binfo, decl;
1469   tree type = TREE_TYPE (exp);
1470
1471   if (type == error_mark_node
1472       || basetype == error_mark_node)
1473     return error_mark_node;
1474
1475   if (processing_template_decl)
1476     {
1477       if (TREE_CODE (name) == BIT_NOT_EXPR)
1478         {
1479           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1480           name = build_min_nt (BIT_NOT_EXPR, type);
1481         }
1482       name = build_min_nt (SCOPE_REF, basetype, name);
1483       return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
1484     }
1485
1486   if (TREE_CODE (type) == REFERENCE_TYPE)
1487     type = TREE_TYPE (type);
1488
1489   if (TREE_CODE (basetype) == TREE_VEC)
1490     {
1491       binfo = basetype;
1492       basetype = BINFO_TYPE (binfo);
1493     }
1494   else
1495     binfo = NULL_TREE;
1496
1497   /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1498      that explicit ~int is caught in the parser; this deals with typedefs
1499      and template parms.  */
1500   if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
1501     {
1502       if (type != basetype)
1503         cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1504                   exp, basetype, type);
1505       name = TREE_OPERAND (name, 0);
1506       if (basetype != name && basetype != get_type_value (name))
1507         cp_error ("qualified type `%T' does not match destructor name `~%T'",
1508                   basetype, name);
1509       return cp_convert (void_type_node, exp);
1510     }
1511
1512   if (! is_aggr_type (basetype, 1))
1513     return error_mark_node;
1514
1515   if (! IS_AGGR_TYPE (type))
1516     {
1517       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1518                 exp, type);
1519       return error_mark_node;
1520     }
1521
1522   if (! binfo)
1523     {
1524       binfo = get_binfo (basetype, type, 1);
1525       if (binfo == error_mark_node)
1526         return error_mark_node;
1527       if (! binfo)
1528         error_not_base_type (basetype, type);
1529     }
1530
1531   if (binfo)
1532     {
1533       if (TREE_CODE (exp) == INDIRECT_REF)
1534         decl = build_indirect_ref
1535           (convert_pointer_to_real
1536            (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
1537       else
1538         decl = build_scoped_ref (exp, basetype);
1539
1540       /* Call to a destructor.  */
1541       if (TREE_CODE (name) == BIT_NOT_EXPR)
1542         {
1543           /* Explicit call to destructor.  */
1544           name = TREE_OPERAND (name, 0);
1545           if (! (name == TYPE_MAIN_VARIANT (TREE_TYPE (decl))
1546                  || name == constructor_name (TREE_TYPE (decl))
1547                  || TREE_TYPE (decl) == get_type_value (name)))
1548             {
1549               cp_error
1550                 ("qualified type `%T' does not match destructor name `~%T'",
1551                  TREE_TYPE (decl), name);
1552               return error_mark_node;
1553             }
1554           if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
1555             return cp_convert (void_type_node, exp);
1556           
1557           return build_delete (TREE_TYPE (decl), decl, integer_two_node,
1558                                LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
1559                                0);
1560         }
1561
1562       /* Call to a method.  */
1563       return build_method_call (decl, name, parms, binfo,
1564                                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1565     }
1566   return error_mark_node;
1567 }
1568
1569 static void
1570 print_candidates (candidates)
1571      tree candidates;
1572 {
1573   cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
1574   candidates = TREE_CHAIN (candidates);
1575
1576   while (candidates)
1577     {
1578       cp_error_at ("                %D", TREE_VALUE (candidates));
1579       candidates = TREE_CHAIN (candidates);
1580     }
1581 }
1582
1583 static void
1584 print_n_candidates (candidates, n)
1585      struct candidate *candidates;
1586      int n;
1587 {
1588   int i;
1589
1590   cp_error_at ("candidates are: %D", candidates[0].function);
1591   for (i = 1; i < n; i++)
1592     cp_error_at ("                %D", candidates[i].function);
1593 }
1594
1595 /* We want the address of a function or method.  We avoid creating a
1596    pointer-to-member function.  */
1597
1598 tree
1599 build_addr_func (function)
1600      tree function;
1601 {
1602   tree type = TREE_TYPE (function);
1603
1604   /* We have to do these by hand to avoid real pointer to member
1605      functions.  */
1606   if (TREE_CODE (type) == METHOD_TYPE)
1607     {
1608       tree addr;
1609
1610       type = build_pointer_type (type);
1611
1612       if (mark_addressable (function) == 0)
1613         return error_mark_node;
1614
1615       addr = build1 (ADDR_EXPR, type, function);
1616
1617       /* Address of a static or external variable or function counts
1618          as a constant */
1619       if (staticp (function))
1620         TREE_CONSTANT (addr) = 1;
1621
1622       function = addr;
1623     }
1624   else
1625     function = default_conversion (function);
1626
1627   return function;
1628 }
1629
1630 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1631    POINTER_TYPE to those.  Note, pointer to member function types
1632    (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
1633
1634 tree
1635 build_call (function, result_type, parms)
1636      tree function, result_type, parms;
1637 {
1638   int is_constructor = 0;
1639
1640   function = build_addr_func (function);
1641
1642   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
1643     {
1644       sorry ("unable to call pointer to member function here");
1645       return error_mark_node;
1646     }
1647
1648   if (TREE_CODE (function) == ADDR_EXPR
1649       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1650       && DECL_CONSTRUCTOR_P (TREE_OPERAND (function, 0)))
1651     is_constructor = 1;
1652
1653   function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
1654   TREE_HAS_CONSTRUCTOR (function) = is_constructor;
1655   TREE_TYPE (function) = result_type;
1656   TREE_SIDE_EFFECTS (function) = 1;
1657   
1658   return function;
1659 }
1660
1661 static tree
1662 default_parm_conversions (parms, last)
1663      tree parms, *last;
1664 {
1665   tree parm, parmtypes = NULL_TREE;
1666
1667   *last = NULL_TREE;
1668
1669   for (parm = parms; parm; parm = TREE_CHAIN (parm))
1670     {
1671       tree t = TREE_TYPE (TREE_VALUE (parm));
1672
1673       if (TREE_CODE (t) == OFFSET_TYPE
1674           || TREE_CODE (t) == METHOD_TYPE
1675           || TREE_CODE (t) == FUNCTION_TYPE)
1676         {
1677           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
1678           t = TREE_TYPE (TREE_VALUE (parm));
1679         }
1680
1681       if (t == error_mark_node)
1682           return error_mark_node;
1683
1684       *last = build_tree_list (NULL_TREE, t);
1685       parmtypes = chainon (parmtypes, *last);
1686     }
1687
1688   return parmtypes;
1689 }
1690
1691
1692 /* Build something of the form ptr->method (args)
1693    or object.method (args).  This can also build
1694    calls to constructors, and find friends.
1695
1696    Member functions always take their class variable
1697    as a pointer.
1698
1699    INSTANCE is a class instance.
1700
1701    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1702
1703    PARMS help to figure out what that NAME really refers to.
1704
1705    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1706    down to the real instance type to use for access checking.  We need this
1707    information to get protected accesses correct.  This parameter is used
1708    by build_member_call.
1709
1710    FLAGS is the logical disjunction of zero or more LOOKUP_
1711    flags.  See cp-tree.h for more info.
1712
1713    If this is all OK, calls build_function_call with the resolved
1714    member function.
1715
1716    This function must also handle being called to perform
1717    initialization, promotion/coercion of arguments, and
1718    instantiation of default parameters.
1719
1720    Note that NAME may refer to an instance variable name.  If
1721    `operator()()' is defined for the type of that field, then we return
1722    that result.  */
1723
1724 tree
1725 build_method_call (instance, name, parms, basetype_path, flags)
1726      tree instance, name, parms, basetype_path;
1727      int flags;
1728 {
1729   register tree function, fntype, value_type;
1730   register tree basetype, save_basetype;
1731   register tree baselink, result, parmtypes;
1732   tree last;
1733   int pass;
1734   tree access = access_public_node;
1735   tree orig_basetype = basetype_path ? BINFO_TYPE (basetype_path) : NULL_TREE;
1736
1737   /* Range of cases for vtable optimization.  */
1738   enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
1739   enum vtable_needs need_vtbl = not_needed;
1740
1741   char *name_kind;
1742   tree save_name = name;
1743   int ever_seen = 0;
1744   tree instance_ptr = NULL_TREE;
1745   int all_virtual = flag_all_virtual;
1746   int static_call_context = 0;
1747   tree found_fns = NULL_TREE;
1748
1749   /* Keep track of `const' and `volatile' objects.  */
1750   int constp, volatilep;
1751
1752 #ifdef GATHER_STATISTICS
1753   n_build_method_call++;
1754 #endif
1755
1756   if (instance == error_mark_node
1757       || name == error_mark_node
1758       || parms == error_mark_node
1759       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
1760     return error_mark_node;
1761
1762   if (processing_template_decl)
1763     {
1764       if (TREE_CODE (name) == BIT_NOT_EXPR)
1765         {
1766           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1767           name = build_min_nt (BIT_NOT_EXPR, type);
1768         }
1769
1770       return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
1771     }
1772
1773   /* This is the logic that magically deletes the second argument to
1774      operator delete, if it is not needed.  */
1775   if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
1776     {
1777       tree save_last = TREE_CHAIN (parms);
1778       tree result;
1779       /* get rid of unneeded argument */
1780       TREE_CHAIN (parms) = NULL_TREE;
1781       result = build_method_call (instance, name, parms, basetype_path,
1782                                   (LOOKUP_SPECULATIVELY|flags)
1783                                   &~LOOKUP_COMPLAIN);
1784       /* If it finds a match, return it.  */
1785       if (result)
1786         return build_method_call (instance, name, parms, basetype_path, flags);
1787       /* If it doesn't work, two argument delete must work */
1788       TREE_CHAIN (parms) = save_last;
1789     }
1790   /* We already know whether it's needed or not for vec delete.  */
1791   else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
1792            && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1793            && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
1794     TREE_CHAIN (parms) = NULL_TREE;
1795
1796   if (TREE_CODE (name) == BIT_NOT_EXPR)
1797     {
1798       flags |= LOOKUP_DESTRUCTOR;
1799       name = TREE_OPERAND (name, 0);
1800       if (parms)
1801         error ("destructors take no parameters");
1802       basetype = TREE_TYPE (instance);
1803       if (TREE_CODE (basetype) == REFERENCE_TYPE)
1804         basetype = TREE_TYPE (basetype);
1805       if (! (name == basetype
1806              || (IS_AGGR_TYPE (basetype)
1807                  && name == constructor_name (basetype))
1808              || basetype == get_type_value (name)))
1809         {
1810           cp_error ("destructor name `~%D' does not match type `%T' of expression",
1811                     name, basetype);
1812           return cp_convert (void_type_node, instance);
1813         }
1814
1815       if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
1816         return cp_convert (void_type_node, instance);
1817       instance = default_conversion (instance);
1818       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1819       return build_delete (build_pointer_type (basetype),
1820                            instance_ptr, integer_two_node,
1821                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
1822     }
1823
1824   if (flag_ansi_overloading)
1825     return build_new_method_call (instance, name, parms, basetype_path, flags);
1826
1827   {
1828     char *xref_name;
1829     
1830     /* Initialize name for error reporting.  */
1831     if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
1832       {
1833         char *p = operator_name_string (name);
1834         xref_name = (char *)alloca (strlen (p) + 10);
1835         sprintf (xref_name, "operator %s", p);
1836       }
1837     else if (TREE_CODE (name) == SCOPE_REF)
1838       xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
1839     else
1840       xref_name = IDENTIFIER_POINTER (name);
1841
1842     GNU_xref_call (current_function_decl, xref_name);
1843   }
1844
1845   if (instance == NULL_TREE)
1846     {
1847       basetype = NULL_TREE;
1848       /* Check cases where this is really a call to raise
1849          an exception.  */
1850       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
1851         {
1852           basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
1853           if (basetype)
1854             basetype = TREE_VALUE (basetype);
1855         }
1856       else if (TREE_CODE (name) == SCOPE_REF
1857                && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
1858         {
1859           if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
1860             return error_mark_node;
1861           basetype = purpose_member (TREE_OPERAND (name, 1),
1862                                      CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
1863           if (basetype)
1864             basetype = TREE_VALUE (basetype);
1865         }
1866
1867       if (basetype != NULL_TREE)
1868         ;
1869       /* call to a constructor...  */
1870       else if (basetype_path)
1871         {
1872           basetype = BINFO_TYPE (basetype_path);
1873           if (name == TYPE_IDENTIFIER (basetype))
1874             name = ctor_identifier;
1875         }
1876       else if (IDENTIFIER_HAS_TYPE_VALUE (name))
1877         {
1878           basetype = IDENTIFIER_TYPE_VALUE (name);
1879           name = ctor_identifier;
1880         }
1881       else
1882         {
1883           tree typedef_name = lookup_name (name, 1);
1884           if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
1885             {
1886               /* Canonicalize the typedef name.  */
1887               basetype = TREE_TYPE (typedef_name);
1888               name = ctor_identifier;
1889             }
1890           else
1891             {
1892               cp_error ("no constructor named `%T' in scope",
1893                         name);
1894               return error_mark_node;
1895             }
1896         }
1897
1898       if (! IS_AGGR_TYPE (basetype))
1899         {
1900         non_aggr_error:
1901           if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
1902             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1903                       name, instance, basetype);
1904
1905           return error_mark_node;
1906         }
1907     }
1908   else if (instance == current_class_ref || instance == current_class_ptr)
1909     {
1910       /* When doing initialization, we side-effect the TREE_TYPE of
1911          current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
1912       basetype = TREE_TYPE (current_class_ref);
1913
1914       /* Anything manifestly `this' in constructors and destructors
1915          has a known type, so virtual function tables are not needed.  */
1916       if (TYPE_VIRTUAL_P (basetype)
1917           && !(flags & LOOKUP_NONVIRTUAL))
1918         need_vtbl = (dtor_label || ctor_label)
1919           ? unneeded : maybe_needed;
1920
1921       /* If `this' is a signature pointer and `name' is not a constructor,
1922          we are calling a signature member function.  In that case, set the
1923          `basetype' to the signature type and dereference the `optr' field.  */
1924       if (IS_SIGNATURE_POINTER (basetype)
1925           && TYPE_IDENTIFIER (basetype) != name)
1926         {
1927           basetype = SIGNATURE_TYPE (basetype);
1928           instance_ptr = instance;
1929           basetype_path = TYPE_BINFO (basetype);
1930         }
1931       else
1932         {
1933           instance = current_class_ref;
1934           instance_ptr = current_class_ptr;
1935           basetype_path = TYPE_BINFO (current_class_type);
1936         }
1937       result = build_field_call (basetype_path, instance_ptr, name, parms);
1938
1939       if (result)
1940         return result;
1941     }
1942   else if (TREE_CODE (instance) == RESULT_DECL)
1943     {
1944       basetype = TREE_TYPE (instance);
1945       /* Should we ever have to make a virtual function reference
1946          from a RESULT_DECL, know that it must be of fixed type
1947          within the scope of this function.  */
1948       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1949         need_vtbl = maybe_needed;
1950       instance_ptr = build1 (ADDR_EXPR, build_pointer_type (basetype), instance);
1951     }
1952   else
1953     {
1954       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
1955       tree inst_ptr_basetype;
1956
1957       static_call_context
1958         = (TREE_CODE (instance) == INDIRECT_REF
1959            && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
1960            && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
1961
1962       if (TREE_CODE (instance) == OFFSET_REF)
1963         instance = resolve_offset_ref (instance);
1964
1965       /* the base type of an instance variable is pointer to class */
1966       basetype = TREE_TYPE (instance);
1967
1968       if (TREE_CODE (basetype) == REFERENCE_TYPE)
1969         {
1970           basetype = TREE_TYPE (basetype);
1971           if (! IS_AGGR_TYPE (basetype))
1972             goto non_aggr_error;
1973           /* Call to convert not needed because we are remaining
1974              within the same type.  */
1975           instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
1976                                  instance);
1977           inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
1978         }
1979       else
1980         {
1981           if (! IS_AGGR_TYPE (basetype)
1982               && ! (TYPE_LANG_SPECIFIC (basetype)
1983                     && (IS_SIGNATURE_POINTER (basetype)
1984                         || IS_SIGNATURE_REFERENCE (basetype))))
1985             goto non_aggr_error;
1986
1987           /* If `instance' is a signature pointer/reference and `name' is
1988              not a constructor, we are calling a signature member function.
1989              In that case set the `basetype' to the signature type.  */
1990           if ((IS_SIGNATURE_POINTER (basetype)
1991                || IS_SIGNATURE_REFERENCE (basetype))
1992               && TYPE_IDENTIFIER (basetype) != name)
1993             basetype = SIGNATURE_TYPE (basetype);
1994
1995           basetype = complete_type (basetype);
1996
1997           if ((IS_SIGNATURE (basetype)
1998                && (instance_ptr = instance))
1999               || (lvalue_p (instance)
2000                   && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
2001               || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
2002             {
2003               if (instance_ptr == error_mark_node)
2004                 return error_mark_node;
2005             }
2006           else if (TREE_CODE (instance) == NOP_EXPR
2007                    || TREE_CODE (instance) == CONSTRUCTOR)
2008             {
2009               /* A cast is not an lvalue.  Initialize a fresh temp
2010                  with the value we are casting from, and proceed with
2011                  that temporary.  We can't cast to a reference type,
2012                  so that simplifies the initialization to something
2013                  we can manage.  */
2014               tree temp = get_temp_name (TREE_TYPE (instance), 0);
2015               if (IS_AGGR_TYPE (TREE_TYPE (instance)))
2016                 expand_aggr_init (temp, instance, 0, flags);
2017               else
2018                 {
2019                   store_init_value (temp, instance);
2020                   expand_decl_init (temp);
2021                 }
2022               instance = temp;
2023               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
2024             }
2025           else
2026             {
2027               if (TREE_CODE (instance) != CALL_EXPR)
2028                 my_friendly_abort (125);
2029               if (TYPE_NEEDS_CONSTRUCTING (basetype))
2030                 instance = build_cplus_new (basetype, instance);
2031               else
2032                 {
2033                   instance = get_temp_name (basetype, 0);
2034                   TREE_ADDRESSABLE (instance) = 1;
2035                 }
2036               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
2037             }
2038           /* @@ Should we call comp_target_types here?  */
2039           if (IS_SIGNATURE (basetype))
2040             inst_ptr_basetype = basetype;
2041           else
2042             inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
2043           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
2044             basetype = inst_ptr_basetype;
2045           else
2046             {
2047               instance_ptr = cp_convert (build_pointer_type (basetype), instance_ptr);
2048               if (instance_ptr == error_mark_node)
2049                 return error_mark_node;
2050             }
2051         }
2052
2053       /* After converting `instance_ptr' above, `inst_ptr_basetype' was
2054          not updated, so we use `basetype' instead.  */
2055       if (basetype_path == NULL_TREE
2056           && IS_SIGNATURE (basetype))
2057         basetype_path = TYPE_BINFO (basetype);
2058       else if (basetype_path == NULL_TREE
2059                || (BINFO_TYPE (basetype_path)
2060                    != TYPE_MAIN_VARIANT (inst_ptr_basetype)))
2061         basetype_path = TYPE_BINFO (inst_ptr_basetype);
2062
2063       result = build_field_call (basetype_path, instance_ptr, name, parms);
2064       if (result)
2065         return result;
2066
2067       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
2068         {
2069           if (TREE_SIDE_EFFECTS (instance_ptr))
2070             {
2071               /* This action is needed because the instance is needed
2072                  for providing the base of the virtual function table.
2073                  Without using a SAVE_EXPR, the function we are building
2074                  may be called twice, or side effects on the instance
2075                  variable (such as a post-increment), may happen twice.  */
2076               instance_ptr = save_expr (instance_ptr);
2077               instance = build_indirect_ref (instance_ptr, NULL_PTR);
2078             }
2079           else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
2080             {
2081               /* This happens when called for operator new ().  */
2082               instance = build_indirect_ref (instance, NULL_PTR);
2083             }
2084
2085           need_vtbl = maybe_needed;
2086         }
2087     }
2088
2089   if (save_name == ctor_identifier)
2090     save_name = TYPE_IDENTIFIER (basetype);
2091
2092   if (TYPE_SIZE (complete_type (basetype)) == 0)
2093     {
2094       /* This is worth complaining about, I think.  */
2095       cp_error ("cannot lookup method in incomplete type `%T'", basetype);
2096       return error_mark_node;
2097     }
2098
2099   save_basetype = TYPE_MAIN_VARIANT (basetype);
2100
2101   parmtypes = default_parm_conversions (parms, &last);
2102   if (parmtypes == error_mark_node)
2103     {
2104       return error_mark_node;
2105     }
2106
2107   if (instance && IS_SIGNATURE (basetype))
2108     {
2109       /* @@ Should this be the constp/volatilep flags for the optr field
2110          of the signature pointer?  */
2111       constp = TYPE_READONLY (basetype);
2112       volatilep = TYPE_VOLATILE (basetype);
2113       parms = expr_tree_cons (NULL_TREE, instance_ptr, parms);
2114     }
2115   else if (instance)
2116     {
2117       /* TREE_READONLY (instance) fails for references.  */
2118       constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
2119       volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
2120       parms = expr_tree_cons (NULL_TREE, instance_ptr, parms);
2121     }
2122   else
2123     {
2124       /* Raw constructors are always in charge.  */
2125       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2126           && ! (flags & LOOKUP_HAS_IN_CHARGE))
2127         {
2128           flags |= LOOKUP_HAS_IN_CHARGE;
2129           parms = expr_tree_cons (NULL_TREE, integer_one_node, parms);
2130           parmtypes = scratch_tree_cons (NULL_TREE, integer_type_node, parmtypes);
2131         }
2132
2133       constp = 0;
2134       volatilep = 0;
2135       instance_ptr = build_int_2 (0, 0);
2136       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
2137       parms = expr_tree_cons (NULL_TREE, instance_ptr, parms);
2138     }
2139
2140   parmtypes = scratch_tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
2141
2142   if (last == NULL_TREE)
2143     last = parmtypes;
2144
2145   /* Look up function name in the structure type definition.  */
2146
2147   /* FIXME Axe most of this now?  */
2148   if ((IDENTIFIER_HAS_TYPE_VALUE (name)
2149        && ! IDENTIFIER_OPNAME_P (name)
2150        && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name)))
2151       || name == constructor_name (basetype)
2152       || name == ctor_identifier)
2153     {
2154       tree tmp = NULL_TREE;
2155       if (IDENTIFIER_TYPE_VALUE (name) == basetype
2156           || name == constructor_name (basetype)
2157           || name == ctor_identifier)
2158         tmp = TYPE_BINFO (basetype);
2159       else
2160         tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
2161       
2162       if (tmp != NULL_TREE)
2163         {
2164           name_kind = "constructor";
2165           
2166           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2167               && ! (flags & LOOKUP_HAS_IN_CHARGE))
2168             {
2169               /* Constructors called for initialization
2170                  only are never in charge.  */
2171               tree tmplist;
2172               
2173               flags |= LOOKUP_HAS_IN_CHARGE;
2174               tmplist = expr_tree_cons (NULL_TREE, integer_zero_node,
2175                                    TREE_CHAIN (parms));
2176               TREE_CHAIN (parms) = tmplist;
2177               tmplist = scratch_tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
2178               TREE_CHAIN (parmtypes) = tmplist;
2179             }
2180           basetype = BINFO_TYPE (tmp);
2181         }
2182       else
2183         name_kind = "method";
2184     }
2185   else
2186     name_kind = "method";
2187   
2188   if (basetype_path == NULL_TREE
2189       || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
2190     basetype_path = TYPE_BINFO (basetype);
2191   result = lookup_fnfields (basetype_path, name,
2192                             (flags & LOOKUP_COMPLAIN));
2193   if (result == error_mark_node)
2194     return error_mark_node;
2195
2196   for (pass = 0; pass < 2; pass++)
2197     {
2198       struct candidate *candidates = 0;
2199       struct candidate *cp = 0;
2200       int len = 0;
2201       unsigned best = 1;
2202
2203       baselink = result;
2204
2205       if (pass > 0)
2206         {
2207           candidates
2208             = (struct candidate *) alloca ((ever_seen+1)
2209                                            * sizeof (struct candidate));
2210           bzero ((char *) candidates, (ever_seen + 1) * sizeof (struct candidate));
2211           cp = candidates;
2212           len = list_length (parms);
2213           ever_seen = 0;
2214
2215           /* First see if a global function has a shot at it.  */
2216           if (flags & LOOKUP_GLOBAL)
2217             {
2218               tree friend_parms;
2219               tree parm = instance_ptr;
2220
2221               if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
2222                 parm = convert_from_reference (parm);
2223               else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
2224                 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
2225               else
2226                 my_friendly_abort (167);
2227
2228               friend_parms = expr_tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
2229
2230               cp->h_len = len;
2231               cp->harshness = (struct harshness_code *)
2232                 alloca ((len + 1) * sizeof (struct harshness_code));
2233
2234               result = build_overload_call_real (name, friend_parms, 0, cp, 1);
2235
2236               /* If it turns out to be the one we were actually looking for
2237                  (it was probably a friend function), the return the
2238                  good result.  */
2239               if (TREE_CODE (result) == CALL_EXPR)
2240                 return result;
2241
2242               while ((cp->h.code & EVIL_CODE) == 0)
2243                 {
2244                   /* non-standard uses: set the field to 0 to indicate
2245                      we are using a non-member function.  */
2246                   cp->u.field = 0;
2247                   if (cp->harshness[len].distance == 0
2248                       && cp->h.code < best)
2249                     best = cp->h.code;
2250                   cp += 1;
2251                 }
2252             }
2253         }
2254
2255       if (baselink)
2256         {
2257           /* We have a hit (of sorts). If the parameter list is
2258              "error_mark_node", or some variant thereof, it won't
2259              match any methods.  Since we have verified that the is
2260              some method vaguely matching this one (in name at least),
2261              silently return.
2262              
2263              Don't stop for friends, however.  */
2264           basetype_path = TREE_PURPOSE (baselink);
2265
2266           function = TREE_VALUE (baselink);
2267           if (TREE_CODE (basetype_path) == TREE_LIST)
2268             basetype_path = TREE_VALUE (basetype_path);
2269           basetype = BINFO_TYPE (basetype_path);
2270
2271           for (; function; function = DECL_CHAIN (function))
2272             {
2273 #ifdef GATHER_STATISTICS
2274               n_inner_fields_searched++;
2275 #endif
2276               ever_seen++;
2277               if (pass > 0)
2278                 found_fns = scratch_tree_cons (NULL_TREE, function, found_fns);
2279
2280               /* Not looking for friends here.  */
2281               if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
2282                   && ! DECL_STATIC_FUNCTION_P (function))
2283                 continue;
2284
2285               if (pass > 0)
2286                 {
2287                   tree these_parms = parms;
2288
2289 #ifdef GATHER_STATISTICS
2290                   n_inner_fields_searched++;
2291 #endif
2292                   cp->h_len = len;
2293                   cp->harshness = (struct harshness_code *)
2294                     alloca ((len + 1) * sizeof (struct harshness_code));
2295
2296                   if (DECL_STATIC_FUNCTION_P (function))
2297                     these_parms = TREE_CHAIN (these_parms);
2298                   compute_conversion_costs (function, these_parms, cp, len);
2299
2300                   if ((cp->h.code & EVIL_CODE) == 0)
2301                     {
2302                       cp->u.field = function;
2303                       cp->function = function;
2304                       cp->basetypes = basetype_path;
2305
2306                       /* Don't allow non-converting constructors to convert.  */
2307                       if (flags & LOOKUP_ONLYCONVERTING
2308                           && DECL_LANG_SPECIFIC (function)
2309                           && DECL_NONCONVERTING_P (function))
2310                         continue;
2311
2312                       /* No "two-level" conversions.  */
2313                       if (flags & LOOKUP_NO_CONVERSION
2314                           && (cp->h.code & USER_CODE))
2315                         continue;
2316
2317                       cp++;
2318                     }
2319                 }
2320             }
2321         }
2322
2323       if (pass == 0)
2324         {
2325           tree igv = lookup_name_nonclass (name);
2326
2327           /* No exact match could be found.  Now try to find match
2328              using default conversions.  */
2329           if ((flags & LOOKUP_GLOBAL) && igv)
2330             {
2331               if (TREE_CODE (igv) == FUNCTION_DECL)
2332                 ever_seen += 1;
2333               else if (TREE_CODE (igv) == TREE_LIST)
2334                 ever_seen += count_functions (igv);
2335             }
2336
2337           if (ever_seen == 0)
2338             {
2339               if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2340                   == LOOKUP_SPECULATIVELY)
2341                 return NULL_TREE;
2342               
2343               TREE_CHAIN (last) = void_list_node;
2344               if (flags & LOOKUP_GLOBAL)
2345                 cp_error ("no global or member function `%D(%A)' defined",
2346                           save_name, parmtypes);
2347               else
2348                 cp_error ("no member function `%T::%D(%A)' defined",
2349                           save_basetype, save_name, TREE_CHAIN (parmtypes));
2350               return error_mark_node;
2351             }
2352           continue;
2353         }
2354
2355       if (cp - candidates != 0)
2356         {
2357           /* Rank from worst to best.  Then cp will point to best one.
2358              Private fields have their bits flipped.  For unsigned
2359              numbers, this should make them look very large.
2360              If the best alternate has a (signed) negative value,
2361              then all we ever saw were private members.  */
2362           if (cp - candidates > 1)
2363             {
2364               int n_candidates = cp - candidates;
2365               extern int warn_synth;
2366               TREE_VALUE (parms) = instance_ptr;
2367               cp = ideal_candidate (candidates, n_candidates, len);
2368               if (cp == (struct candidate *)0)
2369                 {
2370                   if (flags & LOOKUP_COMPLAIN)
2371                     {
2372                       TREE_CHAIN (last) = void_list_node;
2373                       cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2374                                 name_kind, save_name, TREE_CHAIN (parmtypes));
2375                       print_n_candidates (candidates, n_candidates);
2376                     }
2377                   return error_mark_node;
2378                 }
2379               if (cp->h.code & EVIL_CODE)
2380                 return error_mark_node;
2381               if (warn_synth
2382                   && DECL_NAME (cp->function) == ansi_opname[MODIFY_EXPR]
2383                   && DECL_ARTIFICIAL (cp->function)
2384                   && n_candidates == 2)
2385                 {
2386                   cp_warning ("using synthesized `%#D' for copy assignment",
2387                               cp->function);
2388                   cp_warning_at ("  where cfront would use `%#D'",
2389                                  candidates->function);
2390                 }
2391             }
2392           else if (cp[-1].h.code & EVIL_CODE)
2393             {
2394               if (flags & LOOKUP_COMPLAIN)
2395                 cp_error ("ambiguous type conversion requested for %s `%D'",
2396                           name_kind, save_name);
2397               return error_mark_node;
2398             }
2399           else
2400             cp--;
2401
2402           /* The global function was the best, so use it.  */
2403           if (cp->u.field == 0)
2404             {
2405               /* We must convert the instance pointer into a reference type.
2406                  Global overloaded functions can only either take
2407                  aggregate objects (which come for free from references)
2408                  or reference data types anyway.  */
2409               TREE_VALUE (parms) = copy_node (instance_ptr);
2410               TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
2411               return build_function_call (cp->function, parms);
2412             }
2413
2414           function = cp->function;
2415           basetype_path = cp->basetypes;
2416           if (! DECL_STATIC_FUNCTION_P (function))
2417             TREE_VALUE (parms) = cp->arg;
2418           goto found_and_maybe_warn;
2419         }
2420
2421       if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
2422         {
2423           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2424               == LOOKUP_SPECULATIVELY)
2425             return NULL_TREE;
2426
2427           if (DECL_STATIC_FUNCTION_P (cp->function))
2428             parms = TREE_CHAIN (parms);
2429           if (ever_seen)
2430             {
2431               if (flags & LOOKUP_SPECULATIVELY)
2432                 return NULL_TREE;
2433               if (static_call_context
2434                   && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
2435                 cp_error ("object missing in call to `%D'", cp->function);
2436               else if (ever_seen > 1)
2437                 {
2438                   TREE_CHAIN (last) = void_list_node;
2439                   cp_error ("no matching function for call to `%T::%D (%A)%V'",
2440                             TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr))),
2441                             save_name, TREE_CHAIN (parmtypes),
2442                             TREE_TYPE (TREE_TYPE (instance_ptr)));
2443                   TREE_CHAIN (last) = NULL_TREE;
2444                   print_candidates (found_fns);
2445                 }
2446               else
2447                 report_type_mismatch (cp, parms, name_kind);
2448               return error_mark_node;
2449             }
2450
2451           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2452               == LOOKUP_COMPLAIN)
2453             {
2454               cp_error ("%T has no method named %D", save_basetype, save_name);
2455               return error_mark_node;
2456             }
2457           return NULL_TREE;
2458         }
2459       continue;
2460
2461     found_and_maybe_warn:
2462       if ((cp->harshness[0].code & CONST_CODE)
2463           /* 12.1p2: Constructors can be called for const objects.  */
2464           && ! DECL_CONSTRUCTOR_P (cp->function))
2465         {
2466           if (flags & LOOKUP_COMPLAIN)
2467             {
2468               cp_error_at ("non-const member function `%D'", cp->function);
2469               error ("called for const object at this point in file");
2470             }
2471           /* Not good enough for a match.  */
2472           else
2473             return error_mark_node;
2474         }
2475       goto found;
2476     }
2477   /* Silently return error_mark_node.  */
2478   return error_mark_node;
2479
2480  found:
2481   if (flags & LOOKUP_PROTECT)
2482     access = compute_access (basetype_path, function);
2483
2484   if (access == access_private_node)
2485     {
2486       if (flags & LOOKUP_COMPLAIN)
2487         {
2488           cp_error_at ("%s `%+#D' is %s", name_kind, function, 
2489                        TREE_PRIVATE (function) ? "private"
2490                        : "from private base class");
2491           error ("within this context");
2492         }
2493       return error_mark_node;
2494     }
2495   else if (access == access_protected_node)
2496     {
2497       if (flags & LOOKUP_COMPLAIN)
2498         {
2499           cp_error_at ("%s `%+#D' %s", name_kind, function,
2500                        TREE_PROTECTED (function) ? "is protected"
2501                        : "has protected accessibility");
2502           error ("within this context");
2503         }
2504       return error_mark_node;
2505     }
2506
2507   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2508      type (if it exists) is a pointer to.  */
2509
2510   if (DECL_ABSTRACT_VIRTUAL_P (function)
2511       && instance == current_class_ref
2512       && DECL_CONSTRUCTOR_P (current_function_decl)
2513       && ! (flags & LOOKUP_NONVIRTUAL)
2514       && value_member (function, get_abstract_virtuals (basetype)))
2515     cp_error ("abstract virtual `%#D' called from constructor", function);
2516
2517   if (IS_SIGNATURE (basetype))
2518     {
2519       if (static_call_context)
2520         {
2521           cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2522                     basetype, save_name);
2523           return error_mark_node;
2524         }
2525       return build_signature_method_call (function, parms);
2526     }
2527
2528   function = DECL_MAIN_VARIANT (function);
2529   mark_used (function);
2530
2531   fntype = TREE_TYPE (function);
2532   if (TREE_CODE (fntype) == POINTER_TYPE)
2533     fntype = TREE_TYPE (fntype);
2534   basetype = DECL_CLASS_CONTEXT (function);
2535
2536   /* If we are referencing a virtual function from an object
2537      of effectively static type, then there is no need
2538      to go through the virtual function table.  */
2539   if (need_vtbl == maybe_needed)
2540     {
2541       int fixed_type = resolves_to_fixed_type_p (instance, 0);
2542
2543       if (all_virtual == 1
2544           && DECL_VINDEX (function)
2545           && may_be_remote (basetype))
2546         need_vtbl = needed;
2547       else if (DECL_VINDEX (function))
2548         need_vtbl = fixed_type ? unneeded : needed;
2549       else
2550         need_vtbl = not_needed;
2551     }
2552
2553   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
2554       && !DECL_CONSTRUCTOR_P (function))
2555     {
2556       /* Let's be nasty to the user now, and give reasonable
2557          error messages.  */
2558       instance_ptr = current_class_ptr;
2559       if (instance_ptr)
2560         {
2561           if (basetype != current_class_type)
2562             {
2563               if (basetype == error_mark_node)
2564                 return error_mark_node;
2565               else 
2566                 {
2567                   if (orig_basetype != NULL_TREE)
2568                     error_not_base_type (orig_basetype, current_class_type);
2569                   else
2570                     error_not_base_type (function, current_class_type);
2571                   return error_mark_node;
2572                 }
2573             }
2574         }
2575       /* Only allow a static member function to call another static member
2576          function.  */
2577       else if (DECL_LANG_SPECIFIC (function)
2578                && !DECL_STATIC_FUNCTION_P (function))
2579         {
2580           cp_error ("cannot call member function `%D' without object",
2581                     function);
2582           return error_mark_node;
2583         }
2584     }
2585
2586   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2587
2588   if (TYPE_SIZE (complete_type (value_type)) == 0)
2589     {
2590       if (flags & LOOKUP_COMPLAIN)
2591         incomplete_type_error (0, value_type);
2592       return error_mark_node;
2593     }
2594
2595   if (DECL_STATIC_FUNCTION_P (function))
2596     parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2597                                TREE_CHAIN (parms), function, LOOKUP_NORMAL);
2598   else if (need_vtbl == unneeded)
2599     {
2600       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
2601       basetype = TREE_TYPE (instance);
2602       if (TYPE_METHOD_BASETYPE (TREE_TYPE (function))
2603           != TYPE_MAIN_VARIANT (basetype))
2604         {
2605           basetype = DECL_CLASS_CONTEXT (function);
2606           instance_ptr = convert_pointer_to (basetype, instance_ptr);
2607           instance = build_indirect_ref (instance_ptr, NULL_PTR);
2608         }
2609       parms = expr_tree_cons (NULL_TREE, instance_ptr,
2610                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, sub_flags));
2611     }
2612   else
2613     {
2614       if ((flags & LOOKUP_NONVIRTUAL) == 0)
2615         basetype = DECL_CONTEXT (function);
2616
2617       /* First parm could be integer_zerop with casts like
2618          ((Object*)0)->Object::IsA()  */
2619       if (!integer_zerop (TREE_VALUE (parms)))
2620         {
2621           /* Since we can't have inheritance with a union, doing get_binfo
2622              on it won't work.  We do all the convert_pointer_to_real
2623              stuff to handle MI correctly...for unions, that's not
2624              an issue, so we must short-circuit that extra work here.  */
2625           tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
2626           if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
2627             instance_ptr = TREE_VALUE (parms);
2628           else
2629             {
2630               tree binfo = get_binfo (basetype,
2631                                       TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
2632                                       0);
2633               instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
2634             }
2635           instance_ptr
2636             = convert_pointer_to (build_type_variant (basetype,
2637                                                       constp, volatilep),
2638                                   instance_ptr);
2639
2640           if (TREE_CODE (instance_ptr) == COND_EXPR)
2641             {
2642               instance_ptr = save_expr (instance_ptr);
2643               instance = build_indirect_ref (instance_ptr, NULL_PTR);
2644             }
2645           else if (TREE_CODE (instance_ptr) == NOP_EXPR
2646                    && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
2647                    && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
2648             ;
2649           /* The call to `convert_pointer_to' may return error_mark_node.  */
2650           else if (instance_ptr == error_mark_node)
2651             return instance_ptr;
2652           else if (instance == NULL_TREE
2653                    || TREE_CODE (instance) != INDIRECT_REF
2654                    || TREE_OPERAND (instance, 0) != instance_ptr)
2655             instance = build_indirect_ref (instance_ptr, NULL_PTR);
2656         }
2657       parms = expr_tree_cons (NULL_TREE, instance_ptr,
2658                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, LOOKUP_NORMAL));
2659     }
2660
2661   if (parms == error_mark_node
2662       || (parms && TREE_CHAIN (parms) == error_mark_node))
2663     return error_mark_node;
2664
2665   if (need_vtbl == needed)
2666     {
2667       function = build_vfn_ref (&TREE_VALUE (parms), instance,
2668                                 DECL_VINDEX (function));
2669       TREE_TYPE (function) = build_pointer_type (fntype);
2670     }
2671
2672   if (TREE_CODE (function) == FUNCTION_DECL)
2673     GNU_xref_call (current_function_decl,
2674                    IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
2675
2676   result = build_call (function, value_type, parms);
2677   if (IS_AGGR_TYPE (value_type))
2678     result = build_cplus_new (value_type, result);
2679   result = convert_from_reference (result);
2680   return result;
2681 }
2682
2683 /* Similar to `build_method_call', but for overloaded non-member functions.
2684    The name of this function comes through NAME.  The name depends
2685    on PARMS.
2686
2687    Note that this function must handle simple `C' promotions,
2688    as well as variable numbers of arguments (...), and
2689    default arguments to boot.
2690
2691    If the overloading is successful, we return a tree node which
2692    contains the call to the function.
2693
2694    If overloading produces candidates which are probable, but not definite,
2695    we hold these candidates.  If FINAL_CP is non-zero, then we are free
2696    to assume that final_cp points to enough storage for all candidates that
2697    this function might generate.  The `harshness' array is preallocated for
2698    the first candidate, but not for subsequent ones.
2699
2700    Note that the DECL_RTL of FUNCTION must be made to agree with this
2701    function's new name.  */
2702
2703 tree
2704 build_overload_call_real (fnname, parms, flags, final_cp, require_complete)
2705      tree fnname, parms;
2706      int flags;
2707      struct candidate *final_cp;
2708      int require_complete;
2709 {
2710   /* must check for overloading here */
2711   tree functions, function;
2712   tree parmtypes, last;
2713   register tree outer;
2714   int length;
2715   int parmlength = list_length (parms);
2716
2717   struct candidate *candidates, *cp;
2718
2719   if (final_cp)
2720     {
2721       final_cp[0].h.code = 0;
2722       final_cp[0].h.distance = 0;
2723       final_cp[0].function = 0;
2724       /* end marker.  */
2725       final_cp[1].h.code = EVIL_CODE;
2726     }
2727
2728   parmtypes = default_parm_conversions (parms, &last);
2729   if (parmtypes == error_mark_node)
2730     {
2731       if (final_cp)
2732         final_cp->h.code = EVIL_CODE;
2733       return error_mark_node;
2734     }
2735
2736   if (last)
2737     TREE_CHAIN (last) = void_list_node;
2738   else
2739     parmtypes = void_list_node;
2740
2741   if (is_overloaded_fn (fnname))
2742     {
2743       functions = fnname;
2744       if (TREE_CODE (fnname) == TREE_LIST)
2745         fnname = TREE_PURPOSE (functions);
2746       else if (TREE_CODE (fnname) == FUNCTION_DECL)
2747         fnname = DECL_NAME (functions);
2748     }
2749   else 
2750     functions = lookup_name_nonclass (fnname);
2751
2752   if (functions == NULL_TREE)
2753     {
2754       if (flags & LOOKUP_SPECULATIVELY)
2755         return NULL_TREE;
2756       if (flags & LOOKUP_COMPLAIN)
2757         error ("only member functions apply");
2758       if (final_cp)
2759         final_cp->h.code = EVIL_CODE;
2760       return error_mark_node;
2761     }
2762
2763   if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
2764     {
2765       functions = DECL_MAIN_VARIANT (functions);
2766       if (final_cp)
2767         {
2768           /* We are just curious whether this is a viable alternative or
2769              not.  */
2770           compute_conversion_costs (functions, parms, final_cp, parmlength);
2771           return functions;
2772         }
2773       else
2774         return build_function_call_real (functions, parms, 1, flags);
2775     }
2776
2777   if (TREE_CODE (functions) == TREE_LIST
2778       && TREE_VALUE (functions) == NULL_TREE)
2779     {
2780       if (flags & LOOKUP_SPECULATIVELY)
2781         return NULL_TREE;
2782       
2783       if (flags & LOOKUP_COMPLAIN)
2784         cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2785                   TREE_PURPOSE (functions));
2786       if (final_cp)
2787         final_cp->h.code = EVIL_CODE;
2788       return error_mark_node;
2789     }
2790
2791   length = count_functions (functions);
2792   
2793   if (final_cp)
2794     candidates = final_cp;
2795   else
2796     {
2797       candidates
2798         = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
2799       bzero ((char *) candidates, (length + 1) * sizeof (struct candidate));
2800     }
2801
2802   cp = candidates;
2803
2804   my_friendly_assert (is_overloaded_fn (functions), 169);
2805
2806   functions = get_first_fn (functions);
2807
2808   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
2809   for (outer = functions; outer; outer = DECL_CHAIN (outer))
2810     {
2811       int template_cost = 0;
2812       function = outer;
2813       if (TREE_CODE (function) != FUNCTION_DECL
2814           && ! (TREE_CODE (function) == TEMPLATE_DECL
2815                 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
2816         {
2817           enum tree_code code = TREE_CODE (function);
2818           if (code == TEMPLATE_DECL)
2819             code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
2820           if (code == CONST_DECL)
2821             cp_error_at
2822               ("enumeral value `%D' conflicts with function of same name",
2823                function);
2824           else if (code == VAR_DECL)
2825             {
2826               if (TREE_STATIC (function))
2827                 cp_error_at
2828                   ("variable `%D' conflicts with function of same name",
2829                    function);
2830               else
2831                 cp_error_at
2832                   ("constant field `%D' conflicts with function of same name",
2833                    function);
2834             }
2835           else if (code == TYPE_DECL)
2836             continue;
2837           else
2838             my_friendly_abort (2);
2839           error ("at this point in file");
2840           continue;
2841         }
2842       if (TREE_CODE (function) == TEMPLATE_DECL)
2843         {
2844           int ntparms = DECL_NTPARMS (function);
2845           tree targs = make_scratch_vec (ntparms);
2846           int i;
2847
2848           i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (function),
2849                                 &TREE_VEC_ELT (targs, 0),
2850                                 TYPE_ARG_TYPES (TREE_TYPE (function)),
2851                                 parms, NULL_TREE, &template_cost, 0, 0);
2852           if (i == 0)
2853             {
2854               function = instantiate_template (function, targs);
2855               if (function == error_mark_node)
2856                 return function;
2857             }
2858         }
2859
2860       if (TREE_CODE (function) == TEMPLATE_DECL)
2861         {
2862           /* Unconverted template -- failed match.  */
2863           cp->function = function;
2864           cp->u.bad_arg = -4;
2865           cp->h.code = EVIL_CODE;
2866         }
2867       else
2868         {
2869           struct candidate *cp2;
2870
2871           /* Check that this decl is not the same as a function that's in
2872              the list due to some template instantiation.  */
2873           cp2 = candidates;
2874           while (cp2 != cp)
2875             if (cp2->function == function)
2876               break;
2877             else
2878               cp2 += 1;
2879           if (cp2->function == function)
2880             continue;
2881
2882           function = DECL_MAIN_VARIANT (function);
2883
2884           /* Can't use alloca here, since result might be
2885              passed to calling function.  */
2886           cp->h_len = parmlength;
2887           cp->harshness = (struct harshness_code *)
2888             scratchalloc ((parmlength + 1) * sizeof (struct harshness_code));
2889
2890           compute_conversion_costs (function, parms, cp, parmlength);
2891
2892           /* Make sure this is clear as well.  */
2893           cp->h.int_penalty += template_cost;
2894
2895           if ((cp[0].h.code & EVIL_CODE) == 0)
2896             {
2897               cp[1].h.code = EVIL_CODE;
2898               cp++;
2899             }
2900         }
2901     }
2902
2903   if (cp - candidates)
2904     {
2905       tree rval = error_mark_node;
2906
2907       /* Leave marker.  */
2908       cp[0].h.code = EVIL_CODE;
2909       if (cp - candidates > 1)
2910         {
2911           struct candidate *best_cp
2912             = ideal_candidate (candidates, cp - candidates, parmlength);
2913           if (best_cp == (struct candidate *)0)
2914             {
2915               if (flags & LOOKUP_COMPLAIN)
2916                 {
2917                   cp_error ("call of overloaded `%D' is ambiguous", fnname);
2918                   print_n_candidates (candidates, cp - candidates);
2919                 }
2920               return error_mark_node;
2921             }
2922           else
2923             rval = best_cp->function;
2924         }
2925       else
2926         {
2927           cp -= 1;
2928           if (cp->h.code & EVIL_CODE)
2929             {
2930               if (flags & LOOKUP_COMPLAIN)
2931                 error ("type conversion ambiguous");
2932             }
2933           else
2934             rval = cp->function;
2935         }
2936
2937       if (final_cp)
2938         return rval;
2939
2940       return build_function_call_real (rval, parms, require_complete, flags);
2941     }
2942
2943   if (flags & LOOKUP_SPECULATIVELY)
2944     return NULL_TREE;
2945   
2946   if (flags & LOOKUP_COMPLAIN)
2947     report_type_mismatch (cp, parms, "function");
2948
2949   return error_mark_node;
2950 }
2951
2952 /* This requires a complete type on the result of the call.  */
2953
2954 tree
2955 build_overload_call (fnname, parms, flags)
2956      tree fnname, parms;
2957      int flags;
2958 {
2959   return build_overload_call_real (fnname, parms, flags, (struct candidate *)0, 1);
2960 }
2961
2962 /* New overloading code.  */
2963
2964 struct z_candidate {
2965   tree fn;
2966   tree convs;
2967   tree second_conv;
2968   int viable;
2969   tree basetype_path;
2970   tree template;
2971   struct z_candidate *next;
2972 };
2973
2974 #define IDENTITY_RANK 0
2975 #define EXACT_RANK 1
2976 #define PROMO_RANK 2
2977 #define STD_RANK 3
2978 #define PBOOL_RANK 4
2979 #define USER_RANK 5
2980 #define ELLIPSIS_RANK 6
2981 #define BAD_RANK 7
2982
2983 #define ICS_RANK(NODE)                          \
2984   (ICS_BAD_FLAG (NODE) ? BAD_RANK   \
2985    : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK   \
2986    : ICS_USER_FLAG (NODE) ? USER_RANK           \
2987    : ICS_STD_RANK (NODE))
2988
2989 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
2990
2991 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
2992 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
2993 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
2994 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
2995
2996 #define USER_CONV_FN(NODE) TREE_OPERAND (NODE, 1)
2997
2998 int
2999 null_ptr_cst_p (t)
3000      tree t;
3001 {
3002   if (t == null_node
3003       || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
3004     return 1;
3005   return 0;
3006 }
3007
3008 static tree
3009 build_conv (code, type, from)
3010      enum tree_code code;
3011      tree type, from;
3012 {
3013   tree t = build1 (code, type, from);
3014   int rank = ICS_STD_RANK (from);
3015   switch (code)
3016     {
3017     case PTR_CONV:
3018     case PMEM_CONV:
3019     case BASE_CONV:
3020     case STD_CONV:
3021       if (rank < STD_RANK)
3022         rank = STD_RANK;
3023       break;
3024
3025     case QUAL_CONV:
3026       if (rank < EXACT_RANK)
3027         rank = EXACT_RANK;
3028
3029     default:
3030       break;
3031     }
3032   ICS_STD_RANK (t) = rank;
3033   ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
3034   ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
3035   return t;
3036 }
3037
3038 static tree
3039 non_reference (t)
3040      tree t;
3041 {
3042   if (TREE_CODE (t) == REFERENCE_TYPE)
3043     t = TREE_TYPE (t);
3044   return t;
3045 }
3046
3047 static tree
3048 strip_top_quals (t)
3049      tree t;
3050 {
3051   if (TREE_CODE (t) == ARRAY_TYPE)
3052     return t;
3053   return TYPE_MAIN_VARIANT (t);
3054 }
3055
3056 /* Returns the standard conversion path (see [conv]) from type FROM to type
3057    TO, if any.  For proper handling of null pointer constants, you must
3058    also pass the expression EXPR to convert from.  */
3059
3060 static tree
3061 standard_conversion (to, from, expr)
3062      tree to, from, expr;
3063 {
3064   enum tree_code fcode, tcode;
3065   tree conv;
3066   int fromref = 0;
3067
3068   if (TREE_CODE (to) == REFERENCE_TYPE)
3069     to = TREE_TYPE (to);
3070   if (TREE_CODE (from) == REFERENCE_TYPE)
3071     {
3072       fromref = 1;
3073       from = TREE_TYPE (from);
3074     }
3075   to = strip_top_quals (to);
3076   from = strip_top_quals (from);
3077
3078   fcode = TREE_CODE (from);
3079   tcode = TREE_CODE (to);
3080
3081   conv = build1 (IDENTITY_CONV, from, expr);
3082
3083   if (fcode == FUNCTION_TYPE)
3084     {
3085       from = build_pointer_type (from);
3086       fcode = TREE_CODE (from);
3087       conv = build_conv (LVALUE_CONV, from, conv);
3088     }
3089   else if (fcode == ARRAY_TYPE)
3090     {
3091       from = build_pointer_type (TREE_TYPE (from));
3092       fcode = TREE_CODE (from);
3093       conv = build_conv (LVALUE_CONV, from, conv);
3094     }
3095   else if (fromref || (expr && real_lvalue_p (expr)))
3096     conv = build_conv (RVALUE_CONV, from, conv);
3097
3098   if (from == to)
3099     return conv;
3100
3101   if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
3102       && expr && null_ptr_cst_p (expr))
3103     {
3104       conv = build_conv (STD_CONV, to, conv);
3105     }
3106   else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
3107     {
3108       enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
3109       enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
3110
3111       if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
3112           && ufcode != FUNCTION_TYPE)
3113         {
3114           from = build_pointer_type
3115             (cp_build_type_variant (void_type_node,
3116                                     TYPE_READONLY (TREE_TYPE (from)),
3117                                     TYPE_VOLATILE (TREE_TYPE (from))));
3118           conv = build_conv (PTR_CONV, from, conv);
3119         }
3120       else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
3121         {
3122           tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
3123           tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
3124
3125           if (DERIVED_FROM_P (fbase, tbase)
3126               && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
3127                              TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))),
3128                              1)))
3129             {
3130               from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
3131               from = build_pointer_type (from);
3132               conv = build_conv (PMEM_CONV, from, conv);
3133             }
3134         }
3135       else if (IS_AGGR_TYPE (TREE_TYPE (from))
3136                && IS_AGGR_TYPE (TREE_TYPE (to)))
3137         {
3138           if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
3139             {
3140               from = cp_build_type_variant (TREE_TYPE (to),
3141                                             TYPE_READONLY (TREE_TYPE (from)),
3142                                             TYPE_VOLATILE (TREE_TYPE (from)));
3143               from = build_pointer_type (from);
3144               conv = build_conv (PTR_CONV, from, conv);
3145             }
3146         }
3147
3148       if (comptypes (from, to, 1))
3149         /* OK */;
3150       else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
3151         conv = build_conv (QUAL_CONV, to, conv);
3152       else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
3153         {
3154           conv = build_conv (PTR_CONV, to, conv);
3155           ICS_BAD_FLAG (conv) = 1;
3156         }
3157       else
3158         return 0;
3159
3160       from = to;
3161     }
3162   else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
3163     {
3164       tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
3165       tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
3166       tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
3167       tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
3168
3169       if (! DERIVED_FROM_P (fbase, tbase)
3170           || ! comptypes (TREE_TYPE (fromfn), TREE_TYPE (tofn), 1)
3171           || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
3172                           TREE_CHAIN (TYPE_ARG_TYPES (tofn)), 1)
3173           || TYPE_READONLY (fbase) != TYPE_READONLY (tbase)
3174           || TYPE_VOLATILE (fbase) != TYPE_VOLATILE (tbase))
3175         return 0;
3176
3177       from = cp_build_type_variant (tbase, TYPE_READONLY (fbase),
3178                                     TYPE_VOLATILE (fbase));
3179       from = build_cplus_method_type (from, TREE_TYPE (fromfn),
3180                                       TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
3181       from = build_ptrmemfunc_type (build_pointer_type (from));
3182       conv = build_conv (PMEM_CONV, from, conv);
3183     }
3184   else if (tcode == BOOLEAN_TYPE)
3185     {
3186       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
3187              || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
3188         return 0;
3189
3190       conv = build_conv (STD_CONV, to, conv);
3191       if (fcode == POINTER_TYPE
3192           || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
3193         ICS_STD_RANK (conv) = PBOOL_RANK;
3194     }
3195   /* We don't check for ENUMERAL_TYPE here because there are no standard
3196      conversions to enum type.  */
3197   else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
3198            || tcode == REAL_TYPE)
3199     {
3200       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
3201         return 0;
3202       conv = build_conv (STD_CONV, to, conv);
3203
3204       /* Give this a better rank if it's a promotion.  */
3205       if (to == type_promotes_to (from)
3206           && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
3207         ICS_STD_RANK (conv) = PROMO_RANK;
3208     }
3209   else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
3210            && DERIVED_FROM_P (to, from))
3211     {
3212       if (TREE_CODE (conv) == RVALUE_CONV)
3213         conv = TREE_OPERAND (conv, 0);
3214       conv = build_conv (BASE_CONV, to, conv);
3215     }
3216   else
3217     return 0;
3218
3219   return conv;
3220 }
3221
3222 /* Returns the conversion path from type FROM to reference type TO for
3223    purposes of reference binding.  For lvalue binding, either pass a
3224    reference type to FROM or an lvalue expression to EXPR.
3225
3226    Currently does not distinguish in the generated trees between binding to
3227    an lvalue and a temporary.  Should it?  */
3228
3229 static tree
3230 reference_binding (rto, rfrom, expr, flags)
3231      tree rto, rfrom, expr;
3232      int flags;
3233 {
3234   tree conv;
3235   int lvalue = 1;
3236   tree to = TREE_TYPE (rto);
3237   tree from = rfrom;
3238   int related;
3239
3240   if (TREE_CODE (from) == REFERENCE_TYPE)
3241     from = TREE_TYPE (from);
3242   else if (! expr || ! real_lvalue_p (expr))
3243     lvalue = 0;
3244
3245   related = (comptypes (TYPE_MAIN_VARIANT (to),
3246                         TYPE_MAIN_VARIANT (from), 1)
3247              || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
3248                  && DERIVED_FROM_P (to, from)));
3249
3250   if (lvalue && related
3251       && TYPE_READONLY (to) >= TYPE_READONLY (from)
3252       && TYPE_VOLATILE (to) >= TYPE_VOLATILE (from))
3253     {
3254       conv = build1 (IDENTITY_CONV, from, expr);
3255
3256       if (comptypes (TYPE_MAIN_VARIANT (to),
3257                      TYPE_MAIN_VARIANT (from), 1))
3258         conv = build_conv (REF_BIND, rto, conv);
3259       else
3260         {
3261           conv = build_conv (REF_BIND, rto, conv);
3262           ICS_STD_RANK (conv) = STD_RANK;
3263         }
3264     }
3265   else
3266     conv = NULL_TREE;
3267
3268   if (! conv)
3269     {
3270       conv = standard_conversion (to, rfrom, expr);
3271       if (conv)
3272         {
3273           conv = build_conv (REF_BIND, rto, conv);
3274
3275           /* Bind directly to a base subobject of a class rvalue.  Do it
3276              after building the conversion for proper handling of ICS_RANK.  */
3277           if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
3278             TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
3279         }
3280       if (conv
3281           && ((! (TYPE_READONLY (to) && ! TYPE_VOLATILE (to)
3282                   && (flags & LOOKUP_NO_TEMP_BIND) == 0))
3283               /* If T1 is reference-related to T2, cv1 must be the same
3284                  cv-qualification as, or greater cv-qualification than,
3285                  cv2; otherwise, the program is ill-formed.  */
3286               || (related
3287                   && (TYPE_READONLY (to) < TYPE_READONLY (from)
3288                       || TYPE_VOLATILE (to) < TYPE_VOLATILE (from)))))
3289         ICS_BAD_FLAG (conv) = 1;
3290     }
3291
3292   return conv;
3293 }
3294
3295 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
3296    to type TO.  The optional expression EXPR may affect the conversion.
3297    FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
3298    significant.  */
3299
3300 static tree
3301 implicit_conversion (to, from, expr, flags)
3302      tree to, from, expr;
3303      int flags;
3304 {
3305   tree conv;
3306   struct z_candidate *cand;
3307
3308   if (expr && type_unknown_p (expr))
3309     {
3310       expr = instantiate_type (to, expr, 0);
3311       if (expr == error_mark_node)
3312         return 0;
3313       from = TREE_TYPE (expr);
3314     }
3315
3316   if (TREE_CODE (to) == REFERENCE_TYPE)
3317     conv = reference_binding (to, from, expr, flags);
3318   else
3319     conv = standard_conversion (to, from, expr);
3320
3321   if (conv)
3322     ;
3323   else if (expr != NULL_TREE
3324            && (IS_AGGR_TYPE (non_reference (from))
3325             || IS_AGGR_TYPE (non_reference (to)))
3326            && (flags & LOOKUP_NO_CONVERSION) == 0)
3327     {
3328       cand = build_user_type_conversion_1
3329         (to, expr, LOOKUP_ONLYCONVERTING);
3330       if (cand)
3331         conv = cand->second_conv;
3332       if ((! conv || ICS_BAD_FLAG (conv))
3333           && TREE_CODE (to) == REFERENCE_TYPE
3334           && (flags & LOOKUP_NO_TEMP_BIND) == 0)
3335         {
3336           cand = build_user_type_conversion_1
3337             (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
3338           if (cand)
3339             {
3340               if (! TYPE_READONLY (TREE_TYPE (to))
3341                   || TYPE_VOLATILE (TREE_TYPE (to)))
3342                 ICS_BAD_FLAG (cand->second_conv) = 1;
3343               if (!conv || (ICS_BAD_FLAG (conv)
3344                             > ICS_BAD_FLAG (cand->second_conv)))
3345                 conv = build_conv (REF_BIND, to, cand->second_conv);
3346             }
3347         }
3348     }
3349
3350   return conv;
3351 }
3352
3353 /* Create an overload candidate for the function or method FN called with
3354    the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
3355    to implicit_conversion.  */
3356
3357 static struct z_candidate *
3358 add_function_candidate (candidates, fn, arglist, flags)
3359      struct z_candidate *candidates;
3360      tree fn, arglist;
3361      int flags;
3362 {
3363   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
3364   int i, len;
3365   tree convs;
3366   tree parmnode = parmlist;
3367   tree argnode = arglist;
3368   int viable = 1;
3369   struct z_candidate *cand;
3370
3371   /* The `this' and `in_chrg' arguments to constructors are not considered
3372      in overload resolution.  */
3373   if (DECL_CONSTRUCTOR_P (fn))
3374     {
3375       parmnode = TREE_CHAIN (parmnode);
3376       argnode = TREE_CHAIN (argnode);
3377       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3378         {
3379           parmnode = TREE_CHAIN (parmnode);
3380           argnode = TREE_CHAIN (argnode);
3381         }
3382     }
3383
3384   len = list_length (argnode);
3385   convs = make_scratch_vec (len);
3386
3387   for (i = 0; i < len; ++i)
3388     {
3389       tree arg = TREE_VALUE (argnode);
3390       tree argtype = TREE_TYPE (arg);
3391       tree t;
3392
3393       argtype = cp_build_type_variant
3394         (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
3395
3396       if (parmnode == void_list_node)
3397         break;
3398       else if (parmnode)
3399         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
3400       else
3401         {
3402           t = build1 (IDENTITY_CONV, argtype, arg);
3403           ICS_ELLIPSIS_FLAG (t) = 1;
3404         }
3405
3406       if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
3407           && ! DECL_CONSTRUCTOR_P (fn))
3408         ICS_THIS_FLAG (t) = 1;
3409
3410       TREE_VEC_ELT (convs, i) = t;
3411       if (! t)
3412         break;
3413
3414       if (ICS_BAD_FLAG (t))
3415         viable = -1;
3416
3417       if (parmnode)
3418         parmnode = TREE_CHAIN (parmnode);
3419       argnode = TREE_CHAIN (argnode);
3420     }
3421
3422   if (i < len)
3423     viable = 0;
3424
3425   /* Make sure there are default args for the rest of the parms.  */
3426   for (; parmnode && parmnode != void_list_node;
3427        parmnode = TREE_CHAIN (parmnode))
3428     if (! TREE_PURPOSE (parmnode))
3429       {
3430         viable = 0;
3431         break;
3432       }
3433
3434   cand = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
3435
3436   cand->fn = fn;
3437   cand->convs = convs;
3438   cand->second_conv = NULL_TREE;
3439   cand->viable = viable;
3440   cand->basetype_path = NULL_TREE;
3441   cand->template = NULL_TREE;
3442   cand->next = candidates;
3443
3444   return cand;
3445 }
3446
3447 /* Create an overload candidate for the conversion function FN which will
3448    be invoked for expression OBJ, producing a pointer-to-function which
3449    will in turn be called with the argument list ARGLIST, and add it to
3450    CANDIDATES.  FLAGS is passed on to implicit_conversion.  */
3451
3452 static struct z_candidate *
3453 add_conv_candidate (candidates, fn, obj, arglist)
3454      struct z_candidate *candidates;
3455      tree fn, obj, arglist;
3456 {
3457   tree totype = TREE_TYPE (TREE_TYPE (fn));
3458   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
3459   int i, len = list_length (arglist) + 1;
3460   tree convs = make_scratch_vec (len);
3461   tree parmnode = parmlist;
3462   tree argnode = arglist;
3463   int viable = 1;
3464   struct z_candidate *cand;
3465   int flags = LOOKUP_NORMAL;
3466
3467   for (i = 0; i < len; ++i)
3468     {
3469       tree arg = i == 0 ? obj : TREE_VALUE (argnode);
3470       tree argtype = lvalue_type (arg);
3471       tree t;
3472
3473       if (i == 0)
3474         t = implicit_conversion (totype, argtype, arg, flags);
3475       else if (parmnode == void_list_node)
3476         break;
3477       else if (parmnode)
3478         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
3479       else
3480         {
3481           t = build1 (IDENTITY_CONV, argtype, arg);
3482           ICS_ELLIPSIS_FLAG (t) = 1;
3483         }
3484
3485       TREE_VEC_ELT (convs, i) = t;
3486       if (! t)
3487         break;
3488
3489       if (ICS_BAD_FLAG (t))
3490         viable = -1;
3491
3492       if (i == 0)
3493         continue;
3494
3495       if (parmnode)
3496         parmnode = TREE_CHAIN (parmnode);
3497       argnode = TREE_CHAIN (argnode);
3498     }
3499
3500   if (i < len)
3501     viable = 0;
3502
3503   for (; parmnode && parmnode != void_list_node;
3504        parmnode = TREE_CHAIN (parmnode))
3505     if (! TREE_PURPOSE (parmnode))
3506       {
3507         viable = 0;
3508         break;
3509       }
3510
3511   cand = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
3512
3513   cand->fn = fn;
3514   cand->convs = convs;
3515   cand->second_conv = NULL_TREE;
3516   cand->viable = viable;
3517   cand->basetype_path = NULL_TREE;
3518   cand->template = NULL_TREE;
3519   cand->next = candidates;
3520
3521   return cand;
3522 }
3523
3524 static struct z_candidate *
3525 build_builtin_candidate (candidates, fnname, type1, type2,
3526                          args, argtypes, flags)
3527      struct z_candidate *candidates;
3528      tree fnname, type1, type2, *args, *argtypes;
3529      int flags;
3530
3531 {
3532   tree t, convs;
3533   int viable = 1, i;
3534   struct z_candidate *cand;
3535   tree types[2];
3536
3537   types[0] = type1;
3538   types[1] = type2;
3539
3540   convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
3541
3542   for (i = 0; i < 2; ++i)
3543     {
3544       if (! args[i])
3545         break;
3546
3547       t = implicit_conversion (types[i], argtypes[i], args[i], flags);
3548       if (! t)
3549         {
3550           viable = 0;
3551           /* We need something for printing the candidate.  */
3552           t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
3553         }
3554       else if (ICS_BAD_FLAG (t))
3555         viable = 0;
3556       TREE_VEC_ELT (convs, i) = t;
3557     }
3558
3559   /* For COND_EXPR we rearranged the arguments; undo that now.  */
3560   if (args[2])
3561     {
3562       TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
3563       TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
3564       t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
3565       if (t)
3566         TREE_VEC_ELT (convs, 0) = t;
3567       else
3568         viable = 0;
3569     }      
3570
3571   cand = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
3572
3573   cand->fn = fnname;
3574   cand->convs = convs;
3575   cand->second_conv = NULL_TREE;
3576   cand->viable = viable;
3577   cand->basetype_path = NULL_TREE;
3578   cand->template = NULL_TREE;
3579   cand->next = candidates;
3580
3581   return cand;
3582 }
3583
3584 static int
3585 is_complete (t)
3586      tree t;
3587 {
3588   return TYPE_SIZE (complete_type (t)) != NULL_TREE;
3589 }
3590
3591 /* Create any builtin operator overload candidates for the operator in
3592    question given the converted operand types TYPE1 and TYPE2.  The other
3593    args are passed through from add_builtin_candidates to
3594    build_builtin_candidate.  */
3595
3596 static struct z_candidate *
3597 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
3598                        args, argtypes, flags)
3599      struct z_candidate *candidates;
3600      enum tree_code code, code2;
3601      tree fnname, type1, type2, *args, *argtypes;
3602      int flags;
3603 {
3604   switch (code)
3605     {
3606     case POSTINCREMENT_EXPR:
3607     case POSTDECREMENT_EXPR:
3608       args[1] = integer_zero_node;
3609       type2 = integer_type_node;
3610       break;
3611     default:
3612       break;
3613     }
3614
3615   switch (code)
3616     {
3617
3618 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
3619      and  VQ  is  either  volatile or empty, there exist candidate operator
3620      functions of the form
3621              VQ T&   operator++(VQ T&);
3622              T       operator++(VQ T&, int);
3623    5 For every pair T, VQ), where T is an enumeration type or an arithmetic
3624      type  other than bool, and VQ is either volatile or empty, there exist
3625      candidate operator functions of the form
3626              VQ T&   operator--(VQ T&);
3627              T       operator--(VQ T&, int);
3628    6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
3629      complete  object type, and VQ is either volatile or empty, there exist
3630      candidate operator functions of the form
3631              T*VQ&   operator++(T*VQ&);
3632              T*VQ&   operator--(T*VQ&);
3633              T*      operator++(T*VQ&, int);
3634              T*      operator--(T*VQ&, int);  */
3635
3636     case POSTDECREMENT_EXPR:
3637     case PREDECREMENT_EXPR:
3638       if (TREE_CODE (type1) == BOOLEAN_TYPE)
3639         return candidates;
3640     case POSTINCREMENT_EXPR:
3641     case PREINCREMENT_EXPR:
3642       if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
3643           || TYPE_PTROB_P (type1))
3644         {
3645           type1 = build_reference_type (type1);
3646           break;
3647         }
3648       return candidates;
3649
3650 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
3651      exist candidate operator functions of the form
3652
3653              T&      operator*(T*);
3654
3655    8 For every function type T, there exist candidate operator functions of
3656      the form
3657              T&      operator*(T*);  */
3658
3659     case INDIRECT_REF:
3660       if (TREE_CODE (type1) == POINTER_TYPE
3661           && (TYPE_PTROB_P (type1)
3662               || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
3663         break;
3664       return candidates;
3665
3666 /* 9 For every type T, there exist candidate operator functions of the form
3667              T*      operator+(T*);
3668
3669    10For  every  promoted arithmetic type T, there exist candidate operator
3670      functions of the form
3671              T       operator+(T);
3672              T       operator-(T);  */
3673
3674     case CONVERT_EXPR: /* unary + */
3675       if (TREE_CODE (type1) == POINTER_TYPE
3676           && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
3677         break;
3678     case NEGATE_EXPR:
3679       if (ARITHMETIC_TYPE_P (type1))
3680         break;
3681       return candidates;
3682
3683 /* 11For every promoted integral type T,  there  exist  candidate  operator
3684      functions of the form
3685              T       operator~(T);  */
3686
3687     case BIT_NOT_EXPR:
3688       if (INTEGRAL_TYPE_P (type1))
3689         break;
3690       return candidates;
3691
3692 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
3693      is the same type as C2 or is a derived class of C2, T  is  a  complete
3694      object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
3695      there exist candidate operator functions of the form
3696              CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3697      where CV12 is the union of CV1 and CV2.  */
3698
3699     case MEMBER_REF:
3700       if (TREE_CODE (type1) == POINTER_TYPE
3701           && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
3702         {
3703           tree c1 = TREE_TYPE (type1);
3704           tree c2 = (TYPE_PTRMEMFUNC_P (type2)
3705                      ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
3706                      : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
3707
3708           if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
3709               && (TYPE_PTRMEMFUNC_P (type2)
3710                   || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
3711             break;
3712         }
3713       return candidates;
3714
3715 /* 13For every pair of promoted arithmetic types L and R, there exist  can-
3716      didate operator functions of the form
3717              LR      operator*(L, R);
3718              LR      operator/(L, R);
3719              LR      operator+(L, R);
3720              LR      operator-(L, R);
3721              bool    operator<(L, R);
3722              bool    operator>(L, R);
3723              bool    operator<=(L, R);
3724              bool    operator>=(L, R);
3725              bool    operator==(L, R);
3726              bool    operator!=(L, R);
3727      where  LR  is  the  result of the usual arithmetic conversions between
3728      types L and R.
3729
3730    14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
3731      unqualified  complete  object  type and I is a promoted integral type,
3732      there exist candidate operator functions of the form
3733              T*      operator+(T*, I);
3734              T&      operator[](T*, I);
3735              T*      operator-(T*, I);
3736              T*      operator+(I, T*);
3737              T&      operator[](I, T*);
3738
3739    15For every T, where T is a pointer to complete object type, there exist
3740      candidate operator functions of the form112)
3741              ptrdiff_t operator-(T, T);
3742
3743    16For  every pointer type T, there exist candidate operator functions of
3744      the form
3745              bool    operator<(T, T);
3746              bool    operator>(T, T);
3747              bool    operator<=(T, T);
3748              bool    operator>=(T, T);
3749              bool    operator==(T, T);
3750              bool    operator!=(T, T);
3751
3752    17For every pointer to member type T,  there  exist  candidate  operator
3753      functions of the form
3754              bool    operator==(T, T);
3755              bool    operator!=(T, T);  */
3756
3757     case MINUS_EXPR:
3758       if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3759         break;
3760       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
3761         {
3762           type2 = ptrdiff_type_node;
3763           break;
3764         }
3765     case MULT_EXPR:
3766     case TRUNC_DIV_EXPR:
3767       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3768         break;
3769       return candidates;
3770
3771     case EQ_EXPR:
3772     case NE_EXPR:
3773       if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3774           || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
3775         break;
3776       if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
3777           && null_ptr_cst_p (args[1]))
3778         {
3779           type2 = type1;
3780           break;
3781         }
3782       if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
3783           && null_ptr_cst_p (args[0]))
3784         {
3785           type1 = type2;
3786           break;
3787         }
3788     case LT_EXPR:
3789     case GT_EXPR:
3790     case LE_EXPR:
3791     case GE_EXPR:
3792     case MAX_EXPR:
3793     case MIN_EXPR:
3794       if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3795           || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
3796         break;
3797       if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
3798         {
3799           type2 = type1;
3800           break;
3801         }
3802       if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
3803         {
3804           type1 = type2;
3805           break;
3806         }
3807       return candidates;
3808
3809     case PLUS_EXPR:
3810       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3811         break;
3812     case ARRAY_REF:
3813       if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
3814         {
3815           type1 = ptrdiff_type_node;
3816           break;
3817         }
3818       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
3819         {
3820           type2 = ptrdiff_type_node;
3821           break;
3822         }
3823       return candidates;
3824
3825 /* 18For  every pair of promoted integral types L and R, there exist candi-
3826      date operator functions of the form
3827              LR      operator%(L, R);
3828              LR      operator&(L, R);
3829              LR      operator^(L, R);
3830              LR      operator|(L, R);
3831              L       operator<<(L, R);
3832              L       operator>>(L, R);
3833      where LR is the result of the  usual  arithmetic  conversions  between
3834      types L and R.  */
3835
3836     case TRUNC_MOD_EXPR:
3837     case BIT_AND_EXPR:
3838     case BIT_IOR_EXPR:
3839     case BIT_XOR_EXPR:
3840     case LSHIFT_EXPR:
3841     case RSHIFT_EXPR:
3842       if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
3843         break;
3844       return candidates;
3845
3846 /* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
3847      type, VQ is either volatile or empty, and R is a  promoted  arithmetic
3848      type, there exist candidate operator functions of the form
3849              VQ L&   operator=(VQ L&, R);
3850              VQ L&   operator*=(VQ L&, R);
3851              VQ L&   operator/=(VQ L&, R);
3852              VQ L&   operator+=(VQ L&, R);
3853              VQ L&   operator-=(VQ L&, R);
3854
3855    20For  every  pair T, VQ), where T is any type and VQ is either volatile
3856      or empty, there exist candidate operator functions of the form
3857              T*VQ&   operator=(T*VQ&, T*);
3858
3859    21For every pair T, VQ), where T is a pointer to member type and  VQ  is
3860      either  volatile or empty, there exist candidate operator functions of
3861      the form
3862              VQ T&   operator=(VQ T&, T);
3863
3864    22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
3865      unqualified  complete object type, VQ is either volatile or empty, and
3866      I is a promoted integral type, there exist  candidate  operator  func-
3867      tions of the form
3868              T*VQ&   operator+=(T*VQ&, I);
3869              T*VQ&   operator-=(T*VQ&, I);
3870
3871    23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
3872      type, VQ is either volatile or empty, and R  is  a  promoted  integral
3873      type, there exist candidate operator functions of the form
3874
3875              VQ L&   operator%=(VQ L&, R);
3876              VQ L&   operator<<=(VQ L&, R);
3877              VQ L&   operator>>=(VQ L&, R);
3878              VQ L&   operator&=(VQ L&, R);
3879              VQ L&   operator^=(VQ L&, R);
3880              VQ L&   operator|=(VQ L&, R);  */
3881
3882     case MODIFY_EXPR:
3883       switch (code2)
3884         {
3885         case PLUS_EXPR:
3886         case MINUS_EXPR:
3887           if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
3888             {
3889               type2 = ptrdiff_type_node;
3890               break;
3891             }
3892         case MULT_EXPR:
3893         case TRUNC_DIV_EXPR:
3894           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3895             break;
3896           return candidates;
3897
3898         case TRUNC_MOD_EXPR:
3899         case BIT_AND_EXPR:
3900         case BIT_IOR_EXPR:
3901         case BIT_XOR_EXPR:
3902         case LSHIFT_EXPR:
3903         case RSHIFT_EXPR:
3904           if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
3905             break;
3906           return candidates;
3907
3908         case NOP_EXPR:
3909           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3910             break;
3911           if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3912               || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3913               || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
3914               || ((TYPE_PTRMEMFUNC_P (type1)
3915                    || TREE_CODE (type1) == POINTER_TYPE)
3916                   && null_ptr_cst_p (args[1])))
3917             {
3918               type2 = type1;
3919               break;
3920             }
3921           return candidates;
3922
3923         default:
3924           my_friendly_abort (367);
3925         }
3926       type1 = build_reference_type (type1);
3927       break;
3928
3929     case COND_EXPR:
3930       /* Kludge around broken overloading rules whereby
3931          bool ? const char& : enum is ambiguous
3932          (between int and const char&).  */
3933       flags |= LOOKUP_NO_TEMP_BIND;
3934
3935       /* Extension: Support ?: of enumeral type.  Hopefully this will not
3936          be an extension for long.  */
3937       if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
3938         break;
3939       else if (TREE_CODE (type1) == ENUMERAL_TYPE
3940                || TREE_CODE (type2) == ENUMERAL_TYPE)
3941         return candidates;
3942       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3943         break;
3944       if (TREE_CODE (type1) == TREE_CODE (type2)
3945           && (TREE_CODE (type1) == REFERENCE_TYPE
3946               || TREE_CODE (type1) == POINTER_TYPE
3947               || TYPE_PTRMEMFUNC_P (type1)
3948               || IS_AGGR_TYPE (type1)))
3949         break;
3950       if (TREE_CODE (type1) == REFERENCE_TYPE
3951           || TREE_CODE (type2) == REFERENCE_TYPE)
3952         return candidates;
3953       if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
3954            && null_ptr_cst_p (args[1]))
3955           || IS_AGGR_TYPE (type1))
3956         {
3957           type2 = type1;
3958           break;
3959         }
3960       if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
3961            && null_ptr_cst_p (args[0]))
3962           || IS_AGGR_TYPE (type2))
3963         {
3964           type1 = type2;
3965           break;
3966         }
3967       return candidates;
3968
3969     default:
3970       my_friendly_abort (367);
3971     }
3972
3973   /* If we're dealing with two pointer types, we need candidates
3974      for both of them.  */
3975   if (type2 && type1 != type2
3976       && TREE_CODE (type1) == TREE_CODE (type2)
3977       && (TREE_CODE (type1) == REFERENCE_TYPE
3978           || (TREE_CODE (type1) == POINTER_TYPE
3979               && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
3980           || TYPE_PTRMEMFUNC_P (type1)
3981           || IS_AGGR_TYPE (type1)))
3982     {
3983       candidates = build_builtin_candidate
3984         (candidates, fnname, type1, type1, args, argtypes, flags);
3985       return build_builtin_candidate
3986         (candidates, fnname, type2, type2, args, argtypes, flags);
3987     }
3988
3989   return build_builtin_candidate
3990     (candidates, fnname, type1, type2, args, argtypes, flags);
3991 }
3992
3993 tree
3994 type_decays_to (type)
3995      tree type;
3996 {
3997   if (TREE_CODE (type) == ARRAY_TYPE)
3998     return build_pointer_type (TREE_TYPE (type));
3999   if (TREE_CODE (type) == FUNCTION_TYPE)
4000     return build_pointer_type (type);
4001   return type;
4002 }
4003
4004 /* There are three conditions of builtin candidates:
4005
4006    1) bool-taking candidates.  These are the same regardless of the input.
4007    2) pointer-pair taking candidates.  These are generated for each type
4008       one of the input types converts to.
4009    3) arithmetic candidates.  According to the WP, we should generate
4010       all of these, but I'm trying not to... */
4011
4012 static struct z_candidate *
4013 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
4014      struct z_candidate *candidates;
4015      enum tree_code code, code2;
4016      tree fnname, *args;
4017      int flags;
4018 {
4019   int ref1, i;
4020   tree type, argtypes[3], types[2];
4021
4022   for (i = 0; i < 3; ++i)
4023     {
4024       if (args[i])
4025         argtypes[i]  = lvalue_type (args[i]);
4026       else
4027         argtypes[i] = NULL_TREE;
4028     }
4029
4030   switch (code)
4031     {
4032 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
4033      and  VQ  is  either  volatile or empty, there exist candidate operator
4034      functions of the form
4035                  VQ T&   operator++(VQ T&);  */
4036
4037     case POSTINCREMENT_EXPR:
4038     case PREINCREMENT_EXPR:
4039     case POSTDECREMENT_EXPR:
4040     case PREDECREMENT_EXPR:
4041     case MODIFY_EXPR:
4042       ref1 = 1;
4043       break;
4044
4045 /* 24There also exist candidate operator functions of the form
4046              bool    operator!(bool);
4047              bool    operator&&(bool, bool);
4048              bool    operator||(bool, bool);  */
4049
4050     case TRUTH_NOT_EXPR:
4051       return build_builtin_candidate
4052         (candidates, fnname, boolean_type_node,
4053          NULL_TREE, args, argtypes, flags);
4054
4055     case TRUTH_ORIF_EXPR:
4056     case TRUTH_ANDIF_EXPR:
4057       return build_builtin_candidate
4058         (candidates, fnname, boolean_type_node,
4059          boolean_type_node, args, argtypes, flags);
4060
4061     case ADDR_EXPR:
4062     case COMPOUND_EXPR:
4063     case COMPONENT_REF:
4064       return candidates;
4065
4066     default:
4067       ref1 = 0;
4068     }
4069
4070   types[0] = types[1] = NULL_TREE;
4071
4072   for (i = 0; i < 2; ++i)
4073     {
4074       if (! args[i])
4075         ;
4076       else if (IS_AGGR_TYPE (argtypes[i]))
4077         {
4078           tree convs = lookup_conversions (argtypes[i]);
4079
4080           if (code == COND_EXPR)
4081             {
4082               if (real_lvalue_p (args[i]))
4083                 types[i] = scratch_tree_cons
4084                   (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
4085
4086               types[i] = scratch_tree_cons
4087                 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
4088             }
4089                 
4090           else if (! convs || (i == 0 && code == MODIFY_EXPR
4091                                && code2 == NOP_EXPR))
4092             return candidates;
4093
4094           for (; convs; convs = TREE_CHAIN (convs))
4095             {
4096               type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
4097
4098               if (i == 0 && ref1
4099                   && (TREE_CODE (type) != REFERENCE_TYPE
4100                       || TYPE_READONLY (TREE_TYPE (type))))
4101                 continue;
4102
4103               if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
4104                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4105
4106               type = non_reference (type);
4107               if (i != 0 || ! ref1)
4108                 {
4109                   type = TYPE_MAIN_VARIANT (type_decays_to (type));
4110                   if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
4111                     types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4112                   if (INTEGRAL_TYPE_P (type))
4113                     type = type_promotes_to (type);
4114                 }
4115
4116               if (! value_member (type, types[i]))
4117                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4118             }
4119         }
4120       else
4121         {
4122           if (code == COND_EXPR && real_lvalue_p (args[i]))
4123             types[i] = scratch_tree_cons
4124               (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
4125           type = non_reference (argtypes[i]);
4126           if (i != 0 || ! ref1)
4127             {
4128               type = TYPE_MAIN_VARIANT (type_decays_to (type));
4129               if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
4130                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4131               if (INTEGRAL_TYPE_P (type))
4132                 type = type_promotes_to (type);
4133             }
4134           types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4135         }
4136     }
4137
4138   for (; types[0]; types[0] = TREE_CHAIN (types[0]))
4139     {
4140       if (types[1])
4141         for (type = types[1]; type; type = TREE_CHAIN (type))
4142           candidates = add_builtin_candidate
4143             (candidates, code, code2, fnname, TREE_VALUE (types[0]),
4144              TREE_VALUE (type), args, argtypes, flags);
4145       else
4146         candidates = add_builtin_candidate
4147           (candidates, code, code2, fnname, TREE_VALUE (types[0]),
4148            NULL_TREE, args, argtypes, flags);
4149     }
4150
4151   return candidates;
4152 }
4153
4154
4155 /* If TMPL can be successfully instantiated as indicated by
4156    EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
4157
4158    TMPL is the template.  EXPLICIT_TARGS are any explicit template
4159    arguments.  ARGLIST is the arguments provided at the call-site.
4160    The RETURN_TYPE is the desired type for conversion operators.  If
4161    OBJ is NULL_TREE, FLAGS are as for add_function_candidate.  If an
4162    OBJ is supplied, FLAGS are ignored, and OBJ is as for
4163    add_conv_candidate.  */
4164
4165 static struct z_candidate*
4166 add_template_candidate_real (candidates, tmpl, explicit_targs,
4167                              arglist, return_type, flags,
4168                              obj)
4169      struct z_candidate *candidates;
4170      tree tmpl, explicit_targs, arglist, return_type;
4171      int flags;
4172      tree obj;
4173 {
4174   int ntparms = DECL_NTPARMS (tmpl);
4175   tree targs = make_scratch_vec (ntparms);
4176   struct z_candidate *cand;
4177   int i;
4178   tree fn;
4179
4180   i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
4181                            return_type, 0, NULL_TREE); 
4182
4183   if (i != 0)
4184     return candidates;
4185
4186   fn = instantiate_template (tmpl, targs);
4187   if (fn == error_mark_node)
4188     return candidates;
4189
4190   if (obj != NULL_TREE)
4191     /* Aha, this is a conversion function.  */
4192     cand = add_conv_candidate (candidates, fn, obj, arglist);
4193   else
4194     cand = add_function_candidate (candidates, fn, arglist, flags);
4195   if (DECL_TI_TEMPLATE (fn) != tmpl)
4196     /* This situation can occur if a member template of a template
4197        class is specialized.  Then, instantiate_template might return
4198        an instantiation of the specialization, in which case the
4199        DECL_TI_TEMPLATE field will point at the original
4200        specialization.  For example:
4201
4202          template <class T> struct S { template <class U> void f(U);
4203                                        template <> void f(int) {}; };
4204          S<double> sd;
4205          sd.f(3);
4206
4207        Here, TMPL will be template <class U> S<double>::f(U).
4208        And, instantiate template will give us the specialization
4209        template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
4210        for this will point at template <class T> template <> S<T>::f(int),
4211        so that we can find the definition.  For the purposes of
4212        overload resolution, however, we want the original TMPL.  */
4213     cand->template = tree_cons (tmpl, targs, NULL_TREE);
4214   else
4215     cand->template = DECL_TEMPLATE_INFO (fn);
4216
4217   return cand;
4218 }
4219
4220
4221 static struct z_candidate *
4222 add_template_candidate (candidates, tmpl, explicit_targs, 
4223                         arglist, return_type, flags)
4224      struct z_candidate *candidates;
4225      tree tmpl, explicit_targs, arglist, return_type;
4226      int flags;
4227 {
4228   return 
4229     add_template_candidate_real (candidates, tmpl, explicit_targs,
4230                                  arglist, return_type, flags, NULL_TREE);
4231 }
4232
4233
4234 static struct z_candidate *
4235 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
4236      struct z_candidate *candidates;
4237      tree tmpl, obj, arglist, return_type;
4238 {
4239   return 
4240     add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
4241                                  return_type, 0, obj);
4242 }
4243
4244
4245 static int
4246 any_viable (cands)
4247      struct z_candidate *cands;
4248 {
4249   for (; cands; cands = cands->next)
4250     if (pedantic ? cands->viable == 1 : cands->viable)
4251       return 1;
4252   return 0;
4253 }
4254
4255 static struct z_candidate *
4256 splice_viable (cands)
4257      struct z_candidate *cands;
4258 {
4259   struct z_candidate **p = &cands;
4260
4261   for (; *p; )
4262     {
4263       if (pedantic ? (*p)->viable == 1 : (*p)->viable)
4264         p = &((*p)->next);
4265       else
4266         *p = (*p)->next;
4267     }
4268
4269   return cands;
4270 }
4271
4272 static tree
4273 build_this (obj)
4274      tree obj;
4275 {
4276   /* Fix this to work on non-lvalues.  */
4277   if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
4278       || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
4279     return obj;
4280   else
4281     return build_unary_op (ADDR_EXPR, obj, 0);
4282 }
4283
4284 static void
4285 print_z_candidates (candidates)
4286      struct z_candidate *candidates;
4287 {
4288   char *str = "candidates are:";
4289   for (; candidates; candidates = candidates->next)
4290     {
4291       if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
4292         {
4293           if (candidates->fn == ansi_opname [COND_EXPR])
4294             cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
4295                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
4296                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
4297                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
4298           else if (TREE_VEC_LENGTH (candidates->convs) == 2)
4299             cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
4300                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
4301                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
4302           else
4303             cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
4304                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
4305         }
4306       else
4307         cp_error_at ("%s %+D%s", str, candidates->fn,
4308                      candidates->viable == -1 ? " <near match>" : "");
4309       str = "               "; 
4310     }
4311 }
4312
4313 /* Returns the best overload candidate to perform the requested
4314    conversion.  This function is used for three the overloading situations
4315    described in [over.match.copy], [over.match.conv], and [over.match.ref].
4316    If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
4317    per [dcl.init.ref], so we ignore temporary bindings.  */
4318
4319 static struct z_candidate *
4320 build_user_type_conversion_1 (totype, expr, flags)
4321      tree totype, expr;
4322      int flags;
4323 {
4324   struct z_candidate *candidates, *cand;
4325   tree fromtype = TREE_TYPE (expr);
4326   tree ctors = NULL_TREE, convs = NULL_TREE, *p;
4327   tree args = NULL_TREE;
4328   tree templates = NULL_TREE;
4329
4330   if (IS_AGGR_TYPE (totype))
4331     ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
4332   if (IS_AGGR_TYPE (fromtype)
4333       && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
4334     convs = lookup_conversions (fromtype);
4335
4336   candidates = 0;
4337   flags |= LOOKUP_NO_CONVERSION;
4338
4339   if (ctors)
4340     {
4341       tree t = build_int_2 (0, 0);
4342       TREE_TYPE (t) = build_pointer_type (totype);
4343       args = build_scratch_list (NULL_TREE, expr);
4344       if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
4345         args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
4346       args = scratch_tree_cons (NULL_TREE, t, args);
4347
4348       ctors = TREE_VALUE (ctors);
4349     }
4350   for (; ctors; ctors = DECL_CHAIN (ctors))
4351     {
4352       if (DECL_NONCONVERTING_P (ctors))
4353         continue;
4354
4355       if (TREE_CODE (ctors) == TEMPLATE_DECL) 
4356         {
4357           templates = scratch_tree_cons (NULL_TREE, ctors, templates);
4358           candidates = 
4359             add_template_candidate (candidates, ctors,
4360                                     NULL_TREE, args, NULL_TREE, flags);
4361         } 
4362       else 
4363         candidates = add_function_candidate (candidates, ctors,
4364                                              args, flags); 
4365
4366       if (candidates) 
4367         {
4368           candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
4369           candidates->basetype_path = TYPE_BINFO (totype);
4370         } 
4371     }
4372
4373   if (convs)
4374     args = build_scratch_list (NULL_TREE, build_this (expr));
4375
4376   for (; convs; convs = TREE_CHAIN (convs))
4377     {
4378       tree fn = TREE_VALUE (convs);
4379       int convflags = LOOKUP_NO_CONVERSION;
4380       tree ics;
4381
4382       /* If we are called to convert to a reference type, we are trying to
4383          find an lvalue binding, so don't even consider temporaries.  If
4384          we don't find an lvalue binding, the caller will try again to
4385          look for a temporary binding.  */
4386       if (TREE_CODE (totype) == REFERENCE_TYPE)
4387         convflags |= LOOKUP_NO_TEMP_BIND;
4388
4389       if (TREE_CODE (fn) != TEMPLATE_DECL)
4390         ics = implicit_conversion
4391           (totype, TREE_TYPE (TREE_TYPE (fn)), 0, convflags);
4392       else
4393         /* Here, the template conversion operator result must
4394            precisely match the TOTYPE.  (FIXME: Actually, we're
4395            supposed to do some simple conversions here; see
4396            [temp.deduct.conv].).  If the result of the conversion
4397            operator is not actually TOTYPE, then
4398            add_template_candidate will fail below.  */
4399         ics = implicit_conversion (totype, totype, 0, convflags);
4400
4401       if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
4402         /* ignore the near match.  */;
4403       else if (ics)
4404         for (; fn; fn = DECL_CHAIN (fn))
4405           {
4406             if (TREE_CODE (fn) == TEMPLATE_DECL)
4407               {
4408                 templates = scratch_tree_cons (NULL_TREE, fn, templates);
4409                 candidates = 
4410                   add_template_candidate (candidates, fn, NULL_TREE,
4411                                           args, totype, flags);
4412               } 
4413             else 
4414               candidates = add_function_candidate (candidates, fn,
4415                                                    args, flags); 
4416
4417             if (candidates) 
4418               {
4419                 candidates->second_conv = ics;
4420                 candidates->basetype_path = TREE_PURPOSE (convs);
4421                 if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
4422                   candidates->viable = -1;
4423               }
4424           }
4425     }
4426
4427   if (! any_viable (candidates))
4428     {
4429 #if 0
4430       if (flags & LOOKUP_COMPLAIN)
4431         {
4432           if (candidates && ! candidates->next)
4433             /* say why this one won't work or try to be loose */;
4434           else
4435             cp_error ("no viable candidates");
4436         }
4437 #endif
4438
4439       return 0;
4440     }
4441
4442   candidates = splice_viable (candidates);
4443   cand = tourney (candidates);
4444
4445   if (cand == 0)
4446     {
4447       if (flags & LOOKUP_COMPLAIN)
4448         {
4449           cp_error ("conversion from `%T' to `%T' is ambiguous",
4450                     fromtype, totype);
4451           print_z_candidates (candidates);
4452         }
4453
4454       cand = candidates;        /* any one will do */
4455       cand->second_conv = build1 (AMBIG_CONV, totype, expr);
4456       ICS_USER_FLAG (cand->second_conv) = 1;
4457       ICS_BAD_FLAG (cand->second_conv) = 1;
4458
4459       return cand;
4460     }
4461
4462   for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
4463     p = &(TREE_OPERAND (*p, 0));
4464
4465   /* Pedantically, normal function declarations are never considered
4466      to refer to template instantiations, so we only do this with
4467      -fguiding-decls.  */ 
4468   if (flag_guiding_decls && templates && ! cand->template 
4469       && !DECL_INITIAL (cand->fn) 
4470       && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
4471     add_maybe_template (cand->fn, templates);
4472
4473   *p = build
4474     (USER_CONV,
4475      (DECL_CONSTRUCTOR_P (cand->fn)
4476       ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
4477      expr, cand->fn, cand->convs, cand->basetype_path);
4478   ICS_USER_FLAG (cand->second_conv) = 1;
4479   if (cand->viable == -1)
4480     ICS_BAD_FLAG (cand->second_conv) = 1;
4481
4482   return cand;
4483 }
4484
4485 tree
4486 build_user_type_conversion (totype, expr, flags)
4487      tree totype, expr;
4488      int flags;
4489 {
4490   struct z_candidate *cand
4491     = build_user_type_conversion_1 (totype, expr, flags);
4492
4493   if (cand)
4494     {
4495       if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
4496         return error_mark_node;
4497       return convert_from_reference (convert_like (cand->second_conv, expr));
4498     }
4499   return NULL_TREE;
4500 }
4501
4502 /* Do any initial processing on the arguments to a function call.  */
4503
4504 static tree
4505 resolve_args (args)
4506      tree args;
4507 {
4508   tree t;
4509   for (t = args; t; t = TREE_CHAIN (t))
4510     {
4511       if (TREE_VALUE (t) == error_mark_node)
4512         return error_mark_node;
4513       else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
4514         {
4515           error ("invalid use of void expression");
4516           return error_mark_node;
4517         }
4518       else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
4519         TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
4520     }
4521   return args;
4522 }
4523       
4524 tree
4525 build_new_function_call (fn, args)
4526      tree fn, args;
4527 {
4528   struct z_candidate *candidates = 0, *cand;
4529   tree explicit_targs = NULL_TREE;
4530   int template_only = 0;
4531
4532   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4533     {
4534       explicit_targs = TREE_OPERAND (fn, 1);
4535       fn = TREE_OPERAND (fn, 0);
4536       template_only = 1;
4537     }
4538
4539   if (really_overloaded_fn (fn))
4540     {
4541       tree t;
4542       tree templates = NULL_TREE;
4543
4544       args = resolve_args (args);
4545
4546       if (args == error_mark_node)
4547         return error_mark_node;
4548
4549       for (t = TREE_VALUE (fn); t; t = DECL_CHAIN (t))
4550         {
4551           if (TREE_CODE (t) == TEMPLATE_DECL)
4552             {
4553               templates = scratch_tree_cons (NULL_TREE, t, templates);
4554               candidates = add_template_candidate
4555                 (candidates, t, explicit_targs, args, NULL_TREE,
4556                  LOOKUP_NORMAL);  
4557             }
4558           else if (! template_only)
4559             candidates = add_function_candidate
4560               (candidates, t, args, LOOKUP_NORMAL);
4561         }
4562
4563       if (! any_viable (candidates))
4564         {
4565           if (candidates && ! candidates->next)
4566             return build_function_call (candidates->fn, args);
4567           cp_error ("no matching function for call to `%D (%A)'",
4568                     TREE_PURPOSE (fn), args);
4569           if (candidates)
4570             print_z_candidates (candidates);
4571           return error_mark_node;
4572         }
4573       candidates = splice_viable (candidates);
4574       cand = tourney (candidates);
4575
4576       if (cand == 0)
4577         {
4578           cp_error ("call of overloaded `%D (%A)' is ambiguous",
4579                     TREE_PURPOSE (fn), args);
4580           print_z_candidates (candidates);
4581           return error_mark_node;
4582         }
4583
4584       /* Pedantically, normal function declarations are never considered
4585          to refer to template instantiations, so we only do this with
4586          -fguiding-decls.  */
4587       if (flag_guiding_decls && templates && ! cand->template 
4588           && ! DECL_INITIAL (cand->fn))
4589         add_maybe_template (cand->fn, templates);
4590
4591       return build_over_call (cand->fn, cand->convs, args, LOOKUP_NORMAL);
4592     }
4593
4594   return build_function_call (fn, args);
4595 }
4596
4597 static tree
4598 build_object_call (obj, args)
4599      tree obj, args;
4600 {
4601   struct z_candidate *candidates = 0, *cand;
4602   tree fns, convs, mem_args = NULL_TREE;
4603   tree type = TREE_TYPE (obj);
4604   tree templates = NULL_TREE;
4605
4606   fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 0);
4607
4608   args = resolve_args (args);
4609
4610   if (args == error_mark_node)
4611     return error_mark_node;
4612
4613   if (fns)
4614     {
4615       tree fn = TREE_VALUE (fns);
4616       mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
4617
4618       for (; fn; fn = DECL_CHAIN (fn))
4619         {
4620           if (TREE_CODE (fn) == TEMPLATE_DECL)
4621             {
4622               templates = scratch_tree_cons (NULL_TREE, fn, templates);
4623               candidates 
4624                 = add_template_candidate (candidates, fn, NULL_TREE,
4625                                           mem_args, NULL_TREE, 
4626                                           LOOKUP_NORMAL);
4627             }
4628           else
4629             candidates = add_function_candidate
4630               (candidates, fn, mem_args, LOOKUP_NORMAL);
4631
4632           if (candidates)
4633             candidates->basetype_path = TREE_PURPOSE (fns);
4634         }
4635     }
4636
4637   convs = lookup_conversions (type);
4638
4639   for (; convs; convs = TREE_CHAIN (convs))
4640     {
4641       tree fn = TREE_VALUE (convs);
4642       tree totype = TREE_TYPE (TREE_TYPE (fn));
4643
4644       if (TREE_CODE (totype) == POINTER_TYPE
4645           && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
4646         for (; fn; fn = DECL_CHAIN (fn))
4647           {
4648             if (TREE_CODE (fn) == TEMPLATE_DECL) 
4649               {
4650                 templates = scratch_tree_cons (NULL_TREE, fn, templates);
4651                 candidates = add_template_conv_candidate (candidates,
4652                                                           fn,
4653                                                           obj,
4654                                                           args,
4655                                                           totype);
4656               }
4657             else
4658               candidates = add_conv_candidate (candidates, fn, obj, args);
4659
4660             if (candidates)
4661               candidates->basetype_path = TREE_PURPOSE (convs);
4662           }
4663     }
4664
4665   if (! any_viable (candidates))
4666     {
4667       cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
4668       print_z_candidates (candidates);
4669       return error_mark_node;
4670     }
4671
4672   candidates = splice_viable (candidates);
4673   cand = tourney (candidates);
4674
4675   if (cand == 0)
4676     {
4677       cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
4678       print_z_candidates (candidates);
4679       return error_mark_node;
4680     }
4681
4682   if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
4683     return build_over_call (cand->fn, cand->convs, mem_args, LOOKUP_NORMAL);
4684
4685   obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
4686
4687   /* FIXME */
4688   return build_function_call (obj, args);
4689 }
4690
4691 static void
4692 op_error (code, code2, arg1, arg2, arg3, problem)
4693      enum tree_code code, code2;
4694      tree arg1, arg2, arg3;
4695      char *problem;
4696 {
4697   char * opname
4698     = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
4699
4700   switch (code)
4701     {
4702     case COND_EXPR:
4703       cp_error ("%s for `%T ? %T : %T'", problem,
4704                 error_type (arg1), error_type (arg2), error_type (arg3));
4705       break;
4706     case POSTINCREMENT_EXPR:
4707     case POSTDECREMENT_EXPR:
4708       cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
4709       break;
4710     case ARRAY_REF:
4711       cp_error ("%s for `%T[%T]'", problem,
4712                 error_type (arg1), error_type (arg2));
4713       break;
4714     default:
4715       if (arg2)
4716         cp_error ("%s for `%T %s %T'", problem,
4717                   error_type (arg1), opname, error_type (arg2));
4718       else
4719         cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
4720     }
4721 }
4722
4723 tree
4724 build_new_op (code, flags, arg1, arg2, arg3)
4725      enum tree_code code;
4726      int flags;
4727      tree arg1, arg2, arg3;
4728 {
4729   struct z_candidate *candidates = 0, *cand;
4730   tree fns, mem_arglist = NULL_TREE, arglist, fnname;
4731   enum tree_code code2 = NOP_EXPR;
4732   tree templates = NULL_TREE;
4733   tree conv;
4734
4735   if (arg1 == error_mark_node
4736       || arg2 == error_mark_node
4737       || arg3 == error_mark_node)
4738     return error_mark_node;
4739
4740   /* This can happen if a template takes all non-type parameters, e.g.
4741      undeclared_template<1, 5, 72>a;  */
4742   if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
4743     {
4744       cp_error ("`%D' must be declared before use", arg1);
4745       return error_mark_node;
4746     }
4747
4748   if (code == MODIFY_EXPR)
4749     {
4750       code2 = TREE_CODE (arg3);
4751       arg3 = NULL_TREE;
4752       fnname = ansi_assopname[code2];
4753     }
4754   else
4755     fnname = ansi_opname[code];
4756
4757   switch (code)
4758     {
4759     case NEW_EXPR:
4760     case VEC_NEW_EXPR:
4761       {
4762         tree rval;
4763
4764         arglist = scratch_tree_cons (NULL_TREE, arg2, arg3);
4765         if (flags & LOOKUP_GLOBAL)
4766           return build_new_function_call
4767             (lookup_name_nonclass (fnname), arglist);
4768
4769         /* FIXME */
4770         rval = build_method_call
4771           (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
4772                                "new"),
4773            fnname, arglist, NULL_TREE, flags);
4774         if (rval == error_mark_node)
4775           /* User might declare fancy operator new, but invoke it
4776              like standard one.  */
4777           return rval;
4778
4779         TREE_TYPE (rval) = arg1;
4780         return rval;
4781       }
4782
4783     case VEC_DELETE_EXPR:
4784     case DELETE_EXPR:
4785       {
4786         tree rval;
4787
4788         if (flags & LOOKUP_GLOBAL)
4789           return build_new_function_call
4790             (lookup_name_nonclass (fnname),
4791              build_scratch_list (NULL_TREE, arg1));
4792
4793         arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
4794
4795         arg1 = TREE_TYPE (arg1);
4796
4797         /* This handles the case where we're trying to delete
4798            X (*a)[10];
4799            a=new X[5][10];
4800            delete[] a; */
4801            
4802         if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
4803           {
4804             /* Strip off the pointer and the array.  */
4805             arg1 = TREE_TYPE (TREE_TYPE (arg1));
4806
4807             while (TREE_CODE (arg1) == ARRAY_TYPE)
4808                 arg1 = (TREE_TYPE (arg1));
4809
4810             arg1 = build_pointer_type (arg1);
4811           }
4812
4813         /* FIXME */
4814         rval = build_method_call
4815           (build_indirect_ref (build1 (NOP_EXPR, arg1,
4816                                        error_mark_node),
4817                                NULL_PTR),
4818            fnname, arglist, NULL_TREE, flags);
4819 #if 0
4820         /* This can happen when operator delete is protected.  */
4821         my_friendly_assert (rval != error_mark_node, 250);
4822         TREE_TYPE (rval) = void_type_node;
4823 #endif
4824         return rval;
4825       }
4826
4827     case CALL_EXPR:
4828       return build_object_call (arg1, arg2);
4829
4830     default:
4831       break;
4832     }
4833
4834   /* The comma operator can have void args.  */
4835   if (TREE_CODE (arg1) == OFFSET_REF)
4836     arg1 = resolve_offset_ref (arg1);
4837   if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
4838     arg2 = resolve_offset_ref (arg2);
4839   if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
4840     arg3 = resolve_offset_ref (arg3);
4841
4842   if (code == COND_EXPR)
4843     {
4844       if (arg2 == NULL_TREE
4845           || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
4846           || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
4847           || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
4848               && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
4849         goto builtin;
4850     }
4851   else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
4852            && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
4853     goto builtin;
4854
4855   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
4856     arg2 = integer_zero_node;
4857
4858   if (arg2 && arg3)
4859     arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
4860                       (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
4861   else if (arg2)
4862     arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
4863   else
4864     arglist = build_scratch_list (NULL_TREE, arg1);
4865
4866   fns = lookup_name_nonclass (fnname);
4867   /* + Koenig lookup */
4868
4869   if (fns && TREE_CODE (fns) == TREE_LIST)
4870     fns = TREE_VALUE (fns);
4871   for (; fns; fns = DECL_CHAIN (fns))
4872     {
4873       if (TREE_CODE (fns) == TEMPLATE_DECL)
4874         {
4875           templates = scratch_tree_cons (NULL_TREE, fns, templates);
4876           candidates 
4877             = add_template_candidate (candidates, fns, NULL_TREE,
4878                                       arglist, TREE_TYPE (fnname),
4879                                       flags); 
4880         }
4881       else
4882         candidates = add_function_candidate (candidates, fns, arglist, flags);
4883     }
4884
4885   if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
4886     fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 0);
4887   else
4888     fns = NULL_TREE;
4889
4890   if (fns)
4891     {
4892       tree fn = TREE_VALUE (fns);
4893       mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
4894       for (; fn; fn = DECL_CHAIN (fn))
4895         {
4896           tree this_arglist;
4897
4898           if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4899             this_arglist = mem_arglist;
4900           else
4901             this_arglist = arglist;
4902
4903           if (TREE_CODE (fn) == TEMPLATE_DECL)
4904             {
4905               /* A member template. */
4906               templates = scratch_tree_cons (NULL_TREE, fn, templates);
4907               candidates 
4908                 = add_template_candidate (candidates, fn, NULL_TREE,
4909                                           this_arglist,  TREE_TYPE (fnname),
4910                                           flags); 
4911             }
4912           else
4913             candidates = add_function_candidate
4914               (candidates, fn, this_arglist, flags);
4915
4916           if (candidates) 
4917             candidates->basetype_path = TREE_PURPOSE (fns);
4918         }
4919     }
4920
4921   {
4922     tree args[3];
4923
4924     /* Rearrange the arguments for ?: so that add_builtin_candidate only has
4925        to know about two args; a builtin candidate will always have a first
4926        parameter of type bool.  We'll handle that in
4927        build_builtin_candidate.  */
4928     if (code == COND_EXPR)
4929       {
4930         args[0] = arg2;
4931         args[1] = arg3;
4932         args[2] = arg1;
4933       }
4934     else
4935       {
4936         args[0] = arg1;
4937         args[1] = arg2;
4938         args[2] = NULL_TREE;
4939       }
4940
4941     candidates = add_builtin_candidates
4942       (candidates, code, code2, fnname, args, flags);
4943   }
4944
4945   if (! any_viable (candidates))
4946     {
4947       switch (code)
4948         {
4949         case POSTINCREMENT_EXPR:
4950         case POSTDECREMENT_EXPR:
4951           /* Look for an `operator++ (int)'.  If they didn't have
4952              one, then we fall back to the old way of doing things.  */
4953           if (flags & LOOKUP_COMPLAIN)
4954             cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
4955                         fnname, opname_tab [code]);
4956           if (code == POSTINCREMENT_EXPR)
4957             code = PREINCREMENT_EXPR;
4958           else
4959             code = PREDECREMENT_EXPR;   
4960           return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
4961           
4962           /* The caller will deal with these.  */
4963         case ADDR_EXPR:
4964         case COMPOUND_EXPR:
4965         case COMPONENT_REF:
4966           return NULL_TREE;
4967
4968         default:
4969           break;
4970         }
4971       if (flags & LOOKUP_COMPLAIN)
4972         {
4973           op_error (code, code2, arg1, arg2, arg3, "no match");
4974           print_z_candidates (candidates);
4975         }
4976       return error_mark_node;
4977     }
4978   candidates = splice_viable (candidates);
4979   cand = tourney (candidates);
4980
4981   if (cand == 0)
4982     {
4983       if (flags & LOOKUP_COMPLAIN)
4984         {
4985           op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
4986           print_z_candidates (candidates);
4987         }
4988       return error_mark_node;
4989     }
4990
4991   if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4992     {
4993       extern int warn_synth;
4994       if (warn_synth
4995           && fnname == ansi_opname[MODIFY_EXPR]
4996           && DECL_ARTIFICIAL (cand->fn)
4997           && candidates->next
4998           && ! candidates->next->next)
4999         {
5000           cp_warning ("using synthesized `%#D' for copy assignment",
5001                       cand->fn);
5002           cp_warning_at ("  where cfront would use `%#D'",
5003                          cand == candidates
5004                          ? candidates->next->fn
5005                          : candidates->fn);
5006         }
5007
5008       if (DECL_FUNCTION_MEMBER_P (cand->fn))
5009         enforce_access (cand->basetype_path, cand->fn);
5010
5011       /* Pedantically, normal function declarations are never considered
5012          to refer to template instantiations, so we only do this with
5013          -fguiding-decls.  */ 
5014       if (flag_guiding_decls && templates && ! cand->template 
5015           && ! DECL_INITIAL (cand->fn)
5016           && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
5017         add_maybe_template (cand->fn, templates);
5018
5019       return build_over_call
5020         (cand->fn, cand->convs,
5021          TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5022          ? mem_arglist : arglist,
5023          LOOKUP_NORMAL);
5024     }
5025
5026   /* Check for comparison of different enum types.  */
5027   switch (code)
5028     {
5029     case GT_EXPR:
5030     case LT_EXPR:
5031     case GE_EXPR:
5032     case LE_EXPR:
5033     case EQ_EXPR:
5034     case NE_EXPR:
5035       if (flag_int_enum_equivalence == 0 
5036           && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 
5037           && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 
5038           && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5039               != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
5040         {
5041           cp_warning ("comparison between `%#T' and `%#T'", 
5042                       TREE_TYPE (arg1), TREE_TYPE (arg2));
5043         }
5044       break;
5045     default:
5046       break;
5047     }
5048
5049   /* We need to strip any leading REF_BIND so that bitfields don't cause
5050      errors.  This should not remove any important conversions, because
5051      builtins don't apply to class objects directly.  */
5052   conv = TREE_VEC_ELT (cand->convs, 0);
5053   if (TREE_CODE (conv) == REF_BIND)
5054     conv = TREE_OPERAND (conv, 0);
5055   arg1 = convert_like (conv, arg1);
5056   if (arg2)
5057     arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
5058   if (arg3)
5059     arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
5060
5061 builtin:
5062   switch (code)
5063     {
5064     case MODIFY_EXPR:
5065       return build_modify_expr (arg1, code2, arg2);
5066
5067     case INDIRECT_REF:
5068       return build_indirect_ref (arg1, "unary *");
5069
5070     case PLUS_EXPR:
5071     case MINUS_EXPR:
5072     case MULT_EXPR:
5073     case TRUNC_DIV_EXPR:
5074     case GT_EXPR:
5075     case LT_EXPR:
5076     case GE_EXPR:
5077     case LE_EXPR:
5078     case EQ_EXPR:
5079     case NE_EXPR:
5080     case MAX_EXPR:
5081     case MIN_EXPR:
5082     case LSHIFT_EXPR:
5083     case RSHIFT_EXPR:
5084     case TRUNC_MOD_EXPR:
5085     case BIT_AND_EXPR:
5086     case BIT_IOR_EXPR:
5087     case BIT_XOR_EXPR:
5088     case TRUTH_ANDIF_EXPR:
5089     case TRUTH_ORIF_EXPR:
5090       return build_binary_op_nodefault (code, arg1, arg2, code);
5091
5092     case CONVERT_EXPR:
5093     case NEGATE_EXPR:
5094     case BIT_NOT_EXPR:
5095     case TRUTH_NOT_EXPR:
5096     case PREINCREMENT_EXPR:
5097     case POSTINCREMENT_EXPR:
5098     case PREDECREMENT_EXPR:
5099     case POSTDECREMENT_EXPR:
5100     case REALPART_EXPR:
5101     case IMAGPART_EXPR:
5102       return build_unary_op (code, arg1, candidates != 0);
5103
5104     case ARRAY_REF:
5105       return build_array_ref (arg1, arg2);
5106
5107     case COND_EXPR:
5108       return build_conditional_expr (arg1, arg2, arg3);
5109
5110     case MEMBER_REF:
5111       return build_m_component_ref
5112         (build_indirect_ref (arg1, NULL_PTR), arg2);
5113
5114       /* The caller will deal with these.  */
5115     case ADDR_EXPR:
5116     case COMPONENT_REF:
5117     case COMPOUND_EXPR:
5118       return NULL_TREE;
5119
5120     default:
5121       my_friendly_abort (367);
5122       return NULL_TREE;
5123     }
5124 }
5125
5126 /* Build up a call to operator new.  This has to be handled differently
5127    from other operators in the way lookup is handled; first members are
5128    considered, then globals.  CODE is either NEW_EXPR or VEC_NEW_EXPR.
5129    TYPE is the type to be created.  ARGS are any new-placement args.
5130    FLAGS are the usual overloading flags.  */
5131
5132 tree
5133 build_op_new_call (code, type, args, flags)
5134      enum tree_code code;
5135      tree type, args;
5136      int flags;
5137 {
5138   tree fnname = ansi_opname[code];
5139
5140   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
5141       && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
5142     {
5143       tree dummy = build1 (NOP_EXPR, build_pointer_type (type),
5144                            error_mark_node);
5145       dummy = build_indirect_ref (dummy, "new");
5146       return build_method_call (dummy, fnname, args, NULL_TREE, flags);
5147     }
5148   else
5149     return build_new_function_call (lookup_name_nonclass (fnname), args);
5150 }
5151
5152 /* Build a call to operator delete.  This has to be handled very specially,
5153    because the restrictions on what signatures match are different from all
5154    other call instances.  For a normal delete, only a delete taking (void *)
5155    or (void *, size_t) is accepted.  For a placement delete, only an exact
5156    match with the placement new is accepted.
5157
5158    CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
5159    ADDR is the pointer to be deleted.  For placement delete, it is also
5160      used to determine what the corresponding new looked like.
5161    SIZE is the size of the memory block to be deleted.
5162    FLAGS are the usual overloading flags.  */
5163
5164 tree
5165 build_op_delete_call (code, addr, size, flags)
5166      enum tree_code code;
5167      tree addr, size;
5168      int flags;
5169 {
5170   tree fn, fns, fnname, fntype, argtypes, args, type;
5171   int placement;
5172
5173   if (addr == error_mark_node)
5174     return error_mark_node;
5175
5176   type = TREE_TYPE (TREE_TYPE (addr));
5177   fnname = ansi_opname[code];
5178
5179   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
5180     fns = lookup_fnfields (TYPE_BINFO (type), fnname, 0);
5181   else
5182     fns = NULL_TREE;
5183
5184   if (fns)
5185     {
5186       /* Build this up like build_offset_ref does.  */
5187       fns = build_tree_list (error_mark_node, fns);
5188       TREE_TYPE (fns) = build_offset_type (type, unknown_type_node);
5189     }
5190   else
5191     fns = lookup_name_nonclass (fnname);
5192
5193   /* We can recognize a placement delete because of LOOKUP_SPECULATIVELY;
5194      if we are doing placement delete we do nothing if we don't find a
5195      matching op delete.  */
5196   placement = !!(flags & LOOKUP_SPECULATIVELY);
5197   if (placement)
5198     {
5199       /* If placement, we are coming from build_new, and we know that addr
5200          is the allocation expression, so extract the info we need from it.
5201          Obviously, if the build_new process changes this may have to
5202          change as well.  */
5203
5204       /* The NOP_EXPR.  */
5205       tree t = TREE_OPERAND (addr, 1);
5206       /* The CALL_EXPR.  */
5207       t = TREE_OPERAND (t, 0);
5208       /* The function.  */
5209       argtypes = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
5210       /* The second parm type.  */
5211       argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
5212       /* The second argument.  */
5213       args = TREE_CHAIN (TREE_OPERAND (t, 1));
5214
5215       /* Pull the dummy var out of the TARGET_EXPR for use in our call.  */
5216       addr = TREE_OPERAND (addr, 0);
5217     }
5218   else
5219     {
5220       /* First try it without the size argument.  */
5221       argtypes = void_list_node;
5222       args = NULL_TREE;
5223     }
5224
5225   argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
5226   fntype = build_function_type (void_type_node, argtypes);
5227
5228   /* Strip const and volatile from addr.  */
5229   if (type != TYPE_MAIN_VARIANT (type))
5230     addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
5231
5232   /* instantiate_type will always return a plain function; pretend it's
5233      overloaded.  */
5234   if (TREE_CODE (fns) == FUNCTION_DECL)
5235     fns = scratch_tree_cons (NULL_TREE, fns, NULL_TREE);
5236
5237   fn = instantiate_type (fntype, fns, 0);
5238
5239   if (fn != error_mark_node)
5240     {
5241       if (TREE_CODE (TREE_VALUE (fns)) == TREE_LIST)
5242         /* Member functions.  */
5243         enforce_access (TREE_PURPOSE (TREE_VALUE (fns)), fn);
5244       return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
5245     }
5246
5247   if (placement)
5248     return NULL_TREE;
5249
5250   /* Normal delete; now try to find a match including the size argument.  */
5251   argtypes = tree_cons (NULL_TREE, ptr_type_node,
5252                         tree_cons (NULL_TREE, sizetype, void_list_node));
5253   fntype = build_function_type (void_type_node, argtypes);
5254
5255   fn = instantiate_type (fntype, fns, 0);
5256
5257   if (fn != error_mark_node)
5258     return build_function_call
5259       (fn, expr_tree_cons (NULL_TREE, addr,
5260                            build_expr_list (NULL_TREE, size)));
5261
5262   cp_error ("no suitable operator delete for `%T'", type);
5263   return error_mark_node;
5264 }
5265
5266 /* If the current scope isn't allowed to access FUNCTION along
5267    BASETYPE_PATH, give an error.  */
5268
5269 static void
5270 enforce_access (basetype_path, function)
5271      tree basetype_path, function;
5272 {
5273   tree access = compute_access (basetype_path, function);
5274
5275   if (access == access_private_node)
5276     {
5277       cp_error_at ("`%+#D' is %s", function, 
5278                    TREE_PRIVATE (function) ? "private"
5279                    : "from private base class");
5280       error ("within this context");
5281     }
5282   else if (access == access_protected_node)
5283     {
5284       cp_error_at ("`%+#D' %s", function,
5285                    TREE_PROTECTED (function) ? "is protected"
5286                    : "has protected accessibility");
5287       error ("within this context");
5288     }
5289 }
5290
5291 /* Perform the conversions in CONVS on the expression EXPR.  */
5292
5293 static tree
5294 convert_like (convs, expr)
5295      tree convs, expr;
5296 {
5297   if (ICS_BAD_FLAG (convs)
5298       && TREE_CODE (convs) != USER_CONV
5299       && TREE_CODE (convs) != AMBIG_CONV)
5300     {
5301       tree t = convs; 
5302       for (; t; t = TREE_OPERAND (t, 0))
5303         {
5304           if (TREE_CODE (t) == USER_CONV)
5305             {
5306               expr = convert_like (t, expr);
5307               break;
5308             }
5309           else if (TREE_CODE (t) == AMBIG_CONV)
5310             return convert_like (t, expr);
5311           else if (TREE_CODE (t) == IDENTITY_CONV)
5312             break;
5313         }
5314       return convert_for_initialization
5315         (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
5316          "conversion", NULL_TREE, 0);
5317     }
5318
5319   switch (TREE_CODE (convs))
5320     {
5321     case USER_CONV:
5322       {
5323         tree fn = TREE_OPERAND (convs, 1);
5324         tree args;
5325         enforce_access (TREE_OPERAND (convs, 3), fn);
5326
5327         if (DECL_CONSTRUCTOR_P (fn))
5328           {
5329             tree t = build_int_2 (0, 0);
5330             TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
5331
5332             args = build_scratch_list (NULL_TREE, expr);
5333             if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
5334               args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
5335             args = scratch_tree_cons (NULL_TREE, t, args);
5336           }
5337         else
5338           args = build_this (expr);
5339         expr = build_over_call
5340           (TREE_OPERAND (convs, 1), TREE_OPERAND (convs, 2),
5341            args, LOOKUP_NORMAL);
5342
5343         /* If this is a constructor or a function returning an aggr type,
5344            we need to build up a TARGET_EXPR.  */
5345         if (DECL_CONSTRUCTOR_P (fn))
5346           expr = build_cplus_new (TREE_TYPE (convs), expr);
5347
5348         return expr;
5349       }
5350     case IDENTITY_CONV:
5351       if (type_unknown_p (expr))
5352         expr = instantiate_type (TREE_TYPE (convs), expr, 1);
5353       if (TREE_READONLY_DECL_P (expr))
5354         expr = decl_constant_value (expr);
5355       return expr;
5356     case AMBIG_CONV:
5357       /* Call build_user_type_conversion again for the error.  */
5358       return build_user_type_conversion
5359         (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
5360
5361     default:
5362       break;
5363     };
5364
5365   expr = convert_like (TREE_OPERAND (convs, 0), expr);
5366   if (expr == error_mark_node)
5367     return error_mark_node;
5368
5369   switch (TREE_CODE (convs))
5370     {
5371     case RVALUE_CONV:
5372       if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
5373         return expr;
5374       /* else fall through */
5375     case BASE_CONV:
5376       return build_user_type_conversion
5377         (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
5378     case REF_BIND:
5379       return convert_to_reference
5380         (TREE_TYPE (convs), expr,
5381          CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
5382          error_mark_node);
5383     case LVALUE_CONV:
5384       return decay_conversion (expr);
5385
5386     default:
5387       break;
5388     }
5389   return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
5390                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
5391 }
5392
5393 static tree
5394 convert_default_arg (type, arg)
5395      tree type, arg;
5396 {
5397   arg = break_out_target_exprs (arg);
5398
5399   if (TREE_CODE (arg) == CONSTRUCTOR)
5400     {
5401       arg = digest_init (type, arg, 0);
5402       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5403                                         "default argument", 0, 0);
5404     }
5405   else
5406     {
5407       /* This could get clobbered by the following call.  */
5408       if (TREE_HAS_CONSTRUCTOR (arg))
5409         arg = copy_node (arg);
5410
5411       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5412                                         "default argument", 0, 0);
5413 #ifdef PROMOTE_PROTOTYPES
5414       if ((TREE_CODE (type) == INTEGER_TYPE
5415            || TREE_CODE (type) == ENUMERAL_TYPE)
5416           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
5417         arg = default_conversion (arg);
5418 #endif
5419     }
5420
5421   return arg;
5422 }
5423
5424 static tree
5425 build_over_call (fn, convs, args, flags)
5426      tree fn, convs, args;
5427      int flags;
5428 {
5429   tree converted_args = NULL_TREE;
5430   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
5431   tree conv, arg, val;
5432   int i = 0;
5433   int is_method = 0;
5434
5435   if (args && TREE_CODE (args) != TREE_LIST)
5436     args = build_scratch_list (NULL_TREE, args);
5437   arg = args;
5438
5439   /* The implicit parameters to a constructor are not considered by overload
5440      resolution, and must be of the proper type.  */
5441   if (DECL_CONSTRUCTOR_P (fn))
5442     {
5443       converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
5444       arg = TREE_CHAIN (arg);
5445       parm = TREE_CHAIN (parm);
5446       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
5447         {
5448           converted_args = expr_tree_cons
5449             (NULL_TREE, TREE_VALUE (arg), converted_args);
5450           arg = TREE_CHAIN (arg);
5451           parm = TREE_CHAIN (parm);
5452         }
5453     }      
5454   /* Bypass access control for 'this' parameter.  */
5455   else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5456     {
5457       tree parmtype = TREE_VALUE (parm);
5458       tree argtype = TREE_TYPE (TREE_VALUE (arg));
5459       if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
5460         {
5461           int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype))
5462                     < TYPE_VOLATILE (TREE_TYPE (argtype)));
5463           int dc = (TYPE_READONLY (TREE_TYPE (parmtype))
5464                     < TYPE_READONLY (TREE_TYPE (argtype)));
5465           char *p = (dv && dc ? "const and volatile"
5466                               : dc ? "const" : dv ? "volatile" : "");
5467
5468           cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s",
5469                       TREE_TYPE (argtype), fn, p);
5470         }
5471       converted_args = expr_tree_cons
5472         (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
5473          converted_args);
5474       parm = TREE_CHAIN (parm);
5475       arg = TREE_CHAIN (arg);
5476       ++i;
5477       is_method = 1;
5478     }
5479
5480   for (; arg && parm;
5481        parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
5482     {
5483       tree type = TREE_VALUE (parm);
5484
5485       conv = TREE_VEC_ELT (convs, i);
5486       if (ICS_BAD_FLAG (conv))
5487         {
5488           tree t = conv;
5489           val = TREE_VALUE (arg);
5490
5491           for (; t; t = TREE_OPERAND (t, 0))
5492             {
5493               if (TREE_CODE (t) == USER_CONV
5494                   || TREE_CODE (t) == AMBIG_CONV)
5495                 {
5496                   val = convert_like (t, val);
5497                   break;
5498                 }
5499               else if (TREE_CODE (t) == IDENTITY_CONV)
5500                 break;
5501             }
5502           val = convert_for_initialization
5503             (NULL_TREE, type, val, LOOKUP_NORMAL,
5504              "argument passing", fn, i - is_method);
5505         }
5506       else
5507         val = convert_like (conv, TREE_VALUE (arg));
5508
5509 #ifdef PROMOTE_PROTOTYPES
5510       if ((TREE_CODE (type) == INTEGER_TYPE
5511            || TREE_CODE (type) == ENUMERAL_TYPE)
5512           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
5513         val = default_conversion (val);
5514 #endif
5515       converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
5516     }
5517
5518   /* Default arguments */
5519   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
5520     {
5521       tree arg = TREE_PURPOSE (parm);
5522
5523       if (DECL_TEMPLATE_INFO (fn))
5524         /* This came from a template.  Instantiate the default arg here,
5525            not in tsubst.  */
5526         arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
5527       converted_args = expr_tree_cons
5528         (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
5529          converted_args);
5530     }
5531
5532   /* Ellipsis */
5533   for (; arg; arg = TREE_CHAIN (arg))
5534     {
5535       val = TREE_VALUE (arg);
5536
5537       if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
5538           && (TYPE_PRECISION (TREE_TYPE (val))
5539               < TYPE_PRECISION (double_type_node)))
5540         /* Convert `float' to `double'.  */
5541         val = cp_convert (double_type_node, val);
5542       else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
5543                && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
5544         cp_warning ("cannot pass objects of type `%T' through `...'",
5545                     TREE_TYPE (val));
5546       else
5547         /* Convert `short' and `char' to full-size `int'.  */
5548         val = default_conversion (val);
5549
5550       converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
5551     }
5552
5553   converted_args = nreverse (converted_args);
5554
5555   /* Avoid actually calling copy constructors and copy assignment operators,
5556      if possible.  */
5557   if (DECL_CONSTRUCTOR_P (fn)
5558       && TREE_VEC_LENGTH (convs) == 1
5559       && copy_args_p (fn))
5560     {
5561       tree targ;
5562       arg = TREE_CHAIN (converted_args);
5563       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
5564         arg = TREE_CHAIN (arg);
5565       arg = TREE_VALUE (arg);
5566
5567       /* Pull out the real argument, disregarding const-correctness.  */
5568       targ = arg;
5569       while (TREE_CODE (targ) == NOP_EXPR
5570              || TREE_CODE (targ) == NON_LVALUE_EXPR
5571              || TREE_CODE (targ) == CONVERT_EXPR)
5572         targ = TREE_OPERAND (targ, 0);
5573       if (TREE_CODE (targ) == ADDR_EXPR)
5574         {
5575           targ = TREE_OPERAND (targ, 0);
5576           if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
5577                            TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1))
5578             targ = NULL_TREE;
5579         }
5580       else
5581         targ = NULL_TREE;
5582
5583       if (targ)
5584         arg = targ;
5585       else
5586         arg = build_indirect_ref (arg, 0);
5587
5588       /* [class.copy]: the copy constructor is implicitly defined even if
5589          the implementation elided its use.  */
5590       if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
5591         mark_used (fn);
5592
5593       /* If we're creating a temp and we already have one, don't create a
5594          new one.  If we're not creating a temp but we get one, use
5595          INIT_EXPR to collapse the temp into our target.  Otherwise, if the
5596          ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
5597          temp or an INIT_EXPR otherwise.  */
5598       if (integer_zerop (TREE_VALUE (args)))
5599         {
5600           if (! real_lvalue_p (arg))
5601             return arg;
5602           else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
5603             {
5604               val = build (VAR_DECL, DECL_CONTEXT (fn));
5605               layout_decl (val, 0);
5606               val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
5607               TREE_SIDE_EFFECTS (val) = 1;
5608               return val;
5609             }
5610         }
5611       else if (! real_lvalue_p (arg)
5612                || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
5613         {
5614           tree to = stabilize_reference
5615             (build_indirect_ref (TREE_VALUE (args), 0));
5616           val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
5617           TREE_SIDE_EFFECTS (val) = 1;
5618           return build_unary_op (ADDR_EXPR, val, 0);
5619         }
5620     }
5621   else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
5622            && copy_args_p (fn)
5623            && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
5624     {
5625       tree to = stabilize_reference
5626         (build_indirect_ref (TREE_VALUE (converted_args), 0));
5627       arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
5628       val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
5629       TREE_SIDE_EFFECTS (val) = 1;
5630       return val;
5631     }
5632
5633   mark_used (fn);
5634
5635   if (DECL_CONTEXT (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
5636     return build_signature_method_call (fn, converted_args);
5637   else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
5638     {
5639       tree t, *p = &TREE_VALUE (converted_args);
5640       tree binfo = get_binfo
5641         (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
5642       *p = convert_pointer_to_real (binfo, *p);
5643       if (TREE_SIDE_EFFECTS (*p))
5644         *p = save_expr (*p);
5645       t = build_pointer_type (TREE_TYPE (fn));
5646       fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
5647       TREE_TYPE (fn) = t;
5648     }
5649   else if (DECL_INLINE (fn))
5650     fn = inline_conversion (fn);
5651   else
5652     fn = build_addr_func (fn);
5653
5654   /* Recognize certain built-in functions so we can make tree-codes
5655      other than CALL_EXPR.  We do this when it enables fold-const.c
5656      to do something useful.  */
5657
5658   if (TREE_CODE (fn) == ADDR_EXPR
5659       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5660       && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
5661     switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
5662       {
5663       case BUILT_IN_ABS:
5664       case BUILT_IN_LABS:
5665       case BUILT_IN_FABS:
5666         if (converted_args == 0)
5667           return integer_zero_node;
5668         return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
5669       }
5670
5671   fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
5672   if (TREE_TYPE (fn) == void_type_node)
5673     return fn;
5674   fn = require_complete_type (fn);
5675   if (IS_AGGR_TYPE (TREE_TYPE (fn)))
5676     fn = build_cplus_new (TREE_TYPE (fn), fn);
5677   return convert_from_reference (fn);
5678 }
5679
5680 static tree
5681 build_new_method_call (instance, name, args, basetype_path, flags)
5682      tree instance, name, args, basetype_path;
5683      int flags;
5684 {
5685   struct z_candidate *candidates = 0, *cand;
5686   tree explicit_targs = NULL_TREE;
5687   tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
5688   tree pretty_name;
5689   tree user_args = args;
5690   tree templates = NULL_TREE;
5691   int template_only = 0;
5692
5693   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5694     {
5695       explicit_targs = TREE_OPERAND (name, 1);
5696       name = TREE_OPERAND (name, 0);
5697       if (TREE_CODE (name) == TEMPLATE_DECL)
5698         name = DECL_NAME (name);
5699       template_only = 1;
5700     }
5701
5702   /* If there is an extra argument for controlling virtual bases,
5703      remove it for error reporting.  */
5704   if (flags & LOOKUP_HAS_IN_CHARGE)
5705     user_args = TREE_CHAIN (args);
5706
5707   args = resolve_args (args);
5708
5709   if (args == error_mark_node)
5710     return error_mark_node;
5711
5712   if (instance == NULL_TREE)
5713     basetype = BINFO_TYPE (basetype_path);
5714   else
5715     {
5716       if (TREE_CODE (instance) == OFFSET_REF)
5717         instance = resolve_offset_ref (instance);
5718       if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5719         instance = convert_from_reference (instance);
5720       basetype = TREE_TYPE (instance);
5721
5722       /* XXX this should be handled before we get here.  */
5723       if (! IS_AGGR_TYPE (basetype)
5724           && ! (TYPE_LANG_SPECIFIC (basetype)
5725                 && (IS_SIGNATURE_POINTER (basetype)
5726                     || IS_SIGNATURE_REFERENCE (basetype))))
5727         {
5728           if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5729             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5730                       name, instance, basetype);
5731
5732           return error_mark_node;
5733         }
5734
5735       /* If `instance' is a signature pointer/reference and `name' is
5736          not a constructor, we are calling a signature member function.
5737          In that case set the `basetype' to the signature type.  */
5738       if ((IS_SIGNATURE_POINTER (basetype)
5739            || IS_SIGNATURE_REFERENCE (basetype))
5740           && TYPE_IDENTIFIER (basetype) != name)
5741         basetype = SIGNATURE_TYPE (basetype);
5742     }
5743
5744   if (basetype_path == NULL_TREE)
5745     basetype_path = TYPE_BINFO (basetype);
5746
5747   if (instance)
5748     {
5749       instance_ptr = build_this (instance);
5750
5751       if (! template_only)
5752         {
5753           /* XXX this should be handled before we get here.  */
5754           fns = build_field_call (basetype_path, instance_ptr, name, args);
5755           if (fns)
5756             return fns;
5757         }
5758     }
5759   else
5760     {
5761       instance_ptr = build_int_2 (0, 0);
5762       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
5763     }
5764
5765   pretty_name
5766     = (name == ctor_identifier ? constructor_name (basetype) : name);
5767
5768   fns = lookup_fnfields (basetype_path, name, 1);
5769
5770   if (fns == error_mark_node)
5771     return error_mark_node;
5772   if (fns)
5773     {
5774       tree t = TREE_VALUE (fns);
5775       if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
5776           && ! (flags & LOOKUP_HAS_IN_CHARGE))
5777         {
5778           flags |= LOOKUP_HAS_IN_CHARGE;
5779           args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
5780         }
5781       mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
5782       for (; t; t = DECL_CHAIN (t))
5783         {
5784           tree this_arglist;
5785
5786           /* We can end up here for copy-init of same or base class.  */
5787           if (name == ctor_identifier
5788               && (flags & LOOKUP_ONLYCONVERTING)
5789               && DECL_NONCONVERTING_P (t))
5790             continue;
5791           if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
5792             this_arglist = mem_args;
5793           else
5794             this_arglist = args;
5795
5796           if (TREE_CODE (t) == TEMPLATE_DECL)
5797             {
5798               /* A member template. */
5799               templates = scratch_tree_cons (NULL_TREE, t, templates);
5800               candidates = 
5801                 add_template_candidate (candidates, t, explicit_targs,
5802                                         this_arglist,
5803                                         TREE_TYPE (name), flags); 
5804             }
5805           else if (! template_only)
5806             candidates = add_function_candidate (candidates, t,
5807                                                  this_arglist, flags);
5808
5809           if (candidates)
5810             candidates->basetype_path = TREE_PURPOSE (fns);
5811         }
5812     }
5813
5814   if (! any_viable (candidates))
5815     {
5816       /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
5817       if (flags & LOOKUP_SPECULATIVELY)
5818         return NULL_TREE;
5819       cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype,
5820                 pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr)));
5821       print_z_candidates (candidates);
5822       return error_mark_node;
5823     }
5824   candidates = splice_viable (candidates);
5825   cand = tourney (candidates);
5826
5827   if (cand == 0)
5828     {
5829       cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
5830                 user_args);
5831       print_z_candidates (candidates);
5832       return error_mark_node;
5833     }
5834
5835   enforce_access (cand->basetype_path, cand->fn);
5836   if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
5837       && instance == current_class_ref
5838       && DECL_CONSTRUCTOR_P (current_function_decl)
5839       && ! (flags & LOOKUP_NONVIRTUAL)
5840       && value_member (cand->fn, get_abstract_virtuals (basetype)))
5841     cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
5842   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5843       && TREE_CODE (instance_ptr) == NOP_EXPR
5844       && TREE_OPERAND (instance_ptr, 0) == error_mark_node)
5845     cp_error ("cannot call member function `%D' without object", cand->fn);
5846
5847   if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5848       && ((instance == current_class_ref && (dtor_label || ctor_label))
5849           || resolves_to_fixed_type_p (instance, 0)))
5850     flags |= LOOKUP_NONVIRTUAL;
5851
5852   /* Pedantically, normal function declarations are never considered
5853      to refer to template instantiations, so we only do this with
5854      -fguiding-decls.  */ 
5855   if (flag_guiding_decls && templates && ! cand->template 
5856       && ! DECL_INITIAL (cand->fn))
5857     add_maybe_template (cand->fn, templates);
5858
5859   return build_over_call
5860     (cand->fn, cand->convs,
5861      TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
5862      flags);
5863 }
5864
5865 /* Compare two implicit conversion sequences that differ only in their
5866    qualification conversion.  Subroutine of compare_ics.  */
5867
5868 static int
5869 compare_qual (ics1, ics2)
5870      tree ics1, ics2;
5871 {
5872   tree to1 = TREE_TYPE (ics1);
5873   tree to2 = TREE_TYPE (ics2);
5874
5875   if (TYPE_PTRMEMFUNC_P (to1))
5876     to1 = TYPE_PTRMEMFUNC_FN_TYPE (to1);
5877   if (TYPE_PTRMEMFUNC_P (to2))
5878     to2 = TYPE_PTRMEMFUNC_FN_TYPE (to2);
5879
5880   to1 = TREE_TYPE (to1);
5881   to2 = TREE_TYPE (to2);
5882
5883   if (TREE_CODE (to1) == OFFSET_TYPE)
5884     {
5885       to1 = TREE_TYPE (to1);
5886       to2 = TREE_TYPE (to2);
5887     }
5888
5889   if (TYPE_READONLY (to1) >= TYPE_READONLY (to2)
5890       && TYPE_VOLATILE (to1) > TYPE_VOLATILE (to2))
5891     return -1;
5892   else if (TYPE_READONLY (to1) > TYPE_READONLY (to2)
5893            && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2))
5894     return -1;
5895   else if (TYPE_READONLY (to1) <= TYPE_READONLY (to2)
5896            && TYPE_VOLATILE (to1) < TYPE_VOLATILE (to2))
5897     return 1;
5898   else if (TYPE_READONLY (to1) < TYPE_READONLY (to2)
5899            && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2))
5900     return 1;
5901   return 0;
5902 }
5903
5904 /* Determine whether standard conversion sequence ICS1 is a proper
5905    subsequence of ICS2.  We assume that a conversion of the same code
5906    between the same types indicates a subsequence.  */
5907
5908 static int
5909 is_subseq (ics1, ics2)
5910      tree ics1, ics2;
5911 {
5912   /* Do not consider lvalue transformations here.  */
5913   if (TREE_CODE (ics2) == RVALUE_CONV
5914       || TREE_CODE (ics2) == LVALUE_CONV)
5915     return 0;
5916
5917   for (;; ics2 = TREE_OPERAND (ics2, 0))
5918     {
5919       if (TREE_CODE (ics2) == TREE_CODE (ics1)
5920           && comptypes (TREE_TYPE (ics2), TREE_TYPE (ics1), 1)
5921           && comptypes (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5922                         TREE_TYPE (TREE_OPERAND (ics1, 0)), 1))
5923         return 1;
5924
5925       if (TREE_CODE (ics2) == USER_CONV
5926           || TREE_CODE (ics2) == AMBIG_CONV
5927           || TREE_CODE (ics2) == IDENTITY_CONV)
5928         return 0;
5929     }
5930 }
5931
5932 /* Compare two implicit conversion sequences according to the rules set out in
5933    [over.ics.rank].  Return values:
5934
5935       1: ics1 is better than ics2
5936      -1: ics2 is better than ics1
5937       0: ics1 and ics2 are indistinguishable */
5938
5939 static int
5940 compare_ics (ics1, ics2)
5941      tree ics1, ics2;
5942 {
5943   tree main1, main2;
5944
5945   if (TREE_CODE (ics1) == QUAL_CONV)
5946     main1 = TREE_OPERAND (ics1, 0);
5947   else
5948     main1 = ics1;
5949
5950   if (TREE_CODE (ics2) == QUAL_CONV)
5951     main2 = TREE_OPERAND (ics2, 0);
5952   else
5953     main2 = ics2;
5954
5955   /* Conversions for `this' are PTR_CONVs, but we compare them as though
5956      they were REF_BINDs.  */
5957   if (ICS_THIS_FLAG (ics1))
5958     {
5959       tree t = main1;
5960       if (TREE_CODE (t) == PTR_CONV)
5961         t = TREE_OPERAND (t, 0);
5962       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5963       t = build_conv (REF_BIND, TREE_TYPE (ics1), t);
5964       ICS_STD_RANK (t) = ICS_STD_RANK (main1);
5965       main1 = ics1 = t;
5966     }
5967   if (ICS_THIS_FLAG (ics2))
5968     {
5969       tree t = main2;
5970       if (TREE_CODE (t) == PTR_CONV)
5971         t = TREE_OPERAND (t, 0);
5972       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5973       t = build_conv (REF_BIND, TREE_TYPE (ics2), t);
5974       ICS_STD_RANK (t) = ICS_STD_RANK (main2);
5975       main2 = ics2 = t;
5976     }
5977
5978   if (ICS_RANK (ics1) > ICS_RANK (ics2))
5979     return -1;
5980   else if (ICS_RANK (ics1) < ICS_RANK (ics2))
5981     return 1;
5982
5983   if (ICS_RANK (ics1) == BAD_RANK)
5984     {
5985       if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5986           || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5987         return -1;
5988       else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5989                || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5990         return 1;
5991
5992       /* else fall through */
5993     }
5994
5995   /* User-defined  conversion sequence U1 is a better conversion sequence
5996      than another user-defined conversion sequence U2 if they contain the
5997      same user-defined conversion operator or constructor and if the sec-
5998      ond standard conversion sequence of U1 is  better  than  the  second
5999      standard conversion sequence of U2.  */
6000
6001   if (ICS_USER_FLAG (ics1))
6002     {
6003       tree t1, t2;
6004
6005       for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
6006         if (TREE_CODE (t1) == AMBIG_CONV)
6007           return 0;
6008       for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
6009         if (TREE_CODE (t2) == AMBIG_CONV)
6010           return 0;
6011
6012       if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
6013         return 0;
6014       else if (ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
6015         return -1;
6016       else if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
6017         return 1;
6018
6019       /* else fall through */
6020     }
6021
6022 #if 0 /* Handled by ranking */
6023   /* A conversion that is not a conversion of a pointer,  or  pointer  to
6024      member,  to  bool  is  better than another conversion that is such a
6025      conversion.  */
6026 #endif
6027
6028   if (TREE_CODE (main1) != TREE_CODE (main2))
6029     {
6030       /* ...if S1  is  a  proper  subsequence  of  S2  */
6031       if (is_subseq (main1, main2))
6032         return 1;
6033       if (is_subseq (main2, main1))
6034         return -1;
6035       return 0;
6036     }
6037
6038   if (TREE_CODE (main1) == PTR_CONV || TREE_CODE (main1) == PMEM_CONV
6039       || TREE_CODE (main1) == REF_BIND || TREE_CODE (main1) == BASE_CONV)
6040     {
6041       tree to1 = TREE_TYPE (main1);
6042       tree from1 = TREE_TYPE (TREE_OPERAND (main1, 0));
6043       tree to2 = TREE_TYPE (main2);
6044       tree from2 = TREE_TYPE (TREE_OPERAND (main2, 0));
6045       int distf, distt;
6046
6047       /* Standard conversion sequence S1 is a better conversion sequence than
6048          standard conversion sequence S2 if...
6049
6050          S1 and S2 differ only in their qualification conversion  and  they
6051          yield types identical except for cv-qualifiers and S2 adds all the
6052          qualifiers that S1 adds (and in the same places) and S2  adds  yet
6053          more  cv-qualifiers  than  S1,  or the similar case with reference
6054          binding15).  */
6055       if (TREE_CODE (main1) == REF_BIND)
6056         {
6057           if (TYPE_MAIN_VARIANT (TREE_TYPE (to1))
6058               == TYPE_MAIN_VARIANT (TREE_TYPE (to2)))
6059             return compare_qual (ics1, ics2);
6060         }
6061       else if (TREE_CODE (main1) != BASE_CONV && from1 == from2 && to1 == to2)
6062         return compare_qual (ics1, ics2);
6063         
6064       if (TYPE_PTRMEMFUNC_P (to1))
6065         {
6066           to1 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to1)));
6067           from1 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from1)));
6068         }
6069       else if (TREE_CODE (main1) != BASE_CONV)
6070         {
6071           to1 = TREE_TYPE (to1);
6072           if (TREE_CODE (main1) != REF_BIND)
6073             from1 = TREE_TYPE (from1);
6074
6075           if (TREE_CODE (to1) == OFFSET_TYPE)
6076             {
6077               to1 = TYPE_OFFSET_BASETYPE (to1);
6078               from1 = TYPE_OFFSET_BASETYPE (from1);
6079             }
6080         }
6081
6082       if (TYPE_PTRMEMFUNC_P (to2))
6083         {
6084           to2 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to2)));
6085           from2 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from2)));
6086         }
6087       else if (TREE_CODE (main1) != BASE_CONV)
6088         {
6089           to2 = TREE_TYPE (to2);
6090           if (TREE_CODE (main1) != REF_BIND)
6091             from2 = TREE_TYPE (from2);
6092
6093           if (TREE_CODE (to2) == OFFSET_TYPE)
6094             {
6095               to2 = TYPE_OFFSET_BASETYPE (to2);
6096               from2 = TYPE_OFFSET_BASETYPE (from2);
6097             }
6098         }
6099
6100       if (! (IS_AGGR_TYPE (from1) && IS_AGGR_TYPE (from2)))
6101         return 0;
6102
6103       /* The sense of pmem conversions is reversed from that of the other
6104          conversions.  */
6105       if (TREE_CODE (main1) == PMEM_CONV)
6106         {
6107           tree t = from1; from1 = from2; from2 = t;
6108           t = to1; to1 = to2; to2 = t;
6109         }
6110
6111       distf = get_base_distance (from1, from2, 0, 0);
6112       if (distf == -1)
6113         {
6114           distf = -get_base_distance (from2, from1, 0, 0);
6115           if (distf == 1)
6116             return 0;
6117         }
6118
6119       /* If class B is derived directly or indirectly from class A,
6120          conver- sion of B* to A* is better than conversion of B* to
6121          void*, and conversion of A* to void* is better than
6122          conversion of B* to void*.  */
6123
6124       if (TREE_CODE (to1) == VOID_TYPE && TREE_CODE (to2) == VOID_TYPE)
6125         {
6126           if (distf > 0)
6127             return 1;
6128           else if (distf < 0)
6129             return -1;
6130         }
6131       else if (TREE_CODE (to2) == VOID_TYPE && IS_AGGR_TYPE (to1)
6132                && get_base_distance (to1, from1, 0, 0) != -1)
6133         return 1;
6134       else if (TREE_CODE (to1) == VOID_TYPE && IS_AGGR_TYPE (to2)
6135                && get_base_distance (to2, from2, 0, 0) != -1)
6136         return -1;
6137
6138       if (! (IS_AGGR_TYPE (to1) && IS_AGGR_TYPE (to2)))
6139         return 0;
6140
6141       /* If  class B is derived directly or indirectly from class A and class
6142          C is derived directly or indirectly from B */
6143
6144       distt = get_base_distance (to1, to2, 0, 0);
6145       if (distt == -1)
6146         {
6147           distt = -get_base_distance (to2, to1, 0, 0);
6148           if (distt == 1)
6149             return 0;
6150         }
6151
6152       /* --conversion of C* to B* is better than conversion of C* to A*, */
6153       if (distf == 0)
6154         {
6155           if (distt > 0)
6156             return -1;
6157           else if (distt < 0)
6158             return 1;
6159         }
6160       /* --conversion of B* to A* is better than conversion of C* to A*, */
6161       else if (distt == 0)
6162         {
6163           if (distf > 0)
6164             return 1;
6165           else if (distf < 0)
6166             return -1;
6167         }
6168     }
6169   else if (TREE_CODE (TREE_TYPE (main1)) == POINTER_TYPE
6170            || TYPE_PTRMEMFUNC_P (TREE_TYPE (main1)))
6171     {
6172       if (TREE_TYPE (main1) == TREE_TYPE (main2))
6173         return compare_qual (ics1, ics2);
6174
6175 #if 0 /* This is now handled by making identity better than anything else.  */
6176       /* existing practice, not WP-endorsed: const char * -> const char *
6177          is better than char * -> const char *.  (jason 6/29/96) */
6178       if (TREE_TYPE (ics1) == TREE_TYPE (ics2))
6179         return -compare_qual (main1, main2);
6180 #endif
6181     }
6182
6183   return 0;
6184 }
6185
6186 /* The source type for this standard conversion sequence.  */
6187
6188 static tree
6189 source_type (t)
6190      tree t;
6191 {
6192   for (;; t = TREE_OPERAND (t, 0))
6193     {
6194       if (TREE_CODE (t) == USER_CONV
6195           || TREE_CODE (t) == AMBIG_CONV
6196           || TREE_CODE (t) == IDENTITY_CONV)
6197         return TREE_TYPE (t);
6198     }
6199   my_friendly_abort (1823);
6200 }
6201
6202 /* Compare two candidates for overloading as described in
6203    [over.match.best].  Return values:
6204
6205       1: cand1 is better than cand2
6206      -1: cand2 is better than cand1
6207       0: cand1 and cand2 are indistinguishable */
6208
6209 static int
6210 joust (cand1, cand2)
6211      struct z_candidate *cand1, *cand2;
6212 {
6213   int winner = 0;
6214   int i, off1 = 0, off2 = 0, len;
6215
6216   /* Candidates that involve bad conversions are always worse than those
6217      that don't.  */
6218   if (cand1->viable > cand2->viable)
6219     return 1;
6220   if (cand1->viable < cand2->viable)
6221     return -1;
6222
6223   /* a viable function F1
6224      is defined to be a better function than another viable function F2  if
6225      for  all arguments i, ICSi(F1) is not a worse conversion sequence than
6226      ICSi(F2), and then */
6227
6228   /* for some argument j, ICSj(F1) is a better conversion  sequence  than
6229      ICSj(F2) */
6230
6231   /* For comparing static and non-static member functions, we ignore the
6232      implicit object parameter of the non-static function.  The WP says to
6233      pretend that the static function has an object parm, but that won't
6234      work with operator overloading.  */
6235   len = TREE_VEC_LENGTH (cand1->convs);
6236   if (len != TREE_VEC_LENGTH (cand2->convs))
6237     {
6238       if (DECL_STATIC_FUNCTION_P (cand1->fn)
6239           && ! DECL_STATIC_FUNCTION_P (cand2->fn))
6240         off2 = 1;
6241       else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
6242                && DECL_STATIC_FUNCTION_P (cand2->fn))
6243         {
6244           off1 = 1;
6245           --len;
6246         }
6247       else
6248         my_friendly_abort (42);
6249     }
6250
6251   for (i = 0; i < len; ++i)
6252     {
6253       tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
6254       tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
6255       int comp = compare_ics (t1, t2);
6256
6257       if (comp != 0)
6258         {
6259 #if 0 /* move this warning to tourney.  */
6260           if (warn_sign_promo
6261               && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
6262               && TREE_CODE (t1) == STD_CONV
6263               && TREE_CODE (t2) == STD_CONV
6264               && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
6265               && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
6266               && (TYPE_PRECISION (TREE_TYPE (t1))
6267                   == TYPE_PRECISION (TREE_TYPE (t2)))
6268               && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
6269                   || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
6270                       == ENUMERAL_TYPE)))
6271             {
6272               tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
6273               tree type1, type2;
6274               if (comp > 0)
6275                 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2);
6276               else
6277                 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1);
6278
6279               cp_warning ("passing `%T' chooses `%T' over `%T'",
6280                           type, type1, type2);
6281               cp_warning ("  in call to `%D'", DECL_NAME (cand1->fn));
6282             }
6283 #endif
6284
6285           if (winner && comp != winner)
6286             {
6287               winner = 0;
6288               goto tweak;
6289             }
6290           winner = comp;
6291         }
6292     }
6293
6294 #if 0 /* move this warning to tourney.  */
6295   /* warn about confusing overload resolution */
6296   if (winner && cand1->second_conv
6297       && ! DECL_CONSTRUCTOR_P (cand1->fn)
6298       && ! DECL_CONSTRUCTOR_P (cand2->fn))
6299     {
6300       int comp = compare_ics (cand1->second_conv, cand2->second_conv);
6301       if (comp && comp != winner)
6302         {
6303           struct z_candidate *w, *l;
6304           if (winner == 1)
6305             w = cand1, l = cand2;
6306           else
6307             w = cand2, l = cand1;
6308           cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
6309           cp_warning ("  for conversion from `%T' to `%T'",
6310                       TREE_TYPE (source_type (TREE_VEC_ELT (w->convs, 0))),
6311                       TREE_TYPE (w->second_conv));
6312           cp_warning ("  because conversion sequence for `this' argument is better");
6313         }
6314     }
6315 #endif
6316
6317   if (winner)
6318     return winner;
6319
6320   /* or, if not that,
6321      F1 is a non-template function and F2 is a template function */
6322
6323   if (! cand1->template && cand2->template)
6324     return 1;
6325   else if (cand1->template && ! cand2->template)
6326     return -1;
6327   else if (cand1->template && cand2->template)
6328     winner = more_specialized
6329       (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
6330        NULL_TREE);
6331
6332   /* or, if not that,
6333      the  context  is  an  initialization by user-defined conversion (see
6334      _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
6335      sequence  from  the return type of F1 to the destination type (i.e.,
6336      the type of the entity being initialized)  is  a  better  conversion
6337      sequence  than the standard conversion sequence from the return type
6338      of F2 to the destination type.  */
6339
6340   if (! winner && cand1->second_conv)
6341     winner = compare_ics (cand1->second_conv, cand2->second_conv);
6342
6343   /* If the built-in candidates are the same, arbitrarily pick one.  */
6344   if (! winner && cand1->fn == cand2->fn
6345       && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
6346     {
6347       for (i = 0; i < len; ++i)
6348         if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
6349                          TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
6350           break;
6351       if (i == TREE_VEC_LENGTH (cand1->convs))
6352         return 1;
6353
6354       /* Kludge around broken overloading rules whereby
6355          Integer a, b; test ? a : b; is ambiguous, since there's a builtin
6356          that takes references and another that takes values.  */
6357       if (cand1->fn == ansi_opname[COND_EXPR])
6358         {
6359           tree c1 = TREE_VEC_ELT (cand1->convs, 1);
6360           tree c2 = TREE_VEC_ELT (cand2->convs, 1);
6361           tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
6362           tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
6363
6364           if (comptypes (t1, t2, 1))
6365             {
6366               if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
6367                 return 1;
6368               if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
6369                 return -1;
6370             }
6371         }
6372     }
6373
6374 tweak:
6375
6376   /* Extension: If the worst conversion for one candidate is worse than the
6377      worst conversion for the other, take the first.  */
6378   if (! winner && ! pedantic)
6379     {
6380       int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
6381
6382       for (i = 0; i < len; ++i)
6383         {
6384           if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
6385             rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
6386           if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
6387             rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
6388         }
6389
6390       if (rank1 < rank2)
6391         return 1;
6392       if (rank1 > rank2)
6393         return -1;
6394     }
6395
6396   return winner;
6397 }
6398
6399 /* Given a list of candidates for overloading, find the best one, if any.
6400    This algorithm has a worst case of O(2n) (winner is last), and a best
6401    case of O(n/2) (totally ambiguous); much better than a sorting
6402    algorithm.  */
6403
6404 static struct z_candidate *
6405 tourney (candidates)
6406      struct z_candidate *candidates;
6407 {
6408   struct z_candidate *champ = candidates, *challenger;
6409   int fate;
6410
6411   /* Walk through the list once, comparing each current champ to the next
6412      candidate, knocking out a candidate or two with each comparison.  */
6413
6414   for (challenger = champ->next; challenger; )
6415     {
6416       fate = joust (champ, challenger);
6417       if (fate == 1)
6418         challenger = challenger->next;
6419       else
6420         {
6421           if (fate == 0)
6422             {
6423               champ = challenger->next;
6424               if (champ == 0)
6425                 return 0;
6426             }
6427           else
6428             champ = challenger;
6429
6430           challenger = champ->next;
6431         }
6432     }
6433
6434   /* Make sure the champ is better than all the candidates it hasn't yet
6435      been compared to.  This may do one more comparison than necessary.  Oh
6436      well.  */
6437
6438   for (challenger = candidates; challenger != champ;
6439        challenger = challenger->next)
6440     {
6441       fate = joust (champ, challenger);
6442       if (fate != 1)
6443         return 0;
6444     }
6445
6446   return champ;
6447 }
6448
6449 int
6450 can_convert (to, from)
6451      tree to, from;
6452 {
6453   if (flag_ansi_overloading)
6454     {
6455       tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
6456       return (t && ! ICS_BAD_FLAG (t));
6457     }
6458   else
6459     {
6460       struct harshness_code h;
6461       h = convert_harshness (to, from, NULL_TREE);
6462       return (h.code < USER_CODE) && (h.distance >= 0);
6463     }
6464 }
6465
6466 int
6467 can_convert_arg (to, from, arg)
6468      tree to, from, arg;
6469 {
6470   if (flag_ansi_overloading)
6471     {
6472       tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
6473       return (t && ! ICS_BAD_FLAG (t));
6474     }
6475   else
6476     {
6477       struct harshness_code h;
6478       h = convert_harshness (to, from, arg);
6479       return (h.code < USER_CODE) && (h.distance >= 0);
6480     }
6481 }