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. */
29 #include "coretypes.h"
39 #include "diagnostic.h"
41 extern int inhibit_warnings;
43 static tree build_field_call (tree, tree, tree);
44 static struct z_candidate * tourney (struct z_candidate *);
45 static int equal_functions (tree, tree);
46 static int joust (struct z_candidate *, struct z_candidate *, bool);
47 static int compare_ics (tree, tree);
48 static tree build_over_call (struct z_candidate *, tree, int);
49 static tree build_java_interface_fn_ref (tree, tree);
50 #define convert_like(CONV, EXPR) \
51 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0)
52 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
53 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0)
54 static tree convert_like_real (tree, tree, tree, int, int);
55 static void op_error (enum tree_code, enum tree_code, tree, tree,
57 static tree build_object_call (tree, tree);
58 static tree resolve_args (tree);
59 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
60 static void print_z_candidates (struct z_candidate *);
61 static tree build_this (tree);
62 static struct z_candidate *splice_viable (struct z_candidate *);
63 static bool any_viable (struct z_candidate *);
64 static bool any_strictly_viable (struct z_candidate *);
65 static struct z_candidate *add_template_candidate
66 (struct z_candidate *, tree, tree, tree, tree, tree,
67 tree, tree, int, unification_kind_t);
68 static struct z_candidate *add_template_candidate_real
69 (struct z_candidate *, tree, tree, tree, tree, tree,
70 tree, tree, int, tree, unification_kind_t);
71 static struct z_candidate *add_template_conv_candidate
72 (struct z_candidate *, tree, tree, tree, tree, tree, tree);
73 static struct z_candidate *add_builtin_candidates
74 (struct z_candidate *, enum tree_code, enum tree_code,
76 static struct z_candidate *add_builtin_candidate
77 (struct z_candidate *, enum tree_code, enum tree_code,
78 tree, tree, tree, tree *, tree *, int);
79 static bool is_complete (tree);
80 static struct z_candidate *build_builtin_candidate
81 (struct z_candidate *, tree, tree, tree, tree *, tree *,
83 static struct z_candidate *add_conv_candidate
84 (struct z_candidate *, tree, tree, tree, tree, tree);
85 static struct z_candidate *add_function_candidate
86 (struct z_candidate *, tree, tree, tree, tree, tree, int);
87 static tree implicit_conversion (tree, tree, tree, int);
88 static tree standard_conversion (tree, tree, tree);
89 static tree reference_binding (tree, tree, tree, int);
90 static tree non_reference (tree);
91 static tree build_conv (enum tree_code, tree, tree);
92 static bool is_subseq (tree, tree);
93 static tree maybe_handle_ref_bind (tree *);
94 static void maybe_handle_implicit_object (tree *);
95 static struct z_candidate *add_candidate
96 (struct z_candidate *, tree, tree, tree, tree, int);
97 static tree source_type (tree);
98 static void add_warning (struct z_candidate *, struct z_candidate *);
99 static bool reference_related_p (tree, tree);
100 static bool reference_compatible_p (tree, tree);
101 static tree convert_class_to_reference (tree, tree, tree);
102 static tree direct_reference_binding (tree, tree);
103 static bool promoted_arithmetic_type_p (tree);
104 static tree conditional_conversion (tree, tree);
105 static tree call_builtin_trap (void);
108 build_vfield_ref (tree datum, tree type)
110 if (datum == error_mark_node)
111 return error_mark_node;
113 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
114 datum = convert_from_reference (datum);
116 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
117 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
118 datum = convert_to_base (datum, type, /*check_access=*/false);
120 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
121 datum, TYPE_VFIELD (type));
124 /* Build a call to a member of an object. I.e., one that overloads
125 operator ()(), or is a pointer-to-function or pointer-to-method. */
128 build_field_call (tree instance_ptr, tree decl, tree parms)
132 if (decl == error_mark_node || decl == NULL_TREE)
135 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
137 /* If it's a field, try overloading operator (),
138 or calling if the field is a pointer-to-function. */
139 instance = build_indirect_ref (instance_ptr, NULL);
140 instance = build_class_member_access_expr (instance, decl,
141 /*access_path=*/NULL_TREE,
142 /*preserve_reference=*/false);
144 if (instance == error_mark_node)
145 return error_mark_node;
147 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
148 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
149 instance, parms, NULL_TREE);
150 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
151 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
152 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
154 return build_function_call (instance, parms);
160 /* Returns nonzero iff the destructor name specified in NAME
161 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
165 check_dtor_name (tree basetype, tree 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 (tree exp, tree basetype, tree name, tree parms)
209 /* Because this syntactic form does not allow
210 a pointer to a base class to be `stolen',
211 we need not protect the derived->base conversion
214 @@ But we do have to check access privileges later. */
216 tree type = TREE_TYPE (exp);
218 if (type == error_mark_node
219 || basetype == error_mark_node)
220 return error_mark_node;
222 if (processing_template_decl)
224 if (TREE_CODE (name) == BIT_NOT_EXPR
225 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
227 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
229 name = build_min_nt (BIT_NOT_EXPR, type);
231 name = build_min_nt (SCOPE_REF, basetype, name);
232 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
235 if (TREE_CODE (type) == REFERENCE_TYPE)
236 type = TREE_TYPE (type);
238 if (TREE_CODE (basetype) == TREE_VEC)
241 basetype = BINFO_TYPE (binfo);
246 /* Check the destructor call syntax. */
247 if (TREE_CODE (name) == BIT_NOT_EXPR)
249 /* We can get here if someone writes their destructor call like
250 `obj.NS::~T()'; this isn't really a scoped method call, so hand
252 if (TREE_CODE (basetype) == NAMESPACE_DECL)
253 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
255 if (! check_dtor_name (basetype, name))
256 error ("qualified type `%T' does not match destructor name `~%T'",
257 basetype, TREE_OPERAND (name, 0));
259 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
260 that explicit ~int is caught in the parser; this deals with typedefs
261 and template parms. */
262 if (! IS_AGGR_TYPE (basetype))
264 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
265 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
266 exp, basetype, type);
268 return cp_convert (void_type_node, exp);
272 if (TREE_CODE (basetype) == NAMESPACE_DECL)
274 error ("`%D' is a namespace", basetype);
275 return error_mark_node;
277 if (! is_aggr_type (basetype, 1))
278 return error_mark_node;
280 if (! IS_AGGR_TYPE (type))
282 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
284 return error_mark_node;
287 decl = build_scoped_ref (exp, basetype, &binfo);
291 /* Call to a destructor. */
292 if (TREE_CODE (name) == BIT_NOT_EXPR)
294 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
295 return cp_convert (void_type_node, exp);
297 return build_delete (TREE_TYPE (decl), decl,
298 sfk_complete_destructor,
299 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
303 /* Call to a method. */
304 return build_method_call (decl, name, parms, binfo,
305 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
307 return error_mark_node;
310 /* We want the address of a function or method. We avoid creating a
311 pointer-to-member function. */
314 build_addr_func (tree function)
316 tree type = TREE_TYPE (function);
318 /* We have to do these by hand to avoid real pointer to member
320 if (TREE_CODE (type) == METHOD_TYPE)
324 type = build_pointer_type (type);
326 if (!cxx_mark_addressable (function))
327 return error_mark_node;
329 addr = build1 (ADDR_EXPR, type, function);
331 /* Address of a static or external variable or function counts
333 if (staticp (function))
334 TREE_CONSTANT (addr) = 1;
339 function = default_conversion (function);
344 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
345 POINTER_TYPE to those. Note, pointer to member function types
346 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
349 build_call (tree function, tree parms)
351 int is_constructor = 0;
358 function = build_addr_func (function);
360 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
362 sorry ("unable to call pointer to member function here");
363 return error_mark_node;
366 fntype = TREE_TYPE (TREE_TYPE (function));
367 result_type = TREE_TYPE (fntype);
369 if (TREE_CODE (function) == ADDR_EXPR
370 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
371 decl = TREE_OPERAND (function, 0);
375 /* We check both the decl and the type; a function may be known not to
376 throw without being declared throw(). */
377 nothrow = ((decl && TREE_NOTHROW (decl))
378 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
380 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
381 current_function_returns_abnormally = 1;
383 if (decl && TREE_DEPRECATED (decl))
384 warn_deprecated_use (decl);
385 require_complete_eh_spec_types (fntype, decl);
387 if (decl && DECL_CONSTRUCTOR_P (decl))
390 if (decl && ! TREE_USED (decl))
392 /* We invoke build_call directly for several library functions.
393 These may have been declared normally if we're building libgcc,
394 so we can't just check DECL_ARTIFICIAL. */
395 if (DECL_ARTIFICIAL (decl)
396 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
402 /* Don't pass empty class objects by value. This is useful
403 for tags in STL, which are used to control overload resolution.
404 We don't need to handle other cases of copying empty classes. */
405 if (! decl || ! DECL_BUILT_IN (decl))
406 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
407 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
408 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
410 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
411 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
412 TREE_VALUE (tmp), t);
415 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
416 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
417 TREE_TYPE (function) = result_type;
418 TREE_SIDE_EFFECTS (function) = 1;
419 TREE_NOTHROW (function) = nothrow;
424 /* Build something of the form ptr->method (args)
425 or object.method (args). This can also build
426 calls to constructors, and find friends.
428 Member functions always take their class variable
431 INSTANCE is a class instance.
433 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
435 PARMS help to figure out what that NAME really refers to.
437 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
438 down to the real instance type to use for access checking. We need this
439 information to get protected accesses correct. This parameter is used
440 by build_member_call.
442 FLAGS is the logical disjunction of zero or more LOOKUP_
443 flags. See cp-tree.h for more info.
445 If this is all OK, calls build_function_call with the resolved
448 This function must also handle being called to perform
449 initialization, promotion/coercion of arguments, and
450 instantiation of default parameters.
452 Note that NAME may refer to an instance variable name. If
453 `operator()()' is defined for the type of that field, then we return
456 #ifdef GATHER_STATISTICS
457 extern int n_build_method_call;
461 build_method_call (tree instance, tree name, tree parms,
462 tree basetype_path, int flags)
466 tree template_args = NULL_TREE;
467 bool has_template_args = false;
469 #ifdef GATHER_STATISTICS
470 n_build_method_call++;
473 if (instance == error_mark_node
474 || name == error_mark_node
475 || parms == error_mark_node
476 || (instance && TREE_TYPE (instance) == error_mark_node))
477 return error_mark_node;
479 if (processing_template_decl)
481 /* We need to process template parm names here so that tsubst catches
482 them properly. Other type names can wait. */
483 if (TREE_CODE (name) == BIT_NOT_EXPR)
485 tree type = NULL_TREE;
487 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
488 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
489 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
490 type = TREE_TYPE (TREE_OPERAND (name, 0));
492 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
493 name = build_min_nt (BIT_NOT_EXPR, type);
496 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
499 if (TREE_CODE (instance) == OFFSET_REF)
500 instance = resolve_offset_ref (instance);
501 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
502 instance = convert_from_reference (instance);
503 object_type = TREE_TYPE (instance);
505 if (TREE_CODE (name) == BIT_NOT_EXPR)
510 error ("destructors take no parameters");
512 if (! check_dtor_name (object_type, name))
514 ("destructor name `~%T' does not match type `%T' of expression",
515 TREE_OPERAND (name, 0), object_type);
517 if (! TYPE_HAS_DESTRUCTOR (complete_type (object_type)))
518 return cp_convert (void_type_node, instance);
519 instance = default_conversion (instance);
520 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
521 return build_delete (build_pointer_type (object_type),
522 instance_ptr, sfk_complete_destructor,
523 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
526 if (!CLASS_TYPE_P (object_type))
528 if ((flags & LOOKUP_COMPLAIN)
529 && TREE_TYPE (instance) != error_mark_node)
530 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
531 name, instance, object_type);
532 return error_mark_node;
535 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
537 template_args = TREE_OPERAND (name, 1);
538 has_template_args = true;
539 name = TREE_OPERAND (name, 0);
541 if (TREE_CODE (name) == OVERLOAD)
542 name = DECL_NAME (get_first_fn (name));
543 else if (DECL_P (name))
544 name = DECL_NAME (name);
545 if (has_template_args)
546 fn = lookup_fnfields (object_type, name, /*protect=*/2);
548 fn = lookup_member (object_type, name, /*protect=*/2, /*want_type=*/0);
550 if (fn && TREE_CODE (fn) == TREE_LIST && !BASELINK_P (fn))
552 error ("request for member `%D' is ambiguous", name);
553 print_candidates (fn);
554 return error_mark_node;
557 /* If the name could not be found, issue an error. */
560 unqualified_name_lookup_error (name);
561 return error_mark_node;
564 if (BASELINK_P (fn) && has_template_args)
565 BASELINK_FUNCTIONS (fn)
566 = build_nt (TEMPLATE_ID_EXPR,
567 BASELINK_FUNCTIONS (fn),
569 if (BASELINK_P (fn) && basetype_path)
570 BASELINK_ACCESS_BINFO (fn) = basetype_path;
572 return build_new_method_call (instance, fn, parms,
573 /*conversion_path=*/NULL_TREE, flags);
576 /* New overloading code. */
578 struct z_candidate GTY(()) {
579 /* The FUNCTION_DECL that will be called if this candidate is
580 selected by overload resolution. */
585 /* If FN is a member function, the binfo indicating the path used to
586 qualify the name of FN at the call site. This path is used to
587 determine whether or not FN is accessible if it is selected by
588 overload resolution. The DECL_CONTEXT of FN will always be a
589 (possibly improper) base of this binfo. */
591 /* If FN is a non-static member function, the binfo indicating the
592 subobject to which the `this' pointer should be converted if FN
593 is selected by overload resolution. The type pointed to the by
594 the `this' pointer must correspond to the most derived class
595 indicated by the CONVERSION_PATH. */
596 tree conversion_path;
599 struct z_candidate *next;
602 #define IDENTITY_RANK 0
608 #define ELLIPSIS_RANK 6
611 #define ICS_RANK(NODE) \
612 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
613 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
614 : ICS_USER_FLAG (NODE) ? USER_RANK \
615 : ICS_STD_RANK (NODE))
617 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
619 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
620 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
621 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
622 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
624 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
625 should be created to hold the result of the conversion. */
626 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
628 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
629 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
632 null_ptr_cst_p (tree t)
636 A null pointer constant is an integral constant expression
637 (_expr.const_) rvalue of integer type that evaluates to zero. */
639 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
645 /* Returns nonzero if PARMLIST consists of only default parms and/or
649 sufficient_parms_p (tree parmlist)
651 for (; parmlist && parmlist != void_list_node;
652 parmlist = TREE_CHAIN (parmlist))
653 if (!TREE_PURPOSE (parmlist))
659 build_conv (enum tree_code code, tree type, tree from)
662 int rank = ICS_STD_RANK (from);
664 /* We can't use buildl1 here because CODE could be USER_CONV, which
665 takes two arguments. In that case, the caller is responsible for
666 filling in the second argument. */
667 t = make_node (code);
668 TREE_TYPE (t) = type;
669 TREE_OPERAND (t, 0) = from;
682 if (rank < EXACT_RANK)
688 ICS_STD_RANK (t) = rank;
689 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
690 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
694 /* If T is a REFERENCE_TYPE return the type to which T refers.
695 Otherwise, return T itself. */
698 non_reference (tree t)
700 if (TREE_CODE (t) == REFERENCE_TYPE)
706 strip_top_quals (tree t)
708 if (TREE_CODE (t) == ARRAY_TYPE)
710 return TYPE_MAIN_VARIANT (t);
713 /* Returns the standard conversion path (see [conv]) from type FROM to type
714 TO, if any. For proper handling of null pointer constants, you must
715 also pass the expression EXPR to convert from. */
718 standard_conversion (tree to, tree from, tree expr)
720 enum tree_code fcode, tcode;
722 bool fromref = false;
724 if (TREE_CODE (to) == REFERENCE_TYPE)
726 if (TREE_CODE (from) == REFERENCE_TYPE)
729 from = TREE_TYPE (from);
731 to = strip_top_quals (to);
732 from = strip_top_quals (from);
734 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
735 && expr && type_unknown_p (expr))
737 expr = instantiate_type (to, expr, tf_none);
738 if (expr == error_mark_node)
740 from = TREE_TYPE (expr);
743 fcode = TREE_CODE (from);
744 tcode = TREE_CODE (to);
746 conv = build1 (IDENTITY_CONV, from, expr);
748 if (fcode == FUNCTION_TYPE)
750 from = build_pointer_type (from);
751 fcode = TREE_CODE (from);
752 conv = build_conv (LVALUE_CONV, from, conv);
754 else if (fcode == ARRAY_TYPE)
756 from = build_pointer_type (TREE_TYPE (from));
757 fcode = TREE_CODE (from);
758 conv = build_conv (LVALUE_CONV, from, conv);
760 else if (fromref || (expr && lvalue_p (expr)))
761 conv = build_conv (RVALUE_CONV, from, conv);
763 /* Allow conversion between `__complex__' data types */
764 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
766 /* The standard conversion sequence to convert FROM to TO is
767 the standard conversion sequence to perform componentwise
769 tree part_conv = standard_conversion
770 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
774 conv = build_conv (TREE_CODE (part_conv), to, conv);
775 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
783 if (same_type_p (from, to))
786 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
787 && expr && null_ptr_cst_p (expr))
789 conv = build_conv (STD_CONV, to, conv);
791 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
792 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
794 /* For backwards brain damage compatibility, allow interconversion of
795 pointers and integers with a pedwarn. */
796 conv = build_conv (STD_CONV, to, conv);
797 ICS_BAD_FLAG (conv) = 1;
799 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
800 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
802 /* For backwards brain damage compatibility, allow interconversion of
803 enums and integers with a pedwarn. */
804 conv = build_conv (STD_CONV, to, conv);
805 ICS_BAD_FLAG (conv) = 1;
807 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
809 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
810 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
812 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
815 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
816 && ufcode != FUNCTION_TYPE)
818 from = build_pointer_type
819 (cp_build_qualified_type (void_type_node,
820 cp_type_quals (TREE_TYPE (from))));
821 conv = build_conv (PTR_CONV, from, conv);
823 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
825 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
826 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
828 if (DERIVED_FROM_P (fbase, tbase)
829 && (same_type_ignoring_top_level_qualifiers_p
830 (TREE_TYPE (TREE_TYPE (from)),
831 TREE_TYPE (TREE_TYPE (to)))))
833 from = build_ptrmem_type (tbase, TREE_TYPE (TREE_TYPE (from)));
834 conv = build_conv (PMEM_CONV, from, conv);
837 else if (IS_AGGR_TYPE (TREE_TYPE (from))
838 && IS_AGGR_TYPE (TREE_TYPE (to)))
840 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
843 cp_build_qualified_type (TREE_TYPE (to),
844 cp_type_quals (TREE_TYPE (from)));
845 from = build_pointer_type (from);
846 conv = build_conv (PTR_CONV, from, conv);
850 if (same_type_p (from, to))
852 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
853 conv = build_conv (QUAL_CONV, to, conv);
854 else if (expr && string_conv_p (to, expr, 0))
855 /* converting from string constant to char *. */
856 conv = build_conv (QUAL_CONV, to, conv);
857 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
859 conv = build_conv (PTR_CONV, to, conv);
860 ICS_BAD_FLAG (conv) = 1;
867 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
869 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
870 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
871 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
872 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
874 if (!DERIVED_FROM_P (fbase, tbase)
875 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
876 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
877 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
878 || cp_type_quals (fbase) != cp_type_quals (tbase))
881 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
882 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
883 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
884 from = build_ptrmemfunc_type (build_pointer_type (from));
885 conv = build_conv (PMEM_CONV, from, conv);
887 else if (tcode == BOOLEAN_TYPE)
889 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
890 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
893 conv = build_conv (STD_CONV, to, conv);
894 if (fcode == POINTER_TYPE
895 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
896 ICS_STD_RANK (conv) = PBOOL_RANK;
898 /* We don't check for ENUMERAL_TYPE here because there are no standard
899 conversions to enum type. */
900 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
901 || tcode == REAL_TYPE)
903 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
905 conv = build_conv (STD_CONV, to, conv);
907 /* Give this a better rank if it's a promotion. */
908 if (to == type_promotes_to (from)
909 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
910 ICS_STD_RANK (conv) = PROMO_RANK;
912 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
913 && is_properly_derived_from (from, to))
915 if (TREE_CODE (conv) == RVALUE_CONV)
916 conv = TREE_OPERAND (conv, 0);
917 conv = build_conv (BASE_CONV, to, conv);
918 /* The derived-to-base conversion indicates the initialization
919 of a parameter with base type from an object of a derived
920 type. A temporary object is created to hold the result of
922 NEED_TEMPORARY_P (conv) = 1;
930 /* Returns nonzero if T1 is reference-related to T2. */
933 reference_related_p (tree t1, tree t2)
935 t1 = TYPE_MAIN_VARIANT (t1);
936 t2 = TYPE_MAIN_VARIANT (t2);
940 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
941 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
943 return (same_type_p (t1, t2)
944 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
945 && DERIVED_FROM_P (t1, t2)));
948 /* Returns nonzero if T1 is reference-compatible with T2. */
951 reference_compatible_p (tree t1, tree t2)
955 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
956 reference-related to T2 and cv1 is the same cv-qualification as,
957 or greater cv-qualification than, cv2. */
958 return (reference_related_p (t1, t2)
959 && at_least_as_qualified_p (t1, t2));
962 /* Determine whether or not the EXPR (of class type S) can be
963 converted to T as in [over.match.ref]. */
966 convert_class_to_reference (tree t, tree s, tree expr)
971 struct z_candidate *candidates;
972 struct z_candidate *cand;
976 Assuming that "cv1 T" is the underlying type of the reference
977 being initialized, and "cv S" is the type of the initializer
978 expression, with S a class type, the candidate functions are
981 --The conversion functions of S and its base classes are
982 considered. Those that are not hidden within S and yield type
983 "reference to cv2 T2", where "cv1 T" is reference-compatible
984 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
986 The argument list has one argument, which is the initializer
991 /* Conceptually, we should take the address of EXPR and put it in
992 the argument list. Unfortunately, however, that can result in
993 error messages, which we should not issue now because we are just
994 trying to find a conversion operator. Therefore, we use NULL,
995 cast to the appropriate type. */
996 arglist = build_int_2 (0, 0);
997 TREE_TYPE (arglist) = build_pointer_type (s);
998 arglist = build_tree_list (NULL_TREE, arglist);
1000 for (conversions = lookup_conversions (s);
1002 conversions = TREE_CHAIN (conversions))
1004 tree fns = TREE_VALUE (conversions);
1006 for (; fns; fns = OVL_NEXT (fns))
1008 tree f = OVL_CURRENT (fns);
1009 tree t2 = TREE_TYPE (TREE_TYPE (f));
1010 struct z_candidate *old_candidates = candidates;
1012 /* If this is a template function, try to get an exact
1014 if (TREE_CODE (f) == TEMPLATE_DECL)
1017 = add_template_candidate (candidates,
1021 build_reference_type (t),
1023 TREE_PURPOSE (conversions),
1027 if (candidates != old_candidates)
1029 /* Now, see if the conversion function really returns
1030 an lvalue of the appropriate type. From the
1031 point of view of unification, simply returning an
1032 rvalue of the right type is good enough. */
1034 t2 = TREE_TYPE (TREE_TYPE (f));
1035 if (TREE_CODE (t2) != REFERENCE_TYPE
1036 || !reference_compatible_p (t, TREE_TYPE (t2)))
1037 candidates = candidates->next;
1040 else if (TREE_CODE (t2) == REFERENCE_TYPE
1041 && reference_compatible_p (t, TREE_TYPE (t2)))
1043 = add_function_candidate (candidates, f, s, arglist,
1045 TREE_PURPOSE (conversions),
1050 /* If none of the conversion functions worked out, let our caller
1052 if (!any_viable (candidates))
1055 candidates = splice_viable (candidates);
1056 cand = tourney (candidates);
1060 conv = build1 (IDENTITY_CONV, s, expr);
1061 conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1063 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
1064 ICS_USER_FLAG (conv) = 1;
1065 if (cand->viable == -1)
1066 ICS_BAD_FLAG (conv) = 1;
1067 cand->second_conv = conv;
1072 /* A reference of the indicated TYPE is being bound directly to the
1073 expression represented by the implicit conversion sequence CONV.
1074 Return a conversion sequence for this binding. */
1077 direct_reference_binding (tree type, tree conv)
1079 tree t = TREE_TYPE (type);
1083 When a parameter of reference type binds directly
1084 (_dcl.init.ref_) to an argument expression, the implicit
1085 conversion sequence is the identity conversion, unless the
1086 argument expression has a type that is a derived class of the
1087 parameter type, in which case the implicit conversion sequence is
1088 a derived-to-base Conversion.
1090 If the parameter binds directly to the result of applying a
1091 conversion function to the argument expression, the implicit
1092 conversion sequence is a user-defined conversion sequence
1093 (_over.ics.user_), with the second standard conversion sequence
1094 either an identity conversion or, if the conversion function
1095 returns an entity of a type that is a derived class of the
1096 parameter type, a derived-to-base conversion. */
1097 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1099 /* Represent the derived-to-base conversion. */
1100 conv = build_conv (BASE_CONV, t, conv);
1101 /* We will actually be binding to the base-class subobject in
1102 the derived class, so we mark this conversion appropriately.
1103 That way, convert_like knows not to generate a temporary. */
1104 NEED_TEMPORARY_P (conv) = 0;
1106 return build_conv (REF_BIND, type, conv);
1109 /* Returns the conversion path from type FROM to reference type TO for
1110 purposes of reference binding. For lvalue binding, either pass a
1111 reference type to FROM or an lvalue expression to EXPR. If the
1112 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1113 the conversion returned. */
1116 reference_binding (tree rto, tree rfrom, tree expr, int flags)
1118 tree conv = NULL_TREE;
1119 tree to = TREE_TYPE (rto);
1123 cp_lvalue_kind lvalue_p = clk_none;
1125 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1127 expr = instantiate_type (to, expr, tf_none);
1128 if (expr == error_mark_node)
1130 from = TREE_TYPE (expr);
1133 if (TREE_CODE (from) == REFERENCE_TYPE)
1135 /* Anything with reference type is an lvalue. */
1136 lvalue_p = clk_ordinary;
1137 from = TREE_TYPE (from);
1140 lvalue_p = real_lvalue_p (expr);
1142 /* Figure out whether or not the types are reference-related and
1143 reference compatible. We have do do this after stripping
1144 references from FROM. */
1145 related_p = reference_related_p (to, from);
1146 compatible_p = reference_compatible_p (to, from);
1148 if (lvalue_p && compatible_p)
1152 If the initializer expression
1154 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1155 is reference-compatible with "cv2 T2,"
1157 the reference is bound directly to the initializer exprssion
1159 conv = build1 (IDENTITY_CONV, from, expr);
1160 conv = direct_reference_binding (rto, conv);
1161 if ((lvalue_p & clk_bitfield) != 0
1162 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1163 /* For the purposes of overload resolution, we ignore the fact
1164 this expression is a bitfield. (In particular,
1165 [over.ics.ref] says specifically that a function with a
1166 non-const reference parameter is viable even if the
1167 argument is a bitfield.)
1169 However, when we actually call the function we must create
1170 a temporary to which to bind the reference. If the
1171 reference is volatile, or isn't const, then we cannot make
1172 a temporary, so we just issue an error when the conversion
1174 NEED_TEMPORARY_P (conv) = 1;
1177 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1181 If the initializer exprsesion
1183 -- has a class type (i.e., T2 is a class type) can be
1184 implicitly converted to an lvalue of type "cv3 T3," where
1185 "cv1 T1" is reference-compatible with "cv3 T3". (this
1186 conversion is selected by enumerating the applicable
1187 conversion functions (_over.match.ref_) and choosing the
1188 best one through overload resolution. (_over.match_).
1190 the reference is bound to the lvalue result of the conversion
1191 in the second case. */
1192 conv = convert_class_to_reference (to, from, expr);
1194 return direct_reference_binding (rto, conv);
1197 /* From this point on, we conceptually need temporaries, even if we
1198 elide them. Only the cases above are "direct bindings". */
1199 if (flags & LOOKUP_NO_TEMP_BIND)
1204 When a parameter of reference type is not bound directly to an
1205 argument expression, the conversion sequence is the one required
1206 to convert the argument expression to the underlying type of the
1207 reference according to _over.best.ics_. Conceptually, this
1208 conversion sequence corresponds to copy-initializing a temporary
1209 of the underlying type with the argument expression. Any
1210 difference in top-level cv-qualification is subsumed by the
1211 initialization itself and does not constitute a conversion. */
1215 Otherwise, the reference shall be to a non-volatile const type. */
1216 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1221 If the initializer expression is an rvalue, with T2 a class type,
1222 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1223 is bound in one of the following ways:
1225 -- The reference is bound to the object represented by the rvalue
1226 or to a sub-object within that object.
1228 In this case, the implicit conversion sequence is supposed to be
1229 same as we would obtain by generating a temporary. Fortunately,
1230 if the types are reference compatible, then this is either an
1231 identity conversion or the derived-to-base conversion, just as
1232 for direct binding. */
1233 if (CLASS_TYPE_P (from) && compatible_p)
1235 conv = build1 (IDENTITY_CONV, from, expr);
1236 return direct_reference_binding (rto, conv);
1241 Otherwise, a temporary of type "cv1 T1" is created and
1242 initialized from the initializer expression using the rules for a
1243 non-reference copy initialization. If T1 is reference-related to
1244 T2, cv1 must be the same cv-qualification as, or greater
1245 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1246 if (related_p && !at_least_as_qualified_p (to, from))
1249 conv = implicit_conversion (to, from, expr, flags);
1253 conv = build_conv (REF_BIND, rto, conv);
1254 /* This reference binding, unlike those above, requires the
1255 creation of a temporary. */
1256 NEED_TEMPORARY_P (conv) = 1;
1261 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1262 to type TO. The optional expression EXPR may affect the conversion.
1263 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1267 implicit_conversion (tree to, tree from, tree expr, int flags)
1270 struct z_candidate *cand;
1272 /* Resolve expressions like `A::p' that we thought might become
1273 pointers-to-members. */
1274 if (expr && TREE_CODE (expr) == OFFSET_REF)
1276 expr = resolve_offset_ref (expr);
1277 from = TREE_TYPE (expr);
1280 if (from == error_mark_node || to == error_mark_node
1281 || expr == error_mark_node)
1284 /* Make sure both the FROM and TO types are complete so that
1285 user-defined conversions are available. */
1286 complete_type (from);
1289 if (TREE_CODE (to) == REFERENCE_TYPE)
1290 conv = reference_binding (to, from, expr, flags);
1292 conv = standard_conversion (to, from, expr);
1296 else if (expr != NULL_TREE
1297 && (IS_AGGR_TYPE (from)
1298 || IS_AGGR_TYPE (to))
1299 && (flags & LOOKUP_NO_CONVERSION) == 0)
1301 cand = build_user_type_conversion_1
1302 (to, expr, LOOKUP_ONLYCONVERTING);
1304 conv = cand->second_conv;
1306 /* We used to try to bind a reference to a temporary here, but that
1307 is now handled by the recursive call to this function at the end
1308 of reference_binding. */
1314 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1317 static struct z_candidate *
1318 add_candidate (struct z_candidate *candidates,
1319 tree fn, tree convs, tree access_path, tree
1320 conversion_path, int viable)
1322 struct z_candidate *cand
1323 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1326 cand->convs = convs;
1327 cand->access_path = access_path;
1328 cand->conversion_path = conversion_path;
1329 cand->viable = viable;
1330 cand->next = candidates;
1335 /* Create an overload candidate for the function or method FN called with
1336 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1337 to implicit_conversion.
1339 CTYPE, if non-NULL, is the type we want to pretend this function
1340 comes from for purposes of overload resolution. */
1342 static struct z_candidate *
1343 add_function_candidate (struct z_candidate *candidates,
1344 tree fn, tree ctype, tree arglist,
1345 tree access_path, tree conversion_path,
1348 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1351 tree parmnode, argnode;
1354 /* The `this', `in_chrg' and VTT arguments to constructors are not
1355 considered in overload resolution. */
1356 if (DECL_CONSTRUCTOR_P (fn))
1358 parmlist = skip_artificial_parms_for (fn, parmlist);
1359 arglist = skip_artificial_parms_for (fn, arglist);
1362 len = list_length (arglist);
1363 convs = make_tree_vec (len);
1365 /* 13.3.2 - Viable functions [over.match.viable]
1366 First, to be a viable function, a candidate function shall have enough
1367 parameters to agree in number with the arguments in the list.
1369 We need to check this first; otherwise, checking the ICSes might cause
1370 us to produce an ill-formed template instantiation. */
1372 parmnode = parmlist;
1373 for (i = 0; i < len; ++i)
1375 if (parmnode == NULL_TREE || parmnode == void_list_node)
1377 parmnode = TREE_CHAIN (parmnode);
1380 if (i < len && parmnode)
1383 /* Make sure there are default args for the rest of the parms. */
1384 else if (!sufficient_parms_p (parmnode))
1390 /* Second, for F to be a viable function, there shall exist for each
1391 argument an implicit conversion sequence that converts that argument
1392 to the corresponding parameter of F. */
1394 parmnode = parmlist;
1397 for (i = 0; i < len; ++i)
1399 tree arg = TREE_VALUE (argnode);
1400 tree argtype = lvalue_type (arg);
1404 if (parmnode == void_list_node)
1407 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1408 && ! DECL_CONSTRUCTOR_P (fn));
1412 tree parmtype = TREE_VALUE (parmnode);
1414 /* The type of the implicit object parameter ('this') for
1415 overload resolution is not always the same as for the
1416 function itself; conversion functions are considered to
1417 be members of the class being converted, and functions
1418 introduced by a using-declaration are considered to be
1419 members of the class that uses them.
1421 Since build_over_call ignores the ICS for the `this'
1422 parameter, we can just change the parm type. */
1423 if (ctype && is_this)
1426 = build_qualified_type (ctype,
1427 TYPE_QUALS (TREE_TYPE (parmtype)));
1428 parmtype = build_pointer_type (parmtype);
1431 t = implicit_conversion (parmtype, argtype, arg, flags);
1435 t = build1 (IDENTITY_CONV, argtype, arg);
1436 ICS_ELLIPSIS_FLAG (t) = 1;
1440 ICS_THIS_FLAG (t) = 1;
1442 TREE_VEC_ELT (convs, i) = t;
1449 if (ICS_BAD_FLAG (t))
1453 parmnode = TREE_CHAIN (parmnode);
1454 argnode = TREE_CHAIN (argnode);
1458 return add_candidate (candidates, fn, convs, access_path,
1459 conversion_path, viable);
1462 /* Create an overload candidate for the conversion function FN which will
1463 be invoked for expression OBJ, producing a pointer-to-function which
1464 will in turn be called with the argument list ARGLIST, and add it to
1465 CANDIDATES. FLAGS is passed on to implicit_conversion.
1467 Actually, we don't really care about FN; we care about the type it
1468 converts to. There may be multiple conversion functions that will
1469 convert to that type, and we rely on build_user_type_conversion_1 to
1470 choose the best one; so when we create our candidate, we record the type
1471 instead of the function. */
1473 static struct z_candidate *
1474 add_conv_candidate (struct z_candidate *candidates, tree fn, tree obj,
1475 tree arglist, tree access_path, tree conversion_path)
1477 tree totype = TREE_TYPE (TREE_TYPE (fn));
1478 int i, len, viable, flags;
1479 tree parmlist, convs, parmnode, argnode;
1481 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1482 parmlist = TREE_TYPE (parmlist);
1483 parmlist = TYPE_ARG_TYPES (parmlist);
1485 len = list_length (arglist) + 1;
1486 convs = make_tree_vec (len);
1487 parmnode = parmlist;
1490 flags = LOOKUP_NORMAL;
1492 /* Don't bother looking up the same type twice. */
1493 if (candidates && candidates->fn == totype)
1496 for (i = 0; i < len; ++i)
1498 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1499 tree argtype = lvalue_type (arg);
1503 t = implicit_conversion (totype, argtype, arg, flags);
1504 else if (parmnode == void_list_node)
1507 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1510 t = build1 (IDENTITY_CONV, argtype, arg);
1511 ICS_ELLIPSIS_FLAG (t) = 1;
1514 TREE_VEC_ELT (convs, i) = t;
1518 if (ICS_BAD_FLAG (t))
1525 parmnode = TREE_CHAIN (parmnode);
1526 argnode = TREE_CHAIN (argnode);
1532 if (!sufficient_parms_p (parmnode))
1535 return add_candidate (candidates, totype, convs, access_path,
1536 conversion_path, viable);
1539 static struct z_candidate *
1540 build_builtin_candidate (struct z_candidate *candidates, tree fnname,
1541 tree type1, tree type2, tree *args, tree *argtypes,
1551 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1553 for (i = 0; i < 2; ++i)
1558 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1562 /* We need something for printing the candidate. */
1563 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1565 else if (ICS_BAD_FLAG (t))
1567 TREE_VEC_ELT (convs, i) = t;
1570 /* For COND_EXPR we rearranged the arguments; undo that now. */
1573 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1574 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1575 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1577 TREE_VEC_ELT (convs, 0) = t;
1582 return add_candidate (candidates, fnname, convs,
1583 /*access_path=*/NULL_TREE,
1584 /*conversion_path=*/NULL_TREE,
1589 is_complete (tree t)
1591 return COMPLETE_TYPE_P (complete_type (t));
1594 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1597 promoted_arithmetic_type_p (tree type)
1601 In this section, the term promoted integral type is used to refer
1602 to those integral types which are preserved by integral promotion
1603 (including e.g. int and long but excluding e.g. char).
1604 Similarly, the term promoted arithmetic type refers to promoted
1605 integral types plus floating types. */
1606 return ((INTEGRAL_TYPE_P (type)
1607 && same_type_p (type_promotes_to (type), type))
1608 || TREE_CODE (type) == REAL_TYPE);
1611 /* Create any builtin operator overload candidates for the operator in
1612 question given the converted operand types TYPE1 and TYPE2. The other
1613 args are passed through from add_builtin_candidates to
1614 build_builtin_candidate.
1616 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1617 If CODE is requires candidates operands of the same type of the kind
1618 of which TYPE1 and TYPE2 are, we add both candidates
1619 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1621 static struct z_candidate *
1622 add_builtin_candidate (struct z_candidate *candidates, enum tree_code code,
1623 enum tree_code code2, tree fnname, tree type1,
1624 tree type2, tree *args, tree *argtypes, int flags)
1628 case POSTINCREMENT_EXPR:
1629 case POSTDECREMENT_EXPR:
1630 args[1] = integer_zero_node;
1631 type2 = integer_type_node;
1640 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1641 and VQ is either volatile or empty, there exist candidate operator
1642 functions of the form
1643 VQ T& operator++(VQ T&);
1644 T operator++(VQ T&, int);
1645 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1646 type other than bool, and VQ is either volatile or empty, there exist
1647 candidate operator functions of the form
1648 VQ T& operator--(VQ T&);
1649 T operator--(VQ T&, int);
1650 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1651 complete object type, and VQ is either volatile or empty, there exist
1652 candidate operator functions of the form
1653 T*VQ& operator++(T*VQ&);
1654 T*VQ& operator--(T*VQ&);
1655 T* operator++(T*VQ&, int);
1656 T* operator--(T*VQ&, int); */
1658 case POSTDECREMENT_EXPR:
1659 case PREDECREMENT_EXPR:
1660 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1662 case POSTINCREMENT_EXPR:
1663 case PREINCREMENT_EXPR:
1664 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1666 type1 = build_reference_type (type1);
1671 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1672 exist candidate operator functions of the form
1676 8 For every function type T, there exist candidate operator functions of
1678 T& operator*(T*); */
1681 if (TREE_CODE (type1) == POINTER_TYPE
1682 && (TYPE_PTROB_P (type1)
1683 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1687 /* 9 For every type T, there exist candidate operator functions of the form
1690 10For every promoted arithmetic type T, there exist candidate operator
1691 functions of the form
1695 case CONVERT_EXPR: /* unary + */
1696 if (TREE_CODE (type1) == POINTER_TYPE
1697 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1700 if (ARITHMETIC_TYPE_P (type1))
1704 /* 11For every promoted integral type T, there exist candidate operator
1705 functions of the form
1709 if (INTEGRAL_TYPE_P (type1))
1713 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1714 is the same type as C2 or is a derived class of C2, T is a complete
1715 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1716 there exist candidate operator functions of the form
1717 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1718 where CV12 is the union of CV1 and CV2. */
1721 if (TREE_CODE (type1) == POINTER_TYPE
1722 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1724 tree c1 = TREE_TYPE (type1);
1725 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1726 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1727 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1729 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1730 && (TYPE_PTRMEMFUNC_P (type2)
1731 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1736 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1737 didate operator functions of the form
1742 bool operator<(L, R);
1743 bool operator>(L, R);
1744 bool operator<=(L, R);
1745 bool operator>=(L, R);
1746 bool operator==(L, R);
1747 bool operator!=(L, R);
1748 where LR is the result of the usual arithmetic conversions between
1751 14For every pair of types T and I, where T is a cv-qualified or cv-
1752 unqualified complete object type and I is a promoted integral type,
1753 there exist candidate operator functions of the form
1754 T* operator+(T*, I);
1755 T& operator[](T*, I);
1756 T* operator-(T*, I);
1757 T* operator+(I, T*);
1758 T& operator[](I, T*);
1760 15For every T, where T is a pointer to complete object type, there exist
1761 candidate operator functions of the form112)
1762 ptrdiff_t operator-(T, T);
1764 16For every pointer or enumeration type T, there exist candidate operator
1765 functions of the form
1766 bool operator<(T, T);
1767 bool operator>(T, T);
1768 bool operator<=(T, T);
1769 bool operator>=(T, T);
1770 bool operator==(T, T);
1771 bool operator!=(T, T);
1773 17For every pointer to member type T, there exist candidate operator
1774 functions of the form
1775 bool operator==(T, T);
1776 bool operator!=(T, T); */
1779 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1781 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1783 type2 = ptrdiff_type_node;
1787 case TRUNC_DIV_EXPR:
1788 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1794 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1795 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1797 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1798 && null_ptr_cst_p (args[1]))
1803 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1804 && null_ptr_cst_p (args[0]))
1816 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1818 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1820 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1822 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1827 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1835 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1838 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1840 type1 = ptrdiff_type_node;
1843 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1845 type2 = ptrdiff_type_node;
1850 /* 18For every pair of promoted integral types L and R, there exist candi-
1851 date operator functions of the form
1858 where LR is the result of the usual arithmetic conversions between
1861 case TRUNC_MOD_EXPR:
1867 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1871 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1872 type, VQ is either volatile or empty, and R is a promoted arithmetic
1873 type, there exist candidate operator functions of the form
1874 VQ L& operator=(VQ L&, R);
1875 VQ L& operator*=(VQ L&, R);
1876 VQ L& operator/=(VQ L&, R);
1877 VQ L& operator+=(VQ L&, R);
1878 VQ L& operator-=(VQ L&, R);
1880 20For every pair T, VQ), where T is any type and VQ is either volatile
1881 or empty, there exist candidate operator functions of the form
1882 T*VQ& operator=(T*VQ&, T*);
1884 21For every pair T, VQ), where T is a pointer to member type and VQ is
1885 either volatile or empty, there exist candidate operator functions of
1887 VQ T& operator=(VQ T&, T);
1889 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1890 unqualified complete object type, VQ is either volatile or empty, and
1891 I is a promoted integral type, there exist candidate operator func-
1893 T*VQ& operator+=(T*VQ&, I);
1894 T*VQ& operator-=(T*VQ&, I);
1896 23For every triple L, VQ, R), where L is an integral or enumeration
1897 type, VQ is either volatile or empty, and R is a promoted integral
1898 type, there exist candidate operator functions of the form
1900 VQ L& operator%=(VQ L&, R);
1901 VQ L& operator<<=(VQ L&, R);
1902 VQ L& operator>>=(VQ L&, R);
1903 VQ L& operator&=(VQ L&, R);
1904 VQ L& operator^=(VQ L&, R);
1905 VQ L& operator|=(VQ L&, R); */
1912 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1914 type2 = ptrdiff_type_node;
1918 case TRUNC_DIV_EXPR:
1919 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1923 case TRUNC_MOD_EXPR:
1929 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1934 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1936 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1937 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1938 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1939 || ((TYPE_PTRMEMFUNC_P (type1)
1940 || TREE_CODE (type1) == POINTER_TYPE)
1941 && null_ptr_cst_p (args[1])))
1951 type1 = build_reference_type (type1);
1957 For every pair of promoted arithmetic types L and R, there
1958 exist candidate operator functions of the form
1960 LR operator?(bool, L, R);
1962 where LR is the result of the usual arithmetic conversions
1963 between types L and R.
1965 For every type T, where T is a pointer or pointer-to-member
1966 type, there exist candidate operator functions of the form T
1967 operator?(bool, T, T); */
1969 if (promoted_arithmetic_type_p (type1)
1970 && promoted_arithmetic_type_p (type2))
1974 /* Otherwise, the types should be pointers. */
1975 if (!(TREE_CODE (type1) == POINTER_TYPE
1976 || TYPE_PTRMEM_P (type1)
1977 || TYPE_PTRMEMFUNC_P (type1))
1978 || !(TREE_CODE (type2) == POINTER_TYPE
1979 || TYPE_PTRMEM_P (type2)
1980 || TYPE_PTRMEMFUNC_P (type2)))
1983 /* We don't check that the two types are the same; the logic
1984 below will actually create two candidates; one in which both
1985 parameter types are TYPE1, and one in which both parameter
1989 /* These arguments do not make for a valid overloaded operator. */
1996 /* If we're dealing with two pointer types or two enumeral types,
1997 we need candidates for both of them. */
1998 if (type2 && !same_type_p (type1, type2)
1999 && TREE_CODE (type1) == TREE_CODE (type2)
2000 && (TREE_CODE (type1) == REFERENCE_TYPE
2001 || (TREE_CODE (type1) == POINTER_TYPE
2002 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
2003 || TYPE_PTRMEMFUNC_P (type1)
2004 || IS_AGGR_TYPE (type1)
2005 || TREE_CODE (type1) == ENUMERAL_TYPE))
2007 candidates = build_builtin_candidate
2008 (candidates, fnname, type1, type1, args, argtypes, flags);
2009 return build_builtin_candidate
2010 (candidates, fnname, type2, type2, args, argtypes, flags);
2013 return build_builtin_candidate
2014 (candidates, fnname, type1, type2, args, argtypes, flags);
2018 type_decays_to (tree type)
2020 if (TREE_CODE (type) == ARRAY_TYPE)
2021 return build_pointer_type (TREE_TYPE (type));
2022 if (TREE_CODE (type) == FUNCTION_TYPE)
2023 return build_pointer_type (type);
2027 /* There are three conditions of builtin candidates:
2029 1) bool-taking candidates. These are the same regardless of the input.
2030 2) pointer-pair taking candidates. These are generated for each type
2031 one of the input types converts to.
2032 3) arithmetic candidates. According to the standard, we should generate
2033 all of these, but I'm trying not to...
2035 Here we generate a superset of the possible candidates for this particular
2036 case. That is a subset of the full set the standard defines, plus some
2037 other cases which the standard disallows. add_builtin_candidate will
2038 filter out the invalid set. */
2040 static struct z_candidate *
2041 add_builtin_candidates (struct z_candidate *candidates, enum tree_code code,
2042 enum tree_code code2, tree fnname, tree *args,
2047 tree type, argtypes[3];
2048 /* TYPES[i] is the set of possible builtin-operator parameter types
2049 we will consider for the Ith argument. These are represented as
2050 a TREE_LIST; the TREE_VALUE of each node is the potential
2054 for (i = 0; i < 3; ++i)
2057 argtypes[i] = lvalue_type (args[i]);
2059 argtypes[i] = NULL_TREE;
2064 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2065 and VQ is either volatile or empty, there exist candidate operator
2066 functions of the form
2067 VQ T& operator++(VQ T&); */
2069 case POSTINCREMENT_EXPR:
2070 case PREINCREMENT_EXPR:
2071 case POSTDECREMENT_EXPR:
2072 case PREDECREMENT_EXPR:
2077 /* 24There also exist candidate operator functions of the form
2078 bool operator!(bool);
2079 bool operator&&(bool, bool);
2080 bool operator||(bool, bool); */
2082 case TRUTH_NOT_EXPR:
2083 return build_builtin_candidate
2084 (candidates, fnname, boolean_type_node,
2085 NULL_TREE, args, argtypes, flags);
2087 case TRUTH_ORIF_EXPR:
2088 case TRUTH_ANDIF_EXPR:
2089 return build_builtin_candidate
2090 (candidates, fnname, boolean_type_node,
2091 boolean_type_node, args, argtypes, flags);
2112 types[0] = types[1] = NULL_TREE;
2114 for (i = 0; i < 2; ++i)
2118 else if (IS_AGGR_TYPE (argtypes[i]))
2122 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2125 convs = lookup_conversions (argtypes[i]);
2127 if (code == COND_EXPR)
2129 if (real_lvalue_p (args[i]))
2130 types[i] = tree_cons
2131 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2133 types[i] = tree_cons
2134 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2140 for (; convs; convs = TREE_CHAIN (convs))
2142 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2145 && (TREE_CODE (type) != REFERENCE_TYPE
2146 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2149 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2150 types[i] = tree_cons (NULL_TREE, type, types[i]);
2152 type = non_reference (type);
2153 if (i != 0 || ! ref1)
2155 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2156 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2157 types[i] = tree_cons (NULL_TREE, type, types[i]);
2158 if (INTEGRAL_TYPE_P (type))
2159 type = type_promotes_to (type);
2162 if (! value_member (type, types[i]))
2163 types[i] = tree_cons (NULL_TREE, type, types[i]);
2168 if (code == COND_EXPR && real_lvalue_p (args[i]))
2169 types[i] = tree_cons
2170 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2171 type = non_reference (argtypes[i]);
2172 if (i != 0 || ! ref1)
2174 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2175 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2176 types[i] = tree_cons (NULL_TREE, type, types[i]);
2177 if (INTEGRAL_TYPE_P (type))
2178 type = type_promotes_to (type);
2180 types[i] = tree_cons (NULL_TREE, type, types[i]);
2184 /* Run through the possible parameter types of both arguments,
2185 creating candidates with those parameter types. */
2186 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2189 for (type = types[1]; type; type = TREE_CHAIN (type))
2190 candidates = add_builtin_candidate
2191 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2192 TREE_VALUE (type), args, argtypes, flags);
2194 candidates = add_builtin_candidate
2195 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2196 NULL_TREE, args, argtypes, flags);
2203 /* If TMPL can be successfully instantiated as indicated by
2204 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2206 TMPL is the template. EXPLICIT_TARGS are any explicit template
2207 arguments. ARGLIST is the arguments provided at the call-site.
2208 The RETURN_TYPE is the desired type for conversion operators. If
2209 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2210 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2211 add_conv_candidate. */
2213 static struct z_candidate*
2214 add_template_candidate_real (struct z_candidate *candidates, tree tmpl,
2215 tree ctype, tree explicit_targs, tree arglist,
2216 tree return_type, tree access_path,
2217 tree conversion_path, int flags, tree obj,
2218 unification_kind_t strict)
2220 int ntparms = DECL_NTPARMS (tmpl);
2221 tree targs = make_tree_vec (ntparms);
2222 tree args_without_in_chrg = arglist;
2223 struct z_candidate *cand;
2227 /* We don't do deduction on the in-charge parameter, the VTT
2228 parameter or 'this'. */
2229 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2230 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2232 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2233 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2234 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2235 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2237 i = fn_type_unification (tmpl, explicit_targs, targs,
2238 args_without_in_chrg,
2239 return_type, strict, -1);
2244 fn = instantiate_template (tmpl, targs);
2245 if (fn == error_mark_node)
2250 A member function template is never instantiated to perform the
2251 copy of a class object to an object of its class type.
2253 It's a little unclear what this means; the standard explicitly
2254 does allow a template to be used to copy a class. For example,
2259 template <class T> A(const T&);
2262 void g () { A a (f ()); }
2264 the member template will be used to make the copy. The section
2265 quoted above appears in the paragraph that forbids constructors
2266 whose only parameter is (a possibly cv-qualified variant of) the
2267 class type, and a logical interpretation is that the intent was
2268 to forbid the instantiation of member templates which would then
2270 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2272 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2273 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2278 if (obj != NULL_TREE)
2279 /* Aha, this is a conversion function. */
2280 cand = add_conv_candidate (candidates, fn, obj, access_path,
2281 conversion_path, arglist);
2283 cand = add_function_candidate (candidates, fn, ctype,
2284 arglist, access_path,
2285 conversion_path, flags);
2286 if (DECL_TI_TEMPLATE (fn) != tmpl)
2287 /* This situation can occur if a member template of a template
2288 class is specialized. Then, instantiate_template might return
2289 an instantiation of the specialization, in which case the
2290 DECL_TI_TEMPLATE field will point at the original
2291 specialization. For example:
2293 template <class T> struct S { template <class U> void f(U);
2294 template <> void f(int) {}; };
2298 Here, TMPL will be template <class U> S<double>::f(U).
2299 And, instantiate template will give us the specialization
2300 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2301 for this will point at template <class T> template <> S<T>::f(int),
2302 so that we can find the definition. For the purposes of
2303 overload resolution, however, we want the original TMPL. */
2304 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2306 cand->template = DECL_TEMPLATE_INFO (fn);
2312 static struct z_candidate *
2313 add_template_candidate (struct z_candidate *candidates, tree tmpl, tree ctype,
2314 tree explicit_targs, tree arglist, tree return_type,
2315 tree access_path, tree conversion_path, int flags,
2316 unification_kind_t strict)
2319 add_template_candidate_real (candidates, tmpl, ctype,
2320 explicit_targs, arglist, return_type,
2321 access_path, conversion_path,
2322 flags, NULL_TREE, strict);
2326 static struct z_candidate *
2327 add_template_conv_candidate (struct z_candidate *candidates, tree tmpl,
2328 tree obj, tree arglist, tree return_type,
2329 tree access_path, tree conversion_path)
2332 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2333 arglist, return_type, access_path,
2334 conversion_path, 0, obj, DEDUCE_CONV);
2339 any_viable (struct z_candidate *cands)
2341 for (; cands; cands = cands->next)
2342 if (pedantic ? cands->viable == 1 : cands->viable)
2348 any_strictly_viable (struct z_candidate *cands)
2350 for (; cands; cands = cands->next)
2351 if (cands->viable == 1)
2356 static struct z_candidate *
2357 splice_viable (struct z_candidate *cands)
2359 struct z_candidate **p = &cands;
2363 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2373 build_this (tree obj)
2375 /* Fix this to work on non-lvalues. */
2376 return build_unary_op (ADDR_EXPR, obj, 0);
2380 print_z_candidates (struct z_candidate *candidates)
2382 const char *str = "candidates are:";
2383 for (; candidates; candidates = candidates->next)
2385 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2387 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2388 error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn,
2389 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2390 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2391 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2392 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2393 error ("%s %D(%T, %T) <built-in>", str, candidates->fn,
2394 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2395 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2397 error ("%s %D(%T) <built-in>", str, candidates->fn,
2398 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2400 else if (TYPE_P (candidates->fn))
2401 error ("%s %T <conversion>", str, candidates->fn);
2403 cp_error_at ("%s %+#D%s", str, candidates->fn,
2404 candidates->viable == -1 ? " <near match>" : "");
2409 /* Returns the best overload candidate to perform the requested
2410 conversion. This function is used for three the overloading situations
2411 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2412 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2413 per [dcl.init.ref], so we ignore temporary bindings. */
2415 static struct z_candidate *
2416 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2418 struct z_candidate *candidates, *cand;
2419 tree fromtype = TREE_TYPE (expr);
2420 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2421 tree args = NULL_TREE;
2422 tree templates = NULL_TREE;
2424 /* We represent conversion within a hierarchy using RVALUE_CONV and
2425 BASE_CONV, as specified by [over.best.ics]; these become plain
2426 constructor calls, as specified in [dcl.init]. */
2427 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2428 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2430 if (IS_AGGR_TYPE (totype))
2431 ctors = lookup_fnfields (TYPE_BINFO (totype),
2432 complete_ctor_identifier,
2435 if (IS_AGGR_TYPE (fromtype))
2436 convs = lookup_conversions (fromtype);
2439 flags |= LOOKUP_NO_CONVERSION;
2445 ctors = BASELINK_FUNCTIONS (ctors);
2447 t = build_int_2 (0, 0);
2448 TREE_TYPE (t) = build_pointer_type (totype);
2449 args = build_tree_list (NULL_TREE, expr);
2450 /* We should never try to call the abstract or base constructor
2452 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2453 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2455 args = tree_cons (NULL_TREE, t, args);
2457 for (; ctors; ctors = OVL_NEXT (ctors))
2459 tree ctor = OVL_CURRENT (ctors);
2460 if (DECL_NONCONVERTING_P (ctor))
2463 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2465 templates = tree_cons (NULL_TREE, ctor, templates);
2467 add_template_candidate (candidates, ctor, totype,
2468 NULL_TREE, args, NULL_TREE,
2469 TYPE_BINFO (totype),
2470 TYPE_BINFO (totype),
2475 candidates = add_function_candidate (candidates, ctor, totype,
2476 args, TYPE_BINFO (totype),
2477 TYPE_BINFO (totype),
2481 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2485 args = build_tree_list (NULL_TREE, build_this (expr));
2487 for (; convs; convs = TREE_CHAIN (convs))
2490 tree conversion_path = TREE_PURPOSE (convs);
2491 int convflags = LOOKUP_NO_CONVERSION;
2493 /* If we are called to convert to a reference type, we are trying to
2494 find an lvalue binding, so don't even consider temporaries. If
2495 we don't find an lvalue binding, the caller will try again to
2496 look for a temporary binding. */
2497 if (TREE_CODE (totype) == REFERENCE_TYPE)
2498 convflags |= LOOKUP_NO_TEMP_BIND;
2500 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2502 tree fn = OVL_CURRENT (fns);
2503 struct z_candidate *old_candidates = candidates;
2505 /* [over.match.funcs] For conversion functions, the function
2506 is considered to be a member of the class of the implicit
2507 object argument for the purpose of defining the type of
2508 the implicit object parameter.
2510 So we pass fromtype as CTYPE to add_*_candidate. */
2512 if (TREE_CODE (fn) == TEMPLATE_DECL)
2514 templates = tree_cons (NULL_TREE, fn, templates);
2516 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2517 args, totype, TYPE_BINFO (fromtype),
2523 candidates = add_function_candidate (candidates, fn, fromtype,
2525 TYPE_BINFO (fromtype),
2529 if (candidates != old_candidates)
2531 tree ics = implicit_conversion
2532 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2535 candidates->second_conv = ics;
2537 if (ics == NULL_TREE)
2538 candidates->viable = 0;
2539 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2540 candidates->viable = -1;
2545 if (! any_viable (candidates))
2548 candidates = splice_viable (candidates);
2549 cand = tourney (candidates);
2553 if (flags & LOOKUP_COMPLAIN)
2555 error ("conversion from `%T' to `%T' is ambiguous",
2557 print_z_candidates (candidates);
2560 cand = candidates; /* any one will do */
2561 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2562 ICS_USER_FLAG (cand->second_conv) = 1;
2563 /* Don't set ICS_BAD_FLAG; an ambiguous conversion is no worse than
2564 another user-defined conversion. */
2569 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2570 p = &(TREE_OPERAND (*p, 0));
2574 (DECL_CONSTRUCTOR_P (cand->fn)
2575 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2576 expr, build_zc_wrapper (cand));
2578 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2579 if (cand->viable == -1)
2580 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2586 build_user_type_conversion (tree totype, tree expr, int flags)
2588 struct z_candidate *cand
2589 = build_user_type_conversion_1 (totype, expr, flags);
2593 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2594 return error_mark_node;
2595 return convert_from_reference (convert_like (cand->second_conv, expr));
2600 /* Find the possibly overloaded set of functions corresponding to a
2601 call of the form SCOPE::NAME (...). NAME might be a
2602 TEMPLATE_ID_EXPR, OVERLOAD, _DECL, IDENTIFIER_NODE or LOOKUP_EXPR. */
2605 resolve_scoped_fn_name (tree scope, tree name)
2608 tree template_args = NULL_TREE;
2609 bool is_template_id = TREE_CODE (name) == TEMPLATE_ID_EXPR;
2613 template_args = TREE_OPERAND (name, 1);
2614 name = TREE_OPERAND (name, 0);
2616 if (TREE_CODE (name) == OVERLOAD)
2617 name = DECL_NAME (get_first_fn (name));
2618 else if (TREE_CODE (name) == LOOKUP_EXPR)
2619 name = TREE_OPERAND (name, 0);
2621 if (TREE_CODE (scope) == NAMESPACE_DECL)
2622 fn = lookup_namespace_name (scope, name);
2625 if (!TYPE_BEING_DEFINED (scope)
2626 && !COMPLETE_TYPE_P (complete_type (scope)))
2628 error ("incomplete type '%T' cannot be used to name a scope",
2630 return error_mark_node;
2633 if (BASELINK_P (name))
2636 fn = lookup_member (scope, name, /*protect=*/1, /*prefer_type=*/0);
2637 if (fn && current_class_type)
2638 fn = (adjust_result_of_qualified_name_lookup
2639 (fn, scope, current_class_type));
2644 error ("'%D' has no member named '%E'", scope, name);
2645 return error_mark_node;
2651 if (BASELINK_P (fn))
2652 fns = BASELINK_FUNCTIONS (fns);
2653 fns = build_nt (TEMPLATE_ID_EXPR, fns, template_args);
2654 if (BASELINK_P (fn))
2655 BASELINK_FUNCTIONS (fn) = fns;
2663 /* Do any initial processing on the arguments to a function call. */
2666 resolve_args (tree args)
2669 for (t = args; t; t = TREE_CHAIN (t))
2671 tree arg = TREE_VALUE (t);
2673 if (arg == error_mark_node)
2674 return error_mark_node;
2675 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2677 error ("invalid use of void expression");
2678 return error_mark_node;
2680 else if (TREE_CODE (arg) == OFFSET_REF)
2681 arg = resolve_offset_ref (arg);
2682 arg = convert_from_reference (arg);
2683 TREE_VALUE (t) = arg;
2688 /* Return an expression for a call to FN (a namespace-scope function)
2692 build_new_function_call (tree fn, tree args)
2694 struct z_candidate *candidates = 0, *cand;
2695 tree explicit_targs = NULL_TREE;
2696 int template_only = 0;
2698 /* Check FN and ARGS. */
2699 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2700 || TREE_CODE (fn) == TEMPLATE_DECL
2701 || TREE_CODE (fn) == OVERLOAD
2702 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2704 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2707 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2709 explicit_targs = TREE_OPERAND (fn, 1);
2710 fn = TREE_OPERAND (fn, 0);
2714 if (really_overloaded_fn (fn)
2715 || TREE_CODE (fn) == TEMPLATE_DECL)
2718 tree templates = NULL_TREE;
2720 args = resolve_args (args);
2722 if (args == error_mark_node)
2723 return error_mark_node;
2725 for (t1 = fn; t1; t1 = OVL_NEXT (t1))
2727 tree t = OVL_CURRENT (t1);
2729 if (TREE_CODE (t) == TEMPLATE_DECL)
2731 templates = tree_cons (NULL_TREE, t, templates);
2732 candidates = add_template_candidate
2733 (candidates, t, NULL_TREE, explicit_targs, args,
2735 /*access_path=*/NULL_TREE, /*conversion_path=*/NULL_TREE,
2736 LOOKUP_NORMAL, DEDUCE_CALL);
2738 else if (! template_only)
2739 candidates = add_function_candidate
2740 (candidates, t, NULL_TREE, args, /*access_path=*/NULL_TREE,
2741 /*conversion_path=*/NULL_TREE, LOOKUP_NORMAL);
2744 if (! any_viable (candidates))
2746 if (candidates && ! candidates->next)
2747 return build_function_call (candidates->fn, args);
2748 error ("no matching function for call to `%D(%A)'",
2749 DECL_NAME (OVL_CURRENT (fn)), args);
2751 print_z_candidates (candidates);
2752 return error_mark_node;
2754 candidates = splice_viable (candidates);
2755 cand = tourney (candidates);
2759 error ("call of overloaded `%D(%A)' is ambiguous",
2760 DECL_NAME (OVL_FUNCTION (fn)), args);
2761 print_z_candidates (candidates);
2762 return error_mark_node;
2765 return build_over_call (cand, args, LOOKUP_NORMAL);
2768 /* This is not really overloaded. */
2769 fn = OVL_CURRENT (fn);
2771 return build_function_call (fn, args);
2775 build_object_call (tree obj, tree 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 (enum tree_code code, enum tree_code code2,
2886 tree arg1, tree arg2, tree arg3, const char *problem)
2890 if (code == MODIFY_EXPR)
2891 opname = assignment_operator_name_info[code2].name;
2893 opname = operator_name_info[code].name;
2898 error ("%s for `%T ? %T : %T' operator", problem,
2899 error_type (arg1), error_type (arg2), error_type (arg3));
2901 case POSTINCREMENT_EXPR:
2902 case POSTDECREMENT_EXPR:
2903 error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2906 error ("%s for `%T [%T]' operator", problem,
2907 error_type (arg1), error_type (arg2));
2911 error ("%s for `%T %s %T' operator", problem,
2912 error_type (arg1), opname, error_type (arg2));
2914 error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2918 /* Return the implicit conversion sequence that could be used to
2919 convert E1 to E2 in [expr.cond]. */
2922 conditional_conversion (tree e1, tree e2)
2924 tree t1 = non_reference (TREE_TYPE (e1));
2925 tree t2 = non_reference (TREE_TYPE (e2));
2930 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2931 implicitly converted (clause _conv_) to the type "reference to
2932 T2", subject to the constraint that in the conversion the
2933 reference must bind directly (_dcl.init.ref_) to E1. */
2934 if (real_lvalue_p (e2))
2936 conv = implicit_conversion (build_reference_type (t2),
2939 LOOKUP_NO_TEMP_BIND);
2946 If E1 and E2 have class type, and the underlying class types are
2947 the same or one is a base class of the other: E1 can be converted
2948 to match E2 if the class of T2 is the same type as, or a base
2949 class of, the class of T1, and the cv-qualification of T2 is the
2950 same cv-qualification as, or a greater cv-qualification than, the
2951 cv-qualification of T1. If the conversion is applied, E1 is
2952 changed to an rvalue of type T2 that still refers to the original
2953 source class object (or the appropriate subobject thereof). */
2954 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2955 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2956 TYPE_MAIN_VARIANT (t1)))
2958 if (at_least_as_qualified_p (t2, t1))
2960 conv = build1 (IDENTITY_CONV, t1, e1);
2961 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2962 TYPE_MAIN_VARIANT (t2)))
2963 conv = build_conv (BASE_CONV, t2, conv);
2972 E1 can be converted to match E2 if E1 can be implicitly converted
2973 to the type that expression E2 would have if E2 were converted to
2974 an rvalue (or the type it has, if E2 is an rvalue). */
2975 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2978 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2979 arguments to the conditional expression. */
2982 build_conditional_expr (tree arg1, tree arg2, tree arg3)
2987 tree result_type = NULL_TREE;
2988 bool lvalue_p = true;
2989 struct z_candidate *candidates = 0;
2990 struct z_candidate *cand;
2992 /* As a G++ extension, the second argument to the conditional can be
2993 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2994 c'.) If the second operand is omitted, make sure it is
2995 calculated only once. */
2999 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3000 arg1 = arg2 = save_expr (arg1);
3005 The first expr ession is implicitly converted to bool (clause
3007 arg1 = cp_convert (boolean_type_node, arg1);
3009 /* If something has already gone wrong, just pass that fact up the
3011 if (arg1 == error_mark_node
3012 || arg2 == error_mark_node
3013 || arg3 == error_mark_node
3014 || TREE_TYPE (arg1) == error_mark_node
3015 || TREE_TYPE (arg2) == error_mark_node
3016 || TREE_TYPE (arg3) == error_mark_node)
3017 return error_mark_node;
3021 If either the second or the third operand has type (possibly
3022 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3023 array-to-pointer (_conv.array_), and function-to-pointer
3024 (_conv.func_) standard conversions are performed on the second
3025 and third operands. */
3026 arg2_type = TREE_TYPE (arg2);
3027 arg3_type = TREE_TYPE (arg3);
3028 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3030 /* Do the conversions. We don't these for `void' type arguments
3031 since it can't have any effect and since decay_conversion
3032 does not handle that case gracefully. */
3033 if (!VOID_TYPE_P (arg2_type))
3034 arg2 = decay_conversion (arg2);
3035 if (!VOID_TYPE_P (arg3_type))
3036 arg3 = decay_conversion (arg3);
3037 arg2_type = TREE_TYPE (arg2);
3038 arg3_type = TREE_TYPE (arg3);
3042 One of the following shall hold:
3044 --The second or the third operand (but not both) is a
3045 throw-expression (_except.throw_); the result is of the
3046 type of the other and is an rvalue.
3048 --Both the second and the third operands have type void; the
3049 result is of type void and is an rvalue. */
3050 if ((TREE_CODE (arg2) == THROW_EXPR)
3051 ^ (TREE_CODE (arg3) == THROW_EXPR))
3052 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
3053 ? arg3_type : arg2_type);
3054 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3055 result_type = void_type_node;
3058 error ("`%E' has type `void' and is not a throw-expression",
3059 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3060 return error_mark_node;
3064 goto valid_operands;
3068 Otherwise, if the second and third operand have different types,
3069 and either has (possibly cv-qualified) class type, an attempt is
3070 made to convert each of those operands to the type of the other. */
3071 else if (!same_type_p (arg2_type, arg3_type)
3072 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3074 tree conv2 = conditional_conversion (arg2, arg3);
3075 tree conv3 = conditional_conversion (arg3, arg2);
3079 If both can be converted, or one can be converted but the
3080 conversion is ambiguous, the program is ill-formed. If
3081 neither can be converted, the operands are left unchanged and
3082 further checking is performed as described below. If exactly
3083 one conversion is possible, that conversion is applied to the
3084 chosen operand and the converted operand is used in place of
3085 the original operand for the remainder of this section. */
3086 if ((conv2 && !ICS_BAD_FLAG (conv2)
3087 && conv3 && !ICS_BAD_FLAG (conv3))
3088 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3089 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3091 error ("operands to ?: have different types");
3092 return error_mark_node;
3094 else if (conv2 && !ICS_BAD_FLAG (conv2))
3096 arg2 = convert_like (conv2, arg2);
3097 arg2 = convert_from_reference (arg2);
3098 /* That may not quite have done the trick. If the two types
3099 are cv-qualified variants of one another, we will have
3100 just used an IDENTITY_CONV. (There's no conversion from
3101 an lvalue of one class type to an lvalue of another type,
3102 even a cv-qualified variant, and we don't want to lose
3103 lvalue-ness here.) So, we manually add a NOP_EXPR here
3105 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3106 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3107 arg2_type = TREE_TYPE (arg2);
3109 else if (conv3 && !ICS_BAD_FLAG (conv3))
3111 arg3 = convert_like (conv3, arg3);
3112 arg3 = convert_from_reference (arg3);
3113 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3114 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3115 arg3_type = TREE_TYPE (arg3);
3121 If the second and third operands are lvalues and have the same
3122 type, the result is of that type and is an lvalue. */
3123 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3124 same_type_p (arg2_type, arg3_type))
3126 result_type = arg2_type;
3127 goto valid_operands;
3132 Otherwise, the result is an rvalue. If the second and third
3133 operand do not have the same type, and either has (possibly
3134 cv-qualified) class type, overload resolution is used to
3135 determine the conversions (if any) to be applied to the operands
3136 (_over.match.oper_, _over.built_). */
3138 if (!same_type_p (arg2_type, arg3_type)
3139 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3144 /* Rearrange the arguments so that add_builtin_candidate only has
3145 to know about two args. In build_builtin_candidates, the
3146 arguments are unscrambled. */
3150 candidates = add_builtin_candidates (candidates,
3153 ansi_opname (COND_EXPR),
3159 If the overload resolution fails, the program is
3161 if (!any_viable (candidates))
3163 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3164 print_z_candidates (candidates);
3165 return error_mark_node;
3167 candidates = splice_viable (candidates);
3168 cand = tourney (candidates);
3171 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3172 print_z_candidates (candidates);
3173 return error_mark_node;
3178 Otherwise, the conversions thus determined are applied, and
3179 the converted operands are used in place of the original
3180 operands for the remainder of this section. */
3181 conv = TREE_VEC_ELT (cand->convs, 0);
3182 arg1 = convert_like (conv, arg1);
3183 conv = TREE_VEC_ELT (cand->convs, 1);
3184 arg2 = convert_like (conv, arg2);
3185 conv = TREE_VEC_ELT (cand->convs, 2);
3186 arg3 = convert_like (conv, arg3);
3191 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3192 and function-to-pointer (_conv.func_) standard conversions are
3193 performed on the second and third operands.
3195 We need to force the lvalue-to-rvalue conversion here for class types,
3196 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3197 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3200 We use ocp_convert rather than build_user_type_conversion because the
3201 latter returns NULL_TREE on failure, while the former gives an error. */
3203 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3204 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3205 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3207 arg2 = decay_conversion (arg2);
3208 arg2_type = TREE_TYPE (arg2);
3210 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3211 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3212 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3214 arg3 = decay_conversion (arg3);
3215 arg3_type = TREE_TYPE (arg3);
3217 if (arg2 == error_mark_node || arg3 == error_mark_node)
3218 return error_mark_node;
3222 After those conversions, one of the following shall hold:
3224 --The second and third operands have the same type; the result is of
3226 if (same_type_p (arg2_type, arg3_type))
3227 result_type = arg2_type;
3230 --The second and third operands have arithmetic or enumeration
3231 type; the usual arithmetic conversions are performed to bring
3232 them to a common type, and the result is of that type. */
3233 else if ((ARITHMETIC_TYPE_P (arg2_type)
3234 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3235 && (ARITHMETIC_TYPE_P (arg3_type)
3236 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3238 /* In this case, there is always a common type. */
3239 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3242 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3243 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3244 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3245 arg2_type, arg3_type);
3246 else if (extra_warnings
3247 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3248 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3249 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3250 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3251 warning ("enumeral and non-enumeral type in conditional expression");
3253 arg2 = perform_implicit_conversion (result_type, arg2);
3254 arg3 = perform_implicit_conversion (result_type, arg3);
3258 --The second and third operands have pointer type, or one has
3259 pointer type and the other is a null pointer constant; pointer
3260 conversions (_conv.ptr_) and qualification conversions
3261 (_conv.qual_) are performed to bring them to their composite
3262 pointer type (_expr.rel_). The result is of the composite
3265 --The second and third operands have pointer to member type, or
3266 one has pointer to member type and the other is a null pointer
3267 constant; pointer to member conversions (_conv.mem_) and
3268 qualification conversions (_conv.qual_) are performed to bring
3269 them to a common type, whose cv-qualification shall match the
3270 cv-qualification of either the second or the third operand.
3271 The result is of the common type. */
3272 else if ((null_ptr_cst_p (arg2)
3273 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3274 || TYPE_PTRMEMFUNC_P (arg3_type)))
3275 || (null_ptr_cst_p (arg3)
3276 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3277 || TYPE_PTRMEMFUNC_P (arg2_type)))
3278 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3279 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3280 || (TYPE_PTRMEMFUNC_P (arg2_type)
3281 && TYPE_PTRMEMFUNC_P (arg3_type)))
3283 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3284 arg3, "conditional expression");
3285 arg2 = perform_implicit_conversion (result_type, arg2);
3286 arg3 = perform_implicit_conversion (result_type, arg3);
3291 error ("operands to ?: have different types");
3292 return error_mark_node;
3296 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3297 /* Expand both sides into the same slot, hopefully the target of the
3298 ?: expression. We used to check for TARGET_EXPRs here, but now we
3299 sometimes wrap them in NOP_EXPRs so the test would fail. */
3300 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3301 result = build_target_expr_with_type (result, result_type);
3303 /* If this expression is an rvalue, but might be mistaken for an
3304 lvalue, we must add a NON_LVALUE_EXPR. */
3305 if (!lvalue_p && real_lvalue_p (result))
3306 result = build1 (NON_LVALUE_EXPR, result_type, result);
3312 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3)
3314 struct z_candidate *candidates = 0, *cand;
3315 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3316 enum tree_code code2 = NOP_EXPR;
3317 tree templates = NULL_TREE;
3319 bool viable_candidates;
3321 if (arg1 == error_mark_node
3322 || arg2 == error_mark_node
3323 || arg3 == error_mark_node)
3324 return error_mark_node;
3326 /* This can happen if a template takes all non-type parameters, e.g.
3327 undeclared_template<1, 5, 72>a; */
3328 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3330 error ("`%D' must be declared before use", arg1);
3331 return error_mark_node;
3334 if (code == MODIFY_EXPR)
3336 code2 = TREE_CODE (arg3);
3338 fnname = ansi_assopname (code2);
3341 fnname = ansi_opname (code);
3343 if (TREE_CODE (arg1) == OFFSET_REF)
3344 arg1 = resolve_offset_ref (arg1);
3345 arg1 = convert_from_reference (arg1);
3346 if (CLASS_TYPE_P (TREE_TYPE (arg1))
3347 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg1)))
3348 /* Make sure the template type is instantiated now. */
3349 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)));
3355 case VEC_DELETE_EXPR:
3357 /* Use build_op_new_call and build_op_delete_call instead. */
3361 return build_object_call (arg1, arg2);
3369 if (TREE_CODE (arg2) == OFFSET_REF)
3370 arg2 = resolve_offset_ref (arg2);
3371 arg2 = convert_from_reference (arg2);
3372 if (CLASS_TYPE_P (TREE_TYPE (arg2))
3373 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg2)))
3374 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (arg2)));
3378 if (TREE_CODE (arg3) == OFFSET_REF)
3379 arg3 = resolve_offset_ref (arg3);
3380 arg3 = convert_from_reference (arg3);
3381 if (CLASS_TYPE_P (TREE_TYPE (arg3))
3382 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (arg3)))
3383 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (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 (enum tree_code code, tree addr, tree size,
3714 int flags, tree placement)
3716 tree fn = NULL_TREE;
3717 tree fns, fnname, fntype, argtypes, args, type;
3720 if (addr == error_mark_node)
3721 return error_mark_node;
3723 type = TREE_TYPE (TREE_TYPE (addr));
3724 while (TREE_CODE (type) == ARRAY_TYPE)
3725 type = TREE_TYPE (type);
3727 fnname = ansi_opname (code);
3729 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3732 If the result of the lookup is ambiguous or inaccessible, or if
3733 the lookup selects a placement deallocation function, the
3734 program is ill-formed.
3736 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3738 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3739 if (fns == error_mark_node)
3740 return error_mark_node;
3745 if (fns == NULL_TREE)
3746 fns = lookup_name_nonclass (fnname);
3753 /* Find the allocation function that is being called. */
3754 call_expr = placement;
3755 /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3757 while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3758 call_expr = TREE_OPERAND (call_expr, 1);
3759 /* Extract the function. */
3760 alloc_fn = get_callee_fndecl (call_expr);
3761 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3762 /* Then the second parm type. */
3763 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3764 /* Also the second argument. */
3765 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3769 /* First try it without the size argument. */
3770 argtypes = void_list_node;
3774 /* Strip const and volatile from addr. */
3775 addr = cp_convert (ptr_type_node, addr);
3777 /* We make two tries at finding a matching `operator delete'. On
3778 the first pass, we look for an one-operator (or placement)
3779 operator delete. If we're not doing placement delete, then on
3780 the second pass we look for a two-argument delete. */
3781 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3784 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3786 /* Normal delete; now try to find a match including the size
3788 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3789 tree_cons (NULL_TREE, sizetype,
3791 fntype = build_function_type (void_type_node, argtypes);
3793 /* Go through the `operator delete' functions looking for one
3794 with a matching type. */
3795 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3801 /* Exception specifications on the `delete' operator do not
3803 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3805 /* We also don't compare attributes. We're really just
3806 trying to check the types of the first two parameters. */
3807 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3811 /* If we found a match, we're done. */
3816 /* If we have a matching function, call it. */
3819 /* Make sure we have the actual function, and not an
3821 fn = OVL_CURRENT (fn);
3823 /* If the FN is a member function, make sure that it is
3825 if (DECL_CLASS_SCOPE_P (fn))
3826 enforce_access (type, fn);
3829 args = tree_cons (NULL_TREE, addr, args);
3831 args = tree_cons (NULL_TREE, addr,
3832 build_tree_list (NULL_TREE, size));
3834 return build_function_call (fn, args);
3837 /* If we are doing placement delete we do nothing if we don't find a
3838 matching op delete. */
3842 error ("no suitable `operator delete' for `%T'", type);
3843 return error_mark_node;
3846 /* If the current scope isn't allowed to access DECL along
3847 BASETYPE_PATH, give an error. The most derived class in
3848 BASETYPE_PATH is the one used to qualify DECL. */
3851 enforce_access (tree basetype_path, tree decl)
3853 if (!accessible_p (basetype_path, decl))
3855 if (TREE_PRIVATE (decl))
3856 cp_error_at ("`%+#D' is private", decl);
3857 else if (TREE_PROTECTED (decl))
3858 cp_error_at ("`%+#D' is protected", decl);
3860 cp_error_at ("`%+#D' is inaccessible", decl);
3861 error ("within this context");
3868 /* Perform the conversions in CONVS on the expression EXPR.
3869 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3870 indicates the `this' argument of a method. INNER is nonzero when
3871 being called to continue a conversion chain. It is negative when a
3872 reference binding will be applied, positive otherwise. */
3875 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner)
3879 tree totype = TREE_TYPE (convs);
3881 if (ICS_BAD_FLAG (convs)
3882 && TREE_CODE (convs) != USER_CONV
3883 && TREE_CODE (convs) != AMBIG_CONV
3884 && TREE_CODE (convs) != REF_BIND)
3887 for (; t; t = TREE_OPERAND (t, 0))
3889 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3891 expr = convert_like_real (t, expr, fn, argnum, 1);
3894 else if (TREE_CODE (t) == AMBIG_CONV)
3895 return convert_like_real (t, expr, fn, argnum, 1);
3896 else if (TREE_CODE (t) == IDENTITY_CONV)
3899 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3901 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3902 return cp_convert (totype, expr);
3906 expr = dubious_conversion_warnings
3907 (totype, expr, "argument", fn, argnum);
3908 switch (TREE_CODE (convs))
3912 struct z_candidate *cand
3913 = WRAPPER_ZC (TREE_OPERAND (convs, 1));
3914 tree convfn = cand->fn;
3917 if (DECL_CONSTRUCTOR_P (convfn))
3919 tree t = build_int_2 (0, 0);
3920 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3922 args = build_tree_list (NULL_TREE, expr);
3923 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3924 || DECL_HAS_VTT_PARM_P (convfn))
3925 /* We should never try to call the abstract or base constructor
3928 args = tree_cons (NULL_TREE, t, args);
3931 args = build_this (expr);
3932 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3934 /* If this is a constructor or a function returning an aggr type,
3935 we need to build up a TARGET_EXPR. */
3936 if (DECL_CONSTRUCTOR_P (convfn))
3937 expr = build_cplus_new (totype, expr);
3939 /* The result of the call is then used to direct-initialize the object
3940 that is the destination of the copy-initialization. [dcl.init]
3942 Note that this step is not reflected in the conversion sequence;
3943 it affects the semantics when we actually perform the
3944 conversion, but is not considered during overload resolution.
3946 If the target is a class, that means call a ctor. */
3947 if (IS_AGGR_TYPE (totype)
3948 && (inner >= 0 || !lvalue_p (expr)))
3950 savew = warningcount, savee = errorcount;
3951 expr = build_special_member_call
3952 (NULL_TREE, complete_ctor_identifier,
3953 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3954 /* Core issue 84, now a DR, says that we don't allow UDCs
3955 for these args (which deliberately breaks copy-init of an
3956 auto_ptr<Base> from an auto_ptr<Derived>). */
3957 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3959 /* Tell the user where this failing constructor call came from. */
3962 if (warningcount > savew)
3964 (" initializing argument %P of `%D' from result of `%D'",
3965 argnum, fn, convfn);
3966 else if (errorcount > savee)
3968 (" initializing argument %P of `%D' from result of `%D'",
3969 argnum, fn, convfn);
3973 if (warningcount > savew)
3974 warning (" initializing temporary from result of `%D'",
3976 else if (errorcount > savee)
3977 error (" initializing temporary from result of `%D'",
3980 expr = build_cplus_new (totype, expr);
3985 if (type_unknown_p (expr))
3986 expr = instantiate_type (totype, expr, tf_error | tf_warning);
3989 /* Call build_user_type_conversion again for the error. */
3990 return build_user_type_conversion
3991 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3997 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3998 TREE_CODE (convs) == REF_BIND ? -1 : 1);
3999 if (expr == error_mark_node)
4000 return error_mark_node;
4002 /* Convert a non-array constant variable to its underlying value, unless we
4003 are about to bind it to a reference, in which case we need to
4004 leave it as an lvalue. */
4005 if (TREE_CODE (convs) != REF_BIND
4006 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4007 expr = decl_constant_value (expr);
4009 switch (TREE_CODE (convs))
4012 if (! IS_AGGR_TYPE (totype))
4014 /* else fall through */
4016 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4018 /* We are going to bind a reference directly to a base-class
4019 subobject of EXPR. */
4020 tree base_ptr = build_pointer_type (totype);
4022 /* Build an expression for `*((base*) &expr)'. */
4023 expr = build_unary_op (ADDR_EXPR, expr, 0);
4024 expr = perform_implicit_conversion (base_ptr, expr);
4025 expr = build_indirect_ref (expr, "implicit conversion");
4029 /* Copy-initialization where the cv-unqualified version of the source
4030 type is the same class as, or a derived class of, the class of the
4031 destination [is treated as direct-initialization]. [dcl.init] */
4032 savew = warningcount, savee = errorcount;
4033 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4034 build_tree_list (NULL_TREE, expr),
4035 TYPE_BINFO (totype),
4036 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
4039 if (warningcount > savew)
4040 warning (" initializing argument %P of `%D'", argnum, fn);
4041 else if (errorcount > savee)
4042 error (" initializing argument %P of `%D'", argnum, fn);
4044 return build_cplus_new (totype, expr);
4048 tree ref_type = totype;
4050 /* If necessary, create a temporary. */
4051 if (NEED_TEMPORARY_P (convs) || !non_cast_lvalue_p (expr))
4053 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4054 expr = build_target_expr_with_type (expr, type);
4057 /* Take the address of the thing to which we will bind the
4059 expr = build_unary_op (ADDR_EXPR, expr, 1);
4060 if (expr == error_mark_node)
4061 return error_mark_node;
4063 /* Convert it to a pointer to the type referred to by the
4064 reference. This will adjust the pointer if a derived to
4065 base conversion is being performed. */
4066 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4068 /* Convert the pointer to the desired reference type. */
4069 expr = build1 (NOP_EXPR, ref_type, expr);
4075 return decay_conversion (expr);
4078 /* Warn about deprecated conversion if appropriate. */
4079 string_conv_p (totype, expr, 1);
4085 return ocp_convert (totype, expr, CONV_IMPLICIT,
4086 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4089 /* Build a call to __builtin_trap which can be used in an expression. */
4092 call_builtin_trap (void)
4094 tree fn = get_identifier ("__builtin_trap");
4095 if (IDENTIFIER_GLOBAL_VALUE (fn))
4096 fn = IDENTIFIER_GLOBAL_VALUE (fn);
4100 fn = build_call (fn, NULL_TREE);
4101 fn = build (COMPOUND_EXPR, integer_type_node, fn, integer_zero_node);
4105 /* ARG is being passed to a varargs function. Perform any conversions
4106 required. Array/function to pointer decay must have already happened.
4107 Return the converted value. */
4110 convert_arg_to_ellipsis (tree arg)
4112 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4113 && (TYPE_PRECISION (TREE_TYPE (arg))
4114 < TYPE_PRECISION (double_type_node)))
4115 /* Convert `float' to `double'. */
4116 arg = cp_convert (double_type_node, arg);
4118 /* Convert `short' and `char' to full-size `int'. */
4119 arg = default_conversion (arg);
4121 arg = require_complete_type (arg);
4123 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4125 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4126 here and do a bitwise copy, but now cp_expr_size will abort if we
4128 warning ("cannot pass objects of non-POD type `%#T' through `...'; \
4129 call will abort at runtime",
4131 arg = call_builtin_trap ();
4137 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4140 build_x_va_arg (tree expr, tree type)
4142 if (processing_template_decl)
4143 return build_min (VA_ARG_EXPR, type, expr);
4145 type = complete_type_or_else (type, NULL_TREE);
4147 if (expr == error_mark_node || !type)
4148 return error_mark_node;
4150 if (! pod_type_p (type))
4152 /* Undefined behavior [expr.call] 5.2.2/7. */
4153 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4157 return build_va_arg (expr, type);
4160 /* TYPE has been given to va_arg. Apply the default conversions which
4161 would have happened when passed via ellipsis. Return the promoted
4162 type, or the passed type if there is no change. */
4165 cxx_type_promotes_to (tree type)
4169 if (TREE_CODE (type) == ARRAY_TYPE)
4170 return build_pointer_type (TREE_TYPE (type));
4172 if (TREE_CODE (type) == FUNCTION_TYPE)
4173 return build_pointer_type (type);
4175 promote = type_promotes_to (type);
4176 if (same_type_p (type, promote))
4182 /* ARG is a default argument expression being passed to a parameter of
4183 the indicated TYPE, which is a parameter to FN. Do any required
4184 conversions. Return the converted value. */
4187 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4189 if (TREE_CODE (arg) == DEFAULT_ARG)
4191 /* When processing the default args for a class, we can find that
4192 there is an ordering constraint, and we call a function who's
4193 default args have not yet been converted. For instance,
4196 void Foo (A const & = A ());
4198 We must process A::A before A::Foo's default arg can be converted.
4199 Remember the dependent function, so do_pending_defargs can retry,
4201 unprocessed_defarg_fn (fn);
4203 /* Don't return error_mark node, as we won't be able to distinguish
4204 genuine errors from this case, and that would lead to repeated
4205 diagnostics. Just make something of the right type. */
4206 return build1 (NOP_EXPR, type, integer_zero_node);
4209 if (fn && DECL_TEMPLATE_INFO (fn))
4210 arg = tsubst_default_argument (fn, type, arg);
4212 arg = break_out_target_exprs (arg);
4214 if (TREE_CODE (arg) == CONSTRUCTOR)
4216 arg = digest_init (type, arg, 0);
4217 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4218 "default argument", fn, parmnum);
4222 /* This could get clobbered by the following call. */
4223 if (TREE_HAS_CONSTRUCTOR (arg))
4224 arg = copy_node (arg);
4226 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4227 "default argument", fn, parmnum);
4228 arg = convert_for_arg_passing (type, arg);
4234 /* Returns the type which will really be used for passing an argument of
4238 type_passed_as (tree type)
4240 /* Pass classes with copy ctors by invisible reference. */
4241 if (TREE_ADDRESSABLE (type))
4242 type = build_reference_type (type);
4243 else if (PROMOTE_PROTOTYPES
4244 && INTEGRAL_TYPE_P (type)
4245 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4246 type = integer_type_node;
4251 /* Actually perform the appropriate conversion. */
4254 convert_for_arg_passing (tree type, tree val)
4256 if (val == error_mark_node)
4258 /* Pass classes with copy ctors by invisible reference. */
4259 else if (TREE_ADDRESSABLE (type))
4260 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4261 else if (PROMOTE_PROTOTYPES
4262 && INTEGRAL_TYPE_P (type)
4263 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4264 val = default_conversion (val);
4268 /* Subroutine of the various build_*_call functions. Overload resolution
4269 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4270 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4271 bitmask of various LOOKUP_* flags which apply to the call itself. */
4274 build_over_call (struct z_candidate *cand, tree args, int flags)
4277 tree convs = cand->convs;
4278 tree converted_args = NULL_TREE;
4279 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4280 tree conv, arg, val;
4284 /* Give any warnings we noticed during overload resolution. */
4286 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4287 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4289 if (DECL_FUNCTION_MEMBER_P (fn))
4290 enforce_access (cand->access_path, fn);
4292 if (args && TREE_CODE (args) != TREE_LIST)
4293 args = build_tree_list (NULL_TREE, args);
4296 /* The implicit parameters to a constructor are not considered by overload
4297 resolution, and must be of the proper type. */
4298 if (DECL_CONSTRUCTOR_P (fn))
4300 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4301 arg = TREE_CHAIN (arg);
4302 parm = TREE_CHAIN (parm);
4303 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4304 /* We should never try to call the abstract constructor. */
4306 if (DECL_HAS_VTT_PARM_P (fn))
4308 converted_args = tree_cons
4309 (NULL_TREE, TREE_VALUE (arg), converted_args);
4310 arg = TREE_CHAIN (arg);
4311 parm = TREE_CHAIN (parm);
4314 /* Bypass access control for 'this' parameter. */
4315 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4317 tree parmtype = TREE_VALUE (parm);
4318 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4322 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4323 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4324 TREE_TYPE (argtype), fn);
4326 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4327 X is called for an object that is not of type X, or of a type
4328 derived from X, the behavior is undefined.
4330 So we can assume that anything passed as 'this' is non-null, and
4331 optimize accordingly. */
4332 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4333 /* Convert to the base in which the function was declared. */
4334 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4335 converted_arg = build_base_path (PLUS_EXPR,
4337 cand->conversion_path,
4339 /* If fn was found by a using declaration, the conversion path
4340 will be to the derived class, not the base declaring fn. We
4341 must convert from derived to base. */
4342 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4343 TREE_TYPE (parmtype), ba_ignore, NULL);
4345 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4348 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4349 parm = TREE_CHAIN (parm);
4350 arg = TREE_CHAIN (arg);
4356 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4358 tree type = TREE_VALUE (parm);
4360 conv = TREE_VEC_ELT (convs, i);
4361 val = convert_like_with_context
4362 (conv, TREE_VALUE (arg), fn, i - is_method);
4364 val = convert_for_arg_passing (type, val);
4365 converted_args = tree_cons (NULL_TREE, val, converted_args);
4368 /* Default arguments */
4369 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4371 = tree_cons (NULL_TREE,
4372 convert_default_arg (TREE_VALUE (parm),
4373 TREE_PURPOSE (parm),
4378 for (; arg; arg = TREE_CHAIN (arg))
4380 = tree_cons (NULL_TREE,
4381 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4384 converted_args = nreverse (converted_args);
4387 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4390 /* Avoid actually calling copy constructors and copy assignment operators,
4393 if (! flag_elide_constructors)
4394 /* Do things the hard way. */;
4395 else if (TREE_VEC_LENGTH (convs) == 1
4396 && DECL_COPY_CONSTRUCTOR_P (fn))
4399 arg = skip_artificial_parms_for (fn, converted_args);
4400 arg = TREE_VALUE (arg);
4402 /* Pull out the real argument, disregarding const-correctness. */
4404 while (TREE_CODE (targ) == NOP_EXPR
4405 || TREE_CODE (targ) == NON_LVALUE_EXPR
4406 || TREE_CODE (targ) == CONVERT_EXPR)
4407 targ = TREE_OPERAND (targ, 0);
4408 if (TREE_CODE (targ) == ADDR_EXPR)
4410 targ = TREE_OPERAND (targ, 0);
4411 if (!same_type_ignoring_top_level_qualifiers_p
4412 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4421 arg = build_indirect_ref (arg, 0);
4423 /* [class.copy]: the copy constructor is implicitly defined even if
4424 the implementation elided its use. */
4425 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4428 /* If we're creating a temp and we already have one, don't create a
4429 new one. If we're not creating a temp but we get one, use
4430 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4431 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4432 temp or an INIT_EXPR otherwise. */
4433 if (integer_zerop (TREE_VALUE (args)))
4435 if (TREE_CODE (arg) == TARGET_EXPR)
4437 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4438 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4440 else if (TREE_CODE (arg) == TARGET_EXPR
4441 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4444 tree to = stabilize_reference
4445 (build_indirect_ref (TREE_VALUE (args), 0));
4447 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4448 address = build_unary_op (ADDR_EXPR, val, 0);
4449 /* Avoid a warning about this expression, if the address is
4451 TREE_USED (address) = 1;
4455 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4457 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4459 tree to = stabilize_reference
4460 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4462 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4463 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4469 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4471 tree t, *p = &TREE_VALUE (converted_args);
4472 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4475 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4477 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4478 if (TREE_SIDE_EFFECTS (*p))
4479 *p = save_expr (*p);
4480 t = build_pointer_type (TREE_TYPE (fn));
4481 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4482 fn = build_java_interface_fn_ref (fn, *p);
4484 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4487 else if (DECL_INLINE (fn))
4488 fn = inline_conversion (fn);
4490 fn = build_addr_func (fn);
4492 /* Recognize certain built-in functions so we can make tree-codes
4493 other than CALL_EXPR. We do this when it enables fold-const.c
4494 to do something useful. */
4496 if (TREE_CODE (fn) == ADDR_EXPR
4497 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4498 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4501 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4506 /* Some built-in function calls will be evaluated at
4507 compile-time in fold (). */
4508 fn = fold (build_call (fn, converted_args));
4509 if (VOID_TYPE_P (TREE_TYPE (fn)))
4511 fn = require_complete_type (fn);
4512 if (fn == error_mark_node)
4513 return error_mark_node;
4514 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4515 fn = build_cplus_new (TREE_TYPE (fn), fn);
4516 return convert_from_reference (fn);
4519 static GTY(()) tree java_iface_lookup_fn;
4521 /* Make an expression which yields the address of the Java interface
4522 method FN. This is achieved by generating a call to libjava's
4523 _Jv_LookupInterfaceMethodIdx(). */
4526 build_java_interface_fn_ref (tree fn, tree instance)
4528 tree lookup_args, lookup_fn, method, idx;
4529 tree klass_ref, iface, iface_ref;
4532 if (!java_iface_lookup_fn)
4534 tree endlink = build_void_list_node ();
4535 tree t = tree_cons (NULL_TREE, ptr_type_node,
4536 tree_cons (NULL_TREE, ptr_type_node,
4537 tree_cons (NULL_TREE, java_int_type_node,
4539 java_iface_lookup_fn
4540 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4541 build_function_type (ptr_type_node, t),
4542 0, NOT_BUILT_IN, NULL, NULL_TREE);
4545 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4546 This is the first entry in the vtable. */
4547 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4550 /* Get the java.lang.Class pointer for the interface being called. */
4551 iface = DECL_CONTEXT (fn);
4552 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4553 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4554 || DECL_CONTEXT (iface_ref) != iface)
4556 error ("could not find class$ field in java interface type `%T'",
4558 return error_mark_node;
4560 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4562 /* Determine the itable index of FN. */
4564 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4566 if (!DECL_VIRTUAL_P (method))
4572 idx = build_int_2 (i, 0);
4574 lookup_args = tree_cons (NULL_TREE, klass_ref,
4575 tree_cons (NULL_TREE, iface_ref,
4576 build_tree_list (NULL_TREE, idx)));
4577 lookup_fn = build1 (ADDR_EXPR,
4578 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4579 java_iface_lookup_fn);
4580 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4583 /* Returns the value to use for the in-charge parameter when making a
4584 call to a function with the indicated NAME. */
4587 in_charge_arg_for_name (tree name)
4589 if (name == base_ctor_identifier
4590 || name == base_dtor_identifier)
4591 return integer_zero_node;
4592 else if (name == complete_ctor_identifier)
4593 return integer_one_node;
4594 else if (name == complete_dtor_identifier)
4595 return integer_two_node;
4596 else if (name == deleting_dtor_identifier)
4597 return integer_three_node;
4599 /* This function should only be called with one of the names listed
4605 /* Build a call to a constructor, destructor, or an assignment
4606 operator for INSTANCE, an expression with class type. NAME
4607 indicates the special member function to call; ARGS are the
4608 arguments. BINFO indicates the base of INSTANCE that is to be
4609 passed as the `this' parameter to the member function called.
4611 FLAGS are the LOOKUP_* flags to use when processing the call.
4613 If NAME indicates a complete object constructor, INSTANCE may be
4614 NULL_TREE. In this case, the caller will call build_cplus_new to
4615 store the newly constructed object into a VAR_DECL. */
4618 build_special_member_call (tree instance, tree name, tree args,
4619 tree binfo, int flags)
4622 /* The type of the subobject to be constructed or destroyed. */
4625 my_friendly_assert (name == complete_ctor_identifier
4626 || name == base_ctor_identifier
4627 || name == complete_dtor_identifier
4628 || name == base_dtor_identifier
4629 || name == deleting_dtor_identifier
4630 || name == ansi_assopname (NOP_EXPR),
4632 my_friendly_assert (binfo != NULL_TREE, 20020712);
4634 class_type = BINFO_TYPE (binfo);
4636 /* Handle the special case where INSTANCE is NULL_TREE. */
4637 if (name == complete_ctor_identifier && !instance)
4639 instance = build_int_2 (0, 0);
4640 TREE_TYPE (instance) = build_pointer_type (class_type);
4641 instance = build1 (INDIRECT_REF, class_type, instance);
4643 else if (name == complete_dtor_identifier
4644 || name == base_dtor_identifier
4645 || name == deleting_dtor_identifier)
4646 my_friendly_assert (args == NULL_TREE, 20020712);
4648 my_friendly_assert (instance != NULL_TREE, 20020712);
4650 /* Resolve the name. */
4651 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4652 return error_mark_node;
4654 fns = lookup_fnfields (binfo, name, 1);
4656 /* When making a call to a constructor or destructor for a subobject
4657 that uses virtual base classes, pass down a pointer to a VTT for
4659 if ((name == base_ctor_identifier
4660 || name == base_dtor_identifier)
4661 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4666 /* If the current function is a complete object constructor
4667 or destructor, then we fetch the VTT directly.
4668 Otherwise, we look it up using the VTT we were given. */
4669 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4670 vtt = decay_conversion (vtt);
4671 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4672 build (EQ_EXPR, boolean_type_node,
4673 current_in_charge_parm, integer_zero_node),
4676 if (TREE_VIA_VIRTUAL (binfo))
4677 binfo = binfo_for_vbase (class_type, current_class_type);
4678 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4679 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4680 BINFO_SUBVTT_INDEX (binfo));
4682 args = tree_cons (NULL_TREE, sub_vtt, args);
4685 return build_new_method_call (instance, fns, args, binfo, flags);
4688 /* Build a call to "INSTANCE.FN (ARGS)". */
4691 build_new_method_call (tree instance, tree fns, tree args,
4692 tree conversion_path, int flags)
4694 struct z_candidate *candidates = 0, *cand;
4695 tree explicit_targs = NULL_TREE;
4696 tree basetype = NULL_TREE;
4699 tree mem_args = NULL_TREE, instance_ptr;
4700 tree name, pretty_name;
4702 tree templates = NULL_TREE;
4704 int template_only = 0;
4706 my_friendly_assert (instance != NULL_TREE, 20020729);
4708 if (instance == error_mark_node || fns == error_mark_node
4709 || args == error_mark_node)
4710 return error_mark_node;
4712 /* Process the argument list. */
4714 args = resolve_args (args);
4715 if (args == error_mark_node)
4716 return error_mark_node;
4718 if (TREE_CODE (instance) == OFFSET_REF)
4719 instance = resolve_offset_ref (instance);
4720 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4721 instance = convert_from_reference (instance);
4722 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4723 instance_ptr = build_this (instance);
4725 if (!BASELINK_P (fns))
4727 call = build_field_call (instance_ptr, fns, args);
4730 error ("call to non-function `%D'", fns);
4731 return error_mark_node;
4734 if (!conversion_path)
4735 conversion_path = BASELINK_BINFO (fns);
4736 access_binfo = BASELINK_ACCESS_BINFO (fns);
4737 optype = BASELINK_OPTYPE (fns);
4738 fns = BASELINK_FUNCTIONS (fns);
4740 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4742 explicit_targs = TREE_OPERAND (fns, 1);
4743 fns = TREE_OPERAND (fns, 0);
4747 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4748 || TREE_CODE (fns) == TEMPLATE_DECL
4749 || TREE_CODE (fns) == OVERLOAD,
4752 /* XXX this should be handled before we get here. */
4753 if (! IS_AGGR_TYPE (basetype))
4755 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4756 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4757 fns, instance, basetype);
4759 return error_mark_node;
4762 name = DECL_NAME (get_first_fn (fns));
4764 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4766 /* Callers should explicitly indicate whether they want to construct
4767 the complete object or just the part without virtual bases. */
4768 my_friendly_assert (name != ctor_identifier, 20000408);
4769 /* Similarly for destructors. */
4770 my_friendly_assert (name != dtor_identifier, 20000408);
4772 if (name == complete_ctor_identifier
4773 || name == base_ctor_identifier)
4774 pretty_name = constructor_name (basetype);
4776 pretty_name = dtor_identifier;
4784 tree class_type = (conversion_path
4785 ? BINFO_TYPE (conversion_path)
4788 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4789 for (fn = fns; fn; fn = OVL_NEXT (fn))
4791 tree t = OVL_CURRENT (fn);
4794 /* We can end up here for copy-init of same or base class. */
4795 if ((flags & LOOKUP_ONLYCONVERTING)
4796 && DECL_NONCONVERTING_P (t))
4799 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4800 this_arglist = mem_args;
4802 this_arglist = args;
4804 if (TREE_CODE (t) == TEMPLATE_DECL)
4806 /* A member template. */
4807 templates = tree_cons (NULL_TREE, t, templates);
4809 add_template_candidate (candidates, t,
4812 this_arglist, optype,
4818 else if (! template_only)
4819 candidates = add_function_candidate (candidates, t,
4828 if (! any_viable (candidates))
4830 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4831 if (flags & LOOKUP_SPECULATIVELY)
4833 if (!COMPLETE_TYPE_P (basetype))
4834 cxx_incomplete_type_error (instance_ptr, basetype);
4836 error ("no matching function for call to `%T::%D(%A)%#V'",
4837 basetype, pretty_name, user_args,
4838 TREE_TYPE (TREE_TYPE (instance_ptr)));
4839 print_z_candidates (candidates);
4840 return error_mark_node;
4842 candidates = splice_viable (candidates);
4843 cand = tourney (candidates);
4847 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4849 print_z_candidates (candidates);
4850 return error_mark_node;
4853 if (DECL_PURE_VIRTUAL_P (cand->fn)
4854 && instance == current_class_ref
4855 && (DECL_CONSTRUCTOR_P (current_function_decl)
4856 || DECL_DESTRUCTOR_P (current_function_decl))
4857 && ! (flags & LOOKUP_NONVIRTUAL)
4858 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4859 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4860 "abstract virtual `%#D' called from constructor"
4861 : "abstract virtual `%#D' called from destructor"),
4863 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4864 && is_dummy_object (instance_ptr))
4866 error ("cannot call member function `%D' without object", cand->fn);
4867 return error_mark_node;
4870 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4871 && resolves_to_fixed_type_p (instance, 0))
4872 flags |= LOOKUP_NONVIRTUAL;
4874 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4875 call = build_over_call (cand, mem_args, flags);
4878 call = build_over_call (cand, args, flags);
4879 /* In an expression of the form `a->f()' where `f' turns out to
4880 be a static member function, `a' is none-the-less evaluated. */
4881 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
4882 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4888 /* Returns true iff standard conversion sequence ICS1 is a proper
4889 subsequence of ICS2. */
4892 is_subseq (tree ics1, tree ics2)
4894 /* We can assume that a conversion of the same code
4895 between the same types indicates a subsequence since we only get
4896 here if the types we are converting from are the same. */
4898 while (TREE_CODE (ics1) == RVALUE_CONV
4899 || TREE_CODE (ics1) == LVALUE_CONV)
4900 ics1 = TREE_OPERAND (ics1, 0);
4904 while (TREE_CODE (ics2) == RVALUE_CONV
4905 || TREE_CODE (ics2) == LVALUE_CONV)
4906 ics2 = TREE_OPERAND (ics2, 0);
4908 if (TREE_CODE (ics2) == USER_CONV
4909 || TREE_CODE (ics2) == AMBIG_CONV
4910 || TREE_CODE (ics2) == IDENTITY_CONV)
4911 /* At this point, ICS1 cannot be a proper subsequence of
4912 ICS2. We can get a USER_CONV when we are comparing the
4913 second standard conversion sequence of two user conversion
4917 ics2 = TREE_OPERAND (ics2, 0);
4919 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4920 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4921 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4922 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4927 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
4928 be any _TYPE nodes. */
4931 is_properly_derived_from (tree derived, tree base)
4933 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4934 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4937 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4938 considers every class derived from itself. */
4939 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4940 && DERIVED_FROM_P (base, derived));
4943 /* We build the ICS for an implicit object parameter as a pointer
4944 conversion sequence. However, such a sequence should be compared
4945 as if it were a reference conversion sequence. If ICS is the
4946 implicit conversion sequence for an implicit object parameter,
4947 modify it accordingly. */
4950 maybe_handle_implicit_object (tree *ics)
4952 if (ICS_THIS_FLAG (*ics))
4954 /* [over.match.funcs]
4956 For non-static member functions, the type of the
4957 implicit object parameter is "reference to cv X"
4958 where X is the class of which the function is a
4959 member and cv is the cv-qualification on the member
4960 function declaration. */
4962 tree reference_type;
4964 /* The `this' parameter is a pointer to a class type. Make the
4965 implict conversion talk about a reference to that same class
4967 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4968 reference_type = build_reference_type (reference_type);
4970 if (TREE_CODE (t) == QUAL_CONV)
4971 t = TREE_OPERAND (t, 0);
4972 if (TREE_CODE (t) == PTR_CONV)
4973 t = TREE_OPERAND (t, 0);
4974 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4975 t = direct_reference_binding (reference_type, t);
4980 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4981 and return the type to which the reference refers. Otherwise,
4982 leave *ICS unchanged and return NULL_TREE. */
4985 maybe_handle_ref_bind (tree *ics)
4987 if (TREE_CODE (*ics) == REF_BIND)
4989 tree old_ics = *ics;
4990 tree type = TREE_TYPE (TREE_TYPE (old_ics));
4991 *ics = TREE_OPERAND (old_ics, 0);
4992 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4993 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5000 /* Compare two implicit conversion sequences according to the rules set out in
5001 [over.ics.rank]. Return values:
5003 1: ics1 is better than ics2
5004 -1: ics2 is better than ics1
5005 0: ics1 and ics2 are indistinguishable */
5008 compare_ics (tree ics1, tree ics2)
5014 tree deref_from_type1 = NULL_TREE;
5015 tree deref_from_type2 = NULL_TREE;
5016 tree deref_to_type1 = NULL_TREE;
5017 tree deref_to_type2 = NULL_TREE;
5020 /* REF_BINDING is nonzero if the result of the conversion sequence
5021 is a reference type. In that case TARGET_TYPE is the
5022 type referred to by the reference. */
5026 /* Handle implicit object parameters. */
5027 maybe_handle_implicit_object (&ics1);
5028 maybe_handle_implicit_object (&ics2);
5030 /* Handle reference parameters. */
5031 target_type1 = maybe_handle_ref_bind (&ics1);
5032 target_type2 = maybe_handle_ref_bind (&ics2);
5036 When comparing the basic forms of implicit conversion sequences (as
5037 defined in _over.best.ics_)
5039 --a standard conversion sequence (_over.ics.scs_) is a better
5040 conversion sequence than a user-defined conversion sequence
5041 or an ellipsis conversion sequence, and
5043 --a user-defined conversion sequence (_over.ics.user_) is a
5044 better conversion sequence than an ellipsis conversion sequence
5045 (_over.ics.ellipsis_). */
5046 rank1 = ICS_RANK (ics1);
5047 rank2 = ICS_RANK (ics2);
5051 else if (rank1 < rank2)
5054 if (rank1 == BAD_RANK)
5056 /* XXX Isn't this an extension? */
5057 /* Both ICS are bad. We try to make a decision based on what
5058 would have happenned if they'd been good. */
5059 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5060 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5062 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5063 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5066 /* We couldn't make up our minds; try to figure it out below. */
5069 if (ICS_ELLIPSIS_FLAG (ics1))
5070 /* Both conversions are ellipsis conversions. */
5073 /* User-defined conversion sequence U1 is a better conversion sequence
5074 than another user-defined conversion sequence U2 if they contain the
5075 same user-defined conversion operator or constructor and if the sec-
5076 ond standard conversion sequence of U1 is better than the second
5077 standard conversion sequence of U2. */
5079 if (ICS_USER_FLAG (ics1))
5083 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5084 if (TREE_CODE (t1) == AMBIG_CONV)
5086 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5087 if (TREE_CODE (t2) == AMBIG_CONV)
5090 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5093 /* We can just fall through here, after setting up
5094 FROM_TYPE1 and FROM_TYPE2. */
5095 from_type1 = TREE_TYPE (t1);
5096 from_type2 = TREE_TYPE (t2);
5100 /* We're dealing with two standard conversion sequences.
5104 Standard conversion sequence S1 is a better conversion
5105 sequence than standard conversion sequence S2 if
5107 --S1 is a proper subsequence of S2 (comparing the conversion
5108 sequences in the canonical form defined by _over.ics.scs_,
5109 excluding any Lvalue Transformation; the identity
5110 conversion sequence is considered to be a subsequence of
5111 any non-identity conversion sequence */
5114 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5115 from_type1 = TREE_OPERAND (from_type1, 0);
5116 from_type1 = TREE_TYPE (from_type1);
5119 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5120 from_type2 = TREE_OPERAND (from_type2, 0);
5121 from_type2 = TREE_TYPE (from_type2);
5124 if (same_type_p (from_type1, from_type2))
5126 if (is_subseq (ics1, ics2))
5128 if (is_subseq (ics2, ics1))
5131 /* Otherwise, one sequence cannot be a subsequence of the other; they
5132 don't start with the same type. This can happen when comparing the
5133 second standard conversion sequence in two user-defined conversion
5140 --the rank of S1 is better than the rank of S2 (by the rules
5143 Standard conversion sequences are ordered by their ranks: an Exact
5144 Match is a better conversion than a Promotion, which is a better
5145 conversion than a Conversion.
5147 Two conversion sequences with the same rank are indistinguishable
5148 unless one of the following rules applies:
5150 --A conversion that is not a conversion of a pointer, or pointer
5151 to member, to bool is better than another conversion that is such
5154 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5155 so that we do not have to check it explicitly. */
5156 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5158 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5161 to_type1 = TREE_TYPE (ics1);
5162 to_type2 = TREE_TYPE (ics2);
5164 if (TYPE_PTR_P (from_type1)
5165 && TYPE_PTR_P (from_type2)
5166 && TYPE_PTR_P (to_type1)
5167 && TYPE_PTR_P (to_type2))
5169 deref_from_type1 = TREE_TYPE (from_type1);
5170 deref_from_type2 = TREE_TYPE (from_type2);
5171 deref_to_type1 = TREE_TYPE (to_type1);
5172 deref_to_type2 = TREE_TYPE (to_type2);
5174 /* The rules for pointers to members A::* are just like the rules
5175 for pointers A*, except opposite: if B is derived from A then
5176 A::* converts to B::*, not vice versa. For that reason, we
5177 switch the from_ and to_ variables here. */
5178 else if (TYPE_PTRMEM_P (from_type1)
5179 && TYPE_PTRMEM_P (from_type2)
5180 && TYPE_PTRMEM_P (to_type1)
5181 && TYPE_PTRMEM_P (to_type2))
5183 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5184 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5185 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5186 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5188 else if (TYPE_PTRMEMFUNC_P (from_type1)
5189 && TYPE_PTRMEMFUNC_P (from_type2)
5190 && TYPE_PTRMEMFUNC_P (to_type1)
5191 && TYPE_PTRMEMFUNC_P (to_type2))
5193 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5194 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5195 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5196 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5199 if (deref_from_type1 != NULL_TREE
5200 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5201 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5203 /* This was one of the pointer or pointer-like conversions.
5207 --If class B is derived directly or indirectly from class A,
5208 conversion of B* to A* is better than conversion of B* to
5209 void*, and conversion of A* to void* is better than
5210 conversion of B* to void*. */
5211 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5212 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5214 if (is_properly_derived_from (deref_from_type1,
5217 else if (is_properly_derived_from (deref_from_type2,
5221 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5222 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5224 if (same_type_p (deref_from_type1, deref_from_type2))
5226 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5228 if (is_properly_derived_from (deref_from_type1,
5232 /* We know that DEREF_TO_TYPE1 is `void' here. */
5233 else if (is_properly_derived_from (deref_from_type1,
5238 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5239 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5243 --If class B is derived directly or indirectly from class A
5244 and class C is derived directly or indirectly from B,
5246 --conversion of C* to B* is better than conversion of C* to
5249 --conversion of B* to A* is better than conversion of C* to
5251 if (same_type_p (deref_from_type1, deref_from_type2))
5253 if (is_properly_derived_from (deref_to_type1,
5256 else if (is_properly_derived_from (deref_to_type2,
5260 else if (same_type_p (deref_to_type1, deref_to_type2))
5262 if (is_properly_derived_from (deref_from_type2,
5265 else if (is_properly_derived_from (deref_from_type1,
5271 else if (CLASS_TYPE_P (non_reference (from_type1))
5272 && same_type_p (from_type1, from_type2))
5274 tree from = non_reference (from_type1);
5278 --binding of an expression of type C to a reference of type
5279 B& is better than binding an expression of type C to a
5280 reference of type A&
5282 --conversion of C to B is better than conversion of C to A, */
5283 if (is_properly_derived_from (from, to_type1)
5284 && is_properly_derived_from (from, to_type2))
5286 if (is_properly_derived_from (to_type1, to_type2))
5288 else if (is_properly_derived_from (to_type2, to_type1))
5292 else if (CLASS_TYPE_P (non_reference (to_type1))
5293 && same_type_p (to_type1, to_type2))
5295 tree to = non_reference (to_type1);
5299 --binding of an expression of type B to a reference of type
5300 A& is better than binding an expression of type C to a
5301 reference of type A&,
5303 --onversion of B to A is better than conversion of C to A */
5304 if (is_properly_derived_from (from_type1, to)
5305 && is_properly_derived_from (from_type2, to))
5307 if (is_properly_derived_from (from_type2, from_type1))
5309 else if (is_properly_derived_from (from_type1, from_type2))
5316 --S1 and S2 differ only in their qualification conversion and yield
5317 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5318 qualification signature of type T1 is a proper subset of the cv-
5319 qualification signature of type T2 */
5320 if (TREE_CODE (ics1) == QUAL_CONV
5321 && TREE_CODE (ics2) == QUAL_CONV
5322 && same_type_p (from_type1, from_type2))
5323 return comp_cv_qual_signature (to_type1, to_type2);
5327 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5328 types to which the references refer are the same type except for
5329 top-level cv-qualifiers, and the type to which the reference
5330 initialized by S2 refers is more cv-qualified than the type to
5331 which the reference initialized by S1 refers */
5333 if (target_type1 && target_type2
5334 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5335 return comp_cv_qualification (target_type2, target_type1);
5337 /* Neither conversion sequence is better than the other. */
5341 /* The source type for this standard conversion sequence. */
5344 source_type (tree t)
5346 for (;; t = TREE_OPERAND (t, 0))
5348 if (TREE_CODE (t) == USER_CONV
5349 || TREE_CODE (t) == AMBIG_CONV
5350 || TREE_CODE (t) == IDENTITY_CONV)
5351 return TREE_TYPE (t);
5356 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5357 a pointer to LOSER and re-running joust to produce the warning if WINNER
5358 is actually used. */
5361 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5363 winner->warnings = tree_cons (NULL_TREE,
5364 build_zc_wrapper (loser),
5368 /* Returns true iff functions are equivalent. Equivalent functions are
5369 not '==' only if one is a function-local extern function or if
5370 both are extern "C". */
5373 equal_functions (tree fn1, tree fn2)
5375 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5376 || DECL_EXTERN_C_FUNCTION_P (fn1))
5377 return decls_match (fn1, fn2);
5381 /* Compare two candidates for overloading as described in
5382 [over.match.best]. Return values:
5384 1: cand1 is better than cand2
5385 -1: cand2 is better than cand1
5386 0: cand1 and cand2 are indistinguishable */
5389 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5392 int i, off1 = 0, off2 = 0, len;
5394 /* Candidates that involve bad conversions are always worse than those
5396 if (cand1->viable > cand2->viable)
5398 if (cand1->viable < cand2->viable)
5401 /* If we have two pseudo-candidates for conversions to the same type,
5402 or two candidates for the same function, arbitrarily pick one. */
5403 if (cand1->fn == cand2->fn
5404 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5407 /* a viable function F1
5408 is defined to be a better function than another viable function F2 if
5409 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5410 ICSi(F2), and then */
5412 /* for some argument j, ICSj(F1) is a better conversion sequence than
5415 /* For comparing static and non-static member functions, we ignore
5416 the implicit object parameter of the non-static function. The
5417 standard says to pretend that the static function has an object
5418 parm, but that won't work with operator overloading. */
5419 len = TREE_VEC_LENGTH (cand1->convs);
5420 if (len != TREE_VEC_LENGTH (cand2->convs))
5422 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5423 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5425 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5426 && DECL_STATIC_FUNCTION_P (cand2->fn))
5435 for (i = 0; i < len; ++i)
5437 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5438 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5439 int comp = compare_ics (t1, t2);
5444 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5445 && TREE_CODE (t1) == STD_CONV
5446 && TREE_CODE (t2) == STD_CONV
5447 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5448 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5449 && (TYPE_PRECISION (TREE_TYPE (t1))
5450 == TYPE_PRECISION (TREE_TYPE (t2)))
5451 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5452 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5455 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5457 struct z_candidate *w, *l;
5459 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5460 w = cand1, l = cand2;
5462 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5463 w = cand2, l = cand1;
5467 warning ("passing `%T' chooses `%T' over `%T'",
5468 type, type1, type2);
5469 warning (" in call to `%D'", w->fn);
5475 if (winner && comp != winner)
5484 /* warn about confusing overload resolution for user-defined conversions,
5485 either between a constructor and a conversion op, or between two
5487 if (winner && cand1->second_conv
5488 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5489 != DECL_CONSTRUCTOR_P (cand2->fn))
5490 /* Don't warn if the two conv ops convert to the same type... */
5491 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5492 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5493 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5495 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5498 struct z_candidate *w, *l;
5501 w = cand1, l = cand2;
5503 w = cand2, l = cand1;
5504 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5505 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5506 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5507 && (convn = standard_conversion
5508 (TREE_TYPE (TREE_TYPE (l->fn)),
5509 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5510 && TREE_CODE (convn) == QUAL_CONV)
5511 /* Don't complain about `operator char *()' beating
5512 `operator const char *() const'. */;
5515 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5516 if (! DECL_CONSTRUCTOR_P (w->fn))
5517 source = TREE_TYPE (source);
5518 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5519 warning (" for conversion from `%T' to `%T'",
5520 source, TREE_TYPE (w->second_conv));
5521 warning (" because conversion sequence for the argument is better");
5532 F1 is a non-template function and F2 is a template function
5535 if (! cand1->template && cand2->template)
5537 else if (cand1->template && ! cand2->template)
5541 F1 and F2 are template functions and the function template for F1 is
5542 more specialized than the template for F2 according to the partial
5545 if (cand1->template && cand2->template)
5547 winner = more_specialized
5548 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5550 /* Tell the deduction code how many real function arguments
5551 we saw, not counting the implicit 'this' argument. But,
5552 add_function_candidate() suppresses the "this" argument
5555 [temp.func.order]: The presence of unused ellipsis and default
5556 arguments has no effect on the partial ordering of function
5558 TREE_VEC_LENGTH (cand1->convs)
5559 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5560 - DECL_CONSTRUCTOR_P (cand1->fn)));
5567 the context is an initialization by user-defined conversion (see
5568 _dcl.init_ and _over.match.user_) and the standard conversion
5569 sequence from the return type of F1 to the destination type (i.e.,
5570 the type of the entity being initialized) is a better conversion
5571 sequence than the standard conversion sequence from the return type
5572 of F2 to the destination type. */
5574 if (cand1->second_conv)
5576 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5581 /* Check whether we can discard a builtin candidate, either because we
5582 have two identical ones or matching builtin and non-builtin candidates.
5584 (Pedantically in the latter case the builtin which matched the user
5585 function should not be added to the overload set, but we spot it here.
5588 ... the builtin candidates include ...
5589 - do not have the same parameter type list as any non-template
5590 non-member candidate. */
5592 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5593 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5595 for (i = 0; i < len; ++i)
5596 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5597 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5599 if (i == TREE_VEC_LENGTH (cand1->convs))
5601 if (cand1->fn == cand2->fn)
5602 /* Two built-in candidates; arbitrarily pick one. */
5604 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5605 /* cand1 is built-in; prefer cand2. */
5608 /* cand2 is built-in; prefer cand1. */
5613 /* If the two functions are the same (this can happen with declarations
5614 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5615 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5616 && equal_functions (cand1->fn, cand2->fn))
5621 /* Extension: If the worst conversion for one candidate is worse than the
5622 worst conversion for the other, take the first. */
5625 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5626 struct z_candidate *w = 0, *l = 0;
5628 for (i = 0; i < len; ++i)
5630 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5631 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5632 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5633 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5636 winner = 1, w = cand1, l = cand2;
5638 winner = -1, w = cand2, l = cand1;
5643 pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5645 " because worst conversion for the former is better than worst conversion for the latter");
5653 my_friendly_assert (!winner, 20010121);
5657 /* Given a list of candidates for overloading, find the best one, if any.
5658 This algorithm has a worst case of O(2n) (winner is last), and a best
5659 case of O(n/2) (totally ambiguous); much better than a sorting
5662 static struct z_candidate *
5663 tourney (struct z_candidate *candidates)
5665 struct z_candidate *champ = candidates, *challenger;
5667 int champ_compared_to_predecessor = 0;
5669 /* Walk through the list once, comparing each current champ to the next
5670 candidate, knocking out a candidate or two with each comparison. */
5672 for (challenger = champ->next; challenger; )
5674 fate = joust (champ, challenger, 0);
5676 challenger = challenger->next;
5681 champ = challenger->next;
5684 champ_compared_to_predecessor = 0;
5689 champ_compared_to_predecessor = 1;
5692 challenger = champ->next;
5696 /* Make sure the champ is better than all the candidates it hasn't yet
5697 been compared to. */
5699 for (challenger = candidates;
5701 && !(champ_compared_to_predecessor && challenger->next == champ);
5702 challenger = challenger->next)
5704 fate = joust (champ, challenger, 0);
5712 /* Returns nonzero if things of type FROM can be converted to TO. */
5715 can_convert (tree to, tree from)
5717 return can_convert_arg (to, from, NULL_TREE);
5720 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5723 can_convert_arg (tree to, tree from, tree arg)
5725 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5726 return (t && ! ICS_BAD_FLAG (t));
5729 /* Like can_convert_arg, but allows dubious conversions as well. */
5732 can_convert_arg_bad (tree to, tree from, tree arg)
5734 return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
5737 /* Convert EXPR to TYPE. Return the converted expression.
5739 Note that we allow bad conversions here because by the time we get to
5740 this point we are committed to doing the conversion. If we end up
5741 doing a bad conversion, convert_like will complain. */
5744 perform_implicit_conversion (tree type, tree expr)
5748 if (expr == error_mark_node)
5749 return error_mark_node;
5750 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5754 error ("could not convert `%E' to `%T'", expr, type);
5755 return error_mark_node;
5758 return convert_like (conv, expr);
5761 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5762 initializing a variable of that TYPE. Return the converted
5766 initialize_reference (tree type, tree expr)
5770 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5771 if (!conv || ICS_BAD_FLAG (conv))
5773 error ("could not convert `%E' to `%T'", expr, type);
5774 return error_mark_node;
5777 return convert_like (conv, expr);
5780 #include "gt-cp-call.h"