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. */
37 #include "diagnostic.h"
39 extern int inhibit_warnings;
41 static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
43 static tree build_field_call PARAMS ((tree, tree, tree, tree));
44 static struct z_candidate * tourney PARAMS ((struct z_candidate *));
45 static int equal_functions PARAMS ((tree, tree));
46 static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
47 static int compare_ics PARAMS ((tree, tree));
48 static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
49 static tree build_java_interface_fn_ref PARAMS ((tree, tree));
50 #define convert_like(CONV, EXPR) convert_like_real (CONV, EXPR, NULL_TREE, 0, 0)
51 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) convert_like_real (CONV, EXPR, FN, ARGNO, 0)
52 static tree convert_like_real PARAMS ((tree, tree, tree, int, int));
53 static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
55 static tree build_object_call PARAMS ((tree, tree));
56 static tree resolve_args PARAMS ((tree));
57 static struct z_candidate * build_user_type_conversion_1
58 PARAMS ((tree, tree, int));
59 static void print_z_candidates PARAMS ((struct z_candidate *));
60 static tree build_this PARAMS ((tree));
61 static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
62 static int any_viable PARAMS ((struct z_candidate *));
63 static struct z_candidate * add_template_candidate
64 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
66 static struct z_candidate * add_template_candidate_real
67 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
68 tree, unification_kind_t));
69 static struct z_candidate * add_template_conv_candidate
70 PARAMS ((struct z_candidate *, tree, tree, tree, tree));
71 static struct z_candidate * add_builtin_candidates
72 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
74 static struct z_candidate * add_builtin_candidate
75 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
76 tree, tree, tree, tree *, tree *, int));
77 static int is_complete PARAMS ((tree));
78 static struct z_candidate * build_builtin_candidate
79 PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
81 static struct z_candidate * add_conv_candidate
82 PARAMS ((struct z_candidate *, tree, tree, tree));
83 static struct z_candidate * add_function_candidate
84 PARAMS ((struct z_candidate *, tree, tree, tree, int));
85 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
86 static tree standard_conversion PARAMS ((tree, tree, tree));
87 static tree reference_binding PARAMS ((tree, tree, tree, int));
88 static tree non_reference PARAMS ((tree));
89 static tree build_conv PARAMS ((enum tree_code, tree, tree));
90 static int is_subseq PARAMS ((tree, tree));
91 static tree maybe_handle_ref_bind PARAMS ((tree*));
92 static void maybe_handle_implicit_object PARAMS ((tree*));
93 static struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
95 static tree source_type PARAMS ((tree));
96 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
97 static int reference_related_p PARAMS ((tree, tree));
98 static int reference_compatible_p PARAMS ((tree, tree));
99 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
100 static tree direct_reference_binding PARAMS ((tree, tree));
101 static int promoted_arithmetic_type_p PARAMS ((tree));
102 static tree conditional_conversion PARAMS ((tree, tree));
105 build_vfield_ref (datum, type)
110 if (datum == error_mark_node)
111 return error_mark_node;
113 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
114 datum = convert_from_reference (datum);
116 if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
117 rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
118 datum, TYPE_VFIELD (type));
120 rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
125 /* Build a call to a member of an object. I.e., one that overloads
126 operator ()(), or is a pointer-to-function or pointer-to-method. */
129 build_field_call (basetype_path, instance_ptr, name, parms)
130 tree basetype_path, instance_ptr, name, parms;
132 tree field, instance;
134 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
137 /* Speed up the common case. */
138 if (instance_ptr == current_class_ptr
139 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
142 field = lookup_field (basetype_path, name, 1, 0);
144 if (field == error_mark_node || field == NULL_TREE)
147 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
149 /* If it's a field, try overloading operator (),
150 or calling if the field is a pointer-to-function. */
151 instance = build_indirect_ref (instance_ptr, NULL);
152 instance = build_component_ref_1 (instance, field, 0);
154 if (instance == error_mark_node)
155 return error_mark_node;
157 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
158 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
159 instance, parms, NULL_TREE);
160 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
161 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
162 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
164 return build_function_call (instance, parms);
170 /* Returns nonzero iff the destructor name specified in NAME
171 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
175 check_dtor_name (basetype, name)
178 name = TREE_OPERAND (name, 0);
180 /* Just accept something we've already complained about. */
181 if (name == error_mark_node)
184 if (TREE_CODE (name) == TYPE_DECL)
185 name = TREE_TYPE (name);
186 else if (TYPE_P (name))
188 else if (TREE_CODE (name) == IDENTIFIER_NODE)
190 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
191 || (TREE_CODE (basetype) == ENUMERAL_TYPE
192 && name == TYPE_IDENTIFIER (basetype)))
195 name = get_type_value (name);
199 template <class T> struct S { ~S(); };
203 NAME will be a class template. */
204 else if (DECL_CLASS_TEMPLATE_P (name))
207 my_friendly_abort (980605);
209 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
214 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
215 This is how virtual function calls are avoided. */
218 build_scoped_method_call (exp, basetype, name, parms)
219 tree exp, basetype, name, parms;
221 /* Because this syntactic form does not allow
222 a pointer to a base class to be `stolen',
223 we need not protect the derived->base conversion
226 @@ But we do have to check access privileges later. */
228 tree type = TREE_TYPE (exp);
230 if (type == error_mark_node
231 || basetype == error_mark_node)
232 return error_mark_node;
234 if (processing_template_decl)
236 if (TREE_CODE (name) == BIT_NOT_EXPR
237 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
239 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
241 name = build_min_nt (BIT_NOT_EXPR, type);
243 name = build_min_nt (SCOPE_REF, basetype, name);
244 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
247 if (TREE_CODE (type) == REFERENCE_TYPE)
248 type = TREE_TYPE (type);
250 if (TREE_CODE (basetype) == TREE_VEC)
253 basetype = BINFO_TYPE (binfo);
258 /* Check the destructor call syntax. */
259 if (TREE_CODE (name) == BIT_NOT_EXPR)
261 /* We can get here if someone writes their destructor call like
262 `obj.NS::~T()'; this isn't really a scoped method call, so hand
264 if (TREE_CODE (basetype) == NAMESPACE_DECL)
265 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
267 if (! check_dtor_name (basetype, name))
268 cp_error ("qualified type `%T' does not match destructor name `~%T'",
269 basetype, TREE_OPERAND (name, 0));
271 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
272 that explicit ~int is caught in the parser; this deals with typedefs
273 and template parms. */
274 if (! IS_AGGR_TYPE (basetype))
276 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
277 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
278 exp, basetype, type);
280 return cp_convert (void_type_node, exp);
284 if (TREE_CODE (basetype) == NAMESPACE_DECL)
286 cp_error ("`%D' is a namespace", basetype);
287 return error_mark_node;
289 if (! is_aggr_type (basetype, 1))
290 return error_mark_node;
292 if (! IS_AGGR_TYPE (type))
294 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
296 return error_mark_node;
301 binfo = lookup_base (type, basetype, ba_check, NULL);
302 if (binfo == error_mark_node)
303 return error_mark_node;
305 error_not_base_type (basetype, type);
310 if (TREE_CODE (exp) == INDIRECT_REF)
312 decl = build_base_path (PLUS_EXPR,
313 build_unary_op (ADDR_EXPR, exp, 0),
315 decl = build_indirect_ref (decl, NULL);
318 decl = build_scoped_ref (exp, basetype);
320 /* Call to a destructor. */
321 if (TREE_CODE (name) == BIT_NOT_EXPR)
323 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
324 return cp_convert (void_type_node, exp);
326 return build_delete (TREE_TYPE (decl), decl,
327 sfk_complete_destructor,
328 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
332 /* Call to a method. */
333 return build_method_call (decl, name, parms, binfo,
334 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
336 return error_mark_node;
339 /* We want the address of a function or method. We avoid creating a
340 pointer-to-member function. */
343 build_addr_func (function)
346 tree type = TREE_TYPE (function);
348 /* We have to do these by hand to avoid real pointer to member
350 if (TREE_CODE (type) == METHOD_TYPE)
354 type = build_pointer_type (type);
356 if (mark_addressable (function) == 0)
357 return error_mark_node;
359 addr = build1 (ADDR_EXPR, type, function);
361 /* Address of a static or external variable or function counts
363 if (staticp (function))
364 TREE_CONSTANT (addr) = 1;
369 function = default_conversion (function);
374 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
375 POINTER_TYPE to those. Note, pointer to member function types
376 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
379 build_call (function, parms)
380 tree function, parms;
382 int is_constructor = 0;
388 function = build_addr_func (function);
390 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
392 sorry ("unable to call pointer to member function here");
393 return error_mark_node;
396 result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
398 if (TREE_CODE (function) == ADDR_EXPR
399 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
400 decl = TREE_OPERAND (function, 0);
404 /* We check both the decl and the type; a function may be known not to
405 throw without being declared throw(). */
406 nothrow = ((decl && TREE_NOTHROW (decl))
407 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
409 if (decl && DECL_CONSTRUCTOR_P (decl))
412 if (decl && ! TREE_USED (decl))
414 /* We invoke build_call directly for several library functions.
415 These may have been declared normally if we're building libgcc,
416 so we can't just check DECL_ARTIFICIAL. */
417 if (DECL_ARTIFICIAL (decl)
418 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
421 my_friendly_abort (990125);
424 /* Don't pass empty class objects by value. This is useful
425 for tags in STL, which are used to control overload resolution.
426 We don't need to handle other cases of copying empty classes. */
427 if (! decl || ! DECL_BUILT_IN (decl))
428 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
429 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
430 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
432 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
433 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
434 TREE_VALUE (tmp), t);
437 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
438 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
439 TREE_TYPE (function) = result_type;
440 TREE_SIDE_EFFECTS (function) = 1;
441 TREE_NOTHROW (function) = nothrow;
446 /* Build something of the form ptr->method (args)
447 or object.method (args). This can also build
448 calls to constructors, and find friends.
450 Member functions always take their class variable
453 INSTANCE is a class instance.
455 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
457 PARMS help to figure out what that NAME really refers to.
459 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
460 down to the real instance type to use for access checking. We need this
461 information to get protected accesses correct. This parameter is used
462 by build_member_call.
464 FLAGS is the logical disjunction of zero or more LOOKUP_
465 flags. See cp-tree.h for more info.
467 If this is all OK, calls build_function_call with the resolved
470 This function must also handle being called to perform
471 initialization, promotion/coercion of arguments, and
472 instantiation of default parameters.
474 Note that NAME may refer to an instance variable name. If
475 `operator()()' is defined for the type of that field, then we return
478 #ifdef GATHER_STATISTICS
479 extern int n_build_method_call;
483 build_method_call (instance, name, parms, basetype_path, flags)
484 tree instance, name, parms, basetype_path;
487 tree basetype, instance_ptr;
489 #ifdef GATHER_STATISTICS
490 n_build_method_call++;
493 if (instance == error_mark_node
494 || name == error_mark_node
495 || parms == error_mark_node
496 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
497 return error_mark_node;
499 if (processing_template_decl)
501 /* We need to process template parm names here so that tsubst catches
502 them properly. Other type names can wait. */
503 if (TREE_CODE (name) == BIT_NOT_EXPR)
505 tree type = NULL_TREE;
507 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
508 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
509 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
510 type = TREE_TYPE (TREE_OPERAND (name, 0));
512 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
513 name = build_min_nt (BIT_NOT_EXPR, type);
516 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
519 if (TREE_CODE (name) == BIT_NOT_EXPR)
522 error ("destructors take no parameters");
523 basetype = TREE_TYPE (instance);
524 if (TREE_CODE (basetype) == REFERENCE_TYPE)
525 basetype = TREE_TYPE (basetype);
527 if (! check_dtor_name (basetype, name))
529 ("destructor name `~%T' does not match type `%T' of expression",
530 TREE_OPERAND (name, 0), basetype);
532 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
533 return cp_convert (void_type_node, instance);
534 instance = default_conversion (instance);
535 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
536 return build_delete (build_pointer_type (basetype),
537 instance_ptr, sfk_complete_destructor,
538 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
541 return build_new_method_call (instance, name, parms, basetype_path, flags);
544 /* New overloading code. */
554 struct z_candidate *next;
557 #define IDENTITY_RANK 0
563 #define ELLIPSIS_RANK 6
566 #define ICS_RANK(NODE) \
567 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
568 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
569 : ICS_USER_FLAG (NODE) ? USER_RANK \
570 : ICS_STD_RANK (NODE))
572 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
574 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
575 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
576 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
577 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
579 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
580 should be created to hold the result of the conversion. */
581 #define NEED_TEMPORARY_P(NODE) (TREE_LANG_FLAG_4 ((NODE)))
583 #define USER_CONV_CAND(NODE) \
584 ((struct z_candidate *)WRAPPER_PTR (TREE_OPERAND (NODE, 1)))
585 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
593 A null pointer constant is an integral constant expression
594 (_expr.const_) rvalue of integer type that evaluates to zero. */
596 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
602 /* Returns non-zero if PARMLIST consists of only default parms and/or
606 sufficient_parms_p (parmlist)
609 for (; parmlist && parmlist != void_list_node;
610 parmlist = TREE_CHAIN (parmlist))
611 if (!TREE_PURPOSE (parmlist))
617 build_conv (code, type, from)
622 int rank = ICS_STD_RANK (from);
624 /* We can't use buildl1 here because CODE could be USER_CONV, which
625 takes two arguments. In that case, the caller is responsible for
626 filling in the second argument. */
627 t = make_node (code);
628 TREE_TYPE (t) = type;
629 TREE_OPERAND (t, 0) = from;
642 if (rank < EXACT_RANK)
648 ICS_STD_RANK (t) = rank;
649 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
650 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
654 /* If T is a REFERENCE_TYPE return the type to which T refers.
655 Otherwise, return T itself. */
661 if (TREE_CODE (t) == REFERENCE_TYPE)
670 if (TREE_CODE (t) == ARRAY_TYPE)
672 return TYPE_MAIN_VARIANT (t);
675 /* Returns the standard conversion path (see [conv]) from type FROM to type
676 TO, if any. For proper handling of null pointer constants, you must
677 also pass the expression EXPR to convert from. */
680 standard_conversion (to, from, expr)
683 enum tree_code fcode, tcode;
687 if (TREE_CODE (to) == REFERENCE_TYPE)
689 if (TREE_CODE (from) == REFERENCE_TYPE)
692 from = TREE_TYPE (from);
694 to = strip_top_quals (to);
695 from = strip_top_quals (from);
697 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
698 && expr && type_unknown_p (expr))
700 expr = instantiate_type (to, expr, itf_none);
701 if (expr == error_mark_node)
703 from = TREE_TYPE (expr);
706 fcode = TREE_CODE (from);
707 tcode = TREE_CODE (to);
709 conv = build1 (IDENTITY_CONV, from, expr);
711 if (fcode == FUNCTION_TYPE)
713 from = build_pointer_type (from);
714 fcode = TREE_CODE (from);
715 conv = build_conv (LVALUE_CONV, from, conv);
717 else if (fcode == ARRAY_TYPE)
719 from = build_pointer_type (TREE_TYPE (from));
720 fcode = TREE_CODE (from);
721 conv = build_conv (LVALUE_CONV, from, conv);
723 else if (fromref || (expr && lvalue_p (expr)))
724 conv = build_conv (RVALUE_CONV, from, conv);
726 /* Allow conversion between `__complex__' data types */
727 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
729 /* The standard conversion sequence to convert FROM to TO is
730 the standard conversion sequence to perform componentwise
732 tree part_conv = standard_conversion
733 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
737 conv = build_conv (TREE_CODE (part_conv), to, conv);
738 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
746 if (same_type_p (from, to))
749 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
750 && expr && null_ptr_cst_p (expr))
752 conv = build_conv (STD_CONV, to, conv);
754 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
755 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
757 /* For backwards brain damage compatibility, allow interconversion of
758 pointers and integers with a pedwarn. */
759 conv = build_conv (STD_CONV, to, conv);
760 ICS_BAD_FLAG (conv) = 1;
762 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
763 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
765 /* For backwards brain damage compatibility, allow interconversion of
766 enums and integers with a pedwarn. */
767 conv = build_conv (STD_CONV, to, conv);
768 ICS_BAD_FLAG (conv) = 1;
770 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
772 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
773 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
775 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
778 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
779 && ufcode != FUNCTION_TYPE)
781 from = build_pointer_type
782 (cp_build_qualified_type (void_type_node,
783 cp_type_quals (TREE_TYPE (from))));
784 conv = build_conv (PTR_CONV, from, conv);
786 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
788 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
789 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
790 tree binfo = lookup_base (tbase, fbase, ba_check, NULL);
792 if (binfo && !binfo_from_vbase (binfo)
793 && (same_type_ignoring_top_level_qualifiers_p
794 (TREE_TYPE (TREE_TYPE (from)),
795 TREE_TYPE (TREE_TYPE (to)))))
797 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
798 from = build_pointer_type (from);
799 conv = build_conv (PMEM_CONV, from, conv);
802 else if (IS_AGGR_TYPE (TREE_TYPE (from))
803 && IS_AGGR_TYPE (TREE_TYPE (to)))
805 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
808 cp_build_qualified_type (TREE_TYPE (to),
809 cp_type_quals (TREE_TYPE (from)));
810 from = build_pointer_type (from);
811 conv = build_conv (PTR_CONV, from, conv);
815 if (same_type_p (from, to))
817 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
818 conv = build_conv (QUAL_CONV, to, conv);
819 else if (expr && string_conv_p (to, expr, 0))
820 /* converting from string constant to char *. */
821 conv = build_conv (QUAL_CONV, to, conv);
822 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
824 conv = build_conv (PTR_CONV, to, conv);
825 ICS_BAD_FLAG (conv) = 1;
832 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
834 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
835 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
836 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
837 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
838 tree binfo = lookup_base (tbase, fbase, ba_check, NULL);
840 if (!binfo || binfo_from_vbase (binfo)
841 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
842 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
843 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
844 || cp_type_quals (fbase) != cp_type_quals (tbase))
847 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
848 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
849 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
850 from = build_ptrmemfunc_type (build_pointer_type (from));
851 conv = build_conv (PMEM_CONV, from, conv);
853 else if (tcode == BOOLEAN_TYPE)
855 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
856 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
859 conv = build_conv (STD_CONV, to, conv);
860 if (fcode == POINTER_TYPE
861 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
862 ICS_STD_RANK (conv) = PBOOL_RANK;
864 /* We don't check for ENUMERAL_TYPE here because there are no standard
865 conversions to enum type. */
866 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
867 || tcode == REAL_TYPE)
869 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
871 conv = build_conv (STD_CONV, to, conv);
873 /* Give this a better rank if it's a promotion. */
874 if (to == type_promotes_to (from)
875 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
876 ICS_STD_RANK (conv) = PROMO_RANK;
878 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
879 && is_properly_derived_from (from, to))
881 if (TREE_CODE (conv) == RVALUE_CONV)
882 conv = TREE_OPERAND (conv, 0);
883 conv = build_conv (BASE_CONV, to, conv);
884 /* The derived-to-base conversion indicates the initialization
885 of a parameter with base type from an object of a derived
886 type. A temporary object is created to hold the result of
888 NEED_TEMPORARY_P (conv) = 1;
896 /* Returns non-zero if T1 is reference-related to T2. */
899 reference_related_p (t1, t2)
903 t1 = TYPE_MAIN_VARIANT (t1);
904 t2 = TYPE_MAIN_VARIANT (t2);
908 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
909 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
911 return (same_type_p (t1, t2)
912 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
913 && DERIVED_FROM_P (t1, t2)));
916 /* Returns non-zero if T1 is reference-compatible with T2. */
919 reference_compatible_p (t1, t2)
925 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
926 reference-related to T2 and cv1 is the same cv-qualification as,
927 or greater cv-qualification than, cv2. */
928 return (reference_related_p (t1, t2)
929 && at_least_as_qualified_p (t1, t2));
932 /* Determine whether or not the EXPR (of class type S) can be
933 converted to T as in [over.match.ref]. */
936 convert_class_to_reference (t, s, expr)
944 struct z_candidate *candidates;
945 struct z_candidate *cand;
949 Assuming that "cv1 T" is the underlying type of the reference
950 being initialized, and "cv S" is the type of the initializer
951 expression, with S a class type, the candidate functions are
954 --The conversion functions of S and its base classes are
955 considered. Those that are not hidden within S and yield type
956 "reference to cv2 T2", where "cv1 T" is reference-compatible
957 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
959 The argument list has one argument, which is the initializer
964 /* Conceptually, we should take the address of EXPR and put it in
965 the argument list. Unfortunately, however, that can result in
966 error messages, which we should not issue now because we are just
967 trying to find a conversion operator. Therefore, we use NULL,
968 cast to the appropriate type. */
969 arglist = build_int_2 (0, 0);
970 TREE_TYPE (arglist) = build_pointer_type (s);
971 arglist = build_tree_list (NULL_TREE, arglist);
973 for (conversions = lookup_conversions (s);
975 conversions = TREE_CHAIN (conversions))
977 tree fns = TREE_VALUE (conversions);
979 for (; fns; fns = OVL_NEXT (fns))
981 tree f = OVL_CURRENT (fns);
982 tree t2 = TREE_TYPE (TREE_TYPE (f));
983 struct z_candidate *old_candidates = candidates;
985 /* If this is a template function, try to get an exact
987 if (TREE_CODE (f) == TEMPLATE_DECL)
990 = add_template_candidate (candidates,
994 build_reference_type (t),
998 if (candidates != old_candidates)
1000 /* Now, see if the conversion function really returns
1001 an lvalue of the appropriate type. From the
1002 point of view of unification, simply returning an
1003 rvalue of the right type is good enough. */
1005 t2 = TREE_TYPE (TREE_TYPE (f));
1006 if (TREE_CODE (t2) != REFERENCE_TYPE
1007 || !reference_compatible_p (t, TREE_TYPE (t2)))
1008 candidates = candidates->next;
1011 else if (TREE_CODE (t2) == REFERENCE_TYPE
1012 && reference_compatible_p (t, TREE_TYPE (t2)))
1014 = add_function_candidate (candidates, f, s, arglist,
1017 if (candidates != old_candidates)
1018 candidates->basetype_path = TYPE_BINFO (s);
1022 /* If none of the conversion functions worked out, let our caller
1024 if (!any_viable (candidates))
1027 candidates = splice_viable (candidates);
1028 cand = tourney (candidates);
1032 conv = build1 (IDENTITY_CONV, s, expr);
1033 conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1035 TREE_OPERAND (conv, 1) = build_ptr_wrapper (cand);
1036 ICS_USER_FLAG (conv) = 1;
1037 if (cand->viable == -1)
1038 ICS_BAD_FLAG (conv) = 1;
1039 cand->second_conv = conv;
1044 /* A reference of the indicated TYPE is being bound directly to the
1045 expression represented by the implicit conversion sequence CONV.
1046 Return a conversion sequence for this binding. */
1049 direct_reference_binding (type, conv)
1053 tree t = TREE_TYPE (type);
1057 When a parameter of reference type binds directly
1058 (_dcl.init.ref_) to an argument expression, the implicit
1059 conversion sequence is the identity conversion, unless the
1060 argument expression has a type that is a derived class of the
1061 parameter type, in which case the implicit conversion sequence is
1062 a derived-to-base Conversion.
1064 If the parameter binds directly to the result of applying a
1065 conversion function to the argument expression, the implicit
1066 conversion sequence is a user-defined conversion sequence
1067 (_over.ics.user_), with the second standard conversion sequence
1068 either an identity conversion or, if the conversion function
1069 returns an entity of a type that is a derived class of the
1070 parameter type, a derived-to-base conversion. */
1071 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1073 /* Represent the derived-to-base conversion. */
1074 conv = build_conv (BASE_CONV, t, conv);
1075 /* We will actually be binding to the base-class subobject in
1076 the derived class, so we mark this conversion appropriately.
1077 That way, convert_like knows not to generate a temporary. */
1078 NEED_TEMPORARY_P (conv) = 0;
1080 return build_conv (REF_BIND, type, conv);
1083 /* Returns the conversion path from type FROM to reference type TO for
1084 purposes of reference binding. For lvalue binding, either pass a
1085 reference type to FROM or an lvalue expression to EXPR. If the
1086 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1087 the conversion returned. */
1090 reference_binding (rto, rfrom, expr, flags)
1091 tree rto, rfrom, expr;
1094 tree conv = NULL_TREE;
1095 tree to = TREE_TYPE (rto);
1099 cp_lvalue_kind lvalue_p = clk_none;
1101 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1103 expr = instantiate_type (to, expr, itf_none);
1104 if (expr == error_mark_node)
1106 from = TREE_TYPE (expr);
1109 if (TREE_CODE (from) == REFERENCE_TYPE)
1111 /* Anything with reference type is an lvalue. */
1112 lvalue_p = clk_ordinary;
1113 from = TREE_TYPE (from);
1116 lvalue_p = real_lvalue_p (expr);
1118 /* Figure out whether or not the types are reference-related and
1119 reference compatible. We have do do this after stripping
1120 references from FROM. */
1121 related_p = reference_related_p (to, from);
1122 compatible_p = reference_compatible_p (to, from);
1124 if (lvalue_p && compatible_p)
1128 If the intializer expression
1130 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1131 is reference-compatible with "cv2 T2,"
1133 the reference is bound directly to the initializer exprssion
1135 conv = build1 (IDENTITY_CONV, from, expr);
1136 conv = direct_reference_binding (rto, conv);
1137 if ((lvalue_p & clk_bitfield) != 0
1138 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1139 /* For the purposes of overload resolution, we ignore the fact
1140 this expression is a bitfield. (In particular,
1141 [over.ics.ref] says specifically that a function with a
1142 non-const reference parameter is viable even if the
1143 argument is a bitfield.)
1145 However, when we actually call the function we must create
1146 a temporary to which to bind the reference. If the
1147 reference is volatile, or isn't const, then we cannot make
1148 a temporary, so we just issue an error when the conversion
1150 NEED_TEMPORARY_P (conv) = 1;
1153 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1157 If the initializer exprsesion
1159 -- has a class type (i.e., T2 is a class type) can be
1160 implicitly converted to an lvalue of type "cv3 T3," where
1161 "cv1 T1" is reference-compatible with "cv3 T3". (this
1162 conversion is selected by enumerating the applicable
1163 conversion functions (_over.match.ref_) and choosing the
1164 best one through overload resolution. (_over.match_).
1166 the reference is bound to the lvalue result of the conversion
1167 in the second case. */
1168 conv = convert_class_to_reference (to, from, expr);
1170 return direct_reference_binding (rto, conv);
1173 /* From this point on, we conceptually need temporaries, even if we
1174 elide them. Only the cases above are "direct bindings". */
1175 if (flags & LOOKUP_NO_TEMP_BIND)
1180 When a parameter of reference type is not bound directly to an
1181 argument expression, the conversion sequence is the one required
1182 to convert the argument expression to the underlying type of the
1183 reference according to _over.best.ics_. Conceptually, this
1184 conversion sequence corresponds to copy-initializing a temporary
1185 of the underlying type with the argument expression. Any
1186 difference in top-level cv-qualification is subsumed by the
1187 initialization itself and does not constitute a conversion. */
1191 Otherwise, the reference shall be to a non-volatile const type. */
1192 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1197 If the initializer expression is an rvalue, with T2 a class type,
1198 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1199 is bound in one of the following ways:
1201 -- The reference is bound to the object represented by the rvalue
1202 or to a sub-object within that object.
1204 In this case, the implicit conversion sequence is supposed to be
1205 same as we would obtain by generating a temporary. Fortunately,
1206 if the types are reference compatible, then this is either an
1207 identity conversion or the derived-to-base conversion, just as
1208 for direct binding. */
1209 if (CLASS_TYPE_P (from) && compatible_p)
1211 conv = build1 (IDENTITY_CONV, from, expr);
1212 return direct_reference_binding (rto, conv);
1217 Otherwise, a temporary of type "cv1 T1" is created and
1218 initialized from the initializer expression using the rules for a
1219 non-reference copy initialization. If T1 is reference-related to
1220 T2, cv1 must be the same cv-qualification as, or greater
1221 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1222 if (related_p && !at_least_as_qualified_p (to, from))
1225 conv = implicit_conversion (to, from, expr, flags);
1229 conv = build_conv (REF_BIND, rto, conv);
1230 /* This reference binding, unlike those above, requires the
1231 creation of a temporary. */
1232 NEED_TEMPORARY_P (conv) = 1;
1237 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1238 to type TO. The optional expression EXPR may affect the conversion.
1239 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1243 implicit_conversion (to, from, expr, flags)
1244 tree to, from, expr;
1248 struct z_candidate *cand;
1250 /* Resolve expressions like `A::p' that we thought might become
1251 pointers-to-members. */
1252 if (expr && TREE_CODE (expr) == OFFSET_REF)
1254 expr = resolve_offset_ref (expr);
1255 from = TREE_TYPE (expr);
1258 if (from == error_mark_node || to == error_mark_node
1259 || expr == error_mark_node)
1262 /* Make sure both the FROM and TO types are complete so that
1263 user-defined conversions are available. */
1264 complete_type (from);
1267 if (TREE_CODE (to) == REFERENCE_TYPE)
1268 conv = reference_binding (to, from, expr, flags);
1270 conv = standard_conversion (to, from, expr);
1274 else if (expr != NULL_TREE
1275 && (IS_AGGR_TYPE (from)
1276 || IS_AGGR_TYPE (to))
1277 && (flags & LOOKUP_NO_CONVERSION) == 0)
1279 cand = build_user_type_conversion_1
1280 (to, expr, LOOKUP_ONLYCONVERTING);
1282 conv = cand->second_conv;
1284 /* We used to try to bind a reference to a temporary here, but that
1285 is now handled by the recursive call to this function at the end
1286 of reference_binding. */
1292 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1295 static struct z_candidate *
1296 add_candidate (candidates, fn, convs, viable)
1297 struct z_candidate *candidates;
1301 struct z_candidate *cand
1302 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1305 cand->convs = convs;
1306 cand->viable = viable;
1307 cand->next = candidates;
1312 /* Create an overload candidate for the function or method FN called with
1313 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1314 to implicit_conversion.
1316 CTYPE, if non-NULL, is the type we want to pretend this function
1317 comes from for purposes of overload resolution. */
1319 static struct z_candidate *
1320 add_function_candidate (candidates, fn, ctype, arglist, flags)
1321 struct z_candidate *candidates;
1322 tree fn, ctype, arglist;
1325 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1328 tree parmnode, argnode;
1331 /* The `this', `in_chrg' and VTT arguments to constructors are not
1332 considered in overload resolution. */
1333 if (DECL_CONSTRUCTOR_P (fn))
1335 parmlist = skip_artificial_parms_for (fn, parmlist);
1336 arglist = skip_artificial_parms_for (fn, arglist);
1339 len = list_length (arglist);
1340 convs = make_tree_vec (len);
1342 /* 13.3.2 - Viable functions [over.match.viable]
1343 First, to be a viable function, a candidate function shall have enough
1344 parameters to agree in number with the arguments in the list.
1346 We need to check this first; otherwise, checking the ICSes might cause
1347 us to produce an ill-formed template instantiation. */
1349 parmnode = parmlist;
1350 for (i = 0; i < len; ++i)
1352 if (parmnode == NULL_TREE || parmnode == void_list_node)
1354 parmnode = TREE_CHAIN (parmnode);
1357 if (i < len && parmnode)
1360 /* Make sure there are default args for the rest of the parms. */
1361 else if (!sufficient_parms_p (parmnode))
1367 /* Second, for F to be a viable function, there shall exist for each
1368 argument an implicit conversion sequence that converts that argument
1369 to the corresponding parameter of F. */
1371 parmnode = parmlist;
1374 for (i = 0; i < len; ++i)
1376 tree arg = TREE_VALUE (argnode);
1377 tree argtype = lvalue_type (arg);
1381 if (parmnode == void_list_node)
1384 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1385 && ! DECL_CONSTRUCTOR_P (fn));
1389 tree parmtype = TREE_VALUE (parmnode);
1391 /* The type of the implicit object parameter ('this') for
1392 overload resolution is not always the same as for the
1393 function itself; conversion functions are considered to
1394 be members of the class being converted, and functions
1395 introduced by a using-declaration are considered to be
1396 members of the class that uses them.
1398 Since build_over_call ignores the ICS for the `this'
1399 parameter, we can just change the parm type. */
1400 if (ctype && is_this)
1403 = build_qualified_type (ctype,
1404 TYPE_QUALS (TREE_TYPE (parmtype)));
1405 parmtype = build_pointer_type (parmtype);
1408 t = implicit_conversion (parmtype, argtype, arg, flags);
1412 t = build1 (IDENTITY_CONV, argtype, arg);
1413 ICS_ELLIPSIS_FLAG (t) = 1;
1417 ICS_THIS_FLAG (t) = 1;
1419 TREE_VEC_ELT (convs, i) = t;
1426 if (ICS_BAD_FLAG (t))
1430 parmnode = TREE_CHAIN (parmnode);
1431 argnode = TREE_CHAIN (argnode);
1435 return add_candidate (candidates, fn, convs, viable);
1438 /* Create an overload candidate for the conversion function FN which will
1439 be invoked for expression OBJ, producing a pointer-to-function which
1440 will in turn be called with the argument list ARGLIST, and add it to
1441 CANDIDATES. FLAGS is passed on to implicit_conversion.
1443 Actually, we don't really care about FN; we care about the type it
1444 converts to. There may be multiple conversion functions that will
1445 convert to that type, and we rely on build_user_type_conversion_1 to
1446 choose the best one; so when we create our candidate, we record the type
1447 instead of the function. */
1449 static struct z_candidate *
1450 add_conv_candidate (candidates, fn, obj, arglist)
1451 struct z_candidate *candidates;
1452 tree fn, obj, arglist;
1454 tree totype = TREE_TYPE (TREE_TYPE (fn));
1455 int i, len, viable, flags;
1456 tree parmlist, convs, parmnode, argnode;
1458 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1459 parmlist = TREE_TYPE (parmlist);
1460 parmlist = TYPE_ARG_TYPES (parmlist);
1462 len = list_length (arglist) + 1;
1463 convs = make_tree_vec (len);
1464 parmnode = parmlist;
1467 flags = LOOKUP_NORMAL;
1469 /* Don't bother looking up the same type twice. */
1470 if (candidates && candidates->fn == totype)
1473 for (i = 0; i < len; ++i)
1475 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1476 tree argtype = lvalue_type (arg);
1480 t = implicit_conversion (totype, argtype, arg, flags);
1481 else if (parmnode == void_list_node)
1484 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1487 t = build1 (IDENTITY_CONV, argtype, arg);
1488 ICS_ELLIPSIS_FLAG (t) = 1;
1491 TREE_VEC_ELT (convs, i) = t;
1495 if (ICS_BAD_FLAG (t))
1502 parmnode = TREE_CHAIN (parmnode);
1503 argnode = TREE_CHAIN (argnode);
1509 if (!sufficient_parms_p (parmnode))
1512 return add_candidate (candidates, totype, convs, viable);
1515 static struct z_candidate *
1516 build_builtin_candidate (candidates, fnname, type1, type2,
1517 args, argtypes, flags)
1518 struct z_candidate *candidates;
1519 tree fnname, type1, type2, *args, *argtypes;
1530 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1532 for (i = 0; i < 2; ++i)
1537 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1541 /* We need something for printing the candidate. */
1542 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1544 else if (ICS_BAD_FLAG (t))
1546 TREE_VEC_ELT (convs, i) = t;
1549 /* For COND_EXPR we rearranged the arguments; undo that now. */
1552 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1553 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1554 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1556 TREE_VEC_ELT (convs, 0) = t;
1561 return add_candidate (candidates, fnname, convs, viable);
1568 return COMPLETE_TYPE_P (complete_type (t));
1571 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1574 promoted_arithmetic_type_p (type)
1579 In this section, the term promoted integral type is used to refer
1580 to those integral types which are preserved by integral promotion
1581 (including e.g. int and long but excluding e.g. char).
1582 Similarly, the term promoted arithmetic type refers to promoted
1583 integral types plus floating types. */
1584 return ((INTEGRAL_TYPE_P (type)
1585 && same_type_p (type_promotes_to (type), type))
1586 || TREE_CODE (type) == REAL_TYPE);
1589 /* Create any builtin operator overload candidates for the operator in
1590 question given the converted operand types TYPE1 and TYPE2. The other
1591 args are passed through from add_builtin_candidates to
1592 build_builtin_candidate.
1594 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1595 If CODE is requires candidates operands of the same type of the kind
1596 of which TYPE1 and TYPE2 are, we add both candidates
1597 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1599 static struct z_candidate *
1600 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1601 args, argtypes, flags)
1602 struct z_candidate *candidates;
1603 enum tree_code code, code2;
1604 tree fnname, type1, type2, *args, *argtypes;
1609 case POSTINCREMENT_EXPR:
1610 case POSTDECREMENT_EXPR:
1611 args[1] = integer_zero_node;
1612 type2 = integer_type_node;
1621 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1622 and VQ is either volatile or empty, there exist candidate operator
1623 functions of the form
1624 VQ T& operator++(VQ T&);
1625 T operator++(VQ T&, int);
1626 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1627 type other than bool, and VQ is either volatile or empty, there exist
1628 candidate operator functions of the form
1629 VQ T& operator--(VQ T&);
1630 T operator--(VQ T&, int);
1631 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1632 complete object type, and VQ is either volatile or empty, there exist
1633 candidate operator functions of the form
1634 T*VQ& operator++(T*VQ&);
1635 T*VQ& operator--(T*VQ&);
1636 T* operator++(T*VQ&, int);
1637 T* operator--(T*VQ&, int); */
1639 case POSTDECREMENT_EXPR:
1640 case PREDECREMENT_EXPR:
1641 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1643 case POSTINCREMENT_EXPR:
1644 case PREINCREMENT_EXPR:
1645 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1647 type1 = build_reference_type (type1);
1652 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1653 exist candidate operator functions of the form
1657 8 For every function type T, there exist candidate operator functions of
1659 T& operator*(T*); */
1662 if (TREE_CODE (type1) == POINTER_TYPE
1663 && (TYPE_PTROB_P (type1)
1664 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1668 /* 9 For every type T, there exist candidate operator functions of the form
1671 10For every promoted arithmetic type T, there exist candidate operator
1672 functions of the form
1676 case CONVERT_EXPR: /* unary + */
1677 if (TREE_CODE (type1) == POINTER_TYPE
1678 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1681 if (ARITHMETIC_TYPE_P (type1))
1685 /* 11For every promoted integral type T, there exist candidate operator
1686 functions of the form
1690 if (INTEGRAL_TYPE_P (type1))
1694 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1695 is the same type as C2 or is a derived class of C2, T is a complete
1696 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1697 there exist candidate operator functions of the form
1698 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1699 where CV12 is the union of CV1 and CV2. */
1702 if (TREE_CODE (type1) == POINTER_TYPE
1703 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1705 tree c1 = TREE_TYPE (type1);
1706 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1707 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1708 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1710 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1711 && (TYPE_PTRMEMFUNC_P (type2)
1712 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1717 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1718 didate operator functions of the form
1723 bool operator<(L, R);
1724 bool operator>(L, R);
1725 bool operator<=(L, R);
1726 bool operator>=(L, R);
1727 bool operator==(L, R);
1728 bool operator!=(L, R);
1729 where LR is the result of the usual arithmetic conversions between
1732 14For every pair of types T and I, where T is a cv-qualified or cv-
1733 unqualified complete object type and I is a promoted integral type,
1734 there exist candidate operator functions of the form
1735 T* operator+(T*, I);
1736 T& operator[](T*, I);
1737 T* operator-(T*, I);
1738 T* operator+(I, T*);
1739 T& operator[](I, T*);
1741 15For every T, where T is a pointer to complete object type, there exist
1742 candidate operator functions of the form112)
1743 ptrdiff_t operator-(T, T);
1745 16For every pointer or enumeration type T, there exist candidate operator
1746 functions of the form
1747 bool operator<(T, T);
1748 bool operator>(T, T);
1749 bool operator<=(T, T);
1750 bool operator>=(T, T);
1751 bool operator==(T, T);
1752 bool operator!=(T, T);
1754 17For every pointer to member type T, there exist candidate operator
1755 functions of the form
1756 bool operator==(T, T);
1757 bool operator!=(T, T); */
1760 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1762 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1764 type2 = ptrdiff_type_node;
1768 case TRUNC_DIV_EXPR:
1769 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1775 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1776 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1778 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1779 && null_ptr_cst_p (args[1]))
1784 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1785 && null_ptr_cst_p (args[0]))
1797 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1799 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1801 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1803 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1808 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1816 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1819 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1821 type1 = ptrdiff_type_node;
1824 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1826 type2 = ptrdiff_type_node;
1831 /* 18For every pair of promoted integral types L and R, there exist candi-
1832 date operator functions of the form
1839 where LR is the result of the usual arithmetic conversions between
1842 case TRUNC_MOD_EXPR:
1848 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1852 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1853 type, VQ is either volatile or empty, and R is a promoted arithmetic
1854 type, there exist candidate operator functions of the form
1855 VQ L& operator=(VQ L&, R);
1856 VQ L& operator*=(VQ L&, R);
1857 VQ L& operator/=(VQ L&, R);
1858 VQ L& operator+=(VQ L&, R);
1859 VQ L& operator-=(VQ L&, R);
1861 20For every pair T, VQ), where T is any type and VQ is either volatile
1862 or empty, there exist candidate operator functions of the form
1863 T*VQ& operator=(T*VQ&, T*);
1865 21For every pair T, VQ), where T is a pointer to member type and VQ is
1866 either volatile or empty, there exist candidate operator functions of
1868 VQ T& operator=(VQ T&, T);
1870 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1871 unqualified complete object type, VQ is either volatile or empty, and
1872 I is a promoted integral type, there exist candidate operator func-
1874 T*VQ& operator+=(T*VQ&, I);
1875 T*VQ& operator-=(T*VQ&, I);
1877 23For every triple L, VQ, R), where L is an integral or enumeration
1878 type, VQ is either volatile or empty, and R is a promoted integral
1879 type, there exist candidate operator functions of the form
1881 VQ L& operator%=(VQ L&, R);
1882 VQ L& operator<<=(VQ L&, R);
1883 VQ L& operator>>=(VQ L&, R);
1884 VQ L& operator&=(VQ L&, R);
1885 VQ L& operator^=(VQ L&, R);
1886 VQ L& operator|=(VQ L&, R); */
1893 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1895 type2 = ptrdiff_type_node;
1899 case TRUNC_DIV_EXPR:
1900 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1904 case TRUNC_MOD_EXPR:
1910 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1915 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1917 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1918 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1919 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1920 || ((TYPE_PTRMEMFUNC_P (type1)
1921 || TREE_CODE (type1) == POINTER_TYPE)
1922 && null_ptr_cst_p (args[1])))
1930 my_friendly_abort (367);
1932 type1 = build_reference_type (type1);
1938 For every pair of promoted arithmetic types L and R, there
1939 exist candidate operator functions of the form
1941 LR operator?(bool, L, R);
1943 where LR is the result of the usual arithmetic conversions
1944 between types L and R.
1946 For every type T, where T is a pointer or pointer-to-member
1947 type, there exist candidate operator functions of the form T
1948 operator?(bool, T, T); */
1950 if (promoted_arithmetic_type_p (type1)
1951 && promoted_arithmetic_type_p (type2))
1955 /* Otherwise, the types should be pointers. */
1956 if (!(TREE_CODE (type1) == POINTER_TYPE
1957 || TYPE_PTRMEM_P (type1)
1958 || TYPE_PTRMEMFUNC_P (type1))
1959 || !(TREE_CODE (type2) == POINTER_TYPE
1960 || TYPE_PTRMEM_P (type2)
1961 || TYPE_PTRMEMFUNC_P (type2)))
1964 /* We don't check that the two types are the same; the logic
1965 below will actually create two candidates; one in which both
1966 parameter types are TYPE1, and one in which both parameter
1970 /* These arguments do not make for a legal overloaded operator. */
1974 my_friendly_abort (367);
1977 /* If we're dealing with two pointer types or two enumeral types,
1978 we need candidates for both of them. */
1979 if (type2 && !same_type_p (type1, type2)
1980 && TREE_CODE (type1) == TREE_CODE (type2)
1981 && (TREE_CODE (type1) == REFERENCE_TYPE
1982 || (TREE_CODE (type1) == POINTER_TYPE
1983 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1984 || TYPE_PTRMEMFUNC_P (type1)
1985 || IS_AGGR_TYPE (type1)
1986 || TREE_CODE (type1) == ENUMERAL_TYPE))
1988 candidates = build_builtin_candidate
1989 (candidates, fnname, type1, type1, args, argtypes, flags);
1990 return build_builtin_candidate
1991 (candidates, fnname, type2, type2, args, argtypes, flags);
1994 return build_builtin_candidate
1995 (candidates, fnname, type1, type2, args, argtypes, flags);
1999 type_decays_to (type)
2002 if (TREE_CODE (type) == ARRAY_TYPE)
2003 return build_pointer_type (TREE_TYPE (type));
2004 if (TREE_CODE (type) == FUNCTION_TYPE)
2005 return build_pointer_type (type);
2009 /* There are three conditions of builtin candidates:
2011 1) bool-taking candidates. These are the same regardless of the input.
2012 2) pointer-pair taking candidates. These are generated for each type
2013 one of the input types converts to.
2014 3) arithmetic candidates. According to the standard, we should generate
2015 all of these, but I'm trying not to...
2017 Here we generate a superset of the possible candidates for this particular
2018 case. That is a subset of the full set the standard defines, plus some
2019 other cases which the standard disallows. add_builtin_candidate will
2020 filter out the illegal set. */
2022 static struct z_candidate *
2023 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2024 struct z_candidate *candidates;
2025 enum tree_code code, code2;
2031 tree type, argtypes[3];
2032 /* TYPES[i] is the set of possible builtin-operator parameter types
2033 we will consider for the Ith argument. These are represented as
2034 a TREE_LIST; the TREE_VALUE of each node is the potential
2038 for (i = 0; i < 3; ++i)
2041 argtypes[i] = lvalue_type (args[i]);
2043 argtypes[i] = NULL_TREE;
2048 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2049 and VQ is either volatile or empty, there exist candidate operator
2050 functions of the form
2051 VQ T& operator++(VQ T&); */
2053 case POSTINCREMENT_EXPR:
2054 case PREINCREMENT_EXPR:
2055 case POSTDECREMENT_EXPR:
2056 case PREDECREMENT_EXPR:
2061 /* 24There also exist candidate operator functions of the form
2062 bool operator!(bool);
2063 bool operator&&(bool, bool);
2064 bool operator||(bool, bool); */
2066 case TRUTH_NOT_EXPR:
2067 return build_builtin_candidate
2068 (candidates, fnname, boolean_type_node,
2069 NULL_TREE, args, argtypes, flags);
2071 case TRUTH_ORIF_EXPR:
2072 case TRUTH_ANDIF_EXPR:
2073 return build_builtin_candidate
2074 (candidates, fnname, boolean_type_node,
2075 boolean_type_node, args, argtypes, flags);
2096 types[0] = types[1] = NULL_TREE;
2098 for (i = 0; i < 2; ++i)
2102 else if (IS_AGGR_TYPE (argtypes[i]))
2106 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2109 convs = lookup_conversions (argtypes[i]);
2111 if (code == COND_EXPR)
2113 if (real_lvalue_p (args[i]))
2114 types[i] = tree_cons
2115 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2117 types[i] = tree_cons
2118 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2124 for (; convs; convs = TREE_CHAIN (convs))
2126 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2129 && (TREE_CODE (type) != REFERENCE_TYPE
2130 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2133 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2134 types[i] = tree_cons (NULL_TREE, type, types[i]);
2136 type = non_reference (type);
2137 if (i != 0 || ! ref1)
2139 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2140 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2141 types[i] = tree_cons (NULL_TREE, type, types[i]);
2142 if (INTEGRAL_TYPE_P (type))
2143 type = type_promotes_to (type);
2146 if (! value_member (type, types[i]))
2147 types[i] = tree_cons (NULL_TREE, type, types[i]);
2152 if (code == COND_EXPR && real_lvalue_p (args[i]))
2153 types[i] = tree_cons
2154 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2155 type = non_reference (argtypes[i]);
2156 if (i != 0 || ! ref1)
2158 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2159 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2160 types[i] = tree_cons (NULL_TREE, type, types[i]);
2161 if (INTEGRAL_TYPE_P (type))
2162 type = type_promotes_to (type);
2164 types[i] = tree_cons (NULL_TREE, type, types[i]);
2168 /* Run through the possible parameter types of both arguments,
2169 creating candidates with those parameter types. */
2170 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2173 for (type = types[1]; type; type = TREE_CHAIN (type))
2174 candidates = add_builtin_candidate
2175 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2176 TREE_VALUE (type), args, argtypes, flags);
2178 candidates = add_builtin_candidate
2179 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2180 NULL_TREE, args, argtypes, flags);
2187 /* If TMPL can be successfully instantiated as indicated by
2188 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2190 TMPL is the template. EXPLICIT_TARGS are any explicit template
2191 arguments. ARGLIST is the arguments provided at the call-site.
2192 The RETURN_TYPE is the desired type for conversion operators. If
2193 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2194 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2195 add_conv_candidate. */
2197 static struct z_candidate*
2198 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2199 arglist, return_type, flags,
2201 struct z_candidate *candidates;
2202 tree tmpl, ctype, explicit_targs, arglist, return_type;
2205 unification_kind_t strict;
2207 int ntparms = DECL_NTPARMS (tmpl);
2208 tree targs = make_tree_vec (ntparms);
2209 tree args_without_in_chrg = arglist;
2210 struct z_candidate *cand;
2214 /* We don't do deduction on the in-charge parameter, the VTT
2215 parameter or 'this'. */
2216 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2217 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2219 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2220 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2221 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2222 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2224 i = fn_type_unification (tmpl, explicit_targs, targs,
2225 args_without_in_chrg,
2226 return_type, strict, -1);
2231 fn = instantiate_template (tmpl, targs);
2232 if (fn == error_mark_node)
2235 if (obj != NULL_TREE)
2236 /* Aha, this is a conversion function. */
2237 cand = add_conv_candidate (candidates, fn, obj, arglist);
2239 cand = add_function_candidate (candidates, fn, ctype,
2241 if (DECL_TI_TEMPLATE (fn) != tmpl)
2242 /* This situation can occur if a member template of a template
2243 class is specialized. Then, instantiate_template might return
2244 an instantiation of the specialization, in which case the
2245 DECL_TI_TEMPLATE field will point at the original
2246 specialization. For example:
2248 template <class T> struct S { template <class U> void f(U);
2249 template <> void f(int) {}; };
2253 Here, TMPL will be template <class U> S<double>::f(U).
2254 And, instantiate template will give us the specialization
2255 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2256 for this will point at template <class T> template <> S<T>::f(int),
2257 so that we can find the definition. For the purposes of
2258 overload resolution, however, we want the original TMPL. */
2259 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2261 cand->template = DECL_TEMPLATE_INFO (fn);
2267 static struct z_candidate *
2268 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2269 arglist, return_type, flags, strict)
2270 struct z_candidate *candidates;
2271 tree tmpl, ctype, explicit_targs, arglist, return_type;
2273 unification_kind_t strict;
2276 add_template_candidate_real (candidates, tmpl, ctype,
2277 explicit_targs, arglist, return_type, flags,
2282 static struct z_candidate *
2283 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2284 struct z_candidate *candidates;
2285 tree tmpl, obj, arglist, return_type;
2288 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2289 arglist, return_type, 0, obj, DEDUCE_CONV);
2295 struct z_candidate *cands;
2297 for (; cands; cands = cands->next)
2298 if (pedantic ? cands->viable == 1 : cands->viable)
2303 static struct z_candidate *
2304 splice_viable (cands)
2305 struct z_candidate *cands;
2307 struct z_candidate **p = &cands;
2311 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2324 /* Fix this to work on non-lvalues. */
2325 return build_unary_op (ADDR_EXPR, obj, 0);
2329 print_z_candidates (candidates)
2330 struct z_candidate *candidates;
2332 const char *str = "candidates are:";
2333 for (; candidates; candidates = candidates->next)
2335 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2337 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2338 cp_error ("%s %D(%T, %T, %T) <builtin>", str, candidates->fn,
2339 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2340 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2341 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2342 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2343 cp_error ("%s %D(%T, %T) <builtin>", str, candidates->fn,
2344 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2345 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2347 cp_error ("%s %D(%T) <builtin>", str, candidates->fn,
2348 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2350 else if (TYPE_P (candidates->fn))
2351 cp_error ("%s %T <conversion>", str, candidates->fn);
2353 cp_error_at ("%s %+#D%s", str, candidates->fn,
2354 candidates->viable == -1 ? " <near match>" : "");
2359 /* Returns the best overload candidate to perform the requested
2360 conversion. This function is used for three the overloading situations
2361 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2362 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2363 per [dcl.init.ref], so we ignore temporary bindings. */
2365 static struct z_candidate *
2366 build_user_type_conversion_1 (totype, expr, flags)
2370 struct z_candidate *candidates, *cand;
2371 tree fromtype = TREE_TYPE (expr);
2372 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2373 tree args = NULL_TREE;
2374 tree templates = NULL_TREE;
2376 /* We represent conversion within a hierarchy using RVALUE_CONV and
2377 BASE_CONV, as specified by [over.best.ics]; these become plain
2378 constructor calls, as specified in [dcl.init]. */
2379 if (IS_AGGR_TYPE (fromtype) && IS_AGGR_TYPE (totype)
2380 && DERIVED_FROM_P (totype, fromtype))
2383 if (IS_AGGR_TYPE (totype))
2384 ctors = lookup_fnfields (TYPE_BINFO (totype),
2385 complete_ctor_identifier,
2388 if (IS_AGGR_TYPE (fromtype))
2389 convs = lookup_conversions (fromtype);
2392 flags |= LOOKUP_NO_CONVERSION;
2398 ctors = TREE_VALUE (ctors);
2400 t = build_int_2 (0, 0);
2401 TREE_TYPE (t) = build_pointer_type (totype);
2402 args = build_tree_list (NULL_TREE, expr);
2403 if (DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2404 || DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)))
2405 /* We should never try to call the abstract or base constructor
2408 args = tree_cons (NULL_TREE, t, args);
2410 for (; ctors; ctors = OVL_NEXT (ctors))
2412 tree ctor = OVL_CURRENT (ctors);
2413 if (DECL_NONCONVERTING_P (ctor))
2416 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2418 templates = tree_cons (NULL_TREE, ctor, templates);
2420 add_template_candidate (candidates, ctor, totype,
2421 NULL_TREE, args, NULL_TREE, flags,
2425 candidates = add_function_candidate (candidates, ctor, totype,
2430 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2431 candidates->basetype_path = TYPE_BINFO (totype);
2436 args = build_tree_list (NULL_TREE, build_this (expr));
2438 for (; convs; convs = TREE_CHAIN (convs))
2440 tree fns = TREE_VALUE (convs);
2441 int convflags = LOOKUP_NO_CONVERSION;
2444 /* If we are called to convert to a reference type, we are trying to
2445 find an lvalue binding, so don't even consider temporaries. If
2446 we don't find an lvalue binding, the caller will try again to
2447 look for a temporary binding. */
2448 if (TREE_CODE (totype) == REFERENCE_TYPE)
2449 convflags |= LOOKUP_NO_TEMP_BIND;
2451 if (TREE_CODE (OVL_CURRENT (fns)) != TEMPLATE_DECL)
2452 ics = implicit_conversion
2453 (totype, TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns))), 0, convflags);
2455 /* We can't compute this yet. */
2456 ics = error_mark_node;
2458 if (TREE_CODE (totype) == REFERENCE_TYPE && ics && ICS_BAD_FLAG (ics))
2459 /* ignore the near match. */;
2461 for (; fns; fns = OVL_NEXT (fns))
2463 tree fn = OVL_CURRENT (fns);
2464 struct z_candidate *old_candidates = candidates;
2466 /* [over.match.funcs] For conversion functions, the function is
2467 considered to be a member of the class of the implicit object
2468 argument for the purpose of defining the type of the implicit
2471 So we pass fromtype as CTYPE to add_*_candidate. */
2473 if (TREE_CODE (fn) == TEMPLATE_DECL)
2475 templates = tree_cons (NULL_TREE, fn, templates);
2477 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2478 args, totype, flags,
2482 candidates = add_function_candidate (candidates, fn, fromtype,
2485 if (candidates != old_candidates)
2487 if (TREE_CODE (fn) == TEMPLATE_DECL)
2488 ics = implicit_conversion
2489 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2492 candidates->second_conv = ics;
2493 candidates->basetype_path = TYPE_BINFO (fromtype);
2495 if (ics == NULL_TREE)
2496 candidates->viable = 0;
2497 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2498 candidates->viable = -1;
2503 if (! any_viable (candidates))
2506 if (flags & LOOKUP_COMPLAIN)
2508 if (candidates && ! candidates->next)
2509 /* say why this one won't work or try to be loose */;
2511 cp_error ("no viable candidates");
2518 candidates = splice_viable (candidates);
2519 cand = tourney (candidates);
2523 if (flags & LOOKUP_COMPLAIN)
2525 cp_error ("conversion from `%T' to `%T' is ambiguous",
2527 print_z_candidates (candidates);
2530 cand = candidates; /* any one will do */
2531 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2532 ICS_USER_FLAG (cand->second_conv) = 1;
2533 ICS_BAD_FLAG (cand->second_conv) = 1;
2538 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2539 p = &(TREE_OPERAND (*p, 0));
2543 (DECL_CONSTRUCTOR_P (cand->fn)
2544 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2545 expr, build_ptr_wrapper (cand));
2547 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2548 if (cand->viable == -1)
2549 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2555 build_user_type_conversion (totype, expr, flags)
2559 struct z_candidate *cand
2560 = build_user_type_conversion_1 (totype, expr, flags);
2564 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2565 return error_mark_node;
2566 return convert_from_reference (convert_like (cand->second_conv, expr));
2571 /* Do any initial processing on the arguments to a function call. */
2578 for (t = args; t; t = TREE_CHAIN (t))
2580 tree arg = TREE_VALUE (t);
2582 if (arg == error_mark_node)
2583 return error_mark_node;
2584 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2586 error ("invalid use of void expression");
2587 return error_mark_node;
2589 else if (TREE_CODE (arg) == OFFSET_REF)
2590 arg = resolve_offset_ref (arg);
2591 arg = convert_from_reference (arg);
2592 TREE_VALUE (t) = arg;
2598 build_new_function_call (fn, args)
2601 struct z_candidate *candidates = 0, *cand;
2602 tree explicit_targs = NULL_TREE;
2603 int template_only = 0;
2605 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2607 explicit_targs = TREE_OPERAND (fn, 1);
2608 fn = TREE_OPERAND (fn, 0);
2612 if (really_overloaded_fn (fn))
2615 tree templates = NULL_TREE;
2617 args = resolve_args (args);
2619 if (args == error_mark_node)
2620 return error_mark_node;
2622 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2624 tree t = OVL_FUNCTION (t1);
2626 if (TREE_CODE (t) == TEMPLATE_DECL)
2628 templates = tree_cons (NULL_TREE, t, templates);
2629 candidates = add_template_candidate
2630 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2631 LOOKUP_NORMAL, DEDUCE_CALL);
2633 else if (! template_only)
2634 candidates = add_function_candidate
2635 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2638 if (! any_viable (candidates))
2640 if (candidates && ! candidates->next)
2641 return build_function_call (candidates->fn, args);
2642 cp_error ("no matching function for call to `%D(%A)'",
2643 DECL_NAME (OVL_FUNCTION (fn)), args);
2645 print_z_candidates (candidates);
2646 return error_mark_node;
2648 candidates = splice_viable (candidates);
2649 cand = tourney (candidates);
2653 cp_error ("call of overloaded `%D(%A)' is ambiguous",
2654 DECL_NAME (OVL_FUNCTION (fn)), args);
2655 print_z_candidates (candidates);
2656 return error_mark_node;
2659 return build_over_call (cand, args, LOOKUP_NORMAL);
2662 /* This is not really overloaded. */
2663 fn = OVL_CURRENT (fn);
2665 return build_function_call (fn, args);
2669 build_object_call (obj, args)
2672 struct z_candidate *candidates = 0, *cand;
2673 tree fns, convs, mem_args = NULL_TREE;
2674 tree type = TREE_TYPE (obj);
2676 if (TYPE_PTRMEMFUNC_P (type))
2678 /* It's no good looking for an overloaded operator() on a
2679 pointer-to-member-function. */
2680 cp_error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2681 return error_mark_node;
2684 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2685 if (fns == error_mark_node)
2686 return error_mark_node;
2688 args = resolve_args (args);
2690 if (args == error_mark_node)
2691 return error_mark_node;
2695 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2696 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2698 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2700 tree fn = OVL_CURRENT (fns);
2701 if (TREE_CODE (fn) == TEMPLATE_DECL)
2704 = add_template_candidate (candidates, fn, base, NULL_TREE,
2705 mem_args, NULL_TREE,
2706 LOOKUP_NORMAL, DEDUCE_CALL);
2709 candidates = add_function_candidate
2710 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2713 candidates->basetype_path = TYPE_BINFO (type);
2717 convs = lookup_conversions (type);
2719 for (; convs; convs = TREE_CHAIN (convs))
2721 tree fns = TREE_VALUE (convs);
2722 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2724 if ((TREE_CODE (totype) == POINTER_TYPE
2725 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2726 || (TREE_CODE (totype) == REFERENCE_TYPE
2727 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2728 || (TREE_CODE (totype) == REFERENCE_TYPE
2729 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2730 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2731 for (; fns; fns = OVL_NEXT (fns))
2733 tree fn = OVL_CURRENT (fns);
2734 if (TREE_CODE (fn) == TEMPLATE_DECL)
2736 candidates = add_template_conv_candidate (candidates,
2743 candidates = add_conv_candidate (candidates, fn, obj, args);
2747 if (! any_viable (candidates))
2749 cp_error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2750 print_z_candidates (candidates);
2751 return error_mark_node;
2754 candidates = splice_viable (candidates);
2755 cand = tourney (candidates);
2759 cp_error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2760 print_z_candidates (candidates);
2761 return error_mark_node;
2764 /* Since cand->fn will be a type, not a function, for a conversion
2765 function, we must be careful not to unconditionally look at
2767 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2768 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2769 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2771 obj = convert_like_with_context
2772 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2775 return build_function_call (obj, args);
2779 op_error (code, code2, arg1, arg2, arg3, problem)
2780 enum tree_code code, code2;
2781 tree arg1, arg2, arg3;
2782 const char *problem;
2786 if (code == MODIFY_EXPR)
2787 opname = assignment_operator_name_info[code2].name;
2789 opname = operator_name_info[code].name;
2794 cp_error ("%s for `%T ? %T : %T' operator", problem,
2795 error_type (arg1), error_type (arg2), error_type (arg3));
2797 case POSTINCREMENT_EXPR:
2798 case POSTDECREMENT_EXPR:
2799 cp_error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2802 cp_error ("%s for `%T [%T]' operator", problem,
2803 error_type (arg1), error_type (arg2));
2807 cp_error ("%s for `%T %s %T' operator", problem,
2808 error_type (arg1), opname, error_type (arg2));
2810 cp_error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2814 /* Return the implicit conversion sequence that could be used to
2815 convert E1 to E2 in [expr.cond]. */
2818 conditional_conversion (e1, e2)
2822 tree t1 = non_reference (TREE_TYPE (e1));
2823 tree t2 = non_reference (TREE_TYPE (e2));
2828 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2829 implicitly converted (clause _conv_) to the type "reference to
2830 T2", subject to the constraint that in the conversion the
2831 reference must bind directly (_dcl.init.ref_) to E1. */
2832 if (real_lvalue_p (e2))
2834 conv = implicit_conversion (build_reference_type (t2),
2837 LOOKUP_NO_TEMP_BIND);
2844 If E1 and E2 have class type, and the underlying class types are
2845 the same or one is a base class of the other: E1 can be converted
2846 to match E2 if the class of T2 is the same type as, or a base
2847 class of, the class of T1, and the cv-qualification of T2 is the
2848 same cv-qualification as, or a greater cv-qualification than, the
2849 cv-qualification of T1. If the conversion is applied, E1 is
2850 changed to an rvalue of type T2 that still refers to the original
2851 source class object (or the appropriate subobject thereof). */
2852 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2853 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2854 TYPE_MAIN_VARIANT (t1)))
2856 if (at_least_as_qualified_p (t2, t1))
2858 conv = build1 (IDENTITY_CONV, t1, e1);
2859 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2860 TYPE_MAIN_VARIANT (t2)))
2861 conv = build_conv (BASE_CONV, t2, conv);
2870 E1 can be converted to match E2 if E1 can be implicitly converted
2871 to the type that expression E2 would have if E2 were converted to
2872 an rvalue (or the type it has, if E2 is an rvalue). */
2873 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2876 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2877 arguments to the conditional expression. By the time this function
2878 is called, any suitable candidate functions are included in
2882 build_conditional_expr (arg1, arg2, arg3)
2890 tree result_type = NULL_TREE;
2892 struct z_candidate *candidates = 0;
2893 struct z_candidate *cand;
2895 /* As a G++ extension, the second argument to the conditional can be
2896 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2897 c'.) If the second operand is omitted, make sure it is
2898 calculated only once. */
2902 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2903 arg1 = arg2 = save_expr (arg1);
2908 The first expr ession is implicitly converted to bool (clause
2910 arg1 = cp_convert (boolean_type_node, arg1);
2912 /* If something has already gone wrong, just pass that fact up the
2914 if (arg1 == error_mark_node
2915 || arg2 == error_mark_node
2916 || arg3 == error_mark_node
2917 || TREE_TYPE (arg1) == error_mark_node
2918 || TREE_TYPE (arg2) == error_mark_node
2919 || TREE_TYPE (arg3) == error_mark_node)
2920 return error_mark_node;
2924 If either the second or the third operand has type (possibly
2925 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2926 array-to-pointer (_conv.array_), and function-to-pointer
2927 (_conv.func_) standard conversions are performed on the second
2928 and third operands. */
2929 arg2_type = TREE_TYPE (arg2);
2930 arg3_type = TREE_TYPE (arg3);
2931 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2933 /* Do the conversions. We don't these for `void' type arguments
2934 since it can't have any effect and since decay_conversion
2935 does not handle that case gracefully. */
2936 if (!VOID_TYPE_P (arg2_type))
2937 arg2 = decay_conversion (arg2);
2938 if (!VOID_TYPE_P (arg3_type))
2939 arg3 = decay_conversion (arg3);
2940 arg2_type = TREE_TYPE (arg2);
2941 arg3_type = TREE_TYPE (arg3);
2945 One of the following shall hold:
2947 --The second or the third operand (but not both) is a
2948 throw-expression (_except.throw_); the result is of the
2949 type of the other and is an rvalue.
2951 --Both the second and the third operands have type void; the
2952 result is of type void and is an rvalue. */
2953 if ((TREE_CODE (arg2) == THROW_EXPR)
2954 ^ (TREE_CODE (arg3) == THROW_EXPR))
2955 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2956 ? arg3_type : arg2_type);
2957 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2958 result_type = void_type_node;
2961 cp_error ("`%E' has type `void' and is not a throw-expression",
2962 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2963 return error_mark_node;
2967 goto valid_operands;
2971 Otherwise, if the second and third operand have different types,
2972 and either has (possibly cv-qualified) class type, an attempt is
2973 made to convert each of those operands to the type of the other. */
2974 else if (!same_type_p (arg2_type, arg3_type)
2975 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2977 tree conv2 = conditional_conversion (arg2, arg3);
2978 tree conv3 = conditional_conversion (arg3, arg2);
2982 If both can be converted, or one can be converted but the
2983 conversion is ambiguous, the program is ill-formed. If
2984 neither can be converted, the operands are left unchanged and
2985 further checking is performed as described below. If exactly
2986 one conversion is possible, that conversion is applied to the
2987 chosen operand and the converted operand is used in place of
2988 the original operand for the remainder of this section. */
2989 if ((conv2 && !ICS_BAD_FLAG (conv2)
2990 && conv3 && !ICS_BAD_FLAG (conv3))
2991 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
2992 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
2994 cp_error ("operands to ?: have different types");
2995 return error_mark_node;
2997 else if (conv2 && !ICS_BAD_FLAG (conv2))
2999 arg2 = convert_like (conv2, arg2);
3000 arg2 = convert_from_reference (arg2);
3001 /* That may not quite have done the trick. If the two types
3002 are cv-qualified variants of one another, we will have
3003 just used an IDENTITY_CONV. (There's no conversion from
3004 an lvalue of one class type to an lvalue of another type,
3005 even a cv-qualified variant, and we don't want to lose
3006 lvalue-ness here.) So, we manually add a NOP_EXPR here
3008 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3009 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3010 arg2_type = TREE_TYPE (arg2);
3012 else if (conv3 && !ICS_BAD_FLAG (conv3))
3014 arg3 = convert_like (conv3, arg3);
3015 arg3 = convert_from_reference (arg3);
3016 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3017 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3018 arg3_type = TREE_TYPE (arg3);
3024 If the second and third operands are lvalues and have the same
3025 type, the result is of that type and is an lvalue. */
3026 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3027 same_type_p (arg2_type, arg3_type))
3029 result_type = arg2_type;
3030 goto valid_operands;
3035 Otherwise, the result is an rvalue. If the second and third
3036 operand do not have the same type, and either has (possibly
3037 cv-qualified) class type, overload resolution is used to
3038 determine the conversions (if any) to be applied to the operands
3039 (_over.match.oper_, _over.built_). */
3041 if (!same_type_p (arg2_type, arg3_type)
3042 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3047 /* Rearrange the arguments so that add_builtin_candidate only has
3048 to know about two args. In build_builtin_candidates, the
3049 arguments are unscrambled. */
3053 candidates = add_builtin_candidates (candidates,
3056 ansi_opname (COND_EXPR),
3062 If the overload resolution fails, the program is
3064 if (!any_viable (candidates))
3066 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3067 print_z_candidates (candidates);
3068 return error_mark_node;
3070 candidates = splice_viable (candidates);
3071 cand = tourney (candidates);
3074 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3075 print_z_candidates (candidates);
3076 return error_mark_node;
3081 Otherwise, the conversions thus determined are applied, and
3082 the converted operands are used in place of the original
3083 operands for the remainder of this section. */
3084 conv = TREE_VEC_ELT (cand->convs, 0);
3085 arg1 = convert_like (conv, arg1);
3086 conv = TREE_VEC_ELT (cand->convs, 1);
3087 arg2 = convert_like (conv, arg2);
3088 conv = TREE_VEC_ELT (cand->convs, 2);
3089 arg3 = convert_like (conv, arg3);
3094 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3095 and function-to-pointer (_conv.func_) standard conversions are
3096 performed on the second and third operands.
3098 We need to force the lvalue-to-rvalue conversion here for class types,
3099 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3100 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3103 We use ocp_convert rather than build_user_type_conversion because the
3104 latter returns NULL_TREE on failure, while the former gives an error. */
3106 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3107 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3108 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3110 arg2 = decay_conversion (arg2);
3111 arg2_type = TREE_TYPE (arg2);
3113 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3114 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3115 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3117 arg3 = decay_conversion (arg3);
3118 arg3_type = TREE_TYPE (arg3);
3120 if (arg2 == error_mark_node || arg3 == error_mark_node)
3121 return error_mark_node;
3125 After those conversions, one of the following shall hold:
3127 --The second and third operands have the same type; the result is of
3129 if (same_type_p (arg2_type, arg3_type))
3130 result_type = arg2_type;
3133 --The second and third operands have arithmetic or enumeration
3134 type; the usual arithmetic conversions are performed to bring
3135 them to a common type, and the result is of that type. */
3136 else if ((ARITHMETIC_TYPE_P (arg2_type)
3137 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3138 && (ARITHMETIC_TYPE_P (arg3_type)
3139 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3141 /* In this case, there is always a common type. */
3142 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3145 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3146 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3147 cp_warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3148 arg2_type, arg3_type);
3149 else if (extra_warnings
3150 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3151 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3152 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3153 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3154 cp_warning ("enumeral and non-enumeral type in conditional expression");
3156 arg2 = perform_implicit_conversion (result_type, arg2);
3157 arg3 = perform_implicit_conversion (result_type, arg3);
3161 --The second and third operands have pointer type, or one has
3162 pointer type and the other is a null pointer constant; pointer
3163 conversions (_conv.ptr_) and qualification conversions
3164 (_conv.qual_) are performed to bring them to their composite
3165 pointer type (_expr.rel_). The result is of the composite
3168 --The second and third operands have pointer to member type, or
3169 one has pointer to member type and the other is a null pointer
3170 constant; pointer to member conversions (_conv.mem_) and
3171 qualification conversions (_conv.qual_) are performed to bring
3172 them to a common type, whose cv-qualification shall match the
3173 cv-qualification of either the second or the third operand.
3174 The result is of the common type. */
3175 else if ((null_ptr_cst_p (arg2)
3176 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3177 || TYPE_PTRMEMFUNC_P (arg3_type)))
3178 || (null_ptr_cst_p (arg3)
3179 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3180 || TYPE_PTRMEMFUNC_P (arg2_type)))
3181 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3182 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3183 || (TYPE_PTRMEMFUNC_P (arg2_type)
3184 && TYPE_PTRMEMFUNC_P (arg3_type)))
3186 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3187 arg3, "conditional expression");
3188 arg2 = perform_implicit_conversion (result_type, arg2);
3189 arg3 = perform_implicit_conversion (result_type, arg3);
3194 cp_error ("operands to ?: have different types");
3195 return error_mark_node;
3199 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3200 /* Expand both sides into the same slot, hopefully the target of the
3201 ?: expression. We used to check for TARGET_EXPRs here, but now we
3202 sometimes wrap them in NOP_EXPRs so the test would fail. */
3203 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3204 result = build_target_expr_with_type (result, result_type);
3206 /* If this expression is an rvalue, but might be mistaken for an
3207 lvalue, we must add a NON_LVALUE_EXPR. */
3208 if (!lvalue_p && real_lvalue_p (result))
3209 result = build1 (NON_LVALUE_EXPR, result_type, result);
3215 build_new_op (code, flags, arg1, arg2, arg3)
3216 enum tree_code code;
3218 tree arg1, arg2, arg3;
3220 struct z_candidate *candidates = 0, *cand;
3221 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3222 enum tree_code code2 = NOP_EXPR;
3223 tree templates = NULL_TREE;
3226 if (arg1 == error_mark_node
3227 || arg2 == error_mark_node
3228 || arg3 == error_mark_node)
3229 return error_mark_node;
3231 /* This can happen if a template takes all non-type parameters, e.g.
3232 undeclared_template<1, 5, 72>a; */
3233 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3235 cp_error ("`%D' must be declared before use", arg1);
3236 return error_mark_node;
3239 if (code == MODIFY_EXPR)
3241 code2 = TREE_CODE (arg3);
3243 fnname = ansi_assopname (code2);
3246 fnname = ansi_opname (code);
3248 if (TREE_CODE (arg1) == OFFSET_REF)
3249 arg1 = resolve_offset_ref (arg1);
3250 arg1 = convert_from_reference (arg1);
3256 case VEC_DELETE_EXPR:
3258 /* Use build_op_new_call and build_op_delete_call instead. */
3259 my_friendly_abort (981018);
3262 return build_object_call (arg1, arg2);
3270 if (TREE_CODE (arg2) == OFFSET_REF)
3271 arg2 = resolve_offset_ref (arg2);
3272 arg2 = convert_from_reference (arg2);
3276 if (TREE_CODE (arg3) == OFFSET_REF)
3277 arg3 = resolve_offset_ref (arg3);
3278 arg3 = convert_from_reference (arg3);
3281 if (code == COND_EXPR)
3283 if (arg2 == NULL_TREE
3284 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3285 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3286 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3287 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3290 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3291 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3294 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3295 arg2 = integer_zero_node;
3298 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3299 (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3301 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3303 arglist = build_tree_list (NULL_TREE, arg1);
3305 fns = lookup_function_nonclass (fnname, arglist);
3307 if (fns && TREE_CODE (fns) == TREE_LIST)
3308 fns = TREE_VALUE (fns);
3309 for (; fns; fns = OVL_NEXT (fns))
3311 tree fn = OVL_CURRENT (fns);
3312 if (TREE_CODE (fn) == TEMPLATE_DECL)
3314 templates = tree_cons (NULL_TREE, fn, templates);
3316 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3317 arglist, TREE_TYPE (fnname),
3318 flags, DEDUCE_CALL);
3321 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3325 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3327 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3328 if (fns == error_mark_node)
3336 tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3337 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3338 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3340 tree fn = OVL_CURRENT (fns);
3343 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3344 this_arglist = mem_arglist;
3346 this_arglist = arglist;
3348 if (TREE_CODE (fn) == TEMPLATE_DECL)
3350 /* A member template. */
3351 templates = tree_cons (NULL_TREE, fn, templates);
3353 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3354 this_arglist, TREE_TYPE (fnname),
3355 flags, DEDUCE_CALL);
3358 candidates = add_function_candidate
3359 (candidates, fn, basetype, this_arglist, flags);
3362 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3369 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3370 to know about two args; a builtin candidate will always have a first
3371 parameter of type bool. We'll handle that in
3372 build_builtin_candidate. */
3373 if (code == COND_EXPR)
3383 args[2] = NULL_TREE;
3386 candidates = add_builtin_candidates
3387 (candidates, code, code2, fnname, args, flags);
3390 if (! any_viable (candidates))
3394 case POSTINCREMENT_EXPR:
3395 case POSTDECREMENT_EXPR:
3396 /* Look for an `operator++ (int)'. If they didn't have
3397 one, then we fall back to the old way of doing things. */
3398 if (flags & LOOKUP_COMPLAIN)
3399 cp_pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3401 operator_name_info[code].name);
3402 if (code == POSTINCREMENT_EXPR)
3403 code = PREINCREMENT_EXPR;
3405 code = PREDECREMENT_EXPR;
3406 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3408 /* The caller will deal with these. */
3417 if (flags & LOOKUP_COMPLAIN)
3419 op_error (code, code2, arg1, arg2, arg3, "no match");
3420 print_z_candidates (candidates);
3422 return error_mark_node;
3424 candidates = splice_viable (candidates);
3425 cand = tourney (candidates);
3429 if (flags & LOOKUP_COMPLAIN)
3431 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3432 print_z_candidates (candidates);
3434 return error_mark_node;
3437 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3439 extern int warn_synth;
3441 && fnname == ansi_assopname (NOP_EXPR)
3442 && DECL_ARTIFICIAL (cand->fn)
3444 && ! candidates->next->next)
3446 cp_warning ("using synthesized `%#D' for copy assignment",
3448 cp_warning_at (" where cfront would use `%#D'",
3450 ? candidates->next->fn
3454 return build_over_call
3456 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3457 ? mem_arglist : arglist,
3461 /* Check for comparison of different enum types. */
3470 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3471 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3472 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3473 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3475 cp_warning ("comparison between `%#T' and `%#T'",
3476 TREE_TYPE (arg1), TREE_TYPE (arg2));
3483 /* We need to strip any leading REF_BIND so that bitfields don't cause
3484 errors. This should not remove any important conversions, because
3485 builtins don't apply to class objects directly. */
3486 conv = TREE_VEC_ELT (cand->convs, 0);
3487 if (TREE_CODE (conv) == REF_BIND)
3488 conv = TREE_OPERAND (conv, 0);
3489 arg1 = convert_like (conv, arg1);
3492 conv = TREE_VEC_ELT (cand->convs, 1);
3493 if (TREE_CODE (conv) == REF_BIND)
3494 conv = TREE_OPERAND (conv, 0);
3495 arg2 = convert_like (conv, arg2);
3499 conv = TREE_VEC_ELT (cand->convs, 2);
3500 if (TREE_CODE (conv) == REF_BIND)
3501 conv = TREE_OPERAND (conv, 0);
3502 arg3 = convert_like (conv, arg3);
3509 return build_modify_expr (arg1, code2, arg2);
3512 return build_indirect_ref (arg1, "unary *");
3517 case TRUNC_DIV_EXPR:
3528 case TRUNC_MOD_EXPR:
3532 case TRUTH_ANDIF_EXPR:
3533 case TRUTH_ORIF_EXPR:
3534 return cp_build_binary_op (code, arg1, arg2);
3539 case TRUTH_NOT_EXPR:
3540 case PREINCREMENT_EXPR:
3541 case POSTINCREMENT_EXPR:
3542 case PREDECREMENT_EXPR:
3543 case POSTDECREMENT_EXPR:
3546 return build_unary_op (code, arg1, candidates != 0);
3549 return build_array_ref (arg1, arg2);
3552 return build_conditional_expr (arg1, arg2, arg3);
3555 return build_m_component_ref
3556 (build_indirect_ref (arg1, NULL), arg2);
3558 /* The caller will deal with these. */
3565 my_friendly_abort (367);
3570 /* Build a call to operator delete. This has to be handled very specially,
3571 because the restrictions on what signatures match are different from all
3572 other call instances. For a normal delete, only a delete taking (void *)
3573 or (void *, size_t) is accepted. For a placement delete, only an exact
3574 match with the placement new is accepted.
3576 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3577 ADDR is the pointer to be deleted. For placement delete, it is also
3578 used to determine what the corresponding new looked like.
3579 SIZE is the size of the memory block to be deleted.
3580 FLAGS are the usual overloading flags.
3581 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3584 build_op_delete_call (code, addr, size, flags, placement)
3585 enum tree_code code;
3586 tree addr, size, placement;
3589 tree fn = NULL_TREE;
3590 tree fns, fnname, fntype, argtypes, args, type;
3593 if (addr == error_mark_node)
3594 return error_mark_node;
3596 type = TREE_TYPE (TREE_TYPE (addr));
3597 while (TREE_CODE (type) == ARRAY_TYPE)
3598 type = TREE_TYPE (type);
3600 fnname = ansi_opname (code);
3602 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3605 If the result of the lookup is ambiguous or inaccessible, or if
3606 the lookup selects a placement deallocation function, the
3607 program is ill-formed.
3609 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3611 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3612 if (fns == error_mark_node)
3613 return error_mark_node;
3618 if (fns == NULL_TREE)
3619 fns = lookup_name_nonclass (fnname);
3623 /* placement is a CALL_EXPR around an ADDR_EXPR around a function. */
3625 /* Extract the function. */
3626 argtypes = TREE_OPERAND (TREE_OPERAND (placement, 0), 0);
3627 /* Then the second parm type. */
3628 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (argtypes)));
3630 /* Also the second argument. */
3631 args = TREE_CHAIN (TREE_OPERAND (placement, 1));
3635 /* First try it without the size argument. */
3636 argtypes = void_list_node;
3640 /* Strip const and volatile from addr. */
3641 addr = cp_convert (ptr_type_node, addr);
3643 /* We make two tries at finding a matching `operator delete'. On
3644 the first pass, we look for an one-operator (or placement)
3645 operator delete. If we're not doing placement delete, then on
3646 the second pass we look for a two-argument delete. */
3647 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3650 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3652 /* Normal delete; now try to find a match including the size
3654 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3655 tree_cons (NULL_TREE, sizetype,
3657 fntype = build_function_type (void_type_node, argtypes);
3659 /* Go through the `operator delete' functions looking for one
3660 with a matching type. */
3661 for (fn = BASELINK_P (fns) ? TREE_VALUE (fns) : fns;
3667 /* Exception specifications on the `delete' operator do not
3669 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3671 /* We also don't compare attributes. We're really just
3672 trying to check the types of the first two parameters. */
3673 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3677 /* If we found a match, we're done. */
3682 /* If we have a matching function, call it. */
3685 /* Make sure we have the actual function, and not an
3687 fn = OVL_CURRENT (fn);
3689 /* If the FN is a member function, make sure that it is
3691 if (DECL_CLASS_SCOPE_P (fn))
3692 enforce_access (type, fn);
3695 args = tree_cons (NULL_TREE, addr, args);
3697 args = tree_cons (NULL_TREE, addr,
3698 build_tree_list (NULL_TREE, size));
3700 return build_function_call (fn, args);
3703 /* If we are doing placement delete we do nothing if we don't find a
3704 matching op delete. */
3708 cp_error ("no suitable `operator delete' for `%T'", type);
3709 return error_mark_node;
3712 /* If the current scope isn't allowed to access DECL along
3713 BASETYPE_PATH, give an error. The most derived class in
3714 BASETYPE_PATH is the one used to qualify DECL. */
3717 enforce_access (basetype_path, decl)
3723 accessible = accessible_p (basetype_path, decl);
3726 if (TREE_PRIVATE (decl))
3727 cp_error_at ("`%+#D' is private", decl);
3728 else if (TREE_PROTECTED (decl))
3729 cp_error_at ("`%+#D' is protected", decl);
3731 cp_error_at ("`%+#D' is inaccessible", decl);
3732 cp_error ("within this context");
3739 /* Perform the conversions in CONVS on the expression EXPR.
3740 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3741 indicates the `this' argument of a method. INNER is non-zero when
3742 being called to continue a conversion chain. It is negative when a
3743 reference binding will be applied, positive otherwise. */
3746 convert_like_real (convs, expr, fn, argnum, inner)
3754 tree totype = TREE_TYPE (convs);
3756 if (ICS_BAD_FLAG (convs)
3757 && TREE_CODE (convs) != USER_CONV
3758 && TREE_CODE (convs) != AMBIG_CONV
3759 && TREE_CODE (convs) != REF_BIND)
3762 for (; t; t = TREE_OPERAND (t, 0))
3764 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3766 expr = convert_like_real (t, expr, fn, argnum, 1);
3769 else if (TREE_CODE (t) == AMBIG_CONV)
3770 return convert_like_real (t, expr, fn, argnum, 1);
3771 else if (TREE_CODE (t) == IDENTITY_CONV)
3774 cp_pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3776 cp_pedwarn (" initializing argument %P of `%D'", argnum, fn);
3777 return cp_convert (totype, expr);
3781 expr = dubious_conversion_warnings
3782 (totype, expr, "argument", fn, argnum);
3783 switch (TREE_CODE (convs))
3787 struct z_candidate *cand
3788 = WRAPPER_PTR (TREE_OPERAND (convs, 1));
3789 tree convfn = cand->fn;
3792 if (DECL_CONSTRUCTOR_P (convfn))
3794 tree t = build_int_2 (0, 0);
3795 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3797 args = build_tree_list (NULL_TREE, expr);
3798 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3799 || DECL_HAS_VTT_PARM_P (convfn))
3800 /* We should never try to call the abstract or base constructor
3803 args = tree_cons (NULL_TREE, t, args);
3806 args = build_this (expr);
3807 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3809 /* If this is a constructor or a function returning an aggr type,
3810 we need to build up a TARGET_EXPR. */
3811 if (DECL_CONSTRUCTOR_P (convfn))
3812 expr = build_cplus_new (totype, expr);
3814 /* The result of the call is then used to direct-initialize the object
3815 that is the destination of the copy-initialization. [dcl.init]
3817 Note that this step is not reflected in the conversion sequence;
3818 it affects the semantics when we actually perform the
3819 conversion, but is not considered during overload resolution.
3821 If the target is a class, that means call a ctor. */
3822 if (IS_AGGR_TYPE (totype)
3823 && (inner >= 0 || !lvalue_p (expr)))
3825 savew = warningcount, savee = errorcount;
3826 expr = build_new_method_call
3827 (NULL_TREE, complete_ctor_identifier,
3828 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3829 /* Core issue 84, now a DR, says that we don't allow UDCs
3830 for these args (which deliberately breaks copy-init of an
3831 auto_ptr<Base> from an auto_ptr<Derived>). */
3832 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3834 /* Tell the user where this failing constructor call came from. */
3837 if (warningcount > savew)
3839 (" initializing argument %P of `%D' from result of `%D'",
3840 argnum, fn, convfn);
3841 else if (errorcount > savee)
3843 (" initializing argument %P of `%D' from result of `%D'",
3844 argnum, fn, convfn);
3848 if (warningcount > savew)
3849 cp_warning (" initializing temporary from result of `%D'",
3851 else if (errorcount > savee)
3852 cp_error (" initializing temporary from result of `%D'",
3855 expr = build_cplus_new (totype, expr);
3860 if (type_unknown_p (expr))
3861 expr = instantiate_type (totype, expr, itf_complain);
3864 /* Call build_user_type_conversion again for the error. */
3865 return build_user_type_conversion
3866 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3872 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3873 TREE_CODE (convs) == REF_BIND ? -1 : 1);
3874 if (expr == error_mark_node)
3875 return error_mark_node;
3877 /* Convert a non-array constant variable to its underlying value, unless we
3878 are about to bind it to a reference, in which case we need to
3879 leave it as an lvalue. */
3880 if (TREE_CODE (convs) != REF_BIND
3881 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3882 expr = decl_constant_value (expr);
3884 switch (TREE_CODE (convs))
3887 if (! IS_AGGR_TYPE (totype))
3889 /* else fall through */
3891 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3893 /* We are going to bind a reference directly to a base-class
3894 subobject of EXPR. */
3895 tree base_ptr = build_pointer_type (totype);
3897 /* Build an expression for `*((base*) &expr)'. */
3898 expr = build_unary_op (ADDR_EXPR, expr, 0);
3899 expr = perform_implicit_conversion (base_ptr, expr);
3900 expr = build_indirect_ref (expr, "implicit conversion");
3904 /* Copy-initialization where the cv-unqualified version of the source
3905 type is the same class as, or a derived class of, the class of the
3906 destination [is treated as direct-initialization]. [dcl.init] */
3907 savew = warningcount, savee = errorcount;
3908 expr = build_new_method_call (NULL_TREE, complete_ctor_identifier,
3909 build_tree_list (NULL_TREE, expr),
3910 TYPE_BINFO (totype),
3911 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
3914 if (warningcount > savew)
3915 cp_warning (" initializing argument %P of `%D'", argnum, fn);
3916 else if (errorcount > savee)
3917 cp_error (" initializing argument %P of `%D'", argnum, fn);
3919 return build_cplus_new (totype, expr);
3923 tree ref_type = totype;
3925 /* If necessary, create a temporary. */
3926 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
3928 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3929 expr = build_target_expr_with_type (expr, type);
3932 /* Take the address of the thing to which we will bind the
3934 expr = build_unary_op (ADDR_EXPR, expr, 1);
3935 if (expr == error_mark_node)
3936 return error_mark_node;
3938 /* Convert it to a pointer to the type referred to by the
3939 reference. This will adjust the pointer if a derived to
3940 base conversion is being performed. */
3941 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3943 /* Convert the pointer to the desired reference type. */
3944 expr = build1 (NOP_EXPR, ref_type, expr);
3950 return decay_conversion (expr);
3953 /* Warn about deprecated conversion if appropriate. */
3954 string_conv_p (totype, expr, 1);
3960 return ocp_convert (totype, expr, CONV_IMPLICIT,
3961 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3964 /* ARG is being passed to a varargs function. Perform any conversions
3965 required. Array/function to pointer decay must have already happened.
3966 Return the converted value. */
3969 convert_arg_to_ellipsis (arg)
3972 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
3973 && (TYPE_PRECISION (TREE_TYPE (arg))
3974 < TYPE_PRECISION (double_type_node)))
3975 /* Convert `float' to `double'. */
3976 arg = cp_convert (double_type_node, arg);
3978 /* Convert `short' and `char' to full-size `int'. */
3979 arg = default_conversion (arg);
3981 arg = require_complete_type (arg);
3983 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
3985 /* Undefined behaviour [expr.call] 5.2.2/7. */
3986 cp_warning ("cannot pass objects of non-POD type `%#T' through `...'",
3993 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
3996 build_x_va_arg (expr, type)
4000 if (processing_template_decl)
4001 return build_min (VA_ARG_EXPR, type, expr);
4003 type = complete_type_or_else (type, NULL_TREE);
4005 if (expr == error_mark_node || !type)
4006 return error_mark_node;
4008 if (! pod_type_p (type))
4010 /* Undefined behaviour [expr.call] 5.2.2/7. */
4011 cp_warning ("cannot receive objects of non-POD type `%#T' through `...'",
4015 return build_va_arg (expr, type);
4018 /* TYPE has been given to va_arg. Apply the default conversions which would
4019 have happened when passed via ellipsis. Return the promoted type, or
4020 NULL_TREE, if there is no change. */
4023 convert_type_from_ellipsis (type)
4028 if (TREE_CODE (type) == ARRAY_TYPE)
4029 promote = build_pointer_type (TREE_TYPE (type));
4030 else if (TREE_CODE (type) == FUNCTION_TYPE)
4031 promote = build_pointer_type (type);
4033 promote = type_promotes_to (type);
4035 return same_type_p (type, promote) ? NULL_TREE : promote;
4038 /* ARG is a default argument expression being passed to a parameter of
4039 the indicated TYPE, which is a parameter to FN. Do any required
4040 conversions. Return the converted value. */
4043 convert_default_arg (type, arg, fn, parmnum)
4049 if (TREE_CODE (arg) == DEFAULT_ARG)
4051 /* When processing the default args for a class, we can find that
4052 there is an ordering constraint, and we call a function who's
4053 default args have not yet been converted. For instance,
4056 void Foo (A const & = A ());
4058 We must process A::A before A::Foo's default arg can be converted.
4059 Remember the dependent function, so do_pending_defargs can retry,
4061 unprocessed_defarg_fn (fn);
4063 /* Don't return error_mark node, as we won't be able to distinguish
4064 genuine errors from this case, and that would lead to repeated
4065 diagnostics. Just make something of the right type. */
4066 return build1 (NOP_EXPR, type, integer_zero_node);
4069 if (fn && DECL_TEMPLATE_INFO (fn))
4070 arg = tsubst_default_argument (fn, type, arg);
4072 arg = break_out_target_exprs (arg);
4074 if (TREE_CODE (arg) == CONSTRUCTOR)
4076 arg = digest_init (type, arg, 0);
4077 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4078 "default argument", fn, parmnum);
4082 /* This could get clobbered by the following call. */
4083 if (TREE_HAS_CONSTRUCTOR (arg))
4084 arg = copy_node (arg);
4086 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4087 "default argument", fn, parmnum);
4088 if (PROMOTE_PROTOTYPES
4089 && INTEGRAL_TYPE_P (type)
4090 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4091 arg = default_conversion (arg);
4097 /* Subroutine of the various build_*_call functions. Overload resolution
4098 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4099 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4100 bitmask of various LOOKUP_* flags which apply to the call itself. */
4103 build_over_call (cand, args, flags)
4104 struct z_candidate *cand;
4109 tree convs = cand->convs;
4110 tree converted_args = NULL_TREE;
4111 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4112 tree conv, arg, val;
4116 /* Give any warnings we noticed during overload resolution. */
4118 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4119 joust (cand, WRAPPER_PTR (TREE_VALUE (val)), 1);
4121 if (DECL_FUNCTION_MEMBER_P (fn))
4122 enforce_access (cand->basetype_path, fn);
4124 if (args && TREE_CODE (args) != TREE_LIST)
4125 args = build_tree_list (NULL_TREE, args);
4128 /* The implicit parameters to a constructor are not considered by overload
4129 resolution, and must be of the proper type. */
4130 if (DECL_CONSTRUCTOR_P (fn))
4132 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4133 arg = TREE_CHAIN (arg);
4134 parm = TREE_CHAIN (parm);
4135 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4136 /* We should never try to call the abstract constructor. */
4138 if (DECL_HAS_VTT_PARM_P (fn))
4140 converted_args = tree_cons
4141 (NULL_TREE, TREE_VALUE (arg), converted_args);
4142 arg = TREE_CHAIN (arg);
4143 parm = TREE_CHAIN (parm);
4146 /* Bypass access control for 'this' parameter. */
4147 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4149 tree parmtype = TREE_VALUE (parm);
4150 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4152 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4153 cp_pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4154 TREE_TYPE (argtype), fn);
4156 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4157 X is called for an object that is not of type X, or of a type
4158 derived from X, the behavior is undefined.
4160 So we can assume that anything passed as 'this' is non-null, and
4161 optimize accordingly. */
4162 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4163 t = lookup_base (TREE_TYPE (TREE_TYPE (TREE_VALUE (arg))),
4164 TREE_TYPE (parmtype), ba_ignore, NULL);
4165 t = build_base_path (PLUS_EXPR, TREE_VALUE (arg), t, 1);
4166 converted_args = tree_cons (NULL_TREE, t, converted_args);
4167 parm = TREE_CHAIN (parm);
4168 arg = TREE_CHAIN (arg);
4174 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4176 tree type = TREE_VALUE (parm);
4178 conv = TREE_VEC_ELT (convs, i);
4179 val = convert_like_with_context
4180 (conv, TREE_VALUE (arg), fn, i - is_method);
4182 if (PROMOTE_PROTOTYPES
4183 && INTEGRAL_TYPE_P (type)
4184 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4185 val = default_conversion (val);
4186 converted_args = tree_cons (NULL_TREE, val, converted_args);
4189 /* Default arguments */
4190 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4192 = tree_cons (NULL_TREE,
4193 convert_default_arg (TREE_VALUE (parm),
4194 TREE_PURPOSE (parm),
4199 for (; arg; arg = TREE_CHAIN (arg))
4201 = tree_cons (NULL_TREE,
4202 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4205 converted_args = nreverse (converted_args);
4208 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4211 /* Avoid actually calling copy constructors and copy assignment operators,
4214 if (! flag_elide_constructors)
4215 /* Do things the hard way. */;
4216 else if (TREE_VEC_LENGTH (convs) == 1
4217 && DECL_COPY_CONSTRUCTOR_P (fn))
4220 arg = skip_artificial_parms_for (fn, converted_args);
4221 arg = TREE_VALUE (arg);
4223 /* Pull out the real argument, disregarding const-correctness. */
4225 while (TREE_CODE (targ) == NOP_EXPR
4226 || TREE_CODE (targ) == NON_LVALUE_EXPR
4227 || TREE_CODE (targ) == CONVERT_EXPR)
4228 targ = TREE_OPERAND (targ, 0);
4229 if (TREE_CODE (targ) == ADDR_EXPR)
4231 targ = TREE_OPERAND (targ, 0);
4232 if (!same_type_ignoring_top_level_qualifiers_p
4233 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4242 arg = build_indirect_ref (arg, 0);
4244 /* [class.copy]: the copy constructor is implicitly defined even if
4245 the implementation elided its use. */
4246 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4249 /* If we're creating a temp and we already have one, don't create a
4250 new one. If we're not creating a temp but we get one, use
4251 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4252 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4253 temp or an INIT_EXPR otherwise. */
4254 if (integer_zerop (TREE_VALUE (args)))
4256 if (! real_lvalue_p (arg))
4258 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4259 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4261 else if ((!real_lvalue_p (arg)
4262 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4263 /* Empty classes have padding which can be hidden
4264 inside an (empty) base of the class. This must not
4265 be touched as it might overlay things. When the
4266 gcc core learns about empty classes, we can treat it
4267 like other classes. */
4268 && !is_empty_class (DECL_CONTEXT (fn)))
4271 tree to = stabilize_reference
4272 (build_indirect_ref (TREE_VALUE (args), 0));
4274 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4275 address = build_unary_op (ADDR_EXPR, val, 0);
4276 /* Avoid a warning about this expression, if the address is
4278 TREE_USED (address) = 1;
4282 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4284 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4286 tree to = stabilize_reference
4287 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4289 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4290 if (is_empty_class (TREE_TYPE (to)))
4292 TREE_USED (arg) = 1;
4294 val = build (COMPOUND_EXPR, DECL_CONTEXT (fn), arg, to);
4295 /* Even though the assignment may not actually result in any
4296 code being generated, we do not want to warn about the
4297 assignment having no effect. That would be confusing to
4298 users who may be performing the assignment as part of a
4299 generic algorithm, for example.
4301 Ideally, the notions of having side-effects and of being
4302 useless would be orthogonal. */
4303 TREE_SIDE_EFFECTS (val) = 1;
4306 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4312 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4314 tree t, *p = &TREE_VALUE (converted_args);
4315 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4316 DECL_VIRTUAL_CONTEXT (fn),
4318 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4320 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4321 if (TREE_SIDE_EFFECTS (*p))
4322 *p = save_expr (*p);
4323 t = build_pointer_type (TREE_TYPE (fn));
4324 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4325 fn = build_java_interface_fn_ref (fn, *p);
4327 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4330 else if (DECL_INLINE (fn))
4331 fn = inline_conversion (fn);
4333 fn = build_addr_func (fn);
4335 /* Recognize certain built-in functions so we can make tree-codes
4336 other than CALL_EXPR. We do this when it enables fold-const.c
4337 to do something useful. */
4339 if (TREE_CODE (fn) == ADDR_EXPR
4340 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4341 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4344 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4349 /* Some built-in function calls will be evaluated at
4350 compile-time in fold (). */
4351 fn = fold (build_call (fn, converted_args));
4352 if (VOID_TYPE_P (TREE_TYPE (fn)))
4354 fn = require_complete_type (fn);
4355 if (fn == error_mark_node)
4356 return error_mark_node;
4357 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4358 fn = build_cplus_new (TREE_TYPE (fn), fn);
4359 return convert_from_reference (fn);
4362 static tree java_iface_lookup_fn;
4364 /* Make an expression which yields the address of the Java interface
4365 method FN. This is achieved by generating a call to libjava's
4366 _Jv_LookupInterfaceMethodIdx(). */
4369 build_java_interface_fn_ref (fn, instance)
4372 tree lookup_args, lookup_fn, method, idx;
4373 tree klass_ref, iface, iface_ref;
4376 if (!java_iface_lookup_fn)
4378 tree endlink = build_void_list_node ();
4379 tree t = tree_cons (NULL_TREE, ptr_type_node,
4380 tree_cons (NULL_TREE, ptr_type_node,
4381 tree_cons (NULL_TREE, java_int_type_node,
4383 java_iface_lookup_fn
4384 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4385 build_function_type (ptr_type_node, t),
4386 0, NOT_BUILT_IN, NULL);
4387 ggc_add_tree_root (&java_iface_lookup_fn, 1);
4390 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4391 This is the first entry in the vtable. */
4392 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4395 /* Get the java.lang.Class pointer for the interface being called. */
4396 iface = DECL_CONTEXT (fn);
4397 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4398 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4399 || DECL_CONTEXT (iface_ref) != iface)
4401 cp_error ("could not find class$ field in java interface type `%T'",
4403 return error_mark_node;
4405 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4407 /* Determine the itable index of FN. */
4409 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4411 if (!DECL_VIRTUAL_P (method))
4417 idx = build_int_2 (i, 0);
4419 lookup_args = tree_cons (NULL_TREE, klass_ref,
4420 tree_cons (NULL_TREE, iface_ref,
4421 build_tree_list (NULL_TREE, idx)));
4422 lookup_fn = build1 (ADDR_EXPR,
4423 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4424 java_iface_lookup_fn);
4425 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4428 /* Returns the value to use for the in-charge parameter when making a
4429 call to a function with the indicated NAME. */
4432 in_charge_arg_for_name (name)
4435 if (name == base_ctor_identifier
4436 || name == base_dtor_identifier)
4437 return integer_zero_node;
4438 else if (name == complete_ctor_identifier)
4439 return integer_one_node;
4440 else if (name == complete_dtor_identifier)
4441 return integer_two_node;
4442 else if (name == deleting_dtor_identifier)
4443 return integer_three_node;
4445 /* This function should only be called with one of the names listed
4447 my_friendly_abort (20000411);
4452 build_new_method_call (instance, name, args, basetype_path, flags)
4453 tree instance, name, args, basetype_path;
4456 struct z_candidate *candidates = 0, *cand;
4457 tree explicit_targs = NULL_TREE;
4458 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4461 tree templates = NULL_TREE;
4463 int template_only = 0;
4465 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4467 explicit_targs = TREE_OPERAND (name, 1);
4468 name = TREE_OPERAND (name, 0);
4470 name = DECL_NAME (name);
4473 if (TREE_CODE (name) == COMPONENT_REF)
4474 name = TREE_OPERAND (name, 1);
4475 if (TREE_CODE (name) == OVERLOAD)
4476 name = DECL_NAME (OVL_CURRENT (name));
4483 args = resolve_args (args);
4485 if (args == error_mark_node)
4486 return error_mark_node;
4488 if (instance == NULL_TREE)
4489 basetype = BINFO_TYPE (basetype_path);
4492 if (TREE_CODE (instance) == OFFSET_REF)
4493 instance = resolve_offset_ref (instance);
4494 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4495 instance = convert_from_reference (instance);
4496 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4498 /* XXX this should be handled before we get here. */
4499 if (! IS_AGGR_TYPE (basetype))
4501 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4502 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4503 name, instance, basetype);
4505 return error_mark_node;
4509 if (basetype_path == NULL_TREE)
4510 basetype_path = TYPE_BINFO (basetype);
4514 instance_ptr = build_this (instance);
4516 if (! template_only)
4518 /* XXX this should be handled before we get here. */
4519 fns = build_field_call (basetype_path, instance_ptr, name, args);
4526 instance_ptr = build_int_2 (0, 0);
4527 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4530 /* Callers should explicitly indicate whether they want to construct
4531 the complete object or just the part without virtual bases. */
4532 my_friendly_assert (name != ctor_identifier, 20000408);
4533 /* Similarly for destructors. */
4534 my_friendly_assert (name != dtor_identifier, 20000408);
4536 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4540 constructor_p = (name == complete_ctor_identifier
4541 || name == base_ctor_identifier);
4542 pretty_name = (constructor_p
4543 ? constructor_name (basetype) : dtor_identifier);
4545 /* If we're a call to a constructor or destructor for a
4546 subobject that uses virtual base classes, then we need to
4547 pass down a pointer to a VTT for the subobject. */
4548 if ((name == base_ctor_identifier
4549 || name == base_dtor_identifier)
4550 && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4554 tree basebinfo = basetype_path;
4556 /* If the current function is a complete object constructor
4557 or destructor, then we fetch the VTT directly.
4558 Otherwise, we look it up using the VTT we were given. */
4559 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4560 vtt = decay_conversion (vtt);
4561 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4562 build (EQ_EXPR, boolean_type_node,
4563 current_in_charge_parm, integer_zero_node),
4566 if (TREE_VIA_VIRTUAL (basebinfo))
4567 basebinfo = binfo_for_vbase (basetype, current_class_type);
4568 my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4569 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4570 BINFO_SUBVTT_INDEX (basebinfo));
4572 args = tree_cons (NULL_TREE, sub_vtt, args);
4578 fns = lookup_fnfields (basetype_path, name, 1);
4580 if (fns == error_mark_node)
4581 return error_mark_node;
4584 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4585 tree fn = TREE_VALUE (fns);
4586 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4587 for (; fn; fn = OVL_NEXT (fn))
4589 tree t = OVL_CURRENT (fn);
4592 /* We can end up here for copy-init of same or base class. */
4593 if ((flags & LOOKUP_ONLYCONVERTING)
4594 && DECL_NONCONVERTING_P (t))
4597 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4598 this_arglist = mem_args;
4600 this_arglist = args;
4602 if (TREE_CODE (t) == TEMPLATE_DECL)
4604 /* A member template. */
4605 templates = tree_cons (NULL_TREE, t, templates);
4607 add_template_candidate (candidates, t, base, explicit_targs,
4609 TREE_TYPE (name), flags, DEDUCE_CALL);
4611 else if (! template_only)
4612 candidates = add_function_candidate (candidates, t, base,
4613 this_arglist, flags);
4616 candidates->basetype_path = basetype_path;
4620 if (! any_viable (candidates))
4622 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4623 if (flags & LOOKUP_SPECULATIVELY)
4625 if (!COMPLETE_TYPE_P (basetype))
4626 incomplete_type_error (instance_ptr, basetype);
4628 cp_error ("no matching function for call to `%T::%D(%A)%V'",
4629 basetype, pretty_name, user_args,
4630 TREE_TYPE (TREE_TYPE (instance_ptr)));
4631 print_z_candidates (candidates);
4632 return error_mark_node;
4634 candidates = splice_viable (candidates);
4635 cand = tourney (candidates);
4639 cp_error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4641 print_z_candidates (candidates);
4642 return error_mark_node;
4645 if (DECL_PURE_VIRTUAL_P (cand->fn)
4646 && instance == current_class_ref
4647 && (DECL_CONSTRUCTOR_P (current_function_decl)
4648 || DECL_DESTRUCTOR_P (current_function_decl))
4649 && ! (flags & LOOKUP_NONVIRTUAL)
4650 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4651 cp_error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4652 "abstract virtual `%#D' called from constructor"
4653 : "abstract virtual `%#D' called from destructor"),
4655 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4656 && is_dummy_object (instance_ptr))
4658 cp_error ("cannot call member function `%D' without object", cand->fn);
4659 return error_mark_node;
4662 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4663 && ((instance == current_class_ref && (dtor_label || ctor_label))
4664 || resolves_to_fixed_type_p (instance, 0)))
4665 flags |= LOOKUP_NONVIRTUAL;
4667 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4668 call = build_over_call (cand, mem_args, flags);
4671 call = build_over_call (cand, args, flags);
4672 /* Do evaluate the object parameter in a call to a static member
4674 if (TREE_SIDE_EFFECTS (instance))
4675 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4681 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4682 subsequence of ICS2. */
4685 is_subseq (ics1, ics2)
4688 /* We can assume that a conversion of the same code
4689 between the same types indicates a subsequence since we only get
4690 here if the types we are converting from are the same. */
4692 while (TREE_CODE (ics1) == RVALUE_CONV
4693 || TREE_CODE (ics1) == LVALUE_CONV)
4694 ics1 = TREE_OPERAND (ics1, 0);
4698 while (TREE_CODE (ics2) == RVALUE_CONV
4699 || TREE_CODE (ics2) == LVALUE_CONV)
4700 ics2 = TREE_OPERAND (ics2, 0);
4702 if (TREE_CODE (ics2) == USER_CONV
4703 || TREE_CODE (ics2) == AMBIG_CONV
4704 || TREE_CODE (ics2) == IDENTITY_CONV)
4705 /* At this point, ICS1 cannot be a proper subsequence of
4706 ICS2. We can get a USER_CONV when we are comparing the
4707 second standard conversion sequence of two user conversion
4711 ics2 = TREE_OPERAND (ics2, 0);
4713 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4714 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4715 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4716 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4721 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4722 be any _TYPE nodes. */
4725 is_properly_derived_from (derived, base)
4729 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4730 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4733 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4734 considers every class derived from itself. */
4735 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4736 && DERIVED_FROM_P (base, derived));
4739 /* We build the ICS for an implicit object parameter as a pointer
4740 conversion sequence. However, such a sequence should be compared
4741 as if it were a reference conversion sequence. If ICS is the
4742 implicit conversion sequence for an implicit object parameter,
4743 modify it accordingly. */
4746 maybe_handle_implicit_object (ics)
4749 if (ICS_THIS_FLAG (*ics))
4751 /* [over.match.funcs]
4753 For non-static member functions, the type of the
4754 implicit object parameter is "reference to cv X"
4755 where X is the class of which the function is a
4756 member and cv is the cv-qualification on the member
4757 function declaration. */
4759 tree reference_type;
4761 /* The `this' parameter is a pointer to a class type. Make the
4762 implict conversion talk about a reference to that same class
4764 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4765 reference_type = build_reference_type (reference_type);
4767 if (TREE_CODE (t) == QUAL_CONV)
4768 t = TREE_OPERAND (t, 0);
4769 if (TREE_CODE (t) == PTR_CONV)
4770 t = TREE_OPERAND (t, 0);
4771 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4772 t = direct_reference_binding (reference_type, t);
4777 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4778 and return the type to which the reference refers. Otherwise,
4779 leave *ICS unchanged and return NULL_TREE. */
4782 maybe_handle_ref_bind (ics)
4785 if (TREE_CODE (*ics) == REF_BIND)
4787 tree old_ics = *ics;
4788 tree type = TREE_TYPE (TREE_TYPE (old_ics));
4789 *ics = TREE_OPERAND (old_ics, 0);
4790 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4791 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4798 /* Compare two implicit conversion sequences according to the rules set out in
4799 [over.ics.rank]. Return values:
4801 1: ics1 is better than ics2
4802 -1: ics2 is better than ics1
4803 0: ics1 and ics2 are indistinguishable */
4806 compare_ics (ics1, ics2)
4813 tree deref_from_type1 = NULL_TREE;
4814 tree deref_from_type2 = NULL_TREE;
4815 tree deref_to_type1 = NULL_TREE;
4816 tree deref_to_type2 = NULL_TREE;
4819 /* REF_BINDING is non-zero if the result of the conversion sequence
4820 is a reference type. In that case TARGET_TYPE is the
4821 type referred to by the reference. */
4825 /* Handle implicit object parameters. */
4826 maybe_handle_implicit_object (&ics1);
4827 maybe_handle_implicit_object (&ics2);
4829 /* Handle reference parameters. */
4830 target_type1 = maybe_handle_ref_bind (&ics1);
4831 target_type2 = maybe_handle_ref_bind (&ics2);
4835 When comparing the basic forms of implicit conversion sequences (as
4836 defined in _over.best.ics_)
4838 --a standard conversion sequence (_over.ics.scs_) is a better
4839 conversion sequence than a user-defined conversion sequence
4840 or an ellipsis conversion sequence, and
4842 --a user-defined conversion sequence (_over.ics.user_) is a
4843 better conversion sequence than an ellipsis conversion sequence
4844 (_over.ics.ellipsis_). */
4845 rank1 = ICS_RANK (ics1);
4846 rank2 = ICS_RANK (ics2);
4850 else if (rank1 < rank2)
4853 if (rank1 == BAD_RANK)
4855 /* XXX Isn't this an extension? */
4856 /* Both ICS are bad. We try to make a decision based on what
4857 would have happenned if they'd been good. */
4858 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4859 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4861 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4862 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4865 /* We couldn't make up our minds; try to figure it out below. */
4868 if (ICS_ELLIPSIS_FLAG (ics1))
4869 /* Both conversions are ellipsis conversions. */
4872 /* User-defined conversion sequence U1 is a better conversion sequence
4873 than another user-defined conversion sequence U2 if they contain the
4874 same user-defined conversion operator or constructor and if the sec-
4875 ond standard conversion sequence of U1 is better than the second
4876 standard conversion sequence of U2. */
4878 if (ICS_USER_FLAG (ics1))
4882 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4883 if (TREE_CODE (t1) == AMBIG_CONV)
4885 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4886 if (TREE_CODE (t2) == AMBIG_CONV)
4889 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4892 /* We can just fall through here, after setting up
4893 FROM_TYPE1 and FROM_TYPE2. */
4894 from_type1 = TREE_TYPE (t1);
4895 from_type2 = TREE_TYPE (t2);
4899 /* We're dealing with two standard conversion sequences.
4903 Standard conversion sequence S1 is a better conversion
4904 sequence than standard conversion sequence S2 if
4906 --S1 is a proper subsequence of S2 (comparing the conversion
4907 sequences in the canonical form defined by _over.ics.scs_,
4908 excluding any Lvalue Transformation; the identity
4909 conversion sequence is considered to be a subsequence of
4910 any non-identity conversion sequence */
4913 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4914 from_type1 = TREE_OPERAND (from_type1, 0);
4915 from_type1 = TREE_TYPE (from_type1);
4918 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4919 from_type2 = TREE_OPERAND (from_type2, 0);
4920 from_type2 = TREE_TYPE (from_type2);
4923 if (same_type_p (from_type1, from_type2))
4925 if (is_subseq (ics1, ics2))
4927 if (is_subseq (ics2, ics1))
4930 /* Otherwise, one sequence cannot be a subsequence of the other; they
4931 don't start with the same type. This can happen when comparing the
4932 second standard conversion sequence in two user-defined conversion
4939 --the rank of S1 is better than the rank of S2 (by the rules
4942 Standard conversion sequences are ordered by their ranks: an Exact
4943 Match is a better conversion than a Promotion, which is a better
4944 conversion than a Conversion.
4946 Two conversion sequences with the same rank are indistinguishable
4947 unless one of the following rules applies:
4949 --A conversion that is not a conversion of a pointer, or pointer
4950 to member, to bool is better than another conversion that is such
4953 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4954 so that we do not have to check it explicitly. */
4955 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4957 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
4960 to_type1 = TREE_TYPE (ics1);
4961 to_type2 = TREE_TYPE (ics2);
4963 if (TYPE_PTR_P (from_type1)
4964 && TYPE_PTR_P (from_type2)
4965 && TYPE_PTR_P (to_type1)
4966 && TYPE_PTR_P (to_type2))
4968 deref_from_type1 = TREE_TYPE (from_type1);
4969 deref_from_type2 = TREE_TYPE (from_type2);
4970 deref_to_type1 = TREE_TYPE (to_type1);
4971 deref_to_type2 = TREE_TYPE (to_type2);
4973 /* The rules for pointers to members A::* are just like the rules
4974 for pointers A*, except opposite: if B is derived from A then
4975 A::* converts to B::*, not vice versa. For that reason, we
4976 switch the from_ and to_ variables here. */
4977 else if (TYPE_PTRMEM_P (from_type1)
4978 && TYPE_PTRMEM_P (from_type2)
4979 && TYPE_PTRMEM_P (to_type1)
4980 && TYPE_PTRMEM_P (to_type2))
4982 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
4983 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
4984 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
4985 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
4987 else if (TYPE_PTRMEMFUNC_P (from_type1)
4988 && TYPE_PTRMEMFUNC_P (from_type2)
4989 && TYPE_PTRMEMFUNC_P (to_type1)
4990 && TYPE_PTRMEMFUNC_P (to_type2))
4992 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
4993 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
4994 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
4995 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
4998 if (deref_from_type1 != NULL_TREE
4999 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5000 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5002 /* This was one of the pointer or pointer-like conversions.
5006 --If class B is derived directly or indirectly from class A,
5007 conversion of B* to A* is better than conversion of B* to
5008 void*, and conversion of A* to void* is better than
5009 conversion of B* to void*. */
5010 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5011 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5013 if (is_properly_derived_from (deref_from_type1,
5016 else if (is_properly_derived_from (deref_from_type2,
5020 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5021 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5023 if (same_type_p (deref_from_type1, deref_from_type2))
5025 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5027 if (is_properly_derived_from (deref_from_type1,
5031 /* We know that DEREF_TO_TYPE1 is `void' here. */
5032 else if (is_properly_derived_from (deref_from_type1,
5037 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5038 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5042 --If class B is derived directly or indirectly from class A
5043 and class C is derived directly or indirectly from B,
5045 --conversion of C* to B* is better than conversion of C* to
5048 --conversion of B* to A* is better than conversion of C* to
5050 if (same_type_p (deref_from_type1, deref_from_type2))
5052 if (is_properly_derived_from (deref_to_type1,
5055 else if (is_properly_derived_from (deref_to_type2,
5059 else if (same_type_p (deref_to_type1, deref_to_type2))
5061 if (is_properly_derived_from (deref_from_type2,
5064 else if (is_properly_derived_from (deref_from_type1,
5070 else if (CLASS_TYPE_P (non_reference (from_type1))
5071 && same_type_p (from_type1, from_type2))
5073 tree from = non_reference (from_type1);
5077 --binding of an expression of type C to a reference of type
5078 B& is better than binding an expression of type C to a
5079 reference of type A&
5081 --conversion of C to B is better than conversion of C to A, */
5082 if (is_properly_derived_from (from, to_type1)
5083 && is_properly_derived_from (from, to_type2))
5085 if (is_properly_derived_from (to_type1, to_type2))
5087 else if (is_properly_derived_from (to_type2, to_type1))
5091 else if (CLASS_TYPE_P (non_reference (to_type1))
5092 && same_type_p (to_type1, to_type2))
5094 tree to = non_reference (to_type1);
5098 --binding of an expression of type B to a reference of type
5099 A& is better than binding an expression of type C to a
5100 reference of type A&,
5102 --onversion of B to A is better than conversion of C to A */
5103 if (is_properly_derived_from (from_type1, to)
5104 && is_properly_derived_from (from_type2, to))
5106 if (is_properly_derived_from (from_type2, from_type1))
5108 else if (is_properly_derived_from (from_type1, from_type2))
5115 --S1 and S2 differ only in their qualification conversion and yield
5116 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5117 qualification signature of type T1 is a proper subset of the cv-
5118 qualification signature of type T2 */
5119 if (TREE_CODE (ics1) == QUAL_CONV
5120 && TREE_CODE (ics2) == QUAL_CONV
5121 && same_type_p (from_type1, from_type2))
5122 return comp_cv_qual_signature (to_type1, to_type2);
5126 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5127 types to which the references refer are the same type except for
5128 top-level cv-qualifiers, and the type to which the reference
5129 initialized by S2 refers is more cv-qualified than the type to
5130 which the reference initialized by S1 refers */
5132 if (target_type1 && target_type2
5133 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5134 return comp_cv_qualification (target_type2, target_type1);
5136 /* Neither conversion sequence is better than the other. */
5140 /* The source type for this standard conversion sequence. */
5146 for (;; t = TREE_OPERAND (t, 0))
5148 if (TREE_CODE (t) == USER_CONV
5149 || TREE_CODE (t) == AMBIG_CONV
5150 || TREE_CODE (t) == IDENTITY_CONV)
5151 return TREE_TYPE (t);
5153 my_friendly_abort (1823);
5156 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5157 a pointer to LOSER and re-running joust to produce the warning if WINNER
5158 is actually used. */
5161 add_warning (winner, loser)
5162 struct z_candidate *winner, *loser;
5164 winner->warnings = tree_cons (NULL_TREE,
5165 build_ptr_wrapper (loser),
5169 /* Returns true iff functions are equivalent. Equivalent functions are
5170 not '==' only if one is a function-local extern function or if
5171 both are extern "C". */
5174 equal_functions (fn1, fn2)
5178 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5179 || DECL_EXTERN_C_FUNCTION_P (fn1))
5180 return decls_match (fn1, fn2);
5184 /* Compare two candidates for overloading as described in
5185 [over.match.best]. Return values:
5187 1: cand1 is better than cand2
5188 -1: cand2 is better than cand1
5189 0: cand1 and cand2 are indistinguishable */
5192 joust (cand1, cand2, warn)
5193 struct z_candidate *cand1, *cand2;
5197 int i, off1 = 0, off2 = 0, len;
5199 /* Candidates that involve bad conversions are always worse than those
5201 if (cand1->viable > cand2->viable)
5203 if (cand1->viable < cand2->viable)
5206 /* If we have two pseudo-candidates for conversions to the same type,
5207 or two candidates for the same function, arbitrarily pick one. */
5208 if (cand1->fn == cand2->fn
5209 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5212 /* a viable function F1
5213 is defined to be a better function than another viable function F2 if
5214 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5215 ICSi(F2), and then */
5217 /* for some argument j, ICSj(F1) is a better conversion sequence than
5220 /* For comparing static and non-static member functions, we ignore
5221 the implicit object parameter of the non-static function. The
5222 standard says to pretend that the static function has an object
5223 parm, but that won't work with operator overloading. */
5224 len = TREE_VEC_LENGTH (cand1->convs);
5225 if (len != TREE_VEC_LENGTH (cand2->convs))
5227 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5228 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5230 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5231 && DECL_STATIC_FUNCTION_P (cand2->fn))
5237 my_friendly_abort (42);
5240 for (i = 0; i < len; ++i)
5242 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5243 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5244 int comp = compare_ics (t1, t2);
5249 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5250 && TREE_CODE (t1) == STD_CONV
5251 && TREE_CODE (t2) == STD_CONV
5252 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5253 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5254 && (TYPE_PRECISION (TREE_TYPE (t1))
5255 == TYPE_PRECISION (TREE_TYPE (t2)))
5256 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5257 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5260 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5262 struct z_candidate *w, *l;
5264 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5265 w = cand1, l = cand2;
5267 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5268 w = cand2, l = cand1;
5272 cp_warning ("passing `%T' chooses `%T' over `%T'",
5273 type, type1, type2);
5274 cp_warning (" in call to `%D'", w->fn);
5280 if (winner && comp != winner)
5289 /* warn about confusing overload resolution for user-defined conversions,
5290 either between a constructor and a conversion op, or between two
5292 if (winner && cand1->second_conv
5293 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5294 != DECL_CONSTRUCTOR_P (cand2->fn))
5295 /* Don't warn if the two conv ops convert to the same type... */
5296 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5297 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5298 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5300 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5303 struct z_candidate *w, *l;
5306 w = cand1, l = cand2;
5308 w = cand2, l = cand1;
5309 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5310 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5311 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5312 && (convn = standard_conversion
5313 (TREE_TYPE (TREE_TYPE (l->fn)),
5314 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5315 && TREE_CODE (convn) == QUAL_CONV)
5316 /* Don't complain about `operator char *()' beating
5317 `operator const char *() const'. */;
5320 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5321 if (! DECL_CONSTRUCTOR_P (w->fn))
5322 source = TREE_TYPE (source);
5323 cp_warning ("choosing `%D' over `%D'", w->fn, l->fn);
5324 cp_warning (" for conversion from `%T' to `%T'",
5325 source, TREE_TYPE (w->second_conv));
5326 cp_warning (" because conversion sequence for the argument is better");
5337 F1 is a non-template function and F2 is a template function
5340 if (! cand1->template && cand2->template)
5342 else if (cand1->template && ! cand2->template)
5346 F1 and F2 are template functions and the function template for F1 is
5347 more specialized than the template for F2 according to the partial
5350 if (cand1->template && cand2->template)
5352 winner = more_specialized
5353 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5355 /* Tell the deduction code how many real function arguments
5356 we saw, not counting the implicit 'this' argument. But,
5357 add_function_candidate() suppresses the "this" argument
5360 [temp.func.order]: The presence of unused ellipsis and default
5361 arguments has no effect on the partial ordering of function
5363 TREE_VEC_LENGTH (cand1->convs)
5364 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5365 - DECL_CONSTRUCTOR_P (cand1->fn)));
5372 the context is an initialization by user-defined conversion (see
5373 _dcl.init_ and _over.match.user_) and the standard conversion
5374 sequence from the return type of F1 to the destination type (i.e.,
5375 the type of the entity being initialized) is a better conversion
5376 sequence than the standard conversion sequence from the return type
5377 of F2 to the destination type. */
5379 if (cand1->second_conv)
5381 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5386 /* Check whether we can discard a builtin candidate, either because we
5387 have two identical ones or matching builtin and non-builtin candidates.
5389 (Pedantically in the latter case the builtin which matched the user
5390 function should not be added to the overload set, but we spot it here.
5393 ... the builtin candidates include ...
5394 - do not have the same parameter type list as any non-template
5395 non-member candidate. */
5397 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5398 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5400 for (i = 0; i < len; ++i)
5401 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5402 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5404 if (i == TREE_VEC_LENGTH (cand1->convs))
5406 if (cand1->fn == cand2->fn)
5407 /* Two built-in candidates; arbitrarily pick one. */
5409 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5410 /* cand1 is built-in; prefer cand2. */
5413 /* cand2 is built-in; prefer cand1. */
5418 /* If the two functions are the same (this can happen with declarations
5419 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5420 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5421 && equal_functions (cand1->fn, cand2->fn))
5426 /* Extension: If the worst conversion for one candidate is worse than the
5427 worst conversion for the other, take the first. */
5430 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5431 struct z_candidate *w = 0, *l = 0;
5433 for (i = 0; i < len; ++i)
5435 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5436 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5437 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5438 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5441 winner = 1, w = cand1, l = cand2;
5443 winner = -1, w = cand2, l = cand1;
5448 cp_pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5450 " because worst conversion for the former is better than worst conversion for the latter");
5458 my_friendly_assert (!winner, 20010121);
5462 /* Given a list of candidates for overloading, find the best one, if any.
5463 This algorithm has a worst case of O(2n) (winner is last), and a best
5464 case of O(n/2) (totally ambiguous); much better than a sorting
5467 static struct z_candidate *
5468 tourney (candidates)
5469 struct z_candidate *candidates;
5471 struct z_candidate *champ = candidates, *challenger;
5473 int champ_compared_to_predecessor = 0;
5475 /* Walk through the list once, comparing each current champ to the next
5476 candidate, knocking out a candidate or two with each comparison. */
5478 for (challenger = champ->next; challenger; )
5480 fate = joust (champ, challenger, 0);
5482 challenger = challenger->next;
5487 champ = challenger->next;
5490 champ_compared_to_predecessor = 0;
5495 champ_compared_to_predecessor = 1;
5498 challenger = champ->next;
5502 /* Make sure the champ is better than all the candidates it hasn't yet
5503 been compared to. */
5505 for (challenger = candidates;
5507 && !(champ_compared_to_predecessor && challenger->next == champ);
5508 challenger = challenger->next)
5510 fate = joust (champ, challenger, 0);
5518 /* Returns non-zero if things of type FROM can be converted to TO. */
5521 can_convert (to, from)
5524 return can_convert_arg (to, from, NULL_TREE);
5527 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5530 can_convert_arg (to, from, arg)
5533 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5534 return (t && ! ICS_BAD_FLAG (t));
5537 /* Like can_convert_arg, but allows dubious conversions as well. */
5540 can_convert_arg_bad (to, from, arg)
5543 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5547 /* Convert EXPR to TYPE. Return the converted expression.
5549 Note that we allow bad conversions here because by the time we get to
5550 this point we are committed to doing the conversion. If we end up
5551 doing a bad conversion, convert_like will complain. */
5554 perform_implicit_conversion (type, expr)
5560 if (expr == error_mark_node)
5561 return error_mark_node;
5562 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5566 cp_error ("could not convert `%E' to `%T'", expr, type);
5567 return error_mark_node;
5570 return convert_like (conv, expr);
5573 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5574 initializing a variable of that TYPE. Return the converted
5578 initialize_reference (type, expr)
5584 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5585 if (!conv || ICS_BAD_FLAG (conv))
5587 cp_error ("could not convert `%E' to `%T'", expr, type);
5588 return error_mark_node;
5591 return convert_like (conv, expr);