1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
5 modified by Brendan Kehoe (brendan@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
37 #include "diagnostic.h"
39 extern int inhibit_warnings;
41 static tree build_new_method_call PARAMS ((tree, tree, tree, tree, int));
43 static tree build_field_call PARAMS ((tree, tree, tree, tree));
44 static struct z_candidate * tourney PARAMS ((struct z_candidate *));
45 static int equal_functions PARAMS ((tree, tree));
46 static int joust PARAMS ((struct z_candidate *, struct z_candidate *, int));
47 static int compare_ics PARAMS ((tree, tree));
48 static tree build_over_call PARAMS ((struct z_candidate *, tree, int));
49 static tree build_java_interface_fn_ref PARAMS ((tree, tree));
50 #define convert_like(CONV, EXPR) \
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 PARAMS ((tree, tree, tree, int, int));
55 static void op_error PARAMS ((enum tree_code, enum tree_code, tree, tree,
57 static tree build_object_call PARAMS ((tree, tree));
58 static tree resolve_args PARAMS ((tree));
59 static struct z_candidate * build_user_type_conversion_1
60 PARAMS ((tree, tree, int));
61 static void print_z_candidates PARAMS ((struct z_candidate *));
62 static tree build_this PARAMS ((tree));
63 static struct z_candidate * splice_viable PARAMS ((struct z_candidate *));
64 static int any_viable PARAMS ((struct z_candidate *));
65 static int any_strictly_viable PARAMS ((struct z_candidate *));
66 static struct z_candidate * add_template_candidate
67 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
69 static struct z_candidate * add_template_candidate_real
70 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree, int,
71 tree, unification_kind_t));
72 static struct z_candidate * add_template_conv_candidate
73 PARAMS ((struct z_candidate *, tree, tree, tree, tree));
74 static struct z_candidate * add_builtin_candidates
75 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
77 static struct z_candidate * add_builtin_candidate
78 PARAMS ((struct z_candidate *, enum tree_code, enum tree_code,
79 tree, tree, tree, tree *, tree *, int));
80 static int is_complete PARAMS ((tree));
81 static struct z_candidate * build_builtin_candidate
82 PARAMS ((struct z_candidate *, tree, tree, tree, tree *, tree *,
84 static struct z_candidate * add_conv_candidate
85 PARAMS ((struct z_candidate *, tree, tree, tree));
86 static struct z_candidate * add_function_candidate
87 PARAMS ((struct z_candidate *, tree, tree, tree, int));
88 static tree implicit_conversion PARAMS ((tree, tree, tree, int));
89 static tree standard_conversion PARAMS ((tree, tree, tree));
90 static tree reference_binding PARAMS ((tree, tree, tree, int));
91 static tree non_reference PARAMS ((tree));
92 static tree build_conv PARAMS ((enum tree_code, tree, tree));
93 static int is_subseq PARAMS ((tree, tree));
94 static tree maybe_handle_ref_bind PARAMS ((tree*));
95 static void maybe_handle_implicit_object PARAMS ((tree*));
96 static struct z_candidate * add_candidate PARAMS ((struct z_candidate *,
98 static tree source_type PARAMS ((tree));
99 static void add_warning PARAMS ((struct z_candidate *, struct z_candidate *));
100 static int reference_related_p PARAMS ((tree, tree));
101 static int reference_compatible_p PARAMS ((tree, tree));
102 static tree convert_class_to_reference PARAMS ((tree, tree, tree));
103 static tree direct_reference_binding PARAMS ((tree, tree));
104 static int promoted_arithmetic_type_p PARAMS ((tree));
105 static tree conditional_conversion PARAMS ((tree, tree));
108 build_vfield_ref (datum, type)
113 if (datum == error_mark_node)
114 return error_mark_node;
116 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
117 datum = convert_from_reference (datum);
119 if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type))
120 rval = build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
121 datum, TYPE_VFIELD (type));
123 rval = build_component_ref (datum, DECL_NAME (TYPE_VFIELD (type)), NULL_TREE, 0);
128 /* Build a call to a member of an object. I.e., one that overloads
129 operator ()(), or is a pointer-to-function or pointer-to-method. */
132 build_field_call (basetype_path, instance_ptr, name, parms)
133 tree basetype_path, instance_ptr, name, parms;
135 tree field, instance;
137 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
140 /* Speed up the common case. */
141 if (instance_ptr == current_class_ptr
142 && IDENTIFIER_CLASS_VALUE (name) == NULL_TREE)
145 field = lookup_field (basetype_path, name, 1, 0);
147 if (field == error_mark_node || field == NULL_TREE)
150 if (TREE_CODE (field) == FIELD_DECL || TREE_CODE (field) == VAR_DECL)
152 /* If it's a field, try overloading operator (),
153 or calling if the field is a pointer-to-function. */
154 instance = build_indirect_ref (instance_ptr, NULL);
155 instance = build_component_ref_1 (instance, field, 0);
157 if (instance == error_mark_node)
158 return error_mark_node;
160 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
161 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
162 instance, parms, NULL_TREE);
163 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
164 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
165 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
167 return build_function_call (instance, parms);
173 /* Returns nonzero iff the destructor name specified in NAME
174 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
178 check_dtor_name (basetype, name)
181 name = TREE_OPERAND (name, 0);
183 /* Just accept something we've already complained about. */
184 if (name == error_mark_node)
187 if (TREE_CODE (name) == TYPE_DECL)
188 name = TREE_TYPE (name);
189 else if (TYPE_P (name))
191 else if (TREE_CODE (name) == IDENTIFIER_NODE)
193 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
194 || (TREE_CODE (basetype) == ENUMERAL_TYPE
195 && name == TYPE_IDENTIFIER (basetype)))
198 name = get_type_value (name);
202 template <class T> struct S { ~S(); };
206 NAME will be a class template. */
207 else if (DECL_CLASS_TEMPLATE_P (name))
212 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
217 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
218 This is how virtual function calls are avoided. */
221 build_scoped_method_call (exp, basetype, name, parms)
222 tree exp, basetype, name, parms;
224 /* Because this syntactic form does not allow
225 a pointer to a base class to be `stolen',
226 we need not protect the derived->base conversion
229 @@ But we do have to check access privileges later. */
231 tree type = TREE_TYPE (exp);
233 if (type == error_mark_node
234 || basetype == error_mark_node)
235 return error_mark_node;
237 if (processing_template_decl)
239 if (TREE_CODE (name) == BIT_NOT_EXPR
240 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
242 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
244 name = build_min_nt (BIT_NOT_EXPR, type);
246 name = build_min_nt (SCOPE_REF, basetype, name);
247 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
250 if (TREE_CODE (type) == REFERENCE_TYPE)
251 type = TREE_TYPE (type);
253 if (TREE_CODE (basetype) == TREE_VEC)
256 basetype = BINFO_TYPE (binfo);
261 /* Check the destructor call syntax. */
262 if (TREE_CODE (name) == BIT_NOT_EXPR)
264 /* We can get here if someone writes their destructor call like
265 `obj.NS::~T()'; this isn't really a scoped method call, so hand
267 if (TREE_CODE (basetype) == NAMESPACE_DECL)
268 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
270 if (! check_dtor_name (basetype, name))
271 error ("qualified type `%T' does not match destructor name `~%T'",
272 basetype, TREE_OPERAND (name, 0));
274 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
275 that explicit ~int is caught in the parser; this deals with typedefs
276 and template parms. */
277 if (! IS_AGGR_TYPE (basetype))
279 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
280 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
281 exp, basetype, type);
283 return cp_convert (void_type_node, exp);
287 if (TREE_CODE (basetype) == NAMESPACE_DECL)
289 error ("`%D' is a namespace", basetype);
290 return error_mark_node;
292 if (! is_aggr_type (basetype, 1))
293 return error_mark_node;
295 if (! IS_AGGR_TYPE (type))
297 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
299 return error_mark_node;
302 decl = build_scoped_ref (exp, basetype, &binfo);
306 /* Call to a destructor. */
307 if (TREE_CODE (name) == BIT_NOT_EXPR)
309 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
310 return cp_convert (void_type_node, exp);
312 return build_delete (TREE_TYPE (decl), decl,
313 sfk_complete_destructor,
314 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
318 /* Call to a method. */
319 return build_method_call (decl, name, parms, binfo,
320 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
322 return error_mark_node;
325 /* We want the address of a function or method. We avoid creating a
326 pointer-to-member function. */
329 build_addr_func (function)
332 tree type = TREE_TYPE (function);
334 /* We have to do these by hand to avoid real pointer to member
336 if (TREE_CODE (type) == METHOD_TYPE)
340 type = build_pointer_type (type);
342 if (!cxx_mark_addressable (function))
343 return error_mark_node;
345 addr = build1 (ADDR_EXPR, type, function);
347 /* Address of a static or external variable or function counts
349 if (staticp (function))
350 TREE_CONSTANT (addr) = 1;
355 function = default_conversion (function);
360 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
361 POINTER_TYPE to those. Note, pointer to member function types
362 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
365 build_call (function, parms)
366 tree function, parms;
368 int is_constructor = 0;
374 function = build_addr_func (function);
376 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
378 sorry ("unable to call pointer to member function here");
379 return error_mark_node;
382 result_type = TREE_TYPE (TREE_TYPE (TREE_TYPE (function)));
384 if (TREE_CODE (function) == ADDR_EXPR
385 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
386 decl = TREE_OPERAND (function, 0);
390 /* We check both the decl and the type; a function may be known not to
391 throw without being declared throw(). */
392 nothrow = ((decl && TREE_NOTHROW (decl))
393 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
395 if (decl && TREE_THIS_VOLATILE (decl))
396 current_function_returns_abnormally = 1;
398 if (decl && TREE_DEPRECATED (decl))
399 warn_deprecated_use (decl);
401 if (decl && DECL_CONSTRUCTOR_P (decl))
404 if (decl && ! TREE_USED (decl))
406 /* We invoke build_call directly for several library functions.
407 These may have been declared normally if we're building libgcc,
408 so we can't just check DECL_ARTIFICIAL. */
409 if (DECL_ARTIFICIAL (decl)
410 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
416 /* Don't pass empty class objects by value. This is useful
417 for tags in STL, which are used to control overload resolution.
418 We don't need to handle other cases of copying empty classes. */
419 if (! decl || ! DECL_BUILT_IN (decl))
420 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
421 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
422 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
424 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
425 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
426 TREE_VALUE (tmp), t);
429 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
430 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
431 TREE_TYPE (function) = result_type;
432 TREE_SIDE_EFFECTS (function) = 1;
433 TREE_NOTHROW (function) = nothrow;
438 /* Build something of the form ptr->method (args)
439 or object.method (args). This can also build
440 calls to constructors, and find friends.
442 Member functions always take their class variable
445 INSTANCE is a class instance.
447 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
449 PARMS help to figure out what that NAME really refers to.
451 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
452 down to the real instance type to use for access checking. We need this
453 information to get protected accesses correct. This parameter is used
454 by build_member_call.
456 FLAGS is the logical disjunction of zero or more LOOKUP_
457 flags. See cp-tree.h for more info.
459 If this is all OK, calls build_function_call with the resolved
462 This function must also handle being called to perform
463 initialization, promotion/coercion of arguments, and
464 instantiation of default parameters.
466 Note that NAME may refer to an instance variable name. If
467 `operator()()' is defined for the type of that field, then we return
470 #ifdef GATHER_STATISTICS
471 extern int n_build_method_call;
475 build_method_call (instance, name, parms, basetype_path, flags)
476 tree instance, name, parms, basetype_path;
479 tree basetype, instance_ptr;
481 #ifdef GATHER_STATISTICS
482 n_build_method_call++;
485 if (instance == error_mark_node
486 || name == error_mark_node
487 || parms == error_mark_node
488 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
489 return error_mark_node;
491 if (processing_template_decl)
493 /* We need to process template parm names here so that tsubst catches
494 them properly. Other type names can wait. */
495 if (TREE_CODE (name) == BIT_NOT_EXPR)
497 tree type = NULL_TREE;
499 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
500 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
501 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
502 type = TREE_TYPE (TREE_OPERAND (name, 0));
504 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
505 name = build_min_nt (BIT_NOT_EXPR, type);
508 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
511 if (TREE_CODE (name) == BIT_NOT_EXPR)
514 error ("destructors take no parameters");
515 basetype = TREE_TYPE (instance);
516 if (TREE_CODE (basetype) == REFERENCE_TYPE)
517 basetype = TREE_TYPE (basetype);
519 if (! check_dtor_name (basetype, name))
521 ("destructor name `~%T' does not match type `%T' of expression",
522 TREE_OPERAND (name, 0), basetype);
524 if (! TYPE_HAS_DESTRUCTOR (complete_type (basetype)))
525 return cp_convert (void_type_node, instance);
526 instance = default_conversion (instance);
527 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
528 return build_delete (build_pointer_type (basetype),
529 instance_ptr, sfk_complete_destructor,
530 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
533 return build_new_method_call (instance, name, parms, basetype_path, flags);
536 /* New overloading code. */
538 struct z_candidate GTY(()) {
546 struct z_candidate *next;
549 #define IDENTITY_RANK 0
555 #define ELLIPSIS_RANK 6
558 #define ICS_RANK(NODE) \
559 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
560 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
561 : ICS_USER_FLAG (NODE) ? USER_RANK \
562 : ICS_STD_RANK (NODE))
564 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
566 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
567 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
568 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
569 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
571 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
572 should be created to hold the result of the conversion. */
573 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
575 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
576 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
584 A null pointer constant is an integral constant expression
585 (_expr.const_) rvalue of integer type that evaluates to zero. */
587 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
593 /* Returns non-zero if PARMLIST consists of only default parms and/or
597 sufficient_parms_p (parmlist)
600 for (; parmlist && parmlist != void_list_node;
601 parmlist = TREE_CHAIN (parmlist))
602 if (!TREE_PURPOSE (parmlist))
608 build_conv (code, type, from)
613 int rank = ICS_STD_RANK (from);
615 /* We can't use buildl1 here because CODE could be USER_CONV, which
616 takes two arguments. In that case, the caller is responsible for
617 filling in the second argument. */
618 t = make_node (code);
619 TREE_TYPE (t) = type;
620 TREE_OPERAND (t, 0) = from;
633 if (rank < EXACT_RANK)
639 ICS_STD_RANK (t) = rank;
640 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
641 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
645 /* If T is a REFERENCE_TYPE return the type to which T refers.
646 Otherwise, return T itself. */
652 if (TREE_CODE (t) == REFERENCE_TYPE)
661 if (TREE_CODE (t) == ARRAY_TYPE)
663 return TYPE_MAIN_VARIANT (t);
666 /* Returns the standard conversion path (see [conv]) from type FROM to type
667 TO, if any. For proper handling of null pointer constants, you must
668 also pass the expression EXPR to convert from. */
671 standard_conversion (to, from, expr)
674 enum tree_code fcode, tcode;
678 if (TREE_CODE (to) == REFERENCE_TYPE)
680 if (TREE_CODE (from) == REFERENCE_TYPE)
683 from = TREE_TYPE (from);
685 to = strip_top_quals (to);
686 from = strip_top_quals (from);
688 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
689 && expr && type_unknown_p (expr))
691 expr = instantiate_type (to, expr, tf_none);
692 if (expr == error_mark_node)
694 from = TREE_TYPE (expr);
697 fcode = TREE_CODE (from);
698 tcode = TREE_CODE (to);
700 conv = build1 (IDENTITY_CONV, from, expr);
702 if (fcode == FUNCTION_TYPE)
704 from = build_pointer_type (from);
705 fcode = TREE_CODE (from);
706 conv = build_conv (LVALUE_CONV, from, conv);
708 else if (fcode == ARRAY_TYPE)
710 from = build_pointer_type (TREE_TYPE (from));
711 fcode = TREE_CODE (from);
712 conv = build_conv (LVALUE_CONV, from, conv);
714 else if (fromref || (expr && lvalue_p (expr)))
715 conv = build_conv (RVALUE_CONV, from, conv);
717 /* Allow conversion between `__complex__' data types */
718 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
720 /* The standard conversion sequence to convert FROM to TO is
721 the standard conversion sequence to perform componentwise
723 tree part_conv = standard_conversion
724 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
728 conv = build_conv (TREE_CODE (part_conv), to, conv);
729 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
737 if (same_type_p (from, to))
740 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
741 && expr && null_ptr_cst_p (expr))
743 conv = build_conv (STD_CONV, to, conv);
745 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
746 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
748 /* For backwards brain damage compatibility, allow interconversion of
749 pointers and integers with a pedwarn. */
750 conv = build_conv (STD_CONV, to, conv);
751 ICS_BAD_FLAG (conv) = 1;
753 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
754 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
756 /* For backwards brain damage compatibility, allow interconversion of
757 enums and integers with a pedwarn. */
758 conv = build_conv (STD_CONV, to, conv);
759 ICS_BAD_FLAG (conv) = 1;
761 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
763 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
764 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
766 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
769 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
770 && ufcode != FUNCTION_TYPE)
772 from = build_pointer_type
773 (cp_build_qualified_type (void_type_node,
774 cp_type_quals (TREE_TYPE (from))));
775 conv = build_conv (PTR_CONV, from, conv);
777 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
779 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
780 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
782 if (DERIVED_FROM_P (fbase, tbase)
783 && (same_type_ignoring_top_level_qualifiers_p
784 (TREE_TYPE (TREE_TYPE (from)),
785 TREE_TYPE (TREE_TYPE (to)))))
787 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
788 from = build_pointer_type (from);
789 conv = build_conv (PMEM_CONV, from, conv);
792 else if (IS_AGGR_TYPE (TREE_TYPE (from))
793 && IS_AGGR_TYPE (TREE_TYPE (to)))
795 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
798 cp_build_qualified_type (TREE_TYPE (to),
799 cp_type_quals (TREE_TYPE (from)));
800 from = build_pointer_type (from);
801 conv = build_conv (PTR_CONV, from, conv);
805 if (same_type_p (from, to))
807 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
808 conv = build_conv (QUAL_CONV, to, conv);
809 else if (expr && string_conv_p (to, expr, 0))
810 /* converting from string constant to char *. */
811 conv = build_conv (QUAL_CONV, to, conv);
812 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
814 conv = build_conv (PTR_CONV, to, conv);
815 ICS_BAD_FLAG (conv) = 1;
822 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
824 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
825 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
826 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
827 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
829 if (!DERIVED_FROM_P (fbase, tbase)
830 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
831 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
832 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
833 || cp_type_quals (fbase) != cp_type_quals (tbase))
836 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
837 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
838 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
839 from = build_ptrmemfunc_type (build_pointer_type (from));
840 conv = build_conv (PMEM_CONV, from, conv);
842 else if (tcode == BOOLEAN_TYPE)
844 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
845 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
848 conv = build_conv (STD_CONV, to, conv);
849 if (fcode == POINTER_TYPE
850 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
851 ICS_STD_RANK (conv) = PBOOL_RANK;
853 /* We don't check for ENUMERAL_TYPE here because there are no standard
854 conversions to enum type. */
855 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
856 || tcode == REAL_TYPE)
858 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
860 conv = build_conv (STD_CONV, to, conv);
862 /* Give this a better rank if it's a promotion. */
863 if (to == type_promotes_to (from)
864 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
865 ICS_STD_RANK (conv) = PROMO_RANK;
867 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
868 && is_properly_derived_from (from, to))
870 if (TREE_CODE (conv) == RVALUE_CONV)
871 conv = TREE_OPERAND (conv, 0);
872 conv = build_conv (BASE_CONV, to, conv);
873 /* The derived-to-base conversion indicates the initialization
874 of a parameter with base type from an object of a derived
875 type. A temporary object is created to hold the result of
877 NEED_TEMPORARY_P (conv) = 1;
885 /* Returns non-zero if T1 is reference-related to T2. */
888 reference_related_p (t1, t2)
892 t1 = TYPE_MAIN_VARIANT (t1);
893 t2 = TYPE_MAIN_VARIANT (t2);
897 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
898 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
900 return (same_type_p (t1, t2)
901 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
902 && DERIVED_FROM_P (t1, t2)));
905 /* Returns non-zero if T1 is reference-compatible with T2. */
908 reference_compatible_p (t1, t2)
914 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
915 reference-related to T2 and cv1 is the same cv-qualification as,
916 or greater cv-qualification than, cv2. */
917 return (reference_related_p (t1, t2)
918 && at_least_as_qualified_p (t1, t2));
921 /* Determine whether or not the EXPR (of class type S) can be
922 converted to T as in [over.match.ref]. */
925 convert_class_to_reference (t, s, expr)
933 struct z_candidate *candidates;
934 struct z_candidate *cand;
938 Assuming that "cv1 T" is the underlying type of the reference
939 being initialized, and "cv S" is the type of the initializer
940 expression, with S a class type, the candidate functions are
943 --The conversion functions of S and its base classes are
944 considered. Those that are not hidden within S and yield type
945 "reference to cv2 T2", where "cv1 T" is reference-compatible
946 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
948 The argument list has one argument, which is the initializer
953 /* Conceptually, we should take the address of EXPR and put it in
954 the argument list. Unfortunately, however, that can result in
955 error messages, which we should not issue now because we are just
956 trying to find a conversion operator. Therefore, we use NULL,
957 cast to the appropriate type. */
958 arglist = build_int_2 (0, 0);
959 TREE_TYPE (arglist) = build_pointer_type (s);
960 arglist = build_tree_list (NULL_TREE, arglist);
962 for (conversions = lookup_conversions (s);
964 conversions = TREE_CHAIN (conversions))
966 tree fns = TREE_VALUE (conversions);
968 for (; fns; fns = OVL_NEXT (fns))
970 tree f = OVL_CURRENT (fns);
971 tree t2 = TREE_TYPE (TREE_TYPE (f));
972 struct z_candidate *old_candidates = candidates;
974 /* If this is a template function, try to get an exact
976 if (TREE_CODE (f) == TEMPLATE_DECL)
979 = add_template_candidate (candidates,
983 build_reference_type (t),
987 if (candidates != old_candidates)
989 /* Now, see if the conversion function really returns
990 an lvalue of the appropriate type. From the
991 point of view of unification, simply returning an
992 rvalue of the right type is good enough. */
994 t2 = TREE_TYPE (TREE_TYPE (f));
995 if (TREE_CODE (t2) != REFERENCE_TYPE
996 || !reference_compatible_p (t, TREE_TYPE (t2)))
997 candidates = candidates->next;
1000 else if (TREE_CODE (t2) == REFERENCE_TYPE
1001 && reference_compatible_p (t, TREE_TYPE (t2)))
1003 = add_function_candidate (candidates, f, s, arglist,
1006 if (candidates != old_candidates)
1007 candidates->basetype_path = TYPE_BINFO (s);
1011 /* If none of the conversion functions worked out, let our caller
1013 if (!any_viable (candidates))
1016 candidates = splice_viable (candidates);
1017 cand = tourney (candidates);
1021 conv = build1 (IDENTITY_CONV, s, expr);
1022 conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1024 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
1025 ICS_USER_FLAG (conv) = 1;
1026 if (cand->viable == -1)
1027 ICS_BAD_FLAG (conv) = 1;
1028 cand->second_conv = conv;
1033 /* A reference of the indicated TYPE is being bound directly to the
1034 expression represented by the implicit conversion sequence CONV.
1035 Return a conversion sequence for this binding. */
1038 direct_reference_binding (type, conv)
1042 tree t = TREE_TYPE (type);
1046 When a parameter of reference type binds directly
1047 (_dcl.init.ref_) to an argument expression, the implicit
1048 conversion sequence is the identity conversion, unless the
1049 argument expression has a type that is a derived class of the
1050 parameter type, in which case the implicit conversion sequence is
1051 a derived-to-base Conversion.
1053 If the parameter binds directly to the result of applying a
1054 conversion function to the argument expression, the implicit
1055 conversion sequence is a user-defined conversion sequence
1056 (_over.ics.user_), with the second standard conversion sequence
1057 either an identity conversion or, if the conversion function
1058 returns an entity of a type that is a derived class of the
1059 parameter type, a derived-to-base conversion. */
1060 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1062 /* Represent the derived-to-base conversion. */
1063 conv = build_conv (BASE_CONV, t, conv);
1064 /* We will actually be binding to the base-class subobject in
1065 the derived class, so we mark this conversion appropriately.
1066 That way, convert_like knows not to generate a temporary. */
1067 NEED_TEMPORARY_P (conv) = 0;
1069 return build_conv (REF_BIND, type, conv);
1072 /* Returns the conversion path from type FROM to reference type TO for
1073 purposes of reference binding. For lvalue binding, either pass a
1074 reference type to FROM or an lvalue expression to EXPR. If the
1075 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1076 the conversion returned. */
1079 reference_binding (rto, rfrom, expr, flags)
1080 tree rto, rfrom, expr;
1083 tree conv = NULL_TREE;
1084 tree to = TREE_TYPE (rto);
1088 cp_lvalue_kind lvalue_p = clk_none;
1090 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1092 expr = instantiate_type (to, expr, tf_none);
1093 if (expr == error_mark_node)
1095 from = TREE_TYPE (expr);
1098 if (TREE_CODE (from) == REFERENCE_TYPE)
1100 /* Anything with reference type is an lvalue. */
1101 lvalue_p = clk_ordinary;
1102 from = TREE_TYPE (from);
1105 lvalue_p = real_lvalue_p (expr);
1107 /* Figure out whether or not the types are reference-related and
1108 reference compatible. We have do do this after stripping
1109 references from FROM. */
1110 related_p = reference_related_p (to, from);
1111 compatible_p = reference_compatible_p (to, from);
1113 if (lvalue_p && compatible_p)
1117 If the initializer expression
1119 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1120 is reference-compatible with "cv2 T2,"
1122 the reference is bound directly to the initializer exprssion
1124 conv = build1 (IDENTITY_CONV, from, expr);
1125 conv = direct_reference_binding (rto, conv);
1126 if ((lvalue_p & clk_bitfield) != 0
1127 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1128 /* For the purposes of overload resolution, we ignore the fact
1129 this expression is a bitfield. (In particular,
1130 [over.ics.ref] says specifically that a function with a
1131 non-const reference parameter is viable even if the
1132 argument is a bitfield.)
1134 However, when we actually call the function we must create
1135 a temporary to which to bind the reference. If the
1136 reference is volatile, or isn't const, then we cannot make
1137 a temporary, so we just issue an error when the conversion
1139 NEED_TEMPORARY_P (conv) = 1;
1142 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1146 If the initializer exprsesion
1148 -- has a class type (i.e., T2 is a class type) can be
1149 implicitly converted to an lvalue of type "cv3 T3," where
1150 "cv1 T1" is reference-compatible with "cv3 T3". (this
1151 conversion is selected by enumerating the applicable
1152 conversion functions (_over.match.ref_) and choosing the
1153 best one through overload resolution. (_over.match_).
1155 the reference is bound to the lvalue result of the conversion
1156 in the second case. */
1157 conv = convert_class_to_reference (to, from, expr);
1159 return direct_reference_binding (rto, conv);
1162 /* From this point on, we conceptually need temporaries, even if we
1163 elide them. Only the cases above are "direct bindings". */
1164 if (flags & LOOKUP_NO_TEMP_BIND)
1169 When a parameter of reference type is not bound directly to an
1170 argument expression, the conversion sequence is the one required
1171 to convert the argument expression to the underlying type of the
1172 reference according to _over.best.ics_. Conceptually, this
1173 conversion sequence corresponds to copy-initializing a temporary
1174 of the underlying type with the argument expression. Any
1175 difference in top-level cv-qualification is subsumed by the
1176 initialization itself and does not constitute a conversion. */
1180 Otherwise, the reference shall be to a non-volatile const type. */
1181 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1186 If the initializer expression is an rvalue, with T2 a class type,
1187 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1188 is bound in one of the following ways:
1190 -- The reference is bound to the object represented by the rvalue
1191 or to a sub-object within that object.
1193 In this case, the implicit conversion sequence is supposed to be
1194 same as we would obtain by generating a temporary. Fortunately,
1195 if the types are reference compatible, then this is either an
1196 identity conversion or the derived-to-base conversion, just as
1197 for direct binding. */
1198 if (CLASS_TYPE_P (from) && compatible_p)
1200 conv = build1 (IDENTITY_CONV, from, expr);
1201 return direct_reference_binding (rto, conv);
1206 Otherwise, a temporary of type "cv1 T1" is created and
1207 initialized from the initializer expression using the rules for a
1208 non-reference copy initialization. If T1 is reference-related to
1209 T2, cv1 must be the same cv-qualification as, or greater
1210 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1211 if (related_p && !at_least_as_qualified_p (to, from))
1214 conv = implicit_conversion (to, from, expr, flags);
1218 conv = build_conv (REF_BIND, rto, conv);
1219 /* This reference binding, unlike those above, requires the
1220 creation of a temporary. */
1221 NEED_TEMPORARY_P (conv) = 1;
1226 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1227 to type TO. The optional expression EXPR may affect the conversion.
1228 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1232 implicit_conversion (to, from, expr, flags)
1233 tree to, from, expr;
1237 struct z_candidate *cand;
1239 /* Resolve expressions like `A::p' that we thought might become
1240 pointers-to-members. */
1241 if (expr && TREE_CODE (expr) == OFFSET_REF)
1243 expr = resolve_offset_ref (expr);
1244 from = TREE_TYPE (expr);
1247 if (from == error_mark_node || to == error_mark_node
1248 || expr == error_mark_node)
1251 /* Make sure both the FROM and TO types are complete so that
1252 user-defined conversions are available. */
1253 complete_type (from);
1256 if (TREE_CODE (to) == REFERENCE_TYPE)
1257 conv = reference_binding (to, from, expr, flags);
1259 conv = standard_conversion (to, from, expr);
1263 else if (expr != NULL_TREE
1264 && (IS_AGGR_TYPE (from)
1265 || IS_AGGR_TYPE (to))
1266 && (flags & LOOKUP_NO_CONVERSION) == 0)
1268 cand = build_user_type_conversion_1
1269 (to, expr, LOOKUP_ONLYCONVERTING);
1271 conv = cand->second_conv;
1273 /* We used to try to bind a reference to a temporary here, but that
1274 is now handled by the recursive call to this function at the end
1275 of reference_binding. */
1281 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1284 static struct z_candidate *
1285 add_candidate (candidates, fn, convs, viable)
1286 struct z_candidate *candidates;
1290 struct z_candidate *cand
1291 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1294 cand->convs = convs;
1295 cand->viable = viable;
1296 cand->next = candidates;
1301 /* Create an overload candidate for the function or method FN called with
1302 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1303 to implicit_conversion.
1305 CTYPE, if non-NULL, is the type we want to pretend this function
1306 comes from for purposes of overload resolution. */
1308 static struct z_candidate *
1309 add_function_candidate (candidates, fn, ctype, arglist, flags)
1310 struct z_candidate *candidates;
1311 tree fn, ctype, arglist;
1314 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1317 tree parmnode, argnode;
1320 /* The `this', `in_chrg' and VTT arguments to constructors are not
1321 considered in overload resolution. */
1322 if (DECL_CONSTRUCTOR_P (fn))
1324 parmlist = skip_artificial_parms_for (fn, parmlist);
1325 arglist = skip_artificial_parms_for (fn, arglist);
1328 len = list_length (arglist);
1329 convs = make_tree_vec (len);
1331 /* 13.3.2 - Viable functions [over.match.viable]
1332 First, to be a viable function, a candidate function shall have enough
1333 parameters to agree in number with the arguments in the list.
1335 We need to check this first; otherwise, checking the ICSes might cause
1336 us to produce an ill-formed template instantiation. */
1338 parmnode = parmlist;
1339 for (i = 0; i < len; ++i)
1341 if (parmnode == NULL_TREE || parmnode == void_list_node)
1343 parmnode = TREE_CHAIN (parmnode);
1346 if (i < len && parmnode)
1349 /* Make sure there are default args for the rest of the parms. */
1350 else if (!sufficient_parms_p (parmnode))
1356 /* Second, for F to be a viable function, there shall exist for each
1357 argument an implicit conversion sequence that converts that argument
1358 to the corresponding parameter of F. */
1360 parmnode = parmlist;
1363 for (i = 0; i < len; ++i)
1365 tree arg = TREE_VALUE (argnode);
1366 tree argtype = lvalue_type (arg);
1370 if (parmnode == void_list_node)
1373 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1374 && ! DECL_CONSTRUCTOR_P (fn));
1378 tree parmtype = TREE_VALUE (parmnode);
1380 /* The type of the implicit object parameter ('this') for
1381 overload resolution is not always the same as for the
1382 function itself; conversion functions are considered to
1383 be members of the class being converted, and functions
1384 introduced by a using-declaration are considered to be
1385 members of the class that uses them.
1387 Since build_over_call ignores the ICS for the `this'
1388 parameter, we can just change the parm type. */
1389 if (ctype && is_this)
1392 = build_qualified_type (ctype,
1393 TYPE_QUALS (TREE_TYPE (parmtype)));
1394 parmtype = build_pointer_type (parmtype);
1397 t = implicit_conversion (parmtype, argtype, arg, flags);
1401 t = build1 (IDENTITY_CONV, argtype, arg);
1402 ICS_ELLIPSIS_FLAG (t) = 1;
1406 ICS_THIS_FLAG (t) = 1;
1408 TREE_VEC_ELT (convs, i) = t;
1415 if (ICS_BAD_FLAG (t))
1419 parmnode = TREE_CHAIN (parmnode);
1420 argnode = TREE_CHAIN (argnode);
1424 return add_candidate (candidates, fn, convs, viable);
1427 /* Create an overload candidate for the conversion function FN which will
1428 be invoked for expression OBJ, producing a pointer-to-function which
1429 will in turn be called with the argument list ARGLIST, and add it to
1430 CANDIDATES. FLAGS is passed on to implicit_conversion.
1432 Actually, we don't really care about FN; we care about the type it
1433 converts to. There may be multiple conversion functions that will
1434 convert to that type, and we rely on build_user_type_conversion_1 to
1435 choose the best one; so when we create our candidate, we record the type
1436 instead of the function. */
1438 static struct z_candidate *
1439 add_conv_candidate (candidates, fn, obj, arglist)
1440 struct z_candidate *candidates;
1441 tree fn, obj, arglist;
1443 tree totype = TREE_TYPE (TREE_TYPE (fn));
1444 int i, len, viable, flags;
1445 tree parmlist, convs, parmnode, argnode;
1447 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1448 parmlist = TREE_TYPE (parmlist);
1449 parmlist = TYPE_ARG_TYPES (parmlist);
1451 len = list_length (arglist) + 1;
1452 convs = make_tree_vec (len);
1453 parmnode = parmlist;
1456 flags = LOOKUP_NORMAL;
1458 /* Don't bother looking up the same type twice. */
1459 if (candidates && candidates->fn == totype)
1462 for (i = 0; i < len; ++i)
1464 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1465 tree argtype = lvalue_type (arg);
1469 t = implicit_conversion (totype, argtype, arg, flags);
1470 else if (parmnode == void_list_node)
1473 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1476 t = build1 (IDENTITY_CONV, argtype, arg);
1477 ICS_ELLIPSIS_FLAG (t) = 1;
1480 TREE_VEC_ELT (convs, i) = t;
1484 if (ICS_BAD_FLAG (t))
1491 parmnode = TREE_CHAIN (parmnode);
1492 argnode = TREE_CHAIN (argnode);
1498 if (!sufficient_parms_p (parmnode))
1501 return add_candidate (candidates, totype, convs, viable);
1504 static struct z_candidate *
1505 build_builtin_candidate (candidates, fnname, type1, type2,
1506 args, argtypes, flags)
1507 struct z_candidate *candidates;
1508 tree fnname, type1, type2, *args, *argtypes;
1519 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1521 for (i = 0; i < 2; ++i)
1526 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1530 /* We need something for printing the candidate. */
1531 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1533 else if (ICS_BAD_FLAG (t))
1535 TREE_VEC_ELT (convs, i) = t;
1538 /* For COND_EXPR we rearranged the arguments; undo that now. */
1541 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1542 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1543 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1545 TREE_VEC_ELT (convs, 0) = t;
1550 return add_candidate (candidates, fnname, convs, viable);
1557 return COMPLETE_TYPE_P (complete_type (t));
1560 /* Returns non-zero if TYPE is a promoted arithmetic type. */
1563 promoted_arithmetic_type_p (type)
1568 In this section, the term promoted integral type is used to refer
1569 to those integral types which are preserved by integral promotion
1570 (including e.g. int and long but excluding e.g. char).
1571 Similarly, the term promoted arithmetic type refers to promoted
1572 integral types plus floating types. */
1573 return ((INTEGRAL_TYPE_P (type)
1574 && same_type_p (type_promotes_to (type), type))
1575 || TREE_CODE (type) == REAL_TYPE);
1578 /* Create any builtin operator overload candidates for the operator in
1579 question given the converted operand types TYPE1 and TYPE2. The other
1580 args are passed through from add_builtin_candidates to
1581 build_builtin_candidate.
1583 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1584 If CODE is requires candidates operands of the same type of the kind
1585 of which TYPE1 and TYPE2 are, we add both candidates
1586 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1588 static struct z_candidate *
1589 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1590 args, argtypes, flags)
1591 struct z_candidate *candidates;
1592 enum tree_code code, code2;
1593 tree fnname, type1, type2, *args, *argtypes;
1598 case POSTINCREMENT_EXPR:
1599 case POSTDECREMENT_EXPR:
1600 args[1] = integer_zero_node;
1601 type2 = integer_type_node;
1610 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1611 and VQ is either volatile or empty, there exist candidate operator
1612 functions of the form
1613 VQ T& operator++(VQ T&);
1614 T operator++(VQ T&, int);
1615 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1616 type other than bool, and VQ is either volatile or empty, there exist
1617 candidate operator functions of the form
1618 VQ T& operator--(VQ T&);
1619 T operator--(VQ T&, int);
1620 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1621 complete object type, and VQ is either volatile or empty, there exist
1622 candidate operator functions of the form
1623 T*VQ& operator++(T*VQ&);
1624 T*VQ& operator--(T*VQ&);
1625 T* operator++(T*VQ&, int);
1626 T* operator--(T*VQ&, int); */
1628 case POSTDECREMENT_EXPR:
1629 case PREDECREMENT_EXPR:
1630 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1632 case POSTINCREMENT_EXPR:
1633 case PREINCREMENT_EXPR:
1634 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1636 type1 = build_reference_type (type1);
1641 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1642 exist candidate operator functions of the form
1646 8 For every function type T, there exist candidate operator functions of
1648 T& operator*(T*); */
1651 if (TREE_CODE (type1) == POINTER_TYPE
1652 && (TYPE_PTROB_P (type1)
1653 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1657 /* 9 For every type T, there exist candidate operator functions of the form
1660 10For every promoted arithmetic type T, there exist candidate operator
1661 functions of the form
1665 case CONVERT_EXPR: /* unary + */
1666 if (TREE_CODE (type1) == POINTER_TYPE
1667 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1670 if (ARITHMETIC_TYPE_P (type1))
1674 /* 11For every promoted integral type T, there exist candidate operator
1675 functions of the form
1679 if (INTEGRAL_TYPE_P (type1))
1683 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1684 is the same type as C2 or is a derived class of C2, T is a complete
1685 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1686 there exist candidate operator functions of the form
1687 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1688 where CV12 is the union of CV1 and CV2. */
1691 if (TREE_CODE (type1) == POINTER_TYPE
1692 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1694 tree c1 = TREE_TYPE (type1);
1695 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1696 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1697 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1699 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1700 && (TYPE_PTRMEMFUNC_P (type2)
1701 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1706 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1707 didate operator functions of the form
1712 bool operator<(L, R);
1713 bool operator>(L, R);
1714 bool operator<=(L, R);
1715 bool operator>=(L, R);
1716 bool operator==(L, R);
1717 bool operator!=(L, R);
1718 where LR is the result of the usual arithmetic conversions between
1721 14For every pair of types T and I, where T is a cv-qualified or cv-
1722 unqualified complete object type and I is a promoted integral type,
1723 there exist candidate operator functions of the form
1724 T* operator+(T*, I);
1725 T& operator[](T*, I);
1726 T* operator-(T*, I);
1727 T* operator+(I, T*);
1728 T& operator[](I, T*);
1730 15For every T, where T is a pointer to complete object type, there exist
1731 candidate operator functions of the form112)
1732 ptrdiff_t operator-(T, T);
1734 16For every pointer or enumeration type T, there exist candidate operator
1735 functions of the form
1736 bool operator<(T, T);
1737 bool operator>(T, T);
1738 bool operator<=(T, T);
1739 bool operator>=(T, T);
1740 bool operator==(T, T);
1741 bool operator!=(T, T);
1743 17For every pointer to member type T, there exist candidate operator
1744 functions of the form
1745 bool operator==(T, T);
1746 bool operator!=(T, T); */
1749 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1751 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1753 type2 = ptrdiff_type_node;
1757 case TRUNC_DIV_EXPR:
1758 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1764 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1765 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1767 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1768 && null_ptr_cst_p (args[1]))
1773 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1774 && null_ptr_cst_p (args[0]))
1786 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1788 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1790 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1792 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1797 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1805 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1808 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1810 type1 = ptrdiff_type_node;
1813 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1815 type2 = ptrdiff_type_node;
1820 /* 18For every pair of promoted integral types L and R, there exist candi-
1821 date operator functions of the form
1828 where LR is the result of the usual arithmetic conversions between
1831 case TRUNC_MOD_EXPR:
1837 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1841 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1842 type, VQ is either volatile or empty, and R is a promoted arithmetic
1843 type, there exist candidate operator functions of the form
1844 VQ L& operator=(VQ L&, R);
1845 VQ L& operator*=(VQ L&, R);
1846 VQ L& operator/=(VQ L&, R);
1847 VQ L& operator+=(VQ L&, R);
1848 VQ L& operator-=(VQ L&, R);
1850 20For every pair T, VQ), where T is any type and VQ is either volatile
1851 or empty, there exist candidate operator functions of the form
1852 T*VQ& operator=(T*VQ&, T*);
1854 21For every pair T, VQ), where T is a pointer to member type and VQ is
1855 either volatile or empty, there exist candidate operator functions of
1857 VQ T& operator=(VQ T&, T);
1859 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1860 unqualified complete object type, VQ is either volatile or empty, and
1861 I is a promoted integral type, there exist candidate operator func-
1863 T*VQ& operator+=(T*VQ&, I);
1864 T*VQ& operator-=(T*VQ&, I);
1866 23For every triple L, VQ, R), where L is an integral or enumeration
1867 type, VQ is either volatile or empty, and R is a promoted integral
1868 type, there exist candidate operator functions of the form
1870 VQ L& operator%=(VQ L&, R);
1871 VQ L& operator<<=(VQ L&, R);
1872 VQ L& operator>>=(VQ L&, R);
1873 VQ L& operator&=(VQ L&, R);
1874 VQ L& operator^=(VQ L&, R);
1875 VQ L& operator|=(VQ L&, R); */
1882 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1884 type2 = ptrdiff_type_node;
1888 case TRUNC_DIV_EXPR:
1889 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1893 case TRUNC_MOD_EXPR:
1899 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1904 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1906 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1907 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1908 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1909 || ((TYPE_PTRMEMFUNC_P (type1)
1910 || TREE_CODE (type1) == POINTER_TYPE)
1911 && null_ptr_cst_p (args[1])))
1921 type1 = build_reference_type (type1);
1927 For every pair of promoted arithmetic types L and R, there
1928 exist candidate operator functions of the form
1930 LR operator?(bool, L, R);
1932 where LR is the result of the usual arithmetic conversions
1933 between types L and R.
1935 For every type T, where T is a pointer or pointer-to-member
1936 type, there exist candidate operator functions of the form T
1937 operator?(bool, T, T); */
1939 if (promoted_arithmetic_type_p (type1)
1940 && promoted_arithmetic_type_p (type2))
1944 /* Otherwise, the types should be pointers. */
1945 if (!(TREE_CODE (type1) == POINTER_TYPE
1946 || TYPE_PTRMEM_P (type1)
1947 || TYPE_PTRMEMFUNC_P (type1))
1948 || !(TREE_CODE (type2) == POINTER_TYPE
1949 || TYPE_PTRMEM_P (type2)
1950 || TYPE_PTRMEMFUNC_P (type2)))
1953 /* We don't check that the two types are the same; the logic
1954 below will actually create two candidates; one in which both
1955 parameter types are TYPE1, and one in which both parameter
1959 /* These arguments do not make for a legal overloaded operator. */
1966 /* If we're dealing with two pointer types or two enumeral types,
1967 we need candidates for both of them. */
1968 if (type2 && !same_type_p (type1, type2)
1969 && TREE_CODE (type1) == TREE_CODE (type2)
1970 && (TREE_CODE (type1) == REFERENCE_TYPE
1971 || (TREE_CODE (type1) == POINTER_TYPE
1972 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1973 || TYPE_PTRMEMFUNC_P (type1)
1974 || IS_AGGR_TYPE (type1)
1975 || TREE_CODE (type1) == ENUMERAL_TYPE))
1977 candidates = build_builtin_candidate
1978 (candidates, fnname, type1, type1, args, argtypes, flags);
1979 return build_builtin_candidate
1980 (candidates, fnname, type2, type2, args, argtypes, flags);
1983 return build_builtin_candidate
1984 (candidates, fnname, type1, type2, args, argtypes, flags);
1988 type_decays_to (type)
1991 if (TREE_CODE (type) == ARRAY_TYPE)
1992 return build_pointer_type (TREE_TYPE (type));
1993 if (TREE_CODE (type) == FUNCTION_TYPE)
1994 return build_pointer_type (type);
1998 /* There are three conditions of builtin candidates:
2000 1) bool-taking candidates. These are the same regardless of the input.
2001 2) pointer-pair taking candidates. These are generated for each type
2002 one of the input types converts to.
2003 3) arithmetic candidates. According to the standard, we should generate
2004 all of these, but I'm trying not to...
2006 Here we generate a superset of the possible candidates for this particular
2007 case. That is a subset of the full set the standard defines, plus some
2008 other cases which the standard disallows. add_builtin_candidate will
2009 filter out the illegal set. */
2011 static struct z_candidate *
2012 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2013 struct z_candidate *candidates;
2014 enum tree_code code, code2;
2020 tree type, argtypes[3];
2021 /* TYPES[i] is the set of possible builtin-operator parameter types
2022 we will consider for the Ith argument. These are represented as
2023 a TREE_LIST; the TREE_VALUE of each node is the potential
2027 for (i = 0; i < 3; ++i)
2030 argtypes[i] = lvalue_type (args[i]);
2032 argtypes[i] = NULL_TREE;
2037 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2038 and VQ is either volatile or empty, there exist candidate operator
2039 functions of the form
2040 VQ T& operator++(VQ T&); */
2042 case POSTINCREMENT_EXPR:
2043 case PREINCREMENT_EXPR:
2044 case POSTDECREMENT_EXPR:
2045 case PREDECREMENT_EXPR:
2050 /* 24There also exist candidate operator functions of the form
2051 bool operator!(bool);
2052 bool operator&&(bool, bool);
2053 bool operator||(bool, bool); */
2055 case TRUTH_NOT_EXPR:
2056 return build_builtin_candidate
2057 (candidates, fnname, boolean_type_node,
2058 NULL_TREE, args, argtypes, flags);
2060 case TRUTH_ORIF_EXPR:
2061 case TRUTH_ANDIF_EXPR:
2062 return build_builtin_candidate
2063 (candidates, fnname, boolean_type_node,
2064 boolean_type_node, args, argtypes, flags);
2085 types[0] = types[1] = NULL_TREE;
2087 for (i = 0; i < 2; ++i)
2091 else if (IS_AGGR_TYPE (argtypes[i]))
2095 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2098 convs = lookup_conversions (argtypes[i]);
2100 if (code == COND_EXPR)
2102 if (real_lvalue_p (args[i]))
2103 types[i] = tree_cons
2104 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2106 types[i] = tree_cons
2107 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2113 for (; convs; convs = TREE_CHAIN (convs))
2115 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2118 && (TREE_CODE (type) != REFERENCE_TYPE
2119 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2122 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2123 types[i] = tree_cons (NULL_TREE, type, types[i]);
2125 type = non_reference (type);
2126 if (i != 0 || ! ref1)
2128 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2129 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2130 types[i] = tree_cons (NULL_TREE, type, types[i]);
2131 if (INTEGRAL_TYPE_P (type))
2132 type = type_promotes_to (type);
2135 if (! value_member (type, types[i]))
2136 types[i] = tree_cons (NULL_TREE, type, types[i]);
2141 if (code == COND_EXPR && real_lvalue_p (args[i]))
2142 types[i] = tree_cons
2143 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2144 type = non_reference (argtypes[i]);
2145 if (i != 0 || ! ref1)
2147 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2148 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2149 types[i] = tree_cons (NULL_TREE, type, types[i]);
2150 if (INTEGRAL_TYPE_P (type))
2151 type = type_promotes_to (type);
2153 types[i] = tree_cons (NULL_TREE, type, types[i]);
2157 /* Run through the possible parameter types of both arguments,
2158 creating candidates with those parameter types. */
2159 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2162 for (type = types[1]; type; type = TREE_CHAIN (type))
2163 candidates = add_builtin_candidate
2164 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2165 TREE_VALUE (type), args, argtypes, flags);
2167 candidates = add_builtin_candidate
2168 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2169 NULL_TREE, args, argtypes, flags);
2176 /* If TMPL can be successfully instantiated as indicated by
2177 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2179 TMPL is the template. EXPLICIT_TARGS are any explicit template
2180 arguments. ARGLIST is the arguments provided at the call-site.
2181 The RETURN_TYPE is the desired type for conversion operators. If
2182 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2183 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2184 add_conv_candidate. */
2186 static struct z_candidate*
2187 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2188 arglist, return_type, flags,
2190 struct z_candidate *candidates;
2191 tree tmpl, ctype, explicit_targs, arglist, return_type;
2194 unification_kind_t strict;
2196 int ntparms = DECL_NTPARMS (tmpl);
2197 tree targs = make_tree_vec (ntparms);
2198 tree args_without_in_chrg = arglist;
2199 struct z_candidate *cand;
2203 /* We don't do deduction on the in-charge parameter, the VTT
2204 parameter or 'this'. */
2205 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2206 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2208 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2209 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2210 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2211 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2213 i = fn_type_unification (tmpl, explicit_targs, targs,
2214 args_without_in_chrg,
2215 return_type, strict, -1);
2220 fn = instantiate_template (tmpl, targs);
2221 if (fn == error_mark_node)
2226 A member function template is never instantiated to perform the
2227 copy of a class object to an object of its class type.
2229 It's a little unclear what this means; the standard explicitly
2230 does allow a template to be used to copy a class. For example,
2235 template <class T> A(const T&);
2238 void g () { A a (f ()); }
2240 the member template will be used to make the copy. The section
2241 quoted above appears in the paragraph that forbids constructors
2242 whose only parameter is (a possibly cv-qualified variant of) the
2243 class type, and a logical interpretation is that the intent was
2244 to forbid the instantiation of member templates which would then
2246 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2248 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2249 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2254 if (obj != NULL_TREE)
2255 /* Aha, this is a conversion function. */
2256 cand = add_conv_candidate (candidates, fn, obj, arglist);
2258 cand = add_function_candidate (candidates, fn, ctype,
2260 if (DECL_TI_TEMPLATE (fn) != tmpl)
2261 /* This situation can occur if a member template of a template
2262 class is specialized. Then, instantiate_template might return
2263 an instantiation of the specialization, in which case the
2264 DECL_TI_TEMPLATE field will point at the original
2265 specialization. For example:
2267 template <class T> struct S { template <class U> void f(U);
2268 template <> void f(int) {}; };
2272 Here, TMPL will be template <class U> S<double>::f(U).
2273 And, instantiate template will give us the specialization
2274 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2275 for this will point at template <class T> template <> S<T>::f(int),
2276 so that we can find the definition. For the purposes of
2277 overload resolution, however, we want the original TMPL. */
2278 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2280 cand->template = DECL_TEMPLATE_INFO (fn);
2286 static struct z_candidate *
2287 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2288 arglist, return_type, flags, strict)
2289 struct z_candidate *candidates;
2290 tree tmpl, ctype, explicit_targs, arglist, return_type;
2292 unification_kind_t strict;
2295 add_template_candidate_real (candidates, tmpl, ctype,
2296 explicit_targs, arglist, return_type, flags,
2301 static struct z_candidate *
2302 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type)
2303 struct z_candidate *candidates;
2304 tree tmpl, obj, arglist, return_type;
2307 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2308 arglist, return_type, 0, obj, DEDUCE_CONV);
2314 struct z_candidate *cands;
2316 for (; cands; cands = cands->next)
2317 if (pedantic ? cands->viable == 1 : cands->viable)
2323 any_strictly_viable (cands)
2324 struct z_candidate *cands;
2326 for (; cands; cands = cands->next)
2327 if (cands->viable == 1)
2332 static struct z_candidate *
2333 splice_viable (cands)
2334 struct z_candidate *cands;
2336 struct z_candidate **p = &cands;
2340 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2353 /* Fix this to work on non-lvalues. */
2354 return build_unary_op (ADDR_EXPR, obj, 0);
2358 print_z_candidates (candidates)
2359 struct z_candidate *candidates;
2361 const char *str = "candidates are:";
2362 for (; candidates; candidates = candidates->next)
2364 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2366 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2367 error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn,
2368 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2369 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2370 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2371 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2372 error ("%s %D(%T, %T) <built-in>", str, candidates->fn,
2373 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2374 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2376 error ("%s %D(%T) <built-in>", str, candidates->fn,
2377 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2379 else if (TYPE_P (candidates->fn))
2380 error ("%s %T <conversion>", str, candidates->fn);
2382 cp_error_at ("%s %+#D%s", str, candidates->fn,
2383 candidates->viable == -1 ? " <near match>" : "");
2388 /* Returns the best overload candidate to perform the requested
2389 conversion. This function is used for three the overloading situations
2390 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2391 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2392 per [dcl.init.ref], so we ignore temporary bindings. */
2394 static struct z_candidate *
2395 build_user_type_conversion_1 (totype, expr, flags)
2399 struct z_candidate *candidates, *cand;
2400 tree fromtype = TREE_TYPE (expr);
2401 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2402 tree args = NULL_TREE;
2403 tree templates = NULL_TREE;
2405 /* We represent conversion within a hierarchy using RVALUE_CONV and
2406 BASE_CONV, as specified by [over.best.ics]; these become plain
2407 constructor calls, as specified in [dcl.init]. */
2408 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2409 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2411 if (IS_AGGR_TYPE (totype))
2412 ctors = lookup_fnfields (TYPE_BINFO (totype),
2413 complete_ctor_identifier,
2416 if (IS_AGGR_TYPE (fromtype))
2417 convs = lookup_conversions (fromtype);
2420 flags |= LOOKUP_NO_CONVERSION;
2426 ctors = TREE_VALUE (ctors);
2428 t = build_int_2 (0, 0);
2429 TREE_TYPE (t) = build_pointer_type (totype);
2430 args = build_tree_list (NULL_TREE, expr);
2431 /* We should never try to call the abstract or base constructor
2433 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2434 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2436 args = tree_cons (NULL_TREE, t, args);
2438 for (; ctors; ctors = OVL_NEXT (ctors))
2440 tree ctor = OVL_CURRENT (ctors);
2441 if (DECL_NONCONVERTING_P (ctor))
2444 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2446 templates = tree_cons (NULL_TREE, ctor, templates);
2448 add_template_candidate (candidates, ctor, totype,
2449 NULL_TREE, args, NULL_TREE, flags,
2453 candidates = add_function_candidate (candidates, ctor, totype,
2458 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2459 candidates->basetype_path = TYPE_BINFO (totype);
2464 args = build_tree_list (NULL_TREE, build_this (expr));
2466 for (; convs; convs = TREE_CHAIN (convs))
2468 tree fns = TREE_VALUE (convs);
2469 int convflags = LOOKUP_NO_CONVERSION;
2471 /* If we are called to convert to a reference type, we are trying to
2472 find an lvalue binding, so don't even consider temporaries. If
2473 we don't find an lvalue binding, the caller will try again to
2474 look for a temporary binding. */
2475 if (TREE_CODE (totype) == REFERENCE_TYPE)
2476 convflags |= LOOKUP_NO_TEMP_BIND;
2478 for (; fns; fns = OVL_NEXT (fns))
2480 tree fn = OVL_CURRENT (fns);
2481 struct z_candidate *old_candidates = candidates;
2483 /* [over.match.funcs] For conversion functions, the function
2484 is considered to be a member of the class of the implicit
2485 object argument for the purpose of defining the type of
2486 the implicit object parameter.
2488 So we pass fromtype as CTYPE to add_*_candidate. */
2490 if (TREE_CODE (fn) == TEMPLATE_DECL)
2492 templates = tree_cons (NULL_TREE, fn, templates);
2494 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2495 args, totype, flags,
2499 candidates = add_function_candidate (candidates, fn, fromtype,
2502 if (candidates != old_candidates)
2504 tree ics = implicit_conversion
2505 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2508 candidates->second_conv = ics;
2509 candidates->basetype_path = TYPE_BINFO (fromtype);
2511 if (ics == NULL_TREE)
2512 candidates->viable = 0;
2513 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2514 candidates->viable = -1;
2519 if (! any_viable (candidates))
2522 if (flags & LOOKUP_COMPLAIN)
2524 if (candidates && ! candidates->next)
2525 /* say why this one won't work or try to be loose */;
2527 error ("no viable candidates");
2534 candidates = splice_viable (candidates);
2535 cand = tourney (candidates);
2539 if (flags & LOOKUP_COMPLAIN)
2541 error ("conversion from `%T' to `%T' is ambiguous",
2543 print_z_candidates (candidates);
2546 cand = candidates; /* any one will do */
2547 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2548 ICS_USER_FLAG (cand->second_conv) = 1;
2549 ICS_BAD_FLAG (cand->second_conv) = 1;
2554 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2555 p = &(TREE_OPERAND (*p, 0));
2559 (DECL_CONSTRUCTOR_P (cand->fn)
2560 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2561 expr, build_zc_wrapper (cand));
2563 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2564 if (cand->viable == -1)
2565 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2571 build_user_type_conversion (totype, expr, flags)
2575 struct z_candidate *cand
2576 = build_user_type_conversion_1 (totype, expr, flags);
2580 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2581 return error_mark_node;
2582 return convert_from_reference (convert_like (cand->second_conv, expr));
2587 /* Do any initial processing on the arguments to a function call. */
2594 for (t = args; t; t = TREE_CHAIN (t))
2596 tree arg = TREE_VALUE (t);
2598 if (arg == error_mark_node)
2599 return error_mark_node;
2600 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2602 error ("invalid use of void expression");
2603 return error_mark_node;
2605 else if (TREE_CODE (arg) == OFFSET_REF)
2606 arg = resolve_offset_ref (arg);
2607 arg = convert_from_reference (arg);
2608 TREE_VALUE (t) = arg;
2614 build_new_function_call (fn, args)
2617 struct z_candidate *candidates = 0, *cand;
2618 tree explicit_targs = NULL_TREE;
2619 int template_only = 0;
2621 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2623 explicit_targs = TREE_OPERAND (fn, 1);
2624 fn = TREE_OPERAND (fn, 0);
2628 if (really_overloaded_fn (fn))
2631 tree templates = NULL_TREE;
2633 args = resolve_args (args);
2635 if (args == error_mark_node)
2636 return error_mark_node;
2638 for (t1 = fn; t1; t1 = OVL_CHAIN (t1))
2640 tree t = OVL_FUNCTION (t1);
2642 if (TREE_CODE (t) == TEMPLATE_DECL)
2644 templates = tree_cons (NULL_TREE, t, templates);
2645 candidates = add_template_candidate
2646 (candidates, t, NULL_TREE, explicit_targs, args, NULL_TREE,
2647 LOOKUP_NORMAL, DEDUCE_CALL);
2649 else if (! template_only)
2650 candidates = add_function_candidate
2651 (candidates, t, NULL_TREE, args, LOOKUP_NORMAL);
2654 if (! any_viable (candidates))
2656 if (candidates && ! candidates->next)
2657 return build_function_call (candidates->fn, args);
2658 error ("no matching function for call to `%D(%A)'",
2659 DECL_NAME (OVL_FUNCTION (fn)), args);
2661 print_z_candidates (candidates);
2662 return error_mark_node;
2664 candidates = splice_viable (candidates);
2665 cand = tourney (candidates);
2669 error ("call of overloaded `%D(%A)' is ambiguous",
2670 DECL_NAME (OVL_FUNCTION (fn)), args);
2671 print_z_candidates (candidates);
2672 return error_mark_node;
2675 return build_over_call (cand, args, LOOKUP_NORMAL);
2678 /* This is not really overloaded. */
2679 fn = OVL_CURRENT (fn);
2681 return build_function_call (fn, args);
2685 build_object_call (obj, args)
2688 struct z_candidate *candidates = 0, *cand;
2689 tree fns, convs, mem_args = NULL_TREE;
2690 tree type = TREE_TYPE (obj);
2692 if (TYPE_PTRMEMFUNC_P (type))
2694 /* It's no good looking for an overloaded operator() on a
2695 pointer-to-member-function. */
2696 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2697 return error_mark_node;
2700 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2701 if (fns == error_mark_node)
2702 return error_mark_node;
2704 args = resolve_args (args);
2706 if (args == error_mark_node)
2707 return error_mark_node;
2711 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
2712 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2714 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
2716 tree fn = OVL_CURRENT (fns);
2717 if (TREE_CODE (fn) == TEMPLATE_DECL)
2720 = add_template_candidate (candidates, fn, base, NULL_TREE,
2721 mem_args, NULL_TREE,
2722 LOOKUP_NORMAL, DEDUCE_CALL);
2725 candidates = add_function_candidate
2726 (candidates, fn, base, mem_args, LOOKUP_NORMAL);
2729 candidates->basetype_path = TYPE_BINFO (type);
2733 convs = lookup_conversions (type);
2735 for (; convs; convs = TREE_CHAIN (convs))
2737 tree fns = TREE_VALUE (convs);
2738 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2740 if ((TREE_CODE (totype) == POINTER_TYPE
2741 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2742 || (TREE_CODE (totype) == REFERENCE_TYPE
2743 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2744 || (TREE_CODE (totype) == REFERENCE_TYPE
2745 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2746 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2747 for (; fns; fns = OVL_NEXT (fns))
2749 tree fn = OVL_CURRENT (fns);
2750 if (TREE_CODE (fn) == TEMPLATE_DECL)
2752 candidates = add_template_conv_candidate (candidates,
2759 candidates = add_conv_candidate (candidates, fn, obj, args);
2763 if (! any_viable (candidates))
2765 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2766 print_z_candidates (candidates);
2767 return error_mark_node;
2770 candidates = splice_viable (candidates);
2771 cand = tourney (candidates);
2775 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2776 print_z_candidates (candidates);
2777 return error_mark_node;
2780 /* Since cand->fn will be a type, not a function, for a conversion
2781 function, we must be careful not to unconditionally look at
2783 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2784 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2785 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2787 obj = convert_like_with_context
2788 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2791 return build_function_call (obj, args);
2795 op_error (code, code2, arg1, arg2, arg3, problem)
2796 enum tree_code code, code2;
2797 tree arg1, arg2, arg3;
2798 const char *problem;
2802 if (code == MODIFY_EXPR)
2803 opname = assignment_operator_name_info[code2].name;
2805 opname = operator_name_info[code].name;
2810 error ("%s for `%T ? %T : %T' operator", problem,
2811 error_type (arg1), error_type (arg2), error_type (arg3));
2813 case POSTINCREMENT_EXPR:
2814 case POSTDECREMENT_EXPR:
2815 error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2818 error ("%s for `%T [%T]' operator", problem,
2819 error_type (arg1), error_type (arg2));
2823 error ("%s for `%T %s %T' operator", problem,
2824 error_type (arg1), opname, error_type (arg2));
2826 error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2830 /* Return the implicit conversion sequence that could be used to
2831 convert E1 to E2 in [expr.cond]. */
2834 conditional_conversion (e1, e2)
2838 tree t1 = non_reference (TREE_TYPE (e1));
2839 tree t2 = non_reference (TREE_TYPE (e2));
2844 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2845 implicitly converted (clause _conv_) to the type "reference to
2846 T2", subject to the constraint that in the conversion the
2847 reference must bind directly (_dcl.init.ref_) to E1. */
2848 if (real_lvalue_p (e2))
2850 conv = implicit_conversion (build_reference_type (t2),
2853 LOOKUP_NO_TEMP_BIND);
2860 If E1 and E2 have class type, and the underlying class types are
2861 the same or one is a base class of the other: E1 can be converted
2862 to match E2 if the class of T2 is the same type as, or a base
2863 class of, the class of T1, and the cv-qualification of T2 is the
2864 same cv-qualification as, or a greater cv-qualification than, the
2865 cv-qualification of T1. If the conversion is applied, E1 is
2866 changed to an rvalue of type T2 that still refers to the original
2867 source class object (or the appropriate subobject thereof). */
2868 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2869 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2870 TYPE_MAIN_VARIANT (t1)))
2872 if (at_least_as_qualified_p (t2, t1))
2874 conv = build1 (IDENTITY_CONV, t1, e1);
2875 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2876 TYPE_MAIN_VARIANT (t2)))
2877 conv = build_conv (BASE_CONV, t2, conv);
2886 E1 can be converted to match E2 if E1 can be implicitly converted
2887 to the type that expression E2 would have if E2 were converted to
2888 an rvalue (or the type it has, if E2 is an rvalue). */
2889 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2892 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2893 arguments to the conditional expression. By the time this function
2894 is called, any suitable candidate functions are included in
2898 build_conditional_expr (arg1, arg2, arg3)
2906 tree result_type = NULL_TREE;
2908 struct z_candidate *candidates = 0;
2909 struct z_candidate *cand;
2911 /* As a G++ extension, the second argument to the conditional can be
2912 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2913 c'.) If the second operand is omitted, make sure it is
2914 calculated only once. */
2918 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2919 arg1 = arg2 = save_expr (arg1);
2924 The first expr ession is implicitly converted to bool (clause
2926 arg1 = cp_convert (boolean_type_node, arg1);
2928 /* If something has already gone wrong, just pass that fact up the
2930 if (arg1 == error_mark_node
2931 || arg2 == error_mark_node
2932 || arg3 == error_mark_node
2933 || TREE_TYPE (arg1) == error_mark_node
2934 || TREE_TYPE (arg2) == error_mark_node
2935 || TREE_TYPE (arg3) == error_mark_node)
2936 return error_mark_node;
2940 If either the second or the third operand has type (possibly
2941 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2942 array-to-pointer (_conv.array_), and function-to-pointer
2943 (_conv.func_) standard conversions are performed on the second
2944 and third operands. */
2945 arg2_type = TREE_TYPE (arg2);
2946 arg3_type = TREE_TYPE (arg3);
2947 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2949 /* Do the conversions. We don't these for `void' type arguments
2950 since it can't have any effect and since decay_conversion
2951 does not handle that case gracefully. */
2952 if (!VOID_TYPE_P (arg2_type))
2953 arg2 = decay_conversion (arg2);
2954 if (!VOID_TYPE_P (arg3_type))
2955 arg3 = decay_conversion (arg3);
2956 arg2_type = TREE_TYPE (arg2);
2957 arg3_type = TREE_TYPE (arg3);
2961 One of the following shall hold:
2963 --The second or the third operand (but not both) is a
2964 throw-expression (_except.throw_); the result is of the
2965 type of the other and is an rvalue.
2967 --Both the second and the third operands have type void; the
2968 result is of type void and is an rvalue. */
2969 if ((TREE_CODE (arg2) == THROW_EXPR)
2970 ^ (TREE_CODE (arg3) == THROW_EXPR))
2971 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
2972 ? arg3_type : arg2_type);
2973 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
2974 result_type = void_type_node;
2977 error ("`%E' has type `void' and is not a throw-expression",
2978 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
2979 return error_mark_node;
2983 goto valid_operands;
2987 Otherwise, if the second and third operand have different types,
2988 and either has (possibly cv-qualified) class type, an attempt is
2989 made to convert each of those operands to the type of the other. */
2990 else if (!same_type_p (arg2_type, arg3_type)
2991 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
2993 tree conv2 = conditional_conversion (arg2, arg3);
2994 tree conv3 = conditional_conversion (arg3, arg2);
2998 If both can be converted, or one can be converted but the
2999 conversion is ambiguous, the program is ill-formed. If
3000 neither can be converted, the operands are left unchanged and
3001 further checking is performed as described below. If exactly
3002 one conversion is possible, that conversion is applied to the
3003 chosen operand and the converted operand is used in place of
3004 the original operand for the remainder of this section. */
3005 if ((conv2 && !ICS_BAD_FLAG (conv2)
3006 && conv3 && !ICS_BAD_FLAG (conv3))
3007 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3008 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3010 error ("operands to ?: have different types");
3011 return error_mark_node;
3013 else if (conv2 && !ICS_BAD_FLAG (conv2))
3015 arg2 = convert_like (conv2, arg2);
3016 arg2 = convert_from_reference (arg2);
3017 /* That may not quite have done the trick. If the two types
3018 are cv-qualified variants of one another, we will have
3019 just used an IDENTITY_CONV. (There's no conversion from
3020 an lvalue of one class type to an lvalue of another type,
3021 even a cv-qualified variant, and we don't want to lose
3022 lvalue-ness here.) So, we manually add a NOP_EXPR here
3024 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3025 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3026 arg2_type = TREE_TYPE (arg2);
3028 else if (conv3 && !ICS_BAD_FLAG (conv3))
3030 arg3 = convert_like (conv3, arg3);
3031 arg3 = convert_from_reference (arg3);
3032 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3033 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3034 arg3_type = TREE_TYPE (arg3);
3040 If the second and third operands are lvalues and have the same
3041 type, the result is of that type and is an lvalue. */
3042 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3043 same_type_p (arg2_type, arg3_type))
3045 result_type = arg2_type;
3046 goto valid_operands;
3051 Otherwise, the result is an rvalue. If the second and third
3052 operand do not have the same type, and either has (possibly
3053 cv-qualified) class type, overload resolution is used to
3054 determine the conversions (if any) to be applied to the operands
3055 (_over.match.oper_, _over.built_). */
3057 if (!same_type_p (arg2_type, arg3_type)
3058 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3063 /* Rearrange the arguments so that add_builtin_candidate only has
3064 to know about two args. In build_builtin_candidates, the
3065 arguments are unscrambled. */
3069 candidates = add_builtin_candidates (candidates,
3072 ansi_opname (COND_EXPR),
3078 If the overload resolution fails, the program is
3080 if (!any_viable (candidates))
3082 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3083 print_z_candidates (candidates);
3084 return error_mark_node;
3086 candidates = splice_viable (candidates);
3087 cand = tourney (candidates);
3090 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3091 print_z_candidates (candidates);
3092 return error_mark_node;
3097 Otherwise, the conversions thus determined are applied, and
3098 the converted operands are used in place of the original
3099 operands for the remainder of this section. */
3100 conv = TREE_VEC_ELT (cand->convs, 0);
3101 arg1 = convert_like (conv, arg1);
3102 conv = TREE_VEC_ELT (cand->convs, 1);
3103 arg2 = convert_like (conv, arg2);
3104 conv = TREE_VEC_ELT (cand->convs, 2);
3105 arg3 = convert_like (conv, arg3);
3110 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3111 and function-to-pointer (_conv.func_) standard conversions are
3112 performed on the second and third operands.
3114 We need to force the lvalue-to-rvalue conversion here for class types,
3115 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3116 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3119 We use ocp_convert rather than build_user_type_conversion because the
3120 latter returns NULL_TREE on failure, while the former gives an error. */
3122 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3123 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3124 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3126 arg2 = decay_conversion (arg2);
3127 arg2_type = TREE_TYPE (arg2);
3129 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3130 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3131 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3133 arg3 = decay_conversion (arg3);
3134 arg3_type = TREE_TYPE (arg3);
3136 if (arg2 == error_mark_node || arg3 == error_mark_node)
3137 return error_mark_node;
3141 After those conversions, one of the following shall hold:
3143 --The second and third operands have the same type; the result is of
3145 if (same_type_p (arg2_type, arg3_type))
3146 result_type = arg2_type;
3149 --The second and third operands have arithmetic or enumeration
3150 type; the usual arithmetic conversions are performed to bring
3151 them to a common type, and the result is of that type. */
3152 else if ((ARITHMETIC_TYPE_P (arg2_type)
3153 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3154 && (ARITHMETIC_TYPE_P (arg3_type)
3155 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3157 /* In this case, there is always a common type. */
3158 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3161 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3162 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3163 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3164 arg2_type, arg3_type);
3165 else if (extra_warnings
3166 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3167 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3168 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3169 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3170 warning ("enumeral and non-enumeral type in conditional expression");
3172 arg2 = perform_implicit_conversion (result_type, arg2);
3173 arg3 = perform_implicit_conversion (result_type, arg3);
3177 --The second and third operands have pointer type, or one has
3178 pointer type and the other is a null pointer constant; pointer
3179 conversions (_conv.ptr_) and qualification conversions
3180 (_conv.qual_) are performed to bring them to their composite
3181 pointer type (_expr.rel_). The result is of the composite
3184 --The second and third operands have pointer to member type, or
3185 one has pointer to member type and the other is a null pointer
3186 constant; pointer to member conversions (_conv.mem_) and
3187 qualification conversions (_conv.qual_) are performed to bring
3188 them to a common type, whose cv-qualification shall match the
3189 cv-qualification of either the second or the third operand.
3190 The result is of the common type. */
3191 else if ((null_ptr_cst_p (arg2)
3192 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3193 || TYPE_PTRMEMFUNC_P (arg3_type)))
3194 || (null_ptr_cst_p (arg3)
3195 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3196 || TYPE_PTRMEMFUNC_P (arg2_type)))
3197 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3198 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3199 || (TYPE_PTRMEMFUNC_P (arg2_type)
3200 && TYPE_PTRMEMFUNC_P (arg3_type)))
3202 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3203 arg3, "conditional expression");
3204 arg2 = perform_implicit_conversion (result_type, arg2);
3205 arg3 = perform_implicit_conversion (result_type, arg3);
3210 error ("operands to ?: have different types");
3211 return error_mark_node;
3215 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3216 /* Expand both sides into the same slot, hopefully the target of the
3217 ?: expression. We used to check for TARGET_EXPRs here, but now we
3218 sometimes wrap them in NOP_EXPRs so the test would fail. */
3219 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3220 result = build_target_expr_with_type (result, result_type);
3222 /* If this expression is an rvalue, but might be mistaken for an
3223 lvalue, we must add a NON_LVALUE_EXPR. */
3224 if (!lvalue_p && real_lvalue_p (result))
3225 result = build1 (NON_LVALUE_EXPR, result_type, result);
3231 build_new_op (code, flags, arg1, arg2, arg3)
3232 enum tree_code code;
3234 tree arg1, arg2, arg3;
3236 struct z_candidate *candidates = 0, *cand;
3237 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3238 enum tree_code code2 = NOP_EXPR;
3239 tree templates = NULL_TREE;
3241 bool viable_candidates;
3243 if (arg1 == error_mark_node
3244 || arg2 == error_mark_node
3245 || arg3 == error_mark_node)
3246 return error_mark_node;
3248 /* This can happen if a template takes all non-type parameters, e.g.
3249 undeclared_template<1, 5, 72>a; */
3250 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3252 error ("`%D' must be declared before use", arg1);
3253 return error_mark_node;
3256 if (code == MODIFY_EXPR)
3258 code2 = TREE_CODE (arg3);
3260 fnname = ansi_assopname (code2);
3263 fnname = ansi_opname (code);
3265 if (TREE_CODE (arg1) == OFFSET_REF)
3266 arg1 = resolve_offset_ref (arg1);
3267 arg1 = convert_from_reference (arg1);
3273 case VEC_DELETE_EXPR:
3275 /* Use build_op_new_call and build_op_delete_call instead. */
3279 return build_object_call (arg1, arg2);
3287 if (TREE_CODE (arg2) == OFFSET_REF)
3288 arg2 = resolve_offset_ref (arg2);
3289 arg2 = convert_from_reference (arg2);
3293 if (TREE_CODE (arg3) == OFFSET_REF)
3294 arg3 = resolve_offset_ref (arg3);
3295 arg3 = convert_from_reference (arg3);
3298 if (code == COND_EXPR)
3300 if (arg2 == NULL_TREE
3301 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3302 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3303 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3304 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3307 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3308 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3311 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3312 arg2 = integer_zero_node;
3314 arglist = NULL_TREE;
3316 arglist = tree_cons (NULL_TREE, arg3, arglist);
3318 arglist = tree_cons (NULL_TREE, arg2, arglist);
3319 arglist = tree_cons (NULL_TREE, arg1, arglist);
3321 fns = lookup_function_nonclass (fnname, arglist);
3323 if (fns && TREE_CODE (fns) == TREE_LIST)
3324 fns = TREE_VALUE (fns);
3325 for (; fns; fns = OVL_NEXT (fns))
3327 tree fn = OVL_CURRENT (fns);
3328 if (TREE_CODE (fn) == TEMPLATE_DECL)
3330 templates = tree_cons (NULL_TREE, fn, templates);
3332 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3333 arglist, TREE_TYPE (fnname),
3334 flags, DEDUCE_CALL);
3337 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3341 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3343 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3344 if (fns == error_mark_node)
3352 tree basetype = BINFO_TYPE (TREE_PURPOSE (fns));
3353 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3354 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
3356 tree fn = OVL_CURRENT (fns);
3359 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3360 this_arglist = mem_arglist;
3362 this_arglist = arglist;
3364 if (TREE_CODE (fn) == TEMPLATE_DECL)
3366 /* A member template. */
3367 templates = tree_cons (NULL_TREE, fn, templates);
3369 = add_template_candidate (candidates, fn, basetype, NULL_TREE,
3370 this_arglist, TREE_TYPE (fnname),
3371 flags, DEDUCE_CALL);
3374 candidates = add_function_candidate
3375 (candidates, fn, basetype, this_arglist, flags);
3378 candidates->basetype_path = TYPE_BINFO (TREE_TYPE (arg1));
3385 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3386 to know about two args; a builtin candidate will always have a first
3387 parameter of type bool. We'll handle that in
3388 build_builtin_candidate. */
3389 if (code == COND_EXPR)
3399 args[2] = NULL_TREE;
3402 candidates = add_builtin_candidates
3403 (candidates, code, code2, fnname, args, flags);
3410 /* For these, the built-in candidates set is empty
3411 [over.match.oper]/3. We don't want non-strict matches
3412 because exact matches are always possible with built-in
3413 operators. The built-in candidate set for COMPONENT_REF
3414 would be empty too, but since there are no such built-in
3415 operators, we accept non-strict matches for them. */
3416 viable_candidates = any_strictly_viable (candidates);
3420 viable_candidates = any_viable (candidates);
3424 if (! viable_candidates)
3428 case POSTINCREMENT_EXPR:
3429 case POSTDECREMENT_EXPR:
3430 /* Look for an `operator++ (int)'. If they didn't have
3431 one, then we fall back to the old way of doing things. */
3432 if (flags & LOOKUP_COMPLAIN)
3433 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3435 operator_name_info[code].name);
3436 if (code == POSTINCREMENT_EXPR)
3437 code = PREINCREMENT_EXPR;
3439 code = PREDECREMENT_EXPR;
3440 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3442 /* The caller will deal with these. */
3451 if (flags & LOOKUP_COMPLAIN)
3453 op_error (code, code2, arg1, arg2, arg3, "no match");
3454 print_z_candidates (candidates);
3456 return error_mark_node;
3458 candidates = splice_viable (candidates);
3459 cand = tourney (candidates);
3463 if (flags & LOOKUP_COMPLAIN)
3465 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3466 print_z_candidates (candidates);
3468 return error_mark_node;
3471 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3473 extern int warn_synth;
3475 && fnname == ansi_assopname (NOP_EXPR)
3476 && DECL_ARTIFICIAL (cand->fn)
3478 && ! candidates->next->next)
3480 warning ("using synthesized `%#D' for copy assignment",
3482 cp_warning_at (" where cfront would use `%#D'",
3484 ? candidates->next->fn
3488 return build_over_call
3490 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3491 ? mem_arglist : arglist,
3495 /* Check for comparison of different enum types. */
3504 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3505 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3506 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3507 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3509 warning ("comparison between `%#T' and `%#T'",
3510 TREE_TYPE (arg1), TREE_TYPE (arg2));
3517 /* We need to strip any leading REF_BIND so that bitfields don't cause
3518 errors. This should not remove any important conversions, because
3519 builtins don't apply to class objects directly. */
3520 conv = TREE_VEC_ELT (cand->convs, 0);
3521 if (TREE_CODE (conv) == REF_BIND)
3522 conv = TREE_OPERAND (conv, 0);
3523 arg1 = convert_like (conv, arg1);
3526 conv = TREE_VEC_ELT (cand->convs, 1);
3527 if (TREE_CODE (conv) == REF_BIND)
3528 conv = TREE_OPERAND (conv, 0);
3529 arg2 = convert_like (conv, arg2);
3533 conv = TREE_VEC_ELT (cand->convs, 2);
3534 if (TREE_CODE (conv) == REF_BIND)
3535 conv = TREE_OPERAND (conv, 0);
3536 arg3 = convert_like (conv, arg3);
3543 return build_modify_expr (arg1, code2, arg2);
3546 return build_indirect_ref (arg1, "unary *");
3551 case TRUNC_DIV_EXPR:
3562 case TRUNC_MOD_EXPR:
3566 case TRUTH_ANDIF_EXPR:
3567 case TRUTH_ORIF_EXPR:
3568 return cp_build_binary_op (code, arg1, arg2);
3573 case TRUTH_NOT_EXPR:
3574 case PREINCREMENT_EXPR:
3575 case POSTINCREMENT_EXPR:
3576 case PREDECREMENT_EXPR:
3577 case POSTDECREMENT_EXPR:
3580 return build_unary_op (code, arg1, candidates != 0);
3583 return build_array_ref (arg1, arg2);
3586 return build_conditional_expr (arg1, arg2, arg3);
3589 return build_m_component_ref
3590 (build_indirect_ref (arg1, NULL), arg2);
3592 /* The caller will deal with these. */
3604 /* Build a call to operator delete. This has to be handled very specially,
3605 because the restrictions on what signatures match are different from all
3606 other call instances. For a normal delete, only a delete taking (void *)
3607 or (void *, size_t) is accepted. For a placement delete, only an exact
3608 match with the placement new is accepted.
3610 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3611 ADDR is the pointer to be deleted.
3612 SIZE is the size of the memory block to be deleted.
3613 FLAGS are the usual overloading flags.
3614 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3617 build_op_delete_call (code, addr, size, flags, placement)
3618 enum tree_code code;
3619 tree addr, size, placement;
3622 tree fn = NULL_TREE;
3623 tree fns, fnname, fntype, argtypes, args, type;
3626 if (addr == error_mark_node)
3627 return error_mark_node;
3629 type = TREE_TYPE (TREE_TYPE (addr));
3630 while (TREE_CODE (type) == ARRAY_TYPE)
3631 type = TREE_TYPE (type);
3633 fnname = ansi_opname (code);
3635 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3638 If the result of the lookup is ambiguous or inaccessible, or if
3639 the lookup selects a placement deallocation function, the
3640 program is ill-formed.
3642 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3644 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3645 if (fns == error_mark_node)
3646 return error_mark_node;
3651 if (fns == NULL_TREE)
3652 fns = lookup_name_nonclass (fnname);
3659 /* Find the allocation function that is being called. */
3660 call_expr = placement;
3661 /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3663 while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3664 call_expr = TREE_OPERAND (call_expr, 1);
3665 /* Extract the function. */
3666 alloc_fn = get_callee_fndecl (call_expr);
3667 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3668 /* Then the second parm type. */
3669 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3670 /* Also the second argument. */
3671 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3675 /* First try it without the size argument. */
3676 argtypes = void_list_node;
3680 /* Strip const and volatile from addr. */
3681 addr = cp_convert (ptr_type_node, addr);
3683 /* We make two tries at finding a matching `operator delete'. On
3684 the first pass, we look for an one-operator (or placement)
3685 operator delete. If we're not doing placement delete, then on
3686 the second pass we look for a two-argument delete. */
3687 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3690 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3692 /* Normal delete; now try to find a match including the size
3694 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3695 tree_cons (NULL_TREE, sizetype,
3697 fntype = build_function_type (void_type_node, argtypes);
3699 /* Go through the `operator delete' functions looking for one
3700 with a matching type. */
3701 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3707 /* Exception specifications on the `delete' operator do not
3709 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3711 /* We also don't compare attributes. We're really just
3712 trying to check the types of the first two parameters. */
3713 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3717 /* If we found a match, we're done. */
3722 /* If we have a matching function, call it. */
3725 /* Make sure we have the actual function, and not an
3727 fn = OVL_CURRENT (fn);
3729 /* If the FN is a member function, make sure that it is
3731 if (DECL_CLASS_SCOPE_P (fn))
3732 enforce_access (type, fn);
3735 args = tree_cons (NULL_TREE, addr, args);
3737 args = tree_cons (NULL_TREE, addr,
3738 build_tree_list (NULL_TREE, size));
3740 return build_function_call (fn, args);
3743 /* If we are doing placement delete we do nothing if we don't find a
3744 matching op delete. */
3748 error ("no suitable `operator delete' for `%T'", type);
3749 return error_mark_node;
3752 /* If the current scope isn't allowed to access DECL along
3753 BASETYPE_PATH, give an error. The most derived class in
3754 BASETYPE_PATH is the one used to qualify DECL. */
3757 enforce_access (basetype_path, decl)
3763 accessible = accessible_p (basetype_path, decl);
3766 if (TREE_PRIVATE (decl))
3767 cp_error_at ("`%+#D' is private", decl);
3768 else if (TREE_PROTECTED (decl))
3769 cp_error_at ("`%+#D' is protected", decl);
3771 cp_error_at ("`%+#D' is inaccessible", decl);
3772 error ("within this context");
3779 /* Perform the conversions in CONVS on the expression EXPR.
3780 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3781 indicates the `this' argument of a method. INNER is non-zero when
3782 being called to continue a conversion chain. It is negative when a
3783 reference binding will be applied, positive otherwise. */
3786 convert_like_real (convs, expr, fn, argnum, inner)
3794 tree totype = TREE_TYPE (convs);
3796 if (ICS_BAD_FLAG (convs)
3797 && TREE_CODE (convs) != USER_CONV
3798 && TREE_CODE (convs) != AMBIG_CONV
3799 && TREE_CODE (convs) != REF_BIND)
3802 for (; t; t = TREE_OPERAND (t, 0))
3804 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3806 expr = convert_like_real (t, expr, fn, argnum, 1);
3809 else if (TREE_CODE (t) == AMBIG_CONV)
3810 return convert_like_real (t, expr, fn, argnum, 1);
3811 else if (TREE_CODE (t) == IDENTITY_CONV)
3814 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3816 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3817 return cp_convert (totype, expr);
3821 expr = dubious_conversion_warnings
3822 (totype, expr, "argument", fn, argnum);
3823 switch (TREE_CODE (convs))
3827 struct z_candidate *cand
3828 = WRAPPER_ZC (TREE_OPERAND (convs, 1));
3829 tree convfn = cand->fn;
3832 if (DECL_CONSTRUCTOR_P (convfn))
3834 tree t = build_int_2 (0, 0);
3835 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3837 args = build_tree_list (NULL_TREE, expr);
3838 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3839 || DECL_HAS_VTT_PARM_P (convfn))
3840 /* We should never try to call the abstract or base constructor
3843 args = tree_cons (NULL_TREE, t, args);
3846 args = build_this (expr);
3847 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3849 /* If this is a constructor or a function returning an aggr type,
3850 we need to build up a TARGET_EXPR. */
3851 if (DECL_CONSTRUCTOR_P (convfn))
3852 expr = build_cplus_new (totype, expr);
3854 /* The result of the call is then used to direct-initialize the object
3855 that is the destination of the copy-initialization. [dcl.init]
3857 Note that this step is not reflected in the conversion sequence;
3858 it affects the semantics when we actually perform the
3859 conversion, but is not considered during overload resolution.
3861 If the target is a class, that means call a ctor. */
3862 if (IS_AGGR_TYPE (totype)
3863 && (inner >= 0 || !lvalue_p (expr)))
3865 savew = warningcount, savee = errorcount;
3866 expr = build_new_method_call
3867 (NULL_TREE, complete_ctor_identifier,
3868 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3869 /* Core issue 84, now a DR, says that we don't allow UDCs
3870 for these args (which deliberately breaks copy-init of an
3871 auto_ptr<Base> from an auto_ptr<Derived>). */
3872 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3874 /* Tell the user where this failing constructor call came from. */
3877 if (warningcount > savew)
3879 (" initializing argument %P of `%D' from result of `%D'",
3880 argnum, fn, convfn);
3881 else if (errorcount > savee)
3883 (" initializing argument %P of `%D' from result of `%D'",
3884 argnum, fn, convfn);
3888 if (warningcount > savew)
3889 warning (" initializing temporary from result of `%D'",
3891 else if (errorcount > savee)
3892 error (" initializing temporary from result of `%D'",
3895 expr = build_cplus_new (totype, expr);
3900 if (type_unknown_p (expr))
3901 expr = instantiate_type (totype, expr, tf_error | tf_warning);
3904 /* Call build_user_type_conversion again for the error. */
3905 return build_user_type_conversion
3906 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3912 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3913 TREE_CODE (convs) == REF_BIND ? -1 : 1);
3914 if (expr == error_mark_node)
3915 return error_mark_node;
3917 /* Convert a non-array constant variable to its underlying value, unless we
3918 are about to bind it to a reference, in which case we need to
3919 leave it as an lvalue. */
3920 if (TREE_CODE (convs) != REF_BIND
3921 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3922 expr = decl_constant_value (expr);
3924 switch (TREE_CODE (convs))
3927 if (! IS_AGGR_TYPE (totype))
3929 /* else fall through */
3931 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
3933 /* We are going to bind a reference directly to a base-class
3934 subobject of EXPR. */
3935 tree base_ptr = build_pointer_type (totype);
3937 /* Build an expression for `*((base*) &expr)'. */
3938 expr = build_unary_op (ADDR_EXPR, expr, 0);
3939 expr = perform_implicit_conversion (base_ptr, expr);
3940 expr = build_indirect_ref (expr, "implicit conversion");
3944 /* Copy-initialization where the cv-unqualified version of the source
3945 type is the same class as, or a derived class of, the class of the
3946 destination [is treated as direct-initialization]. [dcl.init] */
3947 savew = warningcount, savee = errorcount;
3948 expr = build_new_method_call (NULL_TREE, complete_ctor_identifier,
3949 build_tree_list (NULL_TREE, expr),
3950 TYPE_BINFO (totype),
3951 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
3954 if (warningcount > savew)
3955 warning (" initializing argument %P of `%D'", argnum, fn);
3956 else if (errorcount > savee)
3957 error (" initializing argument %P of `%D'", argnum, fn);
3959 return build_cplus_new (totype, expr);
3963 tree ref_type = totype;
3965 /* If necessary, create a temporary. */
3966 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
3968 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
3969 expr = build_target_expr_with_type (expr, type);
3972 /* Take the address of the thing to which we will bind the
3974 expr = build_unary_op (ADDR_EXPR, expr, 1);
3975 if (expr == error_mark_node)
3976 return error_mark_node;
3978 /* Convert it to a pointer to the type referred to by the
3979 reference. This will adjust the pointer if a derived to
3980 base conversion is being performed. */
3981 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
3983 /* Convert the pointer to the desired reference type. */
3984 expr = build1 (NOP_EXPR, ref_type, expr);
3990 return decay_conversion (expr);
3993 /* Warn about deprecated conversion if appropriate. */
3994 string_conv_p (totype, expr, 1);
4000 return ocp_convert (totype, expr, CONV_IMPLICIT,
4001 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4004 /* ARG is being passed to a varargs function. Perform any conversions
4005 required. Array/function to pointer decay must have already happened.
4006 Return the converted value. */
4009 convert_arg_to_ellipsis (arg)
4012 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4013 && (TYPE_PRECISION (TREE_TYPE (arg))
4014 < TYPE_PRECISION (double_type_node)))
4015 /* Convert `float' to `double'. */
4016 arg = cp_convert (double_type_node, arg);
4018 /* Convert `short' and `char' to full-size `int'. */
4019 arg = default_conversion (arg);
4021 arg = require_complete_type (arg);
4023 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4025 /* Undefined behaviour [expr.call] 5.2.2/7. */
4026 warning ("cannot pass objects of non-POD type `%#T' through `...'",
4033 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4036 build_x_va_arg (expr, type)
4040 if (processing_template_decl)
4041 return build_min (VA_ARG_EXPR, type, expr);
4043 type = complete_type_or_else (type, NULL_TREE);
4045 if (expr == error_mark_node || !type)
4046 return error_mark_node;
4048 if (! pod_type_p (type))
4050 /* Undefined behaviour [expr.call] 5.2.2/7. */
4051 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4055 return build_va_arg (expr, type);
4058 /* TYPE has been given to va_arg. Apply the default conversions which
4059 would have happened when passed via ellipsis. Return the promoted
4060 type, or the passed type if there is no change. */
4063 cxx_type_promotes_to (type)
4068 if (TREE_CODE (type) == ARRAY_TYPE)
4069 return build_pointer_type (TREE_TYPE (type));
4071 if (TREE_CODE (type) == FUNCTION_TYPE)
4072 return build_pointer_type (type);
4074 promote = type_promotes_to (type);
4075 if (same_type_p (type, promote))
4081 /* ARG is a default argument expression being passed to a parameter of
4082 the indicated TYPE, which is a parameter to FN. Do any required
4083 conversions. Return the converted value. */
4086 convert_default_arg (type, arg, fn, parmnum)
4092 if (TREE_CODE (arg) == DEFAULT_ARG)
4094 /* When processing the default args for a class, we can find that
4095 there is an ordering constraint, and we call a function who's
4096 default args have not yet been converted. For instance,
4099 void Foo (A const & = A ());
4101 We must process A::A before A::Foo's default arg can be converted.
4102 Remember the dependent function, so do_pending_defargs can retry,
4104 unprocessed_defarg_fn (fn);
4106 /* Don't return error_mark node, as we won't be able to distinguish
4107 genuine errors from this case, and that would lead to repeated
4108 diagnostics. Just make something of the right type. */
4109 return build1 (NOP_EXPR, type, integer_zero_node);
4112 if (fn && DECL_TEMPLATE_INFO (fn))
4113 arg = tsubst_default_argument (fn, type, arg);
4115 arg = break_out_target_exprs (arg);
4117 if (TREE_CODE (arg) == CONSTRUCTOR)
4119 arg = digest_init (type, arg, 0);
4120 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4121 "default argument", fn, parmnum);
4125 /* This could get clobbered by the following call. */
4126 if (TREE_HAS_CONSTRUCTOR (arg))
4127 arg = copy_node (arg);
4129 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4130 "default argument", fn, parmnum);
4131 if (PROMOTE_PROTOTYPES
4132 && INTEGRAL_TYPE_P (type)
4133 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4134 arg = default_conversion (arg);
4140 /* Subroutine of the various build_*_call functions. Overload resolution
4141 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4142 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4143 bitmask of various LOOKUP_* flags which apply to the call itself. */
4146 build_over_call (cand, args, flags)
4147 struct z_candidate *cand;
4152 tree convs = cand->convs;
4153 tree converted_args = NULL_TREE;
4154 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4155 tree conv, arg, val;
4159 /* Give any warnings we noticed during overload resolution. */
4161 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4162 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4164 if (DECL_FUNCTION_MEMBER_P (fn))
4165 enforce_access (cand->basetype_path, fn);
4167 if (args && TREE_CODE (args) != TREE_LIST)
4168 args = build_tree_list (NULL_TREE, args);
4171 /* The implicit parameters to a constructor are not considered by overload
4172 resolution, and must be of the proper type. */
4173 if (DECL_CONSTRUCTOR_P (fn))
4175 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4176 arg = TREE_CHAIN (arg);
4177 parm = TREE_CHAIN (parm);
4178 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4179 /* We should never try to call the abstract constructor. */
4181 if (DECL_HAS_VTT_PARM_P (fn))
4183 converted_args = tree_cons
4184 (NULL_TREE, TREE_VALUE (arg), converted_args);
4185 arg = TREE_CHAIN (arg);
4186 parm = TREE_CHAIN (parm);
4189 /* Bypass access control for 'this' parameter. */
4190 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4192 tree parmtype = TREE_VALUE (parm);
4193 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4195 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4196 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4197 TREE_TYPE (argtype), fn);
4199 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4200 X is called for an object that is not of type X, or of a type
4201 derived from X, the behavior is undefined.
4203 So we can assume that anything passed as 'this' is non-null, and
4204 optimize accordingly. */
4205 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4206 t = lookup_base (TREE_TYPE (TREE_TYPE (TREE_VALUE (arg))),
4207 TREE_TYPE (parmtype), ba_ignore, NULL);
4208 t = build_base_path (PLUS_EXPR, TREE_VALUE (arg), t, 1);
4209 converted_args = tree_cons (NULL_TREE, t, converted_args);
4210 parm = TREE_CHAIN (parm);
4211 arg = TREE_CHAIN (arg);
4217 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4219 tree type = TREE_VALUE (parm);
4221 conv = TREE_VEC_ELT (convs, i);
4222 val = convert_like_with_context
4223 (conv, TREE_VALUE (arg), fn, i - is_method);
4225 if (PROMOTE_PROTOTYPES
4226 && INTEGRAL_TYPE_P (type)
4227 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4228 val = default_conversion (val);
4229 converted_args = tree_cons (NULL_TREE, val, converted_args);
4232 /* Default arguments */
4233 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4235 = tree_cons (NULL_TREE,
4236 convert_default_arg (TREE_VALUE (parm),
4237 TREE_PURPOSE (parm),
4242 for (; arg; arg = TREE_CHAIN (arg))
4244 = tree_cons (NULL_TREE,
4245 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4248 converted_args = nreverse (converted_args);
4251 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4254 /* Avoid actually calling copy constructors and copy assignment operators,
4257 if (! flag_elide_constructors)
4258 /* Do things the hard way. */;
4259 else if (TREE_VEC_LENGTH (convs) == 1
4260 && DECL_COPY_CONSTRUCTOR_P (fn))
4263 arg = skip_artificial_parms_for (fn, converted_args);
4264 arg = TREE_VALUE (arg);
4266 /* Pull out the real argument, disregarding const-correctness. */
4268 while (TREE_CODE (targ) == NOP_EXPR
4269 || TREE_CODE (targ) == NON_LVALUE_EXPR
4270 || TREE_CODE (targ) == CONVERT_EXPR)
4271 targ = TREE_OPERAND (targ, 0);
4272 if (TREE_CODE (targ) == ADDR_EXPR)
4274 targ = TREE_OPERAND (targ, 0);
4275 if (!same_type_ignoring_top_level_qualifiers_p
4276 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4285 arg = build_indirect_ref (arg, 0);
4287 /* [class.copy]: the copy constructor is implicitly defined even if
4288 the implementation elided its use. */
4289 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4292 /* If we're creating a temp and we already have one, don't create a
4293 new one. If we're not creating a temp but we get one, use
4294 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4295 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4296 temp or an INIT_EXPR otherwise. */
4297 if (integer_zerop (TREE_VALUE (args)))
4299 if (! real_lvalue_p (arg))
4301 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4302 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4304 else if ((!real_lvalue_p (arg)
4305 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4306 /* Empty classes have padding which can be hidden
4307 inside an (empty) base of the class. This must not
4308 be touched as it might overlay things. When the
4309 gcc core learns about empty classes, we can treat it
4310 like other classes. */
4311 && !(is_empty_class (DECL_CONTEXT (fn))
4312 && TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn))))
4315 tree to = stabilize_reference
4316 (build_indirect_ref (TREE_VALUE (args), 0));
4318 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4319 address = build_unary_op (ADDR_EXPR, val, 0);
4320 /* Avoid a warning about this expression, if the address is
4322 TREE_USED (address) = 1;
4326 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4328 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4330 tree to = stabilize_reference
4331 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4333 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4334 if (is_empty_class (TREE_TYPE (to)))
4336 TREE_USED (arg) = 1;
4338 val = build (COMPOUND_EXPR, DECL_CONTEXT (fn), arg, to);
4339 /* Even though the assignment may not actually result in any
4340 code being generated, we do not want to warn about the
4341 assignment having no effect. That would be confusing to
4342 users who may be performing the assignment as part of a
4343 generic algorithm, for example.
4345 Ideally, the notions of having side-effects and of being
4346 useless would be orthogonal. */
4347 TREE_SIDE_EFFECTS (val) = 1;
4348 TREE_NO_UNUSED_WARNING (val) = 1;
4351 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4357 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4359 tree t, *p = &TREE_VALUE (converted_args);
4360 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4361 DECL_VIRTUAL_CONTEXT (fn),
4363 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4365 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4366 if (TREE_SIDE_EFFECTS (*p))
4367 *p = save_expr (*p);
4368 t = build_pointer_type (TREE_TYPE (fn));
4369 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4370 fn = build_java_interface_fn_ref (fn, *p);
4372 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4375 else if (DECL_INLINE (fn))
4376 fn = inline_conversion (fn);
4378 fn = build_addr_func (fn);
4380 /* Recognize certain built-in functions so we can make tree-codes
4381 other than CALL_EXPR. We do this when it enables fold-const.c
4382 to do something useful. */
4384 if (TREE_CODE (fn) == ADDR_EXPR
4385 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4386 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4389 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4394 /* Some built-in function calls will be evaluated at
4395 compile-time in fold (). */
4396 fn = fold (build_call (fn, converted_args));
4397 if (VOID_TYPE_P (TREE_TYPE (fn)))
4399 fn = require_complete_type (fn);
4400 if (fn == error_mark_node)
4401 return error_mark_node;
4402 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4403 fn = build_cplus_new (TREE_TYPE (fn), fn);
4404 return convert_from_reference (fn);
4407 static GTY(()) tree java_iface_lookup_fn;
4409 /* Make an expression which yields the address of the Java interface
4410 method FN. This is achieved by generating a call to libjava's
4411 _Jv_LookupInterfaceMethodIdx(). */
4414 build_java_interface_fn_ref (fn, instance)
4417 tree lookup_args, lookup_fn, method, idx;
4418 tree klass_ref, iface, iface_ref;
4421 if (!java_iface_lookup_fn)
4423 tree endlink = build_void_list_node ();
4424 tree t = tree_cons (NULL_TREE, ptr_type_node,
4425 tree_cons (NULL_TREE, ptr_type_node,
4426 tree_cons (NULL_TREE, java_int_type_node,
4428 java_iface_lookup_fn
4429 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4430 build_function_type (ptr_type_node, t),
4431 0, NOT_BUILT_IN, NULL, NULL_TREE);
4434 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4435 This is the first entry in the vtable. */
4436 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4439 /* Get the java.lang.Class pointer for the interface being called. */
4440 iface = DECL_CONTEXT (fn);
4441 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4442 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4443 || DECL_CONTEXT (iface_ref) != iface)
4445 error ("could not find class$ field in java interface type `%T'",
4447 return error_mark_node;
4449 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4451 /* Determine the itable index of FN. */
4453 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4455 if (!DECL_VIRTUAL_P (method))
4461 idx = build_int_2 (i, 0);
4463 lookup_args = tree_cons (NULL_TREE, klass_ref,
4464 tree_cons (NULL_TREE, iface_ref,
4465 build_tree_list (NULL_TREE, idx)));
4466 lookup_fn = build1 (ADDR_EXPR,
4467 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4468 java_iface_lookup_fn);
4469 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4472 /* Returns the value to use for the in-charge parameter when making a
4473 call to a function with the indicated NAME. */
4476 in_charge_arg_for_name (name)
4479 if (name == base_ctor_identifier
4480 || name == base_dtor_identifier)
4481 return integer_zero_node;
4482 else if (name == complete_ctor_identifier)
4483 return integer_one_node;
4484 else if (name == complete_dtor_identifier)
4485 return integer_two_node;
4486 else if (name == deleting_dtor_identifier)
4487 return integer_three_node;
4489 /* This function should only be called with one of the names listed
4496 build_new_method_call (instance, name, args, basetype_path, flags)
4497 tree instance, name, args, basetype_path;
4500 struct z_candidate *candidates = 0, *cand;
4501 tree explicit_targs = NULL_TREE;
4502 tree basetype, mem_args = NULL_TREE, fns, instance_ptr;
4505 tree templates = NULL_TREE;
4507 int template_only = 0;
4509 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4511 explicit_targs = TREE_OPERAND (name, 1);
4512 name = TREE_OPERAND (name, 0);
4514 name = DECL_NAME (name);
4517 if (TREE_CODE (name) == COMPONENT_REF)
4518 name = TREE_OPERAND (name, 1);
4519 if (TREE_CODE (name) == OVERLOAD)
4520 name = DECL_NAME (OVL_CURRENT (name));
4527 args = resolve_args (args);
4529 if (args == error_mark_node)
4530 return error_mark_node;
4532 if (instance == NULL_TREE)
4533 basetype = BINFO_TYPE (basetype_path);
4536 if (TREE_CODE (instance) == OFFSET_REF)
4537 instance = resolve_offset_ref (instance);
4538 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4539 instance = convert_from_reference (instance);
4540 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4542 /* XXX this should be handled before we get here. */
4543 if (! IS_AGGR_TYPE (basetype))
4545 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4546 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4547 name, instance, basetype);
4549 return error_mark_node;
4553 if (basetype_path == NULL_TREE)
4554 basetype_path = TYPE_BINFO (basetype);
4558 instance_ptr = build_this (instance);
4560 if (! template_only)
4562 /* XXX this should be handled before we get here. */
4563 fns = build_field_call (basetype_path, instance_ptr, name, args);
4570 instance_ptr = build_int_2 (0, 0);
4571 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
4574 /* Callers should explicitly indicate whether they want to construct
4575 the complete object or just the part without virtual bases. */
4576 my_friendly_assert (name != ctor_identifier, 20000408);
4577 /* Similarly for destructors. */
4578 my_friendly_assert (name != dtor_identifier, 20000408);
4580 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4584 constructor_p = (name == complete_ctor_identifier
4585 || name == base_ctor_identifier);
4586 pretty_name = (constructor_p
4587 ? constructor_name (basetype) : dtor_identifier);
4589 /* If we're a call to a constructor or destructor for a
4590 subobject that uses virtual base classes, then we need to
4591 pass down a pointer to a VTT for the subobject. */
4592 if ((name == base_ctor_identifier
4593 || name == base_dtor_identifier)
4594 && TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4598 tree basebinfo = basetype_path;
4600 /* If the current function is a complete object constructor
4601 or destructor, then we fetch the VTT directly.
4602 Otherwise, we look it up using the VTT we were given. */
4603 vtt = IDENTIFIER_GLOBAL_VALUE (get_vtt_name (current_class_type));
4604 vtt = decay_conversion (vtt);
4605 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4606 build (EQ_EXPR, boolean_type_node,
4607 current_in_charge_parm, integer_zero_node),
4610 if (TREE_VIA_VIRTUAL (basebinfo))
4611 basebinfo = binfo_for_vbase (basetype, current_class_type);
4612 my_friendly_assert (BINFO_SUBVTT_INDEX (basebinfo), 20010110);
4613 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4614 BINFO_SUBVTT_INDEX (basebinfo));
4616 args = tree_cons (NULL_TREE, sub_vtt, args);
4622 fns = lookup_fnfields (basetype_path, name, 1);
4624 if (fns == error_mark_node)
4625 return error_mark_node;
4628 tree base = BINFO_TYPE (TREE_PURPOSE (fns));
4629 tree fn = TREE_VALUE (fns);
4630 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4631 for (; fn; fn = OVL_NEXT (fn))
4633 tree t = OVL_CURRENT (fn);
4636 /* We can end up here for copy-init of same or base class. */
4637 if ((flags & LOOKUP_ONLYCONVERTING)
4638 && DECL_NONCONVERTING_P (t))
4641 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4642 this_arglist = mem_args;
4644 this_arglist = args;
4646 if (TREE_CODE (t) == TEMPLATE_DECL)
4648 /* A member template. */
4649 templates = tree_cons (NULL_TREE, t, templates);
4651 add_template_candidate (candidates, t, base, explicit_targs,
4653 TREE_TYPE (name), flags, DEDUCE_CALL);
4655 else if (! template_only)
4656 candidates = add_function_candidate (candidates, t, base,
4657 this_arglist, flags);
4660 candidates->basetype_path = basetype_path;
4664 if (! any_viable (candidates))
4666 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4667 if (flags & LOOKUP_SPECULATIVELY)
4669 if (!COMPLETE_TYPE_P (basetype))
4670 cxx_incomplete_type_error (instance_ptr, basetype);
4672 error ("no matching function for call to `%T::%D(%A)%#V'",
4673 basetype, pretty_name, user_args,
4674 TREE_TYPE (TREE_TYPE (instance_ptr)));
4675 print_z_candidates (candidates);
4676 return error_mark_node;
4678 candidates = splice_viable (candidates);
4679 cand = tourney (candidates);
4683 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4685 print_z_candidates (candidates);
4686 return error_mark_node;
4689 if (DECL_PURE_VIRTUAL_P (cand->fn)
4690 && instance == current_class_ref
4691 && (DECL_CONSTRUCTOR_P (current_function_decl)
4692 || DECL_DESTRUCTOR_P (current_function_decl))
4693 && ! (flags & LOOKUP_NONVIRTUAL)
4694 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4695 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4696 "abstract virtual `%#D' called from constructor"
4697 : "abstract virtual `%#D' called from destructor"),
4699 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4700 && is_dummy_object (instance_ptr))
4702 error ("cannot call member function `%D' without object", cand->fn);
4703 return error_mark_node;
4706 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4707 && resolves_to_fixed_type_p (instance, 0))
4708 flags |= LOOKUP_NONVIRTUAL;
4710 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4711 call = build_over_call (cand, mem_args, flags);
4714 call = build_over_call (cand, args, flags);
4715 /* Do evaluate the object parameter in a call to a static member
4717 if (TREE_SIDE_EFFECTS (instance))
4718 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4724 /* Returns non-zero iff standard conversion sequence ICS1 is a proper
4725 subsequence of ICS2. */
4728 is_subseq (ics1, ics2)
4731 /* We can assume that a conversion of the same code
4732 between the same types indicates a subsequence since we only get
4733 here if the types we are converting from are the same. */
4735 while (TREE_CODE (ics1) == RVALUE_CONV
4736 || TREE_CODE (ics1) == LVALUE_CONV)
4737 ics1 = TREE_OPERAND (ics1, 0);
4741 while (TREE_CODE (ics2) == RVALUE_CONV
4742 || TREE_CODE (ics2) == LVALUE_CONV)
4743 ics2 = TREE_OPERAND (ics2, 0);
4745 if (TREE_CODE (ics2) == USER_CONV
4746 || TREE_CODE (ics2) == AMBIG_CONV
4747 || TREE_CODE (ics2) == IDENTITY_CONV)
4748 /* At this point, ICS1 cannot be a proper subsequence of
4749 ICS2. We can get a USER_CONV when we are comparing the
4750 second standard conversion sequence of two user conversion
4754 ics2 = TREE_OPERAND (ics2, 0);
4756 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4757 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4758 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4759 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4764 /* Returns non-zero iff DERIVED is derived from BASE. The inputs may
4765 be any _TYPE nodes. */
4768 is_properly_derived_from (derived, base)
4772 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4773 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4776 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4777 considers every class derived from itself. */
4778 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4779 && DERIVED_FROM_P (base, derived));
4782 /* We build the ICS for an implicit object parameter as a pointer
4783 conversion sequence. However, such a sequence should be compared
4784 as if it were a reference conversion sequence. If ICS is the
4785 implicit conversion sequence for an implicit object parameter,
4786 modify it accordingly. */
4789 maybe_handle_implicit_object (ics)
4792 if (ICS_THIS_FLAG (*ics))
4794 /* [over.match.funcs]
4796 For non-static member functions, the type of the
4797 implicit object parameter is "reference to cv X"
4798 where X is the class of which the function is a
4799 member and cv is the cv-qualification on the member
4800 function declaration. */
4802 tree reference_type;
4804 /* The `this' parameter is a pointer to a class type. Make the
4805 implict conversion talk about a reference to that same class
4807 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4808 reference_type = build_reference_type (reference_type);
4810 if (TREE_CODE (t) == QUAL_CONV)
4811 t = TREE_OPERAND (t, 0);
4812 if (TREE_CODE (t) == PTR_CONV)
4813 t = TREE_OPERAND (t, 0);
4814 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
4815 t = direct_reference_binding (reference_type, t);
4820 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
4821 and return the type to which the reference refers. Otherwise,
4822 leave *ICS unchanged and return NULL_TREE. */
4825 maybe_handle_ref_bind (ics)
4828 if (TREE_CODE (*ics) == REF_BIND)
4830 tree old_ics = *ics;
4831 tree type = TREE_TYPE (TREE_TYPE (old_ics));
4832 *ics = TREE_OPERAND (old_ics, 0);
4833 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
4834 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
4841 /* Compare two implicit conversion sequences according to the rules set out in
4842 [over.ics.rank]. Return values:
4844 1: ics1 is better than ics2
4845 -1: ics2 is better than ics1
4846 0: ics1 and ics2 are indistinguishable */
4849 compare_ics (ics1, ics2)
4856 tree deref_from_type1 = NULL_TREE;
4857 tree deref_from_type2 = NULL_TREE;
4858 tree deref_to_type1 = NULL_TREE;
4859 tree deref_to_type2 = NULL_TREE;
4862 /* REF_BINDING is non-zero if the result of the conversion sequence
4863 is a reference type. In that case TARGET_TYPE is the
4864 type referred to by the reference. */
4868 /* Handle implicit object parameters. */
4869 maybe_handle_implicit_object (&ics1);
4870 maybe_handle_implicit_object (&ics2);
4872 /* Handle reference parameters. */
4873 target_type1 = maybe_handle_ref_bind (&ics1);
4874 target_type2 = maybe_handle_ref_bind (&ics2);
4878 When comparing the basic forms of implicit conversion sequences (as
4879 defined in _over.best.ics_)
4881 --a standard conversion sequence (_over.ics.scs_) is a better
4882 conversion sequence than a user-defined conversion sequence
4883 or an ellipsis conversion sequence, and
4885 --a user-defined conversion sequence (_over.ics.user_) is a
4886 better conversion sequence than an ellipsis conversion sequence
4887 (_over.ics.ellipsis_). */
4888 rank1 = ICS_RANK (ics1);
4889 rank2 = ICS_RANK (ics2);
4893 else if (rank1 < rank2)
4896 if (rank1 == BAD_RANK)
4898 /* XXX Isn't this an extension? */
4899 /* Both ICS are bad. We try to make a decision based on what
4900 would have happenned if they'd been good. */
4901 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
4902 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
4904 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
4905 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
4908 /* We couldn't make up our minds; try to figure it out below. */
4911 if (ICS_ELLIPSIS_FLAG (ics1))
4912 /* Both conversions are ellipsis conversions. */
4915 /* User-defined conversion sequence U1 is a better conversion sequence
4916 than another user-defined conversion sequence U2 if they contain the
4917 same user-defined conversion operator or constructor and if the sec-
4918 ond standard conversion sequence of U1 is better than the second
4919 standard conversion sequence of U2. */
4921 if (ICS_USER_FLAG (ics1))
4925 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
4926 if (TREE_CODE (t1) == AMBIG_CONV)
4928 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
4929 if (TREE_CODE (t2) == AMBIG_CONV)
4932 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
4935 /* We can just fall through here, after setting up
4936 FROM_TYPE1 and FROM_TYPE2. */
4937 from_type1 = TREE_TYPE (t1);
4938 from_type2 = TREE_TYPE (t2);
4942 /* We're dealing with two standard conversion sequences.
4946 Standard conversion sequence S1 is a better conversion
4947 sequence than standard conversion sequence S2 if
4949 --S1 is a proper subsequence of S2 (comparing the conversion
4950 sequences in the canonical form defined by _over.ics.scs_,
4951 excluding any Lvalue Transformation; the identity
4952 conversion sequence is considered to be a subsequence of
4953 any non-identity conversion sequence */
4956 while (TREE_CODE (from_type1) != IDENTITY_CONV)
4957 from_type1 = TREE_OPERAND (from_type1, 0);
4958 from_type1 = TREE_TYPE (from_type1);
4961 while (TREE_CODE (from_type2) != IDENTITY_CONV)
4962 from_type2 = TREE_OPERAND (from_type2, 0);
4963 from_type2 = TREE_TYPE (from_type2);
4966 if (same_type_p (from_type1, from_type2))
4968 if (is_subseq (ics1, ics2))
4970 if (is_subseq (ics2, ics1))
4973 /* Otherwise, one sequence cannot be a subsequence of the other; they
4974 don't start with the same type. This can happen when comparing the
4975 second standard conversion sequence in two user-defined conversion
4982 --the rank of S1 is better than the rank of S2 (by the rules
4985 Standard conversion sequences are ordered by their ranks: an Exact
4986 Match is a better conversion than a Promotion, which is a better
4987 conversion than a Conversion.
4989 Two conversion sequences with the same rank are indistinguishable
4990 unless one of the following rules applies:
4992 --A conversion that is not a conversion of a pointer, or pointer
4993 to member, to bool is better than another conversion that is such
4996 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
4997 so that we do not have to check it explicitly. */
4998 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5000 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5003 to_type1 = TREE_TYPE (ics1);
5004 to_type2 = TREE_TYPE (ics2);
5006 if (TYPE_PTR_P (from_type1)
5007 && TYPE_PTR_P (from_type2)
5008 && TYPE_PTR_P (to_type1)
5009 && TYPE_PTR_P (to_type2))
5011 deref_from_type1 = TREE_TYPE (from_type1);
5012 deref_from_type2 = TREE_TYPE (from_type2);
5013 deref_to_type1 = TREE_TYPE (to_type1);
5014 deref_to_type2 = TREE_TYPE (to_type2);
5016 /* The rules for pointers to members A::* are just like the rules
5017 for pointers A*, except opposite: if B is derived from A then
5018 A::* converts to B::*, not vice versa. For that reason, we
5019 switch the from_ and to_ variables here. */
5020 else if (TYPE_PTRMEM_P (from_type1)
5021 && TYPE_PTRMEM_P (from_type2)
5022 && TYPE_PTRMEM_P (to_type1)
5023 && TYPE_PTRMEM_P (to_type2))
5025 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5026 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5027 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5028 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5030 else if (TYPE_PTRMEMFUNC_P (from_type1)
5031 && TYPE_PTRMEMFUNC_P (from_type2)
5032 && TYPE_PTRMEMFUNC_P (to_type1)
5033 && TYPE_PTRMEMFUNC_P (to_type2))
5035 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5036 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5037 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5038 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5041 if (deref_from_type1 != NULL_TREE
5042 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5043 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5045 /* This was one of the pointer or pointer-like conversions.
5049 --If class B is derived directly or indirectly from class A,
5050 conversion of B* to A* is better than conversion of B* to
5051 void*, and conversion of A* to void* is better than
5052 conversion of B* to void*. */
5053 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5054 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5056 if (is_properly_derived_from (deref_from_type1,
5059 else if (is_properly_derived_from (deref_from_type2,
5063 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5064 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5066 if (same_type_p (deref_from_type1, deref_from_type2))
5068 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5070 if (is_properly_derived_from (deref_from_type1,
5074 /* We know that DEREF_TO_TYPE1 is `void' here. */
5075 else if (is_properly_derived_from (deref_from_type1,
5080 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5081 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5085 --If class B is derived directly or indirectly from class A
5086 and class C is derived directly or indirectly from B,
5088 --conversion of C* to B* is better than conversion of C* to
5091 --conversion of B* to A* is better than conversion of C* to
5093 if (same_type_p (deref_from_type1, deref_from_type2))
5095 if (is_properly_derived_from (deref_to_type1,
5098 else if (is_properly_derived_from (deref_to_type2,
5102 else if (same_type_p (deref_to_type1, deref_to_type2))
5104 if (is_properly_derived_from (deref_from_type2,
5107 else if (is_properly_derived_from (deref_from_type1,
5113 else if (CLASS_TYPE_P (non_reference (from_type1))
5114 && same_type_p (from_type1, from_type2))
5116 tree from = non_reference (from_type1);
5120 --binding of an expression of type C to a reference of type
5121 B& is better than binding an expression of type C to a
5122 reference of type A&
5124 --conversion of C to B is better than conversion of C to A, */
5125 if (is_properly_derived_from (from, to_type1)
5126 && is_properly_derived_from (from, to_type2))
5128 if (is_properly_derived_from (to_type1, to_type2))
5130 else if (is_properly_derived_from (to_type2, to_type1))
5134 else if (CLASS_TYPE_P (non_reference (to_type1))
5135 && same_type_p (to_type1, to_type2))
5137 tree to = non_reference (to_type1);
5141 --binding of an expression of type B to a reference of type
5142 A& is better than binding an expression of type C to a
5143 reference of type A&,
5145 --onversion of B to A is better than conversion of C to A */
5146 if (is_properly_derived_from (from_type1, to)
5147 && is_properly_derived_from (from_type2, to))
5149 if (is_properly_derived_from (from_type2, from_type1))
5151 else if (is_properly_derived_from (from_type1, from_type2))
5158 --S1 and S2 differ only in their qualification conversion and yield
5159 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5160 qualification signature of type T1 is a proper subset of the cv-
5161 qualification signature of type T2 */
5162 if (TREE_CODE (ics1) == QUAL_CONV
5163 && TREE_CODE (ics2) == QUAL_CONV
5164 && same_type_p (from_type1, from_type2))
5165 return comp_cv_qual_signature (to_type1, to_type2);
5169 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5170 types to which the references refer are the same type except for
5171 top-level cv-qualifiers, and the type to which the reference
5172 initialized by S2 refers is more cv-qualified than the type to
5173 which the reference initialized by S1 refers */
5175 if (target_type1 && target_type2
5176 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5177 return comp_cv_qualification (target_type2, target_type1);
5179 /* Neither conversion sequence is better than the other. */
5183 /* The source type for this standard conversion sequence. */
5189 for (;; t = TREE_OPERAND (t, 0))
5191 if (TREE_CODE (t) == USER_CONV
5192 || TREE_CODE (t) == AMBIG_CONV
5193 || TREE_CODE (t) == IDENTITY_CONV)
5194 return TREE_TYPE (t);
5199 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5200 a pointer to LOSER and re-running joust to produce the warning if WINNER
5201 is actually used. */
5204 add_warning (winner, loser)
5205 struct z_candidate *winner, *loser;
5207 winner->warnings = tree_cons (NULL_TREE,
5208 build_zc_wrapper (loser),
5212 /* Returns true iff functions are equivalent. Equivalent functions are
5213 not '==' only if one is a function-local extern function or if
5214 both are extern "C". */
5217 equal_functions (fn1, fn2)
5221 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5222 || DECL_EXTERN_C_FUNCTION_P (fn1))
5223 return decls_match (fn1, fn2);
5227 /* Compare two candidates for overloading as described in
5228 [over.match.best]. Return values:
5230 1: cand1 is better than cand2
5231 -1: cand2 is better than cand1
5232 0: cand1 and cand2 are indistinguishable */
5235 joust (cand1, cand2, warn)
5236 struct z_candidate *cand1, *cand2;
5240 int i, off1 = 0, off2 = 0, len;
5242 /* Candidates that involve bad conversions are always worse than those
5244 if (cand1->viable > cand2->viable)
5246 if (cand1->viable < cand2->viable)
5249 /* If we have two pseudo-candidates for conversions to the same type,
5250 or two candidates for the same function, arbitrarily pick one. */
5251 if (cand1->fn == cand2->fn
5252 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5255 /* a viable function F1
5256 is defined to be a better function than another viable function F2 if
5257 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5258 ICSi(F2), and then */
5260 /* for some argument j, ICSj(F1) is a better conversion sequence than
5263 /* For comparing static and non-static member functions, we ignore
5264 the implicit object parameter of the non-static function. The
5265 standard says to pretend that the static function has an object
5266 parm, but that won't work with operator overloading. */
5267 len = TREE_VEC_LENGTH (cand1->convs);
5268 if (len != TREE_VEC_LENGTH (cand2->convs))
5270 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5271 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5273 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5274 && DECL_STATIC_FUNCTION_P (cand2->fn))
5283 for (i = 0; i < len; ++i)
5285 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5286 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5287 int comp = compare_ics (t1, t2);
5292 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5293 && TREE_CODE (t1) == STD_CONV
5294 && TREE_CODE (t2) == STD_CONV
5295 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5296 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5297 && (TYPE_PRECISION (TREE_TYPE (t1))
5298 == TYPE_PRECISION (TREE_TYPE (t2)))
5299 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5300 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5303 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5305 struct z_candidate *w, *l;
5307 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5308 w = cand1, l = cand2;
5310 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5311 w = cand2, l = cand1;
5315 warning ("passing `%T' chooses `%T' over `%T'",
5316 type, type1, type2);
5317 warning (" in call to `%D'", w->fn);
5323 if (winner && comp != winner)
5332 /* warn about confusing overload resolution for user-defined conversions,
5333 either between a constructor and a conversion op, or between two
5335 if (winner && cand1->second_conv
5336 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5337 != DECL_CONSTRUCTOR_P (cand2->fn))
5338 /* Don't warn if the two conv ops convert to the same type... */
5339 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5340 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5341 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5343 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5346 struct z_candidate *w, *l;
5349 w = cand1, l = cand2;
5351 w = cand2, l = cand1;
5352 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5353 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5354 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5355 && (convn = standard_conversion
5356 (TREE_TYPE (TREE_TYPE (l->fn)),
5357 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5358 && TREE_CODE (convn) == QUAL_CONV)
5359 /* Don't complain about `operator char *()' beating
5360 `operator const char *() const'. */;
5363 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5364 if (! DECL_CONSTRUCTOR_P (w->fn))
5365 source = TREE_TYPE (source);
5366 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5367 warning (" for conversion from `%T' to `%T'",
5368 source, TREE_TYPE (w->second_conv));
5369 warning (" because conversion sequence for the argument is better");
5380 F1 is a non-template function and F2 is a template function
5383 if (! cand1->template && cand2->template)
5385 else if (cand1->template && ! cand2->template)
5389 F1 and F2 are template functions and the function template for F1 is
5390 more specialized than the template for F2 according to the partial
5393 if (cand1->template && cand2->template)
5395 winner = more_specialized
5396 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5398 /* Tell the deduction code how many real function arguments
5399 we saw, not counting the implicit 'this' argument. But,
5400 add_function_candidate() suppresses the "this" argument
5403 [temp.func.order]: The presence of unused ellipsis and default
5404 arguments has no effect on the partial ordering of function
5406 TREE_VEC_LENGTH (cand1->convs)
5407 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5408 - DECL_CONSTRUCTOR_P (cand1->fn)));
5415 the context is an initialization by user-defined conversion (see
5416 _dcl.init_ and _over.match.user_) and the standard conversion
5417 sequence from the return type of F1 to the destination type (i.e.,
5418 the type of the entity being initialized) is a better conversion
5419 sequence than the standard conversion sequence from the return type
5420 of F2 to the destination type. */
5422 if (cand1->second_conv)
5424 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5429 /* Check whether we can discard a builtin candidate, either because we
5430 have two identical ones or matching builtin and non-builtin candidates.
5432 (Pedantically in the latter case the builtin which matched the user
5433 function should not be added to the overload set, but we spot it here.
5436 ... the builtin candidates include ...
5437 - do not have the same parameter type list as any non-template
5438 non-member candidate. */
5440 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5441 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5443 for (i = 0; i < len; ++i)
5444 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5445 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5447 if (i == TREE_VEC_LENGTH (cand1->convs))
5449 if (cand1->fn == cand2->fn)
5450 /* Two built-in candidates; arbitrarily pick one. */
5452 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5453 /* cand1 is built-in; prefer cand2. */
5456 /* cand2 is built-in; prefer cand1. */
5461 /* If the two functions are the same (this can happen with declarations
5462 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5463 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5464 && equal_functions (cand1->fn, cand2->fn))
5469 /* Extension: If the worst conversion for one candidate is worse than the
5470 worst conversion for the other, take the first. */
5473 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5474 struct z_candidate *w = 0, *l = 0;
5476 for (i = 0; i < len; ++i)
5478 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5479 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5480 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5481 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5484 winner = 1, w = cand1, l = cand2;
5486 winner = -1, w = cand2, l = cand1;
5491 pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5493 " because worst conversion for the former is better than worst conversion for the latter");
5501 my_friendly_assert (!winner, 20010121);
5505 /* Given a list of candidates for overloading, find the best one, if any.
5506 This algorithm has a worst case of O(2n) (winner is last), and a best
5507 case of O(n/2) (totally ambiguous); much better than a sorting
5510 static struct z_candidate *
5511 tourney (candidates)
5512 struct z_candidate *candidates;
5514 struct z_candidate *champ = candidates, *challenger;
5516 int champ_compared_to_predecessor = 0;
5518 /* Walk through the list once, comparing each current champ to the next
5519 candidate, knocking out a candidate or two with each comparison. */
5521 for (challenger = champ->next; challenger; )
5523 fate = joust (champ, challenger, 0);
5525 challenger = challenger->next;
5530 champ = challenger->next;
5533 champ_compared_to_predecessor = 0;
5538 champ_compared_to_predecessor = 1;
5541 challenger = champ->next;
5545 /* Make sure the champ is better than all the candidates it hasn't yet
5546 been compared to. */
5548 for (challenger = candidates;
5550 && !(champ_compared_to_predecessor && challenger->next == champ);
5551 challenger = challenger->next)
5553 fate = joust (champ, challenger, 0);
5561 /* Returns non-zero if things of type FROM can be converted to TO. */
5564 can_convert (to, from)
5567 return can_convert_arg (to, from, NULL_TREE);
5570 /* Returns non-zero if ARG (of type FROM) can be converted to TO. */
5573 can_convert_arg (to, from, arg)
5576 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5577 return (t && ! ICS_BAD_FLAG (t));
5580 /* Like can_convert_arg, but allows dubious conversions as well. */
5583 can_convert_arg_bad (to, from, arg)
5586 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5590 /* Convert EXPR to TYPE. Return the converted expression.
5592 Note that we allow bad conversions here because by the time we get to
5593 this point we are committed to doing the conversion. If we end up
5594 doing a bad conversion, convert_like will complain. */
5597 perform_implicit_conversion (type, expr)
5603 if (expr == error_mark_node)
5604 return error_mark_node;
5605 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5609 error ("could not convert `%E' to `%T'", expr, type);
5610 return error_mark_node;
5613 return convert_like (conv, expr);
5616 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5617 initializing a variable of that TYPE. Return the converted
5621 initialize_reference (type, expr)
5627 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5628 if (!conv || ICS_BAD_FLAG (conv))
5630 error ("could not convert `%E' to `%T'", expr, type);
5631 return error_mark_node;
5634 return convert_like (conv, expr);
5637 #include "gt-cp-call.h"