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).
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)
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, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
40 extern int inhibit_warnings;
41 extern tree ctor_label, dtor_label;
43 /* Compute the ease with which a conversion can be performed
44 between an expected and the given type. */
46 static struct harshness_code convert_harshness PROTO((register tree, register tree, tree));
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))
54 /* Ordering function for overload resolution. Compare two candidates
58 rank_for_overload (x, y)
59 struct candidate *x, *y;
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))
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;
71 if (y->h.code & STD_CODE)
73 if (x->h.code & STD_CODE)
74 return y->h.distance - x->h.distance;
77 if (x->h.code & STD_CODE)
80 return y->h.code - x->h.code;
83 /* Compare two candidates, argument by argument. */
87 struct candidate *x, *y;
91 if (x->h_len != y->h_len)
94 for (i = 0; i < x->h_len; i++)
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;
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;
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. */
115 static struct harshness_code
116 convert_harshness (type, parmtype, parm)
117 register tree type, parmtype;
120 struct harshness_code h;
121 register enum tree_code codel;
122 register enum tree_code coder;
129 #ifdef GATHER_STATISTICS
130 n_convert_harshness++;
133 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
136 parm = convert_from_reference (parm);
137 parmtype = TREE_TYPE (parmtype);
141 lvalue = lvalue_p (parm);
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);
150 codel = TREE_CODE (type);
151 coder = TREE_CODE (parmtype);
153 if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
154 return ZERO_RETURN (h);
156 if (coder == ERROR_MARK)
157 return EVIL_RETURN (h);
159 if (codel == REFERENCE_TYPE)
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);
169 ttl = TREE_TYPE (type);
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);
178 if ((TYPE_READONLY (ttl) < constp)
179 || (TYPE_VOLATILE (ttl) < volatilep))
180 return EVIL_RETURN (h);
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));
187 ttl = TYPE_MAIN_VARIANT (ttl);
189 if (form == OFFSET_TYPE)
191 intype = TREE_TYPE (intype);
192 form = TREE_CODE (intype);
197 if (TREE_CODE (ttl) == ARRAY_TYPE && TREE_CODE (ttr) == ARRAY_TYPE)
199 if (comptypes (ttl, ttr, 1))
200 return ZERO_RETURN (h);
201 return EVIL_RETURN (h);
204 h = convert_harshness (ttl, ttr, NULL_TREE);
205 if (penalty && h.code == 0)
208 h.int_penalty = penalty;
213 if (codel == POINTER_TYPE && fntype_p (parmtype))
216 struct harshness_code h1, h2;
218 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
219 type = TREE_TYPE (type);
221 if (coder == POINTER_TYPE)
223 parmtype = TREE_TYPE (parmtype);
224 coder = TREE_CODE (parmtype);
227 if (coder != TREE_CODE (type))
228 return EVIL_RETURN (h);
230 if (type != parmtype && coder == METHOD_TYPE)
232 tree ttl = TYPE_METHOD_BASETYPE (type);
233 tree ttr = TYPE_METHOD_BASETYPE (parmtype);
235 int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
238 b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
240 return EVIL_RETURN (h);
241 h.distance = -b_or_d;
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)));
253 /* We allow the default conversion between function type
254 and pointer-to-function type for free. */
255 if (comptypes (type, parmtype, 1))
259 return EVIL_RETURN (h);
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)
268 h1.code = TRIVIAL_CODE;
271 if (h2.distance != 0)
275 /* This only works for pointers. */
276 if (TREE_CODE (p1) != POINTER_TYPE
277 && TREE_CODE (p1) != REFERENCE_TYPE)
278 return EVIL_RETURN (h);
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);
286 binfo = get_binfo (p2, p1, 0);
288 binfo = get_binfo (p1, p2, 0);
290 if (! BINFO_OFFSET_ZEROP (binfo))
293 static int explained = 0;
295 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p2, p1);
297 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p1, p2);
300 sorry ("(because pointer values change during conversion)");
302 return EVIL_RETURN (h);
307 if (h2.distance > h1.distance)
308 h1.distance = h2.distance;
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)
315 h2 = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2),
317 if (h2.code & EVIL_CODE)
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;
330 if (h2.distance > h1.distance)
331 h1.distance = h2.distance;
332 p1 = TREE_CHAIN (p1);
333 p2 = TREE_CHAIN (p2);
340 return EVIL_RETURN (h);
341 h1.code |= ELLIPSIS_CODE;
346 if (TREE_PURPOSE (p1) == NULL_TREE)
347 h1.code |= EVIL_CODE;
351 else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
355 /* Get to the OFFSET_TYPE that this might be. */
356 type = TREE_TYPE (type);
358 if (coder != TREE_CODE (type))
359 return EVIL_RETURN (h);
361 ttl = TYPE_OFFSET_BASETYPE (type);
362 ttr = TYPE_OFFSET_BASETYPE (parmtype);
368 int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
371 b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
373 return EVIL_RETURN (h);
374 h.distance = -b_or_d;
381 /* Now test the OFFSET_TYPE's target compatibility. */
382 type = TREE_TYPE (type);
383 parmtype = TREE_TYPE (parmtype);
386 if (coder == UNKNOWN_TYPE)
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);
397 if (coder == VOID_TYPE)
398 return EVIL_RETURN (h);
400 if (codel == BOOLEAN_TYPE)
402 if (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE)
403 return STD_RETURN (h);
404 else if (coder == POINTER_TYPE || coder == OFFSET_TYPE)
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). */
410 return STD_RETURN (h);
412 return EVIL_RETURN (h);
415 if (INTEGRAL_CODE_P (codel))
417 /* Control equivalence of ints an enums. */
419 if (codel == ENUMERAL_TYPE
420 && flag_int_enum_equivalence == 0)
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);
428 /* else enums and ints (almost) freely interconvert. */
430 if (INTEGRAL_CODE_P (coder))
432 if (TYPE_MAIN_VARIANT (type)
433 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
442 else if (coder == REAL_TYPE)
450 if (codel == REAL_TYPE)
452 if (coder == REAL_TYPE)
454 if (TYPE_MAIN_VARIANT (type)
455 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
462 else if (INTEGRAL_CODE_P (coder))
470 /* Convert arrays which have not previously been converted. */
471 if (coder == ARRAY_TYPE)
473 coder = POINTER_TYPE;
476 parm = decay_conversion (parm);
477 parmtype = TREE_TYPE (parm);
480 parmtype = build_pointer_type (TREE_TYPE (parmtype));
483 /* Conversions among pointers */
484 if (codel == POINTER_TYPE && coder == POINTER_TYPE)
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);
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. */
495 if ((TREE_CODE (ttl) == METHOD_TYPE
496 || TREE_CODE (ttl) == FUNCTION_TYPE)
497 && TREE_CODE (ttl) == TREE_CODE (ttr))
499 if (comptypes (ttl, ttr, -1))
501 h.code = penalty ? STD_CODE : 0;
510 if (TREE_CODE (ttl) != VOID_TYPE
511 && (TREE_CODE (ttr) != VOID_TYPE || !parm || !integer_zerop (parm)))
513 if (comp_target_types (type, parmtype, 1) <= 0)
514 return EVIL_RETURN (h);
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),
523 || (comp_target_types (ttl, ttr, 0) > 0)))
524 return EVIL_RETURN (h);
529 tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
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)))
537 else if ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
538 || (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2)))
546 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
548 int b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
551 b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
553 return EVIL_RETURN (h);
554 h.distance = -b_or_d;
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))
567 h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
571 h.code = penalty ? STD_CODE : PROMO_CODE;
572 /* Catch things like `const char *' -> `const void *'
573 vs `const char *' -> `void *'. */
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)))
580 else if ((TYPE_READONLY (tmp1) > TREE_READONLY (tmp2))
581 || (TYPE_VOLATILE (tmp1) > TYPE_VOLATILE (tmp2)))
587 if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
589 /* This is not a bad match, but don't let it beat
590 integer-enum combinations. */
591 if (parm && integer_zerop (parm))
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. */
605 if (codel == RECORD_TYPE && coder == POINTER_TYPE
606 && IS_SIGNATURE_POINTER (type) && IS_SIGNATURE (TREE_TYPE (parmtype)))
607 return ZERO_RETURN (h);
609 if (codel == RECORD_TYPE && coder == RECORD_TYPE)
611 int b_or_d = get_base_distance (type, parmtype, 0, (tree*)0);
614 b_or_d = get_base_distance (parmtype, type, 0, (tree*)0);
616 return EVIL_RETURN (h);
617 h.distance = -b_or_d;
624 return EVIL_RETURN (h);
627 /* A clone of build_type_conversion for checking user-defined conversions in
628 overload resolution. */
631 user_harshness (type, parmtype)
632 register tree type, parmtype;
635 tree winner = NULL_TREE;
639 tree typename = build_typename_overload (type);
640 if (lookup_fnfields (TYPE_BINFO (parmtype), typename, 0))
644 for (conv = lookup_conversions (parmtype); conv; conv = TREE_CHAIN (conv))
646 struct harshness_code tmp;
647 tree cand = TREE_VALUE (conv);
649 if (winner && winner == cand)
652 tmp = convert_harshness (type, TREE_TYPE (TREE_TYPE (cand)), NULL_TREE);
653 if ((tmp.code < USER_CODE) && (tmp.distance >= 0))
672 can_convert (to, from)
675 struct harshness_code h;
676 h = convert_harshness (to, from, NULL_TREE);
677 return (h.code < USER_CODE) && (h.distance >= 0);
681 can_convert_arg (to, from, arg)
684 struct harshness_code h;
685 h = convert_harshness (to, from, arg);
686 return (h.code < USER_CODE) && (h.distance >= 0);
689 #ifdef DEBUG_MATCHING
692 struct harshness_code *h;
694 static char buf[1024];
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");
718 sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
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'
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
739 compute_conversion_costs (function, tta_in, cp, arglen)
742 struct candidate *cp;
745 tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
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;
756 int strike_index = 0, win;
757 struct harshness_code lose;
758 extern int cp_silent;
760 #ifdef GATHER_STATISTICS
761 n_compute_conversion_costs++;
764 #ifndef DEBUG_MATCHING
765 /* We don't emit any warnings or errors while trying out each candidate. */
769 cp->function = function;
770 cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
771 cp->u.bad_arg = 0; /* optimistic! */
775 cp->h.int_penalty = 0;
776 bzero ((char *) cp->harshness,
777 (cp->h_len + 1) * sizeof (struct harshness_code));
781 struct harshness_code h;
783 if (ttf == void_list_node)
786 if (type_unknown_p (TREE_VALUE (tta)))
788 /* Must perform some instantiation here. */
789 tree rhs = TREE_VALUE (tta);
790 tree lhstype = TREE_VALUE (ttf);
792 /* Keep quiet about possible contravariance violations. */
793 int old_inhibit_warnings = inhibit_warnings;
794 inhibit_warnings = 1;
796 /* @@ This is to undo what `grokdeclarator' does to
797 parameter types. It really should go through
798 something more general. */
800 TREE_TYPE (tta) = unknown_type_node;
801 rhs = instantiate_type (lhstype, rhs, 0);
802 inhibit_warnings = old_inhibit_warnings;
804 if (TREE_CODE (rhs) == ERROR_MARK)
807 h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
811 #ifdef DEBUG_MATCHING
812 static tree old_function = NULL_TREE;
814 if (!old_function || function != old_function)
816 cp_error ("trying %D", function);
817 old_function = function;
820 cp_error (" doing (%T) %E against arg %T",
821 TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
825 h = convert_harshness (TREE_VALUE (ttf),
826 TREE_TYPE (TREE_VALUE (tta)),
829 #ifdef DEBUG_MATCHING
830 cp_error (" evaluated %s", print_harshness (&h));
834 cp->harshness[strike_index] = h;
835 if ((h.code & EVIL_CODE)
836 || ((h.code & STD_CODE) && h.distance < 0))
838 cp->u.bad_arg = strike_index;
841 else if (h.code & ELLIPSIS_CODE)
842 ellipsis_strikes += 1;
844 /* This is never set by `convert_harshness'. */
845 else if (h.code & USER_CODE)
852 if ((h.code & STD_CODE) && h.distance)
854 if (h.distance > b_or_d_strikes)
855 b_or_d_strikes = h.distance;
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;
864 ttf = TREE_CHAIN (ttf);
865 tta = TREE_CHAIN (tta);
871 /* ran out of formals, and parmlist is fixed size. */
872 if (ttf /* == void_type_node */)
874 cp->h.code = EVIL_CODE;
881 struct harshness_code h;
882 int l = list_length (tta);
883 ellipsis_strikes += l;
884 h.code = ELLIPSIS_CODE;
888 cp->harshness[strike_index++] = h;
891 else if (ttf && ttf != void_list_node)
893 /* ran out of actuals, and no defaults. */
894 if (TREE_PURPOSE (ttf) == NULL_TREE)
896 cp->h.code = EVIL_CODE;
901 /* Store index of first default. */
902 cp->harshness[arglen].distance = strike_index+1;
905 cp->harshness[arglen].distance = 0;
907 /* Argument list lengths work out, so don't need to check them again. */
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. */
914 /* See if any user-defined conversions apply.
915 But make sure that we do not loop. */
916 static int dont_convert_types = 0;
918 if (dont_convert_types)
920 cp->h.code = EVIL_CODE;
925 win = 0; /* Only get one chance to win. */
926 ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
933 if (ttf == void_list_node)
936 lose = cp->harshness[strike_index];
937 if ((lose.code & EVIL_CODE)
938 || ((lose.code & STD_CODE) && lose.distance < 0))
940 tree actual_type = TREE_TYPE (TREE_VALUE (tta));
941 tree formal_type = TREE_VALUE (ttf);
942 int extra_conversions = 0;
944 dont_convert_types = 1;
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);
951 if (formal_type != error_mark_node
952 && actual_type != error_mark_node)
954 formal_type = complete_type (TYPE_MAIN_VARIANT (formal_type));
955 actual_type = complete_type (TYPE_MAIN_VARIANT (actual_type));
957 if (TYPE_HAS_CONSTRUCTOR (formal_type))
959 /* If it has a constructor for this type,
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)
967 if (TYPE_LANG_SPECIFIC (actual_type)
968 && TYPE_HAS_CONVERSION (actual_type))
970 int extra = user_harshness (formal_type, actual_type);
972 if (extra == EVIL_CODE)
977 extra_conversions = extra;
981 dont_convert_types = 0;
986 cp->harshness[strike_index].code
987 = USER_CODE | (extra_conversions ? STD_CODE : 0);
992 if (cp->u.bad_arg > strike_index)
993 cp->u.bad_arg = strike_index;
995 evil_strikes = win ? 2 : 1;
1000 ttf = TREE_CHAIN (ttf);
1001 tta = TREE_CHAIN (tta);
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)
1013 tree this_parm = TREE_VALUE (ttf_in);
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)))
1019 cp->harshness[0].code |= TRIVIAL_CODE;
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;
1035 cp->h.code = EVIL_CODE;
1036 if (ellipsis_strikes)
1037 cp->h.code |= ELLIPSIS_CODE;
1039 cp->h.code |= USER_CODE;
1041 #ifdef DEBUG_MATCHING
1042 cp_error ("final eval %s", print_harshness (&cp->h));
1046 /* Subroutine of ideal_candidate. See if X or Y is a better match
1050 strictly_better (x, y)
1051 unsigned short x, y;
1059 if (xor >= x || xor >= y)
1064 /* When one of several possible overloaded functions and/or methods
1065 can be called, choose the best candidate for overloading.
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. */
1076 static struct candidate *
1077 ideal_candidate (candidates, n_candidates, len)
1078 struct candidate *candidates;
1082 struct candidate *cp = candidates+n_candidates;
1083 int i, j = -1, best_code;
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
1091 qsort (candidates, n_candidates, sizeof (struct candidate),
1093 best_code = cp[-1].h.code;
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))
1101 for (j = 0; j < n_candidates; j++)
1102 if (! strictly_better (candidates[j].h.code, best_code))
1105 qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
1107 for (i = 0; i < len; i++)
1109 if (cp[-1].harshness[i].code < cp[-2].harshness[i].code)
1111 else if (cp[-1].harshness[i].code > cp[-2].harshness[i].code)
1113 else if (cp[-1].harshness[i].code & STD_CODE)
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)
1119 else if (cp[-1].harshness[i].distance < cp[-2].harshness[i].distance)
1122 else if (cp[-1].harshness[i].code & PROMO_CODE)
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)
1129 else if (cp[-1].harshness[i].int_penalty < cp[-2].harshness[i].int_penalty)
1134 if (! better || worse)
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. */
1145 may_be_remote (parent)
1148 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
1151 if (current_class_type == NULL_TREE)
1154 if (parent == current_class_type)
1157 if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
1163 build_vfield_ref (datum, type)
1167 int old_assume_nonnull_objects = flag_assume_nonnull_objects;
1169 if (datum == error_mark_node)
1170 return error_mark_node;
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);
1177 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
1178 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
1179 datum, CLASSTYPE_VFIELD (type));
1181 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
1182 flag_assume_nonnull_objects = old_assume_nonnull_objects;
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. */
1191 build_field_call (basetype_path, instance_ptr, name, parms)
1192 tree basetype_path, instance_ptr, name, parms;
1194 tree field, instance;
1196 if (instance_ptr == current_class_ptr)
1198 /* Check to see if we really have a reference to an instance variable
1199 with `operator()()' overloaded. */
1200 field = IDENTIFIER_CLASS_VALUE (name);
1202 if (field == NULL_TREE)
1204 cp_error ("`this' has no member named `%D'", name);
1205 return error_mark_node;
1208 if (TREE_CODE (field) == FIELD_DECL)
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;
1216 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1217 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
1218 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
1220 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1222 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
1223 return build_function_call (instance, parms);
1224 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
1225 return build_function_call (instance, tree_cons (NULL_TREE, current_class_ptr, parms));
1231 /* Check to see if this is not really a reference to an instance variable
1232 with `operator()()' overloaded. */
1233 field = lookup_field (basetype_path, name, 1, 0);
1235 /* This can happen if the reference was ambiguous or for access
1237 if (field == error_mark_node)
1238 return error_mark_node;
1243 tree ftype = TREE_TYPE (field);
1245 if (TREE_CODE (ftype) == REFERENCE_TYPE)
1246 ftype = TREE_TYPE (ftype);
1248 if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
1250 /* Make the next search for this field very short. */
1251 basetype = DECL_FIELD_CONTEXT (field);
1252 instance_ptr = convert_pointer_to (basetype, instance_ptr);
1254 instance = build_indirect_ref (instance_ptr, NULL_PTR);
1255 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
1256 build_component_ref_1 (instance, field, 0),
1259 if (TREE_CODE (ftype) == POINTER_TYPE)
1261 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
1262 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
1264 /* This is a member which is a pointer to function. */
1266 = build_component_ref_1 (build_indirect_ref (instance_ptr,
1268 field, LOOKUP_COMPLAIN);
1269 if (ref == error_mark_node)
1270 return error_mark_node;
1271 return build_function_call (ref, parms);
1274 else if (TREE_CODE (ftype) == METHOD_TYPE)
1276 error ("invalid call via pointer-to-member function");
1277 return error_mark_node;
1286 find_scoped_type (type, inner_name, inner_types)
1287 tree type, inner_name, inner_types;
1289 tree tags = CLASSTYPE_TAGS (type);
1293 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1294 enclosing class) is set to the name for the enum type. So, if
1295 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1296 then this test will be true. */
1297 if (TREE_PURPOSE (tags) == inner_name)
1299 if (inner_types == NULL_TREE)
1300 return TYPE_NAME (TREE_VALUE (tags));
1301 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
1303 tags = TREE_CHAIN (tags);
1306 /* Look for a TYPE_DECL. */
1307 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
1308 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
1310 /* Code by raeburn. */
1311 if (inner_types == NULL_TREE)
1313 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
1319 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1320 the name that names the above nested type. INNER_TYPES
1321 is a chain of nested type names (held together by SCOPE_REFs);
1322 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1323 Returns NULL_TREE if there is an error. */
1326 resolve_scope_to_name (outer_type, inner_stuff)
1327 tree outer_type, inner_stuff;
1330 tree inner_name, inner_type;
1332 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
1334 /* We first try to look for a nesting in our current class context,
1335 then try any enclosing classes. */
1336 tree type = current_class_type;
1338 while (type && (TREE_CODE (type) == RECORD_TYPE
1339 || TREE_CODE (type) == UNION_TYPE))
1341 tree rval = resolve_scope_to_name (type, inner_stuff);
1343 if (rval != NULL_TREE)
1345 type = DECL_CONTEXT (TYPE_NAME (type));
1349 if (TREE_CODE (inner_stuff) == SCOPE_REF)
1351 inner_name = TREE_OPERAND (inner_stuff, 0);
1352 inner_type = TREE_OPERAND (inner_stuff, 1);
1356 inner_name = inner_stuff;
1357 inner_type = NULL_TREE;
1360 if (outer_type == NULL_TREE)
1363 /* If we have something that's already a type by itself,
1365 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
1368 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
1373 x = lookup_name (inner_name, 0);
1375 if (x && TREE_CODE (x) == NAMESPACE_DECL)
1377 x = lookup_namespace_name (x, inner_type);
1383 if (! IS_AGGR_TYPE (outer_type))
1386 /* Look for member classes or enums. */
1387 tmp = find_scoped_type (outer_type, inner_name, inner_type);
1389 /* If it's not a type in this class, then go down into the
1390 base classes and search there. */
1391 if (! tmp && TYPE_BINFO (outer_type))
1393 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
1394 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1396 for (i = 0; i < n_baselinks; i++)
1398 tree base_binfo = TREE_VEC_ELT (binfos, i);
1399 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
1409 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1410 This is how virtual function calls are avoided. */
1413 build_scoped_method_call (exp, basetype, name, parms)
1414 tree exp, basetype, name, parms;
1416 /* Because this syntactic form does not allow
1417 a pointer to a base class to be `stolen',
1418 we need not protect the derived->base conversion
1421 @@ But we do have to check access privileges later. */
1423 tree type = TREE_TYPE (exp);
1425 if (type == error_mark_node
1426 || basetype == error_mark_node)
1427 return error_mark_node;
1429 if (current_template_parms)
1431 if (TREE_CODE (name) == BIT_NOT_EXPR)
1433 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1434 name = build_min_nt (BIT_NOT_EXPR, type);
1436 name = build_min_nt (SCOPE_REF, basetype, name);
1437 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
1440 if (TREE_CODE (type) == REFERENCE_TYPE)
1441 type = TREE_TYPE (type);
1443 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1444 that explicit ~int is caught in the parser; this deals with typedefs
1445 and template parms. */
1446 if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
1448 if (type != basetype)
1449 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1450 exp, basetype, type);
1451 name = TREE_OPERAND (name, 0);
1452 if (basetype != name && basetype != get_type_value (name))
1453 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1455 return convert (void_type_node, exp);
1458 if (! is_aggr_type (basetype, 1))
1459 return error_mark_node;
1461 if (! IS_AGGR_TYPE (type))
1463 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1465 return error_mark_node;
1468 if ((binfo = binfo_or_else (basetype, type)))
1470 if (binfo == error_mark_node)
1471 return error_mark_node;
1472 if (TREE_CODE (exp) == INDIRECT_REF)
1473 decl = build_indirect_ref (convert_pointer_to (binfo,
1474 build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
1476 decl = build_scoped_ref (exp, basetype);
1478 /* Call to a destructor. */
1479 if (TREE_CODE (name) == BIT_NOT_EXPR)
1481 /* Explicit call to destructor. */
1482 name = TREE_OPERAND (name, 0);
1483 if (! (name == TYPE_MAIN_VARIANT (TREE_TYPE (decl))
1484 || name == constructor_name (TREE_TYPE (decl))
1485 || TREE_TYPE (decl) == get_type_value (name)))
1488 ("qualified type `%T' does not match destructor name `~%T'",
1489 TREE_TYPE (decl), name);
1490 return error_mark_node;
1492 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
1493 return convert (void_type_node, exp);
1495 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
1496 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
1500 /* Call to a method. */
1501 return build_method_call (decl, name, parms, binfo,
1502 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1504 return error_mark_node;
1508 print_candidates (candidates)
1511 cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
1512 candidates = TREE_CHAIN (candidates);
1516 cp_error_at (" %D", TREE_VALUE (candidates));
1517 candidates = TREE_CHAIN (candidates);
1522 print_n_candidates (candidates, n)
1523 struct candidate *candidates;
1528 cp_error_at ("candidates are: %D", candidates[0].function);
1529 for (i = 1; i < n; i++)
1530 cp_error_at (" %D", candidates[i].function);
1533 /* We want the address of a function or method. We avoid creating a
1534 pointer-to-member function. */
1537 build_addr_func (function)
1540 tree type = TREE_TYPE (function);
1542 /* We have to do these by hand to avoid real pointer to member
1544 if (TREE_CODE (type) == METHOD_TYPE)
1548 type = build_pointer_type (type);
1550 if (mark_addressable (function) == 0)
1551 return error_mark_node;
1553 addr = build1 (ADDR_EXPR, type, function);
1555 /* Address of a static or external variable or function counts
1557 if (staticp (function))
1558 TREE_CONSTANT (addr) = 1;
1563 function = default_conversion (function);
1568 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1569 POINTER_TYPE to those. Note, pointer to member function types
1570 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
1573 build_call (function, result_type, parms)
1574 tree function, result_type, parms;
1576 int is_constructor = 0;
1578 function = build_addr_func (function);
1580 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
1582 sorry ("unable to call pointer to member function here");
1583 return error_mark_node;
1586 if (TREE_CODE (function) == ADDR_EXPR
1587 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1588 && DECL_CONSTRUCTOR_P (TREE_OPERAND (function, 0)))
1591 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
1592 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
1593 TREE_TYPE (function) = result_type;
1594 TREE_SIDE_EFFECTS (function) = 1;
1600 default_parm_conversions (parms, last)
1603 tree parm, parmtypes = NULL_TREE;
1607 for (parm = parms; parm; parm = TREE_CHAIN (parm))
1609 tree t = TREE_TYPE (TREE_VALUE (parm));
1611 if (TREE_CODE (t) == OFFSET_TYPE
1612 || TREE_CODE (t) == METHOD_TYPE
1613 || TREE_CODE (t) == FUNCTION_TYPE)
1615 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
1616 t = TREE_TYPE (TREE_VALUE (parm));
1619 if (t == error_mark_node)
1620 return error_mark_node;
1622 *last = build_tree_list (NULL_TREE, t);
1623 parmtypes = chainon (parmtypes, *last);
1630 /* Build something of the form ptr->method (args)
1631 or object.method (args). This can also build
1632 calls to constructors, and find friends.
1634 Member functions always take their class variable
1637 INSTANCE is a class instance.
1639 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1641 PARMS help to figure out what that NAME really refers to.
1643 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1644 down to the real instance type to use for access checking. We need this
1645 information to get protected accesses correct. This parameter is used
1646 by build_member_call.
1648 FLAGS is the logical disjunction of zero or more LOOKUP_
1649 flags. See cp-tree.h for more info.
1651 If this is all OK, calls build_function_call with the resolved
1654 This function must also handle being called to perform
1655 initialization, promotion/coercion of arguments, and
1656 instantiation of default parameters.
1658 Note that NAME may refer to an instance variable name. If
1659 `operator()()' is defined for the type of that field, then we return
1663 build_method_call (instance, name, parms, basetype_path, flags)
1664 tree instance, name, parms, basetype_path;
1667 register tree function, fntype, value_type;
1668 register tree basetype, save_basetype;
1669 register tree baselink, result, parmtypes, parm;
1672 tree access = access_public_node;
1673 tree orig_basetype = basetype_path ? BINFO_TYPE (basetype_path) : NULL_TREE;
1675 /* Range of cases for vtable optimization. */
1676 enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
1677 enum vtable_needs need_vtbl = not_needed;
1680 tree save_name = name;
1682 tree instance_ptr = NULL_TREE;
1683 int all_virtual = flag_all_virtual;
1684 int static_call_context = 0;
1685 tree found_fns = NULL_TREE;
1687 /* Keep track of `const' and `volatile' objects. */
1688 int constp, volatilep;
1690 #ifdef GATHER_STATISTICS
1691 n_build_method_call++;
1694 if (instance == error_mark_node
1695 || name == error_mark_node
1696 || parms == error_mark_node
1697 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
1698 return error_mark_node;
1700 if (current_template_parms)
1702 if (TREE_CODE (name) == BIT_NOT_EXPR)
1704 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1705 name = build_min_nt (BIT_NOT_EXPR, type);
1708 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
1711 /* This is the logic that magically deletes the second argument to
1712 operator delete, if it is not needed. */
1713 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
1715 tree save_last = TREE_CHAIN (parms);
1717 /* get rid of unneeded argument */
1718 TREE_CHAIN (parms) = NULL_TREE;
1719 result = build_method_call (instance, name, parms, basetype_path,
1720 (LOOKUP_SPECULATIVELY|flags)
1722 /* If it finds a match, return it. */
1724 return build_method_call (instance, name, parms, basetype_path, flags);
1725 /* If it doesn't work, two argument delete must work */
1726 TREE_CHAIN (parms) = save_last;
1728 /* We already know whether it's needed or not for vec delete. */
1729 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
1730 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1731 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
1732 TREE_CHAIN (parms) = NULL_TREE;
1734 if (TREE_CODE (name) == BIT_NOT_EXPR)
1736 flags |= LOOKUP_DESTRUCTOR;
1737 name = TREE_OPERAND (name, 0);
1739 error ("destructors take no parameters");
1740 basetype = TREE_TYPE (instance);
1741 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1742 basetype = TREE_TYPE (basetype);
1743 if (! (name == basetype
1744 || (IS_AGGR_TYPE (basetype)
1745 && name == constructor_name (basetype))
1746 || basetype == get_type_value (name)))
1748 cp_error ("destructor name `~%D' does not match type `%T' of expression",
1750 return convert (void_type_node, instance);
1753 if (! TYPE_HAS_DESTRUCTOR (basetype))
1754 return convert (void_type_node, instance);
1755 instance = default_conversion (instance);
1756 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1757 return build_delete (build_pointer_type (basetype),
1758 instance_ptr, integer_two_node,
1759 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
1765 /* Initialize name for error reporting. */
1766 if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
1768 char *p = operator_name_string (name);
1769 xref_name = (char *)alloca (strlen (p) + 10);
1770 sprintf (xref_name, "operator %s", p);
1772 else if (TREE_CODE (name) == SCOPE_REF)
1773 xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
1775 xref_name = IDENTIFIER_POINTER (name);
1777 GNU_xref_call (current_function_decl, xref_name);
1780 if (instance == NULL_TREE)
1782 basetype = NULL_TREE;
1783 /* Check cases where this is really a call to raise
1785 if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
1787 basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
1789 basetype = TREE_VALUE (basetype);
1791 else if (TREE_CODE (name) == SCOPE_REF
1792 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
1794 if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
1795 return error_mark_node;
1796 basetype = purpose_member (TREE_OPERAND (name, 1),
1797 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
1799 basetype = TREE_VALUE (basetype);
1802 if (basetype != NULL_TREE)
1804 /* call to a constructor... */
1805 else if (basetype_path)
1807 basetype = BINFO_TYPE (basetype_path);
1808 if (name == TYPE_IDENTIFIER (basetype))
1809 name = ctor_identifier;
1811 else if (IDENTIFIER_HAS_TYPE_VALUE (name))
1813 basetype = IDENTIFIER_TYPE_VALUE (name);
1814 name = ctor_identifier;
1818 tree typedef_name = lookup_name (name, 1);
1819 if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
1821 /* Canonicalize the typedef name. */
1822 basetype = TREE_TYPE (typedef_name);
1823 name = ctor_identifier;
1827 cp_error ("no constructor named `%T' in scope",
1829 return error_mark_node;
1833 if (! IS_AGGR_TYPE (basetype))
1836 if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
1837 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1838 name, instance, basetype);
1840 return error_mark_node;
1843 else if (instance == current_class_ref || instance == current_class_ptr)
1845 /* When doing initialization, we side-effect the TREE_TYPE of
1846 current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1847 basetype = TREE_TYPE (current_class_ref);
1849 /* Anything manifestly `this' in constructors and destructors
1850 has a known type, so virtual function tables are not needed. */
1851 if (TYPE_VIRTUAL_P (basetype)
1852 && !(flags & LOOKUP_NONVIRTUAL))
1853 need_vtbl = (dtor_label || ctor_label)
1854 ? unneeded : maybe_needed;
1856 /* If `this' is a signature pointer and `name' is not a constructor,
1857 we are calling a signature member function. In that case, set the
1858 `basetype' to the signature type and dereference the `optr' field. */
1859 if (IS_SIGNATURE_POINTER (basetype)
1860 && TYPE_IDENTIFIER (basetype) != name)
1862 basetype = SIGNATURE_TYPE (basetype);
1863 instance_ptr = build_optr_ref (instance);
1864 instance_ptr = convert (build_pointer_type (basetype), instance_ptr);
1865 basetype_path = TYPE_BINFO (basetype);
1869 instance = current_class_ref;
1870 instance_ptr = current_class_ptr;
1871 basetype_path = TYPE_BINFO (current_class_type);
1873 result = build_field_call (basetype_path, instance_ptr, name, parms);
1878 else if (TREE_CODE (instance) == RESULT_DECL)
1880 basetype = TREE_TYPE (instance);
1881 /* Should we ever have to make a virtual function reference
1882 from a RESULT_DECL, know that it must be of fixed type
1883 within the scope of this function. */
1884 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1885 need_vtbl = maybe_needed;
1886 instance_ptr = build1 (ADDR_EXPR, build_pointer_type (basetype), instance);
1890 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1891 tree inst_ptr_basetype;
1893 static_call_context =
1894 (TREE_CODE (instance) == INDIRECT_REF
1895 && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
1896 && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
1898 if (TREE_CODE (instance) == OFFSET_REF)
1899 instance = resolve_offset_ref (instance);
1901 /* the base type of an instance variable is pointer to class */
1902 basetype = TREE_TYPE (instance);
1904 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1906 basetype = TREE_TYPE (basetype);
1907 if (! IS_AGGR_TYPE (basetype))
1908 goto non_aggr_error;
1909 /* Call to convert not needed because we are remaining
1910 within the same type. */
1911 instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
1913 inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
1917 if (! IS_AGGR_TYPE (basetype)
1918 && ! (TYPE_LANG_SPECIFIC (basetype)
1919 && (IS_SIGNATURE_POINTER (basetype)
1920 || IS_SIGNATURE_REFERENCE (basetype))))
1921 goto non_aggr_error;
1923 /* If `instance' is a signature pointer/reference and `name' is
1924 not a constructor, we are calling a signature member function.
1925 In that case set the `basetype' to the signature type. */
1926 if ((IS_SIGNATURE_POINTER (basetype)
1927 || IS_SIGNATURE_REFERENCE (basetype))
1928 && TYPE_IDENTIFIER (basetype) != name)
1929 basetype = SIGNATURE_TYPE (basetype);
1931 if ((IS_SIGNATURE (basetype)
1932 && (instance_ptr = instance))
1933 || (lvalue_p (instance)
1934 && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
1935 || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
1937 if (instance_ptr == error_mark_node)
1938 return error_mark_node;
1940 else if (TREE_CODE (instance) == NOP_EXPR
1941 || TREE_CODE (instance) == CONSTRUCTOR)
1943 /* A cast is not an lvalue. Initialize a fresh temp
1944 with the value we are casting from, and proceed with
1945 that temporary. We can't cast to a reference type,
1946 so that simplifies the initialization to something
1948 tree temp = get_temp_name (TREE_TYPE (instance), 0);
1949 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
1950 expand_aggr_init (temp, instance, 0, flags);
1953 store_init_value (temp, instance);
1954 expand_decl_init (temp);
1957 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1961 if (TREE_CODE (instance) != CALL_EXPR)
1962 my_friendly_abort (125);
1963 if (TYPE_NEEDS_CONSTRUCTING (basetype))
1964 instance = build_cplus_new (basetype, instance);
1967 instance = get_temp_name (basetype, 0);
1968 TREE_ADDRESSABLE (instance) = 1;
1970 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1972 /* @@ Should we call comp_target_types here? */
1973 if (IS_SIGNATURE (basetype))
1974 inst_ptr_basetype = basetype;
1976 inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
1977 if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
1978 basetype = inst_ptr_basetype;
1981 instance_ptr = convert (build_pointer_type (basetype), instance_ptr);
1982 if (instance_ptr == error_mark_node)
1983 return error_mark_node;
1987 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1988 not updated, so we use `basetype' instead. */
1989 if (basetype_path == NULL_TREE
1990 && IS_SIGNATURE (basetype))
1991 basetype_path = TYPE_BINFO (basetype);
1992 else if (basetype_path == NULL_TREE ||
1993 BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
1994 basetype_path = TYPE_BINFO (inst_ptr_basetype);
1996 result = build_field_call (basetype_path, instance_ptr, name, parms);
2000 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
2002 if (TREE_SIDE_EFFECTS (instance_ptr))
2004 /* This action is needed because the instance is needed
2005 for providing the base of the virtual function table.
2006 Without using a SAVE_EXPR, the function we are building
2007 may be called twice, or side effects on the instance
2008 variable (such as a post-increment), may happen twice. */
2009 instance_ptr = save_expr (instance_ptr);
2010 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2012 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
2014 /* This happens when called for operator new (). */
2015 instance = build_indirect_ref (instance, NULL_PTR);
2018 need_vtbl = maybe_needed;
2022 if (save_name == ctor_identifier)
2023 save_name = TYPE_IDENTIFIER (basetype);
2025 if (TYPE_SIZE (complete_type (basetype)) == 0)
2027 /* This is worth complaining about, I think. */
2028 cp_error ("cannot lookup method in incomplete type `%T'", basetype);
2029 return error_mark_node;
2032 save_basetype = TYPE_MAIN_VARIANT (basetype);
2034 parmtypes = default_parm_conversions (parms, &last);
2035 if (parmtypes == error_mark_node)
2037 return error_mark_node;
2040 if (instance && IS_SIGNATURE (basetype))
2042 /* @@ Should this be the constp/volatilep flags for the optr field
2043 of the signature pointer? */
2044 constp = TYPE_READONLY (basetype);
2045 volatilep = TYPE_VOLATILE (basetype);
2046 parms = tree_cons (NULL_TREE, instance_ptr, parms);
2050 /* TREE_READONLY (instance) fails for references. */
2051 constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
2052 volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
2053 parms = tree_cons (NULL_TREE, instance_ptr, parms);
2057 /* Raw constructors are always in charge. */
2058 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2059 && ! (flags & LOOKUP_HAS_IN_CHARGE))
2061 flags |= LOOKUP_HAS_IN_CHARGE;
2062 parms = tree_cons (NULL_TREE, integer_one_node, parms);
2063 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
2068 instance_ptr = build_int_2 (0, 0);
2069 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
2070 parms = tree_cons (NULL_TREE, instance_ptr, parms);
2073 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
2075 if (last == NULL_TREE)
2078 /* Look up function name in the structure type definition. */
2080 /* FIXME Axe most of this now? */
2081 if ((IDENTIFIER_HAS_TYPE_VALUE (name)
2082 && ! IDENTIFIER_OPNAME_P (name)
2083 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name)))
2084 || name == constructor_name (basetype)
2085 || name == ctor_identifier)
2087 tree tmp = NULL_TREE;
2088 if (IDENTIFIER_TYPE_VALUE (name) == basetype
2089 || name == constructor_name (basetype)
2090 || name == ctor_identifier)
2091 tmp = TYPE_BINFO (basetype);
2093 tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
2095 if (tmp != NULL_TREE)
2097 name_kind = "constructor";
2099 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2100 && ! (flags & LOOKUP_HAS_IN_CHARGE))
2102 /* Constructors called for initialization
2103 only are never in charge. */
2106 flags |= LOOKUP_HAS_IN_CHARGE;
2107 tmplist = tree_cons (NULL_TREE, integer_zero_node,
2108 TREE_CHAIN (parms));
2109 TREE_CHAIN (parms) = tmplist;
2110 tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
2111 TREE_CHAIN (parmtypes) = tmplist;
2113 basetype = BINFO_TYPE (tmp);
2116 name_kind = "method";
2119 name_kind = "method";
2121 if (basetype_path == NULL_TREE
2122 || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
2123 basetype_path = TYPE_BINFO (basetype);
2124 result = lookup_fnfields (basetype_path, name,
2125 (flags & LOOKUP_COMPLAIN));
2126 if (result == error_mark_node)
2127 return error_mark_node;
2129 for (pass = 0; pass < 2; pass++)
2131 struct candidate *candidates;
2132 struct candidate *cp;
2141 = (struct candidate *) alloca ((ever_seen+1)
2142 * sizeof (struct candidate));
2143 bzero ((char *) candidates, (ever_seen + 1) * sizeof (struct candidate));
2145 len = list_length (parms);
2148 /* First see if a global function has a shot at it. */
2149 if (flags & LOOKUP_GLOBAL)
2152 tree parm = instance_ptr;
2154 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
2155 parm = convert_from_reference (parm);
2156 else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
2157 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
2159 my_friendly_abort (167);
2161 friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
2164 cp->harshness = (struct harshness_code *)
2165 alloca ((len + 1) * sizeof (struct harshness_code));
2167 result = build_overload_call_real (name, friend_parms, 0, cp, 1);
2169 /* If it turns out to be the one we were actually looking for
2170 (it was probably a friend function), the return the
2172 if (TREE_CODE (result) == CALL_EXPR)
2175 while ((cp->h.code & EVIL_CODE) == 0)
2177 /* non-standard uses: set the field to 0 to indicate
2178 we are using a non-member function. */
2180 if (cp->harshness[len].distance == 0
2181 && cp->h.code < best)
2190 /* We have a hit (of sorts). If the parameter list is
2191 "error_mark_node", or some variant thereof, it won't
2192 match any methods. Since we have verified that the is
2193 some method vaguely matching this one (in name at least),
2196 Don't stop for friends, however. */
2197 basetype_path = TREE_PURPOSE (baselink);
2199 function = TREE_VALUE (baselink);
2200 if (TREE_CODE (basetype_path) == TREE_LIST)
2201 basetype_path = TREE_VALUE (basetype_path);
2202 basetype = BINFO_TYPE (basetype_path);
2204 for (; function; function = DECL_CHAIN (function))
2206 #ifdef GATHER_STATISTICS
2207 n_inner_fields_searched++;
2211 found_fns = tree_cons (NULL_TREE, function, found_fns);
2213 /* Not looking for friends here. */
2214 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
2215 && ! DECL_STATIC_FUNCTION_P (function))
2220 tree these_parms = parms;
2222 #ifdef GATHER_STATISTICS
2223 n_inner_fields_searched++;
2226 cp->harshness = (struct harshness_code *)
2227 alloca ((len + 1) * sizeof (struct harshness_code));
2229 if (DECL_STATIC_FUNCTION_P (function))
2230 these_parms = TREE_CHAIN (these_parms);
2231 compute_conversion_costs (function, these_parms, cp, len);
2233 if ((cp->h.code & EVIL_CODE) == 0)
2235 cp->u.field = function;
2236 cp->function = function;
2237 cp->basetypes = basetype_path;
2239 /* Don't allow non-converting constructors to convert. */
2240 if (flags & LOOKUP_ONLYCONVERTING
2241 && DECL_LANG_SPECIFIC (function)
2242 && DECL_NONCONVERTING_P (function))
2245 /* No "two-level" conversions. */
2246 if (flags & LOOKUP_NO_CONVERSION
2247 && (cp->h.code & USER_CODE))
2258 tree igv = lookup_name_nonclass (name);
2260 /* No exact match could be found. Now try to find match
2261 using default conversions. */
2262 if ((flags & LOOKUP_GLOBAL) && igv)
2264 if (TREE_CODE (igv) == FUNCTION_DECL)
2266 else if (TREE_CODE (igv) == TREE_LIST)
2267 ever_seen += count_functions (igv);
2272 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2273 == LOOKUP_SPECULATIVELY)
2276 TREE_CHAIN (last) = void_list_node;
2277 if (flags & LOOKUP_GLOBAL)
2278 cp_error ("no global or member function `%D(%A)' defined",
2279 save_name, parmtypes);
2281 cp_error ("no member function `%T::%D(%A)' defined",
2282 save_basetype, save_name, TREE_CHAIN (parmtypes));
2283 return error_mark_node;
2288 if (cp - candidates != 0)
2290 /* Rank from worst to best. Then cp will point to best one.
2291 Private fields have their bits flipped. For unsigned
2292 numbers, this should make them look very large.
2293 If the best alternate has a (signed) negative value,
2294 then all we ever saw were private members. */
2295 if (cp - candidates > 1)
2297 int n_candidates = cp - candidates;
2298 extern int warn_synth;
2299 TREE_VALUE (parms) = instance_ptr;
2300 cp = ideal_candidate (candidates, n_candidates, len);
2301 if (cp == (struct candidate *)0)
2303 if (flags & LOOKUP_COMPLAIN)
2305 TREE_CHAIN (last) = void_list_node;
2306 cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2307 name_kind, save_name, TREE_CHAIN (parmtypes));
2308 print_n_candidates (candidates, n_candidates);
2310 return error_mark_node;
2312 if (cp->h.code & EVIL_CODE)
2313 return error_mark_node;
2315 && DECL_NAME (cp->function) == ansi_opname[MODIFY_EXPR]
2316 && DECL_ARTIFICIAL (cp->function)
2317 && n_candidates == 2)
2319 cp_warning ("using synthesized `%#D' for copy assignment",
2321 cp_warning_at (" where cfront would use `%#D'",
2322 candidates->function);
2325 else if (cp[-1].h.code & EVIL_CODE)
2327 if (flags & LOOKUP_COMPLAIN)
2328 cp_error ("ambiguous type conversion requested for %s `%D'",
2329 name_kind, save_name);
2330 return error_mark_node;
2335 /* The global function was the best, so use it. */
2336 if (cp->u.field == 0)
2338 /* We must convert the instance pointer into a reference type.
2339 Global overloaded functions can only either take
2340 aggregate objects (which come for free from references)
2341 or reference data types anyway. */
2342 TREE_VALUE (parms) = copy_node (instance_ptr);
2343 TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
2344 return build_function_call (cp->function, parms);
2347 function = cp->function;
2348 basetype_path = cp->basetypes;
2349 if (! DECL_STATIC_FUNCTION_P (function))
2350 TREE_VALUE (parms) = cp->arg;
2351 goto found_and_maybe_warn;
2354 if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
2356 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2357 == LOOKUP_SPECULATIVELY)
2360 if (DECL_STATIC_FUNCTION_P (cp->function))
2361 parms = TREE_CHAIN (parms);
2364 if (flags & LOOKUP_SPECULATIVELY)
2366 if (static_call_context
2367 && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
2368 cp_error ("object missing in call to `%D'", cp->function);
2369 else if (ever_seen > 1)
2371 TREE_CHAIN (last) = void_list_node;
2372 cp_error ("no matching function for call to `%T::%D (%A)%V'",
2373 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr))),
2374 save_name, TREE_CHAIN (parmtypes),
2375 TREE_TYPE (TREE_TYPE (instance_ptr)));
2376 TREE_CHAIN (last) = NULL_TREE;
2377 print_candidates (found_fns);
2380 report_type_mismatch (cp, parms, name_kind);
2381 return error_mark_node;
2384 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2387 cp_error ("%T has no method named %D", save_basetype, save_name);
2388 return error_mark_node;
2394 found_and_maybe_warn:
2395 if ((cp->harshness[0].code & CONST_CODE)
2396 /* 12.1p2: Constructors can be called for const objects. */
2397 && ! DECL_CONSTRUCTOR_P (cp->function))
2399 if (flags & LOOKUP_COMPLAIN)
2401 cp_error_at ("non-const member function `%D'", cp->function);
2402 error ("called for const object at this point in file");
2404 /* Not good enough for a match. */
2406 return error_mark_node;
2410 /* Silently return error_mark_node. */
2411 return error_mark_node;
2414 if (flags & LOOKUP_PROTECT)
2415 access = compute_access (basetype_path, function);
2417 if (access == access_private_node)
2419 if (flags & LOOKUP_COMPLAIN)
2421 cp_error_at ("%s `%+#D' is %s", name_kind, function,
2422 TREE_PRIVATE (function) ? "private"
2423 : "from private base class");
2424 error ("within this context");
2426 return error_mark_node;
2428 else if (access == access_protected_node)
2430 if (flags & LOOKUP_COMPLAIN)
2432 cp_error_at ("%s `%+#D' %s", name_kind, function,
2433 TREE_PROTECTED (function) ? "is protected"
2434 : "has protected accessibility");
2435 error ("within this context");
2437 return error_mark_node;
2440 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2441 type (if it exists) is a pointer to. */
2443 if (DECL_ABSTRACT_VIRTUAL_P (function)
2444 && instance == current_class_ref
2445 && DECL_CONSTRUCTOR_P (current_function_decl)
2446 && ! (flags & LOOKUP_NONVIRTUAL)
2447 && value_member (function, get_abstract_virtuals (basetype)))
2448 cp_error ("abstract virtual `%#D' called from constructor", function);
2450 if (IS_SIGNATURE (basetype))
2452 if (static_call_context)
2454 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2455 basetype, save_name);
2456 return error_mark_node;
2458 return build_signature_method_call (basetype, instance, function, parms);
2461 function = DECL_MAIN_VARIANT (function);
2462 mark_used (function);
2464 /* Is it a synthesized method that needs to be synthesized? */
2465 if (DECL_ARTIFICIAL (function) && ! DECL_INITIAL (function)
2466 /* Kludge: don't synthesize for default args. */
2467 && current_function_decl)
2468 synthesize_method (function);
2470 if (pedantic && DECL_THIS_INLINE (function) && ! DECL_ARTIFICIAL (function)
2471 && ! DECL_INITIAL (function) && ! DECL_PENDING_INLINE_INFO (function)
2472 && ! (DECL_TEMPLATE_INFO (function)
2473 && TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (function))))
2474 cp_warning ("inline function `%#D' called before definition", function);
2476 fntype = TREE_TYPE (function);
2477 if (TREE_CODE (fntype) == POINTER_TYPE)
2478 fntype = TREE_TYPE (fntype);
2479 basetype = DECL_CLASS_CONTEXT (function);
2481 /* If we are referencing a virtual function from an object
2482 of effectively static type, then there is no need
2483 to go through the virtual function table. */
2484 if (need_vtbl == maybe_needed)
2486 int fixed_type = resolves_to_fixed_type_p (instance, 0);
2488 if (all_virtual == 1
2489 && DECL_VINDEX (function)
2490 && may_be_remote (basetype))
2492 else if (DECL_VINDEX (function))
2493 need_vtbl = fixed_type ? unneeded : needed;
2495 need_vtbl = not_needed;
2498 if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
2499 && !DECL_CONSTRUCTOR_P (function))
2501 /* Let's be nasty to the user now, and give reasonable
2503 instance_ptr = current_class_ptr;
2506 if (basetype != current_class_type)
2508 if (basetype == error_mark_node)
2509 return error_mark_node;
2512 if (orig_basetype != NULL_TREE)
2513 error_not_base_type (orig_basetype, current_class_type);
2515 error_not_base_type (function, current_class_type);
2516 return error_mark_node;
2520 /* Only allow a static member function to call another static member
2522 else if (DECL_LANG_SPECIFIC (function)
2523 && !DECL_STATIC_FUNCTION_P (function))
2525 cp_error ("cannot call member function `%D' without object",
2527 return error_mark_node;
2531 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2533 if (TYPE_SIZE (complete_type (value_type)) == 0)
2535 if (flags & LOOKUP_COMPLAIN)
2536 incomplete_type_error (0, value_type);
2537 return error_mark_node;
2540 if (DECL_STATIC_FUNCTION_P (function))
2541 parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2542 TREE_CHAIN (parms), function, LOOKUP_NORMAL);
2543 else if (need_vtbl == unneeded)
2545 int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
2546 basetype = TREE_TYPE (instance);
2547 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function))
2548 != TYPE_MAIN_VARIANT (basetype))
2550 basetype = DECL_CLASS_CONTEXT (function);
2551 instance_ptr = convert_pointer_to (basetype, instance_ptr);
2552 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2554 parms = tree_cons (NULL_TREE, instance_ptr,
2555 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, sub_flags));
2559 if ((flags & LOOKUP_NONVIRTUAL) == 0)
2560 basetype = DECL_CONTEXT (function);
2562 /* First parm could be integer_zerop with casts like
2563 ((Object*)0)->Object::IsA() */
2564 if (!integer_zerop (TREE_VALUE (parms)))
2566 /* Since we can't have inheritance with a union, doing get_binfo
2567 on it won't work. We do all the convert_pointer_to_real
2568 stuff to handle MI correctly...for unions, that's not
2569 an issue, so we must short-circuit that extra work here. */
2570 tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
2571 if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
2572 instance_ptr = TREE_VALUE (parms);
2575 tree binfo = get_binfo (basetype,
2576 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
2578 instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
2581 = convert_pointer_to (build_type_variant (basetype,
2585 if (TREE_CODE (instance_ptr) == COND_EXPR)
2587 instance_ptr = save_expr (instance_ptr);
2588 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2590 else if (TREE_CODE (instance_ptr) == NOP_EXPR
2591 && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
2592 && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
2594 /* The call to `convert_pointer_to' may return error_mark_node. */
2595 else if (TREE_CODE (instance_ptr) == ERROR_MARK)
2596 return instance_ptr;
2597 else if (instance == NULL_TREE
2598 || TREE_CODE (instance) != INDIRECT_REF
2599 || TREE_OPERAND (instance, 0) != instance_ptr)
2600 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2602 parms = tree_cons (NULL_TREE, instance_ptr,
2603 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, LOOKUP_NORMAL));
2606 if (parms == error_mark_node
2607 || (parms && TREE_CHAIN (parms) == error_mark_node))
2608 return error_mark_node;
2610 if (need_vtbl == needed)
2612 function = build_vfn_ref (&TREE_VALUE (parms), instance,
2613 DECL_VINDEX (function));
2614 TREE_TYPE (function) = build_pointer_type (fntype);
2617 if (TREE_CODE (function) == FUNCTION_DECL)
2618 GNU_xref_call (current_function_decl,
2619 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
2621 result = build_call (function, value_type, parms);
2622 result = convert_from_reference (result);
2626 /* Similar to `build_method_call', but for overloaded non-member functions.
2627 The name of this function comes through NAME. The name depends
2630 Note that this function must handle simple `C' promotions,
2631 as well as variable numbers of arguments (...), and
2632 default arguments to boot.
2634 If the overloading is successful, we return a tree node which
2635 contains the call to the function.
2637 If overloading produces candidates which are probable, but not definite,
2638 we hold these candidates. If FINAL_CP is non-zero, then we are free
2639 to assume that final_cp points to enough storage for all candidates that
2640 this function might generate. The `harshness' array is preallocated for
2641 the first candidate, but not for subsequent ones.
2643 Note that the DECL_RTL of FUNCTION must be made to agree with this
2644 function's new name. */
2647 build_overload_call_real (fnname, parms, flags, final_cp, require_complete)
2650 struct candidate *final_cp;
2651 int require_complete;
2653 /* must check for overloading here */
2654 tree functions, function, parm;
2655 tree parmtypes, last;
2656 register tree outer;
2658 int parmlength = list_length (parms);
2660 struct candidate *candidates, *cp;
2664 final_cp[0].h.code = 0;
2665 final_cp[0].h.distance = 0;
2666 final_cp[0].function = 0;
2668 final_cp[1].h.code = EVIL_CODE;
2671 parmtypes = default_parm_conversions (parms, &last);
2672 if (parmtypes == error_mark_node)
2675 final_cp->h.code = EVIL_CODE;
2676 return error_mark_node;
2680 TREE_CHAIN (last) = void_list_node;
2682 parmtypes = void_list_node;
2684 if (is_overloaded_fn (fnname))
2687 if (TREE_CODE (fnname) == TREE_LIST)
2688 fnname = TREE_PURPOSE (functions);
2689 else if (TREE_CODE (fnname) == FUNCTION_DECL)
2690 fnname = DECL_NAME (functions);
2693 functions = lookup_name_nonclass (fnname);
2695 if (functions == NULL_TREE)
2697 if (flags & LOOKUP_SPECULATIVELY)
2699 if (flags & LOOKUP_COMPLAIN)
2700 error ("only member functions apply");
2702 final_cp->h.code = EVIL_CODE;
2703 return error_mark_node;
2706 if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
2708 functions = DECL_MAIN_VARIANT (functions);
2711 /* We are just curious whether this is a viable alternative or
2713 compute_conversion_costs (functions, parms, final_cp, parmlength);
2717 return build_function_call_real (functions, parms, 1, flags);
2720 if (TREE_CODE (functions) == TREE_LIST
2721 && TREE_VALUE (functions) == NULL_TREE)
2723 if (flags & LOOKUP_SPECULATIVELY)
2726 if (flags & LOOKUP_COMPLAIN)
2727 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2728 TREE_PURPOSE (functions));
2730 final_cp->h.code = EVIL_CODE;
2731 return error_mark_node;
2734 length = count_functions (functions);
2737 candidates = final_cp;
2741 = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
2742 bzero ((char *) candidates, (length + 1) * sizeof (struct candidate));
2747 my_friendly_assert (is_overloaded_fn (functions), 169);
2749 functions = get_first_fn (functions);
2751 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2752 for (outer = functions; outer; outer = DECL_CHAIN (outer))
2754 int template_cost = 0;
2756 if (TREE_CODE (function) != FUNCTION_DECL
2757 && ! (TREE_CODE (function) == TEMPLATE_DECL
2758 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
2760 enum tree_code code = TREE_CODE (function);
2761 if (code == TEMPLATE_DECL)
2762 code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
2763 if (code == CONST_DECL)
2765 ("enumeral value `%D' conflicts with function of same name",
2767 else if (code == VAR_DECL)
2769 if (TREE_STATIC (function))
2771 ("variable `%D' conflicts with function of same name",
2775 ("constant field `%D' conflicts with function of same name",
2778 else if (code == TYPE_DECL)
2781 my_friendly_abort (2);
2782 error ("at this point in file");
2785 if (TREE_CODE (function) == TEMPLATE_DECL)
2787 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
2788 tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
2791 i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
2792 TYPE_ARG_TYPES (TREE_TYPE (function)),
2793 parms, &template_cost, 0);
2796 function = instantiate_template (function, targs);
2797 if (function == error_mark_node)
2802 if (TREE_CODE (function) == TEMPLATE_DECL)
2804 /* Unconverted template -- failed match. */
2805 cp->function = function;
2807 cp->h.code = EVIL_CODE;
2811 struct candidate *cp2;
2813 /* Check that this decl is not the same as a function that's in
2814 the list due to some template instantiation. */
2817 if (cp2->function == function)
2821 if (cp2->function == function)
2824 function = DECL_MAIN_VARIANT (function);
2826 /* Can't use alloca here, since result might be
2827 passed to calling function. */
2828 cp->h_len = parmlength;
2829 cp->harshness = (struct harshness_code *)
2830 oballoc ((parmlength + 1) * sizeof (struct harshness_code));
2832 compute_conversion_costs (function, parms, cp, parmlength);
2834 /* Make sure this is clear as well. */
2835 cp->h.int_penalty += template_cost;
2837 if ((cp[0].h.code & EVIL_CODE) == 0)
2839 cp[1].h.code = EVIL_CODE;
2845 if (cp - candidates)
2847 tree rval = error_mark_node;
2850 cp[0].h.code = EVIL_CODE;
2851 if (cp - candidates > 1)
2853 struct candidate *best_cp
2854 = ideal_candidate (candidates, cp - candidates, parmlength);
2855 if (best_cp == (struct candidate *)0)
2857 if (flags & LOOKUP_COMPLAIN)
2859 cp_error ("call of overloaded `%D' is ambiguous", fnname);
2860 print_n_candidates (candidates, cp - candidates);
2862 return error_mark_node;
2865 rval = best_cp->function;
2870 if (cp->h.code & EVIL_CODE)
2872 if (flags & LOOKUP_COMPLAIN)
2873 error ("type conversion ambiguous");
2876 rval = cp->function;
2882 return build_function_call_real (rval, parms, require_complete, flags);
2885 if (flags & LOOKUP_SPECULATIVELY)
2888 if (flags & LOOKUP_COMPLAIN)
2889 report_type_mismatch (cp, parms, "function",
2890 decl_as_string (cp->function, 1));
2892 return error_mark_node;
2895 /* This requires a complete type on the result of the call. */
2898 build_overload_call (fnname, parms, flags)
2902 return build_overload_call_real (fnname, parms, flags, (struct candidate *)0, 1);