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