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
;
643 tree cand
= TREE_VALUE (conv
);
645 if (winner
&& winner
== cand
)
648 tmp
= convert_harshness (type
, TREE_TYPE (TREE_TYPE (cand
)), NULL_TREE
);
649 if ((tmp
.code
< USER_CODE
) && (tmp
.distance
>= 0))
668 can_convert (to
, from
)
671 struct harshness_code h
;
672 h
= convert_harshness (to
, from
, NULL_TREE
);
673 return (h
.code
< USER_CODE
) && (h
.distance
>= 0);
677 can_convert_arg (to
, from
, arg
)
680 struct harshness_code h
;
681 h
= convert_harshness (to
, from
, arg
);
682 return (h
.code
< USER_CODE
) && (h
.distance
>= 0);
685 #ifdef DEBUG_MATCHING
688 struct harshness_code
*h
;
690 static char buf
[1024];
693 bzero (buf
, 1024 * sizeof (char));
694 strcat (buf
, "codes=[");
695 if (h
->code
& EVIL_CODE
)
696 strcat (buf
, "EVIL");
697 if (h
->code
& CONST_CODE
)
698 strcat (buf
, " CONST");
699 if (h
->code
& ELLIPSIS_CODE
)
700 strcat (buf
, " ELLIPSIS");
701 if (h
->code
& USER_CODE
)
702 strcat (buf
, " USER");
703 if (h
->code
& STD_CODE
)
704 strcat (buf
, " STD");
705 if (h
->code
& PROMO_CODE
)
706 strcat (buf
, " PROMO");
707 if (h
->code
& QUAL_CODE
)
708 strcat (buf
, " QUAL");
709 if (h
->code
& TRIVIAL_CODE
)
710 strcat (buf
, " TRIVIAL");
714 sprintf (tmp
, "] distance=%d int_penalty=%d", h
->distance
, h
->int_penalty
);
722 /* Algorithm: For each argument, calculate how difficult it is to
723 make FUNCTION accept that argument. If we can easily tell that
724 FUNCTION won't be acceptable to one of the arguments, then we
725 don't need to compute the ease of converting the other arguments,
726 since it will never show up in the intersection of all arguments'
729 Conversions between builtin and user-defined types are allowed, but
730 no function involving such a conversion is preferred to one which
731 does not require such a conversion. Furthermore, such conversions
735 compute_conversion_costs (function
, tta_in
, cp
, arglen
)
738 struct candidate
*cp
;
741 tree ttf_in
= TYPE_ARG_TYPES (TREE_TYPE (function
));
745 /* Start out with no strikes against. */
746 int evil_strikes
= 0;
747 int ellipsis_strikes
= 0;
748 int user_strikes
= 0;
749 int b_or_d_strikes
= 0;
750 int easy_strikes
= 0;
752 int strike_index
= 0, win
;
753 struct harshness_code lose
;
754 extern int cp_silent
;
756 #ifdef GATHER_STATISTICS
757 n_compute_conversion_costs
++;
760 #ifndef DEBUG_MATCHING
761 /* We don't emit any warnings or errors while trying out each candidate. */
765 cp
->function
= function
;
766 cp
->arg
= tta
? TREE_VALUE (tta
) : NULL_TREE
;
767 cp
->u
.bad_arg
= 0; /* optimistic! */
771 cp
->h
.int_penalty
= 0;
772 bzero ((char *) cp
->harshness
,
773 (cp
->h_len
+ 1) * sizeof (struct harshness_code
));
777 struct harshness_code h
;
779 if (ttf
== void_list_node
)
782 if (type_unknown_p (TREE_VALUE (tta
)))
784 /* Must perform some instantiation here. */
785 tree rhs
= TREE_VALUE (tta
);
786 tree lhstype
= TREE_VALUE (ttf
);
788 /* Keep quiet about possible contravariance violations. */
789 int old_inhibit_warnings
= inhibit_warnings
;
790 inhibit_warnings
= 1;
792 /* @@ This is to undo what `grokdeclarator' does to
793 parameter types. It really should go through
794 something more general. */
796 TREE_TYPE (tta
) = unknown_type_node
;
797 rhs
= instantiate_type (lhstype
, rhs
, 0);
798 inhibit_warnings
= old_inhibit_warnings
;
800 if (TREE_CODE (rhs
) == ERROR_MARK
)
803 h
= convert_harshness (lhstype
, TREE_TYPE (rhs
), rhs
);
807 #ifdef DEBUG_MATCHING
808 static tree old_function
= NULL_TREE
;
810 if (!old_function
|| function
!= old_function
)
812 cp_error ("trying %D", function
);
813 old_function
= function
;
816 cp_error (" doing (%T) %E against arg %T",
817 TREE_TYPE (TREE_VALUE (tta
)), TREE_VALUE (tta
),
821 h
= convert_harshness (TREE_VALUE (ttf
),
822 TREE_TYPE (TREE_VALUE (tta
)),
825 #ifdef DEBUG_MATCHING
826 cp_error (" evaluated %s", print_harshness (&h
));
830 cp
->harshness
[strike_index
] = h
;
831 if ((h
.code
& EVIL_CODE
)
832 || ((h
.code
& STD_CODE
) && h
.distance
< 0))
834 cp
->u
.bad_arg
= strike_index
;
837 else if (h
.code
& ELLIPSIS_CODE
)
838 ellipsis_strikes
+= 1;
840 /* This is never set by `convert_harshness'. */
841 else if (h
.code
& USER_CODE
)
848 if ((h
.code
& STD_CODE
) && h
.distance
)
850 if (h
.distance
> b_or_d_strikes
)
851 b_or_d_strikes
= h
.distance
;
854 easy_strikes
+= (h
.code
& (STD_CODE
|PROMO_CODE
|TRIVIAL_CODE
));
855 cp
->h
.code
|= h
.code
;
856 /* Make sure we communicate this. */
857 cp
->h
.int_penalty
+= h
.int_penalty
;
860 ttf
= TREE_CHAIN (ttf
);
861 tta
= TREE_CHAIN (tta
);
867 /* ran out of formals, and parmlist is fixed size. */
868 if (ttf
/* == void_type_node */)
870 cp
->h
.code
= EVIL_CODE
;
877 struct harshness_code h
;
878 int l
= list_length (tta
);
879 ellipsis_strikes
+= l
;
880 h
.code
= ELLIPSIS_CODE
;
884 cp
->harshness
[strike_index
++] = h
;
887 else if (ttf
&& ttf
!= void_list_node
)
889 /* ran out of actuals, and no defaults. */
890 if (TREE_PURPOSE (ttf
) == NULL_TREE
)
892 cp
->h
.code
= EVIL_CODE
;
897 /* Store index of first default. */
898 cp
->harshness
[arglen
].distance
= strike_index
+1;
901 cp
->harshness
[arglen
].distance
= 0;
903 /* Argument list lengths work out, so don't need to check them again. */
906 /* We do not check for derived->base conversions here, since in
907 no case would they give evil strike counts, unless such conversions
908 are somehow ambiguous. */
910 /* See if any user-defined conversions apply.
911 But make sure that we do not loop. */
912 static int dont_convert_types
= 0;
914 if (dont_convert_types
)
916 cp
->h
.code
= EVIL_CODE
;
921 win
= 0; /* Only get one chance to win. */
922 ttf
= TYPE_ARG_TYPES (TREE_TYPE (function
));
929 if (ttf
== void_list_node
)
932 lose
= cp
->harshness
[strike_index
];
933 if ((lose
.code
& EVIL_CODE
)
934 || ((lose
.code
& STD_CODE
) && lose
.distance
< 0))
936 tree actual_type
= TREE_TYPE (TREE_VALUE (tta
));
937 tree formal_type
= TREE_VALUE (ttf
);
938 int extra_conversions
= 0;
940 dont_convert_types
= 1;
942 if (TREE_CODE (formal_type
) == REFERENCE_TYPE
)
943 formal_type
= TREE_TYPE (formal_type
);
944 if (TREE_CODE (actual_type
) == REFERENCE_TYPE
)
945 actual_type
= TREE_TYPE (actual_type
);
947 if (formal_type
!= error_mark_node
948 && actual_type
!= error_mark_node
)
950 formal_type
= TYPE_MAIN_VARIANT (formal_type
);
951 actual_type
= TYPE_MAIN_VARIANT (actual_type
);
953 if (TYPE_HAS_CONSTRUCTOR (formal_type
))
955 /* If it has a constructor for this type,
957 /* @@ There is no way to save this result yet, so
958 success is a NULL_TREE for now. */
959 if (convert_to_aggr (formal_type
, TREE_VALUE (tta
), 0, 1)
963 if (TYPE_LANG_SPECIFIC (actual_type
)
964 && TYPE_HAS_CONVERSION (actual_type
))
966 int extra
= user_harshness (formal_type
, actual_type
);
968 if (extra
== EVIL_CODE
)
973 extra_conversions
= extra
;
977 dont_convert_types
= 0;
982 cp
->harshness
[strike_index
].code
983 = USER_CODE
| (extra_conversions
? STD_CODE
: 0);
988 if (cp
->u
.bad_arg
> strike_index
)
989 cp
->u
.bad_arg
= strike_index
;
991 evil_strikes
= win
? 2 : 1;
996 ttf
= TREE_CHAIN (ttf
);
997 tta
= TREE_CHAIN (tta
);
1002 /* Const member functions get a small penalty because defaulting
1003 to const is less useful than defaulting to non-const. */
1004 /* This is bogus, it does not correspond to anything in the ARM.
1005 This code will be fixed when this entire section is rewritten
1006 to conform to the ARM. (mrs) */
1007 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1009 tree this_parm
= TREE_VALUE (ttf_in
);
1011 if (TREE_CODE (this_parm
) == RECORD_TYPE
/* Is `this' a sig ptr? */
1012 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm
))))
1013 : TYPE_READONLY (TREE_TYPE (this_parm
)))
1015 cp
->harshness
[0].code
|= TRIVIAL_CODE
;
1020 /* Calling a non-const member function from a const member function
1021 is probably invalid, but for now we let it only draw a warning.
1022 We indicate that such a mismatch has occurred by setting the
1023 harshness to a maximum value. */
1024 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in
))) == POINTER_TYPE
1025 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in
))))))
1026 cp
->harshness
[0].code
|= CONST_CODE
;
1031 cp
->h
.code
= EVIL_CODE
;
1032 if (ellipsis_strikes
)
1033 cp
->h
.code
|= ELLIPSIS_CODE
;
1035 cp
->h
.code
|= USER_CODE
;
1037 #ifdef DEBUG_MATCHING
1038 cp_error ("final eval %s", print_harshness (&cp
->h
));
1042 /* Subroutine of ideal_candidate. See if X or Y is a better match
1045 strictly_better (x
, y
)
1046 unsigned short x
, y
;
1054 if (xor >= x
|| xor >= y
)
1059 /* When one of several possible overloaded functions and/or methods
1060 can be called, choose the best candidate for overloading.
1062 BASETYPE is the context from which we start method resolution
1063 or NULL if we are comparing overloaded functions.
1064 CANDIDATES is the array of candidates we have to choose from.
1065 N_CANDIDATES is the length of CANDIDATES.
1066 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1067 choose. It is modified in place when resolving methods. It is not
1068 modified in place when resolving overloaded functions.
1069 LEN is the length of the parameter list. */
1071 static struct candidate
*
1072 ideal_candidate (candidates
, n_candidates
, len
)
1073 struct candidate
*candidates
;
1077 struct candidate
*cp
= candidates
+n_candidates
;
1078 int i
, j
= -1, best_code
;
1080 /* For each argument, sort the functions from best to worst for the arg.
1081 For each function that's not best for this arg, set its overall
1082 harshness to EVIL so that other args won't like it. The candidate
1083 list for the last argument is the intersection of all the best-liked
1086 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
1088 best_code
= cp
[-1].h
.code
;
1090 /* If they're at least as good as each other, do an arg-by-arg check. */
1091 if (! strictly_better (cp
[-1].h
.code
, cp
[-2].h
.code
))
1096 for (j
= 0; j
< n_candidates
; j
++)
1097 if (! strictly_better (candidates
[j
].h
.code
, best_code
))
1100 qsort (candidates
+j
, n_candidates
-j
, sizeof (struct candidate
),
1102 for (i
= 0; i
< len
; i
++)
1104 if (cp
[-1].harshness
[i
].code
< cp
[-2].harshness
[i
].code
)
1106 else if (cp
[-1].harshness
[i
].code
> cp
[-2].harshness
[i
].code
)
1108 else if (cp
[-1].harshness
[i
].code
& STD_CODE
)
1110 /* If it involves a standard conversion, let the
1111 inheritance lattice be the final arbiter. */
1112 if (cp
[-1].harshness
[i
].distance
> cp
[-2].harshness
[i
].distance
)
1114 else if (cp
[-1].harshness
[i
].distance
< cp
[-2].harshness
[i
].distance
)
1117 else if (cp
[-1].harshness
[i
].code
& PROMO_CODE
)
1119 /* For integral promotions, take into account a finer
1120 granularity for determining which types should be favored
1121 over others in such promotions. */
1122 if (cp
[-1].harshness
[i
].int_penalty
> cp
[-2].harshness
[i
].int_penalty
)
1124 else if (cp
[-1].harshness
[i
].int_penalty
< cp
[-2].harshness
[i
].int_penalty
)
1129 if (! better
|| worse
)
1135 /* Assume that if the class referred to is not in the
1136 current class hierarchy, that it may be remote.
1137 PARENT is assumed to be of aggregate type here. */
1139 may_be_remote (parent
)
1142 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent
) == 0)
1145 if (current_class_type
== NULL_TREE
)
1148 if (parent
== current_class_type
)
1151 if (UNIQUELY_DERIVED_FROM_P (parent
, current_class_type
))
1157 build_vfield_ref (datum
, type
)
1161 int old_assume_nonnull_objects
= flag_assume_nonnull_objects
;
1163 if (datum
== error_mark_node
)
1164 return error_mark_node
;
1166 /* Vtable references are always made from non-null objects. */
1167 flag_assume_nonnull_objects
= 1;
1168 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
1169 datum
= convert_from_reference (datum
);
1171 if (! TYPE_USES_COMPLEX_INHERITANCE (type
))
1172 rval
= build (COMPONENT_REF
, TREE_TYPE (CLASSTYPE_VFIELD (type
)),
1173 datum
, CLASSTYPE_VFIELD (type
));
1175 rval
= build_component_ref (datum
, DECL_NAME (CLASSTYPE_VFIELD (type
)), NULL_TREE
, 0);
1176 flag_assume_nonnull_objects
= old_assume_nonnull_objects
;
1181 /* Build a call to a member of an object. I.e., one that overloads
1182 operator ()(), or is a pointer-to-function or pointer-to-method. */
1184 build_field_call (basetype_path
, instance_ptr
, name
, parms
)
1185 tree basetype_path
, instance_ptr
, name
, parms
;
1187 tree field
, instance
;
1189 if (instance_ptr
== current_class_ptr
)
1191 /* Check to see if we really have a reference to an instance variable
1192 with `operator()()' overloaded. */
1193 field
= IDENTIFIER_CLASS_VALUE (name
);
1195 if (field
== NULL_TREE
)
1197 cp_error ("`this' has no member named `%D'", name
);
1198 return error_mark_node
;
1201 if (TREE_CODE (field
) == FIELD_DECL
)
1203 /* If it's a field, try overloading operator (),
1204 or calling if the field is a pointer-to-function. */
1205 instance
= build_component_ref_1 (current_class_ref
, field
, 0);
1206 if (instance
== error_mark_node
)
1207 return error_mark_node
;
1209 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1210 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance
)))
1211 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, instance
, parms
, NULL_TREE
);
1213 if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1215 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == FUNCTION_TYPE
)
1216 return build_function_call (instance
, parms
);
1217 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == METHOD_TYPE
)
1218 return build_function_call (instance
, tree_cons (NULL_TREE
, current_class_ptr
, parms
));
1224 /* Check to see if this is not really a reference to an instance variable
1225 with `operator()()' overloaded. */
1226 field
= lookup_field (basetype_path
, name
, 1, 0);
1228 /* This can happen if the reference was ambiguous or for access
1230 if (field
== error_mark_node
)
1231 return error_mark_node
;
1236 tree ftype
= TREE_TYPE (field
);
1238 if (TREE_CODE (ftype
) == REFERENCE_TYPE
)
1239 ftype
= TREE_TYPE (ftype
);
1241 if (TYPE_LANG_SPECIFIC (ftype
) && TYPE_OVERLOADS_CALL_EXPR (ftype
))
1243 /* Make the next search for this field very short. */
1244 basetype
= DECL_FIELD_CONTEXT (field
);
1245 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
1247 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1248 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
1249 build_component_ref_1 (instance
, field
, 0),
1252 if (TREE_CODE (ftype
) == POINTER_TYPE
)
1254 if (TREE_CODE (TREE_TYPE (ftype
)) == FUNCTION_TYPE
1255 || TREE_CODE (TREE_TYPE (ftype
)) == METHOD_TYPE
)
1257 /* This is a member which is a pointer to function. */
1259 = build_component_ref_1 (build_indirect_ref (instance_ptr
,
1261 field
, LOOKUP_COMPLAIN
);
1262 if (ref
== error_mark_node
)
1263 return error_mark_node
;
1264 return build_function_call (ref
, parms
);
1267 else if (TREE_CODE (ftype
) == METHOD_TYPE
)
1269 error ("invalid call via pointer-to-member function");
1270 return error_mark_node
;
1279 find_scoped_type (type
, inner_name
, inner_types
)
1280 tree type
, inner_name
, inner_types
;
1282 tree tags
= CLASSTYPE_TAGS (type
);
1286 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1287 enclosing class) is set to the name for the enum type. So, if
1288 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1289 then this test will be true. */
1290 if (TREE_PURPOSE (tags
) == inner_name
)
1292 if (inner_types
== NULL_TREE
)
1293 return TYPE_NAME (TREE_VALUE (tags
));
1294 return resolve_scope_to_name (TREE_VALUE (tags
), inner_types
);
1296 tags
= TREE_CHAIN (tags
);
1299 /* Look for a TYPE_DECL. */
1300 for (tags
= TYPE_FIELDS (type
); tags
; tags
= TREE_CHAIN (tags
))
1301 if (TREE_CODE (tags
) == TYPE_DECL
&& DECL_NAME (tags
) == inner_name
)
1303 /* Code by raeburn. */
1304 if (inner_types
== NULL_TREE
)
1306 return resolve_scope_to_name (TREE_TYPE (tags
), inner_types
);
1312 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1313 the name that names the above nested type. INNER_TYPES
1314 is a chain of nested type names (held together by SCOPE_REFs);
1315 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1316 Returns NULL_TREE if there is an error. */
1318 resolve_scope_to_name (outer_type
, inner_stuff
)
1319 tree outer_type
, inner_stuff
;
1322 tree inner_name
, inner_type
;
1324 if (outer_type
== NULL_TREE
&& current_class_type
!= NULL_TREE
)
1326 /* We first try to look for a nesting in our current class context,
1327 then try any enclosing classes. */
1328 tree type
= current_class_type
;
1330 while (type
&& (TREE_CODE (type
) == RECORD_TYPE
1331 || TREE_CODE (type
) == UNION_TYPE
))
1333 tree rval
= resolve_scope_to_name (type
, inner_stuff
);
1335 if (rval
!= NULL_TREE
)
1337 type
= DECL_CONTEXT (TYPE_NAME (type
));
1341 if (TREE_CODE (inner_stuff
) == SCOPE_REF
)
1343 inner_name
= TREE_OPERAND (inner_stuff
, 0);
1344 inner_type
= TREE_OPERAND (inner_stuff
, 1);
1348 inner_name
= inner_stuff
;
1349 inner_type
= NULL_TREE
;
1352 if (outer_type
== NULL_TREE
)
1355 /* If we have something that's already a type by itself,
1357 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name
))
1360 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name
),
1365 x
= lookup_name (inner_name
, 0);
1367 if (x
&& TREE_CODE (x
) == NAMESPACE_DECL
)
1369 x
= lookup_namespace_name (x
, inner_type
);
1375 if (! IS_AGGR_TYPE (outer_type
))
1378 /* Look for member classes or enums. */
1379 tmp
= find_scoped_type (outer_type
, inner_name
, inner_type
);
1381 /* If it's not a type in this class, then go down into the
1382 base classes and search there. */
1383 if (! tmp
&& TYPE_BINFO (outer_type
))
1385 tree binfos
= TYPE_BINFO_BASETYPES (outer_type
);
1386 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1388 for (i
= 0; i
< n_baselinks
; i
++)
1390 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1391 tmp
= resolve_scope_to_name (BINFO_TYPE (base_binfo
), inner_stuff
);
1401 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1402 This is how virtual function calls are avoided. */
1404 build_scoped_method_call (exp
, basetype
, name
, parms
)
1405 tree exp
, basetype
, name
, parms
;
1407 /* Because this syntactic form does not allow
1408 a pointer to a base class to be `stolen',
1409 we need not protect the derived->base conversion
1412 @@ But we do have to check access privileges later. */
1414 tree type
= TREE_TYPE (exp
);
1416 if (type
== error_mark_node
1417 || basetype
== error_mark_node
)
1418 return error_mark_node
;
1420 if (current_template_parms
)
1422 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1424 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 1);
1425 name
= build_min_nt (BIT_NOT_EXPR
, type
);
1427 name
= build_min_nt (SCOPE_REF
, basetype
, name
);
1428 return build_min_nt (METHOD_CALL_EXPR
, name
, exp
, parms
, NULL_TREE
);
1431 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1432 type
= TREE_TYPE (type
);
1434 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1435 that explicit ~int is caught in the parser; this deals with typedefs
1436 and template parms. */
1437 if (TREE_CODE (name
) == BIT_NOT_EXPR
&& ! IS_AGGR_TYPE (basetype
))
1439 if (type
!= basetype
)
1440 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1441 exp
, basetype
, type
);
1442 name
= TREE_OPERAND (name
, 0);
1443 if (basetype
!= name
&& basetype
!= get_type_value (name
))
1444 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1446 return convert (void_type_node
, exp
);
1449 if (! is_aggr_type (basetype
, 1))
1450 return error_mark_node
;
1452 if (! IS_AGGR_TYPE (type
))
1454 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1456 return error_mark_node
;
1459 if ((binfo
= binfo_or_else (basetype
, type
)))
1461 if (binfo
== error_mark_node
)
1462 return error_mark_node
;
1463 if (TREE_CODE (exp
) == INDIRECT_REF
)
1464 decl
= build_indirect_ref (convert_pointer_to (binfo
,
1465 build_unary_op (ADDR_EXPR
, exp
, 0)), NULL_PTR
);
1467 decl
= build_scoped_ref (exp
, basetype
);
1469 /* Call to a destructor. */
1470 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1472 /* Explicit call to destructor. */
1473 name
= TREE_OPERAND (name
, 0);
1474 if (! (name
== TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
1475 || name
== constructor_name (TREE_TYPE (decl
))
1476 || TREE_TYPE (decl
) == get_type_value (name
)))
1479 ("qualified type `%T' does not match destructor name `~%T'",
1480 TREE_TYPE (decl
), name
);
1481 return error_mark_node
;
1483 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
1484 return convert (void_type_node
, exp
);
1486 return build_delete (TREE_TYPE (decl
), decl
, integer_two_node
,
1487 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
1491 /* Call to a method. */
1492 return build_method_call (decl
, name
, parms
, binfo
,
1493 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
1495 return error_mark_node
;
1499 print_candidates (candidates
)
1502 cp_error_at ("candidates are: %D", TREE_VALUE (candidates
));
1503 candidates
= TREE_CHAIN (candidates
);
1507 cp_error_at (" %D", TREE_VALUE (candidates
));
1508 candidates
= TREE_CHAIN (candidates
);
1513 print_n_candidates (candidates
, n
)
1514 struct candidate
*candidates
;
1519 cp_error_at ("candidates are: %D", candidates
[0].function
);
1520 for (i
= 1; i
< n
; i
++)
1521 cp_error_at (" %D", candidates
[i
].function
);
1524 /* We want the address of a function or method. We avoid creating a
1525 pointer-to-member function. */
1527 build_addr_func (function
)
1530 tree type
= TREE_TYPE (function
);
1532 /* We have to do these by hand to avoid real pointer to member
1534 if (TREE_CODE (type
) == METHOD_TYPE
)
1538 type
= build_pointer_type (type
);
1540 if (mark_addressable (function
) == 0)
1541 return error_mark_node
;
1543 addr
= build1 (ADDR_EXPR
, type
, function
);
1545 /* Address of a static or external variable or function counts
1547 if (staticp (function
))
1548 TREE_CONSTANT (addr
) = 1;
1553 function
= default_conversion (function
);
1558 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1559 POINTER_TYPE to those. Note, pointer to member function types
1560 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
1562 build_call (function
, result_type
, parms
)
1563 tree function
, result_type
, parms
;
1565 int is_constructor
= 0;
1567 function
= build_addr_func (function
);
1569 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
1571 sorry ("unable to call pointer to member function here");
1572 return error_mark_node
;
1575 if (TREE_CODE (function
) == ADDR_EXPR
1576 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
1577 && DECL_CONSTRUCTOR_P (TREE_OPERAND (function
, 0)))
1580 function
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
1581 TREE_HAS_CONSTRUCTOR (function
) = is_constructor
;
1582 TREE_TYPE (function
) = result_type
;
1583 TREE_SIDE_EFFECTS (function
) = 1;
1589 default_parm_conversions (parms
, last
)
1592 tree parm
, parmtypes
= NULL_TREE
;
1596 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1598 tree t
= TREE_TYPE (TREE_VALUE (parm
));
1600 if (TREE_CODE (t
) == OFFSET_TYPE
1601 || TREE_CODE (t
) == METHOD_TYPE
1602 || TREE_CODE (t
) == FUNCTION_TYPE
)
1604 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
1605 t
= TREE_TYPE (TREE_VALUE (parm
));
1608 if (t
== error_mark_node
)
1609 return error_mark_node
;
1611 *last
= build_tree_list (NULL_TREE
, t
);
1612 parmtypes
= chainon (parmtypes
, *last
);
1619 /* Build something of the form ptr->method (args)
1620 or object.method (args). This can also build
1621 calls to constructors, and find friends.
1623 Member functions always take their class variable
1626 INSTANCE is a class instance.
1628 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1630 PARMS help to figure out what that NAME really refers to.
1632 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1633 down to the real instance type to use for access checking. We need this
1634 information to get protected accesses correct. This parameter is used
1635 by build_member_call.
1637 FLAGS is the logical disjunction of zero or more LOOKUP_
1638 flags. See cp-tree.h for more info.
1640 If this is all OK, calls build_function_call with the resolved
1643 This function must also handle being called to perform
1644 initialization, promotion/coercion of arguments, and
1645 instantiation of default parameters.
1647 Note that NAME may refer to an instance variable name. If
1648 `operator()()' is defined for the type of that field, then we return
1651 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
1652 tree instance
, name
, parms
, basetype_path
;
1655 register tree function
, fntype
, value_type
;
1656 register tree basetype
, save_basetype
;
1657 register tree baselink
, result
, parmtypes
, parm
;
1660 tree access
= access_public_node
;
1661 tree orig_basetype
= basetype_path
? BINFO_TYPE (basetype_path
) : NULL_TREE
;
1663 /* Range of cases for vtable optimization. */
1664 enum vtable_needs
{ not_needed
, maybe_needed
, unneeded
, needed
};
1665 enum vtable_needs need_vtbl
= not_needed
;
1668 tree save_name
= name
;
1670 tree instance_ptr
= NULL_TREE
;
1671 int all_virtual
= flag_all_virtual
;
1672 int static_call_context
= 0;
1673 tree found_fns
= NULL_TREE
;
1675 /* Keep track of `const' and `volatile' objects. */
1676 int constp
, volatilep
;
1678 #ifdef GATHER_STATISTICS
1679 n_build_method_call
++;
1682 if (instance
== error_mark_node
1683 || name
== error_mark_node
1684 || parms
== error_mark_node
1685 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
1686 return error_mark_node
;
1688 if (current_template_parms
)
1690 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1692 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 1);
1693 name
= build_min_nt (BIT_NOT_EXPR
, type
);
1696 return build_min_nt (METHOD_CALL_EXPR
, name
, instance
, parms
, NULL_TREE
);
1699 /* This is the logic that magically deletes the second argument to
1700 operator delete, if it is not needed. */
1701 if (name
== ansi_opname
[(int) DELETE_EXPR
] && list_length (parms
)==2)
1703 tree save_last
= TREE_CHAIN (parms
);
1705 /* get rid of unneeded argument */
1706 TREE_CHAIN (parms
) = NULL_TREE
;
1707 result
= build_method_call (instance
, name
, parms
, basetype_path
,
1708 (LOOKUP_SPECULATIVELY
|flags
)
1710 /* If it finds a match, return it. */
1712 return build_method_call (instance
, name
, parms
, basetype_path
, flags
);
1713 /* If it doesn't work, two argument delete must work */
1714 TREE_CHAIN (parms
) = save_last
;
1716 /* We already know whether it's needed or not for vec delete. */
1717 else if (name
== ansi_opname
[(int) VEC_DELETE_EXPR
]
1718 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1719 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance
)))
1720 TREE_CHAIN (parms
) = NULL_TREE
;
1722 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1724 flags
|= LOOKUP_DESTRUCTOR
;
1725 name
= TREE_OPERAND (name
, 0);
1727 error ("destructors take no parameters");
1728 basetype
= TREE_TYPE (instance
);
1729 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1730 basetype
= TREE_TYPE (basetype
);
1731 if (! (name
== basetype
1732 || (IS_AGGR_TYPE (basetype
)
1733 && name
== constructor_name (basetype
))
1734 || basetype
== get_type_value (name
)))
1736 cp_error ("destructor name `~%D' does not match type `%T' of expression",
1738 return convert (void_type_node
, instance
);
1741 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1742 return convert (void_type_node
, instance
);
1743 instance
= default_conversion (instance
);
1744 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1745 return build_delete (build_pointer_type (basetype
),
1746 instance_ptr
, integer_two_node
,
1747 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
1753 /* Initialize name for error reporting. */
1754 if (IDENTIFIER_OPNAME_P (name
) && ! IDENTIFIER_TYPENAME_P (name
))
1756 char *p
= operator_name_string (name
);
1757 xref_name
= (char *)alloca (strlen (p
) + 10);
1758 sprintf (xref_name
, "operator %s", p
);
1760 else if (TREE_CODE (name
) == SCOPE_REF
)
1761 xref_name
= IDENTIFIER_POINTER (TREE_OPERAND (name
, 1));
1763 xref_name
= IDENTIFIER_POINTER (name
);
1765 GNU_xref_call (current_function_decl
, xref_name
);
1768 if (instance
== NULL_TREE
)
1770 basetype
= NULL_TREE
;
1771 /* Check cases where this is really a call to raise
1773 if (current_class_type
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
1775 basetype
= purpose_member (name
, CLASSTYPE_TAGS (current_class_type
));
1777 basetype
= TREE_VALUE (basetype
);
1779 else if (TREE_CODE (name
) == SCOPE_REF
1780 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
1782 if (! is_aggr_typedef (TREE_OPERAND (name
, 0), 1))
1783 return error_mark_node
;
1784 basetype
= purpose_member (TREE_OPERAND (name
, 1),
1785 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name
, 0))));
1787 basetype
= TREE_VALUE (basetype
);
1790 if (basetype
!= NULL_TREE
)
1792 /* call to a constructor... */
1793 else if (basetype_path
)
1795 basetype
= BINFO_TYPE (basetype_path
);
1796 if (name
== TYPE_IDENTIFIER (basetype
))
1797 name
= ctor_identifier
;
1799 else if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1801 basetype
= IDENTIFIER_TYPE_VALUE (name
);
1802 name
= ctor_identifier
;
1806 tree typedef_name
= lookup_name (name
, 1);
1807 if (typedef_name
&& TREE_CODE (typedef_name
) == TYPE_DECL
)
1809 /* Canonicalize the typedef name. */
1810 basetype
= TREE_TYPE (typedef_name
);
1811 name
= ctor_identifier
;
1815 cp_error ("no constructor named `%T' in scope",
1817 return error_mark_node
;
1821 if (! IS_AGGR_TYPE (basetype
))
1824 if ((flags
& LOOKUP_COMPLAIN
) && TREE_CODE (basetype
) != ERROR_MARK
)
1825 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1826 name
, instance
, basetype
);
1828 return error_mark_node
;
1831 else if (instance
== current_class_ref
|| instance
== current_class_ptr
)
1833 /* When doing initialization, we side-effect the TREE_TYPE of
1834 current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1835 basetype
= TREE_TYPE (current_class_ref
);
1837 /* Anything manifestly `this' in constructors and destructors
1838 has a known type, so virtual function tables are not needed. */
1839 if (TYPE_VIRTUAL_P (basetype
)
1840 && !(flags
& LOOKUP_NONVIRTUAL
))
1841 need_vtbl
= (dtor_label
|| ctor_label
)
1842 ? unneeded
: maybe_needed
;
1844 /* If `this' is a signature pointer and `name' is not a constructor,
1845 we are calling a signature member function. In that case, set the
1846 `basetype' to the signature type and dereference the `optr' field. */
1847 if (IS_SIGNATURE_POINTER (basetype
)
1848 && TYPE_IDENTIFIER (basetype
) != name
)
1850 basetype
= SIGNATURE_TYPE (basetype
);
1851 instance_ptr
= build_optr_ref (instance
);
1852 instance_ptr
= convert (build_pointer_type (basetype
), instance_ptr
);
1853 basetype_path
= TYPE_BINFO (basetype
);
1857 instance
= current_class_ref
;
1858 instance_ptr
= current_class_ptr
;
1859 basetype_path
= TYPE_BINFO (current_class_type
);
1861 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1866 else if (TREE_CODE (instance
) == RESULT_DECL
)
1868 basetype
= TREE_TYPE (instance
);
1869 /* Should we ever have to make a virtual function reference
1870 from a RESULT_DECL, know that it must be of fixed type
1871 within the scope of this function. */
1872 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1873 need_vtbl
= maybe_needed
;
1874 instance_ptr
= build1 (ADDR_EXPR
, build_pointer_type (basetype
), instance
);
1878 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1879 tree inst_ptr_basetype
;
1881 static_call_context
=
1882 (TREE_CODE (instance
) == INDIRECT_REF
1883 && TREE_CODE (TREE_OPERAND (instance
, 0)) == NOP_EXPR
1884 && TREE_OPERAND (TREE_OPERAND (instance
, 0), 0) == error_mark_node
);
1886 if (TREE_CODE (instance
) == OFFSET_REF
)
1887 instance
= resolve_offset_ref (instance
);
1889 /* the base type of an instance variable is pointer to class */
1890 basetype
= TREE_TYPE (instance
);
1892 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1894 basetype
= TREE_TYPE (basetype
);
1895 if (! IS_AGGR_TYPE (basetype
))
1896 goto non_aggr_error
;
1897 /* Call to convert not needed because we are remaining
1898 within the same type. */
1899 instance_ptr
= build1 (NOP_EXPR
, build_pointer_type (basetype
),
1901 inst_ptr_basetype
= TYPE_MAIN_VARIANT (basetype
);
1905 if (! IS_AGGR_TYPE (basetype
)
1906 && ! (TYPE_LANG_SPECIFIC (basetype
)
1907 && (IS_SIGNATURE_POINTER (basetype
)
1908 || IS_SIGNATURE_REFERENCE (basetype
))))
1909 goto non_aggr_error
;
1911 /* If `instance' is a signature pointer/reference and `name' is
1912 not a constructor, we are calling a signature member function.
1913 In that case set the `basetype' to the signature type. */
1914 if ((IS_SIGNATURE_POINTER (basetype
)
1915 || IS_SIGNATURE_REFERENCE (basetype
))
1916 && TYPE_IDENTIFIER (basetype
) != name
)
1917 basetype
= SIGNATURE_TYPE (basetype
);
1919 if ((IS_SIGNATURE (basetype
)
1920 && (instance_ptr
= instance
))
1921 || (lvalue_p (instance
)
1922 && (instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0)))
1923 || (instance_ptr
= unary_complex_lvalue (ADDR_EXPR
, instance
)))
1925 if (instance_ptr
== error_mark_node
)
1926 return error_mark_node
;
1928 else if (TREE_CODE (instance
) == NOP_EXPR
1929 || TREE_CODE (instance
) == CONSTRUCTOR
)
1931 /* A cast is not an lvalue. Initialize a fresh temp
1932 with the value we are casting from, and proceed with
1933 that temporary. We can't cast to a reference type,
1934 so that simplifies the initialization to something
1936 tree temp
= get_temp_name (TREE_TYPE (instance
), 0);
1937 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
1938 expand_aggr_init (temp
, instance
, 0, flags
);
1941 store_init_value (temp
, instance
);
1942 expand_decl_init (temp
);
1945 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1949 if (TREE_CODE (instance
) != CALL_EXPR
)
1950 my_friendly_abort (125);
1951 if (TYPE_NEEDS_CONSTRUCTING (basetype
))
1952 instance
= build_cplus_new (basetype
, instance
);
1955 instance
= get_temp_name (basetype
, 0);
1956 TREE_ADDRESSABLE (instance
) = 1;
1958 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1960 /* @@ Should we call comp_target_types here? */
1961 if (IS_SIGNATURE (basetype
))
1962 inst_ptr_basetype
= basetype
;
1964 inst_ptr_basetype
= TREE_TYPE (TREE_TYPE (instance_ptr
));
1965 if (TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1966 basetype
= inst_ptr_basetype
;
1969 instance_ptr
= convert (build_pointer_type (basetype
), instance_ptr
);
1970 if (instance_ptr
== error_mark_node
)
1971 return error_mark_node
;
1975 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1976 not updated, so we use `basetype' instead. */
1977 if (basetype_path
== NULL_TREE
1978 && IS_SIGNATURE (basetype
))
1979 basetype_path
= TYPE_BINFO (basetype
);
1980 else if (basetype_path
== NULL_TREE
||
1981 BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1982 basetype_path
= TYPE_BINFO (inst_ptr_basetype
);
1984 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1988 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1990 if (TREE_SIDE_EFFECTS (instance_ptr
))
1992 /* This action is needed because the instance is needed
1993 for providing the base of the virtual function table.
1994 Without using a SAVE_EXPR, the function we are building
1995 may be called twice, or side effects on the instance
1996 variable (such as a post-increment), may happen twice. */
1997 instance_ptr
= save_expr (instance_ptr
);
1998 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2000 else if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
2002 /* This happens when called for operator new (). */
2003 instance
= build_indirect_ref (instance
, NULL_PTR
);
2006 need_vtbl
= maybe_needed
;
2010 if (save_name
== ctor_identifier
)
2011 save_name
= TYPE_IDENTIFIER (basetype
);
2013 if (TYPE_SIZE (complete_type (basetype
)) == 0)
2015 /* This is worth complaining about, I think. */
2016 cp_error ("cannot lookup method in incomplete type `%T'", basetype
);
2017 return error_mark_node
;
2020 save_basetype
= TYPE_MAIN_VARIANT (basetype
);
2022 parmtypes
= default_parm_conversions (parms
, &last
);
2023 if (parmtypes
== error_mark_node
)
2025 return error_mark_node
;
2028 if (instance
&& IS_SIGNATURE (basetype
))
2030 /* @@ Should this be the constp/volatilep flags for the optr field
2031 of the signature pointer? */
2032 constp
= TYPE_READONLY (basetype
);
2033 volatilep
= TYPE_VOLATILE (basetype
);
2034 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2038 /* TREE_READONLY (instance) fails for references. */
2039 constp
= TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2040 volatilep
= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2041 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2045 /* Raw constructors are always in charge. */
2046 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
2047 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
2049 flags
|= LOOKUP_HAS_IN_CHARGE
;
2050 parms
= tree_cons (NULL_TREE
, integer_one_node
, parms
);
2051 parmtypes
= tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
2056 instance_ptr
= build_int_2 (0, 0);
2057 TREE_TYPE (instance_ptr
) = build_pointer_type (basetype
);
2058 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2061 parmtypes
= tree_cons (NULL_TREE
, TREE_TYPE (instance_ptr
), parmtypes
);
2063 if (last
== NULL_TREE
)
2066 /* Look up function name in the structure type definition. */
2068 /* FIXME Axe most of this now? */
2069 if ((IDENTIFIER_HAS_TYPE_VALUE (name
)
2070 && ! IDENTIFIER_OPNAME_P (name
)
2071 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name
)))
2072 || name
== constructor_name (basetype
)
2073 || name
== ctor_identifier
)
2075 tree tmp
= NULL_TREE
;
2076 if (IDENTIFIER_TYPE_VALUE (name
) == basetype
2077 || name
== constructor_name (basetype
)
2078 || name
== ctor_identifier
)
2079 tmp
= TYPE_BINFO (basetype
);
2081 tmp
= get_binfo (IDENTIFIER_TYPE_VALUE (name
), basetype
, 0);
2083 if (tmp
!= NULL_TREE
)
2085 name_kind
= "constructor";
2087 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
2088 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
2090 /* Constructors called for initialization
2091 only are never in charge. */
2094 flags
|= LOOKUP_HAS_IN_CHARGE
;
2095 tmplist
= tree_cons (NULL_TREE
, integer_zero_node
,
2096 TREE_CHAIN (parms
));
2097 TREE_CHAIN (parms
) = tmplist
;
2098 tmplist
= tree_cons (NULL_TREE
, integer_type_node
, TREE_CHAIN (parmtypes
));
2099 TREE_CHAIN (parmtypes
) = tmplist
;
2101 basetype
= BINFO_TYPE (tmp
);
2104 name_kind
= "method";
2107 name_kind
= "method";
2109 if (basetype_path
== NULL_TREE
2110 || BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (basetype
))
2111 basetype_path
= TYPE_BINFO (basetype
);
2112 result
= lookup_fnfields (basetype_path
, name
,
2113 (flags
& LOOKUP_COMPLAIN
));
2114 if (result
== error_mark_node
)
2115 return error_mark_node
;
2117 for (pass
= 0; pass
< 2; pass
++)
2119 struct candidate
*candidates
;
2120 struct candidate
*cp
;
2129 = (struct candidate
*) alloca ((ever_seen
+1)
2130 * sizeof (struct candidate
));
2131 bzero ((char *) candidates
, (ever_seen
+ 1) * sizeof (struct candidate
));
2133 len
= list_length (parms
);
2136 /* First see if a global function has a shot at it. */
2137 if (flags
& LOOKUP_GLOBAL
)
2140 tree parm
= instance_ptr
;
2142 if (TREE_CODE (TREE_TYPE (parm
)) == REFERENCE_TYPE
)
2143 parm
= convert_from_reference (parm
);
2144 else if (TREE_CODE (TREE_TYPE (parm
)) == POINTER_TYPE
)
2145 parm
= build_indirect_ref (parm
, "friendifying parms (compiler error)");
2147 my_friendly_abort (167);
2149 friend_parms
= tree_cons (NULL_TREE
, parm
, TREE_CHAIN (parms
));
2152 cp
->harshness
= (struct harshness_code
*)
2153 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2155 result
= build_overload_call_real (name
, friend_parms
, 0, cp
, 1);
2157 /* If it turns out to be the one we were actually looking for
2158 (it was probably a friend function), the return the
2160 if (TREE_CODE (result
) == CALL_EXPR
)
2163 while ((cp
->h
.code
& EVIL_CODE
) == 0)
2165 /* non-standard uses: set the field to 0 to indicate
2166 we are using a non-member function. */
2168 if (cp
->harshness
[len
].distance
== 0
2169 && cp
->h
.code
< best
)
2178 /* We have a hit (of sorts). If the parameter list is
2179 "error_mark_node", or some variant thereof, it won't
2180 match any methods. Since we have verified that the is
2181 some method vaguely matching this one (in name at least),
2184 Don't stop for friends, however. */
2185 basetype_path
= TREE_PURPOSE (baselink
);
2187 function
= TREE_VALUE (baselink
);
2188 if (TREE_CODE (basetype_path
) == TREE_LIST
)
2189 basetype_path
= TREE_VALUE (basetype_path
);
2190 basetype
= BINFO_TYPE (basetype_path
);
2192 for (; function
; function
= DECL_CHAIN (function
))
2194 #ifdef GATHER_STATISTICS
2195 n_inner_fields_searched
++;
2199 found_fns
= tree_cons (NULL_TREE
, function
, found_fns
);
2201 /* Not looking for friends here. */
2202 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
2203 && ! DECL_STATIC_FUNCTION_P (function
))
2208 tree these_parms
= parms
;
2210 #ifdef GATHER_STATISTICS
2211 n_inner_fields_searched
++;
2214 cp
->harshness
= (struct harshness_code
*)
2215 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2217 if (DECL_STATIC_FUNCTION_P (function
))
2218 these_parms
= TREE_CHAIN (these_parms
);
2219 compute_conversion_costs (function
, these_parms
, cp
, len
);
2221 if ((cp
->h
.code
& EVIL_CODE
) == 0)
2223 cp
->u
.field
= function
;
2224 cp
->function
= function
;
2225 cp
->basetypes
= basetype_path
;
2227 /* Don't allow non-converting constructors to convert. */
2228 if (flags
& LOOKUP_ONLYCONVERTING
2229 && DECL_LANG_SPECIFIC (function
)
2230 && DECL_NONCONVERTING_P (function
))
2233 /* No "two-level" conversions. */
2234 if (flags
& LOOKUP_NO_CONVERSION
2235 && (cp
->h
.code
& USER_CODE
))
2246 tree igv
= lookup_name_nonclass (name
);
2248 /* No exact match could be found. Now try to find match
2249 using default conversions. */
2250 if ((flags
& LOOKUP_GLOBAL
) && igv
)
2252 if (TREE_CODE (igv
) == FUNCTION_DECL
)
2254 else if (TREE_CODE (igv
) == TREE_LIST
)
2255 ever_seen
+= count_functions (igv
);
2260 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2261 == LOOKUP_SPECULATIVELY
)
2264 TREE_CHAIN (last
) = void_list_node
;
2265 if (flags
& LOOKUP_GLOBAL
)
2266 cp_error ("no global or member function `%D(%A)' defined",
2267 save_name
, parmtypes
);
2269 cp_error ("no member function `%T::%D(%A)' defined",
2270 save_basetype
, save_name
, TREE_CHAIN (parmtypes
));
2271 return error_mark_node
;
2276 if (cp
- candidates
!= 0)
2278 /* Rank from worst to best. Then cp will point to best one.
2279 Private fields have their bits flipped. For unsigned
2280 numbers, this should make them look very large.
2281 If the best alternate has a (signed) negative value,
2282 then all we ever saw were private members. */
2283 if (cp
- candidates
> 1)
2285 int n_candidates
= cp
- candidates
;
2286 extern int warn_synth
;
2287 TREE_VALUE (parms
) = instance_ptr
;
2288 cp
= ideal_candidate (candidates
, n_candidates
, len
);
2289 if (cp
== (struct candidate
*)0)
2291 if (flags
& LOOKUP_COMPLAIN
)
2293 TREE_CHAIN (last
) = void_list_node
;
2294 cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2295 name_kind
, save_name
, TREE_CHAIN (parmtypes
));
2296 print_n_candidates (candidates
, n_candidates
);
2298 return error_mark_node
;
2300 if (cp
->h
.code
& EVIL_CODE
)
2301 return error_mark_node
;
2303 && DECL_NAME (cp
->function
) == ansi_opname
[MODIFY_EXPR
]
2304 && DECL_ARTIFICIAL (cp
->function
)
2305 && n_candidates
== 2)
2307 cp_warning ("using synthesized `%#D' for copy assignment",
2309 cp_warning_at (" where cfront would use `%#D'",
2310 candidates
->function
);
2313 else if (cp
[-1].h
.code
& EVIL_CODE
)
2315 if (flags
& LOOKUP_COMPLAIN
)
2316 cp_error ("ambiguous type conversion requested for %s `%D'",
2317 name_kind
, save_name
);
2318 return error_mark_node
;
2323 /* The global function was the best, so use it. */
2324 if (cp
->u
.field
== 0)
2326 /* We must convert the instance pointer into a reference type.
2327 Global overloaded functions can only either take
2328 aggregate objects (which come for free from references)
2329 or reference data types anyway. */
2330 TREE_VALUE (parms
) = copy_node (instance_ptr
);
2331 TREE_TYPE (TREE_VALUE (parms
)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2332 return build_function_call (cp
->function
, parms
);
2335 function
= cp
->function
;
2336 basetype_path
= cp
->basetypes
;
2337 if (! DECL_STATIC_FUNCTION_P (function
))
2338 TREE_VALUE (parms
) = cp
->arg
;
2339 goto found_and_maybe_warn
;
2342 if (flags
& (LOOKUP_COMPLAIN
|LOOKUP_SPECULATIVELY
))
2344 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2345 == LOOKUP_SPECULATIVELY
)
2348 if (DECL_STATIC_FUNCTION_P (cp
->function
))
2349 parms
= TREE_CHAIN (parms
);
2352 if (flags
& LOOKUP_SPECULATIVELY
)
2354 if (static_call_context
2355 && TREE_CODE (TREE_TYPE (cp
->function
)) == METHOD_TYPE
)
2356 cp_error ("object missing in call to `%D'", cp
->function
);
2357 else if (ever_seen
> 1)
2359 TREE_CHAIN (last
) = void_list_node
;
2360 cp_error ("no matching function for call to `%T::%D (%A)%V'",
2361 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr
))),
2362 save_name
, TREE_CHAIN (parmtypes
),
2363 TREE_TYPE (TREE_TYPE (instance_ptr
)));
2364 TREE_CHAIN (last
) = NULL_TREE
;
2365 print_candidates (found_fns
);
2368 report_type_mismatch (cp
, parms
, name_kind
);
2369 return error_mark_node
;
2372 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2375 cp_error ("%T has no method named %D", save_basetype
, save_name
);
2376 return error_mark_node
;
2382 found_and_maybe_warn
:
2383 if ((cp
->harshness
[0].code
& CONST_CODE
)
2384 /* 12.1p2: Constructors can be called for const objects. */
2385 && ! DECL_CONSTRUCTOR_P (cp
->function
))
2387 if (flags
& LOOKUP_COMPLAIN
)
2389 cp_error_at ("non-const member function `%D'", cp
->function
);
2390 error ("called for const object at this point in file");
2392 /* Not good enough for a match. */
2394 return error_mark_node
;
2398 /* Silently return error_mark_node. */
2399 return error_mark_node
;
2402 if (flags
& LOOKUP_PROTECT
)
2403 access
= compute_access (basetype_path
, function
);
2405 if (access
== access_private_node
)
2407 if (flags
& LOOKUP_COMPLAIN
)
2409 cp_error_at ("%s `%+#D' is %s", name_kind
, function
,
2410 TREE_PRIVATE (function
) ? "private"
2411 : "from private base class");
2412 error ("within this context");
2414 return error_mark_node
;
2416 else if (access
== access_protected_node
)
2418 if (flags
& LOOKUP_COMPLAIN
)
2420 cp_error_at ("%s `%+#D' %s", name_kind
, function
,
2421 TREE_PROTECTED (function
) ? "is protected"
2422 : "has protected accessibility");
2423 error ("within this context");
2425 return error_mark_node
;
2428 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2429 type (if it exists) is a pointer to. */
2431 if (DECL_ABSTRACT_VIRTUAL_P (function
)
2432 && instance
== current_class_ref
2433 && DECL_CONSTRUCTOR_P (current_function_decl
)
2434 && ! (flags
& LOOKUP_NONVIRTUAL
)
2435 && value_member (function
, get_abstract_virtuals (basetype
)))
2436 cp_error ("abstract virtual `%#D' called from constructor", function
);
2438 if (IS_SIGNATURE (basetype
))
2440 if (static_call_context
)
2442 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2443 basetype
, save_name
);
2444 return error_mark_node
;
2446 return build_signature_method_call (basetype
, instance
, function
, parms
);
2449 function
= DECL_MAIN_VARIANT (function
);
2450 mark_used (function
);
2452 /* Is it a synthesized method that needs to be synthesized? */
2453 if (DECL_ARTIFICIAL (function
) && ! DECL_INITIAL (function
)
2454 /* Kludge: don't synthesize for default args. */
2455 && current_function_decl
)
2456 synthesize_method (function
);
2458 if (pedantic
&& DECL_THIS_INLINE (function
) && ! DECL_ARTIFICIAL (function
)
2459 && ! DECL_INITIAL (function
) && ! DECL_PENDING_INLINE_INFO (function
)
2460 && ! (DECL_TEMPLATE_INFO (function
)
2461 && TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (function
))))
2462 cp_warning ("inline function `%#D' called before definition", function
);
2464 fntype
= TREE_TYPE (function
);
2465 if (TREE_CODE (fntype
) == POINTER_TYPE
)
2466 fntype
= TREE_TYPE (fntype
);
2467 basetype
= DECL_CLASS_CONTEXT (function
);
2469 /* If we are referencing a virtual function from an object
2470 of effectively static type, then there is no need
2471 to go through the virtual function table. */
2472 if (need_vtbl
== maybe_needed
)
2474 int fixed_type
= resolves_to_fixed_type_p (instance
, 0);
2476 if (all_virtual
== 1
2477 && DECL_VINDEX (function
)
2478 && may_be_remote (basetype
))
2480 else if (DECL_VINDEX (function
))
2481 need_vtbl
= fixed_type
? unneeded
: needed
;
2483 need_vtbl
= not_needed
;
2486 if (TREE_CODE (fntype
) == METHOD_TYPE
&& static_call_context
2487 && !DECL_CONSTRUCTOR_P (function
))
2489 /* Let's be nasty to the user now, and give reasonable
2491 instance_ptr
= current_class_ptr
;
2494 if (basetype
!= current_class_type
)
2496 if (basetype
== error_mark_node
)
2497 return error_mark_node
;
2500 if (orig_basetype
!= NULL_TREE
)
2501 error_not_base_type (orig_basetype
, current_class_type
);
2503 error_not_base_type (function
, current_class_type
);
2504 return error_mark_node
;
2508 /* Only allow a static member function to call another static member
2510 else if (DECL_LANG_SPECIFIC (function
)
2511 && !DECL_STATIC_FUNCTION_P (function
))
2513 cp_error ("cannot call member function `%D' without object",
2515 return error_mark_node
;
2519 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2521 if (TYPE_SIZE (complete_type (value_type
)) == 0)
2523 if (flags
& LOOKUP_COMPLAIN
)
2524 incomplete_type_error (0, value_type
);
2525 return error_mark_node
;
2528 if (DECL_STATIC_FUNCTION_P (function
))
2529 parms
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2530 TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
);
2531 else if (need_vtbl
== unneeded
)
2533 int sub_flags
= DECL_CONSTRUCTOR_P (function
) ? flags
: LOOKUP_NORMAL
;
2534 basetype
= TREE_TYPE (instance
);
2535 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function
))
2536 != TYPE_MAIN_VARIANT (basetype
))
2538 basetype
= DECL_CLASS_CONTEXT (function
);
2539 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
2540 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2542 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2543 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, sub_flags
));
2547 if ((flags
& LOOKUP_NONVIRTUAL
) == 0)
2548 basetype
= DECL_CONTEXT (function
);
2550 /* First parm could be integer_zerop with casts like
2551 ((Object*)0)->Object::IsA() */
2552 if (!integer_zerop (TREE_VALUE (parms
)))
2554 /* Since we can't have inheritance with a union, doing get_binfo
2555 on it won't work. We do all the convert_pointer_to_real
2556 stuff to handle MI correctly...for unions, that's not
2557 an issue, so we must short-circuit that extra work here. */
2558 tree tmp
= TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)));
2559 if (tmp
!= NULL_TREE
&& TREE_CODE (tmp
) == UNION_TYPE
)
2560 instance_ptr
= TREE_VALUE (parms
);
2563 tree binfo
= get_binfo (basetype
,
2564 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
))),
2566 instance_ptr
= convert_pointer_to_real (binfo
, TREE_VALUE (parms
));
2569 = convert_pointer_to (build_type_variant (basetype
,
2573 if (TREE_CODE (instance_ptr
) == COND_EXPR
)
2575 instance_ptr
= save_expr (instance_ptr
);
2576 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2578 else if (TREE_CODE (instance_ptr
) == NOP_EXPR
2579 && TREE_CODE (TREE_OPERAND (instance_ptr
, 0)) == ADDR_EXPR
2580 && TREE_OPERAND (TREE_OPERAND (instance_ptr
, 0), 0) == instance
)
2582 /* The call to `convert_pointer_to' may return error_mark_node. */
2583 else if (TREE_CODE (instance_ptr
) == ERROR_MARK
)
2584 return instance_ptr
;
2585 else if (instance
== NULL_TREE
2586 || TREE_CODE (instance
) != INDIRECT_REF
2587 || TREE_OPERAND (instance
, 0) != instance_ptr
)
2588 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2590 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2591 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
));
2594 if (parms
== error_mark_node
2595 || (parms
&& TREE_CHAIN (parms
) == error_mark_node
))
2596 return error_mark_node
;
2598 if (need_vtbl
== needed
)
2600 function
= build_vfn_ref (&TREE_VALUE (parms
), instance
,
2601 DECL_VINDEX (function
));
2602 TREE_TYPE (function
) = build_pointer_type (fntype
);
2605 if (TREE_CODE (function
) == FUNCTION_DECL
)
2606 GNU_xref_call (current_function_decl
,
2607 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)));
2609 result
= build_call (function
, value_type
, parms
);
2610 result
= convert_from_reference (result
);
2614 /* Similar to `build_method_call', but for overloaded non-member functions.
2615 The name of this function comes through NAME. The name depends
2618 Note that this function must handle simple `C' promotions,
2619 as well as variable numbers of arguments (...), and
2620 default arguments to boot.
2622 If the overloading is successful, we return a tree node which
2623 contains the call to the function.
2625 If overloading produces candidates which are probable, but not definite,
2626 we hold these candidates. If FINAL_CP is non-zero, then we are free
2627 to assume that final_cp points to enough storage for all candidates that
2628 this function might generate. The `harshness' array is preallocated for
2629 the first candidate, but not for subsequent ones.
2631 Note that the DECL_RTL of FUNCTION must be made to agree with this
2632 function's new name. */
2635 build_overload_call_real (fnname
, parms
, flags
, final_cp
, require_complete
)
2638 struct candidate
*final_cp
;
2639 int require_complete
;
2641 /* must check for overloading here */
2642 tree functions
, function
, parm
;
2643 tree parmtypes
, last
;
2644 register tree outer
;
2646 int parmlength
= list_length (parms
);
2648 struct candidate
*candidates
, *cp
;
2652 final_cp
[0].h
.code
= 0;
2653 final_cp
[0].h
.distance
= 0;
2654 final_cp
[0].function
= 0;
2656 final_cp
[1].h
.code
= EVIL_CODE
;
2659 parmtypes
= default_parm_conversions (parms
, &last
);
2660 if (parmtypes
== error_mark_node
)
2663 final_cp
->h
.code
= EVIL_CODE
;
2664 return error_mark_node
;
2668 TREE_CHAIN (last
) = void_list_node
;
2670 parmtypes
= void_list_node
;
2672 if (is_overloaded_fn (fnname
))
2675 if (TREE_CODE (fnname
) == TREE_LIST
)
2676 fnname
= TREE_PURPOSE (functions
);
2677 else if (TREE_CODE (fnname
) == FUNCTION_DECL
)
2678 fnname
= DECL_NAME (functions
);
2681 functions
= lookup_name_nonclass (fnname
);
2683 if (functions
== NULL_TREE
)
2685 if (flags
& LOOKUP_SPECULATIVELY
)
2687 if (flags
& LOOKUP_COMPLAIN
)
2688 error ("only member functions apply");
2690 final_cp
->h
.code
= EVIL_CODE
;
2691 return error_mark_node
;
2694 if (TREE_CODE (functions
) == FUNCTION_DECL
&& ! IDENTIFIER_OPNAME_P (fnname
))
2696 functions
= DECL_MAIN_VARIANT (functions
);
2699 /* We are just curious whether this is a viable alternative or
2701 compute_conversion_costs (functions
, parms
, final_cp
, parmlength
);
2705 return build_function_call_real (functions
, parms
, 1, flags
);
2708 if (TREE_CODE (functions
) == TREE_LIST
2709 && TREE_VALUE (functions
) == NULL_TREE
)
2711 if (flags
& LOOKUP_SPECULATIVELY
)
2714 if (flags
& LOOKUP_COMPLAIN
)
2715 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2716 TREE_PURPOSE (functions
));
2718 final_cp
->h
.code
= EVIL_CODE
;
2719 return error_mark_node
;
2722 length
= count_functions (functions
);
2725 candidates
= final_cp
;
2729 = (struct candidate
*)alloca ((length
+1) * sizeof (struct candidate
));
2730 bzero ((char *) candidates
, (length
+ 1) * sizeof (struct candidate
));
2735 my_friendly_assert (is_overloaded_fn (functions
), 169);
2737 functions
= get_first_fn (functions
);
2739 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2740 for (outer
= functions
; outer
; outer
= DECL_CHAIN (outer
))
2742 int template_cost
= 0;
2744 if (TREE_CODE (function
) != FUNCTION_DECL
2745 && ! (TREE_CODE (function
) == TEMPLATE_DECL
2746 && TREE_CODE (DECL_TEMPLATE_RESULT (function
)) == FUNCTION_DECL
))
2748 enum tree_code code
= TREE_CODE (function
);
2749 if (code
== TEMPLATE_DECL
)
2750 code
= TREE_CODE (DECL_TEMPLATE_RESULT (function
));
2751 if (code
== CONST_DECL
)
2753 ("enumeral value `%D' conflicts with function of same name",
2755 else if (code
== VAR_DECL
)
2757 if (TREE_STATIC (function
))
2759 ("variable `%D' conflicts with function of same name",
2763 ("constant field `%D' conflicts with function of same name",
2766 else if (code
== TYPE_DECL
)
2769 my_friendly_abort (2);
2770 error ("at this point in file");
2773 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2775 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function
));
2776 tree
*targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2779 i
= type_unification (DECL_TEMPLATE_PARMS (function
), targs
,
2780 TYPE_ARG_TYPES (TREE_TYPE (function
)),
2781 parms
, &template_cost
, 0);
2784 function
= instantiate_template (function
, targs
);
2785 if (function
== error_mark_node
)
2790 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2792 /* Unconverted template -- failed match. */
2793 cp
->function
= function
;
2795 cp
->h
.code
= EVIL_CODE
;
2799 struct candidate
*cp2
;
2801 /* Check that this decl is not the same as a function that's in
2802 the list due to some template instantiation. */
2805 if (cp2
->function
== function
)
2809 if (cp2
->function
== function
)
2812 function
= DECL_MAIN_VARIANT (function
);
2814 /* Can't use alloca here, since result might be
2815 passed to calling function. */
2816 cp
->h_len
= parmlength
;
2817 cp
->harshness
= (struct harshness_code
*)
2818 oballoc ((parmlength
+ 1) * sizeof (struct harshness_code
));
2820 compute_conversion_costs (function
, parms
, cp
, parmlength
);
2822 /* Make sure this is clear as well. */
2823 cp
->h
.int_penalty
+= template_cost
;
2825 if ((cp
[0].h
.code
& EVIL_CODE
) == 0)
2827 cp
[1].h
.code
= EVIL_CODE
;
2833 if (cp
- candidates
)
2835 tree rval
= error_mark_node
;
2838 cp
[0].h
.code
= EVIL_CODE
;
2839 if (cp
- candidates
> 1)
2841 struct candidate
*best_cp
2842 = ideal_candidate (candidates
, cp
- candidates
, parmlength
);
2843 if (best_cp
== (struct candidate
*)0)
2845 if (flags
& LOOKUP_COMPLAIN
)
2847 cp_error ("call of overloaded `%D' is ambiguous", fnname
);
2848 print_n_candidates (candidates
, cp
- candidates
);
2850 return error_mark_node
;
2853 rval
= best_cp
->function
;
2858 if (cp
->h
.code
& EVIL_CODE
)
2860 if (flags
& LOOKUP_COMPLAIN
)
2861 error ("type conversion ambiguous");
2864 rval
= cp
->function
;
2870 return build_function_call_real (rval
, parms
, require_complete
, flags
);
2873 if (flags
& LOOKUP_SPECULATIVELY
)
2876 if (flags
& LOOKUP_COMPLAIN
)
2877 report_type_mismatch (cp
, parms
, "function",
2878 decl_as_string (cp
->function
, 1));
2880 return error_mark_node
;
2883 /* This requires a complete type on the result of the call. */
2885 build_overload_call (fnname
, parms
, flags
)
2889 return build_overload_call_real (fnname
, parms
, flags
, (struct candidate
*)0, 1);