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. */
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
39 extern int inhibit_warnings
;
40 extern int flag_assume_nonnull_objects
;
41 extern tree ctor_label
, dtor_label
;
44 extern tree
unary_complex_lvalue ();
46 /* Compute the ease with which a conversion can be performed
47 between an expected and the given type. */
48 static struct harshness_code
convert_harshness ();
50 #define EVIL_RETURN(ARG) ((ARG).code = EVIL_CODE, (ARG))
51 #define STD_RETURN(ARG) ((ARG).code = STD_CODE, (ARG))
52 #define QUAL_RETURN(ARG) ((ARG).code = QUAL_CODE, (ARG))
53 #define TRIVIAL_RETURN(ARG) ((ARG).code = TRIVIAL_CODE, (ARG))
54 #define ZERO_RETURN(ARG) ((ARG).code = 0, (ARG))
56 /* Ordering function for overload resolution. Compare two candidates
59 rank_for_overload (x
, y
)
60 struct candidate
*x
, *y
;
62 if (y
->h
.code
& (EVIL_CODE
|ELLIPSIS_CODE
|USER_CODE
))
63 return y
->h
.code
- x
->h
.code
;
64 if (x
->h
.code
& (EVIL_CODE
|ELLIPSIS_CODE
|USER_CODE
))
67 /* This is set by compute_conversion_costs, for calling a non-const
68 member function from a const member function. */
69 if ((y
->harshness
[0].code
& CONST_CODE
) ^ (x
->harshness
[0].code
& CONST_CODE
))
70 return y
->harshness
[0].code
- x
->harshness
[0].code
;
72 if (y
->h
.code
& STD_CODE
)
74 if (x
->h
.code
& STD_CODE
)
75 return y
->h
.distance
- x
->h
.distance
;
78 if (x
->h
.code
& STD_CODE
)
81 return y
->h
.code
- x
->h
.code
;
84 /* Compare two candidates, argument by argument. */
87 struct candidate
*x
, *y
;
91 if (x
->h_len
!= y
->h_len
)
94 for (i
= 0; i
< x
->h_len
; i
++)
96 if (y
->harshness
[i
].code
- x
->harshness
[i
].code
)
97 return y
->harshness
[i
].code
- x
->harshness
[i
].code
;
98 if ((y
->harshness
[i
].code
& STD_CODE
)
99 && (y
->harshness
[i
].distance
- x
->harshness
[i
].distance
))
100 return y
->harshness
[i
].distance
- x
->harshness
[i
].distance
;
102 /* They're both the same code. Now see if we're dealing with an
103 integral promotion that needs a finer grain of accuracy. */
104 if (y
->harshness
[0].code
& PROMO_CODE
105 && (y
->harshness
[i
].int_penalty
^ x
->harshness
[i
].int_penalty
))
106 return y
->harshness
[i
].int_penalty
- x
->harshness
[i
].int_penalty
;
111 /* TYPE is the type we wish to convert to. PARM is the parameter
112 we have to work with. We use a somewhat arbitrary cost function
113 to measure this conversion. */
114 static struct harshness_code
115 convert_harshness (type
, parmtype
, parm
)
116 register tree type
, parmtype
;
119 struct harshness_code h
;
120 register enum tree_code codel
;
121 register enum tree_code coder
;
128 #ifdef GATHER_STATISTICS
129 n_convert_harshness
++;
132 if (TYPE_PTRMEMFUNC_P (type
))
133 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
134 if (TYPE_PTRMEMFUNC_P (parmtype
))
135 parmtype
= TYPE_PTRMEMFUNC_FN_TYPE (parmtype
);
137 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
)
140 parm
= convert_from_reference (parm
);
141 parmtype
= TREE_TYPE (parmtype
);
145 lvalue
= lvalue_p (parm
);
149 codel
= TREE_CODE (type
);
150 coder
= TREE_CODE (parmtype
);
152 if (TYPE_MAIN_VARIANT (parmtype
) == TYPE_MAIN_VARIANT (type
))
153 return ZERO_RETURN (h
);
155 if (coder
== ERROR_MARK
)
156 return EVIL_RETURN (h
);
158 if (codel
== POINTER_TYPE
&& fntype_p (parmtype
))
161 struct harshness_code h1
, h2
;
163 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
164 type
= TREE_TYPE (type
);
166 if (coder
== POINTER_TYPE
)
168 parmtype
= TREE_TYPE (parmtype
);
169 coder
= TREE_CODE (parmtype
);
172 if (coder
!= TREE_CODE (type
))
173 return EVIL_RETURN (h
);
175 if (type
!= parmtype
&& coder
== METHOD_TYPE
)
177 tree ttl
= TYPE_METHOD_BASETYPE (type
);
178 tree ttr
= TYPE_METHOD_BASETYPE (parmtype
);
180 int b_or_d
= get_base_distance (ttr
, ttl
, 0, 0);
183 b_or_d
= get_base_distance (ttl
, ttr
, 0, 0);
185 return EVIL_RETURN (h
);
186 h
.distance
= -b_or_d
;
192 type
= build_function_type
193 (TREE_TYPE (type
), TREE_CHAIN (TYPE_ARG_TYPES (type
)));
194 parmtype
= build_function_type
195 (TREE_TYPE (parmtype
), TREE_CHAIN (TYPE_ARG_TYPES (parmtype
)));
198 /* We allow the default conversion between function type
199 and pointer-to-function type for free. */
200 if (type
== parmtype
)
204 return EVIL_RETURN (h
);
206 /* Compare return types. */
207 p1
= TREE_TYPE (type
);
208 p2
= TREE_TYPE (parmtype
);
209 h2
= convert_harshness (p1
, p2
, NULL_TREE
);
210 if (h2
.code
& EVIL_CODE
)
213 h1
.code
= TRIVIAL_CODE
;
216 if (h2
.distance
!= 0)
220 /* This only works for pointers. */
221 if (TREE_CODE (p1
) != POINTER_TYPE
222 && TREE_CODE (p1
) != REFERENCE_TYPE
)
223 return EVIL_RETURN (h
);
227 /* Don't die if we happen to be dealing with void*. */
228 if (!IS_AGGR_TYPE (p1
) || !IS_AGGR_TYPE (p2
))
229 return EVIL_RETURN (h
);
231 binfo
= get_binfo (p2
, p1
, 0);
233 binfo
= get_binfo (p1
, p2
, 0);
235 if (! BINFO_OFFSET_ZEROP (binfo
))
238 static int explained
= 0;
240 message_2_types (sorry
, "cannot cast `%s' to `%s' at function call site", p2
, p1
);
242 message_2_types (sorry
, "cannot cast `%s' to `%s' at function call site", p1
, p2
);
245 sorry ("(because pointer values change during conversion)");
247 return EVIL_RETURN (h
);
252 if (h2
.distance
> h1
.distance
)
253 h1
.distance
= h2
.distance
;
255 p1
= TYPE_ARG_TYPES (type
);
256 p2
= TYPE_ARG_TYPES (parmtype
);
257 while (p1
&& TREE_VALUE (p1
) != void_type_node
258 && p2
&& TREE_VALUE (p2
) != void_type_node
)
260 h2
= convert_harshness (TREE_VALUE (p1
), TREE_VALUE (p2
),
262 if (h2
.code
& EVIL_CODE
)
267 /* This only works for pointers and references. */
268 if (TREE_CODE (TREE_VALUE (p1
)) != POINTER_TYPE
269 && TREE_CODE (TREE_VALUE (p1
)) != REFERENCE_TYPE
)
270 return EVIL_RETURN (h
);
271 h2
.distance
= - h2
.distance
;
275 if (h2
.distance
> h1
.distance
)
276 h1
.distance
= h2
.distance
;
277 p1
= TREE_CHAIN (p1
);
278 p2
= TREE_CHAIN (p2
);
285 return EVIL_RETURN (h
);
286 h1
.code
|= ELLIPSIS_CODE
;
291 if (TREE_PURPOSE (p1
) == NULL_TREE
)
292 h1
.code
|= EVIL_CODE
;
296 else if (codel
== POINTER_TYPE
&& coder
== OFFSET_TYPE
)
300 /* Get to the OFFSET_TYPE that this might be. */
301 type
= TREE_TYPE (type
);
303 if (coder
!= TREE_CODE (type
))
304 return EVIL_RETURN (h
);
306 ttl
= TYPE_OFFSET_BASETYPE (type
);
307 ttr
= TYPE_OFFSET_BASETYPE (parmtype
);
313 int b_or_d
= get_base_distance (ttr
, ttl
, 0, 0);
316 b_or_d
= get_base_distance (ttl
, ttr
, 0, 0);
318 return EVIL_RETURN (h
);
319 h
.distance
= -b_or_d
;
326 /* Now test the OFFSET_TYPE's target compatibility. */
327 type
= TREE_TYPE (type
);
328 parmtype
= TREE_TYPE (parmtype
);
331 if (coder
== UNKNOWN_TYPE
)
333 if (codel
== FUNCTION_TYPE
334 || codel
== METHOD_TYPE
335 || (codel
== POINTER_TYPE
336 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
337 || TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)))
338 return TRIVIAL_RETURN (h
);
339 return EVIL_RETURN (h
);
342 if (coder
== VOID_TYPE
)
343 return EVIL_RETURN (h
);
345 if (codel
== BOOLEAN_TYPE
)
347 if (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
)
348 return STD_RETURN (h
);
349 else if (coder
== POINTER_TYPE
|| coder
== OFFSET_TYPE
)
351 /* Make this worse than any conversion to another pointer.
352 FIXME this is how I think the language should work, but it may not
353 end up being how the language is standardized (jason 1/30/95). */
355 return STD_RETURN (h
);
357 return EVIL_RETURN (h
);
360 if (INTEGRAL_CODE_P (codel
))
362 /* Control equivalence of ints an enums. */
364 if (codel
== ENUMERAL_TYPE
365 && flag_int_enum_equivalence
== 0)
367 /* Enums can be converted to ints, but not vice-versa. */
368 if (coder
!= ENUMERAL_TYPE
369 || TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (parmtype
))
370 return EVIL_RETURN (h
);
373 /* else enums and ints (almost) freely interconvert. */
375 if (INTEGRAL_CODE_P (coder
))
377 if (TYPE_MAIN_VARIANT (type
)
378 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
381 #if 0 /* What purpose does this serve? -jason */
382 /* A char, short, wchar_t, etc., should promote to an int if
383 it can handle it, otherwise to an unsigned. So we'll make
385 if (type
!= integer_type_node
)
394 else if (coder
== REAL_TYPE
)
402 if (codel
== REAL_TYPE
)
404 if (coder
== REAL_TYPE
)
406 if (TYPE_MAIN_VARIANT (type
)
407 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
414 else if (INTEGRAL_CODE_P (coder
))
422 /* Convert arrays which have not previously been converted. */
423 if (codel
== ARRAY_TYPE
)
424 codel
= POINTER_TYPE
;
425 if (coder
== ARRAY_TYPE
)
426 coder
= POINTER_TYPE
;
428 /* Conversions among pointers */
429 if (codel
== POINTER_TYPE
&& coder
== POINTER_TYPE
)
431 register tree ttl
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
432 register tree ttr
= TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
));
433 int penalty
= 4 * (ttl
!= ttr
);
435 /* Anything converts to void *. Since this may be `const void *'
436 (etc.) use VOID_TYPE instead of void_type_node. Otherwise, the
437 targets must be the same, except that we do allow (at some cost)
438 conversion between signed and unsigned pointer types. */
440 if ((TREE_CODE (ttl
) == METHOD_TYPE
441 || TREE_CODE (ttl
) == FUNCTION_TYPE
)
442 && TREE_CODE (ttl
) == TREE_CODE (ttr
))
444 if (comptypes (ttl
, ttr
, -1))
446 h
.code
= penalty
? STD_CODE
: 0;
455 if (TREE_CODE (ttl
) != VOID_TYPE
456 && (TREE_CODE (ttr
) != VOID_TYPE
|| !parm
|| !integer_zerop (parm
)))
458 if (TREE_UNSIGNED (ttl
) != TREE_UNSIGNED (ttr
))
460 ttl
= unsigned_type (ttl
);
461 ttr
= unsigned_type (ttr
);
464 if (comp_target_types (ttl
, ttr
, 0) <= 0)
465 return EVIL_RETURN (h
);
468 if (!(TREE_CODE (ttl
) == VOID_TYPE
469 || TREE_CODE (ttr
) == VOID_TYPE
470 || (TREE_UNSIGNED (ttl
) ^ TREE_UNSIGNED (ttr
)
471 && (ttl
= unsigned_type (ttl
),
472 ttr
= unsigned_type (ttr
),
474 || (comp_target_types (ttl
, ttr
, 0) > 0)))
475 return EVIL_RETURN (h
);
478 if (penalty
== 10 || ttr
== ttl
)
480 tree tmp1
= TREE_TYPE (type
), tmp2
= TREE_TYPE (parmtype
);
482 /* If one was unsigned but the other wasn't, then we need to
483 do a standard conversion from T to unsigned T. */
485 h
.code
= PROMO_CODE
; /* was STD_CODE */
489 /* Note conversion from `T*' to `const T*',
490 or `T*' to `volatile T*'. */
492 && ((TYPE_READONLY (tmp1
) != TREE_READONLY (tmp2
))
493 || (TYPE_VOLATILE (tmp1
) != TYPE_VOLATILE (tmp2
))))
501 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
503 int b_or_d
= get_base_distance (ttl
, ttr
, 0, 0);
506 b_or_d
= get_base_distance (ttr
, ttl
, 0, 0);
508 return EVIL_RETURN (h
);
509 h
.distance
= -b_or_d
;
517 /* If converting from a `class*' to a `void*', make it
518 less favorable than any inheritance relationship. */
519 if (TREE_CODE (ttl
) == VOID_TYPE
&& IS_AGGR_TYPE (ttr
))
522 h
.distance
= CLASSTYPE_MAX_DEPTH (ttr
)+1;
525 h
.code
= penalty
? STD_CODE
: PROMO_CODE
;
529 if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
531 /* This is not a bad match, but don't let it beat
532 integer-enum combinations. */
533 if (parm
&& integer_zerop (parm
))
541 /* C++: Since the `this' parameter of a signature member function
542 is represented as a signature pointer to handle default implementations
543 correctly, we can have the case that `type' is a signature pointer
544 while `parmtype' is a pointer to a signature table. We don't really
545 do any conversions in this case, so just return 0. */
547 if (codel
== RECORD_TYPE
&& coder
== POINTER_TYPE
548 && IS_SIGNATURE_POINTER (type
) && IS_SIGNATURE (TREE_TYPE (parmtype
)))
549 return ZERO_RETURN (h
);
551 if (codel
== REFERENCE_TYPE
)
554 int constp
= parm
? TREE_READONLY (parm
) : TYPE_READONLY (parmtype
);
555 int volatilep
= (parm
? TREE_THIS_VOLATILE (parm
)
556 : TYPE_VOLATILE (parmtype
));
557 register tree intype
= TYPE_MAIN_VARIANT (parmtype
);
558 register enum tree_code form
= TREE_CODE (intype
);
561 ttl
= TREE_TYPE (type
);
563 /* Only allow const reference binding if we were given a parm to deal
564 with, since it isn't really a conversion. This is a hack to
565 prevent build_type_conversion from finding this conversion, but
566 still allow overloading to find it. */
567 if (! lvalue
&& ! (parm
&& TYPE_READONLY (ttl
)))
568 return EVIL_RETURN (h
);
570 if (TYPE_READONLY (ttl
) < constp
571 || TYPE_VOLATILE (ttl
) < volatilep
)
572 return EVIL_RETURN (h
);
574 /* When passing a non-const argument into a const reference, dig it a
575 little, so a non-const reference is preferred over this one. */
576 penalty
= ((TYPE_READONLY (ttl
) > constp
)
577 + (TYPE_VOLATILE (ttl
) > volatilep
));
579 ttl
= TYPE_MAIN_VARIANT (ttl
);
581 if (form
== OFFSET_TYPE
)
583 intype
= TREE_TYPE (intype
);
584 form
= TREE_CODE (intype
);
589 /* Maybe handle conversion to base here? */
591 h
= convert_harshness (ttl
, ttr
, NULL_TREE
);
592 if (penalty
&& h
.code
== 0)
595 h
.int_penalty
= penalty
;
599 if (codel
== RECORD_TYPE
&& coder
== RECORD_TYPE
)
601 int b_or_d
= get_base_distance (type
, parmtype
, 0, 0);
604 b_or_d
= get_base_distance (parmtype
, type
, 0, 0);
606 return EVIL_RETURN (h
);
607 h
.distance
= -b_or_d
;
614 return EVIL_RETURN (h
);
618 user_harshness (type
, parmtype
, parm
)
619 register tree type
, parmtype
;
623 tree winner
= NULL_TREE
;
627 tree typename
= build_typename_overload (type
);
628 if (lookup_fnfields (TYPE_BINFO (parmtype
), typename
, 0))
632 for (conv
= lookup_conversions (parmtype
); conv
; conv
= TREE_CHAIN (conv
))
634 struct harshness_code tmp
;
636 if (winner
&& TREE_PURPOSE (winner
) == TREE_PURPOSE (conv
))
639 if (tmp
= convert_harshness (type
, TREE_VALUE (conv
), NULL_TREE
),
640 tmp
.code
< USER_CODE
)
659 can_convert (to
, from
)
662 struct harshness_code h
;
663 h
= convert_harshness (to
, from
, NULL_TREE
);
664 return h
.code
< USER_CODE
;
668 can_convert_arg (to
, from
, arg
)
671 struct harshness_code h
;
672 h
= convert_harshness (to
, from
, arg
);
673 return h
.code
< USER_CODE
;
676 #ifdef DEBUG_MATCHING
679 struct harshness_code
*h
;
681 static char buf
[1024];
684 bzero (buf
, 1024 * sizeof (char));
685 strcat (buf
, "codes=[");
686 if (h
->code
& EVIL_CODE
)
687 strcat (buf
, "EVIL");
688 if (h
->code
& CONST_CODE
)
689 strcat (buf
, " CONST");
690 if (h
->code
& ELLIPSIS_CODE
)
691 strcat (buf
, " ELLIPSIS");
692 if (h
->code
& USER_CODE
)
693 strcat (buf
, " USER");
694 if (h
->code
& STD_CODE
)
695 strcat (buf
, " STD");
696 if (h
->code
& PROMO_CODE
)
697 strcat (buf
, " PROMO");
698 if (h
->code
& QUAL_CODE
)
699 strcat (buf
, " QUAL");
700 if (h
->code
& TRIVIAL_CODE
)
701 strcat (buf
, " TRIVIAL");
705 sprintf (tmp
, "] distance=%d int_penalty=%d", h
->distance
, h
->int_penalty
);
713 /* Algorithm: For each argument, calculate how difficult it is to
714 make FUNCTION accept that argument. If we can easily tell that
715 FUNCTION won't be acceptable to one of the arguments, then we
716 don't need to compute the ease of converting the other arguments,
717 since it will never show up in the intersection of all arguments'
720 Conversions between builtin and user-defined types are allowed, but
721 no function involving such a conversion is preferred to one which
722 does not require such a conversion. Furthermore, such conversions
726 compute_conversion_costs (function
, tta_in
, cp
, arglen
)
729 struct candidate
*cp
;
732 tree ttf_in
= TYPE_ARG_TYPES (TREE_TYPE (function
));
736 /* Start out with no strikes against. */
737 int evil_strikes
= 0;
738 int ellipsis_strikes
= 0;
739 int user_strikes
= 0;
740 int b_or_d_strikes
= 0;
741 int easy_strikes
= 0;
743 int strike_index
= 0, win
;
744 struct harshness_code lose
;
745 extern int cp_silent
;
747 #ifdef GATHER_STATISTICS
748 n_compute_conversion_costs
++;
751 #ifndef DEBUG_MATCHING
752 /* We don't emit any warnings or errors while trying out each candidate. */
756 cp
->function
= function
;
757 cp
->arg
= tta
? TREE_VALUE (tta
) : NULL_TREE
;
758 cp
->u
.bad_arg
= 0; /* optimistic! */
762 cp
->h
.int_penalty
= 0;
763 bzero ((char *) cp
->harshness
,
764 (cp
->h_len
+ 1) * sizeof (struct harshness_code
));
768 struct harshness_code h
;
770 if (ttf
== void_list_node
)
773 if (type_unknown_p (TREE_VALUE (tta
)))
775 /* Must perform some instantiation here. */
776 tree rhs
= TREE_VALUE (tta
);
777 tree lhstype
= TREE_VALUE (ttf
);
779 /* Keep quiet about possible contravariance violations. */
780 int old_inhibit_warnings
= inhibit_warnings
;
781 inhibit_warnings
= 1;
783 /* @@ This is to undo what `grokdeclarator' does to
784 parameter types. It really should go through
785 something more general. */
787 TREE_TYPE (tta
) = unknown_type_node
;
788 rhs
= instantiate_type (lhstype
, rhs
, 0);
789 inhibit_warnings
= old_inhibit_warnings
;
791 if (TREE_CODE (rhs
) == ERROR_MARK
)
794 h
= convert_harshness (lhstype
, TREE_TYPE (rhs
), rhs
);
798 #ifdef DEBUG_MATCHING
799 static tree old_function
= NULL_TREE
;
801 if (!old_function
|| function
!= old_function
)
803 cp_error ("trying %D", function
);
804 old_function
= function
;
807 cp_error (" doing (%T) %E against arg %T",
808 TREE_TYPE (TREE_VALUE (tta
)), TREE_VALUE (tta
),
812 h
= convert_harshness (TREE_VALUE (ttf
),
813 TREE_TYPE (TREE_VALUE (tta
)),
816 #ifdef DEBUG_MATCHING
817 cp_error (" evaluated %s", print_harshness (&h
));
821 cp
->harshness
[strike_index
] = h
;
822 if ((h
.code
& EVIL_CODE
)
823 || ((h
.code
& STD_CODE
) && h
.distance
< 0))
825 cp
->u
.bad_arg
= strike_index
;
828 else if (h
.code
& ELLIPSIS_CODE
)
829 ellipsis_strikes
+= 1;
831 /* This is never set by `convert_harshness'. */
832 else if (h
.code
& USER_CODE
)
839 if ((h
.code
& STD_CODE
) && h
.distance
)
841 if (h
.distance
> b_or_d_strikes
)
842 b_or_d_strikes
= h
.distance
;
845 easy_strikes
+= (h
.code
& (STD_CODE
|PROMO_CODE
|TRIVIAL_CODE
));
846 cp
->h
.code
|= h
.code
;
847 /* Make sure we communicate this. */
848 cp
->h
.int_penalty
+= h
.int_penalty
;
851 ttf
= TREE_CHAIN (ttf
);
852 tta
= TREE_CHAIN (tta
);
858 /* ran out of formals, and parmlist is fixed size. */
859 if (ttf
/* == void_type_node */)
861 cp
->h
.code
= EVIL_CODE
;
868 struct harshness_code h
;
869 int l
= list_length (tta
);
870 ellipsis_strikes
+= l
;
871 h
.code
= ELLIPSIS_CODE
;
875 cp
->harshness
[strike_index
++] = h
;
878 else if (ttf
&& ttf
!= void_list_node
)
880 /* ran out of actuals, and no defaults. */
881 if (TREE_PURPOSE (ttf
) == NULL_TREE
)
883 cp
->h
.code
= EVIL_CODE
;
888 /* Store index of first default. */
889 cp
->harshness
[arglen
].distance
= strike_index
+1;
892 cp
->harshness
[arglen
].distance
= 0;
894 /* Argument list lengths work out, so don't need to check them again. */
897 /* We do not check for derived->base conversions here, since in
898 no case would they give evil strike counts, unless such conversions
899 are somehow ambiguous. */
901 /* See if any user-defined conversions apply.
902 But make sure that we do not loop. */
903 static int dont_convert_types
= 0;
905 if (dont_convert_types
)
907 cp
->h
.code
= EVIL_CODE
;
912 win
= 0; /* Only get one chance to win. */
913 ttf
= TYPE_ARG_TYPES (TREE_TYPE (function
));
920 if (ttf
== void_list_node
)
923 lose
= cp
->harshness
[strike_index
];
924 if ((lose
.code
& EVIL_CODE
)
925 || ((lose
.code
& STD_CODE
) && lose
.distance
< 0))
927 tree actual_type
= TREE_TYPE (TREE_VALUE (tta
));
928 tree formal_type
= TREE_VALUE (ttf
);
929 int extra_conversions
= 0;
931 dont_convert_types
= 1;
933 if (TREE_CODE (formal_type
) == REFERENCE_TYPE
)
934 formal_type
= TREE_TYPE (formal_type
);
935 if (TREE_CODE (actual_type
) == REFERENCE_TYPE
)
936 actual_type
= TREE_TYPE (actual_type
);
938 if (formal_type
!= error_mark_node
939 && actual_type
!= error_mark_node
)
941 formal_type
= TYPE_MAIN_VARIANT (formal_type
);
942 actual_type
= TYPE_MAIN_VARIANT (actual_type
);
944 if (TYPE_HAS_CONSTRUCTOR (formal_type
))
946 /* If it has a constructor for this type,
948 /* @@ There is no way to save this result yet, so
949 success is a NULL_TREE for now. */
950 if (convert_to_aggr (formal_type
, TREE_VALUE (tta
), 0, 1)
954 if (TYPE_LANG_SPECIFIC (actual_type
)
955 && TYPE_HAS_CONVERSION (actual_type
))
957 int extra
= user_harshness (formal_type
, actual_type
);
959 if (extra
== EVIL_CODE
)
964 extra_conversions
= extra
;
968 dont_convert_types
= 0;
973 cp
->harshness
[strike_index
].code
974 = USER_CODE
| (extra_conversions
? STD_CODE
: 0);
979 if (cp
->u
.bad_arg
> strike_index
)
980 cp
->u
.bad_arg
= strike_index
;
982 evil_strikes
= win
? 2 : 1;
987 ttf
= TREE_CHAIN (ttf
);
988 tta
= TREE_CHAIN (tta
);
993 /* Const member functions get a small penalty because defaulting
994 to const is less useful than defaulting to non-const. */
995 /* This is bogus, it does not correspond to anything in the ARM.
996 This code will be fixed when this entire section is rewritten
997 to conform to the ARM. (mrs) */
998 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1000 tree this_parm
= TREE_VALUE (ttf_in
);
1002 if (TREE_CODE (this_parm
) == RECORD_TYPE
/* Is `this' a sig ptr? */
1003 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm
))))
1004 : TYPE_READONLY (TREE_TYPE (this_parm
)))
1006 cp
->harshness
[0].code
|= TRIVIAL_CODE
;
1011 /* Calling a non-const member function from a const member function
1012 is probably invalid, but for now we let it only draw a warning.
1013 We indicate that such a mismatch has occurred by setting the
1014 harshness to a maximum value. */
1015 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in
))) == POINTER_TYPE
1016 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in
))))))
1017 cp
->harshness
[0].code
|= CONST_CODE
;
1022 cp
->h
.code
= EVIL_CODE
;
1023 if (ellipsis_strikes
)
1024 cp
->h
.code
|= ELLIPSIS_CODE
;
1026 cp
->h
.code
|= USER_CODE
;
1028 #ifdef DEBUG_MATCHING
1029 cp_error ("final eval %s", print_harshness (&cp
->h
));
1033 /* Subroutine of ideal_candidate. See if X or Y is a better match
1036 strictly_better (x
, y
)
1037 unsigned short x
, y
;
1045 if (xor >= x
|| xor >= y
)
1050 /* When one of several possible overloaded functions and/or methods
1051 can be called, choose the best candidate for overloading.
1053 BASETYPE is the context from which we start method resolution
1054 or NULL if we are comparing overloaded functions.
1055 CANDIDATES is the array of candidates we have to choose from.
1056 N_CANDIDATES is the length of CANDIDATES.
1057 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1058 choose. It is modified in place when resolving methods. It is not
1059 modified in place when resolving overloaded functions.
1060 LEN is the length of the parameter list. */
1062 static struct candidate
*
1063 ideal_candidate (basetype
, candidates
, n_candidates
, parms
, len
)
1065 struct candidate
*candidates
;
1070 struct candidate
*cp
= candidates
+n_candidates
;
1071 int i
, j
= -1, best_code
;
1073 /* For each argument, sort the functions from best to worst for the arg.
1074 For each function that's not best for this arg, set its overall
1075 harshness to EVIL so that other args won't like it. The candidate
1076 list for the last argument is the intersection of all the best-liked
1080 for (i
= 0; i
< len
; i
++)
1082 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
1084 best_code
= cp
[-1].h
.code
;
1086 /* To find out functions that are worse than that represented
1087 by BEST_CODE, we can't just do a comparison like h.code>best_code.
1088 The total harshness for the "best" fn may be 8|8 for two args, and
1089 the harshness for the next-best may be 8|2. If we just compared,
1090 that would be checking 8>10, which would lead to the next-best
1091 being disqualified. What we actually want to do is get rid
1092 of functions that are definitely worse than that represented
1093 by best_code, i.e. those which have bits set higher than the
1094 highest in best_code. Sooooo, what we do is clear out everything
1095 represented by best_code, and see if we still come up with something
1096 higher. If so (e.g., 8|8 vs 8|16), it'll disqualify it properly. */
1097 for (j
= n_candidates
-2; j
>= 0; j
--)
1098 if ((candidates
[j
].h
.code
& ~best_code
) > best_code
)
1099 candidates
[j
].h
.code
= EVIL_CODE
;
1102 if (cp
[-1].h
.code
& EVIL_CODE
)
1105 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
1107 best_code
= cp
[-1].h
.code
;
1110 /* If they're at least as good as each other, do an arg-by-arg check. */
1111 if (! strictly_better (cp
[-1].h
.code
, cp
[-2].h
.code
))
1116 for (j
= 0; j
< n_candidates
; j
++)
1117 if (! strictly_better (candidates
[j
].h
.code
, best_code
))
1120 qsort (candidates
+j
, n_candidates
-j
, sizeof (struct candidate
),
1122 for (i
= 0; i
< len
; i
++)
1124 if (cp
[-1].harshness
[i
].code
< cp
[-2].harshness
[i
].code
)
1126 else if (cp
[-1].harshness
[i
].code
> cp
[-2].harshness
[i
].code
)
1128 else if (cp
[-1].harshness
[i
].code
& STD_CODE
)
1130 /* If it involves a standard conversion, let the
1131 inheritance lattice be the final arbiter. */
1132 if (cp
[-1].harshness
[i
].distance
> cp
[-2].harshness
[i
].distance
)
1134 else if (cp
[-1].harshness
[i
].distance
< cp
[-2].harshness
[i
].distance
)
1137 else if (cp
[-1].harshness
[i
].code
& PROMO_CODE
)
1139 /* For integral promotions, take into account a finer
1140 granularity for determining which types should be favored
1141 over others in such promotions. */
1142 if (cp
[-1].harshness
[i
].int_penalty
> cp
[-2].harshness
[i
].int_penalty
)
1144 else if (cp
[-1].harshness
[i
].int_penalty
< cp
[-2].harshness
[i
].int_penalty
)
1149 if (! better
|| worse
)
1155 /* Assume that if the class referred to is not in the
1156 current class hierarchy, that it may be remote.
1157 PARENT is assumed to be of aggregate type here. */
1159 may_be_remote (parent
)
1162 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent
) == 0)
1165 if (current_class_type
== NULL_TREE
)
1168 if (parent
== current_class_type
)
1171 if (UNIQUELY_DERIVED_FROM_P (parent
, current_class_type
))
1177 build_vfield_ref (datum
, type
)
1181 int old_assume_nonnull_objects
= flag_assume_nonnull_objects
;
1183 if (datum
== error_mark_node
)
1184 return error_mark_node
;
1186 /* Vtable references are always made from non-null objects. */
1187 flag_assume_nonnull_objects
= 1;
1188 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
1189 datum
= convert_from_reference (datum
);
1191 if (! TYPE_USES_COMPLEX_INHERITANCE (type
))
1192 rval
= build (COMPONENT_REF
, TREE_TYPE (CLASSTYPE_VFIELD (type
)),
1193 datum
, CLASSTYPE_VFIELD (type
));
1195 rval
= build_component_ref (datum
, DECL_NAME (CLASSTYPE_VFIELD (type
)), 0, 0);
1196 flag_assume_nonnull_objects
= old_assume_nonnull_objects
;
1201 /* Build a call to a member of an object. I.e., one that overloads
1202 operator ()(), or is a pointer-to-function or pointer-to-method. */
1204 build_field_call (basetype_path
, instance_ptr
, name
, parms
)
1205 tree basetype_path
, instance_ptr
, name
, parms
;
1207 tree field
, instance
;
1209 if (instance_ptr
== current_class_decl
)
1211 /* Check to see if we really have a reference to an instance variable
1212 with `operator()()' overloaded. */
1213 field
= IDENTIFIER_CLASS_VALUE (name
);
1215 if (field
== NULL_TREE
)
1217 cp_error ("`this' has no member named `%D'", name
);
1218 return error_mark_node
;
1221 if (TREE_CODE (field
) == FIELD_DECL
)
1223 /* If it's a field, try overloading operator (),
1224 or calling if the field is a pointer-to-function. */
1225 instance
= build_component_ref_1 (C_C_D
, field
, 0);
1226 if (instance
== error_mark_node
)
1227 return error_mark_node
;
1229 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1230 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance
)))
1231 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, instance
, parms
, NULL_TREE
);
1233 if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1235 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == FUNCTION_TYPE
)
1236 return build_function_call (instance
, parms
);
1237 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == METHOD_TYPE
)
1238 return build_function_call (instance
, tree_cons (NULL_TREE
, current_class_decl
, parms
));
1244 /* Check to see if this is not really a reference to an instance variable
1245 with `operator()()' overloaded. */
1246 field
= lookup_field (basetype_path
, name
, 1, 0);
1248 /* This can happen if the reference was ambiguous or for access
1250 if (field
== error_mark_node
)
1251 return error_mark_node
;
1256 tree ftype
= TREE_TYPE (field
);
1258 if (TREE_CODE (ftype
) == REFERENCE_TYPE
)
1259 ftype
= TREE_TYPE (ftype
);
1261 if (TYPE_LANG_SPECIFIC (ftype
) && TYPE_OVERLOADS_CALL_EXPR (ftype
))
1263 /* Make the next search for this field very short. */
1264 basetype
= DECL_FIELD_CONTEXT (field
);
1265 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
1267 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1268 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
1269 build_component_ref_1 (instance
, field
, 0),
1272 if (TREE_CODE (ftype
) == POINTER_TYPE
)
1274 if (TREE_CODE (TREE_TYPE (ftype
)) == FUNCTION_TYPE
1275 || TREE_CODE (TREE_TYPE (ftype
)) == METHOD_TYPE
)
1277 /* This is a member which is a pointer to function. */
1279 = build_component_ref_1 (build_indirect_ref (instance_ptr
,
1281 field
, LOOKUP_COMPLAIN
);
1282 if (ref
== error_mark_node
)
1283 return error_mark_node
;
1284 return build_function_call (ref
, parms
);
1287 else if (TREE_CODE (ftype
) == METHOD_TYPE
)
1289 error ("invalid call via pointer-to-member function");
1290 return error_mark_node
;
1299 find_scoped_type (type
, inner_name
, inner_types
)
1300 tree type
, inner_name
, inner_types
;
1302 tree tags
= CLASSTYPE_TAGS (type
);
1306 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1307 enclosing class) is set to the name for the enum type. So, if
1308 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1309 then this test will be true. */
1310 if (TREE_PURPOSE (tags
) == inner_name
)
1312 if (inner_types
== NULL_TREE
)
1313 return DECL_NESTED_TYPENAME (TYPE_NAME (TREE_VALUE (tags
)));
1314 return resolve_scope_to_name (TREE_VALUE (tags
), inner_types
);
1316 tags
= TREE_CHAIN (tags
);
1320 /* XXX This needs to be fixed better. */
1321 if (TREE_CODE (type
) == UNINSTANTIATED_P_TYPE
)
1323 sorry ("nested class lookup in template type");
1328 /* Look for a TYPE_DECL. */
1329 for (tags
= TYPE_FIELDS (type
); tags
; tags
= TREE_CHAIN (tags
))
1330 if (TREE_CODE (tags
) == TYPE_DECL
&& DECL_NAME (tags
) == inner_name
)
1332 /* Code by raeburn. */
1333 if (inner_types
== NULL_TREE
)
1334 return DECL_NESTED_TYPENAME (tags
);
1335 return resolve_scope_to_name (TREE_TYPE (tags
), inner_types
);
1341 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1342 the name that names the above nested type. INNER_TYPES
1343 is a chain of nested type names (held together by SCOPE_REFs);
1344 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1345 Returns NULL_TREE if there is an error. */
1347 resolve_scope_to_name (outer_type
, inner_stuff
)
1348 tree outer_type
, inner_stuff
;
1351 tree inner_name
, inner_type
;
1353 if (outer_type
== NULL_TREE
&& current_class_type
!= NULL_TREE
)
1355 /* We first try to look for a nesting in our current class context,
1356 then try any enclosing classes. */
1357 tree type
= current_class_type
;
1359 while (type
&& (TREE_CODE (type
) == RECORD_TYPE
1360 || TREE_CODE (type
) == UNION_TYPE
))
1362 tree rval
= resolve_scope_to_name (type
, inner_stuff
);
1364 if (rval
!= NULL_TREE
)
1366 type
= DECL_CONTEXT (TYPE_NAME (type
));
1370 if (TREE_CODE (inner_stuff
) == SCOPE_REF
)
1372 inner_name
= TREE_OPERAND (inner_stuff
, 0);
1373 inner_type
= TREE_OPERAND (inner_stuff
, 1);
1377 inner_name
= inner_stuff
;
1378 inner_type
= NULL_TREE
;
1381 if (outer_type
== NULL_TREE
)
1383 /* If we have something that's already a type by itself,
1385 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name
))
1388 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name
),
1395 if (! IS_AGGR_TYPE (outer_type
))
1398 /* Look for member classes or enums. */
1399 tmp
= find_scoped_type (outer_type
, inner_name
, inner_type
);
1401 /* If it's not a type in this class, then go down into the
1402 base classes and search there. */
1403 if (! tmp
&& TYPE_BINFO (outer_type
))
1405 tree binfos
= TYPE_BINFO_BASETYPES (outer_type
);
1406 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1408 for (i
= 0; i
< n_baselinks
; i
++)
1410 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1411 tmp
= resolve_scope_to_name (BINFO_TYPE (base_binfo
), inner_stuff
);
1421 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1422 This is how virtual function calls are avoided. */
1424 build_scoped_method_call (exp
, scopes
, name
, parms
)
1425 tree exp
, scopes
, name
, parms
;
1427 /* Because this syntactic form does not allow
1428 a pointer to a base class to be `stolen',
1429 we need not protect the derived->base conversion
1432 @@ But we do have to check access privileges later. */
1433 tree basename
= resolve_scope_to_name (NULL_TREE
, scopes
);
1434 tree basetype
, binfo
, decl
;
1435 tree type
= TREE_TYPE (exp
);
1437 if (type
== error_mark_node
1438 || basename
== NULL_TREE
)
1439 return error_mark_node
;
1441 basetype
= IDENTIFIER_TYPE_VALUE (basename
);
1443 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1444 type
= TREE_TYPE (type
);
1446 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1447 that explicit ~int is caught in the parser; this deals with typedefs
1448 and template parms. */
1449 if (TREE_CODE (name
) == BIT_NOT_EXPR
&& ! is_aggr_typedef (basename
, 0))
1451 if (type
!= basetype
)
1452 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1453 exp
, basetype
, type
);
1454 name
= TREE_OPERAND (name
, 0);
1455 if (basetype
!= get_type_value (name
))
1456 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1458 return convert (void_type_node
, exp
);
1461 if (! is_aggr_typedef (basename
, 1))
1462 return error_mark_node
;
1464 if (! IS_AGGR_TYPE (type
))
1466 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1468 return error_mark_node
;
1471 if ((binfo
= binfo_or_else (basetype
, type
)))
1473 if (binfo
== error_mark_node
)
1474 return error_mark_node
;
1475 if (TREE_CODE (exp
) == INDIRECT_REF
)
1476 decl
= build_indirect_ref (convert_pointer_to (binfo
,
1477 build_unary_op (ADDR_EXPR
, exp
, 0)), NULL_PTR
);
1479 decl
= build_scoped_ref (exp
, scopes
);
1481 /* Call to a destructor. */
1482 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1484 /* Explicit call to destructor. */
1485 name
= TREE_OPERAND (name
, 0);
1486 if (! (name
== constructor_name (TREE_TYPE (decl
))
1487 || TREE_TYPE (decl
) == get_type_value (name
)))
1490 ("qualified type `%T' does not match destructor name `~%T'",
1491 TREE_TYPE (decl
), name
);
1492 return error_mark_node
;
1494 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
1495 return convert (void_type_node
, exp
);
1497 return build_delete (TREE_TYPE (decl
), decl
, integer_two_node
,
1498 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
1502 /* Call to a method. */
1503 return build_method_call (decl
, name
, parms
, binfo
,
1504 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
1506 return error_mark_node
;
1510 print_candidates (candidates
)
1513 cp_error_at ("candidates are: %D", TREE_VALUE (candidates
));
1514 candidates
= TREE_CHAIN (candidates
);
1518 cp_error_at (" %D", TREE_VALUE (candidates
));
1519 candidates
= TREE_CHAIN (candidates
);
1524 print_n_candidates (candidates
, n
)
1525 struct candidate
*candidates
;
1530 cp_error_at ("candidates are: %D", candidates
[0].function
);
1531 for (i
= 1; i
< n
; i
++)
1532 cp_error_at (" %D", candidates
[i
].function
);
1535 /* Build something of the form ptr->method (args)
1536 or object.method (args). This can also build
1537 calls to constructors, and find friends.
1539 Member functions always take their class variable
1542 INSTANCE is a class instance.
1544 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1546 PARMS help to figure out what that NAME really refers to.
1548 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1549 down to the real instance type to use for access checking. We need this
1550 information to get protected accesses correct. This parameter is used
1551 by build_member_call.
1553 FLAGS is the logical disjunction of zero or more LOOKUP_
1554 flags. See cp-tree.h for more info.
1556 If this is all OK, calls build_function_call with the resolved
1559 This function must also handle being called to perform
1560 initialization, promotion/coercion of arguments, and
1561 instantiation of default parameters.
1563 Note that NAME may refer to an instance variable name. If
1564 `operator()()' is defined for the type of that field, then we return
1567 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
1568 tree instance
, name
, parms
, basetype_path
;
1571 register tree function
, fntype
, value_type
;
1572 register tree basetype
, save_basetype
;
1573 register tree baselink
, result
, method_name
, parmtypes
, parm
;
1576 enum access_type access
= access_public
;
1578 /* Range of cases for vtable optimization. */
1579 enum vtable_needs
{ not_needed
, maybe_needed
, unneeded
, needed
};
1580 enum vtable_needs need_vtbl
= not_needed
;
1584 tree instance_ptr
= NULL_TREE
;
1585 int all_virtual
= flag_all_virtual
;
1586 int static_call_context
= 0;
1587 tree found_fns
= NULL_TREE
;
1589 /* Keep track of `const' and `volatile' objects. */
1590 int constp
, volatilep
;
1592 #ifdef GATHER_STATISTICS
1593 n_build_method_call
++;
1596 if (instance
== error_mark_node
1597 || name
== error_mark_node
1598 || parms
== error_mark_node
1599 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
1600 return error_mark_node
;
1602 /* This is the logic that magically deletes the second argument to
1603 operator delete, if it is not needed. */
1604 if (name
== ansi_opname
[(int) DELETE_EXPR
] && list_length (parms
)==2)
1606 tree save_last
= TREE_CHAIN (parms
);
1608 /* get rid of unneeded argument */
1609 TREE_CHAIN (parms
) = NULL_TREE
;
1610 result
= build_method_call (instance
, name
, parms
, basetype_path
,
1611 (LOOKUP_SPECULATIVELY
|flags
)
1613 /* If it finds a match, return it. */
1615 return build_method_call (instance
, name
, parms
, basetype_path
, flags
);
1616 /* If it doesn't work, two argument delete must work */
1617 TREE_CHAIN (parms
) = save_last
;
1619 /* We already know whether it's needed or not for vec delete. */
1620 else if (name
== ansi_opname
[(int) VEC_DELETE_EXPR
]
1621 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance
)))
1622 TREE_CHAIN (parms
) = NULL_TREE
;
1624 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1626 flags
|= LOOKUP_DESTRUCTOR
;
1627 name
= TREE_OPERAND (name
, 0);
1629 error ("destructors take no parameters");
1630 basetype
= TREE_TYPE (instance
);
1631 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1632 basetype
= TREE_TYPE (basetype
);
1633 if (! ((IS_AGGR_TYPE (basetype
)
1634 && name
== constructor_name (basetype
))
1635 || basetype
== get_type_value (name
)))
1637 cp_error ("destructor name `~%D' does not match type `%T' of expression",
1639 return convert (void_type_node
, instance
);
1642 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1643 return convert (void_type_node
, instance
);
1644 instance
= default_conversion (instance
);
1645 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1646 return build_delete (build_pointer_type (basetype
),
1647 instance_ptr
, integer_two_node
,
1648 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
1654 /* Initialize name for error reporting. */
1655 if (IDENTIFIER_OPNAME_P (name
) && ! IDENTIFIER_TYPENAME_P (name
))
1657 char *p
= operator_name_string (name
);
1658 xref_name
= (char *)alloca (strlen (p
) + 10);
1659 sprintf (xref_name
, "operator %s", p
);
1661 else if (TREE_CODE (name
) == SCOPE_REF
)
1662 xref_name
= IDENTIFIER_POINTER (TREE_OPERAND (name
, 1));
1664 xref_name
= IDENTIFIER_POINTER (name
);
1666 GNU_xref_call (current_function_decl
, xref_name
);
1669 if (instance
== NULL_TREE
)
1671 basetype
= NULL_TREE
;
1672 /* Check cases where this is really a call to raise
1674 if (current_class_type
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
1676 basetype
= purpose_member (name
, CLASSTYPE_TAGS (current_class_type
));
1678 basetype
= TREE_VALUE (basetype
);
1680 else if (TREE_CODE (name
) == SCOPE_REF
1681 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
1683 if (! is_aggr_typedef (TREE_OPERAND (name
, 0), 1))
1684 return error_mark_node
;
1685 basetype
= purpose_member (TREE_OPERAND (name
, 1),
1686 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name
, 0))));
1688 basetype
= TREE_VALUE (basetype
);
1691 if (basetype
!= NULL_TREE
)
1693 /* call to a constructor... */
1694 else if (basetype_path
)
1695 basetype
= BINFO_TYPE (basetype_path
);
1696 else if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1698 basetype
= IDENTIFIER_TYPE_VALUE (name
);
1699 name
= constructor_name_full (basetype
);
1703 tree typedef_name
= lookup_name (name
, 1);
1704 if (typedef_name
&& TREE_CODE (typedef_name
) == TYPE_DECL
)
1706 /* Canonicalize the typedef name. */
1707 basetype
= TREE_TYPE (typedef_name
);
1708 name
= TYPE_IDENTIFIER (basetype
);
1712 cp_error ("no constructor named `%T' in scope",
1714 return error_mark_node
;
1718 if (! IS_AGGR_TYPE (basetype
))
1721 if ((flags
& LOOKUP_COMPLAIN
) && TREE_CODE (basetype
) != ERROR_MARK
)
1722 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1723 name
, instance
, basetype
);
1725 return error_mark_node
;
1728 else if (instance
== C_C_D
|| instance
== current_class_decl
)
1730 /* When doing initialization, we side-effect the TREE_TYPE of
1731 C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1732 basetype
= TREE_TYPE (C_C_D
);
1734 /* Anything manifestly `this' in constructors and destructors
1735 has a known type, so virtual function tables are not needed. */
1736 if (TYPE_VIRTUAL_P (basetype
)
1737 && !(flags
& LOOKUP_NONVIRTUAL
))
1738 need_vtbl
= (dtor_label
|| ctor_label
)
1739 ? unneeded
: maybe_needed
;
1741 /* If `this' is a signature pointer and `name' is not a constructor,
1742 we are calling a signature member function. In that case, set the
1743 `basetype' to the signature type and dereference the `optr' field. */
1744 if (IS_SIGNATURE_POINTER (basetype
)
1745 && TYPE_IDENTIFIER (basetype
) != name
)
1747 basetype
= SIGNATURE_TYPE (basetype
);
1748 instance_ptr
= build_optr_ref (instance
);
1749 instance_ptr
= convert (TYPE_POINTER_TO (basetype
), instance_ptr
);
1750 basetype_path
= TYPE_BINFO (basetype
);
1755 instance_ptr
= current_class_decl
;
1756 basetype_path
= TYPE_BINFO (current_class_type
);
1758 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1763 else if (TREE_CODE (instance
) == RESULT_DECL
)
1765 basetype
= TREE_TYPE (instance
);
1766 /* Should we ever have to make a virtual function reference
1767 from a RESULT_DECL, know that it must be of fixed type
1768 within the scope of this function. */
1769 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1770 need_vtbl
= maybe_needed
;
1771 instance_ptr
= build1 (ADDR_EXPR
, TYPE_POINTER_TO (basetype
), instance
);
1775 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1776 tree inst_ptr_basetype
;
1778 static_call_context
=
1779 (TREE_CODE (instance
) == INDIRECT_REF
1780 && TREE_CODE (TREE_OPERAND (instance
, 0)) == NOP_EXPR
1781 && TREE_OPERAND (TREE_OPERAND (instance
, 0), 0) == error_mark_node
);
1783 if (TREE_CODE (instance
) == OFFSET_REF
)
1784 instance
= resolve_offset_ref (instance
);
1786 /* the base type of an instance variable is pointer to class */
1787 basetype
= TREE_TYPE (instance
);
1789 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1791 basetype
= TREE_TYPE (basetype
);
1792 if (! IS_AGGR_TYPE (basetype
))
1793 goto non_aggr_error
;
1794 /* Call to convert not needed because we are remaining
1795 within the same type. */
1796 instance_ptr
= build1 (NOP_EXPR
, build_pointer_type (basetype
),
1798 inst_ptr_basetype
= TYPE_MAIN_VARIANT (basetype
);
1802 if (! IS_AGGR_TYPE (basetype
)
1803 && ! (TYPE_LANG_SPECIFIC (basetype
)
1804 && (IS_SIGNATURE_POINTER (basetype
)
1805 || IS_SIGNATURE_REFERENCE (basetype
))))
1806 goto non_aggr_error
;
1808 /* If `instance' is a signature pointer/reference and `name' is
1809 not a constructor, we are calling a signature member function.
1810 In that case set the `basetype' to the signature type. */
1811 if ((IS_SIGNATURE_POINTER (basetype
)
1812 || IS_SIGNATURE_REFERENCE (basetype
))
1813 && TYPE_IDENTIFIER (basetype
) != name
)
1814 basetype
= SIGNATURE_TYPE (basetype
);
1816 if ((IS_SIGNATURE (basetype
)
1817 && (instance_ptr
= build_optr_ref (instance
)))
1818 || (lvalue_p (instance
)
1819 && (instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0)))
1820 || (instance_ptr
= unary_complex_lvalue (ADDR_EXPR
, instance
)))
1822 if (instance_ptr
== error_mark_node
)
1823 return error_mark_node
;
1825 else if (TREE_CODE (instance
) == NOP_EXPR
1826 || TREE_CODE (instance
) == CONSTRUCTOR
)
1828 /* A cast is not an lvalue. Initialize a fresh temp
1829 with the value we are casting from, and proceed with
1830 that temporary. We can't cast to a reference type,
1831 so that simplifies the initialization to something
1833 tree temp
= get_temp_name (TREE_TYPE (instance
), 0);
1834 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
1835 expand_aggr_init (temp
, instance
, 0, flags
);
1838 store_init_value (temp
, instance
);
1839 expand_decl_init (temp
);
1842 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1846 if (TREE_CODE (instance
) != CALL_EXPR
)
1847 my_friendly_abort (125);
1848 if (TYPE_NEEDS_CONSTRUCTING (basetype
))
1849 instance
= build_cplus_new (basetype
, instance
, 0);
1852 instance
= get_temp_name (basetype
, 0);
1853 TREE_ADDRESSABLE (instance
) = 1;
1855 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1857 /* @@ Should we call comp_target_types here? */
1858 inst_ptr_basetype
= TREE_TYPE (TREE_TYPE (instance_ptr
));
1859 if (TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1860 basetype
= inst_ptr_basetype
;
1863 instance_ptr
= convert (TYPE_POINTER_TO (basetype
), instance_ptr
);
1864 if (instance_ptr
== error_mark_node
)
1865 return error_mark_node
;
1869 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1870 not updated, so we use `basetype' instead. */
1871 if (basetype_path
== NULL_TREE
1872 && IS_SIGNATURE (basetype
))
1873 basetype_path
= TYPE_BINFO (basetype
);
1874 else if (basetype_path
== NULL_TREE
||
1875 BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1876 basetype_path
= TYPE_BINFO (inst_ptr_basetype
);
1878 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1882 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1884 if (TREE_SIDE_EFFECTS (instance_ptr
))
1886 /* This action is needed because the instance is needed
1887 for providing the base of the virtual function table.
1888 Without using a SAVE_EXPR, the function we are building
1889 may be called twice, or side effects on the instance
1890 variable (such as a post-increment), may happen twice. */
1891 instance_ptr
= save_expr (instance_ptr
);
1892 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1894 else if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1896 /* This happens when called for operator new (). */
1897 instance
= build_indirect_ref (instance
, NULL_PTR
);
1900 need_vtbl
= maybe_needed
;
1904 if (TYPE_SIZE (basetype
) == 0)
1906 /* This is worth complaining about, I think. */
1907 cp_error ("cannot lookup method in incomplete type `%T'", basetype
);
1908 return error_mark_node
;
1911 save_basetype
= TYPE_MAIN_VARIANT (basetype
);
1914 if (all_virtual
== 1
1915 && (! strncmp (IDENTIFIER_POINTER (name
), OPERATOR_METHOD_FORMAT
,
1916 OPERATOR_METHOD_LENGTH
)
1917 || instance_ptr
== NULL_TREE
1918 || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype
) == 0)))
1923 for (parmtypes
= NULL_TREE
, parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1925 tree t
= TREE_TYPE (TREE_VALUE (parm
));
1926 if (TREE_CODE (t
) == OFFSET_TYPE
)
1928 /* Convert OFFSET_TYPE entities to their normal selves. */
1929 TREE_VALUE (parm
) = resolve_offset_ref (TREE_VALUE (parm
));
1930 t
= TREE_TYPE (TREE_VALUE (parm
));
1932 if (TREE_CODE (TREE_VALUE (parm
)) == OFFSET_REF
1933 && TREE_CODE (t
) == METHOD_TYPE
)
1935 TREE_VALUE (parm
) = build_unary_op (ADDR_EXPR
, TREE_VALUE (parm
), 0);
1938 /* This breaks reference-to-array parameters. */
1939 if (TREE_CODE (t
) == ARRAY_TYPE
)
1941 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
1942 This eliminates needless calls to `compute_conversion_costs'. */
1943 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
1944 t
= TREE_TYPE (TREE_VALUE (parm
));
1947 if (t
== error_mark_node
)
1948 return error_mark_node
;
1949 last
= build_tree_list (NULL_TREE
, t
);
1950 parmtypes
= chainon (parmtypes
, last
);
1955 /* TREE_READONLY (instance) fails for references. */
1956 constp
= TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr
)));
1957 volatilep
= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr
)));
1958 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
1962 /* Raw constructors are always in charge. */
1963 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
1964 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
1966 flags
|= LOOKUP_HAS_IN_CHARGE
;
1967 parms
= tree_cons (NULL_TREE
, integer_one_node
, parms
);
1968 parmtypes
= tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
1971 if (flag_this_is_variable
> 0)
1975 instance_ptr
= build_int_2 (0, 0);
1976 TREE_TYPE (instance_ptr
) = TYPE_POINTER_TO (basetype
);
1977 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
1983 instance_ptr
= build_new (NULL_TREE
, basetype
, void_type_node
, 0);
1984 if (instance_ptr
== error_mark_node
)
1985 return error_mark_node
;
1986 instance_ptr
= save_expr (instance_ptr
);
1987 TREE_CALLS_NEW (instance_ptr
) = 1;
1988 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1991 /* This breaks initialization of a reference from a new
1992 expression of a different type. And it doesn't appear to
1993 serve its original purpose any more, either. jason 10/12/94 */
1994 /* If it's a default argument initialized from a ctor, what we get
1995 from instance_ptr will match the arglist for the FUNCTION_DECL
1996 of the constructor. */
1997 if (parms
&& TREE_CODE (TREE_VALUE (parms
)) == CALL_EXPR
1998 && TREE_OPERAND (TREE_VALUE (parms
), 1)
1999 && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms
), 1))))
2000 parms
= build_tree_list (NULL_TREE
, instance_ptr
);
2003 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2007 parmtypes
= tree_cons (NULL_TREE
, TREE_TYPE (instance_ptr
), parmtypes
);
2009 if (last
== NULL_TREE
)
2012 /* Look up function name in the structure type definition. */
2014 if ((IDENTIFIER_HAS_TYPE_VALUE (name
)
2015 && ! IDENTIFIER_OPNAME_P (name
)
2016 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name
))
2017 && TREE_CODE (IDENTIFIER_TYPE_VALUE (name
)) != UNINSTANTIATED_P_TYPE
)
2018 || name
== constructor_name (basetype
))
2020 tree tmp
= NULL_TREE
;
2021 if (IDENTIFIER_TYPE_VALUE (name
) == basetype
2022 || name
== constructor_name (basetype
))
2023 tmp
= TYPE_BINFO (basetype
);
2025 tmp
= get_binfo (IDENTIFIER_TYPE_VALUE (name
), basetype
, 0);
2027 if (tmp
!= NULL_TREE
)
2029 name_kind
= "constructor";
2031 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
2032 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
2034 /* Constructors called for initialization
2035 only are never in charge. */
2038 flags
|= LOOKUP_HAS_IN_CHARGE
;
2039 tmplist
= tree_cons (NULL_TREE
, integer_zero_node
,
2040 TREE_CHAIN (parms
));
2041 TREE_CHAIN (parms
) = tmplist
;
2042 tmplist
= tree_cons (NULL_TREE
, integer_type_node
, TREE_CHAIN (parmtypes
));
2043 TREE_CHAIN (parmtypes
) = tmplist
;
2045 basetype
= BINFO_TYPE (tmp
);
2048 name_kind
= "method";
2051 name_kind
= "method";
2053 if (basetype_path
== NULL_TREE
2054 || BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (basetype
))
2055 basetype_path
= TYPE_BINFO (basetype
);
2056 result
= lookup_fnfields (basetype_path
, name
,
2057 (flags
& LOOKUP_COMPLAIN
));
2058 if (result
== error_mark_node
)
2059 return error_mark_node
;
2063 /* Now, go look for this method name. We do not find destructors here.
2065 Putting `void_list_node' on the end of the parmtypes
2066 fakes out `build_decl_overload' into doing the right thing. */
2067 TREE_CHAIN (last
) = void_list_node
;
2068 method_name
= build_decl_overload (name
, parmtypes
,
2069 1 + (name
== constructor_name (save_basetype
)
2070 || name
== constructor_name_full (save_basetype
)));
2071 TREE_CHAIN (last
) = NULL_TREE
;
2074 for (pass
= 0; pass
< 2; pass
++)
2076 struct candidate
*candidates
;
2077 struct candidate
*cp
;
2081 /* This increments every time we go up the type hierarchy.
2082 The idea is to prefer a function of the derived class if possible. */
2090 = (struct candidate
*) alloca ((ever_seen
+1)
2091 * sizeof (struct candidate
));
2092 bzero ((char *) candidates
, (ever_seen
+ 1) * sizeof (struct candidate
));
2094 len
= list_length (parms
);
2097 /* First see if a global function has a shot at it. */
2098 if (flags
& LOOKUP_GLOBAL
)
2101 tree parm
= instance_ptr
;
2103 if (TREE_CODE (TREE_TYPE (parm
)) == REFERENCE_TYPE
)
2104 parm
= convert_from_reference (parm
);
2105 else if (TREE_CODE (TREE_TYPE (parm
)) == POINTER_TYPE
)
2106 parm
= build_indirect_ref (parm
, "friendifying parms (compiler error)");
2108 my_friendly_abort (167);
2110 friend_parms
= tree_cons (NULL_TREE
, parm
, TREE_CHAIN (parms
));
2113 cp
->harshness
= (struct harshness_code
*)
2114 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2116 result
= build_overload_call (name
, friend_parms
, 0, cp
);
2117 /* If it turns out to be the one we were actually looking for
2118 (it was probably a friend function), the return the
2120 if (TREE_CODE (result
) == CALL_EXPR
)
2123 while ((cp
->h
.code
& EVIL_CODE
) == 0)
2125 /* non-standard uses: set the field to 0 to indicate
2126 we are using a non-member function. */
2128 if (cp
->harshness
[len
].distance
== 0
2129 && cp
->h
.code
< best
)
2138 /* We have a hit (of sorts). If the parameter list is
2139 "error_mark_node", or some variant thereof, it won't
2140 match any methods. Since we have verified that the is
2141 some method vaguely matching this one (in name at least),
2144 Don't stop for friends, however. */
2145 basetype_path
= TREE_PURPOSE (baselink
);
2147 function
= TREE_VALUE (baselink
);
2148 if (TREE_CODE (basetype_path
) == TREE_LIST
)
2149 basetype_path
= TREE_VALUE (basetype_path
);
2150 basetype
= BINFO_TYPE (basetype_path
);
2153 /* Cast the instance variable if necessary. */
2154 if (basetype
!= TYPE_MAIN_VARIANT
2155 (TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)))))
2157 if (basetype
== save_basetype
)
2158 TREE_VALUE (parms
) = instance_ptr
;
2161 tree type
= build_pointer_type
2162 (build_type_variant (basetype
, constp
, volatilep
));
2163 TREE_VALUE (parms
) = convert_force (type
, instance_ptr
, 0);
2167 /* FIXME: this is the wrong place to get an error. Hopefully
2168 the access-control rewrite will make this change more cleanly. */
2169 if (TREE_VALUE (parms
) == error_mark_node
)
2170 return error_mark_node
;
2173 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function
)))
2174 function
= DECL_CHAIN (function
);
2176 for (; function
; function
= DECL_CHAIN (function
))
2178 #ifdef GATHER_STATISTICS
2179 n_inner_fields_searched
++;
2183 found_fns
= tree_cons (NULL_TREE
, function
, found_fns
);
2185 /* Not looking for friends here. */
2186 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
2187 && ! DECL_STATIC_FUNCTION_P (function
))
2192 && DECL_ASSEMBLER_NAME (function
) == method_name
)
2198 tree these_parms
= parms
;
2200 #ifdef GATHER_STATISTICS
2201 n_inner_fields_searched
++;
2204 cp
->harshness
= (struct harshness_code
*)
2205 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2207 if (DECL_STATIC_FUNCTION_P (function
))
2208 these_parms
= TREE_CHAIN (these_parms
);
2209 compute_conversion_costs (function
, these_parms
, cp
, len
);
2211 if ((cp
->h
.code
& EVIL_CODE
) == 0)
2213 cp
->u
.field
= function
;
2214 cp
->function
= function
;
2215 cp
->basetypes
= basetype_path
;
2217 /* Don't allow non-converting constructors to convert. */
2218 if (flags
& LOOKUP_ONLYCONVERTING
2219 && DECL_LANG_SPECIFIC (function
)
2220 && DECL_NONCONVERTING_P (function
))
2223 /* No "two-level" conversions. */
2224 if (flags
& LOOKUP_NO_CONVERSION
2225 && (cp
->h
.code
& USER_CODE
))
2232 /* Now we have run through one link's member functions.
2233 arrange to head-insert this link's links. */
2234 baselink
= next_baselink (baselink
);
2236 /* Don't grab functions from base classes. lookup_fnfield will
2237 do the work to get us down into the right place. */
2238 baselink
= NULL_TREE
;
2242 tree igv
= lookup_name_nonclass (name
);
2244 /* No exact match could be found. Now try to find match
2245 using default conversions. */
2246 if ((flags
& LOOKUP_GLOBAL
) && igv
)
2248 if (TREE_CODE (igv
) == FUNCTION_DECL
)
2250 else if (TREE_CODE (igv
) == TREE_LIST
)
2251 ever_seen
+= count_functions (igv
);
2256 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2257 == LOOKUP_SPECULATIVELY
)
2260 TREE_CHAIN (last
) = void_list_node
;
2261 if (flags
& LOOKUP_GLOBAL
)
2262 cp_error ("no global or member function `%D(%A)' defined",
2265 cp_error ("no member function `%T::%D(%A)' defined",
2266 save_basetype
, name
, TREE_CHAIN (parmtypes
));
2267 return error_mark_node
;
2272 if (cp
- candidates
!= 0)
2274 /* Rank from worst to best. Then cp will point to best one.
2275 Private fields have their bits flipped. For unsigned
2276 numbers, this should make them look very large.
2277 If the best alternate has a (signed) negative value,
2278 then all we ever saw were private members. */
2279 if (cp
- candidates
> 1)
2281 int n_candidates
= cp
- candidates
;
2282 extern int warn_synth
;
2283 TREE_VALUE (parms
) = instance_ptr
;
2284 cp
= ideal_candidate (save_basetype
, candidates
,
2285 n_candidates
, parms
, len
);
2286 if (cp
== (struct candidate
*)0)
2288 if (flags
& LOOKUP_COMPLAIN
)
2290 TREE_CHAIN (last
) = void_list_node
;
2291 cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2292 name_kind
, name
, TREE_CHAIN (parmtypes
));
2293 print_n_candidates (candidates
, n_candidates
);
2295 return error_mark_node
;
2297 if (cp
->h
.code
& EVIL_CODE
)
2298 return error_mark_node
;
2300 && DECL_NAME (cp
->function
) == ansi_opname
[MODIFY_EXPR
]
2301 && DECL_ARTIFICIAL (cp
->function
)
2302 && n_candidates
== 2)
2304 cp_warning ("using synthesized `%#D' for copy assignment",
2306 cp_warning_at (" where cfront would use `%#D'",
2307 candidates
->function
);
2310 else if (cp
[-1].h
.code
& EVIL_CODE
)
2312 if (flags
& LOOKUP_COMPLAIN
)
2313 cp_error ("ambiguous type conversion requested for %s `%D'",
2315 return error_mark_node
;
2320 /* The global function was the best, so use it. */
2321 if (cp
->u
.field
== 0)
2323 /* We must convert the instance pointer into a reference type.
2324 Global overloaded functions can only either take
2325 aggregate objects (which come for free from references)
2326 or reference data types anyway. */
2327 TREE_VALUE (parms
) = copy_node (instance_ptr
);
2328 TREE_TYPE (TREE_VALUE (parms
)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2329 return build_function_call (cp
->function
, parms
);
2332 function
= cp
->function
;
2333 basetype_path
= cp
->basetypes
;
2334 if (! DECL_STATIC_FUNCTION_P (function
))
2335 TREE_VALUE (parms
) = cp
->arg
;
2336 goto found_and_maybe_warn
;
2339 if (flags
& (LOOKUP_COMPLAIN
|LOOKUP_SPECULATIVELY
))
2341 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2342 == LOOKUP_SPECULATIVELY
)
2345 if (DECL_STATIC_FUNCTION_P (cp
->function
))
2346 parms
= TREE_CHAIN (parms
);
2349 if (flags
& LOOKUP_SPECULATIVELY
)
2351 if (static_call_context
2352 && TREE_CODE (TREE_TYPE (cp
->function
)) == METHOD_TYPE
)
2353 cp_error ("object missing in call to `%D'", cp
->function
);
2354 else if (ever_seen
> 1)
2356 TREE_CHAIN (last
) = void_list_node
;
2357 cp_error ("no matching function for call to `%T::%D (%A)'",
2358 TREE_TYPE (TREE_TYPE (instance_ptr
)),
2359 name
, TREE_CHAIN (parmtypes
));
2360 TREE_CHAIN (last
) = NULL_TREE
;
2361 print_candidates (found_fns
);
2364 report_type_mismatch (cp
, parms
, name_kind
);
2365 return error_mark_node
;
2368 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2371 cp_error ("%T has no method named %D", save_basetype
, name
);
2372 return error_mark_node
;
2378 found_and_maybe_warn
:
2379 if ((cp
->harshness
[0].code
& CONST_CODE
)
2380 /* 12.1p2: Constructors can be called for const objects. */
2381 && ! DECL_CONSTRUCTOR_P (cp
->function
))
2383 if (flags
& LOOKUP_COMPLAIN
)
2385 cp_error_at ("non-const member function `%D'", cp
->function
);
2386 error ("called for const object at this point in file");
2388 /* Not good enough for a match. */
2390 return error_mark_node
;
2394 /* Silently return error_mark_node. */
2395 return error_mark_node
;
2398 if (flags
& LOOKUP_PROTECT
)
2399 access
= compute_access (basetype_path
, function
);
2401 if (access
== access_private
)
2403 if (flags
& LOOKUP_COMPLAIN
)
2405 cp_error_at ("%s `%+#D' is %s", name_kind
, function
,
2406 TREE_PRIVATE (function
) ? "private"
2407 : "from private base class");
2408 error ("within this context");
2410 return error_mark_node
;
2412 else if (access
== access_protected
)
2414 if (flags
& LOOKUP_COMPLAIN
)
2416 cp_error_at ("%s `%+#D' %s", name_kind
, function
,
2417 TREE_PROTECTED (function
) ? "is protected"
2418 : "has protected accessibility");
2419 error ("within this context");
2421 return error_mark_node
;
2424 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2425 type (if it exists) is a pointer to. */
2427 if (DECL_ABSTRACT_VIRTUAL_P (function
)
2428 && instance
== C_C_D
2429 && DECL_CONSTRUCTOR_P (current_function_decl
)
2430 && ! (flags
& LOOKUP_NONVIRTUAL
)
2431 && value_member (function
, get_abstract_virtuals (basetype
)))
2432 cp_error ("abstract virtual `%#D' called from constructor", function
);
2434 if (IS_SIGNATURE (basetype
) && static_call_context
)
2436 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2438 return error_mark_node
;
2440 else if (IS_SIGNATURE (basetype
))
2441 return build_signature_method_call (basetype
, instance
, function
, parms
);
2443 function
= DECL_MAIN_VARIANT (function
);
2444 /* Declare external function if necessary. */
2445 assemble_external (function
);
2448 /* Is it a synthesized method that needs to be synthesized? */
2449 if (DECL_ARTIFICIAL (function
) && ! flag_no_inline
2450 && ! DECL_INITIAL (function
)
2451 /* Kludge: don't synthesize for default args. */
2452 && current_function_decl
)
2453 synthesize_method (function
);
2456 if (pedantic
&& DECL_THIS_INLINE (function
) && ! DECL_ARTIFICIAL (function
)
2457 && ! DECL_INITIAL (function
) && ! DECL_PENDING_INLINE_INFO (function
))
2458 cp_pedwarn ("inline function `%#D' called before definition", function
);
2460 fntype
= TREE_TYPE (function
);
2461 if (TREE_CODE (fntype
) == POINTER_TYPE
)
2462 fntype
= TREE_TYPE (fntype
);
2463 basetype
= DECL_CLASS_CONTEXT (function
);
2465 /* If we are referencing a virtual function from an object
2466 of effectively static type, then there is no need
2467 to go through the virtual function table. */
2468 if (need_vtbl
== maybe_needed
)
2470 int fixed_type
= resolves_to_fixed_type_p (instance
, 0);
2472 if (all_virtual
== 1
2473 && DECL_VINDEX (function
)
2474 && may_be_remote (basetype
))
2476 else if (DECL_VINDEX (function
))
2477 need_vtbl
= fixed_type
? unneeded
: needed
;
2479 need_vtbl
= not_needed
;
2482 if (TREE_CODE (fntype
) == METHOD_TYPE
&& static_call_context
2483 && !DECL_CONSTRUCTOR_P (function
))
2485 /* Let's be nice to the user for now, and give reasonable
2486 default behavior. */
2487 instance_ptr
= current_class_decl
;
2490 if (basetype
!= current_class_type
)
2492 tree binfo
= get_binfo (basetype
, current_class_type
, 1);
2493 if (binfo
== NULL_TREE
)
2495 error_not_base_type (function
, current_class_type
);
2496 return error_mark_node
;
2498 else if (basetype
== error_mark_node
)
2499 return error_mark_node
;
2502 /* Only allow a static member function to call another static member
2504 else if (DECL_LANG_SPECIFIC (function
)
2505 && !DECL_STATIC_FUNCTION_P (function
))
2507 cp_error ("cannot call member function `%D' without object",
2509 return error_mark_node
;
2513 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2515 if (TYPE_SIZE (value_type
) == 0)
2517 if (flags
& LOOKUP_COMPLAIN
)
2518 incomplete_type_error (0, value_type
);
2519 return error_mark_node
;
2522 if (DECL_STATIC_FUNCTION_P (function
))
2523 parms
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2524 TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
);
2525 else if (need_vtbl
== unneeded
)
2527 int sub_flags
= DECL_CONSTRUCTOR_P (function
) ? flags
: LOOKUP_NORMAL
;
2528 basetype
= TREE_TYPE (instance
);
2529 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function
)) != TYPE_MAIN_VARIANT (basetype
)
2530 && TYPE_USES_COMPLEX_INHERITANCE (basetype
))
2532 basetype
= DECL_CLASS_CONTEXT (function
);
2533 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
2534 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2536 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2537 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, sub_flags
));
2541 if ((flags
& LOOKUP_NONVIRTUAL
) == 0)
2542 basetype
= DECL_CONTEXT (function
);
2544 /* First parm could be integer_zerop with casts like
2545 ((Object*)0)->Object::IsA() */
2546 if (!integer_zerop (TREE_VALUE (parms
)))
2548 /* Since we can't have inheritance with a union, doing get_binfo
2549 on it won't work. We do all the convert_pointer_to_real
2550 stuff to handle MI correctly...for unions, that's not
2551 an issue, so we must short-circuit that extra work here. */
2552 tree tmp
= TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)));
2553 if (tmp
!= NULL_TREE
&& TREE_CODE (tmp
) == UNION_TYPE
)
2554 instance_ptr
= TREE_VALUE (parms
);
2557 tree binfo
= get_binfo (basetype
,
2558 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
))),
2560 instance_ptr
= convert_pointer_to_real (binfo
, TREE_VALUE (parms
));
2563 = convert_pointer_to (build_type_variant (basetype
,
2567 if (TREE_CODE (instance_ptr
) == COND_EXPR
)
2569 instance_ptr
= save_expr (instance_ptr
);
2570 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2572 else if (TREE_CODE (instance_ptr
) == NOP_EXPR
2573 && TREE_CODE (TREE_OPERAND (instance_ptr
, 0)) == ADDR_EXPR
2574 && TREE_OPERAND (TREE_OPERAND (instance_ptr
, 0), 0) == instance
)
2576 /* The call to `convert_pointer_to' may return error_mark_node. */
2577 else if (TREE_CODE (instance_ptr
) == ERROR_MARK
)
2578 return instance_ptr
;
2579 else if (instance
== NULL_TREE
2580 || TREE_CODE (instance
) != INDIRECT_REF
2581 || TREE_OPERAND (instance
, 0) != instance_ptr
)
2582 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2584 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2585 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
));
2589 /* Constructors do not overload method calls. */
2590 else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype
)
2591 && name
!= TYPE_IDENTIFIER (basetype
)
2592 && (TREE_CODE (function
) != FUNCTION_DECL
2593 || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)),
2594 OPERATOR_METHOD_FORMAT
,
2595 OPERATOR_METHOD_LENGTH
))
2596 && (may_be_remote (basetype
) || instance
!= C_C_D
))
2600 parms
= TREE_CHAIN (parms
);
2602 if (!all_virtual
&& TREE_CODE (function
) == FUNCTION_DECL
)
2603 fn_as_int
= build_unary_op (ADDR_EXPR
, function
, 0);
2605 fn_as_int
= convert (TREE_TYPE (default_conversion (function
)), DECL_VINDEX (function
));
2606 if (all_virtual
== 1)
2607 fn_as_int
= convert (integer_type_node
, fn_as_int
);
2609 result
= build_opfncall (METHOD_CALL_EXPR
, LOOKUP_NORMAL
, instance
, fn_as_int
, parms
);
2611 if (result
== NULL_TREE
)
2613 compiler_error ("could not overload `operator->()(...)'");
2614 return error_mark_node
;
2616 else if (result
== error_mark_node
)
2617 return error_mark_node
;
2620 /* Do this if we want the result of operator->() to inherit
2621 the type of the function it is subbing for. */
2622 TREE_TYPE (result
) = value_type
;
2629 if (need_vtbl
== needed
)
2631 function
= build_vfn_ref (&TREE_VALUE (parms
), instance
,
2632 DECL_VINDEX (function
));
2633 TREE_TYPE (function
) = build_pointer_type (fntype
);
2636 if (TREE_CODE (function
) == FUNCTION_DECL
)
2637 GNU_xref_call (current_function_decl
,
2638 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)));
2643 if (TREE_CODE (function
) == FUNCTION_DECL
)
2645 is_constructor
= DECL_CONSTRUCTOR_P (function
);
2646 TREE_USED (function
) = 1;
2647 function
= default_conversion (function
);
2652 function
= default_conversion (function
);
2655 result
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
2657 TREE_TYPE (result
) = value_type
;
2658 TREE_SIDE_EFFECTS (result
) = 1;
2659 TREE_HAS_CONSTRUCTOR (result
) = is_constructor
;
2660 result
= convert_from_reference (result
);
2665 /* Similar to `build_method_call', but for overloaded non-member functions.
2666 The name of this function comes through NAME. The name depends
2669 Note that this function must handle simple `C' promotions,
2670 as well as variable numbers of arguments (...), and
2671 default arguments to boot.
2673 If the overloading is successful, we return a tree node which
2674 contains the call to the function.
2676 If overloading produces candidates which are probable, but not definite,
2677 we hold these candidates. If FINAL_CP is non-zero, then we are free
2678 to assume that final_cp points to enough storage for all candidates that
2679 this function might generate. The `harshness' array is preallocated for
2680 the first candidate, but not for subsequent ones.
2682 Note that the DECL_RTL of FUNCTION must be made to agree with this
2683 function's new name. */
2686 build_overload_call_real (fnname
, parms
, flags
, final_cp
, buildxxx
)
2689 struct candidate
*final_cp
;
2692 /* must check for overloading here */
2693 tree overload_name
, functions
, function
, parm
;
2694 tree parmtypes
= NULL_TREE
, last
= NULL_TREE
;
2695 register tree outer
;
2697 int parmlength
= list_length (parms
);
2699 struct candidate
*candidates
, *cp
;
2703 final_cp
[0].h
.code
= 0;
2704 final_cp
[0].h
.distance
= 0;
2705 final_cp
[0].function
= 0;
2707 final_cp
[1].h
.code
= EVIL_CODE
;
2710 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2712 register tree t
= TREE_TYPE (TREE_VALUE (parm
));
2714 if (t
== error_mark_node
)
2717 final_cp
->h
.code
= EVIL_CODE
;
2718 return error_mark_node
;
2720 if (TREE_CODE (t
) == OFFSET_TYPE
)
2722 /* This breaks reference-to-array parameters. */
2723 || TREE_CODE (t
) == ARRAY_TYPE
2726 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
2727 Also convert OFFSET_TYPE entities to their normal selves.
2728 This eliminates needless calls to `compute_conversion_costs'. */
2729 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
2730 t
= TREE_TYPE (TREE_VALUE (parm
));
2732 last
= build_tree_list (NULL_TREE
, t
);
2733 parmtypes
= chainon (parmtypes
, last
);
2736 TREE_CHAIN (last
) = void_list_node
;
2738 parmtypes
= void_list_node
;
2740 if (is_overloaded_fn (fnname
))
2743 if (TREE_CODE (fnname
) == TREE_LIST
)
2744 fnname
= TREE_PURPOSE (functions
);
2745 else if (TREE_CODE (fnname
) == FUNCTION_DECL
)
2746 fnname
= DECL_NAME (functions
);
2749 functions
= lookup_name_nonclass (fnname
);
2751 if (functions
== NULL_TREE
)
2753 if (flags
& LOOKUP_SPECULATIVELY
)
2755 if (flags
& LOOKUP_COMPLAIN
)
2756 error ("only member functions apply");
2758 final_cp
->h
.code
= EVIL_CODE
;
2759 return error_mark_node
;
2762 if (TREE_CODE (functions
) == FUNCTION_DECL
&& ! IDENTIFIER_OPNAME_P (fnname
))
2764 functions
= DECL_MAIN_VARIANT (functions
);
2767 /* We are just curious whether this is a viable alternative or
2769 compute_conversion_costs (functions
, parms
, final_cp
, parmlength
);
2773 return build_function_call_real (functions
, parms
, 1, flags
);
2776 if (TREE_CODE (functions
) == TREE_LIST
2777 && TREE_VALUE (functions
) == NULL_TREE
)
2779 if (flags
& LOOKUP_SPECULATIVELY
)
2782 if (flags
& LOOKUP_COMPLAIN
)
2783 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2784 TREE_PURPOSE (functions
));
2786 final_cp
->h
.code
= EVIL_CODE
;
2787 return error_mark_node
;
2790 length
= count_functions (functions
);
2793 candidates
= final_cp
;
2797 = (struct candidate
*)alloca ((length
+1) * sizeof (struct candidate
));
2798 bzero ((char *) candidates
, (length
+ 1) * sizeof (struct candidate
));
2803 my_friendly_assert (is_overloaded_fn (functions
), 169);
2805 functions
= get_first_fn (functions
);
2807 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2808 for (outer
= functions
; outer
; outer
= DECL_CHAIN (outer
))
2810 int template_cost
= 0;
2812 if (TREE_CODE (function
) != FUNCTION_DECL
2813 && ! (TREE_CODE (function
) == TEMPLATE_DECL
2814 && ! DECL_TEMPLATE_IS_CLASS (function
)
2815 && TREE_CODE (DECL_TEMPLATE_RESULT (function
)) == FUNCTION_DECL
))
2817 enum tree_code code
= TREE_CODE (function
);
2818 if (code
== TEMPLATE_DECL
)
2819 code
= TREE_CODE (DECL_TEMPLATE_RESULT (function
));
2820 if (code
== CONST_DECL
)
2822 ("enumeral value `%D' conflicts with function of same name",
2824 else if (code
== VAR_DECL
)
2826 if (TREE_STATIC (function
))
2828 ("variable `%D' conflicts with function of same name",
2832 ("constant field `%D' conflicts with function of same name",
2835 else if (code
== TYPE_DECL
)
2838 my_friendly_abort (2);
2839 error ("at this point in file");
2842 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2844 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function
));
2845 tree
*targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2848 i
= type_unification (DECL_TEMPLATE_PARMS (function
), targs
,
2849 TYPE_ARG_TYPES (TREE_TYPE (function
)),
2850 parms
, &template_cost
, 0);
2852 function
= instantiate_template (function
, targs
);
2855 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2857 /* Unconverted template -- failed match. */
2858 cp
->function
= function
;
2860 cp
->h
.code
= EVIL_CODE
;
2864 struct candidate
*cp2
;
2866 /* Check that this decl is not the same as a function that's in
2867 the list due to some template instantiation. */
2870 if (cp2
->function
== function
)
2874 if (cp2
->function
== function
)
2877 function
= DECL_MAIN_VARIANT (function
);
2879 /* Can't use alloca here, since result might be
2880 passed to calling function. */
2881 cp
->h_len
= parmlength
;
2882 cp
->harshness
= (struct harshness_code
*)
2883 oballoc ((parmlength
+ 1) * sizeof (struct harshness_code
));
2885 compute_conversion_costs (function
, parms
, cp
, parmlength
);
2887 /* Make sure this is clear as well. */
2888 cp
->h
.int_penalty
+= template_cost
;
2890 if ((cp
[0].h
.code
& EVIL_CODE
) == 0)
2892 cp
[1].h
.code
= EVIL_CODE
;
2898 if (cp
- candidates
)
2900 tree rval
= error_mark_node
;
2903 cp
[0].h
.code
= EVIL_CODE
;
2904 if (cp
- candidates
> 1)
2906 struct candidate
*best_cp
2907 = ideal_candidate (NULL_TREE
, candidates
,
2908 cp
- candidates
, parms
, parmlength
);
2909 if (best_cp
== (struct candidate
*)0)
2911 if (flags
& LOOKUP_COMPLAIN
)
2913 cp_error ("call of overloaded `%D' is ambiguous", fnname
);
2914 print_n_candidates (candidates
, cp
- candidates
);
2916 return error_mark_node
;
2919 rval
= best_cp
->function
;
2924 if (cp
->h
.code
& EVIL_CODE
)
2926 if (flags
& LOOKUP_COMPLAIN
)
2927 error ("type conversion ambiguous");
2930 rval
= cp
->function
;
2936 return buildxxx
? build_function_call_real (rval
, parms
, 0, flags
)
2937 : build_function_call_real (rval
, parms
, 1, flags
);
2940 if (flags
& LOOKUP_SPECULATIVELY
)
2943 if (flags
& LOOKUP_COMPLAIN
)
2944 report_type_mismatch (cp
, parms
, "function",
2945 decl_as_string (cp
->function
, 1));
2947 return error_mark_node
;
2951 build_overload_call (fnname
, parms
, flags
, final_cp
)
2954 struct candidate
*final_cp
;
2956 return build_overload_call_real (fnname
, parms
, flags
, final_cp
, 0);
2960 build_overload_call_maybe (fnname
, parms
, flags
, final_cp
)
2963 struct candidate
*final_cp
;
2965 return build_overload_call_real (fnname
, parms
, flags
, final_cp
, 1);