1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 hacked by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* High-level class interface. */
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
38 extern int inhibit_warnings
;
39 extern int flag_assume_nonnull_objects
;
40 extern tree ctor_label
, dtor_label
;
43 extern tree
unary_complex_lvalue ();
45 /* Compute the ease with which a conversion can be performed
46 between an expected and the given type. */
47 static int convert_harshness_old ();
48 static struct harshness_code
convert_harshness_ansi ();
51 /* Note the old method also uses USER_HARSHNESS, BASE_DERIVED_HARSHNESS,
55 #define EVIL_HARSHNESS(ARG) ((ARG) & 1)
56 #define ELLIPSIS_HARSHNESS(ARG) ((ARG) & 2)
57 #define CONTRAVARIANT_HARSHNESS(ARG) ((ARG) & 8)
58 #define INT_TO_BD_HARSHNESS(ARG) (((ARG) << 5) | 16)
59 #define INT_FROM_BD_HARSHNESS(ARG) ((ARG) >> 5)
60 #define INT_TO_EASY_HARSHNESS(ARG) ((ARG) << 5)
61 #define INT_FROM_EASY_HARSHNESS(ARG) ((ARG) >> 5)
62 #define ONLY_EASY_HARSHNESS(ARG) (((ARG) & 31) == 0)
66 #define EVIL_RETURN(ARG) ((ARG).code = EVIL_CODE, (ARG))
67 #define QUAL_RETURN(ARG) ((ARG).code = QUAL_CODE, (ARG))
68 #define TRIVIAL_RETURN(ARG) ((ARG).code = TRIVIAL_CODE, (ARG))
69 #define ZERO_RETURN(ARG) ((ARG).code = 0, (ARG))
71 #define USER_HARSHNESS(ARG) ((ARG) & 4)
72 #define BASE_DERIVED_HARSHNESS(ARG) ((ARG) & 16)
73 #define CONST_HARSHNESS(ARG) ((ARG) & 2048)
75 /* Ordering function for overload resolution. Compare two candidates
78 rank_for_overload_ansi (x
, y
)
79 struct candidate
*x
, *y
;
81 if (y
->h
.code
& (EVIL_CODE
|ELLIPSIS_CODE
|USER_CODE
))
82 return y
->h
.code
- x
->h
.code
;
83 if (x
->h
.code
& (EVIL_CODE
|ELLIPSIS_CODE
|USER_CODE
))
86 /* This is set by compute_conversion_costs, for calling a non-const
87 member function from a const member function. */
88 if ((y
->v
.ansi_harshness
[0].code
& CONST_CODE
) ^ (x
->v
.ansi_harshness
[0].code
& CONST_CODE
))
89 return y
->v
.ansi_harshness
[0].code
- x
->v
.ansi_harshness
[0].code
;
91 if (y
->h
.code
& STD_CODE
)
93 if (x
->h
.code
& STD_CODE
)
94 return y
->h
.distance
- x
->h
.distance
;
97 if (x
->h
.code
& STD_CODE
)
100 return y
->h
.code
- x
->h
.code
;
104 rank_for_overload_old (x
, y
)
105 struct candidate
*x
, *y
;
107 if (y
->evil
- x
->evil
)
108 return y
->evil
- x
->evil
;
109 if (CONST_HARSHNESS (y
->v
.old_harshness
[0]) ^ CONST_HARSHNESS (x
->v
.old_harshness
[0]))
110 return y
->v
.old_harshness
[0] - x
->v
.old_harshness
[0];
111 if (y
->ellipsis
- x
->ellipsis
)
112 return y
->ellipsis
- x
->ellipsis
;
113 if (y
->user
- x
->user
)
114 return y
->user
- x
->user
;
115 if (y
->b_or_d
- x
->b_or_d
)
116 return y
->b_or_d
- x
->b_or_d
;
117 return y
->easy
- x
->easy
;
121 rank_for_overload (x
, y
)
122 struct candidate
*x
, *y
;
124 if (flag_ansi_overloading
)
125 return rank_for_overload_ansi (x
, y
);
127 return rank_for_overload_old (x
, y
);
130 /* Compare two candidates, argument by argument. */
132 rank_for_ideal (x
, y
)
133 struct candidate
*x
, *y
;
137 if (x
->h_len
!= y
->h_len
)
140 for (i
= 0; i
< x
->h_len
; i
++)
142 if (y
->v
.ansi_harshness
[i
].code
- x
->v
.ansi_harshness
[i
].code
)
143 return y
->v
.ansi_harshness
[i
].code
- x
->v
.ansi_harshness
[i
].code
;
144 if ((y
->v
.ansi_harshness
[i
].code
& STD_CODE
)
145 && (y
->v
.ansi_harshness
[i
].distance
- x
->v
.ansi_harshness
[i
].distance
))
146 return y
->v
.ansi_harshness
[i
].distance
- x
->v
.ansi_harshness
[i
].distance
;
148 /* They're both the same code. Now see if we're dealing with an
149 integral promotion that needs a finer grain of accuracy. */
150 if (y
->v
.ansi_harshness
[0].code
& PROMO_CODE
151 && (y
->v
.ansi_harshness
[i
].int_penalty
^ x
->v
.ansi_harshness
[i
].int_penalty
))
152 return y
->v
.ansi_harshness
[i
].int_penalty
- x
->v
.ansi_harshness
[i
].int_penalty
;
157 /* TYPE is the type we wish to convert to. PARM is the parameter
158 we have to work with. We use a somewhat arbitrary cost function
159 to measure this conversion. */
160 static struct harshness_code
161 convert_harshness_ansi (type
, parmtype
, parm
)
162 register tree type
, parmtype
;
165 struct harshness_code h
;
166 register enum tree_code codel
;
167 register enum tree_code coder
;
173 #ifdef GATHER_STATISTICS
174 n_convert_harshness
++;
177 if (TYPE_PTRMEMFUNC_P (type
))
178 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
179 if (TYPE_PTRMEMFUNC_P (parmtype
))
180 parmtype
= TYPE_PTRMEMFUNC_FN_TYPE (parmtype
);
182 codel
= TREE_CODE (type
);
183 coder
= TREE_CODE (parmtype
);
185 if (TYPE_MAIN_VARIANT (parmtype
) == TYPE_MAIN_VARIANT (type
))
186 return ZERO_RETURN (h
);
188 if (coder
== ERROR_MARK
)
189 return EVIL_RETURN (h
);
191 if (codel
== POINTER_TYPE
&& fntype_p (parmtype
))
194 struct harshness_code h1
, h2
;
196 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
197 type
= TREE_TYPE (type
);
199 if (coder
== POINTER_TYPE
)
201 parmtype
= TREE_TYPE (parmtype
);
202 coder
= TREE_CODE (parmtype
);
205 if (coder
!= TREE_CODE (type
))
206 return EVIL_RETURN (h
);
208 /* We allow the default conversion between function type
209 and pointer-to-function type for free. */
210 if (type
== parmtype
)
211 return ZERO_RETURN (h
);
213 /* Compare return types. */
214 p1
= TREE_TYPE (type
);
215 p2
= TREE_TYPE (parmtype
);
216 h2
= convert_harshness_ansi (p1
, p2
, NULL_TREE
);
217 if (h2
.code
& EVIL_CODE
)
220 h1
.code
= TRIVIAL_CODE
;
223 if (h2
.distance
!= 0)
227 /* This only works for pointers. */
228 if (TREE_CODE (p1
) != POINTER_TYPE
229 && TREE_CODE (p1
) != REFERENCE_TYPE
)
230 return EVIL_RETURN (h
);
234 /* Don't die if we happen to be dealing with void*. */
235 if (!IS_AGGR_TYPE (p1
) || !IS_AGGR_TYPE (p2
))
236 return EVIL_RETURN (h
);
238 binfo
= get_binfo (p2
, p1
, 0);
240 binfo
= get_binfo (p1
, p2
, 0);
242 if (! BINFO_OFFSET_ZEROP (binfo
))
244 static int explained
= 0;
246 message_2_types (sorry
, "cannot cast `%d' to `%d' at function call site", p2
, p1
);
248 message_2_types (sorry
, "cannot cast `%d' to `%d' at function call site", p1
, p2
);
251 sorry ("(because pointer values change during conversion)");
252 return EVIL_RETURN (h
);
257 if (h2
.distance
> h1
.distance
)
258 h1
.distance
= h2
.distance
;
260 p1
= TYPE_ARG_TYPES (type
);
261 p2
= TYPE_ARG_TYPES (parmtype
);
262 while (p1
&& TREE_VALUE (p1
) != void_type_node
263 && p2
&& TREE_VALUE (p2
) != void_type_node
)
265 h2
= convert_harshness_ansi (TREE_VALUE (p1
), TREE_VALUE (p2
),
267 if (h2
.code
& EVIL_CODE
)
272 /* This only works for pointers and references. */
273 if (TREE_CODE (TREE_VALUE (p1
)) != POINTER_TYPE
274 && TREE_CODE (TREE_VALUE (p1
)) != REFERENCE_TYPE
)
275 return EVIL_RETURN (h
);
276 h2
.distance
= - h2
.distance
;
280 if (h2
.distance
> h1
.distance
)
281 h1
.distance
= h2
.distance
;
282 p1
= TREE_CHAIN (p1
);
283 p2
= TREE_CHAIN (p2
);
290 return EVIL_RETURN (h
);
291 h1
.code
|= ELLIPSIS_CODE
;
296 if (TREE_PURPOSE (p1
) == NULL_TREE
)
297 h1
.code
|= EVIL_CODE
;
301 else if (codel
== POINTER_TYPE
&& coder
== OFFSET_TYPE
)
303 /* Get to the OFFSET_TYPE that this might be. */
304 type
= TREE_TYPE (type
);
306 if (coder
!= TREE_CODE (type
))
307 return EVIL_RETURN (h
);
309 if (TYPE_OFFSET_BASETYPE (type
) == TYPE_OFFSET_BASETYPE (parmtype
))
311 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (type
),
312 TYPE_OFFSET_BASETYPE (parmtype
)))
317 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (parmtype
),
318 TYPE_OFFSET_BASETYPE (type
)))
324 return EVIL_RETURN (h
);
325 /* Now test the OFFSET_TYPE's target compatibility. */
326 type
= TREE_TYPE (type
);
327 parmtype
= TREE_TYPE (parmtype
);
330 if (coder
== UNKNOWN_TYPE
)
332 if (codel
== FUNCTION_TYPE
333 || codel
== METHOD_TYPE
334 || (codel
== POINTER_TYPE
335 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
336 || TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)))
337 return TRIVIAL_RETURN (h
);
338 return EVIL_RETURN (h
);
341 if (coder
== VOID_TYPE
)
342 return EVIL_RETURN (h
);
344 if (codel
== ENUMERAL_TYPE
|| codel
== INTEGER_TYPE
)
346 /* Control equivalence of ints an enums. */
348 if (codel
== ENUMERAL_TYPE
349 && flag_int_enum_equivalence
== 0)
351 /* Enums can be converted to ints, but not vice-versa. */
352 if (coder
!= ENUMERAL_TYPE
353 || TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (parmtype
))
354 return EVIL_RETURN (h
);
357 /* else enums and ints (almost) freely interconvert. */
359 if (coder
== INTEGER_TYPE
|| coder
== ENUMERAL_TYPE
)
361 if (TYPE_MAIN_VARIANT (type
)
362 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
365 #if 0 /* What purpose does this serve? -jason */
366 /* A char, short, wchar_t, etc., should promote to an int if
367 it can handle it, otherwise to an unsigned. So we'll make
369 if (type
!= integer_type_node
)
378 else if (coder
== REAL_TYPE
)
386 if (codel
== REAL_TYPE
)
388 if (coder
== REAL_TYPE
)
390 if (TYPE_MAIN_VARIANT (type
)
391 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
398 else if (coder
== INTEGER_TYPE
|| coder
== ENUMERAL_TYPE
)
406 /* Convert arrays which have not previously been converted. */
407 if (codel
== ARRAY_TYPE
)
408 codel
= POINTER_TYPE
;
409 if (coder
== ARRAY_TYPE
)
410 coder
= POINTER_TYPE
;
412 /* Conversions among pointers */
413 if (codel
== POINTER_TYPE
&& coder
== POINTER_TYPE
)
415 register tree ttl
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
416 register tree ttr
= TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
));
417 int penalty
= 4 * (ttl
!= ttr
);
419 /* Anything converts to void *. void * converts to anything.
420 Since these may be `const void *' (etc.) use VOID_TYPE
421 instead of void_type_node. Otherwise, the targets must be the same,
422 except that we do allow (at some cost) conversion between signed and
423 unsigned pointer types. */
425 if ((TREE_CODE (ttl
) == METHOD_TYPE
426 || TREE_CODE (ttl
) == FUNCTION_TYPE
)
427 && TREE_CODE (ttl
) == TREE_CODE (ttr
))
429 if (comptypes (ttl
, ttr
, -1))
431 h
.code
= penalty
? STD_CODE
: 0;
440 if (TREE_CODE (ttl
) != VOID_TYPE
&& TREE_CODE (ttr
) != VOID_TYPE
)
442 if (TREE_UNSIGNED (ttl
) != TREE_UNSIGNED (ttr
))
444 ttl
= unsigned_type (ttl
);
445 ttr
= unsigned_type (ttr
);
448 if (! comp_target_types (ttl
, ttr
, 0))
449 return EVIL_RETURN (h
);
452 if (!(TREE_CODE (ttl
) == VOID_TYPE
453 || TREE_CODE (ttr
) == VOID_TYPE
454 || (TREE_UNSIGNED (ttl
) ^ TREE_UNSIGNED (ttr
)
455 && (ttl
= unsigned_type (ttl
),
456 ttr
= unsigned_type (ttr
),
458 || (comp_target_types (ttl
, ttr
, 0))))
459 return EVIL_RETURN (h
);
462 if (penalty
== 10 || ttr
== ttl
)
464 tree tmp1
= TREE_TYPE (type
), tmp2
= TREE_TYPE (parmtype
);
466 /* If one was unsigned but the other wasn't, then we need to
467 do a standard conversion from T to unsigned T. */
469 h
.code
= PROMO_CODE
; /* was STD_CODE */
473 /* Note conversion from `T*' to `const T*',
474 or `T*' to `volatile T*'. */
476 && ((TYPE_READONLY (tmp1
) != TREE_READONLY (tmp2
))
477 || (TYPE_VOLATILE (tmp1
) != TYPE_VOLATILE (tmp2
))))
485 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
487 int b_or_d
= get_base_distance (ttl
, ttr
, 0, 0);
490 b_or_d
= get_base_distance (ttr
, ttl
, 0, 0);
492 return EVIL_RETURN (h
);
493 h
.distance
= -b_or_d
;
501 /* If converting from a `class*' to a `void*', make it
502 less favorable than any inheritance relationship. */
503 if (TREE_CODE (ttl
) == VOID_TYPE
&& IS_AGGR_TYPE (ttr
))
506 h
.distance
= CLASSTYPE_MAX_DEPTH (ttr
)+1;
509 h
.code
= penalty
? STD_CODE
: PROMO_CODE
;
513 if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
515 /* This is not a bad match, but don't let it beat
516 integer-enum combinations. */
517 if (parm
&& integer_zerop (parm
))
525 /* C++: one of the types must be a reference type. */
528 register tree intype
= TYPE_MAIN_VARIANT (parmtype
);
529 register enum tree_code form
= TREE_CODE (intype
);
532 if (codel
== REFERENCE_TYPE
|| coder
== REFERENCE_TYPE
)
534 ttl
= TYPE_MAIN_VARIANT (type
);
536 if (codel
== REFERENCE_TYPE
)
538 ttl
= TREE_TYPE (ttl
);
540 /* When passing a non-const argument into a const reference,
541 dig it a little, so a non-const reference is preferred over
543 if (parm
&& TREE_READONLY (ttl
) && ! TREE_READONLY (parm
))
548 ttl
= TYPE_MAIN_VARIANT (ttl
);
550 if (form
== OFFSET_TYPE
)
552 intype
= TREE_TYPE (intype
);
553 form
= TREE_CODE (intype
);
556 if (form
== REFERENCE_TYPE
)
558 intype
= TYPE_MAIN_VARIANT (TREE_TYPE (intype
));
561 return ZERO_RETURN (h
);
566 /* Can reference be built up? */
567 if (ttl
== intype
&& penalty
== 0) {
568 /* Because the READONLY and VIRTUAL bits are not always in
569 the type, this extra check is necessary. The problem
570 should be fixed someplace else, and this extra code
573 Also, if type if a reference, the readonly bits could
574 either be in the outer type (with reference) or on the
575 inner type (the thing being referenced). (mrs) */
577 && ((TREE_READONLY (parm
)
578 && ! (TYPE_READONLY (type
)
579 || (TREE_CODE (type
) == REFERENCE_TYPE
580 && TYPE_READONLY (TREE_TYPE (type
)))))
581 || (TREE_SIDE_EFFECTS (parm
)
582 && ! (TYPE_VOLATILE (type
)
583 || (TREE_CODE (type
) == REFERENCE_TYPE
584 && TYPE_VOLATILE (TREE_TYPE (type
)))))))
587 return ZERO_RETURN (h
);
593 else if (form
== REFERENCE_TYPE
)
597 tree tmp
= convert_from_reference (parm
);
598 intype
= TYPE_MAIN_VARIANT (TREE_TYPE (tmp
));
604 intype
= TREE_TYPE (intype
);
605 while (TREE_CODE (intype
) == REFERENCE_TYPE
);
606 intype
= TYPE_MAIN_VARIANT (intype
);
610 return ZERO_RETURN (h
);
615 if (TREE_UNSIGNED (ttl
) ^ TREE_UNSIGNED (intype
))
617 ttl
= unsigned_type (ttl
);
618 intype
= unsigned_type (intype
);
624 /* If the initializer is not an lvalue, then it does not
625 matter if we make life easier for the programmer
626 by creating a temporary variable with which to
628 if (parm
&& (coder
== INTEGER_TYPE
629 || coder
== ENUMERAL_TYPE
630 || coder
== REAL_TYPE
)
631 && ! lvalue_p (parm
))
633 h
= convert_harshness_ansi (ttl
, ttr
, NULL_TREE
);
634 if (penalty
> 2 || h
.code
!= 0)
637 h
.code
|= TRIVIAL_CODE
;
651 h
.code
= TRIVIAL_CODE
;
652 /* We set this here so that build_overload_call_real will be
653 able to see the penalty we found, rather than just looking
654 at a TRIVIAL_CODE with no other information. */
655 h
.int_penalty
= penalty
;
660 /* Pointers to voids always convert for pointers. But
661 make them less natural than more specific matches. */
662 if (TREE_CODE (ttl
) == POINTER_TYPE
&& TREE_CODE (ttr
) == POINTER_TYPE
)
664 if (TREE_TYPE (ttl
) == void_type_node
665 || TREE_TYPE (ttr
) == void_type_node
)
673 if (parm
&& codel
!= REFERENCE_TYPE
)
675 h
= convert_harshness_ansi (ttl
, ttr
, NULL_TREE
);
678 else if (penalty
== 4)
684 /* Here it does matter. If this conversion is from derived to base,
685 allow it. Otherwise, types must be compatible in the strong sense. */
686 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
688 int b_or_d
= get_base_distance (ttl
, ttr
, 0, 0);
691 b_or_d
= get_base_distance (ttr
, ttl
, 0, 0);
693 return EVIL_RETURN (h
);
694 h
.distance
= -b_or_d
;
696 /* Say that this conversion is relatively painless.
697 If it turns out that there is a user-defined X(X&)
698 constructor, then that will be invoked, but that's
699 preferable to dealing with other user-defined conversions
700 that may produce surprising results. */
707 if (comp_target_types (ttl
, intype
, 1))
716 if (codel
== RECORD_TYPE
&& coder
== RECORD_TYPE
)
718 int b_or_d
= get_base_distance (type
, parmtype
, 0, 0);
721 b_or_d
= get_base_distance (parmtype
, type
, 0, 0);
723 return EVIL_RETURN (h
);
724 h
.distance
= -b_or_d
;
731 return EVIL_RETURN (h
);
734 /* TYPE is the type we wish to convert to. PARM is the parameter
735 we have to work with. We use a somewhat arbitrary cost function
736 to measure this conversion. */
738 convert_harshness_old (type
, parmtype
, parm
)
739 register tree type
, parmtype
;
742 register enum tree_code codel
;
743 register enum tree_code coder
;
745 #ifdef GATHER_STATISTICS
746 n_convert_harshness
++;
749 if (TYPE_PTRMEMFUNC_P (type
))
750 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
751 if (TYPE_PTRMEMFUNC_P (parmtype
))
752 parmtype
= TYPE_PTRMEMFUNC_FN_TYPE (parmtype
);
754 codel
= TREE_CODE (type
);
755 coder
= TREE_CODE (parmtype
);
757 if (TYPE_MAIN_VARIANT (parmtype
) == TYPE_MAIN_VARIANT (type
))
760 if (coder
== ERROR_MARK
)
763 if (codel
== POINTER_TYPE
&& fntype_p (parmtype
))
766 int harshness
, new_harshness
;
768 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
769 type
= TREE_TYPE (type
);
771 if (coder
== POINTER_TYPE
)
773 parmtype
= TREE_TYPE (parmtype
);
774 coder
= TREE_CODE (parmtype
);
777 if (coder
!= TREE_CODE (type
))
782 /* We allow the default conversion between function type
783 and pointer-to-function type for free. */
784 if (type
== parmtype
)
787 /* Compare return types. */
788 p1
= TREE_TYPE (type
);
789 p2
= TREE_TYPE (parmtype
);
790 new_harshness
= convert_harshness_old (p1
, p2
, NULL_TREE
);
791 if (EVIL_HARSHNESS (new_harshness
))
794 if (BASE_DERIVED_HARSHNESS (new_harshness
))
798 /* This only works for pointers. */
799 if (TREE_CODE (p1
) != POINTER_TYPE
800 && TREE_CODE (p1
) != REFERENCE_TYPE
)
805 /* Don't die if we happen to be dealing with void*. */
806 if (!IS_AGGR_TYPE (p1
) || !IS_AGGR_TYPE (p2
))
808 if (CONTRAVARIANT_HARSHNESS (new_harshness
))
809 binfo
= get_binfo (p2
, p1
, 0);
811 binfo
= get_binfo (p1
, p2
, 0);
813 if (! BINFO_OFFSET_ZEROP (binfo
))
815 static int explained
= 0;
816 if (CONTRAVARIANT_HARSHNESS (new_harshness
))
817 message_2_types (sorry
, "cannot cast `%d' to `%d' at function call site", p2
, p1
);
819 message_2_types (sorry
, "cannot cast `%d' to `%d' at function call site", p1
, p2
);
822 sorry ("(because pointer values change during conversion)");
827 harshness
|= new_harshness
;
829 p1
= TYPE_ARG_TYPES (type
);
830 p2
= TYPE_ARG_TYPES (parmtype
);
831 while (p1
&& TREE_VALUE (p1
) != void_type_node
832 && p2
&& TREE_VALUE (p2
) != void_type_node
)
834 new_harshness
= convert_harshness_old (TREE_VALUE (p1
),
835 TREE_VALUE (p2
), NULL_TREE
);
836 if (EVIL_HARSHNESS (new_harshness
))
839 if (BASE_DERIVED_HARSHNESS (new_harshness
))
841 /* This only works for pointers and references. */
842 if (TREE_CODE (TREE_VALUE (p1
)) != POINTER_TYPE
843 && TREE_CODE (TREE_VALUE (p1
)) != REFERENCE_TYPE
)
845 new_harshness
^= CONTRAVARIANT_HARSHNESS (new_harshness
);
846 harshness
|= new_harshness
;
848 /* This trick allows use to accumulate easy type
849 conversions without messing up the bits that encode
850 info about more involved things. */
851 else if (ONLY_EASY_HARSHNESS (new_harshness
))
852 harshness
+= new_harshness
;
854 harshness
|= new_harshness
;
855 p1
= TREE_CHAIN (p1
);
856 p2
= TREE_CHAIN (p2
);
861 return p1
? EVIL
: (harshness
| ELLIPSIS_HARSHNESS (-1));
863 return harshness
| (TREE_PURPOSE (p1
) == NULL_TREE
);
865 else if (codel
== POINTER_TYPE
&& coder
== OFFSET_TYPE
)
867 /* XXX: Note this is set a few times, but it's never actually
871 /* Get to the OFFSET_TYPE that this might be. */
872 type
= TREE_TYPE (type
);
874 if (coder
!= TREE_CODE (type
))
879 if (TYPE_OFFSET_BASETYPE (type
) == TYPE_OFFSET_BASETYPE (parmtype
))
881 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (type
),
882 TYPE_OFFSET_BASETYPE (parmtype
)))
883 harshness
= INT_TO_BD_HARSHNESS (1);
884 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (parmtype
),
885 TYPE_OFFSET_BASETYPE (type
)))
886 harshness
= CONTRAVARIANT_HARSHNESS (-1);
889 /* Now test the OFFSET_TYPE's target compatibility. */
890 type
= TREE_TYPE (type
);
891 parmtype
= TREE_TYPE (parmtype
);
894 if (coder
== UNKNOWN_TYPE
)
896 if (codel
== FUNCTION_TYPE
897 || codel
== METHOD_TYPE
898 || (codel
== POINTER_TYPE
899 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
900 || TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)))
905 if (coder
== VOID_TYPE
)
908 if (codel
== ENUMERAL_TYPE
|| codel
== INTEGER_TYPE
)
910 /* Control equivalence of ints an enums. */
912 if (codel
== ENUMERAL_TYPE
913 && flag_int_enum_equivalence
== 0)
915 /* Enums can be converted to ints, but not vice-versa. */
916 if (coder
!= ENUMERAL_TYPE
917 || TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (parmtype
))
921 /* else enums and ints (almost) freely interconvert. */
923 if (coder
== INTEGER_TYPE
|| coder
== ENUMERAL_TYPE
)
925 int easy
= TREE_UNSIGNED (type
) ^ TREE_UNSIGNED (parmtype
);
928 if (TYPE_MODE (type
) != TYPE_MODE (parmtype
))
930 return INT_TO_EASY_HARSHNESS (easy
);
932 else if (coder
== REAL_TYPE
)
933 return INT_TO_EASY_HARSHNESS (4);
936 if (codel
== REAL_TYPE
)
937 if (coder
== REAL_TYPE
)
938 /* Shun converting between float and double if a choice exists. */
940 if (TYPE_MODE (type
) != TYPE_MODE (parmtype
))
941 return INT_TO_EASY_HARSHNESS (2);
944 else if (coder
== INTEGER_TYPE
|| coder
== ENUMERAL_TYPE
)
945 return INT_TO_EASY_HARSHNESS (4);
947 /* convert arrays which have not previously been converted. */
948 if (codel
== ARRAY_TYPE
)
949 codel
= POINTER_TYPE
;
950 if (coder
== ARRAY_TYPE
)
951 coder
= POINTER_TYPE
;
953 /* Conversions among pointers */
954 if (codel
== POINTER_TYPE
&& coder
== POINTER_TYPE
)
956 register tree ttl
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
957 register tree ttr
= TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
));
958 int penalty
= 4 * (ttl
!= ttr
);
959 /* Anything converts to void *. void * converts to anything.
960 Since these may be `const void *' (etc.) use VOID_TYPE
961 instead of void_type_node.
962 Otherwise, the targets must be the same,
963 except that we do allow (at some cost) conversion
964 between signed and unsinged pointer types. */
966 if ((TREE_CODE (ttl
) == METHOD_TYPE
967 || TREE_CODE (ttl
) == FUNCTION_TYPE
)
968 && TREE_CODE (ttl
) == TREE_CODE (ttr
))
970 if (comptypes (ttl
, ttr
, -1))
971 return INT_TO_EASY_HARSHNESS (penalty
);
975 if (!(TREE_CODE (ttl
) == VOID_TYPE
976 || TREE_CODE (ttr
) == VOID_TYPE
977 || (TREE_UNSIGNED (ttl
) ^ TREE_UNSIGNED (ttr
)
978 && (ttl
= unsigned_type (ttl
),
979 ttr
= unsigned_type (ttr
),
981 || (comp_target_types (ttl
, ttr
, 0))))
985 return INT_TO_EASY_HARSHNESS (10);
987 return INT_TO_BD_HARSHNESS (0);
989 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
991 int b_or_d
= get_base_distance (ttl
, ttr
, 0, 0);
994 b_or_d
= get_base_distance (ttr
, ttl
, 0, 0);
997 return CONTRAVARIANT_HARSHNESS (-1);
999 return INT_TO_BD_HARSHNESS (b_or_d
);
1001 /* If converting from a `class*' to a `void*', make it
1002 less favorable than any inheritance relationship. */
1003 if (TREE_CODE (ttl
) == VOID_TYPE
&& IS_AGGR_TYPE (ttr
))
1004 return INT_TO_BD_HARSHNESS (CLASSTYPE_MAX_DEPTH (ttr
)+1);
1005 return INT_TO_EASY_HARSHNESS (penalty
);
1008 if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
1010 /* This is not a bad match, but don't let it beat
1011 integer-enum combinations. */
1012 if (parm
&& integer_zerop (parm
))
1013 return INT_TO_EASY_HARSHNESS (4);
1016 /* C++: Since the `this' parameter of a signature member function
1017 is represented as a signature pointer to handle default implementations
1018 correctly, we can have the case that `type' is a signature pointer
1019 while `parmtype' is a pointer to a signature table. We don't really
1020 do any conversions in this case, so just return 0. */
1022 if (codel
== RECORD_TYPE
&& coder
== POINTER_TYPE
1023 && IS_SIGNATURE_POINTER (type
) && IS_SIGNATURE (TREE_TYPE (parmtype
)))
1026 /* C++: one of the types must be a reference type. */
1029 register tree intype
= TYPE_MAIN_VARIANT (parmtype
);
1030 register enum tree_code form
= TREE_CODE (intype
);
1033 if (codel
== REFERENCE_TYPE
|| coder
== REFERENCE_TYPE
)
1035 ttl
= TYPE_MAIN_VARIANT (type
);
1037 if (codel
== REFERENCE_TYPE
)
1039 ttl
= TREE_TYPE (ttl
);
1041 /* When passing a non-const argument into a const reference,
1042 dig it a little, so a non-const reference is preferred over
1044 if (parm
&& TREE_READONLY (ttl
) && ! TREE_READONLY (parm
))
1049 ttl
= TYPE_MAIN_VARIANT (ttl
);
1051 if (form
== OFFSET_TYPE
)
1053 intype
= TREE_TYPE (intype
);
1054 form
= TREE_CODE (intype
);
1057 if (form
== REFERENCE_TYPE
)
1059 intype
= TYPE_MAIN_VARIANT (TREE_TYPE (intype
));
1067 /* Can reference be built up? */
1068 if (ttl
== intype
&& penalty
== 0) {
1069 /* Because the READONLY bits and VIRTUAL bits are not always
1070 in the type, this extra check is necessary. The problem
1071 should be fixed someplace else, and this extra code
1074 Also, if type if a reference, the readonly bits could
1075 either be in the outer type (with reference) or on the
1076 inner type (the thing being referenced). (mrs) */
1078 && ((TREE_READONLY (parm
)
1079 && ! (TYPE_READONLY (type
)
1080 || (TREE_CODE (type
) == REFERENCE_TYPE
1081 && TYPE_READONLY (TREE_TYPE (type
)))))
1082 || (TREE_SIDE_EFFECTS (parm
)
1083 && ! (TYPE_VOLATILE (type
)
1084 || (TREE_CODE (type
) == REFERENCE_TYPE
1085 && TYPE_VOLATILE (TREE_TYPE (type
)))))))
1094 else if (form
== REFERENCE_TYPE
)
1098 tree tmp
= convert_from_reference (parm
);
1099 intype
= TYPE_MAIN_VARIANT (TREE_TYPE (tmp
));
1106 intype
= TREE_TYPE (intype
);
1108 while (TREE_CODE (intype
) == REFERENCE_TYPE
);
1109 intype
= TYPE_MAIN_VARIANT (intype
);
1118 if (TREE_UNSIGNED (ttl
) ^ TREE_UNSIGNED (intype
))
1120 ttl
= unsigned_type (ttl
);
1121 intype
= unsigned_type (intype
);
1127 /* If the initializer is not an lvalue, then it does not
1128 matter if we make life easier for the programmer
1129 by creating a temporary variable with which to
1131 if (parm
&& (coder
== INTEGER_TYPE
1132 || coder
== ENUMERAL_TYPE
1133 || coder
== REAL_TYPE
)
1134 && ! lvalue_p (parm
))
1135 return (convert_harshness_old (ttl
, ttr
, NULL_TREE
)
1136 | INT_TO_EASY_HARSHNESS (penalty
));
1141 return INT_TO_EASY_HARSHNESS (penalty
);
1142 return INT_TO_BD_HARSHNESS (0);
1145 /* Pointers to voids always convert for pointers. But
1146 make them less natural than more specific matches. */
1147 if (TREE_CODE (ttl
) == POINTER_TYPE
&& TREE_CODE (ttr
) == POINTER_TYPE
)
1148 if (TREE_TYPE (ttl
) == void_type_node
1149 || TREE_TYPE (ttr
) == void_type_node
)
1150 return INT_TO_EASY_HARSHNESS (penalty
+1);
1152 if (parm
&& codel
!= REFERENCE_TYPE
)
1153 return (convert_harshness_old (ttl
, ttr
, NULL_TREE
)
1154 | INT_TO_EASY_HARSHNESS (penalty
));
1156 /* Here it does matter. If this conversion is from
1157 derived to base, allow it. Otherwise, types must
1158 be compatible in the strong sense. */
1159 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
1161 int b_or_d
= get_base_distance (ttl
, ttr
, 0, 0);
1164 b_or_d
= get_base_distance (ttr
, ttl
, 0, 0);
1167 return CONTRAVARIANT_HARSHNESS (-1);
1169 /* Say that this conversion is relatively painless.
1170 If it turns out that there is a user-defined X(X&)
1171 constructor, then that will be invoked, but that's
1172 preferable to dealing with other user-defined conversions
1173 that may produce surprising results. */
1174 return INT_TO_BD_HARSHNESS (b_or_d
);
1177 if (comp_target_types (ttl
, intype
, 1))
1178 return INT_TO_EASY_HARSHNESS (penalty
);
1181 if (codel
== RECORD_TYPE
&& coder
== RECORD_TYPE
)
1183 int b_or_d
= get_base_distance (type
, parmtype
, 0, 0);
1186 b_or_d
= get_base_distance (parmtype
, type
, 0, 0);
1189 return CONTRAVARIANT_HARSHNESS (-1);
1191 return INT_TO_BD_HARSHNESS (b_or_d
);
1196 #ifdef DEBUG_MATCHING
1199 struct harshness_code
*h
;
1201 static char buf
[1024];
1204 bzero (buf
, 1024 * sizeof (char));
1205 strcat (buf
, "codes=[");
1206 if (h
->code
& EVIL_CODE
)
1207 strcat (buf
, "EVIL");
1208 if (h
->code
& CONST_CODE
)
1209 strcat (buf
, " CONST");
1210 if (h
->code
& ELLIPSIS_CODE
)
1211 strcat (buf
, " ELLIPSIS");
1212 if (h
->code
& USER_CODE
)
1213 strcat (buf
, " USER");
1214 if (h
->code
& STD_CODE
)
1215 strcat (buf
, " STD");
1216 if (h
->code
& PROMO_CODE
)
1217 strcat (buf
, " PROMO");
1218 if (h
->code
& QUAL_CODE
)
1219 strcat (buf
, " QUAL");
1220 if (h
->code
& TRIVIAL_CODE
)
1221 strcat (buf
, " TRIVIAL");
1225 sprintf (tmp
, "] distance=%d int_penalty=%d", h
->distance
, h
->int_penalty
);
1233 /* Algorithm: For each argument, calculate how difficult it is to
1234 make FUNCTION accept that argument. If we can easily tell that
1235 FUNCTION won't be acceptable to one of the arguments, then we
1236 don't need to compute the ease of converting the other arguments,
1237 since it will never show up in the intersection of all arguments'
1240 Conversions between builtin and user-defined types are allowed, but
1241 no function involving such a conversion is preferred to one which
1242 does not require such a conversion. Furthermore, such conversions
1246 compute_conversion_costs_ansi (function
, tta_in
, cp
, arglen
)
1249 struct candidate
*cp
;
1252 tree ttf_in
= TYPE_ARG_TYPES (TREE_TYPE (function
));
1256 /* Start out with no strikes against. */
1257 int evil_strikes
= 0;
1258 int ellipsis_strikes
= 0;
1259 int user_strikes
= 0;
1260 int b_or_d_strikes
= 0;
1261 int easy_strikes
= 0;
1263 int strike_index
= 0, win
;
1264 struct harshness_code lose
;
1266 #ifdef GATHER_STATISTICS
1267 n_compute_conversion_costs
++;
1270 cp
->function
= function
;
1271 cp
->arg
= tta
? TREE_VALUE (tta
) : NULL_TREE
;
1272 cp
->u
.bad_arg
= 0; /* optimistic! */
1276 cp
->h
.int_penalty
= 0;
1277 bzero (cp
->v
.ansi_harshness
,
1278 (cp
->h_len
+ 1) * sizeof (struct harshness_code
));
1282 struct harshness_code h
;
1284 if (ttf
== void_list_node
)
1287 if (type_unknown_p (TREE_VALUE (tta
)))
1289 /* Must perform some instantiation here. */
1290 tree rhs
= TREE_VALUE (tta
);
1291 tree lhstype
= TREE_VALUE (ttf
);
1293 /* Keep quiet about possible contravariance violations. */
1294 int old_inhibit_warnings
= inhibit_warnings
;
1295 inhibit_warnings
= 1;
1297 /* @@ This is to undo what `grokdeclarator' does to
1298 parameter types. It really should go through
1299 something more general. */
1301 TREE_TYPE (tta
) = unknown_type_node
;
1302 rhs
= instantiate_type (lhstype
, rhs
, 0);
1303 inhibit_warnings
= old_inhibit_warnings
;
1305 if (TREE_CODE (rhs
) == ERROR_MARK
)
1308 h
= convert_harshness_ansi (lhstype
, TREE_TYPE (rhs
), rhs
);
1312 #ifdef DEBUG_MATCHING
1313 static tree old_function
= NULL_TREE
;
1315 if (!old_function
|| function
!= old_function
)
1317 cp_error ("trying %D", function
);
1318 old_function
= function
;
1321 cp_error (" doing (%T) %E against arg %T",
1322 TREE_TYPE (TREE_VALUE (tta
)), TREE_VALUE (tta
),
1326 h
= convert_harshness_ansi (TREE_VALUE (ttf
),
1327 TREE_TYPE (TREE_VALUE (tta
)),
1330 #ifdef DEBUG_MATCHING
1331 cp_error (" evaluated %s", print_harshness (&h
));
1335 cp
->v
.ansi_harshness
[strike_index
] = h
;
1336 if ((h
.code
& EVIL_CODE
)
1337 || ((h
.code
& STD_CODE
) && h
.distance
< 0))
1339 cp
->u
.bad_arg
= strike_index
;
1342 else if (h
.code
& ELLIPSIS_CODE
)
1343 ellipsis_strikes
+= 1;
1345 /* This is never set by `convert_harshness_ansi'. */
1346 else if (h
.code
& USER_CODE
)
1353 if ((h
.code
& STD_CODE
) && h
.distance
)
1355 if (h
.distance
> b_or_d_strikes
)
1356 b_or_d_strikes
= h
.distance
;
1359 easy_strikes
+= (h
.code
& (STD_CODE
|PROMO_CODE
|TRIVIAL_CODE
));
1360 cp
->h
.code
|= h
.code
;
1361 /* Make sure we communicate this. */
1362 cp
->h
.int_penalty
+= h
.int_penalty
;
1365 ttf
= TREE_CHAIN (ttf
);
1366 tta
= TREE_CHAIN (tta
);
1372 /* ran out of formals, and parmlist is fixed size. */
1373 if (ttf
/* == void_type_node */)
1375 cp
->h
.code
= EVIL_CODE
;
1381 struct harshness_code h
;
1382 int l
= list_length (tta
);
1383 ellipsis_strikes
+= l
;
1384 h
.code
= ELLIPSIS_CODE
;
1388 cp
->v
.ansi_harshness
[strike_index
++] = h
;
1391 else if (ttf
&& ttf
!= void_list_node
)
1393 /* ran out of actuals, and no defaults. */
1394 if (TREE_PURPOSE (ttf
) == NULL_TREE
)
1396 cp
->h
.code
= EVIL_CODE
;
1400 /* Store index of first default. */
1401 cp
->v
.ansi_harshness
[arglen
].distance
= strike_index
+1;
1404 cp
->v
.ansi_harshness
[arglen
].distance
= 0;
1406 /* Argument list lengths work out, so don't need to check them again. */
1409 /* We do not check for derived->base conversions here, since in
1410 no case would they give evil strike counts, unless such conversions
1411 are somehow ambiguous. */
1413 /* See if any user-defined conversions apply.
1414 But make sure that we do not loop. */
1415 static int dont_convert_types
= 0;
1417 if (dont_convert_types
)
1419 cp
->h
.code
= EVIL_CODE
;
1423 win
= 0; /* Only get one chance to win. */
1424 ttf
= TYPE_ARG_TYPES (TREE_TYPE (function
));
1431 if (ttf
== void_list_node
)
1434 lose
= cp
->v
.ansi_harshness
[strike_index
];
1435 if ((lose
.code
& EVIL_CODE
)
1436 || ((lose
.code
& STD_CODE
) && lose
.distance
< 0))
1438 tree actual_type
= TREE_TYPE (TREE_VALUE (tta
));
1439 tree formal_type
= TREE_VALUE (ttf
);
1440 int extra_conversions
= 0;
1442 dont_convert_types
= 1;
1444 if (TREE_CODE (formal_type
) == REFERENCE_TYPE
)
1445 formal_type
= TREE_TYPE (formal_type
);
1446 if (TREE_CODE (actual_type
) == REFERENCE_TYPE
)
1447 actual_type
= TREE_TYPE (actual_type
);
1449 if (formal_type
!= error_mark_node
1450 && actual_type
!= error_mark_node
)
1452 formal_type
= TYPE_MAIN_VARIANT (formal_type
);
1453 actual_type
= TYPE_MAIN_VARIANT (actual_type
);
1455 if (TYPE_HAS_CONSTRUCTOR (formal_type
))
1457 /* If it has a constructor for this type,
1459 /* @@ There is no way to save this result yet, so
1460 success is a NULL_TREE for now. */
1461 if (convert_to_aggr (formal_type
, TREE_VALUE (tta
), 0, 1)
1465 if (TYPE_LANG_SPECIFIC (actual_type
)
1466 && TYPE_HAS_CONVERSION (actual_type
))
1469 /* Don't issue warnings since we're only groping
1470 around for the right answer, we haven't yet
1471 committed to going with this solution. */
1472 int old_inhibit_warnings
= inhibit_warnings
;
1474 inhibit_warnings
= 1;
1475 conv
= build_type_conversion
1476 (CALL_EXPR
, TREE_VALUE (ttf
), TREE_VALUE (tta
), 0);
1477 inhibit_warnings
= old_inhibit_warnings
;
1481 if (conv
== error_mark_node
)
1486 if (TREE_CODE (conv
) != CALL_EXPR
)
1487 extra_conversions
= 1;
1490 else if (TREE_CODE (TREE_VALUE (ttf
)) == REFERENCE_TYPE
)
1492 conv
= build_type_conversion (CALL_EXPR
, formal_type
,
1493 TREE_VALUE (tta
), 0);
1496 if (conv
== error_mark_node
)
1501 if (TREE_CODE (conv
) != CALL_EXPR
)
1502 extra_conversions
= 1;
1508 dont_convert_types
= 0;
1513 cp
->v
.ansi_harshness
[strike_index
].code
1514 = USER_CODE
| (extra_conversions
? STD_CODE
: 0);
1519 if (cp
->u
.bad_arg
> strike_index
)
1520 cp
->u
.bad_arg
= strike_index
;
1522 evil_strikes
= win
? 2 : 1;
1527 ttf
= TREE_CHAIN (ttf
);
1528 tta
= TREE_CHAIN (tta
);
1533 /* Const member functions get a small penalty because defaulting
1534 to const is less useful than defaulting to non-const. */
1535 /* This is bogus, it does not correspond to anything in the ARM.
1536 This code will be fixed when this entire section is rewritten
1537 to conform to the ARM. (mrs) */
1538 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1540 tree this_parm
= TREE_VALUE (ttf_in
);
1542 if (TREE_CODE (this_parm
) == RECORD_TYPE
/* Is `this' a sig ptr? */
1543 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm
))))
1544 : TYPE_READONLY (TREE_TYPE (this_parm
)))
1546 cp
->v
.ansi_harshness
[0].code
|= TRIVIAL_CODE
;
1551 /* Calling a non-const member function from a const member function
1552 is probably invalid, but for now we let it only draw a warning.
1553 We indicate that such a mismatch has occurred by setting the
1554 harshness to a maximum value. */
1555 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in
))) == POINTER_TYPE
1556 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in
))))))
1557 cp
->v
.ansi_harshness
[0].code
|= CONST_CODE
;
1562 cp
->h
.code
= EVIL_CODE
;
1563 if (ellipsis_strikes
)
1564 cp
->h
.code
|= ELLIPSIS_CODE
;
1566 cp
->h
.code
|= USER_CODE
;
1567 #ifdef DEBUG_MATCHING
1568 cp_error ("final eval %s", print_harshness (&cp
->h
));
1573 compute_conversion_costs_old (function
, tta_in
, cp
, arglen
)
1576 struct candidate
*cp
;
1579 tree ttf_in
= TYPE_ARG_TYPES (TREE_TYPE (function
));
1583 /* Start out with no strikes against. */
1584 int evil_strikes
= 0;
1585 int ellipsis_strikes
= 0;
1586 int user_strikes
= 0;
1587 int b_or_d_strikes
= 0;
1588 int easy_strikes
= 0;
1590 int strike_index
= 0, win
, lose
;
1592 #ifdef GATHER_STATISTICS
1593 n_compute_conversion_costs
++;
1596 cp
->function
= function
;
1597 cp
->arg
= tta
? TREE_VALUE (tta
) : NULL_TREE
;
1598 cp
->u
.bad_arg
= 0; /* optimistic! */
1600 bzero (cp
->v
.old_harshness
, (cp
->h_len
+ 1) * sizeof (unsigned short));
1606 if (ttf
== void_list_node
)
1609 if (type_unknown_p (TREE_VALUE (tta
)))
1611 /* Must perform some instantiation here. */
1612 tree rhs
= TREE_VALUE (tta
);
1613 tree lhstype
= TREE_VALUE (ttf
);
1615 /* Keep quiet about possible contravariance violations. */
1616 int old_inhibit_warnings
= inhibit_warnings
;
1617 inhibit_warnings
= 1;
1619 /* @@ This is to undo what `grokdeclarator' does to
1620 parameter types. It really should go through
1621 something more general. */
1623 TREE_TYPE (tta
) = unknown_type_node
;
1624 rhs
= instantiate_type (lhstype
, rhs
, 0);
1625 inhibit_warnings
= old_inhibit_warnings
;
1627 if (TREE_CODE (rhs
) == ERROR_MARK
)
1631 harshness
= convert_harshness_old (lhstype
, TREE_TYPE (rhs
),
1633 /* harshness |= 2; */
1637 harshness
= convert_harshness_old (TREE_VALUE (ttf
),
1638 TREE_TYPE (TREE_VALUE (tta
)),
1641 cp
->v
.old_harshness
[strike_index
] = harshness
;
1642 if (EVIL_HARSHNESS (harshness
)
1643 || CONTRAVARIANT_HARSHNESS (harshness
))
1645 cp
->u
.bad_arg
= strike_index
;
1648 else if (ELLIPSIS_HARSHNESS (harshness
))
1650 ellipsis_strikes
+= 1;
1653 /* This is never set by `convert_harshness_old'. */
1654 else if (USER_HARSHNESS (harshness
))
1659 else if (BASE_DERIVED_HARSHNESS (harshness
))
1661 b_or_d_strikes
+= INT_FROM_BD_HARSHNESS (harshness
);
1664 easy_strikes
+= INT_FROM_EASY_HARSHNESS (harshness
);
1665 ttf
= TREE_CHAIN (ttf
);
1666 tta
= TREE_CHAIN (tta
);
1672 /* ran out of formals, and parmlist is fixed size. */
1673 if (ttf
/* == void_type_node */)
1679 else ellipsis_strikes
+= list_length (tta
);
1681 else if (ttf
&& ttf
!= void_list_node
)
1683 /* ran out of actuals, and no defaults. */
1684 if (TREE_PURPOSE (ttf
) == NULL_TREE
)
1690 /* Store index of first default. */
1691 cp
->v
.old_harshness
[arglen
] = strike_index
+1;
1694 cp
->v
.old_harshness
[arglen
] = 0;
1696 /* Argument list lengths work out, so don't need to check them again. */
1699 /* We do not check for derived->base conversions here, since in
1700 no case would they give evil strike counts, unless such conversions
1701 are somehow ambiguous. */
1703 /* See if any user-defined conversions apply.
1704 But make sure that we do not loop. */
1705 static int dont_convert_types
= 0;
1707 if (dont_convert_types
)
1713 win
= 0; /* Only get one chance to win. */
1714 ttf
= TYPE_ARG_TYPES (TREE_TYPE (function
));
1721 if (ttf
== void_list_node
)
1724 lose
= cp
->v
.old_harshness
[strike_index
];
1725 if (EVIL_HARSHNESS (lose
)
1726 || CONTRAVARIANT_HARSHNESS (lose
))
1728 tree actual_type
= TREE_TYPE (TREE_VALUE (tta
));
1729 tree formal_type
= TREE_VALUE (ttf
);
1731 dont_convert_types
= 1;
1733 if (TREE_CODE (formal_type
) == REFERENCE_TYPE
)
1734 formal_type
= TREE_TYPE (formal_type
);
1735 if (TREE_CODE (actual_type
) == REFERENCE_TYPE
)
1736 actual_type
= TREE_TYPE (actual_type
);
1738 if (formal_type
!= error_mark_node
1739 && actual_type
!= error_mark_node
)
1741 formal_type
= TYPE_MAIN_VARIANT (formal_type
);
1742 actual_type
= TYPE_MAIN_VARIANT (actual_type
);
1744 if (TYPE_HAS_CONSTRUCTOR (formal_type
))
1746 /* If it has a constructor for this type, try to use it. */
1747 if (convert_to_aggr (formal_type
, TREE_VALUE (tta
), 0, 1)
1750 /* @@ There is no way to save this result yet.
1751 @@ So success is NULL_TREE for now. */
1755 if (TYPE_LANG_SPECIFIC (actual_type
) && TYPE_HAS_CONVERSION (actual_type
))
1757 if (TREE_CODE (formal_type
) == INTEGER_TYPE
1758 && TYPE_HAS_INT_CONVERSION (actual_type
))
1760 else if (TREE_CODE (formal_type
) == REAL_TYPE
1761 && TYPE_HAS_REAL_CONVERSION (actual_type
))
1765 tree conv
= build_type_conversion (CALL_EXPR
, TREE_VALUE (ttf
), TREE_VALUE (tta
), 0);
1768 if (conv
== error_mark_node
)
1773 else if (TREE_CODE (TREE_VALUE (ttf
)) == REFERENCE_TYPE
)
1775 conv
= build_type_conversion (CALL_EXPR
, formal_type
, TREE_VALUE (tta
), 0);
1778 if (conv
== error_mark_node
)
1787 dont_convert_types
= 0;
1792 cp
->v
.old_harshness
[strike_index
] = USER_HARSHNESS (-1);
1797 if (cp
->u
.bad_arg
> strike_index
)
1798 cp
->u
.bad_arg
= strike_index
;
1800 evil_strikes
= win
? 2 : 1;
1805 ttf
= TREE_CHAIN (ttf
);
1806 tta
= TREE_CHAIN (tta
);
1811 /* Const member functions get a small penalty because defaulting
1812 to const is less useful than defaulting to non-const. */
1813 /* This is bogus, it does not correspond to anything in the ARM.
1814 This code will be fixed when this entire section is rewritten
1815 to conform to the ARM. (mrs) */
1816 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1818 tree this_parm
= TREE_VALUE (ttf_in
);
1820 if (TREE_CODE (this_parm
) == RECORD_TYPE
/* Is `this' a sig ptr? */
1821 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm
))))
1822 : TYPE_READONLY (TREE_TYPE (this_parm
)))
1824 cp
->v
.old_harshness
[0] += INT_TO_EASY_HARSHNESS (1);
1829 /* Calling a non-const member function from a const member function
1830 is probably invalid, but for now we let it only draw a warning.
1831 We indicate that such a mismatch has occurred by setting the
1832 harshness to a maximum value. */
1833 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in
))) == POINTER_TYPE
1834 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in
))))))
1835 cp
->v
.old_harshness
[0] |= CONST_HARSHNESS (-1);
1839 cp
->evil
= evil_strikes
;
1840 cp
->ellipsis
= ellipsis_strikes
;
1841 cp
->user
= user_strikes
;
1842 cp
->b_or_d
= b_or_d_strikes
;
1843 cp
->easy
= easy_strikes
;
1847 compute_conversion_costs (function
, tta_in
, cp
, arglen
)
1850 struct candidate
*cp
;
1853 if (flag_ansi_overloading
)
1854 compute_conversion_costs_ansi (function
, tta_in
, cp
, arglen
);
1856 compute_conversion_costs_old (function
, tta_in
, cp
, arglen
);
1859 /* When one of several possible overloaded functions and/or methods
1860 can be called, choose the best candidate for overloading.
1862 BASETYPE is the context from which we start method resolution
1863 or NULL if we are comparing overloaded functions.
1864 CANDIDATES is the array of candidates we have to choose from.
1865 N_CANDIDATES is the length of CANDIDATES.
1866 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1867 choose. It is modified in place when resolving methods. It is not
1868 modified in place when resolving overloaded functions.
1869 LEN is the length of the parameter list. */
1871 static struct candidate
*
1872 ideal_candidate_old (basetype
, candidates
, n_candidates
, parms
, len
)
1874 struct candidate
*candidates
;
1879 struct candidate
*cp
= candidates
+ n_candidates
;
1883 qsort (candidates
, /* char *base */
1884 n_candidates
, /* int nel */
1885 sizeof (struct candidate
), /* int width */
1886 rank_for_overload
); /* int (*compar)() */
1888 /* If the best candidate requires user-defined conversions,
1889 and its user-defined conversions are a strict subset
1890 of all other candidates requiring user-defined conversions,
1891 then it is, in fact, the best. */
1892 for (i
= -1; cp
+ i
!= candidates
; i
--)
1893 if (cp
[i
].user
== 0)
1900 /* Check that every other candidate requires those conversions
1901 as a strict subset of their conversions. */
1902 if (cp
[i
].user
== cp
[-1].user
)
1905 /* Look at subset relationship more closely. */
1908 for (ttf
= TYPE_ARG_TYPES (TREE_TYPE (cp
[i
].function
)),
1909 ttf0
= TYPE_ARG_TYPES (TREE_TYPE (cp
[-1].function
)),
1910 index
= 0; index
< len
; index
++)
1912 if (USER_HARSHNESS (cp
[i
].v
.old_harshness
[index
]))
1914 /* If our "best" candidate also needs a conversion,
1915 it must be the same one. */
1916 if (USER_HARSHNESS (cp
[-1].v
.old_harshness
[index
])
1917 && TREE_VALUE (ttf
) != TREE_VALUE (ttf0
))
1920 ttf
= TREE_CHAIN (ttf
);
1921 ttf0
= TREE_CHAIN (ttf0
);
1922 /* Handle `...' gracefully. */
1923 if (ttf
== NULL_TREE
|| ttf0
== NULL_TREE
)
1928 /* The best was the best. */
1931 /* Use other rules for determining "bestness". */
1935 /* If the best two candidates we find require user-defined
1936 conversions, we may need to report and error message. */
1937 if (cp
[-1].user
&& cp
[-2].user
1938 && (cp
[-1].b_or_d
|| cp
[-2].b_or_d
== 0))
1940 /* If the best two methods found involved user-defined
1941 type conversions, then we must see whether one
1942 of them is exactly what we wanted. If not, then
1943 we have an ambiguity. */
1952 /* Stash all of our parameters in safe places
1953 so that we can perform type conversions in place. */
1956 TREE_PURPOSE (tta
) = TREE_VALUE (tta
);
1957 tta
= TREE_CHAIN (tta
);
1963 int exact_conversions
= 0;
1967 if (DECL_STATIC_FUNCTION_P (cp
[i
].function
))
1968 tta
= TREE_CHAIN (tta
);
1969 /* special note, we don't go through len parameters, because we
1970 may only need len-1 parameters because of a call to a static
1972 for (ttf
= TYPE_ARG_TYPES (TREE_TYPE (cp
[i
].function
)), index
= 0;
1974 tta
= TREE_CHAIN (tta
), ttf
= TREE_CHAIN (ttf
), index
++)
1976 /* If this is a varargs function, there's no conversion to do,
1977 but don't accept an arg that needs a copy ctor. */
1978 if (ttf
== NULL_TREE
)
1980 /* FIXME: verify that we cannot get here with an
1981 arg that needs a ctor. */
1985 if (USER_HARSHNESS (cp
[i
].v
.old_harshness
[index
]))
1987 tree this_parm
= build_type_conversion (CALL_EXPR
, TREE_VALUE (ttf
), TREE_PURPOSE (tta
), 2);
1988 if (basetype
!= NULL_TREE
)
1989 TREE_VALUE (tta
) = this_parm
;
1992 if (TREE_CODE (this_parm
) != CONVERT_EXPR
1993 && (TREE_CODE (this_parm
) != NOP_EXPR
1994 || comp_target_types (TREE_TYPE (this_parm
),
1995 TREE_TYPE (TREE_OPERAND (this_parm
, 0)), 1)))
1996 exact_conversions
+= 1;
1998 else if (PROMOTES_TO_AGGR_TYPE (TREE_VALUE (ttf
), REFERENCE_TYPE
))
2000 /* To get here we had to have succeeded via
2002 TREE_VALUE (tta
) = TREE_PURPOSE (tta
);
2003 exact_conversions
+= 1;
2007 if (exact_conversions
== cp
[i
].user
)
2012 f1
= cp
[best
].function
;
2015 p1
= TYPE_ARG_TYPES (TREE_TYPE (f1
));
2020 /* Don't complain if next best is from base class. */
2021 tree f2
= cp
[i
].function
;
2023 if (TREE_CODE (TREE_TYPE (f1
)) == METHOD_TYPE
2024 && TREE_CODE (TREE_TYPE (f2
)) == METHOD_TYPE
2025 && BASE_DERIVED_HARSHNESS (cp
[i
].v
.old_harshness
[0])
2026 && cp
[best
].v
.old_harshness
[0] < cp
[i
].v
.old_harshness
[0])
2029 tree p2
= TYPE_ARG_TYPES (TREE_TYPE (f2
));
2031 if (! compparms (TREE_CHAIN (p1
), TREE_CHAIN (p2
), 1))
2039 /* Ensure that there's nothing ambiguous about these
2042 for (index
= 0; index
< len
; index
++)
2044 /* Type conversions must be piecewise equivalent. */
2045 if (USER_HARSHNESS (cp
[best
].v
.old_harshness
[index
])
2046 != USER_HARSHNESS (cp
[i
].v
.old_harshness
[index
]))
2048 /* If there's anything we like better about the
2049 other function, consider it ambiguous. */
2050 if (cp
[i
].v
.old_harshness
[index
] < cp
[best
].v
.old_harshness
[index
])
2052 /* If any single one it diffent, then the whole is
2054 if (cp
[i
].v
.old_harshness
[index
] != cp
[best
].v
.old_harshness
[index
])
2058 /* If we can't tell the difference between the two, it
2063 /* If we made it to here, it means we're satisfied that
2064 BEST is still best. */
2069 } while (cp
+ i
!= candidates
);
2073 int exact_conversions
= cp
[best
].user
;
2075 if (DECL_STATIC_FUNCTION_P (cp
[best
].function
))
2076 tta
= TREE_CHAIN (parms
);
2077 for (ttf
= TYPE_ARG_TYPES (TREE_TYPE (cp
[best
].function
)), index
= 0;
2078 exact_conversions
> 0;
2079 tta
= TREE_CHAIN (tta
), ttf
= TREE_CHAIN (ttf
), index
++)
2081 if (USER_HARSHNESS (cp
[best
].v
.old_harshness
[index
]))
2083 /* We must now fill in the slot we left behind.
2084 @@ This could be optimized to use the value previously
2085 @@ computed by build_type_conversion in some cases. */
2086 if (basetype
!= NULL_TREE
)
2087 TREE_VALUE (tta
) = convert (TREE_VALUE (ttf
), TREE_PURPOSE (tta
));
2088 exact_conversions
-= 1;
2091 TREE_VALUE (tta
) = TREE_PURPOSE (tta
);
2097 /* If the best two candidates we find both use default parameters,
2098 we may need to report and error. Don't need to worry if next-best
2099 candidate is forced to use user-defined conversion when best is not. */
2100 if (cp
[-2].user
== 0
2101 && cp
[-1].v
.old_harshness
[len
] != 0 && cp
[-2].v
.old_harshness
[len
] != 0)
2103 tree tt1
= TYPE_ARG_TYPES (TREE_TYPE (cp
[-1].function
));
2104 tree tt2
= TYPE_ARG_TYPES (TREE_TYPE (cp
[-2].function
));
2105 unsigned i
= cp
[-1].v
.old_harshness
[len
];
2107 if (cp
[-2].v
.old_harshness
[len
] < i
)
2108 i
= cp
[-2].v
.old_harshness
[len
];
2111 if (TYPE_MAIN_VARIANT (TREE_VALUE (tt1
))
2112 != TYPE_MAIN_VARIANT (TREE_VALUE (tt2
)))
2113 /* These lists are not identical, so we can choose our best candidate. */
2115 tt1
= TREE_CHAIN (tt1
);
2116 tt2
= TREE_CHAIN (tt2
);
2118 /* To get here, both lists had the same parameters up to the defaults
2119 which were used. This is an ambiguous request. */
2123 /* Otherwise, return our best candidate. Note that if we get candidates
2124 from independent base classes, we have an ambiguity, even if one
2125 argument list look a little better than another one. */
2126 if (cp
[-1].b_or_d
&& basetype
&& TYPE_USES_MULTIPLE_INHERITANCE (basetype
))
2128 int i
= n_candidates
- 1, best
= i
;
2129 tree base1
= NULL_TREE
;
2131 if (TREE_CODE (TREE_TYPE (candidates
[i
].function
)) == FUNCTION_TYPE
)
2134 for (; i
>= 0 && candidates
[i
].user
== 0 && candidates
[i
].evil
== 0; i
--)
2136 if (TREE_CODE (TREE_TYPE (candidates
[i
].function
)) == METHOD_TYPE
)
2138 tree newbase
= DECL_CLASS_CONTEXT (candidates
[i
].function
);
2140 if (base1
!= NULL_TREE
)
2142 /* newbase could be a base or a parent of base1 */
2143 if (newbase
!= base1
&& ! UNIQUELY_DERIVED_FROM_P (newbase
, base1
)
2144 && ! UNIQUELY_DERIVED_FROM_P (base1
, newbase
))
2146 cp_error ("ambiguous request for function from distinct base classes of type `%T'", basetype
);
2147 cp_error_at (" first candidate is `%#D'",
2148 candidates
[best
].function
);
2149 cp_error_at (" second candidate is `%#D'",
2150 candidates
[i
].function
);
2166 /* Don't accept a candidate as being ideal if it's indistinguishable
2167 from another candidate. */
2168 if (rank_for_overload (cp
-1, cp
-2) == 0)
2170 /* If the types are distinguishably different (like
2171 `long' vs. `unsigned long'), that's ok. But if they are arbitrarily
2172 different, such as `int (*)(void)' vs. `void (*)(int)',
2174 tree p1
= TYPE_ARG_TYPES (TREE_TYPE (cp
[-1].function
));
2175 tree p2
= TYPE_ARG_TYPES (TREE_TYPE (cp
[-2].function
));
2178 if (TREE_CODE (TREE_VALUE (p1
)) == POINTER_TYPE
2179 && TREE_CODE (TREE_TYPE (TREE_VALUE (p1
))) == FUNCTION_TYPE
2180 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
2182 p1
= TREE_CHAIN (p1
);
2183 p2
= TREE_CHAIN (p2
);
2192 /* In the case where there is no ideal candidate, restore
2193 TREE_VALUE slots of PARMS from TREE_PURPOSE slots. */
2196 TREE_VALUE (parms
) = TREE_PURPOSE (parms
);
2197 parms
= TREE_CHAIN (parms
);
2202 /* Subroutine of ideal_candidate. See if X or Y is a better match
2205 strictly_better (x
, y
)
2206 unsigned short x
, y
;
2214 if (xor >= x
|| xor >= y
)
2219 static struct candidate
*
2220 ideal_candidate_ansi (basetype
, candidates
, n_candidates
, parms
, len
)
2222 struct candidate
*candidates
;
2227 struct candidate
*cp
= candidates
+n_candidates
;
2228 int i
, j
= -1, best_code
;
2230 /* For each argument, sort the functions from best to worst for the arg.
2231 For each function that's not best for this arg, set its overall
2232 harshness to EVIL so that other args won't like it. The candidate
2233 list for the last argument is the intersection of all the best-liked
2237 for (i
= 0; i
< len
; i
++)
2239 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
2241 best_code
= cp
[-1].h
.code
;
2243 /* To find out functions that are worse than that represented
2244 by BEST_CODE, we can't just do a comparison like h.code>best_code.
2245 The total harshness for the "best" fn may be 8|8 for two args, and
2246 the harshness for the next-best may be 8|2. If we just compared,
2247 that would be checking 8>10, which would lead to the next-best
2248 being disqualified. What we actually want to do is get rid
2249 of functions that are definitely worse than that represented
2250 by best_code, i.e. those which have bits set higher than the
2251 highest in best_code. Sooooo, what we do is clear out everything
2252 represented by best_code, and see if we still come up with something
2253 higher. If so (e.g., 8|8 vs 8|16), it'll disqualify it properly. */
2254 for (j
= n_candidates
-2; j
>= 0; j
--)
2255 if ((candidates
[j
].h
.code
& ~best_code
) > best_code
)
2256 candidates
[j
].h
.code
= EVIL_CODE
;
2259 if (cp
[-1].h
.code
& EVIL_CODE
)
2262 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
2264 best_code
= cp
[-1].h
.code
;
2267 /* If they're at least as good as each other, do an arg-by-arg check. */
2268 if (! strictly_better (cp
[-1].h
.code
, cp
[-2].h
.code
))
2273 for (j
= 0; j
< n_candidates
; j
++)
2274 if (! strictly_better (candidates
[j
].h
.code
, best_code
))
2277 qsort (candidates
+j
, n_candidates
-j
, sizeof (struct candidate
),
2279 for (i
= 0; i
< len
; i
++)
2281 if (cp
[-1].v
.ansi_harshness
[i
].code
< cp
[-2].v
.ansi_harshness
[i
].code
)
2283 else if (cp
[-1].v
.ansi_harshness
[i
].code
> cp
[-2].v
.ansi_harshness
[i
].code
)
2285 else if (cp
[-1].v
.ansi_harshness
[i
].code
& STD_CODE
)
2287 /* If it involves a standard conversion, let the
2288 inheritance lattice be the final arbiter. */
2289 if (cp
[-1].v
.ansi_harshness
[i
].distance
> cp
[-2].v
.ansi_harshness
[i
].distance
)
2291 else if (cp
[-1].v
.ansi_harshness
[i
].distance
< cp
[-2].v
.ansi_harshness
[i
].distance
)
2294 else if (cp
[-1].v
.ansi_harshness
[i
].code
& PROMO_CODE
)
2296 /* For integral promotions, take into account a finer
2297 granularity for determining which types should be favored
2298 over others in such promotions. */
2299 if (cp
[-1].v
.ansi_harshness
[i
].int_penalty
> cp
[-2].v
.ansi_harshness
[i
].int_penalty
)
2301 else if (cp
[-1].v
.ansi_harshness
[i
].int_penalty
< cp
[-2].v
.ansi_harshness
[i
].int_penalty
)
2306 if (! better
|| worse
)
2312 static struct candidate
*
2313 ideal_candidate (basetype
, candidates
, n_candidates
, parms
, len
)
2315 struct candidate
*candidates
;
2320 if (flag_ansi_overloading
)
2321 return ideal_candidate_ansi (basetype
, candidates
, n_candidates
, parms
,
2324 return ideal_candidate_old (basetype
, candidates
, n_candidates
, parms
,
2328 /* Assume that if the class referred to is not in the
2329 current class hierarchy, that it may be remote.
2330 PARENT is assumed to be of aggregate type here. */
2332 may_be_remote (parent
)
2335 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent
) == 0)
2338 if (current_class_type
== NULL_TREE
)
2341 if (parent
== current_class_type
)
2344 if (UNIQUELY_DERIVED_FROM_P (parent
, current_class_type
))
2350 build_vfield_ref (datum
, type
)
2354 int old_assume_nonnull_objects
= flag_assume_nonnull_objects
;
2356 if (datum
== error_mark_node
)
2357 return error_mark_node
;
2359 /* Vtable references are always made from non-null objects. */
2360 flag_assume_nonnull_objects
= 1;
2361 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
2362 datum
= convert_from_reference (datum
);
2364 if (! TYPE_USES_COMPLEX_INHERITANCE (type
))
2365 rval
= build (COMPONENT_REF
, TREE_TYPE (CLASSTYPE_VFIELD (type
)),
2366 datum
, CLASSTYPE_VFIELD (type
));
2368 rval
= build_component_ref (datum
, DECL_NAME (CLASSTYPE_VFIELD (type
)), 0, 0);
2369 flag_assume_nonnull_objects
= old_assume_nonnull_objects
;
2374 /* Build a call to a member of an object. I.e., one that overloads
2375 operator ()(), or is a pointer-to-function or pointer-to-method. */
2377 build_field_call (basetype_path
, instance_ptr
, name
, parms
)
2378 tree basetype_path
, instance_ptr
, name
, parms
;
2380 tree field
, instance
;
2382 if (instance_ptr
== current_class_decl
)
2384 /* Check to see if we really have a reference to an instance variable
2385 with `operator()()' overloaded. */
2386 field
= IDENTIFIER_CLASS_VALUE (name
);
2388 if (field
== NULL_TREE
)
2390 cp_error ("`this' has no member named `%D'", name
);
2391 return error_mark_node
;
2394 if (TREE_CODE (field
) == FIELD_DECL
)
2396 /* If it's a field, try overloading operator (),
2397 or calling if the field is a pointer-to-function. */
2398 instance
= build_component_ref_1 (C_C_D
, field
, 0);
2399 if (instance
== error_mark_node
)
2400 return error_mark_node
;
2402 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
2403 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance
)))
2404 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, instance
, parms
, NULL_TREE
);
2406 if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
2408 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == FUNCTION_TYPE
)
2409 return build_function_call (instance
, parms
);
2410 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == METHOD_TYPE
)
2411 return build_function_call (instance
, tree_cons (NULL_TREE
, current_class_decl
, parms
));
2417 /* Check to see if this is not really a reference to an instance variable
2418 with `operator()()' overloaded. */
2419 field
= lookup_field (basetype_path
, name
, 1, 0);
2421 /* This can happen if the reference was ambiguous or for access
2423 if (field
== error_mark_node
)
2424 return error_mark_node
;
2429 tree ftype
= TREE_TYPE (field
);
2431 if (TREE_CODE (ftype
) == REFERENCE_TYPE
)
2432 ftype
= TREE_TYPE (ftype
);
2434 if (TYPE_LANG_SPECIFIC (ftype
) && TYPE_OVERLOADS_CALL_EXPR (ftype
))
2436 /* Make the next search for this field very short. */
2437 basetype
= DECL_FIELD_CONTEXT (field
);
2438 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
2440 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2441 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
2442 build_component_ref_1 (instance
, field
, 0),
2445 if (TREE_CODE (ftype
) == POINTER_TYPE
)
2447 if (TREE_CODE (TREE_TYPE (ftype
)) == FUNCTION_TYPE
2448 || TREE_CODE (TREE_TYPE (ftype
)) == METHOD_TYPE
)
2450 /* This is a member which is a pointer to function. */
2452 = build_component_ref_1 (build_indirect_ref (instance_ptr
,
2454 field
, LOOKUP_COMPLAIN
);
2455 if (ref
== error_mark_node
)
2456 return error_mark_node
;
2457 return build_function_call (ref
, parms
);
2460 else if (TREE_CODE (ftype
) == METHOD_TYPE
)
2462 error ("invalid call via pointer-to-member function");
2463 return error_mark_node
;
2472 find_scoped_type (type
, inner_name
, inner_types
)
2473 tree type
, inner_name
, inner_types
;
2475 tree tags
= CLASSTYPE_TAGS (type
);
2479 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
2480 enclosing class) is set to the name for the enum type. So, if
2481 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
2482 then this test will be true. */
2483 if (TREE_PURPOSE (tags
) == inner_name
)
2485 if (inner_types
== NULL_TREE
)
2486 return DECL_NESTED_TYPENAME (TYPE_NAME (TREE_VALUE (tags
)));
2487 return resolve_scope_to_name (TREE_VALUE (tags
), inner_types
);
2489 tags
= TREE_CHAIN (tags
);
2493 /* XXX This needs to be fixed better. */
2494 if (TREE_CODE (type
) == UNINSTANTIATED_P_TYPE
)
2496 sorry ("nested class lookup in template type");
2501 /* Look for a TYPE_DECL. */
2502 for (tags
= TYPE_FIELDS (type
); tags
; tags
= TREE_CHAIN (tags
))
2503 if (TREE_CODE (tags
) == TYPE_DECL
&& DECL_NAME (tags
) == inner_name
)
2505 /* Code by raeburn. */
2506 if (inner_types
== NULL_TREE
)
2507 return DECL_NESTED_TYPENAME (tags
);
2508 return resolve_scope_to_name (TREE_TYPE (tags
), inner_types
);
2514 /* Resolve an expression NAME1::NAME2::...::NAMEn to
2515 the name that names the above nested type. INNER_TYPES
2516 is a chain of nested type names (held together by SCOPE_REFs);
2517 OUTER_TYPE is the type we know to enclose INNER_TYPES.
2518 Returns NULL_TREE if there is an error. */
2520 resolve_scope_to_name (outer_type
, inner_stuff
)
2521 tree outer_type
, inner_stuff
;
2524 tree inner_name
, inner_type
;
2526 if (outer_type
== NULL_TREE
&& current_class_type
!= NULL_TREE
)
2528 /* We first try to look for a nesting in our current class context,
2529 then try any enclosing classes. */
2530 tree type
= current_class_type
;
2532 while (type
&& (TREE_CODE (type
) == RECORD_TYPE
2533 || TREE_CODE (type
) == UNION_TYPE
))
2535 tree rval
= resolve_scope_to_name (type
, inner_stuff
);
2537 if (rval
!= NULL_TREE
)
2539 type
= DECL_CONTEXT (TYPE_NAME (type
));
2543 if (TREE_CODE (inner_stuff
) == SCOPE_REF
)
2545 inner_name
= TREE_OPERAND (inner_stuff
, 0);
2546 inner_type
= TREE_OPERAND (inner_stuff
, 1);
2550 inner_name
= inner_stuff
;
2551 inner_type
= NULL_TREE
;
2554 if (outer_type
== NULL_TREE
)
2556 /* If we have something that's already a type by itself,
2558 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name
))
2561 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name
),
2568 if (! IS_AGGR_TYPE (outer_type
))
2571 /* Look for member classes or enums. */
2572 tmp
= find_scoped_type (outer_type
, inner_name
, inner_type
);
2574 /* If it's not a type in this class, then go down into the
2575 base classes and search there. */
2576 if (! tmp
&& TYPE_BINFO (outer_type
))
2578 tree binfos
= TYPE_BINFO_BASETYPES (outer_type
);
2579 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2581 for (i
= 0; i
< n_baselinks
; i
++)
2583 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2584 tmp
= resolve_scope_to_name (BINFO_TYPE (base_binfo
), inner_stuff
);
2594 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
2595 This is how virtual function calls are avoided. */
2597 build_scoped_method_call (exp
, scopes
, name
, parms
)
2598 tree exp
, scopes
, name
, parms
;
2600 /* Because this syntactic form does not allow
2601 a pointer to a base class to be `stolen',
2602 we need not protect the derived->base conversion
2605 @@ But we do have to check access privileges later. */
2606 tree basename
= resolve_scope_to_name (NULL_TREE
, scopes
);
2607 tree basetype
, binfo
, decl
;
2608 tree type
= TREE_TYPE (exp
);
2610 if (type
== error_mark_node
2611 || basename
== NULL_TREE
)
2612 return error_mark_node
;
2614 basetype
= IDENTIFIER_TYPE_VALUE (basename
);
2616 if (TREE_CODE (type
) == REFERENCE_TYPE
)
2617 type
= TREE_TYPE (type
);
2619 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
2620 that explicit ~int is caught in the parser; this deals with typedefs
2621 and template parms. */
2622 if (TREE_CODE (name
) == BIT_NOT_EXPR
&& ! is_aggr_typedef (basename
, 0))
2624 if (type
!= basetype
)
2625 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
2626 exp
, basetype
, type
);
2627 name
= IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name
, 0));
2628 if (basetype
!= name
)
2629 cp_error ("qualified type `%T' does not match destructor type `%T'",
2631 return void_zero_node
;
2634 if (! is_aggr_typedef (basename
, 1))
2635 return error_mark_node
;
2637 if (! IS_AGGR_TYPE (type
))
2639 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
2641 return error_mark_node
;
2644 if ((binfo
= binfo_or_else (basetype
, type
)))
2646 if (binfo
== error_mark_node
)
2647 return error_mark_node
;
2648 if (TREE_CODE (exp
) == INDIRECT_REF
)
2649 decl
= build_indirect_ref (convert_pointer_to (binfo
,
2650 build_unary_op (ADDR_EXPR
, exp
, 0)), NULL_PTR
);
2652 decl
= build_scoped_ref (exp
, scopes
);
2654 /* Call to a destructor. */
2655 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2657 /* Explicit call to destructor. */
2658 name
= TREE_OPERAND (name
, 0);
2659 if (name
!= constructor_name (TREE_TYPE (decl
)))
2662 ("qualified type `%T' does not match destructor type `%T'",
2663 TREE_TYPE (decl
), name
);
2664 return error_mark_node
;
2666 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
2667 return void_zero_node
;
2669 return build_delete (TREE_TYPE (decl
), decl
, integer_two_node
,
2670 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
2674 /* Call to a method. */
2675 return build_method_call (decl
, name
, parms
, binfo
,
2676 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
2678 return error_mark_node
;
2682 print_candidates (candidates
)
2685 cp_error_at ("candidates are: %D", TREE_VALUE (candidates
));
2686 candidates
= TREE_CHAIN (candidates
);
2690 cp_error_at (" %D", TREE_VALUE (candidates
));
2691 candidates
= TREE_CHAIN (candidates
);
2696 print_n_candidates (candidates
, n
)
2697 struct candidate
*candidates
;
2702 cp_error_at ("candidates are: %D", candidates
[0].function
);
2703 for (i
= 1; i
< n
; i
++)
2704 cp_error_at (" %D", candidates
[i
].function
);
2707 /* Build something of the form ptr->method (args)
2708 or object.method (args). This can also build
2709 calls to constructors, and find friends.
2711 Member functions always take their class variable
2714 INSTANCE is a class instance.
2716 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
2718 PARMS help to figure out what that NAME really refers to.
2720 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
2721 down to the real instance type to use for access checking. We need this
2722 information to get protected accesses correct. This parameter is used
2723 by build_member_call.
2725 FLAGS is the logical disjunction of zero or more LOOKUP_
2726 flags. See cp-tree.h for more info.
2728 If this is all OK, calls build_function_call with the resolved
2731 This function must also handle being called to perform
2732 initialization, promotion/coercion of arguments, and
2733 instantiation of default parameters.
2735 Note that NAME may refer to an instance variable name. If
2736 `operator()()' is defined for the type of that field, then we return
2739 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
2740 tree instance
, name
, parms
, basetype_path
;
2743 register tree function
, fntype
, value_type
;
2744 register tree basetype
, save_basetype
;
2745 register tree baselink
, result
, method_name
, parmtypes
, parm
;
2748 enum access_type access
= access_public
;
2750 /* Range of cases for vtable optimization. */
2751 enum vtable_needs
{ not_needed
, maybe_needed
, unneeded
, needed
};
2752 enum vtable_needs need_vtbl
= not_needed
;
2756 tree instance_ptr
= NULL_TREE
;
2757 int all_virtual
= flag_all_virtual
;
2758 int static_call_context
= 0;
2759 tree found_fns
= NULL_TREE
;
2761 /* Keep track of `const' and `volatile' objects. */
2762 int constp
, volatilep
;
2764 #ifdef GATHER_STATISTICS
2765 n_build_method_call
++;
2768 if (instance
== error_mark_node
2769 || name
== error_mark_node
2770 || parms
== error_mark_node
2771 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
2772 return error_mark_node
;
2774 /* This is the logic that magically deletes the second argument to
2775 operator delete, if it is not needed. */
2776 if (name
== ansi_opname
[(int) DELETE_EXPR
] && list_length (parms
)==2)
2778 tree save_last
= TREE_CHAIN (parms
);
2780 /* get rid of unneeded argument */
2781 TREE_CHAIN (parms
) = NULL_TREE
;
2782 result
= build_method_call (instance
, name
, parms
, basetype_path
,
2783 (LOOKUP_SPECULATIVELY
|flags
)
2785 /* If it works, return it. */
2786 if (result
&& result
!= error_mark_node
)
2787 return build_method_call (instance
, name
, parms
, basetype_path
, flags
);
2788 /* If it doesn't work, two argument delete must work */
2789 TREE_CHAIN (parms
) = save_last
;
2791 /* We already know whether it's needed or not for vec delete. */
2792 else if (name
== ansi_opname
[(int) VEC_DELETE_EXPR
]
2793 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance
)))
2794 TREE_CHAIN (parms
) = NULL_TREE
;
2796 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2798 flags
|= LOOKUP_DESTRUCTOR
;
2799 name
= TREE_OPERAND (name
, 0);
2801 error ("destructors take no parameters");
2802 basetype
= get_type_value (name
);
2803 if (basetype
== NULL_TREE
)
2805 cp_error ("call to destructor for non-type `%D'", name
);
2806 return void_zero_node
;
2808 if (basetype
!= TREE_TYPE(instance
))
2809 basetype
= TREE_TYPE(instance
);
2810 if (! TYPE_HAS_DESTRUCTOR (basetype
))
2811 return void_zero_node
;
2812 instance
= default_conversion (instance
);
2813 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
2814 return build_delete (build_pointer_type (basetype
),
2815 instance_ptr
, integer_two_node
,
2816 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
2822 /* Initialize name for error reporting. */
2823 if (IDENTIFIER_OPNAME_P (name
) && ! IDENTIFIER_TYPENAME_P (name
))
2825 char *p
= operator_name_string (name
);
2826 xref_name
= (char *)alloca (strlen (p
) + 10);
2827 sprintf (xref_name
, "operator %s", p
);
2829 else if (TREE_CODE (name
) == SCOPE_REF
)
2830 xref_name
= IDENTIFIER_POINTER (TREE_OPERAND (name
, 1));
2832 xref_name
= IDENTIFIER_POINTER (name
);
2834 GNU_xref_call (current_function_decl
, xref_name
);
2837 if (instance
== NULL_TREE
)
2839 basetype
= NULL_TREE
;
2840 /* Check cases where this is really a call to raise
2842 if (current_class_type
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
2844 basetype
= purpose_member (name
, CLASSTYPE_TAGS (current_class_type
));
2846 basetype
= TREE_VALUE (basetype
);
2848 else if (TREE_CODE (name
) == SCOPE_REF
2849 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
2851 if (! is_aggr_typedef (TREE_OPERAND (name
, 0), 1))
2852 return error_mark_node
;
2853 basetype
= purpose_member (TREE_OPERAND (name
, 1),
2854 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name
, 0))));
2856 basetype
= TREE_VALUE (basetype
);
2859 if (basetype
!= NULL_TREE
)
2861 /* call to a constructor... */
2862 else if (basetype_path
)
2863 basetype
= BINFO_TYPE (basetype_path
);
2864 else if (IDENTIFIER_HAS_TYPE_VALUE (name
))
2866 basetype
= IDENTIFIER_TYPE_VALUE (name
);
2867 name
= constructor_name_full (basetype
);
2871 tree typedef_name
= lookup_name (name
, 1);
2872 if (typedef_name
&& TREE_CODE (typedef_name
) == TYPE_DECL
)
2874 /* Canonicalize the typedef name. */
2875 basetype
= TREE_TYPE (typedef_name
);
2876 name
= TYPE_IDENTIFIER (basetype
);
2880 cp_error ("no constructor named `%T' in scope",
2882 return error_mark_node
;
2886 if (! IS_AGGR_TYPE (basetype
))
2889 if ((flags
& LOOKUP_COMPLAIN
) && TREE_CODE (basetype
) != ERROR_MARK
)
2890 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2891 name
, instance
, basetype
);
2893 return error_mark_node
;
2896 else if (instance
== C_C_D
|| instance
== current_class_decl
)
2898 /* When doing initialization, we side-effect the TREE_TYPE of
2899 C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
2900 basetype
= TREE_TYPE (C_C_D
);
2902 /* Anything manifestly `this' in constructors and destructors
2903 has a known type, so virtual function tables are not needed. */
2904 if (TYPE_VIRTUAL_P (basetype
)
2905 && !(flags
& LOOKUP_NONVIRTUAL
))
2906 need_vtbl
= (dtor_label
|| ctor_label
)
2907 ? unneeded
: maybe_needed
;
2910 instance_ptr
= current_class_decl
;
2911 result
= build_field_call (TYPE_BINFO (current_class_type
),
2912 instance_ptr
, name
, parms
);
2917 else if (TREE_CODE (instance
) == RESULT_DECL
)
2919 basetype
= TREE_TYPE (instance
);
2920 /* Should we ever have to make a virtual function reference
2921 from a RESULT_DECL, know that it must be of fixed type
2922 within the scope of this function. */
2923 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
2924 need_vtbl
= maybe_needed
;
2925 instance_ptr
= build1 (ADDR_EXPR
, TYPE_POINTER_TO (basetype
), instance
);
2929 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
2930 tree inst_ptr_basetype
;
2932 static_call_context
=
2933 (TREE_CODE (instance
) == INDIRECT_REF
2934 && TREE_CODE (TREE_OPERAND (instance
, 0)) == NOP_EXPR
2935 && TREE_OPERAND (TREE_OPERAND (instance
, 0), 0) == error_mark_node
);
2937 /* the base type of an instance variable is pointer to class */
2938 basetype
= TREE_TYPE (instance
);
2940 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
2942 basetype
= TREE_TYPE (basetype
);
2943 if (! IS_AGGR_TYPE (basetype
))
2944 goto non_aggr_error
;
2945 /* Call to convert not needed because we are remaining
2946 within the same type. */
2947 instance_ptr
= build1 (NOP_EXPR
, build_pointer_type (basetype
),
2949 inst_ptr_basetype
= TYPE_MAIN_VARIANT (basetype
);
2953 if (! IS_AGGR_TYPE (basetype
))
2954 goto non_aggr_error
;
2956 if (IS_SIGNATURE_POINTER (basetype
)
2957 || IS_SIGNATURE_REFERENCE (basetype
))
2958 basetype
= SIGNATURE_TYPE (basetype
);
2960 if ((IS_SIGNATURE (basetype
)
2961 && (instance_ptr
= build_optr_ref (instance
)))
2962 || (lvalue_p (instance
)
2963 && (instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0)))
2964 || (instance_ptr
= unary_complex_lvalue (ADDR_EXPR
, instance
)))
2966 if (instance_ptr
== error_mark_node
)
2967 return error_mark_node
;
2969 else if (TREE_CODE (instance
) == NOP_EXPR
2970 || TREE_CODE (instance
) == CONSTRUCTOR
)
2972 /* A cast is not an lvalue. Initialize a fresh temp
2973 with the value we are casting from, and proceed with
2974 that temporary. We can't cast to a reference type,
2975 so that simplifies the initialization to something
2977 tree temp
= get_temp_name (TREE_TYPE (instance
), 0);
2978 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
2979 expand_aggr_init (temp
, instance
, 0);
2982 store_init_value (temp
, instance
);
2983 expand_decl_init (temp
);
2986 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
2990 if (TREE_CODE (instance
) != CALL_EXPR
)
2991 my_friendly_abort (125);
2992 if (TYPE_NEEDS_CONSTRUCTING (basetype
))
2993 instance
= build_cplus_new (basetype
, instance
, 0);
2996 instance
= get_temp_name (basetype
, 0);
2997 TREE_ADDRESSABLE (instance
) = 1;
2999 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
3001 /* @@ Should we call comp_target_types here? */
3002 inst_ptr_basetype
= TREE_TYPE (TREE_TYPE (instance_ptr
));
3003 if (TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (inst_ptr_basetype
))
3004 basetype
= inst_ptr_basetype
;
3007 instance_ptr
= convert (TYPE_POINTER_TO (basetype
), instance_ptr
);
3008 if (instance_ptr
== error_mark_node
)
3009 return error_mark_node
;
3013 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
3014 not updated, so we use `basetype' instead. */
3015 if (basetype_path
== NULL_TREE
3016 && IS_SIGNATURE (basetype
))
3017 basetype_path
= TYPE_BINFO (basetype
);
3018 else if (basetype_path
== NULL_TREE
||
3019 BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (inst_ptr_basetype
))
3020 basetype_path
= TYPE_BINFO (inst_ptr_basetype
);
3022 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
3026 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
3028 if (TREE_SIDE_EFFECTS (instance_ptr
))
3030 /* This action is needed because the instance is needed
3031 for providing the base of the virtual function table.
3032 Without using a SAVE_EXPR, the function we are building
3033 may be called twice, or side effects on the instance
3034 variable (such as a post-increment), may happen twice. */
3035 instance_ptr
= save_expr (instance_ptr
);
3036 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
3038 else if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
3040 /* This happens when called for operator new (). */
3041 instance
= build_indirect_ref (instance
, NULL_PTR
);
3044 need_vtbl
= maybe_needed
;
3048 if (TYPE_SIZE (basetype
) == 0)
3050 /* This is worth complaining about, I think. */
3051 cp_error ("cannot lookup method in incomplete type `%T'", basetype
);
3052 return error_mark_node
;
3055 save_basetype
= TYPE_MAIN_VARIANT (basetype
);
3058 if (all_virtual
== 1
3059 && (! strncmp (IDENTIFIER_POINTER (name
), OPERATOR_METHOD_FORMAT
,
3060 OPERATOR_METHOD_LENGTH
)
3061 || instance_ptr
== NULL_TREE
3062 || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype
) == 0)))
3067 for (parmtypes
= NULL_TREE
, parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
3069 tree t
= TREE_TYPE (TREE_VALUE (parm
));
3070 if (TREE_CODE (t
) == OFFSET_TYPE
)
3072 /* Convert OFFSET_TYPE entities to their normal selves. */
3073 TREE_VALUE (parm
) = resolve_offset_ref (TREE_VALUE (parm
));
3074 t
= TREE_TYPE (TREE_VALUE (parm
));
3076 if (TREE_CODE (TREE_VALUE (parm
)) == OFFSET_REF
3077 && TREE_CODE (t
) == METHOD_TYPE
)
3079 TREE_VALUE (parm
) = build_unary_op (ADDR_EXPR
, TREE_VALUE (parm
), 0);
3081 if (TREE_CODE (t
) == ARRAY_TYPE
)
3083 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3084 This eliminates needless calls to `compute_conversion_costs'. */
3085 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
3086 t
= TREE_TYPE (TREE_VALUE (parm
));
3088 if (t
== error_mark_node
)
3089 return error_mark_node
;
3090 last
= build_tree_list (NULL_TREE
, t
);
3091 parmtypes
= chainon (parmtypes
, last
);
3096 /* TREE_READONLY (instance) fails for references. */
3097 constp
= TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr
)));
3098 volatilep
= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr
)));
3099 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
3103 /* Raw constructors are always in charge. */
3104 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
3105 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
3107 flags
|= LOOKUP_HAS_IN_CHARGE
;
3108 parms
= tree_cons (NULL_TREE
, integer_one_node
, parms
);
3109 parmtypes
= tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
3112 if (flag_this_is_variable
> 0)
3116 parms
= tree_cons (NULL_TREE
, build1 (NOP_EXPR
, TYPE_POINTER_TO (basetype
), integer_zero_node
), parms
);
3122 instance_ptr
= build_new (NULL_TREE
, basetype
, void_type_node
, 0);
3123 if (instance_ptr
== error_mark_node
)
3124 return error_mark_node
;
3125 instance_ptr
= save_expr (instance_ptr
);
3126 TREE_CALLS_NEW (instance_ptr
) = 1;
3127 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
3129 /* If it's a default argument initialized from a ctor, what we get
3130 from instance_ptr will match the arglist for the FUNCTION_DECL
3131 of the constructor. */
3132 if (parms
&& TREE_CODE (TREE_VALUE (parms
)) == CALL_EXPR
3133 && TREE_OPERAND (TREE_VALUE (parms
), 1)
3134 && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms
), 1))))
3135 parms
= build_tree_list (NULL_TREE
, instance_ptr
);
3137 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
3141 parmtypes
= tree_cons (NULL_TREE
, TREE_TYPE (instance_ptr
), parmtypes
);
3143 if (last
== NULL_TREE
)
3146 /* Look up function name in the structure type definition. */
3148 if ((IDENTIFIER_HAS_TYPE_VALUE (name
)
3149 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name
))
3150 && TREE_CODE(IDENTIFIER_TYPE_VALUE (name
)) != UNINSTANTIATED_P_TYPE
)
3151 || name
== constructor_name (basetype
))
3153 tree tmp
= NULL_TREE
;
3154 if (IDENTIFIER_TYPE_VALUE (name
) == basetype
3155 || name
== constructor_name (basetype
))
3156 tmp
= TYPE_BINFO (basetype
);
3158 tmp
= get_binfo (IDENTIFIER_TYPE_VALUE (name
), basetype
, 0);
3160 if (tmp
!= NULL_TREE
)
3162 name_kind
= "constructor";
3164 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
3165 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
3167 /* Constructors called for initialization
3168 only are never in charge. */
3171 flags
|= LOOKUP_HAS_IN_CHARGE
;
3172 tmplist
= tree_cons (NULL_TREE
, integer_zero_node
,
3173 TREE_CHAIN (parms
));
3174 TREE_CHAIN (parms
) = tmplist
;
3175 tmplist
= tree_cons (NULL_TREE
, integer_type_node
, TREE_CHAIN (parmtypes
));
3176 TREE_CHAIN (parmtypes
) = tmplist
;
3178 basetype
= BINFO_TYPE (tmp
);
3181 name_kind
= "method";
3184 name_kind
= "method";
3186 if (basetype_path
== NULL_TREE
3187 || BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (basetype
))
3188 basetype_path
= TYPE_BINFO (basetype
);
3189 result
= lookup_fnfields (basetype_path
, name
,
3190 (flags
& LOOKUP_COMPLAIN
));
3191 if (result
== error_mark_node
)
3192 return error_mark_node
;
3195 /* Now, go look for this method name. We do not find destructors here.
3197 Putting `void_list_node' on the end of the parmtypes
3198 fakes out `build_decl_overload' into doing the right thing. */
3199 TREE_CHAIN (last
) = void_list_node
;
3200 method_name
= build_decl_overload (name
, parmtypes
,
3201 1 + (name
== constructor_name (save_basetype
)
3202 || name
== constructor_name_full (save_basetype
)));
3203 TREE_CHAIN (last
) = NULL_TREE
;
3205 for (pass
= 0; pass
< 2; pass
++)
3207 struct candidate
*candidates
;
3208 struct candidate
*cp
;
3212 /* This increments every time we go up the type hierarchy.
3213 The idea is to prefer a function of the derived class if possible. */
3221 = (struct candidate
*) alloca ((ever_seen
+1)
3222 * sizeof (struct candidate
));
3223 bzero (candidates
, (ever_seen
+ 1) * sizeof (struct candidate
));
3225 len
= list_length (parms
);
3228 /* First see if a global function has a shot at it. */
3229 if (flags
& LOOKUP_GLOBAL
)
3232 tree parm
= instance_ptr
;
3234 if (TREE_CODE (TREE_TYPE (parm
)) == REFERENCE_TYPE
)
3236 /* TREE_VALUE (parms) may have been modified by now;
3237 restore it to its original value. */
3238 TREE_VALUE (parms
) = parm
;
3239 friend_parms
= parms
;
3241 else if (TREE_CODE (TREE_TYPE (parm
)) == POINTER_TYPE
)
3244 parm
= build_indirect_ref (parm
, "friendifying parms (compiler error)");
3245 new_type
= build_reference_type (TREE_TYPE (parm
));
3246 /* It is possible that this should go down a layer. */
3247 new_type
= build_type_variant (new_type
, constp
, volatilep
);
3248 parm
= convert (new_type
, parm
);
3249 friend_parms
= tree_cons (NULL_TREE
, parm
, TREE_CHAIN (parms
));
3252 my_friendly_abort (167);
3255 if (flag_ansi_overloading
)
3256 cp
->v
.ansi_harshness
= (struct harshness_code
*)
3257 alloca ((len
+ 1) * sizeof (struct harshness_code
));
3259 cp
->v
.old_harshness
= (unsigned short *)
3260 alloca ((len
+ 1) * sizeof (unsigned short));
3262 result
= build_overload_call (name
, friend_parms
, 0, cp
);
3263 /* If it turns out to be the one we were actually looking for
3264 (it was probably a friend function), the return the
3266 if (TREE_CODE (result
) == CALL_EXPR
)
3269 if (flag_ansi_overloading
)
3270 while ((cp
->h
.code
& EVIL_CODE
) == 0)
3272 /* non-standard uses: set the field to 0 to indicate
3273 we are using a non-member function. */
3275 if (cp
->v
.ansi_harshness
[len
].distance
== 0
3276 && cp
->h
.code
< best
)
3281 while (cp
->evil
== 0)
3283 /* non-standard uses: set the field to 0 to indicate
3284 we are using a non-member function. */
3286 if (cp
->v
.old_harshness
[len
] == 0
3287 && cp
->v
.old_harshness
[len
] == 0
3288 && cp
->ellipsis
== 0 && cp
->user
== 0 && cp
->b_or_d
== 0
3298 /* We have a hit (of sorts). If the parameter list is
3299 "error_mark_node", or some variant thereof, it won't
3300 match any methods. Since we have verified that the is
3301 some method vaguely matching this one (in name at least),
3304 Don't stop for friends, however. */
3305 basetype_path
= TREE_PURPOSE (baselink
);
3307 function
= TREE_VALUE (baselink
);
3308 if (TREE_CODE (basetype_path
) == TREE_LIST
)
3309 basetype_path
= TREE_VALUE (basetype_path
);
3310 basetype
= BINFO_TYPE (basetype_path
);
3312 /* Cast the instance variable if necessary. */
3313 if (basetype
!= TYPE_MAIN_VARIANT
3314 (TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)))))
3316 if (basetype
== save_basetype
)
3317 TREE_VALUE (parms
) = instance_ptr
;
3320 tree type
= build_pointer_type
3321 (build_type_variant (basetype
, constp
, volatilep
));
3322 TREE_VALUE (parms
) = convert_force (type
, instance_ptr
);
3326 /* FIXME: this is the wrong place to get an error. Hopefully
3327 the access-control rewrite will make this change more cleanly. */
3328 if (TREE_VALUE (parms
) == error_mark_node
)
3329 return error_mark_node
;
3331 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function
)))
3332 function
= DECL_CHAIN (function
);
3334 for (; function
; function
= DECL_CHAIN (function
))
3336 #ifdef GATHER_STATISTICS
3337 n_inner_fields_searched
++;
3341 found_fns
= tree_cons (NULL_TREE
, function
, found_fns
);
3343 /* Not looking for friends here. */
3344 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
3345 && ! DECL_STATIC_FUNCTION_P (function
))
3349 && DECL_ASSEMBLER_NAME (function
) == method_name
)
3354 tree these_parms
= parms
;
3356 #ifdef GATHER_STATISTICS
3357 n_inner_fields_searched
++;
3360 if (flag_ansi_overloading
)
3361 cp
->v
.ansi_harshness
= (struct harshness_code
*)
3362 alloca ((len
+ 1) * sizeof (struct harshness_code
));
3364 cp
->v
.old_harshness
= (unsigned short *)
3365 alloca ((len
+ 1) * sizeof (unsigned short));
3367 if (DECL_STATIC_FUNCTION_P (function
))
3368 these_parms
= TREE_CHAIN (these_parms
);
3369 compute_conversion_costs (function
, these_parms
, cp
, len
);
3371 if (!flag_ansi_overloading
)
3372 cp
->b_or_d
+= b_or_d
;
3374 if ((flag_ansi_overloading
&& (cp
->h
.code
& EVIL_CODE
) == 0)
3375 || (!flag_ansi_overloading
&& cp
->evil
== 0))
3377 cp
->u
.field
= function
;
3378 cp
->function
= function
;
3379 cp
->basetypes
= basetype_path
;
3381 /* No "two-level" conversions. */
3382 if (flags
& LOOKUP_NO_CONVERSION
3383 && ((flag_ansi_overloading
3384 && (cp
->h
.code
& USER_CODE
))
3385 || (!flag_ansi_overloading
3389 /* If we used default parameters, we must
3390 check to see whether anyone else might
3391 use them also, and report a possible
3393 if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype
)
3394 && ((flag_ansi_overloading
3395 && cp
->v
.ansi_harshness
[len
].distance
== 0
3396 && cp
->h
.code
< best
)
3397 || (!flag_ansi_overloading
3398 && cp
->v
.old_harshness
[len
] == 0
3399 && CONST_HARSHNESS (cp
->v
.old_harshness
[0]) == 0
3400 && cp
->ellipsis
== 0 && cp
->user
== 0 && cp
->b_or_d
== 0
3401 && cp
->easy
< best
)))
3403 if (! DECL_STATIC_FUNCTION_P (function
))
3404 TREE_VALUE (parms
) = cp
->arg
;
3406 goto found_and_maybe_warn
;
3412 /* Now we have run through one link's member functions.
3413 arrange to head-insert this link's links. */
3414 baselink
= next_baselink (baselink
);
3416 /* Don't grab functions from base classes. lookup_fnfield will
3417 do the work to get us down into the right place. */
3418 baselink
= NULL_TREE
;
3422 tree igv
= lookup_name_nonclass (name
);
3424 /* No exact match could be found. Now try to find match
3425 using default conversions. */
3426 if ((flags
& LOOKUP_GLOBAL
) && igv
)
3428 if (TREE_CODE (igv
) == FUNCTION_DECL
)
3430 else if (TREE_CODE (igv
) == TREE_LIST
)
3431 ever_seen
+= count_functions (igv
);
3436 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
3437 == LOOKUP_SPECULATIVELY
)
3440 TREE_CHAIN (last
) = void_list_node
;
3441 if (flags
& LOOKUP_GLOBAL
)
3442 cp_error ("no global or member function `%D(%A)' defined",
3445 cp_error ("no member function `%T::%D(%A)' defined",
3446 save_basetype
, name
, TREE_CHAIN (parmtypes
));
3447 return error_mark_node
;
3452 if (cp
- candidates
!= 0)
3454 /* Rank from worst to best. Then cp will point to best one.
3455 Private fields have their bits flipped. For unsigned
3456 numbers, this should make them look very large.
3457 If the best alternate has a (signed) negative value,
3458 then all we ever saw were private members. */
3459 if (cp
- candidates
> 1)
3461 int n_candidates
= cp
- candidates
;
3462 TREE_VALUE (parms
) = instance_ptr
;
3463 cp
= ideal_candidate (save_basetype
, candidates
,
3464 n_candidates
, parms
, len
);
3465 if (cp
== (struct candidate
*)0)
3467 if (flags
& LOOKUP_COMPLAIN
)
3469 cp_error ("call of overloaded %s `%D' is ambiguous",
3471 print_n_candidates (candidates
, n_candidates
);
3473 return error_mark_node
;
3475 if ((flag_ansi_overloading
&& (cp
->h
.code
& EVIL_CODE
))
3476 || (!flag_ansi_overloading
&& cp
->evil
))
3477 return error_mark_node
;
3479 else if ((flag_ansi_overloading
&& (cp
[-1].h
.code
& EVIL_CODE
))
3480 || (!flag_ansi_overloading
&& cp
[-1].evil
== 2))
3482 if (flags
& LOOKUP_COMPLAIN
)
3483 cp_error ("ambiguous type conversion requested for %s `%D'",
3485 return error_mark_node
;
3490 /* The global function was the best, so use it. */
3491 if (cp
->u
.field
== 0)
3493 /* We must convert the instance pointer into a reference type.
3494 Global overloaded functions can only either take
3495 aggregate objects (which come for free from references)
3496 or reference data types anyway. */
3497 TREE_VALUE (parms
) = copy_node (instance_ptr
);
3498 TREE_TYPE (TREE_VALUE (parms
)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr
)));
3499 return build_function_call (cp
->function
, parms
);
3502 function
= cp
->function
;
3503 basetype_path
= cp
->basetypes
;
3504 if (! DECL_STATIC_FUNCTION_P (function
))
3505 TREE_VALUE (parms
) = cp
->arg
;
3506 goto found_and_maybe_warn
;
3509 if (flags
& (LOOKUP_COMPLAIN
|LOOKUP_SPECULATIVELY
))
3511 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
3512 == LOOKUP_SPECULATIVELY
)
3515 if (DECL_STATIC_FUNCTION_P (cp
->function
))
3516 parms
= TREE_CHAIN (parms
);
3519 if (flags
& LOOKUP_SPECULATIVELY
)
3521 if (static_call_context
3522 && TREE_CODE (TREE_TYPE (cp
->function
)) == METHOD_TYPE
)
3523 cp_error ("object missing in call to `%D'", cp
->function
);
3524 else if (ever_seen
> 1)
3526 TREE_CHAIN (last
) = void_list_node
;
3527 cp_error ("no matching function for call to `%T::%D (%A)'",
3528 TREE_TYPE (TREE_TYPE (instance_ptr
)),
3529 name
, TREE_CHAIN (parmtypes
));
3530 TREE_CHAIN (last
) = NULL_TREE
;
3531 print_candidates (found_fns
);
3534 report_type_mismatch (cp
, parms
, name_kind
);
3535 return error_mark_node
;
3538 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
3541 cp_error ("%T has no method named %D", save_basetype
, name
);
3542 return error_mark_node
;
3548 found_and_maybe_warn
:
3549 if (((flag_ansi_overloading
3550 && (cp
->v
.ansi_harshness
[0].code
& CONST_CODE
))
3551 || (!flag_ansi_overloading
3552 && CONST_HARSHNESS (cp
->v
.old_harshness
[0])))
3553 /* 12.1p2: Constructors can be called for const objects. */
3554 && ! DECL_CONSTRUCTOR_P (cp
->function
))
3556 if (flags
& LOOKUP_COMPLAIN
)
3558 cp_error_at ("non-const member function `%D'", cp
->function
);
3559 error ("called for const object at this point in file");
3561 /* Not good enough for a match. */
3563 return error_mark_node
;
3567 /* Silently return error_mark_node. */
3568 return error_mark_node
;
3571 if (flags
& LOOKUP_PROTECT
)
3572 access
= compute_access (basetype_path
, function
);
3574 if (access
== access_private
)
3576 if (flags
& LOOKUP_COMPLAIN
)
3578 cp_error_at ("%s `%+#D' is %s", name_kind
, function
,
3579 TREE_PRIVATE (function
) ? "private"
3580 : "from private base class");
3581 error ("within this context");
3583 return error_mark_node
;
3585 else if (access
== access_protected
)
3587 if (flags
& LOOKUP_COMPLAIN
)
3589 cp_error_at ("%s `%+#D' %s", name_kind
, function
,
3590 TREE_PROTECTED (function
) ? "is protected"
3591 : "has protected accessibility");
3592 error ("within this context");
3594 return error_mark_node
;
3597 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
3598 type (if it exists) is a pointer to. */
3600 if (DECL_ABSTRACT_VIRTUAL_P (function
)
3601 && instance
== C_C_D
3602 && DECL_CONSTRUCTOR_P (current_function_decl
)
3603 && ! (flags
& LOOKUP_NONVIRTUAL
)
3604 && value_member (function
, get_abstract_virtuals (basetype
)))
3605 cp_error ("abstract virtual `%#D' called from constructor", function
);
3607 if (IS_SIGNATURE (basetype
) && static_call_context
)
3609 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
3611 return error_mark_node
;
3613 else if (IS_SIGNATURE (basetype
))
3614 return build_signature_method_call (basetype
, instance
, function
, parms
);
3616 function
= DECL_MAIN_VARIANT (function
);
3617 /* Declare external function if necessary. */
3618 assemble_external (function
);
3620 fntype
= TREE_TYPE (function
);
3621 if (TREE_CODE (fntype
) == POINTER_TYPE
)
3622 fntype
= TREE_TYPE (fntype
);
3623 basetype
= DECL_CLASS_CONTEXT (function
);
3625 /* If we are referencing a virtual function from an object
3626 of effectively static type, then there is no need
3627 to go through the virtual function table. */
3628 if (need_vtbl
== maybe_needed
)
3630 int fixed_type
= resolves_to_fixed_type_p (instance
, 0);
3632 if (all_virtual
== 1
3633 && DECL_VINDEX (function
)
3634 && may_be_remote (basetype
))
3636 else if (DECL_VINDEX (function
))
3637 need_vtbl
= fixed_type
? unneeded
: needed
;
3639 need_vtbl
= not_needed
;
3642 if (TREE_CODE (fntype
) == METHOD_TYPE
&& static_call_context
3643 && !DECL_CONSTRUCTOR_P (function
))
3645 /* Let's be nice to the user for now, and give reasonable
3646 default behavior. */
3647 instance_ptr
= current_class_decl
;
3650 if (basetype
!= current_class_type
)
3652 tree binfo
= get_binfo (basetype
, current_class_type
, 1);
3653 if (binfo
== NULL_TREE
)
3655 error_not_base_type (function
, current_class_type
);
3656 return error_mark_node
;
3658 else if (basetype
== error_mark_node
)
3659 return error_mark_node
;
3662 /* Only allow a static member function to call another static member
3664 else if (DECL_LANG_SPECIFIC (function
)
3665 && !DECL_STATIC_FUNCTION_P (function
))
3667 cp_error ("cannot call member function `%D' without object",
3669 return error_mark_node
;
3673 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
3675 if (TYPE_SIZE (value_type
) == 0)
3677 if (flags
& LOOKUP_COMPLAIN
)
3678 incomplete_type_error (0, value_type
);
3679 return error_mark_node
;
3682 /* We do not pass FUNCTION into `convert_arguments', because by
3683 now everything should be ok. If not, then we have a serious error. */
3684 if (DECL_STATIC_FUNCTION_P (function
))
3685 parms
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
3686 TREE_CHAIN (parms
), NULL_TREE
, LOOKUP_NORMAL
);
3687 else if (need_vtbl
== unneeded
)
3689 int sub_flags
= DECL_CONSTRUCTOR_P (function
) ? flags
: LOOKUP_NORMAL
;
3690 basetype
= TREE_TYPE (instance
);
3691 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function
)) != TYPE_MAIN_VARIANT (basetype
)
3692 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
3694 basetype
= DECL_CLASS_CONTEXT (function
);
3695 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
3696 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
3698 parms
= tree_cons (NULL_TREE
, instance_ptr
,
3699 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), NULL_TREE
, sub_flags
));
3703 if ((flags
& LOOKUP_NONVIRTUAL
) == 0)
3704 basetype
= DECL_CONTEXT (function
);
3706 /* First parm could be integer_zerop with casts like
3707 ((Object*)0)->Object::IsA() */
3708 if (!integer_zerop (TREE_VALUE (parms
)))
3710 /* Since we can't have inheritance with a union, doing get_binfo
3711 on it won't work. We do all the convert_pointer_to_real
3712 stuff to handle MI correctly...for unions, that's not
3713 an issue, so we must short-circuit that extra work here. */
3714 tree tmp
= TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)));
3715 if (tmp
!= NULL_TREE
&& TREE_CODE (tmp
) == UNION_TYPE
)
3716 instance_ptr
= TREE_VALUE (parms
);
3719 tree binfo
= get_binfo (basetype
,
3720 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
))),
3722 instance_ptr
= convert_pointer_to_real (binfo
, TREE_VALUE (parms
));
3725 = convert_pointer_to (build_type_variant (basetype
,
3729 if (TREE_CODE (instance_ptr
) == COND_EXPR
)
3731 instance_ptr
= save_expr (instance_ptr
);
3732 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
3734 else if (TREE_CODE (instance_ptr
) == NOP_EXPR
3735 && TREE_CODE (TREE_OPERAND (instance_ptr
, 0)) == ADDR_EXPR
3736 && TREE_OPERAND (TREE_OPERAND (instance_ptr
, 0), 0) == instance
)
3738 /* The call to `convert_pointer_to' may return error_mark_node. */
3739 else if (TREE_CODE (instance_ptr
) == ERROR_MARK
)
3740 return instance_ptr
;
3741 else if (instance
== NULL_TREE
3742 || TREE_CODE (instance
) != INDIRECT_REF
3743 || TREE_OPERAND (instance
, 0) != instance_ptr
)
3744 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
3746 parms
= tree_cons (NULL_TREE
, instance_ptr
,
3747 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), NULL_TREE
, LOOKUP_NORMAL
));
3751 /* Constructors do not overload method calls. */
3752 else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype
)
3753 && name
!= TYPE_IDENTIFIER (basetype
)
3754 && (TREE_CODE (function
) != FUNCTION_DECL
3755 || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)),
3756 OPERATOR_METHOD_FORMAT
,
3757 OPERATOR_METHOD_LENGTH
))
3758 && (may_be_remote (basetype
) || instance
!= C_C_D
))
3762 parms
= TREE_CHAIN (parms
);
3764 if (!all_virtual
&& TREE_CODE (function
) == FUNCTION_DECL
)
3765 fn_as_int
= build_unary_op (ADDR_EXPR
, function
, 0);
3767 fn_as_int
= convert (TREE_TYPE (default_conversion (function
)), DECL_VINDEX (function
));
3768 if (all_virtual
== 1)
3769 fn_as_int
= convert (integer_type_node
, fn_as_int
);
3771 result
= build_opfncall (METHOD_CALL_EXPR
, LOOKUP_NORMAL
, instance
, fn_as_int
, parms
);
3773 if (result
== NULL_TREE
)
3775 compiler_error ("could not overload `operator->()(...)'");
3776 return error_mark_node
;
3778 else if (result
== error_mark_node
)
3779 return error_mark_node
;
3782 /* Do this if we want the result of operator->() to inherit
3783 the type of the function it is subbing for. */
3784 TREE_TYPE (result
) = value_type
;
3791 if (need_vtbl
== needed
)
3793 function
= build_vfn_ref (&TREE_VALUE (parms
), instance
,
3794 DECL_VINDEX (function
));
3795 TREE_TYPE (function
) = build_pointer_type (fntype
);
3798 if (TREE_CODE (function
) == FUNCTION_DECL
)
3799 GNU_xref_call (current_function_decl
,
3800 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)));
3805 if (TREE_CODE (function
) == FUNCTION_DECL
)
3807 is_constructor
= DECL_CONSTRUCTOR_P (function
);
3808 if (DECL_INLINE (function
))
3809 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
3812 assemble_external (function
);
3813 TREE_USED (function
) = 1;
3814 function
= default_conversion (function
);
3820 function
= default_conversion (function
);
3823 result
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
3825 TREE_TYPE (result
) = value_type
;
3826 TREE_SIDE_EFFECTS (result
) = 1;
3827 TREE_RAISES (result
)
3828 = TYPE_RAISES_EXCEPTIONS (fntype
) || (parms
&& TREE_RAISES (parms
));
3829 TREE_HAS_CONSTRUCTOR (result
) = is_constructor
;
3834 /* Similar to `build_method_call', but for overloaded non-member functions.
3835 The name of this function comes through NAME. The name depends
3838 Note that this function must handle simple `C' promotions,
3839 as well as variable numbers of arguments (...), and
3840 default arguments to boot.
3842 If the overloading is successful, we return a tree node which
3843 contains the call to the function.
3845 If overloading produces candidates which are probable, but not definite,
3846 we hold these candidates. If FINAL_CP is non-zero, then we are free
3847 to assume that final_cp points to enough storage for all candidates that
3848 this function might generate. The `harshness' array is preallocated for
3849 the first candidate, but not for subsequent ones.
3851 Note that the DECL_RTL of FUNCTION must be made to agree with this
3852 function's new name. */
3855 build_overload_call_real (fnname
, parms
, flags
, final_cp
, buildxxx
)
3858 struct candidate
*final_cp
;
3861 /* must check for overloading here */
3862 tree overload_name
, functions
, function
, parm
;
3863 tree parmtypes
= NULL_TREE
, last
= NULL_TREE
;
3864 register tree outer
;
3866 int parmlength
= list_length (parms
);
3868 struct candidate
*candidates
, *cp
;
3872 if (flag_ansi_overloading
)
3874 final_cp
[0].h
.code
= 0;
3875 final_cp
[0].h
.distance
= 0;
3876 final_cp
[0].function
= 0;
3878 final_cp
[1].h
.code
= EVIL_CODE
;
3882 final_cp
[0].evil
= 0;
3883 final_cp
[0].user
= 0;
3884 final_cp
[0].b_or_d
= 0;
3885 final_cp
[0].easy
= 0;
3886 final_cp
[0].function
= 0;
3888 final_cp
[1].evil
= 1;
3892 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
3894 register tree t
= TREE_TYPE (TREE_VALUE (parm
));
3896 if (t
== error_mark_node
)
3900 if (flag_ansi_overloading
)
3901 final_cp
->h
.code
= EVIL_CODE
;
3905 return error_mark_node
;
3907 if (TREE_CODE (t
) == ARRAY_TYPE
|| TREE_CODE (t
) == OFFSET_TYPE
)
3909 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
3910 Also convert OFFSET_TYPE entities to their normal selves.
3911 This eliminates needless calls to `compute_conversion_costs'. */
3912 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
3913 t
= TREE_TYPE (TREE_VALUE (parm
));
3915 last
= build_tree_list (NULL_TREE
, t
);
3916 parmtypes
= chainon (parmtypes
, last
);
3919 TREE_CHAIN (last
) = void_list_node
;
3921 parmtypes
= void_list_node
;
3923 if (! flag_ansi_overloading
)
3927 /* This is a speed improvement that ends up not working properly in
3928 the situation of fns with and without default parameters. I turned
3929 this off in the new method so it'll go through the argument matching
3930 code to properly diagnose a match/failure. (bpk) */
3931 overload_name
= build_decl_overload (fnname
, parmtypes
, 0);
3932 fn
= lookup_name_nonclass (overload_name
);
3934 /* Now check to see whether or not we can win.
3935 Note that if we are called from `build_method_call',
3936 then we cannot have a mis-match, because we would have
3937 already found such a winning case. */
3939 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
)
3940 return build_function_call (DECL_MAIN_VARIANT (fn
), parms
);
3943 functions
= lookup_name_nonclass (fnname
);
3945 if (functions
== NULL_TREE
)
3947 if (flags
& LOOKUP_SPECULATIVELY
)
3949 if (flags
& LOOKUP_COMPLAIN
)
3950 error ("only member functions apply");
3953 if (flag_ansi_overloading
)
3954 final_cp
->h
.code
= EVIL_CODE
;
3958 return error_mark_node
;
3961 if (TREE_CODE (functions
) == FUNCTION_DECL
&& ! IDENTIFIER_OPNAME_P (fnname
))
3963 functions
= DECL_MAIN_VARIANT (functions
);
3966 /* We are just curious whether this is a viable alternative or
3968 compute_conversion_costs (functions
, parms
, final_cp
, parmlength
);
3972 return build_function_call_real (functions
, parms
, 1, flags
);
3975 if (TREE_CODE (functions
) == TREE_LIST
3976 && TREE_VALUE (functions
) == NULL_TREE
)
3978 if (flags
& LOOKUP_SPECULATIVELY
)
3981 if (flags
& LOOKUP_COMPLAIN
)
3982 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
3983 TREE_PURPOSE (functions
));
3986 if (flag_ansi_overloading
)
3987 final_cp
->h
.code
= EVIL_CODE
;
3991 return error_mark_node
;
3994 length
= count_functions (functions
);
3997 candidates
= final_cp
;
4001 = (struct candidate
*)alloca ((length
+1) * sizeof (struct candidate
));
4002 bzero (candidates
, (length
+ 1) * sizeof (struct candidate
));
4007 my_friendly_assert (is_overloaded_fn (functions
), 169);
4009 functions
= get_first_fn (functions
);
4011 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
4012 for (outer
= functions
; outer
; outer
= DECL_CHAIN (outer
))
4014 int template_cost
= 0;
4016 if (TREE_CODE (function
) != FUNCTION_DECL
4017 && ! (TREE_CODE (function
) == TEMPLATE_DECL
4018 && ! DECL_TEMPLATE_IS_CLASS (function
)
4019 && TREE_CODE (DECL_TEMPLATE_RESULT (function
)) == FUNCTION_DECL
))
4021 enum tree_code code
= TREE_CODE (function
);
4022 if (code
== TEMPLATE_DECL
)
4023 code
= TREE_CODE (DECL_TEMPLATE_RESULT (function
));
4024 if (code
== CONST_DECL
)
4026 ("enumeral value `%D' conflicts with function of same name",
4028 else if (code
== VAR_DECL
)
4030 if (TREE_STATIC (function
))
4032 ("variable `%D' conflicts with function of same name",
4036 ("constant field `%D' conflicts with function of same name",
4039 else if (code
== TYPE_DECL
)
4042 my_friendly_abort (2);
4043 error ("at this point in file");
4046 if (TREE_CODE (function
) == TEMPLATE_DECL
)
4048 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function
));
4049 tree
*targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
4052 i
= type_unification (DECL_TEMPLATE_PARMS (function
), targs
,
4053 TYPE_ARG_TYPES (TREE_TYPE (function
)),
4054 parms
, &template_cost
, 0);
4056 function
= instantiate_template (function
, targs
);
4059 if (TREE_CODE (function
) == TEMPLATE_DECL
)
4061 /* Unconverted template -- failed match. */
4062 cp
->function
= function
;
4064 if (flag_ansi_overloading
)
4065 cp
->h
.code
= EVIL_CODE
;
4071 struct candidate
*cp2
;
4073 /* Check that this decl is not the same as a function that's in
4074 the list due to some template instantiation. */
4077 if (cp2
->function
== function
)
4081 if (cp2
->function
== function
)
4084 function
= DECL_MAIN_VARIANT (function
);
4086 /* Can't use alloca here, since result might be
4087 passed to calling function. */
4088 cp
->h_len
= parmlength
;
4089 if (flag_ansi_overloading
)
4090 cp
->v
.ansi_harshness
= (struct harshness_code
*)
4091 oballoc ((parmlength
+ 1) * sizeof (struct harshness_code
));
4093 cp
->v
.old_harshness
= (unsigned short *)
4094 oballoc ((parmlength
+ 1) * sizeof (unsigned short));
4096 compute_conversion_costs (function
, parms
, cp
, parmlength
);
4098 if (flag_ansi_overloading
)
4099 /* Make sure this is clear as well. */
4100 cp
->h
.int_penalty
+= template_cost
;
4102 /* Should really add another field... */
4103 cp
->easy
= cp
->easy
* 128 + template_cost
;
4105 /* It seemed easier to have both if stmts in here, rather
4106 than excluding the hell out of it with flag_ansi_overloading
4107 everywhere. (bpk) */
4108 if (flag_ansi_overloading
)
4110 if ((cp
[0].h
.code
& EVIL_CODE
) == 0)
4112 cp
[1].h
.code
= EVIL_CODE
;
4114 /* int_penalty is set by convert_harshness_ansi for cases
4115 where we need to know about any penalties that would
4116 otherwise make a TRIVIAL_CODE pass. */
4118 && template_cost
== 0
4119 && cp
[0].h
.code
<= TRIVIAL_CODE
4120 && cp
[0].h
.int_penalty
== 0)
4122 final_cp
[0].h
= cp
[0].h
;
4130 if (cp
[0].evil
== 0)
4134 && cp
[0].user
== 0 && cp
[0].b_or_d
== 0
4135 && template_cost
== 0
4138 final_cp
[0].easy
= cp
[0].easy
;
4147 if (cp
- candidates
)
4149 tree rval
= error_mark_node
;
4152 if (flag_ansi_overloading
)
4153 cp
[0].h
.code
= EVIL_CODE
;
4156 if (cp
- candidates
> 1)
4158 struct candidate
*best_cp
4159 = ideal_candidate (NULL_TREE
, candidates
,
4160 cp
- candidates
, parms
, parmlength
);
4161 if (best_cp
== (struct candidate
*)0)
4163 if (flags
& LOOKUP_COMPLAIN
)
4165 cp_error ("call of overloaded `%D' is ambiguous", fnname
);
4166 print_n_candidates (candidates
, cp
- candidates
);
4168 return error_mark_node
;
4171 rval
= best_cp
->function
;
4176 if ((flag_ansi_overloading
&& (cp
->h
.code
& EVIL_CODE
))
4177 || (!flag_ansi_overloading
&& cp
->evil
> 1))
4179 if (flags
& LOOKUP_COMPLAIN
)
4180 error ("type conversion ambiguous");
4183 rval
= cp
->function
;
4189 return buildxxx
? build_function_call_real (rval
, parms
, 0, flags
)
4190 : build_function_call_real (rval
, parms
, 1, flags
);
4193 if (flags
& LOOKUP_SPECULATIVELY
)
4196 if (flags
& LOOKUP_COMPLAIN
)
4197 report_type_mismatch (cp
, parms
, "function",
4198 decl_as_string (cp
->function
, 1));
4200 return error_mark_node
;
4204 build_overload_call (fnname
, parms
, flags
, final_cp
)
4207 struct candidate
*final_cp
;
4209 return build_overload_call_real (fnname
, parms
, flags
, final_cp
, 0);
4213 build_overload_call_maybe (fnname
, parms
, flags
, final_cp
)
4216 struct candidate
*final_cp
;
4218 return build_overload_call_real (fnname
, parms
, flags
, final_cp
, 1);