OSDN Git Service

* call.c, class.c, pt.c, cp-tree.h: Remove nsubsts parm from
[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), 0);
1480           if (type)
1481             name = build_min_nt (BIT_NOT_EXPR, type);
1482         }
1483       name = build_min_nt (SCOPE_REF, basetype, name);
1484       return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
1485     }
1486
1487   if (TREE_CODE (type) == REFERENCE_TYPE)
1488     type = TREE_TYPE (type);
1489
1490   if (TREE_CODE (basetype) == TREE_VEC)
1491     {
1492       binfo = basetype;
1493       basetype = BINFO_TYPE (binfo);
1494     }
1495   else
1496     binfo = NULL_TREE;
1497
1498   /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1499      that explicit ~int is caught in the parser; this deals with typedefs
1500      and template parms.  */
1501   if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
1502     {
1503       if (type != basetype)
1504         cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1505                   exp, basetype, type);
1506       name = TREE_OPERAND (name, 0);
1507       if (basetype != name && basetype != get_type_value (name))
1508         cp_error ("qualified type `%T' does not match destructor name `~%T'",
1509                   basetype, name);
1510       return cp_convert (void_type_node, exp);
1511     }
1512
1513   if (! is_aggr_type (basetype, 1))
1514     return error_mark_node;
1515
1516   if (! IS_AGGR_TYPE (type))
1517     {
1518       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1519                 exp, type);
1520       return error_mark_node;
1521     }
1522
1523   if (! binfo)
1524     {
1525       binfo = get_binfo (basetype, type, 1);
1526       if (binfo == error_mark_node)
1527         return error_mark_node;
1528       if (! binfo)
1529         error_not_base_type (basetype, type);
1530     }
1531
1532   if (binfo)
1533     {
1534       if (TREE_CODE (exp) == INDIRECT_REF)
1535         decl = build_indirect_ref
1536           (convert_pointer_to_real
1537            (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
1538       else
1539         decl = build_scoped_ref (exp, basetype);
1540
1541       /* Call to a destructor.  */
1542       if (TREE_CODE (name) == BIT_NOT_EXPR)
1543         {
1544           /* Explicit call to destructor.  */
1545           name = TREE_OPERAND (name, 0);
1546           if (! (name == TYPE_MAIN_VARIANT (TREE_TYPE (decl))
1547                  || name == constructor_name (TREE_TYPE (decl))
1548                  || TREE_TYPE (decl) == get_type_value (name)))
1549             {
1550               cp_error
1551                 ("qualified type `%T' does not match destructor name `~%T'",
1552                  TREE_TYPE (decl), name);
1553               return error_mark_node;
1554             }
1555           if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
1556             return cp_convert (void_type_node, exp);
1557           
1558           return build_delete (TREE_TYPE (decl), decl, integer_two_node,
1559                                LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
1560                                0);
1561         }
1562
1563       /* Call to a method.  */
1564       return build_method_call (decl, name, parms, binfo,
1565                                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1566     }
1567   return error_mark_node;
1568 }
1569
1570 static void
1571 print_candidates (candidates)
1572      tree candidates;
1573 {
1574   cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
1575   candidates = TREE_CHAIN (candidates);
1576
1577   while (candidates)
1578     {
1579       cp_error_at ("                %D", TREE_VALUE (candidates));
1580       candidates = TREE_CHAIN (candidates);
1581     }
1582 }
1583
1584 static void
1585 print_n_candidates (candidates, n)
1586      struct candidate *candidates;
1587      int n;
1588 {
1589   int i;
1590
1591   cp_error_at ("candidates are: %D", candidates[0].function);
1592   for (i = 1; i < n; i++)
1593     cp_error_at ("                %D", candidates[i].function);
1594 }
1595
1596 /* We want the address of a function or method.  We avoid creating a
1597    pointer-to-member function.  */
1598
1599 tree
1600 build_addr_func (function)
1601      tree function;
1602 {
1603   tree type = TREE_TYPE (function);
1604
1605   /* We have to do these by hand to avoid real pointer to member
1606      functions.  */
1607   if (TREE_CODE (type) == METHOD_TYPE)
1608     {
1609       tree addr;
1610
1611       type = build_pointer_type (type);
1612
1613       if (mark_addressable (function) == 0)
1614         return error_mark_node;
1615
1616       addr = build1 (ADDR_EXPR, type, function);
1617
1618       /* Address of a static or external variable or function counts
1619          as a constant */
1620       if (staticp (function))
1621         TREE_CONSTANT (addr) = 1;
1622
1623       function = addr;
1624     }
1625   else
1626     function = default_conversion (function);
1627
1628   return function;
1629 }
1630
1631 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1632    POINTER_TYPE to those.  Note, pointer to member function types
1633    (TYPE_PTRMEMFUNC_P) must be handled by our callers.  */
1634
1635 tree
1636 build_call (function, result_type, parms)
1637      tree function, result_type, parms;
1638 {
1639   int is_constructor = 0;
1640
1641   function = build_addr_func (function);
1642
1643   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
1644     {
1645       sorry ("unable to call pointer to member function here");
1646       return error_mark_node;
1647     }
1648
1649   if (TREE_CODE (function) == ADDR_EXPR
1650       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1651       && DECL_CONSTRUCTOR_P (TREE_OPERAND (function, 0)))
1652     is_constructor = 1;
1653
1654   function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
1655   TREE_HAS_CONSTRUCTOR (function) = is_constructor;
1656   TREE_TYPE (function) = result_type;
1657   TREE_SIDE_EFFECTS (function) = 1;
1658   
1659   return function;
1660 }
1661
1662 static tree
1663 default_parm_conversions (parms, last)
1664      tree parms, *last;
1665 {
1666   tree parm, parmtypes = NULL_TREE;
1667
1668   *last = NULL_TREE;
1669
1670   for (parm = parms; parm; parm = TREE_CHAIN (parm))
1671     {
1672       tree t = TREE_TYPE (TREE_VALUE (parm));
1673
1674       if (TREE_CODE (t) == OFFSET_TYPE
1675           || TREE_CODE (t) == METHOD_TYPE
1676           || TREE_CODE (t) == FUNCTION_TYPE)
1677         {
1678           TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
1679           t = TREE_TYPE (TREE_VALUE (parm));
1680         }
1681
1682       if (t == error_mark_node)
1683           return error_mark_node;
1684
1685       *last = build_tree_list (NULL_TREE, t);
1686       parmtypes = chainon (parmtypes, *last);
1687     }
1688
1689   return parmtypes;
1690 }
1691
1692
1693 /* Build something of the form ptr->method (args)
1694    or object.method (args).  This can also build
1695    calls to constructors, and find friends.
1696
1697    Member functions always take their class variable
1698    as a pointer.
1699
1700    INSTANCE is a class instance.
1701
1702    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1703
1704    PARMS help to figure out what that NAME really refers to.
1705
1706    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1707    down to the real instance type to use for access checking.  We need this
1708    information to get protected accesses correct.  This parameter is used
1709    by build_member_call.
1710
1711    FLAGS is the logical disjunction of zero or more LOOKUP_
1712    flags.  See cp-tree.h for more info.
1713
1714    If this is all OK, calls build_function_call with the resolved
1715    member function.
1716
1717    This function must also handle being called to perform
1718    initialization, promotion/coercion of arguments, and
1719    instantiation of default parameters.
1720
1721    Note that NAME may refer to an instance variable name.  If
1722    `operator()()' is defined for the type of that field, then we return
1723    that result.  */
1724
1725 tree
1726 build_method_call (instance, name, parms, basetype_path, flags)
1727      tree instance, name, parms, basetype_path;
1728      int flags;
1729 {
1730   register tree function, fntype, value_type;
1731   register tree basetype, save_basetype;
1732   register tree baselink, result, parmtypes;
1733   tree last;
1734   int pass;
1735   tree access = access_public_node;
1736   tree orig_basetype = basetype_path ? BINFO_TYPE (basetype_path) : NULL_TREE;
1737
1738   /* Range of cases for vtable optimization.  */
1739   enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
1740   enum vtable_needs need_vtbl = not_needed;
1741
1742   char *name_kind;
1743   tree save_name = name;
1744   int ever_seen = 0;
1745   tree instance_ptr = NULL_TREE;
1746   int all_virtual = flag_all_virtual;
1747   int static_call_context = 0;
1748   tree found_fns = NULL_TREE;
1749
1750   /* Keep track of `const' and `volatile' objects.  */
1751   int constp, volatilep;
1752
1753 #ifdef GATHER_STATISTICS
1754   n_build_method_call++;
1755 #endif
1756
1757   if (instance == error_mark_node
1758       || name == error_mark_node
1759       || parms == error_mark_node
1760       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
1761     return error_mark_node;
1762
1763   if (processing_template_decl)
1764     {
1765       if (TREE_CODE (name) == BIT_NOT_EXPR)
1766         {
1767           tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1768           name = build_min_nt (BIT_NOT_EXPR, type);
1769         }
1770
1771       return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
1772     }
1773
1774   /* This is the logic that magically deletes the second argument to
1775      operator delete, if it is not needed.  */
1776   if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
1777     {
1778       tree save_last = TREE_CHAIN (parms);
1779       tree result;
1780       /* get rid of unneeded argument */
1781       TREE_CHAIN (parms) = NULL_TREE;
1782       result = build_method_call (instance, name, parms, basetype_path,
1783                                   (LOOKUP_SPECULATIVELY|flags)
1784                                   &~LOOKUP_COMPLAIN);
1785       /* If it finds a match, return it.  */
1786       if (result)
1787         return build_method_call (instance, name, parms, basetype_path, flags);
1788       /* If it doesn't work, two argument delete must work */
1789       TREE_CHAIN (parms) = save_last;
1790     }
1791   /* We already know whether it's needed or not for vec delete.  */
1792   else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
1793            && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1794            && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
1795     TREE_CHAIN (parms) = NULL_TREE;
1796
1797   if (TREE_CODE (name) == BIT_NOT_EXPR)
1798     {
1799       flags |= LOOKUP_DESTRUCTOR;
1800       name = TREE_OPERAND (name, 0);
1801       if (parms)
1802         error ("destructors take no parameters");
1803       basetype = TREE_TYPE (instance);
1804       if (TREE_CODE (basetype) == REFERENCE_TYPE)
1805         basetype = TREE_TYPE (basetype);
1806       if (! (name == basetype
1807              || (IS_AGGR_TYPE (basetype)
1808                  && name == constructor_name (basetype))
1809              || basetype == get_type_value (name)))
1810         {
1811           cp_error ("destructor name `~%D' does not match type `%T' of expression",
1812                     name, basetype);
1813           return cp_convert (void_type_node, instance);
1814         }
1815
1816       if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
1817         return cp_convert (void_type_node, instance);
1818       instance = default_conversion (instance);
1819       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1820       return build_delete (build_pointer_type (basetype),
1821                            instance_ptr, integer_two_node,
1822                            LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
1823     }
1824
1825   if (flag_ansi_overloading)
1826     return build_new_method_call (instance, name, parms, basetype_path, flags);
1827
1828   {
1829     char *xref_name;
1830     
1831     /* Initialize name for error reporting.  */
1832     if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
1833       {
1834         char *p = operator_name_string (name);
1835         xref_name = (char *)alloca (strlen (p) + 10);
1836         sprintf (xref_name, "operator %s", p);
1837       }
1838     else if (TREE_CODE (name) == SCOPE_REF)
1839       xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
1840     else
1841       xref_name = IDENTIFIER_POINTER (name);
1842
1843     GNU_xref_call (current_function_decl, xref_name);
1844   }
1845
1846   if (instance == NULL_TREE)
1847     {
1848       basetype = NULL_TREE;
1849       /* Check cases where this is really a call to raise
1850          an exception.  */
1851       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
1852         {
1853           basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
1854           if (basetype)
1855             basetype = TREE_VALUE (basetype);
1856         }
1857       else if (TREE_CODE (name) == SCOPE_REF
1858                && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
1859         {
1860           if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
1861             return error_mark_node;
1862           basetype = purpose_member (TREE_OPERAND (name, 1),
1863                                      CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
1864           if (basetype)
1865             basetype = TREE_VALUE (basetype);
1866         }
1867
1868       if (basetype != NULL_TREE)
1869         ;
1870       /* call to a constructor...  */
1871       else if (basetype_path)
1872         {
1873           basetype = BINFO_TYPE (basetype_path);
1874           if (name == TYPE_IDENTIFIER (basetype))
1875             name = ctor_identifier;
1876         }
1877       else if (IDENTIFIER_HAS_TYPE_VALUE (name))
1878         {
1879           basetype = IDENTIFIER_TYPE_VALUE (name);
1880           name = ctor_identifier;
1881         }
1882       else
1883         {
1884           tree typedef_name = lookup_name (name, 1);
1885           if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
1886             {
1887               /* Canonicalize the typedef name.  */
1888               basetype = TREE_TYPE (typedef_name);
1889               name = ctor_identifier;
1890             }
1891           else
1892             {
1893               cp_error ("no constructor named `%T' in scope",
1894                         name);
1895               return error_mark_node;
1896             }
1897         }
1898
1899       if (! IS_AGGR_TYPE (basetype))
1900         {
1901         non_aggr_error:
1902           if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
1903             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1904                       name, instance, basetype);
1905
1906           return error_mark_node;
1907         }
1908     }
1909   else if (instance == current_class_ref || instance == current_class_ptr)
1910     {
1911       /* When doing initialization, we side-effect the TREE_TYPE of
1912          current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
1913       basetype = TREE_TYPE (current_class_ref);
1914
1915       /* Anything manifestly `this' in constructors and destructors
1916          has a known type, so virtual function tables are not needed.  */
1917       if (TYPE_VIRTUAL_P (basetype)
1918           && !(flags & LOOKUP_NONVIRTUAL))
1919         need_vtbl = (dtor_label || ctor_label)
1920           ? unneeded : maybe_needed;
1921
1922       /* If `this' is a signature pointer and `name' is not a constructor,
1923          we are calling a signature member function.  In that case, set the
1924          `basetype' to the signature type and dereference the `optr' field.  */
1925       if (IS_SIGNATURE_POINTER (basetype)
1926           && TYPE_IDENTIFIER (basetype) != name)
1927         {
1928           basetype = SIGNATURE_TYPE (basetype);
1929           instance_ptr = instance;
1930           basetype_path = TYPE_BINFO (basetype);
1931         }
1932       else
1933         {
1934           instance = current_class_ref;
1935           instance_ptr = current_class_ptr;
1936           basetype_path = TYPE_BINFO (current_class_type);
1937         }
1938       result = build_field_call (basetype_path, instance_ptr, name, parms);
1939
1940       if (result)
1941         return result;
1942     }
1943   else if (TREE_CODE (instance) == RESULT_DECL)
1944     {
1945       basetype = TREE_TYPE (instance);
1946       /* Should we ever have to make a virtual function reference
1947          from a RESULT_DECL, know that it must be of fixed type
1948          within the scope of this function.  */
1949       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1950         need_vtbl = maybe_needed;
1951       instance_ptr = build1 (ADDR_EXPR, build_pointer_type (basetype), instance);
1952     }
1953   else
1954     {
1955       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
1956       tree inst_ptr_basetype;
1957
1958       static_call_context
1959         = (TREE_CODE (instance) == INDIRECT_REF
1960            && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
1961            && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
1962
1963       if (TREE_CODE (instance) == OFFSET_REF)
1964         instance = resolve_offset_ref (instance);
1965
1966       /* the base type of an instance variable is pointer to class */
1967       basetype = TREE_TYPE (instance);
1968
1969       if (TREE_CODE (basetype) == REFERENCE_TYPE)
1970         {
1971           basetype = TREE_TYPE (basetype);
1972           if (! IS_AGGR_TYPE (basetype))
1973             goto non_aggr_error;
1974           /* Call to convert not needed because we are remaining
1975              within the same type.  */
1976           instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
1977                                  instance);
1978           inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
1979         }
1980       else
1981         {
1982           if (! IS_AGGR_TYPE (basetype)
1983               && ! (TYPE_LANG_SPECIFIC (basetype)
1984                     && (IS_SIGNATURE_POINTER (basetype)
1985                         || IS_SIGNATURE_REFERENCE (basetype))))
1986             goto non_aggr_error;
1987
1988           /* If `instance' is a signature pointer/reference and `name' is
1989              not a constructor, we are calling a signature member function.
1990              In that case set the `basetype' to the signature type.  */
1991           if ((IS_SIGNATURE_POINTER (basetype)
1992                || IS_SIGNATURE_REFERENCE (basetype))
1993               && TYPE_IDENTIFIER (basetype) != name)
1994             basetype = SIGNATURE_TYPE (basetype);
1995
1996           basetype = complete_type (basetype);
1997
1998           if ((IS_SIGNATURE (basetype)
1999                && (instance_ptr = instance))
2000               || (lvalue_p (instance)
2001                   && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
2002               || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
2003             {
2004               if (instance_ptr == error_mark_node)
2005                 return error_mark_node;
2006             }
2007           else if (TREE_CODE (instance) == NOP_EXPR
2008                    || TREE_CODE (instance) == CONSTRUCTOR)
2009             {
2010               /* A cast is not an lvalue.  Initialize a fresh temp
2011                  with the value we are casting from, and proceed with
2012                  that temporary.  We can't cast to a reference type,
2013                  so that simplifies the initialization to something
2014                  we can manage.  */
2015               tree temp = get_temp_name (TREE_TYPE (instance), 0);
2016               if (IS_AGGR_TYPE (TREE_TYPE (instance)))
2017                 expand_aggr_init (temp, instance, 0, flags);
2018               else
2019                 {
2020                   store_init_value (temp, instance);
2021                   expand_decl_init (temp);
2022                 }
2023               instance = temp;
2024               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
2025             }
2026           else
2027             {
2028               if (TREE_CODE (instance) != CALL_EXPR)
2029                 my_friendly_abort (125);
2030               if (TYPE_NEEDS_CONSTRUCTING (basetype))
2031                 instance = build_cplus_new (basetype, instance);
2032               else
2033                 {
2034                   instance = get_temp_name (basetype, 0);
2035                   TREE_ADDRESSABLE (instance) = 1;
2036                 }
2037               instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
2038             }
2039           /* @@ Should we call comp_target_types here?  */
2040           if (IS_SIGNATURE (basetype))
2041             inst_ptr_basetype = basetype;
2042           else
2043             inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
2044           if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
2045             basetype = inst_ptr_basetype;
2046           else
2047             {
2048               instance_ptr = cp_convert (build_pointer_type (basetype), instance_ptr);
2049               if (instance_ptr == error_mark_node)
2050                 return error_mark_node;
2051             }
2052         }
2053
2054       /* After converting `instance_ptr' above, `inst_ptr_basetype' was
2055          not updated, so we use `basetype' instead.  */
2056       if (basetype_path == NULL_TREE
2057           && IS_SIGNATURE (basetype))
2058         basetype_path = TYPE_BINFO (basetype);
2059       else if (basetype_path == NULL_TREE
2060                || (BINFO_TYPE (basetype_path)
2061                    != TYPE_MAIN_VARIANT (inst_ptr_basetype)))
2062         basetype_path = TYPE_BINFO (inst_ptr_basetype);
2063
2064       result = build_field_call (basetype_path, instance_ptr, name, parms);
2065       if (result)
2066         return result;
2067
2068       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
2069         {
2070           if (TREE_SIDE_EFFECTS (instance_ptr))
2071             {
2072               /* This action is needed because the instance is needed
2073                  for providing the base of the virtual function table.
2074                  Without using a SAVE_EXPR, the function we are building
2075                  may be called twice, or side effects on the instance
2076                  variable (such as a post-increment), may happen twice.  */
2077               instance_ptr = save_expr (instance_ptr);
2078               instance = build_indirect_ref (instance_ptr, NULL_PTR);
2079             }
2080           else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
2081             {
2082               /* This happens when called for operator new ().  */
2083               instance = build_indirect_ref (instance, NULL_PTR);
2084             }
2085
2086           need_vtbl = maybe_needed;
2087         }
2088     }
2089
2090   if (save_name == ctor_identifier)
2091     save_name = TYPE_IDENTIFIER (basetype);
2092
2093   if (TYPE_SIZE (complete_type (basetype)) == 0)
2094     {
2095       /* This is worth complaining about, I think.  */
2096       cp_error ("cannot lookup method in incomplete type `%T'", basetype);
2097       return error_mark_node;
2098     }
2099
2100   save_basetype = TYPE_MAIN_VARIANT (basetype);
2101
2102   parmtypes = default_parm_conversions (parms, &last);
2103   if (parmtypes == error_mark_node)
2104     {
2105       return error_mark_node;
2106     }
2107
2108   if (instance && IS_SIGNATURE (basetype))
2109     {
2110       /* @@ Should this be the constp/volatilep flags for the optr field
2111          of the signature pointer?  */
2112       constp = TYPE_READONLY (basetype);
2113       volatilep = TYPE_VOLATILE (basetype);
2114       parms = expr_tree_cons (NULL_TREE, instance_ptr, parms);
2115     }
2116   else if (instance)
2117     {
2118       /* TREE_READONLY (instance) fails for references.  */
2119       constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
2120       volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
2121       parms = expr_tree_cons (NULL_TREE, instance_ptr, parms);
2122     }
2123   else
2124     {
2125       /* Raw constructors are always in charge.  */
2126       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2127           && ! (flags & LOOKUP_HAS_IN_CHARGE))
2128         {
2129           flags |= LOOKUP_HAS_IN_CHARGE;
2130           parms = expr_tree_cons (NULL_TREE, integer_one_node, parms);
2131           parmtypes = scratch_tree_cons (NULL_TREE, integer_type_node, parmtypes);
2132         }
2133
2134       constp = 0;
2135       volatilep = 0;
2136       instance_ptr = build_int_2 (0, 0);
2137       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
2138       parms = expr_tree_cons (NULL_TREE, instance_ptr, parms);
2139     }
2140
2141   parmtypes = scratch_tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
2142
2143   if (last == NULL_TREE)
2144     last = parmtypes;
2145
2146   /* Look up function name in the structure type definition.  */
2147
2148   /* FIXME Axe most of this now?  */
2149   if ((IDENTIFIER_HAS_TYPE_VALUE (name)
2150        && ! IDENTIFIER_OPNAME_P (name)
2151        && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name)))
2152       || name == constructor_name (basetype)
2153       || name == ctor_identifier)
2154     {
2155       tree tmp = NULL_TREE;
2156       if (IDENTIFIER_TYPE_VALUE (name) == basetype
2157           || name == constructor_name (basetype)
2158           || name == ctor_identifier)
2159         tmp = TYPE_BINFO (basetype);
2160       else
2161         tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
2162       
2163       if (tmp != NULL_TREE)
2164         {
2165           name_kind = "constructor";
2166           
2167           if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2168               && ! (flags & LOOKUP_HAS_IN_CHARGE))
2169             {
2170               /* Constructors called for initialization
2171                  only are never in charge.  */
2172               tree tmplist;
2173               
2174               flags |= LOOKUP_HAS_IN_CHARGE;
2175               tmplist = expr_tree_cons (NULL_TREE, integer_zero_node,
2176                                    TREE_CHAIN (parms));
2177               TREE_CHAIN (parms) = tmplist;
2178               tmplist = scratch_tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
2179               TREE_CHAIN (parmtypes) = tmplist;
2180             }
2181           basetype = BINFO_TYPE (tmp);
2182         }
2183       else
2184         name_kind = "method";
2185     }
2186   else
2187     name_kind = "method";
2188   
2189   if (basetype_path == NULL_TREE
2190       || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
2191     basetype_path = TYPE_BINFO (basetype);
2192   result = lookup_fnfields (basetype_path, name,
2193                             (flags & LOOKUP_COMPLAIN));
2194   if (result == error_mark_node)
2195     return error_mark_node;
2196
2197   for (pass = 0; pass < 2; pass++)
2198     {
2199       struct candidate *candidates = 0;
2200       struct candidate *cp = 0;
2201       int len = 0;
2202       unsigned best = 1;
2203
2204       baselink = result;
2205
2206       if (pass > 0)
2207         {
2208           candidates
2209             = (struct candidate *) alloca ((ever_seen+1)
2210                                            * sizeof (struct candidate));
2211           bzero ((char *) candidates, (ever_seen + 1) * sizeof (struct candidate));
2212           cp = candidates;
2213           len = list_length (parms);
2214           ever_seen = 0;
2215
2216           /* First see if a global function has a shot at it.  */
2217           if (flags & LOOKUP_GLOBAL)
2218             {
2219               tree friend_parms;
2220               tree parm = instance_ptr;
2221
2222               if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
2223                 parm = convert_from_reference (parm);
2224               else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
2225                 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
2226               else
2227                 my_friendly_abort (167);
2228
2229               friend_parms = expr_tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
2230
2231               cp->h_len = len;
2232               cp->harshness = (struct harshness_code *)
2233                 alloca ((len + 1) * sizeof (struct harshness_code));
2234
2235               result = build_overload_call_real (name, friend_parms, 0, cp, 1);
2236
2237               /* If it turns out to be the one we were actually looking for
2238                  (it was probably a friend function), the return the
2239                  good result.  */
2240               if (TREE_CODE (result) == CALL_EXPR)
2241                 return result;
2242
2243               while ((cp->h.code & EVIL_CODE) == 0)
2244                 {
2245                   /* non-standard uses: set the field to 0 to indicate
2246                      we are using a non-member function.  */
2247                   cp->u.field = 0;
2248                   if (cp->harshness[len].distance == 0
2249                       && cp->h.code < best)
2250                     best = cp->h.code;
2251                   cp += 1;
2252                 }
2253             }
2254         }
2255
2256       if (baselink)
2257         {
2258           /* We have a hit (of sorts). If the parameter list is
2259              "error_mark_node", or some variant thereof, it won't
2260              match any methods.  Since we have verified that the is
2261              some method vaguely matching this one (in name at least),
2262              silently return.
2263              
2264              Don't stop for friends, however.  */
2265           basetype_path = TREE_PURPOSE (baselink);
2266
2267           function = TREE_VALUE (baselink);
2268           if (TREE_CODE (basetype_path) == TREE_LIST)
2269             basetype_path = TREE_VALUE (basetype_path);
2270           basetype = BINFO_TYPE (basetype_path);
2271
2272           for (; function; function = DECL_CHAIN (function))
2273             {
2274 #ifdef GATHER_STATISTICS
2275               n_inner_fields_searched++;
2276 #endif
2277               ever_seen++;
2278               if (pass > 0)
2279                 found_fns = scratch_tree_cons (NULL_TREE, function, found_fns);
2280
2281               /* Not looking for friends here.  */
2282               if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
2283                   && ! DECL_STATIC_FUNCTION_P (function))
2284                 continue;
2285
2286               if (pass > 0)
2287                 {
2288                   tree these_parms = parms;
2289
2290 #ifdef GATHER_STATISTICS
2291                   n_inner_fields_searched++;
2292 #endif
2293                   cp->h_len = len;
2294                   cp->harshness = (struct harshness_code *)
2295                     alloca ((len + 1) * sizeof (struct harshness_code));
2296
2297                   if (DECL_STATIC_FUNCTION_P (function))
2298                     these_parms = TREE_CHAIN (these_parms);
2299                   compute_conversion_costs (function, these_parms, cp, len);
2300
2301                   if ((cp->h.code & EVIL_CODE) == 0)
2302                     {
2303                       cp->u.field = function;
2304                       cp->function = function;
2305                       cp->basetypes = basetype_path;
2306
2307                       /* Don't allow non-converting constructors to convert.  */
2308                       if (flags & LOOKUP_ONLYCONVERTING
2309                           && DECL_LANG_SPECIFIC (function)
2310                           && DECL_NONCONVERTING_P (function))
2311                         continue;
2312
2313                       /* No "two-level" conversions.  */
2314                       if (flags & LOOKUP_NO_CONVERSION
2315                           && (cp->h.code & USER_CODE))
2316                         continue;
2317
2318                       cp++;
2319                     }
2320                 }
2321             }
2322         }
2323
2324       if (pass == 0)
2325         {
2326           tree igv = lookup_name_nonclass (name);
2327
2328           /* No exact match could be found.  Now try to find match
2329              using default conversions.  */
2330           if ((flags & LOOKUP_GLOBAL) && igv)
2331             {
2332               if (TREE_CODE (igv) == FUNCTION_DECL)
2333                 ever_seen += 1;
2334               else if (TREE_CODE (igv) == TREE_LIST)
2335                 ever_seen += count_functions (igv);
2336             }
2337
2338           if (ever_seen == 0)
2339             {
2340               if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2341                   == LOOKUP_SPECULATIVELY)
2342                 return NULL_TREE;
2343               
2344               TREE_CHAIN (last) = void_list_node;
2345               if (flags & LOOKUP_GLOBAL)
2346                 cp_error ("no global or member function `%D(%A)' defined",
2347                           save_name, parmtypes);
2348               else
2349                 cp_error ("no member function `%T::%D(%A)' defined",
2350                           save_basetype, save_name, TREE_CHAIN (parmtypes));
2351               return error_mark_node;
2352             }
2353           continue;
2354         }
2355
2356       if (cp - candidates != 0)
2357         {
2358           /* Rank from worst to best.  Then cp will point to best one.
2359              Private fields have their bits flipped.  For unsigned
2360              numbers, this should make them look very large.
2361              If the best alternate has a (signed) negative value,
2362              then all we ever saw were private members.  */
2363           if (cp - candidates > 1)
2364             {
2365               int n_candidates = cp - candidates;
2366               extern int warn_synth;
2367               TREE_VALUE (parms) = instance_ptr;
2368               cp = ideal_candidate (candidates, n_candidates, len);
2369               if (cp == (struct candidate *)0)
2370                 {
2371                   if (flags & LOOKUP_COMPLAIN)
2372                     {
2373                       TREE_CHAIN (last) = void_list_node;
2374                       cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2375                                 name_kind, save_name, TREE_CHAIN (parmtypes));
2376                       print_n_candidates (candidates, n_candidates);
2377                     }
2378                   return error_mark_node;
2379                 }
2380               if (cp->h.code & EVIL_CODE)
2381                 return error_mark_node;
2382               if (warn_synth
2383                   && DECL_NAME (cp->function) == ansi_opname[MODIFY_EXPR]
2384                   && DECL_ARTIFICIAL (cp->function)
2385                   && n_candidates == 2)
2386                 {
2387                   cp_warning ("using synthesized `%#D' for copy assignment",
2388                               cp->function);
2389                   cp_warning_at ("  where cfront would use `%#D'",
2390                                  candidates->function);
2391                 }
2392             }
2393           else if (cp[-1].h.code & EVIL_CODE)
2394             {
2395               if (flags & LOOKUP_COMPLAIN)
2396                 cp_error ("ambiguous type conversion requested for %s `%D'",
2397                           name_kind, save_name);
2398               return error_mark_node;
2399             }
2400           else
2401             cp--;
2402
2403           /* The global function was the best, so use it.  */
2404           if (cp->u.field == 0)
2405             {
2406               /* We must convert the instance pointer into a reference type.
2407                  Global overloaded functions can only either take
2408                  aggregate objects (which come for free from references)
2409                  or reference data types anyway.  */
2410               TREE_VALUE (parms) = copy_node (instance_ptr);
2411               TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
2412               return build_function_call (cp->function, parms);
2413             }
2414
2415           function = cp->function;
2416           basetype_path = cp->basetypes;
2417           if (! DECL_STATIC_FUNCTION_P (function))
2418             TREE_VALUE (parms) = cp->arg;
2419           goto found_and_maybe_warn;
2420         }
2421
2422       if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
2423         {
2424           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2425               == LOOKUP_SPECULATIVELY)
2426             return NULL_TREE;
2427
2428           if (DECL_STATIC_FUNCTION_P (cp->function))
2429             parms = TREE_CHAIN (parms);
2430           if (ever_seen)
2431             {
2432               if (flags & LOOKUP_SPECULATIVELY)
2433                 return NULL_TREE;
2434               if (static_call_context
2435                   && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
2436                 cp_error ("object missing in call to `%D'", cp->function);
2437               else if (ever_seen > 1)
2438                 {
2439                   TREE_CHAIN (last) = void_list_node;
2440                   cp_error ("no matching function for call to `%T::%D (%A)%V'",
2441                             TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr))),
2442                             save_name, TREE_CHAIN (parmtypes),
2443                             TREE_TYPE (TREE_TYPE (instance_ptr)));
2444                   TREE_CHAIN (last) = NULL_TREE;
2445                   print_candidates (found_fns);
2446                 }
2447               else
2448                 report_type_mismatch (cp, parms, name_kind);
2449               return error_mark_node;
2450             }
2451
2452           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2453               == LOOKUP_COMPLAIN)
2454             {
2455               cp_error ("%T has no method named %D", save_basetype, save_name);
2456               return error_mark_node;
2457             }
2458           return NULL_TREE;
2459         }
2460       continue;
2461
2462     found_and_maybe_warn:
2463       if ((cp->harshness[0].code & CONST_CODE)
2464           /* 12.1p2: Constructors can be called for const objects.  */
2465           && ! DECL_CONSTRUCTOR_P (cp->function))
2466         {
2467           if (flags & LOOKUP_COMPLAIN)
2468             {
2469               cp_error_at ("non-const member function `%D'", cp->function);
2470               error ("called for const object at this point in file");
2471             }
2472           /* Not good enough for a match.  */
2473           else
2474             return error_mark_node;
2475         }
2476       goto found;
2477     }
2478   /* Silently return error_mark_node.  */
2479   return error_mark_node;
2480
2481  found:
2482   if (flags & LOOKUP_PROTECT)
2483     access = compute_access (basetype_path, function);
2484
2485   if (access == access_private_node)
2486     {
2487       if (flags & LOOKUP_COMPLAIN)
2488         {
2489           cp_error_at ("%s `%+#D' is %s", name_kind, function, 
2490                        TREE_PRIVATE (function) ? "private"
2491                        : "from private base class");
2492           error ("within this context");
2493         }
2494       return error_mark_node;
2495     }
2496   else if (access == access_protected_node)
2497     {
2498       if (flags & LOOKUP_COMPLAIN)
2499         {
2500           cp_error_at ("%s `%+#D' %s", name_kind, function,
2501                        TREE_PROTECTED (function) ? "is protected"
2502                        : "has protected accessibility");
2503           error ("within this context");
2504         }
2505       return error_mark_node;
2506     }
2507
2508   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2509      type (if it exists) is a pointer to.  */
2510
2511   if (DECL_ABSTRACT_VIRTUAL_P (function)
2512       && instance == current_class_ref
2513       && DECL_CONSTRUCTOR_P (current_function_decl)
2514       && ! (flags & LOOKUP_NONVIRTUAL)
2515       && value_member (function, get_abstract_virtuals (basetype)))
2516     cp_error ("abstract virtual `%#D' called from constructor", function);
2517
2518   if (IS_SIGNATURE (basetype))
2519     {
2520       if (static_call_context)
2521         {
2522           cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2523                     basetype, save_name);
2524           return error_mark_node;
2525         }
2526       return build_signature_method_call (function, parms);
2527     }
2528
2529   function = DECL_MAIN_VARIANT (function);
2530   mark_used (function);
2531
2532   fntype = TREE_TYPE (function);
2533   if (TREE_CODE (fntype) == POINTER_TYPE)
2534     fntype = TREE_TYPE (fntype);
2535   basetype = DECL_CLASS_CONTEXT (function);
2536
2537   /* If we are referencing a virtual function from an object
2538      of effectively static type, then there is no need
2539      to go through the virtual function table.  */
2540   if (need_vtbl == maybe_needed)
2541     {
2542       int fixed_type = resolves_to_fixed_type_p (instance, 0);
2543
2544       if (all_virtual == 1
2545           && DECL_VINDEX (function)
2546           && may_be_remote (basetype))
2547         need_vtbl = needed;
2548       else if (DECL_VINDEX (function))
2549         need_vtbl = fixed_type ? unneeded : needed;
2550       else
2551         need_vtbl = not_needed;
2552     }
2553
2554   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
2555       && !DECL_CONSTRUCTOR_P (function))
2556     {
2557       /* Let's be nasty to the user now, and give reasonable
2558          error messages.  */
2559       instance_ptr = current_class_ptr;
2560       if (instance_ptr)
2561         {
2562           if (basetype != current_class_type)
2563             {
2564               if (basetype == error_mark_node)
2565                 return error_mark_node;
2566               else 
2567                 {
2568                   if (orig_basetype != NULL_TREE)
2569                     error_not_base_type (orig_basetype, current_class_type);
2570                   else
2571                     error_not_base_type (function, current_class_type);
2572                   return error_mark_node;
2573                 }
2574             }
2575         }
2576       /* Only allow a static member function to call another static member
2577          function.  */
2578       else if (DECL_LANG_SPECIFIC (function)
2579                && !DECL_STATIC_FUNCTION_P (function))
2580         {
2581           cp_error ("cannot call member function `%D' without object",
2582                     function);
2583           return error_mark_node;
2584         }
2585     }
2586
2587   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2588
2589   if (TYPE_SIZE (complete_type (value_type)) == 0)
2590     {
2591       if (flags & LOOKUP_COMPLAIN)
2592         incomplete_type_error (0, value_type);
2593       return error_mark_node;
2594     }
2595
2596   if (DECL_STATIC_FUNCTION_P (function))
2597     parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2598                                TREE_CHAIN (parms), function, LOOKUP_NORMAL);
2599   else if (need_vtbl == unneeded)
2600     {
2601       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
2602       basetype = TREE_TYPE (instance);
2603       if (TYPE_METHOD_BASETYPE (TREE_TYPE (function))
2604           != TYPE_MAIN_VARIANT (basetype))
2605         {
2606           basetype = DECL_CLASS_CONTEXT (function);
2607           instance_ptr = convert_pointer_to (basetype, instance_ptr);
2608           instance = build_indirect_ref (instance_ptr, NULL_PTR);
2609         }
2610       parms = expr_tree_cons (NULL_TREE, instance_ptr,
2611                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, sub_flags));
2612     }
2613   else
2614     {
2615       if ((flags & LOOKUP_NONVIRTUAL) == 0)
2616         basetype = DECL_CONTEXT (function);
2617
2618       /* First parm could be integer_zerop with casts like
2619          ((Object*)0)->Object::IsA()  */
2620       if (!integer_zerop (TREE_VALUE (parms)))
2621         {
2622           /* Since we can't have inheritance with a union, doing get_binfo
2623              on it won't work.  We do all the convert_pointer_to_real
2624              stuff to handle MI correctly...for unions, that's not
2625              an issue, so we must short-circuit that extra work here.  */
2626           tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
2627           if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
2628             instance_ptr = TREE_VALUE (parms);
2629           else
2630             {
2631               tree binfo = get_binfo (basetype,
2632                                       TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
2633                                       0);
2634               instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
2635             }
2636           instance_ptr
2637             = convert_pointer_to (build_type_variant (basetype,
2638                                                       constp, volatilep),
2639                                   instance_ptr);
2640
2641           if (TREE_CODE (instance_ptr) == COND_EXPR)
2642             {
2643               instance_ptr = save_expr (instance_ptr);
2644               instance = build_indirect_ref (instance_ptr, NULL_PTR);
2645             }
2646           else if (TREE_CODE (instance_ptr) == NOP_EXPR
2647                    && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
2648                    && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
2649             ;
2650           /* The call to `convert_pointer_to' may return error_mark_node.  */
2651           else if (instance_ptr == error_mark_node)
2652             return instance_ptr;
2653           else if (instance == NULL_TREE
2654                    || TREE_CODE (instance) != INDIRECT_REF
2655                    || TREE_OPERAND (instance, 0) != instance_ptr)
2656             instance = build_indirect_ref (instance_ptr, NULL_PTR);
2657         }
2658       parms = expr_tree_cons (NULL_TREE, instance_ptr,
2659                          convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, LOOKUP_NORMAL));
2660     }
2661
2662   if (parms == error_mark_node
2663       || (parms && TREE_CHAIN (parms) == error_mark_node))
2664     return error_mark_node;
2665
2666   if (need_vtbl == needed)
2667     {
2668       function = build_vfn_ref (&TREE_VALUE (parms), instance,
2669                                 DECL_VINDEX (function));
2670       TREE_TYPE (function) = build_pointer_type (fntype);
2671     }
2672
2673   if (TREE_CODE (function) == FUNCTION_DECL)
2674     GNU_xref_call (current_function_decl,
2675                    IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
2676
2677   result = build_call (function, value_type, parms);
2678   if (IS_AGGR_TYPE (value_type))
2679     result = build_cplus_new (value_type, result);
2680   result = convert_from_reference (result);
2681   return result;
2682 }
2683
2684 /* Similar to `build_method_call', but for overloaded non-member functions.
2685    The name of this function comes through NAME.  The name depends
2686    on PARMS.
2687
2688    Note that this function must handle simple `C' promotions,
2689    as well as variable numbers of arguments (...), and
2690    default arguments to boot.
2691
2692    If the overloading is successful, we return a tree node which
2693    contains the call to the function.
2694
2695    If overloading produces candidates which are probable, but not definite,
2696    we hold these candidates.  If FINAL_CP is non-zero, then we are free
2697    to assume that final_cp points to enough storage for all candidates that
2698    this function might generate.  The `harshness' array is preallocated for
2699    the first candidate, but not for subsequent ones.
2700
2701    Note that the DECL_RTL of FUNCTION must be made to agree with this
2702    function's new name.  */
2703
2704 tree
2705 build_overload_call_real (fnname, parms, flags, final_cp, require_complete)
2706      tree fnname, parms;
2707      int flags;
2708      struct candidate *final_cp;
2709      int require_complete;
2710 {
2711   /* must check for overloading here */
2712   tree functions, function;
2713   tree parmtypes, last;
2714   register tree outer;
2715   int length;
2716   int parmlength = list_length (parms);
2717
2718   struct candidate *candidates, *cp;
2719
2720   if (final_cp)
2721     {
2722       final_cp[0].h.code = 0;
2723       final_cp[0].h.distance = 0;
2724       final_cp[0].function = 0;
2725       /* end marker.  */
2726       final_cp[1].h.code = EVIL_CODE;
2727     }
2728
2729   parmtypes = default_parm_conversions (parms, &last);
2730   if (parmtypes == error_mark_node)
2731     {
2732       if (final_cp)
2733         final_cp->h.code = EVIL_CODE;
2734       return error_mark_node;
2735     }
2736
2737   if (last)
2738     TREE_CHAIN (last) = void_list_node;
2739   else
2740     parmtypes = void_list_node;
2741
2742   if (is_overloaded_fn (fnname))
2743     {
2744       functions = fnname;
2745       if (TREE_CODE (fnname) == TREE_LIST)
2746         fnname = TREE_PURPOSE (functions);
2747       else if (TREE_CODE (fnname) == FUNCTION_DECL)
2748         fnname = DECL_NAME (functions);
2749     }
2750   else 
2751     functions = lookup_name_nonclass (fnname);
2752
2753   if (functions == NULL_TREE)
2754     {
2755       if (flags & LOOKUP_SPECULATIVELY)
2756         return NULL_TREE;
2757       if (flags & LOOKUP_COMPLAIN)
2758         error ("only member functions apply");
2759       if (final_cp)
2760         final_cp->h.code = EVIL_CODE;
2761       return error_mark_node;
2762     }
2763
2764   if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
2765     {
2766       functions = DECL_MAIN_VARIANT (functions);
2767       if (final_cp)
2768         {
2769           /* We are just curious whether this is a viable alternative or
2770              not.  */
2771           compute_conversion_costs (functions, parms, final_cp, parmlength);
2772           return functions;
2773         }
2774       else
2775         return build_function_call_real (functions, parms, 1, flags);
2776     }
2777
2778   if (TREE_CODE (functions) == TREE_LIST
2779       && TREE_VALUE (functions) == NULL_TREE)
2780     {
2781       if (flags & LOOKUP_SPECULATIVELY)
2782         return NULL_TREE;
2783       
2784       if (flags & LOOKUP_COMPLAIN)
2785         cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2786                   TREE_PURPOSE (functions));
2787       if (final_cp)
2788         final_cp->h.code = EVIL_CODE;
2789       return error_mark_node;
2790     }
2791
2792   length = count_functions (functions);
2793   
2794   if (final_cp)
2795     candidates = final_cp;
2796   else
2797     {
2798       candidates
2799         = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
2800       bzero ((char *) candidates, (length + 1) * sizeof (struct candidate));
2801     }
2802
2803   cp = candidates;
2804
2805   my_friendly_assert (is_overloaded_fn (functions), 169);
2806
2807   functions = get_first_fn (functions);
2808
2809   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
2810   for (outer = functions; outer; outer = DECL_CHAIN (outer))
2811     {
2812       int template_cost = 0;
2813       function = outer;
2814       if (TREE_CODE (function) != FUNCTION_DECL
2815           && ! (TREE_CODE (function) == TEMPLATE_DECL
2816                 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
2817         {
2818           enum tree_code code = TREE_CODE (function);
2819           if (code == TEMPLATE_DECL)
2820             code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
2821           if (code == CONST_DECL)
2822             cp_error_at
2823               ("enumeral value `%D' conflicts with function of same name",
2824                function);
2825           else if (code == VAR_DECL)
2826             {
2827               if (TREE_STATIC (function))
2828                 cp_error_at
2829                   ("variable `%D' conflicts with function of same name",
2830                    function);
2831               else
2832                 cp_error_at
2833                   ("constant field `%D' conflicts with function of same name",
2834                    function);
2835             }
2836           else if (code == TYPE_DECL)
2837             continue;
2838           else
2839             my_friendly_abort (2);
2840           error ("at this point in file");
2841           continue;
2842         }
2843       if (TREE_CODE (function) == TEMPLATE_DECL)
2844         {
2845           int ntparms = DECL_NTPARMS (function);
2846           tree targs = make_scratch_vec (ntparms);
2847           int i;
2848
2849           i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (function),
2850                                 &TREE_VEC_ELT (targs, 0),
2851                                 TYPE_ARG_TYPES (TREE_TYPE (function)),
2852                                 parms, NULL_TREE, 0, 0);
2853           if (i == 0)
2854             {
2855               function = instantiate_template (function, targs);
2856               if (function == error_mark_node)
2857                 return function;
2858             }
2859         }
2860
2861       if (TREE_CODE (function) == TEMPLATE_DECL)
2862         {
2863           /* Unconverted template -- failed match.  */
2864           cp->function = function;
2865           cp->u.bad_arg = -4;
2866           cp->h.code = EVIL_CODE;
2867         }
2868       else
2869         {
2870           struct candidate *cp2;
2871
2872           /* Check that this decl is not the same as a function that's in
2873              the list due to some template instantiation.  */
2874           cp2 = candidates;
2875           while (cp2 != cp)
2876             if (cp2->function == function)
2877               break;
2878             else
2879               cp2 += 1;
2880           if (cp2->function == function)
2881             continue;
2882
2883           function = DECL_MAIN_VARIANT (function);
2884
2885           /* Can't use alloca here, since result might be
2886              passed to calling function.  */
2887           cp->h_len = parmlength;
2888           cp->harshness = (struct harshness_code *)
2889             scratchalloc ((parmlength + 1) * sizeof (struct harshness_code));
2890
2891           compute_conversion_costs (function, parms, cp, parmlength);
2892
2893           /* Make sure this is clear as well.  */
2894           cp->h.int_penalty += template_cost;
2895
2896           if ((cp[0].h.code & EVIL_CODE) == 0)
2897             {
2898               cp[1].h.code = EVIL_CODE;
2899               cp++;
2900             }
2901         }
2902     }
2903
2904   if (cp - candidates)
2905     {
2906       tree rval = error_mark_node;
2907
2908       /* Leave marker.  */
2909       cp[0].h.code = EVIL_CODE;
2910       if (cp - candidates > 1)
2911         {
2912           struct candidate *best_cp
2913             = ideal_candidate (candidates, cp - candidates, parmlength);
2914           if (best_cp == (struct candidate *)0)
2915             {
2916               if (flags & LOOKUP_COMPLAIN)
2917                 {
2918                   cp_error ("call of overloaded `%D' is ambiguous", fnname);
2919                   print_n_candidates (candidates, cp - candidates);
2920                 }
2921               return error_mark_node;
2922             }
2923           else
2924             rval = best_cp->function;
2925         }
2926       else
2927         {
2928           cp -= 1;
2929           if (cp->h.code & EVIL_CODE)
2930             {
2931               if (flags & LOOKUP_COMPLAIN)
2932                 error ("type conversion ambiguous");
2933             }
2934           else
2935             rval = cp->function;
2936         }
2937
2938       if (final_cp)
2939         return rval;
2940
2941       return build_function_call_real (rval, parms, require_complete, flags);
2942     }
2943
2944   if (flags & LOOKUP_SPECULATIVELY)
2945     return NULL_TREE;
2946   
2947   if (flags & LOOKUP_COMPLAIN)
2948     report_type_mismatch (cp, parms, "function");
2949
2950   return error_mark_node;
2951 }
2952
2953 /* This requires a complete type on the result of the call.  */
2954
2955 tree
2956 build_overload_call (fnname, parms, flags)
2957      tree fnname, parms;
2958      int flags;
2959 {
2960   return build_overload_call_real (fnname, parms, flags, (struct candidate *)0, 1);
2961 }
2962
2963 /* New overloading code.  */
2964
2965 struct z_candidate {
2966   tree fn;
2967   tree convs;
2968   tree second_conv;
2969   int viable;
2970   tree basetype_path;
2971   tree template;
2972   struct z_candidate *next;
2973 };
2974
2975 #define IDENTITY_RANK 0
2976 #define EXACT_RANK 1
2977 #define PROMO_RANK 2
2978 #define STD_RANK 3
2979 #define PBOOL_RANK 4
2980 #define USER_RANK 5
2981 #define ELLIPSIS_RANK 6
2982 #define BAD_RANK 7
2983
2984 #define ICS_RANK(NODE)                          \
2985   (ICS_BAD_FLAG (NODE) ? BAD_RANK   \
2986    : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK   \
2987    : ICS_USER_FLAG (NODE) ? USER_RANK           \
2988    : ICS_STD_RANK (NODE))
2989
2990 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
2991
2992 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
2993 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
2994 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
2995 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
2996
2997 #define USER_CONV_FN(NODE) TREE_OPERAND (NODE, 1)
2998
2999 int
3000 null_ptr_cst_p (t)
3001      tree t;
3002 {
3003   if (t == null_node
3004       || (integer_zerop (t) && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE))
3005     return 1;
3006   return 0;
3007 }
3008
3009 static tree
3010 build_conv (code, type, from)
3011      enum tree_code code;
3012      tree type, from;
3013 {
3014   tree t = build1 (code, type, from);
3015   int rank = ICS_STD_RANK (from);
3016   switch (code)
3017     {
3018     case PTR_CONV:
3019     case PMEM_CONV:
3020     case BASE_CONV:
3021     case STD_CONV:
3022       if (rank < STD_RANK)
3023         rank = STD_RANK;
3024       break;
3025
3026     case QUAL_CONV:
3027       if (rank < EXACT_RANK)
3028         rank = EXACT_RANK;
3029
3030     default:
3031       break;
3032     }
3033   ICS_STD_RANK (t) = rank;
3034   ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
3035   ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
3036   return t;
3037 }
3038
3039 static tree
3040 non_reference (t)
3041      tree t;
3042 {
3043   if (TREE_CODE (t) == REFERENCE_TYPE)
3044     t = TREE_TYPE (t);
3045   return t;
3046 }
3047
3048 static tree
3049 strip_top_quals (t)
3050      tree t;
3051 {
3052   if (TREE_CODE (t) == ARRAY_TYPE)
3053     return t;
3054   return TYPE_MAIN_VARIANT (t);
3055 }
3056
3057 /* Returns the standard conversion path (see [conv]) from type FROM to type
3058    TO, if any.  For proper handling of null pointer constants, you must
3059    also pass the expression EXPR to convert from.  */
3060
3061 static tree
3062 standard_conversion (to, from, expr)
3063      tree to, from, expr;
3064 {
3065   enum tree_code fcode, tcode;
3066   tree conv;
3067   int fromref = 0;
3068
3069   if (TREE_CODE (to) == REFERENCE_TYPE)
3070     to = TREE_TYPE (to);
3071   if (TREE_CODE (from) == REFERENCE_TYPE)
3072     {
3073       fromref = 1;
3074       from = TREE_TYPE (from);
3075     }
3076   to = strip_top_quals (to);
3077   from = strip_top_quals (from);
3078
3079   fcode = TREE_CODE (from);
3080   tcode = TREE_CODE (to);
3081
3082   conv = build1 (IDENTITY_CONV, from, expr);
3083
3084   if (fcode == FUNCTION_TYPE)
3085     {
3086       from = build_pointer_type (from);
3087       fcode = TREE_CODE (from);
3088       conv = build_conv (LVALUE_CONV, from, conv);
3089     }
3090   else if (fcode == ARRAY_TYPE)
3091     {
3092       from = build_pointer_type (TREE_TYPE (from));
3093       fcode = TREE_CODE (from);
3094       conv = build_conv (LVALUE_CONV, from, conv);
3095     }
3096   else if (fromref || (expr && real_lvalue_p (expr)))
3097     conv = build_conv (RVALUE_CONV, from, conv);
3098
3099   if (from == to)
3100     return conv;
3101
3102   if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
3103       && expr && null_ptr_cst_p (expr))
3104     {
3105       conv = build_conv (STD_CONV, to, conv);
3106     }
3107   else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
3108     {
3109       enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
3110       enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
3111
3112       if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
3113           && ufcode != FUNCTION_TYPE)
3114         {
3115           from = build_pointer_type
3116             (cp_build_type_variant (void_type_node,
3117                                     TYPE_READONLY (TREE_TYPE (from)),
3118                                     TYPE_VOLATILE (TREE_TYPE (from))));
3119           conv = build_conv (PTR_CONV, from, conv);
3120         }
3121       else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
3122         {
3123           tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
3124           tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
3125
3126           if (DERIVED_FROM_P (fbase, tbase)
3127               && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
3128                              TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))),
3129                              1)))
3130             {
3131               from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
3132               from = build_pointer_type (from);
3133               conv = build_conv (PMEM_CONV, from, conv);
3134             }
3135         }
3136       else if (IS_AGGR_TYPE (TREE_TYPE (from))
3137                && IS_AGGR_TYPE (TREE_TYPE (to)))
3138         {
3139           if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
3140             {
3141               from = cp_build_type_variant (TREE_TYPE (to),
3142                                             TYPE_READONLY (TREE_TYPE (from)),
3143                                             TYPE_VOLATILE (TREE_TYPE (from)));
3144               from = build_pointer_type (from);
3145               conv = build_conv (PTR_CONV, from, conv);
3146             }
3147         }
3148
3149       if (comptypes (from, to, 1))
3150         /* OK */;
3151       else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
3152         conv = build_conv (QUAL_CONV, to, conv);
3153       else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
3154         {
3155           conv = build_conv (PTR_CONV, to, conv);
3156           ICS_BAD_FLAG (conv) = 1;
3157         }
3158       else
3159         return 0;
3160
3161       from = to;
3162     }
3163   else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
3164     {
3165       tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
3166       tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
3167       tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
3168       tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
3169
3170       if (! DERIVED_FROM_P (fbase, tbase)
3171           || ! comptypes (TREE_TYPE (fromfn), TREE_TYPE (tofn), 1)
3172           || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
3173                           TREE_CHAIN (TYPE_ARG_TYPES (tofn)), 1)
3174           || TYPE_READONLY (fbase) != TYPE_READONLY (tbase)
3175           || TYPE_VOLATILE (fbase) != TYPE_VOLATILE (tbase))
3176         return 0;
3177
3178       from = cp_build_type_variant (tbase, TYPE_READONLY (fbase),
3179                                     TYPE_VOLATILE (fbase));
3180       from = build_cplus_method_type (from, TREE_TYPE (fromfn),
3181                                       TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
3182       from = build_ptrmemfunc_type (build_pointer_type (from));
3183       conv = build_conv (PMEM_CONV, from, conv);
3184     }
3185   else if (tcode == BOOLEAN_TYPE)
3186     {
3187       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
3188              || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
3189         return 0;
3190
3191       conv = build_conv (STD_CONV, to, conv);
3192       if (fcode == POINTER_TYPE
3193           || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
3194         ICS_STD_RANK (conv) = PBOOL_RANK;
3195     }
3196   /* We don't check for ENUMERAL_TYPE here because there are no standard
3197      conversions to enum type.  */
3198   else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
3199            || tcode == REAL_TYPE)
3200     {
3201       if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
3202         return 0;
3203       conv = build_conv (STD_CONV, to, conv);
3204
3205       /* Give this a better rank if it's a promotion.  */
3206       if (to == type_promotes_to (from)
3207           && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
3208         ICS_STD_RANK (conv) = PROMO_RANK;
3209     }
3210   else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
3211            && DERIVED_FROM_P (to, from))
3212     {
3213       if (TREE_CODE (conv) == RVALUE_CONV)
3214         conv = TREE_OPERAND (conv, 0);
3215       conv = build_conv (BASE_CONV, to, conv);
3216     }
3217   else
3218     return 0;
3219
3220   return conv;
3221 }
3222
3223 /* Returns the conversion path from type FROM to reference type TO for
3224    purposes of reference binding.  For lvalue binding, either pass a
3225    reference type to FROM or an lvalue expression to EXPR.
3226
3227    Currently does not distinguish in the generated trees between binding to
3228    an lvalue and a temporary.  Should it?  */
3229
3230 static tree
3231 reference_binding (rto, rfrom, expr, flags)
3232      tree rto, rfrom, expr;
3233      int flags;
3234 {
3235   tree conv;
3236   int lvalue = 1;
3237   tree to = TREE_TYPE (rto);
3238   tree from = rfrom;
3239   int related;
3240
3241   if (TREE_CODE (from) == REFERENCE_TYPE)
3242     from = TREE_TYPE (from);
3243   else if (! expr || ! real_lvalue_p (expr))
3244     lvalue = 0;
3245
3246   related = (comptypes (TYPE_MAIN_VARIANT (to),
3247                         TYPE_MAIN_VARIANT (from), 1)
3248              || (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
3249                  && DERIVED_FROM_P (to, from)));
3250
3251   if (lvalue && related
3252       && TYPE_READONLY (to) >= TYPE_READONLY (from)
3253       && TYPE_VOLATILE (to) >= TYPE_VOLATILE (from))
3254     {
3255       conv = build1 (IDENTITY_CONV, from, expr);
3256
3257       if (comptypes (TYPE_MAIN_VARIANT (to),
3258                      TYPE_MAIN_VARIANT (from), 1))
3259         conv = build_conv (REF_BIND, rto, conv);
3260       else
3261         {
3262           conv = build_conv (REF_BIND, rto, conv);
3263           ICS_STD_RANK (conv) = STD_RANK;
3264         }
3265     }
3266   else
3267     conv = NULL_TREE;
3268
3269   if (! conv)
3270     {
3271       conv = standard_conversion (to, rfrom, expr);
3272       if (conv)
3273         {
3274           conv = build_conv (REF_BIND, rto, conv);
3275
3276           /* Bind directly to a base subobject of a class rvalue.  Do it
3277              after building the conversion for proper handling of ICS_RANK.  */
3278           if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
3279             TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
3280         }
3281       if (conv
3282           && ((! (TYPE_READONLY (to) && ! TYPE_VOLATILE (to)
3283                   && (flags & LOOKUP_NO_TEMP_BIND) == 0))
3284               /* If T1 is reference-related to T2, cv1 must be the same
3285                  cv-qualification as, or greater cv-qualification than,
3286                  cv2; otherwise, the program is ill-formed.  */
3287               || (related
3288                   && (TYPE_READONLY (to) < TYPE_READONLY (from)
3289                       || TYPE_VOLATILE (to) < TYPE_VOLATILE (from)))))
3290         ICS_BAD_FLAG (conv) = 1;
3291     }
3292
3293   return conv;
3294 }
3295
3296 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
3297    to type TO.  The optional expression EXPR may affect the conversion.
3298    FLAGS are the usual overloading flags.  Only LOOKUP_NO_CONVERSION is
3299    significant.  */
3300
3301 static tree
3302 implicit_conversion (to, from, expr, flags)
3303      tree to, from, expr;
3304      int flags;
3305 {
3306   tree conv;
3307   struct z_candidate *cand;
3308
3309   if (expr && type_unknown_p (expr))
3310     {
3311       expr = instantiate_type (to, expr, 0);
3312       if (expr == error_mark_node)
3313         return 0;
3314       from = TREE_TYPE (expr);
3315     }
3316
3317   if (TREE_CODE (to) == REFERENCE_TYPE)
3318     conv = reference_binding (to, from, expr, flags);
3319   else
3320     conv = standard_conversion (to, from, expr);
3321
3322   if (conv)
3323     ;
3324   else if (expr != NULL_TREE
3325            && (IS_AGGR_TYPE (non_reference (from))
3326             || IS_AGGR_TYPE (non_reference (to)))
3327            && (flags & LOOKUP_NO_CONVERSION) == 0)
3328     {
3329       cand = build_user_type_conversion_1
3330         (to, expr, LOOKUP_ONLYCONVERTING);
3331       if (cand)
3332         conv = cand->second_conv;
3333       if ((! conv || ICS_BAD_FLAG (conv))
3334           && TREE_CODE (to) == REFERENCE_TYPE
3335           && (flags & LOOKUP_NO_TEMP_BIND) == 0)
3336         {
3337           cand = build_user_type_conversion_1
3338             (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
3339           if (cand)
3340             {
3341               if (! TYPE_READONLY (TREE_TYPE (to))
3342                   || TYPE_VOLATILE (TREE_TYPE (to)))
3343                 ICS_BAD_FLAG (cand->second_conv) = 1;
3344               if (!conv || (ICS_BAD_FLAG (conv)
3345                             > ICS_BAD_FLAG (cand->second_conv)))
3346                 conv = build_conv (REF_BIND, to, cand->second_conv);
3347             }
3348         }
3349     }
3350
3351   return conv;
3352 }
3353
3354 /* Create an overload candidate for the function or method FN called with
3355    the argument list ARGLIST and add it to CANDIDATES.  FLAGS is passed on
3356    to implicit_conversion.  */
3357
3358 static struct z_candidate *
3359 add_function_candidate (candidates, fn, arglist, flags)
3360      struct z_candidate *candidates;
3361      tree fn, arglist;
3362      int flags;
3363 {
3364   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
3365   int i, len;
3366   tree convs;
3367   tree parmnode = parmlist;
3368   tree argnode = arglist;
3369   int viable = 1;
3370   struct z_candidate *cand;
3371
3372   /* The `this' and `in_chrg' arguments to constructors are not considered
3373      in overload resolution.  */
3374   if (DECL_CONSTRUCTOR_P (fn))
3375     {
3376       parmnode = TREE_CHAIN (parmnode);
3377       argnode = TREE_CHAIN (argnode);
3378       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3379         {
3380           parmnode = TREE_CHAIN (parmnode);
3381           argnode = TREE_CHAIN (argnode);
3382         }
3383     }
3384
3385   len = list_length (argnode);
3386   convs = make_scratch_vec (len);
3387
3388   for (i = 0; i < len; ++i)
3389     {
3390       tree arg = TREE_VALUE (argnode);
3391       tree argtype = TREE_TYPE (arg);
3392       tree t;
3393
3394       argtype = cp_build_type_variant
3395         (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
3396
3397       if (parmnode == void_list_node)
3398         break;
3399       else if (parmnode)
3400         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
3401       else
3402         {
3403           t = build1 (IDENTITY_CONV, argtype, arg);
3404           ICS_ELLIPSIS_FLAG (t) = 1;
3405         }
3406
3407       if (i == 0 && t && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
3408           && ! DECL_CONSTRUCTOR_P (fn))
3409         ICS_THIS_FLAG (t) = 1;
3410
3411       TREE_VEC_ELT (convs, i) = t;
3412       if (! t)
3413         break;
3414
3415       if (ICS_BAD_FLAG (t))
3416         viable = -1;
3417
3418       if (parmnode)
3419         parmnode = TREE_CHAIN (parmnode);
3420       argnode = TREE_CHAIN (argnode);
3421     }
3422
3423   if (i < len)
3424     viable = 0;
3425
3426   /* Make sure there are default args for the rest of the parms.  */
3427   for (; parmnode && parmnode != void_list_node;
3428        parmnode = TREE_CHAIN (parmnode))
3429     if (! TREE_PURPOSE (parmnode))
3430       {
3431         viable = 0;
3432         break;
3433       }
3434
3435   cand = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
3436
3437   cand->fn = fn;
3438   cand->convs = convs;
3439   cand->second_conv = NULL_TREE;
3440   cand->viable = viable;
3441   cand->basetype_path = NULL_TREE;
3442   cand->template = NULL_TREE;
3443   cand->next = candidates;
3444
3445   return cand;
3446 }
3447
3448 /* Create an overload candidate for the conversion function FN which will
3449    be invoked for expression OBJ, producing a pointer-to-function which
3450    will in turn be called with the argument list ARGLIST, and add it to
3451    CANDIDATES.  FLAGS is passed on to implicit_conversion.  */
3452
3453 static struct z_candidate *
3454 add_conv_candidate (candidates, fn, obj, arglist)
3455      struct z_candidate *candidates;
3456      tree fn, obj, arglist;
3457 {
3458   tree totype = TREE_TYPE (TREE_TYPE (fn));
3459   tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
3460   int i, len = list_length (arglist) + 1;
3461   tree convs = make_scratch_vec (len);
3462   tree parmnode = parmlist;
3463   tree argnode = arglist;
3464   int viable = 1;
3465   struct z_candidate *cand;
3466   int flags = LOOKUP_NORMAL;
3467
3468   for (i = 0; i < len; ++i)
3469     {
3470       tree arg = i == 0 ? obj : TREE_VALUE (argnode);
3471       tree argtype = lvalue_type (arg);
3472       tree t;
3473
3474       if (i == 0)
3475         t = implicit_conversion (totype, argtype, arg, flags);
3476       else if (parmnode == void_list_node)
3477         break;
3478       else if (parmnode)
3479         t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
3480       else
3481         {
3482           t = build1 (IDENTITY_CONV, argtype, arg);
3483           ICS_ELLIPSIS_FLAG (t) = 1;
3484         }
3485
3486       TREE_VEC_ELT (convs, i) = t;
3487       if (! t)
3488         break;
3489
3490       if (ICS_BAD_FLAG (t))
3491         viable = -1;
3492
3493       if (i == 0)
3494         continue;
3495
3496       if (parmnode)
3497         parmnode = TREE_CHAIN (parmnode);
3498       argnode = TREE_CHAIN (argnode);
3499     }
3500
3501   if (i < len)
3502     viable = 0;
3503
3504   for (; parmnode && parmnode != void_list_node;
3505        parmnode = TREE_CHAIN (parmnode))
3506     if (! TREE_PURPOSE (parmnode))
3507       {
3508         viable = 0;
3509         break;
3510       }
3511
3512   cand = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
3513
3514   cand->fn = fn;
3515   cand->convs = convs;
3516   cand->second_conv = NULL_TREE;
3517   cand->viable = viable;
3518   cand->basetype_path = NULL_TREE;
3519   cand->template = NULL_TREE;
3520   cand->next = candidates;
3521
3522   return cand;
3523 }
3524
3525 static struct z_candidate *
3526 build_builtin_candidate (candidates, fnname, type1, type2,
3527                          args, argtypes, flags)
3528      struct z_candidate *candidates;
3529      tree fnname, type1, type2, *args, *argtypes;
3530      int flags;
3531
3532 {
3533   tree t, convs;
3534   int viable = 1, i;
3535   struct z_candidate *cand;
3536   tree types[2];
3537
3538   types[0] = type1;
3539   types[1] = type2;
3540
3541   convs = make_scratch_vec (args[2] ? 3 : (args[1] ? 2 : 1));
3542
3543   for (i = 0; i < 2; ++i)
3544     {
3545       if (! args[i])
3546         break;
3547
3548       t = implicit_conversion (types[i], argtypes[i], args[i], flags);
3549       if (! t)
3550         {
3551           viable = 0;
3552           /* We need something for printing the candidate.  */
3553           t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
3554         }
3555       else if (ICS_BAD_FLAG (t))
3556         viable = 0;
3557       TREE_VEC_ELT (convs, i) = t;
3558     }
3559
3560   /* For COND_EXPR we rearranged the arguments; undo that now.  */
3561   if (args[2])
3562     {
3563       TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
3564       TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
3565       t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
3566       if (t)
3567         TREE_VEC_ELT (convs, 0) = t;
3568       else
3569         viable = 0;
3570     }      
3571
3572   cand = (struct z_candidate *) scratchalloc (sizeof (struct z_candidate));
3573
3574   cand->fn = fnname;
3575   cand->convs = convs;
3576   cand->second_conv = NULL_TREE;
3577   cand->viable = viable;
3578   cand->basetype_path = NULL_TREE;
3579   cand->template = NULL_TREE;
3580   cand->next = candidates;
3581
3582   return cand;
3583 }
3584
3585 static int
3586 is_complete (t)
3587      tree t;
3588 {
3589   return TYPE_SIZE (complete_type (t)) != NULL_TREE;
3590 }
3591
3592 /* Create any builtin operator overload candidates for the operator in
3593    question given the converted operand types TYPE1 and TYPE2.  The other
3594    args are passed through from add_builtin_candidates to
3595    build_builtin_candidate.  */
3596
3597 static struct z_candidate *
3598 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
3599                        args, argtypes, flags)
3600      struct z_candidate *candidates;
3601      enum tree_code code, code2;
3602      tree fnname, type1, type2, *args, *argtypes;
3603      int flags;
3604 {
3605   switch (code)
3606     {
3607     case POSTINCREMENT_EXPR:
3608     case POSTDECREMENT_EXPR:
3609       args[1] = integer_zero_node;
3610       type2 = integer_type_node;
3611       break;
3612     default:
3613       break;
3614     }
3615
3616   switch (code)
3617     {
3618
3619 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
3620      and  VQ  is  either  volatile or empty, there exist candidate operator
3621      functions of the form
3622              VQ T&   operator++(VQ T&);
3623              T       operator++(VQ T&, int);
3624    5 For every pair T, VQ), where T is an enumeration type or an arithmetic
3625      type  other than bool, and VQ is either volatile or empty, there exist
3626      candidate operator functions of the form
3627              VQ T&   operator--(VQ T&);
3628              T       operator--(VQ T&, int);
3629    6 For every pair T, VQ), where T is  a  cv-qualified  or  cv-unqualified
3630      complete  object type, and VQ is either volatile or empty, there exist
3631      candidate operator functions of the form
3632              T*VQ&   operator++(T*VQ&);
3633              T*VQ&   operator--(T*VQ&);
3634              T*      operator++(T*VQ&, int);
3635              T*      operator--(T*VQ&, int);  */
3636
3637     case POSTDECREMENT_EXPR:
3638     case PREDECREMENT_EXPR:
3639       if (TREE_CODE (type1) == BOOLEAN_TYPE)
3640         return candidates;
3641     case POSTINCREMENT_EXPR:
3642     case PREINCREMENT_EXPR:
3643       if ((ARITHMETIC_TYPE_P (type1) && TREE_CODE (type1) != ENUMERAL_TYPE)
3644           || TYPE_PTROB_P (type1))
3645         {
3646           type1 = build_reference_type (type1);
3647           break;
3648         }
3649       return candidates;
3650
3651 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
3652      exist candidate operator functions of the form
3653
3654              T&      operator*(T*);
3655
3656    8 For every function type T, there exist candidate operator functions of
3657      the form
3658              T&      operator*(T*);  */
3659
3660     case INDIRECT_REF:
3661       if (TREE_CODE (type1) == POINTER_TYPE
3662           && (TYPE_PTROB_P (type1)
3663               || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
3664         break;
3665       return candidates;
3666
3667 /* 9 For every type T, there exist candidate operator functions of the form
3668              T*      operator+(T*);
3669
3670    10For  every  promoted arithmetic type T, there exist candidate operator
3671      functions of the form
3672              T       operator+(T);
3673              T       operator-(T);  */
3674
3675     case CONVERT_EXPR: /* unary + */
3676       if (TREE_CODE (type1) == POINTER_TYPE
3677           && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
3678         break;
3679     case NEGATE_EXPR:
3680       if (ARITHMETIC_TYPE_P (type1))
3681         break;
3682       return candidates;
3683
3684 /* 11For every promoted integral type T,  there  exist  candidate  operator
3685      functions of the form
3686              T       operator~(T);  */
3687
3688     case BIT_NOT_EXPR:
3689       if (INTEGRAL_TYPE_P (type1))
3690         break;
3691       return candidates;
3692
3693 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
3694      is the same type as C2 or is a derived class of C2, T  is  a  complete
3695      object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
3696      there exist candidate operator functions of the form
3697              CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3698      where CV12 is the union of CV1 and CV2.  */
3699
3700     case MEMBER_REF:
3701       if (TREE_CODE (type1) == POINTER_TYPE
3702           && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
3703         {
3704           tree c1 = TREE_TYPE (type1);
3705           tree c2 = (TYPE_PTRMEMFUNC_P (type2)
3706                      ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
3707                      : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
3708
3709           if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
3710               && (TYPE_PTRMEMFUNC_P (type2)
3711                   || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
3712             break;
3713         }
3714       return candidates;
3715
3716 /* 13For every pair of promoted arithmetic types L and R, there exist  can-
3717      didate operator functions of the form
3718              LR      operator*(L, R);
3719              LR      operator/(L, R);
3720              LR      operator+(L, R);
3721              LR      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              bool    operator!=(L, R);
3728      where  LR  is  the  result of the usual arithmetic conversions between
3729      types L and R.
3730
3731    14For every pair of types T and I, where T  is  a  cv-qualified  or  cv-
3732      unqualified  complete  object  type and I is a promoted integral type,
3733      there exist candidate operator functions of the form
3734              T*      operator+(T*, I);
3735              T&      operator[](T*, I);
3736              T*      operator-(T*, I);
3737              T*      operator+(I, T*);
3738              T&      operator[](I, T*);
3739
3740    15For every T, where T is a pointer to complete object type, there exist
3741      candidate operator functions of the form112)
3742              ptrdiff_t operator-(T, T);
3743
3744    16For  every pointer type T, there exist candidate operator functions of
3745      the form
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              bool    operator!=(T, T);
3752
3753    17For every pointer to member type T,  there  exist  candidate  operator
3754      functions of the form
3755              bool    operator==(T, T);
3756              bool    operator!=(T, T);  */
3757
3758     case MINUS_EXPR:
3759       if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
3760         break;
3761       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
3762         {
3763           type2 = ptrdiff_type_node;
3764           break;
3765         }
3766     case MULT_EXPR:
3767     case TRUNC_DIV_EXPR:
3768       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3769         break;
3770       return candidates;
3771
3772     case EQ_EXPR:
3773     case NE_EXPR:
3774       if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3775           || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
3776         break;
3777       if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
3778           && null_ptr_cst_p (args[1]))
3779         {
3780           type2 = type1;
3781           break;
3782         }
3783       if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
3784           && null_ptr_cst_p (args[0]))
3785         {
3786           type1 = type2;
3787           break;
3788         }
3789     case LT_EXPR:
3790     case GT_EXPR:
3791     case LE_EXPR:
3792     case GE_EXPR:
3793     case MAX_EXPR:
3794     case MIN_EXPR:
3795       if ((ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3796           || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)))
3797         break;
3798       if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
3799         {
3800           type2 = type1;
3801           break;
3802         }
3803       if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
3804         {
3805           type1 = type2;
3806           break;
3807         }
3808       return candidates;
3809
3810     case PLUS_EXPR:
3811       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3812         break;
3813     case ARRAY_REF:
3814       if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
3815         {
3816           type1 = ptrdiff_type_node;
3817           break;
3818         }
3819       if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
3820         {
3821           type2 = ptrdiff_type_node;
3822           break;
3823         }
3824       return candidates;
3825
3826 /* 18For  every pair of promoted integral types L and R, there exist candi-
3827      date operator functions of the form
3828              LR      operator%(L, R);
3829              LR      operator&(L, R);
3830              LR      operator^(L, R);
3831              LR      operator|(L, R);
3832              L       operator<<(L, R);
3833              L       operator>>(L, R);
3834      where LR is the result of the  usual  arithmetic  conversions  between
3835      types L and R.  */
3836
3837     case TRUNC_MOD_EXPR:
3838     case BIT_AND_EXPR:
3839     case BIT_IOR_EXPR:
3840     case BIT_XOR_EXPR:
3841     case LSHIFT_EXPR:
3842     case RSHIFT_EXPR:
3843       if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
3844         break;
3845       return candidates;
3846
3847 /* 19For  every  triple  L, VQ, R), where L is an arithmetic or enumeration
3848      type, VQ is either volatile or empty, and R is a  promoted  arithmetic
3849      type, there exist candidate operator functions of the form
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              VQ L&   operator-=(VQ L&, R);
3855
3856    20For  every  pair T, VQ), where T is any type and VQ is either volatile
3857      or empty, there exist candidate operator functions of the form
3858              T*VQ&   operator=(T*VQ&, T*);
3859
3860    21For every pair T, VQ), where T is a pointer to member type and  VQ  is
3861      either  volatile or empty, there exist candidate operator functions of
3862      the form
3863              VQ T&   operator=(VQ T&, T);
3864
3865    22For every triple  T,  VQ,  I),  where  T  is  a  cv-qualified  or  cv-
3866      unqualified  complete object type, VQ is either volatile or empty, and
3867      I is a promoted integral type, there exist  candidate  operator  func-
3868      tions of the form
3869              T*VQ&   operator+=(T*VQ&, I);
3870              T*VQ&   operator-=(T*VQ&, I);
3871
3872    23For  every  triple  L,  VQ,  R), where L is an integral or enumeration
3873      type, VQ is either volatile or empty, and R  is  a  promoted  integral
3874      type, there exist candidate operator functions of the form
3875
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              VQ L&   operator|=(VQ L&, R);  */
3882
3883     case MODIFY_EXPR:
3884       switch (code2)
3885         {
3886         case PLUS_EXPR:
3887         case MINUS_EXPR:
3888           if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
3889             {
3890               type2 = ptrdiff_type_node;
3891               break;
3892             }
3893         case MULT_EXPR:
3894         case TRUNC_DIV_EXPR:
3895           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3896             break;
3897           return candidates;
3898
3899         case TRUNC_MOD_EXPR:
3900         case BIT_AND_EXPR:
3901         case BIT_IOR_EXPR:
3902         case BIT_XOR_EXPR:
3903         case LSHIFT_EXPR:
3904         case RSHIFT_EXPR:
3905           if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
3906             break;
3907           return candidates;
3908
3909         case NOP_EXPR:
3910           if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3911             break;
3912           if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3913               || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3914               || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
3915               || ((TYPE_PTRMEMFUNC_P (type1)
3916                    || TREE_CODE (type1) == POINTER_TYPE)
3917                   && null_ptr_cst_p (args[1])))
3918             {
3919               type2 = type1;
3920               break;
3921             }
3922           return candidates;
3923
3924         default:
3925           my_friendly_abort (367);
3926         }
3927       type1 = build_reference_type (type1);
3928       break;
3929
3930     case COND_EXPR:
3931       /* Kludge around broken overloading rules whereby
3932          bool ? const char& : enum is ambiguous
3933          (between int and const char&).  */
3934       flags |= LOOKUP_NO_TEMP_BIND;
3935
3936       /* Extension: Support ?: of enumeral type.  Hopefully this will not
3937          be an extension for long.  */
3938       if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
3939         break;
3940       else if (TREE_CODE (type1) == ENUMERAL_TYPE
3941                || TREE_CODE (type2) == ENUMERAL_TYPE)
3942         return candidates;
3943       if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3944         break;
3945       if (TREE_CODE (type1) == TREE_CODE (type2)
3946           && (TREE_CODE (type1) == REFERENCE_TYPE
3947               || TREE_CODE (type1) == POINTER_TYPE
3948               || TYPE_PTRMEMFUNC_P (type1)
3949               || IS_AGGR_TYPE (type1)))
3950         break;
3951       if (TREE_CODE (type1) == REFERENCE_TYPE
3952           || TREE_CODE (type2) == REFERENCE_TYPE)
3953         return candidates;
3954       if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
3955            && null_ptr_cst_p (args[1]))
3956           || IS_AGGR_TYPE (type1))
3957         {
3958           type2 = type1;
3959           break;
3960         }
3961       if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
3962            && null_ptr_cst_p (args[0]))
3963           || IS_AGGR_TYPE (type2))
3964         {
3965           type1 = type2;
3966           break;
3967         }
3968       return candidates;
3969
3970     default:
3971       my_friendly_abort (367);
3972     }
3973
3974   /* If we're dealing with two pointer types, we need candidates
3975      for both of them.  */
3976   if (type2 && type1 != type2
3977       && TREE_CODE (type1) == TREE_CODE (type2)
3978       && (TREE_CODE (type1) == REFERENCE_TYPE
3979           || (TREE_CODE (type1) == POINTER_TYPE
3980               && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
3981           || TYPE_PTRMEMFUNC_P (type1)
3982           || IS_AGGR_TYPE (type1)))
3983     {
3984       candidates = build_builtin_candidate
3985         (candidates, fnname, type1, type1, args, argtypes, flags);
3986       return build_builtin_candidate
3987         (candidates, fnname, type2, type2, args, argtypes, flags);
3988     }
3989
3990   return build_builtin_candidate
3991     (candidates, fnname, type1, type2, args, argtypes, flags);
3992 }
3993
3994 tree
3995 type_decays_to (type)
3996      tree type;
3997 {
3998   if (TREE_CODE (type) == ARRAY_TYPE)
3999     return build_pointer_type (TREE_TYPE (type));
4000   if (TREE_CODE (type) == FUNCTION_TYPE)
4001     return build_pointer_type (type);
4002   return type;
4003 }
4004
4005 /* There are three conditions of builtin candidates:
4006
4007    1) bool-taking candidates.  These are the same regardless of the input.
4008    2) pointer-pair taking candidates.  These are generated for each type
4009       one of the input types converts to.
4010    3) arithmetic candidates.  According to the WP, we should generate
4011       all of these, but I'm trying not to... */
4012
4013 static struct z_candidate *
4014 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
4015      struct z_candidate *candidates;
4016      enum tree_code code, code2;
4017      tree fnname, *args;
4018      int flags;
4019 {
4020   int ref1, i;
4021   tree type, argtypes[3], types[2];
4022
4023   for (i = 0; i < 3; ++i)
4024     {
4025       if (args[i])
4026         argtypes[i]  = lvalue_type (args[i]);
4027       else
4028         argtypes[i] = NULL_TREE;
4029     }
4030
4031   switch (code)
4032     {
4033 /* 4 For every pair T, VQ), where T is an arithmetic or  enumeration  type,
4034      and  VQ  is  either  volatile or empty, there exist candidate operator
4035      functions of the form
4036                  VQ T&   operator++(VQ T&);  */
4037
4038     case POSTINCREMENT_EXPR:
4039     case PREINCREMENT_EXPR:
4040     case POSTDECREMENT_EXPR:
4041     case PREDECREMENT_EXPR:
4042     case MODIFY_EXPR:
4043       ref1 = 1;
4044       break;
4045
4046 /* 24There also exist candidate operator functions of the form
4047              bool    operator!(bool);
4048              bool    operator&&(bool, bool);
4049              bool    operator||(bool, bool);  */
4050
4051     case TRUTH_NOT_EXPR:
4052       return build_builtin_candidate
4053         (candidates, fnname, boolean_type_node,
4054          NULL_TREE, args, argtypes, flags);
4055
4056     case TRUTH_ORIF_EXPR:
4057     case TRUTH_ANDIF_EXPR:
4058       return build_builtin_candidate
4059         (candidates, fnname, boolean_type_node,
4060          boolean_type_node, args, argtypes, flags);
4061
4062     case ADDR_EXPR:
4063     case COMPOUND_EXPR:
4064     case COMPONENT_REF:
4065       return candidates;
4066
4067     default:
4068       ref1 = 0;
4069     }
4070
4071   types[0] = types[1] = NULL_TREE;
4072
4073   for (i = 0; i < 2; ++i)
4074     {
4075       if (! args[i])
4076         ;
4077       else if (IS_AGGR_TYPE (argtypes[i]))
4078         {
4079           tree convs = lookup_conversions (argtypes[i]);
4080
4081           if (code == COND_EXPR)
4082             {
4083               if (real_lvalue_p (args[i]))
4084                 types[i] = scratch_tree_cons
4085                   (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
4086
4087               types[i] = scratch_tree_cons
4088                 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
4089             }
4090                 
4091           else if (! convs || (i == 0 && code == MODIFY_EXPR
4092                                && code2 == NOP_EXPR))
4093             return candidates;
4094
4095           for (; convs; convs = TREE_CHAIN (convs))
4096             {
4097               type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
4098
4099               if (i == 0 && ref1
4100                   && (TREE_CODE (type) != REFERENCE_TYPE
4101                       || TYPE_READONLY (TREE_TYPE (type))))
4102                 continue;
4103
4104               if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
4105                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4106
4107               type = non_reference (type);
4108               if (i != 0 || ! ref1)
4109                 {
4110                   type = TYPE_MAIN_VARIANT (type_decays_to (type));
4111                   if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
4112                     types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4113                   if (INTEGRAL_TYPE_P (type))
4114                     type = type_promotes_to (type);
4115                 }
4116
4117               if (! value_member (type, types[i]))
4118                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4119             }
4120         }
4121       else
4122         {
4123           if (code == COND_EXPR && real_lvalue_p (args[i]))
4124             types[i] = scratch_tree_cons
4125               (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
4126           type = non_reference (argtypes[i]);
4127           if (i != 0 || ! ref1)
4128             {
4129               type = TYPE_MAIN_VARIANT (type_decays_to (type));
4130               if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
4131                 types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4132               if (INTEGRAL_TYPE_P (type))
4133                 type = type_promotes_to (type);
4134             }
4135           types[i] = scratch_tree_cons (NULL_TREE, type, types[i]);
4136         }
4137     }
4138
4139   for (; types[0]; types[0] = TREE_CHAIN (types[0]))
4140     {
4141       if (types[1])
4142         for (type = types[1]; type; type = TREE_CHAIN (type))
4143           candidates = add_builtin_candidate
4144             (candidates, code, code2, fnname, TREE_VALUE (types[0]),
4145              TREE_VALUE (type), args, argtypes, flags);
4146       else
4147         candidates = add_builtin_candidate
4148           (candidates, code, code2, fnname, TREE_VALUE (types[0]),
4149            NULL_TREE, args, argtypes, flags);
4150     }
4151
4152   return candidates;
4153 }
4154
4155
4156 /* If TMPL can be successfully instantiated as indicated by
4157    EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
4158
4159    TMPL is the template.  EXPLICIT_TARGS are any explicit template
4160    arguments.  ARGLIST is the arguments provided at the call-site.
4161    The RETURN_TYPE is the desired type for conversion operators.  If
4162    OBJ is NULL_TREE, FLAGS are as for add_function_candidate.  If an
4163    OBJ is supplied, FLAGS are ignored, and OBJ is as for
4164    add_conv_candidate.  */
4165
4166 static struct z_candidate*
4167 add_template_candidate_real (candidates, tmpl, explicit_targs,
4168                              arglist, return_type, flags,
4169                              obj)
4170      struct z_candidate *candidates;
4171      tree tmpl, explicit_targs, arglist, return_type;
4172      int flags;
4173      tree obj;
4174 {
4175   int ntparms = DECL_NTPARMS (tmpl);
4176   tree targs = make_scratch_vec (ntparms);
4177   struct z_candidate *cand;
4178   int i;
4179   tree fn;
4180
4181   i = fn_type_unification (tmpl, explicit_targs, targs, arglist,
4182                            return_type, 0, NULL_TREE); 
4183
4184   if (i != 0)
4185     return candidates;
4186
4187   fn = instantiate_template (tmpl, targs);
4188   if (fn == error_mark_node)
4189     return candidates;
4190
4191   if (obj != NULL_TREE)
4192     /* Aha, this is a conversion function.  */
4193     cand = add_conv_candidate (candidates, fn, obj, arglist);
4194   else
4195     cand = add_function_candidate (candidates, fn, arglist, flags);
4196   if (DECL_TI_TEMPLATE (fn) != tmpl)
4197     /* This situation can occur if a member template of a template
4198        class is specialized.  Then, instantiate_template might return
4199        an instantiation of the specialization, in which case the
4200        DECL_TI_TEMPLATE field will point at the original
4201        specialization.  For example:
4202
4203          template <class T> struct S { template <class U> void f(U);
4204                                        template <> void f(int) {}; };
4205          S<double> sd;
4206          sd.f(3);
4207
4208        Here, TMPL will be template <class U> S<double>::f(U).
4209        And, instantiate template will give us the specialization
4210        template <> S<double>::f(int).  But, the DECL_TI_TEMPLATE field
4211        for this will point at template <class T> template <> S<T>::f(int),
4212        so that we can find the definition.  For the purposes of
4213        overload resolution, however, we want the original TMPL.  */
4214     cand->template = tree_cons (tmpl, targs, NULL_TREE);
4215   else
4216     cand->template = DECL_TEMPLATE_INFO (fn);
4217
4218   return cand;
4219 }
4220
4221
4222 static struct z_candidate *
4223 add_template_candidate (candidates, tmpl, explicit_targs, 
4224                         arglist, return_type, flags)
4225      struct z_candidate *candidates;
4226      tree tmpl, explicit_targs, arglist, return_type;
4227      int flags;
4228 {
4229   return 
4230     add_template_candidate_real (candidates, tmpl, explicit_targs,
4231                                  arglist, return_type, flags, NULL_TREE);
4232 }
4233
4234
4235 static struct z_candidate *
4236 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
4237      struct z_candidate *candidates;
4238      tree tmpl, obj, arglist, return_type;
4239 {
4240   return 
4241     add_template_candidate_real (candidates, tmpl, NULL_TREE, arglist,
4242                                  return_type, 0, obj);
4243 }
4244
4245
4246 static int
4247 any_viable (cands)
4248      struct z_candidate *cands;
4249 {
4250   for (; cands; cands = cands->next)
4251     if (pedantic ? cands->viable == 1 : cands->viable)
4252       return 1;
4253   return 0;
4254 }
4255
4256 static struct z_candidate *
4257 splice_viable (cands)
4258      struct z_candidate *cands;
4259 {
4260   struct z_candidate **p = &cands;
4261
4262   for (; *p; )
4263     {
4264       if (pedantic ? (*p)->viable == 1 : (*p)->viable)
4265         p = &((*p)->next);
4266       else
4267         *p = (*p)->next;
4268     }
4269
4270   return cands;
4271 }
4272
4273 static tree
4274 build_this (obj)
4275      tree obj;
4276 {
4277   /* Fix this to work on non-lvalues.  */
4278   if (IS_SIGNATURE_POINTER (TREE_TYPE (obj))
4279       || IS_SIGNATURE_REFERENCE (TREE_TYPE (obj)))
4280     return obj;
4281   else
4282     return build_unary_op (ADDR_EXPR, obj, 0);
4283 }
4284
4285 static void
4286 print_z_candidates (candidates)
4287      struct z_candidate *candidates;
4288 {
4289   char *str = "candidates are:";
4290   for (; candidates; candidates = candidates->next)
4291     {
4292       if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
4293         {
4294           if (candidates->fn == ansi_opname [COND_EXPR])
4295             cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
4296                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
4297                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
4298                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
4299           else if (TREE_VEC_LENGTH (candidates->convs) == 2)
4300             cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
4301                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
4302                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
4303           else
4304             cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
4305                       TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
4306         }
4307       else
4308         cp_error_at ("%s %+D%s", str, candidates->fn,
4309                      candidates->viable == -1 ? " <near match>" : "");
4310       str = "               "; 
4311     }
4312 }
4313
4314 /* Returns the best overload candidate to perform the requested
4315    conversion.  This function is used for three the overloading situations
4316    described in [over.match.copy], [over.match.conv], and [over.match.ref].
4317    If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
4318    per [dcl.init.ref], so we ignore temporary bindings.  */
4319
4320 static struct z_candidate *
4321 build_user_type_conversion_1 (totype, expr, flags)
4322      tree totype, expr;
4323      int flags;
4324 {
4325   struct z_candidate *candidates, *cand;
4326   tree fromtype = TREE_TYPE (expr);
4327   tree ctors = NULL_TREE, convs = NULL_TREE, *p;
4328   tree args = NULL_TREE;
4329   tree templates = NULL_TREE;
4330
4331   if (IS_AGGR_TYPE (totype))
4332     ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
4333   if (IS_AGGR_TYPE (fromtype)
4334       && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
4335     convs = lookup_conversions (fromtype);
4336
4337   candidates = 0;
4338   flags |= LOOKUP_NO_CONVERSION;
4339
4340   if (ctors)
4341     {
4342       tree t = build_int_2 (0, 0);
4343       TREE_TYPE (t) = build_pointer_type (totype);
4344       args = build_scratch_list (NULL_TREE, expr);
4345       if (TYPE_USES_VIRTUAL_BASECLASSES (totype))
4346         args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
4347       args = scratch_tree_cons (NULL_TREE, t, args);
4348
4349       ctors = TREE_VALUE (ctors);
4350     }
4351   for (; ctors; ctors = DECL_CHAIN (ctors))
4352     {
4353       if (DECL_NONCONVERTING_P (ctors))
4354         continue;
4355
4356       if (TREE_CODE (ctors) == TEMPLATE_DECL) 
4357         {
4358           templates = scratch_tree_cons (NULL_TREE, ctors, templates);
4359           candidates = 
4360             add_template_candidate (candidates, ctors,
4361                                     NULL_TREE, args, NULL_TREE, flags);
4362         } 
4363       else 
4364         candidates = add_function_candidate (candidates, ctors,
4365                                              args, flags); 
4366
4367       if (candidates) 
4368         {
4369           candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
4370           candidates->basetype_path = TYPE_BINFO (totype);
4371         } 
4372     }
4373
4374   if (convs)
4375     args = build_scratch_list (NULL_TREE, build_this (expr));
4376
4377   for (; convs; convs = TREE_CHAIN (convs))
4378     {
4379       tree fn = TREE_VALUE (convs);
4380       int convflags = LOOKUP_NO_CONVERSION;
4381       tree ics;
4382
4383       /* If we are called to convert to a reference type, we are trying to
4384          find an lvalue binding, so don't even consider temporaries.  If
4385          we don't find an lvalue binding, the caller will try again to
4386          look for a temporary binding.  */
4387       if (TREE_CODE (totype) == REFERENCE_TYPE)
4388         convflags |= LOOKUP_NO_TEMP_BIND;
4389
4390       if (TREE_CODE (fn) != TEMPLATE_DECL)
4391         ics = implicit_conversion
4392           (totype, TREE_TYPE (TREE_TYPE (fn)), 0, convflags);
4393       else
4394         /* Here, the template conversion operator result must
4395            precisely match the TOTYPE.  (FIXME: Actually, we're
4396            supposed to do some simple conversions here; see
4397            [temp.deduct.conv].).  If the result of the conversion
4398            operator is not actually TOTYPE, then
4399            add_template_candidate will fail below.  */
4400         ics = implicit_conversion (totype, totype, 0, convflags);
4401
4402       if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
4403         /* ignore the near match.  */;
4404       else if (ics)
4405         for (; fn; fn = DECL_CHAIN (fn))
4406           {
4407             if (TREE_CODE (fn) == TEMPLATE_DECL)
4408               {
4409                 templates = scratch_tree_cons (NULL_TREE, fn, templates);
4410                 candidates = 
4411                   add_template_candidate (candidates, fn, NULL_TREE,
4412                                           args, totype, flags);
4413               } 
4414             else 
4415               candidates = add_function_candidate (candidates, fn,
4416                                                    args, flags); 
4417
4418             if (candidates) 
4419               {
4420                 candidates->second_conv = ics;
4421                 candidates->basetype_path = TREE_PURPOSE (convs);
4422                 if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
4423                   candidates->viable = -1;
4424               }
4425           }
4426     }
4427
4428   if (! any_viable (candidates))
4429     {
4430 #if 0
4431       if (flags & LOOKUP_COMPLAIN)
4432         {
4433           if (candidates && ! candidates->next)
4434             /* say why this one won't work or try to be loose */;
4435           else
4436             cp_error ("no viable candidates");
4437         }
4438 #endif
4439
4440       return 0;
4441     }
4442
4443   candidates = splice_viable (candidates);
4444   cand = tourney (candidates);
4445
4446   if (cand == 0)
4447     {
4448       if (flags & LOOKUP_COMPLAIN)
4449         {
4450           cp_error ("conversion from `%T' to `%T' is ambiguous",
4451                     fromtype, totype);
4452           print_z_candidates (candidates);
4453         }
4454
4455       cand = candidates;        /* any one will do */
4456       cand->second_conv = build1 (AMBIG_CONV, totype, expr);
4457       ICS_USER_FLAG (cand->second_conv) = 1;
4458       ICS_BAD_FLAG (cand->second_conv) = 1;
4459
4460       return cand;
4461     }
4462
4463   for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
4464     p = &(TREE_OPERAND (*p, 0));
4465
4466   /* Pedantically, normal function declarations are never considered
4467      to refer to template instantiations, so we only do this with
4468      -fguiding-decls.  */ 
4469   if (flag_guiding_decls && templates && ! cand->template 
4470       && !DECL_INITIAL (cand->fn) 
4471       && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
4472     add_maybe_template (cand->fn, templates);
4473
4474   *p = build
4475     (USER_CONV,
4476      (DECL_CONSTRUCTOR_P (cand->fn)
4477       ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
4478      expr, cand->fn, cand->convs, cand->basetype_path);
4479   ICS_USER_FLAG (cand->second_conv) = 1;
4480   if (cand->viable == -1)
4481     ICS_BAD_FLAG (cand->second_conv) = 1;
4482
4483   return cand;
4484 }
4485
4486 tree
4487 build_user_type_conversion (totype, expr, flags)
4488      tree totype, expr;
4489      int flags;
4490 {
4491   struct z_candidate *cand
4492     = build_user_type_conversion_1 (totype, expr, flags);
4493
4494   if (cand)
4495     {
4496       if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
4497         return error_mark_node;
4498       return convert_from_reference (convert_like (cand->second_conv, expr));
4499     }
4500   return NULL_TREE;
4501 }
4502
4503 /* Do any initial processing on the arguments to a function call.  */
4504
4505 static tree
4506 resolve_args (args)
4507      tree args;
4508 {
4509   tree t;
4510   for (t = args; t; t = TREE_CHAIN (t))
4511     {
4512       if (TREE_VALUE (t) == error_mark_node)
4513         return error_mark_node;
4514       else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
4515         {
4516           error ("invalid use of void expression");
4517           return error_mark_node;
4518         }
4519       else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
4520         TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
4521     }
4522   return args;
4523 }
4524       
4525 tree
4526 build_new_function_call (fn, args)
4527      tree fn, args;
4528 {
4529   struct z_candidate *candidates = 0, *cand;
4530   tree explicit_targs = NULL_TREE;
4531   int template_only = 0;
4532
4533   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4534     {
4535       explicit_targs = TREE_OPERAND (fn, 1);
4536       fn = TREE_OPERAND (fn, 0);
4537       template_only = 1;
4538     }
4539
4540   if (really_overloaded_fn (fn))
4541     {
4542       tree t;
4543       tree templates = NULL_TREE;
4544
4545       args = resolve_args (args);
4546
4547       if (args == error_mark_node)
4548         return error_mark_node;
4549
4550       for (t = TREE_VALUE (fn); t; t = DECL_CHAIN (t))
4551         {
4552           if (TREE_CODE (t) == TEMPLATE_DECL)
4553             {
4554               templates = scratch_tree_cons (NULL_TREE, t, templates);
4555               candidates = add_template_candidate
4556                 (candidates, t, explicit_targs, args, NULL_TREE,
4557                  LOOKUP_NORMAL);  
4558             }
4559           else if (! template_only)
4560             candidates = add_function_candidate
4561               (candidates, t, args, LOOKUP_NORMAL);
4562         }
4563
4564       if (! any_viable (candidates))
4565         {
4566           if (candidates && ! candidates->next)
4567             return build_function_call (candidates->fn, args);
4568           cp_error ("no matching function for call to `%D (%A)'",
4569                     TREE_PURPOSE (fn), args);
4570           if (candidates)
4571             print_z_candidates (candidates);
4572           return error_mark_node;
4573         }
4574       candidates = splice_viable (candidates);
4575       cand = tourney (candidates);
4576
4577       if (cand == 0)
4578         {
4579           cp_error ("call of overloaded `%D (%A)' is ambiguous",
4580                     TREE_PURPOSE (fn), args);
4581           print_z_candidates (candidates);
4582           return error_mark_node;
4583         }
4584
4585       /* Pedantically, normal function declarations are never considered
4586          to refer to template instantiations, so we only do this with
4587          -fguiding-decls.  */
4588       if (flag_guiding_decls && templates && ! cand->template 
4589           && ! DECL_INITIAL (cand->fn))
4590         add_maybe_template (cand->fn, templates);
4591
4592       return build_over_call (cand->fn, cand->convs, args, LOOKUP_NORMAL);
4593     }
4594
4595   return build_function_call (fn, args);
4596 }
4597
4598 static tree
4599 build_object_call (obj, args)
4600      tree obj, args;
4601 {
4602   struct z_candidate *candidates = 0, *cand;
4603   tree fns, convs, mem_args = NULL_TREE;
4604   tree type = TREE_TYPE (obj);
4605   tree templates = NULL_TREE;
4606
4607   fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 0);
4608
4609   args = resolve_args (args);
4610
4611   if (args == error_mark_node)
4612     return error_mark_node;
4613
4614   if (fns)
4615     {
4616       tree fn = TREE_VALUE (fns);
4617       mem_args = scratch_tree_cons (NULL_TREE, build_this (obj), args);
4618
4619       for (; fn; fn = DECL_CHAIN (fn))
4620         {
4621           if (TREE_CODE (fn) == TEMPLATE_DECL)
4622             {
4623               templates = scratch_tree_cons (NULL_TREE, fn, templates);
4624               candidates 
4625                 = add_template_candidate (candidates, fn, NULL_TREE,
4626                                           mem_args, NULL_TREE, 
4627                                           LOOKUP_NORMAL);
4628             }
4629           else
4630             candidates = add_function_candidate
4631               (candidates, fn, mem_args, LOOKUP_NORMAL);
4632
4633           if (candidates)
4634             candidates->basetype_path = TREE_PURPOSE (fns);
4635         }
4636     }
4637
4638   convs = lookup_conversions (type);
4639
4640   for (; convs; convs = TREE_CHAIN (convs))
4641     {
4642       tree fn = TREE_VALUE (convs);
4643       tree totype = TREE_TYPE (TREE_TYPE (fn));
4644
4645       if (TREE_CODE (totype) == POINTER_TYPE
4646           && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
4647         for (; fn; fn = DECL_CHAIN (fn))
4648           {
4649             if (TREE_CODE (fn) == TEMPLATE_DECL) 
4650               {
4651                 templates = scratch_tree_cons (NULL_TREE, fn, templates);
4652                 candidates = add_template_conv_candidate (candidates,
4653                                                           fn,
4654                                                           obj,
4655                                                           args,
4656                                                           totype);
4657               }
4658             else
4659               candidates = add_conv_candidate (candidates, fn, obj, args);
4660
4661             if (candidates)
4662               candidates->basetype_path = TREE_PURPOSE (convs);
4663           }
4664     }
4665
4666   if (! any_viable (candidates))
4667     {
4668       cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
4669       print_z_candidates (candidates);
4670       return error_mark_node;
4671     }
4672
4673   candidates = splice_viable (candidates);
4674   cand = tourney (candidates);
4675
4676   if (cand == 0)
4677     {
4678       cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
4679       print_z_candidates (candidates);
4680       return error_mark_node;
4681     }
4682
4683   if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
4684     return build_over_call (cand->fn, cand->convs, mem_args, LOOKUP_NORMAL);
4685
4686   obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
4687
4688   /* FIXME */
4689   return build_function_call (obj, args);
4690 }
4691
4692 static void
4693 op_error (code, code2, arg1, arg2, arg3, problem)
4694      enum tree_code code, code2;
4695      tree arg1, arg2, arg3;
4696      char *problem;
4697 {
4698   char * opname
4699     = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
4700
4701   switch (code)
4702     {
4703     case COND_EXPR:
4704       cp_error ("%s for `%T ? %T : %T'", problem,
4705                 error_type (arg1), error_type (arg2), error_type (arg3));
4706       break;
4707     case POSTINCREMENT_EXPR:
4708     case POSTDECREMENT_EXPR:
4709       cp_error ("%s for `%T%s'", problem, error_type (arg1), opname);
4710       break;
4711     case ARRAY_REF:
4712       cp_error ("%s for `%T[%T]'", problem,
4713                 error_type (arg1), error_type (arg2));
4714       break;
4715     default:
4716       if (arg2)
4717         cp_error ("%s for `%T %s %T'", problem,
4718                   error_type (arg1), opname, error_type (arg2));
4719       else
4720         cp_error ("%s for `%s%T'", problem, opname, error_type (arg1));
4721     }
4722 }
4723
4724 tree
4725 build_new_op (code, flags, arg1, arg2, arg3)
4726      enum tree_code code;
4727      int flags;
4728      tree arg1, arg2, arg3;
4729 {
4730   struct z_candidate *candidates = 0, *cand;
4731   tree fns, mem_arglist = NULL_TREE, arglist, fnname;
4732   enum tree_code code2 = NOP_EXPR;
4733   tree templates = NULL_TREE;
4734   tree conv;
4735
4736   if (arg1 == error_mark_node
4737       || arg2 == error_mark_node
4738       || arg3 == error_mark_node)
4739     return error_mark_node;
4740
4741   /* This can happen if a template takes all non-type parameters, e.g.
4742      undeclared_template<1, 5, 72>a;  */
4743   if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
4744     {
4745       cp_error ("`%D' must be declared before use", arg1);
4746       return error_mark_node;
4747     }
4748
4749   if (code == MODIFY_EXPR)
4750     {
4751       code2 = TREE_CODE (arg3);
4752       arg3 = NULL_TREE;
4753       fnname = ansi_assopname[code2];
4754     }
4755   else
4756     fnname = ansi_opname[code];
4757
4758   switch (code)
4759     {
4760     case NEW_EXPR:
4761     case VEC_NEW_EXPR:
4762       {
4763         tree rval;
4764
4765         arglist = scratch_tree_cons (NULL_TREE, arg2, arg3);
4766         if (flags & LOOKUP_GLOBAL)
4767           return build_new_function_call
4768             (lookup_name_nonclass (fnname), arglist);
4769
4770         /* FIXME */
4771         rval = build_method_call
4772           (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
4773                                "new"),
4774            fnname, arglist, NULL_TREE, flags);
4775         if (rval == error_mark_node)
4776           /* User might declare fancy operator new, but invoke it
4777              like standard one.  */
4778           return rval;
4779
4780         TREE_TYPE (rval) = arg1;
4781         return rval;
4782       }
4783
4784     case VEC_DELETE_EXPR:
4785     case DELETE_EXPR:
4786       {
4787         tree rval;
4788
4789         if (flags & LOOKUP_GLOBAL)
4790           return build_new_function_call
4791             (lookup_name_nonclass (fnname),
4792              build_scratch_list (NULL_TREE, arg1));
4793
4794         arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
4795
4796         arg1 = TREE_TYPE (arg1);
4797
4798         /* This handles the case where we're trying to delete
4799            X (*a)[10];
4800            a=new X[5][10];
4801            delete[] a; */
4802            
4803         if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
4804           {
4805             /* Strip off the pointer and the array.  */
4806             arg1 = TREE_TYPE (TREE_TYPE (arg1));
4807
4808             while (TREE_CODE (arg1) == ARRAY_TYPE)
4809                 arg1 = (TREE_TYPE (arg1));
4810
4811             arg1 = build_pointer_type (arg1);
4812           }
4813
4814         /* FIXME */
4815         rval = build_method_call
4816           (build_indirect_ref (build1 (NOP_EXPR, arg1,
4817                                        error_mark_node),
4818                                NULL_PTR),
4819            fnname, arglist, NULL_TREE, flags);
4820 #if 0
4821         /* This can happen when operator delete is protected.  */
4822         my_friendly_assert (rval != error_mark_node, 250);
4823         TREE_TYPE (rval) = void_type_node;
4824 #endif
4825         return rval;
4826       }
4827
4828     case CALL_EXPR:
4829       return build_object_call (arg1, arg2);
4830
4831     default:
4832       break;
4833     }
4834
4835   /* The comma operator can have void args.  */
4836   if (TREE_CODE (arg1) == OFFSET_REF)
4837     arg1 = resolve_offset_ref (arg1);
4838   if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
4839     arg2 = resolve_offset_ref (arg2);
4840   if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
4841     arg3 = resolve_offset_ref (arg3);
4842
4843   if (code == COND_EXPR)
4844     {
4845       if (arg2 == NULL_TREE
4846           || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
4847           || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
4848           || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
4849               && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
4850         goto builtin;
4851     }
4852   else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
4853            && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
4854     goto builtin;
4855
4856   if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
4857     arg2 = integer_zero_node;
4858
4859   if (arg2 && arg3)
4860     arglist = scratch_tree_cons (NULL_TREE, arg1, scratch_tree_cons
4861                       (NULL_TREE, arg2, build_scratch_list (NULL_TREE, arg3)));
4862   else if (arg2)
4863     arglist = scratch_tree_cons (NULL_TREE, arg1, build_scratch_list (NULL_TREE, arg2));
4864   else
4865     arglist = build_scratch_list (NULL_TREE, arg1);
4866
4867   fns = lookup_name_nonclass (fnname);
4868   /* + Koenig lookup */
4869
4870   if (fns && TREE_CODE (fns) == TREE_LIST)
4871     fns = TREE_VALUE (fns);
4872   for (; fns; fns = DECL_CHAIN (fns))
4873     {
4874       if (TREE_CODE (fns) == TEMPLATE_DECL)
4875         {
4876           templates = scratch_tree_cons (NULL_TREE, fns, templates);
4877           candidates 
4878             = add_template_candidate (candidates, fns, NULL_TREE,
4879                                       arglist, TREE_TYPE (fnname),
4880                                       flags); 
4881         }
4882       else
4883         candidates = add_function_candidate (candidates, fns, arglist, flags);
4884     }
4885
4886   if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
4887     fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 0);
4888   else
4889     fns = NULL_TREE;
4890
4891   if (fns)
4892     {
4893       tree fn = TREE_VALUE (fns);
4894       mem_arglist = scratch_tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
4895       for (; fn; fn = DECL_CHAIN (fn))
4896         {
4897           tree this_arglist;
4898
4899           if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4900             this_arglist = mem_arglist;
4901           else
4902             this_arglist = arglist;
4903
4904           if (TREE_CODE (fn) == TEMPLATE_DECL)
4905             {
4906               /* A member template. */
4907               templates = scratch_tree_cons (NULL_TREE, fn, templates);
4908               candidates 
4909                 = add_template_candidate (candidates, fn, NULL_TREE,
4910                                           this_arglist,  TREE_TYPE (fnname),
4911                                           flags); 
4912             }
4913           else
4914             candidates = add_function_candidate
4915               (candidates, fn, this_arglist, flags);
4916
4917           if (candidates) 
4918             candidates->basetype_path = TREE_PURPOSE (fns);
4919         }
4920     }
4921
4922   {
4923     tree args[3];
4924
4925     /* Rearrange the arguments for ?: so that add_builtin_candidate only has
4926        to know about two args; a builtin candidate will always have a first
4927        parameter of type bool.  We'll handle that in
4928        build_builtin_candidate.  */
4929     if (code == COND_EXPR)
4930       {
4931         args[0] = arg2;
4932         args[1] = arg3;
4933         args[2] = arg1;
4934       }
4935     else
4936       {
4937         args[0] = arg1;
4938         args[1] = arg2;
4939         args[2] = NULL_TREE;
4940       }
4941
4942     candidates = add_builtin_candidates
4943       (candidates, code, code2, fnname, args, flags);
4944   }
4945
4946   if (! any_viable (candidates))
4947     {
4948       switch (code)
4949         {
4950         case POSTINCREMENT_EXPR:
4951         case POSTDECREMENT_EXPR:
4952           /* Look for an `operator++ (int)'.  If they didn't have
4953              one, then we fall back to the old way of doing things.  */
4954           if (flags & LOOKUP_COMPLAIN)
4955             cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
4956                         fnname, opname_tab [code]);
4957           if (code == POSTINCREMENT_EXPR)
4958             code = PREINCREMENT_EXPR;
4959           else
4960             code = PREDECREMENT_EXPR;   
4961           return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
4962           
4963           /* The caller will deal with these.  */
4964         case ADDR_EXPR:
4965         case COMPOUND_EXPR:
4966         case COMPONENT_REF:
4967           return NULL_TREE;
4968
4969         default:
4970           break;
4971         }
4972       if (flags & LOOKUP_COMPLAIN)
4973         {
4974           op_error (code, code2, arg1, arg2, arg3, "no match");
4975           print_z_candidates (candidates);
4976         }
4977       return error_mark_node;
4978     }
4979   candidates = splice_viable (candidates);
4980   cand = tourney (candidates);
4981
4982   if (cand == 0)
4983     {
4984       if (flags & LOOKUP_COMPLAIN)
4985         {
4986           op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
4987           print_z_candidates (candidates);
4988         }
4989       return error_mark_node;
4990     }
4991
4992   if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4993     {
4994       extern int warn_synth;
4995       if (warn_synth
4996           && fnname == ansi_opname[MODIFY_EXPR]
4997           && DECL_ARTIFICIAL (cand->fn)
4998           && candidates->next
4999           && ! candidates->next->next)
5000         {
5001           cp_warning ("using synthesized `%#D' for copy assignment",
5002                       cand->fn);
5003           cp_warning_at ("  where cfront would use `%#D'",
5004                          cand == candidates
5005                          ? candidates->next->fn
5006                          : candidates->fn);
5007         }
5008
5009       if (DECL_FUNCTION_MEMBER_P (cand->fn))
5010         enforce_access (cand->basetype_path, cand->fn);
5011
5012       /* Pedantically, normal function declarations are never considered
5013          to refer to template instantiations, so we only do this with
5014          -fguiding-decls.  */ 
5015       if (flag_guiding_decls && templates && ! cand->template 
5016           && ! DECL_INITIAL (cand->fn)
5017           && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE)
5018         add_maybe_template (cand->fn, templates);
5019
5020       return build_over_call
5021         (cand->fn, cand->convs,
5022          TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5023          ? mem_arglist : arglist,
5024          LOOKUP_NORMAL);
5025     }
5026
5027   /* Check for comparison of different enum types.  */
5028   switch (code)
5029     {
5030     case GT_EXPR:
5031     case LT_EXPR:
5032     case GE_EXPR:
5033     case LE_EXPR:
5034     case EQ_EXPR:
5035     case NE_EXPR:
5036       if (flag_int_enum_equivalence == 0 
5037           && TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE 
5038           && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE 
5039           && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
5040               != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
5041         {
5042           cp_warning ("comparison between `%#T' and `%#T'", 
5043                       TREE_TYPE (arg1), TREE_TYPE (arg2));
5044         }
5045       break;
5046     default:
5047       break;
5048     }
5049
5050   /* We need to strip any leading REF_BIND so that bitfields don't cause
5051      errors.  This should not remove any important conversions, because
5052      builtins don't apply to class objects directly.  */
5053   conv = TREE_VEC_ELT (cand->convs, 0);
5054   if (TREE_CODE (conv) == REF_BIND)
5055     conv = TREE_OPERAND (conv, 0);
5056   arg1 = convert_like (conv, arg1);
5057   if (arg2)
5058     arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
5059   if (arg3)
5060     arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
5061
5062 builtin:
5063   switch (code)
5064     {
5065     case MODIFY_EXPR:
5066       return build_modify_expr (arg1, code2, arg2);
5067
5068     case INDIRECT_REF:
5069       return build_indirect_ref (arg1, "unary *");
5070
5071     case PLUS_EXPR:
5072     case MINUS_EXPR:
5073     case MULT_EXPR:
5074     case TRUNC_DIV_EXPR:
5075     case GT_EXPR:
5076     case LT_EXPR:
5077     case GE_EXPR:
5078     case LE_EXPR:
5079     case EQ_EXPR:
5080     case NE_EXPR:
5081     case MAX_EXPR:
5082     case MIN_EXPR:
5083     case LSHIFT_EXPR:
5084     case RSHIFT_EXPR:
5085     case TRUNC_MOD_EXPR:
5086     case BIT_AND_EXPR:
5087     case BIT_IOR_EXPR:
5088     case BIT_XOR_EXPR:
5089     case TRUTH_ANDIF_EXPR:
5090     case TRUTH_ORIF_EXPR:
5091       return build_binary_op_nodefault (code, arg1, arg2, code);
5092
5093     case CONVERT_EXPR:
5094     case NEGATE_EXPR:
5095     case BIT_NOT_EXPR:
5096     case TRUTH_NOT_EXPR:
5097     case PREINCREMENT_EXPR:
5098     case POSTINCREMENT_EXPR:
5099     case PREDECREMENT_EXPR:
5100     case POSTDECREMENT_EXPR:
5101     case REALPART_EXPR:
5102     case IMAGPART_EXPR:
5103       return build_unary_op (code, arg1, candidates != 0);
5104
5105     case ARRAY_REF:
5106       return build_array_ref (arg1, arg2);
5107
5108     case COND_EXPR:
5109       return build_conditional_expr (arg1, arg2, arg3);
5110
5111     case MEMBER_REF:
5112       return build_m_component_ref
5113         (build_indirect_ref (arg1, NULL_PTR), arg2);
5114
5115       /* The caller will deal with these.  */
5116     case ADDR_EXPR:
5117     case COMPONENT_REF:
5118     case COMPOUND_EXPR:
5119       return NULL_TREE;
5120
5121     default:
5122       my_friendly_abort (367);
5123       return NULL_TREE;
5124     }
5125 }
5126
5127 /* Build up a call to operator new.  This has to be handled differently
5128    from other operators in the way lookup is handled; first members are
5129    considered, then globals.  CODE is either NEW_EXPR or VEC_NEW_EXPR.
5130    TYPE is the type to be created.  ARGS are any new-placement args.
5131    FLAGS are the usual overloading flags.  */
5132
5133 tree
5134 build_op_new_call (code, type, args, flags)
5135      enum tree_code code;
5136      tree type, args;
5137      int flags;
5138 {
5139   tree fnname = ansi_opname[code];
5140
5141   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL)
5142       && (TYPE_GETS_NEW (type) & (1 << (code == VEC_NEW_EXPR))))
5143     {
5144       tree dummy = build1 (NOP_EXPR, build_pointer_type (type),
5145                            error_mark_node);
5146       dummy = build_indirect_ref (dummy, "new");
5147       return build_method_call (dummy, fnname, args, NULL_TREE, flags);
5148     }
5149   else
5150     return build_new_function_call (lookup_name_nonclass (fnname), args);
5151 }
5152
5153 /* Build a call to operator delete.  This has to be handled very specially,
5154    because the restrictions on what signatures match are different from all
5155    other call instances.  For a normal delete, only a delete taking (void *)
5156    or (void *, size_t) is accepted.  For a placement delete, only an exact
5157    match with the placement new is accepted.
5158
5159    CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
5160    ADDR is the pointer to be deleted.  For placement delete, it is also
5161      used to determine what the corresponding new looked like.
5162    SIZE is the size of the memory block to be deleted.
5163    FLAGS are the usual overloading flags.  */
5164
5165 tree
5166 build_op_delete_call (code, addr, size, flags)
5167      enum tree_code code;
5168      tree addr, size;
5169      int flags;
5170 {
5171   tree fn, fns, fnname, fntype, argtypes, args, type;
5172   int placement;
5173
5174   if (addr == error_mark_node)
5175     return error_mark_node;
5176
5177   type = TREE_TYPE (TREE_TYPE (addr));
5178   fnname = ansi_opname[code];
5179
5180   if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
5181     fns = lookup_fnfields (TYPE_BINFO (type), fnname, 0);
5182   else
5183     fns = NULL_TREE;
5184
5185   if (fns)
5186     {
5187       /* Build this up like build_offset_ref does.  */
5188       fns = build_tree_list (error_mark_node, fns);
5189       TREE_TYPE (fns) = build_offset_type (type, unknown_type_node);
5190     }
5191   else
5192     fns = lookup_name_nonclass (fnname);
5193
5194   /* We can recognize a placement delete because of LOOKUP_SPECULATIVELY;
5195      if we are doing placement delete we do nothing if we don't find a
5196      matching op delete.  */
5197   placement = !!(flags & LOOKUP_SPECULATIVELY);
5198   if (placement)
5199     {
5200       /* If placement, we are coming from build_new, and we know that addr
5201          is the allocation expression, so extract the info we need from it.
5202          Obviously, if the build_new process changes this may have to
5203          change as well.  */
5204
5205       /* The NOP_EXPR.  */
5206       tree t = TREE_OPERAND (addr, 1);
5207       /* The CALL_EXPR.  */
5208       t = TREE_OPERAND (t, 0);
5209       /* The function.  */
5210       argtypes = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
5211       /* The second parm type.  */
5212       argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
5213       /* The second argument.  */
5214       args = TREE_CHAIN (TREE_OPERAND (t, 1));
5215
5216       /* Pull the dummy var out of the TARGET_EXPR for use in our call.  */
5217       addr = TREE_OPERAND (addr, 0);
5218     }
5219   else
5220     {
5221       /* First try it without the size argument.  */
5222       argtypes = void_list_node;
5223       args = NULL_TREE;
5224     }
5225
5226   argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
5227   fntype = build_function_type (void_type_node, argtypes);
5228
5229   /* Strip const and volatile from addr.  */
5230   if (type != TYPE_MAIN_VARIANT (type))
5231     addr = cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type)), addr);
5232
5233   /* instantiate_type will always return a plain function; pretend it's
5234      overloaded.  */
5235   if (TREE_CODE (fns) == FUNCTION_DECL)
5236     fns = scratch_tree_cons (NULL_TREE, fns, NULL_TREE);
5237
5238   fn = instantiate_type (fntype, fns, 0);
5239
5240   if (fn != error_mark_node)
5241     {
5242       if (TREE_CODE (TREE_VALUE (fns)) == TREE_LIST)
5243         /* Member functions.  */
5244         enforce_access (TREE_PURPOSE (TREE_VALUE (fns)), fn);
5245       return build_function_call (fn, expr_tree_cons (NULL_TREE, addr, args));
5246     }
5247
5248   if (placement)
5249     return NULL_TREE;
5250
5251   /* Normal delete; now try to find a match including the size argument.  */
5252   argtypes = tree_cons (NULL_TREE, ptr_type_node,
5253                         tree_cons (NULL_TREE, sizetype, void_list_node));
5254   fntype = build_function_type (void_type_node, argtypes);
5255
5256   fn = instantiate_type (fntype, fns, 0);
5257
5258   if (fn != error_mark_node)
5259     return build_function_call
5260       (fn, expr_tree_cons (NULL_TREE, addr,
5261                            build_expr_list (NULL_TREE, size)));
5262
5263   cp_error ("no suitable operator delete for `%T'", type);
5264   return error_mark_node;
5265 }
5266
5267 /* If the current scope isn't allowed to access FUNCTION along
5268    BASETYPE_PATH, give an error.  */
5269
5270 static void
5271 enforce_access (basetype_path, function)
5272      tree basetype_path, function;
5273 {
5274   tree access = compute_access (basetype_path, function);
5275
5276   if (access == access_private_node)
5277     {
5278       cp_error_at ("`%+#D' is %s", function, 
5279                    TREE_PRIVATE (function) ? "private"
5280                    : "from private base class");
5281       error ("within this context");
5282     }
5283   else if (access == access_protected_node)
5284     {
5285       cp_error_at ("`%+#D' %s", function,
5286                    TREE_PROTECTED (function) ? "is protected"
5287                    : "has protected accessibility");
5288       error ("within this context");
5289     }
5290 }
5291
5292 /* Perform the conversions in CONVS on the expression EXPR.  */
5293
5294 static tree
5295 convert_like (convs, expr)
5296      tree convs, expr;
5297 {
5298   if (ICS_BAD_FLAG (convs)
5299       && TREE_CODE (convs) != USER_CONV
5300       && TREE_CODE (convs) != AMBIG_CONV)
5301     {
5302       tree t = convs; 
5303       for (; t; t = TREE_OPERAND (t, 0))
5304         {
5305           if (TREE_CODE (t) == USER_CONV)
5306             {
5307               expr = convert_like (t, expr);
5308               break;
5309             }
5310           else if (TREE_CODE (t) == AMBIG_CONV)
5311             return convert_like (t, expr);
5312           else if (TREE_CODE (t) == IDENTITY_CONV)
5313             break;
5314         }
5315       return convert_for_initialization
5316         (NULL_TREE, TREE_TYPE (convs), expr, LOOKUP_NORMAL,
5317          "conversion", NULL_TREE, 0);
5318     }
5319
5320   switch (TREE_CODE (convs))
5321     {
5322     case USER_CONV:
5323       {
5324         tree fn = TREE_OPERAND (convs, 1);
5325         tree args;
5326         enforce_access (TREE_OPERAND (convs, 3), fn);
5327
5328         if (DECL_CONSTRUCTOR_P (fn))
5329           {
5330             tree t = build_int_2 (0, 0);
5331             TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
5332
5333             args = build_scratch_list (NULL_TREE, expr);
5334             if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
5335               args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
5336             args = scratch_tree_cons (NULL_TREE, t, args);
5337           }
5338         else
5339           args = build_this (expr);
5340         expr = build_over_call
5341           (TREE_OPERAND (convs, 1), TREE_OPERAND (convs, 2),
5342            args, LOOKUP_NORMAL);
5343
5344         /* If this is a constructor or a function returning an aggr type,
5345            we need to build up a TARGET_EXPR.  */
5346         if (DECL_CONSTRUCTOR_P (fn))
5347           expr = build_cplus_new (TREE_TYPE (convs), expr);
5348
5349         return expr;
5350       }
5351     case IDENTITY_CONV:
5352       if (type_unknown_p (expr))
5353         expr = instantiate_type (TREE_TYPE (convs), expr, 1);
5354       if (TREE_READONLY_DECL_P (expr))
5355         expr = decl_constant_value (expr);
5356       return expr;
5357     case AMBIG_CONV:
5358       /* Call build_user_type_conversion again for the error.  */
5359       return build_user_type_conversion
5360         (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
5361
5362     default:
5363       break;
5364     };
5365
5366   expr = convert_like (TREE_OPERAND (convs, 0), expr);
5367   if (expr == error_mark_node)
5368     return error_mark_node;
5369
5370   switch (TREE_CODE (convs))
5371     {
5372     case RVALUE_CONV:
5373       if (! IS_AGGR_TYPE (TREE_TYPE (convs)))
5374         return expr;
5375       /* else fall through */
5376     case BASE_CONV:
5377       return build_user_type_conversion
5378         (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
5379     case REF_BIND:
5380       return convert_to_reference
5381         (TREE_TYPE (convs), expr,
5382          CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
5383          error_mark_node);
5384     case LVALUE_CONV:
5385       return decay_conversion (expr);
5386
5387     default:
5388       break;
5389     }
5390   return ocp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
5391                       LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
5392 }
5393
5394 static tree
5395 convert_default_arg (type, arg)
5396      tree type, arg;
5397 {
5398   arg = break_out_target_exprs (arg);
5399
5400   if (TREE_CODE (arg) == CONSTRUCTOR)
5401     {
5402       arg = digest_init (type, arg, 0);
5403       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5404                                         "default argument", 0, 0);
5405     }
5406   else
5407     {
5408       /* This could get clobbered by the following call.  */
5409       if (TREE_HAS_CONSTRUCTOR (arg))
5410         arg = copy_node (arg);
5411
5412       arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5413                                         "default argument", 0, 0);
5414 #ifdef PROMOTE_PROTOTYPES
5415       if ((TREE_CODE (type) == INTEGER_TYPE
5416            || TREE_CODE (type) == ENUMERAL_TYPE)
5417           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
5418         arg = default_conversion (arg);
5419 #endif
5420     }
5421
5422   return arg;
5423 }
5424
5425 static tree
5426 build_over_call (fn, convs, args, flags)
5427      tree fn, convs, args;
5428      int flags;
5429 {
5430   tree converted_args = NULL_TREE;
5431   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
5432   tree conv, arg, val;
5433   int i = 0;
5434   int is_method = 0;
5435
5436   if (args && TREE_CODE (args) != TREE_LIST)
5437     args = build_scratch_list (NULL_TREE, args);
5438   arg = args;
5439
5440   /* The implicit parameters to a constructor are not considered by overload
5441      resolution, and must be of the proper type.  */
5442   if (DECL_CONSTRUCTOR_P (fn))
5443     {
5444       converted_args = expr_tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
5445       arg = TREE_CHAIN (arg);
5446       parm = TREE_CHAIN (parm);
5447       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
5448         {
5449           converted_args = expr_tree_cons
5450             (NULL_TREE, TREE_VALUE (arg), converted_args);
5451           arg = TREE_CHAIN (arg);
5452           parm = TREE_CHAIN (parm);
5453         }
5454     }      
5455   /* Bypass access control for 'this' parameter.  */
5456   else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5457     {
5458       tree parmtype = TREE_VALUE (parm);
5459       tree argtype = TREE_TYPE (TREE_VALUE (arg));
5460       if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
5461         {
5462           int dv = (TYPE_VOLATILE (TREE_TYPE (parmtype))
5463                     < TYPE_VOLATILE (TREE_TYPE (argtype)));
5464           int dc = (TYPE_READONLY (TREE_TYPE (parmtype))
5465                     < TYPE_READONLY (TREE_TYPE (argtype)));
5466           char *p = (dv && dc ? "const and volatile"
5467                               : dc ? "const" : dv ? "volatile" : "");
5468
5469           cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards %s",
5470                       TREE_TYPE (argtype), fn, p);
5471         }
5472       converted_args = expr_tree_cons
5473         (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
5474          converted_args);
5475       parm = TREE_CHAIN (parm);
5476       arg = TREE_CHAIN (arg);
5477       ++i;
5478       is_method = 1;
5479     }
5480
5481   for (; arg && parm;
5482        parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
5483     {
5484       tree type = TREE_VALUE (parm);
5485
5486       conv = TREE_VEC_ELT (convs, i);
5487       if (ICS_BAD_FLAG (conv))
5488         {
5489           tree t = conv;
5490           val = TREE_VALUE (arg);
5491
5492           for (; t; t = TREE_OPERAND (t, 0))
5493             {
5494               if (TREE_CODE (t) == USER_CONV
5495                   || TREE_CODE (t) == AMBIG_CONV)
5496                 {
5497                   val = convert_like (t, val);
5498                   break;
5499                 }
5500               else if (TREE_CODE (t) == IDENTITY_CONV)
5501                 break;
5502             }
5503           val = convert_for_initialization
5504             (NULL_TREE, type, val, LOOKUP_NORMAL,
5505              "argument passing", fn, i - is_method);
5506         }
5507       else
5508         val = convert_like (conv, TREE_VALUE (arg));
5509
5510 #ifdef PROMOTE_PROTOTYPES
5511       if ((TREE_CODE (type) == INTEGER_TYPE
5512            || TREE_CODE (type) == ENUMERAL_TYPE)
5513           && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
5514         val = default_conversion (val);
5515 #endif
5516       converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
5517     }
5518
5519   /* Default arguments */
5520   for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
5521     {
5522       tree arg = TREE_PURPOSE (parm);
5523
5524       if (DECL_TEMPLATE_INFO (fn))
5525         /* This came from a template.  Instantiate the default arg here,
5526            not in tsubst.  */
5527         arg = tsubst_expr (arg, DECL_TI_ARGS (fn), NULL_TREE);
5528       converted_args = expr_tree_cons
5529         (NULL_TREE, convert_default_arg (TREE_VALUE (parm), arg),
5530          converted_args);
5531     }
5532
5533   /* Ellipsis */
5534   for (; arg; arg = TREE_CHAIN (arg))
5535     {
5536       val = TREE_VALUE (arg);
5537
5538       if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
5539           && (TYPE_PRECISION (TREE_TYPE (val))
5540               < TYPE_PRECISION (double_type_node)))
5541         /* Convert `float' to `double'.  */
5542         val = cp_convert (double_type_node, val);
5543       else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
5544                && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
5545         cp_warning ("cannot pass objects of type `%T' through `...'",
5546                     TREE_TYPE (val));
5547       else
5548         /* Convert `short' and `char' to full-size `int'.  */
5549         val = default_conversion (val);
5550
5551       converted_args = expr_tree_cons (NULL_TREE, val, converted_args);
5552     }
5553
5554   converted_args = nreverse (converted_args);
5555
5556   /* Avoid actually calling copy constructors and copy assignment operators,
5557      if possible.  */
5558   if (DECL_CONSTRUCTOR_P (fn)
5559       && TREE_VEC_LENGTH (convs) == 1
5560       && copy_args_p (fn))
5561     {
5562       tree targ;
5563       arg = TREE_CHAIN (converted_args);
5564       if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
5565         arg = TREE_CHAIN (arg);
5566       arg = TREE_VALUE (arg);
5567
5568       /* Pull out the real argument, disregarding const-correctness.  */
5569       targ = arg;
5570       while (TREE_CODE (targ) == NOP_EXPR
5571              || TREE_CODE (targ) == NON_LVALUE_EXPR
5572              || TREE_CODE (targ) == CONVERT_EXPR)
5573         targ = TREE_OPERAND (targ, 0);
5574       if (TREE_CODE (targ) == ADDR_EXPR)
5575         {
5576           targ = TREE_OPERAND (targ, 0);
5577           if (! comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg))),
5578                            TYPE_MAIN_VARIANT (TREE_TYPE (targ)), 1))
5579             targ = NULL_TREE;
5580         }
5581       else
5582         targ = NULL_TREE;
5583
5584       if (targ)
5585         arg = targ;
5586       else
5587         arg = build_indirect_ref (arg, 0);
5588
5589       /* [class.copy]: the copy constructor is implicitly defined even if
5590          the implementation elided its use.  */
5591       if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
5592         mark_used (fn);
5593
5594       /* If we're creating a temp and we already have one, don't create a
5595          new one.  If we're not creating a temp but we get one, use
5596          INIT_EXPR to collapse the temp into our target.  Otherwise, if the
5597          ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
5598          temp or an INIT_EXPR otherwise.  */
5599       if (integer_zerop (TREE_VALUE (args)))
5600         {
5601           if (! real_lvalue_p (arg))
5602             return arg;
5603           else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
5604             {
5605               val = build (VAR_DECL, DECL_CONTEXT (fn));
5606               layout_decl (val, 0);
5607               val = build (TARGET_EXPR, DECL_CONTEXT (fn), val, arg, 0, 0);
5608               TREE_SIDE_EFFECTS (val) = 1;
5609               return val;
5610             }
5611         }
5612       else if (! real_lvalue_p (arg)
5613                || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
5614         {
5615           tree to = stabilize_reference
5616             (build_indirect_ref (TREE_VALUE (args), 0));
5617           val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
5618           TREE_SIDE_EFFECTS (val) = 1;
5619           return build_unary_op (ADDR_EXPR, val, 0);
5620         }
5621     }
5622   else if (DECL_NAME (fn) == ansi_opname[MODIFY_EXPR]
5623            && copy_args_p (fn)
5624            && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
5625     {
5626       tree to = stabilize_reference
5627         (build_indirect_ref (TREE_VALUE (converted_args), 0));
5628       arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
5629       val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
5630       TREE_SIDE_EFFECTS (val) = 1;
5631       return val;
5632     }
5633
5634   mark_used (fn);
5635
5636   if (DECL_CONTEXT (fn) && IS_SIGNATURE (DECL_CONTEXT (fn)))
5637     return build_signature_method_call (fn, converted_args);
5638   else if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
5639     {
5640       tree t, *p = &TREE_VALUE (converted_args);
5641       tree binfo = get_binfo
5642         (DECL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
5643       *p = convert_pointer_to_real (binfo, *p);
5644       if (TREE_SIDE_EFFECTS (*p))
5645         *p = save_expr (*p);
5646       t = build_pointer_type (TREE_TYPE (fn));
5647       fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
5648       TREE_TYPE (fn) = t;
5649     }
5650   else if (DECL_INLINE (fn))
5651     fn = inline_conversion (fn);
5652   else
5653     fn = build_addr_func (fn);
5654
5655   /* Recognize certain built-in functions so we can make tree-codes
5656      other than CALL_EXPR.  We do this when it enables fold-const.c
5657      to do something useful.  */
5658
5659   if (TREE_CODE (fn) == ADDR_EXPR
5660       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5661       && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
5662     switch (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)))
5663       {
5664       case BUILT_IN_ABS:
5665       case BUILT_IN_LABS:
5666       case BUILT_IN_FABS:
5667         if (converted_args == 0)
5668           return integer_zero_node;
5669         return build_unary_op (ABS_EXPR, TREE_VALUE (converted_args), 0);
5670       }
5671
5672   fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
5673   if (TREE_TYPE (fn) == void_type_node)
5674     return fn;
5675   fn = require_complete_type (fn);
5676   if (IS_AGGR_TYPE (TREE_TYPE (fn)))
5677     fn = build_cplus_new (TREE_TYPE (fn), fn);
5678   return convert_from_reference (fn);
5679 }
5680
5681 static tree
5682 build_new_method_call (instance, name, args, basetype_path, flags)
5683      tree instance, name, args, basetype_path;
5684      int flags;
5685 {
5686   struct z_candidate *candidates = 0, *cand;
5687   tree explicit_targs = NULL_TREE;
5688   tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
5689   tree pretty_name;
5690   tree user_args = args;
5691   tree templates = NULL_TREE;
5692   int template_only = 0;
5693
5694   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5695     {
5696       explicit_targs = TREE_OPERAND (name, 1);
5697       name = TREE_OPERAND (name, 0);
5698       if (TREE_CODE (name) == TEMPLATE_DECL)
5699         name = DECL_NAME (name);
5700       template_only = 1;
5701     }
5702
5703   /* If there is an extra argument for controlling virtual bases,
5704      remove it for error reporting.  */
5705   if (flags & LOOKUP_HAS_IN_CHARGE)
5706     user_args = TREE_CHAIN (args);
5707
5708   args = resolve_args (args);
5709
5710   if (args == error_mark_node)
5711     return error_mark_node;
5712
5713   if (instance == NULL_TREE)
5714     basetype = BINFO_TYPE (basetype_path);
5715   else
5716     {
5717       if (TREE_CODE (instance) == OFFSET_REF)
5718         instance = resolve_offset_ref (instance);
5719       if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5720         instance = convert_from_reference (instance);
5721       basetype = TREE_TYPE (instance);
5722
5723       /* XXX this should be handled before we get here.  */
5724       if (! IS_AGGR_TYPE (basetype)
5725           && ! (TYPE_LANG_SPECIFIC (basetype)
5726                 && (IS_SIGNATURE_POINTER (basetype)
5727                     || IS_SIGNATURE_REFERENCE (basetype))))
5728         {
5729           if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5730             cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5731                       name, instance, basetype);
5732
5733           return error_mark_node;
5734         }
5735
5736       /* If `instance' is a signature pointer/reference and `name' is
5737          not a constructor, we are calling a signature member function.
5738          In that case set the `basetype' to the signature type.  */
5739       if ((IS_SIGNATURE_POINTER (basetype)
5740            || IS_SIGNATURE_REFERENCE (basetype))
5741           && TYPE_IDENTIFIER (basetype) != name)
5742         basetype = SIGNATURE_TYPE (basetype);
5743     }
5744
5745   if (basetype_path == NULL_TREE)
5746     basetype_path = TYPE_BINFO (basetype);
5747
5748   if (instance)
5749     {
5750       instance_ptr = build_this (instance);
5751
5752       if (! template_only)
5753         {
5754           /* XXX this should be handled before we get here.  */
5755           fns = build_field_call (basetype_path, instance_ptr, name, args);
5756           if (fns)
5757             return fns;
5758         }
5759     }
5760   else
5761     {
5762       instance_ptr = build_int_2 (0, 0);
5763       TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
5764     }
5765
5766   pretty_name
5767     = (name == ctor_identifier ? constructor_name (basetype) : name);
5768
5769   fns = lookup_fnfields (basetype_path, name, 1);
5770
5771   if (fns == error_mark_node)
5772     return error_mark_node;
5773   if (fns)
5774     {
5775       tree t = TREE_VALUE (fns);
5776       if (name == ctor_identifier && TYPE_USES_VIRTUAL_BASECLASSES (basetype)
5777           && ! (flags & LOOKUP_HAS_IN_CHARGE))
5778         {
5779           flags |= LOOKUP_HAS_IN_CHARGE;
5780           args = scratch_tree_cons (NULL_TREE, integer_one_node, args);
5781         }
5782       mem_args = scratch_tree_cons (NULL_TREE, instance_ptr, args);
5783       for (; t; t = DECL_CHAIN (t))
5784         {
5785           tree this_arglist;
5786
5787           /* We can end up here for copy-init of same or base class.  */
5788           if (name == ctor_identifier
5789               && (flags & LOOKUP_ONLYCONVERTING)
5790               && DECL_NONCONVERTING_P (t))
5791             continue;
5792           if (TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)
5793             this_arglist = mem_args;
5794           else
5795             this_arglist = args;
5796
5797           if (TREE_CODE (t) == TEMPLATE_DECL)
5798             {
5799               /* A member template. */
5800               templates = scratch_tree_cons (NULL_TREE, t, templates);
5801               candidates = 
5802                 add_template_candidate (candidates, t, explicit_targs,
5803                                         this_arglist,
5804                                         TREE_TYPE (name), flags); 
5805             }
5806           else if (! template_only)
5807             candidates = add_function_candidate (candidates, t,
5808                                                  this_arglist, flags);
5809
5810           if (candidates)
5811             candidates->basetype_path = TREE_PURPOSE (fns);
5812         }
5813     }
5814
5815   if (! any_viable (candidates))
5816     {
5817       /* XXX will LOOKUP_SPECULATIVELY be needed when this is done?  */
5818       if (flags & LOOKUP_SPECULATIVELY)
5819         return NULL_TREE;
5820       cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype,
5821                 pretty_name, user_args, TREE_TYPE (TREE_TYPE (instance_ptr)));
5822       print_z_candidates (candidates);
5823       return error_mark_node;
5824     }
5825   candidates = splice_viable (candidates);
5826   cand = tourney (candidates);
5827
5828   if (cand == 0)
5829     {
5830       cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
5831                 user_args);
5832       print_z_candidates (candidates);
5833       return error_mark_node;
5834     }
5835
5836   enforce_access (cand->basetype_path, cand->fn);
5837   if (DECL_ABSTRACT_VIRTUAL_P (cand->fn)
5838       && instance == current_class_ref
5839       && DECL_CONSTRUCTOR_P (current_function_decl)
5840       && ! (flags & LOOKUP_NONVIRTUAL)
5841       && value_member (cand->fn, get_abstract_virtuals (basetype)))
5842     cp_error ("abstract virtual `%#D' called from constructor", cand->fn);
5843   if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5844       && TREE_CODE (instance_ptr) == NOP_EXPR
5845       && TREE_OPERAND (instance_ptr, 0) == error_mark_node)
5846     cp_error ("cannot call member function `%D' without object", cand->fn);
5847
5848   if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5849       && ((instance == current_class_ref && (dtor_label || ctor_label))
5850           || resolves_to_fixed_type_p (instance, 0)))
5851     flags |= LOOKUP_NONVIRTUAL;
5852
5853   /* Pedantically, normal function declarations are never considered
5854      to refer to template instantiations, so we only do this with
5855      -fguiding-decls.  */ 
5856   if (flag_guiding_decls && templates && ! cand->template 
5857       && ! DECL_INITIAL (cand->fn))
5858     add_maybe_template (cand->fn, templates);
5859
5860   return build_over_call
5861     (cand->fn, cand->convs,
5862      TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE ? mem_args : args,
5863      flags);
5864 }
5865
5866 /* Compare two implicit conversion sequences that differ only in their
5867    qualification conversion.  Subroutine of compare_ics.  */
5868
5869 static int
5870 compare_qual (ics1, ics2)
5871      tree ics1, ics2;
5872 {
5873   tree to1 = TREE_TYPE (ics1);
5874   tree to2 = TREE_TYPE (ics2);
5875
5876   if (TYPE_PTRMEMFUNC_P (to1))
5877     to1 = TYPE_PTRMEMFUNC_FN_TYPE (to1);
5878   if (TYPE_PTRMEMFUNC_P (to2))
5879     to2 = TYPE_PTRMEMFUNC_FN_TYPE (to2);
5880
5881   to1 = TREE_TYPE (to1);
5882   to2 = TREE_TYPE (to2);
5883
5884   if (TREE_CODE (to1) == OFFSET_TYPE)
5885     {
5886       to1 = TREE_TYPE (to1);
5887       to2 = TREE_TYPE (to2);
5888     }
5889
5890   if (TYPE_READONLY (to1) >= TYPE_READONLY (to2)
5891       && TYPE_VOLATILE (to1) > TYPE_VOLATILE (to2))
5892     return -1;
5893   else if (TYPE_READONLY (to1) > TYPE_READONLY (to2)
5894            && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2))
5895     return -1;
5896   else if (TYPE_READONLY (to1) <= TYPE_READONLY (to2)
5897            && TYPE_VOLATILE (to1) < TYPE_VOLATILE (to2))
5898     return 1;
5899   else if (TYPE_READONLY (to1) < TYPE_READONLY (to2)
5900            && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2))
5901     return 1;
5902   return 0;
5903 }
5904
5905 /* Determine whether standard conversion sequence ICS1 is a proper
5906    subsequence of ICS2.  We assume that a conversion of the same code
5907    between the same types indicates a subsequence.  */
5908
5909 static int
5910 is_subseq (ics1, ics2)
5911      tree ics1, ics2;
5912 {
5913   /* Do not consider lvalue transformations here.  */
5914   if (TREE_CODE (ics2) == RVALUE_CONV
5915       || TREE_CODE (ics2) == LVALUE_CONV)
5916     return 0;
5917
5918   for (;; ics2 = TREE_OPERAND (ics2, 0))
5919     {
5920       if (TREE_CODE (ics2) == TREE_CODE (ics1)
5921           && comptypes (TREE_TYPE (ics2), TREE_TYPE (ics1), 1)
5922           && comptypes (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5923                         TREE_TYPE (TREE_OPERAND (ics1, 0)), 1))
5924         return 1;
5925
5926       if (TREE_CODE (ics2) == USER_CONV
5927           || TREE_CODE (ics2) == AMBIG_CONV
5928           || TREE_CODE (ics2) == IDENTITY_CONV)
5929         return 0;
5930     }
5931 }
5932
5933 /* Compare two implicit conversion sequences according to the rules set out in
5934    [over.ics.rank].  Return values:
5935
5936       1: ics1 is better than ics2
5937      -1: ics2 is better than ics1
5938       0: ics1 and ics2 are indistinguishable */
5939
5940 static int
5941 compare_ics (ics1, ics2)
5942      tree ics1, ics2;
5943 {
5944   tree main1, main2;
5945
5946   if (TREE_CODE (ics1) == QUAL_CONV)
5947     main1 = TREE_OPERAND (ics1, 0);
5948   else
5949     main1 = ics1;
5950
5951   if (TREE_CODE (ics2) == QUAL_CONV)
5952     main2 = TREE_OPERAND (ics2, 0);
5953   else
5954     main2 = ics2;
5955
5956   /* Conversions for `this' are PTR_CONVs, but we compare them as though
5957      they were REF_BINDs.  */
5958   if (ICS_THIS_FLAG (ics1))
5959     {
5960       tree t = main1;
5961       if (TREE_CODE (t) == PTR_CONV)
5962         t = TREE_OPERAND (t, 0);
5963       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5964       t = build_conv (REF_BIND, TREE_TYPE (ics1), t);
5965       ICS_STD_RANK (t) = ICS_STD_RANK (main1);
5966       main1 = ics1 = t;
5967     }
5968   if (ICS_THIS_FLAG (ics2))
5969     {
5970       tree t = main2;
5971       if (TREE_CODE (t) == PTR_CONV)
5972         t = TREE_OPERAND (t, 0);
5973       t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5974       t = build_conv (REF_BIND, TREE_TYPE (ics2), t);
5975       ICS_STD_RANK (t) = ICS_STD_RANK (main2);
5976       main2 = ics2 = t;
5977     }
5978
5979   if (ICS_RANK (ics1) > ICS_RANK (ics2))
5980     return -1;
5981   else if (ICS_RANK (ics1) < ICS_RANK (ics2))
5982     return 1;
5983
5984   if (ICS_RANK (ics1) == BAD_RANK)
5985     {
5986       if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5987           || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5988         return -1;
5989       else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5990                || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5991         return 1;
5992
5993       /* else fall through */
5994     }
5995
5996   /* User-defined  conversion sequence U1 is a better conversion sequence
5997      than another user-defined conversion sequence U2 if they contain the
5998      same user-defined conversion operator or constructor and if the sec-
5999      ond standard conversion sequence of U1 is  better  than  the  second
6000      standard conversion sequence of U2.  */
6001
6002   if (ICS_USER_FLAG (ics1))
6003     {
6004       tree t1, t2;
6005
6006       for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
6007         if (TREE_CODE (t1) == AMBIG_CONV)
6008           return 0;
6009       for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
6010         if (TREE_CODE (t2) == AMBIG_CONV)
6011           return 0;
6012
6013       if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
6014         return 0;
6015       else if (ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
6016         return -1;
6017       else if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
6018         return 1;
6019
6020       /* else fall through */
6021     }
6022
6023 #if 0 /* Handled by ranking */
6024   /* A conversion that is not a conversion of a pointer,  or  pointer  to
6025      member,  to  bool  is  better than another conversion that is such a
6026      conversion.  */
6027 #endif
6028
6029   if (TREE_CODE (main1) != TREE_CODE (main2))
6030     {
6031       /* ...if S1  is  a  proper  subsequence  of  S2  */
6032       if (is_subseq (main1, main2))
6033         return 1;
6034       if (is_subseq (main2, main1))
6035         return -1;
6036       return 0;
6037     }
6038
6039   if (TREE_CODE (main1) == PTR_CONV || TREE_CODE (main1) == PMEM_CONV
6040       || TREE_CODE (main1) == REF_BIND || TREE_CODE (main1) == BASE_CONV)
6041     {
6042       tree to1 = TREE_TYPE (main1);
6043       tree from1 = TREE_TYPE (TREE_OPERAND (main1, 0));
6044       tree to2 = TREE_TYPE (main2);
6045       tree from2 = TREE_TYPE (TREE_OPERAND (main2, 0));
6046       int distf, distt;
6047
6048       /* Standard conversion sequence S1 is a better conversion sequence than
6049          standard conversion sequence S2 if...
6050
6051          S1 and S2 differ only in their qualification conversion  and  they
6052          yield types identical except for cv-qualifiers and S2 adds all the
6053          qualifiers that S1 adds (and in the same places) and S2  adds  yet
6054          more  cv-qualifiers  than  S1,  or the similar case with reference
6055          binding15).  */
6056       if (TREE_CODE (main1) == REF_BIND)
6057         {
6058           if (TYPE_MAIN_VARIANT (TREE_TYPE (to1))
6059               == TYPE_MAIN_VARIANT (TREE_TYPE (to2)))
6060             return compare_qual (ics1, ics2);
6061         }
6062       else if (TREE_CODE (main1) != BASE_CONV && from1 == from2 && to1 == to2)
6063         return compare_qual (ics1, ics2);
6064         
6065       if (TYPE_PTRMEMFUNC_P (to1))
6066         {
6067           to1 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to1)));
6068           from1 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from1)));
6069         }
6070       else if (TREE_CODE (main1) != BASE_CONV)
6071         {
6072           to1 = TREE_TYPE (to1);
6073           if (TREE_CODE (main1) != REF_BIND)
6074             from1 = TREE_TYPE (from1);
6075
6076           if (TREE_CODE (to1) == OFFSET_TYPE)
6077             {
6078               to1 = TYPE_OFFSET_BASETYPE (to1);
6079               from1 = TYPE_OFFSET_BASETYPE (from1);
6080             }
6081         }
6082
6083       if (TYPE_PTRMEMFUNC_P (to2))
6084         {
6085           to2 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to2)));
6086           from2 = TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from2)));
6087         }
6088       else if (TREE_CODE (main1) != BASE_CONV)
6089         {
6090           to2 = TREE_TYPE (to2);
6091           if (TREE_CODE (main1) != REF_BIND)
6092             from2 = TREE_TYPE (from2);
6093
6094           if (TREE_CODE (to2) == OFFSET_TYPE)
6095             {
6096               to2 = TYPE_OFFSET_BASETYPE (to2);
6097               from2 = TYPE_OFFSET_BASETYPE (from2);
6098             }
6099         }
6100
6101       if (! (IS_AGGR_TYPE (from1) && IS_AGGR_TYPE (from2)))
6102         return 0;
6103
6104       /* The sense of pmem conversions is reversed from that of the other
6105          conversions.  */
6106       if (TREE_CODE (main1) == PMEM_CONV)
6107         {
6108           tree t = from1; from1 = from2; from2 = t;
6109           t = to1; to1 = to2; to2 = t;
6110         }
6111
6112       distf = get_base_distance (from1, from2, 0, 0);
6113       if (distf == -1)
6114         {
6115           distf = -get_base_distance (from2, from1, 0, 0);
6116           if (distf == 1)
6117             return 0;
6118         }
6119
6120       /* If class B is derived directly or indirectly from class A,
6121          conver- sion of B* to A* is better than conversion of B* to
6122          void*, and conversion of A* to void* is better than
6123          conversion of B* to void*.  */
6124
6125       if (TREE_CODE (to1) == VOID_TYPE && TREE_CODE (to2) == VOID_TYPE)
6126         {
6127           if (distf > 0)
6128             return 1;
6129           else if (distf < 0)
6130             return -1;
6131         }
6132       else if (TREE_CODE (to2) == VOID_TYPE && IS_AGGR_TYPE (to1)
6133                && get_base_distance (to1, from1, 0, 0) != -1)
6134         return 1;
6135       else if (TREE_CODE (to1) == VOID_TYPE && IS_AGGR_TYPE (to2)
6136                && get_base_distance (to2, from2, 0, 0) != -1)
6137         return -1;
6138
6139       if (! (IS_AGGR_TYPE (to1) && IS_AGGR_TYPE (to2)))
6140         return 0;
6141
6142       /* If  class B is derived directly or indirectly from class A and class
6143          C is derived directly or indirectly from B */
6144
6145       distt = get_base_distance (to1, to2, 0, 0);
6146       if (distt == -1)
6147         {
6148           distt = -get_base_distance (to2, to1, 0, 0);
6149           if (distt == 1)
6150             return 0;
6151         }
6152
6153       /* --conversion of C* to B* is better than conversion of C* to A*, */
6154       if (distf == 0)
6155         {
6156           if (distt > 0)
6157             return -1;
6158           else if (distt < 0)
6159             return 1;
6160         }
6161       /* --conversion of B* to A* is better than conversion of C* to A*, */
6162       else if (distt == 0)
6163         {
6164           if (distf > 0)
6165             return 1;
6166           else if (distf < 0)
6167             return -1;
6168         }
6169     }
6170   else if (TREE_CODE (TREE_TYPE (main1)) == POINTER_TYPE
6171            || TYPE_PTRMEMFUNC_P (TREE_TYPE (main1)))
6172     {
6173       if (TREE_TYPE (main1) == TREE_TYPE (main2))
6174         return compare_qual (ics1, ics2);
6175
6176 #if 0 /* This is now handled by making identity better than anything else.  */
6177       /* existing practice, not WP-endorsed: const char * -> const char *
6178          is better than char * -> const char *.  (jason 6/29/96) */
6179       if (TREE_TYPE (ics1) == TREE_TYPE (ics2))
6180         return -compare_qual (main1, main2);
6181 #endif
6182     }
6183
6184   return 0;
6185 }
6186
6187 /* The source type for this standard conversion sequence.  */
6188
6189 static tree
6190 source_type (t)
6191      tree t;
6192 {
6193   for (;; t = TREE_OPERAND (t, 0))
6194     {
6195       if (TREE_CODE (t) == USER_CONV
6196           || TREE_CODE (t) == AMBIG_CONV
6197           || TREE_CODE (t) == IDENTITY_CONV)
6198         return TREE_TYPE (t);
6199     }
6200   my_friendly_abort (1823);
6201 }
6202
6203 /* Compare two candidates for overloading as described in
6204    [over.match.best].  Return values:
6205
6206       1: cand1 is better than cand2
6207      -1: cand2 is better than cand1
6208       0: cand1 and cand2 are indistinguishable */
6209
6210 static int
6211 joust (cand1, cand2)
6212      struct z_candidate *cand1, *cand2;
6213 {
6214   int winner = 0;
6215   int i, off1 = 0, off2 = 0, len;
6216
6217   /* Candidates that involve bad conversions are always worse than those
6218      that don't.  */
6219   if (cand1->viable > cand2->viable)
6220     return 1;
6221   if (cand1->viable < cand2->viable)
6222     return -1;
6223
6224   /* a viable function F1
6225      is defined to be a better function than another viable function F2  if
6226      for  all arguments i, ICSi(F1) is not a worse conversion sequence than
6227      ICSi(F2), and then */
6228
6229   /* for some argument j, ICSj(F1) is a better conversion  sequence  than
6230      ICSj(F2) */
6231
6232   /* For comparing static and non-static member functions, we ignore the
6233      implicit object parameter of the non-static function.  The WP says to
6234      pretend that the static function has an object parm, but that won't
6235      work with operator overloading.  */
6236   len = TREE_VEC_LENGTH (cand1->convs);
6237   if (len != TREE_VEC_LENGTH (cand2->convs))
6238     {
6239       if (DECL_STATIC_FUNCTION_P (cand1->fn)
6240           && ! DECL_STATIC_FUNCTION_P (cand2->fn))
6241         off2 = 1;
6242       else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
6243                && DECL_STATIC_FUNCTION_P (cand2->fn))
6244         {
6245           off1 = 1;
6246           --len;
6247         }
6248       else
6249         my_friendly_abort (42);
6250     }
6251
6252   for (i = 0; i < len; ++i)
6253     {
6254       tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
6255       tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
6256       int comp = compare_ics (t1, t2);
6257
6258       if (comp != 0)
6259         {
6260 #if 0 /* move this warning to tourney.  */
6261           if (warn_sign_promo
6262               && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
6263               && TREE_CODE (t1) == STD_CONV
6264               && TREE_CODE (t2) == STD_CONV
6265               && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
6266               && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
6267               && (TYPE_PRECISION (TREE_TYPE (t1))
6268                   == TYPE_PRECISION (TREE_TYPE (t2)))
6269               && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
6270                   || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
6271                       == ENUMERAL_TYPE)))
6272             {
6273               tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
6274               tree type1, type2;
6275               if (comp > 0)
6276                 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2);
6277               else
6278                 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1);
6279
6280               cp_warning ("passing `%T' chooses `%T' over `%T'",
6281                           type, type1, type2);
6282               cp_warning ("  in call to `%D'", DECL_NAME (cand1->fn));
6283             }
6284 #endif
6285
6286           if (winner && comp != winner)
6287             {
6288               winner = 0;
6289               goto tweak;
6290             }
6291           winner = comp;
6292         }
6293     }
6294
6295 #if 0 /* move this warning to tourney.  */
6296   /* warn about confusing overload resolution */
6297   if (winner && cand1->second_conv
6298       && ! DECL_CONSTRUCTOR_P (cand1->fn)
6299       && ! DECL_CONSTRUCTOR_P (cand2->fn))
6300     {
6301       int comp = compare_ics (cand1->second_conv, cand2->second_conv);
6302       if (comp && comp != winner)
6303         {
6304           struct z_candidate *w, *l;
6305           if (winner == 1)
6306             w = cand1, l = cand2;
6307           else
6308             w = cand2, l = cand1;
6309           cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
6310           cp_warning ("  for conversion from `%T' to `%T'",
6311                       TREE_TYPE (source_type (TREE_VEC_ELT (w->convs, 0))),
6312                       TREE_TYPE (w->second_conv));
6313           cp_warning ("  because conversion sequence for `this' argument is better");
6314         }
6315     }
6316 #endif
6317
6318   if (winner)
6319     return winner;
6320
6321   /* or, if not that,
6322      F1 is a non-template function and F2 is a template function */
6323
6324   if (! cand1->template && cand2->template)
6325     return 1;
6326   else if (cand1->template && ! cand2->template)
6327     return -1;
6328   else if (cand1->template && cand2->template)
6329     winner = more_specialized
6330       (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
6331        NULL_TREE);
6332
6333   /* or, if not that,
6334      the  context  is  an  initialization by user-defined conversion (see
6335      _dcl.init_  and  _over.match.user_)  and  the  standard   conversion
6336      sequence  from  the return type of F1 to the destination type (i.e.,
6337      the type of the entity being initialized)  is  a  better  conversion
6338      sequence  than the standard conversion sequence from the return type
6339      of F2 to the destination type.  */
6340
6341   if (! winner && cand1->second_conv)
6342     winner = compare_ics (cand1->second_conv, cand2->second_conv);
6343
6344   /* If the built-in candidates are the same, arbitrarily pick one.  */
6345   if (! winner && cand1->fn == cand2->fn
6346       && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
6347     {
6348       for (i = 0; i < len; ++i)
6349         if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
6350                          TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
6351           break;
6352       if (i == TREE_VEC_LENGTH (cand1->convs))
6353         return 1;
6354
6355       /* Kludge around broken overloading rules whereby
6356          Integer a, b; test ? a : b; is ambiguous, since there's a builtin
6357          that takes references and another that takes values.  */
6358       if (cand1->fn == ansi_opname[COND_EXPR])
6359         {
6360           tree c1 = TREE_VEC_ELT (cand1->convs, 1);
6361           tree c2 = TREE_VEC_ELT (cand2->convs, 1);
6362           tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
6363           tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
6364
6365           if (comptypes (t1, t2, 1))
6366             {
6367               if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
6368                 return 1;
6369               if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
6370                 return -1;
6371             }
6372         }
6373     }
6374
6375 tweak:
6376
6377   /* Extension: If the worst conversion for one candidate is worse than the
6378      worst conversion for the other, take the first.  */
6379   if (! winner && ! pedantic)
6380     {
6381       int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
6382
6383       for (i = 0; i < len; ++i)
6384         {
6385           if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
6386             rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
6387           if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
6388             rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
6389         }
6390
6391       if (rank1 < rank2)
6392         return 1;
6393       if (rank1 > rank2)
6394         return -1;
6395     }
6396
6397   return winner;
6398 }
6399
6400 /* Given a list of candidates for overloading, find the best one, if any.
6401    This algorithm has a worst case of O(2n) (winner is last), and a best
6402    case of O(n/2) (totally ambiguous); much better than a sorting
6403    algorithm.  */
6404
6405 static struct z_candidate *
6406 tourney (candidates)
6407      struct z_candidate *candidates;
6408 {
6409   struct z_candidate *champ = candidates, *challenger;
6410   int fate;
6411
6412   /* Walk through the list once, comparing each current champ to the next
6413      candidate, knocking out a candidate or two with each comparison.  */
6414
6415   for (challenger = champ->next; challenger; )
6416     {
6417       fate = joust (champ, challenger);
6418       if (fate == 1)
6419         challenger = challenger->next;
6420       else
6421         {
6422           if (fate == 0)
6423             {
6424               champ = challenger->next;
6425               if (champ == 0)
6426                 return 0;
6427             }
6428           else
6429             champ = challenger;
6430
6431           challenger = champ->next;
6432         }
6433     }
6434
6435   /* Make sure the champ is better than all the candidates it hasn't yet
6436      been compared to.  This may do one more comparison than necessary.  Oh
6437      well.  */
6438
6439   for (challenger = candidates; challenger != champ;
6440        challenger = challenger->next)
6441     {
6442       fate = joust (champ, challenger);
6443       if (fate != 1)
6444         return 0;
6445     }
6446
6447   return champ;
6448 }
6449
6450 int
6451 can_convert (to, from)
6452      tree to, from;
6453 {
6454   if (flag_ansi_overloading)
6455     {
6456       tree t = implicit_conversion (to, from, NULL_TREE, LOOKUP_NORMAL);
6457       return (t && ! ICS_BAD_FLAG (t));
6458     }
6459   else
6460     {
6461       struct harshness_code h;
6462       h = convert_harshness (to, from, NULL_TREE);
6463       return (h.code < USER_CODE) && (h.distance >= 0);
6464     }
6465 }
6466
6467 int
6468 can_convert_arg (to, from, arg)
6469      tree to, from, arg;
6470 {
6471   if (flag_ansi_overloading)
6472     {
6473       tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
6474       return (t && ! ICS_BAD_FLAG (t));
6475     }
6476   else
6477     {
6478       struct harshness_code h;
6479       h = convert_harshness (to, from, arg);
6480       return (h.code < USER_CODE) && (h.distance >= 0);
6481     }
6482 }