1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 hacked by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
40 extern int inhibit_warnings
;
41 extern tree ctor_label
, dtor_label
;
43 /* Compute the ease with which a conversion can be performed
44 between an expected and the given type. */
46 static struct harshness_code convert_harshness
PROTO((register tree
, register tree
, tree
));
48 #define EVIL_RETURN(ARG) ((ARG).code = EVIL_CODE, (ARG))
49 #define STD_RETURN(ARG) ((ARG).code = STD_CODE, (ARG))
50 #define QUAL_RETURN(ARG) ((ARG).code = QUAL_CODE, (ARG))
51 #define TRIVIAL_RETURN(ARG) ((ARG).code = TRIVIAL_CODE, (ARG))
52 #define ZERO_RETURN(ARG) ((ARG).code = 0, (ARG))
54 /* 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. */
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. */
115 static struct harshness_code
116 convert_harshness (type
, parmtype
, parm
)
117 register tree type
, parmtype
;
120 struct harshness_code h
;
121 register enum tree_code codel
;
122 register enum tree_code coder
;
129 #ifdef GATHER_STATISTICS
130 n_convert_harshness
++;
133 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
)
136 parm
= convert_from_reference (parm
);
137 parmtype
= TREE_TYPE (parmtype
);
141 lvalue
= lvalue_p (parm
);
145 if (TYPE_PTRMEMFUNC_P (type
))
146 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
147 if (TYPE_PTRMEMFUNC_P (parmtype
))
148 parmtype
= TYPE_PTRMEMFUNC_FN_TYPE (parmtype
);
150 codel
= TREE_CODE (type
);
151 coder
= TREE_CODE (parmtype
);
153 if (TYPE_MAIN_VARIANT (parmtype
) == TYPE_MAIN_VARIANT (type
))
154 return ZERO_RETURN (h
);
156 if (coder
== ERROR_MARK
)
157 return EVIL_RETURN (h
);
159 if (codel
== REFERENCE_TYPE
)
162 int constp
= parm
? TREE_READONLY (parm
) : TYPE_READONLY (parmtype
);
163 int volatilep
= (parm
? TREE_THIS_VOLATILE (parm
)
164 : TYPE_VOLATILE (parmtype
));
165 register tree intype
= TYPE_MAIN_VARIANT (parmtype
);
166 register enum tree_code form
= TREE_CODE (intype
);
169 ttl
= TREE_TYPE (type
);
171 /* Only allow const reference binding if we were given a parm to deal
172 with, since it isn't really a conversion. This is a hack to
173 prevent build_type_conversion from finding this conversion, but
174 still allow overloading to find it. */
175 if (! lvalue
&& ! (parm
&& TYPE_READONLY (ttl
)))
176 return EVIL_RETURN (h
);
178 if ((TYPE_READONLY (ttl
) < constp
)
179 || (TYPE_VOLATILE (ttl
) < volatilep
))
180 return EVIL_RETURN (h
);
182 /* When passing a non-const argument into a const reference, dig it a
183 little, so a non-const reference is preferred over this one. */
184 penalty
= ((TYPE_READONLY (ttl
) > constp
)
185 + (TYPE_VOLATILE (ttl
) > volatilep
));
187 ttl
= TYPE_MAIN_VARIANT (ttl
);
189 if (form
== OFFSET_TYPE
)
191 intype
= TREE_TYPE (intype
);
192 form
= TREE_CODE (intype
);
197 if (TREE_CODE (ttl
) == ARRAY_TYPE
&& TREE_CODE (ttr
) == ARRAY_TYPE
)
199 if (comptypes (ttl
, ttr
, 1))
200 return ZERO_RETURN (h
);
201 return EVIL_RETURN (h
);
204 h
= convert_harshness (ttl
, ttr
, NULL_TREE
);
205 if (penalty
&& h
.code
== 0)
208 h
.int_penalty
= penalty
;
213 if (codel
== POINTER_TYPE
&& fntype_p (parmtype
))
216 struct harshness_code h1
, h2
;
218 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
219 type
= TREE_TYPE (type
);
221 if (coder
== POINTER_TYPE
)
223 parmtype
= TREE_TYPE (parmtype
);
224 coder
= TREE_CODE (parmtype
);
227 if (coder
!= TREE_CODE (type
))
228 return EVIL_RETURN (h
);
230 if (type
!= parmtype
&& coder
== METHOD_TYPE
)
232 tree ttl
= TYPE_METHOD_BASETYPE (type
);
233 tree ttr
= TYPE_METHOD_BASETYPE (parmtype
);
235 int b_or_d
= get_base_distance (ttr
, ttl
, 0, (tree
*)0);
238 b_or_d
= get_base_distance (ttl
, ttr
, 0, (tree
*)0);
240 return EVIL_RETURN (h
);
241 h
.distance
= -b_or_d
;
247 type
= build_function_type
248 (TREE_TYPE (type
), TREE_CHAIN (TYPE_ARG_TYPES (type
)));
249 parmtype
= build_function_type
250 (TREE_TYPE (parmtype
), TREE_CHAIN (TYPE_ARG_TYPES (parmtype
)));
253 /* We allow the default conversion between function type
254 and pointer-to-function type for free. */
255 if (comptypes (type
, parmtype
, 1))
259 return EVIL_RETURN (h
);
261 /* Compare return types. */
262 p1
= TREE_TYPE (type
);
263 p2
= TREE_TYPE (parmtype
);
264 h2
= convert_harshness (p1
, p2
, NULL_TREE
);
265 if (h2
.code
& EVIL_CODE
)
268 h1
.code
= TRIVIAL_CODE
;
271 if (h2
.distance
!= 0)
275 /* This only works for pointers. */
276 if (TREE_CODE (p1
) != POINTER_TYPE
277 && TREE_CODE (p1
) != REFERENCE_TYPE
)
278 return EVIL_RETURN (h
);
282 /* Don't die if we happen to be dealing with void*. */
283 if (!IS_AGGR_TYPE (p1
) || !IS_AGGR_TYPE (p2
))
284 return EVIL_RETURN (h
);
286 binfo
= get_binfo (p2
, p1
, 0);
288 binfo
= get_binfo (p1
, p2
, 0);
290 if (! BINFO_OFFSET_ZEROP (binfo
))
293 static int explained
= 0;
295 message_2_types (sorry
, "cannot cast `%s' to `%s' at function call site", p2
, p1
);
297 message_2_types (sorry
, "cannot cast `%s' to `%s' at function call site", p1
, p2
);
300 sorry ("(because pointer values change during conversion)");
302 return EVIL_RETURN (h
);
307 if (h2
.distance
> h1
.distance
)
308 h1
.distance
= h2
.distance
;
310 p1
= TYPE_ARG_TYPES (type
);
311 p2
= TYPE_ARG_TYPES (parmtype
);
312 while (p1
&& TREE_VALUE (p1
) != void_type_node
313 && p2
&& TREE_VALUE (p2
) != void_type_node
)
315 h2
= convert_harshness (TREE_VALUE (p1
), TREE_VALUE (p2
),
317 if (h2
.code
& EVIL_CODE
)
322 /* This only works for pointers and references. */
323 if (TREE_CODE (TREE_VALUE (p1
)) != POINTER_TYPE
324 && TREE_CODE (TREE_VALUE (p1
)) != REFERENCE_TYPE
)
325 return EVIL_RETURN (h
);
326 h2
.distance
= - h2
.distance
;
330 if (h2
.distance
> h1
.distance
)
331 h1
.distance
= h2
.distance
;
332 p1
= TREE_CHAIN (p1
);
333 p2
= TREE_CHAIN (p2
);
340 return EVIL_RETURN (h
);
341 h1
.code
|= ELLIPSIS_CODE
;
346 if (TREE_PURPOSE (p1
) == NULL_TREE
)
347 h1
.code
|= EVIL_CODE
;
351 else if (codel
== POINTER_TYPE
&& coder
== OFFSET_TYPE
)
355 /* Get to the OFFSET_TYPE that this might be. */
356 type
= TREE_TYPE (type
);
358 if (coder
!= TREE_CODE (type
))
359 return EVIL_RETURN (h
);
361 ttl
= TYPE_OFFSET_BASETYPE (type
);
362 ttr
= TYPE_OFFSET_BASETYPE (parmtype
);
368 int b_or_d
= get_base_distance (ttr
, ttl
, 0, (tree
*)0);
371 b_or_d
= get_base_distance (ttl
, ttr
, 0, (tree
*)0);
373 return EVIL_RETURN (h
);
374 h
.distance
= -b_or_d
;
381 /* Now test the OFFSET_TYPE's target compatibility. */
382 type
= TREE_TYPE (type
);
383 parmtype
= TREE_TYPE (parmtype
);
386 if (coder
== UNKNOWN_TYPE
)
388 if (codel
== FUNCTION_TYPE
389 || codel
== METHOD_TYPE
390 || (codel
== POINTER_TYPE
391 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
392 || TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)))
393 return TRIVIAL_RETURN (h
);
394 return EVIL_RETURN (h
);
397 if (coder
== VOID_TYPE
)
398 return EVIL_RETURN (h
);
400 if (codel
== BOOLEAN_TYPE
)
402 if (INTEGRAL_CODE_P (coder
) || coder
== REAL_TYPE
)
403 return STD_RETURN (h
);
404 else if (coder
== POINTER_TYPE
|| coder
== OFFSET_TYPE
)
406 /* Make this worse than any conversion to another pointer.
407 FIXME this is how I think the language should work, but it may not
408 end up being how the language is standardized (jason 1/30/95). */
410 return STD_RETURN (h
);
412 return EVIL_RETURN (h
);
415 if (INTEGRAL_CODE_P (codel
))
417 /* Control equivalence of ints an enums. */
419 if (codel
== ENUMERAL_TYPE
420 && flag_int_enum_equivalence
== 0)
422 /* Enums can be converted to ints, but not vice-versa. */
423 if (coder
!= ENUMERAL_TYPE
424 || TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (parmtype
))
425 return EVIL_RETURN (h
);
428 /* else enums and ints (almost) freely interconvert. */
430 if (INTEGRAL_CODE_P (coder
))
432 if (TYPE_MAIN_VARIANT (type
)
433 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
442 else if (coder
== REAL_TYPE
)
450 if (codel
== REAL_TYPE
)
452 if (coder
== REAL_TYPE
)
454 if (TYPE_MAIN_VARIANT (type
)
455 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype
)))
462 else if (INTEGRAL_CODE_P (coder
))
470 /* Convert arrays which have not previously been converted. */
471 if (coder
== ARRAY_TYPE
)
473 coder
= POINTER_TYPE
;
476 parm
= decay_conversion (parm
);
477 parmtype
= TREE_TYPE (parm
);
480 parmtype
= build_pointer_type (TREE_TYPE (parmtype
));
483 /* Conversions among pointers */
484 if (codel
== POINTER_TYPE
&& coder
== POINTER_TYPE
)
486 register tree ttl
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
487 register tree ttr
= TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
));
488 int penalty
= 4 * (ttl
!= ttr
);
490 /* Anything converts to void *. Since this may be `const void *'
491 (etc.) use VOID_TYPE instead of void_type_node. Otherwise, the
492 targets must be the same, except that we do allow (at some cost)
493 conversion between signed and unsigned pointer types. */
495 if ((TREE_CODE (ttl
) == METHOD_TYPE
496 || TREE_CODE (ttl
) == FUNCTION_TYPE
)
497 && TREE_CODE (ttl
) == TREE_CODE (ttr
))
499 if (comptypes (ttl
, ttr
, -1))
501 h
.code
= penalty
? STD_CODE
: 0;
510 if (TREE_CODE (ttl
) != VOID_TYPE
511 && (TREE_CODE (ttr
) != VOID_TYPE
|| !parm
|| !null_ptr_cst_p (parm
)))
513 if (comp_target_types (type
, parmtype
, 1) <= 0)
514 return EVIL_RETURN (h
);
517 if (!(TREE_CODE (ttl
) == VOID_TYPE
518 || TREE_CODE (ttr
) == VOID_TYPE
519 || (TREE_UNSIGNED (ttl
) ^ TREE_UNSIGNED (ttr
)
520 && (ttl
= unsigned_type (ttl
),
521 ttr
= unsigned_type (ttr
),
523 || (comp_target_types (ttl
, ttr
, 0) > 0)))
524 return EVIL_RETURN (h
);
529 tree tmp1
= TREE_TYPE (type
), tmp2
= TREE_TYPE (parmtype
);
532 /* Note conversion from `T*' to `const T*',
533 or `T*' to `volatile T*'. */
534 if ((TYPE_READONLY (tmp1
) < TREE_READONLY (tmp2
))
535 || (TYPE_VOLATILE (tmp1
) < TYPE_VOLATILE (tmp2
)))
537 else if ((TYPE_READONLY (tmp1
) != TREE_READONLY (tmp2
))
538 || (TYPE_VOLATILE (tmp1
) != TYPE_VOLATILE (tmp2
)))
546 if (TREE_CODE (ttl
) == RECORD_TYPE
&& TREE_CODE (ttr
) == RECORD_TYPE
)
548 int b_or_d
= get_base_distance (ttl
, ttr
, 0, (tree
*)0);
551 b_or_d
= get_base_distance (ttr
, ttl
, 0, (tree
*)0);
553 return EVIL_RETURN (h
);
554 h
.distance
= -b_or_d
;
562 /* If converting from a `class*' to a `void*', make it
563 less favorable than any inheritance relationship. */
564 if (TREE_CODE (ttl
) == VOID_TYPE
&& IS_AGGR_TYPE (ttr
))
567 h
.distance
= CLASSTYPE_MAX_DEPTH (ttr
)+1;
571 h
.code
= penalty
? STD_CODE
: PROMO_CODE
;
572 /* Catch things like `const char *' -> `const void *'
573 vs `const char *' -> `void *'. */
576 tree tmp1
= TREE_TYPE (type
), tmp2
= TREE_TYPE (parmtype
);
577 if ((TYPE_READONLY (tmp1
) < TREE_READONLY (tmp2
))
578 || (TYPE_VOLATILE (tmp1
) < TYPE_VOLATILE (tmp2
)))
580 else if ((TYPE_READONLY (tmp1
) > TREE_READONLY (tmp2
))
581 || (TYPE_VOLATILE (tmp1
) > TYPE_VOLATILE (tmp2
)))
587 if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
589 /* This is not a bad match, but don't let it beat
590 integer-enum combinations. */
591 if (parm
&& integer_zerop (parm
))
599 /* C++: Since the `this' parameter of a signature member function
600 is represented as a signature pointer to handle default implementations
601 correctly, we can have the case that `type' is a signature pointer
602 while `parmtype' is a pointer to a signature table. We don't really
603 do any conversions in this case, so just return 0. */
605 if (codel
== RECORD_TYPE
&& coder
== POINTER_TYPE
606 && IS_SIGNATURE_POINTER (type
) && IS_SIGNATURE (TREE_TYPE (parmtype
)))
607 return ZERO_RETURN (h
);
609 if (codel
== RECORD_TYPE
&& coder
== RECORD_TYPE
)
611 int b_or_d
= get_base_distance (type
, parmtype
, 0, (tree
*)0);
614 b_or_d
= get_base_distance (parmtype
, type
, 0, (tree
*)0);
616 return EVIL_RETURN (h
);
617 h
.distance
= -b_or_d
;
624 return EVIL_RETURN (h
);
627 /* A clone of build_type_conversion for checking user-defined conversions in
628 overload resolution. */
631 user_harshness (type
, parmtype
)
632 register tree type
, parmtype
;
635 tree winner
= NULL_TREE
;
639 tree typename
= build_typename_overload (type
);
640 if (lookup_fnfields (TYPE_BINFO (parmtype
), typename
, 0))
644 for (conv
= lookup_conversions (parmtype
); conv
; conv
= TREE_CHAIN (conv
))
646 struct harshness_code tmp
;
647 tree cand
= TREE_VALUE (conv
);
649 if (winner
&& winner
== cand
)
652 tmp
= convert_harshness (type
, TREE_TYPE (TREE_TYPE (cand
)), NULL_TREE
);
653 if ((tmp
.code
< USER_CODE
) && (tmp
.distance
>= 0))
672 can_convert (to
, from
)
675 struct harshness_code h
;
676 h
= convert_harshness (to
, from
, NULL_TREE
);
677 return (h
.code
< USER_CODE
) && (h
.distance
>= 0);
681 can_convert_arg (to
, from
, arg
)
684 struct harshness_code h
;
685 h
= convert_harshness (to
, from
, arg
);
686 return (h
.code
< USER_CODE
) && (h
.distance
>= 0);
689 #ifdef DEBUG_MATCHING
692 struct harshness_code
*h
;
694 static char buf
[1024];
697 bzero (buf
, 1024 * sizeof (char));
698 strcat (buf
, "codes=[");
699 if (h
->code
& EVIL_CODE
)
700 strcat (buf
, "EVIL");
701 if (h
->code
& CONST_CODE
)
702 strcat (buf
, " CONST");
703 if (h
->code
& ELLIPSIS_CODE
)
704 strcat (buf
, " ELLIPSIS");
705 if (h
->code
& USER_CODE
)
706 strcat (buf
, " USER");
707 if (h
->code
& STD_CODE
)
708 strcat (buf
, " STD");
709 if (h
->code
& PROMO_CODE
)
710 strcat (buf
, " PROMO");
711 if (h
->code
& QUAL_CODE
)
712 strcat (buf
, " QUAL");
713 if (h
->code
& TRIVIAL_CODE
)
714 strcat (buf
, " TRIVIAL");
718 sprintf (tmp
, "] distance=%d int_penalty=%d", h
->distance
, h
->int_penalty
);
726 /* Algorithm: For each argument, calculate how difficult it is to
727 make FUNCTION accept that argument. If we can easily tell that
728 FUNCTION won't be acceptable to one of the arguments, then we
729 don't need to compute the ease of converting the other arguments,
730 since it will never show up in the intersection of all arguments'
733 Conversions between builtin and user-defined types are allowed, but
734 no function involving such a conversion is preferred to one which
735 does not require such a conversion. Furthermore, such conversions
739 compute_conversion_costs (function
, tta_in
, cp
, arglen
)
742 struct candidate
*cp
;
745 tree ttf_in
= TYPE_ARG_TYPES (TREE_TYPE (function
));
749 /* Start out with no strikes against. */
750 int evil_strikes
= 0;
751 int ellipsis_strikes
= 0;
752 int user_strikes
= 0;
753 int b_or_d_strikes
= 0;
754 int easy_strikes
= 0;
756 int strike_index
= 0, win
;
757 struct harshness_code lose
;
758 extern int cp_silent
;
760 #ifdef GATHER_STATISTICS
761 n_compute_conversion_costs
++;
764 #ifndef DEBUG_MATCHING
765 /* We don't emit any warnings or errors while trying out each candidate. */
769 cp
->function
= function
;
770 cp
->arg
= tta
? TREE_VALUE (tta
) : NULL_TREE
;
771 cp
->u
.bad_arg
= 0; /* optimistic! */
775 cp
->h
.int_penalty
= 0;
776 bzero ((char *) cp
->harshness
,
777 (cp
->h_len
+ 1) * sizeof (struct harshness_code
));
781 struct harshness_code h
;
783 if (ttf
== void_list_node
)
786 if (type_unknown_p (TREE_VALUE (tta
)))
788 /* Must perform some instantiation here. */
789 tree rhs
= TREE_VALUE (tta
);
790 tree lhstype
= TREE_VALUE (ttf
);
792 /* Keep quiet about possible contravariance violations. */
793 int old_inhibit_warnings
= inhibit_warnings
;
794 inhibit_warnings
= 1;
796 /* @@ This is to undo what `grokdeclarator' does to
797 parameter types. It really should go through
798 something more general. */
800 TREE_TYPE (tta
) = unknown_type_node
;
801 rhs
= instantiate_type (lhstype
, rhs
, 0);
802 inhibit_warnings
= old_inhibit_warnings
;
804 if (TREE_CODE (rhs
) == ERROR_MARK
)
807 h
= convert_harshness (lhstype
, TREE_TYPE (rhs
), rhs
);
811 #ifdef DEBUG_MATCHING
812 static tree old_function
= NULL_TREE
;
814 if (!old_function
|| function
!= old_function
)
816 cp_error ("trying %D", function
);
817 old_function
= function
;
820 cp_error (" doing (%T) %E against arg %T",
821 TREE_TYPE (TREE_VALUE (tta
)), TREE_VALUE (tta
),
825 h
= convert_harshness (TREE_VALUE (ttf
),
826 TREE_TYPE (TREE_VALUE (tta
)),
829 #ifdef DEBUG_MATCHING
830 cp_error (" evaluated %s", print_harshness (&h
));
834 cp
->harshness
[strike_index
] = h
;
835 if ((h
.code
& EVIL_CODE
)
836 || ((h
.code
& STD_CODE
) && h
.distance
< 0))
838 cp
->u
.bad_arg
= strike_index
;
841 else if (h
.code
& ELLIPSIS_CODE
)
842 ellipsis_strikes
+= 1;
844 /* This is never set by `convert_harshness'. */
845 else if (h
.code
& USER_CODE
)
852 if ((h
.code
& STD_CODE
) && h
.distance
)
854 if (h
.distance
> b_or_d_strikes
)
855 b_or_d_strikes
= h
.distance
;
858 easy_strikes
+= (h
.code
& (STD_CODE
|PROMO_CODE
|TRIVIAL_CODE
));
859 cp
->h
.code
|= h
.code
;
860 /* Make sure we communicate this. */
861 cp
->h
.int_penalty
+= h
.int_penalty
;
864 ttf
= TREE_CHAIN (ttf
);
865 tta
= TREE_CHAIN (tta
);
871 /* ran out of formals, and parmlist is fixed size. */
872 if (ttf
/* == void_type_node */)
874 cp
->h
.code
= EVIL_CODE
;
881 struct harshness_code h
;
882 int l
= list_length (tta
);
883 ellipsis_strikes
+= l
;
884 h
.code
= ELLIPSIS_CODE
;
888 cp
->harshness
[strike_index
++] = h
;
891 else if (ttf
&& ttf
!= void_list_node
)
893 /* ran out of actuals, and no defaults. */
894 if (TREE_PURPOSE (ttf
) == NULL_TREE
)
896 cp
->h
.code
= EVIL_CODE
;
901 /* Store index of first default. */
902 cp
->harshness
[arglen
].distance
= strike_index
+1;
905 cp
->harshness
[arglen
].distance
= 0;
907 /* Argument list lengths work out, so don't need to check them again. */
910 /* We do not check for derived->base conversions here, since in
911 no case would they give evil strike counts, unless such conversions
912 are somehow ambiguous. */
914 /* See if any user-defined conversions apply.
915 But make sure that we do not loop. */
916 static int dont_convert_types
= 0;
918 if (dont_convert_types
)
920 cp
->h
.code
= EVIL_CODE
;
925 win
= 0; /* Only get one chance to win. */
926 ttf
= TYPE_ARG_TYPES (TREE_TYPE (function
));
933 if (ttf
== void_list_node
)
936 lose
= cp
->harshness
[strike_index
];
937 if ((lose
.code
& EVIL_CODE
)
938 || ((lose
.code
& STD_CODE
) && lose
.distance
< 0))
940 tree actual_type
= TREE_TYPE (TREE_VALUE (tta
));
941 tree formal_type
= TREE_VALUE (ttf
);
942 int extra_conversions
= 0;
944 dont_convert_types
= 1;
946 if (TREE_CODE (formal_type
) == REFERENCE_TYPE
)
947 formal_type
= TREE_TYPE (formal_type
);
948 if (TREE_CODE (actual_type
) == REFERENCE_TYPE
)
949 actual_type
= TREE_TYPE (actual_type
);
951 if (formal_type
!= error_mark_node
952 && actual_type
!= error_mark_node
)
954 formal_type
= complete_type (TYPE_MAIN_VARIANT (formal_type
));
955 actual_type
= complete_type (TYPE_MAIN_VARIANT (actual_type
));
957 if (TYPE_HAS_CONSTRUCTOR (formal_type
))
959 /* If it has a constructor for this type,
961 /* @@ There is no way to save this result yet, so
962 success is a NULL_TREE for now. */
963 if (convert_to_aggr (formal_type
, TREE_VALUE (tta
), 0, 1)
967 if (TYPE_LANG_SPECIFIC (actual_type
)
968 && TYPE_HAS_CONVERSION (actual_type
))
970 int extra
= user_harshness (formal_type
, actual_type
);
972 if (extra
== EVIL_CODE
)
977 extra_conversions
= extra
;
981 dont_convert_types
= 0;
986 cp
->harshness
[strike_index
].code
987 = USER_CODE
| (extra_conversions
? STD_CODE
: 0);
992 if (cp
->u
.bad_arg
> strike_index
)
993 cp
->u
.bad_arg
= strike_index
;
995 evil_strikes
= win
? 2 : 1;
1000 ttf
= TREE_CHAIN (ttf
);
1001 tta
= TREE_CHAIN (tta
);
1006 /* Const member functions get a small penalty because defaulting
1007 to const is less useful than defaulting to non-const. */
1008 /* This is bogus, it does not correspond to anything in the ARM.
1009 This code will be fixed when this entire section is rewritten
1010 to conform to the ARM. (mrs) */
1011 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1013 tree this_parm
= TREE_VALUE (ttf_in
);
1015 if (TREE_CODE (this_parm
) == RECORD_TYPE
/* Is `this' a sig ptr? */
1016 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm
))))
1017 : TYPE_READONLY (TREE_TYPE (this_parm
)))
1019 cp
->harshness
[0].code
|= TRIVIAL_CODE
;
1024 /* Calling a non-const member function from a const member function
1025 is probably invalid, but for now we let it only draw a warning.
1026 We indicate that such a mismatch has occurred by setting the
1027 harshness to a maximum value. */
1028 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in
))) == POINTER_TYPE
1029 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in
))))))
1030 cp
->harshness
[0].code
|= CONST_CODE
;
1035 cp
->h
.code
= EVIL_CODE
;
1036 if (ellipsis_strikes
)
1037 cp
->h
.code
|= ELLIPSIS_CODE
;
1039 cp
->h
.code
|= USER_CODE
;
1041 #ifdef DEBUG_MATCHING
1042 cp_error ("final eval %s", print_harshness (&cp
->h
));
1046 /* Subroutine of ideal_candidate. See if X or Y is a better match
1050 strictly_better (x
, y
)
1051 unsigned short x
, y
;
1059 if (xor >= x
|| xor >= y
)
1064 /* When one of several possible overloaded functions and/or methods
1065 can be called, choose the best candidate for overloading.
1067 BASETYPE is the context from which we start method resolution
1068 or NULL if we are comparing overloaded functions.
1069 CANDIDATES is the array of candidates we have to choose from.
1070 N_CANDIDATES is the length of CANDIDATES.
1071 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1072 choose. It is modified in place when resolving methods. It is not
1073 modified in place when resolving overloaded functions.
1074 LEN is the length of the parameter list. */
1076 static struct candidate
*
1077 ideal_candidate (candidates
, n_candidates
, len
)
1078 struct candidate
*candidates
;
1082 struct candidate
*cp
= candidates
+n_candidates
;
1083 int i
, j
= -1, best_code
;
1085 /* For each argument, sort the functions from best to worst for the arg.
1086 For each function that's not best for this arg, set its overall
1087 harshness to EVIL so that other args won't like it. The candidate
1088 list for the last argument is the intersection of all the best-liked
1091 qsort (candidates
, n_candidates
, sizeof (struct candidate
),
1093 best_code
= cp
[-1].h
.code
;
1095 /* If they're at least as good as each other, do an arg-by-arg check. */
1096 if (! strictly_better (cp
[-1].h
.code
, cp
[-2].h
.code
))
1101 for (j
= 0; j
< n_candidates
; j
++)
1102 if (! strictly_better (candidates
[j
].h
.code
, best_code
))
1105 qsort (candidates
+j
, n_candidates
-j
, sizeof (struct candidate
),
1107 for (i
= 0; i
< len
; i
++)
1109 if (cp
[-1].harshness
[i
].code
< cp
[-2].harshness
[i
].code
)
1111 else if (cp
[-1].harshness
[i
].code
> cp
[-2].harshness
[i
].code
)
1113 else if (cp
[-1].harshness
[i
].code
& STD_CODE
)
1115 /* If it involves a standard conversion, let the
1116 inheritance lattice be the final arbiter. */
1117 if (cp
[-1].harshness
[i
].distance
> cp
[-2].harshness
[i
].distance
)
1119 else if (cp
[-1].harshness
[i
].distance
< cp
[-2].harshness
[i
].distance
)
1122 else if (cp
[-1].harshness
[i
].code
& PROMO_CODE
)
1124 /* For integral promotions, take into account a finer
1125 granularity for determining which types should be favored
1126 over others in such promotions. */
1127 if (cp
[-1].harshness
[i
].int_penalty
> cp
[-2].harshness
[i
].int_penalty
)
1129 else if (cp
[-1].harshness
[i
].int_penalty
< cp
[-2].harshness
[i
].int_penalty
)
1134 if (! better
|| worse
)
1140 /* Assume that if the class referred to is not in the
1141 current class hierarchy, that it may be remote.
1142 PARENT is assumed to be of aggregate type here. */
1145 may_be_remote (parent
)
1148 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent
) == 0)
1151 if (current_class_type
== NULL_TREE
)
1154 if (parent
== current_class_type
)
1157 if (UNIQUELY_DERIVED_FROM_P (parent
, current_class_type
))
1163 build_vfield_ref (datum
, type
)
1167 int old_assume_nonnull_objects
= flag_assume_nonnull_objects
;
1169 if (datum
== error_mark_node
)
1170 return error_mark_node
;
1172 /* Vtable references are always made from non-null objects. */
1173 flag_assume_nonnull_objects
= 1;
1174 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
1175 datum
= convert_from_reference (datum
);
1177 if (! TYPE_USES_COMPLEX_INHERITANCE (type
))
1178 rval
= build (COMPONENT_REF
, TREE_TYPE (CLASSTYPE_VFIELD (type
)),
1179 datum
, CLASSTYPE_VFIELD (type
));
1181 rval
= build_component_ref (datum
, DECL_NAME (CLASSTYPE_VFIELD (type
)), NULL_TREE
, 0);
1182 flag_assume_nonnull_objects
= old_assume_nonnull_objects
;
1187 /* Build a call to a member of an object. I.e., one that overloads
1188 operator ()(), or is a pointer-to-function or pointer-to-method. */
1191 build_field_call (basetype_path
, instance_ptr
, name
, parms
)
1192 tree basetype_path
, instance_ptr
, name
, parms
;
1194 tree field
, instance
;
1196 if (instance_ptr
== current_class_ptr
)
1198 /* Check to see if we really have a reference to an instance variable
1199 with `operator()()' overloaded. */
1200 field
= IDENTIFIER_CLASS_VALUE (name
);
1202 if (field
== NULL_TREE
)
1204 cp_error ("`this' has no member named `%D'", name
);
1205 return error_mark_node
;
1208 if (TREE_CODE (field
) == FIELD_DECL
)
1210 /* If it's a field, try overloading operator (),
1211 or calling if the field is a pointer-to-function. */
1212 instance
= build_component_ref_1 (current_class_ref
, field
, 0);
1213 if (instance
== error_mark_node
)
1214 return error_mark_node
;
1216 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1217 && (TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance
))
1218 || flag_ansi_overloading
))
1219 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
, instance
, parms
, NULL_TREE
);
1221 if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
1223 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == FUNCTION_TYPE
)
1224 return build_function_call (instance
, parms
);
1225 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == METHOD_TYPE
)
1226 return build_function_call (instance
, tree_cons (NULL_TREE
, current_class_ptr
, parms
));
1232 /* Check to see if this is not really a reference to an instance variable
1233 with `operator()()' overloaded. */
1234 field
= lookup_field (basetype_path
, name
, 1, 0);
1236 /* This can happen if the reference was ambiguous or for access
1238 if (field
== error_mark_node
)
1239 return error_mark_node
;
1244 tree ftype
= TREE_TYPE (field
);
1246 if (TREE_CODE (ftype
) == REFERENCE_TYPE
)
1247 ftype
= TREE_TYPE (ftype
);
1249 if (TYPE_LANG_SPECIFIC (ftype
)
1250 && (TYPE_OVERLOADS_CALL_EXPR (ftype
) || flag_ansi_overloading
))
1252 /* Make the next search for this field very short. */
1253 basetype
= DECL_FIELD_CONTEXT (field
);
1254 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
1256 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
1257 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
1258 build_component_ref_1 (instance
, field
, 0),
1261 if (TREE_CODE (ftype
) == POINTER_TYPE
)
1263 if (TREE_CODE (TREE_TYPE (ftype
)) == FUNCTION_TYPE
1264 || TREE_CODE (TREE_TYPE (ftype
)) == METHOD_TYPE
)
1266 /* This is a member which is a pointer to function. */
1268 = build_component_ref_1 (build_indirect_ref (instance_ptr
,
1270 field
, LOOKUP_COMPLAIN
);
1271 if (ref
== error_mark_node
)
1272 return error_mark_node
;
1273 return build_function_call (ref
, parms
);
1276 else if (TREE_CODE (ftype
) == METHOD_TYPE
)
1278 error ("invalid call via pointer-to-member function");
1279 return error_mark_node
;
1288 find_scoped_type (type
, inner_name
, inner_types
)
1289 tree type
, inner_name
, inner_types
;
1291 tree tags
= CLASSTYPE_TAGS (type
);
1295 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1296 enclosing class) is set to the name for the enum type. So, if
1297 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1298 then this test will be true. */
1299 if (TREE_PURPOSE (tags
) == inner_name
)
1301 if (inner_types
== NULL_TREE
)
1302 return TYPE_NAME (TREE_VALUE (tags
));
1303 return resolve_scope_to_name (TREE_VALUE (tags
), inner_types
);
1305 tags
= TREE_CHAIN (tags
);
1308 /* Look for a TYPE_DECL. */
1309 for (tags
= TYPE_FIELDS (type
); tags
; tags
= TREE_CHAIN (tags
))
1310 if (TREE_CODE (tags
) == TYPE_DECL
&& DECL_NAME (tags
) == inner_name
)
1312 /* Code by raeburn. */
1313 if (inner_types
== NULL_TREE
)
1315 return resolve_scope_to_name (TREE_TYPE (tags
), inner_types
);
1321 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1322 the name that names the above nested type. INNER_TYPES
1323 is a chain of nested type names (held together by SCOPE_REFs);
1324 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1325 Returns NULL_TREE if there is an error. */
1328 resolve_scope_to_name (outer_type
, inner_stuff
)
1329 tree outer_type
, inner_stuff
;
1332 tree inner_name
, inner_type
;
1334 if (outer_type
== NULL_TREE
&& current_class_type
!= NULL_TREE
)
1336 /* We first try to look for a nesting in our current class context,
1337 then try any enclosing classes. */
1338 tree type
= current_class_type
;
1340 while (type
&& (TREE_CODE (type
) == RECORD_TYPE
1341 || TREE_CODE (type
) == UNION_TYPE
))
1343 tree rval
= resolve_scope_to_name (type
, inner_stuff
);
1345 if (rval
!= NULL_TREE
)
1347 type
= DECL_CONTEXT (TYPE_NAME (type
));
1351 if (TREE_CODE (inner_stuff
) == SCOPE_REF
)
1353 inner_name
= TREE_OPERAND (inner_stuff
, 0);
1354 inner_type
= TREE_OPERAND (inner_stuff
, 1);
1358 inner_name
= inner_stuff
;
1359 inner_type
= NULL_TREE
;
1362 if (outer_type
== NULL_TREE
)
1365 /* If we have something that's already a type by itself,
1367 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name
))
1370 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name
),
1375 x
= lookup_name (inner_name
, 0);
1377 if (x
&& TREE_CODE (x
) == NAMESPACE_DECL
)
1379 x
= lookup_namespace_name (x
, inner_type
);
1385 if (! IS_AGGR_TYPE (outer_type
))
1388 /* Look for member classes or enums. */
1389 tmp
= find_scoped_type (outer_type
, inner_name
, inner_type
);
1391 /* If it's not a type in this class, then go down into the
1392 base classes and search there. */
1393 if (! tmp
&& TYPE_BINFO (outer_type
))
1395 tree binfos
= TYPE_BINFO_BASETYPES (outer_type
);
1396 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
1398 for (i
= 0; i
< n_baselinks
; i
++)
1400 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
1401 tmp
= resolve_scope_to_name (BINFO_TYPE (base_binfo
), inner_stuff
);
1411 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1412 This is how virtual function calls are avoided. */
1415 build_scoped_method_call (exp
, basetype
, name
, parms
)
1416 tree exp
, basetype
, name
, parms
;
1418 /* Because this syntactic form does not allow
1419 a pointer to a base class to be `stolen',
1420 we need not protect the derived->base conversion
1423 @@ But we do have to check access privileges later. */
1425 tree type
= TREE_TYPE (exp
);
1427 if (type
== error_mark_node
1428 || basetype
== error_mark_node
)
1429 return error_mark_node
;
1431 if (current_template_parms
)
1433 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1435 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 1);
1436 name
= build_min_nt (BIT_NOT_EXPR
, type
);
1438 name
= build_min_nt (SCOPE_REF
, basetype
, name
);
1439 return build_min_nt (METHOD_CALL_EXPR
, name
, exp
, parms
, NULL_TREE
);
1442 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1443 type
= TREE_TYPE (type
);
1445 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1446 that explicit ~int is caught in the parser; this deals with typedefs
1447 and template parms. */
1448 if (TREE_CODE (name
) == BIT_NOT_EXPR
&& ! IS_AGGR_TYPE (basetype
))
1450 if (type
!= basetype
)
1451 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1452 exp
, basetype
, type
);
1453 name
= TREE_OPERAND (name
, 0);
1454 if (basetype
!= name
&& basetype
!= get_type_value (name
))
1455 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1457 return convert (void_type_node
, exp
);
1460 if (! is_aggr_type (basetype
, 1))
1461 return error_mark_node
;
1463 if (! IS_AGGR_TYPE (type
))
1465 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1467 return error_mark_node
;
1470 if ((binfo
= binfo_or_else (basetype
, type
)))
1472 if (binfo
== error_mark_node
)
1473 return error_mark_node
;
1474 if (TREE_CODE (exp
) == INDIRECT_REF
)
1475 decl
= build_indirect_ref (convert_pointer_to (binfo
,
1476 build_unary_op (ADDR_EXPR
, exp
, 0)), NULL_PTR
);
1478 decl
= build_scoped_ref (exp
, basetype
);
1480 /* Call to a destructor. */
1481 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1483 /* Explicit call to destructor. */
1484 name
= TREE_OPERAND (name
, 0);
1485 if (! (name
== TYPE_MAIN_VARIANT (TREE_TYPE (decl
))
1486 || 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 /* We want the address of a function or method. We avoid creating a
1536 pointer-to-member function. */
1539 build_addr_func (function
)
1542 tree type
= TREE_TYPE (function
);
1544 /* We have to do these by hand to avoid real pointer to member
1546 if (TREE_CODE (type
) == METHOD_TYPE
)
1550 type
= build_pointer_type (type
);
1552 if (mark_addressable (function
) == 0)
1553 return error_mark_node
;
1555 addr
= build1 (ADDR_EXPR
, type
, function
);
1557 /* Address of a static or external variable or function counts
1559 if (staticp (function
))
1560 TREE_CONSTANT (addr
) = 1;
1565 function
= default_conversion (function
);
1570 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1571 POINTER_TYPE to those. Note, pointer to member function types
1572 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
1575 build_call (function
, result_type
, parms
)
1576 tree function
, result_type
, parms
;
1578 int is_constructor
= 0;
1580 function
= build_addr_func (function
);
1582 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
1584 sorry ("unable to call pointer to member function here");
1585 return error_mark_node
;
1588 if (TREE_CODE (function
) == ADDR_EXPR
1589 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
1590 && DECL_CONSTRUCTOR_P (TREE_OPERAND (function
, 0)))
1593 function
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
1594 TREE_HAS_CONSTRUCTOR (function
) = is_constructor
;
1595 TREE_TYPE (function
) = result_type
;
1596 TREE_SIDE_EFFECTS (function
) = 1;
1602 default_parm_conversions (parms
, last
)
1605 tree parm
, parmtypes
= NULL_TREE
;
1609 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1611 tree t
= TREE_TYPE (TREE_VALUE (parm
));
1613 if (TREE_CODE (t
) == OFFSET_TYPE
1614 || TREE_CODE (t
) == METHOD_TYPE
1615 || TREE_CODE (t
) == FUNCTION_TYPE
)
1617 TREE_VALUE (parm
) = default_conversion (TREE_VALUE (parm
));
1618 t
= TREE_TYPE (TREE_VALUE (parm
));
1621 if (t
== error_mark_node
)
1622 return error_mark_node
;
1624 *last
= build_tree_list (NULL_TREE
, t
);
1625 parmtypes
= chainon (parmtypes
, *last
);
1632 /* Build something of the form ptr->method (args)
1633 or object.method (args). This can also build
1634 calls to constructors, and find friends.
1636 Member functions always take their class variable
1639 INSTANCE is a class instance.
1641 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1643 PARMS help to figure out what that NAME really refers to.
1645 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1646 down to the real instance type to use for access checking. We need this
1647 information to get protected accesses correct. This parameter is used
1648 by build_member_call.
1650 FLAGS is the logical disjunction of zero or more LOOKUP_
1651 flags. See cp-tree.h for more info.
1653 If this is all OK, calls build_function_call with the resolved
1656 This function must also handle being called to perform
1657 initialization, promotion/coercion of arguments, and
1658 instantiation of default parameters.
1660 Note that NAME may refer to an instance variable name. If
1661 `operator()()' is defined for the type of that field, then we return
1665 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
1666 tree instance
, name
, parms
, basetype_path
;
1669 register tree function
, fntype
, value_type
;
1670 register tree basetype
, save_basetype
;
1671 register tree baselink
, result
, parmtypes
, parm
;
1674 tree access
= access_public_node
;
1675 tree orig_basetype
= basetype_path
? BINFO_TYPE (basetype_path
) : NULL_TREE
;
1677 /* Range of cases for vtable optimization. */
1678 enum vtable_needs
{ not_needed
, maybe_needed
, unneeded
, needed
};
1679 enum vtable_needs need_vtbl
= not_needed
;
1682 tree save_name
= name
;
1684 tree instance_ptr
= NULL_TREE
;
1685 int all_virtual
= flag_all_virtual
;
1686 int static_call_context
= 0;
1687 tree found_fns
= NULL_TREE
;
1689 /* Keep track of `const' and `volatile' objects. */
1690 int constp
, volatilep
;
1692 #ifdef GATHER_STATISTICS
1693 n_build_method_call
++;
1696 if (instance
== error_mark_node
1697 || name
== error_mark_node
1698 || parms
== error_mark_node
1699 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
1700 return error_mark_node
;
1702 if (current_template_parms
)
1704 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1706 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 1);
1707 name
= build_min_nt (BIT_NOT_EXPR
, type
);
1710 return build_min_nt (METHOD_CALL_EXPR
, name
, instance
, parms
, NULL_TREE
);
1713 /* This is the logic that magically deletes the second argument to
1714 operator delete, if it is not needed. */
1715 if (name
== ansi_opname
[(int) DELETE_EXPR
] && list_length (parms
)==2)
1717 tree save_last
= TREE_CHAIN (parms
);
1719 /* get rid of unneeded argument */
1720 TREE_CHAIN (parms
) = NULL_TREE
;
1721 result
= build_method_call (instance
, name
, parms
, basetype_path
,
1722 (LOOKUP_SPECULATIVELY
|flags
)
1724 /* If it finds a match, return it. */
1726 return build_method_call (instance
, name
, parms
, basetype_path
, flags
);
1727 /* If it doesn't work, two argument delete must work */
1728 TREE_CHAIN (parms
) = save_last
;
1730 /* We already know whether it's needed or not for vec delete. */
1731 else if (name
== ansi_opname
[(int) VEC_DELETE_EXPR
]
1732 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance
))
1733 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance
)))
1734 TREE_CHAIN (parms
) = NULL_TREE
;
1736 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1738 flags
|= LOOKUP_DESTRUCTOR
;
1739 name
= TREE_OPERAND (name
, 0);
1741 error ("destructors take no parameters");
1742 basetype
= TREE_TYPE (instance
);
1743 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1744 basetype
= TREE_TYPE (basetype
);
1745 if (! (name
== basetype
1746 || (IS_AGGR_TYPE (basetype
)
1747 && name
== constructor_name (basetype
))
1748 || basetype
== get_type_value (name
)))
1750 cp_error ("destructor name `~%D' does not match type `%T' of expression",
1752 return convert (void_type_node
, instance
);
1755 if (! TYPE_HAS_DESTRUCTOR (basetype
))
1756 return convert (void_type_node
, instance
);
1757 instance
= default_conversion (instance
);
1758 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1759 return build_delete (build_pointer_type (basetype
),
1760 instance_ptr
, integer_two_node
,
1761 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
1764 if (flag_ansi_overloading
)
1765 return build_new_method_call (instance
, name
, parms
, basetype_path
, flags
);
1770 /* Initialize name for error reporting. */
1771 if (IDENTIFIER_OPNAME_P (name
) && ! IDENTIFIER_TYPENAME_P (name
))
1773 char *p
= operator_name_string (name
);
1774 xref_name
= (char *)alloca (strlen (p
) + 10);
1775 sprintf (xref_name
, "operator %s", p
);
1777 else if (TREE_CODE (name
) == SCOPE_REF
)
1778 xref_name
= IDENTIFIER_POINTER (TREE_OPERAND (name
, 1));
1780 xref_name
= IDENTIFIER_POINTER (name
);
1782 GNU_xref_call (current_function_decl
, xref_name
);
1785 if (instance
== NULL_TREE
)
1787 basetype
= NULL_TREE
;
1788 /* Check cases where this is really a call to raise
1790 if (current_class_type
&& TREE_CODE (name
) == IDENTIFIER_NODE
)
1792 basetype
= purpose_member (name
, CLASSTYPE_TAGS (current_class_type
));
1794 basetype
= TREE_VALUE (basetype
);
1796 else if (TREE_CODE (name
) == SCOPE_REF
1797 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
1799 if (! is_aggr_typedef (TREE_OPERAND (name
, 0), 1))
1800 return error_mark_node
;
1801 basetype
= purpose_member (TREE_OPERAND (name
, 1),
1802 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name
, 0))));
1804 basetype
= TREE_VALUE (basetype
);
1807 if (basetype
!= NULL_TREE
)
1809 /* call to a constructor... */
1810 else if (basetype_path
)
1812 basetype
= BINFO_TYPE (basetype_path
);
1813 if (name
== TYPE_IDENTIFIER (basetype
))
1814 name
= ctor_identifier
;
1816 else if (IDENTIFIER_HAS_TYPE_VALUE (name
))
1818 basetype
= IDENTIFIER_TYPE_VALUE (name
);
1819 name
= ctor_identifier
;
1823 tree typedef_name
= lookup_name (name
, 1);
1824 if (typedef_name
&& TREE_CODE (typedef_name
) == TYPE_DECL
)
1826 /* Canonicalize the typedef name. */
1827 basetype
= TREE_TYPE (typedef_name
);
1828 name
= ctor_identifier
;
1832 cp_error ("no constructor named `%T' in scope",
1834 return error_mark_node
;
1838 if (! IS_AGGR_TYPE (basetype
))
1841 if ((flags
& LOOKUP_COMPLAIN
) && TREE_CODE (basetype
) != ERROR_MARK
)
1842 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1843 name
, instance
, basetype
);
1845 return error_mark_node
;
1848 else if (instance
== current_class_ref
|| instance
== current_class_ptr
)
1850 /* When doing initialization, we side-effect the TREE_TYPE of
1851 current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1852 basetype
= TREE_TYPE (current_class_ref
);
1854 /* Anything manifestly `this' in constructors and destructors
1855 has a known type, so virtual function tables are not needed. */
1856 if (TYPE_VIRTUAL_P (basetype
)
1857 && !(flags
& LOOKUP_NONVIRTUAL
))
1858 need_vtbl
= (dtor_label
|| ctor_label
)
1859 ? unneeded
: maybe_needed
;
1861 /* If `this' is a signature pointer and `name' is not a constructor,
1862 we are calling a signature member function. In that case, set the
1863 `basetype' to the signature type and dereference the `optr' field. */
1864 if (IS_SIGNATURE_POINTER (basetype
)
1865 && TYPE_IDENTIFIER (basetype
) != name
)
1867 basetype
= SIGNATURE_TYPE (basetype
);
1868 instance_ptr
= build_optr_ref (instance
);
1869 instance_ptr
= convert (build_pointer_type (basetype
), instance_ptr
);
1870 basetype_path
= TYPE_BINFO (basetype
);
1874 instance
= current_class_ref
;
1875 instance_ptr
= current_class_ptr
;
1876 basetype_path
= TYPE_BINFO (current_class_type
);
1878 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
1883 else if (TREE_CODE (instance
) == RESULT_DECL
)
1885 basetype
= TREE_TYPE (instance
);
1886 /* Should we ever have to make a virtual function reference
1887 from a RESULT_DECL, know that it must be of fixed type
1888 within the scope of this function. */
1889 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
1890 need_vtbl
= maybe_needed
;
1891 instance_ptr
= build1 (ADDR_EXPR
, build_pointer_type (basetype
), instance
);
1895 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1896 tree inst_ptr_basetype
;
1898 static_call_context
=
1899 (TREE_CODE (instance
) == INDIRECT_REF
1900 && TREE_CODE (TREE_OPERAND (instance
, 0)) == NOP_EXPR
1901 && TREE_OPERAND (TREE_OPERAND (instance
, 0), 0) == error_mark_node
);
1903 if (TREE_CODE (instance
) == OFFSET_REF
)
1904 instance
= resolve_offset_ref (instance
);
1906 /* the base type of an instance variable is pointer to class */
1907 basetype
= TREE_TYPE (instance
);
1909 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1911 basetype
= TREE_TYPE (basetype
);
1912 if (! IS_AGGR_TYPE (basetype
))
1913 goto non_aggr_error
;
1914 /* Call to convert not needed because we are remaining
1915 within the same type. */
1916 instance_ptr
= build1 (NOP_EXPR
, build_pointer_type (basetype
),
1918 inst_ptr_basetype
= TYPE_MAIN_VARIANT (basetype
);
1922 if (! IS_AGGR_TYPE (basetype
)
1923 && ! (TYPE_LANG_SPECIFIC (basetype
)
1924 && (IS_SIGNATURE_POINTER (basetype
)
1925 || IS_SIGNATURE_REFERENCE (basetype
))))
1926 goto non_aggr_error
;
1928 /* If `instance' is a signature pointer/reference and `name' is
1929 not a constructor, we are calling a signature member function.
1930 In that case set the `basetype' to the signature type. */
1931 if ((IS_SIGNATURE_POINTER (basetype
)
1932 || IS_SIGNATURE_REFERENCE (basetype
))
1933 && TYPE_IDENTIFIER (basetype
) != name
)
1934 basetype
= SIGNATURE_TYPE (basetype
);
1936 basetype
= complete_type (basetype
);
1938 if ((IS_SIGNATURE (basetype
)
1939 && (instance_ptr
= instance
))
1940 || (lvalue_p (instance
)
1941 && (instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0)))
1942 || (instance_ptr
= unary_complex_lvalue (ADDR_EXPR
, instance
)))
1944 if (instance_ptr
== error_mark_node
)
1945 return error_mark_node
;
1947 else if (TREE_CODE (instance
) == NOP_EXPR
1948 || TREE_CODE (instance
) == CONSTRUCTOR
)
1950 /* A cast is not an lvalue. Initialize a fresh temp
1951 with the value we are casting from, and proceed with
1952 that temporary. We can't cast to a reference type,
1953 so that simplifies the initialization to something
1955 tree temp
= get_temp_name (TREE_TYPE (instance
), 0);
1956 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
1957 expand_aggr_init (temp
, instance
, 0, flags
);
1960 store_init_value (temp
, instance
);
1961 expand_decl_init (temp
);
1964 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1968 if (TREE_CODE (instance
) != CALL_EXPR
)
1969 my_friendly_abort (125);
1970 if (TYPE_NEEDS_CONSTRUCTING (basetype
))
1971 instance
= build_cplus_new (basetype
, instance
);
1974 instance
= get_temp_name (basetype
, 0);
1975 TREE_ADDRESSABLE (instance
) = 1;
1977 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
1979 /* @@ Should we call comp_target_types here? */
1980 if (IS_SIGNATURE (basetype
))
1981 inst_ptr_basetype
= basetype
;
1983 inst_ptr_basetype
= TREE_TYPE (TREE_TYPE (instance_ptr
));
1984 if (TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (inst_ptr_basetype
))
1985 basetype
= inst_ptr_basetype
;
1988 instance_ptr
= convert (build_pointer_type (basetype
), instance_ptr
);
1989 if (instance_ptr
== error_mark_node
)
1990 return error_mark_node
;
1994 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1995 not updated, so we use `basetype' instead. */
1996 if (basetype_path
== NULL_TREE
1997 && IS_SIGNATURE (basetype
))
1998 basetype_path
= TYPE_BINFO (basetype
);
1999 else if (basetype_path
== NULL_TREE
||
2000 BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (inst_ptr_basetype
))
2001 basetype_path
= TYPE_BINFO (inst_ptr_basetype
);
2003 result
= build_field_call (basetype_path
, instance_ptr
, name
, parms
);
2007 if (!(flags
& LOOKUP_NONVIRTUAL
) && TYPE_VIRTUAL_P (basetype
))
2009 if (TREE_SIDE_EFFECTS (instance_ptr
))
2011 /* This action is needed because the instance is needed
2012 for providing the base of the virtual function table.
2013 Without using a SAVE_EXPR, the function we are building
2014 may be called twice, or side effects on the instance
2015 variable (such as a post-increment), may happen twice. */
2016 instance_ptr
= save_expr (instance_ptr
);
2017 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2019 else if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
2021 /* This happens when called for operator new (). */
2022 instance
= build_indirect_ref (instance
, NULL_PTR
);
2025 need_vtbl
= maybe_needed
;
2029 if (save_name
== ctor_identifier
)
2030 save_name
= TYPE_IDENTIFIER (basetype
);
2032 if (TYPE_SIZE (complete_type (basetype
)) == 0)
2034 /* This is worth complaining about, I think. */
2035 cp_error ("cannot lookup method in incomplete type `%T'", basetype
);
2036 return error_mark_node
;
2039 save_basetype
= TYPE_MAIN_VARIANT (basetype
);
2041 parmtypes
= default_parm_conversions (parms
, &last
);
2042 if (parmtypes
== error_mark_node
)
2044 return error_mark_node
;
2047 if (instance
&& IS_SIGNATURE (basetype
))
2049 /* @@ Should this be the constp/volatilep flags for the optr field
2050 of the signature pointer? */
2051 constp
= TYPE_READONLY (basetype
);
2052 volatilep
= TYPE_VOLATILE (basetype
);
2053 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2057 /* TREE_READONLY (instance) fails for references. */
2058 constp
= TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2059 volatilep
= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2060 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2064 /* Raw constructors are always in charge. */
2065 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
2066 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
2068 flags
|= LOOKUP_HAS_IN_CHARGE
;
2069 parms
= tree_cons (NULL_TREE
, integer_one_node
, parms
);
2070 parmtypes
= tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
2075 instance_ptr
= build_int_2 (0, 0);
2076 TREE_TYPE (instance_ptr
) = build_pointer_type (basetype
);
2077 parms
= tree_cons (NULL_TREE
, instance_ptr
, parms
);
2080 parmtypes
= tree_cons (NULL_TREE
, TREE_TYPE (instance_ptr
), parmtypes
);
2082 if (last
== NULL_TREE
)
2085 /* Look up function name in the structure type definition. */
2087 /* FIXME Axe most of this now? */
2088 if ((IDENTIFIER_HAS_TYPE_VALUE (name
)
2089 && ! IDENTIFIER_OPNAME_P (name
)
2090 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name
)))
2091 || name
== constructor_name (basetype
)
2092 || name
== ctor_identifier
)
2094 tree tmp
= NULL_TREE
;
2095 if (IDENTIFIER_TYPE_VALUE (name
) == basetype
2096 || name
== constructor_name (basetype
)
2097 || name
== ctor_identifier
)
2098 tmp
= TYPE_BINFO (basetype
);
2100 tmp
= get_binfo (IDENTIFIER_TYPE_VALUE (name
), basetype
, 0);
2102 if (tmp
!= NULL_TREE
)
2104 name_kind
= "constructor";
2106 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
2107 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
2109 /* Constructors called for initialization
2110 only are never in charge. */
2113 flags
|= LOOKUP_HAS_IN_CHARGE
;
2114 tmplist
= tree_cons (NULL_TREE
, integer_zero_node
,
2115 TREE_CHAIN (parms
));
2116 TREE_CHAIN (parms
) = tmplist
;
2117 tmplist
= tree_cons (NULL_TREE
, integer_type_node
, TREE_CHAIN (parmtypes
));
2118 TREE_CHAIN (parmtypes
) = tmplist
;
2120 basetype
= BINFO_TYPE (tmp
);
2123 name_kind
= "method";
2126 name_kind
= "method";
2128 if (basetype_path
== NULL_TREE
2129 || BINFO_TYPE (basetype_path
) != TYPE_MAIN_VARIANT (basetype
))
2130 basetype_path
= TYPE_BINFO (basetype
);
2131 result
= lookup_fnfields (basetype_path
, name
,
2132 (flags
& LOOKUP_COMPLAIN
));
2133 if (result
== error_mark_node
)
2134 return error_mark_node
;
2136 for (pass
= 0; pass
< 2; pass
++)
2138 struct candidate
*candidates
;
2139 struct candidate
*cp
;
2148 = (struct candidate
*) alloca ((ever_seen
+1)
2149 * sizeof (struct candidate
));
2150 bzero ((char *) candidates
, (ever_seen
+ 1) * sizeof (struct candidate
));
2152 len
= list_length (parms
);
2155 /* First see if a global function has a shot at it. */
2156 if (flags
& LOOKUP_GLOBAL
)
2159 tree parm
= instance_ptr
;
2161 if (TREE_CODE (TREE_TYPE (parm
)) == REFERENCE_TYPE
)
2162 parm
= convert_from_reference (parm
);
2163 else if (TREE_CODE (TREE_TYPE (parm
)) == POINTER_TYPE
)
2164 parm
= build_indirect_ref (parm
, "friendifying parms (compiler error)");
2166 my_friendly_abort (167);
2168 friend_parms
= tree_cons (NULL_TREE
, parm
, TREE_CHAIN (parms
));
2171 cp
->harshness
= (struct harshness_code
*)
2172 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2174 result
= build_overload_call_real (name
, friend_parms
, 0, cp
, 1);
2176 /* If it turns out to be the one we were actually looking for
2177 (it was probably a friend function), the return the
2179 if (TREE_CODE (result
) == CALL_EXPR
)
2182 while ((cp
->h
.code
& EVIL_CODE
) == 0)
2184 /* non-standard uses: set the field to 0 to indicate
2185 we are using a non-member function. */
2187 if (cp
->harshness
[len
].distance
== 0
2188 && cp
->h
.code
< best
)
2197 /* We have a hit (of sorts). If the parameter list is
2198 "error_mark_node", or some variant thereof, it won't
2199 match any methods. Since we have verified that the is
2200 some method vaguely matching this one (in name at least),
2203 Don't stop for friends, however. */
2204 basetype_path
= TREE_PURPOSE (baselink
);
2206 function
= TREE_VALUE (baselink
);
2207 if (TREE_CODE (basetype_path
) == TREE_LIST
)
2208 basetype_path
= TREE_VALUE (basetype_path
);
2209 basetype
= BINFO_TYPE (basetype_path
);
2211 for (; function
; function
= DECL_CHAIN (function
))
2213 #ifdef GATHER_STATISTICS
2214 n_inner_fields_searched
++;
2218 found_fns
= tree_cons (NULL_TREE
, function
, found_fns
);
2220 /* Not looking for friends here. */
2221 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
2222 && ! DECL_STATIC_FUNCTION_P (function
))
2227 tree these_parms
= parms
;
2229 #ifdef GATHER_STATISTICS
2230 n_inner_fields_searched
++;
2233 cp
->harshness
= (struct harshness_code
*)
2234 alloca ((len
+ 1) * sizeof (struct harshness_code
));
2236 if (DECL_STATIC_FUNCTION_P (function
))
2237 these_parms
= TREE_CHAIN (these_parms
);
2238 compute_conversion_costs (function
, these_parms
, cp
, len
);
2240 if ((cp
->h
.code
& EVIL_CODE
) == 0)
2242 cp
->u
.field
= function
;
2243 cp
->function
= function
;
2244 cp
->basetypes
= basetype_path
;
2246 /* Don't allow non-converting constructors to convert. */
2247 if (flags
& LOOKUP_ONLYCONVERTING
2248 && DECL_LANG_SPECIFIC (function
)
2249 && DECL_NONCONVERTING_P (function
))
2252 /* No "two-level" conversions. */
2253 if (flags
& LOOKUP_NO_CONVERSION
2254 && (cp
->h
.code
& USER_CODE
))
2265 tree igv
= lookup_name_nonclass (name
);
2267 /* No exact match could be found. Now try to find match
2268 using default conversions. */
2269 if ((flags
& LOOKUP_GLOBAL
) && igv
)
2271 if (TREE_CODE (igv
) == FUNCTION_DECL
)
2273 else if (TREE_CODE (igv
) == TREE_LIST
)
2274 ever_seen
+= count_functions (igv
);
2279 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2280 == LOOKUP_SPECULATIVELY
)
2283 TREE_CHAIN (last
) = void_list_node
;
2284 if (flags
& LOOKUP_GLOBAL
)
2285 cp_error ("no global or member function `%D(%A)' defined",
2286 save_name
, parmtypes
);
2288 cp_error ("no member function `%T::%D(%A)' defined",
2289 save_basetype
, save_name
, TREE_CHAIN (parmtypes
));
2290 return error_mark_node
;
2295 if (cp
- candidates
!= 0)
2297 /* Rank from worst to best. Then cp will point to best one.
2298 Private fields have their bits flipped. For unsigned
2299 numbers, this should make them look very large.
2300 If the best alternate has a (signed) negative value,
2301 then all we ever saw were private members. */
2302 if (cp
- candidates
> 1)
2304 int n_candidates
= cp
- candidates
;
2305 extern int warn_synth
;
2306 TREE_VALUE (parms
) = instance_ptr
;
2307 cp
= ideal_candidate (candidates
, n_candidates
, len
);
2308 if (cp
== (struct candidate
*)0)
2310 if (flags
& LOOKUP_COMPLAIN
)
2312 TREE_CHAIN (last
) = void_list_node
;
2313 cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2314 name_kind
, save_name
, TREE_CHAIN (parmtypes
));
2315 print_n_candidates (candidates
, n_candidates
);
2317 return error_mark_node
;
2319 if (cp
->h
.code
& EVIL_CODE
)
2320 return error_mark_node
;
2322 && DECL_NAME (cp
->function
) == ansi_opname
[MODIFY_EXPR
]
2323 && DECL_ARTIFICIAL (cp
->function
)
2324 && n_candidates
== 2)
2326 cp_warning ("using synthesized `%#D' for copy assignment",
2328 cp_warning_at (" where cfront would use `%#D'",
2329 candidates
->function
);
2332 else if (cp
[-1].h
.code
& EVIL_CODE
)
2334 if (flags
& LOOKUP_COMPLAIN
)
2335 cp_error ("ambiguous type conversion requested for %s `%D'",
2336 name_kind
, save_name
);
2337 return error_mark_node
;
2342 /* The global function was the best, so use it. */
2343 if (cp
->u
.field
== 0)
2345 /* We must convert the instance pointer into a reference type.
2346 Global overloaded functions can only either take
2347 aggregate objects (which come for free from references)
2348 or reference data types anyway. */
2349 TREE_VALUE (parms
) = copy_node (instance_ptr
);
2350 TREE_TYPE (TREE_VALUE (parms
)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr
)));
2351 return build_function_call (cp
->function
, parms
);
2354 function
= cp
->function
;
2355 basetype_path
= cp
->basetypes
;
2356 if (! DECL_STATIC_FUNCTION_P (function
))
2357 TREE_VALUE (parms
) = cp
->arg
;
2358 goto found_and_maybe_warn
;
2361 if (flags
& (LOOKUP_COMPLAIN
|LOOKUP_SPECULATIVELY
))
2363 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2364 == LOOKUP_SPECULATIVELY
)
2367 if (DECL_STATIC_FUNCTION_P (cp
->function
))
2368 parms
= TREE_CHAIN (parms
);
2371 if (flags
& LOOKUP_SPECULATIVELY
)
2373 if (static_call_context
2374 && TREE_CODE (TREE_TYPE (cp
->function
)) == METHOD_TYPE
)
2375 cp_error ("object missing in call to `%D'", cp
->function
);
2376 else if (ever_seen
> 1)
2378 TREE_CHAIN (last
) = void_list_node
;
2379 cp_error ("no matching function for call to `%T::%D (%A)%V'",
2380 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr
))),
2381 save_name
, TREE_CHAIN (parmtypes
),
2382 TREE_TYPE (TREE_TYPE (instance_ptr
)));
2383 TREE_CHAIN (last
) = NULL_TREE
;
2384 print_candidates (found_fns
);
2387 report_type_mismatch (cp
, parms
, name_kind
);
2388 return error_mark_node
;
2391 if ((flags
& (LOOKUP_SPECULATIVELY
|LOOKUP_COMPLAIN
))
2394 cp_error ("%T has no method named %D", save_basetype
, save_name
);
2395 return error_mark_node
;
2401 found_and_maybe_warn
:
2402 if ((cp
->harshness
[0].code
& CONST_CODE
)
2403 /* 12.1p2: Constructors can be called for const objects. */
2404 && ! DECL_CONSTRUCTOR_P (cp
->function
))
2406 if (flags
& LOOKUP_COMPLAIN
)
2408 cp_error_at ("non-const member function `%D'", cp
->function
);
2409 error ("called for const object at this point in file");
2411 /* Not good enough for a match. */
2413 return error_mark_node
;
2417 /* Silently return error_mark_node. */
2418 return error_mark_node
;
2421 if (flags
& LOOKUP_PROTECT
)
2422 access
= compute_access (basetype_path
, function
);
2424 if (access
== access_private_node
)
2426 if (flags
& LOOKUP_COMPLAIN
)
2428 cp_error_at ("%s `%+#D' is %s", name_kind
, function
,
2429 TREE_PRIVATE (function
) ? "private"
2430 : "from private base class");
2431 error ("within this context");
2433 return error_mark_node
;
2435 else if (access
== access_protected_node
)
2437 if (flags
& LOOKUP_COMPLAIN
)
2439 cp_error_at ("%s `%+#D' %s", name_kind
, function
,
2440 TREE_PROTECTED (function
) ? "is protected"
2441 : "has protected accessibility");
2442 error ("within this context");
2444 return error_mark_node
;
2447 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2448 type (if it exists) is a pointer to. */
2450 if (DECL_ABSTRACT_VIRTUAL_P (function
)
2451 && instance
== current_class_ref
2452 && DECL_CONSTRUCTOR_P (current_function_decl
)
2453 && ! (flags
& LOOKUP_NONVIRTUAL
)
2454 && value_member (function
, get_abstract_virtuals (basetype
)))
2455 cp_error ("abstract virtual `%#D' called from constructor", function
);
2457 if (IS_SIGNATURE (basetype
))
2459 if (static_call_context
)
2461 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2462 basetype
, save_name
);
2463 return error_mark_node
;
2465 return build_signature_method_call (basetype
, instance
, function
, parms
);
2468 function
= DECL_MAIN_VARIANT (function
);
2469 mark_used (function
);
2471 if (pedantic
&& DECL_THIS_INLINE (function
) && ! DECL_ARTIFICIAL (function
)
2472 && ! DECL_INITIAL (function
) && ! DECL_PENDING_INLINE_INFO (function
)
2473 && ! (DECL_TEMPLATE_INFO (function
)
2474 && TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (function
))))
2475 cp_warning ("inline function `%#D' called before definition", function
);
2477 fntype
= TREE_TYPE (function
);
2478 if (TREE_CODE (fntype
) == POINTER_TYPE
)
2479 fntype
= TREE_TYPE (fntype
);
2480 basetype
= DECL_CLASS_CONTEXT (function
);
2482 /* If we are referencing a virtual function from an object
2483 of effectively static type, then there is no need
2484 to go through the virtual function table. */
2485 if (need_vtbl
== maybe_needed
)
2487 int fixed_type
= resolves_to_fixed_type_p (instance
, 0);
2489 if (all_virtual
== 1
2490 && DECL_VINDEX (function
)
2491 && may_be_remote (basetype
))
2493 else if (DECL_VINDEX (function
))
2494 need_vtbl
= fixed_type
? unneeded
: needed
;
2496 need_vtbl
= not_needed
;
2499 if (TREE_CODE (fntype
) == METHOD_TYPE
&& static_call_context
2500 && !DECL_CONSTRUCTOR_P (function
))
2502 /* Let's be nasty to the user now, and give reasonable
2504 instance_ptr
= current_class_ptr
;
2507 if (basetype
!= current_class_type
)
2509 if (basetype
== error_mark_node
)
2510 return error_mark_node
;
2513 if (orig_basetype
!= NULL_TREE
)
2514 error_not_base_type (orig_basetype
, current_class_type
);
2516 error_not_base_type (function
, current_class_type
);
2517 return error_mark_node
;
2521 /* Only allow a static member function to call another static member
2523 else if (DECL_LANG_SPECIFIC (function
)
2524 && !DECL_STATIC_FUNCTION_P (function
))
2526 cp_error ("cannot call member function `%D' without object",
2528 return error_mark_node
;
2532 value_type
= TREE_TYPE (fntype
) ? TREE_TYPE (fntype
) : void_type_node
;
2534 if (TYPE_SIZE (complete_type (value_type
)) == 0)
2536 if (flags
& LOOKUP_COMPLAIN
)
2537 incomplete_type_error (0, value_type
);
2538 return error_mark_node
;
2541 if (DECL_STATIC_FUNCTION_P (function
))
2542 parms
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
2543 TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
);
2544 else if (need_vtbl
== unneeded
)
2546 int sub_flags
= DECL_CONSTRUCTOR_P (function
) ? flags
: LOOKUP_NORMAL
;
2547 basetype
= TREE_TYPE (instance
);
2548 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function
))
2549 != TYPE_MAIN_VARIANT (basetype
))
2551 basetype
= DECL_CLASS_CONTEXT (function
);
2552 instance_ptr
= convert_pointer_to (basetype
, instance_ptr
);
2553 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2555 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2556 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, sub_flags
));
2560 if ((flags
& LOOKUP_NONVIRTUAL
) == 0)
2561 basetype
= DECL_CONTEXT (function
);
2563 /* First parm could be integer_zerop with casts like
2564 ((Object*)0)->Object::IsA() */
2565 if (!integer_zerop (TREE_VALUE (parms
)))
2567 /* Since we can't have inheritance with a union, doing get_binfo
2568 on it won't work. We do all the convert_pointer_to_real
2569 stuff to handle MI correctly...for unions, that's not
2570 an issue, so we must short-circuit that extra work here. */
2571 tree tmp
= TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
)));
2572 if (tmp
!= NULL_TREE
&& TREE_CODE (tmp
) == UNION_TYPE
)
2573 instance_ptr
= TREE_VALUE (parms
);
2576 tree binfo
= get_binfo (basetype
,
2577 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms
))),
2579 instance_ptr
= convert_pointer_to_real (binfo
, TREE_VALUE (parms
));
2582 = convert_pointer_to (build_type_variant (basetype
,
2586 if (TREE_CODE (instance_ptr
) == COND_EXPR
)
2588 instance_ptr
= save_expr (instance_ptr
);
2589 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2591 else if (TREE_CODE (instance_ptr
) == NOP_EXPR
2592 && TREE_CODE (TREE_OPERAND (instance_ptr
, 0)) == ADDR_EXPR
2593 && TREE_OPERAND (TREE_OPERAND (instance_ptr
, 0), 0) == instance
)
2595 /* The call to `convert_pointer_to' may return error_mark_node. */
2596 else if (TREE_CODE (instance_ptr
) == ERROR_MARK
)
2597 return instance_ptr
;
2598 else if (instance
== NULL_TREE
2599 || TREE_CODE (instance
) != INDIRECT_REF
2600 || TREE_OPERAND (instance
, 0) != instance_ptr
)
2601 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
2603 parms
= tree_cons (NULL_TREE
, instance_ptr
,
2604 convert_arguments (NULL_TREE
, TREE_CHAIN (TYPE_ARG_TYPES (fntype
)), TREE_CHAIN (parms
), function
, LOOKUP_NORMAL
));
2607 if (parms
== error_mark_node
2608 || (parms
&& TREE_CHAIN (parms
) == error_mark_node
))
2609 return error_mark_node
;
2611 if (need_vtbl
== needed
)
2613 function
= build_vfn_ref (&TREE_VALUE (parms
), instance
,
2614 DECL_VINDEX (function
));
2615 TREE_TYPE (function
) = build_pointer_type (fntype
);
2618 if (TREE_CODE (function
) == FUNCTION_DECL
)
2619 GNU_xref_call (current_function_decl
,
2620 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
)));
2622 result
= build_call (function
, value_type
, parms
);
2623 if (IS_AGGR_TYPE (value_type
))
2624 result
= build_cplus_new (value_type
, result
);
2625 result
= convert_from_reference (result
);
2629 /* Similar to `build_method_call', but for overloaded non-member functions.
2630 The name of this function comes through NAME. The name depends
2633 Note that this function must handle simple `C' promotions,
2634 as well as variable numbers of arguments (...), and
2635 default arguments to boot.
2637 If the overloading is successful, we return a tree node which
2638 contains the call to the function.
2640 If overloading produces candidates which are probable, but not definite,
2641 we hold these candidates. If FINAL_CP is non-zero, then we are free
2642 to assume that final_cp points to enough storage for all candidates that
2643 this function might generate. The `harshness' array is preallocated for
2644 the first candidate, but not for subsequent ones.
2646 Note that the DECL_RTL of FUNCTION must be made to agree with this
2647 function's new name. */
2650 build_overload_call_real (fnname
, parms
, flags
, final_cp
, require_complete
)
2653 struct candidate
*final_cp
;
2654 int require_complete
;
2656 /* must check for overloading here */
2657 tree functions
, function
, parm
;
2658 tree parmtypes
, last
;
2659 register tree outer
;
2661 int parmlength
= list_length (parms
);
2663 struct candidate
*candidates
, *cp
;
2667 final_cp
[0].h
.code
= 0;
2668 final_cp
[0].h
.distance
= 0;
2669 final_cp
[0].function
= 0;
2671 final_cp
[1].h
.code
= EVIL_CODE
;
2674 parmtypes
= default_parm_conversions (parms
, &last
);
2675 if (parmtypes
== error_mark_node
)
2678 final_cp
->h
.code
= EVIL_CODE
;
2679 return error_mark_node
;
2683 TREE_CHAIN (last
) = void_list_node
;
2685 parmtypes
= void_list_node
;
2687 if (is_overloaded_fn (fnname
))
2690 if (TREE_CODE (fnname
) == TREE_LIST
)
2691 fnname
= TREE_PURPOSE (functions
);
2692 else if (TREE_CODE (fnname
) == FUNCTION_DECL
)
2693 fnname
= DECL_NAME (functions
);
2696 functions
= lookup_name_nonclass (fnname
);
2698 if (functions
== NULL_TREE
)
2700 if (flags
& LOOKUP_SPECULATIVELY
)
2702 if (flags
& LOOKUP_COMPLAIN
)
2703 error ("only member functions apply");
2705 final_cp
->h
.code
= EVIL_CODE
;
2706 return error_mark_node
;
2709 if (TREE_CODE (functions
) == FUNCTION_DECL
&& ! IDENTIFIER_OPNAME_P (fnname
))
2711 functions
= DECL_MAIN_VARIANT (functions
);
2714 /* We are just curious whether this is a viable alternative or
2716 compute_conversion_costs (functions
, parms
, final_cp
, parmlength
);
2720 return build_function_call_real (functions
, parms
, 1, flags
);
2723 if (TREE_CODE (functions
) == TREE_LIST
2724 && TREE_VALUE (functions
) == NULL_TREE
)
2726 if (flags
& LOOKUP_SPECULATIVELY
)
2729 if (flags
& LOOKUP_COMPLAIN
)
2730 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2731 TREE_PURPOSE (functions
));
2733 final_cp
->h
.code
= EVIL_CODE
;
2734 return error_mark_node
;
2737 length
= count_functions (functions
);
2740 candidates
= final_cp
;
2744 = (struct candidate
*)alloca ((length
+1) * sizeof (struct candidate
));
2745 bzero ((char *) candidates
, (length
+ 1) * sizeof (struct candidate
));
2750 my_friendly_assert (is_overloaded_fn (functions
), 169);
2752 functions
= get_first_fn (functions
);
2754 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2755 for (outer
= functions
; outer
; outer
= DECL_CHAIN (outer
))
2757 int template_cost
= 0;
2759 if (TREE_CODE (function
) != FUNCTION_DECL
2760 && ! (TREE_CODE (function
) == TEMPLATE_DECL
2761 && TREE_CODE (DECL_TEMPLATE_RESULT (function
)) == FUNCTION_DECL
))
2763 enum tree_code code
= TREE_CODE (function
);
2764 if (code
== TEMPLATE_DECL
)
2765 code
= TREE_CODE (DECL_TEMPLATE_RESULT (function
));
2766 if (code
== CONST_DECL
)
2768 ("enumeral value `%D' conflicts with function of same name",
2770 else if (code
== VAR_DECL
)
2772 if (TREE_STATIC (function
))
2774 ("variable `%D' conflicts with function of same name",
2778 ("constant field `%D' conflicts with function of same name",
2781 else if (code
== TYPE_DECL
)
2784 my_friendly_abort (2);
2785 error ("at this point in file");
2788 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2790 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function
));
2791 tree
*targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2794 i
= type_unification (DECL_TEMPLATE_PARMS (function
), targs
,
2795 TYPE_ARG_TYPES (TREE_TYPE (function
)),
2796 parms
, &template_cost
, 0, 0);
2799 function
= instantiate_template (function
, targs
);
2800 if (function
== error_mark_node
)
2805 if (TREE_CODE (function
) == TEMPLATE_DECL
)
2807 /* Unconverted template -- failed match. */
2808 cp
->function
= function
;
2810 cp
->h
.code
= EVIL_CODE
;
2814 struct candidate
*cp2
;
2816 /* Check that this decl is not the same as a function that's in
2817 the list due to some template instantiation. */
2820 if (cp2
->function
== function
)
2824 if (cp2
->function
== function
)
2827 function
= DECL_MAIN_VARIANT (function
);
2829 /* Can't use alloca here, since result might be
2830 passed to calling function. */
2831 cp
->h_len
= parmlength
;
2832 cp
->harshness
= (struct harshness_code
*)
2833 oballoc ((parmlength
+ 1) * sizeof (struct harshness_code
));
2835 compute_conversion_costs (function
, parms
, cp
, parmlength
);
2837 /* Make sure this is clear as well. */
2838 cp
->h
.int_penalty
+= template_cost
;
2840 if ((cp
[0].h
.code
& EVIL_CODE
) == 0)
2842 cp
[1].h
.code
= EVIL_CODE
;
2848 if (cp
- candidates
)
2850 tree rval
= error_mark_node
;
2853 cp
[0].h
.code
= EVIL_CODE
;
2854 if (cp
- candidates
> 1)
2856 struct candidate
*best_cp
2857 = ideal_candidate (candidates
, cp
- candidates
, parmlength
);
2858 if (best_cp
== (struct candidate
*)0)
2860 if (flags
& LOOKUP_COMPLAIN
)
2862 cp_error ("call of overloaded `%D' is ambiguous", fnname
);
2863 print_n_candidates (candidates
, cp
- candidates
);
2865 return error_mark_node
;
2868 rval
= best_cp
->function
;
2873 if (cp
->h
.code
& EVIL_CODE
)
2875 if (flags
& LOOKUP_COMPLAIN
)
2876 error ("type conversion ambiguous");
2879 rval
= cp
->function
;
2885 return build_function_call_real (rval
, parms
, require_complete
, flags
);
2888 if (flags
& LOOKUP_SPECULATIVELY
)
2891 if (flags
& LOOKUP_COMPLAIN
)
2892 report_type_mismatch (cp
, parms
, "function",
2893 decl_as_string (cp
->function
, 1));
2895 return error_mark_node
;
2898 /* This requires a complete type on the result of the call. */
2901 build_overload_call (fnname
, parms
, flags
)
2905 return build_overload_call_real (fnname
, parms
, flags
, (struct candidate
*)0, 1);
2908 /* New overloading code. */
2910 struct z_candidate
{
2917 struct z_candidate
*next
;
2920 #define IDENTITY_RANK 0
2921 #define EXACT_RANK 1
2922 #define PROMO_RANK 2
2924 #define PBOOL_RANK 4
2926 #define ELLIPSIS_RANK 6
2928 #define ICS_RANK(NODE) \
2929 (ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
2930 : ICS_USER_FLAG (NODE) ? USER_RANK \
2931 : ICS_STD_RANK (NODE))
2933 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
2935 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
2936 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
2938 #define USER_CONV_FN(NODE) TREE_OPERAND (NODE, 1)
2940 static struct z_candidate
* build_user_type_conversion_1 ();
2941 static tree
convert_like ();
2942 static tree
build_over_call ();
2943 static struct z_candidate
* tourney ();
2944 static void enforce_access ();
2950 if (t
== null_pointer_node
2951 || integer_zerop (t
) && INTEGRAL_TYPE_P (TREE_TYPE (t
)))
2953 /* Remove this eventually. */
2954 if (! pedantic
&& TREE_TYPE (t
) == ptr_type_node
&& integer_zerop (t
))
2960 build_conv (code
, type
, from
)
2961 enum tree_code code
;
2964 tree t
= build1 (code
, type
, from
);
2965 int rank
= ICS_STD_RANK (from
);
2972 if (rank
< STD_RANK
)
2979 if (rank
< EXACT_RANK
)
2985 ICS_STD_RANK (t
) = rank
;
2986 ICS_USER_FLAG (t
) = ICS_USER_FLAG (from
);
2994 if (TREE_CODE (t
) == REFERENCE_TYPE
)
2999 /* Returns the standard conversion path (see [conv]) from type FROM to type
3000 TO, if any. For proper handling of null pointer constants, you must
3001 also pass the expression EXPR to convert from. */
3004 standard_conversion (to
, from
, expr
)
3005 tree to
, from
, expr
;
3007 enum tree_code fcode
, tcode
;
3010 fcode
= TREE_CODE (from
);
3011 tcode
= TREE_CODE (to
);
3013 conv
= build1 (IDENTITY_CONV
, from
, expr
);
3018 if (fcode
== FUNCTION_TYPE
)
3020 from
= build_pointer_type (from
);
3021 fcode
= TREE_CODE (from
);
3022 conv
= build_conv (LVALUE_CONV
, from
, conv
);
3024 else if (fcode
== ARRAY_TYPE
)
3026 from
= build_pointer_type (TREE_TYPE (from
));
3027 fcode
= TREE_CODE (from
);
3028 conv
= build_conv (LVALUE_CONV
, from
, conv
);
3031 if ((tcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (to
))
3032 && expr
&& null_ptr_cst_p (expr
))
3034 conv
= build_conv (STD_CONV
, to
, conv
);
3036 else if (tcode
== POINTER_TYPE
&& fcode
== POINTER_TYPE
)
3038 enum tree_code ufcode
= TREE_CODE (TREE_TYPE (from
));
3039 enum tree_code utcode
= TREE_CODE (TREE_TYPE (to
));
3041 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (from
)),
3042 TYPE_MAIN_VARIANT (TREE_TYPE (to
)), 1))
3044 else if (utcode
== VOID_TYPE
&& ufcode
!= OFFSET_TYPE
3045 && ufcode
!= FUNCTION_TYPE
)
3047 from
= build_pointer_type
3048 (cp_build_type_variant (void_type_node
,
3049 TYPE_READONLY (TREE_TYPE (from
)),
3050 TYPE_VOLATILE (TREE_TYPE (from
))));
3051 conv
= build_conv (PTR_CONV
, from
, conv
);
3053 else if (ufcode
== OFFSET_TYPE
&& utcode
== OFFSET_TYPE
)
3055 tree fbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from
));
3056 tree tbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to
));
3058 if (DERIVED_FROM_P (fbase
, tbase
)
3059 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from
))),
3060 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to
))),
3063 from
= build_offset_type (tbase
, TREE_TYPE (TREE_TYPE (from
)));
3064 from
= build_pointer_type (from
);
3065 conv
= build_conv (PMEM_CONV
, from
, conv
);
3070 else if (IS_AGGR_TYPE (TREE_TYPE (from
))
3071 && IS_AGGR_TYPE (TREE_TYPE (to
)))
3073 if (DERIVED_FROM_P (TREE_TYPE (to
), TREE_TYPE (from
)))
3075 from
= cp_build_type_variant (TREE_TYPE (to
),
3076 TYPE_READONLY (TREE_TYPE (from
)),
3077 TYPE_VOLATILE (TREE_TYPE (from
)));
3078 from
= build_pointer_type (from
);
3079 conv
= build_conv (PTR_CONV
, from
, conv
);
3087 if (! comptypes (from
, to
, 1))
3089 if (! comp_ptr_ttypes (TREE_TYPE (to
), TREE_TYPE (from
)))
3093 conv
= build_conv (QUAL_CONV
, from
, conv
);
3096 else if (TYPE_PTRMEMFUNC_P (to
) && TYPE_PTRMEMFUNC_P (from
))
3098 tree fromfn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from
));
3099 tree tofn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to
));
3100 tree fbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn
)));
3101 tree tbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn
)));
3103 if (! DERIVED_FROM_P (fbase
, tbase
)
3104 || ! comptypes (TREE_TYPE (fromfn
), TREE_TYPE (tofn
), 1)
3105 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)),
3106 TREE_CHAIN (TYPE_ARG_TYPES (tofn
)), 1)
3107 || TYPE_READONLY (fbase
) != TYPE_READONLY (tbase
)
3108 || TYPE_VOLATILE (fbase
) != TYPE_VOLATILE (tbase
))
3111 from
= cp_build_type_variant (tbase
, TYPE_READONLY (fbase
),
3112 TYPE_VOLATILE (fbase
));
3113 from
= build_cplus_method_type (from
, TREE_TYPE (fromfn
),
3114 TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)));
3115 from
= build_ptrmemfunc_type (build_pointer_type (from
));
3116 conv
= build_conv (PMEM_CONV
, from
, conv
);
3118 else if (tcode
== BOOLEAN_TYPE
)
3120 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
3121 || fcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (from
)))
3124 conv
= build_conv (STD_CONV
, to
, conv
);
3125 if (fcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (from
)
3126 && ICS_STD_RANK (conv
) < PBOOL_RANK
)
3127 ICS_STD_RANK (conv
) = PBOOL_RANK
;
3129 /* We don't check for ENUMERAL_TYPE here because there are no standard
3130 conversions to enum type. */
3131 else if (tcode
== INTEGER_TYPE
|| tcode
== BOOLEAN_TYPE
3132 || tcode
== REAL_TYPE
)
3134 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
))
3136 conv
= build_conv (STD_CONV
, to
, conv
);
3138 /* Give this a better rank if it's a promotion. */
3139 if (to
== type_promotes_to (from
)
3140 && ICS_STD_RANK (TREE_OPERAND (conv
, 0)) <= PROMO_RANK
)
3141 ICS_STD_RANK (conv
) = PROMO_RANK
;
3143 else if (IS_AGGR_TYPE (to
) && IS_AGGR_TYPE (from
)
3144 && DERIVED_FROM_P (to
, from
))
3145 conv
= build_conv (BASE_CONV
, to
, conv
);
3156 if (TREE_CODE (t
) == ARRAY_TYPE
)
3158 return TYPE_MAIN_VARIANT (t
);
3161 /* Returns the conversion path from type FROM to reference type TO for
3162 purposes of reference binding. For lvalue binding, either pass a
3163 reference type to FROM or an lvalue expression to EXPR.
3165 Currently does not distinguish in the generated trees between binding to
3166 an lvalue and a temporary. Should it? */
3169 reference_binding (rto
, from
, expr
, flags
)
3170 tree rto
, from
, expr
;
3175 tree to
= TREE_TYPE (rto
);
3177 if (TREE_CODE (from
) == REFERENCE_TYPE
)
3178 from
= TREE_TYPE (from
);
3179 else if (! expr
|| ! real_lvalue_p (expr
))
3183 && TYPE_READONLY (to
) >= TYPE_READONLY (from
)
3184 && TYPE_VOLATILE (to
) >= TYPE_VOLATILE (from
))
3186 conv
= build1 (IDENTITY_CONV
, from
, expr
);
3188 if (TYPE_MAIN_VARIANT (to
) == TYPE_MAIN_VARIANT (from
))
3189 conv
= build_conv (REF_BIND
, rto
, conv
);
3190 else if (IS_AGGR_TYPE (to
) && IS_AGGR_TYPE (from
)
3191 && DERIVED_FROM_P (to
, from
))
3193 conv
= build_conv (REF_BIND
, rto
, conv
);
3194 ICS_STD_RANK (conv
) = STD_RANK
;
3202 if (! conv
&& TYPE_READONLY (to
) && ! TYPE_VOLATILE (to
)
3203 && (flags
& LOOKUP_NO_TEMP_BIND
) == 0)
3205 conv
= standard_conversion
3206 (TYPE_MAIN_VARIANT (to
), strip_top_quals (from
), expr
);
3209 conv
= build_conv (REF_BIND
, rto
, conv
);
3211 /* Bind directly to a base subobject of a class rvalue. */
3212 if (TREE_CODE (TREE_OPERAND (conv
, 0)) == BASE_CONV
)
3213 TREE_OPERAND (conv
, 0) = TREE_OPERAND (TREE_OPERAND (conv
, 0), 0);
3220 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
3221 to type TO. The optional expression EXPR may affect the conversion.
3222 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
3226 implicit_conversion (to
, from
, expr
, flags
)
3227 tree to
, from
, expr
;
3231 struct z_candidate
*cand
;
3233 if (expr
&& type_unknown_p (expr
))
3235 expr
= instantiate_type (to
, expr
, 0);
3236 if (expr
== error_mark_node
)
3238 from
= TREE_TYPE (expr
);
3241 if (TREE_CODE (to
) == REFERENCE_TYPE
)
3242 conv
= reference_binding (to
, from
, expr
, flags
);
3244 conv
= standard_conversion
3245 (TYPE_MAIN_VARIANT (non_reference (to
)),
3246 strip_top_quals (non_reference (from
)), expr
);
3250 if (TREE_CODE (conv
) == IDENTITY_CONV
&& IS_AGGR_TYPE (to
)
3251 && (TREE_CODE (from
) == REFERENCE_TYPE
|| (expr
&& real_lvalue_p (expr
))))
3252 conv
= build_conv (RVALUE_CONV
, to
, conv
);
3254 else if ((IS_AGGR_TYPE (non_reference (from
))
3255 || IS_AGGR_TYPE (non_reference (to
)))
3256 && (flags
& LOOKUP_NO_CONVERSION
) == 0)
3258 if (TREE_CODE (to
) == REFERENCE_TYPE
3259 && TYPE_READONLY (TREE_TYPE (to
))
3260 && ! TYPE_VOLATILE (TREE_TYPE (to
))
3261 && (flags
& LOOKUP_NO_TEMP_BIND
) == 0)
3263 cand
= build_user_type_conversion_1
3264 (TYPE_MAIN_VARIANT (TREE_TYPE (to
)), expr
, LOOKUP_ONLYCONVERTING
);
3266 conv
= build_conv (REF_BIND
, to
, cand
->second_conv
);
3270 cand
= build_user_type_conversion_1
3271 (to
, expr
, LOOKUP_ONLYCONVERTING
);
3273 conv
= cand
->second_conv
;
3280 /* Create an overload candidate for the function or method FN called with
3281 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
3282 to implicit_conversion. */
3284 static struct z_candidate
*
3285 add_function_candidate (candidates
, fn
, arglist
, flags
)
3286 struct z_candidate
*candidates
;
3290 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3293 tree parmnode
= parmlist
;
3294 tree argnode
= arglist
;
3296 struct z_candidate
*cand
;
3298 /* The `this' and `in_chrg' arguments to constructors are not considered
3299 in overload resolution. */
3300 if (DECL_CONSTRUCTOR_P (fn
))
3302 parmnode
= TREE_CHAIN (parmnode
);
3303 argnode
= TREE_CHAIN (argnode
);
3304 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
3306 parmnode
= TREE_CHAIN (parmnode
);
3307 argnode
= TREE_CHAIN (argnode
);
3311 len
= list_length (argnode
);
3312 convs
= make_tree_vec (len
);
3314 for (i
= 0; i
< len
; ++i
)
3316 tree arg
= TREE_VALUE (argnode
);
3317 tree argtype
= TREE_TYPE (arg
);
3320 argtype
= cp_build_type_variant
3321 (argtype
, TREE_READONLY (arg
), TREE_THIS_VOLATILE (arg
));
3323 if (parmnode
== void_list_node
)
3326 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
, flags
);
3329 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
3330 ICS_ELLIPSIS_FLAG (t
) = 1;
3333 TREE_VEC_ELT (convs
, i
) = t
;
3338 parmnode
= TREE_CHAIN (parmnode
);
3339 argnode
= TREE_CHAIN (argnode
);
3345 /* Make sure there are default args for the rest of the parms. */
3346 for (; parmnode
&& parmnode
!= void_list_node
;
3347 parmnode
= TREE_CHAIN (parmnode
))
3348 if (! TREE_PURPOSE (parmnode
))
3354 cand
= (struct z_candidate
*) oballoc (sizeof (struct z_candidate
));
3357 cand
->convs
= convs
;
3358 cand
->second_conv
= NULL_TREE
;
3359 cand
->viable
= viable
;
3360 cand
->basetype_path
= NULL_TREE
;
3361 cand
->template = NULL_TREE
;
3362 cand
->next
= candidates
;
3367 /* Create an overload candidate for the conversion function FN which will
3368 be invoked for expression OBJ, producing a pointer-to-function which
3369 will in turn be called with the argument list ARGLIST, and add it to
3370 CANDIDATES. FLAGS is passed on to implicit_conversion. */
3372 static struct z_candidate
*
3373 add_conv_candidate (candidates
, fn
, obj
, arglist
)
3374 struct z_candidate
*candidates
;
3375 tree fn
, obj
, arglist
;
3377 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
3378 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (totype
));
3379 int i
, len
= list_length (arglist
) + 1;
3380 tree convs
= make_tree_vec (len
);
3381 tree parmnode
= parmlist
;
3382 tree argnode
= arglist
;
3384 struct z_candidate
*cand
;
3385 int flags
= LOOKUP_NORMAL
;
3387 for (i
= 0; i
< len
; ++i
)
3389 tree arg
= i
== 0 ? obj
: TREE_VALUE (argnode
);
3390 tree argtype
= TREE_TYPE (arg
);
3393 argtype
= cp_build_type_variant
3394 (argtype
, TREE_READONLY (arg
), TREE_THIS_VOLATILE (arg
));
3397 t
= implicit_conversion (totype
, argtype
, arg
, flags
);
3398 else if (parmnode
== void_list_node
)
3401 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
, flags
);
3404 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
3405 ICS_ELLIPSIS_FLAG (t
) = 1;
3408 TREE_VEC_ELT (convs
, i
) = t
;
3416 parmnode
= TREE_CHAIN (parmnode
);
3417 argnode
= TREE_CHAIN (argnode
);
3423 for (; parmnode
&& parmnode
!= void_list_node
;
3424 parmnode
= TREE_CHAIN (parmnode
))
3425 if (! TREE_PURPOSE (parmnode
))
3431 cand
= (struct z_candidate
*) oballoc (sizeof (struct z_candidate
));
3434 cand
->convs
= convs
;
3435 cand
->second_conv
= NULL_TREE
;
3436 cand
->viable
= viable
;
3437 cand
->basetype_path
= NULL_TREE
;
3438 cand
->template = NULL_TREE
;
3439 cand
->next
= candidates
;
3448 return (TREE_CODE (t
) == POINTER_TYPE
3449 && TREE_CODE (TREE_TYPE (t
)) != OFFSET_TYPE
3450 && TREE_CODE (TREE_TYPE (t
)) != FUNCTION_TYPE
3451 && TREE_CODE (TREE_TYPE (t
)) != VOID_TYPE
3452 && TYPE_SIZE (complete_type (TREE_TYPE (t
))) != NULL_TREE
);
3455 #define TYPE_PTRMEM_P(NODE) \
3456 (TREE_CODE (NODE) == POINTER_TYPE \
3457 && TREE_CODE (TREE_TYPE (NODE)) == OFFSET_TYPE)
3458 #define TYPE_PTR_P(NODE) \
3459 (TREE_CODE (NODE) == POINTER_TYPE \
3460 && TREE_CODE (TREE_TYPE (NODE)) != OFFSET_TYPE)
3461 #define TYPE_PTROB_P(NODE) \
3462 (TYPE_PTR_P (NODE) && TREE_CODE (TREE_TYPE (NODE)) != FUNCTION_TYPE \
3463 && TREE_CODE (TREE_TYPE (NODE)) != VOID_TYPE)
3465 static struct z_candidate
*
3466 build_builtin_candidate (candidates
, fnname
, type1
, type2
,
3467 args
, argtypes
, flags
)
3468 struct z_candidate
*candidates
;
3469 tree fnname
, type1
, type2
, *args
, *argtypes
;
3475 struct z_candidate
*cand
;
3481 convs
= make_tree_vec (args
[2] ? 3 : (args
[1] ? 2 : 1));
3483 for (i
= 0; i
< 2; ++i
)
3488 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
], flags
);
3492 /* We need something for printing the candidate. */
3493 t
= build1 (IDENTITY_CONV
, types
[i
], NULL_TREE
);
3495 TREE_VEC_ELT (convs
, i
) = t
;
3498 /* For COND_EXPR we rearranged the arguments; undo that now. */
3501 TREE_VEC_ELT (convs
, 2) = TREE_VEC_ELT (convs
, 1);
3502 TREE_VEC_ELT (convs
, 1) = TREE_VEC_ELT (convs
, 0);
3503 t
= implicit_conversion (boolean_type_node
, argtypes
[2], args
[2], flags
);
3505 TREE_VEC_ELT (convs
, 0) = t
;
3510 cand
= (struct z_candidate
*) oballoc (sizeof (struct z_candidate
));
3513 cand
->convs
= convs
;
3514 cand
->second_conv
= NULL_TREE
;
3515 cand
->viable
= viable
;
3516 cand
->basetype_path
= NULL_TREE
;
3517 cand
->template = NULL_TREE
;
3518 cand
->next
= candidates
;
3527 return TYPE_SIZE (complete_type (t
)) != NULL_TREE
;
3530 /* Create any builtin operator overload candidates for the operator in
3531 question given the converted operand types TYPE1 and TYPE2. The other
3532 args are passed through from add_builtin_candidates to
3533 build_builtin_candidate. */
3535 static struct z_candidate
*
3536 add_builtin_candidate (candidates
, code
, code2
, fnname
, type1
, type2
,
3537 args
, argtypes
, flags
)
3538 struct z_candidate
*candidates
;
3539 enum tree_code code
, code2
;
3540 tree fnname
, type1
, type2
, *args
, *argtypes
;
3545 case POSTINCREMENT_EXPR
:
3546 case POSTDECREMENT_EXPR
:
3547 args
[1] = integer_zero_node
;
3548 type2
= integer_type_node
;
3554 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3555 and VQ is either volatile or empty, there exist candidate operator
3556 functions of the form
3557 VQ T& operator++(VQ T&);
3558 T operator++(VQ T&, int);
3559 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
3560 type other than bool, and VQ is either volatile or empty, there exist
3561 candidate operator functions of the form
3562 VQ T& operator--(VQ T&);
3563 T operator--(VQ T&, int);
3564 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
3565 complete object type, and VQ is either volatile or empty, there exist
3566 candidate operator functions of the form
3567 T*VQ& operator++(T*VQ&);
3568 T*VQ& operator--(T*VQ&);
3569 T* operator++(T*VQ&, int);
3570 T* operator--(T*VQ&, int); */
3572 case POSTDECREMENT_EXPR
:
3573 case PREDECREMENT_EXPR
:
3574 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
3576 case POSTINCREMENT_EXPR
:
3577 case PREINCREMENT_EXPR
:
3578 if ((ARITHMETIC_TYPE_P (type1
) && TREE_CODE (type1
) != ENUMERAL_TYPE
)
3579 || ptr_complete_ob (type1
))
3581 type1
= build_reference_type (type1
);
3586 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
3587 exist candidate operator functions of the form
3591 8 For every function type T, there exist candidate operator functions of
3593 T& operator*(T*); */
3596 if (TREE_CODE (type1
) == POINTER_TYPE
3597 && (ptr_complete_ob (type1
)
3598 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
3602 /* 9 For every type T, there exist candidate operator functions of the form
3605 10For every promoted arithmetic type T, there exist candidate operator
3606 functions of the form
3610 case CONVERT_EXPR
: /* unary + */
3611 if (TREE_CODE (type1
) == POINTER_TYPE
3612 && TREE_CODE (TREE_TYPE (type1
)) != OFFSET_TYPE
)
3615 if (ARITHMETIC_TYPE_P (type1
))
3619 /* 11For every promoted integral type T, there exist candidate operator
3620 functions of the form
3624 if (INTEGRAL_TYPE_P (type1
))
3628 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
3629 is the same type as C2 or is a derived class of C2, T is a complete
3630 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
3631 there exist candidate operator functions of the form
3632 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
3633 where CV12 is the union of CV1 and CV2. */
3636 if (TREE_CODE (type1
) == POINTER_TYPE
3637 && (TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
)))
3639 tree c1
= TREE_TYPE (type1
);
3640 tree c2
= (TYPE_PTRMEMFUNC_P (type2
)
3641 ? TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2
))
3642 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2
)));
3644 if (IS_AGGR_TYPE (c1
) && DERIVED_FROM_P (c2
, c1
)
3645 && (TYPE_PTRMEMFUNC_P (type2
)
3646 || is_complete (TREE_TYPE (TREE_TYPE (type2
)))))
3651 /* 13For every pair of promoted arithmetic types L and R, there exist can-
3652 didate operator functions of the form
3657 bool operator<(L, R);
3658 bool operator>(L, R);
3659 bool operator<=(L, R);
3660 bool operator>=(L, R);
3661 bool operator==(L, R);
3662 bool operator!=(L, R);
3663 where LR is the result of the usual arithmetic conversions between
3666 14For every pair of types T and I, where T is a cv-qualified or cv-
3667 unqualified complete object type and I is a promoted integral type,
3668 there exist candidate operator functions of the form
3669 T* operator+(T*, I);
3670 T& operator[](T*, I);
3671 T* operator-(T*, I);
3672 T* operator+(I, T*);
3673 T& operator[](I, T*);
3675 15For every T, where T is a pointer to complete object type, there exist
3676 candidate operator functions of the form112)
3677 ptrdiff_t operator-(T, T);
3679 16For every pointer type T, there exist candidate operator functions of
3681 bool operator<(T, T);
3682 bool operator>(T, T);
3683 bool operator<=(T, T);
3684 bool operator>=(T, T);
3685 bool operator==(T, T);
3686 bool operator!=(T, T);
3688 17For every pointer to member type T, there exist candidate operator
3689 functions of the form
3690 bool operator==(T, T);
3691 bool operator!=(T, T); */
3694 if (ptr_complete_ob (type1
) && ptr_complete_ob (type2
))
3696 if (ptr_complete_ob (type1
) && INTEGRAL_TYPE_P (type2
))
3698 type2
= ptrdiff_type_node
;
3702 case TRUNC_DIV_EXPR
:
3703 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
3709 if (TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
)
3710 || TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
))
3712 if ((TYPE_PTRMEMFUNC_P (type1
) || TYPE_PTRMEM_P (type1
))
3713 && null_ptr_cst_p (args
[1]))
3718 if ((TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
))
3719 && null_ptr_cst_p (args
[0]))
3730 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
)
3731 || TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
3733 if (TYPE_PTR_P (type1
) && null_ptr_cst_p (args
[1]))
3738 if (null_ptr_cst_p (args
[0]) && TYPE_PTR_P (type2
))
3746 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
3749 if (INTEGRAL_TYPE_P (type1
) && ptr_complete_ob (type2
))
3751 type1
= ptrdiff_type_node
;
3754 if (ptr_complete_ob (type1
) && INTEGRAL_TYPE_P (type2
))
3756 type2
= ptrdiff_type_node
;
3761 /* 18For every pair of promoted integral types L and R, there exist candi-
3762 date operator functions of the form
3769 where LR is the result of the usual arithmetic conversions between
3772 case TRUNC_MOD_EXPR
:
3778 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
3782 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3783 type, VQ is either volatile or empty, and R is a promoted arithmetic
3784 type, there exist candidate operator functions of the form
3785 VQ L& operator=(VQ L&, R);
3786 VQ L& operator*=(VQ L&, R);
3787 VQ L& operator/=(VQ L&, R);
3788 VQ L& operator+=(VQ L&, R);
3789 VQ L& operator-=(VQ L&, R);
3791 20For every pair T, VQ), where T is any type and VQ is either volatile
3792 or empty, there exist candidate operator functions of the form
3793 T*VQ& operator=(T*VQ&, T*);
3795 21For every pair T, VQ), where T is a pointer to member type and VQ is
3796 either volatile or empty, there exist candidate operator functions of
3798 VQ T& operator=(VQ T&, T);
3800 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3801 unqualified complete object type, VQ is either volatile or empty, and
3802 I is a promoted integral type, there exist candidate operator func-
3804 T*VQ& operator+=(T*VQ&, I);
3805 T*VQ& operator-=(T*VQ&, I);
3807 23For every triple L, VQ, R), where L is an integral or enumeration
3808 type, VQ is either volatile or empty, and R is a promoted integral
3809 type, there exist candidate operator functions of the form
3811 VQ L& operator%=(VQ L&, R);
3812 VQ L& operator<<=(VQ L&, R);
3813 VQ L& operator>>=(VQ L&, R);
3814 VQ L& operator&=(VQ L&, R);
3815 VQ L& operator^=(VQ L&, R);
3816 VQ L& operator|=(VQ L&, R); */
3823 if (ptr_complete_ob (type1
) && INTEGRAL_TYPE_P (type2
))
3825 type2
= ptrdiff_type_node
;
3829 case TRUNC_DIV_EXPR
:
3830 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
3834 case TRUNC_MOD_EXPR
:
3840 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
3845 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
3847 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
3848 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
3849 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
))
3850 || ((TYPE_PTRMEMFUNC_P (type1
)
3851 || TREE_CODE (type1
) == POINTER_TYPE
)
3852 && null_ptr_cst_p (args
[1])))
3860 my_friendly_abort (367);
3862 type1
= build_reference_type (type1
);
3866 /* Kludge around broken overloading rules whereby
3867 bool ? const char& : enum is ambiguous. */
3868 flags
|= LOOKUP_NO_TEMP_BIND
;
3869 if (TREE_CODE (type1
) == ENUMERAL_TYPE
&& type1
== type2
)
3871 else if (TREE_CODE (type1
) == ENUMERAL_TYPE
3872 || TREE_CODE (type2
) == ENUMERAL_TYPE
)
3874 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
3876 if (TREE_CODE (type1
) == TREE_CODE (type2
)
3877 && (TREE_CODE (type1
) == REFERENCE_TYPE
3878 || TREE_CODE (type1
) == POINTER_TYPE
3879 || TYPE_PTRMEMFUNC_P (type1
)
3880 || IS_AGGR_TYPE (type1
)))
3882 if (TREE_CODE (type1
) == REFERENCE_TYPE
3883 || TREE_CODE (type2
) == REFERENCE_TYPE
)
3885 if (((TYPE_PTRMEMFUNC_P (type1
) || TREE_CODE (type1
) == POINTER_TYPE
)
3886 && null_ptr_cst_p (args
[1]))
3887 || IS_AGGR_TYPE (type1
))
3892 if (((TYPE_PTRMEMFUNC_P (type2
) || TREE_CODE (type2
) == POINTER_TYPE
)
3893 && null_ptr_cst_p (args
[0]))
3894 || IS_AGGR_TYPE (type2
))
3902 my_friendly_abort (367);
3905 /* If we're dealing with two pointer types, we need candidates
3906 for both of them. */
3907 if (type2
&& type1
!= type2
3908 && TREE_CODE (type1
) == TREE_CODE (type2
)
3909 && (TREE_CODE (type1
) == REFERENCE_TYPE
3910 || (TREE_CODE (type1
) == POINTER_TYPE
3911 && TYPE_PTRMEM_P (type1
) == TYPE_PTRMEM_P (type2
))
3912 || TYPE_PTRMEMFUNC_P (type1
)
3913 || IS_AGGR_TYPE (type1
)))
3915 candidates
= build_builtin_candidate
3916 (candidates
, fnname
, type1
, type1
, args
, argtypes
, flags
);
3917 return build_builtin_candidate
3918 (candidates
, fnname
, type2
, type2
, args
, argtypes
, flags
);
3921 return build_builtin_candidate
3922 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
);
3926 type_decays_to (type
)
3929 if (TREE_CODE (type
) == ARRAY_TYPE
)
3930 return build_pointer_type (TREE_TYPE (type
));
3931 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3932 return build_pointer_type (type
);
3936 /* There are three conditions of builtin candidates:
3938 1) bool-taking candidates. These are the same regardless of the input.
3939 2) pointer-pair taking candidates. These are generated for each type
3940 one of the input types converts to.
3941 3) arithmetic candidates. According to the WP, we should generate
3942 all of these, but I'm trying not to... */
3944 static struct z_candidate
*
3945 add_builtin_candidates (candidates
, code
, code2
, fnname
, args
, flags
)
3946 struct z_candidate
*candidates
;
3947 enum tree_code code
, code2
;
3952 tree type
, argtypes
[3], types
[2];
3954 for (i
= 0; i
< 3; ++i
)
3957 argtypes
[i
] = cp_build_type_variant
3958 (TREE_TYPE (args
[i
]), TREE_READONLY (args
[i
]),
3959 TREE_THIS_VOLATILE (args
[i
]));
3961 argtypes
[i
] = NULL_TREE
;
3966 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3967 and VQ is either volatile or empty, there exist candidate operator
3968 functions of the form
3969 VQ T& operator++(VQ T&); */
3971 case POSTINCREMENT_EXPR
:
3972 case PREINCREMENT_EXPR
:
3973 case POSTDECREMENT_EXPR
:
3974 case PREDECREMENT_EXPR
:
3979 /* 24There also exist candidate operator functions of the form
3980 bool operator!(bool);
3981 bool operator&&(bool, bool);
3982 bool operator||(bool, bool); */
3984 case TRUTH_NOT_EXPR
:
3985 return build_builtin_candidate
3986 (candidates
, fnname
, boolean_type_node
,
3987 NULL_TREE
, args
, argtypes
, flags
);
3989 case TRUTH_ORIF_EXPR
:
3990 case TRUTH_ANDIF_EXPR
:
3991 return build_builtin_candidate
3992 (candidates
, fnname
, boolean_type_node
,
3993 boolean_type_node
, args
, argtypes
, flags
);
4004 types
[0] = types
[1] = NULL_TREE
;
4006 for (i
= 0; i
< 2; ++i
)
4010 else if (IS_AGGR_TYPE (argtypes
[i
]))
4012 tree convs
= lookup_conversions (argtypes
[i
]);
4014 if (code
== COND_EXPR
)
4016 if (real_lvalue_p (args
[i
]))
4017 types
[i
] = tree_cons
4018 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
4020 types
[i
] = tree_cons
4021 (NULL_TREE
, TYPE_MAIN_VARIANT (argtypes
[i
]), types
[i
]);
4024 else if (! convs
|| (i
== 0 && code
== MODIFY_EXPR
4025 && code2
== NOP_EXPR
))
4028 for (; convs
; convs
= TREE_CHAIN (convs
))
4030 type
= TREE_TYPE (TREE_TYPE (TREE_VALUE (convs
)));
4033 && (TREE_CODE (type
) != REFERENCE_TYPE
4034 || TYPE_READONLY (TREE_TYPE (type
))))
4037 if (code
== COND_EXPR
&& TREE_CODE (type
) == REFERENCE_TYPE
)
4038 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
4040 type
= non_reference (type
);
4041 if (i
!= 0 || ! ref1
)
4043 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
4044 if (code
== COND_EXPR
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
4045 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
4046 if (INTEGRAL_TYPE_P (type
))
4047 type
= type_promotes_to (type
);
4050 if (! value_member (type
, types
[i
]))
4051 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
4056 if (code
== COND_EXPR
&& real_lvalue_p (args
[i
]))
4057 types
[i
] = tree_cons
4058 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
4059 type
= non_reference (argtypes
[i
]);
4060 if (i
!= 0 || ! ref1
)
4062 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
4063 if (code
== COND_EXPR
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
4064 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
4065 if (INTEGRAL_TYPE_P (type
))
4066 type
= type_promotes_to (type
);
4068 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
4072 for (; types
[0]; types
[0] = TREE_CHAIN (types
[0]))
4075 for (type
= types
[1]; type
; type
= TREE_CHAIN (type
))
4076 candidates
= add_builtin_candidate
4077 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
4078 TREE_VALUE (type
), args
, argtypes
, flags
);
4080 candidates
= add_builtin_candidate
4081 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
4082 NULL_TREE
, args
, argtypes
, flags
);
4088 static struct z_candidate
*
4089 add_template_candidate (candidates
, tmpl
, arglist
, flags
)
4090 struct z_candidate
*candidates
;
4094 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl
));
4095 tree
*targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
4096 struct z_candidate
*cand
;
4100 i
= type_unification (DECL_TEMPLATE_PARMS (tmpl
), targs
,
4101 TYPE_ARG_TYPES (TREE_TYPE (tmpl
)),
4102 arglist
, &dummy
, 0, 0);
4106 fn
= instantiate_template (tmpl
, targs
);
4107 if (fn
== error_mark_node
)
4110 cand
= add_function_candidate (candidates
, fn
, arglist
, flags
);
4111 cand
->template = DECL_TEMPLATE_INFO (fn
);
4117 struct z_candidate
*cands
;
4119 for (; cands
; cands
= cands
->next
)
4125 static struct z_candidate
*
4126 splice_viable (cands
)
4127 struct z_candidate
*cands
;
4129 struct z_candidate
**p
= &cands
;
4146 /* Fix this to work on non-lvalues. */
4147 return build_unary_op (ADDR_EXPR
, obj
, 0);
4151 print_z_candidates (candidates
)
4152 struct z_candidate
*candidates
;
4154 char *str
= "candidates are:";
4155 for (; candidates
; candidates
= candidates
->next
)
4157 if (TREE_CODE (candidates
->fn
) == IDENTIFIER_NODE
)
4159 if (candidates
->fn
== ansi_opname
[COND_EXPR
])
4160 cp_error ("%s %D(%T, %T, %T) <builtin>", str
, candidates
->fn
,
4161 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
4162 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)),
4163 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 2)));
4164 else if (TREE_VEC_LENGTH (candidates
->convs
) == 2)
4165 cp_error ("%s %D(%T, %T) <builtin>", str
, candidates
->fn
,
4166 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
4167 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)));
4169 cp_error ("%s %D(%T) <builtin>", str
, candidates
->fn
,
4170 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)));
4173 cp_error_at ("%s %+D", str
, candidates
->fn
);
4178 /* Returns the best overload candidate to perform the requested
4181 static struct z_candidate
*
4182 build_user_type_conversion_1 (totype
, expr
, flags
)
4186 struct z_candidate
*candidates
, *cand
;
4187 tree fromtype
= TREE_TYPE (expr
);
4188 tree ctors
= NULL_TREE
, convs
= NULL_TREE
, *p
;
4191 if (IS_AGGR_TYPE (totype
))
4192 ctors
= lookup_fnfields (TYPE_BINFO (totype
), ctor_identifier
, 0);
4193 if (IS_AGGR_TYPE (fromtype
)
4194 && (! IS_AGGR_TYPE (totype
) || ! DERIVED_FROM_P (totype
, fromtype
)))
4195 convs
= lookup_conversions (fromtype
);
4198 flags
|= LOOKUP_NO_CONVERSION
;
4202 tree t
= build_int_2 (0, 0);
4203 TREE_TYPE (t
) = build_pointer_type (totype
);
4204 args
= build_tree_list (NULL_TREE
, expr
);
4205 if (TYPE_USES_VIRTUAL_BASECLASSES (totype
))
4206 args
= tree_cons (NULL_TREE
, integer_one_node
, args
);
4207 args
= tree_cons (NULL_TREE
, t
, args
);
4209 ctors
= TREE_VALUE (ctors
);
4211 for (; ctors
; ctors
= DECL_CHAIN (ctors
))
4213 if ((flags
& LOOKUP_ONLYCONVERTING
) && DECL_NONCONVERTING_P (ctors
))
4216 candidates
= add_function_candidate (candidates
, ctors
, args
, flags
);
4217 candidates
->second_conv
= build1 (IDENTITY_CONV
, totype
, NULL_TREE
);
4218 candidates
->basetype_path
= TYPE_BINFO (totype
);
4222 args
= build_tree_list (NULL_TREE
, build_this (expr
));
4224 for (; convs
; convs
= TREE_CHAIN (convs
))
4226 tree fn
= TREE_VALUE (convs
);
4227 tree ics
= implicit_conversion
4228 (totype
, TREE_TYPE (TREE_TYPE (fn
)), 0, LOOKUP_NO_CONVERSION
);
4230 for (; fn
; fn
= DECL_CHAIN (fn
))
4232 candidates
= add_function_candidate (candidates
, fn
, args
, flags
);
4233 candidates
->second_conv
= ics
;
4234 candidates
->basetype_path
= TREE_PURPOSE (convs
);
4238 if (! any_viable (candidates
))
4241 if (flags
& LOOKUP_COMPLAIN
)
4243 if (candidates
&& ! candidates
->next
)
4244 /* say why this one won't work or try to be loose */;
4246 cp_error ("no viable candidates");
4253 candidates
= splice_viable (candidates
);
4254 cand
= tourney (candidates
, totype
);
4258 if (flags
& LOOKUP_COMPLAIN
)
4260 cp_error ("conversion from `%T' to `%T' is ambiguous",
4262 print_z_candidates (candidates
);
4265 cand
= candidates
; /* any one will do */
4266 cand
->second_conv
= build1 (AMBIG_CONV
, totype
, expr
);
4267 ICS_USER_FLAG (cand
->second_conv
) = 1;
4272 for (p
= &(cand
->second_conv
); TREE_CODE (*p
) != IDENTITY_CONV
; )
4273 p
= &(TREE_OPERAND (*p
, 0));
4277 (DECL_CONSTRUCTOR_P (cand
->fn
)
4278 ? totype
: non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
)))),
4279 NULL_TREE
, cand
->fn
, cand
->convs
, cand
->basetype_path
);
4280 ICS_USER_FLAG (cand
->second_conv
) = 1;
4286 build_user_type_conversion (totype
, expr
, flags
)
4287 tree totype
, expr
, flags
;
4289 struct z_candidate
*cand
4290 = build_user_type_conversion_1 (totype
, expr
, flags
);
4294 if (TREE_CODE (cand
->second_conv
) == AMBIG_CONV
)
4295 return error_mark_node
;
4296 return convert_from_reference (convert_like (cand
->second_conv
, expr
));
4302 build_new_function_call (fn
, args
, obj
)
4305 struct z_candidate
*candidates
= 0, *cand
;
4307 if (obj
== NULL_TREE
&& TREE_CODE (fn
) == TREE_LIST
)
4310 tree templates
= NULL_TREE
;
4312 for (t
= args
; t
; t
= TREE_CHAIN (t
))
4313 if (TREE_VALUE (t
) == error_mark_node
)
4314 return error_mark_node
;
4316 for (t
= TREE_VALUE (fn
); t
; t
= DECL_CHAIN (t
))
4318 if (TREE_CODE (t
) == TEMPLATE_DECL
)
4320 templates
= decl_tree_cons (NULL_TREE
, t
, templates
);
4321 candidates
= add_template_candidate
4322 (candidates
, t
, args
, LOOKUP_NORMAL
);
4325 candidates
= add_function_candidate
4326 (candidates
, t
, args
, LOOKUP_NORMAL
);
4329 if (! any_viable (candidates
))
4331 if (candidates
&& ! candidates
->next
)
4332 return build_function_call (candidates
->fn
, args
);
4334 cp_error ("no matching function for call to `%D (%A)'",
4335 TREE_PURPOSE (fn
), args
);
4336 return error_mark_node
;
4338 candidates
= splice_viable (candidates
);
4339 cand
= tourney (candidates
, NULL_TREE
);
4343 cp_error ("call of overloaded `%D (%A)' is ambiguous",
4344 TREE_PURPOSE (fn
), args
);
4345 print_z_candidates (candidates
);
4346 return error_mark_node
;
4349 /* Pedantically, it is ill-formed to define a function that could
4350 also be a template instantiation, but we won't implement that
4351 until things settle down. */
4352 if (templates
&& ! cand
->template && ! DECL_INITIAL (cand
->fn
))
4353 add_maybe_template (cand
->fn
, templates
);
4355 return build_over_call (cand
->fn
, cand
->convs
, args
, LOOKUP_NORMAL
);
4358 return build_function_call (fn
, args
);
4362 build_object_call (obj
, args
)
4365 struct z_candidate
*candidates
= 0, *cand
;
4366 tree fns
, convs
, mem_args
, *p
;
4367 enum tree_code code2
= NOP_EXPR
;
4368 tree type
= TREE_TYPE (obj
);
4370 fns
= lookup_fnfields (TYPE_BINFO (type
), ansi_opname
[CALL_EXPR
], 0);
4374 tree fn
= TREE_VALUE (fns
);
4375 mem_args
= tree_cons (NULL_TREE
, build_this (obj
), args
);
4377 for (; fn
; fn
= DECL_CHAIN (fn
))
4379 candidates
= add_function_candidate
4380 (candidates
, fn
, mem_args
, LOOKUP_NORMAL
);
4381 candidates
->basetype_path
= TREE_PURPOSE (fns
);
4385 convs
= lookup_conversions (type
);
4387 for (; convs
; convs
= TREE_CHAIN (convs
))
4389 tree fn
= TREE_VALUE (convs
);
4390 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
4392 if (TREE_CODE (totype
) == POINTER_TYPE
4393 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
4394 for (; fn
; fn
= DECL_CHAIN (fn
))
4396 candidates
= add_conv_candidate (candidates
, fn
, obj
, args
);
4397 candidates
->basetype_path
= TREE_PURPOSE (convs
);
4401 if (! any_viable (candidates
))
4403 cp_error ("no match for call to `(%T) (%A)", TREE_TYPE (obj
), args
);
4404 print_z_candidates (candidates
);
4405 return error_mark_node
;
4408 candidates
= splice_viable (candidates
);
4409 cand
= tourney (candidates
, NULL_TREE
);
4413 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj
), args
);
4414 print_z_candidates (candidates
);
4415 return error_mark_node
;
4418 if (DECL_NAME (cand
->fn
) == ansi_opname
[CALL_EXPR
])
4419 return build_over_call (cand
->fn
, cand
->convs
, mem_args
, LOOKUP_NORMAL
);
4421 obj
= convert_like (TREE_VEC_ELT (cand
->convs
, 0), obj
);
4424 return build_function_call (obj
, args
);
4428 op_error (code
, code2
, arg1
, arg2
, arg3
, problem
)
4429 enum tree_code code
, code2
;
4430 tree arg1
, arg2
, arg3
;
4434 = (code
== MODIFY_EXPR
? assignop_tab
[code2
] : opname_tab
[code
]);
4439 cp_error ("%s for `%T ? %T : %T'", problem
,
4440 TREE_TYPE (arg1
), TREE_TYPE (arg2
), TREE_TYPE (arg3
));
4442 case POSTINCREMENT_EXPR
:
4443 case POSTDECREMENT_EXPR
:
4444 cp_error ("%s for `%T%s'", problem
, TREE_TYPE (arg1
), opname
);
4447 cp_error ("%s for `%T[%T]'", problem
,
4448 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
4452 cp_error ("%s for `%T %s %T'", problem
,
4453 TREE_TYPE (arg1
), opname
, TREE_TYPE (arg2
));
4455 cp_error ("%s for `%s%T'", problem
, opname
, TREE_TYPE (arg1
));
4460 build_new_op (code
, flags
, arg1
, arg2
, arg3
)
4461 enum tree_code code
;
4463 tree arg1
, arg2
, arg3
;
4465 struct z_candidate
*candidates
= 0, *cand
;
4466 tree fns
, mem_arglist
, arglist
, fnname
, *p
;
4467 enum tree_code code2
= NOP_EXPR
;
4468 tree templates
= NULL_TREE
;
4470 if (arg1
== error_mark_node
)
4471 return error_mark_node
;
4473 if (code
== MODIFY_EXPR
)
4475 code2
= TREE_CODE (arg3
);
4477 fnname
= ansi_assopname
[code2
];
4480 fnname
= ansi_opname
[code
];
4489 arglist
= tree_cons (NULL_TREE
, arg2
, arg3
);
4490 if (flags
& LOOKUP_GLOBAL
)
4491 return build_new_function_call
4492 (lookup_name_nonclass (fnname
), arglist
, NULL_TREE
);
4495 rval
= build_method_call
4496 (build_indirect_ref (build1 (NOP_EXPR
, arg1
, error_mark_node
),
4498 fnname
, arglist
, NULL_TREE
, flags
);
4499 if (rval
== error_mark_node
)
4500 /* User might declare fancy operator new, but invoke it
4501 like standard one. */
4504 TREE_TYPE (rval
) = arg1
;
4505 TREE_CALLS_NEW (rval
) = 1;
4509 case VEC_DELETE_EXPR
:
4514 if (flags
& LOOKUP_GLOBAL
)
4515 return build_new_function_call
4516 (lookup_name_nonclass (fnname
),
4517 build_tree_list (NULL_TREE
, arg1
), NULL_TREE
);
4519 arglist
= tree_cons (NULL_TREE
, arg1
, build_tree_list (NULL_TREE
, arg2
));
4521 arg1
= TREE_TYPE (arg1
);
4523 /* This handles the case where we're trying to delete
4528 if (TREE_CODE (TREE_TYPE (arg1
)) == ARRAY_TYPE
)
4530 /* Strip off the pointer and the array. */
4531 arg1
= TREE_TYPE (TREE_TYPE (arg1
));
4533 while (TREE_CODE (arg1
) == ARRAY_TYPE
)
4534 arg1
= (TREE_TYPE (arg1
));
4536 arg1
= build_pointer_type (arg1
);
4540 rval
= build_method_call
4541 (build_indirect_ref (build1 (NOP_EXPR
, arg1
,
4544 fnname
, arglist
, NULL_TREE
, flags
);
4546 /* This can happen when operator delete is protected. */
4547 my_friendly_assert (rval
!= error_mark_node
, 250);
4548 TREE_TYPE (rval
) = void_type_node
;
4554 return build_object_call (arg1
, arg2
);
4557 /* The comma operator can have void args. */
4558 if (TREE_CODE (arg1
) == OFFSET_REF
)
4559 arg1
= resolve_offset_ref (arg1
);
4560 if (arg2
&& TREE_CODE (arg2
) == OFFSET_REF
)
4561 arg2
= resolve_offset_ref (arg2
);
4562 if (arg3
&& TREE_CODE (arg3
) == OFFSET_REF
)
4563 arg3
= resolve_offset_ref (arg3
);
4565 if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1
))
4566 && (! arg2
|| ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
)))
4567 && (! arg3
|| ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3
))))
4570 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
4571 arg2
= integer_zero_node
;
4573 fns
= lookup_name_nonclass (fnname
);
4574 /* + Koenig lookup */
4577 arglist
= tree_cons (NULL_TREE
, arg1
, tree_cons
4578 (NULL_TREE
, arg2
, build_tree_list (NULL_TREE
, arg3
)));
4580 arglist
= tree_cons (NULL_TREE
, arg1
, build_tree_list (NULL_TREE
, arg2
));
4582 arglist
= build_tree_list (NULL_TREE
, arg1
);
4584 if (fns
&& TREE_CODE (fns
) == TREE_LIST
)
4585 fns
= TREE_VALUE (fns
);
4586 for (; fns
; fns
= DECL_CHAIN (fns
))
4588 if (TREE_CODE (fns
) == TEMPLATE_DECL
)
4590 templates
= decl_tree_cons (NULL_TREE
, fns
, templates
);
4591 candidates
= add_template_candidate
4592 (candidates
, fns
, arglist
, flags
);
4595 candidates
= add_function_candidate (candidates
, fns
, arglist
, flags
);
4598 if (IS_AGGR_TYPE (TREE_TYPE (arg1
)))
4599 fns
= lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1
)), fnname
, 0);
4605 tree fn
= TREE_VALUE (fns
);
4606 mem_arglist
= tree_cons (NULL_TREE
, build_this (arg1
), TREE_CHAIN (arglist
));
4607 for (; fn
; fn
= DECL_CHAIN (fn
))
4609 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4610 candidates
= add_function_candidate
4611 (candidates
, fn
, mem_arglist
, flags
);
4613 candidates
= add_function_candidate (candidates
, fn
, arglist
, flags
);
4615 candidates
->basetype_path
= TREE_PURPOSE (fns
);
4622 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
4623 to know about two args; a builtin candidate will always have a first
4624 parameter of type bool. We'll handle that in
4625 build_builtin_candidate. */
4626 if (code
== COND_EXPR
)
4636 args
[2] = NULL_TREE
;
4639 candidates
= add_builtin_candidates
4640 (candidates
, code
, code2
, fnname
, args
, flags
);
4643 if (! any_viable (candidates
))
4647 case POSTINCREMENT_EXPR
:
4648 case POSTDECREMENT_EXPR
:
4649 /* Look for an `operator++ (int)'. If they didn't have
4650 one, then we fall back to the old way of doing things. */
4651 if (flags
& LOOKUP_COMPLAIN
)
4652 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
4653 fnname
, opname_tab
[code
]);
4654 if (code
== POSTINCREMENT_EXPR
)
4655 code
= PREINCREMENT_EXPR
;
4657 code
= PREDECREMENT_EXPR
;
4658 return build_new_op (code
, flags
, arg1
, NULL_TREE
, NULL_TREE
);
4660 /* The caller will deal with these. */
4666 if (flags
& LOOKUP_COMPLAIN
)
4668 op_error (code
, code2
, arg1
, arg2
, arg3
, "no match");
4669 print_z_candidates (candidates
);
4671 return error_mark_node
;
4673 candidates
= splice_viable (candidates
);
4674 cand
= tourney (candidates
, NULL_TREE
);
4678 if (flags
& LOOKUP_COMPLAIN
)
4680 op_error (code
, code2
, arg1
, arg2
, arg3
, "ambiguous overload");
4681 print_z_candidates (candidates
);
4683 return error_mark_node
;
4686 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
4688 extern int warn_synth
;
4690 && fnname
== ansi_opname
[MODIFY_EXPR
]
4691 && DECL_ARTIFICIAL (cand
->fn
)
4693 && ! candidates
->next
->next
)
4695 cp_warning ("using synthesized `%#D' for copy assignment",
4697 cp_warning_at (" where cfront would use `%#D'",
4699 ? candidates
->next
->fn
4703 if (DECL_FUNCTION_MEMBER_P (cand
->fn
))
4704 enforce_access (cand
->basetype_path
, cand
->fn
);
4706 /* Pedantically, it is ill-formed to define a function that could
4707 also be a template instantiation, but we won't implement that
4708 until things settle down. */
4709 if (templates
&& ! cand
->template && ! DECL_INITIAL (cand
->fn
)
4710 && TREE_CODE (TREE_TYPE (cand
->fn
)) != METHOD_TYPE
)
4711 add_maybe_template (cand
->fn
, templates
);
4713 return build_over_call
4714 (cand
->fn
, cand
->convs
,
4715 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
4716 ? mem_arglist
: arglist
,
4720 arg1
= convert_from_reference
4721 (convert_like (TREE_VEC_ELT (cand
->convs
, 0), arg1
));
4723 arg2
= convert_like (TREE_VEC_ELT (cand
->convs
, 1), arg2
);
4725 arg3
= convert_like (TREE_VEC_ELT (cand
->convs
, 2), arg3
);
4731 return build_modify_expr (arg1
, code2
, arg2
);
4734 return build_indirect_ref (arg1
, "unary *");
4739 case TRUNC_DIV_EXPR
:
4750 case TRUNC_MOD_EXPR
:
4754 case TRUTH_ANDIF_EXPR
:
4755 case TRUTH_ORIF_EXPR
:
4756 return build_binary_op_nodefault (code
, arg1
, arg2
, code
);
4761 case TRUTH_NOT_EXPR
:
4762 case PREINCREMENT_EXPR
:
4763 case POSTINCREMENT_EXPR
:
4764 case PREDECREMENT_EXPR
:
4765 case POSTDECREMENT_EXPR
:
4766 return build_unary_op (code
, arg1
, candidates
!= 0);
4769 return build_array_ref (arg1
, arg2
);
4772 return build_conditional_expr (arg1
, arg2
, arg3
);
4775 return build_m_component_ref
4776 (build_indirect_ref (arg1
, NULL_PTR
), arg2
);
4778 /* The caller will deal with these. */
4785 my_friendly_abort (367);
4790 enforce_access (basetype_path
, function
)
4791 tree basetype_path
, function
;
4793 tree access
= compute_access (basetype_path
, function
);
4795 if (access
== access_private_node
)
4797 cp_error_at ("`%+#D' is %s", function
,
4798 TREE_PRIVATE (function
) ? "private"
4799 : "from private base class");
4800 error ("within this context");
4802 else if (access
== access_protected_node
)
4804 cp_error_at ("`%+#D' %s", function
,
4805 TREE_PROTECTED (function
) ? "is protected"
4806 : "has protected accessibility");
4807 error ("within this context");
4811 /* Perform the conversions in CONVS on the expression EXPR. */
4814 convert_like (convs
, expr
)
4817 switch (TREE_CODE (convs
))
4821 tree fn
= TREE_OPERAND (convs
, 1);
4823 enforce_access (TREE_OPERAND (convs
, 3), fn
);
4825 if (DECL_CONSTRUCTOR_P (fn
))
4827 tree t
= build_int_2 (0, 0);
4828 TREE_TYPE (t
) = build_pointer_type (DECL_CONTEXT (fn
));
4830 args
= build_tree_list (NULL_TREE
, expr
);
4831 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
4832 args
= tree_cons (NULL_TREE
, integer_one_node
, args
);
4833 args
= tree_cons (NULL_TREE
, t
, args
);
4836 args
= build_this (expr
);
4837 expr
= build_over_call
4838 (TREE_OPERAND (convs
, 1), TREE_OPERAND (convs
, 2),
4839 args
, LOOKUP_NORMAL
);
4841 /* If this is a constructor or a function returning an aggr type,
4842 we need to build up a TARGET_EXPR. */
4843 if (DECL_CONSTRUCTOR_P (fn
))
4844 expr
= build_cplus_new (TREE_TYPE (convs
), expr
);
4849 if (type_unknown_p (expr
))
4850 expr
= instantiate_type (TREE_TYPE (convs
), expr
, 1);
4853 /* Call build_user_type_conversion again for the error. */
4854 return build_user_type_conversion
4855 (TREE_TYPE (convs
), TREE_OPERAND (convs
, 0), LOOKUP_NORMAL
);
4858 expr
= convert_like (TREE_OPERAND (convs
, 0), expr
);
4859 if (expr
== error_mark_node
)
4860 return error_mark_node
;
4862 switch (TREE_CODE (convs
))
4866 return build_user_type_conversion
4867 (TREE_TYPE (convs
), expr
, LOOKUP_NORMAL
);
4869 return convert_to_reference
4870 (TREE_TYPE (convs
), expr
,
4871 CONV_IMPLICIT
, LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
|INDIRECT_BIND
,
4874 return decay_conversion (expr
);
4876 return cp_convert (TREE_TYPE (convs
), expr
, CONV_IMPLICIT
,
4877 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
4881 convert_default_arg (type
, arg
)
4884 arg
= break_out_target_exprs (arg
);
4886 if (TREE_CODE (arg
) == CONSTRUCTOR
)
4888 arg
= digest_init (type
, arg
, 0);
4889 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
4890 "default argument", 0, 0);
4894 /* This could get clobbered by the following call. */
4895 if (TREE_HAS_CONSTRUCTOR (arg
))
4896 arg
= copy_node (arg
);
4898 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
4899 "default argument", 0, 0);
4900 #ifdef PROMOTE_PROTOTYPES
4901 if ((TREE_CODE (type
) == INTEGER_TYPE
4902 || TREE_CODE (type
) == ENUMERAL_TYPE
)
4903 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
4904 arg
= default_conversion (arg
);
4912 build_over_call (fn
, convs
, args
, flags
)
4913 tree fn
, convs
, args
;
4916 tree converted_args
= NULL_TREE
;
4917 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4918 tree conv
, arg
, val
;
4921 if (args
&& TREE_CODE (args
) != TREE_LIST
)
4922 args
= build_tree_list (NULL_TREE
, args
);
4925 /* The implicit parameters to a constructor are not considered by overload
4926 resolution, and must be of the proper type. */
4927 if (DECL_CONSTRUCTOR_P (fn
))
4929 converted_args
= tree_cons (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
4930 arg
= TREE_CHAIN (arg
);
4931 parm
= TREE_CHAIN (parm
);
4932 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
4934 converted_args
= tree_cons
4935 (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
4936 arg
= TREE_CHAIN (arg
);
4937 parm
= TREE_CHAIN (parm
);
4940 /* Bypass access control for 'this' parameter. */
4941 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
4943 converted_args
= tree_cons
4944 (NULL_TREE
, convert_force (TREE_VALUE (parm
), TREE_VALUE (arg
), CONV_C_CAST
),
4946 parm
= TREE_CHAIN (parm
);
4947 arg
= TREE_CHAIN (arg
);
4951 for (; conv
= TREE_VEC_ELT (convs
, i
), arg
&& parm
;
4952 parm
= TREE_CHAIN (parm
), arg
= TREE_CHAIN (arg
), ++i
)
4954 tree type
= TREE_VALUE (parm
);
4955 val
= convert_like (conv
, TREE_VALUE (arg
));
4957 #ifdef PROMOTE_PROTOTYPES
4958 if ((TREE_CODE (type
) == INTEGER_TYPE
4959 || TREE_CODE (type
) == ENUMERAL_TYPE
)
4960 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
4961 val
= default_conversion (val
);
4963 converted_args
= tree_cons (NULL_TREE
, val
, converted_args
);
4966 /* Default arguments */
4967 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
))
4968 converted_args
= tree_cons
4970 convert_default_arg (TREE_VALUE (parm
), TREE_PURPOSE (parm
)),
4974 for (; arg
; arg
= TREE_CHAIN (arg
))
4976 val
= TREE_VALUE (arg
);
4978 if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
4979 && (TYPE_PRECISION (TREE_TYPE (val
))
4980 < TYPE_PRECISION (double_type_node
)))
4981 /* Convert `float' to `double'. */
4982 val
= convert (double_type_node
, val
);
4983 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val
))
4984 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val
)))
4985 cp_warning ("cannot pass objects of type `%T' through `...'",
4988 /* Convert `short' and `char' to full-size `int'. */
4989 val
= default_conversion (val
);
4991 converted_args
= tree_cons (NULL_TREE
, val
, converted_args
);
4994 converted_args
= nreverse (converted_args
);
4998 if (pedantic
&& DECL_THIS_INLINE (fn
) && ! DECL_ARTIFICIAL (fn
)
4999 && ! DECL_INITIAL (fn
) && ! DECL_PENDING_INLINE_INFO (fn
)
5000 && ! (DECL_TEMPLATE_INFO (fn
)
5001 && TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (fn
))))
5002 cp_warning ("inline function `%#D' called before definition", fn
);
5004 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
5006 tree t
, *p
= &TREE_VALUE (converted_args
);
5007 tree binfo
= get_binfo
5008 (DECL_CONTEXT (fn
), TREE_TYPE (TREE_TYPE (*p
)), 0);
5009 *p
= convert_pointer_to_real (binfo
, *p
);
5010 if (TREE_SIDE_EFFECTS (*p
))
5011 *p
= save_expr (*p
);
5012 t
= build_pointer_type (TREE_TYPE (fn
));
5013 fn
= build_vfn_ref (p
, build_indirect_ref (*p
, 0), DECL_VINDEX (fn
));
5016 else if (DECL_INLINE (fn
))
5017 fn
= inline_conversion (fn
);
5019 fn
= build_addr_func (fn
);
5021 fn
= build_call (fn
, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
))), converted_args
);
5022 if (TREE_TYPE (fn
) == void_type_node
)
5024 if (IS_AGGR_TYPE (TREE_TYPE (fn
)))
5025 fn
= build_cplus_new (TREE_TYPE (fn
), fn
);
5026 return convert_from_reference (require_complete_type (fn
));
5030 build_new_method_call (instance
, name
, args
, basetype_path
, flags
)
5031 tree instance
, name
, args
, basetype_path
;
5034 struct z_candidate
*candidates
= 0, *cand
;
5035 tree basetype
, mem_args
, fns
, instance_ptr
;
5038 if (instance
== NULL_TREE
)
5039 basetype
= BINFO_TYPE (basetype_path
);
5042 if (TREE_CODE (instance
) == OFFSET_REF
)
5043 instance
= resolve_offset_ref (instance
);
5044 if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
5045 instance
= convert_from_reference (instance
);
5046 basetype
= TREE_TYPE (instance
);
5048 /* XXX this should be handled before we get here. */
5049 if (! IS_AGGR_TYPE (basetype
))
5051 if ((flags
& LOOKUP_COMPLAIN
) && basetype
!= error_mark_node
)
5052 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5053 name
, instance
, basetype
);
5055 return error_mark_node
;
5059 if (basetype_path
== NULL_TREE
)
5060 basetype_path
= TYPE_BINFO (basetype
);
5064 instance_ptr
= build_this (instance
);
5066 /* XXX this should be handled before we get here. */
5067 fns
= build_field_call (basetype_path
, instance_ptr
, name
, args
);
5073 instance_ptr
= build_int_2 (0, 0);
5074 TREE_TYPE (instance_ptr
) = build_pointer_type (basetype
);
5078 (name
== ctor_identifier
? constructor_name_full (basetype
) : name
);
5080 fns
= lookup_fnfields (basetype_path
, name
, 1);
5082 if (fns
== error_mark_node
)
5083 return error_mark_node
;
5086 tree t
= TREE_VALUE (fns
);
5087 if (name
== ctor_identifier
&& TYPE_USES_VIRTUAL_BASECLASSES (basetype
)
5088 && ! (flags
& LOOKUP_HAS_IN_CHARGE
))
5090 flags
|= LOOKUP_HAS_IN_CHARGE
;
5091 args
= tree_cons (NULL_TREE
, integer_one_node
, args
);
5093 mem_args
= tree_cons (NULL_TREE
, instance_ptr
, args
);
5094 for (; t
; t
= DECL_CHAIN (t
))
5096 /* XXX copy-init should go through build_user_type_conversion. */
5097 if (name
== ctor_identifier
5098 && (flags
& LOOKUP_ONLYCONVERTING
)
5099 && DECL_NONCONVERTING_P (t
))
5101 if (TREE_CODE (TREE_TYPE (t
)) == METHOD_TYPE
)
5102 candidates
= add_function_candidate
5103 (candidates
, t
, mem_args
, flags
);
5105 candidates
= add_function_candidate (candidates
, t
, args
, flags
);
5106 candidates
->basetype_path
= TREE_PURPOSE (fns
);
5110 if (! any_viable (candidates
))
5112 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
5113 if (flags
& LOOKUP_SPECULATIVELY
)
5115 cp_error ("no matching function for call to `%T::%D (%A)%V'", basetype
,
5116 pretty_name
, args
, TREE_TYPE (TREE_TYPE (instance_ptr
)));
5117 print_z_candidates (candidates
);
5118 return error_mark_node
;
5120 candidates
= splice_viable (candidates
);
5121 cand
= tourney (candidates
, NULL_TREE
);
5125 cp_error ("call of overloaded `%D(%A)' is ambiguous");
5126 print_z_candidates (candidates
);
5127 return error_mark_node
;
5130 enforce_access (cand
->basetype_path
, cand
->fn
);
5131 if (DECL_ABSTRACT_VIRTUAL_P (cand
->fn
)
5132 && instance
== current_class_ref
5133 && DECL_CONSTRUCTOR_P (current_function_decl
)
5134 && ! (flags
& LOOKUP_NONVIRTUAL
)
5135 && value_member (cand
->fn
, get_abstract_virtuals (basetype
)))
5136 cp_error ("abstract virtual `%#D' called from constructor", cand
->fn
);
5137 if (TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
5138 && TREE_CODE (instance_ptr
) == NOP_EXPR
5139 && TREE_OPERAND (instance_ptr
, 0) == error_mark_node
)
5140 cp_error ("cannot call member function `%D' without object", cand
->fn
);
5142 if (DECL_VINDEX (cand
->fn
) && ! (flags
& LOOKUP_NONVIRTUAL
)
5143 && ((instance
== current_class_ref
&& (dtor_label
|| ctor_label
))
5144 || resolves_to_fixed_type_p (instance
, 0)))
5145 flags
|= LOOKUP_NONVIRTUAL
;
5147 return build_over_call
5148 (cand
->fn
, cand
->convs
,
5149 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
? mem_args
: args
,
5153 /* Compare two implicit conversion sequences that differ only in their
5154 qualification conversion. Subroutine of compare_ics. */
5157 compare_qual (ics1
, ics2
)
5160 tree to1
= TREE_TYPE (ics1
);
5161 tree to2
= TREE_TYPE (ics2
);
5163 to1
= TREE_TYPE (to1
);
5164 to2
= TREE_TYPE (to2
);
5166 if (TREE_CODE (to1
) == OFFSET_TYPE
)
5168 to1
= TREE_TYPE (to1
);
5169 to2
= TREE_TYPE (to2
);
5172 if (TYPE_READONLY (to1
) >= TYPE_READONLY (to2
)
5173 && TYPE_VOLATILE (to1
) > TYPE_VOLATILE (to2
))
5175 else if (TYPE_READONLY (to1
) > TYPE_READONLY (to2
)
5176 && TYPE_VOLATILE (to1
) == TYPE_VOLATILE (to2
))
5178 else if (TYPE_READONLY (to1
) <= TYPE_READONLY (to2
)
5179 && TYPE_VOLATILE (to1
) < TYPE_VOLATILE (to2
))
5181 else if (TYPE_READONLY (to1
) < TYPE_READONLY (to2
)
5182 && TYPE_VOLATILE (to1
) == TYPE_VOLATILE (to2
))
5187 /* Compare two implicit conversion sequences according to the rules set out in
5188 [over.ics.rank]. Return values:
5190 1: ics1 is better than ics2
5191 -1: ics2 is better than ics1
5192 0: ics1 and ics2 are indistinguishable */
5195 compare_ics (ics1
, ics2
)
5200 if (ICS_RANK (ics1
) > ICS_RANK (ics2
))
5202 else if (ICS_RANK (ics1
) < ICS_RANK (ics2
))
5205 /* User-defined conversion sequence U1 is a better conversion sequence
5206 than another user-defined conversion sequence U2 if they contain the
5207 same user-defined conversion operator or constructor and if the sec-
5208 ond standard conversion sequence of U1 is better than the second
5209 standard conversion sequence of U2. */
5211 if (ICS_RANK (ics1
) == USER_RANK
)
5215 for (t1
= ics1
; TREE_CODE (t1
) != USER_CONV
; t1
= TREE_OPERAND (t1
, 0))
5216 if (TREE_CODE (t1
) == AMBIG_CONV
)
5218 for (t2
= ics2
; TREE_CODE (t2
) != USER_CONV
; t2
= TREE_OPERAND (t2
, 0))
5219 if (TREE_CODE (t2
) == AMBIG_CONV
)
5222 if (USER_CONV_FN (t1
) != USER_CONV_FN (t2
))
5224 else if (ICS_STD_RANK (ics1
) > ICS_STD_RANK (ics2
))
5226 else if (ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
5229 /* else fall through */
5232 #if 0 /* Handled by ranking */
5233 /* A conversion that is not a conversion of a pointer, or pointer to
5234 member, to bool is better than another conversion that is such a
5238 if (TREE_CODE (ics1
) == QUAL_CONV
)
5239 main1
= TREE_OPERAND (ics1
, 0);
5243 if (TREE_CODE (ics2
) == QUAL_CONV
)
5244 main2
= TREE_OPERAND (ics2
, 0);
5248 if (TREE_CODE (main1
) != TREE_CODE (main2
))
5251 if (TREE_CODE (main1
) == PTR_CONV
|| TREE_CODE (main1
) == PMEM_CONV
5252 || TREE_CODE (main1
) == REF_BIND
|| TREE_CODE (main1
) == BASE_CONV
)
5254 tree to1
= TREE_TYPE (main1
);
5255 tree from1
= TREE_TYPE (TREE_OPERAND (main1
, 0));
5256 tree to2
= TREE_TYPE (main2
);
5257 tree from2
= TREE_TYPE (TREE_OPERAND (main2
, 0));
5260 /* Standard conversion sequence S1 is a better conversion sequence than
5261 standard conversion sequence S2 if...
5263 S1 and S2 differ only in their qualification conversion and they
5264 yield types identical except for cv-qualifiers and S2 adds all the
5265 qualifiers that S1 adds (and in the same places) and S2 adds yet
5266 more cv-qualifiers than S1, or the similar case with reference
5268 if (TREE_CODE (main1
) == REF_BIND
)
5270 if (TYPE_MAIN_VARIANT (TREE_TYPE (to1
))
5271 == TYPE_MAIN_VARIANT (TREE_TYPE (to2
)))
5272 return compare_qual (ics1
, ics2
);
5274 else if (TREE_CODE (main1
) != BASE_CONV
&& from1
== from2
&& to1
== to2
)
5275 return compare_qual (ics1
, ics2
);
5277 if (TYPE_PTRMEMFUNC_P (to1
))
5279 to1
= TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (to1
));
5280 from1
= TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (from1
));
5282 else if (TREE_CODE (main1
) != BASE_CONV
)
5284 to1
= TREE_TYPE (to1
);
5285 if (TREE_CODE (main1
) != REF_BIND
)
5286 from1
= TREE_TYPE (from1
);
5288 if (TREE_CODE (to1
) == OFFSET_TYPE
)
5290 to1
= TYPE_OFFSET_BASETYPE (to1
);
5291 from1
= TYPE_OFFSET_BASETYPE (from1
);
5295 if (TYPE_PTRMEMFUNC_P (to2
))
5297 to2
= TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (to2
));
5298 from2
= TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (from2
));
5300 else if (TREE_CODE (main1
) != BASE_CONV
)
5302 to2
= TREE_TYPE (to2
);
5303 if (TREE_CODE (main1
) != REF_BIND
)
5304 from2
= TREE_TYPE (from2
);
5306 if (TREE_CODE (to2
) == OFFSET_TYPE
)
5308 to2
= TYPE_OFFSET_BASETYPE (to2
);
5309 from2
= TYPE_OFFSET_BASETYPE (from2
);
5313 if (! (IS_AGGR_TYPE (from1
) && IS_AGGR_TYPE (from2
)))
5316 /* The sense of pmem conversions is reversed from that of the other
5318 if (TREE_CODE (main1
) == PMEM_CONV
)
5320 tree t
= from1
; from1
= from2
; from2
= t
;
5321 t
= to1
; to1
= to2
; to2
= t
;
5324 distf
= get_base_distance (from1
, from2
, 0, 0);
5327 distf
= -get_base_distance (from2
, from1
, 0, 0);
5332 /* If class B is derived directly or indirectly from class A,
5333 conver- sion of B* to A* is better than conversion of B* to
5334 void*, and conversion of A* to void* is better than
5335 conversion of B* to void*. */
5337 if (TREE_CODE (to1
) == VOID_TYPE
&& TREE_CODE (to2
) == VOID_TYPE
)
5344 else if (TREE_CODE (to2
) == VOID_TYPE
&& IS_AGGR_TYPE (to1
)
5345 && get_base_distance (to1
, from1
, 0, 0) != -1)
5347 else if (TREE_CODE (to1
) == VOID_TYPE
&& IS_AGGR_TYPE (to2
)
5348 && get_base_distance (to2
, from2
, 0, 0) != -1)
5351 if (! (IS_AGGR_TYPE (to1
) && IS_AGGR_TYPE (to2
)))
5354 /* If class B is derived directly or indirectly from class A and class
5355 C is derived directly or indirectly from B */
5357 distt
= get_base_distance (to1
, to2
, 0, 0);
5360 distt
= -get_base_distance (to2
, to1
, 0, 0);
5365 /* --conversion of C* to B* is better than conversion of C* to A*, */
5373 /* --conversion of B* to A* is better than conversion of C* to A*, */
5374 else if (distt
== 0)
5382 else if (TREE_CODE (TREE_TYPE (main1
)) == POINTER_TYPE
5383 || TYPE_PTRMEMFUNC_P (TREE_TYPE (main1
)))
5385 if (TREE_TYPE (main1
) == TREE_TYPE (main2
))
5386 return compare_qual (ics1
, ics2
);
5388 #if 0 /* This is now handled by making identity better than anything else. */
5389 /* existing practice, not WP-endorsed: const char * -> const char *
5390 is better than char * -> const char *. (jason 6/29/96) */
5391 if (TREE_TYPE (ics1
) == TREE_TYPE (ics2
))
5392 return -compare_qual (main1
, main2
);
5399 /* Compare two candidates for overloading as described in
5400 [over.match.best]. Return values:
5402 1: cand1 is better than cand2
5403 -1: cand2 is better than cand1
5404 0: cand1 and cand2 are indistinguishable */
5407 joust (cand1
, cand2
)
5408 struct z_candidate
*cand1
, *cand2
;
5411 int i
, off1
= 0, off2
= 0, len
;
5413 /* a viable function F1
5414 is defined to be a better function than another viable function F2 if
5415 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5416 ICSi(F2), and then */
5418 /* for some argument j, ICSj(F1) is a better conversion sequence than
5421 /* For comparing static and non-static member functions, we ignore the
5422 implicit object parameter of the non-static function. The WP says to
5423 pretend that the static function has an object parm, but that won't
5424 work with operator overloading. */
5425 len
= TREE_VEC_LENGTH (cand1
->convs
);
5426 if (len
!= TREE_VEC_LENGTH (cand2
->convs
))
5428 if (DECL_STATIC_FUNCTION_P (cand1
->fn
)
5429 && ! DECL_STATIC_FUNCTION_P (cand2
->fn
))
5431 else if (! DECL_STATIC_FUNCTION_P (cand1
->fn
)
5432 && DECL_STATIC_FUNCTION_P (cand2
->fn
))
5438 my_friendly_abort (42);
5441 for (i
= 0; i
< len
; ++i
)
5443 int comp
= compare_ics (TREE_VEC_ELT (cand1
->convs
, i
+off1
),
5444 TREE_VEC_ELT (cand2
->convs
, i
+off2
));
5448 if (winner
&& comp
!= winner
)
5458 F1 is a non-template function and F2 is a template function */
5460 if (! cand1
->template && cand2
->template)
5462 else if (cand1
->template && ! cand2
->template)
5464 else if (cand1
->template && cand2
->template)
5465 winner
= more_specialized
5466 (TI_TEMPLATE (cand1
->template), TI_TEMPLATE (cand2
->template));
5469 the context is an initialization by user-defined conversion (see
5470 _dcl.init_ and _over.match.user_) and the standard conversion
5471 sequence from the return type of F1 to the destination type (i.e.,
5472 the type of the entity being initialized) is a better conversion
5473 sequence than the standard conversion sequence from the return type
5474 of F2 to the destination type. */
5476 if (! winner
&& cand1
->second_conv
)
5477 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5479 /* If the built-in candidates are the same, arbitrarily pick one. */
5480 if (! winner
&& cand1
->fn
== cand2
->fn
5481 && TREE_CODE (cand1
->fn
) == IDENTIFIER_NODE
)
5483 for (i
= 0; i
< TREE_VEC_LENGTH (cand1
->convs
); ++i
)
5484 if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1
->convs
, i
)),
5485 TREE_TYPE (TREE_VEC_ELT (cand2
->convs
, i
)), 1))
5487 if (i
== TREE_VEC_LENGTH (cand1
->convs
))
5489 /* Kludge around broken overloading rules whereby
5490 bool ? void *const & : void *const & is ambiguous. */
5491 if (cand1
->fn
== ansi_opname
[COND_EXPR
])
5493 tree c1
= TREE_VEC_ELT (cand1
->convs
, 1);
5494 tree c2
= TREE_VEC_ELT (cand2
->convs
, 1);
5495 tree t1
= strip_top_quals (non_reference (TREE_TYPE (c1
)));
5496 tree t2
= strip_top_quals (non_reference (TREE_TYPE (c2
)));
5498 if (comptypes (t1
, t2
, 1))
5500 if (TREE_CODE (c1
) == REF_BIND
&& TREE_CODE (c2
) != REF_BIND
)
5502 if (TREE_CODE (c1
) != REF_BIND
&& TREE_CODE (c2
) == REF_BIND
)
5511 /* Given a list of candidates for overloading, find the best one, if any.
5512 This algorithm has a worst case of O(2n) (winner is last), and a best
5513 case of O(n/2) (totally ambiguous); much better than a sorting
5516 static struct z_candidate
*
5517 tourney (candidates
)
5518 struct z_candidate
*candidates
;
5520 struct z_candidate
*champ
= candidates
, *challenger
;
5523 /* Walk through the list once, comparing each current champ to the next
5524 candidate, knocking out a candidate or two with each comparison. */
5526 for (challenger
= champ
->next
; challenger
; )
5528 fate
= joust (champ
, challenger
);
5530 challenger
= challenger
->next
;
5535 champ
= challenger
->next
;
5542 challenger
= champ
->next
;
5546 /* Make sure the champ is better than all the candidates it hasn't yet
5547 been compared to. This may do one more comparison than necessary. Oh
5550 for (challenger
= candidates
; challenger
!= champ
;
5551 challenger
= challenger
->next
)
5553 fate
= joust (champ
, challenger
);