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);
3351 case VEC_DELETE_EXPR:
3353 /* Use build_op_new_call and build_op_delete_call instead. */
3357 return build_object_call (arg1, arg2);
3365 if (TREE_CODE (arg2) == OFFSET_REF)
3366 arg2 = resolve_offset_ref (arg2);
3367 arg2 = convert_from_reference (arg2);
3371 if (TREE_CODE (arg3) == OFFSET_REF)
3372 arg3 = resolve_offset_ref (arg3);
3373 arg3 = convert_from_reference (arg3);
3376 if (code == COND_EXPR)
3378 if (arg2 == NULL_TREE
3379 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3380 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3381 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3382 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3385 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3386 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3389 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3390 arg2 = integer_zero_node;
3392 arglist = NULL_TREE;
3394 arglist = tree_cons (NULL_TREE, arg3, arglist);
3396 arglist = tree_cons (NULL_TREE, arg2, arglist);
3397 arglist = tree_cons (NULL_TREE, arg1, arglist);
3399 fns = lookup_function_nonclass (fnname, arglist);
3401 if (fns && TREE_CODE (fns) == TREE_LIST)
3402 fns = TREE_VALUE (fns);
3403 for (; fns; fns = OVL_NEXT (fns))
3405 tree fn = OVL_CURRENT (fns);
3406 if (TREE_CODE (fn) == TEMPLATE_DECL)
3408 templates = tree_cons (NULL_TREE, fn, templates);
3410 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3411 arglist, TREE_TYPE (fnname),
3412 /*access_path=*/NULL_TREE,
3413 /*conversion_path=*/NULL_TREE,
3414 flags, DEDUCE_CALL);
3417 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3419 /*access_path=*/NULL_TREE,
3420 /*conversion_path=*/NULL_TREE,
3424 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3426 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3427 if (fns == error_mark_node)
3435 tree conversion_path = BASELINK_BINFO (fns);
3437 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3438 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
3440 tree fn = OVL_CURRENT (fns);
3442 tree access_path = TYPE_BINFO (TREE_TYPE (arg1));
3444 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3445 this_arglist = mem_arglist;
3447 this_arglist = arglist;
3449 if (TREE_CODE (fn) == TEMPLATE_DECL)
3451 /* A member template. */
3452 templates = tree_cons (NULL_TREE, fn, templates);
3454 = add_template_candidate (candidates, fn,
3455 BINFO_TYPE (conversion_path),
3457 this_arglist, TREE_TYPE (fnname),
3458 access_path, conversion_path,
3459 flags, DEDUCE_CALL);
3462 candidates = add_function_candidate
3463 (candidates, fn, BINFO_TYPE (conversion_path), this_arglist,
3464 access_path, conversion_path, flags);
3471 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3472 to know about two args; a builtin candidate will always have a first
3473 parameter of type bool. We'll handle that in
3474 build_builtin_candidate. */
3475 if (code == COND_EXPR)
3485 args[2] = NULL_TREE;
3488 candidates = add_builtin_candidates
3489 (candidates, code, code2, fnname, args, flags);
3496 /* For these, the built-in candidates set is empty
3497 [over.match.oper]/3. We don't want non-strict matches
3498 because exact matches are always possible with built-in
3499 operators. The built-in candidate set for COMPONENT_REF
3500 would be empty too, but since there are no such built-in
3501 operators, we accept non-strict matches for them. */
3502 viable_candidates = any_strictly_viable (candidates);
3506 viable_candidates = any_viable (candidates);
3510 if (! viable_candidates)
3514 case POSTINCREMENT_EXPR:
3515 case POSTDECREMENT_EXPR:
3516 /* Look for an `operator++ (int)'. If they didn't have
3517 one, then we fall back to the old way of doing things. */
3518 if (flags & LOOKUP_COMPLAIN)
3519 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3521 operator_name_info[code].name);
3522 if (code == POSTINCREMENT_EXPR)
3523 code = PREINCREMENT_EXPR;
3525 code = PREDECREMENT_EXPR;
3526 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3528 /* The caller will deal with these. */
3537 if (flags & LOOKUP_COMPLAIN)
3539 op_error (code, code2, arg1, arg2, arg3, "no match");
3540 print_z_candidates (candidates);
3542 return error_mark_node;
3544 candidates = splice_viable (candidates);
3545 cand = tourney (candidates);
3549 if (flags & LOOKUP_COMPLAIN)
3551 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3552 print_z_candidates (candidates);
3554 return error_mark_node;
3557 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3559 extern int warn_synth;
3561 && fnname == ansi_assopname (NOP_EXPR)
3562 && DECL_ARTIFICIAL (cand->fn)
3564 && ! candidates->next->next)
3566 warning ("using synthesized `%#D' for copy assignment",
3568 cp_warning_at (" where cfront would use `%#D'",
3570 ? candidates->next->fn
3574 return build_over_call
3576 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3577 ? mem_arglist : arglist,
3581 /* Check for comparison of different enum types. */
3590 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3591 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3592 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3593 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3595 warning ("comparison between `%#T' and `%#T'",
3596 TREE_TYPE (arg1), TREE_TYPE (arg2));
3603 /* We need to strip any leading REF_BIND so that bitfields don't cause
3604 errors. This should not remove any important conversions, because
3605 builtins don't apply to class objects directly. */
3606 conv = TREE_VEC_ELT (cand->convs, 0);
3607 if (TREE_CODE (conv) == REF_BIND)
3608 conv = TREE_OPERAND (conv, 0);
3609 arg1 = convert_like (conv, arg1);
3612 conv = TREE_VEC_ELT (cand->convs, 1);
3613 if (TREE_CODE (conv) == REF_BIND)
3614 conv = TREE_OPERAND (conv, 0);
3615 arg2 = convert_like (conv, arg2);
3619 conv = TREE_VEC_ELT (cand->convs, 2);
3620 if (TREE_CODE (conv) == REF_BIND)
3621 conv = TREE_OPERAND (conv, 0);
3622 arg3 = convert_like (conv, arg3);
3629 return build_modify_expr (arg1, code2, arg2);
3632 return build_indirect_ref (arg1, "unary *");
3637 case TRUNC_DIV_EXPR:
3648 case TRUNC_MOD_EXPR:
3652 case TRUTH_ANDIF_EXPR:
3653 case TRUTH_ORIF_EXPR:
3654 return cp_build_binary_op (code, arg1, arg2);
3659 case TRUTH_NOT_EXPR:
3660 case PREINCREMENT_EXPR:
3661 case POSTINCREMENT_EXPR:
3662 case PREDECREMENT_EXPR:
3663 case POSTDECREMENT_EXPR:
3666 return build_unary_op (code, arg1, candidates != 0);
3669 return build_array_ref (arg1, arg2);
3672 return build_conditional_expr (arg1, arg2, arg3);
3675 return build_m_component_ref
3676 (build_indirect_ref (arg1, NULL), arg2);
3678 /* The caller will deal with these. */
3690 /* Build a call to operator delete. This has to be handled very specially,
3691 because the restrictions on what signatures match are different from all
3692 other call instances. For a normal delete, only a delete taking (void *)
3693 or (void *, size_t) is accepted. For a placement delete, only an exact
3694 match with the placement new is accepted.
3696 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3697 ADDR is the pointer to be deleted.
3698 SIZE is the size of the memory block to be deleted.
3699 FLAGS are the usual overloading flags.
3700 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3703 build_op_delete_call (enum tree_code code, tree addr, tree size,
3704 int flags, tree placement)
3706 tree fn = NULL_TREE;
3707 tree fns, fnname, fntype, argtypes, args, type;
3710 if (addr == error_mark_node)
3711 return error_mark_node;
3713 type = TREE_TYPE (TREE_TYPE (addr));
3714 while (TREE_CODE (type) == ARRAY_TYPE)
3715 type = TREE_TYPE (type);
3717 fnname = ansi_opname (code);
3719 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3722 If the result of the lookup is ambiguous or inaccessible, or if
3723 the lookup selects a placement deallocation function, the
3724 program is ill-formed.
3726 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3728 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3729 if (fns == error_mark_node)
3730 return error_mark_node;
3735 if (fns == NULL_TREE)
3736 fns = lookup_name_nonclass (fnname);
3743 /* Find the allocation function that is being called. */
3744 call_expr = placement;
3745 /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3747 while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3748 call_expr = TREE_OPERAND (call_expr, 1);
3749 /* Extract the function. */
3750 alloc_fn = get_callee_fndecl (call_expr);
3751 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3752 /* Then the second parm type. */
3753 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3754 /* Also the second argument. */
3755 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3759 /* First try it without the size argument. */
3760 argtypes = void_list_node;
3764 /* Strip const and volatile from addr. */
3765 addr = cp_convert (ptr_type_node, addr);
3767 /* We make two tries at finding a matching `operator delete'. On
3768 the first pass, we look for an one-operator (or placement)
3769 operator delete. If we're not doing placement delete, then on
3770 the second pass we look for a two-argument delete. */
3771 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3774 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3776 /* Normal delete; now try to find a match including the size
3778 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3779 tree_cons (NULL_TREE, sizetype,
3781 fntype = build_function_type (void_type_node, argtypes);
3783 /* Go through the `operator delete' functions looking for one
3784 with a matching type. */
3785 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3791 /* Exception specifications on the `delete' operator do not
3793 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3795 /* We also don't compare attributes. We're really just
3796 trying to check the types of the first two parameters. */
3797 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3801 /* If we found a match, we're done. */
3806 /* If we have a matching function, call it. */
3809 /* Make sure we have the actual function, and not an
3811 fn = OVL_CURRENT (fn);
3813 /* If the FN is a member function, make sure that it is
3815 if (DECL_CLASS_SCOPE_P (fn))
3816 enforce_access (type, fn);
3819 args = tree_cons (NULL_TREE, addr, args);
3821 args = tree_cons (NULL_TREE, addr,
3822 build_tree_list (NULL_TREE, size));
3824 return build_function_call (fn, args);
3827 /* If we are doing placement delete we do nothing if we don't find a
3828 matching op delete. */
3832 error ("no suitable `operator delete' for `%T'", type);
3833 return error_mark_node;
3836 /* If the current scope isn't allowed to access DECL along
3837 BASETYPE_PATH, give an error. The most derived class in
3838 BASETYPE_PATH is the one used to qualify DECL. */
3841 enforce_access (tree basetype_path, tree decl)
3843 if (!accessible_p (basetype_path, decl))
3845 if (TREE_PRIVATE (decl))
3846 cp_error_at ("`%+#D' is private", decl);
3847 else if (TREE_PROTECTED (decl))
3848 cp_error_at ("`%+#D' is protected", decl);
3850 cp_error_at ("`%+#D' is inaccessible", decl);
3851 error ("within this context");
3858 /* Perform the conversions in CONVS on the expression EXPR.
3859 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3860 indicates the `this' argument of a method. INNER is nonzero when
3861 being called to continue a conversion chain. It is negative when a
3862 reference binding will be applied, positive otherwise. */
3865 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner)
3869 tree totype = TREE_TYPE (convs);
3871 if (ICS_BAD_FLAG (convs)
3872 && TREE_CODE (convs) != USER_CONV
3873 && TREE_CODE (convs) != AMBIG_CONV
3874 && TREE_CODE (convs) != REF_BIND)
3877 for (; t; t = TREE_OPERAND (t, 0))
3879 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3881 expr = convert_like_real (t, expr, fn, argnum, 1);
3884 else if (TREE_CODE (t) == AMBIG_CONV)
3885 return convert_like_real (t, expr, fn, argnum, 1);
3886 else if (TREE_CODE (t) == IDENTITY_CONV)
3889 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3891 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3892 return cp_convert (totype, expr);
3896 expr = dubious_conversion_warnings
3897 (totype, expr, "argument", fn, argnum);
3898 switch (TREE_CODE (convs))
3902 struct z_candidate *cand
3903 = WRAPPER_ZC (TREE_OPERAND (convs, 1));
3904 tree convfn = cand->fn;
3907 if (DECL_CONSTRUCTOR_P (convfn))
3909 tree t = build_int_2 (0, 0);
3910 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3912 args = build_tree_list (NULL_TREE, expr);
3913 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3914 || DECL_HAS_VTT_PARM_P (convfn))
3915 /* We should never try to call the abstract or base constructor
3918 args = tree_cons (NULL_TREE, t, args);
3921 args = build_this (expr);
3922 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3924 /* If this is a constructor or a function returning an aggr type,
3925 we need to build up a TARGET_EXPR. */
3926 if (DECL_CONSTRUCTOR_P (convfn))
3927 expr = build_cplus_new (totype, expr);
3929 /* The result of the call is then used to direct-initialize the object
3930 that is the destination of the copy-initialization. [dcl.init]
3932 Note that this step is not reflected in the conversion sequence;
3933 it affects the semantics when we actually perform the
3934 conversion, but is not considered during overload resolution.
3936 If the target is a class, that means call a ctor. */
3937 if (IS_AGGR_TYPE (totype)
3938 && (inner >= 0 || !lvalue_p (expr)))
3940 savew = warningcount, savee = errorcount;
3941 expr = build_special_member_call
3942 (NULL_TREE, complete_ctor_identifier,
3943 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3944 /* Core issue 84, now a DR, says that we don't allow UDCs
3945 for these args (which deliberately breaks copy-init of an
3946 auto_ptr<Base> from an auto_ptr<Derived>). */
3947 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3949 /* Tell the user where this failing constructor call came from. */
3952 if (warningcount > savew)
3954 (" initializing argument %P of `%D' from result of `%D'",
3955 argnum, fn, convfn);
3956 else if (errorcount > savee)
3958 (" initializing argument %P of `%D' from result of `%D'",
3959 argnum, fn, convfn);
3963 if (warningcount > savew)
3964 warning (" initializing temporary from result of `%D'",
3966 else if (errorcount > savee)
3967 error (" initializing temporary from result of `%D'",
3970 expr = build_cplus_new (totype, expr);
3975 if (type_unknown_p (expr))
3976 expr = instantiate_type (totype, expr, tf_error | tf_warning);
3979 /* Call build_user_type_conversion again for the error. */
3980 return build_user_type_conversion
3981 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3987 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3988 TREE_CODE (convs) == REF_BIND ? -1 : 1);
3989 if (expr == error_mark_node)
3990 return error_mark_node;
3992 /* Convert a non-array constant variable to its underlying value, unless we
3993 are about to bind it to a reference, in which case we need to
3994 leave it as an lvalue. */
3995 if (TREE_CODE (convs) != REF_BIND
3996 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3997 expr = decl_constant_value (expr);
3999 switch (TREE_CODE (convs))
4002 if (! IS_AGGR_TYPE (totype))
4004 /* else fall through */
4006 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4008 /* We are going to bind a reference directly to a base-class
4009 subobject of EXPR. */
4010 tree base_ptr = build_pointer_type (totype);
4012 /* Build an expression for `*((base*) &expr)'. */
4013 expr = build_unary_op (ADDR_EXPR, expr, 0);
4014 expr = perform_implicit_conversion (base_ptr, expr);
4015 expr = build_indirect_ref (expr, "implicit conversion");
4019 /* Copy-initialization where the cv-unqualified version of the source
4020 type is the same class as, or a derived class of, the class of the
4021 destination [is treated as direct-initialization]. [dcl.init] */
4022 savew = warningcount, savee = errorcount;
4023 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4024 build_tree_list (NULL_TREE, expr),
4025 TYPE_BINFO (totype),
4026 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
4029 if (warningcount > savew)
4030 warning (" initializing argument %P of `%D'", argnum, fn);
4031 else if (errorcount > savee)
4032 error (" initializing argument %P of `%D'", argnum, fn);
4034 return build_cplus_new (totype, expr);
4038 tree ref_type = totype;
4040 /* If necessary, create a temporary. */
4041 if (NEED_TEMPORARY_P (convs) || !non_cast_lvalue_p (expr))
4043 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4044 expr = build_target_expr_with_type (expr, type);
4047 /* Take the address of the thing to which we will bind the
4049 expr = build_unary_op (ADDR_EXPR, expr, 1);
4050 if (expr == error_mark_node)
4051 return error_mark_node;
4053 /* Convert it to a pointer to the type referred to by the
4054 reference. This will adjust the pointer if a derived to
4055 base conversion is being performed. */
4056 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4058 /* Convert the pointer to the desired reference type. */
4059 expr = build1 (NOP_EXPR, ref_type, expr);
4065 return decay_conversion (expr);
4068 /* Warn about deprecated conversion if appropriate. */
4069 string_conv_p (totype, expr, 1);
4075 return ocp_convert (totype, expr, CONV_IMPLICIT,
4076 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4079 /* Build a call to __builtin_trap which can be used in an expression. */
4082 call_builtin_trap (void)
4084 tree fn = get_identifier ("__builtin_trap");
4085 if (IDENTIFIER_GLOBAL_VALUE (fn))
4086 fn = IDENTIFIER_GLOBAL_VALUE (fn);
4090 fn = build_call (fn, NULL_TREE);
4091 fn = build (COMPOUND_EXPR, integer_type_node, fn, integer_zero_node);
4095 /* ARG is being passed to a varargs function. Perform any conversions
4096 required. Array/function to pointer decay must have already happened.
4097 Return the converted value. */
4100 convert_arg_to_ellipsis (tree arg)
4102 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4103 && (TYPE_PRECISION (TREE_TYPE (arg))
4104 < TYPE_PRECISION (double_type_node)))
4105 /* Convert `float' to `double'. */
4106 arg = cp_convert (double_type_node, arg);
4108 /* Convert `short' and `char' to full-size `int'. */
4109 arg = default_conversion (arg);
4111 arg = require_complete_type (arg);
4113 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4115 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4116 here and do a bitwise copy, but now cp_expr_size will abort if we
4118 warning ("cannot pass objects of non-POD type `%#T' through `...'; \
4119 call will abort at runtime",
4121 arg = call_builtin_trap ();
4127 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4130 build_x_va_arg (tree expr, tree type)
4132 if (processing_template_decl)
4133 return build_min (VA_ARG_EXPR, type, expr);
4135 type = complete_type_or_else (type, NULL_TREE);
4137 if (expr == error_mark_node || !type)
4138 return error_mark_node;
4140 if (! pod_type_p (type))
4142 /* Undefined behavior [expr.call] 5.2.2/7. */
4143 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4147 return build_va_arg (expr, type);
4150 /* TYPE has been given to va_arg. Apply the default conversions which
4151 would have happened when passed via ellipsis. Return the promoted
4152 type, or the passed type if there is no change. */
4155 cxx_type_promotes_to (tree type)
4159 if (TREE_CODE (type) == ARRAY_TYPE)
4160 return build_pointer_type (TREE_TYPE (type));
4162 if (TREE_CODE (type) == FUNCTION_TYPE)
4163 return build_pointer_type (type);
4165 promote = type_promotes_to (type);
4166 if (same_type_p (type, promote))
4172 /* ARG is a default argument expression being passed to a parameter of
4173 the indicated TYPE, which is a parameter to FN. Do any required
4174 conversions. Return the converted value. */
4177 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4179 if (TREE_CODE (arg) == DEFAULT_ARG)
4181 /* When processing the default args for a class, we can find that
4182 there is an ordering constraint, and we call a function who's
4183 default args have not yet been converted. For instance,
4186 void Foo (A const & = A ());
4188 We must process A::A before A::Foo's default arg can be converted.
4189 Remember the dependent function, so do_pending_defargs can retry,
4191 unprocessed_defarg_fn (fn);
4193 /* Don't return error_mark node, as we won't be able to distinguish
4194 genuine errors from this case, and that would lead to repeated
4195 diagnostics. Just make something of the right type. */
4196 return build1 (NOP_EXPR, type, integer_zero_node);
4199 if (fn && DECL_TEMPLATE_INFO (fn))
4200 arg = tsubst_default_argument (fn, type, arg);
4202 arg = break_out_target_exprs (arg);
4204 if (TREE_CODE (arg) == CONSTRUCTOR)
4206 arg = digest_init (type, arg, 0);
4207 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4208 "default argument", fn, parmnum);
4212 /* This could get clobbered by the following call. */
4213 if (TREE_HAS_CONSTRUCTOR (arg))
4214 arg = copy_node (arg);
4216 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4217 "default argument", fn, parmnum);
4218 arg = convert_for_arg_passing (type, arg);
4224 /* Returns the type which will really be used for passing an argument of
4228 type_passed_as (tree type)
4230 /* Pass classes with copy ctors by invisible reference. */
4231 if (TREE_ADDRESSABLE (type))
4232 type = build_reference_type (type);
4233 else if (PROMOTE_PROTOTYPES
4234 && INTEGRAL_TYPE_P (type)
4235 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4236 type = integer_type_node;
4241 /* Actually perform the appropriate conversion. */
4244 convert_for_arg_passing (tree type, tree val)
4246 if (val == error_mark_node)
4248 /* Pass classes with copy ctors by invisible reference. */
4249 else if (TREE_ADDRESSABLE (type))
4250 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4251 else if (PROMOTE_PROTOTYPES
4252 && INTEGRAL_TYPE_P (type)
4253 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4254 val = default_conversion (val);
4258 /* Subroutine of the various build_*_call functions. Overload resolution
4259 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4260 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4261 bitmask of various LOOKUP_* flags which apply to the call itself. */
4264 build_over_call (struct z_candidate *cand, tree args, int flags)
4267 tree convs = cand->convs;
4268 tree converted_args = NULL_TREE;
4269 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4270 tree conv, arg, val;
4274 /* Give any warnings we noticed during overload resolution. */
4276 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4277 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4279 if (DECL_FUNCTION_MEMBER_P (fn))
4280 enforce_access (cand->access_path, fn);
4282 if (args && TREE_CODE (args) != TREE_LIST)
4283 args = build_tree_list (NULL_TREE, args);
4286 /* The implicit parameters to a constructor are not considered by overload
4287 resolution, and must be of the proper type. */
4288 if (DECL_CONSTRUCTOR_P (fn))
4290 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4291 arg = TREE_CHAIN (arg);
4292 parm = TREE_CHAIN (parm);
4293 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4294 /* We should never try to call the abstract constructor. */
4296 if (DECL_HAS_VTT_PARM_P (fn))
4298 converted_args = tree_cons
4299 (NULL_TREE, TREE_VALUE (arg), converted_args);
4300 arg = TREE_CHAIN (arg);
4301 parm = TREE_CHAIN (parm);
4304 /* Bypass access control for 'this' parameter. */
4305 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4307 tree parmtype = TREE_VALUE (parm);
4308 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4312 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4313 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4314 TREE_TYPE (argtype), fn);
4316 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4317 X is called for an object that is not of type X, or of a type
4318 derived from X, the behavior is undefined.
4320 So we can assume that anything passed as 'this' is non-null, and
4321 optimize accordingly. */
4322 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4323 /* Convert to the base in which the function was declared. */
4324 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4325 converted_arg = build_base_path (PLUS_EXPR,
4327 cand->conversion_path,
4329 /* If fn was found by a using declaration, the conversion path
4330 will be to the derived class, not the base declaring fn. We
4331 must convert from derived to base. */
4332 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4333 TREE_TYPE (parmtype), ba_ignore, NULL);
4335 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4338 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4339 parm = TREE_CHAIN (parm);
4340 arg = TREE_CHAIN (arg);
4346 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4348 tree type = TREE_VALUE (parm);
4350 conv = TREE_VEC_ELT (convs, i);
4351 val = convert_like_with_context
4352 (conv, TREE_VALUE (arg), fn, i - is_method);
4354 val = convert_for_arg_passing (type, val);
4355 converted_args = tree_cons (NULL_TREE, val, converted_args);
4358 /* Default arguments */
4359 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4361 = tree_cons (NULL_TREE,
4362 convert_default_arg (TREE_VALUE (parm),
4363 TREE_PURPOSE (parm),
4368 for (; arg; arg = TREE_CHAIN (arg))
4370 = tree_cons (NULL_TREE,
4371 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4374 converted_args = nreverse (converted_args);
4377 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4380 /* Avoid actually calling copy constructors and copy assignment operators,
4383 if (! flag_elide_constructors)
4384 /* Do things the hard way. */;
4385 else if (TREE_VEC_LENGTH (convs) == 1
4386 && DECL_COPY_CONSTRUCTOR_P (fn))
4389 arg = skip_artificial_parms_for (fn, converted_args);
4390 arg = TREE_VALUE (arg);
4392 /* Pull out the real argument, disregarding const-correctness. */
4394 while (TREE_CODE (targ) == NOP_EXPR
4395 || TREE_CODE (targ) == NON_LVALUE_EXPR
4396 || TREE_CODE (targ) == CONVERT_EXPR)
4397 targ = TREE_OPERAND (targ, 0);
4398 if (TREE_CODE (targ) == ADDR_EXPR)
4400 targ = TREE_OPERAND (targ, 0);
4401 if (!same_type_ignoring_top_level_qualifiers_p
4402 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4411 arg = build_indirect_ref (arg, 0);
4413 /* [class.copy]: the copy constructor is implicitly defined even if
4414 the implementation elided its use. */
4415 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4418 /* If we're creating a temp and we already have one, don't create a
4419 new one. If we're not creating a temp but we get one, use
4420 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4421 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4422 temp or an INIT_EXPR otherwise. */
4423 if (integer_zerop (TREE_VALUE (args)))
4425 if (TREE_CODE (arg) == TARGET_EXPR)
4427 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4428 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4430 else if (TREE_CODE (arg) == TARGET_EXPR
4431 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4434 tree to = stabilize_reference
4435 (build_indirect_ref (TREE_VALUE (args), 0));
4437 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4438 address = build_unary_op (ADDR_EXPR, val, 0);
4439 /* Avoid a warning about this expression, if the address is
4441 TREE_USED (address) = 1;
4445 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4447 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4449 tree to = stabilize_reference
4450 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4452 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4453 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4459 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4461 tree t, *p = &TREE_VALUE (converted_args);
4462 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4465 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4467 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4468 if (TREE_SIDE_EFFECTS (*p))
4469 *p = save_expr (*p);
4470 t = build_pointer_type (TREE_TYPE (fn));
4471 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4472 fn = build_java_interface_fn_ref (fn, *p);
4474 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4477 else if (DECL_INLINE (fn))
4478 fn = inline_conversion (fn);
4480 fn = build_addr_func (fn);
4482 /* Recognize certain built-in functions so we can make tree-codes
4483 other than CALL_EXPR. We do this when it enables fold-const.c
4484 to do something useful. */
4486 if (TREE_CODE (fn) == ADDR_EXPR
4487 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4488 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4491 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4496 /* Some built-in function calls will be evaluated at
4497 compile-time in fold (). */
4498 fn = fold (build_call (fn, converted_args));
4499 if (VOID_TYPE_P (TREE_TYPE (fn)))
4501 fn = require_complete_type (fn);
4502 if (fn == error_mark_node)
4503 return error_mark_node;
4504 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4505 fn = build_cplus_new (TREE_TYPE (fn), fn);
4506 return convert_from_reference (fn);
4509 static GTY(()) tree java_iface_lookup_fn;
4511 /* Make an expression which yields the address of the Java interface
4512 method FN. This is achieved by generating a call to libjava's
4513 _Jv_LookupInterfaceMethodIdx(). */
4516 build_java_interface_fn_ref (tree fn, tree instance)
4518 tree lookup_args, lookup_fn, method, idx;
4519 tree klass_ref, iface, iface_ref;
4522 if (!java_iface_lookup_fn)
4524 tree endlink = build_void_list_node ();
4525 tree t = tree_cons (NULL_TREE, ptr_type_node,
4526 tree_cons (NULL_TREE, ptr_type_node,
4527 tree_cons (NULL_TREE, java_int_type_node,
4529 java_iface_lookup_fn
4530 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4531 build_function_type (ptr_type_node, t),
4532 0, NOT_BUILT_IN, NULL, NULL_TREE);
4535 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4536 This is the first entry in the vtable. */
4537 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4540 /* Get the java.lang.Class pointer for the interface being called. */
4541 iface = DECL_CONTEXT (fn);
4542 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4543 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4544 || DECL_CONTEXT (iface_ref) != iface)
4546 error ("could not find class$ field in java interface type `%T'",
4548 return error_mark_node;
4550 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4552 /* Determine the itable index of FN. */
4554 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4556 if (!DECL_VIRTUAL_P (method))
4562 idx = build_int_2 (i, 0);
4564 lookup_args = tree_cons (NULL_TREE, klass_ref,
4565 tree_cons (NULL_TREE, iface_ref,
4566 build_tree_list (NULL_TREE, idx)));
4567 lookup_fn = build1 (ADDR_EXPR,
4568 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4569 java_iface_lookup_fn);
4570 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4573 /* Returns the value to use for the in-charge parameter when making a
4574 call to a function with the indicated NAME. */
4577 in_charge_arg_for_name (tree name)
4579 if (name == base_ctor_identifier
4580 || name == base_dtor_identifier)
4581 return integer_zero_node;
4582 else if (name == complete_ctor_identifier)
4583 return integer_one_node;
4584 else if (name == complete_dtor_identifier)
4585 return integer_two_node;
4586 else if (name == deleting_dtor_identifier)
4587 return integer_three_node;
4589 /* This function should only be called with one of the names listed
4595 /* Build a call to a constructor, destructor, or an assignment
4596 operator for INSTANCE, an expression with class type. NAME
4597 indicates the special member function to call; ARGS are the
4598 arguments. BINFO indicates the base of INSTANCE that is to be
4599 passed as the `this' parameter to the member function called.
4601 FLAGS are the LOOKUP_* flags to use when processing the call.
4603 If NAME indicates a complete object constructor, INSTANCE may be
4604 NULL_TREE. In this case, the caller will call build_cplus_new to
4605 store the newly constructed object into a VAR_DECL. */
4608 build_special_member_call (tree instance, tree name, tree args,
4609 tree binfo, int flags)
4612 /* The type of the subobject to be constructed or destroyed. */
4615 my_friendly_assert (name == complete_ctor_identifier
4616 || name == base_ctor_identifier
4617 || name == complete_dtor_identifier
4618 || name == base_dtor_identifier
4619 || name == deleting_dtor_identifier
4620 || name == ansi_assopname (NOP_EXPR),
4622 my_friendly_assert (binfo != NULL_TREE, 20020712);
4624 class_type = BINFO_TYPE (binfo);
4626 /* Handle the special case where INSTANCE is NULL_TREE. */
4627 if (name == complete_ctor_identifier && !instance)
4629 instance = build_int_2 (0, 0);
4630 TREE_TYPE (instance) = build_pointer_type (class_type);
4631 instance = build1 (INDIRECT_REF, class_type, instance);
4633 else if (name == complete_dtor_identifier
4634 || name == base_dtor_identifier
4635 || name == deleting_dtor_identifier)
4636 my_friendly_assert (args == NULL_TREE, 20020712);
4638 my_friendly_assert (instance != NULL_TREE, 20020712);
4640 /* Resolve the name. */
4641 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4642 return error_mark_node;
4644 fns = lookup_fnfields (binfo, name, 1);
4646 /* When making a call to a constructor or destructor for a subobject
4647 that uses virtual base classes, pass down a pointer to a VTT for
4649 if ((name == base_ctor_identifier
4650 || name == base_dtor_identifier)
4651 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4656 /* If the current function is a complete object constructor
4657 or destructor, then we fetch the VTT directly.
4658 Otherwise, we look it up using the VTT we were given. */
4659 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4660 vtt = decay_conversion (vtt);
4661 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4662 build (EQ_EXPR, boolean_type_node,
4663 current_in_charge_parm, integer_zero_node),
4666 if (TREE_VIA_VIRTUAL (binfo))
4667 binfo = binfo_for_vbase (class_type, current_class_type);
4668 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4669 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4670 BINFO_SUBVTT_INDEX (binfo));
4672 args = tree_cons (NULL_TREE, sub_vtt, args);
4675 return build_new_method_call (instance, fns, args, binfo, flags);
4678 /* Build a call to "INSTANCE.FN (ARGS)". */
4681 build_new_method_call (tree instance, tree fns, tree args,
4682 tree conversion_path, int flags)
4684 struct z_candidate *candidates = 0, *cand;
4685 tree explicit_targs = NULL_TREE;
4686 tree basetype = NULL_TREE;
4689 tree mem_args = NULL_TREE, instance_ptr;
4690 tree name, pretty_name;
4692 tree templates = NULL_TREE;
4694 int template_only = 0;
4696 my_friendly_assert (instance != NULL_TREE, 20020729);
4698 if (instance == error_mark_node || fns == error_mark_node
4699 || args == error_mark_node)
4700 return error_mark_node;
4702 /* Process the argument list. */
4704 args = resolve_args (args);
4705 if (args == error_mark_node)
4706 return error_mark_node;
4708 if (TREE_CODE (instance) == OFFSET_REF)
4709 instance = resolve_offset_ref (instance);
4710 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4711 instance = convert_from_reference (instance);
4712 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4713 instance_ptr = build_this (instance);
4715 if (!BASELINK_P (fns))
4717 call = build_field_call (instance_ptr, fns, args);
4720 error ("call to non-function `%D'", fns);
4721 return error_mark_node;
4724 if (!conversion_path)
4725 conversion_path = BASELINK_BINFO (fns);
4726 access_binfo = BASELINK_ACCESS_BINFO (fns);
4727 optype = BASELINK_OPTYPE (fns);
4728 fns = BASELINK_FUNCTIONS (fns);
4730 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4732 explicit_targs = TREE_OPERAND (fns, 1);
4733 fns = TREE_OPERAND (fns, 0);
4737 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4738 || TREE_CODE (fns) == TEMPLATE_DECL
4739 || TREE_CODE (fns) == OVERLOAD,
4742 /* XXX this should be handled before we get here. */
4743 if (! IS_AGGR_TYPE (basetype))
4745 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4746 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4747 fns, instance, basetype);
4749 return error_mark_node;
4752 name = DECL_NAME (get_first_fn (fns));
4754 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4756 /* Callers should explicitly indicate whether they want to construct
4757 the complete object or just the part without virtual bases. */
4758 my_friendly_assert (name != ctor_identifier, 20000408);
4759 /* Similarly for destructors. */
4760 my_friendly_assert (name != dtor_identifier, 20000408);
4762 if (name == complete_ctor_identifier
4763 || name == base_ctor_identifier)
4764 pretty_name = constructor_name (basetype);
4766 pretty_name = dtor_identifier;
4774 tree class_type = (conversion_path
4775 ? BINFO_TYPE (conversion_path)
4778 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4779 for (fn = fns; fn; fn = OVL_NEXT (fn))
4781 tree t = OVL_CURRENT (fn);
4784 /* We can end up here for copy-init of same or base class. */
4785 if ((flags & LOOKUP_ONLYCONVERTING)
4786 && DECL_NONCONVERTING_P (t))
4789 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4790 this_arglist = mem_args;
4792 this_arglist = args;
4794 if (TREE_CODE (t) == TEMPLATE_DECL)
4796 /* A member template. */
4797 templates = tree_cons (NULL_TREE, t, templates);
4799 add_template_candidate (candidates, t,
4802 this_arglist, optype,
4808 else if (! template_only)
4809 candidates = add_function_candidate (candidates, t,
4818 if (! any_viable (candidates))
4820 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4821 if (flags & LOOKUP_SPECULATIVELY)
4823 if (!COMPLETE_TYPE_P (basetype))
4824 cxx_incomplete_type_error (instance_ptr, basetype);
4826 error ("no matching function for call to `%T::%D(%A)%#V'",
4827 basetype, pretty_name, user_args,
4828 TREE_TYPE (TREE_TYPE (instance_ptr)));
4829 print_z_candidates (candidates);
4830 return error_mark_node;
4832 candidates = splice_viable (candidates);
4833 cand = tourney (candidates);
4837 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4839 print_z_candidates (candidates);
4840 return error_mark_node;
4843 if (DECL_PURE_VIRTUAL_P (cand->fn)
4844 && instance == current_class_ref
4845 && (DECL_CONSTRUCTOR_P (current_function_decl)
4846 || DECL_DESTRUCTOR_P (current_function_decl))
4847 && ! (flags & LOOKUP_NONVIRTUAL)
4848 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4849 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4850 "abstract virtual `%#D' called from constructor"
4851 : "abstract virtual `%#D' called from destructor"),
4853 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4854 && is_dummy_object (instance_ptr))
4856 error ("cannot call member function `%D' without object", cand->fn);
4857 return error_mark_node;
4860 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4861 && resolves_to_fixed_type_p (instance, 0))
4862 flags |= LOOKUP_NONVIRTUAL;
4864 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4865 call = build_over_call (cand, mem_args, flags);
4868 call = build_over_call (cand, args, flags);
4869 /* In an expression of the form `a->f()' where `f' turns out to
4870 be a static member function, `a' is none-the-less evaluated. */
4871 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
4872 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4878 /* Returns true iff standard conversion sequence ICS1 is a proper
4879 subsequence of ICS2. */
4882 is_subseq (tree ics1, tree ics2)
4884 /* We can assume that a conversion of the same code
4885 between the same types indicates a subsequence since we only get
4886 here if the types we are converting from are the same. */
4888 while (TREE_CODE (ics1) == RVALUE_CONV
4889 || TREE_CODE (ics1) == LVALUE_CONV)
4890 ics1 = TREE_OPERAND (ics1, 0);
4894 while (TREE_CODE (ics2) == RVALUE_CONV
4895 || TREE_CODE (ics2) == LVALUE_CONV)
4896 ics2 = TREE_OPERAND (ics2, 0);
4898 if (TREE_CODE (ics2) == USER_CONV
4899 || TREE_CODE (ics2) == AMBIG_CONV
4900 || TREE_CODE (ics2) == IDENTITY_CONV)
4901 /* At this point, ICS1 cannot be a proper subsequence of
4902 ICS2. We can get a USER_CONV when we are comparing the
4903 second standard conversion sequence of two user conversion
4907 ics2 = TREE_OPERAND (ics2, 0);
4909 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4910 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4911 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4912 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4917 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
4918 be any _TYPE nodes. */
4921 is_properly_derived_from (tree derived, tree base)
4923 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4924 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4927 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4928 considers every class derived from itself. */
4929 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4930 && DERIVED_FROM_P (base, derived));
4933 /* We build the ICS for an implicit object parameter as a pointer
4934 conversion sequence. However, such a sequence should be compared
4935 as if it were a reference conversion sequence. If ICS is the
4936 implicit conversion sequence for an implicit object parameter,
4937 modify it accordingly. */
4940 maybe_handle_implicit_object (tree *ics)
4942 if (ICS_THIS_FLAG (*ics))
4944 /* [over.match.funcs]
4946 For non-static member functions, the type of the
4947 implicit object parameter is "reference to cv X"
4948 where X is the class of which the function is a
4949 member and cv is the cv-qualification on the member
4950 function declaration. */
4952 tree reference_type;
4954 /* The `this' parameter is a pointer to a class type. Make the
4955 implict conversion talk about a reference to that same class
4957 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4958 reference_type = build_reference_type (reference_type);
4960 if (TREE_CODE (t) == QUAL_CONV)
4961 t = TREE_OPERAND (t, 0);
4962 if (TREE_CODE (t) == PTR_CONV)
4963 t = TREE_OPERAND (t, 0);
4964 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4965 t = direct_reference_binding (reference_type, t);
4970 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4971 and return the type to which the reference refers. Otherwise,
4972 leave *ICS unchanged and return NULL_TREE. */
4975 maybe_handle_ref_bind (tree *ics)
4977 if (TREE_CODE (*ics) == REF_BIND)
4979 tree old_ics = *ics;
4980 tree type = TREE_TYPE (TREE_TYPE (old_ics));
4981 *ics = TREE_OPERAND (old_ics, 0);
4982 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4983 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4990 /* Compare two implicit conversion sequences according to the rules set out in
4991 [over.ics.rank]. Return values:
4993 1: ics1 is better than ics2
4994 -1: ics2 is better than ics1
4995 0: ics1 and ics2 are indistinguishable */
4998 compare_ics (tree ics1, tree ics2)
5004 tree deref_from_type1 = NULL_TREE;
5005 tree deref_from_type2 = NULL_TREE;
5006 tree deref_to_type1 = NULL_TREE;
5007 tree deref_to_type2 = NULL_TREE;
5010 /* REF_BINDING is nonzero if the result of the conversion sequence
5011 is a reference type. In that case TARGET_TYPE is the
5012 type referred to by the reference. */
5016 /* Handle implicit object parameters. */
5017 maybe_handle_implicit_object (&ics1);
5018 maybe_handle_implicit_object (&ics2);
5020 /* Handle reference parameters. */
5021 target_type1 = maybe_handle_ref_bind (&ics1);
5022 target_type2 = maybe_handle_ref_bind (&ics2);
5026 When comparing the basic forms of implicit conversion sequences (as
5027 defined in _over.best.ics_)
5029 --a standard conversion sequence (_over.ics.scs_) is a better
5030 conversion sequence than a user-defined conversion sequence
5031 or an ellipsis conversion sequence, and
5033 --a user-defined conversion sequence (_over.ics.user_) is a
5034 better conversion sequence than an ellipsis conversion sequence
5035 (_over.ics.ellipsis_). */
5036 rank1 = ICS_RANK (ics1);
5037 rank2 = ICS_RANK (ics2);
5041 else if (rank1 < rank2)
5044 if (rank1 == BAD_RANK)
5046 /* XXX Isn't this an extension? */
5047 /* Both ICS are bad. We try to make a decision based on what
5048 would have happenned if they'd been good. */
5049 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5050 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5052 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5053 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5056 /* We couldn't make up our minds; try to figure it out below. */
5059 if (ICS_ELLIPSIS_FLAG (ics1))
5060 /* Both conversions are ellipsis conversions. */
5063 /* User-defined conversion sequence U1 is a better conversion sequence
5064 than another user-defined conversion sequence U2 if they contain the
5065 same user-defined conversion operator or constructor and if the sec-
5066 ond standard conversion sequence of U1 is better than the second
5067 standard conversion sequence of U2. */
5069 if (ICS_USER_FLAG (ics1))
5073 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5074 if (TREE_CODE (t1) == AMBIG_CONV)
5076 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5077 if (TREE_CODE (t2) == AMBIG_CONV)
5080 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5083 /* We can just fall through here, after setting up
5084 FROM_TYPE1 and FROM_TYPE2. */
5085 from_type1 = TREE_TYPE (t1);
5086 from_type2 = TREE_TYPE (t2);
5090 /* We're dealing with two standard conversion sequences.
5094 Standard conversion sequence S1 is a better conversion
5095 sequence than standard conversion sequence S2 if
5097 --S1 is a proper subsequence of S2 (comparing the conversion
5098 sequences in the canonical form defined by _over.ics.scs_,
5099 excluding any Lvalue Transformation; the identity
5100 conversion sequence is considered to be a subsequence of
5101 any non-identity conversion sequence */
5104 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5105 from_type1 = TREE_OPERAND (from_type1, 0);
5106 from_type1 = TREE_TYPE (from_type1);
5109 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5110 from_type2 = TREE_OPERAND (from_type2, 0);
5111 from_type2 = TREE_TYPE (from_type2);
5114 if (same_type_p (from_type1, from_type2))
5116 if (is_subseq (ics1, ics2))
5118 if (is_subseq (ics2, ics1))
5121 /* Otherwise, one sequence cannot be a subsequence of the other; they
5122 don't start with the same type. This can happen when comparing the
5123 second standard conversion sequence in two user-defined conversion
5130 --the rank of S1 is better than the rank of S2 (by the rules
5133 Standard conversion sequences are ordered by their ranks: an Exact
5134 Match is a better conversion than a Promotion, which is a better
5135 conversion than a Conversion.
5137 Two conversion sequences with the same rank are indistinguishable
5138 unless one of the following rules applies:
5140 --A conversion that is not a conversion of a pointer, or pointer
5141 to member, to bool is better than another conversion that is such
5144 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5145 so that we do not have to check it explicitly. */
5146 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5148 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5151 to_type1 = TREE_TYPE (ics1);
5152 to_type2 = TREE_TYPE (ics2);
5154 if (TYPE_PTR_P (from_type1)
5155 && TYPE_PTR_P (from_type2)
5156 && TYPE_PTR_P (to_type1)
5157 && TYPE_PTR_P (to_type2))
5159 deref_from_type1 = TREE_TYPE (from_type1);
5160 deref_from_type2 = TREE_TYPE (from_type2);
5161 deref_to_type1 = TREE_TYPE (to_type1);
5162 deref_to_type2 = TREE_TYPE (to_type2);
5164 /* The rules for pointers to members A::* are just like the rules
5165 for pointers A*, except opposite: if B is derived from A then
5166 A::* converts to B::*, not vice versa. For that reason, we
5167 switch the from_ and to_ variables here. */
5168 else if (TYPE_PTRMEM_P (from_type1)
5169 && TYPE_PTRMEM_P (from_type2)
5170 && TYPE_PTRMEM_P (to_type1)
5171 && TYPE_PTRMEM_P (to_type2))
5173 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5174 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5175 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5176 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5178 else if (TYPE_PTRMEMFUNC_P (from_type1)
5179 && TYPE_PTRMEMFUNC_P (from_type2)
5180 && TYPE_PTRMEMFUNC_P (to_type1)
5181 && TYPE_PTRMEMFUNC_P (to_type2))
5183 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5184 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5185 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5186 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5189 if (deref_from_type1 != NULL_TREE
5190 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5191 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5193 /* This was one of the pointer or pointer-like conversions.
5197 --If class B is derived directly or indirectly from class A,
5198 conversion of B* to A* is better than conversion of B* to
5199 void*, and conversion of A* to void* is better than
5200 conversion of B* to void*. */
5201 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5202 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5204 if (is_properly_derived_from (deref_from_type1,
5207 else if (is_properly_derived_from (deref_from_type2,
5211 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5212 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5214 if (same_type_p (deref_from_type1, deref_from_type2))
5216 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5218 if (is_properly_derived_from (deref_from_type1,
5222 /* We know that DEREF_TO_TYPE1 is `void' here. */
5223 else if (is_properly_derived_from (deref_from_type1,
5228 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5229 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5233 --If class B is derived directly or indirectly from class A
5234 and class C is derived directly or indirectly from B,
5236 --conversion of C* to B* is better than conversion of C* to
5239 --conversion of B* to A* is better than conversion of C* to
5241 if (same_type_p (deref_from_type1, deref_from_type2))
5243 if (is_properly_derived_from (deref_to_type1,
5246 else if (is_properly_derived_from (deref_to_type2,
5250 else if (same_type_p (deref_to_type1, deref_to_type2))
5252 if (is_properly_derived_from (deref_from_type2,
5255 else if (is_properly_derived_from (deref_from_type1,
5261 else if (CLASS_TYPE_P (non_reference (from_type1))
5262 && same_type_p (from_type1, from_type2))
5264 tree from = non_reference (from_type1);
5268 --binding of an expression of type C to a reference of type
5269 B& is better than binding an expression of type C to a
5270 reference of type A&
5272 --conversion of C to B is better than conversion of C to A, */
5273 if (is_properly_derived_from (from, to_type1)
5274 && is_properly_derived_from (from, to_type2))
5276 if (is_properly_derived_from (to_type1, to_type2))
5278 else if (is_properly_derived_from (to_type2, to_type1))
5282 else if (CLASS_TYPE_P (non_reference (to_type1))
5283 && same_type_p (to_type1, to_type2))
5285 tree to = non_reference (to_type1);
5289 --binding of an expression of type B to a reference of type
5290 A& is better than binding an expression of type C to a
5291 reference of type A&,
5293 --onversion of B to A is better than conversion of C to A */
5294 if (is_properly_derived_from (from_type1, to)
5295 && is_properly_derived_from (from_type2, to))
5297 if (is_properly_derived_from (from_type2, from_type1))
5299 else if (is_properly_derived_from (from_type1, from_type2))
5306 --S1 and S2 differ only in their qualification conversion and yield
5307 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5308 qualification signature of type T1 is a proper subset of the cv-
5309 qualification signature of type T2 */
5310 if (TREE_CODE (ics1) == QUAL_CONV
5311 && TREE_CODE (ics2) == QUAL_CONV
5312 && same_type_p (from_type1, from_type2))
5313 return comp_cv_qual_signature (to_type1, to_type2);
5317 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5318 types to which the references refer are the same type except for
5319 top-level cv-qualifiers, and the type to which the reference
5320 initialized by S2 refers is more cv-qualified than the type to
5321 which the reference initialized by S1 refers */
5323 if (target_type1 && target_type2
5324 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5325 return comp_cv_qualification (target_type2, target_type1);
5327 /* Neither conversion sequence is better than the other. */
5331 /* The source type for this standard conversion sequence. */
5334 source_type (tree t)
5336 for (;; t = TREE_OPERAND (t, 0))
5338 if (TREE_CODE (t) == USER_CONV
5339 || TREE_CODE (t) == AMBIG_CONV
5340 || TREE_CODE (t) == IDENTITY_CONV)
5341 return TREE_TYPE (t);
5346 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5347 a pointer to LOSER and re-running joust to produce the warning if WINNER
5348 is actually used. */
5351 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5353 winner->warnings = tree_cons (NULL_TREE,
5354 build_zc_wrapper (loser),
5358 /* Returns true iff functions are equivalent. Equivalent functions are
5359 not '==' only if one is a function-local extern function or if
5360 both are extern "C". */
5363 equal_functions (tree fn1, tree fn2)
5365 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5366 || DECL_EXTERN_C_FUNCTION_P (fn1))
5367 return decls_match (fn1, fn2);
5371 /* Compare two candidates for overloading as described in
5372 [over.match.best]. Return values:
5374 1: cand1 is better than cand2
5375 -1: cand2 is better than cand1
5376 0: cand1 and cand2 are indistinguishable */
5379 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5382 int i, off1 = 0, off2 = 0, len;
5384 /* Candidates that involve bad conversions are always worse than those
5386 if (cand1->viable > cand2->viable)
5388 if (cand1->viable < cand2->viable)
5391 /* If we have two pseudo-candidates for conversions to the same type,
5392 or two candidates for the same function, arbitrarily pick one. */
5393 if (cand1->fn == cand2->fn
5394 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5397 /* a viable function F1
5398 is defined to be a better function than another viable function F2 if
5399 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5400 ICSi(F2), and then */
5402 /* for some argument j, ICSj(F1) is a better conversion sequence than
5405 /* For comparing static and non-static member functions, we ignore
5406 the implicit object parameter of the non-static function. The
5407 standard says to pretend that the static function has an object
5408 parm, but that won't work with operator overloading. */
5409 len = TREE_VEC_LENGTH (cand1->convs);
5410 if (len != TREE_VEC_LENGTH (cand2->convs))
5412 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5413 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5415 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5416 && DECL_STATIC_FUNCTION_P (cand2->fn))
5425 for (i = 0; i < len; ++i)
5427 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5428 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5429 int comp = compare_ics (t1, t2);
5434 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5435 && TREE_CODE (t1) == STD_CONV
5436 && TREE_CODE (t2) == STD_CONV
5437 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5438 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5439 && (TYPE_PRECISION (TREE_TYPE (t1))
5440 == TYPE_PRECISION (TREE_TYPE (t2)))
5441 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5442 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5445 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5447 struct z_candidate *w, *l;
5449 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5450 w = cand1, l = cand2;
5452 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5453 w = cand2, l = cand1;
5457 warning ("passing `%T' chooses `%T' over `%T'",
5458 type, type1, type2);
5459 warning (" in call to `%D'", w->fn);
5465 if (winner && comp != winner)
5474 /* warn about confusing overload resolution for user-defined conversions,
5475 either between a constructor and a conversion op, or between two
5477 if (winner && cand1->second_conv
5478 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5479 != DECL_CONSTRUCTOR_P (cand2->fn))
5480 /* Don't warn if the two conv ops convert to the same type... */
5481 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5482 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5483 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5485 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5488 struct z_candidate *w, *l;
5491 w = cand1, l = cand2;
5493 w = cand2, l = cand1;
5494 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5495 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5496 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5497 && (convn = standard_conversion
5498 (TREE_TYPE (TREE_TYPE (l->fn)),
5499 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5500 && TREE_CODE (convn) == QUAL_CONV)
5501 /* Don't complain about `operator char *()' beating
5502 `operator const char *() const'. */;
5505 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5506 if (! DECL_CONSTRUCTOR_P (w->fn))
5507 source = TREE_TYPE (source);
5508 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5509 warning (" for conversion from `%T' to `%T'",
5510 source, TREE_TYPE (w->second_conv));
5511 warning (" because conversion sequence for the argument is better");
5522 F1 is a non-template function and F2 is a template function
5525 if (! cand1->template && cand2->template)
5527 else if (cand1->template && ! cand2->template)
5531 F1 and F2 are template functions and the function template for F1 is
5532 more specialized than the template for F2 according to the partial
5535 if (cand1->template && cand2->template)
5537 winner = more_specialized
5538 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5540 /* Tell the deduction code how many real function arguments
5541 we saw, not counting the implicit 'this' argument. But,
5542 add_function_candidate() suppresses the "this" argument
5545 [temp.func.order]: The presence of unused ellipsis and default
5546 arguments has no effect on the partial ordering of function
5548 TREE_VEC_LENGTH (cand1->convs)
5549 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5550 - DECL_CONSTRUCTOR_P (cand1->fn)));
5557 the context is an initialization by user-defined conversion (see
5558 _dcl.init_ and _over.match.user_) and the standard conversion
5559 sequence from the return type of F1 to the destination type (i.e.,
5560 the type of the entity being initialized) is a better conversion
5561 sequence than the standard conversion sequence from the return type
5562 of F2 to the destination type. */
5564 if (cand1->second_conv)
5566 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5571 /* Check whether we can discard a builtin candidate, either because we
5572 have two identical ones or matching builtin and non-builtin candidates.
5574 (Pedantically in the latter case the builtin which matched the user
5575 function should not be added to the overload set, but we spot it here.
5578 ... the builtin candidates include ...
5579 - do not have the same parameter type list as any non-template
5580 non-member candidate. */
5582 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5583 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5585 for (i = 0; i < len; ++i)
5586 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5587 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5589 if (i == TREE_VEC_LENGTH (cand1->convs))
5591 if (cand1->fn == cand2->fn)
5592 /* Two built-in candidates; arbitrarily pick one. */
5594 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5595 /* cand1 is built-in; prefer cand2. */
5598 /* cand2 is built-in; prefer cand1. */
5603 /* If the two functions are the same (this can happen with declarations
5604 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5605 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5606 && equal_functions (cand1->fn, cand2->fn))
5611 /* Extension: If the worst conversion for one candidate is worse than the
5612 worst conversion for the other, take the first. */
5615 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5616 struct z_candidate *w = 0, *l = 0;
5618 for (i = 0; i < len; ++i)
5620 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5621 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5622 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5623 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5626 winner = 1, w = cand1, l = cand2;
5628 winner = -1, w = cand2, l = cand1;
5633 pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5635 " because worst conversion for the former is better than worst conversion for the latter");
5643 my_friendly_assert (!winner, 20010121);
5647 /* Given a list of candidates for overloading, find the best one, if any.
5648 This algorithm has a worst case of O(2n) (winner is last), and a best
5649 case of O(n/2) (totally ambiguous); much better than a sorting
5652 static struct z_candidate *
5653 tourney (struct z_candidate *candidates)
5655 struct z_candidate *champ = candidates, *challenger;
5657 int champ_compared_to_predecessor = 0;
5659 /* Walk through the list once, comparing each current champ to the next
5660 candidate, knocking out a candidate or two with each comparison. */
5662 for (challenger = champ->next; challenger; )
5664 fate = joust (champ, challenger, 0);
5666 challenger = challenger->next;
5671 champ = challenger->next;
5674 champ_compared_to_predecessor = 0;
5679 champ_compared_to_predecessor = 1;
5682 challenger = champ->next;
5686 /* Make sure the champ is better than all the candidates it hasn't yet
5687 been compared to. */
5689 for (challenger = candidates;
5691 && !(champ_compared_to_predecessor && challenger->next == champ);
5692 challenger = challenger->next)
5694 fate = joust (champ, challenger, 0);
5702 /* Returns nonzero if things of type FROM can be converted to TO. */
5705 can_convert (tree to, tree from)
5707 return can_convert_arg (to, from, NULL_TREE);
5710 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5713 can_convert_arg (tree to, tree from, tree arg)
5715 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5716 return (t && ! ICS_BAD_FLAG (t));
5719 /* Like can_convert_arg, but allows dubious conversions as well. */
5722 can_convert_arg_bad (tree to, tree from, tree arg)
5724 return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
5727 /* Convert EXPR to TYPE. Return the converted expression.
5729 Note that we allow bad conversions here because by the time we get to
5730 this point we are committed to doing the conversion. If we end up
5731 doing a bad conversion, convert_like will complain. */
5734 perform_implicit_conversion (tree type, tree expr)
5738 if (expr == error_mark_node)
5739 return error_mark_node;
5740 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5744 error ("could not convert `%E' to `%T'", expr, type);
5745 return error_mark_node;
5748 return convert_like (conv, expr);
5751 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5752 initializing a variable of that TYPE. Return the converted
5756 initialize_reference (tree type, tree expr)
5760 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5761 if (!conv || ICS_BAD_FLAG (conv))
5763 error ("could not convert `%E' to `%T'", expr, type);
5764 return error_mark_node;
5767 return convert_like (conv, expr);
5770 #include "gt-cp-call.h"