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)
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. */
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
38 extern int inhibit_warnings;
39 extern int flag_assume_nonnull_objects;
40 extern tree ctor_label, dtor_label;
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 int convert_harshness_old ();
48 static struct harshness_code convert_harshness_ansi ();
51 /* Note the old method also uses USER_HARSHNESS, BASE_DERIVED_HARSHNESS,
55 #define EVIL_HARSHNESS(ARG) ((ARG) & 1)
56 #define ELLIPSIS_HARSHNESS(ARG) ((ARG) & 2)
57 #define CONTRAVARIANT_HARSHNESS(ARG) ((ARG) & 8)
58 #define INT_TO_BD_HARSHNESS(ARG) (((ARG) << 5) | 16)
59 #define INT_FROM_BD_HARSHNESS(ARG) ((ARG) >> 5)
60 #define INT_TO_EASY_HARSHNESS(ARG) ((ARG) << 5)
61 #define INT_FROM_EASY_HARSHNESS(ARG) ((ARG) >> 5)
62 #define ONLY_EASY_HARSHNESS(ARG) (((ARG) & 31) == 0)
66 #define EVIL_RETURN(ARG) ((ARG).code = EVIL_CODE, (ARG))
67 #define QUAL_RETURN(ARG) ((ARG).code = QUAL_CODE, (ARG))
68 #define TRIVIAL_RETURN(ARG) ((ARG).code = TRIVIAL_CODE, (ARG))
69 #define ZERO_RETURN(ARG) ((ARG).code = 0, (ARG))
71 #define USER_HARSHNESS(ARG) ((ARG) & 4)
72 #define BASE_DERIVED_HARSHNESS(ARG) ((ARG) & 16)
73 #define CONST_HARSHNESS(ARG) ((ARG) & 2048)
75 /* Ordering function for overload resolution. Compare two candidates
78 rank_for_overload_ansi (x, y)
79 struct candidate *x, *y;
81 if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
82 return y->h.code - x->h.code;
83 if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
86 /* This is set by compute_conversion_costs, for calling a non-const
87 member function from a const member function. */
88 if ((y->v.ansi_harshness[0].code & CONST_CODE) ^ (x->v.ansi_harshness[0].code & CONST_CODE))
89 return y->v.ansi_harshness[0].code - x->v.ansi_harshness[0].code;
91 if (y->h.code & STD_CODE)
93 if (x->h.code & STD_CODE)
94 return y->h.distance - x->h.distance;
97 if (x->h.code & STD_CODE)
100 return y->h.code - x->h.code;
104 rank_for_overload_old (x, y)
105 struct candidate *x, *y;
107 if (y->evil - x->evil)
108 return y->evil - x->evil;
109 if (CONST_HARSHNESS (y->v.old_harshness[0]) ^ CONST_HARSHNESS (x->v.old_harshness[0]))
110 return y->v.old_harshness[0] - x->v.old_harshness[0];
111 if (y->ellipsis - x->ellipsis)
112 return y->ellipsis - x->ellipsis;
113 if (y->user - x->user)
114 return y->user - x->user;
115 if (y->b_or_d - x->b_or_d)
116 return y->b_or_d - x->b_or_d;
117 return y->easy - x->easy;
121 rank_for_overload (x, y)
122 struct candidate *x, *y;
124 if (flag_ansi_overloading)
125 return rank_for_overload_ansi (x, y);
127 return rank_for_overload_old (x, y);
130 /* Compare two candidates, argument by argument. */
132 rank_for_ideal (x, y)
133 struct candidate *x, *y;
137 if (x->h_len != y->h_len)
140 for (i = 0; i < x->h_len; i++)
142 if (y->v.ansi_harshness[i].code - x->v.ansi_harshness[i].code)
143 return y->v.ansi_harshness[i].code - x->v.ansi_harshness[i].code;
144 if ((y->v.ansi_harshness[i].code & STD_CODE)
145 && (y->v.ansi_harshness[i].distance - x->v.ansi_harshness[i].distance))
146 return y->v.ansi_harshness[i].distance - x->v.ansi_harshness[i].distance;
148 /* They're both the same code. Now see if we're dealing with an
149 integral promotion that needs a finer grain of accuracy. */
150 if (y->v.ansi_harshness[0].code & PROMO_CODE
151 && (y->v.ansi_harshness[i].int_penalty ^ x->v.ansi_harshness[i].int_penalty))
152 return y->v.ansi_harshness[i].int_penalty - x->v.ansi_harshness[i].int_penalty;
157 /* TYPE is the type we wish to convert to. PARM is the parameter
158 we have to work with. We use a somewhat arbitrary cost function
159 to measure this conversion. */
160 static struct harshness_code
161 convert_harshness_ansi (type, parmtype, parm)
162 register tree type, parmtype;
165 struct harshness_code h;
166 register enum tree_code codel;
167 register enum tree_code coder;
173 #ifdef GATHER_STATISTICS
174 n_convert_harshness++;
177 if (TYPE_PTRMEMFUNC_P (type))
178 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
179 if (TYPE_PTRMEMFUNC_P (parmtype))
180 parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
182 codel = TREE_CODE (type);
183 coder = TREE_CODE (parmtype);
185 if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
186 return ZERO_RETURN (h);
188 if (coder == ERROR_MARK)
189 return EVIL_RETURN (h);
191 if (codel == POINTER_TYPE && fntype_p (parmtype))
194 struct harshness_code h1, h2;
196 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
197 type = TREE_TYPE (type);
199 if (coder == POINTER_TYPE)
201 parmtype = TREE_TYPE (parmtype);
202 coder = TREE_CODE (parmtype);
205 if (coder != TREE_CODE (type))
206 return EVIL_RETURN (h);
208 /* We allow the default conversion between function type
209 and pointer-to-function type for free. */
210 if (type == parmtype)
211 return ZERO_RETURN (h);
213 /* Compare return types. */
214 p1 = TREE_TYPE (type);
215 p2 = TREE_TYPE (parmtype);
216 h2 = convert_harshness_ansi (p1, p2, NULL_TREE);
217 if (h2.code & EVIL_CODE)
220 h1.code = TRIVIAL_CODE;
223 if (h2.distance != 0)
227 /* This only works for pointers. */
228 if (TREE_CODE (p1) != POINTER_TYPE
229 && TREE_CODE (p1) != REFERENCE_TYPE)
230 return EVIL_RETURN (h);
234 /* Don't die if we happen to be dealing with void*. */
235 if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
236 return EVIL_RETURN (h);
238 binfo = get_binfo (p2, p1, 0);
240 binfo = get_binfo (p1, p2, 0);
242 if (! BINFO_OFFSET_ZEROP (binfo))
244 static int explained = 0;
246 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p2, p1);
248 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p1, p2);
251 sorry ("(because pointer values change during conversion)");
252 return EVIL_RETURN (h);
257 if (h2.distance > h1.distance)
258 h1.distance = h2.distance;
260 p1 = TYPE_ARG_TYPES (type);
261 p2 = TYPE_ARG_TYPES (parmtype);
262 while (p1 && TREE_VALUE (p1) != void_type_node
263 && p2 && TREE_VALUE (p2) != void_type_node)
265 h2 = convert_harshness_ansi (TREE_VALUE (p1), TREE_VALUE (p2),
267 if (h2.code & EVIL_CODE)
272 /* This only works for pointers and references. */
273 if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
274 && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
275 return EVIL_RETURN (h);
276 h2.distance = - h2.distance;
280 if (h2.distance > h1.distance)
281 h1.distance = h2.distance;
282 p1 = TREE_CHAIN (p1);
283 p2 = TREE_CHAIN (p2);
290 return EVIL_RETURN (h);
291 h1.code |= ELLIPSIS_CODE;
296 if (TREE_PURPOSE (p1) == NULL_TREE)
297 h1.code |= EVIL_CODE;
301 else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
303 /* Get to the OFFSET_TYPE that this might be. */
304 type = TREE_TYPE (type);
306 if (coder != TREE_CODE (type))
307 return EVIL_RETURN (h);
309 if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
311 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (type),
312 TYPE_OFFSET_BASETYPE (parmtype)))
317 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (parmtype),
318 TYPE_OFFSET_BASETYPE (type)))
324 return EVIL_RETURN (h);
325 /* Now test the OFFSET_TYPE's target compatibility. */
326 type = TREE_TYPE (type);
327 parmtype = TREE_TYPE (parmtype);
330 if (coder == UNKNOWN_TYPE)
332 if (codel == FUNCTION_TYPE
333 || codel == METHOD_TYPE
334 || (codel == POINTER_TYPE
335 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
336 || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
337 return TRIVIAL_RETURN (h);
338 return EVIL_RETURN (h);
341 if (coder == VOID_TYPE)
342 return EVIL_RETURN (h);
344 if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
346 /* Control equivalence of ints an enums. */
348 if (codel == ENUMERAL_TYPE
349 && flag_int_enum_equivalence == 0)
351 /* Enums can be converted to ints, but not vice-versa. */
352 if (coder != ENUMERAL_TYPE
353 || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
354 return EVIL_RETURN (h);
357 /* else enums and ints (almost) freely interconvert. */
359 if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
361 if ((TREE_UNSIGNED (type) ^ TREE_UNSIGNED (parmtype))
363 || TYPE_MODE (type) != TYPE_MODE (parmtype))
365 /* Make sure a value-preserving condition [from a smaller type to
366 a larger type] is preferred to a possibly value-destroying
367 standard conversion [from a larger type to a smaller type]. */
368 if (TYPE_PRECISION (type) >= TYPE_PRECISION (parmtype))
371 /* A char, short, wchar_t, etc., should promote to an int if
372 it can handle it, otherwise to an unsigned. So we'll make
374 if (type != integer_type_node)
381 /* If the three above conditions didn't trigger, we have found two
382 very similar types. On systems where they're the same size, we
383 can end up here with TYPE as `long' and PARMTYPE as `int'. Make
384 sure we realize that, even though they're the same mode, we will
385 have to do some sort of integral promotion on the type, since
386 they're not the same. */
387 if (! comptypes (type, parmtype, 1) && h.code == 0)
389 /* This call to common_type will return the best type for the
390 combination. If it matches TYPE, that means we'll be converting
391 from a so-called smaller type (in PARMTYPE) to the larger in TYPE,
392 thus an integral promotion. Otherwise, it must be going from a
393 larger type in PARMTYPE to a smaller expected type in TYPE, so we
394 make it a standard conversion instead. */
395 if (common_type (type, parmtype) == type)
403 else if (coder == REAL_TYPE)
411 if (codel == REAL_TYPE)
413 if (coder == REAL_TYPE)
415 /* Shun converting among float, double, and long double if a
420 else if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
428 /* Convert arrays which have not previously been converted. */
429 if (codel == ARRAY_TYPE)
430 codel = POINTER_TYPE;
431 if (coder == ARRAY_TYPE)
432 coder = POINTER_TYPE;
434 /* Conversions among pointers */
435 if (codel == POINTER_TYPE && coder == POINTER_TYPE)
437 register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
438 register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
439 int penalty = 4 * (ttl != ttr);
441 /* Anything converts to void *. void * converts to anything.
442 Since these may be `const void *' (etc.) use VOID_TYPE
443 instead of void_type_node. Otherwise, the targets must be the same,
444 except that we do allow (at some cost) conversion between signed and
445 unsigned pointer types. */
447 if ((TREE_CODE (ttl) == METHOD_TYPE
448 || TREE_CODE (ttl) == FUNCTION_TYPE)
449 && TREE_CODE (ttl) == TREE_CODE (ttr))
451 if (comptypes (ttl, ttr, -1))
453 h.code = penalty ? STD_CODE : 0;
462 if (TREE_CODE (ttl) != VOID_TYPE && TREE_CODE (ttr) != VOID_TYPE)
464 if (TREE_UNSIGNED (ttl) != TREE_UNSIGNED (ttr))
466 ttl = unsigned_type (ttl);
467 ttr = unsigned_type (ttr);
470 if (! comp_target_types (ttl, ttr, 0))
471 return EVIL_RETURN (h);
474 if (!(TREE_CODE (ttl) == VOID_TYPE
475 || TREE_CODE (ttr) == VOID_TYPE
476 || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
477 && (ttl = unsigned_type (ttl),
478 ttr = unsigned_type (ttr),
480 || (comp_target_types (ttl, ttr, 0))))
481 return EVIL_RETURN (h);
484 if (penalty == 10 || ttr == ttl)
486 tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
488 /* If one was unsigned but the other wasn't, then we need to
489 do a standard conversion from T to unsigned T. */
491 h.code = PROMO_CODE; /* was STD_CODE */
495 /* Note conversion from `T*' to `const T*',
496 or `T*' to `volatile T*'. */
498 && ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
499 || (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2))))
507 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
509 int b_or_d = get_base_distance (ttl, ttr, 0, 0);
512 b_or_d = get_base_distance (ttr, ttl, 0, 0);
514 return EVIL_RETURN (h);
515 h.distance = -b_or_d;
523 /* If converting from a `class*' to a `void*', make it
524 less favorable than any inheritance relationship. */
525 if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
528 h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
531 h.code = penalty ? STD_CODE : PROMO_CODE;
535 if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
537 /* This is not a bad match, but don't let it beat
538 integer-enum combinations. */
539 if (parm && integer_zerop (parm))
547 /* C++: one of the types must be a reference type. */
550 register tree intype = TYPE_MAIN_VARIANT (parmtype);
551 register enum tree_code form = TREE_CODE (intype);
554 if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
556 ttl = TYPE_MAIN_VARIANT (type);
558 if (codel == REFERENCE_TYPE)
560 ttl = TREE_TYPE (ttl);
562 /* When passing a non-const argument into a const reference,
563 dig it a little, so a non-const reference is preferred over
565 if (parm && TREE_READONLY (ttl) && ! TREE_READONLY (parm))
570 ttl = TYPE_MAIN_VARIANT (ttl);
572 if (form == OFFSET_TYPE)
574 intype = TREE_TYPE (intype);
575 form = TREE_CODE (intype);
578 if (form == REFERENCE_TYPE)
580 intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
583 return ZERO_RETURN (h);
588 /* Can reference be built up? */
589 if (ttl == intype && penalty == 0) {
590 /* Because the READONLY and VIRTUAL bits are not always in
591 the type, this extra check is necessary. The problem
592 should be fixed someplace else, and this extra code
595 Also, if type if a reference, the readonly bits could
596 either be in the outer type (with reference) or on the
597 inner type (the thing being referenced). (mrs) */
599 && ((TREE_READONLY (parm)
600 && ! (TYPE_READONLY (type)
601 || (TREE_CODE (type) == REFERENCE_TYPE
602 && TYPE_READONLY (TREE_TYPE (type)))))
603 || (TREE_SIDE_EFFECTS (parm)
604 && ! (TYPE_VOLATILE (type)
605 || (TREE_CODE (type) == REFERENCE_TYPE
606 && TYPE_VOLATILE (TREE_TYPE (type)))))))
609 return ZERO_RETURN (h);
615 else if (form == REFERENCE_TYPE)
619 tree tmp = convert_from_reference (parm);
620 intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
626 intype = TREE_TYPE (intype);
627 while (TREE_CODE (intype) == REFERENCE_TYPE);
628 intype = TYPE_MAIN_VARIANT (intype);
632 return ZERO_RETURN (h);
637 if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
639 ttl = unsigned_type (ttl);
640 intype = unsigned_type (intype);
646 /* If the initializer is not an lvalue, then it does not
647 matter if we make life easier for the programmer
648 by creating a temporary variable with which to
650 if (parm && (coder == INTEGER_TYPE
651 || coder == ENUMERAL_TYPE
652 || coder == REAL_TYPE)
653 && ! lvalue_p (parm))
655 h = convert_harshness_ansi (ttl, ttr, NULL_TREE);
656 if (penalty > 2 || h.code != 0)
659 h.code |= TRIVIAL_CODE;
673 h.code = TRIVIAL_CODE;
674 /* We set this here so that build_overload_call_real will be
675 able to see the penalty we found, rather than just looking
676 at a TRIVIAL_CODE with no other information. */
677 h.int_penalty = penalty;
682 /* Pointers to voids always convert for pointers. But
683 make them less natural than more specific matches. */
684 if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
686 if (TREE_TYPE (ttl) == void_type_node
687 || TREE_TYPE (ttr) == void_type_node)
695 if (parm && codel != REFERENCE_TYPE)
697 h = convert_harshness_ansi (ttl, ttr, NULL_TREE);
704 /* Here it does matter. If this conversion is from derived to base,
705 allow it. Otherwise, types must be compatible in the strong sense. */
706 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
708 int b_or_d = get_base_distance (ttl, ttr, 0, 0);
711 b_or_d = get_base_distance (ttr, ttl, 0, 0);
713 return EVIL_RETURN (h);
714 h.distance = -b_or_d;
716 /* Say that this conversion is relatively painless.
717 If it turns out that there is a user-defined X(X&)
718 constructor, then that will be invoked, but that's
719 preferable to dealing with other user-defined conversions
720 that may produce surprising results. */
727 if (comp_target_types (ttl, intype, 1))
736 if (codel == RECORD_TYPE && coder == RECORD_TYPE)
738 int b_or_d = get_base_distance (type, parmtype, 0, 0);
741 b_or_d = get_base_distance (parmtype, type, 0, 0);
743 return EVIL_RETURN (h);
744 h.distance = -b_or_d;
751 return EVIL_RETURN (h);
754 /* TYPE is the type we wish to convert to. PARM is the parameter
755 we have to work with. We use a somewhat arbitrary cost function
756 to measure this conversion. */
758 convert_harshness_old (type, parmtype, parm)
759 register tree type, parmtype;
762 register enum tree_code codel;
763 register enum tree_code coder;
765 #ifdef GATHER_STATISTICS
766 n_convert_harshness++;
769 if (TYPE_PTRMEMFUNC_P (type))
770 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
771 if (TYPE_PTRMEMFUNC_P (parmtype))
772 parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
774 codel = TREE_CODE (type);
775 coder = TREE_CODE (parmtype);
777 if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
780 if (coder == ERROR_MARK)
783 if (codel == POINTER_TYPE && fntype_p (parmtype))
786 int harshness, new_harshness;
788 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
789 type = TREE_TYPE (type);
791 if (coder == POINTER_TYPE)
793 parmtype = TREE_TYPE (parmtype);
794 coder = TREE_CODE (parmtype);
797 if (coder != TREE_CODE (type))
802 /* We allow the default conversion between function type
803 and pointer-to-function type for free. */
804 if (type == parmtype)
807 /* Compare return types. */
808 p1 = TREE_TYPE (type);
809 p2 = TREE_TYPE (parmtype);
810 new_harshness = convert_harshness_old (p1, p2, NULL_TREE);
811 if (EVIL_HARSHNESS (new_harshness))
814 if (BASE_DERIVED_HARSHNESS (new_harshness))
818 /* This only works for pointers. */
819 if (TREE_CODE (p1) != POINTER_TYPE
820 && TREE_CODE (p1) != REFERENCE_TYPE)
825 /* Don't die if we happen to be dealing with void*. */
826 if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
828 if (CONTRAVARIANT_HARSHNESS (new_harshness))
829 binfo = get_binfo (p2, p1, 0);
831 binfo = get_binfo (p1, p2, 0);
833 if (! BINFO_OFFSET_ZEROP (binfo))
835 static int explained = 0;
836 if (CONTRAVARIANT_HARSHNESS (new_harshness))
837 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p2, p1);
839 message_2_types (sorry, "cannot cast `%d' to `%d' at function call site", p1, p2);
842 sorry ("(because pointer values change during conversion)");
847 harshness |= new_harshness;
849 p1 = TYPE_ARG_TYPES (type);
850 p2 = TYPE_ARG_TYPES (parmtype);
851 while (p1 && TREE_VALUE (p1) != void_type_node
852 && p2 && TREE_VALUE (p2) != void_type_node)
854 new_harshness = convert_harshness_old (TREE_VALUE (p1),
855 TREE_VALUE (p2), NULL_TREE);
856 if (EVIL_HARSHNESS (new_harshness))
859 if (BASE_DERIVED_HARSHNESS (new_harshness))
861 /* This only works for pointers and references. */
862 if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
863 && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
865 new_harshness ^= CONTRAVARIANT_HARSHNESS (new_harshness);
866 harshness |= new_harshness;
868 /* This trick allows use to accumulate easy type
869 conversions without messing up the bits that encode
870 info about more involved things. */
871 else if (ONLY_EASY_HARSHNESS (new_harshness))
872 harshness += new_harshness;
874 harshness |= new_harshness;
875 p1 = TREE_CHAIN (p1);
876 p2 = TREE_CHAIN (p2);
881 return p1 ? EVIL : (harshness | ELLIPSIS_HARSHNESS (-1));
883 return harshness | (TREE_PURPOSE (p1) == NULL_TREE);
885 else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
887 /* XXX: Note this is set a few times, but it's never actually
891 /* Get to the OFFSET_TYPE that this might be. */
892 type = TREE_TYPE (type);
894 if (coder != TREE_CODE (type))
899 if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
901 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (type),
902 TYPE_OFFSET_BASETYPE (parmtype)))
903 harshness = INT_TO_BD_HARSHNESS (1);
904 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (parmtype),
905 TYPE_OFFSET_BASETYPE (type)))
906 harshness = CONTRAVARIANT_HARSHNESS (-1);
909 /* Now test the OFFSET_TYPE's target compatibility. */
910 type = TREE_TYPE (type);
911 parmtype = TREE_TYPE (parmtype);
914 if (coder == UNKNOWN_TYPE)
916 if (codel == FUNCTION_TYPE
917 || codel == METHOD_TYPE
918 || (codel == POINTER_TYPE
919 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
920 || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
925 if (coder == VOID_TYPE)
928 if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
930 /* Control equivalence of ints an enums. */
932 if (codel == ENUMERAL_TYPE
933 && flag_int_enum_equivalence == 0)
935 /* Enums can be converted to ints, but not vice-versa. */
936 if (coder != ENUMERAL_TYPE
937 || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
941 /* else enums and ints (almost) freely interconvert. */
943 if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
945 int easy = TREE_UNSIGNED (type) ^ TREE_UNSIGNED (parmtype);
948 if (TYPE_MODE (type) != TYPE_MODE (parmtype))
950 return INT_TO_EASY_HARSHNESS (easy);
952 else if (coder == REAL_TYPE)
953 return INT_TO_EASY_HARSHNESS (4);
956 if (codel == REAL_TYPE)
957 if (coder == REAL_TYPE)
958 /* Shun converting between float and double if a choice exists. */
960 if (TYPE_MODE (type) != TYPE_MODE (parmtype))
961 return INT_TO_EASY_HARSHNESS (2);
964 else if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
965 return INT_TO_EASY_HARSHNESS (4);
967 /* convert arrays which have not previously been converted. */
968 if (codel == ARRAY_TYPE)
969 codel = POINTER_TYPE;
970 if (coder == ARRAY_TYPE)
971 coder = POINTER_TYPE;
973 /* Conversions among pointers */
974 if (codel == POINTER_TYPE && coder == POINTER_TYPE)
976 register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
977 register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
978 int penalty = 4 * (ttl != ttr);
979 /* Anything converts to void *. void * converts to anything.
980 Since these may be `const void *' (etc.) use VOID_TYPE
981 instead of void_type_node.
982 Otherwise, the targets must be the same,
983 except that we do allow (at some cost) conversion
984 between signed and unsinged pointer types. */
986 if ((TREE_CODE (ttl) == METHOD_TYPE
987 || TREE_CODE (ttl) == FUNCTION_TYPE)
988 && TREE_CODE (ttl) == TREE_CODE (ttr))
990 if (comptypes (ttl, ttr, -1))
991 return INT_TO_EASY_HARSHNESS (penalty);
995 if (!(TREE_CODE (ttl) == VOID_TYPE
996 || TREE_CODE (ttr) == VOID_TYPE
997 || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
998 && (ttl = unsigned_type (ttl),
999 ttr = unsigned_type (ttr),
1001 || (comp_target_types (ttl, ttr, 0))))
1005 return INT_TO_EASY_HARSHNESS (10);
1007 return INT_TO_BD_HARSHNESS (0);
1009 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
1011 int b_or_d = get_base_distance (ttl, ttr, 0, 0);
1014 b_or_d = get_base_distance (ttr, ttl, 0, 0);
1017 return CONTRAVARIANT_HARSHNESS (-1);
1019 return INT_TO_BD_HARSHNESS (b_or_d);
1021 /* If converting from a `class*' to a `void*', make it
1022 less favorable than any inheritance relationship. */
1023 if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
1024 return INT_TO_BD_HARSHNESS (CLASSTYPE_MAX_DEPTH (ttr)+1);
1025 return INT_TO_EASY_HARSHNESS (penalty);
1028 if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
1030 /* This is not a bad match, but don't let it beat
1031 integer-enum combinations. */
1032 if (parm && integer_zerop (parm))
1033 return INT_TO_EASY_HARSHNESS (4);
1036 /* C++: Since the `this' parameter of a signature member function
1037 is represented as a signature pointer to handle default implementations
1038 correctly, we can have the case that `type' is a signature pointer
1039 while `parmtype' is a pointer to a signature table. We don't really
1040 do any conversions in this case, so just return 0. */
1042 if (codel == RECORD_TYPE && coder == POINTER_TYPE
1043 && IS_SIGNATURE_POINTER (type) && IS_SIGNATURE (TREE_TYPE (parmtype)))
1046 /* C++: one of the types must be a reference type. */
1049 register tree intype = TYPE_MAIN_VARIANT (parmtype);
1050 register enum tree_code form = TREE_CODE (intype);
1053 if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
1055 ttl = TYPE_MAIN_VARIANT (type);
1057 if (codel == REFERENCE_TYPE)
1059 ttl = TREE_TYPE (ttl);
1061 /* When passing a non-const argument into a const reference,
1062 dig it a little, so a non-const reference is preferred over
1064 if (parm && TREE_READONLY (ttl) && ! TREE_READONLY (parm))
1069 ttl = TYPE_MAIN_VARIANT (ttl);
1071 if (form == OFFSET_TYPE)
1073 intype = TREE_TYPE (intype);
1074 form = TREE_CODE (intype);
1077 if (form == REFERENCE_TYPE)
1079 intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
1087 /* Can reference be built up? */
1088 if (ttl == intype && penalty == 0) {
1089 /* Because the READONLY bits and VIRTUAL bits are not always
1090 in the type, this extra check is necessary. The problem
1091 should be fixed someplace else, and this extra code
1094 Also, if type if a reference, the readonly bits could
1095 either be in the outer type (with reference) or on the
1096 inner type (the thing being referenced). (mrs) */
1098 && ((TREE_READONLY (parm)
1099 && ! (TYPE_READONLY (type)
1100 || (TREE_CODE (type) == REFERENCE_TYPE
1101 && TYPE_READONLY (TREE_TYPE (type)))))
1102 || (TREE_SIDE_EFFECTS (parm)
1103 && ! (TYPE_VOLATILE (type)
1104 || (TREE_CODE (type) == REFERENCE_TYPE
1105 && TYPE_VOLATILE (TREE_TYPE (type)))))))
1114 else if (form == REFERENCE_TYPE)
1118 tree tmp = convert_from_reference (parm);
1119 intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
1126 intype = TREE_TYPE (intype);
1128 while (TREE_CODE (intype) == REFERENCE_TYPE);
1129 intype = TYPE_MAIN_VARIANT (intype);
1138 if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
1140 ttl = unsigned_type (ttl);
1141 intype = unsigned_type (intype);
1147 /* If the initializer is not an lvalue, then it does not
1148 matter if we make life easier for the programmer
1149 by creating a temporary variable with which to
1151 if (parm && (coder == INTEGER_TYPE
1152 || coder == ENUMERAL_TYPE
1153 || coder == REAL_TYPE)
1154 && ! lvalue_p (parm))
1155 return (convert_harshness_old (ttl, ttr, NULL_TREE)
1156 | INT_TO_EASY_HARSHNESS (penalty));
1161 return INT_TO_EASY_HARSHNESS (penalty);
1162 return INT_TO_BD_HARSHNESS (0);
1165 /* Pointers to voids always convert for pointers. But
1166 make them less natural than more specific matches. */
1167 if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
1168 if (TREE_TYPE (ttl) == void_type_node
1169 || TREE_TYPE (ttr) == void_type_node)
1170 return INT_TO_EASY_HARSHNESS (penalty+1);
1172 if (parm && codel != REFERENCE_TYPE)
1173 return (convert_harshness_old (ttl, ttr, NULL_TREE)
1174 | INT_TO_EASY_HARSHNESS (penalty));
1176 /* Here it does matter. If this conversion is from
1177 derived to base, allow it. Otherwise, types must
1178 be compatible in the strong sense. */
1179 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
1181 int b_or_d = get_base_distance (ttl, ttr, 0, 0);
1184 b_or_d = get_base_distance (ttr, ttl, 0, 0);
1187 return CONTRAVARIANT_HARSHNESS (-1);
1189 /* Say that this conversion is relatively painless.
1190 If it turns out that there is a user-defined X(X&)
1191 constructor, then that will be invoked, but that's
1192 preferable to dealing with other user-defined conversions
1193 that may produce surprising results. */
1194 return INT_TO_BD_HARSHNESS (b_or_d);
1197 if (comp_target_types (ttl, intype, 1))
1198 return INT_TO_EASY_HARSHNESS (penalty);
1201 if (codel == RECORD_TYPE && coder == RECORD_TYPE)
1203 int b_or_d = get_base_distance (type, parmtype, 0, 0);
1206 b_or_d = get_base_distance (parmtype, type, 0, 0);
1209 return CONTRAVARIANT_HARSHNESS (-1);
1211 return INT_TO_BD_HARSHNESS (b_or_d);
1216 #ifdef DEBUG_MATCHING
1219 struct harshness_code *h;
1221 static char buf[1024];
1224 bzero (buf, 1024 * sizeof (char));
1225 strcat (buf, "codes=[");
1226 if (h->code & EVIL_CODE)
1227 strcat (buf, "EVIL");
1228 if (h->code & CONST_CODE)
1229 strcat (buf, " CONST");
1230 if (h->code & ELLIPSIS_CODE)
1231 strcat (buf, " ELLIPSIS");
1232 if (h->code & USER_CODE)
1233 strcat (buf, " USER");
1234 if (h->code & STD_CODE)
1235 strcat (buf, " STD");
1236 if (h->code & PROMO_CODE)
1237 strcat (buf, " PROMO");
1238 if (h->code & QUAL_CODE)
1239 strcat (buf, " QUAL");
1240 if (h->code & TRIVIAL_CODE)
1241 strcat (buf, " TRIVIAL");
1245 sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
1253 /* Algorithm: For each argument, calculate how difficult it is to
1254 make FUNCTION accept that argument. If we can easily tell that
1255 FUNCTION won't be acceptable to one of the arguments, then we
1256 don't need to compute the ease of converting the other arguments,
1257 since it will never show up in the intersection of all arguments'
1260 Conversions between builtin and user-defined types are allowed, but
1261 no function involving such a conversion is preferred to one which
1262 does not require such a conversion. Furthermore, such conversions
1266 compute_conversion_costs_ansi (function, tta_in, cp, arglen)
1269 struct candidate *cp;
1272 tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
1276 /* Start out with no strikes against. */
1277 int evil_strikes = 0;
1278 int ellipsis_strikes = 0;
1279 int user_strikes = 0;
1280 int b_or_d_strikes = 0;
1281 int easy_strikes = 0;
1283 int strike_index = 0, win;
1284 struct harshness_code lose;
1286 #ifdef GATHER_STATISTICS
1287 n_compute_conversion_costs++;
1290 cp->function = function;
1291 cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
1292 cp->u.bad_arg = 0; /* optimistic! */
1296 cp->h.int_penalty = 0;
1297 bzero (cp->v.ansi_harshness,
1298 (cp->h_len + 1) * sizeof (struct harshness_code));
1302 struct harshness_code h;
1304 if (ttf == void_list_node)
1307 if (type_unknown_p (TREE_VALUE (tta)))
1309 /* Must perform some instantiation here. */
1310 tree rhs = TREE_VALUE (tta);
1311 tree lhstype = TREE_VALUE (ttf);
1313 /* Keep quiet about possible contravariance violations. */
1314 int old_inhibit_warnings = inhibit_warnings;
1315 inhibit_warnings = 1;
1317 /* @@ This is to undo what `grokdeclarator' does to
1318 parameter types. It really should go through
1319 something more general. */
1321 TREE_TYPE (tta) = unknown_type_node;
1322 rhs = instantiate_type (lhstype, rhs, 0);
1323 inhibit_warnings = old_inhibit_warnings;
1325 if (TREE_CODE (rhs) == ERROR_MARK)
1328 h = convert_harshness_ansi (lhstype, TREE_TYPE (rhs), rhs);
1332 #ifdef DEBUG_MATCHING
1333 static tree old_function = NULL_TREE;
1335 if (!old_function || function != old_function)
1337 cp_error ("trying %D", function);
1338 old_function = function;
1341 cp_error (" doing (%T) %E against arg %T",
1342 TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
1346 h = convert_harshness_ansi (TREE_VALUE (ttf),
1347 TREE_TYPE (TREE_VALUE (tta)),
1350 #ifdef DEBUG_MATCHING
1351 cp_error (" evaluated %s", print_harshness (&h));
1355 cp->v.ansi_harshness[strike_index] = h;
1356 if ((h.code & EVIL_CODE)
1357 || ((h.code & STD_CODE) && h.distance < 0))
1359 cp->u.bad_arg = strike_index;
1362 else if (h.code & ELLIPSIS_CODE)
1363 ellipsis_strikes += 1;
1365 /* This is never set by `convert_harshness_ansi'. */
1366 else if (h.code & USER_CODE)
1373 if ((h.code & STD_CODE) && h.distance)
1375 if (h.distance > b_or_d_strikes)
1376 b_or_d_strikes = h.distance;
1379 easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
1380 cp->h.code |= h.code;
1381 /* Make sure we communicate this. */
1382 cp->h.int_penalty += h.int_penalty;
1385 ttf = TREE_CHAIN (ttf);
1386 tta = TREE_CHAIN (tta);
1392 /* ran out of formals, and parmlist is fixed size. */
1393 if (ttf /* == void_type_node */)
1395 cp->h.code = EVIL_CODE;
1401 struct harshness_code h;
1402 int l = list_length (tta);
1403 ellipsis_strikes += l;
1404 h.code = ELLIPSIS_CODE;
1408 cp->v.ansi_harshness[strike_index++] = h;
1411 else if (ttf && ttf != void_list_node)
1413 /* ran out of actuals, and no defaults. */
1414 if (TREE_PURPOSE (ttf) == NULL_TREE)
1416 cp->h.code = EVIL_CODE;
1420 /* Store index of first default. */
1421 cp->v.ansi_harshness[arglen].distance = strike_index+1;
1424 cp->v.ansi_harshness[arglen].distance = 0;
1426 /* Argument list lengths work out, so don't need to check them again. */
1429 /* We do not check for derived->base conversions here, since in
1430 no case would they give evil strike counts, unless such conversions
1431 are somehow ambiguous. */
1433 /* See if any user-defined conversions apply.
1434 But make sure that we do not loop. */
1435 static int dont_convert_types = 0;
1437 if (dont_convert_types)
1439 cp->h.code = EVIL_CODE;
1443 win = 0; /* Only get one chance to win. */
1444 ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
1451 if (ttf == void_list_node)
1454 lose = cp->v.ansi_harshness[strike_index];
1455 if ((lose.code & EVIL_CODE)
1456 || ((lose.code & STD_CODE) && lose.distance < 0))
1458 tree actual_type = TREE_TYPE (TREE_VALUE (tta));
1459 tree formal_type = TREE_VALUE (ttf);
1461 dont_convert_types = 1;
1463 if (TREE_CODE (formal_type) == REFERENCE_TYPE)
1464 formal_type = TREE_TYPE (formal_type);
1465 if (TREE_CODE (actual_type) == REFERENCE_TYPE)
1466 actual_type = TREE_TYPE (actual_type);
1468 if (formal_type != error_mark_node
1469 && actual_type != error_mark_node)
1471 formal_type = TYPE_MAIN_VARIANT (formal_type);
1472 actual_type = TYPE_MAIN_VARIANT (actual_type);
1474 if (TYPE_HAS_CONSTRUCTOR (formal_type))
1476 /* If it has a constructor for this type,
1478 /* @@ There is no way to save this result yet, so
1479 success is a NULL_TREE for now. */
1480 if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
1484 if (TYPE_LANG_SPECIFIC (actual_type)
1485 && TYPE_HAS_CONVERSION (actual_type))
1487 if (TREE_CODE (formal_type) == INTEGER_TYPE
1488 && TYPE_HAS_INT_CONVERSION (actual_type))
1490 else if (TREE_CODE (formal_type) == REAL_TYPE
1491 && TYPE_HAS_REAL_CONVERSION (actual_type))
1496 /* Don't issue warnings since we're only groping
1497 around for the right answer, we haven't yet
1498 committed to going with this solution. */
1499 int old_inhibit_warnings = inhibit_warnings;
1501 inhibit_warnings = 1;
1502 conv = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
1503 inhibit_warnings = old_inhibit_warnings;
1507 if (conv == error_mark_node)
1512 else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
1514 conv = build_type_conversion (CALL_EXPR, formal_type, TREE_VALUE (tta), 0);
1517 if (conv == error_mark_node)
1526 dont_convert_types = 0;
1531 cp->v.ansi_harshness[strike_index].code = USER_CODE;
1536 if (cp->u.bad_arg > strike_index)
1537 cp->u.bad_arg = strike_index;
1539 evil_strikes = win ? 2 : 1;
1544 ttf = TREE_CHAIN (ttf);
1545 tta = TREE_CHAIN (tta);
1550 /* Const member functions get a small penalty because defaulting
1551 to const is less useful than defaulting to non-const. */
1552 /* This is bogus, it does not correspond to anything in the ARM.
1553 This code will be fixed when this entire section is rewritten
1554 to conform to the ARM. (mrs) */
1555 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1557 tree this_parm = TREE_VALUE (ttf_in);
1559 if (TREE_CODE (this_parm) == RECORD_TYPE /* Is `this' a sig ptr? */
1560 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
1561 : TYPE_READONLY (TREE_TYPE (this_parm)))
1563 cp->v.ansi_harshness[0].code |= TRIVIAL_CODE;
1568 /* Calling a non-const member function from a const member function
1569 is probably invalid, but for now we let it only draw a warning.
1570 We indicate that such a mismatch has occurred by setting the
1571 harshness to a maximum value. */
1572 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1573 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1574 cp->v.ansi_harshness[0].code |= CONST_CODE;
1579 cp->h.code = EVIL_CODE;
1580 if (ellipsis_strikes)
1581 cp->h.code |= ELLIPSIS_CODE;
1583 cp->h.code |= USER_CODE;
1584 #ifdef DEBUG_MATCHING
1585 cp_error ("final eval %s", print_harshness (&cp->h));
1590 compute_conversion_costs_old (function, tta_in, cp, arglen)
1593 struct candidate *cp;
1596 tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
1600 /* Start out with no strikes against. */
1601 int evil_strikes = 0;
1602 int ellipsis_strikes = 0;
1603 int user_strikes = 0;
1604 int b_or_d_strikes = 0;
1605 int easy_strikes = 0;
1607 int strike_index = 0, win, lose;
1609 #ifdef GATHER_STATISTICS
1610 n_compute_conversion_costs++;
1613 cp->function = function;
1614 cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
1615 cp->u.bad_arg = 0; /* optimistic! */
1617 bzero (cp->v.old_harshness, (cp->h_len + 1) * sizeof (unsigned short));
1623 if (ttf == void_list_node)
1626 if (type_unknown_p (TREE_VALUE (tta)))
1628 /* Must perform some instantiation here. */
1629 tree rhs = TREE_VALUE (tta);
1630 tree lhstype = TREE_VALUE (ttf);
1632 /* Keep quiet about possible contravariance violations. */
1633 int old_inhibit_warnings = inhibit_warnings;
1634 inhibit_warnings = 1;
1636 /* @@ This is to undo what `grokdeclarator' does to
1637 parameter types. It really should go through
1638 something more general. */
1640 TREE_TYPE (tta) = unknown_type_node;
1641 rhs = instantiate_type (lhstype, rhs, 0);
1642 inhibit_warnings = old_inhibit_warnings;
1644 if (TREE_CODE (rhs) == ERROR_MARK)
1648 harshness = convert_harshness_old (lhstype, TREE_TYPE (rhs),
1650 /* harshness |= 2; */
1654 harshness = convert_harshness_old (TREE_VALUE (ttf),
1655 TREE_TYPE (TREE_VALUE (tta)),
1658 cp->v.old_harshness[strike_index] = harshness;
1659 if (EVIL_HARSHNESS (harshness)
1660 || CONTRAVARIANT_HARSHNESS (harshness))
1662 cp->u.bad_arg = strike_index;
1665 else if (ELLIPSIS_HARSHNESS (harshness))
1667 ellipsis_strikes += 1;
1670 /* This is never set by `convert_harshness_old'. */
1671 else if (USER_HARSHNESS (harshness))
1676 else if (BASE_DERIVED_HARSHNESS (harshness))
1678 b_or_d_strikes += INT_FROM_BD_HARSHNESS (harshness);
1681 easy_strikes += INT_FROM_EASY_HARSHNESS (harshness);
1682 ttf = TREE_CHAIN (ttf);
1683 tta = TREE_CHAIN (tta);
1689 /* ran out of formals, and parmlist is fixed size. */
1690 if (ttf /* == void_type_node */)
1696 else ellipsis_strikes += list_length (tta);
1698 else if (ttf && ttf != void_list_node)
1700 /* ran out of actuals, and no defaults. */
1701 if (TREE_PURPOSE (ttf) == NULL_TREE)
1707 /* Store index of first default. */
1708 cp->v.old_harshness[arglen] = strike_index+1;
1711 cp->v.old_harshness[arglen] = 0;
1713 /* Argument list lengths work out, so don't need to check them again. */
1716 /* We do not check for derived->base conversions here, since in
1717 no case would they give evil strike counts, unless such conversions
1718 are somehow ambiguous. */
1720 /* See if any user-defined conversions apply.
1721 But make sure that we do not loop. */
1722 static int dont_convert_types = 0;
1724 if (dont_convert_types)
1730 win = 0; /* Only get one chance to win. */
1731 ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
1738 if (ttf == void_list_node)
1741 lose = cp->v.old_harshness[strike_index];
1742 if (EVIL_HARSHNESS (lose)
1743 || CONTRAVARIANT_HARSHNESS (lose))
1745 tree actual_type = TREE_TYPE (TREE_VALUE (tta));
1746 tree formal_type = TREE_VALUE (ttf);
1748 dont_convert_types = 1;
1750 if (TREE_CODE (formal_type) == REFERENCE_TYPE)
1751 formal_type = TREE_TYPE (formal_type);
1752 if (TREE_CODE (actual_type) == REFERENCE_TYPE)
1753 actual_type = TREE_TYPE (actual_type);
1755 if (formal_type != error_mark_node
1756 && actual_type != error_mark_node)
1758 formal_type = TYPE_MAIN_VARIANT (formal_type);
1759 actual_type = TYPE_MAIN_VARIANT (actual_type);
1761 if (TYPE_HAS_CONSTRUCTOR (formal_type))
1763 /* If it has a constructor for this type, try to use it. */
1764 if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
1767 /* @@ There is no way to save this result yet.
1768 @@ So success is NULL_TREE for now. */
1772 if (TYPE_LANG_SPECIFIC (actual_type) && TYPE_HAS_CONVERSION (actual_type))
1774 if (TREE_CODE (formal_type) == INTEGER_TYPE
1775 && TYPE_HAS_INT_CONVERSION (actual_type))
1777 else if (TREE_CODE (formal_type) == REAL_TYPE
1778 && TYPE_HAS_REAL_CONVERSION (actual_type))
1782 tree conv = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
1785 if (conv == error_mark_node)
1790 else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
1792 conv = build_type_conversion (CALL_EXPR, formal_type, TREE_VALUE (tta), 0);
1795 if (conv == error_mark_node)
1804 dont_convert_types = 0;
1809 cp->v.old_harshness[strike_index] = USER_HARSHNESS (-1);
1814 if (cp->u.bad_arg > strike_index)
1815 cp->u.bad_arg = strike_index;
1817 evil_strikes = win ? 2 : 1;
1822 ttf = TREE_CHAIN (ttf);
1823 tta = TREE_CHAIN (tta);
1828 /* Const member functions get a small penalty because defaulting
1829 to const is less useful than defaulting to non-const. */
1830 /* This is bogus, it does not correspond to anything in the ARM.
1831 This code will be fixed when this entire section is rewritten
1832 to conform to the ARM. (mrs) */
1833 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1835 tree this_parm = TREE_VALUE (ttf_in);
1837 if (TREE_CODE (this_parm) == RECORD_TYPE /* Is `this' a sig ptr? */
1838 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
1839 : TYPE_READONLY (TREE_TYPE (this_parm)))
1841 cp->v.old_harshness[0] += INT_TO_EASY_HARSHNESS (1);
1846 /* Calling a non-const member function from a const member function
1847 is probably invalid, but for now we let it only draw a warning.
1848 We indicate that such a mismatch has occurred by setting the
1849 harshness to a maximum value. */
1850 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1851 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1852 cp->v.old_harshness[0] |= CONST_HARSHNESS (-1);
1856 cp->evil = evil_strikes;
1857 cp->ellipsis = ellipsis_strikes;
1858 cp->user = user_strikes;
1859 cp->b_or_d = b_or_d_strikes;
1860 cp->easy = easy_strikes;
1864 compute_conversion_costs (function, tta_in, cp, arglen)
1867 struct candidate *cp;
1870 if (flag_ansi_overloading)
1871 compute_conversion_costs_ansi (function, tta_in, cp, arglen);
1873 compute_conversion_costs_old (function, tta_in, cp, arglen);
1876 /* When one of several possible overloaded functions and/or methods
1877 can be called, choose the best candidate for overloading.
1879 BASETYPE is the context from which we start method resolution
1880 or NULL if we are comparing overloaded functions.
1881 CANDIDATES is the array of candidates we have to choose from.
1882 N_CANDIDATES is the length of CANDIDATES.
1883 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1884 choose. It is modified in place when resolving methods. It is not
1885 modified in place when resolving overloaded functions.
1886 LEN is the length of the parameter list. */
1888 static struct candidate *
1889 ideal_candidate_old (basetype, candidates, n_candidates, parms, len)
1891 struct candidate *candidates;
1896 struct candidate *cp = candidates + n_candidates;
1900 qsort (candidates, /* char *base */
1901 n_candidates, /* int nel */
1902 sizeof (struct candidate), /* int width */
1903 rank_for_overload); /* int (*compar)() */
1905 /* If the best candidate requires user-defined conversions,
1906 and its user-defined conversions are a strict subset
1907 of all other candidates requiring user-defined conversions,
1908 then it is, in fact, the best. */
1909 for (i = -1; cp + i != candidates; i--)
1910 if (cp[i].user == 0)
1917 /* Check that every other candidate requires those conversions
1918 as a strict subset of their conversions. */
1919 if (cp[i].user == cp[-1].user)
1922 /* Look at subset relationship more closely. */
1925 for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)),
1926 ttf0 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function)),
1927 index = 0; index < len; index++)
1929 if (USER_HARSHNESS (cp[i].v.old_harshness[index]))
1931 /* If our "best" candidate also needs a conversion,
1932 it must be the same one. */
1933 if (USER_HARSHNESS (cp[-1].v.old_harshness[index])
1934 && TREE_VALUE (ttf) != TREE_VALUE (ttf0))
1937 ttf = TREE_CHAIN (ttf);
1938 ttf0 = TREE_CHAIN (ttf0);
1939 /* Handle `...' gracefully. */
1940 if (ttf == NULL_TREE || ttf0 == NULL_TREE)
1945 /* The best was the best. */
1948 /* Use other rules for determining "bestness". */
1952 /* If the best two candidates we find require user-defined
1953 conversions, we may need to report and error message. */
1954 if (cp[-1].user && cp[-2].user
1955 && (cp[-1].b_or_d || cp[-2].b_or_d == 0))
1957 /* If the best two methods found involved user-defined
1958 type conversions, then we must see whether one
1959 of them is exactly what we wanted. If not, then
1960 we have an ambiguity. */
1969 /* Stash all of our parameters in safe places
1970 so that we can perform type conversions in place. */
1973 TREE_PURPOSE (tta) = TREE_VALUE (tta);
1974 tta = TREE_CHAIN (tta);
1980 int exact_conversions = 0;
1984 if (DECL_STATIC_FUNCTION_P (cp[i].function))
1985 tta = TREE_CHAIN (tta);
1986 /* special note, we don't go through len parameters, because we
1987 may only need len-1 parameters because of a call to a static
1989 for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)), index = 0;
1991 tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
1993 /* If this is a varargs function, there's no conversion to do,
1994 but don't accept an arg that needs a copy ctor. */
1995 if (ttf == NULL_TREE)
1997 /* FIXME: verify that we cannot get here with an
1998 arg that needs a ctor. */
2002 if (USER_HARSHNESS (cp[i].v.old_harshness[index]))
2004 tree this_parm = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_PURPOSE (tta), 2);
2005 if (basetype != NULL_TREE)
2006 TREE_VALUE (tta) = this_parm;
2009 if (TREE_CODE (this_parm) != CONVERT_EXPR
2010 && (TREE_CODE (this_parm) != NOP_EXPR
2011 || comp_target_types (TREE_TYPE (this_parm),
2012 TREE_TYPE (TREE_OPERAND (this_parm, 0)), 1)))
2013 exact_conversions += 1;
2015 else if (PROMOTES_TO_AGGR_TYPE (TREE_VALUE (ttf), REFERENCE_TYPE))
2017 /* To get here we had to have succeeded via
2019 TREE_VALUE (tta) = TREE_PURPOSE (tta);
2020 exact_conversions += 1;
2024 if (exact_conversions == cp[i].user)
2029 f1 = cp[best].function;
2032 p1 = TYPE_ARG_TYPES (TREE_TYPE (f1));
2037 /* Don't complain if next best is from base class. */
2038 tree f2 = cp[i].function;
2040 if (TREE_CODE (TREE_TYPE (f1)) == METHOD_TYPE
2041 && TREE_CODE (TREE_TYPE (f2)) == METHOD_TYPE
2042 && BASE_DERIVED_HARSHNESS (cp[i].v.old_harshness[0])
2043 && cp[best].v.old_harshness[0] < cp[i].v.old_harshness[0])
2046 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (f2));
2048 if (! compparms (TREE_CHAIN (p1), TREE_CHAIN (p2), 1))
2056 /* Ensure that there's nothing ambiguous about these
2059 for (index = 0; index < len; index++)
2061 /* Type conversions must be piecewise equivalent. */
2062 if (USER_HARSHNESS (cp[best].v.old_harshness[index])
2063 != USER_HARSHNESS (cp[i].v.old_harshness[index]))
2065 /* If there's anything we like better about the
2066 other function, consider it ambiguous. */
2067 if (cp[i].v.old_harshness[index] < cp[best].v.old_harshness[index])
2069 /* If any single one it diffent, then the whole is
2071 if (cp[i].v.old_harshness[index] != cp[best].v.old_harshness[index])
2075 /* If we can't tell the difference between the two, it
2080 /* If we made it to here, it means we're satisfied that
2081 BEST is still best. */
2086 } while (cp + i != candidates);
2090 int exact_conversions = cp[best].user;
2092 if (DECL_STATIC_FUNCTION_P (cp[best].function))
2093 tta = TREE_CHAIN (parms);
2094 for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[best].function)), index = 0;
2095 exact_conversions > 0;
2096 tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
2098 if (USER_HARSHNESS (cp[best].v.old_harshness[index]))
2100 /* We must now fill in the slot we left behind.
2101 @@ This could be optimized to use the value previously
2102 @@ computed by build_type_conversion in some cases. */
2103 if (basetype != NULL_TREE)
2104 TREE_VALUE (tta) = convert (TREE_VALUE (ttf), TREE_PURPOSE (tta));
2105 exact_conversions -= 1;
2108 TREE_VALUE (tta) = TREE_PURPOSE (tta);
2114 /* If the best two candidates we find both use default parameters,
2115 we may need to report and error. Don't need to worry if next-best
2116 candidate is forced to use user-defined conversion when best is not. */
2117 if (cp[-2].user == 0
2118 && cp[-1].v.old_harshness[len] != 0 && cp[-2].v.old_harshness[len] != 0)
2120 tree tt1 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function));
2121 tree tt2 = TYPE_ARG_TYPES (TREE_TYPE (cp[-2].function));
2122 unsigned i = cp[-1].v.old_harshness[len];
2124 if (cp[-2].v.old_harshness[len] < i)
2125 i = cp[-2].v.old_harshness[len];
2128 if (TYPE_MAIN_VARIANT (TREE_VALUE (tt1))
2129 != TYPE_MAIN_VARIANT (TREE_VALUE (tt2)))
2130 /* These lists are not identical, so we can choose our best candidate. */
2132 tt1 = TREE_CHAIN (tt1);
2133 tt2 = TREE_CHAIN (tt2);
2135 /* To get here, both lists had the same parameters up to the defaults
2136 which were used. This is an ambiguous request. */
2140 /* Otherwise, return our best candidate. Note that if we get candidates
2141 from independent base classes, we have an ambiguity, even if one
2142 argument list look a little better than another one. */
2143 if (cp[-1].b_or_d && basetype && TYPE_USES_MULTIPLE_INHERITANCE (basetype))
2145 int i = n_candidates - 1, best = i;
2146 tree base1 = NULL_TREE;
2148 if (TREE_CODE (TREE_TYPE (candidates[i].function)) == FUNCTION_TYPE)
2151 for (; i >= 0 && candidates[i].user == 0 && candidates[i].evil == 0; i--)
2153 if (TREE_CODE (TREE_TYPE (candidates[i].function)) == METHOD_TYPE)
2155 tree newbase = DECL_CLASS_CONTEXT (candidates[i].function);
2157 if (base1 != NULL_TREE)
2159 /* newbase could be a base or a parent of base1 */
2160 if (newbase != base1 && ! UNIQUELY_DERIVED_FROM_P (newbase, base1)
2161 && ! UNIQUELY_DERIVED_FROM_P (base1, newbase))
2163 cp_error ("ambiguous request for function from distinct base classes of type `%T'", basetype);
2164 cp_error_at (" first candidate is `%#D'",
2165 candidates[best].function);
2166 cp_error_at (" second candidate is `%#D'",
2167 candidates[i].function);
2183 /* Don't accept a candidate as being ideal if it's indistinguishable
2184 from another candidate. */
2185 if (rank_for_overload (cp-1, cp-2) == 0)
2187 /* If the types are distinguishably different (like
2188 `long' vs. `unsigned long'), that's ok. But if they are arbitrarily
2189 different, such as `int (*)(void)' vs. `void (*)(int)',
2191 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function));
2192 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (cp[-2].function));
2195 if (TREE_CODE (TREE_VALUE (p1)) == POINTER_TYPE
2196 && TREE_CODE (TREE_TYPE (TREE_VALUE (p1))) == FUNCTION_TYPE
2197 && TREE_VALUE (p1) != TREE_VALUE (p2))
2199 p1 = TREE_CHAIN (p1);
2200 p2 = TREE_CHAIN (p2);
2209 /* In the case where there is no ideal candidate, restore
2210 TREE_VALUE slots of PARMS from TREE_PURPOSE slots. */
2213 TREE_VALUE (parms) = TREE_PURPOSE (parms);
2214 parms = TREE_CHAIN (parms);
2219 /* Subroutine of ideal_candidate. See if X or Y is a better match
2222 strictly_better (x, y)
2223 unsigned short x, y;
2231 if (xor >= x || xor >= y)
2236 static struct candidate *
2237 ideal_candidate_ansi (basetype, candidates, n_candidates, parms, len)
2239 struct candidate *candidates;
2244 struct candidate *cp = candidates+n_candidates;
2245 int i, j = -1, best_code;
2247 /* For each argument, sort the functions from best to worst for the arg.
2248 For each function that's not best for this arg, set its overall
2249 harshness to EVIL so that other args won't like it. The candidate
2250 list for the last argument is the intersection of all the best-liked
2254 for (i = 0; i < len; i++)
2256 qsort (candidates, n_candidates, sizeof (struct candidate),
2258 best_code = cp[-1].h.code;
2260 /* To find out functions that are worse than that represented
2261 by BEST_CODE, we can't just do a comparison like h.code>best_code.
2262 The total harshness for the "best" fn may be 8|8 for two args, and
2263 the harshness for the next-best may be 8|2. If we just compared,
2264 that would be checking 8>10, which would lead to the next-best
2265 being disqualified. What we actually want to do is get rid
2266 of functions that are definitely worse than that represented
2267 by best_code, i.e. those which have bits set higher than the
2268 highest in best_code. Sooooo, what we do is clear out everything
2269 represented by best_code, and see if we still come up with something
2270 higher. If so (e.g., 8|8 vs 8|16), it'll disqualify it properly. */
2271 for (j = n_candidates-2; j >= 0; j--)
2272 if ((candidates[j].h.code & ~best_code) > best_code)
2273 candidates[j].h.code = EVIL_CODE;
2276 if (cp[-1].h.code & EVIL_CODE)
2279 qsort (candidates, n_candidates, sizeof (struct candidate),
2281 best_code = cp[-1].h.code;
2284 /* If they're at least as good as each other, do an arg-by-arg check. */
2285 if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
2290 for (j = 0; j < n_candidates; j++)
2291 if (! strictly_better (candidates[j].h.code, best_code))
2294 qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
2296 for (i = 0; i < len; i++)
2298 if (cp[-1].v.ansi_harshness[i].code < cp[-2].v.ansi_harshness[i].code)
2300 else if (cp[-1].v.ansi_harshness[i].code > cp[-2].v.ansi_harshness[i].code)
2302 else if (cp[-1].v.ansi_harshness[i].code & STD_CODE)
2304 /* If it involves a standard conversion, let the
2305 inheritance lattice be the final arbiter. */
2306 if (cp[-1].v.ansi_harshness[i].distance > cp[-2].v.ansi_harshness[i].distance)
2308 else if (cp[-1].v.ansi_harshness[i].distance < cp[-2].v.ansi_harshness[i].distance)
2311 else if (cp[-1].v.ansi_harshness[i].code & PROMO_CODE)
2313 /* For integral promotions, take into account a finer
2314 granularity for determining which types should be favored
2315 over others in such promotions. */
2316 if (cp[-1].v.ansi_harshness[i].int_penalty > cp[-2].v.ansi_harshness[i].int_penalty)
2318 else if (cp[-1].v.ansi_harshness[i].int_penalty < cp[-2].v.ansi_harshness[i].int_penalty)
2323 if (! better || worse)
2329 static struct candidate *
2330 ideal_candidate (basetype, candidates, n_candidates, parms, len)
2332 struct candidate *candidates;
2337 if (flag_ansi_overloading)
2338 return ideal_candidate_ansi (basetype, candidates, n_candidates, parms,
2341 return ideal_candidate_old (basetype, candidates, n_candidates, parms,
2345 /* Assume that if the class referred to is not in the
2346 current class hierarchy, that it may be remote.
2347 PARENT is assumed to be of aggregate type here. */
2349 may_be_remote (parent)
2352 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
2355 if (current_class_type == NULL_TREE)
2358 if (parent == current_class_type)
2361 if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
2367 build_vfield_ref (datum, type)
2371 int old_assume_nonnull_objects = flag_assume_nonnull_objects;
2373 if (datum == error_mark_node)
2374 return error_mark_node;
2376 /* Vtable references are always made from non-null objects. */
2377 flag_assume_nonnull_objects = 1;
2378 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
2379 datum = convert_from_reference (datum);
2381 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
2382 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
2383 datum, CLASSTYPE_VFIELD (type));
2385 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
2386 flag_assume_nonnull_objects = old_assume_nonnull_objects;
2391 /* Build a call to a member of an object. I.e., one that overloads
2392 operator ()(), or is a pointer-to-function or pointer-to-method. */
2394 build_field_call (basetype_path, instance_ptr, name, parms)
2395 tree basetype_path, instance_ptr, name, parms;
2397 tree field, instance;
2399 if (instance_ptr == current_class_decl)
2401 /* Check to see if we really have a reference to an instance variable
2402 with `operator()()' overloaded. */
2403 field = IDENTIFIER_CLASS_VALUE (name);
2405 if (field == NULL_TREE)
2407 cp_error ("`this' has no member named `%D'", name);
2408 return error_mark_node;
2411 if (TREE_CODE (field) == FIELD_DECL)
2413 /* If it's a field, try overloading operator (),
2414 or calling if the field is a pointer-to-function. */
2415 instance = build_component_ref_1 (C_C_D, field, 0);
2416 if (instance == error_mark_node)
2417 return error_mark_node;
2419 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
2420 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
2421 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
2423 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
2425 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
2426 return build_function_call (instance, parms);
2427 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
2428 return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
2434 /* Check to see if this is not really a reference to an instance variable
2435 with `operator()()' overloaded. */
2436 field = lookup_field (basetype_path, name, 1, 0);
2438 /* This can happen if the reference was ambiguous or for access
2440 if (field == error_mark_node)
2441 return error_mark_node;
2446 tree ftype = TREE_TYPE (field);
2448 if (TREE_CODE (ftype) == REFERENCE_TYPE)
2449 ftype = TREE_TYPE (ftype);
2451 if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
2453 /* Make the next search for this field very short. */
2454 basetype = DECL_FIELD_CONTEXT (field);
2455 instance_ptr = convert_pointer_to (basetype, instance_ptr);
2457 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2458 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
2459 build_component_ref_1 (instance, field, 0),
2462 if (TREE_CODE (ftype) == POINTER_TYPE)
2464 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
2465 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
2467 /* This is a member which is a pointer to function. */
2469 = build_component_ref_1 (build_indirect_ref (instance_ptr,
2471 field, LOOKUP_COMPLAIN);
2472 if (ref == error_mark_node)
2473 return error_mark_node;
2474 return build_function_call (ref, parms);
2477 else if (TREE_CODE (ftype) == METHOD_TYPE)
2479 error ("invalid call via pointer-to-member function");
2480 return error_mark_node;
2489 find_scoped_type (type, inner_name, inner_types)
2490 tree type, inner_name, inner_types;
2492 tree tags = CLASSTYPE_TAGS (type);
2496 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
2497 enclosing class) is set to the name for the enum type. So, if
2498 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
2499 then this test will be true. */
2500 if (TREE_PURPOSE (tags) == inner_name)
2502 if (inner_types == NULL_TREE)
2503 return DECL_NESTED_TYPENAME (TYPE_NAME (TREE_VALUE (tags)));
2504 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
2506 tags = TREE_CHAIN (tags);
2510 /* XXX This needs to be fixed better. */
2511 if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
2513 sorry ("nested class lookup in template type");
2518 /* Look for a TYPE_DECL. */
2519 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
2520 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
2522 /* Code by raeburn. */
2523 if (inner_types == NULL_TREE)
2524 return DECL_NESTED_TYPENAME (tags);
2525 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
2531 /* Resolve an expression NAME1::NAME2::...::NAMEn to
2532 the name that names the above nested type. INNER_TYPES
2533 is a chain of nested type names (held together by SCOPE_REFs);
2534 OUTER_TYPE is the type we know to enclose INNER_TYPES.
2535 Returns NULL_TREE if there is an error. */
2537 resolve_scope_to_name (outer_type, inner_stuff)
2538 tree outer_type, inner_stuff;
2541 tree inner_name, inner_type;
2543 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
2545 /* We first try to look for a nesting in our current class context,
2546 then try any enclosing classes. */
2547 tree type = current_class_type;
2549 while (type && (TREE_CODE (type) == RECORD_TYPE
2550 || TREE_CODE (type) == UNION_TYPE))
2552 tree rval = resolve_scope_to_name (type, inner_stuff);
2554 if (rval != NULL_TREE)
2556 type = DECL_CONTEXT (TYPE_NAME (type));
2560 if (TREE_CODE (inner_stuff) == SCOPE_REF)
2562 inner_name = TREE_OPERAND (inner_stuff, 0);
2563 inner_type = TREE_OPERAND (inner_stuff, 1);
2567 inner_name = inner_stuff;
2568 inner_type = NULL_TREE;
2571 if (outer_type == NULL_TREE)
2573 /* If we have something that's already a type by itself,
2575 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
2578 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
2585 if (! IS_AGGR_TYPE (outer_type))
2588 /* Look for member classes or enums. */
2589 tmp = find_scoped_type (outer_type, inner_name, inner_type);
2591 /* If it's not a type in this class, then go down into the
2592 base classes and search there. */
2593 if (! tmp && TYPE_BINFO (outer_type))
2595 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
2596 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2598 for (i = 0; i < n_baselinks; i++)
2600 tree base_binfo = TREE_VEC_ELT (binfos, i);
2601 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
2611 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
2612 This is how virtual function calls are avoided. */
2614 build_scoped_method_call (exp, scopes, name, parms)
2615 tree exp, scopes, name, parms;
2617 /* Because this syntactic form does not allow
2618 a pointer to a base class to be `stolen',
2619 we need not protect the derived->base conversion
2622 @@ But we do have to check access privileges later. */
2623 tree basename = resolve_scope_to_name (NULL_TREE, scopes);
2624 tree basetype, binfo, decl;
2625 tree type = TREE_TYPE (exp);
2627 if (type == error_mark_node
2628 || basename == NULL_TREE)
2629 return error_mark_node;
2631 basetype = IDENTIFIER_TYPE_VALUE (basename);
2633 if (TREE_CODE (type) == REFERENCE_TYPE)
2634 type = TREE_TYPE (type);
2636 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
2637 that explicit ~int is caught in the parser; this deals with typedefs
2638 and template parms. */
2639 if (TREE_CODE (name) == BIT_NOT_EXPR && ! is_aggr_typedef (basename, 0))
2641 if (type != basetype)
2642 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
2643 exp, basetype, type);
2644 name = IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0));
2645 if (basetype != name)
2646 cp_error ("qualified type `%T' does not match destructor type `%T'",
2648 return void_zero_node;
2651 if (! is_aggr_typedef (basename, 1))
2652 return error_mark_node;
2654 if (! IS_AGGR_TYPE (type))
2656 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
2658 return error_mark_node;
2661 if (binfo = binfo_or_else (basetype, type))
2663 if (binfo == error_mark_node)
2664 return error_mark_node;
2665 if (TREE_CODE (exp) == INDIRECT_REF)
2666 decl = build_indirect_ref (convert_pointer_to (binfo,
2667 build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
2669 decl = build_scoped_ref (exp, scopes);
2671 /* Call to a destructor. */
2672 if (TREE_CODE (name) == BIT_NOT_EXPR)
2674 /* Explicit call to destructor. */
2675 name = TREE_OPERAND (name, 0);
2676 if (TREE_TYPE (decl) !=
2677 (IDENTIFIER_CLASS_VALUE (name)
2678 ? IDENTIFIER_CLASS_TYPE_VALUE (name)
2679 : IDENTIFIER_TYPE_VALUE (name)))
2682 ("qualified type `%T' does not match destructor type `%T'",
2683 TREE_TYPE (decl), name);
2684 return error_mark_node;
2686 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
2687 return void_zero_node;
2689 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
2690 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
2694 /* Call to a method. */
2695 return build_method_call (decl, name, parms, NULL_TREE,
2696 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
2698 return error_mark_node;
2702 print_candidates (candidates)
2705 cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
2706 candidates = TREE_CHAIN (candidates);
2710 cp_error_at (" %D", TREE_VALUE (candidates));
2711 candidates = TREE_CHAIN (candidates);
2716 print_n_candidates (candidates, n)
2717 struct candidate *candidates;
2722 cp_error_at ("candidates are: %D", candidates[0].function);
2723 for (i = 1; i < n; i++)
2724 cp_error_at (" %D", candidates[i].function);
2727 /* Build something of the form ptr->method (args)
2728 or object.method (args). This can also build
2729 calls to constructors, and find friends.
2731 Member functions always take their class variable
2734 INSTANCE is a class instance.
2736 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
2738 PARMS help to figure out what that NAME really refers to.
2740 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
2741 down to the real instance type to use for access checking. We need this
2742 information to get protected accesses correct. This parameter is used
2743 by build_member_call.
2745 FLAGS is the logical disjunction of zero or more LOOKUP_
2746 flags. See cp-tree.h for more info.
2748 If this is all OK, calls build_function_call with the resolved
2751 This function must also handle being called to perform
2752 initialization, promotion/coercion of arguments, and
2753 instantiation of default parameters.
2755 Note that NAME may refer to an instance variable name. If
2756 `operator()()' is defined for the type of that field, then we return
2759 build_method_call (instance, name, parms, basetype_path, flags)
2760 tree instance, name, parms, basetype_path;
2763 register tree function, fntype, value_type;
2764 register tree basetype, save_basetype;
2765 register tree baselink, result, method_name, parmtypes, parm;
2768 enum access_type access = access_public;
2770 /* Range of cases for vtable optimization. */
2771 enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
2772 enum vtable_needs need_vtbl = not_needed;
2776 tree instance_ptr = NULL_TREE;
2777 int all_virtual = flag_all_virtual;
2778 int static_call_context = 0;
2779 tree found_fns = NULL_TREE;
2781 /* Keep track of `const' and `volatile' objects. */
2782 int constp, volatilep;
2784 #ifdef GATHER_STATISTICS
2785 n_build_method_call++;
2788 if (instance == error_mark_node
2789 || name == error_mark_node
2790 || parms == error_mark_node
2791 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
2792 return error_mark_node;
2794 /* This is the logic that magically deletes the second argument to
2795 operator delete, if it is not needed. */
2796 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
2798 tree save_last = TREE_CHAIN (parms);
2800 /* get rid of unneeded argument */
2801 TREE_CHAIN (parms) = NULL_TREE;
2802 result = build_method_call (instance, name, parms, basetype_path,
2803 (LOOKUP_SPECULATIVELY|flags)
2805 /* If it works, return it. */
2806 if (result && result != error_mark_node)
2807 return build_method_call (instance, name, parms, basetype_path, flags);
2808 /* If it doesn't work, two argument delete must work */
2809 TREE_CHAIN (parms) = save_last;
2812 if (TREE_CODE (name) == BIT_NOT_EXPR)
2814 flags |= LOOKUP_DESTRUCTOR;
2815 name = TREE_OPERAND (name, 0);
2817 error ("destructors take no parameters");
2818 basetype = get_type_value (name);
2819 if (basetype == NULL_TREE)
2821 cp_error ("call to destructor for non-type `%D'", name);
2822 return void_zero_node;
2824 if (basetype != TREE_TYPE(instance))
2825 basetype = TREE_TYPE(instance);
2826 if (! TYPE_HAS_DESTRUCTOR (basetype))
2827 return void_zero_node;
2828 instance = default_conversion (instance);
2829 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
2830 return build_delete (build_pointer_type (basetype),
2831 instance_ptr, integer_two_node,
2832 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
2838 /* Initialize name for error reporting. */
2839 if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
2841 char *p = operator_name_string (name);
2842 xref_name = (char *)alloca (strlen (p) + 10);
2843 sprintf (xref_name, "operator %s", p);
2845 else if (TREE_CODE (name) == SCOPE_REF)
2846 xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
2848 xref_name = IDENTIFIER_POINTER (name);
2850 GNU_xref_call (current_function_decl, xref_name);
2853 if (instance == NULL_TREE)
2855 basetype = NULL_TREE;
2856 /* Check cases where this is really a call to raise
2858 if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
2860 basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
2862 basetype = TREE_VALUE (basetype);
2864 else if (TREE_CODE (name) == SCOPE_REF
2865 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
2867 if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
2868 return error_mark_node;
2869 basetype = purpose_member (TREE_OPERAND (name, 1),
2870 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
2872 basetype = TREE_VALUE (basetype);
2875 if (basetype != NULL_TREE)
2877 /* call to a constructor... */
2878 else if (IDENTIFIER_HAS_TYPE_VALUE (name))
2880 basetype = IDENTIFIER_TYPE_VALUE (name);
2881 name = constructor_name_full (basetype);
2885 tree typedef_name = lookup_name (name, 1);
2886 if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
2888 /* Canonicalize the typedef name. */
2889 basetype = TREE_TYPE (typedef_name);
2890 name = TYPE_IDENTIFIER (basetype);
2894 cp_error ("no constructor named `%T' in scope",
2896 return error_mark_node;
2900 if (! IS_AGGR_TYPE (basetype))
2903 if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
2904 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2905 name, instance, basetype);
2907 return error_mark_node;
2910 else if (instance == C_C_D || instance == current_class_decl)
2912 /* When doing initialization, we side-effect the TREE_TYPE of
2913 C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
2914 basetype = TREE_TYPE (C_C_D);
2916 /* Anything manifestly `this' in constructors and destructors
2917 has a known type, so virtual function tables are not needed. */
2918 if (TYPE_VIRTUAL_P (basetype)
2919 && !(flags & LOOKUP_NONVIRTUAL))
2920 need_vtbl = (dtor_label || ctor_label)
2921 ? unneeded : maybe_needed;
2924 instance_ptr = current_class_decl;
2925 result = build_field_call (TYPE_BINFO (current_class_type),
2926 instance_ptr, name, parms);
2931 else if (TREE_CODE (instance) == RESULT_DECL)
2933 basetype = TREE_TYPE (instance);
2934 /* Should we ever have to make a virtual function reference
2935 from a RESULT_DECL, know that it must be of fixed type
2936 within the scope of this function. */
2937 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
2938 need_vtbl = maybe_needed;
2939 instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
2941 else if (instance == current_exception_object)
2943 instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (current_exception_type),
2944 TREE_OPERAND (current_exception_object, 0));
2945 mark_addressable (TREE_OPERAND (current_exception_object, 0));
2946 result = build_field_call (TYPE_BINFO (current_exception_type),
2947 instance_ptr, name, parms);
2950 cp_error ("exception member `%D' cannot be invoked", name);
2951 return error_mark_node;
2955 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
2956 tree inst_ptr_basetype;
2958 static_call_context =
2959 (TREE_CODE (instance) == INDIRECT_REF
2960 && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
2961 && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
2963 /* the base type of an instance variable is pointer to class */
2964 basetype = TREE_TYPE (instance);
2966 if (TREE_CODE (basetype) == REFERENCE_TYPE)
2968 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (basetype));
2969 if (! IS_AGGR_TYPE (basetype))
2970 goto non_aggr_error;
2971 /* Call to convert not needed because we are remaining
2972 within the same type. */
2973 instance_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), instance);
2974 inst_ptr_basetype = basetype;
2978 if (! IS_AGGR_TYPE (basetype))
2979 goto non_aggr_error;
2981 if (IS_SIGNATURE_POINTER (basetype)
2982 || IS_SIGNATURE_REFERENCE (basetype))
2983 basetype = SIGNATURE_TYPE (basetype);
2985 if ((IS_SIGNATURE (basetype)
2986 && (instance_ptr = build_optr_ref (instance)))
2987 || (lvalue_p (instance)
2988 && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
2989 || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
2991 if (instance_ptr == error_mark_node)
2992 return error_mark_node;
2994 else if (TREE_CODE (instance) == NOP_EXPR
2995 || TREE_CODE (instance) == CONSTRUCTOR)
2997 /* A cast is not an lvalue. Initialize a fresh temp
2998 with the value we are casting from, and proceed with
2999 that temporary. We can't cast to a reference type,
3000 so that simplifies the initialization to something
3002 tree temp = get_temp_name (TREE_TYPE (instance), 0);
3003 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
3004 expand_aggr_init (temp, instance, 0);
3007 store_init_value (temp, instance);
3008 expand_decl_init (temp);
3011 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
3015 if (TREE_CODE (instance) != CALL_EXPR)
3016 my_friendly_abort (125);
3017 if (TYPE_NEEDS_CONSTRUCTING (basetype))
3018 instance = build_cplus_new (basetype, instance, 0);
3021 instance = get_temp_name (basetype, 0);
3022 TREE_ADDRESSABLE (instance) = 1;
3024 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
3026 /* @@ Should we call comp_target_types here? */
3027 inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
3028 if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
3029 basetype = inst_ptr_basetype;
3032 instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
3033 if (instance_ptr == error_mark_node)
3034 return error_mark_node;
3038 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
3039 not updated, so we use `basetype' instead. */
3040 if (basetype_path == NULL_TREE
3041 && IS_SIGNATURE (basetype))
3042 basetype_path = TYPE_BINFO (basetype);
3043 else if (basetype_path == NULL_TREE ||
3044 BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
3045 basetype_path = TYPE_BINFO (inst_ptr_basetype);
3047 result = build_field_call (basetype_path, instance_ptr, name, parms);
3051 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
3053 if (TREE_SIDE_EFFECTS (instance_ptr))
3055 /* This action is needed because the instance is needed
3056 for providing the base of the virtual function table.
3057 Without using a SAVE_EXPR, the function we are building
3058 may be called twice, or side effects on the instance
3059 variable (such as a post-increment), may happen twice. */
3060 instance_ptr = save_expr (instance_ptr);
3061 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3063 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
3065 /* This happens when called for operator new (). */
3066 instance = build_indirect_ref (instance, NULL_PTR);
3069 need_vtbl = maybe_needed;
3073 if (TYPE_SIZE (basetype) == 0)
3075 /* This is worth complaining about, I think. */
3076 cp_error ("cannot lookup method in incomplete type `%T'", basetype);
3077 return error_mark_node;
3080 save_basetype = basetype;
3083 if (all_virtual == 1
3084 && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
3085 OPERATOR_METHOD_LENGTH)
3086 || instance_ptr == NULL_TREE
3087 || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0)))
3092 for (parmtypes = NULL_TREE, parm = parms; parm; parm = TREE_CHAIN (parm))
3094 tree t = TREE_TYPE (TREE_VALUE (parm));
3095 if (TREE_CODE (t) == OFFSET_TYPE)
3097 /* Convert OFFSET_TYPE entities to their normal selves. */
3098 TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
3099 t = TREE_TYPE (TREE_VALUE (parm));
3101 if (TREE_CODE (TREE_VALUE (parm)) == OFFSET_REF
3102 && TREE_CODE (t) == METHOD_TYPE)
3104 TREE_VALUE (parm) = build_unary_op (ADDR_EXPR, TREE_VALUE (parm), 0);
3106 if (TREE_CODE (t) == ARRAY_TYPE)
3108 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3109 This eliminates needless calls to `compute_conversion_costs'. */
3110 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
3111 t = TREE_TYPE (TREE_VALUE (parm));
3113 if (t == error_mark_node)
3114 return error_mark_node;
3115 last = build_tree_list (NULL_TREE, t);
3116 parmtypes = chainon (parmtypes, last);
3121 constp = TREE_READONLY (instance);
3122 volatilep = TREE_THIS_VOLATILE (instance);
3123 parms = tree_cons (NULL_TREE, instance_ptr, parms);
3127 /* Raw constructors are always in charge. */
3128 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3129 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3131 flags |= LOOKUP_HAS_IN_CHARGE;
3132 parms = tree_cons (NULL_TREE, integer_one_node, parms);
3133 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
3136 if (flag_this_is_variable > 0)
3140 parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
3146 instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
3147 if (instance_ptr == error_mark_node)
3148 return error_mark_node;
3149 instance_ptr = save_expr (instance_ptr);
3150 TREE_CALLS_NEW (instance_ptr) = 1;
3151 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3153 /* If it's a default argument initialized from a ctor, what we get
3154 from instance_ptr will match the arglist for the FUNCTION_DECL
3155 of the constructor. */
3156 if (parms && TREE_CODE (TREE_VALUE (parms)) == CALL_EXPR
3157 && TREE_OPERAND (TREE_VALUE (parms), 1)
3158 && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms), 1))))
3159 parms = build_tree_list (NULL_TREE, instance_ptr);
3161 parms = tree_cons (NULL_TREE, instance_ptr, parms);
3164 parmtypes = tree_cons (NULL_TREE,
3165 build_pointer_type (build_type_variant (basetype, constp, volatilep)),
3167 if (last == NULL_TREE)
3170 /* Look up function name in the structure type definition. */
3172 if ((IDENTIFIER_HAS_TYPE_VALUE (name)
3173 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name))
3174 && TREE_CODE(IDENTIFIER_TYPE_VALUE (name)) != UNINSTANTIATED_P_TYPE)
3175 || name == constructor_name (basetype))
3177 tree tmp = NULL_TREE;
3178 if (IDENTIFIER_TYPE_VALUE (name) == basetype
3179 || name == constructor_name (basetype))
3180 tmp = TYPE_BINFO (basetype);
3182 tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
3184 if (tmp != NULL_TREE)
3186 name_kind = "constructor";
3188 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3189 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3191 /* Constructors called for initialization
3192 only are never in charge. */
3195 flags |= LOOKUP_HAS_IN_CHARGE;
3196 tmplist = tree_cons (NULL_TREE, integer_zero_node,
3197 TREE_CHAIN (parms));
3198 TREE_CHAIN (parms) = tmplist;
3199 tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
3200 TREE_CHAIN (parmtypes) = tmplist;
3202 basetype = BINFO_TYPE (tmp);
3205 name_kind = "method";
3208 name_kind = "method";
3210 if (basetype_path == NULL_TREE
3211 || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
3212 basetype_path = TYPE_BINFO (basetype);
3213 result = lookup_fnfields (basetype_path, name,
3214 (flags & LOOKUP_COMPLAIN));
3215 if (result == error_mark_node)
3216 return error_mark_node;
3219 /* Now, go look for this method name. We do not find destructors here.
3221 Putting `void_list_node' on the end of the parmtypes
3222 fakes out `build_decl_overload' into doing the right thing. */
3223 TREE_CHAIN (last) = void_list_node;
3224 method_name = build_decl_overload (name, parmtypes,
3225 1 + (name == constructor_name (save_basetype)
3226 || name == constructor_name_full (save_basetype)));
3227 TREE_CHAIN (last) = NULL_TREE;
3229 for (pass = 0; pass < 2; pass++)
3231 struct candidate *candidates;
3232 struct candidate *cp;
3236 /* This increments every time we go up the type hierarchy.
3237 The idea is to prefer a function of the derived class if possible. */
3245 = (struct candidate *) alloca ((ever_seen+1)
3246 * sizeof (struct candidate));
3247 bzero (candidates, (ever_seen + 1) * sizeof (struct candidate));
3249 len = list_length (parms);
3252 /* First see if a global function has a shot at it. */
3253 if (flags & LOOKUP_GLOBAL)
3256 tree parm = TREE_VALUE (parms);
3258 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
3259 friend_parms = parms;
3260 else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
3263 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
3264 new_type = build_reference_type (TREE_TYPE (parm));
3265 /* It is possible that this should go down a layer. */
3266 new_type = build_type_variant (new_type,
3267 TREE_READONLY (parm),
3268 TREE_THIS_VOLATILE (parm));
3269 parm = convert (new_type, parm);
3270 friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
3273 my_friendly_abort (167);
3276 if (flag_ansi_overloading)
3277 cp->v.ansi_harshness = (struct harshness_code *)
3278 alloca ((len + 1) * sizeof (struct harshness_code));
3280 cp->v.old_harshness = (unsigned short *)
3281 alloca ((len + 1) * sizeof (unsigned short));
3283 result = build_overload_call (name, friend_parms, 0, cp);
3284 /* If it turns out to be the one we were actually looking for
3285 (it was probably a friend function), the return the
3287 if (TREE_CODE (result) == CALL_EXPR)
3290 if (flag_ansi_overloading)
3291 while ((cp->h.code & EVIL_CODE) == 0)
3293 /* non-standard uses: set the field to 0 to indicate
3294 we are using a non-member function. */
3296 if (cp->v.ansi_harshness[len].distance == 0
3297 && cp->h.code < best)
3302 while (cp->evil == 0)
3304 /* non-standard uses: set the field to 0 to indicate
3305 we are using a non-member function. */
3307 if (cp->v.old_harshness[len] == 0
3308 && cp->v.old_harshness[len] == 0
3309 && cp->ellipsis == 0 && cp->user == 0 && cp->b_or_d == 0
3319 /* We have a hit (of sorts). If the parameter list is
3320 "error_mark_node", or some variant thereof, it won't
3321 match any methods. Since we have verified that the is
3322 some method vaguely matching this one (in name at least),
3325 Don't stop for friends, however. */
3326 basetype_path = TREE_PURPOSE (baselink);
3328 function = TREE_VALUE (baselink);
3329 if (TREE_CODE (basetype_path) == TREE_LIST)
3330 basetype_path = TREE_VALUE (basetype_path);
3331 basetype = BINFO_TYPE (basetype_path);
3333 /* Cast the instance variable to the appropriate type. */
3334 TREE_VALUE (parms) = convert_force (TYPE_POINTER_TO (basetype),
3336 /* FIXME: this is the wrong place to get an error. Hopefully
3337 the access-control rewrite will make this change more cleanly. */
3338 if (TREE_VALUE (parms) == error_mark_node)
3339 return error_mark_node;
3341 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
3342 function = DECL_CHAIN (function);
3344 for (; function; function = DECL_CHAIN (function))
3346 #ifdef GATHER_STATISTICS
3347 n_inner_fields_searched++;
3351 found_fns = tree_cons (NULL_TREE, function, found_fns);
3353 /* Not looking for friends here. */
3354 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
3355 && ! DECL_STATIC_FUNCTION_P (function))
3359 && DECL_ASSEMBLER_NAME (function) == method_name)
3364 tree these_parms = parms;
3366 #ifdef GATHER_STATISTICS
3367 n_inner_fields_searched++;
3370 if (flag_ansi_overloading)
3371 cp->v.ansi_harshness = (struct harshness_code *)
3372 alloca ((len + 1) * sizeof (struct harshness_code));
3374 cp->v.old_harshness = (unsigned short *)
3375 alloca ((len + 1) * sizeof (unsigned short));
3377 if (DECL_STATIC_FUNCTION_P (function))
3378 these_parms = TREE_CHAIN (these_parms);
3379 compute_conversion_costs (function, these_parms, cp, len);
3381 if (!flag_ansi_overloading)
3382 cp->b_or_d += b_or_d;
3384 if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE) == 0)
3385 || (!flag_ansi_overloading && cp->evil == 0))
3387 cp->u.field = function;
3388 cp->function = function;
3389 cp->basetypes = basetype_path;
3391 /* No "two-level" conversions. */
3392 if (flags & LOOKUP_NO_CONVERSION
3393 && ((flag_ansi_overloading
3394 && (cp->h.code & USER_CODE))
3395 || (!flag_ansi_overloading
3399 /* If we used default parameters, we must
3400 check to see whether anyone else might
3401 use them also, and report a possible
3403 if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
3404 && ((flag_ansi_overloading
3405 && cp->v.ansi_harshness[len].distance == 0
3406 && cp->h.code < best)
3407 || (!flag_ansi_overloading
3408 && cp->v.old_harshness[len] == 0
3409 && CONST_HARSHNESS (cp->v.old_harshness[0]) == 0
3410 && cp->ellipsis == 0 && cp->user == 0 && cp->b_or_d == 0
3411 && cp->easy < best)))
3413 if (! DECL_STATIC_FUNCTION_P (function))
3414 TREE_VALUE (parms) = cp->arg;
3416 goto found_and_maybe_warn;
3422 /* Now we have run through one link's member functions.
3423 arrange to head-insert this link's links. */
3424 baselink = next_baselink (baselink);
3426 /* Don't grab functions from base classes. lookup_fnfield will
3427 do the work to get us down into the right place. */
3428 baselink = NULL_TREE;
3432 tree igv = IDENTIFIER_GLOBAL_VALUE (name);
3434 /* No exact match could be found. Now try to find match
3435 using default conversions. */
3436 if ((flags & LOOKUP_GLOBAL) && igv)
3438 if (TREE_CODE (igv) == FUNCTION_DECL)
3440 else if (TREE_CODE (igv) == TREE_LIST)
3441 ever_seen += count_functions (igv);
3446 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3447 == LOOKUP_SPECULATIVELY)
3450 TREE_CHAIN (last) = void_list_node;
3451 if (flags & LOOKUP_GLOBAL)
3452 cp_error ("no global or member function `%D(%A)' defined",
3455 cp_error ("no member function `%T::%D(%A)' defined",
3456 save_basetype, name, TREE_CHAIN (parmtypes));
3457 return error_mark_node;
3462 if (cp - candidates != 0)
3464 /* Rank from worst to best. Then cp will point to best one.
3465 Private fields have their bits flipped. For unsigned
3466 numbers, this should make them look very large.
3467 If the best alternate has a (signed) negative value,
3468 then all we ever saw were private members. */
3469 if (cp - candidates > 1)
3471 cp = ideal_candidate (save_basetype, candidates,
3472 cp - candidates, parms, len);
3473 if (cp == (struct candidate *)0)
3475 cp_error ("ambiguous type conversion requested for %s `%D'",
3477 return error_mark_node;
3479 if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE))
3480 || (!flag_ansi_overloading && cp->evil))
3481 return error_mark_node;
3483 else if ((flag_ansi_overloading && (cp[-1].h.code & EVIL_CODE))
3484 || (!flag_ansi_overloading && cp[-1].evil == 2))
3486 cp_error ("ambiguous type conversion requested for %s `%D'",
3488 return error_mark_node;
3493 /* The global function was the best, so use it. */
3494 if (cp->u.field == 0)
3496 /* We must convert the instance pointer into a reference type.
3497 Global overloaded functions can only either take
3498 aggregate objects (which come for free from references)
3499 or reference data types anyway. */
3500 TREE_VALUE (parms) = copy_node (instance_ptr);
3501 TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
3502 return build_function_call (cp->function, parms);
3505 function = cp->function;
3506 basetype_path = cp->basetypes;
3507 if (! DECL_STATIC_FUNCTION_P (function))
3508 TREE_VALUE (parms) = cp->arg;
3509 goto found_and_maybe_warn;
3512 if ((flags & ~LOOKUP_GLOBAL) & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
3514 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3515 == LOOKUP_SPECULATIVELY)
3518 if (DECL_STATIC_FUNCTION_P (cp->function))
3519 parms = TREE_CHAIN (parms);
3522 if (flags & LOOKUP_SPECULATIVELY)
3524 if (static_call_context
3525 && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
3526 cp_error ("object missing in call to `%D'", cp->function);
3527 else if (ever_seen > 1)
3529 TREE_CHAIN (last) = void_list_node;
3530 cp_error ("no matching function for call to `%T::%D (%A)'",
3531 TREE_TYPE (TREE_TYPE (instance_ptr)),
3532 name, TREE_CHAIN (parmtypes));
3533 TREE_CHAIN (last) = NULL_TREE;
3534 print_candidates (found_fns);
3537 report_type_mismatch (cp, parms, name_kind);
3538 return error_mark_node;
3541 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3544 cp_error ("%T has no method named %D", save_basetype, name);
3545 return error_mark_node;
3551 found_and_maybe_warn:
3552 if ((flag_ansi_overloading
3553 && (cp->v.ansi_harshness[0].code & CONST_CODE))
3554 || (!flag_ansi_overloading
3555 && CONST_HARSHNESS (cp->v.old_harshness[0])))
3557 if (flags & LOOKUP_COMPLAIN)
3559 cp_error_at ("non-const member function `%D'", cp->function);
3560 error ("called for const object at this point in file");
3562 /* Not good enough for a match. */
3564 return error_mark_node;
3568 /* Silently return error_mark_node. */
3569 return error_mark_node;
3572 if (flags & LOOKUP_PROTECT)
3573 access = compute_access (basetype_path, function);
3575 if (access == access_private)
3577 if (flags & LOOKUP_COMPLAIN)
3579 cp_error_at ("%s `%+#D' is %s", name_kind, function,
3580 TREE_PRIVATE (function) ? "private"
3581 : "from private base class");
3582 error ("within this context");
3584 return error_mark_node;
3586 else if (access == access_protected)
3588 if (flags & LOOKUP_COMPLAIN)
3590 cp_error_at ("%s `%+#D' %s", name_kind, function,
3591 TREE_PROTECTED (function) ? "is protected"
3592 : "has protected accessibility");
3593 error ("within this context");
3595 return error_mark_node;
3598 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
3599 type (if it exists) is a pointer to. */
3601 if (IS_SIGNATURE (basetype) && static_call_context)
3603 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
3605 return error_mark_node;
3607 else if (IS_SIGNATURE (basetype))
3608 return build_signature_method_call (basetype, instance, function, parms);
3610 function = DECL_MAIN_VARIANT (function);
3611 /* Declare external function if necessary. */
3612 assemble_external (function);
3614 fntype = TREE_TYPE (function);
3615 if (TREE_CODE (fntype) == POINTER_TYPE)
3616 fntype = TREE_TYPE (fntype);
3617 basetype = DECL_CLASS_CONTEXT (function);
3619 /* If we are referencing a virtual function from an object
3620 of effectively static type, then there is no need
3621 to go through the virtual function table. */
3622 if (need_vtbl == maybe_needed)
3624 int fixed_type = resolves_to_fixed_type_p (instance, 0);
3626 if (all_virtual == 1
3627 && DECL_VINDEX (function)
3628 && may_be_remote (basetype))
3630 else if (DECL_VINDEX (function))
3631 need_vtbl = fixed_type ? unneeded : needed;
3633 need_vtbl = not_needed;
3636 if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
3637 && !DECL_CONSTRUCTOR_P (function))
3639 /* Let's be nice to the user for now, and give reasonable
3640 default behavior. */
3641 instance_ptr = current_class_decl;
3644 if (basetype != current_class_type)
3646 tree binfo = get_binfo (basetype, current_class_type, 1);
3647 if (binfo == NULL_TREE)
3649 error_not_base_type (function, current_class_type);
3650 return error_mark_node;
3652 else if (basetype == error_mark_node)
3653 return error_mark_node;
3656 /* Only allow a static member function to call another static member
3658 else if (DECL_LANG_SPECIFIC (function)
3659 && !DECL_STATIC_FUNCTION_P (function))
3661 cp_error ("cannot call member function `%D' without object",
3663 return error_mark_node;
3667 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3669 if (TYPE_SIZE (value_type) == 0)
3671 if (flags & LOOKUP_COMPLAIN)
3672 incomplete_type_error (0, value_type);
3673 return error_mark_node;
3676 /* We do not pass FUNCTION into `convert_arguments', because by
3677 now everything should be ok. If not, then we have a serious error. */
3678 if (DECL_STATIC_FUNCTION_P (function))
3679 parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
3680 TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
3681 else if (need_vtbl == unneeded)
3683 int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
3684 basetype = TREE_TYPE (instance);
3685 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function)) != TYPE_MAIN_VARIANT (basetype)
3686 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3688 basetype = DECL_CLASS_CONTEXT (function);
3689 instance_ptr = convert_pointer_to (basetype, instance_ptr);
3690 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3692 parms = tree_cons (NULL_TREE, instance_ptr,
3693 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
3697 if ((flags & LOOKUP_NONVIRTUAL) == 0)
3698 basetype = DECL_CONTEXT (function);
3700 /* First parm could be integer_zerop with casts like
3701 ((Object*)0)->Object::IsA() */
3702 if (!integer_zerop (TREE_VALUE (parms)))
3704 /* Since we can't have inheritance with a union, doing get_binfo
3705 on it won't work. We do all the convert_pointer_to_real
3706 stuff to handle MI correctly...for unions, that's not
3707 an issue, so we must short-circuit that extra work here. */
3708 tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
3709 if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
3710 instance_ptr = TREE_VALUE (parms);
3713 tree binfo = get_binfo (basetype,
3714 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3716 instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3719 = convert_pointer_to (build_type_variant (basetype,
3723 if (TREE_CODE (instance_ptr) == COND_EXPR)
3725 instance_ptr = save_expr (instance_ptr);
3726 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3728 else if (TREE_CODE (instance_ptr) == NOP_EXPR
3729 && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
3730 && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
3732 /* The call to `convert_pointer_to' may return error_mark_node. */
3733 else if (TREE_CODE (instance_ptr) == ERROR_MARK)
3734 return instance_ptr;
3735 else if (instance == NULL_TREE
3736 || TREE_CODE (instance) != INDIRECT_REF
3737 || TREE_OPERAND (instance, 0) != instance_ptr)
3738 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3740 parms = tree_cons (NULL_TREE, instance_ptr,
3741 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL));
3745 /* Constructors do not overload method calls. */
3746 else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
3747 && name != TYPE_IDENTIFIER (basetype)
3748 && (TREE_CODE (function) != FUNCTION_DECL
3749 || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)),
3750 OPERATOR_METHOD_FORMAT,
3751 OPERATOR_METHOD_LENGTH))
3752 && (may_be_remote (basetype) || instance != C_C_D))
3756 parms = TREE_CHAIN (parms);
3758 if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
3759 fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
3761 fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
3762 if (all_virtual == 1)
3763 fn_as_int = convert (integer_type_node, fn_as_int);
3765 result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
3767 if (result == NULL_TREE)
3769 compiler_error ("could not overload `operator->()(...)'");
3770 return error_mark_node;
3772 else if (result == error_mark_node)
3773 return error_mark_node;
3776 /* Do this if we want the result of operator->() to inherit
3777 the type of the function it is subbing for. */
3778 TREE_TYPE (result) = value_type;
3785 if (need_vtbl == needed)
3787 function = build_vfn_ref (&TREE_VALUE (parms), instance, DECL_VINDEX (function));
3788 TREE_TYPE (function) = build_pointer_type (fntype);
3791 if (TREE_CODE (function) == FUNCTION_DECL)
3792 GNU_xref_call (current_function_decl,
3793 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
3798 if (TREE_CODE (function) == FUNCTION_DECL)
3800 is_constructor = DECL_CONSTRUCTOR_P (function);
3801 if (DECL_INLINE (function))
3802 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
3805 assemble_external (function);
3806 TREE_USED (function) = 1;
3807 function = default_conversion (function);
3813 function = default_conversion (function);
3816 result = build_nt (CALL_EXPR, function, parms, NULL_TREE);
3818 TREE_TYPE (result) = value_type;
3819 TREE_SIDE_EFFECTS (result) = 1;
3820 TREE_RAISES (result)
3821 = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
3822 TREE_HAS_CONSTRUCTOR (result) = is_constructor;
3827 /* Similar to `build_method_call', but for overloaded non-member functions.
3828 The name of this function comes through NAME. The name depends
3831 Note that this function must handle simple `C' promotions,
3832 as well as variable numbers of arguments (...), and
3833 default arguments to boot.
3835 If the overloading is successful, we return a tree node which
3836 contains the call to the function.
3838 If overloading produces candidates which are probable, but not definite,
3839 we hold these candidates. If FINAL_CP is non-zero, then we are free
3840 to assume that final_cp points to enough storage for all candidates that
3841 this function might generate. The `harshness' array is preallocated for
3842 the first candidate, but not for subsequent ones.
3844 Note that the DECL_RTL of FUNCTION must be made to agree with this
3845 function's new name. */
3848 build_overload_call_real (fnname, parms, flags, final_cp, buildxxx)
3851 struct candidate *final_cp;
3854 /* must check for overloading here */
3855 tree overload_name, functions, function, parm;
3856 tree parmtypes = NULL_TREE, last = NULL_TREE;
3857 register tree outer;
3859 int parmlength = list_length (parms);
3861 struct candidate *candidates, *cp;
3865 if (flag_ansi_overloading)
3867 final_cp[0].h.code = 0;
3868 final_cp[0].h.distance = 0;
3869 final_cp[0].function = 0;
3871 final_cp[1].h.code = EVIL_CODE;
3875 final_cp[0].evil = 0;
3876 final_cp[0].user = 0;
3877 final_cp[0].b_or_d = 0;
3878 final_cp[0].easy = 0;
3879 final_cp[0].function = 0;
3881 final_cp[1].evil = 1;
3885 for (parm = parms; parm; parm = TREE_CHAIN (parm))
3887 register tree t = TREE_TYPE (TREE_VALUE (parm));
3889 if (t == error_mark_node)
3893 if (flag_ansi_overloading)
3894 final_cp->h.code = EVIL_CODE;
3898 return error_mark_node;
3900 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
3902 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3903 Also convert OFFSET_TYPE entities to their normal selves.
3904 This eliminates needless calls to `compute_conversion_costs'. */
3905 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
3906 t = TREE_TYPE (TREE_VALUE (parm));
3908 last = build_tree_list (NULL_TREE, t);
3909 parmtypes = chainon (parmtypes, last);
3912 TREE_CHAIN (last) = void_list_node;
3914 parmtypes = void_list_node;
3916 if (! flag_ansi_overloading)
3918 /* This is a speed improvement that ends up not working properly in
3919 the situation of fns with and without default parameters. I turned
3920 this off in the new method so it'll go through the argument matching
3921 code to properly diagnose a match/failure. (bpk) */
3922 overload_name = build_decl_overload (fnname, parmtypes, 0);
3924 /* Now check to see whether or not we can win.
3925 Note that if we are called from `build_method_call',
3926 then we cannot have a mis-match, because we would have
3927 already found such a winning case. */
3929 if (IDENTIFIER_GLOBAL_VALUE (overload_name))
3930 if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (overload_name)) != TREE_LIST)
3931 return build_function_call (DECL_MAIN_VARIANT (IDENTIFIER_GLOBAL_VALUE (overload_name)), parms);
3934 functions = IDENTIFIER_GLOBAL_VALUE (fnname);
3936 if (functions == NULL_TREE)
3938 if (flags & LOOKUP_SPECULATIVELY)
3940 if (flags & LOOKUP_COMPLAIN)
3941 error ("only member functions apply");
3944 if (flag_ansi_overloading)
3945 final_cp->h.code = EVIL_CODE;
3949 return error_mark_node;
3952 if (! TREE_OVERLOADED (fnname))
3954 functions = DECL_MAIN_VARIANT (functions);
3957 /* We are just curious whether this is a viable alternative or
3959 compute_conversion_costs (functions, parms, final_cp, parmlength);
3963 return build_function_call_real (functions, parms, 1, flags);
3966 if (TREE_CODE (functions) == TREE_LIST
3967 && TREE_VALUE (functions) == NULL_TREE)
3969 if (flags & LOOKUP_SPECULATIVELY)
3972 if (flags & LOOKUP_COMPLAIN)
3973 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
3974 TREE_PURPOSE (functions));
3977 if (flag_ansi_overloading)
3978 final_cp->h.code = EVIL_CODE;
3982 return error_mark_node;
3985 length = count_functions (functions);
3988 candidates = final_cp;
3992 = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
3993 bzero (candidates, (length + 1) * sizeof (struct candidate));
3998 my_friendly_assert (is_overloaded_fn (functions), 169);
4000 functions = get_first_fn (functions);
4002 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
4003 for (outer = functions; outer; outer = DECL_CHAIN (outer))
4005 int template_cost = 0;
4007 if (TREE_CODE (function) != FUNCTION_DECL
4008 && ! (TREE_CODE (function) == TEMPLATE_DECL
4009 && ! DECL_TEMPLATE_IS_CLASS (function)
4010 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
4012 enum tree_code code = TREE_CODE (function);
4013 if (code == TEMPLATE_DECL)
4014 code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
4015 if (code == CONST_DECL)
4017 ("enumeral value `%D' conflicts with function of same name",
4019 else if (code == VAR_DECL)
4021 if (TREE_STATIC (function))
4023 ("variable `%D' conflicts with function of same name",
4027 ("constant field `%D' conflicts with function of same name",
4030 else if (code == TYPE_DECL)
4033 my_friendly_abort (2);
4034 error ("at this point in file");
4037 if (TREE_CODE (function) == TEMPLATE_DECL)
4039 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
4040 tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
4043 i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
4044 TYPE_ARG_TYPES (TREE_TYPE (function)),
4045 parms, &template_cost, 0);
4048 struct candidate *cp2;
4050 function = instantiate_template (function, targs);
4051 /* Now check that the template instantiated for this is not
4052 the same as a function that's in the list due to some
4053 previous instantiation. */
4056 if (cp2->function == function)
4060 if (cp2->function == function)
4065 if (TREE_CODE (function) == TEMPLATE_DECL)
4067 /* Unconverted template -- failed match. */
4068 cp->function = function;
4070 if (flag_ansi_overloading)
4071 cp->h.code = EVIL_CODE;
4077 function = DECL_MAIN_VARIANT (function);
4079 /* Can't use alloca here, since result might be
4080 passed to calling function. */
4081 cp->h_len = parmlength;
4082 if (flag_ansi_overloading)
4083 cp->v.ansi_harshness = (struct harshness_code *)
4084 oballoc ((parmlength + 1) * sizeof (struct harshness_code));
4086 cp->v.old_harshness = (unsigned short *)
4087 oballoc ((parmlength + 1) * sizeof (unsigned short));
4089 compute_conversion_costs (function, parms, cp, parmlength);
4091 if (flag_ansi_overloading)
4092 /* Make sure this is clear as well. */
4093 cp->h.int_penalty += template_cost;
4095 /* Should really add another field... */
4096 cp->easy = cp->easy * 128 + template_cost;
4098 /* It seemed easier to have both if stmts in here, rather
4099 than excluding the hell out of it with flag_ansi_overloading
4100 everywhere. (bpk) */
4101 if (flag_ansi_overloading)
4103 if ((cp[0].h.code & EVIL_CODE) == 0)
4105 cp[1].h.code = EVIL_CODE;
4107 /* int_penalty is set by convert_harshness_ansi for cases
4108 where we need to know about any penalties that would
4109 otherwise make a TRIVIAL_CODE pass. */
4111 && template_cost == 0
4112 && cp[0].h.code <= TRIVIAL_CODE
4113 && cp[0].h.int_penalty == 0)
4115 final_cp[0].h = cp[0].h;
4123 if (cp[0].evil == 0)
4127 && cp[0].user == 0 && cp[0].b_or_d == 0
4128 && template_cost == 0
4131 final_cp[0].easy = cp[0].easy;
4140 if (cp - candidates)
4142 tree rval = error_mark_node;
4145 if (flag_ansi_overloading)
4146 cp[0].h.code = EVIL_CODE;
4149 if (cp - candidates > 1)
4151 struct candidate *best_cp
4152 = ideal_candidate (NULL_TREE, candidates,
4153 cp - candidates, parms, parmlength);
4154 if (best_cp == (struct candidate *)0)
4156 if (flags & LOOKUP_COMPLAIN)
4158 cp_error ("call of overloaded `%D' is ambiguous", fnname);
4159 print_n_candidates (candidates, cp - candidates);
4161 return error_mark_node;
4164 rval = best_cp->function;
4169 if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE))
4170 || (!flag_ansi_overloading && cp->evil > 1))
4172 if (flags & LOOKUP_COMPLAIN)
4173 error ("type conversion ambiguous");
4176 rval = cp->function;
4182 return buildxxx ? build_function_call_real (rval, parms, 0, flags)
4183 : build_function_call_real (rval, parms, 1, flags);
4186 if (flags & LOOKUP_SPECULATIVELY)
4189 if (flags & LOOKUP_COMPLAIN)
4190 report_type_mismatch (cp, parms, "function",
4191 decl_as_string (cp->function, 1));
4193 return error_mark_node;
4197 build_overload_call (fnname, parms, flags, final_cp)
4200 struct candidate *final_cp;
4202 return build_overload_call_real (fnname, parms, flags, final_cp, 0);
4206 build_overload_call_maybe (fnname, parms, flags, final_cp)
4209 struct candidate *final_cp;
4211 return build_overload_call_real (fnname, parms, flags, final_cp, 1);