1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
5 modified by Brendan Kehoe (brendan@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
39 extern int inhibit_warnings
;
41 static tree build_new_method_call
PARAMS ((tree
, tree
, tree
, tree
, int));
43 static tree build_field_call
PARAMS ((tree
, tree
, tree
, tree
));
44 static struct z_candidate
* tourney
PARAMS ((struct z_candidate
*));
45 static int equal_functions
PARAMS ((tree
, tree
));
46 static int joust
PARAMS ((struct z_candidate
*, struct z_candidate
*, int));
47 static int compare_ics
PARAMS ((tree
, tree
));
48 static tree build_over_call
PARAMS ((struct z_candidate
*, tree
, int));
49 #define convert_like(CONV, EXPR) convert_like_real (CONV, EXPR, NULL_TREE, 0, 0)
50 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) convert_like_real (CONV, EXPR, FN, ARGNO, 0)
51 static tree convert_like_real
PARAMS ((tree
, tree
, tree
, int, int));
52 static void op_error
PARAMS ((enum tree_code
, enum tree_code
, tree
, tree
,
54 static tree build_object_call
PARAMS ((tree
, tree
));
55 static tree resolve_args
PARAMS ((tree
));
56 static struct z_candidate
* build_user_type_conversion_1
57 PARAMS ((tree
, tree
, int));
58 static void print_z_candidates
PARAMS ((struct z_candidate
*));
59 static tree build_this
PARAMS ((tree
));
60 static struct z_candidate
* splice_viable
PARAMS ((struct z_candidate
*));
61 static int any_viable
PARAMS ((struct z_candidate
*));
62 static struct z_candidate
* add_template_candidate
63 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
, tree
, int,
65 static struct z_candidate
* add_template_candidate_real
66 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
, tree
, int,
67 tree
, unification_kind_t
));
68 static struct z_candidate
* add_template_conv_candidate
69 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
));
70 static struct z_candidate
* add_builtin_candidates
71 PARAMS ((struct z_candidate
*, enum tree_code
, enum tree_code
,
73 static struct z_candidate
* add_builtin_candidate
74 PARAMS ((struct z_candidate
*, enum tree_code
, enum tree_code
,
75 tree
, tree
, tree
, tree
*, tree
*, int));
76 static int is_complete
PARAMS ((tree
));
77 static struct z_candidate
* build_builtin_candidate
78 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, tree
*, tree
*,
80 static struct z_candidate
* add_conv_candidate
81 PARAMS ((struct z_candidate
*, tree
, tree
, tree
));
82 static struct z_candidate
* add_function_candidate
83 PARAMS ((struct z_candidate
*, tree
, tree
, tree
, int));
84 static tree implicit_conversion
PARAMS ((tree
, tree
, tree
, int));
85 static tree standard_conversion
PARAMS ((tree
, tree
, tree
));
86 static tree reference_binding
PARAMS ((tree
, tree
, tree
, int));
87 static tree non_reference
PARAMS ((tree
));
88 static tree build_conv
PARAMS ((enum tree_code
, tree
, tree
));
89 static int is_subseq
PARAMS ((tree
, tree
));
90 static int maybe_handle_ref_bind
PARAMS ((tree
*, tree
*));
91 static void maybe_handle_implicit_object
PARAMS ((tree
*));
92 static struct z_candidate
* add_candidate
PARAMS ((struct z_candidate
*,
94 static tree source_type
PARAMS ((tree
));
95 static void add_warning
PARAMS ((struct z_candidate
*, struct z_candidate
*));
96 static int reference_related_p
PARAMS ((tree
, tree
));
97 static int reference_compatible_p
PARAMS ((tree
, tree
));
98 static tree convert_class_to_reference
PARAMS ((tree
, tree
, tree
));
99 static tree direct_reference_binding
PARAMS ((tree
, tree
));
100 static int promoted_arithmetic_type_p
PARAMS ((tree
));
101 static tree conditional_conversion
PARAMS ((tree
, tree
));
104 build_vfield_ref (datum
, type
)
109 if (datum
== error_mark_node
)
110 return error_mark_node
;
112 if (TREE_CODE (TREE_TYPE (datum
)) == REFERENCE_TYPE
)
113 datum
= convert_from_reference (datum
);
115 if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
))
116 rval
= build (COMPONENT_REF
, TREE_TYPE (TYPE_VFIELD (type
)),
117 datum
, TYPE_VFIELD (type
));
119 rval
= build_component_ref (datum
, DECL_NAME (TYPE_VFIELD (type
)), NULL_TREE
, 0);
124 /* Build a call to a member of an object. I.e., one that overloads
125 operator ()(), or is a pointer-to-function or pointer-to-method. */
128 build_field_call (basetype_path
, instance_ptr
, name
, parms
)
129 tree basetype_path
, instance_ptr
, name
, parms
;
131 tree field
, instance
;
133 if (IDENTIFIER_CTOR_OR_DTOR_P (name
))
136 /* Speed up the common case. */
137 if (instance_ptr
== current_class_ptr
138 && IDENTIFIER_CLASS_VALUE (name
) == NULL_TREE
)
141 field
= lookup_field (basetype_path
, name
, 1, 0);
143 if (field
== error_mark_node
|| field
== NULL_TREE
)
146 if (TREE_CODE (field
) == FIELD_DECL
|| TREE_CODE (field
) == VAR_DECL
)
148 /* If it's a field, try overloading operator (),
149 or calling if the field is a pointer-to-function. */
150 instance
= build_indirect_ref (instance_ptr
, NULL_PTR
);
151 instance
= build_component_ref_1 (instance
, field
, 0);
153 if (instance
== error_mark_node
)
154 return error_mark_node
;
156 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
157 return build_opfncall (CALL_EXPR
, LOOKUP_NORMAL
,
158 instance
, parms
, NULL_TREE
);
159 else if (TREE_CODE (TREE_TYPE (instance
)) == POINTER_TYPE
)
161 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
))) == FUNCTION_TYPE
)
162 return build_function_call (instance
, parms
);
163 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance
)))
165 return build_function_call
166 (instance
, tree_cons (NULL_TREE
, instance_ptr
, parms
));
173 /* Returns nonzero iff the destructor name specified in NAME
174 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
178 check_dtor_name (basetype
, name
)
181 name
= TREE_OPERAND (name
, 0);
183 /* Just accept something we've already complained about. */
184 if (name
== error_mark_node
)
187 if (TREE_CODE (name
) == TYPE_DECL
)
188 name
= TREE_TYPE (name
);
189 else if (TYPE_P (name
))
191 else if (TREE_CODE (name
) == IDENTIFIER_NODE
)
193 if ((IS_AGGR_TYPE (basetype
) && name
== constructor_name (basetype
))
194 || (TREE_CODE (basetype
) == ENUMERAL_TYPE
195 && name
== TYPE_IDENTIFIER (basetype
)))
198 name
= get_type_value (name
);
201 my_friendly_abort (980605);
203 if (name
&& TYPE_MAIN_VARIANT (basetype
) == TYPE_MAIN_VARIANT (name
))
208 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
209 This is how virtual function calls are avoided. */
212 build_scoped_method_call (exp
, basetype
, name
, parms
)
213 tree exp
, basetype
, name
, parms
;
215 /* Because this syntactic form does not allow
216 a pointer to a base class to be `stolen',
217 we need not protect the derived->base conversion
220 @@ But we do have to check access privileges later. */
222 tree type
= TREE_TYPE (exp
);
224 if (type
== error_mark_node
225 || basetype
== error_mark_node
)
226 return error_mark_node
;
228 if (processing_template_decl
)
230 if (TREE_CODE (name
) == BIT_NOT_EXPR
231 && TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
233 tree type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 0);
235 name
= build_min_nt (BIT_NOT_EXPR
, type
);
237 name
= build_min_nt (SCOPE_REF
, basetype
, name
);
238 return build_min_nt (METHOD_CALL_EXPR
, name
, exp
, parms
, NULL_TREE
);
241 if (TREE_CODE (type
) == REFERENCE_TYPE
)
242 type
= TREE_TYPE (type
);
244 if (TREE_CODE (basetype
) == TREE_VEC
)
247 basetype
= BINFO_TYPE (binfo
);
252 /* Check the destructor call syntax. */
253 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
255 /* We can get here if someone writes their destructor call like
256 `obj.NS::~T()'; this isn't really a scoped method call, so hand
258 if (TREE_CODE (basetype
) == NAMESPACE_DECL
)
259 return build_method_call (exp
, name
, parms
, NULL_TREE
, LOOKUP_NORMAL
);
261 if (! check_dtor_name (basetype
, name
))
262 cp_error ("qualified type `%T' does not match destructor name `~%T'",
263 basetype
, TREE_OPERAND (name
, 0));
265 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
266 that explicit ~int is caught in the parser; this deals with typedefs
267 and template parms. */
268 if (! IS_AGGR_TYPE (basetype
))
270 if (TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (basetype
))
271 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
272 exp
, basetype
, type
);
274 return cp_convert (void_type_node
, exp
);
278 if (! is_aggr_type (basetype
, 1))
279 return error_mark_node
;
281 if (! IS_AGGR_TYPE (type
))
283 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
285 return error_mark_node
;
290 binfo
= get_binfo (basetype
, type
, 1);
291 if (binfo
== error_mark_node
)
292 return error_mark_node
;
294 error_not_base_type (basetype
, type
);
299 if (TREE_CODE (exp
) == INDIRECT_REF
)
300 decl
= build_indirect_ref
301 (convert_pointer_to_real
302 (binfo
, build_unary_op (ADDR_EXPR
, exp
, 0)), NULL_PTR
);
304 decl
= build_scoped_ref (exp
, basetype
);
306 /* Call to a destructor. */
307 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
309 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
310 return cp_convert (void_type_node
, exp
);
312 return build_delete (TREE_TYPE (decl
), decl
,
313 sfk_complete_destructor
,
314 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
318 /* Call to a method. */
319 return build_method_call (decl
, name
, parms
, binfo
,
320 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
322 return error_mark_node
;
325 /* We want the address of a function or method. We avoid creating a
326 pointer-to-member function. */
329 build_addr_func (function
)
332 tree type
= TREE_TYPE (function
);
334 /* We have to do these by hand to avoid real pointer to member
336 if (TREE_CODE (type
) == METHOD_TYPE
)
340 type
= build_pointer_type (type
);
342 if (mark_addressable (function
) == 0)
343 return error_mark_node
;
345 addr
= build1 (ADDR_EXPR
, type
, function
);
347 /* Address of a static or external variable or function counts
349 if (staticp (function
))
350 TREE_CONSTANT (addr
) = 1;
355 function
= default_conversion (function
);
360 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
361 POINTER_TYPE to those. Note, pointer to member function types
362 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
365 build_call (function
, parms
)
366 tree function
, parms
;
368 int is_constructor
= 0;
374 function
= build_addr_func (function
);
376 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
378 sorry ("unable to call pointer to member function here");
379 return error_mark_node
;
382 result_type
= TREE_TYPE (TREE_TYPE (TREE_TYPE (function
)));
384 if (TREE_CODE (function
) == ADDR_EXPR
385 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
386 decl
= TREE_OPERAND (function
, 0);
390 /* We check both the decl and the type; a function may be known not to
391 throw without being declared throw(). */
392 nothrow
= ((decl
&& TREE_NOTHROW (decl
))
393 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function
))));
395 if (decl
&& DECL_CONSTRUCTOR_P (decl
))
398 if (decl
&& ! TREE_USED (decl
))
400 /* We invoke build_call directly for several library functions. */
401 if (DECL_ARTIFICIAL (decl
))
404 my_friendly_abort (990125);
407 /* Don't pass empty class objects by value. This is useful
408 for tags in STL, which are used to control overload resolution.
409 We don't need to handle other cases of copying empty classes. */
410 if (! decl
|| ! DECL_BUILT_IN (decl
))
411 for (tmp
= parms
; tmp
; tmp
= TREE_CHAIN (tmp
))
412 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp
)))
413 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp
))))
415 tree t
= build (EMPTY_CLASS_EXPR
, TREE_TYPE (TREE_VALUE (tmp
)));
416 TREE_VALUE (tmp
) = build (COMPOUND_EXPR
, TREE_TYPE (t
),
417 TREE_VALUE (tmp
), t
);
420 function
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
421 TREE_HAS_CONSTRUCTOR (function
) = is_constructor
;
422 TREE_TYPE (function
) = result_type
;
423 TREE_SIDE_EFFECTS (function
) = 1;
424 TREE_NOTHROW (function
) = nothrow
;
429 /* Build something of the form ptr->method (args)
430 or object.method (args). This can also build
431 calls to constructors, and find friends.
433 Member functions always take their class variable
436 INSTANCE is a class instance.
438 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
440 PARMS help to figure out what that NAME really refers to.
442 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
443 down to the real instance type to use for access checking. We need this
444 information to get protected accesses correct. This parameter is used
445 by build_member_call.
447 FLAGS is the logical disjunction of zero or more LOOKUP_
448 flags. See cp-tree.h for more info.
450 If this is all OK, calls build_function_call with the resolved
453 This function must also handle being called to perform
454 initialization, promotion/coercion of arguments, and
455 instantiation of default parameters.
457 Note that NAME may refer to an instance variable name. If
458 `operator()()' is defined for the type of that field, then we return
461 #ifdef GATHER_STATISTICS
462 extern int n_build_method_call
;
466 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
467 tree instance
, name
, parms
, basetype_path
;
470 tree basetype
, instance_ptr
;
472 #ifdef GATHER_STATISTICS
473 n_build_method_call
++;
476 if (instance
== error_mark_node
477 || name
== error_mark_node
478 || parms
== error_mark_node
479 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
480 return error_mark_node
;
482 if (processing_template_decl
)
484 /* We need to process template parm names here so that tsubst catches
485 them properly. Other type names can wait. */
486 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
488 tree type
= NULL_TREE
;
490 if (TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
491 type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 0);
492 else if (TREE_CODE (TREE_OPERAND (name
, 0)) == TYPE_DECL
)
493 type
= TREE_TYPE (TREE_OPERAND (name
, 0));
495 if (type
&& TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
496 name
= build_min_nt (BIT_NOT_EXPR
, type
);
499 return build_min_nt (METHOD_CALL_EXPR
, name
, instance
, parms
, NULL_TREE
);
502 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
505 error ("destructors take no parameters");
506 basetype
= TREE_TYPE (instance
);
507 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
508 basetype
= TREE_TYPE (basetype
);
510 if (! check_dtor_name (basetype
, name
))
512 ("destructor name `~%T' does not match type `%T' of expression",
513 TREE_OPERAND (name
, 0), basetype
);
515 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype
)))
516 return cp_convert (void_type_node
, instance
);
517 instance
= default_conversion (instance
);
518 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
519 return build_delete (build_pointer_type (basetype
),
520 instance_ptr
, sfk_complete_destructor
,
521 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
524 return build_new_method_call (instance
, name
, parms
, basetype_path
, flags
);
527 /* New overloading code. */
537 struct z_candidate
*next
;
540 #define IDENTITY_RANK 0
546 #define ELLIPSIS_RANK 6
549 #define ICS_RANK(NODE) \
550 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
551 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
552 : ICS_USER_FLAG (NODE) ? USER_RANK \
553 : ICS_STD_RANK (NODE))
555 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
557 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
558 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
559 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
560 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
562 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
563 should be created to hold the result of the conversion. */
564 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
566 #define USER_CONV_CAND(NODE) \
567 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
568 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
576 A null pointer constant is an integral constant expression
577 (_expr.const_) rvalue of integer type that evaluates to zero. */
579 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t
)) && integer_zerop (t
)))
585 build_conv (code
, type
, from
)
589 tree t
= build1 (code
, type
, from
);
590 int rank
= ICS_STD_RANK (from
);
602 if (rank
< EXACT_RANK
)
608 ICS_STD_RANK (t
) = rank
;
609 ICS_USER_FLAG (t
) = ICS_USER_FLAG (from
);
610 ICS_BAD_FLAG (t
) = ICS_BAD_FLAG (from
);
618 if (TREE_CODE (t
) == REFERENCE_TYPE
)
627 if (TREE_CODE (t
) == ARRAY_TYPE
)
629 return TYPE_MAIN_VARIANT (t
);
632 /* Returns the standard conversion path (see [conv]) from type FROM to type
633 TO, if any. For proper handling of null pointer constants, you must
634 also pass the expression EXPR to convert from. */
637 standard_conversion (to
, from
, expr
)
640 enum tree_code fcode
, tcode
;
644 if (TREE_CODE (to
) == REFERENCE_TYPE
)
646 if (TREE_CODE (from
) == REFERENCE_TYPE
)
649 from
= TREE_TYPE (from
);
651 to
= strip_top_quals (to
);
652 from
= strip_top_quals (from
);
654 if ((TYPE_PTRFN_P (to
) || TYPE_PTRMEMFUNC_P (to
))
655 && expr
&& type_unknown_p (expr
))
657 expr
= instantiate_type (to
, expr
, 0);
658 if (expr
== error_mark_node
)
660 from
= TREE_TYPE (expr
);
663 fcode
= TREE_CODE (from
);
664 tcode
= TREE_CODE (to
);
666 conv
= build1 (IDENTITY_CONV
, from
, expr
);
668 if (fcode
== FUNCTION_TYPE
)
670 from
= build_pointer_type (from
);
671 fcode
= TREE_CODE (from
);
672 conv
= build_conv (LVALUE_CONV
, from
, conv
);
674 else if (fcode
== ARRAY_TYPE
)
676 from
= build_pointer_type (TREE_TYPE (from
));
677 fcode
= TREE_CODE (from
);
678 conv
= build_conv (LVALUE_CONV
, from
, conv
);
680 else if (fromref
|| (expr
&& real_lvalue_p (expr
)))
681 conv
= build_conv (RVALUE_CONV
, from
, conv
);
683 /* Allow conversion between `__complex__' data types */
684 if (tcode
== COMPLEX_TYPE
&& fcode
== COMPLEX_TYPE
)
686 /* The standard conversion sequence to convert FROM to TO is
687 the standard conversion sequence to perform componentwise
689 tree part_conv
= standard_conversion
690 (TREE_TYPE (to
), TREE_TYPE (from
), NULL_TREE
);
694 conv
= build_conv (TREE_CODE (part_conv
), to
, conv
);
695 ICS_STD_RANK (conv
) = ICS_STD_RANK (part_conv
);
703 if (same_type_p (from
, to
))
706 if ((tcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (to
))
707 && expr
&& null_ptr_cst_p (expr
))
709 conv
= build_conv (STD_CONV
, to
, conv
);
711 else if (tcode
== POINTER_TYPE
&& fcode
== POINTER_TYPE
)
713 enum tree_code ufcode
= TREE_CODE (TREE_TYPE (from
));
714 enum tree_code utcode
= TREE_CODE (TREE_TYPE (to
));
716 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from
),
719 else if (utcode
== VOID_TYPE
&& ufcode
!= OFFSET_TYPE
720 && ufcode
!= FUNCTION_TYPE
)
722 from
= build_pointer_type
723 (cp_build_qualified_type (void_type_node
,
724 CP_TYPE_QUALS (TREE_TYPE (from
))));
725 conv
= build_conv (PTR_CONV
, from
, conv
);
727 else if (ufcode
== OFFSET_TYPE
&& utcode
== OFFSET_TYPE
)
729 tree fbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from
));
730 tree tbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to
));
732 if (DERIVED_FROM_P (fbase
, tbase
)
733 && (same_type_ignoring_top_level_qualifiers_p
734 (TREE_TYPE (TREE_TYPE (from
)),
735 TREE_TYPE (TREE_TYPE (to
)))))
737 from
= build_offset_type (tbase
, TREE_TYPE (TREE_TYPE (from
)));
738 from
= build_pointer_type (from
);
739 conv
= build_conv (PMEM_CONV
, from
, conv
);
742 else if (IS_AGGR_TYPE (TREE_TYPE (from
))
743 && IS_AGGR_TYPE (TREE_TYPE (to
)))
745 if (DERIVED_FROM_P (TREE_TYPE (to
), TREE_TYPE (from
)))
748 cp_build_qualified_type (TREE_TYPE (to
),
749 CP_TYPE_QUALS (TREE_TYPE (from
)));
750 from
= build_pointer_type (from
);
751 conv
= build_conv (PTR_CONV
, from
, conv
);
755 if (same_type_p (from
, to
))
757 else if (comp_ptr_ttypes (TREE_TYPE (to
), TREE_TYPE (from
)))
758 conv
= build_conv (QUAL_CONV
, to
, conv
);
759 else if (expr
&& string_conv_p (to
, expr
, 0))
760 /* converting from string constant to char *. */
761 conv
= build_conv (QUAL_CONV
, to
, conv
);
762 else if (ptr_reasonably_similar (TREE_TYPE (to
), TREE_TYPE (from
)))
764 conv
= build_conv (PTR_CONV
, to
, conv
);
765 ICS_BAD_FLAG (conv
) = 1;
772 else if (TYPE_PTRMEMFUNC_P (to
) && TYPE_PTRMEMFUNC_P (from
))
774 tree fromfn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from
));
775 tree tofn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to
));
776 tree fbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn
)));
777 tree tbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn
)));
779 if (! DERIVED_FROM_P (fbase
, tbase
)
780 || ! same_type_p (TREE_TYPE (fromfn
), TREE_TYPE (tofn
))
781 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)),
782 TREE_CHAIN (TYPE_ARG_TYPES (tofn
)))
783 || CP_TYPE_QUALS (fbase
) != CP_TYPE_QUALS (tbase
))
786 from
= cp_build_qualified_type (tbase
, CP_TYPE_QUALS (fbase
));
787 from
= build_cplus_method_type (from
, TREE_TYPE (fromfn
),
788 TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)));
789 from
= build_ptrmemfunc_type (build_pointer_type (from
));
790 conv
= build_conv (PMEM_CONV
, from
, conv
);
792 else if (tcode
== BOOLEAN_TYPE
)
794 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
795 || fcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (from
)))
798 conv
= build_conv (STD_CONV
, to
, conv
);
799 if (fcode
== POINTER_TYPE
800 || (TYPE_PTRMEMFUNC_P (from
) && ICS_STD_RANK (conv
) < PBOOL_RANK
))
801 ICS_STD_RANK (conv
) = PBOOL_RANK
;
803 /* We don't check for ENUMERAL_TYPE here because there are no standard
804 conversions to enum type. */
805 else if (tcode
== INTEGER_TYPE
|| tcode
== BOOLEAN_TYPE
806 || tcode
== REAL_TYPE
)
808 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
))
810 conv
= build_conv (STD_CONV
, to
, conv
);
812 /* Give this a better rank if it's a promotion. */
813 if (to
== type_promotes_to (from
)
814 && ICS_STD_RANK (TREE_OPERAND (conv
, 0)) <= PROMO_RANK
)
815 ICS_STD_RANK (conv
) = PROMO_RANK
;
817 else if (IS_AGGR_TYPE (to
) && IS_AGGR_TYPE (from
)
818 && is_properly_derived_from (from
, to
))
820 if (TREE_CODE (conv
) == RVALUE_CONV
)
821 conv
= TREE_OPERAND (conv
, 0);
822 conv
= build_conv (BASE_CONV
, to
, conv
);
823 /* The derived-to-base conversion indicates the initialization
824 of a parameter with base type from an object of a derived
825 type. A temporary object is created to hold the result of
827 NEED_TEMPORARY_P (conv
) = 1;
835 /* Returns non-zero if T1 is reference-related to T2. */
838 reference_related_p (t1
, t2
)
842 t1
= TYPE_MAIN_VARIANT (t1
);
843 t2
= TYPE_MAIN_VARIANT (t2
);
847 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
848 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
850 return (same_type_p (t1
, t2
)
851 || (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
852 && DERIVED_FROM_P (t1
, t2
)));
855 /* Returns non-zero if T1 is reference-compatible with T2. */
858 reference_compatible_p (t1
, t2
)
864 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
865 reference-related to T2 and cv1 is the same cv-qualification as,
866 or greater cv-qualification than, cv2. */
867 return (reference_related_p (t1
, t2
)
868 && at_least_as_qualified_p (t1
, t2
));
871 /* Determine whether or not the EXPR (of class type S) can be
872 converted to T as in [over.match.ref]. */
875 convert_class_to_reference (t
, s
, expr
)
883 struct z_candidate
*candidates
;
884 struct z_candidate
*cand
;
888 Assuming that "cv1 T" is the underlying type of the reference
889 being initialized, and "cv S" is the type of the initializer
890 expression, with S a class type, the candidate functions are
893 --The conversion functions of S and its base classes are
894 considered. Those that are not hidden within S and yield type
895 "reference to cv2 T2", where "cv1 T" is reference-compatible
896 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
898 The argument list has one argument, which is the initializer
903 /* Conceptually, we should take the address of EXPR and put it in
904 the argument list. Unfortunately, however, that can result in
905 error messages, which we should not issue now because we are just
906 trying to find a conversion operator. Therefore, we use NULL,
907 cast to the appropriate type. */
908 arglist
= build_int_2 (0, 0);
909 TREE_TYPE (arglist
) = build_pointer_type (s
);
910 arglist
= build_tree_list (NULL_TREE
, arglist
);
912 for (conversions
= lookup_conversions (s
);
914 conversions
= TREE_CHAIN (conversions
))
916 tree fns
= TREE_VALUE (conversions
);
918 for (; fns
; fns
= OVL_NEXT (fns
))
920 tree f
= OVL_CURRENT (fns
);
921 tree t2
= TREE_TYPE (TREE_TYPE (f
));
922 struct z_candidate
*old_candidates
= candidates
;
924 /* If this is a template function, try to get an exact
926 if (TREE_CODE (f
) == TEMPLATE_DECL
)
929 = add_template_candidate (candidates
,
933 build_reference_type (t
),
937 if (candidates
!= old_candidates
)
939 /* Now, see if the conversion function really returns
940 an lvalue of the appropriate type. From the
941 point of view of unification, simply returning an
942 rvalue of the right type is good enough. */
944 t2
= TREE_TYPE (TREE_TYPE (f
));
945 if (TREE_CODE (t2
) != REFERENCE_TYPE
946 || !reference_compatible_p (t
, TREE_TYPE (t2
)))
947 candidates
= candidates
->next
;
950 else if (TREE_CODE (t2
) == REFERENCE_TYPE
951 && reference_compatible_p (t
, TREE_TYPE (t2
)))
953 = add_function_candidate (candidates
, f
, s
, arglist
,
956 if (candidates
!= old_candidates
)
957 candidates
->basetype_path
= TYPE_BINFO (s
);
961 /* If none of the conversion functions worked out, let our caller
963 if (!any_viable (candidates
))
966 candidates
= splice_viable (candidates
);
967 cand
= tourney (candidates
);
971 conv
= build1 (IDENTITY_CONV
, s
, expr
);
972 conv
= build_conv (USER_CONV
,
973 non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
))),
975 TREE_OPERAND (conv
, 1) = build_expr_ptr_wrapper (cand
);
976 ICS_USER_FLAG (conv
) = 1;
977 if (cand
->viable
== -1)
978 ICS_BAD_FLAG (conv
) = 1;
979 cand
->second_conv
= conv
;
984 /* A reference of the indicated TYPE is being bound directly to the
985 expression represented by the implicit conversion sequence CONV.
986 Return a conversion sequence for this binding. */
989 direct_reference_binding (type
, conv
)
993 tree t
= TREE_TYPE (type
);
997 When a parameter of reference type binds directly
998 (_dcl.init.ref_) to an argument expression, the implicit
999 conversion sequence is the identity conversion, unless the
1000 argument expression has a type that is a derived class of the
1001 parameter type, in which case the implicit conversion sequence is
1002 a derived-to-base Conversion.
1004 If the parameter binds directly to the result of applying a
1005 conversion function to the argument expression, the implicit
1006 conversion sequence is a user-defined conversion sequence
1007 (_over.ics.user_), with the second standard conversion sequence
1008 either an identity conversion or, if the conversion function
1009 returns an entity of a type that is a derived class of the
1010 parameter type, a derived-to-base conversion. */
1011 if (!same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (conv
)))
1013 /* Represent the derived-to-base conversion. */
1014 conv
= build_conv (BASE_CONV
, t
, conv
);
1015 /* We will actually be binding to the base-class subobject in
1016 the derived class, so we mark this conversion appropriately.
1017 That way, convert_like knows not to generate a temporary. */
1018 NEED_TEMPORARY_P (conv
) = 0;
1020 return build_conv (REF_BIND
, type
, conv
);
1023 /* Returns the conversion path from type FROM to reference type TO for
1024 purposes of reference binding. For lvalue binding, either pass a
1025 reference type to FROM or an lvalue expression to EXPR. If the
1026 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1027 the conversion returned. */
1030 reference_binding (rto
, rfrom
, expr
, flags
)
1031 tree rto
, rfrom
, expr
;
1034 tree conv
= NULL_TREE
;
1035 tree to
= TREE_TYPE (rto
);
1039 cp_lvalue_kind lvalue_p
= clk_none
;
1041 if (TREE_CODE (to
) == FUNCTION_TYPE
&& expr
&& type_unknown_p (expr
))
1043 expr
= instantiate_type (to
, expr
, 0);
1044 if (expr
== error_mark_node
)
1046 from
= TREE_TYPE (expr
);
1049 if (TREE_CODE (from
) == REFERENCE_TYPE
)
1051 /* Anything with reference type is an lvalue. */
1052 lvalue_p
= clk_ordinary
;
1053 from
= TREE_TYPE (from
);
1056 lvalue_p
= real_lvalue_p (expr
);
1058 /* Figure out whether or not the types are reference-related and
1059 reference compatible. We have do do this after stripping
1060 references from FROM. */
1061 related_p
= reference_related_p (to
, from
);
1062 compatible_p
= reference_compatible_p (to
, from
);
1064 if (lvalue_p
&& compatible_p
)
1068 If the intializer expression
1070 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1071 is reference-compatible with "cv2 T2,"
1073 the reference is bound directly to the initializer exprssion
1075 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1076 conv
= direct_reference_binding (rto
, conv
);
1077 if ((lvalue_p
& clk_bitfield
) != 0
1078 && CP_TYPE_CONST_NON_VOLATILE_P (to
))
1079 /* For the purposes of overload resolution, we ignore the fact
1080 this expression is a bitfield. (In particular,
1081 [over.ics.ref] says specifically that a function with a
1082 non-const reference parameter is viable even if the
1083 argument is a bitfield.)
1085 However, when we actually call the function we must create
1086 a temporary to which to bind the reference. If the
1087 reference is volatile, or isn't const, then we cannot make
1088 a temporary, so we just issue an error when the conversion
1090 NEED_TEMPORARY_P (conv
) = 1;
1093 else if (CLASS_TYPE_P (from
) && !(flags
& LOOKUP_NO_CONVERSION
))
1097 If the initializer exprsesion
1099 -- has a class type (i.e., T2 is a class type) can be
1100 implicitly converted to an lvalue of type "cv3 T3," where
1101 "cv1 T1" is reference-compatible with "cv3 T3". (this
1102 conversion is selected by enumerating the applicable
1103 conversion functions (_over.match.ref_) and choosing the
1104 best one through overload resolution. (_over.match_).
1106 the reference is bound to the lvalue result of the conversion
1107 in the second case. */
1108 conv
= convert_class_to_reference (to
, from
, expr
);
1110 return direct_reference_binding (rto
, conv
);
1113 /* From this point on, we conceptually need temporaries, even if we
1114 elide them. Only the cases above are "direct bindings". */
1115 if (flags
& LOOKUP_NO_TEMP_BIND
)
1120 When a parameter of reference type is not bound directly to an
1121 argument expression, the conversion sequence is the one required
1122 to convert the argument expression to the underlying type of the
1123 reference according to _over.best.ics_. Conceptually, this
1124 conversion sequence corresponds to copy-initializing a temporary
1125 of the underlying type with the argument expression. Any
1126 difference in top-level cv-qualification is subsumed by the
1127 initialization itself and does not constitute a conversion. */
1131 Otherwise, the reference shall be to a non-volatile const type. */
1132 if (!CP_TYPE_CONST_NON_VOLATILE_P (to
))
1137 If the initializer expression is an rvalue, with T2 a class type,
1138 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1139 is bound in one of the following ways:
1141 -- The reference is bound to the object represented by the rvalue
1142 or to a sub-object within that object.
1144 In this case, the implicit conversion sequence is supposed to be
1145 same as we would obtain by generating a temporary. Fortunately,
1146 if the types are reference compatible, then this is either an
1147 identity conversion or the derived-to-base conversion, just as
1148 for direct binding. */
1149 if (CLASS_TYPE_P (from
) && compatible_p
)
1151 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1152 return direct_reference_binding (rto
, conv
);
1157 Otherwise, a temporary of type "cv1 T1" is created and
1158 initialized from the initializer expression using the rules for a
1159 non-reference copy initialization. If T1 is reference-related to
1160 T2, cv1 must be the same cv-qualification as, or greater
1161 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1162 if (related_p
&& !at_least_as_qualified_p (to
, from
))
1165 conv
= implicit_conversion (to
, from
, expr
, flags
);
1169 conv
= build_conv (REF_BIND
, rto
, conv
);
1170 /* This reference binding, unlike those above, requires the
1171 creation of a temporary. */
1172 NEED_TEMPORARY_P (conv
) = 1;
1177 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1178 to type TO. The optional expression EXPR may affect the conversion.
1179 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1183 implicit_conversion (to
, from
, expr
, flags
)
1184 tree to
, from
, expr
;
1188 struct z_candidate
*cand
;
1190 /* Resolve expressions like `A::p' that we thought might become
1191 pointers-to-members. */
1192 if (expr
&& TREE_CODE (expr
) == OFFSET_REF
)
1194 expr
= resolve_offset_ref (expr
);
1195 from
= TREE_TYPE (expr
);
1198 if (from
== error_mark_node
|| to
== error_mark_node
1199 || expr
== error_mark_node
)
1202 /* Make sure both the FROM and TO types are complete so that
1203 user-defined conversions are available. */
1204 complete_type (from
);
1207 if (TREE_CODE (to
) == REFERENCE_TYPE
)
1208 conv
= reference_binding (to
, from
, expr
, flags
);
1210 conv
= standard_conversion (to
, from
, expr
);
1214 else if (expr
!= NULL_TREE
1215 && (IS_AGGR_TYPE (non_reference (from
))
1216 || IS_AGGR_TYPE (non_reference (to
)))
1217 && (flags
& LOOKUP_NO_CONVERSION
) == 0)
1219 cand
= build_user_type_conversion_1
1220 (to
, expr
, LOOKUP_ONLYCONVERTING
);
1222 conv
= cand
->second_conv
;
1223 if ((! conv
|| ICS_BAD_FLAG (conv
))
1224 && TREE_CODE (to
) == REFERENCE_TYPE
1225 && (flags
& LOOKUP_NO_TEMP_BIND
) == 0)
1227 cand
= build_user_type_conversion_1
1228 (TYPE_MAIN_VARIANT (TREE_TYPE (to
)), expr
, LOOKUP_ONLYCONVERTING
);
1231 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to
)))
1232 ICS_BAD_FLAG (cand
->second_conv
) = 1;
1233 if (!conv
|| (ICS_BAD_FLAG (conv
)
1234 > ICS_BAD_FLAG (cand
->second_conv
)))
1235 conv
= build_conv (REF_BIND
, to
, cand
->second_conv
);
1243 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1246 static struct z_candidate
*
1247 add_candidate (candidates
, fn
, convs
, viable
)
1248 struct z_candidate
*candidates
;
1252 struct z_candidate
*cand
1253 = (struct z_candidate
*) ggc_alloc_cleared (sizeof (struct z_candidate
));
1256 cand
->convs
= convs
;
1257 cand
->viable
= viable
;
1258 cand
->next
= candidates
;
1263 /* Create an overload candidate for the function or method FN called with
1264 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1265 to implicit_conversion.
1267 CTYPE, if non-NULL, is the type we want to pretend this function
1268 comes from for purposes of overload resolution. */
1270 static struct z_candidate
*
1271 add_function_candidate (candidates
, fn
, ctype
, arglist
, flags
)
1272 struct z_candidate
*candidates
;
1273 tree fn
, ctype
, arglist
;
1276 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1279 tree parmnode
, argnode
;
1282 /* The `this' and `in_chrg' arguments to constructors are not considered
1283 in overload resolution. */
1284 if (DECL_CONSTRUCTOR_P (fn
))
1286 parmlist
= TREE_CHAIN (parmlist
);
1287 arglist
= TREE_CHAIN (arglist
);
1288 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
1290 parmlist
= TREE_CHAIN (parmlist
);
1291 arglist
= TREE_CHAIN (arglist
);
1295 len
= list_length (arglist
);
1296 convs
= make_tree_vec (len
);
1298 /* 13.3.2 - Viable functions [over.match.viable]
1299 First, to be a viable function, a candidate function shall have enough
1300 parameters to agree in number with the arguments in the list.
1302 We need to check this first; otherwise, checking the ICSes might cause
1303 us to produce an ill-formed template instantiation. */
1305 parmnode
= parmlist
;
1306 for (i
= 0; i
< len
; ++i
)
1308 if (parmnode
== NULL_TREE
|| parmnode
== void_list_node
)
1310 parmnode
= TREE_CHAIN (parmnode
);
1313 if (i
< len
&& parmnode
)
1316 /* Make sure there are default args for the rest of the parms. */
1317 else for (; parmnode
&& parmnode
!= void_list_node
;
1318 parmnode
= TREE_CHAIN (parmnode
))
1319 if (! TREE_PURPOSE (parmnode
))
1328 /* Second, for F to be a viable function, there shall exist for each
1329 argument an implicit conversion sequence that converts that argument
1330 to the corresponding parameter of F. */
1332 parmnode
= parmlist
;
1335 for (i
= 0; i
< len
; ++i
)
1337 tree arg
= TREE_VALUE (argnode
);
1338 tree argtype
= lvalue_type (arg
);
1342 if (parmnode
== void_list_node
)
1345 is_this
= (i
== 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1346 && ! DECL_CONSTRUCTOR_P (fn
));
1350 tree parmtype
= TREE_VALUE (parmnode
);
1352 /* The type of the implicit object parameter ('this') for
1353 overload resolution is not always the same as for the
1354 function itself; conversion functions are considered to
1355 be members of the class being converted, and functions
1356 introduced by a using-declaration are considered to be
1357 members of the class that uses them.
1359 Since build_over_call ignores the ICS for the `this'
1360 parameter, we can just change the parm type. */
1361 if (ctype
&& is_this
)
1364 = build_qualified_type (ctype
,
1365 TYPE_QUALS (TREE_TYPE (parmtype
)));
1366 parmtype
= build_pointer_type (parmtype
);
1369 t
= implicit_conversion (parmtype
, argtype
, arg
, flags
);
1373 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1374 ICS_ELLIPSIS_FLAG (t
) = 1;
1378 ICS_THIS_FLAG (t
) = 1;
1380 TREE_VEC_ELT (convs
, i
) = t
;
1387 if (ICS_BAD_FLAG (t
))
1391 parmnode
= TREE_CHAIN (parmnode
);
1392 argnode
= TREE_CHAIN (argnode
);
1396 return add_candidate (candidates
, fn
, convs
, viable
);
1399 /* Create an overload candidate for the conversion function FN which will
1400 be invoked for expression OBJ, producing a pointer-to-function which
1401 will in turn be called with the argument list ARGLIST, and add it to
1402 CANDIDATES. FLAGS is passed on to implicit_conversion.
1404 Actually, we don't really care about FN; we care about the type it
1405 converts to. There may be multiple conversion functions that will
1406 convert to that type, and we rely on build_user_type_conversion_1 to
1407 choose the best one; so when we create our candidate, we record the type
1408 instead of the function. */
1410 static struct z_candidate
*
1411 add_conv_candidate (candidates
, fn
, obj
, arglist
)
1412 struct z_candidate
*candidates
;
1413 tree fn
, obj
, arglist
;
1415 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
1416 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (totype
));
1417 int i
, len
= list_length (arglist
) + 1;
1418 tree convs
= make_tree_vec (len
);
1419 tree parmnode
= parmlist
;
1420 tree argnode
= arglist
;
1422 int flags
= LOOKUP_NORMAL
;
1424 /* Don't bother looking up the same type twice. */
1425 if (candidates
&& candidates
->fn
== totype
)
1428 for (i
= 0; i
< len
; ++i
)
1430 tree arg
= i
== 0 ? obj
: TREE_VALUE (argnode
);
1431 tree argtype
= lvalue_type (arg
);
1435 t
= implicit_conversion (totype
, argtype
, arg
, flags
);
1436 else if (parmnode
== void_list_node
)
1439 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
, flags
);
1442 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1443 ICS_ELLIPSIS_FLAG (t
) = 1;
1446 TREE_VEC_ELT (convs
, i
) = t
;
1450 if (ICS_BAD_FLAG (t
))
1457 parmnode
= TREE_CHAIN (parmnode
);
1458 argnode
= TREE_CHAIN (argnode
);
1464 for (; parmnode
&& parmnode
!= void_list_node
;
1465 parmnode
= TREE_CHAIN (parmnode
))
1466 if (! TREE_PURPOSE (parmnode
))
1472 return add_candidate (candidates
, totype
, convs
, viable
);
1475 static struct z_candidate
*
1476 build_builtin_candidate (candidates
, fnname
, type1
, type2
,
1477 args
, argtypes
, flags
)
1478 struct z_candidate
*candidates
;
1479 tree fnname
, type1
, type2
, *args
, *argtypes
;
1490 convs
= make_tree_vec (args
[2] ? 3 : (args
[1] ? 2 : 1));
1492 for (i
= 0; i
< 2; ++i
)
1497 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
], flags
);
1501 /* We need something for printing the candidate. */
1502 t
= build1 (IDENTITY_CONV
, types
[i
], NULL_TREE
);
1504 else if (ICS_BAD_FLAG (t
))
1506 TREE_VEC_ELT (convs
, i
) = t
;
1509 /* For COND_EXPR we rearranged the arguments; undo that now. */
1512 TREE_VEC_ELT (convs
, 2) = TREE_VEC_ELT (convs
, 1);
1513 TREE_VEC_ELT (convs
, 1) = TREE_VEC_ELT (convs
, 0);
1514 t
= implicit_conversion (boolean_type_node
, argtypes
[2], args
[2], flags
);
1516 TREE_VEC_ELT (convs
, 0) = t
;
1521 return add_candidate (candidates
, fnname
, convs
, viable
);
1528 return COMPLETE_TYPE_P (complete_type (t
));
1531 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1534 promoted_arithmetic_type_p (type
)
1539 In this section, the term promoted integral type is used to refer
1540 to those integral types which are preserved by integral promotion
1541 (including e.g. int and long but excluding e.g. char).
1542 Similarly, the term promoted arithmetic type refers to promoted
1543 integral types plus floating types. */
1544 return ((INTEGRAL_TYPE_P (type
)
1545 && same_type_p (type_promotes_to (type
), type
))
1546 || TREE_CODE (type
) == REAL_TYPE
);
1549 /* Create any builtin operator overload candidates for the operator in
1550 question given the converted operand types TYPE1 and TYPE2. The other
1551 args are passed through from add_builtin_candidates to
1552 build_builtin_candidate. */
1554 static struct z_candidate
*
1555 add_builtin_candidate (candidates
, code
, code2
, fnname
, type1
, type2
,
1556 args
, argtypes
, flags
)
1557 struct z_candidate
*candidates
;
1558 enum tree_code code
, code2
;
1559 tree fnname
, type1
, type2
, *args
, *argtypes
;
1564 case POSTINCREMENT_EXPR
:
1565 case POSTDECREMENT_EXPR
:
1566 args
[1] = integer_zero_node
;
1567 type2
= integer_type_node
;
1576 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1577 and VQ is either volatile or empty, there exist candidate operator
1578 functions of the form
1579 VQ T& operator++(VQ T&);
1580 T operator++(VQ T&, int);
1581 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1582 type other than bool, and VQ is either volatile or empty, there exist
1583 candidate operator functions of the form
1584 VQ T& operator--(VQ T&);
1585 T operator--(VQ T&, int);
1586 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1587 complete object type, and VQ is either volatile or empty, there exist
1588 candidate operator functions of the form
1589 T*VQ& operator++(T*VQ&);
1590 T*VQ& operator--(T*VQ&);
1591 T* operator++(T*VQ&, int);
1592 T* operator--(T*VQ&, int); */
1594 case POSTDECREMENT_EXPR
:
1595 case PREDECREMENT_EXPR
:
1596 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
1598 case POSTINCREMENT_EXPR
:
1599 case PREINCREMENT_EXPR
:
1600 if ((ARITHMETIC_TYPE_P (type1
) && TREE_CODE (type1
) != ENUMERAL_TYPE
)
1601 || TYPE_PTROB_P (type1
))
1603 type1
= build_reference_type (type1
);
1608 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1609 exist candidate operator functions of the form
1613 8 For every function type T, there exist candidate operator functions of
1615 T& operator*(T*); */
1618 if (TREE_CODE (type1
) == POINTER_TYPE
1619 && (TYPE_PTROB_P (type1
)
1620 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
1624 /* 9 For every type T, there exist candidate operator functions of the form
1627 10For every promoted arithmetic type T, there exist candidate operator
1628 functions of the form
1632 case CONVERT_EXPR
: /* unary + */
1633 if (TREE_CODE (type1
) == POINTER_TYPE
1634 && TREE_CODE (TREE_TYPE (type1
)) != OFFSET_TYPE
)
1637 if (ARITHMETIC_TYPE_P (type1
))
1641 /* 11For every promoted integral type T, there exist candidate operator
1642 functions of the form
1646 if (INTEGRAL_TYPE_P (type1
))
1650 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1651 is the same type as C2 or is a derived class of C2, T is a complete
1652 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1653 there exist candidate operator functions of the form
1654 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1655 where CV12 is the union of CV1 and CV2. */
1658 if (TREE_CODE (type1
) == POINTER_TYPE
1659 && (TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
)))
1661 tree c1
= TREE_TYPE (type1
);
1662 tree c2
= (TYPE_PTRMEMFUNC_P (type2
)
1663 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2
)))
1664 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2
)));
1666 if (IS_AGGR_TYPE (c1
) && DERIVED_FROM_P (c2
, c1
)
1667 && (TYPE_PTRMEMFUNC_P (type2
)
1668 || is_complete (TREE_TYPE (TREE_TYPE (type2
)))))
1673 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1674 didate operator functions of the form
1679 bool operator<(L, R);
1680 bool operator>(L, R);
1681 bool operator<=(L, R);
1682 bool operator>=(L, R);
1683 bool operator==(L, R);
1684 bool operator!=(L, R);
1685 where LR is the result of the usual arithmetic conversions between
1688 14For every pair of types T and I, where T is a cv-qualified or cv-
1689 unqualified complete object type and I is a promoted integral type,
1690 there exist candidate operator functions of the form
1691 T* operator+(T*, I);
1692 T& operator[](T*, I);
1693 T* operator-(T*, I);
1694 T* operator+(I, T*);
1695 T& operator[](I, T*);
1697 15For every T, where T is a pointer to complete object type, there exist
1698 candidate operator functions of the form112)
1699 ptrdiff_t operator-(T, T);
1701 16For every pointer type T, there exist candidate operator functions of
1703 bool operator<(T, T);
1704 bool operator>(T, T);
1705 bool operator<=(T, T);
1706 bool operator>=(T, T);
1707 bool operator==(T, T);
1708 bool operator!=(T, T);
1710 17For every pointer to member type T, there exist candidate operator
1711 functions of the form
1712 bool operator==(T, T);
1713 bool operator!=(T, T); */
1716 if (TYPE_PTROB_P (type1
) && TYPE_PTROB_P (type2
))
1718 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1720 type2
= ptrdiff_type_node
;
1724 case TRUNC_DIV_EXPR
:
1725 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1731 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1732 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
)))
1734 if ((TYPE_PTRMEMFUNC_P (type1
) || TYPE_PTRMEM_P (type1
))
1735 && null_ptr_cst_p (args
[1]))
1740 if ((TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
))
1741 && null_ptr_cst_p (args
[0]))
1752 if ((ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1753 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
)))
1755 if (TYPE_PTR_P (type1
) && null_ptr_cst_p (args
[1]))
1760 if (null_ptr_cst_p (args
[0]) && TYPE_PTR_P (type2
))
1768 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1771 if (INTEGRAL_TYPE_P (type1
) && TYPE_PTROB_P (type2
))
1773 type1
= ptrdiff_type_node
;
1776 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1778 type2
= ptrdiff_type_node
;
1783 /* 18For every pair of promoted integral types L and R, there exist candi-
1784 date operator functions of the form
1791 where LR is the result of the usual arithmetic conversions between
1794 case TRUNC_MOD_EXPR
:
1800 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1804 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1805 type, VQ is either volatile or empty, and R is a promoted arithmetic
1806 type, there exist candidate operator functions of the form
1807 VQ L& operator=(VQ L&, R);
1808 VQ L& operator*=(VQ L&, R);
1809 VQ L& operator/=(VQ L&, R);
1810 VQ L& operator+=(VQ L&, R);
1811 VQ L& operator-=(VQ L&, R);
1813 20For every pair T, VQ), where T is any type and VQ is either volatile
1814 or empty, there exist candidate operator functions of the form
1815 T*VQ& operator=(T*VQ&, T*);
1817 21For every pair T, VQ), where T is a pointer to member type and VQ is
1818 either volatile or empty, there exist candidate operator functions of
1820 VQ T& operator=(VQ T&, T);
1822 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1823 unqualified complete object type, VQ is either volatile or empty, and
1824 I is a promoted integral type, there exist candidate operator func-
1826 T*VQ& operator+=(T*VQ&, I);
1827 T*VQ& operator-=(T*VQ&, I);
1829 23For every triple L, VQ, R), where L is an integral or enumeration
1830 type, VQ is either volatile or empty, and R is a promoted integral
1831 type, there exist candidate operator functions of the form
1833 VQ L& operator%=(VQ L&, R);
1834 VQ L& operator<<=(VQ L&, R);
1835 VQ L& operator>>=(VQ L&, R);
1836 VQ L& operator&=(VQ L&, R);
1837 VQ L& operator^=(VQ L&, R);
1838 VQ L& operator|=(VQ L&, R); */
1845 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1847 type2
= ptrdiff_type_node
;
1851 case TRUNC_DIV_EXPR
:
1852 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1856 case TRUNC_MOD_EXPR
:
1862 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1867 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1869 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1870 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1871 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
))
1872 || ((TYPE_PTRMEMFUNC_P (type1
)
1873 || TREE_CODE (type1
) == POINTER_TYPE
)
1874 && null_ptr_cst_p (args
[1])))
1882 my_friendly_abort (367);
1884 type1
= build_reference_type (type1
);
1890 For every pair of promoted arithmetic types L and R, there
1891 exist candidate operator functions of the form
1893 LR operator?(bool, L, R);
1895 where LR is the result of the usual arithmetic conversions
1896 between types L and R.
1898 For every type T, where T is a pointer or pointer-to-member
1899 type, there exist candidate operator functions of the form T
1900 operator?(bool, T, T); */
1902 if (promoted_arithmetic_type_p (type1
)
1903 && promoted_arithmetic_type_p (type2
))
1907 /* Otherwise, the types should be pointers. */
1908 if (!(TREE_CODE (type1
) == POINTER_TYPE
1909 || TYPE_PTRMEM_P (type1
)
1910 || TYPE_PTRMEMFUNC_P (type1
))
1911 || !(TREE_CODE (type2
) == POINTER_TYPE
1912 || TYPE_PTRMEM_P (type2
)
1913 || TYPE_PTRMEMFUNC_P (type2
)))
1916 /* We don't check that the two types are the same; the logic
1917 below will actually create two candidates; one in which both
1918 parameter types are TYPE1, and one in which both parameter
1922 /* These arguments do not make for a legal overloaded operator. */
1926 my_friendly_abort (367);
1929 /* If we're dealing with two pointer types, we need candidates
1930 for both of them. */
1931 if (type2
&& !same_type_p (type1
, type2
)
1932 && TREE_CODE (type1
) == TREE_CODE (type2
)
1933 && (TREE_CODE (type1
) == REFERENCE_TYPE
1934 || (TREE_CODE (type1
) == POINTER_TYPE
1935 && TYPE_PTRMEM_P (type1
) == TYPE_PTRMEM_P (type2
))
1936 || TYPE_PTRMEMFUNC_P (type1
)
1937 || IS_AGGR_TYPE (type1
)))
1939 candidates
= build_builtin_candidate
1940 (candidates
, fnname
, type1
, type1
, args
, argtypes
, flags
);
1941 return build_builtin_candidate
1942 (candidates
, fnname
, type2
, type2
, args
, argtypes
, flags
);
1945 return build_builtin_candidate
1946 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
);
1950 type_decays_to (type
)
1953 if (TREE_CODE (type
) == ARRAY_TYPE
)
1954 return build_pointer_type (TREE_TYPE (type
));
1955 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1956 return build_pointer_type (type
);
1960 /* There are three conditions of builtin candidates:
1962 1) bool-taking candidates. These are the same regardless of the input.
1963 2) pointer-pair taking candidates. These are generated for each type
1964 one of the input types converts to.
1965 3) arithmetic candidates. According to the standard, we should generate
1966 all of these, but I'm trying not to... */
1968 static struct z_candidate
*
1969 add_builtin_candidates (candidates
, code
, code2
, fnname
, args
, flags
)
1970 struct z_candidate
*candidates
;
1971 enum tree_code code
, code2
;
1976 tree type
, argtypes
[3];
1977 /* TYPES[i] is the set of possible builtin-operator parameter types
1978 we will consider for the Ith argument. These are represented as
1979 a TREE_LIST; the TREE_VALUE of each node is the potential
1983 for (i
= 0; i
< 3; ++i
)
1986 argtypes
[i
] = lvalue_type (args
[i
]);
1988 argtypes
[i
] = NULL_TREE
;
1993 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1994 and VQ is either volatile or empty, there exist candidate operator
1995 functions of the form
1996 VQ T& operator++(VQ T&); */
1998 case POSTINCREMENT_EXPR
:
1999 case PREINCREMENT_EXPR
:
2000 case POSTDECREMENT_EXPR
:
2001 case PREDECREMENT_EXPR
:
2006 /* 24There also exist candidate operator functions of the form
2007 bool operator!(bool);
2008 bool operator&&(bool, bool);
2009 bool operator||(bool, bool); */
2011 case TRUTH_NOT_EXPR
:
2012 return build_builtin_candidate
2013 (candidates
, fnname
, boolean_type_node
,
2014 NULL_TREE
, args
, argtypes
, flags
);
2016 case TRUTH_ORIF_EXPR
:
2017 case TRUTH_ANDIF_EXPR
:
2018 return build_builtin_candidate
2019 (candidates
, fnname
, boolean_type_node
,
2020 boolean_type_node
, args
, argtypes
, flags
);
2031 types
[0] = types
[1] = NULL_TREE
;
2033 for (i
= 0; i
< 2; ++i
)
2037 else if (IS_AGGR_TYPE (argtypes
[i
]))
2041 if (i
== 0 && code
== MODIFY_EXPR
&& code2
== NOP_EXPR
)
2044 convs
= lookup_conversions (argtypes
[i
]);
2046 if (code
== COND_EXPR
)
2048 if (real_lvalue_p (args
[i
]))
2049 types
[i
] = tree_cons
2050 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2052 types
[i
] = tree_cons
2053 (NULL_TREE
, TYPE_MAIN_VARIANT (argtypes
[i
]), types
[i
]);
2059 for (; convs
; convs
= TREE_CHAIN (convs
))
2061 type
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs
))));
2064 && (TREE_CODE (type
) != REFERENCE_TYPE
2065 || CP_TYPE_CONST_P (TREE_TYPE (type
))))
2068 if (code
== COND_EXPR
&& TREE_CODE (type
) == REFERENCE_TYPE
)
2069 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2071 type
= non_reference (type
);
2072 if (i
!= 0 || ! ref1
)
2074 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2075 if (code
== COND_EXPR
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2076 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2077 if (INTEGRAL_TYPE_P (type
))
2078 type
= type_promotes_to (type
);
2081 if (! value_member (type
, types
[i
]))
2082 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2087 if (code
== COND_EXPR
&& real_lvalue_p (args
[i
]))
2088 types
[i
] = tree_cons
2089 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2090 type
= non_reference (argtypes
[i
]);
2091 if (i
!= 0 || ! ref1
)
2093 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2094 if (code
== COND_EXPR
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2095 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2096 if (INTEGRAL_TYPE_P (type
))
2097 type
= type_promotes_to (type
);
2099 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2103 /* Run through the possible parameter types of both arguments,
2104 creating candidates with those parameter types. */
2105 for (; types
[0]; types
[0] = TREE_CHAIN (types
[0]))
2108 for (type
= types
[1]; type
; type
= TREE_CHAIN (type
))
2109 candidates
= add_builtin_candidate
2110 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2111 TREE_VALUE (type
), args
, argtypes
, flags
);
2113 candidates
= add_builtin_candidate
2114 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2115 NULL_TREE
, args
, argtypes
, flags
);
2122 /* If TMPL can be successfully instantiated as indicated by
2123 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2125 TMPL is the template. EXPLICIT_TARGS are any explicit template
2126 arguments. ARGLIST is the arguments provided at the call-site.
2127 The RETURN_TYPE is the desired type for conversion operators. If
2128 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2129 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2130 add_conv_candidate. */
2132 static struct z_candidate
*
2133 add_template_candidate_real (candidates
, tmpl
, ctype
, explicit_targs
,
2134 arglist
, return_type
, flags
,
2136 struct z_candidate
*candidates
;
2137 tree tmpl
, ctype
, explicit_targs
, arglist
, return_type
;
2140 unification_kind_t strict
;
2142 int ntparms
= DECL_NTPARMS (tmpl
);
2143 tree targs
= make_tree_vec (ntparms
);
2144 tree args_without_in_chrg
;
2145 struct z_candidate
*cand
;
2149 /* TEMPLATE_DECLs do not have the in-charge parameter, nor the VTT
2150 parameter. So, skip it here before attempting to perform
2151 argument deduction. */
2152 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl
)
2153 || DECL_BASE_CONSTRUCTOR_P (tmpl
))
2154 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl
)))
2155 args_without_in_chrg
= tree_cons (NULL_TREE
,
2156 TREE_VALUE (arglist
),
2157 TREE_CHAIN (TREE_CHAIN (arglist
)));
2159 args_without_in_chrg
= arglist
;
2161 i
= fn_type_unification (tmpl
, explicit_targs
, targs
,
2162 args_without_in_chrg
,
2163 return_type
, strict
);
2168 fn
= instantiate_template (tmpl
, targs
);
2169 if (fn
== error_mark_node
)
2172 if (obj
!= NULL_TREE
)
2173 /* Aha, this is a conversion function. */
2174 cand
= add_conv_candidate (candidates
, fn
, obj
, arglist
);
2176 cand
= add_function_candidate (candidates
, fn
, ctype
,
2178 if (DECL_TI_TEMPLATE (fn
) != tmpl
)
2179 /* This situation can occur if a member template of a template
2180 class is specialized. Then, instantiate_template might return
2181 an instantiation of the specialization, in which case the
2182 DECL_TI_TEMPLATE field will point at the original
2183 specialization. For example:
2185 template <class T> struct S { template <class U> void f(U);
2186 template <> void f(int) {}; };
2190 Here, TMPL will be template <class U> S<double>::f(U).
2191 And, instantiate template will give us the specialization
2192 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2193 for this will point at template <class T> template <> S<T>::f(int),
2194 so that we can find the definition. For the purposes of
2195 overload resolution, however, we want the original TMPL. */
2196 cand
->template = tree_cons (tmpl
, targs
, NULL_TREE
);
2198 cand
->template = DECL_TEMPLATE_INFO (fn
);
2204 static struct z_candidate
*
2205 add_template_candidate (candidates
, tmpl
, ctype
, explicit_targs
,
2206 arglist
, return_type
, flags
, strict
)
2207 struct z_candidate
*candidates
;
2208 tree tmpl
, ctype
, explicit_targs
, arglist
, return_type
;
2210 unification_kind_t strict
;
2213 add_template_candidate_real (candidates
, tmpl
, ctype
,
2214 explicit_targs
, arglist
, return_type
, flags
,
2219 static struct z_candidate
*
2220 add_template_conv_candidate (candidates
, tmpl
, obj
, arglist
, return_type
)
2221 struct z_candidate
*candidates
;
2222 tree tmpl
, obj
, arglist
, return_type
;
2225 add_template_candidate_real (candidates
, tmpl
, NULL_TREE
, NULL_TREE
,
2226 arglist
, return_type
, 0, obj
, DEDUCE_CONV
);
2232 struct z_candidate
*cands
;
2234 for (; cands
; cands
= cands
->next
)
2235 if (pedantic
? cands
->viable
== 1 : cands
->viable
)
2240 static struct z_candidate
*
2241 splice_viable (cands
)
2242 struct z_candidate
*cands
;
2244 struct z_candidate
**p
= &cands
;
2248 if (pedantic
? (*p
)->viable
== 1 : (*p
)->viable
)
2261 /* Fix this to work on non-lvalues. */
2262 return build_unary_op (ADDR_EXPR
, obj
, 0);
2266 print_z_candidates (candidates
)
2267 struct z_candidate
*candidates
;
2269 const char *str
= "candidates are:";
2270 for (; candidates
; candidates
= candidates
->next
)
2272 if (TREE_CODE (candidates
->fn
) == IDENTIFIER_NODE
)
2274 if (TREE_VEC_LENGTH (candidates
->convs
) == 3)
2275 cp_error ("%s %D(%T, %T, %T) <builtin>", str
, candidates
->fn
,
2276 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2277 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)),
2278 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 2)));
2279 else if (TREE_VEC_LENGTH (candidates
->convs
) == 2)
2280 cp_error ("%s %D(%T, %T) <builtin>", str
, candidates
->fn
,
2281 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2282 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)));
2284 cp_error ("%s %D(%T) <builtin>", str
, candidates
->fn
,
2285 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)));
2287 else if (TYPE_P (candidates
->fn
))
2288 cp_error ("%s %T <conversion>", str
, candidates
->fn
);
2290 cp_error_at ("%s %+#D%s", str
, candidates
->fn
,
2291 candidates
->viable
== -1 ? " <near match>" : "");
2296 /* Returns the best overload candidate to perform the requested
2297 conversion. This function is used for three the overloading situations
2298 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2299 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2300 per [dcl.init.ref], so we ignore temporary bindings. */
2302 static struct z_candidate
*
2303 build_user_type_conversion_1 (totype
, expr
, flags
)
2307 struct z_candidate
*candidates
, *cand
;
2308 tree fromtype
= TREE_TYPE (expr
);
2309 tree ctors
= NULL_TREE
, convs
= NULL_TREE
, *p
;
2310 tree args
= NULL_TREE
;
2311 tree templates
= NULL_TREE
;
2313 if (IS_AGGR_TYPE (totype
))
2314 ctors
= lookup_fnfields (TYPE_BINFO (totype
),
2316 ? complete_ctor_identifier
2320 if (IS_AGGR_TYPE (fromtype
)
2321 && (! IS_AGGR_TYPE (totype
) || ! DERIVED_FROM_P (totype
, fromtype
)))
2322 convs
= lookup_conversions (fromtype
);
2325 flags
|= LOOKUP_NO_CONVERSION
;
2331 ctors
= TREE_VALUE (ctors
);
2333 t
= build_int_2 (0, 0);
2334 TREE_TYPE (t
) = build_pointer_type (totype
);
2335 args
= build_tree_list (NULL_TREE
, expr
);
2336 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors
)))
2337 args
= tree_cons (NULL_TREE
,
2338 in_charge_arg_for_name (complete_ctor_identifier
),
2340 args
= tree_cons (NULL_TREE
, t
, args
);
2342 for (; ctors
; ctors
= OVL_NEXT (ctors
))
2344 tree ctor
= OVL_CURRENT (ctors
);
2345 if (DECL_NONCONVERTING_P (ctor
))
2348 if (TREE_CODE (ctor
) == TEMPLATE_DECL
)
2350 templates
= tree_cons (NULL_TREE
, ctor
, templates
);
2352 add_template_candidate (candidates
, ctor
, totype
,
2353 NULL_TREE
, args
, NULL_TREE
, flags
,
2357 candidates
= add_function_candidate (candidates
, ctor
, totype
,
2362 candidates
->second_conv
= build1 (IDENTITY_CONV
, totype
, NULL_TREE
);
2363 candidates
->basetype_path
= TYPE_BINFO (totype
);
2368 args
= build_tree_list (NULL_TREE
, build_this (expr
));
2370 for (; convs
; convs
= TREE_CHAIN (convs
))
2372 tree fns
= TREE_VALUE (convs
);
2373 int convflags
= LOOKUP_NO_CONVERSION
;
2376 /* If we are called to convert to a reference type, we are trying to
2377 find an lvalue binding, so don't even consider temporaries. If
2378 we don't find an lvalue binding, the caller will try again to
2379 look for a temporary binding. */
2380 if (TREE_CODE (totype
) == REFERENCE_TYPE
)
2381 convflags
|= LOOKUP_NO_TEMP_BIND
;
2383 if (TREE_CODE (OVL_CURRENT (fns
)) != TEMPLATE_DECL
)
2384 ics
= implicit_conversion
2385 (totype
, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns
))), 0, convflags
);
2387 /* We can't compute this yet. */
2388 ics
= error_mark_node
;
2390 if (TREE_CODE (totype
) == REFERENCE_TYPE
&& ics
&& ICS_BAD_FLAG (ics
))
2391 /* ignore the near match. */;
2393 for (; fns
; fns
= OVL_NEXT (fns
))
2395 tree fn
= OVL_CURRENT (fns
);
2396 struct z_candidate
*old_candidates
= candidates
;
2398 /* [over.match.funcs] For conversion functions, the function is
2399 considered to be a member of the class of the implicit object
2400 argument for the purpose of defining the type of the implicit
2403 So we pass fromtype as CTYPE to add_*_candidate. */
2405 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2407 templates
= tree_cons (NULL_TREE
, fn
, templates
);
2409 add_template_candidate (candidates
, fn
, fromtype
, NULL_TREE
,
2410 args
, totype
, flags
,
2414 candidates
= add_function_candidate (candidates
, fn
, fromtype
,
2417 if (candidates
!= old_candidates
)
2419 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2420 ics
= implicit_conversion
2421 (totype
, TREE_TYPE (TREE_TYPE (candidates
->fn
)),
2424 candidates
->second_conv
= ics
;
2425 candidates
->basetype_path
= TYPE_BINFO (fromtype
);
2427 if (ics
== NULL_TREE
)
2428 candidates
->viable
= 0;
2429 else if (candidates
->viable
== 1 && ICS_BAD_FLAG (ics
))
2430 candidates
->viable
= -1;
2435 if (! any_viable (candidates
))
2438 if (flags
& LOOKUP_COMPLAIN
)
2440 if (candidates
&& ! candidates
->next
)
2441 /* say why this one won't work or try to be loose */;
2443 cp_error ("no viable candidates");
2450 candidates
= splice_viable (candidates
);
2451 cand
= tourney (candidates
);
2455 if (flags
& LOOKUP_COMPLAIN
)
2457 cp_error ("conversion from `%T' to `%T' is ambiguous",
2459 print_z_candidates (candidates
);
2462 cand
= candidates
; /* any one will do */
2463 cand
->second_conv
= build1 (AMBIG_CONV
, totype
, expr
);
2464 ICS_USER_FLAG (cand
->second_conv
) = 1;
2465 ICS_BAD_FLAG (cand
->second_conv
) = 1;
2470 for (p
= &(cand
->second_conv
); TREE_CODE (*p
) != IDENTITY_CONV
; )
2471 p
= &(TREE_OPERAND (*p
, 0));
2475 (DECL_CONSTRUCTOR_P (cand
->fn
)
2476 ? totype
: non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
)))),
2477 expr
, build_expr_ptr_wrapper (cand
));
2478 ICS_USER_FLAG (cand
->second_conv
) = 1;
2479 if (cand
->viable
== -1)
2480 ICS_BAD_FLAG (cand
->second_conv
) = 1;
2486 build_user_type_conversion (totype
, expr
, flags
)
2490 struct z_candidate
*cand
2491 = build_user_type_conversion_1 (totype
, expr
, flags
);
2495 if (TREE_CODE (cand
->second_conv
) == AMBIG_CONV
)
2496 return error_mark_node
;
2497 return convert_from_reference
2498 (convert_like_with_context
2499 (cand
->second_conv
, expr
, cand
->fn
, 0));
2504 /* Do any initial processing on the arguments to a function call. */
2511 for (t
= args
; t
; t
= TREE_CHAIN (t
))
2513 if (TREE_VALUE (t
) == error_mark_node
)
2514 return error_mark_node
;
2515 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t
))) == VOID_TYPE
)
2517 error ("invalid use of void expression");
2518 return error_mark_node
;
2520 else if (TREE_CODE (TREE_VALUE (t
)) == OFFSET_REF
)
2521 TREE_VALUE (t
) = resolve_offset_ref (TREE_VALUE (t
));
2527 build_new_function_call (fn
, args
)
2530 struct z_candidate
*candidates
= 0, *cand
;
2531 tree explicit_targs
= NULL_TREE
;
2532 int template_only
= 0;
2534 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2536 explicit_targs
= TREE_OPERAND (fn
, 1);
2537 fn
= TREE_OPERAND (fn
, 0);
2541 if (really_overloaded_fn (fn
))
2544 tree templates
= NULL_TREE
;
2546 args
= resolve_args (args
);
2548 if (args
== error_mark_node
)
2549 return error_mark_node
;
2551 for (t1
= fn
; t1
; t1
= OVL_CHAIN (t1
))
2553 tree t
= OVL_FUNCTION (t1
);
2555 if (TREE_CODE (t
) == TEMPLATE_DECL
)
2557 templates
= tree_cons (NULL_TREE
, t
, templates
);
2558 candidates
= add_template_candidate
2559 (candidates
, t
, NULL_TREE
, explicit_targs
, args
, NULL_TREE
,
2560 LOOKUP_NORMAL
, DEDUCE_CALL
);
2562 else if (! template_only
)
2563 candidates
= add_function_candidate
2564 (candidates
, t
, NULL_TREE
, args
, LOOKUP_NORMAL
);
2567 if (! any_viable (candidates
))
2569 if (candidates
&& ! candidates
->next
)
2570 return build_function_call (candidates
->fn
, args
);
2571 cp_error ("no matching function for call to `%D (%A)'",
2572 DECL_NAME (OVL_FUNCTION (fn
)), args
);
2574 print_z_candidates (candidates
);
2575 return error_mark_node
;
2577 candidates
= splice_viable (candidates
);
2578 cand
= tourney (candidates
);
2582 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2583 DECL_NAME (OVL_FUNCTION (fn
)), args
);
2584 print_z_candidates (candidates
);
2585 return error_mark_node
;
2588 return build_over_call (cand
, args
, LOOKUP_NORMAL
);
2591 /* This is not really overloaded. */
2592 fn
= OVL_CURRENT (fn
);
2594 return build_function_call (fn
, args
);
2598 build_object_call (obj
, args
)
2601 struct z_candidate
*candidates
= 0, *cand
;
2602 tree fns
, convs
, mem_args
= NULL_TREE
;
2603 tree type
= TREE_TYPE (obj
);
2605 if (TYPE_PTRMEMFUNC_P (type
))
2607 /* It's no good looking for an overloaded operator() on a
2608 pointer-to-member-function. */
2609 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj
);
2610 return error_mark_node
;
2613 fns
= lookup_fnfields (TYPE_BINFO (type
), ansi_opname (CALL_EXPR
), 1);
2614 if (fns
== error_mark_node
)
2615 return error_mark_node
;
2617 args
= resolve_args (args
);
2619 if (args
== error_mark_node
)
2620 return error_mark_node
;
2624 tree base
= BINFO_TYPE (TREE_PURPOSE (fns
));
2625 mem_args
= tree_cons (NULL_TREE
, build_this (obj
), args
);
2627 for (fns
= TREE_VALUE (fns
); fns
; fns
= OVL_NEXT (fns
))
2629 tree fn
= OVL_CURRENT (fns
);
2630 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2633 = add_template_candidate (candidates
, fn
, base
, NULL_TREE
,
2634 mem_args
, NULL_TREE
,
2635 LOOKUP_NORMAL
, DEDUCE_CALL
);
2638 candidates
= add_function_candidate
2639 (candidates
, fn
, base
, mem_args
, LOOKUP_NORMAL
);
2642 candidates
->basetype_path
= TYPE_BINFO (type
);
2646 convs
= lookup_conversions (type
);
2648 for (; convs
; convs
= TREE_CHAIN (convs
))
2650 tree fns
= TREE_VALUE (convs
);
2651 tree totype
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns
)));
2653 if ((TREE_CODE (totype
) == POINTER_TYPE
2654 || TREE_CODE (totype
) == REFERENCE_TYPE
)
2655 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
2656 for (; fns
; fns
= OVL_NEXT (fns
))
2658 tree fn
= OVL_CURRENT (fns
);
2659 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2661 candidates
= add_template_conv_candidate (candidates
,
2668 candidates
= add_conv_candidate (candidates
, fn
, obj
, args
);
2672 if (! any_viable (candidates
))
2674 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj
), args
);
2675 print_z_candidates (candidates
);
2676 return error_mark_node
;
2679 candidates
= splice_viable (candidates
);
2680 cand
= tourney (candidates
);
2684 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj
), args
);
2685 print_z_candidates (candidates
);
2686 return error_mark_node
;
2689 /* Since cand->fn will be a type, not a function, for a conversion
2690 function, we must be careful not to unconditionally look at
2692 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
2693 && DECL_OVERLOADED_OPERATOR_P (cand
->fn
) == CALL_EXPR
)
2694 return build_over_call (cand
, mem_args
, LOOKUP_NORMAL
);
2696 obj
= convert_like_with_context
2697 (TREE_VEC_ELT (cand
->convs
, 0), obj
, cand
->fn
, -1);
2700 return build_function_call (obj
, args
);
2704 op_error (code
, code2
, arg1
, arg2
, arg3
, problem
)
2705 enum tree_code code
, code2
;
2706 tree arg1
, arg2
, arg3
;
2707 const char *problem
;
2709 const char * opname
;
2711 if (code
== MODIFY_EXPR
)
2712 opname
= assignment_operator_name_info
[code2
].name
;
2714 opname
= operator_name_info
[code
].name
;
2719 cp_error ("%s for `%T ? %T : %T'", problem
,
2720 error_type (arg1
), error_type (arg2
), error_type (arg3
));
2722 case POSTINCREMENT_EXPR
:
2723 case POSTDECREMENT_EXPR
:
2724 cp_error ("%s for `%T%s'", problem
, error_type (arg1
), opname
);
2727 cp_error ("%s for `%T[%T]'", problem
,
2728 error_type (arg1
), error_type (arg2
));
2732 cp_error ("%s for `%T %s %T'", problem
,
2733 error_type (arg1
), opname
, error_type (arg2
));
2735 cp_error ("%s for `%s%T'", problem
, opname
, error_type (arg1
));
2739 /* Return the implicit conversion sequence that could be used to
2740 convert E1 to E2 in [expr.cond]. */
2743 conditional_conversion (e1
, e2
)
2747 tree t1
= non_reference (TREE_TYPE (e1
));
2748 tree t2
= non_reference (TREE_TYPE (e2
));
2753 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2754 implicitly converted (clause _conv_) to the type "reference to
2755 T2", subject to the constraint that in the conversion the
2756 reference must bind directly (_dcl.init.ref_) to E1. */
2757 if (real_lvalue_p (e2
))
2759 conv
= implicit_conversion (build_reference_type (t2
),
2762 LOOKUP_NO_TEMP_BIND
);
2769 If E1 and E2 have class type, and the underlying class types are
2770 the same or one is a base class of the other: E1 can be converted
2771 to match E2 if the class of T2 is the same type as, or a base
2772 class of, the class of T1, and the cv-qualification of T2 is the
2773 same cv-qualification as, or a greater cv-qualification than, the
2774 cv-qualification of T1. If the conversion is applied, E1 is
2775 changed to an rvalue of type T2 that still refers to the original
2776 source class object (or the appropriate subobject thereof). */
2777 if (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
2778 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2
),
2779 TYPE_MAIN_VARIANT (t1
)))
2781 if (at_least_as_qualified_p (t2
, t1
))
2783 conv
= build1 (IDENTITY_CONV
, t1
, e1
);
2784 if (!same_type_p (TYPE_MAIN_VARIANT (t1
),
2785 TYPE_MAIN_VARIANT (t2
)))
2786 conv
= build_conv (BASE_CONV
, t2
, conv
);
2795 E1 can be converted to match E2 if E1 can be implicitly converted
2796 to the type that expression E2 would have if E2 were converted to
2797 an rvalue (or the type it has, if E2 is an rvalue). */
2798 return implicit_conversion (t2
, t1
, e1
, LOOKUP_NORMAL
);
2801 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2802 arguments to the conditional expression. By the time this function
2803 is called, any suitable candidate functions are included in
2807 build_conditional_expr (arg1
, arg2
, arg3
)
2815 tree result_type
= NULL_TREE
;
2817 struct z_candidate
*candidates
= 0;
2818 struct z_candidate
*cand
;
2820 /* As a G++ extension, the second argument to the conditional can be
2821 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2822 c'.) If the second operand is omitted, make sure it is
2823 calculated only once. */
2827 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2828 arg1
= arg2
= save_expr (arg1
);
2833 The first expr ession is implicitly converted to bool (clause
2835 arg1
= cp_convert (boolean_type_node
, arg1
);
2837 /* If something has already gone wrong, just pass that fact up the
2839 if (arg1
== error_mark_node
2840 || arg2
== error_mark_node
2841 || arg3
== error_mark_node
2842 || TREE_TYPE (arg1
) == error_mark_node
2843 || TREE_TYPE (arg2
) == error_mark_node
2844 || TREE_TYPE (arg3
) == error_mark_node
)
2845 return error_mark_node
;
2847 /* Convert from reference types to ordinary types; no expressions
2848 really have reference type in C++. */
2849 arg2
= convert_from_reference (arg2
);
2850 arg3
= convert_from_reference (arg3
);
2854 If either the second or the third operand has type (possibly
2855 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2856 array-to-pointer (_conv.array_), and function-to-pointer
2857 (_conv.func_) standard conversions are performed on the second
2858 and third operands. */
2859 arg2_type
= TREE_TYPE (arg2
);
2860 arg3_type
= TREE_TYPE (arg3
);
2861 if (VOID_TYPE_P (arg2_type
) || VOID_TYPE_P (arg3_type
))
2863 /* Do the conversions. We don't these for `void' type arguments
2864 since it can't have any effect and since decay_conversion
2865 does not handle that case gracefully. */
2866 if (!VOID_TYPE_P (arg2_type
))
2867 arg2
= decay_conversion (arg2
);
2868 if (!VOID_TYPE_P (arg3_type
))
2869 arg3
= decay_conversion (arg3
);
2870 arg2_type
= TREE_TYPE (arg2
);
2871 arg3_type
= TREE_TYPE (arg3
);
2875 One of the following shall hold:
2877 --The second or the third operand (but not both) is a
2878 throw-expression (_except.throw_); the result is of the
2879 type of the other and is an rvalue.
2881 --Both the second and the third operands have type void; the
2882 result is of type void and is an rvalue. */
2883 if ((TREE_CODE (arg2
) == THROW_EXPR
)
2884 ^ (TREE_CODE (arg3
) == THROW_EXPR
))
2885 result_type
= ((TREE_CODE (arg2
) == THROW_EXPR
)
2886 ? arg3_type
: arg2_type
);
2887 else if (VOID_TYPE_P (arg2_type
) && VOID_TYPE_P (arg3_type
))
2888 result_type
= void_type_node
;
2891 cp_error ("`%E' has type `void' and is not a throw-expression",
2892 VOID_TYPE_P (arg2_type
) ? arg2
: arg3
);
2893 return error_mark_node
;
2897 goto valid_operands
;
2901 Otherwise, if the second and third operand have different types,
2902 and either has (possibly cv-qualified) class type, an attempt is
2903 made to convert each of those operands to the type of the other. */
2904 else if (!same_type_p (arg2_type
, arg3_type
)
2905 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
2907 tree conv2
= conditional_conversion (arg2
, arg3
);
2908 tree conv3
= conditional_conversion (arg3
, arg2
);
2912 If both can be converted, or one can be converted but the
2913 conversion is ambiguous, the program is ill-formed. If
2914 neither can be converted, the operands are left unchanged and
2915 further checking is performed as described below. If exactly
2916 one conversion is possible, that conversion is applied to the
2917 chosen operand and the converted operand is used in place of
2918 the original operand for the remainder of this section. */
2919 if ((conv2
&& !ICS_BAD_FLAG (conv2
)
2920 && conv3
&& !ICS_BAD_FLAG (conv3
))
2921 || (conv2
&& TREE_CODE (conv2
) == AMBIG_CONV
)
2922 || (conv3
&& TREE_CODE (conv3
) == AMBIG_CONV
))
2924 cp_error ("operands to ?: have different types");
2925 return error_mark_node
;
2927 else if (conv2
&& !ICS_BAD_FLAG (conv2
))
2929 arg2
= convert_like (conv2
, arg2
);
2930 arg2
= convert_from_reference (arg2
);
2931 /* That may not quite have done the trick. If the two types
2932 are cv-qualified variants of one another, we will have
2933 just used an IDENTITY_CONV. (There's no conversion from
2934 an lvalue of one class type to an lvalue of another type,
2935 even a cv-qualified variant, and we don't want to lose
2936 lvalue-ness here.) So, we manually add a NOP_EXPR here
2938 if (!same_type_p (TREE_TYPE (arg2
), arg3_type
))
2939 arg2
= build1 (NOP_EXPR
, arg3_type
, arg2
);
2940 arg2_type
= TREE_TYPE (arg2
);
2942 else if (conv3
&& !ICS_BAD_FLAG (conv3
))
2944 arg3
= convert_like (conv3
, arg3
);
2945 arg3
= convert_from_reference (arg3
);
2946 if (!same_type_p (TREE_TYPE (arg3
), arg2_type
))
2947 arg3
= build1 (NOP_EXPR
, arg2_type
, arg3
);
2948 arg3_type
= TREE_TYPE (arg3
);
2954 If the second and third operands are lvalues and have the same
2955 type, the result is of that type and is an lvalue. */
2956 if (real_lvalue_p (arg2
) && real_lvalue_p (arg3
) &&
2957 same_type_p (arg2_type
, arg3_type
))
2959 result_type
= arg2_type
;
2960 goto valid_operands
;
2965 Otherwise, the result is an rvalue. If the second and third
2966 operand do not have the same type, and either has (possibly
2967 cv-qualified) class type, overload resolution is used to
2968 determine the conversions (if any) to be applied to the operands
2969 (_over.match.oper_, _over.built_). */
2971 if (!same_type_p (arg2_type
, arg3_type
)
2972 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
2977 /* Rearrange the arguments so that add_builtin_candidate only has
2978 to know about two args. In build_builtin_candidates, the
2979 arguments are unscrambled. */
2983 candidates
= add_builtin_candidates (candidates
,
2986 ansi_opname (COND_EXPR
),
2992 If the overload resolution fails, the program is
2994 if (!any_viable (candidates
))
2996 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
2997 print_z_candidates (candidates
);
2998 return error_mark_node
;
3000 candidates
= splice_viable (candidates
);
3001 cand
= tourney (candidates
);
3004 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
3005 print_z_candidates (candidates
);
3006 return error_mark_node
;
3011 Otherwise, the conversions thus determined are applied, and
3012 the converted operands are used in place of the original
3013 operands for the remainder of this section. */
3014 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3015 arg1
= convert_like (conv
, arg1
);
3016 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3017 arg2
= convert_like (conv
, arg2
);
3018 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3019 arg3
= convert_like (conv
, arg3
);
3024 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3025 and function-to-pointer (_conv.func_) standard conversions are
3026 performed on the second and third operands.
3028 We need to force the lvalue-to-rvalue conversion here for class types,
3029 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3030 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3033 if (IS_AGGR_TYPE (TREE_TYPE (arg2
)) && real_lvalue_p (arg2
))
3034 arg2
= build_user_type_conversion (TREE_TYPE (arg2
), arg2
, LOOKUP_NORMAL
);
3036 arg2
= decay_conversion (arg2
);
3037 arg2_type
= TREE_TYPE (arg2
);
3039 if (IS_AGGR_TYPE (TREE_TYPE (arg3
)) && real_lvalue_p (arg3
))
3040 arg3
= build_user_type_conversion (TREE_TYPE (arg3
), arg3
, LOOKUP_NORMAL
);
3042 arg3
= decay_conversion (arg3
);
3043 arg3_type
= TREE_TYPE (arg3
);
3047 After those conversions, one of the following shall hold:
3049 --The second and third operands have the same type; the result is of
3051 if (same_type_p (arg2_type
, arg3_type
))
3052 result_type
= arg2_type
;
3055 --The second and third operands have arithmetic or enumeration
3056 type; the usual arithmetic conversions are performed to bring
3057 them to a common type, and the result is of that type. */
3058 else if ((ARITHMETIC_TYPE_P (arg2_type
)
3059 || TREE_CODE (arg2_type
) == ENUMERAL_TYPE
)
3060 && (ARITHMETIC_TYPE_P (arg3_type
)
3061 || TREE_CODE (arg3_type
) == ENUMERAL_TYPE
))
3063 /* In this case, there is always a common type. */
3064 result_type
= type_after_usual_arithmetic_conversions (arg2_type
,
3067 if (TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3068 && TREE_CODE (arg3_type
) == ENUMERAL_TYPE
)
3069 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3070 arg2_type
, arg3_type
);
3071 else if (extra_warnings
3072 && ((TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3073 && !same_type_p (arg3_type
, type_promotes_to (arg2_type
)))
3074 || (TREE_CODE (arg3_type
) == ENUMERAL_TYPE
3075 && !same_type_p (arg2_type
, type_promotes_to (arg3_type
)))))
3076 cp_warning ("enumeral and non-enumeral type in conditional expression");
3078 arg2
= perform_implicit_conversion (result_type
, arg2
);
3079 arg3
= perform_implicit_conversion (result_type
, arg3
);
3083 --The second and third operands have pointer type, or one has
3084 pointer type and the other is a null pointer constant; pointer
3085 conversions (_conv.ptr_) and qualification conversions
3086 (_conv.qual_) are performed to bring them to their composite
3087 pointer type (_expr.rel_). The result is of the composite
3090 --The second and third operands have pointer to member type, or
3091 one has pointer to member type and the other is a null pointer
3092 constant; pointer to member conversions (_conv.mem_) and
3093 qualification conversions (_conv.qual_) are performed to bring
3094 them to a common type, whose cv-qualification shall match the
3095 cv-qualification of either the second or the third operand.
3096 The result is of the common type. */
3097 else if ((null_ptr_cst_p (arg2
)
3098 && (TYPE_PTR_P (arg3_type
) || TYPE_PTRMEM_P (arg3_type
)
3099 || TYPE_PTRMEMFUNC_P (arg3_type
)))
3100 || (null_ptr_cst_p (arg3
)
3101 && (TYPE_PTR_P (arg2_type
) || TYPE_PTRMEM_P (arg2_type
)
3102 || TYPE_PTRMEMFUNC_P (arg2_type
)))
3103 || (TYPE_PTR_P (arg2_type
) && TYPE_PTR_P (arg3_type
))
3104 || (TYPE_PTRMEM_P (arg2_type
) && TYPE_PTRMEM_P (arg3_type
))
3105 || (TYPE_PTRMEMFUNC_P (arg2_type
)
3106 && TYPE_PTRMEMFUNC_P (arg3_type
)))
3108 result_type
= composite_pointer_type (arg2_type
, arg3_type
, arg2
,
3109 arg3
, "conditional expression");
3110 arg2
= perform_implicit_conversion (result_type
, arg2
);
3111 arg3
= perform_implicit_conversion (result_type
, arg3
);
3116 cp_error ("operands to ?: have different types");
3117 return error_mark_node
;
3121 result
= fold (build (COND_EXPR
, result_type
, arg1
, arg2
, arg3
));
3122 /* Expand both sides into the same slot, hopefully the target of the
3123 ?: expression. We used to check for TARGET_EXPRs here, but now we
3124 sometimes wrap them in NOP_EXPRs so the test would fail. */
3125 if (!lvalue_p
&& IS_AGGR_TYPE (result_type
))
3126 result
= build_target_expr_with_type (result
, result_type
);
3128 /* If this expression is an rvalue, but might be mistaken for an
3129 lvalue, we must add a NON_LVALUE_EXPR. */
3130 if (!lvalue_p
&& real_lvalue_p (result
))
3131 result
= build1 (NON_LVALUE_EXPR
, result_type
, result
);
3137 build_new_op (code
, flags
, arg1
, arg2
, arg3
)
3138 enum tree_code code
;
3140 tree arg1
, arg2
, arg3
;
3142 struct z_candidate
*candidates
= 0, *cand
;
3143 tree fns
, mem_arglist
= NULL_TREE
, arglist
, fnname
;
3144 enum tree_code code2
= NOP_EXPR
;
3145 tree templates
= NULL_TREE
;
3148 if (arg1
== error_mark_node
3149 || arg2
== error_mark_node
3150 || arg3
== error_mark_node
)
3151 return error_mark_node
;
3153 /* This can happen if a template takes all non-type parameters, e.g.
3154 undeclared_template<1, 5, 72>a; */
3155 if (code
== LT_EXPR
&& TREE_CODE (arg1
) == TEMPLATE_DECL
)
3157 cp_error ("`%D' must be declared before use", arg1
);
3158 return error_mark_node
;
3161 if (code
== MODIFY_EXPR
)
3163 code2
= TREE_CODE (arg3
);
3165 fnname
= ansi_assopname (code2
);
3168 fnname
= ansi_opname (code
);
3174 case VEC_DELETE_EXPR
:
3176 /* Use build_op_new_call and build_op_delete_call instead. */
3177 my_friendly_abort (981018);
3180 return build_object_call (arg1
, arg2
);
3186 /* The comma operator can have void args. */
3187 if (TREE_CODE (arg1
) == OFFSET_REF
)
3188 arg1
= resolve_offset_ref (arg1
);
3189 if (arg2
&& TREE_CODE (arg2
) == OFFSET_REF
)
3190 arg2
= resolve_offset_ref (arg2
);
3191 if (arg3
&& TREE_CODE (arg3
) == OFFSET_REF
)
3192 arg3
= resolve_offset_ref (arg3
);
3194 if (code
== COND_EXPR
)
3196 if (arg2
== NULL_TREE
3197 || TREE_CODE (TREE_TYPE (arg2
)) == VOID_TYPE
3198 || TREE_CODE (TREE_TYPE (arg3
)) == VOID_TYPE
3199 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))
3200 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3
))))
3203 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1
))
3204 && (! arg2
|| ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))))
3207 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
3208 arg2
= integer_zero_node
;
3211 arglist
= tree_cons (NULL_TREE
, arg1
, tree_cons
3212 (NULL_TREE
, arg2
, build_tree_list (NULL_TREE
, arg3
)));
3214 arglist
= tree_cons (NULL_TREE
, arg1
, build_tree_list (NULL_TREE
, arg2
));
3216 arglist
= build_tree_list (NULL_TREE
, arg1
);
3218 fns
= lookup_function_nonclass (fnname
, arglist
);
3220 if (fns
&& TREE_CODE (fns
) == TREE_LIST
)
3221 fns
= TREE_VALUE (fns
);
3222 for (; fns
; fns
= OVL_NEXT (fns
))
3224 tree fn
= OVL_CURRENT (fns
);
3225 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3227 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3229 = add_template_candidate (candidates
, fn
, NULL_TREE
, NULL_TREE
,
3230 arglist
, TREE_TYPE (fnname
),
3231 flags
, DEDUCE_CALL
);
3234 candidates
= add_function_candidate (candidates
, fn
, NULL_TREE
,
3238 if (IS_AGGR_TYPE (TREE_TYPE (arg1
)))
3240 fns
= lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1
)), fnname
, 1);
3241 if (fns
== error_mark_node
)
3249 tree basetype
= BINFO_TYPE (TREE_PURPOSE (fns
));
3250 mem_arglist
= tree_cons (NULL_TREE
, build_this (arg1
), TREE_CHAIN (arglist
));
3251 for (fns
= TREE_VALUE (fns
); fns
; fns
= OVL_NEXT (fns
))
3253 tree fn
= OVL_CURRENT (fns
);
3256 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
3257 this_arglist
= mem_arglist
;
3259 this_arglist
= arglist
;
3261 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3263 /* A member template. */
3264 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3266 = add_template_candidate (candidates
, fn
, basetype
, NULL_TREE
,
3267 this_arglist
, TREE_TYPE (fnname
),
3268 flags
, DEDUCE_CALL
);
3271 candidates
= add_function_candidate
3272 (candidates
, fn
, basetype
, this_arglist
, flags
);
3275 candidates
->basetype_path
= TYPE_BINFO (TREE_TYPE (arg1
));
3282 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3283 to know about two args; a builtin candidate will always have a first
3284 parameter of type bool. We'll handle that in
3285 build_builtin_candidate. */
3286 if (code
== COND_EXPR
)
3296 args
[2] = NULL_TREE
;
3299 candidates
= add_builtin_candidates
3300 (candidates
, code
, code2
, fnname
, args
, flags
);
3303 if (! any_viable (candidates
))
3307 case POSTINCREMENT_EXPR
:
3308 case POSTDECREMENT_EXPR
:
3309 /* Look for an `operator++ (int)'. If they didn't have
3310 one, then we fall back to the old way of doing things. */
3311 if (flags
& LOOKUP_COMPLAIN
)
3312 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3314 operator_name_info
[code
].name
);
3315 if (code
== POSTINCREMENT_EXPR
)
3316 code
= PREINCREMENT_EXPR
;
3318 code
= PREDECREMENT_EXPR
;
3319 return build_new_op (code
, flags
, arg1
, NULL_TREE
, NULL_TREE
);
3321 /* The caller will deal with these. */
3330 if (flags
& LOOKUP_COMPLAIN
)
3332 op_error (code
, code2
, arg1
, arg2
, arg3
, "no match");
3333 print_z_candidates (candidates
);
3335 return error_mark_node
;
3337 candidates
= splice_viable (candidates
);
3338 cand
= tourney (candidates
);
3342 if (flags
& LOOKUP_COMPLAIN
)
3344 op_error (code
, code2
, arg1
, arg2
, arg3
, "ambiguous overload");
3345 print_z_candidates (candidates
);
3347 return error_mark_node
;
3350 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
3352 extern int warn_synth
;
3354 && fnname
== ansi_assopname (NOP_EXPR
)
3355 && DECL_ARTIFICIAL (cand
->fn
)
3357 && ! candidates
->next
->next
)
3359 cp_warning ("using synthesized `%#D' for copy assignment",
3361 cp_warning_at (" where cfront would use `%#D'",
3363 ? candidates
->next
->fn
3367 return build_over_call
3369 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
3370 ? mem_arglist
: arglist
,
3374 /* Check for comparison of different enum types. */
3383 if (TREE_CODE (TREE_TYPE (arg1
)) == ENUMERAL_TYPE
3384 && TREE_CODE (TREE_TYPE (arg2
)) == ENUMERAL_TYPE
3385 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
))
3386 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2
))))
3388 cp_warning ("comparison between `%#T' and `%#T'",
3389 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
3396 /* We need to strip any leading REF_BIND so that bitfields don't cause
3397 errors. This should not remove any important conversions, because
3398 builtins don't apply to class objects directly. */
3399 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3400 if (TREE_CODE (conv
) == REF_BIND
)
3401 conv
= TREE_OPERAND (conv
, 0);
3402 arg1
= convert_like (conv
, arg1
);
3405 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3406 if (TREE_CODE (conv
) == REF_BIND
)
3407 conv
= TREE_OPERAND (conv
, 0);
3408 arg2
= convert_like (conv
, arg2
);
3412 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3413 if (TREE_CODE (conv
) == REF_BIND
)
3414 conv
= TREE_OPERAND (conv
, 0);
3415 arg3
= convert_like (conv
, arg3
);
3422 return build_modify_expr (arg1
, code2
, arg2
);
3425 return build_indirect_ref (arg1
, "unary *");
3430 case TRUNC_DIV_EXPR
:
3441 case TRUNC_MOD_EXPR
:
3445 case TRUTH_ANDIF_EXPR
:
3446 case TRUTH_ORIF_EXPR
:
3447 return build_binary_op (code
, arg1
, arg2
);
3452 case TRUTH_NOT_EXPR
:
3453 case PREINCREMENT_EXPR
:
3454 case POSTINCREMENT_EXPR
:
3455 case PREDECREMENT_EXPR
:
3456 case POSTDECREMENT_EXPR
:
3459 return build_unary_op (code
, arg1
, candidates
!= 0);
3462 return build_array_ref (arg1
, arg2
);
3465 return build_conditional_expr (arg1
, arg2
, arg3
);
3468 return build_m_component_ref
3469 (build_indirect_ref (arg1
, NULL_PTR
), arg2
);
3471 /* The caller will deal with these. */
3478 my_friendly_abort (367);
3483 /* Build a call to operator delete. This has to be handled very specially,
3484 because the restrictions on what signatures match are different from all
3485 other call instances. For a normal delete, only a delete taking (void *)
3486 or (void *, size_t) is accepted. For a placement delete, only an exact
3487 match with the placement new is accepted.
3489 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3490 ADDR is the pointer to be deleted. For placement delete, it is also
3491 used to determine what the corresponding new looked like.
3492 SIZE is the size of the memory block to be deleted.
3493 FLAGS are the usual overloading flags.
3494 PLACEMENT is the corresponding placement new call, or 0. */
3497 build_op_delete_call (code
, addr
, size
, flags
, placement
)
3498 enum tree_code code
;
3499 tree addr
, size
, placement
;
3502 tree fn
, fns
, fnname
, fntype
, argtypes
, args
, type
;
3504 if (addr
== error_mark_node
)
3505 return error_mark_node
;
3507 type
= TREE_TYPE (TREE_TYPE (addr
));
3508 fnname
= ansi_opname (code
);
3510 if (IS_AGGR_TYPE (type
) && ! (flags
& LOOKUP_GLOBAL
))
3513 If the result of the lookup is ambiguous or inaccessible, or if
3514 the lookup selects a placement deallocation function, the
3515 program is ill-formed.
3517 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3519 fns
= lookup_fnfields (TYPE_BINFO (type
), fnname
, 1);
3520 if (fns
== error_mark_node
)
3521 return error_mark_node
;
3526 if (fns
== NULL_TREE
)
3527 fns
= lookup_name_nonclass (fnname
);
3531 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3533 /* Extract the function. */
3534 argtypes
= TREE_OPERAND (TREE_OPERAND (placement
, 0), 0);
3535 /* Then the second parm type. */
3536 argtypes
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes
)));
3538 /* Also the second argument. */
3539 args
= TREE_CHAIN (TREE_OPERAND (placement
, 1));
3543 /* First try it without the size argument. */
3544 argtypes
= void_list_node
;
3548 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
, argtypes
);
3549 fntype
= build_function_type (void_type_node
, argtypes
);
3551 /* Strip const and volatile from addr. */
3552 if (type
!= TYPE_MAIN_VARIANT (type
))
3553 addr
= cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type
)), addr
);
3555 fn
= instantiate_type (fntype
, fns
, 2);
3557 if (fn
!= error_mark_node
)
3559 if (TREE_CODE (fns
) == TREE_LIST
)
3560 /* Member functions. */
3561 enforce_access (type
, fn
);
3562 return build_function_call (fn
, tree_cons (NULL_TREE
, addr
, args
));
3565 /* If we are doing placement delete we do nothing if we don't find a
3566 matching op delete. */
3570 /* Normal delete; now try to find a match including the size argument. */
3571 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
,
3572 tree_cons (NULL_TREE
, sizetype
, void_list_node
));
3573 fntype
= build_function_type (void_type_node
, argtypes
);
3575 fn
= instantiate_type (fntype
, fns
, 2);
3577 if (fn
!= error_mark_node
)
3579 if (BASELINK_P (fns
))
3580 /* Member functions. */
3581 enforce_access (type
, fn
);
3582 return build_function_call
3583 (fn
, tree_cons (NULL_TREE
, addr
,
3584 build_tree_list (NULL_TREE
, size
)));
3587 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3588 destructor, in which case the error should be deferred
3589 until someone actually tries to delete one of these. */
3590 if (flags
& LOOKUP_SPECULATIVELY
)
3593 cp_error ("no suitable `operator delete' for `%T'", type
);
3594 return error_mark_node
;
3597 /* If the current scope isn't allowed to access DECL along
3598 BASETYPE_PATH, give an error. The most derived class in
3599 BASETYPE_PATH is the one used to qualify DECL. */
3602 enforce_access (basetype_path
, decl
)
3608 accessible
= accessible_p (basetype_path
, decl
);
3611 if (TREE_PRIVATE (decl
))
3612 cp_error_at ("`%+#D' is private", decl
);
3613 else if (TREE_PROTECTED (decl
))
3614 cp_error_at ("`%+#D' is protected", decl
);
3616 cp_error_at ("`%+#D' is inaccessible", decl
);
3617 cp_error ("within this context");
3624 /* Perform the conversions in CONVS on the expression EXPR.
3625 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3626 indicates the `this' argument of a method. INNER is non-zero when
3627 being called to continue a conversion chain. */
3630 convert_like_real (convs
, expr
, fn
, argnum
, inner
)
3636 if (ICS_BAD_FLAG (convs
)
3637 && TREE_CODE (convs
) != USER_CONV
3638 && TREE_CODE (convs
) != AMBIG_CONV
3639 && TREE_CODE (convs
) != REF_BIND
)
3642 for (; t
; t
= TREE_OPERAND (t
, 0))
3644 if (TREE_CODE (t
) == USER_CONV
)
3646 expr
= convert_like_real (t
, expr
, fn
, argnum
, 1);
3649 else if (TREE_CODE (t
) == AMBIG_CONV
)
3650 return convert_like_real (t
, expr
, fn
, argnum
, 1);
3651 else if (TREE_CODE (t
) == IDENTITY_CONV
)
3654 return convert_for_initialization
3655 (NULL_TREE
, TREE_TYPE (convs
), expr
, LOOKUP_NORMAL
,
3656 "conversion", fn
, argnum
);
3660 expr
= dubious_conversion_warnings
3661 (TREE_TYPE (convs
), expr
, "argument", fn
, argnum
);
3662 switch (TREE_CODE (convs
))
3666 struct z_candidate
*cand
3667 = WRAPPER_PTR (TREE_OPERAND (convs
, 1));
3671 if (DECL_CONSTRUCTOR_P (fn
))
3673 tree t
= build_int_2 (0, 0);
3674 TREE_TYPE (t
) = build_pointer_type (DECL_CONTEXT (fn
));
3676 args
= build_tree_list (NULL_TREE
, expr
);
3677 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
3678 args
= tree_cons (NULL_TREE
, integer_one_node
, args
);
3679 args
= tree_cons (NULL_TREE
, t
, args
);
3682 args
= build_this (expr
);
3683 expr
= build_over_call (cand
, args
, LOOKUP_NORMAL
);
3685 /* If this is a constructor or a function returning an aggr type,
3686 we need to build up a TARGET_EXPR. */
3687 if (DECL_CONSTRUCTOR_P (fn
))
3688 expr
= build_cplus_new (TREE_TYPE (convs
), expr
);
3693 if (type_unknown_p (expr
))
3694 expr
= instantiate_type (TREE_TYPE (convs
), expr
, 1);
3697 /* Call build_user_type_conversion again for the error. */
3698 return build_user_type_conversion
3699 (TREE_TYPE (convs
), TREE_OPERAND (convs
, 0), LOOKUP_NORMAL
);
3705 expr
= convert_like_real (TREE_OPERAND (convs
, 0), expr
, fn
, argnum
, 1);
3706 if (expr
== error_mark_node
)
3707 return error_mark_node
;
3709 /* Convert a non-array constant variable to its underlying value, unless we
3710 are about to bind it to a reference, in which case we need to
3711 leave it as an lvalue. */
3712 if (TREE_CODE (convs
) != REF_BIND
3713 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
3714 expr
= decl_constant_value (expr
);
3716 switch (TREE_CODE (convs
))
3719 if (! IS_AGGR_TYPE (TREE_TYPE (convs
)))
3721 /* else fall through */
3723 if (TREE_CODE (convs
) == BASE_CONV
&& !NEED_TEMPORARY_P (convs
))
3725 /* We are going to bind a reference directly to a base-class
3726 subobject of EXPR. */
3727 tree base_ptr
= build_pointer_type (TREE_TYPE (convs
));
3729 /* Build an expression for `*((base*) &expr)'. */
3730 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
3731 expr
= perform_implicit_conversion (base_ptr
, expr
);
3732 expr
= build_indirect_ref (expr
, "implicit conversion");
3737 tree cvt_expr
= build_user_type_conversion
3738 (TREE_TYPE (convs
), expr
, LOOKUP_NORMAL
);
3741 /* This can occur if, for example, the EXPR has incomplete
3742 type. We can't check for that before attempting the
3743 conversion because the type might be an incomplete
3744 array type, which is OK if some constructor for the
3745 destination type takes a pointer argument. */
3746 if (!COMPLETE_TYPE_P (TREE_TYPE (expr
)))
3748 if (same_type_p (TREE_TYPE (expr
), TREE_TYPE (convs
)))
3749 incomplete_type_error (expr
, TREE_TYPE (expr
));
3751 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3752 expr
, TREE_TYPE (expr
), TREE_TYPE (convs
));
3755 cp_error ("could not convert `%E' to `%T'",
3756 expr
, TREE_TYPE (convs
));
3757 return error_mark_node
;
3764 tree ref_type
= TREE_TYPE (convs
);
3766 /* If necessary, create a temporary. */
3767 if (NEED_TEMPORARY_P (convs
))
3769 tree type
= TREE_TYPE (TREE_OPERAND (convs
, 0));
3770 expr
= build_target_expr_with_type (expr
, type
);
3773 /* Take the address of the thing to which we will bind the
3775 expr
= build_unary_op (ADDR_EXPR
, expr
, 1);
3776 if (expr
== error_mark_node
)
3777 return error_mark_node
;
3779 /* Convert it to a pointer to the type referred to by the
3780 reference. This will adjust the pointer if a derived to
3781 base conversion is being performed. */
3782 expr
= cp_convert (build_pointer_type (TREE_TYPE (ref_type
)),
3784 /* Convert the pointer to the desired reference type. */
3785 expr
= build1 (NOP_EXPR
, ref_type
, expr
);
3791 return decay_conversion (expr
);
3794 /* Warn about deprecated conversion if appropriate. */
3795 string_conv_p (TREE_TYPE (convs
), expr
, 1);
3801 return ocp_convert (TREE_TYPE (convs
), expr
, CONV_IMPLICIT
,
3802 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
3805 /* ARG is being passed to a varargs function. Perform any conversions
3806 required. Array/function to pointer decay must have already happened.
3807 Return the converted value. */
3810 convert_arg_to_ellipsis (arg
)
3813 if (TREE_CODE (TREE_TYPE (arg
)) == REAL_TYPE
3814 && (TYPE_PRECISION (TREE_TYPE (arg
))
3815 < TYPE_PRECISION (double_type_node
)))
3816 /* Convert `float' to `double'. */
3817 arg
= cp_convert (double_type_node
, arg
);
3819 /* Convert `short' and `char' to full-size `int'. */
3820 arg
= default_conversion (arg
);
3822 arg
= require_complete_type (arg
);
3824 if (arg
!= error_mark_node
&& ! pod_type_p (TREE_TYPE (arg
)))
3826 /* Undefined behaviour [expr.call] 5.2.2/7. */
3827 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3834 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3837 build_x_va_arg (expr
, type
)
3841 if (processing_template_decl
)
3842 return build_min (VA_ARG_EXPR
, type
, expr
);
3844 type
= complete_type_or_else (type
, NULL_TREE
);
3846 if (expr
== error_mark_node
|| !type
)
3847 return error_mark_node
;
3849 if (! pod_type_p (type
))
3851 /* Undefined behaviour [expr.call] 5.2.2/7. */
3852 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3856 return build_va_arg (expr
, type
);
3859 /* TYPE has been given to va_arg. Apply the default conversions which would
3860 have happened when passed via ellipsis. Return the promoted type, or
3861 NULL_TREE, if there is no change. */
3864 convert_type_from_ellipsis (type
)
3869 if (TREE_CODE (type
) == ARRAY_TYPE
)
3870 promote
= build_pointer_type (TREE_TYPE (type
));
3871 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
3872 promote
= build_pointer_type (type
);
3874 promote
= type_promotes_to (type
);
3876 return same_type_p (type
, promote
) ? NULL_TREE
: promote
;
3879 /* ARG is a default argument expression being passed to a parameter of
3880 the indicated TYPE, which is a parameter to FN. Do any required
3881 conversions. Return the converted value. */
3884 convert_default_arg (type
, arg
, fn
, parmnum
)
3890 if (fn
&& DECL_TEMPLATE_INFO (fn
))
3891 arg
= tsubst_default_argument (fn
, type
, arg
);
3893 arg
= break_out_target_exprs (arg
);
3895 if (TREE_CODE (arg
) == CONSTRUCTOR
)
3897 arg
= digest_init (type
, arg
, 0);
3898 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
3899 "default argument", fn
, parmnum
);
3903 /* This could get clobbered by the following call. */
3904 if (TREE_HAS_CONSTRUCTOR (arg
))
3905 arg
= copy_node (arg
);
3907 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
3908 "default argument", fn
, parmnum
);
3909 if (PROMOTE_PROTOTYPES
3910 && (TREE_CODE (type
) == INTEGER_TYPE
3911 || TREE_CODE (type
) == ENUMERAL_TYPE
)
3912 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3913 arg
= default_conversion (arg
);
3920 build_over_call (cand
, args
, flags
)
3921 struct z_candidate
*cand
;
3926 tree convs
= cand
->convs
;
3927 tree converted_args
= NULL_TREE
;
3928 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3929 tree conv
, arg
, val
;
3933 /* Give any warnings we noticed during overload resolution. */
3935 for (val
= cand
->warnings
; val
; val
= TREE_CHAIN (val
))
3936 joust (cand
, WRAPPER_PTR (TREE_VALUE (val
)), 1);
3938 if (DECL_FUNCTION_MEMBER_P (fn
))
3939 enforce_access (cand
->basetype_path
, fn
);
3941 if (args
&& TREE_CODE (args
) != TREE_LIST
)
3942 args
= build_tree_list (NULL_TREE
, args
);
3945 /* The implicit parameters to a constructor are not considered by overload
3946 resolution, and must be of the proper type. */
3947 if (DECL_CONSTRUCTOR_P (fn
))
3949 converted_args
= tree_cons (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
3950 arg
= TREE_CHAIN (arg
);
3951 parm
= TREE_CHAIN (parm
);
3952 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
3954 converted_args
= tree_cons
3955 (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
3956 arg
= TREE_CHAIN (arg
);
3957 parm
= TREE_CHAIN (parm
);
3960 /* Bypass access control for 'this' parameter. */
3961 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
3963 tree parmtype
= TREE_VALUE (parm
);
3964 tree argtype
= TREE_TYPE (TREE_VALUE (arg
));
3966 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs
, i
)))
3967 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3968 TREE_TYPE (argtype
), fn
);
3970 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3971 X is called for an object that is not of type X, or of a type
3972 derived from X, the behavior is undefined.
3974 So we can assume that anything passed as 'this' is non-null, and
3975 optimize accordingly. */
3976 my_friendly_assert (TREE_CODE (parmtype
) == POINTER_TYPE
, 19990811);
3977 t
= convert_pointer_to_real (TREE_TYPE (parmtype
), TREE_VALUE (arg
));
3978 converted_args
= tree_cons (NULL_TREE
, t
, converted_args
);
3979 parm
= TREE_CHAIN (parm
);
3980 arg
= TREE_CHAIN (arg
);
3986 parm
= TREE_CHAIN (parm
), arg
= TREE_CHAIN (arg
), ++i
)
3988 tree type
= TREE_VALUE (parm
);
3990 conv
= TREE_VEC_ELT (convs
, i
);
3991 if (ICS_BAD_FLAG (conv
))
3994 val
= TREE_VALUE (arg
);
3996 for (; t
; t
= TREE_OPERAND (t
, 0))
3998 if (TREE_CODE (t
) == USER_CONV
3999 || TREE_CODE (t
) == AMBIG_CONV
)
4001 val
= convert_like_with_context (t
, val
, fn
, i
- is_method
);
4004 else if (TREE_CODE (t
) == IDENTITY_CONV
)
4007 val
= convert_for_initialization
4008 (NULL_TREE
, type
, val
, LOOKUP_NORMAL
,
4009 "argument", fn
, i
- is_method
);
4013 val
= TREE_VALUE (arg
);
4014 val
= convert_like_with_context
4015 (conv
, TREE_VALUE (arg
), fn
, i
- is_method
);
4018 if (PROMOTE_PROTOTYPES
4019 && (TREE_CODE (type
) == INTEGER_TYPE
4020 || TREE_CODE (type
) == ENUMERAL_TYPE
)
4021 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
4022 val
= default_conversion (val
);
4023 converted_args
= tree_cons (NULL_TREE
, val
, converted_args
);
4026 /* Default arguments */
4027 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
), i
++)
4029 = tree_cons (NULL_TREE
,
4030 convert_default_arg (TREE_VALUE (parm
),
4031 TREE_PURPOSE (parm
),
4036 for (; arg
; arg
= TREE_CHAIN (arg
))
4038 = tree_cons (NULL_TREE
,
4039 convert_arg_to_ellipsis (TREE_VALUE (arg
)),
4042 converted_args
= nreverse (converted_args
);
4044 if (warn_format
&& (DECL_NAME (fn
) || DECL_ASSEMBLER_NAME (fn
)))
4045 check_function_format (DECL_NAME (fn
), DECL_ASSEMBLER_NAME (fn
),
4048 /* Avoid actually calling copy constructors and copy assignment operators,
4051 if (! flag_elide_constructors
)
4052 /* Do things the hard way. */;
4053 else if (TREE_VEC_LENGTH (convs
) == 1
4054 && DECL_COPY_CONSTRUCTOR_P (fn
))
4057 arg
= TREE_CHAIN (converted_args
);
4058 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
4059 arg
= TREE_CHAIN (arg
);
4060 arg
= TREE_VALUE (arg
);
4062 /* Pull out the real argument, disregarding const-correctness. */
4064 while (TREE_CODE (targ
) == NOP_EXPR
4065 || TREE_CODE (targ
) == NON_LVALUE_EXPR
4066 || TREE_CODE (targ
) == CONVERT_EXPR
)
4067 targ
= TREE_OPERAND (targ
, 0);
4068 if (TREE_CODE (targ
) == ADDR_EXPR
)
4070 targ
= TREE_OPERAND (targ
, 0);
4071 if (!same_type_ignoring_top_level_qualifiers_p
4072 (TREE_TYPE (TREE_TYPE (arg
)), TREE_TYPE (targ
)))
4081 arg
= build_indirect_ref (arg
, 0);
4083 /* [class.copy]: the copy constructor is implicitly defined even if
4084 the implementation elided its use. */
4085 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn
)))
4088 /* If we're creating a temp and we already have one, don't create a
4089 new one. If we're not creating a temp but we get one, use
4090 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4091 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4092 temp or an INIT_EXPR otherwise. */
4093 if (integer_zerop (TREE_VALUE (args
)))
4095 if (! real_lvalue_p (arg
))
4097 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4098 return build_target_expr_with_type (arg
, DECL_CONTEXT (fn
));
4100 else if (! real_lvalue_p (arg
)
4101 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4104 tree to
= stabilize_reference
4105 (build_indirect_ref (TREE_VALUE (args
), 0));
4107 /* If we're initializing an empty class, then we actually
4108 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4109 reason is that the dummy padding member in the target may
4110 not actually be allocated if TO is a base class
4111 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4112 padding, a MODIFY_EXPR will preserve its value, which is
4113 the right thing to do if it's not really padding at all.
4115 It's not safe to just throw away the ARG if we're looking
4116 at an empty class because the ARG might contain a
4117 TARGET_EXPR which wants to be bound to TO. If it is not,
4118 expand_expr will assign a dummy slot for the TARGET_EXPR,
4119 and we will call a destructor for it, which is wrong,
4120 because we will also destroy TO, but will never have
4122 val
= build (is_empty_class (DECL_CONTEXT (fn
))
4123 ? MODIFY_EXPR
: INIT_EXPR
,
4124 DECL_CONTEXT (fn
), to
, arg
);
4125 address
= build_unary_op (ADDR_EXPR
, val
, 0);
4126 /* Avoid a warning about this expression, if the address is
4128 TREE_USED (address
) = 1;
4132 else if (DECL_OVERLOADED_OPERATOR_P (fn
) == NOP_EXPR
4134 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn
)))
4136 tree to
= stabilize_reference
4137 (build_indirect_ref (TREE_VALUE (converted_args
), 0));
4139 arg
= build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args
)), 0);
4141 val
= build (MODIFY_EXPR
, TREE_TYPE (to
), to
, arg
);
4147 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
4149 tree t
, *p
= &TREE_VALUE (converted_args
);
4150 tree binfo
= get_binfo
4151 (DECL_VIRTUAL_CONTEXT (fn
), TREE_TYPE (TREE_TYPE (*p
)), 0);
4152 *p
= convert_pointer_to_real (binfo
, *p
);
4153 if (TREE_SIDE_EFFECTS (*p
))
4154 *p
= save_expr (*p
);
4155 t
= build_pointer_type (TREE_TYPE (fn
));
4156 fn
= build_vfn_ref (p
, build_indirect_ref (*p
, 0), DECL_VINDEX (fn
));
4159 else if (DECL_INLINE (fn
))
4160 fn
= inline_conversion (fn
);
4162 fn
= build_addr_func (fn
);
4164 /* Recognize certain built-in functions so we can make tree-codes
4165 other than CALL_EXPR. We do this when it enables fold-const.c
4166 to do something useful. */
4168 if (TREE_CODE (fn
) == ADDR_EXPR
4169 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
4170 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0)))
4173 exp
= expand_tree_builtin (TREE_OPERAND (fn
, 0), args
, converted_args
);
4178 fn
= build_call (fn
, converted_args
);
4179 if (TREE_CODE (TREE_TYPE (fn
)) == VOID_TYPE
)
4181 fn
= require_complete_type (fn
);
4182 if (IS_AGGR_TYPE (TREE_TYPE (fn
)))
4183 fn
= build_cplus_new (TREE_TYPE (fn
), fn
);
4184 return convert_from_reference (fn
);
4187 /* Returns the value to use for the in-charge parameter when making a
4188 call to a function with the indicated NAME. */
4191 in_charge_arg_for_name (name
)
4194 if (name
== base_ctor_identifier
4195 || name
== base_dtor_identifier
)
4196 return integer_zero_node
;
4197 else if (name
== complete_ctor_identifier
)
4198 return integer_one_node
;
4199 else if (name
== complete_dtor_identifier
)
4200 return integer_two_node
;
4201 else if (name
== deleting_dtor_identifier
)
4202 return integer_three_node
;
4204 /* This function should only be called with one of the names listed
4206 my_friendly_abort (20000411);
4211 build_new_method_call (instance
, name
, args
, basetype_path
, flags
)
4212 tree instance
, name
, args
, basetype_path
;
4215 struct z_candidate
*candidates
= 0, *cand
;
4216 tree explicit_targs
= NULL_TREE
;
4217 tree basetype
, mem_args
= NULL_TREE
, fns
, instance_ptr
;
4220 tree templates
= NULL_TREE
;
4221 int template_only
= 0;
4223 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
4225 explicit_targs
= TREE_OPERAND (name
, 1);
4226 name
= TREE_OPERAND (name
, 0);
4228 name
= DECL_NAME (name
);
4231 if (TREE_CODE (name
) == COMPONENT_REF
)
4232 name
= TREE_OPERAND (name
, 1);
4233 if (TREE_CODE (name
) == OVERLOAD
)
4234 name
= DECL_NAME (OVL_CURRENT (name
));
4241 args
= resolve_args (args
);
4243 if (args
== error_mark_node
)
4244 return error_mark_node
;
4246 if (instance
== NULL_TREE
)
4247 basetype
= BINFO_TYPE (basetype_path
);
4250 if (TREE_CODE (instance
) == OFFSET_REF
)
4251 instance
= resolve_offset_ref (instance
);
4252 if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
4253 instance
= convert_from_reference (instance
);
4254 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (instance
));
4256 /* XXX this should be handled before we get here. */
4257 if (! IS_AGGR_TYPE (basetype
))
4259 if ((flags
& LOOKUP_COMPLAIN
) && basetype
!= error_mark_node
)
4260 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4261 name
, instance
, basetype
);
4263 return error_mark_node
;
4267 if (basetype_path
== NULL_TREE
)
4268 basetype_path
= TYPE_BINFO (basetype
);
4272 instance_ptr
= build_this (instance
);
4274 if (! template_only
)
4276 /* XXX this should be handled before we get here. */
4277 fns
= build_field_call (basetype_path
, instance_ptr
, name
, args
);
4284 instance_ptr
= build_int_2 (0, 0);
4285 TREE_TYPE (instance_ptr
) = build_pointer_type (basetype
);
4288 /* Callers should explicitly indicate whether they want to construct
4289 the complete object or just the part without virtual bases. */
4290 my_friendly_assert (name
!= ctor_identifier
, 20000408);
4291 /* Similarly for destructors. */
4292 my_friendly_assert (name
!= dtor_identifier
, 20000408);
4294 if (IDENTIFIER_CTOR_OR_DTOR_P (name
))
4298 constructor_p
= (name
== complete_ctor_identifier
4299 || name
== base_ctor_identifier
);
4300 pretty_name
= (constructor_p
4301 ? constructor_name (basetype
) : dtor_identifier
);
4305 /* Add the in-charge parameter as an implicit first argument. */
4307 || TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
4308 args
= tree_cons (NULL_TREE
,
4309 in_charge_arg_for_name (name
),
4312 /* We want to call the normal constructor function under the
4314 name
= constructor_p
? ctor_identifier
: dtor_identifier
;
4316 /* If we're call a subobject constructor or destructor for a
4317 subobject that uses virtual base classes, then we need to
4318 pass down a pointer to a VTT for the subobject. */
4319 else if ((name
== base_ctor_identifier
4320 || name
== base_dtor_identifier
)
4321 && TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
4326 /* If the current function is a complete object constructor
4327 or destructor, then we fetch the VTT directly.
4328 Otherwise, we look it up using the VTT we were given. */
4329 vtt
= IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type
));
4330 vtt
= build_unary_op (ADDR_EXPR
, vtt
, /*noconvert=*/1);
4331 vtt
= build (COND_EXPR
, TREE_TYPE (vtt
),
4332 DECL_USE_VTT_PARM (current_function_decl
),
4333 DECL_VTT_PARM (current_function_decl
),
4336 sub_vtt
= build (PLUS_EXPR
, TREE_TYPE (vtt
), vtt
,
4337 BINFO_SUBVTT_INDEX (basetype_path
));
4338 sub_vtt
= build_indirect_ref (sub_vtt
, NULL
);
4340 args
= tree_cons (NULL_TREE
, sub_vtt
, args
);
4346 fns
= lookup_fnfields (basetype_path
, name
, 1);
4348 if (fns
== error_mark_node
)
4349 return error_mark_node
;
4352 tree base
= BINFO_TYPE (TREE_PURPOSE (fns
));
4353 tree fn
= TREE_VALUE (fns
);
4354 mem_args
= tree_cons (NULL_TREE
, instance_ptr
, args
);
4355 for (; fn
; fn
= OVL_NEXT (fn
))
4357 tree t
= OVL_CURRENT (fn
);
4360 /* We can end up here for copy-init of same or base class. */
4361 if ((flags
& LOOKUP_ONLYCONVERTING
)
4362 && DECL_NONCONVERTING_P (t
))
4365 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
4366 this_arglist
= mem_args
;
4368 this_arglist
= args
;
4370 if (TREE_CODE (t
) == TEMPLATE_DECL
)
4372 /* A member template. */
4373 templates
= tree_cons (NULL_TREE
, t
, templates
);
4375 add_template_candidate (candidates
, t
, base
, explicit_targs
,
4377 TREE_TYPE (name
), flags
, DEDUCE_CALL
);
4379 else if (! template_only
)
4380 candidates
= add_function_candidate (candidates
, t
, base
,
4381 this_arglist
, flags
);
4384 candidates
->basetype_path
= basetype_path
;
4388 if (! any_viable (candidates
))
4390 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4391 if (flags
& LOOKUP_SPECULATIVELY
)
4393 if (!COMPLETE_TYPE_P (basetype
))
4394 incomplete_type_error (instance_ptr
, basetype
);
4396 cp_error ("no matching function for call to `%T::%D (%A)%V'",
4397 basetype
, pretty_name
, user_args
,
4398 TREE_TYPE (TREE_TYPE (instance_ptr
)));
4399 print_z_candidates (candidates
);
4400 return error_mark_node
;
4402 candidates
= splice_viable (candidates
);
4403 cand
= tourney (candidates
);
4407 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name
,
4409 print_z_candidates (candidates
);
4410 return error_mark_node
;
4413 if (DECL_PURE_VIRTUAL_P (cand
->fn
)
4414 && instance
== current_class_ref
4415 && (DECL_CONSTRUCTOR_P (current_function_decl
)
4416 || DECL_DESTRUCTOR_P (current_function_decl
))
4417 && ! (flags
& LOOKUP_NONVIRTUAL
)
4418 && value_member (cand
->fn
, CLASSTYPE_PURE_VIRTUALS (basetype
)))
4419 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl
) ?
4420 "abstract virtual `%#D' called from constructor"
4421 : "abstract virtual `%#D' called from destructor"),
4423 if (TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
4424 && is_dummy_object (instance_ptr
))
4426 cp_error ("cannot call member function `%D' without object", cand
->fn
);
4427 return error_mark_node
;
4430 if (DECL_VINDEX (cand
->fn
) && ! (flags
& LOOKUP_NONVIRTUAL
)
4431 && ((instance
== current_class_ref
&& (dtor_label
|| ctor_label
))
4432 || resolves_to_fixed_type_p (instance
, 0)))
4433 flags
|= LOOKUP_NONVIRTUAL
;
4435 return build_over_call
4437 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
? mem_args
: args
,
4441 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4442 subsequence of ICS2. */
4445 is_subseq (ics1
, ics2
)
4448 /* We can assume that a conversion of the same code
4449 between the same types indicates a subsequence since we only get
4450 here if the types we are converting from are the same. */
4452 while (TREE_CODE (ics1
) == RVALUE_CONV
4453 || TREE_CODE (ics1
) == LVALUE_CONV
)
4454 ics1
= TREE_OPERAND (ics1
, 0);
4458 while (TREE_CODE (ics2
) == RVALUE_CONV
4459 || TREE_CODE (ics2
) == LVALUE_CONV
)
4460 ics2
= TREE_OPERAND (ics2
, 0);
4462 if (TREE_CODE (ics2
) == USER_CONV
4463 || TREE_CODE (ics2
) == AMBIG_CONV
4464 || TREE_CODE (ics2
) == IDENTITY_CONV
)
4465 /* At this point, ICS1 cannot be a proper subsequence of
4466 ICS2. We can get a USER_CONV when we are comparing the
4467 second standard conversion sequence of two user conversion
4471 ics2
= TREE_OPERAND (ics2
, 0);
4473 if (TREE_CODE (ics2
) == TREE_CODE (ics1
)
4474 && same_type_p (TREE_TYPE (ics2
), TREE_TYPE (ics1
))
4475 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2
, 0)),
4476 TREE_TYPE (TREE_OPERAND (ics1
, 0))))
4481 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4482 be any _TYPE nodes. */
4485 is_properly_derived_from (derived
, base
)
4489 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived
))
4490 || !IS_AGGR_TYPE_CODE (TREE_CODE (base
)))
4493 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4494 considers every class derived from itself. */
4495 return (!same_type_ignoring_top_level_qualifiers_p (derived
, base
)
4496 && DERIVED_FROM_P (base
, derived
));
4499 /* We build the ICS for an implicit object parameter as a pointer
4500 conversion sequence. However, such a sequence should be compared
4501 as if it were a reference conversion sequence. If ICS is the
4502 implicit conversion sequence for an implicit object parameter,
4503 modify it accordingly. */
4506 maybe_handle_implicit_object (ics
)
4509 if (ICS_THIS_FLAG (*ics
))
4511 /* [over.match.funcs]
4513 For non-static member functions, the type of the
4514 implicit object parameter is "reference to cv X"
4515 where X is the class of which the function is a
4516 member and cv is the cv-qualification on the member
4517 function declaration. */
4519 tree reference_type
;
4521 /* The `this' parameter is a pointer to a class type. Make the
4522 implict conversion talk about a reference to that same class
4524 reference_type
= TREE_TYPE (TREE_TYPE (*ics
));
4525 reference_type
= build_reference_type (reference_type
);
4527 if (TREE_CODE (t
) == QUAL_CONV
)
4528 t
= TREE_OPERAND (t
, 0);
4529 if (TREE_CODE (t
) == PTR_CONV
)
4530 t
= TREE_OPERAND (t
, 0);
4531 t
= build1 (IDENTITY_CONV
, TREE_TYPE (TREE_TYPE (t
)), NULL_TREE
);
4532 t
= direct_reference_binding (reference_type
, t
);
4537 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4538 to the type the reference originally referred to, and return 1.
4539 Otherwise, return 0. */
4542 maybe_handle_ref_bind (ics
, target_type
)
4546 if (TREE_CODE (*ics
) == REF_BIND
)
4548 *target_type
= TREE_TYPE (TREE_TYPE (*ics
));
4549 *ics
= TREE_OPERAND (*ics
, 0);
4556 /* Compare two implicit conversion sequences according to the rules set out in
4557 [over.ics.rank]. Return values:
4559 1: ics1 is better than ics2
4560 -1: ics2 is better than ics1
4561 0: ics1 and ics2 are indistinguishable */
4564 compare_ics (ics1
, ics2
)
4571 tree deref_from_type1
= NULL_TREE
;
4572 tree deref_from_type2
= NULL_TREE
;
4573 tree deref_to_type1
= NULL_TREE
;
4574 tree deref_to_type2
= NULL_TREE
;
4576 /* REF_BINDING is non-zero if the result of the conversion sequence
4577 is a reference type. In that case TARGET_TYPE is the
4578 type referred to by the reference. */
4584 /* Handle implicit object parameters. */
4585 maybe_handle_implicit_object (&ics1
);
4586 maybe_handle_implicit_object (&ics2
);
4588 /* Handle reference parameters. */
4589 ref_binding1
= maybe_handle_ref_bind (&ics1
, &target_type1
);
4590 ref_binding2
= maybe_handle_ref_bind (&ics2
, &target_type2
);
4594 When comparing the basic forms of implicit conversion sequences (as
4595 defined in _over.best.ics_)
4597 --a standard conversion sequence (_over.ics.scs_) is a better
4598 conversion sequence than a user-defined conversion sequence
4599 or an ellipsis conversion sequence, and
4601 --a user-defined conversion sequence (_over.ics.user_) is a
4602 better conversion sequence than an ellipsis conversion sequence
4603 (_over.ics.ellipsis_). */
4604 if (ICS_RANK (ics1
) > ICS_RANK (ics2
))
4606 else if (ICS_RANK (ics1
) < ICS_RANK (ics2
))
4609 if (ICS_RANK (ics1
) == BAD_RANK
)
4611 /* Both ICS are bad. We try to make a decision based on what
4612 would have happenned if they'd been good. */
4613 if (ICS_USER_FLAG (ics1
) > ICS_USER_FLAG (ics2
)
4614 || ICS_STD_RANK (ics1
) > ICS_STD_RANK (ics2
))
4616 else if (ICS_USER_FLAG (ics1
) < ICS_USER_FLAG (ics2
)
4617 || ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
4620 /* We couldn't make up our minds; try to figure it out below. */
4623 if (ICS_ELLIPSIS_FLAG (ics1
))
4624 /* Both conversions are ellipsis conversions. */
4627 /* User-defined conversion sequence U1 is a better conversion sequence
4628 than another user-defined conversion sequence U2 if they contain the
4629 same user-defined conversion operator or constructor and if the sec-
4630 ond standard conversion sequence of U1 is better than the second
4631 standard conversion sequence of U2. */
4633 if (ICS_USER_FLAG (ics1
))
4637 for (t1
= ics1
; TREE_CODE (t1
) != USER_CONV
; t1
= TREE_OPERAND (t1
, 0))
4638 if (TREE_CODE (t1
) == AMBIG_CONV
)
4640 for (t2
= ics2
; TREE_CODE (t2
) != USER_CONV
; t2
= TREE_OPERAND (t2
, 0))
4641 if (TREE_CODE (t2
) == AMBIG_CONV
)
4644 if (USER_CONV_FN (t1
) != USER_CONV_FN (t2
))
4647 /* We can just fall through here, after setting up
4648 FROM_TYPE1 and FROM_TYPE2. */
4649 from_type1
= TREE_TYPE (t1
);
4650 from_type2
= TREE_TYPE (t2
);
4654 /* We're dealing with two standard conversion sequences.
4658 Standard conversion sequence S1 is a better conversion
4659 sequence than standard conversion sequence S2 if
4661 --S1 is a proper subsequence of S2 (comparing the conversion
4662 sequences in the canonical form defined by _over.ics.scs_,
4663 excluding any Lvalue Transformation; the identity
4664 conversion sequence is considered to be a subsequence of
4665 any non-identity conversion sequence */
4668 while (TREE_CODE (from_type1
) != IDENTITY_CONV
)
4669 from_type1
= TREE_OPERAND (from_type1
, 0);
4670 from_type1
= TREE_TYPE (from_type1
);
4673 while (TREE_CODE (from_type2
) != IDENTITY_CONV
)
4674 from_type2
= TREE_OPERAND (from_type2
, 0);
4675 from_type2
= TREE_TYPE (from_type2
);
4678 if (same_type_p (from_type1
, from_type2
))
4680 if (is_subseq (ics1
, ics2
))
4682 if (is_subseq (ics2
, ics1
))
4685 /* Otherwise, one sequence cannot be a subsequence of the other; they
4686 don't start with the same type. This can happen when comparing the
4687 second standard conversion sequence in two user-defined conversion
4694 --the rank of S1 is better than the rank of S2 (by the rules
4697 Standard conversion sequences are ordered by their ranks: an Exact
4698 Match is a better conversion than a Promotion, which is a better
4699 conversion than a Conversion.
4701 Two conversion sequences with the same rank are indistinguishable
4702 unless one of the following rules applies:
4704 --A conversion that is not a conversion of a pointer, or pointer
4705 to member, to bool is better than another conversion that is such
4708 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4709 so that we do not have to check it explicitly. */
4710 if (ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
4712 else if (ICS_STD_RANK (ics2
) < ICS_STD_RANK (ics1
))
4715 to_type1
= TREE_TYPE (ics1
);
4716 to_type2
= TREE_TYPE (ics2
);
4718 if (TYPE_PTR_P (from_type1
)
4719 && TYPE_PTR_P (from_type2
)
4720 && TYPE_PTR_P (to_type1
)
4721 && TYPE_PTR_P (to_type2
))
4723 deref_from_type1
= TREE_TYPE (from_type1
);
4724 deref_from_type2
= TREE_TYPE (from_type2
);
4725 deref_to_type1
= TREE_TYPE (to_type1
);
4726 deref_to_type2
= TREE_TYPE (to_type2
);
4728 /* The rules for pointers to members A::* are just like the rules
4729 for pointers A*, except opposite: if B is derived from A then
4730 A::* converts to B::*, not vice versa. For that reason, we
4731 switch the from_ and to_ variables here. */
4732 else if (TYPE_PTRMEM_P (from_type1
)
4733 && TYPE_PTRMEM_P (from_type2
)
4734 && TYPE_PTRMEM_P (to_type1
)
4735 && TYPE_PTRMEM_P (to_type2
))
4737 deref_to_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1
));
4738 deref_to_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2
));
4739 deref_from_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1
));
4740 deref_from_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2
));
4742 else if (TYPE_PTRMEMFUNC_P (from_type1
)
4743 && TYPE_PTRMEMFUNC_P (from_type2
)
4744 && TYPE_PTRMEMFUNC_P (to_type1
)
4745 && TYPE_PTRMEMFUNC_P (to_type2
))
4747 deref_to_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1
);
4748 deref_to_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2
);
4749 deref_from_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1
);
4750 deref_from_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2
);
4753 if (deref_from_type1
!= NULL_TREE
4754 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1
))
4755 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2
)))
4757 /* This was one of the pointer or pointer-like conversions.
4761 --If class B is derived directly or indirectly from class A,
4762 conversion of B* to A* is better than conversion of B* to
4763 void*, and conversion of A* to void* is better than
4764 conversion of B* to void*. */
4765 if (TREE_CODE (deref_to_type1
) == VOID_TYPE
4766 && TREE_CODE (deref_to_type2
) == VOID_TYPE
)
4768 if (is_properly_derived_from (deref_from_type1
,
4771 else if (is_properly_derived_from (deref_from_type2
,
4775 else if (TREE_CODE (deref_to_type1
) == VOID_TYPE
4776 || TREE_CODE (deref_to_type2
) == VOID_TYPE
)
4778 if (same_type_p (deref_from_type1
, deref_from_type2
))
4780 if (TREE_CODE (deref_to_type2
) == VOID_TYPE
)
4782 if (is_properly_derived_from (deref_from_type1
,
4786 /* We know that DEREF_TO_TYPE1 is `void' here. */
4787 else if (is_properly_derived_from (deref_from_type1
,
4792 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1
))
4793 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2
)))
4797 --If class B is derived directly or indirectly from class A
4798 and class C is derived directly or indirectly from B,
4800 --conversion of C* to B* is better than conversion of C* to
4803 --conversion of B* to A* is better than conversion of C* to
4805 if (same_type_p (deref_from_type1
, deref_from_type2
))
4807 if (is_properly_derived_from (deref_to_type1
,
4810 else if (is_properly_derived_from (deref_to_type2
,
4814 else if (same_type_p (deref_to_type1
, deref_to_type2
))
4816 if (is_properly_derived_from (deref_from_type2
,
4819 else if (is_properly_derived_from (deref_from_type1
,
4825 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1
))
4826 && same_type_p (from_type1
, from_type2
))
4830 --binding of an expression of type C to a reference of type
4831 B& is better than binding an expression of type C to a
4832 reference of type A&
4834 --conversion of C to B is better than conversion of C to A, */
4835 if (is_properly_derived_from (from_type1
, to_type1
)
4836 && is_properly_derived_from (from_type1
, to_type2
))
4838 if (is_properly_derived_from (to_type1
, to_type2
))
4840 else if (is_properly_derived_from (to_type2
, to_type1
))
4844 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1
))
4845 && same_type_p (to_type1
, to_type2
))
4849 --binding of an expression of type B to a reference of type
4850 A& is better than binding an expression of type C to a
4851 reference of type A&,
4853 --onversion of B to A is better than conversion of C to A */
4854 if (is_properly_derived_from (from_type1
, to_type1
)
4855 && is_properly_derived_from (from_type2
, to_type1
))
4857 if (is_properly_derived_from (from_type2
, from_type1
))
4859 else if (is_properly_derived_from (from_type1
, from_type2
))
4866 --S1 and S2 differ only in their qualification conversion and yield
4867 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4868 qualification signature of type T1 is a proper subset of the cv-
4869 qualification signature of type T2 */
4870 if (TREE_CODE (ics1
) == QUAL_CONV
4871 && TREE_CODE (ics2
) == QUAL_CONV
4872 && same_type_p (from_type1
, from_type2
))
4873 return comp_cv_qual_signature (to_type1
, to_type2
);
4877 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4878 types to which the references refer are the same type except for
4879 top-level cv-qualifiers, and the type to which the reference
4880 initialized by S2 refers is more cv-qualified than the type to
4881 which the reference initialized by S1 refers */
4883 if (ref_binding1
&& ref_binding2
4884 && same_type_ignoring_top_level_qualifiers_p (to_type1
, to_type2
))
4885 return comp_cv_qualification (target_type2
, target_type1
);
4887 /* Neither conversion sequence is better than the other. */
4891 /* The source type for this standard conversion sequence. */
4897 for (;; t
= TREE_OPERAND (t
, 0))
4899 if (TREE_CODE (t
) == USER_CONV
4900 || TREE_CODE (t
) == AMBIG_CONV
4901 || TREE_CODE (t
) == IDENTITY_CONV
)
4902 return TREE_TYPE (t
);
4904 my_friendly_abort (1823);
4907 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4908 a pointer to LOSER and re-running joust to produce the warning if WINNER
4909 is actually used. */
4912 add_warning (winner
, loser
)
4913 struct z_candidate
*winner
, *loser
;
4915 winner
->warnings
= tree_cons (NULL_PTR
,
4916 build_expr_ptr_wrapper (loser
),
4920 /* Returns true iff functions are equivalent. Equivalent functions are
4921 not identical only if one is a function-local extern function. */
4924 equal_functions (fn1
, fn2
)
4928 if (DECL_LOCAL_FUNCTION_P (fn1
) || DECL_LOCAL_FUNCTION_P (fn2
))
4929 return decls_match (fn1
, fn2
);
4933 /* Compare two candidates for overloading as described in
4934 [over.match.best]. Return values:
4936 1: cand1 is better than cand2
4937 -1: cand2 is better than cand1
4938 0: cand1 and cand2 are indistinguishable */
4941 joust (cand1
, cand2
, warn
)
4942 struct z_candidate
*cand1
, *cand2
;
4946 int i
, off1
= 0, off2
= 0, len
;
4948 /* Candidates that involve bad conversions are always worse than those
4950 if (cand1
->viable
> cand2
->viable
)
4952 if (cand1
->viable
< cand2
->viable
)
4955 /* If we have two pseudo-candidates for conversions to the same type,
4956 or two candidates for the same function, arbitrarily pick one. */
4957 if (cand1
->fn
== cand2
->fn
4958 && (TYPE_P (cand1
->fn
) || DECL_P (cand1
->fn
)))
4961 /* a viable function F1
4962 is defined to be a better function than another viable function F2 if
4963 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4964 ICSi(F2), and then */
4966 /* for some argument j, ICSj(F1) is a better conversion sequence than
4969 /* For comparing static and non-static member functions, we ignore
4970 the implicit object parameter of the non-static function. The
4971 standard says to pretend that the static function has an object
4972 parm, but that won't work with operator overloading. */
4973 len
= TREE_VEC_LENGTH (cand1
->convs
);
4974 if (len
!= TREE_VEC_LENGTH (cand2
->convs
))
4976 if (DECL_STATIC_FUNCTION_P (cand1
->fn
)
4977 && ! DECL_STATIC_FUNCTION_P (cand2
->fn
))
4979 else if (! DECL_STATIC_FUNCTION_P (cand1
->fn
)
4980 && DECL_STATIC_FUNCTION_P (cand2
->fn
))
4986 my_friendly_abort (42);
4989 for (i
= 0; i
< len
; ++i
)
4991 tree t1
= TREE_VEC_ELT (cand1
->convs
, i
+off1
);
4992 tree t2
= TREE_VEC_ELT (cand2
->convs
, i
+off2
);
4993 int comp
= compare_ics (t1
, t2
);
4998 && ICS_RANK (t1
) + ICS_RANK (t2
) == STD_RANK
+ PROMO_RANK
4999 && TREE_CODE (t1
) == STD_CONV
5000 && TREE_CODE (t2
) == STD_CONV
5001 && TREE_CODE (TREE_TYPE (t1
)) == INTEGER_TYPE
5002 && TREE_CODE (TREE_TYPE (t2
)) == INTEGER_TYPE
5003 && (TYPE_PRECISION (TREE_TYPE (t1
))
5004 == TYPE_PRECISION (TREE_TYPE (t2
)))
5005 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5006 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5009 tree type
= TREE_TYPE (TREE_OPERAND (t1
, 0));
5011 struct z_candidate
*w
, *l
;
5013 type1
= TREE_TYPE (t1
), type2
= TREE_TYPE (t2
),
5014 w
= cand1
, l
= cand2
;
5016 type1
= TREE_TYPE (t2
), type2
= TREE_TYPE (t1
),
5017 w
= cand2
, l
= cand1
;
5021 cp_warning ("passing `%T' chooses `%T' over `%T'",
5022 type
, type1
, type2
);
5023 cp_warning (" in call to `%D'", w
->fn
);
5029 if (winner
&& comp
!= winner
)
5038 /* warn about confusing overload resolution for user-defined conversions,
5039 either between a constructor and a conversion op, or between two
5041 if (winner
&& cand1
->second_conv
5042 && ((DECL_CONSTRUCTOR_P (cand1
->fn
)
5043 != DECL_CONSTRUCTOR_P (cand2
->fn
))
5044 /* Don't warn if the two conv ops convert to the same type... */
5045 || (! DECL_CONSTRUCTOR_P (cand1
->fn
)
5046 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1
->fn
)),
5047 TREE_TYPE (TREE_TYPE (cand2
->fn
))))))
5049 int comp
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5052 struct z_candidate
*w
, *l
;
5054 w
= cand1
, l
= cand2
;
5056 w
= cand2
, l
= cand1
;
5059 tree source
= source_type (TREE_VEC_ELT (w
->convs
, 0));
5060 if (! DECL_CONSTRUCTOR_P (w
->fn
))
5061 source
= TREE_TYPE (source
);
5062 cp_warning ("choosing `%D' over `%D'", w
->fn
, l
->fn
);
5063 cp_warning (" for conversion from `%T' to `%T'",
5064 source
, TREE_TYPE (w
->second_conv
));
5065 cp_warning (" because conversion sequence for the argument is better");
5076 F1 is a non-template function and F2 is a template function */
5078 if (! cand1
->template && cand2
->template)
5080 else if (cand1
->template && ! cand2
->template)
5082 else if (cand1
->template && cand2
->template)
5083 winner
= more_specialized
5084 (TI_TEMPLATE (cand1
->template), TI_TEMPLATE (cand2
->template),
5088 the context is an initialization by user-defined conversion (see
5089 _dcl.init_ and _over.match.user_) and the standard conversion
5090 sequence from the return type of F1 to the destination type (i.e.,
5091 the type of the entity being initialized) is a better conversion
5092 sequence than the standard conversion sequence from the return type
5093 of F2 to the destination type. */
5095 if (! winner
&& cand1
->second_conv
)
5096 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5098 /* If the built-in candidates are the same, arbitrarily pick one. */
5099 if (! winner
&& cand1
->fn
== cand2
->fn
5100 && TREE_CODE (cand1
->fn
) == IDENTIFIER_NODE
)
5102 for (i
= 0; i
< len
; ++i
)
5103 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1
->convs
, i
)),
5104 TREE_TYPE (TREE_VEC_ELT (cand2
->convs
, i
))))
5106 if (i
== TREE_VEC_LENGTH (cand1
->convs
))
5109 /* Kludge around broken overloading rules whereby
5110 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5111 that takes references and another that takes values. */
5112 if (cand1
->fn
== ansi_opname (COND_EXPR
))
5114 tree c1
= TREE_VEC_ELT (cand1
->convs
, 1);
5115 tree c2
= TREE_VEC_ELT (cand2
->convs
, 1);
5116 tree t1
= strip_top_quals (non_reference (TREE_TYPE (c1
)));
5117 tree t2
= strip_top_quals (non_reference (TREE_TYPE (c2
)));
5119 if (same_type_p (t1
, t2
))
5121 if (TREE_CODE (c1
) == REF_BIND
&& TREE_CODE (c2
) != REF_BIND
)
5123 if (TREE_CODE (c1
) != REF_BIND
&& TREE_CODE (c2
) == REF_BIND
)
5129 /* If the two functions are the same (this can happen with declarations
5130 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5131 if (DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
5132 && equal_functions (cand1
->fn
, cand2
->fn
))
5137 /* Extension: If the worst conversion for one candidate is worse than the
5138 worst conversion for the other, take the first. */
5139 if (! winner
&& ! pedantic
)
5141 int rank1
= IDENTITY_RANK
, rank2
= IDENTITY_RANK
;
5143 for (i
= 0; i
< len
; ++i
)
5145 if (ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
)) > rank1
)
5146 rank1
= ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
));
5147 if (ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
)) > rank2
)
5148 rank2
= ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
));
5160 /* Given a list of candidates for overloading, find the best one, if any.
5161 This algorithm has a worst case of O(2n) (winner is last), and a best
5162 case of O(n/2) (totally ambiguous); much better than a sorting
5165 static struct z_candidate
*
5166 tourney (candidates
)
5167 struct z_candidate
*candidates
;
5169 struct z_candidate
*champ
= candidates
, *challenger
;
5171 int champ_compared_to_predecessor
= 0;
5173 /* Walk through the list once, comparing each current champ to the next
5174 candidate, knocking out a candidate or two with each comparison. */
5176 for (challenger
= champ
->next
; challenger
; )
5178 fate
= joust (champ
, challenger
, 0);
5180 challenger
= challenger
->next
;
5185 champ
= challenger
->next
;
5188 champ_compared_to_predecessor
= 0;
5193 champ_compared_to_predecessor
= 1;
5196 challenger
= champ
->next
;
5200 /* Make sure the champ is better than all the candidates it hasn't yet
5201 been compared to. */
5203 for (challenger
= candidates
;
5205 && !(champ_compared_to_predecessor
&& challenger
->next
== champ
);
5206 challenger
= challenger
->next
)
5208 fate
= joust (champ
, challenger
, 0);
5216 /* Returns non-zero if things of type FROM can be converted to TO. */
5219 can_convert (to
, from
)
5222 return can_convert_arg (to
, from
, NULL_TREE
);
5225 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5228 can_convert_arg (to
, from
, arg
)
5231 tree t
= implicit_conversion (to
, from
, arg
, LOOKUP_NORMAL
);
5232 return (t
&& ! ICS_BAD_FLAG (t
));
5235 /* Convert EXPR to TYPE. Return the converted expression. */
5238 perform_implicit_conversion (type
, expr
)
5244 if (expr
== error_mark_node
)
5245 return error_mark_node
;
5246 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
5248 if (!conv
|| ICS_BAD_FLAG (conv
))
5250 cp_error ("could not convert `%E' to `%T'", expr
, type
);
5251 return error_mark_node
;
5254 return convert_like (conv
, expr
);
5257 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5258 initializing a variable of that TYPE. Return the converted
5262 initialize_reference (type
, expr
)
5268 conv
= reference_binding (type
, TREE_TYPE (expr
), expr
, LOOKUP_NORMAL
);
5269 if (!conv
|| ICS_BAD_FLAG (conv
))
5271 cp_error ("could not convert `%E' to `%T'", expr
, type
);
5272 return error_mark_node
;
5275 return convert_like (conv
, expr
);