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;
42 /* From cp-typeck.c: */
43 extern tree unary_complex_lvalue ();
45 /* Compute the ease with which a conversion can be performed
46 between an expected and the given type. */
47 static 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 (! TYPE_HAS_DESTRUCTOR (basetype))
2825 return void_zero_node;
2826 instance = default_conversion (instance);
2827 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
2828 return build_delete (build_pointer_type (basetype),
2829 instance_ptr, integer_two_node,
2830 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
2836 /* Initialize name for error reporting. */
2837 if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
2839 char *p = operator_name_string (name);
2840 xref_name = (char *)alloca (strlen (p) + 10);
2841 sprintf (xref_name, "operator %s", p);
2843 else if (TREE_CODE (name) == SCOPE_REF)
2844 xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
2846 xref_name = IDENTIFIER_POINTER (name);
2848 GNU_xref_call (current_function_decl, xref_name);
2851 if (instance == NULL_TREE)
2853 basetype = NULL_TREE;
2854 /* Check cases where this is really a call to raise
2856 if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
2858 basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
2860 basetype = TREE_VALUE (basetype);
2862 else if (TREE_CODE (name) == SCOPE_REF
2863 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
2865 if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
2866 return error_mark_node;
2867 basetype = purpose_member (TREE_OPERAND (name, 1),
2868 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
2870 basetype = TREE_VALUE (basetype);
2873 if (basetype != NULL_TREE)
2875 /* call to a constructor... */
2876 else if (IDENTIFIER_HAS_TYPE_VALUE (name))
2878 basetype = IDENTIFIER_TYPE_VALUE (name);
2879 name = constructor_name_full (basetype);
2883 tree typedef_name = lookup_name (name, 1);
2884 if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
2886 /* Canonicalize the typedef name. */
2887 basetype = TREE_TYPE (typedef_name);
2888 name = TYPE_IDENTIFIER (basetype);
2892 cp_error ("no constructor named `%T' in scope",
2894 return error_mark_node;
2898 if (! IS_AGGR_TYPE (basetype))
2901 if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
2902 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2903 name, instance, basetype);
2905 return error_mark_node;
2908 else if (instance == C_C_D || instance == current_class_decl)
2910 /* When doing initialization, we side-effect the TREE_TYPE of
2911 C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
2912 basetype = TREE_TYPE (C_C_D);
2914 /* Anything manifestly `this' in constructors and destructors
2915 has a known type, so virtual function tables are not needed. */
2916 if (TYPE_VIRTUAL_P (basetype)
2917 && !(flags & LOOKUP_NONVIRTUAL))
2918 need_vtbl = (dtor_label || ctor_label)
2919 ? unneeded : maybe_needed;
2922 instance_ptr = current_class_decl;
2923 result = build_field_call (TYPE_BINFO (current_class_type),
2924 instance_ptr, name, parms);
2929 else if (TREE_CODE (instance) == RESULT_DECL)
2931 basetype = TREE_TYPE (instance);
2932 /* Should we ever have to make a virtual function reference
2933 from a RESULT_DECL, know that it must be of fixed type
2934 within the scope of this function. */
2935 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
2936 need_vtbl = maybe_needed;
2937 instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
2939 else if (instance == current_exception_object)
2941 instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (current_exception_type),
2942 TREE_OPERAND (current_exception_object, 0));
2943 mark_addressable (TREE_OPERAND (current_exception_object, 0));
2944 result = build_field_call (TYPE_BINFO (current_exception_type),
2945 instance_ptr, name, parms);
2948 cp_error ("exception member `%D' cannot be invoked", name);
2949 return error_mark_node;
2953 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
2954 tree inst_ptr_basetype;
2956 static_call_context =
2957 (TREE_CODE (instance) == INDIRECT_REF
2958 && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
2959 && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
2961 /* the base type of an instance variable is pointer to class */
2962 basetype = TREE_TYPE (instance);
2964 if (TREE_CODE (basetype) == REFERENCE_TYPE)
2966 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (basetype));
2967 if (! IS_AGGR_TYPE (basetype))
2968 goto non_aggr_error;
2969 /* Call to convert not needed because we are remaining
2970 within the same type. */
2971 instance_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), instance);
2972 inst_ptr_basetype = basetype;
2976 if (! IS_AGGR_TYPE (basetype))
2977 goto non_aggr_error;
2979 if (IS_SIGNATURE_POINTER (basetype)
2980 || IS_SIGNATURE_REFERENCE (basetype))
2981 basetype = SIGNATURE_TYPE (basetype);
2983 if ((IS_SIGNATURE (basetype)
2984 && (instance_ptr = build_optr_ref (instance)))
2985 || (lvalue_p (instance)
2986 && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
2987 || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
2989 if (instance_ptr == error_mark_node)
2990 return error_mark_node;
2992 else if (TREE_CODE (instance) == NOP_EXPR
2993 || TREE_CODE (instance) == CONSTRUCTOR)
2995 /* A cast is not an lvalue. Initialize a fresh temp
2996 with the value we are casting from, and proceed with
2997 that temporary. We can't cast to a reference type,
2998 so that simplifies the initialization to something
3000 tree temp = get_temp_name (TREE_TYPE (instance), 0);
3001 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
3002 expand_aggr_init (temp, instance, 0);
3005 store_init_value (temp, instance);
3006 expand_decl_init (temp);
3009 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
3013 if (TREE_CODE (instance) != CALL_EXPR)
3014 my_friendly_abort (125);
3015 if (TYPE_NEEDS_CONSTRUCTING (basetype))
3016 instance = build_cplus_new (basetype, instance, 0);
3019 instance = get_temp_name (basetype, 0);
3020 TREE_ADDRESSABLE (instance) = 1;
3022 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
3024 /* @@ Should we call comp_target_types here? */
3025 inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
3026 if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
3027 basetype = inst_ptr_basetype;
3030 instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
3031 if (instance_ptr == error_mark_node)
3032 return error_mark_node;
3036 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
3037 not updated, so we use `basetype' instead. */
3038 if (basetype_path == NULL_TREE
3039 && IS_SIGNATURE (basetype))
3040 basetype_path = TYPE_BINFO (basetype);
3041 else if (basetype_path == NULL_TREE ||
3042 BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
3043 basetype_path = TYPE_BINFO (inst_ptr_basetype);
3045 result = build_field_call (basetype_path, instance_ptr, name, parms);
3049 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
3051 if (TREE_SIDE_EFFECTS (instance_ptr))
3053 /* This action is needed because the instance is needed
3054 for providing the base of the virtual function table.
3055 Without using a SAVE_EXPR, the function we are building
3056 may be called twice, or side effects on the instance
3057 variable (such as a post-increment), may happen twice. */
3058 instance_ptr = save_expr (instance_ptr);
3059 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3061 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
3063 /* This happens when called for operator new (). */
3064 instance = build_indirect_ref (instance, NULL_PTR);
3067 need_vtbl = maybe_needed;
3071 if (TYPE_SIZE (basetype) == 0)
3073 /* This is worth complaining about, I think. */
3074 cp_error ("cannot lookup method in incomplete type `%T'", basetype);
3075 return error_mark_node;
3078 save_basetype = basetype;
3081 if (all_virtual == 1
3082 && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
3083 OPERATOR_METHOD_LENGTH)
3084 || instance_ptr == NULL_TREE
3085 || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0)))
3090 for (parmtypes = NULL_TREE, parm = parms; parm; parm = TREE_CHAIN (parm))
3092 tree t = TREE_TYPE (TREE_VALUE (parm));
3093 if (TREE_CODE (t) == OFFSET_TYPE)
3095 /* Convert OFFSET_TYPE entities to their normal selves. */
3096 TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
3097 t = TREE_TYPE (TREE_VALUE (parm));
3099 if (TREE_CODE (t) == ARRAY_TYPE)
3101 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3102 This eliminates needless calls to `compute_conversion_costs'. */
3103 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
3104 t = TREE_TYPE (TREE_VALUE (parm));
3106 if (t == error_mark_node)
3107 return error_mark_node;
3108 last = build_tree_list (NULL_TREE, t);
3109 parmtypes = chainon (parmtypes, last);
3114 constp = TREE_READONLY (instance);
3115 volatilep = TREE_THIS_VOLATILE (instance);
3116 parms = tree_cons (NULL_TREE, instance_ptr, parms);
3120 /* Raw constructors are always in charge. */
3121 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3122 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3124 flags |= LOOKUP_HAS_IN_CHARGE;
3125 parms = tree_cons (NULL_TREE, integer_one_node, parms);
3126 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
3129 if (flag_this_is_variable > 0)
3133 parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
3139 instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
3140 if (instance_ptr == error_mark_node)
3141 return error_mark_node;
3142 instance_ptr = save_expr (instance_ptr);
3143 TREE_CALLS_NEW (instance_ptr) = 1;
3144 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3146 /* If it's a default argument initialized from a ctor, what we get
3147 from instance_ptr will match the arglist for the FUNCTION_DECL
3148 of the constructor. */
3149 if (parms && TREE_CODE (TREE_VALUE (parms)) == CALL_EXPR
3150 && TREE_OPERAND (TREE_VALUE (parms), 1)
3151 && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms), 1))))
3152 parms = build_tree_list (NULL_TREE, instance_ptr);
3154 parms = tree_cons (NULL_TREE, instance_ptr, parms);
3157 parmtypes = tree_cons (NULL_TREE,
3158 build_pointer_type (build_type_variant (basetype, constp, volatilep)),
3160 if (last == NULL_TREE)
3163 /* Look up function name in the structure type definition. */
3165 if ((IDENTIFIER_HAS_TYPE_VALUE (name)
3166 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name))
3167 && TREE_CODE(IDENTIFIER_TYPE_VALUE (name)) != UNINSTANTIATED_P_TYPE)
3168 || name == constructor_name (basetype))
3170 tree tmp = NULL_TREE;
3171 if (IDENTIFIER_TYPE_VALUE (name) == basetype
3172 || name == constructor_name (basetype))
3173 tmp = TYPE_BINFO (basetype);
3175 tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
3177 if (tmp != NULL_TREE)
3179 name_kind = "constructor";
3181 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
3182 && ! (flags & LOOKUP_HAS_IN_CHARGE))
3184 /* Constructors called for initialization
3185 only are never in charge. */
3188 flags |= LOOKUP_HAS_IN_CHARGE;
3189 tmplist = tree_cons (NULL_TREE, integer_zero_node,
3190 TREE_CHAIN (parms));
3191 TREE_CHAIN (parms) = tmplist;
3192 tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
3193 TREE_CHAIN (parmtypes) = tmplist;
3195 basetype = BINFO_TYPE (tmp);
3198 name_kind = "method";
3201 name_kind = "method";
3203 if (basetype_path == NULL_TREE
3204 || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
3205 basetype_path = TYPE_BINFO (basetype);
3206 result = lookup_fnfields (basetype_path, name,
3207 (flags & LOOKUP_COMPLAIN));
3208 if (result == error_mark_node)
3209 return error_mark_node;
3212 /* Now, go look for this method name. We do not find destructors here.
3214 Putting `void_list_node' on the end of the parmtypes
3215 fakes out `build_decl_overload' into doing the right thing. */
3216 TREE_CHAIN (last) = void_list_node;
3217 method_name = build_decl_overload (name, parmtypes,
3218 1 + (name == constructor_name (save_basetype)
3219 || name == constructor_name_full (save_basetype)));
3220 TREE_CHAIN (last) = NULL_TREE;
3222 for (pass = 0; pass < 2; pass++)
3224 struct candidate *candidates;
3225 struct candidate *cp;
3229 /* This increments every time we go up the type hierarchy.
3230 The idea is to prefer a function of the derived class if possible. */
3238 = (struct candidate *) alloca ((ever_seen+1)
3239 * sizeof (struct candidate));
3240 bzero (candidates, (ever_seen + 1) * sizeof (struct candidate));
3242 len = list_length (parms);
3245 /* First see if a global function has a shot at it. */
3246 if (flags & LOOKUP_GLOBAL)
3249 tree parm = TREE_VALUE (parms);
3251 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
3252 friend_parms = parms;
3253 else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
3256 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
3257 new_type = build_reference_type (TREE_TYPE (parm));
3258 /* It is possible that this should go down a layer. */
3259 new_type = build_type_variant (new_type,
3260 TREE_READONLY (parm),
3261 TREE_THIS_VOLATILE (parm));
3262 parm = convert (new_type, parm);
3263 friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
3266 my_friendly_abort (167);
3269 if (flag_ansi_overloading)
3270 cp->v.ansi_harshness = (struct harshness_code *)
3271 alloca ((len + 1) * sizeof (struct harshness_code));
3273 cp->v.old_harshness = (unsigned short *)
3274 alloca ((len + 1) * sizeof (unsigned short));
3276 result = build_overload_call (name, friend_parms, 0, cp);
3277 /* If it turns out to be the one we were actually looking for
3278 (it was probably a friend function), the return the
3280 if (TREE_CODE (result) == CALL_EXPR)
3283 if (flag_ansi_overloading)
3284 while ((cp->h.code & EVIL_CODE) == 0)
3286 /* non-standard uses: set the field to 0 to indicate
3287 we are using a non-member function. */
3289 if (cp->v.ansi_harshness[len].distance == 0
3290 && cp->h.code < best)
3295 while (cp->evil == 0)
3297 /* non-standard uses: set the field to 0 to indicate
3298 we are using a non-member function. */
3300 if (cp->v.old_harshness[len] == 0
3301 && cp->v.old_harshness[len] == 0
3302 && cp->ellipsis == 0 && cp->user == 0 && cp->b_or_d == 0
3312 /* We have a hit (of sorts). If the parameter list is
3313 "error_mark_node", or some variant thereof, it won't
3314 match any methods. Since we have verified that the is
3315 some method vaguely matching this one (in name at least),
3318 Don't stop for friends, however. */
3319 basetype_path = TREE_PURPOSE (baselink);
3321 function = TREE_VALUE (baselink);
3322 if (TREE_CODE (basetype_path) == TREE_LIST)
3323 basetype_path = TREE_VALUE (basetype_path);
3324 basetype = BINFO_TYPE (basetype_path);
3326 /* Cast the instance variable to the appropriate type. */
3327 TREE_VALUE (parms) = convert_force (TYPE_POINTER_TO (basetype),
3329 /* FIXME: this is the wrong place to get an error. Hopefully
3330 the access-control rewrite will make this change more cleanly. */
3331 if (TREE_VALUE (parms) == error_mark_node)
3332 return error_mark_node;
3334 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
3335 function = DECL_CHAIN (function);
3337 for (; function; function = DECL_CHAIN (function))
3339 #ifdef GATHER_STATISTICS
3340 n_inner_fields_searched++;
3344 found_fns = tree_cons (NULL_TREE, function, found_fns);
3346 /* Not looking for friends here. */
3347 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
3348 && ! DECL_STATIC_FUNCTION_P (function))
3352 && DECL_ASSEMBLER_NAME (function) == method_name)
3357 tree these_parms = parms;
3359 #ifdef GATHER_STATISTICS
3360 n_inner_fields_searched++;
3363 if (flag_ansi_overloading)
3364 cp->v.ansi_harshness = (struct harshness_code *)
3365 alloca ((len + 1) * sizeof (struct harshness_code));
3367 cp->v.old_harshness = (unsigned short *)
3368 alloca ((len + 1) * sizeof (unsigned short));
3370 if (DECL_STATIC_FUNCTION_P (function))
3371 these_parms = TREE_CHAIN (these_parms);
3372 compute_conversion_costs (function, these_parms, cp, len);
3374 if (!flag_ansi_overloading)
3375 cp->b_or_d += b_or_d;
3377 if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE) == 0)
3378 || (!flag_ansi_overloading && cp->evil == 0))
3380 cp->u.field = function;
3381 cp->function = function;
3382 cp->basetypes = basetype_path;
3384 /* No "two-level" conversions. */
3385 if (flags & LOOKUP_NO_CONVERSION
3386 && ((flag_ansi_overloading
3387 && (cp->h.code & USER_CODE))
3388 || (!flag_ansi_overloading
3392 /* If we used default parameters, we must
3393 check to see whether anyone else might
3394 use them also, and report a possible
3396 if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
3397 && ((flag_ansi_overloading
3398 && cp->v.ansi_harshness[len].distance == 0
3399 && cp->h.code < best)
3400 || (!flag_ansi_overloading
3401 && cp->v.old_harshness[len] == 0
3402 && CONST_HARSHNESS (cp->v.old_harshness[0]) == 0
3403 && cp->ellipsis == 0 && cp->user == 0 && cp->b_or_d == 0
3404 && cp->easy < best)))
3406 if (! DECL_STATIC_FUNCTION_P (function))
3407 TREE_VALUE (parms) = cp->arg;
3409 goto found_and_maybe_warn;
3415 /* Now we have run through one link's member functions.
3416 arrange to head-insert this link's links. */
3417 baselink = next_baselink (baselink);
3419 /* Don't grab functions from base classes. lookup_fnfield will
3420 do the work to get us down into the right place. */
3421 baselink = NULL_TREE;
3425 tree igv = IDENTIFIER_GLOBAL_VALUE (name);
3427 /* No exact match could be found. Now try to find match
3428 using default conversions. */
3429 if ((flags & LOOKUP_GLOBAL) && igv)
3431 if (TREE_CODE (igv) == FUNCTION_DECL)
3433 else if (TREE_CODE (igv) == TREE_LIST)
3434 ever_seen += count_functions (igv);
3439 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3440 == LOOKUP_SPECULATIVELY)
3443 TREE_CHAIN (last) = void_list_node;
3444 if (flags & LOOKUP_GLOBAL)
3445 cp_error ("no global or member function `%D(%A)' defined",
3448 cp_error ("no member function `%T::%D(%A)' defined",
3449 save_basetype, name, TREE_CHAIN (parmtypes));
3450 return error_mark_node;
3455 if (cp - candidates != 0)
3457 /* Rank from worst to best. Then cp will point to best one.
3458 Private fields have their bits flipped. For unsigned
3459 numbers, this should make them look very large.
3460 If the best alternate has a (signed) negative value,
3461 then all we ever saw were private members. */
3462 if (cp - candidates > 1)
3464 cp = ideal_candidate (save_basetype, candidates,
3465 cp - candidates, parms, len);
3466 if (cp == (struct candidate *)0)
3468 cp_error ("ambiguous type conversion requested for %s `%D'",
3470 return error_mark_node;
3472 if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE))
3473 || (!flag_ansi_overloading && cp->evil))
3474 return error_mark_node;
3476 else if ((flag_ansi_overloading && (cp[-1].h.code & EVIL_CODE))
3477 || (!flag_ansi_overloading && cp[-1].evil == 2))
3479 cp_error ("ambiguous type conversion requested for %s `%D'",
3481 return error_mark_node;
3486 /* The global function was the best, so use it. */
3487 if (cp->u.field == 0)
3489 /* We must convert the instance pointer into a reference type.
3490 Global overloaded functions can only either take
3491 aggregate objects (which come for free from references)
3492 or reference data types anyway. */
3493 TREE_VALUE (parms) = copy_node (instance_ptr);
3494 TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
3495 return build_function_call (cp->function, parms);
3498 function = cp->function;
3499 basetype_path = cp->basetypes;
3500 if (! DECL_STATIC_FUNCTION_P (function))
3501 TREE_VALUE (parms) = cp->arg;
3502 goto found_and_maybe_warn;
3505 if ((flags & ~LOOKUP_GLOBAL) & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
3507 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3508 == LOOKUP_SPECULATIVELY)
3511 if (DECL_STATIC_FUNCTION_P (cp->function))
3512 parms = TREE_CHAIN (parms);
3515 if (flags & LOOKUP_SPECULATIVELY)
3517 if (static_call_context
3518 && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
3519 cp_error ("object missing in call to `%D'", cp->function);
3520 else if (ever_seen > 1)
3522 TREE_CHAIN (last) = void_list_node;
3523 cp_error ("no matching function for call to `%T::%D (%A)'",
3524 TREE_TYPE (TREE_TYPE (instance_ptr)),
3525 name, TREE_CHAIN (parmtypes));
3526 TREE_CHAIN (last) = NULL_TREE;
3527 print_candidates (found_fns);
3530 report_type_mismatch (cp, parms, name_kind);
3531 return error_mark_node;
3534 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
3537 cp_error ("%T has no method named %D", save_basetype, name);
3538 return error_mark_node;
3544 found_and_maybe_warn:
3545 if ((flag_ansi_overloading
3546 && (cp->v.ansi_harshness[0].code & CONST_CODE))
3547 || (!flag_ansi_overloading
3548 && CONST_HARSHNESS (cp->v.old_harshness[0])))
3550 if (flags & LOOKUP_COMPLAIN)
3552 cp_error_at ("non-const member function `%D'", cp->function);
3553 error ("called for const object at this point in file");
3555 /* Not good enough for a match. */
3557 return error_mark_node;
3561 /* Silently return error_mark_node. */
3562 return error_mark_node;
3565 if (flags & LOOKUP_PROTECT)
3566 access = compute_access (basetype_path, function);
3568 if (access == access_private)
3570 if (flags & LOOKUP_COMPLAIN)
3572 cp_error_at ("%s `%+#D' is %s", name_kind, function,
3573 TREE_PRIVATE (function) ? "private"
3574 : "from private base class");
3575 error ("within this context");
3577 return error_mark_node;
3579 else if (access == access_protected)
3581 if (flags & LOOKUP_COMPLAIN)
3583 cp_error_at ("%s `%+#D' %s", name_kind, function,
3584 TREE_PROTECTED (function) ? "is protected"
3585 : "has protected accessibility");
3586 error ("within this context");
3588 return error_mark_node;
3591 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
3592 type (if it exists) is a pointer to. */
3594 if (IS_SIGNATURE (basetype) && static_call_context)
3596 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
3598 return error_mark_node;
3600 else if (IS_SIGNATURE (basetype))
3601 return build_signature_method_call (basetype, instance, function, parms);
3603 function = DECL_MAIN_VARIANT (function);
3604 /* Declare external function if necessary. */
3605 assemble_external (function);
3607 fntype = TREE_TYPE (function);
3608 if (TREE_CODE (fntype) == POINTER_TYPE)
3609 fntype = TREE_TYPE (fntype);
3610 basetype = DECL_CLASS_CONTEXT (function);
3612 /* If we are referencing a virtual function from an object
3613 of effectively static type, then there is no need
3614 to go through the virtual function table. */
3615 if (need_vtbl == maybe_needed)
3617 int fixed_type = resolves_to_fixed_type_p (instance, 0);
3619 if (all_virtual == 1
3620 && DECL_VINDEX (function)
3621 && may_be_remote (basetype))
3623 else if (DECL_VINDEX (function))
3624 need_vtbl = fixed_type ? unneeded : needed;
3626 need_vtbl = not_needed;
3629 if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
3630 && !DECL_CONSTRUCTOR_P (function))
3632 /* Let's be nice to the user for now, and give reasonable
3633 default behavior. */
3634 instance_ptr = current_class_decl;
3637 if (basetype != current_class_type)
3639 tree binfo = get_binfo (basetype, current_class_type, 1);
3640 if (binfo == NULL_TREE)
3642 error_not_base_type (function, current_class_type);
3643 return error_mark_node;
3645 else if (basetype == error_mark_node)
3646 return error_mark_node;
3649 /* Only allow a static member function to call another static member
3651 else if (DECL_LANG_SPECIFIC (function)
3652 && !DECL_STATIC_FUNCTION_P (function))
3654 cp_error ("cannot call member function `%D' without object",
3656 return error_mark_node;
3660 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3662 if (TYPE_SIZE (value_type) == 0)
3664 if (flags & LOOKUP_COMPLAIN)
3665 incomplete_type_error (0, value_type);
3666 return error_mark_node;
3669 /* We do not pass FUNCTION into `convert_arguments', because by
3670 now everything should be ok. If not, then we have a serious error. */
3671 if (DECL_STATIC_FUNCTION_P (function))
3672 parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
3673 TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
3674 else if (need_vtbl == unneeded)
3676 int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
3677 basetype = TREE_TYPE (instance);
3678 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function)) != TYPE_MAIN_VARIANT (basetype)
3679 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
3681 basetype = DECL_CLASS_CONTEXT (function);
3682 instance_ptr = convert_pointer_to (basetype, instance_ptr);
3683 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3685 parms = tree_cons (NULL_TREE, instance_ptr,
3686 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
3690 if ((flags & LOOKUP_NONVIRTUAL) == 0)
3691 basetype = DECL_CONTEXT (function);
3693 /* First parm could be integer_zerop with casts like
3694 ((Object*)0)->Object::IsA() */
3695 if (!integer_zerop (TREE_VALUE (parms)))
3697 /* Since we can't have inheritance with a union, doing get_binfo
3698 on it won't work. We do all the convert_pointer_to_real
3699 stuff to handle MI correctly...for unions, that's not
3700 an issue, so we must short-circuit that extra work here. */
3701 tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
3702 if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
3703 instance_ptr = TREE_VALUE (parms);
3706 tree binfo = get_binfo (basetype,
3707 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3709 instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3712 = convert_pointer_to (build_type_variant (basetype,
3716 if (TREE_CODE (instance_ptr) == COND_EXPR)
3718 instance_ptr = save_expr (instance_ptr);
3719 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3721 else if (TREE_CODE (instance_ptr) == NOP_EXPR
3722 && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
3723 && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
3725 /* The call to `convert_pointer_to' may return error_mark_node. */
3726 else if (TREE_CODE (instance_ptr) == ERROR_MARK)
3727 return instance_ptr;
3728 else if (instance == NULL_TREE
3729 || TREE_CODE (instance) != INDIRECT_REF
3730 || TREE_OPERAND (instance, 0) != instance_ptr)
3731 instance = build_indirect_ref (instance_ptr, NULL_PTR);
3733 parms = tree_cons (NULL_TREE, instance_ptr,
3734 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL));
3738 /* Constructors do not overload method calls. */
3739 else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
3740 && name != TYPE_IDENTIFIER (basetype)
3741 && (TREE_CODE (function) != FUNCTION_DECL
3742 || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)),
3743 OPERATOR_METHOD_FORMAT,
3744 OPERATOR_METHOD_LENGTH))
3745 && (may_be_remote (basetype) || instance != C_C_D))
3749 parms = TREE_CHAIN (parms);
3751 if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
3752 fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
3754 fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
3755 if (all_virtual == 1)
3756 fn_as_int = convert (integer_type_node, fn_as_int);
3758 result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
3760 if (result == NULL_TREE)
3762 compiler_error ("could not overload `operator->()(...)'");
3763 return error_mark_node;
3765 else if (result == error_mark_node)
3766 return error_mark_node;
3769 /* Do this if we want the result of operator->() to inherit
3770 the type of the function it is subbing for. */
3771 TREE_TYPE (result) = value_type;
3778 if (need_vtbl == needed)
3780 function = build_vfn_ref (&TREE_VALUE (parms), instance, DECL_VINDEX (function));
3781 TREE_TYPE (function) = build_pointer_type (fntype);
3784 if (TREE_CODE (function) == FUNCTION_DECL)
3785 GNU_xref_call (current_function_decl,
3786 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
3791 if (TREE_CODE (function) == FUNCTION_DECL)
3793 is_constructor = DECL_CONSTRUCTOR_P (function);
3794 if (DECL_INLINE (function))
3795 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
3798 assemble_external (function);
3799 TREE_USED (function) = 1;
3800 function = default_conversion (function);
3806 function = default_conversion (function);
3809 result = build_nt (CALL_EXPR, function, parms, NULL_TREE);
3811 TREE_TYPE (result) = value_type;
3812 TREE_SIDE_EFFECTS (result) = 1;
3813 TREE_RAISES (result)
3814 = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
3815 TREE_HAS_CONSTRUCTOR (result) = is_constructor;
3820 /* Similar to `build_method_call', but for overloaded non-member functions.
3821 The name of this function comes through NAME. The name depends
3824 Note that this function must handle simple `C' promotions,
3825 as well as variable numbers of arguments (...), and
3826 default arguments to boot.
3828 If the overloading is successful, we return a tree node which
3829 contains the call to the function.
3831 If overloading produces candidates which are probable, but not definite,
3832 we hold these candidates. If FINAL_CP is non-zero, then we are free
3833 to assume that final_cp points to enough storage for all candidates that
3834 this function might generate. The `harshness' array is preallocated for
3835 the first candidate, but not for subsequent ones.
3837 Note that the DECL_RTL of FUNCTION must be made to agree with this
3838 function's new name. */
3841 build_overload_call_real (fnname, parms, flags, final_cp, buildxxx)
3844 struct candidate *final_cp;
3847 /* must check for overloading here */
3848 tree overload_name, functions, function, parm;
3849 tree parmtypes = NULL_TREE, last = NULL_TREE;
3850 register tree outer;
3852 int parmlength = list_length (parms);
3854 struct candidate *candidates, *cp;
3858 if (flag_ansi_overloading)
3860 final_cp[0].h.code = 0;
3861 final_cp[0].h.distance = 0;
3862 final_cp[0].function = 0;
3864 final_cp[1].h.code = EVIL_CODE;
3868 final_cp[0].evil = 0;
3869 final_cp[0].user = 0;
3870 final_cp[0].b_or_d = 0;
3871 final_cp[0].easy = 0;
3872 final_cp[0].function = 0;
3874 final_cp[1].evil = 1;
3878 for (parm = parms; parm; parm = TREE_CHAIN (parm))
3880 register tree t = TREE_TYPE (TREE_VALUE (parm));
3882 if (t == error_mark_node)
3886 if (flag_ansi_overloading)
3887 final_cp->h.code = EVIL_CODE;
3891 return error_mark_node;
3893 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
3895 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3896 Also convert OFFSET_TYPE entities to their normal selves.
3897 This eliminates needless calls to `compute_conversion_costs'. */
3898 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
3899 t = TREE_TYPE (TREE_VALUE (parm));
3901 last = build_tree_list (NULL_TREE, t);
3902 parmtypes = chainon (parmtypes, last);
3905 TREE_CHAIN (last) = void_list_node;
3907 parmtypes = void_list_node;
3909 if (! flag_ansi_overloading)
3911 /* This is a speed improvement that ends up not working properly in
3912 the situation of fns with and without default parameters. I turned
3913 this off in the new method so it'll go through the argument matching
3914 code to properly diagnose a match/failure. (bpk) */
3915 overload_name = build_decl_overload (fnname, parmtypes, 0);
3917 /* Now check to see whether or not we can win.
3918 Note that if we are called from `build_method_call',
3919 then we cannot have a mis-match, because we would have
3920 already found such a winning case. */
3922 if (IDENTIFIER_GLOBAL_VALUE (overload_name))
3923 if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (overload_name)) != TREE_LIST)
3924 return build_function_call (DECL_MAIN_VARIANT (IDENTIFIER_GLOBAL_VALUE (overload_name)), parms);
3927 functions = IDENTIFIER_GLOBAL_VALUE (fnname);
3929 if (functions == NULL_TREE)
3931 if (flags & LOOKUP_SPECULATIVELY)
3933 if (flags & LOOKUP_COMPLAIN)
3934 error ("only member functions apply");
3937 if (flag_ansi_overloading)
3938 final_cp->h.code = EVIL_CODE;
3942 return error_mark_node;
3945 if (! TREE_OVERLOADED (fnname))
3947 functions = DECL_MAIN_VARIANT (functions);
3950 /* We are just curious whether this is a viable alternative or
3952 compute_conversion_costs (functions, parms, final_cp, parmlength);
3956 return build_function_call_real (functions, parms, 1, flags);
3959 if (TREE_CODE (functions) == TREE_LIST
3960 && TREE_VALUE (functions) == NULL_TREE)
3962 if (flags & LOOKUP_SPECULATIVELY)
3965 if (flags & LOOKUP_COMPLAIN)
3966 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
3967 TREE_PURPOSE (functions));
3970 if (flag_ansi_overloading)
3971 final_cp->h.code = EVIL_CODE;
3975 return error_mark_node;
3978 length = count_functions (functions);
3981 candidates = final_cp;
3985 = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
3986 bzero (candidates, (length + 1) * sizeof (struct candidate));
3991 my_friendly_assert (is_overloaded_fn (functions), 169);
3993 functions = get_first_fn (functions);
3995 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
3996 for (outer = functions; outer; outer = DECL_CHAIN (outer))
3998 int template_cost = 0;
4000 if (TREE_CODE (function) != FUNCTION_DECL
4001 && ! (TREE_CODE (function) == TEMPLATE_DECL
4002 && ! DECL_TEMPLATE_IS_CLASS (function)
4003 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
4005 enum tree_code code = TREE_CODE (function);
4006 if (code == TEMPLATE_DECL)
4007 code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
4008 if (code == CONST_DECL)
4010 ("enumeral value `%D' conflicts with function of same name",
4012 else if (code == VAR_DECL)
4014 if (TREE_STATIC (function))
4016 ("variable `%D' conflicts with function of same name",
4020 ("constant field `%D' conflicts with function of same name",
4023 else if (code == TYPE_DECL)
4026 my_friendly_abort (2);
4027 error ("at this point in file");
4030 if (TREE_CODE (function) == TEMPLATE_DECL)
4032 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
4033 tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
4036 i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
4037 TYPE_ARG_TYPES (TREE_TYPE (function)),
4038 parms, &template_cost, 0);
4041 struct candidate *cp2;
4043 function = instantiate_template (function, targs);
4044 /* Now check that the template instantiated for this is not
4045 the same as a function that's in the list due to some
4046 previous instantiation. */
4049 if (cp2->function == function)
4053 if (cp2->function == function)
4058 if (TREE_CODE (function) == TEMPLATE_DECL)
4060 /* Unconverted template -- failed match. */
4061 cp->function = function;
4063 if (flag_ansi_overloading)
4064 cp->h.code = EVIL_CODE;
4070 function = DECL_MAIN_VARIANT (function);
4072 /* Can't use alloca here, since result might be
4073 passed to calling function. */
4074 cp->h_len = parmlength;
4075 if (flag_ansi_overloading)
4076 cp->v.ansi_harshness = (struct harshness_code *)
4077 oballoc ((parmlength + 1) * sizeof (struct harshness_code));
4079 cp->v.old_harshness = (unsigned short *)
4080 oballoc ((parmlength + 1) * sizeof (unsigned short));
4082 compute_conversion_costs (function, parms, cp, parmlength);
4084 if (flag_ansi_overloading)
4085 /* Make sure this is clear as well. */
4086 cp->h.int_penalty += template_cost;
4088 /* Should really add another field... */
4089 cp->easy = cp->easy * 128 + template_cost;
4091 /* It seemed easier to have both if stmts in here, rather
4092 than excluding the hell out of it with flag_ansi_overloading
4093 everywhere. (bpk) */
4094 if (flag_ansi_overloading)
4096 if ((cp[0].h.code & EVIL_CODE) == 0)
4098 cp[1].h.code = EVIL_CODE;
4100 /* int_penalty is set by convert_harshness_ansi for cases
4101 where we need to know about any penalties that would
4102 otherwise make a TRIVIAL_CODE pass. */
4104 && template_cost == 0
4105 && cp[0].h.code <= TRIVIAL_CODE
4106 && cp[0].h.int_penalty == 0)
4108 final_cp[0].h = cp[0].h;
4116 if (cp[0].evil == 0)
4120 && cp[0].user == 0 && cp[0].b_or_d == 0
4121 && template_cost == 0
4124 final_cp[0].easy = cp[0].easy;
4133 if (cp - candidates)
4135 tree rval = error_mark_node;
4138 if (flag_ansi_overloading)
4139 cp[0].h.code = EVIL_CODE;
4142 if (cp - candidates > 1)
4144 struct candidate *best_cp
4145 = ideal_candidate (NULL_TREE, candidates,
4146 cp - candidates, parms, parmlength);
4147 if (best_cp == (struct candidate *)0)
4149 if (flags & LOOKUP_COMPLAIN)
4151 cp_error ("call of overloaded `%D' is ambiguous", fnname);
4152 print_n_candidates (candidates, cp - candidates);
4154 return error_mark_node;
4157 rval = best_cp->function;
4162 if ((flag_ansi_overloading && (cp->h.code & EVIL_CODE))
4163 || (!flag_ansi_overloading && cp->evil > 1))
4165 if (flags & LOOKUP_COMPLAIN)
4166 error ("type conversion ambiguous");
4169 rval = cp->function;
4175 return buildxxx ? build_function_call_real (rval, parms, 0, flags)
4176 : build_function_call_real (rval, parms, 1, flags);
4179 if (flags & LOOKUP_SPECULATIVELY)
4182 if (flags & LOOKUP_COMPLAIN)
4183 report_type_mismatch (cp, parms, "function",
4184 decl_as_string (cp->function, 1));
4186 return error_mark_node;
4190 build_overload_call (fnname, parms, flags, final_cp)
4193 struct candidate *final_cp;
4195 return build_overload_call_real (fnname, parms, flags, final_cp, 0);
4199 build_overload_call_maybe (fnname, parms, flags, final_cp)
4202 struct candidate *final_cp;
4204 return build_overload_call_real (fnname, parms, flags, final_cp, 1);