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