1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 hacked by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* High-level class interface. */
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
38 extern int inhibit_warnings
;
39 extern int flag_assume_nonnull_objects
;
40 extern tree ctor_label
, dtor_label
;
43 extern tree
unary_complex_lvalue ();
45 /* Compute the ease with which a conversion can be performed
46 between an expected and the given type. */
47 static struct harshness_code
convert_harshness ();
49 #define EVIL_RETURN(ARG) ((ARG).code = EVIL_CODE, (ARG))
50 #define STD_RETURN(ARG) ((ARG).code = STD_CODE, (ARG))
51 #define QUAL_RETURN(ARG) ((ARG).code = QUAL_CODE, (ARG))
52 #define TRIVIAL_RETURN(ARG) ((ARG).code = TRIVIAL_CODE, (ARG))
53 #define ZERO_RETURN(ARG) ((ARG).code = 0, (ARG))
55 /* Ordering function for overload resolution. Compare two candidates
58 rank_for_overload (x
, y
)
59 struct candidate
*x
, *y
;
61 if (y
->h
.code
& (EVIL_CODE
|ELLIPSIS_CODE
|USER_CODE
))
62 return y
->h
.code
- x
->h
.code
;
63 if (x
->h
.code
& (EVIL_CODE
|ELLIPSIS_CODE
|USER_CODE
))
66 /* This is set by compute_conversion_costs, for calling a non-const
67 member function from a const member function. */
68 if ((y
->harshness
[0].code
& CONST_CODE
) ^ (x
->harshness
[0].code
& CONST_CODE
))
69 return y
->harshness
[0].code
- x
->harshness
[0].code
;
71 if (y
->h
.code
& STD_CODE
)
73 if (x
->h
.code
& STD_CODE
)
74 return y
->h
.distance
- x
->h
.distance
;
77 if (x
->h
.code
& STD_CODE
)
80 return y
->h
.code
- x
->h
.code
;
83 /* Compare two candidates, argument by argument. */
86 struct candidate
*x
, *y
;
90 if (x
->h_len
!= y
->h_len
)
93 for (i
= 0; i
< x
->h_len
; i
++)
95 if (y
->harshness
[i
].code
- x
->harshness
[i
].code
)
96 return y
->harshness
[i
].code
- x
->harshness
[i
].code
;
97 if ((y
->harshness
[i
].code
& STD_CODE
)
98 && (y
->harshness
[i
].distance
- x
->harshness
[i
].distance
))
99 return y
->harshness
[i
].distance
- x
->harshness
[i
].distance
;
101 /* They're both the same code. Now see if we're dealing with an
102 integral promotion that needs a finer grain of accuracy. */
103 if (y
->harshness
[0].code
& PROMO_CODE
104 && (y
->harshness
[i
].int_penalty
^ x
->harshness
[i
].int_penalty
))
105 return y
->harshness
[i
].int_penalty
- x
->harshness
[i
].int_penalty
;
110 /* TYPE is the type we wish to convert to. PARM is the parameter
111 we have to work with. We use a somewhat arbitrary cost function
112 to measure this conversion. */
113 static struct harshness_code
114 convert_harshness (type
, parmtype
, parm
)
115 register tree type
, parmtype
;
118 struct harshness_code h
;
119 register enum tree_code codel
;
120 register enum tree_code coder
;
127 #ifdef GATHER_STATISTICS
128 n_convert_harshness
++;
131 if (TYPE_PTRMEMFUNC_P (type
))
132 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
133 if (TYPE_PTRMEMFUNC_P (parmtype
))
134 parmtype
= TYPE_PTRMEMFUNC_FN_TYPE (parmtype
);
136 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
)
139 parm
= convert_from_reference (parm
);
140 parmtype
= TREE_TYPE (parmtype
);
144 lvalue
= lvalue_p (parm
);
148 codel
= TREE_CODE (type
);
149 coder
= TREE_CODE (parmtype
);
151 if (TYPE_MAIN_VARIANT (parmtype
) == TYPE_MAIN_VARIANT (type
))
152 return ZERO_RETURN (h
);
154 if (coder
== ERROR_MARK
)
155 return EVIL_RETURN (h
);
157 if (codel
== POINTER_TYPE
&& fntype_p (parmtype
))
160 struct harshness_code h1
, h2
;
162 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
163 type
= TREE_TYPE (type
);
165 if (coder
== POINTER_TYPE
)
167 parmtype
= TREE_TYPE (parmtype
);
168 coder
= TREE_CODE (parmtype
);
171 if (coder
!= TREE_CODE (type
))
172 return EVIL_RETURN (h
);
174 /* We allow the default conversion between function type
175 and pointer-to-function type for free. */
176 if (type
== parmtype
)
177 return ZERO_RETURN (h
);
180 return EVIL_RETURN (h
);
182 /* Compare return types. */
183 p1
= TREE_TYPE (type
);
184 p2
= TREE_TYPE (parmtype
);
185 h2
= convert_harshness (p1
, p2
, NULL_TREE
);
186 if (h2
.code
& EVIL_CODE
)
189 h1
.code
= TRIVIAL_CODE
;
192 if (h2
.distance
!= 0)
196 /* This only works for pointers. */
197 if (TREE_CODE (p1
) != POINTER_TYPE
198 && TREE_CODE (p1
) != REFERENCE_TYPE
)
199 return EVIL_RETURN (h
);
203 /* Don't die if we happen to be dealing with void*. */
204 if (!IS_AGGR_TYPE (p1
) || !IS_AGGR_TYPE (p2
))
205 return EVIL_RETURN (h
);
207 binfo
= get_binfo (p2
, p1
, 0);
209 binfo
= get_binfo (p1
, p2
, 0);
211 if (! BINFO_OFFSET_ZEROP (binfo
))
214 static int explained
= 0;
216 message_2_types (sorry
, "cannot cast `%s' to `%s' at function call site", p2
, p1
);
218 message_2_types (sorry
, "cannot cast `%s' to `%s' at function call site", p1
, p2
);
221 sorry ("(because pointer values change during conversion)");
223 return EVIL_RETURN (h
);
228 if (h2
.distance
> h1
.distance
)
229 h1
.distance
= h2
.distance
;
231 p1
= TYPE_ARG_TYPES (type
);
232 p2
= TYPE_ARG_TYPES (parmtype
);
233 while (p1
&& TREE_VALUE (p1
) != void_type_node
234 && p2
&& TREE_VALUE (p2
) != void_type_node
)
236 h2
= convert_harshness (TREE_VALUE (p1
), TREE_VALUE (p2
),
238 if (h2
.code
& EVIL_CODE
)
243 /* This only works for pointers and references. */
244 if (TREE_CODE (TREE_VALUE (p1
)) != POINTER_TYPE
245 && TREE_CODE (TREE_VALUE (p1
)) != REFERENCE_TYPE
)
246 return EVIL_RETURN (h
);
247 h2
.distance
= - h2
.distance
;
251 if (h2
.distance
> h1
.distance
)
252 h1
.distance
= h2
.distance
;
253 p1
= TREE_CHAIN (p1
);
254 p2
= TREE_CHAIN (p2
);
261 return EVIL_RETURN (h
);
262 h1
.code
|= ELLIPSIS_CODE
;
267 if (TREE_PURPOSE (p1
) == NULL_TREE
)
268 h1
.code
|= EVIL_CODE
;
272 else if (codel
== POINTER_TYPE
&& coder
== OFFSET_TYPE
)
274 /* Get to the OFFSET_TYPE that this might be. */
275 type
= TREE_TYPE (type
);
277 if (coder
!= TREE_CODE (type
))
278 return EVIL_RETURN (h
);
280 if (TYPE_OFFSET_BASETYPE (type
) == TYPE_OFFSET_BASETYPE (parmtype
))
282 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (type
),
283 TYPE_OFFSET_BASETYPE (parmtype
)))
288 else if (UNIQUELY_DERIVED_FROM_P (TYPE_OFFSET_BASETYPE (parmtype
),
289 TYPE_OFFSET_BASETYPE (type
)))
295 return EVIL_RETURN (h
);
296 /* Now test the OFFSET_TYPE's target compatibility. */
297 type
= TREE_TYPE (type
);
298 parmtype
= TREE_TYPE (parmtype
);
301 if (coder
== UNKNOWN_TYPE
)
303 if (codel
== FUNCTION_TYPE
304 || codel
== METHOD_TYPE
305 || (codel
== POINTER_TYPE
306 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
307 || TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)))
308 return TRIVIAL_RETURN (h
);
309 return EVIL_RETURN (h
);
312 if (coder
== VOID_TYPE
)
313 return EVIL_RETURN (h
);
315 if (codel
== BOOLEAN_TYPE
)
317 if (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
)
318 return STD_RETURN (h
);
319 else if (coder
== POINTER_TYPE
|| coder
== OFFSET_TYPE
)
321 /* Make this worse than any conversion to another pointer.
322 FIXME this is how I think the language should work, but it may not
323 end up being how the language is standardized (jason 1/30/95). */
325 return STD_RETURN (h
);
327 return EVIL_RETURN (h
);
330 if (INTEGRAL_CODE_P (codel
))
332 /* Control equivalence of ints an enums. */
334 if (codel
== ENUMERAL_TYPE
335 && flag_int_enum_equivalence
== 0)
337 /* Enums can be converted to ints, but not vice-versa. */
338 if (coder
!= ENUMERAL_TYPE
339 || TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (parmtype
))
340 return EVIL_RETURN (h
);
343 /* else enums and ints (almost) freely interconvert. */
345 if (INTEGRAL_CODE_P (coder
))
347 if (TYPE_MAIN_VARIANT (type
)
348 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
351 #if 0 /* What purpose does this serve? -jason */
352 /* A char, short, wchar_t, etc., should promote to an int if
353 it can handle it, otherwise to an unsigned. So we'll make
355 if (type
!= integer_type_node
)
364 else if (coder
== REAL_TYPE
)
372 if (codel
== REAL_TYPE
)
374 if (coder
== REAL_TYPE
)
376 if (TYPE_MAIN_VARIANT (type
)
377 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
384 else if (INTEGRAL_CODE_P (coder
))
392 /* Convert arrays which have not previously been converted. */
393 if (codel
== ARRAY_TYPE
)
394 codel
= POINTER_TYPE
;
395 if (coder
== ARRAY_TYPE
)
396 coder
= POINTER_TYPE
;
398 /* Conversions among pointers */
399 if (codel
== POINTER_TYPE
&& coder
== POINTER_TYPE
)
401 register tree ttl
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
402 register tree ttr
= TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
));
403 int penalty
= 4 * (ttl
!= ttr
);
405 /* Anything converts to void *. Since this may be `const void *'
406 (etc.) use VOID_TYPE instead of void_type_node. Otherwise, the
407 targets must be the same, except that we do allow (at some cost)
408 conversion between signed and unsigned pointer types. */
410 if ((TREE_CODE (ttl
) == METHOD_TYPE
411 || TREE_CODE (ttl
) == FUNCTION_TYPE
)
412 && TREE_CODE (ttl
) == TREE_CODE (ttr
))
414 if (comptypes (ttl
, ttr
, -1))
416 h
.code
= penalty
? STD_CODE
: 0;
425 if (TREE_CODE (ttl
) != VOID_TYPE
426 && (TREE_CODE (ttr
) != VOID_TYPE
|| !parm
|| !integer_zerop (parm
)))
428 if (TREE_UNSIGNED (ttl
) != TREE_UNSIGNED (ttr
))
430 ttl
= unsigned_type (ttl
);
431 ttr
= unsigned_type (ttr
);
434 if (comp_target_types (ttl
, ttr
, 0) <= 0)
435 return EVIL_RETURN (h
);
438 if (!(TREE_CODE (ttl
) == VOID_TYPE
439 || TREE_CODE (ttr
) == VOID_TYPE
440 || (TREE_UNSIGNED (ttl
) ^ TREE_UNSIGNED (ttr
)
441 && (ttl
= unsigned_type (ttl
),
442 ttr
= unsigned_type (ttr
),
444 || (comp_target_types (ttl
, ttr
, 0) > 0)))
445 return EVIL_RETURN (h
);
448 if (penalty
== 10 || ttr
== ttl
)
450 tree tmp1
= TREE_TYPE (type
), tmp2
= TREE_TYPE (parmtype
);
452 /* If one was unsigned but the other wasn't, then we need to
453 do a standard conversion from T to unsigned T. */
455 h
.code
= PROMO_CODE
; /* was STD_CODE */
459 /* Note conversion from `T*' to `const T*',
460 or `T*' to `volatile T*'. */
462 && ((TYPE_READONLY (tmp1
) != TREE_READONLY (tmp2
))
463 || (TYPE_VOLATILE (tmp1
) != TYPE_VOLATILE (tmp2
))))
471 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
473 int b_or_d
= get_base_distance (ttl
, ttr
, 0, 0);
476 b_or_d
= get_base_distance (ttr
, ttl
, 0, 0);
478 return EVIL_RETURN (h
);
479 h
.distance
= -b_or_d
;
487 /* If converting from a `class*' to a `void*', make it
488 less favorable than any inheritance relationship. */
489 if (TREE_CODE (ttl
) == VOID_TYPE
&& IS_AGGR_TYPE (ttr
))
492 h
.distance
= CLASSTYPE_MAX_DEPTH (ttr
)+1;
495 h
.code
= penalty
? STD_CODE
: PROMO_CODE
;
499 if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
501 /* This is not a bad match, but don't let it beat
502 integer-enum combinations. */
503 if (parm
&& integer_zerop (parm
))
511 /* C++: Since the `this' parameter of a signature member function
512 is represented as a signature pointer to handle default implementations
513 correctly, we can have the case that `type' is a signature pointer
514 while `parmtype' is a pointer to a signature table. We don't really
515 do any conversions in this case, so just return 0. */
517 if (codel
== RECORD_TYPE
&& coder
== POINTER_TYPE
518 && IS_SIGNATURE_POINTER (type
) && IS_SIGNATURE (TREE_TYPE (parmtype
)))
519 return ZERO_RETURN (h
);
521 if (codel
== REFERENCE_TYPE
)
524 int constp
= parm
? TREE_READONLY (parm
) : TYPE_READONLY (parmtype
);
525 int volatilep
= (parm
? TREE_THIS_VOLATILE (parm
)
526 : TYPE_VOLATILE (parmtype
));
527 register tree intype
= TYPE_MAIN_VARIANT (parmtype
);
528 register enum tree_code form
= TREE_CODE (intype
);
531 ttl
= TREE_TYPE (type
);
533 /* Only allow const reference binding if we were given a parm to deal
534 with, since it isn't really a conversion. This is a hack to
535 prevent build_type_conversion from finding this conversion, but
536 still allow overloading to find it. */
537 if (! lvalue
&& ! (parm
&& TYPE_READONLY (ttl
)))
538 return EVIL_RETURN (h
);
540 if (TYPE_READONLY (ttl
) < constp
541 || TYPE_VOLATILE (ttl
) < volatilep
)
542 return EVIL_RETURN (h
);
544 /* When passing a non-const argument into a const reference, dig it a
545 little, so a non-const reference is preferred over this one. */
546 penalty
= ((TYPE_READONLY (ttl
) > constp
)
547 + (TYPE_VOLATILE (ttl
) > volatilep
));
549 ttl
= TYPE_MAIN_VARIANT (ttl
);
551 if (form
== OFFSET_TYPE
)
553 intype
= TREE_TYPE (intype
);
554 form
= TREE_CODE (intype
);
559 /* Maybe handle conversion to base here? */
561 h
= convert_harshness (ttl
, ttr
, NULL_TREE
);
562 if (penalty
&& h
.code
== 0)
565 h
.int_penalty
= penalty
;
569 if (codel
== RECORD_TYPE
&& coder
== RECORD_TYPE
)
571 int b_or_d
= get_base_distance (type
, parmtype
, 0, 0);
574 b_or_d
= get_base_distance (parmtype
, type
, 0, 0);
576 return EVIL_RETURN (h
);
577 h
.distance
= -b_or_d
;
584 return EVIL_RETURN (h
);
588 can_convert (to
, from
)
591 struct harshness_code h
;
592 h
= convert_harshness (to
, from
, NULL_TREE
);
593 return h
.code
< USER_CODE
;
597 can_convert_arg (to
, from
, arg
)
600 struct harshness_code h
;
601 h
= convert_harshness (to
, from
, arg
);
602 return h
.code
< USER_CODE
;
605 #ifdef DEBUG_MATCHING
608 struct harshness_code
*h
;
610 static char buf
[1024];
613 bzero (buf
, 1024 * sizeof (char));
614 strcat (buf
, "codes=[");
615 if (h
->code
& EVIL_CODE
)
616 strcat (buf
, "EVIL");
617 if (h
->code
& CONST_CODE
)
618 strcat (buf
, " CONST");
619 if (h
->code
& ELLIPSIS_CODE
)
620 strcat (buf
, " ELLIPSIS");
621 if (h
->code
& USER_CODE
)
622 strcat (buf
, " USER");
623 if (h
->code
& STD_CODE
)
624 strcat (buf
, " STD");
625 if (h
->code
& PROMO_CODE
)
626 strcat (buf
, " PROMO");
627 if (h
->code
& QUAL_CODE
)
628 strcat (buf
, " QUAL");
629 if (h
->code
& TRIVIAL_CODE
)
630 strcat (buf
, " TRIVIAL");
634 sprintf (tmp
, "] distance=%d int_penalty=%d", h
->distance
, h
->int_penalty
);
642 /* Algorithm: For each argument, calculate how difficult it is to
643 make FUNCTION accept that argument. If we can easily tell that
644 FUNCTION won't be acceptable to one of the arguments, then we
645 don't need to compute the ease of converting the other arguments,
646 since it will never show up in the intersection of all arguments'
649 Conversions between builtin and user-defined types are allowed, but
650 no function involving such a conversion is preferred to one which
651 does not require such a conversion. Furthermore, such conversions
655 compute_conversion_costs (function
, tta_in
, cp
, arglen
)
658 struct candidate
*cp
;
661 tree ttf_in
= TYPE_ARG_TYPES (TREE_TYPE (function
));
665 /* Start out with no strikes against. */
666 int evil_strikes
= 0;
667 int ellipsis_strikes
= 0;
668 int user_strikes
= 0;
669 int b_or_d_strikes
= 0;
670 int easy_strikes
= 0;
672 int strike_index
= 0, win
;
673 struct harshness_code lose
;
674 extern int cp_silent
;
676 #ifdef GATHER_STATISTICS
677 n_compute_conversion_costs
++;
680 #ifndef DEBUG_MATCHING
681 /* We don't emit any warnings or errors while trying out each candidate. */
685 cp
->function
= function
;
686 cp
->arg
= tta
? TREE_VALUE (tta
) : NULL_TREE
;
687 cp
->u
.bad_arg
= 0; /* optimistic! */
691 cp
->h
.int_penalty
= 0;
692 bzero ((char *) cp
->harshness
,
693 (cp
->h_len
+ 1) * sizeof (struct harshness_code
));
697 struct harshness_code h
;
699 if (ttf
== void_list_node
)
702 if (type_unknown_p (TREE_VALUE (tta
)))
704 /* Must perform some instantiation here. */
705 tree rhs
= TREE_VALUE (tta
);
706 tree lhstype
= TREE_VALUE (ttf
);
708 /* Keep quiet about possible contravariance violations. */
709 int old_inhibit_warnings
= inhibit_warnings
;
710 inhibit_warnings
= 1;
712 /* @@ This is to undo what `grokdeclarator' does to
713 parameter types. It really should go through
714 something more general. */
716 TREE_TYPE (tta
) = unknown_type_node
;
717 rhs
= instantiate_type (lhstype
, rhs
, 0);
718 inhibit_warnings
= old_inhibit_warnings
;
720 if (TREE_CODE (rhs
) == ERROR_MARK
)
723 h
= convert_harshness (lhstype
, TREE_TYPE (rhs
), rhs
);
727 #ifdef DEBUG_MATCHING
728 static tree old_function
= NULL_TREE
;
730 if (!old_function
|| function
!= old_function
)
732 cp_error ("trying %D", function
);
733 old_function
= function
;
736 cp_error (" doing (%T) %E against arg %T",
737 TREE_TYPE (TREE_VALUE (tta
)), TREE_VALUE (tta
),
741 h
= convert_harshness (TREE_VALUE (ttf
),
742 TREE_TYPE (TREE_VALUE (tta
)),
745 #ifdef DEBUG_MATCHING
746 cp_error (" evaluated %s", print_harshness (&h
));
750 cp
->harshness
[strike_index
] = h
;
751 if ((h
.code
& EVIL_CODE
)
752 || ((h
.code
& STD_CODE
) && h
.distance
< 0))
754 cp
->u
.bad_arg
= strike_index
;
757 else if (h
.code
& ELLIPSIS_CODE
)
758 ellipsis_strikes
+= 1;
760 /* This is never set by `convert_harshness'. */
761 else if (h
.code
& USER_CODE
)
768 if ((h
.code
& STD_CODE
) && h
.distance
)
770 if (h
.distance
> b_or_d_strikes
)
771 b_or_d_strikes
= h
.distance
;
774 easy_strikes
+= (h
.code
& (STD_CODE
|PROMO_CODE
|TRIVIAL_CODE
));
775 cp
->h
.code
|= h
.code
;
776 /* Make sure we communicate this. */
777 cp
->h
.int_penalty
+= h
.int_penalty
;
780 ttf
= TREE_CHAIN (ttf
);
781 tta
= TREE_CHAIN (tta
);
787 /* ran out of formals, and parmlist is fixed size. */
788 if (ttf
/* == void_type_node */)
790 cp
->h
.code
= EVIL_CODE
;
797 struct harshness_code h
;
798 int l
= list_length (tta
);
799 ellipsis_strikes
+= l
;
800 h
.code
= ELLIPSIS_CODE
;
804 cp
->harshness
[strike_index
++] = h
;
807 else if (ttf
&& ttf
!= void_list_node
)
809 /* ran out of actuals, and no defaults. */
810 if (TREE_PURPOSE (ttf
) == NULL_TREE
)
812 cp
->h
.code
= EVIL_CODE
;
817 /* Store index of first default. */
818 cp
->harshness
[arglen
].distance
= strike_index
+1;
821 cp
->harshness
[arglen
].distance
= 0;
823 /* Argument list lengths work out, so don't need to check them again. */
826 /* We do not check for derived->base conversions here, since in
827 no case would they give evil strike counts, unless such conversions
828 are somehow ambiguous. */
830 /* See if any user-defined conversions apply.
831 But make sure that we do not loop. */
832 static int dont_convert_types
= 0;
834 if (dont_convert_types
)
836 cp
->h
.code
= EVIL_CODE
;
841 win
= 0; /* Only get one chance to win. */
842 ttf
= TYPE_ARG_TYPES (TREE_TYPE (function
));
849 if (ttf
== void_list_node
)
852 lose
= cp
->harshness
[strike_index
];
853 if ((lose
.code
& EVIL_CODE
)
854 || ((lose
.code
& STD_CODE
) && lose
.distance
< 0))
856 tree actual_type
= TREE_TYPE (TREE_VALUE (tta
));
857 tree formal_type
= TREE_VALUE (ttf
);
858 int extra_conversions
= 0;
860 dont_convert_types
= 1;
862 if (TREE_CODE (formal_type
) == REFERENCE_TYPE
)
863 formal_type
= TREE_TYPE (formal_type
);
864 if (TREE_CODE (actual_type
) == REFERENCE_TYPE
)
865 actual_type
= TREE_TYPE (actual_type
);
867 if (formal_type
!= error_mark_node
868 && actual_type
!= error_mark_node
)
870 formal_type
= TYPE_MAIN_VARIANT (formal_type
);
871 actual_type
= TYPE_MAIN_VARIANT (actual_type
);
873 if (TYPE_HAS_CONSTRUCTOR (formal_type
))
875 /* If it has a constructor for this type,
877 /* @@ There is no way to save this result yet, so
878 success is a NULL_TREE for now. */
879 if (convert_to_aggr (formal_type
, TREE_VALUE (tta
), 0, 1)
883 if (TYPE_LANG_SPECIFIC (actual_type
)
884 && TYPE_HAS_CONVERSION (actual_type
))
887 /* Don't issue warnings since we're only groping
888 around for the right answer, we haven't yet
889 committed to going with this solution. */
890 int old_inhibit_warnings
= inhibit_warnings
;
892 inhibit_warnings
= 1;
893 conv
= build_type_conversion
894 (CALL_EXPR
, formal_type
, TREE_VALUE (tta
), 0);
895 inhibit_warnings
= old_inhibit_warnings
;
899 if (conv
== error_mark_node
900 || (TREE_CODE (TREE_VALUE (ttf
)) == REFERENCE_TYPE
901 && ! TYPE_READONLY (TREE_VALUE (TREE_VALUE (ttf
)))
902 && ! lvalue_p (conv
)))
907 if (TREE_CODE (conv
) != CALL_EXPR
)
908 extra_conversions
= 1;
913 dont_convert_types
= 0;
918 cp
->harshness
[strike_index
].code
919 = USER_CODE
| (extra_conversions
? STD_CODE
: 0);
924 if (cp
->u
.bad_arg
> strike_index
)
925 cp
->u
.bad_arg
= strike_index
;
927 evil_strikes
= win
? 2 : 1;
932 ttf
= TREE_CHAIN (ttf
);
933 tta
= TREE_CHAIN (tta
);
938 /* Const member functions get a small penalty because defaulting
939 to const is less useful than defaulting to non-const. */
940 /* This is bogus, it does not correspond to anything in the ARM.
941 This code will be fixed when this entire section is rewritten
942 to conform to the ARM. (mrs) */
943 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
945 tree this_parm
= TREE_VALUE (ttf_in
);
947 if (TREE_CODE (this_parm
) == RECORD_TYPE
/* Is `this' a sig ptr? */
948 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm
))))
949 : TYPE_READONLY (TREE_TYPE (this_parm
)))
951 cp
->harshness
[0].code
|= TRIVIAL_CODE
;
956 /* Calling a non-const member function from a const member function
957 is probably invalid, but for now we let it only draw a warning.
958 We indicate that such a mismatch has occurred by setting the
959 harshness to a maximum value. */
960 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in
))) == POINTER_TYPE
961 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in
))))))
962 cp
->harshness
[0].code
|= CONST_CODE
;
967 cp
->h
.code
= EVIL_CODE
;
968 if (ellipsis_strikes
)
969 cp
->h
.code
|= ELLIPSIS_CODE
;
971 cp
->h
.code
|= USER_CODE
;
973 #ifdef DEBUG_MATCHING
974 cp_error ("final eval %s", print_harshness (&cp
->h
));
978 /* Subroutine of ideal_candidate. See if X or Y is a better match
981 strictly_better (x
, y
)
990 if (xor >= x
|| xor >= y
)
995 /* When one of several possible overloaded functions and/or methods
996 can be called, choose the best candidate for overloading.
998 BASETYPE is the context from which we start method resolution
999 or NULL if we are comparing overloaded functions.
1000 CANDIDATES is the array of candidates we have to choose from.
1001 N_CANDIDATES is the length of CANDIDATES.
1002 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1003 choose. It is modified in place when resolving methods. It is not
1004 modified in place when resolving overloaded functions.
1005 LEN is the length of the parameter list. */
1007 static struct candidate
*
1008 ideal_candidate (basetype
, candidates
, n_candidates
, parms
, len
)
1010 struct candidate
*candidates
;
1015 struct candidate
*cp
= candidates
+n_candidates
;
1016 int i
, j
= -1, best_code
;
1018 /* For each argument, sort the functions from best to worst for the arg.
1019 For each function that's not best for this arg, set its overall
1020 harshness to EVIL so that other args won't like it. The candidate
1021 list for the last argument is the intersection of all the best-liked
1025 for (i
= 0; i
< len
; i
++)
1027 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
1029 best_code
= cp
[-1].h
.code
;
1031 /* To find out functions that are worse than that represented
1032 by BEST_CODE, we can't just do a comparison like h.code>best_code.
1033 The total harshness for the "best" fn may be 8|8 for two args, and
1034 the harshness for the next-best may be 8|2. If we just compared,
1035 that would be checking 8>10, which would lead to the next-best
1036 being disqualified. What we actually want to do is get rid
1037 of functions that are definitely worse than that represented
1038 by best_code, i.e. those which have bits set higher than the
1039 highest in best_code. Sooooo, what we do is clear out everything
1040 represented by best_code, and see if we still come up with something
1041 higher. If so (e.g., 8|8 vs 8|16), it'll disqualify it properly. */
1042 for (j
= n_candidates
-2; j
>= 0; j
--)
1043 if ((candidates
[j
].h
.code
& ~best_code
) > best_code
)
1044 candidates
[j
].h
.code
= EVIL_CODE
;
1047 if (cp
[-1].h
.code
& EVIL_CODE
)
1050 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
1052 best_code
= cp
[-1].h
.code
;
1055 /* If they're at least as good as each other, do an arg-by-arg check. */
1056 if (! strictly_better (cp
[-1].h
.code
, cp
[-2].h
.code
))
1061 for (j
= 0; j
< n_candidates
; j
++)
1062 if (! strictly_better (candidates
[j
].h
.code
, best_code
))
1065 qsort (candidates
+j
, n_candidates
-j
, sizeof (struct candidate
),
1067 for (i
= 0; i
< len
; i
++)
1069 if (cp
[-1].harshness
[i
].code
< cp
[-2].harshness
[i
].code
)
1071 else if (cp
[-1].harshness
[i
].code
> cp
[-2].harshness
[i
].code
)
1073 else if (cp
[-1].harshness
[i
].code
& STD_CODE
)
1075 /* If it involves a standard conversion, let the
1076 inheritance lattice be the final arbiter. */
1077 if (cp
[-1].harshness
[i
].distance
> cp
[-2].harshness
[i
].distance
)
1079 else if (cp
[-1].harshness
[i
].distance
< cp
[-2].harshness
[i
].distance
)
1082 else if (cp
[-1].harshness
[i
].code
& PROMO_CODE
)
1084 /* For integral promotions, take into account a finer
1085 granularity for determining which types should be favored
1086 over others in such promotions. */
1087 if (cp
[-1].harshness
[i
].int_penalty
> cp
[-2].harshness
[i
].int_penalty
)
1089 else if (cp
[-1].harshness
[i
].int_penalty
< cp
[-2].harshness
[i
].int_penalty
)
1094 if (! better
|| worse
)
1100 /* Assume that if the class referred to is not in the
1101 current class hierarchy, that it may be remote.
1102 PARENT is assumed to be of aggregate type here. */
1104 may_be_remote (parent
)
1107 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent
) == 0)
1110 if (current_class_type
== NULL_TREE
)
1113 if (parent
== current_class_type
)
1116 if (UNIQUELY_DERIVED_FROM_P (parent
, current_class_type
))
1122 build_vfield_ref (datum
, type
)
1126 int old_assume_nonnull_objects
= flag_assume_nonnull_objects
;
1128 if (datum
== error_mark_node
)
1129 return error_mark_node
;
1131 /* Vtable references are always made from non-null objects. */
1132 flag_assume_nonnull_objects
= 1;
1133 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
1134 datum
= convert_from_reference (datum
);
1136 if (! TYPE_USES_COMPLEX_INHERITANCE (type
))
1137 rval
= build (COMPONENT_REF
, TREE_TYPE (CLASSTYPE_VFIELD (type
)),
1138 datum
, CLASSTYPE_VFIELD (type
));
1140 rval
= build_component_ref (datum
, DECL_NAME (CLASSTYPE_VFIELD (type
)), 0, 0);
1141 flag_assume_nonnull_objects
= old_assume_nonnull_objects
;
1146 /* Build a call to a member of an object. I.e., one that overloads
1147 operator ()(), or is a pointer-to-function or pointer-to-method. */
1149 build_field_call (basetype_path
, instance_ptr
, name
, parms
)
1150 tree basetype_path
, instance_ptr
, name
, parms
;
1152 tree field
, instance
;
1154 if (instance_ptr
== current_class_decl
)
1156 /* Check to see if we really have a reference to an instance variable
1157 with `operator()()' overloaded. */
1158 field
= IDENTIFIER_CLASS_VALUE (name
);
1160 if (field
== NULL_TREE
)
1162 cp_error ("`this' has no member named `%D'", name
);
1163 return error_mark_node
;
1166 if (TREE_CODE (field
) == FIELD_DECL
)
1168 /* If it's a field, try overloading operator (),
1169 or calling if the field is a pointer-to-function. */
1170 instance
= build_component_ref_1 (C_C_D
, field
, 0);
1171 if (instance
== error_mark_node
)
1172 return error_mark_node
;
1174 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1175 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance
)))
1176 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, instance
, parms
, NULL_TREE
);
1178 if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1180 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == FUNCTION_TYPE
)
1181 return build_function_call (instance
, parms
);
1182 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == METHOD_TYPE
)
1183 return build_function_call (instance
, tree_cons (NULL_TREE
, current_class_decl
, parms
));
1189 /* Check to see if this is not really a reference to an instance variable
1190 with `operator()()' overloaded. */
1191 field
= lookup_field (basetype_path
, name
, 1, 0);
1193 /* This can happen if the reference was ambiguous or for access
1195 if (field
== error_mark_node
)
1196 return error_mark_node
;
1201 tree ftype
= TREE_TYPE (field
);
1203 if (TREE_CODE (ftype
) == REFERENCE_TYPE
)
1204 ftype
= TREE_TYPE (ftype
);
1206 if (TYPE_LANG_SPECIFIC (ftype
) && TYPE_OVERLOADS_CALL_EXPR (ftype
))
1208 /* Make the next search for this field very short. */
1209 basetype
= DECL_FIELD_CONTEXT (field
);
1210 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
1212 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1213 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
1214 build_component_ref_1 (instance
, field
, 0),
1217 if (TREE_CODE (ftype
) == POINTER_TYPE
)
1219 if (TREE_CODE (TREE_TYPE (ftype
)) == FUNCTION_TYPE
1220 || TREE_CODE (TREE_TYPE (ftype
)) == METHOD_TYPE
)
1222 /* This is a member which is a pointer to function. */
1224 = build_component_ref_1 (build_indirect_ref (instance_ptr
,
1226 field
, LOOKUP_COMPLAIN
);
1227 if (ref
== error_mark_node
)
1228 return error_mark_node
;
1229 return build_function_call (ref
, parms
);
1232 else if (TREE_CODE (ftype
) == METHOD_TYPE
)
1234 error ("invalid call via pointer-to-member function");
1235 return error_mark_node
;
1244 find_scoped_type (type
, inner_name
, inner_types
)
1245 tree type
, inner_name
, inner_types
;
1247 tree tags
= CLASSTYPE_TAGS (type
);
1251 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1252 enclosing class) is set to the name for the enum type. So, if
1253 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1254 then this test will be true. */
1255 if (TREE_PURPOSE (tags
) == inner_name
)
1257 if (inner_types
== NULL_TREE
)
1258 return DECL_NESTED_TYPENAME (TYPE_NAME (TREE_VALUE (tags
)));
1259 return resolve_scope_to_name (TREE_VALUE (tags
), inner_types
);
1261 tags
= TREE_CHAIN (tags
);
1265 /* XXX This needs to be fixed better. */
1266 if (TREE_CODE (type
) == UNINSTANTIATED_P_TYPE
)
1268 sorry ("nested class lookup in template type");
1273 /* Look for a TYPE_DECL. */
1274 for (tags
= TYPE_FIELDS (type
); tags
; tags
= TREE_CHAIN (tags
))
1275 if (TREE_CODE (tags
) == TYPE_DECL
&& DECL_NAME (tags
) == inner_name
)
1277 /* Code by raeburn. */
1278 if (inner_types
== NULL_TREE
)
1279 return DECL_NESTED_TYPENAME (tags
);
1280 return resolve_scope_to_name (TREE_TYPE (tags
), inner_types
);
1286 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1287 the name that names the above nested type. INNER_TYPES
1288 is a chain of nested type names (held together by SCOPE_REFs);
1289 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1290 Returns NULL_TREE if there is an error. */
1292 resolve_scope_to_name (outer_type
, inner_stuff
)
1293 tree outer_type
, inner_stuff
;
1296 tree inner_name
, inner_type
;
1298 if (outer_type
== NULL_TREE
&& current_class_type
!= NULL_TREE
)
1300 /* We first try to look for a nesting in our current class context,
1301 then try any enclosing classes. */
1302 tree type
= current_class_type
;
1304 while (type
&& (TREE_CODE (type
) == RECORD_TYPE
1305 || TREE_CODE (type
) == UNION_TYPE
))
1307 tree rval
= resolve_scope_to_name (type
, inner_stuff
);
1309 if (rval
!= NULL_TREE
)
1311 type
= DECL_CONTEXT (TYPE_NAME (type
));
1315 if (TREE_CODE (inner_stuff
) == SCOPE_REF
)
1317 inner_name
= TREE_OPERAND (inner_stuff
, 0);
1318 inner_type
= TREE_OPERAND (inner_stuff
, 1);
1322 inner_name
= inner_stuff
;
1323 inner_type
= NULL_TREE
;
1326 if (outer_type
== NULL_TREE
)
1328 /* If we have something that's already a type by itself,
1330 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name
))
1333 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name
),
1340 if (! IS_AGGR_TYPE (outer_type
))
1343 /* Look for member classes or enums. */
1344 tmp
= find_scoped_type (outer_type
, inner_name
, inner_type
);
1346 /* If it's not a type in this class, then go down into the
1347 base classes and search there. */
1348 if (! tmp
&& TYPE_BINFO (outer_type
))
1350 tree binfos
= TYPE_BINFO_BASETYPES (outer_type
);
1351 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1353 for (i
= 0; i
< n_baselinks
; i
++)
1355 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1356 tmp
= resolve_scope_to_name (BINFO_TYPE (base_binfo
), inner_stuff
);
1366 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1367 This is how virtual function calls are avoided. */
1369 build_scoped_method_call (exp
, scopes
, name
, parms
)
1370 tree exp
, scopes
, name
, parms
;
1372 /* Because this syntactic form does not allow
1373 a pointer to a base class to be `stolen',
1374 we need not protect the derived->base conversion
1377 @@ But we do have to check access privileges later. */
1378 tree basename
= resolve_scope_to_name (NULL_TREE
, scopes
);
1379 tree basetype
, binfo
, decl
;
1380 tree type
= TREE_TYPE (exp
);
1382 if (type
== error_mark_node
1383 || basename
== NULL_TREE
)
1384 return error_mark_node
;
1386 basetype
= IDENTIFIER_TYPE_VALUE (basename
);
1388 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1389 type
= TREE_TYPE (type
);
1391 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1392 that explicit ~int is caught in the parser; this deals with typedefs
1393 and template parms. */
1394 if (TREE_CODE (name
) == BIT_NOT_EXPR
&& ! is_aggr_typedef (basename
, 0))
1396 if (type
!= basetype
)
1397 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1398 exp
, basetype
, type
);
1399 name
= TREE_OPERAND (name
, 0);
1400 if (basetype
!= get_type_value (name
))
1401 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1403 return convert (void_type_node
, exp
);
1406 if (! is_aggr_typedef (basename
, 1))
1407 return error_mark_node
;
1409 if (! IS_AGGR_TYPE (type
))
1411 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1413 return error_mark_node
;
1416 if ((binfo
= binfo_or_else (basetype
, type
)))
1418 if (binfo
== error_mark_node
)
1419 return error_mark_node
;
1420 if (TREE_CODE (exp
) == INDIRECT_REF
)
1421 decl
= build_indirect_ref (convert_pointer_to (binfo
,
1422 build_unary_op (ADDR_EXPR
, exp
, 0)), NULL_PTR
);
1424 decl
= build_scoped_ref (exp
, scopes
);
1426 /* Call to a destructor. */
1427 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1429 /* Explicit call to destructor. */
1430 name
= TREE_OPERAND (name
, 0);
1431 if (! (name
== constructor_name (TREE_TYPE (decl
))
1432 || TREE_TYPE (decl
) == get_type_value (name
)))
1435 ("qualified type `%T' does not match destructor name `~%T'",
1436 TREE_TYPE (decl
), name
);
1437 return error_mark_node
;
1439 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
1440 return convert (void_type_node
, exp
);
1442 return build_delete (TREE_TYPE (decl
), decl
, integer_two_node
,
1443 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
1447 /* Call to a method. */
1448 return build_method_call (decl
, name
, parms
, binfo
,
1449 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
1451 return error_mark_node
;
1455 print_candidates (candidates
)
1458 cp_error_at ("candidates are: %D", TREE_VALUE (candidates
));
1459 candidates
= TREE_CHAIN (candidates
);
1463 cp_error_at (" %D", TREE_VALUE (candidates
));
1464 candidates
= TREE_CHAIN (candidates
);
1469 print_n_candidates (candidates
, n
)
1470 struct candidate
*candidates
;
1475 cp_error_at ("candidates are: %D", candidates
[0].function
);
1476 for (i
= 1; i
< n
; i
++)
1477 cp_error_at (" %D", candidates
[i
].function
);
1480 /* Build something of the form ptr->method (args)
1481 or object.method (args). This can also build
1482 calls to constructors, and find friends.
1484 Member functions always take their class variable
1487 INSTANCE is a class instance.
1489 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1491 PARMS help to figure out what that NAME really refers to.
1493 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1494 down to the real instance type to use for access checking. We need this
1495 information to get protected accesses correct. This parameter is used
1496 by build_member_call.
1498 FLAGS is the logical disjunction of zero or more LOOKUP_
1499 flags. See cp-tree.h for more info.
1501 If this is all OK, calls build_function_call with the resolved
1504 This function must also handle being called to perform
1505 initialization, promotion/coercion of arguments, and
1506 instantiation of default parameters.
1508 Note that NAME may refer to an instance variable name. If
1509 `operator()()' is defined for the type of that field, then we return
1512 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
1513 tree instance
, name
, parms
, basetype_path
;
1516 register tree function
, fntype
, value_type
;
1517 register tree basetype
, save_basetype
;
1518 register tree baselink
, result
, method_name
, parmtypes
, parm
;
1521 enum access_type access
= access_public
;
1523 /* Range of cases for vtable optimization. */
1524 enum vtable_needs
{ not_needed
, maybe_needed
, unneeded
, needed
};
1525 enum vtable_needs need_vtbl
= not_needed
;
1529 tree instance_ptr
= NULL_TREE
;
1530 int all_virtual
= flag_all_virtual
;
1531 int static_call_context
= 0;
1532 tree found_fns
= NULL_TREE
;
1534 /* Keep track of `const' and `volatile' objects. */
1535 int constp
, volatilep
;
1537 #ifdef GATHER_STATISTICS
1538 n_build_method_call
++;
1541 if (instance
== error_mark_node
1542 || name
== error_mark_node
1543 || parms
== error_mark_node
1544 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
1545 return error_mark_node
;
1547 /* This is the logic that magically deletes the second argument to
1548 operator delete, if it is not needed. */
1549 if (name
== ansi_opname
[(int) DELETE_EXPR
] && list_length (parms
)==2)
1551 tree save_last
= TREE_CHAIN (parms
);
1553 /* get rid of unneeded argument */
1554 TREE_CHAIN (parms
) = NULL_TREE
;
1555 result
= build_method_call (instance
, name
, parms
, basetype_path
,
1556 (LOOKUP_SPECULATIVELY
|flags
)
1558 /* If it finds a match, return it. */
1560 return build_method_call (instance
, name
, parms
, basetype_path
, flags
);
1561 /* If it doesn't work, two argument delete must work */
1562 TREE_CHAIN (parms
) = save_last
;
1564 /* We already know whether it's needed or not for vec delete. */
1565 else if (name
== ansi_opname
[(int) VEC_DELETE_EXPR
]
1566 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance
)))
1567 TREE_CHAIN (parms
) = NULL_TREE
;
1569 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1571 flags
|= LOOKUP_DESTRUCTOR
;
1572 name
= TREE_OPERAND (name
, 0);
1574 error ("destructors take no parameters");
1575 basetype
= TREE_TYPE (instance
);
1576 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1577 basetype
= TREE_TYPE (basetype
);
1578 if (! ((IS_AGGR_TYPE (basetype
)
1579 && name
== constructor_name (basetype
))
1580 || basetype
== get_type_value (name
)))
1582 cp_error ("destructor name `~%D' does not match type `%T' of expression",
1584 return convert (void_type_node
, instance
);
1587 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1588 return convert (void_type_node
, instance
);
1589 instance
= default_conversion (instance
);
1590 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1591 return build_delete (build_pointer_type (basetype
),
1592 instance_ptr
, integer_two_node
,
1593 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
1599 /* Initialize name for error reporting. */
1600 if (IDENTIFIER_OPNAME_P (name
) && ! IDENTIFIER_TYPENAME_P (name
))
1602 char *p
= operator_name_string (name
);
1603 xref_name
= (char *)alloca (strlen (p
) + 10);
1604 sprintf (xref_name
, "operator %s", p
);
1606 else if (TREE_CODE (name
) == SCOPE_REF
)
1607 xref_name
= IDENTIFIER_POINTER (TREE_OPERAND (name
, 1));
1609 xref_name
= IDENTIFIER_POINTER (name
);
1611 GNU_xref_call (current_function_decl
, xref_name
);
1614 if (instance
== NULL_TREE
)
1616 basetype
= NULL_TREE
;
1617 /* Check cases where this is really a call to raise
1619 if (current_class_type
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
1621 basetype
= purpose_member (name
, CLASSTYPE_TAGS (current_class_type
));
1623 basetype
= TREE_VALUE (basetype
);
1625 else if (TREE_CODE (name
) == SCOPE_REF
1626 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
1628 if (! is_aggr_typedef (TREE_OPERAND (name
, 0), 1))
1629 return error_mark_node
;
1630 basetype
= purpose_member (TREE_OPERAND (name
, 1),
1631 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name
, 0))));
1633 basetype
= TREE_VALUE (basetype
);
1636 if (basetype
!= NULL_TREE
)
1638 /* call to a constructor... */
1639 else if (basetype_path
)
1640 basetype
= BINFO_TYPE (basetype_path
);
1641 else if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1643 basetype
= IDENTIFIER_TYPE_VALUE (name
);
1644 name
= constructor_name_full (basetype
);
1648 tree typedef_name
= lookup_name (name
, 1);
1649 if (typedef_name
&& TREE_CODE (typedef_name
) == TYPE_DECL
)
1651 /* Canonicalize the typedef name. */
1652 basetype
= TREE_TYPE (typedef_name
);
1653 name
= TYPE_IDENTIFIER (basetype
);
1657 cp_error ("no constructor named `%T' in scope",
1659 return error_mark_node
;
1663 if (! IS_AGGR_TYPE (basetype
))
1666 if ((flags
& LOOKUP_COMPLAIN
) && TREE_CODE (basetype
) != ERROR_MARK
)
1667 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1668 name
, instance
, basetype
);
1670 return error_mark_node
;
1673 else if (instance
== C_C_D
|| instance
== current_class_decl
)
1675 /* When doing initialization, we side-effect the TREE_TYPE of
1676 C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1677 basetype
= TREE_TYPE (C_C_D
);
1679 /* Anything manifestly `this' in constructors and destructors
1680 has a known type, so virtual function tables are not needed. */
1681 if (TYPE_VIRTUAL_P (basetype
)
1682 && !(flags
& LOOKUP_NONVIRTUAL
))
1683 need_vtbl
= (dtor_label
|| ctor_label
)
1684 ? unneeded
: maybe_needed
;
1687 instance_ptr
= current_class_decl
;
1688 result
= build_field_call (TYPE_BINFO (current_class_type
),
1689 instance_ptr
, name
, parms
);
1694 else if (TREE_CODE (instance
) == RESULT_DECL
)
1696 basetype
= TREE_TYPE (instance
);
1697 /* Should we ever have to make a virtual function reference
1698 from a RESULT_DECL, know that it must be of fixed type
1699 within the scope of this function. */
1700 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1701 need_vtbl
= maybe_needed
;
1702 instance_ptr
= build1 (ADDR_EXPR
, TYPE_POINTER_TO (basetype
), instance
);
1706 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1707 tree inst_ptr_basetype
;
1709 static_call_context
=
1710 (TREE_CODE (instance
) == INDIRECT_REF
1711 && TREE_CODE (TREE_OPERAND (instance
, 0)) == NOP_EXPR
1712 && TREE_OPERAND (TREE_OPERAND (instance
, 0), 0) == error_mark_node
);
1714 if (TREE_CODE (instance
) == OFFSET_REF
)
1715 instance
= resolve_offset_ref (instance
);
1717 /* the base type of an instance variable is pointer to class */
1718 basetype
= TREE_TYPE (instance
);
1720 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1722 basetype
= TREE_TYPE (basetype
);
1723 if (! IS_AGGR_TYPE (basetype
))
1724 goto non_aggr_error
;
1725 /* Call to convert not needed because we are remaining
1726 within the same type. */
1727 instance_ptr
= build1 (NOP_EXPR
, build_pointer_type (basetype
),
1729 inst_ptr_basetype
= TYPE_MAIN_VARIANT (basetype
);
1733 if (! IS_AGGR_TYPE (basetype
)
1734 && ! (TYPE_LANG_SPECIFIC (basetype
)
1735 && (IS_SIGNATURE_POINTER (basetype
)
1736 || IS_SIGNATURE_REFERENCE (basetype
))))
1737 goto non_aggr_error
;
1739 /* If `instance' is a signature pointer/reference and `name' is
1740 not a constructor, we are calling a signature member function.
1741 In that case set the `basetype' to the signature type. */
1742 if ((IS_SIGNATURE_POINTER (basetype
)
1743 || IS_SIGNATURE_REFERENCE (basetype
))
1744 && TYPE_IDENTIFIER (basetype
) != name
)
1745 basetype
= SIGNATURE_TYPE (basetype
);
1747 if ((IS_SIGNATURE (basetype
)
1748 && (instance_ptr
= build_optr_ref (instance
)))
1749 || (lvalue_p (instance
)
1750 && (instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0)))
1751 || (instance_ptr
= unary_complex_lvalue (ADDR_EXPR
, instance
)))
1753 if (instance_ptr
== error_mark_node
)
1754 return error_mark_node
;
1756 else if (TREE_CODE (instance
) == NOP_EXPR
1757 || TREE_CODE (instance
) == CONSTRUCTOR
)
1759 /* A cast is not an lvalue. Initialize a fresh temp
1760 with the value we are casting from, and proceed with
1761 that temporary. We can't cast to a reference type,
1762 so that simplifies the initialization to something
1764 tree temp
= get_temp_name (TREE_TYPE (instance
), 0);
1765 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
1766 expand_aggr_init (temp
, instance
, 0, flags
);
1769 store_init_value (temp
, instance
);
1770 expand_decl_init (temp
);
1773 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1777 if (TREE_CODE (instance
) != CALL_EXPR
1778 #ifdef PCC_STATIC_STRUCT_RETURN
1779 && TREE_CODE (instance
) != RTL_EXPR
1782 my_friendly_abort (125);
1783 if (TYPE_NEEDS_CONSTRUCTING (basetype
))
1784 instance
= build_cplus_new (basetype
, instance
, 0);
1787 instance
= get_temp_name (basetype
, 0);
1788 TREE_ADDRESSABLE (instance
) = 1;
1790 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1792 /* @@ Should we call comp_target_types here? */
1793 inst_ptr_basetype
= TREE_TYPE (TREE_TYPE (instance_ptr
));
1794 if (TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1795 basetype
= inst_ptr_basetype
;
1798 instance_ptr
= convert (TYPE_POINTER_TO (basetype
), instance_ptr
);
1799 if (instance_ptr
== error_mark_node
)
1800 return error_mark_node
;
1804 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1805 not updated, so we use `basetype' instead. */
1806 if (basetype_path
== NULL_TREE
1807 && IS_SIGNATURE (basetype
))
1808 basetype_path
= TYPE_BINFO (basetype
);
1809 else if (basetype_path
== NULL_TREE
||
1810 BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1811 basetype_path
= TYPE_BINFO (inst_ptr_basetype
);
1813 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1817 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1819 if (TREE_SIDE_EFFECTS (instance_ptr
))
1821 /* This action is needed because the instance is needed
1822 for providing the base of the virtual function table.
1823 Without using a SAVE_EXPR, the function we are building
1824 may be called twice, or side effects on the instance
1825 variable (such as a post-increment), may happen twice. */
1826 instance_ptr
= save_expr (instance_ptr
);
1827 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1829 else if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1831 /* This happens when called for operator new (). */
1832 instance
= build_indirect_ref (instance
, NULL_PTR
);
1835 need_vtbl
= maybe_needed
;
1839 if (TYPE_SIZE (basetype
) == 0)
1841 /* This is worth complaining about, I think. */
1842 cp_error ("cannot lookup method in incomplete type `%T'", basetype
);
1843 return error_mark_node
;
1846 save_basetype
= TYPE_MAIN_VARIANT (basetype
);
1849 if (all_virtual
== 1
1850 && (! strncmp (IDENTIFIER_POINTER (name
), OPERATOR_METHOD_FORMAT
,
1851 OPERATOR_METHOD_LENGTH
)
1852 || instance_ptr
== NULL_TREE
1853 || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype
) == 0)))
1858 for (parmtypes
= NULL_TREE
, parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1860 tree t
= TREE_TYPE (TREE_VALUE (parm
));
1861 if (TREE_CODE (t
) == OFFSET_TYPE
)
1863 /* Convert OFFSET_TYPE entities to their normal selves. */
1864 TREE_VALUE (parm
) = resolve_offset_ref (TREE_VALUE (parm
));
1865 t
= TREE_TYPE (TREE_VALUE (parm
));
1867 if (TREE_CODE (TREE_VALUE (parm
)) == OFFSET_REF
1868 && TREE_CODE (t
) == METHOD_TYPE
)
1870 TREE_VALUE (parm
) = build_unary_op (ADDR_EXPR
, TREE_VALUE (parm
), 0);
1873 /* This breaks reference-to-array parameters. */
1874 if (TREE_CODE (t
) == ARRAY_TYPE
)
1876 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
1877 This eliminates needless calls to `compute_conversion_costs'. */
1878 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
1879 t
= TREE_TYPE (TREE_VALUE (parm
));
1882 if (t
== error_mark_node
)
1883 return error_mark_node
;
1884 last
= build_tree_list (NULL_TREE
, t
);
1885 parmtypes
= chainon (parmtypes
, last
);
1890 /* TREE_READONLY (instance) fails for references. */
1891 constp
= TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr
)));
1892 volatilep
= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr
)));
1893 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
1897 /* Raw constructors are always in charge. */
1898 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
1899 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
1901 flags
|= LOOKUP_HAS_IN_CHARGE
;
1902 parms
= tree_cons (NULL_TREE
, integer_one_node
, parms
);
1903 parmtypes
= tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
1906 if (flag_this_is_variable
> 0)
1910 instance_ptr
= build_int_2 (0, 0);
1911 TREE_TYPE (instance_ptr
) = TYPE_POINTER_TO (basetype
);
1912 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
1918 instance_ptr
= build_new (NULL_TREE
, basetype
, void_type_node
, 0);
1919 if (instance_ptr
== error_mark_node
)
1920 return error_mark_node
;
1921 instance_ptr
= save_expr (instance_ptr
);
1922 TREE_CALLS_NEW (instance_ptr
) = 1;
1923 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1926 /* This breaks initialization of a reference from a new
1927 expression of a different type. And it doesn't appear to
1928 serve its original purpose any more, either. jason 10/12/94 */
1929 /* If it's a default argument initialized from a ctor, what we get
1930 from instance_ptr will match the arglist for the FUNCTION_DECL
1931 of the constructor. */
1932 if (parms
&& TREE_CODE (TREE_VALUE (parms
)) == CALL_EXPR
1933 && TREE_OPERAND (TREE_VALUE (parms
), 1)
1934 && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms
), 1))))
1935 parms
= build_tree_list (NULL_TREE
, instance_ptr
);
1938 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
1942 parmtypes
= tree_cons (NULL_TREE
, TREE_TYPE (instance_ptr
), parmtypes
);
1944 if (last
== NULL_TREE
)
1947 /* Look up function name in the structure type definition. */
1949 if ((IDENTIFIER_HAS_TYPE_VALUE (name
)
1950 && ! IDENTIFIER_OPNAME_P (name
)
1951 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name
))
1952 && TREE_CODE (IDENTIFIER_TYPE_VALUE (name
)) != UNINSTANTIATED_P_TYPE
)
1953 || name
== constructor_name (basetype
))
1955 tree tmp
= NULL_TREE
;
1956 if (IDENTIFIER_TYPE_VALUE (name
) == basetype
1957 || name
== constructor_name (basetype
))
1958 tmp
= TYPE_BINFO (basetype
);
1960 tmp
= get_binfo (IDENTIFIER_TYPE_VALUE (name
), basetype
, 0);
1962 if (tmp
!= NULL_TREE
)
1964 name_kind
= "constructor";
1966 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
1967 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
1969 /* Constructors called for initialization
1970 only are never in charge. */
1973 flags
|= LOOKUP_HAS_IN_CHARGE
;
1974 tmplist
= tree_cons (NULL_TREE
, integer_zero_node
,
1975 TREE_CHAIN (parms
));
1976 TREE_CHAIN (parms
) = tmplist
;
1977 tmplist
= tree_cons (NULL_TREE
, integer_type_node
, TREE_CHAIN (parmtypes
));
1978 TREE_CHAIN (parmtypes
) = tmplist
;
1980 basetype
= BINFO_TYPE (tmp
);
1983 name_kind
= "method";
1986 name_kind
= "method";
1988 if (basetype_path
== NULL_TREE
1989 || BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (basetype
))
1990 basetype_path
= TYPE_BINFO (basetype
);
1991 result
= lookup_fnfields (basetype_path
, name
,
1992 (flags
& LOOKUP_COMPLAIN
));
1993 if (result
== error_mark_node
)
1994 return error_mark_node
;
1998 /* Now, go look for this method name. We do not find destructors here.
2000 Putting `void_list_node' on the end of the parmtypes
2001 fakes out `build_decl_overload' into doing the right thing. */
2002 TREE_CHAIN (last
) = void_list_node
;
2003 method_name
= build_decl_overload (name
, parmtypes
,
2004 1 + (name
== constructor_name (save_basetype
)
2005 || name
== constructor_name_full (save_basetype
)));
2006 TREE_CHAIN (last
) = NULL_TREE
;
2009 for (pass
= 0; pass
< 2; pass
++)
2011 struct candidate
*candidates
;
2012 struct candidate
*cp
;
2016 /* This increments every time we go up the type hierarchy.
2017 The idea is to prefer a function of the derived class if possible. */
2025 = (struct candidate
*) alloca ((ever_seen
+1)
2026 * sizeof (struct candidate
));
2027 bzero ((char *) candidates
, (ever_seen
+ 1) * sizeof (struct candidate
));
2029 len
= list_length (parms
);
2032 /* First see if a global function has a shot at it. */
2033 if (flags
& LOOKUP_GLOBAL
)
2036 tree parm
= instance_ptr
;
2038 if (TREE_CODE (TREE_TYPE (parm
)) == REFERENCE_TYPE
)
2039 parm
= convert_from_reference (parm
);
2040 else if (TREE_CODE (TREE_TYPE (parm
)) == POINTER_TYPE
)
2041 parm
= build_indirect_ref (parm
, "friendifying parms (compiler error)");
2043 my_friendly_abort (167);
2045 friend_parms
= tree_cons (NULL_TREE
, parm
, TREE_CHAIN (parms
));
2048 cp
->harshness
= (struct harshness_code
*)
2049 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2051 result
= build_overload_call (name
, friend_parms
, 0, cp
);
2052 /* If it turns out to be the one we were actually looking for
2053 (it was probably a friend function), the return the
2055 if (TREE_CODE (result
) == CALL_EXPR
)
2058 while ((cp
->h
.code
& EVIL_CODE
) == 0)
2060 /* non-standard uses: set the field to 0 to indicate
2061 we are using a non-member function. */
2063 if (cp
->harshness
[len
].distance
== 0
2064 && cp
->h
.code
< best
)
2073 /* We have a hit (of sorts). If the parameter list is
2074 "error_mark_node", or some variant thereof, it won't
2075 match any methods. Since we have verified that the is
2076 some method vaguely matching this one (in name at least),
2079 Don't stop for friends, however. */
2080 basetype_path
= TREE_PURPOSE (baselink
);
2082 function
= TREE_VALUE (baselink
);
2083 if (TREE_CODE (basetype_path
) == TREE_LIST
)
2084 basetype_path
= TREE_VALUE (basetype_path
);
2085 basetype
= BINFO_TYPE (basetype_path
);
2088 /* Cast the instance variable if necessary. */
2089 if (basetype
!= TYPE_MAIN_VARIANT
2090 (TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)))))
2092 if (basetype
== save_basetype
)
2093 TREE_VALUE (parms
) = instance_ptr
;
2096 tree type
= build_pointer_type
2097 (build_type_variant (basetype
, constp
, volatilep
));
2098 TREE_VALUE (parms
) = convert_force (type
, instance_ptr
, 0);
2102 /* FIXME: this is the wrong place to get an error. Hopefully
2103 the access-control rewrite will make this change more cleanly. */
2104 if (TREE_VALUE (parms
) == error_mark_node
)
2105 return error_mark_node
;
2108 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function
)))
2109 function
= DECL_CHAIN (function
);
2111 for (; function
; function
= DECL_CHAIN (function
))
2113 #ifdef GATHER_STATISTICS
2114 n_inner_fields_searched
++;
2118 found_fns
= tree_cons (NULL_TREE
, function
, found_fns
);
2120 /* Not looking for friends here. */
2121 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
2122 && ! DECL_STATIC_FUNCTION_P (function
))
2127 && DECL_ASSEMBLER_NAME (function
) == method_name
)
2133 tree these_parms
= parms
;
2135 #ifdef GATHER_STATISTICS
2136 n_inner_fields_searched
++;
2139 cp
->harshness
= (struct harshness_code
*)
2140 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2142 if (DECL_STATIC_FUNCTION_P (function
))
2143 these_parms
= TREE_CHAIN (these_parms
);
2144 compute_conversion_costs (function
, these_parms
, cp
, len
);
2146 if ((cp
->h
.code
& EVIL_CODE
) == 0)
2148 cp
->u
.field
= function
;
2149 cp
->function
= function
;
2150 cp
->basetypes
= basetype_path
;
2152 /* Don't allow non-converting constructors to convert. */
2153 if (flags
& LOOKUP_ONLYCONVERTING
2154 && DECL_LANG_SPECIFIC (function
)
2155 && DECL_NONCONVERTING_P (function
))
2158 /* No "two-level" conversions. */
2159 if (flags
& LOOKUP_NO_CONVERSION
2160 && (cp
->h
.code
& USER_CODE
))
2167 /* Now we have run through one link's member functions.
2168 arrange to head-insert this link's links. */
2169 baselink
= next_baselink (baselink
);
2171 /* Don't grab functions from base classes. lookup_fnfield will
2172 do the work to get us down into the right place. */
2173 baselink
= NULL_TREE
;
2177 tree igv
= lookup_name_nonclass (name
);
2179 /* No exact match could be found. Now try to find match
2180 using default conversions. */
2181 if ((flags
& LOOKUP_GLOBAL
) && igv
)
2183 if (TREE_CODE (igv
) == FUNCTION_DECL
)
2185 else if (TREE_CODE (igv
) == TREE_LIST
)
2186 ever_seen
+= count_functions (igv
);
2191 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2192 == LOOKUP_SPECULATIVELY
)
2195 TREE_CHAIN (last
) = void_list_node
;
2196 if (flags
& LOOKUP_GLOBAL
)
2197 cp_error ("no global or member function `%D(%A)' defined",
2200 cp_error ("no member function `%T::%D(%A)' defined",
2201 save_basetype
, name
, TREE_CHAIN (parmtypes
));
2202 return error_mark_node
;
2207 if (cp
- candidates
!= 0)
2209 /* Rank from worst to best. Then cp will point to best one.
2210 Private fields have their bits flipped. For unsigned
2211 numbers, this should make them look very large.
2212 If the best alternate has a (signed) negative value,
2213 then all we ever saw were private members. */
2214 if (cp
- candidates
> 1)
2216 int n_candidates
= cp
- candidates
;
2217 extern int warn_synth
;
2218 TREE_VALUE (parms
) = instance_ptr
;
2219 cp
= ideal_candidate (save_basetype
, candidates
,
2220 n_candidates
, parms
, len
);
2221 if (cp
== (struct candidate
*)0)
2223 if (flags
& LOOKUP_COMPLAIN
)
2225 TREE_CHAIN (last
) = void_list_node
;
2226 cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2227 name_kind
, name
, TREE_CHAIN (parmtypes
));
2228 print_n_candidates (candidates
, n_candidates
);
2230 return error_mark_node
;
2232 if (cp
->h
.code
& EVIL_CODE
)
2233 return error_mark_node
;
2235 && DECL_NAME (cp
->function
) == ansi_opname
[MODIFY_EXPR
]
2236 && DECL_ARTIFICIAL (cp
->function
)
2237 && n_candidates
== 2)
2239 cp_warning ("using synthesized `%#D' for copy assignment",
2241 cp_warning_at (" where cfront would use `%#D'",
2242 candidates
->function
);
2245 else if (cp
[-1].h
.code
& EVIL_CODE
)
2247 if (flags
& LOOKUP_COMPLAIN
)
2248 cp_error ("ambiguous type conversion requested for %s `%D'",
2250 return error_mark_node
;
2255 /* The global function was the best, so use it. */
2256 if (cp
->u
.field
== 0)
2258 /* We must convert the instance pointer into a reference type.
2259 Global overloaded functions can only either take
2260 aggregate objects (which come for free from references)
2261 or reference data types anyway. */
2262 TREE_VALUE (parms
) = copy_node (instance_ptr
);
2263 TREE_TYPE (TREE_VALUE (parms
)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2264 return build_function_call (cp
->function
, parms
);
2267 function
= cp
->function
;
2268 basetype_path
= cp
->basetypes
;
2269 if (! DECL_STATIC_FUNCTION_P (function
))
2270 TREE_VALUE (parms
) = cp
->arg
;
2271 goto found_and_maybe_warn
;
2274 if (flags
& (LOOKUP_COMPLAIN
|LOOKUP_SPECULATIVELY
))
2276 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2277 == LOOKUP_SPECULATIVELY
)
2280 if (DECL_STATIC_FUNCTION_P (cp
->function
))
2281 parms
= TREE_CHAIN (parms
);
2284 if (flags
& LOOKUP_SPECULATIVELY
)
2286 if (static_call_context
2287 && TREE_CODE (TREE_TYPE (cp
->function
)) == METHOD_TYPE
)
2288 cp_error ("object missing in call to `%D'", cp
->function
);
2289 else if (ever_seen
> 1)
2291 TREE_CHAIN (last
) = void_list_node
;
2292 cp_error ("no matching function for call to `%T::%D (%A)'",
2293 TREE_TYPE (TREE_TYPE (instance_ptr
)),
2294 name
, TREE_CHAIN (parmtypes
));
2295 TREE_CHAIN (last
) = NULL_TREE
;
2296 print_candidates (found_fns
);
2299 report_type_mismatch (cp
, parms
, name_kind
);
2300 return error_mark_node
;
2303 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2306 cp_error ("%T has no method named %D", save_basetype
, name
);
2307 return error_mark_node
;
2313 found_and_maybe_warn
:
2314 if ((cp
->harshness
[0].code
& CONST_CODE
)
2315 /* 12.1p2: Constructors can be called for const objects. */
2316 && ! DECL_CONSTRUCTOR_P (cp
->function
))
2318 if (flags
& LOOKUP_COMPLAIN
)
2320 cp_error_at ("non-const member function `%D'", cp
->function
);
2321 error ("called for const object at this point in file");
2323 /* Not good enough for a match. */
2325 return error_mark_node
;
2329 /* Silently return error_mark_node. */
2330 return error_mark_node
;
2333 if (flags
& LOOKUP_PROTECT
)
2334 access
= compute_access (basetype_path
, function
);
2336 if (access
== access_private
)
2338 if (flags
& LOOKUP_COMPLAIN
)
2340 cp_error_at ("%s `%+#D' is %s", name_kind
, function
,
2341 TREE_PRIVATE (function
) ? "private"
2342 : "from private base class");
2343 error ("within this context");
2345 return error_mark_node
;
2347 else if (access
== access_protected
)
2349 if (flags
& LOOKUP_COMPLAIN
)
2351 cp_error_at ("%s `%+#D' %s", name_kind
, function
,
2352 TREE_PROTECTED (function
) ? "is protected"
2353 : "has protected accessibility");
2354 error ("within this context");
2356 return error_mark_node
;
2359 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2360 type (if it exists) is a pointer to. */
2362 if (DECL_ABSTRACT_VIRTUAL_P (function
)
2363 && instance
== C_C_D
2364 && DECL_CONSTRUCTOR_P (current_function_decl
)
2365 && ! (flags
& LOOKUP_NONVIRTUAL
)
2366 && value_member (function
, get_abstract_virtuals (basetype
)))
2367 cp_error ("abstract virtual `%#D' called from constructor", function
);
2369 if (IS_SIGNATURE (basetype
) && static_call_context
)
2371 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2373 return error_mark_node
;
2375 else if (IS_SIGNATURE (basetype
))
2376 return build_signature_method_call (basetype
, instance
, function
, parms
);
2378 function
= DECL_MAIN_VARIANT (function
);
2379 /* Declare external function if necessary. */
2380 assemble_external (function
);
2383 /* Is it a synthesized method that needs to be synthesized? */
2384 if (DECL_ARTIFICIAL (function
) && ! flag_no_inline
2385 && DECL_SAVED_INSNS (function
) == 0
2386 && ! TREE_ASM_WRITTEN (function
)
2387 /* Kludge: don't synthesize for default args. */
2388 && current_function_decl
)
2389 synthesize_method (function
);
2392 fntype
= TREE_TYPE (function
);
2393 if (TREE_CODE (fntype
) == POINTER_TYPE
)
2394 fntype
= TREE_TYPE (fntype
);
2395 basetype
= DECL_CLASS_CONTEXT (function
);
2397 /* If we are referencing a virtual function from an object
2398 of effectively static type, then there is no need
2399 to go through the virtual function table. */
2400 if (need_vtbl
== maybe_needed
)
2402 int fixed_type
= resolves_to_fixed_type_p (instance
, 0);
2404 if (all_virtual
== 1
2405 && DECL_VINDEX (function
)
2406 && may_be_remote (basetype
))
2408 else if (DECL_VINDEX (function
))
2409 need_vtbl
= fixed_type
? unneeded
: needed
;
2411 need_vtbl
= not_needed
;
2414 if (TREE_CODE (fntype
) == METHOD_TYPE
&& static_call_context
2415 && !DECL_CONSTRUCTOR_P (function
))
2417 /* Let's be nice to the user for now, and give reasonable
2418 default behavior. */
2419 instance_ptr
= current_class_decl
;
2422 if (basetype
!= current_class_type
)
2424 tree binfo
= get_binfo (basetype
, current_class_type
, 1);
2425 if (binfo
== NULL_TREE
)
2427 error_not_base_type (function
, current_class_type
);
2428 return error_mark_node
;
2430 else if (basetype
== error_mark_node
)
2431 return error_mark_node
;
2434 /* Only allow a static member function to call another static member
2436 else if (DECL_LANG_SPECIFIC (function
)
2437 && !DECL_STATIC_FUNCTION_P (function
))
2439 cp_error ("cannot call member function `%D' without object",
2441 return error_mark_node
;
2445 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2447 if (TYPE_SIZE (value_type
) == 0)
2449 if (flags
& LOOKUP_COMPLAIN
)
2450 incomplete_type_error (0, value_type
);
2451 return error_mark_node
;
2454 if (DECL_STATIC_FUNCTION_P (function
))
2455 parms
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2456 TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
);
2457 else if (need_vtbl
== unneeded
)
2459 int sub_flags
= DECL_CONSTRUCTOR_P (function
) ? flags
: LOOKUP_NORMAL
;
2460 basetype
= TREE_TYPE (instance
);
2461 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function
)) != TYPE_MAIN_VARIANT (basetype
)
2462 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
2464 basetype
= DECL_CLASS_CONTEXT (function
);
2465 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
2466 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2468 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2469 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, sub_flags
));
2473 if ((flags
& LOOKUP_NONVIRTUAL
) == 0)
2474 basetype
= DECL_CONTEXT (function
);
2476 /* First parm could be integer_zerop with casts like
2477 ((Object*)0)->Object::IsA() */
2478 if (!integer_zerop (TREE_VALUE (parms
)))
2480 /* Since we can't have inheritance with a union, doing get_binfo
2481 on it won't work. We do all the convert_pointer_to_real
2482 stuff to handle MI correctly...for unions, that's not
2483 an issue, so we must short-circuit that extra work here. */
2484 tree tmp
= TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)));
2485 if (tmp
!= NULL_TREE
&& TREE_CODE (tmp
) == UNION_TYPE
)
2486 instance_ptr
= TREE_VALUE (parms
);
2489 tree binfo
= get_binfo (basetype
,
2490 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
))),
2492 instance_ptr
= convert_pointer_to_real (binfo
, TREE_VALUE (parms
));
2495 = convert_pointer_to (build_type_variant (basetype
,
2499 if (TREE_CODE (instance_ptr
) == COND_EXPR
)
2501 instance_ptr
= save_expr (instance_ptr
);
2502 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2504 else if (TREE_CODE (instance_ptr
) == NOP_EXPR
2505 && TREE_CODE (TREE_OPERAND (instance_ptr
, 0)) == ADDR_EXPR
2506 && TREE_OPERAND (TREE_OPERAND (instance_ptr
, 0), 0) == instance
)
2508 /* The call to `convert_pointer_to' may return error_mark_node. */
2509 else if (TREE_CODE (instance_ptr
) == ERROR_MARK
)
2510 return instance_ptr
;
2511 else if (instance
== NULL_TREE
2512 || TREE_CODE (instance
) != INDIRECT_REF
2513 || TREE_OPERAND (instance
, 0) != instance_ptr
)
2514 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2516 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2517 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
));
2521 /* Constructors do not overload method calls. */
2522 else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype
)
2523 && name
!= TYPE_IDENTIFIER (basetype
)
2524 && (TREE_CODE (function
) != FUNCTION_DECL
2525 || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)),
2526 OPERATOR_METHOD_FORMAT
,
2527 OPERATOR_METHOD_LENGTH
))
2528 && (may_be_remote (basetype
) || instance
!= C_C_D
))
2532 parms
= TREE_CHAIN (parms
);
2534 if (!all_virtual
&& TREE_CODE (function
) == FUNCTION_DECL
)
2535 fn_as_int
= build_unary_op (ADDR_EXPR
, function
, 0);
2537 fn_as_int
= convert (TREE_TYPE (default_conversion (function
)), DECL_VINDEX (function
));
2538 if (all_virtual
== 1)
2539 fn_as_int
= convert (integer_type_node
, fn_as_int
);
2541 result
= build_opfncall (METHOD_CALL_EXPR
, LOOKUP_NORMAL
, instance
, fn_as_int
, parms
);
2543 if (result
== NULL_TREE
)
2545 compiler_error ("could not overload `operator->()(...)'");
2546 return error_mark_node
;
2548 else if (result
== error_mark_node
)
2549 return error_mark_node
;
2552 /* Do this if we want the result of operator->() to inherit
2553 the type of the function it is subbing for. */
2554 TREE_TYPE (result
) = value_type
;
2561 if (need_vtbl
== needed
)
2563 function
= build_vfn_ref (&TREE_VALUE (parms
), instance
,
2564 DECL_VINDEX (function
));
2565 TREE_TYPE (function
) = build_pointer_type (fntype
);
2568 if (TREE_CODE (function
) == FUNCTION_DECL
)
2569 GNU_xref_call (current_function_decl
,
2570 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)));
2575 if (TREE_CODE (function
) == FUNCTION_DECL
)
2577 is_constructor
= DECL_CONSTRUCTOR_P (function
);
2578 if (DECL_INLINE (function
))
2579 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
2582 assemble_external (function
);
2583 TREE_USED (function
) = 1;
2584 function
= default_conversion (function
);
2590 function
= default_conversion (function
);
2593 result
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
2595 TREE_TYPE (result
) = value_type
;
2596 TREE_SIDE_EFFECTS (result
) = 1;
2597 TREE_HAS_CONSTRUCTOR (result
) = is_constructor
;
2598 result
= convert_from_reference (result
);
2603 /* Similar to `build_method_call', but for overloaded non-member functions.
2604 The name of this function comes through NAME. The name depends
2607 Note that this function must handle simple `C' promotions,
2608 as well as variable numbers of arguments (...), and
2609 default arguments to boot.
2611 If the overloading is successful, we return a tree node which
2612 contains the call to the function.
2614 If overloading produces candidates which are probable, but not definite,
2615 we hold these candidates. If FINAL_CP is non-zero, then we are free
2616 to assume that final_cp points to enough storage for all candidates that
2617 this function might generate. The `harshness' array is preallocated for
2618 the first candidate, but not for subsequent ones.
2620 Note that the DECL_RTL of FUNCTION must be made to agree with this
2621 function's new name. */
2624 build_overload_call_real (fnname
, parms
, flags
, final_cp
, buildxxx
)
2627 struct candidate
*final_cp
;
2630 /* must check for overloading here */
2631 tree overload_name
, functions
, function
, parm
;
2632 tree parmtypes
= NULL_TREE
, last
= NULL_TREE
;
2633 register tree outer
;
2635 int parmlength
= list_length (parms
);
2637 struct candidate
*candidates
, *cp
;
2641 final_cp
[0].h
.code
= 0;
2642 final_cp
[0].h
.distance
= 0;
2643 final_cp
[0].function
= 0;
2645 final_cp
[1].h
.code
= EVIL_CODE
;
2648 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2650 register tree t
= TREE_TYPE (TREE_VALUE (parm
));
2652 if (t
== error_mark_node
)
2655 final_cp
->h
.code
= EVIL_CODE
;
2656 return error_mark_node
;
2658 if (TREE_CODE (t
) == OFFSET_TYPE
)
2660 /* This breaks reference-to-array parameters. */
2661 || TREE_CODE (t
) == ARRAY_TYPE
2664 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
2665 Also convert OFFSET_TYPE entities to their normal selves.
2666 This eliminates needless calls to `compute_conversion_costs'. */
2667 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
2668 t
= TREE_TYPE (TREE_VALUE (parm
));
2670 last
= build_tree_list (NULL_TREE
, t
);
2671 parmtypes
= chainon (parmtypes
, last
);
2674 TREE_CHAIN (last
) = void_list_node
;
2676 parmtypes
= void_list_node
;
2678 if (is_overloaded_fn (fnname
))
2681 if (TREE_CODE (fnname
) == TREE_LIST
)
2682 fnname
= TREE_PURPOSE (functions
);
2683 else if (TREE_CODE (fnname
) == FUNCTION_DECL
)
2684 fnname
= DECL_NAME (functions
);
2687 functions
= lookup_name_nonclass (fnname
);
2689 if (functions
== NULL_TREE
)
2691 if (flags
& LOOKUP_SPECULATIVELY
)
2693 if (flags
& LOOKUP_COMPLAIN
)
2694 error ("only member functions apply");
2696 final_cp
->h
.code
= EVIL_CODE
;
2697 return error_mark_node
;
2700 if (TREE_CODE (functions
) == FUNCTION_DECL
&& ! IDENTIFIER_OPNAME_P (fnname
))
2702 functions
= DECL_MAIN_VARIANT (functions
);
2705 /* We are just curious whether this is a viable alternative or
2707 compute_conversion_costs (functions
, parms
, final_cp
, parmlength
);
2711 return build_function_call_real (functions
, parms
, 1, flags
);
2714 if (TREE_CODE (functions
) == TREE_LIST
2715 && TREE_VALUE (functions
) == NULL_TREE
)
2717 if (flags
& LOOKUP_SPECULATIVELY
)
2720 if (flags
& LOOKUP_COMPLAIN
)
2721 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2722 TREE_PURPOSE (functions
));
2724 final_cp
->h
.code
= EVIL_CODE
;
2725 return error_mark_node
;
2728 length
= count_functions (functions
);
2731 candidates
= final_cp
;
2735 = (struct candidate
*)alloca ((length
+1) * sizeof (struct candidate
));
2736 bzero ((char *) candidates
, (length
+ 1) * sizeof (struct candidate
));
2741 my_friendly_assert (is_overloaded_fn (functions
), 169);
2743 functions
= get_first_fn (functions
);
2745 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2746 for (outer
= functions
; outer
; outer
= DECL_CHAIN (outer
))
2748 int template_cost
= 0;
2750 if (TREE_CODE (function
) != FUNCTION_DECL
2751 && ! (TREE_CODE (function
) == TEMPLATE_DECL
2752 && ! DECL_TEMPLATE_IS_CLASS (function
)
2753 && TREE_CODE (DECL_TEMPLATE_RESULT (function
)) == FUNCTION_DECL
))
2755 enum tree_code code
= TREE_CODE (function
);
2756 if (code
== TEMPLATE_DECL
)
2757 code
= TREE_CODE (DECL_TEMPLATE_RESULT (function
));
2758 if (code
== CONST_DECL
)
2760 ("enumeral value `%D' conflicts with function of same name",
2762 else if (code
== VAR_DECL
)
2764 if (TREE_STATIC (function
))
2766 ("variable `%D' conflicts with function of same name",
2770 ("constant field `%D' conflicts with function of same name",
2773 else if (code
== TYPE_DECL
)
2776 my_friendly_abort (2);
2777 error ("at this point in file");
2780 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2782 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function
));
2783 tree
*targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2786 i
= type_unification (DECL_TEMPLATE_PARMS (function
), targs
,
2787 TYPE_ARG_TYPES (TREE_TYPE (function
)),
2788 parms
, &template_cost
, 0);
2790 function
= instantiate_template (function
, targs
);
2793 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2795 /* Unconverted template -- failed match. */
2796 cp
->function
= function
;
2798 cp
->h
.code
= EVIL_CODE
;
2802 struct candidate
*cp2
;
2804 /* Check that this decl is not the same as a function that's in
2805 the list due to some template instantiation. */
2808 if (cp2
->function
== function
)
2812 if (cp2
->function
== function
)
2815 function
= DECL_MAIN_VARIANT (function
);
2817 /* Can't use alloca here, since result might be
2818 passed to calling function. */
2819 cp
->h_len
= parmlength
;
2820 cp
->harshness
= (struct harshness_code
*)
2821 oballoc ((parmlength
+ 1) * sizeof (struct harshness_code
));
2823 compute_conversion_costs (function
, parms
, cp
, parmlength
);
2825 /* Make sure this is clear as well. */
2826 cp
->h
.int_penalty
+= template_cost
;
2828 if ((cp
[0].h
.code
& EVIL_CODE
) == 0)
2830 cp
[1].h
.code
= EVIL_CODE
;
2836 if (cp
- candidates
)
2838 tree rval
= error_mark_node
;
2841 cp
[0].h
.code
= EVIL_CODE
;
2842 if (cp
- candidates
> 1)
2844 struct candidate
*best_cp
2845 = ideal_candidate (NULL_TREE
, candidates
,
2846 cp
- candidates
, parms
, parmlength
);
2847 if (best_cp
== (struct candidate
*)0)
2849 if (flags
& LOOKUP_COMPLAIN
)
2851 cp_error ("call of overloaded `%D' is ambiguous", fnname
);
2852 print_n_candidates (candidates
, cp
- candidates
);
2854 return error_mark_node
;
2857 rval
= best_cp
->function
;
2862 if (cp
->h
.code
& EVIL_CODE
)
2864 if (flags
& LOOKUP_COMPLAIN
)
2865 error ("type conversion ambiguous");
2868 rval
= cp
->function
;
2874 return buildxxx
? build_function_call_real (rval
, parms
, 0, flags
)
2875 : build_function_call_real (rval
, parms
, 1, flags
);
2878 if (flags
& LOOKUP_SPECULATIVELY
)
2881 if (flags
& LOOKUP_COMPLAIN
)
2882 report_type_mismatch (cp
, parms
, "function",
2883 decl_as_string (cp
->function
, 1));
2885 return error_mark_node
;
2889 build_overload_call (fnname
, parms
, flags
, final_cp
)
2892 struct candidate
*final_cp
;
2894 return build_overload_call_real (fnname
, parms
, flags
, final_cp
, 0);
2898 build_overload_call_maybe (fnname
, parms
, flags
, final_cp
)
2901 struct candidate
*final_cp
;
2903 return build_overload_call_real (fnname
, parms
, flags
, final_cp
, 1);