1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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_field_call PARAMS ((tree, tree, tree));
42 static struct z_candidate * tourney PARAMS ((struct z_candidate *));
43 static int equal_functions PARAMS ((tree, tree));
44 static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
45 static int compare_ics PARAMS ((tree, tree));
46 static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
47 static tree build_java_interface_fn_ref PARAMS ((tree, tree));
48 #define convert_like(CONV, EXPR) \
49 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0)
50 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
51 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 int any_strictly_viable PARAMS ((struct z_candidate *));
64 static struct z_candidate * add_template_candidate
65 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree,
66 tree, tree, int, unification_kind_t));
67 static struct z_candidate * add_template_candidate_real
68 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree,
69 tree, tree, int, tree, unification_kind_t));
70 static struct z_candidate * add_template_conv_candidate
71 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, tree));
72 static struct z_candidate * add_builtin_candidates
73 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
75 static struct z_candidate * add_builtin_candidate
76 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
77 tree, tree, tree, tree *, tree *, int));
78 static int is_complete PARAMS ((tree));
79 static struct z_candidate * build_builtin_candidate
80 PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
82 static struct z_candidate * add_conv_candidate
83 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree));
84 static struct z_candidate * add_function_candidate
85 (struct z_candidate *, tree, tree, tree, tree, tree, int);
86 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
87 static tree standard_conversion PARAMS ((tree, tree, tree));
88 static tree reference_binding PARAMS ((tree, tree, tree, int));
89 static tree non_reference PARAMS ((tree));
90 static tree build_conv PARAMS ((enum tree_code, tree, tree));
91 static int is_subseq PARAMS ((tree, tree));
92 static tree maybe_handle_ref_bind PARAMS ((tree*));
93 static void maybe_handle_implicit_object PARAMS ((tree*));
94 static struct z_candidate *add_candidate
95 (struct z_candidate *, tree, tree, tree, tree, int);
96 static tree source_type PARAMS ((tree));
97 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
98 static int reference_related_p PARAMS ((tree, tree));
99 static int reference_compatible_p PARAMS ((tree, tree));
100 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
101 static tree direct_reference_binding PARAMS ((tree, tree));
102 static int promoted_arithmetic_type_p PARAMS ((tree));
103 static tree conditional_conversion PARAMS ((tree, tree));
106 build_vfield_ref (datum, type)
109 if (datum == error_mark_node)
110 return error_mark_node;
112 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
113 datum = convert_from_reference (datum);
115 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
116 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
117 datum = convert_to_base (datum, type, /*check_access=*/false);
119 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
120 datum, TYPE_VFIELD (type));
123 /* Build a call to a member of an object. I.e., one that overloads
124 operator ()(), or is a pointer-to-function or pointer-to-method. */
127 build_field_call (tree instance_ptr, tree decl, tree parms)
131 if (decl == error_mark_node || decl == NULL_TREE)
134 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
136 /* If it's a field, try overloading operator (),
137 or calling if the field is a pointer-to-function. */
138 instance = build_indirect_ref (instance_ptr, NULL);
139 instance = build_class_member_access_expr (instance, decl,
140 /*access_path=*/NULL_TREE,
141 /*preserve_reference=*/false);
143 if (instance == error_mark_node)
144 return error_mark_node;
146 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
147 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
148 instance, parms, NULL_TREE);
149 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
150 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
151 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
153 return build_function_call (instance, parms);
159 /* Returns nonzero iff the destructor name specified in NAME
160 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
164 check_dtor_name (basetype, name)
167 name = TREE_OPERAND (name, 0);
169 /* Just accept something we've already complained about. */
170 if (name == error_mark_node)
173 if (TREE_CODE (name) == TYPE_DECL)
174 name = TREE_TYPE (name);
175 else if (TYPE_P (name))
177 else if (TREE_CODE (name) == IDENTIFIER_NODE)
179 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
180 || (TREE_CODE (basetype) == ENUMERAL_TYPE
181 && name == TYPE_IDENTIFIER (basetype)))
184 name = get_type_value (name);
188 template <class T> struct S { ~S(); };
192 NAME will be a class template. */
193 else if (DECL_CLASS_TEMPLATE_P (name))
198 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
203 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
204 This is how virtual function calls are avoided. */
207 build_scoped_method_call (exp, basetype, name, parms)
208 tree exp, basetype, name, parms;
210 /* Because this syntactic form does not allow
211 a pointer to a base class to be `stolen',
212 we need not protect the derived->base conversion
215 @@ But we do have to check access privileges later. */
217 tree type = TREE_TYPE (exp);
219 if (type == error_mark_node
220 || basetype == error_mark_node)
221 return error_mark_node;
223 if (processing_template_decl)
225 if (TREE_CODE (name) == BIT_NOT_EXPR
226 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
228 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
230 name = build_min_nt (BIT_NOT_EXPR, type);
232 name = build_min_nt (SCOPE_REF, basetype, name);
233 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
236 if (TREE_CODE (type) == REFERENCE_TYPE)
237 type = TREE_TYPE (type);
239 if (TREE_CODE (basetype) == TREE_VEC)
242 basetype = BINFO_TYPE (binfo);
247 /* Check the destructor call syntax. */
248 if (TREE_CODE (name) == BIT_NOT_EXPR)
250 /* We can get here if someone writes their destructor call like
251 `obj.NS::~T()'; this isn't really a scoped method call, so hand
253 if (TREE_CODE (basetype) == NAMESPACE_DECL)
254 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
256 if (! check_dtor_name (basetype, name))
257 error ("qualified type `%T' does not match destructor name `~%T'",
258 basetype, TREE_OPERAND (name, 0));
260 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
261 that explicit ~int is caught in the parser; this deals with typedefs
262 and template parms. */
263 if (! IS_AGGR_TYPE (basetype))
265 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
266 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
267 exp, basetype, type);
269 return cp_convert (void_type_node, exp);
273 if (TREE_CODE (basetype) == NAMESPACE_DECL)
275 error ("`%D' is a namespace", basetype);
276 return error_mark_node;
278 if (! is_aggr_type (basetype, 1))
279 return error_mark_node;
281 if (! IS_AGGR_TYPE (type))
283 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
285 return error_mark_node;
288 decl = build_scoped_ref (exp, basetype, &binfo);
292 /* Call to a destructor. */
293 if (TREE_CODE (name) == BIT_NOT_EXPR)
295 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
296 return cp_convert (void_type_node, exp);
298 return build_delete (TREE_TYPE (decl), decl,
299 sfk_complete_destructor,
300 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
304 /* Call to a method. */
305 return build_method_call (decl, name, parms, binfo,
306 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
308 return error_mark_node;
311 /* We want the address of a function or method. We avoid creating a
312 pointer-to-member function. */
315 build_addr_func (function)
318 tree type = TREE_TYPE (function);
320 /* We have to do these by hand to avoid real pointer to member
322 if (TREE_CODE (type) == METHOD_TYPE)
326 type = build_pointer_type (type);
328 if (!cxx_mark_addressable (function))
329 return error_mark_node;
331 addr = build1 (ADDR_EXPR, type, function);
333 /* Address of a static or external variable or function counts
335 if (staticp (function))
336 TREE_CONSTANT (addr) = 1;
341 function = default_conversion (function);
346 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
347 POINTER_TYPE to those. Note, pointer to member function types
348 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
351 build_call (function, parms)
352 tree function, parms;
354 int is_constructor = 0;
361 function = build_addr_func (function);
363 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
365 sorry ("unable to call pointer to member function here");
366 return error_mark_node;
369 fntype = TREE_TYPE (TREE_TYPE (function));
370 result_type = TREE_TYPE (fntype);
372 if (TREE_CODE (function) == ADDR_EXPR
373 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
374 decl = TREE_OPERAND (function, 0);
378 /* We check both the decl and the type; a function may be known not to
379 throw without being declared throw(). */
380 nothrow = ((decl && TREE_NOTHROW (decl))
381 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
383 if (decl && TREE_THIS_VOLATILE (decl))
384 current_function_returns_abnormally = 1;
386 if (decl && TREE_DEPRECATED (decl))
387 warn_deprecated_use (decl);
388 require_complete_eh_spec_types (fntype, decl);
390 if (decl && DECL_CONSTRUCTOR_P (decl))
393 if (decl && ! TREE_USED (decl))
395 /* We invoke build_call directly for several library functions.
396 These may have been declared normally if we're building libgcc,
397 so we can't just check DECL_ARTIFICIAL. */
398 if (DECL_ARTIFICIAL (decl)
399 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
405 /* Don't pass empty class objects by value. This is useful
406 for tags in STL, which are used to control overload resolution.
407 We don't need to handle other cases of copying empty classes. */
408 if (! decl || ! DECL_BUILT_IN (decl))
409 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
410 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
411 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
413 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
414 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
415 TREE_VALUE (tmp), t);
418 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
419 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
420 TREE_TYPE (function) = result_type;
421 TREE_SIDE_EFFECTS (function) = 1;
422 TREE_NOTHROW (function) = nothrow;
427 /* Build something of the form ptr->method (args)
428 or object.method (args). This can also build
429 calls to constructors, and find friends.
431 Member functions always take their class variable
434 INSTANCE is a class instance.
436 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
438 PARMS help to figure out what that NAME really refers to.
440 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
441 down to the real instance type to use for access checking. We need this
442 information to get protected accesses correct. This parameter is used
443 by build_member_call.
445 FLAGS is the logical disjunction of zero or more LOOKUP_
446 flags. See cp-tree.h for more info.
448 If this is all OK, calls build_function_call with the resolved
451 This function must also handle being called to perform
452 initialization, promotion/coercion of arguments, and
453 instantiation of default parameters.
455 Note that NAME may refer to an instance variable name. If
456 `operator()()' is defined for the type of that field, then we return
459 #ifdef GATHER_STATISTICS
460 extern int n_build_method_call;
464 build_method_call (instance, name, parms, basetype_path, flags)
465 tree instance, name, parms, basetype_path;
470 tree template_args = NULL_TREE;
471 bool has_template_args = false;
473 #ifdef GATHER_STATISTICS
474 n_build_method_call++;
477 if (instance == error_mark_node
478 || name == error_mark_node
479 || parms == error_mark_node
480 || (instance && TREE_TYPE (instance) == error_mark_node))
481 return error_mark_node;
483 if (processing_template_decl)
485 /* We need to process template parm names here so that tsubst catches
486 them properly. Other type names can wait. */
487 if (TREE_CODE (name) == BIT_NOT_EXPR)
489 tree type = NULL_TREE;
491 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
492 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
493 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
494 type = TREE_TYPE (TREE_OPERAND (name, 0));
496 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
497 name = build_min_nt (BIT_NOT_EXPR, type);
500 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
503 if (TREE_CODE (instance) == OFFSET_REF)
504 instance = resolve_offset_ref (instance);
505 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
506 instance = convert_from_reference (instance);
507 object_type = TREE_TYPE (instance);
509 if (TREE_CODE (name) == BIT_NOT_EXPR)
514 error ("destructors take no parameters");
516 if (! check_dtor_name (object_type, name))
518 ("destructor name `~%T' does not match type `%T' of expression",
519 TREE_OPERAND (name, 0), object_type);
521 if (! TYPE_HAS_DESTRUCTOR (complete_type (object_type)))
522 return cp_convert (void_type_node, instance);
523 instance = default_conversion (instance);
524 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
525 return build_delete (build_pointer_type (object_type),
526 instance_ptr, sfk_complete_destructor,
527 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
530 if (!CLASS_TYPE_P (object_type))
532 if ((flags & LOOKUP_COMPLAIN)
533 && TREE_TYPE (instance) != error_mark_node)
534 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
535 name, instance, object_type);
536 return error_mark_node;
539 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
541 template_args = TREE_OPERAND (name, 1);
542 has_template_args = true;
543 name = TREE_OPERAND (name, 0);
545 if (TREE_CODE (name) == OVERLOAD)
546 name = DECL_NAME (get_first_fn (name));
547 else if (DECL_P (name))
548 name = DECL_NAME (name);
549 if (has_template_args)
550 fn = lookup_fnfields (object_type, name, /*protect=*/2);
552 fn = lookup_member (object_type, name, /*protect=*/2, /*want_type=*/0);
554 if (fn && TREE_CODE (fn) == TREE_LIST && !BASELINK_P (fn))
556 error ("request for member `%D' is ambiguous", name);
557 print_candidates (fn);
558 return error_mark_node;
561 /* If the name could not be found, issue an error. */
564 unqualified_name_lookup_error (name);
565 return error_mark_node;
568 if (BASELINK_P (fn) && has_template_args)
569 BASELINK_FUNCTIONS (fn)
570 = build_nt (TEMPLATE_ID_EXPR,
571 BASELINK_FUNCTIONS (fn),
573 if (BASELINK_P (fn) && basetype_path)
574 BASELINK_ACCESS_BINFO (fn) = basetype_path;
576 return build_new_method_call (instance, fn, parms,
577 /*conversion_path=*/NULL_TREE, flags);
580 /* New overloading code. */
582 struct z_candidate GTY(()) {
583 /* The FUNCTION_DECL that will be called if this candidate is
584 selected by overload resolution. */
589 /* If FN is a member function, the binfo indicating the path used to
590 qualify the name of FN at the call site. This path is used to
591 determine whether or not FN is accessible if it is selected by
592 overload resolution. The DECL_CONTEXT of FN will always be a
593 (possibly improper) base of this binfo. */
595 /* If FN is a non-static member function, the binfo indicating the
596 subobject to which the `this' pointer should be converted if FN
597 is selected by overload resolution. The type pointed to the by
598 the `this' pointer must correspond to the most derived class
599 indicated by the CONVERSION_PATH. */
600 tree conversion_path;
603 struct z_candidate *next;
606 #define IDENTITY_RANK 0
612 #define ELLIPSIS_RANK 6
615 #define ICS_RANK(NODE) \
616 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
617 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
618 : ICS_USER_FLAG (NODE) ? USER_RANK \
619 : ICS_STD_RANK (NODE))
621 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
623 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
624 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
625 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
626 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
628 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
629 should be created to hold the result of the conversion. */
630 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
632 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
633 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
641 A null pointer constant is an integral constant expression
642 (_expr.const_) rvalue of integer type that evaluates to zero. */
644 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
650 /* Returns non-zero if PARMLIST consists of only default parms and/or
654 sufficient_parms_p (parmlist)
657 for (; parmlist && parmlist != void_list_node;
658 parmlist = TREE_CHAIN (parmlist))
659 if (!TREE_PURPOSE (parmlist))
665 build_conv (code, type, from)
670 int rank = ICS_STD_RANK (from);
672 /* We can't use buildl1 here because CODE could be USER_CONV, which
673 takes two arguments. In that case, the caller is responsible for
674 filling in the second argument. */
675 t = make_node (code);
676 TREE_TYPE (t) = type;
677 TREE_OPERAND (t, 0) = from;
690 if (rank < EXACT_RANK)
696 ICS_STD_RANK (t) = rank;
697 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
698 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
702 /* If T is a REFERENCE_TYPE return the type to which T refers.
703 Otherwise, return T itself. */
709 if (TREE_CODE (t) == REFERENCE_TYPE)
718 if (TREE_CODE (t) == ARRAY_TYPE)
720 return TYPE_MAIN_VARIANT (t);
723 /* Returns the standard conversion path (see [conv]) from type FROM to type
724 TO, if any. For proper handling of null pointer constants, you must
725 also pass the expression EXPR to convert from. */
728 standard_conversion (to, from, expr)
731 enum tree_code fcode, tcode;
735 if (TREE_CODE (to) == REFERENCE_TYPE)
737 if (TREE_CODE (from) == REFERENCE_TYPE)
740 from = TREE_TYPE (from);
742 to = strip_top_quals (to);
743 from = strip_top_quals (from);
745 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
746 && expr && type_unknown_p (expr))
748 expr = instantiate_type (to, expr, tf_none);
749 if (expr == error_mark_node)
751 from = TREE_TYPE (expr);
754 fcode = TREE_CODE (from);
755 tcode = TREE_CODE (to);
757 conv = build1 (IDENTITY_CONV, from, expr);
759 if (fcode == FUNCTION_TYPE)
761 from = build_pointer_type (from);
762 fcode = TREE_CODE (from);
763 conv = build_conv (LVALUE_CONV, from, conv);
765 else if (fcode == ARRAY_TYPE)
767 from = build_pointer_type (TREE_TYPE (from));
768 fcode = TREE_CODE (from);
769 conv = build_conv (LVALUE_CONV, from, conv);
771 else if (fromref || (expr && lvalue_p (expr)))
772 conv = build_conv (RVALUE_CONV, from, conv);
774 /* Allow conversion between `__complex__' data types */
775 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
777 /* The standard conversion sequence to convert FROM to TO is
778 the standard conversion sequence to perform componentwise
780 tree part_conv = standard_conversion
781 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
785 conv = build_conv (TREE_CODE (part_conv), to, conv);
786 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
794 if (same_type_p (from, to))
797 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
798 && expr && null_ptr_cst_p (expr))
800 conv = build_conv (STD_CONV, to, conv);
802 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
803 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
805 /* For backwards brain damage compatibility, allow interconversion of
806 pointers and integers with a pedwarn. */
807 conv = build_conv (STD_CONV, to, conv);
808 ICS_BAD_FLAG (conv) = 1;
810 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
811 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
813 /* For backwards brain damage compatibility, allow interconversion of
814 enums and integers with a pedwarn. */
815 conv = build_conv (STD_CONV, to, conv);
816 ICS_BAD_FLAG (conv) = 1;
818 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
820 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
821 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
823 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
826 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
827 && ufcode != FUNCTION_TYPE)
829 from = build_pointer_type
830 (cp_build_qualified_type (void_type_node,
831 cp_type_quals (TREE_TYPE (from))));
832 conv = build_conv (PTR_CONV, from, conv);
834 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
836 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
837 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
839 if (DERIVED_FROM_P (fbase, tbase)
840 && (same_type_ignoring_top_level_qualifiers_p
841 (TREE_TYPE (TREE_TYPE (from)),
842 TREE_TYPE (TREE_TYPE (to)))))
844 from = build_ptrmem_type (tbase, TREE_TYPE (TREE_TYPE (from)));
845 conv = build_conv (PMEM_CONV, from, conv);
848 else if (IS_AGGR_TYPE (TREE_TYPE (from))
849 && IS_AGGR_TYPE (TREE_TYPE (to)))
851 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
854 cp_build_qualified_type (TREE_TYPE (to),
855 cp_type_quals (TREE_TYPE (from)));
856 from = build_pointer_type (from);
857 conv = build_conv (PTR_CONV, from, conv);
861 if (same_type_p (from, to))
863 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
864 conv = build_conv (QUAL_CONV, to, conv);
865 else if (expr && string_conv_p (to, expr, 0))
866 /* converting from string constant to char *. */
867 conv = build_conv (QUAL_CONV, to, conv);
868 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
870 conv = build_conv (PTR_CONV, to, conv);
871 ICS_BAD_FLAG (conv) = 1;
878 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
880 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
881 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
882 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
883 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
885 if (!DERIVED_FROM_P (fbase, tbase)
886 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
887 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
888 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
889 || cp_type_quals (fbase) != cp_type_quals (tbase))
892 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
893 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
894 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
895 from = build_ptrmemfunc_type (build_pointer_type (from));
896 conv = build_conv (PMEM_CONV, from, conv);
898 else if (tcode == BOOLEAN_TYPE)
900 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
901 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
904 conv = build_conv (STD_CONV, to, conv);
905 if (fcode == POINTER_TYPE
906 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
907 ICS_STD_RANK (conv) = PBOOL_RANK;
909 /* We don't check for ENUMERAL_TYPE here because there are no standard
910 conversions to enum type. */
911 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
912 || tcode == REAL_TYPE)
914 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
916 conv = build_conv (STD_CONV, to, conv);
918 /* Give this a better rank if it's a promotion. */
919 if (to == type_promotes_to (from)
920 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
921 ICS_STD_RANK (conv) = PROMO_RANK;
923 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
924 && is_properly_derived_from (from, to))
926 if (TREE_CODE (conv) == RVALUE_CONV)
927 conv = TREE_OPERAND (conv, 0);
928 conv = build_conv (BASE_CONV, to, conv);
929 /* The derived-to-base conversion indicates the initialization
930 of a parameter with base type from an object of a derived
931 type. A temporary object is created to hold the result of
933 NEED_TEMPORARY_P (conv) = 1;
941 /* Returns non-zero if T1 is reference-related to T2. */
944 reference_related_p (t1, t2)
948 t1 = TYPE_MAIN_VARIANT (t1);
949 t2 = TYPE_MAIN_VARIANT (t2);
953 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
954 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
956 return (same_type_p (t1, t2)
957 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
958 && DERIVED_FROM_P (t1, t2)));
961 /* Returns non-zero if T1 is reference-compatible with T2. */
964 reference_compatible_p (t1, t2)
970 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
971 reference-related to T2 and cv1 is the same cv-qualification as,
972 or greater cv-qualification than, cv2. */
973 return (reference_related_p (t1, t2)
974 && at_least_as_qualified_p (t1, t2));
977 /* Determine whether or not the EXPR (of class type S) can be
978 converted to T as in [over.match.ref]. */
981 convert_class_to_reference (t, s, expr)
989 struct z_candidate *candidates;
990 struct z_candidate *cand;
994 Assuming that "cv1 T" is the underlying type of the reference
995 being initialized, and "cv S" is the type of the initializer
996 expression, with S a class type, the candidate functions are
999 --The conversion functions of S and its base classes are
1000 considered. Those that are not hidden within S and yield type
1001 "reference to cv2 T2", where "cv1 T" is reference-compatible
1002 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
1004 The argument list has one argument, which is the initializer
1009 /* Conceptually, we should take the address of EXPR and put it in
1010 the argument list. Unfortunately, however, that can result in
1011 error messages, which we should not issue now because we are just
1012 trying to find a conversion operator. Therefore, we use NULL,
1013 cast to the appropriate type. */
1014 arglist = build_int_2 (0, 0);
1015 TREE_TYPE (arglist) = build_pointer_type (s);
1016 arglist = build_tree_list (NULL_TREE, arglist);
1018 for (conversions = lookup_conversions (s);
1020 conversions = TREE_CHAIN (conversions))
1022 tree fns = TREE_VALUE (conversions);
1024 for (; fns; fns = OVL_NEXT (fns))
1026 tree f = OVL_CURRENT (fns);
1027 tree t2 = TREE_TYPE (TREE_TYPE (f));
1028 struct z_candidate *old_candidates = candidates;
1030 /* If this is a template function, try to get an exact
1032 if (TREE_CODE (f) == TEMPLATE_DECL)
1035 = add_template_candidate (candidates,
1039 build_reference_type (t),
1041 TREE_PURPOSE (conversions),
1045 if (candidates != old_candidates)
1047 /* Now, see if the conversion function really returns
1048 an lvalue of the appropriate type. From the
1049 point of view of unification, simply returning an
1050 rvalue of the right type is good enough. */
1052 t2 = TREE_TYPE (TREE_TYPE (f));
1053 if (TREE_CODE (t2) != REFERENCE_TYPE
1054 || !reference_compatible_p (t, TREE_TYPE (t2)))
1055 candidates = candidates->next;
1058 else if (TREE_CODE (t2) == REFERENCE_TYPE
1059 && reference_compatible_p (t, TREE_TYPE (t2)))
1061 = add_function_candidate (candidates, f, s, arglist,
1063 TREE_PURPOSE (conversions),
1068 /* If none of the conversion functions worked out, let our caller
1070 if (!any_viable (candidates))
1073 candidates = splice_viable (candidates);
1074 cand = tourney (candidates);
1078 conv = build1 (IDENTITY_CONV, s, expr);
1079 conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1081 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
1082 ICS_USER_FLAG (conv) = 1;
1083 if (cand->viable == -1)
1084 ICS_BAD_FLAG (conv) = 1;
1085 cand->second_conv = conv;
1090 /* A reference of the indicated TYPE is being bound directly to the
1091 expression represented by the implicit conversion sequence CONV.
1092 Return a conversion sequence for this binding. */
1095 direct_reference_binding (type, conv)
1099 tree t = TREE_TYPE (type);
1103 When a parameter of reference type binds directly
1104 (_dcl.init.ref_) to an argument expression, the implicit
1105 conversion sequence is the identity conversion, unless the
1106 argument expression has a type that is a derived class of the
1107 parameter type, in which case the implicit conversion sequence is
1108 a derived-to-base Conversion.
1110 If the parameter binds directly to the result of applying a
1111 conversion function to the argument expression, the implicit
1112 conversion sequence is a user-defined conversion sequence
1113 (_over.ics.user_), with the second standard conversion sequence
1114 either an identity conversion or, if the conversion function
1115 returns an entity of a type that is a derived class of the
1116 parameter type, a derived-to-base conversion. */
1117 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1119 /* Represent the derived-to-base conversion. */
1120 conv = build_conv (BASE_CONV, t, conv);
1121 /* We will actually be binding to the base-class subobject in
1122 the derived class, so we mark this conversion appropriately.
1123 That way, convert_like knows not to generate a temporary. */
1124 NEED_TEMPORARY_P (conv) = 0;
1126 return build_conv (REF_BIND, type, conv);
1129 /* Returns the conversion path from type FROM to reference type TO for
1130 purposes of reference binding. For lvalue binding, either pass a
1131 reference type to FROM or an lvalue expression to EXPR. If the
1132 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1133 the conversion returned. */
1136 reference_binding (rto, rfrom, expr, flags)
1137 tree rto, rfrom, expr;
1140 tree conv = NULL_TREE;
1141 tree to = TREE_TYPE (rto);
1145 cp_lvalue_kind lvalue_p = clk_none;
1147 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1149 expr = instantiate_type (to, expr, tf_none);
1150 if (expr == error_mark_node)
1152 from = TREE_TYPE (expr);
1155 if (TREE_CODE (from) == REFERENCE_TYPE)
1157 /* Anything with reference type is an lvalue. */
1158 lvalue_p = clk_ordinary;
1159 from = TREE_TYPE (from);
1162 lvalue_p = real_lvalue_p (expr);
1164 /* Figure out whether or not the types are reference-related and
1165 reference compatible. We have do do this after stripping
1166 references from FROM. */
1167 related_p = reference_related_p (to, from);
1168 compatible_p = reference_compatible_p (to, from);
1170 if (lvalue_p && compatible_p)
1174 If the initializer expression
1176 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1177 is reference-compatible with "cv2 T2,"
1179 the reference is bound directly to the initializer exprssion
1181 conv = build1 (IDENTITY_CONV, from, expr);
1182 conv = direct_reference_binding (rto, conv);
1183 if ((lvalue_p & clk_bitfield) != 0
1184 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1185 /* For the purposes of overload resolution, we ignore the fact
1186 this expression is a bitfield. (In particular,
1187 [over.ics.ref] says specifically that a function with a
1188 non-const reference parameter is viable even if the
1189 argument is a bitfield.)
1191 However, when we actually call the function we must create
1192 a temporary to which to bind the reference. If the
1193 reference is volatile, or isn't const, then we cannot make
1194 a temporary, so we just issue an error when the conversion
1196 NEED_TEMPORARY_P (conv) = 1;
1199 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1203 If the initializer exprsesion
1205 -- has a class type (i.e., T2 is a class type) can be
1206 implicitly converted to an lvalue of type "cv3 T3," where
1207 "cv1 T1" is reference-compatible with "cv3 T3". (this
1208 conversion is selected by enumerating the applicable
1209 conversion functions (_over.match.ref_) and choosing the
1210 best one through overload resolution. (_over.match_).
1212 the reference is bound to the lvalue result of the conversion
1213 in the second case. */
1214 conv = convert_class_to_reference (to, from, expr);
1216 return direct_reference_binding (rto, conv);
1219 /* From this point on, we conceptually need temporaries, even if we
1220 elide them. Only the cases above are "direct bindings". */
1221 if (flags & LOOKUP_NO_TEMP_BIND)
1226 When a parameter of reference type is not bound directly to an
1227 argument expression, the conversion sequence is the one required
1228 to convert the argument expression to the underlying type of the
1229 reference according to _over.best.ics_. Conceptually, this
1230 conversion sequence corresponds to copy-initializing a temporary
1231 of the underlying type with the argument expression. Any
1232 difference in top-level cv-qualification is subsumed by the
1233 initialization itself and does not constitute a conversion. */
1237 Otherwise, the reference shall be to a non-volatile const type. */
1238 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1243 If the initializer expression is an rvalue, with T2 a class type,
1244 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1245 is bound in one of the following ways:
1247 -- The reference is bound to the object represented by the rvalue
1248 or to a sub-object within that object.
1250 In this case, the implicit conversion sequence is supposed to be
1251 same as we would obtain by generating a temporary. Fortunately,
1252 if the types are reference compatible, then this is either an
1253 identity conversion or the derived-to-base conversion, just as
1254 for direct binding. */
1255 if (CLASS_TYPE_P (from) && compatible_p)
1257 conv = build1 (IDENTITY_CONV, from, expr);
1258 return direct_reference_binding (rto, conv);
1263 Otherwise, a temporary of type "cv1 T1" is created and
1264 initialized from the initializer expression using the rules for a
1265 non-reference copy initialization. If T1 is reference-related to
1266 T2, cv1 must be the same cv-qualification as, or greater
1267 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1268 if (related_p && !at_least_as_qualified_p (to, from))
1271 conv = implicit_conversion (to, from, expr, flags);
1275 conv = build_conv (REF_BIND, rto, conv);
1276 /* This reference binding, unlike those above, requires the
1277 creation of a temporary. */
1278 NEED_TEMPORARY_P (conv) = 1;
1283 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1284 to type TO. The optional expression EXPR may affect the conversion.
1285 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1289 implicit_conversion (to, from, expr, flags)
1290 tree to, from, expr;
1294 struct z_candidate *cand;
1296 /* Resolve expressions like `A::p' that we thought might become
1297 pointers-to-members. */
1298 if (expr && TREE_CODE (expr) == OFFSET_REF)
1300 expr = resolve_offset_ref (expr);
1301 from = TREE_TYPE (expr);
1304 if (from == error_mark_node || to == error_mark_node
1305 || expr == error_mark_node)
1308 /* Make sure both the FROM and TO types are complete so that
1309 user-defined conversions are available. */
1310 complete_type (from);
1313 if (TREE_CODE (to) == REFERENCE_TYPE)
1314 conv = reference_binding (to, from, expr, flags);
1316 conv = standard_conversion (to, from, expr);
1320 else if (expr != NULL_TREE
1321 && (IS_AGGR_TYPE (from)
1322 || IS_AGGR_TYPE (to))
1323 && (flags & LOOKUP_NO_CONVERSION) == 0)
1325 cand = build_user_type_conversion_1
1326 (to, expr, LOOKUP_ONLYCONVERTING);
1328 conv = cand->second_conv;
1330 /* We used to try to bind a reference to a temporary here, but that
1331 is now handled by the recursive call to this function at the end
1332 of reference_binding. */
1338 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1341 static struct z_candidate *
1342 add_candidate (struct z_candidate *candidates,
1343 tree fn, tree convs, tree access_path, tree
1344 conversion_path, int viable)
1346 struct z_candidate *cand
1347 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1350 cand->convs = convs;
1351 cand->access_path = access_path;
1352 cand->conversion_path = conversion_path;
1353 cand->viable = viable;
1354 cand->next = candidates;
1359 /* Create an overload candidate for the function or method FN called with
1360 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1361 to implicit_conversion.
1363 CTYPE, if non-NULL, is the type we want to pretend this function
1364 comes from for purposes of overload resolution. */
1366 static struct z_candidate *
1367 add_function_candidate (struct z_candidate *candidates,
1368 tree fn, tree ctype, tree arglist,
1369 tree access_path, tree conversion_path,
1372 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1375 tree parmnode, argnode;
1378 /* The `this', `in_chrg' and VTT arguments to constructors are not
1379 considered in overload resolution. */
1380 if (DECL_CONSTRUCTOR_P (fn))
1382 parmlist = skip_artificial_parms_for (fn, parmlist);
1383 arglist = skip_artificial_parms_for (fn, arglist);
1386 len = list_length (arglist);
1387 convs = make_tree_vec (len);
1389 /* 13.3.2 - Viable functions [over.match.viable]
1390 First, to be a viable function, a candidate function shall have enough
1391 parameters to agree in number with the arguments in the list.
1393 We need to check this first; otherwise, checking the ICSes might cause
1394 us to produce an ill-formed template instantiation. */
1396 parmnode = parmlist;
1397 for (i = 0; i < len; ++i)
1399 if (parmnode == NULL_TREE || parmnode == void_list_node)
1401 parmnode = TREE_CHAIN (parmnode);
1404 if (i < len && parmnode)
1407 /* Make sure there are default args for the rest of the parms. */
1408 else if (!sufficient_parms_p (parmnode))
1414 /* Second, for F to be a viable function, there shall exist for each
1415 argument an implicit conversion sequence that converts that argument
1416 to the corresponding parameter of F. */
1418 parmnode = parmlist;
1421 for (i = 0; i < len; ++i)
1423 tree arg = TREE_VALUE (argnode);
1424 tree argtype = lvalue_type (arg);
1428 if (parmnode == void_list_node)
1431 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1432 && ! DECL_CONSTRUCTOR_P (fn));
1436 tree parmtype = TREE_VALUE (parmnode);
1438 /* The type of the implicit object parameter ('this') for
1439 overload resolution is not always the same as for the
1440 function itself; conversion functions are considered to
1441 be members of the class being converted, and functions
1442 introduced by a using-declaration are considered to be
1443 members of the class that uses them.
1445 Since build_over_call ignores the ICS for the `this'
1446 parameter, we can just change the parm type. */
1447 if (ctype && is_this)
1450 = build_qualified_type (ctype,
1451 TYPE_QUALS (TREE_TYPE (parmtype)));
1452 parmtype = build_pointer_type (parmtype);
1455 t = implicit_conversion (parmtype, argtype, arg, flags);
1459 t = build1 (IDENTITY_CONV, argtype, arg);
1460 ICS_ELLIPSIS_FLAG (t) = 1;
1464 ICS_THIS_FLAG (t) = 1;
1466 TREE_VEC_ELT (convs, i) = t;
1473 if (ICS_BAD_FLAG (t))
1477 parmnode = TREE_CHAIN (parmnode);
1478 argnode = TREE_CHAIN (argnode);
1482 return add_candidate (candidates, fn, convs, access_path,
1483 conversion_path, viable);
1486 /* Create an overload candidate for the conversion function FN which will
1487 be invoked for expression OBJ, producing a pointer-to-function which
1488 will in turn be called with the argument list ARGLIST, and add it to
1489 CANDIDATES. FLAGS is passed on to implicit_conversion.
1491 Actually, we don't really care about FN; we care about the type it
1492 converts to. There may be multiple conversion functions that will
1493 convert to that type, and we rely on build_user_type_conversion_1 to
1494 choose the best one; so when we create our candidate, we record the type
1495 instead of the function. */
1497 static struct z_candidate *
1498 add_conv_candidate (candidates, fn, obj, arglist, access_path,
1500 struct z_candidate *candidates;
1501 tree fn, obj, arglist;
1503 tree conversion_path;
1505 tree totype = TREE_TYPE (TREE_TYPE (fn));
1506 int i, len, viable, flags;
1507 tree parmlist, convs, parmnode, argnode;
1509 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1510 parmlist = TREE_TYPE (parmlist);
1511 parmlist = TYPE_ARG_TYPES (parmlist);
1513 len = list_length (arglist) + 1;
1514 convs = make_tree_vec (len);
1515 parmnode = parmlist;
1518 flags = LOOKUP_NORMAL;
1520 /* Don't bother looking up the same type twice. */
1521 if (candidates && candidates->fn == totype)
1524 for (i = 0; i < len; ++i)
1526 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1527 tree argtype = lvalue_type (arg);
1531 t = implicit_conversion (totype, argtype, arg, flags);
1532 else if (parmnode == void_list_node)
1535 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1538 t = build1 (IDENTITY_CONV, argtype, arg);
1539 ICS_ELLIPSIS_FLAG (t) = 1;
1542 TREE_VEC_ELT (convs, i) = t;
1546 if (ICS_BAD_FLAG (t))
1553 parmnode = TREE_CHAIN (parmnode);
1554 argnode = TREE_CHAIN (argnode);
1560 if (!sufficient_parms_p (parmnode))
1563 return add_candidate (candidates, totype, convs, access_path,
1564 conversion_path, viable);
1567 static struct z_candidate *
1568 build_builtin_candidate (candidates, fnname, type1, type2,
1569 args, argtypes, flags)
1570 struct z_candidate *candidates;
1571 tree fnname, type1, type2, *args, *argtypes;
1582 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1584 for (i = 0; i < 2; ++i)
1589 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1593 /* We need something for printing the candidate. */
1594 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1596 else if (ICS_BAD_FLAG (t))
1598 TREE_VEC_ELT (convs, i) = t;
1601 /* For COND_EXPR we rearranged the arguments; undo that now. */
1604 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1605 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1606 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1608 TREE_VEC_ELT (convs, 0) = t;
1613 return add_candidate (candidates, fnname, convs,
1614 /*access_path=*/NULL_TREE,
1615 /*conversion_path=*/NULL_TREE,
1623 return COMPLETE_TYPE_P (complete_type (t));
1626 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1629 promoted_arithmetic_type_p (type)
1634 In this section, the term promoted integral type is used to refer
1635 to those integral types which are preserved by integral promotion
1636 (including e.g. int and long but excluding e.g. char).
1637 Similarly, the term promoted arithmetic type refers to promoted
1638 integral types plus floating types. */
1639 return ((INTEGRAL_TYPE_P (type)
1640 && same_type_p (type_promotes_to (type), type))
1641 || TREE_CODE (type) == REAL_TYPE);
1644 /* Create any builtin operator overload candidates for the operator in
1645 question given the converted operand types TYPE1 and TYPE2. The other
1646 args are passed through from add_builtin_candidates to
1647 build_builtin_candidate.
1649 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1650 If CODE is requires candidates operands of the same type of the kind
1651 of which TYPE1 and TYPE2 are, we add both candidates
1652 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1654 static struct z_candidate *
1655 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1656 args, argtypes, flags)
1657 struct z_candidate *candidates;
1658 enum tree_code code, code2;
1659 tree fnname, type1, type2, *args, *argtypes;
1664 case POSTINCREMENT_EXPR:
1665 case POSTDECREMENT_EXPR:
1666 args[1] = integer_zero_node;
1667 type2 = integer_type_node;
1676 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1677 and VQ is either volatile or empty, there exist candidate operator
1678 functions of the form
1679 VQ T& operator++(VQ T&);
1680 T operator++(VQ T&, int);
1681 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1682 type other than bool, and VQ is either volatile or empty, there exist
1683 candidate operator functions of the form
1684 VQ T& operator--(VQ T&);
1685 T operator--(VQ T&, int);
1686 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1687 complete object type, and VQ is either volatile or empty, there exist
1688 candidate operator functions of the form
1689 T*VQ& operator++(T*VQ&);
1690 T*VQ& operator--(T*VQ&);
1691 T* operator++(T*VQ&, int);
1692 T* operator--(T*VQ&, int); */
1694 case POSTDECREMENT_EXPR:
1695 case PREDECREMENT_EXPR:
1696 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1698 case POSTINCREMENT_EXPR:
1699 case PREINCREMENT_EXPR:
1700 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1702 type1 = build_reference_type (type1);
1707 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1708 exist candidate operator functions of the form
1712 8 For every function type T, there exist candidate operator functions of
1714 T& operator*(T*); */
1717 if (TREE_CODE (type1) == POINTER_TYPE
1718 && (TYPE_PTROB_P (type1)
1719 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1723 /* 9 For every type T, there exist candidate operator functions of the form
1726 10For every promoted arithmetic type T, there exist candidate operator
1727 functions of the form
1731 case CONVERT_EXPR: /* unary + */
1732 if (TREE_CODE (type1) == POINTER_TYPE
1733 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1736 if (ARITHMETIC_TYPE_P (type1))
1740 /* 11For every promoted integral type T, there exist candidate operator
1741 functions of the form
1745 if (INTEGRAL_TYPE_P (type1))
1749 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1750 is the same type as C2 or is a derived class of C2, T is a complete
1751 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1752 there exist candidate operator functions of the form
1753 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1754 where CV12 is the union of CV1 and CV2. */
1757 if (TREE_CODE (type1) == POINTER_TYPE
1758 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1760 tree c1 = TREE_TYPE (type1);
1761 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1762 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1763 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1765 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1766 && (TYPE_PTRMEMFUNC_P (type2)
1767 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1772 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1773 didate operator functions of the form
1778 bool operator<(L, R);
1779 bool operator>(L, R);
1780 bool operator<=(L, R);
1781 bool operator>=(L, R);
1782 bool operator==(L, R);
1783 bool operator!=(L, R);
1784 where LR is the result of the usual arithmetic conversions between
1787 14For every pair of types T and I, where T is a cv-qualified or cv-
1788 unqualified complete object type and I is a promoted integral type,
1789 there exist candidate operator functions of the form
1790 T* operator+(T*, I);
1791 T& operator[](T*, I);
1792 T* operator-(T*, I);
1793 T* operator+(I, T*);
1794 T& operator[](I, T*);
1796 15For every T, where T is a pointer to complete object type, there exist
1797 candidate operator functions of the form112)
1798 ptrdiff_t operator-(T, T);
1800 16For every pointer or enumeration type T, there exist candidate operator
1801 functions of the form
1802 bool operator<(T, T);
1803 bool operator>(T, T);
1804 bool operator<=(T, T);
1805 bool operator>=(T, T);
1806 bool operator==(T, T);
1807 bool operator!=(T, T);
1809 17For every pointer to member type T, there exist candidate operator
1810 functions of the form
1811 bool operator==(T, T);
1812 bool operator!=(T, T); */
1815 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1817 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1819 type2 = ptrdiff_type_node;
1823 case TRUNC_DIV_EXPR:
1824 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1830 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1831 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1833 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1834 && null_ptr_cst_p (args[1]))
1839 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1840 && null_ptr_cst_p (args[0]))
1852 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1854 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1856 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1858 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1863 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1871 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1874 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1876 type1 = ptrdiff_type_node;
1879 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1881 type2 = ptrdiff_type_node;
1886 /* 18For every pair of promoted integral types L and R, there exist candi-
1887 date operator functions of the form
1894 where LR is the result of the usual arithmetic conversions between
1897 case TRUNC_MOD_EXPR:
1903 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1907 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1908 type, VQ is either volatile or empty, and R is a promoted arithmetic
1909 type, there exist candidate operator functions of the form
1910 VQ L& operator=(VQ L&, R);
1911 VQ L& operator*=(VQ L&, R);
1912 VQ L& operator/=(VQ L&, R);
1913 VQ L& operator+=(VQ L&, R);
1914 VQ L& operator-=(VQ L&, R);
1916 20For every pair T, VQ), where T is any type and VQ is either volatile
1917 or empty, there exist candidate operator functions of the form
1918 T*VQ& operator=(T*VQ&, T*);
1920 21For every pair T, VQ), where T is a pointer to member type and VQ is
1921 either volatile or empty, there exist candidate operator functions of
1923 VQ T& operator=(VQ T&, T);
1925 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1926 unqualified complete object type, VQ is either volatile or empty, and
1927 I is a promoted integral type, there exist candidate operator func-
1929 T*VQ& operator+=(T*VQ&, I);
1930 T*VQ& operator-=(T*VQ&, I);
1932 23For every triple L, VQ, R), where L is an integral or enumeration
1933 type, VQ is either volatile or empty, and R is a promoted integral
1934 type, there exist candidate operator functions of the form
1936 VQ L& operator%=(VQ L&, R);
1937 VQ L& operator<<=(VQ L&, R);
1938 VQ L& operator>>=(VQ L&, R);
1939 VQ L& operator&=(VQ L&, R);
1940 VQ L& operator^=(VQ L&, R);
1941 VQ L& operator|=(VQ L&, R); */
1948 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1950 type2 = ptrdiff_type_node;
1954 case TRUNC_DIV_EXPR:
1955 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1959 case TRUNC_MOD_EXPR:
1965 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1970 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1972 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1973 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1974 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1975 || ((TYPE_PTRMEMFUNC_P (type1)
1976 || TREE_CODE (type1) == POINTER_TYPE)
1977 && null_ptr_cst_p (args[1])))
1987 type1 = build_reference_type (type1);
1993 For every pair of promoted arithmetic types L and R, there
1994 exist candidate operator functions of the form
1996 LR operator?(bool, L, R);
1998 where LR is the result of the usual arithmetic conversions
1999 between types L and R.
2001 For every type T, where T is a pointer or pointer-to-member
2002 type, there exist candidate operator functions of the form T
2003 operator?(bool, T, T); */
2005 if (promoted_arithmetic_type_p (type1)
2006 && promoted_arithmetic_type_p (type2))
2010 /* Otherwise, the types should be pointers. */
2011 if (!(TREE_CODE (type1) == POINTER_TYPE
2012 || TYPE_PTRMEM_P (type1)
2013 || TYPE_PTRMEMFUNC_P (type1))
2014 || !(TREE_CODE (type2) == POINTER_TYPE
2015 || TYPE_PTRMEM_P (type2)
2016 || TYPE_PTRMEMFUNC_P (type2)))
2019 /* We don't check that the two types are the same; the logic
2020 below will actually create two candidates; one in which both
2021 parameter types are TYPE1, and one in which both parameter
2025 /* These arguments do not make for a valid overloaded operator. */
2032 /* If we're dealing with two pointer types or two enumeral types,
2033 we need candidates for both of them. */
2034 if (type2 && !same_type_p (type1, type2)
2035 && TREE_CODE (type1) == TREE_CODE (type2)
2036 && (TREE_CODE (type1) == REFERENCE_TYPE
2037 || (TREE_CODE (type1) == POINTER_TYPE
2038 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
2039 || TYPE_PTRMEMFUNC_P (type1)
2040 || IS_AGGR_TYPE (type1)
2041 || TREE_CODE (type1) == ENUMERAL_TYPE))
2043 candidates = build_builtin_candidate
2044 (candidates, fnname, type1, type1, args, argtypes, flags);
2045 return build_builtin_candidate
2046 (candidates, fnname, type2, type2, args, argtypes, flags);
2049 return build_builtin_candidate
2050 (candidates, fnname, type1, type2, args, argtypes, flags);
2054 type_decays_to (type)
2057 if (TREE_CODE (type) == ARRAY_TYPE)
2058 return build_pointer_type (TREE_TYPE (type));
2059 if (TREE_CODE (type) == FUNCTION_TYPE)
2060 return build_pointer_type (type);
2064 /* There are three conditions of builtin candidates:
2066 1) bool-taking candidates. These are the same regardless of the input.
2067 2) pointer-pair taking candidates. These are generated for each type
2068 one of the input types converts to.
2069 3) arithmetic candidates. According to the standard, we should generate
2070 all of these, but I'm trying not to...
2072 Here we generate a superset of the possible candidates for this particular
2073 case. That is a subset of the full set the standard defines, plus some
2074 other cases which the standard disallows. add_builtin_candidate will
2075 filter out the invalid set. */
2077 static struct z_candidate *
2078 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2079 struct z_candidate *candidates;
2080 enum tree_code code, code2;
2086 tree type, argtypes[3];
2087 /* TYPES[i] is the set of possible builtin-operator parameter types
2088 we will consider for the Ith argument. These are represented as
2089 a TREE_LIST; the TREE_VALUE of each node is the potential
2093 for (i = 0; i < 3; ++i)
2096 argtypes[i] = lvalue_type (args[i]);
2098 argtypes[i] = NULL_TREE;
2103 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2104 and VQ is either volatile or empty, there exist candidate operator
2105 functions of the form
2106 VQ T& operator++(VQ T&); */
2108 case POSTINCREMENT_EXPR:
2109 case PREINCREMENT_EXPR:
2110 case POSTDECREMENT_EXPR:
2111 case PREDECREMENT_EXPR:
2116 /* 24There also exist candidate operator functions of the form
2117 bool operator!(bool);
2118 bool operator&&(bool, bool);
2119 bool operator||(bool, bool); */
2121 case TRUTH_NOT_EXPR:
2122 return build_builtin_candidate
2123 (candidates, fnname, boolean_type_node,
2124 NULL_TREE, args, argtypes, flags);
2126 case TRUTH_ORIF_EXPR:
2127 case TRUTH_ANDIF_EXPR:
2128 return build_builtin_candidate
2129 (candidates, fnname, boolean_type_node,
2130 boolean_type_node, args, argtypes, flags);
2151 types[0] = types[1] = NULL_TREE;
2153 for (i = 0; i < 2; ++i)
2157 else if (IS_AGGR_TYPE (argtypes[i]))
2161 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2164 convs = lookup_conversions (argtypes[i]);
2166 if (code == COND_EXPR)
2168 if (real_lvalue_p (args[i]))
2169 types[i] = tree_cons
2170 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2172 types[i] = tree_cons
2173 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2179 for (; convs; convs = TREE_CHAIN (convs))
2181 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2184 && (TREE_CODE (type) != REFERENCE_TYPE
2185 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2188 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2189 types[i] = tree_cons (NULL_TREE, type, types[i]);
2191 type = non_reference (type);
2192 if (i != 0 || ! ref1)
2194 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2195 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2196 types[i] = tree_cons (NULL_TREE, type, types[i]);
2197 if (INTEGRAL_TYPE_P (type))
2198 type = type_promotes_to (type);
2201 if (! value_member (type, types[i]))
2202 types[i] = tree_cons (NULL_TREE, type, types[i]);
2207 if (code == COND_EXPR && real_lvalue_p (args[i]))
2208 types[i] = tree_cons
2209 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2210 type = non_reference (argtypes[i]);
2211 if (i != 0 || ! ref1)
2213 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2214 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2215 types[i] = tree_cons (NULL_TREE, type, types[i]);
2216 if (INTEGRAL_TYPE_P (type))
2217 type = type_promotes_to (type);
2219 types[i] = tree_cons (NULL_TREE, type, types[i]);
2223 /* Run through the possible parameter types of both arguments,
2224 creating candidates with those parameter types. */
2225 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2228 for (type = types[1]; type; type = TREE_CHAIN (type))
2229 candidates = add_builtin_candidate
2230 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2231 TREE_VALUE (type), args, argtypes, flags);
2233 candidates = add_builtin_candidate
2234 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2235 NULL_TREE, args, argtypes, flags);
2242 /* If TMPL can be successfully instantiated as indicated by
2243 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2245 TMPL is the template. EXPLICIT_TARGS are any explicit template
2246 arguments. ARGLIST is the arguments provided at the call-site.
2247 The RETURN_TYPE is the desired type for conversion operators. If
2248 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2249 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2250 add_conv_candidate. */
2252 static struct z_candidate*
2253 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2254 arglist, return_type, access_path,
2255 conversion_path, flags, obj, strict)
2256 struct z_candidate *candidates;
2257 tree tmpl, ctype, explicit_targs, arglist, return_type;
2259 tree conversion_path;
2262 unification_kind_t strict;
2264 int ntparms = DECL_NTPARMS (tmpl);
2265 tree targs = make_tree_vec (ntparms);
2266 tree args_without_in_chrg = arglist;
2267 struct z_candidate *cand;
2271 /* We don't do deduction on the in-charge parameter, the VTT
2272 parameter or 'this'. */
2273 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2274 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2276 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2277 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2278 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2279 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2281 i = fn_type_unification (tmpl, explicit_targs, targs,
2282 args_without_in_chrg,
2283 return_type, strict, -1);
2288 fn = instantiate_template (tmpl, targs);
2289 if (fn == error_mark_node)
2294 A member function template is never instantiated to perform the
2295 copy of a class object to an object of its class type.
2297 It's a little unclear what this means; the standard explicitly
2298 does allow a template to be used to copy a class. For example,
2303 template <class T> A(const T&);
2306 void g () { A a (f ()); }
2308 the member template will be used to make the copy. The section
2309 quoted above appears in the paragraph that forbids constructors
2310 whose only parameter is (a possibly cv-qualified variant of) the
2311 class type, and a logical interpretation is that the intent was
2312 to forbid the instantiation of member templates which would then
2314 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2316 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2317 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2322 if (obj != NULL_TREE)
2323 /* Aha, this is a conversion function. */
2324 cand = add_conv_candidate (candidates, fn, obj, access_path,
2325 conversion_path, arglist);
2327 cand = add_function_candidate (candidates, fn, ctype,
2328 arglist, access_path,
2329 conversion_path, flags);
2330 if (DECL_TI_TEMPLATE (fn) != tmpl)
2331 /* This situation can occur if a member template of a template
2332 class is specialized. Then, instantiate_template might return
2333 an instantiation of the specialization, in which case the
2334 DECL_TI_TEMPLATE field will point at the original
2335 specialization. For example:
2337 template <class T> struct S { template <class U> void f(U);
2338 template <> void f(int) {}; };
2342 Here, TMPL will be template <class U> S<double>::f(U).
2343 And, instantiate template will give us the specialization
2344 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2345 for this will point at template <class T> template <> S<T>::f(int),
2346 so that we can find the definition. For the purposes of
2347 overload resolution, however, we want the original TMPL. */
2348 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2350 cand->template = DECL_TEMPLATE_INFO (fn);
2356 static struct z_candidate *
2357 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2358 arglist, return_type, access_path,
2359 conversion_path, flags, strict)
2360 struct z_candidate *candidates;
2361 tree tmpl, ctype, explicit_targs, arglist, return_type;
2363 tree conversion_path;
2365 unification_kind_t strict;
2368 add_template_candidate_real (candidates, tmpl, ctype,
2369 explicit_targs, arglist, return_type,
2370 access_path, conversion_path,
2371 flags, NULL_TREE, strict);
2375 static struct z_candidate *
2376 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type,
2377 access_path, conversion_path)
2378 struct z_candidate *candidates;
2379 tree tmpl, obj, arglist, return_type;
2381 tree conversion_path;
2384 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2385 arglist, return_type, access_path,
2386 conversion_path, 0, obj, DEDUCE_CONV);
2392 struct z_candidate *cands;
2394 for (; cands; cands = cands->next)
2395 if (pedantic ? cands->viable == 1 : cands->viable)
2401 any_strictly_viable (cands)
2402 struct z_candidate *cands;
2404 for (; cands; cands = cands->next)
2405 if (cands->viable == 1)
2410 static struct z_candidate *
2411 splice_viable (cands)
2412 struct z_candidate *cands;
2414 struct z_candidate **p = &cands;
2418 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2431 /* Fix this to work on non-lvalues. */
2432 return build_unary_op (ADDR_EXPR, obj, 0);
2436 print_z_candidates (candidates)
2437 struct z_candidate *candidates;
2439 const char *str = "candidates are:";
2440 for (; candidates; candidates = candidates->next)
2442 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2444 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2445 error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn,
2446 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2447 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2448 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2449 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2450 error ("%s %D(%T, %T) <built-in>", str, candidates->fn,
2451 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2452 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2454 error ("%s %D(%T) <built-in>", str, candidates->fn,
2455 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2457 else if (TYPE_P (candidates->fn))
2458 error ("%s %T <conversion>", str, candidates->fn);
2460 cp_error_at ("%s %+#D%s", str, candidates->fn,
2461 candidates->viable == -1 ? " <near match>" : "");
2466 /* Returns the best overload candidate to perform the requested
2467 conversion. This function is used for three the overloading situations
2468 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2469 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2470 per [dcl.init.ref], so we ignore temporary bindings. */
2472 static struct z_candidate *
2473 build_user_type_conversion_1 (totype, expr, flags)
2477 struct z_candidate *candidates, *cand;
2478 tree fromtype = TREE_TYPE (expr);
2479 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2480 tree args = NULL_TREE;
2481 tree templates = NULL_TREE;
2483 /* We represent conversion within a hierarchy using RVALUE_CONV and
2484 BASE_CONV, as specified by [over.best.ics]; these become plain
2485 constructor calls, as specified in [dcl.init]. */
2486 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2487 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2489 if (IS_AGGR_TYPE (totype))
2490 ctors = lookup_fnfields (TYPE_BINFO (totype),
2491 complete_ctor_identifier,
2494 if (IS_AGGR_TYPE (fromtype))
2495 convs = lookup_conversions (fromtype);
2498 flags |= LOOKUP_NO_CONVERSION;
2504 ctors = BASELINK_FUNCTIONS (ctors);
2506 t = build_int_2 (0, 0);
2507 TREE_TYPE (t) = build_pointer_type (totype);
2508 args = build_tree_list (NULL_TREE, expr);
2509 /* We should never try to call the abstract or base constructor
2511 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2512 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2514 args = tree_cons (NULL_TREE, t, args);
2516 for (; ctors; ctors = OVL_NEXT (ctors))
2518 tree ctor = OVL_CURRENT (ctors);
2519 if (DECL_NONCONVERTING_P (ctor))
2522 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2524 templates = tree_cons (NULL_TREE, ctor, templates);
2526 add_template_candidate (candidates, ctor, totype,
2527 NULL_TREE, args, NULL_TREE,
2528 TYPE_BINFO (totype),
2529 TYPE_BINFO (totype),
2534 candidates = add_function_candidate (candidates, ctor, totype,
2535 args, TYPE_BINFO (totype),
2536 TYPE_BINFO (totype),
2540 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2544 args = build_tree_list (NULL_TREE, build_this (expr));
2546 for (; convs; convs = TREE_CHAIN (convs))
2549 tree conversion_path = TREE_PURPOSE (convs);
2550 int convflags = LOOKUP_NO_CONVERSION;
2552 /* If we are called to convert to a reference type, we are trying to
2553 find an lvalue binding, so don't even consider temporaries. If
2554 we don't find an lvalue binding, the caller will try again to
2555 look for a temporary binding. */
2556 if (TREE_CODE (totype) == REFERENCE_TYPE)
2557 convflags |= LOOKUP_NO_TEMP_BIND;
2559 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2561 tree fn = OVL_CURRENT (fns);
2562 struct z_candidate *old_candidates = candidates;
2564 /* [over.match.funcs] For conversion functions, the function
2565 is considered to be a member of the class of the implicit
2566 object argument for the purpose of defining the type of
2567 the implicit object parameter.
2569 So we pass fromtype as CTYPE to add_*_candidate. */
2571 if (TREE_CODE (fn) == TEMPLATE_DECL)
2573 templates = tree_cons (NULL_TREE, fn, templates);
2575 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2576 args, totype, TYPE_BINFO (fromtype),
2582 candidates = add_function_candidate (candidates, fn, fromtype,
2584 TYPE_BINFO (fromtype),
2588 if (candidates != old_candidates)
2590 tree ics = implicit_conversion
2591 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2594 candidates->second_conv = ics;
2596 if (ics == NULL_TREE)
2597 candidates->viable = 0;
2598 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2599 candidates->viable = -1;
2604 if (! any_viable (candidates))
2607 candidates = splice_viable (candidates);
2608 cand = tourney (candidates);
2612 if (flags & LOOKUP_COMPLAIN)
2614 error ("conversion from `%T' to `%T' is ambiguous",
2616 print_z_candidates (candidates);
2619 cand = candidates; /* any one will do */
2620 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2621 ICS_USER_FLAG (cand->second_conv) = 1;
2622 ICS_BAD_FLAG (cand->second_conv) = 1;
2627 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2628 p = &(TREE_OPERAND (*p, 0));
2632 (DECL_CONSTRUCTOR_P (cand->fn)
2633 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2634 expr, build_zc_wrapper (cand));
2636 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2637 if (cand->viable == -1)
2638 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2644 build_user_type_conversion (totype, expr, flags)
2648 struct z_candidate *cand
2649 = build_user_type_conversion_1 (totype, expr, flags);
2653 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2654 return error_mark_node;
2655 return convert_from_reference (convert_like (cand->second_conv, expr));
2660 /* Do any initial processing on the arguments to a function call. */
2667 for (t = args; t; t = TREE_CHAIN (t))
2669 tree arg = TREE_VALUE (t);
2671 if (arg == error_mark_node)
2672 return error_mark_node;
2673 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2675 error ("invalid use of void expression");
2676 return error_mark_node;
2678 else if (TREE_CODE (arg) == OFFSET_REF)
2679 arg = resolve_offset_ref (arg);
2680 arg = convert_from_reference (arg);
2681 TREE_VALUE (t) = arg;
2686 /* Return an expression for a call to FN (a namespace-scope function)
2690 build_new_function_call (fn, args)
2693 struct z_candidate *candidates = 0, *cand;
2694 tree explicit_targs = NULL_TREE;
2695 int template_only = 0;
2697 /* Check FN and ARGS. */
2698 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2699 || TREE_CODE (fn) == TEMPLATE_DECL
2700 || TREE_CODE (fn) == OVERLOAD
2701 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2703 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2706 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2708 explicit_targs = TREE_OPERAND (fn, 1);
2709 fn = TREE_OPERAND (fn, 0);
2713 if (really_overloaded_fn (fn)
2714 || TREE_CODE (fn) == TEMPLATE_DECL)
2717 tree templates = NULL_TREE;
2719 args = resolve_args (args);
2721 if (args == error_mark_node)
2722 return error_mark_node;
2724 for (t1 = fn; t1; t1 = OVL_NEXT (t1))
2726 tree t = OVL_CURRENT (t1);
2728 if (TREE_CODE (t) == TEMPLATE_DECL)
2730 templates = tree_cons (NULL_TREE, t, templates);
2731 candidates = add_template_candidate
2732 (candidates, t, NULL_TREE, explicit_targs, args,
2734 /*access_path=*/NULL_TREE, /*conversion_path=*/NULL_TREE,
2735 LOOKUP_NORMAL, DEDUCE_CALL);
2737 else if (! template_only)
2738 candidates = add_function_candidate
2739 (candidates, t, NULL_TREE, args, /*access_path=*/NULL_TREE,
2740 /*conversion_path=*/NULL_TREE, LOOKUP_NORMAL);
2743 if (! any_viable (candidates))
2745 if (candidates && ! candidates->next)
2746 return build_function_call (candidates->fn, args);
2747 error ("no matching function for call to `%D(%A)'",
2748 DECL_NAME (OVL_CURRENT (fn)), args);
2750 print_z_candidates (candidates);
2751 return error_mark_node;
2753 candidates = splice_viable (candidates);
2754 cand = tourney (candidates);
2758 error ("call of overloaded `%D(%A)' is ambiguous",
2759 DECL_NAME (OVL_FUNCTION (fn)), args);
2760 print_z_candidates (candidates);
2761 return error_mark_node;
2764 return build_over_call (cand, args, LOOKUP_NORMAL);
2767 /* This is not really overloaded. */
2768 fn = OVL_CURRENT (fn);
2770 return build_function_call (fn, args);
2774 build_object_call (obj, args)
2777 struct z_candidate *candidates = 0, *cand;
2778 tree fns, convs, mem_args = NULL_TREE;
2779 tree type = TREE_TYPE (obj);
2781 if (TYPE_PTRMEMFUNC_P (type))
2783 /* It's no good looking for an overloaded operator() on a
2784 pointer-to-member-function. */
2785 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2786 return error_mark_node;
2789 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2790 if (fns == error_mark_node)
2791 return error_mark_node;
2793 args = resolve_args (args);
2795 if (args == error_mark_node)
2796 return error_mark_node;
2800 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2801 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2803 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2805 tree fn = OVL_CURRENT (fns);
2806 if (TREE_CODE (fn) == TEMPLATE_DECL)
2809 = add_template_candidate (candidates, fn, base, NULL_TREE,
2810 mem_args, NULL_TREE,
2813 LOOKUP_NORMAL, DEDUCE_CALL);
2816 candidates = add_function_candidate
2817 (candidates, fn, base, mem_args, TYPE_BINFO (type),
2818 TYPE_BINFO (type), LOOKUP_NORMAL);
2822 convs = lookup_conversions (type);
2824 for (; convs; convs = TREE_CHAIN (convs))
2826 tree fns = TREE_VALUE (convs);
2827 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2829 if ((TREE_CODE (totype) == POINTER_TYPE
2830 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2831 || (TREE_CODE (totype) == REFERENCE_TYPE
2832 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2833 || (TREE_CODE (totype) == REFERENCE_TYPE
2834 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2835 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2836 for (; fns; fns = OVL_NEXT (fns))
2838 tree fn = OVL_CURRENT (fns);
2839 if (TREE_CODE (fn) == TEMPLATE_DECL)
2841 candidates = (add_template_conv_candidate
2842 (candidates, fn, obj, args, totype,
2843 /*access_path=*/NULL_TREE,
2844 /*conversion_path=*/NULL_TREE));
2847 candidates = add_conv_candidate (candidates, fn, obj, args,
2848 /*conversion_path=*/NULL_TREE,
2849 /*access_path=*/NULL_TREE);
2853 if (! any_viable (candidates))
2855 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2856 print_z_candidates (candidates);
2857 return error_mark_node;
2860 candidates = splice_viable (candidates);
2861 cand = tourney (candidates);
2865 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2866 print_z_candidates (candidates);
2867 return error_mark_node;
2870 /* Since cand->fn will be a type, not a function, for a conversion
2871 function, we must be careful not to unconditionally look at
2873 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2874 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2875 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2877 obj = convert_like_with_context
2878 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2881 return build_function_call (obj, args);
2885 op_error (code, code2, arg1, arg2, arg3, problem)
2886 enum tree_code code, code2;
2887 tree arg1, arg2, arg3;
2888 const char *problem;
2892 if (code == MODIFY_EXPR)
2893 opname = assignment_operator_name_info[code2].name;
2895 opname = operator_name_info[code].name;
2900 error ("%s for `%T ? %T : %T' operator", problem,
2901 error_type (arg1), error_type (arg2), error_type (arg3));
2903 case POSTINCREMENT_EXPR:
2904 case POSTDECREMENT_EXPR:
2905 error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2908 error ("%s for `%T [%T]' operator", problem,
2909 error_type (arg1), error_type (arg2));
2913 error ("%s for `%T %s %T' operator", problem,
2914 error_type (arg1), opname, error_type (arg2));
2916 error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2920 /* Return the implicit conversion sequence that could be used to
2921 convert E1 to E2 in [expr.cond]. */
2924 conditional_conversion (e1, e2)
2928 tree t1 = non_reference (TREE_TYPE (e1));
2929 tree t2 = non_reference (TREE_TYPE (e2));
2934 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2935 implicitly converted (clause _conv_) to the type "reference to
2936 T2", subject to the constraint that in the conversion the
2937 reference must bind directly (_dcl.init.ref_) to E1. */
2938 if (real_lvalue_p (e2))
2940 conv = implicit_conversion (build_reference_type (t2),
2943 LOOKUP_NO_TEMP_BIND);
2950 If E1 and E2 have class type, and the underlying class types are
2951 the same or one is a base class of the other: E1 can be converted
2952 to match E2 if the class of T2 is the same type as, or a base
2953 class of, the class of T1, and the cv-qualification of T2 is the
2954 same cv-qualification as, or a greater cv-qualification than, the
2955 cv-qualification of T1. If the conversion is applied, E1 is
2956 changed to an rvalue of type T2 that still refers to the original
2957 source class object (or the appropriate subobject thereof). */
2958 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2959 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2960 TYPE_MAIN_VARIANT (t1)))
2962 if (at_least_as_qualified_p (t2, t1))
2964 conv = build1 (IDENTITY_CONV, t1, e1);
2965 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2966 TYPE_MAIN_VARIANT (t2)))
2967 conv = build_conv (BASE_CONV, t2, conv);
2976 E1 can be converted to match E2 if E1 can be implicitly converted
2977 to the type that expression E2 would have if E2 were converted to
2978 an rvalue (or the type it has, if E2 is an rvalue). */
2979 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2982 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2983 arguments to the conditional expression. */
2986 build_conditional_expr (arg1, arg2, arg3)
2994 tree result_type = NULL_TREE;
2996 struct z_candidate *candidates = 0;
2997 struct z_candidate *cand;
2999 /* As a G++ extension, the second argument to the conditional can be
3000 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3001 c'.) If the second operand is omitted, make sure it is
3002 calculated only once. */
3006 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3007 arg1 = arg2 = save_expr (arg1);
3012 The first expr ession is implicitly converted to bool (clause
3014 arg1 = cp_convert (boolean_type_node, arg1);
3016 /* If something has already gone wrong, just pass that fact up the
3018 if (arg1 == error_mark_node
3019 || arg2 == error_mark_node
3020 || arg3 == error_mark_node
3021 || TREE_TYPE (arg1) == error_mark_node
3022 || TREE_TYPE (arg2) == error_mark_node
3023 || TREE_TYPE (arg3) == error_mark_node)
3024 return error_mark_node;
3028 If either the second or the third operand has type (possibly
3029 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3030 array-to-pointer (_conv.array_), and function-to-pointer
3031 (_conv.func_) standard conversions are performed on the second
3032 and third operands. */
3033 arg2_type = TREE_TYPE (arg2);
3034 arg3_type = TREE_TYPE (arg3);
3035 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3037 /* Do the conversions. We don't these for `void' type arguments
3038 since it can't have any effect and since decay_conversion
3039 does not handle that case gracefully. */
3040 if (!VOID_TYPE_P (arg2_type))
3041 arg2 = decay_conversion (arg2);
3042 if (!VOID_TYPE_P (arg3_type))
3043 arg3 = decay_conversion (arg3);
3044 arg2_type = TREE_TYPE (arg2);
3045 arg3_type = TREE_TYPE (arg3);
3049 One of the following shall hold:
3051 --The second or the third operand (but not both) is a
3052 throw-expression (_except.throw_); the result is of the
3053 type of the other and is an rvalue.
3055 --Both the second and the third operands have type void; the
3056 result is of type void and is an rvalue. */
3057 if ((TREE_CODE (arg2) == THROW_EXPR)
3058 ^ (TREE_CODE (arg3) == THROW_EXPR))
3059 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
3060 ? arg3_type : arg2_type);
3061 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3062 result_type = void_type_node;
3065 error ("`%E' has type `void' and is not a throw-expression",
3066 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3067 return error_mark_node;
3071 goto valid_operands;
3075 Otherwise, if the second and third operand have different types,
3076 and either has (possibly cv-qualified) class type, an attempt is
3077 made to convert each of those operands to the type of the other. */
3078 else if (!same_type_p (arg2_type, arg3_type)
3079 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3081 tree conv2 = conditional_conversion (arg2, arg3);
3082 tree conv3 = conditional_conversion (arg3, arg2);
3086 If both can be converted, or one can be converted but the
3087 conversion is ambiguous, the program is ill-formed. If
3088 neither can be converted, the operands are left unchanged and
3089 further checking is performed as described below. If exactly
3090 one conversion is possible, that conversion is applied to the
3091 chosen operand and the converted operand is used in place of
3092 the original operand for the remainder of this section. */
3093 if ((conv2 && !ICS_BAD_FLAG (conv2)
3094 && conv3 && !ICS_BAD_FLAG (conv3))
3095 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3096 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3098 error ("operands to ?: have different types");
3099 return error_mark_node;
3101 else if (conv2 && !ICS_BAD_FLAG (conv2))
3103 arg2 = convert_like (conv2, arg2);
3104 arg2 = convert_from_reference (arg2);
3105 /* That may not quite have done the trick. If the two types
3106 are cv-qualified variants of one another, we will have
3107 just used an IDENTITY_CONV. (There's no conversion from
3108 an lvalue of one class type to an lvalue of another type,
3109 even a cv-qualified variant, and we don't want to lose
3110 lvalue-ness here.) So, we manually add a NOP_EXPR here
3112 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3113 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3114 arg2_type = TREE_TYPE (arg2);
3116 else if (conv3 && !ICS_BAD_FLAG (conv3))
3118 arg3 = convert_like (conv3, arg3);
3119 arg3 = convert_from_reference (arg3);
3120 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3121 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3122 arg3_type = TREE_TYPE (arg3);
3128 If the second and third operands are lvalues and have the same
3129 type, the result is of that type and is an lvalue. */
3130 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3131 same_type_p (arg2_type, arg3_type))
3133 result_type = arg2_type;
3134 goto valid_operands;
3139 Otherwise, the result is an rvalue. If the second and third
3140 operand do not have the same type, and either has (possibly
3141 cv-qualified) class type, overload resolution is used to
3142 determine the conversions (if any) to be applied to the operands
3143 (_over.match.oper_, _over.built_). */
3145 if (!same_type_p (arg2_type, arg3_type)
3146 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3151 /* Rearrange the arguments so that add_builtin_candidate only has
3152 to know about two args. In build_builtin_candidates, the
3153 arguments are unscrambled. */
3157 candidates = add_builtin_candidates (candidates,
3160 ansi_opname (COND_EXPR),
3166 If the overload resolution fails, the program is
3168 if (!any_viable (candidates))
3170 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3171 print_z_candidates (candidates);
3172 return error_mark_node;
3174 candidates = splice_viable (candidates);
3175 cand = tourney (candidates);
3178 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3179 print_z_candidates (candidates);
3180 return error_mark_node;
3185 Otherwise, the conversions thus determined are applied, and
3186 the converted operands are used in place of the original
3187 operands for the remainder of this section. */
3188 conv = TREE_VEC_ELT (cand->convs, 0);
3189 arg1 = convert_like (conv, arg1);
3190 conv = TREE_VEC_ELT (cand->convs, 1);
3191 arg2 = convert_like (conv, arg2);
3192 conv = TREE_VEC_ELT (cand->convs, 2);
3193 arg3 = convert_like (conv, arg3);
3198 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3199 and function-to-pointer (_conv.func_) standard conversions are
3200 performed on the second and third operands.
3202 We need to force the lvalue-to-rvalue conversion here for class types,
3203 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3204 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3207 We use ocp_convert rather than build_user_type_conversion because the
3208 latter returns NULL_TREE on failure, while the former gives an error. */
3210 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3211 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3212 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3214 arg2 = decay_conversion (arg2);
3215 arg2_type = TREE_TYPE (arg2);
3217 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3218 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3219 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3221 arg3 = decay_conversion (arg3);
3222 arg3_type = TREE_TYPE (arg3);
3224 if (arg2 == error_mark_node || arg3 == error_mark_node)
3225 return error_mark_node;
3229 After those conversions, one of the following shall hold:
3231 --The second and third operands have the same type; the result is of
3233 if (same_type_p (arg2_type, arg3_type))
3234 result_type = arg2_type;
3237 --The second and third operands have arithmetic or enumeration
3238 type; the usual arithmetic conversions are performed to bring
3239 them to a common type, and the result is of that type. */
3240 else if ((ARITHMETIC_TYPE_P (arg2_type)
3241 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3242 && (ARITHMETIC_TYPE_P (arg3_type)
3243 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3245 /* In this case, there is always a common type. */
3246 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3249 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3250 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3251 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3252 arg2_type, arg3_type);
3253 else if (extra_warnings
3254 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3255 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3256 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3257 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3258 warning ("enumeral and non-enumeral type in conditional expression");
3260 arg2 = perform_implicit_conversion (result_type, arg2);
3261 arg3 = perform_implicit_conversion (result_type, arg3);
3265 --The second and third operands have pointer type, or one has
3266 pointer type and the other is a null pointer constant; pointer
3267 conversions (_conv.ptr_) and qualification conversions
3268 (_conv.qual_) are performed to bring them to their composite
3269 pointer type (_expr.rel_). The result is of the composite
3272 --The second and third operands have pointer to member type, or
3273 one has pointer to member type and the other is a null pointer
3274 constant; pointer to member conversions (_conv.mem_) and
3275 qualification conversions (_conv.qual_) are performed to bring
3276 them to a common type, whose cv-qualification shall match the
3277 cv-qualification of either the second or the third operand.
3278 The result is of the common type. */
3279 else if ((null_ptr_cst_p (arg2)
3280 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3281 || TYPE_PTRMEMFUNC_P (arg3_type)))
3282 || (null_ptr_cst_p (arg3)
3283 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3284 || TYPE_PTRMEMFUNC_P (arg2_type)))
3285 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3286 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3287 || (TYPE_PTRMEMFUNC_P (arg2_type)
3288 && TYPE_PTRMEMFUNC_P (arg3_type)))
3290 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3291 arg3, "conditional expression");
3292 arg2 = perform_implicit_conversion (result_type, arg2);
3293 arg3 = perform_implicit_conversion (result_type, arg3);
3298 error ("operands to ?: have different types");
3299 return error_mark_node;
3303 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3304 /* Expand both sides into the same slot, hopefully the target of the
3305 ?: expression. We used to check for TARGET_EXPRs here, but now we
3306 sometimes wrap them in NOP_EXPRs so the test would fail. */
3307 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3308 result = build_target_expr_with_type (result, result_type);
3310 /* If this expression is an rvalue, but might be mistaken for an
3311 lvalue, we must add a NON_LVALUE_EXPR. */
3312 if (!lvalue_p && real_lvalue_p (result))
3313 result = build1 (NON_LVALUE_EXPR, result_type, result);
3319 build_new_op (code, flags, arg1, arg2, arg3)
3320 enum tree_code code;
3322 tree arg1, arg2, arg3;
3324 struct z_candidate *candidates = 0, *cand;
3325 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3326 enum tree_code code2 = NOP_EXPR;
3327 tree templates = NULL_TREE;
3329 bool viable_candidates;
3331 if (arg1 == error_mark_node
3332 || arg2 == error_mark_node
3333 || arg3 == error_mark_node)
3334 return error_mark_node;
3336 /* This can happen if a template takes all non-type parameters, e.g.
3337 undeclared_template<1, 5, 72>a; */
3338 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3340 error ("`%D' must be declared before use", arg1);
3341 return error_mark_node;
3344 if (code == MODIFY_EXPR)
3346 code2 = TREE_CODE (arg3);
3348 fnname = ansi_assopname (code2);
3351 fnname = ansi_opname (code);
3353 if (TREE_CODE (arg1) == OFFSET_REF)
3354 arg1 = resolve_offset_ref (arg1);
3355 arg1 = convert_from_reference (arg1);
3361 case VEC_DELETE_EXPR:
3363 /* Use build_op_new_call and build_op_delete_call instead. */
3367 return build_object_call (arg1, arg2);
3375 if (TREE_CODE (arg2) == OFFSET_REF)
3376 arg2 = resolve_offset_ref (arg2);
3377 arg2 = convert_from_reference (arg2);
3381 if (TREE_CODE (arg3) == OFFSET_REF)
3382 arg3 = resolve_offset_ref (arg3);
3383 arg3 = convert_from_reference (arg3);
3386 if (code == COND_EXPR)
3388 if (arg2 == NULL_TREE
3389 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3390 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3391 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3392 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3395 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3396 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3399 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3400 arg2 = integer_zero_node;
3402 arglist = NULL_TREE;
3404 arglist = tree_cons (NULL_TREE, arg3, arglist);
3406 arglist = tree_cons (NULL_TREE, arg2, arglist);
3407 arglist = tree_cons (NULL_TREE, arg1, arglist);
3409 fns = lookup_function_nonclass (fnname, arglist);
3411 if (fns && TREE_CODE (fns) == TREE_LIST)
3412 fns = TREE_VALUE (fns);
3413 for (; fns; fns = OVL_NEXT (fns))
3415 tree fn = OVL_CURRENT (fns);
3416 if (TREE_CODE (fn) == TEMPLATE_DECL)
3418 templates = tree_cons (NULL_TREE, fn, templates);
3420 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3421 arglist, TREE_TYPE (fnname),
3422 /*access_path=*/NULL_TREE,
3423 /*conversion_path=*/NULL_TREE,
3424 flags, DEDUCE_CALL);
3427 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3429 /*access_path=*/NULL_TREE,
3430 /*conversion_path=*/NULL_TREE,
3434 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3436 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3437 if (fns == error_mark_node)
3445 tree conversion_path = BASELINK_BINFO (fns);
3447 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3448 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
3450 tree fn = OVL_CURRENT (fns);
3452 tree access_path = TYPE_BINFO (TREE_TYPE (arg1));
3454 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3455 this_arglist = mem_arglist;
3457 this_arglist = arglist;
3459 if (TREE_CODE (fn) == TEMPLATE_DECL)
3461 /* A member template. */
3462 templates = tree_cons (NULL_TREE, fn, templates);
3464 = add_template_candidate (candidates, fn,
3465 BINFO_TYPE (conversion_path),
3467 this_arglist, TREE_TYPE (fnname),
3468 access_path, conversion_path,
3469 flags, DEDUCE_CALL);
3472 candidates = add_function_candidate
3473 (candidates, fn, BINFO_TYPE (conversion_path), this_arglist,
3474 access_path, conversion_path, flags);
3481 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3482 to know about two args; a builtin candidate will always have a first
3483 parameter of type bool. We'll handle that in
3484 build_builtin_candidate. */
3485 if (code == COND_EXPR)
3495 args[2] = NULL_TREE;
3498 candidates = add_builtin_candidates
3499 (candidates, code, code2, fnname, args, flags);
3506 /* For these, the built-in candidates set is empty
3507 [over.match.oper]/3. We don't want non-strict matches
3508 because exact matches are always possible with built-in
3509 operators. The built-in candidate set for COMPONENT_REF
3510 would be empty too, but since there are no such built-in
3511 operators, we accept non-strict matches for them. */
3512 viable_candidates = any_strictly_viable (candidates);
3516 viable_candidates = any_viable (candidates);
3520 if (! viable_candidates)
3524 case POSTINCREMENT_EXPR:
3525 case POSTDECREMENT_EXPR:
3526 /* Look for an `operator++ (int)'. If they didn't have
3527 one, then we fall back to the old way of doing things. */
3528 if (flags & LOOKUP_COMPLAIN)
3529 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3531 operator_name_info[code].name);
3532 if (code == POSTINCREMENT_EXPR)
3533 code = PREINCREMENT_EXPR;
3535 code = PREDECREMENT_EXPR;
3536 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3538 /* The caller will deal with these. */
3547 if (flags & LOOKUP_COMPLAIN)
3549 op_error (code, code2, arg1, arg2, arg3, "no match");
3550 print_z_candidates (candidates);
3552 return error_mark_node;
3554 candidates = splice_viable (candidates);
3555 cand = tourney (candidates);
3559 if (flags & LOOKUP_COMPLAIN)
3561 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3562 print_z_candidates (candidates);
3564 return error_mark_node;
3567 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3569 extern int warn_synth;
3571 && fnname == ansi_assopname (NOP_EXPR)
3572 && DECL_ARTIFICIAL (cand->fn)
3574 && ! candidates->next->next)
3576 warning ("using synthesized `%#D' for copy assignment",
3578 cp_warning_at (" where cfront would use `%#D'",
3580 ? candidates->next->fn
3584 return build_over_call
3586 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3587 ? mem_arglist : arglist,
3591 /* Check for comparison of different enum types. */
3600 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3601 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3602 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3603 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3605 warning ("comparison between `%#T' and `%#T'",
3606 TREE_TYPE (arg1), TREE_TYPE (arg2));
3613 /* We need to strip any leading REF_BIND so that bitfields don't cause
3614 errors. This should not remove any important conversions, because
3615 builtins don't apply to class objects directly. */
3616 conv = TREE_VEC_ELT (cand->convs, 0);
3617 if (TREE_CODE (conv) == REF_BIND)
3618 conv = TREE_OPERAND (conv, 0);
3619 arg1 = convert_like (conv, arg1);
3622 conv = TREE_VEC_ELT (cand->convs, 1);
3623 if (TREE_CODE (conv) == REF_BIND)
3624 conv = TREE_OPERAND (conv, 0);
3625 arg2 = convert_like (conv, arg2);
3629 conv = TREE_VEC_ELT (cand->convs, 2);
3630 if (TREE_CODE (conv) == REF_BIND)
3631 conv = TREE_OPERAND (conv, 0);
3632 arg3 = convert_like (conv, arg3);
3639 return build_modify_expr (arg1, code2, arg2);
3642 return build_indirect_ref (arg1, "unary *");
3647 case TRUNC_DIV_EXPR:
3658 case TRUNC_MOD_EXPR:
3662 case TRUTH_ANDIF_EXPR:
3663 case TRUTH_ORIF_EXPR:
3664 return cp_build_binary_op (code, arg1, arg2);
3669 case TRUTH_NOT_EXPR:
3670 case PREINCREMENT_EXPR:
3671 case POSTINCREMENT_EXPR:
3672 case PREDECREMENT_EXPR:
3673 case POSTDECREMENT_EXPR:
3676 return build_unary_op (code, arg1, candidates != 0);
3679 return build_array_ref (arg1, arg2);
3682 return build_conditional_expr (arg1, arg2, arg3);
3685 return build_m_component_ref
3686 (build_indirect_ref (arg1, NULL), arg2);
3688 /* The caller will deal with these. */
3700 /* Build a call to operator delete. This has to be handled very specially,
3701 because the restrictions on what signatures match are different from all
3702 other call instances. For a normal delete, only a delete taking (void *)
3703 or (void *, size_t) is accepted. For a placement delete, only an exact
3704 match with the placement new is accepted.
3706 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3707 ADDR is the pointer to be deleted.
3708 SIZE is the size of the memory block to be deleted.
3709 FLAGS are the usual overloading flags.
3710 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3713 build_op_delete_call (code, addr, size, flags, placement)
3714 enum tree_code code;
3715 tree addr, size, placement;
3718 tree fn = NULL_TREE;
3719 tree fns, fnname, fntype, argtypes, args, type;
3722 if (addr == error_mark_node)
3723 return error_mark_node;
3725 type = TREE_TYPE (TREE_TYPE (addr));
3726 while (TREE_CODE (type) == ARRAY_TYPE)
3727 type = TREE_TYPE (type);
3729 fnname = ansi_opname (code);
3731 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3734 If the result of the lookup is ambiguous or inaccessible, or if
3735 the lookup selects a placement deallocation function, the
3736 program is ill-formed.
3738 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3740 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3741 if (fns == error_mark_node)
3742 return error_mark_node;
3747 if (fns == NULL_TREE)
3748 fns = lookup_name_nonclass (fnname);
3755 /* Find the allocation function that is being called. */
3756 call_expr = placement;
3757 /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3759 while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3760 call_expr = TREE_OPERAND (call_expr, 1);
3761 /* Extract the function. */
3762 alloc_fn = get_callee_fndecl (call_expr);
3763 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3764 /* Then the second parm type. */
3765 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3766 /* Also the second argument. */
3767 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3771 /* First try it without the size argument. */
3772 argtypes = void_list_node;
3776 /* Strip const and volatile from addr. */
3777 addr = cp_convert (ptr_type_node, addr);
3779 /* We make two tries at finding a matching `operator delete'. On
3780 the first pass, we look for an one-operator (or placement)
3781 operator delete. If we're not doing placement delete, then on
3782 the second pass we look for a two-argument delete. */
3783 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3786 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3788 /* Normal delete; now try to find a match including the size
3790 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3791 tree_cons (NULL_TREE, sizetype,
3793 fntype = build_function_type (void_type_node, argtypes);
3795 /* Go through the `operator delete' functions looking for one
3796 with a matching type. */
3797 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3803 /* Exception specifications on the `delete' operator do not
3805 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3807 /* We also don't compare attributes. We're really just
3808 trying to check the types of the first two parameters. */
3809 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3813 /* If we found a match, we're done. */
3818 /* If we have a matching function, call it. */
3821 /* Make sure we have the actual function, and not an
3823 fn = OVL_CURRENT (fn);
3825 /* If the FN is a member function, make sure that it is
3827 if (DECL_CLASS_SCOPE_P (fn))
3828 enforce_access (type, fn);
3831 args = tree_cons (NULL_TREE, addr, args);
3833 args = tree_cons (NULL_TREE, addr,
3834 build_tree_list (NULL_TREE, size));
3836 return build_function_call (fn, args);
3839 /* If we are doing placement delete we do nothing if we don't find a
3840 matching op delete. */
3844 error ("no suitable `operator delete' for `%T'", type);
3845 return error_mark_node;
3848 /* If the current scope isn't allowed to access DECL along
3849 BASETYPE_PATH, give an error. The most derived class in
3850 BASETYPE_PATH is the one used to qualify DECL. */
3853 enforce_access (basetype_path, decl)
3859 accessible = accessible_p (basetype_path, decl);
3862 if (TREE_PRIVATE (decl))
3863 cp_error_at ("`%+#D' is private", decl);
3864 else if (TREE_PROTECTED (decl))
3865 cp_error_at ("`%+#D' is protected", decl);
3867 cp_error_at ("`%+#D' is inaccessible", decl);
3868 error ("within this context");
3875 /* Perform the conversions in CONVS on the expression EXPR.
3876 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3877 indicates the `this' argument of a method. INNER is non-zero when
3878 being called to continue a conversion chain. It is negative when a
3879 reference binding will be applied, positive otherwise. */
3882 convert_like_real (convs, expr, fn, argnum, inner)
3890 tree totype = TREE_TYPE (convs);
3892 if (ICS_BAD_FLAG (convs)
3893 && TREE_CODE (convs) != USER_CONV
3894 && TREE_CODE (convs) != AMBIG_CONV
3895 && TREE_CODE (convs) != REF_BIND)
3898 for (; t; t = TREE_OPERAND (t, 0))
3900 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3902 expr = convert_like_real (t, expr, fn, argnum, 1);
3905 else if (TREE_CODE (t) == AMBIG_CONV)
3906 return convert_like_real (t, expr, fn, argnum, 1);
3907 else if (TREE_CODE (t) == IDENTITY_CONV)
3910 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3912 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3913 return cp_convert (totype, expr);
3917 expr = dubious_conversion_warnings
3918 (totype, expr, "argument", fn, argnum);
3919 switch (TREE_CODE (convs))
3923 struct z_candidate *cand
3924 = WRAPPER_ZC (TREE_OPERAND (convs, 1));
3925 tree convfn = cand->fn;
3928 if (DECL_CONSTRUCTOR_P (convfn))
3930 tree t = build_int_2 (0, 0);
3931 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3933 args = build_tree_list (NULL_TREE, expr);
3934 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3935 || DECL_HAS_VTT_PARM_P (convfn))
3936 /* We should never try to call the abstract or base constructor
3939 args = tree_cons (NULL_TREE, t, args);
3942 args = build_this (expr);
3943 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3945 /* If this is a constructor or a function returning an aggr type,
3946 we need to build up a TARGET_EXPR. */
3947 if (DECL_CONSTRUCTOR_P (convfn))
3948 expr = build_cplus_new (totype, expr);
3950 /* The result of the call is then used to direct-initialize the object
3951 that is the destination of the copy-initialization. [dcl.init]
3953 Note that this step is not reflected in the conversion sequence;
3954 it affects the semantics when we actually perform the
3955 conversion, but is not considered during overload resolution.
3957 If the target is a class, that means call a ctor. */
3958 if (IS_AGGR_TYPE (totype)
3959 && (inner >= 0 || !lvalue_p (expr)))
3961 savew = warningcount, savee = errorcount;
3962 expr = build_special_member_call
3963 (NULL_TREE, complete_ctor_identifier,
3964 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3965 /* Core issue 84, now a DR, says that we don't allow UDCs
3966 for these args (which deliberately breaks copy-init of an
3967 auto_ptr<Base> from an auto_ptr<Derived>). */
3968 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3970 /* Tell the user where this failing constructor call came from. */
3973 if (warningcount > savew)
3975 (" initializing argument %P of `%D' from result of `%D'",
3976 argnum, fn, convfn);
3977 else if (errorcount > savee)
3979 (" initializing argument %P of `%D' from result of `%D'",
3980 argnum, fn, convfn);
3984 if (warningcount > savew)
3985 warning (" initializing temporary from result of `%D'",
3987 else if (errorcount > savee)
3988 error (" initializing temporary from result of `%D'",
3991 expr = build_cplus_new (totype, expr);
3996 if (type_unknown_p (expr))
3997 expr = instantiate_type (totype, expr, tf_error | tf_warning);
4000 /* Call build_user_type_conversion again for the error. */
4001 return build_user_type_conversion
4002 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
4008 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
4009 TREE_CODE (convs) == REF_BIND ? -1 : 1);
4010 if (expr == error_mark_node)
4011 return error_mark_node;
4013 /* Convert a non-array constant variable to its underlying value, unless we
4014 are about to bind it to a reference, in which case we need to
4015 leave it as an lvalue. */
4016 if (TREE_CODE (convs) != REF_BIND
4017 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4018 expr = decl_constant_value (expr);
4020 switch (TREE_CODE (convs))
4023 if (! IS_AGGR_TYPE (totype))
4025 /* else fall through */
4027 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4029 /* We are going to bind a reference directly to a base-class
4030 subobject of EXPR. */
4031 tree base_ptr = build_pointer_type (totype);
4033 /* Build an expression for `*((base*) &expr)'. */
4034 expr = build_unary_op (ADDR_EXPR, expr, 0);
4035 expr = perform_implicit_conversion (base_ptr, expr);
4036 expr = build_indirect_ref (expr, "implicit conversion");
4040 /* Copy-initialization where the cv-unqualified version of the source
4041 type is the same class as, or a derived class of, the class of the
4042 destination [is treated as direct-initialization]. [dcl.init] */
4043 savew = warningcount, savee = errorcount;
4044 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4045 build_tree_list (NULL_TREE, expr),
4046 TYPE_BINFO (totype),
4047 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
4050 if (warningcount > savew)
4051 warning (" initializing argument %P of `%D'", argnum, fn);
4052 else if (errorcount > savee)
4053 error (" initializing argument %P of `%D'", argnum, fn);
4055 return build_cplus_new (totype, expr);
4059 tree ref_type = totype;
4061 /* If necessary, create a temporary. */
4062 if (NEED_TEMPORARY_P (convs) || !non_cast_lvalue_p (expr))
4064 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4065 expr = build_target_expr_with_type (expr, type);
4068 /* Take the address of the thing to which we will bind the
4070 expr = build_unary_op (ADDR_EXPR, expr, 1);
4071 if (expr == error_mark_node)
4072 return error_mark_node;
4074 /* Convert it to a pointer to the type referred to by the
4075 reference. This will adjust the pointer if a derived to
4076 base conversion is being performed. */
4077 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4079 /* Convert the pointer to the desired reference type. */
4080 expr = build1 (NOP_EXPR, ref_type, expr);
4086 return decay_conversion (expr);
4089 /* Warn about deprecated conversion if appropriate. */
4090 string_conv_p (totype, expr, 1);
4096 return ocp_convert (totype, expr, CONV_IMPLICIT,
4097 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4100 /* ARG is being passed to a varargs function. Perform any conversions
4101 required. Array/function to pointer decay must have already happened.
4102 Return the converted value. */
4105 convert_arg_to_ellipsis (arg)
4108 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4109 && (TYPE_PRECISION (TREE_TYPE (arg))
4110 < TYPE_PRECISION (double_type_node)))
4111 /* Convert `float' to `double'. */
4112 arg = cp_convert (double_type_node, arg);
4114 /* Convert `short' and `char' to full-size `int'. */
4115 arg = default_conversion (arg);
4117 arg = require_complete_type (arg);
4119 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4121 /* Undefined behaviour [expr.call] 5.2.2/7. We used to just warn
4122 here and do a bitwise copy, but now cp_expr_size will abort if we
4124 error ("cannot pass objects of non-POD type `%#T' through `...'",
4126 arg = error_mark_node;
4132 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4135 build_x_va_arg (expr, type)
4139 if (processing_template_decl)
4140 return build_min (VA_ARG_EXPR, type, expr);
4142 type = complete_type_or_else (type, NULL_TREE);
4144 if (expr == error_mark_node || !type)
4145 return error_mark_node;
4147 if (! pod_type_p (type))
4149 /* Undefined behaviour [expr.call] 5.2.2/7. */
4150 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4154 return build_va_arg (expr, type);
4157 /* TYPE has been given to va_arg. Apply the default conversions which
4158 would have happened when passed via ellipsis. Return the promoted
4159 type, or the passed type if there is no change. */
4162 cxx_type_promotes_to (type)
4167 if (TREE_CODE (type) == ARRAY_TYPE)
4168 return build_pointer_type (TREE_TYPE (type));
4170 if (TREE_CODE (type) == FUNCTION_TYPE)
4171 return build_pointer_type (type);
4173 promote = type_promotes_to (type);
4174 if (same_type_p (type, promote))
4180 /* ARG is a default argument expression being passed to a parameter of
4181 the indicated TYPE, which is a parameter to FN. Do any required
4182 conversions. Return the converted value. */
4185 convert_default_arg (type, arg, fn, parmnum)
4191 if (TREE_CODE (arg) == DEFAULT_ARG)
4193 /* When processing the default args for a class, we can find that
4194 there is an ordering constraint, and we call a function who's
4195 default args have not yet been converted. For instance,
4198 void Foo (A const & = A ());
4200 We must process A::A before A::Foo's default arg can be converted.
4201 Remember the dependent function, so do_pending_defargs can retry,
4203 unprocessed_defarg_fn (fn);
4205 /* Don't return error_mark node, as we won't be able to distinguish
4206 genuine errors from this case, and that would lead to repeated
4207 diagnostics. Just make something of the right type. */
4208 return build1 (NOP_EXPR, type, integer_zero_node);
4211 if (fn && DECL_TEMPLATE_INFO (fn))
4212 arg = tsubst_default_argument (fn, type, arg);
4214 arg = break_out_target_exprs (arg);
4216 if (TREE_CODE (arg) == CONSTRUCTOR)
4218 arg = digest_init (type, arg, 0);
4219 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4220 "default argument", fn, parmnum);
4224 /* This could get clobbered by the following call. */
4225 if (TREE_HAS_CONSTRUCTOR (arg))
4226 arg = copy_node (arg);
4228 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4229 "default argument", fn, parmnum);
4230 arg = convert_for_arg_passing (type, arg);
4236 /* Returns the type which will really be used for passing an argument of
4240 type_passed_as (type)
4243 /* Pass classes with copy ctors by invisible reference. */
4244 if (TREE_ADDRESSABLE (type))
4245 type = build_reference_type (type);
4246 else if (PROMOTE_PROTOTYPES
4247 && INTEGRAL_TYPE_P (type)
4248 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4249 type = integer_type_node;
4254 /* Actually perform the appropriate conversion. */
4257 convert_for_arg_passing (type, val)
4260 /* Pass classes with copy ctors by invisible reference. */
4261 if (TREE_ADDRESSABLE (type))
4262 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4263 else if (PROMOTE_PROTOTYPES
4264 && INTEGRAL_TYPE_P (type)
4265 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4266 val = default_conversion (val);
4270 /* Convert VALUE for assignment into inlined parameter PARM. */
4273 cp_convert_parm_for_inlining (parm, value, fn)
4275 tree fn ATTRIBUTE_UNUSED;
4277 /* When inlining, we don't need to mess with invisible references, so
4278 undo the ADDR_EXPR. */
4279 if (TREE_ADDRESSABLE (TREE_TYPE (parm)))
4280 value = build_indirect_ref (value, NULL);
4284 /* Subroutine of the various build_*_call functions. Overload resolution
4285 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4286 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4287 bitmask of various LOOKUP_* flags which apply to the call itself. */
4290 build_over_call (cand, args, flags)
4291 struct z_candidate *cand;
4296 tree convs = cand->convs;
4297 tree converted_args = NULL_TREE;
4298 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4299 tree conv, arg, val;
4303 /* Give any warnings we noticed during overload resolution. */
4305 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4306 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4308 if (DECL_FUNCTION_MEMBER_P (fn))
4309 enforce_access (cand->access_path, fn);
4311 if (args && TREE_CODE (args) != TREE_LIST)
4312 args = build_tree_list (NULL_TREE, args);
4315 /* The implicit parameters to a constructor are not considered by overload
4316 resolution, and must be of the proper type. */
4317 if (DECL_CONSTRUCTOR_P (fn))
4319 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4320 arg = TREE_CHAIN (arg);
4321 parm = TREE_CHAIN (parm);
4322 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4323 /* We should never try to call the abstract constructor. */
4325 if (DECL_HAS_VTT_PARM_P (fn))
4327 converted_args = tree_cons
4328 (NULL_TREE, TREE_VALUE (arg), converted_args);
4329 arg = TREE_CHAIN (arg);
4330 parm = TREE_CHAIN (parm);
4333 /* Bypass access control for 'this' parameter. */
4334 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4336 tree parmtype = TREE_VALUE (parm);
4337 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4340 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4341 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4342 TREE_TYPE (argtype), fn);
4344 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4345 X is called for an object that is not of type X, or of a type
4346 derived from X, the behavior is undefined.
4348 So we can assume that anything passed as 'this' is non-null, and
4349 optimize accordingly. */
4350 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4351 /* Convert to the base in which the function was declared. */
4352 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4353 converted_arg = build_base_path (PLUS_EXPR,
4355 cand->conversion_path,
4357 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4358 parm = TREE_CHAIN (parm);
4359 arg = TREE_CHAIN (arg);
4365 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4367 tree type = TREE_VALUE (parm);
4369 conv = TREE_VEC_ELT (convs, i);
4370 val = convert_like_with_context
4371 (conv, TREE_VALUE (arg), fn, i - is_method);
4373 val = convert_for_arg_passing (type, val);
4374 converted_args = tree_cons (NULL_TREE, val, converted_args);
4377 /* Default arguments */
4378 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4380 = tree_cons (NULL_TREE,
4381 convert_default_arg (TREE_VALUE (parm),
4382 TREE_PURPOSE (parm),
4387 for (; arg; arg = TREE_CHAIN (arg))
4389 = tree_cons (NULL_TREE,
4390 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4393 converted_args = nreverse (converted_args);
4396 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4399 /* Avoid actually calling copy constructors and copy assignment operators,
4402 if (! flag_elide_constructors)
4403 /* Do things the hard way. */;
4404 else if (TREE_VEC_LENGTH (convs) == 1
4405 && DECL_COPY_CONSTRUCTOR_P (fn))
4408 arg = skip_artificial_parms_for (fn, converted_args);
4409 arg = TREE_VALUE (arg);
4411 /* Pull out the real argument, disregarding const-correctness. */
4413 while (TREE_CODE (targ) == NOP_EXPR
4414 || TREE_CODE (targ) == NON_LVALUE_EXPR
4415 || TREE_CODE (targ) == CONVERT_EXPR)
4416 targ = TREE_OPERAND (targ, 0);
4417 if (TREE_CODE (targ) == ADDR_EXPR)
4419 targ = TREE_OPERAND (targ, 0);
4420 if (!same_type_ignoring_top_level_qualifiers_p
4421 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4430 arg = build_indirect_ref (arg, 0);
4432 /* [class.copy]: the copy constructor is implicitly defined even if
4433 the implementation elided its use. */
4434 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4437 /* If we're creating a temp and we already have one, don't create a
4438 new one. If we're not creating a temp but we get one, use
4439 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4440 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4441 temp or an INIT_EXPR otherwise. */
4442 if (integer_zerop (TREE_VALUE (args)))
4444 if (! real_lvalue_p (arg))
4446 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4447 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4449 else if (!real_lvalue_p (arg)
4450 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4453 tree to = stabilize_reference
4454 (build_indirect_ref (TREE_VALUE (args), 0));
4456 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4457 address = build_unary_op (ADDR_EXPR, val, 0);
4458 /* Avoid a warning about this expression, if the address is
4460 TREE_USED (address) = 1;
4464 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4466 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4468 tree to = stabilize_reference
4469 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4471 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4472 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4478 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4480 tree t, *p = &TREE_VALUE (converted_args);
4481 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4482 DECL_VIRTUAL_CONTEXT (fn),
4484 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4486 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4487 if (TREE_SIDE_EFFECTS (*p))
4488 *p = save_expr (*p);
4489 t = build_pointer_type (TREE_TYPE (fn));
4490 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4491 fn = build_java_interface_fn_ref (fn, *p);
4493 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4496 else if (DECL_INLINE (fn))
4497 fn = inline_conversion (fn);
4499 fn = build_addr_func (fn);
4501 /* Recognize certain built-in functions so we can make tree-codes
4502 other than CALL_EXPR. We do this when it enables fold-const.c
4503 to do something useful. */
4505 if (TREE_CODE (fn) == ADDR_EXPR
4506 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4507 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4510 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4515 /* Some built-in function calls will be evaluated at
4516 compile-time in fold (). */
4517 fn = fold (build_call (fn, converted_args));
4518 if (VOID_TYPE_P (TREE_TYPE (fn)))
4520 fn = require_complete_type (fn);
4521 if (fn == error_mark_node)
4522 return error_mark_node;
4523 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4524 fn = build_cplus_new (TREE_TYPE (fn), fn);
4525 return convert_from_reference (fn);
4528 static GTY(()) tree java_iface_lookup_fn;
4530 /* Make an expression which yields the address of the Java interface
4531 method FN. This is achieved by generating a call to libjava's
4532 _Jv_LookupInterfaceMethodIdx(). */
4535 build_java_interface_fn_ref (fn, instance)
4538 tree lookup_args, lookup_fn, method, idx;
4539 tree klass_ref, iface, iface_ref;
4542 if (!java_iface_lookup_fn)
4544 tree endlink = build_void_list_node ();
4545 tree t = tree_cons (NULL_TREE, ptr_type_node,
4546 tree_cons (NULL_TREE, ptr_type_node,
4547 tree_cons (NULL_TREE, java_int_type_node,
4549 java_iface_lookup_fn
4550 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4551 build_function_type (ptr_type_node, t),
4552 0, NOT_BUILT_IN, NULL, NULL_TREE);
4555 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4556 This is the first entry in the vtable. */
4557 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4560 /* Get the java.lang.Class pointer for the interface being called. */
4561 iface = DECL_CONTEXT (fn);
4562 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4563 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4564 || DECL_CONTEXT (iface_ref) != iface)
4566 error ("could not find class$ field in java interface type `%T'",
4568 return error_mark_node;
4570 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4572 /* Determine the itable index of FN. */
4574 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4576 if (!DECL_VIRTUAL_P (method))
4582 idx = build_int_2 (i, 0);
4584 lookup_args = tree_cons (NULL_TREE, klass_ref,
4585 tree_cons (NULL_TREE, iface_ref,
4586 build_tree_list (NULL_TREE, idx)));
4587 lookup_fn = build1 (ADDR_EXPR,
4588 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4589 java_iface_lookup_fn);
4590 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4593 /* Returns the value to use for the in-charge parameter when making a
4594 call to a function with the indicated NAME. */
4597 in_charge_arg_for_name (name)
4600 if (name == base_ctor_identifier
4601 || name == base_dtor_identifier)
4602 return integer_zero_node;
4603 else if (name == complete_ctor_identifier)
4604 return integer_one_node;
4605 else if (name == complete_dtor_identifier)
4606 return integer_two_node;
4607 else if (name == deleting_dtor_identifier)
4608 return integer_three_node;
4610 /* This function should only be called with one of the names listed
4616 /* Build a call to a constructor, destructor, or an assignment
4617 operator for INSTANCE, an expression with class type. NAME
4618 indicates the special member function to call; ARGS are the
4619 arguments. BINFO indicates the base of INSTANCE that is to be
4620 passed as the `this' parameter to the member function called.
4622 FLAGS are the LOOKUP_* flags to use when processing the call.
4624 If NAME indicates a complete object constructor, INSTANCE may be
4625 NULL_TREE. In this case, the caller will call build_cplus_new to
4626 store the newly constructed object into a VAR_DECL. */
4629 build_special_member_call (tree instance, tree name, tree args,
4630 tree binfo, int flags)
4633 /* The type of the subobject to be constructed or destroyed. */
4636 my_friendly_assert (name == complete_ctor_identifier
4637 || name == base_ctor_identifier
4638 || name == complete_dtor_identifier
4639 || name == base_dtor_identifier
4640 || name == deleting_dtor_identifier
4641 || name == ansi_assopname (NOP_EXPR),
4643 my_friendly_assert (binfo != NULL_TREE, 20020712);
4645 class_type = BINFO_TYPE (binfo);
4647 /* Handle the special case where INSTANCE is NULL_TREE. */
4648 if (name == complete_ctor_identifier && !instance)
4650 instance = build_int_2 (0, 0);
4651 TREE_TYPE (instance) = build_pointer_type (class_type);
4652 instance = build1 (INDIRECT_REF, class_type, instance);
4654 else if (name == complete_dtor_identifier
4655 || name == base_dtor_identifier
4656 || name == deleting_dtor_identifier)
4657 my_friendly_assert (args == NULL_TREE, 20020712);
4659 my_friendly_assert (instance != NULL_TREE, 20020712);
4661 /* Resolve the name. */
4662 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4663 return error_mark_node;
4665 fns = lookup_fnfields (binfo, name, 1);
4667 /* When making a call to a constructor or destructor for a subobject
4668 that uses virtual base classes, pass down a pointer to a VTT for
4670 if ((name == base_ctor_identifier
4671 || name == base_dtor_identifier)
4672 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4677 /* If the current function is a complete object constructor
4678 or destructor, then we fetch the VTT directly.
4679 Otherwise, we look it up using the VTT we were given. */
4680 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4681 vtt = decay_conversion (vtt);
4682 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4683 build (EQ_EXPR, boolean_type_node,
4684 current_in_charge_parm, integer_zero_node),
4687 if (TREE_VIA_VIRTUAL (binfo))
4688 binfo = binfo_for_vbase (class_type, current_class_type);
4689 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4690 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4691 BINFO_SUBVTT_INDEX (binfo));
4693 args = tree_cons (NULL_TREE, sub_vtt, args);
4696 return build_new_method_call (instance, fns, args, binfo, flags);
4699 /* Build a call to "INSTANCE.FN (ARGS)". */
4702 build_new_method_call (tree instance, tree fns, tree args,
4703 tree conversion_path, int flags)
4705 struct z_candidate *candidates = 0, *cand;
4706 tree explicit_targs = NULL_TREE;
4707 tree basetype = NULL_TREE;
4710 tree mem_args = NULL_TREE, instance_ptr;
4711 tree name, pretty_name;
4713 tree templates = NULL_TREE;
4715 int template_only = 0;
4717 my_friendly_assert (instance != NULL_TREE, 20020729);
4719 if (instance == error_mark_node || fns == error_mark_node
4720 || args == error_mark_node)
4721 return error_mark_node;
4723 /* Process the argument list. */
4725 args = resolve_args (args);
4726 if (args == error_mark_node)
4727 return error_mark_node;
4729 if (TREE_CODE (instance) == OFFSET_REF)
4730 instance = resolve_offset_ref (instance);
4731 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4732 instance = convert_from_reference (instance);
4733 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4734 instance_ptr = build_this (instance);
4736 if (!BASELINK_P (fns))
4738 call = build_field_call (instance_ptr, fns, args);
4741 error ("call to non-function `%D'", fns);
4742 return error_mark_node;
4745 if (!conversion_path)
4746 conversion_path = BASELINK_BINFO (fns);
4747 access_binfo = BASELINK_ACCESS_BINFO (fns);
4748 optype = BASELINK_OPTYPE (fns);
4749 fns = BASELINK_FUNCTIONS (fns);
4751 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4753 explicit_targs = TREE_OPERAND (fns, 1);
4754 fns = TREE_OPERAND (fns, 0);
4758 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4759 || TREE_CODE (fns) == TEMPLATE_DECL
4760 || TREE_CODE (fns) == OVERLOAD,
4763 /* XXX this should be handled before we get here. */
4764 if (! IS_AGGR_TYPE (basetype))
4766 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4767 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4768 fns, instance, basetype);
4770 return error_mark_node;
4773 name = DECL_NAME (get_first_fn (fns));
4775 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4777 /* Callers should explicitly indicate whether they want to construct
4778 the complete object or just the part without virtual bases. */
4779 my_friendly_assert (name != ctor_identifier, 20000408);
4780 /* Similarly for destructors. */
4781 my_friendly_assert (name != dtor_identifier, 20000408);
4783 if (name == complete_ctor_identifier
4784 || name == base_ctor_identifier)
4785 pretty_name = constructor_name (basetype);
4787 pretty_name = dtor_identifier;
4795 tree class_type = (conversion_path
4796 ? BINFO_TYPE (conversion_path)
4799 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4800 for (fn = fns; fn; fn = OVL_NEXT (fn))
4802 tree t = OVL_CURRENT (fn);
4805 /* We can end up here for copy-init of same or base class. */
4806 if ((flags & LOOKUP_ONLYCONVERTING)
4807 && DECL_NONCONVERTING_P (t))
4810 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4811 this_arglist = mem_args;
4813 this_arglist = args;
4815 if (TREE_CODE (t) == TEMPLATE_DECL)
4817 /* A member template. */
4818 templates = tree_cons (NULL_TREE, t, templates);
4820 add_template_candidate (candidates, t,
4823 this_arglist, optype,
4829 else if (! template_only)
4830 candidates = add_function_candidate (candidates, t,
4839 if (! any_viable (candidates))
4841 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4842 if (flags & LOOKUP_SPECULATIVELY)
4844 if (!COMPLETE_TYPE_P (basetype))
4845 cxx_incomplete_type_error (instance_ptr, basetype);
4847 error ("no matching function for call to `%T::%D(%A)%#V'",
4848 basetype, pretty_name, user_args,
4849 TREE_TYPE (TREE_TYPE (instance_ptr)));
4850 print_z_candidates (candidates);
4851 return error_mark_node;
4853 candidates = splice_viable (candidates);
4854 cand = tourney (candidates);
4858 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4860 print_z_candidates (candidates);
4861 return error_mark_node;
4864 if (DECL_PURE_VIRTUAL_P (cand->fn)
4865 && instance == current_class_ref
4866 && (DECL_CONSTRUCTOR_P (current_function_decl)
4867 || DECL_DESTRUCTOR_P (current_function_decl))
4868 && ! (flags & LOOKUP_NONVIRTUAL)
4869 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4870 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4871 "abstract virtual `%#D' called from constructor"
4872 : "abstract virtual `%#D' called from destructor"),
4874 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4875 && is_dummy_object (instance_ptr))
4877 error ("cannot call member function `%D' without object", cand->fn);
4878 return error_mark_node;
4881 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4882 && resolves_to_fixed_type_p (instance, 0))
4883 flags |= LOOKUP_NONVIRTUAL;
4885 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4886 call = build_over_call (cand, mem_args, flags);
4889 call = build_over_call (cand, args, flags);
4890 /* In an expression of the form `a->f()' where `f' turns out to
4891 be a static member function, `a' is none-the-less evaluated. */
4892 if (instance && TREE_SIDE_EFFECTS (instance))
4893 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4899 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4900 subsequence of ICS2. */
4903 is_subseq (ics1, ics2)
4906 /* We can assume that a conversion of the same code
4907 between the same types indicates a subsequence since we only get
4908 here if the types we are converting from are the same. */
4910 while (TREE_CODE (ics1) == RVALUE_CONV
4911 || TREE_CODE (ics1) == LVALUE_CONV)
4912 ics1 = TREE_OPERAND (ics1, 0);
4916 while (TREE_CODE (ics2) == RVALUE_CONV
4917 || TREE_CODE (ics2) == LVALUE_CONV)
4918 ics2 = TREE_OPERAND (ics2, 0);
4920 if (TREE_CODE (ics2) == USER_CONV
4921 || TREE_CODE (ics2) == AMBIG_CONV
4922 || TREE_CODE (ics2) == IDENTITY_CONV)
4923 /* At this point, ICS1 cannot be a proper subsequence of
4924 ICS2. We can get a USER_CONV when we are comparing the
4925 second standard conversion sequence of two user conversion
4929 ics2 = TREE_OPERAND (ics2, 0);
4931 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4932 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4933 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4934 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4939 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4940 be any _TYPE nodes. */
4943 is_properly_derived_from (derived, base)
4947 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4948 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4951 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4952 considers every class derived from itself. */
4953 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4954 && DERIVED_FROM_P (base, derived));
4957 /* We build the ICS for an implicit object parameter as a pointer
4958 conversion sequence. However, such a sequence should be compared
4959 as if it were a reference conversion sequence. If ICS is the
4960 implicit conversion sequence for an implicit object parameter,
4961 modify it accordingly. */
4964 maybe_handle_implicit_object (ics)
4967 if (ICS_THIS_FLAG (*ics))
4969 /* [over.match.funcs]
4971 For non-static member functions, the type of the
4972 implicit object parameter is "reference to cv X"
4973 where X is the class of which the function is a
4974 member and cv is the cv-qualification on the member
4975 function declaration. */
4977 tree reference_type;
4979 /* The `this' parameter is a pointer to a class type. Make the
4980 implict conversion talk about a reference to that same class
4982 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4983 reference_type = build_reference_type (reference_type);
4985 if (TREE_CODE (t) == QUAL_CONV)
4986 t = TREE_OPERAND (t, 0);
4987 if (TREE_CODE (t) == PTR_CONV)
4988 t = TREE_OPERAND (t, 0);
4989 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4990 t = direct_reference_binding (reference_type, t);
4995 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4996 and return the type to which the reference refers. Otherwise,
4997 leave *ICS unchanged and return NULL_TREE. */
5000 maybe_handle_ref_bind (ics)
5003 if (TREE_CODE (*ics) == REF_BIND)
5005 tree old_ics = *ics;
5006 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5007 *ics = TREE_OPERAND (old_ics, 0);
5008 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5009 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5016 /* Compare two implicit conversion sequences according to the rules set out in
5017 [over.ics.rank]. Return values:
5019 1: ics1 is better than ics2
5020 -1: ics2 is better than ics1
5021 0: ics1 and ics2 are indistinguishable */
5024 compare_ics (ics1, ics2)
5031 tree deref_from_type1 = NULL_TREE;
5032 tree deref_from_type2 = NULL_TREE;
5033 tree deref_to_type1 = NULL_TREE;
5034 tree deref_to_type2 = NULL_TREE;
5037 /* REF_BINDING is non-zero if the result of the conversion sequence
5038 is a reference type. In that case TARGET_TYPE is the
5039 type referred to by the reference. */
5043 /* Handle implicit object parameters. */
5044 maybe_handle_implicit_object (&ics1);
5045 maybe_handle_implicit_object (&ics2);
5047 /* Handle reference parameters. */
5048 target_type1 = maybe_handle_ref_bind (&ics1);
5049 target_type2 = maybe_handle_ref_bind (&ics2);
5053 When comparing the basic forms of implicit conversion sequences (as
5054 defined in _over.best.ics_)
5056 --a standard conversion sequence (_over.ics.scs_) is a better
5057 conversion sequence than a user-defined conversion sequence
5058 or an ellipsis conversion sequence, and
5060 --a user-defined conversion sequence (_over.ics.user_) is a
5061 better conversion sequence than an ellipsis conversion sequence
5062 (_over.ics.ellipsis_). */
5063 rank1 = ICS_RANK (ics1);
5064 rank2 = ICS_RANK (ics2);
5068 else if (rank1 < rank2)
5071 if (rank1 == BAD_RANK)
5073 /* XXX Isn't this an extension? */
5074 /* Both ICS are bad. We try to make a decision based on what
5075 would have happenned if they'd been good. */
5076 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5077 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5079 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5080 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5083 /* We couldn't make up our minds; try to figure it out below. */
5086 if (ICS_ELLIPSIS_FLAG (ics1))
5087 /* Both conversions are ellipsis conversions. */
5090 /* User-defined conversion sequence U1 is a better conversion sequence
5091 than another user-defined conversion sequence U2 if they contain the
5092 same user-defined conversion operator or constructor and if the sec-
5093 ond standard conversion sequence of U1 is better than the second
5094 standard conversion sequence of U2. */
5096 if (ICS_USER_FLAG (ics1))
5100 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5101 if (TREE_CODE (t1) == AMBIG_CONV)
5103 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5104 if (TREE_CODE (t2) == AMBIG_CONV)
5107 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5110 /* We can just fall through here, after setting up
5111 FROM_TYPE1 and FROM_TYPE2. */
5112 from_type1 = TREE_TYPE (t1);
5113 from_type2 = TREE_TYPE (t2);
5117 /* We're dealing with two standard conversion sequences.
5121 Standard conversion sequence S1 is a better conversion
5122 sequence than standard conversion sequence S2 if
5124 --S1 is a proper subsequence of S2 (comparing the conversion
5125 sequences in the canonical form defined by _over.ics.scs_,
5126 excluding any Lvalue Transformation; the identity
5127 conversion sequence is considered to be a subsequence of
5128 any non-identity conversion sequence */
5131 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5132 from_type1 = TREE_OPERAND (from_type1, 0);
5133 from_type1 = TREE_TYPE (from_type1);
5136 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5137 from_type2 = TREE_OPERAND (from_type2, 0);
5138 from_type2 = TREE_TYPE (from_type2);
5141 if (same_type_p (from_type1, from_type2))
5143 if (is_subseq (ics1, ics2))
5145 if (is_subseq (ics2, ics1))
5148 /* Otherwise, one sequence cannot be a subsequence of the other; they
5149 don't start with the same type. This can happen when comparing the
5150 second standard conversion sequence in two user-defined conversion
5157 --the rank of S1 is better than the rank of S2 (by the rules
5160 Standard conversion sequences are ordered by their ranks: an Exact
5161 Match is a better conversion than a Promotion, which is a better
5162 conversion than a Conversion.
5164 Two conversion sequences with the same rank are indistinguishable
5165 unless one of the following rules applies:
5167 --A conversion that is not a conversion of a pointer, or pointer
5168 to member, to bool is better than another conversion that is such
5171 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5172 so that we do not have to check it explicitly. */
5173 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5175 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5178 to_type1 = TREE_TYPE (ics1);
5179 to_type2 = TREE_TYPE (ics2);
5181 if (TYPE_PTR_P (from_type1)
5182 && TYPE_PTR_P (from_type2)
5183 && TYPE_PTR_P (to_type1)
5184 && TYPE_PTR_P (to_type2))
5186 deref_from_type1 = TREE_TYPE (from_type1);
5187 deref_from_type2 = TREE_TYPE (from_type2);
5188 deref_to_type1 = TREE_TYPE (to_type1);
5189 deref_to_type2 = TREE_TYPE (to_type2);
5191 /* The rules for pointers to members A::* are just like the rules
5192 for pointers A*, except opposite: if B is derived from A then
5193 A::* converts to B::*, not vice versa. For that reason, we
5194 switch the from_ and to_ variables here. */
5195 else if (TYPE_PTRMEM_P (from_type1)
5196 && TYPE_PTRMEM_P (from_type2)
5197 && TYPE_PTRMEM_P (to_type1)
5198 && TYPE_PTRMEM_P (to_type2))
5200 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5201 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5202 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5203 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5205 else if (TYPE_PTRMEMFUNC_P (from_type1)
5206 && TYPE_PTRMEMFUNC_P (from_type2)
5207 && TYPE_PTRMEMFUNC_P (to_type1)
5208 && TYPE_PTRMEMFUNC_P (to_type2))
5210 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5211 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5212 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5213 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5216 if (deref_from_type1 != NULL_TREE
5217 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5218 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5220 /* This was one of the pointer or pointer-like conversions.
5224 --If class B is derived directly or indirectly from class A,
5225 conversion of B* to A* is better than conversion of B* to
5226 void*, and conversion of A* to void* is better than
5227 conversion of B* to void*. */
5228 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5229 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5231 if (is_properly_derived_from (deref_from_type1,
5234 else if (is_properly_derived_from (deref_from_type2,
5238 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5239 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5241 if (same_type_p (deref_from_type1, deref_from_type2))
5243 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5245 if (is_properly_derived_from (deref_from_type1,
5249 /* We know that DEREF_TO_TYPE1 is `void' here. */
5250 else if (is_properly_derived_from (deref_from_type1,
5255 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5256 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5260 --If class B is derived directly or indirectly from class A
5261 and class C is derived directly or indirectly from B,
5263 --conversion of C* to B* is better than conversion of C* to
5266 --conversion of B* to A* is better than conversion of C* to
5268 if (same_type_p (deref_from_type1, deref_from_type2))
5270 if (is_properly_derived_from (deref_to_type1,
5273 else if (is_properly_derived_from (deref_to_type2,
5277 else if (same_type_p (deref_to_type1, deref_to_type2))
5279 if (is_properly_derived_from (deref_from_type2,
5282 else if (is_properly_derived_from (deref_from_type1,
5288 else if (CLASS_TYPE_P (non_reference (from_type1))
5289 && same_type_p (from_type1, from_type2))
5291 tree from = non_reference (from_type1);
5295 --binding of an expression of type C to a reference of type
5296 B& is better than binding an expression of type C to a
5297 reference of type A&
5299 --conversion of C to B is better than conversion of C to A, */
5300 if (is_properly_derived_from (from, to_type1)
5301 && is_properly_derived_from (from, to_type2))
5303 if (is_properly_derived_from (to_type1, to_type2))
5305 else if (is_properly_derived_from (to_type2, to_type1))
5309 else if (CLASS_TYPE_P (non_reference (to_type1))
5310 && same_type_p (to_type1, to_type2))
5312 tree to = non_reference (to_type1);
5316 --binding of an expression of type B to a reference of type
5317 A& is better than binding an expression of type C to a
5318 reference of type A&,
5320 --onversion of B to A is better than conversion of C to A */
5321 if (is_properly_derived_from (from_type1, to)
5322 && is_properly_derived_from (from_type2, to))
5324 if (is_properly_derived_from (from_type2, from_type1))
5326 else if (is_properly_derived_from (from_type1, from_type2))
5333 --S1 and S2 differ only in their qualification conversion and yield
5334 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5335 qualification signature of type T1 is a proper subset of the cv-
5336 qualification signature of type T2 */
5337 if (TREE_CODE (ics1) == QUAL_CONV
5338 && TREE_CODE (ics2) == QUAL_CONV
5339 && same_type_p (from_type1, from_type2))
5340 return comp_cv_qual_signature (to_type1, to_type2);
5344 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5345 types to which the references refer are the same type except for
5346 top-level cv-qualifiers, and the type to which the reference
5347 initialized by S2 refers is more cv-qualified than the type to
5348 which the reference initialized by S1 refers */
5350 if (target_type1 && target_type2
5351 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5352 return comp_cv_qualification (target_type2, target_type1);
5354 /* Neither conversion sequence is better than the other. */
5358 /* The source type for this standard conversion sequence. */
5364 for (;; t = TREE_OPERAND (t, 0))
5366 if (TREE_CODE (t) == USER_CONV
5367 || TREE_CODE (t) == AMBIG_CONV
5368 || TREE_CODE (t) == IDENTITY_CONV)
5369 return TREE_TYPE (t);
5374 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5375 a pointer to LOSER and re-running joust to produce the warning if WINNER
5376 is actually used. */
5379 add_warning (winner, loser)
5380 struct z_candidate *winner, *loser;
5382 winner->warnings = tree_cons (NULL_TREE,
5383 build_zc_wrapper (loser),
5387 /* Returns true iff functions are equivalent. Equivalent functions are
5388 not '==' only if one is a function-local extern function or if
5389 both are extern "C". */
5392 equal_functions (fn1, fn2)
5396 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5397 || DECL_EXTERN_C_FUNCTION_P (fn1))
5398 return decls_match (fn1, fn2);
5402 /* Compare two candidates for overloading as described in
5403 [over.match.best]. Return values:
5405 1: cand1 is better than cand2
5406 -1: cand2 is better than cand1
5407 0: cand1 and cand2 are indistinguishable */
5410 joust (cand1, cand2, warn)
5411 struct z_candidate *cand1, *cand2;
5415 int i, off1 = 0, off2 = 0, len;
5417 /* Candidates that involve bad conversions are always worse than those
5419 if (cand1->viable > cand2->viable)
5421 if (cand1->viable < cand2->viable)
5424 /* If we have two pseudo-candidates for conversions to the same type,
5425 or two candidates for the same function, arbitrarily pick one. */
5426 if (cand1->fn == cand2->fn
5427 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5430 /* a viable function F1
5431 is defined to be a better function than another viable function F2 if
5432 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5433 ICSi(F2), and then */
5435 /* for some argument j, ICSj(F1) is a better conversion sequence than
5438 /* For comparing static and non-static member functions, we ignore
5439 the implicit object parameter of the non-static function. The
5440 standard says to pretend that the static function has an object
5441 parm, but that won't work with operator overloading. */
5442 len = TREE_VEC_LENGTH (cand1->convs);
5443 if (len != TREE_VEC_LENGTH (cand2->convs))
5445 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5446 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5448 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5449 && DECL_STATIC_FUNCTION_P (cand2->fn))
5458 for (i = 0; i < len; ++i)
5460 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5461 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5462 int comp = compare_ics (t1, t2);
5467 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5468 && TREE_CODE (t1) == STD_CONV
5469 && TREE_CODE (t2) == STD_CONV
5470 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5471 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5472 && (TYPE_PRECISION (TREE_TYPE (t1))
5473 == TYPE_PRECISION (TREE_TYPE (t2)))
5474 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5475 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5478 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5480 struct z_candidate *w, *l;
5482 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5483 w = cand1, l = cand2;
5485 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5486 w = cand2, l = cand1;
5490 warning ("passing `%T' chooses `%T' over `%T'",
5491 type, type1, type2);
5492 warning (" in call to `%D'", w->fn);
5498 if (winner && comp != winner)
5507 /* warn about confusing overload resolution for user-defined conversions,
5508 either between a constructor and a conversion op, or between two
5510 if (winner && cand1->second_conv
5511 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5512 != DECL_CONSTRUCTOR_P (cand2->fn))
5513 /* Don't warn if the two conv ops convert to the same type... */
5514 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5515 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5516 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5518 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5521 struct z_candidate *w, *l;
5524 w = cand1, l = cand2;
5526 w = cand2, l = cand1;
5527 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5528 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5529 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5530 && (convn = standard_conversion
5531 (TREE_TYPE (TREE_TYPE (l->fn)),
5532 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5533 && TREE_CODE (convn) == QUAL_CONV)
5534 /* Don't complain about `operator char *()' beating
5535 `operator const char *() const'. */;
5538 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5539 if (! DECL_CONSTRUCTOR_P (w->fn))
5540 source = TREE_TYPE (source);
5541 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5542 warning (" for conversion from `%T' to `%T'",
5543 source, TREE_TYPE (w->second_conv));
5544 warning (" because conversion sequence for the argument is better");
5555 F1 is a non-template function and F2 is a template function
5558 if (! cand1->template && cand2->template)
5560 else if (cand1->template && ! cand2->template)
5564 F1 and F2 are template functions and the function template for F1 is
5565 more specialized than the template for F2 according to the partial
5568 if (cand1->template && cand2->template)
5570 winner = more_specialized
5571 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5573 /* Tell the deduction code how many real function arguments
5574 we saw, not counting the implicit 'this' argument. But,
5575 add_function_candidate() suppresses the "this" argument
5578 [temp.func.order]: The presence of unused ellipsis and default
5579 arguments has no effect on the partial ordering of function
5581 TREE_VEC_LENGTH (cand1->convs)
5582 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5583 - DECL_CONSTRUCTOR_P (cand1->fn)));
5590 the context is an initialization by user-defined conversion (see
5591 _dcl.init_ and _over.match.user_) and the standard conversion
5592 sequence from the return type of F1 to the destination type (i.e.,
5593 the type of the entity being initialized) is a better conversion
5594 sequence than the standard conversion sequence from the return type
5595 of F2 to the destination type. */
5597 if (cand1->second_conv)
5599 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5604 /* Check whether we can discard a builtin candidate, either because we
5605 have two identical ones or matching builtin and non-builtin candidates.
5607 (Pedantically in the latter case the builtin which matched the user
5608 function should not be added to the overload set, but we spot it here.
5611 ... the builtin candidates include ...
5612 - do not have the same parameter type list as any non-template
5613 non-member candidate. */
5615 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5616 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5618 for (i = 0; i < len; ++i)
5619 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5620 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5622 if (i == TREE_VEC_LENGTH (cand1->convs))
5624 if (cand1->fn == cand2->fn)
5625 /* Two built-in candidates; arbitrarily pick one. */
5627 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5628 /* cand1 is built-in; prefer cand2. */
5631 /* cand2 is built-in; prefer cand1. */
5636 /* If the two functions are the same (this can happen with declarations
5637 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5638 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5639 && equal_functions (cand1->fn, cand2->fn))
5644 /* Extension: If the worst conversion for one candidate is worse than the
5645 worst conversion for the other, take the first. */
5648 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5649 struct z_candidate *w = 0, *l = 0;
5651 for (i = 0; i < len; ++i)
5653 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5654 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5655 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5656 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5659 winner = 1, w = cand1, l = cand2;
5661 winner = -1, w = cand2, l = cand1;
5666 pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5668 " because worst conversion for the former is better than worst conversion for the latter");
5676 my_friendly_assert (!winner, 20010121);
5680 /* Given a list of candidates for overloading, find the best one, if any.
5681 This algorithm has a worst case of O(2n) (winner is last), and a best
5682 case of O(n/2) (totally ambiguous); much better than a sorting
5685 static struct z_candidate *
5686 tourney (candidates)
5687 struct z_candidate *candidates;
5689 struct z_candidate *champ = candidates, *challenger;
5691 int champ_compared_to_predecessor = 0;
5693 /* Walk through the list once, comparing each current champ to the next
5694 candidate, knocking out a candidate or two with each comparison. */
5696 for (challenger = champ->next; challenger; )
5698 fate = joust (champ, challenger, 0);
5700 challenger = challenger->next;
5705 champ = challenger->next;
5708 champ_compared_to_predecessor = 0;
5713 champ_compared_to_predecessor = 1;
5716 challenger = champ->next;
5720 /* Make sure the champ is better than all the candidates it hasn't yet
5721 been compared to. */
5723 for (challenger = candidates;
5725 && !(champ_compared_to_predecessor && challenger->next == champ);
5726 challenger = challenger->next)
5728 fate = joust (champ, challenger, 0);
5736 /* Returns non-zero if things of type FROM can be converted to TO. */
5739 can_convert (to, from)
5742 return can_convert_arg (to, from, NULL_TREE);
5745 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5748 can_convert_arg (to, from, arg)
5751 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5752 return (t && ! ICS_BAD_FLAG (t));
5755 /* Like can_convert_arg, but allows dubious conversions as well. */
5758 can_convert_arg_bad (to, from, arg)
5761 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5765 /* Convert EXPR to TYPE. Return the converted expression.
5767 Note that we allow bad conversions here because by the time we get to
5768 this point we are committed to doing the conversion. If we end up
5769 doing a bad conversion, convert_like will complain. */
5772 perform_implicit_conversion (type, expr)
5778 if (expr == error_mark_node)
5779 return error_mark_node;
5780 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5784 error ("could not convert `%E' to `%T'", expr, type);
5785 return error_mark_node;
5788 return convert_like (conv, expr);
5791 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5792 initializing a variable of that TYPE. Return the converted
5796 initialize_reference (type, expr)
5802 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5803 if (!conv || ICS_BAD_FLAG (conv))
5805 error ("could not convert `%E' to `%T'", expr, type);
5806 return error_mark_node;
5809 return convert_like (conv, expr);
5812 #include "gt-cp-call.h"