(all insn and peephole patterns): Rewrite without using arm_output_asm_insn.
[official-gcc.git] / gcc / cp / call.c
blobe957776711d4f0082768d3a1c9cc005843cad460
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 hacked by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
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.
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.
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* High-level class interface. */
25 #include "config.h"
26 #include "tree.h"
27 #include <stdio.h>
28 #include "cp-tree.h"
29 #include "class.h"
30 #include "flags.h"
32 #include "obstack.h"
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
36 extern void sorry ();
38 extern int inhibit_warnings;
39 extern int flag_assume_nonnull_objects;
40 extern tree ctor_label, dtor_label;
42 /* From typeck.c: */
43 extern tree unary_complex_lvalue ();
45 /* Compute the ease with which a conversion can be performed
46 between an expected and the given type. */
47 static struct harshness_code convert_harshness ();
49 #define EVIL_RETURN(ARG) ((ARG).code = EVIL_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))
54 /* Ordering function for overload resolution. Compare two candidates
55 by gross quality. */
56 int
57 rank_for_overload (x, y)
58 struct candidate *x, *y;
60 if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
61 return y->h.code - x->h.code;
62 if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
63 return -1;
65 /* This is set by compute_conversion_costs, for calling a non-const
66 member function from a const member function. */
67 if ((y->harshness[0].code & CONST_CODE) ^ (x->harshness[0].code & CONST_CODE))
68 return y->harshness[0].code - x->harshness[0].code;
70 if (y->h.code & STD_CODE)
72 if (x->h.code & STD_CODE)
73 return y->h.distance - x->h.distance;
74 return 1;
76 if (x->h.code & STD_CODE)
77 return -1;
79 return y->h.code - x->h.code;
82 /* Compare two candidates, argument by argument. */
83 int
84 rank_for_ideal (x, y)
85 struct candidate *x, *y;
87 int i;
89 if (x->h_len != y->h_len)
90 abort ();
92 for (i = 0; i < x->h_len; i++)
94 if (y->harshness[i].code - x->harshness[i].code)
95 return y->harshness[i].code - x->harshness[i].code;
96 if ((y->harshness[i].code & STD_CODE)
97 && (y->harshness[i].distance - x->harshness[i].distance))
98 return y->harshness[i].distance - x->harshness[i].distance;
100 /* They're both the same code. Now see if we're dealing with an
101 integral promotion that needs a finer grain of accuracy. */
102 if (y->harshness[0].code & PROMO_CODE
103 && (y->harshness[i].int_penalty ^ x->harshness[i].int_penalty))
104 return y->harshness[i].int_penalty - x->harshness[i].int_penalty;
106 return 0;
109 /* TYPE is the type we wish to convert to. PARM is the parameter
110 we have to work with. We use a somewhat arbitrary cost function
111 to measure this conversion. */
112 static struct harshness_code
113 convert_harshness (type, parmtype, parm)
114 register tree type, parmtype;
115 tree parm;
117 struct harshness_code h;
118 register enum tree_code codel;
119 register enum tree_code coder;
121 h.code = 0;
122 h.distance = 0;
123 h.int_penalty = 0;
125 #ifdef GATHER_STATISTICS
126 n_convert_harshness++;
127 #endif
129 if (TYPE_PTRMEMFUNC_P (type))
130 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
131 if (TYPE_PTRMEMFUNC_P (parmtype))
132 parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
134 codel = TREE_CODE (type);
135 coder = TREE_CODE (parmtype);
137 if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
138 return ZERO_RETURN (h);
140 if (coder == ERROR_MARK)
141 return EVIL_RETURN (h);
143 if (codel == POINTER_TYPE && fntype_p (parmtype))
145 tree p1, p2;
146 struct harshness_code h1, h2;
148 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
149 type = TREE_TYPE (type);
151 if (coder == POINTER_TYPE)
153 parmtype = TREE_TYPE (parmtype);
154 coder = TREE_CODE (parmtype);
157 if (coder != TREE_CODE (type))
158 return EVIL_RETURN (h);
160 /* We allow the default conversion between function type
161 and pointer-to-function type for free. */
162 if (type == parmtype)
163 return ZERO_RETURN (h);
165 /* Compare return types. */
166 p1 = TREE_TYPE (type);
167 p2 = TREE_TYPE (parmtype);
168 h2 = convert_harshness (p1, p2, NULL_TREE);
169 if (h2.code & EVIL_CODE)
170 return h2;
172 h1.code = TRIVIAL_CODE;
173 h1.distance = 0;
175 if (h2.distance != 0)
177 tree binfo;
179 /* This only works for pointers. */
180 if (TREE_CODE (p1) != POINTER_TYPE
181 && TREE_CODE (p1) != REFERENCE_TYPE)
182 return EVIL_RETURN (h);
184 p1 = TREE_TYPE (p1);
185 p2 = TREE_TYPE (p2);
186 /* Don't die if we happen to be dealing with void*. */
187 if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
188 return EVIL_RETURN (h);
189 if (h2.distance < 0)
190 binfo = get_binfo (p2, p1, 0);
191 else
192 binfo = get_binfo (p1, p2, 0);
194 if (! BINFO_OFFSET_ZEROP (binfo))
196 static int explained = 0;
197 if (h2.distance < 0)
198 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p2, p1);
199 else
200 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p1, p2);
202 if (! explained++)
203 sorry ("(because pointer values change during conversion)");
204 return EVIL_RETURN (h);
208 h1.code |= h2.code;
209 if (h2.distance > h1.distance)
210 h1.distance = h2.distance;
212 p1 = TYPE_ARG_TYPES (type);
213 p2 = TYPE_ARG_TYPES (parmtype);
214 while (p1 && TREE_VALUE (p1) != void_type_node
215 && p2 && TREE_VALUE (p2) != void_type_node)
217 h2 = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2),
218 NULL_TREE);
219 if (h2.code & EVIL_CODE)
220 return h2;
222 if (h2.distance)
224 /* This only works for pointers and references. */
225 if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
226 && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
227 return EVIL_RETURN (h);
228 h2.distance = - h2.distance;
231 h1.code |= h2.code;
232 if (h2.distance > h1.distance)
233 h1.distance = h2.distance;
234 p1 = TREE_CHAIN (p1);
235 p2 = TREE_CHAIN (p2);
237 if (p1 == p2)
238 return h1;
239 if (p2)
241 if (p1)
242 return EVIL_RETURN (h);
243 h1.code |= ELLIPSIS_CODE;
244 return h1;
246 if (p1)
248 if (TREE_PURPOSE (p1) == NULL_TREE)
249 h1.code |= EVIL_CODE;
250 return h1;
253 else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
255 /* Get to the OFFSET_TYPE that this might be. */
256 type = TREE_TYPE (type);
258 if (coder != TREE_CODE (type))
259 return EVIL_RETURN (h);
261 if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
262 h.code = 0;
263 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (type),
264 TYPE_OFFSET_BASETYPE (parmtype)))
266 h.code = STD_CODE;
267 h.distance = 1;
269 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (parmtype),
270 TYPE_OFFSET_BASETYPE (type)))
272 h.code = STD_CODE;
273 h.distance = -1;
275 else
276 return EVIL_RETURN (h);
277 /* Now test the OFFSET_TYPE's target compatibility. */
278 type = TREE_TYPE (type);
279 parmtype = TREE_TYPE (parmtype);
282 if (coder == UNKNOWN_TYPE)
284 if (codel == FUNCTION_TYPE
285 || codel == METHOD_TYPE
286 || (codel == POINTER_TYPE
287 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
288 || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
289 return TRIVIAL_RETURN (h);
290 return EVIL_RETURN (h);
293 if (coder == VOID_TYPE)
294 return EVIL_RETURN (h);
296 if (INTEGRAL_CODE_P (codel))
298 /* Control equivalence of ints an enums. */
300 if (codel == ENUMERAL_TYPE
301 && flag_int_enum_equivalence == 0)
303 /* Enums can be converted to ints, but not vice-versa. */
304 if (coder != ENUMERAL_TYPE
305 || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
306 return EVIL_RETURN (h);
309 /* else enums and ints (almost) freely interconvert. */
311 if (INTEGRAL_CODE_P (coder))
313 if (TYPE_MAIN_VARIANT (type)
314 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
316 h.code = PROMO_CODE;
317 #if 0 /* What purpose does this serve? -jason */
318 /* A char, short, wchar_t, etc., should promote to an int if
319 it can handle it, otherwise to an unsigned. So we'll make
320 an unsigned. */
321 if (type != integer_type_node)
322 h.int_penalty = 1;
323 #endif
325 else
326 h.code = STD_CODE;
328 return h;
330 else if (coder == REAL_TYPE)
332 h.code = STD_CODE;
333 h.distance = 0;
334 return h;
338 if (codel == REAL_TYPE)
340 if (coder == REAL_TYPE)
342 if (TYPE_MAIN_VARIANT (type)
343 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
344 h.code = PROMO_CODE;
345 else
346 h.code = STD_CODE;
348 return h;
350 else if (INTEGRAL_CODE_P (coder))
352 h.code = STD_CODE;
353 h.distance = 0;
354 return h;
358 /* Convert arrays which have not previously been converted. */
359 if (codel == ARRAY_TYPE)
360 codel = POINTER_TYPE;
361 if (coder == ARRAY_TYPE)
362 coder = POINTER_TYPE;
364 /* Conversions among pointers */
365 if (codel == POINTER_TYPE && coder == POINTER_TYPE)
367 register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
368 register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
369 int penalty = 4 * (ttl != ttr);
371 /* Anything converts to void *. void * converts to anything.
372 Since these may be `const void *' (etc.) use VOID_TYPE
373 instead of void_type_node. Otherwise, the targets must be the same,
374 except that we do allow (at some cost) conversion between signed and
375 unsigned pointer types. */
377 if ((TREE_CODE (ttl) == METHOD_TYPE
378 || TREE_CODE (ttl) == FUNCTION_TYPE)
379 && TREE_CODE (ttl) == TREE_CODE (ttr))
381 if (comptypes (ttl, ttr, -1))
383 h.code = penalty ? STD_CODE : 0;
384 h.distance = 0;
386 else
387 h.code = EVIL_CODE;
388 return h;
391 #if 1
392 if (TREE_CODE (ttl) != VOID_TYPE && TREE_CODE (ttr) != VOID_TYPE)
394 if (TREE_UNSIGNED (ttl) != TREE_UNSIGNED (ttr))
396 ttl = unsigned_type (ttl);
397 ttr = unsigned_type (ttr);
398 penalty = 10;
400 if (! comp_target_types (ttl, ttr, 0))
401 return EVIL_RETURN (h);
403 #else
404 if (!(TREE_CODE (ttl) == VOID_TYPE
405 || TREE_CODE (ttr) == VOID_TYPE
406 || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
407 && (ttl = unsigned_type (ttl),
408 ttr = unsigned_type (ttr),
409 penalty = 10, 0))
410 || (comp_target_types (ttl, ttr, 0))))
411 return EVIL_RETURN (h);
412 #endif
414 if (penalty == 10 || ttr == ttl)
416 tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
418 /* If one was unsigned but the other wasn't, then we need to
419 do a standard conversion from T to unsigned T. */
420 if (penalty == 10)
421 h.code = PROMO_CODE; /* was STD_CODE */
422 else
423 h.code = 0;
425 /* Note conversion from `T*' to `const T*',
426 or `T*' to `volatile T*'. */
427 if (ttl == ttr
428 && ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
429 || (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2))))
430 h.code |= QUAL_CODE;
432 h.distance = 0;
433 return h;
437 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
439 int b_or_d = get_base_distance (ttl, ttr, 0, 0);
440 if (b_or_d < 0)
442 b_or_d = get_base_distance (ttr, ttl, 0, 0);
443 if (b_or_d < 0)
444 return EVIL_RETURN (h);
445 h.distance = -b_or_d;
447 else
448 h.distance = b_or_d;
449 h.code = STD_CODE;
450 return h;
453 /* If converting from a `class*' to a `void*', make it
454 less favorable than any inheritance relationship. */
455 if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
457 h.code = STD_CODE;
458 h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
459 return h;
461 h.code = penalty ? STD_CODE : PROMO_CODE;
462 return h;
465 if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
467 /* This is not a bad match, but don't let it beat
468 integer-enum combinations. */
469 if (parm && integer_zerop (parm))
471 h.code = STD_CODE;
472 h.distance = 0;
473 return h;
477 /* C++: one of the types must be a reference type. */
479 tree ttl, ttr;
480 register tree intype = TYPE_MAIN_VARIANT (parmtype);
481 register enum tree_code form = TREE_CODE (intype);
482 int penalty = 0;
484 if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
486 ttl = TYPE_MAIN_VARIANT (type);
488 if (codel == REFERENCE_TYPE)
490 ttl = TREE_TYPE (ttl);
492 /* When passing a non-const argument into a const reference,
493 dig it a little, so a non-const reference is preferred over
494 this one. (mrs) */
495 if (parm && TREE_READONLY (ttl) && ! TREE_READONLY (parm))
496 penalty = 2;
497 else
498 penalty = 0;
500 ttl = TYPE_MAIN_VARIANT (ttl);
502 if (form == OFFSET_TYPE)
504 intype = TREE_TYPE (intype);
505 form = TREE_CODE (intype);
508 if (form == REFERENCE_TYPE)
510 intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
512 if (ttl == intype)
513 return ZERO_RETURN (h);
514 penalty = 2;
516 else
518 /* Can reference be built up? */
519 if (ttl == intype && penalty == 0) {
520 /* Because the READONLY and VIRTUAL bits are not always in
521 the type, this extra check is necessary. The problem
522 should be fixed someplace else, and this extra code
523 removed.
525 Also, if type if a reference, the readonly bits could
526 either be in the outer type (with reference) or on the
527 inner type (the thing being referenced). (mrs) */
528 if (parm
529 && ((TREE_READONLY (parm)
530 && ! (TYPE_READONLY (type)
531 || (TREE_CODE (type) == REFERENCE_TYPE
532 && TYPE_READONLY (TREE_TYPE (type)))))
533 || (TREE_SIDE_EFFECTS (parm)
534 && ! (TYPE_VOLATILE (type)
535 || (TREE_CODE (type) == REFERENCE_TYPE
536 && TYPE_VOLATILE (TREE_TYPE (type)))))))
537 penalty = 2;
538 else
539 return ZERO_RETURN (h);
541 else
542 penalty = 2;
545 else if (form == REFERENCE_TYPE)
547 if (parm)
549 tree tmp = convert_from_reference (parm);
550 intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
552 else
554 intype = parmtype;
556 intype = TREE_TYPE (intype);
557 while (TREE_CODE (intype) == REFERENCE_TYPE);
558 intype = TYPE_MAIN_VARIANT (intype);
561 if (ttl == intype)
562 return ZERO_RETURN (h);
563 else
564 penalty = 2;
567 if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
569 ttl = unsigned_type (ttl);
570 intype = unsigned_type (intype);
571 penalty += 2;
574 ttr = intype;
576 /* If the initializer is not an lvalue, then it does not
577 matter if we make life easier for the programmer
578 by creating a temporary variable with which to
579 hold the result. */
580 if (parm && (INTEGRAL_CODE_P (coder)
581 || coder == REAL_TYPE)
582 && ! lvalue_p (parm))
584 h = convert_harshness (ttl, ttr, NULL_TREE);
585 if (penalty > 2 || h.code != 0)
586 h.code |= STD_CODE;
587 else
588 h.code |= TRIVIAL_CODE;
589 h.distance = 0;
590 return h;
593 if (ttl == ttr)
595 if (penalty > 2)
597 h.code = STD_CODE;
598 h.distance = 0;
600 else
602 h.code = TRIVIAL_CODE;
603 /* We set this here so that build_overload_call_real will be
604 able to see the penalty we found, rather than just looking
605 at a TRIVIAL_CODE with no other information. */
606 h.int_penalty = penalty;
608 return h;
611 /* Pointers to voids always convert for pointers. But
612 make them less natural than more specific matches. */
613 if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
615 if (TREE_TYPE (ttl) == void_type_node
616 || TREE_TYPE (ttr) == void_type_node)
618 h.code = STD_CODE;
619 h.distance = 0;
620 return h;
624 if (parm && codel != REFERENCE_TYPE)
626 h = convert_harshness (ttl, ttr, NULL_TREE);
627 if (penalty == 2)
628 h.code |= QUAL_CODE;
629 else if (penalty == 4)
630 h.code |= STD_CODE;
631 h.distance = 0;
632 return h;
635 /* Here it does matter. If this conversion is from derived to base,
636 allow it. Otherwise, types must be compatible in the strong sense. */
637 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
639 int b_or_d = get_base_distance (ttl, ttr, 0, 0);
640 if (b_or_d < 0)
642 b_or_d = get_base_distance (ttr, ttl, 0, 0);
643 if (b_or_d < 0)
644 return EVIL_RETURN (h);
645 h.distance = -b_or_d;
647 /* Say that this conversion is relatively painless.
648 If it turns out that there is a user-defined X(X&)
649 constructor, then that will be invoked, but that's
650 preferable to dealing with other user-defined conversions
651 that may produce surprising results. */
652 else
653 h.distance = b_or_d;
654 h.code = STD_CODE;
655 return h;
658 if (comp_target_types (ttl, intype, 1))
660 if (penalty)
661 h.code = STD_CODE;
662 h.distance = 0;
663 return h;
667 if (codel == RECORD_TYPE && coder == RECORD_TYPE)
669 int b_or_d = get_base_distance (type, parmtype, 0, 0);
670 if (b_or_d < 0)
672 b_or_d = get_base_distance (parmtype, type, 0, 0);
673 if (b_or_d < 0)
674 return EVIL_RETURN (h);
675 h.distance = -b_or_d;
677 else
678 h.distance = b_or_d;
679 h.code = STD_CODE;
680 return h;
682 return EVIL_RETURN (h);
685 #ifdef DEBUG_MATCHING
686 static char *
687 print_harshness (h)
688 struct harshness_code *h;
690 static char buf[1024];
691 char tmp[1024];
693 bzero (buf, 1024 * sizeof (char));
694 strcat (buf, "codes=[");
695 if (h->code & EVIL_CODE)
696 strcat (buf, "EVIL");
697 if (h->code & CONST_CODE)
698 strcat (buf, " CONST");
699 if (h->code & ELLIPSIS_CODE)
700 strcat (buf, " ELLIPSIS");
701 if (h->code & USER_CODE)
702 strcat (buf, " USER");
703 if (h->code & STD_CODE)
704 strcat (buf, " STD");
705 if (h->code & PROMO_CODE)
706 strcat (buf, " PROMO");
707 if (h->code & QUAL_CODE)
708 strcat (buf, " QUAL");
709 if (h->code & TRIVIAL_CODE)
710 strcat (buf, " TRIVIAL");
711 if (buf[0] == '\0')
712 strcat (buf, "0");
714 sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
716 strcat (buf, tmp);
718 return buf;
720 #endif
722 /* Algorithm: For each argument, calculate how difficult it is to
723 make FUNCTION accept that argument. If we can easily tell that
724 FUNCTION won't be acceptable to one of the arguments, then we
725 don't need to compute the ease of converting the other arguments,
726 since it will never show up in the intersection of all arguments'
727 favorite functions.
729 Conversions between builtin and user-defined types are allowed, but
730 no function involving such a conversion is preferred to one which
731 does not require such a conversion. Furthermore, such conversions
732 must be unique. */
734 void
735 compute_conversion_costs (function, tta_in, cp, arglen)
736 tree function;
737 tree tta_in;
738 struct candidate *cp;
739 int arglen;
741 tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
742 tree ttf = ttf_in;
743 tree tta = tta_in;
745 /* Start out with no strikes against. */
746 int evil_strikes = 0;
747 int ellipsis_strikes = 0;
748 int user_strikes = 0;
749 int b_or_d_strikes = 0;
750 int easy_strikes = 0;
752 int strike_index = 0, win;
753 struct harshness_code lose;
755 #ifdef GATHER_STATISTICS
756 n_compute_conversion_costs++;
757 #endif
759 cp->function = function;
760 cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
761 cp->u.bad_arg = 0; /* optimistic! */
763 cp->h.code = 0;
764 cp->h.distance = 0;
765 cp->h.int_penalty = 0;
766 bzero (cp->harshness,
767 (cp->h_len + 1) * sizeof (struct harshness_code));
769 while (ttf && tta)
771 struct harshness_code h;
773 if (ttf == void_list_node)
774 break;
776 if (type_unknown_p (TREE_VALUE (tta)))
778 /* Must perform some instantiation here. */
779 tree rhs = TREE_VALUE (tta);
780 tree lhstype = TREE_VALUE (ttf);
782 /* Keep quiet about possible contravariance violations. */
783 int old_inhibit_warnings = inhibit_warnings;
784 inhibit_warnings = 1;
786 /* @@ This is to undo what `grokdeclarator' does to
787 parameter types. It really should go through
788 something more general. */
790 TREE_TYPE (tta) = unknown_type_node;
791 rhs = instantiate_type (lhstype, rhs, 0);
792 inhibit_warnings = old_inhibit_warnings;
794 if (TREE_CODE (rhs) == ERROR_MARK)
795 h.code = EVIL_CODE;
796 else
797 h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
799 else
801 #ifdef DEBUG_MATCHING
802 static tree old_function = NULL_TREE;
804 if (!old_function || function != old_function)
806 cp_error ("trying %D", function);
807 old_function = function;
810 cp_error (" doing (%T) %E against arg %T",
811 TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
812 TREE_VALUE (ttf));
813 #endif
815 h = convert_harshness (TREE_VALUE (ttf),
816 TREE_TYPE (TREE_VALUE (tta)),
817 TREE_VALUE (tta));
819 #ifdef DEBUG_MATCHING
820 cp_error (" evaluated %s", print_harshness (&h));
821 #endif
824 cp->harshness[strike_index] = h;
825 if ((h.code & EVIL_CODE)
826 || ((h.code & STD_CODE) && h.distance < 0))
828 cp->u.bad_arg = strike_index;
829 evil_strikes = 1;
831 else if (h.code & ELLIPSIS_CODE)
832 ellipsis_strikes += 1;
833 #if 0
834 /* This is never set by `convert_harshness'. */
835 else if (h.code & USER_CODE)
837 user_strikes += 1;
839 #endif
840 else
842 if ((h.code & STD_CODE) && h.distance)
844 if (h.distance > b_or_d_strikes)
845 b_or_d_strikes = h.distance;
847 else
848 easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
849 cp->h.code |= h.code;
850 /* Make sure we communicate this. */
851 cp->h.int_penalty += h.int_penalty;
854 ttf = TREE_CHAIN (ttf);
855 tta = TREE_CHAIN (tta);
856 strike_index += 1;
859 if (tta)
861 /* ran out of formals, and parmlist is fixed size. */
862 if (ttf /* == void_type_node */)
864 cp->h.code = EVIL_CODE;
865 cp->u.bad_arg = -1;
866 return;
868 else
870 struct harshness_code h;
871 int l = list_length (tta);
872 ellipsis_strikes += l;
873 h.code = ELLIPSIS_CODE;
874 h.distance = 0;
875 h.int_penalty = 0;
876 for (; l; --l)
877 cp->harshness[strike_index++] = h;
880 else if (ttf && ttf != void_list_node)
882 /* ran out of actuals, and no defaults. */
883 if (TREE_PURPOSE (ttf) == NULL_TREE)
885 cp->h.code = EVIL_CODE;
886 cp->u.bad_arg = -2;
887 return;
889 /* Store index of first default. */
890 cp->harshness[arglen].distance = strike_index+1;
892 else
893 cp->harshness[arglen].distance = 0;
895 /* Argument list lengths work out, so don't need to check them again. */
896 if (evil_strikes)
898 /* We do not check for derived->base conversions here, since in
899 no case would they give evil strike counts, unless such conversions
900 are somehow ambiguous. */
902 /* See if any user-defined conversions apply.
903 But make sure that we do not loop. */
904 static int dont_convert_types = 0;
906 if (dont_convert_types)
908 cp->h.code = EVIL_CODE;
909 return;
912 win = 0; /* Only get one chance to win. */
913 ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
914 tta = tta_in;
915 strike_index = 0;
916 evil_strikes = 0;
918 while (ttf && tta)
920 if (ttf == void_list_node)
921 break;
923 lose = cp->harshness[strike_index];
924 if ((lose.code & EVIL_CODE)
925 || ((lose.code & STD_CODE) && lose.distance < 0))
927 tree actual_type = TREE_TYPE (TREE_VALUE (tta));
928 tree formal_type = TREE_VALUE (ttf);
929 int extra_conversions = 0;
931 dont_convert_types = 1;
933 if (TREE_CODE (formal_type) == REFERENCE_TYPE)
934 formal_type = TREE_TYPE (formal_type);
935 if (TREE_CODE (actual_type) == REFERENCE_TYPE)
936 actual_type = TREE_TYPE (actual_type);
938 if (formal_type != error_mark_node
939 && actual_type != error_mark_node)
941 formal_type = TYPE_MAIN_VARIANT (formal_type);
942 actual_type = TYPE_MAIN_VARIANT (actual_type);
944 if (TYPE_HAS_CONSTRUCTOR (formal_type))
946 /* If it has a constructor for this type,
947 try to use it. */
948 /* @@ There is no way to save this result yet, so
949 success is a NULL_TREE for now. */
950 if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
951 != error_mark_node)
952 win++;
954 if (TYPE_LANG_SPECIFIC (actual_type)
955 && TYPE_HAS_CONVERSION (actual_type))
957 tree conv;
958 /* Don't issue warnings since we're only groping
959 around for the right answer, we haven't yet
960 committed to going with this solution. */
961 int old_inhibit_warnings = inhibit_warnings;
963 inhibit_warnings = 1;
964 conv = build_type_conversion
965 (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
966 inhibit_warnings = old_inhibit_warnings;
968 if (conv)
970 if (conv == error_mark_node)
971 win += 2;
972 else
974 win++;
975 if (TREE_CODE (conv) != CALL_EXPR)
976 extra_conversions = 1;
979 else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
981 conv = build_type_conversion (CALL_EXPR, formal_type,
982 TREE_VALUE (tta), 0);
983 if (conv)
985 if (conv == error_mark_node)
986 win += 2;
987 else
989 win++;
990 if (TREE_CODE (conv) != CALL_EXPR)
991 extra_conversions = 1;
997 dont_convert_types = 0;
999 if (win == 1)
1001 user_strikes += 1;
1002 cp->harshness[strike_index].code
1003 = USER_CODE | (extra_conversions ? STD_CODE : 0);
1004 win = 0;
1006 else
1008 if (cp->u.bad_arg > strike_index)
1009 cp->u.bad_arg = strike_index;
1011 evil_strikes = win ? 2 : 1;
1012 break;
1016 ttf = TREE_CHAIN (ttf);
1017 tta = TREE_CHAIN (tta);
1018 strike_index += 1;
1022 /* Const member functions get a small penalty because defaulting
1023 to const is less useful than defaulting to non-const. */
1024 /* This is bogus, it does not correspond to anything in the ARM.
1025 This code will be fixed when this entire section is rewritten
1026 to conform to the ARM. (mrs) */
1027 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1029 tree this_parm = TREE_VALUE (ttf_in);
1031 if (TREE_CODE (this_parm) == RECORD_TYPE /* Is `this' a sig ptr? */
1032 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
1033 : TYPE_READONLY (TREE_TYPE (this_parm)))
1035 cp->harshness[0].code |= TRIVIAL_CODE;
1036 ++easy_strikes;
1038 else
1040 /* Calling a non-const member function from a const member function
1041 is probably invalid, but for now we let it only draw a warning.
1042 We indicate that such a mismatch has occurred by setting the
1043 harshness to a maximum value. */
1044 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1045 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1046 cp->harshness[0].code |= CONST_CODE;
1050 if (evil_strikes)
1051 cp->h.code = EVIL_CODE;
1052 if (ellipsis_strikes)
1053 cp->h.code |= ELLIPSIS_CODE;
1054 if (user_strikes)
1055 cp->h.code |= USER_CODE;
1056 #ifdef DEBUG_MATCHING
1057 cp_error ("final eval %s", print_harshness (&cp->h));
1058 #endif
1061 /* Subroutine of ideal_candidate. See if X or Y is a better match
1062 than the other. */
1063 static int
1064 strictly_better (x, y)
1065 unsigned short x, y;
1067 unsigned short xor;
1069 if (x == y)
1070 return 0;
1072 xor = x ^ y;
1073 if (xor >= x || xor >= y)
1074 return 1;
1075 return 0;
1078 /* When one of several possible overloaded functions and/or methods
1079 can be called, choose the best candidate for overloading.
1081 BASETYPE is the context from which we start method resolution
1082 or NULL if we are comparing overloaded functions.
1083 CANDIDATES is the array of candidates we have to choose from.
1084 N_CANDIDATES is the length of CANDIDATES.
1085 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1086 choose. It is modified in place when resolving methods. It is not
1087 modified in place when resolving overloaded functions.
1088 LEN is the length of the parameter list. */
1090 static struct candidate *
1091 ideal_candidate (basetype, candidates, n_candidates, parms, len)
1092 tree basetype;
1093 struct candidate *candidates;
1094 int n_candidates;
1095 tree parms;
1096 int len;
1098 struct candidate *cp = candidates+n_candidates;
1099 int i, j = -1, best_code;
1101 /* For each argument, sort the functions from best to worst for the arg.
1102 For each function that's not best for this arg, set its overall
1103 harshness to EVIL so that other args won't like it. The candidate
1104 list for the last argument is the intersection of all the best-liked
1105 functions. */
1107 #if 0
1108 for (i = 0; i < len; i++)
1110 qsort (candidates, n_candidates, sizeof (struct candidate),
1111 rank_for_overload);
1112 best_code = cp[-1].h.code;
1114 /* To find out functions that are worse than that represented
1115 by BEST_CODE, we can't just do a comparison like h.code>best_code.
1116 The total harshness for the "best" fn may be 8|8 for two args, and
1117 the harshness for the next-best may be 8|2. If we just compared,
1118 that would be checking 8>10, which would lead to the next-best
1119 being disqualified. What we actually want to do is get rid
1120 of functions that are definitely worse than that represented
1121 by best_code, i.e. those which have bits set higher than the
1122 highest in best_code. Sooooo, what we do is clear out everything
1123 represented by best_code, and see if we still come up with something
1124 higher. If so (e.g., 8|8 vs 8|16), it'll disqualify it properly. */
1125 for (j = n_candidates-2; j >= 0; j--)
1126 if ((candidates[j].h.code & ~best_code) > best_code)
1127 candidates[j].h.code = EVIL_CODE;
1130 if (cp[-1].h.code & EVIL_CODE)
1131 return NULL;
1132 #else
1133 qsort (candidates, n_candidates, sizeof (struct candidate),
1134 rank_for_overload);
1135 best_code = cp[-1].h.code;
1136 #endif
1138 /* If they're at least as good as each other, do an arg-by-arg check. */
1139 if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
1141 int better = 0;
1142 int worse = 0;
1144 for (j = 0; j < n_candidates; j++)
1145 if (! strictly_better (candidates[j].h.code, best_code))
1146 break;
1148 qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
1149 rank_for_ideal);
1150 for (i = 0; i < len; i++)
1152 if (cp[-1].harshness[i].code < cp[-2].harshness[i].code)
1153 better = 1;
1154 else if (cp[-1].harshness[i].code > cp[-2].harshness[i].code)
1155 worse = 1;
1156 else if (cp[-1].harshness[i].code & STD_CODE)
1158 /* If it involves a standard conversion, let the
1159 inheritance lattice be the final arbiter. */
1160 if (cp[-1].harshness[i].distance > cp[-2].harshness[i].distance)
1161 worse = 1;
1162 else if (cp[-1].harshness[i].distance < cp[-2].harshness[i].distance)
1163 better = 1;
1165 else if (cp[-1].harshness[i].code & PROMO_CODE)
1167 /* For integral promotions, take into account a finer
1168 granularity for determining which types should be favored
1169 over others in such promotions. */
1170 if (cp[-1].harshness[i].int_penalty > cp[-2].harshness[i].int_penalty)
1171 worse = 1;
1172 else if (cp[-1].harshness[i].int_penalty < cp[-2].harshness[i].int_penalty)
1173 better = 1;
1177 if (! better || worse)
1178 return NULL;
1180 return cp-1;
1183 /* Assume that if the class referred to is not in the
1184 current class hierarchy, that it may be remote.
1185 PARENT is assumed to be of aggregate type here. */
1186 static int
1187 may_be_remote (parent)
1188 tree parent;
1190 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
1191 return 0;
1193 if (current_class_type == NULL_TREE)
1194 return 0;
1196 if (parent == current_class_type)
1197 return 0;
1199 if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
1200 return 0;
1201 return 1;
1204 tree
1205 build_vfield_ref (datum, type)
1206 tree datum, type;
1208 tree rval;
1209 int old_assume_nonnull_objects = flag_assume_nonnull_objects;
1211 if (datum == error_mark_node)
1212 return error_mark_node;
1214 /* Vtable references are always made from non-null objects. */
1215 flag_assume_nonnull_objects = 1;
1216 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
1217 datum = convert_from_reference (datum);
1219 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
1220 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
1221 datum, CLASSTYPE_VFIELD (type));
1222 else
1223 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
1224 flag_assume_nonnull_objects = old_assume_nonnull_objects;
1226 return rval;
1229 /* Build a call to a member of an object. I.e., one that overloads
1230 operator ()(), or is a pointer-to-function or pointer-to-method. */
1231 static tree
1232 build_field_call (basetype_path, instance_ptr, name, parms)
1233 tree basetype_path, instance_ptr, name, parms;
1235 tree field, instance;
1237 if (instance_ptr == current_class_decl)
1239 /* Check to see if we really have a reference to an instance variable
1240 with `operator()()' overloaded. */
1241 field = IDENTIFIER_CLASS_VALUE (name);
1243 if (field == NULL_TREE)
1245 cp_error ("`this' has no member named `%D'", name);
1246 return error_mark_node;
1249 if (TREE_CODE (field) == FIELD_DECL)
1251 /* If it's a field, try overloading operator (),
1252 or calling if the field is a pointer-to-function. */
1253 instance = build_component_ref_1 (C_C_D, field, 0);
1254 if (instance == error_mark_node)
1255 return error_mark_node;
1257 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1258 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
1259 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
1261 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1263 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
1264 return build_function_call (instance, parms);
1265 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
1266 return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
1269 return NULL_TREE;
1272 /* Check to see if this is not really a reference to an instance variable
1273 with `operator()()' overloaded. */
1274 field = lookup_field (basetype_path, name, 1, 0);
1276 /* This can happen if the reference was ambiguous or for access
1277 violations. */
1278 if (field == error_mark_node)
1279 return error_mark_node;
1281 if (field)
1283 tree basetype;
1284 tree ftype = TREE_TYPE (field);
1286 if (TREE_CODE (ftype) == REFERENCE_TYPE)
1287 ftype = TREE_TYPE (ftype);
1289 if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
1291 /* Make the next search for this field very short. */
1292 basetype = DECL_FIELD_CONTEXT (field);
1293 instance_ptr = convert_pointer_to (basetype, instance_ptr);
1295 instance = build_indirect_ref (instance_ptr, NULL_PTR);
1296 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
1297 build_component_ref_1 (instance, field, 0),
1298 parms, NULL_TREE);
1300 if (TREE_CODE (ftype) == POINTER_TYPE)
1302 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
1303 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
1305 /* This is a member which is a pointer to function. */
1306 tree ref
1307 = build_component_ref_1 (build_indirect_ref (instance_ptr,
1308 NULL_PTR),
1309 field, LOOKUP_COMPLAIN);
1310 if (ref == error_mark_node)
1311 return error_mark_node;
1312 return build_function_call (ref, parms);
1315 else if (TREE_CODE (ftype) == METHOD_TYPE)
1317 error ("invalid call via pointer-to-member function");
1318 return error_mark_node;
1320 else
1321 return NULL_TREE;
1323 return NULL_TREE;
1326 tree
1327 find_scoped_type (type, inner_name, inner_types)
1328 tree type, inner_name, inner_types;
1330 tree tags = CLASSTYPE_TAGS (type);
1332 while (tags)
1334 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1335 enclosing class) is set to the name for the enum type. So, if
1336 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1337 then this test will be true. */
1338 if (TREE_PURPOSE (tags) == inner_name)
1340 if (inner_types == NULL_TREE)
1341 return DECL_NESTED_TYPENAME (TYPE_NAME (TREE_VALUE (tags)));
1342 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
1344 tags = TREE_CHAIN (tags);
1347 #if 0
1348 /* XXX This needs to be fixed better. */
1349 if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
1351 sorry ("nested class lookup in template type");
1352 return NULL_TREE;
1354 #endif
1356 /* Look for a TYPE_DECL. */
1357 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
1358 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
1360 /* Code by raeburn. */
1361 if (inner_types == NULL_TREE)
1362 return DECL_NESTED_TYPENAME (tags);
1363 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
1366 return NULL_TREE;
1369 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1370 the name that names the above nested type. INNER_TYPES
1371 is a chain of nested type names (held together by SCOPE_REFs);
1372 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1373 Returns NULL_TREE if there is an error. */
1374 tree
1375 resolve_scope_to_name (outer_type, inner_stuff)
1376 tree outer_type, inner_stuff;
1378 register tree tmp;
1379 tree inner_name, inner_type;
1381 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
1383 /* We first try to look for a nesting in our current class context,
1384 then try any enclosing classes. */
1385 tree type = current_class_type;
1387 while (type && (TREE_CODE (type) == RECORD_TYPE
1388 || TREE_CODE (type) == UNION_TYPE))
1390 tree rval = resolve_scope_to_name (type, inner_stuff);
1392 if (rval != NULL_TREE)
1393 return rval;
1394 type = DECL_CONTEXT (TYPE_NAME (type));
1398 if (TREE_CODE (inner_stuff) == SCOPE_REF)
1400 inner_name = TREE_OPERAND (inner_stuff, 0);
1401 inner_type = TREE_OPERAND (inner_stuff, 1);
1403 else
1405 inner_name = inner_stuff;
1406 inner_type = NULL_TREE;
1409 if (outer_type == NULL_TREE)
1411 /* If we have something that's already a type by itself,
1412 use that. */
1413 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
1415 if (inner_type)
1416 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
1417 inner_type);
1418 return inner_name;
1420 return NULL_TREE;
1423 if (! IS_AGGR_TYPE (outer_type))
1424 return NULL_TREE;
1426 /* Look for member classes or enums. */
1427 tmp = find_scoped_type (outer_type, inner_name, inner_type);
1429 /* If it's not a type in this class, then go down into the
1430 base classes and search there. */
1431 if (! tmp && TYPE_BINFO (outer_type))
1433 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
1434 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1436 for (i = 0; i < n_baselinks; i++)
1438 tree base_binfo = TREE_VEC_ELT (binfos, i);
1439 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
1440 if (tmp)
1441 return tmp;
1443 tmp = NULL_TREE;
1446 return tmp;
1449 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1450 This is how virtual function calls are avoided. */
1451 tree
1452 build_scoped_method_call (exp, scopes, name, parms)
1453 tree exp, scopes, name, parms;
1455 /* Because this syntactic form does not allow
1456 a pointer to a base class to be `stolen',
1457 we need not protect the derived->base conversion
1458 that happens here.
1460 @@ But we do have to check access privileges later. */
1461 tree basename = resolve_scope_to_name (NULL_TREE, scopes);
1462 tree basetype, binfo, decl;
1463 tree type = TREE_TYPE (exp);
1465 if (type == error_mark_node
1466 || basename == NULL_TREE)
1467 return error_mark_node;
1469 basetype = IDENTIFIER_TYPE_VALUE (basename);
1471 if (TREE_CODE (type) == REFERENCE_TYPE)
1472 type = TREE_TYPE (type);
1474 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1475 that explicit ~int is caught in the parser; this deals with typedefs
1476 and template parms. */
1477 if (TREE_CODE (name) == BIT_NOT_EXPR && ! is_aggr_typedef (basename, 0))
1479 if (type != basetype)
1480 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1481 exp, basetype, type);
1482 name = IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0));
1483 if (basetype != name)
1484 cp_error ("qualified type `%T' does not match destructor type `%T'",
1485 basetype, name);
1486 return void_zero_node;
1489 if (! is_aggr_typedef (basename, 1))
1490 return error_mark_node;
1492 if (! IS_AGGR_TYPE (type))
1494 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1495 exp, type);
1496 return error_mark_node;
1499 if ((binfo = binfo_or_else (basetype, type)))
1501 if (binfo == error_mark_node)
1502 return error_mark_node;
1503 if (TREE_CODE (exp) == INDIRECT_REF)
1504 decl = build_indirect_ref (convert_pointer_to (binfo,
1505 build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
1506 else
1507 decl = build_scoped_ref (exp, scopes);
1509 /* Call to a destructor. */
1510 if (TREE_CODE (name) == BIT_NOT_EXPR)
1512 /* Explicit call to destructor. */
1513 name = TREE_OPERAND (name, 0);
1514 if (name != constructor_name (TREE_TYPE (decl)))
1516 cp_error
1517 ("qualified type `%T' does not match destructor type `%T'",
1518 TREE_TYPE (decl), name);
1519 return error_mark_node;
1521 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
1522 return void_zero_node;
1524 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
1525 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
1529 /* Call to a method. */
1530 return build_method_call (decl, name, parms, binfo,
1531 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1533 return error_mark_node;
1536 static void
1537 print_candidates (candidates)
1538 tree candidates;
1540 cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
1541 candidates = TREE_CHAIN (candidates);
1543 while (candidates)
1545 cp_error_at (" %D", TREE_VALUE (candidates));
1546 candidates = TREE_CHAIN (candidates);
1550 static void
1551 print_n_candidates (candidates, n)
1552 struct candidate *candidates;
1553 int n;
1555 int i;
1557 cp_error_at ("candidates are: %D", candidates[0].function);
1558 for (i = 1; i < n; i++)
1559 cp_error_at (" %D", candidates[i].function);
1562 /* Build something of the form ptr->method (args)
1563 or object.method (args). This can also build
1564 calls to constructors, and find friends.
1566 Member functions always take their class variable
1567 as a pointer.
1569 INSTANCE is a class instance.
1571 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1573 PARMS help to figure out what that NAME really refers to.
1575 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1576 down to the real instance type to use for access checking. We need this
1577 information to get protected accesses correct. This parameter is used
1578 by build_member_call.
1580 FLAGS is the logical disjunction of zero or more LOOKUP_
1581 flags. See cp-tree.h for more info.
1583 If this is all OK, calls build_function_call with the resolved
1584 member function.
1586 This function must also handle being called to perform
1587 initialization, promotion/coercion of arguments, and
1588 instantiation of default parameters.
1590 Note that NAME may refer to an instance variable name. If
1591 `operator()()' is defined for the type of that field, then we return
1592 that result. */
1593 tree
1594 build_method_call (instance, name, parms, basetype_path, flags)
1595 tree instance, name, parms, basetype_path;
1596 int flags;
1598 register tree function, fntype, value_type;
1599 register tree basetype, save_basetype;
1600 register tree baselink, result, method_name, parmtypes, parm;
1601 tree last;
1602 int pass;
1603 enum access_type access = access_public;
1605 /* Range of cases for vtable optimization. */
1606 enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
1607 enum vtable_needs need_vtbl = not_needed;
1609 char *name_kind;
1610 int ever_seen = 0;
1611 tree instance_ptr = NULL_TREE;
1612 int all_virtual = flag_all_virtual;
1613 int static_call_context = 0;
1614 tree found_fns = NULL_TREE;
1616 /* Keep track of `const' and `volatile' objects. */
1617 int constp, volatilep;
1619 #ifdef GATHER_STATISTICS
1620 n_build_method_call++;
1621 #endif
1623 if (instance == error_mark_node
1624 || name == error_mark_node
1625 || parms == error_mark_node
1626 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
1627 return error_mark_node;
1629 /* This is the logic that magically deletes the second argument to
1630 operator delete, if it is not needed. */
1631 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
1633 tree save_last = TREE_CHAIN (parms);
1634 tree result;
1635 /* get rid of unneeded argument */
1636 TREE_CHAIN (parms) = NULL_TREE;
1637 result = build_method_call (instance, name, parms, basetype_path,
1638 (LOOKUP_SPECULATIVELY|flags)
1639 &~LOOKUP_COMPLAIN);
1640 /* If it works, return it. */
1641 if (result && result != error_mark_node)
1642 return build_method_call (instance, name, parms, basetype_path, flags);
1643 /* If it doesn't work, two argument delete must work */
1644 TREE_CHAIN (parms) = save_last;
1646 /* We already know whether it's needed or not for vec delete. */
1647 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
1648 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
1649 TREE_CHAIN (parms) = NULL_TREE;
1651 if (TREE_CODE (name) == BIT_NOT_EXPR)
1653 flags |= LOOKUP_DESTRUCTOR;
1654 name = TREE_OPERAND (name, 0);
1655 if (parms)
1656 error ("destructors take no parameters");
1657 basetype = get_type_value (name);
1658 if (basetype == NULL_TREE)
1660 cp_error ("call to destructor for non-type `%D'", name);
1661 return void_zero_node;
1663 if (basetype != TREE_TYPE(instance))
1664 basetype = TREE_TYPE(instance);
1665 if (! TYPE_HAS_DESTRUCTOR (basetype))
1666 return void_zero_node;
1667 instance = default_conversion (instance);
1668 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1669 return build_delete (build_pointer_type (basetype),
1670 instance_ptr, integer_two_node,
1671 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
1675 char *xref_name;
1677 /* Initialize name for error reporting. */
1678 if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
1680 char *p = operator_name_string (name);
1681 xref_name = (char *)alloca (strlen (p) + 10);
1682 sprintf (xref_name, "operator %s", p);
1684 else if (TREE_CODE (name) == SCOPE_REF)
1685 xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
1686 else
1687 xref_name = IDENTIFIER_POINTER (name);
1689 GNU_xref_call (current_function_decl, xref_name);
1692 if (instance == NULL_TREE)
1694 basetype = NULL_TREE;
1695 /* Check cases where this is really a call to raise
1696 an exception. */
1697 if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
1699 basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
1700 if (basetype)
1701 basetype = TREE_VALUE (basetype);
1703 else if (TREE_CODE (name) == SCOPE_REF
1704 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
1706 if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
1707 return error_mark_node;
1708 basetype = purpose_member (TREE_OPERAND (name, 1),
1709 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
1710 if (basetype)
1711 basetype = TREE_VALUE (basetype);
1714 if (basetype != NULL_TREE)
1716 /* call to a constructor... */
1717 else if (basetype_path)
1718 basetype = BINFO_TYPE (basetype_path);
1719 else if (IDENTIFIER_HAS_TYPE_VALUE (name))
1721 basetype = IDENTIFIER_TYPE_VALUE (name);
1722 name = constructor_name_full (basetype);
1724 else
1726 tree typedef_name = lookup_name (name, 1);
1727 if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
1729 /* Canonicalize the typedef name. */
1730 basetype = TREE_TYPE (typedef_name);
1731 name = TYPE_IDENTIFIER (basetype);
1733 else
1735 cp_error ("no constructor named `%T' in scope",
1736 name);
1737 return error_mark_node;
1741 if (! IS_AGGR_TYPE (basetype))
1743 non_aggr_error:
1744 if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
1745 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1746 name, instance, basetype);
1748 return error_mark_node;
1751 else if (instance == C_C_D || instance == current_class_decl)
1753 /* When doing initialization, we side-effect the TREE_TYPE of
1754 C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1755 basetype = TREE_TYPE (C_C_D);
1757 /* Anything manifestly `this' in constructors and destructors
1758 has a known type, so virtual function tables are not needed. */
1759 if (TYPE_VIRTUAL_P (basetype)
1760 && !(flags & LOOKUP_NONVIRTUAL))
1761 need_vtbl = (dtor_label || ctor_label)
1762 ? unneeded : maybe_needed;
1764 instance = C_C_D;
1765 instance_ptr = current_class_decl;
1766 result = build_field_call (TYPE_BINFO (current_class_type),
1767 instance_ptr, name, parms);
1769 if (result)
1770 return result;
1772 else if (TREE_CODE (instance) == RESULT_DECL)
1774 basetype = TREE_TYPE (instance);
1775 /* Should we ever have to make a virtual function reference
1776 from a RESULT_DECL, know that it must be of fixed type
1777 within the scope of this function. */
1778 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1779 need_vtbl = maybe_needed;
1780 instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
1782 else
1784 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1785 tree inst_ptr_basetype;
1787 static_call_context =
1788 (TREE_CODE (instance) == INDIRECT_REF
1789 && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
1790 && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
1792 /* the base type of an instance variable is pointer to class */
1793 basetype = TREE_TYPE (instance);
1795 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1797 basetype = TREE_TYPE (basetype);
1798 if (! IS_AGGR_TYPE (basetype))
1799 goto non_aggr_error;
1800 /* Call to convert not needed because we are remaining
1801 within the same type. */
1802 instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
1803 instance);
1804 inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
1806 else
1808 if (! IS_AGGR_TYPE (basetype))
1809 goto non_aggr_error;
1811 if (IS_SIGNATURE_POINTER (basetype)
1812 || IS_SIGNATURE_REFERENCE (basetype))
1813 basetype = SIGNATURE_TYPE (basetype);
1815 if ((IS_SIGNATURE (basetype)
1816 && (instance_ptr = build_optr_ref (instance)))
1817 || (lvalue_p (instance)
1818 && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
1819 || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
1821 if (instance_ptr == error_mark_node)
1822 return error_mark_node;
1824 else if (TREE_CODE (instance) == NOP_EXPR
1825 || TREE_CODE (instance) == CONSTRUCTOR)
1827 /* A cast is not an lvalue. Initialize a fresh temp
1828 with the value we are casting from, and proceed with
1829 that temporary. We can't cast to a reference type,
1830 so that simplifies the initialization to something
1831 we can manage. */
1832 tree temp = get_temp_name (TREE_TYPE (instance), 0);
1833 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
1834 expand_aggr_init (temp, instance, 0);
1835 else
1837 store_init_value (temp, instance);
1838 expand_decl_init (temp);
1840 instance = temp;
1841 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1843 else
1845 if (TREE_CODE (instance) != CALL_EXPR)
1846 my_friendly_abort (125);
1847 if (TYPE_NEEDS_CONSTRUCTING (basetype))
1848 instance = build_cplus_new (basetype, instance, 0);
1849 else
1851 instance = get_temp_name (basetype, 0);
1852 TREE_ADDRESSABLE (instance) = 1;
1854 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1856 /* @@ Should we call comp_target_types here? */
1857 inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
1858 if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
1859 basetype = inst_ptr_basetype;
1860 else
1862 instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
1863 if (instance_ptr == error_mark_node)
1864 return error_mark_node;
1868 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1869 not updated, so we use `basetype' instead. */
1870 if (basetype_path == NULL_TREE
1871 && IS_SIGNATURE (basetype))
1872 basetype_path = TYPE_BINFO (basetype);
1873 else if (basetype_path == NULL_TREE ||
1874 BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
1875 basetype_path = TYPE_BINFO (inst_ptr_basetype);
1877 result = build_field_call (basetype_path, instance_ptr, name, parms);
1878 if (result)
1879 return result;
1881 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1883 if (TREE_SIDE_EFFECTS (instance_ptr))
1885 /* This action is needed because the instance is needed
1886 for providing the base of the virtual function table.
1887 Without using a SAVE_EXPR, the function we are building
1888 may be called twice, or side effects on the instance
1889 variable (such as a post-increment), may happen twice. */
1890 instance_ptr = save_expr (instance_ptr);
1891 instance = build_indirect_ref (instance_ptr, NULL_PTR);
1893 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1895 /* This happens when called for operator new (). */
1896 instance = build_indirect_ref (instance, NULL_PTR);
1899 need_vtbl = maybe_needed;
1903 if (TYPE_SIZE (basetype) == 0)
1905 /* This is worth complaining about, I think. */
1906 cp_error ("cannot lookup method in incomplete type `%T'", basetype);
1907 return error_mark_node;
1910 save_basetype = TYPE_MAIN_VARIANT (basetype);
1912 #if 0
1913 if (all_virtual == 1
1914 && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
1915 OPERATOR_METHOD_LENGTH)
1916 || instance_ptr == NULL_TREE
1917 || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0)))
1918 all_virtual = 0;
1919 #endif
1921 last = NULL_TREE;
1922 for (parmtypes = NULL_TREE, parm = parms; parm; parm = TREE_CHAIN (parm))
1924 tree t = TREE_TYPE (TREE_VALUE (parm));
1925 if (TREE_CODE (t) == OFFSET_TYPE)
1927 /* Convert OFFSET_TYPE entities to their normal selves. */
1928 TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
1929 t = TREE_TYPE (TREE_VALUE (parm));
1931 if (TREE_CODE (TREE_VALUE (parm)) == OFFSET_REF
1932 && TREE_CODE (t) == METHOD_TYPE)
1934 TREE_VALUE (parm) = build_unary_op (ADDR_EXPR, TREE_VALUE (parm), 0);
1936 if (TREE_CODE (t) == ARRAY_TYPE)
1938 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
1939 This eliminates needless calls to `compute_conversion_costs'. */
1940 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
1941 t = TREE_TYPE (TREE_VALUE (parm));
1943 if (t == error_mark_node)
1944 return error_mark_node;
1945 last = build_tree_list (NULL_TREE, t);
1946 parmtypes = chainon (parmtypes, last);
1949 if (instance)
1951 /* TREE_READONLY (instance) fails for references. */
1952 constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
1953 volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
1954 parms = tree_cons (NULL_TREE, instance_ptr, parms);
1956 else
1958 /* Raw constructors are always in charge. */
1959 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
1960 && ! (flags & LOOKUP_HAS_IN_CHARGE))
1962 flags |= LOOKUP_HAS_IN_CHARGE;
1963 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1964 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
1967 if (flag_this_is_variable > 0)
1969 constp = 0;
1970 volatilep = 0;
1971 parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
1973 else
1975 constp = 0;
1976 volatilep = 0;
1977 instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
1978 if (instance_ptr == error_mark_node)
1979 return error_mark_node;
1980 instance_ptr = save_expr (instance_ptr);
1981 TREE_CALLS_NEW (instance_ptr) = 1;
1982 instance = build_indirect_ref (instance_ptr, NULL_PTR);
1984 /* If it's a default argument initialized from a ctor, what we get
1985 from instance_ptr will match the arglist for the FUNCTION_DECL
1986 of the constructor. */
1987 if (parms && TREE_CODE (TREE_VALUE (parms)) == CALL_EXPR
1988 && TREE_OPERAND (TREE_VALUE (parms), 1)
1989 && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms), 1))))
1990 parms = build_tree_list (NULL_TREE, instance_ptr);
1991 else
1992 parms = tree_cons (NULL_TREE, instance_ptr, parms);
1996 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
1998 if (last == NULL_TREE)
1999 last = parmtypes;
2001 /* Look up function name in the structure type definition. */
2003 if ((IDENTIFIER_HAS_TYPE_VALUE (name)
2004 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name))
2005 && TREE_CODE(IDENTIFIER_TYPE_VALUE (name)) != UNINSTANTIATED_P_TYPE)
2006 || name == constructor_name (basetype))
2008 tree tmp = NULL_TREE;
2009 if (IDENTIFIER_TYPE_VALUE (name) == basetype
2010 || name == constructor_name (basetype))
2011 tmp = TYPE_BINFO (basetype);
2012 else
2013 tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
2015 if (tmp != NULL_TREE)
2017 name_kind = "constructor";
2019 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2020 && ! (flags & LOOKUP_HAS_IN_CHARGE))
2022 /* Constructors called for initialization
2023 only are never in charge. */
2024 tree tmplist;
2026 flags |= LOOKUP_HAS_IN_CHARGE;
2027 tmplist = tree_cons (NULL_TREE, integer_zero_node,
2028 TREE_CHAIN (parms));
2029 TREE_CHAIN (parms) = tmplist;
2030 tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
2031 TREE_CHAIN (parmtypes) = tmplist;
2033 basetype = BINFO_TYPE (tmp);
2035 else
2036 name_kind = "method";
2038 else
2039 name_kind = "method";
2041 if (basetype_path == NULL_TREE
2042 || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
2043 basetype_path = TYPE_BINFO (basetype);
2044 result = lookup_fnfields (basetype_path, name,
2045 (flags & LOOKUP_COMPLAIN));
2046 if (result == error_mark_node)
2047 return error_mark_node;
2050 /* Now, go look for this method name. We do not find destructors here.
2052 Putting `void_list_node' on the end of the parmtypes
2053 fakes out `build_decl_overload' into doing the right thing. */
2054 TREE_CHAIN (last) = void_list_node;
2055 method_name = build_decl_overload (name, parmtypes,
2056 1 + (name == constructor_name (save_basetype)
2057 || name == constructor_name_full (save_basetype)));
2058 TREE_CHAIN (last) = NULL_TREE;
2060 for (pass = 0; pass < 2; pass++)
2062 struct candidate *candidates;
2063 struct candidate *cp;
2064 int len;
2065 unsigned best = 1;
2067 /* This increments every time we go up the type hierarchy.
2068 The idea is to prefer a function of the derived class if possible. */
2069 int b_or_d = 0;
2071 baselink = result;
2073 if (pass > 0)
2075 candidates
2076 = (struct candidate *) alloca ((ever_seen+1)
2077 * sizeof (struct candidate));
2078 bzero (candidates, (ever_seen + 1) * sizeof (struct candidate));
2079 cp = candidates;
2080 len = list_length (parms);
2081 ever_seen = 0;
2083 /* First see if a global function has a shot at it. */
2084 if (flags & LOOKUP_GLOBAL)
2086 tree friend_parms;
2087 tree parm = instance_ptr;
2089 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
2091 /* TREE_VALUE (parms) may have been modified by now;
2092 restore it to its original value. */
2093 TREE_VALUE (parms) = parm;
2094 friend_parms = parms;
2096 else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
2098 tree new_type;
2099 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
2100 new_type = build_reference_type (TREE_TYPE (parm));
2101 /* It is possible that this should go down a layer. */
2102 new_type = build_type_variant (new_type, constp, volatilep);
2103 parm = convert (new_type, parm);
2104 friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
2106 else
2107 my_friendly_abort (167);
2109 cp->h_len = len;
2110 cp->harshness = (struct harshness_code *)
2111 alloca ((len + 1) * sizeof (struct harshness_code));
2113 result = build_overload_call (name, friend_parms, 0, cp);
2114 /* If it turns out to be the one we were actually looking for
2115 (it was probably a friend function), the return the
2116 good result. */
2117 if (TREE_CODE (result) == CALL_EXPR)
2118 return result;
2120 while ((cp->h.code & EVIL_CODE) == 0)
2122 /* non-standard uses: set the field to 0 to indicate
2123 we are using a non-member function. */
2124 cp->u.field = 0;
2125 if (cp->harshness[len].distance == 0
2126 && cp->h.code < best)
2127 best = cp->h.code;
2128 cp += 1;
2133 while (baselink)
2135 /* We have a hit (of sorts). If the parameter list is
2136 "error_mark_node", or some variant thereof, it won't
2137 match any methods. Since we have verified that the is
2138 some method vaguely matching this one (in name at least),
2139 silently return.
2141 Don't stop for friends, however. */
2142 basetype_path = TREE_PURPOSE (baselink);
2144 function = TREE_VALUE (baselink);
2145 if (TREE_CODE (basetype_path) == TREE_LIST)
2146 basetype_path = TREE_VALUE (basetype_path);
2147 basetype = BINFO_TYPE (basetype_path);
2149 /* Cast the instance variable if necessary. */
2150 if (basetype != TYPE_MAIN_VARIANT
2151 (TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)))))
2153 if (basetype == save_basetype)
2154 TREE_VALUE (parms) = instance_ptr;
2155 else
2157 tree type = build_pointer_type
2158 (build_type_variant (basetype, constp, volatilep));
2159 TREE_VALUE (parms) = convert_force (type, instance_ptr);
2163 /* FIXME: this is the wrong place to get an error. Hopefully
2164 the access-control rewrite will make this change more cleanly. */
2165 if (TREE_VALUE (parms) == error_mark_node)
2166 return error_mark_node;
2168 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
2169 function = DECL_CHAIN (function);
2171 for (; function; function = DECL_CHAIN (function))
2173 #ifdef GATHER_STATISTICS
2174 n_inner_fields_searched++;
2175 #endif
2176 ever_seen++;
2177 if (pass > 0)
2178 found_fns = tree_cons (NULL_TREE, function, found_fns);
2180 /* Not looking for friends here. */
2181 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
2182 && ! DECL_STATIC_FUNCTION_P (function))
2183 continue;
2185 if (pass == 0
2186 && DECL_ASSEMBLER_NAME (function) == method_name)
2187 goto found;
2189 if (pass > 0)
2191 tree these_parms = parms;
2193 #ifdef GATHER_STATISTICS
2194 n_inner_fields_searched++;
2195 #endif
2196 cp->h_len = len;
2197 cp->harshness = (struct harshness_code *)
2198 alloca ((len + 1) * sizeof (struct harshness_code));
2200 if (DECL_STATIC_FUNCTION_P (function))
2201 these_parms = TREE_CHAIN (these_parms);
2202 compute_conversion_costs (function, these_parms, cp, len);
2204 if ((cp->h.code & EVIL_CODE) == 0)
2206 cp->u.field = function;
2207 cp->function = function;
2208 cp->basetypes = basetype_path;
2210 /* No "two-level" conversions. */
2211 if (flags & LOOKUP_NO_CONVERSION
2212 && (cp->h.code & USER_CODE))
2213 continue;
2215 /* If we used default parameters, we must
2216 check to see whether anyone else might
2217 use them also, and report a possible
2218 ambiguity. */
2219 if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
2220 && cp->harshness[len].distance == 0
2221 && cp->h.code < best)
2223 if (! DECL_STATIC_FUNCTION_P (function))
2224 TREE_VALUE (parms) = cp->arg;
2225 if (best == 1)
2226 goto found_and_maybe_warn;
2228 cp++;
2232 /* Now we have run through one link's member functions.
2233 arrange to head-insert this link's links. */
2234 baselink = next_baselink (baselink);
2235 b_or_d += 1;
2236 /* Don't grab functions from base classes. lookup_fnfield will
2237 do the work to get us down into the right place. */
2238 baselink = NULL_TREE;
2240 if (pass == 0)
2242 tree igv = lookup_name_nonclass (name);
2244 /* No exact match could be found. Now try to find match
2245 using default conversions. */
2246 if ((flags & LOOKUP_GLOBAL) && igv)
2248 if (TREE_CODE (igv) == FUNCTION_DECL)
2249 ever_seen += 1;
2250 else if (TREE_CODE (igv) == TREE_LIST)
2251 ever_seen += count_functions (igv);
2254 if (ever_seen == 0)
2256 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2257 == LOOKUP_SPECULATIVELY)
2258 return NULL_TREE;
2260 TREE_CHAIN (last) = void_list_node;
2261 if (flags & LOOKUP_GLOBAL)
2262 cp_error ("no global or member function `%D(%A)' defined",
2263 name, parmtypes);
2264 else
2265 cp_error ("no member function `%T::%D(%A)' defined",
2266 save_basetype, name, TREE_CHAIN (parmtypes));
2267 return error_mark_node;
2269 continue;
2272 if (cp - candidates != 0)
2274 /* Rank from worst to best. Then cp will point to best one.
2275 Private fields have their bits flipped. For unsigned
2276 numbers, this should make them look very large.
2277 If the best alternate has a (signed) negative value,
2278 then all we ever saw were private members. */
2279 if (cp - candidates > 1)
2281 int n_candidates = cp - candidates;
2282 TREE_VALUE (parms) = instance_ptr;
2283 cp = ideal_candidate (save_basetype, candidates,
2284 n_candidates, parms, len);
2285 if (cp == (struct candidate *)0)
2287 if (flags & LOOKUP_COMPLAIN)
2289 cp_error ("call of overloaded %s `%D' is ambiguous",
2290 name_kind, name);
2291 print_n_candidates (candidates, n_candidates);
2293 return error_mark_node;
2295 if (cp->h.code & EVIL_CODE)
2296 return error_mark_node;
2298 else if (cp[-1].h.code & EVIL_CODE)
2300 if (flags & LOOKUP_COMPLAIN)
2301 cp_error ("ambiguous type conversion requested for %s `%D'",
2302 name_kind, name);
2303 return error_mark_node;
2305 else
2306 cp--;
2308 /* The global function was the best, so use it. */
2309 if (cp->u.field == 0)
2311 /* We must convert the instance pointer into a reference type.
2312 Global overloaded functions can only either take
2313 aggregate objects (which come for free from references)
2314 or reference data types anyway. */
2315 TREE_VALUE (parms) = copy_node (instance_ptr);
2316 TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
2317 return build_function_call (cp->function, parms);
2320 function = cp->function;
2321 basetype_path = cp->basetypes;
2322 if (! DECL_STATIC_FUNCTION_P (function))
2323 TREE_VALUE (parms) = cp->arg;
2324 goto found_and_maybe_warn;
2327 if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
2329 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2330 == LOOKUP_SPECULATIVELY)
2331 return NULL_TREE;
2333 if (DECL_STATIC_FUNCTION_P (cp->function))
2334 parms = TREE_CHAIN (parms);
2335 if (ever_seen)
2337 if (flags & LOOKUP_SPECULATIVELY)
2338 return NULL_TREE;
2339 if (static_call_context
2340 && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
2341 cp_error ("object missing in call to `%D'", cp->function);
2342 else if (ever_seen > 1)
2344 TREE_CHAIN (last) = void_list_node;
2345 cp_error ("no matching function for call to `%T::%D (%A)'",
2346 TREE_TYPE (TREE_TYPE (instance_ptr)),
2347 name, TREE_CHAIN (parmtypes));
2348 TREE_CHAIN (last) = NULL_TREE;
2349 print_candidates (found_fns);
2351 else
2352 report_type_mismatch (cp, parms, name_kind);
2353 return error_mark_node;
2356 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2357 == LOOKUP_COMPLAIN)
2359 cp_error ("%T has no method named %D", save_basetype, name);
2360 return error_mark_node;
2362 return NULL_TREE;
2364 continue;
2366 found_and_maybe_warn:
2367 if ((cp->harshness[0].code & CONST_CODE)
2368 /* 12.1p2: Constructors can be called for const objects. */
2369 && ! DECL_CONSTRUCTOR_P (cp->function))
2371 if (flags & LOOKUP_COMPLAIN)
2373 cp_error_at ("non-const member function `%D'", cp->function);
2374 error ("called for const object at this point in file");
2376 /* Not good enough for a match. */
2377 else
2378 return error_mark_node;
2380 goto found;
2382 /* Silently return error_mark_node. */
2383 return error_mark_node;
2385 found:
2386 if (flags & LOOKUP_PROTECT)
2387 access = compute_access (basetype_path, function);
2389 if (access == access_private)
2391 if (flags & LOOKUP_COMPLAIN)
2393 cp_error_at ("%s `%+#D' is %s", name_kind, function,
2394 TREE_PRIVATE (function) ? "private"
2395 : "from private base class");
2396 error ("within this context");
2398 return error_mark_node;
2400 else if (access == access_protected)
2402 if (flags & LOOKUP_COMPLAIN)
2404 cp_error_at ("%s `%+#D' %s", name_kind, function,
2405 TREE_PROTECTED (function) ? "is protected"
2406 : "has protected accessibility");
2407 error ("within this context");
2409 return error_mark_node;
2412 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2413 type (if it exists) is a pointer to. */
2415 if (DECL_ABSTRACT_VIRTUAL_P (function)
2416 && instance == C_C_D
2417 && DECL_CONSTRUCTOR_P (current_function_decl)
2418 && ! (flags & LOOKUP_NONVIRTUAL)
2419 && value_member (function, get_abstract_virtuals (basetype)))
2420 cp_error ("abstract virtual `%#D' called from constructor", function);
2422 if (IS_SIGNATURE (basetype) && static_call_context)
2424 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2425 basetype, name);
2426 return error_mark_node;
2428 else if (IS_SIGNATURE (basetype))
2429 return build_signature_method_call (basetype, instance, function, parms);
2431 function = DECL_MAIN_VARIANT (function);
2432 /* Declare external function if necessary. */
2433 assemble_external (function);
2435 fntype = TREE_TYPE (function);
2436 if (TREE_CODE (fntype) == POINTER_TYPE)
2437 fntype = TREE_TYPE (fntype);
2438 basetype = DECL_CLASS_CONTEXT (function);
2440 /* If we are referencing a virtual function from an object
2441 of effectively static type, then there is no need
2442 to go through the virtual function table. */
2443 if (need_vtbl == maybe_needed)
2445 int fixed_type = resolves_to_fixed_type_p (instance, 0);
2447 if (all_virtual == 1
2448 && DECL_VINDEX (function)
2449 && may_be_remote (basetype))
2450 need_vtbl = needed;
2451 else if (DECL_VINDEX (function))
2452 need_vtbl = fixed_type ? unneeded : needed;
2453 else
2454 need_vtbl = not_needed;
2457 if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
2458 && !DECL_CONSTRUCTOR_P (function))
2460 /* Let's be nice to the user for now, and give reasonable
2461 default behavior. */
2462 instance_ptr = current_class_decl;
2463 if (instance_ptr)
2465 if (basetype != current_class_type)
2467 tree binfo = get_binfo (basetype, current_class_type, 1);
2468 if (binfo == NULL_TREE)
2470 error_not_base_type (function, current_class_type);
2471 return error_mark_node;
2473 else if (basetype == error_mark_node)
2474 return error_mark_node;
2477 /* Only allow a static member function to call another static member
2478 function. */
2479 else if (DECL_LANG_SPECIFIC (function)
2480 && !DECL_STATIC_FUNCTION_P (function))
2482 cp_error ("cannot call member function `%D' without object",
2483 function);
2484 return error_mark_node;
2488 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2490 if (TYPE_SIZE (value_type) == 0)
2492 if (flags & LOOKUP_COMPLAIN)
2493 incomplete_type_error (0, value_type);
2494 return error_mark_node;
2497 /* We do not pass FUNCTION into `convert_arguments', because by
2498 now everything should be ok. If not, then we have a serious error. */
2499 if (DECL_STATIC_FUNCTION_P (function))
2500 parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2501 TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
2502 else if (need_vtbl == unneeded)
2504 int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
2505 basetype = TREE_TYPE (instance);
2506 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function)) != TYPE_MAIN_VARIANT (basetype)
2507 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2509 basetype = DECL_CLASS_CONTEXT (function);
2510 instance_ptr = convert_pointer_to (basetype, instance_ptr);
2511 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2513 parms = tree_cons (NULL_TREE, instance_ptr,
2514 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
2516 else
2518 if ((flags & LOOKUP_NONVIRTUAL) == 0)
2519 basetype = DECL_CONTEXT (function);
2521 /* First parm could be integer_zerop with casts like
2522 ((Object*)0)->Object::IsA() */
2523 if (!integer_zerop (TREE_VALUE (parms)))
2525 /* Since we can't have inheritance with a union, doing get_binfo
2526 on it won't work. We do all the convert_pointer_to_real
2527 stuff to handle MI correctly...for unions, that's not
2528 an issue, so we must short-circuit that extra work here. */
2529 tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
2530 if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
2531 instance_ptr = TREE_VALUE (parms);
2532 else
2534 tree binfo = get_binfo (basetype,
2535 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
2537 instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
2539 instance_ptr
2540 = convert_pointer_to (build_type_variant (basetype,
2541 constp, volatilep),
2542 instance_ptr);
2544 if (TREE_CODE (instance_ptr) == COND_EXPR)
2546 instance_ptr = save_expr (instance_ptr);
2547 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2549 else if (TREE_CODE (instance_ptr) == NOP_EXPR
2550 && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
2551 && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
2553 /* The call to `convert_pointer_to' may return error_mark_node. */
2554 else if (TREE_CODE (instance_ptr) == ERROR_MARK)
2555 return instance_ptr;
2556 else if (instance == NULL_TREE
2557 || TREE_CODE (instance) != INDIRECT_REF
2558 || TREE_OPERAND (instance, 0) != instance_ptr)
2559 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2561 parms = tree_cons (NULL_TREE, instance_ptr,
2562 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL));
2565 #if 0
2566 /* Constructors do not overload method calls. */
2567 else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
2568 && name != TYPE_IDENTIFIER (basetype)
2569 && (TREE_CODE (function) != FUNCTION_DECL
2570 || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)),
2571 OPERATOR_METHOD_FORMAT,
2572 OPERATOR_METHOD_LENGTH))
2573 && (may_be_remote (basetype) || instance != C_C_D))
2575 tree fn_as_int;
2577 parms = TREE_CHAIN (parms);
2579 if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
2580 fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
2581 else
2582 fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
2583 if (all_virtual == 1)
2584 fn_as_int = convert (integer_type_node, fn_as_int);
2586 result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
2588 if (result == NULL_TREE)
2590 compiler_error ("could not overload `operator->()(...)'");
2591 return error_mark_node;
2593 else if (result == error_mark_node)
2594 return error_mark_node;
2596 #if 0
2597 /* Do this if we want the result of operator->() to inherit
2598 the type of the function it is subbing for. */
2599 TREE_TYPE (result) = value_type;
2600 #endif
2602 return result;
2604 #endif
2606 if (need_vtbl == needed)
2608 function = build_vfn_ref (&TREE_VALUE (parms), instance,
2609 DECL_VINDEX (function));
2610 TREE_TYPE (function) = build_pointer_type (fntype);
2613 if (TREE_CODE (function) == FUNCTION_DECL)
2614 GNU_xref_call (current_function_decl,
2615 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
2618 int is_constructor;
2620 if (TREE_CODE (function) == FUNCTION_DECL)
2622 is_constructor = DECL_CONSTRUCTOR_P (function);
2623 if (DECL_INLINE (function))
2624 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
2625 else
2627 assemble_external (function);
2628 TREE_USED (function) = 1;
2629 function = default_conversion (function);
2632 else
2634 is_constructor = 0;
2635 function = default_conversion (function);
2638 result = build_nt (CALL_EXPR, function, parms, NULL_TREE);
2640 TREE_TYPE (result) = value_type;
2641 TREE_SIDE_EFFECTS (result) = 1;
2642 TREE_RAISES (result)
2643 = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
2644 TREE_HAS_CONSTRUCTOR (result) = is_constructor;
2645 return result;
2649 /* Similar to `build_method_call', but for overloaded non-member functions.
2650 The name of this function comes through NAME. The name depends
2651 on PARMS.
2653 Note that this function must handle simple `C' promotions,
2654 as well as variable numbers of arguments (...), and
2655 default arguments to boot.
2657 If the overloading is successful, we return a tree node which
2658 contains the call to the function.
2660 If overloading produces candidates which are probable, but not definite,
2661 we hold these candidates. If FINAL_CP is non-zero, then we are free
2662 to assume that final_cp points to enough storage for all candidates that
2663 this function might generate. The `harshness' array is preallocated for
2664 the first candidate, but not for subsequent ones.
2666 Note that the DECL_RTL of FUNCTION must be made to agree with this
2667 function's new name. */
2669 tree
2670 build_overload_call_real (fnname, parms, flags, final_cp, buildxxx)
2671 tree fnname, parms;
2672 int flags;
2673 struct candidate *final_cp;
2674 int buildxxx;
2676 /* must check for overloading here */
2677 tree overload_name, functions, function, parm;
2678 tree parmtypes = NULL_TREE, last = NULL_TREE;
2679 register tree outer;
2680 int length;
2681 int parmlength = list_length (parms);
2683 struct candidate *candidates, *cp;
2685 if (final_cp)
2687 final_cp[0].h.code = 0;
2688 final_cp[0].h.distance = 0;
2689 final_cp[0].function = 0;
2690 /* end marker. */
2691 final_cp[1].h.code = EVIL_CODE;
2694 for (parm = parms; parm; parm = TREE_CHAIN (parm))
2696 register tree t = TREE_TYPE (TREE_VALUE (parm));
2698 if (t == error_mark_node)
2700 if (final_cp)
2701 final_cp->h.code = EVIL_CODE;
2702 return error_mark_node;
2704 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
2706 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
2707 Also convert OFFSET_TYPE entities to their normal selves.
2708 This eliminates needless calls to `compute_conversion_costs'. */
2709 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
2710 t = TREE_TYPE (TREE_VALUE (parm));
2712 last = build_tree_list (NULL_TREE, t);
2713 parmtypes = chainon (parmtypes, last);
2715 if (last)
2716 TREE_CHAIN (last) = void_list_node;
2717 else
2718 parmtypes = void_list_node;
2720 functions = lookup_name_nonclass (fnname);
2722 if (functions == NULL_TREE)
2724 if (flags & LOOKUP_SPECULATIVELY)
2725 return NULL_TREE;
2726 if (flags & LOOKUP_COMPLAIN)
2727 error ("only member functions apply");
2728 if (final_cp)
2729 final_cp->h.code = EVIL_CODE;
2730 return error_mark_node;
2733 if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
2735 functions = DECL_MAIN_VARIANT (functions);
2736 if (final_cp)
2738 /* We are just curious whether this is a viable alternative or
2739 not. */
2740 compute_conversion_costs (functions, parms, final_cp, parmlength);
2741 return functions;
2743 else
2744 return build_function_call_real (functions, parms, 1, flags);
2747 if (TREE_CODE (functions) == TREE_LIST
2748 && TREE_VALUE (functions) == NULL_TREE)
2750 if (flags & LOOKUP_SPECULATIVELY)
2751 return NULL_TREE;
2753 if (flags & LOOKUP_COMPLAIN)
2754 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2755 TREE_PURPOSE (functions));
2756 if (final_cp)
2757 final_cp->h.code = EVIL_CODE;
2758 return error_mark_node;
2761 length = count_functions (functions);
2763 if (final_cp)
2764 candidates = final_cp;
2765 else
2767 candidates
2768 = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
2769 bzero (candidates, (length + 1) * sizeof (struct candidate));
2772 cp = candidates;
2774 my_friendly_assert (is_overloaded_fn (functions), 169);
2776 functions = get_first_fn (functions);
2778 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2779 for (outer = functions; outer; outer = DECL_CHAIN (outer))
2781 int template_cost = 0;
2782 function = outer;
2783 if (TREE_CODE (function) != FUNCTION_DECL
2784 && ! (TREE_CODE (function) == TEMPLATE_DECL
2785 && ! DECL_TEMPLATE_IS_CLASS (function)
2786 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
2788 enum tree_code code = TREE_CODE (function);
2789 if (code == TEMPLATE_DECL)
2790 code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
2791 if (code == CONST_DECL)
2792 cp_error_at
2793 ("enumeral value `%D' conflicts with function of same name",
2794 function);
2795 else if (code == VAR_DECL)
2797 if (TREE_STATIC (function))
2798 cp_error_at
2799 ("variable `%D' conflicts with function of same name",
2800 function);
2801 else
2802 cp_error_at
2803 ("constant field `%D' conflicts with function of same name",
2804 function);
2806 else if (code == TYPE_DECL)
2807 continue;
2808 else
2809 my_friendly_abort (2);
2810 error ("at this point in file");
2811 continue;
2813 if (TREE_CODE (function) == TEMPLATE_DECL)
2815 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
2816 tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
2817 int i;
2819 i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
2820 TYPE_ARG_TYPES (TREE_TYPE (function)),
2821 parms, &template_cost, 0);
2822 if (i == 0)
2823 function = instantiate_template (function, targs);
2826 if (TREE_CODE (function) == TEMPLATE_DECL)
2828 /* Unconverted template -- failed match. */
2829 cp->function = function;
2830 cp->u.bad_arg = -4;
2831 cp->h.code = EVIL_CODE;
2833 else
2835 struct candidate *cp2;
2837 /* Check that this decl is not the same as a function that's in
2838 the list due to some template instantiation. */
2839 cp2 = candidates;
2840 while (cp2 != cp)
2841 if (cp2->function == function)
2842 break;
2843 else
2844 cp2 += 1;
2845 if (cp2->function == function)
2846 continue;
2848 function = DECL_MAIN_VARIANT (function);
2850 /* Can't use alloca here, since result might be
2851 passed to calling function. */
2852 cp->h_len = parmlength;
2853 cp->harshness = (struct harshness_code *)
2854 oballoc ((parmlength + 1) * sizeof (struct harshness_code));
2856 compute_conversion_costs (function, parms, cp, parmlength);
2858 /* Make sure this is clear as well. */
2859 cp->h.int_penalty += template_cost;
2861 if ((cp[0].h.code & EVIL_CODE) == 0)
2863 cp[1].h.code = EVIL_CODE;
2865 /* int_penalty is set by convert_harshness_ansi for cases
2866 where we need to know about any penalties that would
2867 otherwise make a TRIVIAL_CODE pass. */
2868 if (final_cp
2869 && template_cost == 0
2870 && cp[0].h.code <= TRIVIAL_CODE
2871 && cp[0].h.int_penalty == 0)
2873 final_cp[0].h = cp[0].h;
2874 return function;
2876 cp++;
2881 if (cp - candidates)
2883 tree rval = error_mark_node;
2885 /* Leave marker. */
2886 cp[0].h.code = EVIL_CODE;
2887 if (cp - candidates > 1)
2889 struct candidate *best_cp
2890 = ideal_candidate (NULL_TREE, candidates,
2891 cp - candidates, parms, parmlength);
2892 if (best_cp == (struct candidate *)0)
2894 if (flags & LOOKUP_COMPLAIN)
2896 cp_error ("call of overloaded `%D' is ambiguous", fnname);
2897 print_n_candidates (candidates, cp - candidates);
2899 return error_mark_node;
2901 else
2902 rval = best_cp->function;
2904 else
2906 cp -= 1;
2907 if (cp->h.code & EVIL_CODE)
2909 if (flags & LOOKUP_COMPLAIN)
2910 error ("type conversion ambiguous");
2912 else
2913 rval = cp->function;
2916 if (final_cp)
2917 return rval;
2919 return buildxxx ? build_function_call_real (rval, parms, 0, flags)
2920 : build_function_call_real (rval, parms, 1, flags);
2923 if (flags & LOOKUP_SPECULATIVELY)
2924 return NULL_TREE;
2926 if (flags & LOOKUP_COMPLAIN)
2927 report_type_mismatch (cp, parms, "function",
2928 decl_as_string (cp->function, 1));
2930 return error_mark_node;
2933 tree
2934 build_overload_call (fnname, parms, flags, final_cp)
2935 tree fnname, parms;
2936 int flags;
2937 struct candidate *final_cp;
2939 return build_overload_call_real (fnname, parms, flags, final_cp, 0);
2942 tree
2943 build_overload_call_maybe (fnname, parms, flags, final_cp)
2944 tree fnname, parms;
2945 int flags;
2946 struct candidate *final_cp;
2948 return build_overload_call_real (fnname, parms, flags, final_cp, 1);