1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 hacked by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
40 extern int inhibit_warnings
;
41 extern tree ctor_label
, dtor_label
;
43 /* Compute the ease with which a conversion can be performed
44 between an expected and the given type. */
45 static struct harshness_code convert_harshness
PROTO((register tree
, register tree
, tree
));
47 #define EVIL_RETURN(ARG) ((ARG).code = EVIL_CODE, (ARG))
48 #define STD_RETURN(ARG) ((ARG).code = STD_CODE, (ARG))
49 #define QUAL_RETURN(ARG) ((ARG).code = QUAL_CODE, (ARG))
50 #define TRIVIAL_RETURN(ARG) ((ARG).code = TRIVIAL_CODE, (ARG))
51 #define ZERO_RETURN(ARG) ((ARG).code = 0, (ARG))
53 /* Ordering function for overload resolution. Compare two candidates
56 rank_for_overload (x
, y
)
57 struct candidate
*x
, *y
;
59 if (y
->h
.code
& (EVIL_CODE
|ELLIPSIS_CODE
|USER_CODE
))
60 return y
->h
.code
- x
->h
.code
;
61 if (x
->h
.code
& (EVIL_CODE
|ELLIPSIS_CODE
|USER_CODE
))
64 /* This is set by compute_conversion_costs, for calling a non-const
65 member function from a const member function. */
66 if ((y
->harshness
[0].code
& CONST_CODE
) ^ (x
->harshness
[0].code
& CONST_CODE
))
67 return y
->harshness
[0].code
- x
->harshness
[0].code
;
69 if (y
->h
.code
& STD_CODE
)
71 if (x
->h
.code
& STD_CODE
)
72 return y
->h
.distance
- x
->h
.distance
;
75 if (x
->h
.code
& STD_CODE
)
78 return y
->h
.code
- x
->h
.code
;
81 /* Compare two candidates, argument by argument. */
84 struct candidate
*x
, *y
;
88 if (x
->h_len
!= y
->h_len
)
91 for (i
= 0; i
< x
->h_len
; i
++)
93 if (y
->harshness
[i
].code
- x
->harshness
[i
].code
)
94 return y
->harshness
[i
].code
- x
->harshness
[i
].code
;
95 if ((y
->harshness
[i
].code
& STD_CODE
)
96 && (y
->harshness
[i
].distance
- x
->harshness
[i
].distance
))
97 return y
->harshness
[i
].distance
- x
->harshness
[i
].distance
;
99 /* They're both the same code. Now see if we're dealing with an
100 integral promotion that needs a finer grain of accuracy. */
101 if (y
->harshness
[0].code
& PROMO_CODE
102 && (y
->harshness
[i
].int_penalty
^ x
->harshness
[i
].int_penalty
))
103 return y
->harshness
[i
].int_penalty
- x
->harshness
[i
].int_penalty
;
108 /* TYPE is the type we wish to convert to. PARM is the parameter
109 we have to work with. We use a somewhat arbitrary cost function
110 to measure this conversion. */
111 static struct harshness_code
112 convert_harshness (type
, parmtype
, parm
)
113 register tree type
, parmtype
;
116 struct harshness_code h
;
117 register enum tree_code codel
;
118 register enum tree_code coder
;
125 #ifdef GATHER_STATISTICS
126 n_convert_harshness
++;
129 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
)
132 parm
= convert_from_reference (parm
);
133 parmtype
= TREE_TYPE (parmtype
);
137 lvalue
= lvalue_p (parm
);
141 if (TYPE_PTRMEMFUNC_P (type
))
142 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
143 if (TYPE_PTRMEMFUNC_P (parmtype
))
144 parmtype
= TYPE_PTRMEMFUNC_FN_TYPE (parmtype
);
146 codel
= TREE_CODE (type
);
147 coder
= TREE_CODE (parmtype
);
149 if (TYPE_MAIN_VARIANT (parmtype
) == TYPE_MAIN_VARIANT (type
))
150 return ZERO_RETURN (h
);
152 if (coder
== ERROR_MARK
)
153 return EVIL_RETURN (h
);
155 if (codel
== REFERENCE_TYPE
)
158 int constp
= parm
? TREE_READONLY (parm
) : TYPE_READONLY (parmtype
);
159 int volatilep
= (parm
? TREE_THIS_VOLATILE (parm
)
160 : TYPE_VOLATILE (parmtype
));
161 register tree intype
= TYPE_MAIN_VARIANT (parmtype
);
162 register enum tree_code form
= TREE_CODE (intype
);
165 ttl
= TREE_TYPE (type
);
167 /* Only allow const reference binding if we were given a parm to deal
168 with, since it isn't really a conversion. This is a hack to
169 prevent build_type_conversion from finding this conversion, but
170 still allow overloading to find it. */
171 if (! lvalue
&& ! (parm
&& TYPE_READONLY (ttl
)))
172 return EVIL_RETURN (h
);
174 if ((TYPE_READONLY (ttl
) < constp
)
175 || (TYPE_VOLATILE (ttl
) < volatilep
))
176 return EVIL_RETURN (h
);
178 /* When passing a non-const argument into a const reference, dig it a
179 little, so a non-const reference is preferred over this one. */
180 penalty
= ((TYPE_READONLY (ttl
) > constp
)
181 + (TYPE_VOLATILE (ttl
) > volatilep
));
183 ttl
= TYPE_MAIN_VARIANT (ttl
);
185 if (form
== OFFSET_TYPE
)
187 intype
= TREE_TYPE (intype
);
188 form
= TREE_CODE (intype
);
193 if (TREE_CODE (ttl
) == ARRAY_TYPE
&& TREE_CODE (ttr
) == ARRAY_TYPE
)
195 if (comptypes (ttl
, ttr
, 1))
196 return ZERO_RETURN (h
);
197 return EVIL_RETURN (h
);
200 h
= convert_harshness (ttl
, ttr
, NULL_TREE
);
201 if (penalty
&& h
.code
== 0)
204 h
.int_penalty
= penalty
;
209 if (codel
== POINTER_TYPE
&& fntype_p (parmtype
))
212 struct harshness_code h1
, h2
;
214 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
215 type
= TREE_TYPE (type
);
217 if (coder
== POINTER_TYPE
)
219 parmtype
= TREE_TYPE (parmtype
);
220 coder
= TREE_CODE (parmtype
);
223 if (coder
!= TREE_CODE (type
))
224 return EVIL_RETURN (h
);
226 if (type
!= parmtype
&& coder
== METHOD_TYPE
)
228 tree ttl
= TYPE_METHOD_BASETYPE (type
);
229 tree ttr
= TYPE_METHOD_BASETYPE (parmtype
);
231 int b_or_d
= get_base_distance (ttr
, ttl
, 0, (tree
*)0);
234 b_or_d
= get_base_distance (ttl
, ttr
, 0, (tree
*)0);
236 return EVIL_RETURN (h
);
237 h
.distance
= -b_or_d
;
243 type
= build_function_type
244 (TREE_TYPE (type
), TREE_CHAIN (TYPE_ARG_TYPES (type
)));
245 parmtype
= build_function_type
246 (TREE_TYPE (parmtype
), TREE_CHAIN (TYPE_ARG_TYPES (parmtype
)));
249 /* We allow the default conversion between function type
250 and pointer-to-function type for free. */
251 if (comptypes (type
, parmtype
, 1))
255 return EVIL_RETURN (h
);
257 /* Compare return types. */
258 p1
= TREE_TYPE (type
);
259 p2
= TREE_TYPE (parmtype
);
260 h2
= convert_harshness (p1
, p2
, NULL_TREE
);
261 if (h2
.code
& EVIL_CODE
)
264 h1
.code
= TRIVIAL_CODE
;
267 if (h2
.distance
!= 0)
271 /* This only works for pointers. */
272 if (TREE_CODE (p1
) != POINTER_TYPE
273 && TREE_CODE (p1
) != REFERENCE_TYPE
)
274 return EVIL_RETURN (h
);
278 /* Don't die if we happen to be dealing with void*. */
279 if (!IS_AGGR_TYPE (p1
) || !IS_AGGR_TYPE (p2
))
280 return EVIL_RETURN (h
);
282 binfo
= get_binfo (p2
, p1
, 0);
284 binfo
= get_binfo (p1
, p2
, 0);
286 if (! BINFO_OFFSET_ZEROP (binfo
))
289 static int explained
= 0;
291 message_2_types (sorry
, "cannot cast `%s' to `%s' at function call site", p2
, p1
);
293 message_2_types (sorry
, "cannot cast `%s' to `%s' at function call site", p1
, p2
);
296 sorry ("(because pointer values change during conversion)");
298 return EVIL_RETURN (h
);
303 if (h2
.distance
> h1
.distance
)
304 h1
.distance
= h2
.distance
;
306 p1
= TYPE_ARG_TYPES (type
);
307 p2
= TYPE_ARG_TYPES (parmtype
);
308 while (p1
&& TREE_VALUE (p1
) != void_type_node
309 && p2
&& TREE_VALUE (p2
) != void_type_node
)
311 h2
= convert_harshness (TREE_VALUE (p1
), TREE_VALUE (p2
),
313 if (h2
.code
& EVIL_CODE
)
318 /* This only works for pointers and references. */
319 if (TREE_CODE (TREE_VALUE (p1
)) != POINTER_TYPE
320 && TREE_CODE (TREE_VALUE (p1
)) != REFERENCE_TYPE
)
321 return EVIL_RETURN (h
);
322 h2
.distance
= - h2
.distance
;
326 if (h2
.distance
> h1
.distance
)
327 h1
.distance
= h2
.distance
;
328 p1
= TREE_CHAIN (p1
);
329 p2
= TREE_CHAIN (p2
);
336 return EVIL_RETURN (h
);
337 h1
.code
|= ELLIPSIS_CODE
;
342 if (TREE_PURPOSE (p1
) == NULL_TREE
)
343 h1
.code
|= EVIL_CODE
;
347 else if (codel
== POINTER_TYPE
&& coder
== OFFSET_TYPE
)
351 /* Get to the OFFSET_TYPE that this might be. */
352 type
= TREE_TYPE (type
);
354 if (coder
!= TREE_CODE (type
))
355 return EVIL_RETURN (h
);
357 ttl
= TYPE_OFFSET_BASETYPE (type
);
358 ttr
= TYPE_OFFSET_BASETYPE (parmtype
);
364 int b_or_d
= get_base_distance (ttr
, ttl
, 0, (tree
*)0);
367 b_or_d
= get_base_distance (ttl
, ttr
, 0, (tree
*)0);
369 return EVIL_RETURN (h
);
370 h
.distance
= -b_or_d
;
377 /* Now test the OFFSET_TYPE's target compatibility. */
378 type
= TREE_TYPE (type
);
379 parmtype
= TREE_TYPE (parmtype
);
382 if (coder
== UNKNOWN_TYPE
)
384 if (codel
== FUNCTION_TYPE
385 || codel
== METHOD_TYPE
386 || (codel
== POINTER_TYPE
387 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
388 || TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)))
389 return TRIVIAL_RETURN (h
);
390 return EVIL_RETURN (h
);
393 if (coder
== VOID_TYPE
)
394 return EVIL_RETURN (h
);
396 if (codel
== BOOLEAN_TYPE
)
398 if (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
)
399 return STD_RETURN (h
);
400 else if (coder
== POINTER_TYPE
|| coder
== OFFSET_TYPE
)
402 /* Make this worse than any conversion to another pointer.
403 FIXME this is how I think the language should work, but it may not
404 end up being how the language is standardized (jason 1/30/95). */
406 return STD_RETURN (h
);
408 return EVIL_RETURN (h
);
411 if (INTEGRAL_CODE_P (codel
))
413 /* Control equivalence of ints an enums. */
415 if (codel
== ENUMERAL_TYPE
416 && flag_int_enum_equivalence
== 0)
418 /* Enums can be converted to ints, but not vice-versa. */
419 if (coder
!= ENUMERAL_TYPE
420 || TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (parmtype
))
421 return EVIL_RETURN (h
);
424 /* else enums and ints (almost) freely interconvert. */
426 if (INTEGRAL_CODE_P (coder
))
428 if (TYPE_MAIN_VARIANT (type
)
429 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
438 else if (coder
== REAL_TYPE
)
446 if (codel
== REAL_TYPE
)
448 if (coder
== REAL_TYPE
)
450 if (TYPE_MAIN_VARIANT (type
)
451 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
458 else if (INTEGRAL_CODE_P (coder
))
466 /* Convert arrays which have not previously been converted. */
467 if (coder
== ARRAY_TYPE
)
469 coder
= POINTER_TYPE
;
472 parm
= decay_conversion (parm
);
473 parmtype
= TREE_TYPE (parm
);
476 parmtype
= build_pointer_type (TREE_TYPE (parmtype
));
479 /* Conversions among pointers */
480 if (codel
== POINTER_TYPE
&& coder
== POINTER_TYPE
)
482 register tree ttl
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
483 register tree ttr
= TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
));
484 int penalty
= 4 * (ttl
!= ttr
);
486 /* Anything converts to void *. Since this may be `const void *'
487 (etc.) use VOID_TYPE instead of void_type_node. Otherwise, the
488 targets must be the same, except that we do allow (at some cost)
489 conversion between signed and unsigned pointer types. */
491 if ((TREE_CODE (ttl
) == METHOD_TYPE
492 || TREE_CODE (ttl
) == FUNCTION_TYPE
)
493 && TREE_CODE (ttl
) == TREE_CODE (ttr
))
495 if (comptypes (ttl
, ttr
, -1))
497 h
.code
= penalty
? STD_CODE
: 0;
506 if (TREE_CODE (ttl
) != VOID_TYPE
507 && (TREE_CODE (ttr
) != VOID_TYPE
|| !parm
|| !integer_zerop (parm
)))
509 if (comp_target_types (type
, parmtype
, 1) <= 0)
510 return EVIL_RETURN (h
);
513 if (!(TREE_CODE (ttl
) == VOID_TYPE
514 || TREE_CODE (ttr
) == VOID_TYPE
515 || (TREE_UNSIGNED (ttl
) ^ TREE_UNSIGNED (ttr
)
516 && (ttl
= unsigned_type (ttl
),
517 ttr
= unsigned_type (ttr
),
519 || (comp_target_types (ttl
, ttr
, 0) > 0)))
520 return EVIL_RETURN (h
);
525 tree tmp1
= TREE_TYPE (type
), tmp2
= TREE_TYPE (parmtype
);
528 /* Note conversion from `T*' to `const T*',
529 or `T*' to `volatile T*'. */
530 if ((TYPE_READONLY (tmp1
) < TREE_READONLY (tmp2
))
531 || (TYPE_VOLATILE (tmp1
) < TYPE_VOLATILE (tmp2
)))
533 else if ((TYPE_READONLY (tmp1
) != TREE_READONLY (tmp2
))
534 || (TYPE_VOLATILE (tmp1
) != TYPE_VOLATILE (tmp2
)))
542 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
544 int b_or_d
= get_base_distance (ttl
, ttr
, 0, (tree
*)0);
547 b_or_d
= get_base_distance (ttr
, ttl
, 0, (tree
*)0);
549 return EVIL_RETURN (h
);
550 h
.distance
= -b_or_d
;
558 /* If converting from a `class*' to a `void*', make it
559 less favorable than any inheritance relationship. */
560 if (TREE_CODE (ttl
) == VOID_TYPE
&& IS_AGGR_TYPE (ttr
))
563 h
.distance
= CLASSTYPE_MAX_DEPTH (ttr
)+1;
567 h
.code
= penalty
? STD_CODE
: PROMO_CODE
;
568 /* Catch things like `const char *' -> `const void *'
569 vs `const char *' -> `void *'. */
572 tree tmp1
= TREE_TYPE (type
), tmp2
= TREE_TYPE (parmtype
);
573 if ((TYPE_READONLY (tmp1
) < TREE_READONLY (tmp2
))
574 || (TYPE_VOLATILE (tmp1
) < TYPE_VOLATILE (tmp2
)))
576 else if ((TYPE_READONLY (tmp1
) > TREE_READONLY (tmp2
))
577 || (TYPE_VOLATILE (tmp1
) > TYPE_VOLATILE (tmp2
)))
583 if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
585 /* This is not a bad match, but don't let it beat
586 integer-enum combinations. */
587 if (parm
&& integer_zerop (parm
))
595 /* C++: Since the `this' parameter of a signature member function
596 is represented as a signature pointer to handle default implementations
597 correctly, we can have the case that `type' is a signature pointer
598 while `parmtype' is a pointer to a signature table. We don't really
599 do any conversions in this case, so just return 0. */
601 if (codel
== RECORD_TYPE
&& coder
== POINTER_TYPE
602 && IS_SIGNATURE_POINTER (type
) && IS_SIGNATURE (TREE_TYPE (parmtype
)))
603 return ZERO_RETURN (h
);
605 if (codel
== RECORD_TYPE
&& coder
== RECORD_TYPE
)
607 int b_or_d
= get_base_distance (type
, parmtype
, 0, (tree
*)0);
610 b_or_d
= get_base_distance (parmtype
, type
, 0, (tree
*)0);
612 return EVIL_RETURN (h
);
613 h
.distance
= -b_or_d
;
620 return EVIL_RETURN (h
);
623 /* A clone of build_type_conversion for checking user-defined conversions in
624 overload resolution. */
627 user_harshness (type
, parmtype
)
628 register tree type
, parmtype
;
631 tree winner
= NULL_TREE
;
635 tree typename
= build_typename_overload (type
);
636 if (lookup_fnfields (TYPE_BINFO (parmtype
), typename
, 0))
640 for (conv
= lookup_conversions (parmtype
); conv
; conv
= TREE_CHAIN (conv
))
642 struct harshness_code tmp
;
644 if (winner
&& TREE_PURPOSE (winner
) == TREE_PURPOSE (conv
))
647 if (tmp
= convert_harshness (type
, TREE_VALUE (conv
), NULL_TREE
),
648 (tmp
.code
< USER_CODE
) && (tmp
.distance
>= 0))
667 can_convert (to
, from
)
670 struct harshness_code h
;
671 h
= convert_harshness (to
, from
, NULL_TREE
);
672 return (h
.code
< USER_CODE
) && (h
.distance
>= 0);
676 can_convert_arg (to
, from
, arg
)
679 struct harshness_code h
;
680 h
= convert_harshness (to
, from
, arg
);
681 return (h
.code
< USER_CODE
) && (h
.distance
>= 0);
684 #ifdef DEBUG_MATCHING
687 struct harshness_code
*h
;
689 static char buf
[1024];
692 bzero (buf
, 1024 * sizeof (char));
693 strcat (buf
, "codes=[");
694 if (h
->code
& EVIL_CODE
)
695 strcat (buf
, "EVIL");
696 if (h
->code
& CONST_CODE
)
697 strcat (buf
, " CONST");
698 if (h
->code
& ELLIPSIS_CODE
)
699 strcat (buf
, " ELLIPSIS");
700 if (h
->code
& USER_CODE
)
701 strcat (buf
, " USER");
702 if (h
->code
& STD_CODE
)
703 strcat (buf
, " STD");
704 if (h
->code
& PROMO_CODE
)
705 strcat (buf
, " PROMO");
706 if (h
->code
& QUAL_CODE
)
707 strcat (buf
, " QUAL");
708 if (h
->code
& TRIVIAL_CODE
)
709 strcat (buf
, " TRIVIAL");
713 sprintf (tmp
, "] distance=%d int_penalty=%d", h
->distance
, h
->int_penalty
);
721 /* Algorithm: For each argument, calculate how difficult it is to
722 make FUNCTION accept that argument. If we can easily tell that
723 FUNCTION won't be acceptable to one of the arguments, then we
724 don't need to compute the ease of converting the other arguments,
725 since it will never show up in the intersection of all arguments'
728 Conversions between builtin and user-defined types are allowed, but
729 no function involving such a conversion is preferred to one which
730 does not require such a conversion. Furthermore, such conversions
734 compute_conversion_costs (function
, tta_in
, cp
, arglen
)
737 struct candidate
*cp
;
740 tree ttf_in
= TYPE_ARG_TYPES (TREE_TYPE (function
));
744 /* Start out with no strikes against. */
745 int evil_strikes
= 0;
746 int ellipsis_strikes
= 0;
747 int user_strikes
= 0;
748 int b_or_d_strikes
= 0;
749 int easy_strikes
= 0;
751 int strike_index
= 0, win
;
752 struct harshness_code lose
;
753 extern int cp_silent
;
755 #ifdef GATHER_STATISTICS
756 n_compute_conversion_costs
++;
759 #ifndef DEBUG_MATCHING
760 /* We don't emit any warnings or errors while trying out each candidate. */
764 cp
->function
= function
;
765 cp
->arg
= tta
? TREE_VALUE (tta
) : NULL_TREE
;
766 cp
->u
.bad_arg
= 0; /* optimistic! */
770 cp
->h
.int_penalty
= 0;
771 bzero ((char *) cp
->harshness
,
772 (cp
->h_len
+ 1) * sizeof (struct harshness_code
));
776 struct harshness_code h
;
778 if (ttf
== void_list_node
)
781 if (type_unknown_p (TREE_VALUE (tta
)))
783 /* Must perform some instantiation here. */
784 tree rhs
= TREE_VALUE (tta
);
785 tree lhstype
= TREE_VALUE (ttf
);
787 /* Keep quiet about possible contravariance violations. */
788 int old_inhibit_warnings
= inhibit_warnings
;
789 inhibit_warnings
= 1;
791 /* @@ This is to undo what `grokdeclarator' does to
792 parameter types. It really should go through
793 something more general. */
795 TREE_TYPE (tta
) = unknown_type_node
;
796 rhs
= instantiate_type (lhstype
, rhs
, 0);
797 inhibit_warnings
= old_inhibit_warnings
;
799 if (TREE_CODE (rhs
) == ERROR_MARK
)
802 h
= convert_harshness (lhstype
, TREE_TYPE (rhs
), rhs
);
806 #ifdef DEBUG_MATCHING
807 static tree old_function
= NULL_TREE
;
809 if (!old_function
|| function
!= old_function
)
811 cp_error ("trying %D", function
);
812 old_function
= function
;
815 cp_error (" doing (%T) %E against arg %T",
816 TREE_TYPE (TREE_VALUE (tta
)), TREE_VALUE (tta
),
820 h
= convert_harshness (TREE_VALUE (ttf
),
821 TREE_TYPE (TREE_VALUE (tta
)),
824 #ifdef DEBUG_MATCHING
825 cp_error (" evaluated %s", print_harshness (&h
));
829 cp
->harshness
[strike_index
] = h
;
830 if ((h
.code
& EVIL_CODE
)
831 || ((h
.code
& STD_CODE
) && h
.distance
< 0))
833 cp
->u
.bad_arg
= strike_index
;
836 else if (h
.code
& ELLIPSIS_CODE
)
837 ellipsis_strikes
+= 1;
839 /* This is never set by `convert_harshness'. */
840 else if (h
.code
& USER_CODE
)
847 if ((h
.code
& STD_CODE
) && h
.distance
)
849 if (h
.distance
> b_or_d_strikes
)
850 b_or_d_strikes
= h
.distance
;
853 easy_strikes
+= (h
.code
& (STD_CODE
|PROMO_CODE
|TRIVIAL_CODE
));
854 cp
->h
.code
|= h
.code
;
855 /* Make sure we communicate this. */
856 cp
->h
.int_penalty
+= h
.int_penalty
;
859 ttf
= TREE_CHAIN (ttf
);
860 tta
= TREE_CHAIN (tta
);
866 /* ran out of formals, and parmlist is fixed size. */
867 if (ttf
/* == void_type_node */)
869 cp
->h
.code
= EVIL_CODE
;
876 struct harshness_code h
;
877 int l
= list_length (tta
);
878 ellipsis_strikes
+= l
;
879 h
.code
= ELLIPSIS_CODE
;
883 cp
->harshness
[strike_index
++] = h
;
886 else if (ttf
&& ttf
!= void_list_node
)
888 /* ran out of actuals, and no defaults. */
889 if (TREE_PURPOSE (ttf
) == NULL_TREE
)
891 cp
->h
.code
= EVIL_CODE
;
896 /* Store index of first default. */
897 cp
->harshness
[arglen
].distance
= strike_index
+1;
900 cp
->harshness
[arglen
].distance
= 0;
902 /* Argument list lengths work out, so don't need to check them again. */
905 /* We do not check for derived->base conversions here, since in
906 no case would they give evil strike counts, unless such conversions
907 are somehow ambiguous. */
909 /* See if any user-defined conversions apply.
910 But make sure that we do not loop. */
911 static int dont_convert_types
= 0;
913 if (dont_convert_types
)
915 cp
->h
.code
= EVIL_CODE
;
920 win
= 0; /* Only get one chance to win. */
921 ttf
= TYPE_ARG_TYPES (TREE_TYPE (function
));
928 if (ttf
== void_list_node
)
931 lose
= cp
->harshness
[strike_index
];
932 if ((lose
.code
& EVIL_CODE
)
933 || ((lose
.code
& STD_CODE
) && lose
.distance
< 0))
935 tree actual_type
= TREE_TYPE (TREE_VALUE (tta
));
936 tree formal_type
= TREE_VALUE (ttf
);
937 int extra_conversions
= 0;
939 dont_convert_types
= 1;
941 if (TREE_CODE (formal_type
) == REFERENCE_TYPE
)
942 formal_type
= TREE_TYPE (formal_type
);
943 if (TREE_CODE (actual_type
) == REFERENCE_TYPE
)
944 actual_type
= TREE_TYPE (actual_type
);
946 if (formal_type
!= error_mark_node
947 && actual_type
!= error_mark_node
)
949 formal_type
= TYPE_MAIN_VARIANT (formal_type
);
950 actual_type
= TYPE_MAIN_VARIANT (actual_type
);
952 if (TYPE_HAS_CONSTRUCTOR (formal_type
))
954 /* If it has a constructor for this type,
956 /* @@ There is no way to save this result yet, so
957 success is a NULL_TREE for now. */
958 if (convert_to_aggr (formal_type
, TREE_VALUE (tta
), 0, 1)
962 if (TYPE_LANG_SPECIFIC (actual_type
)
963 && TYPE_HAS_CONVERSION (actual_type
))
965 int extra
= user_harshness (formal_type
, actual_type
);
967 if (extra
== EVIL_CODE
)
972 extra_conversions
= extra
;
976 dont_convert_types
= 0;
981 cp
->harshness
[strike_index
].code
982 = USER_CODE
| (extra_conversions
? STD_CODE
: 0);
987 if (cp
->u
.bad_arg
> strike_index
)
988 cp
->u
.bad_arg
= strike_index
;
990 evil_strikes
= win
? 2 : 1;
995 ttf
= TREE_CHAIN (ttf
);
996 tta
= TREE_CHAIN (tta
);
1001 /* Const member functions get a small penalty because defaulting
1002 to const is less useful than defaulting to non-const. */
1003 /* This is bogus, it does not correspond to anything in the ARM.
1004 This code will be fixed when this entire section is rewritten
1005 to conform to the ARM. (mrs) */
1006 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1008 tree this_parm
= TREE_VALUE (ttf_in
);
1010 if (TREE_CODE (this_parm
) == RECORD_TYPE
/* Is `this' a sig ptr? */
1011 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm
))))
1012 : TYPE_READONLY (TREE_TYPE (this_parm
)))
1014 cp
->harshness
[0].code
|= TRIVIAL_CODE
;
1019 /* Calling a non-const member function from a const member function
1020 is probably invalid, but for now we let it only draw a warning.
1021 We indicate that such a mismatch has occurred by setting the
1022 harshness to a maximum value. */
1023 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in
))) == POINTER_TYPE
1024 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in
))))))
1025 cp
->harshness
[0].code
|= CONST_CODE
;
1030 cp
->h
.code
= EVIL_CODE
;
1031 if (ellipsis_strikes
)
1032 cp
->h
.code
|= ELLIPSIS_CODE
;
1034 cp
->h
.code
|= USER_CODE
;
1036 #ifdef DEBUG_MATCHING
1037 cp_error ("final eval %s", print_harshness (&cp
->h
));
1041 /* Subroutine of ideal_candidate. See if X or Y is a better match
1044 strictly_better (x
, y
)
1045 unsigned short x
, y
;
1053 if (xor >= x
|| xor >= y
)
1058 /* When one of several possible overloaded functions and/or methods
1059 can be called, choose the best candidate for overloading.
1061 BASETYPE is the context from which we start method resolution
1062 or NULL if we are comparing overloaded functions.
1063 CANDIDATES is the array of candidates we have to choose from.
1064 N_CANDIDATES is the length of CANDIDATES.
1065 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1066 choose. It is modified in place when resolving methods. It is not
1067 modified in place when resolving overloaded functions.
1068 LEN is the length of the parameter list. */
1070 static struct candidate
*
1071 ideal_candidate (candidates
, n_candidates
, len
)
1072 struct candidate
*candidates
;
1076 struct candidate
*cp
= candidates
+n_candidates
;
1077 int i
, j
= -1, best_code
;
1079 /* For each argument, sort the functions from best to worst for the arg.
1080 For each function that's not best for this arg, set its overall
1081 harshness to EVIL so that other args won't like it. The candidate
1082 list for the last argument is the intersection of all the best-liked
1085 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
1087 best_code
= cp
[-1].h
.code
;
1089 /* If they're at least as good as each other, do an arg-by-arg check. */
1090 if (! strictly_better (cp
[-1].h
.code
, cp
[-2].h
.code
))
1095 for (j
= 0; j
< n_candidates
; j
++)
1096 if (! strictly_better (candidates
[j
].h
.code
, best_code
))
1099 qsort (candidates
+j
, n_candidates
-j
, sizeof (struct candidate
),
1101 for (i
= 0; i
< len
; i
++)
1103 if (cp
[-1].harshness
[i
].code
< cp
[-2].harshness
[i
].code
)
1105 else if (cp
[-1].harshness
[i
].code
> cp
[-2].harshness
[i
].code
)
1107 else if (cp
[-1].harshness
[i
].code
& STD_CODE
)
1109 /* If it involves a standard conversion, let the
1110 inheritance lattice be the final arbiter. */
1111 if (cp
[-1].harshness
[i
].distance
> cp
[-2].harshness
[i
].distance
)
1113 else if (cp
[-1].harshness
[i
].distance
< cp
[-2].harshness
[i
].distance
)
1116 else if (cp
[-1].harshness
[i
].code
& PROMO_CODE
)
1118 /* For integral promotions, take into account a finer
1119 granularity for determining which types should be favored
1120 over others in such promotions. */
1121 if (cp
[-1].harshness
[i
].int_penalty
> cp
[-2].harshness
[i
].int_penalty
)
1123 else if (cp
[-1].harshness
[i
].int_penalty
< cp
[-2].harshness
[i
].int_penalty
)
1128 if (! better
|| worse
)
1134 /* Assume that if the class referred to is not in the
1135 current class hierarchy, that it may be remote.
1136 PARENT is assumed to be of aggregate type here. */
1138 may_be_remote (parent
)
1141 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent
) == 0)
1144 if (current_class_type
== NULL_TREE
)
1147 if (parent
== current_class_type
)
1150 if (UNIQUELY_DERIVED_FROM_P (parent
, current_class_type
))
1156 build_vfield_ref (datum
, type
)
1160 int old_assume_nonnull_objects
= flag_assume_nonnull_objects
;
1162 if (datum
== error_mark_node
)
1163 return error_mark_node
;
1165 /* Vtable references are always made from non-null objects. */
1166 flag_assume_nonnull_objects
= 1;
1167 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
1168 datum
= convert_from_reference (datum
);
1170 if (! TYPE_USES_COMPLEX_INHERITANCE (type
))
1171 rval
= build (COMPONENT_REF
, TREE_TYPE (CLASSTYPE_VFIELD (type
)),
1172 datum
, CLASSTYPE_VFIELD (type
));
1174 rval
= build_component_ref (datum
, DECL_NAME (CLASSTYPE_VFIELD (type
)), NULL_TREE
, 0);
1175 flag_assume_nonnull_objects
= old_assume_nonnull_objects
;
1180 /* Build a call to a member of an object. I.e., one that overloads
1181 operator ()(), or is a pointer-to-function or pointer-to-method. */
1183 build_field_call (basetype_path
, instance_ptr
, name
, parms
)
1184 tree basetype_path
, instance_ptr
, name
, parms
;
1186 tree field
, instance
;
1188 if (instance_ptr
== current_class_ptr
)
1190 /* Check to see if we really have a reference to an instance variable
1191 with `operator()()' overloaded. */
1192 field
= IDENTIFIER_CLASS_VALUE (name
);
1194 if (field
== NULL_TREE
)
1196 cp_error ("`this' has no member named `%D'", name
);
1197 return error_mark_node
;
1200 if (TREE_CODE (field
) == FIELD_DECL
)
1202 /* If it's a field, try overloading operator (),
1203 or calling if the field is a pointer-to-function. */
1204 instance
= build_component_ref_1 (current_class_ref
, field
, 0);
1205 if (instance
== error_mark_node
)
1206 return error_mark_node
;
1208 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1209 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance
)))
1210 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, instance
, parms
, NULL_TREE
);
1212 if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1214 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == FUNCTION_TYPE
)
1215 return build_function_call (instance
, parms
);
1216 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == METHOD_TYPE
)
1217 return build_function_call (instance
, tree_cons (NULL_TREE
, current_class_ptr
, parms
));
1223 /* Check to see if this is not really a reference to an instance variable
1224 with `operator()()' overloaded. */
1225 field
= lookup_field (basetype_path
, name
, 1, 0);
1227 /* This can happen if the reference was ambiguous or for access
1229 if (field
== error_mark_node
)
1230 return error_mark_node
;
1235 tree ftype
= TREE_TYPE (field
);
1237 if (TREE_CODE (ftype
) == REFERENCE_TYPE
)
1238 ftype
= TREE_TYPE (ftype
);
1240 if (TYPE_LANG_SPECIFIC (ftype
) && TYPE_OVERLOADS_CALL_EXPR (ftype
))
1242 /* Make the next search for this field very short. */
1243 basetype
= DECL_FIELD_CONTEXT (field
);
1244 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
1246 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1247 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
1248 build_component_ref_1 (instance
, field
, 0),
1251 if (TREE_CODE (ftype
) == POINTER_TYPE
)
1253 if (TREE_CODE (TREE_TYPE (ftype
)) == FUNCTION_TYPE
1254 || TREE_CODE (TREE_TYPE (ftype
)) == METHOD_TYPE
)
1256 /* This is a member which is a pointer to function. */
1258 = build_component_ref_1 (build_indirect_ref (instance_ptr
,
1260 field
, LOOKUP_COMPLAIN
);
1261 if (ref
== error_mark_node
)
1262 return error_mark_node
;
1263 return build_function_call (ref
, parms
);
1266 else if (TREE_CODE (ftype
) == METHOD_TYPE
)
1268 error ("invalid call via pointer-to-member function");
1269 return error_mark_node
;
1278 find_scoped_type (type
, inner_name
, inner_types
)
1279 tree type
, inner_name
, inner_types
;
1281 tree tags
= CLASSTYPE_TAGS (type
);
1285 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1286 enclosing class) is set to the name for the enum type. So, if
1287 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1288 then this test will be true. */
1289 if (TREE_PURPOSE (tags
) == inner_name
)
1291 if (inner_types
== NULL_TREE
)
1292 return TYPE_NAME (TREE_VALUE (tags
));
1293 return resolve_scope_to_name (TREE_VALUE (tags
), inner_types
);
1295 tags
= TREE_CHAIN (tags
);
1298 /* Look for a TYPE_DECL. */
1299 for (tags
= TYPE_FIELDS (type
); tags
; tags
= TREE_CHAIN (tags
))
1300 if (TREE_CODE (tags
) == TYPE_DECL
&& DECL_NAME (tags
) == inner_name
)
1302 /* Code by raeburn. */
1303 if (inner_types
== NULL_TREE
)
1305 return resolve_scope_to_name (TREE_TYPE (tags
), inner_types
);
1311 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1312 the name that names the above nested type. INNER_TYPES
1313 is a chain of nested type names (held together by SCOPE_REFs);
1314 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1315 Returns NULL_TREE if there is an error. */
1317 resolve_scope_to_name (outer_type
, inner_stuff
)
1318 tree outer_type
, inner_stuff
;
1321 tree inner_name
, inner_type
;
1323 if (outer_type
== NULL_TREE
&& current_class_type
!= NULL_TREE
)
1325 /* We first try to look for a nesting in our current class context,
1326 then try any enclosing classes. */
1327 tree type
= current_class_type
;
1329 while (type
&& (TREE_CODE (type
) == RECORD_TYPE
1330 || TREE_CODE (type
) == UNION_TYPE
))
1332 tree rval
= resolve_scope_to_name (type
, inner_stuff
);
1334 if (rval
!= NULL_TREE
)
1336 type
= DECL_CONTEXT (TYPE_NAME (type
));
1340 if (TREE_CODE (inner_stuff
) == SCOPE_REF
)
1342 inner_name
= TREE_OPERAND (inner_stuff
, 0);
1343 inner_type
= TREE_OPERAND (inner_stuff
, 1);
1347 inner_name
= inner_stuff
;
1348 inner_type
= NULL_TREE
;
1351 if (outer_type
== NULL_TREE
)
1354 /* If we have something that's already a type by itself,
1356 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name
))
1359 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name
),
1364 x
= lookup_name (inner_name
, 0);
1366 if (x
&& TREE_CODE (x
) == NAMESPACE_DECL
)
1368 x
= lookup_namespace_name (x
, inner_type
);
1374 if (! IS_AGGR_TYPE (outer_type
))
1377 /* Look for member classes or enums. */
1378 tmp
= find_scoped_type (outer_type
, inner_name
, inner_type
);
1380 /* If it's not a type in this class, then go down into the
1381 base classes and search there. */
1382 if (! tmp
&& TYPE_BINFO (outer_type
))
1384 tree binfos
= TYPE_BINFO_BASETYPES (outer_type
);
1385 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1387 for (i
= 0; i
< n_baselinks
; i
++)
1389 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1390 tmp
= resolve_scope_to_name (BINFO_TYPE (base_binfo
), inner_stuff
);
1400 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1401 This is how virtual function calls are avoided. */
1403 build_scoped_method_call (exp
, basetype
, name
, parms
)
1404 tree exp
, basetype
, name
, parms
;
1406 /* Because this syntactic form does not allow
1407 a pointer to a base class to be `stolen',
1408 we need not protect the derived->base conversion
1411 @@ But we do have to check access privileges later. */
1413 tree type
= TREE_TYPE (exp
);
1415 if (type
== error_mark_node
1416 || basetype
== error_mark_node
)
1417 return error_mark_node
;
1419 if (current_template_parms
)
1421 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1423 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 1);
1424 name
= build_min_nt (BIT_NOT_EXPR
, type
);
1426 name
= build_min_nt (SCOPE_REF
, basetype
, name
);
1427 return build_min_nt (METHOD_CALL_EXPR
, name
, exp
, parms
, NULL_TREE
);
1430 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1431 type
= TREE_TYPE (type
);
1433 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1434 that explicit ~int is caught in the parser; this deals with typedefs
1435 and template parms. */
1436 if (TREE_CODE (name
) == BIT_NOT_EXPR
&& ! IS_AGGR_TYPE (basetype
))
1438 if (type
!= basetype
)
1439 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1440 exp
, basetype
, type
);
1441 name
= TREE_OPERAND (name
, 0);
1442 if (basetype
!= name
&& basetype
!= get_type_value (name
))
1443 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1445 return convert (void_type_node
, exp
);
1448 if (! is_aggr_type (basetype
, 1))
1449 return error_mark_node
;
1451 if (! IS_AGGR_TYPE (type
))
1453 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1455 return error_mark_node
;
1458 if ((binfo
= binfo_or_else (basetype
, type
)))
1460 if (binfo
== error_mark_node
)
1461 return error_mark_node
;
1462 if (TREE_CODE (exp
) == INDIRECT_REF
)
1463 decl
= build_indirect_ref (convert_pointer_to (binfo
,
1464 build_unary_op (ADDR_EXPR
, exp
, 0)), NULL_PTR
);
1466 decl
= build_scoped_ref (exp
, basetype
);
1468 /* Call to a destructor. */
1469 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1471 /* Explicit call to destructor. */
1472 name
= TREE_OPERAND (name
, 0);
1473 if (! (name
== TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
1474 || name
== constructor_name (TREE_TYPE (decl
))
1475 || TREE_TYPE (decl
) == get_type_value (name
)))
1478 ("qualified type `%T' does not match destructor name `~%T'",
1479 TREE_TYPE (decl
), name
);
1480 return error_mark_node
;
1482 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
1483 return convert (void_type_node
, exp
);
1485 return build_delete (TREE_TYPE (decl
), decl
, integer_two_node
,
1486 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
1490 /* Call to a method. */
1491 return build_method_call (decl
, name
, parms
, binfo
,
1492 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
1494 return error_mark_node
;
1498 print_candidates (candidates
)
1501 cp_error_at ("candidates are: %D", TREE_VALUE (candidates
));
1502 candidates
= TREE_CHAIN (candidates
);
1506 cp_error_at (" %D", TREE_VALUE (candidates
));
1507 candidates
= TREE_CHAIN (candidates
);
1512 print_n_candidates (candidates
, n
)
1513 struct candidate
*candidates
;
1518 cp_error_at ("candidates are: %D", candidates
[0].function
);
1519 for (i
= 1; i
< n
; i
++)
1520 cp_error_at (" %D", candidates
[i
].function
);
1523 /* We want the address of a function or method. We avoid creating a
1524 pointer-to-member function. */
1526 build_addr_func (function
)
1529 tree type
= TREE_TYPE (function
);
1531 /* We have to do these by hand to avoid real pointer to member
1533 if (TREE_CODE (type
) == METHOD_TYPE
)
1537 type
= build_pointer_type (type
);
1539 if (mark_addressable (function
) == 0)
1540 return error_mark_node
;
1542 addr
= build1 (ADDR_EXPR
, type
, function
);
1544 /* Address of a static or external variable or function counts
1546 if (staticp (function
))
1547 TREE_CONSTANT (addr
) = 1;
1552 function
= default_conversion (function
);
1557 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1558 POINTER_TYPE to those. Note, pointer to member function types
1559 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
1561 build_call (function
, result_type
, parms
)
1562 tree function
, result_type
, parms
;
1564 int is_constructor
= 0;
1566 function
= build_addr_func (function
);
1568 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
1570 sorry ("unable to call pointer to member function here");
1571 return error_mark_node
;
1574 if (TREE_CODE (function
) == ADDR_EXPR
1575 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
1576 && DECL_CONSTRUCTOR_P (TREE_OPERAND (function
, 0)))
1579 function
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
1580 TREE_HAS_CONSTRUCTOR (function
) = is_constructor
;
1581 TREE_TYPE (function
) = result_type
;
1582 TREE_SIDE_EFFECTS (function
) = 1;
1588 /* Build something of the form ptr->method (args)
1589 or object.method (args). This can also build
1590 calls to constructors, and find friends.
1592 Member functions always take their class variable
1595 INSTANCE is a class instance.
1597 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1599 PARMS help to figure out what that NAME really refers to.
1601 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1602 down to the real instance type to use for access checking. We need this
1603 information to get protected accesses correct. This parameter is used
1604 by build_member_call.
1606 FLAGS is the logical disjunction of zero or more LOOKUP_
1607 flags. See cp-tree.h for more info.
1609 If this is all OK, calls build_function_call with the resolved
1612 This function must also handle being called to perform
1613 initialization, promotion/coercion of arguments, and
1614 instantiation of default parameters.
1616 Note that NAME may refer to an instance variable name. If
1617 `operator()()' is defined for the type of that field, then we return
1620 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
1621 tree instance
, name
, parms
, basetype_path
;
1624 register tree function
, fntype
, value_type
;
1625 register tree basetype
, save_basetype
;
1626 register tree baselink
, result
, parmtypes
, parm
;
1629 tree access
= access_public_node
;
1630 tree orig_basetype
= basetype_path
? BINFO_TYPE (basetype_path
) : NULL_TREE
;
1632 /* Range of cases for vtable optimization. */
1633 enum vtable_needs
{ not_needed
, maybe_needed
, unneeded
, needed
};
1634 enum vtable_needs need_vtbl
= not_needed
;
1637 tree save_name
= name
;
1639 tree instance_ptr
= NULL_TREE
;
1640 int all_virtual
= flag_all_virtual
;
1641 int static_call_context
= 0;
1642 tree found_fns
= NULL_TREE
;
1644 /* Keep track of `const' and `volatile' objects. */
1645 int constp
, volatilep
;
1647 #ifdef GATHER_STATISTICS
1648 n_build_method_call
++;
1651 if (instance
== error_mark_node
1652 || name
== error_mark_node
1653 || parms
== error_mark_node
1654 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
1655 return error_mark_node
;
1657 if (current_template_parms
)
1659 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1661 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 1);
1662 name
= build_min_nt (BIT_NOT_EXPR
, type
);
1665 return build_min_nt (METHOD_CALL_EXPR
, name
, instance
, parms
, NULL_TREE
);
1668 /* This is the logic that magically deletes the second argument to
1669 operator delete, if it is not needed. */
1670 if (name
== ansi_opname
[(int) DELETE_EXPR
] && list_length (parms
)==2)
1672 tree save_last
= TREE_CHAIN (parms
);
1674 /* get rid of unneeded argument */
1675 TREE_CHAIN (parms
) = NULL_TREE
;
1676 result
= build_method_call (instance
, name
, parms
, basetype_path
,
1677 (LOOKUP_SPECULATIVELY
|flags
)
1679 /* If it finds a match, return it. */
1681 return build_method_call (instance
, name
, parms
, basetype_path
, flags
);
1682 /* If it doesn't work, two argument delete must work */
1683 TREE_CHAIN (parms
) = save_last
;
1685 /* We already know whether it's needed or not for vec delete. */
1686 else if (name
== ansi_opname
[(int) VEC_DELETE_EXPR
]
1687 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1688 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance
)))
1689 TREE_CHAIN (parms
) = NULL_TREE
;
1691 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1693 flags
|= LOOKUP_DESTRUCTOR
;
1694 name
= TREE_OPERAND (name
, 0);
1696 error ("destructors take no parameters");
1697 basetype
= TREE_TYPE (instance
);
1698 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1699 basetype
= TREE_TYPE (basetype
);
1700 if (! (name
== basetype
1701 || (IS_AGGR_TYPE (basetype
)
1702 && name
== constructor_name (basetype
))
1703 || basetype
== get_type_value (name
)))
1705 cp_error ("destructor name `~%D' does not match type `%T' of expression",
1707 return convert (void_type_node
, instance
);
1710 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1711 return convert (void_type_node
, instance
);
1712 instance
= default_conversion (instance
);
1713 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1714 return build_delete (build_pointer_type (basetype
),
1715 instance_ptr
, integer_two_node
,
1716 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
1722 /* Initialize name for error reporting. */
1723 if (IDENTIFIER_OPNAME_P (name
) && ! IDENTIFIER_TYPENAME_P (name
))
1725 char *p
= operator_name_string (name
);
1726 xref_name
= (char *)alloca (strlen (p
) + 10);
1727 sprintf (xref_name
, "operator %s", p
);
1729 else if (TREE_CODE (name
) == SCOPE_REF
)
1730 xref_name
= IDENTIFIER_POINTER (TREE_OPERAND (name
, 1));
1732 xref_name
= IDENTIFIER_POINTER (name
);
1734 GNU_xref_call (current_function_decl
, xref_name
);
1737 if (instance
== NULL_TREE
)
1739 basetype
= NULL_TREE
;
1740 /* Check cases where this is really a call to raise
1742 if (current_class_type
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
1744 basetype
= purpose_member (name
, CLASSTYPE_TAGS (current_class_type
));
1746 basetype
= TREE_VALUE (basetype
);
1748 else if (TREE_CODE (name
) == SCOPE_REF
1749 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
1751 if (! is_aggr_typedef (TREE_OPERAND (name
, 0), 1))
1752 return error_mark_node
;
1753 basetype
= purpose_member (TREE_OPERAND (name
, 1),
1754 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name
, 0))));
1756 basetype
= TREE_VALUE (basetype
);
1759 if (basetype
!= NULL_TREE
)
1761 /* call to a constructor... */
1762 else if (basetype_path
)
1764 basetype
= BINFO_TYPE (basetype_path
);
1765 if (name
== TYPE_IDENTIFIER (basetype
))
1766 name
= ctor_identifier
;
1768 else if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1770 basetype
= IDENTIFIER_TYPE_VALUE (name
);
1771 name
= ctor_identifier
;
1775 tree typedef_name
= lookup_name (name
, 1);
1776 if (typedef_name
&& TREE_CODE (typedef_name
) == TYPE_DECL
)
1778 /* Canonicalize the typedef name. */
1779 basetype
= TREE_TYPE (typedef_name
);
1780 name
= ctor_identifier
;
1784 cp_error ("no constructor named `%T' in scope",
1786 return error_mark_node
;
1790 if (! IS_AGGR_TYPE (basetype
))
1793 if ((flags
& LOOKUP_COMPLAIN
) && TREE_CODE (basetype
) != ERROR_MARK
)
1794 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1795 name
, instance
, basetype
);
1797 return error_mark_node
;
1800 else if (instance
== current_class_ref
|| instance
== current_class_ptr
)
1802 /* When doing initialization, we side-effect the TREE_TYPE of
1803 current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1804 basetype
= TREE_TYPE (current_class_ref
);
1806 /* Anything manifestly `this' in constructors and destructors
1807 has a known type, so virtual function tables are not needed. */
1808 if (TYPE_VIRTUAL_P (basetype
)
1809 && !(flags
& LOOKUP_NONVIRTUAL
))
1810 need_vtbl
= (dtor_label
|| ctor_label
)
1811 ? unneeded
: maybe_needed
;
1813 /* If `this' is a signature pointer and `name' is not a constructor,
1814 we are calling a signature member function. In that case, set the
1815 `basetype' to the signature type and dereference the `optr' field. */
1816 if (IS_SIGNATURE_POINTER (basetype
)
1817 && TYPE_IDENTIFIER (basetype
) != name
)
1819 basetype
= SIGNATURE_TYPE (basetype
);
1820 instance_ptr
= build_optr_ref (instance
);
1821 instance_ptr
= convert (build_pointer_type (basetype
), instance_ptr
);
1822 basetype_path
= TYPE_BINFO (basetype
);
1826 instance
= current_class_ref
;
1827 instance_ptr
= current_class_ptr
;
1828 basetype_path
= TYPE_BINFO (current_class_type
);
1830 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1835 else if (TREE_CODE (instance
) == RESULT_DECL
)
1837 basetype
= TREE_TYPE (instance
);
1838 /* Should we ever have to make a virtual function reference
1839 from a RESULT_DECL, know that it must be of fixed type
1840 within the scope of this function. */
1841 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1842 need_vtbl
= maybe_needed
;
1843 instance_ptr
= build1 (ADDR_EXPR
, build_pointer_type (basetype
), instance
);
1847 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1848 tree inst_ptr_basetype
;
1850 static_call_context
=
1851 (TREE_CODE (instance
) == INDIRECT_REF
1852 && TREE_CODE (TREE_OPERAND (instance
, 0)) == NOP_EXPR
1853 && TREE_OPERAND (TREE_OPERAND (instance
, 0), 0) == error_mark_node
);
1855 if (TREE_CODE (instance
) == OFFSET_REF
)
1856 instance
= resolve_offset_ref (instance
);
1858 /* the base type of an instance variable is pointer to class */
1859 basetype
= TREE_TYPE (instance
);
1861 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1863 basetype
= TREE_TYPE (basetype
);
1864 if (! IS_AGGR_TYPE (basetype
))
1865 goto non_aggr_error
;
1866 /* Call to convert not needed because we are remaining
1867 within the same type. */
1868 instance_ptr
= build1 (NOP_EXPR
, build_pointer_type (basetype
),
1870 inst_ptr_basetype
= TYPE_MAIN_VARIANT (basetype
);
1874 if (! IS_AGGR_TYPE (basetype
)
1875 && ! (TYPE_LANG_SPECIFIC (basetype
)
1876 && (IS_SIGNATURE_POINTER (basetype
)
1877 || IS_SIGNATURE_REFERENCE (basetype
))))
1878 goto non_aggr_error
;
1880 /* If `instance' is a signature pointer/reference and `name' is
1881 not a constructor, we are calling a signature member function.
1882 In that case set the `basetype' to the signature type. */
1883 if ((IS_SIGNATURE_POINTER (basetype
)
1884 || IS_SIGNATURE_REFERENCE (basetype
))
1885 && TYPE_IDENTIFIER (basetype
) != name
)
1886 basetype
= SIGNATURE_TYPE (basetype
);
1888 if ((IS_SIGNATURE (basetype
)
1889 && (instance_ptr
= instance
))
1890 || (lvalue_p (instance
)
1891 && (instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0)))
1892 || (instance_ptr
= unary_complex_lvalue (ADDR_EXPR
, instance
)))
1894 if (instance_ptr
== error_mark_node
)
1895 return error_mark_node
;
1897 else if (TREE_CODE (instance
) == NOP_EXPR
1898 || TREE_CODE (instance
) == CONSTRUCTOR
)
1900 /* A cast is not an lvalue. Initialize a fresh temp
1901 with the value we are casting from, and proceed with
1902 that temporary. We can't cast to a reference type,
1903 so that simplifies the initialization to something
1905 tree temp
= get_temp_name (TREE_TYPE (instance
), 0);
1906 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
1907 expand_aggr_init (temp
, instance
, 0, flags
);
1910 store_init_value (temp
, instance
);
1911 expand_decl_init (temp
);
1914 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1918 if (TREE_CODE (instance
) != CALL_EXPR
)
1919 my_friendly_abort (125);
1920 if (TYPE_NEEDS_CONSTRUCTING (basetype
))
1921 instance
= build_cplus_new (basetype
, instance
);
1924 instance
= get_temp_name (basetype
, 0);
1925 TREE_ADDRESSABLE (instance
) = 1;
1927 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1929 /* @@ Should we call comp_target_types here? */
1930 if (IS_SIGNATURE (basetype
))
1931 inst_ptr_basetype
= basetype
;
1933 inst_ptr_basetype
= TREE_TYPE (TREE_TYPE (instance_ptr
));
1934 if (TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1935 basetype
= inst_ptr_basetype
;
1938 instance_ptr
= convert (build_pointer_type (basetype
), instance_ptr
);
1939 if (instance_ptr
== error_mark_node
)
1940 return error_mark_node
;
1944 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1945 not updated, so we use `basetype' instead. */
1946 if (basetype_path
== NULL_TREE
1947 && IS_SIGNATURE (basetype
))
1948 basetype_path
= TYPE_BINFO (basetype
);
1949 else if (basetype_path
== NULL_TREE
||
1950 BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1951 basetype_path
= TYPE_BINFO (inst_ptr_basetype
);
1953 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1957 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1959 if (TREE_SIDE_EFFECTS (instance_ptr
))
1961 /* This action is needed because the instance is needed
1962 for providing the base of the virtual function table.
1963 Without using a SAVE_EXPR, the function we are building
1964 may be called twice, or side effects on the instance
1965 variable (such as a post-increment), may happen twice. */
1966 instance_ptr
= save_expr (instance_ptr
);
1967 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1969 else if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1971 /* This happens when called for operator new (). */
1972 instance
= build_indirect_ref (instance
, NULL_PTR
);
1975 need_vtbl
= maybe_needed
;
1979 if (save_name
== ctor_identifier
)
1980 save_name
= TYPE_IDENTIFIER (basetype
);
1982 if (TYPE_SIZE (complete_type (basetype
)) == 0)
1984 /* This is worth complaining about, I think. */
1985 cp_error ("cannot lookup method in incomplete type `%T'", basetype
);
1986 return error_mark_node
;
1989 save_basetype
= TYPE_MAIN_VARIANT (basetype
);
1992 for (parmtypes
= NULL_TREE
, parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1994 tree t
= TREE_TYPE (TREE_VALUE (parm
));
1995 if (TREE_CODE (t
) == OFFSET_TYPE
)
1997 /* Convert OFFSET_TYPE entities to their normal selves. */
1998 TREE_VALUE (parm
) = resolve_offset_ref (TREE_VALUE (parm
));
1999 t
= TREE_TYPE (TREE_VALUE (parm
));
2001 if (TREE_CODE (t
) == METHOD_TYPE
)
2003 cp_pedwarn ("assuming & on `%E'", TREE_VALUE (parm
));
2004 TREE_VALUE (parm
) = build_unary_op (ADDR_EXPR
, TREE_VALUE (parm
), 0);
2007 /* This breaks reference-to-array parameters. */
2008 if (TREE_CODE (t
) == ARRAY_TYPE
)
2010 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
2011 This eliminates needless calls to `compute_conversion_costs'. */
2012 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
2013 t
= TREE_TYPE (TREE_VALUE (parm
));
2016 if (t
== error_mark_node
)
2017 return error_mark_node
;
2018 last
= build_tree_list (NULL_TREE
, t
);
2019 parmtypes
= chainon (parmtypes
, last
);
2022 if (instance
&& IS_SIGNATURE (basetype
))
2024 /* @@ Should this be the constp/volatilep flags for the optr field
2025 of the signature pointer? */
2026 constp
= TYPE_READONLY (basetype
);
2027 volatilep
= TYPE_VOLATILE (basetype
);
2028 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2032 /* TREE_READONLY (instance) fails for references. */
2033 constp
= TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2034 volatilep
= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2035 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2039 /* Raw constructors are always in charge. */
2040 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
2041 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
2043 flags
|= LOOKUP_HAS_IN_CHARGE
;
2044 parms
= tree_cons (NULL_TREE
, integer_one_node
, parms
);
2045 parmtypes
= tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
2050 instance_ptr
= build_int_2 (0, 0);
2051 TREE_TYPE (instance_ptr
) = build_pointer_type (basetype
);
2052 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2055 parmtypes
= tree_cons (NULL_TREE
, TREE_TYPE (instance_ptr
), parmtypes
);
2057 if (last
== NULL_TREE
)
2060 /* Look up function name in the structure type definition. */
2062 /* FIXME Axe most of this now? */
2063 if ((IDENTIFIER_HAS_TYPE_VALUE (name
)
2064 && ! IDENTIFIER_OPNAME_P (name
)
2065 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name
)))
2066 || name
== constructor_name (basetype
)
2067 || name
== ctor_identifier
)
2069 tree tmp
= NULL_TREE
;
2070 if (IDENTIFIER_TYPE_VALUE (name
) == basetype
2071 || name
== constructor_name (basetype
)
2072 || name
== ctor_identifier
)
2073 tmp
= TYPE_BINFO (basetype
);
2075 tmp
= get_binfo (IDENTIFIER_TYPE_VALUE (name
), basetype
, 0);
2077 if (tmp
!= NULL_TREE
)
2079 name_kind
= "constructor";
2081 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
2082 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
2084 /* Constructors called for initialization
2085 only are never in charge. */
2088 flags
|= LOOKUP_HAS_IN_CHARGE
;
2089 tmplist
= tree_cons (NULL_TREE
, integer_zero_node
,
2090 TREE_CHAIN (parms
));
2091 TREE_CHAIN (parms
) = tmplist
;
2092 tmplist
= tree_cons (NULL_TREE
, integer_type_node
, TREE_CHAIN (parmtypes
));
2093 TREE_CHAIN (parmtypes
) = tmplist
;
2095 basetype
= BINFO_TYPE (tmp
);
2098 name_kind
= "method";
2101 name_kind
= "method";
2103 if (basetype_path
== NULL_TREE
2104 || BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (basetype
))
2105 basetype_path
= TYPE_BINFO (basetype
);
2106 result
= lookup_fnfields (basetype_path
, name
,
2107 (flags
& LOOKUP_COMPLAIN
));
2108 if (result
== error_mark_node
)
2109 return error_mark_node
;
2111 for (pass
= 0; pass
< 2; pass
++)
2113 struct candidate
*candidates
;
2114 struct candidate
*cp
;
2123 = (struct candidate
*) alloca ((ever_seen
+1)
2124 * sizeof (struct candidate
));
2125 bzero ((char *) candidates
, (ever_seen
+ 1) * sizeof (struct candidate
));
2127 len
= list_length (parms
);
2130 /* First see if a global function has a shot at it. */
2131 if (flags
& LOOKUP_GLOBAL
)
2134 tree parm
= instance_ptr
;
2136 if (TREE_CODE (TREE_TYPE (parm
)) == REFERENCE_TYPE
)
2137 parm
= convert_from_reference (parm
);
2138 else if (TREE_CODE (TREE_TYPE (parm
)) == POINTER_TYPE
)
2139 parm
= build_indirect_ref (parm
, "friendifying parms (compiler error)");
2141 my_friendly_abort (167);
2143 friend_parms
= tree_cons (NULL_TREE
, parm
, TREE_CHAIN (parms
));
2146 cp
->harshness
= (struct harshness_code
*)
2147 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2149 result
= build_overload_call (name
, friend_parms
, 0, cp
);
2151 /* If it turns out to be the one we were actually looking for
2152 (it was probably a friend function), the return the
2154 if (TREE_CODE (result
) == CALL_EXPR
)
2157 while ((cp
->h
.code
& EVIL_CODE
) == 0)
2159 /* non-standard uses: set the field to 0 to indicate
2160 we are using a non-member function. */
2162 if (cp
->harshness
[len
].distance
== 0
2163 && cp
->h
.code
< best
)
2172 /* We have a hit (of sorts). If the parameter list is
2173 "error_mark_node", or some variant thereof, it won't
2174 match any methods. Since we have verified that the is
2175 some method vaguely matching this one (in name at least),
2178 Don't stop for friends, however. */
2179 basetype_path
= TREE_PURPOSE (baselink
);
2181 function
= TREE_VALUE (baselink
);
2182 if (TREE_CODE (basetype_path
) == TREE_LIST
)
2183 basetype_path
= TREE_VALUE (basetype_path
);
2184 basetype
= BINFO_TYPE (basetype_path
);
2186 for (; function
; function
= DECL_CHAIN (function
))
2188 #ifdef GATHER_STATISTICS
2189 n_inner_fields_searched
++;
2193 found_fns
= tree_cons (NULL_TREE
, function
, found_fns
);
2195 /* Not looking for friends here. */
2196 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
2197 && ! DECL_STATIC_FUNCTION_P (function
))
2202 tree these_parms
= parms
;
2204 #ifdef GATHER_STATISTICS
2205 n_inner_fields_searched
++;
2208 cp
->harshness
= (struct harshness_code
*)
2209 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2211 if (DECL_STATIC_FUNCTION_P (function
))
2212 these_parms
= TREE_CHAIN (these_parms
);
2213 compute_conversion_costs (function
, these_parms
, cp
, len
);
2215 if ((cp
->h
.code
& EVIL_CODE
) == 0)
2217 cp
->u
.field
= function
;
2218 cp
->function
= function
;
2219 cp
->basetypes
= basetype_path
;
2221 /* Don't allow non-converting constructors to convert. */
2222 if (flags
& LOOKUP_ONLYCONVERTING
2223 && DECL_LANG_SPECIFIC (function
)
2224 && DECL_NONCONVERTING_P (function
))
2227 /* No "two-level" conversions. */
2228 if (flags
& LOOKUP_NO_CONVERSION
2229 && (cp
->h
.code
& USER_CODE
))
2240 tree igv
= lookup_name_nonclass (name
);
2242 /* No exact match could be found. Now try to find match
2243 using default conversions. */
2244 if ((flags
& LOOKUP_GLOBAL
) && igv
)
2246 if (TREE_CODE (igv
) == FUNCTION_DECL
)
2248 else if (TREE_CODE (igv
) == TREE_LIST
)
2249 ever_seen
+= count_functions (igv
);
2254 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2255 == LOOKUP_SPECULATIVELY
)
2258 TREE_CHAIN (last
) = void_list_node
;
2259 if (flags
& LOOKUP_GLOBAL
)
2260 cp_error ("no global or member function `%D(%A)' defined",
2261 save_name
, parmtypes
);
2263 cp_error ("no member function `%T::%D(%A)' defined",
2264 save_basetype
, save_name
, TREE_CHAIN (parmtypes
));
2265 return error_mark_node
;
2270 if (cp
- candidates
!= 0)
2272 /* Rank from worst to best. Then cp will point to best one.
2273 Private fields have their bits flipped. For unsigned
2274 numbers, this should make them look very large.
2275 If the best alternate has a (signed) negative value,
2276 then all we ever saw were private members. */
2277 if (cp
- candidates
> 1)
2279 int n_candidates
= cp
- candidates
;
2280 extern int warn_synth
;
2281 TREE_VALUE (parms
) = instance_ptr
;
2282 cp
= ideal_candidate (candidates
, n_candidates
, len
);
2283 if (cp
== (struct candidate
*)0)
2285 if (flags
& LOOKUP_COMPLAIN
)
2287 TREE_CHAIN (last
) = void_list_node
;
2288 cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2289 name_kind
, save_name
, TREE_CHAIN (parmtypes
));
2290 print_n_candidates (candidates
, n_candidates
);
2292 return error_mark_node
;
2294 if (cp
->h
.code
& EVIL_CODE
)
2295 return error_mark_node
;
2297 && DECL_NAME (cp
->function
) == ansi_opname
[MODIFY_EXPR
]
2298 && DECL_ARTIFICIAL (cp
->function
)
2299 && n_candidates
== 2)
2301 cp_warning ("using synthesized `%#D' for copy assignment",
2303 cp_warning_at (" where cfront would use `%#D'",
2304 candidates
->function
);
2307 else if (cp
[-1].h
.code
& EVIL_CODE
)
2309 if (flags
& LOOKUP_COMPLAIN
)
2310 cp_error ("ambiguous type conversion requested for %s `%D'",
2311 name_kind
, save_name
);
2312 return error_mark_node
;
2317 /* The global function was the best, so use it. */
2318 if (cp
->u
.field
== 0)
2320 /* We must convert the instance pointer into a reference type.
2321 Global overloaded functions can only either take
2322 aggregate objects (which come for free from references)
2323 or reference data types anyway. */
2324 TREE_VALUE (parms
) = copy_node (instance_ptr
);
2325 TREE_TYPE (TREE_VALUE (parms
)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2326 return build_function_call (cp
->function
, parms
);
2329 function
= cp
->function
;
2330 basetype_path
= cp
->basetypes
;
2331 if (! DECL_STATIC_FUNCTION_P (function
))
2332 TREE_VALUE (parms
) = cp
->arg
;
2333 goto found_and_maybe_warn
;
2336 if (flags
& (LOOKUP_COMPLAIN
|LOOKUP_SPECULATIVELY
))
2338 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2339 == LOOKUP_SPECULATIVELY
)
2342 if (DECL_STATIC_FUNCTION_P (cp
->function
))
2343 parms
= TREE_CHAIN (parms
);
2346 if (flags
& LOOKUP_SPECULATIVELY
)
2348 if (static_call_context
2349 && TREE_CODE (TREE_TYPE (cp
->function
)) == METHOD_TYPE
)
2350 cp_error ("object missing in call to `%D'", cp
->function
);
2351 else if (ever_seen
> 1)
2353 TREE_CHAIN (last
) = void_list_node
;
2354 cp_error ("no matching function for call to `%T::%D (%A)%V'",
2355 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr
))),
2356 save_name
, TREE_CHAIN (parmtypes
),
2357 TREE_TYPE (TREE_TYPE (instance_ptr
)));
2358 TREE_CHAIN (last
) = NULL_TREE
;
2359 print_candidates (found_fns
);
2362 report_type_mismatch (cp
, parms
, name_kind
);
2363 return error_mark_node
;
2366 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2369 cp_error ("%T has no method named %D", save_basetype
, save_name
);
2370 return error_mark_node
;
2376 found_and_maybe_warn
:
2377 if ((cp
->harshness
[0].code
& CONST_CODE
)
2378 /* 12.1p2: Constructors can be called for const objects. */
2379 && ! DECL_CONSTRUCTOR_P (cp
->function
))
2381 if (flags
& LOOKUP_COMPLAIN
)
2383 cp_error_at ("non-const member function `%D'", cp
->function
);
2384 error ("called for const object at this point in file");
2386 /* Not good enough for a match. */
2388 return error_mark_node
;
2392 /* Silently return error_mark_node. */
2393 return error_mark_node
;
2396 if (flags
& LOOKUP_PROTECT
)
2397 access
= compute_access (basetype_path
, function
);
2399 if (access
== access_private_node
)
2401 if (flags
& LOOKUP_COMPLAIN
)
2403 cp_error_at ("%s `%+#D' is %s", name_kind
, function
,
2404 TREE_PRIVATE (function
) ? "private"
2405 : "from private base class");
2406 error ("within this context");
2408 return error_mark_node
;
2410 else if (access
== access_protected_node
)
2412 if (flags
& LOOKUP_COMPLAIN
)
2414 cp_error_at ("%s `%+#D' %s", name_kind
, function
,
2415 TREE_PROTECTED (function
) ? "is protected"
2416 : "has protected accessibility");
2417 error ("within this context");
2419 return error_mark_node
;
2422 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2423 type (if it exists) is a pointer to. */
2425 if (DECL_ABSTRACT_VIRTUAL_P (function
)
2426 && instance
== current_class_ref
2427 && DECL_CONSTRUCTOR_P (current_function_decl
)
2428 && ! (flags
& LOOKUP_NONVIRTUAL
)
2429 && value_member (function
, get_abstract_virtuals (basetype
)))
2430 cp_error ("abstract virtual `%#D' called from constructor", function
);
2432 if (IS_SIGNATURE (basetype
) && static_call_context
)
2434 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2435 basetype
, save_name
);
2436 return error_mark_node
;
2438 else if (IS_SIGNATURE (basetype
))
2439 return build_signature_method_call (basetype
, instance
, function
, parms
);
2441 function
= DECL_MAIN_VARIANT (function
);
2442 mark_used (function
);
2444 /* Is it a synthesized method that needs to be synthesized? */
2445 if (DECL_ARTIFICIAL (function
) && ! DECL_INITIAL (function
)
2446 /* Kludge: don't synthesize for default args. */
2447 && current_function_decl
)
2448 synthesize_method (function
);
2450 if (pedantic
&& DECL_THIS_INLINE (function
) && ! DECL_ARTIFICIAL (function
)
2451 && ! DECL_INITIAL (function
) && ! DECL_PENDING_INLINE_INFO (function
)
2452 && ! (DECL_TEMPLATE_INFO (function
)
2453 && TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (function
))))
2454 cp_warning ("inline function `%#D' called before definition", function
);
2456 fntype
= TREE_TYPE (function
);
2457 if (TREE_CODE (fntype
) == POINTER_TYPE
)
2458 fntype
= TREE_TYPE (fntype
);
2459 basetype
= DECL_CLASS_CONTEXT (function
);
2461 /* If we are referencing a virtual function from an object
2462 of effectively static type, then there is no need
2463 to go through the virtual function table. */
2464 if (need_vtbl
== maybe_needed
)
2466 int fixed_type
= resolves_to_fixed_type_p (instance
, 0);
2468 if (all_virtual
== 1
2469 && DECL_VINDEX (function
)
2470 && may_be_remote (basetype
))
2472 else if (DECL_VINDEX (function
))
2473 need_vtbl
= fixed_type
? unneeded
: needed
;
2475 need_vtbl
= not_needed
;
2478 if (TREE_CODE (fntype
) == METHOD_TYPE
&& static_call_context
2479 && !DECL_CONSTRUCTOR_P (function
))
2481 /* Let's be nasty to the user now, and give reasonable
2483 instance_ptr
= current_class_ptr
;
2486 if (basetype
!= current_class_type
)
2488 if (basetype
== error_mark_node
)
2489 return error_mark_node
;
2492 if (orig_basetype
!= NULL_TREE
)
2493 error_not_base_type (orig_basetype
, current_class_type
);
2495 error_not_base_type (function
, current_class_type
);
2496 return error_mark_node
;
2500 /* Only allow a static member function to call another static member
2502 else if (DECL_LANG_SPECIFIC (function
)
2503 && !DECL_STATIC_FUNCTION_P (function
))
2505 cp_error ("cannot call member function `%D' without object",
2507 return error_mark_node
;
2511 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2513 if (TYPE_SIZE (complete_type (value_type
)) == 0)
2515 if (flags
& LOOKUP_COMPLAIN
)
2516 incomplete_type_error (0, value_type
);
2517 return error_mark_node
;
2520 if (DECL_STATIC_FUNCTION_P (function
))
2521 parms
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2522 TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
);
2523 else if (need_vtbl
== unneeded
)
2525 int sub_flags
= DECL_CONSTRUCTOR_P (function
) ? flags
: LOOKUP_NORMAL
;
2526 basetype
= TREE_TYPE (instance
);
2527 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function
)) != TYPE_MAIN_VARIANT (basetype
)
2528 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
2530 basetype
= DECL_CLASS_CONTEXT (function
);
2531 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
2532 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2534 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2535 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, sub_flags
));
2539 if ((flags
& LOOKUP_NONVIRTUAL
) == 0)
2540 basetype
= DECL_CONTEXT (function
);
2542 /* First parm could be integer_zerop with casts like
2543 ((Object*)0)->Object::IsA() */
2544 if (!integer_zerop (TREE_VALUE (parms
)))
2546 /* Since we can't have inheritance with a union, doing get_binfo
2547 on it won't work. We do all the convert_pointer_to_real
2548 stuff to handle MI correctly...for unions, that's not
2549 an issue, so we must short-circuit that extra work here. */
2550 tree tmp
= TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)));
2551 if (tmp
!= NULL_TREE
&& TREE_CODE (tmp
) == UNION_TYPE
)
2552 instance_ptr
= TREE_VALUE (parms
);
2555 tree binfo
= get_binfo (basetype
,
2556 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
))),
2558 instance_ptr
= convert_pointer_to_real (binfo
, TREE_VALUE (parms
));
2561 = convert_pointer_to (build_type_variant (basetype
,
2565 if (TREE_CODE (instance_ptr
) == COND_EXPR
)
2567 instance_ptr
= save_expr (instance_ptr
);
2568 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2570 else if (TREE_CODE (instance_ptr
) == NOP_EXPR
2571 && TREE_CODE (TREE_OPERAND (instance_ptr
, 0)) == ADDR_EXPR
2572 && TREE_OPERAND (TREE_OPERAND (instance_ptr
, 0), 0) == instance
)
2574 /* The call to `convert_pointer_to' may return error_mark_node. */
2575 else if (TREE_CODE (instance_ptr
) == ERROR_MARK
)
2576 return instance_ptr
;
2577 else if (instance
== NULL_TREE
2578 || TREE_CODE (instance
) != INDIRECT_REF
2579 || TREE_OPERAND (instance
, 0) != instance_ptr
)
2580 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2582 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2583 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
));
2586 if (parms
== error_mark_node
2587 || (parms
&& TREE_CHAIN (parms
) == error_mark_node
))
2588 return error_mark_node
;
2590 if (need_vtbl
== needed
)
2592 function
= build_vfn_ref (&TREE_VALUE (parms
), instance
,
2593 DECL_VINDEX (function
));
2594 TREE_TYPE (function
) = build_pointer_type (fntype
);
2597 if (TREE_CODE (function
) == FUNCTION_DECL
)
2598 GNU_xref_call (current_function_decl
,
2599 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)));
2601 result
= build_call (function
, value_type
, parms
);
2602 result
= convert_from_reference (result
);
2606 /* Similar to `build_method_call', but for overloaded non-member functions.
2607 The name of this function comes through NAME. The name depends
2610 Note that this function must handle simple `C' promotions,
2611 as well as variable numbers of arguments (...), and
2612 default arguments to boot.
2614 If the overloading is successful, we return a tree node which
2615 contains the call to the function.
2617 If overloading produces candidates which are probable, but not definite,
2618 we hold these candidates. If FINAL_CP is non-zero, then we are free
2619 to assume that final_cp points to enough storage for all candidates that
2620 this function might generate. The `harshness' array is preallocated for
2621 the first candidate, but not for subsequent ones.
2623 Note that the DECL_RTL of FUNCTION must be made to agree with this
2624 function's new name. */
2627 build_overload_call_real (fnname
, parms
, flags
, final_cp
, require_complete
)
2630 struct candidate
*final_cp
;
2631 int require_complete
;
2633 /* must check for overloading here */
2634 tree functions
, function
, parm
;
2635 tree parmtypes
= NULL_TREE
, last
= NULL_TREE
;
2636 register tree outer
;
2638 int parmlength
= list_length (parms
);
2640 struct candidate
*candidates
, *cp
;
2644 final_cp
[0].h
.code
= 0;
2645 final_cp
[0].h
.distance
= 0;
2646 final_cp
[0].function
= 0;
2648 final_cp
[1].h
.code
= EVIL_CODE
;
2651 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2653 register tree t
= TREE_TYPE (TREE_VALUE (parm
));
2655 if (t
== error_mark_node
)
2658 final_cp
->h
.code
= EVIL_CODE
;
2659 return error_mark_node
;
2661 if (TREE_CODE (t
) == OFFSET_TYPE
)
2663 /* This breaks reference-to-array parameters. */
2664 || TREE_CODE (t
) == ARRAY_TYPE
2667 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
2668 Also convert OFFSET_TYPE entities to their normal selves.
2669 This eliminates needless calls to `compute_conversion_costs'. */
2670 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
2671 t
= TREE_TYPE (TREE_VALUE (parm
));
2673 last
= build_tree_list (NULL_TREE
, t
);
2674 parmtypes
= chainon (parmtypes
, last
);
2677 TREE_CHAIN (last
) = void_list_node
;
2679 parmtypes
= void_list_node
;
2681 if (is_overloaded_fn (fnname
))
2684 if (TREE_CODE (fnname
) == TREE_LIST
)
2685 fnname
= TREE_PURPOSE (functions
);
2686 else if (TREE_CODE (fnname
) == FUNCTION_DECL
)
2687 fnname
= DECL_NAME (functions
);
2690 functions
= lookup_name_nonclass (fnname
);
2692 if (functions
== NULL_TREE
)
2694 if (flags
& LOOKUP_SPECULATIVELY
)
2696 if (flags
& LOOKUP_COMPLAIN
)
2697 error ("only member functions apply");
2699 final_cp
->h
.code
= EVIL_CODE
;
2700 return error_mark_node
;
2703 if (TREE_CODE (functions
) == FUNCTION_DECL
&& ! IDENTIFIER_OPNAME_P (fnname
))
2705 functions
= DECL_MAIN_VARIANT (functions
);
2708 /* We are just curious whether this is a viable alternative or
2710 compute_conversion_costs (functions
, parms
, final_cp
, parmlength
);
2714 return build_function_call_real (functions
, parms
, 1, flags
);
2717 if (TREE_CODE (functions
) == TREE_LIST
2718 && TREE_VALUE (functions
) == NULL_TREE
)
2720 if (flags
& LOOKUP_SPECULATIVELY
)
2723 if (flags
& LOOKUP_COMPLAIN
)
2724 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2725 TREE_PURPOSE (functions
));
2727 final_cp
->h
.code
= EVIL_CODE
;
2728 return error_mark_node
;
2731 length
= count_functions (functions
);
2734 candidates
= final_cp
;
2738 = (struct candidate
*)alloca ((length
+1) * sizeof (struct candidate
));
2739 bzero ((char *) candidates
, (length
+ 1) * sizeof (struct candidate
));
2744 my_friendly_assert (is_overloaded_fn (functions
), 169);
2746 functions
= get_first_fn (functions
);
2748 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2749 for (outer
= functions
; outer
; outer
= DECL_CHAIN (outer
))
2751 int template_cost
= 0;
2753 if (TREE_CODE (function
) != FUNCTION_DECL
2754 && ! (TREE_CODE (function
) == TEMPLATE_DECL
2755 && TREE_CODE (DECL_TEMPLATE_RESULT (function
)) == FUNCTION_DECL
))
2757 enum tree_code code
= TREE_CODE (function
);
2758 if (code
== TEMPLATE_DECL
)
2759 code
= TREE_CODE (DECL_TEMPLATE_RESULT (function
));
2760 if (code
== CONST_DECL
)
2762 ("enumeral value `%D' conflicts with function of same name",
2764 else if (code
== VAR_DECL
)
2766 if (TREE_STATIC (function
))
2768 ("variable `%D' conflicts with function of same name",
2772 ("constant field `%D' conflicts with function of same name",
2775 else if (code
== TYPE_DECL
)
2778 my_friendly_abort (2);
2779 error ("at this point in file");
2782 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2784 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function
));
2785 tree
*targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2788 i
= type_unification (DECL_TEMPLATE_PARMS (function
), targs
,
2789 TYPE_ARG_TYPES (TREE_TYPE (function
)),
2790 parms
, &template_cost
, 0);
2793 function
= instantiate_template (function
, targs
);
2794 if (function
== error_mark_node
)
2799 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2801 /* Unconverted template -- failed match. */
2802 cp
->function
= function
;
2804 cp
->h
.code
= EVIL_CODE
;
2808 struct candidate
*cp2
;
2810 /* Check that this decl is not the same as a function that's in
2811 the list due to some template instantiation. */
2814 if (cp2
->function
== function
)
2818 if (cp2
->function
== function
)
2821 function
= DECL_MAIN_VARIANT (function
);
2823 /* Can't use alloca here, since result might be
2824 passed to calling function. */
2825 cp
->h_len
= parmlength
;
2826 cp
->harshness
= (struct harshness_code
*)
2827 oballoc ((parmlength
+ 1) * sizeof (struct harshness_code
));
2829 compute_conversion_costs (function
, parms
, cp
, parmlength
);
2831 /* Make sure this is clear as well. */
2832 cp
->h
.int_penalty
+= template_cost
;
2834 if ((cp
[0].h
.code
& EVIL_CODE
) == 0)
2836 cp
[1].h
.code
= EVIL_CODE
;
2842 if (cp
- candidates
)
2844 tree rval
= error_mark_node
;
2847 cp
[0].h
.code
= EVIL_CODE
;
2848 if (cp
- candidates
> 1)
2850 struct candidate
*best_cp
2851 = ideal_candidate (candidates
, cp
- candidates
, parmlength
);
2852 if (best_cp
== (struct candidate
*)0)
2854 if (flags
& LOOKUP_COMPLAIN
)
2856 cp_error ("call of overloaded `%D' is ambiguous", fnname
);
2857 print_n_candidates (candidates
, cp
- candidates
);
2859 return error_mark_node
;
2862 rval
= best_cp
->function
;
2867 if (cp
->h
.code
& EVIL_CODE
)
2869 if (flags
& LOOKUP_COMPLAIN
)
2870 error ("type conversion ambiguous");
2873 rval
= cp
->function
;
2879 return build_function_call_real (rval
, parms
, require_complete
, flags
);
2882 if (flags
& LOOKUP_SPECULATIVELY
)
2885 if (flags
& LOOKUP_COMPLAIN
)
2886 report_type_mismatch (cp
, parms
, "function",
2887 decl_as_string (cp
->function
, 1));
2889 return error_mark_node
;
2892 /* This requires a complete type on the result of the call. */
2894 build_overload_call (fnname
, parms
, flags
, final_cp
)
2897 struct candidate
*final_cp
;
2899 return build_overload_call_real (fnname
, parms
, flags
, final_cp
, 1);