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 (TREE_UNSIGNED (ttl
) != TREE_UNSIGNED (ttr
))
511 ttl
= unsigned_type (ttl
);
512 ttr
= unsigned_type (ttr
);
515 if (comp_target_types (type
, parmtype
, 1) <= 0)
516 return EVIL_RETURN (h
);
519 if (!(TREE_CODE (ttl
) == VOID_TYPE
520 || TREE_CODE (ttr
) == VOID_TYPE
521 || (TREE_UNSIGNED (ttl
) ^ TREE_UNSIGNED (ttr
)
522 && (ttl
= unsigned_type (ttl
),
523 ttr
= unsigned_type (ttr
),
525 || (comp_target_types (ttl
, ttr
, 0) > 0)))
526 return EVIL_RETURN (h
);
529 if (penalty
== 10 || ttr
== ttl
)
531 tree tmp1
= TREE_TYPE (type
), tmp2
= TREE_TYPE (parmtype
);
533 /* If one was unsigned but the other wasn't, then we need to
534 do a standard conversion from T to unsigned T. */
536 h
.code
= PROMO_CODE
; /* was STD_CODE */
540 /* Note conversion from `T*' to `const T*',
541 or `T*' to `volatile T*'. */
543 && ((TYPE_READONLY (tmp1
) != TREE_READONLY (tmp2
))
544 || (TYPE_VOLATILE (tmp1
) != TYPE_VOLATILE (tmp2
))))
552 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
554 int b_or_d
= get_base_distance (ttl
, ttr
, 0, (tree
*)0);
557 b_or_d
= get_base_distance (ttr
, ttl
, 0, (tree
*)0);
559 return EVIL_RETURN (h
);
560 h
.distance
= -b_or_d
;
568 /* If converting from a `class*' to a `void*', make it
569 less favorable than any inheritance relationship. */
570 if (TREE_CODE (ttl
) == VOID_TYPE
&& IS_AGGR_TYPE (ttr
))
573 h
.distance
= CLASSTYPE_MAX_DEPTH (ttr
)+1;
577 h
.code
= penalty
? STD_CODE
: PROMO_CODE
;
578 /* Catch things like `const char *' -> `const void *'
579 vs `const char *' -> `void *'. */
582 tree tmp1
= TREE_TYPE (type
), tmp2
= TREE_TYPE (parmtype
);
583 if ((TYPE_READONLY (tmp1
) != TREE_READONLY (tmp2
))
584 || (TYPE_VOLATILE (tmp1
) != TYPE_VOLATILE (tmp2
)))
590 if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
592 /* This is not a bad match, but don't let it beat
593 integer-enum combinations. */
594 if (parm
&& integer_zerop (parm
))
602 /* C++: Since the `this' parameter of a signature member function
603 is represented as a signature pointer to handle default implementations
604 correctly, we can have the case that `type' is a signature pointer
605 while `parmtype' is a pointer to a signature table. We don't really
606 do any conversions in this case, so just return 0. */
608 if (codel
== RECORD_TYPE
&& coder
== POINTER_TYPE
609 && IS_SIGNATURE_POINTER (type
) && IS_SIGNATURE (TREE_TYPE (parmtype
)))
610 return ZERO_RETURN (h
);
612 if (codel
== RECORD_TYPE
&& coder
== RECORD_TYPE
)
614 int b_or_d
= get_base_distance (type
, parmtype
, 0, (tree
*)0);
617 b_or_d
= get_base_distance (parmtype
, type
, 0, (tree
*)0);
619 return EVIL_RETURN (h
);
620 h
.distance
= -b_or_d
;
627 return EVIL_RETURN (h
);
630 /* A clone of build_type_conversion for checking user-defined conversions in
631 overload resolution. */
634 user_harshness (type
, parmtype
)
635 register tree type
, parmtype
;
638 tree winner
= NULL_TREE
;
642 tree typename
= build_typename_overload (type
);
643 if (lookup_fnfields (TYPE_BINFO (parmtype
), typename
, 0))
647 for (conv
= lookup_conversions (parmtype
); conv
; conv
= TREE_CHAIN (conv
))
649 struct harshness_code tmp
;
651 if (winner
&& TREE_PURPOSE (winner
) == TREE_PURPOSE (conv
))
654 if (tmp
= convert_harshness (type
, TREE_VALUE (conv
), NULL_TREE
),
655 (tmp
.code
< USER_CODE
) && (tmp
.distance
>= 0))
674 can_convert (to
, from
)
677 struct harshness_code h
;
678 h
= convert_harshness (to
, from
, NULL_TREE
);
679 return (h
.code
< USER_CODE
) && (h
.distance
>= 0);
683 can_convert_arg (to
, from
, arg
)
686 struct harshness_code h
;
687 h
= convert_harshness (to
, from
, arg
);
688 return (h
.code
< USER_CODE
) && (h
.distance
>= 0);
691 #ifdef DEBUG_MATCHING
694 struct harshness_code
*h
;
696 static char buf
[1024];
699 bzero (buf
, 1024 * sizeof (char));
700 strcat (buf
, "codes=[");
701 if (h
->code
& EVIL_CODE
)
702 strcat (buf
, "EVIL");
703 if (h
->code
& CONST_CODE
)
704 strcat (buf
, " CONST");
705 if (h
->code
& ELLIPSIS_CODE
)
706 strcat (buf
, " ELLIPSIS");
707 if (h
->code
& USER_CODE
)
708 strcat (buf
, " USER");
709 if (h
->code
& STD_CODE
)
710 strcat (buf
, " STD");
711 if (h
->code
& PROMO_CODE
)
712 strcat (buf
, " PROMO");
713 if (h
->code
& QUAL_CODE
)
714 strcat (buf
, " QUAL");
715 if (h
->code
& TRIVIAL_CODE
)
716 strcat (buf
, " TRIVIAL");
720 sprintf (tmp
, "] distance=%d int_penalty=%d", h
->distance
, h
->int_penalty
);
728 /* Algorithm: For each argument, calculate how difficult it is to
729 make FUNCTION accept that argument. If we can easily tell that
730 FUNCTION won't be acceptable to one of the arguments, then we
731 don't need to compute the ease of converting the other arguments,
732 since it will never show up in the intersection of all arguments'
735 Conversions between builtin and user-defined types are allowed, but
736 no function involving such a conversion is preferred to one which
737 does not require such a conversion. Furthermore, such conversions
741 compute_conversion_costs (function
, tta_in
, cp
, arglen
)
744 struct candidate
*cp
;
747 tree ttf_in
= TYPE_ARG_TYPES (TREE_TYPE (function
));
751 /* Start out with no strikes against. */
752 int evil_strikes
= 0;
753 int ellipsis_strikes
= 0;
754 int user_strikes
= 0;
755 int b_or_d_strikes
= 0;
756 int easy_strikes
= 0;
758 int strike_index
= 0, win
;
759 struct harshness_code lose
;
760 extern int cp_silent
;
762 #ifdef GATHER_STATISTICS
763 n_compute_conversion_costs
++;
766 #ifndef DEBUG_MATCHING
767 /* We don't emit any warnings or errors while trying out each candidate. */
771 cp
->function
= function
;
772 cp
->arg
= tta
? TREE_VALUE (tta
) : NULL_TREE
;
773 cp
->u
.bad_arg
= 0; /* optimistic! */
777 cp
->h
.int_penalty
= 0;
778 bzero ((char *) cp
->harshness
,
779 (cp
->h_len
+ 1) * sizeof (struct harshness_code
));
783 struct harshness_code h
;
785 if (ttf
== void_list_node
)
788 if (type_unknown_p (TREE_VALUE (tta
)))
790 /* Must perform some instantiation here. */
791 tree rhs
= TREE_VALUE (tta
);
792 tree lhstype
= TREE_VALUE (ttf
);
794 /* Keep quiet about possible contravariance violations. */
795 int old_inhibit_warnings
= inhibit_warnings
;
796 inhibit_warnings
= 1;
798 /* @@ This is to undo what `grokdeclarator' does to
799 parameter types. It really should go through
800 something more general. */
802 TREE_TYPE (tta
) = unknown_type_node
;
803 rhs
= instantiate_type (lhstype
, rhs
, 0);
804 inhibit_warnings
= old_inhibit_warnings
;
806 if (TREE_CODE (rhs
) == ERROR_MARK
)
809 h
= convert_harshness (lhstype
, TREE_TYPE (rhs
), rhs
);
813 #ifdef DEBUG_MATCHING
814 static tree old_function
= NULL_TREE
;
816 if (!old_function
|| function
!= old_function
)
818 cp_error ("trying %D", function
);
819 old_function
= function
;
822 cp_error (" doing (%T) %E against arg %T",
823 TREE_TYPE (TREE_VALUE (tta
)), TREE_VALUE (tta
),
827 h
= convert_harshness (TREE_VALUE (ttf
),
828 TREE_TYPE (TREE_VALUE (tta
)),
831 #ifdef DEBUG_MATCHING
832 cp_error (" evaluated %s", print_harshness (&h
));
836 cp
->harshness
[strike_index
] = h
;
837 if ((h
.code
& EVIL_CODE
)
838 || ((h
.code
& STD_CODE
) && h
.distance
< 0))
840 cp
->u
.bad_arg
= strike_index
;
843 else if (h
.code
& ELLIPSIS_CODE
)
844 ellipsis_strikes
+= 1;
846 /* This is never set by `convert_harshness'. */
847 else if (h
.code
& USER_CODE
)
854 if ((h
.code
& STD_CODE
) && h
.distance
)
856 if (h
.distance
> b_or_d_strikes
)
857 b_or_d_strikes
= h
.distance
;
860 easy_strikes
+= (h
.code
& (STD_CODE
|PROMO_CODE
|TRIVIAL_CODE
));
861 cp
->h
.code
|= h
.code
;
862 /* Make sure we communicate this. */
863 cp
->h
.int_penalty
+= h
.int_penalty
;
866 ttf
= TREE_CHAIN (ttf
);
867 tta
= TREE_CHAIN (tta
);
873 /* ran out of formals, and parmlist is fixed size. */
874 if (ttf
/* == void_type_node */)
876 cp
->h
.code
= EVIL_CODE
;
883 struct harshness_code h
;
884 int l
= list_length (tta
);
885 ellipsis_strikes
+= l
;
886 h
.code
= ELLIPSIS_CODE
;
890 cp
->harshness
[strike_index
++] = h
;
893 else if (ttf
&& ttf
!= void_list_node
)
895 /* ran out of actuals, and no defaults. */
896 if (TREE_PURPOSE (ttf
) == NULL_TREE
)
898 cp
->h
.code
= EVIL_CODE
;
903 /* Store index of first default. */
904 cp
->harshness
[arglen
].distance
= strike_index
+1;
907 cp
->harshness
[arglen
].distance
= 0;
909 /* Argument list lengths work out, so don't need to check them again. */
912 /* We do not check for derived->base conversions here, since in
913 no case would they give evil strike counts, unless such conversions
914 are somehow ambiguous. */
916 /* See if any user-defined conversions apply.
917 But make sure that we do not loop. */
918 static int dont_convert_types
= 0;
920 if (dont_convert_types
)
922 cp
->h
.code
= EVIL_CODE
;
927 win
= 0; /* Only get one chance to win. */
928 ttf
= TYPE_ARG_TYPES (TREE_TYPE (function
));
935 if (ttf
== void_list_node
)
938 lose
= cp
->harshness
[strike_index
];
939 if ((lose
.code
& EVIL_CODE
)
940 || ((lose
.code
& STD_CODE
) && lose
.distance
< 0))
942 tree actual_type
= TREE_TYPE (TREE_VALUE (tta
));
943 tree formal_type
= TREE_VALUE (ttf
);
944 int extra_conversions
= 0;
946 dont_convert_types
= 1;
948 if (TREE_CODE (formal_type
) == REFERENCE_TYPE
)
949 formal_type
= TREE_TYPE (formal_type
);
950 if (TREE_CODE (actual_type
) == REFERENCE_TYPE
)
951 actual_type
= TREE_TYPE (actual_type
);
953 if (formal_type
!= error_mark_node
954 && actual_type
!= error_mark_node
)
956 formal_type
= TYPE_MAIN_VARIANT (formal_type
);
957 actual_type
= TYPE_MAIN_VARIANT (actual_type
);
959 if (TYPE_HAS_CONSTRUCTOR (formal_type
))
961 /* If it has a constructor for this type,
963 /* @@ There is no way to save this result yet, so
964 success is a NULL_TREE for now. */
965 if (convert_to_aggr (formal_type
, TREE_VALUE (tta
), 0, 1)
969 if (TYPE_LANG_SPECIFIC (actual_type
)
970 && TYPE_HAS_CONVERSION (actual_type
))
972 int extra
= user_harshness (formal_type
, actual_type
);
974 if (extra
== EVIL_CODE
)
979 extra_conversions
= extra
;
983 dont_convert_types
= 0;
988 cp
->harshness
[strike_index
].code
989 = USER_CODE
| (extra_conversions
? STD_CODE
: 0);
994 if (cp
->u
.bad_arg
> strike_index
)
995 cp
->u
.bad_arg
= strike_index
;
997 evil_strikes
= win
? 2 : 1;
1002 ttf
= TREE_CHAIN (ttf
);
1003 tta
= TREE_CHAIN (tta
);
1008 /* Const member functions get a small penalty because defaulting
1009 to const is less useful than defaulting to non-const. */
1010 /* This is bogus, it does not correspond to anything in the ARM.
1011 This code will be fixed when this entire section is rewritten
1012 to conform to the ARM. (mrs) */
1013 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1015 tree this_parm
= TREE_VALUE (ttf_in
);
1017 if (TREE_CODE (this_parm
) == RECORD_TYPE
/* Is `this' a sig ptr? */
1018 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm
))))
1019 : TYPE_READONLY (TREE_TYPE (this_parm
)))
1021 cp
->harshness
[0].code
|= TRIVIAL_CODE
;
1026 /* Calling a non-const member function from a const member function
1027 is probably invalid, but for now we let it only draw a warning.
1028 We indicate that such a mismatch has occurred by setting the
1029 harshness to a maximum value. */
1030 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in
))) == POINTER_TYPE
1031 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in
))))))
1032 cp
->harshness
[0].code
|= CONST_CODE
;
1037 cp
->h
.code
= EVIL_CODE
;
1038 if (ellipsis_strikes
)
1039 cp
->h
.code
|= ELLIPSIS_CODE
;
1041 cp
->h
.code
|= USER_CODE
;
1043 #ifdef DEBUG_MATCHING
1044 cp_error ("final eval %s", print_harshness (&cp
->h
));
1048 /* Subroutine of ideal_candidate. See if X or Y is a better match
1051 strictly_better (x
, y
)
1052 unsigned short x
, y
;
1060 if (xor >= x
|| xor >= y
)
1065 /* When one of several possible overloaded functions and/or methods
1066 can be called, choose the best candidate for overloading.
1068 BASETYPE is the context from which we start method resolution
1069 or NULL if we are comparing overloaded functions.
1070 CANDIDATES is the array of candidates we have to choose from.
1071 N_CANDIDATES is the length of CANDIDATES.
1072 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1073 choose. It is modified in place when resolving methods. It is not
1074 modified in place when resolving overloaded functions.
1075 LEN is the length of the parameter list. */
1077 static struct candidate
*
1078 ideal_candidate (candidates
, n_candidates
, len
)
1079 struct candidate
*candidates
;
1083 struct candidate
*cp
= candidates
+n_candidates
;
1084 int i
, j
= -1, best_code
;
1086 /* For each argument, sort the functions from best to worst for the arg.
1087 For each function that's not best for this arg, set its overall
1088 harshness to EVIL so that other args won't like it. The candidate
1089 list for the last argument is the intersection of all the best-liked
1092 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
1094 best_code
= cp
[-1].h
.code
;
1096 /* If they're at least as good as each other, do an arg-by-arg check. */
1097 if (! strictly_better (cp
[-1].h
.code
, cp
[-2].h
.code
))
1102 for (j
= 0; j
< n_candidates
; j
++)
1103 if (! strictly_better (candidates
[j
].h
.code
, best_code
))
1106 qsort (candidates
+j
, n_candidates
-j
, sizeof (struct candidate
),
1108 for (i
= 0; i
< len
; i
++)
1110 if (cp
[-1].harshness
[i
].code
< cp
[-2].harshness
[i
].code
)
1112 else if (cp
[-1].harshness
[i
].code
> cp
[-2].harshness
[i
].code
)
1114 else if (cp
[-1].harshness
[i
].code
& STD_CODE
)
1116 /* If it involves a standard conversion, let the
1117 inheritance lattice be the final arbiter. */
1118 if (cp
[-1].harshness
[i
].distance
> cp
[-2].harshness
[i
].distance
)
1120 else if (cp
[-1].harshness
[i
].distance
< cp
[-2].harshness
[i
].distance
)
1123 else if (cp
[-1].harshness
[i
].code
& PROMO_CODE
)
1125 /* For integral promotions, take into account a finer
1126 granularity for determining which types should be favored
1127 over others in such promotions. */
1128 if (cp
[-1].harshness
[i
].int_penalty
> cp
[-2].harshness
[i
].int_penalty
)
1130 else if (cp
[-1].harshness
[i
].int_penalty
< cp
[-2].harshness
[i
].int_penalty
)
1135 if (! better
|| worse
)
1141 /* Assume that if the class referred to is not in the
1142 current class hierarchy, that it may be remote.
1143 PARENT is assumed to be of aggregate type here. */
1145 may_be_remote (parent
)
1148 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent
) == 0)
1151 if (current_class_type
== NULL_TREE
)
1154 if (parent
== current_class_type
)
1157 if (UNIQUELY_DERIVED_FROM_P (parent
, current_class_type
))
1163 build_vfield_ref (datum
, type
)
1167 int old_assume_nonnull_objects
= flag_assume_nonnull_objects
;
1169 if (datum
== error_mark_node
)
1170 return error_mark_node
;
1172 /* Vtable references are always made from non-null objects. */
1173 flag_assume_nonnull_objects
= 1;
1174 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
1175 datum
= convert_from_reference (datum
);
1177 if (! TYPE_USES_COMPLEX_INHERITANCE (type
))
1178 rval
= build (COMPONENT_REF
, TREE_TYPE (CLASSTYPE_VFIELD (type
)),
1179 datum
, CLASSTYPE_VFIELD (type
));
1181 rval
= build_component_ref (datum
, DECL_NAME (CLASSTYPE_VFIELD (type
)), NULL_TREE
, 0);
1182 flag_assume_nonnull_objects
= old_assume_nonnull_objects
;
1187 /* Build a call to a member of an object. I.e., one that overloads
1188 operator ()(), or is a pointer-to-function or pointer-to-method. */
1190 build_field_call (basetype_path
, instance_ptr
, name
, parms
)
1191 tree basetype_path
, instance_ptr
, name
, parms
;
1193 tree field
, instance
;
1195 if (instance_ptr
== current_class_decl
)
1197 /* Check to see if we really have a reference to an instance variable
1198 with `operator()()' overloaded. */
1199 field
= IDENTIFIER_CLASS_VALUE (name
);
1201 if (field
== NULL_TREE
)
1203 cp_error ("`this' has no member named `%D'", name
);
1204 return error_mark_node
;
1207 if (TREE_CODE (field
) == FIELD_DECL
)
1209 /* If it's a field, try overloading operator (),
1210 or calling if the field is a pointer-to-function. */
1211 instance
= build_component_ref_1 (C_C_D
, field
, 0);
1212 if (instance
== error_mark_node
)
1213 return error_mark_node
;
1215 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1216 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance
)))
1217 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, instance
, parms
, NULL_TREE
);
1219 if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1221 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == FUNCTION_TYPE
)
1222 return build_function_call (instance
, parms
);
1223 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == METHOD_TYPE
)
1224 return build_function_call (instance
, tree_cons (NULL_TREE
, current_class_decl
, parms
));
1230 /* Check to see if this is not really a reference to an instance variable
1231 with `operator()()' overloaded. */
1232 field
= lookup_field (basetype_path
, name
, 1, 0);
1234 /* This can happen if the reference was ambiguous or for access
1236 if (field
== error_mark_node
)
1237 return error_mark_node
;
1242 tree ftype
= TREE_TYPE (field
);
1244 if (TREE_CODE (ftype
) == REFERENCE_TYPE
)
1245 ftype
= TREE_TYPE (ftype
);
1247 if (TYPE_LANG_SPECIFIC (ftype
) && TYPE_OVERLOADS_CALL_EXPR (ftype
))
1249 /* Make the next search for this field very short. */
1250 basetype
= DECL_FIELD_CONTEXT (field
);
1251 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
1253 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1254 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
1255 build_component_ref_1 (instance
, field
, 0),
1258 if (TREE_CODE (ftype
) == POINTER_TYPE
)
1260 if (TREE_CODE (TREE_TYPE (ftype
)) == FUNCTION_TYPE
1261 || TREE_CODE (TREE_TYPE (ftype
)) == METHOD_TYPE
)
1263 /* This is a member which is a pointer to function. */
1265 = build_component_ref_1 (build_indirect_ref (instance_ptr
,
1267 field
, LOOKUP_COMPLAIN
);
1268 if (ref
== error_mark_node
)
1269 return error_mark_node
;
1270 return build_function_call (ref
, parms
);
1273 else if (TREE_CODE (ftype
) == METHOD_TYPE
)
1275 error ("invalid call via pointer-to-member function");
1276 return error_mark_node
;
1285 find_scoped_type (type
, inner_name
, inner_types
)
1286 tree type
, inner_name
, inner_types
;
1288 tree tags
= CLASSTYPE_TAGS (type
);
1292 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1293 enclosing class) is set to the name for the enum type. So, if
1294 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1295 then this test will be true. */
1296 if (TREE_PURPOSE (tags
) == inner_name
)
1298 if (inner_types
== NULL_TREE
)
1299 return TYPE_NAME (TREE_VALUE (tags
));
1300 return resolve_scope_to_name (TREE_VALUE (tags
), inner_types
);
1302 tags
= TREE_CHAIN (tags
);
1305 /* Look for a TYPE_DECL. */
1306 for (tags
= TYPE_FIELDS (type
); tags
; tags
= TREE_CHAIN (tags
))
1307 if (TREE_CODE (tags
) == TYPE_DECL
&& DECL_NAME (tags
) == inner_name
)
1309 /* Code by raeburn. */
1310 if (inner_types
== NULL_TREE
)
1312 return resolve_scope_to_name (TREE_TYPE (tags
), inner_types
);
1318 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1319 the name that names the above nested type. INNER_TYPES
1320 is a chain of nested type names (held together by SCOPE_REFs);
1321 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1322 Returns NULL_TREE if there is an error. */
1324 resolve_scope_to_name (outer_type
, inner_stuff
)
1325 tree outer_type
, inner_stuff
;
1328 tree inner_name
, inner_type
;
1330 if (outer_type
== NULL_TREE
&& current_class_type
!= NULL_TREE
)
1332 /* We first try to look for a nesting in our current class context,
1333 then try any enclosing classes. */
1334 tree type
= current_class_type
;
1336 while (type
&& (TREE_CODE (type
) == RECORD_TYPE
1337 || TREE_CODE (type
) == UNION_TYPE
))
1339 tree rval
= resolve_scope_to_name (type
, inner_stuff
);
1341 if (rval
!= NULL_TREE
)
1343 type
= DECL_CONTEXT (TYPE_NAME (type
));
1347 if (TREE_CODE (inner_stuff
) == SCOPE_REF
)
1349 inner_name
= TREE_OPERAND (inner_stuff
, 0);
1350 inner_type
= TREE_OPERAND (inner_stuff
, 1);
1354 inner_name
= inner_stuff
;
1355 inner_type
= NULL_TREE
;
1358 if (outer_type
== NULL_TREE
)
1361 /* If we have something that's already a type by itself,
1363 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name
))
1366 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name
),
1371 x
= lookup_name (inner_name
, 0);
1373 if (x
&& TREE_CODE (x
) == NAMESPACE_DECL
)
1375 x
= lookup_namespace_name (x
, inner_type
);
1381 if (! IS_AGGR_TYPE (outer_type
))
1384 /* Look for member classes or enums. */
1385 tmp
= find_scoped_type (outer_type
, inner_name
, inner_type
);
1387 /* If it's not a type in this class, then go down into the
1388 base classes and search there. */
1389 if (! tmp
&& TYPE_BINFO (outer_type
))
1391 tree binfos
= TYPE_BINFO_BASETYPES (outer_type
);
1392 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1394 for (i
= 0; i
< n_baselinks
; i
++)
1396 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1397 tmp
= resolve_scope_to_name (BINFO_TYPE (base_binfo
), inner_stuff
);
1407 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1408 This is how virtual function calls are avoided. */
1410 build_scoped_method_call (exp
, basetype
, name
, parms
)
1411 tree exp
, basetype
, name
, parms
;
1413 /* Because this syntactic form does not allow
1414 a pointer to a base class to be `stolen',
1415 we need not protect the derived->base conversion
1418 @@ But we do have to check access privileges later. */
1420 tree type
= TREE_TYPE (exp
);
1422 if (type
== error_mark_node
1423 || basetype
== error_mark_node
)
1424 return error_mark_node
;
1426 if (current_template_parms
)
1428 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1430 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 1);
1431 name
= build_min_nt (BIT_NOT_EXPR
, type
);
1433 name
= build_min_nt (SCOPE_REF
, basetype
, name
);
1434 return build_min_nt (METHOD_CALL_EXPR
, name
, exp
, parms
, NULL_TREE
);
1437 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1438 type
= TREE_TYPE (type
);
1440 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1441 that explicit ~int is caught in the parser; this deals with typedefs
1442 and template parms. */
1443 if (TREE_CODE (name
) == BIT_NOT_EXPR
&& ! IS_AGGR_TYPE (basetype
))
1445 if (type
!= basetype
)
1446 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1447 exp
, basetype
, type
);
1448 name
= TREE_OPERAND (name
, 0);
1449 if (basetype
!= name
&& basetype
!= get_type_value (name
))
1450 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1452 return convert (void_type_node
, exp
);
1455 if (! is_aggr_type (basetype
, 1))
1456 return error_mark_node
;
1458 if (! IS_AGGR_TYPE (type
))
1460 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1462 return error_mark_node
;
1465 if ((binfo
= binfo_or_else (basetype
, type
)))
1467 if (binfo
== error_mark_node
)
1468 return error_mark_node
;
1469 if (TREE_CODE (exp
) == INDIRECT_REF
)
1470 decl
= build_indirect_ref (convert_pointer_to (binfo
,
1471 build_unary_op (ADDR_EXPR
, exp
, 0)), NULL_PTR
);
1473 decl
= build_scoped_ref (exp
, basetype
);
1475 /* Call to a destructor. */
1476 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1478 /* Explicit call to destructor. */
1479 name
= TREE_OPERAND (name
, 0);
1480 if (! (name
== TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
1481 || name
== constructor_name (TREE_TYPE (decl
))
1482 || TREE_TYPE (decl
) == get_type_value (name
)))
1485 ("qualified type `%T' does not match destructor name `~%T'",
1486 TREE_TYPE (decl
), name
);
1487 return error_mark_node
;
1489 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
1490 return convert (void_type_node
, exp
);
1492 return build_delete (TREE_TYPE (decl
), decl
, integer_two_node
,
1493 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
1497 /* Call to a method. */
1498 return build_method_call (decl
, name
, parms
, binfo
,
1499 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
1501 return error_mark_node
;
1505 print_candidates (candidates
)
1508 cp_error_at ("candidates are: %D", TREE_VALUE (candidates
));
1509 candidates
= TREE_CHAIN (candidates
);
1513 cp_error_at (" %D", TREE_VALUE (candidates
));
1514 candidates
= TREE_CHAIN (candidates
);
1519 print_n_candidates (candidates
, n
)
1520 struct candidate
*candidates
;
1525 cp_error_at ("candidates are: %D", candidates
[0].function
);
1526 for (i
= 1; i
< n
; i
++)
1527 cp_error_at (" %D", candidates
[i
].function
);
1530 /* Build something of the form ptr->method (args)
1531 or object.method (args). This can also build
1532 calls to constructors, and find friends.
1534 Member functions always take their class variable
1537 INSTANCE is a class instance.
1539 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1541 PARMS help to figure out what that NAME really refers to.
1543 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1544 down to the real instance type to use for access checking. We need this
1545 information to get protected accesses correct. This parameter is used
1546 by build_member_call.
1548 FLAGS is the logical disjunction of zero or more LOOKUP_
1549 flags. See cp-tree.h for more info.
1551 If this is all OK, calls build_function_call with the resolved
1554 This function must also handle being called to perform
1555 initialization, promotion/coercion of arguments, and
1556 instantiation of default parameters.
1558 Note that NAME may refer to an instance variable name. If
1559 `operator()()' is defined for the type of that field, then we return
1562 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
1563 tree instance
, name
, parms
, basetype_path
;
1566 register tree function
, fntype
, value_type
;
1567 register tree basetype
, save_basetype
;
1568 register tree baselink
, result
, parmtypes
, parm
;
1571 tree access
= access_public_node
;
1572 tree orig_basetype
= basetype_path
? BINFO_TYPE (basetype_path
) : NULL_TREE
;
1574 /* Range of cases for vtable optimization. */
1575 enum vtable_needs
{ not_needed
, maybe_needed
, unneeded
, needed
};
1576 enum vtable_needs need_vtbl
= not_needed
;
1579 tree save_name
= name
;
1581 tree instance_ptr
= NULL_TREE
;
1582 int all_virtual
= flag_all_virtual
;
1583 int static_call_context
= 0;
1584 tree found_fns
= NULL_TREE
;
1586 /* Keep track of `const' and `volatile' objects. */
1587 int constp
, volatilep
;
1589 #ifdef GATHER_STATISTICS
1590 n_build_method_call
++;
1593 if (instance
== error_mark_node
1594 || name
== error_mark_node
1595 || parms
== error_mark_node
1596 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
1597 return error_mark_node
;
1599 if (current_template_parms
)
1601 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1603 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 1);
1604 name
= build_min_nt (BIT_NOT_EXPR
, type
);
1607 return build_min_nt (METHOD_CALL_EXPR
, name
, instance
, parms
, NULL_TREE
);
1610 /* This is the logic that magically deletes the second argument to
1611 operator delete, if it is not needed. */
1612 if (name
== ansi_opname
[(int) DELETE_EXPR
] && list_length (parms
)==2)
1614 tree save_last
= TREE_CHAIN (parms
);
1616 /* get rid of unneeded argument */
1617 TREE_CHAIN (parms
) = NULL_TREE
;
1618 result
= build_method_call (instance
, name
, parms
, basetype_path
,
1619 (LOOKUP_SPECULATIVELY
|flags
)
1621 /* If it finds a match, return it. */
1623 return build_method_call (instance
, name
, parms
, basetype_path
, flags
);
1624 /* If it doesn't work, two argument delete must work */
1625 TREE_CHAIN (parms
) = save_last
;
1627 /* We already know whether it's needed or not for vec delete. */
1628 else if (name
== ansi_opname
[(int) VEC_DELETE_EXPR
]
1629 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1630 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance
)))
1631 TREE_CHAIN (parms
) = NULL_TREE
;
1633 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1635 flags
|= LOOKUP_DESTRUCTOR
;
1636 name
= TREE_OPERAND (name
, 0);
1638 error ("destructors take no parameters");
1639 basetype
= TREE_TYPE (instance
);
1640 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1641 basetype
= TREE_TYPE (basetype
);
1642 if (! (name
== basetype
1643 || (IS_AGGR_TYPE (basetype
)
1644 && name
== constructor_name (basetype
))
1645 || basetype
== get_type_value (name
)))
1647 cp_error ("destructor name `~%D' does not match type `%T' of expression",
1649 return convert (void_type_node
, instance
);
1652 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1653 return convert (void_type_node
, instance
);
1654 instance
= default_conversion (instance
);
1655 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1656 return build_delete (build_pointer_type (basetype
),
1657 instance_ptr
, integer_two_node
,
1658 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
1664 /* Initialize name for error reporting. */
1665 if (IDENTIFIER_OPNAME_P (name
) && ! IDENTIFIER_TYPENAME_P (name
))
1667 char *p
= operator_name_string (name
);
1668 xref_name
= (char *)alloca (strlen (p
) + 10);
1669 sprintf (xref_name
, "operator %s", p
);
1671 else if (TREE_CODE (name
) == SCOPE_REF
)
1672 xref_name
= IDENTIFIER_POINTER (TREE_OPERAND (name
, 1));
1674 xref_name
= IDENTIFIER_POINTER (name
);
1676 GNU_xref_call (current_function_decl
, xref_name
);
1679 if (instance
== NULL_TREE
)
1681 basetype
= NULL_TREE
;
1682 /* Check cases where this is really a call to raise
1684 if (current_class_type
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
1686 basetype
= purpose_member (name
, CLASSTYPE_TAGS (current_class_type
));
1688 basetype
= TREE_VALUE (basetype
);
1690 else if (TREE_CODE (name
) == SCOPE_REF
1691 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
1693 if (! is_aggr_typedef (TREE_OPERAND (name
, 0), 1))
1694 return error_mark_node
;
1695 basetype
= purpose_member (TREE_OPERAND (name
, 1),
1696 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name
, 0))));
1698 basetype
= TREE_VALUE (basetype
);
1701 if (basetype
!= NULL_TREE
)
1703 /* call to a constructor... */
1704 else if (basetype_path
)
1706 basetype
= BINFO_TYPE (basetype_path
);
1707 if (name
== TYPE_IDENTIFIER (basetype
))
1708 name
= ctor_identifier
;
1710 else if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1712 basetype
= IDENTIFIER_TYPE_VALUE (name
);
1713 name
= ctor_identifier
;
1717 tree typedef_name
= lookup_name (name
, 1);
1718 if (typedef_name
&& TREE_CODE (typedef_name
) == TYPE_DECL
)
1720 /* Canonicalize the typedef name. */
1721 basetype
= TREE_TYPE (typedef_name
);
1722 name
= ctor_identifier
;
1726 cp_error ("no constructor named `%T' in scope",
1728 return error_mark_node
;
1732 if (! IS_AGGR_TYPE (basetype
))
1735 if ((flags
& LOOKUP_COMPLAIN
) && TREE_CODE (basetype
) != ERROR_MARK
)
1736 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1737 name
, instance
, basetype
);
1739 return error_mark_node
;
1742 else if (instance
== C_C_D
|| instance
== current_class_decl
)
1744 /* When doing initialization, we side-effect the TREE_TYPE of
1745 C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1746 basetype
= TREE_TYPE (C_C_D
);
1748 /* Anything manifestly `this' in constructors and destructors
1749 has a known type, so virtual function tables are not needed. */
1750 if (TYPE_VIRTUAL_P (basetype
)
1751 && !(flags
& LOOKUP_NONVIRTUAL
))
1752 need_vtbl
= (dtor_label
|| ctor_label
)
1753 ? unneeded
: maybe_needed
;
1755 /* If `this' is a signature pointer and `name' is not a constructor,
1756 we are calling a signature member function. In that case, set the
1757 `basetype' to the signature type and dereference the `optr' field. */
1758 if (IS_SIGNATURE_POINTER (basetype
)
1759 && TYPE_IDENTIFIER (basetype
) != name
)
1761 basetype
= SIGNATURE_TYPE (basetype
);
1762 instance_ptr
= build_optr_ref (instance
);
1763 instance_ptr
= convert (build_pointer_type (basetype
), instance_ptr
);
1764 basetype_path
= TYPE_BINFO (basetype
);
1769 instance_ptr
= current_class_decl
;
1770 basetype_path
= TYPE_BINFO (current_class_type
);
1772 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1777 else if (TREE_CODE (instance
) == RESULT_DECL
)
1779 basetype
= TREE_TYPE (instance
);
1780 /* Should we ever have to make a virtual function reference
1781 from a RESULT_DECL, know that it must be of fixed type
1782 within the scope of this function. */
1783 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1784 need_vtbl
= maybe_needed
;
1785 instance_ptr
= build1 (ADDR_EXPR
, build_pointer_type (basetype
), instance
);
1789 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1790 tree inst_ptr_basetype
;
1792 static_call_context
=
1793 (TREE_CODE (instance
) == INDIRECT_REF
1794 && TREE_CODE (TREE_OPERAND (instance
, 0)) == NOP_EXPR
1795 && TREE_OPERAND (TREE_OPERAND (instance
, 0), 0) == error_mark_node
);
1797 if (TREE_CODE (instance
) == OFFSET_REF
)
1798 instance
= resolve_offset_ref (instance
);
1800 /* the base type of an instance variable is pointer to class */
1801 basetype
= TREE_TYPE (instance
);
1803 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1805 basetype
= TREE_TYPE (basetype
);
1806 if (! IS_AGGR_TYPE (basetype
))
1807 goto non_aggr_error
;
1808 /* Call to convert not needed because we are remaining
1809 within the same type. */
1810 instance_ptr
= build1 (NOP_EXPR
, build_pointer_type (basetype
),
1812 inst_ptr_basetype
= TYPE_MAIN_VARIANT (basetype
);
1816 if (! IS_AGGR_TYPE (basetype
)
1817 && ! (TYPE_LANG_SPECIFIC (basetype
)
1818 && (IS_SIGNATURE_POINTER (basetype
)
1819 || IS_SIGNATURE_REFERENCE (basetype
))))
1820 goto non_aggr_error
;
1822 /* If `instance' is a signature pointer/reference and `name' is
1823 not a constructor, we are calling a signature member function.
1824 In that case set the `basetype' to the signature type. */
1825 if ((IS_SIGNATURE_POINTER (basetype
)
1826 || IS_SIGNATURE_REFERENCE (basetype
))
1827 && TYPE_IDENTIFIER (basetype
) != name
)
1828 basetype
= SIGNATURE_TYPE (basetype
);
1830 if ((IS_SIGNATURE (basetype
)
1831 && (instance_ptr
= instance
))
1832 || (lvalue_p (instance
)
1833 && (instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0)))
1834 || (instance_ptr
= unary_complex_lvalue (ADDR_EXPR
, instance
)))
1836 if (instance_ptr
== error_mark_node
)
1837 return error_mark_node
;
1839 else if (TREE_CODE (instance
) == NOP_EXPR
1840 || TREE_CODE (instance
) == CONSTRUCTOR
)
1842 /* A cast is not an lvalue. Initialize a fresh temp
1843 with the value we are casting from, and proceed with
1844 that temporary. We can't cast to a reference type,
1845 so that simplifies the initialization to something
1847 tree temp
= get_temp_name (TREE_TYPE (instance
), 0);
1848 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
1849 expand_aggr_init (temp
, instance
, 0, flags
);
1852 store_init_value (temp
, instance
);
1853 expand_decl_init (temp
);
1856 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1860 if (TREE_CODE (instance
) != CALL_EXPR
)
1861 my_friendly_abort (125);
1862 if (TYPE_NEEDS_CONSTRUCTING (basetype
))
1863 instance
= build_cplus_new (basetype
, instance
);
1866 instance
= get_temp_name (basetype
, 0);
1867 TREE_ADDRESSABLE (instance
) = 1;
1869 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1871 /* @@ Should we call comp_target_types here? */
1872 if (IS_SIGNATURE (basetype
))
1873 inst_ptr_basetype
= basetype
;
1875 inst_ptr_basetype
= TREE_TYPE (TREE_TYPE (instance_ptr
));
1876 if (TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1877 basetype
= inst_ptr_basetype
;
1880 instance_ptr
= convert (build_pointer_type (basetype
), instance_ptr
);
1881 if (instance_ptr
== error_mark_node
)
1882 return error_mark_node
;
1886 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1887 not updated, so we use `basetype' instead. */
1888 if (basetype_path
== NULL_TREE
1889 && IS_SIGNATURE (basetype
))
1890 basetype_path
= TYPE_BINFO (basetype
);
1891 else if (basetype_path
== NULL_TREE
||
1892 BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1893 basetype_path
= TYPE_BINFO (inst_ptr_basetype
);
1895 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1899 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1901 if (TREE_SIDE_EFFECTS (instance_ptr
))
1903 /* This action is needed because the instance is needed
1904 for providing the base of the virtual function table.
1905 Without using a SAVE_EXPR, the function we are building
1906 may be called twice, or side effects on the instance
1907 variable (such as a post-increment), may happen twice. */
1908 instance_ptr
= save_expr (instance_ptr
);
1909 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1911 else if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1913 /* This happens when called for operator new (). */
1914 instance
= build_indirect_ref (instance
, NULL_PTR
);
1917 need_vtbl
= maybe_needed
;
1921 if (save_name
== ctor_identifier
)
1922 save_name
= TYPE_IDENTIFIER (basetype
);
1924 if (TYPE_SIZE (complete_type (basetype
)) == 0)
1926 /* This is worth complaining about, I think. */
1927 cp_error ("cannot lookup method in incomplete type `%T'", basetype
);
1928 return error_mark_node
;
1931 save_basetype
= TYPE_MAIN_VARIANT (basetype
);
1934 for (parmtypes
= NULL_TREE
, parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1936 tree t
= TREE_TYPE (TREE_VALUE (parm
));
1937 if (TREE_CODE (t
) == OFFSET_TYPE
)
1939 /* Convert OFFSET_TYPE entities to their normal selves. */
1940 TREE_VALUE (parm
) = resolve_offset_ref (TREE_VALUE (parm
));
1941 t
= TREE_TYPE (TREE_VALUE (parm
));
1943 if (TREE_CODE (TREE_VALUE (parm
)) == OFFSET_REF
1944 && TREE_CODE (t
) == METHOD_TYPE
)
1946 TREE_VALUE (parm
) = build_unary_op (ADDR_EXPR
, TREE_VALUE (parm
), 0);
1949 /* This breaks reference-to-array parameters. */
1950 if (TREE_CODE (t
) == ARRAY_TYPE
)
1952 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
1953 This eliminates needless calls to `compute_conversion_costs'. */
1954 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
1955 t
= TREE_TYPE (TREE_VALUE (parm
));
1958 if (t
== error_mark_node
)
1959 return error_mark_node
;
1960 last
= build_tree_list (NULL_TREE
, t
);
1961 parmtypes
= chainon (parmtypes
, last
);
1964 if (instance
&& IS_SIGNATURE (basetype
))
1966 /* @@ Should this be the constp/volatilep flags for the optr field
1967 of the signature pointer? */
1968 constp
= TYPE_READONLY (basetype
);
1969 volatilep
= TYPE_VOLATILE (basetype
);
1970 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
1974 /* TREE_READONLY (instance) fails for references. */
1975 constp
= TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr
)));
1976 volatilep
= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr
)));
1977 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
1981 /* Raw constructors are always in charge. */
1982 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
1983 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
1985 flags
|= LOOKUP_HAS_IN_CHARGE
;
1986 parms
= tree_cons (NULL_TREE
, integer_one_node
, parms
);
1987 parmtypes
= tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
1992 instance_ptr
= build_int_2 (0, 0);
1993 TREE_TYPE (instance_ptr
) = build_pointer_type (basetype
);
1994 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
1997 parmtypes
= tree_cons (NULL_TREE
, TREE_TYPE (instance_ptr
), parmtypes
);
1999 if (last
== NULL_TREE
)
2002 /* Look up function name in the structure type definition. */
2004 /* FIXME Axe most of this now? */
2005 if ((IDENTIFIER_HAS_TYPE_VALUE (name
)
2006 && ! IDENTIFIER_OPNAME_P (name
)
2007 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name
)))
2008 || name
== constructor_name (basetype
)
2009 || name
== ctor_identifier
)
2011 tree tmp
= NULL_TREE
;
2012 if (IDENTIFIER_TYPE_VALUE (name
) == basetype
2013 || name
== constructor_name (basetype
)
2014 || name
== ctor_identifier
)
2015 tmp
= TYPE_BINFO (basetype
);
2017 tmp
= get_binfo (IDENTIFIER_TYPE_VALUE (name
), basetype
, 0);
2019 if (tmp
!= NULL_TREE
)
2021 name_kind
= "constructor";
2023 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
2024 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
2026 /* Constructors called for initialization
2027 only are never in charge. */
2030 flags
|= LOOKUP_HAS_IN_CHARGE
;
2031 tmplist
= tree_cons (NULL_TREE
, integer_zero_node
,
2032 TREE_CHAIN (parms
));
2033 TREE_CHAIN (parms
) = tmplist
;
2034 tmplist
= tree_cons (NULL_TREE
, integer_type_node
, TREE_CHAIN (parmtypes
));
2035 TREE_CHAIN (parmtypes
) = tmplist
;
2037 basetype
= BINFO_TYPE (tmp
);
2040 name_kind
= "method";
2043 name_kind
= "method";
2045 if (basetype_path
== NULL_TREE
2046 || BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (basetype
))
2047 basetype_path
= TYPE_BINFO (basetype
);
2048 result
= lookup_fnfields (basetype_path
, name
,
2049 (flags
& LOOKUP_COMPLAIN
));
2050 if (result
== error_mark_node
)
2051 return error_mark_node
;
2053 for (pass
= 0; pass
< 2; pass
++)
2055 struct candidate
*candidates
;
2056 struct candidate
*cp
;
2065 = (struct candidate
*) alloca ((ever_seen
+1)
2066 * sizeof (struct candidate
));
2067 bzero ((char *) candidates
, (ever_seen
+ 1) * sizeof (struct candidate
));
2069 len
= list_length (parms
);
2072 /* First see if a global function has a shot at it. */
2073 if (flags
& LOOKUP_GLOBAL
)
2076 tree parm
= instance_ptr
;
2078 if (TREE_CODE (TREE_TYPE (parm
)) == REFERENCE_TYPE
)
2079 parm
= convert_from_reference (parm
);
2080 else if (TREE_CODE (TREE_TYPE (parm
)) == POINTER_TYPE
)
2081 parm
= build_indirect_ref (parm
, "friendifying parms (compiler error)");
2083 my_friendly_abort (167);
2085 friend_parms
= tree_cons (NULL_TREE
, parm
, TREE_CHAIN (parms
));
2088 cp
->harshness
= (struct harshness_code
*)
2089 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2091 result
= build_overload_call (name
, friend_parms
, 0, cp
);
2093 /* If it turns out to be the one we were actually looking for
2094 (it was probably a friend function), the return the
2096 if (TREE_CODE (result
) == CALL_EXPR
)
2099 while ((cp
->h
.code
& EVIL_CODE
) == 0)
2101 /* non-standard uses: set the field to 0 to indicate
2102 we are using a non-member function. */
2104 if (cp
->harshness
[len
].distance
== 0
2105 && cp
->h
.code
< best
)
2114 /* We have a hit (of sorts). If the parameter list is
2115 "error_mark_node", or some variant thereof, it won't
2116 match any methods. Since we have verified that the is
2117 some method vaguely matching this one (in name at least),
2120 Don't stop for friends, however. */
2121 basetype_path
= TREE_PURPOSE (baselink
);
2123 function
= TREE_VALUE (baselink
);
2124 if (TREE_CODE (basetype_path
) == TREE_LIST
)
2125 basetype_path
= TREE_VALUE (basetype_path
);
2126 basetype
= BINFO_TYPE (basetype_path
);
2128 for (; function
; function
= DECL_CHAIN (function
))
2130 #ifdef GATHER_STATISTICS
2131 n_inner_fields_searched
++;
2135 found_fns
= tree_cons (NULL_TREE
, function
, found_fns
);
2137 /* Not looking for friends here. */
2138 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
2139 && ! DECL_STATIC_FUNCTION_P (function
))
2144 tree these_parms
= parms
;
2146 #ifdef GATHER_STATISTICS
2147 n_inner_fields_searched
++;
2150 cp
->harshness
= (struct harshness_code
*)
2151 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2153 if (DECL_STATIC_FUNCTION_P (function
))
2154 these_parms
= TREE_CHAIN (these_parms
);
2155 compute_conversion_costs (function
, these_parms
, cp
, len
);
2157 if ((cp
->h
.code
& EVIL_CODE
) == 0)
2159 cp
->u
.field
= function
;
2160 cp
->function
= function
;
2161 cp
->basetypes
= basetype_path
;
2163 /* Don't allow non-converting constructors to convert. */
2164 if (flags
& LOOKUP_ONLYCONVERTING
2165 && DECL_LANG_SPECIFIC (function
)
2166 && DECL_NONCONVERTING_P (function
))
2169 /* No "two-level" conversions. */
2170 if (flags
& LOOKUP_NO_CONVERSION
2171 && (cp
->h
.code
& USER_CODE
))
2182 tree igv
= lookup_name_nonclass (name
);
2184 /* No exact match could be found. Now try to find match
2185 using default conversions. */
2186 if ((flags
& LOOKUP_GLOBAL
) && igv
)
2188 if (TREE_CODE (igv
) == FUNCTION_DECL
)
2190 else if (TREE_CODE (igv
) == TREE_LIST
)
2191 ever_seen
+= count_functions (igv
);
2196 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2197 == LOOKUP_SPECULATIVELY
)
2200 TREE_CHAIN (last
) = void_list_node
;
2201 if (flags
& LOOKUP_GLOBAL
)
2202 cp_error ("no global or member function `%D(%A)' defined",
2203 save_name
, parmtypes
);
2205 cp_error ("no member function `%T::%D(%A)' defined",
2206 save_basetype
, save_name
, TREE_CHAIN (parmtypes
));
2207 return error_mark_node
;
2212 if (cp
- candidates
!= 0)
2214 /* Rank from worst to best. Then cp will point to best one.
2215 Private fields have their bits flipped. For unsigned
2216 numbers, this should make them look very large.
2217 If the best alternate has a (signed) negative value,
2218 then all we ever saw were private members. */
2219 if (cp
- candidates
> 1)
2221 int n_candidates
= cp
- candidates
;
2222 extern int warn_synth
;
2223 TREE_VALUE (parms
) = instance_ptr
;
2224 cp
= ideal_candidate (candidates
, n_candidates
, len
);
2225 if (cp
== (struct candidate
*)0)
2227 if (flags
& LOOKUP_COMPLAIN
)
2229 TREE_CHAIN (last
) = void_list_node
;
2230 cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2231 name_kind
, save_name
, TREE_CHAIN (parmtypes
));
2232 print_n_candidates (candidates
, n_candidates
);
2234 return error_mark_node
;
2236 if (cp
->h
.code
& EVIL_CODE
)
2237 return error_mark_node
;
2239 && DECL_NAME (cp
->function
) == ansi_opname
[MODIFY_EXPR
]
2240 && DECL_ARTIFICIAL (cp
->function
)
2241 && n_candidates
== 2)
2243 cp_warning ("using synthesized `%#D' for copy assignment",
2245 cp_warning_at (" where cfront would use `%#D'",
2246 candidates
->function
);
2249 else if (cp
[-1].h
.code
& EVIL_CODE
)
2251 if (flags
& LOOKUP_COMPLAIN
)
2252 cp_error ("ambiguous type conversion requested for %s `%D'",
2253 name_kind
, save_name
);
2254 return error_mark_node
;
2259 /* The global function was the best, so use it. */
2260 if (cp
->u
.field
== 0)
2262 /* We must convert the instance pointer into a reference type.
2263 Global overloaded functions can only either take
2264 aggregate objects (which come for free from references)
2265 or reference data types anyway. */
2266 TREE_VALUE (parms
) = copy_node (instance_ptr
);
2267 TREE_TYPE (TREE_VALUE (parms
)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2268 return build_function_call (cp
->function
, parms
);
2271 function
= cp
->function
;
2272 basetype_path
= cp
->basetypes
;
2273 if (! DECL_STATIC_FUNCTION_P (function
))
2274 TREE_VALUE (parms
) = cp
->arg
;
2275 goto found_and_maybe_warn
;
2278 if (flags
& (LOOKUP_COMPLAIN
|LOOKUP_SPECULATIVELY
))
2280 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2281 == LOOKUP_SPECULATIVELY
)
2284 if (DECL_STATIC_FUNCTION_P (cp
->function
))
2285 parms
= TREE_CHAIN (parms
);
2288 if (flags
& LOOKUP_SPECULATIVELY
)
2290 if (static_call_context
2291 && TREE_CODE (TREE_TYPE (cp
->function
)) == METHOD_TYPE
)
2292 cp_error ("object missing in call to `%D'", cp
->function
);
2293 else if (ever_seen
> 1)
2295 TREE_CHAIN (last
) = void_list_node
;
2296 cp_error ("no matching function for call to `%T::%D (%A)%V'",
2297 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr
))),
2298 save_name
, TREE_CHAIN (parmtypes
),
2299 TREE_TYPE (TREE_TYPE (instance_ptr
)));
2300 TREE_CHAIN (last
) = NULL_TREE
;
2301 print_candidates (found_fns
);
2304 report_type_mismatch (cp
, parms
, name_kind
);
2305 return error_mark_node
;
2308 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2311 cp_error ("%T has no method named %D", save_basetype
, save_name
);
2312 return error_mark_node
;
2318 found_and_maybe_warn
:
2319 if ((cp
->harshness
[0].code
& CONST_CODE
)
2320 /* 12.1p2: Constructors can be called for const objects. */
2321 && ! DECL_CONSTRUCTOR_P (cp
->function
))
2323 if (flags
& LOOKUP_COMPLAIN
)
2325 cp_error_at ("non-const member function `%D'", cp
->function
);
2326 error ("called for const object at this point in file");
2328 /* Not good enough for a match. */
2330 return error_mark_node
;
2334 /* Silently return error_mark_node. */
2335 return error_mark_node
;
2338 if (flags
& LOOKUP_PROTECT
)
2339 access
= compute_access (basetype_path
, function
);
2341 if (access
== access_private_node
)
2343 if (flags
& LOOKUP_COMPLAIN
)
2345 cp_error_at ("%s `%+#D' is %s", name_kind
, function
,
2346 TREE_PRIVATE (function
) ? "private"
2347 : "from private base class");
2348 error ("within this context");
2350 return error_mark_node
;
2352 else if (access
== access_protected_node
)
2354 if (flags
& LOOKUP_COMPLAIN
)
2356 cp_error_at ("%s `%+#D' %s", name_kind
, function
,
2357 TREE_PROTECTED (function
) ? "is protected"
2358 : "has protected accessibility");
2359 error ("within this context");
2361 return error_mark_node
;
2364 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2365 type (if it exists) is a pointer to. */
2367 if (DECL_ABSTRACT_VIRTUAL_P (function
)
2368 && instance
== C_C_D
2369 && DECL_CONSTRUCTOR_P (current_function_decl
)
2370 && ! (flags
& LOOKUP_NONVIRTUAL
)
2371 && value_member (function
, get_abstract_virtuals (basetype
)))
2372 cp_error ("abstract virtual `%#D' called from constructor", function
);
2374 if (IS_SIGNATURE (basetype
) && static_call_context
)
2376 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2377 basetype
, save_name
);
2378 return error_mark_node
;
2380 else if (IS_SIGNATURE (basetype
))
2381 return build_signature_method_call (basetype
, instance
, function
, parms
);
2383 function
= DECL_MAIN_VARIANT (function
);
2384 mark_used (function
);
2386 /* Is it a synthesized method that needs to be synthesized? */
2387 if (DECL_ARTIFICIAL (function
) && ! DECL_INITIAL (function
)
2388 /* Kludge: don't synthesize for default args. */
2389 && current_function_decl
)
2390 synthesize_method (function
);
2392 if (pedantic
&& DECL_THIS_INLINE (function
) && ! DECL_ARTIFICIAL (function
)
2393 && ! DECL_INITIAL (function
) && ! DECL_PENDING_INLINE_INFO (function
)
2394 && ! (DECL_TEMPLATE_INFO (function
)
2395 && TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (function
))))
2396 cp_warning ("inline function `%#D' called before definition", function
);
2398 fntype
= TREE_TYPE (function
);
2399 if (TREE_CODE (fntype
) == POINTER_TYPE
)
2400 fntype
= TREE_TYPE (fntype
);
2401 basetype
= DECL_CLASS_CONTEXT (function
);
2403 /* If we are referencing a virtual function from an object
2404 of effectively static type, then there is no need
2405 to go through the virtual function table. */
2406 if (need_vtbl
== maybe_needed
)
2408 int fixed_type
= resolves_to_fixed_type_p (instance
, 0);
2410 if (all_virtual
== 1
2411 && DECL_VINDEX (function
)
2412 && may_be_remote (basetype
))
2414 else if (DECL_VINDEX (function
))
2415 need_vtbl
= fixed_type
? unneeded
: needed
;
2417 need_vtbl
= not_needed
;
2420 if (TREE_CODE (fntype
) == METHOD_TYPE
&& static_call_context
2421 && !DECL_CONSTRUCTOR_P (function
))
2423 /* Let's be nasty to the user now, and give reasonable
2425 instance_ptr
= current_class_decl
;
2428 if (basetype
!= current_class_type
)
2430 if (basetype
== error_mark_node
)
2431 return error_mark_node
;
2434 if (orig_basetype
!= NULL_TREE
)
2435 error_not_base_type (orig_basetype
, current_class_type
);
2437 error_not_base_type (function
, current_class_type
);
2438 return error_mark_node
;
2442 /* Only allow a static member function to call another static member
2444 else if (DECL_LANG_SPECIFIC (function
)
2445 && !DECL_STATIC_FUNCTION_P (function
))
2447 cp_error ("cannot call member function `%D' without object",
2449 return error_mark_node
;
2453 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2455 if (TYPE_SIZE (complete_type (value_type
)) == 0)
2457 if (flags
& LOOKUP_COMPLAIN
)
2458 incomplete_type_error (0, value_type
);
2459 return error_mark_node
;
2462 if (DECL_STATIC_FUNCTION_P (function
))
2463 parms
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2464 TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
);
2465 else if (need_vtbl
== unneeded
)
2467 int sub_flags
= DECL_CONSTRUCTOR_P (function
) ? flags
: LOOKUP_NORMAL
;
2468 basetype
= TREE_TYPE (instance
);
2469 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function
)) != TYPE_MAIN_VARIANT (basetype
)
2470 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
2472 basetype
= DECL_CLASS_CONTEXT (function
);
2473 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
2474 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2476 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2477 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, sub_flags
));
2481 if ((flags
& LOOKUP_NONVIRTUAL
) == 0)
2482 basetype
= DECL_CONTEXT (function
);
2484 /* First parm could be integer_zerop with casts like
2485 ((Object*)0)->Object::IsA() */
2486 if (!integer_zerop (TREE_VALUE (parms
)))
2488 /* Since we can't have inheritance with a union, doing get_binfo
2489 on it won't work. We do all the convert_pointer_to_real
2490 stuff to handle MI correctly...for unions, that's not
2491 an issue, so we must short-circuit that extra work here. */
2492 tree tmp
= TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)));
2493 if (tmp
!= NULL_TREE
&& TREE_CODE (tmp
) == UNION_TYPE
)
2494 instance_ptr
= TREE_VALUE (parms
);
2497 tree binfo
= get_binfo (basetype
,
2498 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
))),
2500 instance_ptr
= convert_pointer_to_real (binfo
, TREE_VALUE (parms
));
2503 = convert_pointer_to (build_type_variant (basetype
,
2507 if (TREE_CODE (instance_ptr
) == COND_EXPR
)
2509 instance_ptr
= save_expr (instance_ptr
);
2510 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2512 else if (TREE_CODE (instance_ptr
) == NOP_EXPR
2513 && TREE_CODE (TREE_OPERAND (instance_ptr
, 0)) == ADDR_EXPR
2514 && TREE_OPERAND (TREE_OPERAND (instance_ptr
, 0), 0) == instance
)
2516 /* The call to `convert_pointer_to' may return error_mark_node. */
2517 else if (TREE_CODE (instance_ptr
) == ERROR_MARK
)
2518 return instance_ptr
;
2519 else if (instance
== NULL_TREE
2520 || TREE_CODE (instance
) != INDIRECT_REF
2521 || TREE_OPERAND (instance
, 0) != instance_ptr
)
2522 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2524 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2525 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
));
2528 if (parms
== error_mark_node
2529 || (parms
&& TREE_CHAIN (parms
) == error_mark_node
))
2530 return error_mark_node
;
2532 if (need_vtbl
== needed
)
2534 function
= build_vfn_ref (&TREE_VALUE (parms
), instance
,
2535 DECL_VINDEX (function
));
2536 TREE_TYPE (function
) = build_pointer_type (fntype
);
2539 if (TREE_CODE (function
) == FUNCTION_DECL
)
2540 GNU_xref_call (current_function_decl
,
2541 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)));
2545 = TREE_CODE (function
) == FUNCTION_DECL
2546 && DECL_CONSTRUCTOR_P (function
);
2548 function
= default_conversion (function
);
2550 result
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
2552 TREE_TYPE (result
) = value_type
;
2553 TREE_SIDE_EFFECTS (result
) = 1;
2554 TREE_HAS_CONSTRUCTOR (result
) = is_constructor
;
2555 result
= convert_from_reference (result
);
2560 /* Similar to `build_method_call', but for overloaded non-member functions.
2561 The name of this function comes through NAME. The name depends
2564 Note that this function must handle simple `C' promotions,
2565 as well as variable numbers of arguments (...), and
2566 default arguments to boot.
2568 If the overloading is successful, we return a tree node which
2569 contains the call to the function.
2571 If overloading produces candidates which are probable, but not definite,
2572 we hold these candidates. If FINAL_CP is non-zero, then we are free
2573 to assume that final_cp points to enough storage for all candidates that
2574 this function might generate. The `harshness' array is preallocated for
2575 the first candidate, but not for subsequent ones.
2577 Note that the DECL_RTL of FUNCTION must be made to agree with this
2578 function's new name. */
2581 build_overload_call_real (fnname
, parms
, flags
, final_cp
, require_complete
)
2584 struct candidate
*final_cp
;
2585 int require_complete
;
2587 /* must check for overloading here */
2588 tree functions
, function
, parm
;
2589 tree parmtypes
= NULL_TREE
, last
= NULL_TREE
;
2590 register tree outer
;
2592 int parmlength
= list_length (parms
);
2594 struct candidate
*candidates
, *cp
;
2598 final_cp
[0].h
.code
= 0;
2599 final_cp
[0].h
.distance
= 0;
2600 final_cp
[0].function
= 0;
2602 final_cp
[1].h
.code
= EVIL_CODE
;
2605 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2607 register tree t
= TREE_TYPE (TREE_VALUE (parm
));
2609 if (t
== error_mark_node
)
2612 final_cp
->h
.code
= EVIL_CODE
;
2613 return error_mark_node
;
2615 if (TREE_CODE (t
) == OFFSET_TYPE
)
2617 /* This breaks reference-to-array parameters. */
2618 || TREE_CODE (t
) == ARRAY_TYPE
2621 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
2622 Also convert OFFSET_TYPE entities to their normal selves.
2623 This eliminates needless calls to `compute_conversion_costs'. */
2624 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
2625 t
= TREE_TYPE (TREE_VALUE (parm
));
2627 last
= build_tree_list (NULL_TREE
, t
);
2628 parmtypes
= chainon (parmtypes
, last
);
2631 TREE_CHAIN (last
) = void_list_node
;
2633 parmtypes
= void_list_node
;
2635 if (is_overloaded_fn (fnname
))
2638 if (TREE_CODE (fnname
) == TREE_LIST
)
2639 fnname
= TREE_PURPOSE (functions
);
2640 else if (TREE_CODE (fnname
) == FUNCTION_DECL
)
2641 fnname
= DECL_NAME (functions
);
2644 functions
= lookup_name_nonclass (fnname
);
2646 if (functions
== NULL_TREE
)
2648 if (flags
& LOOKUP_SPECULATIVELY
)
2650 if (flags
& LOOKUP_COMPLAIN
)
2651 error ("only member functions apply");
2653 final_cp
->h
.code
= EVIL_CODE
;
2654 return error_mark_node
;
2657 if (TREE_CODE (functions
) == FUNCTION_DECL
&& ! IDENTIFIER_OPNAME_P (fnname
))
2659 functions
= DECL_MAIN_VARIANT (functions
);
2662 /* We are just curious whether this is a viable alternative or
2664 compute_conversion_costs (functions
, parms
, final_cp
, parmlength
);
2668 return build_function_call_real (functions
, parms
, 1, flags
);
2671 if (TREE_CODE (functions
) == TREE_LIST
2672 && TREE_VALUE (functions
) == NULL_TREE
)
2674 if (flags
& LOOKUP_SPECULATIVELY
)
2677 if (flags
& LOOKUP_COMPLAIN
)
2678 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2679 TREE_PURPOSE (functions
));
2681 final_cp
->h
.code
= EVIL_CODE
;
2682 return error_mark_node
;
2685 length
= count_functions (functions
);
2688 candidates
= final_cp
;
2692 = (struct candidate
*)alloca ((length
+1) * sizeof (struct candidate
));
2693 bzero ((char *) candidates
, (length
+ 1) * sizeof (struct candidate
));
2698 my_friendly_assert (is_overloaded_fn (functions
), 169);
2700 functions
= get_first_fn (functions
);
2702 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2703 for (outer
= functions
; outer
; outer
= DECL_CHAIN (outer
))
2705 int template_cost
= 0;
2707 if (TREE_CODE (function
) != FUNCTION_DECL
2708 && ! (TREE_CODE (function
) == TEMPLATE_DECL
2709 && TREE_CODE (DECL_TEMPLATE_RESULT (function
)) == FUNCTION_DECL
))
2711 enum tree_code code
= TREE_CODE (function
);
2712 if (code
== TEMPLATE_DECL
)
2713 code
= TREE_CODE (DECL_TEMPLATE_RESULT (function
));
2714 if (code
== CONST_DECL
)
2716 ("enumeral value `%D' conflicts with function of same name",
2718 else if (code
== VAR_DECL
)
2720 if (TREE_STATIC (function
))
2722 ("variable `%D' conflicts with function of same name",
2726 ("constant field `%D' conflicts with function of same name",
2729 else if (code
== TYPE_DECL
)
2732 my_friendly_abort (2);
2733 error ("at this point in file");
2736 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2738 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function
));
2739 tree
*targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2742 i
= type_unification (DECL_TEMPLATE_PARMS (function
), targs
,
2743 TYPE_ARG_TYPES (TREE_TYPE (function
)),
2744 parms
, &template_cost
, 0);
2747 function
= instantiate_template (function
, targs
);
2748 if (function
== error_mark_node
)
2753 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2755 /* Unconverted template -- failed match. */
2756 cp
->function
= function
;
2758 cp
->h
.code
= EVIL_CODE
;
2762 struct candidate
*cp2
;
2764 /* Check that this decl is not the same as a function that's in
2765 the list due to some template instantiation. */
2768 if (cp2
->function
== function
)
2772 if (cp2
->function
== function
)
2775 function
= DECL_MAIN_VARIANT (function
);
2777 /* Can't use alloca here, since result might be
2778 passed to calling function. */
2779 cp
->h_len
= parmlength
;
2780 cp
->harshness
= (struct harshness_code
*)
2781 oballoc ((parmlength
+ 1) * sizeof (struct harshness_code
));
2783 compute_conversion_costs (function
, parms
, cp
, parmlength
);
2785 /* Make sure this is clear as well. */
2786 cp
->h
.int_penalty
+= template_cost
;
2788 if ((cp
[0].h
.code
& EVIL_CODE
) == 0)
2790 cp
[1].h
.code
= EVIL_CODE
;
2796 if (cp
- candidates
)
2798 tree rval
= error_mark_node
;
2801 cp
[0].h
.code
= EVIL_CODE
;
2802 if (cp
- candidates
> 1)
2804 struct candidate
*best_cp
2805 = ideal_candidate (candidates
, cp
- candidates
, parmlength
);
2806 if (best_cp
== (struct candidate
*)0)
2808 if (flags
& LOOKUP_COMPLAIN
)
2810 cp_error ("call of overloaded `%D' is ambiguous", fnname
);
2811 print_n_candidates (candidates
, cp
- candidates
);
2813 return error_mark_node
;
2816 rval
= best_cp
->function
;
2821 if (cp
->h
.code
& EVIL_CODE
)
2823 if (flags
& LOOKUP_COMPLAIN
)
2824 error ("type conversion ambiguous");
2827 rval
= cp
->function
;
2833 return build_function_call_real (rval
, parms
, require_complete
, flags
);
2836 if (flags
& LOOKUP_SPECULATIVELY
)
2839 if (flags
& LOOKUP_COMPLAIN
)
2840 report_type_mismatch (cp
, parms
, "function",
2841 decl_as_string (cp
->function
, 1));
2843 return error_mark_node
;
2846 /* This requires a complete type on the result of the call. */
2848 build_overload_call (fnname
, parms
, flags
, final_cp
)
2851 struct candidate
*final_cp
;
2853 return build_overload_call_real (fnname
, parms
, flags
, final_cp
, 1);