1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) and
5 modified by Brendan Kehoe (brendan@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 /* High-level class interface. */
29 #include "coretypes.h"
39 #include "diagnostic.h"
41 extern int inhibit_warnings;
43 static tree build_field_call PARAMS ((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,
68 tree, tree, int, unification_kind_t));
69 static struct z_candidate * add_template_candidate_real
70 PARAMS ((struct z_candidate *, tree, tree, tree, tree, tree,
71 tree, tree, int, tree, unification_kind_t));
72 static struct z_candidate * add_template_conv_candidate
73 PARAMS ((struct z_candidate *, tree, tree, 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, tree, tree));
86 static struct z_candidate * add_function_candidate
87 (struct z_candidate *, tree, tree, 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
97 (struct z_candidate *, tree, tree, tree, tree, int);
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));
106 static tree call_builtin_trap PARAMS ((void));
109 build_vfield_ref (datum, type)
112 if (datum == error_mark_node)
113 return error_mark_node;
115 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
116 datum = convert_from_reference (datum);
118 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
119 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
120 datum = convert_to_base (datum, type, /*check_access=*/false);
122 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
123 datum, TYPE_VFIELD (type));
126 /* Build a call to a member of an object. I.e., one that overloads
127 operator ()(), or is a pointer-to-function or pointer-to-method. */
130 build_field_call (tree instance_ptr, tree decl, tree parms)
134 if (decl == error_mark_node || decl == NULL_TREE)
137 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
139 /* If it's a field, try overloading operator (),
140 or calling if the field is a pointer-to-function. */
141 instance = build_indirect_ref (instance_ptr, NULL);
142 instance = build_class_member_access_expr (instance, decl,
143 /*access_path=*/NULL_TREE,
144 /*preserve_reference=*/false);
146 if (instance == error_mark_node)
147 return error_mark_node;
149 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
150 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
151 instance, parms, NULL_TREE);
152 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
153 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
154 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
156 return build_function_call (instance, parms);
162 /* Returns nonzero iff the destructor name specified in NAME
163 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
167 check_dtor_name (basetype, name)
170 name = TREE_OPERAND (name, 0);
172 /* Just accept something we've already complained about. */
173 if (name == error_mark_node)
176 if (TREE_CODE (name) == TYPE_DECL)
177 name = TREE_TYPE (name);
178 else if (TYPE_P (name))
180 else if (TREE_CODE (name) == IDENTIFIER_NODE)
182 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
183 || (TREE_CODE (basetype) == ENUMERAL_TYPE
184 && name == TYPE_IDENTIFIER (basetype)))
187 name = get_type_value (name);
191 template <class T> struct S { ~S(); };
195 NAME will be a class template. */
196 else if (DECL_CLASS_TEMPLATE_P (name))
201 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
206 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
207 This is how virtual function calls are avoided. */
210 build_scoped_method_call (exp, basetype, name, parms)
211 tree exp, basetype, name, parms;
213 /* Because this syntactic form does not allow
214 a pointer to a base class to be `stolen',
215 we need not protect the derived->base conversion
218 @@ But we do have to check access privileges later. */
220 tree type = TREE_TYPE (exp);
222 if (type == error_mark_node
223 || basetype == error_mark_node)
224 return error_mark_node;
226 if (processing_template_decl)
228 if (TREE_CODE (name) == BIT_NOT_EXPR
229 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
231 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
233 name = build_min_nt (BIT_NOT_EXPR, type);
235 name = build_min_nt (SCOPE_REF, basetype, name);
236 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
239 if (TREE_CODE (type) == REFERENCE_TYPE)
240 type = TREE_TYPE (type);
242 if (TREE_CODE (basetype) == TREE_VEC)
245 basetype = BINFO_TYPE (binfo);
250 /* Check the destructor call syntax. */
251 if (TREE_CODE (name) == BIT_NOT_EXPR)
253 /* We can get here if someone writes their destructor call like
254 `obj.NS::~T()'; this isn't really a scoped method call, so hand
256 if (TREE_CODE (basetype) == NAMESPACE_DECL)
257 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
259 if (! check_dtor_name (basetype, name))
260 error ("qualified type `%T' does not match destructor name `~%T'",
261 basetype, TREE_OPERAND (name, 0));
263 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
264 that explicit ~int is caught in the parser; this deals with typedefs
265 and template parms. */
266 if (! IS_AGGR_TYPE (basetype))
268 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
269 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
270 exp, basetype, type);
272 return cp_convert (void_type_node, exp);
276 if (TREE_CODE (basetype) == NAMESPACE_DECL)
278 error ("`%D' is a namespace", basetype);
279 return error_mark_node;
281 if (! is_aggr_type (basetype, 1))
282 return error_mark_node;
284 if (! IS_AGGR_TYPE (type))
286 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
288 return error_mark_node;
291 decl = build_scoped_ref (exp, basetype, &binfo);
295 /* Call to a destructor. */
296 if (TREE_CODE (name) == BIT_NOT_EXPR)
298 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
299 return cp_convert (void_type_node, exp);
301 return build_delete (TREE_TYPE (decl), decl,
302 sfk_complete_destructor,
303 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
307 /* Call to a method. */
308 return build_method_call (decl, name, parms, binfo,
309 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
311 return error_mark_node;
314 /* We want the address of a function or method. We avoid creating a
315 pointer-to-member function. */
318 build_addr_func (function)
321 tree type = TREE_TYPE (function);
323 /* We have to do these by hand to avoid real pointer to member
325 if (TREE_CODE (type) == METHOD_TYPE)
329 type = build_pointer_type (type);
331 if (!cxx_mark_addressable (function))
332 return error_mark_node;
334 addr = build1 (ADDR_EXPR, type, function);
336 /* Address of a static or external variable or function counts
338 if (staticp (function))
339 TREE_CONSTANT (addr) = 1;
344 function = default_conversion (function);
349 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
350 POINTER_TYPE to those. Note, pointer to member function types
351 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
354 build_call (function, parms)
355 tree function, parms;
357 int is_constructor = 0;
364 function = build_addr_func (function);
366 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
368 sorry ("unable to call pointer to member function here");
369 return error_mark_node;
372 fntype = TREE_TYPE (TREE_TYPE (function));
373 result_type = TREE_TYPE (fntype);
375 if (TREE_CODE (function) == ADDR_EXPR
376 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
377 decl = TREE_OPERAND (function, 0);
381 /* We check both the decl and the type; a function may be known not to
382 throw without being declared throw(). */
383 nothrow = ((decl && TREE_NOTHROW (decl))
384 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
386 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
387 current_function_returns_abnormally = 1;
389 if (decl && TREE_DEPRECATED (decl))
390 warn_deprecated_use (decl);
391 require_complete_eh_spec_types (fntype, decl);
393 if (decl && DECL_CONSTRUCTOR_P (decl))
396 if (decl && ! TREE_USED (decl))
398 /* We invoke build_call directly for several library functions.
399 These may have been declared normally if we're building libgcc,
400 so we can't just check DECL_ARTIFICIAL. */
401 if (DECL_ARTIFICIAL (decl)
402 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
408 /* Don't pass empty class objects by value. This is useful
409 for tags in STL, which are used to control overload resolution.
410 We don't need to handle other cases of copying empty classes. */
411 if (! decl || ! DECL_BUILT_IN (decl))
412 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
413 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
414 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
416 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
417 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
418 TREE_VALUE (tmp), t);
421 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
422 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
423 TREE_TYPE (function) = result_type;
424 TREE_SIDE_EFFECTS (function) = 1;
425 TREE_NOTHROW (function) = nothrow;
430 /* Build something of the form ptr->method (args)
431 or object.method (args). This can also build
432 calls to constructors, and find friends.
434 Member functions always take their class variable
437 INSTANCE is a class instance.
439 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
441 PARMS help to figure out what that NAME really refers to.
443 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
444 down to the real instance type to use for access checking. We need this
445 information to get protected accesses correct. This parameter is used
446 by build_member_call.
448 FLAGS is the logical disjunction of zero or more LOOKUP_
449 flags. See cp-tree.h for more info.
451 If this is all OK, calls build_function_call with the resolved
454 This function must also handle being called to perform
455 initialization, promotion/coercion of arguments, and
456 instantiation of default parameters.
458 Note that NAME may refer to an instance variable name. If
459 `operator()()' is defined for the type of that field, then we return
462 #ifdef GATHER_STATISTICS
463 extern int n_build_method_call;
467 build_method_call (instance, name, parms, basetype_path, flags)
468 tree instance, name, parms, basetype_path;
473 tree template_args = NULL_TREE;
474 bool has_template_args = false;
476 #ifdef GATHER_STATISTICS
477 n_build_method_call++;
480 if (instance == error_mark_node
481 || name == error_mark_node
482 || parms == error_mark_node
483 || (instance && TREE_TYPE (instance) == error_mark_node))
484 return error_mark_node;
486 if (processing_template_decl)
488 /* We need to process template parm names here so that tsubst catches
489 them properly. Other type names can wait. */
490 if (TREE_CODE (name) == BIT_NOT_EXPR)
492 tree type = NULL_TREE;
494 if (TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
495 type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 0);
496 else if (TREE_CODE (TREE_OPERAND (name, 0)) == TYPE_DECL)
497 type = TREE_TYPE (TREE_OPERAND (name, 0));
499 if (type && TREE_CODE (type) == TEMPLATE_TYPE_PARM)
500 name = build_min_nt (BIT_NOT_EXPR, type);
503 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
506 if (TREE_CODE (instance) == OFFSET_REF)
507 instance = resolve_offset_ref (instance);
508 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
509 instance = convert_from_reference (instance);
510 object_type = TREE_TYPE (instance);
512 if (TREE_CODE (name) == BIT_NOT_EXPR)
517 error ("destructors take no parameters");
519 if (! check_dtor_name (object_type, name))
521 ("destructor name `~%T' does not match type `%T' of expression",
522 TREE_OPERAND (name, 0), object_type);
524 if (! TYPE_HAS_DESTRUCTOR (complete_type (object_type)))
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 (object_type),
529 instance_ptr, sfk_complete_destructor,
530 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
533 if (!CLASS_TYPE_P (object_type))
535 if ((flags & LOOKUP_COMPLAIN)
536 && TREE_TYPE (instance) != error_mark_node)
537 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
538 name, instance, object_type);
539 return error_mark_node;
542 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
544 template_args = TREE_OPERAND (name, 1);
545 has_template_args = true;
546 name = TREE_OPERAND (name, 0);
548 if (TREE_CODE (name) == OVERLOAD)
549 name = DECL_NAME (get_first_fn (name));
550 else if (DECL_P (name))
551 name = DECL_NAME (name);
552 if (has_template_args)
553 fn = lookup_fnfields (object_type, name, /*protect=*/2);
555 fn = lookup_member (object_type, name, /*protect=*/2, /*want_type=*/0);
557 if (fn && TREE_CODE (fn) == TREE_LIST && !BASELINK_P (fn))
559 error ("request for member `%D' is ambiguous", name);
560 print_candidates (fn);
561 return error_mark_node;
564 /* If the name could not be found, issue an error. */
567 unqualified_name_lookup_error (name);
568 return error_mark_node;
571 if (BASELINK_P (fn) && has_template_args)
572 BASELINK_FUNCTIONS (fn)
573 = build_nt (TEMPLATE_ID_EXPR,
574 BASELINK_FUNCTIONS (fn),
576 if (BASELINK_P (fn) && basetype_path)
577 BASELINK_ACCESS_BINFO (fn) = basetype_path;
579 return build_new_method_call (instance, fn, parms,
580 /*conversion_path=*/NULL_TREE, flags);
583 /* New overloading code. */
585 struct z_candidate GTY(()) {
586 /* The FUNCTION_DECL that will be called if this candidate is
587 selected by overload resolution. */
592 /* If FN is a member function, the binfo indicating the path used to
593 qualify the name of FN at the call site. This path is used to
594 determine whether or not FN is accessible if it is selected by
595 overload resolution. The DECL_CONTEXT of FN will always be a
596 (possibly improper) base of this binfo. */
598 /* If FN is a non-static member function, the binfo indicating the
599 subobject to which the `this' pointer should be converted if FN
600 is selected by overload resolution. The type pointed to the by
601 the `this' pointer must correspond to the most derived class
602 indicated by the CONVERSION_PATH. */
603 tree conversion_path;
606 struct z_candidate *next;
609 #define IDENTITY_RANK 0
615 #define ELLIPSIS_RANK 6
618 #define ICS_RANK(NODE) \
619 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
620 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
621 : ICS_USER_FLAG (NODE) ? USER_RANK \
622 : ICS_STD_RANK (NODE))
624 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
626 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
627 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
628 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
629 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
631 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
632 should be created to hold the result of the conversion. */
633 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
635 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
636 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
644 A null pointer constant is an integral constant expression
645 (_expr.const_) rvalue of integer type that evaluates to zero. */
647 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
653 /* Returns nonzero if PARMLIST consists of only default parms and/or
657 sufficient_parms_p (parmlist)
660 for (; parmlist && parmlist != void_list_node;
661 parmlist = TREE_CHAIN (parmlist))
662 if (!TREE_PURPOSE (parmlist))
668 build_conv (code, type, from)
673 int rank = ICS_STD_RANK (from);
675 /* We can't use buildl1 here because CODE could be USER_CONV, which
676 takes two arguments. In that case, the caller is responsible for
677 filling in the second argument. */
678 t = make_node (code);
679 TREE_TYPE (t) = type;
680 TREE_OPERAND (t, 0) = from;
693 if (rank < EXACT_RANK)
699 ICS_STD_RANK (t) = rank;
700 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
701 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
705 /* If T is a REFERENCE_TYPE return the type to which T refers.
706 Otherwise, return T itself. */
712 if (TREE_CODE (t) == REFERENCE_TYPE)
721 if (TREE_CODE (t) == ARRAY_TYPE)
723 return TYPE_MAIN_VARIANT (t);
726 /* Returns the standard conversion path (see [conv]) from type FROM to type
727 TO, if any. For proper handling of null pointer constants, you must
728 also pass the expression EXPR to convert from. */
731 standard_conversion (to, from, expr)
734 enum tree_code fcode, tcode;
738 if (TREE_CODE (to) == REFERENCE_TYPE)
740 if (TREE_CODE (from) == REFERENCE_TYPE)
743 from = TREE_TYPE (from);
745 to = strip_top_quals (to);
746 from = strip_top_quals (from);
748 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
749 && expr && type_unknown_p (expr))
751 expr = instantiate_type (to, expr, tf_none);
752 if (expr == error_mark_node)
754 from = TREE_TYPE (expr);
757 fcode = TREE_CODE (from);
758 tcode = TREE_CODE (to);
760 conv = build1 (IDENTITY_CONV, from, expr);
762 if (fcode == FUNCTION_TYPE)
764 from = build_pointer_type (from);
765 fcode = TREE_CODE (from);
766 conv = build_conv (LVALUE_CONV, from, conv);
768 else if (fcode == ARRAY_TYPE)
770 from = build_pointer_type (TREE_TYPE (from));
771 fcode = TREE_CODE (from);
772 conv = build_conv (LVALUE_CONV, from, conv);
774 else if (fromref || (expr && lvalue_p (expr)))
775 conv = build_conv (RVALUE_CONV, from, conv);
777 /* Allow conversion between `__complex__' data types */
778 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
780 /* The standard conversion sequence to convert FROM to TO is
781 the standard conversion sequence to perform componentwise
783 tree part_conv = standard_conversion
784 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
788 conv = build_conv (TREE_CODE (part_conv), to, conv);
789 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
797 if (same_type_p (from, to))
800 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
801 && expr && null_ptr_cst_p (expr))
803 conv = build_conv (STD_CONV, to, conv);
805 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
806 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
808 /* For backwards brain damage compatibility, allow interconversion of
809 pointers and integers with a pedwarn. */
810 conv = build_conv (STD_CONV, to, conv);
811 ICS_BAD_FLAG (conv) = 1;
813 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
814 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
816 /* For backwards brain damage compatibility, allow interconversion of
817 enums and integers with a pedwarn. */
818 conv = build_conv (STD_CONV, to, conv);
819 ICS_BAD_FLAG (conv) = 1;
821 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
823 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
824 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
826 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
829 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
830 && ufcode != FUNCTION_TYPE)
832 from = build_pointer_type
833 (cp_build_qualified_type (void_type_node,
834 cp_type_quals (TREE_TYPE (from))));
835 conv = build_conv (PTR_CONV, from, conv);
837 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
839 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
840 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
842 if (DERIVED_FROM_P (fbase, tbase)
843 && (same_type_ignoring_top_level_qualifiers_p
844 (TREE_TYPE (TREE_TYPE (from)),
845 TREE_TYPE (TREE_TYPE (to)))))
847 from = build_ptrmem_type (tbase, TREE_TYPE (TREE_TYPE (from)));
848 conv = build_conv (PMEM_CONV, from, conv);
851 else if (IS_AGGR_TYPE (TREE_TYPE (from))
852 && IS_AGGR_TYPE (TREE_TYPE (to)))
854 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
857 cp_build_qualified_type (TREE_TYPE (to),
858 cp_type_quals (TREE_TYPE (from)));
859 from = build_pointer_type (from);
860 conv = build_conv (PTR_CONV, from, conv);
864 if (same_type_p (from, to))
866 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
867 conv = build_conv (QUAL_CONV, to, conv);
868 else if (expr && string_conv_p (to, expr, 0))
869 /* converting from string constant to char *. */
870 conv = build_conv (QUAL_CONV, to, conv);
871 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
873 conv = build_conv (PTR_CONV, to, conv);
874 ICS_BAD_FLAG (conv) = 1;
881 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
883 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
884 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
885 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
886 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
888 if (!DERIVED_FROM_P (fbase, tbase)
889 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
890 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
891 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
892 || cp_type_quals (fbase) != cp_type_quals (tbase))
895 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
896 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
897 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
898 from = build_ptrmemfunc_type (build_pointer_type (from));
899 conv = build_conv (PMEM_CONV, from, conv);
901 else if (tcode == BOOLEAN_TYPE)
903 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
904 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
907 conv = build_conv (STD_CONV, to, conv);
908 if (fcode == POINTER_TYPE
909 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
910 ICS_STD_RANK (conv) = PBOOL_RANK;
912 /* We don't check for ENUMERAL_TYPE here because there are no standard
913 conversions to enum type. */
914 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
915 || tcode == REAL_TYPE)
917 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
919 conv = build_conv (STD_CONV, to, conv);
921 /* Give this a better rank if it's a promotion. */
922 if (to == type_promotes_to (from)
923 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
924 ICS_STD_RANK (conv) = PROMO_RANK;
926 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
927 && is_properly_derived_from (from, to))
929 if (TREE_CODE (conv) == RVALUE_CONV)
930 conv = TREE_OPERAND (conv, 0);
931 conv = build_conv (BASE_CONV, to, conv);
932 /* The derived-to-base conversion indicates the initialization
933 of a parameter with base type from an object of a derived
934 type. A temporary object is created to hold the result of
936 NEED_TEMPORARY_P (conv) = 1;
944 /* Returns nonzero if T1 is reference-related to T2. */
947 reference_related_p (t1, t2)
951 t1 = TYPE_MAIN_VARIANT (t1);
952 t2 = TYPE_MAIN_VARIANT (t2);
956 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
957 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
959 return (same_type_p (t1, t2)
960 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
961 && DERIVED_FROM_P (t1, t2)));
964 /* Returns nonzero if T1 is reference-compatible with T2. */
967 reference_compatible_p (t1, t2)
973 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
974 reference-related to T2 and cv1 is the same cv-qualification as,
975 or greater cv-qualification than, cv2. */
976 return (reference_related_p (t1, t2)
977 && at_least_as_qualified_p (t1, t2));
980 /* Determine whether or not the EXPR (of class type S) can be
981 converted to T as in [over.match.ref]. */
984 convert_class_to_reference (t, s, expr)
992 struct z_candidate *candidates;
993 struct z_candidate *cand;
997 Assuming that "cv1 T" is the underlying type of the reference
998 being initialized, and "cv S" is the type of the initializer
999 expression, with S a class type, the candidate functions are
1000 selected as follows:
1002 --The conversion functions of S and its base classes are
1003 considered. Those that are not hidden within S and yield type
1004 "reference to cv2 T2", where "cv1 T" is reference-compatible
1005 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
1007 The argument list has one argument, which is the initializer
1012 /* Conceptually, we should take the address of EXPR and put it in
1013 the argument list. Unfortunately, however, that can result in
1014 error messages, which we should not issue now because we are just
1015 trying to find a conversion operator. Therefore, we use NULL,
1016 cast to the appropriate type. */
1017 arglist = build_int_2 (0, 0);
1018 TREE_TYPE (arglist) = build_pointer_type (s);
1019 arglist = build_tree_list (NULL_TREE, arglist);
1021 for (conversions = lookup_conversions (s);
1023 conversions = TREE_CHAIN (conversions))
1025 tree fns = TREE_VALUE (conversions);
1027 for (; fns; fns = OVL_NEXT (fns))
1029 tree f = OVL_CURRENT (fns);
1030 tree t2 = TREE_TYPE (TREE_TYPE (f));
1031 struct z_candidate *old_candidates = candidates;
1033 /* If this is a template function, try to get an exact
1035 if (TREE_CODE (f) == TEMPLATE_DECL)
1038 = add_template_candidate (candidates,
1042 build_reference_type (t),
1044 TREE_PURPOSE (conversions),
1048 if (candidates != old_candidates)
1050 /* Now, see if the conversion function really returns
1051 an lvalue of the appropriate type. From the
1052 point of view of unification, simply returning an
1053 rvalue of the right type is good enough. */
1055 t2 = TREE_TYPE (TREE_TYPE (f));
1056 if (TREE_CODE (t2) != REFERENCE_TYPE
1057 || !reference_compatible_p (t, TREE_TYPE (t2)))
1058 candidates = candidates->next;
1061 else if (TREE_CODE (t2) == REFERENCE_TYPE
1062 && reference_compatible_p (t, TREE_TYPE (t2)))
1064 = add_function_candidate (candidates, f, s, arglist,
1066 TREE_PURPOSE (conversions),
1071 /* If none of the conversion functions worked out, let our caller
1073 if (!any_viable (candidates))
1076 candidates = splice_viable (candidates);
1077 cand = tourney (candidates);
1081 conv = build1 (IDENTITY_CONV, s, expr);
1082 conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1084 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
1085 ICS_USER_FLAG (conv) = 1;
1086 if (cand->viable == -1)
1087 ICS_BAD_FLAG (conv) = 1;
1088 cand->second_conv = conv;
1093 /* A reference of the indicated TYPE is being bound directly to the
1094 expression represented by the implicit conversion sequence CONV.
1095 Return a conversion sequence for this binding. */
1098 direct_reference_binding (type, conv)
1102 tree t = TREE_TYPE (type);
1106 When a parameter of reference type binds directly
1107 (_dcl.init.ref_) to an argument expression, the implicit
1108 conversion sequence is the identity conversion, unless the
1109 argument expression has a type that is a derived class of the
1110 parameter type, in which case the implicit conversion sequence is
1111 a derived-to-base Conversion.
1113 If the parameter binds directly to the result of applying a
1114 conversion function to the argument expression, the implicit
1115 conversion sequence is a user-defined conversion sequence
1116 (_over.ics.user_), with the second standard conversion sequence
1117 either an identity conversion or, if the conversion function
1118 returns an entity of a type that is a derived class of the
1119 parameter type, a derived-to-base conversion. */
1120 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1122 /* Represent the derived-to-base conversion. */
1123 conv = build_conv (BASE_CONV, t, conv);
1124 /* We will actually be binding to the base-class subobject in
1125 the derived class, so we mark this conversion appropriately.
1126 That way, convert_like knows not to generate a temporary. */
1127 NEED_TEMPORARY_P (conv) = 0;
1129 return build_conv (REF_BIND, type, conv);
1132 /* Returns the conversion path from type FROM to reference type TO for
1133 purposes of reference binding. For lvalue binding, either pass a
1134 reference type to FROM or an lvalue expression to EXPR. If the
1135 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1136 the conversion returned. */
1139 reference_binding (rto, rfrom, expr, flags)
1140 tree rto, rfrom, expr;
1143 tree conv = NULL_TREE;
1144 tree to = TREE_TYPE (rto);
1148 cp_lvalue_kind lvalue_p = clk_none;
1150 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1152 expr = instantiate_type (to, expr, tf_none);
1153 if (expr == error_mark_node)
1155 from = TREE_TYPE (expr);
1158 if (TREE_CODE (from) == REFERENCE_TYPE)
1160 /* Anything with reference type is an lvalue. */
1161 lvalue_p = clk_ordinary;
1162 from = TREE_TYPE (from);
1165 lvalue_p = real_lvalue_p (expr);
1167 /* Figure out whether or not the types are reference-related and
1168 reference compatible. We have do do this after stripping
1169 references from FROM. */
1170 related_p = reference_related_p (to, from);
1171 compatible_p = reference_compatible_p (to, from);
1173 if (lvalue_p && compatible_p)
1177 If the initializer expression
1179 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1180 is reference-compatible with "cv2 T2,"
1182 the reference is bound directly to the initializer exprssion
1184 conv = build1 (IDENTITY_CONV, from, expr);
1185 conv = direct_reference_binding (rto, conv);
1186 if ((lvalue_p & clk_bitfield) != 0
1187 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1188 /* For the purposes of overload resolution, we ignore the fact
1189 this expression is a bitfield. (In particular,
1190 [over.ics.ref] says specifically that a function with a
1191 non-const reference parameter is viable even if the
1192 argument is a bitfield.)
1194 However, when we actually call the function we must create
1195 a temporary to which to bind the reference. If the
1196 reference is volatile, or isn't const, then we cannot make
1197 a temporary, so we just issue an error when the conversion
1199 NEED_TEMPORARY_P (conv) = 1;
1202 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1206 If the initializer exprsesion
1208 -- has a class type (i.e., T2 is a class type) can be
1209 implicitly converted to an lvalue of type "cv3 T3," where
1210 "cv1 T1" is reference-compatible with "cv3 T3". (this
1211 conversion is selected by enumerating the applicable
1212 conversion functions (_over.match.ref_) and choosing the
1213 best one through overload resolution. (_over.match_).
1215 the reference is bound to the lvalue result of the conversion
1216 in the second case. */
1217 conv = convert_class_to_reference (to, from, expr);
1219 return direct_reference_binding (rto, conv);
1222 /* From this point on, we conceptually need temporaries, even if we
1223 elide them. Only the cases above are "direct bindings". */
1224 if (flags & LOOKUP_NO_TEMP_BIND)
1229 When a parameter of reference type is not bound directly to an
1230 argument expression, the conversion sequence is the one required
1231 to convert the argument expression to the underlying type of the
1232 reference according to _over.best.ics_. Conceptually, this
1233 conversion sequence corresponds to copy-initializing a temporary
1234 of the underlying type with the argument expression. Any
1235 difference in top-level cv-qualification is subsumed by the
1236 initialization itself and does not constitute a conversion. */
1240 Otherwise, the reference shall be to a non-volatile const type. */
1241 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1246 If the initializer expression is an rvalue, with T2 a class type,
1247 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1248 is bound in one of the following ways:
1250 -- The reference is bound to the object represented by the rvalue
1251 or to a sub-object within that object.
1253 In this case, the implicit conversion sequence is supposed to be
1254 same as we would obtain by generating a temporary. Fortunately,
1255 if the types are reference compatible, then this is either an
1256 identity conversion or the derived-to-base conversion, just as
1257 for direct binding. */
1258 if (CLASS_TYPE_P (from) && compatible_p)
1260 conv = build1 (IDENTITY_CONV, from, expr);
1261 return direct_reference_binding (rto, conv);
1266 Otherwise, a temporary of type "cv1 T1" is created and
1267 initialized from the initializer expression using the rules for a
1268 non-reference copy initialization. If T1 is reference-related to
1269 T2, cv1 must be the same cv-qualification as, or greater
1270 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1271 if (related_p && !at_least_as_qualified_p (to, from))
1274 conv = implicit_conversion (to, from, expr, flags);
1278 conv = build_conv (REF_BIND, rto, conv);
1279 /* This reference binding, unlike those above, requires the
1280 creation of a temporary. */
1281 NEED_TEMPORARY_P (conv) = 1;
1286 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1287 to type TO. The optional expression EXPR may affect the conversion.
1288 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1292 implicit_conversion (to, from, expr, flags)
1293 tree to, from, expr;
1297 struct z_candidate *cand;
1299 /* Resolve expressions like `A::p' that we thought might become
1300 pointers-to-members. */
1301 if (expr && TREE_CODE (expr) == OFFSET_REF)
1303 expr = resolve_offset_ref (expr);
1304 from = TREE_TYPE (expr);
1307 if (from == error_mark_node || to == error_mark_node
1308 || expr == error_mark_node)
1311 /* Make sure both the FROM and TO types are complete so that
1312 user-defined conversions are available. */
1313 complete_type (from);
1316 if (TREE_CODE (to) == REFERENCE_TYPE)
1317 conv = reference_binding (to, from, expr, flags);
1319 conv = standard_conversion (to, from, expr);
1323 else if (expr != NULL_TREE
1324 && (IS_AGGR_TYPE (from)
1325 || IS_AGGR_TYPE (to))
1326 && (flags & LOOKUP_NO_CONVERSION) == 0)
1328 cand = build_user_type_conversion_1
1329 (to, expr, LOOKUP_ONLYCONVERTING);
1331 conv = cand->second_conv;
1333 /* We used to try to bind a reference to a temporary here, but that
1334 is now handled by the recursive call to this function at the end
1335 of reference_binding. */
1341 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1344 static struct z_candidate *
1345 add_candidate (struct z_candidate *candidates,
1346 tree fn, tree convs, tree access_path, tree
1347 conversion_path, int viable)
1349 struct z_candidate *cand
1350 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1353 cand->convs = convs;
1354 cand->access_path = access_path;
1355 cand->conversion_path = conversion_path;
1356 cand->viable = viable;
1357 cand->next = candidates;
1362 /* Create an overload candidate for the function or method FN called with
1363 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1364 to implicit_conversion.
1366 CTYPE, if non-NULL, is the type we want to pretend this function
1367 comes from for purposes of overload resolution. */
1369 static struct z_candidate *
1370 add_function_candidate (struct z_candidate *candidates,
1371 tree fn, tree ctype, tree arglist,
1372 tree access_path, tree conversion_path,
1375 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1378 tree parmnode, argnode;
1381 /* The `this', `in_chrg' and VTT arguments to constructors are not
1382 considered in overload resolution. */
1383 if (DECL_CONSTRUCTOR_P (fn))
1385 parmlist = skip_artificial_parms_for (fn, parmlist);
1386 arglist = skip_artificial_parms_for (fn, arglist);
1389 len = list_length (arglist);
1390 convs = make_tree_vec (len);
1392 /* 13.3.2 - Viable functions [over.match.viable]
1393 First, to be a viable function, a candidate function shall have enough
1394 parameters to agree in number with the arguments in the list.
1396 We need to check this first; otherwise, checking the ICSes might cause
1397 us to produce an ill-formed template instantiation. */
1399 parmnode = parmlist;
1400 for (i = 0; i < len; ++i)
1402 if (parmnode == NULL_TREE || parmnode == void_list_node)
1404 parmnode = TREE_CHAIN (parmnode);
1407 if (i < len && parmnode)
1410 /* Make sure there are default args for the rest of the parms. */
1411 else if (!sufficient_parms_p (parmnode))
1417 /* Second, for F to be a viable function, there shall exist for each
1418 argument an implicit conversion sequence that converts that argument
1419 to the corresponding parameter of F. */
1421 parmnode = parmlist;
1424 for (i = 0; i < len; ++i)
1426 tree arg = TREE_VALUE (argnode);
1427 tree argtype = lvalue_type (arg);
1431 if (parmnode == void_list_node)
1434 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1435 && ! DECL_CONSTRUCTOR_P (fn));
1439 tree parmtype = TREE_VALUE (parmnode);
1441 /* The type of the implicit object parameter ('this') for
1442 overload resolution is not always the same as for the
1443 function itself; conversion functions are considered to
1444 be members of the class being converted, and functions
1445 introduced by a using-declaration are considered to be
1446 members of the class that uses them.
1448 Since build_over_call ignores the ICS for the `this'
1449 parameter, we can just change the parm type. */
1450 if (ctype && is_this)
1453 = build_qualified_type (ctype,
1454 TYPE_QUALS (TREE_TYPE (parmtype)));
1455 parmtype = build_pointer_type (parmtype);
1458 t = implicit_conversion (parmtype, argtype, arg, flags);
1462 t = build1 (IDENTITY_CONV, argtype, arg);
1463 ICS_ELLIPSIS_FLAG (t) = 1;
1467 ICS_THIS_FLAG (t) = 1;
1469 TREE_VEC_ELT (convs, i) = t;
1476 if (ICS_BAD_FLAG (t))
1480 parmnode = TREE_CHAIN (parmnode);
1481 argnode = TREE_CHAIN (argnode);
1485 return add_candidate (candidates, fn, convs, access_path,
1486 conversion_path, viable);
1489 /* Create an overload candidate for the conversion function FN which will
1490 be invoked for expression OBJ, producing a pointer-to-function which
1491 will in turn be called with the argument list ARGLIST, and add it to
1492 CANDIDATES. FLAGS is passed on to implicit_conversion.
1494 Actually, we don't really care about FN; we care about the type it
1495 converts to. There may be multiple conversion functions that will
1496 convert to that type, and we rely on build_user_type_conversion_1 to
1497 choose the best one; so when we create our candidate, we record the type
1498 instead of the function. */
1500 static struct z_candidate *
1501 add_conv_candidate (candidates, fn, obj, arglist, access_path,
1503 struct z_candidate *candidates;
1504 tree fn, obj, arglist;
1506 tree conversion_path;
1508 tree totype = TREE_TYPE (TREE_TYPE (fn));
1509 int i, len, viable, flags;
1510 tree parmlist, convs, parmnode, argnode;
1512 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1513 parmlist = TREE_TYPE (parmlist);
1514 parmlist = TYPE_ARG_TYPES (parmlist);
1516 len = list_length (arglist) + 1;
1517 convs = make_tree_vec (len);
1518 parmnode = parmlist;
1521 flags = LOOKUP_NORMAL;
1523 /* Don't bother looking up the same type twice. */
1524 if (candidates && candidates->fn == totype)
1527 for (i = 0; i < len; ++i)
1529 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1530 tree argtype = lvalue_type (arg);
1534 t = implicit_conversion (totype, argtype, arg, flags);
1535 else if (parmnode == void_list_node)
1538 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1541 t = build1 (IDENTITY_CONV, argtype, arg);
1542 ICS_ELLIPSIS_FLAG (t) = 1;
1545 TREE_VEC_ELT (convs, i) = t;
1549 if (ICS_BAD_FLAG (t))
1556 parmnode = TREE_CHAIN (parmnode);
1557 argnode = TREE_CHAIN (argnode);
1563 if (!sufficient_parms_p (parmnode))
1566 return add_candidate (candidates, totype, convs, access_path,
1567 conversion_path, viable);
1570 static struct z_candidate *
1571 build_builtin_candidate (candidates, fnname, type1, type2,
1572 args, argtypes, flags)
1573 struct z_candidate *candidates;
1574 tree fnname, type1, type2, *args, *argtypes;
1585 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1587 for (i = 0; i < 2; ++i)
1592 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1596 /* We need something for printing the candidate. */
1597 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1599 else if (ICS_BAD_FLAG (t))
1601 TREE_VEC_ELT (convs, i) = t;
1604 /* For COND_EXPR we rearranged the arguments; undo that now. */
1607 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1608 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1609 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1611 TREE_VEC_ELT (convs, 0) = t;
1616 return add_candidate (candidates, fnname, convs,
1617 /*access_path=*/NULL_TREE,
1618 /*conversion_path=*/NULL_TREE,
1626 return COMPLETE_TYPE_P (complete_type (t));
1629 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1632 promoted_arithmetic_type_p (type)
1637 In this section, the term promoted integral type is used to refer
1638 to those integral types which are preserved by integral promotion
1639 (including e.g. int and long but excluding e.g. char).
1640 Similarly, the term promoted arithmetic type refers to promoted
1641 integral types plus floating types. */
1642 return ((INTEGRAL_TYPE_P (type)
1643 && same_type_p (type_promotes_to (type), type))
1644 || TREE_CODE (type) == REAL_TYPE);
1647 /* Create any builtin operator overload candidates for the operator in
1648 question given the converted operand types TYPE1 and TYPE2. The other
1649 args are passed through from add_builtin_candidates to
1650 build_builtin_candidate.
1652 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1653 If CODE is requires candidates operands of the same type of the kind
1654 of which TYPE1 and TYPE2 are, we add both candidates
1655 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1657 static struct z_candidate *
1658 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
1659 args, argtypes, flags)
1660 struct z_candidate *candidates;
1661 enum tree_code code, code2;
1662 tree fnname, type1, type2, *args, *argtypes;
1667 case POSTINCREMENT_EXPR:
1668 case POSTDECREMENT_EXPR:
1669 args[1] = integer_zero_node;
1670 type2 = integer_type_node;
1679 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1680 and VQ is either volatile or empty, there exist candidate operator
1681 functions of the form
1682 VQ T& operator++(VQ T&);
1683 T operator++(VQ T&, int);
1684 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1685 type other than bool, and VQ is either volatile or empty, there exist
1686 candidate operator functions of the form
1687 VQ T& operator--(VQ T&);
1688 T operator--(VQ T&, int);
1689 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1690 complete object type, and VQ is either volatile or empty, there exist
1691 candidate operator functions of the form
1692 T*VQ& operator++(T*VQ&);
1693 T*VQ& operator--(T*VQ&);
1694 T* operator++(T*VQ&, int);
1695 T* operator--(T*VQ&, int); */
1697 case POSTDECREMENT_EXPR:
1698 case PREDECREMENT_EXPR:
1699 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1701 case POSTINCREMENT_EXPR:
1702 case PREINCREMENT_EXPR:
1703 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1705 type1 = build_reference_type (type1);
1710 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1711 exist candidate operator functions of the form
1715 8 For every function type T, there exist candidate operator functions of
1717 T& operator*(T*); */
1720 if (TREE_CODE (type1) == POINTER_TYPE
1721 && (TYPE_PTROB_P (type1)
1722 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1726 /* 9 For every type T, there exist candidate operator functions of the form
1729 10For every promoted arithmetic type T, there exist candidate operator
1730 functions of the form
1734 case CONVERT_EXPR: /* unary + */
1735 if (TREE_CODE (type1) == POINTER_TYPE
1736 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1739 if (ARITHMETIC_TYPE_P (type1))
1743 /* 11For every promoted integral type T, there exist candidate operator
1744 functions of the form
1748 if (INTEGRAL_TYPE_P (type1))
1752 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1753 is the same type as C2 or is a derived class of C2, T is a complete
1754 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1755 there exist candidate operator functions of the form
1756 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1757 where CV12 is the union of CV1 and CV2. */
1760 if (TREE_CODE (type1) == POINTER_TYPE
1761 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1763 tree c1 = TREE_TYPE (type1);
1764 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1765 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1766 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1768 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1769 && (TYPE_PTRMEMFUNC_P (type2)
1770 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1775 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1776 didate operator functions of the form
1781 bool operator<(L, R);
1782 bool operator>(L, R);
1783 bool operator<=(L, R);
1784 bool operator>=(L, R);
1785 bool operator==(L, R);
1786 bool operator!=(L, R);
1787 where LR is the result of the usual arithmetic conversions between
1790 14For every pair of types T and I, where T is a cv-qualified or cv-
1791 unqualified complete object type and I is a promoted integral type,
1792 there exist candidate operator functions of the form
1793 T* operator+(T*, I);
1794 T& operator[](T*, I);
1795 T* operator-(T*, I);
1796 T* operator+(I, T*);
1797 T& operator[](I, T*);
1799 15For every T, where T is a pointer to complete object type, there exist
1800 candidate operator functions of the form112)
1801 ptrdiff_t operator-(T, T);
1803 16For every pointer or enumeration type T, there exist candidate operator
1804 functions of the form
1805 bool operator<(T, T);
1806 bool operator>(T, T);
1807 bool operator<=(T, T);
1808 bool operator>=(T, T);
1809 bool operator==(T, T);
1810 bool operator!=(T, T);
1812 17For every pointer to member type T, there exist candidate operator
1813 functions of the form
1814 bool operator==(T, T);
1815 bool operator!=(T, T); */
1818 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1820 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1822 type2 = ptrdiff_type_node;
1826 case TRUNC_DIV_EXPR:
1827 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1833 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1834 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1836 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1837 && null_ptr_cst_p (args[1]))
1842 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1843 && null_ptr_cst_p (args[0]))
1855 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1857 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1859 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1861 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1866 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1874 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1877 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1879 type1 = ptrdiff_type_node;
1882 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1884 type2 = ptrdiff_type_node;
1889 /* 18For every pair of promoted integral types L and R, there exist candi-
1890 date operator functions of the form
1897 where LR is the result of the usual arithmetic conversions between
1900 case TRUNC_MOD_EXPR:
1906 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1910 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1911 type, VQ is either volatile or empty, and R is a promoted arithmetic
1912 type, there exist candidate operator functions of the form
1913 VQ L& operator=(VQ L&, R);
1914 VQ L& operator*=(VQ L&, R);
1915 VQ L& operator/=(VQ L&, R);
1916 VQ L& operator+=(VQ L&, R);
1917 VQ L& operator-=(VQ L&, R);
1919 20For every pair T, VQ), where T is any type and VQ is either volatile
1920 or empty, there exist candidate operator functions of the form
1921 T*VQ& operator=(T*VQ&, T*);
1923 21For every pair T, VQ), where T is a pointer to member type and VQ is
1924 either volatile or empty, there exist candidate operator functions of
1926 VQ T& operator=(VQ T&, T);
1928 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1929 unqualified complete object type, VQ is either volatile or empty, and
1930 I is a promoted integral type, there exist candidate operator func-
1932 T*VQ& operator+=(T*VQ&, I);
1933 T*VQ& operator-=(T*VQ&, I);
1935 23For every triple L, VQ, R), where L is an integral or enumeration
1936 type, VQ is either volatile or empty, and R is a promoted integral
1937 type, there exist candidate operator functions of the form
1939 VQ L& operator%=(VQ L&, R);
1940 VQ L& operator<<=(VQ L&, R);
1941 VQ L& operator>>=(VQ L&, R);
1942 VQ L& operator&=(VQ L&, R);
1943 VQ L& operator^=(VQ L&, R);
1944 VQ L& operator|=(VQ L&, R); */
1951 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1953 type2 = ptrdiff_type_node;
1957 case TRUNC_DIV_EXPR:
1958 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1962 case TRUNC_MOD_EXPR:
1968 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1973 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1975 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1976 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1977 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1978 || ((TYPE_PTRMEMFUNC_P (type1)
1979 || TREE_CODE (type1) == POINTER_TYPE)
1980 && null_ptr_cst_p (args[1])))
1990 type1 = build_reference_type (type1);
1996 For every pair of promoted arithmetic types L and R, there
1997 exist candidate operator functions of the form
1999 LR operator?(bool, L, R);
2001 where LR is the result of the usual arithmetic conversions
2002 between types L and R.
2004 For every type T, where T is a pointer or pointer-to-member
2005 type, there exist candidate operator functions of the form T
2006 operator?(bool, T, T); */
2008 if (promoted_arithmetic_type_p (type1)
2009 && promoted_arithmetic_type_p (type2))
2013 /* Otherwise, the types should be pointers. */
2014 if (!(TREE_CODE (type1) == POINTER_TYPE
2015 || TYPE_PTRMEM_P (type1)
2016 || TYPE_PTRMEMFUNC_P (type1))
2017 || !(TREE_CODE (type2) == POINTER_TYPE
2018 || TYPE_PTRMEM_P (type2)
2019 || TYPE_PTRMEMFUNC_P (type2)))
2022 /* We don't check that the two types are the same; the logic
2023 below will actually create two candidates; one in which both
2024 parameter types are TYPE1, and one in which both parameter
2028 /* These arguments do not make for a valid overloaded operator. */
2035 /* If we're dealing with two pointer types or two enumeral types,
2036 we need candidates for both of them. */
2037 if (type2 && !same_type_p (type1, type2)
2038 && TREE_CODE (type1) == TREE_CODE (type2)
2039 && (TREE_CODE (type1) == REFERENCE_TYPE
2040 || (TREE_CODE (type1) == POINTER_TYPE
2041 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
2042 || TYPE_PTRMEMFUNC_P (type1)
2043 || IS_AGGR_TYPE (type1)
2044 || TREE_CODE (type1) == ENUMERAL_TYPE))
2046 candidates = build_builtin_candidate
2047 (candidates, fnname, type1, type1, args, argtypes, flags);
2048 return build_builtin_candidate
2049 (candidates, fnname, type2, type2, args, argtypes, flags);
2052 return build_builtin_candidate
2053 (candidates, fnname, type1, type2, args, argtypes, flags);
2057 type_decays_to (type)
2060 if (TREE_CODE (type) == ARRAY_TYPE)
2061 return build_pointer_type (TREE_TYPE (type));
2062 if (TREE_CODE (type) == FUNCTION_TYPE)
2063 return build_pointer_type (type);
2067 /* There are three conditions of builtin candidates:
2069 1) bool-taking candidates. These are the same regardless of the input.
2070 2) pointer-pair taking candidates. These are generated for each type
2071 one of the input types converts to.
2072 3) arithmetic candidates. According to the standard, we should generate
2073 all of these, but I'm trying not to...
2075 Here we generate a superset of the possible candidates for this particular
2076 case. That is a subset of the full set the standard defines, plus some
2077 other cases which the standard disallows. add_builtin_candidate will
2078 filter out the invalid set. */
2080 static struct z_candidate *
2081 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2082 struct z_candidate *candidates;
2083 enum tree_code code, code2;
2089 tree type, argtypes[3];
2090 /* TYPES[i] is the set of possible builtin-operator parameter types
2091 we will consider for the Ith argument. These are represented as
2092 a TREE_LIST; the TREE_VALUE of each node is the potential
2096 for (i = 0; i < 3; ++i)
2099 argtypes[i] = lvalue_type (args[i]);
2101 argtypes[i] = NULL_TREE;
2106 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2107 and VQ is either volatile or empty, there exist candidate operator
2108 functions of the form
2109 VQ T& operator++(VQ T&); */
2111 case POSTINCREMENT_EXPR:
2112 case PREINCREMENT_EXPR:
2113 case POSTDECREMENT_EXPR:
2114 case PREDECREMENT_EXPR:
2119 /* 24There also exist candidate operator functions of the form
2120 bool operator!(bool);
2121 bool operator&&(bool, bool);
2122 bool operator||(bool, bool); */
2124 case TRUTH_NOT_EXPR:
2125 return build_builtin_candidate
2126 (candidates, fnname, boolean_type_node,
2127 NULL_TREE, args, argtypes, flags);
2129 case TRUTH_ORIF_EXPR:
2130 case TRUTH_ANDIF_EXPR:
2131 return build_builtin_candidate
2132 (candidates, fnname, boolean_type_node,
2133 boolean_type_node, args, argtypes, flags);
2154 types[0] = types[1] = NULL_TREE;
2156 for (i = 0; i < 2; ++i)
2160 else if (IS_AGGR_TYPE (argtypes[i]))
2164 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2167 convs = lookup_conversions (argtypes[i]);
2169 if (code == COND_EXPR)
2171 if (real_lvalue_p (args[i]))
2172 types[i] = tree_cons
2173 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2175 types[i] = tree_cons
2176 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2182 for (; convs; convs = TREE_CHAIN (convs))
2184 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2187 && (TREE_CODE (type) != REFERENCE_TYPE
2188 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2191 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2192 types[i] = tree_cons (NULL_TREE, type, types[i]);
2194 type = non_reference (type);
2195 if (i != 0 || ! ref1)
2197 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2198 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2199 types[i] = tree_cons (NULL_TREE, type, types[i]);
2200 if (INTEGRAL_TYPE_P (type))
2201 type = type_promotes_to (type);
2204 if (! value_member (type, types[i]))
2205 types[i] = tree_cons (NULL_TREE, type, types[i]);
2210 if (code == COND_EXPR && real_lvalue_p (args[i]))
2211 types[i] = tree_cons
2212 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2213 type = non_reference (argtypes[i]);
2214 if (i != 0 || ! ref1)
2216 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2217 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2218 types[i] = tree_cons (NULL_TREE, type, types[i]);
2219 if (INTEGRAL_TYPE_P (type))
2220 type = type_promotes_to (type);
2222 types[i] = tree_cons (NULL_TREE, type, types[i]);
2226 /* Run through the possible parameter types of both arguments,
2227 creating candidates with those parameter types. */
2228 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2231 for (type = types[1]; type; type = TREE_CHAIN (type))
2232 candidates = add_builtin_candidate
2233 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2234 TREE_VALUE (type), args, argtypes, flags);
2236 candidates = add_builtin_candidate
2237 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2238 NULL_TREE, args, argtypes, flags);
2245 /* If TMPL can be successfully instantiated as indicated by
2246 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2248 TMPL is the template. EXPLICIT_TARGS are any explicit template
2249 arguments. ARGLIST is the arguments provided at the call-site.
2250 The RETURN_TYPE is the desired type for conversion operators. If
2251 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2252 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2253 add_conv_candidate. */
2255 static struct z_candidate*
2256 add_template_candidate_real (candidates, tmpl, ctype, explicit_targs,
2257 arglist, return_type, access_path,
2258 conversion_path, flags, obj, strict)
2259 struct z_candidate *candidates;
2260 tree tmpl, ctype, explicit_targs, arglist, return_type;
2262 tree conversion_path;
2265 unification_kind_t strict;
2267 int ntparms = DECL_NTPARMS (tmpl);
2268 tree targs = make_tree_vec (ntparms);
2269 tree args_without_in_chrg = arglist;
2270 struct z_candidate *cand;
2274 /* We don't do deduction on the in-charge parameter, the VTT
2275 parameter or 'this'. */
2276 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2277 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2279 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2280 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2281 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2282 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2284 i = fn_type_unification (tmpl, explicit_targs, targs,
2285 args_without_in_chrg,
2286 return_type, strict, -1);
2291 fn = instantiate_template (tmpl, targs);
2292 if (fn == error_mark_node)
2297 A member function template is never instantiated to perform the
2298 copy of a class object to an object of its class type.
2300 It's a little unclear what this means; the standard explicitly
2301 does allow a template to be used to copy a class. For example,
2306 template <class T> A(const T&);
2309 void g () { A a (f ()); }
2311 the member template will be used to make the copy. The section
2312 quoted above appears in the paragraph that forbids constructors
2313 whose only parameter is (a possibly cv-qualified variant of) the
2314 class type, and a logical interpretation is that the intent was
2315 to forbid the instantiation of member templates which would then
2317 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2319 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2320 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2325 if (obj != NULL_TREE)
2326 /* Aha, this is a conversion function. */
2327 cand = add_conv_candidate (candidates, fn, obj, access_path,
2328 conversion_path, arglist);
2330 cand = add_function_candidate (candidates, fn, ctype,
2331 arglist, access_path,
2332 conversion_path, flags);
2333 if (DECL_TI_TEMPLATE (fn) != tmpl)
2334 /* This situation can occur if a member template of a template
2335 class is specialized. Then, instantiate_template might return
2336 an instantiation of the specialization, in which case the
2337 DECL_TI_TEMPLATE field will point at the original
2338 specialization. For example:
2340 template <class T> struct S { template <class U> void f(U);
2341 template <> void f(int) {}; };
2345 Here, TMPL will be template <class U> S<double>::f(U).
2346 And, instantiate template will give us the specialization
2347 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2348 for this will point at template <class T> template <> S<T>::f(int),
2349 so that we can find the definition. For the purposes of
2350 overload resolution, however, we want the original TMPL. */
2351 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2353 cand->template = DECL_TEMPLATE_INFO (fn);
2359 static struct z_candidate *
2360 add_template_candidate (candidates, tmpl, ctype, explicit_targs,
2361 arglist, return_type, access_path,
2362 conversion_path, flags, strict)
2363 struct z_candidate *candidates;
2364 tree tmpl, ctype, explicit_targs, arglist, return_type;
2366 tree conversion_path;
2368 unification_kind_t strict;
2371 add_template_candidate_real (candidates, tmpl, ctype,
2372 explicit_targs, arglist, return_type,
2373 access_path, conversion_path,
2374 flags, NULL_TREE, strict);
2378 static struct z_candidate *
2379 add_template_conv_candidate (candidates, tmpl, obj, arglist, return_type,
2380 access_path, conversion_path)
2381 struct z_candidate *candidates;
2382 tree tmpl, obj, arglist, return_type;
2384 tree conversion_path;
2387 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2388 arglist, return_type, access_path,
2389 conversion_path, 0, obj, DEDUCE_CONV);
2395 struct z_candidate *cands;
2397 for (; cands; cands = cands->next)
2398 if (pedantic ? cands->viable == 1 : cands->viable)
2404 any_strictly_viable (cands)
2405 struct z_candidate *cands;
2407 for (; cands; cands = cands->next)
2408 if (cands->viable == 1)
2413 static struct z_candidate *
2414 splice_viable (cands)
2415 struct z_candidate *cands;
2417 struct z_candidate **p = &cands;
2421 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2434 /* Fix this to work on non-lvalues. */
2435 return build_unary_op (ADDR_EXPR, obj, 0);
2439 print_z_candidates (candidates)
2440 struct z_candidate *candidates;
2442 const char *str = "candidates are:";
2443 for (; candidates; candidates = candidates->next)
2445 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2447 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2448 error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn,
2449 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2450 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2451 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2452 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2453 error ("%s %D(%T, %T) <built-in>", str, candidates->fn,
2454 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2455 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2457 error ("%s %D(%T) <built-in>", str, candidates->fn,
2458 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2460 else if (TYPE_P (candidates->fn))
2461 error ("%s %T <conversion>", str, candidates->fn);
2463 cp_error_at ("%s %+#D%s", str, candidates->fn,
2464 candidates->viable == -1 ? " <near match>" : "");
2469 /* Returns the best overload candidate to perform the requested
2470 conversion. This function is used for three the overloading situations
2471 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2472 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2473 per [dcl.init.ref], so we ignore temporary bindings. */
2475 static struct z_candidate *
2476 build_user_type_conversion_1 (totype, expr, flags)
2480 struct z_candidate *candidates, *cand;
2481 tree fromtype = TREE_TYPE (expr);
2482 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2483 tree args = NULL_TREE;
2484 tree templates = NULL_TREE;
2486 /* We represent conversion within a hierarchy using RVALUE_CONV and
2487 BASE_CONV, as specified by [over.best.ics]; these become plain
2488 constructor calls, as specified in [dcl.init]. */
2489 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2490 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2492 if (IS_AGGR_TYPE (totype))
2493 ctors = lookup_fnfields (TYPE_BINFO (totype),
2494 complete_ctor_identifier,
2497 if (IS_AGGR_TYPE (fromtype))
2498 convs = lookup_conversions (fromtype);
2501 flags |= LOOKUP_NO_CONVERSION;
2507 ctors = BASELINK_FUNCTIONS (ctors);
2509 t = build_int_2 (0, 0);
2510 TREE_TYPE (t) = build_pointer_type (totype);
2511 args = build_tree_list (NULL_TREE, expr);
2512 /* We should never try to call the abstract or base constructor
2514 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2515 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2517 args = tree_cons (NULL_TREE, t, args);
2519 for (; ctors; ctors = OVL_NEXT (ctors))
2521 tree ctor = OVL_CURRENT (ctors);
2522 if (DECL_NONCONVERTING_P (ctor))
2525 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2527 templates = tree_cons (NULL_TREE, ctor, templates);
2529 add_template_candidate (candidates, ctor, totype,
2530 NULL_TREE, args, NULL_TREE,
2531 TYPE_BINFO (totype),
2532 TYPE_BINFO (totype),
2537 candidates = add_function_candidate (candidates, ctor, totype,
2538 args, TYPE_BINFO (totype),
2539 TYPE_BINFO (totype),
2543 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2547 args = build_tree_list (NULL_TREE, build_this (expr));
2549 for (; convs; convs = TREE_CHAIN (convs))
2552 tree conversion_path = TREE_PURPOSE (convs);
2553 int convflags = LOOKUP_NO_CONVERSION;
2555 /* If we are called to convert to a reference type, we are trying to
2556 find an lvalue binding, so don't even consider temporaries. If
2557 we don't find an lvalue binding, the caller will try again to
2558 look for a temporary binding. */
2559 if (TREE_CODE (totype) == REFERENCE_TYPE)
2560 convflags |= LOOKUP_NO_TEMP_BIND;
2562 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2564 tree fn = OVL_CURRENT (fns);
2565 struct z_candidate *old_candidates = candidates;
2567 /* [over.match.funcs] For conversion functions, the function
2568 is considered to be a member of the class of the implicit
2569 object argument for the purpose of defining the type of
2570 the implicit object parameter.
2572 So we pass fromtype as CTYPE to add_*_candidate. */
2574 if (TREE_CODE (fn) == TEMPLATE_DECL)
2576 templates = tree_cons (NULL_TREE, fn, templates);
2578 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2579 args, totype, TYPE_BINFO (fromtype),
2585 candidates = add_function_candidate (candidates, fn, fromtype,
2587 TYPE_BINFO (fromtype),
2591 if (candidates != old_candidates)
2593 tree ics = implicit_conversion
2594 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2597 candidates->second_conv = ics;
2599 if (ics == NULL_TREE)
2600 candidates->viable = 0;
2601 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2602 candidates->viable = -1;
2607 if (! any_viable (candidates))
2610 candidates = splice_viable (candidates);
2611 cand = tourney (candidates);
2615 if (flags & LOOKUP_COMPLAIN)
2617 error ("conversion from `%T' to `%T' is ambiguous",
2619 print_z_candidates (candidates);
2622 cand = candidates; /* any one will do */
2623 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2624 ICS_USER_FLAG (cand->second_conv) = 1;
2625 /* Don't set ICS_BAD_FLAG; an ambiguous conversion is no worse than
2626 another user-defined conversion. */
2631 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2632 p = &(TREE_OPERAND (*p, 0));
2636 (DECL_CONSTRUCTOR_P (cand->fn)
2637 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2638 expr, build_zc_wrapper (cand));
2640 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2641 if (cand->viable == -1)
2642 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2648 build_user_type_conversion (totype, expr, flags)
2652 struct z_candidate *cand
2653 = build_user_type_conversion_1 (totype, expr, flags);
2657 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2658 return error_mark_node;
2659 return convert_from_reference (convert_like (cand->second_conv, expr));
2664 /* Do any initial processing on the arguments to a function call. */
2671 for (t = args; t; t = TREE_CHAIN (t))
2673 tree arg = TREE_VALUE (t);
2675 if (arg == error_mark_node)
2676 return error_mark_node;
2677 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2679 error ("invalid use of void expression");
2680 return error_mark_node;
2682 else if (TREE_CODE (arg) == OFFSET_REF)
2683 arg = resolve_offset_ref (arg);
2684 arg = convert_from_reference (arg);
2685 TREE_VALUE (t) = arg;
2690 /* Return an expression for a call to FN (a namespace-scope function)
2694 build_new_function_call (fn, args)
2697 struct z_candidate *candidates = 0, *cand;
2698 tree explicit_targs = NULL_TREE;
2699 int template_only = 0;
2701 /* Check FN and ARGS. */
2702 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2703 || TREE_CODE (fn) == TEMPLATE_DECL
2704 || TREE_CODE (fn) == OVERLOAD
2705 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2707 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2710 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2712 explicit_targs = TREE_OPERAND (fn, 1);
2713 fn = TREE_OPERAND (fn, 0);
2717 if (really_overloaded_fn (fn)
2718 || TREE_CODE (fn) == TEMPLATE_DECL)
2721 tree templates = NULL_TREE;
2723 args = resolve_args (args);
2725 if (args == error_mark_node)
2726 return error_mark_node;
2728 for (t1 = fn; t1; t1 = OVL_NEXT (t1))
2730 tree t = OVL_CURRENT (t1);
2732 if (TREE_CODE (t) == TEMPLATE_DECL)
2734 templates = tree_cons (NULL_TREE, t, templates);
2735 candidates = add_template_candidate
2736 (candidates, t, NULL_TREE, explicit_targs, args,
2738 /*access_path=*/NULL_TREE, /*conversion_path=*/NULL_TREE,
2739 LOOKUP_NORMAL, DEDUCE_CALL);
2741 else if (! template_only)
2742 candidates = add_function_candidate
2743 (candidates, t, NULL_TREE, args, /*access_path=*/NULL_TREE,
2744 /*conversion_path=*/NULL_TREE, LOOKUP_NORMAL);
2747 if (! any_viable (candidates))
2749 if (candidates && ! candidates->next)
2750 return build_function_call (candidates->fn, args);
2751 error ("no matching function for call to `%D(%A)'",
2752 DECL_NAME (OVL_CURRENT (fn)), args);
2754 print_z_candidates (candidates);
2755 return error_mark_node;
2757 candidates = splice_viable (candidates);
2758 cand = tourney (candidates);
2762 error ("call of overloaded `%D(%A)' is ambiguous",
2763 DECL_NAME (OVL_FUNCTION (fn)), args);
2764 print_z_candidates (candidates);
2765 return error_mark_node;
2768 return build_over_call (cand, args, LOOKUP_NORMAL);
2771 /* This is not really overloaded. */
2772 fn = OVL_CURRENT (fn);
2774 return build_function_call (fn, args);
2778 build_object_call (obj, args)
2781 struct z_candidate *candidates = 0, *cand;
2782 tree fns, convs, mem_args = NULL_TREE;
2783 tree type = TREE_TYPE (obj);
2785 if (TYPE_PTRMEMFUNC_P (type))
2787 /* It's no good looking for an overloaded operator() on a
2788 pointer-to-member-function. */
2789 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2790 return error_mark_node;
2793 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2794 if (fns == error_mark_node)
2795 return error_mark_node;
2797 args = resolve_args (args);
2799 if (args == error_mark_node)
2800 return error_mark_node;
2804 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2805 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2807 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2809 tree fn = OVL_CURRENT (fns);
2810 if (TREE_CODE (fn) == TEMPLATE_DECL)
2813 = add_template_candidate (candidates, fn, base, NULL_TREE,
2814 mem_args, NULL_TREE,
2817 LOOKUP_NORMAL, DEDUCE_CALL);
2820 candidates = add_function_candidate
2821 (candidates, fn, base, mem_args, TYPE_BINFO (type),
2822 TYPE_BINFO (type), LOOKUP_NORMAL);
2826 convs = lookup_conversions (type);
2828 for (; convs; convs = TREE_CHAIN (convs))
2830 tree fns = TREE_VALUE (convs);
2831 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2833 if ((TREE_CODE (totype) == POINTER_TYPE
2834 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2835 || (TREE_CODE (totype) == REFERENCE_TYPE
2836 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2837 || (TREE_CODE (totype) == REFERENCE_TYPE
2838 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2839 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2840 for (; fns; fns = OVL_NEXT (fns))
2842 tree fn = OVL_CURRENT (fns);
2843 if (TREE_CODE (fn) == TEMPLATE_DECL)
2845 candidates = (add_template_conv_candidate
2846 (candidates, fn, obj, args, totype,
2847 /*access_path=*/NULL_TREE,
2848 /*conversion_path=*/NULL_TREE));
2851 candidates = add_conv_candidate (candidates, fn, obj, args,
2852 /*conversion_path=*/NULL_TREE,
2853 /*access_path=*/NULL_TREE);
2857 if (! any_viable (candidates))
2859 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2860 print_z_candidates (candidates);
2861 return error_mark_node;
2864 candidates = splice_viable (candidates);
2865 cand = tourney (candidates);
2869 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2870 print_z_candidates (candidates);
2871 return error_mark_node;
2874 /* Since cand->fn will be a type, not a function, for a conversion
2875 function, we must be careful not to unconditionally look at
2877 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2878 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2879 return build_over_call (cand, mem_args, LOOKUP_NORMAL);
2881 obj = convert_like_with_context
2882 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2885 return build_function_call (obj, args);
2889 op_error (code, code2, arg1, arg2, arg3, problem)
2890 enum tree_code code, code2;
2891 tree arg1, arg2, arg3;
2892 const char *problem;
2896 if (code == MODIFY_EXPR)
2897 opname = assignment_operator_name_info[code2].name;
2899 opname = operator_name_info[code].name;
2904 error ("%s for `%T ? %T : %T' operator", problem,
2905 error_type (arg1), error_type (arg2), error_type (arg3));
2907 case POSTINCREMENT_EXPR:
2908 case POSTDECREMENT_EXPR:
2909 error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2912 error ("%s for `%T [%T]' operator", problem,
2913 error_type (arg1), error_type (arg2));
2917 error ("%s for `%T %s %T' operator", problem,
2918 error_type (arg1), opname, error_type (arg2));
2920 error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2924 /* Return the implicit conversion sequence that could be used to
2925 convert E1 to E2 in [expr.cond]. */
2928 conditional_conversion (e1, e2)
2932 tree t1 = non_reference (TREE_TYPE (e1));
2933 tree t2 = non_reference (TREE_TYPE (e2));
2938 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2939 implicitly converted (clause _conv_) to the type "reference to
2940 T2", subject to the constraint that in the conversion the
2941 reference must bind directly (_dcl.init.ref_) to E1. */
2942 if (real_lvalue_p (e2))
2944 conv = implicit_conversion (build_reference_type (t2),
2947 LOOKUP_NO_TEMP_BIND);
2954 If E1 and E2 have class type, and the underlying class types are
2955 the same or one is a base class of the other: E1 can be converted
2956 to match E2 if the class of T2 is the same type as, or a base
2957 class of, the class of T1, and the cv-qualification of T2 is the
2958 same cv-qualification as, or a greater cv-qualification than, the
2959 cv-qualification of T1. If the conversion is applied, E1 is
2960 changed to an rvalue of type T2 that still refers to the original
2961 source class object (or the appropriate subobject thereof). */
2962 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2963 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2964 TYPE_MAIN_VARIANT (t1)))
2966 if (at_least_as_qualified_p (t2, t1))
2968 conv = build1 (IDENTITY_CONV, t1, e1);
2969 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2970 TYPE_MAIN_VARIANT (t2)))
2971 conv = build_conv (BASE_CONV, t2, conv);
2980 E1 can be converted to match E2 if E1 can be implicitly converted
2981 to the type that expression E2 would have if E2 were converted to
2982 an rvalue (or the type it has, if E2 is an rvalue). */
2983 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2986 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2987 arguments to the conditional expression. */
2990 build_conditional_expr (arg1, arg2, arg3)
2998 tree result_type = NULL_TREE;
3000 struct z_candidate *candidates = 0;
3001 struct z_candidate *cand;
3003 /* As a G++ extension, the second argument to the conditional can be
3004 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3005 c'.) If the second operand is omitted, make sure it is
3006 calculated only once. */
3010 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
3011 arg1 = arg2 = save_expr (arg1);
3016 The first expr ession is implicitly converted to bool (clause
3018 arg1 = cp_convert (boolean_type_node, arg1);
3020 /* If something has already gone wrong, just pass that fact up the
3022 if (arg1 == error_mark_node
3023 || arg2 == error_mark_node
3024 || arg3 == error_mark_node
3025 || TREE_TYPE (arg1) == error_mark_node
3026 || TREE_TYPE (arg2) == error_mark_node
3027 || TREE_TYPE (arg3) == error_mark_node)
3028 return error_mark_node;
3032 If either the second or the third operand has type (possibly
3033 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3034 array-to-pointer (_conv.array_), and function-to-pointer
3035 (_conv.func_) standard conversions are performed on the second
3036 and third operands. */
3037 arg2_type = TREE_TYPE (arg2);
3038 arg3_type = TREE_TYPE (arg3);
3039 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3041 /* Do the conversions. We don't these for `void' type arguments
3042 since it can't have any effect and since decay_conversion
3043 does not handle that case gracefully. */
3044 if (!VOID_TYPE_P (arg2_type))
3045 arg2 = decay_conversion (arg2);
3046 if (!VOID_TYPE_P (arg3_type))
3047 arg3 = decay_conversion (arg3);
3048 arg2_type = TREE_TYPE (arg2);
3049 arg3_type = TREE_TYPE (arg3);
3053 One of the following shall hold:
3055 --The second or the third operand (but not both) is a
3056 throw-expression (_except.throw_); the result is of the
3057 type of the other and is an rvalue.
3059 --Both the second and the third operands have type void; the
3060 result is of type void and is an rvalue. */
3061 if ((TREE_CODE (arg2) == THROW_EXPR)
3062 ^ (TREE_CODE (arg3) == THROW_EXPR))
3063 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
3064 ? arg3_type : arg2_type);
3065 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3066 result_type = void_type_node;
3069 error ("`%E' has type `void' and is not a throw-expression",
3070 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3071 return error_mark_node;
3075 goto valid_operands;
3079 Otherwise, if the second and third operand have different types,
3080 and either has (possibly cv-qualified) class type, an attempt is
3081 made to convert each of those operands to the type of the other. */
3082 else if (!same_type_p (arg2_type, arg3_type)
3083 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3085 tree conv2 = conditional_conversion (arg2, arg3);
3086 tree conv3 = conditional_conversion (arg3, arg2);
3090 If both can be converted, or one can be converted but the
3091 conversion is ambiguous, the program is ill-formed. If
3092 neither can be converted, the operands are left unchanged and
3093 further checking is performed as described below. If exactly
3094 one conversion is possible, that conversion is applied to the
3095 chosen operand and the converted operand is used in place of
3096 the original operand for the remainder of this section. */
3097 if ((conv2 && !ICS_BAD_FLAG (conv2)
3098 && conv3 && !ICS_BAD_FLAG (conv3))
3099 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3100 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3102 error ("operands to ?: have different types");
3103 return error_mark_node;
3105 else if (conv2 && !ICS_BAD_FLAG (conv2))
3107 arg2 = convert_like (conv2, arg2);
3108 arg2 = convert_from_reference (arg2);
3109 /* That may not quite have done the trick. If the two types
3110 are cv-qualified variants of one another, we will have
3111 just used an IDENTITY_CONV. (There's no conversion from
3112 an lvalue of one class type to an lvalue of another type,
3113 even a cv-qualified variant, and we don't want to lose
3114 lvalue-ness here.) So, we manually add a NOP_EXPR here
3116 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3117 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3118 arg2_type = TREE_TYPE (arg2);
3120 else if (conv3 && !ICS_BAD_FLAG (conv3))
3122 arg3 = convert_like (conv3, arg3);
3123 arg3 = convert_from_reference (arg3);
3124 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3125 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3126 arg3_type = TREE_TYPE (arg3);
3132 If the second and third operands are lvalues and have the same
3133 type, the result is of that type and is an lvalue. */
3134 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3135 same_type_p (arg2_type, arg3_type))
3137 result_type = arg2_type;
3138 goto valid_operands;
3143 Otherwise, the result is an rvalue. If the second and third
3144 operand do not have the same type, and either has (possibly
3145 cv-qualified) class type, overload resolution is used to
3146 determine the conversions (if any) to be applied to the operands
3147 (_over.match.oper_, _over.built_). */
3149 if (!same_type_p (arg2_type, arg3_type)
3150 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3155 /* Rearrange the arguments so that add_builtin_candidate only has
3156 to know about two args. In build_builtin_candidates, the
3157 arguments are unscrambled. */
3161 candidates = add_builtin_candidates (candidates,
3164 ansi_opname (COND_EXPR),
3170 If the overload resolution fails, the program is
3172 if (!any_viable (candidates))
3174 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3175 print_z_candidates (candidates);
3176 return error_mark_node;
3178 candidates = splice_viable (candidates);
3179 cand = tourney (candidates);
3182 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3183 print_z_candidates (candidates);
3184 return error_mark_node;
3189 Otherwise, the conversions thus determined are applied, and
3190 the converted operands are used in place of the original
3191 operands for the remainder of this section. */
3192 conv = TREE_VEC_ELT (cand->convs, 0);
3193 arg1 = convert_like (conv, arg1);
3194 conv = TREE_VEC_ELT (cand->convs, 1);
3195 arg2 = convert_like (conv, arg2);
3196 conv = TREE_VEC_ELT (cand->convs, 2);
3197 arg3 = convert_like (conv, arg3);
3202 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3203 and function-to-pointer (_conv.func_) standard conversions are
3204 performed on the second and third operands.
3206 We need to force the lvalue-to-rvalue conversion here for class types,
3207 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3208 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3211 We use ocp_convert rather than build_user_type_conversion because the
3212 latter returns NULL_TREE on failure, while the former gives an error. */
3214 if (IS_AGGR_TYPE (TREE_TYPE (arg2)) && real_lvalue_p (arg2))
3215 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3216 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3218 arg2 = decay_conversion (arg2);
3219 arg2_type = TREE_TYPE (arg2);
3221 if (IS_AGGR_TYPE (TREE_TYPE (arg3)) && real_lvalue_p (arg3))
3222 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3223 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3225 arg3 = decay_conversion (arg3);
3226 arg3_type = TREE_TYPE (arg3);
3228 if (arg2 == error_mark_node || arg3 == error_mark_node)
3229 return error_mark_node;
3233 After those conversions, one of the following shall hold:
3235 --The second and third operands have the same type; the result is of
3237 if (same_type_p (arg2_type, arg3_type))
3238 result_type = arg2_type;
3241 --The second and third operands have arithmetic or enumeration
3242 type; the usual arithmetic conversions are performed to bring
3243 them to a common type, and the result is of that type. */
3244 else if ((ARITHMETIC_TYPE_P (arg2_type)
3245 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3246 && (ARITHMETIC_TYPE_P (arg3_type)
3247 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3249 /* In this case, there is always a common type. */
3250 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3253 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3254 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3255 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3256 arg2_type, arg3_type);
3257 else if (extra_warnings
3258 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3259 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3260 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3261 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3262 warning ("enumeral and non-enumeral type in conditional expression");
3264 arg2 = perform_implicit_conversion (result_type, arg2);
3265 arg3 = perform_implicit_conversion (result_type, arg3);
3269 --The second and third operands have pointer type, or one has
3270 pointer type and the other is a null pointer constant; pointer
3271 conversions (_conv.ptr_) and qualification conversions
3272 (_conv.qual_) are performed to bring them to their composite
3273 pointer type (_expr.rel_). The result is of the composite
3276 --The second and third operands have pointer to member type, or
3277 one has pointer to member type and the other is a null pointer
3278 constant; pointer to member conversions (_conv.mem_) and
3279 qualification conversions (_conv.qual_) are performed to bring
3280 them to a common type, whose cv-qualification shall match the
3281 cv-qualification of either the second or the third operand.
3282 The result is of the common type. */
3283 else if ((null_ptr_cst_p (arg2)
3284 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3285 || TYPE_PTRMEMFUNC_P (arg3_type)))
3286 || (null_ptr_cst_p (arg3)
3287 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3288 || TYPE_PTRMEMFUNC_P (arg2_type)))
3289 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3290 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3291 || (TYPE_PTRMEMFUNC_P (arg2_type)
3292 && TYPE_PTRMEMFUNC_P (arg3_type)))
3294 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3295 arg3, "conditional expression");
3296 arg2 = perform_implicit_conversion (result_type, arg2);
3297 arg3 = perform_implicit_conversion (result_type, arg3);
3302 error ("operands to ?: have different types");
3303 return error_mark_node;
3307 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3308 /* Expand both sides into the same slot, hopefully the target of the
3309 ?: expression. We used to check for TARGET_EXPRs here, but now we
3310 sometimes wrap them in NOP_EXPRs so the test would fail. */
3311 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3312 result = build_target_expr_with_type (result, result_type);
3314 /* If this expression is an rvalue, but might be mistaken for an
3315 lvalue, we must add a NON_LVALUE_EXPR. */
3316 if (!lvalue_p && real_lvalue_p (result))
3317 result = build1 (NON_LVALUE_EXPR, result_type, result);
3323 build_new_op (code, flags, arg1, arg2, arg3)
3324 enum tree_code code;
3326 tree arg1, arg2, arg3;
3328 struct z_candidate *candidates = 0, *cand;
3329 tree fns, mem_arglist = NULL_TREE, arglist, fnname;
3330 enum tree_code code2 = NOP_EXPR;
3331 tree templates = NULL_TREE;
3333 bool viable_candidates;
3335 if (arg1 == error_mark_node
3336 || arg2 == error_mark_node
3337 || arg3 == error_mark_node)
3338 return error_mark_node;
3340 /* This can happen if a template takes all non-type parameters, e.g.
3341 undeclared_template<1, 5, 72>a; */
3342 if (code == LT_EXPR && TREE_CODE (arg1) == TEMPLATE_DECL)
3344 error ("`%D' must be declared before use", arg1);
3345 return error_mark_node;
3348 if (code == MODIFY_EXPR)
3350 code2 = TREE_CODE (arg3);
3352 fnname = ansi_assopname (code2);
3355 fnname = ansi_opname (code);
3357 if (TREE_CODE (arg1) == OFFSET_REF)
3358 arg1 = resolve_offset_ref (arg1);
3359 arg1 = convert_from_reference (arg1);
3365 case VEC_DELETE_EXPR:
3367 /* Use build_op_new_call and build_op_delete_call instead. */
3371 return build_object_call (arg1, arg2);
3379 if (TREE_CODE (arg2) == OFFSET_REF)
3380 arg2 = resolve_offset_ref (arg2);
3381 arg2 = convert_from_reference (arg2);
3385 if (TREE_CODE (arg3) == OFFSET_REF)
3386 arg3 = resolve_offset_ref (arg3);
3387 arg3 = convert_from_reference (arg3);
3390 if (code == COND_EXPR)
3392 if (arg2 == NULL_TREE
3393 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3394 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3395 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3396 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3399 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3400 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3403 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3404 arg2 = integer_zero_node;
3406 arglist = NULL_TREE;
3408 arglist = tree_cons (NULL_TREE, arg3, arglist);
3410 arglist = tree_cons (NULL_TREE, arg2, arglist);
3411 arglist = tree_cons (NULL_TREE, arg1, arglist);
3413 fns = lookup_function_nonclass (fnname, arglist);
3415 if (fns && TREE_CODE (fns) == TREE_LIST)
3416 fns = TREE_VALUE (fns);
3417 for (; fns; fns = OVL_NEXT (fns))
3419 tree fn = OVL_CURRENT (fns);
3420 if (TREE_CODE (fn) == TEMPLATE_DECL)
3422 templates = tree_cons (NULL_TREE, fn, templates);
3424 = add_template_candidate (candidates, fn, NULL_TREE, NULL_TREE,
3425 arglist, TREE_TYPE (fnname),
3426 /*access_path=*/NULL_TREE,
3427 /*conversion_path=*/NULL_TREE,
3428 flags, DEDUCE_CALL);
3431 candidates = add_function_candidate (candidates, fn, NULL_TREE,
3433 /*access_path=*/NULL_TREE,
3434 /*conversion_path=*/NULL_TREE,
3438 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3440 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3441 if (fns == error_mark_node)
3449 tree conversion_path = BASELINK_BINFO (fns);
3451 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3452 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
3454 tree fn = OVL_CURRENT (fns);
3456 tree access_path = TYPE_BINFO (TREE_TYPE (arg1));
3458 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3459 this_arglist = mem_arglist;
3461 this_arglist = arglist;
3463 if (TREE_CODE (fn) == TEMPLATE_DECL)
3465 /* A member template. */
3466 templates = tree_cons (NULL_TREE, fn, templates);
3468 = add_template_candidate (candidates, fn,
3469 BINFO_TYPE (conversion_path),
3471 this_arglist, TREE_TYPE (fnname),
3472 access_path, conversion_path,
3473 flags, DEDUCE_CALL);
3476 candidates = add_function_candidate
3477 (candidates, fn, BINFO_TYPE (conversion_path), this_arglist,
3478 access_path, conversion_path, flags);
3485 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3486 to know about two args; a builtin candidate will always have a first
3487 parameter of type bool. We'll handle that in
3488 build_builtin_candidate. */
3489 if (code == COND_EXPR)
3499 args[2] = NULL_TREE;
3502 candidates = add_builtin_candidates
3503 (candidates, code, code2, fnname, args, flags);
3510 /* For these, the built-in candidates set is empty
3511 [over.match.oper]/3. We don't want non-strict matches
3512 because exact matches are always possible with built-in
3513 operators. The built-in candidate set for COMPONENT_REF
3514 would be empty too, but since there are no such built-in
3515 operators, we accept non-strict matches for them. */
3516 viable_candidates = any_strictly_viable (candidates);
3520 viable_candidates = any_viable (candidates);
3524 if (! viable_candidates)
3528 case POSTINCREMENT_EXPR:
3529 case POSTDECREMENT_EXPR:
3530 /* Look for an `operator++ (int)'. If they didn't have
3531 one, then we fall back to the old way of doing things. */
3532 if (flags & LOOKUP_COMPLAIN)
3533 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3535 operator_name_info[code].name);
3536 if (code == POSTINCREMENT_EXPR)
3537 code = PREINCREMENT_EXPR;
3539 code = PREDECREMENT_EXPR;
3540 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3542 /* The caller will deal with these. */
3551 if (flags & LOOKUP_COMPLAIN)
3553 op_error (code, code2, arg1, arg2, arg3, "no match");
3554 print_z_candidates (candidates);
3556 return error_mark_node;
3558 candidates = splice_viable (candidates);
3559 cand = tourney (candidates);
3563 if (flags & LOOKUP_COMPLAIN)
3565 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3566 print_z_candidates (candidates);
3568 return error_mark_node;
3571 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3573 extern int warn_synth;
3575 && fnname == ansi_assopname (NOP_EXPR)
3576 && DECL_ARTIFICIAL (cand->fn)
3578 && ! candidates->next->next)
3580 warning ("using synthesized `%#D' for copy assignment",
3582 cp_warning_at (" where cfront would use `%#D'",
3584 ? candidates->next->fn
3588 return build_over_call
3590 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3591 ? mem_arglist : arglist,
3595 /* Check for comparison of different enum types. */
3604 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3605 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3606 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3607 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3609 warning ("comparison between `%#T' and `%#T'",
3610 TREE_TYPE (arg1), TREE_TYPE (arg2));
3617 /* We need to strip any leading REF_BIND so that bitfields don't cause
3618 errors. This should not remove any important conversions, because
3619 builtins don't apply to class objects directly. */
3620 conv = TREE_VEC_ELT (cand->convs, 0);
3621 if (TREE_CODE (conv) == REF_BIND)
3622 conv = TREE_OPERAND (conv, 0);
3623 arg1 = convert_like (conv, arg1);
3626 conv = TREE_VEC_ELT (cand->convs, 1);
3627 if (TREE_CODE (conv) == REF_BIND)
3628 conv = TREE_OPERAND (conv, 0);
3629 arg2 = convert_like (conv, arg2);
3633 conv = TREE_VEC_ELT (cand->convs, 2);
3634 if (TREE_CODE (conv) == REF_BIND)
3635 conv = TREE_OPERAND (conv, 0);
3636 arg3 = convert_like (conv, arg3);
3643 return build_modify_expr (arg1, code2, arg2);
3646 return build_indirect_ref (arg1, "unary *");
3651 case TRUNC_DIV_EXPR:
3662 case TRUNC_MOD_EXPR:
3666 case TRUTH_ANDIF_EXPR:
3667 case TRUTH_ORIF_EXPR:
3668 return cp_build_binary_op (code, arg1, arg2);
3673 case TRUTH_NOT_EXPR:
3674 case PREINCREMENT_EXPR:
3675 case POSTINCREMENT_EXPR:
3676 case PREDECREMENT_EXPR:
3677 case POSTDECREMENT_EXPR:
3680 return build_unary_op (code, arg1, candidates != 0);
3683 return build_array_ref (arg1, arg2);
3686 return build_conditional_expr (arg1, arg2, arg3);
3689 return build_m_component_ref
3690 (build_indirect_ref (arg1, NULL), arg2);
3692 /* The caller will deal with these. */
3704 /* Build a call to operator delete. This has to be handled very specially,
3705 because the restrictions on what signatures match are different from all
3706 other call instances. For a normal delete, only a delete taking (void *)
3707 or (void *, size_t) is accepted. For a placement delete, only an exact
3708 match with the placement new is accepted.
3710 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3711 ADDR is the pointer to be deleted.
3712 SIZE is the size of the memory block to be deleted.
3713 FLAGS are the usual overloading flags.
3714 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3717 build_op_delete_call (code, addr, size, flags, placement)
3718 enum tree_code code;
3719 tree addr, size, placement;
3722 tree fn = NULL_TREE;
3723 tree fns, fnname, fntype, argtypes, args, type;
3726 if (addr == error_mark_node)
3727 return error_mark_node;
3729 type = TREE_TYPE (TREE_TYPE (addr));
3730 while (TREE_CODE (type) == ARRAY_TYPE)
3731 type = TREE_TYPE (type);
3733 fnname = ansi_opname (code);
3735 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3738 If the result of the lookup is ambiguous or inaccessible, or if
3739 the lookup selects a placement deallocation function, the
3740 program is ill-formed.
3742 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3744 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3745 if (fns == error_mark_node)
3746 return error_mark_node;
3751 if (fns == NULL_TREE)
3752 fns = lookup_name_nonclass (fnname);
3759 /* Find the allocation function that is being called. */
3760 call_expr = placement;
3761 /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3763 while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3764 call_expr = TREE_OPERAND (call_expr, 1);
3765 /* Extract the function. */
3766 alloc_fn = get_callee_fndecl (call_expr);
3767 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3768 /* Then the second parm type. */
3769 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3770 /* Also the second argument. */
3771 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3775 /* First try it without the size argument. */
3776 argtypes = void_list_node;
3780 /* Strip const and volatile from addr. */
3781 addr = cp_convert (ptr_type_node, addr);
3783 /* We make two tries at finding a matching `operator delete'. On
3784 the first pass, we look for an one-operator (or placement)
3785 operator delete. If we're not doing placement delete, then on
3786 the second pass we look for a two-argument delete. */
3787 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3790 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3792 /* Normal delete; now try to find a match including the size
3794 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3795 tree_cons (NULL_TREE, sizetype,
3797 fntype = build_function_type (void_type_node, argtypes);
3799 /* Go through the `operator delete' functions looking for one
3800 with a matching type. */
3801 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3807 /* Exception specifications on the `delete' operator do not
3809 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3811 /* We also don't compare attributes. We're really just
3812 trying to check the types of the first two parameters. */
3813 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3817 /* If we found a match, we're done. */
3822 /* If we have a matching function, call it. */
3825 /* Make sure we have the actual function, and not an
3827 fn = OVL_CURRENT (fn);
3829 /* If the FN is a member function, make sure that it is
3831 if (DECL_CLASS_SCOPE_P (fn))
3832 enforce_access (type, fn);
3835 args = tree_cons (NULL_TREE, addr, args);
3837 args = tree_cons (NULL_TREE, addr,
3838 build_tree_list (NULL_TREE, size));
3840 return build_function_call (fn, args);
3843 /* If we are doing placement delete we do nothing if we don't find a
3844 matching op delete. */
3848 error ("no suitable `operator delete' for `%T'", type);
3849 return error_mark_node;
3852 /* If the current scope isn't allowed to access DECL along
3853 BASETYPE_PATH, give an error. The most derived class in
3854 BASETYPE_PATH is the one used to qualify DECL. */
3857 enforce_access (basetype_path, decl)
3863 accessible = accessible_p (basetype_path, decl);
3866 if (TREE_PRIVATE (decl))
3867 cp_error_at ("`%+#D' is private", decl);
3868 else if (TREE_PROTECTED (decl))
3869 cp_error_at ("`%+#D' is protected", decl);
3871 cp_error_at ("`%+#D' is inaccessible", decl);
3872 error ("within this context");
3879 /* Perform the conversions in CONVS on the expression EXPR.
3880 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3881 indicates the `this' argument of a method. INNER is nonzero when
3882 being called to continue a conversion chain. It is negative when a
3883 reference binding will be applied, positive otherwise. */
3886 convert_like_real (convs, expr, fn, argnum, inner)
3894 tree totype = TREE_TYPE (convs);
3896 if (ICS_BAD_FLAG (convs)
3897 && TREE_CODE (convs) != USER_CONV
3898 && TREE_CODE (convs) != AMBIG_CONV
3899 && TREE_CODE (convs) != REF_BIND)
3902 for (; t; t = TREE_OPERAND (t, 0))
3904 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3906 expr = convert_like_real (t, expr, fn, argnum, 1);
3909 else if (TREE_CODE (t) == AMBIG_CONV)
3910 return convert_like_real (t, expr, fn, argnum, 1);
3911 else if (TREE_CODE (t) == IDENTITY_CONV)
3914 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3916 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3917 return cp_convert (totype, expr);
3921 expr = dubious_conversion_warnings
3922 (totype, expr, "argument", fn, argnum);
3923 switch (TREE_CODE (convs))
3927 struct z_candidate *cand
3928 = WRAPPER_ZC (TREE_OPERAND (convs, 1));
3929 tree convfn = cand->fn;
3932 if (DECL_CONSTRUCTOR_P (convfn))
3934 tree t = build_int_2 (0, 0);
3935 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3937 args = build_tree_list (NULL_TREE, expr);
3938 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3939 || DECL_HAS_VTT_PARM_P (convfn))
3940 /* We should never try to call the abstract or base constructor
3943 args = tree_cons (NULL_TREE, t, args);
3946 args = build_this (expr);
3947 expr = build_over_call (cand, args, LOOKUP_NORMAL);
3949 /* If this is a constructor or a function returning an aggr type,
3950 we need to build up a TARGET_EXPR. */
3951 if (DECL_CONSTRUCTOR_P (convfn))
3952 expr = build_cplus_new (totype, expr);
3954 /* The result of the call is then used to direct-initialize the object
3955 that is the destination of the copy-initialization. [dcl.init]
3957 Note that this step is not reflected in the conversion sequence;
3958 it affects the semantics when we actually perform the
3959 conversion, but is not considered during overload resolution.
3961 If the target is a class, that means call a ctor. */
3962 if (IS_AGGR_TYPE (totype)
3963 && (inner >= 0 || !lvalue_p (expr)))
3965 savew = warningcount, savee = errorcount;
3966 expr = build_special_member_call
3967 (NULL_TREE, complete_ctor_identifier,
3968 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3969 /* Core issue 84, now a DR, says that we don't allow UDCs
3970 for these args (which deliberately breaks copy-init of an
3971 auto_ptr<Base> from an auto_ptr<Derived>). */
3972 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3974 /* Tell the user where this failing constructor call came from. */
3977 if (warningcount > savew)
3979 (" initializing argument %P of `%D' from result of `%D'",
3980 argnum, fn, convfn);
3981 else if (errorcount > savee)
3983 (" initializing argument %P of `%D' from result of `%D'",
3984 argnum, fn, convfn);
3988 if (warningcount > savew)
3989 warning (" initializing temporary from result of `%D'",
3991 else if (errorcount > savee)
3992 error (" initializing temporary from result of `%D'",
3995 expr = build_cplus_new (totype, expr);
4000 if (type_unknown_p (expr))
4001 expr = instantiate_type (totype, expr, tf_error | tf_warning);
4004 /* Call build_user_type_conversion again for the error. */
4005 return build_user_type_conversion
4006 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
4012 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
4013 TREE_CODE (convs) == REF_BIND ? -1 : 1);
4014 if (expr == error_mark_node)
4015 return error_mark_node;
4017 /* Convert a non-array constant variable to its underlying value, unless we
4018 are about to bind it to a reference, in which case we need to
4019 leave it as an lvalue. */
4020 if (TREE_CODE (convs) != REF_BIND
4021 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4022 expr = decl_constant_value (expr);
4024 switch (TREE_CODE (convs))
4027 if (! IS_AGGR_TYPE (totype))
4029 /* else fall through */
4031 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4033 /* We are going to bind a reference directly to a base-class
4034 subobject of EXPR. */
4035 tree base_ptr = build_pointer_type (totype);
4037 /* Build an expression for `*((base*) &expr)'. */
4038 expr = build_unary_op (ADDR_EXPR, expr, 0);
4039 expr = perform_implicit_conversion (base_ptr, expr);
4040 expr = build_indirect_ref (expr, "implicit conversion");
4044 /* Copy-initialization where the cv-unqualified version of the source
4045 type is the same class as, or a derived class of, the class of the
4046 destination [is treated as direct-initialization]. [dcl.init] */
4047 savew = warningcount, savee = errorcount;
4048 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4049 build_tree_list (NULL_TREE, expr),
4050 TYPE_BINFO (totype),
4051 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
4054 if (warningcount > savew)
4055 warning (" initializing argument %P of `%D'", argnum, fn);
4056 else if (errorcount > savee)
4057 error (" initializing argument %P of `%D'", argnum, fn);
4059 return build_cplus_new (totype, expr);
4063 tree ref_type = totype;
4065 /* If necessary, create a temporary. */
4066 if (NEED_TEMPORARY_P (convs) || !non_cast_lvalue_p (expr))
4068 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4069 expr = build_target_expr_with_type (expr, type);
4072 /* Take the address of the thing to which we will bind the
4074 expr = build_unary_op (ADDR_EXPR, expr, 1);
4075 if (expr == error_mark_node)
4076 return error_mark_node;
4078 /* Convert it to a pointer to the type referred to by the
4079 reference. This will adjust the pointer if a derived to
4080 base conversion is being performed. */
4081 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4083 /* Convert the pointer to the desired reference type. */
4084 expr = build1 (NOP_EXPR, ref_type, expr);
4090 return decay_conversion (expr);
4093 /* Warn about deprecated conversion if appropriate. */
4094 string_conv_p (totype, expr, 1);
4100 return ocp_convert (totype, expr, CONV_IMPLICIT,
4101 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4104 /* Build a call to __builtin_trap which can be used in an expression. */
4107 call_builtin_trap ()
4109 tree fn = get_identifier ("__builtin_trap");
4110 if (IDENTIFIER_GLOBAL_VALUE (fn))
4111 fn = IDENTIFIER_GLOBAL_VALUE (fn);
4115 fn = build_call (fn, NULL_TREE);
4116 fn = build (COMPOUND_EXPR, integer_type_node, fn, integer_zero_node);
4120 /* ARG is being passed to a varargs function. Perform any conversions
4121 required. Array/function to pointer decay must have already happened.
4122 Return the converted value. */
4125 convert_arg_to_ellipsis (arg)
4128 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4129 && (TYPE_PRECISION (TREE_TYPE (arg))
4130 < TYPE_PRECISION (double_type_node)))
4131 /* Convert `float' to `double'. */
4132 arg = cp_convert (double_type_node, arg);
4134 /* Convert `short' and `char' to full-size `int'. */
4135 arg = default_conversion (arg);
4137 arg = require_complete_type (arg);
4139 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4141 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4142 here and do a bitwise copy, but now cp_expr_size will abort if we
4144 warning ("cannot pass objects of non-POD type `%#T' through `...'; \
4145 call will abort at runtime",
4147 arg = call_builtin_trap ();
4153 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4156 build_x_va_arg (expr, type)
4160 if (processing_template_decl)
4161 return build_min (VA_ARG_EXPR, type, expr);
4163 type = complete_type_or_else (type, NULL_TREE);
4165 if (expr == error_mark_node || !type)
4166 return error_mark_node;
4168 if (! pod_type_p (type))
4170 /* Undefined behavior [expr.call] 5.2.2/7. */
4171 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4175 return build_va_arg (expr, type);
4178 /* TYPE has been given to va_arg. Apply the default conversions which
4179 would have happened when passed via ellipsis. Return the promoted
4180 type, or the passed type if there is no change. */
4183 cxx_type_promotes_to (type)
4188 if (TREE_CODE (type) == ARRAY_TYPE)
4189 return build_pointer_type (TREE_TYPE (type));
4191 if (TREE_CODE (type) == FUNCTION_TYPE)
4192 return build_pointer_type (type);
4194 promote = type_promotes_to (type);
4195 if (same_type_p (type, promote))
4201 /* ARG is a default argument expression being passed to a parameter of
4202 the indicated TYPE, which is a parameter to FN. Do any required
4203 conversions. Return the converted value. */
4206 convert_default_arg (type, arg, fn, parmnum)
4212 if (TREE_CODE (arg) == DEFAULT_ARG)
4214 /* When processing the default args for a class, we can find that
4215 there is an ordering constraint, and we call a function who's
4216 default args have not yet been converted. For instance,
4219 void Foo (A const & = A ());
4221 We must process A::A before A::Foo's default arg can be converted.
4222 Remember the dependent function, so do_pending_defargs can retry,
4224 unprocessed_defarg_fn (fn);
4226 /* Don't return error_mark node, as we won't be able to distinguish
4227 genuine errors from this case, and that would lead to repeated
4228 diagnostics. Just make something of the right type. */
4229 return build1 (NOP_EXPR, type, integer_zero_node);
4232 if (fn && DECL_TEMPLATE_INFO (fn))
4233 arg = tsubst_default_argument (fn, type, arg);
4235 arg = break_out_target_exprs (arg);
4237 if (TREE_CODE (arg) == CONSTRUCTOR)
4239 arg = digest_init (type, arg, 0);
4240 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4241 "default argument", fn, parmnum);
4245 /* This could get clobbered by the following call. */
4246 if (TREE_HAS_CONSTRUCTOR (arg))
4247 arg = copy_node (arg);
4249 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4250 "default argument", fn, parmnum);
4251 arg = convert_for_arg_passing (type, arg);
4257 /* Returns the type which will really be used for passing an argument of
4261 type_passed_as (type)
4264 /* Pass classes with copy ctors by invisible reference. */
4265 if (TREE_ADDRESSABLE (type))
4266 type = build_reference_type (type);
4267 else if (PROMOTE_PROTOTYPES
4268 && INTEGRAL_TYPE_P (type)
4269 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4270 type = integer_type_node;
4275 /* Actually perform the appropriate conversion. */
4278 convert_for_arg_passing (type, val)
4281 if (val == error_mark_node)
4283 /* Pass classes with copy ctors by invisible reference. */
4284 else if (TREE_ADDRESSABLE (type))
4285 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4286 else if (PROMOTE_PROTOTYPES
4287 && INTEGRAL_TYPE_P (type)
4288 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4289 val = default_conversion (val);
4293 /* Subroutine of the various build_*_call functions. Overload resolution
4294 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4295 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4296 bitmask of various LOOKUP_* flags which apply to the call itself. */
4299 build_over_call (cand, args, flags)
4300 struct z_candidate *cand;
4305 tree convs = cand->convs;
4306 tree converted_args = NULL_TREE;
4307 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4308 tree conv, arg, val;
4312 /* Give any warnings we noticed during overload resolution. */
4314 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4315 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4317 if (DECL_FUNCTION_MEMBER_P (fn))
4318 enforce_access (cand->access_path, fn);
4320 if (args && TREE_CODE (args) != TREE_LIST)
4321 args = build_tree_list (NULL_TREE, args);
4324 /* The implicit parameters to a constructor are not considered by overload
4325 resolution, and must be of the proper type. */
4326 if (DECL_CONSTRUCTOR_P (fn))
4328 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4329 arg = TREE_CHAIN (arg);
4330 parm = TREE_CHAIN (parm);
4331 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4332 /* We should never try to call the abstract constructor. */
4334 if (DECL_HAS_VTT_PARM_P (fn))
4336 converted_args = tree_cons
4337 (NULL_TREE, TREE_VALUE (arg), converted_args);
4338 arg = TREE_CHAIN (arg);
4339 parm = TREE_CHAIN (parm);
4342 /* Bypass access control for 'this' parameter. */
4343 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4345 tree parmtype = TREE_VALUE (parm);
4346 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4350 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4351 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4352 TREE_TYPE (argtype), fn);
4354 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4355 X is called for an object that is not of type X, or of a type
4356 derived from X, the behavior is undefined.
4358 So we can assume that anything passed as 'this' is non-null, and
4359 optimize accordingly. */
4360 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4361 /* Convert to the base in which the function was declared. */
4362 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4363 converted_arg = build_base_path (PLUS_EXPR,
4365 cand->conversion_path,
4367 /* If fn was found by a using declaration, the conversion path
4368 will be to the derived class, not the base declaring fn. We
4369 must convert from derived to base. */
4370 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4371 TREE_TYPE (parmtype), ba_ignore, NULL);
4373 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4376 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4377 parm = TREE_CHAIN (parm);
4378 arg = TREE_CHAIN (arg);
4384 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4386 tree type = TREE_VALUE (parm);
4388 conv = TREE_VEC_ELT (convs, i);
4389 val = convert_like_with_context
4390 (conv, TREE_VALUE (arg), fn, i - is_method);
4392 val = convert_for_arg_passing (type, val);
4393 converted_args = tree_cons (NULL_TREE, val, converted_args);
4396 /* Default arguments */
4397 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4399 = tree_cons (NULL_TREE,
4400 convert_default_arg (TREE_VALUE (parm),
4401 TREE_PURPOSE (parm),
4406 for (; arg; arg = TREE_CHAIN (arg))
4408 = tree_cons (NULL_TREE,
4409 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4412 converted_args = nreverse (converted_args);
4415 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4418 /* Avoid actually calling copy constructors and copy assignment operators,
4421 if (! flag_elide_constructors)
4422 /* Do things the hard way. */;
4423 else if (TREE_VEC_LENGTH (convs) == 1
4424 && DECL_COPY_CONSTRUCTOR_P (fn))
4427 arg = skip_artificial_parms_for (fn, converted_args);
4428 arg = TREE_VALUE (arg);
4430 /* Pull out the real argument, disregarding const-correctness. */
4432 while (TREE_CODE (targ) == NOP_EXPR
4433 || TREE_CODE (targ) == NON_LVALUE_EXPR
4434 || TREE_CODE (targ) == CONVERT_EXPR)
4435 targ = TREE_OPERAND (targ, 0);
4436 if (TREE_CODE (targ) == ADDR_EXPR)
4438 targ = TREE_OPERAND (targ, 0);
4439 if (!same_type_ignoring_top_level_qualifiers_p
4440 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4449 arg = build_indirect_ref (arg, 0);
4451 /* [class.copy]: the copy constructor is implicitly defined even if
4452 the implementation elided its use. */
4453 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4456 /* If we're creating a temp and we already have one, don't create a
4457 new one. If we're not creating a temp but we get one, use
4458 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4459 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4460 temp or an INIT_EXPR otherwise. */
4461 if (integer_zerop (TREE_VALUE (args)))
4463 if (TREE_CODE (arg) == TARGET_EXPR)
4465 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4466 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4468 else if (TREE_CODE (arg) == TARGET_EXPR
4469 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4472 tree to = stabilize_reference
4473 (build_indirect_ref (TREE_VALUE (args), 0));
4475 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4476 address = build_unary_op (ADDR_EXPR, val, 0);
4477 /* Avoid a warning about this expression, if the address is
4479 TREE_USED (address) = 1;
4483 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4485 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4487 tree to = stabilize_reference
4488 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4490 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4491 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4497 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4499 tree t, *p = &TREE_VALUE (converted_args);
4500 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4503 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4505 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4506 if (TREE_SIDE_EFFECTS (*p))
4507 *p = save_expr (*p);
4508 t = build_pointer_type (TREE_TYPE (fn));
4509 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4510 fn = build_java_interface_fn_ref (fn, *p);
4512 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4515 else if (DECL_INLINE (fn))
4516 fn = inline_conversion (fn);
4518 fn = build_addr_func (fn);
4520 /* Recognize certain built-in functions so we can make tree-codes
4521 other than CALL_EXPR. We do this when it enables fold-const.c
4522 to do something useful. */
4524 if (TREE_CODE (fn) == ADDR_EXPR
4525 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4526 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4529 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4534 /* Some built-in function calls will be evaluated at
4535 compile-time in fold (). */
4536 fn = fold (build_call (fn, converted_args));
4537 if (VOID_TYPE_P (TREE_TYPE (fn)))
4539 fn = require_complete_type (fn);
4540 if (fn == error_mark_node)
4541 return error_mark_node;
4542 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4543 fn = build_cplus_new (TREE_TYPE (fn), fn);
4544 return convert_from_reference (fn);
4547 static GTY(()) tree java_iface_lookup_fn;
4549 /* Make an expression which yields the address of the Java interface
4550 method FN. This is achieved by generating a call to libjava's
4551 _Jv_LookupInterfaceMethodIdx(). */
4554 build_java_interface_fn_ref (fn, instance)
4557 tree lookup_args, lookup_fn, method, idx;
4558 tree klass_ref, iface, iface_ref;
4561 if (!java_iface_lookup_fn)
4563 tree endlink = build_void_list_node ();
4564 tree t = tree_cons (NULL_TREE, ptr_type_node,
4565 tree_cons (NULL_TREE, ptr_type_node,
4566 tree_cons (NULL_TREE, java_int_type_node,
4568 java_iface_lookup_fn
4569 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4570 build_function_type (ptr_type_node, t),
4571 0, NOT_BUILT_IN, NULL, NULL_TREE);
4574 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4575 This is the first entry in the vtable. */
4576 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4579 /* Get the java.lang.Class pointer for the interface being called. */
4580 iface = DECL_CONTEXT (fn);
4581 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, 0);
4582 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4583 || DECL_CONTEXT (iface_ref) != iface)
4585 error ("could not find class$ field in java interface type `%T'",
4587 return error_mark_node;
4589 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4591 /* Determine the itable index of FN. */
4593 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4595 if (!DECL_VIRTUAL_P (method))
4601 idx = build_int_2 (i, 0);
4603 lookup_args = tree_cons (NULL_TREE, klass_ref,
4604 tree_cons (NULL_TREE, iface_ref,
4605 build_tree_list (NULL_TREE, idx)));
4606 lookup_fn = build1 (ADDR_EXPR,
4607 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4608 java_iface_lookup_fn);
4609 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4612 /* Returns the value to use for the in-charge parameter when making a
4613 call to a function with the indicated NAME. */
4616 in_charge_arg_for_name (name)
4619 if (name == base_ctor_identifier
4620 || name == base_dtor_identifier)
4621 return integer_zero_node;
4622 else if (name == complete_ctor_identifier)
4623 return integer_one_node;
4624 else if (name == complete_dtor_identifier)
4625 return integer_two_node;
4626 else if (name == deleting_dtor_identifier)
4627 return integer_three_node;
4629 /* This function should only be called with one of the names listed
4635 /* Build a call to a constructor, destructor, or an assignment
4636 operator for INSTANCE, an expression with class type. NAME
4637 indicates the special member function to call; ARGS are the
4638 arguments. BINFO indicates the base of INSTANCE that is to be
4639 passed as the `this' parameter to the member function called.
4641 FLAGS are the LOOKUP_* flags to use when processing the call.
4643 If NAME indicates a complete object constructor, INSTANCE may be
4644 NULL_TREE. In this case, the caller will call build_cplus_new to
4645 store the newly constructed object into a VAR_DECL. */
4648 build_special_member_call (tree instance, tree name, tree args,
4649 tree binfo, int flags)
4652 /* The type of the subobject to be constructed or destroyed. */
4655 my_friendly_assert (name == complete_ctor_identifier
4656 || name == base_ctor_identifier
4657 || name == complete_dtor_identifier
4658 || name == base_dtor_identifier
4659 || name == deleting_dtor_identifier
4660 || name == ansi_assopname (NOP_EXPR),
4662 my_friendly_assert (binfo != NULL_TREE, 20020712);
4664 class_type = BINFO_TYPE (binfo);
4666 /* Handle the special case where INSTANCE is NULL_TREE. */
4667 if (name == complete_ctor_identifier && !instance)
4669 instance = build_int_2 (0, 0);
4670 TREE_TYPE (instance) = build_pointer_type (class_type);
4671 instance = build1 (INDIRECT_REF, class_type, instance);
4673 else if (name == complete_dtor_identifier
4674 || name == base_dtor_identifier
4675 || name == deleting_dtor_identifier)
4676 my_friendly_assert (args == NULL_TREE, 20020712);
4678 my_friendly_assert (instance != NULL_TREE, 20020712);
4680 /* Resolve the name. */
4681 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4682 return error_mark_node;
4684 fns = lookup_fnfields (binfo, name, 1);
4686 /* When making a call to a constructor or destructor for a subobject
4687 that uses virtual base classes, pass down a pointer to a VTT for
4689 if ((name == base_ctor_identifier
4690 || name == base_dtor_identifier)
4691 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4696 /* If the current function is a complete object constructor
4697 or destructor, then we fetch the VTT directly.
4698 Otherwise, we look it up using the VTT we were given. */
4699 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4700 vtt = decay_conversion (vtt);
4701 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4702 build (EQ_EXPR, boolean_type_node,
4703 current_in_charge_parm, integer_zero_node),
4706 if (TREE_VIA_VIRTUAL (binfo))
4707 binfo = binfo_for_vbase (class_type, current_class_type);
4708 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4709 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4710 BINFO_SUBVTT_INDEX (binfo));
4712 args = tree_cons (NULL_TREE, sub_vtt, args);
4715 return build_new_method_call (instance, fns, args, binfo, flags);
4718 /* Build a call to "INSTANCE.FN (ARGS)". */
4721 build_new_method_call (tree instance, tree fns, tree args,
4722 tree conversion_path, int flags)
4724 struct z_candidate *candidates = 0, *cand;
4725 tree explicit_targs = NULL_TREE;
4726 tree basetype = NULL_TREE;
4729 tree mem_args = NULL_TREE, instance_ptr;
4730 tree name, pretty_name;
4732 tree templates = NULL_TREE;
4734 int template_only = 0;
4736 my_friendly_assert (instance != NULL_TREE, 20020729);
4738 if (instance == error_mark_node || fns == error_mark_node
4739 || args == error_mark_node)
4740 return error_mark_node;
4742 /* Process the argument list. */
4744 args = resolve_args (args);
4745 if (args == error_mark_node)
4746 return error_mark_node;
4748 if (TREE_CODE (instance) == OFFSET_REF)
4749 instance = resolve_offset_ref (instance);
4750 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4751 instance = convert_from_reference (instance);
4752 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4753 instance_ptr = build_this (instance);
4755 if (!BASELINK_P (fns))
4757 call = build_field_call (instance_ptr, fns, args);
4760 error ("call to non-function `%D'", fns);
4761 return error_mark_node;
4764 if (!conversion_path)
4765 conversion_path = BASELINK_BINFO (fns);
4766 access_binfo = BASELINK_ACCESS_BINFO (fns);
4767 optype = BASELINK_OPTYPE (fns);
4768 fns = BASELINK_FUNCTIONS (fns);
4770 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4772 explicit_targs = TREE_OPERAND (fns, 1);
4773 fns = TREE_OPERAND (fns, 0);
4777 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4778 || TREE_CODE (fns) == TEMPLATE_DECL
4779 || TREE_CODE (fns) == OVERLOAD,
4782 /* XXX this should be handled before we get here. */
4783 if (! IS_AGGR_TYPE (basetype))
4785 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4786 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4787 fns, instance, basetype);
4789 return error_mark_node;
4792 name = DECL_NAME (get_first_fn (fns));
4794 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4796 /* Callers should explicitly indicate whether they want to construct
4797 the complete object or just the part without virtual bases. */
4798 my_friendly_assert (name != ctor_identifier, 20000408);
4799 /* Similarly for destructors. */
4800 my_friendly_assert (name != dtor_identifier, 20000408);
4802 if (name == complete_ctor_identifier
4803 || name == base_ctor_identifier)
4804 pretty_name = constructor_name (basetype);
4806 pretty_name = dtor_identifier;
4814 tree class_type = (conversion_path
4815 ? BINFO_TYPE (conversion_path)
4818 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4819 for (fn = fns; fn; fn = OVL_NEXT (fn))
4821 tree t = OVL_CURRENT (fn);
4824 /* We can end up here for copy-init of same or base class. */
4825 if ((flags & LOOKUP_ONLYCONVERTING)
4826 && DECL_NONCONVERTING_P (t))
4829 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4830 this_arglist = mem_args;
4832 this_arglist = args;
4834 if (TREE_CODE (t) == TEMPLATE_DECL)
4836 /* A member template. */
4837 templates = tree_cons (NULL_TREE, t, templates);
4839 add_template_candidate (candidates, t,
4842 this_arglist, optype,
4848 else if (! template_only)
4849 candidates = add_function_candidate (candidates, t,
4858 if (! any_viable (candidates))
4860 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4861 if (flags & LOOKUP_SPECULATIVELY)
4863 if (!COMPLETE_TYPE_P (basetype))
4864 cxx_incomplete_type_error (instance_ptr, basetype);
4866 error ("no matching function for call to `%T::%D(%A)%#V'",
4867 basetype, pretty_name, user_args,
4868 TREE_TYPE (TREE_TYPE (instance_ptr)));
4869 print_z_candidates (candidates);
4870 return error_mark_node;
4872 candidates = splice_viable (candidates);
4873 cand = tourney (candidates);
4877 error ("call of overloaded `%D(%A)' is ambiguous", pretty_name,
4879 print_z_candidates (candidates);
4880 return error_mark_node;
4883 if (DECL_PURE_VIRTUAL_P (cand->fn)
4884 && instance == current_class_ref
4885 && (DECL_CONSTRUCTOR_P (current_function_decl)
4886 || DECL_DESTRUCTOR_P (current_function_decl))
4887 && ! (flags & LOOKUP_NONVIRTUAL)
4888 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4889 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4890 "abstract virtual `%#D' called from constructor"
4891 : "abstract virtual `%#D' called from destructor"),
4893 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4894 && is_dummy_object (instance_ptr))
4896 error ("cannot call member function `%D' without object", cand->fn);
4897 return error_mark_node;
4900 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4901 && resolves_to_fixed_type_p (instance, 0))
4902 flags |= LOOKUP_NONVIRTUAL;
4904 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4905 call = build_over_call (cand, mem_args, flags);
4908 call = build_over_call (cand, args, flags);
4909 /* In an expression of the form `a->f()' where `f' turns out to
4910 be a static member function, `a' is none-the-less evaluated. */
4911 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
4912 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4918 /* Returns nonzero iff standard conversion sequence ICS1 is a proper
4919 subsequence of ICS2. */
4922 is_subseq (ics1, ics2)
4925 /* We can assume that a conversion of the same code
4926 between the same types indicates a subsequence since we only get
4927 here if the types we are converting from are the same. */
4929 while (TREE_CODE (ics1) == RVALUE_CONV
4930 || TREE_CODE (ics1) == LVALUE_CONV)
4931 ics1 = TREE_OPERAND (ics1, 0);
4935 while (TREE_CODE (ics2) == RVALUE_CONV
4936 || TREE_CODE (ics2) == LVALUE_CONV)
4937 ics2 = TREE_OPERAND (ics2, 0);
4939 if (TREE_CODE (ics2) == USER_CONV
4940 || TREE_CODE (ics2) == AMBIG_CONV
4941 || TREE_CODE (ics2) == IDENTITY_CONV)
4942 /* At this point, ICS1 cannot be a proper subsequence of
4943 ICS2. We can get a USER_CONV when we are comparing the
4944 second standard conversion sequence of two user conversion
4948 ics2 = TREE_OPERAND (ics2, 0);
4950 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4951 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4952 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4953 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4958 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
4959 be any _TYPE nodes. */
4962 is_properly_derived_from (derived, base)
4966 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4967 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4970 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4971 considers every class derived from itself. */
4972 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4973 && DERIVED_FROM_P (base, derived));
4976 /* We build the ICS for an implicit object parameter as a pointer
4977 conversion sequence. However, such a sequence should be compared
4978 as if it were a reference conversion sequence. If ICS is the
4979 implicit conversion sequence for an implicit object parameter,
4980 modify it accordingly. */
4983 maybe_handle_implicit_object (ics)
4986 if (ICS_THIS_FLAG (*ics))
4988 /* [over.match.funcs]
4990 For non-static member functions, the type of the
4991 implicit object parameter is "reference to cv X"
4992 where X is the class of which the function is a
4993 member and cv is the cv-qualification on the member
4994 function declaration. */
4996 tree reference_type;
4998 /* The `this' parameter is a pointer to a class type. Make the
4999 implict conversion talk about a reference to that same class
5001 reference_type = TREE_TYPE (TREE_TYPE (*ics));
5002 reference_type = build_reference_type (reference_type);
5004 if (TREE_CODE (t) == QUAL_CONV)
5005 t = TREE_OPERAND (t, 0);
5006 if (TREE_CODE (t) == PTR_CONV)
5007 t = TREE_OPERAND (t, 0);
5008 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5009 t = direct_reference_binding (reference_type, t);
5014 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5015 and return the type to which the reference refers. Otherwise,
5016 leave *ICS unchanged and return NULL_TREE. */
5019 maybe_handle_ref_bind (ics)
5022 if (TREE_CODE (*ics) == REF_BIND)
5024 tree old_ics = *ics;
5025 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5026 *ics = TREE_OPERAND (old_ics, 0);
5027 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5028 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5035 /* Compare two implicit conversion sequences according to the rules set out in
5036 [over.ics.rank]. Return values:
5038 1: ics1 is better than ics2
5039 -1: ics2 is better than ics1
5040 0: ics1 and ics2 are indistinguishable */
5043 compare_ics (ics1, ics2)
5050 tree deref_from_type1 = NULL_TREE;
5051 tree deref_from_type2 = NULL_TREE;
5052 tree deref_to_type1 = NULL_TREE;
5053 tree deref_to_type2 = NULL_TREE;
5056 /* REF_BINDING is nonzero if the result of the conversion sequence
5057 is a reference type. In that case TARGET_TYPE is the
5058 type referred to by the reference. */
5062 /* Handle implicit object parameters. */
5063 maybe_handle_implicit_object (&ics1);
5064 maybe_handle_implicit_object (&ics2);
5066 /* Handle reference parameters. */
5067 target_type1 = maybe_handle_ref_bind (&ics1);
5068 target_type2 = maybe_handle_ref_bind (&ics2);
5072 When comparing the basic forms of implicit conversion sequences (as
5073 defined in _over.best.ics_)
5075 --a standard conversion sequence (_over.ics.scs_) is a better
5076 conversion sequence than a user-defined conversion sequence
5077 or an ellipsis conversion sequence, and
5079 --a user-defined conversion sequence (_over.ics.user_) is a
5080 better conversion sequence than an ellipsis conversion sequence
5081 (_over.ics.ellipsis_). */
5082 rank1 = ICS_RANK (ics1);
5083 rank2 = ICS_RANK (ics2);
5087 else if (rank1 < rank2)
5090 if (rank1 == BAD_RANK)
5092 /* XXX Isn't this an extension? */
5093 /* Both ICS are bad. We try to make a decision based on what
5094 would have happenned if they'd been good. */
5095 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5096 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5098 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5099 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5102 /* We couldn't make up our minds; try to figure it out below. */
5105 if (ICS_ELLIPSIS_FLAG (ics1))
5106 /* Both conversions are ellipsis conversions. */
5109 /* User-defined conversion sequence U1 is a better conversion sequence
5110 than another user-defined conversion sequence U2 if they contain the
5111 same user-defined conversion operator or constructor and if the sec-
5112 ond standard conversion sequence of U1 is better than the second
5113 standard conversion sequence of U2. */
5115 if (ICS_USER_FLAG (ics1))
5119 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5120 if (TREE_CODE (t1) == AMBIG_CONV)
5122 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5123 if (TREE_CODE (t2) == AMBIG_CONV)
5126 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5129 /* We can just fall through here, after setting up
5130 FROM_TYPE1 and FROM_TYPE2. */
5131 from_type1 = TREE_TYPE (t1);
5132 from_type2 = TREE_TYPE (t2);
5136 /* We're dealing with two standard conversion sequences.
5140 Standard conversion sequence S1 is a better conversion
5141 sequence than standard conversion sequence S2 if
5143 --S1 is a proper subsequence of S2 (comparing the conversion
5144 sequences in the canonical form defined by _over.ics.scs_,
5145 excluding any Lvalue Transformation; the identity
5146 conversion sequence is considered to be a subsequence of
5147 any non-identity conversion sequence */
5150 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5151 from_type1 = TREE_OPERAND (from_type1, 0);
5152 from_type1 = TREE_TYPE (from_type1);
5155 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5156 from_type2 = TREE_OPERAND (from_type2, 0);
5157 from_type2 = TREE_TYPE (from_type2);
5160 if (same_type_p (from_type1, from_type2))
5162 if (is_subseq (ics1, ics2))
5164 if (is_subseq (ics2, ics1))
5167 /* Otherwise, one sequence cannot be a subsequence of the other; they
5168 don't start with the same type. This can happen when comparing the
5169 second standard conversion sequence in two user-defined conversion
5176 --the rank of S1 is better than the rank of S2 (by the rules
5179 Standard conversion sequences are ordered by their ranks: an Exact
5180 Match is a better conversion than a Promotion, which is a better
5181 conversion than a Conversion.
5183 Two conversion sequences with the same rank are indistinguishable
5184 unless one of the following rules applies:
5186 --A conversion that is not a conversion of a pointer, or pointer
5187 to member, to bool is better than another conversion that is such
5190 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5191 so that we do not have to check it explicitly. */
5192 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5194 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5197 to_type1 = TREE_TYPE (ics1);
5198 to_type2 = TREE_TYPE (ics2);
5200 if (TYPE_PTR_P (from_type1)
5201 && TYPE_PTR_P (from_type2)
5202 && TYPE_PTR_P (to_type1)
5203 && TYPE_PTR_P (to_type2))
5205 deref_from_type1 = TREE_TYPE (from_type1);
5206 deref_from_type2 = TREE_TYPE (from_type2);
5207 deref_to_type1 = TREE_TYPE (to_type1);
5208 deref_to_type2 = TREE_TYPE (to_type2);
5210 /* The rules for pointers to members A::* are just like the rules
5211 for pointers A*, except opposite: if B is derived from A then
5212 A::* converts to B::*, not vice versa. For that reason, we
5213 switch the from_ and to_ variables here. */
5214 else if (TYPE_PTRMEM_P (from_type1)
5215 && TYPE_PTRMEM_P (from_type2)
5216 && TYPE_PTRMEM_P (to_type1)
5217 && TYPE_PTRMEM_P (to_type2))
5219 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5220 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5221 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5222 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5224 else if (TYPE_PTRMEMFUNC_P (from_type1)
5225 && TYPE_PTRMEMFUNC_P (from_type2)
5226 && TYPE_PTRMEMFUNC_P (to_type1)
5227 && TYPE_PTRMEMFUNC_P (to_type2))
5229 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5230 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5231 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5232 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5235 if (deref_from_type1 != NULL_TREE
5236 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5237 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5239 /* This was one of the pointer or pointer-like conversions.
5243 --If class B is derived directly or indirectly from class A,
5244 conversion of B* to A* is better than conversion of B* to
5245 void*, and conversion of A* to void* is better than
5246 conversion of B* to void*. */
5247 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5248 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5250 if (is_properly_derived_from (deref_from_type1,
5253 else if (is_properly_derived_from (deref_from_type2,
5257 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5258 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5260 if (same_type_p (deref_from_type1, deref_from_type2))
5262 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5264 if (is_properly_derived_from (deref_from_type1,
5268 /* We know that DEREF_TO_TYPE1 is `void' here. */
5269 else if (is_properly_derived_from (deref_from_type1,
5274 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5275 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5279 --If class B is derived directly or indirectly from class A
5280 and class C is derived directly or indirectly from B,
5282 --conversion of C* to B* is better than conversion of C* to
5285 --conversion of B* to A* is better than conversion of C* to
5287 if (same_type_p (deref_from_type1, deref_from_type2))
5289 if (is_properly_derived_from (deref_to_type1,
5292 else if (is_properly_derived_from (deref_to_type2,
5296 else if (same_type_p (deref_to_type1, deref_to_type2))
5298 if (is_properly_derived_from (deref_from_type2,
5301 else if (is_properly_derived_from (deref_from_type1,
5307 else if (CLASS_TYPE_P (non_reference (from_type1))
5308 && same_type_p (from_type1, from_type2))
5310 tree from = non_reference (from_type1);
5314 --binding of an expression of type C to a reference of type
5315 B& is better than binding an expression of type C to a
5316 reference of type A&
5318 --conversion of C to B is better than conversion of C to A, */
5319 if (is_properly_derived_from (from, to_type1)
5320 && is_properly_derived_from (from, to_type2))
5322 if (is_properly_derived_from (to_type1, to_type2))
5324 else if (is_properly_derived_from (to_type2, to_type1))
5328 else if (CLASS_TYPE_P (non_reference (to_type1))
5329 && same_type_p (to_type1, to_type2))
5331 tree to = non_reference (to_type1);
5335 --binding of an expression of type B to a reference of type
5336 A& is better than binding an expression of type C to a
5337 reference of type A&,
5339 --onversion of B to A is better than conversion of C to A */
5340 if (is_properly_derived_from (from_type1, to)
5341 && is_properly_derived_from (from_type2, to))
5343 if (is_properly_derived_from (from_type2, from_type1))
5345 else if (is_properly_derived_from (from_type1, from_type2))
5352 --S1 and S2 differ only in their qualification conversion and yield
5353 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5354 qualification signature of type T1 is a proper subset of the cv-
5355 qualification signature of type T2 */
5356 if (TREE_CODE (ics1) == QUAL_CONV
5357 && TREE_CODE (ics2) == QUAL_CONV
5358 && same_type_p (from_type1, from_type2))
5359 return comp_cv_qual_signature (to_type1, to_type2);
5363 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5364 types to which the references refer are the same type except for
5365 top-level cv-qualifiers, and the type to which the reference
5366 initialized by S2 refers is more cv-qualified than the type to
5367 which the reference initialized by S1 refers */
5369 if (target_type1 && target_type2
5370 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5371 return comp_cv_qualification (target_type2, target_type1);
5373 /* Neither conversion sequence is better than the other. */
5377 /* The source type for this standard conversion sequence. */
5383 for (;; t = TREE_OPERAND (t, 0))
5385 if (TREE_CODE (t) == USER_CONV
5386 || TREE_CODE (t) == AMBIG_CONV
5387 || TREE_CODE (t) == IDENTITY_CONV)
5388 return TREE_TYPE (t);
5393 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5394 a pointer to LOSER and re-running joust to produce the warning if WINNER
5395 is actually used. */
5398 add_warning (winner, loser)
5399 struct z_candidate *winner, *loser;
5401 winner->warnings = tree_cons (NULL_TREE,
5402 build_zc_wrapper (loser),
5406 /* Returns true iff functions are equivalent. Equivalent functions are
5407 not '==' only if one is a function-local extern function or if
5408 both are extern "C". */
5411 equal_functions (fn1, fn2)
5415 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5416 || DECL_EXTERN_C_FUNCTION_P (fn1))
5417 return decls_match (fn1, fn2);
5421 /* Compare two candidates for overloading as described in
5422 [over.match.best]. Return values:
5424 1: cand1 is better than cand2
5425 -1: cand2 is better than cand1
5426 0: cand1 and cand2 are indistinguishable */
5429 joust (cand1, cand2, warn)
5430 struct z_candidate *cand1, *cand2;
5434 int i, off1 = 0, off2 = 0, len;
5436 /* Candidates that involve bad conversions are always worse than those
5438 if (cand1->viable > cand2->viable)
5440 if (cand1->viable < cand2->viable)
5443 /* If we have two pseudo-candidates for conversions to the same type,
5444 or two candidates for the same function, arbitrarily pick one. */
5445 if (cand1->fn == cand2->fn
5446 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5449 /* a viable function F1
5450 is defined to be a better function than another viable function F2 if
5451 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5452 ICSi(F2), and then */
5454 /* for some argument j, ICSj(F1) is a better conversion sequence than
5457 /* For comparing static and non-static member functions, we ignore
5458 the implicit object parameter of the non-static function. The
5459 standard says to pretend that the static function has an object
5460 parm, but that won't work with operator overloading. */
5461 len = TREE_VEC_LENGTH (cand1->convs);
5462 if (len != TREE_VEC_LENGTH (cand2->convs))
5464 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5465 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5467 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5468 && DECL_STATIC_FUNCTION_P (cand2->fn))
5477 for (i = 0; i < len; ++i)
5479 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5480 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5481 int comp = compare_ics (t1, t2);
5486 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5487 && TREE_CODE (t1) == STD_CONV
5488 && TREE_CODE (t2) == STD_CONV
5489 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5490 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5491 && (TYPE_PRECISION (TREE_TYPE (t1))
5492 == TYPE_PRECISION (TREE_TYPE (t2)))
5493 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5494 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5497 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5499 struct z_candidate *w, *l;
5501 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5502 w = cand1, l = cand2;
5504 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5505 w = cand2, l = cand1;
5509 warning ("passing `%T' chooses `%T' over `%T'",
5510 type, type1, type2);
5511 warning (" in call to `%D'", w->fn);
5517 if (winner && comp != winner)
5526 /* warn about confusing overload resolution for user-defined conversions,
5527 either between a constructor and a conversion op, or between two
5529 if (winner && cand1->second_conv
5530 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5531 != DECL_CONSTRUCTOR_P (cand2->fn))
5532 /* Don't warn if the two conv ops convert to the same type... */
5533 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5534 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5535 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5537 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5540 struct z_candidate *w, *l;
5543 w = cand1, l = cand2;
5545 w = cand2, l = cand1;
5546 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5547 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5548 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5549 && (convn = standard_conversion
5550 (TREE_TYPE (TREE_TYPE (l->fn)),
5551 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5552 && TREE_CODE (convn) == QUAL_CONV)
5553 /* Don't complain about `operator char *()' beating
5554 `operator const char *() const'. */;
5557 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5558 if (! DECL_CONSTRUCTOR_P (w->fn))
5559 source = TREE_TYPE (source);
5560 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5561 warning (" for conversion from `%T' to `%T'",
5562 source, TREE_TYPE (w->second_conv));
5563 warning (" because conversion sequence for the argument is better");
5574 F1 is a non-template function and F2 is a template function
5577 if (! cand1->template && cand2->template)
5579 else if (cand1->template && ! cand2->template)
5583 F1 and F2 are template functions and the function template for F1 is
5584 more specialized than the template for F2 according to the partial
5587 if (cand1->template && cand2->template)
5589 winner = more_specialized
5590 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5592 /* Tell the deduction code how many real function arguments
5593 we saw, not counting the implicit 'this' argument. But,
5594 add_function_candidate() suppresses the "this" argument
5597 [temp.func.order]: The presence of unused ellipsis and default
5598 arguments has no effect on the partial ordering of function
5600 TREE_VEC_LENGTH (cand1->convs)
5601 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5602 - DECL_CONSTRUCTOR_P (cand1->fn)));
5609 the context is an initialization by user-defined conversion (see
5610 _dcl.init_ and _over.match.user_) and the standard conversion
5611 sequence from the return type of F1 to the destination type (i.e.,
5612 the type of the entity being initialized) is a better conversion
5613 sequence than the standard conversion sequence from the return type
5614 of F2 to the destination type. */
5616 if (cand1->second_conv)
5618 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5623 /* Check whether we can discard a builtin candidate, either because we
5624 have two identical ones or matching builtin and non-builtin candidates.
5626 (Pedantically in the latter case the builtin which matched the user
5627 function should not be added to the overload set, but we spot it here.
5630 ... the builtin candidates include ...
5631 - do not have the same parameter type list as any non-template
5632 non-member candidate. */
5634 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5635 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5637 for (i = 0; i < len; ++i)
5638 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5639 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5641 if (i == TREE_VEC_LENGTH (cand1->convs))
5643 if (cand1->fn == cand2->fn)
5644 /* Two built-in candidates; arbitrarily pick one. */
5646 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5647 /* cand1 is built-in; prefer cand2. */
5650 /* cand2 is built-in; prefer cand1. */
5655 /* If the two functions are the same (this can happen with declarations
5656 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5657 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5658 && equal_functions (cand1->fn, cand2->fn))
5663 /* Extension: If the worst conversion for one candidate is worse than the
5664 worst conversion for the other, take the first. */
5667 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5668 struct z_candidate *w = 0, *l = 0;
5670 for (i = 0; i < len; ++i)
5672 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5673 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5674 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5675 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5678 winner = 1, w = cand1, l = cand2;
5680 winner = -1, w = cand2, l = cand1;
5685 pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5687 " because worst conversion for the former is better than worst conversion for the latter");
5695 my_friendly_assert (!winner, 20010121);
5699 /* Given a list of candidates for overloading, find the best one, if any.
5700 This algorithm has a worst case of O(2n) (winner is last), and a best
5701 case of O(n/2) (totally ambiguous); much better than a sorting
5704 static struct z_candidate *
5705 tourney (candidates)
5706 struct z_candidate *candidates;
5708 struct z_candidate *champ = candidates, *challenger;
5710 int champ_compared_to_predecessor = 0;
5712 /* Walk through the list once, comparing each current champ to the next
5713 candidate, knocking out a candidate or two with each comparison. */
5715 for (challenger = champ->next; challenger; )
5717 fate = joust (champ, challenger, 0);
5719 challenger = challenger->next;
5724 champ = challenger->next;
5727 champ_compared_to_predecessor = 0;
5732 champ_compared_to_predecessor = 1;
5735 challenger = champ->next;
5739 /* Make sure the champ is better than all the candidates it hasn't yet
5740 been compared to. */
5742 for (challenger = candidates;
5744 && !(champ_compared_to_predecessor && challenger->next == champ);
5745 challenger = challenger->next)
5747 fate = joust (champ, challenger, 0);
5755 /* Returns nonzero if things of type FROM can be converted to TO. */
5758 can_convert (to, from)
5761 return can_convert_arg (to, from, NULL_TREE);
5764 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5767 can_convert_arg (to, from, arg)
5770 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5771 return (t && ! ICS_BAD_FLAG (t));
5774 /* Like can_convert_arg, but allows dubious conversions as well. */
5777 can_convert_arg_bad (to, from, arg)
5780 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5784 /* Convert EXPR to TYPE. Return the converted expression.
5786 Note that we allow bad conversions here because by the time we get to
5787 this point we are committed to doing the conversion. If we end up
5788 doing a bad conversion, convert_like will complain. */
5791 perform_implicit_conversion (type, expr)
5797 if (expr == error_mark_node)
5798 return error_mark_node;
5799 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5803 error ("could not convert `%E' to `%T'", expr, type);
5804 return error_mark_node;
5807 return convert_like (conv, expr);
5810 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5811 initializing a variable of that TYPE. Return the converted
5815 initialize_reference (type, expr)
5821 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5822 if (!conv || ICS_BAD_FLAG (conv))
5824 error ("could not convert `%E' to `%T'", expr, type);
5825 return error_mark_node;
5828 return convert_like (conv, expr);
5831 #include "gt-cp-call.h"