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 (TREE_CODE (basetype
) == NAMESPACE_DECL
)
280 cp_error ("`%D' is a namespace", basetype
);
281 return error_mark_node
;
283 if (! is_aggr_type (basetype
, 1))
284 return error_mark_node
;
286 if (! IS_AGGR_TYPE (type
))
288 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
290 return error_mark_node
;
295 binfo
= get_binfo (basetype
, type
, 1);
296 if (binfo
== error_mark_node
)
297 return error_mark_node
;
299 error_not_base_type (basetype
, type
);
304 if (TREE_CODE (exp
) == INDIRECT_REF
)
305 decl
= build_indirect_ref
306 (convert_pointer_to_real
307 (binfo
, build_unary_op (ADDR_EXPR
, exp
, 0)), NULL_PTR
);
309 decl
= build_scoped_ref (exp
, basetype
);
311 /* Call to a destructor. */
312 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
314 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl
)))
315 return cp_convert (void_type_node
, exp
);
317 return build_delete (TREE_TYPE (decl
), decl
,
318 sfk_complete_destructor
,
319 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
,
323 /* Call to a method. */
324 return build_method_call (decl
, name
, parms
, binfo
,
325 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
);
327 return error_mark_node
;
330 /* We want the address of a function or method. We avoid creating a
331 pointer-to-member function. */
334 build_addr_func (function
)
337 tree type
= TREE_TYPE (function
);
339 /* We have to do these by hand to avoid real pointer to member
341 if (TREE_CODE (type
) == METHOD_TYPE
)
345 type
= build_pointer_type (type
);
347 if (mark_addressable (function
) == 0)
348 return error_mark_node
;
350 addr
= build1 (ADDR_EXPR
, type
, function
);
352 /* Address of a static or external variable or function counts
354 if (staticp (function
))
355 TREE_CONSTANT (addr
) = 1;
360 function
= default_conversion (function
);
365 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
366 POINTER_TYPE to those. Note, pointer to member function types
367 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
370 build_call (function
, parms
)
371 tree function
, parms
;
373 int is_constructor
= 0;
379 function
= build_addr_func (function
);
381 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
383 sorry ("unable to call pointer to member function here");
384 return error_mark_node
;
387 result_type
= TREE_TYPE (TREE_TYPE (TREE_TYPE (function
)));
389 if (TREE_CODE (function
) == ADDR_EXPR
390 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
391 decl
= TREE_OPERAND (function
, 0);
395 /* We check both the decl and the type; a function may be known not to
396 throw without being declared throw(). */
397 nothrow
= ((decl
&& TREE_NOTHROW (decl
))
398 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function
))));
400 if (decl
&& DECL_CONSTRUCTOR_P (decl
))
403 if (decl
&& ! TREE_USED (decl
))
405 /* We invoke build_call directly for several library functions.
406 These may have been declared normally if we're building libgcc,
407 so we can't just check DECL_ARTIFICIAL. */
408 if (DECL_ARTIFICIAL (decl
)
409 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl
)), "__", 2))
412 my_friendly_abort (990125);
415 /* Don't pass empty class objects by value. This is useful
416 for tags in STL, which are used to control overload resolution.
417 We don't need to handle other cases of copying empty classes. */
418 if (! decl
|| ! DECL_BUILT_IN (decl
))
419 for (tmp
= parms
; tmp
; tmp
= TREE_CHAIN (tmp
))
420 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp
)))
421 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp
))))
423 tree t
= build (EMPTY_CLASS_EXPR
, TREE_TYPE (TREE_VALUE (tmp
)));
424 TREE_VALUE (tmp
) = build (COMPOUND_EXPR
, TREE_TYPE (t
),
425 TREE_VALUE (tmp
), t
);
428 function
= build_nt (CALL_EXPR
, function
, parms
, NULL_TREE
);
429 TREE_HAS_CONSTRUCTOR (function
) = is_constructor
;
430 TREE_TYPE (function
) = result_type
;
431 TREE_SIDE_EFFECTS (function
) = 1;
432 TREE_NOTHROW (function
) = nothrow
;
437 /* Build something of the form ptr->method (args)
438 or object.method (args). This can also build
439 calls to constructors, and find friends.
441 Member functions always take their class variable
444 INSTANCE is a class instance.
446 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
448 PARMS help to figure out what that NAME really refers to.
450 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
451 down to the real instance type to use for access checking. We need this
452 information to get protected accesses correct. This parameter is used
453 by build_member_call.
455 FLAGS is the logical disjunction of zero or more LOOKUP_
456 flags. See cp-tree.h for more info.
458 If this is all OK, calls build_function_call with the resolved
461 This function must also handle being called to perform
462 initialization, promotion/coercion of arguments, and
463 instantiation of default parameters.
465 Note that NAME may refer to an instance variable name. If
466 `operator()()' is defined for the type of that field, then we return
469 #ifdef GATHER_STATISTICS
470 extern int n_build_method_call
;
474 build_method_call (instance
, name
, parms
, basetype_path
, flags
)
475 tree instance
, name
, parms
, basetype_path
;
478 tree basetype
, instance_ptr
;
480 #ifdef GATHER_STATISTICS
481 n_build_method_call
++;
484 if (instance
== error_mark_node
485 || name
== error_mark_node
486 || parms
== error_mark_node
487 || (instance
!= NULL_TREE
&& TREE_TYPE (instance
) == error_mark_node
))
488 return error_mark_node
;
490 if (processing_template_decl
)
492 /* We need to process template parm names here so that tsubst catches
493 them properly. Other type names can wait. */
494 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
496 tree type
= NULL_TREE
;
498 if (TREE_CODE (TREE_OPERAND (name
, 0)) == IDENTIFIER_NODE
)
499 type
= get_aggr_from_typedef (TREE_OPERAND (name
, 0), 0);
500 else if (TREE_CODE (TREE_OPERAND (name
, 0)) == TYPE_DECL
)
501 type
= TREE_TYPE (TREE_OPERAND (name
, 0));
503 if (type
&& TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
504 name
= build_min_nt (BIT_NOT_EXPR
, type
);
507 return build_min_nt (METHOD_CALL_EXPR
, name
, instance
, parms
, NULL_TREE
);
510 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
513 error ("destructors take no parameters");
514 basetype
= TREE_TYPE (instance
);
515 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
516 basetype
= TREE_TYPE (basetype
);
518 if (! check_dtor_name (basetype
, name
))
520 ("destructor name `~%T' does not match type `%T' of expression",
521 TREE_OPERAND (name
, 0), basetype
);
523 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype
)))
524 return cp_convert (void_type_node
, instance
);
525 instance
= default_conversion (instance
);
526 instance_ptr
= build_unary_op (ADDR_EXPR
, instance
, 0);
527 return build_delete (build_pointer_type (basetype
),
528 instance_ptr
, sfk_complete_destructor
,
529 LOOKUP_NORMAL
|LOOKUP_DESTRUCTOR
, 0);
532 return build_new_method_call (instance
, name
, parms
, basetype_path
, flags
);
535 /* New overloading code. */
545 struct z_candidate
*next
;
548 #define IDENTITY_RANK 0
554 #define ELLIPSIS_RANK 6
557 #define ICS_RANK(NODE) \
558 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
559 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
560 : ICS_USER_FLAG (NODE) ? USER_RANK \
561 : ICS_STD_RANK (NODE))
563 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
565 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
566 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
567 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
568 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
570 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
571 should be created to hold the result of the conversion. */
572 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
574 #define USER_CONV_CAND(NODE) \
575 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
576 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
584 A null pointer constant is an integral constant expression
585 (_expr.const_) rvalue of integer type that evaluates to zero. */
587 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t
)) && integer_zerop (t
)))
593 build_conv (code
, type
, from
)
597 tree t
= build1 (code
, type
, from
);
598 int rank
= ICS_STD_RANK (from
);
610 if (rank
< EXACT_RANK
)
616 ICS_STD_RANK (t
) = rank
;
617 ICS_USER_FLAG (t
) = ICS_USER_FLAG (from
);
618 ICS_BAD_FLAG (t
) = ICS_BAD_FLAG (from
);
626 if (TREE_CODE (t
) == REFERENCE_TYPE
)
635 if (TREE_CODE (t
) == ARRAY_TYPE
)
637 return TYPE_MAIN_VARIANT (t
);
640 /* Returns the standard conversion path (see [conv]) from type FROM to type
641 TO, if any. For proper handling of null pointer constants, you must
642 also pass the expression EXPR to convert from. */
645 standard_conversion (to
, from
, expr
)
648 enum tree_code fcode
, tcode
;
652 if (TREE_CODE (to
) == REFERENCE_TYPE
)
654 if (TREE_CODE (from
) == REFERENCE_TYPE
)
657 from
= TREE_TYPE (from
);
659 to
= strip_top_quals (to
);
660 from
= strip_top_quals (from
);
662 if ((TYPE_PTRFN_P (to
) || TYPE_PTRMEMFUNC_P (to
))
663 && expr
&& type_unknown_p (expr
))
665 expr
= instantiate_type (to
, expr
, itf_none
);
666 if (expr
== error_mark_node
)
668 from
= TREE_TYPE (expr
);
671 fcode
= TREE_CODE (from
);
672 tcode
= TREE_CODE (to
);
674 conv
= build1 (IDENTITY_CONV
, from
, expr
);
676 if (fcode
== FUNCTION_TYPE
)
678 from
= build_pointer_type (from
);
679 fcode
= TREE_CODE (from
);
680 conv
= build_conv (LVALUE_CONV
, from
, conv
);
682 else if (fcode
== ARRAY_TYPE
)
684 from
= build_pointer_type (TREE_TYPE (from
));
685 fcode
= TREE_CODE (from
);
686 conv
= build_conv (LVALUE_CONV
, from
, conv
);
688 else if (fromref
|| (expr
&& lvalue_p (expr
)))
689 conv
= build_conv (RVALUE_CONV
, from
, conv
);
691 /* Allow conversion between `__complex__' data types */
692 if (tcode
== COMPLEX_TYPE
&& fcode
== COMPLEX_TYPE
)
694 /* The standard conversion sequence to convert FROM to TO is
695 the standard conversion sequence to perform componentwise
697 tree part_conv
= standard_conversion
698 (TREE_TYPE (to
), TREE_TYPE (from
), NULL_TREE
);
702 conv
= build_conv (TREE_CODE (part_conv
), to
, conv
);
703 ICS_STD_RANK (conv
) = ICS_STD_RANK (part_conv
);
711 if (same_type_p (from
, to
))
714 if ((tcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (to
))
715 && expr
&& null_ptr_cst_p (expr
))
717 conv
= build_conv (STD_CONV
, to
, conv
);
719 else if (tcode
== POINTER_TYPE
&& fcode
== POINTER_TYPE
)
721 enum tree_code ufcode
= TREE_CODE (TREE_TYPE (from
));
722 enum tree_code utcode
= TREE_CODE (TREE_TYPE (to
));
724 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from
),
727 else if (utcode
== VOID_TYPE
&& ufcode
!= OFFSET_TYPE
728 && ufcode
!= FUNCTION_TYPE
)
730 from
= build_pointer_type
731 (cp_build_qualified_type (void_type_node
,
732 CP_TYPE_QUALS (TREE_TYPE (from
))));
733 conv
= build_conv (PTR_CONV
, from
, conv
);
735 else if (ufcode
== OFFSET_TYPE
&& utcode
== OFFSET_TYPE
)
737 tree fbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from
));
738 tree tbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to
));
739 tree binfo
= get_binfo (fbase
, tbase
, 1);
741 if (binfo
&& !binfo_from_vbase (binfo
)
742 && (same_type_ignoring_top_level_qualifiers_p
743 (TREE_TYPE (TREE_TYPE (from
)),
744 TREE_TYPE (TREE_TYPE (to
)))))
746 from
= build_offset_type (tbase
, TREE_TYPE (TREE_TYPE (from
)));
747 from
= build_pointer_type (from
);
748 conv
= build_conv (PMEM_CONV
, from
, conv
);
751 else if (IS_AGGR_TYPE (TREE_TYPE (from
))
752 && IS_AGGR_TYPE (TREE_TYPE (to
)))
754 if (DERIVED_FROM_P (TREE_TYPE (to
), TREE_TYPE (from
)))
757 cp_build_qualified_type (TREE_TYPE (to
),
758 CP_TYPE_QUALS (TREE_TYPE (from
)));
759 from
= build_pointer_type (from
);
760 conv
= build_conv (PTR_CONV
, from
, conv
);
764 if (same_type_p (from
, to
))
766 else if (comp_ptr_ttypes (TREE_TYPE (to
), TREE_TYPE (from
)))
767 conv
= build_conv (QUAL_CONV
, to
, conv
);
768 else if (expr
&& string_conv_p (to
, expr
, 0))
769 /* converting from string constant to char *. */
770 conv
= build_conv (QUAL_CONV
, to
, conv
);
771 else if (ptr_reasonably_similar (TREE_TYPE (to
), TREE_TYPE (from
)))
773 conv
= build_conv (PTR_CONV
, to
, conv
);
774 ICS_BAD_FLAG (conv
) = 1;
781 else if (TYPE_PTRMEMFUNC_P (to
) && TYPE_PTRMEMFUNC_P (from
))
783 tree fromfn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from
));
784 tree tofn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to
));
785 tree fbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn
)));
786 tree tbase
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn
)));
787 tree binfo
= get_binfo (fbase
, tbase
, 1);
789 if (!binfo
|| binfo_from_vbase (binfo
)
790 || !same_type_p (TREE_TYPE (fromfn
), TREE_TYPE (tofn
))
791 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)),
792 TREE_CHAIN (TYPE_ARG_TYPES (tofn
)))
793 || CP_TYPE_QUALS (fbase
) != CP_TYPE_QUALS (tbase
))
796 from
= cp_build_qualified_type (tbase
, CP_TYPE_QUALS (fbase
));
797 from
= build_cplus_method_type (from
, TREE_TYPE (fromfn
),
798 TREE_CHAIN (TYPE_ARG_TYPES (fromfn
)));
799 from
= build_ptrmemfunc_type (build_pointer_type (from
));
800 conv
= build_conv (PMEM_CONV
, from
, conv
);
802 else if (tcode
== BOOLEAN_TYPE
)
804 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
805 || fcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (from
)))
808 conv
= build_conv (STD_CONV
, to
, conv
);
809 if (fcode
== POINTER_TYPE
810 || (TYPE_PTRMEMFUNC_P (from
) && ICS_STD_RANK (conv
) < PBOOL_RANK
))
811 ICS_STD_RANK (conv
) = PBOOL_RANK
;
813 /* We don't check for ENUMERAL_TYPE here because there are no standard
814 conversions to enum type. */
815 else if (tcode
== INTEGER_TYPE
|| tcode
== BOOLEAN_TYPE
816 || tcode
== REAL_TYPE
)
818 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
))
820 conv
= build_conv (STD_CONV
, to
, conv
);
822 /* Give this a better rank if it's a promotion. */
823 if (to
== type_promotes_to (from
)
824 && ICS_STD_RANK (TREE_OPERAND (conv
, 0)) <= PROMO_RANK
)
825 ICS_STD_RANK (conv
) = PROMO_RANK
;
827 else if (IS_AGGR_TYPE (to
) && IS_AGGR_TYPE (from
)
828 && is_properly_derived_from (from
, to
))
830 if (TREE_CODE (conv
) == RVALUE_CONV
)
831 conv
= TREE_OPERAND (conv
, 0);
832 conv
= build_conv (BASE_CONV
, to
, conv
);
833 /* The derived-to-base conversion indicates the initialization
834 of a parameter with base type from an object of a derived
835 type. A temporary object is created to hold the result of
837 NEED_TEMPORARY_P (conv
) = 1;
845 /* Returns non-zero if T1 is reference-related to T2. */
848 reference_related_p (t1
, t2
)
852 t1
= TYPE_MAIN_VARIANT (t1
);
853 t2
= TYPE_MAIN_VARIANT (t2
);
857 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
858 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
860 return (same_type_p (t1
, t2
)
861 || (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
862 && DERIVED_FROM_P (t1
, t2
)));
865 /* Returns non-zero if T1 is reference-compatible with T2. */
868 reference_compatible_p (t1
, t2
)
874 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
875 reference-related to T2 and cv1 is the same cv-qualification as,
876 or greater cv-qualification than, cv2. */
877 return (reference_related_p (t1
, t2
)
878 && at_least_as_qualified_p (t1
, t2
));
881 /* Determine whether or not the EXPR (of class type S) can be
882 converted to T as in [over.match.ref]. */
885 convert_class_to_reference (t
, s
, expr
)
893 struct z_candidate
*candidates
;
894 struct z_candidate
*cand
;
898 Assuming that "cv1 T" is the underlying type of the reference
899 being initialized, and "cv S" is the type of the initializer
900 expression, with S a class type, the candidate functions are
903 --The conversion functions of S and its base classes are
904 considered. Those that are not hidden within S and yield type
905 "reference to cv2 T2", where "cv1 T" is reference-compatible
906 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
908 The argument list has one argument, which is the initializer
913 /* Conceptually, we should take the address of EXPR and put it in
914 the argument list. Unfortunately, however, that can result in
915 error messages, which we should not issue now because we are just
916 trying to find a conversion operator. Therefore, we use NULL,
917 cast to the appropriate type. */
918 arglist
= build_int_2 (0, 0);
919 TREE_TYPE (arglist
) = build_pointer_type (s
);
920 arglist
= build_tree_list (NULL_TREE
, arglist
);
922 for (conversions
= lookup_conversions (s
);
924 conversions
= TREE_CHAIN (conversions
))
926 tree fns
= TREE_VALUE (conversions
);
928 for (; fns
; fns
= OVL_NEXT (fns
))
930 tree f
= OVL_CURRENT (fns
);
931 tree t2
= TREE_TYPE (TREE_TYPE (f
));
932 struct z_candidate
*old_candidates
= candidates
;
934 /* If this is a template function, try to get an exact
936 if (TREE_CODE (f
) == TEMPLATE_DECL
)
939 = add_template_candidate (candidates
,
943 build_reference_type (t
),
947 if (candidates
!= old_candidates
)
949 /* Now, see if the conversion function really returns
950 an lvalue of the appropriate type. From the
951 point of view of unification, simply returning an
952 rvalue of the right type is good enough. */
954 t2
= TREE_TYPE (TREE_TYPE (f
));
955 if (TREE_CODE (t2
) != REFERENCE_TYPE
956 || !reference_compatible_p (t
, TREE_TYPE (t2
)))
957 candidates
= candidates
->next
;
960 else if (TREE_CODE (t2
) == REFERENCE_TYPE
961 && reference_compatible_p (t
, TREE_TYPE (t2
)))
963 = add_function_candidate (candidates
, f
, s
, arglist
,
966 if (candidates
!= old_candidates
)
967 candidates
->basetype_path
= TYPE_BINFO (s
);
971 /* If none of the conversion functions worked out, let our caller
973 if (!any_viable (candidates
))
976 candidates
= splice_viable (candidates
);
977 cand
= tourney (candidates
);
981 conv
= build1 (IDENTITY_CONV
, s
, expr
);
982 conv
= build_conv (USER_CONV
,
983 non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
))),
985 TREE_OPERAND (conv
, 1) = build_expr_ptr_wrapper (cand
);
986 ICS_USER_FLAG (conv
) = 1;
987 if (cand
->viable
== -1)
988 ICS_BAD_FLAG (conv
) = 1;
989 cand
->second_conv
= conv
;
994 /* A reference of the indicated TYPE is being bound directly to the
995 expression represented by the implicit conversion sequence CONV.
996 Return a conversion sequence for this binding. */
999 direct_reference_binding (type
, conv
)
1003 tree t
= TREE_TYPE (type
);
1007 When a parameter of reference type binds directly
1008 (_dcl.init.ref_) to an argument expression, the implicit
1009 conversion sequence is the identity conversion, unless the
1010 argument expression has a type that is a derived class of the
1011 parameter type, in which case the implicit conversion sequence is
1012 a derived-to-base Conversion.
1014 If the parameter binds directly to the result of applying a
1015 conversion function to the argument expression, the implicit
1016 conversion sequence is a user-defined conversion sequence
1017 (_over.ics.user_), with the second standard conversion sequence
1018 either an identity conversion or, if the conversion function
1019 returns an entity of a type that is a derived class of the
1020 parameter type, a derived-to-base conversion. */
1021 if (!same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (conv
)))
1023 /* Represent the derived-to-base conversion. */
1024 conv
= build_conv (BASE_CONV
, t
, conv
);
1025 /* We will actually be binding to the base-class subobject in
1026 the derived class, so we mark this conversion appropriately.
1027 That way, convert_like knows not to generate a temporary. */
1028 NEED_TEMPORARY_P (conv
) = 0;
1030 return build_conv (REF_BIND
, type
, conv
);
1033 /* Returns the conversion path from type FROM to reference type TO for
1034 purposes of reference binding. For lvalue binding, either pass a
1035 reference type to FROM or an lvalue expression to EXPR. If the
1036 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1037 the conversion returned. */
1040 reference_binding (rto
, rfrom
, expr
, flags
)
1041 tree rto
, rfrom
, expr
;
1044 tree conv
= NULL_TREE
;
1045 tree to
= TREE_TYPE (rto
);
1049 cp_lvalue_kind lvalue_p
= clk_none
;
1051 if (TREE_CODE (to
) == FUNCTION_TYPE
&& expr
&& type_unknown_p (expr
))
1053 expr
= instantiate_type (to
, expr
, itf_none
);
1054 if (expr
== error_mark_node
)
1056 from
= TREE_TYPE (expr
);
1059 if (TREE_CODE (from
) == REFERENCE_TYPE
)
1061 /* Anything with reference type is an lvalue. */
1062 lvalue_p
= clk_ordinary
;
1063 from
= TREE_TYPE (from
);
1066 lvalue_p
= real_lvalue_p (expr
);
1068 /* Figure out whether or not the types are reference-related and
1069 reference compatible. We have do do this after stripping
1070 references from FROM. */
1071 related_p
= reference_related_p (to
, from
);
1072 compatible_p
= reference_compatible_p (to
, from
);
1074 if (lvalue_p
&& compatible_p
)
1078 If the intializer expression
1080 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1081 is reference-compatible with "cv2 T2,"
1083 the reference is bound directly to the initializer exprssion
1085 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1086 conv
= direct_reference_binding (rto
, conv
);
1087 if ((lvalue_p
& clk_bitfield
) != 0
1088 && CP_TYPE_CONST_NON_VOLATILE_P (to
))
1089 /* For the purposes of overload resolution, we ignore the fact
1090 this expression is a bitfield. (In particular,
1091 [over.ics.ref] says specifically that a function with a
1092 non-const reference parameter is viable even if the
1093 argument is a bitfield.)
1095 However, when we actually call the function we must create
1096 a temporary to which to bind the reference. If the
1097 reference is volatile, or isn't const, then we cannot make
1098 a temporary, so we just issue an error when the conversion
1100 NEED_TEMPORARY_P (conv
) = 1;
1103 else if (CLASS_TYPE_P (from
) && !(flags
& LOOKUP_NO_CONVERSION
))
1107 If the initializer exprsesion
1109 -- has a class type (i.e., T2 is a class type) can be
1110 implicitly converted to an lvalue of type "cv3 T3," where
1111 "cv1 T1" is reference-compatible with "cv3 T3". (this
1112 conversion is selected by enumerating the applicable
1113 conversion functions (_over.match.ref_) and choosing the
1114 best one through overload resolution. (_over.match_).
1116 the reference is bound to the lvalue result of the conversion
1117 in the second case. */
1118 conv
= convert_class_to_reference (to
, from
, expr
);
1120 return direct_reference_binding (rto
, conv
);
1123 /* From this point on, we conceptually need temporaries, even if we
1124 elide them. Only the cases above are "direct bindings". */
1125 if (flags
& LOOKUP_NO_TEMP_BIND
)
1130 When a parameter of reference type is not bound directly to an
1131 argument expression, the conversion sequence is the one required
1132 to convert the argument expression to the underlying type of the
1133 reference according to _over.best.ics_. Conceptually, this
1134 conversion sequence corresponds to copy-initializing a temporary
1135 of the underlying type with the argument expression. Any
1136 difference in top-level cv-qualification is subsumed by the
1137 initialization itself and does not constitute a conversion. */
1141 Otherwise, the reference shall be to a non-volatile const type. */
1142 if (!CP_TYPE_CONST_NON_VOLATILE_P (to
))
1147 If the initializer expression is an rvalue, with T2 a class type,
1148 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1149 is bound in one of the following ways:
1151 -- The reference is bound to the object represented by the rvalue
1152 or to a sub-object within that object.
1154 In this case, the implicit conversion sequence is supposed to be
1155 same as we would obtain by generating a temporary. Fortunately,
1156 if the types are reference compatible, then this is either an
1157 identity conversion or the derived-to-base conversion, just as
1158 for direct binding. */
1159 if (CLASS_TYPE_P (from
) && compatible_p
)
1161 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1162 return direct_reference_binding (rto
, conv
);
1167 Otherwise, a temporary of type "cv1 T1" is created and
1168 initialized from the initializer expression using the rules for a
1169 non-reference copy initialization. If T1 is reference-related to
1170 T2, cv1 must be the same cv-qualification as, or greater
1171 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1172 if (related_p
&& !at_least_as_qualified_p (to
, from
))
1175 conv
= implicit_conversion (to
, from
, expr
, flags
);
1179 conv
= build_conv (REF_BIND
, rto
, conv
);
1180 /* This reference binding, unlike those above, requires the
1181 creation of a temporary. */
1182 NEED_TEMPORARY_P (conv
) = 1;
1187 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1188 to type TO. The optional expression EXPR may affect the conversion.
1189 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1193 implicit_conversion (to
, from
, expr
, flags
)
1194 tree to
, from
, expr
;
1198 struct z_candidate
*cand
;
1200 /* Resolve expressions like `A::p' that we thought might become
1201 pointers-to-members. */
1202 if (expr
&& TREE_CODE (expr
) == OFFSET_REF
)
1204 expr
= resolve_offset_ref (expr
);
1205 from
= TREE_TYPE (expr
);
1208 if (from
== error_mark_node
|| to
== error_mark_node
1209 || expr
== error_mark_node
)
1212 /* Make sure both the FROM and TO types are complete so that
1213 user-defined conversions are available. */
1214 complete_type (from
);
1217 if (TREE_CODE (to
) == REFERENCE_TYPE
)
1218 conv
= reference_binding (to
, from
, expr
, flags
);
1220 conv
= standard_conversion (to
, from
, expr
);
1224 else if (expr
!= NULL_TREE
1225 && (IS_AGGR_TYPE (non_reference (from
))
1226 || IS_AGGR_TYPE (non_reference (to
)))
1227 && (flags
& LOOKUP_NO_CONVERSION
) == 0)
1229 cand
= build_user_type_conversion_1
1230 (to
, expr
, LOOKUP_ONLYCONVERTING
);
1232 conv
= cand
->second_conv
;
1233 if ((! conv
|| ICS_BAD_FLAG (conv
))
1234 && TREE_CODE (to
) == REFERENCE_TYPE
1235 && (flags
& LOOKUP_NO_TEMP_BIND
) == 0)
1237 cand
= build_user_type_conversion_1
1238 (TYPE_MAIN_VARIANT (TREE_TYPE (to
)), expr
, LOOKUP_ONLYCONVERTING
);
1241 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (to
)))
1242 ICS_BAD_FLAG (cand
->second_conv
) = 1;
1243 if (!conv
|| (ICS_BAD_FLAG (conv
)
1244 > ICS_BAD_FLAG (cand
->second_conv
)))
1245 conv
= build_conv (REF_BIND
, to
, cand
->second_conv
);
1253 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1256 static struct z_candidate
*
1257 add_candidate (candidates
, fn
, convs
, viable
)
1258 struct z_candidate
*candidates
;
1262 struct z_candidate
*cand
1263 = (struct z_candidate
*) ggc_alloc_cleared (sizeof (struct z_candidate
));
1266 cand
->convs
= convs
;
1267 cand
->viable
= viable
;
1268 cand
->next
= candidates
;
1273 /* Create an overload candidate for the function or method FN called with
1274 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1275 to implicit_conversion.
1277 CTYPE, if non-NULL, is the type we want to pretend this function
1278 comes from for purposes of overload resolution. */
1280 static struct z_candidate
*
1281 add_function_candidate (candidates
, fn
, ctype
, arglist
, flags
)
1282 struct z_candidate
*candidates
;
1283 tree fn
, ctype
, arglist
;
1286 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1289 tree parmnode
, argnode
;
1292 /* The `this' and `in_chrg' arguments to constructors are not considered
1293 in overload resolution. */
1294 if (DECL_CONSTRUCTOR_P (fn
))
1296 parmlist
= TREE_CHAIN (parmlist
);
1297 arglist
= TREE_CHAIN (arglist
);
1298 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
1300 parmlist
= TREE_CHAIN (parmlist
);
1301 arglist
= TREE_CHAIN (arglist
);
1305 len
= list_length (arglist
);
1306 convs
= make_tree_vec (len
);
1308 /* 13.3.2 - Viable functions [over.match.viable]
1309 First, to be a viable function, a candidate function shall have enough
1310 parameters to agree in number with the arguments in the list.
1312 We need to check this first; otherwise, checking the ICSes might cause
1313 us to produce an ill-formed template instantiation. */
1315 parmnode
= parmlist
;
1316 for (i
= 0; i
< len
; ++i
)
1318 if (parmnode
== NULL_TREE
|| parmnode
== void_list_node
)
1320 parmnode
= TREE_CHAIN (parmnode
);
1323 if (i
< len
&& parmnode
)
1326 /* Make sure there are default args for the rest of the parms. */
1327 else for (; parmnode
&& parmnode
!= void_list_node
;
1328 parmnode
= TREE_CHAIN (parmnode
))
1329 if (! TREE_PURPOSE (parmnode
))
1338 /* Second, for F to be a viable function, there shall exist for each
1339 argument an implicit conversion sequence that converts that argument
1340 to the corresponding parameter of F. */
1342 parmnode
= parmlist
;
1345 for (i
= 0; i
< len
; ++i
)
1347 tree arg
= TREE_VALUE (argnode
);
1348 tree argtype
= lvalue_type (arg
);
1352 if (parmnode
== void_list_node
)
1355 is_this
= (i
== 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1356 && ! DECL_CONSTRUCTOR_P (fn
));
1360 tree parmtype
= TREE_VALUE (parmnode
);
1362 /* The type of the implicit object parameter ('this') for
1363 overload resolution is not always the same as for the
1364 function itself; conversion functions are considered to
1365 be members of the class being converted, and functions
1366 introduced by a using-declaration are considered to be
1367 members of the class that uses them.
1369 Since build_over_call ignores the ICS for the `this'
1370 parameter, we can just change the parm type. */
1371 if (ctype
&& is_this
)
1374 = build_qualified_type (ctype
,
1375 TYPE_QUALS (TREE_TYPE (parmtype
)));
1376 parmtype
= build_pointer_type (parmtype
);
1379 t
= implicit_conversion (parmtype
, argtype
, arg
, flags
);
1383 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1384 ICS_ELLIPSIS_FLAG (t
) = 1;
1388 ICS_THIS_FLAG (t
) = 1;
1390 TREE_VEC_ELT (convs
, i
) = t
;
1397 if (ICS_BAD_FLAG (t
))
1401 parmnode
= TREE_CHAIN (parmnode
);
1402 argnode
= TREE_CHAIN (argnode
);
1406 return add_candidate (candidates
, fn
, convs
, viable
);
1409 /* Create an overload candidate for the conversion function FN which will
1410 be invoked for expression OBJ, producing a pointer-to-function which
1411 will in turn be called with the argument list ARGLIST, and add it to
1412 CANDIDATES. FLAGS is passed on to implicit_conversion.
1414 Actually, we don't really care about FN; we care about the type it
1415 converts to. There may be multiple conversion functions that will
1416 convert to that type, and we rely on build_user_type_conversion_1 to
1417 choose the best one; so when we create our candidate, we record the type
1418 instead of the function. */
1420 static struct z_candidate
*
1421 add_conv_candidate (candidates
, fn
, obj
, arglist
)
1422 struct z_candidate
*candidates
;
1423 tree fn
, obj
, arglist
;
1425 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
1426 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (totype
));
1427 int i
, len
= list_length (arglist
) + 1;
1428 tree convs
= make_tree_vec (len
);
1429 tree parmnode
= parmlist
;
1430 tree argnode
= arglist
;
1432 int flags
= LOOKUP_NORMAL
;
1434 /* Don't bother looking up the same type twice. */
1435 if (candidates
&& candidates
->fn
== totype
)
1438 for (i
= 0; i
< len
; ++i
)
1440 tree arg
= i
== 0 ? obj
: TREE_VALUE (argnode
);
1441 tree argtype
= lvalue_type (arg
);
1445 t
= implicit_conversion (totype
, argtype
, arg
, flags
);
1446 else if (parmnode
== void_list_node
)
1449 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
, flags
);
1452 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
1453 ICS_ELLIPSIS_FLAG (t
) = 1;
1456 TREE_VEC_ELT (convs
, i
) = t
;
1460 if (ICS_BAD_FLAG (t
))
1467 parmnode
= TREE_CHAIN (parmnode
);
1468 argnode
= TREE_CHAIN (argnode
);
1474 for (; parmnode
&& parmnode
!= void_list_node
;
1475 parmnode
= TREE_CHAIN (parmnode
))
1476 if (! TREE_PURPOSE (parmnode
))
1482 return add_candidate (candidates
, totype
, convs
, viable
);
1485 static struct z_candidate
*
1486 build_builtin_candidate (candidates
, fnname
, type1
, type2
,
1487 args
, argtypes
, flags
)
1488 struct z_candidate
*candidates
;
1489 tree fnname
, type1
, type2
, *args
, *argtypes
;
1500 convs
= make_tree_vec (args
[2] ? 3 : (args
[1] ? 2 : 1));
1502 for (i
= 0; i
< 2; ++i
)
1507 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
], flags
);
1511 /* We need something for printing the candidate. */
1512 t
= build1 (IDENTITY_CONV
, types
[i
], NULL_TREE
);
1514 else if (ICS_BAD_FLAG (t
))
1516 TREE_VEC_ELT (convs
, i
) = t
;
1519 /* For COND_EXPR we rearranged the arguments; undo that now. */
1522 TREE_VEC_ELT (convs
, 2) = TREE_VEC_ELT (convs
, 1);
1523 TREE_VEC_ELT (convs
, 1) = TREE_VEC_ELT (convs
, 0);
1524 t
= implicit_conversion (boolean_type_node
, argtypes
[2], args
[2], flags
);
1526 TREE_VEC_ELT (convs
, 0) = t
;
1531 return add_candidate (candidates
, fnname
, convs
, viable
);
1538 return COMPLETE_TYPE_P (complete_type (t
));
1541 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1544 promoted_arithmetic_type_p (type
)
1549 In this section, the term promoted integral type is used to refer
1550 to those integral types which are preserved by integral promotion
1551 (including e.g. int and long but excluding e.g. char).
1552 Similarly, the term promoted arithmetic type refers to promoted
1553 integral types plus floating types. */
1554 return ((INTEGRAL_TYPE_P (type
)
1555 && same_type_p (type_promotes_to (type
), type
))
1556 || TREE_CODE (type
) == REAL_TYPE
);
1559 /* Create any builtin operator overload candidates for the operator in
1560 question given the converted operand types TYPE1 and TYPE2. The other
1561 args are passed through from add_builtin_candidates to
1562 build_builtin_candidate. */
1564 static struct z_candidate
*
1565 add_builtin_candidate (candidates
, code
, code2
, fnname
, type1
, type2
,
1566 args
, argtypes
, flags
)
1567 struct z_candidate
*candidates
;
1568 enum tree_code code
, code2
;
1569 tree fnname
, type1
, type2
, *args
, *argtypes
;
1574 case POSTINCREMENT_EXPR
:
1575 case POSTDECREMENT_EXPR
:
1576 args
[1] = integer_zero_node
;
1577 type2
= integer_type_node
;
1586 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1587 and VQ is either volatile or empty, there exist candidate operator
1588 functions of the form
1589 VQ T& operator++(VQ T&);
1590 T operator++(VQ T&, int);
1591 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1592 type other than bool, and VQ is either volatile or empty, there exist
1593 candidate operator functions of the form
1594 VQ T& operator--(VQ T&);
1595 T operator--(VQ T&, int);
1596 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1597 complete object type, and VQ is either volatile or empty, there exist
1598 candidate operator functions of the form
1599 T*VQ& operator++(T*VQ&);
1600 T*VQ& operator--(T*VQ&);
1601 T* operator++(T*VQ&, int);
1602 T* operator--(T*VQ&, int); */
1604 case POSTDECREMENT_EXPR
:
1605 case PREDECREMENT_EXPR
:
1606 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
1608 case POSTINCREMENT_EXPR
:
1609 case PREINCREMENT_EXPR
:
1610 if ((ARITHMETIC_TYPE_P (type1
) && TREE_CODE (type1
) != ENUMERAL_TYPE
)
1611 || TYPE_PTROB_P (type1
))
1613 type1
= build_reference_type (type1
);
1618 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1619 exist candidate operator functions of the form
1623 8 For every function type T, there exist candidate operator functions of
1625 T& operator*(T*); */
1628 if (TREE_CODE (type1
) == POINTER_TYPE
1629 && (TYPE_PTROB_P (type1
)
1630 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
1634 /* 9 For every type T, there exist candidate operator functions of the form
1637 10For every promoted arithmetic type T, there exist candidate operator
1638 functions of the form
1642 case CONVERT_EXPR
: /* unary + */
1643 if (TREE_CODE (type1
) == POINTER_TYPE
1644 && TREE_CODE (TREE_TYPE (type1
)) != OFFSET_TYPE
)
1647 if (ARITHMETIC_TYPE_P (type1
))
1651 /* 11For every promoted integral type T, there exist candidate operator
1652 functions of the form
1656 if (INTEGRAL_TYPE_P (type1
))
1660 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1661 is the same type as C2 or is a derived class of C2, T is a complete
1662 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1663 there exist candidate operator functions of the form
1664 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1665 where CV12 is the union of CV1 and CV2. */
1668 if (TREE_CODE (type1
) == POINTER_TYPE
1669 && (TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
)))
1671 tree c1
= TREE_TYPE (type1
);
1672 tree c2
= (TYPE_PTRMEMFUNC_P (type2
)
1673 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2
)))
1674 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2
)));
1676 if (IS_AGGR_TYPE (c1
) && DERIVED_FROM_P (c2
, c1
)
1677 && (TYPE_PTRMEMFUNC_P (type2
)
1678 || is_complete (TREE_TYPE (TREE_TYPE (type2
)))))
1683 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1684 didate operator functions of the form
1689 bool operator<(L, R);
1690 bool operator>(L, R);
1691 bool operator<=(L, R);
1692 bool operator>=(L, R);
1693 bool operator==(L, R);
1694 bool operator!=(L, R);
1695 where LR is the result of the usual arithmetic conversions between
1698 14For every pair of types T and I, where T is a cv-qualified or cv-
1699 unqualified complete object type and I is a promoted integral type,
1700 there exist candidate operator functions of the form
1701 T* operator+(T*, I);
1702 T& operator[](T*, I);
1703 T* operator-(T*, I);
1704 T* operator+(I, T*);
1705 T& operator[](I, T*);
1707 15For every T, where T is a pointer to complete object type, there exist
1708 candidate operator functions of the form112)
1709 ptrdiff_t operator-(T, T);
1711 16For every pointer type T, there exist candidate operator functions of
1713 bool operator<(T, T);
1714 bool operator>(T, T);
1715 bool operator<=(T, T);
1716 bool operator>=(T, T);
1717 bool operator==(T, T);
1718 bool operator!=(T, T);
1720 17For every pointer to member type T, there exist candidate operator
1721 functions of the form
1722 bool operator==(T, T);
1723 bool operator!=(T, T); */
1726 if (TYPE_PTROB_P (type1
) && TYPE_PTROB_P (type2
))
1728 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1730 type2
= ptrdiff_type_node
;
1734 case TRUNC_DIV_EXPR
:
1735 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1741 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1742 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
)))
1744 if ((TYPE_PTRMEMFUNC_P (type1
) || TYPE_PTRMEM_P (type1
))
1745 && null_ptr_cst_p (args
[1]))
1750 if ((TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
))
1751 && null_ptr_cst_p (args
[0]))
1762 if ((ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1763 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
)))
1765 if (TYPE_PTR_P (type1
) && null_ptr_cst_p (args
[1]))
1770 if (null_ptr_cst_p (args
[0]) && TYPE_PTR_P (type2
))
1778 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1781 if (INTEGRAL_TYPE_P (type1
) && TYPE_PTROB_P (type2
))
1783 type1
= ptrdiff_type_node
;
1786 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1788 type2
= ptrdiff_type_node
;
1793 /* 18For every pair of promoted integral types L and R, there exist candi-
1794 date operator functions of the form
1801 where LR is the result of the usual arithmetic conversions between
1804 case TRUNC_MOD_EXPR
:
1810 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1814 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1815 type, VQ is either volatile or empty, and R is a promoted arithmetic
1816 type, there exist candidate operator functions of the form
1817 VQ L& operator=(VQ L&, R);
1818 VQ L& operator*=(VQ L&, R);
1819 VQ L& operator/=(VQ L&, R);
1820 VQ L& operator+=(VQ L&, R);
1821 VQ L& operator-=(VQ L&, R);
1823 20For every pair T, VQ), where T is any type and VQ is either volatile
1824 or empty, there exist candidate operator functions of the form
1825 T*VQ& operator=(T*VQ&, T*);
1827 21For every pair T, VQ), where T is a pointer to member type and VQ is
1828 either volatile or empty, there exist candidate operator functions of
1830 VQ T& operator=(VQ T&, T);
1832 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1833 unqualified complete object type, VQ is either volatile or empty, and
1834 I is a promoted integral type, there exist candidate operator func-
1836 T*VQ& operator+=(T*VQ&, I);
1837 T*VQ& operator-=(T*VQ&, I);
1839 23For every triple L, VQ, R), where L is an integral or enumeration
1840 type, VQ is either volatile or empty, and R is a promoted integral
1841 type, there exist candidate operator functions of the form
1843 VQ L& operator%=(VQ L&, R);
1844 VQ L& operator<<=(VQ L&, R);
1845 VQ L& operator>>=(VQ L&, R);
1846 VQ L& operator&=(VQ L&, R);
1847 VQ L& operator^=(VQ L&, R);
1848 VQ L& operator|=(VQ L&, R); */
1855 if (TYPE_PTROB_P (type1
) && INTEGRAL_TYPE_P (type2
))
1857 type2
= ptrdiff_type_node
;
1861 case TRUNC_DIV_EXPR
:
1862 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1866 case TRUNC_MOD_EXPR
:
1872 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
1877 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
1879 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
1880 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1881 || (TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
))
1882 || ((TYPE_PTRMEMFUNC_P (type1
)
1883 || TREE_CODE (type1
) == POINTER_TYPE
)
1884 && null_ptr_cst_p (args
[1])))
1892 my_friendly_abort (367);
1894 type1
= build_reference_type (type1
);
1900 For every pair of promoted arithmetic types L and R, there
1901 exist candidate operator functions of the form
1903 LR operator?(bool, L, R);
1905 where LR is the result of the usual arithmetic conversions
1906 between types L and R.
1908 For every type T, where T is a pointer or pointer-to-member
1909 type, there exist candidate operator functions of the form T
1910 operator?(bool, T, T); */
1912 if (promoted_arithmetic_type_p (type1
)
1913 && promoted_arithmetic_type_p (type2
))
1917 /* Otherwise, the types should be pointers. */
1918 if (!(TREE_CODE (type1
) == POINTER_TYPE
1919 || TYPE_PTRMEM_P (type1
)
1920 || TYPE_PTRMEMFUNC_P (type1
))
1921 || !(TREE_CODE (type2
) == POINTER_TYPE
1922 || TYPE_PTRMEM_P (type2
)
1923 || TYPE_PTRMEMFUNC_P (type2
)))
1926 /* We don't check that the two types are the same; the logic
1927 below will actually create two candidates; one in which both
1928 parameter types are TYPE1, and one in which both parameter
1932 /* These arguments do not make for a legal overloaded operator. */
1936 my_friendly_abort (367);
1939 /* If we're dealing with two pointer types, we need candidates
1940 for both of them. */
1941 if (type2
&& !same_type_p (type1
, type2
)
1942 && TREE_CODE (type1
) == TREE_CODE (type2
)
1943 && (TREE_CODE (type1
) == REFERENCE_TYPE
1944 || (TREE_CODE (type1
) == POINTER_TYPE
1945 && TYPE_PTRMEM_P (type1
) == TYPE_PTRMEM_P (type2
))
1946 || TYPE_PTRMEMFUNC_P (type1
)
1947 || IS_AGGR_TYPE (type1
)))
1949 candidates
= build_builtin_candidate
1950 (candidates
, fnname
, type1
, type1
, args
, argtypes
, flags
);
1951 return build_builtin_candidate
1952 (candidates
, fnname
, type2
, type2
, args
, argtypes
, flags
);
1955 return build_builtin_candidate
1956 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
);
1960 type_decays_to (type
)
1963 if (TREE_CODE (type
) == ARRAY_TYPE
)
1964 return build_pointer_type (TREE_TYPE (type
));
1965 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1966 return build_pointer_type (type
);
1970 /* There are three conditions of builtin candidates:
1972 1) bool-taking candidates. These are the same regardless of the input.
1973 2) pointer-pair taking candidates. These are generated for each type
1974 one of the input types converts to.
1975 3) arithmetic candidates. According to the standard, we should generate
1976 all of these, but I'm trying not to... */
1978 static struct z_candidate
*
1979 add_builtin_candidates (candidates
, code
, code2
, fnname
, args
, flags
)
1980 struct z_candidate
*candidates
;
1981 enum tree_code code
, code2
;
1986 tree type
, argtypes
[3];
1987 /* TYPES[i] is the set of possible builtin-operator parameter types
1988 we will consider for the Ith argument. These are represented as
1989 a TREE_LIST; the TREE_VALUE of each node is the potential
1993 for (i
= 0; i
< 3; ++i
)
1996 argtypes
[i
] = lvalue_type (args
[i
]);
1998 argtypes
[i
] = NULL_TREE
;
2003 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2004 and VQ is either volatile or empty, there exist candidate operator
2005 functions of the form
2006 VQ T& operator++(VQ T&); */
2008 case POSTINCREMENT_EXPR
:
2009 case PREINCREMENT_EXPR
:
2010 case POSTDECREMENT_EXPR
:
2011 case PREDECREMENT_EXPR
:
2016 /* 24There also exist candidate operator functions of the form
2017 bool operator!(bool);
2018 bool operator&&(bool, bool);
2019 bool operator||(bool, bool); */
2021 case TRUTH_NOT_EXPR
:
2022 return build_builtin_candidate
2023 (candidates
, fnname
, boolean_type_node
,
2024 NULL_TREE
, args
, argtypes
, flags
);
2026 case TRUTH_ORIF_EXPR
:
2027 case TRUTH_ANDIF_EXPR
:
2028 return build_builtin_candidate
2029 (candidates
, fnname
, boolean_type_node
,
2030 boolean_type_node
, args
, argtypes
, flags
);
2041 types
[0] = types
[1] = NULL_TREE
;
2043 for (i
= 0; i
< 2; ++i
)
2047 else if (IS_AGGR_TYPE (argtypes
[i
]))
2051 if (i
== 0 && code
== MODIFY_EXPR
&& code2
== NOP_EXPR
)
2054 convs
= lookup_conversions (argtypes
[i
]);
2056 if (code
== COND_EXPR
)
2058 if (real_lvalue_p (args
[i
]))
2059 types
[i
] = tree_cons
2060 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2062 types
[i
] = tree_cons
2063 (NULL_TREE
, TYPE_MAIN_VARIANT (argtypes
[i
]), types
[i
]);
2069 for (; convs
; convs
= TREE_CHAIN (convs
))
2071 type
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs
))));
2074 && (TREE_CODE (type
) != REFERENCE_TYPE
2075 || CP_TYPE_CONST_P (TREE_TYPE (type
))))
2078 if (code
== COND_EXPR
&& TREE_CODE (type
) == REFERENCE_TYPE
)
2079 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2081 type
= non_reference (type
);
2082 if (i
!= 0 || ! ref1
)
2084 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2085 if (code
== COND_EXPR
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2086 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2087 if (INTEGRAL_TYPE_P (type
))
2088 type
= type_promotes_to (type
);
2091 if (! value_member (type
, types
[i
]))
2092 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2097 if (code
== COND_EXPR
&& real_lvalue_p (args
[i
]))
2098 types
[i
] = tree_cons
2099 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2100 type
= non_reference (argtypes
[i
]);
2101 if (i
!= 0 || ! ref1
)
2103 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
2104 if (code
== COND_EXPR
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
2105 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2106 if (INTEGRAL_TYPE_P (type
))
2107 type
= type_promotes_to (type
);
2109 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2113 /* Run through the possible parameter types of both arguments,
2114 creating candidates with those parameter types. */
2115 for (; types
[0]; types
[0] = TREE_CHAIN (types
[0]))
2118 for (type
= types
[1]; type
; type
= TREE_CHAIN (type
))
2119 candidates
= add_builtin_candidate
2120 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2121 TREE_VALUE (type
), args
, argtypes
, flags
);
2123 candidates
= add_builtin_candidate
2124 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2125 NULL_TREE
, args
, argtypes
, flags
);
2132 /* If TMPL can be successfully instantiated as indicated by
2133 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2135 TMPL is the template. EXPLICIT_TARGS are any explicit template
2136 arguments. ARGLIST is the arguments provided at the call-site.
2137 The RETURN_TYPE is the desired type for conversion operators. If
2138 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2139 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2140 add_conv_candidate. */
2142 static struct z_candidate
*
2143 add_template_candidate_real (candidates
, tmpl
, ctype
, explicit_targs
,
2144 arglist
, return_type
, flags
,
2146 struct z_candidate
*candidates
;
2147 tree tmpl
, ctype
, explicit_targs
, arglist
, return_type
;
2150 unification_kind_t strict
;
2152 int ntparms
= DECL_NTPARMS (tmpl
);
2153 tree targs
= make_tree_vec (ntparms
);
2154 tree args_without_in_chrg
;
2155 struct z_candidate
*cand
;
2159 /* TEMPLATE_DECLs do not have the in-charge parameter, nor the VTT
2160 parameter. So, skip it here before attempting to perform
2161 argument deduction. */
2162 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl
)
2163 || DECL_BASE_CONSTRUCTOR_P (tmpl
))
2164 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl
)))
2165 args_without_in_chrg
= tree_cons (NULL_TREE
,
2166 TREE_VALUE (arglist
),
2167 TREE_CHAIN (TREE_CHAIN (arglist
)));
2169 args_without_in_chrg
= arglist
;
2171 i
= fn_type_unification (tmpl
, explicit_targs
, targs
,
2172 args_without_in_chrg
,
2173 return_type
, strict
);
2178 fn
= instantiate_template (tmpl
, targs
);
2179 if (fn
== error_mark_node
)
2182 if (obj
!= NULL_TREE
)
2183 /* Aha, this is a conversion function. */
2184 cand
= add_conv_candidate (candidates
, fn
, obj
, arglist
);
2186 cand
= add_function_candidate (candidates
, fn
, ctype
,
2188 if (DECL_TI_TEMPLATE (fn
) != tmpl
)
2189 /* This situation can occur if a member template of a template
2190 class is specialized. Then, instantiate_template might return
2191 an instantiation of the specialization, in which case the
2192 DECL_TI_TEMPLATE field will point at the original
2193 specialization. For example:
2195 template <class T> struct S { template <class U> void f(U);
2196 template <> void f(int) {}; };
2200 Here, TMPL will be template <class U> S<double>::f(U).
2201 And, instantiate template will give us the specialization
2202 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2203 for this will point at template <class T> template <> S<T>::f(int),
2204 so that we can find the definition. For the purposes of
2205 overload resolution, however, we want the original TMPL. */
2206 cand
->template = tree_cons (tmpl
, targs
, NULL_TREE
);
2208 cand
->template = DECL_TEMPLATE_INFO (fn
);
2214 static struct z_candidate
*
2215 add_template_candidate (candidates
, tmpl
, ctype
, explicit_targs
,
2216 arglist
, return_type
, flags
, strict
)
2217 struct z_candidate
*candidates
;
2218 tree tmpl
, ctype
, explicit_targs
, arglist
, return_type
;
2220 unification_kind_t strict
;
2223 add_template_candidate_real (candidates
, tmpl
, ctype
,
2224 explicit_targs
, arglist
, return_type
, flags
,
2229 static struct z_candidate
*
2230 add_template_conv_candidate (candidates
, tmpl
, obj
, arglist
, return_type
)
2231 struct z_candidate
*candidates
;
2232 tree tmpl
, obj
, arglist
, return_type
;
2235 add_template_candidate_real (candidates
, tmpl
, NULL_TREE
, NULL_TREE
,
2236 arglist
, return_type
, 0, obj
, DEDUCE_CONV
);
2242 struct z_candidate
*cands
;
2244 for (; cands
; cands
= cands
->next
)
2245 if (pedantic
? cands
->viable
== 1 : cands
->viable
)
2250 static struct z_candidate
*
2251 splice_viable (cands
)
2252 struct z_candidate
*cands
;
2254 struct z_candidate
**p
= &cands
;
2258 if (pedantic
? (*p
)->viable
== 1 : (*p
)->viable
)
2271 /* Fix this to work on non-lvalues. */
2272 return build_unary_op (ADDR_EXPR
, obj
, 0);
2276 print_z_candidates (candidates
)
2277 struct z_candidate
*candidates
;
2279 const char *str
= "candidates are:";
2280 for (; candidates
; candidates
= candidates
->next
)
2282 if (TREE_CODE (candidates
->fn
) == IDENTIFIER_NODE
)
2284 if (TREE_VEC_LENGTH (candidates
->convs
) == 3)
2285 cp_error ("%s %D(%T, %T, %T) <builtin>", str
, candidates
->fn
,
2286 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2287 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)),
2288 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 2)));
2289 else if (TREE_VEC_LENGTH (candidates
->convs
) == 2)
2290 cp_error ("%s %D(%T, %T) <builtin>", str
, candidates
->fn
,
2291 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)),
2292 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 1)));
2294 cp_error ("%s %D(%T) <builtin>", str
, candidates
->fn
,
2295 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)));
2297 else if (TYPE_P (candidates
->fn
))
2298 cp_error ("%s %T <conversion>", str
, candidates
->fn
);
2300 cp_error_at ("%s %+#D%s", str
, candidates
->fn
,
2301 candidates
->viable
== -1 ? " <near match>" : "");
2306 /* Returns the best overload candidate to perform the requested
2307 conversion. This function is used for three the overloading situations
2308 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2309 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2310 per [dcl.init.ref], so we ignore temporary bindings. */
2312 static struct z_candidate
*
2313 build_user_type_conversion_1 (totype
, expr
, flags
)
2317 struct z_candidate
*candidates
, *cand
;
2318 tree fromtype
= TREE_TYPE (expr
);
2319 tree ctors
= NULL_TREE
, convs
= NULL_TREE
, *p
;
2320 tree args
= NULL_TREE
;
2321 tree templates
= NULL_TREE
;
2323 if (IS_AGGR_TYPE (totype
))
2324 ctors
= lookup_fnfields (TYPE_BINFO (totype
),
2326 ? complete_ctor_identifier
2330 if (IS_AGGR_TYPE (fromtype
)
2331 && (! IS_AGGR_TYPE (totype
) || ! DERIVED_FROM_P (totype
, fromtype
)))
2332 convs
= lookup_conversions (fromtype
);
2335 flags
|= LOOKUP_NO_CONVERSION
;
2341 ctors
= TREE_VALUE (ctors
);
2343 t
= build_int_2 (0, 0);
2344 TREE_TYPE (t
) = build_pointer_type (totype
);
2345 args
= build_tree_list (NULL_TREE
, expr
);
2346 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors
)))
2347 args
= tree_cons (NULL_TREE
,
2348 in_charge_arg_for_name (complete_ctor_identifier
),
2350 args
= tree_cons (NULL_TREE
, t
, args
);
2352 for (; ctors
; ctors
= OVL_NEXT (ctors
))
2354 tree ctor
= OVL_CURRENT (ctors
);
2355 if (DECL_NONCONVERTING_P (ctor
))
2358 if (TREE_CODE (ctor
) == TEMPLATE_DECL
)
2360 templates
= tree_cons (NULL_TREE
, ctor
, templates
);
2362 add_template_candidate (candidates
, ctor
, totype
,
2363 NULL_TREE
, args
, NULL_TREE
, flags
,
2367 candidates
= add_function_candidate (candidates
, ctor
, totype
,
2372 candidates
->second_conv
= build1 (IDENTITY_CONV
, totype
, NULL_TREE
);
2373 candidates
->basetype_path
= TYPE_BINFO (totype
);
2378 args
= build_tree_list (NULL_TREE
, build_this (expr
));
2380 for (; convs
; convs
= TREE_CHAIN (convs
))
2382 tree fns
= TREE_VALUE (convs
);
2383 int convflags
= LOOKUP_NO_CONVERSION
;
2386 /* If we are called to convert to a reference type, we are trying to
2387 find an lvalue binding, so don't even consider temporaries. If
2388 we don't find an lvalue binding, the caller will try again to
2389 look for a temporary binding. */
2390 if (TREE_CODE (totype
) == REFERENCE_TYPE
)
2391 convflags
|= LOOKUP_NO_TEMP_BIND
;
2393 if (TREE_CODE (OVL_CURRENT (fns
)) != TEMPLATE_DECL
)
2394 ics
= implicit_conversion
2395 (totype
, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns
))), 0, convflags
);
2397 /* We can't compute this yet. */
2398 ics
= error_mark_node
;
2400 if (TREE_CODE (totype
) == REFERENCE_TYPE
&& ics
&& ICS_BAD_FLAG (ics
))
2401 /* ignore the near match. */;
2403 for (; fns
; fns
= OVL_NEXT (fns
))
2405 tree fn
= OVL_CURRENT (fns
);
2406 struct z_candidate
*old_candidates
= candidates
;
2408 /* [over.match.funcs] For conversion functions, the function is
2409 considered to be a member of the class of the implicit object
2410 argument for the purpose of defining the type of the implicit
2413 So we pass fromtype as CTYPE to add_*_candidate. */
2415 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2417 templates
= tree_cons (NULL_TREE
, fn
, templates
);
2419 add_template_candidate (candidates
, fn
, fromtype
, NULL_TREE
,
2420 args
, totype
, flags
,
2424 candidates
= add_function_candidate (candidates
, fn
, fromtype
,
2427 if (candidates
!= old_candidates
)
2429 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2430 ics
= implicit_conversion
2431 (totype
, TREE_TYPE (TREE_TYPE (candidates
->fn
)),
2434 candidates
->second_conv
= ics
;
2435 candidates
->basetype_path
= TYPE_BINFO (fromtype
);
2437 if (ics
== NULL_TREE
)
2438 candidates
->viable
= 0;
2439 else if (candidates
->viable
== 1 && ICS_BAD_FLAG (ics
))
2440 candidates
->viable
= -1;
2445 if (! any_viable (candidates
))
2448 if (flags
& LOOKUP_COMPLAIN
)
2450 if (candidates
&& ! candidates
->next
)
2451 /* say why this one won't work or try to be loose */;
2453 cp_error ("no viable candidates");
2460 candidates
= splice_viable (candidates
);
2461 cand
= tourney (candidates
);
2465 if (flags
& LOOKUP_COMPLAIN
)
2467 cp_error ("conversion from `%T' to `%T' is ambiguous",
2469 print_z_candidates (candidates
);
2472 cand
= candidates
; /* any one will do */
2473 cand
->second_conv
= build1 (AMBIG_CONV
, totype
, expr
);
2474 ICS_USER_FLAG (cand
->second_conv
) = 1;
2475 ICS_BAD_FLAG (cand
->second_conv
) = 1;
2480 for (p
= &(cand
->second_conv
); TREE_CODE (*p
) != IDENTITY_CONV
; )
2481 p
= &(TREE_OPERAND (*p
, 0));
2485 (DECL_CONSTRUCTOR_P (cand
->fn
)
2486 ? totype
: non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
)))),
2487 expr
, build_expr_ptr_wrapper (cand
));
2488 ICS_USER_FLAG (cand
->second_conv
) = 1;
2489 if (cand
->viable
== -1)
2490 ICS_BAD_FLAG (cand
->second_conv
) = 1;
2496 build_user_type_conversion (totype
, expr
, flags
)
2500 struct z_candidate
*cand
2501 = build_user_type_conversion_1 (totype
, expr
, flags
);
2505 if (TREE_CODE (cand
->second_conv
) == AMBIG_CONV
)
2506 return error_mark_node
;
2507 return convert_from_reference
2508 (convert_like_with_context
2509 (cand
->second_conv
, expr
, cand
->fn
, 0));
2514 /* Do any initial processing on the arguments to a function call. */
2521 for (t
= args
; t
; t
= TREE_CHAIN (t
))
2523 if (TREE_VALUE (t
) == error_mark_node
)
2524 return error_mark_node
;
2525 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t
))) == VOID_TYPE
)
2527 error ("invalid use of void expression");
2528 return error_mark_node
;
2530 else if (TREE_CODE (TREE_VALUE (t
)) == OFFSET_REF
)
2531 TREE_VALUE (t
) = resolve_offset_ref (TREE_VALUE (t
));
2537 build_new_function_call (fn
, args
)
2540 struct z_candidate
*candidates
= 0, *cand
;
2541 tree explicit_targs
= NULL_TREE
;
2542 int template_only
= 0;
2544 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2546 explicit_targs
= TREE_OPERAND (fn
, 1);
2547 fn
= TREE_OPERAND (fn
, 0);
2551 if (really_overloaded_fn (fn
))
2554 tree templates
= NULL_TREE
;
2556 args
= resolve_args (args
);
2558 if (args
== error_mark_node
)
2559 return error_mark_node
;
2561 for (t1
= fn
; t1
; t1
= OVL_CHAIN (t1
))
2563 tree t
= OVL_FUNCTION (t1
);
2565 if (TREE_CODE (t
) == TEMPLATE_DECL
)
2567 templates
= tree_cons (NULL_TREE
, t
, templates
);
2568 candidates
= add_template_candidate
2569 (candidates
, t
, NULL_TREE
, explicit_targs
, args
, NULL_TREE
,
2570 LOOKUP_NORMAL
, DEDUCE_CALL
);
2572 else if (! template_only
)
2573 candidates
= add_function_candidate
2574 (candidates
, t
, NULL_TREE
, args
, LOOKUP_NORMAL
);
2577 if (! any_viable (candidates
))
2579 if (candidates
&& ! candidates
->next
)
2580 return build_function_call (candidates
->fn
, args
);
2581 cp_error ("no matching function for call to `%D (%A)'",
2582 DECL_NAME (OVL_FUNCTION (fn
)), args
);
2584 print_z_candidates (candidates
);
2585 return error_mark_node
;
2587 candidates
= splice_viable (candidates
);
2588 cand
= tourney (candidates
);
2592 cp_error ("call of overloaded `%D (%A)' is ambiguous",
2593 DECL_NAME (OVL_FUNCTION (fn
)), args
);
2594 print_z_candidates (candidates
);
2595 return error_mark_node
;
2598 return build_over_call (cand
, args
, LOOKUP_NORMAL
);
2601 /* This is not really overloaded. */
2602 fn
= OVL_CURRENT (fn
);
2604 return build_function_call (fn
, args
);
2608 build_object_call (obj
, args
)
2611 struct z_candidate
*candidates
= 0, *cand
;
2612 tree fns
, convs
, mem_args
= NULL_TREE
;
2613 tree type
= TREE_TYPE (obj
);
2615 if (TYPE_PTRMEMFUNC_P (type
))
2617 /* It's no good looking for an overloaded operator() on a
2618 pointer-to-member-function. */
2619 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj
);
2620 return error_mark_node
;
2623 fns
= lookup_fnfields (TYPE_BINFO (type
), ansi_opname (CALL_EXPR
), 1);
2624 if (fns
== error_mark_node
)
2625 return error_mark_node
;
2627 args
= resolve_args (args
);
2629 if (args
== error_mark_node
)
2630 return error_mark_node
;
2634 tree base
= BINFO_TYPE (TREE_PURPOSE (fns
));
2635 mem_args
= tree_cons (NULL_TREE
, build_this (obj
), args
);
2637 for (fns
= TREE_VALUE (fns
); fns
; fns
= OVL_NEXT (fns
))
2639 tree fn
= OVL_CURRENT (fns
);
2640 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2643 = add_template_candidate (candidates
, fn
, base
, NULL_TREE
,
2644 mem_args
, NULL_TREE
,
2645 LOOKUP_NORMAL
, DEDUCE_CALL
);
2648 candidates
= add_function_candidate
2649 (candidates
, fn
, base
, mem_args
, LOOKUP_NORMAL
);
2652 candidates
->basetype_path
= TYPE_BINFO (type
);
2656 convs
= lookup_conversions (type
);
2658 for (; convs
; convs
= TREE_CHAIN (convs
))
2660 tree fns
= TREE_VALUE (convs
);
2661 tree totype
= TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns
)));
2663 if ((TREE_CODE (totype
) == POINTER_TYPE
2664 || TREE_CODE (totype
) == REFERENCE_TYPE
)
2665 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
2666 for (; fns
; fns
= OVL_NEXT (fns
))
2668 tree fn
= OVL_CURRENT (fns
);
2669 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2671 candidates
= add_template_conv_candidate (candidates
,
2678 candidates
= add_conv_candidate (candidates
, fn
, obj
, args
);
2682 if (! any_viable (candidates
))
2684 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj
), args
);
2685 print_z_candidates (candidates
);
2686 return error_mark_node
;
2689 candidates
= splice_viable (candidates
);
2690 cand
= tourney (candidates
);
2694 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj
), args
);
2695 print_z_candidates (candidates
);
2696 return error_mark_node
;
2699 /* Since cand->fn will be a type, not a function, for a conversion
2700 function, we must be careful not to unconditionally look at
2702 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
2703 && DECL_OVERLOADED_OPERATOR_P (cand
->fn
) == CALL_EXPR
)
2704 return build_over_call (cand
, mem_args
, LOOKUP_NORMAL
);
2706 obj
= convert_like_with_context
2707 (TREE_VEC_ELT (cand
->convs
, 0), obj
, cand
->fn
, -1);
2710 return build_function_call (obj
, args
);
2714 op_error (code
, code2
, arg1
, arg2
, arg3
, problem
)
2715 enum tree_code code
, code2
;
2716 tree arg1
, arg2
, arg3
;
2717 const char *problem
;
2721 if (code
== MODIFY_EXPR
)
2722 opname
= assignment_operator_name_info
[code2
].name
;
2724 opname
= operator_name_info
[code
].name
;
2729 cp_error ("%s for `%T ? %T : %T' operator", problem
,
2730 error_type (arg1
), error_type (arg2
), error_type (arg3
));
2732 case POSTINCREMENT_EXPR
:
2733 case POSTDECREMENT_EXPR
:
2734 cp_error ("%s for `%T %s' operator", problem
, error_type (arg1
), opname
);
2737 cp_error ("%s for `%T [%T]' operator", problem
,
2738 error_type (arg1
), error_type (arg2
));
2742 cp_error ("%s for `%T %s %T' operator", problem
,
2743 error_type (arg1
), opname
, error_type (arg2
));
2745 cp_error ("%s for `%s %T' operator", problem
, opname
, error_type (arg1
));
2749 /* Return the implicit conversion sequence that could be used to
2750 convert E1 to E2 in [expr.cond]. */
2753 conditional_conversion (e1
, e2
)
2757 tree t1
= non_reference (TREE_TYPE (e1
));
2758 tree t2
= non_reference (TREE_TYPE (e2
));
2763 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2764 implicitly converted (clause _conv_) to the type "reference to
2765 T2", subject to the constraint that in the conversion the
2766 reference must bind directly (_dcl.init.ref_) to E1. */
2767 if (real_lvalue_p (e2
))
2769 conv
= implicit_conversion (build_reference_type (t2
),
2772 LOOKUP_NO_TEMP_BIND
);
2779 If E1 and E2 have class type, and the underlying class types are
2780 the same or one is a base class of the other: E1 can be converted
2781 to match E2 if the class of T2 is the same type as, or a base
2782 class of, the class of T1, and the cv-qualification of T2 is the
2783 same cv-qualification as, or a greater cv-qualification than, the
2784 cv-qualification of T1. If the conversion is applied, E1 is
2785 changed to an rvalue of type T2 that still refers to the original
2786 source class object (or the appropriate subobject thereof). */
2787 if (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
2788 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2
),
2789 TYPE_MAIN_VARIANT (t1
)))
2791 if (at_least_as_qualified_p (t2
, t1
))
2793 conv
= build1 (IDENTITY_CONV
, t1
, e1
);
2794 if (!same_type_p (TYPE_MAIN_VARIANT (t1
),
2795 TYPE_MAIN_VARIANT (t2
)))
2796 conv
= build_conv (BASE_CONV
, t2
, conv
);
2805 E1 can be converted to match E2 if E1 can be implicitly converted
2806 to the type that expression E2 would have if E2 were converted to
2807 an rvalue (or the type it has, if E2 is an rvalue). */
2808 return implicit_conversion (t2
, t1
, e1
, LOOKUP_NORMAL
);
2811 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2812 arguments to the conditional expression. By the time this function
2813 is called, any suitable candidate functions are included in
2817 build_conditional_expr (arg1
, arg2
, arg3
)
2825 tree result_type
= NULL_TREE
;
2827 struct z_candidate
*candidates
= 0;
2828 struct z_candidate
*cand
;
2830 /* As a G++ extension, the second argument to the conditional can be
2831 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2832 c'.) If the second operand is omitted, make sure it is
2833 calculated only once. */
2837 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2838 arg1
= arg2
= save_expr (arg1
);
2843 The first expr ession is implicitly converted to bool (clause
2845 arg1
= cp_convert (boolean_type_node
, arg1
);
2847 /* If something has already gone wrong, just pass that fact up the
2849 if (arg1
== error_mark_node
2850 || arg2
== error_mark_node
2851 || arg3
== error_mark_node
2852 || TREE_TYPE (arg1
) == error_mark_node
2853 || TREE_TYPE (arg2
) == error_mark_node
2854 || TREE_TYPE (arg3
) == error_mark_node
)
2855 return error_mark_node
;
2857 /* Convert from reference types to ordinary types; no expressions
2858 really have reference type in C++. */
2859 arg2
= convert_from_reference (arg2
);
2860 arg3
= convert_from_reference (arg3
);
2864 If either the second or the third operand has type (possibly
2865 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2866 array-to-pointer (_conv.array_), and function-to-pointer
2867 (_conv.func_) standard conversions are performed on the second
2868 and third operands. */
2869 arg2_type
= TREE_TYPE (arg2
);
2870 arg3_type
= TREE_TYPE (arg3
);
2871 if (VOID_TYPE_P (arg2_type
) || VOID_TYPE_P (arg3_type
))
2873 /* Do the conversions. We don't these for `void' type arguments
2874 since it can't have any effect and since decay_conversion
2875 does not handle that case gracefully. */
2876 if (!VOID_TYPE_P (arg2_type
))
2877 arg2
= decay_conversion (arg2
);
2878 if (!VOID_TYPE_P (arg3_type
))
2879 arg3
= decay_conversion (arg3
);
2880 arg2_type
= TREE_TYPE (arg2
);
2881 arg3_type
= TREE_TYPE (arg3
);
2885 One of the following shall hold:
2887 --The second or the third operand (but not both) is a
2888 throw-expression (_except.throw_); the result is of the
2889 type of the other and is an rvalue.
2891 --Both the second and the third operands have type void; the
2892 result is of type void and is an rvalue. */
2893 if ((TREE_CODE (arg2
) == THROW_EXPR
)
2894 ^ (TREE_CODE (arg3
) == THROW_EXPR
))
2895 result_type
= ((TREE_CODE (arg2
) == THROW_EXPR
)
2896 ? arg3_type
: arg2_type
);
2897 else if (VOID_TYPE_P (arg2_type
) && VOID_TYPE_P (arg3_type
))
2898 result_type
= void_type_node
;
2901 cp_error ("`%E' has type `void' and is not a throw-expression",
2902 VOID_TYPE_P (arg2_type
) ? arg2
: arg3
);
2903 return error_mark_node
;
2907 goto valid_operands
;
2911 Otherwise, if the second and third operand have different types,
2912 and either has (possibly cv-qualified) class type, an attempt is
2913 made to convert each of those operands to the type of the other. */
2914 else if (!same_type_p (arg2_type
, arg3_type
)
2915 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
2917 tree conv2
= conditional_conversion (arg2
, arg3
);
2918 tree conv3
= conditional_conversion (arg3
, arg2
);
2922 If both can be converted, or one can be converted but the
2923 conversion is ambiguous, the program is ill-formed. If
2924 neither can be converted, the operands are left unchanged and
2925 further checking is performed as described below. If exactly
2926 one conversion is possible, that conversion is applied to the
2927 chosen operand and the converted operand is used in place of
2928 the original operand for the remainder of this section. */
2929 if ((conv2
&& !ICS_BAD_FLAG (conv2
)
2930 && conv3
&& !ICS_BAD_FLAG (conv3
))
2931 || (conv2
&& TREE_CODE (conv2
) == AMBIG_CONV
)
2932 || (conv3
&& TREE_CODE (conv3
) == AMBIG_CONV
))
2934 cp_error ("operands to ?: have different types");
2935 return error_mark_node
;
2937 else if (conv2
&& !ICS_BAD_FLAG (conv2
))
2939 arg2
= convert_like (conv2
, arg2
);
2940 arg2
= convert_from_reference (arg2
);
2941 /* That may not quite have done the trick. If the two types
2942 are cv-qualified variants of one another, we will have
2943 just used an IDENTITY_CONV. (There's no conversion from
2944 an lvalue of one class type to an lvalue of another type,
2945 even a cv-qualified variant, and we don't want to lose
2946 lvalue-ness here.) So, we manually add a NOP_EXPR here
2948 if (!same_type_p (TREE_TYPE (arg2
), arg3_type
))
2949 arg2
= build1 (NOP_EXPR
, arg3_type
, arg2
);
2950 arg2_type
= TREE_TYPE (arg2
);
2952 else if (conv3
&& !ICS_BAD_FLAG (conv3
))
2954 arg3
= convert_like (conv3
, arg3
);
2955 arg3
= convert_from_reference (arg3
);
2956 if (!same_type_p (TREE_TYPE (arg3
), arg2_type
))
2957 arg3
= build1 (NOP_EXPR
, arg2_type
, arg3
);
2958 arg3_type
= TREE_TYPE (arg3
);
2964 If the second and third operands are lvalues and have the same
2965 type, the result is of that type and is an lvalue. */
2966 if (real_lvalue_p (arg2
) && real_lvalue_p (arg3
) &&
2967 same_type_p (arg2_type
, arg3_type
))
2969 result_type
= arg2_type
;
2970 goto valid_operands
;
2975 Otherwise, the result is an rvalue. If the second and third
2976 operand do not have the same type, and either has (possibly
2977 cv-qualified) class type, overload resolution is used to
2978 determine the conversions (if any) to be applied to the operands
2979 (_over.match.oper_, _over.built_). */
2981 if (!same_type_p (arg2_type
, arg3_type
)
2982 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
2987 /* Rearrange the arguments so that add_builtin_candidate only has
2988 to know about two args. In build_builtin_candidates, the
2989 arguments are unscrambled. */
2993 candidates
= add_builtin_candidates (candidates
,
2996 ansi_opname (COND_EXPR
),
3002 If the overload resolution fails, the program is
3004 if (!any_viable (candidates
))
3006 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
3007 print_z_candidates (candidates
);
3008 return error_mark_node
;
3010 candidates
= splice_viable (candidates
);
3011 cand
= tourney (candidates
);
3014 op_error (COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, "no match");
3015 print_z_candidates (candidates
);
3016 return error_mark_node
;
3021 Otherwise, the conversions thus determined are applied, and
3022 the converted operands are used in place of the original
3023 operands for the remainder of this section. */
3024 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3025 arg1
= convert_like (conv
, arg1
);
3026 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3027 arg2
= convert_like (conv
, arg2
);
3028 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3029 arg3
= convert_like (conv
, arg3
);
3034 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3035 and function-to-pointer (_conv.func_) standard conversions are
3036 performed on the second and third operands.
3038 We need to force the lvalue-to-rvalue conversion here for class types,
3039 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3040 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3043 We use ocp_convert rather than build_user_type_conversion because the
3044 latter returns NULL_TREE on failure, while the former gives an error. */
3046 if (IS_AGGR_TYPE (TREE_TYPE (arg2
)) && real_lvalue_p (arg2
))
3047 arg2
= ocp_convert (TREE_TYPE (arg2
), arg2
,
3048 CONV_IMPLICIT
|CONV_FORCE_TEMP
, LOOKUP_NORMAL
);
3050 arg2
= decay_conversion (arg2
);
3051 arg2_type
= TREE_TYPE (arg2
);
3053 if (IS_AGGR_TYPE (TREE_TYPE (arg3
)) && real_lvalue_p (arg3
))
3054 arg3
= ocp_convert (TREE_TYPE (arg3
), arg3
,
3055 CONV_IMPLICIT
|CONV_FORCE_TEMP
, LOOKUP_NORMAL
);
3057 arg3
= decay_conversion (arg3
);
3058 arg3_type
= TREE_TYPE (arg3
);
3062 After those conversions, one of the following shall hold:
3064 --The second and third operands have the same type; the result is of
3066 if (same_type_p (arg2_type
, arg3_type
))
3067 result_type
= arg2_type
;
3070 --The second and third operands have arithmetic or enumeration
3071 type; the usual arithmetic conversions are performed to bring
3072 them to a common type, and the result is of that type. */
3073 else if ((ARITHMETIC_TYPE_P (arg2_type
)
3074 || TREE_CODE (arg2_type
) == ENUMERAL_TYPE
)
3075 && (ARITHMETIC_TYPE_P (arg3_type
)
3076 || TREE_CODE (arg3_type
) == ENUMERAL_TYPE
))
3078 /* In this case, there is always a common type. */
3079 result_type
= type_after_usual_arithmetic_conversions (arg2_type
,
3082 if (TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3083 && TREE_CODE (arg3_type
) == ENUMERAL_TYPE
)
3084 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3085 arg2_type
, arg3_type
);
3086 else if (extra_warnings
3087 && ((TREE_CODE (arg2_type
) == ENUMERAL_TYPE
3088 && !same_type_p (arg3_type
, type_promotes_to (arg2_type
)))
3089 || (TREE_CODE (arg3_type
) == ENUMERAL_TYPE
3090 && !same_type_p (arg2_type
, type_promotes_to (arg3_type
)))))
3091 cp_warning ("enumeral and non-enumeral type in conditional expression");
3093 arg2
= perform_implicit_conversion (result_type
, arg2
);
3094 arg3
= perform_implicit_conversion (result_type
, arg3
);
3098 --The second and third operands have pointer type, or one has
3099 pointer type and the other is a null pointer constant; pointer
3100 conversions (_conv.ptr_) and qualification conversions
3101 (_conv.qual_) are performed to bring them to their composite
3102 pointer type (_expr.rel_). The result is of the composite
3105 --The second and third operands have pointer to member type, or
3106 one has pointer to member type and the other is a null pointer
3107 constant; pointer to member conversions (_conv.mem_) and
3108 qualification conversions (_conv.qual_) are performed to bring
3109 them to a common type, whose cv-qualification shall match the
3110 cv-qualification of either the second or the third operand.
3111 The result is of the common type. */
3112 else if ((null_ptr_cst_p (arg2
)
3113 && (TYPE_PTR_P (arg3_type
) || TYPE_PTRMEM_P (arg3_type
)
3114 || TYPE_PTRMEMFUNC_P (arg3_type
)))
3115 || (null_ptr_cst_p (arg3
)
3116 && (TYPE_PTR_P (arg2_type
) || TYPE_PTRMEM_P (arg2_type
)
3117 || TYPE_PTRMEMFUNC_P (arg2_type
)))
3118 || (TYPE_PTR_P (arg2_type
) && TYPE_PTR_P (arg3_type
))
3119 || (TYPE_PTRMEM_P (arg2_type
) && TYPE_PTRMEM_P (arg3_type
))
3120 || (TYPE_PTRMEMFUNC_P (arg2_type
)
3121 && TYPE_PTRMEMFUNC_P (arg3_type
)))
3123 result_type
= composite_pointer_type (arg2_type
, arg3_type
, arg2
,
3124 arg3
, "conditional expression");
3125 arg2
= perform_implicit_conversion (result_type
, arg2
);
3126 arg3
= perform_implicit_conversion (result_type
, arg3
);
3131 cp_error ("operands to ?: have different types");
3132 return error_mark_node
;
3136 result
= fold (build (COND_EXPR
, result_type
, arg1
, arg2
, arg3
));
3137 /* Expand both sides into the same slot, hopefully the target of the
3138 ?: expression. We used to check for TARGET_EXPRs here, but now we
3139 sometimes wrap them in NOP_EXPRs so the test would fail. */
3140 if (!lvalue_p
&& IS_AGGR_TYPE (result_type
))
3141 result
= build_target_expr_with_type (result
, result_type
);
3143 /* If this expression is an rvalue, but might be mistaken for an
3144 lvalue, we must add a NON_LVALUE_EXPR. */
3145 if (!lvalue_p
&& real_lvalue_p (result
))
3146 result
= build1 (NON_LVALUE_EXPR
, result_type
, result
);
3152 build_new_op (code
, flags
, arg1
, arg2
, arg3
)
3153 enum tree_code code
;
3155 tree arg1
, arg2
, arg3
;
3157 struct z_candidate
*candidates
= 0, *cand
;
3158 tree fns
, mem_arglist
= NULL_TREE
, arglist
, fnname
;
3159 enum tree_code code2
= NOP_EXPR
;
3160 tree templates
= NULL_TREE
;
3163 if (arg1
== error_mark_node
3164 || arg2
== error_mark_node
3165 || arg3
== error_mark_node
)
3166 return error_mark_node
;
3168 /* This can happen if a template takes all non-type parameters, e.g.
3169 undeclared_template<1, 5, 72>a; */
3170 if (code
== LT_EXPR
&& TREE_CODE (arg1
) == TEMPLATE_DECL
)
3172 cp_error ("`%D' must be declared before use", arg1
);
3173 return error_mark_node
;
3176 if (code
== MODIFY_EXPR
)
3178 code2
= TREE_CODE (arg3
);
3180 fnname
= ansi_assopname (code2
);
3183 fnname
= ansi_opname (code
);
3189 case VEC_DELETE_EXPR
:
3191 /* Use build_op_new_call and build_op_delete_call instead. */
3192 my_friendly_abort (981018);
3195 return build_object_call (arg1
, arg2
);
3201 /* The comma operator can have void args. */
3202 if (TREE_CODE (arg1
) == OFFSET_REF
)
3203 arg1
= resolve_offset_ref (arg1
);
3204 if (arg2
&& TREE_CODE (arg2
) == OFFSET_REF
)
3205 arg2
= resolve_offset_ref (arg2
);
3206 if (arg3
&& TREE_CODE (arg3
) == OFFSET_REF
)
3207 arg3
= resolve_offset_ref (arg3
);
3209 if (code
== COND_EXPR
)
3211 if (arg2
== NULL_TREE
3212 || TREE_CODE (TREE_TYPE (arg2
)) == VOID_TYPE
3213 || TREE_CODE (TREE_TYPE (arg3
)) == VOID_TYPE
3214 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))
3215 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3
))))
3218 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1
))
3219 && (! arg2
|| ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
))))
3222 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
3223 arg2
= integer_zero_node
;
3226 arglist
= tree_cons (NULL_TREE
, arg1
, tree_cons
3227 (NULL_TREE
, arg2
, build_tree_list (NULL_TREE
, arg3
)));
3229 arglist
= tree_cons (NULL_TREE
, arg1
, build_tree_list (NULL_TREE
, arg2
));
3231 arglist
= build_tree_list (NULL_TREE
, arg1
);
3233 fns
= lookup_function_nonclass (fnname
, arglist
);
3235 if (fns
&& TREE_CODE (fns
) == TREE_LIST
)
3236 fns
= TREE_VALUE (fns
);
3237 for (; fns
; fns
= OVL_NEXT (fns
))
3239 tree fn
= OVL_CURRENT (fns
);
3240 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3242 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3244 = add_template_candidate (candidates
, fn
, NULL_TREE
, NULL_TREE
,
3245 arglist
, TREE_TYPE (fnname
),
3246 flags
, DEDUCE_CALL
);
3249 candidates
= add_function_candidate (candidates
, fn
, NULL_TREE
,
3253 if (IS_AGGR_TYPE (TREE_TYPE (arg1
)))
3255 fns
= lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1
)), fnname
, 1);
3256 if (fns
== error_mark_node
)
3264 tree basetype
= BINFO_TYPE (TREE_PURPOSE (fns
));
3265 mem_arglist
= tree_cons (NULL_TREE
, build_this (arg1
), TREE_CHAIN (arglist
));
3266 for (fns
= TREE_VALUE (fns
); fns
; fns
= OVL_NEXT (fns
))
3268 tree fn
= OVL_CURRENT (fns
);
3271 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
3272 this_arglist
= mem_arglist
;
3274 this_arglist
= arglist
;
3276 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3278 /* A member template. */
3279 templates
= tree_cons (NULL_TREE
, fn
, templates
);
3281 = add_template_candidate (candidates
, fn
, basetype
, NULL_TREE
,
3282 this_arglist
, TREE_TYPE (fnname
),
3283 flags
, DEDUCE_CALL
);
3286 candidates
= add_function_candidate
3287 (candidates
, fn
, basetype
, this_arglist
, flags
);
3290 candidates
->basetype_path
= TYPE_BINFO (TREE_TYPE (arg1
));
3297 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3298 to know about two args; a builtin candidate will always have a first
3299 parameter of type bool. We'll handle that in
3300 build_builtin_candidate. */
3301 if (code
== COND_EXPR
)
3311 args
[2] = NULL_TREE
;
3314 candidates
= add_builtin_candidates
3315 (candidates
, code
, code2
, fnname
, args
, flags
);
3318 if (! any_viable (candidates
))
3322 case POSTINCREMENT_EXPR
:
3323 case POSTDECREMENT_EXPR
:
3324 /* Look for an `operator++ (int)'. If they didn't have
3325 one, then we fall back to the old way of doing things. */
3326 if (flags
& LOOKUP_COMPLAIN
)
3327 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3329 operator_name_info
[code
].name
);
3330 if (code
== POSTINCREMENT_EXPR
)
3331 code
= PREINCREMENT_EXPR
;
3333 code
= PREDECREMENT_EXPR
;
3334 return build_new_op (code
, flags
, arg1
, NULL_TREE
, NULL_TREE
);
3336 /* The caller will deal with these. */
3345 if (flags
& LOOKUP_COMPLAIN
)
3347 op_error (code
, code2
, arg1
, arg2
, arg3
, "no match");
3348 print_z_candidates (candidates
);
3350 return error_mark_node
;
3352 candidates
= splice_viable (candidates
);
3353 cand
= tourney (candidates
);
3357 if (flags
& LOOKUP_COMPLAIN
)
3359 op_error (code
, code2
, arg1
, arg2
, arg3
, "ambiguous overload");
3360 print_z_candidates (candidates
);
3362 return error_mark_node
;
3365 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
3367 extern int warn_synth
;
3369 && fnname
== ansi_assopname (NOP_EXPR
)
3370 && DECL_ARTIFICIAL (cand
->fn
)
3372 && ! candidates
->next
->next
)
3374 cp_warning ("using synthesized `%#D' for copy assignment",
3376 cp_warning_at (" where cfront would use `%#D'",
3378 ? candidates
->next
->fn
3382 return build_over_call
3384 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
3385 ? mem_arglist
: arglist
,
3389 /* Check for comparison of different enum types. */
3398 if (TREE_CODE (TREE_TYPE (arg1
)) == ENUMERAL_TYPE
3399 && TREE_CODE (TREE_TYPE (arg2
)) == ENUMERAL_TYPE
3400 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
))
3401 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2
))))
3403 cp_warning ("comparison between `%#T' and `%#T'",
3404 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
3411 /* We need to strip any leading REF_BIND so that bitfields don't cause
3412 errors. This should not remove any important conversions, because
3413 builtins don't apply to class objects directly. */
3414 conv
= TREE_VEC_ELT (cand
->convs
, 0);
3415 if (TREE_CODE (conv
) == REF_BIND
)
3416 conv
= TREE_OPERAND (conv
, 0);
3417 arg1
= convert_like (conv
, arg1
);
3420 conv
= TREE_VEC_ELT (cand
->convs
, 1);
3421 if (TREE_CODE (conv
) == REF_BIND
)
3422 conv
= TREE_OPERAND (conv
, 0);
3423 arg2
= convert_like (conv
, arg2
);
3427 conv
= TREE_VEC_ELT (cand
->convs
, 2);
3428 if (TREE_CODE (conv
) == REF_BIND
)
3429 conv
= TREE_OPERAND (conv
, 0);
3430 arg3
= convert_like (conv
, arg3
);
3437 return build_modify_expr (arg1
, code2
, arg2
);
3440 return build_indirect_ref (arg1
, "unary *");
3445 case TRUNC_DIV_EXPR
:
3456 case TRUNC_MOD_EXPR
:
3460 case TRUTH_ANDIF_EXPR
:
3461 case TRUTH_ORIF_EXPR
:
3462 return cp_build_binary_op (code
, arg1
, arg2
);
3467 case TRUTH_NOT_EXPR
:
3468 case PREINCREMENT_EXPR
:
3469 case POSTINCREMENT_EXPR
:
3470 case PREDECREMENT_EXPR
:
3471 case POSTDECREMENT_EXPR
:
3474 return build_unary_op (code
, arg1
, candidates
!= 0);
3477 return build_array_ref (arg1
, arg2
);
3480 return build_conditional_expr (arg1
, arg2
, arg3
);
3483 return build_m_component_ref
3484 (build_indirect_ref (arg1
, NULL_PTR
), arg2
);
3486 /* The caller will deal with these. */
3493 my_friendly_abort (367);
3498 /* Build a call to operator delete. This has to be handled very specially,
3499 because the restrictions on what signatures match are different from all
3500 other call instances. For a normal delete, only a delete taking (void *)
3501 or (void *, size_t) is accepted. For a placement delete, only an exact
3502 match with the placement new is accepted.
3504 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3505 ADDR is the pointer to be deleted. For placement delete, it is also
3506 used to determine what the corresponding new looked like.
3507 SIZE is the size of the memory block to be deleted.
3508 FLAGS are the usual overloading flags.
3509 PLACEMENT is the corresponding placement new call, or 0. */
3512 build_op_delete_call (code
, addr
, size
, flags
, placement
)
3513 enum tree_code code
;
3514 tree addr
, size
, placement
;
3517 tree fn
, fns
, fnname
, fntype
, argtypes
, args
, type
;
3519 if (addr
== error_mark_node
)
3520 return error_mark_node
;
3522 type
= TREE_TYPE (TREE_TYPE (addr
));
3523 fnname
= ansi_opname (code
);
3525 if (IS_AGGR_TYPE (type
) && ! (flags
& LOOKUP_GLOBAL
))
3528 If the result of the lookup is ambiguous or inaccessible, or if
3529 the lookup selects a placement deallocation function, the
3530 program is ill-formed.
3532 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3534 fns
= lookup_fnfields (TYPE_BINFO (type
), fnname
, 1);
3535 if (fns
== error_mark_node
)
3536 return error_mark_node
;
3541 if (fns
== NULL_TREE
)
3542 fns
= lookup_name_nonclass (fnname
);
3546 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3548 /* Extract the function. */
3549 argtypes
= TREE_OPERAND (TREE_OPERAND (placement
, 0), 0);
3550 /* Then the second parm type. */
3551 argtypes
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes
)));
3553 /* Also the second argument. */
3554 args
= TREE_CHAIN (TREE_OPERAND (placement
, 1));
3558 /* First try it without the size argument. */
3559 argtypes
= void_list_node
;
3563 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
, argtypes
);
3564 fntype
= build_function_type (void_type_node
, argtypes
);
3566 /* Strip const and volatile from addr. */
3567 if (type
!= TYPE_MAIN_VARIANT (type
))
3568 addr
= cp_convert (build_pointer_type (TYPE_MAIN_VARIANT (type
)), addr
);
3570 fn
= instantiate_type (fntype
, fns
, itf_no_attributes
);
3572 if (fn
!= error_mark_node
)
3574 if (TREE_CODE (fns
) == TREE_LIST
)
3575 /* Member functions. */
3576 enforce_access (type
, fn
);
3577 return build_function_call (fn
, tree_cons (NULL_TREE
, addr
, args
));
3580 /* If we are doing placement delete we do nothing if we don't find a
3581 matching op delete. */
3585 /* Normal delete; now try to find a match including the size argument. */
3586 argtypes
= tree_cons (NULL_TREE
, ptr_type_node
,
3587 tree_cons (NULL_TREE
, sizetype
, void_list_node
));
3588 fntype
= build_function_type (void_type_node
, argtypes
);
3590 fn
= instantiate_type (fntype
, fns
, itf_no_attributes
);
3592 if (fn
!= error_mark_node
)
3594 if (BASELINK_P (fns
))
3595 /* Member functions. */
3596 enforce_access (type
, fn
);
3597 return build_function_call
3598 (fn
, tree_cons (NULL_TREE
, addr
,
3599 build_tree_list (NULL_TREE
, size
)));
3602 /* finish_function passes LOOKUP_SPECULATIVELY if we're in a
3603 destructor, in which case the error should be deferred
3604 until someone actually tries to delete one of these. */
3605 if (flags
& LOOKUP_SPECULATIVELY
)
3608 cp_error ("no suitable `operator delete' for `%T'", type
);
3609 return error_mark_node
;
3612 /* If the current scope isn't allowed to access DECL along
3613 BASETYPE_PATH, give an error. The most derived class in
3614 BASETYPE_PATH is the one used to qualify DECL. */
3617 enforce_access (basetype_path
, decl
)
3623 accessible
= accessible_p (basetype_path
, decl
);
3626 if (TREE_PRIVATE (decl
))
3627 cp_error_at ("`%+#D' is private", decl
);
3628 else if (TREE_PROTECTED (decl
))
3629 cp_error_at ("`%+#D' is protected", decl
);
3631 cp_error_at ("`%+#D' is inaccessible", decl
);
3632 cp_error ("within this context");
3639 /* Perform the conversions in CONVS on the expression EXPR.
3640 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3641 indicates the `this' argument of a method. INNER is non-zero when
3642 being called to continue a conversion chain. */
3645 convert_like_real (convs
, expr
, fn
, argnum
, inner
)
3651 if (ICS_BAD_FLAG (convs
)
3652 && TREE_CODE (convs
) != USER_CONV
3653 && TREE_CODE (convs
) != AMBIG_CONV
3654 && TREE_CODE (convs
) != REF_BIND
)
3657 for (; t
; t
= TREE_OPERAND (t
, 0))
3659 if (TREE_CODE (t
) == USER_CONV
)
3661 expr
= convert_like_real (t
, expr
, fn
, argnum
, 1);
3664 else if (TREE_CODE (t
) == AMBIG_CONV
)
3665 return convert_like_real (t
, expr
, fn
, argnum
, 1);
3666 else if (TREE_CODE (t
) == IDENTITY_CONV
)
3669 return convert_for_initialization
3670 (NULL_TREE
, TREE_TYPE (convs
), expr
, LOOKUP_NORMAL
,
3671 "conversion", fn
, argnum
);
3675 expr
= dubious_conversion_warnings
3676 (TREE_TYPE (convs
), expr
, "argument", fn
, argnum
);
3677 switch (TREE_CODE (convs
))
3681 struct z_candidate
*cand
3682 = WRAPPER_PTR (TREE_OPERAND (convs
, 1));
3686 if (DECL_CONSTRUCTOR_P (fn
))
3688 tree t
= build_int_2 (0, 0);
3689 TREE_TYPE (t
) = build_pointer_type (DECL_CONTEXT (fn
));
3691 args
= build_tree_list (NULL_TREE
, expr
);
3692 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
3693 args
= tree_cons (NULL_TREE
, integer_one_node
, args
);
3694 args
= tree_cons (NULL_TREE
, t
, args
);
3697 args
= build_this (expr
);
3698 expr
= build_over_call (cand
, args
, LOOKUP_NORMAL
);
3700 /* If this is a constructor or a function returning an aggr type,
3701 we need to build up a TARGET_EXPR. */
3702 if (DECL_CONSTRUCTOR_P (fn
))
3703 expr
= build_cplus_new (TREE_TYPE (convs
), expr
);
3708 if (type_unknown_p (expr
))
3709 expr
= instantiate_type (TREE_TYPE (convs
), expr
, itf_complain
);
3712 /* Call build_user_type_conversion again for the error. */
3713 return build_user_type_conversion
3714 (TREE_TYPE (convs
), TREE_OPERAND (convs
, 0), LOOKUP_NORMAL
);
3720 expr
= convert_like_real (TREE_OPERAND (convs
, 0), expr
, fn
, argnum
, 1);
3721 if (expr
== error_mark_node
)
3722 return error_mark_node
;
3724 /* Convert a non-array constant variable to its underlying value, unless we
3725 are about to bind it to a reference, in which case we need to
3726 leave it as an lvalue. */
3727 if (TREE_CODE (convs
) != REF_BIND
3728 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
3729 expr
= decl_constant_value (expr
);
3731 switch (TREE_CODE (convs
))
3734 if (! IS_AGGR_TYPE (TREE_TYPE (convs
)))
3736 /* else fall through */
3738 if (TREE_CODE (convs
) == BASE_CONV
&& !NEED_TEMPORARY_P (convs
))
3740 /* We are going to bind a reference directly to a base-class
3741 subobject of EXPR. */
3742 tree base_ptr
= build_pointer_type (TREE_TYPE (convs
));
3744 /* Build an expression for `*((base*) &expr)'. */
3745 expr
= build_unary_op (ADDR_EXPR
, expr
, 0);
3746 expr
= perform_implicit_conversion (base_ptr
, expr
);
3747 expr
= build_indirect_ref (expr
, "implicit conversion");
3752 tree cvt_expr
= build_user_type_conversion
3753 (TREE_TYPE (convs
), expr
, LOOKUP_NORMAL
);
3756 /* This can occur if, for example, the EXPR has incomplete
3757 type. We can't check for that before attempting the
3758 conversion because the type might be an incomplete
3759 array type, which is OK if some constructor for the
3760 destination type takes a pointer argument. */
3761 if (!COMPLETE_TYPE_P (TREE_TYPE (expr
)))
3763 if (same_type_p (TREE_TYPE (expr
), TREE_TYPE (convs
)))
3764 incomplete_type_error (expr
, TREE_TYPE (expr
));
3766 cp_error ("could not convert `%E' (with incomplete type `%T') to `%T'",
3767 expr
, TREE_TYPE (expr
), TREE_TYPE (convs
));
3770 cp_error ("could not convert `%E' to `%T'",
3771 expr
, TREE_TYPE (convs
));
3772 return error_mark_node
;
3779 tree ref_type
= TREE_TYPE (convs
);
3781 /* If necessary, create a temporary. */
3782 if (NEED_TEMPORARY_P (convs
))
3784 tree type
= TREE_TYPE (TREE_OPERAND (convs
, 0));
3785 expr
= build_target_expr_with_type (expr
, type
);
3788 /* Take the address of the thing to which we will bind the
3790 expr
= build_unary_op (ADDR_EXPR
, expr
, 1);
3791 if (expr
== error_mark_node
)
3792 return error_mark_node
;
3794 /* Convert it to a pointer to the type referred to by the
3795 reference. This will adjust the pointer if a derived to
3796 base conversion is being performed. */
3797 expr
= cp_convert (build_pointer_type (TREE_TYPE (ref_type
)),
3799 /* Convert the pointer to the desired reference type. */
3800 expr
= build1 (NOP_EXPR
, ref_type
, expr
);
3806 return decay_conversion (expr
);
3809 /* Warn about deprecated conversion if appropriate. */
3810 string_conv_p (TREE_TYPE (convs
), expr
, 1);
3816 return ocp_convert (TREE_TYPE (convs
), expr
, CONV_IMPLICIT
,
3817 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
3820 /* ARG is being passed to a varargs function. Perform any conversions
3821 required. Array/function to pointer decay must have already happened.
3822 Return the converted value. */
3825 convert_arg_to_ellipsis (arg
)
3828 if (TREE_CODE (TREE_TYPE (arg
)) == REAL_TYPE
3829 && (TYPE_PRECISION (TREE_TYPE (arg
))
3830 < TYPE_PRECISION (double_type_node
)))
3831 /* Convert `float' to `double'. */
3832 arg
= cp_convert (double_type_node
, arg
);
3834 /* Convert `short' and `char' to full-size `int'. */
3835 arg
= default_conversion (arg
);
3837 arg
= require_complete_type (arg
);
3839 if (arg
!= error_mark_node
&& ! pod_type_p (TREE_TYPE (arg
)))
3841 /* Undefined behaviour [expr.call] 5.2.2/7. */
3842 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3849 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3852 build_x_va_arg (expr
, type
)
3856 if (processing_template_decl
)
3857 return build_min (VA_ARG_EXPR
, type
, expr
);
3859 type
= complete_type_or_else (type
, NULL_TREE
);
3861 if (expr
== error_mark_node
|| !type
)
3862 return error_mark_node
;
3864 if (! pod_type_p (type
))
3866 /* Undefined behaviour [expr.call] 5.2.2/7. */
3867 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3871 return build_va_arg (expr
, type
);
3874 /* TYPE has been given to va_arg. Apply the default conversions which would
3875 have happened when passed via ellipsis. Return the promoted type, or
3876 NULL_TREE, if there is no change. */
3879 convert_type_from_ellipsis (type
)
3884 if (TREE_CODE (type
) == ARRAY_TYPE
)
3885 promote
= build_pointer_type (TREE_TYPE (type
));
3886 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
3887 promote
= build_pointer_type (type
);
3889 promote
= type_promotes_to (type
);
3891 return same_type_p (type
, promote
) ? NULL_TREE
: promote
;
3894 /* ARG is a default argument expression being passed to a parameter of
3895 the indicated TYPE, which is a parameter to FN. Do any required
3896 conversions. Return the converted value. */
3899 convert_default_arg (type
, arg
, fn
, parmnum
)
3905 if (fn
&& DECL_TEMPLATE_INFO (fn
))
3906 arg
= tsubst_default_argument (fn
, type
, arg
);
3908 arg
= break_out_target_exprs (arg
);
3910 if (TREE_CODE (arg
) == CONSTRUCTOR
)
3912 arg
= digest_init (type
, arg
, 0);
3913 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
3914 "default argument", fn
, parmnum
);
3918 /* This could get clobbered by the following call. */
3919 if (TREE_HAS_CONSTRUCTOR (arg
))
3920 arg
= copy_node (arg
);
3922 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
3923 "default argument", fn
, parmnum
);
3924 if (PROMOTE_PROTOTYPES
3925 && (TREE_CODE (type
) == INTEGER_TYPE
3926 || TREE_CODE (type
) == ENUMERAL_TYPE
)
3927 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3928 arg
= default_conversion (arg
);
3935 build_over_call (cand
, args
, flags
)
3936 struct z_candidate
*cand
;
3941 tree convs
= cand
->convs
;
3942 tree converted_args
= NULL_TREE
;
3943 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3944 tree conv
, arg
, val
;
3948 /* Give any warnings we noticed during overload resolution. */
3950 for (val
= cand
->warnings
; val
; val
= TREE_CHAIN (val
))
3951 joust (cand
, WRAPPER_PTR (TREE_VALUE (val
)), 1);
3953 if (DECL_FUNCTION_MEMBER_P (fn
))
3954 enforce_access (cand
->basetype_path
, fn
);
3956 if (args
&& TREE_CODE (args
) != TREE_LIST
)
3957 args
= build_tree_list (NULL_TREE
, args
);
3960 /* The implicit parameters to a constructor are not considered by overload
3961 resolution, and must be of the proper type. */
3962 if (DECL_CONSTRUCTOR_P (fn
))
3964 converted_args
= tree_cons (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
3965 arg
= TREE_CHAIN (arg
);
3966 parm
= TREE_CHAIN (parm
);
3967 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
3969 converted_args
= tree_cons
3970 (NULL_TREE
, TREE_VALUE (arg
), converted_args
);
3971 arg
= TREE_CHAIN (arg
);
3972 parm
= TREE_CHAIN (parm
);
3975 /* Bypass access control for 'this' parameter. */
3976 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
3978 tree parmtype
= TREE_VALUE (parm
);
3979 tree argtype
= TREE_TYPE (TREE_VALUE (arg
));
3981 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs
, i
)))
3982 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
3983 TREE_TYPE (argtype
), fn
);
3985 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
3986 X is called for an object that is not of type X, or of a type
3987 derived from X, the behavior is undefined.
3989 So we can assume that anything passed as 'this' is non-null, and
3990 optimize accordingly. */
3991 my_friendly_assert (TREE_CODE (parmtype
) == POINTER_TYPE
, 19990811);
3992 t
= convert_pointer_to_real (TREE_TYPE (parmtype
), TREE_VALUE (arg
));
3993 converted_args
= tree_cons (NULL_TREE
, t
, converted_args
);
3994 parm
= TREE_CHAIN (parm
);
3995 arg
= TREE_CHAIN (arg
);
4001 parm
= TREE_CHAIN (parm
), arg
= TREE_CHAIN (arg
), ++i
)
4003 tree type
= TREE_VALUE (parm
);
4005 conv
= TREE_VEC_ELT (convs
, i
);
4006 if (ICS_BAD_FLAG (conv
))
4009 val
= TREE_VALUE (arg
);
4011 for (; t
; t
= TREE_OPERAND (t
, 0))
4013 if (TREE_CODE (t
) == USER_CONV
4014 || TREE_CODE (t
) == AMBIG_CONV
)
4016 val
= convert_like_with_context (t
, val
, fn
, i
- is_method
);
4019 else if (TREE_CODE (t
) == IDENTITY_CONV
)
4022 val
= convert_for_initialization
4023 (NULL_TREE
, type
, val
, LOOKUP_NORMAL
,
4024 "argument", fn
, i
- is_method
);
4028 val
= TREE_VALUE (arg
);
4029 val
= convert_like_with_context
4030 (conv
, TREE_VALUE (arg
), fn
, i
- is_method
);
4033 if (PROMOTE_PROTOTYPES
4034 && (TREE_CODE (type
) == INTEGER_TYPE
4035 || TREE_CODE (type
) == ENUMERAL_TYPE
)
4036 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
4037 val
= default_conversion (val
);
4038 converted_args
= tree_cons (NULL_TREE
, val
, converted_args
);
4041 /* Default arguments */
4042 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
), i
++)
4044 = tree_cons (NULL_TREE
,
4045 convert_default_arg (TREE_VALUE (parm
),
4046 TREE_PURPOSE (parm
),
4051 for (; arg
; arg
= TREE_CHAIN (arg
))
4053 = tree_cons (NULL_TREE
,
4054 convert_arg_to_ellipsis (TREE_VALUE (arg
)),
4057 converted_args
= nreverse (converted_args
);
4059 if (warn_format
&& (DECL_NAME (fn
) || DECL_ASSEMBLER_NAME (fn
)))
4060 check_function_format (NULL
, DECL_NAME (fn
), DECL_ASSEMBLER_NAME (fn
),
4063 /* Avoid actually calling copy constructors and copy assignment operators,
4066 if (! flag_elide_constructors
)
4067 /* Do things the hard way. */;
4068 else if (TREE_VEC_LENGTH (convs
) == 1
4069 && DECL_COPY_CONSTRUCTOR_P (fn
))
4072 arg
= TREE_CHAIN (converted_args
);
4073 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
4074 arg
= TREE_CHAIN (arg
);
4075 arg
= TREE_VALUE (arg
);
4077 /* Pull out the real argument, disregarding const-correctness. */
4079 while (TREE_CODE (targ
) == NOP_EXPR
4080 || TREE_CODE (targ
) == NON_LVALUE_EXPR
4081 || TREE_CODE (targ
) == CONVERT_EXPR
)
4082 targ
= TREE_OPERAND (targ
, 0);
4083 if (TREE_CODE (targ
) == ADDR_EXPR
)
4085 targ
= TREE_OPERAND (targ
, 0);
4086 if (!same_type_ignoring_top_level_qualifiers_p
4087 (TREE_TYPE (TREE_TYPE (arg
)), TREE_TYPE (targ
)))
4096 arg
= build_indirect_ref (arg
, 0);
4098 /* [class.copy]: the copy constructor is implicitly defined even if
4099 the implementation elided its use. */
4100 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn
)))
4103 /* If we're creating a temp and we already have one, don't create a
4104 new one. If we're not creating a temp but we get one, use
4105 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4106 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4107 temp or an INIT_EXPR otherwise. */
4108 if (integer_zerop (TREE_VALUE (args
)))
4110 if (! real_lvalue_p (arg
))
4112 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4113 return build_target_expr_with_type (arg
, DECL_CONTEXT (fn
));
4115 else if (! real_lvalue_p (arg
)
4116 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn
)))
4119 tree to
= stabilize_reference
4120 (build_indirect_ref (TREE_VALUE (args
), 0));
4122 /* If we're initializing an empty class, then we actually
4123 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4124 reason is that the dummy padding member in the target may
4125 not actually be allocated if TO is a base class
4126 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4127 padding, a MODIFY_EXPR will preserve its value, which is
4128 the right thing to do if it's not really padding at all.
4130 It's not safe to just throw away the ARG if we're looking
4131 at an empty class because the ARG might contain a
4132 TARGET_EXPR which wants to be bound to TO. If it is not,
4133 expand_expr will assign a dummy slot for the TARGET_EXPR,
4134 and we will call a destructor for it, which is wrong,
4135 because we will also destroy TO, but will never have
4137 val
= build (is_empty_class (DECL_CONTEXT (fn
))
4138 ? MODIFY_EXPR
: INIT_EXPR
,
4139 DECL_CONTEXT (fn
), to
, arg
);
4140 address
= build_unary_op (ADDR_EXPR
, val
, 0);
4141 /* Avoid a warning about this expression, if the address is
4143 TREE_USED (address
) = 1;
4147 else if (DECL_OVERLOADED_OPERATOR_P (fn
) == NOP_EXPR
4149 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn
)))
4151 tree to
= stabilize_reference
4152 (build_indirect_ref (TREE_VALUE (converted_args
), 0));
4154 arg
= build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args
)), 0);
4156 val
= build (MODIFY_EXPR
, TREE_TYPE (to
), to
, arg
);
4162 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
4164 tree t
, *p
= &TREE_VALUE (converted_args
);
4165 tree binfo
= get_binfo
4166 (DECL_VIRTUAL_CONTEXT (fn
), TREE_TYPE (TREE_TYPE (*p
)), 0);
4167 *p
= convert_pointer_to_real (binfo
, *p
);
4168 if (TREE_SIDE_EFFECTS (*p
))
4169 *p
= save_expr (*p
);
4170 t
= build_pointer_type (TREE_TYPE (fn
));
4171 fn
= build_vfn_ref (p
, build_indirect_ref (*p
, 0), DECL_VINDEX (fn
));
4174 else if (DECL_INLINE (fn
))
4175 fn
= inline_conversion (fn
);
4177 fn
= build_addr_func (fn
);
4179 /* Recognize certain built-in functions so we can make tree-codes
4180 other than CALL_EXPR. We do this when it enables fold-const.c
4181 to do something useful. */
4183 if (TREE_CODE (fn
) == ADDR_EXPR
4184 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
4185 && DECL_BUILT_IN (TREE_OPERAND (fn
, 0)))
4188 exp
= expand_tree_builtin (TREE_OPERAND (fn
, 0), args
, converted_args
);
4193 fn
= fold (build_call (fn
, converted_args
));
4194 if (VOID_TYPE_P (TREE_TYPE (fn
)))
4196 fn
= require_complete_type (fn
);
4197 if (fn
== error_mark_node
)
4198 return error_mark_node
;
4199 if (IS_AGGR_TYPE (TREE_TYPE (fn
)))
4200 fn
= build_cplus_new (TREE_TYPE (fn
), fn
);
4201 return convert_from_reference (fn
);
4204 /* Returns the value to use for the in-charge parameter when making a
4205 call to a function with the indicated NAME. */
4208 in_charge_arg_for_name (name
)
4211 if (name
== base_ctor_identifier
4212 || name
== base_dtor_identifier
)
4213 return integer_zero_node
;
4214 else if (name
== complete_ctor_identifier
)
4215 return integer_one_node
;
4216 else if (name
== complete_dtor_identifier
)
4217 return integer_two_node
;
4218 else if (name
== deleting_dtor_identifier
)
4219 return integer_three_node
;
4221 /* This function should only be called with one of the names listed
4223 my_friendly_abort (20000411);
4228 build_new_method_call (instance
, name
, args
, basetype_path
, flags
)
4229 tree instance
, name
, args
, basetype_path
;
4232 struct z_candidate
*candidates
= 0, *cand
;
4233 tree explicit_targs
= NULL_TREE
;
4234 tree basetype
, mem_args
= NULL_TREE
, fns
, instance_ptr
;
4237 tree templates
= NULL_TREE
;
4238 int template_only
= 0;
4240 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
4242 explicit_targs
= TREE_OPERAND (name
, 1);
4243 name
= TREE_OPERAND (name
, 0);
4245 name
= DECL_NAME (name
);
4248 if (TREE_CODE (name
) == COMPONENT_REF
)
4249 name
= TREE_OPERAND (name
, 1);
4250 if (TREE_CODE (name
) == OVERLOAD
)
4251 name
= DECL_NAME (OVL_CURRENT (name
));
4258 args
= resolve_args (args
);
4260 if (args
== error_mark_node
)
4261 return error_mark_node
;
4263 if (instance
== NULL_TREE
)
4264 basetype
= BINFO_TYPE (basetype_path
);
4267 if (TREE_CODE (instance
) == OFFSET_REF
)
4268 instance
= resolve_offset_ref (instance
);
4269 if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
4270 instance
= convert_from_reference (instance
);
4271 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (instance
));
4273 /* XXX this should be handled before we get here. */
4274 if (! IS_AGGR_TYPE (basetype
))
4276 if ((flags
& LOOKUP_COMPLAIN
) && basetype
!= error_mark_node
)
4277 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4278 name
, instance
, basetype
);
4280 return error_mark_node
;
4284 if (basetype_path
== NULL_TREE
)
4285 basetype_path
= TYPE_BINFO (basetype
);
4289 instance_ptr
= build_this (instance
);
4291 if (! template_only
)
4293 /* XXX this should be handled before we get here. */
4294 fns
= build_field_call (basetype_path
, instance_ptr
, name
, args
);
4301 instance_ptr
= build_int_2 (0, 0);
4302 TREE_TYPE (instance_ptr
) = build_pointer_type (basetype
);
4305 /* Callers should explicitly indicate whether they want to construct
4306 the complete object or just the part without virtual bases. */
4307 my_friendly_assert (name
!= ctor_identifier
, 20000408);
4308 /* Similarly for destructors. */
4309 my_friendly_assert (name
!= dtor_identifier
, 20000408);
4311 if (IDENTIFIER_CTOR_OR_DTOR_P (name
))
4315 constructor_p
= (name
== complete_ctor_identifier
4316 || name
== base_ctor_identifier
);
4317 pretty_name
= (constructor_p
4318 ? constructor_name (basetype
) : dtor_identifier
);
4322 /* Add the in-charge parameter as an implicit first argument. */
4324 || TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
4325 args
= tree_cons (NULL_TREE
,
4326 in_charge_arg_for_name (name
),
4329 /* We want to call the normal constructor function under the
4331 name
= constructor_p
? ctor_identifier
: dtor_identifier
;
4333 /* If we're call a subobject constructor or destructor for a
4334 subobject that uses virtual base classes, then we need to
4335 pass down a pointer to a VTT for the subobject. */
4336 else if ((name
== base_ctor_identifier
4337 || name
== base_dtor_identifier
)
4338 && TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
4343 /* If the current function is a complete object constructor
4344 or destructor, then we fetch the VTT directly.
4345 Otherwise, we look it up using the VTT we were given. */
4346 vtt
= IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type
));
4347 vtt
= build_unary_op (ADDR_EXPR
, vtt
, /*noconvert=*/1);
4348 vtt
= build (COND_EXPR
, TREE_TYPE (vtt
),
4349 DECL_USE_VTT_PARM (current_function_decl
),
4350 DECL_VTT_PARM (current_function_decl
),
4353 sub_vtt
= build (PLUS_EXPR
, TREE_TYPE (vtt
), vtt
,
4354 BINFO_SUBVTT_INDEX (basetype_path
));
4355 sub_vtt
= build_indirect_ref (sub_vtt
, NULL
);
4357 args
= tree_cons (NULL_TREE
, sub_vtt
, args
);
4363 fns
= lookup_fnfields (basetype_path
, name
, 1);
4365 if (fns
== error_mark_node
)
4366 return error_mark_node
;
4369 tree base
= BINFO_TYPE (TREE_PURPOSE (fns
));
4370 tree fn
= TREE_VALUE (fns
);
4371 mem_args
= tree_cons (NULL_TREE
, instance_ptr
, args
);
4372 for (; fn
; fn
= OVL_NEXT (fn
))
4374 tree t
= OVL_CURRENT (fn
);
4377 /* We can end up here for copy-init of same or base class. */
4378 if ((flags
& LOOKUP_ONLYCONVERTING
)
4379 && DECL_NONCONVERTING_P (t
))
4382 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
4383 this_arglist
= mem_args
;
4385 this_arglist
= args
;
4387 if (TREE_CODE (t
) == TEMPLATE_DECL
)
4389 /* A member template. */
4390 templates
= tree_cons (NULL_TREE
, t
, templates
);
4392 add_template_candidate (candidates
, t
, base
, explicit_targs
,
4394 TREE_TYPE (name
), flags
, DEDUCE_CALL
);
4396 else if (! template_only
)
4397 candidates
= add_function_candidate (candidates
, t
, base
,
4398 this_arglist
, flags
);
4401 candidates
->basetype_path
= basetype_path
;
4405 if (! any_viable (candidates
))
4407 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4408 if (flags
& LOOKUP_SPECULATIVELY
)
4410 if (!COMPLETE_TYPE_P (basetype
))
4411 incomplete_type_error (instance_ptr
, basetype
);
4413 cp_error ("no matching function for call to `%T::%D (%A)%V'",
4414 basetype
, pretty_name
, user_args
,
4415 TREE_TYPE (TREE_TYPE (instance_ptr
)));
4416 print_z_candidates (candidates
);
4417 return error_mark_node
;
4419 candidates
= splice_viable (candidates
);
4420 cand
= tourney (candidates
);
4424 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name
,
4426 print_z_candidates (candidates
);
4427 return error_mark_node
;
4430 if (DECL_PURE_VIRTUAL_P (cand
->fn
)
4431 && instance
== current_class_ref
4432 && (DECL_CONSTRUCTOR_P (current_function_decl
)
4433 || DECL_DESTRUCTOR_P (current_function_decl
))
4434 && ! (flags
& LOOKUP_NONVIRTUAL
)
4435 && value_member (cand
->fn
, CLASSTYPE_PURE_VIRTUALS (basetype
)))
4436 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl
) ?
4437 "abstract virtual `%#D' called from constructor"
4438 : "abstract virtual `%#D' called from destructor"),
4440 if (TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
4441 && is_dummy_object (instance_ptr
))
4443 cp_error ("cannot call member function `%D' without object", cand
->fn
);
4444 return error_mark_node
;
4447 if (DECL_VINDEX (cand
->fn
) && ! (flags
& LOOKUP_NONVIRTUAL
)
4448 && ((instance
== current_class_ref
&& (dtor_label
|| ctor_label
))
4449 || resolves_to_fixed_type_p (instance
, 0)))
4450 flags
|= LOOKUP_NONVIRTUAL
;
4452 return build_over_call
4454 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
? mem_args
: args
,
4458 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4459 subsequence of ICS2. */
4462 is_subseq (ics1
, ics2
)
4465 /* We can assume that a conversion of the same code
4466 between the same types indicates a subsequence since we only get
4467 here if the types we are converting from are the same. */
4469 while (TREE_CODE (ics1
) == RVALUE_CONV
4470 || TREE_CODE (ics1
) == LVALUE_CONV
)
4471 ics1
= TREE_OPERAND (ics1
, 0);
4475 while (TREE_CODE (ics2
) == RVALUE_CONV
4476 || TREE_CODE (ics2
) == LVALUE_CONV
)
4477 ics2
= TREE_OPERAND (ics2
, 0);
4479 if (TREE_CODE (ics2
) == USER_CONV
4480 || TREE_CODE (ics2
) == AMBIG_CONV
4481 || TREE_CODE (ics2
) == IDENTITY_CONV
)
4482 /* At this point, ICS1 cannot be a proper subsequence of
4483 ICS2. We can get a USER_CONV when we are comparing the
4484 second standard conversion sequence of two user conversion
4488 ics2
= TREE_OPERAND (ics2
, 0);
4490 if (TREE_CODE (ics2
) == TREE_CODE (ics1
)
4491 && same_type_p (TREE_TYPE (ics2
), TREE_TYPE (ics1
))
4492 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2
, 0)),
4493 TREE_TYPE (TREE_OPERAND (ics1
, 0))))
4498 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4499 be any _TYPE nodes. */
4502 is_properly_derived_from (derived
, base
)
4506 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived
))
4507 || !IS_AGGR_TYPE_CODE (TREE_CODE (base
)))
4510 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4511 considers every class derived from itself. */
4512 return (!same_type_ignoring_top_level_qualifiers_p (derived
, base
)
4513 && DERIVED_FROM_P (base
, derived
));
4516 /* We build the ICS for an implicit object parameter as a pointer
4517 conversion sequence. However, such a sequence should be compared
4518 as if it were a reference conversion sequence. If ICS is the
4519 implicit conversion sequence for an implicit object parameter,
4520 modify it accordingly. */
4523 maybe_handle_implicit_object (ics
)
4526 if (ICS_THIS_FLAG (*ics
))
4528 /* [over.match.funcs]
4530 For non-static member functions, the type of the
4531 implicit object parameter is "reference to cv X"
4532 where X is the class of which the function is a
4533 member and cv is the cv-qualification on the member
4534 function declaration. */
4536 tree reference_type
;
4538 /* The `this' parameter is a pointer to a class type. Make the
4539 implict conversion talk about a reference to that same class
4541 reference_type
= TREE_TYPE (TREE_TYPE (*ics
));
4542 reference_type
= build_reference_type (reference_type
);
4544 if (TREE_CODE (t
) == QUAL_CONV
)
4545 t
= TREE_OPERAND (t
, 0);
4546 if (TREE_CODE (t
) == PTR_CONV
)
4547 t
= TREE_OPERAND (t
, 0);
4548 t
= build1 (IDENTITY_CONV
, TREE_TYPE (TREE_TYPE (t
)), NULL_TREE
);
4549 t
= direct_reference_binding (reference_type
, t
);
4554 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4555 to the type the reference originally referred to, and return 1.
4556 Otherwise, return 0. */
4559 maybe_handle_ref_bind (ics
, target_type
)
4563 if (TREE_CODE (*ics
) == REF_BIND
)
4565 *target_type
= TREE_TYPE (TREE_TYPE (*ics
));
4566 *ics
= TREE_OPERAND (*ics
, 0);
4573 /* Compare two implicit conversion sequences according to the rules set out in
4574 [over.ics.rank]. Return values:
4576 1: ics1 is better than ics2
4577 -1: ics2 is better than ics1
4578 0: ics1 and ics2 are indistinguishable */
4581 compare_ics (ics1
, ics2
)
4588 tree deref_from_type1
= NULL_TREE
;
4589 tree deref_from_type2
= NULL_TREE
;
4590 tree deref_to_type1
= NULL_TREE
;
4591 tree deref_to_type2
= NULL_TREE
;
4593 /* REF_BINDING is non-zero if the result of the conversion sequence
4594 is a reference type. In that case TARGET_TYPE is the
4595 type referred to by the reference. */
4601 /* Handle implicit object parameters. */
4602 maybe_handle_implicit_object (&ics1
);
4603 maybe_handle_implicit_object (&ics2
);
4605 /* Handle reference parameters. */
4606 ref_binding1
= maybe_handle_ref_bind (&ics1
, &target_type1
);
4607 ref_binding2
= maybe_handle_ref_bind (&ics2
, &target_type2
);
4611 When comparing the basic forms of implicit conversion sequences (as
4612 defined in _over.best.ics_)
4614 --a standard conversion sequence (_over.ics.scs_) is a better
4615 conversion sequence than a user-defined conversion sequence
4616 or an ellipsis conversion sequence, and
4618 --a user-defined conversion sequence (_over.ics.user_) is a
4619 better conversion sequence than an ellipsis conversion sequence
4620 (_over.ics.ellipsis_). */
4621 if (ICS_RANK (ics1
) > ICS_RANK (ics2
))
4623 else if (ICS_RANK (ics1
) < ICS_RANK (ics2
))
4626 if (ICS_RANK (ics1
) == BAD_RANK
)
4628 /* Both ICS are bad. We try to make a decision based on what
4629 would have happenned if they'd been good. */
4630 if (ICS_USER_FLAG (ics1
) > ICS_USER_FLAG (ics2
)
4631 || ICS_STD_RANK (ics1
) > ICS_STD_RANK (ics2
))
4633 else if (ICS_USER_FLAG (ics1
) < ICS_USER_FLAG (ics2
)
4634 || ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
4637 /* We couldn't make up our minds; try to figure it out below. */
4640 if (ICS_ELLIPSIS_FLAG (ics1
))
4641 /* Both conversions are ellipsis conversions. */
4644 /* User-defined conversion sequence U1 is a better conversion sequence
4645 than another user-defined conversion sequence U2 if they contain the
4646 same user-defined conversion operator or constructor and if the sec-
4647 ond standard conversion sequence of U1 is better than the second
4648 standard conversion sequence of U2. */
4650 if (ICS_USER_FLAG (ics1
))
4654 for (t1
= ics1
; TREE_CODE (t1
) != USER_CONV
; t1
= TREE_OPERAND (t1
, 0))
4655 if (TREE_CODE (t1
) == AMBIG_CONV
)
4657 for (t2
= ics2
; TREE_CODE (t2
) != USER_CONV
; t2
= TREE_OPERAND (t2
, 0))
4658 if (TREE_CODE (t2
) == AMBIG_CONV
)
4661 if (USER_CONV_FN (t1
) != USER_CONV_FN (t2
))
4664 /* We can just fall through here, after setting up
4665 FROM_TYPE1 and FROM_TYPE2. */
4666 from_type1
= TREE_TYPE (t1
);
4667 from_type2
= TREE_TYPE (t2
);
4671 /* We're dealing with two standard conversion sequences.
4675 Standard conversion sequence S1 is a better conversion
4676 sequence than standard conversion sequence S2 if
4678 --S1 is a proper subsequence of S2 (comparing the conversion
4679 sequences in the canonical form defined by _over.ics.scs_,
4680 excluding any Lvalue Transformation; the identity
4681 conversion sequence is considered to be a subsequence of
4682 any non-identity conversion sequence */
4685 while (TREE_CODE (from_type1
) != IDENTITY_CONV
)
4686 from_type1
= TREE_OPERAND (from_type1
, 0);
4687 from_type1
= TREE_TYPE (from_type1
);
4690 while (TREE_CODE (from_type2
) != IDENTITY_CONV
)
4691 from_type2
= TREE_OPERAND (from_type2
, 0);
4692 from_type2
= TREE_TYPE (from_type2
);
4695 if (same_type_p (from_type1
, from_type2
))
4697 if (is_subseq (ics1
, ics2
))
4699 if (is_subseq (ics2
, ics1
))
4702 /* Otherwise, one sequence cannot be a subsequence of the other; they
4703 don't start with the same type. This can happen when comparing the
4704 second standard conversion sequence in two user-defined conversion
4711 --the rank of S1 is better than the rank of S2 (by the rules
4714 Standard conversion sequences are ordered by their ranks: an Exact
4715 Match is a better conversion than a Promotion, which is a better
4716 conversion than a Conversion.
4718 Two conversion sequences with the same rank are indistinguishable
4719 unless one of the following rules applies:
4721 --A conversion that is not a conversion of a pointer, or pointer
4722 to member, to bool is better than another conversion that is such
4725 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4726 so that we do not have to check it explicitly. */
4727 if (ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
4729 else if (ICS_STD_RANK (ics2
) < ICS_STD_RANK (ics1
))
4732 to_type1
= TREE_TYPE (ics1
);
4733 to_type2
= TREE_TYPE (ics2
);
4735 if (TYPE_PTR_P (from_type1
)
4736 && TYPE_PTR_P (from_type2
)
4737 && TYPE_PTR_P (to_type1
)
4738 && TYPE_PTR_P (to_type2
))
4740 deref_from_type1
= TREE_TYPE (from_type1
);
4741 deref_from_type2
= TREE_TYPE (from_type2
);
4742 deref_to_type1
= TREE_TYPE (to_type1
);
4743 deref_to_type2
= TREE_TYPE (to_type2
);
4745 /* The rules for pointers to members A::* are just like the rules
4746 for pointers A*, except opposite: if B is derived from A then
4747 A::* converts to B::*, not vice versa. For that reason, we
4748 switch the from_ and to_ variables here. */
4749 else if (TYPE_PTRMEM_P (from_type1
)
4750 && TYPE_PTRMEM_P (from_type2
)
4751 && TYPE_PTRMEM_P (to_type1
)
4752 && TYPE_PTRMEM_P (to_type2
))
4754 deref_to_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1
));
4755 deref_to_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2
));
4756 deref_from_type1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1
));
4757 deref_from_type2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2
));
4759 else if (TYPE_PTRMEMFUNC_P (from_type1
)
4760 && TYPE_PTRMEMFUNC_P (from_type2
)
4761 && TYPE_PTRMEMFUNC_P (to_type1
)
4762 && TYPE_PTRMEMFUNC_P (to_type2
))
4764 deref_to_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1
);
4765 deref_to_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2
);
4766 deref_from_type1
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1
);
4767 deref_from_type2
= TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2
);
4770 if (deref_from_type1
!= NULL_TREE
4771 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1
))
4772 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2
)))
4774 /* This was one of the pointer or pointer-like conversions.
4778 --If class B is derived directly or indirectly from class A,
4779 conversion of B* to A* is better than conversion of B* to
4780 void*, and conversion of A* to void* is better than
4781 conversion of B* to void*. */
4782 if (TREE_CODE (deref_to_type1
) == VOID_TYPE
4783 && TREE_CODE (deref_to_type2
) == VOID_TYPE
)
4785 if (is_properly_derived_from (deref_from_type1
,
4788 else if (is_properly_derived_from (deref_from_type2
,
4792 else if (TREE_CODE (deref_to_type1
) == VOID_TYPE
4793 || TREE_CODE (deref_to_type2
) == VOID_TYPE
)
4795 if (same_type_p (deref_from_type1
, deref_from_type2
))
4797 if (TREE_CODE (deref_to_type2
) == VOID_TYPE
)
4799 if (is_properly_derived_from (deref_from_type1
,
4803 /* We know that DEREF_TO_TYPE1 is `void' here. */
4804 else if (is_properly_derived_from (deref_from_type1
,
4809 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1
))
4810 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2
)))
4814 --If class B is derived directly or indirectly from class A
4815 and class C is derived directly or indirectly from B,
4817 --conversion of C* to B* is better than conversion of C* to
4820 --conversion of B* to A* is better than conversion of C* to
4822 if (same_type_p (deref_from_type1
, deref_from_type2
))
4824 if (is_properly_derived_from (deref_to_type1
,
4827 else if (is_properly_derived_from (deref_to_type2
,
4831 else if (same_type_p (deref_to_type1
, deref_to_type2
))
4833 if (is_properly_derived_from (deref_from_type2
,
4836 else if (is_properly_derived_from (deref_from_type1
,
4842 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1
))
4843 && same_type_p (from_type1
, from_type2
))
4847 --binding of an expression of type C to a reference of type
4848 B& is better than binding an expression of type C to a
4849 reference of type A&
4851 --conversion of C to B is better than conversion of C to A, */
4852 if (is_properly_derived_from (from_type1
, to_type1
)
4853 && is_properly_derived_from (from_type1
, to_type2
))
4855 if (is_properly_derived_from (to_type1
, to_type2
))
4857 else if (is_properly_derived_from (to_type2
, to_type1
))
4861 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1
))
4862 && same_type_p (to_type1
, to_type2
))
4866 --binding of an expression of type B to a reference of type
4867 A& is better than binding an expression of type C to a
4868 reference of type A&,
4870 --onversion of B to A is better than conversion of C to A */
4871 if (is_properly_derived_from (from_type1
, to_type1
)
4872 && is_properly_derived_from (from_type2
, to_type1
))
4874 if (is_properly_derived_from (from_type2
, from_type1
))
4876 else if (is_properly_derived_from (from_type1
, from_type2
))
4883 --S1 and S2 differ only in their qualification conversion and yield
4884 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
4885 qualification signature of type T1 is a proper subset of the cv-
4886 qualification signature of type T2 */
4887 if (TREE_CODE (ics1
) == QUAL_CONV
4888 && TREE_CODE (ics2
) == QUAL_CONV
4889 && same_type_p (from_type1
, from_type2
))
4890 return comp_cv_qual_signature (to_type1
, to_type2
);
4894 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
4895 types to which the references refer are the same type except for
4896 top-level cv-qualifiers, and the type to which the reference
4897 initialized by S2 refers is more cv-qualified than the type to
4898 which the reference initialized by S1 refers */
4900 if (ref_binding1
&& ref_binding2
4901 && same_type_ignoring_top_level_qualifiers_p (to_type1
, to_type2
))
4902 return comp_cv_qualification (target_type2
, target_type1
);
4904 /* Neither conversion sequence is better than the other. */
4908 /* The source type for this standard conversion sequence. */
4914 for (;; t
= TREE_OPERAND (t
, 0))
4916 if (TREE_CODE (t
) == USER_CONV
4917 || TREE_CODE (t
) == AMBIG_CONV
4918 || TREE_CODE (t
) == IDENTITY_CONV
)
4919 return TREE_TYPE (t
);
4921 my_friendly_abort (1823);
4924 /* Note a warning about preferring WINNER to LOSER. We do this by storing
4925 a pointer to LOSER and re-running joust to produce the warning if WINNER
4926 is actually used. */
4929 add_warning (winner
, loser
)
4930 struct z_candidate
*winner
, *loser
;
4932 winner
->warnings
= tree_cons (NULL_PTR
,
4933 build_expr_ptr_wrapper (loser
),
4937 /* Returns true iff functions are equivalent. Equivalent functions are
4938 not '==' only if one is a function-local extern function or if
4939 both are extern "C". */
4942 equal_functions (fn1
, fn2
)
4946 if (DECL_LOCAL_FUNCTION_P (fn1
) || DECL_LOCAL_FUNCTION_P (fn2
)
4947 || DECL_EXTERN_C_FUNCTION_P (fn1
))
4948 return decls_match (fn1
, fn2
);
4952 /* Compare two candidates for overloading as described in
4953 [over.match.best]. Return values:
4955 1: cand1 is better than cand2
4956 -1: cand2 is better than cand1
4957 0: cand1 and cand2 are indistinguishable */
4960 joust (cand1
, cand2
, warn
)
4961 struct z_candidate
*cand1
, *cand2
;
4965 int i
, off1
= 0, off2
= 0, len
;
4967 /* Candidates that involve bad conversions are always worse than those
4969 if (cand1
->viable
> cand2
->viable
)
4971 if (cand1
->viable
< cand2
->viable
)
4974 /* If we have two pseudo-candidates for conversions to the same type,
4975 or two candidates for the same function, arbitrarily pick one. */
4976 if (cand1
->fn
== cand2
->fn
4977 && (TYPE_P (cand1
->fn
) || DECL_P (cand1
->fn
)))
4980 /* a viable function F1
4981 is defined to be a better function than another viable function F2 if
4982 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4983 ICSi(F2), and then */
4985 /* for some argument j, ICSj(F1) is a better conversion sequence than
4988 /* For comparing static and non-static member functions, we ignore
4989 the implicit object parameter of the non-static function. The
4990 standard says to pretend that the static function has an object
4991 parm, but that won't work with operator overloading. */
4992 len
= TREE_VEC_LENGTH (cand1
->convs
);
4993 if (len
!= TREE_VEC_LENGTH (cand2
->convs
))
4995 if (DECL_STATIC_FUNCTION_P (cand1
->fn
)
4996 && ! DECL_STATIC_FUNCTION_P (cand2
->fn
))
4998 else if (! DECL_STATIC_FUNCTION_P (cand1
->fn
)
4999 && DECL_STATIC_FUNCTION_P (cand2
->fn
))
5005 my_friendly_abort (42);
5008 for (i
= 0; i
< len
; ++i
)
5010 tree t1
= TREE_VEC_ELT (cand1
->convs
, i
+off1
);
5011 tree t2
= TREE_VEC_ELT (cand2
->convs
, i
+off2
);
5012 int comp
= compare_ics (t1
, t2
);
5017 && ICS_RANK (t1
) + ICS_RANK (t2
) == STD_RANK
+ PROMO_RANK
5018 && TREE_CODE (t1
) == STD_CONV
5019 && TREE_CODE (t2
) == STD_CONV
5020 && TREE_CODE (TREE_TYPE (t1
)) == INTEGER_TYPE
5021 && TREE_CODE (TREE_TYPE (t2
)) == INTEGER_TYPE
5022 && (TYPE_PRECISION (TREE_TYPE (t1
))
5023 == TYPE_PRECISION (TREE_TYPE (t2
)))
5024 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5025 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1
, 0)))
5028 tree type
= TREE_TYPE (TREE_OPERAND (t1
, 0));
5030 struct z_candidate
*w
, *l
;
5032 type1
= TREE_TYPE (t1
), type2
= TREE_TYPE (t2
),
5033 w
= cand1
, l
= cand2
;
5035 type1
= TREE_TYPE (t2
), type2
= TREE_TYPE (t1
),
5036 w
= cand2
, l
= cand1
;
5040 cp_warning ("passing `%T' chooses `%T' over `%T'",
5041 type
, type1
, type2
);
5042 cp_warning (" in call to `%D'", w
->fn
);
5048 if (winner
&& comp
!= winner
)
5057 /* warn about confusing overload resolution for user-defined conversions,
5058 either between a constructor and a conversion op, or between two
5060 if (winner
&& cand1
->second_conv
5061 && ((DECL_CONSTRUCTOR_P (cand1
->fn
)
5062 != DECL_CONSTRUCTOR_P (cand2
->fn
))
5063 /* Don't warn if the two conv ops convert to the same type... */
5064 || (! DECL_CONSTRUCTOR_P (cand1
->fn
)
5065 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1
->fn
)),
5066 TREE_TYPE (TREE_TYPE (cand2
->fn
))))))
5068 int comp
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5071 struct z_candidate
*w
, *l
;
5074 w
= cand1
, l
= cand2
;
5076 w
= cand2
, l
= cand1
;
5077 if (DECL_CONTEXT (cand1
->fn
) == DECL_CONTEXT (cand2
->fn
)
5078 && ! DECL_CONSTRUCTOR_P (cand1
->fn
)
5079 && ! DECL_CONSTRUCTOR_P (cand2
->fn
)
5080 && (convn
= standard_conversion
5081 (TREE_TYPE (TREE_TYPE (l
->fn
)),
5082 TREE_TYPE (TREE_TYPE (w
->fn
)), NULL_TREE
))
5083 && TREE_CODE (convn
) == QUAL_CONV
)
5084 /* Don't complain about `operator char *()' beating
5085 `operator const char *() const'. */;
5088 tree source
= source_type (TREE_VEC_ELT (w
->convs
, 0));
5089 if (! DECL_CONSTRUCTOR_P (w
->fn
))
5090 source
= TREE_TYPE (source
);
5091 cp_warning ("choosing `%D' over `%D'", w
->fn
, l
->fn
);
5092 cp_warning (" for conversion from `%T' to `%T'",
5093 source
, TREE_TYPE (w
->second_conv
));
5094 cp_warning (" because conversion sequence for the argument is better");
5105 F1 is a non-template function and F2 is a template function */
5107 if (! cand1
->template && cand2
->template)
5109 else if (cand1
->template && ! cand2
->template)
5111 else if (cand1
->template && cand2
->template)
5112 winner
= more_specialized
5113 (TI_TEMPLATE (cand1
->template), TI_TEMPLATE (cand2
->template),
5117 the context is an initialization by user-defined conversion (see
5118 _dcl.init_ and _over.match.user_) and the standard conversion
5119 sequence from the return type of F1 to the destination type (i.e.,
5120 the type of the entity being initialized) is a better conversion
5121 sequence than the standard conversion sequence from the return type
5122 of F2 to the destination type. */
5124 if (! winner
&& cand1
->second_conv
)
5125 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
5127 /* If the built-in candidates are the same, arbitrarily pick one. */
5128 if (! winner
&& cand1
->fn
== cand2
->fn
5129 && TREE_CODE (cand1
->fn
) == IDENTIFIER_NODE
)
5131 for (i
= 0; i
< len
; ++i
)
5132 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1
->convs
, i
)),
5133 TREE_TYPE (TREE_VEC_ELT (cand2
->convs
, i
))))
5135 if (i
== TREE_VEC_LENGTH (cand1
->convs
))
5138 /* Kludge around broken overloading rules whereby
5139 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5140 that takes references and another that takes values. */
5141 if (cand1
->fn
== ansi_opname (COND_EXPR
))
5143 tree c1
= TREE_VEC_ELT (cand1
->convs
, 1);
5144 tree c2
= TREE_VEC_ELT (cand2
->convs
, 1);
5145 tree t1
= strip_top_quals (non_reference (TREE_TYPE (c1
)));
5146 tree t2
= strip_top_quals (non_reference (TREE_TYPE (c2
)));
5148 if (same_type_p (t1
, t2
))
5150 if (TREE_CODE (c1
) == REF_BIND
&& TREE_CODE (c2
) != REF_BIND
)
5152 if (TREE_CODE (c1
) != REF_BIND
&& TREE_CODE (c2
) == REF_BIND
)
5158 /* If the two functions are the same (this can happen with declarations
5159 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5160 if (DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
5161 && equal_functions (cand1
->fn
, cand2
->fn
))
5166 /* Extension: If the worst conversion for one candidate is worse than the
5167 worst conversion for the other, take the first. */
5168 if (! winner
&& ! pedantic
)
5170 int rank1
= IDENTITY_RANK
, rank2
= IDENTITY_RANK
;
5172 for (i
= 0; i
< len
; ++i
)
5174 if (ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
)) > rank1
)
5175 rank1
= ICS_RANK (TREE_VEC_ELT (cand1
->convs
, i
+off1
));
5176 if (ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
)) > rank2
)
5177 rank2
= ICS_RANK (TREE_VEC_ELT (cand2
->convs
, i
+off2
));
5189 /* Given a list of candidates for overloading, find the best one, if any.
5190 This algorithm has a worst case of O(2n) (winner is last), and a best
5191 case of O(n/2) (totally ambiguous); much better than a sorting
5194 static struct z_candidate
*
5195 tourney (candidates
)
5196 struct z_candidate
*candidates
;
5198 struct z_candidate
*champ
= candidates
, *challenger
;
5200 int champ_compared_to_predecessor
= 0;
5202 /* Walk through the list once, comparing each current champ to the next
5203 candidate, knocking out a candidate or two with each comparison. */
5205 for (challenger
= champ
->next
; challenger
; )
5207 fate
= joust (champ
, challenger
, 0);
5209 challenger
= challenger
->next
;
5214 champ
= challenger
->next
;
5217 champ_compared_to_predecessor
= 0;
5222 champ_compared_to_predecessor
= 1;
5225 challenger
= champ
->next
;
5229 /* Make sure the champ is better than all the candidates it hasn't yet
5230 been compared to. */
5232 for (challenger
= candidates
;
5234 && !(champ_compared_to_predecessor
&& challenger
->next
== champ
);
5235 challenger
= challenger
->next
)
5237 fate
= joust (champ
, challenger
, 0);
5245 /* Returns non-zero if things of type FROM can be converted to TO. */
5248 can_convert (to
, from
)
5251 return can_convert_arg (to
, from
, NULL_TREE
);
5254 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5257 can_convert_arg (to
, from
, arg
)
5260 tree t
= implicit_conversion (to
, from
, arg
, LOOKUP_NORMAL
);
5261 return (t
&& ! ICS_BAD_FLAG (t
));
5264 /* Convert EXPR to TYPE. Return the converted expression. */
5267 perform_implicit_conversion (type
, expr
)
5273 if (expr
== error_mark_node
)
5274 return error_mark_node
;
5275 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
5277 if (!conv
|| ICS_BAD_FLAG (conv
))
5279 cp_error ("could not convert `%E' to `%T'", expr
, type
);
5280 return error_mark_node
;
5283 return convert_like (conv
, expr
);
5286 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5287 initializing a variable of that TYPE. Return the converted
5291 initialize_reference (type
, expr
)
5297 conv
= reference_binding (type
, TREE_TYPE (expr
), expr
, LOOKUP_NORMAL
);
5298 if (!conv
|| ICS_BAD_FLAG (conv
))
5300 cp_error ("could not convert `%E' to `%T'", expr
, type
);
5301 return error_mark_node
;
5304 return convert_like (conv
, expr
);