1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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. */
38 extern int inhibit_warnings;
40 static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
42 static tree build_field_call PARAMS ((tree, tree, tree, tree));
43 static struct z_candidate * tourney PARAMS ((struct z_candidate *));
44 static int equal_functions PARAMS ((tree, tree));
45 static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
46 static int compare_ics PARAMS ((tree, tree));
47 static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
48 static tree build_java_interface_fn_ref PARAMS ((tree, tree));
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);
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)) == FUNCTION_TYPE
160 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
161 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
163 return build_function_call (instance, parms);
169 /* Returns nonzero iff the destructor name specified in NAME
170 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
174 check_dtor_name (basetype, name)
177 name = TREE_OPERAND (name, 0);
179 /* Just accept something we've already complained about. */
180 if (name == error_mark_node)
183 if (TREE_CODE (name) == TYPE_DECL)
184 name = TREE_TYPE (name);
185 else if (TYPE_P (name))
187 else if (TREE_CODE (name) == IDENTIFIER_NODE)
189 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
190 || (TREE_CODE (basetype) == ENUMERAL_TYPE
191 && name == TYPE_IDENTIFIER (basetype)))
194 name = get_type_value (name);
198 template <class T> struct S { ~S(); };
202 NAME will be a class template. */
203 else if (DECL_CLASS_TEMPLATE_P (name))
206 my_friendly_abort (980605);
208 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
213 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
214 This is how virtual function calls are avoided. */
217 build_scoped_method_call (exp, basetype, name, parms)
218 tree exp, basetype, name, parms;
220 /* Because this syntactic form does not allow
221 a pointer to a base class to be `stolen',
222 we need not protect the derived->base conversion
225 @@ But we do have to check access privileges later. */
227 tree type = TREE_TYPE (exp);
229 if (type == error_mark_node
230 || basetype == error_mark_node)
231 return error_mark_node;
233 if (processing_template_decl)
235 if (TREE_CODE (name) == BIT_NOT_EXPR
236 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
238 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
240 name = build_min_nt (BIT_NOT_EXPR, type);
242 name = build_min_nt (SCOPE_REF, basetype, name);
243 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
246 if (TREE_CODE (type) == REFERENCE_TYPE)
247 type = TREE_TYPE (type);
249 if (TREE_CODE (basetype) == TREE_VEC)
252 basetype = BINFO_TYPE (binfo);
257 /* Check the destructor call syntax. */
258 if (TREE_CODE (name) == BIT_NOT_EXPR)
260 /* We can get here if someone writes their destructor call like
261 `obj.NS::~T()'; this isn't really a scoped method call, so hand
263 if (TREE_CODE (basetype) == NAMESPACE_DECL)
264 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
266 if (! check_dtor_name (basetype, name))
267 cp_error ("qualified type `%T' does not match destructor name `~%T'",
268 basetype, TREE_OPERAND (name, 0));
270 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
271 that explicit ~int is caught in the parser; this deals with typedefs
272 and template parms. */
273 if (! IS_AGGR_TYPE (basetype))
275 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
276 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
277 exp, basetype, type);
279 return cp_convert (void_type_node, exp);
283 if (TREE_CODE (basetype) == NAMESPACE_DECL)
285 cp_error ("`%D' is a namespace", basetype);
286 return error_mark_node;
288 if (! is_aggr_type (basetype, 1))
289 return error_mark_node;
291 if (! IS_AGGR_TYPE (type))
293 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
295 return error_mark_node;
300 binfo = get_binfo (basetype, type, 1);
301 if (binfo == error_mark_node)
302 return error_mark_node;
304 error_not_base_type (basetype, type);
309 if (TREE_CODE (exp) == INDIRECT_REF)
310 decl = build_indirect_ref
311 (convert_pointer_to_real
312 (binfo, build_unary_op (ADDR_EXPR, exp, 0)), NULL);
314 decl = build_scoped_ref (exp, basetype);
316 /* Call to a destructor. */
317 if (TREE_CODE (name) == BIT_NOT_EXPR)
319 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
320 return cp_convert (void_type_node, exp);
322 return build_delete (TREE_TYPE (decl), decl,
323 sfk_complete_destructor,
324 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
328 /* Call to a method. */
329 return build_method_call (decl, name, parms, binfo,
330 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
332 return error_mark_node;
335 /* We want the address of a function or method. We avoid creating a
336 pointer-to-member function. */
339 build_addr_func (function)
342 tree type = TREE_TYPE (function);
344 /* We have to do these by hand to avoid real pointer to member
346 if (TREE_CODE (type) == METHOD_TYPE)
350 type = build_pointer_type (type);
352 if (mark_addressable (function) == 0)
353 return error_mark_node;
355 addr = build1 (ADDR_EXPR, type, function);
357 /* Address of a static or external variable or function counts
359 if (staticp (function))
360 TREE_CONSTANT (addr) = 1;
365 function = default_conversion (function);
370 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
371 POINTER_TYPE to those. Note, pointer to member function types
372 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
375 build_call (function, parms)
376 tree function, parms;
378 int is_constructor = 0;
384 function = build_addr_func (function);
386 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
388 sorry ("unable to call pointer to member function here");
389 return error_mark_node;
392 result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
394 if (TREE_CODE (function) == ADDR_EXPR
395 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
396 decl = TREE_OPERAND (function, 0);
400 /* We check both the decl and the type; a function may be known not to
401 throw without being declared throw(). */
402 nothrow = ((decl && TREE_NOTHROW (decl))
403 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
405 if (decl && DECL_CONSTRUCTOR_P (decl))
408 if (decl && ! TREE_USED (decl))
410 /* We invoke build_call directly for several library functions.
411 These may have been declared normally if we're building libgcc,
412 so we can't just check DECL_ARTIFICIAL. */
413 if (DECL_ARTIFICIAL (decl)
414 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
417 my_friendly_abort (990125);
420 /* Don't pass empty class objects by value. This is useful
421 for tags in STL, which are used to control overload resolution.
422 We don't need to handle other cases of copying empty classes. */
423 if (! decl || ! DECL_BUILT_IN (decl))
424 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
425 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
426 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
428 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
429 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
430 TREE_VALUE (tmp), t);
433 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
434 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
435 TREE_TYPE (function) = result_type;
436 TREE_SIDE_EFFECTS (function) = 1;
437 TREE_NOTHROW (function) = nothrow;
442 /* Build something of the form ptr->method (args)
443 or object.method (args). This can also build
444 calls to constructors, and find friends.
446 Member functions always take their class variable
449 INSTANCE is a class instance.
451 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
453 PARMS help to figure out what that NAME really refers to.
455 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
456 down to the real instance type to use for access checking. We need this
457 information to get protected accesses correct. This parameter is used
458 by build_member_call.
460 FLAGS is the logical disjunction of zero or more LOOKUP_
461 flags. See cp-tree.h for more info.
463 If this is all OK, calls build_function_call with the resolved
466 This function must also handle being called to perform
467 initialization, promotion/coercion of arguments, and
468 instantiation of default parameters.
470 Note that NAME may refer to an instance variable name. If
471 `operator()()' is defined for the type of that field, then we return
474 #ifdef GATHER_STATISTICS
475 extern int n_build_method_call;
479 build_method_call (instance, name, parms, basetype_path, flags)
480 tree instance, name, parms, basetype_path;
483 tree basetype, instance_ptr;
485 #ifdef GATHER_STATISTICS
486 n_build_method_call++;
489 if (instance == error_mark_node
490 || name == error_mark_node
491 || parms == error_mark_node
492 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
493 return error_mark_node;
495 if (processing_template_decl)
497 /* We need to process template parm names here so that tsubst catches
498 them properly. Other type names can wait. */
499 if (TREE_CODE (name) == BIT_NOT_EXPR)
501 tree type = NULL_TREE;
503 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
504 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
505 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
506 type = TREE_TYPE (TREE_OPERAND (name, 0));
508 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
509 name = build_min_nt (BIT_NOT_EXPR, type);
512 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
515 if (TREE_CODE (name) == BIT_NOT_EXPR)
518 error ("destructors take no parameters");
519 basetype = TREE_TYPE (instance);
520 if (TREE_CODE (basetype) == REFERENCE_TYPE)
521 basetype = TREE_TYPE (basetype);
523 if (! check_dtor_name (basetype, name))
525 ("destructor name `~%T' does not match type `%T' of expression",
526 TREE_OPERAND (name, 0), basetype);
528 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
529 return cp_convert (void_type_node, instance);
530 instance = default_conversion (instance);
531 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
532 return build_delete (build_pointer_type (basetype),
533 instance_ptr, sfk_complete_destructor,
534 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
537 return build_new_method_call (instance, name, parms, basetype_path, flags);
540 /* New overloading code. */
550 struct z_candidate *next;
553 #define IDENTITY_RANK 0
559 #define ELLIPSIS_RANK 6
562 #define ICS_RANK(NODE) \
563 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
564 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
565 : ICS_USER_FLAG (NODE) ? USER_RANK \
566 : ICS_STD_RANK (NODE))
568 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
570 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
571 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
572 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
573 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
575 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
576 should be created to hold the result of the conversion. */
577 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
579 #define USER_CONV_CAND(NODE) \
580 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
581 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
589 A null pointer constant is an integral constant expression
590 (_expr.const_) rvalue of integer type that evaluates to zero. */
592 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
598 /* Returns non-zero if PARMLIST consists of only default parms and/or
602 sufficient_parms_p (parmlist)
605 for (; parmlist && parmlist != void_list_node;
606 parmlist = TREE_CHAIN (parmlist))
607 if (!TREE_PURPOSE (parmlist))
613 build_conv (code, type, from)
618 int rank = ICS_STD_RANK (from);
620 /* We can't use buildl1 here because CODE could be USER_CONV, which
621 takes two arguments. In that case, the caller is responsible for
622 filling in the second argument. */
623 t = make_node (code);
624 TREE_TYPE (t) = type;
625 TREE_OPERAND (t, 0) = from;
638 if (rank < EXACT_RANK)
644 ICS_STD_RANK (t) = rank;
645 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
646 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
654 if (TREE_CODE (t) == REFERENCE_TYPE)
663 if (TREE_CODE (t) == ARRAY_TYPE)
665 return TYPE_MAIN_VARIANT (t);
668 /* Returns the standard conversion path (see [conv]) from type FROM to type
669 TO, if any. For proper handling of null pointer constants, you must
670 also pass the expression EXPR to convert from. */
673 standard_conversion (to, from, expr)
676 enum tree_code fcode, tcode;
680 if (TREE_CODE (to) == REFERENCE_TYPE)
682 if (TREE_CODE (from) == REFERENCE_TYPE)
685 from = TREE_TYPE (from);
687 to = strip_top_quals (to);
688 from = strip_top_quals (from);
690 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
691 && expr && type_unknown_p (expr))
693 expr = instantiate_type (to, expr, itf_none);
694 if (expr == error_mark_node)
696 from = TREE_TYPE (expr);
699 fcode = TREE_CODE (from);
700 tcode = TREE_CODE (to);
702 conv = build1 (IDENTITY_CONV, from, expr);
704 if (fcode == FUNCTION_TYPE)
706 from = build_pointer_type (from);
707 fcode = TREE_CODE (from);
708 conv = build_conv (LVALUE_CONV, from, conv);
710 else if (fcode == ARRAY_TYPE)
712 from = build_pointer_type (TREE_TYPE (from));
713 fcode = TREE_CODE (from);
714 conv = build_conv (LVALUE_CONV, from, conv);
716 else if (fromref || (expr && lvalue_p (expr)))
717 conv = build_conv (RVALUE_CONV, from, conv);
719 /* Allow conversion between `__complex__' data types */
720 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
722 /* The standard conversion sequence to convert FROM to TO is
723 the standard conversion sequence to perform componentwise
725 tree part_conv = standard_conversion
726 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
730 conv = build_conv (TREE_CODE (part_conv), to, conv);
731 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
739 if (same_type_p (from, to))
742 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
743 && expr && null_ptr_cst_p (expr))
745 conv = build_conv (STD_CONV, to, conv);
747 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
749 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
750 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
752 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
755 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
756 && ufcode != FUNCTION_TYPE)
758 from = build_pointer_type
759 (cp_build_qualified_type (void_type_node,
760 CP_TYPE_QUALS (TREE_TYPE (from))));
761 conv = build_conv (PTR_CONV, from, conv);
763 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
765 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
766 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
767 tree binfo = get_binfo (fbase, tbase, 1);
769 if (binfo && !binfo_from_vbase (binfo)
770 && (same_type_ignoring_top_level_qualifiers_p
771 (TREE_TYPE (TREE_TYPE (from)),
772 TREE_TYPE (TREE_TYPE (to)))))
774 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
775 from = build_pointer_type (from);
776 conv = build_conv (PMEM_CONV, from, conv);
779 else if (IS_AGGR_TYPE (TREE_TYPE (from))
780 && IS_AGGR_TYPE (TREE_TYPE (to)))
782 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
785 cp_build_qualified_type (TREE_TYPE (to),
786 CP_TYPE_QUALS (TREE_TYPE (from)));
787 from = build_pointer_type (from);
788 conv = build_conv (PTR_CONV, from, conv);
792 if (same_type_p (from, to))
794 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
795 conv = build_conv (QUAL_CONV, to, conv);
796 else if (expr && string_conv_p (to, expr, 0))
797 /* converting from string constant to char *. */
798 conv = build_conv (QUAL_CONV, to, conv);
799 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
801 conv = build_conv (PTR_CONV, to, conv);
802 ICS_BAD_FLAG (conv) = 1;
809 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
811 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
812 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
813 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
814 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
815 tree binfo = get_binfo (fbase, tbase, 1);
817 if (!binfo || binfo_from_vbase (binfo)
818 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
819 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
820 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
821 || CP_TYPE_QUALS (fbase) != CP_TYPE_QUALS (tbase))
824 from = cp_build_qualified_type (tbase, CP_TYPE_QUALS (fbase));
825 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
826 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
827 from = build_ptrmemfunc_type (build_pointer_type (from));
828 conv = build_conv (PMEM_CONV, from, conv);
830 else if (tcode == BOOLEAN_TYPE)
832 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
833 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
836 conv = build_conv (STD_CONV, to, conv);
837 if (fcode == POINTER_TYPE
838 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
839 ICS_STD_RANK (conv) = PBOOL_RANK;
841 /* We don't check for ENUMERAL_TYPE here because there are no standard
842 conversions to enum type. */
843 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
844 || tcode == REAL_TYPE)
846 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
848 conv = build_conv (STD_CONV, to, conv);
850 /* Give this a better rank if it's a promotion. */
851 if (to == type_promotes_to (from)
852 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
853 ICS_STD_RANK (conv) = PROMO_RANK;
855 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
856 && is_properly_derived_from (from, to))
858 if (TREE_CODE (conv) == RVALUE_CONV)
859 conv = TREE_OPERAND (conv, 0);
860 conv = build_conv (BASE_CONV, to, conv);
861 /* The derived-to-base conversion indicates the initialization
862 of a parameter with base type from an object of a derived
863 type. A temporary object is created to hold the result of
865 NEED_TEMPORARY_P (conv) = 1;
873 /* Returns non-zero if T1 is reference-related to T2. */
876 reference_related_p (t1, t2)
880 t1 = TYPE_MAIN_VARIANT (t1);
881 t2 = TYPE_MAIN_VARIANT (t2);
885 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
886 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
888 return (same_type_p (t1, t2)
889 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
890 && DERIVED_FROM_P (t1, t2)));
893 /* Returns non-zero if T1 is reference-compatible with T2. */
896 reference_compatible_p (t1, t2)
902 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
903 reference-related to T2 and cv1 is the same cv-qualification as,
904 or greater cv-qualification than, cv2. */
905 return (reference_related_p (t1, t2)
906 && at_least_as_qualified_p (t1, t2));
909 /* Determine whether or not the EXPR (of class type S) can be
910 converted to T as in [over.match.ref]. */
913 convert_class_to_reference (t, s, expr)
921 struct z_candidate *candidates;
922 struct z_candidate *cand;
926 Assuming that "cv1 T" is the underlying type of the reference
927 being initialized, and "cv S" is the type of the initializer
928 expression, with S a class type, the candidate functions are
931 --The conversion functions of S and its base classes are
932 considered. Those that are not hidden within S and yield type
933 "reference to cv2 T2", where "cv1 T" is reference-compatible
934 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
936 The argument list has one argument, which is the initializer
941 /* Conceptually, we should take the address of EXPR and put it in
942 the argument list. Unfortunately, however, that can result in
943 error messages, which we should not issue now because we are just
944 trying to find a conversion operator. Therefore, we use NULL,
945 cast to the appropriate type. */
946 arglist = build_int_2 (0, 0);
947 TREE_TYPE (arglist) = build_pointer_type (s);
948 arglist = build_tree_list (NULL_TREE, arglist);
950 for (conversions = lookup_conversions (s);
952 conversions = TREE_CHAIN (conversions))
954 tree fns = TREE_VALUE (conversions);
956 for (; fns; fns = OVL_NEXT (fns))
958 tree f = OVL_CURRENT (fns);
959 tree t2 = TREE_TYPE (TREE_TYPE (f));
960 struct z_candidate *old_candidates = candidates;
962 /* If this is a template function, try to get an exact
964 if (TREE_CODE (f) == TEMPLATE_DECL)
967 = add_template_candidate (candidates,
971 build_reference_type (t),
975 if (candidates != old_candidates)
977 /* Now, see if the conversion function really returns
978 an lvalue of the appropriate type. From the
979 point of view of unification, simply returning an
980 rvalue of the right type is good enough. */
982 t2 = TREE_TYPE (TREE_TYPE (f));
983 if (TREE_CODE (t2) != REFERENCE_TYPE
984 || !reference_compatible_p (t, TREE_TYPE (t2)))
985 candidates = candidates->next;
988 else if (TREE_CODE (t2) == REFERENCE_TYPE
989 && reference_compatible_p (t, TREE_TYPE (t2)))
991 = add_function_candidate (candidates, f, s, arglist,
994 if (candidates != old_candidates)
995 candidates->basetype_path = TYPE_BINFO (s);
999 /* If none of the conversion functions worked out, let our caller
1001 if (!any_viable (candidates))
1004 candidates = splice_viable (candidates);
1005 cand = tourney (candidates);
1009 conv = build1 (IDENTITY_CONV, s, expr);
1010 conv = build_conv (USER_CONV,
1011 non_reference (TREE_TYPE (TREE_TYPE (cand->fn))),
1013 TREE_OPERAND (conv, 1) = build_ptr_wrapper (cand);
1014 ICS_USER_FLAG (conv) = 1;
1015 if (cand->viable == -1)
1016 ICS_BAD_FLAG (conv) = 1;
1017 cand->second_conv = conv;
1022 /* A reference of the indicated TYPE is being bound directly to the
1023 expression represented by the implicit conversion sequence CONV.
1024 Return a conversion sequence for this binding. */
1027 direct_reference_binding (type, conv)
1031 tree t = TREE_TYPE (type);
1035 When a parameter of reference type binds directly
1036 (_dcl.init.ref_) to an argument expression, the implicit
1037 conversion sequence is the identity conversion, unless the
1038 argument expression has a type that is a derived class of the
1039 parameter type, in which case the implicit conversion sequence is
1040 a derived-to-base Conversion.
1042 If the parameter binds directly to the result of applying a
1043 conversion function to the argument expression, the implicit
1044 conversion sequence is a user-defined conversion sequence
1045 (_over.ics.user_), with the second standard conversion sequence
1046 either an identity conversion or, if the conversion function
1047 returns an entity of a type that is a derived class of the
1048 parameter type, a derived-to-base conversion. */
1049 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1051 /* Represent the derived-to-base conversion. */
1052 conv = build_conv (BASE_CONV, t, conv);
1053 /* We will actually be binding to the base-class subobject in
1054 the derived class, so we mark this conversion appropriately.
1055 That way, convert_like knows not to generate a temporary. */
1056 NEED_TEMPORARY_P (conv) = 0;
1058 return build_conv (REF_BIND, type, conv);
1061 /* Returns the conversion path from type FROM to reference type TO for
1062 purposes of reference binding. For lvalue binding, either pass a
1063 reference type to FROM or an lvalue expression to EXPR. If the
1064 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1065 the conversion returned. */
1068 reference_binding (rto, rfrom, expr, flags)
1069 tree rto, rfrom, expr;
1072 tree conv = NULL_TREE;
1073 tree to = TREE_TYPE (rto);
1077 cp_lvalue_kind lvalue_p = clk_none;
1079 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1081 expr = instantiate_type (to, expr, itf_none);
1082 if (expr == error_mark_node)
1084 from = TREE_TYPE (expr);
1087 if (TREE_CODE (from) == REFERENCE_TYPE)
1089 /* Anything with reference type is an lvalue. */
1090 lvalue_p = clk_ordinary;
1091 from = TREE_TYPE (from);
1094 lvalue_p = real_lvalue_p (expr);
1096 /* Figure out whether or not the types are reference-related and
1097 reference compatible. We have do do this after stripping
1098 references from FROM. */
1099 related_p = reference_related_p (to, from);
1100 compatible_p = reference_compatible_p (to, from);
1102 if (lvalue_p && compatible_p)
1106 If the intializer expression
1108 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1109 is reference-compatible with "cv2 T2,"
1111 the reference is bound directly to the initializer exprssion
1113 conv = build1 (IDENTITY_CONV, from, expr);
1114 conv = direct_reference_binding (rto, conv);
1115 if ((lvalue_p & clk_bitfield) != 0
1116 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1117 /* For the purposes of overload resolution, we ignore the fact
1118 this expression is a bitfield. (In particular,
1119 [over.ics.ref] says specifically that a function with a
1120 non-const reference parameter is viable even if the
1121 argument is a bitfield.)
1123 However, when we actually call the function we must create
1124 a temporary to which to bind the reference. If the
1125 reference is volatile, or isn't const, then we cannot make
1126 a temporary, so we just issue an error when the conversion
1128 NEED_TEMPORARY_P (conv) = 1;
1131 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1135 If the initializer exprsesion
1137 -- has a class type (i.e., T2 is a class type) can be
1138 implicitly converted to an lvalue of type "cv3 T3," where
1139 "cv1 T1" is reference-compatible with "cv3 T3". (this
1140 conversion is selected by enumerating the applicable
1141 conversion functions (_over.match.ref_) and choosing the
1142 best one through overload resolution. (_over.match_).
1144 the reference is bound to the lvalue result of the conversion
1145 in the second case. */
1146 conv = convert_class_to_reference (to, from, expr);
1148 return direct_reference_binding (rto, conv);
1151 /* From this point on, we conceptually need temporaries, even if we
1152 elide them. Only the cases above are "direct bindings". */
1153 if (flags & LOOKUP_NO_TEMP_BIND)
1158 When a parameter of reference type is not bound directly to an
1159 argument expression, the conversion sequence is the one required
1160 to convert the argument expression to the underlying type of the
1161 reference according to _over.best.ics_. Conceptually, this
1162 conversion sequence corresponds to copy-initializing a temporary
1163 of the underlying type with the argument expression. Any
1164 difference in top-level cv-qualification is subsumed by the
1165 initialization itself and does not constitute a conversion. */
1169 Otherwise, the reference shall be to a non-volatile const type. */
1170 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1175 If the initializer expression is an rvalue, with T2 a class type,
1176 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1177 is bound in one of the following ways:
1179 -- The reference is bound to the object represented by the rvalue
1180 or to a sub-object within that object.
1182 In this case, the implicit conversion sequence is supposed to be
1183 same as we would obtain by generating a temporary. Fortunately,
1184 if the types are reference compatible, then this is either an
1185 identity conversion or the derived-to-base conversion, just as
1186 for direct binding. */
1187 if (CLASS_TYPE_P (from) && compatible_p)
1189 conv = build1 (IDENTITY_CONV, from, expr);
1190 return direct_reference_binding (rto, conv);
1195 Otherwise, a temporary of type "cv1 T1" is created and
1196 initialized from the initializer expression using the rules for a
1197 non-reference copy initialization. If T1 is reference-related to
1198 T2, cv1 must be the same cv-qualification as, or greater
1199 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1200 if (related_p && !at_least_as_qualified_p (to, from))
1203 conv = implicit_conversion (to, from, expr, flags);
1207 conv = build_conv (REF_BIND, rto, conv);
1208 /* This reference binding, unlike those above, requires the
1209 creation of a temporary. */
1210 NEED_TEMPORARY_P (conv) = 1;
1215 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1216 to type TO. The optional expression EXPR may affect the conversion.
1217 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1221 implicit_conversion (to, from, expr, flags)
1222 tree to, from, expr;
1226 struct z_candidate *cand;
1228 /* Resolve expressions like `A::p' that we thought might become
1229 pointers-to-members. */
1230 if (expr && TREE_CODE (expr) == OFFSET_REF)
1232 expr = resolve_offset_ref (expr);
1233 from = TREE_TYPE (expr);
1236 if (from == error_mark_node || to == error_mark_node
1237 || expr == error_mark_node)
1240 /* Make sure both the FROM and TO types are complete so that
1241 user-defined conversions are available. */
1242 complete_type (from);
1245 if (TREE_CODE (to) == REFERENCE_TYPE)
1246 conv = reference_binding (to, from, expr, flags);
1248 conv = standard_conversion (to, from, expr);
1252 else if (expr != NULL_TREE
1253 && (IS_AGGR_TYPE (from)
1254 || IS_AGGR_TYPE (to))
1255 && (flags & LOOKUP_NO_CONVERSION) == 0)
1257 cand = build_user_type_conversion_1
1258 (to, expr, LOOKUP_ONLYCONVERTING);
1260 conv = cand->second_conv;
1262 /* We used to try to bind a reference to a temporary here, but that
1263 is now handled by the recursive call to this function at the end
1264 of reference_binding. */
1270 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1273 static struct z_candidate *
1274 add_candidate (candidates, fn, convs, viable)
1275 struct z_candidate *candidates;
1279 struct z_candidate *cand
1280 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1283 cand->convs = convs;
1284 cand->viable = viable;
1285 cand->next = candidates;
1290 /* Create an overload candidate for the function or method FN called with
1291 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1292 to implicit_conversion.
1294 CTYPE, if non-NULL, is the type we want to pretend this function
1295 comes from for purposes of overload resolution. */
1297 static struct z_candidate *
1298 add_function_candidate (candidates, fn, ctype, arglist, flags)
1299 struct z_candidate *candidates;
1300 tree fn, ctype, arglist;
1303 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1306 tree parmnode, argnode;
1309 /* The `this', `in_chrg' and VTT arguments to constructors are not
1310 considered in overload resolution. */
1311 if (DECL_CONSTRUCTOR_P (fn))
1313 parmlist = skip_artificial_parms_for (fn, parmlist);
1314 arglist = skip_artificial_parms_for (fn, arglist);
1317 len = list_length (arglist);
1318 convs = make_tree_vec (len);
1320 /* 13.3.2 - Viable functions [over.match.viable]
1321 First, to be a viable function, a candidate function shall have enough
1322 parameters to agree in number with the arguments in the list.
1324 We need to check this first; otherwise, checking the ICSes might cause
1325 us to produce an ill-formed template instantiation. */
1327 parmnode = parmlist;
1328 for (i = 0; i < len; ++i)
1330 if (parmnode == NULL_TREE || parmnode == void_list_node)
1332 parmnode = TREE_CHAIN (parmnode);
1335 if (i < len && parmnode)
1338 /* Make sure there are default args for the rest of the parms. */
1339 else if (!sufficient_parms_p (parmnode))
1345 /* Second, for F to be a viable function, there shall exist for each
1346 argument an implicit conversion sequence that converts that argument
1347 to the corresponding parameter of F. */
1349 parmnode = parmlist;
1352 for (i = 0; i < len; ++i)
1354 tree arg = TREE_VALUE (argnode);
1355 tree argtype = lvalue_type (arg);
1359 if (parmnode == void_list_node)
1362 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1363 && ! DECL_CONSTRUCTOR_P (fn));
1367 tree parmtype = TREE_VALUE (parmnode);
1369 /* The type of the implicit object parameter ('this') for
1370 overload resolution is not always the same as for the
1371 function itself; conversion functions are considered to
1372 be members of the class being converted, and functions
1373 introduced by a using-declaration are considered to be
1374 members of the class that uses them.
1376 Since build_over_call ignores the ICS for the `this'
1377 parameter, we can just change the parm type. */
1378 if (ctype && is_this)
1381 = build_qualified_type (ctype,
1382 TYPE_QUALS (TREE_TYPE (parmtype)));
1383 parmtype = build_pointer_type (parmtype);
1386 t = implicit_conversion (parmtype, argtype, arg, flags);
1390 t = build1 (IDENTITY_CONV, argtype, arg);
1391 ICS_ELLIPSIS_FLAG (t) = 1;
1395 ICS_THIS_FLAG (t) = 1;
1397 TREE_VEC_ELT (convs, i) = t;
1404 if (ICS_BAD_FLAG (t))
1408 parmnode = TREE_CHAIN (parmnode);
1409 argnode = TREE_CHAIN (argnode);
1413 return add_candidate (candidates, fn, convs, viable);
1416 /* Create an overload candidate for the conversion function FN which will
1417 be invoked for expression OBJ, producing a pointer-to-function which
1418 will in turn be called with the argument list ARGLIST, and add it to
1419 CANDIDATES. FLAGS is passed on to implicit_conversion.
1421 Actually, we don't really care about FN; we care about the type it
1422 converts to. There may be multiple conversion functions that will
1423 convert to that type, and we rely on build_user_type_conversion_1 to
1424 choose the best one; so when we create our candidate, we record the type
1425 instead of the function. */
1427 static struct z_candidate *
1428 add_conv_candidate (candidates, fn, obj, arglist)
1429 struct z_candidate *candidates;
1430 tree fn, obj, arglist;
1432 tree totype = TREE_TYPE (TREE_TYPE (fn));
1433 int i, len, viable, flags;
1434 tree parmlist, convs, parmnode, argnode;
1436 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1437 parmlist = TREE_TYPE (parmlist);
1438 parmlist = TYPE_ARG_TYPES (parmlist);
1440 len = list_length (arglist) + 1;
1441 convs = make_tree_vec (len);
1442 parmnode = parmlist;
1445 flags = LOOKUP_NORMAL;
1447 /* Don't bother looking up the same type twice. */
1448 if (candidates && candidates->fn == totype)
1451 for (i = 0; i < len; ++i)
1453 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1454 tree argtype = lvalue_type (arg);
1458 t = implicit_conversion (totype, argtype, arg, flags);
1459 else if (parmnode == void_list_node)
1462 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1465 t = build1 (IDENTITY_CONV, argtype, arg);
1466 ICS_ELLIPSIS_FLAG (t) = 1;
1469 TREE_VEC_ELT (convs, i) = t;
1473 if (ICS_BAD_FLAG (t))
1480 parmnode = TREE_CHAIN (parmnode);
1481 argnode = TREE_CHAIN (argnode);
1487 if (!sufficient_parms_p (parmnode))
1490 return add_candidate (candidates, totype, convs, viable);
1493 static struct z_candidate *
1494 build_builtin_candidate (candidates, fnname, type1, type2,
1495 args, argtypes, flags)
1496 struct z_candidate *candidates;
1497 tree fnname, type1, type2, *args, *argtypes;
1508 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1510 for (i = 0; i < 2; ++i)
1515 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1519 /* We need something for printing the candidate. */
1520 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1522 else if (ICS_BAD_FLAG (t))
1524 TREE_VEC_ELT (convs, i) = t;
1527 /* For COND_EXPR we rearranged the arguments; undo that now. */
1530 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1531 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1532 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1534 TREE_VEC_ELT (convs, 0) = t;
1539 return add_candidate (candidates, fnname, convs, viable);
1546 return COMPLETE_TYPE_P (complete_type (t));
1549 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1552 promoted_arithmetic_type_p (type)
1557 In this section, the term promoted integral type is used to refer
1558 to those integral types which are preserved by integral promotion
1559 (including e.g. int and long but excluding e.g. char).
1560 Similarly, the term promoted arithmetic type refers to promoted
1561 integral types plus floating types. */
1562 return ((INTEGRAL_TYPE_P (type)
1563 && same_type_p (type_promotes_to (type), type))
1564 || TREE_CODE (type) == REAL_TYPE);
1567 /* Create any builtin operator overload candidates for the operator in
1568 question given the converted operand types TYPE1 and TYPE2. The other
1569 args are passed through from add_builtin_candidates to
1570 build_builtin_candidate.
1572 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1573 If CODE is requires candidates operands of the same type of the kind
1574 of which TYPE1 and TYPE2 are, we add both candidates
1575 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1577 static struct z_candidate *
1578 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1579 args, argtypes, flags)
1580 struct z_candidate *candidates;
1581 enum tree_code code, code2;
1582 tree fnname, type1, type2, *args, *argtypes;
1587 case POSTINCREMENT_EXPR:
1588 case POSTDECREMENT_EXPR:
1589 args[1] = integer_zero_node;
1590 type2 = integer_type_node;
1599 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1600 and VQ is either volatile or empty, there exist candidate operator
1601 functions of the form
1602 VQ T& operator++(VQ T&);
1603 T operator++(VQ T&, int);
1604 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1605 type other than bool, and VQ is either volatile or empty, there exist
1606 candidate operator functions of the form
1607 VQ T& operator--(VQ T&);
1608 T operator--(VQ T&, int);
1609 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1610 complete object type, and VQ is either volatile or empty, there exist
1611 candidate operator functions of the form
1612 T*VQ& operator++(T*VQ&);
1613 T*VQ& operator--(T*VQ&);
1614 T* operator++(T*VQ&, int);
1615 T* operator--(T*VQ&, int); */
1617 case POSTDECREMENT_EXPR:
1618 case PREDECREMENT_EXPR:
1619 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1621 case POSTINCREMENT_EXPR:
1622 case PREINCREMENT_EXPR:
1623 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1625 type1 = build_reference_type (type1);
1630 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1631 exist candidate operator functions of the form
1635 8 For every function type T, there exist candidate operator functions of
1637 T& operator*(T*); */
1640 if (TREE_CODE (type1) == POINTER_TYPE
1641 && (TYPE_PTROB_P (type1)
1642 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1646 /* 9 For every type T, there exist candidate operator functions of the form
1649 10For every promoted arithmetic type T, there exist candidate operator
1650 functions of the form
1654 case CONVERT_EXPR: /* unary + */
1655 if (TREE_CODE (type1) == POINTER_TYPE
1656 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1659 if (ARITHMETIC_TYPE_P (type1))
1663 /* 11For every promoted integral type T, there exist candidate operator
1664 functions of the form
1668 if (INTEGRAL_TYPE_P (type1))
1672 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1673 is the same type as C2 or is a derived class of C2, T is a complete
1674 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1675 there exist candidate operator functions of the form
1676 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1677 where CV12 is the union of CV1 and CV2. */
1680 if (TREE_CODE (type1) == POINTER_TYPE
1681 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1683 tree c1 = TREE_TYPE (type1);
1684 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1685 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1686 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1688 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1689 && (TYPE_PTRMEMFUNC_P (type2)
1690 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1695 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1696 didate operator functions of the form
1701 bool operator<(L, R);
1702 bool operator>(L, R);
1703 bool operator<=(L, R);
1704 bool operator>=(L, R);
1705 bool operator==(L, R);
1706 bool operator!=(L, R);
1707 where LR is the result of the usual arithmetic conversions between
1710 14For every pair of types T and I, where T is a cv-qualified or cv-
1711 unqualified complete object type and I is a promoted integral type,
1712 there exist candidate operator functions of the form
1713 T* operator+(T*, I);
1714 T& operator[](T*, I);
1715 T* operator-(T*, I);
1716 T* operator+(I, T*);
1717 T& operator[](I, T*);
1719 15For every T, where T is a pointer to complete object type, there exist
1720 candidate operator functions of the form112)
1721 ptrdiff_t operator-(T, T);
1723 16For every pointer or enumeration type T, there exist candidate operator
1724 functions of the form
1725 bool operator<(T, T);
1726 bool operator>(T, T);
1727 bool operator<=(T, T);
1728 bool operator>=(T, T);
1729 bool operator==(T, T);
1730 bool operator!=(T, T);
1732 17For every pointer to member type T, there exist candidate operator
1733 functions of the form
1734 bool operator==(T, T);
1735 bool operator!=(T, T); */
1738 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1740 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1742 type2 = ptrdiff_type_node;
1746 case TRUNC_DIV_EXPR:
1747 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1753 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1754 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1756 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1757 && null_ptr_cst_p (args[1]))
1762 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1763 && null_ptr_cst_p (args[0]))
1775 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1777 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1779 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1781 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1786 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1794 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1797 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1799 type1 = ptrdiff_type_node;
1802 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1804 type2 = ptrdiff_type_node;
1809 /* 18For every pair of promoted integral types L and R, there exist candi-
1810 date operator functions of the form
1817 where LR is the result of the usual arithmetic conversions between
1820 case TRUNC_MOD_EXPR:
1826 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1830 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1831 type, VQ is either volatile or empty, and R is a promoted arithmetic
1832 type, there exist candidate operator functions of the form
1833 VQ L& operator=(VQ L&, R);
1834 VQ L& operator*=(VQ L&, R);
1835 VQ L& operator/=(VQ L&, R);
1836 VQ L& operator+=(VQ L&, R);
1837 VQ L& operator-=(VQ L&, R);
1839 20For every pair T, VQ), where T is any type and VQ is either volatile
1840 or empty, there exist candidate operator functions of the form
1841 T*VQ& operator=(T*VQ&, T*);
1843 21For every pair T, VQ), where T is a pointer to member type and VQ is
1844 either volatile or empty, there exist candidate operator functions of
1846 VQ T& operator=(VQ T&, T);
1848 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1849 unqualified complete object type, VQ is either volatile or empty, and
1850 I is a promoted integral type, there exist candidate operator func-
1852 T*VQ& operator+=(T*VQ&, I);
1853 T*VQ& operator-=(T*VQ&, I);
1855 23For every triple L, VQ, R), where L is an integral or enumeration
1856 type, VQ is either volatile or empty, and R is a promoted integral
1857 type, there exist candidate operator functions of the form
1859 VQ L& operator%=(VQ L&, R);
1860 VQ L& operator<<=(VQ L&, R);
1861 VQ L& operator>>=(VQ L&, R);
1862 VQ L& operator&=(VQ L&, R);
1863 VQ L& operator^=(VQ L&, R);
1864 VQ L& operator|=(VQ L&, R); */
1871 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1873 type2 = ptrdiff_type_node;
1877 case TRUNC_DIV_EXPR:
1878 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1882 case TRUNC_MOD_EXPR:
1888 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1893 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1895 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1896 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1897 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1898 || ((TYPE_PTRMEMFUNC_P (type1)
1899 || TREE_CODE (type1) == POINTER_TYPE)
1900 && null_ptr_cst_p (args[1])))
1908 my_friendly_abort (367);
1910 type1 = build_reference_type (type1);
1916 For every pair of promoted arithmetic types L and R, there
1917 exist candidate operator functions of the form
1919 LR operator?(bool, L, R);
1921 where LR is the result of the usual arithmetic conversions
1922 between types L and R.
1924 For every type T, where T is a pointer or pointer-to-member
1925 type, there exist candidate operator functions of the form T
1926 operator?(bool, T, T); */
1928 if (promoted_arithmetic_type_p (type1)
1929 && promoted_arithmetic_type_p (type2))
1933 /* Otherwise, the types should be pointers. */
1934 if (!(TREE_CODE (type1) == POINTER_TYPE
1935 || TYPE_PTRMEM_P (type1)
1936 || TYPE_PTRMEMFUNC_P (type1))
1937 || !(TREE_CODE (type2) == POINTER_TYPE
1938 || TYPE_PTRMEM_P (type2)
1939 || TYPE_PTRMEMFUNC_P (type2)))
1942 /* We don't check that the two types are the same; the logic
1943 below will actually create two candidates; one in which both
1944 parameter types are TYPE1, and one in which both parameter
1948 /* These arguments do not make for a legal overloaded operator. */
1952 my_friendly_abort (367);
1955 /* If we're dealing with two pointer types or two enumeral types,
1956 we need candidates for both of them. */
1957 if (type2 && !same_type_p (type1, type2)
1958 && TREE_CODE (type1) == TREE_CODE (type2)
1959 && (TREE_CODE (type1) == REFERENCE_TYPE
1960 || (TREE_CODE (type1) == POINTER_TYPE
1961 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1962 || TYPE_PTRMEMFUNC_P (type1)
1963 || IS_AGGR_TYPE (type1)
1964 || TREE_CODE (type1) == ENUMERAL_TYPE))
1966 candidates = build_builtin_candidate
1967 (candidates, fnname, type1, type1, args, argtypes, flags);
1968 return build_builtin_candidate
1969 (candidates, fnname, type2, type2, args, argtypes, flags);
1972 return build_builtin_candidate
1973 (candidates, fnname, type1, type2, args, argtypes, flags);
1977 type_decays_to (type)
1980 if (TREE_CODE (type) == ARRAY_TYPE)
1981 return build_pointer_type (TREE_TYPE (type));
1982 if (TREE_CODE (type) == FUNCTION_TYPE)
1983 return build_pointer_type (type);
1987 /* There are three conditions of builtin candidates:
1989 1) bool-taking candidates. These are the same regardless of the input.
1990 2) pointer-pair taking candidates. These are generated for each type
1991 one of the input types converts to.
1992 3) arithmetic candidates. According to the standard, we should generate
1993 all of these, but I'm trying not to...
1995 Here we generate a superset of the possible candidates for this particular
1996 case. That is a subset of the full set the standard defines, plus some
1997 other cases which the standard disallows. add_builtin_candidate will
1998 filter out the illegal set. */
2000 static struct z_candidate *
2001 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2002 struct z_candidate *candidates;
2003 enum tree_code code, code2;
2009 tree type, argtypes[3];
2010 /* TYPES[i] is the set of possible builtin-operator parameter types
2011 we will consider for the Ith argument. These are represented as
2012 a TREE_LIST; the TREE_VALUE of each node is the potential
2016 for (i = 0; i < 3; ++i)
2019 argtypes[i] = lvalue_type (args[i]);
2021 argtypes[i] = NULL_TREE;
2026 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2027 and VQ is either volatile or empty, there exist candidate operator
2028 functions of the form
2029 VQ T& operator++(VQ T&); */
2031 case POSTINCREMENT_EXPR:
2032 case PREINCREMENT_EXPR:
2033 case POSTDECREMENT_EXPR:
2034 case PREDECREMENT_EXPR:
2039 /* 24There also exist candidate operator functions of the form
2040 bool operator!(bool);
2041 bool operator&&(bool, bool);
2042 bool operator||(bool, bool); */
2044 case TRUTH_NOT_EXPR:
2045 return build_builtin_candidate
2046 (candidates, fnname, boolean_type_node,
2047 NULL_TREE, args, argtypes, flags);
2049 case TRUTH_ORIF_EXPR:
2050 case TRUTH_ANDIF_EXPR:
2051 return build_builtin_candidate
2052 (candidates, fnname, boolean_type_node,
2053 boolean_type_node, args, argtypes, flags);
2074 types[0] = types[1] = NULL_TREE;
2076 for (i = 0; i < 2; ++i)
2080 else if (IS_AGGR_TYPE (argtypes[i]))
2084 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2087 convs = lookup_conversions (argtypes[i]);
2089 if (code == COND_EXPR)
2091 if (real_lvalue_p (args[i]))
2092 types[i] = tree_cons
2093 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2095 types[i] = tree_cons
2096 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2102 for (; convs; convs = TREE_CHAIN (convs))
2104 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2107 && (TREE_CODE (type) != REFERENCE_TYPE
2108 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2111 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2112 types[i] = tree_cons (NULL_TREE, type, types[i]);
2114 type = non_reference (type);
2115 if (i != 0 || ! ref1)
2117 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2118 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2119 types[i] = tree_cons (NULL_TREE, type, types[i]);
2120 if (INTEGRAL_TYPE_P (type))
2121 type = type_promotes_to (type);
2124 if (! value_member (type, types[i]))
2125 types[i] = tree_cons (NULL_TREE, type, types[i]);
2130 if (code == COND_EXPR && real_lvalue_p (args[i]))
2131 types[i] = tree_cons
2132 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2133 type = non_reference (argtypes[i]);
2134 if (i != 0 || ! ref1)
2136 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2137 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2138 types[i] = tree_cons (NULL_TREE, type, types[i]);
2139 if (INTEGRAL_TYPE_P (type))
2140 type = type_promotes_to (type);
2142 types[i] = tree_cons (NULL_TREE, type, types[i]);
2146 /* Run through the possible parameter types of both arguments,
2147 creating candidates with those parameter types. */
2148 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2151 for (type = types[1]; type; type = TREE_CHAIN (type))
2152 candidates = add_builtin_candidate
2153 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2154 TREE_VALUE (type), args, argtypes, flags);
2156 candidates = add_builtin_candidate
2157 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2158 NULL_TREE, args, argtypes, flags);
2165 /* If TMPL can be successfully instantiated as indicated by
2166 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2168 TMPL is the template. EXPLICIT_TARGS are any explicit template
2169 arguments. ARGLIST is the arguments provided at the call-site.
2170 The RETURN_TYPE is the desired type for conversion operators. If
2171 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2172 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2173 add_conv_candidate. */
2175 static struct z_candidate*
2176 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2177 arglist, return_type, flags,
2179 struct z_candidate *candidates;
2180 tree tmpl, ctype, explicit_targs, arglist, return_type;
2183 unification_kind_t strict;
2185 int ntparms = DECL_NTPARMS (tmpl);
2186 tree targs = make_tree_vec (ntparms);
2187 tree args_without_in_chrg = arglist;
2188 struct z_candidate *cand;
2192 /* We don't do deduction on the in-charge parameter, the VTT
2193 parameter or 'this'. */
2194 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2195 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2197 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2198 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2199 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2200 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2202 i = fn_type_unification (tmpl, explicit_targs, targs,
2203 args_without_in_chrg,
2204 return_type, strict, -1);
2209 fn = instantiate_template (tmpl, targs);
2210 if (fn == error_mark_node)
2213 if (obj != NULL_TREE)
2214 /* Aha, this is a conversion function. */
2215 cand = add_conv_candidate (candidates, fn, obj, arglist);
2217 cand = add_function_candidate (candidates, fn, ctype,
2219 if (DECL_TI_TEMPLATE (fn) != tmpl)
2220 /* This situation can occur if a member template of a template
2221 class is specialized. Then, instantiate_template might return
2222 an instantiation of the specialization, in which case the
2223 DECL_TI_TEMPLATE field will point at the original
2224 specialization. For example:
2226 template <class T> struct S { template <class U> void f(U);
2227 template <> void f(int) {}; };
2231 Here, TMPL will be template <class U> S<double>::f(U).
2232 And, instantiate template will give us the specialization
2233 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2234 for this will point at template <class T> template <> S<T>::f(int),
2235 so that we can find the definition. For the purposes of
2236 overload resolution, however, we want the original TMPL. */
2237 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2239 cand->template = DECL_TEMPLATE_INFO (fn);
2245 static struct z_candidate *
2246 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2247 arglist, return_type, flags, strict)
2248 struct z_candidate *candidates;
2249 tree tmpl, ctype, explicit_targs, arglist, return_type;
2251 unification_kind_t strict;
2254 add_template_candidate_real (candidates, tmpl, ctype,
2255 explicit_targs, arglist, return_type, flags,
2260 static struct z_candidate *
2261 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2262 struct z_candidate *candidates;
2263 tree tmpl, obj, arglist, return_type;
2266 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2267 arglist, return_type, 0, obj, DEDUCE_CONV);
2273 struct z_candidate *cands;
2275 for (; cands; cands = cands->next)
2276 if (pedantic ? cands->viable == 1 : cands->viable)
2281 static struct z_candidate *
2282 splice_viable (cands)
2283 struct z_candidate *cands;
2285 struct z_candidate **p = &cands;
2289 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2302 /* Fix this to work on non-lvalues. */
2303 return build_unary_op (ADDR_EXPR, obj, 0);
2307 print_z_candidates (candidates)
2308 struct z_candidate *candidates;
2310 const char *str = "candidates are:";
2311 for (; candidates; candidates = candidates->next)
2313 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2315 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2316 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2317 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2318 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2319 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2320 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2321 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2322 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2323 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2325 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2326 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2328 else if (TYPE_P (candidates->fn))
2329 cp_error ("%s %T <conversion>", str, candidates->fn);
2331 cp_error_at ("%s %+#D%s", str, candidates->fn,
2332 candidates->viable == -1 ? " <near match>" : "");
2337 /* Returns the best overload candidate to perform the requested
2338 conversion. This function is used for three the overloading situations
2339 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2340 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2341 per [dcl.init.ref], so we ignore temporary bindings. */
2343 static struct z_candidate *
2344 build_user_type_conversion_1 (totype, expr, flags)
2348 struct z_candidate *candidates, *cand;
2349 tree fromtype = TREE_TYPE (expr);
2350 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2351 tree args = NULL_TREE;
2352 tree templates = NULL_TREE;
2354 /* We represent conversion within a hierarchy using RVALUE_CONV and
2355 BASE_CONV, as specified by [over.best.ics]; these become plain
2356 constructor calls, as specified in [dcl.init]. */
2357 if (IS_AGGR_TYPE (fromtype) && IS_AGGR_TYPE (totype)
2358 && DERIVED_FROM_P (totype, fromtype))
2361 if (IS_AGGR_TYPE (totype))
2362 ctors = lookup_fnfields (TYPE_BINFO (totype),
2363 complete_ctor_identifier,
2366 if (IS_AGGR_TYPE (fromtype))
2367 convs = lookup_conversions (fromtype);
2370 flags |= LOOKUP_NO_CONVERSION;
2376 ctors = TREE_VALUE (ctors);
2378 t = build_int_2 (0, 0);
2379 TREE_TYPE (t) = build_pointer_type (totype);
2380 args = build_tree_list (NULL_TREE, expr);
2381 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2382 || DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)))
2383 /* We should never try to call the abstract or base constructor
2386 args = tree_cons (NULL_TREE, t, args);
2388 for (; ctors; ctors = OVL_NEXT (ctors))
2390 tree ctor = OVL_CURRENT (ctors);
2391 if (DECL_NONCONVERTING_P (ctor))
2394 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2396 templates = tree_cons (NULL_TREE, ctor, templates);
2398 add_template_candidate (candidates, ctor, totype,
2399 NULL_TREE, args, NULL_TREE, flags,
2403 candidates = add_function_candidate (candidates, ctor, totype,
2408 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2409 candidates->basetype_path = TYPE_BINFO (totype);
2414 args = build_tree_list (NULL_TREE, build_this (expr));
2416 for (; convs; convs = TREE_CHAIN (convs))
2418 tree fns = TREE_VALUE (convs);
2419 int convflags = LOOKUP_NO_CONVERSION;
2422 /* If we are called to convert to a reference type, we are trying to
2423 find an lvalue binding, so don't even consider temporaries. If
2424 we don't find an lvalue binding, the caller will try again to
2425 look for a temporary binding. */
2426 if (TREE_CODE (totype) == REFERENCE_TYPE)
2427 convflags |= LOOKUP_NO_TEMP_BIND;
2429 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2430 ics = implicit_conversion
2431 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2433 /* We can't compute this yet. */
2434 ics = error_mark_node;
2436 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2437 /* ignore the near match. */;
2439 for (; fns; fns = OVL_NEXT (fns))
2441 tree fn = OVL_CURRENT (fns);
2442 struct z_candidate *old_candidates = candidates;
2444 /* [over.match.funcs] For conversion functions, the function is
2445 considered to be a member of the class of the implicit object
2446 argument for the purpose of defining the type of the implicit
2449 So we pass fromtype as CTYPE to add_*_candidate. */
2451 if (TREE_CODE (fn) == TEMPLATE_DECL)
2453 templates = tree_cons (NULL_TREE, fn, templates);
2455 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2456 args, totype, flags,
2460 candidates = add_function_candidate (candidates, fn, fromtype,
2463 if (candidates != old_candidates)
2465 if (TREE_CODE (fn) == TEMPLATE_DECL)
2466 ics = implicit_conversion
2467 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2470 candidates->second_conv = ics;
2471 candidates->basetype_path = TYPE_BINFO (fromtype);
2473 if (ics == NULL_TREE)
2474 candidates->viable = 0;
2475 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2476 candidates->viable = -1;
2481 if (! any_viable (candidates))
2484 if (flags & LOOKUP_COMPLAIN)
2486 if (candidates && ! candidates->next)
2487 /* say why this one won't work or try to be loose */;
2489 cp_error ("no viable candidates");
2496 candidates = splice_viable (candidates);
2497 cand = tourney (candidates);
2501 if (flags & LOOKUP_COMPLAIN)
2503 cp_error ("conversion from `%T' to `%T' is ambiguous",
2505 print_z_candidates (candidates);
2508 cand = candidates; /* any one will do */
2509 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2510 ICS_USER_FLAG (cand->second_conv) = 1;
2511 ICS_BAD_FLAG (cand->second_conv) = 1;
2516 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2517 p = &(TREE_OPERAND (*p, 0));
2521 (DECL_CONSTRUCTOR_P (cand->fn)
2522 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2523 expr, build_ptr_wrapper (cand));
2525 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2526 if (cand->viable == -1)
2527 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2533 build_user_type_conversion (totype, expr, flags)
2537 struct z_candidate *cand
2538 = build_user_type_conversion_1 (totype, expr, flags);
2542 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2543 return error_mark_node;
2544 return convert_from_reference (convert_like (cand->second_conv, expr));
2549 /* Do any initial processing on the arguments to a function call. */
2556 for (t = args; t; t = TREE_CHAIN (t))
2558 if (TREE_VALUE (t) == error_mark_node)
2559 return error_mark_node;
2560 else if (TREE_CODE (TREE_TYPE (TREE_VALUE (t))) == VOID_TYPE)
2562 error ("invalid use of void expression");
2563 return error_mark_node;
2565 else if (TREE_CODE (TREE_VALUE (t)) == OFFSET_REF)
2566 TREE_VALUE (t) = resolve_offset_ref (TREE_VALUE (t));
2572 build_new_function_call (fn, args)
2575 struct z_candidate *candidates = 0, *cand;
2576 tree explicit_targs = NULL_TREE;
2577 int template_only = 0;
2579 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2581 explicit_targs = TREE_OPERAND (fn, 1);
2582 fn = TREE_OPERAND (fn, 0);
2586 if (really_overloaded_fn (fn))
2589 tree templates = NULL_TREE;
2591 args = resolve_args (args);
2593 if (args == error_mark_node)
2594 return error_mark_node;
2596 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2598 tree t = OVL_FUNCTION (t1);
2600 if (TREE_CODE (t) == TEMPLATE_DECL)
2602 templates = tree_cons (NULL_TREE, t, templates);
2603 candidates = add_template_candidate
2604 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2605 LOOKUP_NORMAL, DEDUCE_CALL);
2607 else if (! template_only)
2608 candidates = add_function_candidate
2609 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2612 if (! any_viable (candidates))
2614 if (candidates && ! candidates->next)
2615 return build_function_call (candidates->fn, args);
2616 cp_error ("no matching function for call to `%D(%A)'",
2617 DECL_NAME (OVL_FUNCTION (fn)), args);
2619 print_z_candidates (candidates);
2620 return error_mark_node;
2622 candidates = splice_viable (candidates);
2623 cand = tourney (candidates);
2627 cp_error ("call of overloaded `%D(%A)' is ambiguous",
2628 DECL_NAME (OVL_FUNCTION (fn)), args);
2629 print_z_candidates (candidates);
2630 return error_mark_node;
2633 return build_over_call (cand, args, LOOKUP_NORMAL);
2636 /* This is not really overloaded. */
2637 fn = OVL_CURRENT (fn);
2639 return build_function_call (fn, args);
2643 build_object_call (obj, args)
2646 struct z_candidate *candidates = 0, *cand;
2647 tree fns, convs, mem_args = NULL_TREE;
2648 tree type = TREE_TYPE (obj);
2650 if (TYPE_PTRMEMFUNC_P (type))
2652 /* It's no good looking for an overloaded operator() on a
2653 pointer-to-member-function. */
2654 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2655 return error_mark_node;
2658 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2659 if (fns == error_mark_node)
2660 return error_mark_node;
2662 args = resolve_args (args);
2664 if (args == error_mark_node)
2665 return error_mark_node;
2669 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2670 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2672 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2674 tree fn = OVL_CURRENT (fns);
2675 if (TREE_CODE (fn) == TEMPLATE_DECL)
2678 = add_template_candidate (candidates, fn, base, NULL_TREE,
2679 mem_args, NULL_TREE,
2680 LOOKUP_NORMAL, DEDUCE_CALL);
2683 candidates = add_function_candidate
2684 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2687 candidates->basetype_path = TYPE_BINFO (type);
2691 convs = lookup_conversions (type);
2693 for (; convs; convs = TREE_CHAIN (convs))
2695 tree fns = TREE_VALUE (convs);
2696 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2698 if ((TREE_CODE (totype) == POINTER_TYPE
2699 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2700 || (TREE_CODE (totype) == REFERENCE_TYPE
2701 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2702 || (TREE_CODE (totype) == REFERENCE_TYPE
2703 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2704 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2705 for (; fns; fns = OVL_NEXT (fns))
2707 tree fn = OVL_CURRENT (fns);
2708 if (TREE_CODE (fn) == TEMPLATE_DECL)
2710 candidates = add_template_conv_candidate (candidates,
2717 candidates = add_conv_candidate (candidates, fn, obj, args);
2721 if (! any_viable (candidates))
2723 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2724 print_z_candidates (candidates);
2725 return error_mark_node;
2728 candidates = splice_viable (candidates);
2729 cand = tourney (candidates);
2733 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2734 print_z_candidates (candidates);
2735 return error_mark_node;
2738 /* Since cand->fn will be a type, not a function, for a conversion
2739 function, we must be careful not to unconditionally look at
2741 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2742 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2743 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2745 obj = convert_like_with_context
2746 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2749 return build_function_call (obj, args);
2753 op_error (code, code2, arg1, arg2, arg3, problem)
2754 enum tree_code code, code2;
2755 tree arg1, arg2, arg3;
2756 const char *problem;
2760 if (code == MODIFY_EXPR)
2761 opname = assignment_operator_name_info[code2].name;
2763 opname = operator_name_info[code].name;
2768 cp_error ("%s for `%T ? %T : %T' operator", problem,
2769 error_type (arg1), error_type (arg2), error_type (arg3));
2771 case POSTINCREMENT_EXPR:
2772 case POSTDECREMENT_EXPR:
2773 cp_error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2776 cp_error ("%s for `%T [%T]' operator", problem,
2777 error_type (arg1), error_type (arg2));
2781 cp_error ("%s for `%T %s %T' operator", problem,
2782 error_type (arg1), opname, error_type (arg2));
2784 cp_error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2788 /* Return the implicit conversion sequence that could be used to
2789 convert E1 to E2 in [expr.cond]. */
2792 conditional_conversion (e1, e2)
2796 tree t1 = non_reference (TREE_TYPE (e1));
2797 tree t2 = non_reference (TREE_TYPE (e2));
2802 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2803 implicitly converted (clause _conv_) to the type "reference to
2804 T2", subject to the constraint that in the conversion the
2805 reference must bind directly (_dcl.init.ref_) to E1. */
2806 if (real_lvalue_p (e2))
2808 conv = implicit_conversion (build_reference_type (t2),
2811 LOOKUP_NO_TEMP_BIND);
2818 If E1 and E2 have class type, and the underlying class types are
2819 the same or one is a base class of the other: E1 can be converted
2820 to match E2 if the class of T2 is the same type as, or a base
2821 class of, the class of T1, and the cv-qualification of T2 is the
2822 same cv-qualification as, or a greater cv-qualification than, the
2823 cv-qualification of T1. If the conversion is applied, E1 is
2824 changed to an rvalue of type T2 that still refers to the original
2825 source class object (or the appropriate subobject thereof). */
2826 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2827 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2828 TYPE_MAIN_VARIANT (t1)))
2830 if (at_least_as_qualified_p (t2, t1))
2832 conv = build1 (IDENTITY_CONV, t1, e1);
2833 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2834 TYPE_MAIN_VARIANT (t2)))
2835 conv = build_conv (BASE_CONV, t2, conv);
2844 E1 can be converted to match E2 if E1 can be implicitly converted
2845 to the type that expression E2 would have if E2 were converted to
2846 an rvalue (or the type it has, if E2 is an rvalue). */
2847 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2850 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2851 arguments to the conditional expression. By the time this function
2852 is called, any suitable candidate functions are included in
2856 build_conditional_expr (arg1, arg2, arg3)
2864 tree result_type = NULL_TREE;
2866 struct z_candidate *candidates = 0;
2867 struct z_candidate *cand;
2869 /* As a G++ extension, the second argument to the conditional can be
2870 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2871 c'.) If the second operand is omitted, make sure it is
2872 calculated only once. */
2876 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2877 arg1 = arg2 = save_expr (arg1);
2882 The first expr ession is implicitly converted to bool (clause
2884 arg1 = cp_convert (boolean_type_node, arg1);
2886 /* If something has already gone wrong, just pass that fact up the
2888 if (arg1 == error_mark_node
2889 || arg2 == error_mark_node
2890 || arg3 == error_mark_node
2891 || TREE_TYPE (arg1) == error_mark_node
2892 || TREE_TYPE (arg2) == error_mark_node
2893 || TREE_TYPE (arg3) == error_mark_node)
2894 return error_mark_node;
2898 If either the second or the third operand has type (possibly
2899 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2900 array-to-pointer (_conv.array_), and function-to-pointer
2901 (_conv.func_) standard conversions are performed on the second
2902 and third operands. */
2903 arg2_type = TREE_TYPE (arg2);
2904 arg3_type = TREE_TYPE (arg3);
2905 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2907 /* Do the conversions. We don't these for `void' type arguments
2908 since it can't have any effect and since decay_conversion
2909 does not handle that case gracefully. */
2910 if (!VOID_TYPE_P (arg2_type))
2911 arg2 = decay_conversion (arg2);
2912 if (!VOID_TYPE_P (arg3_type))
2913 arg3 = decay_conversion (arg3);
2914 arg2_type = TREE_TYPE (arg2);
2915 arg3_type = TREE_TYPE (arg3);
2919 One of the following shall hold:
2921 --The second or the third operand (but not both) is a
2922 throw-expression (_except.throw_); the result is of the
2923 type of the other and is an rvalue.
2925 --Both the second and the third operands have type void; the
2926 result is of type void and is an rvalue. */
2927 if ((TREE_CODE (arg2) == THROW_EXPR)
2928 ^ (TREE_CODE (arg3) == THROW_EXPR))
2929 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2930 ? arg3_type : arg2_type);
2931 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2932 result_type = void_type_node;
2935 cp_error ("`%E' has type `void' and is not a throw-expression",
2936 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2937 return error_mark_node;
2941 goto valid_operands;
2945 Otherwise, if the second and third operand have different types,
2946 and either has (possibly cv-qualified) class type, an attempt is
2947 made to convert each of those operands to the type of the other. */
2948 else if (!same_type_p (arg2_type, arg3_type)
2949 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2951 tree conv2 = conditional_conversion (arg2, arg3);
2952 tree conv3 = conditional_conversion (arg3, arg2);
2956 If both can be converted, or one can be converted but the
2957 conversion is ambiguous, the program is ill-formed. If
2958 neither can be converted, the operands are left unchanged and
2959 further checking is performed as described below. If exactly
2960 one conversion is possible, that conversion is applied to the
2961 chosen operand and the converted operand is used in place of
2962 the original operand for the remainder of this section. */
2963 if ((conv2 && !ICS_BAD_FLAG (conv2)
2964 && conv3 && !ICS_BAD_FLAG (conv3))
2965 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2966 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2968 cp_error ("operands to ?: have different types");
2969 return error_mark_node;
2971 else if (conv2 && !ICS_BAD_FLAG (conv2))
2973 arg2 = convert_like (conv2, arg2);
2974 arg2 = convert_from_reference (arg2);
2975 /* That may not quite have done the trick. If the two types
2976 are cv-qualified variants of one another, we will have
2977 just used an IDENTITY_CONV. (There's no conversion from
2978 an lvalue of one class type to an lvalue of another type,
2979 even a cv-qualified variant, and we don't want to lose
2980 lvalue-ness here.) So, we manually add a NOP_EXPR here
2982 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
2983 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
2984 arg2_type = TREE_TYPE (arg2);
2986 else if (conv3 && !ICS_BAD_FLAG (conv3))
2988 arg3 = convert_like (conv3, arg3);
2989 arg3 = convert_from_reference (arg3);
2990 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
2991 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
2992 arg3_type = TREE_TYPE (arg3);
2998 If the second and third operands are lvalues and have the same
2999 type, the result is of that type and is an lvalue. */
3000 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3001 same_type_p (arg2_type, arg3_type))
3003 result_type = arg2_type;
3004 goto valid_operands;
3009 Otherwise, the result is an rvalue. If the second and third
3010 operand do not have the same type, and either has (possibly
3011 cv-qualified) class type, overload resolution is used to
3012 determine the conversions (if any) to be applied to the operands
3013 (_over.match.oper_, _over.built_). */
3015 if (!same_type_p (arg2_type, arg3_type)
3016 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3021 /* Rearrange the arguments so that add_builtin_candidate only has
3022 to know about two args. In build_builtin_candidates, the
3023 arguments are unscrambled. */
3027 candidates = add_builtin_candidates (candidates,
3030 ansi_opname (COND_EXPR),
3036 If the overload resolution fails, the program is
3038 if (!any_viable (candidates))
3040 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3041 print_z_candidates (candidates);
3042 return error_mark_node;
3044 candidates = splice_viable (candidates);
3045 cand = tourney (candidates);
3048 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3049 print_z_candidates (candidates);
3050 return error_mark_node;
3055 Otherwise, the conversions thus determined are applied, and
3056 the converted operands are used in place of the original
3057 operands for the remainder of this section. */
3058 conv = TREE_VEC_ELT (cand->convs, 0);
3059 arg1 = convert_like (conv, arg1);
3060 conv = TREE_VEC_ELT (cand->convs, 1);
3061 arg2 = convert_like (conv, arg2);
3062 conv = TREE_VEC_ELT (cand->convs, 2);
3063 arg3 = convert_like (conv, arg3);
3068 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3069 and function-to-pointer (_conv.func_) standard conversions are
3070 performed on the second and third operands.
3072 We need to force the lvalue-to-rvalue conversion here for class types,
3073 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3074 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3077 We use ocp_convert rather than build_user_type_conversion because the
3078 latter returns NULL_TREE on failure, while the former gives an error. */
3080 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3081 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3082 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3084 arg2 = decay_conversion (arg2);
3085 arg2_type = TREE_TYPE (arg2);
3087 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3088 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3089 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3091 arg3 = decay_conversion (arg3);
3092 arg3_type = TREE_TYPE (arg3);
3096 After those conversions, one of the following shall hold:
3098 --The second and third operands have the same type; the result is of
3100 if (same_type_p (arg2_type, arg3_type))
3101 result_type = arg2_type;
3104 --The second and third operands have arithmetic or enumeration
3105 type; the usual arithmetic conversions are performed to bring
3106 them to a common type, and the result is of that type. */
3107 else if ((ARITHMETIC_TYPE_P (arg2_type)
3108 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3109 && (ARITHMETIC_TYPE_P (arg3_type)
3110 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3112 /* In this case, there is always a common type. */
3113 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3116 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3117 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3118 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3119 arg2_type, arg3_type);
3120 else if (extra_warnings
3121 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3122 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3123 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3124 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3125 cp_warning ("enumeral and non-enumeral type in conditional expression");
3127 arg2 = perform_implicit_conversion (result_type, arg2);
3128 arg3 = perform_implicit_conversion (result_type, arg3);
3132 --The second and third operands have pointer type, or one has
3133 pointer type and the other is a null pointer constant; pointer
3134 conversions (_conv.ptr_) and qualification conversions
3135 (_conv.qual_) are performed to bring them to their composite
3136 pointer type (_expr.rel_). The result is of the composite
3139 --The second and third operands have pointer to member type, or
3140 one has pointer to member type and the other is a null pointer
3141 constant; pointer to member conversions (_conv.mem_) and
3142 qualification conversions (_conv.qual_) are performed to bring
3143 them to a common type, whose cv-qualification shall match the
3144 cv-qualification of either the second or the third operand.
3145 The result is of the common type. */
3146 else if ((null_ptr_cst_p (arg2)
3147 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3148 || TYPE_PTRMEMFUNC_P (arg3_type)))
3149 || (null_ptr_cst_p (arg3)
3150 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3151 || TYPE_PTRMEMFUNC_P (arg2_type)))
3152 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3153 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3154 || (TYPE_PTRMEMFUNC_P (arg2_type)
3155 && TYPE_PTRMEMFUNC_P (arg3_type)))
3157 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3158 arg3, "conditional expression");
3159 arg2 = perform_implicit_conversion (result_type, arg2);
3160 arg3 = perform_implicit_conversion (result_type, arg3);
3165 cp_error ("operands to ?: have different types");
3166 return error_mark_node;
3170 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3171 /* Expand both sides into the same slot, hopefully the target of the
3172 ?: expression. We used to check for TARGET_EXPRs here, but now we
3173 sometimes wrap them in NOP_EXPRs so the test would fail. */
3174 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3175 result = build_target_expr_with_type (result, result_type);
3177 /* If this expression is an rvalue, but might be mistaken for an
3178 lvalue, we must add a NON_LVALUE_EXPR. */
3179 if (!lvalue_p && real_lvalue_p (result))
3180 result = build1 (NON_LVALUE_EXPR, result_type, result);
3186 build_new_op (code, flags, arg1, arg2, arg3)
3187 enum tree_code code;
3189 tree arg1, arg2, arg3;
3191 struct z_candidate *candidates = 0, *cand;
3192 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3193 enum tree_code code2 = NOP_EXPR;
3194 tree templates = NULL_TREE;
3197 if (arg1 == error_mark_node
3198 || arg2 == error_mark_node
3199 || arg3 == error_mark_node)
3200 return error_mark_node;
3202 /* This can happen if a template takes all non-type parameters, e.g.
3203 undeclared_template<1, 5, 72>a; */
3204 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3206 cp_error ("`%D' must be declared before use", arg1);
3207 return error_mark_node;
3210 if (code == MODIFY_EXPR)
3212 code2 = TREE_CODE (arg3);
3214 fnname = ansi_assopname (code2);
3217 fnname = ansi_opname (code);
3223 case VEC_DELETE_EXPR:
3225 /* Use build_op_new_call and build_op_delete_call instead. */
3226 my_friendly_abort (981018);
3229 return build_object_call (arg1, arg2);
3235 /* The comma operator can have void args. */
3236 if (TREE_CODE (arg1) == OFFSET_REF)
3237 arg1 = resolve_offset_ref (arg1);
3238 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3239 arg2 = resolve_offset_ref (arg2);
3240 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3241 arg3 = resolve_offset_ref (arg3);
3243 arg1 = convert_from_reference (arg1);
3245 arg2 = convert_from_reference (arg2);
3247 arg3 = convert_from_reference (arg3);
3249 if (code == COND_EXPR)
3251 if (arg2 == NULL_TREE
3252 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3253 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3254 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3255 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3258 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3259 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3262 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3263 arg2 = integer_zero_node;
3266 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3267 (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3269 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3271 arglist = build_tree_list (NULL_TREE, arg1);
3273 fns = lookup_function_nonclass (fnname, arglist);
3275 if (fns && TREE_CODE (fns) == TREE_LIST)
3276 fns = TREE_VALUE (fns);
3277 for (; fns; fns = OVL_NEXT (fns))
3279 tree fn = OVL_CURRENT (fns);
3280 if (TREE_CODE (fn) == TEMPLATE_DECL)
3282 templates = tree_cons (NULL_TREE, fn, templates);
3284 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3285 arglist, TREE_TYPE (fnname),
3286 flags, DEDUCE_CALL);
3289 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3293 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3295 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3296 if (fns == error_mark_node)
3304 tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3305 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3306 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3308 tree fn = OVL_CURRENT (fns);
3311 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3312 this_arglist = mem_arglist;
3314 this_arglist = arglist;
3316 if (TREE_CODE (fn) == TEMPLATE_DECL)
3318 /* A member template. */
3319 templates = tree_cons (NULL_TREE, fn, templates);
3321 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3322 this_arglist, TREE_TYPE (fnname),
3323 flags, DEDUCE_CALL);
3326 candidates = add_function_candidate
3327 (candidates, fn, basetype, this_arglist, flags);
3330 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3337 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3338 to know about two args; a builtin candidate will always have a first
3339 parameter of type bool. We'll handle that in
3340 build_builtin_candidate. */
3341 if (code == COND_EXPR)
3351 args[2] = NULL_TREE;
3354 candidates = add_builtin_candidates
3355 (candidates, code, code2, fnname, args, flags);
3358 if (! any_viable (candidates))
3362 case POSTINCREMENT_EXPR:
3363 case POSTDECREMENT_EXPR:
3364 /* Look for an `operator++ (int)'. If they didn't have
3365 one, then we fall back to the old way of doing things. */
3366 if (flags & LOOKUP_COMPLAIN)
3367 cp_pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3369 operator_name_info[code].name);
3370 if (code == POSTINCREMENT_EXPR)
3371 code = PREINCREMENT_EXPR;
3373 code = PREDECREMENT_EXPR;
3374 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3376 /* The caller will deal with these. */
3385 if (flags & LOOKUP_COMPLAIN)
3387 op_error (code, code2, arg1, arg2, arg3, "no match");
3388 print_z_candidates (candidates);
3390 return error_mark_node;
3392 candidates = splice_viable (candidates);
3393 cand = tourney (candidates);
3397 if (flags & LOOKUP_COMPLAIN)
3399 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3400 print_z_candidates (candidates);
3402 return error_mark_node;
3405 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3407 extern int warn_synth;
3409 && fnname == ansi_assopname (NOP_EXPR)
3410 && DECL_ARTIFICIAL (cand->fn)
3412 && ! candidates->next->next)
3414 cp_warning ("using synthesized `%#D' for copy assignment",
3416 cp_warning_at (" where cfront would use `%#D'",
3418 ? candidates->next->fn
3422 return build_over_call
3424 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3425 ? mem_arglist : arglist,
3429 /* Check for comparison of different enum types. */
3438 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3439 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3440 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3441 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3443 cp_warning ("comparison between `%#T' and `%#T'",
3444 TREE_TYPE (arg1), TREE_TYPE (arg2));
3451 /* We need to strip any leading REF_BIND so that bitfields don't cause
3452 errors. This should not remove any important conversions, because
3453 builtins don't apply to class objects directly. */
3454 conv = TREE_VEC_ELT (cand->convs, 0);
3455 if (TREE_CODE (conv) == REF_BIND)
3456 conv = TREE_OPERAND (conv, 0);
3457 arg1 = convert_like (conv, arg1);
3460 conv = TREE_VEC_ELT (cand->convs, 1);
3461 if (TREE_CODE (conv) == REF_BIND)
3462 conv = TREE_OPERAND (conv, 0);
3463 arg2 = convert_like (conv, arg2);
3467 conv = TREE_VEC_ELT (cand->convs, 2);
3468 if (TREE_CODE (conv) == REF_BIND)
3469 conv = TREE_OPERAND (conv, 0);
3470 arg3 = convert_like (conv, arg3);
3477 return build_modify_expr (arg1, code2, arg2);
3480 return build_indirect_ref (arg1, "unary *");
3485 case TRUNC_DIV_EXPR:
3496 case TRUNC_MOD_EXPR:
3500 case TRUTH_ANDIF_EXPR:
3501 case TRUTH_ORIF_EXPR:
3502 return cp_build_binary_op (code, arg1, arg2);
3507 case TRUTH_NOT_EXPR:
3508 case PREINCREMENT_EXPR:
3509 case POSTINCREMENT_EXPR:
3510 case PREDECREMENT_EXPR:
3511 case POSTDECREMENT_EXPR:
3514 return build_unary_op (code, arg1, candidates != 0);
3517 return build_array_ref (arg1, arg2);
3520 return build_conditional_expr (arg1, arg2, arg3);
3523 return build_m_component_ref
3524 (build_indirect_ref (arg1, NULL), arg2);
3526 /* The caller will deal with these. */
3533 my_friendly_abort (367);
3538 /* Build a call to operator delete. This has to be handled very specially,
3539 because the restrictions on what signatures match are different from all
3540 other call instances. For a normal delete, only a delete taking (void *)
3541 or (void *, size_t) is accepted. For a placement delete, only an exact
3542 match with the placement new is accepted.
3544 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3545 ADDR is the pointer to be deleted. For placement delete, it is also
3546 used to determine what the corresponding new looked like.
3547 SIZE is the size of the memory block to be deleted.
3548 FLAGS are the usual overloading flags.
3549 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3552 build_op_delete_call (code, addr, size, flags, placement)
3553 enum tree_code code;
3554 tree addr, size, placement;
3557 tree fn, fns, fnname, fntype, argtypes, args, type;
3560 if (addr == error_mark_node)
3561 return error_mark_node;
3563 type = TREE_TYPE (TREE_TYPE (addr));
3564 while (TREE_CODE (type) == ARRAY_TYPE)
3565 type = TREE_TYPE (type);
3567 fnname = ansi_opname (code);
3569 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3572 If the result of the lookup is ambiguous or inaccessible, or if
3573 the lookup selects a placement deallocation function, the
3574 program is ill-formed.
3576 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3578 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3579 if (fns == error_mark_node)
3580 return error_mark_node;
3585 if (fns == NULL_TREE)
3586 fns = lookup_name_nonclass (fnname);
3590 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3592 /* Extract the function. */
3593 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3594 /* Then the second parm type. */
3595 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3597 /* Also the second argument. */
3598 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3602 /* First try it without the size argument. */
3603 argtypes = void_list_node;
3607 /* Strip const and volatile from addr. */
3608 addr = cp_convert (ptr_type_node, addr);
3610 /* We make two tries at finding a matching `operator delete'. On
3611 the first pass, we look for an one-operator (or placement)
3612 operator delete. If we're not doing placement delete, then on
3613 the second pass we look for a two-argument delete. */
3614 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3617 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3619 /* Normal delete; now try to find a match including the size
3621 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3622 tree_cons (NULL_TREE, sizetype,
3624 fntype = build_function_type (void_type_node, argtypes);
3626 /* Go through the `operator delete' functions looking for one
3627 with a matching type. */
3628 for (fn = BASELINK_P (fns) ? TREE_VALUE (fns) : fns;
3634 /* Exception specifications on the `delete' operator do not
3636 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3638 /* We also don't compare attributes. We're really just
3639 trying to check the types of the first two parameters. */
3640 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3644 /* If we found a match, we're done. */
3649 /* If we have a matching function, call it. */
3652 /* Make sure we have the actual function, and not an
3654 fn = OVL_CURRENT (fn);
3656 /* If the FN is a member function, make sure that it is
3658 if (DECL_CLASS_SCOPE_P (fn))
3659 enforce_access (type, fn);
3662 args = tree_cons (NULL_TREE, addr, args);
3664 args = tree_cons (NULL_TREE, addr,
3665 build_tree_list (NULL_TREE, size));
3667 return build_function_call (fn, args);
3670 /* If we are doing placement delete we do nothing if we don't find a
3671 matching op delete. */
3675 cp_error ("no suitable `operator delete' for `%T'", type);
3676 return error_mark_node;
3679 /* If the current scope isn't allowed to access DECL along
3680 BASETYPE_PATH, give an error. The most derived class in
3681 BASETYPE_PATH is the one used to qualify DECL. */
3684 enforce_access (basetype_path, decl)
3690 accessible = accessible_p (basetype_path, decl);
3693 if (TREE_PRIVATE (decl))
3694 cp_error_at ("`%+#D' is private", decl);
3695 else if (TREE_PROTECTED (decl))
3696 cp_error_at ("`%+#D' is protected", decl);
3698 cp_error_at ("`%+#D' is inaccessible", decl);
3699 cp_error ("within this context");
3706 /* Perform the conversions in CONVS on the expression EXPR.
3707 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3708 indicates the `this' argument of a method. INNER is non-zero when
3709 being called to continue a conversion chain. It is negative when a
3710 reference binding will be applied, positive otherwise. */
3713 convert_like_real (convs, expr, fn, argnum, inner)
3719 extern int warningcount, errorcount;
3722 tree totype = TREE_TYPE (convs);
3724 if (ICS_BAD_FLAG (convs)
3725 && TREE_CODE (convs) != USER_CONV
3726 && TREE_CODE (convs) != AMBIG_CONV
3727 && TREE_CODE (convs) != REF_BIND)
3730 for (; t; t = TREE_OPERAND (t, 0))
3732 if (TREE_CODE (t) == USER_CONV)
3734 expr = convert_like_real (t, expr, fn, argnum, 1);
3737 else if (TREE_CODE (t) == AMBIG_CONV)
3738 return convert_like_real (t, expr, fn, argnum, 1);
3739 else if (TREE_CODE (t) == IDENTITY_CONV)
3742 return convert_for_initialization
3743 (NULL_TREE, totype, expr, LOOKUP_NORMAL,
3744 "conversion", fn, argnum);
3748 expr = dubious_conversion_warnings
3749 (totype, expr, "argument", fn, argnum);
3750 switch (TREE_CODE (convs))
3754 struct z_candidate *cand
3755 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3756 tree convfn = cand->fn;
3759 if (DECL_CONSTRUCTOR_P (convfn))
3761 tree t = build_int_2 (0, 0);
3762 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3764 args = build_tree_list (NULL_TREE, expr);
3765 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3766 || DECL_HAS_VTT_PARM_P (convfn))
3767 /* We should never try to call the abstract or base constructor
3770 args = tree_cons (NULL_TREE, t, args);
3773 args = build_this (expr);
3774 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3776 /* If this is a constructor or a function returning an aggr type,
3777 we need to build up a TARGET_EXPR. */
3778 if (DECL_CONSTRUCTOR_P (convfn))
3779 expr = build_cplus_new (totype, expr);
3781 /* The result of the call is then used to direct-initialize the object
3782 that is the destination of the copy-initialization. [dcl.init]
3784 Note that this step is not reflected in the conversion sequence;
3785 it affects the semantics when we actually perform the
3786 conversion, but is not considered during overload resolution.
3788 If the target is a class, that means call a ctor. */
3789 if (IS_AGGR_TYPE (totype)
3790 && (inner >= 0 || !real_lvalue_p (expr)))
3792 savew = warningcount, savee = errorcount;
3793 expr = build_new_method_call
3794 (NULL_TREE, complete_ctor_identifier,
3795 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3796 /* Core issue 84, now a DR, says that we don't allow UDCs
3797 for these args (which deliberately breaks copy-init of an
3798 auto_ptr<Base> from an auto_ptr<Derived>). */
3799 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3801 /* Tell the user where this failing constructor call came from. */
3804 if (warningcount > savew)
3806 (" initializing argument %P of `%D' from result of `%D'",
3807 argnum, fn, convfn);
3808 else if (errorcount > savee)
3810 (" initializing argument %P of `%D' from result of `%D'",
3811 argnum, fn, convfn);
3815 if (warningcount > savew)
3816 cp_warning (" initializing temporary from result of `%D'",
3818 else if (errorcount > savee)
3819 cp_error (" initializing temporary from result of `%D'",
3822 expr = build_cplus_new (totype, expr);
3827 if (type_unknown_p (expr))
3828 expr = instantiate_type (totype, expr, itf_complain);
3831 /* Call build_user_type_conversion again for the error. */
3832 return build_user_type_conversion
3833 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3839 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3840 TREE_CODE (convs) == REF_BIND ? -1 : 1);
3841 if (expr == error_mark_node)
3842 return error_mark_node;
3844 /* Convert a non-array constant variable to its underlying value, unless we
3845 are about to bind it to a reference, in which case we need to
3846 leave it as an lvalue. */
3847 if (TREE_CODE (convs) != REF_BIND
3848 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3849 expr = decl_constant_value (expr);
3851 switch (TREE_CODE (convs))
3854 if (! IS_AGGR_TYPE (totype))
3856 /* else fall through */
3858 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3860 /* We are going to bind a reference directly to a base-class
3861 subobject of EXPR. */
3862 tree base_ptr = build_pointer_type (totype);
3864 /* Build an expression for `*((base*) &expr)'. */
3865 expr = build_unary_op (ADDR_EXPR, expr, 0);
3866 expr = perform_implicit_conversion (base_ptr, expr);
3867 expr = build_indirect_ref (expr, "implicit conversion");
3871 /* Copy-initialization where the cv-unqualified version of the source
3872 type is the same class as, or a derived class of, the class of the
3873 destination [is treated as direct-initialization]. [dcl.init] */
3875 savew = warningcount, savee = errorcount;
3876 expr = build_new_method_call (NULL_TREE, complete_ctor_identifier,
3877 build_tree_list (NULL_TREE, expr),
3878 TYPE_BINFO (totype),
3879 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
3882 if (warningcount > savew)
3883 cp_warning (" initializing argument %P of `%D'", argnum, fn);
3884 else if (errorcount > savee)
3885 cp_error (" initializing argument %P of `%D'", argnum, fn);
3887 return build_cplus_new (totype, expr);
3891 tree ref_type = totype;
3893 /* If necessary, create a temporary. */
3894 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
3896 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3897 expr = build_target_expr_with_type (expr, type);
3900 /* Take the address of the thing to which we will bind the
3902 expr = build_unary_op (ADDR_EXPR, expr, 1);
3903 if (expr == error_mark_node)
3904 return error_mark_node;
3906 /* Convert it to a pointer to the type referred to by the
3907 reference. This will adjust the pointer if a derived to
3908 base conversion is being performed. */
3909 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3911 /* Convert the pointer to the desired reference type. */
3912 expr = build1 (NOP_EXPR, ref_type, expr);
3918 return decay_conversion (expr);
3921 /* Warn about deprecated conversion if appropriate. */
3922 string_conv_p (totype, expr, 1);
3928 return ocp_convert (totype, expr, CONV_IMPLICIT,
3929 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3932 /* ARG is being passed to a varargs function. Perform any conversions
3933 required. Array/function to pointer decay must have already happened.
3934 Return the converted value. */
3937 convert_arg_to_ellipsis (arg)
3940 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3941 && (TYPE_PRECISION (TREE_TYPE (arg))
3942 < TYPE_PRECISION (double_type_node)))
3943 /* Convert `float' to `double'. */
3944 arg = cp_convert (double_type_node, arg);
3946 /* Convert `short' and `char' to full-size `int'. */
3947 arg = default_conversion (arg);
3949 arg = require_complete_type (arg);
3951 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3953 /* Undefined behaviour [expr.call] 5.2.2/7. */
3954 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3961 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3964 build_x_va_arg (expr, type)
3968 if (processing_template_decl)
3969 return build_min (VA_ARG_EXPR, type, expr);
3971 type = complete_type_or_else (type, NULL_TREE);
3973 if (expr == error_mark_node || !type)
3974 return error_mark_node;
3976 if (! pod_type_p (type))
3978 /* Undefined behaviour [expr.call] 5.2.2/7. */
3979 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
3983 return build_va_arg (expr, type);
3986 /* TYPE has been given to va_arg. Apply the default conversions which would
3987 have happened when passed via ellipsis. Return the promoted type, or
3988 NULL_TREE, if there is no change. */
3991 convert_type_from_ellipsis (type)
3996 if (TREE_CODE (type) == ARRAY_TYPE)
3997 promote = build_pointer_type (TREE_TYPE (type));
3998 else if (TREE_CODE (type) == FUNCTION_TYPE)
3999 promote = build_pointer_type (type);
4001 promote = type_promotes_to (type);
4003 return same_type_p (type, promote) ? NULL_TREE : promote;
4006 /* ARG is a default argument expression being passed to a parameter of
4007 the indicated TYPE, which is a parameter to FN. Do any required
4008 conversions. Return the converted value. */
4011 convert_default_arg (type, arg, fn, parmnum)
4017 if (TREE_CODE (arg) == DEFAULT_ARG)
4019 /* When processing the default args for a class, we can find that
4020 there is an ordering constraint, and we call a function who's
4021 default args have not yet been converted. For instance,
4024 void Foo (A const & = A ());
4026 We must process A::A before A::Foo's default arg can be converted.
4027 Remember the dependent function, so do_pending_defargs can retry,
4029 unprocessed_defarg_fn (fn);
4031 /* Don't return error_mark node, as we won't be able to distinguish
4032 genuine errors from this case, and that would lead to repeated
4033 diagnostics. Just make something of the right type. */
4034 return build1 (NOP_EXPR, type, integer_zero_node);
4037 if (fn && DECL_TEMPLATE_INFO (fn))
4038 arg = tsubst_default_argument (fn, type, arg);
4040 arg = break_out_target_exprs (arg);
4042 if (TREE_CODE (arg) == CONSTRUCTOR)
4044 arg = digest_init (type, arg, 0);
4045 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4046 "default argument", fn, parmnum);
4050 /* This could get clobbered by the following call. */
4051 if (TREE_HAS_CONSTRUCTOR (arg))
4052 arg = copy_node (arg);
4054 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4055 "default argument", fn, parmnum);
4056 if (PROMOTE_PROTOTYPES
4057 && INTEGRAL_TYPE_P (type)
4058 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4059 arg = default_conversion (arg);
4065 /* Subroutine of the various build_*_call functions. Overload resolution
4066 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4067 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4068 bitmask of various LOOKUP_* flags which apply to the call itself. */
4071 build_over_call (cand, args, flags)
4072 struct z_candidate *cand;
4077 tree convs = cand->convs;
4078 tree converted_args = NULL_TREE;
4079 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4080 tree conv, arg, val;
4084 /* Give any warnings we noticed during overload resolution. */
4086 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4087 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
4089 if (DECL_FUNCTION_MEMBER_P (fn))
4090 enforce_access (cand->basetype_path, fn);
4092 if (args && TREE_CODE (args) != TREE_LIST)
4093 args = build_tree_list (NULL_TREE, args);
4096 /* The implicit parameters to a constructor are not considered by overload
4097 resolution, and must be of the proper type. */
4098 if (DECL_CONSTRUCTOR_P (fn))
4100 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4101 arg = TREE_CHAIN (arg);
4102 parm = TREE_CHAIN (parm);
4103 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4104 /* We should never try to call the abstract constructor. */
4106 if (DECL_HAS_VTT_PARM_P (fn))
4108 converted_args = tree_cons
4109 (NULL_TREE, TREE_VALUE (arg), converted_args);
4110 arg = TREE_CHAIN (arg);
4111 parm = TREE_CHAIN (parm);
4114 /* Bypass access control for 'this' parameter. */
4115 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4117 tree parmtype = TREE_VALUE (parm);
4118 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4120 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4121 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4122 TREE_TYPE (argtype), fn);
4124 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4125 X is called for an object that is not of type X, or of a type
4126 derived from X, the behavior is undefined.
4128 So we can assume that anything passed as 'this' is non-null, and
4129 optimize accordingly. */
4130 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4131 t = convert_pointer_to_real (TREE_TYPE (parmtype), TREE_VALUE (arg));
4132 converted_args = tree_cons (NULL_TREE, t, converted_args);
4133 parm = TREE_CHAIN (parm);
4134 arg = TREE_CHAIN (arg);
4140 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4142 tree type = TREE_VALUE (parm);
4144 conv = TREE_VEC_ELT (convs, i);
4145 if (ICS_BAD_FLAG (conv))
4148 val = TREE_VALUE (arg);
4150 for (; t; t = TREE_OPERAND (t, 0))
4152 if (TREE_CODE (t) == USER_CONV
4153 || TREE_CODE (t) == AMBIG_CONV)
4155 val = convert_like_with_context (t, val, fn, i - is_method);
4158 else if (TREE_CODE (t) == IDENTITY_CONV)
4161 val = convert_for_initialization
4162 (NULL_TREE, type, val, LOOKUP_NORMAL,
4163 "argument", fn, i - is_method);
4167 val = TREE_VALUE (arg);
4168 val = convert_like_with_context
4169 (conv, TREE_VALUE (arg), fn, i - is_method);
4172 if (PROMOTE_PROTOTYPES
4173 && INTEGRAL_TYPE_P (type)
4174 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4175 val = default_conversion (val);
4176 converted_args = tree_cons (NULL_TREE, val, converted_args);
4179 /* Default arguments */
4180 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4182 = tree_cons (NULL_TREE,
4183 convert_default_arg (TREE_VALUE (parm),
4184 TREE_PURPOSE (parm),
4189 for (; arg; arg = TREE_CHAIN (arg))
4191 = tree_cons (NULL_TREE,
4192 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4195 converted_args = nreverse (converted_args);
4197 if (warn_format && (DECL_NAME (fn) || DECL_ASSEMBLER_NAME (fn)))
4198 check_function_format (NULL, DECL_NAME (fn), DECL_ASSEMBLER_NAME (fn),
4201 /* Avoid actually calling copy constructors and copy assignment operators,
4204 if (! flag_elide_constructors)
4205 /* Do things the hard way. */;
4206 else if (TREE_VEC_LENGTH (convs) == 1
4207 && DECL_COPY_CONSTRUCTOR_P (fn))
4210 arg = skip_artificial_parms_for (fn, converted_args);
4211 arg = TREE_VALUE (arg);
4213 /* Pull out the real argument, disregarding const-correctness. */
4215 while (TREE_CODE (targ) == NOP_EXPR
4216 || TREE_CODE (targ) == NON_LVALUE_EXPR
4217 || TREE_CODE (targ) == CONVERT_EXPR)
4218 targ = TREE_OPERAND (targ, 0);
4219 if (TREE_CODE (targ) == ADDR_EXPR)
4221 targ = TREE_OPERAND (targ, 0);
4222 if (!same_type_ignoring_top_level_qualifiers_p
4223 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4232 arg = build_indirect_ref (arg, 0);
4234 /* [class.copy]: the copy constructor is implicitly defined even if
4235 the implementation elided its use. */
4236 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4239 /* If we're creating a temp and we already have one, don't create a
4240 new one. If we're not creating a temp but we get one, use
4241 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4242 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4243 temp or an INIT_EXPR otherwise. */
4244 if (integer_zerop (TREE_VALUE (args)))
4246 if (! real_lvalue_p (arg))
4248 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4249 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4251 else if (! real_lvalue_p (arg)
4252 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4255 tree to = stabilize_reference
4256 (build_indirect_ref (TREE_VALUE (args), 0));
4258 /* If we're initializing an empty class, then we actually
4259 have to use a MODIFY_EXPR rather than an INIT_EXPR. The
4260 reason is that the dummy padding member in the target may
4261 not actually be allocated if TO is a base class
4262 subobject. Since we've set TYPE_NONCOPIED_PARTS on the
4263 padding, a MODIFY_EXPR will preserve its value, which is
4264 the right thing to do if it's not really padding at all.
4266 It's not safe to just throw away the ARG if we're looking
4267 at an empty class because the ARG might contain a
4268 TARGET_EXPR which wants to be bound to TO. If it is not,
4269 expand_expr will assign a dummy slot for the TARGET_EXPR,
4270 and we will call a destructor for it, which is wrong,
4271 because we will also destroy TO, but will never have
4273 val = build (is_empty_class (DECL_CONTEXT (fn))
4274 ? MODIFY_EXPR : INIT_EXPR,
4275 DECL_CONTEXT (fn), to, arg);
4276 address = build_unary_op (ADDR_EXPR, val, 0);
4277 /* Avoid a warning about this expression, if the address is
4279 TREE_USED (address) = 1;
4283 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4285 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4287 tree to = stabilize_reference
4288 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4290 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4292 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4298 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4300 tree t, *p = &TREE_VALUE (converted_args);
4301 tree binfo = get_binfo
4302 (DECL_VIRTUAL_CONTEXT (fn), TREE_TYPE (TREE_TYPE (*p)), 0);
4303 *p = convert_pointer_to_real (binfo, *p);
4304 if (TREE_SIDE_EFFECTS (*p))
4305 *p = save_expr (*p);
4306 t = build_pointer_type (TREE_TYPE (fn));
4307 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4308 fn = build_java_interface_fn_ref (fn, *p);
4310 fn = build_vfn_ref (p, build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4313 else if (DECL_INLINE (fn))
4314 fn = inline_conversion (fn);
4316 fn = build_addr_func (fn);
4318 /* Recognize certain built-in functions so we can make tree-codes
4319 other than CALL_EXPR. We do this when it enables fold-const.c
4320 to do something useful. */
4322 if (TREE_CODE (fn) == ADDR_EXPR
4323 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4324 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4327 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4332 /* Some built-in function calls will be evaluated at
4333 compile-time in fold (). */
4334 fn = fold (build_call (fn, converted_args));
4335 if (VOID_TYPE_P (TREE_TYPE (fn)))
4337 fn = require_complete_type (fn);
4338 if (fn == error_mark_node)
4339 return error_mark_node;
4340 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4341 fn = build_cplus_new (TREE_TYPE (fn), fn);
4342 return convert_from_reference (fn);
4345 static tree java_iface_lookup_fn;
4347 /* Make an expression which yields the address of the Java interface
4348 method FN. This is achieved by generating a call to libjava's
4349 _Jv_LookupInterfaceMethodIdx(). */
4352 build_java_interface_fn_ref (fn, instance)
4355 tree lookup_args, lookup_fn, method, idx;
4356 tree klass_ref, iface, iface_ref;
4359 if (!java_iface_lookup_fn)
4361 tree endlink = build_void_list_node ();
4362 tree t = tree_cons (NULL_TREE, ptr_type_node,
4363 tree_cons (NULL_TREE, ptr_type_node,
4364 tree_cons (NULL_TREE, java_int_type_node,
4366 java_iface_lookup_fn
4367 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4368 build_function_type (ptr_type_node, t),
4369 0, NOT_BUILT_IN, NULL);
4370 ggc_add_tree_root (&java_iface_lookup_fn, 1);
4373 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4374 This is the first entry in the vtable. */
4375 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4378 /* Get the java.lang.Class pointer for the interface being called. */
4379 iface = DECL_CONTEXT (fn);
4380 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4381 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4382 || DECL_CONTEXT (iface_ref) != iface)
4384 cp_error ("Could not find class$ field in java interface type `%T'",
4386 return error_mark_node;
4388 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4390 /* Determine the itable index of FN. */
4392 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4394 if (!DECL_VIRTUAL_P (method))
4400 idx = build_int_2 (i, 0);
4402 lookup_args = tree_cons (NULL_TREE, klass_ref,
4403 tree_cons (NULL_TREE, iface_ref,
4404 build_tree_list (NULL_TREE, idx)));
4405 lookup_fn = build1 (ADDR_EXPR,
4406 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4407 java_iface_lookup_fn);
4408 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4411 /* Returns the value to use for the in-charge parameter when making a
4412 call to a function with the indicated NAME. */
4415 in_charge_arg_for_name (name)
4418 if (name == base_ctor_identifier
4419 || name == base_dtor_identifier)
4420 return integer_zero_node;
4421 else if (name == complete_ctor_identifier)
4422 return integer_one_node;
4423 else if (name == complete_dtor_identifier)
4424 return integer_two_node;
4425 else if (name == deleting_dtor_identifier)
4426 return integer_three_node;
4428 /* This function should only be called with one of the names listed
4430 my_friendly_abort (20000411);
4435 build_new_method_call (instance, name, args, basetype_path, flags)
4436 tree instance, name, args, basetype_path;
4439 struct z_candidate *candidates = 0, *cand;
4440 tree explicit_targs = NULL_TREE;
4441 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4444 tree templates = NULL_TREE;
4446 int template_only = 0;
4448 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4450 explicit_targs = TREE_OPERAND (name, 1);
4451 name = TREE_OPERAND (name, 0);
4453 name = DECL_NAME (name);
4456 if (TREE_CODE (name) == COMPONENT_REF)
4457 name = TREE_OPERAND (name, 1);
4458 if (TREE_CODE (name) == OVERLOAD)
4459 name = DECL_NAME (OVL_CURRENT (name));
4466 args = resolve_args (args);
4468 if (args == error_mark_node)
4469 return error_mark_node;
4471 if (instance == NULL_TREE)
4472 basetype = BINFO_TYPE (basetype_path);
4475 if (TREE_CODE (instance) == OFFSET_REF)
4476 instance = resolve_offset_ref (instance);
4477 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4478 instance = convert_from_reference (instance);
4479 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4481 /* XXX this should be handled before we get here. */
4482 if (! IS_AGGR_TYPE (basetype))
4484 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4485 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4486 name, instance, basetype);
4488 return error_mark_node;
4492 if (basetype_path == NULL_TREE)
4493 basetype_path = TYPE_BINFO (basetype);
4497 instance_ptr = build_this (instance);
4499 if (! template_only)
4501 /* XXX this should be handled before we get here. */
4502 fns = build_field_call (basetype_path, instance_ptr, name, args);
4509 instance_ptr = build_int_2 (0, 0);
4510 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4513 /* Callers should explicitly indicate whether they want to construct
4514 the complete object or just the part without virtual bases. */
4515 my_friendly_assert (name != ctor_identifier, 20000408);
4516 /* Similarly for destructors. */
4517 my_friendly_assert (name != dtor_identifier, 20000408);
4519 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4523 constructor_p = (name == complete_ctor_identifier
4524 || name == base_ctor_identifier);
4525 pretty_name = (constructor_p
4526 ? constructor_name (basetype) : dtor_identifier);
4528 /* If we're a call to a constructor or destructor for a
4529 subobject that uses virtual base classes, then we need to
4530 pass down a pointer to a VTT for the subobject. */
4531 if ((name == base_ctor_identifier
4532 || name == base_dtor_identifier)
4533 && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4537 tree basebinfo = basetype_path;
4539 /* If the current function is a complete object constructor
4540 or destructor, then we fetch the VTT directly.
4541 Otherwise, we look it up using the VTT we were given. */
4542 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4543 vtt = decay_conversion (vtt);
4544 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4545 build (EQ_EXPR, boolean_type_node,
4546 current_in_charge_parm, integer_zero_node),
4549 if (TREE_VIA_VIRTUAL (basebinfo))
4550 basebinfo = binfo_for_vbase (basetype, current_class_type);
4551 my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4552 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4553 BINFO_SUBVTT_INDEX (basebinfo));
4555 args = tree_cons (NULL_TREE, sub_vtt, args);
4561 fns = lookup_fnfields (basetype_path, name, 1);
4563 if (fns == error_mark_node)
4564 return error_mark_node;
4567 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4568 tree fn = TREE_VALUE (fns);
4569 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4570 for (; fn; fn = OVL_NEXT (fn))
4572 tree t = OVL_CURRENT (fn);
4575 /* We can end up here for copy-init of same or base class. */
4576 if ((flags & LOOKUP_ONLYCONVERTING)
4577 && DECL_NONCONVERTING_P (t))
4580 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4581 this_arglist = mem_args;
4583 this_arglist = args;
4585 if (TREE_CODE (t) == TEMPLATE_DECL)
4587 /* A member template. */
4588 templates = tree_cons (NULL_TREE, t, templates);
4590 add_template_candidate (candidates, t, base, explicit_targs,
4592 TREE_TYPE (name), flags, DEDUCE_CALL);
4594 else if (! template_only)
4595 candidates = add_function_candidate (candidates, t, base,
4596 this_arglist, flags);
4599 candidates->basetype_path = basetype_path;
4603 if (! any_viable (candidates))
4605 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4606 if (flags & LOOKUP_SPECULATIVELY)
4608 if (!COMPLETE_TYPE_P (basetype))
4609 incomplete_type_error (instance_ptr, basetype);
4611 cp_error ("no matching function for call to `%T::%D(%A)%V'",
4612 basetype, pretty_name, user_args,
4613 TREE_TYPE (TREE_TYPE (instance_ptr)));
4614 print_z_candidates (candidates);
4615 return error_mark_node;
4617 candidates = splice_viable (candidates);
4618 cand = tourney (candidates);
4622 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4624 print_z_candidates (candidates);
4625 return error_mark_node;
4628 if (DECL_PURE_VIRTUAL_P (cand->fn)
4629 && instance == current_class_ref
4630 && (DECL_CONSTRUCTOR_P (current_function_decl)
4631 || DECL_DESTRUCTOR_P (current_function_decl))
4632 && ! (flags & LOOKUP_NONVIRTUAL)
4633 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4634 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4635 "abstract virtual `%#D' called from constructor"
4636 : "abstract virtual `%#D' called from destructor"),
4638 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4639 && is_dummy_object (instance_ptr))
4641 cp_error ("cannot call member function `%D' without object", cand->fn);
4642 return error_mark_node;
4645 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4646 && ((instance == current_class_ref && (dtor_label || ctor_label))
4647 || resolves_to_fixed_type_p (instance, 0)))
4648 flags |= LOOKUP_NONVIRTUAL;
4650 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4651 call = build_over_call (cand, mem_args, flags);
4654 call = build_over_call (cand, args, flags);
4655 /* Do evaluate the object parameter in a call to a static member
4657 if (TREE_SIDE_EFFECTS (instance))
4658 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4664 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4665 subsequence of ICS2. */
4668 is_subseq (ics1, ics2)
4671 /* We can assume that a conversion of the same code
4672 between the same types indicates a subsequence since we only get
4673 here if the types we are converting from are the same. */
4675 while (TREE_CODE (ics1) == RVALUE_CONV
4676 || TREE_CODE (ics1) == LVALUE_CONV)
4677 ics1 = TREE_OPERAND (ics1, 0);
4681 while (TREE_CODE (ics2) == RVALUE_CONV
4682 || TREE_CODE (ics2) == LVALUE_CONV)
4683 ics2 = TREE_OPERAND (ics2, 0);
4685 if (TREE_CODE (ics2) == USER_CONV
4686 || TREE_CODE (ics2) == AMBIG_CONV
4687 || TREE_CODE (ics2) == IDENTITY_CONV)
4688 /* At this point, ICS1 cannot be a proper subsequence of
4689 ICS2. We can get a USER_CONV when we are comparing the
4690 second standard conversion sequence of two user conversion
4694 ics2 = TREE_OPERAND (ics2, 0);
4696 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4697 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4698 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4699 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4704 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4705 be any _TYPE nodes. */
4708 is_properly_derived_from (derived, base)
4712 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4713 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4716 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4717 considers every class derived from itself. */
4718 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4719 && DERIVED_FROM_P (base, derived));
4722 /* We build the ICS for an implicit object parameter as a pointer
4723 conversion sequence. However, such a sequence should be compared
4724 as if it were a reference conversion sequence. If ICS is the
4725 implicit conversion sequence for an implicit object parameter,
4726 modify it accordingly. */
4729 maybe_handle_implicit_object (ics)
4732 if (ICS_THIS_FLAG (*ics))
4734 /* [over.match.funcs]
4736 For non-static member functions, the type of the
4737 implicit object parameter is "reference to cv X"
4738 where X is the class of which the function is a
4739 member and cv is the cv-qualification on the member
4740 function declaration. */
4742 tree reference_type;
4744 /* The `this' parameter is a pointer to a class type. Make the
4745 implict conversion talk about a reference to that same class
4747 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4748 reference_type = build_reference_type (reference_type);
4750 if (TREE_CODE (t) == QUAL_CONV)
4751 t = TREE_OPERAND (t, 0);
4752 if (TREE_CODE (t) == PTR_CONV)
4753 t = TREE_OPERAND (t, 0);
4754 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4755 t = direct_reference_binding (reference_type, t);
4760 /* If ICS is a REF_BIND, modify it appropriately, set TARGET_TYPE
4761 to the type the reference originally referred to, and return 1.
4762 Otherwise, return 0. */
4765 maybe_handle_ref_bind (ics, target_type)
4769 if (TREE_CODE (*ics) == REF_BIND)
4771 tree old_ics = *ics;
4772 *target_type = TREE_TYPE (TREE_TYPE (*ics));
4773 *ics = TREE_OPERAND (*ics, 0);
4774 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4775 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4782 /* Compare two implicit conversion sequences according to the rules set out in
4783 [over.ics.rank]. Return values:
4785 1: ics1 is better than ics2
4786 -1: ics2 is better than ics1
4787 0: ics1 and ics2 are indistinguishable */
4790 compare_ics (ics1, ics2)
4797 tree deref_from_type1 = NULL_TREE;
4798 tree deref_from_type2 = NULL_TREE;
4799 tree deref_to_type1 = NULL_TREE;
4800 tree deref_to_type2 = NULL_TREE;
4803 /* REF_BINDING is non-zero if the result of the conversion sequence
4804 is a reference type. In that case TARGET_TYPE is the
4805 type referred to by the reference. */
4811 /* Handle implicit object parameters. */
4812 maybe_handle_implicit_object (&ics1);
4813 maybe_handle_implicit_object (&ics2);
4815 /* Handle reference parameters. */
4816 ref_binding1 = maybe_handle_ref_bind (&ics1, &target_type1);
4817 ref_binding2 = maybe_handle_ref_bind (&ics2, &target_type2);
4821 When comparing the basic forms of implicit conversion sequences (as
4822 defined in _over.best.ics_)
4824 --a standard conversion sequence (_over.ics.scs_) is a better
4825 conversion sequence than a user-defined conversion sequence
4826 or an ellipsis conversion sequence, and
4828 --a user-defined conversion sequence (_over.ics.user_) is a
4829 better conversion sequence than an ellipsis conversion sequence
4830 (_over.ics.ellipsis_). */
4831 rank1 = ICS_RANK (ics1);
4832 rank2 = ICS_RANK (ics2);
4836 else if (rank1 < rank2)
4839 if (rank1 == BAD_RANK)
4841 /* XXX Isn't this an extension? */
4842 /* Both ICS are bad. We try to make a decision based on what
4843 would have happenned if they'd been good. */
4844 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4845 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4847 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4848 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4851 /* We couldn't make up our minds; try to figure it out below. */
4854 if (ICS_ELLIPSIS_FLAG (ics1))
4855 /* Both conversions are ellipsis conversions. */
4858 /* User-defined conversion sequence U1 is a better conversion sequence
4859 than another user-defined conversion sequence U2 if they contain the
4860 same user-defined conversion operator or constructor and if the sec-
4861 ond standard conversion sequence of U1 is better than the second
4862 standard conversion sequence of U2. */
4864 if (ICS_USER_FLAG (ics1))
4868 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4869 if (TREE_CODE (t1) == AMBIG_CONV)
4871 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4872 if (TREE_CODE (t2) == AMBIG_CONV)
4875 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4878 /* We can just fall through here, after setting up
4879 FROM_TYPE1 and FROM_TYPE2. */
4880 from_type1 = TREE_TYPE (t1);
4881 from_type2 = TREE_TYPE (t2);
4885 /* We're dealing with two standard conversion sequences.
4889 Standard conversion sequence S1 is a better conversion
4890 sequence than standard conversion sequence S2 if
4892 --S1 is a proper subsequence of S2 (comparing the conversion
4893 sequences in the canonical form defined by _over.ics.scs_,
4894 excluding any Lvalue Transformation; the identity
4895 conversion sequence is considered to be a subsequence of
4896 any non-identity conversion sequence */
4899 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4900 from_type1 = TREE_OPERAND (from_type1, 0);
4901 from_type1 = TREE_TYPE (from_type1);
4904 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4905 from_type2 = TREE_OPERAND (from_type2, 0);
4906 from_type2 = TREE_TYPE (from_type2);
4909 if (same_type_p (from_type1, from_type2))
4911 if (is_subseq (ics1, ics2))
4913 if (is_subseq (ics2, ics1))
4916 /* Otherwise, one sequence cannot be a subsequence of the other; they
4917 don't start with the same type. This can happen when comparing the
4918 second standard conversion sequence in two user-defined conversion
4925 --the rank of S1 is better than the rank of S2 (by the rules
4928 Standard conversion sequences are ordered by their ranks: an Exact
4929 Match is a better conversion than a Promotion, which is a better
4930 conversion than a Conversion.
4932 Two conversion sequences with the same rank are indistinguishable
4933 unless one of the following rules applies:
4935 --A conversion that is not a conversion of a pointer, or pointer
4936 to member, to bool is better than another conversion that is such
4939 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4940 so that we do not have to check it explicitly. */
4941 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4943 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4946 to_type1 = TREE_TYPE (ics1);
4947 to_type2 = TREE_TYPE (ics2);
4949 if (TYPE_PTR_P (from_type1)
4950 && TYPE_PTR_P (from_type2)
4951 && TYPE_PTR_P (to_type1)
4952 && TYPE_PTR_P (to_type2))
4954 deref_from_type1 = TREE_TYPE (from_type1);
4955 deref_from_type2 = TREE_TYPE (from_type2);
4956 deref_to_type1 = TREE_TYPE (to_type1);
4957 deref_to_type2 = TREE_TYPE (to_type2);
4959 /* The rules for pointers to members A::* are just like the rules
4960 for pointers A*, except opposite: if B is derived from A then
4961 A::* converts to B::*, not vice versa. For that reason, we
4962 switch the from_ and to_ variables here. */
4963 else if (TYPE_PTRMEM_P (from_type1)
4964 && TYPE_PTRMEM_P (from_type2)
4965 && TYPE_PTRMEM_P (to_type1)
4966 && TYPE_PTRMEM_P (to_type2))
4968 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4969 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4970 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4971 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4973 else if (TYPE_PTRMEMFUNC_P (from_type1)
4974 && TYPE_PTRMEMFUNC_P (from_type2)
4975 && TYPE_PTRMEMFUNC_P (to_type1)
4976 && TYPE_PTRMEMFUNC_P (to_type2))
4978 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4979 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4980 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4981 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4984 if (deref_from_type1 != NULL_TREE
4985 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
4986 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
4988 /* This was one of the pointer or pointer-like conversions.
4992 --If class B is derived directly or indirectly from class A,
4993 conversion of B* to A* is better than conversion of B* to
4994 void*, and conversion of A* to void* is better than
4995 conversion of B* to void*. */
4996 if (TREE_CODE (deref_to_type1) == VOID_TYPE
4997 && TREE_CODE (deref_to_type2) == VOID_TYPE)
4999 if (is_properly_derived_from (deref_from_type1,
5002 else if (is_properly_derived_from (deref_from_type2,
5006 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5007 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5009 if (same_type_p (deref_from_type1, deref_from_type2))
5011 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5013 if (is_properly_derived_from (deref_from_type1,
5017 /* We know that DEREF_TO_TYPE1 is `void' here. */
5018 else if (is_properly_derived_from (deref_from_type1,
5023 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5024 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5028 --If class B is derived directly or indirectly from class A
5029 and class C is derived directly or indirectly from B,
5031 --conversion of C* to B* is better than conversion of C* to
5034 --conversion of B* to A* is better than conversion of C* to
5036 if (same_type_p (deref_from_type1, deref_from_type2))
5038 if (is_properly_derived_from (deref_to_type1,
5041 else if (is_properly_derived_from (deref_to_type2,
5045 else if (same_type_p (deref_to_type1, deref_to_type2))
5047 if (is_properly_derived_from (deref_from_type2,
5050 else if (is_properly_derived_from (deref_from_type1,
5056 else if (IS_AGGR_TYPE_CODE (TREE_CODE (from_type1))
5057 && same_type_p (from_type1, from_type2))
5061 --binding of an expression of type C to a reference of type
5062 B& is better than binding an expression of type C to a
5063 reference of type A&
5065 --conversion of C to B is better than conversion of C to A, */
5066 if (is_properly_derived_from (from_type1, to_type1)
5067 && is_properly_derived_from (from_type1, to_type2))
5069 if (is_properly_derived_from (to_type1, to_type2))
5071 else if (is_properly_derived_from (to_type2, to_type1))
5075 else if (IS_AGGR_TYPE_CODE (TREE_CODE (to_type1))
5076 && same_type_p (to_type1, to_type2))
5080 --binding of an expression of type B to a reference of type
5081 A& is better than binding an expression of type C to a
5082 reference of type A&,
5084 --onversion of B to A is better than conversion of C to A */
5085 if (is_properly_derived_from (from_type1, to_type1)
5086 && is_properly_derived_from (from_type2, to_type1))
5088 if (is_properly_derived_from (from_type2, from_type1))
5090 else if (is_properly_derived_from (from_type1, from_type2))
5097 --S1 and S2 differ only in their qualification conversion and yield
5098 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5099 qualification signature of type T1 is a proper subset of the cv-
5100 qualification signature of type T2 */
5101 if (TREE_CODE (ics1) == QUAL_CONV
5102 && TREE_CODE (ics2) == QUAL_CONV
5103 && same_type_p (from_type1, from_type2))
5104 return comp_cv_qual_signature (to_type1, to_type2);
5108 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5109 types to which the references refer are the same type except for
5110 top-level cv-qualifiers, and the type to which the reference
5111 initialized by S2 refers is more cv-qualified than the type to
5112 which the reference initialized by S1 refers */
5114 if (ref_binding1 && ref_binding2
5115 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5116 return comp_cv_qualification (target_type2, target_type1);
5118 /* Neither conversion sequence is better than the other. */
5122 /* The source type for this standard conversion sequence. */
5128 for (;; t = TREE_OPERAND (t, 0))
5130 if (TREE_CODE (t) == USER_CONV
5131 || TREE_CODE (t) == AMBIG_CONV
5132 || TREE_CODE (t) == IDENTITY_CONV)
5133 return TREE_TYPE (t);
5135 my_friendly_abort (1823);
5138 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5139 a pointer to LOSER and re-running joust to produce the warning if WINNER
5140 is actually used. */
5143 add_warning (winner, loser)
5144 struct z_candidate *winner, *loser;
5146 winner->warnings = tree_cons (NULL_TREE,
5147 build_ptr_wrapper (loser),
5151 /* Returns true iff functions are equivalent. Equivalent functions are
5152 not '==' only if one is a function-local extern function or if
5153 both are extern "C". */
5156 equal_functions (fn1, fn2)
5160 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5161 || DECL_EXTERN_C_FUNCTION_P (fn1))
5162 return decls_match (fn1, fn2);
5166 /* Compare two candidates for overloading as described in
5167 [over.match.best]. Return values:
5169 1: cand1 is better than cand2
5170 -1: cand2 is better than cand1
5171 0: cand1 and cand2 are indistinguishable */
5174 joust (cand1, cand2, warn)
5175 struct z_candidate *cand1, *cand2;
5179 int i, off1 = 0, off2 = 0, len;
5181 /* Candidates that involve bad conversions are always worse than those
5183 if (cand1->viable > cand2->viable)
5185 if (cand1->viable < cand2->viable)
5188 /* If we have two pseudo-candidates for conversions to the same type,
5189 or two candidates for the same function, arbitrarily pick one. */
5190 if (cand1->fn == cand2->fn
5191 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5194 /* a viable function F1
5195 is defined to be a better function than another viable function F2 if
5196 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5197 ICSi(F2), and then */
5199 /* for some argument j, ICSj(F1) is a better conversion sequence than
5202 /* For comparing static and non-static member functions, we ignore
5203 the implicit object parameter of the non-static function. The
5204 standard says to pretend that the static function has an object
5205 parm, but that won't work with operator overloading. */
5206 len = TREE_VEC_LENGTH (cand1->convs);
5207 if (len != TREE_VEC_LENGTH (cand2->convs))
5209 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5210 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5212 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5213 && DECL_STATIC_FUNCTION_P (cand2->fn))
5219 my_friendly_abort (42);
5222 for (i = 0; i < len; ++i)
5224 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5225 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5226 int comp = compare_ics (t1, t2);
5231 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5232 && TREE_CODE (t1) == STD_CONV
5233 && TREE_CODE (t2) == STD_CONV
5234 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5235 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5236 && (TYPE_PRECISION (TREE_TYPE (t1))
5237 == TYPE_PRECISION (TREE_TYPE (t2)))
5238 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5239 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5242 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5244 struct z_candidate *w, *l;
5246 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5247 w = cand1, l = cand2;
5249 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5250 w = cand2, l = cand1;
5254 cp_warning ("passing `%T' chooses `%T' over `%T'",
5255 type, type1, type2);
5256 cp_warning (" in call to `%D'", w->fn);
5262 if (winner && comp != winner)
5271 /* warn about confusing overload resolution for user-defined conversions,
5272 either between a constructor and a conversion op, or between two
5274 if (winner && cand1->second_conv
5275 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5276 != DECL_CONSTRUCTOR_P (cand2->fn))
5277 /* Don't warn if the two conv ops convert to the same type... */
5278 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5279 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5280 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5282 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5285 struct z_candidate *w, *l;
5288 w = cand1, l = cand2;
5290 w = cand2, l = cand1;
5291 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5292 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5293 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5294 && (convn = standard_conversion
5295 (TREE_TYPE (TREE_TYPE (l->fn)),
5296 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5297 && TREE_CODE (convn) == QUAL_CONV)
5298 /* Don't complain about `operator char *()' beating
5299 `operator const char *() const'. */;
5302 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5303 if (! DECL_CONSTRUCTOR_P (w->fn))
5304 source = TREE_TYPE (source);
5305 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
5306 cp_warning (" for conversion from `%T' to `%T'",
5307 source, TREE_TYPE (w->second_conv));
5308 cp_warning (" because conversion sequence for the argument is better");
5319 F1 is a non-template function and F2 is a template function
5322 if (! cand1->template && cand2->template)
5324 else if (cand1->template && ! cand2->template)
5328 F1 and F2 are template functions and the function template for F1 is
5329 more specialized than the template for F2 according to the partial
5332 if (cand1->template && cand2->template)
5334 winner = more_specialized
5335 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5337 /* Tell the deduction code how many real function arguments
5338 we saw, not counting the implicit 'this' argument. But,
5339 add_function_candidate() suppresses the "this" argument
5342 [temp.func.order]: The presence of unused ellipsis and default
5343 arguments has no effect on the partial ordering of function
5345 TREE_VEC_LENGTH (cand1->convs)
5346 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5347 - DECL_CONSTRUCTOR_P (cand1->fn)));
5353 /* a non-template user function is better than a builtin. (Pedantically
5354 the builtin which matched the user function should not be added to
5355 the overload set, but we spot it here.
5358 ... the builtin candidates include ...
5359 - do not have the same parameter type list as any non-template
5360 non-member candidate. */
5362 if (TREE_CODE (cand1->fn) != IDENTIFIER_NODE
5363 && TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5365 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5366 && TREE_CODE (cand2->fn) != IDENTIFIER_NODE)
5370 the context is an initialization by user-defined conversion (see
5371 _dcl.init_ and _over.match.user_) and the standard conversion
5372 sequence from the return type of F1 to the destination type (i.e.,
5373 the type of the entity being initialized) is a better conversion
5374 sequence than the standard conversion sequence from the return type
5375 of F2 to the destination type. */
5377 if (cand1->second_conv)
5379 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5384 /* If the built-in candidates are the same, arbitrarily pick one. */
5385 if (cand1->fn == cand2->fn
5386 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5388 for (i = 0; i < len; ++i)
5389 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5390 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5392 if (i == TREE_VEC_LENGTH (cand1->convs))
5395 /* Kludge around broken overloading rules whereby
5396 Integer a, b; test ? a : b; is ambiguous, since there's a builtin
5397 that takes references and another that takes values. */
5398 if (cand1->fn == ansi_opname (COND_EXPR))
5400 tree c1 = TREE_VEC_ELT (cand1->convs, 1);
5401 tree c2 = TREE_VEC_ELT (cand2->convs, 1);
5402 tree t1 = strip_top_quals (non_reference (TREE_TYPE (c1)));
5403 tree t2 = strip_top_quals (non_reference (TREE_TYPE (c2)));
5405 if (same_type_p (t1, t2))
5407 if (TREE_CODE (c1) == REF_BIND && TREE_CODE (c2) != REF_BIND)
5409 if (TREE_CODE (c1) != REF_BIND && TREE_CODE (c2) == REF_BIND)
5415 /* If the two functions are the same (this can happen with declarations
5416 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5417 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5418 && equal_functions (cand1->fn, cand2->fn))
5423 /* Extension: If the worst conversion for one candidate is worse than the
5424 worst conversion for the other, take the first. */
5427 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5428 struct z_candidate *w, *l;
5430 for (i = 0; i < len; ++i)
5432 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5433 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5434 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5435 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5438 winner = 1, w = cand1, l = cand2;
5440 winner = -1, w = cand2, l = cand1;
5445 cp_pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5447 " because worst conversion for the former is better than worst conversion for the latter");
5455 my_friendly_assert (!winner, 20010121);
5459 /* Given a list of candidates for overloading, find the best one, if any.
5460 This algorithm has a worst case of O(2n) (winner is last), and a best
5461 case of O(n/2) (totally ambiguous); much better than a sorting
5464 static struct z_candidate *
5465 tourney (candidates)
5466 struct z_candidate *candidates;
5468 struct z_candidate *champ = candidates, *challenger;
5470 int champ_compared_to_predecessor = 0;
5472 /* Walk through the list once, comparing each current champ to the next
5473 candidate, knocking out a candidate or two with each comparison. */
5475 for (challenger = champ->next; challenger; )
5477 fate = joust (champ, challenger, 0);
5479 challenger = challenger->next;
5484 champ = challenger->next;
5487 champ_compared_to_predecessor = 0;
5492 champ_compared_to_predecessor = 1;
5495 challenger = champ->next;
5499 /* Make sure the champ is better than all the candidates it hasn't yet
5500 been compared to. */
5502 for (challenger = candidates;
5504 && !(champ_compared_to_predecessor && challenger->next == champ);
5505 challenger = challenger->next)
5507 fate = joust (champ, challenger, 0);
5515 /* Returns non-zero if things of type FROM can be converted to TO. */
5518 can_convert (to, from)
5521 return can_convert_arg (to, from, NULL_TREE);
5524 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5527 can_convert_arg (to, from, arg)
5530 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5531 return (t && ! ICS_BAD_FLAG (t));
5534 /* Convert EXPR to TYPE. Return the converted expression. */
5537 perform_implicit_conversion (type, expr)
5543 if (expr == error_mark_node)
5544 return error_mark_node;
5545 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5547 if (!conv || ICS_BAD_FLAG (conv))
5549 cp_error ("could not convert `%E' to `%T'", expr, type);
5550 return error_mark_node;
5553 return convert_like (conv, expr);
5556 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5557 initializing a variable of that TYPE. Return the converted
5561 initialize_reference (type, expr)
5567 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5568 if (!conv || ICS_BAD_FLAG (conv))
5570 cp_error ("could not convert `%E' to `%T'", expr, type);
5571 return error_mark_node;
5574 return convert_like (conv, expr);