OSDN Git Service

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